blob: 3145ca13d2559d3df95341d93a32a9fd097c407e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * sgiseeq.c: Seeq8003 ethernet driver for SGI machines.
3 *
4 * Copyright (C) 1996 David S. Miller (dm@engr.sgi.com)
5 */
Ralf Baechlea4d4d512006-03-08 11:49:31 +00006
7#undef DEBUG
8
Linus Torvalds1da177e2005-04-16 15:20:36 -07009#include <linux/kernel.h>
10#include <linux/module.h>
11#include <linux/errno.h>
12#include <linux/init.h>
13#include <linux/types.h>
14#include <linux/interrupt.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/string.h>
16#include <linux/delay.h>
17#include <linux/netdevice.h>
Ralf Baechledf9f5402007-05-11 15:48:58 +010018#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070019#include <linux/etherdevice.h>
20#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <asm/sgi/hpc3.h>
23#include <asm/sgi/ip22.h>
Ralf Baechledf9f5402007-05-11 15:48:58 +010024#include <asm/sgi/seeq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
26#include "sgiseeq.h"
27
Linus Torvalds1da177e2005-04-16 15:20:36 -070028static char *sgiseeqstr = "SGI Seeq8003";
29
30/*
31 * If you want speed, you do something silly, it always has worked for me. So,
32 * with that in mind, I've decided to make this driver look completely like a
33 * stupid Lance from a driver architecture perspective. Only difference is that
34 * here our "ring buffer" looks and acts like a real Lance one does but is
35 * layed out like how the HPC DMA and the Seeq want it to. You'd be surprised
36 * how a stupid idea like this can pay off in performance, not to mention
37 * making this driver 2,000 times easier to write. ;-)
38 */
39
40/* Tune these if we tend to run out often etc. */
41#define SEEQ_RX_BUFFERS 16
42#define SEEQ_TX_BUFFERS 16
43
44#define PKT_BUF_SZ 1584
45
46#define NEXT_RX(i) (((i) + 1) & (SEEQ_RX_BUFFERS - 1))
47#define NEXT_TX(i) (((i) + 1) & (SEEQ_TX_BUFFERS - 1))
48#define PREV_RX(i) (((i) - 1) & (SEEQ_RX_BUFFERS - 1))
49#define PREV_TX(i) (((i) - 1) & (SEEQ_TX_BUFFERS - 1))
50
51#define TX_BUFFS_AVAIL(sp) ((sp->tx_old <= sp->tx_new) ? \
52 sp->tx_old + (SEEQ_TX_BUFFERS - 1) - sp->tx_new : \
53 sp->tx_old - sp->tx_new - 1)
54
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +010055#define VIRT_TO_DMA(sp, v) ((sp)->srings_dma + \
56 (dma_addr_t)((unsigned long)(v) - \
57 (unsigned long)((sp)->rx_desc)))
58
59#define DMA_SYNC_DESC_CPU(dev, addr) \
60 do { dma_cache_sync((dev)->dev.parent, (void *)addr, \
61 sizeof(struct sgiseeq_rx_desc), DMA_FROM_DEVICE); } while (0)
62
63#define DMA_SYNC_DESC_DEV(dev, addr) \
64 do { dma_cache_sync((dev)->dev.parent, (void *)addr, \
65 sizeof(struct sgiseeq_rx_desc), DMA_TO_DEVICE); } while (0)
66
67/* Copy frames shorter than rx_copybreak, otherwise pass on up in
68 * a full sized sk_buff. Value of 100 stolen from tulip.c (!alpha).
69 */
70static int rx_copybreak = 100;
71
72#define PAD_SIZE (128 - sizeof(struct hpc_dma_desc) - sizeof(void *))
73
Linus Torvalds1da177e2005-04-16 15:20:36 -070074struct sgiseeq_rx_desc {
75 volatile struct hpc_dma_desc rdma;
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +010076 u8 padding[PAD_SIZE];
77 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070078};
79
80struct sgiseeq_tx_desc {
81 volatile struct hpc_dma_desc tdma;
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +010082 u8 padding[PAD_SIZE];
83 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084};
85
86/*
87 * Warning: This structure is layed out in a certain way because HPC dma
88 * descriptors must be 8-byte aligned. So don't touch this without
89 * some care.
90 */
91struct sgiseeq_init_block { /* Note the name ;-) */
92 struct sgiseeq_rx_desc rxvector[SEEQ_RX_BUFFERS];
93 struct sgiseeq_tx_desc txvector[SEEQ_TX_BUFFERS];
94};
95
96struct sgiseeq_private {
97 struct sgiseeq_init_block *srings;
Ralf Baechle99cd1492007-09-04 14:41:01 +010098 dma_addr_t srings_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -070099
100 /* Ptrs to the descriptors in uncached space. */
101 struct sgiseeq_rx_desc *rx_desc;
102 struct sgiseeq_tx_desc *tx_desc;
103
104 char *name;
105 struct hpc3_ethregs *hregs;
106 struct sgiseeq_regs *sregs;
107
108 /* Ring entry counters. */
109 unsigned int rx_new, tx_new;
110 unsigned int rx_old, tx_old;
111
112 int is_edlc;
113 unsigned char control;
114 unsigned char mode;
115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 spinlock_t tx_lock;
117};
118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119static inline void hpc3_eth_reset(struct hpc3_ethregs *hregs)
120{
Ralf Baechle302a5c4b2005-10-10 14:50:56 +0100121 hregs->reset = HPC3_ERST_CRESET | HPC3_ERST_CLRIRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 udelay(20);
Ralf Baechle302a5c4b2005-10-10 14:50:56 +0100123 hregs->reset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124}
125
126static inline void reset_hpc3_and_seeq(struct hpc3_ethregs *hregs,
127 struct sgiseeq_regs *sregs)
128{
129 hregs->rx_ctrl = hregs->tx_ctrl = 0;
130 hpc3_eth_reset(hregs);
131}
132
133#define RSTAT_GO_BITS (SEEQ_RCMD_IGOOD | SEEQ_RCMD_IEOF | SEEQ_RCMD_ISHORT | \
134 SEEQ_RCMD_IDRIB | SEEQ_RCMD_ICRC)
135
136static inline void seeq_go(struct sgiseeq_private *sp,
137 struct hpc3_ethregs *hregs,
138 struct sgiseeq_regs *sregs)
139{
140 sregs->rstat = sp->mode | RSTAT_GO_BITS;
141 hregs->rx_ctrl = HPC3_ERXCTRL_ACTIVE;
142}
143
144static inline void __sgiseeq_set_mac_address(struct net_device *dev)
145{
146 struct sgiseeq_private *sp = netdev_priv(dev);
147 struct sgiseeq_regs *sregs = sp->sregs;
148 int i;
149
150 sregs->tstat = SEEQ_TCMD_RB0;
151 for (i = 0; i < 6; i++)
152 sregs->rw.eth_addr[i] = dev->dev_addr[i];
153}
154
155static int sgiseeq_set_mac_address(struct net_device *dev, void *addr)
156{
157 struct sgiseeq_private *sp = netdev_priv(dev);
158 struct sockaddr *sa = addr;
159
160 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
161
162 spin_lock_irq(&sp->tx_lock);
163 __sgiseeq_set_mac_address(dev);
164 spin_unlock_irq(&sp->tx_lock);
165
166 return 0;
167}
168
169#define TCNTINFO_INIT (HPCDMA_EOX | HPCDMA_ETXD)
170#define RCNTCFG_INIT (HPCDMA_OWN | HPCDMA_EORP | HPCDMA_XIE)
171#define RCNTINFO_INIT (RCNTCFG_INIT | (PKT_BUF_SZ & HPCDMA_BCNT))
172
173static int seeq_init_ring(struct net_device *dev)
174{
175 struct sgiseeq_private *sp = netdev_priv(dev);
176 int i;
177
178 netif_stop_queue(dev);
179 sp->rx_new = sp->tx_new = 0;
180 sp->rx_old = sp->tx_old = 0;
181
182 __sgiseeq_set_mac_address(dev);
183
184 /* Setup tx ring. */
185 for(i = 0; i < SEEQ_TX_BUFFERS; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 sp->tx_desc[i].tdma.cntinfo = TCNTINFO_INIT;
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100187 DMA_SYNC_DESC_DEV(dev, &sp->tx_desc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189
190 /* And now the rx ring. */
191 for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
192 if (!sp->rx_desc[i].rdma.pbuf) {
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100193 dma_addr_t dma_addr;
194 struct sk_buff *skb = netdev_alloc_skb(dev, PKT_BUF_SZ);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100196 if (skb == NULL)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 return -ENOMEM;
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100198 skb_reserve(skb, 2);
199 dma_addr = dma_map_single(dev->dev.parent,
200 skb->data - 2,
201 PKT_BUF_SZ, DMA_FROM_DEVICE);
202 sp->rx_desc[i].skb = skb;
203 sp->rx_desc[i].rdma.pbuf = dma_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 }
205 sp->rx_desc[i].rdma.cntinfo = RCNTINFO_INIT;
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100206 DMA_SYNC_DESC_DEV(dev, &sp->rx_desc[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 }
208 sp->rx_desc[i - 1].rdma.cntinfo |= HPCDMA_EOR;
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100209 DMA_SYNC_DESC_DEV(dev, &sp->rx_desc[i - 1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 return 0;
211}
212
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100213static void seeq_purge_ring(struct net_device *dev)
214{
215 struct sgiseeq_private *sp = netdev_priv(dev);
216 int i;
217
218 /* clear tx ring. */
219 for (i = 0; i < SEEQ_TX_BUFFERS; i++) {
220 if (sp->tx_desc[i].skb) {
221 dev_kfree_skb(sp->tx_desc[i].skb);
222 sp->tx_desc[i].skb = NULL;
223 }
224 }
225
226 /* And now the rx ring. */
227 for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
228 if (sp->rx_desc[i].skb) {
229 dev_kfree_skb(sp->rx_desc[i].skb);
230 sp->rx_desc[i].skb = NULL;
231 }
232 }
233}
234
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235#ifdef DEBUG
236static struct sgiseeq_private *gpriv;
237static struct net_device *gdev;
238
Ralf Baechlea4d4d512006-03-08 11:49:31 +0000239static void sgiseeq_dump_rings(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240{
241 static int once;
242 struct sgiseeq_rx_desc *r = gpriv->rx_desc;
243 struct sgiseeq_tx_desc *t = gpriv->tx_desc;
244 struct hpc3_ethregs *hregs = gpriv->hregs;
245 int i;
246
247 if (once)
248 return;
249 once++;
250 printk("RING DUMP:\n");
251 for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
252 printk("RX [%d]: @(%p) [%08x,%08x,%08x] ",
253 i, (&r[i]), r[i].rdma.pbuf, r[i].rdma.cntinfo,
254 r[i].rdma.pnext);
255 i += 1;
256 printk("-- [%d]: @(%p) [%08x,%08x,%08x]\n",
257 i, (&r[i]), r[i].rdma.pbuf, r[i].rdma.cntinfo,
258 r[i].rdma.pnext);
259 }
260 for (i = 0; i < SEEQ_TX_BUFFERS; i++) {
261 printk("TX [%d]: @(%p) [%08x,%08x,%08x] ",
262 i, (&t[i]), t[i].tdma.pbuf, t[i].tdma.cntinfo,
263 t[i].tdma.pnext);
264 i += 1;
265 printk("-- [%d]: @(%p) [%08x,%08x,%08x]\n",
266 i, (&t[i]), t[i].tdma.pbuf, t[i].tdma.cntinfo,
267 t[i].tdma.pnext);
268 }
269 printk("INFO: [rx_new = %d rx_old=%d] [tx_new = %d tx_old = %d]\n",
270 gpriv->rx_new, gpriv->rx_old, gpriv->tx_new, gpriv->tx_old);
271 printk("RREGS: rx_cbptr[%08x] rx_ndptr[%08x] rx_ctrl[%08x]\n",
272 hregs->rx_cbptr, hregs->rx_ndptr, hregs->rx_ctrl);
273 printk("TREGS: tx_cbptr[%08x] tx_ndptr[%08x] tx_ctrl[%08x]\n",
274 hregs->tx_cbptr, hregs->tx_ndptr, hregs->tx_ctrl);
275}
276#endif
277
278#define TSTAT_INIT_SEEQ (SEEQ_TCMD_IPT|SEEQ_TCMD_I16|SEEQ_TCMD_IC|SEEQ_TCMD_IUF)
279#define TSTAT_INIT_EDLC ((TSTAT_INIT_SEEQ) | SEEQ_TCMD_RB2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280
281static int init_seeq(struct net_device *dev, struct sgiseeq_private *sp,
282 struct sgiseeq_regs *sregs)
283{
284 struct hpc3_ethregs *hregs = sp->hregs;
285 int err;
286
287 reset_hpc3_and_seeq(hregs, sregs);
288 err = seeq_init_ring(dev);
289 if (err)
290 return err;
291
292 /* Setup to field the proper interrupt types. */
293 if (sp->is_edlc) {
294 sregs->tstat = TSTAT_INIT_EDLC;
295 sregs->rw.wregs.control = sp->control;
296 sregs->rw.wregs.frame_gap = 0;
297 } else {
298 sregs->tstat = TSTAT_INIT_SEEQ;
299 }
300
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100301 hregs->rx_ndptr = VIRT_TO_DMA(sp, sp->rx_desc);
302 hregs->tx_ndptr = VIRT_TO_DMA(sp, sp->tx_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303
304 seeq_go(sp, hregs, sregs);
305 return 0;
306}
307
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700308static void record_rx_errors(struct net_device *dev, unsigned char status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
310 if (status & SEEQ_RSTAT_OVERF ||
311 status & SEEQ_RSTAT_SFRAME)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700312 dev->stats.rx_over_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 if (status & SEEQ_RSTAT_CERROR)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700314 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 if (status & SEEQ_RSTAT_DERROR)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700316 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 if (status & SEEQ_RSTAT_REOF)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700318 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319}
320
321static inline void rx_maybe_restart(struct sgiseeq_private *sp,
322 struct hpc3_ethregs *hregs,
323 struct sgiseeq_regs *sregs)
324{
325 if (!(hregs->rx_ctrl & HPC3_ERXCTRL_ACTIVE)) {
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100326 hregs->rx_ndptr = VIRT_TO_DMA(sp, sp->rx_desc + sp->rx_new);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 seeq_go(sp, hregs, sregs);
328 }
329}
330
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331static inline void sgiseeq_rx(struct net_device *dev, struct sgiseeq_private *sp,
332 struct hpc3_ethregs *hregs,
333 struct sgiseeq_regs *sregs)
334{
335 struct sgiseeq_rx_desc *rd;
Ralf Baechlea4d4d512006-03-08 11:49:31 +0000336 struct sk_buff *skb = NULL;
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100337 struct sk_buff *newskb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 unsigned char pkt_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 int len = 0;
340 unsigned int orig_end = PREV_RX(sp->rx_new);
341
342 /* Service every received packet. */
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100343 rd = &sp->rx_desc[sp->rx_new];
344 DMA_SYNC_DESC_CPU(dev, rd);
345 while (!(rd->rdma.cntinfo & HPCDMA_OWN)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 len = PKT_BUF_SZ - (rd->rdma.cntinfo & HPCDMA_BCNT) - 3;
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100347 dma_unmap_single(dev->dev.parent, rd->rdma.pbuf,
348 PKT_BUF_SZ, DMA_FROM_DEVICE);
349 pkt_status = rd->skb->data[len];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 if (pkt_status & SEEQ_RSTAT_FIG) {
351 /* Packet is OK. */
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100352 /* We don't want to receive our own packets */
353 if (memcmp(rd->skb->data + 6, dev->dev_addr, ETH_ALEN)) {
354 if (len > rx_copybreak) {
355 skb = rd->skb;
356 newskb = netdev_alloc_skb(dev, PKT_BUF_SZ);
357 if (!newskb) {
358 newskb = skb;
359 skb = NULL;
360 goto memory_squeeze;
361 }
362 skb_reserve(newskb, 2);
363 } else {
364 skb = netdev_alloc_skb(dev, len + 2);
365 if (skb) {
366 skb_reserve(skb, 2);
367 skb_copy_to_linear_data(skb, rd->skb->data, len);
368 }
369 newskb = rd->skb;
370 }
371memory_squeeze:
372 if (skb) {
373 skb_put(skb, len);
374 skb->protocol = eth_type_trans(skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 netif_rx(skb);
376 dev->last_rx = jiffies;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700377 dev->stats.rx_packets++;
378 dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 } else {
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100380 printk(KERN_NOTICE "%s: Memory squeeze, deferring packet.\n",
381 dev->name);
382 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 }
384 } else {
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100385 /* Silently drop my own packets */
386 newskb = rd->skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 }
388 } else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700389 record_rx_errors(dev, pkt_status);
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100390 newskb = rd->skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100392 rd->skb = newskb;
393 rd->rdma.pbuf = dma_map_single(dev->dev.parent,
394 newskb->data - 2,
395 PKT_BUF_SZ, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
397 /* Return the entry to the ring pool. */
398 rd->rdma.cntinfo = RCNTINFO_INIT;
399 sp->rx_new = NEXT_RX(sp->rx_new);
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100400 DMA_SYNC_DESC_DEV(dev, rd);
401 rd = &sp->rx_desc[sp->rx_new];
402 DMA_SYNC_DESC_CPU(dev, rd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 }
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100404 DMA_SYNC_DESC_CPU(dev, &sp->rx_desc[orig_end]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 sp->rx_desc[orig_end].rdma.cntinfo &= ~(HPCDMA_EOR);
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100406 DMA_SYNC_DESC_DEV(dev, &sp->rx_desc[orig_end]);
407 DMA_SYNC_DESC_CPU(dev, &sp->rx_desc[PREV_RX(sp->rx_new)]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700408 sp->rx_desc[PREV_RX(sp->rx_new)].rdma.cntinfo |= HPCDMA_EOR;
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100409 DMA_SYNC_DESC_DEV(dev, &sp->rx_desc[PREV_RX(sp->rx_new)]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 rx_maybe_restart(sp, hregs, sregs);
411}
412
413static inline void tx_maybe_reset_collisions(struct sgiseeq_private *sp,
414 struct sgiseeq_regs *sregs)
415{
416 if (sp->is_edlc) {
417 sregs->rw.wregs.control = sp->control & ~(SEEQ_CTRL_XCNT);
418 sregs->rw.wregs.control = sp->control;
419 }
420}
421
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100422static inline void kick_tx(struct net_device *dev,
423 struct sgiseeq_private *sp,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 struct hpc3_ethregs *hregs)
425{
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100426 struct sgiseeq_tx_desc *td;
427 int i = sp->tx_old;
428
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 /* If the HPC aint doin nothin, and there are more packets
430 * with ETXD cleared and XIU set we must make very certain
431 * that we restart the HPC else we risk locking up the
432 * adapter. The following code is only safe iff the HPCDMA
433 * is not active!
434 */
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100435 td = &sp->tx_desc[i];
436 DMA_SYNC_DESC_CPU(dev, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 while ((td->tdma.cntinfo & (HPCDMA_XIU | HPCDMA_ETXD)) ==
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100438 (HPCDMA_XIU | HPCDMA_ETXD)) {
439 i = NEXT_TX(i);
440 td = &sp->tx_desc[i];
441 DMA_SYNC_DESC_CPU(dev, td);
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 if (td->tdma.cntinfo & HPCDMA_XIU) {
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100444 hregs->tx_ndptr = VIRT_TO_DMA(sp, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 hregs->tx_ctrl = HPC3_ETXCTRL_ACTIVE;
446 }
447}
448
449static inline void sgiseeq_tx(struct net_device *dev, struct sgiseeq_private *sp,
450 struct hpc3_ethregs *hregs,
451 struct sgiseeq_regs *sregs)
452{
453 struct sgiseeq_tx_desc *td;
454 unsigned long status = hregs->tx_ctrl;
455 int j;
456
457 tx_maybe_reset_collisions(sp, sregs);
458
459 if (!(status & (HPC3_ETXCTRL_ACTIVE | SEEQ_TSTAT_PTRANS))) {
460 /* Oops, HPC detected some sort of error. */
461 if (status & SEEQ_TSTAT_R16)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700462 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700463 if (status & SEEQ_TSTAT_UFLOW)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700464 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465 if (status & SEEQ_TSTAT_LCLS)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700466 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 }
468
469 /* Ack 'em... */
470 for (j = sp->tx_old; j != sp->tx_new; j = NEXT_TX(j)) {
471 td = &sp->tx_desc[j];
472
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100473 DMA_SYNC_DESC_CPU(dev, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474 if (!(td->tdma.cntinfo & (HPCDMA_XIU)))
475 break;
476 if (!(td->tdma.cntinfo & (HPCDMA_ETXD))) {
477 if (!(status & HPC3_ETXCTRL_ACTIVE)) {
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100478 hregs->tx_ndptr = VIRT_TO_DMA(sp, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479 hregs->tx_ctrl = HPC3_ETXCTRL_ACTIVE;
480 }
481 break;
482 }
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700483 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 sp->tx_old = NEXT_TX(sp->tx_old);
485 td->tdma.cntinfo &= ~(HPCDMA_XIU | HPCDMA_XIE);
486 td->tdma.cntinfo |= HPCDMA_EOX;
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100487 if (td->skb) {
488 dev_kfree_skb_any(td->skb);
489 td->skb = NULL;
490 }
491 DMA_SYNC_DESC_DEV(dev, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 }
493}
494
David Howells7d12e782006-10-05 14:55:46 +0100495static irqreturn_t sgiseeq_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
497 struct net_device *dev = (struct net_device *) dev_id;
498 struct sgiseeq_private *sp = netdev_priv(dev);
499 struct hpc3_ethregs *hregs = sp->hregs;
500 struct sgiseeq_regs *sregs = sp->sregs;
501
502 spin_lock(&sp->tx_lock);
503
504 /* Ack the IRQ and set software state. */
Ralf Baechle302a5c4b2005-10-10 14:50:56 +0100505 hregs->reset = HPC3_ERST_CLRIRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 /* Always check for received packets. */
508 sgiseeq_rx(dev, sp, hregs, sregs);
509
510 /* Only check for tx acks if we have something queued. */
511 if (sp->tx_old != sp->tx_new)
512 sgiseeq_tx(dev, sp, hregs, sregs);
513
514 if ((TX_BUFFS_AVAIL(sp) > 0) && netif_queue_stopped(dev)) {
515 netif_wake_queue(dev);
516 }
517 spin_unlock(&sp->tx_lock);
518
519 return IRQ_HANDLED;
520}
521
522static int sgiseeq_open(struct net_device *dev)
523{
524 struct sgiseeq_private *sp = netdev_priv(dev);
525 struct sgiseeq_regs *sregs = sp->sregs;
526 unsigned int irq = dev->irq;
527 int err;
528
529 if (request_irq(irq, sgiseeq_interrupt, 0, sgiseeqstr, dev)) {
530 printk(KERN_ERR "Seeq8003: Can't get irq %d\n", dev->irq);
531 err = -EAGAIN;
532 }
533
534 err = init_seeq(dev, sp, sregs);
535 if (err)
536 goto out_free_irq;
537
538 netif_start_queue(dev);
539
540 return 0;
541
542out_free_irq:
543 free_irq(irq, dev);
544
545 return err;
546}
547
548static int sgiseeq_close(struct net_device *dev)
549{
550 struct sgiseeq_private *sp = netdev_priv(dev);
551 struct sgiseeq_regs *sregs = sp->sregs;
Ralf Baechle28914392005-10-10 14:50:51 +0100552 unsigned int irq = dev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554 netif_stop_queue(dev);
555
556 /* Shutdown the Seeq. */
557 reset_hpc3_and_seeq(sp->hregs, sregs);
Ralf Baechle28914392005-10-10 14:50:51 +0100558 free_irq(irq, dev);
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100559 seeq_purge_ring(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 return 0;
562}
563
564static inline int sgiseeq_reset(struct net_device *dev)
565{
566 struct sgiseeq_private *sp = netdev_priv(dev);
567 struct sgiseeq_regs *sregs = sp->sregs;
568 int err;
569
570 err = init_seeq(dev, sp, sregs);
571 if (err)
572 return err;
573
574 dev->trans_start = jiffies;
575 netif_wake_queue(dev);
576
577 return 0;
578}
579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580static int sgiseeq_start_xmit(struct sk_buff *skb, struct net_device *dev)
581{
582 struct sgiseeq_private *sp = netdev_priv(dev);
583 struct hpc3_ethregs *hregs = sp->hregs;
584 unsigned long flags;
585 struct sgiseeq_tx_desc *td;
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100586 int len, entry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588 spin_lock_irqsave(&sp->tx_lock, flags);
589
590 /* Setup... */
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100591 len = skb->len;
592 if (len < ETH_ZLEN) {
593 if (skb_padto(skb, ETH_ZLEN))
594 return 0;
595 len = ETH_ZLEN;
596 }
597
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700598 dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 entry = sp->tx_new;
600 td = &sp->tx_desc[entry];
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100601 DMA_SYNC_DESC_CPU(dev, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602
603 /* Create entry. There are so many races with adding a new
604 * descriptor to the chain:
605 * 1) Assume that the HPC is off processing a DMA chain while
606 * we are changing all of the following.
607 * 2) Do no allow the HPC to look at a new descriptor until
608 * we have completely set up it's state. This means, do
609 * not clear HPCDMA_EOX in the current last descritptor
610 * until the one we are adding looks consistent and could
611 * be processes right now.
612 * 3) The tx interrupt code must notice when we've added a new
613 * entry and the HPC got to the end of the chain before we
614 * added this new entry and restarted it.
615 */
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100616 td->skb = skb;
617 td->tdma.pbuf = dma_map_single(dev->dev.parent, skb->data,
618 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 td->tdma.cntinfo = (len & HPCDMA_BCNT) |
620 HPCDMA_XIU | HPCDMA_EOXP | HPCDMA_XIE | HPCDMA_EOX;
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100621 DMA_SYNC_DESC_DEV(dev, td);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 if (sp->tx_old != sp->tx_new) {
623 struct sgiseeq_tx_desc *backend;
624
625 backend = &sp->tx_desc[PREV_TX(sp->tx_new)];
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100626 DMA_SYNC_DESC_CPU(dev, backend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627 backend->tdma.cntinfo &= ~HPCDMA_EOX;
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100628 DMA_SYNC_DESC_DEV(dev, backend);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 }
630 sp->tx_new = NEXT_TX(sp->tx_new); /* Advance. */
631
632 /* Maybe kick the HPC back into motion. */
633 if (!(hregs->tx_ctrl & HPC3_ETXCTRL_ACTIVE))
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100634 kick_tx(dev, sp, hregs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 dev->trans_start = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 if (!TX_BUFFS_AVAIL(sp))
639 netif_stop_queue(dev);
640 spin_unlock_irqrestore(&sp->tx_lock, flags);
641
642 return 0;
643}
644
645static void timeout(struct net_device *dev)
646{
647 printk(KERN_NOTICE "%s: transmit timed out, resetting\n", dev->name);
648 sgiseeq_reset(dev);
649
650 dev->trans_start = jiffies;
651 netif_wake_queue(dev);
652}
653
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654static void sgiseeq_set_multicast(struct net_device *dev)
655{
656 struct sgiseeq_private *sp = (struct sgiseeq_private *) dev->priv;
657 unsigned char oldmode = sp->mode;
658
659 if(dev->flags & IFF_PROMISC)
660 sp->mode = SEEQ_RCMD_RANY;
661 else if ((dev->flags & IFF_ALLMULTI) || dev->mc_count)
662 sp->mode = SEEQ_RCMD_RBMCAST;
663 else
664 sp->mode = SEEQ_RCMD_RBCAST;
665
666 /* XXX I know this sucks, but is there a better way to reprogram
667 * XXX the receiver? At least, this shouldn't happen too often.
668 */
669
670 if (oldmode != sp->mode)
671 sgiseeq_reset(dev);
672}
673
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100674static inline void setup_tx_ring(struct net_device *dev,
675 struct sgiseeq_tx_desc *buf,
676 int nbufs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677{
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100678 struct sgiseeq_private *sp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 int i = 0;
680
681 while (i < (nbufs - 1)) {
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100682 buf[i].tdma.pnext = VIRT_TO_DMA(sp, buf + i + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 buf[i].tdma.pbuf = 0;
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100684 DMA_SYNC_DESC_DEV(dev, &buf[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 i++;
686 }
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100687 buf[i].tdma.pnext = VIRT_TO_DMA(sp, buf);
688 DMA_SYNC_DESC_DEV(dev, &buf[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689}
690
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100691static inline void setup_rx_ring(struct net_device *dev,
692 struct sgiseeq_rx_desc *buf,
693 int nbufs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694{
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100695 struct sgiseeq_private *sp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 int i = 0;
697
698 while (i < (nbufs - 1)) {
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100699 buf[i].rdma.pnext = VIRT_TO_DMA(sp, buf + i + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 buf[i].rdma.pbuf = 0;
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100701 DMA_SYNC_DESC_DEV(dev, &buf[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 i++;
703 }
704 buf[i].rdma.pbuf = 0;
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100705 buf[i].rdma.pnext = VIRT_TO_DMA(sp, buf);
706 DMA_SYNC_DESC_DEV(dev, &buf[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707}
708
Ralf Baechledf9f5402007-05-11 15:48:58 +0100709static int __init sgiseeq_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Ralf Baechledf9f5402007-05-11 15:48:58 +0100711 struct sgiseeq_platform_data *pd = pdev->dev.platform_data;
712 struct hpc3_regs *hpcregs = pd->hpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 struct sgiseeq_init_block *sr;
Ralf Baechledf9f5402007-05-11 15:48:58 +0100714 unsigned int irq = pd->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 struct sgiseeq_private *sp;
716 struct net_device *dev;
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100717 int err;
Joe Perches0795af52007-10-03 17:59:30 -0700718 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
720 dev = alloc_etherdev(sizeof (struct sgiseeq_private));
721 if (!dev) {
722 printk(KERN_ERR "Sgiseeq: Etherdev alloc failed, aborting.\n");
723 err = -ENOMEM;
724 goto err_out;
725 }
Ralf Baechledf9f5402007-05-11 15:48:58 +0100726
727 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 sp = netdev_priv(dev);
729
730 /* Make private data page aligned */
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100731 sr = dma_alloc_noncoherent(&pdev->dev, sizeof(*sp->srings),
Ralf Baechle99cd1492007-09-04 14:41:01 +0100732 &sp->srings_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 if (!sr) {
734 printk(KERN_ERR "Sgiseeq: Page alloc failed, aborting.\n");
735 err = -ENOMEM;
736 goto err_out_free_dev;
737 }
738 sp->srings = sr;
Ralf Baechle99cd1492007-09-04 14:41:01 +0100739 sp->rx_desc = sp->srings->rxvector;
740 sp->tx_desc = sp->srings->txvector;
741
742 /* A couple calculations now, saves many cycles later. */
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100743 setup_rx_ring(dev, sp->rx_desc, SEEQ_RX_BUFFERS);
744 setup_tx_ring(dev, sp->tx_desc, SEEQ_TX_BUFFERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Ralf Baechledf9f5402007-05-11 15:48:58 +0100746 memcpy(dev->dev_addr, pd->mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748#ifdef DEBUG
749 gpriv = sp;
750 gdev = dev;
751#endif
Ralf Baechle302a5c4b2005-10-10 14:50:56 +0100752 sp->sregs = (struct sgiseeq_regs *) &hpcregs->eth_ext[0];
753 sp->hregs = &hpcregs->ethregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 sp->name = sgiseeqstr;
755 sp->mode = SEEQ_RCMD_RBCAST;
756
Ralf Baechle302a5c4b2005-10-10 14:50:56 +0100757 /* Setup PIO and DMA transfer timing */
758 sp->hregs->pconfig = 0x161;
759 sp->hregs->dconfig = HPC3_EDCFG_FIRQ | HPC3_EDCFG_FEOP |
760 HPC3_EDCFG_FRXDC | HPC3_EDCFG_PTO | 0x026;
761
Ladislav Michl78ee5b32007-02-28 01:18:35 +0000762 /* Setup PIO and DMA transfer timing */
763 sp->hregs->pconfig = 0x161;
764 sp->hregs->dconfig = HPC3_EDCFG_FIRQ | HPC3_EDCFG_FEOP |
765 HPC3_EDCFG_FRXDC | HPC3_EDCFG_PTO | 0x026;
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 /* Reset the chip. */
768 hpc3_eth_reset(sp->hregs);
769
770 sp->is_edlc = !(sp->sregs->rw.rregs.collision_tx[0] & 0xff);
771 if (sp->is_edlc)
772 sp->control = SEEQ_CTRL_XCNT | SEEQ_CTRL_ACCNT |
773 SEEQ_CTRL_SFLAG | SEEQ_CTRL_ESHORT |
774 SEEQ_CTRL_ENCARR;
775
776 dev->open = sgiseeq_open;
777 dev->stop = sgiseeq_close;
778 dev->hard_start_xmit = sgiseeq_start_xmit;
779 dev->tx_timeout = timeout;
780 dev->watchdog_timeo = (200 * HZ) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 dev->set_multicast_list = sgiseeq_set_multicast;
782 dev->set_mac_address = sgiseeq_set_mac_address;
783 dev->irq = irq;
784
785 if (register_netdev(dev)) {
786 printk(KERN_ERR "Sgiseeq: Cannot register net device, "
787 "aborting.\n");
788 err = -ENODEV;
789 goto err_out_free_page;
790 }
791
Joe Perches0795af52007-10-03 17:59:30 -0700792 printk(KERN_INFO "%s: %s %s\n",
793 dev->name, sgiseeqstr, print_mac(mac, dev->dev_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 return 0;
796
797err_out_free_page:
Ralf Baechle28914392005-10-10 14:50:51 +0100798 free_page((unsigned long) sp->srings);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799err_out_free_dev:
800 kfree(dev);
801
802err_out:
803 return err;
804}
805
Ralf Baechlee3efb052007-08-22 16:03:52 +0100806static int __exit sgiseeq_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
Ralf Baechledf9f5402007-05-11 15:48:58 +0100808 struct net_device *dev = platform_get_drvdata(pdev);
809 struct sgiseeq_private *sp = netdev_priv(dev);
Ladislav Michl78ee5b32007-02-28 01:18:35 +0000810
Ralf Baechledf9f5402007-05-11 15:48:58 +0100811 unregister_netdev(dev);
Thomas Bogendoerfer43831b12007-11-24 13:29:19 +0100812 dma_free_noncoherent(&pdev->dev, sizeof(*sp->srings), sp->srings,
813 sp->srings_dma);
Ralf Baechledf9f5402007-05-11 15:48:58 +0100814 free_netdev(dev);
815 platform_set_drvdata(pdev, NULL);
Ralf Baechlee3efb052007-08-22 16:03:52 +0100816
817 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Ralf Baechledf9f5402007-05-11 15:48:58 +0100820static struct platform_driver sgiseeq_driver = {
821 .probe = sgiseeq_probe,
822 .remove = __devexit_p(sgiseeq_remove),
823 .driver = {
824 .name = "sgiseeq"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 }
Ralf Baechledf9f5402007-05-11 15:48:58 +0100826};
827
828static int __init sgiseeq_module_init(void)
829{
830 if (platform_driver_register(&sgiseeq_driver)) {
831 printk(KERN_ERR "Driver registration failed\n");
832 return -ENODEV;
833 }
834
835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Ralf Baechledf9f5402007-05-11 15:48:58 +0100838static void __exit sgiseeq_module_exit(void)
839{
840 platform_driver_unregister(&sgiseeq_driver);
841}
842
843module_init(sgiseeq_module_init);
844module_exit(sgiseeq_module_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845
Ralf Baechle302a5c4b2005-10-10 14:50:56 +0100846MODULE_DESCRIPTION("SGI Seeq 8003 driver");
847MODULE_AUTHOR("Linux/MIPS Mailing List <linux-mips@linux-mips.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848MODULE_LICENSE("GPL");