Linux Audio

Check our new training course

Loading...
v4.6
 
  1/*
  2 * Simulated Ethernet Driver
  3 *
  4 * Copyright (C) 1999-2001, 2003 Hewlett-Packard Co
  5 *	Stephane Eranian <eranian@hpl.hp.com>
  6 */
  7#include <linux/kernel.h>
  8#include <linux/sched.h>
  9#include <linux/types.h>
 10#include <linux/in.h>
 11#include <linux/string.h>
 12#include <linux/init.h>
 13#include <linux/errno.h>
 14#include <linux/interrupt.h>
 15#include <linux/netdevice.h>
 16#include <linux/etherdevice.h>
 17#include <linux/inetdevice.h>
 18#include <linux/if_ether.h>
 19#include <linux/if_arp.h>
 20#include <linux/skbuff.h>
 21#include <linux/notifier.h>
 22#include <linux/bitops.h>
 23#include <asm/irq.h>
 24#include <asm/hpsim.h>
 25
 26#include "hpsim_ssc.h"
 27
 28#define SIMETH_RECV_MAX	10
 29
 30/*
 31 * Maximum possible received frame for Ethernet.
 32 * We preallocate an sk_buff of that size to avoid costly
 33 * memcpy for temporary buffer into sk_buff. We do basically
 34 * what's done in other drivers, like eepro with a ring.
 35 * The difference is, of course, that we don't have real DMA !!!
 36 */
 37#define SIMETH_FRAME_SIZE	ETH_FRAME_LEN
 38
 39
 40#define NETWORK_INTR			8
 41
 42struct simeth_local {
 43	struct net_device_stats stats;
 44	int 			simfd;	 /* descriptor in the simulator */
 45};
 46
 47static int simeth_probe1(void);
 48static int simeth_open(struct net_device *dev);
 49static int simeth_close(struct net_device *dev);
 50static int simeth_tx(struct sk_buff *skb, struct net_device *dev);
 51static int simeth_rx(struct net_device *dev);
 52static struct net_device_stats *simeth_get_stats(struct net_device *dev);
 53static irqreturn_t simeth_interrupt(int irq, void *dev_id);
 54static void set_multicast_list(struct net_device *dev);
 55static int simeth_device_event(struct notifier_block *this,unsigned long event, void *ptr);
 56
 57static char *simeth_version="0.3";
 58
 59/*
 60 * This variable is used to establish a mapping between the Linux/ia64 kernel
 61 * and the host linux kernel.
 62 *
 63 * As of today, we support only one card, even though most of the code
 64 * is ready for many more. The mapping is then:
 65 *	linux/ia64 -> linux/x86
 66 * 	   eth0    -> eth1
 67 *
 68 * In the future, we some string operations, we could easily support up
 69 * to 10 cards (0-9).
 70 *
 71 * The default mapping can be changed on the kernel command line by
 72 * specifying simeth=ethX (or whatever string you want).
 73 */
 74static char *simeth_device="eth0";	 /* default host interface to use */
 75
 76
 77
 78static volatile unsigned int card_count; /* how many cards "found" so far */
 79static int simeth_debug;		/* set to 1 to get debug information */
 80
 81/*
 82 * Used to catch IFF_UP & IFF_DOWN events
 83 */
 84static struct notifier_block simeth_dev_notifier = {
 85	simeth_device_event,
 86	NULL
 87};
 88
 89
 90/*
 91 * Function used when using a kernel command line option.
 92 *
 93 * Format: simeth=interface_name (like eth0)
 94 */
 95static int __init
 96simeth_setup(char *str)
 97{
 98	simeth_device = str;
 99	return 1;
100}
101
102__setup("simeth=", simeth_setup);
103
104/*
105 * Function used to probe for simeth devices when not installed
106 * as a loadable module
107 */
108
109int __init
110simeth_probe (void)
111{
112	int r;
113
114	printk(KERN_INFO "simeth: v%s\n", simeth_version);
115
116	r = simeth_probe1();
117
118	if (r == 0) register_netdevice_notifier(&simeth_dev_notifier);
119
120	return r;
121}
122
123static inline int
124netdev_probe(char *name, unsigned char *ether)
125{
126	return ia64_ssc(__pa(name), __pa(ether), 0,0, SSC_NETDEV_PROBE);
127}
128
129
130static inline int
131netdev_attach(int fd, int irq, unsigned int ipaddr)
132{
133	/* this puts the host interface in the right mode (start interrupting) */
134	return ia64_ssc(fd, ipaddr, 0,0, SSC_NETDEV_ATTACH);
135}
136
137
138static inline int
139netdev_detach(int fd)
140{
141	/*
142	 * inactivate the host interface (don't interrupt anymore) */
143	return ia64_ssc(fd, 0,0,0, SSC_NETDEV_DETACH);
144}
145
146static inline int
147netdev_send(int fd, unsigned char *buf, unsigned int len)
148{
149	return ia64_ssc(fd, __pa(buf), len, 0, SSC_NETDEV_SEND);
150}
151
152static inline int
153netdev_read(int fd, unsigned char *buf, unsigned int len)
154{
155	return ia64_ssc(fd, __pa(buf), len, 0, SSC_NETDEV_RECV);
156}
157
158static const struct net_device_ops simeth_netdev_ops = {
159	.ndo_open		= simeth_open,
160	.ndo_stop		= simeth_close,
161	.ndo_start_xmit		= simeth_tx,
162	.ndo_get_stats		= simeth_get_stats,
163	.ndo_set_rx_mode	= set_multicast_list, /* not yet used */
164
165};
166
167/*
168 * Function shared with module code, so cannot be in init section
169 *
170 * So far this function "detects" only one card (test_&_set) but could
171 * be extended easily.
172 *
173 * Return:
174 * 	- -ENODEV is no device found
175 *	- -ENOMEM is no more memory
176 *	- 0 otherwise
177 */
178static int
179simeth_probe1(void)
180{
181	unsigned char mac_addr[ETH_ALEN];
182	struct simeth_local *local;
183	struct net_device *dev;
184	int fd, err, rc;
185
186	/*
187	 * XXX Fix me
188	 * let's support just one card for now
189	 */
190	if (test_and_set_bit(0, &card_count))
191		return -ENODEV;
192
193	/*
194	 * check with the simulator for the device
195	 */
196	fd = netdev_probe(simeth_device, mac_addr);
197	if (fd == -1)
198		return -ENODEV;
199
200	dev = alloc_etherdev(sizeof(struct simeth_local));
201	if (!dev)
202		return -ENOMEM;
203
204	memcpy(dev->dev_addr, mac_addr, sizeof(mac_addr));
205
206	local = netdev_priv(dev);
207	local->simfd = fd; /* keep track of underlying file descriptor */
208
209	dev->netdev_ops = &simeth_netdev_ops;
210
211	err = register_netdev(dev);
212	if (err) {
213		free_netdev(dev);
214		return err;
215	}
216
217	/*
218	 * attach the interrupt in the simulator, this does enable interrupts
219	 * until a netdev_attach() is called
220	 */
221	if ((rc = hpsim_get_irq(NETWORK_INTR)) < 0)
222		panic("%s: out of interrupt vectors!\n", __func__);
223	dev->irq = rc;
224
225	printk(KERN_INFO "%s: hosteth=%s simfd=%d, HwAddr=%pm, IRQ %d\n",
226	       dev->name, simeth_device, local->simfd, dev->dev_addr, dev->irq);
227
228	return 0;
229}
230
231/*
232 * actually binds the device to an interrupt vector
233 */
234static int
235simeth_open(struct net_device *dev)
236{
237	if (request_irq(dev->irq, simeth_interrupt, 0, "simeth", dev)) {
238		printk(KERN_WARNING "simeth: unable to get IRQ %d.\n", dev->irq);
239		return -EAGAIN;
240	}
241
242	netif_start_queue(dev);
243
244	return 0;
245}
246
247/* copied from lapbether.c */
248static __inline__ int dev_is_ethdev(struct net_device *dev)
249{
250       return ( dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5));
251}
252
253
254/*
255 * Handler for IFF_UP or IFF_DOWN
256 *
257 * The reason for that is that we don't want to be interrupted when the
258 * interface is down. There is no way to unconnect in the simualtor. Instead
259 * we use this function to shutdown packet processing in the frame filter
260 * in the simulator. Thus no interrupts are generated
261 *
262 *
263 * That's also the place where we pass the IP address of this device to the
264 * simulator so that that we can start filtering packets for it
265 *
266 * There may be a better way of doing this, but I don't know which yet.
267 */
268static int
269simeth_device_event(struct notifier_block *this,unsigned long event, void *ptr)
270{
271	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
272	struct simeth_local *local;
273	struct in_device *in_dev;
274	struct in_ifaddr **ifap = NULL;
275	struct in_ifaddr *ifa = NULL;
276	int r;
277
278
279	if ( ! dev ) {
280		printk(KERN_WARNING "simeth_device_event dev=0\n");
281		return NOTIFY_DONE;
282	}
283
284	if (dev_net(dev) != &init_net)
285		return NOTIFY_DONE;
286
287	if ( event != NETDEV_UP && event != NETDEV_DOWN ) return NOTIFY_DONE;
288
289	/*
290	 * Check whether or not it's for an ethernet device
291	 *
292	 * XXX Fixme: This works only as long as we support one
293	 * type of ethernet device.
294	 */
295	if ( !dev_is_ethdev(dev) ) return NOTIFY_DONE;
296
297	if ((in_dev=dev->ip_ptr) != NULL) {
298		for (ifap=&in_dev->ifa_list; (ifa=*ifap) != NULL; ifap=&ifa->ifa_next)
299			if (strcmp(dev->name, ifa->ifa_label) == 0) break;
300	}
301	if ( ifa == NULL ) {
302		printk(KERN_ERR "simeth_open: can't find device %s's ifa\n", dev->name);
303		return NOTIFY_DONE;
304	}
305
306	printk(KERN_INFO "simeth_device_event: %s ipaddr=0x%x\n",
307	       dev->name, ntohl(ifa->ifa_local));
308
309	/*
310	 * XXX Fix me
311	 * if the device was up, and we're simply reconfiguring it, not sure
312	 * we get DOWN then UP.
313	 */
314
315	local = netdev_priv(dev);
316	/* now do it for real */
317	r = event == NETDEV_UP ?
318		netdev_attach(local->simfd, dev->irq, ntohl(ifa->ifa_local)):
319		netdev_detach(local->simfd);
320
321	printk(KERN_INFO "simeth: netdev_attach/detach: event=%s ->%d\n",
322	       event == NETDEV_UP ? "attach":"detach", r);
323
324	return NOTIFY_DONE;
325}
326
327static int
328simeth_close(struct net_device *dev)
329{
330	netif_stop_queue(dev);
331
332	free_irq(dev->irq, dev);
333
334	return 0;
335}
336
337/*
338 * Only used for debug
339 */
340static void
341frame_print(unsigned char *from, unsigned char *frame, int len)
342{
343	int i;
344
345	printk("%s: (%d) %02x", from, len, frame[0] & 0xff);
346	for(i=1; i < 6; i++ ) {
347		printk(":%02x", frame[i] &0xff);
348	}
349	printk(" %2x", frame[6] &0xff);
350	for(i=7; i < 12; i++ ) {
351		printk(":%02x", frame[i] &0xff);
352	}
353	printk(" [%02x%02x]\n", frame[12], frame[13]);
354
355	for(i=14; i < len; i++ ) {
356		printk("%02x ", frame[i] &0xff);
357		if ( (i%10)==0) printk("\n");
358	}
359	printk("\n");
360}
361
362
363/*
364 * Function used to transmit of frame, very last one on the path before
365 * going to the simulator.
366 */
367static int
368simeth_tx(struct sk_buff *skb, struct net_device *dev)
369{
370	struct simeth_local *local = netdev_priv(dev);
371
372#if 0
373	/* ensure we have at least ETH_ZLEN bytes (min frame size) */
374	unsigned int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
375	/* Where do the extra padding bytes comes from inthe skbuff ? */
376#else
377	/* the real driver in the host system is going to take care of that
378	 * or maybe it's the NIC itself.
379	 */
380	unsigned int length = skb->len;
381#endif
382
383	local->stats.tx_bytes += skb->len;
384	local->stats.tx_packets++;
385
386
387	if (simeth_debug > 5) frame_print("simeth_tx", skb->data, length);
388
389	netdev_send(local->simfd, skb->data, length);
390
391	/*
392	 * we are synchronous on write, so we don't simulate a
393	 * trasnmit complete interrupt, thus we don't need to arm a tx
394	 */
395
396	dev_kfree_skb(skb);
397	return NETDEV_TX_OK;
398}
399
400static inline struct sk_buff *
401make_new_skb(struct net_device *dev)
402{
403	struct sk_buff *nskb;
404
405	/*
406	 * The +2 is used to make sure that the IP header is nicely
407	 * aligned (on 4byte boundary I assume 14+2=16)
408	 */
409	nskb = dev_alloc_skb(SIMETH_FRAME_SIZE + 2);
410	if ( nskb == NULL ) {
411		printk(KERN_NOTICE "%s: memory squeeze. dropping packet.\n", dev->name);
412		return NULL;
413	}
414
415	skb_reserve(nskb, 2);	/* Align IP on 16 byte boundaries */
416
417	skb_put(nskb,SIMETH_FRAME_SIZE);
418
419	return nskb;
420}
421
422/*
423 * called from interrupt handler to process a received frame
424 */
425static int
426simeth_rx(struct net_device *dev)
427{
428	struct simeth_local	*local;
429	struct sk_buff		*skb;
430	int			len;
431	int			rcv_count = SIMETH_RECV_MAX;
432
433	local = netdev_priv(dev);
434	/*
435	 * the loop concept has been borrowed from other drivers
436	 * looks to me like it's a throttling thing to avoid pushing to many
437	 * packets at one time into the stack. Making sure we can process them
438	 * upstream and make forward progress overall
439	 */
440	do {
441		if ( (skb=make_new_skb(dev)) == NULL ) {
442			printk(KERN_NOTICE "%s: memory squeeze. dropping packet.\n", dev->name);
443			local->stats.rx_dropped++;
444			return 0;
445		}
446		/*
447		 * Read only one frame at a time
448		 */
449		len = netdev_read(local->simfd, skb->data, SIMETH_FRAME_SIZE);
450		if ( len == 0 ) {
451			if ( simeth_debug > 0 ) printk(KERN_WARNING "%s: count=%d netdev_read=0\n",
452						       dev->name, SIMETH_RECV_MAX-rcv_count);
453			break;
454		}
455#if 0
456		/*
457		 * XXX Fix me
458		 * Should really do a csum+copy here
459		 */
460		skb_copy_to_linear_data(skb, frame, len);
461#endif
462		skb->protocol = eth_type_trans(skb, dev);
463
464		if ( simeth_debug > 6 ) frame_print("simeth_rx", skb->data, len);
465
466		/*
467		 * push the packet up & trigger software interrupt
468		 */
469		netif_rx(skb);
470
471		local->stats.rx_packets++;
472		local->stats.rx_bytes += len;
473
474	} while ( --rcv_count );
475
476	return len; /* 0 = nothing left to read, otherwise, we can try again */
477}
478
479/*
480 * Interrupt handler (Yes, we can do it too !!!)
481 */
482static irqreturn_t
483simeth_interrupt(int irq, void *dev_id)
484{
485	struct net_device *dev = dev_id;
486
487	/*
488	 * very simple loop because we get interrupts only when receiving
489	 */
490	while (simeth_rx(dev));
491	return IRQ_HANDLED;
492}
493
494static struct net_device_stats *
495simeth_get_stats(struct net_device *dev)
496{
497	struct simeth_local *local = netdev_priv(dev);
498
499	return &local->stats;
500}
501
502/* fake multicast ability */
503static void
504set_multicast_list(struct net_device *dev)
505{
506	printk(KERN_WARNING "%s: set_multicast_list called\n", dev->name);
507}
508
509__initcall(simeth_probe);
v4.17
  1// SPDX-License-Identifier: GPL-2.0
  2/*
  3 * Simulated Ethernet Driver
  4 *
  5 * Copyright (C) 1999-2001, 2003 Hewlett-Packard Co
  6 *	Stephane Eranian <eranian@hpl.hp.com>
  7 */
  8#include <linux/kernel.h>
  9#include <linux/sched.h>
 10#include <linux/types.h>
 11#include <linux/in.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 <linux/bitops.h>
 24#include <asm/irq.h>
 25#include <asm/hpsim.h>
 26
 27#include "hpsim_ssc.h"
 28
 29#define SIMETH_RECV_MAX	10
 30
 31/*
 32 * Maximum possible received frame for Ethernet.
 33 * We preallocate an sk_buff of that size to avoid costly
 34 * memcpy for temporary buffer into sk_buff. We do basically
 35 * what's done in other drivers, like eepro with a ring.
 36 * The difference is, of course, that we don't have real DMA !!!
 37 */
 38#define SIMETH_FRAME_SIZE	ETH_FRAME_LEN
 39
 40
 41#define NETWORK_INTR			8
 42
 43struct simeth_local {
 44	struct net_device_stats stats;
 45	int 			simfd;	 /* descriptor in the simulator */
 46};
 47
 48static int simeth_probe1(void);
 49static int simeth_open(struct net_device *dev);
 50static int simeth_close(struct net_device *dev);
 51static int simeth_tx(struct sk_buff *skb, struct net_device *dev);
 52static int simeth_rx(struct net_device *dev);
 53static struct net_device_stats *simeth_get_stats(struct net_device *dev);
 54static irqreturn_t simeth_interrupt(int irq, void *dev_id);
 55static void set_multicast_list(struct net_device *dev);
 56static int simeth_device_event(struct notifier_block *this,unsigned long event, void *ptr);
 57
 58static char *simeth_version="0.3";
 59
 60/*
 61 * This variable is used to establish a mapping between the Linux/ia64 kernel
 62 * and the host linux kernel.
 63 *
 64 * As of today, we support only one card, even though most of the code
 65 * is ready for many more. The mapping is then:
 66 *	linux/ia64 -> linux/x86
 67 * 	   eth0    -> eth1
 68 *
 69 * In the future, we some string operations, we could easily support up
 70 * to 10 cards (0-9).
 71 *
 72 * The default mapping can be changed on the kernel command line by
 73 * specifying simeth=ethX (or whatever string you want).
 74 */
 75static char *simeth_device="eth0";	 /* default host interface to use */
 76
 77
 78
 79static volatile unsigned int card_count; /* how many cards "found" so far */
 80static int simeth_debug;		/* set to 1 to get debug information */
 81
 82/*
 83 * Used to catch IFF_UP & IFF_DOWN events
 84 */
 85static struct notifier_block simeth_dev_notifier = {
 86	simeth_device_event,
 87	NULL
 88};
 89
 90
 91/*
 92 * Function used when using a kernel command line option.
 93 *
 94 * Format: simeth=interface_name (like eth0)
 95 */
 96static int __init
 97simeth_setup(char *str)
 98{
 99	simeth_device = str;
100	return 1;
101}
102
103__setup("simeth=", simeth_setup);
104
105/*
106 * Function used to probe for simeth devices when not installed
107 * as a loadable module
108 */
109
110int __init
111simeth_probe (void)
112{
113	int r;
114
115	printk(KERN_INFO "simeth: v%s\n", simeth_version);
116
117	r = simeth_probe1();
118
119	if (r == 0) register_netdevice_notifier(&simeth_dev_notifier);
120
121	return r;
122}
123
124static inline int
125netdev_probe(char *name, unsigned char *ether)
126{
127	return ia64_ssc(__pa(name), __pa(ether), 0,0, SSC_NETDEV_PROBE);
128}
129
130
131static inline int
132netdev_attach(int fd, int irq, unsigned int ipaddr)
133{
134	/* this puts the host interface in the right mode (start interrupting) */
135	return ia64_ssc(fd, ipaddr, 0,0, SSC_NETDEV_ATTACH);
136}
137
138
139static inline int
140netdev_detach(int fd)
141{
142	/*
143	 * inactivate the host interface (don't interrupt anymore) */
144	return ia64_ssc(fd, 0,0,0, SSC_NETDEV_DETACH);
145}
146
147static inline int
148netdev_send(int fd, unsigned char *buf, unsigned int len)
149{
150	return ia64_ssc(fd, __pa(buf), len, 0, SSC_NETDEV_SEND);
151}
152
153static inline int
154netdev_read(int fd, unsigned char *buf, unsigned int len)
155{
156	return ia64_ssc(fd, __pa(buf), len, 0, SSC_NETDEV_RECV);
157}
158
159static const struct net_device_ops simeth_netdev_ops = {
160	.ndo_open		= simeth_open,
161	.ndo_stop		= simeth_close,
162	.ndo_start_xmit		= simeth_tx,
163	.ndo_get_stats		= simeth_get_stats,
164	.ndo_set_rx_mode	= set_multicast_list, /* not yet used */
165
166};
167
168/*
169 * Function shared with module code, so cannot be in init section
170 *
171 * So far this function "detects" only one card (test_&_set) but could
172 * be extended easily.
173 *
174 * Return:
175 * 	- -ENODEV is no device found
176 *	- -ENOMEM is no more memory
177 *	- 0 otherwise
178 */
179static int
180simeth_probe1(void)
181{
182	unsigned char mac_addr[ETH_ALEN];
183	struct simeth_local *local;
184	struct net_device *dev;
185	int fd, err, rc;
186
187	/*
188	 * XXX Fix me
189	 * let's support just one card for now
190	 */
191	if (test_and_set_bit(0, &card_count))
192		return -ENODEV;
193
194	/*
195	 * check with the simulator for the device
196	 */
197	fd = netdev_probe(simeth_device, mac_addr);
198	if (fd == -1)
199		return -ENODEV;
200
201	dev = alloc_etherdev(sizeof(struct simeth_local));
202	if (!dev)
203		return -ENOMEM;
204
205	memcpy(dev->dev_addr, mac_addr, sizeof(mac_addr));
206
207	local = netdev_priv(dev);
208	local->simfd = fd; /* keep track of underlying file descriptor */
209
210	dev->netdev_ops = &simeth_netdev_ops;
211
212	err = register_netdev(dev);
213	if (err) {
214		free_netdev(dev);
215		return err;
216	}
217
218	/*
219	 * attach the interrupt in the simulator, this does enable interrupts
220	 * until a netdev_attach() is called
221	 */
222	if ((rc = hpsim_get_irq(NETWORK_INTR)) < 0)
223		panic("%s: out of interrupt vectors!\n", __func__);
224	dev->irq = rc;
225
226	printk(KERN_INFO "%s: hosteth=%s simfd=%d, HwAddr=%pm, IRQ %d\n",
227	       dev->name, simeth_device, local->simfd, dev->dev_addr, dev->irq);
228
229	return 0;
230}
231
232/*
233 * actually binds the device to an interrupt vector
234 */
235static int
236simeth_open(struct net_device *dev)
237{
238	if (request_irq(dev->irq, simeth_interrupt, 0, "simeth", dev)) {
239		printk(KERN_WARNING "simeth: unable to get IRQ %d.\n", dev->irq);
240		return -EAGAIN;
241	}
242
243	netif_start_queue(dev);
244
245	return 0;
246}
247
248/* copied from lapbether.c */
249static __inline__ int dev_is_ethdev(struct net_device *dev)
250{
251       return ( dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5));
252}
253
254
255/*
256 * Handler for IFF_UP or IFF_DOWN
257 *
258 * The reason for that is that we don't want to be interrupted when the
259 * interface is down. There is no way to unconnect in the simualtor. Instead
260 * we use this function to shutdown packet processing in the frame filter
261 * in the simulator. Thus no interrupts are generated
262 *
263 *
264 * That's also the place where we pass the IP address of this device to the
265 * simulator so that that we can start filtering packets for it
266 *
267 * There may be a better way of doing this, but I don't know which yet.
268 */
269static int
270simeth_device_event(struct notifier_block *this,unsigned long event, void *ptr)
271{
272	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
273	struct simeth_local *local;
274	struct in_device *in_dev;
275	struct in_ifaddr **ifap = NULL;
276	struct in_ifaddr *ifa = NULL;
277	int r;
278
279
280	if ( ! dev ) {
281		printk(KERN_WARNING "simeth_device_event dev=0\n");
282		return NOTIFY_DONE;
283	}
284
285	if (dev_net(dev) != &init_net)
286		return NOTIFY_DONE;
287
288	if ( event != NETDEV_UP && event != NETDEV_DOWN ) return NOTIFY_DONE;
289
290	/*
291	 * Check whether or not it's for an ethernet device
292	 *
293	 * XXX Fixme: This works only as long as we support one
294	 * type of ethernet device.
295	 */
296	if ( !dev_is_ethdev(dev) ) return NOTIFY_DONE;
297
298	if ((in_dev=dev->ip_ptr) != NULL) {
299		for (ifap=&in_dev->ifa_list; (ifa=*ifap) != NULL; ifap=&ifa->ifa_next)
300			if (strcmp(dev->name, ifa->ifa_label) == 0) break;
301	}
302	if ( ifa == NULL ) {
303		printk(KERN_ERR "simeth_open: can't find device %s's ifa\n", dev->name);
304		return NOTIFY_DONE;
305	}
306
307	printk(KERN_INFO "simeth_device_event: %s ipaddr=0x%x\n",
308	       dev->name, ntohl(ifa->ifa_local));
309
310	/*
311	 * XXX Fix me
312	 * if the device was up, and we're simply reconfiguring it, not sure
313	 * we get DOWN then UP.
314	 */
315
316	local = netdev_priv(dev);
317	/* now do it for real */
318	r = event == NETDEV_UP ?
319		netdev_attach(local->simfd, dev->irq, ntohl(ifa->ifa_local)):
320		netdev_detach(local->simfd);
321
322	printk(KERN_INFO "simeth: netdev_attach/detach: event=%s ->%d\n",
323	       event == NETDEV_UP ? "attach":"detach", r);
324
325	return NOTIFY_DONE;
326}
327
328static int
329simeth_close(struct net_device *dev)
330{
331	netif_stop_queue(dev);
332
333	free_irq(dev->irq, dev);
334
335	return 0;
336}
337
338/*
339 * Only used for debug
340 */
341static void
342frame_print(unsigned char *from, unsigned char *frame, int len)
343{
344	int i;
345
346	printk("%s: (%d) %02x", from, len, frame[0] & 0xff);
347	for(i=1; i < 6; i++ ) {
348		printk(":%02x", frame[i] &0xff);
349	}
350	printk(" %2x", frame[6] &0xff);
351	for(i=7; i < 12; i++ ) {
352		printk(":%02x", frame[i] &0xff);
353	}
354	printk(" [%02x%02x]\n", frame[12], frame[13]);
355
356	for(i=14; i < len; i++ ) {
357		printk("%02x ", frame[i] &0xff);
358		if ( (i%10)==0) printk("\n");
359	}
360	printk("\n");
361}
362
363
364/*
365 * Function used to transmit of frame, very last one on the path before
366 * going to the simulator.
367 */
368static int
369simeth_tx(struct sk_buff *skb, struct net_device *dev)
370{
371	struct simeth_local *local = netdev_priv(dev);
372
373#if 0
374	/* ensure we have at least ETH_ZLEN bytes (min frame size) */
375	unsigned int length = ETH_ZLEN < skb->len ? skb->len : ETH_ZLEN;
376	/* Where do the extra padding bytes comes from inthe skbuff ? */
377#else
378	/* the real driver in the host system is going to take care of that
379	 * or maybe it's the NIC itself.
380	 */
381	unsigned int length = skb->len;
382#endif
383
384	local->stats.tx_bytes += skb->len;
385	local->stats.tx_packets++;
386
387
388	if (simeth_debug > 5) frame_print("simeth_tx", skb->data, length);
389
390	netdev_send(local->simfd, skb->data, length);
391
392	/*
393	 * we are synchronous on write, so we don't simulate a
394	 * trasnmit complete interrupt, thus we don't need to arm a tx
395	 */
396
397	dev_kfree_skb(skb);
398	return NETDEV_TX_OK;
399}
400
401static inline struct sk_buff *
402make_new_skb(struct net_device *dev)
403{
404	struct sk_buff *nskb;
405
406	/*
407	 * The +2 is used to make sure that the IP header is nicely
408	 * aligned (on 4byte boundary I assume 14+2=16)
409	 */
410	nskb = dev_alloc_skb(SIMETH_FRAME_SIZE + 2);
411	if ( nskb == NULL ) {
412		printk(KERN_NOTICE "%s: memory squeeze. dropping packet.\n", dev->name);
413		return NULL;
414	}
415
416	skb_reserve(nskb, 2);	/* Align IP on 16 byte boundaries */
417
418	skb_put(nskb,SIMETH_FRAME_SIZE);
419
420	return nskb;
421}
422
423/*
424 * called from interrupt handler to process a received frame
425 */
426static int
427simeth_rx(struct net_device *dev)
428{
429	struct simeth_local	*local;
430	struct sk_buff		*skb;
431	int			len;
432	int			rcv_count = SIMETH_RECV_MAX;
433
434	local = netdev_priv(dev);
435	/*
436	 * the loop concept has been borrowed from other drivers
437	 * looks to me like it's a throttling thing to avoid pushing to many
438	 * packets at one time into the stack. Making sure we can process them
439	 * upstream and make forward progress overall
440	 */
441	do {
442		if ( (skb=make_new_skb(dev)) == NULL ) {
443			printk(KERN_NOTICE "%s: memory squeeze. dropping packet.\n", dev->name);
444			local->stats.rx_dropped++;
445			return 0;
446		}
447		/*
448		 * Read only one frame at a time
449		 */
450		len = netdev_read(local->simfd, skb->data, SIMETH_FRAME_SIZE);
451		if ( len == 0 ) {
452			if ( simeth_debug > 0 ) printk(KERN_WARNING "%s: count=%d netdev_read=0\n",
453						       dev->name, SIMETH_RECV_MAX-rcv_count);
454			break;
455		}
456#if 0
457		/*
458		 * XXX Fix me
459		 * Should really do a csum+copy here
460		 */
461		skb_copy_to_linear_data(skb, frame, len);
462#endif
463		skb->protocol = eth_type_trans(skb, dev);
464
465		if ( simeth_debug > 6 ) frame_print("simeth_rx", skb->data, len);
466
467		/*
468		 * push the packet up & trigger software interrupt
469		 */
470		netif_rx(skb);
471
472		local->stats.rx_packets++;
473		local->stats.rx_bytes += len;
474
475	} while ( --rcv_count );
476
477	return len; /* 0 = nothing left to read, otherwise, we can try again */
478}
479
480/*
481 * Interrupt handler (Yes, we can do it too !!!)
482 */
483static irqreturn_t
484simeth_interrupt(int irq, void *dev_id)
485{
486	struct net_device *dev = dev_id;
487
488	/*
489	 * very simple loop because we get interrupts only when receiving
490	 */
491	while (simeth_rx(dev));
492	return IRQ_HANDLED;
493}
494
495static struct net_device_stats *
496simeth_get_stats(struct net_device *dev)
497{
498	struct simeth_local *local = netdev_priv(dev);
499
500	return &local->stats;
501}
502
503/* fake multicast ability */
504static void
505set_multicast_list(struct net_device *dev)
506{
507	printk(KERN_WARNING "%s: set_multicast_list called\n", dev->name);
508}
509
510__initcall(simeth_probe);