1 /* myri_sbus.c: MyriCOM MyriNET SBUS card driver.
3 * Copyright (C) 1996, 1999, 2006 David S. Miller (davem@davemloft.net)
6 static char version[] =
7 "myri_sbus.c:v2.0 June 23, 2006 David S. Miller (davem@davemloft.net)\n";
9 #include <linux/module.h>
10 #include <linux/config.h>
11 #include <linux/errno.h>
12 #include <linux/kernel.h>
13 #include <linux/types.h>
14 #include <linux/fcntl.h>
15 #include <linux/interrupt.h>
16 #include <linux/ioport.h>
18 #include <linux/slab.h>
19 #include <linux/string.h>
20 #include <linux/delay.h>
21 #include <linux/init.h>
22 #include <linux/netdevice.h>
23 #include <linux/etherdevice.h>
24 #include <linux/skbuff.h>
25 #include <linux/bitops.h>
32 #include <asm/system.h>
35 #include <asm/byteorder.h>
36 #include <asm/idprom.h>
38 #include <asm/openprom.h>
39 #include <asm/oplib.h>
40 #include <asm/auxio.h>
41 #include <asm/pgtable.h>
43 #include <asm/checksum.h>
45 #include "myri_sbus.h"
46 #include "myri_code.h"
48 /* #define DEBUG_DETECT */
49 /* #define DEBUG_IRQ */
50 /* #define DEBUG_TRANSMIT */
51 /* #define DEBUG_RECEIVE */
52 /* #define DEBUG_HEADER */
55 #define DET(x) printk x
61 #define DIRQ(x) printk x
67 #define DTX(x) printk x
73 #define DRX(x) printk x
79 #define DHDR(x) printk x
84 static void myri_reset_off(void __iomem *lp, void __iomem *cregs)
87 sbus_writel(0, lp + LANAI_EIMASK);
89 /* Turn RESET function off. */
90 sbus_writel(CONTROL_ROFF, cregs + MYRICTRL_CTRL);
93 static void myri_reset_on(void __iomem *cregs)
95 /* Enable RESET function. */
96 sbus_writel(CONTROL_RON, cregs + MYRICTRL_CTRL);
99 sbus_writel(CONTROL_DIRQ, cregs + MYRICTRL_CTRL);
102 static void myri_disable_irq(void __iomem *lp, void __iomem *cregs)
104 sbus_writel(CONTROL_DIRQ, cregs + MYRICTRL_CTRL);
105 sbus_writel(0, lp + LANAI_EIMASK);
106 sbus_writel(ISTAT_HOST, lp + LANAI_ISTAT);
109 static void myri_enable_irq(void __iomem *lp, void __iomem *cregs)
111 sbus_writel(CONTROL_EIRQ, cregs + MYRICTRL_CTRL);
112 sbus_writel(ISTAT_HOST, lp + LANAI_EIMASK);
115 static inline void bang_the_chip(struct myri_eth *mp)
117 struct myri_shmem __iomem *shmem = mp->shmem;
118 void __iomem *cregs = mp->cregs;
120 sbus_writel(1, &shmem->send);
121 sbus_writel(CONTROL_WON, cregs + MYRICTRL_CTRL);
124 static int myri_do_handshake(struct myri_eth *mp)
126 struct myri_shmem __iomem *shmem = mp->shmem;
127 void __iomem *cregs = mp->cregs;
128 struct myri_channel __iomem *chan = &shmem->channel;
131 DET(("myri_do_handshake: "));
132 if (sbus_readl(&chan->state) == STATE_READY) {
133 DET(("Already STATE_READY, failed.\n"));
134 return -1; /* We're hosed... */
137 myri_disable_irq(mp->lregs, cregs);
139 while (tick++ <= 25) {
143 DET(("shakedown, CONTROL_WON, "));
144 sbus_writel(1, &shmem->shakedown);
145 sbus_writel(CONTROL_WON, cregs + MYRICTRL_CTRL);
147 softstate = sbus_readl(&chan->state);
148 DET(("chanstate[%08x] ", softstate));
149 if (softstate == STATE_READY) {
150 DET(("wakeup successful, "));
154 if (softstate != STATE_WFN) {
155 DET(("not WFN setting that, "));
156 sbus_writel(STATE_WFN, &chan->state);
162 myri_enable_irq(mp->lregs, cregs);
165 DET(("25 ticks we lose, failure.\n"));
172 static int myri_load_lanai(struct myri_eth *mp)
174 struct net_device *dev = mp->dev;
175 struct myri_shmem __iomem *shmem = mp->shmem;
179 myri_disable_irq(mp->lregs, mp->cregs);
180 myri_reset_on(mp->cregs);
183 for (i = 0; i < mp->eeprom.ramsz; i++)
184 sbus_writeb(0, rptr + i);
186 if (mp->eeprom.cpuvers >= CPUVERS_3_0)
187 sbus_writel(mp->eeprom.cval, mp->lregs + LANAI_CVAL);
189 /* Load executable code. */
190 for (i = 0; i < sizeof(lanai4_code); i++)
191 sbus_writeb(lanai4_code[i], rptr + (lanai4_code_off * 2) + i);
193 /* Load data segment. */
194 for (i = 0; i < sizeof(lanai4_data); i++)
195 sbus_writeb(lanai4_data[i], rptr + (lanai4_data_off * 2) + i);
197 /* Set device address. */
198 sbus_writeb(0, &shmem->addr[0]);
199 sbus_writeb(0, &shmem->addr[1]);
200 for (i = 0; i < 6; i++)
201 sbus_writeb(dev->dev_addr[i],
202 &shmem->addr[i + 2]);
204 /* Set SBUS bursts and interrupt mask. */
205 sbus_writel(((mp->myri_bursts & 0xf8) >> 3), &shmem->burst);
206 sbus_writel(SHMEM_IMASK_RX, &shmem->imask);
208 /* Release the LANAI. */
209 myri_disable_irq(mp->lregs, mp->cregs);
210 myri_reset_off(mp->lregs, mp->cregs);
211 myri_disable_irq(mp->lregs, mp->cregs);
213 /* Wait for the reset to complete. */
214 for (i = 0; i < 5000; i++) {
215 if (sbus_readl(&shmem->channel.state) != STATE_READY)
222 printk(KERN_ERR "myricom: Chip would not reset after firmware load.\n");
224 i = myri_do_handshake(mp);
226 printk(KERN_ERR "myricom: Handshake with LANAI failed.\n");
228 if (mp->eeprom.cpuvers == CPUVERS_4_0)
229 sbus_writel(0, mp->lregs + LANAI_VERS);
234 static void myri_clean_rings(struct myri_eth *mp)
236 struct sendq __iomem *sq = mp->sq;
237 struct recvq __iomem *rq = mp->rq;
240 sbus_writel(0, &rq->tail);
241 sbus_writel(0, &rq->head);
242 for (i = 0; i < (RX_RING_SIZE+1); i++) {
243 if (mp->rx_skbs[i] != NULL) {
244 struct myri_rxd __iomem *rxd = &rq->myri_rxd[i];
247 dma_addr = sbus_readl(&rxd->myri_scatters[0].addr);
248 sbus_unmap_single(mp->myri_sdev, dma_addr, RX_ALLOC_SIZE, SBUS_DMA_FROMDEVICE);
249 dev_kfree_skb(mp->rx_skbs[i]);
250 mp->rx_skbs[i] = NULL;
255 sbus_writel(0, &sq->tail);
256 sbus_writel(0, &sq->head);
257 for (i = 0; i < TX_RING_SIZE; i++) {
258 if (mp->tx_skbs[i] != NULL) {
259 struct sk_buff *skb = mp->tx_skbs[i];
260 struct myri_txd __iomem *txd = &sq->myri_txd[i];
263 dma_addr = sbus_readl(&txd->myri_gathers[0].addr);
264 sbus_unmap_single(mp->myri_sdev, dma_addr, (skb->len + 3) & ~3, SBUS_DMA_TODEVICE);
265 dev_kfree_skb(mp->tx_skbs[i]);
266 mp->tx_skbs[i] = NULL;
271 static void myri_init_rings(struct myri_eth *mp, int from_irq)
273 struct recvq __iomem *rq = mp->rq;
274 struct myri_rxd __iomem *rxd = &rq->myri_rxd[0];
275 struct net_device *dev = mp->dev;
276 gfp_t gfp_flags = GFP_KERNEL;
279 if (from_irq || in_interrupt())
280 gfp_flags = GFP_ATOMIC;
282 myri_clean_rings(mp);
283 for (i = 0; i < RX_RING_SIZE; i++) {
284 struct sk_buff *skb = myri_alloc_skb(RX_ALLOC_SIZE, gfp_flags);
289 mp->rx_skbs[i] = skb;
291 skb_put(skb, RX_ALLOC_SIZE);
293 dma_addr = sbus_map_single(mp->myri_sdev, skb->data, RX_ALLOC_SIZE, SBUS_DMA_FROMDEVICE);
294 sbus_writel(dma_addr, &rxd[i].myri_scatters[0].addr);
295 sbus_writel(RX_ALLOC_SIZE, &rxd[i].myri_scatters[0].len);
296 sbus_writel(i, &rxd[i].ctx);
297 sbus_writel(1, &rxd[i].num_sg);
299 sbus_writel(0, &rq->head);
300 sbus_writel(RX_RING_SIZE, &rq->tail);
303 static int myri_init(struct myri_eth *mp, int from_irq)
305 myri_init_rings(mp, from_irq);
309 static void myri_is_not_so_happy(struct myri_eth *mp)
314 static void dump_ehdr(struct ethhdr *ehdr)
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],
325 static void dump_ehdr_and_myripad(unsigned char *stuff)
327 struct ethhdr *ehdr = (struct ethhdr *) (stuff + 2);
329 printk("pad[%02x:%02x]", stuff[0], stuff[1]);
330 printk("ehdr[h_dst(%02x:%02x:%02x:%02x:%02x:%02x)"
331 "h_source(%02x:%02x:%02x:%02x:%02x:%02x)h_proto(%04x)]\n",
332 ehdr->h_dest[0], ehdr->h_dest[1], ehdr->h_dest[2],
333 ehdr->h_dest[3], ehdr->h_dest[4], ehdr->h_dest[4],
334 ehdr->h_source[0], ehdr->h_source[1], ehdr->h_source[2],
335 ehdr->h_source[3], ehdr->h_source[4], ehdr->h_source[4],
340 static void myri_tx(struct myri_eth *mp, struct net_device *dev)
342 struct sendq __iomem *sq= mp->sq;
343 int entry = mp->tx_old;
344 int limit = sbus_readl(&sq->head);
346 DTX(("entry[%d] limit[%d] ", entry, limit));
349 while (entry != limit) {
350 struct sk_buff *skb = mp->tx_skbs[entry];
353 DTX(("SKB[%d] ", entry));
354 dma_addr = sbus_readl(&sq->myri_txd[entry].myri_gathers[0].addr);
355 sbus_unmap_single(mp->myri_sdev, dma_addr, skb->len, SBUS_DMA_TODEVICE);
357 mp->tx_skbs[entry] = NULL;
358 mp->enet_stats.tx_packets++;
359 entry = NEXT_TX(entry);
364 /* Determine the packet's protocol ID. The rule here is that we
365 * assume 802.3 if the type field is short enough to be a length.
366 * This is normal practice and works for any 'now in use' protocol.
368 static __be16 myri_type_trans(struct sk_buff *skb, struct net_device *dev)
373 skb->mac.raw = (((unsigned char *)skb->data) + MYRI_PAD_LEN);
374 skb_pull(skb, dev->hard_header_len);
378 DHDR(("myri_type_trans: "));
381 if (*eth->h_dest & 1) {
382 if (memcmp(eth->h_dest, dev->broadcast, ETH_ALEN)==0)
383 skb->pkt_type = PACKET_BROADCAST;
385 skb->pkt_type = PACKET_MULTICAST;
386 } else if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) {
387 if (memcmp(eth->h_dest, dev->dev_addr, ETH_ALEN))
388 skb->pkt_type = PACKET_OTHERHOST;
391 if (ntohs(eth->h_proto) >= 1536)
396 /* This is a magic hack to spot IPX packets. Older Novell breaks
397 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
398 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
399 * won't work for fault tolerant netware but does for the rest.
401 if (*(unsigned short *)rawp == 0xFFFF)
402 return htons(ETH_P_802_3);
405 return htons(ETH_P_802_2);
408 static void myri_rx(struct myri_eth *mp, struct net_device *dev)
410 struct recvq __iomem *rq = mp->rq;
411 struct recvq __iomem *rqa = mp->rqack;
412 int entry = sbus_readl(&rqa->head);
413 int limit = sbus_readl(&rqa->tail);
416 DRX(("entry[%d] limit[%d] ", entry, limit));
421 while (entry != limit) {
422 struct myri_rxd __iomem *rxdack = &rqa->myri_rxd[entry];
423 u32 csum = sbus_readl(&rxdack->csum);
424 int len = sbus_readl(&rxdack->myri_scatters[0].len);
425 int index = sbus_readl(&rxdack->ctx);
426 struct myri_rxd __iomem *rxd = &rq->myri_rxd[sbus_readl(&rq->tail)];
427 struct sk_buff *skb = mp->rx_skbs[index];
430 sbus_writel(NEXT_RX(entry), &rqa->head);
432 /* Check for errors. */
433 DRX(("rxd[%d]: %p len[%d] csum[%08x] ", entry, rxd, len, csum));
434 sbus_dma_sync_single_for_cpu(mp->myri_sdev,
435 sbus_readl(&rxd->myri_scatters[0].addr),
436 RX_ALLOC_SIZE, SBUS_DMA_FROMDEVICE);
437 if (len < (ETH_HLEN + MYRI_PAD_LEN) || (skb->data[0] != MYRI_PAD_LEN)) {
439 mp->enet_stats.rx_errors++;
440 if (len < (ETH_HLEN + MYRI_PAD_LEN)) {
441 DRX(("BAD_LENGTH] "));
442 mp->enet_stats.rx_length_errors++;
444 DRX(("NO_PADDING] "));
445 mp->enet_stats.rx_frame_errors++;
448 /* Return it to the LANAI. */
452 mp->enet_stats.rx_dropped++;
453 sbus_dma_sync_single_for_device(mp->myri_sdev,
454 sbus_readl(&rxd->myri_scatters[0].addr),
456 SBUS_DMA_FROMDEVICE);
457 sbus_writel(RX_ALLOC_SIZE, &rxd->myri_scatters[0].len);
458 sbus_writel(index, &rxd->ctx);
459 sbus_writel(1, &rxd->num_sg);
460 sbus_writel(NEXT_RX(sbus_readl(&rq->tail)), &rq->tail);
464 DRX(("len[%d] ", len));
465 if (len > RX_COPY_THRESHOLD) {
466 struct sk_buff *new_skb;
470 new_skb = myri_alloc_skb(RX_ALLOC_SIZE, GFP_ATOMIC);
471 if (new_skb == NULL) {
472 DRX(("skb_alloc(FAILED) "));
475 sbus_unmap_single(mp->myri_sdev,
476 sbus_readl(&rxd->myri_scatters[0].addr),
478 SBUS_DMA_FROMDEVICE);
479 mp->rx_skbs[index] = new_skb;
481 skb_put(new_skb, RX_ALLOC_SIZE);
482 dma_addr = sbus_map_single(mp->myri_sdev,
485 SBUS_DMA_FROMDEVICE);
486 sbus_writel(dma_addr, &rxd->myri_scatters[0].addr);
487 sbus_writel(RX_ALLOC_SIZE, &rxd->myri_scatters[0].len);
488 sbus_writel(index, &rxd->ctx);
489 sbus_writel(1, &rxd->num_sg);
490 sbus_writel(NEXT_RX(sbus_readl(&rq->tail)), &rq->tail);
492 /* Trim the original skb for the netif. */
493 DRX(("trim(%d) ", len));
496 struct sk_buff *copy_skb = dev_alloc_skb(len);
499 if (copy_skb == NULL) {
500 DRX(("dev_alloc_skb(FAILED) "));
503 /* DMA sync already done above. */
505 DRX(("resv_and_put "));
506 skb_put(copy_skb, len);
507 memcpy(copy_skb->data, skb->data, len);
509 /* Reuse original ring buffer. */
511 sbus_dma_sync_single_for_device(mp->myri_sdev,
512 sbus_readl(&rxd->myri_scatters[0].addr),
514 SBUS_DMA_FROMDEVICE);
515 sbus_writel(RX_ALLOC_SIZE, &rxd->myri_scatters[0].len);
516 sbus_writel(index, &rxd->ctx);
517 sbus_writel(1, &rxd->num_sg);
518 sbus_writel(NEXT_RX(sbus_readl(&rq->tail)), &rq->tail);
523 /* Just like the happy meal we get checksums from this card. */
525 skb->ip_summed = CHECKSUM_UNNECESSARY; /* XXX */
527 skb->protocol = myri_type_trans(skb, dev);
528 DRX(("prot[%04x] netif_rx ", skb->protocol));
531 dev->last_rx = jiffies;
532 mp->enet_stats.rx_packets++;
533 mp->enet_stats.rx_bytes += len;
536 entry = NEXT_RX(entry);
540 static irqreturn_t myri_interrupt(int irq, void *dev_id, struct pt_regs *regs)
542 struct net_device *dev = (struct net_device *) dev_id;
543 struct myri_eth *mp = (struct myri_eth *) dev->priv;
544 void __iomem *lregs = mp->lregs;
545 struct myri_channel __iomem *chan = &mp->shmem->channel;
550 spin_lock_irqsave(&mp->irq_lock, flags);
552 status = sbus_readl(lregs + LANAI_ISTAT);
553 DIRQ(("myri_interrupt: status[%08x] ", status));
554 if (status & ISTAT_HOST) {
558 DIRQ(("IRQ_DISAB "));
559 myri_disable_irq(lregs, mp->cregs);
560 softstate = sbus_readl(&chan->state);
561 DIRQ(("state[%08x] ", softstate));
562 if (softstate != STATE_READY) {
563 DIRQ(("myri_not_so_happy "));
564 myri_is_not_so_happy(mp);
566 DIRQ(("\nmyri_rx: "));
568 DIRQ(("\nistat=ISTAT_HOST "));
569 sbus_writel(ISTAT_HOST, lregs + LANAI_ISTAT);
571 myri_enable_irq(lregs, mp->cregs);
575 spin_unlock_irqrestore(&mp->irq_lock, flags);
577 return IRQ_RETVAL(handled);
580 static int myri_open(struct net_device *dev)
582 struct myri_eth *mp = (struct myri_eth *) dev->priv;
584 return myri_init(mp, in_interrupt());
587 static int myri_close(struct net_device *dev)
589 struct myri_eth *mp = (struct myri_eth *) dev->priv;
591 myri_clean_rings(mp);
595 static void myri_tx_timeout(struct net_device *dev)
597 struct myri_eth *mp = (struct myri_eth *) dev->priv;
599 printk(KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
601 mp->enet_stats.tx_errors++;
603 netif_wake_queue(dev);
606 static int myri_start_xmit(struct sk_buff *skb, struct net_device *dev)
608 struct myri_eth *mp = (struct myri_eth *) dev->priv;
609 struct sendq __iomem *sq = mp->sq;
610 struct myri_txd __iomem *txd;
612 unsigned int head, tail;
616 DTX(("myri_start_xmit: "));
620 netif_stop_queue(dev);
622 /* This is just to prevent multiple PIO reads for TX_BUFFS_AVAIL. */
623 head = sbus_readl(&sq->head);
624 tail = sbus_readl(&sq->tail);
626 if (!TX_BUFFS_AVAIL(head, tail)) {
627 DTX(("no buffs available, returning 1\n"));
631 spin_lock_irqsave(&mp->irq_lock, flags);
633 DHDR(("xmit[skbdata(%p)]\n", skb->data));
635 dump_ehdr_and_myripad(((unsigned char *) skb->data));
638 /* XXX Maybe this can go as well. */
642 len = (len + 4) & (~3);
645 entry = sbus_readl(&sq->tail);
647 txd = &sq->myri_txd[entry];
648 mp->tx_skbs[entry] = skb;
650 /* Must do this before we sbus map it. */
651 if (skb->data[MYRI_PAD_LEN] & 0x1) {
652 sbus_writew(0xffff, &txd->addr[0]);
653 sbus_writew(0xffff, &txd->addr[1]);
654 sbus_writew(0xffff, &txd->addr[2]);
655 sbus_writew(0xffff, &txd->addr[3]);
657 sbus_writew(0xffff, &txd->addr[0]);
658 sbus_writew((skb->data[0] << 8) | skb->data[1], &txd->addr[1]);
659 sbus_writew((skb->data[2] << 8) | skb->data[3], &txd->addr[2]);
660 sbus_writew((skb->data[4] << 8) | skb->data[5], &txd->addr[3]);
663 dma_addr = sbus_map_single(mp->myri_sdev, skb->data, len, SBUS_DMA_TODEVICE);
664 sbus_writel(dma_addr, &txd->myri_gathers[0].addr);
665 sbus_writel(len, &txd->myri_gathers[0].len);
666 sbus_writel(1, &txd->num_sg);
667 sbus_writel(KERNEL_CHANNEL, &txd->chan);
668 sbus_writel(len, &txd->len);
669 sbus_writel((u32)-1, &txd->csum_off);
670 sbus_writel(0, &txd->csum_field);
672 sbus_writel(NEXT_TX(entry), &sq->tail);
673 DTX(("BangTheChip "));
676 DTX(("tbusy=0, returning 0\n"));
677 netif_start_queue(dev);
678 spin_unlock_irqrestore(&mp->irq_lock, flags);
682 /* Create the MyriNet MAC header for an arbitrary protocol layer
684 * saddr=NULL means use device source address
685 * daddr=NULL means leave destination address (eg unresolved arp)
687 static int myri_header(struct sk_buff *skb, struct net_device *dev, unsigned short type,
688 void *daddr, void *saddr, unsigned len)
690 struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
691 unsigned char *pad = (unsigned char *) skb_push(skb, MYRI_PAD_LEN);
694 DHDR(("myri_header: pad[%02x,%02x] ", pad[0], pad[1]));
698 /* Set the MyriNET padding identifier. */
699 pad[0] = MYRI_PAD_LEN;
702 /* Set the protocol type. For a packet of type ETH_P_802_3 we put the length
703 * in here instead. It is up to the 802.2 layer to carry protocol information.
705 if (type != ETH_P_802_3)
706 eth->h_proto = htons(type);
708 eth->h_proto = htons(len);
710 /* Set the source hardware address. */
712 memcpy(eth->h_source, saddr, dev->addr_len);
714 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
716 /* Anyway, the loopback-device should never use this function... */
717 if (dev->flags & IFF_LOOPBACK) {
719 for (i = 0; i < dev->addr_len; i++)
721 return(dev->hard_header_len);
725 memcpy(eth->h_dest, daddr, dev->addr_len);
726 return dev->hard_header_len;
728 return -dev->hard_header_len;
731 /* Rebuild the MyriNet MAC header. This is called after an ARP
732 * (or in future other address resolution) has completed on this
733 * sk_buff. We now let ARP fill in the other fields.
735 static int myri_rebuild_header(struct sk_buff *skb)
737 unsigned char *pad = (unsigned char *) skb->data;
738 struct ethhdr *eth = (struct ethhdr *) (pad + MYRI_PAD_LEN);
739 struct net_device *dev = skb->dev;
742 DHDR(("myri_rebuild_header: pad[%02x,%02x] ", pad[0], pad[1]));
746 /* Refill MyriNet padding identifiers, this is just being anal. */
747 pad[0] = MYRI_PAD_LEN;
750 switch (eth->h_proto)
753 case __constant_htons(ETH_P_IP):
754 return arp_find(eth->h_dest, skb);
759 "%s: unable to resolve type %X addresses.\n",
760 dev->name, (int)eth->h_proto);
762 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
770 int myri_header_cache(struct neighbour *neigh, struct hh_cache *hh)
772 unsigned short type = hh->hh_type;
775 struct net_device *dev = neigh->dev;
777 pad = ((unsigned char *) hh->hh_data) +
778 HH_DATA_OFF(sizeof(*eth) + MYRI_PAD_LEN);
779 eth = (struct ethhdr *) (pad + MYRI_PAD_LEN);
781 if (type == __constant_htons(ETH_P_802_3))
784 /* Refill MyriNet padding identifiers, this is just being anal. */
785 pad[0] = MYRI_PAD_LEN;
789 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
790 memcpy(eth->h_dest, neigh->ha, dev->addr_len);
796 /* Called by Address Resolution module to notify changes in address. */
797 void myri_header_cache_update(struct hh_cache *hh, struct net_device *dev, unsigned char * haddr)
799 memcpy(((u8*)hh->hh_data) + HH_DATA_OFF(sizeof(struct ethhdr)),
800 haddr, dev->addr_len);
803 static int myri_change_mtu(struct net_device *dev, int new_mtu)
805 if ((new_mtu < (ETH_HLEN + MYRI_PAD_LEN)) || (new_mtu > MYRINET_MTU))
811 static struct net_device_stats *myri_get_stats(struct net_device *dev)
812 { return &(((struct myri_eth *)dev->priv)->enet_stats); }
814 static void myri_set_multicast(struct net_device *dev)
816 /* Do nothing, all MyriCOM nodes transmit multicast frames
817 * as broadcast packets...
821 static inline void set_boardid_from_idprom(struct myri_eth *mp, int num)
823 mp->eeprom.id[0] = 0;
824 mp->eeprom.id[1] = idprom->id_machtype;
825 mp->eeprom.id[2] = (idprom->id_sernum >> 16) & 0xff;
826 mp->eeprom.id[3] = (idprom->id_sernum >> 8) & 0xff;
827 mp->eeprom.id[4] = (idprom->id_sernum >> 0) & 0xff;
828 mp->eeprom.id[5] = num;
831 static inline void determine_reg_space_size(struct myri_eth *mp)
833 switch(mp->eeprom.cpuvers) {
838 mp->reg_size = (3 * 128 * 1024) + 4096;
843 mp->reg_size = ((4096<<1) + mp->eeprom.ramsz);
849 printk("myricom: AIEEE weird cpu version %04x assuming pre4.0\n",
851 mp->reg_size = (3 * 128 * 1024) + 4096;
856 static void dump_eeprom(struct myri_eth *mp)
858 printk("EEPROM: clockval[%08x] cpuvers[%04x] "
859 "id[%02x,%02x,%02x,%02x,%02x,%02x]\n",
860 mp->eeprom.cval, mp->eeprom.cpuvers,
861 mp->eeprom.id[0], mp->eeprom.id[1], mp->eeprom.id[2],
862 mp->eeprom.id[3], mp->eeprom.id[4], mp->eeprom.id[5]);
863 printk("EEPROM: ramsz[%08x]\n", mp->eeprom.ramsz);
864 printk("EEPROM: fvers[%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
865 mp->eeprom.fvers[0], mp->eeprom.fvers[1], mp->eeprom.fvers[2],
866 mp->eeprom.fvers[3], mp->eeprom.fvers[4], mp->eeprom.fvers[5],
867 mp->eeprom.fvers[6], mp->eeprom.fvers[7]);
868 printk("EEPROM: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
869 mp->eeprom.fvers[8], mp->eeprom.fvers[9], mp->eeprom.fvers[10],
870 mp->eeprom.fvers[11], mp->eeprom.fvers[12], mp->eeprom.fvers[13],
871 mp->eeprom.fvers[14], mp->eeprom.fvers[15]);
872 printk("EEPROM: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
873 mp->eeprom.fvers[16], mp->eeprom.fvers[17], mp->eeprom.fvers[18],
874 mp->eeprom.fvers[19], mp->eeprom.fvers[20], mp->eeprom.fvers[21],
875 mp->eeprom.fvers[22], mp->eeprom.fvers[23]);
876 printk("EEPROM: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x]\n",
877 mp->eeprom.fvers[24], mp->eeprom.fvers[25], mp->eeprom.fvers[26],
878 mp->eeprom.fvers[27], mp->eeprom.fvers[28], mp->eeprom.fvers[29],
879 mp->eeprom.fvers[30], mp->eeprom.fvers[31]);
880 printk("EEPROM: mvers[%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
881 mp->eeprom.mvers[0], mp->eeprom.mvers[1], mp->eeprom.mvers[2],
882 mp->eeprom.mvers[3], mp->eeprom.mvers[4], mp->eeprom.mvers[5],
883 mp->eeprom.mvers[6], mp->eeprom.mvers[7]);
884 printk("EEPROM: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x]\n",
885 mp->eeprom.mvers[8], mp->eeprom.mvers[9], mp->eeprom.mvers[10],
886 mp->eeprom.mvers[11], mp->eeprom.mvers[12], mp->eeprom.mvers[13],
887 mp->eeprom.mvers[14], mp->eeprom.mvers[15]);
888 printk("EEPROM: dlval[%04x] brd_type[%04x] bus_type[%04x] prod_code[%04x]\n",
889 mp->eeprom.dlval, mp->eeprom.brd_type, mp->eeprom.bus_type,
890 mp->eeprom.prod_code);
891 printk("EEPROM: serial_num[%08x]\n", mp->eeprom.serial_num);
895 static int __init myri_ether_init(struct sbus_dev *sdev)
898 static unsigned version_printed;
899 struct net_device *dev;
901 unsigned char prop_buf[32];
904 DET(("myri_ether_init(%p,%d):\n", sdev, num));
905 dev = alloc_etherdev(sizeof(struct myri_eth));
910 if (version_printed++ == 0)
913 SET_MODULE_OWNER(dev);
914 SET_NETDEV_DEV(dev, &sdev->ofdev.dev);
916 mp = (struct myri_eth *) dev->priv;
917 spin_lock_init(&mp->irq_lock);
918 mp->myri_sdev = sdev;
920 /* Clean out skb arrays. */
921 for (i = 0; i < (RX_RING_SIZE + 1); i++)
922 mp->rx_skbs[i] = NULL;
924 for (i = 0; i < TX_RING_SIZE; i++)
925 mp->tx_skbs[i] = NULL;
927 /* First check for EEPROM information. */
928 i = prom_getproperty(sdev->prom_node, "myrinet-eeprom-info",
929 (char *)&mp->eeprom, sizeof(struct myri_eeprom));
930 DET(("prom_getprop(myrinet-eeprom-info) returns %d\n", i));
931 if (i == 0 || i == -1) {
932 /* No eeprom property, must cook up the values ourselves. */
933 DET(("No EEPROM: "));
934 mp->eeprom.bus_type = BUS_TYPE_SBUS;
935 mp->eeprom.cpuvers = prom_getintdefault(sdev->prom_node,"cpu_version",0);
936 mp->eeprom.cval = prom_getintdefault(sdev->prom_node,"clock_value",0);
937 mp->eeprom.ramsz = prom_getintdefault(sdev->prom_node,"sram_size",0);
938 DET(("cpuvers[%d] cval[%d] ramsz[%d]\n", mp->eeprom.cpuvers,
939 mp->eeprom.cval, mp->eeprom.ramsz));
940 if (mp->eeprom.cpuvers == 0) {
941 DET(("EEPROM: cpuvers was zero, setting to %04x\n",CPUVERS_2_3));
942 mp->eeprom.cpuvers = CPUVERS_2_3;
944 if (mp->eeprom.cpuvers < CPUVERS_3_0) {
945 DET(("EEPROM: cpuvers < CPUVERS_3_0, clockval set to zero.\n"));
948 if (mp->eeprom.ramsz == 0) {
949 DET(("EEPROM: ramsz == 0, setting to 128k\n"));
950 mp->eeprom.ramsz = (128 * 1024);
952 i = prom_getproperty(sdev->prom_node, "myrinet-board-id",
954 DET(("EEPROM: prom_getprop(myrinet-board-id) returns %d\n", i));
955 if ((i != 0) && (i != -1))
956 memcpy(&mp->eeprom.id[0], &prop_buf[0], 6);
958 set_boardid_from_idprom(mp, num);
959 i = prom_getproperty(sdev->prom_node, "fpga_version",
960 &mp->eeprom.fvers[0], 32);
961 DET(("EEPROM: prom_getprop(fpga_version) returns %d\n", i));
962 if (i == 0 || i == -1)
963 memset(&mp->eeprom.fvers[0], 0, 32);
965 if (mp->eeprom.cpuvers == CPUVERS_4_1) {
966 DET(("EEPROM: cpuvers CPUVERS_4_1, "));
967 if (mp->eeprom.ramsz == (128 * 1024)) {
968 DET(("ramsize 128k, setting to 256k, "));
969 mp->eeprom.ramsz = (256 * 1024);
971 if ((mp->eeprom.cval==0x40414041)||(mp->eeprom.cval==0x90449044)){
972 DET(("changing cval from %08x to %08x ",
973 mp->eeprom.cval, 0x50e450e4));
974 mp->eeprom.cval = 0x50e450e4;
983 for (i = 0; i < 6; i++)
984 dev->dev_addr[i] = mp->eeprom.id[i];
986 determine_reg_space_size(mp);
988 /* Map in the MyriCOM register/localram set. */
989 if (mp->eeprom.cpuvers < CPUVERS_4_0) {
990 /* XXX Makes no sense, if control reg is non-existant this
991 * XXX driver cannot function at all... maybe pre-4.0 is
992 * XXX only a valid version for PCI cards? Ask feldy...
994 DET(("Mapping regs for cpuvers < CPUVERS_4_0\n"));
995 mp->regs = sbus_ioremap(&sdev->resource[0], 0,
996 mp->reg_size, "MyriCOM Regs");
998 printk("MyriCOM: Cannot map MyriCOM registers.\n");
1001 mp->lanai = mp->regs + (256 * 1024);
1002 mp->lregs = mp->lanai + (0x10000 * 2);
1004 DET(("Mapping regs for cpuvers >= CPUVERS_4_0\n"));
1005 mp->cregs = sbus_ioremap(&sdev->resource[0], 0,
1006 PAGE_SIZE, "MyriCOM Control Regs");
1007 mp->lregs = sbus_ioremap(&sdev->resource[0], (256 * 1024),
1008 PAGE_SIZE, "MyriCOM LANAI Regs");
1010 sbus_ioremap(&sdev->resource[0], (512 * 1024),
1011 mp->eeprom.ramsz, "MyriCOM SRAM");
1013 DET(("Registers mapped: cregs[%p] lregs[%p] lanai[%p]\n",
1014 mp->cregs, mp->lregs, mp->lanai));
1016 if (mp->eeprom.cpuvers >= CPUVERS_4_0)
1017 mp->shmem_base = 0xf000;
1019 mp->shmem_base = 0x8000;
1021 DET(("Shared memory base is %04x, ", mp->shmem_base));
1023 mp->shmem = (struct myri_shmem __iomem *)
1024 (mp->lanai + (mp->shmem_base * 2));
1025 DET(("shmem mapped at %p\n", mp->shmem));
1027 mp->rqack = &mp->shmem->channel.recvqa;
1028 mp->rq = &mp->shmem->channel.recvq;
1029 mp->sq = &mp->shmem->channel.sendq;
1031 /* Reset the board. */
1032 DET(("Resetting LANAI\n"));
1033 myri_reset_off(mp->lregs, mp->cregs);
1034 myri_reset_on(mp->cregs);
1036 /* Turn IRQ's off. */
1037 myri_disable_irq(mp->lregs, mp->cregs);
1039 /* Reset once more. */
1040 myri_reset_on(mp->cregs);
1042 /* Get the supported DVMA burst sizes from our SBUS. */
1043 mp->myri_bursts = prom_getintdefault(mp->myri_sdev->bus->prom_node,
1044 "burst-sizes", 0x00);
1046 if (!sbus_can_burst64(sdev))
1047 mp->myri_bursts &= ~(DMA_BURST64);
1049 DET(("MYRI bursts %02x\n", mp->myri_bursts));
1051 /* Encode SBUS interrupt level in second control register. */
1052 i = prom_getint(sdev->prom_node, "interrupts");
1055 DET(("prom_getint(interrupts)==%d, irqlvl set to %04x\n",
1058 sbus_writel((1 << i), mp->cregs + MYRICTRL_IRQLVL);
1061 dev->open = &myri_open;
1062 dev->stop = &myri_close;
1063 dev->hard_start_xmit = &myri_start_xmit;
1064 dev->tx_timeout = &myri_tx_timeout;
1065 dev->watchdog_timeo = 5*HZ;
1066 dev->get_stats = &myri_get_stats;
1067 dev->set_multicast_list = &myri_set_multicast;
1068 dev->irq = sdev->irqs[0];
1070 /* Register interrupt handler now. */
1071 DET(("Requesting MYRIcom IRQ line.\n"));
1072 if (request_irq(dev->irq, &myri_interrupt,
1073 SA_SHIRQ, "MyriCOM Ethernet", (void *) dev)) {
1074 printk("MyriCOM: Cannot register interrupt handler.\n");
1078 dev->mtu = MYRINET_MTU;
1079 dev->change_mtu = myri_change_mtu;
1080 dev->hard_header = myri_header;
1081 dev->rebuild_header = myri_rebuild_header;
1082 dev->hard_header_len = (ETH_HLEN + MYRI_PAD_LEN);
1083 dev->hard_header_cache = myri_header_cache;
1084 dev->header_cache_update= myri_header_cache_update;
1086 /* Load code onto the LANai. */
1087 DET(("Loading LANAI firmware\n"));
1088 myri_load_lanai(mp);
1090 if (register_netdev(dev)) {
1091 printk("MyriCOM: Cannot register device.\n");
1095 dev_set_drvdata(&sdev->ofdev.dev, mp);
1099 printk("%s: MyriCOM MyriNET Ethernet ", dev->name);
1101 for (i = 0; i < 6; i++)
1102 printk("%2.2x%c", dev->dev_addr[i],
1103 i == 5 ? ' ' : ':');
1109 free_irq(dev->irq, dev);
1111 /* This will also free the co-allocated 'dev->priv' */
1117 static int __devinit myri_sbus_probe(struct of_device *dev, const struct of_device_id *match)
1119 struct sbus_dev *sdev = to_sbus_device(&dev->dev);
1121 return myri_ether_init(sdev);
1124 static int __devexit myri_sbus_remove(struct of_device *dev)
1126 struct myri_eth *mp = dev_get_drvdata(&dev->dev);
1127 struct net_device *net_dev = mp->dev;
1129 unregister_netdevice(net_dev);
1131 free_irq(net_dev->irq, net_dev);
1133 if (mp->eeprom.cpuvers < CPUVERS_4_0) {
1134 sbus_iounmap(mp->regs, mp->reg_size);
1136 sbus_iounmap(mp->cregs, PAGE_SIZE);
1137 sbus_iounmap(mp->lregs, (256 * 1024));
1138 sbus_iounmap(mp->lanai, (512 * 1024));
1141 free_netdev(net_dev);
1143 dev_set_drvdata(&dev->dev, NULL);
1148 static struct of_device_id myri_sbus_match[] = {
1150 .name = "MYRICOM,mlanai",
1158 MODULE_DEVICE_TABLE(of, myri_sbus_match);
1160 static struct of_platform_driver myri_sbus_driver = {
1162 .match_table = myri_sbus_match,
1163 .probe = myri_sbus_probe,
1164 .remove = __devexit_p(myri_sbus_remove),
1167 static int __init myri_sbus_init(void)
1169 return of_register_driver(&myri_sbus_driver, &sbus_bus_type);
1172 static void __exit myri_sbus_exit(void)
1174 of_unregister_driver(&myri_sbus_driver);
1177 module_init(myri_sbus_init);
1178 module_exit(myri_sbus_exit);
1180 MODULE_LICENSE("GPL");