blob: 8b4313085359ce703f2f4bd6c91c815c43a2c030 [file] [log] [blame]
David S. Millera46c30f2006-06-23 21:32:48 -07001/* myri_sbus.c: MyriCOM MyriNET SBUS card driver.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 *
David S. Miller9e6b6e72008-08-26 23:36:58 -07003 * Copyright (C) 1996, 1999, 2006, 2008 David S. Miller (davem@davemloft.net)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 */
5
6static char version[] =
David S. Millera46c30f2006-06-23 21:32:48 -07007 "myri_sbus.c:v2.0 June 23, 2006 David S. Miller (davem@davemloft.net)\n";
Linus Torvalds1da177e2005-04-16 15:20:36 -07008
9#include <linux/module.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/errno.h>
11#include <linux/kernel.h>
12#include <linux/types.h>
13#include <linux/fcntl.h>
14#include <linux/interrupt.h>
15#include <linux/ioport.h>
16#include <linux/in.h>
17#include <linux/slab.h>
18#include <linux/string.h>
19#include <linux/delay.h>
20#include <linux/init.h>
21#include <linux/netdevice.h>
22#include <linux/etherdevice.h>
23#include <linux/skbuff.h>
24#include <linux/bitops.h>
David S. Miller738f2b72008-08-27 18:09:11 -070025#include <linux/dma-mapping.h>
David S. Miller9e6b6e72008-08-26 23:36:58 -070026#include <linux/of.h>
27#include <linux/of_device.h>
Jaswinder Singh Rajputcd36e9e2009-04-04 16:38:42 -070028#include <linux/firmware.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
30#include <net/dst.h>
31#include <net/arp.h>
32#include <net/sock.h>
33#include <net/ipv6.h>
34
35#include <asm/system.h>
36#include <asm/io.h>
37#include <asm/dma.h>
38#include <asm/byteorder.h>
39#include <asm/idprom.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040#include <asm/openprom.h>
41#include <asm/oplib.h>
42#include <asm/auxio.h>
43#include <asm/pgtable.h>
44#include <asm/irq.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045
46#include "myri_sbus.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48/* #define DEBUG_DETECT */
49/* #define DEBUG_IRQ */
50/* #define DEBUG_TRANSMIT */
51/* #define DEBUG_RECEIVE */
52/* #define DEBUG_HEADER */
53
54#ifdef DEBUG_DETECT
55#define DET(x) printk x
56#else
57#define DET(x)
58#endif
59
60#ifdef DEBUG_IRQ
61#define DIRQ(x) printk x
62#else
63#define DIRQ(x)
64#endif
65
66#ifdef DEBUG_TRANSMIT
67#define DTX(x) printk x
68#else
69#define DTX(x)
70#endif
71
72#ifdef DEBUG_RECEIVE
73#define DRX(x) printk x
74#else
75#define DRX(x)
76#endif
77
78#ifdef DEBUG_HEADER
79#define DHDR(x) printk x
80#else
81#define DHDR(x)
82#endif
83
Jaswinder Singh Rajputcd36e9e2009-04-04 16:38:42 -070084/* Firmware name */
85#define FWNAME "myricom/lanai.bin"
86
Linus Torvalds1da177e2005-04-16 15:20:36 -070087static void myri_reset_off(void __iomem *lp, void __iomem *cregs)
88{
89 /* Clear IRQ mask. */
90 sbus_writel(0, lp + LANAI_EIMASK);
91
92 /* Turn RESET function off. */
93 sbus_writel(CONTROL_ROFF, cregs + MYRICTRL_CTRL);
94}
95
96static void myri_reset_on(void __iomem *cregs)
97{
98 /* Enable RESET function. */
99 sbus_writel(CONTROL_RON, cregs + MYRICTRL_CTRL);
100
101 /* Disable IRQ's. */
102 sbus_writel(CONTROL_DIRQ, cregs + MYRICTRL_CTRL);
103}
104
105static void myri_disable_irq(void __iomem *lp, void __iomem *cregs)
106{
107 sbus_writel(CONTROL_DIRQ, cregs + MYRICTRL_CTRL);
108 sbus_writel(0, lp + LANAI_EIMASK);
109 sbus_writel(ISTAT_HOST, lp + LANAI_ISTAT);
110}
111
112static void myri_enable_irq(void __iomem *lp, void __iomem *cregs)
113{
114 sbus_writel(CONTROL_EIRQ, cregs + MYRICTRL_CTRL);
115 sbus_writel(ISTAT_HOST, lp + LANAI_EIMASK);
116}
117
118static inline void bang_the_chip(struct myri_eth *mp)
119{
120 struct myri_shmem __iomem *shmem = mp->shmem;
121 void __iomem *cregs = mp->cregs;
122
123 sbus_writel(1, &shmem->send);
124 sbus_writel(CONTROL_WON, cregs + MYRICTRL_CTRL);
125}
126
127static int myri_do_handshake(struct myri_eth *mp)
128{
129 struct myri_shmem __iomem *shmem = mp->shmem;
130 void __iomem *cregs = mp->cregs;
131 struct myri_channel __iomem *chan = &shmem->channel;
132 int tick = 0;
133
134 DET(("myri_do_handshake: "));
135 if (sbus_readl(&chan->state) == STATE_READY) {
136 DET(("Already STATE_READY, failed.\n"));
137 return -1; /* We're hosed... */
138 }
139
140 myri_disable_irq(mp->lregs, cregs);
141
Roel Kluin9db77202007-11-13 03:16:17 -0800142 while (tick++ < 25) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 u32 softstate;
144
145 /* Wake it up. */
146 DET(("shakedown, CONTROL_WON, "));
147 sbus_writel(1, &shmem->shakedown);
148 sbus_writel(CONTROL_WON, cregs + MYRICTRL_CTRL);
149
150 softstate = sbus_readl(&chan->state);
151 DET(("chanstate[%08x] ", softstate));
152 if (softstate == STATE_READY) {
153 DET(("wakeup successful, "));
154 break;
155 }
156
157 if (softstate != STATE_WFN) {
158 DET(("not WFN setting that, "));
159 sbus_writel(STATE_WFN, &chan->state);
160 }
161
162 udelay(20);
163 }
164
165 myri_enable_irq(mp->lregs, cregs);
166
167 if (tick > 25) {
168 DET(("25 ticks we lose, failure.\n"));
169 return -1;
170 }
171 DET(("success\n"));
172 return 0;
173}
174
Ben Collinsb48194b2006-10-17 19:11:31 -0700175static int __devinit myri_load_lanai(struct myri_eth *mp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176{
Jaswinder Singh Rajputcd36e9e2009-04-04 16:38:42 -0700177 const struct firmware *fw;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 struct net_device *dev = mp->dev;
179 struct myri_shmem __iomem *shmem = mp->shmem;
180 void __iomem *rptr;
Jaswinder Singh Rajputcd36e9e2009-04-04 16:38:42 -0700181 int i, lanai4_data_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182
183 myri_disable_irq(mp->lregs, mp->cregs);
184 myri_reset_on(mp->cregs);
185
186 rptr = mp->lanai;
187 for (i = 0; i < mp->eeprom.ramsz; i++)
188 sbus_writeb(0, rptr + i);
189
190 if (mp->eeprom.cpuvers >= CPUVERS_3_0)
191 sbus_writel(mp->eeprom.cval, mp->lregs + LANAI_CVAL);
192
Jaswinder Singh Rajputcd36e9e2009-04-04 16:38:42 -0700193 i = request_firmware(&fw, FWNAME, &mp->myri_op->dev);
194 if (i) {
195 printk(KERN_ERR "Failed to load image \"%s\" err %d\n",
196 FWNAME, i);
197 return i;
198 }
199 if (fw->size < 2) {
200 printk(KERN_ERR "Bogus length %zu in image \"%s\"\n",
201 fw->size, FWNAME);
202 release_firmware(fw);
203 return -EINVAL;
204 }
205 lanai4_data_size = fw->data[0] << 8 | fw->data[1];
206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 /* Load executable code. */
Jaswinder Singh Rajputcd36e9e2009-04-04 16:38:42 -0700208 for (i = 2; i < fw->size; i++)
209 sbus_writeb(fw->data[i], rptr++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 /* Load data segment. */
Jaswinder Singh Rajputcd36e9e2009-04-04 16:38:42 -0700212 for (i = 0; i < lanai4_data_size; i++)
213 sbus_writeb(0, rptr++);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
215 /* Set device address. */
216 sbus_writeb(0, &shmem->addr[0]);
217 sbus_writeb(0, &shmem->addr[1]);
218 for (i = 0; i < 6; i++)
219 sbus_writeb(dev->dev_addr[i],
220 &shmem->addr[i + 2]);
221
222 /* Set SBUS bursts and interrupt mask. */
223 sbus_writel(((mp->myri_bursts & 0xf8) >> 3), &shmem->burst);
224 sbus_writel(SHMEM_IMASK_RX, &shmem->imask);
225
226 /* Release the LANAI. */
227 myri_disable_irq(mp->lregs, mp->cregs);
228 myri_reset_off(mp->lregs, mp->cregs);
229 myri_disable_irq(mp->lregs, mp->cregs);
230
231 /* Wait for the reset to complete. */
232 for (i = 0; i < 5000; i++) {
233 if (sbus_readl(&shmem->channel.state) != STATE_READY)
234 break;
235 else
236 udelay(10);
237 }
238
239 if (i == 5000)
240 printk(KERN_ERR "myricom: Chip would not reset after firmware load.\n");
241
242 i = myri_do_handshake(mp);
243 if (i)
244 printk(KERN_ERR "myricom: Handshake with LANAI failed.\n");
245
246 if (mp->eeprom.cpuvers == CPUVERS_4_0)
247 sbus_writel(0, mp->lregs + LANAI_VERS);
248
Jaswinder Singh Rajputcd36e9e2009-04-04 16:38:42 -0700249 release_firmware(fw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 return i;
251}
252
253static void myri_clean_rings(struct myri_eth *mp)
254{
255 struct sendq __iomem *sq = mp->sq;
256 struct recvq __iomem *rq = mp->rq;
257 int i;
258
259 sbus_writel(0, &rq->tail);
260 sbus_writel(0, &rq->head);
261 for (i = 0; i < (RX_RING_SIZE+1); i++) {
262 if (mp->rx_skbs[i] != NULL) {
263 struct myri_rxd __iomem *rxd = &rq->myri_rxd[i];
264 u32 dma_addr;
265
266 dma_addr = sbus_readl(&rxd->myri_scatters[0].addr);
David S. Miller9e6b6e72008-08-26 23:36:58 -0700267 dma_unmap_single(&mp->myri_op->dev, dma_addr,
David S. Miller738f2b72008-08-27 18:09:11 -0700268 RX_ALLOC_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 dev_kfree_skb(mp->rx_skbs[i]);
270 mp->rx_skbs[i] = NULL;
271 }
272 }
273
274 mp->tx_old = 0;
275 sbus_writel(0, &sq->tail);
276 sbus_writel(0, &sq->head);
277 for (i = 0; i < TX_RING_SIZE; i++) {
278 if (mp->tx_skbs[i] != NULL) {
279 struct sk_buff *skb = mp->tx_skbs[i];
280 struct myri_txd __iomem *txd = &sq->myri_txd[i];
281 u32 dma_addr;
282
283 dma_addr = sbus_readl(&txd->myri_gathers[0].addr);
David S. Miller9e6b6e72008-08-26 23:36:58 -0700284 dma_unmap_single(&mp->myri_op->dev, dma_addr,
David S. Miller738f2b72008-08-27 18:09:11 -0700285 (skb->len + 3) & ~3,
286 DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 dev_kfree_skb(mp->tx_skbs[i]);
288 mp->tx_skbs[i] = NULL;
289 }
290 }
291}
292
293static void myri_init_rings(struct myri_eth *mp, int from_irq)
294{
295 struct recvq __iomem *rq = mp->rq;
296 struct myri_rxd __iomem *rxd = &rq->myri_rxd[0];
297 struct net_device *dev = mp->dev;
Al Viro9e249742005-10-21 03:22:29 -0400298 gfp_t gfp_flags = GFP_KERNEL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 int i;
300
301 if (from_irq || in_interrupt())
302 gfp_flags = GFP_ATOMIC;
303
304 myri_clean_rings(mp);
305 for (i = 0; i < RX_RING_SIZE; i++) {
306 struct sk_buff *skb = myri_alloc_skb(RX_ALLOC_SIZE, gfp_flags);
307 u32 dma_addr;
308
309 if (!skb)
310 continue;
311 mp->rx_skbs[i] = skb;
312 skb->dev = dev;
313 skb_put(skb, RX_ALLOC_SIZE);
314
David S. Miller9e6b6e72008-08-26 23:36:58 -0700315 dma_addr = dma_map_single(&mp->myri_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -0700316 skb->data, RX_ALLOC_SIZE,
317 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 sbus_writel(dma_addr, &rxd[i].myri_scatters[0].addr);
319 sbus_writel(RX_ALLOC_SIZE, &rxd[i].myri_scatters[0].len);
320 sbus_writel(i, &rxd[i].ctx);
321 sbus_writel(1, &rxd[i].num_sg);
322 }
323 sbus_writel(0, &rq->head);
324 sbus_writel(RX_RING_SIZE, &rq->tail);
325}
326
327static int myri_init(struct myri_eth *mp, int from_irq)
328{
329 myri_init_rings(mp, from_irq);
330 return 0;
331}
332
333static void myri_is_not_so_happy(struct myri_eth *mp)
334{
335}
336
337#ifdef DEBUG_HEADER
338static void dump_ehdr(struct ethhdr *ehdr)
339{
Johannes Berge1749612008-10-27 15:59:26 -0700340 printk("ehdr[h_dst(%pM)"
341 "h_source(%pM)"
Joe Perches0795af52007-10-03 17:59:30 -0700342 "h_proto(%04x)]\n",
Johannes Berge1749612008-10-27 15:59:26 -0700343 ehdr->h_dest, ehdr->h_source, ehdr->h_proto);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
346static void dump_ehdr_and_myripad(unsigned char *stuff)
347{
348 struct ethhdr *ehdr = (struct ethhdr *) (stuff + 2);
349
350 printk("pad[%02x:%02x]", stuff[0], stuff[1]);
Joe Perches0795af52007-10-03 17:59:30 -0700351 dump_ehdr(ehdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352}
353#endif
354
355static void myri_tx(struct myri_eth *mp, struct net_device *dev)
356{
357 struct sendq __iomem *sq= mp->sq;
358 int entry = mp->tx_old;
359 int limit = sbus_readl(&sq->head);
360
361 DTX(("entry[%d] limit[%d] ", entry, limit));
362 if (entry == limit)
363 return;
364 while (entry != limit) {
365 struct sk_buff *skb = mp->tx_skbs[entry];
366 u32 dma_addr;
367
368 DTX(("SKB[%d] ", entry));
369 dma_addr = sbus_readl(&sq->myri_txd[entry].myri_gathers[0].addr);
David S. Miller9e6b6e72008-08-26 23:36:58 -0700370 dma_unmap_single(&mp->myri_op->dev, dma_addr,
David S. Miller738f2b72008-08-27 18:09:11 -0700371 skb->len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 dev_kfree_skb(skb);
373 mp->tx_skbs[entry] = NULL;
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700374 dev->stats.tx_packets++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 entry = NEXT_TX(entry);
376 }
377 mp->tx_old = entry;
378}
379
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400380/* Determine the packet's protocol ID. The rule here is that we
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 * assume 802.3 if the type field is short enough to be a length.
382 * This is normal practice and works for any 'now in use' protocol.
383 */
Alexey Dobriyanab611482005-07-12 12:08:43 -0700384static __be16 myri_type_trans(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385{
386 struct ethhdr *eth;
387 unsigned char *rawp;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400388
Arnaldo Carvalho de Melo48d49d0c2007-03-10 12:30:58 -0300389 skb_set_mac_header(skb, MYRI_PAD_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 skb_pull(skb, dev->hard_header_len);
391 eth = eth_hdr(skb);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400392
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393#ifdef DEBUG_HEADER
394 DHDR(("myri_type_trans: "));
395 dump_ehdr(eth);
396#endif
397 if (*eth->h_dest & 1) {
398 if (memcmp(eth->h_dest, dev->broadcast, ETH_ALEN)==0)
399 skb->pkt_type = PACKET_BROADCAST;
400 else
401 skb->pkt_type = PACKET_MULTICAST;
402 } else if (dev->flags & (IFF_PROMISC|IFF_ALLMULTI)) {
403 if (memcmp(eth->h_dest, dev->dev_addr, ETH_ALEN))
404 skb->pkt_type = PACKET_OTHERHOST;
405 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400406
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 if (ntohs(eth->h_proto) >= 1536)
408 return eth->h_proto;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400409
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410 rawp = skb->data;
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400411
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412 /* This is a magic hack to spot IPX packets. Older Novell breaks
413 * the protocol design and runs IPX over 802.3 without an 802.2 LLC
414 * layer. We look for FFFF which isn't a used 802.2 SSAP/DSAP. This
415 * won't work for fault tolerant netware but does for the rest.
416 */
417 if (*(unsigned short *)rawp == 0xFFFF)
418 return htons(ETH_P_802_3);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 /* Real 802.2 LLC */
421 return htons(ETH_P_802_2);
422}
423
424static void myri_rx(struct myri_eth *mp, struct net_device *dev)
425{
426 struct recvq __iomem *rq = mp->rq;
427 struct recvq __iomem *rqa = mp->rqack;
428 int entry = sbus_readl(&rqa->head);
429 int limit = sbus_readl(&rqa->tail);
430 int drops;
431
432 DRX(("entry[%d] limit[%d] ", entry, limit));
433 if (entry == limit)
434 return;
435 drops = 0;
436 DRX(("\n"));
437 while (entry != limit) {
438 struct myri_rxd __iomem *rxdack = &rqa->myri_rxd[entry];
439 u32 csum = sbus_readl(&rxdack->csum);
440 int len = sbus_readl(&rxdack->myri_scatters[0].len);
441 int index = sbus_readl(&rxdack->ctx);
442 struct myri_rxd __iomem *rxd = &rq->myri_rxd[sbus_readl(&rq->tail)];
443 struct sk_buff *skb = mp->rx_skbs[index];
444
445 /* Ack it. */
446 sbus_writel(NEXT_RX(entry), &rqa->head);
447
448 /* Check for errors. */
449 DRX(("rxd[%d]: %p len[%d] csum[%08x] ", entry, rxd, len, csum));
David S. Miller9e6b6e72008-08-26 23:36:58 -0700450 dma_sync_single_for_cpu(&mp->myri_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -0700451 sbus_readl(&rxd->myri_scatters[0].addr),
452 RX_ALLOC_SIZE, DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700453 if (len < (ETH_HLEN + MYRI_PAD_LEN) || (skb->data[0] != MYRI_PAD_LEN)) {
454 DRX(("ERROR["));
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700455 dev->stats.rx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 if (len < (ETH_HLEN + MYRI_PAD_LEN)) {
457 DRX(("BAD_LENGTH] "));
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700458 dev->stats.rx_length_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 } else {
460 DRX(("NO_PADDING] "));
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700461 dev->stats.rx_frame_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463
464 /* Return it to the LANAI. */
465 drop_it:
466 drops++;
467 DRX(("DROP "));
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700468 dev->stats.rx_dropped++;
David S. Miller9e6b6e72008-08-26 23:36:58 -0700469 dma_sync_single_for_device(&mp->myri_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -0700470 sbus_readl(&rxd->myri_scatters[0].addr),
471 RX_ALLOC_SIZE,
472 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 sbus_writel(RX_ALLOC_SIZE, &rxd->myri_scatters[0].len);
474 sbus_writel(index, &rxd->ctx);
475 sbus_writel(1, &rxd->num_sg);
476 sbus_writel(NEXT_RX(sbus_readl(&rq->tail)), &rq->tail);
477 goto next;
478 }
479
480 DRX(("len[%d] ", len));
481 if (len > RX_COPY_THRESHOLD) {
482 struct sk_buff *new_skb;
483 u32 dma_addr;
484
485 DRX(("BIGBUFF "));
486 new_skb = myri_alloc_skb(RX_ALLOC_SIZE, GFP_ATOMIC);
487 if (new_skb == NULL) {
488 DRX(("skb_alloc(FAILED) "));
489 goto drop_it;
490 }
David S. Miller9e6b6e72008-08-26 23:36:58 -0700491 dma_unmap_single(&mp->myri_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -0700492 sbus_readl(&rxd->myri_scatters[0].addr),
493 RX_ALLOC_SIZE,
494 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 mp->rx_skbs[index] = new_skb;
496 new_skb->dev = dev;
497 skb_put(new_skb, RX_ALLOC_SIZE);
David S. Miller9e6b6e72008-08-26 23:36:58 -0700498 dma_addr = dma_map_single(&mp->myri_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -0700499 new_skb->data,
500 RX_ALLOC_SIZE,
501 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 sbus_writel(dma_addr, &rxd->myri_scatters[0].addr);
503 sbus_writel(RX_ALLOC_SIZE, &rxd->myri_scatters[0].len);
504 sbus_writel(index, &rxd->ctx);
505 sbus_writel(1, &rxd->num_sg);
506 sbus_writel(NEXT_RX(sbus_readl(&rq->tail)), &rq->tail);
507
508 /* Trim the original skb for the netif. */
509 DRX(("trim(%d) ", len));
510 skb_trim(skb, len);
511 } else {
512 struct sk_buff *copy_skb = dev_alloc_skb(len);
513
514 DRX(("SMALLBUFF "));
515 if (copy_skb == NULL) {
516 DRX(("dev_alloc_skb(FAILED) "));
517 goto drop_it;
518 }
519 /* DMA sync already done above. */
520 copy_skb->dev = dev;
521 DRX(("resv_and_put "));
522 skb_put(copy_skb, len);
Arnaldo Carvalho de Melod626f622007-03-27 18:55:52 -0300523 skb_copy_from_linear_data(skb, copy_skb->data, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 /* Reuse original ring buffer. */
526 DRX(("reuse "));
David S. Miller9e6b6e72008-08-26 23:36:58 -0700527 dma_sync_single_for_device(&mp->myri_op->dev,
David S. Miller738f2b72008-08-27 18:09:11 -0700528 sbus_readl(&rxd->myri_scatters[0].addr),
529 RX_ALLOC_SIZE,
530 DMA_FROM_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 sbus_writel(RX_ALLOC_SIZE, &rxd->myri_scatters[0].len);
532 sbus_writel(index, &rxd->ctx);
533 sbus_writel(1, &rxd->num_sg);
534 sbus_writel(NEXT_RX(sbus_readl(&rq->tail)), &rq->tail);
535
536 skb = copy_skb;
537 }
538
539 /* Just like the happy meal we get checksums from this card. */
540 skb->csum = csum;
541 skb->ip_summed = CHECKSUM_UNNECESSARY; /* XXX */
542
543 skb->protocol = myri_type_trans(skb, dev);
544 DRX(("prot[%04x] netif_rx ", skb->protocol));
545 netif_rx(skb);
546
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700547 dev->stats.rx_packets++;
548 dev->stats.rx_bytes += len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 next:
550 DRX(("NEXT\n"));
551 entry = NEXT_RX(entry);
552 }
553}
554
David Howells7d12e782006-10-05 14:55:46 +0100555static irqreturn_t myri_interrupt(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
557 struct net_device *dev = (struct net_device *) dev_id;
Wang Chen4cf16532008-11-12 23:38:14 -0800558 struct myri_eth *mp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 void __iomem *lregs = mp->lregs;
560 struct myri_channel __iomem *chan = &mp->shmem->channel;
561 unsigned long flags;
562 u32 status;
563 int handled = 0;
564
565 spin_lock_irqsave(&mp->irq_lock, flags);
566
567 status = sbus_readl(lregs + LANAI_ISTAT);
568 DIRQ(("myri_interrupt: status[%08x] ", status));
569 if (status & ISTAT_HOST) {
570 u32 softstate;
571
572 handled = 1;
573 DIRQ(("IRQ_DISAB "));
574 myri_disable_irq(lregs, mp->cregs);
575 softstate = sbus_readl(&chan->state);
576 DIRQ(("state[%08x] ", softstate));
577 if (softstate != STATE_READY) {
578 DIRQ(("myri_not_so_happy "));
579 myri_is_not_so_happy(mp);
580 }
581 DIRQ(("\nmyri_rx: "));
582 myri_rx(mp, dev);
583 DIRQ(("\nistat=ISTAT_HOST "));
584 sbus_writel(ISTAT_HOST, lregs + LANAI_ISTAT);
585 DIRQ(("IRQ_ENAB "));
586 myri_enable_irq(lregs, mp->cregs);
587 }
588 DIRQ(("\n"));
589
590 spin_unlock_irqrestore(&mp->irq_lock, flags);
591
592 return IRQ_RETVAL(handled);
593}
594
595static int myri_open(struct net_device *dev)
596{
Wang Chen4cf16532008-11-12 23:38:14 -0800597 struct myri_eth *mp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 return myri_init(mp, in_interrupt());
600}
601
602static int myri_close(struct net_device *dev)
603{
Wang Chen4cf16532008-11-12 23:38:14 -0800604 struct myri_eth *mp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
606 myri_clean_rings(mp);
607 return 0;
608}
609
610static void myri_tx_timeout(struct net_device *dev)
611{
Wang Chen4cf16532008-11-12 23:38:14 -0800612 struct myri_eth *mp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614 printk(KERN_ERR "%s: transmit timed out, resetting\n", dev->name);
615
Jeff Garzik09f75cd2007-10-03 17:41:50 -0700616 dev->stats.tx_errors++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617 myri_init(mp, 0);
618 netif_wake_queue(dev);
619}
620
621static int myri_start_xmit(struct sk_buff *skb, struct net_device *dev)
622{
Wang Chen4cf16532008-11-12 23:38:14 -0800623 struct myri_eth *mp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 struct sendq __iomem *sq = mp->sq;
625 struct myri_txd __iomem *txd;
626 unsigned long flags;
627 unsigned int head, tail;
628 int len, entry;
629 u32 dma_addr;
630
631 DTX(("myri_start_xmit: "));
632
633 myri_tx(mp, dev);
634
635 netif_stop_queue(dev);
636
637 /* This is just to prevent multiple PIO reads for TX_BUFFS_AVAIL. */
638 head = sbus_readl(&sq->head);
639 tail = sbus_readl(&sq->tail);
640
641 if (!TX_BUFFS_AVAIL(head, tail)) {
642 DTX(("no buffs available, returning 1\n"));
Patrick McHardy5b548142009-06-12 06:22:29 +0000643 return NETDEV_TX_BUSY;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 }
645
646 spin_lock_irqsave(&mp->irq_lock, flags);
647
648 DHDR(("xmit[skbdata(%p)]\n", skb->data));
649#ifdef DEBUG_HEADER
650 dump_ehdr_and_myripad(((unsigned char *) skb->data));
651#endif
652
653 /* XXX Maybe this can go as well. */
654 len = skb->len;
655 if (len & 3) {
656 DTX(("len&3 "));
657 len = (len + 4) & (~3);
658 }
659
660 entry = sbus_readl(&sq->tail);
661
662 txd = &sq->myri_txd[entry];
663 mp->tx_skbs[entry] = skb;
664
665 /* Must do this before we sbus map it. */
666 if (skb->data[MYRI_PAD_LEN] & 0x1) {
667 sbus_writew(0xffff, &txd->addr[0]);
668 sbus_writew(0xffff, &txd->addr[1]);
669 sbus_writew(0xffff, &txd->addr[2]);
670 sbus_writew(0xffff, &txd->addr[3]);
671 } else {
672 sbus_writew(0xffff, &txd->addr[0]);
673 sbus_writew((skb->data[0] << 8) | skb->data[1], &txd->addr[1]);
674 sbus_writew((skb->data[2] << 8) | skb->data[3], &txd->addr[2]);
675 sbus_writew((skb->data[4] << 8) | skb->data[5], &txd->addr[3]);
676 }
677
David S. Miller9e6b6e72008-08-26 23:36:58 -0700678 dma_addr = dma_map_single(&mp->myri_op->dev, skb->data,
David S. Miller738f2b72008-08-27 18:09:11 -0700679 len, DMA_TO_DEVICE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 sbus_writel(dma_addr, &txd->myri_gathers[0].addr);
681 sbus_writel(len, &txd->myri_gathers[0].len);
682 sbus_writel(1, &txd->num_sg);
683 sbus_writel(KERNEL_CHANNEL, &txd->chan);
684 sbus_writel(len, &txd->len);
685 sbus_writel((u32)-1, &txd->csum_off);
686 sbus_writel(0, &txd->csum_field);
687
688 sbus_writel(NEXT_TX(entry), &sq->tail);
689 DTX(("BangTheChip "));
690 bang_the_chip(mp);
691
692 DTX(("tbusy=0, returning 0\n"));
693 netif_start_queue(dev);
694 spin_unlock_irqrestore(&mp->irq_lock, flags);
Patrick McHardy6ed10652009-06-23 06:03:08 +0000695 return NETDEV_TX_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696}
697
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400698/* Create the MyriNet MAC header for an arbitrary protocol layer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 *
700 * saddr=NULL means use device source address
701 * daddr=NULL means leave destination address (eg unresolved arp)
702 */
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700703static int myri_header(struct sk_buff *skb, struct net_device *dev,
704 unsigned short type, const void *daddr,
705 const void *saddr, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706{
707 struct ethhdr *eth = (struct ethhdr *) skb_push(skb, ETH_HLEN);
708 unsigned char *pad = (unsigned char *) skb_push(skb, MYRI_PAD_LEN);
709
710#ifdef DEBUG_HEADER
711 DHDR(("myri_header: pad[%02x,%02x] ", pad[0], pad[1]));
712 dump_ehdr(eth);
713#endif
714
715 /* Set the MyriNET padding identifier. */
716 pad[0] = MYRI_PAD_LEN;
717 pad[1] = 0xab;
718
Octavian Purdilabf9ae532009-12-26 11:50:59 +0000719 /* Set the protocol type. For a packet of type ETH_P_802_3/2 we put the
720 * length in here instead.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 */
Octavian Purdilabf9ae532009-12-26 11:50:59 +0000722 if (type != ETH_P_802_3 && type != ETH_P_802_2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 eth->h_proto = htons(type);
724 else
725 eth->h_proto = htons(len);
726
727 /* Set the source hardware address. */
728 if (saddr)
729 memcpy(eth->h_source, saddr, dev->addr_len);
730 else
731 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
732
733 /* Anyway, the loopback-device should never use this function... */
734 if (dev->flags & IFF_LOOPBACK) {
735 int i;
736 for (i = 0; i < dev->addr_len; i++)
737 eth->h_dest[i] = 0;
738 return(dev->hard_header_len);
739 }
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400740
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (daddr) {
742 memcpy(eth->h_dest, daddr, dev->addr_len);
743 return dev->hard_header_len;
744 }
745 return -dev->hard_header_len;
746}
747
748/* Rebuild the MyriNet MAC header. This is called after an ARP
749 * (or in future other address resolution) has completed on this
750 * sk_buff. We now let ARP fill in the other fields.
751 */
752static int myri_rebuild_header(struct sk_buff *skb)
753{
754 unsigned char *pad = (unsigned char *) skb->data;
755 struct ethhdr *eth = (struct ethhdr *) (pad + MYRI_PAD_LEN);
756 struct net_device *dev = skb->dev;
757
758#ifdef DEBUG_HEADER
759 DHDR(("myri_rebuild_header: pad[%02x,%02x] ", pad[0], pad[1]));
760 dump_ehdr(eth);
761#endif
762
763 /* Refill MyriNet padding identifiers, this is just being anal. */
764 pad[0] = MYRI_PAD_LEN;
765 pad[1] = 0xab;
766
767 switch (eth->h_proto)
768 {
769#ifdef CONFIG_INET
Harvey Harrison09640e62009-02-01 00:45:17 -0800770 case cpu_to_be16(ETH_P_IP):
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 return arp_find(eth->h_dest, skb);
772#endif
773
774 default:
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400775 printk(KERN_DEBUG
776 "%s: unable to resolve type %X addresses.\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 dev->name, (int)eth->h_proto);
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400778
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
780 return 0;
781 break;
782 }
783
Jeff Garzik6aa20a22006-09-13 13:24:59 -0400784 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785}
786
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700787static int myri_header_cache(const struct neighbour *neigh, struct hh_cache *hh)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788{
789 unsigned short type = hh->hh_type;
790 unsigned char *pad;
791 struct ethhdr *eth;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700792 const struct net_device *dev = neigh->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793
794 pad = ((unsigned char *) hh->hh_data) +
795 HH_DATA_OFF(sizeof(*eth) + MYRI_PAD_LEN);
796 eth = (struct ethhdr *) (pad + MYRI_PAD_LEN);
797
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700798 if (type == htons(ETH_P_802_3))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799 return -1;
800
801 /* Refill MyriNet padding identifiers, this is just being anal. */
802 pad[0] = MYRI_PAD_LEN;
803 pad[1] = 0xab;
804
805 eth->h_proto = type;
806 memcpy(eth->h_source, dev->dev_addr, dev->addr_len);
807 memcpy(eth->h_dest, neigh->ha, dev->addr_len);
808 hh->hh_len = 16;
809 return 0;
810}
811
812
813/* Called by Address Resolution module to notify changes in address. */
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700814void myri_header_cache_update(struct hh_cache *hh,
815 const struct net_device *dev,
816 const unsigned char * haddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817{
818 memcpy(((u8*)hh->hh_data) + HH_DATA_OFF(sizeof(struct ethhdr)),
819 haddr, dev->addr_len);
820}
821
822static int myri_change_mtu(struct net_device *dev, int new_mtu)
823{
824 if ((new_mtu < (ETH_HLEN + MYRI_PAD_LEN)) || (new_mtu > MYRINET_MTU))
825 return -EINVAL;
826 dev->mtu = new_mtu;
827 return 0;
828}
829
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830static void myri_set_multicast(struct net_device *dev)
831{
832 /* Do nothing, all MyriCOM nodes transmit multicast frames
833 * as broadcast packets...
834 */
835}
836
837static inline void set_boardid_from_idprom(struct myri_eth *mp, int num)
838{
839 mp->eeprom.id[0] = 0;
840 mp->eeprom.id[1] = idprom->id_machtype;
841 mp->eeprom.id[2] = (idprom->id_sernum >> 16) & 0xff;
842 mp->eeprom.id[3] = (idprom->id_sernum >> 8) & 0xff;
843 mp->eeprom.id[4] = (idprom->id_sernum >> 0) & 0xff;
844 mp->eeprom.id[5] = num;
845}
846
847static inline void determine_reg_space_size(struct myri_eth *mp)
848{
849 switch(mp->eeprom.cpuvers) {
850 case CPUVERS_2_3:
851 case CPUVERS_3_0:
852 case CPUVERS_3_1:
853 case CPUVERS_3_2:
854 mp->reg_size = (3 * 128 * 1024) + 4096;
855 break;
856
857 case CPUVERS_4_0:
858 case CPUVERS_4_1:
859 mp->reg_size = ((4096<<1) + mp->eeprom.ramsz);
860 break;
861
862 case CPUVERS_4_2:
863 case CPUVERS_5_0:
864 default:
865 printk("myricom: AIEEE weird cpu version %04x assuming pre4.0\n",
866 mp->eeprom.cpuvers);
867 mp->reg_size = (3 * 128 * 1024) + 4096;
868 };
869}
870
871#ifdef DEBUG_DETECT
872static void dump_eeprom(struct myri_eth *mp)
873{
874 printk("EEPROM: clockval[%08x] cpuvers[%04x] "
875 "id[%02x,%02x,%02x,%02x,%02x,%02x]\n",
876 mp->eeprom.cval, mp->eeprom.cpuvers,
877 mp->eeprom.id[0], mp->eeprom.id[1], mp->eeprom.id[2],
878 mp->eeprom.id[3], mp->eeprom.id[4], mp->eeprom.id[5]);
879 printk("EEPROM: ramsz[%08x]\n", mp->eeprom.ramsz);
880 printk("EEPROM: fvers[%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
881 mp->eeprom.fvers[0], mp->eeprom.fvers[1], mp->eeprom.fvers[2],
882 mp->eeprom.fvers[3], mp->eeprom.fvers[4], mp->eeprom.fvers[5],
883 mp->eeprom.fvers[6], mp->eeprom.fvers[7]);
884 printk("EEPROM: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
885 mp->eeprom.fvers[8], mp->eeprom.fvers[9], mp->eeprom.fvers[10],
886 mp->eeprom.fvers[11], mp->eeprom.fvers[12], mp->eeprom.fvers[13],
887 mp->eeprom.fvers[14], mp->eeprom.fvers[15]);
888 printk("EEPROM: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
889 mp->eeprom.fvers[16], mp->eeprom.fvers[17], mp->eeprom.fvers[18],
890 mp->eeprom.fvers[19], mp->eeprom.fvers[20], mp->eeprom.fvers[21],
891 mp->eeprom.fvers[22], mp->eeprom.fvers[23]);
892 printk("EEPROM: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x]\n",
893 mp->eeprom.fvers[24], mp->eeprom.fvers[25], mp->eeprom.fvers[26],
894 mp->eeprom.fvers[27], mp->eeprom.fvers[28], mp->eeprom.fvers[29],
895 mp->eeprom.fvers[30], mp->eeprom.fvers[31]);
896 printk("EEPROM: mvers[%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\n",
897 mp->eeprom.mvers[0], mp->eeprom.mvers[1], mp->eeprom.mvers[2],
898 mp->eeprom.mvers[3], mp->eeprom.mvers[4], mp->eeprom.mvers[5],
899 mp->eeprom.mvers[6], mp->eeprom.mvers[7]);
900 printk("EEPROM: %02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x]\n",
901 mp->eeprom.mvers[8], mp->eeprom.mvers[9], mp->eeprom.mvers[10],
902 mp->eeprom.mvers[11], mp->eeprom.mvers[12], mp->eeprom.mvers[13],
903 mp->eeprom.mvers[14], mp->eeprom.mvers[15]);
904 printk("EEPROM: dlval[%04x] brd_type[%04x] bus_type[%04x] prod_code[%04x]\n",
905 mp->eeprom.dlval, mp->eeprom.brd_type, mp->eeprom.bus_type,
906 mp->eeprom.prod_code);
907 printk("EEPROM: serial_num[%08x]\n", mp->eeprom.serial_num);
908}
909#endif
910
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700911static const struct header_ops myri_header_ops = {
912 .create = myri_header,
913 .rebuild = myri_rebuild_header,
914 .cache = myri_header_cache,
915 .cache_update = myri_header_cache_update,
916};
917
David S. Milleradaa0db2009-03-23 13:33:11 -0700918static const struct net_device_ops myri_ops = {
919 .ndo_open = myri_open,
920 .ndo_stop = myri_close,
921 .ndo_start_xmit = myri_start_xmit,
922 .ndo_set_multicast_list = myri_set_multicast,
923 .ndo_tx_timeout = myri_tx_timeout,
924 .ndo_change_mtu = myri_change_mtu,
David S. Millerdac46962009-03-23 14:29:24 -0700925 .ndo_set_mac_address = eth_mac_addr,
926 .ndo_validate_addr = eth_validate_addr,
David S. Milleradaa0db2009-03-23 13:33:11 -0700927};
928
David S. Miller9e6b6e72008-08-26 23:36:58 -0700929static int __devinit myri_sbus_probe(struct of_device *op, const struct of_device_id *match)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930{
David S. Miller9e6b6e72008-08-26 23:36:58 -0700931 struct device_node *dp = op->node;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 static unsigned version_printed;
933 struct net_device *dev;
David S. Miller9e6b6e72008-08-26 23:36:58 -0700934 struct myri_eth *mp;
935 const void *prop;
936 static int num;
937 int i, len;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938
David S. Miller9e6b6e72008-08-26 23:36:58 -0700939 DET(("myri_ether_init(%p,%d):\n", op, num));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 dev = alloc_etherdev(sizeof(struct myri_eth));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 if (!dev)
942 return -ENOMEM;
943
944 if (version_printed++ == 0)
945 printk(version);
946
David S. Miller9e6b6e72008-08-26 23:36:58 -0700947 SET_NETDEV_DEV(dev, &op->dev);
David S. Millera46c30f2006-06-23 21:32:48 -0700948
David S. Miller9e6b6e72008-08-26 23:36:58 -0700949 mp = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 spin_lock_init(&mp->irq_lock);
David S. Miller9e6b6e72008-08-26 23:36:58 -0700951 mp->myri_op = op;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700952
953 /* Clean out skb arrays. */
954 for (i = 0; i < (RX_RING_SIZE + 1); i++)
955 mp->rx_skbs[i] = NULL;
956
957 for (i = 0; i < TX_RING_SIZE; i++)
958 mp->tx_skbs[i] = NULL;
959
960 /* First check for EEPROM information. */
David S. Miller9e6b6e72008-08-26 23:36:58 -0700961 prop = of_get_property(dp, "myrinet-eeprom-info", &len);
962
963 if (prop)
964 memcpy(&mp->eeprom, prop, sizeof(struct myri_eeprom));
965 if (!prop) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 /* No eeprom property, must cook up the values ourselves. */
967 DET(("No EEPROM: "));
968 mp->eeprom.bus_type = BUS_TYPE_SBUS;
David S. Miller9e6b6e72008-08-26 23:36:58 -0700969 mp->eeprom.cpuvers =
970 of_getintprop_default(dp, "cpu_version", 0);
971 mp->eeprom.cval =
972 of_getintprop_default(dp, "clock_value", 0);
973 mp->eeprom.ramsz = of_getintprop_default(dp, "sram_size", 0);
974 if (!mp->eeprom.cpuvers)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 mp->eeprom.cpuvers = CPUVERS_2_3;
David S. Miller9e6b6e72008-08-26 23:36:58 -0700976 if (mp->eeprom.cpuvers < CPUVERS_3_0)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700977 mp->eeprom.cval = 0;
David S. Miller9e6b6e72008-08-26 23:36:58 -0700978 if (!mp->eeprom.ramsz)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 mp->eeprom.ramsz = (128 * 1024);
David S. Miller9e6b6e72008-08-26 23:36:58 -0700980
981 prop = of_get_property(dp, "myrinet-board-id", &len);
982 if (prop)
983 memcpy(&mp->eeprom.id[0], prop, 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 else
985 set_boardid_from_idprom(mp, num);
David S. Miller9e6b6e72008-08-26 23:36:58 -0700986
987 prop = of_get_property(dp, "fpga_version", &len);
988 if (prop)
989 memcpy(&mp->eeprom.fvers[0], prop, 32);
990 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 memset(&mp->eeprom.fvers[0], 0, 32);
992
993 if (mp->eeprom.cpuvers == CPUVERS_4_1) {
David S. Miller9e6b6e72008-08-26 23:36:58 -0700994 if (mp->eeprom.ramsz == (128 * 1024))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995 mp->eeprom.ramsz = (256 * 1024);
David S. Miller9e6b6e72008-08-26 23:36:58 -0700996 if ((mp->eeprom.cval == 0x40414041) ||
997 (mp->eeprom.cval == 0x90449044))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700998 mp->eeprom.cval = 0x50e450e4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 }
1000 }
1001#ifdef DEBUG_DETECT
1002 dump_eeprom(mp);
1003#endif
1004
1005 for (i = 0; i < 6; i++)
1006 dev->dev_addr[i] = mp->eeprom.id[i];
1007
1008 determine_reg_space_size(mp);
1009
1010 /* Map in the MyriCOM register/localram set. */
1011 if (mp->eeprom.cpuvers < CPUVERS_4_0) {
1012 /* XXX Makes no sense, if control reg is non-existant this
1013 * XXX driver cannot function at all... maybe pre-4.0 is
1014 * XXX only a valid version for PCI cards? Ask feldy...
1015 */
1016 DET(("Mapping regs for cpuvers < CPUVERS_4_0\n"));
David S. Miller9e6b6e72008-08-26 23:36:58 -07001017 mp->regs = of_ioremap(&op->resource[0], 0,
1018 mp->reg_size, "MyriCOM Regs");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 if (!mp->regs) {
1020 printk("MyriCOM: Cannot map MyriCOM registers.\n");
1021 goto err;
1022 }
1023 mp->lanai = mp->regs + (256 * 1024);
1024 mp->lregs = mp->lanai + (0x10000 * 2);
1025 } else {
1026 DET(("Mapping regs for cpuvers >= CPUVERS_4_0\n"));
David S. Miller9e6b6e72008-08-26 23:36:58 -07001027 mp->cregs = of_ioremap(&op->resource[0], 0,
1028 PAGE_SIZE, "MyriCOM Control Regs");
1029 mp->lregs = of_ioremap(&op->resource[0], (256 * 1024),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 PAGE_SIZE, "MyriCOM LANAI Regs");
David S. Miller9e6b6e72008-08-26 23:36:58 -07001031 mp->lanai = of_ioremap(&op->resource[0], (512 * 1024),
1032 mp->eeprom.ramsz, "MyriCOM SRAM");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 }
1034 DET(("Registers mapped: cregs[%p] lregs[%p] lanai[%p]\n",
1035 mp->cregs, mp->lregs, mp->lanai));
1036
1037 if (mp->eeprom.cpuvers >= CPUVERS_4_0)
1038 mp->shmem_base = 0xf000;
1039 else
1040 mp->shmem_base = 0x8000;
1041
1042 DET(("Shared memory base is %04x, ", mp->shmem_base));
1043
1044 mp->shmem = (struct myri_shmem __iomem *)
1045 (mp->lanai + (mp->shmem_base * 2));
1046 DET(("shmem mapped at %p\n", mp->shmem));
1047
1048 mp->rqack = &mp->shmem->channel.recvqa;
1049 mp->rq = &mp->shmem->channel.recvq;
1050 mp->sq = &mp->shmem->channel.sendq;
1051
1052 /* Reset the board. */
1053 DET(("Resetting LANAI\n"));
1054 myri_reset_off(mp->lregs, mp->cregs);
1055 myri_reset_on(mp->cregs);
1056
1057 /* Turn IRQ's off. */
1058 myri_disable_irq(mp->lregs, mp->cregs);
1059
1060 /* Reset once more. */
1061 myri_reset_on(mp->cregs);
1062
1063 /* Get the supported DVMA burst sizes from our SBUS. */
David S. Miller9e6b6e72008-08-26 23:36:58 -07001064 mp->myri_bursts = of_getintprop_default(dp->parent,
1065 "burst-sizes", 0x00);
David S. Miller63237ee2008-08-26 23:33:42 -07001066 if (!sbus_can_burst64())
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067 mp->myri_bursts &= ~(DMA_BURST64);
1068
1069 DET(("MYRI bursts %02x\n", mp->myri_bursts));
1070
1071 /* Encode SBUS interrupt level in second control register. */
David S. Miller9e6b6e72008-08-26 23:36:58 -07001072 i = of_getintprop_default(dp, "interrupts", 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 if (i == 0)
1074 i = 4;
1075 DET(("prom_getint(interrupts)==%d, irqlvl set to %04x\n",
1076 i, (1 << i)));
1077
1078 sbus_writel((1 << i), mp->cregs + MYRICTRL_IRQLVL);
1079
1080 mp->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 dev->watchdog_timeo = 5*HZ;
David S. Miller9e6b6e72008-08-26 23:36:58 -07001082 dev->irq = op->irqs[0];
David S. Milleradaa0db2009-03-23 13:33:11 -07001083 dev->netdev_ops = &myri_ops;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084
1085 /* Register interrupt handler now. */
1086 DET(("Requesting MYRIcom IRQ line.\n"));
Joe Perchesa0607fd2009-11-18 23:29:17 -08001087 if (request_irq(dev->irq, myri_interrupt,
Thomas Gleixner1fb9df52006-07-01 19:29:39 -07001088 IRQF_SHARED, "MyriCOM Ethernet", (void *) dev)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089 printk("MyriCOM: Cannot register interrupt handler.\n");
1090 goto err;
1091 }
1092
1093 dev->mtu = MYRINET_MTU;
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001094 dev->header_ops = &myri_header_ops;
1095
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 dev->hard_header_len = (ETH_HLEN + MYRI_PAD_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 /* Load code onto the LANai. */
1099 DET(("Loading LANAI firmware\n"));
Jaswinder Singh Rajputcd36e9e2009-04-04 16:38:42 -07001100 if (myri_load_lanai(mp)) {
1101 printk(KERN_ERR "MyriCOM: Cannot Load LANAI firmware.\n");
1102 goto err_free_irq;
1103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104
1105 if (register_netdev(dev)) {
1106 printk("MyriCOM: Cannot register device.\n");
1107 goto err_free_irq;
1108 }
1109
David S. Miller9e6b6e72008-08-26 23:36:58 -07001110 dev_set_drvdata(&op->dev, mp);
David S. Millera46c30f2006-06-23 21:32:48 -07001111
1112 num++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113
Johannes Berge1749612008-10-27 15:59:26 -07001114 printk("%s: MyriCOM MyriNET Ethernet %pM\n",
1115 dev->name, dev->dev_addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116
1117 return 0;
1118
1119err_free_irq:
1120 free_irq(dev->irq, dev);
1121err:
Wang Chenb74ca3a2008-12-08 01:14:16 -08001122 /* This will also free the co-allocated private data*/
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 free_netdev(dev);
1124 return -ENODEV;
1125}
1126
David S. Miller9e6b6e72008-08-26 23:36:58 -07001127static int __devexit myri_sbus_remove(struct of_device *op)
David S. Millera46c30f2006-06-23 21:32:48 -07001128{
David S. Miller9e6b6e72008-08-26 23:36:58 -07001129 struct myri_eth *mp = dev_get_drvdata(&op->dev);
David S. Millera46c30f2006-06-23 21:32:48 -07001130 struct net_device *net_dev = mp->dev;
1131
Herbert Xu7afb9dc2008-10-05 09:20:28 -07001132 unregister_netdev(net_dev);
David S. Millera46c30f2006-06-23 21:32:48 -07001133
1134 free_irq(net_dev->irq, net_dev);
1135
1136 if (mp->eeprom.cpuvers < CPUVERS_4_0) {
David S. Miller9e6b6e72008-08-26 23:36:58 -07001137 of_iounmap(&op->resource[0], mp->regs, mp->reg_size);
David S. Millera46c30f2006-06-23 21:32:48 -07001138 } else {
David S. Miller9e6b6e72008-08-26 23:36:58 -07001139 of_iounmap(&op->resource[0], mp->cregs, PAGE_SIZE);
1140 of_iounmap(&op->resource[0], mp->lregs, (256 * 1024));
1141 of_iounmap(&op->resource[0], mp->lanai, (512 * 1024));
David S. Millera46c30f2006-06-23 21:32:48 -07001142 }
1143
1144 free_netdev(net_dev);
1145
David S. Miller9e6b6e72008-08-26 23:36:58 -07001146 dev_set_drvdata(&op->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148 return 0;
1149}
1150
David S. Millerfd098312008-08-31 01:23:17 -07001151static const struct of_device_id myri_sbus_match[] = {
David S. Millera46c30f2006-06-23 21:32:48 -07001152 {
1153 .name = "MYRICOM,mlanai",
1154 },
1155 {
1156 .name = "myri",
1157 },
1158 {},
1159};
1160
1161MODULE_DEVICE_TABLE(of, myri_sbus_match);
1162
1163static struct of_platform_driver myri_sbus_driver = {
1164 .name = "myri",
1165 .match_table = myri_sbus_match,
1166 .probe = myri_sbus_probe,
1167 .remove = __devexit_p(myri_sbus_remove),
1168};
1169
1170static int __init myri_sbus_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
David S. Miller9e6b6e72008-08-26 23:36:58 -07001172 return of_register_driver(&myri_sbus_driver, &of_bus_type);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
1174
David S. Millera46c30f2006-06-23 21:32:48 -07001175static void __exit myri_sbus_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176{
David S. Millera46c30f2006-06-23 21:32:48 -07001177 of_unregister_driver(&myri_sbus_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178}
1179
David S. Millera46c30f2006-06-23 21:32:48 -07001180module_init(myri_sbus_init);
1181module_exit(myri_sbus_exit);
1182
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183MODULE_LICENSE("GPL");
Jaswinder Singh Rajputcd36e9e2009-04-04 16:38:42 -07001184MODULE_FIRMWARE(FWNAME);