xref: /openbmc/linux/drivers/net/ethernet/seeq/sgiseeq.c (revision 03ab8e6297acd1bc0eedaa050e2a1635c576fd11)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
29e13fbf7SJeff Kirsher /*
39e13fbf7SJeff Kirsher  * sgiseeq.c: Seeq8003 ethernet driver for SGI machines.
49e13fbf7SJeff Kirsher  *
59e13fbf7SJeff Kirsher  * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
69e13fbf7SJeff Kirsher  */
79e13fbf7SJeff Kirsher 
89e13fbf7SJeff Kirsher #undef DEBUG
99e13fbf7SJeff Kirsher 
109e13fbf7SJeff Kirsher #include <linux/dma-mapping.h>
119e13fbf7SJeff Kirsher #include <linux/kernel.h>
129e13fbf7SJeff Kirsher #include <linux/module.h>
139e13fbf7SJeff Kirsher #include <linux/slab.h>
149e13fbf7SJeff Kirsher #include <linux/errno.h>
159e13fbf7SJeff Kirsher #include <linux/types.h>
169e13fbf7SJeff Kirsher #include <linux/interrupt.h>
179e13fbf7SJeff Kirsher #include <linux/string.h>
189e13fbf7SJeff Kirsher #include <linux/delay.h>
199e13fbf7SJeff Kirsher #include <linux/netdevice.h>
209e13fbf7SJeff Kirsher #include <linux/platform_device.h>
219e13fbf7SJeff Kirsher #include <linux/etherdevice.h>
229e13fbf7SJeff Kirsher #include <linux/skbuff.h>
239e13fbf7SJeff Kirsher 
249e13fbf7SJeff Kirsher #include <asm/sgi/hpc3.h>
259e13fbf7SJeff Kirsher #include <asm/sgi/ip22.h>
269e13fbf7SJeff Kirsher #include <asm/sgi/seeq.h>
279e13fbf7SJeff Kirsher 
289e13fbf7SJeff Kirsher #include "sgiseeq.h"
299e13fbf7SJeff Kirsher 
309e13fbf7SJeff Kirsher static char *sgiseeqstr = "SGI Seeq8003";
319e13fbf7SJeff Kirsher 
329e13fbf7SJeff Kirsher /*
339e13fbf7SJeff Kirsher  * If you want speed, you do something silly, it always has worked for me.  So,
349e13fbf7SJeff Kirsher  * with that in mind, I've decided to make this driver look completely like a
359e13fbf7SJeff Kirsher  * stupid Lance from a driver architecture perspective.  Only difference is that
369e13fbf7SJeff Kirsher  * here our "ring buffer" looks and acts like a real Lance one does but is
379e13fbf7SJeff Kirsher  * laid out like how the HPC DMA and the Seeq want it to.  You'd be surprised
389e13fbf7SJeff Kirsher  * how a stupid idea like this can pay off in performance, not to mention
399e13fbf7SJeff Kirsher  * making this driver 2,000 times easier to write. ;-)
409e13fbf7SJeff Kirsher  */
419e13fbf7SJeff Kirsher 
429e13fbf7SJeff Kirsher /* Tune these if we tend to run out often etc. */
439e13fbf7SJeff Kirsher #define SEEQ_RX_BUFFERS  16
449e13fbf7SJeff Kirsher #define SEEQ_TX_BUFFERS  16
459e13fbf7SJeff Kirsher 
469e13fbf7SJeff Kirsher #define PKT_BUF_SZ       1584
479e13fbf7SJeff Kirsher 
489e13fbf7SJeff Kirsher #define NEXT_RX(i)  (((i) + 1) & (SEEQ_RX_BUFFERS - 1))
499e13fbf7SJeff Kirsher #define NEXT_TX(i)  (((i) + 1) & (SEEQ_TX_BUFFERS - 1))
509e13fbf7SJeff Kirsher #define PREV_RX(i)  (((i) - 1) & (SEEQ_RX_BUFFERS - 1))
519e13fbf7SJeff Kirsher #define PREV_TX(i)  (((i) - 1) & (SEEQ_TX_BUFFERS - 1))
529e13fbf7SJeff Kirsher 
539e13fbf7SJeff Kirsher #define TX_BUFFS_AVAIL(sp) ((sp->tx_old <= sp->tx_new) ? \
549e13fbf7SJeff Kirsher 			    sp->tx_old + (SEEQ_TX_BUFFERS - 1) - sp->tx_new : \
559e13fbf7SJeff Kirsher 			    sp->tx_old - sp->tx_new - 1)
569e13fbf7SJeff Kirsher 
579e13fbf7SJeff Kirsher #define VIRT_TO_DMA(sp, v) ((sp)->srings_dma +                                 \
589e13fbf7SJeff Kirsher 				  (dma_addr_t)((unsigned long)(v) -            \
599e13fbf7SJeff Kirsher 					       (unsigned long)((sp)->rx_desc)))
609e13fbf7SJeff Kirsher 
619e13fbf7SJeff Kirsher /* Copy frames shorter than rx_copybreak, otherwise pass on up in
629e13fbf7SJeff Kirsher  * a full sized sk_buff.  Value of 100 stolen from tulip.c (!alpha).
639e13fbf7SJeff Kirsher  */
649e13fbf7SJeff Kirsher static int rx_copybreak = 100;
659e13fbf7SJeff Kirsher 
669e13fbf7SJeff Kirsher #define PAD_SIZE    (128 - sizeof(struct hpc_dma_desc) - sizeof(void *))
679e13fbf7SJeff Kirsher 
689e13fbf7SJeff Kirsher struct sgiseeq_rx_desc {
699e13fbf7SJeff Kirsher 	volatile struct hpc_dma_desc rdma;
709e13fbf7SJeff Kirsher 	u8 padding[PAD_SIZE];
719e13fbf7SJeff Kirsher 	struct sk_buff *skb;
729e13fbf7SJeff Kirsher };
739e13fbf7SJeff Kirsher 
749e13fbf7SJeff Kirsher struct sgiseeq_tx_desc {
759e13fbf7SJeff Kirsher 	volatile struct hpc_dma_desc tdma;
769e13fbf7SJeff Kirsher 	u8 padding[PAD_SIZE];
779e13fbf7SJeff Kirsher 	struct sk_buff *skb;
789e13fbf7SJeff Kirsher };
799e13fbf7SJeff Kirsher 
809e13fbf7SJeff Kirsher /*
819e13fbf7SJeff Kirsher  * Warning: This structure is laid out in a certain way because HPC dma
829e13fbf7SJeff Kirsher  *          descriptors must be 8-byte aligned.  So don't touch this without
839e13fbf7SJeff Kirsher  *          some care.
849e13fbf7SJeff Kirsher  */
859e13fbf7SJeff Kirsher struct sgiseeq_init_block { /* Note the name ;-) */
869e13fbf7SJeff Kirsher 	struct sgiseeq_rx_desc rxvector[SEEQ_RX_BUFFERS];
879e13fbf7SJeff Kirsher 	struct sgiseeq_tx_desc txvector[SEEQ_TX_BUFFERS];
889e13fbf7SJeff Kirsher };
899e13fbf7SJeff Kirsher 
909e13fbf7SJeff Kirsher struct sgiseeq_private {
919e13fbf7SJeff Kirsher 	struct sgiseeq_init_block *srings;
929e13fbf7SJeff Kirsher 	dma_addr_t srings_dma;
939e13fbf7SJeff Kirsher 
949e13fbf7SJeff Kirsher 	/* Ptrs to the descriptors in uncached space. */
959e13fbf7SJeff Kirsher 	struct sgiseeq_rx_desc *rx_desc;
969e13fbf7SJeff Kirsher 	struct sgiseeq_tx_desc *tx_desc;
979e13fbf7SJeff Kirsher 
989e13fbf7SJeff Kirsher 	char *name;
999e13fbf7SJeff Kirsher 	struct hpc3_ethregs *hregs;
1009e13fbf7SJeff Kirsher 	struct sgiseeq_regs *sregs;
1019e13fbf7SJeff Kirsher 
1029e13fbf7SJeff Kirsher 	/* Ring entry counters. */
1039e13fbf7SJeff Kirsher 	unsigned int rx_new, tx_new;
1049e13fbf7SJeff Kirsher 	unsigned int rx_old, tx_old;
1059e13fbf7SJeff Kirsher 
1069e13fbf7SJeff Kirsher 	int is_edlc;
1079e13fbf7SJeff Kirsher 	unsigned char control;
1089e13fbf7SJeff Kirsher 	unsigned char mode;
1099e13fbf7SJeff Kirsher 
1109e13fbf7SJeff Kirsher 	spinlock_t tx_lock;
1119e13fbf7SJeff Kirsher };
1129e13fbf7SJeff Kirsher 
dma_sync_desc_cpu(struct net_device * dev,void * addr)1139e13fbf7SJeff Kirsher static inline void dma_sync_desc_cpu(struct net_device *dev, void *addr)
1149e13fbf7SJeff Kirsher {
1157f3bb7f5SChristoph Hellwig 	struct sgiseeq_private *sp = netdev_priv(dev);
1167f3bb7f5SChristoph Hellwig 
1177f3bb7f5SChristoph Hellwig 	dma_sync_single_for_cpu(dev->dev.parent, VIRT_TO_DMA(sp, addr),
1187f3bb7f5SChristoph Hellwig 			sizeof(struct sgiseeq_rx_desc), DMA_BIDIRECTIONAL);
1199e13fbf7SJeff Kirsher }
1209e13fbf7SJeff Kirsher 
dma_sync_desc_dev(struct net_device * dev,void * addr)1219e13fbf7SJeff Kirsher static inline void dma_sync_desc_dev(struct net_device *dev, void *addr)
1229e13fbf7SJeff Kirsher {
1237f3bb7f5SChristoph Hellwig 	struct sgiseeq_private *sp = netdev_priv(dev);
1247f3bb7f5SChristoph Hellwig 
1257f3bb7f5SChristoph Hellwig 	dma_sync_single_for_device(dev->dev.parent, VIRT_TO_DMA(sp, addr),
1267f3bb7f5SChristoph Hellwig 			sizeof(struct sgiseeq_rx_desc), DMA_BIDIRECTIONAL);
1279e13fbf7SJeff Kirsher }
1289e13fbf7SJeff Kirsher 
hpc3_eth_reset(struct hpc3_ethregs * hregs)1299e13fbf7SJeff Kirsher static inline void hpc3_eth_reset(struct hpc3_ethregs *hregs)
1309e13fbf7SJeff Kirsher {
1319e13fbf7SJeff Kirsher 	hregs->reset = HPC3_ERST_CRESET | HPC3_ERST_CLRIRQ;
1329e13fbf7SJeff Kirsher 	udelay(20);
1339e13fbf7SJeff Kirsher 	hregs->reset = 0;
1349e13fbf7SJeff Kirsher }
1359e13fbf7SJeff Kirsher 
reset_hpc3_and_seeq(struct hpc3_ethregs * hregs,struct sgiseeq_regs * sregs)1369e13fbf7SJeff Kirsher static inline void reset_hpc3_and_seeq(struct hpc3_ethregs *hregs,
1379e13fbf7SJeff Kirsher 				       struct sgiseeq_regs *sregs)
1389e13fbf7SJeff Kirsher {
1399e13fbf7SJeff Kirsher 	hregs->rx_ctrl = hregs->tx_ctrl = 0;
1409e13fbf7SJeff Kirsher 	hpc3_eth_reset(hregs);
1419e13fbf7SJeff Kirsher }
1429e13fbf7SJeff Kirsher 
1439e13fbf7SJeff Kirsher #define RSTAT_GO_BITS (SEEQ_RCMD_IGOOD | SEEQ_RCMD_IEOF | SEEQ_RCMD_ISHORT | \
1449e13fbf7SJeff Kirsher 		       SEEQ_RCMD_IDRIB | SEEQ_RCMD_ICRC)
1459e13fbf7SJeff Kirsher 
seeq_go(struct sgiseeq_private * sp,struct hpc3_ethregs * hregs,struct sgiseeq_regs * sregs)1469e13fbf7SJeff Kirsher static inline void seeq_go(struct sgiseeq_private *sp,
1479e13fbf7SJeff Kirsher 			   struct hpc3_ethregs *hregs,
1489e13fbf7SJeff Kirsher 			   struct sgiseeq_regs *sregs)
1499e13fbf7SJeff Kirsher {
1509e13fbf7SJeff Kirsher 	sregs->rstat = sp->mode | RSTAT_GO_BITS;
1519e13fbf7SJeff Kirsher 	hregs->rx_ctrl = HPC3_ERXCTRL_ACTIVE;
1529e13fbf7SJeff Kirsher }
1539e13fbf7SJeff Kirsher 
__sgiseeq_set_mac_address(struct net_device * dev)1549e13fbf7SJeff Kirsher static inline void __sgiseeq_set_mac_address(struct net_device *dev)
1559e13fbf7SJeff Kirsher {
1569e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
1579e13fbf7SJeff Kirsher 	struct sgiseeq_regs *sregs = sp->sregs;
1589e13fbf7SJeff Kirsher 	int i;
1599e13fbf7SJeff Kirsher 
1609e13fbf7SJeff Kirsher 	sregs->tstat = SEEQ_TCMD_RB0;
1619e13fbf7SJeff Kirsher 	for (i = 0; i < 6; i++)
1629e13fbf7SJeff Kirsher 		sregs->rw.eth_addr[i] = dev->dev_addr[i];
1639e13fbf7SJeff Kirsher }
1649e13fbf7SJeff Kirsher 
sgiseeq_set_mac_address(struct net_device * dev,void * addr)1659e13fbf7SJeff Kirsher static int sgiseeq_set_mac_address(struct net_device *dev, void *addr)
1669e13fbf7SJeff Kirsher {
1679e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
1689e13fbf7SJeff Kirsher 	struct sockaddr *sa = addr;
1699e13fbf7SJeff Kirsher 
170*a05e4c0aSJakub Kicinski 	eth_hw_addr_set(dev, sa->sa_data);
1719e13fbf7SJeff Kirsher 
1729e13fbf7SJeff Kirsher 	spin_lock_irq(&sp->tx_lock);
1739e13fbf7SJeff Kirsher 	__sgiseeq_set_mac_address(dev);
1749e13fbf7SJeff Kirsher 	spin_unlock_irq(&sp->tx_lock);
1759e13fbf7SJeff Kirsher 
1769e13fbf7SJeff Kirsher 	return 0;
1779e13fbf7SJeff Kirsher }
1789e13fbf7SJeff Kirsher 
1799e13fbf7SJeff Kirsher #define TCNTINFO_INIT (HPCDMA_EOX | HPCDMA_ETXD)
1809e13fbf7SJeff Kirsher #define RCNTCFG_INIT  (HPCDMA_OWN | HPCDMA_EORP | HPCDMA_XIE)
1819e13fbf7SJeff Kirsher #define RCNTINFO_INIT (RCNTCFG_INIT | (PKT_BUF_SZ & HPCDMA_BCNT))
1829e13fbf7SJeff Kirsher 
seeq_init_ring(struct net_device * dev)1839e13fbf7SJeff Kirsher static int seeq_init_ring(struct net_device *dev)
1849e13fbf7SJeff Kirsher {
1859e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
1869e13fbf7SJeff Kirsher 	int i;
1879e13fbf7SJeff Kirsher 
1889e13fbf7SJeff Kirsher 	netif_stop_queue(dev);
1899e13fbf7SJeff Kirsher 	sp->rx_new = sp->tx_new = 0;
1909e13fbf7SJeff Kirsher 	sp->rx_old = sp->tx_old = 0;
1919e13fbf7SJeff Kirsher 
1929e13fbf7SJeff Kirsher 	__sgiseeq_set_mac_address(dev);
1939e13fbf7SJeff Kirsher 
1949e13fbf7SJeff Kirsher 	/* Setup tx ring. */
1959e13fbf7SJeff Kirsher 	for(i = 0; i < SEEQ_TX_BUFFERS; i++) {
1969e13fbf7SJeff Kirsher 		sp->tx_desc[i].tdma.cntinfo = TCNTINFO_INIT;
1979e13fbf7SJeff Kirsher 		dma_sync_desc_dev(dev, &sp->tx_desc[i]);
1989e13fbf7SJeff Kirsher 	}
1999e13fbf7SJeff Kirsher 
2009e13fbf7SJeff Kirsher 	/* And now the rx ring. */
2019e13fbf7SJeff Kirsher 	for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
2029e13fbf7SJeff Kirsher 		if (!sp->rx_desc[i].skb) {
2039e13fbf7SJeff Kirsher 			dma_addr_t dma_addr;
2049e13fbf7SJeff Kirsher 			struct sk_buff *skb = netdev_alloc_skb(dev, PKT_BUF_SZ);
2059e13fbf7SJeff Kirsher 
2069e13fbf7SJeff Kirsher 			if (skb == NULL)
2079e13fbf7SJeff Kirsher 				return -ENOMEM;
2089e13fbf7SJeff Kirsher 			skb_reserve(skb, 2);
2099e13fbf7SJeff Kirsher 			dma_addr = dma_map_single(dev->dev.parent,
2109e13fbf7SJeff Kirsher 						  skb->data - 2,
2119e13fbf7SJeff Kirsher 						  PKT_BUF_SZ, DMA_FROM_DEVICE);
2129e13fbf7SJeff Kirsher 			sp->rx_desc[i].skb = skb;
2139e13fbf7SJeff Kirsher 			sp->rx_desc[i].rdma.pbuf = dma_addr;
2149e13fbf7SJeff Kirsher 		}
2159e13fbf7SJeff Kirsher 		sp->rx_desc[i].rdma.cntinfo = RCNTINFO_INIT;
2169e13fbf7SJeff Kirsher 		dma_sync_desc_dev(dev, &sp->rx_desc[i]);
2179e13fbf7SJeff Kirsher 	}
2189e13fbf7SJeff Kirsher 	sp->rx_desc[i - 1].rdma.cntinfo |= HPCDMA_EOR;
2199e13fbf7SJeff Kirsher 	dma_sync_desc_dev(dev, &sp->rx_desc[i - 1]);
2209e13fbf7SJeff Kirsher 	return 0;
2219e13fbf7SJeff Kirsher }
2229e13fbf7SJeff Kirsher 
seeq_purge_ring(struct net_device * dev)2239e13fbf7SJeff Kirsher static void seeq_purge_ring(struct net_device *dev)
2249e13fbf7SJeff Kirsher {
2259e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
2269e13fbf7SJeff Kirsher 	int i;
2279e13fbf7SJeff Kirsher 
2289e13fbf7SJeff Kirsher 	/* clear tx ring. */
2299e13fbf7SJeff Kirsher 	for (i = 0; i < SEEQ_TX_BUFFERS; i++) {
2309e13fbf7SJeff Kirsher 		if (sp->tx_desc[i].skb) {
2319e13fbf7SJeff Kirsher 			dev_kfree_skb(sp->tx_desc[i].skb);
2329e13fbf7SJeff Kirsher 			sp->tx_desc[i].skb = NULL;
2339e13fbf7SJeff Kirsher 		}
2349e13fbf7SJeff Kirsher 	}
2359e13fbf7SJeff Kirsher 
2369e13fbf7SJeff Kirsher 	/* And now the rx ring. */
2379e13fbf7SJeff Kirsher 	for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
2389e13fbf7SJeff Kirsher 		if (sp->rx_desc[i].skb) {
2399e13fbf7SJeff Kirsher 			dev_kfree_skb(sp->rx_desc[i].skb);
2409e13fbf7SJeff Kirsher 			sp->rx_desc[i].skb = NULL;
2419e13fbf7SJeff Kirsher 		}
2429e13fbf7SJeff Kirsher 	}
2439e13fbf7SJeff Kirsher }
2449e13fbf7SJeff Kirsher 
2459e13fbf7SJeff Kirsher #ifdef DEBUG
2469e13fbf7SJeff Kirsher static struct sgiseeq_private *gpriv;
2479e13fbf7SJeff Kirsher static struct net_device *gdev;
2489e13fbf7SJeff Kirsher 
sgiseeq_dump_rings(void)2499e13fbf7SJeff Kirsher static void sgiseeq_dump_rings(void)
2509e13fbf7SJeff Kirsher {
2519e13fbf7SJeff Kirsher 	static int once;
2529e13fbf7SJeff Kirsher 	struct sgiseeq_rx_desc *r = gpriv->rx_desc;
2539e13fbf7SJeff Kirsher 	struct sgiseeq_tx_desc *t = gpriv->tx_desc;
2549e13fbf7SJeff Kirsher 	struct hpc3_ethregs *hregs = gpriv->hregs;
2559e13fbf7SJeff Kirsher 	int i;
2569e13fbf7SJeff Kirsher 
2579e13fbf7SJeff Kirsher 	if (once)
2589e13fbf7SJeff Kirsher 		return;
2599e13fbf7SJeff Kirsher 	once++;
2609e13fbf7SJeff Kirsher 	printk("RING DUMP:\n");
2619e13fbf7SJeff Kirsher 	for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
2629e13fbf7SJeff Kirsher 		printk("RX [%d]: @(%p) [%08x,%08x,%08x] ",
2639e13fbf7SJeff Kirsher 		       i, (&r[i]), r[i].rdma.pbuf, r[i].rdma.cntinfo,
2649e13fbf7SJeff Kirsher 		       r[i].rdma.pnext);
2659e13fbf7SJeff Kirsher 		i += 1;
2669e13fbf7SJeff Kirsher 		printk("-- [%d]: @(%p) [%08x,%08x,%08x]\n",
2679e13fbf7SJeff Kirsher 		       i, (&r[i]), r[i].rdma.pbuf, r[i].rdma.cntinfo,
2689e13fbf7SJeff Kirsher 		       r[i].rdma.pnext);
2699e13fbf7SJeff Kirsher 	}
2709e13fbf7SJeff Kirsher 	for (i = 0; i < SEEQ_TX_BUFFERS; i++) {
2719e13fbf7SJeff Kirsher 		printk("TX [%d]: @(%p) [%08x,%08x,%08x] ",
2729e13fbf7SJeff Kirsher 		       i, (&t[i]), t[i].tdma.pbuf, t[i].tdma.cntinfo,
2739e13fbf7SJeff Kirsher 		       t[i].tdma.pnext);
2749e13fbf7SJeff Kirsher 		i += 1;
2759e13fbf7SJeff Kirsher 		printk("-- [%d]: @(%p) [%08x,%08x,%08x]\n",
2769e13fbf7SJeff Kirsher 		       i, (&t[i]), t[i].tdma.pbuf, t[i].tdma.cntinfo,
2779e13fbf7SJeff Kirsher 		       t[i].tdma.pnext);
2789e13fbf7SJeff Kirsher 	}
2799e13fbf7SJeff Kirsher 	printk("INFO: [rx_new = %d rx_old=%d] [tx_new = %d tx_old = %d]\n",
2809e13fbf7SJeff Kirsher 	       gpriv->rx_new, gpriv->rx_old, gpriv->tx_new, gpriv->tx_old);
2819e13fbf7SJeff Kirsher 	printk("RREGS: rx_cbptr[%08x] rx_ndptr[%08x] rx_ctrl[%08x]\n",
2829e13fbf7SJeff Kirsher 	       hregs->rx_cbptr, hregs->rx_ndptr, hregs->rx_ctrl);
2839e13fbf7SJeff Kirsher 	printk("TREGS: tx_cbptr[%08x] tx_ndptr[%08x] tx_ctrl[%08x]\n",
2849e13fbf7SJeff Kirsher 	       hregs->tx_cbptr, hregs->tx_ndptr, hregs->tx_ctrl);
2859e13fbf7SJeff Kirsher }
2869e13fbf7SJeff Kirsher #endif
2879e13fbf7SJeff Kirsher 
2889e13fbf7SJeff Kirsher #define TSTAT_INIT_SEEQ (SEEQ_TCMD_IPT|SEEQ_TCMD_I16|SEEQ_TCMD_IC|SEEQ_TCMD_IUF)
2899e13fbf7SJeff Kirsher #define TSTAT_INIT_EDLC ((TSTAT_INIT_SEEQ) | SEEQ_TCMD_RB2)
2909e13fbf7SJeff Kirsher 
init_seeq(struct net_device * dev,struct sgiseeq_private * sp,struct sgiseeq_regs * sregs)2919e13fbf7SJeff Kirsher static int init_seeq(struct net_device *dev, struct sgiseeq_private *sp,
2929e13fbf7SJeff Kirsher 		     struct sgiseeq_regs *sregs)
2939e13fbf7SJeff Kirsher {
2949e13fbf7SJeff Kirsher 	struct hpc3_ethregs *hregs = sp->hregs;
2959e13fbf7SJeff Kirsher 	int err;
2969e13fbf7SJeff Kirsher 
2979e13fbf7SJeff Kirsher 	reset_hpc3_and_seeq(hregs, sregs);
2989e13fbf7SJeff Kirsher 	err = seeq_init_ring(dev);
2999e13fbf7SJeff Kirsher 	if (err)
3009e13fbf7SJeff Kirsher 		return err;
3019e13fbf7SJeff Kirsher 
3029e13fbf7SJeff Kirsher 	/* Setup to field the proper interrupt types. */
3039e13fbf7SJeff Kirsher 	if (sp->is_edlc) {
3049e13fbf7SJeff Kirsher 		sregs->tstat = TSTAT_INIT_EDLC;
3059e13fbf7SJeff Kirsher 		sregs->rw.wregs.control = sp->control;
3069e13fbf7SJeff Kirsher 		sregs->rw.wregs.frame_gap = 0;
3079e13fbf7SJeff Kirsher 	} else {
3089e13fbf7SJeff Kirsher 		sregs->tstat = TSTAT_INIT_SEEQ;
3099e13fbf7SJeff Kirsher 	}
3109e13fbf7SJeff Kirsher 
3119e13fbf7SJeff Kirsher 	hregs->rx_ndptr = VIRT_TO_DMA(sp, sp->rx_desc);
3129e13fbf7SJeff Kirsher 	hregs->tx_ndptr = VIRT_TO_DMA(sp, sp->tx_desc);
3139e13fbf7SJeff Kirsher 
3149e13fbf7SJeff Kirsher 	seeq_go(sp, hregs, sregs);
3159e13fbf7SJeff Kirsher 	return 0;
3169e13fbf7SJeff Kirsher }
3179e13fbf7SJeff Kirsher 
record_rx_errors(struct net_device * dev,unsigned char status)3189e13fbf7SJeff Kirsher static void record_rx_errors(struct net_device *dev, unsigned char status)
3199e13fbf7SJeff Kirsher {
3209e13fbf7SJeff Kirsher 	if (status & SEEQ_RSTAT_OVERF ||
3219e13fbf7SJeff Kirsher 	    status & SEEQ_RSTAT_SFRAME)
3229e13fbf7SJeff Kirsher 		dev->stats.rx_over_errors++;
3239e13fbf7SJeff Kirsher 	if (status & SEEQ_RSTAT_CERROR)
3249e13fbf7SJeff Kirsher 		dev->stats.rx_crc_errors++;
3259e13fbf7SJeff Kirsher 	if (status & SEEQ_RSTAT_DERROR)
3269e13fbf7SJeff Kirsher 		dev->stats.rx_frame_errors++;
3279e13fbf7SJeff Kirsher 	if (status & SEEQ_RSTAT_REOF)
3289e13fbf7SJeff Kirsher 		dev->stats.rx_errors++;
3299e13fbf7SJeff Kirsher }
3309e13fbf7SJeff Kirsher 
rx_maybe_restart(struct sgiseeq_private * sp,struct hpc3_ethregs * hregs,struct sgiseeq_regs * sregs)3319e13fbf7SJeff Kirsher static inline void rx_maybe_restart(struct sgiseeq_private *sp,
3329e13fbf7SJeff Kirsher 				    struct hpc3_ethregs *hregs,
3339e13fbf7SJeff Kirsher 				    struct sgiseeq_regs *sregs)
3349e13fbf7SJeff Kirsher {
3359e13fbf7SJeff Kirsher 	if (!(hregs->rx_ctrl & HPC3_ERXCTRL_ACTIVE)) {
3369e13fbf7SJeff Kirsher 		hregs->rx_ndptr = VIRT_TO_DMA(sp, sp->rx_desc + sp->rx_new);
3379e13fbf7SJeff Kirsher 		seeq_go(sp, hregs, sregs);
3389e13fbf7SJeff Kirsher 	}
3399e13fbf7SJeff Kirsher }
3409e13fbf7SJeff Kirsher 
sgiseeq_rx(struct net_device * dev,struct sgiseeq_private * sp,struct hpc3_ethregs * hregs,struct sgiseeq_regs * sregs)3419e13fbf7SJeff Kirsher static inline void sgiseeq_rx(struct net_device *dev, struct sgiseeq_private *sp,
3429e13fbf7SJeff Kirsher 			      struct hpc3_ethregs *hregs,
3439e13fbf7SJeff Kirsher 			      struct sgiseeq_regs *sregs)
3449e13fbf7SJeff Kirsher {
3459e13fbf7SJeff Kirsher 	struct sgiseeq_rx_desc *rd;
3469e13fbf7SJeff Kirsher 	struct sk_buff *skb = NULL;
3479e13fbf7SJeff Kirsher 	struct sk_buff *newskb;
3489e13fbf7SJeff Kirsher 	unsigned char pkt_status;
3499e13fbf7SJeff Kirsher 	int len = 0;
3509e13fbf7SJeff Kirsher 	unsigned int orig_end = PREV_RX(sp->rx_new);
3519e13fbf7SJeff Kirsher 
3529e13fbf7SJeff Kirsher 	/* Service every received packet. */
3539e13fbf7SJeff Kirsher 	rd = &sp->rx_desc[sp->rx_new];
3549e13fbf7SJeff Kirsher 	dma_sync_desc_cpu(dev, rd);
3559e13fbf7SJeff Kirsher 	while (!(rd->rdma.cntinfo & HPCDMA_OWN)) {
3569e13fbf7SJeff Kirsher 		len = PKT_BUF_SZ - (rd->rdma.cntinfo & HPCDMA_BCNT) - 3;
3579e13fbf7SJeff Kirsher 		dma_unmap_single(dev->dev.parent, rd->rdma.pbuf,
3589e13fbf7SJeff Kirsher 				 PKT_BUF_SZ, DMA_FROM_DEVICE);
3599e13fbf7SJeff Kirsher 		pkt_status = rd->skb->data[len];
3609e13fbf7SJeff Kirsher 		if (pkt_status & SEEQ_RSTAT_FIG) {
3619e13fbf7SJeff Kirsher 			/* Packet is OK. */
3629e13fbf7SJeff Kirsher 			/* We don't want to receive our own packets */
3639116d7b0Sdingtianhong 			if (!ether_addr_equal(rd->skb->data + 6, dev->dev_addr)) {
3649e13fbf7SJeff Kirsher 				if (len > rx_copybreak) {
3659e13fbf7SJeff Kirsher 					skb = rd->skb;
3669e13fbf7SJeff Kirsher 					newskb = netdev_alloc_skb(dev, PKT_BUF_SZ);
3679e13fbf7SJeff Kirsher 					if (!newskb) {
3689e13fbf7SJeff Kirsher 						newskb = skb;
3699e13fbf7SJeff Kirsher 						skb = NULL;
3709e13fbf7SJeff Kirsher 						goto memory_squeeze;
3719e13fbf7SJeff Kirsher 					}
3729e13fbf7SJeff Kirsher 					skb_reserve(newskb, 2);
3739e13fbf7SJeff Kirsher 				} else {
3749e13fbf7SJeff Kirsher 					skb = netdev_alloc_skb_ip_align(dev, len);
3759e13fbf7SJeff Kirsher 					if (skb)
3769e13fbf7SJeff Kirsher 						skb_copy_to_linear_data(skb, rd->skb->data, len);
3779e13fbf7SJeff Kirsher 
3789e13fbf7SJeff Kirsher 					newskb = rd->skb;
3799e13fbf7SJeff Kirsher 				}
3809e13fbf7SJeff Kirsher memory_squeeze:
3819e13fbf7SJeff Kirsher 				if (skb) {
3829e13fbf7SJeff Kirsher 					skb_put(skb, len);
3839e13fbf7SJeff Kirsher 					skb->protocol = eth_type_trans(skb, dev);
3849e13fbf7SJeff Kirsher 					netif_rx(skb);
3859e13fbf7SJeff Kirsher 					dev->stats.rx_packets++;
3869e13fbf7SJeff Kirsher 					dev->stats.rx_bytes += len;
3879e13fbf7SJeff Kirsher 				} else {
3889e13fbf7SJeff Kirsher 					dev->stats.rx_dropped++;
3899e13fbf7SJeff Kirsher 				}
3909e13fbf7SJeff Kirsher 			} else {
3919e13fbf7SJeff Kirsher 				/* Silently drop my own packets */
3929e13fbf7SJeff Kirsher 				newskb = rd->skb;
3939e13fbf7SJeff Kirsher 			}
3949e13fbf7SJeff Kirsher 		} else {
3959e13fbf7SJeff Kirsher 			record_rx_errors(dev, pkt_status);
3969e13fbf7SJeff Kirsher 			newskb = rd->skb;
3979e13fbf7SJeff Kirsher 		}
3989e13fbf7SJeff Kirsher 		rd->skb = newskb;
3999e13fbf7SJeff Kirsher 		rd->rdma.pbuf = dma_map_single(dev->dev.parent,
4009e13fbf7SJeff Kirsher 					       newskb->data - 2,
4019e13fbf7SJeff Kirsher 					       PKT_BUF_SZ, DMA_FROM_DEVICE);
4029e13fbf7SJeff Kirsher 
4039e13fbf7SJeff Kirsher 		/* Return the entry to the ring pool. */
4049e13fbf7SJeff Kirsher 		rd->rdma.cntinfo = RCNTINFO_INIT;
4059e13fbf7SJeff Kirsher 		sp->rx_new = NEXT_RX(sp->rx_new);
4069e13fbf7SJeff Kirsher 		dma_sync_desc_dev(dev, rd);
4079e13fbf7SJeff Kirsher 		rd = &sp->rx_desc[sp->rx_new];
4089e13fbf7SJeff Kirsher 		dma_sync_desc_cpu(dev, rd);
4099e13fbf7SJeff Kirsher 	}
4107f3bb7f5SChristoph Hellwig 	dma_sync_desc_dev(dev, rd);
4117f3bb7f5SChristoph Hellwig 
4129e13fbf7SJeff Kirsher 	dma_sync_desc_cpu(dev, &sp->rx_desc[orig_end]);
4139e13fbf7SJeff Kirsher 	sp->rx_desc[orig_end].rdma.cntinfo &= ~(HPCDMA_EOR);
4149e13fbf7SJeff Kirsher 	dma_sync_desc_dev(dev, &sp->rx_desc[orig_end]);
4159e13fbf7SJeff Kirsher 	dma_sync_desc_cpu(dev, &sp->rx_desc[PREV_RX(sp->rx_new)]);
4169e13fbf7SJeff Kirsher 	sp->rx_desc[PREV_RX(sp->rx_new)].rdma.cntinfo |= HPCDMA_EOR;
4179e13fbf7SJeff Kirsher 	dma_sync_desc_dev(dev, &sp->rx_desc[PREV_RX(sp->rx_new)]);
4189e13fbf7SJeff Kirsher 	rx_maybe_restart(sp, hregs, sregs);
4199e13fbf7SJeff Kirsher }
4209e13fbf7SJeff Kirsher 
tx_maybe_reset_collisions(struct sgiseeq_private * sp,struct sgiseeq_regs * sregs)4219e13fbf7SJeff Kirsher static inline void tx_maybe_reset_collisions(struct sgiseeq_private *sp,
4229e13fbf7SJeff Kirsher 					     struct sgiseeq_regs *sregs)
4239e13fbf7SJeff Kirsher {
4249e13fbf7SJeff Kirsher 	if (sp->is_edlc) {
4259e13fbf7SJeff Kirsher 		sregs->rw.wregs.control = sp->control & ~(SEEQ_CTRL_XCNT);
4269e13fbf7SJeff Kirsher 		sregs->rw.wregs.control = sp->control;
4279e13fbf7SJeff Kirsher 	}
4289e13fbf7SJeff Kirsher }
4299e13fbf7SJeff Kirsher 
kick_tx(struct net_device * dev,struct sgiseeq_private * sp,struct hpc3_ethregs * hregs)4309e13fbf7SJeff Kirsher static inline void kick_tx(struct net_device *dev,
4319e13fbf7SJeff Kirsher 			   struct sgiseeq_private *sp,
4329e13fbf7SJeff Kirsher 			   struct hpc3_ethregs *hregs)
4339e13fbf7SJeff Kirsher {
4349e13fbf7SJeff Kirsher 	struct sgiseeq_tx_desc *td;
4359e13fbf7SJeff Kirsher 	int i = sp->tx_old;
4369e13fbf7SJeff Kirsher 
4379e13fbf7SJeff Kirsher 	/* If the HPC aint doin nothin, and there are more packets
4389e13fbf7SJeff Kirsher 	 * with ETXD cleared and XIU set we must make very certain
4399e13fbf7SJeff Kirsher 	 * that we restart the HPC else we risk locking up the
4409e13fbf7SJeff Kirsher 	 * adapter.  The following code is only safe iff the HPCDMA
4419e13fbf7SJeff Kirsher 	 * is not active!
4429e13fbf7SJeff Kirsher 	 */
4439e13fbf7SJeff Kirsher 	td = &sp->tx_desc[i];
4449e13fbf7SJeff Kirsher 	dma_sync_desc_cpu(dev, td);
4459e13fbf7SJeff Kirsher 	while ((td->tdma.cntinfo & (HPCDMA_XIU | HPCDMA_ETXD)) ==
4469e13fbf7SJeff Kirsher 	      (HPCDMA_XIU | HPCDMA_ETXD)) {
4479e13fbf7SJeff Kirsher 		i = NEXT_TX(i);
4489e13fbf7SJeff Kirsher 		td = &sp->tx_desc[i];
4499e13fbf7SJeff Kirsher 		dma_sync_desc_cpu(dev, td);
4509e13fbf7SJeff Kirsher 	}
4519e13fbf7SJeff Kirsher 	if (td->tdma.cntinfo & HPCDMA_XIU) {
4527f3bb7f5SChristoph Hellwig 		dma_sync_desc_dev(dev, td);
4539e13fbf7SJeff Kirsher 		hregs->tx_ndptr = VIRT_TO_DMA(sp, td);
4549e13fbf7SJeff Kirsher 		hregs->tx_ctrl = HPC3_ETXCTRL_ACTIVE;
4559e13fbf7SJeff Kirsher 	}
4569e13fbf7SJeff Kirsher }
4579e13fbf7SJeff Kirsher 
sgiseeq_tx(struct net_device * dev,struct sgiseeq_private * sp,struct hpc3_ethregs * hregs,struct sgiseeq_regs * sregs)4589e13fbf7SJeff Kirsher static inline void sgiseeq_tx(struct net_device *dev, struct sgiseeq_private *sp,
4599e13fbf7SJeff Kirsher 			      struct hpc3_ethregs *hregs,
4609e13fbf7SJeff Kirsher 			      struct sgiseeq_regs *sregs)
4619e13fbf7SJeff Kirsher {
4629e13fbf7SJeff Kirsher 	struct sgiseeq_tx_desc *td;
4639e13fbf7SJeff Kirsher 	unsigned long status = hregs->tx_ctrl;
4649e13fbf7SJeff Kirsher 	int j;
4659e13fbf7SJeff Kirsher 
4669e13fbf7SJeff Kirsher 	tx_maybe_reset_collisions(sp, sregs);
4679e13fbf7SJeff Kirsher 
4689e13fbf7SJeff Kirsher 	if (!(status & (HPC3_ETXCTRL_ACTIVE | SEEQ_TSTAT_PTRANS))) {
4699e13fbf7SJeff Kirsher 		/* Oops, HPC detected some sort of error. */
4709e13fbf7SJeff Kirsher 		if (status & SEEQ_TSTAT_R16)
4719e13fbf7SJeff Kirsher 			dev->stats.tx_aborted_errors++;
4729e13fbf7SJeff Kirsher 		if (status & SEEQ_TSTAT_UFLOW)
4739e13fbf7SJeff Kirsher 			dev->stats.tx_fifo_errors++;
4749e13fbf7SJeff Kirsher 		if (status & SEEQ_TSTAT_LCLS)
4759e13fbf7SJeff Kirsher 			dev->stats.collisions++;
4769e13fbf7SJeff Kirsher 	}
4779e13fbf7SJeff Kirsher 
4789e13fbf7SJeff Kirsher 	/* Ack 'em... */
4799e13fbf7SJeff Kirsher 	for (j = sp->tx_old; j != sp->tx_new; j = NEXT_TX(j)) {
4809e13fbf7SJeff Kirsher 		td = &sp->tx_desc[j];
4819e13fbf7SJeff Kirsher 
4829e13fbf7SJeff Kirsher 		dma_sync_desc_cpu(dev, td);
4839e13fbf7SJeff Kirsher 		if (!(td->tdma.cntinfo & (HPCDMA_XIU)))
4849e13fbf7SJeff Kirsher 			break;
4859e13fbf7SJeff Kirsher 		if (!(td->tdma.cntinfo & (HPCDMA_ETXD))) {
4867f3bb7f5SChristoph Hellwig 			dma_sync_desc_dev(dev, td);
4879e13fbf7SJeff Kirsher 			if (!(status & HPC3_ETXCTRL_ACTIVE)) {
4889e13fbf7SJeff Kirsher 				hregs->tx_ndptr = VIRT_TO_DMA(sp, td);
4899e13fbf7SJeff Kirsher 				hregs->tx_ctrl = HPC3_ETXCTRL_ACTIVE;
4909e13fbf7SJeff Kirsher 			}
4919e13fbf7SJeff Kirsher 			break;
4929e13fbf7SJeff Kirsher 		}
4939e13fbf7SJeff Kirsher 		dev->stats.tx_packets++;
4949e13fbf7SJeff Kirsher 		sp->tx_old = NEXT_TX(sp->tx_old);
4959e13fbf7SJeff Kirsher 		td->tdma.cntinfo &= ~(HPCDMA_XIU | HPCDMA_XIE);
4969e13fbf7SJeff Kirsher 		td->tdma.cntinfo |= HPCDMA_EOX;
4979e13fbf7SJeff Kirsher 		if (td->skb) {
4989e13fbf7SJeff Kirsher 			dev_kfree_skb_any(td->skb);
4999e13fbf7SJeff Kirsher 			td->skb = NULL;
5009e13fbf7SJeff Kirsher 		}
5019e13fbf7SJeff Kirsher 		dma_sync_desc_dev(dev, td);
5029e13fbf7SJeff Kirsher 	}
5039e13fbf7SJeff Kirsher }
5049e13fbf7SJeff Kirsher 
sgiseeq_interrupt(int irq,void * dev_id)5059e13fbf7SJeff Kirsher static irqreturn_t sgiseeq_interrupt(int irq, void *dev_id)
5069e13fbf7SJeff Kirsher {
5079e13fbf7SJeff Kirsher 	struct net_device *dev = (struct net_device *) dev_id;
5089e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
5099e13fbf7SJeff Kirsher 	struct hpc3_ethregs *hregs = sp->hregs;
5109e13fbf7SJeff Kirsher 	struct sgiseeq_regs *sregs = sp->sregs;
5119e13fbf7SJeff Kirsher 
5129e13fbf7SJeff Kirsher 	spin_lock(&sp->tx_lock);
5139e13fbf7SJeff Kirsher 
5149e13fbf7SJeff Kirsher 	/* Ack the IRQ and set software state. */
5159e13fbf7SJeff Kirsher 	hregs->reset = HPC3_ERST_CLRIRQ;
5169e13fbf7SJeff Kirsher 
5179e13fbf7SJeff Kirsher 	/* Always check for received packets. */
5189e13fbf7SJeff Kirsher 	sgiseeq_rx(dev, sp, hregs, sregs);
5199e13fbf7SJeff Kirsher 
5209e13fbf7SJeff Kirsher 	/* Only check for tx acks if we have something queued. */
5219e13fbf7SJeff Kirsher 	if (sp->tx_old != sp->tx_new)
5229e13fbf7SJeff Kirsher 		sgiseeq_tx(dev, sp, hregs, sregs);
5239e13fbf7SJeff Kirsher 
5249e13fbf7SJeff Kirsher 	if ((TX_BUFFS_AVAIL(sp) > 0) && netif_queue_stopped(dev)) {
5259e13fbf7SJeff Kirsher 		netif_wake_queue(dev);
5269e13fbf7SJeff Kirsher 	}
5279e13fbf7SJeff Kirsher 	spin_unlock(&sp->tx_lock);
5289e13fbf7SJeff Kirsher 
5299e13fbf7SJeff Kirsher 	return IRQ_HANDLED;
5309e13fbf7SJeff Kirsher }
5319e13fbf7SJeff Kirsher 
sgiseeq_open(struct net_device * dev)5329e13fbf7SJeff Kirsher static int sgiseeq_open(struct net_device *dev)
5339e13fbf7SJeff Kirsher {
5349e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
5359e13fbf7SJeff Kirsher 	struct sgiseeq_regs *sregs = sp->sregs;
5369e13fbf7SJeff Kirsher 	unsigned int irq = dev->irq;
5379e13fbf7SJeff Kirsher 	int err;
5389e13fbf7SJeff Kirsher 
5399e13fbf7SJeff Kirsher 	if (request_irq(irq, sgiseeq_interrupt, 0, sgiseeqstr, dev)) {
5409e13fbf7SJeff Kirsher 		printk(KERN_ERR "Seeq8003: Can't get irq %d\n", dev->irq);
5419e13fbf7SJeff Kirsher 		return -EAGAIN;
5429e13fbf7SJeff Kirsher 	}
5439e13fbf7SJeff Kirsher 
5449e13fbf7SJeff Kirsher 	err = init_seeq(dev, sp, sregs);
5459e13fbf7SJeff Kirsher 	if (err)
5469e13fbf7SJeff Kirsher 		goto out_free_irq;
5479e13fbf7SJeff Kirsher 
5489e13fbf7SJeff Kirsher 	netif_start_queue(dev);
5499e13fbf7SJeff Kirsher 
5509e13fbf7SJeff Kirsher 	return 0;
5519e13fbf7SJeff Kirsher 
5529e13fbf7SJeff Kirsher out_free_irq:
5539e13fbf7SJeff Kirsher 	free_irq(irq, dev);
5549e13fbf7SJeff Kirsher 
5559e13fbf7SJeff Kirsher 	return err;
5569e13fbf7SJeff Kirsher }
5579e13fbf7SJeff Kirsher 
sgiseeq_close(struct net_device * dev)5589e13fbf7SJeff Kirsher static int sgiseeq_close(struct net_device *dev)
5599e13fbf7SJeff Kirsher {
5609e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
5619e13fbf7SJeff Kirsher 	struct sgiseeq_regs *sregs = sp->sregs;
5629e13fbf7SJeff Kirsher 	unsigned int irq = dev->irq;
5639e13fbf7SJeff Kirsher 
5649e13fbf7SJeff Kirsher 	netif_stop_queue(dev);
5659e13fbf7SJeff Kirsher 
5669e13fbf7SJeff Kirsher 	/* Shutdown the Seeq. */
5679e13fbf7SJeff Kirsher 	reset_hpc3_and_seeq(sp->hregs, sregs);
5689e13fbf7SJeff Kirsher 	free_irq(irq, dev);
5699e13fbf7SJeff Kirsher 	seeq_purge_ring(dev);
5709e13fbf7SJeff Kirsher 
5719e13fbf7SJeff Kirsher 	return 0;
5729e13fbf7SJeff Kirsher }
5739e13fbf7SJeff Kirsher 
sgiseeq_reset(struct net_device * dev)5749e13fbf7SJeff Kirsher static inline int sgiseeq_reset(struct net_device *dev)
5759e13fbf7SJeff Kirsher {
5769e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
5779e13fbf7SJeff Kirsher 	struct sgiseeq_regs *sregs = sp->sregs;
5789e13fbf7SJeff Kirsher 	int err;
5799e13fbf7SJeff Kirsher 
5809e13fbf7SJeff Kirsher 	err = init_seeq(dev, sp, sregs);
5819e13fbf7SJeff Kirsher 	if (err)
5829e13fbf7SJeff Kirsher 		return err;
5839e13fbf7SJeff Kirsher 
584860e9538SFlorian Westphal 	netif_trans_update(dev); /* prevent tx timeout */
5859e13fbf7SJeff Kirsher 	netif_wake_queue(dev);
5869e13fbf7SJeff Kirsher 
5879e13fbf7SJeff Kirsher 	return 0;
5889e13fbf7SJeff Kirsher }
5899e13fbf7SJeff Kirsher 
59072b46279SYueHaibing static netdev_tx_t
sgiseeq_start_xmit(struct sk_buff * skb,struct net_device * dev)59172b46279SYueHaibing sgiseeq_start_xmit(struct sk_buff *skb, struct net_device *dev)
5929e13fbf7SJeff Kirsher {
5939e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
5949e13fbf7SJeff Kirsher 	struct hpc3_ethregs *hregs = sp->hregs;
5959e13fbf7SJeff Kirsher 	unsigned long flags;
5969e13fbf7SJeff Kirsher 	struct sgiseeq_tx_desc *td;
5979e13fbf7SJeff Kirsher 	int len, entry;
5989e13fbf7SJeff Kirsher 
5999e13fbf7SJeff Kirsher 	spin_lock_irqsave(&sp->tx_lock, flags);
6009e13fbf7SJeff Kirsher 
6019e13fbf7SJeff Kirsher 	/* Setup... */
6029e13fbf7SJeff Kirsher 	len = skb->len;
6039e13fbf7SJeff Kirsher 	if (len < ETH_ZLEN) {
6049e13fbf7SJeff Kirsher 		if (skb_padto(skb, ETH_ZLEN)) {
6059e13fbf7SJeff Kirsher 			spin_unlock_irqrestore(&sp->tx_lock, flags);
6069e13fbf7SJeff Kirsher 			return NETDEV_TX_OK;
6079e13fbf7SJeff Kirsher 		}
6089e13fbf7SJeff Kirsher 		len = ETH_ZLEN;
6099e13fbf7SJeff Kirsher 	}
6109e13fbf7SJeff Kirsher 
6119e13fbf7SJeff Kirsher 	dev->stats.tx_bytes += len;
6129e13fbf7SJeff Kirsher 	entry = sp->tx_new;
6139e13fbf7SJeff Kirsher 	td = &sp->tx_desc[entry];
6149e13fbf7SJeff Kirsher 	dma_sync_desc_cpu(dev, td);
6159e13fbf7SJeff Kirsher 
6169e13fbf7SJeff Kirsher 	/* Create entry.  There are so many races with adding a new
6179e13fbf7SJeff Kirsher 	 * descriptor to the chain:
6189e13fbf7SJeff Kirsher 	 * 1) Assume that the HPC is off processing a DMA chain while
6199e13fbf7SJeff Kirsher 	 *    we are changing all of the following.
6209e13fbf7SJeff Kirsher 	 * 2) Do no allow the HPC to look at a new descriptor until
6219e13fbf7SJeff Kirsher 	 *    we have completely set up it's state.  This means, do
6229e13fbf7SJeff Kirsher 	 *    not clear HPCDMA_EOX in the current last descritptor
6239e13fbf7SJeff Kirsher 	 *    until the one we are adding looks consistent and could
6249e13fbf7SJeff Kirsher 	 *    be processes right now.
6259e13fbf7SJeff Kirsher 	 * 3) The tx interrupt code must notice when we've added a new
6269e13fbf7SJeff Kirsher 	 *    entry and the HPC got to the end of the chain before we
6279e13fbf7SJeff Kirsher 	 *    added this new entry and restarted it.
6289e13fbf7SJeff Kirsher 	 */
6299e13fbf7SJeff Kirsher 	td->skb = skb;
6309e13fbf7SJeff Kirsher 	td->tdma.pbuf = dma_map_single(dev->dev.parent, skb->data,
6319e13fbf7SJeff Kirsher 				       len, DMA_TO_DEVICE);
6329e13fbf7SJeff Kirsher 	td->tdma.cntinfo = (len & HPCDMA_BCNT) |
6339e13fbf7SJeff Kirsher 	                   HPCDMA_XIU | HPCDMA_EOXP | HPCDMA_XIE | HPCDMA_EOX;
6349e13fbf7SJeff Kirsher 	dma_sync_desc_dev(dev, td);
6359e13fbf7SJeff Kirsher 	if (sp->tx_old != sp->tx_new) {
6369e13fbf7SJeff Kirsher 		struct sgiseeq_tx_desc *backend;
6379e13fbf7SJeff Kirsher 
6389e13fbf7SJeff Kirsher 		backend = &sp->tx_desc[PREV_TX(sp->tx_new)];
6399e13fbf7SJeff Kirsher 		dma_sync_desc_cpu(dev, backend);
6409e13fbf7SJeff Kirsher 		backend->tdma.cntinfo &= ~HPCDMA_EOX;
6419e13fbf7SJeff Kirsher 		dma_sync_desc_dev(dev, backend);
6429e13fbf7SJeff Kirsher 	}
6439e13fbf7SJeff Kirsher 	sp->tx_new = NEXT_TX(sp->tx_new); /* Advance. */
6449e13fbf7SJeff Kirsher 
6459e13fbf7SJeff Kirsher 	/* Maybe kick the HPC back into motion. */
6469e13fbf7SJeff Kirsher 	if (!(hregs->tx_ctrl & HPC3_ETXCTRL_ACTIVE))
6479e13fbf7SJeff Kirsher 		kick_tx(dev, sp, hregs);
6489e13fbf7SJeff Kirsher 
6499e13fbf7SJeff Kirsher 	if (!TX_BUFFS_AVAIL(sp))
6509e13fbf7SJeff Kirsher 		netif_stop_queue(dev);
6519e13fbf7SJeff Kirsher 	spin_unlock_irqrestore(&sp->tx_lock, flags);
6529e13fbf7SJeff Kirsher 
6539e13fbf7SJeff Kirsher 	return NETDEV_TX_OK;
6549e13fbf7SJeff Kirsher }
6559e13fbf7SJeff Kirsher 
timeout(struct net_device * dev,unsigned int txqueue)6560290bd29SMichael S. Tsirkin static void timeout(struct net_device *dev, unsigned int txqueue)
6579e13fbf7SJeff Kirsher {
6589e13fbf7SJeff Kirsher 	printk(KERN_NOTICE "%s: transmit timed out, resetting\n", dev->name);
6599e13fbf7SJeff Kirsher 	sgiseeq_reset(dev);
6609e13fbf7SJeff Kirsher 
661860e9538SFlorian Westphal 	netif_trans_update(dev); /* prevent tx timeout */
6629e13fbf7SJeff Kirsher 	netif_wake_queue(dev);
6639e13fbf7SJeff Kirsher }
6649e13fbf7SJeff Kirsher 
sgiseeq_set_multicast(struct net_device * dev)6659e13fbf7SJeff Kirsher static void sgiseeq_set_multicast(struct net_device *dev)
6669e13fbf7SJeff Kirsher {
6679e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
6689e13fbf7SJeff Kirsher 	unsigned char oldmode = sp->mode;
6699e13fbf7SJeff Kirsher 
6709e13fbf7SJeff Kirsher 	if(dev->flags & IFF_PROMISC)
6719e13fbf7SJeff Kirsher 		sp->mode = SEEQ_RCMD_RANY;
6729e13fbf7SJeff Kirsher 	else if ((dev->flags & IFF_ALLMULTI) || !netdev_mc_empty(dev))
6739e13fbf7SJeff Kirsher 		sp->mode = SEEQ_RCMD_RBMCAST;
6749e13fbf7SJeff Kirsher 	else
6759e13fbf7SJeff Kirsher 		sp->mode = SEEQ_RCMD_RBCAST;
6769e13fbf7SJeff Kirsher 
6779e13fbf7SJeff Kirsher 	/* XXX I know this sucks, but is there a better way to reprogram
6789e13fbf7SJeff Kirsher 	 * XXX the receiver? At least, this shouldn't happen too often.
6799e13fbf7SJeff Kirsher 	 */
6809e13fbf7SJeff Kirsher 
6819e13fbf7SJeff Kirsher 	if (oldmode != sp->mode)
6829e13fbf7SJeff Kirsher 		sgiseeq_reset(dev);
6839e13fbf7SJeff Kirsher }
6849e13fbf7SJeff Kirsher 
setup_tx_ring(struct net_device * dev,struct sgiseeq_tx_desc * buf,int nbufs)6859e13fbf7SJeff Kirsher static inline void setup_tx_ring(struct net_device *dev,
6869e13fbf7SJeff Kirsher 				 struct sgiseeq_tx_desc *buf,
6879e13fbf7SJeff Kirsher 				 int nbufs)
6889e13fbf7SJeff Kirsher {
6899e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
6909e13fbf7SJeff Kirsher 	int i = 0;
6919e13fbf7SJeff Kirsher 
6929e13fbf7SJeff Kirsher 	while (i < (nbufs - 1)) {
6939e13fbf7SJeff Kirsher 		buf[i].tdma.pnext = VIRT_TO_DMA(sp, buf + i + 1);
6949e13fbf7SJeff Kirsher 		buf[i].tdma.pbuf = 0;
6959e13fbf7SJeff Kirsher 		dma_sync_desc_dev(dev, &buf[i]);
6969e13fbf7SJeff Kirsher 		i++;
6979e13fbf7SJeff Kirsher 	}
6989e13fbf7SJeff Kirsher 	buf[i].tdma.pnext = VIRT_TO_DMA(sp, buf);
6999e13fbf7SJeff Kirsher 	dma_sync_desc_dev(dev, &buf[i]);
7009e13fbf7SJeff Kirsher }
7019e13fbf7SJeff Kirsher 
setup_rx_ring(struct net_device * dev,struct sgiseeq_rx_desc * buf,int nbufs)7029e13fbf7SJeff Kirsher static inline void setup_rx_ring(struct net_device *dev,
7039e13fbf7SJeff Kirsher 				 struct sgiseeq_rx_desc *buf,
7049e13fbf7SJeff Kirsher 				 int nbufs)
7059e13fbf7SJeff Kirsher {
7069e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
7079e13fbf7SJeff Kirsher 	int i = 0;
7089e13fbf7SJeff Kirsher 
7099e13fbf7SJeff Kirsher 	while (i < (nbufs - 1)) {
7109e13fbf7SJeff Kirsher 		buf[i].rdma.pnext = VIRT_TO_DMA(sp, buf + i + 1);
7119e13fbf7SJeff Kirsher 		buf[i].rdma.pbuf = 0;
7129e13fbf7SJeff Kirsher 		dma_sync_desc_dev(dev, &buf[i]);
7139e13fbf7SJeff Kirsher 		i++;
7149e13fbf7SJeff Kirsher 	}
7159e13fbf7SJeff Kirsher 	buf[i].rdma.pbuf = 0;
7169e13fbf7SJeff Kirsher 	buf[i].rdma.pnext = VIRT_TO_DMA(sp, buf);
7179e13fbf7SJeff Kirsher 	dma_sync_desc_dev(dev, &buf[i]);
7189e13fbf7SJeff Kirsher }
7199e13fbf7SJeff Kirsher 
7209e13fbf7SJeff Kirsher static const struct net_device_ops sgiseeq_netdev_ops = {
7219e13fbf7SJeff Kirsher 	.ndo_open		= sgiseeq_open,
7229e13fbf7SJeff Kirsher 	.ndo_stop		= sgiseeq_close,
7239e13fbf7SJeff Kirsher 	.ndo_start_xmit		= sgiseeq_start_xmit,
7249e13fbf7SJeff Kirsher 	.ndo_tx_timeout		= timeout,
725afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= sgiseeq_set_multicast,
7269e13fbf7SJeff Kirsher 	.ndo_set_mac_address	= sgiseeq_set_mac_address,
7279e13fbf7SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
7289e13fbf7SJeff Kirsher };
7299e13fbf7SJeff Kirsher 
sgiseeq_probe(struct platform_device * pdev)7305911ce0dSBill Pemberton static int sgiseeq_probe(struct platform_device *pdev)
7319e13fbf7SJeff Kirsher {
732d7765428SJingoo Han 	struct sgiseeq_platform_data *pd = dev_get_platdata(&pdev->dev);
7339e13fbf7SJeff Kirsher 	struct hpc3_regs *hpcregs = pd->hpc;
7349e13fbf7SJeff Kirsher 	struct sgiseeq_init_block *sr;
7359e13fbf7SJeff Kirsher 	unsigned int irq = pd->irq;
7369e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp;
7379e13fbf7SJeff Kirsher 	struct net_device *dev;
7389e13fbf7SJeff Kirsher 	int err;
7399e13fbf7SJeff Kirsher 
7409e13fbf7SJeff Kirsher 	dev = alloc_etherdev(sizeof (struct sgiseeq_private));
7419e13fbf7SJeff Kirsher 	if (!dev) {
7429e13fbf7SJeff Kirsher 		err = -ENOMEM;
7439e13fbf7SJeff Kirsher 		goto err_out;
7449e13fbf7SJeff Kirsher 	}
7459e13fbf7SJeff Kirsher 
7469e13fbf7SJeff Kirsher 	platform_set_drvdata(pdev, dev);
7475afcd14cSThomas Bogendoerfer 	SET_NETDEV_DEV(dev, &pdev->dev);
7489e13fbf7SJeff Kirsher 	sp = netdev_priv(dev);
7499e13fbf7SJeff Kirsher 
7509e13fbf7SJeff Kirsher 	/* Make private data page aligned */
7517f3bb7f5SChristoph Hellwig 	sr = dma_alloc_noncoherent(&pdev->dev, sizeof(*sp->srings),
7527f3bb7f5SChristoph Hellwig 			&sp->srings_dma, DMA_BIDIRECTIONAL, GFP_KERNEL);
7539e13fbf7SJeff Kirsher 	if (!sr) {
7549e13fbf7SJeff Kirsher 		printk(KERN_ERR "Sgiseeq: Page alloc failed, aborting.\n");
7559e13fbf7SJeff Kirsher 		err = -ENOMEM;
7569e13fbf7SJeff Kirsher 		goto err_out_free_dev;
7579e13fbf7SJeff Kirsher 	}
7589e13fbf7SJeff Kirsher 	sp->srings = sr;
7599e13fbf7SJeff Kirsher 	sp->rx_desc = sp->srings->rxvector;
7609e13fbf7SJeff Kirsher 	sp->tx_desc = sp->srings->txvector;
761b8dfc6a0SJean Delvare 	spin_lock_init(&sp->tx_lock);
7629e13fbf7SJeff Kirsher 
7639e13fbf7SJeff Kirsher 	/* A couple calculations now, saves many cycles later. */
7649e13fbf7SJeff Kirsher 	setup_rx_ring(dev, sp->rx_desc, SEEQ_RX_BUFFERS);
7659e13fbf7SJeff Kirsher 	setup_tx_ring(dev, sp->tx_desc, SEEQ_TX_BUFFERS);
7669e13fbf7SJeff Kirsher 
767a96d317fSJakub Kicinski 	eth_hw_addr_set(dev, pd->mac);
7689e13fbf7SJeff Kirsher 
7699e13fbf7SJeff Kirsher #ifdef DEBUG
7709e13fbf7SJeff Kirsher 	gpriv = sp;
7719e13fbf7SJeff Kirsher 	gdev = dev;
7729e13fbf7SJeff Kirsher #endif
7739e13fbf7SJeff Kirsher 	sp->sregs = (struct sgiseeq_regs *) &hpcregs->eth_ext[0];
7749e13fbf7SJeff Kirsher 	sp->hregs = &hpcregs->ethregs;
7759e13fbf7SJeff Kirsher 	sp->name = sgiseeqstr;
7769e13fbf7SJeff Kirsher 	sp->mode = SEEQ_RCMD_RBCAST;
7779e13fbf7SJeff Kirsher 
7789e13fbf7SJeff Kirsher 	/* Setup PIO and DMA transfer timing */
7799e13fbf7SJeff Kirsher 	sp->hregs->pconfig = 0x161;
7809e13fbf7SJeff Kirsher 	sp->hregs->dconfig = HPC3_EDCFG_FIRQ | HPC3_EDCFG_FEOP |
7819e13fbf7SJeff Kirsher 			     HPC3_EDCFG_FRXDC | HPC3_EDCFG_PTO | 0x026;
7829e13fbf7SJeff Kirsher 
7839e13fbf7SJeff Kirsher 	/* Setup PIO and DMA transfer timing */
7849e13fbf7SJeff Kirsher 	sp->hregs->pconfig = 0x161;
7859e13fbf7SJeff Kirsher 	sp->hregs->dconfig = HPC3_EDCFG_FIRQ | HPC3_EDCFG_FEOP |
7869e13fbf7SJeff Kirsher 			     HPC3_EDCFG_FRXDC | HPC3_EDCFG_PTO | 0x026;
7879e13fbf7SJeff Kirsher 
7889e13fbf7SJeff Kirsher 	/* Reset the chip. */
7899e13fbf7SJeff Kirsher 	hpc3_eth_reset(sp->hregs);
7909e13fbf7SJeff Kirsher 
7919e13fbf7SJeff Kirsher 	sp->is_edlc = !(sp->sregs->rw.rregs.collision_tx[0] & 0xff);
7929e13fbf7SJeff Kirsher 	if (sp->is_edlc)
7939e13fbf7SJeff Kirsher 		sp->control = SEEQ_CTRL_XCNT | SEEQ_CTRL_ACCNT |
7949e13fbf7SJeff Kirsher 			      SEEQ_CTRL_SFLAG | SEEQ_CTRL_ESHORT |
7959e13fbf7SJeff Kirsher 			      SEEQ_CTRL_ENCARR;
7969e13fbf7SJeff Kirsher 
7979e13fbf7SJeff Kirsher 	dev->netdev_ops		= &sgiseeq_netdev_ops;
7989e13fbf7SJeff Kirsher 	dev->watchdog_timeo	= (200 * HZ) / 1000;
7999e13fbf7SJeff Kirsher 	dev->irq		= irq;
8009e13fbf7SJeff Kirsher 
8019e13fbf7SJeff Kirsher 	if (register_netdev(dev)) {
8029e13fbf7SJeff Kirsher 		printk(KERN_ERR "Sgiseeq: Cannot register net device, "
8039e13fbf7SJeff Kirsher 		       "aborting.\n");
8049e13fbf7SJeff Kirsher 		err = -ENODEV;
805e1e54ec7SChristophe JAILLET 		goto err_out_free_attrs;
8069e13fbf7SJeff Kirsher 	}
8079e13fbf7SJeff Kirsher 
8089e13fbf7SJeff Kirsher 	printk(KERN_INFO "%s: %s %pM\n", dev->name, sgiseeqstr, dev->dev_addr);
8099e13fbf7SJeff Kirsher 
8109e13fbf7SJeff Kirsher 	return 0;
8119e13fbf7SJeff Kirsher 
812e1e54ec7SChristophe JAILLET err_out_free_attrs:
8137f3bb7f5SChristoph Hellwig 	dma_free_noncoherent(&pdev->dev, sizeof(*sp->srings), sp->srings,
8147f3bb7f5SChristoph Hellwig 		       sp->srings_dma, DMA_BIDIRECTIONAL);
8159e13fbf7SJeff Kirsher err_out_free_dev:
8169e13fbf7SJeff Kirsher 	free_netdev(dev);
8179e13fbf7SJeff Kirsher 
8189e13fbf7SJeff Kirsher err_out:
8199e13fbf7SJeff Kirsher 	return err;
8209e13fbf7SJeff Kirsher }
8219e13fbf7SJeff Kirsher 
sgiseeq_remove(struct platform_device * pdev)822be12502eSDmitry Torokhov static int sgiseeq_remove(struct platform_device *pdev)
8239e13fbf7SJeff Kirsher {
8249e13fbf7SJeff Kirsher 	struct net_device *dev = platform_get_drvdata(pdev);
8259e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
8269e13fbf7SJeff Kirsher 
8279e13fbf7SJeff Kirsher 	unregister_netdev(dev);
8287f3bb7f5SChristoph Hellwig 	dma_free_noncoherent(&pdev->dev, sizeof(*sp->srings), sp->srings,
8297f3bb7f5SChristoph Hellwig 		       sp->srings_dma, DMA_BIDIRECTIONAL);
8309e13fbf7SJeff Kirsher 	free_netdev(dev);
8319e13fbf7SJeff Kirsher 
8329e13fbf7SJeff Kirsher 	return 0;
8339e13fbf7SJeff Kirsher }
8349e13fbf7SJeff Kirsher 
8359e13fbf7SJeff Kirsher static struct platform_driver sgiseeq_driver = {
8369e13fbf7SJeff Kirsher 	.probe	= sgiseeq_probe,
837be12502eSDmitry Torokhov 	.remove	= sgiseeq_remove,
8389e13fbf7SJeff Kirsher 	.driver = {
8399e13fbf7SJeff Kirsher 		.name	= "sgiseeq",
8409e13fbf7SJeff Kirsher 	}
8419e13fbf7SJeff Kirsher };
8429e13fbf7SJeff Kirsher 
843db62f684SAxel Lin module_platform_driver(sgiseeq_driver);
8449e13fbf7SJeff Kirsher 
8459e13fbf7SJeff Kirsher MODULE_DESCRIPTION("SGI Seeq 8003 driver");
8469e13fbf7SJeff Kirsher MODULE_AUTHOR("Linux/MIPS Mailing List <linux-mips@linux-mips.org>");
8479e13fbf7SJeff Kirsher MODULE_LICENSE("GPL");
8489e13fbf7SJeff Kirsher MODULE_ALIAS("platform:sgiseeq");
849