* add p cc
[mascara-docs.git] / i386 / linux / linux-2.3.21 / drivers / net / myri_sbus.c
blob5bf2ba8c66635e49ecbf4b68776d60bc0588ef19
1 /* myri_sbus.h: MyriCOM MyriNET SBUS card driver.
3 * Copyright (C) 1996 David S. Miller (davem@caip.rutgers.edu)
4 */
6 static char *version =
7 "myri_sbus.c:v1.0 10/Dec/96 David S. Miller (davem@caipfs.rutgers.edu)\n";
9 #include <linux/module.h>
11 #include <linux/config.h>
12 #include <linux/kernel.h>
13 #include <linux/sched.h>
14 #include <linux/types.h>
15 #include <linux/fcntl.h>
16 #include <linux/interrupt.h>
17 #include <linux/ptrace.h>
18 #include <linux/ioport.h>
19 #include <linux/in.h>
20 #include <linux/malloc.h>
21 #include <linux/string.h>
22 #include <linux/delay.h>
23 #include <linux/init.h>
25 #include <asm/system.h>
26 #include <asm/bitops.h>
27 #include <asm/io.h>
28 #include <asm/dma.h>
29 #include <linux/errno.h>
30 #include <asm/byteorder.h>
32 #include <asm/idprom.h>
33 #include <asm/sbus.h>
34 #include <asm/openprom.h>
35 #include <asm/oplib.h>
36 #include <asm/auxio.h>
37 #include <asm/pgtable.h>
38 #include <asm/irq.h>
40 #include <linux/netdevice.h>
41 #include <linux/etherdevice.h>
42 #include <linux/skbuff.h>
44 #include <net/dst.h>
45 #include <net/arp.h>
46 #include <net/sock.h>
47 #include <net/ipv6.h>
49 #include <asm/checksum.h>
51 #include "myri_sbus.h"
53 #include "myri_code.h"
55 /* #define DEBUG_DETECT */
56 /* #define DEBUG_IRQ */
57 /* #define DEBUG_TRANSMIT */
58 /* #define DEBUG_RECEIVE */
59 /* #define DEBUG_HEADER */
61 #ifdef DEBUG_DETECT
62 #define DET(x) printk x
63 #else
64 #define DET(x)
65 #endif
67 #ifdef DEBUG_IRQ
68 #define DIRQ(x) printk x
69 #else
70 #define DIRQ(x)
71 #endif
73 #ifdef DEBUG_TRANSMIT
74 #define DTX(x) printk x
75 #else
76 #define DTX(x)
77 #endif
79 #ifdef DEBUG_RECEIVE
80 #define DRX(x) printk x
81 #else
82 #define DRX(x)
83 #endif
85 #ifdef DEBUG_HEADER
86 #define DHDR(x) printk x
87 #else
88 #define DHDR(x)
89 #endif
91 #ifdef MODULE
92 static struct myri_eth *root_myri_dev = NULL;
93 #endif
95 static inline void myri_reset_off(struct lanai_regs *lp, struct myri_control *cregs)
97 lp->eimask = 0; /* Clear IRQ mask. */
98 cregs->ctrl = CONTROL_ROFF; /* Turn RESET function off. */
101 static inline void myri_reset_on(struct myri_control *cregs)
103 cregs->ctrl = CONTROL_RON; /* Enable RESET function. */
104 cregs->ctrl = CONTROL_DIRQ; /* Disable IRQ's. */
107 static inline void myri_disable_irq(struct lanai_regs *lp, struct myri_control *cregs)
109 cregs->ctrl = CONTROL_DIRQ;
110 lp->eimask = 0;
111 lp->istat = ISTAT_HOST;
114 static inline void myri_enable_irq(struct lanai_regs *lp, struct myri_control *cregs)
116 cregs->ctrl = CONTROL_EIRQ;
117 lp->eimask = ISTAT_HOST;
120 static inline void bang_the_chip(struct myri_eth *mp)
122 struct myri_shmem *shmem = mp->shmem;
123 struct myri_control *cregs = mp->cregs;
125 shmem->send = 1;
126 cregs->ctrl = CONTROL_WON;
129 static inline int myri_do_handshake(struct myri_eth *mp)
131 struct myri_shmem *shmem = mp->shmem;
132 struct myri_control *cregs = mp->cregs;
133 struct myri_channel *chan = &shmem->channel;
134 int tick = 0;
136 DET(("myri_do_handshake: "));
137 if(chan->state == STATE_READY) {
138 DET(("Already STATE_READY, failed.\n"));
139 return -1; /* We're hosed... */
142 myri_disable_irq(mp->lregs, cregs);
144 while(tick++ <= 25) {
145 unsigned int softstate;
147 /* Wake it up. */
148 DET(("shakedown, CONTROL_WON, "));
149 shmem->shakedown = 1;
150 cregs->ctrl = CONTROL_WON;
152 softstate = chan->state;
153 DET(("chanstate[%08x] ", softstate));
154 if(softstate == STATE_READY) {
155 DET(("wakeup successful, "));
156 break;
159 if(softstate != STATE_WFN) {
160 DET(("not WFN setting that, "));
161 chan->state = STATE_WFN;
164 udelay(20);
167 myri_enable_irq(mp->lregs, cregs);
169 if(tick > 25) {
170 DET(("25 ticks we lose, failure.\n"));
171 return -1;
173 DET(("success\n"));
174 return 0;
177 static inline int myri_load_lanai(struct myri_eth *mp)
179 struct net_device *dev = mp->dev;
180 struct myri_shmem *shmem = mp->shmem;
181 unsigned char *rptr;
182 int i;
184 myri_disable_irq(mp->lregs, mp->cregs);
185 myri_reset_on(mp->cregs);
187 rptr = (unsigned char *) mp->lanai;
188 for(i = 0; i < mp->eeprom.ramsz; i++)
189 rptr[i] = 0;
191 if(mp->eeprom.cpuvers >= CPUVERS_3_0)
192 mp->lregs->cval = mp->eeprom.cval;
194 /* Load executable code. */
195 for(i = 0; i < sizeof(lanai4_code); i++)
196 rptr[(lanai4_code_off * 2) + i] = lanai4_code[i];
198 /* Load data segment. */
199 for(i = 0; i < sizeof(lanai4_data); i++)
200 rptr[(lanai4_data_off * 2) + i] = lanai4_data[i];
202 /* Set device address. */
203 shmem->addr[0] = shmem->addr[1] = 0;
204 for(i = 0; i < 6; i++)
205 shmem->addr[i + 2] = dev->dev_addr[i];
207 /* Set SBUS bursts and interrupt mask. */
208 shmem->burst = ((mp->myri_bursts & 0xf8) >> 3);
209 shmem->imask = SHMEM_IMASK_RX;
211 /* Release the LANAI. */
212 myri_disable_irq(mp->lregs, mp->cregs);
213 myri_reset_off(mp->lregs, mp->cregs);
214 myri_disable_irq(mp->lregs, mp->cregs);
216 /* Wait for the reset to complete. */
217 for(i = 0; i < 5000; i++) {
218 if(shmem->channel.state != STATE_READY)
219 break;
220 else
221 udelay(10);
224 if(i == 5000)
225 printk("myricom: Chip would not reset after firmware load.\n");
227 i = myri_do_handshake(mp);
228 if(i)
229 printk("myricom: Handshake with LANAI failed.\n");
231 if(mp->eeprom.cpuvers == CPUVERS_4_0)
232 mp->lregs->vers = 0;
234 return i;
237 static void myri_clean_rings(struct myri_eth *mp)
239 struct sendq *sq = mp->sq;
240 struct recvq *rq = mp->rq;
241 int i;
243 rq->tail = rq->head = 0;
244 for(i = 0; i < (RX_RING_SIZE+1); i++) {
245 if(mp->rx_skbs[i] != NULL) {
246 dev_kfree_skb(mp->rx_skbs[i]);
247 mp->rx_skbs[i] = NULL;
251 mp->tx_old = sq->tail = sq->head = 0;
252 for(i = 0; i < TX_RING_SIZE; i++) {
253 if(mp->tx_skbs[i] != NULL) {
254 dev_kfree_skb(mp->tx_skbs[i]);
255 mp->tx_skbs[i] = NULL;
260 static void myri_init_rings(struct myri_eth *mp, int from_irq)
262 struct recvq *rq = mp->rq;
263 struct myri_rxd *rxd = &rq->myri_rxd[0];
264 struct net_device *dev = mp->dev;
265 int gfp_flags = GFP_KERNEL;
266 int i;
268 if(from_irq || in_interrupt())
269 gfp_flags = GFP_ATOMIC;
271 myri_clean_rings(mp);
272 for(i = 0; i < RX_RING_SIZE; i++) {
273 struct sk_buff *skb = myri_alloc_skb(RX_ALLOC_SIZE, gfp_flags);
275 if(!skb)
276 continue;
277 mp->rx_skbs[i] = skb;
278 skb->dev = dev;
279 skb_put(skb, RX_ALLOC_SIZE);
280 rxd[i].myri_scatters[0].addr = sbus_dvma_addr(skb->data);
281 rxd[i].myri_scatters[0].len = RX_ALLOC_SIZE;
282 rxd[i].ctx = i;
283 rxd[i].num_sg = 1;
285 rq->head = 0;
286 rq->tail = RX_RING_SIZE;
289 static int myri_init(struct myri_eth *mp, int from_irq)
291 myri_init_rings(mp, from_irq);
292 return 0;
295 static void myri_is_not_so_happy(struct myri_eth *mp)
299 #ifdef DEBUG_HEADER
300 static void dump_ehdr(struct ethhdr *ehdr)
302 printk("ehdr[h_dst(%02x:%02x:%02x:%02x:%02x:%02x)"
303 "h_source(%02x:%02x:%02x:%02x:%02x:%02x)h_proto(%04x)]\n",
304 ehdr->h_dest[0], ehdr->h_dest[1], ehdr->h_dest[2],
305 ehdr->h_dest[3], ehdr->h_dest[4], ehdr->h_dest[4],
306 ehdr->h_source[0], ehdr->h_source[1], ehdr->h_source[2],
307 ehdr->h_source[3], ehdr->h_source[4], ehdr->h_source[4],
308 ehdr->h_proto);
311 static void dump_ehdr_and_myripad(unsigned char *stuff)
313 struct ethhdr *ehdr = (struct ethhdr *) (stuff + 2);
315 printk("pad[%02x:%02x]", stuff[0], stuff[1]);
316 printk("ehdr[h_dst(%02x:%02x:%02x:%02x:%02x:%02x)"
317 "h_source(%02x:%02x:%02x:%02x:%02x:%02x)h_proto(%04x)]\n",
318 ehdr->h_dest[0], ehdr->h_dest[1], ehdr->h_dest[2],
319 ehdr->h_dest[3], ehdr->h_dest[4], ehdr->h_dest[4],
320 ehdr->h_source[0], ehdr->h_source[1], ehdr->h_source[2],
321 ehdr->h_source[3], ehdr->h_source[4], ehdr->h_source[4],
322 ehdr->h_proto);
324 #endif
326 static inline void myri_tx(struct myri_eth *mp, struct net_device *dev)
328 struct sendq *sq = mp->sq;
329 int entry = mp->tx_old;
330 int limit = sq->head;
332 DTX(("entry[%d] limit[%d] ", entry, limit));
333 if(entry == limit)
334 return;
335 while(entry != limit) {
336 struct sk_buff *skb = mp->tx_skbs[entry];
338 DTX(("SKB[%d] ", entry));
339 dev_kfree_skb(skb);
340 mp->tx_skbs[entry] = NULL;
341 mp->enet_stats.tx_packets++;
342 entry = NEXT_TX(entry);
344 mp->tx_old = entry;
347 /* Determine the packet's protocol ID. The rule here is that we
348 * assume 802.3 if the type field is short enough to be a length.
349 * This is normal practice and works for any 'now in use' protocol.
351 static unsigned short myri_type_trans(struct sk_buff *skb, struct net_device *dev)
353 struct ethhdr *eth;
354 unsigned char *rawp;
356 skb->mac.raw = (((unsigned char *)skb->data) + MYRI_PAD_LEN);
357 skb_pull(skb, dev->hard_header_len);
358 eth = skb->mac.ethernet;
360 #ifdef DEBUG_HEADER
361 DHDR(("myri_type_trans: "));
362 dump_ehdr(eth);
363 #endif
364 if(*eth->h_dest & 1) {
365 if(memcmp(eth->h_dest, dev->broadcast, ETH_ALEN)==0)
366 skb->pkt_type = PACKET_BROADCAST;
367 else
368 skb->pkt_type = PACKET_MULTICAST;
369 } else if(dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) {
370 if(memcmp(eth->h_dest, dev->dev_addr, ETH_ALEN))
371 skb->pkt_type = PACKET_OTHERHOST;
374 if(ntohs(eth->h_proto) >= 1536)
375 return eth->h_proto;
377 rawp = skb->data;
379 /* This is a magic hack to spot IPX packets. Older Novell breaks
380 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
381 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
382 * won't work for fault tolerant netware but does for the rest.
384 if (*(unsigned short *)rawp == 0xFFFF)
385 return htons(ETH_P_802_3);
387 /* Real 802.2 LLC */
388 return htons(ETH_P_802_2);
391 static inline void myri_rx(struct myri_eth *mp, struct net_device *dev)
393 struct recvq *rq = mp->rq;
394 struct recvq *rqa = mp->rqack;
395 int entry = rqa->head;
396 int limit = rqa->tail;
397 int drops;
399 DRX(("entry[%d] limit[%d] ", entry, limit));
400 if(entry == limit)
401 return;
402 drops = 0;
403 DRX(("\n"));
404 while(entry != limit) {
405 struct myri_rxd *rxdack = &rqa->myri_rxd[entry];
406 unsigned int csum = rxdack->csum;
407 int len = rxdack->myri_scatters[0].len;
408 int index = rxdack->ctx;
409 struct myri_rxd *rxd = &rq->myri_rxd[rq->tail];
410 struct sk_buff *skb = mp->rx_skbs[index];
412 /* Ack it. */
413 rqa->head = NEXT_RX(entry);
415 /* Check for errors. */
416 DRX(("rxd[%d]: %p len[%d] csum[%08x] ", entry, rxd, len, csum));
417 if((len < (ETH_HLEN + MYRI_PAD_LEN)) || (skb->data[0] != MYRI_PAD_LEN)) {
418 DRX(("ERROR["));
419 mp->enet_stats.rx_errors++;
420 if(len < (ETH_HLEN + MYRI_PAD_LEN)) {
421 DRX(("BAD_LENGTH] "));
422 mp->enet_stats.rx_length_errors++;
423 } else {
424 DRX(("NO_PADDING] "));
425 mp->enet_stats.rx_frame_errors++;
428 /* Return it to the LANAI. */
429 drop_it:
430 drops++;
431 DRX(("DROP "));
432 mp->enet_stats.rx_dropped++;
433 rxd->myri_scatters[0].addr = sbus_dvma_addr(skb->data);
434 rxd->myri_scatters[0].len = RX_ALLOC_SIZE;
435 rxd->ctx = index;
436 rxd->num_sg = 1;
437 rq->tail = NEXT_RX(rq->tail);
438 goto next;
441 #ifdef NEED_DMA_SYNCHRONIZATION
442 mmu_sync_dma(sbus_dvma_addr(skb->data),
443 skb->len, mp->myri_sbus_dev->my_bus);
444 #endif
446 DRX(("len[%d] ", len));
447 if(len > RX_COPY_THRESHOLD) {
448 struct sk_buff *new_skb;
450 DRX(("BIGBUFF "));
451 new_skb = myri_alloc_skb(RX_ALLOC_SIZE, GFP_ATOMIC);
452 if(!new_skb) {
453 DRX(("skb_alloc(FAILED) "));
454 goto drop_it;
456 mp->rx_skbs[index] = new_skb;
457 new_skb->dev = dev;
458 skb_put(new_skb, RX_ALLOC_SIZE);
459 rxd->myri_scatters[0].addr = sbus_dvma_addr(new_skb->data);
460 rxd->myri_scatters[0].len = RX_ALLOC_SIZE;
461 rxd->ctx = index;
462 rxd->num_sg = 1;
463 rq->tail = NEXT_RX(rq->tail);
465 /* Trim the original skb for the netif. */
466 DRX(("trim(%d) ", len));
467 skb_trim(skb, len);
468 } else {
469 struct sk_buff *copy_skb = dev_alloc_skb(len);
471 DRX(("SMALLBUFF "));
472 if(!copy_skb) {
473 DRX(("dev_alloc_skb(FAILED) "));
474 goto drop_it;
476 copy_skb->dev = dev;
477 DRX(("resv_and_put "));
478 skb_put(copy_skb, len);
479 memcpy(copy_skb->data, skb->data, len);
481 /* Reuse original ring buffer. */
482 DRX(("reuse "));
483 rxd->myri_scatters[0].addr = sbus_dvma_addr(skb->data);
484 rxd->myri_scatters[0].len = RX_ALLOC_SIZE;
485 rxd->ctx = index;
486 rxd->num_sg = 1;
487 rq->tail = NEXT_RX(rq->tail);
489 skb = copy_skb;
492 /* Just like the happy meal we get checksums from this card. */
493 skb->csum = csum;
494 skb->ip_summed = CHECKSUM_UNNECESSARY; /* XXX */
496 skb->protocol = myri_type_trans(skb, dev);
497 DRX(("prot[%04x] netif_rx ", skb->protocol));
498 netif_rx(skb);
500 mp->enet_stats.rx_packets++;
501 next:
502 DRX(("NEXT\n"));
503 entry = NEXT_RX(entry);
507 static void myri_interrupt(int irq, void *dev_id, struct pt_regs *regs)
509 struct net_device *dev = (struct net_device *) dev_id;
510 struct myri_eth *mp = (struct myri_eth *) dev->priv;
511 struct lanai_regs *lregs = mp->lregs;
512 struct myri_channel *chan = &mp->shmem->channel;
513 unsigned int status;
515 status = lregs->istat;
516 DIRQ(("myri_interrupt: status[%08x] ", status));
517 if(status & ISTAT_HOST) {
518 unsigned int softstate;
520 DIRQ(("IRQ_DISAB "));
521 myri_disable_irq(lregs, mp->cregs);
522 dev->interrupt = 1;
523 softstate = chan->state;
524 DIRQ(("state[%08x] ", softstate));
525 if(softstate != STATE_READY) {
526 DIRQ(("myri_not_so_happy "));
527 myri_is_not_so_happy(mp);
529 DIRQ(("\nmyri_rx: "));
530 myri_rx(mp, dev);
531 DIRQ(("\nistat=ISTAT_HOST "));
532 lregs->istat = ISTAT_HOST;
533 dev->interrupt = 0;
534 DIRQ(("IRQ_ENAB "));
535 myri_enable_irq(lregs, mp->cregs);
537 DIRQ(("\n"));
540 static int myri_open(struct net_device *dev)
542 struct myri_eth *mp = (struct myri_eth *) dev->priv;
544 return myri_init(mp, in_interrupt());
547 static int myri_close(struct net_device *dev)
549 struct myri_eth *mp = (struct myri_eth *) dev->priv;
551 myri_clean_rings(mp);
552 return 0;
555 static int myri_start_xmit(struct sk_buff *skb, struct net_device *dev)
557 struct myri_eth *mp = (struct myri_eth *) dev->priv;
558 struct sendq *sq = mp->sq;
559 struct myri_txd *txd;
560 unsigned char *srcptr;
561 unsigned long flags;
562 unsigned int head, tail;
563 int len, entry;
565 DTX(("myri_start_xmit: "));
567 myri_tx(mp, dev);
569 if(dev->tbusy) {
570 int tickssofar = jiffies - dev->trans_start;
572 DTX(("tbusy tickssofar[%d] ", tickssofar));
573 if(tickssofar < 40) {
574 DTX(("returning 1\n"));
575 return 1;
576 } else {
577 DTX(("resetting, return 0\n"));
578 printk("%s: transmit timed out, resetting\n", dev->name);
579 mp->enet_stats.tx_errors++;
580 myri_init(mp, in_interrupt());
581 dev->tbusy = 0;
582 dev->trans_start = jiffies;
583 return 0;
587 if(test_and_set_bit(0, (void *) &dev->tbusy) != 0) {
588 DTX(("tbusy, maybe a race? returning 1\n"));
589 printk("%s: Transmitter access conflict.\n", dev->name);
590 return 1;
594 #ifdef NEED_DMA_SYNCHRONIZATION
595 mmu_sync_dma(sbus_dvma_addr(skb->data),
596 skb->len, mp->myri_sbus_dev->my_bus);
597 #endif
599 /* This is just to prevent multiple PIO reads for TX_BUFFS_AVAIL. */
600 head = sq->head;
601 tail = sq->tail;
603 if(!TX_BUFFS_AVAIL(head, tail)) {
604 DTX(("no buffs available, returning 1\n"));
605 return 1;
608 save_and_cli(flags);
610 DHDR(("xmit[skbdata(%p)]\n", skb->data));
611 #ifdef DEBUG_HEADER
612 dump_ehdr_and_myripad(((unsigned char *) skb->data));
613 #endif
615 /* XXX Maybe this can go as well. */
616 len = skb->len;
617 if(len & 3) {
618 DTX(("len&3 "));
619 len = (len + 4) & (~3);
622 entry = sq->tail;
624 txd = &sq->myri_txd[entry];
625 mp->tx_skbs[entry] = skb;
627 txd->myri_gathers[0].addr = sbus_dvma_addr(skb->data);
628 txd->myri_gathers[0].len = len;
629 txd->num_sg = 1;
630 txd->chan = KERNEL_CHANNEL;
631 txd->len = len;
632 txd->csum_off = ((unsigned int)-1);
633 txd->csum_field = 0;
635 srcptr = (((unsigned char *) skb->data) + MYRI_PAD_LEN);
636 if(srcptr[0] & 0x1) {
637 txd->addr[0] = txd->addr[1] = txd->addr[2] = txd->addr[3] = 0xffff;
638 } else {
639 txd->addr[0] = 0;
640 txd->addr[1] = (srcptr[0] << 8) | srcptr[1];
641 txd->addr[2] = (srcptr[2] << 8) | srcptr[3];
642 txd->addr[3] = (srcptr[4] << 8) | srcptr[5];
644 sq->tail = NEXT_TX(entry);
645 DTX(("BangTheChip "));
646 bang_the_chip(mp);
648 DTX(("tbusy=0, returning 0\n"));
649 dev->tbusy = 0;
650 restore_flags(flags);
651 return 0;
654 /* Create the MyriNet MAC header for an arbitrary protocol layer
656 * saddr=NULL means use device source address
657 * daddr=NULL means leave destination address (eg unresolved arp)
659 static int myri_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
660 void *daddr, void *saddr, unsigned len)
662 struct ethhdr *eth = (struct ethhdr *)skb_push(skb,ETH_HLEN);
663 unsigned char *pad = (unsigned char *)skb_push(skb,MYRI_PAD_LEN);
665 #ifdef DEBUG_HEADER
666 DHDR(("myri_header: pad[%02x,%02x] ", pad[0], pad[1]));
667 dump_ehdr(eth);
668 #endif
670 /* Set the MyriNET padding identifier. */
671 pad[0] = MYRI_PAD_LEN;
672 pad[1] = 0xab;
674 /* Set the protocol type. For a packet of type ETH_P_802_3 we put the length
675 * in here instead. It is up to the 802.2 layer to carry protocol information.
677 if(type != ETH_P_802_3)
678 eth->h_proto = htons(type);
679 else
680 eth->h_proto = htons(len);
682 /* Set the source hardware address. */
683 if(saddr)
684 memcpy(eth->h_source, saddr, dev->addr_len);
685 else
686 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
688 /* Anyway, the loopback-device should never use this function... */
689 if (dev->flags & IFF_LOOPBACK) {
690 int i;
691 for(i = 0; i < dev->addr_len; i++)
692 eth->h_dest[i] = 0;
693 return(dev->hard_header_len);
696 if(daddr) {
697 memcpy(eth->h_dest, daddr, dev->addr_len);
698 return dev->hard_header_len;
700 return -dev->hard_header_len;
703 /* Rebuild the MyriNet MAC header. This is called after an ARP
704 * (or in future other address resolution) has completed on this
705 * sk_buff. We now let ARP fill in the other fields.
707 static int myri_rebuild_header(struct sk_buff *skb)
709 unsigned char *pad = (unsigned char *)skb->data;
710 struct ethhdr *eth = (struct ethhdr *)(pad + MYRI_PAD_LEN);
711 struct net_device *dev = skb->dev;
713 #ifdef DEBUG_HEADER
714 DHDR(("myri_rebuild_header: pad[%02x,%02x] ", pad[0], pad[1]));
715 dump_ehdr(eth);
716 #endif
718 /* Refill MyriNet padding identifiers, this is just being anal. */
719 pad[0] = MYRI_PAD_LEN;
720 pad[1] = 0xab;
722 switch (eth->h_proto)
724 #ifdef CONFIG_INET
725 case __constant_htons(ETH_P_IP):
726 return arp_find(eth->h_dest, skb);
727 #endif
729 default:
730 printk(KERN_DEBUG
731 "%s: unable to resolve type %X addresses.\n",
732 dev->name, (int)eth->h_proto);
734 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
735 return 0;
736 break;
739 return 0;
742 int myri_header_cache(struct neighbour *neigh, struct hh_cache *hh)
744 unsigned short type = hh->hh_type;
745 unsigned char *pad = (unsigned char *)hh->hh_data;
746 struct ethhdr *eth = (struct ethhdr *)(pad + MYRI_PAD_LEN);
747 struct net_device *dev = neigh->dev;
749 if (type == __constant_htons(ETH_P_802_3))
750 return -1;
752 /* Refill MyriNet padding identifiers, this is just being anal. */
753 pad[0] = MYRI_PAD_LEN;
754 pad[1] = 0xab;
756 eth->h_proto = type;
757 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
758 memcpy(eth->h_dest, neigh->ha, dev->addr_len);
759 hh->hh_len = 16;
760 return 0;
764 /* Called by Address Resolution module to notify changes in address. */
765 void myri_header_cache_update(struct hh_cache *hh, struct net_device *dev, unsigned char * haddr)
767 memcpy(((u8*)hh->hh_data) + 2, haddr, dev->addr_len);
770 static int myri_change_mtu(struct net_device *dev, int new_mtu)
772 if ((new_mtu < (ETH_HLEN + MYRI_PAD_LEN)) || (new_mtu > MYRINET_MTU))
773 return -EINVAL;
774 dev->mtu = new_mtu;
775 return 0;
778 static struct net_device_stats *myri_get_stats(struct net_device *dev)
779 { return &(((struct myri_eth *)dev->priv)->enet_stats); }
781 #define CRC_POLYNOMIAL_BE 0x04c11db7UL /* Ethernet CRC, big endian */
782 #define CRC_POLYNOMIAL_LE 0xedb88320UL /* Ethernet CRC, little endian */
784 static void myri_set_multicast(struct net_device *dev)
786 /* Do nothing, all MyriCOM nodes transmit multicast frames
787 * as broadcast packets...
791 static inline void set_boardid_from_idprom(struct myri_eth *mp, int num)
793 mp->eeprom.id[0] = 0;
794 mp->eeprom.id[1] = idprom->id_machtype;
795 mp->eeprom.id[2] = (idprom->id_sernum >> 16) & 0xff;
796 mp->eeprom.id[3] = (idprom->id_sernum >> 8) & 0xff;
797 mp->eeprom.id[4] = (idprom->id_sernum >> 0) & 0xff;
798 mp->eeprom.id[5] = num;
801 static inline void determine_reg_space_size(struct myri_eth *mp)
803 switch(mp->eeprom.cpuvers) {
804 case CPUVERS_2_3:
805 case CPUVERS_3_0:
806 case CPUVERS_3_1:
807 case CPUVERS_3_2:
808 mp->reg_size = (3 * 128 * 1024) + 4096;
809 break;
811 case CPUVERS_4_0:
812 case CPUVERS_4_1:
813 mp->reg_size = ((4096<<1) + mp->eeprom.ramsz);
814 break;
816 case CPUVERS_4_2:
817 case CPUVERS_5_0:
818 default:
819 printk("myricom: AIEEE weird cpu version %04x assuming pre4.0\n",
820 mp->eeprom.cpuvers);
821 mp->reg_size = (3 * 128 * 1024) + 4096;
825 #ifdef DEBUG_DETECT
826 static void dump_eeprom(struct myri_eth *mp)
828 printk("EEPROM: clockval[%08x] cpuvers[%04x] "
829 "id[%02x,%02x,%02x,%02x,%02x,%02x]\n",
830 mp->eeprom.cval, mp->eeprom.cpuvers,
831 mp->eeprom.id[0], mp->eeprom.id[1], mp->eeprom.id[2],
832 mp->eeprom.id[3], mp->eeprom.id[4], mp->eeprom.id[5]);
833 printk("EEPROM: ramsz[%08x]\n", mp->eeprom.ramsz);
834 printk("EEPROM: fvers[%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
835 mp->eeprom.fvers[0], mp->eeprom.fvers[1], mp->eeprom.fvers[2],
836 mp->eeprom.fvers[3], mp->eeprom.fvers[4], mp->eeprom.fvers[5],
837 mp->eeprom.fvers[6], mp->eeprom.fvers[7]);
838 printk("EEPROM: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
839 mp->eeprom.fvers[8], mp->eeprom.fvers[9], mp->eeprom.fvers[10],
840 mp->eeprom.fvers[11], mp->eeprom.fvers[12], mp->eeprom.fvers[13],
841 mp->eeprom.fvers[14], mp->eeprom.fvers[15]);
842 printk("EEPROM: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
843 mp->eeprom.fvers[16], mp->eeprom.fvers[17], mp->eeprom.fvers[18],
844 mp->eeprom.fvers[19], mp->eeprom.fvers[20], mp->eeprom.fvers[21],
845 mp->eeprom.fvers[22], mp->eeprom.fvers[23]);
846 printk("EEPROM: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x]\n",
847 mp->eeprom.fvers[24], mp->eeprom.fvers[25], mp->eeprom.fvers[26],
848 mp->eeprom.fvers[27], mp->eeprom.fvers[28], mp->eeprom.fvers[29],
849 mp->eeprom.fvers[30], mp->eeprom.fvers[31]);
850 printk("EEPROM: mvers[%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
851 mp->eeprom.mvers[0], mp->eeprom.mvers[1], mp->eeprom.mvers[2],
852 mp->eeprom.mvers[3], mp->eeprom.mvers[4], mp->eeprom.mvers[5],
853 mp->eeprom.mvers[6], mp->eeprom.mvers[7]);
854 printk("EEPROM: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x]\n",
855 mp->eeprom.mvers[8], mp->eeprom.mvers[9], mp->eeprom.mvers[10],
856 mp->eeprom.mvers[11], mp->eeprom.mvers[12], mp->eeprom.mvers[13],
857 mp->eeprom.mvers[14], mp->eeprom.mvers[15]);
858 printk("EEPROM: dlval[%04x] brd_type[%04x] bus_type[%04x] prod_code[%04x]\n",
859 mp->eeprom.dlval, mp->eeprom.brd_type, mp->eeprom.bus_type,
860 mp->eeprom.prod_code);
861 printk("EEPROM: serial_num[%08x]\n", mp->eeprom.serial_num);
863 #endif
865 static inline int myri_ether_init(struct net_device *dev, struct linux_sbus_device *sdev, int num)
867 static unsigned version_printed = 0;
868 struct myri_eth *mp;
869 unsigned char prop_buf[32];
870 int i;
872 DET(("myri_ether_init(%p,%p,%d):\n", dev, sdev, num));
873 dev = init_etherdev(0, sizeof(struct myri_eth));
875 if(version_printed++ == 0)
876 printk(version);
878 printk("%s: MyriCOM MyriNET Ethernet ", dev->name);
879 dev->base_addr = (long) sdev;
881 mp = (struct myri_eth *) dev->priv;
882 mp->myri_sbus_dev = sdev;
884 /* Clean out skb arrays. */
885 for(i = 0; i < (RX_RING_SIZE + 1); i++)
886 mp->rx_skbs[i] = NULL;
888 for(i = 0; i < TX_RING_SIZE; i++)
889 mp->tx_skbs[i] = NULL;
891 /* First check for EEPROM information. */
892 i = prom_getproperty(sdev->prom_node, "myrinet-eeprom-info",
893 (char *)&mp->eeprom, sizeof(struct myri_eeprom));
894 DET(("prom_getprop(myrinet-eeprom-info) returns %d\n", i));
895 if(i == 0 || i == -1) {
896 /* No eeprom property, must cook up the values ourselves. */
897 DET(("No EEPROM: "));
898 mp->eeprom.bus_type = BUS_TYPE_SBUS;
899 mp->eeprom.cpuvers = prom_getintdefault(sdev->prom_node,"cpu_version",0);
900 mp->eeprom.cval = prom_getintdefault(sdev->prom_node,"clock_value",0);
901 mp->eeprom.ramsz = prom_getintdefault(sdev->prom_node,"sram_size",0);
902 DET(("cpuvers[%d] cval[%d] ramsz[%d]\n", mp->eeprom.cpuvers,
903 mp->eeprom.cval, mp->eeprom.ramsz));
904 if(mp->eeprom.cpuvers == 0) {
905 DET(("EEPROM: cpuvers was zero, setting to %04x\n",CPUVERS_2_3));
906 mp->eeprom.cpuvers = CPUVERS_2_3;
908 if(mp->eeprom.cpuvers < CPUVERS_3_0) {
909 DET(("EEPROM: cpuvers < CPUVERS_3_0, clockval set to zero.\n"));
910 mp->eeprom.cval = 0;
912 if(mp->eeprom.ramsz == 0) {
913 DET(("EEPROM: ramsz == 0, setting to 128k\n"));
914 mp->eeprom.ramsz = (128 * 1024);
916 i = prom_getproperty(sdev->prom_node, "myrinet-board-id",
917 &prop_buf[0], 10);
918 DET(("EEPROM: prom_getprop(myrinet-board-id) returns %d\n", i));
919 if((i != 0) && (i != -1))
920 memcpy(&mp->eeprom.id[0], &prop_buf[0], 6);
921 else
922 set_boardid_from_idprom(mp, num);
923 i = prom_getproperty(sdev->prom_node, "fpga_version",
924 &mp->eeprom.fvers[0], 32);
925 DET(("EEPROM: prom_getprop(fpga_version) returns %d\n", i));
926 if(i == 0 || i == -1)
927 memset(&mp->eeprom.fvers[0], 0, 32);
929 if(mp->eeprom.cpuvers == CPUVERS_4_1) {
930 DET(("EEPROM: cpuvers CPUVERS_4_1, "));
931 if(mp->eeprom.ramsz == (128 * 1024)) {
932 DET(("ramsize 128k, setting to 256k, "));
933 mp->eeprom.ramsz = (256 * 1024);
935 if((mp->eeprom.cval==0x40414041)||(mp->eeprom.cval==0x90449044)){
936 DET(("changing cval from %08x to %08x ",
937 mp->eeprom.cval, 0x50e450e4));
938 mp->eeprom.cval = 0x50e450e4;
940 DET(("\n"));
943 #ifdef DEBUG_DETECT
944 dump_eeprom(mp);
945 #endif
947 for(i = 0; i < 6; i++)
948 printk("%2.2x%c",
949 dev->dev_addr[i] = mp->eeprom.id[i],
950 i == 5 ? ' ' : ':');
951 printk("\n");
953 determine_reg_space_size(mp);
955 /* Map in the MyriCOM register/localram set. */
956 prom_apply_sbus_ranges(sdev->my_bus, &sdev->reg_addrs[0],
957 sdev->num_registers, sdev);
958 if(mp->eeprom.cpuvers < CPUVERS_4_0) {
959 /* XXX Makes no sense, if control reg is non-existant this
960 * XXX driver cannot function at all... maybe pre-4.0 is
961 * XXX only a valid version for PCI cards? Ask feldy...
963 DET(("Mapping regs for cpuvers < CPUVERS_4_0\n"));
964 mp->regs = (struct myri_regs *)
965 sparc_alloc_io(sdev->reg_addrs[0].phys_addr, 0,
966 mp->reg_size, "MyriCOM Regs",
967 sdev->reg_addrs[0].which_io, 0);
968 if(!mp->regs) {
969 printk("MyriCOM: Cannot map MyriCOM registers.\n");
970 return ENODEV;
972 mp->lanai = (unsigned short *) (((unsigned long)mp->regs) + (256*1024));
973 mp->lanai3 = (unsigned int *) mp->lanai;
974 mp->lregs = (struct lanai_regs *) &mp->lanai[0x10000];
975 } else {
976 DET(("Mapping regs for cpuvers >= CPUVERS_4_0\n"));
977 mp->cregs = (struct myri_control *)
978 sparc_alloc_io(sdev->reg_addrs[0].phys_addr, 0,
979 PAGE_SIZE, "MyriCOM Control Regs",
980 sdev->reg_addrs[0].which_io, 0);
981 mp->lregs = (struct lanai_regs *)
982 sparc_alloc_io(sdev->reg_addrs[0].phys_addr + (256 * 1024),
983 0, PAGE_SIZE, "MyriCOM LANAI Regs",
984 sdev->reg_addrs[0].which_io, 0);
985 mp->lanai = (unsigned short *)
986 sparc_alloc_io(sdev->reg_addrs[0].phys_addr + (512 * 1024),
987 0, mp->eeprom.ramsz, "MyriCOM SRAM",
988 sdev->reg_addrs[0].which_io, 0);
989 mp->lanai3 = (unsigned int *) mp->lanai;
991 DET(("Registers mapped: cregs[%p] lregs[%p] lanai[%p] lanai3[%p]\n",
992 mp->cregs, mp->lregs, mp->lanai, mp->lanai3));
994 if(mp->eeprom.cpuvers >= CPUVERS_4_0)
995 mp->shmem_base = 0xf000;
996 else
997 mp->shmem_base = 0x8000;
999 DET(("Shared memory base is %04x, ", mp->shmem_base));
1001 mp->shmem = (struct myri_shmem *) &mp->lanai[mp->shmem_base];
1002 DET(("shmem mapped at %p\n", mp->shmem));
1004 mp->rqack = &mp->shmem->channel.recvqa;
1005 mp->rq = &mp->shmem->channel.recvq;
1006 mp->sq = &mp->shmem->channel.sendq;
1008 /* Reset the board. */
1009 DET(("Resetting LANAI\n"));
1010 myri_reset_off(mp->lregs, mp->cregs);
1011 myri_reset_on(mp->cregs);
1013 /* Turn IRQ's off. */
1014 myri_disable_irq(mp->lregs, mp->cregs);
1016 /* Reset once more. */
1017 myri_reset_on(mp->cregs);
1019 /* Get the supported DVMA burst sizes from our SBUS. */
1020 mp->myri_bursts = prom_getintdefault(mp->myri_sbus_dev->my_bus->prom_node,
1021 "burst-sizes", 0x00);
1023 #if 1 /* XXX Until sun4m SBUS burst workaround is written. */
1024 if(sparc_cpu_model == sun4m)
1025 mp->myri_bursts &= ~(DMA_BURST64);
1026 #endif
1027 DET(("MYRI bursts %02x\n", mp->myri_bursts));
1029 /* Encode SBUS interrupt level in second control register. */
1030 i = prom_getint(sdev->prom_node, "interrupts");
1031 if(i == 0)
1032 i = 4;
1033 DET(("prom_getint(interrupts)==%d, irqlvl set to %04x\n",
1034 i, (1 << i)));
1035 mp->cregs->irqlvl = (1 << i);
1037 mp->dev = dev;
1038 dev->open = &myri_open;
1039 dev->stop = &myri_close;
1040 dev->hard_start_xmit = &myri_start_xmit;
1041 dev->get_stats = &myri_get_stats;
1042 dev->set_multicast_list = &myri_set_multicast;
1043 dev->irq = sdev->irqs[0];
1044 dev->dma = 0;
1046 /* Register interrupt handler now. */
1047 DET(("Requesting MYRIcom IRQ line.\n"));
1048 if(request_irq(dev->irq, &myri_interrupt,
1049 SA_SHIRQ, "MyriCOM Ethernet", (void *) dev)) {
1050 printk("MyriCOM: Cannot register interrupt handler.\n");
1051 return ENODEV;
1054 DET(("ether_setup()\n"));
1055 ether_setup(dev);
1057 dev->mtu = MYRINET_MTU;
1058 dev->change_mtu = myri_change_mtu;
1059 dev->hard_header = myri_header;
1060 dev->rebuild_header = myri_rebuild_header;
1061 dev->hard_header_len = (ETH_HLEN + MYRI_PAD_LEN);
1062 dev->hard_header_cache = myri_header_cache;
1063 dev->header_cache_update= myri_header_cache_update;
1065 /* Load code onto the LANai. */
1066 DET(("Loading LANAI firmware\n"));
1067 myri_load_lanai(mp);
1069 #ifdef MODULE
1070 dev->ifindex = dev_new_index();
1071 mp->next_module = root_myri_dev;
1072 root_myri_dev = mp;
1073 #endif
1074 return 0;
1077 int __init myri_sbus_probe(struct net_device *dev)
1079 struct linux_sbus *bus;
1080 struct linux_sbus_device *sdev = 0;
1081 static int called = 0;
1082 int cards = 0, v;
1084 if(called)
1085 return ENODEV;
1086 called++;
1088 for_each_sbus(bus) {
1089 for_each_sbusdev(sdev, bus) {
1090 if(cards) dev = NULL;
1091 if(!strcmp(sdev->prom_name, "MYRICOM,mlanai") ||
1092 !strcmp(sdev->prom_name, "myri")) {
1093 cards++;
1094 DET(("Found myricom myrinet as %s\n", sdev->prom_name));
1095 if((v = myri_ether_init(dev, sdev, (cards - 1))))
1096 return v;
1100 if(!cards)
1101 return ENODEV;
1102 return 0;
1105 #ifdef MODULE
1108 init_module(void)
1110 root_myri_dev = NULL;
1111 return myri_sbus_probe(NULL);
1114 void
1115 cleanup_module(void)
1117 struct myri_eth *mp;
1119 /* No need to check MOD_IN_USE, as sys_delete_module() checks. */
1120 while (root_myri_dev) {
1121 mp = root_myri_dev->next_module;
1123 unregister_netdev(root_myri_dev->dev);
1124 kfree(root_myri_dev->dev);
1125 root_myri_dev = mp;
1129 #endif /* MODULE */