blob: 28effbecdab61cc3cb49d9ee38b563b42c154e99 [file] [log] [blame]
Jan Ceuleers0977f812012-06-05 03:42:12 +00001/* drivers/net/ethernet/freescale/gianfar.c
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
3 * Gianfar Ethernet Driver
Andy Fleming7f7f5312005-11-11 12:38:59 -06004 * This driver is designed for the non-CPM ethernet controllers
5 * on the 85xx and 83xx family of integrated processors
Linus Torvalds1da177e2005-04-16 15:20:36 -07006 * Based on 8260_io/fcc_enet.c
7 *
8 * Author: Andy Fleming
Kumar Gala4c8d3d92005-11-13 16:06:30 -08009 * Maintainer: Kumar Gala
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +000010 * Modifier: Sandeep Gopalpet <sandeep.kumar@freescale.com>
Linus Torvalds1da177e2005-04-16 15:20:36 -070011 *
Claudiu Manoil20862782014-02-17 12:53:14 +020012 * Copyright 2002-2009, 2011-2013 Freescale Semiconductor, Inc.
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +000013 * Copyright 2007 MontaVista Software, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -070014 *
15 * This program is free software; you can redistribute it and/or modify it
16 * under the terms of the GNU General Public License as published by the
17 * Free Software Foundation; either version 2 of the License, or (at your
18 * option) any later version.
19 *
20 * Gianfar: AKA Lambda Draconis, "Dragon"
21 * RA 11 31 24.2
22 * Dec +69 19 52
23 * V 3.84
24 * B-V +1.62
25 *
26 * Theory of operation
Kumar Gala0bbaf062005-06-20 10:54:21 -050027 *
Andy Flemingb31a1d82008-12-16 15:29:15 -080028 * The driver is initialized through of_device. Configuration information
29 * is therefore conveyed through an OF-style device tree.
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 *
31 * The Gianfar Ethernet Controller uses a ring of buffer
32 * descriptors. The beginning is indicated by a register
Kumar Gala0bbaf062005-06-20 10:54:21 -050033 * pointing to the physical address of the start of the ring.
34 * The end is determined by a "wrap" bit being set in the
Linus Torvalds1da177e2005-04-16 15:20:36 -070035 * last descriptor of the ring.
36 *
37 * When a packet is received, the RXF bit in the
Kumar Gala0bbaf062005-06-20 10:54:21 -050038 * IEVENT register is set, triggering an interrupt when the
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 * corresponding bit in the IMASK register is also set (if
40 * interrupt coalescing is active, then the interrupt may not
41 * happen immediately, but will wait until either a set number
Andy Flemingbb40dcb2005-09-23 22:54:21 -040042 * of frames or amount of time have passed). In NAPI, the
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * interrupt handler will signal there is work to be done, and
Francois Romieu0aa15382008-07-11 00:33:52 +020044 * exit. This method will start at the last known empty
Kumar Gala0bbaf062005-06-20 10:54:21 -050045 * descriptor, and process every subsequent descriptor until there
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 * are none left with data (NAPI will stop after a set number of
47 * packets to give time to other tasks, but will eventually
48 * process all the packets). The data arrives inside a
49 * pre-allocated skb, and so after the skb is passed up to the
50 * stack, a new skb must be allocated, and the address field in
51 * the buffer descriptor must be updated to indicate this new
52 * skb.
53 *
54 * When the kernel requests that a packet be transmitted, the
55 * driver starts where it left off last time, and points the
56 * descriptor at the buffer which was passed in. The driver
57 * then informs the DMA engine that there are packets ready to
58 * be transmitted. Once the controller is finished transmitting
59 * the packet, an interrupt may be triggered (under the same
60 * conditions as for reception, but depending on the TXF bit).
61 * The driver then cleans up the buffer.
62 */
63
Joe Perches59deab22011-06-14 08:57:47 +000064#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
65#define DEBUG
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067#include <linux/kernel.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070068#include <linux/string.h>
69#include <linux/errno.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040070#include <linux/unistd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070071#include <linux/slab.h>
72#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#include <linux/delay.h>
74#include <linux/netdevice.h>
75#include <linux/etherdevice.h>
76#include <linux/skbuff.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050077#include <linux/if_vlan.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#include <linux/spinlock.h>
79#include <linux/mm.h>
Rob Herring5af50732013-09-17 14:28:33 -050080#include <linux/of_address.h>
81#include <linux/of_irq.h>
Grant Likelyfe192a42009-04-25 12:53:12 +000082#include <linux/of_mdio.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -080083#include <linux/of_platform.h>
Kumar Gala0bbaf062005-06-20 10:54:21 -050084#include <linux/ip.h>
85#include <linux/tcp.h>
86#include <linux/udp.h>
Kumar Gala9c07b8842006-01-11 11:26:25 -080087#include <linux/in.h>
Manfred Rudigiercc772ab2010-04-08 23:10:03 +000088#include <linux/net_tstamp.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90#include <asm/io.h>
Anton Vorontsov7d350972010-06-30 06:39:12 +000091#include <asm/reg.h>
Claudiu Manoil2969b1f2013-10-09 20:20:41 +030092#include <asm/mpc85xx.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070093#include <asm/irq.h>
94#include <asm/uaccess.h>
95#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070096#include <linux/dma-mapping.h>
97#include <linux/crc32.h>
Andy Flemingbb40dcb2005-09-23 22:54:21 -040098#include <linux/mii.h>
99#include <linux/phy.h>
Andy Flemingb31a1d82008-12-16 15:29:15 -0800100#include <linux/phy_fixed.h>
101#include <linux/of.h>
David Daney4b6ba8a2010-10-26 15:07:13 -0700102#include <linux/of_net.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104#include "gianfar.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106#define TX_TIMEOUT (1*HZ)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
Andy Fleming7f7f5312005-11-11 12:38:59 -0600108const char gfar_driver_version[] = "1.3";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110static int gfar_enet_open(struct net_device *dev);
111static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev);
Sebastian Siewiorab939902008-08-19 21:12:45 +0200112static void gfar_reset_task(struct work_struct *work);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113static void gfar_timeout(struct net_device *dev);
114static int gfar_close(struct net_device *dev);
Andy Fleming815b97c2008-04-22 17:18:29 -0500115struct sk_buff *gfar_new_skb(struct net_device *dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000116static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000117 struct sk_buff *skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118static int gfar_set_mac_address(struct net_device *dev);
119static int gfar_change_mtu(struct net_device *dev, int new_mtu);
David Howells7d12e782006-10-05 14:55:46 +0100120static irqreturn_t gfar_error(int irq, void *dev_id);
121static irqreturn_t gfar_transmit(int irq, void *dev_id);
122static irqreturn_t gfar_interrupt(int irq, void *dev_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123static void adjust_link(struct net_device *dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124static int init_phy(struct net_device *dev);
Grant Likely74888762011-02-22 21:05:51 -0700125static int gfar_probe(struct platform_device *ofdev);
Grant Likely2dc11582010-08-06 09:25:50 -0600126static int gfar_remove(struct platform_device *ofdev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -0400127static void free_skb_resources(struct gfar_private *priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128static void gfar_set_multi(struct net_device *dev);
129static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr);
Kapil Junejad3c12872007-05-11 18:25:11 -0500130static void gfar_configure_serdes(struct net_device *dev);
Claudiu Manoilaeb12c52014-03-07 14:42:45 +0200131static int gfar_poll_rx(struct napi_struct *napi, int budget);
132static int gfar_poll_tx(struct napi_struct *napi, int budget);
133static int gfar_poll_rx_sq(struct napi_struct *napi, int budget);
134static int gfar_poll_tx_sq(struct napi_struct *napi, int budget);
Vitaly Woolf2d71c22006-11-07 13:27:02 +0300135#ifdef CONFIG_NET_POLL_CONTROLLER
136static void gfar_netpoll(struct net_device *dev);
137#endif
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000138int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit);
Claudiu Manoilc233cf402013-03-19 07:40:02 +0000139static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue);
Claudiu Manoil61db26c2013-02-14 05:00:05 +0000140static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
141 int amount_pull, struct napi_struct *napi);
Claudiu Manoilc10650b2014-02-17 12:53:18 +0200142static void gfar_halt_nodisable(struct gfar_private *priv);
Andy Fleming7f7f5312005-11-11 12:38:59 -0600143static void gfar_clear_exact_match(struct net_device *dev);
Joe Perchesb6bc7652010-12-21 02:16:08 -0800144static void gfar_set_mac_for_addr(struct net_device *dev, int num,
145 const u8 *addr);
Andy Fleming26ccfc32009-03-10 12:58:28 +0000146static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148MODULE_AUTHOR("Freescale Semiconductor, Inc");
149MODULE_DESCRIPTION("Gianfar Ethernet Driver");
150MODULE_LICENSE("GPL");
151
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000152static void gfar_init_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000153 dma_addr_t buf)
154{
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000155 u32 lstatus;
156
157 bdp->bufPtr = buf;
158
159 lstatus = BD_LFLAG(RXBD_EMPTY | RXBD_INTERRUPT);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000160 if (bdp == rx_queue->rx_bd_base + rx_queue->rx_ring_size - 1)
Anton Vorontsov8a102fe2009-10-12 06:00:37 +0000161 lstatus |= BD_LFLAG(RXBD_WRAP);
162
163 eieio();
164
165 bdp->lstatus = lstatus;
166}
167
Anton Vorontsov87283272009-10-12 06:00:39 +0000168static int gfar_init_bds(struct net_device *ndev)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000169{
Anton Vorontsov87283272009-10-12 06:00:39 +0000170 struct gfar_private *priv = netdev_priv(ndev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000171 struct gfar_priv_tx_q *tx_queue = NULL;
172 struct gfar_priv_rx_q *rx_queue = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000173 struct txbd8 *txbdp;
174 struct rxbd8 *rxbdp;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000175 int i, j;
Anton Vorontsov87283272009-10-12 06:00:39 +0000176
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000177 for (i = 0; i < priv->num_tx_queues; i++) {
178 tx_queue = priv->tx_queue[i];
179 /* Initialize some variables in our dev structure */
180 tx_queue->num_txbdfree = tx_queue->tx_ring_size;
181 tx_queue->dirty_tx = tx_queue->tx_bd_base;
182 tx_queue->cur_tx = tx_queue->tx_bd_base;
183 tx_queue->skb_curtx = 0;
184 tx_queue->skb_dirtytx = 0;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000185
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000186 /* Initialize Transmit Descriptor Ring */
187 txbdp = tx_queue->tx_bd_base;
188 for (j = 0; j < tx_queue->tx_ring_size; j++) {
189 txbdp->lstatus = 0;
190 txbdp->bufPtr = 0;
191 txbdp++;
Anton Vorontsov87283272009-10-12 06:00:39 +0000192 }
193
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000194 /* Set the last descriptor in the ring to indicate wrap */
195 txbdp--;
196 txbdp->status |= TXBD_WRAP;
197 }
198
199 for (i = 0; i < priv->num_rx_queues; i++) {
200 rx_queue = priv->rx_queue[i];
201 rx_queue->cur_rx = rx_queue->rx_bd_base;
202 rx_queue->skb_currx = 0;
203 rxbdp = rx_queue->rx_bd_base;
204
205 for (j = 0; j < rx_queue->rx_ring_size; j++) {
206 struct sk_buff *skb = rx_queue->rx_skbuff[j];
207
208 if (skb) {
209 gfar_init_rxbdp(rx_queue, rxbdp,
210 rxbdp->bufPtr);
211 } else {
212 skb = gfar_new_skb(ndev);
213 if (!skb) {
Joe Perches59deab22011-06-14 08:57:47 +0000214 netdev_err(ndev, "Can't allocate RX buffers\n");
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +0000215 return -ENOMEM;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000216 }
217 rx_queue->rx_skbuff[j] = skb;
218
219 gfar_new_rxbdp(rx_queue, rxbdp, skb);
220 }
221
222 rxbdp++;
223 }
224
Anton Vorontsov87283272009-10-12 06:00:39 +0000225 }
226
227 return 0;
228}
229
230static int gfar_alloc_skb_resources(struct net_device *ndev)
231{
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000232 void *vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000233 dma_addr_t addr;
234 int i, j, k;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000235 struct gfar_private *priv = netdev_priv(ndev);
Claudiu Manoil369ec162013-02-14 05:00:02 +0000236 struct device *dev = priv->dev;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000237 struct gfar_priv_tx_q *tx_queue = NULL;
238 struct gfar_priv_rx_q *rx_queue = NULL;
239
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000240 priv->total_tx_ring_size = 0;
241 for (i = 0; i < priv->num_tx_queues; i++)
242 priv->total_tx_ring_size += priv->tx_queue[i]->tx_ring_size;
243
244 priv->total_rx_ring_size = 0;
245 for (i = 0; i < priv->num_rx_queues; i++)
246 priv->total_rx_ring_size += priv->rx_queue[i]->rx_ring_size;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000247
248 /* Allocate memory for the buffer descriptors */
Anton Vorontsov87283272009-10-12 06:00:39 +0000249 vaddr = dma_alloc_coherent(dev,
Joe Perchesd0320f72013-03-14 13:07:21 +0000250 (priv->total_tx_ring_size *
251 sizeof(struct txbd8)) +
252 (priv->total_rx_ring_size *
253 sizeof(struct rxbd8)),
254 &addr, GFP_KERNEL);
255 if (!vaddr)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000256 return -ENOMEM;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000257
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000258 for (i = 0; i < priv->num_tx_queues; i++) {
259 tx_queue = priv->tx_queue[i];
Joe Perches43d620c2011-06-16 19:08:06 +0000260 tx_queue->tx_bd_base = vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000261 tx_queue->tx_bd_dma_base = addr;
262 tx_queue->dev = ndev;
263 /* enet DMA only understands physical addresses */
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000264 addr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
265 vaddr += sizeof(struct txbd8) * tx_queue->tx_ring_size;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000266 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000267
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000268 /* Start the rx descriptor ring where the tx ring leaves off */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000269 for (i = 0; i < priv->num_rx_queues; i++) {
270 rx_queue = priv->rx_queue[i];
Joe Perches43d620c2011-06-16 19:08:06 +0000271 rx_queue->rx_bd_base = vaddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000272 rx_queue->rx_bd_dma_base = addr;
273 rx_queue->dev = ndev;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000274 addr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
275 vaddr += sizeof(struct rxbd8) * rx_queue->rx_ring_size;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000276 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000277
278 /* Setup the skbuff rings */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000279 for (i = 0; i < priv->num_tx_queues; i++) {
280 tx_queue = priv->tx_queue[i];
Joe Perches14f8dc42013-02-07 11:46:27 +0000281 tx_queue->tx_skbuff =
282 kmalloc_array(tx_queue->tx_ring_size,
283 sizeof(*tx_queue->tx_skbuff),
284 GFP_KERNEL);
285 if (!tx_queue->tx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000286 goto cleanup;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000287
288 for (k = 0; k < tx_queue->tx_ring_size; k++)
289 tx_queue->tx_skbuff[k] = NULL;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000290 }
291
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000292 for (i = 0; i < priv->num_rx_queues; i++) {
293 rx_queue = priv->rx_queue[i];
Joe Perches14f8dc42013-02-07 11:46:27 +0000294 rx_queue->rx_skbuff =
295 kmalloc_array(rx_queue->rx_ring_size,
296 sizeof(*rx_queue->rx_skbuff),
297 GFP_KERNEL);
298 if (!rx_queue->rx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000299 goto cleanup;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000300
301 for (j = 0; j < rx_queue->rx_ring_size; j++)
302 rx_queue->rx_skbuff[j] = NULL;
303 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000304
Anton Vorontsov87283272009-10-12 06:00:39 +0000305 if (gfar_init_bds(ndev))
306 goto cleanup;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000307
308 return 0;
309
310cleanup:
311 free_skb_resources(priv);
312 return -ENOMEM;
313}
314
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000315static void gfar_init_tx_rx_base(struct gfar_private *priv)
316{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000317 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov18294ad2009-11-04 12:53:00 +0000318 u32 __iomem *baddr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000319 int i;
320
321 baddr = &regs->tbase0;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000322 for (i = 0; i < priv->num_tx_queues; i++) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000323 gfar_write(baddr, priv->tx_queue[i]->tx_bd_dma_base);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000324 baddr += 2;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000325 }
326
327 baddr = &regs->rbase0;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000328 for (i = 0; i < priv->num_rx_queues; i++) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000329 gfar_write(baddr, priv->rx_queue[i]->rx_bd_dma_base);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000330 baddr += 2;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000331 }
332}
333
Claudiu Manoil88302642014-02-24 12:13:43 +0200334static void gfar_rx_buff_size_config(struct gfar_private *priv)
335{
336 int frame_size = priv->ndev->mtu + ETH_HLEN;
337
338 /* set this when rx hw offload (TOE) functions are being used */
339 priv->uses_rxfcb = 0;
340
341 if (priv->ndev->features & (NETIF_F_RXCSUM | NETIF_F_HW_VLAN_CTAG_RX))
342 priv->uses_rxfcb = 1;
343
344 if (priv->hwts_rx_en)
345 priv->uses_rxfcb = 1;
346
347 if (priv->uses_rxfcb)
348 frame_size += GMAC_FCB_LEN;
349
350 frame_size += priv->padding;
351
352 frame_size = (frame_size & ~(INCREMENTAL_BUFFER_SIZE - 1)) +
353 INCREMENTAL_BUFFER_SIZE;
354
355 priv->rx_buffer_size = frame_size;
356}
357
Claudiu Manoila328ac92014-02-24 12:13:42 +0200358static void gfar_mac_rx_config(struct gfar_private *priv)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000359{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000360 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000361 u32 rctrl = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000362
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000363 if (priv->rx_filer_enable) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000364 rctrl |= RCTRL_FILREN;
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000365 /* Program the RIR0 reg with the required distribution */
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200366 if (priv->poll_mode == GFAR_SQ_POLLING)
367 gfar_write(&regs->rir0, DEFAULT_2RXQ_RIR0);
368 else /* GFAR_MQ_POLLING */
369 gfar_write(&regs->rir0, DEFAULT_8RXQ_RIR0);
Sandeep Gopalpet1ccb8382009-12-16 01:14:58 +0000370 }
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000371
Claudiu Manoilf5ae6272013-01-23 00:18:36 +0000372 /* Restore PROMISC mode */
Claudiu Manoila328ac92014-02-24 12:13:42 +0200373 if (priv->ndev->flags & IFF_PROMISC)
Claudiu Manoilf5ae6272013-01-23 00:18:36 +0000374 rctrl |= RCTRL_PROM;
375
Claudiu Manoil88302642014-02-24 12:13:43 +0200376 if (priv->ndev->features & NETIF_F_RXCSUM)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000377 rctrl |= RCTRL_CHECKSUMMING;
378
Claudiu Manoil88302642014-02-24 12:13:43 +0200379 if (priv->extended_hash)
380 rctrl |= RCTRL_EXTHASH | RCTRL_EMEN;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000381
382 if (priv->padding) {
383 rctrl &= ~RCTRL_PAL_MASK;
384 rctrl |= RCTRL_PADDING(priv->padding);
385 }
386
Manfred Rudigier97553f72010-06-11 01:49:05 +0000387 /* Enable HW time stamping if requested from user space */
Claudiu Manoil88302642014-02-24 12:13:43 +0200388 if (priv->hwts_rx_en)
Manfred Rudigier97553f72010-06-11 01:49:05 +0000389 rctrl |= RCTRL_PRSDEP_INIT | RCTRL_TS_ENABLE;
390
Claudiu Manoil88302642014-02-24 12:13:43 +0200391 if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_RX)
Sebastian Pöhnb852b722011-07-26 00:03:13 +0000392 rctrl |= RCTRL_VLEX | RCTRL_PRSDEP_INIT;
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000393
394 /* Init rctrl based on our settings */
395 gfar_write(&regs->rctrl, rctrl);
Claudiu Manoila328ac92014-02-24 12:13:42 +0200396}
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000397
Claudiu Manoila328ac92014-02-24 12:13:42 +0200398static void gfar_mac_tx_config(struct gfar_private *priv)
399{
400 struct gfar __iomem *regs = priv->gfargrp[0].regs;
401 u32 tctrl = 0;
402
403 if (priv->ndev->features & NETIF_F_IP_CSUM)
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000404 tctrl |= TCTRL_INIT_CSUM;
405
Claudiu Manoilb98b8ba2012-09-23 22:39:08 +0000406 if (priv->prio_sched_en)
407 tctrl |= TCTRL_TXSCHED_PRIO;
408 else {
409 tctrl |= TCTRL_TXSCHED_WRRS;
410 gfar_write(&regs->tr03wt, DEFAULT_WRRS_WEIGHT);
411 gfar_write(&regs->tr47wt, DEFAULT_WRRS_WEIGHT);
412 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000413
Claudiu Manoil88302642014-02-24 12:13:43 +0200414 if (priv->ndev->features & NETIF_F_HW_VLAN_CTAG_TX)
415 tctrl |= TCTRL_VLINS;
416
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000417 gfar_write(&regs->tctrl, tctrl);
Anton Vorontsov826aa4a2009-10-12 06:00:34 +0000418}
419
Claudiu Manoilf19015b2014-02-24 12:13:46 +0200420static void gfar_configure_coalescing(struct gfar_private *priv,
421 unsigned long tx_mask, unsigned long rx_mask)
422{
423 struct gfar __iomem *regs = priv->gfargrp[0].regs;
424 u32 __iomem *baddr;
425
426 if (priv->mode == MQ_MG_MODE) {
427 int i = 0;
428
429 baddr = &regs->txic0;
430 for_each_set_bit(i, &tx_mask, priv->num_tx_queues) {
431 gfar_write(baddr + i, 0);
432 if (likely(priv->tx_queue[i]->txcoalescing))
433 gfar_write(baddr + i, priv->tx_queue[i]->txic);
434 }
435
436 baddr = &regs->rxic0;
437 for_each_set_bit(i, &rx_mask, priv->num_rx_queues) {
438 gfar_write(baddr + i, 0);
439 if (likely(priv->rx_queue[i]->rxcoalescing))
440 gfar_write(baddr + i, priv->rx_queue[i]->rxic);
441 }
442 } else {
443 /* Backward compatible case -- even if we enable
444 * multiple queues, there's only single reg to program
445 */
446 gfar_write(&regs->txic, 0);
447 if (likely(priv->tx_queue[0]->txcoalescing))
448 gfar_write(&regs->txic, priv->tx_queue[0]->txic);
449
450 gfar_write(&regs->rxic, 0);
451 if (unlikely(priv->rx_queue[0]->rxcoalescing))
452 gfar_write(&regs->rxic, priv->rx_queue[0]->rxic);
453 }
454}
455
456void gfar_configure_coalescing_all(struct gfar_private *priv)
457{
458 gfar_configure_coalescing(priv, 0xFF, 0xFF);
459}
460
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000461static struct net_device_stats *gfar_get_stats(struct net_device *dev)
462{
463 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000464 unsigned long rx_packets = 0, rx_bytes = 0, rx_dropped = 0;
465 unsigned long tx_packets = 0, tx_bytes = 0;
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000466 int i;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000467
468 for (i = 0; i < priv->num_rx_queues; i++) {
469 rx_packets += priv->rx_queue[i]->stats.rx_packets;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000470 rx_bytes += priv->rx_queue[i]->stats.rx_bytes;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000471 rx_dropped += priv->rx_queue[i]->stats.rx_dropped;
472 }
473
474 dev->stats.rx_packets = rx_packets;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000475 dev->stats.rx_bytes = rx_bytes;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000476 dev->stats.rx_dropped = rx_dropped;
477
478 for (i = 0; i < priv->num_tx_queues; i++) {
Eric Dumazet1ac9ad12011-01-12 12:13:14 +0000479 tx_bytes += priv->tx_queue[i]->stats.tx_bytes;
480 tx_packets += priv->tx_queue[i]->stats.tx_packets;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000481 }
482
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000483 dev->stats.tx_bytes = tx_bytes;
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000484 dev->stats.tx_packets = tx_packets;
485
486 return &dev->stats;
487}
488
Andy Fleming26ccfc32009-03-10 12:58:28 +0000489static const struct net_device_ops gfar_netdev_ops = {
490 .ndo_open = gfar_enet_open,
491 .ndo_start_xmit = gfar_start_xmit,
492 .ndo_stop = gfar_close,
493 .ndo_change_mtu = gfar_change_mtu,
Michał Mirosław8b3afe92011-04-15 04:50:50 +0000494 .ndo_set_features = gfar_set_features,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000495 .ndo_set_rx_mode = gfar_set_multi,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000496 .ndo_tx_timeout = gfar_timeout,
497 .ndo_do_ioctl = gfar_ioctl,
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +0000498 .ndo_get_stats = gfar_get_stats,
Ben Hutchings240c1022009-07-09 17:54:35 +0000499 .ndo_set_mac_address = eth_mac_addr,
500 .ndo_validate_addr = eth_validate_addr,
Andy Fleming26ccfc32009-03-10 12:58:28 +0000501#ifdef CONFIG_NET_POLL_CONTROLLER
502 .ndo_poll_controller = gfar_netpoll,
503#endif
504};
505
Claudiu Manoilefeddce2014-02-17 12:53:17 +0200506static void gfar_ints_disable(struct gfar_private *priv)
507{
508 int i;
509 for (i = 0; i < priv->num_grps; i++) {
510 struct gfar __iomem *regs = priv->gfargrp[i].regs;
511 /* Clear IEVENT */
512 gfar_write(&regs->ievent, IEVENT_INIT_CLEAR);
513
514 /* Initialize IMASK */
515 gfar_write(&regs->imask, IMASK_INIT_CLEAR);
516 }
517}
518
519static void gfar_ints_enable(struct gfar_private *priv)
520{
521 int i;
522 for (i = 0; i < priv->num_grps; i++) {
523 struct gfar __iomem *regs = priv->gfargrp[i].regs;
524 /* Unmask the interrupts we look for */
525 gfar_write(&regs->imask, IMASK_DEFAULT);
526 }
527}
528
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000529void lock_tx_qs(struct gfar_private *priv)
530{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000531 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000532
533 for (i = 0; i < priv->num_tx_queues; i++)
534 spin_lock(&priv->tx_queue[i]->txlock);
535}
536
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000537void unlock_tx_qs(struct gfar_private *priv)
538{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000539 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000540
541 for (i = 0; i < priv->num_tx_queues; i++)
542 spin_unlock(&priv->tx_queue[i]->txlock);
543}
544
Claudiu Manoil20862782014-02-17 12:53:14 +0200545static int gfar_alloc_tx_queues(struct gfar_private *priv)
546{
547 int i;
548
549 for (i = 0; i < priv->num_tx_queues; i++) {
550 priv->tx_queue[i] = kzalloc(sizeof(struct gfar_priv_tx_q),
551 GFP_KERNEL);
552 if (!priv->tx_queue[i])
553 return -ENOMEM;
554
555 priv->tx_queue[i]->tx_skbuff = NULL;
556 priv->tx_queue[i]->qindex = i;
557 priv->tx_queue[i]->dev = priv->ndev;
558 spin_lock_init(&(priv->tx_queue[i]->txlock));
559 }
560 return 0;
561}
562
563static int gfar_alloc_rx_queues(struct gfar_private *priv)
564{
565 int i;
566
567 for (i = 0; i < priv->num_rx_queues; i++) {
568 priv->rx_queue[i] = kzalloc(sizeof(struct gfar_priv_rx_q),
569 GFP_KERNEL);
570 if (!priv->rx_queue[i])
571 return -ENOMEM;
572
573 priv->rx_queue[i]->rx_skbuff = NULL;
574 priv->rx_queue[i]->qindex = i;
575 priv->rx_queue[i]->dev = priv->ndev;
Claudiu Manoil20862782014-02-17 12:53:14 +0200576 }
577 return 0;
578}
579
580static void gfar_free_tx_queues(struct gfar_private *priv)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000581{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000582 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000583
584 for (i = 0; i < priv->num_tx_queues; i++)
585 kfree(priv->tx_queue[i]);
586}
587
Claudiu Manoil20862782014-02-17 12:53:14 +0200588static void gfar_free_rx_queues(struct gfar_private *priv)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000589{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000590 int i;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000591
592 for (i = 0; i < priv->num_rx_queues; i++)
593 kfree(priv->rx_queue[i]);
594}
595
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000596static void unmap_group_regs(struct gfar_private *priv)
597{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000598 int i;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000599
600 for (i = 0; i < MAXGROUPS; i++)
601 if (priv->gfargrp[i].regs)
602 iounmap(priv->gfargrp[i].regs);
603}
604
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000605static void free_gfar_dev(struct gfar_private *priv)
606{
607 int i, j;
608
609 for (i = 0; i < priv->num_grps; i++)
610 for (j = 0; j < GFAR_NUM_IRQS; j++) {
611 kfree(priv->gfargrp[i].irqinfo[j]);
612 priv->gfargrp[i].irqinfo[j] = NULL;
613 }
614
615 free_netdev(priv->ndev);
616}
617
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000618static void disable_napi(struct gfar_private *priv)
619{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000620 int i;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000621
Claudiu Manoilaeb12c52014-03-07 14:42:45 +0200622 for (i = 0; i < priv->num_grps; i++) {
623 napi_disable(&priv->gfargrp[i].napi_rx);
624 napi_disable(&priv->gfargrp[i].napi_tx);
625 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000626}
627
628static void enable_napi(struct gfar_private *priv)
629{
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +0000630 int i;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000631
Claudiu Manoilaeb12c52014-03-07 14:42:45 +0200632 for (i = 0; i < priv->num_grps; i++) {
633 napi_enable(&priv->gfargrp[i].napi_rx);
634 napi_enable(&priv->gfargrp[i].napi_tx);
635 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000636}
637
638static int gfar_parse_group(struct device_node *np,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000639 struct gfar_private *priv, const char *model)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000640{
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000641 struct gfar_priv_grp *grp = &priv->gfargrp[priv->num_grps];
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000642 int i;
643
Paul Gortmaker7c1e7e92013-02-04 09:49:42 +0000644 for (i = 0; i < GFAR_NUM_IRQS; i++) {
645 grp->irqinfo[i] = kzalloc(sizeof(struct gfar_irqinfo),
646 GFP_KERNEL);
647 if (!grp->irqinfo[i])
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000648 return -ENOMEM;
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000649 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000650
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000651 grp->regs = of_iomap(np, 0);
652 if (!grp->regs)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000653 return -ENOMEM;
654
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000655 gfar_irq(grp, TX)->irq = irq_of_parse_and_map(np, 0);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000656
657 /* If we aren't the FEC we have multiple interrupts */
658 if (model && strcasecmp(model, "FEC")) {
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000659 gfar_irq(grp, RX)->irq = irq_of_parse_and_map(np, 1);
660 gfar_irq(grp, ER)->irq = irq_of_parse_and_map(np, 2);
661 if (gfar_irq(grp, TX)->irq == NO_IRQ ||
662 gfar_irq(grp, RX)->irq == NO_IRQ ||
663 gfar_irq(grp, ER)->irq == NO_IRQ)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000664 return -EINVAL;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000665 }
666
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000667 grp->priv = priv;
668 spin_lock_init(&grp->grplock);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000669 if (priv->mode == MQ_MG_MODE) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200670 u32 *rxq_mask, *txq_mask;
671 rxq_mask = (u32 *)of_get_property(np, "fsl,rx-bit-map", NULL);
672 txq_mask = (u32 *)of_get_property(np, "fsl,tx-bit-map", NULL);
673
674 if (priv->poll_mode == GFAR_SQ_POLLING) {
675 /* One Q per interrupt group: Q0 to G0, Q1 to G1 */
676 grp->rx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
677 grp->tx_bit_map = (DEFAULT_MAPPING >> priv->num_grps);
678 } else { /* GFAR_MQ_POLLING */
679 grp->rx_bit_map = rxq_mask ?
680 *rxq_mask : (DEFAULT_MAPPING >> priv->num_grps);
681 grp->tx_bit_map = txq_mask ?
682 *txq_mask : (DEFAULT_MAPPING >> priv->num_grps);
683 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000684 } else {
Claudiu Manoil5fedcc12013-01-29 03:55:11 +0000685 grp->rx_bit_map = 0xFF;
686 grp->tx_bit_map = 0xFF;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000687 }
Claudiu Manoil20862782014-02-17 12:53:14 +0200688
689 /* bit_map's MSB is q0 (from q0 to q7) but, for_each_set_bit parses
690 * right to left, so we need to revert the 8 bits to get the q index
691 */
692 grp->rx_bit_map = bitrev8(grp->rx_bit_map);
693 grp->tx_bit_map = bitrev8(grp->tx_bit_map);
694
695 /* Calculate RSTAT, TSTAT, RQUEUE and TQUEUE values,
696 * also assign queues to groups
697 */
698 for_each_set_bit(i, &grp->rx_bit_map, priv->num_rx_queues) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200699 if (!grp->rx_queue)
700 grp->rx_queue = priv->rx_queue[i];
Claudiu Manoil20862782014-02-17 12:53:14 +0200701 grp->num_rx_queues++;
702 grp->rstat |= (RSTAT_CLEAR_RHALT >> i);
703 priv->rqueue |= ((RQUEUE_EN0 | RQUEUE_EX0) >> i);
704 priv->rx_queue[i]->grp = grp;
705 }
706
707 for_each_set_bit(i, &grp->tx_bit_map, priv->num_tx_queues) {
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200708 if (!grp->tx_queue)
709 grp->tx_queue = priv->tx_queue[i];
Claudiu Manoil20862782014-02-17 12:53:14 +0200710 grp->num_tx_queues++;
711 grp->tstat |= (TSTAT_CLEAR_THALT >> i);
712 priv->tqueue |= (TQUEUE_EN0 >> i);
713 priv->tx_queue[i]->grp = grp;
714 }
715
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000716 priv->num_grps++;
717
718 return 0;
719}
720
Grant Likely2dc11582010-08-06 09:25:50 -0600721static int gfar_of_init(struct platform_device *ofdev, struct net_device **pdev)
Andy Flemingb31a1d82008-12-16 15:29:15 -0800722{
Andy Flemingb31a1d82008-12-16 15:29:15 -0800723 const char *model;
724 const char *ctype;
725 const void *mac_addr;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000726 int err = 0, i;
727 struct net_device *dev = NULL;
728 struct gfar_private *priv = NULL;
Grant Likely61c7a082010-04-13 16:12:29 -0700729 struct device_node *np = ofdev->dev.of_node;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000730 struct device_node *child = NULL;
Andy Fleming4d7902f2009-02-04 16:43:44 -0800731 const u32 *stash;
732 const u32 *stash_len;
733 const u32 *stash_idx;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000734 unsigned int num_tx_qs, num_rx_qs;
735 u32 *tx_queues, *rx_queues;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800736
737 if (!np || !of_device_is_available(np))
738 return -ENODEV;
739
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200740 /* parse the num of HW tx and rx queues */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000741 tx_queues = (u32 *)of_get_property(np, "fsl,num_tx_queues", NULL);
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200742 rx_queues = (u32 *)of_get_property(np, "fsl,num_rx_queues", NULL);
743
744 if (priv->mode == SQ_SG_MODE) {
745 num_tx_qs = 1;
746 num_rx_qs = 1;
747 } else { /* MQ_MG_MODE */
748 if (priv->poll_mode == GFAR_SQ_POLLING) {
749 num_tx_qs = 2; /* one q per int group */
750 num_rx_qs = 2; /* one q per int group */
751 } else { /* GFAR_MQ_POLLING */
752 num_tx_qs = tx_queues ? *tx_queues : 1;
753 num_rx_qs = rx_queues ? *rx_queues : 1;
754 }
755 }
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000756
757 if (num_tx_qs > MAX_TX_QS) {
Joe Perches59deab22011-06-14 08:57:47 +0000758 pr_err("num_tx_qs(=%d) greater than MAX_TX_QS(=%d)\n",
759 num_tx_qs, MAX_TX_QS);
760 pr_err("Cannot do alloc_etherdev, aborting\n");
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000761 return -EINVAL;
762 }
763
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000764 if (num_rx_qs > MAX_RX_QS) {
Joe Perches59deab22011-06-14 08:57:47 +0000765 pr_err("num_rx_qs(=%d) greater than MAX_RX_QS(=%d)\n",
766 num_rx_qs, MAX_RX_QS);
767 pr_err("Cannot do alloc_etherdev, aborting\n");
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000768 return -EINVAL;
769 }
770
771 *pdev = alloc_etherdev_mq(sizeof(*priv), num_tx_qs);
772 dev = *pdev;
773 if (NULL == dev)
774 return -ENOMEM;
775
776 priv = netdev_priv(dev);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000777 priv->ndev = dev;
778
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000779 priv->num_tx_queues = num_tx_qs;
Ben Hutchingsfe069122010-09-27 08:27:37 +0000780 netif_set_real_num_rx_queues(dev, num_rx_qs);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +0000781 priv->num_rx_queues = num_rx_qs;
Claudiu Manoil20862782014-02-17 12:53:14 +0200782
783 err = gfar_alloc_tx_queues(priv);
784 if (err)
785 goto tx_alloc_failed;
786
787 err = gfar_alloc_rx_queues(priv);
788 if (err)
789 goto rx_alloc_failed;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800790
Jan Ceuleers0977f812012-06-05 03:42:12 +0000791 /* Init Rx queue filer rule set linked list */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -0700792 INIT_LIST_HEAD(&priv->rx_list.list);
793 priv->rx_list.count = 0;
794 mutex_init(&priv->rx_queue_access);
795
Andy Flemingb31a1d82008-12-16 15:29:15 -0800796 model = of_get_property(np, "model", NULL);
797
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000798 for (i = 0; i < MAXGROUPS; i++)
799 priv->gfargrp[i].regs = NULL;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800800
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000801 /* Parse and initialize group specific information */
802 if (of_device_is_compatible(np, "fsl,etsec2")) {
803 priv->mode = MQ_MG_MODE;
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200804 priv->poll_mode = GFAR_SQ_POLLING;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000805 for_each_child_of_node(np, child) {
806 err = gfar_parse_group(child, priv, model);
807 if (err)
808 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800809 }
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000810 } else {
811 priv->mode = SQ_SG_MODE;
Claudiu Manoil71ff9e32014-03-07 14:42:46 +0200812 priv->poll_mode = GFAR_SQ_POLLING;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000813 err = gfar_parse_group(np, priv, model);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000814 if (err)
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000815 goto err_grp_init;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800816 }
817
Andy Fleming4d7902f2009-02-04 16:43:44 -0800818 stash = of_get_property(np, "bd-stash", NULL);
819
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +0000820 if (stash) {
Andy Fleming4d7902f2009-02-04 16:43:44 -0800821 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BD_STASHING;
822 priv->bd_stash_en = 1;
823 }
824
825 stash_len = of_get_property(np, "rx-stash-len", NULL);
826
827 if (stash_len)
828 priv->rx_stash_size = *stash_len;
829
830 stash_idx = of_get_property(np, "rx-stash-idx", NULL);
831
832 if (stash_idx)
833 priv->rx_stash_index = *stash_idx;
834
835 if (stash_len || stash_idx)
836 priv->device_flags |= FSL_GIANFAR_DEV_HAS_BUF_STASHING;
837
Andy Flemingb31a1d82008-12-16 15:29:15 -0800838 mac_addr = of_get_mac_address(np);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000839
Andy Flemingb31a1d82008-12-16 15:29:15 -0800840 if (mac_addr)
Joe Perches6a3c910c2011-11-16 09:38:02 +0000841 memcpy(dev->dev_addr, mac_addr, ETH_ALEN);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800842
843 if (model && !strcasecmp(model, "TSEC"))
Claudiu Manoil34018fd2014-02-17 12:53:15 +0200844 priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000845 FSL_GIANFAR_DEV_HAS_COALESCE |
846 FSL_GIANFAR_DEV_HAS_RMON |
847 FSL_GIANFAR_DEV_HAS_MULTI_INTR;
848
Andy Flemingb31a1d82008-12-16 15:29:15 -0800849 if (model && !strcasecmp(model, "eTSEC"))
Claudiu Manoil34018fd2014-02-17 12:53:15 +0200850 priv->device_flags |= FSL_GIANFAR_DEV_HAS_GIGABIT |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000851 FSL_GIANFAR_DEV_HAS_COALESCE |
852 FSL_GIANFAR_DEV_HAS_RMON |
853 FSL_GIANFAR_DEV_HAS_MULTI_INTR |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +0000854 FSL_GIANFAR_DEV_HAS_CSUM |
855 FSL_GIANFAR_DEV_HAS_VLAN |
856 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET |
857 FSL_GIANFAR_DEV_HAS_EXTENDED_HASH |
858 FSL_GIANFAR_DEV_HAS_TIMER;
Andy Flemingb31a1d82008-12-16 15:29:15 -0800859
860 ctype = of_get_property(np, "phy-connection-type", NULL);
861
862 /* We only care about rgmii-id. The rest are autodetected */
863 if (ctype && !strcmp(ctype, "rgmii-id"))
864 priv->interface = PHY_INTERFACE_MODE_RGMII_ID;
865 else
866 priv->interface = PHY_INTERFACE_MODE_MII;
867
868 if (of_get_property(np, "fsl,magic-packet", NULL))
869 priv->device_flags |= FSL_GIANFAR_DEV_HAS_MAGIC_PACKET;
870
Grant Likelyfe192a42009-04-25 12:53:12 +0000871 priv->phy_node = of_parse_phandle(np, "phy-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800872
873 /* Find the TBI PHY. If it's not there, we don't support SGMII */
Grant Likelyfe192a42009-04-25 12:53:12 +0000874 priv->tbi_node = of_parse_phandle(np, "tbi-handle", 0);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800875
876 return 0;
877
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +0000878err_grp_init:
879 unmap_group_regs(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +0200880rx_alloc_failed:
881 gfar_free_rx_queues(priv);
882tx_alloc_failed:
883 gfar_free_tx_queues(priv);
Claudiu Manoilee873fd2013-01-29 03:55:12 +0000884 free_gfar_dev(priv);
Andy Flemingb31a1d82008-12-16 15:29:15 -0800885 return err;
886}
887
Ben Hutchingsca0c88c2013-11-18 23:05:27 +0000888static int gfar_hwtstamp_set(struct net_device *netdev, struct ifreq *ifr)
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000889{
890 struct hwtstamp_config config;
891 struct gfar_private *priv = netdev_priv(netdev);
892
893 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
894 return -EFAULT;
895
896 /* reserved for future extensions */
897 if (config.flags)
898 return -EINVAL;
899
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +0000900 switch (config.tx_type) {
901 case HWTSTAMP_TX_OFF:
902 priv->hwts_tx_en = 0;
903 break;
904 case HWTSTAMP_TX_ON:
905 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
906 return -ERANGE;
907 priv->hwts_tx_en = 1;
908 break;
909 default:
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000910 return -ERANGE;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +0000911 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000912
913 switch (config.rx_filter) {
914 case HWTSTAMP_FILTER_NONE:
Manfred Rudigier97553f72010-06-11 01:49:05 +0000915 if (priv->hwts_rx_en) {
Manfred Rudigier97553f72010-06-11 01:49:05 +0000916 priv->hwts_rx_en = 0;
Claudiu Manoil08511332014-02-24 12:13:45 +0200917 reset_gfar(netdev);
Manfred Rudigier97553f72010-06-11 01:49:05 +0000918 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000919 break;
920 default:
921 if (!(priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER))
922 return -ERANGE;
Manfred Rudigier97553f72010-06-11 01:49:05 +0000923 if (!priv->hwts_rx_en) {
Manfred Rudigier97553f72010-06-11 01:49:05 +0000924 priv->hwts_rx_en = 1;
Claudiu Manoil08511332014-02-24 12:13:45 +0200925 reset_gfar(netdev);
Manfred Rudigier97553f72010-06-11 01:49:05 +0000926 }
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000927 config.rx_filter = HWTSTAMP_FILTER_ALL;
928 break;
929 }
930
931 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
932 -EFAULT : 0;
933}
934
Ben Hutchingsca0c88c2013-11-18 23:05:27 +0000935static int gfar_hwtstamp_get(struct net_device *netdev, struct ifreq *ifr)
936{
937 struct hwtstamp_config config;
938 struct gfar_private *priv = netdev_priv(netdev);
939
940 config.flags = 0;
941 config.tx_type = priv->hwts_tx_en ? HWTSTAMP_TX_ON : HWTSTAMP_TX_OFF;
942 config.rx_filter = (priv->hwts_rx_en ?
943 HWTSTAMP_FILTER_ALL : HWTSTAMP_FILTER_NONE);
944
945 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
946 -EFAULT : 0;
947}
948
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000949static int gfar_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
950{
951 struct gfar_private *priv = netdev_priv(dev);
952
953 if (!netif_running(dev))
954 return -EINVAL;
955
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000956 if (cmd == SIOCSHWTSTAMP)
Ben Hutchingsca0c88c2013-11-18 23:05:27 +0000957 return gfar_hwtstamp_set(dev, rq);
958 if (cmd == SIOCGHWTSTAMP)
959 return gfar_hwtstamp_get(dev, rq);
Manfred Rudigiercc772ab2010-04-08 23:10:03 +0000960
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000961 if (!priv->phydev)
962 return -ENODEV;
963
Richard Cochran28b04112010-07-17 08:48:55 +0000964 return phy_mii_ioctl(priv->phydev, rq, cmd);
Clifford Wolf0faac9f2009-01-09 10:23:11 +0000965}
966
Anton Vorontsov18294ad2009-11-04 12:53:00 +0000967static u32 cluster_entry_per_class(struct gfar_private *priv, u32 rqfar,
968 u32 class)
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000969{
970 u32 rqfpr = FPR_FILER_MASK;
971 u32 rqfcr = 0x0;
972
973 rqfar--;
974 rqfcr = RQFCR_CLE | RQFCR_PID_MASK | RQFCR_CMP_EXACT;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000975 priv->ftp_rqfpr[rqfar] = rqfpr;
976 priv->ftp_rqfcr[rqfar] = rqfcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000977 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
978
979 rqfar--;
980 rqfcr = RQFCR_CMP_NOMATCH;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000981 priv->ftp_rqfpr[rqfar] = rqfpr;
982 priv->ftp_rqfcr[rqfar] = rqfcr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000983 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
984
985 rqfar--;
986 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_PARSE | RQFCR_CLE | RQFCR_AND;
987 rqfpr = class;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000988 priv->ftp_rqfcr[rqfar] = rqfcr;
989 priv->ftp_rqfpr[rqfar] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000990 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
991
992 rqfar--;
993 rqfcr = RQFCR_CMP_EXACT | RQFCR_PID_MASK | RQFCR_AND;
994 rqfpr = class;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +0000995 priv->ftp_rqfcr[rqfar] = rqfcr;
996 priv->ftp_rqfpr[rqfar] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +0000997 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
998
999 return rqfar;
1000}
1001
1002static void gfar_init_filer_table(struct gfar_private *priv)
1003{
1004 int i = 0x0;
1005 u32 rqfar = MAX_FILER_IDX;
1006 u32 rqfcr = 0x0;
1007 u32 rqfpr = FPR_FILER_MASK;
1008
1009 /* Default rule */
1010 rqfcr = RQFCR_CMP_MATCH;
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001011 priv->ftp_rqfcr[rqfar] = rqfcr;
1012 priv->ftp_rqfpr[rqfar] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001013 gfar_write_filer(priv, rqfar, rqfcr, rqfpr);
1014
1015 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6);
1016 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_UDP);
1017 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV6 | RQFPR_TCP);
1018 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4);
1019 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_UDP);
1020 rqfar = cluster_entry_per_class(priv, rqfar, RQFPR_IPV4 | RQFPR_TCP);
1021
Uwe Kleine-König85dd08e2010-06-11 12:16:55 +02001022 /* cur_filer_idx indicated the first non-masked rule */
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001023 priv->cur_filer_idx = rqfar;
1024
1025 /* Rest are masked rules */
1026 rqfcr = RQFCR_CMP_NOMATCH;
1027 for (i = 0; i < rqfar; i++) {
Wu Jiajun-B063786c43e042011-06-07 21:46:51 +00001028 priv->ftp_rqfcr[i] = rqfcr;
1029 priv->ftp_rqfpr[i] = rqfpr;
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001030 gfar_write_filer(priv, i, rqfcr, rqfpr);
1031 }
1032}
1033
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001034static void __gfar_detect_errata_83xx(struct gfar_private *priv)
Anton Vorontsov7d350972010-06-30 06:39:12 +00001035{
Anton Vorontsov7d350972010-06-30 06:39:12 +00001036 unsigned int pvr = mfspr(SPRN_PVR);
1037 unsigned int svr = mfspr(SPRN_SVR);
1038 unsigned int mod = (svr >> 16) & 0xfff6; /* w/o E suffix */
1039 unsigned int rev = svr & 0xffff;
1040
1041 /* MPC8313 Rev 2.0 and higher; All MPC837x */
1042 if ((pvr == 0x80850010 && mod == 0x80b0 && rev >= 0x0020) ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001043 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
Anton Vorontsov7d350972010-06-30 06:39:12 +00001044 priv->errata |= GFAR_ERRATA_74;
1045
Anton Vorontsovdeb90ea2010-06-30 06:39:13 +00001046 /* MPC8313 and MPC837x all rev */
1047 if ((pvr == 0x80850010 && mod == 0x80b0) ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001048 (pvr == 0x80861010 && (mod & 0xfff9) == 0x80c0))
Anton Vorontsovdeb90ea2010-06-30 06:39:13 +00001049 priv->errata |= GFAR_ERRATA_76;
1050
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001051 /* MPC8313 Rev < 2.0 */
1052 if (pvr == 0x80850010 && mod == 0x80b0 && rev < 0x0020)
Alex Dubov4363c2fdd2011-03-16 17:57:13 +00001053 priv->errata |= GFAR_ERRATA_12;
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001054}
1055
1056static void __gfar_detect_errata_85xx(struct gfar_private *priv)
1057{
1058 unsigned int svr = mfspr(SPRN_SVR);
1059
1060 if ((SVR_SOC_VER(svr) == SVR_8548) && (SVR_REV(svr) == 0x20))
1061 priv->errata |= GFAR_ERRATA_12;
Claudiu Manoil53fad772013-10-09 20:20:42 +03001062 if (((SVR_SOC_VER(svr) == SVR_P2020) && (SVR_REV(svr) < 0x20)) ||
1063 ((SVR_SOC_VER(svr) == SVR_P2010) && (SVR_REV(svr) < 0x20)))
1064 priv->errata |= GFAR_ERRATA_76; /* aka eTSEC 20 */
Claudiu Manoil2969b1f2013-10-09 20:20:41 +03001065}
1066
1067static void gfar_detect_errata(struct gfar_private *priv)
1068{
1069 struct device *dev = &priv->ofdev->dev;
1070
1071 /* no plans to fix */
1072 priv->errata |= GFAR_ERRATA_A002;
1073
1074 if (pvr_version_is(PVR_VER_E500V1) || pvr_version_is(PVR_VER_E500V2))
1075 __gfar_detect_errata_85xx(priv);
1076 else /* non-mpc85xx parts, i.e. e300 core based */
1077 __gfar_detect_errata_83xx(priv);
Alex Dubov4363c2fdd2011-03-16 17:57:13 +00001078
Anton Vorontsov7d350972010-06-30 06:39:12 +00001079 if (priv->errata)
1080 dev_info(dev, "enabled errata workarounds, flags: 0x%x\n",
1081 priv->errata);
1082}
1083
Claudiu Manoil08511332014-02-24 12:13:45 +02001084void gfar_mac_reset(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085{
Claudiu Manoil20862782014-02-17 12:53:14 +02001086 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Claudiu Manoila328ac92014-02-24 12:13:42 +02001087 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 /* Reset MAC layer */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001090 gfar_write(&regs->maccfg1, MACCFG1_SOFT_RESET);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Andy Flemingb98ac702009-02-04 16:38:05 -08001092 /* We need to delay at least 3 TX clocks */
Claudiu Manoila328ac92014-02-24 12:13:42 +02001093 udelay(3);
Andy Flemingb98ac702009-02-04 16:38:05 -08001094
Claudiu Manoil23402bd2013-08-12 13:53:26 +03001095 /* the soft reset bit is not self-resetting, so we need to
1096 * clear it before resuming normal operation
1097 */
Claudiu Manoil20862782014-02-17 12:53:14 +02001098 gfar_write(&regs->maccfg1, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099
Claudiu Manoila328ac92014-02-24 12:13:42 +02001100 udelay(3);
1101
Claudiu Manoil88302642014-02-24 12:13:43 +02001102 /* Compute rx_buff_size based on config flags */
1103 gfar_rx_buff_size_config(priv);
1104
1105 /* Initialize the max receive frame/buffer lengths */
1106 gfar_write(&regs->maxfrm, priv->rx_buffer_size);
Claudiu Manoila328ac92014-02-24 12:13:42 +02001107 gfar_write(&regs->mrblr, priv->rx_buffer_size);
1108
1109 /* Initialize the Minimum Frame Length Register */
1110 gfar_write(&regs->minflr, MINFLR_INIT_SETTINGS);
1111
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 /* Initialize MACCFG2. */
Anton Vorontsov7d350972010-06-30 06:39:12 +00001113 tempval = MACCFG2_INIT_SETTINGS;
Claudiu Manoil88302642014-02-24 12:13:43 +02001114
1115 /* If the mtu is larger than the max size for standard
1116 * ethernet frames (ie, a jumbo frame), then set maccfg2
1117 * to allow huge frames, and to check the length
1118 */
1119 if (priv->rx_buffer_size > DEFAULT_RX_BUFFER_SIZE ||
1120 gfar_has_errata(priv, GFAR_ERRATA_74))
Anton Vorontsov7d350972010-06-30 06:39:12 +00001121 tempval |= MACCFG2_HUGEFRAME | MACCFG2_LENGTHCHECK;
Claudiu Manoil88302642014-02-24 12:13:43 +02001122
Anton Vorontsov7d350972010-06-30 06:39:12 +00001123 gfar_write(&regs->maccfg2, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Claudiu Manoila328ac92014-02-24 12:13:42 +02001125 /* Clear mac addr hash registers */
1126 gfar_write(&regs->igaddr0, 0);
1127 gfar_write(&regs->igaddr1, 0);
1128 gfar_write(&regs->igaddr2, 0);
1129 gfar_write(&regs->igaddr3, 0);
1130 gfar_write(&regs->igaddr4, 0);
1131 gfar_write(&regs->igaddr5, 0);
1132 gfar_write(&regs->igaddr6, 0);
1133 gfar_write(&regs->igaddr7, 0);
1134
1135 gfar_write(&regs->gaddr0, 0);
1136 gfar_write(&regs->gaddr1, 0);
1137 gfar_write(&regs->gaddr2, 0);
1138 gfar_write(&regs->gaddr3, 0);
1139 gfar_write(&regs->gaddr4, 0);
1140 gfar_write(&regs->gaddr5, 0);
1141 gfar_write(&regs->gaddr6, 0);
1142 gfar_write(&regs->gaddr7, 0);
1143
1144 if (priv->extended_hash)
1145 gfar_clear_exact_match(priv->ndev);
1146
1147 gfar_mac_rx_config(priv);
1148
1149 gfar_mac_tx_config(priv);
1150
1151 gfar_set_mac_address(priv->ndev);
1152
1153 gfar_set_multi(priv->ndev);
1154
1155 /* clear ievent and imask before configuring coalescing */
1156 gfar_ints_disable(priv);
1157
1158 /* Configure the coalescing support */
1159 gfar_configure_coalescing_all(priv);
1160}
1161
1162static void gfar_hw_init(struct gfar_private *priv)
1163{
1164 struct gfar __iomem *regs = priv->gfargrp[0].regs;
1165 u32 attrs;
1166
1167 /* Stop the DMA engine now, in case it was running before
1168 * (The firmware could have used it, and left it running).
1169 */
1170 gfar_halt(priv);
1171
1172 gfar_mac_reset(priv);
1173
1174 /* Zero out the rmon mib registers if it has them */
1175 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_RMON) {
1176 memset_io(&(regs->rmon), 0, sizeof(struct rmon_mib));
1177
1178 /* Mask off the CAM interrupts */
1179 gfar_write(&regs->rmon.cam1, 0xffffffff);
1180 gfar_write(&regs->rmon.cam2, 0xffffffff);
1181 }
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 /* Initialize ECNTRL */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001184 gfar_write(&regs->ecntrl, ECNTRL_INIT_SETTINGS);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001185
Claudiu Manoil34018fd2014-02-17 12:53:15 +02001186 /* Set the extraction length and index */
1187 attrs = ATTRELI_EL(priv->rx_stash_size) |
1188 ATTRELI_EI(priv->rx_stash_index);
1189
1190 gfar_write(&regs->attreli, attrs);
1191
1192 /* Start with defaults, and add stashing
1193 * depending on driver parameters
1194 */
1195 attrs = ATTR_INIT_SETTINGS;
1196
1197 if (priv->bd_stash_en)
1198 attrs |= ATTR_BDSTASH;
1199
1200 if (priv->rx_stash_size != 0)
1201 attrs |= ATTR_BUFSTASH;
1202
1203 gfar_write(&regs->attr, attrs);
1204
1205 /* FIFO configs */
1206 gfar_write(&regs->fifo_tx_thr, DEFAULT_FIFO_TX_THR);
1207 gfar_write(&regs->fifo_tx_starve, DEFAULT_FIFO_TX_STARVE);
1208 gfar_write(&regs->fifo_tx_starve_shutoff, DEFAULT_FIFO_TX_STARVE_OFF);
1209
Claudiu Manoil20862782014-02-17 12:53:14 +02001210 /* Program the interrupt steering regs, only for MG devices */
1211 if (priv->num_grps > 1)
1212 gfar_write_isrg(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +02001213}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214
Claudiu Manoil20862782014-02-17 12:53:14 +02001215static void __init gfar_init_addr_hash_table(struct gfar_private *priv)
1216{
1217 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001218
Andy Flemingb31a1d82008-12-16 15:29:15 -08001219 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_EXTENDED_HASH) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001220 priv->extended_hash = 1;
1221 priv->hash_width = 9;
1222
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001223 priv->hash_regs[0] = &regs->igaddr0;
1224 priv->hash_regs[1] = &regs->igaddr1;
1225 priv->hash_regs[2] = &regs->igaddr2;
1226 priv->hash_regs[3] = &regs->igaddr3;
1227 priv->hash_regs[4] = &regs->igaddr4;
1228 priv->hash_regs[5] = &regs->igaddr5;
1229 priv->hash_regs[6] = &regs->igaddr6;
1230 priv->hash_regs[7] = &regs->igaddr7;
1231 priv->hash_regs[8] = &regs->gaddr0;
1232 priv->hash_regs[9] = &regs->gaddr1;
1233 priv->hash_regs[10] = &regs->gaddr2;
1234 priv->hash_regs[11] = &regs->gaddr3;
1235 priv->hash_regs[12] = &regs->gaddr4;
1236 priv->hash_regs[13] = &regs->gaddr5;
1237 priv->hash_regs[14] = &regs->gaddr6;
1238 priv->hash_regs[15] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001239
1240 } else {
1241 priv->extended_hash = 0;
1242 priv->hash_width = 8;
1243
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001244 priv->hash_regs[0] = &regs->gaddr0;
1245 priv->hash_regs[1] = &regs->gaddr1;
1246 priv->hash_regs[2] = &regs->gaddr2;
1247 priv->hash_regs[3] = &regs->gaddr3;
1248 priv->hash_regs[4] = &regs->gaddr4;
1249 priv->hash_regs[5] = &regs->gaddr5;
1250 priv->hash_regs[6] = &regs->gaddr6;
1251 priv->hash_regs[7] = &regs->gaddr7;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001252 }
Claudiu Manoil20862782014-02-17 12:53:14 +02001253}
1254
1255/* Set up the ethernet device structure, private data,
1256 * and anything else we need before we start
1257 */
1258static int gfar_probe(struct platform_device *ofdev)
1259{
1260 struct net_device *dev = NULL;
1261 struct gfar_private *priv = NULL;
1262 int err = 0, i;
1263
1264 err = gfar_of_init(ofdev, &dev);
1265
1266 if (err)
1267 return err;
1268
1269 priv = netdev_priv(dev);
1270 priv->ndev = dev;
1271 priv->ofdev = ofdev;
1272 priv->dev = &ofdev->dev;
1273 SET_NETDEV_DEV(dev, &ofdev->dev);
1274
1275 spin_lock_init(&priv->bflock);
1276 INIT_WORK(&priv->reset_task, gfar_reset_task);
1277
1278 platform_set_drvdata(ofdev, priv);
1279
1280 gfar_detect_errata(priv);
1281
Claudiu Manoil20862782014-02-17 12:53:14 +02001282 /* Set the dev->base_addr to the gfar reg region */
1283 dev->base_addr = (unsigned long) priv->gfargrp[0].regs;
1284
1285 /* Fill in the dev structure */
1286 dev->watchdog_timeo = TX_TIMEOUT;
1287 dev->mtu = 1500;
1288 dev->netdev_ops = &gfar_netdev_ops;
1289 dev->ethtool_ops = &gfar_ethtool_ops;
1290
1291 /* Register for napi ...We are registering NAPI for each grp */
Claudiu Manoil71ff9e32014-03-07 14:42:46 +02001292 for (i = 0; i < priv->num_grps; i++) {
1293 if (priv->poll_mode == GFAR_SQ_POLLING) {
1294 netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
1295 gfar_poll_rx_sq, GFAR_DEV_WEIGHT);
1296 netif_napi_add(dev, &priv->gfargrp[i].napi_tx,
1297 gfar_poll_tx_sq, 2);
1298 } else {
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02001299 netif_napi_add(dev, &priv->gfargrp[i].napi_rx,
1300 gfar_poll_rx, GFAR_DEV_WEIGHT);
1301 netif_napi_add(dev, &priv->gfargrp[i].napi_tx,
1302 gfar_poll_tx, 2);
1303 }
1304 }
Claudiu Manoil20862782014-02-17 12:53:14 +02001305
1306 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_CSUM) {
1307 dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG |
1308 NETIF_F_RXCSUM;
1309 dev->features |= NETIF_F_IP_CSUM | NETIF_F_SG |
1310 NETIF_F_RXCSUM | NETIF_F_HIGHDMA;
1311 }
1312
1313 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_VLAN) {
1314 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX |
1315 NETIF_F_HW_VLAN_CTAG_RX;
1316 dev->features |= NETIF_F_HW_VLAN_CTAG_RX;
1317 }
1318
1319 gfar_init_addr_hash_table(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001320
Claudiu Manoil532c37b2014-02-17 12:53:16 +02001321 /* Insert receive time stamps into padding alignment bytes */
1322 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
1323 priv->padding = 8;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001324
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00001325 if (dev->features & NETIF_F_IP_CSUM ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001326 priv->device_flags & FSL_GIANFAR_DEV_HAS_TIMER)
Wu Jiajun-B06378bee9e582012-05-21 23:00:48 +00001327 dev->needed_headroom = GMAC_FCB_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
1329 priv->rx_buffer_size = DEFAULT_RX_BUFFER_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001331 /* Initializing some of the rx/tx queue level parameters */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001332 for (i = 0; i < priv->num_tx_queues; i++) {
1333 priv->tx_queue[i]->tx_ring_size = DEFAULT_TX_RING_SIZE;
1334 priv->tx_queue[i]->num_txbdfree = DEFAULT_TX_RING_SIZE;
1335 priv->tx_queue[i]->txcoalescing = DEFAULT_TX_COALESCE;
1336 priv->tx_queue[i]->txic = DEFAULT_TXIC;
1337 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001338
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001339 for (i = 0; i < priv->num_rx_queues; i++) {
1340 priv->rx_queue[i]->rx_ring_size = DEFAULT_RX_RING_SIZE;
1341 priv->rx_queue[i]->rxcoalescing = DEFAULT_RX_COALESCE;
1342 priv->rx_queue[i]->rxic = DEFAULT_RXIC;
1343 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Jan Ceuleers0977f812012-06-05 03:42:12 +00001345 /* always enable rx filer */
Sebastian Poehn4aa3a712011-06-20 13:57:59 -07001346 priv->rx_filer_enable = 1;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001347 /* Enable most messages by default */
1348 priv->msg_enable = (NETIF_MSG_IFUP << 1 ) - 1;
Claudiu Manoilb98b8ba2012-09-23 22:39:08 +00001349 /* use pritority h/w tx queue scheduling for single queue devices */
1350 if (priv->num_tx_queues == 1)
1351 priv->prio_sched_en = 1;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001352
Claudiu Manoil08511332014-02-24 12:13:45 +02001353 set_bit(GFAR_DOWN, &priv->state);
1354
Claudiu Manoila328ac92014-02-24 12:13:42 +02001355 gfar_hw_init(priv);
Trent Piephod3eab822008-10-02 11:12:24 +00001356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 err = register_netdev(dev);
1358
1359 if (err) {
Joe Perches59deab22011-06-14 08:57:47 +00001360 pr_err("%s: Cannot register net device, aborting\n", dev->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 goto register_fail;
1362 }
1363
Claudiu Manoila328ac92014-02-24 12:13:42 +02001364 /* Carrier starts down, phylib will bring it up */
1365 netif_carrier_off(dev);
1366
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001367 device_init_wakeup(&dev->dev,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001368 priv->device_flags &
1369 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08001370
Dai Harukic50a5d92008-12-17 16:51:32 -08001371 /* fill out IRQ number and name fields */
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001372 for (i = 0; i < priv->num_grps; i++) {
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001373 struct gfar_priv_grp *grp = &priv->gfargrp[i];
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001374 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001375 sprintf(gfar_irq(grp, TX)->name, "%s%s%c%s",
Joe Perches0015e552012-03-25 07:10:07 +00001376 dev->name, "_g", '0' + i, "_tx");
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001377 sprintf(gfar_irq(grp, RX)->name, "%s%s%c%s",
Joe Perches0015e552012-03-25 07:10:07 +00001378 dev->name, "_g", '0' + i, "_rx");
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001379 sprintf(gfar_irq(grp, ER)->name, "%s%s%c%s",
Joe Perches0015e552012-03-25 07:10:07 +00001380 dev->name, "_g", '0' + i, "_er");
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001381 } else
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001382 strcpy(gfar_irq(grp, TX)->name, dev->name);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001383 }
Dai Harukic50a5d92008-12-17 16:51:32 -08001384
Sandeep Gopalpet7a8b3372009-11-02 07:03:40 +00001385 /* Initialize the filer table */
1386 gfar_init_filer_table(priv);
1387
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 /* Print out the device info */
Joe Perches59deab22011-06-14 08:57:47 +00001389 netdev_info(dev, "mac: %pM\n", dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390
Jan Ceuleers0977f812012-06-05 03:42:12 +00001391 /* Even more device info helps when determining which kernel
1392 * provided which set of benchmarks.
1393 */
Joe Perches59deab22011-06-14 08:57:47 +00001394 netdev_info(dev, "Running with NAPI enabled\n");
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001395 for (i = 0; i < priv->num_rx_queues; i++)
Joe Perches59deab22011-06-14 08:57:47 +00001396 netdev_info(dev, "RX BD ring size for Q[%d]: %d\n",
1397 i, priv->rx_queue[i]->rx_ring_size);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001398 for (i = 0; i < priv->num_tx_queues; i++)
Joe Perches59deab22011-06-14 08:57:47 +00001399 netdev_info(dev, "TX BD ring size for Q[%d]: %d\n",
1400 i, priv->tx_queue[i]->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401
1402 return 0;
1403
1404register_fail:
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001405 unmap_group_regs(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +02001406 gfar_free_rx_queues(priv);
1407 gfar_free_tx_queues(priv);
Grant Likelyfe192a42009-04-25 12:53:12 +00001408 if (priv->phy_node)
1409 of_node_put(priv->phy_node);
1410 if (priv->tbi_node)
1411 of_node_put(priv->tbi_node);
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001412 free_gfar_dev(priv);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001413 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414}
1415
Grant Likely2dc11582010-08-06 09:25:50 -06001416static int gfar_remove(struct platform_device *ofdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417{
Jingoo Han8513fbd2013-05-23 00:52:31 +00001418 struct gfar_private *priv = platform_get_drvdata(ofdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
Grant Likelyfe192a42009-04-25 12:53:12 +00001420 if (priv->phy_node)
1421 of_node_put(priv->phy_node);
1422 if (priv->tbi_node)
1423 of_node_put(priv->tbi_node);
1424
David S. Millerd9d8e042009-09-06 01:41:02 -07001425 unregister_netdev(priv->ndev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001426 unmap_group_regs(priv);
Claudiu Manoil20862782014-02-17 12:53:14 +02001427 gfar_free_rx_queues(priv);
1428 gfar_free_tx_queues(priv);
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001429 free_gfar_dev(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001430
1431 return 0;
1432}
1433
Scott Woodd87eb122008-07-11 18:04:45 -05001434#ifdef CONFIG_PM
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001435
1436static int gfar_suspend(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001437{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001438 struct gfar_private *priv = dev_get_drvdata(dev);
1439 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001440 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001441 unsigned long flags;
1442 u32 tempval;
1443
1444 int magic_packet = priv->wol_en &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001445 (priv->device_flags &
1446 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001447
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001448 netif_device_detach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001449
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001450 if (netif_running(ndev)) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001451
1452 local_irq_save(flags);
1453 lock_tx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001454
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001455 gfar_halt_nodisable(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001456
1457 /* Disable Tx, and Rx if wake-on-LAN is disabled. */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001458 tempval = gfar_read(&regs->maccfg1);
Scott Woodd87eb122008-07-11 18:04:45 -05001459
1460 tempval &= ~MACCFG1_TX_EN;
1461
1462 if (!magic_packet)
1463 tempval &= ~MACCFG1_RX_EN;
1464
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001465 gfar_write(&regs->maccfg1, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001466
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001467 unlock_tx_qs(priv);
1468 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001469
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001470 disable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001471
1472 if (magic_packet) {
1473 /* Enable interrupt on Magic Packet */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001474 gfar_write(&regs->imask, IMASK_MAG);
Scott Woodd87eb122008-07-11 18:04:45 -05001475
1476 /* Enable Magic Packet mode */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001477 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001478 tempval |= MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001479 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001480 } else {
1481 phy_stop(priv->phydev);
1482 }
1483 }
1484
1485 return 0;
1486}
1487
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001488static int gfar_resume(struct device *dev)
Scott Woodd87eb122008-07-11 18:04:45 -05001489{
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001490 struct gfar_private *priv = dev_get_drvdata(dev);
1491 struct net_device *ndev = priv->ndev;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001492 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001493 unsigned long flags;
1494 u32 tempval;
1495 int magic_packet = priv->wol_en &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001496 (priv->device_flags &
1497 FSL_GIANFAR_DEV_HAS_MAGIC_PACKET);
Scott Woodd87eb122008-07-11 18:04:45 -05001498
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001499 if (!netif_running(ndev)) {
1500 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001501 return 0;
1502 }
1503
1504 if (!magic_packet && priv->phydev)
1505 phy_start(priv->phydev);
1506
1507 /* Disable Magic Packet mode, in case something
1508 * else woke us up.
1509 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001510 local_irq_save(flags);
1511 lock_tx_qs(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001512
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001513 tempval = gfar_read(&regs->maccfg2);
Scott Woodd87eb122008-07-11 18:04:45 -05001514 tempval &= ~MACCFG2_MPEN;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001515 gfar_write(&regs->maccfg2, tempval);
Scott Woodd87eb122008-07-11 18:04:45 -05001516
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001517 gfar_start(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001518
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001519 unlock_tx_qs(priv);
1520 local_irq_restore(flags);
Scott Woodd87eb122008-07-11 18:04:45 -05001521
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001522 netif_device_attach(ndev);
Scott Woodd87eb122008-07-11 18:04:45 -05001523
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001524 enable_napi(priv);
Scott Woodd87eb122008-07-11 18:04:45 -05001525
1526 return 0;
1527}
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001528
1529static int gfar_restore(struct device *dev)
1530{
1531 struct gfar_private *priv = dev_get_drvdata(dev);
1532 struct net_device *ndev = priv->ndev;
1533
Wang Dongsheng103cdd12012-11-09 04:43:51 +00001534 if (!netif_running(ndev)) {
1535 netif_device_attach(ndev);
1536
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001537 return 0;
Wang Dongsheng103cdd12012-11-09 04:43:51 +00001538 }
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001539
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +00001540 if (gfar_init_bds(ndev)) {
1541 free_skb_resources(priv);
1542 return -ENOMEM;
1543 }
1544
Claudiu Manoila328ac92014-02-24 12:13:42 +02001545 gfar_mac_reset(priv);
1546
1547 gfar_init_tx_rx_base(priv);
1548
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001549 gfar_start(priv);
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001550
1551 priv->oldlink = 0;
1552 priv->oldspeed = 0;
1553 priv->oldduplex = -1;
1554
1555 if (priv->phydev)
1556 phy_start(priv->phydev);
1557
1558 netif_device_attach(ndev);
Anton Vorontsov5ea681d2009-11-10 14:11:05 +00001559 enable_napi(priv);
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001560
1561 return 0;
1562}
1563
1564static struct dev_pm_ops gfar_pm_ops = {
1565 .suspend = gfar_suspend,
1566 .resume = gfar_resume,
1567 .freeze = gfar_suspend,
1568 .thaw = gfar_resume,
1569 .restore = gfar_restore,
1570};
1571
1572#define GFAR_PM_OPS (&gfar_pm_ops)
1573
Scott Woodd87eb122008-07-11 18:04:45 -05001574#else
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001575
1576#define GFAR_PM_OPS NULL
Anton Vorontsovbe926fc2009-10-12 06:00:42 +00001577
Scott Woodd87eb122008-07-11 18:04:45 -05001578#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001580/* Reads the controller's registers to determine what interface
1581 * connects it to the PHY.
1582 */
1583static phy_interface_t gfar_get_interface(struct net_device *dev)
1584{
1585 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001586 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001587 u32 ecntrl;
1588
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001589 ecntrl = gfar_read(&regs->ecntrl);
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001590
1591 if (ecntrl & ECNTRL_SGMII_MODE)
1592 return PHY_INTERFACE_MODE_SGMII;
1593
1594 if (ecntrl & ECNTRL_TBI_MODE) {
1595 if (ecntrl & ECNTRL_REDUCED_MODE)
1596 return PHY_INTERFACE_MODE_RTBI;
1597 else
1598 return PHY_INTERFACE_MODE_TBI;
1599 }
1600
1601 if (ecntrl & ECNTRL_REDUCED_MODE) {
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001602 if (ecntrl & ECNTRL_REDUCED_MII_MODE) {
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001603 return PHY_INTERFACE_MODE_RMII;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001604 }
Andy Fleming7132ab72007-07-11 11:43:07 -05001605 else {
Andy Flemingb31a1d82008-12-16 15:29:15 -08001606 phy_interface_t interface = priv->interface;
Andy Fleming7132ab72007-07-11 11:43:07 -05001607
Jan Ceuleers0977f812012-06-05 03:42:12 +00001608 /* This isn't autodetected right now, so it must
Andy Fleming7132ab72007-07-11 11:43:07 -05001609 * be set by the device tree or platform code.
1610 */
1611 if (interface == PHY_INTERFACE_MODE_RGMII_ID)
1612 return PHY_INTERFACE_MODE_RGMII_ID;
1613
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001614 return PHY_INTERFACE_MODE_RGMII;
Andy Fleming7132ab72007-07-11 11:43:07 -05001615 }
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001616 }
1617
Andy Flemingb31a1d82008-12-16 15:29:15 -08001618 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT)
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001619 return PHY_INTERFACE_MODE_GMII;
1620
1621 return PHY_INTERFACE_MODE_MII;
1622}
1623
1624
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001625/* Initializes driver's PHY state, and attaches to the PHY.
1626 * Returns 0 on success.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001627 */
1628static int init_phy(struct net_device *dev)
1629{
1630 struct gfar_private *priv = netdev_priv(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001631 uint gigabit_support =
Andy Flemingb31a1d82008-12-16 15:29:15 -08001632 priv->device_flags & FSL_GIANFAR_DEV_HAS_GIGABIT ?
Claudiu Manoil23402bd2013-08-12 13:53:26 +03001633 GFAR_SUPPORTED_GBIT : 0;
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001634 phy_interface_t interface;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635
1636 priv->oldlink = 0;
1637 priv->oldspeed = 0;
1638 priv->oldduplex = -1;
1639
Andy Fleminge8a2b6a2006-12-01 12:01:06 -06001640 interface = gfar_get_interface(dev);
1641
Anton Vorontsov1db780f2009-07-16 21:31:42 +00001642 priv->phydev = of_phy_connect(dev, priv->phy_node, &adjust_link, 0,
1643 interface);
1644 if (!priv->phydev)
1645 priv->phydev = of_phy_connect_fixed_link(dev, &adjust_link,
1646 interface);
1647 if (!priv->phydev) {
1648 dev_err(&dev->dev, "could not attach to PHY\n");
1649 return -ENODEV;
Grant Likelyfe192a42009-04-25 12:53:12 +00001650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Kapil Junejad3c12872007-05-11 18:25:11 -05001652 if (interface == PHY_INTERFACE_MODE_SGMII)
1653 gfar_configure_serdes(dev);
1654
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001655 /* Remove any features not supported by the controller */
Grant Likelyfe192a42009-04-25 12:53:12 +00001656 priv->phydev->supported &= (GFAR_SUPPORTED | gigabit_support);
1657 priv->phydev->advertising = priv->phydev->supported;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658
1659 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660}
1661
Jan Ceuleers0977f812012-06-05 03:42:12 +00001662/* Initialize TBI PHY interface for communicating with the
Paul Gortmakerd0313582008-04-17 00:08:10 -04001663 * SERDES lynx PHY on the chip. We communicate with this PHY
1664 * through the MDIO bus on each controller, treating it as a
1665 * "normal" PHY at the address found in the TBIPA register. We assume
1666 * that the TBIPA register is valid. Either the MDIO bus code will set
1667 * it to a value that doesn't conflict with other PHYs on the bus, or the
1668 * value doesn't matter, as there are no other PHYs on the bus.
1669 */
Kapil Junejad3c12872007-05-11 18:25:11 -05001670static void gfar_configure_serdes(struct net_device *dev)
1671{
1672 struct gfar_private *priv = netdev_priv(dev);
Grant Likelyfe192a42009-04-25 12:53:12 +00001673 struct phy_device *tbiphy;
Trent Piephoc1324192008-10-30 18:17:06 -07001674
Grant Likelyfe192a42009-04-25 12:53:12 +00001675 if (!priv->tbi_node) {
1676 dev_warn(&dev->dev, "error: SGMII mode requires that the "
1677 "device tree specify a tbi-handle\n");
1678 return;
1679 }
1680
1681 tbiphy = of_phy_find_device(priv->tbi_node);
1682 if (!tbiphy) {
1683 dev_err(&dev->dev, "error: Could not get TBI device\n");
Andy Flemingb31a1d82008-12-16 15:29:15 -08001684 return;
1685 }
Kapil Junejad3c12872007-05-11 18:25:11 -05001686
Jan Ceuleers0977f812012-06-05 03:42:12 +00001687 /* If the link is already up, we must already be ok, and don't need to
Trent Piephobdb59f92008-10-30 18:17:07 -07001688 * configure and reset the TBI<->SerDes link. Maybe U-Boot configured
1689 * everything for us? Resetting it takes the link down and requires
1690 * several seconds for it to come back.
1691 */
Grant Likelyfe192a42009-04-25 12:53:12 +00001692 if (phy_read(tbiphy, MII_BMSR) & BMSR_LSTATUS)
Andy Flemingb31a1d82008-12-16 15:29:15 -08001693 return;
Kapil Junejad3c12872007-05-11 18:25:11 -05001694
Paul Gortmakerd0313582008-04-17 00:08:10 -04001695 /* Single clk mode, mii mode off(for serdes communication) */
Grant Likelyfe192a42009-04-25 12:53:12 +00001696 phy_write(tbiphy, MII_TBICON, TBICON_CLK_SELECT);
Kapil Junejad3c12872007-05-11 18:25:11 -05001697
Grant Likelyfe192a42009-04-25 12:53:12 +00001698 phy_write(tbiphy, MII_ADVERTISE,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001699 ADVERTISE_1000XFULL | ADVERTISE_1000XPAUSE |
1700 ADVERTISE_1000XPSE_ASYM);
Kapil Junejad3c12872007-05-11 18:25:11 -05001701
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001702 phy_write(tbiphy, MII_BMCR,
1703 BMCR_ANENABLE | BMCR_ANRESTART | BMCR_FULLDPLX |
1704 BMCR_SPEED1000);
Kapil Junejad3c12872007-05-11 18:25:11 -05001705}
1706
Anton Vorontsov511d9342010-06-30 06:39:15 +00001707static int __gfar_is_rx_idle(struct gfar_private *priv)
1708{
1709 u32 res;
1710
Jan Ceuleers0977f812012-06-05 03:42:12 +00001711 /* Normaly TSEC should not hang on GRS commands, so we should
Anton Vorontsov511d9342010-06-30 06:39:15 +00001712 * actually wait for IEVENT_GRSC flag.
1713 */
Claudiu Manoilad3660c2013-10-09 20:20:40 +03001714 if (!gfar_has_errata(priv, GFAR_ERRATA_A002))
Anton Vorontsov511d9342010-06-30 06:39:15 +00001715 return 0;
1716
Jan Ceuleers0977f812012-06-05 03:42:12 +00001717 /* Read the eTSEC register at offset 0xD1C. If bits 7-14 are
Anton Vorontsov511d9342010-06-30 06:39:15 +00001718 * the same as bits 23-30, the eTSEC Rx is assumed to be idle
1719 * and the Rx can be safely reset.
1720 */
1721 res = gfar_read((void __iomem *)priv->gfargrp[0].regs + 0xd1c);
1722 res &= 0x7f807f80;
1723 if ((res & 0xffff) == (res >> 16))
1724 return 1;
1725
1726 return 0;
1727}
Kumar Gala0bbaf062005-06-20 10:54:21 -05001728
1729/* Halt the receive and transmit queues */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001730static void gfar_halt_nodisable(struct gfar_private *priv)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731{
Claudiu Manoilefeddce2014-02-17 12:53:17 +02001732 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001733 u32 tempval;
1734
Claudiu Manoilefeddce2014-02-17 12:53:17 +02001735 gfar_ints_disable(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 /* Stop the DMA, and wait for it to stop */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001738 tempval = gfar_read(&regs->dmactrl);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001739 if ((tempval & (DMACTRL_GRS | DMACTRL_GTS)) !=
1740 (DMACTRL_GRS | DMACTRL_GTS)) {
Anton Vorontsov511d9342010-06-30 06:39:15 +00001741 int ret;
1742
Linus Torvalds1da177e2005-04-16 15:20:36 -07001743 tempval |= (DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001744 gfar_write(&regs->dmactrl, tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745
Anton Vorontsov511d9342010-06-30 06:39:15 +00001746 do {
1747 ret = spin_event_timeout(((gfar_read(&regs->ievent) &
1748 (IEVENT_GRSC | IEVENT_GTSC)) ==
1749 (IEVENT_GRSC | IEVENT_GTSC)), 1000000, 0);
1750 if (!ret && !(gfar_read(&regs->ievent) & IEVENT_GRSC))
1751 ret = __gfar_is_rx_idle(priv);
1752 } while (!ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 }
Scott Woodd87eb122008-07-11 18:04:45 -05001754}
Scott Woodd87eb122008-07-11 18:04:45 -05001755
1756/* Halt the receive and transmit queues */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001757void gfar_halt(struct gfar_private *priv)
Scott Woodd87eb122008-07-11 18:04:45 -05001758{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001759 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Scott Woodd87eb122008-07-11 18:04:45 -05001760 u32 tempval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001761
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001762 /* Dissable the Rx/Tx hw queues */
1763 gfar_write(&regs->rqueue, 0);
1764 gfar_write(&regs->tqueue, 0);
Scott Wood2a54adc2008-08-12 15:10:46 -05001765
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001766 mdelay(10);
1767
1768 gfar_halt_nodisable(priv);
1769
1770 /* Disable Rx/Tx DMA */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771 tempval = gfar_read(&regs->maccfg1);
1772 tempval &= ~(MACCFG1_RX_EN | MACCFG1_TX_EN);
1773 gfar_write(&regs->maccfg1, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001774}
1775
1776void stop_gfar(struct net_device *dev)
1777{
1778 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001779
Claudiu Manoil08511332014-02-24 12:13:45 +02001780 netif_tx_stop_all_queues(dev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04001781
Claudiu Manoil08511332014-02-24 12:13:45 +02001782 smp_mb__before_clear_bit();
1783 set_bit(GFAR_DOWN, &priv->state);
1784 smp_mb__after_clear_bit();
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001785
Claudiu Manoil08511332014-02-24 12:13:45 +02001786 disable_napi(priv);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001787
Claudiu Manoil08511332014-02-24 12:13:45 +02001788 /* disable ints and gracefully shut down Rx/Tx DMA */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001789 gfar_halt(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Claudiu Manoil08511332014-02-24 12:13:45 +02001791 phy_stop(priv->phydev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793 free_skb_resources(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001794}
1795
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001796static void free_skb_tx_queue(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001797{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 struct txbd8 *txbdp;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001799 struct gfar_private *priv = netdev_priv(tx_queue->dev);
Dai Haruki4669bc92008-12-17 16:51:04 -08001800 int i, j;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001802 txbdp = tx_queue->tx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001804 for (i = 0; i < tx_queue->tx_ring_size; i++) {
1805 if (!tx_queue->tx_skbuff[i])
Dai Haruki4669bc92008-12-17 16:51:04 -08001806 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001807
Claudiu Manoil369ec162013-02-14 05:00:02 +00001808 dma_unmap_single(priv->dev, txbdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001809 txbdp->length, DMA_TO_DEVICE);
Dai Haruki4669bc92008-12-17 16:51:04 -08001810 txbdp->lstatus = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001811 for (j = 0; j < skb_shinfo(tx_queue->tx_skbuff[i])->nr_frags;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001812 j++) {
Dai Haruki4669bc92008-12-17 16:51:04 -08001813 txbdp++;
Claudiu Manoil369ec162013-02-14 05:00:02 +00001814 dma_unmap_page(priv->dev, txbdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001815 txbdp->length, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 }
Andy Flemingad5da7a2008-05-07 13:20:55 -05001817 txbdp++;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001818 dev_kfree_skb_any(tx_queue->tx_skbuff[i]);
1819 tx_queue->tx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001820 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001821 kfree(tx_queue->tx_skbuff);
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +00001822 tx_queue->tx_skbuff = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001823}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001825static void free_skb_rx_queue(struct gfar_priv_rx_q *rx_queue)
1826{
1827 struct rxbd8 *rxbdp;
1828 struct gfar_private *priv = netdev_priv(rx_queue->dev);
1829 int i;
1830
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001831 rxbdp = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001832
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001833 for (i = 0; i < rx_queue->rx_ring_size; i++) {
1834 if (rx_queue->rx_skbuff[i]) {
Claudiu Manoil369ec162013-02-14 05:00:02 +00001835 dma_unmap_single(priv->dev, rxbdp->bufPtr,
1836 priv->rx_buffer_size,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001837 DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001838 dev_kfree_skb_any(rx_queue->rx_skbuff[i]);
1839 rx_queue->rx_skbuff[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 }
Anton Vorontsove69edd22009-10-12 06:00:30 +00001841 rxbdp->lstatus = 0;
1842 rxbdp->bufPtr = 0;
1843 rxbdp++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 }
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00001845 kfree(rx_queue->rx_skbuff);
Claudiu Manoil1eb8f7a2012-11-08 22:11:41 +00001846 rx_queue->rx_skbuff = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001847}
Anton Vorontsove69edd22009-10-12 06:00:30 +00001848
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001849/* If there are any tx skbs or rx skbs still around, free them.
Jan Ceuleers0977f812012-06-05 03:42:12 +00001850 * Then free tx_skbuff and rx_skbuff
1851 */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001852static void free_skb_resources(struct gfar_private *priv)
1853{
1854 struct gfar_priv_tx_q *tx_queue = NULL;
1855 struct gfar_priv_rx_q *rx_queue = NULL;
1856 int i;
1857
1858 /* Go through all the buffer descriptors and free their data buffers */
1859 for (i = 0; i < priv->num_tx_queues; i++) {
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05001860 struct netdev_queue *txq;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001861
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001862 tx_queue = priv->tx_queue[i];
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05001863 txq = netdev_get_tx_queue(tx_queue->dev, tx_queue->qindex);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001864 if (tx_queue->tx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001865 free_skb_tx_queue(tx_queue);
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05001866 netdev_tx_reset_queue(txq);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001867 }
1868
1869 for (i = 0; i < priv->num_rx_queues; i++) {
1870 rx_queue = priv->rx_queue[i];
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001871 if (rx_queue->rx_skbuff)
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00001872 free_skb_rx_queue(rx_queue);
1873 }
1874
Claudiu Manoil369ec162013-02-14 05:00:02 +00001875 dma_free_coherent(priv->dev,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00001876 sizeof(struct txbd8) * priv->total_tx_ring_size +
1877 sizeof(struct rxbd8) * priv->total_rx_ring_size,
1878 priv->tx_queue[0]->tx_bd_base,
1879 priv->tx_queue[0]->tx_bd_dma_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001880}
1881
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001882void gfar_start(struct gfar_private *priv)
Kumar Gala0bbaf062005-06-20 10:54:21 -05001883{
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001884 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001885 u32 tempval;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001886 int i = 0;
Kumar Gala0bbaf062005-06-20 10:54:21 -05001887
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001888 /* Enable Rx/Tx hw queues */
1889 gfar_write(&regs->rqueue, priv->rqueue);
1890 gfar_write(&regs->tqueue, priv->tqueue);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001891
1892 /* Initialize DMACTRL to have WWR and WOP */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001893 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001894 tempval |= DMACTRL_INIT_SETTINGS;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001895 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001896
Kumar Gala0bbaf062005-06-20 10:54:21 -05001897 /* Make sure we aren't stopped */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001898 tempval = gfar_read(&regs->dmactrl);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001899 tempval &= ~(DMACTRL_GRS | DMACTRL_GTS);
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00001900 gfar_write(&regs->dmactrl, tempval);
Kumar Gala0bbaf062005-06-20 10:54:21 -05001901
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001902 for (i = 0; i < priv->num_grps; i++) {
1903 regs = priv->gfargrp[i].regs;
1904 /* Clear THLT/RHLT, so that the DMA starts polling now */
1905 gfar_write(&regs->tstat, priv->gfargrp[i].tstat);
1906 gfar_write(&regs->rstat, priv->gfargrp[i].rstat);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001907 }
Dai Haruki12dea572008-12-16 15:30:20 -08001908
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001909 /* Enable Rx/Tx DMA */
1910 tempval = gfar_read(&regs->maccfg1);
1911 tempval |= (MACCFG1_RX_EN | MACCFG1_TX_EN);
1912 gfar_write(&regs->maccfg1, tempval);
1913
Claudiu Manoilefeddce2014-02-17 12:53:17 +02001914 gfar_ints_enable(priv);
1915
Claudiu Manoilc10650b2014-02-17 12:53:18 +02001916 priv->ndev->trans_start = jiffies; /* prevent tx timeout */
Kumar Gala0bbaf062005-06-20 10:54:21 -05001917}
1918
Claudiu Manoil80ec3962014-02-24 12:13:44 +02001919static void free_grp_irqs(struct gfar_priv_grp *grp)
1920{
1921 free_irq(gfar_irq(grp, TX)->irq, grp);
1922 free_irq(gfar_irq(grp, RX)->irq, grp);
1923 free_irq(gfar_irq(grp, ER)->irq, grp);
1924}
1925
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001926static int register_grp_irqs(struct gfar_priv_grp *grp)
1927{
1928 struct gfar_private *priv = grp->priv;
1929 struct net_device *dev = priv->ndev;
Anton Vorontsovccc05c62009-10-12 06:00:26 +00001930 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931
Linus Torvalds1da177e2005-04-16 15:20:36 -07001932 /* If the device has multiple interrupts, register for
Jan Ceuleers0977f812012-06-05 03:42:12 +00001933 * them. Otherwise, only register for the one
1934 */
Andy Flemingb31a1d82008-12-16 15:29:15 -08001935 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Kumar Gala0bbaf062005-06-20 10:54:21 -05001936 /* Install our interrupt handlers for Error,
Jan Ceuleers0977f812012-06-05 03:42:12 +00001937 * Transmit, and Receive
1938 */
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001939 err = request_irq(gfar_irq(grp, ER)->irq, gfar_error, 0,
1940 gfar_irq(grp, ER)->name, grp);
1941 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00001942 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001943 gfar_irq(grp, ER)->irq);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001944
Julia Lawall2145f1a2010-08-05 10:26:20 +00001945 goto err_irq_fail;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 }
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001947 err = request_irq(gfar_irq(grp, TX)->irq, gfar_transmit, 0,
1948 gfar_irq(grp, TX)->name, grp);
1949 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00001950 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001951 gfar_irq(grp, TX)->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 goto tx_irq_fail;
1953 }
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001954 err = request_irq(gfar_irq(grp, RX)->irq, gfar_receive, 0,
1955 gfar_irq(grp, RX)->name, grp);
1956 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00001957 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001958 gfar_irq(grp, RX)->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 goto rx_irq_fail;
1960 }
1961 } else {
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001962 err = request_irq(gfar_irq(grp, TX)->irq, gfar_interrupt, 0,
1963 gfar_irq(grp, TX)->name, grp);
1964 if (err < 0) {
Joe Perches59deab22011-06-14 08:57:47 +00001965 netif_err(priv, intr, dev, "Can't get IRQ %d\n",
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001966 gfar_irq(grp, TX)->irq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 goto err_irq_fail;
1968 }
1969 }
1970
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001971 return 0;
1972
1973rx_irq_fail:
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001974 free_irq(gfar_irq(grp, TX)->irq, grp);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001975tx_irq_fail:
Claudiu Manoilee873fd2013-01-29 03:55:12 +00001976 free_irq(gfar_irq(grp, ER)->irq, grp);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00001977err_irq_fail:
1978 return err;
1979
1980}
1981
Claudiu Manoil80ec3962014-02-24 12:13:44 +02001982static void gfar_free_irq(struct gfar_private *priv)
1983{
1984 int i;
1985
1986 /* Free the IRQs */
1987 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
1988 for (i = 0; i < priv->num_grps; i++)
1989 free_grp_irqs(&priv->gfargrp[i]);
1990 } else {
1991 for (i = 0; i < priv->num_grps; i++)
1992 free_irq(gfar_irq(&priv->gfargrp[i], TX)->irq,
1993 &priv->gfargrp[i]);
1994 }
1995}
1996
1997static int gfar_request_irq(struct gfar_private *priv)
1998{
1999 int err, i, j;
2000
2001 for (i = 0; i < priv->num_grps; i++) {
2002 err = register_grp_irqs(&priv->gfargrp[i]);
2003 if (err) {
2004 for (j = 0; j < i; j++)
2005 free_grp_irqs(&priv->gfargrp[j]);
2006 return err;
2007 }
2008 }
2009
2010 return 0;
2011}
2012
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002013/* Bring the controller up and running */
2014int startup_gfar(struct net_device *ndev)
2015{
2016 struct gfar_private *priv = netdev_priv(ndev);
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002017 int err;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002018
Claudiu Manoila328ac92014-02-24 12:13:42 +02002019 gfar_mac_reset(priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002020
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002021 err = gfar_alloc_skb_resources(ndev);
2022 if (err)
2023 return err;
2024
Claudiu Manoila328ac92014-02-24 12:13:42 +02002025 gfar_init_tx_rx_base(priv);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002026
Claudiu Manoil08511332014-02-24 12:13:45 +02002027 smp_mb__before_clear_bit();
2028 clear_bit(GFAR_DOWN, &priv->state);
2029 smp_mb__after_clear_bit();
2030
2031 /* Start Rx/Tx DMA and enable the interrupts */
Claudiu Manoilc10650b2014-02-17 12:53:18 +02002032 gfar_start(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002033
Anton Vorontsov826aa4a2009-10-12 06:00:34 +00002034 phy_start(priv->phydev);
2035
Claudiu Manoil08511332014-02-24 12:13:45 +02002036 enable_napi(priv);
2037
2038 netif_tx_wake_all_queues(ndev);
2039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041}
2042
Jan Ceuleers0977f812012-06-05 03:42:12 +00002043/* Called when something needs to use the ethernet device
2044 * Returns 0 for success.
2045 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046static int gfar_enet_open(struct net_device *dev)
2047{
Li Yang94e8cc32007-10-12 21:53:51 +08002048 struct gfar_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 int err;
2050
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 err = init_phy(dev);
Claudiu Manoil08511332014-02-24 12:13:45 +02002052 if (err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053 return err;
2054
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002055 err = gfar_request_irq(priv);
2056 if (err)
2057 return err;
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 err = startup_gfar(dev);
Claudiu Manoil08511332014-02-24 12:13:45 +02002060 if (err)
Anton Vorontsovdb0e8e32007-10-17 23:57:46 +04002061 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062
Anton Vorontsov2884e5c2009-02-01 00:52:34 -08002063 device_set_wakeup_enable(&dev->dev, priv->wol_en);
2064
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 return err;
2066}
2067
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002068static inline struct txfcb *gfar_add_fcb(struct sk_buff *skb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002069{
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002070 struct txfcb *fcb = (struct txfcb *)skb_push(skb, GMAC_FCB_LEN);
Kumar Gala6c31d552009-04-28 08:04:10 -07002071
2072 memset(fcb, 0, GMAC_FCB_LEN);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002073
Kumar Gala0bbaf062005-06-20 10:54:21 -05002074 return fcb;
2075}
2076
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002077static inline void gfar_tx_checksum(struct sk_buff *skb, struct txfcb *fcb,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002078 int fcb_length)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002079{
Kumar Gala0bbaf062005-06-20 10:54:21 -05002080 /* If we're here, it's a IP packet with a TCP or UDP
2081 * payload. We set it to checksum, using a pseudo-header
2082 * we provide
2083 */
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +00002084 u8 flags = TXFCB_DEFAULT;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002085
Jan Ceuleers0977f812012-06-05 03:42:12 +00002086 /* Tell the controller what the protocol is
2087 * And provide the already calculated phcs
2088 */
Arnaldo Carvalho de Meloeddc9ec2007-04-20 22:47:35 -07002089 if (ip_hdr(skb)->protocol == IPPROTO_UDP) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06002090 flags |= TXFCB_UDP;
Arnaldo Carvalho de Melo4bedb452007-03-13 14:28:48 -03002091 fcb->phcs = udp_hdr(skb)->check;
Andy Fleming7f7f5312005-11-11 12:38:59 -06002092 } else
Kumar Gala8da32de2007-06-29 00:12:04 -05002093 fcb->phcs = tcp_hdr(skb)->check;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002094
2095 /* l3os is the distance between the start of the
2096 * frame (skb->data) and the start of the IP hdr.
2097 * l4os is the distance between the start of the
Jan Ceuleers0977f812012-06-05 03:42:12 +00002098 * l3 hdr and the l4 hdr
2099 */
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002100 fcb->l3os = (u16)(skb_network_offset(skb) - fcb_length);
Arnaldo Carvalho de Melocfe1fc72007-03-16 17:26:39 -03002101 fcb->l4os = skb_network_header_len(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002102
Andy Fleming7f7f5312005-11-11 12:38:59 -06002103 fcb->flags = flags;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002104}
2105
Andy Fleming7f7f5312005-11-11 12:38:59 -06002106void inline gfar_tx_vlan(struct sk_buff *skb, struct txfcb *fcb)
Kumar Gala0bbaf062005-06-20 10:54:21 -05002107{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002108 fcb->flags |= TXFCB_VLN;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002109 fcb->vlctl = vlan_tx_tag_get(skb);
2110}
2111
Dai Haruki4669bc92008-12-17 16:51:04 -08002112static inline struct txbd8 *skip_txbd(struct txbd8 *bdp, int stride,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002113 struct txbd8 *base, int ring_size)
Dai Haruki4669bc92008-12-17 16:51:04 -08002114{
2115 struct txbd8 *new_bd = bdp + stride;
2116
2117 return (new_bd >= (base + ring_size)) ? (new_bd - ring_size) : new_bd;
2118}
2119
2120static inline struct txbd8 *next_txbd(struct txbd8 *bdp, struct txbd8 *base,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002121 int ring_size)
Dai Haruki4669bc92008-12-17 16:51:04 -08002122{
2123 return skip_txbd(bdp, 1, base, ring_size);
2124}
2125
Claudiu Manoil02d88fb2013-08-05 17:20:09 +03002126/* eTSEC12: csum generation not supported for some fcb offsets */
2127static inline bool gfar_csum_errata_12(struct gfar_private *priv,
2128 unsigned long fcb_addr)
2129{
2130 return (gfar_has_errata(priv, GFAR_ERRATA_12) &&
2131 (fcb_addr % 0x20) > 0x18);
2132}
2133
2134/* eTSEC76: csum generation for frames larger than 2500 may
2135 * cause excess delays before start of transmission
2136 */
2137static inline bool gfar_csum_errata_76(struct gfar_private *priv,
2138 unsigned int len)
2139{
2140 return (gfar_has_errata(priv, GFAR_ERRATA_76) &&
2141 (len > 2500));
2142}
2143
Jan Ceuleers0977f812012-06-05 03:42:12 +00002144/* This is called by the kernel when a frame is ready for transmission.
2145 * It is pointed to by the dev->hard_start_xmit function pointer
2146 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147static int gfar_start_xmit(struct sk_buff *skb, struct net_device *dev)
2148{
2149 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002150 struct gfar_priv_tx_q *tx_queue = NULL;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002151 struct netdev_queue *txq;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002152 struct gfar __iomem *regs = NULL;
Kumar Gala0bbaf062005-06-20 10:54:21 -05002153 struct txfcb *fcb = NULL;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002154 struct txbd8 *txbdp, *txbdp_start, *base, *txbdp_tstamp = NULL;
Dai Haruki5a5efed2008-12-16 15:34:50 -08002155 u32 lstatus;
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002156 int i, rq = 0;
2157 int do_tstamp, do_csum, do_vlan;
Dai Haruki4669bc92008-12-17 16:51:04 -08002158 u32 bufaddr;
Andy Flemingfef61082006-04-20 16:44:29 -05002159 unsigned long flags;
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002160 unsigned int nr_frags, nr_txbds, bytes_sent, fcb_len = 0;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002161
2162 rq = skb->queue_mapping;
2163 tx_queue = priv->tx_queue[rq];
2164 txq = netdev_get_tx_queue(dev, rq);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002165 base = tx_queue->tx_bd_base;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002166 regs = tx_queue->grp->regs;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002167
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002168 do_csum = (CHECKSUM_PARTIAL == skb->ip_summed);
2169 do_vlan = vlan_tx_tag_present(skb);
2170 do_tstamp = (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP) &&
2171 priv->hwts_tx_en;
2172
2173 if (do_csum || do_vlan)
2174 fcb_len = GMAC_FCB_LEN;
2175
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002176 /* check if time stamp should be generated */
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002177 if (unlikely(do_tstamp))
2178 fcb_len = GMAC_FCB_LEN + GMAC_TXPAL_LEN;
Dai Haruki4669bc92008-12-17 16:51:04 -08002179
Li Yang5b28bea2009-03-27 15:54:30 -07002180 /* make space for additional header when fcb is needed */
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002181 if (fcb_len && unlikely(skb_headroom(skb) < fcb_len)) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002182 struct sk_buff *skb_new;
2183
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002184 skb_new = skb_realloc_headroom(skb, fcb_len);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002185 if (!skb_new) {
2186 dev->stats.tx_errors++;
David S. Millerbd14ba82009-03-27 01:10:58 -07002187 kfree_skb(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002188 return NETDEV_TX_OK;
2189 }
Manfred Rudigierdb83d132012-01-09 23:26:50 +00002190
Eric Dumazet313b0372012-07-05 11:45:13 +00002191 if (skb->sk)
2192 skb_set_owner_w(skb_new, skb->sk);
2193 consume_skb(skb);
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002194 skb = skb_new;
2195 }
2196
Dai Haruki4669bc92008-12-17 16:51:04 -08002197 /* total number of fragments in the SKB */
2198 nr_frags = skb_shinfo(skb)->nr_frags;
2199
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002200 /* calculate the required number of TxBDs for this skb */
2201 if (unlikely(do_tstamp))
2202 nr_txbds = nr_frags + 2;
2203 else
2204 nr_txbds = nr_frags + 1;
2205
Dai Haruki4669bc92008-12-17 16:51:04 -08002206 /* check if there is space to queue this packet */
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002207 if (nr_txbds > tx_queue->num_txbdfree) {
Dai Haruki4669bc92008-12-17 16:51:04 -08002208 /* no space, stop the queue */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002209 netif_tx_stop_queue(txq);
Dai Haruki4669bc92008-12-17 16:51:04 -08002210 dev->stats.tx_fifo_errors++;
Dai Haruki4669bc92008-12-17 16:51:04 -08002211 return NETDEV_TX_BUSY;
2212 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213
2214 /* Update transmit stats */
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002215 bytes_sent = skb->len;
2216 tx_queue->stats.tx_bytes += bytes_sent;
2217 /* keep Tx bytes on wire for BQL accounting */
2218 GFAR_CB(skb)->bytes_sent = bytes_sent;
Eric Dumazet1ac9ad12011-01-12 12:13:14 +00002219 tx_queue->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002220
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002221 txbdp = txbdp_start = tx_queue->cur_tx;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002222 lstatus = txbdp->lstatus;
2223
2224 /* Time stamp insertion requires one additional TxBD */
2225 if (unlikely(do_tstamp))
2226 txbdp_tstamp = txbdp = next_txbd(txbdp, base,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002227 tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228
Dai Haruki4669bc92008-12-17 16:51:04 -08002229 if (nr_frags == 0) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002230 if (unlikely(do_tstamp))
2231 txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_LAST |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002232 TXBD_INTERRUPT);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002233 else
2234 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
Dai Haruki4669bc92008-12-17 16:51:04 -08002235 } else {
2236 /* Place the fragment addresses and lengths into the TxBDs */
2237 for (i = 0; i < nr_frags; i++) {
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002238 unsigned int frag_len;
Dai Haruki4669bc92008-12-17 16:51:04 -08002239 /* Point at the next BD, wrapping as needed */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002240 txbdp = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002241
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002242 frag_len = skb_shinfo(skb)->frags[i].size;
Dai Haruki4669bc92008-12-17 16:51:04 -08002243
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002244 lstatus = txbdp->lstatus | frag_len |
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002245 BD_LFLAG(TXBD_READY);
Dai Haruki4669bc92008-12-17 16:51:04 -08002246
2247 /* Handle the last BD specially */
2248 if (i == nr_frags - 1)
2249 lstatus |= BD_LFLAG(TXBD_LAST | TXBD_INTERRUPT);
2250
Claudiu Manoil369ec162013-02-14 05:00:02 +00002251 bufaddr = skb_frag_dma_map(priv->dev,
Ian Campbell2234a722011-08-29 23:18:29 +00002252 &skb_shinfo(skb)->frags[i],
2253 0,
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002254 frag_len,
Ian Campbell2234a722011-08-29 23:18:29 +00002255 DMA_TO_DEVICE);
Dai Haruki4669bc92008-12-17 16:51:04 -08002256
2257 /* set the TxBD length and buffer pointer */
2258 txbdp->bufPtr = bufaddr;
2259 txbdp->lstatus = lstatus;
2260 }
2261
2262 lstatus = txbdp_start->lstatus;
2263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002265 /* Add TxPAL between FCB and frame if required */
2266 if (unlikely(do_tstamp)) {
2267 skb_push(skb, GMAC_TXPAL_LEN);
2268 memset(skb->data, 0, GMAC_TXPAL_LEN);
2269 }
2270
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002271 /* Add TxFCB if required */
2272 if (fcb_len) {
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002273 fcb = gfar_add_fcb(skb);
Claudiu Manoil02d88fb2013-08-05 17:20:09 +03002274 lstatus |= BD_LFLAG(TXBD_TOE);
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002275 }
2276
2277 /* Set up checksumming */
2278 if (do_csum) {
2279 gfar_tx_checksum(skb, fcb, fcb_len);
Claudiu Manoil02d88fb2013-08-05 17:20:09 +03002280
2281 if (unlikely(gfar_csum_errata_12(priv, (unsigned long)fcb)) ||
2282 unlikely(gfar_csum_errata_76(priv, skb->len))) {
Alex Dubov4363c2fdd2011-03-16 17:57:13 +00002283 __skb_pull(skb, GMAC_FCB_LEN);
2284 skb_checksum_help(skb);
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002285 if (do_vlan || do_tstamp) {
2286 /* put back a new fcb for vlan/tstamp TOE */
2287 fcb = gfar_add_fcb(skb);
2288 } else {
2289 /* Tx TOE not used */
2290 lstatus &= ~(BD_LFLAG(TXBD_TOE));
2291 fcb = NULL;
2292 }
Alex Dubov4363c2fdd2011-03-16 17:57:13 +00002293 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002294 }
2295
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002296 if (do_vlan)
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002297 gfar_tx_vlan(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002298
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002299 /* Setup tx hardware time stamping if requested */
2300 if (unlikely(do_tstamp)) {
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002301 skb_shinfo(skb)->tx_flags |= SKBTX_IN_PROGRESS;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002302 fcb->ptp = 1;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002303 }
2304
Claudiu Manoil369ec162013-02-14 05:00:02 +00002305 txbdp_start->bufPtr = dma_map_single(priv->dev, skb->data,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002306 skb_headlen(skb), DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002307
Jan Ceuleers0977f812012-06-05 03:42:12 +00002308 /* If time stamping is requested one additional TxBD must be set up. The
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002309 * first TxBD points to the FCB and must have a data length of
2310 * GMAC_FCB_LEN. The second TxBD points to the actual frame data with
2311 * the full frame length.
2312 */
2313 if (unlikely(do_tstamp)) {
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002314 txbdp_tstamp->bufPtr = txbdp_start->bufPtr + fcb_len;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002315 txbdp_tstamp->lstatus |= BD_LFLAG(TXBD_READY) |
Claudiu Manoil0d0cffd2013-08-05 17:20:10 +03002316 (skb_headlen(skb) - fcb_len);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002317 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | GMAC_FCB_LEN;
2318 } else {
2319 lstatus |= BD_LFLAG(TXBD_CRC | TXBD_READY) | skb_headlen(skb);
2320 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002322 netdev_tx_sent_queue(txq, bytes_sent);
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002323
Jan Ceuleers0977f812012-06-05 03:42:12 +00002324 /* We can work in parallel with gfar_clean_tx_ring(), except
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002325 * when modifying num_txbdfree. Note that we didn't grab the lock
2326 * when we were reading the num_txbdfree and checking for available
2327 * space, that's because outside of this function it can only grow,
2328 * and once we've got needed space, it cannot suddenly disappear.
2329 *
2330 * The lock also protects us from gfar_error(), which can modify
2331 * regs->tstat and thus retrigger the transfers, which is why we
2332 * also must grab the lock before setting ready bit for the first
2333 * to be transmitted BD.
2334 */
2335 spin_lock_irqsave(&tx_queue->txlock, flags);
2336
Jan Ceuleers0977f812012-06-05 03:42:12 +00002337 /* The powerpc-specific eieio() is used, as wmb() has too strong
Scott Wood3b6330c2007-05-16 15:06:59 -05002338 * semantics (it requires synchronization between cacheable and
2339 * uncacheable mappings, which eieio doesn't provide and which we
2340 * don't need), thus requiring a more expensive sync instruction. At
2341 * some point, the set of architecture-independent barrier functions
2342 * should be expanded to include weaker barriers.
2343 */
Scott Wood3b6330c2007-05-16 15:06:59 -05002344 eieio();
Andy Fleming7f7f5312005-11-11 12:38:59 -06002345
Dai Haruki4669bc92008-12-17 16:51:04 -08002346 txbdp_start->lstatus = lstatus;
2347
Anton Vorontsov0eddba52010-03-03 08:18:58 +00002348 eieio(); /* force lstatus write before tx_skbuff */
2349
2350 tx_queue->tx_skbuff[tx_queue->skb_curtx] = skb;
2351
Dai Haruki4669bc92008-12-17 16:51:04 -08002352 /* Update the current skb pointer to the next entry we will use
Jan Ceuleers0977f812012-06-05 03:42:12 +00002353 * (wrapping if necessary)
2354 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002355 tx_queue->skb_curtx = (tx_queue->skb_curtx + 1) &
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002356 TX_RING_MOD_MASK(tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002357
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002358 tx_queue->cur_tx = next_txbd(txbdp, base, tx_queue->tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002359
2360 /* reduce TxBD free count */
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002361 tx_queue->num_txbdfree -= (nr_txbds);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362
2363 /* If the next BD still needs to be cleaned up, then the bds
Jan Ceuleers0977f812012-06-05 03:42:12 +00002364 * are full. We need to tell the kernel to stop sending us stuff.
2365 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002366 if (!tx_queue->num_txbdfree) {
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002367 netif_tx_stop_queue(txq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002368
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002369 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370 }
2371
Linus Torvalds1da177e2005-04-16 15:20:36 -07002372 /* Tell the DMA to go go go */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002373 gfar_write(&regs->tstat, TSTAT_CLEAR_THALT >> tx_queue->qindex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
2375 /* Unlock priv */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002376 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Stephen Hemminger54dc79f2009-03-27 00:38:45 -07002378 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
2380
2381/* Stops the kernel queue, and halts the controller */
2382static int gfar_close(struct net_device *dev)
2383{
2384 struct gfar_private *priv = netdev_priv(dev);
Stephen Hemmingerbea33482007-10-03 16:41:36 -07002385
Sebastian Siewiorab939902008-08-19 21:12:45 +02002386 cancel_work_sync(&priv->reset_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 stop_gfar(dev);
2388
Andy Flemingbb40dcb2005-09-23 22:54:21 -04002389 /* Disconnect from the PHY */
2390 phy_disconnect(priv->phydev);
2391 priv->phydev = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002392
Claudiu Manoil80ec3962014-02-24 12:13:44 +02002393 gfar_free_irq(priv);
2394
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395 return 0;
2396}
2397
Linus Torvalds1da177e2005-04-16 15:20:36 -07002398/* Changes the mac address if the controller is not running. */
Andy Flemingf162b9d2008-05-02 13:00:30 -05002399static int gfar_set_mac_address(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400{
Andy Fleming7f7f5312005-11-11 12:38:59 -06002401 gfar_set_mac_for_addr(dev, 0, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002402
2403 return 0;
2404}
2405
Linus Torvalds1da177e2005-04-16 15:20:36 -07002406static int gfar_change_mtu(struct net_device *dev, int new_mtu)
2407{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002409 int frame_size = new_mtu + ETH_HLEN;
2410
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 if ((frame_size < 64) || (frame_size > JUMBO_FRAME_SIZE)) {
Joe Perches59deab22011-06-14 08:57:47 +00002412 netif_err(priv, drv, dev, "Invalid MTU setting\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002413 return -EINVAL;
2414 }
2415
Claudiu Manoil08511332014-02-24 12:13:45 +02002416 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
2417 cpu_relax();
2418
Claudiu Manoil88302642014-02-24 12:13:43 +02002419 if (dev->flags & IFF_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002420 stop_gfar(dev);
2421
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422 dev->mtu = new_mtu;
2423
Claudiu Manoil88302642014-02-24 12:13:43 +02002424 if (dev->flags & IFF_UP)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002425 startup_gfar(dev);
2426
Claudiu Manoil08511332014-02-24 12:13:45 +02002427 clear_bit_unlock(GFAR_RESETTING, &priv->state);
2428
Linus Torvalds1da177e2005-04-16 15:20:36 -07002429 return 0;
2430}
2431
Claudiu Manoil08511332014-02-24 12:13:45 +02002432void reset_gfar(struct net_device *ndev)
2433{
2434 struct gfar_private *priv = netdev_priv(ndev);
2435
2436 while (test_and_set_bit_lock(GFAR_RESETTING, &priv->state))
2437 cpu_relax();
2438
2439 stop_gfar(ndev);
2440 startup_gfar(ndev);
2441
2442 clear_bit_unlock(GFAR_RESETTING, &priv->state);
2443}
2444
Sebastian Siewiorab939902008-08-19 21:12:45 +02002445/* gfar_reset_task gets scheduled when a packet has not been
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446 * transmitted after a set amount of time.
2447 * For now, assume that clearing out all the structures, and
Sebastian Siewiorab939902008-08-19 21:12:45 +02002448 * starting over will fix the problem.
2449 */
2450static void gfar_reset_task(struct work_struct *work)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002451{
Sebastian Siewiorab939902008-08-19 21:12:45 +02002452 struct gfar_private *priv = container_of(work, struct gfar_private,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002453 reset_task);
Claudiu Manoil08511332014-02-24 12:13:45 +02002454 reset_gfar(priv->ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002455}
2456
Sebastian Siewiorab939902008-08-19 21:12:45 +02002457static void gfar_timeout(struct net_device *dev)
2458{
2459 struct gfar_private *priv = netdev_priv(dev);
2460
2461 dev->stats.tx_errors++;
2462 schedule_work(&priv->reset_task);
2463}
2464
Eran Libertyacbc0f02010-07-07 15:54:54 -07002465static void gfar_align_skb(struct sk_buff *skb)
2466{
2467 /* We need the data buffer to be aligned properly. We will reserve
2468 * as many bytes as needed to align the data properly
2469 */
2470 skb_reserve(skb, RXBUF_ALIGNMENT -
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002471 (((unsigned long) skb->data) & (RXBUF_ALIGNMENT - 1)));
Eran Libertyacbc0f02010-07-07 15:54:54 -07002472}
2473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474/* Interrupt Handler for Transmit complete */
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002475static void gfar_clean_tx_ring(struct gfar_priv_tx_q *tx_queue)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002477 struct net_device *dev = tx_queue->dev;
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002478 struct netdev_queue *txq;
Dai Harukid080cd62008-04-09 19:37:51 -05002479 struct gfar_private *priv = netdev_priv(dev);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002480 struct txbd8 *bdp, *next = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002481 struct txbd8 *lbdp = NULL;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002482 struct txbd8 *base = tx_queue->tx_bd_base;
Dai Haruki4669bc92008-12-17 16:51:04 -08002483 struct sk_buff *skb;
2484 int skb_dirtytx;
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002485 int tx_ring_size = tx_queue->tx_ring_size;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002486 int frags = 0, nr_txbds = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002487 int i;
Dai Harukid080cd62008-04-09 19:37:51 -05002488 int howmany = 0;
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002489 int tqi = tx_queue->qindex;
2490 unsigned int bytes_sent = 0;
Dai Haruki4669bc92008-12-17 16:51:04 -08002491 u32 lstatus;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002492 size_t buflen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002494 txq = netdev_get_tx_queue(dev, tqi);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002495 bdp = tx_queue->dirty_tx;
2496 skb_dirtytx = tx_queue->skb_dirtytx;
Dai Haruki4669bc92008-12-17 16:51:04 -08002497
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002498 while ((skb = tx_queue->tx_skbuff[skb_dirtytx])) {
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002499 unsigned long flags;
2500
Dai Haruki4669bc92008-12-17 16:51:04 -08002501 frags = skb_shinfo(skb)->nr_frags;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002502
Jan Ceuleers0977f812012-06-05 03:42:12 +00002503 /* When time stamping, one additional TxBD must be freed.
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002504 * Also, we need to dma_unmap_single() the TxPAL.
2505 */
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002506 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS))
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002507 nr_txbds = frags + 2;
2508 else
2509 nr_txbds = frags + 1;
2510
2511 lbdp = skip_txbd(bdp, nr_txbds - 1, base, tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002512
2513 lstatus = lbdp->lstatus;
2514
2515 /* Only clean completed frames */
2516 if ((lstatus & BD_LFLAG(TXBD_READY)) &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002517 (lstatus & BD_LENGTH_MASK))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 break;
2519
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002520 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002521 next = next_txbd(bdp, base, tx_ring_size);
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002522 buflen = next->length + GMAC_FCB_LEN + GMAC_TXPAL_LEN;
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002523 } else
2524 buflen = bdp->length;
2525
Claudiu Manoil369ec162013-02-14 05:00:02 +00002526 dma_unmap_single(priv->dev, bdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002527 buflen, DMA_TO_DEVICE);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002528
Oliver Hartkopp2244d072010-08-17 08:59:14 +00002529 if (unlikely(skb_shinfo(skb)->tx_flags & SKBTX_IN_PROGRESS)) {
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002530 struct skb_shared_hwtstamps shhwtstamps;
2531 u64 *ns = (u64*) (((u32)skb->data + 0x10) & ~0x7);
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002532
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002533 memset(&shhwtstamps, 0, sizeof(shhwtstamps));
2534 shhwtstamps.hwtstamp = ns_to_ktime(*ns);
Manfred Rudigier9c4886e2012-01-09 23:26:51 +00002535 skb_pull(skb, GMAC_FCB_LEN + GMAC_TXPAL_LEN);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002536 skb_tstamp_tx(skb, &shhwtstamps);
2537 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2538 bdp = next;
2539 }
Dai Haruki4669bc92008-12-17 16:51:04 -08002540
2541 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2542 bdp = next_txbd(bdp, base, tx_ring_size);
2543
2544 for (i = 0; i < frags; i++) {
Claudiu Manoil369ec162013-02-14 05:00:02 +00002545 dma_unmap_page(priv->dev, bdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002546 bdp->length, DMA_TO_DEVICE);
Dai Haruki4669bc92008-12-17 16:51:04 -08002547 bdp->lstatus &= BD_LFLAG(TXBD_WRAP);
2548 bdp = next_txbd(bdp, base, tx_ring_size);
2549 }
2550
Claudiu Manoil50ad0762013-08-30 15:01:15 +03002551 bytes_sent += GFAR_CB(skb)->bytes_sent;
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002552
Eric Dumazetacb600d2012-10-05 06:23:55 +00002553 dev_kfree_skb_any(skb);
Andy Fleming0fd56bb2009-02-04 16:43:16 -08002554
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002555 tx_queue->tx_skbuff[skb_dirtytx] = NULL;
Dai Haruki4669bc92008-12-17 16:51:04 -08002556
2557 skb_dirtytx = (skb_dirtytx + 1) &
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002558 TX_RING_MOD_MASK(tx_ring_size);
Dai Haruki4669bc92008-12-17 16:51:04 -08002559
Dai Harukid080cd62008-04-09 19:37:51 -05002560 howmany++;
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002561 spin_lock_irqsave(&tx_queue->txlock, flags);
Manfred Rudigierf0ee7ac2010-04-08 23:10:35 +00002562 tx_queue->num_txbdfree += nr_txbds;
Anton Vorontsova3bc1f12009-11-10 14:11:10 +00002563 spin_unlock_irqrestore(&tx_queue->txlock, flags);
Dai Haruki4669bc92008-12-17 16:51:04 -08002564 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565
Dai Haruki4669bc92008-12-17 16:51:04 -08002566 /* If we freed a buffer, we can restart transmission, if necessary */
Claudiu Manoil08511332014-02-24 12:13:45 +02002567 if (tx_queue->num_txbdfree &&
2568 netif_tx_queue_stopped(txq) &&
2569 !(test_bit(GFAR_DOWN, &priv->state)))
2570 netif_wake_subqueue(priv->ndev, tqi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571
Dai Haruki4669bc92008-12-17 16:51:04 -08002572 /* Update dirty indicators */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002573 tx_queue->skb_dirtytx = skb_dirtytx;
2574 tx_queue->dirty_tx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575
Paul Gortmakerd8a0f1b2012-01-06 13:51:03 -05002576 netdev_tx_completed_queue(txq, howmany, bytes_sent);
Dai Harukid080cd62008-04-09 19:37:51 -05002577}
2578
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002579static void gfar_new_rxbdp(struct gfar_priv_rx_q *rx_queue, struct rxbd8 *bdp,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002580 struct sk_buff *skb)
Andy Fleming815b97c2008-04-22 17:18:29 -05002581{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002582 struct net_device *dev = rx_queue->dev;
Andy Fleming815b97c2008-04-22 17:18:29 -05002583 struct gfar_private *priv = netdev_priv(dev);
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00002584 dma_addr_t buf;
Andy Fleming815b97c2008-04-22 17:18:29 -05002585
Claudiu Manoil369ec162013-02-14 05:00:02 +00002586 buf = dma_map_single(priv->dev, skb->data,
Anton Vorontsov8a102fe2009-10-12 06:00:37 +00002587 priv->rx_buffer_size, DMA_FROM_DEVICE);
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002588 gfar_init_rxbdp(rx_queue, bdp, buf);
Andy Fleming815b97c2008-04-22 17:18:29 -05002589}
2590
Jan Ceuleers2281a0f2012-06-05 03:42:11 +00002591static struct sk_buff *gfar_alloc_skb(struct net_device *dev)
Eran Libertyacbc0f02010-07-07 15:54:54 -07002592{
2593 struct gfar_private *priv = netdev_priv(dev);
Eric Dumazetacb600d2012-10-05 06:23:55 +00002594 struct sk_buff *skb;
Eran Libertyacbc0f02010-07-07 15:54:54 -07002595
2596 skb = netdev_alloc_skb(dev, priv->rx_buffer_size + RXBUF_ALIGNMENT);
2597 if (!skb)
2598 return NULL;
2599
2600 gfar_align_skb(skb);
2601
2602 return skb;
2603}
Andy Fleming815b97c2008-04-22 17:18:29 -05002604
Jan Ceuleers2281a0f2012-06-05 03:42:11 +00002605struct sk_buff *gfar_new_skb(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002606{
Eric Dumazetacb600d2012-10-05 06:23:55 +00002607 return gfar_alloc_skb(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002608}
2609
Li Yang298e1a92007-10-16 14:18:13 +08002610static inline void count_errors(unsigned short status, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611{
Li Yang298e1a92007-10-16 14:18:13 +08002612 struct gfar_private *priv = netdev_priv(dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -07002613 struct net_device_stats *stats = &dev->stats;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 struct gfar_extra_stats *estats = &priv->extra_stats;
2615
Jan Ceuleers0977f812012-06-05 03:42:12 +00002616 /* If the packet was truncated, none of the other errors matter */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 if (status & RXBD_TRUNCATED) {
2618 stats->rx_length_errors++;
2619
Paul Gortmaker212079d2013-02-12 15:38:19 -05002620 atomic64_inc(&estats->rx_trunc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621
2622 return;
2623 }
2624 /* Count the errors, if there were any */
2625 if (status & (RXBD_LARGE | RXBD_SHORT)) {
2626 stats->rx_length_errors++;
2627
2628 if (status & RXBD_LARGE)
Paul Gortmaker212079d2013-02-12 15:38:19 -05002629 atomic64_inc(&estats->rx_large);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 else
Paul Gortmaker212079d2013-02-12 15:38:19 -05002631 atomic64_inc(&estats->rx_short);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 }
2633 if (status & RXBD_NONOCTET) {
2634 stats->rx_frame_errors++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05002635 atomic64_inc(&estats->rx_nonoctet);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636 }
2637 if (status & RXBD_CRCERR) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05002638 atomic64_inc(&estats->rx_crcerr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002639 stats->rx_crc_errors++;
2640 }
2641 if (status & RXBD_OVERRUN) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05002642 atomic64_inc(&estats->rx_overrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002643 stats->rx_crc_errors++;
2644 }
2645}
2646
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00002647irqreturn_t gfar_receive(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002648{
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002649 struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2650 unsigned long flags;
2651 u32 imask;
2652
2653 if (likely(napi_schedule_prep(&grp->napi_rx))) {
2654 spin_lock_irqsave(&grp->grplock, flags);
2655 imask = gfar_read(&grp->regs->imask);
2656 imask &= IMASK_RX_DISABLED;
2657 gfar_write(&grp->regs->imask, imask);
2658 spin_unlock_irqrestore(&grp->grplock, flags);
2659 __napi_schedule(&grp->napi_rx);
2660 } else {
2661 /* Clear IEVENT, so interrupts aren't called again
2662 * because of the packets that have already arrived.
2663 */
2664 gfar_write(&grp->regs->ievent, IEVENT_RX_MASK);
2665 }
2666
2667 return IRQ_HANDLED;
2668}
2669
2670/* Interrupt Handler for Transmit complete */
2671static irqreturn_t gfar_transmit(int irq, void *grp_id)
2672{
2673 struct gfar_priv_grp *grp = (struct gfar_priv_grp *)grp_id;
2674 unsigned long flags;
2675 u32 imask;
2676
2677 if (likely(napi_schedule_prep(&grp->napi_tx))) {
2678 spin_lock_irqsave(&grp->grplock, flags);
2679 imask = gfar_read(&grp->regs->imask);
2680 imask &= IMASK_TX_DISABLED;
2681 gfar_write(&grp->regs->imask, imask);
2682 spin_unlock_irqrestore(&grp->grplock, flags);
2683 __napi_schedule(&grp->napi_tx);
2684 } else {
2685 /* Clear IEVENT, so interrupts aren't called again
2686 * because of the packets that have already arrived.
2687 */
2688 gfar_write(&grp->regs->ievent, IEVENT_TX_MASK);
2689 }
2690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691 return IRQ_HANDLED;
2692}
2693
Kumar Gala0bbaf062005-06-20 10:54:21 -05002694static inline void gfar_rx_checksum(struct sk_buff *skb, struct rxfcb *fcb)
2695{
2696 /* If valid headers were found, and valid sums
2697 * were verified, then we tell the kernel that no
Jan Ceuleers0977f812012-06-05 03:42:12 +00002698 * checksumming is necessary. Otherwise, it is [FIXME]
2699 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06002700 if ((fcb->flags & RXFCB_CSUM_MASK) == (RXFCB_CIP | RXFCB_CTU))
Kumar Gala0bbaf062005-06-20 10:54:21 -05002701 skb->ip_summed = CHECKSUM_UNNECESSARY;
2702 else
Eric Dumazetbc8acf22010-09-02 13:07:41 -07002703 skb_checksum_none_assert(skb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002704}
2705
2706
Jan Ceuleers0977f812012-06-05 03:42:12 +00002707/* gfar_process_frame() -- handle one incoming packet if skb isn't NULL. */
Claudiu Manoil61db26c2013-02-14 05:00:05 +00002708static void gfar_process_frame(struct net_device *dev, struct sk_buff *skb,
2709 int amount_pull, struct napi_struct *napi)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002710{
2711 struct gfar_private *priv = netdev_priv(dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002712 struct rxfcb *fcb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713
Dai Haruki2c2db482008-12-16 15:31:15 -08002714 /* fcb is at the beginning if exists */
2715 fcb = (struct rxfcb *)skb->data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716
Jan Ceuleers0977f812012-06-05 03:42:12 +00002717 /* Remove the FCB from the skb
2718 * Remove the padded bytes, if there are any
2719 */
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002720 if (amount_pull) {
2721 skb_record_rx_queue(skb, fcb->rq);
Dai Haruki2c2db482008-12-16 15:31:15 -08002722 skb_pull(skb, amount_pull);
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002723 }
Kumar Gala0bbaf062005-06-20 10:54:21 -05002724
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00002725 /* Get receive timestamp from the skb */
2726 if (priv->hwts_rx_en) {
2727 struct skb_shared_hwtstamps *shhwtstamps = skb_hwtstamps(skb);
2728 u64 *ns = (u64 *) skb->data;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002729
Manfred Rudigiercc772ab2010-04-08 23:10:03 +00002730 memset(shhwtstamps, 0, sizeof(*shhwtstamps));
2731 shhwtstamps->hwtstamp = ns_to_ktime(*ns);
2732 }
2733
2734 if (priv->padding)
2735 skb_pull(skb, priv->padding);
2736
Michał Mirosław8b3afe92011-04-15 04:50:50 +00002737 if (dev->features & NETIF_F_RXCSUM)
Dai Haruki2c2db482008-12-16 15:31:15 -08002738 gfar_rx_checksum(skb, fcb);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002739
Dai Haruki2c2db482008-12-16 15:31:15 -08002740 /* Tell the skb what kind of packet this is */
2741 skb->protocol = eth_type_trans(skb, dev);
Kumar Gala0bbaf062005-06-20 10:54:21 -05002742
Patrick McHardyf6469682013-04-19 02:04:27 +00002743 /* There's need to check for NETIF_F_HW_VLAN_CTAG_RX here.
David S. Miller823dcd22011-08-20 10:39:12 -07002744 * Even if vlan rx accel is disabled, on some chips
2745 * RXFCB_VLN is pseudo randomly set.
2746 */
Patrick McHardyf6469682013-04-19 02:04:27 +00002747 if (dev->features & NETIF_F_HW_VLAN_CTAG_RX &&
David S. Miller823dcd22011-08-20 10:39:12 -07002748 fcb->flags & RXFCB_VLN)
David S. Millere5905c82013-04-22 19:24:19 -04002749 __vlan_hwaccel_put_tag(skb, htons(ETH_P_8021Q), fcb->vlctl);
Jiri Pirko87c288c2011-07-20 04:54:19 +00002750
Dai Haruki2c2db482008-12-16 15:31:15 -08002751 /* Send the packet up the stack */
Claudiu Manoil953d2762013-03-21 03:12:15 +00002752 napi_gro_receive(napi, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753
Linus Torvalds1da177e2005-04-16 15:20:36 -07002754}
2755
2756/* gfar_clean_rx_ring() -- Processes each frame in the rx ring
Jan Ceuleers2281a0f2012-06-05 03:42:11 +00002757 * until the budget/quota has been reached. Returns the number
2758 * of frames handled
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002760int gfar_clean_rx_ring(struct gfar_priv_rx_q *rx_queue, int rx_work_limit)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761{
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002762 struct net_device *dev = rx_queue->dev;
Andy Fleming31de1982008-12-16 15:33:40 -08002763 struct rxbd8 *bdp, *base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 struct sk_buff *skb;
Dai Haruki2c2db482008-12-16 15:31:15 -08002765 int pkt_len;
2766 int amount_pull;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 int howmany = 0;
2768 struct gfar_private *priv = netdev_priv(dev);
2769
2770 /* Get the first full descriptor */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002771 bdp = rx_queue->cur_rx;
2772 base = rx_queue->rx_bd_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773
Claudiu Manoilba779712013-02-14 05:00:07 +00002774 amount_pull = priv->uses_rxfcb ? GMAC_FCB_LEN : 0;
Dai Haruki2c2db482008-12-16 15:31:15 -08002775
Linus Torvalds1da177e2005-04-16 15:20:36 -07002776 while (!((bdp->status & RXBD_EMPTY) || (--rx_work_limit < 0))) {
Andy Fleming815b97c2008-04-22 17:18:29 -05002777 struct sk_buff *newskb;
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002778
Scott Wood3b6330c2007-05-16 15:06:59 -05002779 rmb();
Andy Fleming815b97c2008-04-22 17:18:29 -05002780
2781 /* Add another skb for the future */
2782 newskb = gfar_new_skb(dev);
2783
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002784 skb = rx_queue->rx_skbuff[rx_queue->skb_currx];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002785
Claudiu Manoil369ec162013-02-14 05:00:02 +00002786 dma_unmap_single(priv->dev, bdp->bufPtr,
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002787 priv->rx_buffer_size, DMA_FROM_DEVICE);
Andy Fleming81183052008-11-12 10:07:11 -06002788
Anton Vorontsov63b88b92010-06-11 10:51:03 +00002789 if (unlikely(!(bdp->status & RXBD_ERR) &&
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002790 bdp->length > priv->rx_buffer_size))
Anton Vorontsov63b88b92010-06-11 10:51:03 +00002791 bdp->status = RXBD_LARGE;
2792
Andy Fleming815b97c2008-04-22 17:18:29 -05002793 /* We drop the frame if we failed to allocate a new buffer */
2794 if (unlikely(!newskb || !(bdp->status & RXBD_LAST) ||
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002795 bdp->status & RXBD_ERR)) {
Andy Fleming815b97c2008-04-22 17:18:29 -05002796 count_errors(bdp->status, dev);
2797
2798 if (unlikely(!newskb))
2799 newskb = skb;
Eran Libertyacbc0f02010-07-07 15:54:54 -07002800 else if (skb)
Eric Dumazetacb600d2012-10-05 06:23:55 +00002801 dev_kfree_skb(skb);
Andy Fleming815b97c2008-04-22 17:18:29 -05002802 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 /* Increment the number of packets */
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002804 rx_queue->stats.rx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 howmany++;
2806
Dai Haruki2c2db482008-12-16 15:31:15 -08002807 if (likely(skb)) {
2808 pkt_len = bdp->length - ETH_FCS_LEN;
2809 /* Remove the FCS from the packet length */
2810 skb_put(skb, pkt_len);
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002811 rx_queue->stats.rx_bytes += pkt_len;
Sandeep Gopalpetf74dac02009-12-24 03:13:06 +00002812 skb_record_rx_queue(skb, rx_queue->qindex);
Wu Jiajun-B06378cd754a52012-04-19 22:54:35 +00002813 gfar_process_frame(dev, skb, amount_pull,
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002814 &rx_queue->grp->napi_rx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Dai Haruki2c2db482008-12-16 15:31:15 -08002816 } else {
Joe Perches59deab22011-06-14 08:57:47 +00002817 netif_warn(priv, rx_err, dev, "Missing skb!\n");
Sandeep Gopalpeta7f38042009-12-16 01:15:07 +00002818 rx_queue->stats.rx_dropped++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05002819 atomic64_inc(&priv->extra_stats.rx_skbmissing);
Dai Haruki2c2db482008-12-16 15:31:15 -08002820 }
2821
Linus Torvalds1da177e2005-04-16 15:20:36 -07002822 }
2823
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002824 rx_queue->rx_skbuff[rx_queue->skb_currx] = newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Andy Fleming815b97c2008-04-22 17:18:29 -05002826 /* Setup the new bdp */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002827 gfar_new_rxbdp(rx_queue, bdp, newskb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 /* Update to the next pointer */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002830 bdp = next_bd(bdp, base, rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831
2832 /* update to point at the next skb */
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002833 rx_queue->skb_currx = (rx_queue->skb_currx + 1) &
2834 RX_RING_MOD_MASK(rx_queue->rx_ring_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 }
2836
2837 /* Update the current rxbd pointer to be the next one */
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00002838 rx_queue->cur_rx = bdp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002839
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840 return howmany;
2841}
2842
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002843static int gfar_poll_rx_sq(struct napi_struct *napi, int budget)
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002844{
2845 struct gfar_priv_grp *gfargrp =
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002846 container_of(napi, struct gfar_priv_grp, napi_rx);
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002847 struct gfar __iomem *regs = gfargrp->regs;
Claudiu Manoil71ff9e32014-03-07 14:42:46 +02002848 struct gfar_priv_rx_q *rx_queue = gfargrp->rx_queue;
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002849 int work_done = 0;
2850
2851 /* Clear IEVENT, so interrupts aren't called again
2852 * because of the packets that have already arrived
2853 */
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002854 gfar_write(&regs->ievent, IEVENT_RX_MASK);
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002855
2856 work_done = gfar_clean_rx_ring(rx_queue, budget);
2857
2858 if (work_done < budget) {
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002859 u32 imask;
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002860 napi_complete(napi);
2861 /* Clear the halt bit in RSTAT */
2862 gfar_write(&regs->rstat, gfargrp->rstat);
2863
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002864 spin_lock_irq(&gfargrp->grplock);
2865 imask = gfar_read(&regs->imask);
2866 imask |= IMASK_RX_DEFAULT;
2867 gfar_write(&regs->imask, imask);
2868 spin_unlock_irq(&gfargrp->grplock);
Claudiu Manoil5eaedf32013-06-10 20:19:48 +03002869 }
2870
2871 return work_done;
2872}
2873
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002874static int gfar_poll_tx_sq(struct napi_struct *napi, int budget)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002875{
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00002876 struct gfar_priv_grp *gfargrp =
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002877 container_of(napi, struct gfar_priv_grp, napi_tx);
2878 struct gfar __iomem *regs = gfargrp->regs;
Claudiu Manoil71ff9e32014-03-07 14:42:46 +02002879 struct gfar_priv_tx_q *tx_queue = gfargrp->tx_queue;
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002880 u32 imask;
2881
2882 /* Clear IEVENT, so interrupts aren't called again
2883 * because of the packets that have already arrived
2884 */
2885 gfar_write(&regs->ievent, IEVENT_TX_MASK);
2886
2887 /* run Tx cleanup to completion */
2888 if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx])
2889 gfar_clean_tx_ring(tx_queue);
2890
2891 napi_complete(napi);
2892
2893 spin_lock_irq(&gfargrp->grplock);
2894 imask = gfar_read(&regs->imask);
2895 imask |= IMASK_TX_DEFAULT;
2896 gfar_write(&regs->imask, imask);
2897 spin_unlock_irq(&gfargrp->grplock);
2898
2899 return 0;
2900}
2901
2902static int gfar_poll_rx(struct napi_struct *napi, int budget)
2903{
2904 struct gfar_priv_grp *gfargrp =
2905 container_of(napi, struct gfar_priv_grp, napi_rx);
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002906 struct gfar_private *priv = gfargrp->priv;
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00002907 struct gfar __iomem *regs = gfargrp->regs;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002908 struct gfar_priv_rx_q *rx_queue = NULL;
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002909 int work_done = 0, work_done_per_q = 0;
Claudiu Manoil39c0a0d2013-03-21 03:12:13 +00002910 int i, budget_per_q = 0;
Claudiu Manoil6be5ed32013-03-19 07:40:03 +00002911 unsigned long rstat_rxf;
2912 int num_act_queues;
Dai Harukid080cd62008-04-09 19:37:51 -05002913
Dai Haruki8c7396a2008-12-17 16:52:00 -08002914 /* Clear IEVENT, so interrupts aren't called again
Jan Ceuleers0977f812012-06-05 03:42:12 +00002915 * because of the packets that have already arrived
2916 */
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002917 gfar_write(&regs->ievent, IEVENT_RX_MASK);
Dai Haruki8c7396a2008-12-17 16:52:00 -08002918
Claudiu Manoil6be5ed32013-03-19 07:40:03 +00002919 rstat_rxf = gfar_read(&regs->rstat) & RSTAT_RXF_MASK;
2920
2921 num_act_queues = bitmap_weight(&rstat_rxf, MAX_RX_QS);
2922 if (num_act_queues)
2923 budget_per_q = budget/num_act_queues;
2924
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002925 for_each_set_bit(i, &gfargrp->rx_bit_map, priv->num_rx_queues) {
2926 /* skip queue if not active */
2927 if (!(rstat_rxf & (RSTAT_CLEAR_RXF0 >> i)))
2928 continue;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002929
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002930 rx_queue = priv->rx_queue[i];
2931 work_done_per_q =
2932 gfar_clean_rx_ring(rx_queue, budget_per_q);
2933 work_done += work_done_per_q;
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002934
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002935 /* finished processing this queue */
2936 if (work_done_per_q < budget_per_q) {
2937 /* clear active queue hw indication */
2938 gfar_write(&regs->rstat,
2939 RSTAT_CLEAR_RXF0 >> i);
2940 num_act_queues--;
Claudiu Manoil6be5ed32013-03-19 07:40:03 +00002941
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002942 if (!num_act_queues)
2943 break;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00002944 }
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002945 }
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002946
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002947 if (!num_act_queues) {
2948 u32 imask;
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002949 napi_complete(napi);
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002950
Claudiu Manoil3ba405d2013-10-14 17:05:09 +03002951 /* Clear the halt bit in RSTAT */
2952 gfar_write(&regs->rstat, gfargrp->rstat);
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002953
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002954 spin_lock_irq(&gfargrp->grplock);
2955 imask = gfar_read(&regs->imask);
2956 imask |= IMASK_RX_DEFAULT;
2957 gfar_write(&regs->imask, imask);
2958 spin_unlock_irq(&gfargrp->grplock);
Dai Harukid080cd62008-04-09 19:37:51 -05002959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002960
Claudiu Manoilc233cf402013-03-19 07:40:02 +00002961 return work_done;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962}
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963
Claudiu Manoilaeb12c52014-03-07 14:42:45 +02002964static int gfar_poll_tx(struct napi_struct *napi, int budget)
2965{
2966 struct gfar_priv_grp *gfargrp =
2967 container_of(napi, struct gfar_priv_grp, napi_tx);
2968 struct gfar_private *priv = gfargrp->priv;
2969 struct gfar __iomem *regs = gfargrp->regs;
2970 struct gfar_priv_tx_q *tx_queue = NULL;
2971 int has_tx_work = 0;
2972 int i;
2973
2974 /* Clear IEVENT, so interrupts aren't called again
2975 * because of the packets that have already arrived
2976 */
2977 gfar_write(&regs->ievent, IEVENT_TX_MASK);
2978
2979 for_each_set_bit(i, &gfargrp->tx_bit_map, priv->num_tx_queues) {
2980 tx_queue = priv->tx_queue[i];
2981 /* run Tx cleanup to completion */
2982 if (tx_queue->tx_skbuff[tx_queue->skb_dirtytx]) {
2983 gfar_clean_tx_ring(tx_queue);
2984 has_tx_work = 1;
2985 }
2986 }
2987
2988 if (!has_tx_work) {
2989 u32 imask;
2990 napi_complete(napi);
2991
2992 spin_lock_irq(&gfargrp->grplock);
2993 imask = gfar_read(&regs->imask);
2994 imask |= IMASK_TX_DEFAULT;
2995 gfar_write(&regs->imask, imask);
2996 spin_unlock_irq(&gfargrp->grplock);
2997 }
2998
2999 return 0;
3000}
3001
3002
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003003#ifdef CONFIG_NET_POLL_CONTROLLER
Jan Ceuleers0977f812012-06-05 03:42:12 +00003004/* Polling 'interrupt' - used by things like netconsole to send skbs
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003005 * without having to re-enable interrupts. It's not called while
3006 * the interrupt routine is executing.
3007 */
3008static void gfar_netpoll(struct net_device *dev)
3009{
3010 struct gfar_private *priv = netdev_priv(dev);
Jan Ceuleers3a2e16c2012-06-05 03:42:14 +00003011 int i;
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003012
3013 /* If the device has multiple interrupts, run tx/rx */
Andy Flemingb31a1d82008-12-16 15:29:15 -08003014 if (priv->device_flags & FSL_GIANFAR_DEV_HAS_MULTI_INTR) {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003015 for (i = 0; i < priv->num_grps; i++) {
Paul Gortmaker62ed8392013-02-24 05:38:31 +00003016 struct gfar_priv_grp *grp = &priv->gfargrp[i];
3017
3018 disable_irq(gfar_irq(grp, TX)->irq);
3019 disable_irq(gfar_irq(grp, RX)->irq);
3020 disable_irq(gfar_irq(grp, ER)->irq);
3021 gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
3022 enable_irq(gfar_irq(grp, ER)->irq);
3023 enable_irq(gfar_irq(grp, RX)->irq);
3024 enable_irq(gfar_irq(grp, TX)->irq);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003025 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003026 } else {
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003027 for (i = 0; i < priv->num_grps; i++) {
Paul Gortmaker62ed8392013-02-24 05:38:31 +00003028 struct gfar_priv_grp *grp = &priv->gfargrp[i];
3029
3030 disable_irq(gfar_irq(grp, TX)->irq);
3031 gfar_interrupt(gfar_irq(grp, TX)->irq, grp);
3032 enable_irq(gfar_irq(grp, TX)->irq);
Anton Vorontsov43de0042009-12-09 02:52:19 -08003033 }
Vitaly Woolf2d71c22006-11-07 13:27:02 +03003034 }
3035}
3036#endif
3037
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038/* The interrupt handler for devices with one interrupt */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003039static irqreturn_t gfar_interrupt(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003041 struct gfar_priv_grp *gfargrp = grp_id;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003042
3043 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003044 u32 events = gfar_read(&gfargrp->regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003045
Linus Torvalds1da177e2005-04-16 15:20:36 -07003046 /* Check for reception */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003047 if (events & IEVENT_RX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003048 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003049
3050 /* Check for transmit completion */
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003051 if (events & IEVENT_TX_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003052 gfar_transmit(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003053
Sergei Shtylyov538cc7e2007-02-15 17:56:01 +04003054 /* Check for errors */
3055 if (events & IEVENT_ERR_MASK)
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003056 gfar_error(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003057
3058 return IRQ_HANDLED;
3059}
3060
Claudiu Manoil23402bd2013-08-12 13:53:26 +03003061static u32 gfar_get_flowctrl_cfg(struct gfar_private *priv)
3062{
3063 struct phy_device *phydev = priv->phydev;
3064 u32 val = 0;
3065
3066 if (!phydev->duplex)
3067 return val;
3068
3069 if (!priv->pause_aneg_en) {
3070 if (priv->tx_pause_en)
3071 val |= MACCFG1_TX_FLOW;
3072 if (priv->rx_pause_en)
3073 val |= MACCFG1_RX_FLOW;
3074 } else {
3075 u16 lcl_adv, rmt_adv;
3076 u8 flowctrl;
3077 /* get link partner capabilities */
3078 rmt_adv = 0;
3079 if (phydev->pause)
3080 rmt_adv = LPA_PAUSE_CAP;
3081 if (phydev->asym_pause)
3082 rmt_adv |= LPA_PAUSE_ASYM;
3083
3084 lcl_adv = mii_advertise_flowctrl(phydev->advertising);
3085
3086 flowctrl = mii_resolve_flowctrl_fdx(lcl_adv, rmt_adv);
3087 if (flowctrl & FLOW_CTRL_TX)
3088 val |= MACCFG1_TX_FLOW;
3089 if (flowctrl & FLOW_CTRL_RX)
3090 val |= MACCFG1_RX_FLOW;
3091 }
3092
3093 return val;
3094}
3095
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096/* Called every time the controller might need to be made
3097 * aware of new link state. The PHY code conveys this
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003098 * information through variables in the phydev structure, and this
Linus Torvalds1da177e2005-04-16 15:20:36 -07003099 * function converts those variables into the appropriate
3100 * register values, and can bring down the device if needed.
3101 */
3102static void adjust_link(struct net_device *dev)
3103{
3104 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003105 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003106 struct phy_device *phydev = priv->phydev;
3107 int new_state = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
Claudiu Manoil08511332014-02-24 12:13:45 +02003109 if (test_bit(GFAR_RESETTING, &priv->state))
3110 return;
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00003111
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003112 if (phydev->link) {
Claudiu Manoil23402bd2013-08-12 13:53:26 +03003113 u32 tempval1 = gfar_read(&regs->maccfg1);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003114 u32 tempval = gfar_read(&regs->maccfg2);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003115 u32 ecntrl = gfar_read(&regs->ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003116
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 /* Now we make sure that we can be in full duplex mode.
Jan Ceuleers0977f812012-06-05 03:42:12 +00003118 * If not, we operate in half-duplex mode.
3119 */
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003120 if (phydev->duplex != priv->oldduplex) {
3121 new_state = 1;
3122 if (!(phydev->duplex))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123 tempval &= ~(MACCFG2_FULL_DUPLEX);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003124 else
Linus Torvalds1da177e2005-04-16 15:20:36 -07003125 tempval |= MACCFG2_FULL_DUPLEX;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003127 priv->oldduplex = phydev->duplex;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128 }
3129
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003130 if (phydev->speed != priv->oldspeed) {
3131 new_state = 1;
3132 switch (phydev->speed) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003133 case 1000:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 tempval =
3135 ((tempval & ~(MACCFG2_IF)) | MACCFG2_GMII);
Li Yangf430e492009-01-06 14:08:10 -08003136
3137 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138 break;
3139 case 100:
3140 case 10:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003141 tempval =
3142 ((tempval & ~(MACCFG2_IF)) | MACCFG2_MII);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003143
3144 /* Reduced mode distinguishes
Jan Ceuleers0977f812012-06-05 03:42:12 +00003145 * between 10 and 100
3146 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06003147 if (phydev->speed == SPEED_100)
3148 ecntrl |= ECNTRL_R100;
3149 else
3150 ecntrl &= ~(ECNTRL_R100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003151 break;
3152 default:
Joe Perches59deab22011-06-14 08:57:47 +00003153 netif_warn(priv, link, dev,
3154 "Ack! Speed (%d) is not 10/100/1000!\n",
3155 phydev->speed);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 break;
3157 }
3158
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003159 priv->oldspeed = phydev->speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003160 }
3161
Claudiu Manoil23402bd2013-08-12 13:53:26 +03003162 tempval1 &= ~(MACCFG1_TX_FLOW | MACCFG1_RX_FLOW);
3163 tempval1 |= gfar_get_flowctrl_cfg(priv);
3164
3165 gfar_write(&regs->maccfg1, tempval1);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003166 gfar_write(&regs->maccfg2, tempval);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003167 gfar_write(&regs->ecntrl, ecntrl);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003168
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 if (!priv->oldlink) {
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003170 new_state = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 priv->oldlink = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 }
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003173 } else if (priv->oldlink) {
3174 new_state = 1;
3175 priv->oldlink = 0;
3176 priv->oldspeed = 0;
3177 priv->oldduplex = -1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003180 if (new_state && netif_msg_link(priv))
3181 phy_print_status(phydev);
Andy Flemingbb40dcb2005-09-23 22:54:21 -04003182}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003183
3184/* Update the hash table based on the current list of multicast
3185 * addresses we subscribe to. Also, change the promiscuity of
3186 * the device based on the flags (this function is called
Jan Ceuleers0977f812012-06-05 03:42:12 +00003187 * whenever dev->flags is changed
3188 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003189static void gfar_set_multi(struct net_device *dev)
3190{
Jiri Pirko22bedad32010-04-01 21:22:57 +00003191 struct netdev_hw_addr *ha;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003193 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003194 u32 tempval;
3195
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00003196 if (dev->flags & IFF_PROMISC) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 /* Set RCTRL to PROM */
3198 tempval = gfar_read(&regs->rctrl);
3199 tempval |= RCTRL_PROM;
3200 gfar_write(&regs->rctrl, tempval);
3201 } else {
3202 /* Set RCTRL to not PROM */
3203 tempval = gfar_read(&regs->rctrl);
3204 tempval &= ~(RCTRL_PROM);
3205 gfar_write(&regs->rctrl, tempval);
3206 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -04003207
Sandeep Gopalpeta12f8012009-11-02 07:03:00 +00003208 if (dev->flags & IFF_ALLMULTI) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003209 /* Set the hash to rx all multicast frames */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003210 gfar_write(&regs->igaddr0, 0xffffffff);
3211 gfar_write(&regs->igaddr1, 0xffffffff);
3212 gfar_write(&regs->igaddr2, 0xffffffff);
3213 gfar_write(&regs->igaddr3, 0xffffffff);
3214 gfar_write(&regs->igaddr4, 0xffffffff);
3215 gfar_write(&regs->igaddr5, 0xffffffff);
3216 gfar_write(&regs->igaddr6, 0xffffffff);
3217 gfar_write(&regs->igaddr7, 0xffffffff);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 gfar_write(&regs->gaddr0, 0xffffffff);
3219 gfar_write(&regs->gaddr1, 0xffffffff);
3220 gfar_write(&regs->gaddr2, 0xffffffff);
3221 gfar_write(&regs->gaddr3, 0xffffffff);
3222 gfar_write(&regs->gaddr4, 0xffffffff);
3223 gfar_write(&regs->gaddr5, 0xffffffff);
3224 gfar_write(&regs->gaddr6, 0xffffffff);
3225 gfar_write(&regs->gaddr7, 0xffffffff);
3226 } else {
Andy Fleming7f7f5312005-11-11 12:38:59 -06003227 int em_num;
3228 int idx;
3229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230 /* zero out the hash */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003231 gfar_write(&regs->igaddr0, 0x0);
3232 gfar_write(&regs->igaddr1, 0x0);
3233 gfar_write(&regs->igaddr2, 0x0);
3234 gfar_write(&regs->igaddr3, 0x0);
3235 gfar_write(&regs->igaddr4, 0x0);
3236 gfar_write(&regs->igaddr5, 0x0);
3237 gfar_write(&regs->igaddr6, 0x0);
3238 gfar_write(&regs->igaddr7, 0x0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003239 gfar_write(&regs->gaddr0, 0x0);
3240 gfar_write(&regs->gaddr1, 0x0);
3241 gfar_write(&regs->gaddr2, 0x0);
3242 gfar_write(&regs->gaddr3, 0x0);
3243 gfar_write(&regs->gaddr4, 0x0);
3244 gfar_write(&regs->gaddr5, 0x0);
3245 gfar_write(&regs->gaddr6, 0x0);
3246 gfar_write(&regs->gaddr7, 0x0);
3247
Andy Fleming7f7f5312005-11-11 12:38:59 -06003248 /* If we have extended hash tables, we need to
3249 * clear the exact match registers to prepare for
Jan Ceuleers0977f812012-06-05 03:42:12 +00003250 * setting them
3251 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06003252 if (priv->extended_hash) {
3253 em_num = GFAR_EM_NUM + 1;
3254 gfar_clear_exact_match(dev);
3255 idx = 1;
3256 } else {
3257 idx = 0;
3258 em_num = 0;
3259 }
3260
Jiri Pirko4cd24ea2010-02-08 04:30:35 +00003261 if (netdev_mc_empty(dev))
Linus Torvalds1da177e2005-04-16 15:20:36 -07003262 return;
3263
3264 /* Parse the list, and set the appropriate bits */
Jiri Pirko22bedad32010-04-01 21:22:57 +00003265 netdev_for_each_mc_addr(ha, dev) {
Andy Fleming7f7f5312005-11-11 12:38:59 -06003266 if (idx < em_num) {
Jiri Pirko22bedad32010-04-01 21:22:57 +00003267 gfar_set_mac_for_addr(dev, idx, ha->addr);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003268 idx++;
3269 } else
Jiri Pirko22bedad32010-04-01 21:22:57 +00003270 gfar_set_hash_for_addr(dev, ha->addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003271 }
3272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273}
3274
Andy Fleming7f7f5312005-11-11 12:38:59 -06003275
3276/* Clears each of the exact match registers to zero, so they
Jan Ceuleers0977f812012-06-05 03:42:12 +00003277 * don't interfere with normal reception
3278 */
Andy Fleming7f7f5312005-11-11 12:38:59 -06003279static void gfar_clear_exact_match(struct net_device *dev)
3280{
3281 int idx;
Joe Perches6a3c910c2011-11-16 09:38:02 +00003282 static const u8 zero_arr[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
Andy Fleming7f7f5312005-11-11 12:38:59 -06003283
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00003284 for (idx = 1; idx < GFAR_EM_NUM + 1; idx++)
Joe Perchesb6bc7652010-12-21 02:16:08 -08003285 gfar_set_mac_for_addr(dev, idx, zero_arr);
Andy Fleming7f7f5312005-11-11 12:38:59 -06003286}
3287
Linus Torvalds1da177e2005-04-16 15:20:36 -07003288/* Set the appropriate hash bit for the given addr */
3289/* The algorithm works like so:
3290 * 1) Take the Destination Address (ie the multicast address), and
3291 * do a CRC on it (little endian), and reverse the bits of the
3292 * result.
3293 * 2) Use the 8 most significant bits as a hash into a 256-entry
3294 * table. The table is controlled through 8 32-bit registers:
3295 * gaddr0-7. gaddr0's MSB is entry 0, and gaddr7's LSB is
3296 * gaddr7. This means that the 3 most significant bits in the
3297 * hash index which gaddr register to use, and the 5 other bits
3298 * indicate which bit (assuming an IBM numbering scheme, which
3299 * for PowerPC (tm) is usually the case) in the register holds
Jan Ceuleers0977f812012-06-05 03:42:12 +00003300 * the entry.
3301 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302static void gfar_set_hash_for_addr(struct net_device *dev, u8 *addr)
3303{
3304 u32 tempval;
3305 struct gfar_private *priv = netdev_priv(dev);
Joe Perches6a3c910c2011-11-16 09:38:02 +00003306 u32 result = ether_crc(ETH_ALEN, addr);
Kumar Gala0bbaf062005-06-20 10:54:21 -05003307 int width = priv->hash_width;
3308 u8 whichbit = (result >> (32 - width)) & 0x1f;
3309 u8 whichreg = result >> (32 - width + 5);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 u32 value = (1 << (31-whichbit));
3311
Kumar Gala0bbaf062005-06-20 10:54:21 -05003312 tempval = gfar_read(priv->hash_regs[whichreg]);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003313 tempval |= value;
Kumar Gala0bbaf062005-06-20 10:54:21 -05003314 gfar_write(priv->hash_regs[whichreg], tempval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315}
3316
Andy Fleming7f7f5312005-11-11 12:38:59 -06003317
3318/* There are multiple MAC Address register pairs on some controllers
3319 * This function sets the numth pair to a given address
3320 */
Joe Perchesb6bc7652010-12-21 02:16:08 -08003321static void gfar_set_mac_for_addr(struct net_device *dev, int num,
3322 const u8 *addr)
Andy Fleming7f7f5312005-11-11 12:38:59 -06003323{
3324 struct gfar_private *priv = netdev_priv(dev);
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003325 struct gfar __iomem *regs = priv->gfargrp[0].regs;
Andy Fleming7f7f5312005-11-11 12:38:59 -06003326 int idx;
Joe Perches6a3c910c2011-11-16 09:38:02 +00003327 char tmpbuf[ETH_ALEN];
Andy Fleming7f7f5312005-11-11 12:38:59 -06003328 u32 tempval;
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003329 u32 __iomem *macptr = &regs->macstnaddr1;
Andy Fleming7f7f5312005-11-11 12:38:59 -06003330
3331 macptr += num*2;
3332
Jan Ceuleers0977f812012-06-05 03:42:12 +00003333 /* Now copy it into the mac registers backwards, cuz
3334 * little endian is silly
3335 */
Joe Perches6a3c910c2011-11-16 09:38:02 +00003336 for (idx = 0; idx < ETH_ALEN; idx++)
3337 tmpbuf[ETH_ALEN - 1 - idx] = addr[idx];
Andy Fleming7f7f5312005-11-11 12:38:59 -06003338
3339 gfar_write(macptr, *((u32 *) (tmpbuf)));
3340
3341 tempval = *((u32 *) (tmpbuf + 4));
3342
3343 gfar_write(macptr+1, tempval);
3344}
3345
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346/* GFAR error interrupt handler */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003347static irqreturn_t gfar_error(int irq, void *grp_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003349 struct gfar_priv_grp *gfargrp = grp_id;
3350 struct gfar __iomem *regs = gfargrp->regs;
3351 struct gfar_private *priv= gfargrp->priv;
3352 struct net_device *dev = priv->ndev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353
3354 /* Save ievent for future reference */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003355 u32 events = gfar_read(&regs->ievent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003356
3357 /* Clear IEVENT */
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003358 gfar_write(&regs->ievent, events & IEVENT_ERR_MASK);
Scott Woodd87eb122008-07-11 18:04:45 -05003359
3360 /* Magic Packet is not an error. */
Andy Flemingb31a1d82008-12-16 15:29:15 -08003361 if ((priv->device_flags & FSL_GIANFAR_DEV_HAS_MAGIC_PACKET) &&
Scott Woodd87eb122008-07-11 18:04:45 -05003362 (events & IEVENT_MAG))
3363 events &= ~IEVENT_MAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364
3365 /* Hmm... */
Kumar Gala0bbaf062005-06-20 10:54:21 -05003366 if (netif_msg_rx_err(priv) || netif_msg_tx_err(priv))
Jan Ceuleersbc4598b2012-06-05 03:42:13 +00003367 netdev_dbg(dev,
3368 "error interrupt (ievent=0x%08x imask=0x%08x)\n",
Joe Perches59deab22011-06-14 08:57:47 +00003369 events, gfar_read(&regs->imask));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003370
3371 /* Update the error counters */
3372 if (events & IEVENT_TXE) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003373 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374
3375 if (events & IEVENT_LC)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003376 dev->stats.tx_window_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 if (events & IEVENT_CRL)
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003378 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 if (events & IEVENT_XFUN) {
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003380 unsigned long flags;
3381
Joe Perches59deab22011-06-14 08:57:47 +00003382 netif_dbg(priv, tx_err, dev,
3383 "TX FIFO underrun, packet dropped\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003384 dev->stats.tx_dropped++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05003385 atomic64_inc(&priv->extra_stats.tx_underrun);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003386
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003387 local_irq_save(flags);
3388 lock_tx_qs(priv);
3389
Linus Torvalds1da177e2005-04-16 15:20:36 -07003390 /* Reactivate the Tx Queues */
Sandeep Gopalpetfba4ed02009-11-02 07:03:15 +00003391 gfar_write(&regs->tstat, gfargrp->tstat);
Anton Vorontsov836cf7f2009-11-10 14:11:08 +00003392
3393 unlock_tx_qs(priv);
3394 local_irq_restore(flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 }
Joe Perches59deab22011-06-14 08:57:47 +00003396 netif_dbg(priv, tx_err, dev, "Transmit Error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 }
3398 if (events & IEVENT_BSY) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003399 dev->stats.rx_errors++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05003400 atomic64_inc(&priv->extra_stats.rx_bsy);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003401
Sandeep Gopalpetf4983702009-11-02 07:03:09 +00003402 gfar_receive(irq, grp_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003403
Joe Perches59deab22011-06-14 08:57:47 +00003404 netif_dbg(priv, rx_err, dev, "busy error (rstat: %x)\n",
3405 gfar_read(&regs->rstat));
Linus Torvalds1da177e2005-04-16 15:20:36 -07003406 }
3407 if (events & IEVENT_BABR) {
Jeff Garzik09f75cd2007-10-03 17:41:50 -07003408 dev->stats.rx_errors++;
Paul Gortmaker212079d2013-02-12 15:38:19 -05003409 atomic64_inc(&priv->extra_stats.rx_babr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410
Joe Perches59deab22011-06-14 08:57:47 +00003411 netif_dbg(priv, rx_err, dev, "babbling RX error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003412 }
3413 if (events & IEVENT_EBERR) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05003414 atomic64_inc(&priv->extra_stats.eberr);
Joe Perches59deab22011-06-14 08:57:47 +00003415 netif_dbg(priv, rx_err, dev, "bus error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 }
Joe Perches59deab22011-06-14 08:57:47 +00003417 if (events & IEVENT_RXC)
3418 netif_dbg(priv, rx_status, dev, "control frame\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003419
3420 if (events & IEVENT_BABT) {
Paul Gortmaker212079d2013-02-12 15:38:19 -05003421 atomic64_inc(&priv->extra_stats.tx_babt);
Joe Perches59deab22011-06-14 08:57:47 +00003422 netif_dbg(priv, tx_err, dev, "babbling TX error\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423 }
3424 return IRQ_HANDLED;
3425}
3426
Andy Flemingb31a1d82008-12-16 15:29:15 -08003427static struct of_device_id gfar_match[] =
3428{
3429 {
3430 .type = "network",
3431 .compatible = "gianfar",
3432 },
Sandeep Gopalpet46ceb602009-11-02 07:03:34 +00003433 {
3434 .compatible = "fsl,etsec2",
3435 },
Andy Flemingb31a1d82008-12-16 15:29:15 -08003436 {},
3437};
Anton Vorontsove72701a2009-10-14 14:54:52 -07003438MODULE_DEVICE_TABLE(of, gfar_match);
Andy Flemingb31a1d82008-12-16 15:29:15 -08003439
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440/* Structure for a device driver */
Grant Likely74888762011-02-22 21:05:51 -07003441static struct platform_driver gfar_driver = {
Grant Likely40182942010-04-13 16:13:02 -07003442 .driver = {
3443 .name = "fsl-gianfar",
3444 .owner = THIS_MODULE,
3445 .pm = GFAR_PM_OPS,
3446 .of_match_table = gfar_match,
3447 },
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448 .probe = gfar_probe,
3449 .remove = gfar_remove,
3450};
3451
Axel Lindb62f682011-11-27 16:44:17 +00003452module_platform_driver(gfar_driver);