2 * Simulated Ethernet Driver
4 * Copyright (C) 1999-2001, 2003 Hewlett-Packard Co
5 * Stephane Eranian <eranian@hpl.hp.com>
7 #include <linux/config.h>
8 #include <linux/kernel.h>
9 #include <linux/sched.h>
10 #include <linux/types.h>
12 #include <linux/string.h>
13 #include <linux/init.h>
14 #include <linux/errno.h>
15 #include <linux/interrupt.h>
16 #include <linux/netdevice.h>
17 #include <linux/etherdevice.h>
18 #include <linux/inetdevice.h>
19 #include <linux/if_ether.h>
20 #include <linux/if_arp.h>
21 #include <linux/skbuff.h>
22 #include <linux/notifier.h>
23 #include <asm/bitops.h>
24 #include <asm/system.h>
27 #define SIMETH_RECV_MAX 10
30 * Maximum possible received frame for Ethernet.
31 * We preallocate an sk_buff of that size to avoid costly
32 * memcpy for temporary buffer into sk_buff. We do basically
33 * what's done in other drivers, like eepro with a ring.
34 * The difference is, of course, that we don't have real DMA !!!
36 #define SIMETH_FRAME_SIZE ETH_FRAME_LEN
39 #define SSC_NETDEV_PROBE 100
40 #define SSC_NETDEV_SEND 101
41 #define SSC_NETDEV_RECV 102
42 #define SSC_NETDEV_ATTACH 103
43 #define SSC_NETDEV_DETACH 104
45 #define NETWORK_INTR 8
48 struct net_device_stats stats;
49 int simfd; /* descriptor in the simulator */
52 static int simeth_probe1(void);
53 static int simeth_open(struct net_device *dev);
54 static int simeth_close(struct net_device *dev);
55 static int simeth_tx(struct sk_buff *skb, struct net_device *dev);
56 static int simeth_rx(struct net_device *dev);
57 static struct net_device_stats *simeth_get_stats(struct net_device *dev);
58 static irqreturn_t simeth_interrupt(int irq, void *dev_id, struct pt_regs * regs);
59 static void set_multicast_list(struct net_device *dev);
60 static int simeth_device_event(struct notifier_block *this,unsigned long event, void *ptr);
62 static char *simeth_version="0.3";
65 * This variable is used to establish a mapping between the Linux/ia64 kernel
66 * and the host linux kernel.
68 * As of today, we support only one card, even though most of the code
69 * is ready for many more. The mapping is then:
70 * linux/ia64 -> linux/x86
73 * In the future, we some string operations, we could easily support up
76 * The default mapping can be changed on the kernel command line by
77 * specifying simeth=ethX (or whatever string you want).
79 static char *simeth_device="eth0"; /* default host interface to use */
83 static volatile unsigned int card_count; /* how many cards "found" so far */
84 static int simeth_debug; /* set to 1 to get debug information */
87 * Used to catch IFF_UP & IFF_DOWN events
89 static struct notifier_block simeth_dev_notifier = {
96 * Function used when using a kernel command line option.
98 * Format: simeth=interface_name (like eth0)
101 simeth_setup(char *str)
107 __setup("simeth=", simeth_setup);
110 * Function used to probe for simeth devices when not installed
111 * as a loadable module
119 printk(KERN_INFO "simeth: v%s\n", simeth_version);
123 if (r == 0) register_netdevice_notifier(&simeth_dev_notifier);
128 extern long ia64_ssc (long, long, long, long, int);
129 extern void ia64_ssc_connect_irq (long intr, long irq);
132 netdev_probe(char *name, unsigned char *ether)
134 return ia64_ssc(__pa(name), __pa(ether), 0,0, SSC_NETDEV_PROBE);
139 netdev_connect(int irq)
142 * this does not support multiple cards
143 * also no return value
145 ia64_ssc_connect_irq(NETWORK_INTR, irq);
150 netdev_attach(int fd, int irq, unsigned int ipaddr)
152 /* this puts the host interface in the right mode (start interrupting) */
153 return ia64_ssc(fd, ipaddr, 0,0, SSC_NETDEV_ATTACH);
158 netdev_detach(int fd)
161 * inactivate the host interface (don't interrupt anymore) */
162 return ia64_ssc(fd, 0,0,0, SSC_NETDEV_DETACH);
166 netdev_send(int fd, unsigned char *buf, unsigned int len)
168 return ia64_ssc(fd, __pa(buf), len, 0, SSC_NETDEV_SEND);
172 netdev_read(int fd, unsigned char *buf, unsigned int len)
174 return ia64_ssc(fd, __pa(buf), len, 0, SSC_NETDEV_RECV);
178 * Function shared with module code, so cannot be in init section
180 * So far this function "detects" only one card (test_&_set) but could
181 * be extended easily.
184 * - -ENODEV is no device found
185 * - -ENOMEM is no more memory
191 unsigned char mac_addr[ETH_ALEN];
192 struct simeth_local *local;
193 struct net_device *dev;
198 * let's support just one card for now
200 if (test_and_set_bit(0, &card_count))
204 * check with the simulator for the device
206 fd = netdev_probe(simeth_device, mac_addr);
210 dev = init_etherdev(NULL, sizeof(struct simeth_local));
214 memcpy(dev->dev_addr, mac_addr, sizeof(mac_addr));
216 dev->irq = ia64_alloc_vector();
219 * attach the interrupt in the simulator, this does enable interrupts
220 * until a netdev_attach() is called
222 netdev_connect(dev->irq);
224 memset(dev->priv, 0, sizeof(struct simeth_local));
227 local->simfd = fd; /* keep track of underlying file descriptor */
229 dev->open = simeth_open;
230 dev->stop = simeth_close;
231 dev->hard_start_xmit = simeth_tx;
232 dev->get_stats = simeth_get_stats;
233 dev->set_multicast_list = set_multicast_list; /* no yet used */
235 /* Fill in the fields of the device structure with ethernet-generic values. */
238 printk(KERN_INFO "%s: hosteth=%s simfd=%d, HwAddr",
239 dev->name, simeth_device, local->simfd);
240 for(i = 0; i < ETH_ALEN; i++) {
241 printk(" %2.2x", dev->dev_addr[i]);
243 printk(", IRQ %d\n", dev->irq);
249 * actually binds the device to an interrupt vector
252 simeth_open(struct net_device *dev)
254 if (request_irq(dev->irq, simeth_interrupt, 0, "simeth", dev)) {
255 printk(KERN_WARNING "simeth: unable to get IRQ %d.\n", dev->irq);
259 netif_start_queue(dev);
264 /* copied from lapbether.c */
265 static __inline__ int dev_is_ethdev(struct net_device *dev)
267 return ( dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5));
272 * Handler for IFF_UP or IFF_DOWN
274 * The reason for that is that we don't want to be interrupted when the
275 * interface is down. There is no way to unconnect in the simualtor. Instead
276 * we use this function to shutdown packet processing in the frame filter
277 * in the simulator. Thus no interrupts are generated
280 * That's also the place where we pass the IP address of this device to the
281 * simulator so that that we can start filtering packets for it
283 * There may be a better way of doing this, but I don't know which yet.
286 simeth_device_event(struct notifier_block *this,unsigned long event, void *ptr)
288 struct net_device *dev = (struct net_device *)ptr;
289 struct simeth_local *local;
290 struct in_device *in_dev;
291 struct in_ifaddr **ifap = NULL;
292 struct in_ifaddr *ifa = NULL;
297 printk(KERN_WARNING "simeth_device_event dev=0\n");
301 if ( event != NETDEV_UP && event != NETDEV_DOWN ) return NOTIFY_DONE;
304 * Check whether or not it's for an ethernet device
306 * XXX Fixme: This works only as long as we support one
307 * type of ethernet device.
309 if ( !dev_is_ethdev(dev) ) return NOTIFY_DONE;
311 if ((in_dev=dev->ip_ptr) != NULL) {
312 for (ifap=&in_dev->ifa_list; (ifa=*ifap) != NULL; ifap=&ifa->ifa_next)
313 if (strcmp(dev->name, ifa->ifa_label) == 0) break;
316 printk(KERN_ERR "simeth_open: can't find device %s's ifa\n", dev->name);
320 printk(KERN_INFO "simeth_device_event: %s ipaddr=0x%x\n",
321 dev->name, htonl(ifa->ifa_local));
325 * if the device was up, and we're simply reconfiguring it, not sure
326 * we get DOWN then UP.
330 /* now do it for real */
331 r = event == NETDEV_UP ?
332 netdev_attach(local->simfd, dev->irq, htonl(ifa->ifa_local)):
333 netdev_detach(local->simfd);
335 printk(KERN_INFO "simeth: netdev_attach/detach: event=%s ->%d\n",
336 event == NETDEV_UP ? "attach":"detach", r);
342 simeth_close(struct net_device *dev)
344 netif_stop_queue(dev);
346 free_irq(dev->irq, dev);
352 * Only used for debug
355 frame_print(unsigned char *from, unsigned char *frame, int len)
359 printk("%s: (%d) %02x", from, len, frame[0] & 0xff);
360 for(i=1; i < 6; i++ ) {
361 printk(":%02x", frame[i] &0xff);
363 printk(" %2x", frame[6] &0xff);
364 for(i=7; i < 12; i++ ) {
365 printk(":%02x", frame[i] &0xff);
367 printk(" [%02x%02x]\n", frame[12], frame[13]);
369 for(i=14; i < len; i++ ) {
370 printk("%02x ", frame[i] &0xff);
371 if ( (i%10)==0) printk("\n");
378 * Function used to transmit of frame, very last one on the path before
379 * going to the simulator.
382 simeth_tx(struct sk_buff *skb, struct net_device *dev)
384 struct simeth_local *local = (struct simeth_local *)dev->priv;
387 /* ensure we have at least ETH_ZLEN bytes (min frame size) */
388 unsigned int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
389 /* Where do the extra padding bytes comes from inthe skbuff ? */
391 /* the real driver in the host system is going to take care of that
392 * or maybe it's the NIC itself.
394 unsigned int length = skb->len;
397 local->stats.tx_bytes += skb->len;
398 local->stats.tx_packets++;
401 if (simeth_debug > 5) frame_print("simeth_tx", skb->data, length);
403 netdev_send(local->simfd, skb->data, length);
406 * we are synchronous on write, so we don't simulate a
407 * trasnmit complete interrupt, thus we don't need to arm a tx
414 static inline struct sk_buff *
415 make_new_skb(struct net_device *dev)
417 struct sk_buff *nskb;
420 * The +2 is used to make sure that the IP header is nicely
421 * aligned (on 4byte boundary I assume 14+2=16)
423 nskb = dev_alloc_skb(SIMETH_FRAME_SIZE + 2);
424 if ( nskb == NULL ) {
425 printk(KERN_NOTICE "%s: memory squeeze. dropping packet.\n", dev->name);
430 skb_reserve(nskb, 2); /* Align IP on 16 byte boundaries */
432 skb_put(nskb,SIMETH_FRAME_SIZE);
438 * called from interrupt handler to process a received frame
441 simeth_rx(struct net_device *dev)
443 struct simeth_local *local;
446 int rcv_count = SIMETH_RECV_MAX;
448 local = (struct simeth_local *)dev->priv;
450 * the loop concept has been borrowed from other drivers
451 * looks to me like it's a throttling thing to avoid pushing to many
452 * packets at one time into the stack. Making sure we can process them
453 * upstream and make forward progress overall
456 if ( (skb=make_new_skb(dev)) == NULL ) {
457 printk(KERN_NOTICE "%s: memory squeeze. dropping packet.\n", dev->name);
458 local->stats.rx_dropped++;
462 * Read only one frame at a time
464 len = netdev_read(local->simfd, skb->data, SIMETH_FRAME_SIZE);
466 if ( simeth_debug > 0 ) printk(KERN_WARNING "%s: count=%d netdev_read=0\n",
467 dev->name, SIMETH_RECV_MAX-rcv_count);
473 * Should really do a csum+copy here
475 memcpy(skb->data, frame, len);
477 skb->protocol = eth_type_trans(skb, dev);
479 if ( simeth_debug > 6 ) frame_print("simeth_rx", skb->data, len);
482 * push the packet up & trigger software interrupt
486 local->stats.rx_packets++;
487 local->stats.rx_bytes += len;
489 } while ( --rcv_count );
491 return len; /* 0 = nothing left to read, otherwise, we can try again */
495 * Interrupt handler (Yes, we can do it too !!!)
498 simeth_interrupt(int irq, void *dev_id, struct pt_regs * regs)
500 struct net_device *dev = dev_id;
503 printk(KERN_WARNING "simeth: irq %d for unknown device\n", irq);
508 * very simple loop because we get interrupts only when receiving
510 while (simeth_rx(dev));
514 static struct net_device_stats *
515 simeth_get_stats(struct net_device *dev)
517 struct simeth_local *local = (struct simeth_local *) dev->priv;
519 return &local->stats;
522 /* fake multicast ability */
524 set_multicast_list(struct net_device *dev)
526 printk(KERN_WARNING "%s: set_multicast_list called\n", dev->name);
529 #ifdef CONFIG_NET_FASTROUTE
531 simeth_accept_fastpath(struct net_device *dev, struct dst_entry *dst)
533 printk(KERN_WARNING "%s: simeth_accept_fastpath called\n", dev->name);
538 __initcall(simeth_probe);