xref: /openbmc/linux/drivers/net/ethernet/seeq/sgiseeq.c (revision 9e13fbf7af3cb044f365e8df9c0e9277715cfc7c)
1*9e13fbf7SJeff Kirsher /*
2*9e13fbf7SJeff Kirsher  * sgiseeq.c: Seeq8003 ethernet driver for SGI machines.
3*9e13fbf7SJeff Kirsher  *
4*9e13fbf7SJeff Kirsher  * Copyright (C) 1996 David S. Miller (davem@davemloft.net)
5*9e13fbf7SJeff Kirsher  */
6*9e13fbf7SJeff Kirsher 
7*9e13fbf7SJeff Kirsher #undef DEBUG
8*9e13fbf7SJeff Kirsher 
9*9e13fbf7SJeff Kirsher #include <linux/dma-mapping.h>
10*9e13fbf7SJeff Kirsher #include <linux/kernel.h>
11*9e13fbf7SJeff Kirsher #include <linux/module.h>
12*9e13fbf7SJeff Kirsher #include <linux/slab.h>
13*9e13fbf7SJeff Kirsher #include <linux/errno.h>
14*9e13fbf7SJeff Kirsher #include <linux/init.h>
15*9e13fbf7SJeff Kirsher #include <linux/types.h>
16*9e13fbf7SJeff Kirsher #include <linux/interrupt.h>
17*9e13fbf7SJeff Kirsher #include <linux/string.h>
18*9e13fbf7SJeff Kirsher #include <linux/delay.h>
19*9e13fbf7SJeff Kirsher #include <linux/netdevice.h>
20*9e13fbf7SJeff Kirsher #include <linux/platform_device.h>
21*9e13fbf7SJeff Kirsher #include <linux/etherdevice.h>
22*9e13fbf7SJeff Kirsher #include <linux/skbuff.h>
23*9e13fbf7SJeff Kirsher 
24*9e13fbf7SJeff Kirsher #include <asm/sgi/hpc3.h>
25*9e13fbf7SJeff Kirsher #include <asm/sgi/ip22.h>
26*9e13fbf7SJeff Kirsher #include <asm/sgi/seeq.h>
27*9e13fbf7SJeff Kirsher 
28*9e13fbf7SJeff Kirsher #include "sgiseeq.h"
29*9e13fbf7SJeff Kirsher 
30*9e13fbf7SJeff Kirsher static char *sgiseeqstr = "SGI Seeq8003";
31*9e13fbf7SJeff Kirsher 
32*9e13fbf7SJeff Kirsher /*
33*9e13fbf7SJeff Kirsher  * If you want speed, you do something silly, it always has worked for me.  So,
34*9e13fbf7SJeff Kirsher  * with that in mind, I've decided to make this driver look completely like a
35*9e13fbf7SJeff Kirsher  * stupid Lance from a driver architecture perspective.  Only difference is that
36*9e13fbf7SJeff Kirsher  * here our "ring buffer" looks and acts like a real Lance one does but is
37*9e13fbf7SJeff Kirsher  * laid out like how the HPC DMA and the Seeq want it to.  You'd be surprised
38*9e13fbf7SJeff Kirsher  * how a stupid idea like this can pay off in performance, not to mention
39*9e13fbf7SJeff Kirsher  * making this driver 2,000 times easier to write. ;-)
40*9e13fbf7SJeff Kirsher  */
41*9e13fbf7SJeff Kirsher 
42*9e13fbf7SJeff Kirsher /* Tune these if we tend to run out often etc. */
43*9e13fbf7SJeff Kirsher #define SEEQ_RX_BUFFERS  16
44*9e13fbf7SJeff Kirsher #define SEEQ_TX_BUFFERS  16
45*9e13fbf7SJeff Kirsher 
46*9e13fbf7SJeff Kirsher #define PKT_BUF_SZ       1584
47*9e13fbf7SJeff Kirsher 
48*9e13fbf7SJeff Kirsher #define NEXT_RX(i)  (((i) + 1) & (SEEQ_RX_BUFFERS - 1))
49*9e13fbf7SJeff Kirsher #define NEXT_TX(i)  (((i) + 1) & (SEEQ_TX_BUFFERS - 1))
50*9e13fbf7SJeff Kirsher #define PREV_RX(i)  (((i) - 1) & (SEEQ_RX_BUFFERS - 1))
51*9e13fbf7SJeff Kirsher #define PREV_TX(i)  (((i) - 1) & (SEEQ_TX_BUFFERS - 1))
52*9e13fbf7SJeff Kirsher 
53*9e13fbf7SJeff Kirsher #define TX_BUFFS_AVAIL(sp) ((sp->tx_old <= sp->tx_new) ? \
54*9e13fbf7SJeff Kirsher 			    sp->tx_old + (SEEQ_TX_BUFFERS - 1) - sp->tx_new : \
55*9e13fbf7SJeff Kirsher 			    sp->tx_old - sp->tx_new - 1)
56*9e13fbf7SJeff Kirsher 
57*9e13fbf7SJeff Kirsher #define VIRT_TO_DMA(sp, v) ((sp)->srings_dma +                                 \
58*9e13fbf7SJeff Kirsher 				  (dma_addr_t)((unsigned long)(v) -            \
59*9e13fbf7SJeff Kirsher 					       (unsigned long)((sp)->rx_desc)))
60*9e13fbf7SJeff Kirsher 
61*9e13fbf7SJeff Kirsher /* Copy frames shorter than rx_copybreak, otherwise pass on up in
62*9e13fbf7SJeff Kirsher  * a full sized sk_buff.  Value of 100 stolen from tulip.c (!alpha).
63*9e13fbf7SJeff Kirsher  */
64*9e13fbf7SJeff Kirsher static int rx_copybreak = 100;
65*9e13fbf7SJeff Kirsher 
66*9e13fbf7SJeff Kirsher #define PAD_SIZE    (128 - sizeof(struct hpc_dma_desc) - sizeof(void *))
67*9e13fbf7SJeff Kirsher 
68*9e13fbf7SJeff Kirsher struct sgiseeq_rx_desc {
69*9e13fbf7SJeff Kirsher 	volatile struct hpc_dma_desc rdma;
70*9e13fbf7SJeff Kirsher 	u8 padding[PAD_SIZE];
71*9e13fbf7SJeff Kirsher 	struct sk_buff *skb;
72*9e13fbf7SJeff Kirsher };
73*9e13fbf7SJeff Kirsher 
74*9e13fbf7SJeff Kirsher struct sgiseeq_tx_desc {
75*9e13fbf7SJeff Kirsher 	volatile struct hpc_dma_desc tdma;
76*9e13fbf7SJeff Kirsher 	u8 padding[PAD_SIZE];
77*9e13fbf7SJeff Kirsher 	struct sk_buff *skb;
78*9e13fbf7SJeff Kirsher };
79*9e13fbf7SJeff Kirsher 
80*9e13fbf7SJeff Kirsher /*
81*9e13fbf7SJeff Kirsher  * Warning: This structure is laid out in a certain way because HPC dma
82*9e13fbf7SJeff Kirsher  *          descriptors must be 8-byte aligned.  So don't touch this without
83*9e13fbf7SJeff Kirsher  *          some care.
84*9e13fbf7SJeff Kirsher  */
85*9e13fbf7SJeff Kirsher struct sgiseeq_init_block { /* Note the name ;-) */
86*9e13fbf7SJeff Kirsher 	struct sgiseeq_rx_desc rxvector[SEEQ_RX_BUFFERS];
87*9e13fbf7SJeff Kirsher 	struct sgiseeq_tx_desc txvector[SEEQ_TX_BUFFERS];
88*9e13fbf7SJeff Kirsher };
89*9e13fbf7SJeff Kirsher 
90*9e13fbf7SJeff Kirsher struct sgiseeq_private {
91*9e13fbf7SJeff Kirsher 	struct sgiseeq_init_block *srings;
92*9e13fbf7SJeff Kirsher 	dma_addr_t srings_dma;
93*9e13fbf7SJeff Kirsher 
94*9e13fbf7SJeff Kirsher 	/* Ptrs to the descriptors in uncached space. */
95*9e13fbf7SJeff Kirsher 	struct sgiseeq_rx_desc *rx_desc;
96*9e13fbf7SJeff Kirsher 	struct sgiseeq_tx_desc *tx_desc;
97*9e13fbf7SJeff Kirsher 
98*9e13fbf7SJeff Kirsher 	char *name;
99*9e13fbf7SJeff Kirsher 	struct hpc3_ethregs *hregs;
100*9e13fbf7SJeff Kirsher 	struct sgiseeq_regs *sregs;
101*9e13fbf7SJeff Kirsher 
102*9e13fbf7SJeff Kirsher 	/* Ring entry counters. */
103*9e13fbf7SJeff Kirsher 	unsigned int rx_new, tx_new;
104*9e13fbf7SJeff Kirsher 	unsigned int rx_old, tx_old;
105*9e13fbf7SJeff Kirsher 
106*9e13fbf7SJeff Kirsher 	int is_edlc;
107*9e13fbf7SJeff Kirsher 	unsigned char control;
108*9e13fbf7SJeff Kirsher 	unsigned char mode;
109*9e13fbf7SJeff Kirsher 
110*9e13fbf7SJeff Kirsher 	spinlock_t tx_lock;
111*9e13fbf7SJeff Kirsher };
112*9e13fbf7SJeff Kirsher 
113*9e13fbf7SJeff Kirsher static inline void dma_sync_desc_cpu(struct net_device *dev, void *addr)
114*9e13fbf7SJeff Kirsher {
115*9e13fbf7SJeff Kirsher 	dma_cache_sync(dev->dev.parent, addr, sizeof(struct sgiseeq_rx_desc),
116*9e13fbf7SJeff Kirsher 		       DMA_FROM_DEVICE);
117*9e13fbf7SJeff Kirsher }
118*9e13fbf7SJeff Kirsher 
119*9e13fbf7SJeff Kirsher static inline void dma_sync_desc_dev(struct net_device *dev, void *addr)
120*9e13fbf7SJeff Kirsher {
121*9e13fbf7SJeff Kirsher 	dma_cache_sync(dev->dev.parent, addr, sizeof(struct sgiseeq_rx_desc),
122*9e13fbf7SJeff Kirsher 		       DMA_TO_DEVICE);
123*9e13fbf7SJeff Kirsher }
124*9e13fbf7SJeff Kirsher 
125*9e13fbf7SJeff Kirsher static inline void hpc3_eth_reset(struct hpc3_ethregs *hregs)
126*9e13fbf7SJeff Kirsher {
127*9e13fbf7SJeff Kirsher 	hregs->reset = HPC3_ERST_CRESET | HPC3_ERST_CLRIRQ;
128*9e13fbf7SJeff Kirsher 	udelay(20);
129*9e13fbf7SJeff Kirsher 	hregs->reset = 0;
130*9e13fbf7SJeff Kirsher }
131*9e13fbf7SJeff Kirsher 
132*9e13fbf7SJeff Kirsher static inline void reset_hpc3_and_seeq(struct hpc3_ethregs *hregs,
133*9e13fbf7SJeff Kirsher 				       struct sgiseeq_regs *sregs)
134*9e13fbf7SJeff Kirsher {
135*9e13fbf7SJeff Kirsher 	hregs->rx_ctrl = hregs->tx_ctrl = 0;
136*9e13fbf7SJeff Kirsher 	hpc3_eth_reset(hregs);
137*9e13fbf7SJeff Kirsher }
138*9e13fbf7SJeff Kirsher 
139*9e13fbf7SJeff Kirsher #define RSTAT_GO_BITS (SEEQ_RCMD_IGOOD | SEEQ_RCMD_IEOF | SEEQ_RCMD_ISHORT | \
140*9e13fbf7SJeff Kirsher 		       SEEQ_RCMD_IDRIB | SEEQ_RCMD_ICRC)
141*9e13fbf7SJeff Kirsher 
142*9e13fbf7SJeff Kirsher static inline void seeq_go(struct sgiseeq_private *sp,
143*9e13fbf7SJeff Kirsher 			   struct hpc3_ethregs *hregs,
144*9e13fbf7SJeff Kirsher 			   struct sgiseeq_regs *sregs)
145*9e13fbf7SJeff Kirsher {
146*9e13fbf7SJeff Kirsher 	sregs->rstat = sp->mode | RSTAT_GO_BITS;
147*9e13fbf7SJeff Kirsher 	hregs->rx_ctrl = HPC3_ERXCTRL_ACTIVE;
148*9e13fbf7SJeff Kirsher }
149*9e13fbf7SJeff Kirsher 
150*9e13fbf7SJeff Kirsher static inline void __sgiseeq_set_mac_address(struct net_device *dev)
151*9e13fbf7SJeff Kirsher {
152*9e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
153*9e13fbf7SJeff Kirsher 	struct sgiseeq_regs *sregs = sp->sregs;
154*9e13fbf7SJeff Kirsher 	int i;
155*9e13fbf7SJeff Kirsher 
156*9e13fbf7SJeff Kirsher 	sregs->tstat = SEEQ_TCMD_RB0;
157*9e13fbf7SJeff Kirsher 	for (i = 0; i < 6; i++)
158*9e13fbf7SJeff Kirsher 		sregs->rw.eth_addr[i] = dev->dev_addr[i];
159*9e13fbf7SJeff Kirsher }
160*9e13fbf7SJeff Kirsher 
161*9e13fbf7SJeff Kirsher static int sgiseeq_set_mac_address(struct net_device *dev, void *addr)
162*9e13fbf7SJeff Kirsher {
163*9e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
164*9e13fbf7SJeff Kirsher 	struct sockaddr *sa = addr;
165*9e13fbf7SJeff Kirsher 
166*9e13fbf7SJeff Kirsher 	memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
167*9e13fbf7SJeff Kirsher 
168*9e13fbf7SJeff Kirsher 	spin_lock_irq(&sp->tx_lock);
169*9e13fbf7SJeff Kirsher 	__sgiseeq_set_mac_address(dev);
170*9e13fbf7SJeff Kirsher 	spin_unlock_irq(&sp->tx_lock);
171*9e13fbf7SJeff Kirsher 
172*9e13fbf7SJeff Kirsher 	return 0;
173*9e13fbf7SJeff Kirsher }
174*9e13fbf7SJeff Kirsher 
175*9e13fbf7SJeff Kirsher #define TCNTINFO_INIT (HPCDMA_EOX | HPCDMA_ETXD)
176*9e13fbf7SJeff Kirsher #define RCNTCFG_INIT  (HPCDMA_OWN | HPCDMA_EORP | HPCDMA_XIE)
177*9e13fbf7SJeff Kirsher #define RCNTINFO_INIT (RCNTCFG_INIT | (PKT_BUF_SZ & HPCDMA_BCNT))
178*9e13fbf7SJeff Kirsher 
179*9e13fbf7SJeff Kirsher static int seeq_init_ring(struct net_device *dev)
180*9e13fbf7SJeff Kirsher {
181*9e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
182*9e13fbf7SJeff Kirsher 	int i;
183*9e13fbf7SJeff Kirsher 
184*9e13fbf7SJeff Kirsher 	netif_stop_queue(dev);
185*9e13fbf7SJeff Kirsher 	sp->rx_new = sp->tx_new = 0;
186*9e13fbf7SJeff Kirsher 	sp->rx_old = sp->tx_old = 0;
187*9e13fbf7SJeff Kirsher 
188*9e13fbf7SJeff Kirsher 	__sgiseeq_set_mac_address(dev);
189*9e13fbf7SJeff Kirsher 
190*9e13fbf7SJeff Kirsher 	/* Setup tx ring. */
191*9e13fbf7SJeff Kirsher 	for(i = 0; i < SEEQ_TX_BUFFERS; i++) {
192*9e13fbf7SJeff Kirsher 		sp->tx_desc[i].tdma.cntinfo = TCNTINFO_INIT;
193*9e13fbf7SJeff Kirsher 		dma_sync_desc_dev(dev, &sp->tx_desc[i]);
194*9e13fbf7SJeff Kirsher 	}
195*9e13fbf7SJeff Kirsher 
196*9e13fbf7SJeff Kirsher 	/* And now the rx ring. */
197*9e13fbf7SJeff Kirsher 	for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
198*9e13fbf7SJeff Kirsher 		if (!sp->rx_desc[i].skb) {
199*9e13fbf7SJeff Kirsher 			dma_addr_t dma_addr;
200*9e13fbf7SJeff Kirsher 			struct sk_buff *skb = netdev_alloc_skb(dev, PKT_BUF_SZ);
201*9e13fbf7SJeff Kirsher 
202*9e13fbf7SJeff Kirsher 			if (skb == NULL)
203*9e13fbf7SJeff Kirsher 				return -ENOMEM;
204*9e13fbf7SJeff Kirsher 			skb_reserve(skb, 2);
205*9e13fbf7SJeff Kirsher 			dma_addr = dma_map_single(dev->dev.parent,
206*9e13fbf7SJeff Kirsher 						  skb->data - 2,
207*9e13fbf7SJeff Kirsher 						  PKT_BUF_SZ, DMA_FROM_DEVICE);
208*9e13fbf7SJeff Kirsher 			sp->rx_desc[i].skb = skb;
209*9e13fbf7SJeff Kirsher 			sp->rx_desc[i].rdma.pbuf = dma_addr;
210*9e13fbf7SJeff Kirsher 		}
211*9e13fbf7SJeff Kirsher 		sp->rx_desc[i].rdma.cntinfo = RCNTINFO_INIT;
212*9e13fbf7SJeff Kirsher 		dma_sync_desc_dev(dev, &sp->rx_desc[i]);
213*9e13fbf7SJeff Kirsher 	}
214*9e13fbf7SJeff Kirsher 	sp->rx_desc[i - 1].rdma.cntinfo |= HPCDMA_EOR;
215*9e13fbf7SJeff Kirsher 	dma_sync_desc_dev(dev, &sp->rx_desc[i - 1]);
216*9e13fbf7SJeff Kirsher 	return 0;
217*9e13fbf7SJeff Kirsher }
218*9e13fbf7SJeff Kirsher 
219*9e13fbf7SJeff Kirsher static void seeq_purge_ring(struct net_device *dev)
220*9e13fbf7SJeff Kirsher {
221*9e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
222*9e13fbf7SJeff Kirsher 	int i;
223*9e13fbf7SJeff Kirsher 
224*9e13fbf7SJeff Kirsher 	/* clear tx ring. */
225*9e13fbf7SJeff Kirsher 	for (i = 0; i < SEEQ_TX_BUFFERS; i++) {
226*9e13fbf7SJeff Kirsher 		if (sp->tx_desc[i].skb) {
227*9e13fbf7SJeff Kirsher 			dev_kfree_skb(sp->tx_desc[i].skb);
228*9e13fbf7SJeff Kirsher 			sp->tx_desc[i].skb = NULL;
229*9e13fbf7SJeff Kirsher 		}
230*9e13fbf7SJeff Kirsher 	}
231*9e13fbf7SJeff Kirsher 
232*9e13fbf7SJeff Kirsher 	/* And now the rx ring. */
233*9e13fbf7SJeff Kirsher 	for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
234*9e13fbf7SJeff Kirsher 		if (sp->rx_desc[i].skb) {
235*9e13fbf7SJeff Kirsher 			dev_kfree_skb(sp->rx_desc[i].skb);
236*9e13fbf7SJeff Kirsher 			sp->rx_desc[i].skb = NULL;
237*9e13fbf7SJeff Kirsher 		}
238*9e13fbf7SJeff Kirsher 	}
239*9e13fbf7SJeff Kirsher }
240*9e13fbf7SJeff Kirsher 
241*9e13fbf7SJeff Kirsher #ifdef DEBUG
242*9e13fbf7SJeff Kirsher static struct sgiseeq_private *gpriv;
243*9e13fbf7SJeff Kirsher static struct net_device *gdev;
244*9e13fbf7SJeff Kirsher 
245*9e13fbf7SJeff Kirsher static void sgiseeq_dump_rings(void)
246*9e13fbf7SJeff Kirsher {
247*9e13fbf7SJeff Kirsher 	static int once;
248*9e13fbf7SJeff Kirsher 	struct sgiseeq_rx_desc *r = gpriv->rx_desc;
249*9e13fbf7SJeff Kirsher 	struct sgiseeq_tx_desc *t = gpriv->tx_desc;
250*9e13fbf7SJeff Kirsher 	struct hpc3_ethregs *hregs = gpriv->hregs;
251*9e13fbf7SJeff Kirsher 	int i;
252*9e13fbf7SJeff Kirsher 
253*9e13fbf7SJeff Kirsher 	if (once)
254*9e13fbf7SJeff Kirsher 		return;
255*9e13fbf7SJeff Kirsher 	once++;
256*9e13fbf7SJeff Kirsher 	printk("RING DUMP:\n");
257*9e13fbf7SJeff Kirsher 	for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
258*9e13fbf7SJeff Kirsher 		printk("RX [%d]: @(%p) [%08x,%08x,%08x] ",
259*9e13fbf7SJeff Kirsher 		       i, (&r[i]), r[i].rdma.pbuf, r[i].rdma.cntinfo,
260*9e13fbf7SJeff Kirsher 		       r[i].rdma.pnext);
261*9e13fbf7SJeff Kirsher 		i += 1;
262*9e13fbf7SJeff Kirsher 		printk("-- [%d]: @(%p) [%08x,%08x,%08x]\n",
263*9e13fbf7SJeff Kirsher 		       i, (&r[i]), r[i].rdma.pbuf, r[i].rdma.cntinfo,
264*9e13fbf7SJeff Kirsher 		       r[i].rdma.pnext);
265*9e13fbf7SJeff Kirsher 	}
266*9e13fbf7SJeff Kirsher 	for (i = 0; i < SEEQ_TX_BUFFERS; i++) {
267*9e13fbf7SJeff Kirsher 		printk("TX [%d]: @(%p) [%08x,%08x,%08x] ",
268*9e13fbf7SJeff Kirsher 		       i, (&t[i]), t[i].tdma.pbuf, t[i].tdma.cntinfo,
269*9e13fbf7SJeff Kirsher 		       t[i].tdma.pnext);
270*9e13fbf7SJeff Kirsher 		i += 1;
271*9e13fbf7SJeff Kirsher 		printk("-- [%d]: @(%p) [%08x,%08x,%08x]\n",
272*9e13fbf7SJeff Kirsher 		       i, (&t[i]), t[i].tdma.pbuf, t[i].tdma.cntinfo,
273*9e13fbf7SJeff Kirsher 		       t[i].tdma.pnext);
274*9e13fbf7SJeff Kirsher 	}
275*9e13fbf7SJeff Kirsher 	printk("INFO: [rx_new = %d rx_old=%d] [tx_new = %d tx_old = %d]\n",
276*9e13fbf7SJeff Kirsher 	       gpriv->rx_new, gpriv->rx_old, gpriv->tx_new, gpriv->tx_old);
277*9e13fbf7SJeff Kirsher 	printk("RREGS: rx_cbptr[%08x] rx_ndptr[%08x] rx_ctrl[%08x]\n",
278*9e13fbf7SJeff Kirsher 	       hregs->rx_cbptr, hregs->rx_ndptr, hregs->rx_ctrl);
279*9e13fbf7SJeff Kirsher 	printk("TREGS: tx_cbptr[%08x] tx_ndptr[%08x] tx_ctrl[%08x]\n",
280*9e13fbf7SJeff Kirsher 	       hregs->tx_cbptr, hregs->tx_ndptr, hregs->tx_ctrl);
281*9e13fbf7SJeff Kirsher }
282*9e13fbf7SJeff Kirsher #endif
283*9e13fbf7SJeff Kirsher 
284*9e13fbf7SJeff Kirsher #define TSTAT_INIT_SEEQ (SEEQ_TCMD_IPT|SEEQ_TCMD_I16|SEEQ_TCMD_IC|SEEQ_TCMD_IUF)
285*9e13fbf7SJeff Kirsher #define TSTAT_INIT_EDLC ((TSTAT_INIT_SEEQ) | SEEQ_TCMD_RB2)
286*9e13fbf7SJeff Kirsher 
287*9e13fbf7SJeff Kirsher static int init_seeq(struct net_device *dev, struct sgiseeq_private *sp,
288*9e13fbf7SJeff Kirsher 		     struct sgiseeq_regs *sregs)
289*9e13fbf7SJeff Kirsher {
290*9e13fbf7SJeff Kirsher 	struct hpc3_ethregs *hregs = sp->hregs;
291*9e13fbf7SJeff Kirsher 	int err;
292*9e13fbf7SJeff Kirsher 
293*9e13fbf7SJeff Kirsher 	reset_hpc3_and_seeq(hregs, sregs);
294*9e13fbf7SJeff Kirsher 	err = seeq_init_ring(dev);
295*9e13fbf7SJeff Kirsher 	if (err)
296*9e13fbf7SJeff Kirsher 		return err;
297*9e13fbf7SJeff Kirsher 
298*9e13fbf7SJeff Kirsher 	/* Setup to field the proper interrupt types. */
299*9e13fbf7SJeff Kirsher 	if (sp->is_edlc) {
300*9e13fbf7SJeff Kirsher 		sregs->tstat = TSTAT_INIT_EDLC;
301*9e13fbf7SJeff Kirsher 		sregs->rw.wregs.control = sp->control;
302*9e13fbf7SJeff Kirsher 		sregs->rw.wregs.frame_gap = 0;
303*9e13fbf7SJeff Kirsher 	} else {
304*9e13fbf7SJeff Kirsher 		sregs->tstat = TSTAT_INIT_SEEQ;
305*9e13fbf7SJeff Kirsher 	}
306*9e13fbf7SJeff Kirsher 
307*9e13fbf7SJeff Kirsher 	hregs->rx_ndptr = VIRT_TO_DMA(sp, sp->rx_desc);
308*9e13fbf7SJeff Kirsher 	hregs->tx_ndptr = VIRT_TO_DMA(sp, sp->tx_desc);
309*9e13fbf7SJeff Kirsher 
310*9e13fbf7SJeff Kirsher 	seeq_go(sp, hregs, sregs);
311*9e13fbf7SJeff Kirsher 	return 0;
312*9e13fbf7SJeff Kirsher }
313*9e13fbf7SJeff Kirsher 
314*9e13fbf7SJeff Kirsher static void record_rx_errors(struct net_device *dev, unsigned char status)
315*9e13fbf7SJeff Kirsher {
316*9e13fbf7SJeff Kirsher 	if (status & SEEQ_RSTAT_OVERF ||
317*9e13fbf7SJeff Kirsher 	    status & SEEQ_RSTAT_SFRAME)
318*9e13fbf7SJeff Kirsher 		dev->stats.rx_over_errors++;
319*9e13fbf7SJeff Kirsher 	if (status & SEEQ_RSTAT_CERROR)
320*9e13fbf7SJeff Kirsher 		dev->stats.rx_crc_errors++;
321*9e13fbf7SJeff Kirsher 	if (status & SEEQ_RSTAT_DERROR)
322*9e13fbf7SJeff Kirsher 		dev->stats.rx_frame_errors++;
323*9e13fbf7SJeff Kirsher 	if (status & SEEQ_RSTAT_REOF)
324*9e13fbf7SJeff Kirsher 		dev->stats.rx_errors++;
325*9e13fbf7SJeff Kirsher }
326*9e13fbf7SJeff Kirsher 
327*9e13fbf7SJeff Kirsher static inline void rx_maybe_restart(struct sgiseeq_private *sp,
328*9e13fbf7SJeff Kirsher 				    struct hpc3_ethregs *hregs,
329*9e13fbf7SJeff Kirsher 				    struct sgiseeq_regs *sregs)
330*9e13fbf7SJeff Kirsher {
331*9e13fbf7SJeff Kirsher 	if (!(hregs->rx_ctrl & HPC3_ERXCTRL_ACTIVE)) {
332*9e13fbf7SJeff Kirsher 		hregs->rx_ndptr = VIRT_TO_DMA(sp, sp->rx_desc + sp->rx_new);
333*9e13fbf7SJeff Kirsher 		seeq_go(sp, hregs, sregs);
334*9e13fbf7SJeff Kirsher 	}
335*9e13fbf7SJeff Kirsher }
336*9e13fbf7SJeff Kirsher 
337*9e13fbf7SJeff Kirsher static inline void sgiseeq_rx(struct net_device *dev, struct sgiseeq_private *sp,
338*9e13fbf7SJeff Kirsher 			      struct hpc3_ethregs *hregs,
339*9e13fbf7SJeff Kirsher 			      struct sgiseeq_regs *sregs)
340*9e13fbf7SJeff Kirsher {
341*9e13fbf7SJeff Kirsher 	struct sgiseeq_rx_desc *rd;
342*9e13fbf7SJeff Kirsher 	struct sk_buff *skb = NULL;
343*9e13fbf7SJeff Kirsher 	struct sk_buff *newskb;
344*9e13fbf7SJeff Kirsher 	unsigned char pkt_status;
345*9e13fbf7SJeff Kirsher 	int len = 0;
346*9e13fbf7SJeff Kirsher 	unsigned int orig_end = PREV_RX(sp->rx_new);
347*9e13fbf7SJeff Kirsher 
348*9e13fbf7SJeff Kirsher 	/* Service every received packet. */
349*9e13fbf7SJeff Kirsher 	rd = &sp->rx_desc[sp->rx_new];
350*9e13fbf7SJeff Kirsher 	dma_sync_desc_cpu(dev, rd);
351*9e13fbf7SJeff Kirsher 	while (!(rd->rdma.cntinfo & HPCDMA_OWN)) {
352*9e13fbf7SJeff Kirsher 		len = PKT_BUF_SZ - (rd->rdma.cntinfo & HPCDMA_BCNT) - 3;
353*9e13fbf7SJeff Kirsher 		dma_unmap_single(dev->dev.parent, rd->rdma.pbuf,
354*9e13fbf7SJeff Kirsher 				 PKT_BUF_SZ, DMA_FROM_DEVICE);
355*9e13fbf7SJeff Kirsher 		pkt_status = rd->skb->data[len];
356*9e13fbf7SJeff Kirsher 		if (pkt_status & SEEQ_RSTAT_FIG) {
357*9e13fbf7SJeff Kirsher 			/* Packet is OK. */
358*9e13fbf7SJeff Kirsher 			/* We don't want to receive our own packets */
359*9e13fbf7SJeff Kirsher 			if (memcmp(rd->skb->data + 6, dev->dev_addr, ETH_ALEN)) {
360*9e13fbf7SJeff Kirsher 				if (len > rx_copybreak) {
361*9e13fbf7SJeff Kirsher 					skb = rd->skb;
362*9e13fbf7SJeff Kirsher 					newskb = netdev_alloc_skb(dev, PKT_BUF_SZ);
363*9e13fbf7SJeff Kirsher 					if (!newskb) {
364*9e13fbf7SJeff Kirsher 						newskb = skb;
365*9e13fbf7SJeff Kirsher 						skb = NULL;
366*9e13fbf7SJeff Kirsher 						goto memory_squeeze;
367*9e13fbf7SJeff Kirsher 					}
368*9e13fbf7SJeff Kirsher 					skb_reserve(newskb, 2);
369*9e13fbf7SJeff Kirsher 				} else {
370*9e13fbf7SJeff Kirsher 					skb = netdev_alloc_skb_ip_align(dev, len);
371*9e13fbf7SJeff Kirsher 					if (skb)
372*9e13fbf7SJeff Kirsher 						skb_copy_to_linear_data(skb, rd->skb->data, len);
373*9e13fbf7SJeff Kirsher 
374*9e13fbf7SJeff Kirsher 					newskb = rd->skb;
375*9e13fbf7SJeff Kirsher 				}
376*9e13fbf7SJeff Kirsher memory_squeeze:
377*9e13fbf7SJeff Kirsher 				if (skb) {
378*9e13fbf7SJeff Kirsher 					skb_put(skb, len);
379*9e13fbf7SJeff Kirsher 					skb->protocol = eth_type_trans(skb, dev);
380*9e13fbf7SJeff Kirsher 					netif_rx(skb);
381*9e13fbf7SJeff Kirsher 					dev->stats.rx_packets++;
382*9e13fbf7SJeff Kirsher 					dev->stats.rx_bytes += len;
383*9e13fbf7SJeff Kirsher 				} else {
384*9e13fbf7SJeff Kirsher 					printk(KERN_NOTICE "%s: Memory squeeze, deferring packet.\n",
385*9e13fbf7SJeff Kirsher 						dev->name);
386*9e13fbf7SJeff Kirsher 					dev->stats.rx_dropped++;
387*9e13fbf7SJeff Kirsher 				}
388*9e13fbf7SJeff Kirsher 			} else {
389*9e13fbf7SJeff Kirsher 				/* Silently drop my own packets */
390*9e13fbf7SJeff Kirsher 				newskb = rd->skb;
391*9e13fbf7SJeff Kirsher 			}
392*9e13fbf7SJeff Kirsher 		} else {
393*9e13fbf7SJeff Kirsher 			record_rx_errors(dev, pkt_status);
394*9e13fbf7SJeff Kirsher 			newskb = rd->skb;
395*9e13fbf7SJeff Kirsher 		}
396*9e13fbf7SJeff Kirsher 		rd->skb = newskb;
397*9e13fbf7SJeff Kirsher 		rd->rdma.pbuf = dma_map_single(dev->dev.parent,
398*9e13fbf7SJeff Kirsher 					       newskb->data - 2,
399*9e13fbf7SJeff Kirsher 					       PKT_BUF_SZ, DMA_FROM_DEVICE);
400*9e13fbf7SJeff Kirsher 
401*9e13fbf7SJeff Kirsher 		/* Return the entry to the ring pool. */
402*9e13fbf7SJeff Kirsher 		rd->rdma.cntinfo = RCNTINFO_INIT;
403*9e13fbf7SJeff Kirsher 		sp->rx_new = NEXT_RX(sp->rx_new);
404*9e13fbf7SJeff Kirsher 		dma_sync_desc_dev(dev, rd);
405*9e13fbf7SJeff Kirsher 		rd = &sp->rx_desc[sp->rx_new];
406*9e13fbf7SJeff Kirsher 		dma_sync_desc_cpu(dev, rd);
407*9e13fbf7SJeff Kirsher 	}
408*9e13fbf7SJeff Kirsher 	dma_sync_desc_cpu(dev, &sp->rx_desc[orig_end]);
409*9e13fbf7SJeff Kirsher 	sp->rx_desc[orig_end].rdma.cntinfo &= ~(HPCDMA_EOR);
410*9e13fbf7SJeff Kirsher 	dma_sync_desc_dev(dev, &sp->rx_desc[orig_end]);
411*9e13fbf7SJeff Kirsher 	dma_sync_desc_cpu(dev, &sp->rx_desc[PREV_RX(sp->rx_new)]);
412*9e13fbf7SJeff Kirsher 	sp->rx_desc[PREV_RX(sp->rx_new)].rdma.cntinfo |= HPCDMA_EOR;
413*9e13fbf7SJeff Kirsher 	dma_sync_desc_dev(dev, &sp->rx_desc[PREV_RX(sp->rx_new)]);
414*9e13fbf7SJeff Kirsher 	rx_maybe_restart(sp, hregs, sregs);
415*9e13fbf7SJeff Kirsher }
416*9e13fbf7SJeff Kirsher 
417*9e13fbf7SJeff Kirsher static inline void tx_maybe_reset_collisions(struct sgiseeq_private *sp,
418*9e13fbf7SJeff Kirsher 					     struct sgiseeq_regs *sregs)
419*9e13fbf7SJeff Kirsher {
420*9e13fbf7SJeff Kirsher 	if (sp->is_edlc) {
421*9e13fbf7SJeff Kirsher 		sregs->rw.wregs.control = sp->control & ~(SEEQ_CTRL_XCNT);
422*9e13fbf7SJeff Kirsher 		sregs->rw.wregs.control = sp->control;
423*9e13fbf7SJeff Kirsher 	}
424*9e13fbf7SJeff Kirsher }
425*9e13fbf7SJeff Kirsher 
426*9e13fbf7SJeff Kirsher static inline void kick_tx(struct net_device *dev,
427*9e13fbf7SJeff Kirsher 			   struct sgiseeq_private *sp,
428*9e13fbf7SJeff Kirsher 			   struct hpc3_ethregs *hregs)
429*9e13fbf7SJeff Kirsher {
430*9e13fbf7SJeff Kirsher 	struct sgiseeq_tx_desc *td;
431*9e13fbf7SJeff Kirsher 	int i = sp->tx_old;
432*9e13fbf7SJeff Kirsher 
433*9e13fbf7SJeff Kirsher 	/* If the HPC aint doin nothin, and there are more packets
434*9e13fbf7SJeff Kirsher 	 * with ETXD cleared and XIU set we must make very certain
435*9e13fbf7SJeff Kirsher 	 * that we restart the HPC else we risk locking up the
436*9e13fbf7SJeff Kirsher 	 * adapter.  The following code is only safe iff the HPCDMA
437*9e13fbf7SJeff Kirsher 	 * is not active!
438*9e13fbf7SJeff Kirsher 	 */
439*9e13fbf7SJeff Kirsher 	td = &sp->tx_desc[i];
440*9e13fbf7SJeff Kirsher 	dma_sync_desc_cpu(dev, td);
441*9e13fbf7SJeff Kirsher 	while ((td->tdma.cntinfo & (HPCDMA_XIU | HPCDMA_ETXD)) ==
442*9e13fbf7SJeff Kirsher 	      (HPCDMA_XIU | HPCDMA_ETXD)) {
443*9e13fbf7SJeff Kirsher 		i = NEXT_TX(i);
444*9e13fbf7SJeff Kirsher 		td = &sp->tx_desc[i];
445*9e13fbf7SJeff Kirsher 		dma_sync_desc_cpu(dev, td);
446*9e13fbf7SJeff Kirsher 	}
447*9e13fbf7SJeff Kirsher 	if (td->tdma.cntinfo & HPCDMA_XIU) {
448*9e13fbf7SJeff Kirsher 		hregs->tx_ndptr = VIRT_TO_DMA(sp, td);
449*9e13fbf7SJeff Kirsher 		hregs->tx_ctrl = HPC3_ETXCTRL_ACTIVE;
450*9e13fbf7SJeff Kirsher 	}
451*9e13fbf7SJeff Kirsher }
452*9e13fbf7SJeff Kirsher 
453*9e13fbf7SJeff Kirsher static inline void sgiseeq_tx(struct net_device *dev, struct sgiseeq_private *sp,
454*9e13fbf7SJeff Kirsher 			      struct hpc3_ethregs *hregs,
455*9e13fbf7SJeff Kirsher 			      struct sgiseeq_regs *sregs)
456*9e13fbf7SJeff Kirsher {
457*9e13fbf7SJeff Kirsher 	struct sgiseeq_tx_desc *td;
458*9e13fbf7SJeff Kirsher 	unsigned long status = hregs->tx_ctrl;
459*9e13fbf7SJeff Kirsher 	int j;
460*9e13fbf7SJeff Kirsher 
461*9e13fbf7SJeff Kirsher 	tx_maybe_reset_collisions(sp, sregs);
462*9e13fbf7SJeff Kirsher 
463*9e13fbf7SJeff Kirsher 	if (!(status & (HPC3_ETXCTRL_ACTIVE | SEEQ_TSTAT_PTRANS))) {
464*9e13fbf7SJeff Kirsher 		/* Oops, HPC detected some sort of error. */
465*9e13fbf7SJeff Kirsher 		if (status & SEEQ_TSTAT_R16)
466*9e13fbf7SJeff Kirsher 			dev->stats.tx_aborted_errors++;
467*9e13fbf7SJeff Kirsher 		if (status & SEEQ_TSTAT_UFLOW)
468*9e13fbf7SJeff Kirsher 			dev->stats.tx_fifo_errors++;
469*9e13fbf7SJeff Kirsher 		if (status & SEEQ_TSTAT_LCLS)
470*9e13fbf7SJeff Kirsher 			dev->stats.collisions++;
471*9e13fbf7SJeff Kirsher 	}
472*9e13fbf7SJeff Kirsher 
473*9e13fbf7SJeff Kirsher 	/* Ack 'em... */
474*9e13fbf7SJeff Kirsher 	for (j = sp->tx_old; j != sp->tx_new; j = NEXT_TX(j)) {
475*9e13fbf7SJeff Kirsher 		td = &sp->tx_desc[j];
476*9e13fbf7SJeff Kirsher 
477*9e13fbf7SJeff Kirsher 		dma_sync_desc_cpu(dev, td);
478*9e13fbf7SJeff Kirsher 		if (!(td->tdma.cntinfo & (HPCDMA_XIU)))
479*9e13fbf7SJeff Kirsher 			break;
480*9e13fbf7SJeff Kirsher 		if (!(td->tdma.cntinfo & (HPCDMA_ETXD))) {
481*9e13fbf7SJeff Kirsher 			if (!(status & HPC3_ETXCTRL_ACTIVE)) {
482*9e13fbf7SJeff Kirsher 				hregs->tx_ndptr = VIRT_TO_DMA(sp, td);
483*9e13fbf7SJeff Kirsher 				hregs->tx_ctrl = HPC3_ETXCTRL_ACTIVE;
484*9e13fbf7SJeff Kirsher 			}
485*9e13fbf7SJeff Kirsher 			break;
486*9e13fbf7SJeff Kirsher 		}
487*9e13fbf7SJeff Kirsher 		dev->stats.tx_packets++;
488*9e13fbf7SJeff Kirsher 		sp->tx_old = NEXT_TX(sp->tx_old);
489*9e13fbf7SJeff Kirsher 		td->tdma.cntinfo &= ~(HPCDMA_XIU | HPCDMA_XIE);
490*9e13fbf7SJeff Kirsher 		td->tdma.cntinfo |= HPCDMA_EOX;
491*9e13fbf7SJeff Kirsher 		if (td->skb) {
492*9e13fbf7SJeff Kirsher 			dev_kfree_skb_any(td->skb);
493*9e13fbf7SJeff Kirsher 			td->skb = NULL;
494*9e13fbf7SJeff Kirsher 		}
495*9e13fbf7SJeff Kirsher 		dma_sync_desc_dev(dev, td);
496*9e13fbf7SJeff Kirsher 	}
497*9e13fbf7SJeff Kirsher }
498*9e13fbf7SJeff Kirsher 
499*9e13fbf7SJeff Kirsher static irqreturn_t sgiseeq_interrupt(int irq, void *dev_id)
500*9e13fbf7SJeff Kirsher {
501*9e13fbf7SJeff Kirsher 	struct net_device *dev = (struct net_device *) dev_id;
502*9e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
503*9e13fbf7SJeff Kirsher 	struct hpc3_ethregs *hregs = sp->hregs;
504*9e13fbf7SJeff Kirsher 	struct sgiseeq_regs *sregs = sp->sregs;
505*9e13fbf7SJeff Kirsher 
506*9e13fbf7SJeff Kirsher 	spin_lock(&sp->tx_lock);
507*9e13fbf7SJeff Kirsher 
508*9e13fbf7SJeff Kirsher 	/* Ack the IRQ and set software state. */
509*9e13fbf7SJeff Kirsher 	hregs->reset = HPC3_ERST_CLRIRQ;
510*9e13fbf7SJeff Kirsher 
511*9e13fbf7SJeff Kirsher 	/* Always check for received packets. */
512*9e13fbf7SJeff Kirsher 	sgiseeq_rx(dev, sp, hregs, sregs);
513*9e13fbf7SJeff Kirsher 
514*9e13fbf7SJeff Kirsher 	/* Only check for tx acks if we have something queued. */
515*9e13fbf7SJeff Kirsher 	if (sp->tx_old != sp->tx_new)
516*9e13fbf7SJeff Kirsher 		sgiseeq_tx(dev, sp, hregs, sregs);
517*9e13fbf7SJeff Kirsher 
518*9e13fbf7SJeff Kirsher 	if ((TX_BUFFS_AVAIL(sp) > 0) && netif_queue_stopped(dev)) {
519*9e13fbf7SJeff Kirsher 		netif_wake_queue(dev);
520*9e13fbf7SJeff Kirsher 	}
521*9e13fbf7SJeff Kirsher 	spin_unlock(&sp->tx_lock);
522*9e13fbf7SJeff Kirsher 
523*9e13fbf7SJeff Kirsher 	return IRQ_HANDLED;
524*9e13fbf7SJeff Kirsher }
525*9e13fbf7SJeff Kirsher 
526*9e13fbf7SJeff Kirsher static int sgiseeq_open(struct net_device *dev)
527*9e13fbf7SJeff Kirsher {
528*9e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
529*9e13fbf7SJeff Kirsher 	struct sgiseeq_regs *sregs = sp->sregs;
530*9e13fbf7SJeff Kirsher 	unsigned int irq = dev->irq;
531*9e13fbf7SJeff Kirsher 	int err;
532*9e13fbf7SJeff Kirsher 
533*9e13fbf7SJeff Kirsher 	if (request_irq(irq, sgiseeq_interrupt, 0, sgiseeqstr, dev)) {
534*9e13fbf7SJeff Kirsher 		printk(KERN_ERR "Seeq8003: Can't get irq %d\n", dev->irq);
535*9e13fbf7SJeff Kirsher 		return -EAGAIN;
536*9e13fbf7SJeff Kirsher 	}
537*9e13fbf7SJeff Kirsher 
538*9e13fbf7SJeff Kirsher 	err = init_seeq(dev, sp, sregs);
539*9e13fbf7SJeff Kirsher 	if (err)
540*9e13fbf7SJeff Kirsher 		goto out_free_irq;
541*9e13fbf7SJeff Kirsher 
542*9e13fbf7SJeff Kirsher 	netif_start_queue(dev);
543*9e13fbf7SJeff Kirsher 
544*9e13fbf7SJeff Kirsher 	return 0;
545*9e13fbf7SJeff Kirsher 
546*9e13fbf7SJeff Kirsher out_free_irq:
547*9e13fbf7SJeff Kirsher 	free_irq(irq, dev);
548*9e13fbf7SJeff Kirsher 
549*9e13fbf7SJeff Kirsher 	return err;
550*9e13fbf7SJeff Kirsher }
551*9e13fbf7SJeff Kirsher 
552*9e13fbf7SJeff Kirsher static int sgiseeq_close(struct net_device *dev)
553*9e13fbf7SJeff Kirsher {
554*9e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
555*9e13fbf7SJeff Kirsher 	struct sgiseeq_regs *sregs = sp->sregs;
556*9e13fbf7SJeff Kirsher 	unsigned int irq = dev->irq;
557*9e13fbf7SJeff Kirsher 
558*9e13fbf7SJeff Kirsher 	netif_stop_queue(dev);
559*9e13fbf7SJeff Kirsher 
560*9e13fbf7SJeff Kirsher 	/* Shutdown the Seeq. */
561*9e13fbf7SJeff Kirsher 	reset_hpc3_and_seeq(sp->hregs, sregs);
562*9e13fbf7SJeff Kirsher 	free_irq(irq, dev);
563*9e13fbf7SJeff Kirsher 	seeq_purge_ring(dev);
564*9e13fbf7SJeff Kirsher 
565*9e13fbf7SJeff Kirsher 	return 0;
566*9e13fbf7SJeff Kirsher }
567*9e13fbf7SJeff Kirsher 
568*9e13fbf7SJeff Kirsher static inline int sgiseeq_reset(struct net_device *dev)
569*9e13fbf7SJeff Kirsher {
570*9e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
571*9e13fbf7SJeff Kirsher 	struct sgiseeq_regs *sregs = sp->sregs;
572*9e13fbf7SJeff Kirsher 	int err;
573*9e13fbf7SJeff Kirsher 
574*9e13fbf7SJeff Kirsher 	err = init_seeq(dev, sp, sregs);
575*9e13fbf7SJeff Kirsher 	if (err)
576*9e13fbf7SJeff Kirsher 		return err;
577*9e13fbf7SJeff Kirsher 
578*9e13fbf7SJeff Kirsher 	dev->trans_start = jiffies; /* prevent tx timeout */
579*9e13fbf7SJeff Kirsher 	netif_wake_queue(dev);
580*9e13fbf7SJeff Kirsher 
581*9e13fbf7SJeff Kirsher 	return 0;
582*9e13fbf7SJeff Kirsher }
583*9e13fbf7SJeff Kirsher 
584*9e13fbf7SJeff Kirsher static int sgiseeq_start_xmit(struct sk_buff *skb, struct net_device *dev)
585*9e13fbf7SJeff Kirsher {
586*9e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
587*9e13fbf7SJeff Kirsher 	struct hpc3_ethregs *hregs = sp->hregs;
588*9e13fbf7SJeff Kirsher 	unsigned long flags;
589*9e13fbf7SJeff Kirsher 	struct sgiseeq_tx_desc *td;
590*9e13fbf7SJeff Kirsher 	int len, entry;
591*9e13fbf7SJeff Kirsher 
592*9e13fbf7SJeff Kirsher 	spin_lock_irqsave(&sp->tx_lock, flags);
593*9e13fbf7SJeff Kirsher 
594*9e13fbf7SJeff Kirsher 	/* Setup... */
595*9e13fbf7SJeff Kirsher 	len = skb->len;
596*9e13fbf7SJeff Kirsher 	if (len < ETH_ZLEN) {
597*9e13fbf7SJeff Kirsher 		if (skb_padto(skb, ETH_ZLEN)) {
598*9e13fbf7SJeff Kirsher 			spin_unlock_irqrestore(&sp->tx_lock, flags);
599*9e13fbf7SJeff Kirsher 			return NETDEV_TX_OK;
600*9e13fbf7SJeff Kirsher 		}
601*9e13fbf7SJeff Kirsher 		len = ETH_ZLEN;
602*9e13fbf7SJeff Kirsher 	}
603*9e13fbf7SJeff Kirsher 
604*9e13fbf7SJeff Kirsher 	dev->stats.tx_bytes += len;
605*9e13fbf7SJeff Kirsher 	entry = sp->tx_new;
606*9e13fbf7SJeff Kirsher 	td = &sp->tx_desc[entry];
607*9e13fbf7SJeff Kirsher 	dma_sync_desc_cpu(dev, td);
608*9e13fbf7SJeff Kirsher 
609*9e13fbf7SJeff Kirsher 	/* Create entry.  There are so many races with adding a new
610*9e13fbf7SJeff Kirsher 	 * descriptor to the chain:
611*9e13fbf7SJeff Kirsher 	 * 1) Assume that the HPC is off processing a DMA chain while
612*9e13fbf7SJeff Kirsher 	 *    we are changing all of the following.
613*9e13fbf7SJeff Kirsher 	 * 2) Do no allow the HPC to look at a new descriptor until
614*9e13fbf7SJeff Kirsher 	 *    we have completely set up it's state.  This means, do
615*9e13fbf7SJeff Kirsher 	 *    not clear HPCDMA_EOX in the current last descritptor
616*9e13fbf7SJeff Kirsher 	 *    until the one we are adding looks consistent and could
617*9e13fbf7SJeff Kirsher 	 *    be processes right now.
618*9e13fbf7SJeff Kirsher 	 * 3) The tx interrupt code must notice when we've added a new
619*9e13fbf7SJeff Kirsher 	 *    entry and the HPC got to the end of the chain before we
620*9e13fbf7SJeff Kirsher 	 *    added this new entry and restarted it.
621*9e13fbf7SJeff Kirsher 	 */
622*9e13fbf7SJeff Kirsher 	td->skb = skb;
623*9e13fbf7SJeff Kirsher 	td->tdma.pbuf = dma_map_single(dev->dev.parent, skb->data,
624*9e13fbf7SJeff Kirsher 				       len, DMA_TO_DEVICE);
625*9e13fbf7SJeff Kirsher 	td->tdma.cntinfo = (len & HPCDMA_BCNT) |
626*9e13fbf7SJeff Kirsher 	                   HPCDMA_XIU | HPCDMA_EOXP | HPCDMA_XIE | HPCDMA_EOX;
627*9e13fbf7SJeff Kirsher 	dma_sync_desc_dev(dev, td);
628*9e13fbf7SJeff Kirsher 	if (sp->tx_old != sp->tx_new) {
629*9e13fbf7SJeff Kirsher 		struct sgiseeq_tx_desc *backend;
630*9e13fbf7SJeff Kirsher 
631*9e13fbf7SJeff Kirsher 		backend = &sp->tx_desc[PREV_TX(sp->tx_new)];
632*9e13fbf7SJeff Kirsher 		dma_sync_desc_cpu(dev, backend);
633*9e13fbf7SJeff Kirsher 		backend->tdma.cntinfo &= ~HPCDMA_EOX;
634*9e13fbf7SJeff Kirsher 		dma_sync_desc_dev(dev, backend);
635*9e13fbf7SJeff Kirsher 	}
636*9e13fbf7SJeff Kirsher 	sp->tx_new = NEXT_TX(sp->tx_new); /* Advance. */
637*9e13fbf7SJeff Kirsher 
638*9e13fbf7SJeff Kirsher 	/* Maybe kick the HPC back into motion. */
639*9e13fbf7SJeff Kirsher 	if (!(hregs->tx_ctrl & HPC3_ETXCTRL_ACTIVE))
640*9e13fbf7SJeff Kirsher 		kick_tx(dev, sp, hregs);
641*9e13fbf7SJeff Kirsher 
642*9e13fbf7SJeff Kirsher 	if (!TX_BUFFS_AVAIL(sp))
643*9e13fbf7SJeff Kirsher 		netif_stop_queue(dev);
644*9e13fbf7SJeff Kirsher 	spin_unlock_irqrestore(&sp->tx_lock, flags);
645*9e13fbf7SJeff Kirsher 
646*9e13fbf7SJeff Kirsher 	return NETDEV_TX_OK;
647*9e13fbf7SJeff Kirsher }
648*9e13fbf7SJeff Kirsher 
649*9e13fbf7SJeff Kirsher static void timeout(struct net_device *dev)
650*9e13fbf7SJeff Kirsher {
651*9e13fbf7SJeff Kirsher 	printk(KERN_NOTICE "%s: transmit timed out, resetting\n", dev->name);
652*9e13fbf7SJeff Kirsher 	sgiseeq_reset(dev);
653*9e13fbf7SJeff Kirsher 
654*9e13fbf7SJeff Kirsher 	dev->trans_start = jiffies; /* prevent tx timeout */
655*9e13fbf7SJeff Kirsher 	netif_wake_queue(dev);
656*9e13fbf7SJeff Kirsher }
657*9e13fbf7SJeff Kirsher 
658*9e13fbf7SJeff Kirsher static void sgiseeq_set_multicast(struct net_device *dev)
659*9e13fbf7SJeff Kirsher {
660*9e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
661*9e13fbf7SJeff Kirsher 	unsigned char oldmode = sp->mode;
662*9e13fbf7SJeff Kirsher 
663*9e13fbf7SJeff Kirsher 	if(dev->flags & IFF_PROMISC)
664*9e13fbf7SJeff Kirsher 		sp->mode = SEEQ_RCMD_RANY;
665*9e13fbf7SJeff Kirsher 	else if ((dev->flags & IFF_ALLMULTI) || !netdev_mc_empty(dev))
666*9e13fbf7SJeff Kirsher 		sp->mode = SEEQ_RCMD_RBMCAST;
667*9e13fbf7SJeff Kirsher 	else
668*9e13fbf7SJeff Kirsher 		sp->mode = SEEQ_RCMD_RBCAST;
669*9e13fbf7SJeff Kirsher 
670*9e13fbf7SJeff Kirsher 	/* XXX I know this sucks, but is there a better way to reprogram
671*9e13fbf7SJeff Kirsher 	 * XXX the receiver? At least, this shouldn't happen too often.
672*9e13fbf7SJeff Kirsher 	 */
673*9e13fbf7SJeff Kirsher 
674*9e13fbf7SJeff Kirsher 	if (oldmode != sp->mode)
675*9e13fbf7SJeff Kirsher 		sgiseeq_reset(dev);
676*9e13fbf7SJeff Kirsher }
677*9e13fbf7SJeff Kirsher 
678*9e13fbf7SJeff Kirsher static inline void setup_tx_ring(struct net_device *dev,
679*9e13fbf7SJeff Kirsher 				 struct sgiseeq_tx_desc *buf,
680*9e13fbf7SJeff Kirsher 				 int nbufs)
681*9e13fbf7SJeff Kirsher {
682*9e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
683*9e13fbf7SJeff Kirsher 	int i = 0;
684*9e13fbf7SJeff Kirsher 
685*9e13fbf7SJeff Kirsher 	while (i < (nbufs - 1)) {
686*9e13fbf7SJeff Kirsher 		buf[i].tdma.pnext = VIRT_TO_DMA(sp, buf + i + 1);
687*9e13fbf7SJeff Kirsher 		buf[i].tdma.pbuf = 0;
688*9e13fbf7SJeff Kirsher 		dma_sync_desc_dev(dev, &buf[i]);
689*9e13fbf7SJeff Kirsher 		i++;
690*9e13fbf7SJeff Kirsher 	}
691*9e13fbf7SJeff Kirsher 	buf[i].tdma.pnext = VIRT_TO_DMA(sp, buf);
692*9e13fbf7SJeff Kirsher 	dma_sync_desc_dev(dev, &buf[i]);
693*9e13fbf7SJeff Kirsher }
694*9e13fbf7SJeff Kirsher 
695*9e13fbf7SJeff Kirsher static inline void setup_rx_ring(struct net_device *dev,
696*9e13fbf7SJeff Kirsher 				 struct sgiseeq_rx_desc *buf,
697*9e13fbf7SJeff Kirsher 				 int nbufs)
698*9e13fbf7SJeff Kirsher {
699*9e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
700*9e13fbf7SJeff Kirsher 	int i = 0;
701*9e13fbf7SJeff Kirsher 
702*9e13fbf7SJeff Kirsher 	while (i < (nbufs - 1)) {
703*9e13fbf7SJeff Kirsher 		buf[i].rdma.pnext = VIRT_TO_DMA(sp, buf + i + 1);
704*9e13fbf7SJeff Kirsher 		buf[i].rdma.pbuf = 0;
705*9e13fbf7SJeff Kirsher 		dma_sync_desc_dev(dev, &buf[i]);
706*9e13fbf7SJeff Kirsher 		i++;
707*9e13fbf7SJeff Kirsher 	}
708*9e13fbf7SJeff Kirsher 	buf[i].rdma.pbuf = 0;
709*9e13fbf7SJeff Kirsher 	buf[i].rdma.pnext = VIRT_TO_DMA(sp, buf);
710*9e13fbf7SJeff Kirsher 	dma_sync_desc_dev(dev, &buf[i]);
711*9e13fbf7SJeff Kirsher }
712*9e13fbf7SJeff Kirsher 
713*9e13fbf7SJeff Kirsher static const struct net_device_ops sgiseeq_netdev_ops = {
714*9e13fbf7SJeff Kirsher 	.ndo_open		= sgiseeq_open,
715*9e13fbf7SJeff Kirsher 	.ndo_stop		= sgiseeq_close,
716*9e13fbf7SJeff Kirsher 	.ndo_start_xmit		= sgiseeq_start_xmit,
717*9e13fbf7SJeff Kirsher 	.ndo_tx_timeout		= timeout,
718*9e13fbf7SJeff Kirsher 	.ndo_set_multicast_list	= sgiseeq_set_multicast,
719*9e13fbf7SJeff Kirsher 	.ndo_set_mac_address	= sgiseeq_set_mac_address,
720*9e13fbf7SJeff Kirsher 	.ndo_change_mtu		= eth_change_mtu,
721*9e13fbf7SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
722*9e13fbf7SJeff Kirsher };
723*9e13fbf7SJeff Kirsher 
724*9e13fbf7SJeff Kirsher static int __devinit sgiseeq_probe(struct platform_device *pdev)
725*9e13fbf7SJeff Kirsher {
726*9e13fbf7SJeff Kirsher 	struct sgiseeq_platform_data *pd = pdev->dev.platform_data;
727*9e13fbf7SJeff Kirsher 	struct hpc3_regs *hpcregs = pd->hpc;
728*9e13fbf7SJeff Kirsher 	struct sgiseeq_init_block *sr;
729*9e13fbf7SJeff Kirsher 	unsigned int irq = pd->irq;
730*9e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp;
731*9e13fbf7SJeff Kirsher 	struct net_device *dev;
732*9e13fbf7SJeff Kirsher 	int err;
733*9e13fbf7SJeff Kirsher 
734*9e13fbf7SJeff Kirsher 	dev = alloc_etherdev(sizeof (struct sgiseeq_private));
735*9e13fbf7SJeff Kirsher 	if (!dev) {
736*9e13fbf7SJeff Kirsher 		printk(KERN_ERR "Sgiseeq: Etherdev alloc failed, aborting.\n");
737*9e13fbf7SJeff Kirsher 		err = -ENOMEM;
738*9e13fbf7SJeff Kirsher 		goto err_out;
739*9e13fbf7SJeff Kirsher 	}
740*9e13fbf7SJeff Kirsher 
741*9e13fbf7SJeff Kirsher 	platform_set_drvdata(pdev, dev);
742*9e13fbf7SJeff Kirsher 	sp = netdev_priv(dev);
743*9e13fbf7SJeff Kirsher 
744*9e13fbf7SJeff Kirsher 	/* Make private data page aligned */
745*9e13fbf7SJeff Kirsher 	sr = dma_alloc_noncoherent(&pdev->dev, sizeof(*sp->srings),
746*9e13fbf7SJeff Kirsher 				&sp->srings_dma, GFP_KERNEL);
747*9e13fbf7SJeff Kirsher 	if (!sr) {
748*9e13fbf7SJeff Kirsher 		printk(KERN_ERR "Sgiseeq: Page alloc failed, aborting.\n");
749*9e13fbf7SJeff Kirsher 		err = -ENOMEM;
750*9e13fbf7SJeff Kirsher 		goto err_out_free_dev;
751*9e13fbf7SJeff Kirsher 	}
752*9e13fbf7SJeff Kirsher 	sp->srings = sr;
753*9e13fbf7SJeff Kirsher 	sp->rx_desc = sp->srings->rxvector;
754*9e13fbf7SJeff Kirsher 	sp->tx_desc = sp->srings->txvector;
755*9e13fbf7SJeff Kirsher 
756*9e13fbf7SJeff Kirsher 	/* A couple calculations now, saves many cycles later. */
757*9e13fbf7SJeff Kirsher 	setup_rx_ring(dev, sp->rx_desc, SEEQ_RX_BUFFERS);
758*9e13fbf7SJeff Kirsher 	setup_tx_ring(dev, sp->tx_desc, SEEQ_TX_BUFFERS);
759*9e13fbf7SJeff Kirsher 
760*9e13fbf7SJeff Kirsher 	memcpy(dev->dev_addr, pd->mac, ETH_ALEN);
761*9e13fbf7SJeff Kirsher 
762*9e13fbf7SJeff Kirsher #ifdef DEBUG
763*9e13fbf7SJeff Kirsher 	gpriv = sp;
764*9e13fbf7SJeff Kirsher 	gdev = dev;
765*9e13fbf7SJeff Kirsher #endif
766*9e13fbf7SJeff Kirsher 	sp->sregs = (struct sgiseeq_regs *) &hpcregs->eth_ext[0];
767*9e13fbf7SJeff Kirsher 	sp->hregs = &hpcregs->ethregs;
768*9e13fbf7SJeff Kirsher 	sp->name = sgiseeqstr;
769*9e13fbf7SJeff Kirsher 	sp->mode = SEEQ_RCMD_RBCAST;
770*9e13fbf7SJeff Kirsher 
771*9e13fbf7SJeff Kirsher 	/* Setup PIO and DMA transfer timing */
772*9e13fbf7SJeff Kirsher 	sp->hregs->pconfig = 0x161;
773*9e13fbf7SJeff Kirsher 	sp->hregs->dconfig = HPC3_EDCFG_FIRQ | HPC3_EDCFG_FEOP |
774*9e13fbf7SJeff Kirsher 			     HPC3_EDCFG_FRXDC | HPC3_EDCFG_PTO | 0x026;
775*9e13fbf7SJeff Kirsher 
776*9e13fbf7SJeff Kirsher 	/* Setup PIO and DMA transfer timing */
777*9e13fbf7SJeff Kirsher 	sp->hregs->pconfig = 0x161;
778*9e13fbf7SJeff Kirsher 	sp->hregs->dconfig = HPC3_EDCFG_FIRQ | HPC3_EDCFG_FEOP |
779*9e13fbf7SJeff Kirsher 			     HPC3_EDCFG_FRXDC | HPC3_EDCFG_PTO | 0x026;
780*9e13fbf7SJeff Kirsher 
781*9e13fbf7SJeff Kirsher 	/* Reset the chip. */
782*9e13fbf7SJeff Kirsher 	hpc3_eth_reset(sp->hregs);
783*9e13fbf7SJeff Kirsher 
784*9e13fbf7SJeff Kirsher 	sp->is_edlc = !(sp->sregs->rw.rregs.collision_tx[0] & 0xff);
785*9e13fbf7SJeff Kirsher 	if (sp->is_edlc)
786*9e13fbf7SJeff Kirsher 		sp->control = SEEQ_CTRL_XCNT | SEEQ_CTRL_ACCNT |
787*9e13fbf7SJeff Kirsher 			      SEEQ_CTRL_SFLAG | SEEQ_CTRL_ESHORT |
788*9e13fbf7SJeff Kirsher 			      SEEQ_CTRL_ENCARR;
789*9e13fbf7SJeff Kirsher 
790*9e13fbf7SJeff Kirsher 	dev->netdev_ops		= &sgiseeq_netdev_ops;
791*9e13fbf7SJeff Kirsher 	dev->watchdog_timeo	= (200 * HZ) / 1000;
792*9e13fbf7SJeff Kirsher 	dev->irq		= irq;
793*9e13fbf7SJeff Kirsher 
794*9e13fbf7SJeff Kirsher 	if (register_netdev(dev)) {
795*9e13fbf7SJeff Kirsher 		printk(KERN_ERR "Sgiseeq: Cannot register net device, "
796*9e13fbf7SJeff Kirsher 		       "aborting.\n");
797*9e13fbf7SJeff Kirsher 		err = -ENODEV;
798*9e13fbf7SJeff Kirsher 		goto err_out_free_page;
799*9e13fbf7SJeff Kirsher 	}
800*9e13fbf7SJeff Kirsher 
801*9e13fbf7SJeff Kirsher 	printk(KERN_INFO "%s: %s %pM\n", dev->name, sgiseeqstr, dev->dev_addr);
802*9e13fbf7SJeff Kirsher 
803*9e13fbf7SJeff Kirsher 	return 0;
804*9e13fbf7SJeff Kirsher 
805*9e13fbf7SJeff Kirsher err_out_free_page:
806*9e13fbf7SJeff Kirsher 	free_page((unsigned long) sp->srings);
807*9e13fbf7SJeff Kirsher err_out_free_dev:
808*9e13fbf7SJeff Kirsher 	free_netdev(dev);
809*9e13fbf7SJeff Kirsher 
810*9e13fbf7SJeff Kirsher err_out:
811*9e13fbf7SJeff Kirsher 	return err;
812*9e13fbf7SJeff Kirsher }
813*9e13fbf7SJeff Kirsher 
814*9e13fbf7SJeff Kirsher static int __exit sgiseeq_remove(struct platform_device *pdev)
815*9e13fbf7SJeff Kirsher {
816*9e13fbf7SJeff Kirsher 	struct net_device *dev = platform_get_drvdata(pdev);
817*9e13fbf7SJeff Kirsher 	struct sgiseeq_private *sp = netdev_priv(dev);
818*9e13fbf7SJeff Kirsher 
819*9e13fbf7SJeff Kirsher 	unregister_netdev(dev);
820*9e13fbf7SJeff Kirsher 	dma_free_noncoherent(&pdev->dev, sizeof(*sp->srings), sp->srings,
821*9e13fbf7SJeff Kirsher 			     sp->srings_dma);
822*9e13fbf7SJeff Kirsher 	free_netdev(dev);
823*9e13fbf7SJeff Kirsher 	platform_set_drvdata(pdev, NULL);
824*9e13fbf7SJeff Kirsher 
825*9e13fbf7SJeff Kirsher 	return 0;
826*9e13fbf7SJeff Kirsher }
827*9e13fbf7SJeff Kirsher 
828*9e13fbf7SJeff Kirsher static struct platform_driver sgiseeq_driver = {
829*9e13fbf7SJeff Kirsher 	.probe	= sgiseeq_probe,
830*9e13fbf7SJeff Kirsher 	.remove	= __exit_p(sgiseeq_remove),
831*9e13fbf7SJeff Kirsher 	.driver = {
832*9e13fbf7SJeff Kirsher 		.name	= "sgiseeq",
833*9e13fbf7SJeff Kirsher 		.owner	= THIS_MODULE,
834*9e13fbf7SJeff Kirsher 	}
835*9e13fbf7SJeff Kirsher };
836*9e13fbf7SJeff Kirsher 
837*9e13fbf7SJeff Kirsher static int __init sgiseeq_module_init(void)
838*9e13fbf7SJeff Kirsher {
839*9e13fbf7SJeff Kirsher 	if (platform_driver_register(&sgiseeq_driver)) {
840*9e13fbf7SJeff Kirsher 		printk(KERN_ERR "Driver registration failed\n");
841*9e13fbf7SJeff Kirsher 		return -ENODEV;
842*9e13fbf7SJeff Kirsher 	}
843*9e13fbf7SJeff Kirsher 
844*9e13fbf7SJeff Kirsher 	return 0;
845*9e13fbf7SJeff Kirsher }
846*9e13fbf7SJeff Kirsher 
847*9e13fbf7SJeff Kirsher static void __exit sgiseeq_module_exit(void)
848*9e13fbf7SJeff Kirsher {
849*9e13fbf7SJeff Kirsher 	platform_driver_unregister(&sgiseeq_driver);
850*9e13fbf7SJeff Kirsher }
851*9e13fbf7SJeff Kirsher 
852*9e13fbf7SJeff Kirsher module_init(sgiseeq_module_init);
853*9e13fbf7SJeff Kirsher module_exit(sgiseeq_module_exit);
854*9e13fbf7SJeff Kirsher 
855*9e13fbf7SJeff Kirsher MODULE_DESCRIPTION("SGI Seeq 8003 driver");
856*9e13fbf7SJeff Kirsher MODULE_AUTHOR("Linux/MIPS Mailing List <linux-mips@linux-mips.org>");
857*9e13fbf7SJeff Kirsher MODULE_LICENSE("GPL");
858*9e13fbf7SJeff Kirsher MODULE_ALIAS("platform:sgiseeq");
859