blob: ff4056310356a8e04b2b33634d1098e474347b2c [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/slab.h>
16#include <linux/string.h>
17#include <linux/delay.h>
18#include <linux/netdevice.h>
Ralf Baechledf9f5402007-05-11 15:48:58 +010019#include <linux/platform_device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/etherdevice.h>
21#include <linux/skbuff.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <asm/sgi/hpc3.h>
24#include <asm/sgi/ip22.h>
Ralf Baechledf9f5402007-05-11 15:48:58 +010025#include <asm/sgi/seeq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070026
27#include "sgiseeq.h"
28
Linus Torvalds1da177e2005-04-16 15:20:36 -070029static char *sgiseeqstr = "SGI Seeq8003";
30
31/*
32 * If you want speed, you do something silly, it always has worked for me. So,
33 * with that in mind, I've decided to make this driver look completely like a
34 * stupid Lance from a driver architecture perspective. Only difference is that
35 * here our "ring buffer" looks and acts like a real Lance one does but is
36 * layed out like how the HPC DMA and the Seeq want it to. You'd be surprised
37 * how a stupid idea like this can pay off in performance, not to mention
38 * making this driver 2,000 times easier to write. ;-)
39 */
40
41/* Tune these if we tend to run out often etc. */
42#define SEEQ_RX_BUFFERS 16
43#define SEEQ_TX_BUFFERS 16
44
45#define PKT_BUF_SZ 1584
46
47#define NEXT_RX(i) (((i) + 1) & (SEEQ_RX_BUFFERS - 1))
48#define NEXT_TX(i) (((i) + 1) & (SEEQ_TX_BUFFERS - 1))
49#define PREV_RX(i) (((i) - 1) & (SEEQ_RX_BUFFERS - 1))
50#define PREV_TX(i) (((i) - 1) & (SEEQ_TX_BUFFERS - 1))
51
52#define TX_BUFFS_AVAIL(sp) ((sp->tx_old <= sp->tx_new) ? \
53 sp->tx_old + (SEEQ_TX_BUFFERS - 1) - sp->tx_new : \
54 sp->tx_old - sp->tx_new - 1)
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056struct sgiseeq_rx_desc {
57 volatile struct hpc_dma_desc rdma;
58 volatile signed int buf_vaddr;
59};
60
61struct sgiseeq_tx_desc {
62 volatile struct hpc_dma_desc tdma;
63 volatile signed int buf_vaddr;
64};
65
66/*
67 * Warning: This structure is layed out in a certain way because HPC dma
68 * descriptors must be 8-byte aligned. So don't touch this without
69 * some care.
70 */
71struct sgiseeq_init_block { /* Note the name ;-) */
72 struct sgiseeq_rx_desc rxvector[SEEQ_RX_BUFFERS];
73 struct sgiseeq_tx_desc txvector[SEEQ_TX_BUFFERS];
74};
75
76struct sgiseeq_private {
77 struct sgiseeq_init_block *srings;
Ralf Baechle99cd1492007-09-04 14:41:01 +010078 dma_addr_t srings_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80 /* Ptrs to the descriptors in uncached space. */
81 struct sgiseeq_rx_desc *rx_desc;
82 struct sgiseeq_tx_desc *tx_desc;
83
84 char *name;
85 struct hpc3_ethregs *hregs;
86 struct sgiseeq_regs *sregs;
87
88 /* Ring entry counters. */
89 unsigned int rx_new, tx_new;
90 unsigned int rx_old, tx_old;
91
92 int is_edlc;
93 unsigned char control;
94 unsigned char mode;
95
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 spinlock_t tx_lock;
97};
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099static inline void hpc3_eth_reset(struct hpc3_ethregs *hregs)
100{
Ralf Baechle302a5c4b2005-10-10 14:50:56 +0100101 hregs->reset = HPC3_ERST_CRESET | HPC3_ERST_CLRIRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 udelay(20);
Ralf Baechle302a5c4b2005-10-10 14:50:56 +0100103 hregs->reset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104}
105
106static inline void reset_hpc3_and_seeq(struct hpc3_ethregs *hregs,
107 struct sgiseeq_regs *sregs)
108{
109 hregs->rx_ctrl = hregs->tx_ctrl = 0;
110 hpc3_eth_reset(hregs);
111}
112
113#define RSTAT_GO_BITS (SEEQ_RCMD_IGOOD | SEEQ_RCMD_IEOF | SEEQ_RCMD_ISHORT | \
114 SEEQ_RCMD_IDRIB | SEEQ_RCMD_ICRC)
115
116static inline void seeq_go(struct sgiseeq_private *sp,
117 struct hpc3_ethregs *hregs,
118 struct sgiseeq_regs *sregs)
119{
120 sregs->rstat = sp->mode | RSTAT_GO_BITS;
121 hregs->rx_ctrl = HPC3_ERXCTRL_ACTIVE;
122}
123
124static inline void __sgiseeq_set_mac_address(struct net_device *dev)
125{
126 struct sgiseeq_private *sp = netdev_priv(dev);
127 struct sgiseeq_regs *sregs = sp->sregs;
128 int i;
129
130 sregs->tstat = SEEQ_TCMD_RB0;
131 for (i = 0; i < 6; i++)
132 sregs->rw.eth_addr[i] = dev->dev_addr[i];
133}
134
135static int sgiseeq_set_mac_address(struct net_device *dev, void *addr)
136{
137 struct sgiseeq_private *sp = netdev_priv(dev);
138 struct sockaddr *sa = addr;
139
140 memcpy(dev->dev_addr, sa->sa_data, dev->addr_len);
141
142 spin_lock_irq(&sp->tx_lock);
143 __sgiseeq_set_mac_address(dev);
144 spin_unlock_irq(&sp->tx_lock);
145
146 return 0;
147}
148
149#define TCNTINFO_INIT (HPCDMA_EOX | HPCDMA_ETXD)
150#define RCNTCFG_INIT (HPCDMA_OWN | HPCDMA_EORP | HPCDMA_XIE)
151#define RCNTINFO_INIT (RCNTCFG_INIT | (PKT_BUF_SZ & HPCDMA_BCNT))
152
153static int seeq_init_ring(struct net_device *dev)
154{
155 struct sgiseeq_private *sp = netdev_priv(dev);
156 int i;
157
158 netif_stop_queue(dev);
159 sp->rx_new = sp->tx_new = 0;
160 sp->rx_old = sp->tx_old = 0;
161
162 __sgiseeq_set_mac_address(dev);
163
164 /* Setup tx ring. */
165 for(i = 0; i < SEEQ_TX_BUFFERS; i++) {
166 if (!sp->tx_desc[i].tdma.pbuf) {
167 unsigned long buffer;
168
169 buffer = (unsigned long) kmalloc(PKT_BUF_SZ, GFP_KERNEL);
170 if (!buffer)
171 return -ENOMEM;
172 sp->tx_desc[i].buf_vaddr = CKSEG1ADDR(buffer);
173 sp->tx_desc[i].tdma.pbuf = CPHYSADDR(buffer);
174 }
175 sp->tx_desc[i].tdma.cntinfo = TCNTINFO_INIT;
176 }
177
178 /* And now the rx ring. */
179 for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
180 if (!sp->rx_desc[i].rdma.pbuf) {
181 unsigned long buffer;
182
183 buffer = (unsigned long) kmalloc(PKT_BUF_SZ, GFP_KERNEL);
184 if (!buffer)
185 return -ENOMEM;
186 sp->rx_desc[i].buf_vaddr = CKSEG1ADDR(buffer);
187 sp->rx_desc[i].rdma.pbuf = CPHYSADDR(buffer);
188 }
189 sp->rx_desc[i].rdma.cntinfo = RCNTINFO_INIT;
190 }
191 sp->rx_desc[i - 1].rdma.cntinfo |= HPCDMA_EOR;
192 return 0;
193}
194
195#ifdef DEBUG
196static struct sgiseeq_private *gpriv;
197static struct net_device *gdev;
198
Ralf Baechlea4d4d512006-03-08 11:49:31 +0000199static void sgiseeq_dump_rings(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200{
201 static int once;
202 struct sgiseeq_rx_desc *r = gpriv->rx_desc;
203 struct sgiseeq_tx_desc *t = gpriv->tx_desc;
204 struct hpc3_ethregs *hregs = gpriv->hregs;
205 int i;
206
207 if (once)
208 return;
209 once++;
210 printk("RING DUMP:\n");
211 for (i = 0; i < SEEQ_RX_BUFFERS; i++) {
212 printk("RX [%d]: @(%p) [%08x,%08x,%08x] ",
213 i, (&r[i]), r[i].rdma.pbuf, r[i].rdma.cntinfo,
214 r[i].rdma.pnext);
215 i += 1;
216 printk("-- [%d]: @(%p) [%08x,%08x,%08x]\n",
217 i, (&r[i]), r[i].rdma.pbuf, r[i].rdma.cntinfo,
218 r[i].rdma.pnext);
219 }
220 for (i = 0; i < SEEQ_TX_BUFFERS; i++) {
221 printk("TX [%d]: @(%p) [%08x,%08x,%08x] ",
222 i, (&t[i]), t[i].tdma.pbuf, t[i].tdma.cntinfo,
223 t[i].tdma.pnext);
224 i += 1;
225 printk("-- [%d]: @(%p) [%08x,%08x,%08x]\n",
226 i, (&t[i]), t[i].tdma.pbuf, t[i].tdma.cntinfo,
227 t[i].tdma.pnext);
228 }
229 printk("INFO: [rx_new = %d rx_old=%d] [tx_new = %d tx_old = %d]\n",
230 gpriv->rx_new, gpriv->rx_old, gpriv->tx_new, gpriv->tx_old);
231 printk("RREGS: rx_cbptr[%08x] rx_ndptr[%08x] rx_ctrl[%08x]\n",
232 hregs->rx_cbptr, hregs->rx_ndptr, hregs->rx_ctrl);
233 printk("TREGS: tx_cbptr[%08x] tx_ndptr[%08x] tx_ctrl[%08x]\n",
234 hregs->tx_cbptr, hregs->tx_ndptr, hregs->tx_ctrl);
235}
236#endif
237
238#define TSTAT_INIT_SEEQ (SEEQ_TCMD_IPT|SEEQ_TCMD_I16|SEEQ_TCMD_IC|SEEQ_TCMD_IUF)
239#define TSTAT_INIT_EDLC ((TSTAT_INIT_SEEQ) | SEEQ_TCMD_RB2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
241static int init_seeq(struct net_device *dev, struct sgiseeq_private *sp,
242 struct sgiseeq_regs *sregs)
243{
244 struct hpc3_ethregs *hregs = sp->hregs;
245 int err;
246
247 reset_hpc3_and_seeq(hregs, sregs);
248 err = seeq_init_ring(dev);
249 if (err)
250 return err;
251
252 /* Setup to field the proper interrupt types. */
253 if (sp->is_edlc) {
254 sregs->tstat = TSTAT_INIT_EDLC;
255 sregs->rw.wregs.control = sp->control;
256 sregs->rw.wregs.frame_gap = 0;
257 } else {
258 sregs->tstat = TSTAT_INIT_SEEQ;
259 }
260
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261 hregs->rx_ndptr = CPHYSADDR(sp->rx_desc);
262 hregs->tx_ndptr = CPHYSADDR(sp->tx_desc);
263
264 seeq_go(sp, hregs, sregs);
265 return 0;
266}
267
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700268static void record_rx_errors(struct net_device *dev, unsigned char status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
270 if (status & SEEQ_RSTAT_OVERF ||
271 status & SEEQ_RSTAT_SFRAME)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700272 dev->stats.rx_over_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273 if (status & SEEQ_RSTAT_CERROR)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700274 dev->stats.rx_crc_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (status & SEEQ_RSTAT_DERROR)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700276 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 if (status & SEEQ_RSTAT_REOF)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700278 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281static inline void rx_maybe_restart(struct sgiseeq_private *sp,
282 struct hpc3_ethregs *hregs,
283 struct sgiseeq_regs *sregs)
284{
285 if (!(hregs->rx_ctrl & HPC3_ERXCTRL_ACTIVE)) {
286 hregs->rx_ndptr = CPHYSADDR(sp->rx_desc + sp->rx_new);
287 seeq_go(sp, hregs, sregs);
288 }
289}
290
291#define for_each_rx(rd, sp) for((rd) = &(sp)->rx_desc[(sp)->rx_new]; \
292 !((rd)->rdma.cntinfo & HPCDMA_OWN); \
293 (rd) = &(sp)->rx_desc[(sp)->rx_new])
294
295static inline void sgiseeq_rx(struct net_device *dev, struct sgiseeq_private *sp,
296 struct hpc3_ethregs *hregs,
297 struct sgiseeq_regs *sregs)
298{
299 struct sgiseeq_rx_desc *rd;
Ralf Baechlea4d4d512006-03-08 11:49:31 +0000300 struct sk_buff *skb = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 unsigned char pkt_status;
Ralf Baechlea4d4d512006-03-08 11:49:31 +0000302 unsigned char *pkt_pointer = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 int len = 0;
304 unsigned int orig_end = PREV_RX(sp->rx_new);
305
306 /* Service every received packet. */
307 for_each_rx(rd, sp) {
308 len = PKT_BUF_SZ - (rd->rdma.cntinfo & HPCDMA_BCNT) - 3;
309 pkt_pointer = (unsigned char *)(long)rd->buf_vaddr;
310 pkt_status = pkt_pointer[len + 2];
311
312 if (pkt_status & SEEQ_RSTAT_FIG) {
313 /* Packet is OK. */
314 skb = dev_alloc_skb(len + 2);
315
316 if (skb) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 skb_reserve(skb, 2);
318 skb_put(skb, len);
319
320 /* Copy out of kseg1 to avoid silly cache flush. */
David S. Miller8c7b7fa2007-07-10 22:08:12 -0700321 skb_copy_to_linear_data(skb, pkt_pointer + 2, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 skb->protocol = eth_type_trans(skb, dev);
323
324 /* We don't want to receive our own packets */
325 if (memcmp(eth_hdr(skb)->h_source, dev->dev_addr, ETH_ALEN)) {
326 netif_rx(skb);
327 dev->last_rx = jiffies;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700328 dev->stats.rx_packets++;
329 dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330 } else {
331 /* Silently drop my own packets */
332 dev_kfree_skb_irq(skb);
333 }
334 } else {
335 printk (KERN_NOTICE "%s: Memory squeeze, deferring packet.\n",
336 dev->name);
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700337 dev->stats.rx_dropped++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 }
339 } else {
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700340 record_rx_errors(dev, pkt_status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 }
342
343 /* Return the entry to the ring pool. */
344 rd->rdma.cntinfo = RCNTINFO_INIT;
345 sp->rx_new = NEXT_RX(sp->rx_new);
346 }
347 sp->rx_desc[orig_end].rdma.cntinfo &= ~(HPCDMA_EOR);
348 sp->rx_desc[PREV_RX(sp->rx_new)].rdma.cntinfo |= HPCDMA_EOR;
349 rx_maybe_restart(sp, hregs, sregs);
350}
351
352static inline void tx_maybe_reset_collisions(struct sgiseeq_private *sp,
353 struct sgiseeq_regs *sregs)
354{
355 if (sp->is_edlc) {
356 sregs->rw.wregs.control = sp->control & ~(SEEQ_CTRL_XCNT);
357 sregs->rw.wregs.control = sp->control;
358 }
359}
360
361static inline void kick_tx(struct sgiseeq_tx_desc *td,
362 struct hpc3_ethregs *hregs)
363{
364 /* If the HPC aint doin nothin, and there are more packets
365 * with ETXD cleared and XIU set we must make very certain
366 * that we restart the HPC else we risk locking up the
367 * adapter. The following code is only safe iff the HPCDMA
368 * is not active!
369 */
370 while ((td->tdma.cntinfo & (HPCDMA_XIU | HPCDMA_ETXD)) ==
371 (HPCDMA_XIU | HPCDMA_ETXD))
372 td = (struct sgiseeq_tx_desc *)(long) CKSEG1ADDR(td->tdma.pnext);
373 if (td->tdma.cntinfo & HPCDMA_XIU) {
374 hregs->tx_ndptr = CPHYSADDR(td);
375 hregs->tx_ctrl = HPC3_ETXCTRL_ACTIVE;
376 }
377}
378
379static inline void sgiseeq_tx(struct net_device *dev, struct sgiseeq_private *sp,
380 struct hpc3_ethregs *hregs,
381 struct sgiseeq_regs *sregs)
382{
383 struct sgiseeq_tx_desc *td;
384 unsigned long status = hregs->tx_ctrl;
385 int j;
386
387 tx_maybe_reset_collisions(sp, sregs);
388
389 if (!(status & (HPC3_ETXCTRL_ACTIVE | SEEQ_TSTAT_PTRANS))) {
390 /* Oops, HPC detected some sort of error. */
391 if (status & SEEQ_TSTAT_R16)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700392 dev->stats.tx_aborted_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 if (status & SEEQ_TSTAT_UFLOW)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700394 dev->stats.tx_fifo_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 if (status & SEEQ_TSTAT_LCLS)
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700396 dev->stats.collisions++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 }
398
399 /* Ack 'em... */
400 for (j = sp->tx_old; j != sp->tx_new; j = NEXT_TX(j)) {
401 td = &sp->tx_desc[j];
402
403 if (!(td->tdma.cntinfo & (HPCDMA_XIU)))
404 break;
405 if (!(td->tdma.cntinfo & (HPCDMA_ETXD))) {
406 if (!(status & HPC3_ETXCTRL_ACTIVE)) {
407 hregs->tx_ndptr = CPHYSADDR(td);
408 hregs->tx_ctrl = HPC3_ETXCTRL_ACTIVE;
409 }
410 break;
411 }
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700412 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 sp->tx_old = NEXT_TX(sp->tx_old);
414 td->tdma.cntinfo &= ~(HPCDMA_XIU | HPCDMA_XIE);
415 td->tdma.cntinfo |= HPCDMA_EOX;
416 }
417}
418
David Howells7d12e782006-10-05 14:55:46 +0100419static irqreturn_t sgiseeq_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420{
421 struct net_device *dev = (struct net_device *) dev_id;
422 struct sgiseeq_private *sp = netdev_priv(dev);
423 struct hpc3_ethregs *hregs = sp->hregs;
424 struct sgiseeq_regs *sregs = sp->sregs;
425
426 spin_lock(&sp->tx_lock);
427
428 /* Ack the IRQ and set software state. */
Ralf Baechle302a5c4b2005-10-10 14:50:56 +0100429 hregs->reset = HPC3_ERST_CLRIRQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430
431 /* Always check for received packets. */
432 sgiseeq_rx(dev, sp, hregs, sregs);
433
434 /* Only check for tx acks if we have something queued. */
435 if (sp->tx_old != sp->tx_new)
436 sgiseeq_tx(dev, sp, hregs, sregs);
437
438 if ((TX_BUFFS_AVAIL(sp) > 0) && netif_queue_stopped(dev)) {
439 netif_wake_queue(dev);
440 }
441 spin_unlock(&sp->tx_lock);
442
443 return IRQ_HANDLED;
444}
445
446static int sgiseeq_open(struct net_device *dev)
447{
448 struct sgiseeq_private *sp = netdev_priv(dev);
449 struct sgiseeq_regs *sregs = sp->sregs;
450 unsigned int irq = dev->irq;
451 int err;
452
453 if (request_irq(irq, sgiseeq_interrupt, 0, sgiseeqstr, dev)) {
454 printk(KERN_ERR "Seeq8003: Can't get irq %d\n", dev->irq);
455 err = -EAGAIN;
456 }
457
458 err = init_seeq(dev, sp, sregs);
459 if (err)
460 goto out_free_irq;
461
462 netif_start_queue(dev);
463
464 return 0;
465
466out_free_irq:
467 free_irq(irq, dev);
468
469 return err;
470}
471
472static int sgiseeq_close(struct net_device *dev)
473{
474 struct sgiseeq_private *sp = netdev_priv(dev);
475 struct sgiseeq_regs *sregs = sp->sregs;
Ralf Baechle28914392005-10-10 14:50:51 +0100476 unsigned int irq = dev->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
478 netif_stop_queue(dev);
479
480 /* Shutdown the Seeq. */
481 reset_hpc3_and_seeq(sp->hregs, sregs);
Ralf Baechle28914392005-10-10 14:50:51 +0100482 free_irq(irq, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483
484 return 0;
485}
486
487static inline int sgiseeq_reset(struct net_device *dev)
488{
489 struct sgiseeq_private *sp = netdev_priv(dev);
490 struct sgiseeq_regs *sregs = sp->sregs;
491 int err;
492
493 err = init_seeq(dev, sp, sregs);
494 if (err)
495 return err;
496
497 dev->trans_start = jiffies;
498 netif_wake_queue(dev);
499
500 return 0;
501}
502
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503static int sgiseeq_start_xmit(struct sk_buff *skb, struct net_device *dev)
504{
505 struct sgiseeq_private *sp = netdev_priv(dev);
506 struct hpc3_ethregs *hregs = sp->hregs;
507 unsigned long flags;
508 struct sgiseeq_tx_desc *td;
509 int skblen, len, entry;
510
511 spin_lock_irqsave(&sp->tx_lock, flags);
512
513 /* Setup... */
514 skblen = skb->len;
515 len = (skblen <= ETH_ZLEN) ? ETH_ZLEN : skblen;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700516 dev->stats.tx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 entry = sp->tx_new;
518 td = &sp->tx_desc[entry];
519
520 /* Create entry. There are so many races with adding a new
521 * descriptor to the chain:
522 * 1) Assume that the HPC is off processing a DMA chain while
523 * we are changing all of the following.
524 * 2) Do no allow the HPC to look at a new descriptor until
525 * we have completely set up it's state. This means, do
526 * not clear HPCDMA_EOX in the current last descritptor
527 * until the one we are adding looks consistent and could
528 * be processes right now.
529 * 3) The tx interrupt code must notice when we've added a new
530 * entry and the HPC got to the end of the chain before we
531 * added this new entry and restarted it.
532 */
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300533 skb_copy_from_linear_data(skb, (char *)(long)td->buf_vaddr, skblen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 if (len != skblen)
535 memset((char *)(long)td->buf_vaddr + skb->len, 0, len-skblen);
536 td->tdma.cntinfo = (len & HPCDMA_BCNT) |
537 HPCDMA_XIU | HPCDMA_EOXP | HPCDMA_XIE | HPCDMA_EOX;
538 if (sp->tx_old != sp->tx_new) {
539 struct sgiseeq_tx_desc *backend;
540
541 backend = &sp->tx_desc[PREV_TX(sp->tx_new)];
542 backend->tdma.cntinfo &= ~HPCDMA_EOX;
543 }
544 sp->tx_new = NEXT_TX(sp->tx_new); /* Advance. */
545
546 /* Maybe kick the HPC back into motion. */
547 if (!(hregs->tx_ctrl & HPC3_ETXCTRL_ACTIVE))
548 kick_tx(&sp->tx_desc[sp->tx_old], hregs);
549
550 dev->trans_start = jiffies;
551 dev_kfree_skb(skb);
552
553 if (!TX_BUFFS_AVAIL(sp))
554 netif_stop_queue(dev);
555 spin_unlock_irqrestore(&sp->tx_lock, flags);
556
557 return 0;
558}
559
560static void timeout(struct net_device *dev)
561{
562 printk(KERN_NOTICE "%s: transmit timed out, resetting\n", dev->name);
563 sgiseeq_reset(dev);
564
565 dev->trans_start = jiffies;
566 netif_wake_queue(dev);
567}
568
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569static void sgiseeq_set_multicast(struct net_device *dev)
570{
571 struct sgiseeq_private *sp = (struct sgiseeq_private *) dev->priv;
572 unsigned char oldmode = sp->mode;
573
574 if(dev->flags & IFF_PROMISC)
575 sp->mode = SEEQ_RCMD_RANY;
576 else if ((dev->flags & IFF_ALLMULTI) || dev->mc_count)
577 sp->mode = SEEQ_RCMD_RBMCAST;
578 else
579 sp->mode = SEEQ_RCMD_RBCAST;
580
581 /* XXX I know this sucks, but is there a better way to reprogram
582 * XXX the receiver? At least, this shouldn't happen too often.
583 */
584
585 if (oldmode != sp->mode)
586 sgiseeq_reset(dev);
587}
588
589static inline void setup_tx_ring(struct sgiseeq_tx_desc *buf, int nbufs)
590{
591 int i = 0;
592
593 while (i < (nbufs - 1)) {
594 buf[i].tdma.pnext = CPHYSADDR(buf + i + 1);
595 buf[i].tdma.pbuf = 0;
596 i++;
597 }
598 buf[i].tdma.pnext = CPHYSADDR(buf);
599}
600
601static inline void setup_rx_ring(struct sgiseeq_rx_desc *buf, int nbufs)
602{
603 int i = 0;
604
605 while (i < (nbufs - 1)) {
606 buf[i].rdma.pnext = CPHYSADDR(buf + i + 1);
607 buf[i].rdma.pbuf = 0;
608 i++;
609 }
610 buf[i].rdma.pbuf = 0;
611 buf[i].rdma.pnext = CPHYSADDR(buf);
612}
613
614#define ALIGNED(x) ((((unsigned long)(x)) + 0xf) & ~(0xf))
615
Ralf Baechledf9f5402007-05-11 15:48:58 +0100616static int __init sgiseeq_probe(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617{
Ralf Baechledf9f5402007-05-11 15:48:58 +0100618 struct sgiseeq_platform_data *pd = pdev->dev.platform_data;
619 struct hpc3_regs *hpcregs = pd->hpc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620 struct sgiseeq_init_block *sr;
Ralf Baechledf9f5402007-05-11 15:48:58 +0100621 unsigned int irq = pd->irq;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 struct sgiseeq_private *sp;
623 struct net_device *dev;
624 int err, i;
Joe Perches0795af52007-10-03 17:59:30 -0700625 DECLARE_MAC_BUF(mac);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
627 dev = alloc_etherdev(sizeof (struct sgiseeq_private));
628 if (!dev) {
629 printk(KERN_ERR "Sgiseeq: Etherdev alloc failed, aborting.\n");
630 err = -ENOMEM;
631 goto err_out;
632 }
Ralf Baechledf9f5402007-05-11 15:48:58 +0100633
634 platform_set_drvdata(pdev, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 sp = netdev_priv(dev);
636
637 /* Make private data page aligned */
Ralf Baechle99cd1492007-09-04 14:41:01 +0100638 sr = dma_alloc_coherent(&pdev->dev, sizeof(*sp->srings),
639 &sp->srings_dma, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 if (!sr) {
641 printk(KERN_ERR "Sgiseeq: Page alloc failed, aborting.\n");
642 err = -ENOMEM;
643 goto err_out_free_dev;
644 }
645 sp->srings = sr;
Ralf Baechle99cd1492007-09-04 14:41:01 +0100646 sp->rx_desc = sp->srings->rxvector;
647 sp->tx_desc = sp->srings->txvector;
648
649 /* A couple calculations now, saves many cycles later. */
650 setup_rx_ring(sp->rx_desc, SEEQ_RX_BUFFERS);
651 setup_tx_ring(sp->tx_desc, SEEQ_TX_BUFFERS);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652
Ralf Baechledf9f5402007-05-11 15:48:58 +0100653 memcpy(dev->dev_addr, pd->mac, ETH_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654
655#ifdef DEBUG
656 gpriv = sp;
657 gdev = dev;
658#endif
Ralf Baechle302a5c4b2005-10-10 14:50:56 +0100659 sp->sregs = (struct sgiseeq_regs *) &hpcregs->eth_ext[0];
660 sp->hregs = &hpcregs->ethregs;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 sp->name = sgiseeqstr;
662 sp->mode = SEEQ_RCMD_RBCAST;
663
Ralf Baechle302a5c4b2005-10-10 14:50:56 +0100664 /* Setup PIO and DMA transfer timing */
665 sp->hregs->pconfig = 0x161;
666 sp->hregs->dconfig = HPC3_EDCFG_FIRQ | HPC3_EDCFG_FEOP |
667 HPC3_EDCFG_FRXDC | HPC3_EDCFG_PTO | 0x026;
668
Ladislav Michl78ee5b32007-02-28 01:18:35 +0000669 /* Setup PIO and DMA transfer timing */
670 sp->hregs->pconfig = 0x161;
671 sp->hregs->dconfig = HPC3_EDCFG_FIRQ | HPC3_EDCFG_FEOP |
672 HPC3_EDCFG_FRXDC | HPC3_EDCFG_PTO | 0x026;
673
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 /* Reset the chip. */
675 hpc3_eth_reset(sp->hregs);
676
677 sp->is_edlc = !(sp->sregs->rw.rregs.collision_tx[0] & 0xff);
678 if (sp->is_edlc)
679 sp->control = SEEQ_CTRL_XCNT | SEEQ_CTRL_ACCNT |
680 SEEQ_CTRL_SFLAG | SEEQ_CTRL_ESHORT |
681 SEEQ_CTRL_ENCARR;
682
683 dev->open = sgiseeq_open;
684 dev->stop = sgiseeq_close;
685 dev->hard_start_xmit = sgiseeq_start_xmit;
686 dev->tx_timeout = timeout;
687 dev->watchdog_timeo = (200 * HZ) / 1000;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688 dev->set_multicast_list = sgiseeq_set_multicast;
689 dev->set_mac_address = sgiseeq_set_mac_address;
690 dev->irq = irq;
691
692 if (register_netdev(dev)) {
693 printk(KERN_ERR "Sgiseeq: Cannot register net device, "
694 "aborting.\n");
695 err = -ENODEV;
696 goto err_out_free_page;
697 }
698
Joe Perches0795af52007-10-03 17:59:30 -0700699 printk(KERN_INFO "%s: %s %s\n",
700 dev->name, sgiseeqstr, print_mac(mac, dev->dev_addr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return 0;
703
704err_out_free_page:
Ralf Baechle28914392005-10-10 14:50:51 +0100705 free_page((unsigned long) sp->srings);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706err_out_free_dev:
707 kfree(dev);
708
709err_out:
710 return err;
711}
712
Ralf Baechlee3efb052007-08-22 16:03:52 +0100713static int __exit sgiseeq_remove(struct platform_device *pdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714{
Ralf Baechledf9f5402007-05-11 15:48:58 +0100715 struct net_device *dev = platform_get_drvdata(pdev);
716 struct sgiseeq_private *sp = netdev_priv(dev);
Ladislav Michl78ee5b32007-02-28 01:18:35 +0000717
Ralf Baechledf9f5402007-05-11 15:48:58 +0100718 unregister_netdev(dev);
Ralf Baechle99cd1492007-09-04 14:41:01 +0100719 dma_free_coherent(&pdev->dev, sizeof(*sp->srings), sp->srings,
720 sp->srings_dma);
Ralf Baechledf9f5402007-05-11 15:48:58 +0100721 free_netdev(dev);
722 platform_set_drvdata(pdev, NULL);
Ralf Baechlee3efb052007-08-22 16:03:52 +0100723
724 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725}
726
Ralf Baechledf9f5402007-05-11 15:48:58 +0100727static struct platform_driver sgiseeq_driver = {
728 .probe = sgiseeq_probe,
729 .remove = __devexit_p(sgiseeq_remove),
730 .driver = {
731 .name = "sgiseeq"
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 }
Ralf Baechledf9f5402007-05-11 15:48:58 +0100733};
734
735static int __init sgiseeq_module_init(void)
736{
737 if (platform_driver_register(&sgiseeq_driver)) {
738 printk(KERN_ERR "Driver registration failed\n");
739 return -ENODEV;
740 }
741
742 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743}
744
Ralf Baechledf9f5402007-05-11 15:48:58 +0100745static void __exit sgiseeq_module_exit(void)
746{
747 platform_driver_unregister(&sgiseeq_driver);
748}
749
750module_init(sgiseeq_module_init);
751module_exit(sgiseeq_module_exit);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752
Ralf Baechle302a5c4b2005-10-10 14:50:56 +0100753MODULE_DESCRIPTION("SGI Seeq 8003 driver");
754MODULE_AUTHOR("Linux/MIPS Mailing List <linux-mips@linux-mips.org>");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755MODULE_LICENSE("GPL");