blob: 513ed8b1ba582add37602ab4c93faaf462c7f3a3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * meth.c -- O2 Builtin 10/100 Ethernet driver
3 *
4 * Copyright (C) 2001-2003 Ilya Volynets
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
10 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include <linux/delay.h>
Ralf Baechlee9712902007-05-24 12:54:04 +010012#include <linux/dma-mapping.h>
13#include <linux/init.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/slab.h>
Ralf Baechlee9712902007-05-24 12:54:04 +010018#include <linux/errno.h>
19#include <linux/types.h>
20#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
22#include <linux/in.h>
23#include <linux/in6.h>
24#include <linux/device.h> /* struct device, et al */
25#include <linux/netdevice.h> /* struct device, and other headers */
26#include <linux/etherdevice.h> /* eth_type_trans */
27#include <linux/ip.h> /* struct iphdr */
28#include <linux/tcp.h> /* struct tcphdr */
29#include <linux/skbuff.h>
30#include <linux/mii.h> /* MII definitions */
Joshua Kinardc0d2b832011-12-26 19:06:15 +000031#include <linux/crc32.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include <asm/ip32/mace.h>
34#include <asm/ip32/ip32_ints.h>
35
36#include <asm/io.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38#include "meth.h"
39
40#ifndef MFE_DEBUG
41#define MFE_DEBUG 0
42#endif
43
44#if MFE_DEBUG>=1
Harvey Harrisonb39d66a2008-08-20 16:52:04 -070045#define DPRINTK(str,args...) printk(KERN_DEBUG "meth: %s: " str, __func__ , ## args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#define MFE_RX_DEBUG 2
47#else
48#define DPRINTK(str,args...)
49#define MFE_RX_DEBUG 0
50#endif
51
52
53static const char *meth_str="SGI O2 Fast Ethernet";
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Linus Torvalds1da177e2005-04-16 15:20:36 -070055/* The maximum time waited (in jiffies) before assuming a Tx failed. (400ms) */
56#define TX_TIMEOUT (400*HZ/1000)
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058static int timeout = TX_TIMEOUT;
Rusty Russell8d3b33f2006-03-25 03:07:05 -080059module_param(timeout, int, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
61/*
Joshua Kinardc0d2b832011-12-26 19:06:15 +000062 * Maximum number of multicast addresses to filter (vs. Rx-all-multicast).
63 * MACE Ethernet uses a 64 element hash table based on the Ethernet CRC.
64 */
65#define METH_MCF_LIMIT 32
66
67/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * This structure is private to each device. It is used to pass
69 * packets in and out, so there is place for a packet
70 */
71struct meth_private {
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 /* in-memory copy of MAC Control register */
Joshua Kinardc0d2b832011-12-26 19:06:15 +000073 u64 mac_ctrl;
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 /* in-memory copy of DMA Control register */
76 unsigned long dma_ctrl;
77 /* address of PHY, used by mdio_* functions, initialized in mdio_probe */
78 unsigned long phy_addr;
79 tx_packet *tx_ring;
80 dma_addr_t tx_ring_dma;
81 struct sk_buff *tx_skbs[TX_RING_ENTRIES];
82 dma_addr_t tx_skb_dmas[TX_RING_ENTRIES];
83 unsigned long tx_read, tx_write, tx_count;
84
85 rx_packet *rx_ring[RX_RING_ENTRIES];
86 dma_addr_t rx_ring_dmas[RX_RING_ENTRIES];
87 struct sk_buff *rx_skbs[RX_RING_ENTRIES];
88 unsigned long rx_write;
89
Joshua Kinardc0d2b832011-12-26 19:06:15 +000090 /* Multicast filter. */
91 u64 mcast_filter;
92
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 spinlock_t meth_lock;
94};
95
96static void meth_tx_timeout(struct net_device *dev);
David Howells7d12e782006-10-05 14:55:46 +010097static irqreturn_t meth_interrupt(int irq, void *dev_id);
Jeff Garzik6aa20a22006-09-13 13:24:59 -040098
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/* global, initialized in ip32-setup.c */
100char o2meth_eaddr[8]={0,0,0,0,0,0,0,0};
101
102static inline void load_eaddr(struct net_device *dev)
103{
104 int i;
Thomas Bogendoerferedba2a12008-01-06 00:21:47 -0800105 u64 macaddr;
Joe Perches0795af52007-10-03 17:59:30 -0700106
Johannes Berge1749612008-10-27 15:59:26 -0700107 DPRINTK("Loading MAC Address: %pM\n", dev->dev_addr);
Thomas Bogendoerferedba2a12008-01-06 00:21:47 -0800108 macaddr = 0;
109 for (i = 0; i < 6; i++)
Thomas Bogendoerferd91d4bb2008-07-31 01:14:24 +0200110 macaddr |= (u64)dev->dev_addr[i] << ((5 - i) * 8);
Thomas Bogendoerferedba2a12008-01-06 00:21:47 -0800111
112 mace->eth.mac_addr = macaddr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113}
114
115/*
116 * Waits for BUSY status of mdio bus to clear
117 */
118#define WAIT_FOR_PHY(___rval) \
119 while ((___rval = mace->eth.phy_data) & MDIO_BUSY) { \
120 udelay(25); \
121 }
122/*read phy register, return value read */
123static unsigned long mdio_read(struct meth_private *priv, unsigned long phyreg)
124{
125 unsigned long rval;
126 WAIT_FOR_PHY(rval);
127 mace->eth.phy_regs = (priv->phy_addr << 5) | (phyreg & 0x1f);
128 udelay(25);
129 mace->eth.phy_trans_go = 1;
130 udelay(25);
131 WAIT_FOR_PHY(rval);
132 return rval & MDIO_DATA_MASK;
133}
134
135static int mdio_probe(struct meth_private *priv)
136{
137 int i;
Ralf Baechlea8f492c2009-05-16 01:21:58 +0000138 unsigned long p2, p3, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 /* check if phy is detected already */
140 if(priv->phy_addr>=0&&priv->phy_addr<32)
141 return 0;
Ralf Baechlea8f492c2009-05-16 01:21:58 +0000142 spin_lock_irqsave(&priv->meth_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 for (i=0;i<32;++i){
144 priv->phy_addr=i;
145 p2=mdio_read(priv,2);
146 p3=mdio_read(priv,3);
147#if MFE_DEBUG>=2
148 switch ((p2<<12)|(p3>>4)){
149 case PHY_QS6612X:
150 DPRINTK("PHY is QS6612X\n");
151 break;
152 case PHY_ICS1889:
153 DPRINTK("PHY is ICS1889\n");
154 break;
155 case PHY_ICS1890:
156 DPRINTK("PHY is ICS1890\n");
157 break;
158 case PHY_DP83840:
159 DPRINTK("PHY is DP83840\n");
160 break;
161 }
162#endif
163 if(p2!=0xffff&&p2!=0x0000){
164 DPRINTK("PHY code: %x\n",(p2<<12)|(p3>>4));
165 break;
166 }
167 }
Ralf Baechlea8f492c2009-05-16 01:21:58 +0000168 spin_unlock_irqrestore(&priv->meth_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if(priv->phy_addr<32) {
170 return 0;
171 }
172 DPRINTK("Oopsie! PHY is not known!\n");
173 priv->phy_addr=-1;
174 return -ENODEV;
175}
176
177static void meth_check_link(struct net_device *dev)
178{
Ralf Baechledaeafdc2007-02-16 12:52:17 +0000179 struct meth_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 unsigned long mii_advertising = mdio_read(priv, 4);
181 unsigned long mii_partner = mdio_read(priv, 5);
182 unsigned long negotiated = mii_advertising & mii_partner;
183 unsigned long duplex, speed;
184
185 if (mii_partner == 0xffff)
186 return;
187
188 speed = (negotiated & 0x0380) ? METH_100MBIT : 0;
189 duplex = ((negotiated & 0x0100) || (negotiated & 0x01C0) == 0x0040) ?
190 METH_PHY_FDX : 0;
191
192 if ((priv->mac_ctrl & METH_PHY_FDX) ^ duplex) {
193 DPRINTK("Setting %s-duplex\n", duplex ? "full" : "half");
194 if (duplex)
195 priv->mac_ctrl |= METH_PHY_FDX;
196 else
197 priv->mac_ctrl &= ~METH_PHY_FDX;
198 mace->eth.mac_ctrl = priv->mac_ctrl;
199 }
200
201 if ((priv->mac_ctrl & METH_100MBIT) ^ speed) {
202 DPRINTK("Setting %dMbs mode\n", speed ? 100 : 10);
203 if (duplex)
204 priv->mac_ctrl |= METH_100MBIT;
205 else
206 priv->mac_ctrl &= ~METH_100MBIT;
207 mace->eth.mac_ctrl = priv->mac_ctrl;
208 }
209}
210
211
212static int meth_init_tx_ring(struct meth_private *priv)
213{
214 /* Init TX ring */
Joe Perchesede23fa2013-08-26 22:45:23 -0700215 priv->tx_ring = dma_zalloc_coherent(NULL, TX_RING_BUFFER_SIZE,
216 &priv->tx_ring_dma, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 if (!priv->tx_ring)
218 return -ENOMEM;
Joe Perches1f9061d22013-03-15 07:23:58 +0000219
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 priv->tx_count = priv->tx_read = priv->tx_write = 0;
221 mace->eth.tx_ring_base = priv->tx_ring_dma;
222 /* Now init skb save area */
223 memset(priv->tx_skbs, 0, sizeof(priv->tx_skbs));
224 memset(priv->tx_skb_dmas, 0, sizeof(priv->tx_skb_dmas));
225 return 0;
226}
227
228static int meth_init_rx_ring(struct meth_private *priv)
229{
230 int i;
231
232 for (i = 0; i < RX_RING_ENTRIES; i++) {
233 priv->rx_skbs[i] = alloc_skb(METH_RX_BUFF_SIZE, 0);
234 /* 8byte status vector + 3quad padding + 2byte padding,
235 * to put data on 64bit aligned boundary */
236 skb_reserve(priv->rx_skbs[i],METH_RX_HEAD);
237 priv->rx_ring[i]=(rx_packet*)(priv->rx_skbs[i]->head);
238 /* I'll need to re-sync it after each RX */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400239 priv->rx_ring_dmas[i] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 dma_map_single(NULL, priv->rx_ring[i],
241 METH_RX_BUFF_SIZE, DMA_FROM_DEVICE);
242 mace->eth.rx_fifo = priv->rx_ring_dmas[i];
243 }
244 priv->rx_write = 0;
245 return 0;
246}
247static void meth_free_tx_ring(struct meth_private *priv)
248{
249 int i;
250
251 /* Remove any pending skb */
252 for (i = 0; i < TX_RING_ENTRIES; i++) {
253 if (priv->tx_skbs[i])
254 dev_kfree_skb(priv->tx_skbs[i]);
255 priv->tx_skbs[i] = NULL;
256 }
257 dma_free_coherent(NULL, TX_RING_BUFFER_SIZE, priv->tx_ring,
258 priv->tx_ring_dma);
259}
260
261/* Presumes RX DMA engine is stopped, and RX fifo ring is reset */
262static void meth_free_rx_ring(struct meth_private *priv)
263{
264 int i;
265
266 for (i = 0; i < RX_RING_ENTRIES; i++) {
267 dma_unmap_single(NULL, priv->rx_ring_dmas[i],
268 METH_RX_BUFF_SIZE, DMA_FROM_DEVICE);
269 priv->rx_ring[i] = 0;
270 priv->rx_ring_dmas[i] = 0;
271 kfree_skb(priv->rx_skbs[i]);
272 }
273}
274
275int meth_reset(struct net_device *dev)
276{
Ralf Baechledaeafdc2007-02-16 12:52:17 +0000277 struct meth_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 /* Reset card */
280 mace->eth.mac_ctrl = SGI_MAC_RESET;
281 udelay(1);
282 mace->eth.mac_ctrl = 0;
283 udelay(25);
284
285 /* Load ethernet address */
286 load_eaddr(dev);
287 /* Should load some "errata", but later */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400288
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 /* Check for device */
290 if (mdio_probe(priv) < 0) {
291 DPRINTK("Unable to find PHY\n");
292 return -ENODEV;
293 }
294
295 /* Initial mode: 10 | Half-duplex | Accept normal packets */
296 priv->mac_ctrl = METH_ACCEPT_MCAST | METH_DEFAULT_IPG;
Christoph Lametercdc18a62008-07-11 16:12:34 -0500297 if (dev->flags & IFF_PROMISC)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 priv->mac_ctrl |= METH_PROMISC;
299 mace->eth.mac_ctrl = priv->mac_ctrl;
300
301 /* Autonegotiate speed and duplex mode */
302 meth_check_link(dev);
303
304 /* Now set dma control, but don't enable DMA, yet */
305 priv->dma_ctrl = (4 << METH_RX_OFFSET_SHIFT) |
306 (RX_RING_ENTRIES << METH_RX_DEPTH_SHIFT);
307 mace->eth.dma_ctrl = priv->dma_ctrl;
308
309 return 0;
310}
311
312/*============End Helper Routines=====================*/
313
314/*
315 * Open and close
316 */
317static int meth_open(struct net_device *dev)
318{
Ralf Baechledaeafdc2007-02-16 12:52:17 +0000319 struct meth_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 int ret;
321
322 priv->phy_addr = -1; /* No PHY is known yet... */
323
324 /* Initialize the hardware */
325 ret = meth_reset(dev);
326 if (ret < 0)
327 return ret;
328
329 /* Allocate the ring buffers */
330 ret = meth_init_tx_ring(priv);
331 if (ret < 0)
332 return ret;
333 ret = meth_init_rx_ring(priv);
334 if (ret < 0)
335 goto out_free_tx_ring;
336
337 ret = request_irq(dev->irq, meth_interrupt, 0, meth_str, dev);
338 if (ret) {
339 printk(KERN_ERR "%s: Can't get irq %d\n", dev->name, dev->irq);
340 goto out_free_rx_ring;
341 }
342
343 /* Start DMA */
344 priv->dma_ctrl |= METH_DMA_TX_EN | /*METH_DMA_TX_INT_EN |*/
345 METH_DMA_RX_EN | METH_DMA_RX_INT_EN;
346 mace->eth.dma_ctrl = priv->dma_ctrl;
347
348 DPRINTK("About to start queue\n");
349 netif_start_queue(dev);
350
351 return 0;
352
353out_free_rx_ring:
354 meth_free_rx_ring(priv);
355out_free_tx_ring:
356 meth_free_tx_ring(priv);
357
358 return ret;
359}
360
361static int meth_release(struct net_device *dev)
362{
Ralf Baechledaeafdc2007-02-16 12:52:17 +0000363 struct meth_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 DPRINTK("Stopping queue\n");
366 netif_stop_queue(dev); /* can't transmit any more */
367 /* shut down DMA */
368 priv->dma_ctrl &= ~(METH_DMA_TX_EN | METH_DMA_TX_INT_EN |
369 METH_DMA_RX_EN | METH_DMA_RX_INT_EN);
370 mace->eth.dma_ctrl = priv->dma_ctrl;
371 free_irq(dev->irq, dev);
372 meth_free_tx_ring(priv);
373 meth_free_rx_ring(priv);
374
375 return 0;
376}
377
378/*
379 * Receive a packet: retrieve, encapsulate and pass over to upper levels
380 */
381static void meth_rx(struct net_device* dev, unsigned long int_status)
382{
383 struct sk_buff *skb;
Ralf Baechlea8f492c2009-05-16 01:21:58 +0000384 unsigned long status, flags;
Ralf Baechledaeafdc2007-02-16 12:52:17 +0000385 struct meth_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 unsigned long fifo_rptr = (int_status & METH_INT_RX_RPTR_MASK) >> 8;
387
Ralf Baechlea8f492c2009-05-16 01:21:58 +0000388 spin_lock_irqsave(&priv->meth_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 priv->dma_ctrl &= ~METH_DMA_RX_INT_EN;
390 mace->eth.dma_ctrl = priv->dma_ctrl;
Ralf Baechlea8f492c2009-05-16 01:21:58 +0000391 spin_unlock_irqrestore(&priv->meth_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 if (int_status & METH_INT_RX_UNDERFLOW) {
394 fifo_rptr = (fifo_rptr - 1) & 0x0f;
395 }
396 while (priv->rx_write != fifo_rptr) {
397 dma_unmap_single(NULL, priv->rx_ring_dmas[priv->rx_write],
398 METH_RX_BUFF_SIZE, DMA_FROM_DEVICE);
399 status = priv->rx_ring[priv->rx_write]->status.raw;
400#if MFE_DEBUG
401 if (!(status & METH_RX_ST_VALID)) {
402 DPRINTK("Not received? status=%016lx\n",status);
403 }
404#endif
405 if ((!(status & METH_RX_STATUS_ERRORS)) && (status & METH_RX_ST_VALID)) {
406 int len = (status & 0xffff) - 4; /* omit CRC */
407 /* length sanity check */
408 if (len < 60 || len > 1518) {
Ralf Baechle12e22e82009-03-30 14:49:41 +0200409 printk(KERN_DEBUG "%s: bogus packet size: %ld, status=%#2Lx.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 dev->name, priv->rx_write,
411 priv->rx_ring[priv->rx_write]->status.raw);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700412 dev->stats.rx_errors++;
413 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 skb = priv->rx_skbs[priv->rx_write];
415 } else {
Ralf Baechle09e06f62007-08-15 12:53:16 +0100416 skb = alloc_skb(METH_RX_BUFF_SIZE, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 if (!skb) {
418 /* Ouch! No memory! Drop packet on the floor */
419 DPRINTK("No mem: dropping packet\n");
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700420 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 skb = priv->rx_skbs[priv->rx_write];
422 } else {
423 struct sk_buff *skb_c = priv->rx_skbs[priv->rx_write];
424 /* 8byte status vector + 3quad padding + 2byte padding,
425 * to put data on 64bit aligned boundary */
426 skb_reserve(skb, METH_RX_HEAD);
427 /* Write metadata, and then pass to the receive level */
428 skb_put(skb_c, len);
429 priv->rx_skbs[priv->rx_write] = skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 skb_c->protocol = eth_type_trans(skb_c, dev);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700431 dev->stats.rx_packets++;
432 dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 netif_rx(skb_c);
434 }
435 }
436 } else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700437 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 skb=priv->rx_skbs[priv->rx_write];
439#if MFE_DEBUG>0
440 printk(KERN_WARNING "meth: RX error: status=0x%016lx\n",status);
441 if(status&METH_RX_ST_RCV_CODE_VIOLATION)
442 printk(KERN_WARNING "Receive Code Violation\n");
443 if(status&METH_RX_ST_CRC_ERR)
444 printk(KERN_WARNING "CRC error\n");
445 if(status&METH_RX_ST_INV_PREAMBLE_CTX)
446 printk(KERN_WARNING "Invalid Preamble Context\n");
447 if(status&METH_RX_ST_LONG_EVT_SEEN)
448 printk(KERN_WARNING "Long Event Seen...\n");
449 if(status&METH_RX_ST_BAD_PACKET)
450 printk(KERN_WARNING "Bad Packet\n");
451 if(status&METH_RX_ST_CARRIER_EVT_SEEN)
452 printk(KERN_WARNING "Carrier Event Seen\n");
453#endif
454 }
455 priv->rx_ring[priv->rx_write] = (rx_packet*)skb->head;
456 priv->rx_ring[priv->rx_write]->status.raw = 0;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400457 priv->rx_ring_dmas[priv->rx_write] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 dma_map_single(NULL, priv->rx_ring[priv->rx_write],
459 METH_RX_BUFF_SIZE, DMA_FROM_DEVICE);
460 mace->eth.rx_fifo = priv->rx_ring_dmas[priv->rx_write];
461 ADVANCE_RX_PTR(priv->rx_write);
462 }
Ralf Baechlea8f492c2009-05-16 01:21:58 +0000463 spin_lock_irqsave(&priv->meth_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 /* In case there was underflow, and Rx DMA was disabled */
465 priv->dma_ctrl |= METH_DMA_RX_INT_EN | METH_DMA_RX_EN;
466 mace->eth.dma_ctrl = priv->dma_ctrl;
467 mace->eth.int_stat = METH_INT_RX_THRESHOLD;
Ralf Baechlea8f492c2009-05-16 01:21:58 +0000468 spin_unlock_irqrestore(&priv->meth_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469}
470
471static int meth_tx_full(struct net_device *dev)
472{
Ralf Baechledaeafdc2007-02-16 12:52:17 +0000473 struct meth_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Eric Dumazet807540b2010-09-23 05:40:09 +0000475 return priv->tx_count >= TX_RING_ENTRIES - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476}
477
478static void meth_tx_cleanup(struct net_device* dev, unsigned long int_status)
479{
Ralf Baechledaeafdc2007-02-16 12:52:17 +0000480 struct meth_private *priv = netdev_priv(dev);
Ralf Baechlea8f492c2009-05-16 01:21:58 +0000481 unsigned long status, flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 struct sk_buff *skb;
483 unsigned long rptr = (int_status&TX_INFO_RPTR) >> 16;
484
Ralf Baechlea8f492c2009-05-16 01:21:58 +0000485 spin_lock_irqsave(&priv->meth_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486
487 /* Stop DMA notification */
488 priv->dma_ctrl &= ~(METH_DMA_TX_INT_EN);
489 mace->eth.dma_ctrl = priv->dma_ctrl;
490
491 while (priv->tx_read != rptr) {
492 skb = priv->tx_skbs[priv->tx_read];
493 status = priv->tx_ring[priv->tx_read].header.raw;
494#if MFE_DEBUG>=1
495 if (priv->tx_read == priv->tx_write)
496 DPRINTK("Auchi! tx_read=%d,tx_write=%d,rptr=%d?\n", priv->tx_read, priv->tx_write,rptr);
497#endif
498 if (status & METH_TX_ST_DONE) {
499 if (status & METH_TX_ST_SUCCESS){
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700500 dev->stats.tx_packets++;
501 dev->stats.tx_bytes += skb->len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 } else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700503 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504#if MFE_DEBUG>=1
505 DPRINTK("TX error: status=%016lx <",status);
506 if(status & METH_TX_ST_SUCCESS)
507 printk(" SUCCESS");
508 if(status & METH_TX_ST_TOOLONG)
509 printk(" TOOLONG");
510 if(status & METH_TX_ST_UNDERRUN)
511 printk(" UNDERRUN");
512 if(status & METH_TX_ST_EXCCOLL)
513 printk(" EXCCOLL");
514 if(status & METH_TX_ST_DEFER)
515 printk(" DEFER");
516 if(status & METH_TX_ST_LATECOLL)
517 printk(" LATECOLL");
518 printk(" >\n");
519#endif
520 }
521 } else {
522 DPRINTK("RPTR points us here, but packet not done?\n");
523 break;
524 }
525 dev_kfree_skb_irq(skb);
526 priv->tx_skbs[priv->tx_read] = NULL;
527 priv->tx_ring[priv->tx_read].header.raw = 0;
528 priv->tx_read = (priv->tx_read+1)&(TX_RING_ENTRIES-1);
529 priv->tx_count--;
530 }
531
532 /* wake up queue if it was stopped */
533 if (netif_queue_stopped(dev) && !meth_tx_full(dev)) {
534 netif_wake_queue(dev);
535 }
536
537 mace->eth.int_stat = METH_INT_TX_EMPTY | METH_INT_TX_PKT;
Ralf Baechlea8f492c2009-05-16 01:21:58 +0000538 spin_unlock_irqrestore(&priv->meth_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
541static void meth_error(struct net_device* dev, unsigned status)
542{
Ralf Baechledaeafdc2007-02-16 12:52:17 +0000543 struct meth_private *priv = netdev_priv(dev);
Ralf Baechlea8f492c2009-05-16 01:21:58 +0000544 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
546 printk(KERN_WARNING "meth: error status: 0x%08x\n",status);
547 /* check for errors too... */
548 if (status & (METH_INT_TX_LINK_FAIL))
549 printk(KERN_WARNING "meth: link failure\n");
550 /* Should I do full reset in this case? */
551 if (status & (METH_INT_MEM_ERROR))
552 printk(KERN_WARNING "meth: memory error\n");
553 if (status & (METH_INT_TX_ABORT))
554 printk(KERN_WARNING "meth: aborted\n");
555 if (status & (METH_INT_RX_OVERFLOW))
556 printk(KERN_WARNING "meth: Rx overflow\n");
557 if (status & (METH_INT_RX_UNDERFLOW)) {
558 printk(KERN_WARNING "meth: Rx underflow\n");
Ralf Baechlea8f492c2009-05-16 01:21:58 +0000559 spin_lock_irqsave(&priv->meth_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 mace->eth.int_stat = METH_INT_RX_UNDERFLOW;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400561 /* more underflow interrupts will be delivered,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 * effectively throwing us into an infinite loop.
563 * Thus I stop processing Rx in this case. */
564 priv->dma_ctrl &= ~METH_DMA_RX_EN;
565 mace->eth.dma_ctrl = priv->dma_ctrl;
566 DPRINTK("Disabled meth Rx DMA temporarily\n");
Ralf Baechlea8f492c2009-05-16 01:21:58 +0000567 spin_unlock_irqrestore(&priv->meth_lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 }
569 mace->eth.int_stat = METH_INT_ERROR;
570}
571
572/*
573 * The typical interrupt entry point
574 */
David Howells7d12e782006-10-05 14:55:46 +0100575static irqreturn_t meth_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576{
577 struct net_device *dev = (struct net_device *)dev_id;
Ralf Baechledaeafdc2007-02-16 12:52:17 +0000578 struct meth_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 unsigned long status;
580
581 status = mace->eth.int_stat;
582 while (status & 0xff) {
583 /* First handle errors - if we get Rx underflow,
584 * Rx DMA will be disabled, and Rx handler will reenable
585 * it. I don't think it's possible to get Rx underflow,
586 * without getting Rx interrupt */
587 if (status & METH_INT_ERROR) {
588 meth_error(dev, status);
589 }
590 if (status & (METH_INT_TX_EMPTY | METH_INT_TX_PKT)) {
591 /* a transmission is over: free the skb */
592 meth_tx_cleanup(dev, status);
593 }
594 if (status & METH_INT_RX_THRESHOLD) {
595 if (!(priv->dma_ctrl & METH_DMA_RX_INT_EN))
596 break;
597 /* send it to meth_rx for handling */
598 meth_rx(dev, status);
599 }
600 status = mace->eth.int_stat;
601 }
602
603 return IRQ_HANDLED;
604}
605
606/*
607 * Transmits packets that fit into TX descriptor (are <=120B)
608 */
609static void meth_tx_short_prepare(struct meth_private *priv,
610 struct sk_buff *skb)
611{
612 tx_packet *desc = &priv->tx_ring[priv->tx_write];
613 int len = (skb->len < ETH_ZLEN) ? ETH_ZLEN : skb->len;
614
615 desc->header.raw = METH_TX_CMD_INT_EN | (len-1) | ((128-len) << 16);
616 /* maybe I should set whole thing to 0 first... */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300617 skb_copy_from_linear_data(skb, desc->data.dt + (120 - len), skb->len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 if (skb->len < len)
619 memset(desc->data.dt + 120 - len + skb->len, 0, len-skb->len);
620}
621#define TX_CATBUF1 BIT(25)
622static void meth_tx_1page_prepare(struct meth_private *priv,
623 struct sk_buff *skb)
624{
625 tx_packet *desc = &priv->tx_ring[priv->tx_write];
626 void *buffer_data = (void *)(((unsigned long)skb->data + 7) & ~7);
627 int unaligned_len = (int)((unsigned long)buffer_data - (unsigned long)skb->data);
628 int buffer_len = skb->len - unaligned_len;
629 dma_addr_t catbuf;
630
631 desc->header.raw = METH_TX_CMD_INT_EN | TX_CATBUF1 | (skb->len - 1);
632
633 /* unaligned part */
634 if (unaligned_len) {
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300635 skb_copy_from_linear_data(skb, desc->data.dt + (120 - unaligned_len),
636 unaligned_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 desc->header.raw |= (128 - unaligned_len) << 16;
638 }
639
640 /* first page */
641 catbuf = dma_map_single(NULL, buffer_data, buffer_len,
642 DMA_TO_DEVICE);
643 desc->data.cat_buf[0].form.start_addr = catbuf >> 3;
644 desc->data.cat_buf[0].form.len = buffer_len - 1;
645}
646#define TX_CATBUF2 BIT(26)
647static void meth_tx_2page_prepare(struct meth_private *priv,
648 struct sk_buff *skb)
649{
650 tx_packet *desc = &priv->tx_ring[priv->tx_write];
651 void *buffer1_data = (void *)(((unsigned long)skb->data + 7) & ~7);
652 void *buffer2_data = (void *)PAGE_ALIGN((unsigned long)skb->data);
653 int unaligned_len = (int)((unsigned long)buffer1_data - (unsigned long)skb->data);
654 int buffer1_len = (int)((unsigned long)buffer2_data - (unsigned long)buffer1_data);
655 int buffer2_len = skb->len - buffer1_len - unaligned_len;
656 dma_addr_t catbuf1, catbuf2;
657
658 desc->header.raw = METH_TX_CMD_INT_EN | TX_CATBUF1 | TX_CATBUF2| (skb->len - 1);
659 /* unaligned part */
660 if (unaligned_len){
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300661 skb_copy_from_linear_data(skb, desc->data.dt + (120 - unaligned_len),
662 unaligned_len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 desc->header.raw |= (128 - unaligned_len) << 16;
664 }
665
666 /* first page */
667 catbuf1 = dma_map_single(NULL, buffer1_data, buffer1_len,
668 DMA_TO_DEVICE);
669 desc->data.cat_buf[0].form.start_addr = catbuf1 >> 3;
670 desc->data.cat_buf[0].form.len = buffer1_len - 1;
671 /* second page */
672 catbuf2 = dma_map_single(NULL, buffer2_data, buffer2_len,
673 DMA_TO_DEVICE);
674 desc->data.cat_buf[1].form.start_addr = catbuf2 >> 3;
675 desc->data.cat_buf[1].form.len = buffer2_len - 1;
676}
677
678static void meth_add_to_tx_ring(struct meth_private *priv, struct sk_buff *skb)
679{
680 /* Remember the skb, so we can free it at interrupt time */
681 priv->tx_skbs[priv->tx_write] = skb;
682 if (skb->len <= 120) {
683 /* Whole packet fits into descriptor */
684 meth_tx_short_prepare(priv, skb);
685 } else if (PAGE_ALIGN((unsigned long)skb->data) !=
686 PAGE_ALIGN((unsigned long)skb->data + skb->len - 1)) {
687 /* Packet crosses page boundary */
688 meth_tx_2page_prepare(priv, skb);
689 } else {
690 /* Packet is in one page */
691 meth_tx_1page_prepare(priv, skb);
692 }
693 priv->tx_write = (priv->tx_write + 1) & (TX_RING_ENTRIES - 1);
694 mace->eth.tx_info = priv->tx_write;
695 priv->tx_count++;
696}
697
698/*
699 * Transmit a packet (called by the kernel)
700 */
701static int meth_tx(struct sk_buff *skb, struct net_device *dev)
702{
Ralf Baechledaeafdc2007-02-16 12:52:17 +0000703 struct meth_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 unsigned long flags;
705
706 spin_lock_irqsave(&priv->meth_lock, flags);
707 /* Stop DMA notification */
708 priv->dma_ctrl &= ~(METH_DMA_TX_INT_EN);
709 mace->eth.dma_ctrl = priv->dma_ctrl;
710
711 meth_add_to_tx_ring(priv, skb);
712 dev->trans_start = jiffies; /* save the timestamp */
713
714 /* If TX ring is full, tell the upper layer to stop sending packets */
715 if (meth_tx_full(dev)) {
716 printk(KERN_DEBUG "TX full: stopping\n");
717 netif_stop_queue(dev);
718 }
719
720 /* Restart DMA notification */
721 priv->dma_ctrl |= METH_DMA_TX_INT_EN;
722 mace->eth.dma_ctrl = priv->dma_ctrl;
723
724 spin_unlock_irqrestore(&priv->meth_lock, flags);
725
Patrick McHardy6ed10652009-06-23 06:03:08 +0000726 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727}
728
729/*
730 * Deal with a transmit timeout.
731 */
732static void meth_tx_timeout(struct net_device *dev)
733{
Ralf Baechledaeafdc2007-02-16 12:52:17 +0000734 struct meth_private *priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 unsigned long flags;
736
737 printk(KERN_WARNING "%s: transmit timed out\n", dev->name);
738
739 /* Protect against concurrent rx interrupts */
740 spin_lock_irqsave(&priv->meth_lock,flags);
741
742 /* Try to reset the interface. */
743 meth_reset(dev);
744
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700745 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
747 /* Clear all rings */
748 meth_free_tx_ring(priv);
749 meth_free_rx_ring(priv);
750 meth_init_tx_ring(priv);
751 meth_init_rx_ring(priv);
752
753 /* Restart dma */
754 priv->dma_ctrl |= METH_DMA_TX_EN | METH_DMA_RX_EN | METH_DMA_RX_INT_EN;
755 mace->eth.dma_ctrl = priv->dma_ctrl;
756
757 /* Enable interrupt */
758 spin_unlock_irqrestore(&priv->meth_lock, flags);
759
Eric Dumazet1ae5dc32010-05-10 05:01:31 -0700760 dev->trans_start = jiffies; /* prevent tx timeout */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 netif_wake_queue(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762}
763
764/*
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400765 * Ioctl commands
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 */
767static int meth_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
768{
769 /* XXX Not yet implemented */
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400770 switch(cmd) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 case SIOCGMIIPHY:
772 case SIOCGMIIREG:
773 case SIOCSMIIREG:
774 default:
775 return -EOPNOTSUPP;
776 }
777}
778
Joshua Kinardc0d2b832011-12-26 19:06:15 +0000779static void meth_set_rx_mode(struct net_device *dev)
780{
781 struct meth_private *priv = netdev_priv(dev);
782 unsigned long flags;
783
784 netif_stop_queue(dev);
785 spin_lock_irqsave(&priv->meth_lock, flags);
786 priv->mac_ctrl &= ~METH_PROMISC;
787
788 if (dev->flags & IFF_PROMISC) {
789 priv->mac_ctrl |= METH_PROMISC;
790 priv->mcast_filter = 0xffffffffffffffffUL;
791 } else if ((netdev_mc_count(dev) > METH_MCF_LIMIT) ||
792 (dev->flags & IFF_ALLMULTI)) {
793 priv->mac_ctrl |= METH_ACCEPT_AMCAST;
794 priv->mcast_filter = 0xffffffffffffffffUL;
795 } else {
796 struct netdev_hw_addr *ha;
797 priv->mac_ctrl |= METH_ACCEPT_MCAST;
798
799 netdev_for_each_mc_addr(ha, dev)
800 set_bit((ether_crc(ETH_ALEN, ha->addr) >> 26),
801 (volatile unsigned long *)&priv->mcast_filter);
802 }
803
804 /* Write the changes to the chip registers. */
805 mace->eth.mac_ctrl = priv->mac_ctrl;
806 mace->eth.mcast_filter = priv->mcast_filter;
807
808 /* Done! */
809 spin_unlock_irqrestore(&priv->meth_lock, flags);
810 netif_wake_queue(dev);
811}
812
Alexander Beregalovd53bc2d2009-04-15 12:52:50 +0000813static const struct net_device_ops meth_netdev_ops = {
814 .ndo_open = meth_open,
815 .ndo_stop = meth_release,
816 .ndo_start_xmit = meth_tx,
817 .ndo_do_ioctl = meth_ioctl,
818 .ndo_tx_timeout = meth_tx_timeout,
819 .ndo_change_mtu = eth_change_mtu,
820 .ndo_validate_addr = eth_validate_addr,
821 .ndo_set_mac_address = eth_mac_addr,
Joshua Kinardc0d2b832011-12-26 19:06:15 +0000822 .ndo_set_rx_mode = meth_set_rx_mode,
Alexander Beregalovd53bc2d2009-04-15 12:52:50 +0000823};
824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825/*
826 * The init function.
827 */
Bill Pembertonf48a3c22012-12-03 09:24:01 -0500828static int meth_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
830 struct net_device *dev;
831 struct meth_private *priv;
Ralf Baechlee9712902007-05-24 12:54:04 +0100832 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
834 dev = alloc_etherdev(sizeof(struct meth_private));
835 if (!dev)
Ralf Baechlee9712902007-05-24 12:54:04 +0100836 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Alexander Beregalovd53bc2d2009-04-15 12:52:50 +0000838 dev->netdev_ops = &meth_netdev_ops;
839 dev->watchdog_timeo = timeout;
840 dev->irq = MACE_ETHERNET_IRQ;
841 dev->base_addr = (unsigned long)&mace->eth;
Joe Perchesd458cdf2013-10-01 19:04:40 -0700842 memcpy(dev->dev_addr, o2meth_eaddr, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
Ralf Baechledaeafdc2007-02-16 12:52:17 +0000844 priv = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 spin_lock_init(&priv->meth_lock);
Ralf Baechlee9712902007-05-24 12:54:04 +0100846 SET_NETDEV_DEV(dev, &pdev->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Ralf Baechlee9712902007-05-24 12:54:04 +0100848 err = register_netdev(dev);
849 if (err) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 free_netdev(dev);
Ralf Baechlee9712902007-05-24 12:54:04 +0100851 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 }
853
854 printk(KERN_INFO "%s: SGI MACE Ethernet rev. %d\n",
855 dev->name, (unsigned int)(mace->eth.mac_ctrl >> 29));
856 return 0;
857}
858
Ralf Baechlee9712902007-05-24 12:54:04 +0100859static int __exit meth_remove(struct platform_device *pdev)
860{
861 struct net_device *dev = platform_get_drvdata(pdev);
862
863 unregister_netdev(dev);
864 free_netdev(dev);
Ralf Baechlee9712902007-05-24 12:54:04 +0100865
866 return 0;
867}
868
869static struct platform_driver meth_driver = {
870 .probe = meth_probe,
Uwe Kleine-König1ebb5a12009-09-30 22:28:17 +0000871 .remove = __exit_p(meth_remove),
Ralf Baechlee9712902007-05-24 12:54:04 +0100872 .driver = {
873 .name = "meth",
Kay Sievers72abb462008-04-18 13:50:44 -0700874 .owner = THIS_MODULE,
Ralf Baechlee9712902007-05-24 12:54:04 +0100875 }
876};
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Axel Lindb62f682011-11-27 16:44:17 +0000878module_platform_driver(meth_driver);
Ralf Baechlee9712902007-05-24 12:54:04 +0100879
880MODULE_AUTHOR("Ilya Volynets <ilya@theIlya.com>");
881MODULE_DESCRIPTION("SGI O2 Builtin Fast Ethernet driver");
882MODULE_LICENSE("GPL");
Kay Sievers72abb462008-04-18 13:50:44 -0700883MODULE_ALIAS("platform:meth");