• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6/arch/ia64/hp/sim/
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/system.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_connect(int irq)
133{
134	ia64_ssc_connect_irq(NETWORK_INTR, irq);
135	return 0;
136}
137
138static inline int
139netdev_attach(int fd, int irq, unsigned int ipaddr)
140{
141	/* this puts the host interface in the right mode (start interrupting) */
142	return ia64_ssc(fd, ipaddr, 0,0, SSC_NETDEV_ATTACH);
143}
144
145
146static inline int
147netdev_detach(int fd)
148{
149	/*
150	 * inactivate the host interface (don't interrupt anymore) */
151	return ia64_ssc(fd, 0,0,0, SSC_NETDEV_DETACH);
152}
153
154static inline int
155netdev_send(int fd, unsigned char *buf, unsigned int len)
156{
157	return ia64_ssc(fd, __pa(buf), len, 0, SSC_NETDEV_SEND);
158}
159
160static inline int
161netdev_read(int fd, unsigned char *buf, unsigned int len)
162{
163	return ia64_ssc(fd, __pa(buf), len, 0, SSC_NETDEV_RECV);
164}
165
166static const struct net_device_ops simeth_netdev_ops = {
167	.ndo_open		= simeth_open,
168	.ndo_stop		= simeth_close,
169	.ndo_start_xmit		= simeth_tx,
170	.ndo_get_stats		= simeth_get_stats,
171	.ndo_set_multicast_list	= set_multicast_list, /* not yet used */
172
173};
174
175/*
176 * Function shared with module code, so cannot be in init section
177 *
178 * So far this function "detects" only one card (test_&_set) but could
179 * be extended easily.
180 *
181 * Return:
182 * 	- -ENODEV is no device found
183 *	- -ENOMEM is no more memory
184 *	- 0 otherwise
185 */
186static int
187simeth_probe1(void)
188{
189	unsigned char mac_addr[ETH_ALEN];
190	struct simeth_local *local;
191	struct net_device *dev;
192	int fd, i, err, rc;
193
194	if (test_and_set_bit(0, &card_count))
195		return -ENODEV;
196
197	/*
198	 * check with the simulator for the device
199	 */
200	fd = netdev_probe(simeth_device, mac_addr);
201	if (fd == -1)
202		return -ENODEV;
203
204	dev = alloc_etherdev(sizeof(struct simeth_local));
205	if (!dev)
206		return -ENOMEM;
207
208	memcpy(dev->dev_addr, mac_addr, sizeof(mac_addr));
209
210	local = netdev_priv(dev);
211	local->simfd = fd; /* keep track of underlying file descriptor */
212
213	dev->netdev_ops = &simeth_netdev_ops;
214
215	err = register_netdev(dev);
216	if (err) {
217		free_netdev(dev);
218		return err;
219	}
220
221	if ((rc = assign_irq_vector(AUTO_ASSIGN)) < 0)
222		panic("%s: out of interrupt vectors!\n", __func__);
223	dev->irq = rc;
224
225	/*
226	 * attach the interrupt in the simulator, this does enable interrupts
227	 * until a netdev_attach() is called
228	 */
229	netdev_connect(dev->irq);
230
231	printk(KERN_INFO "%s: hosteth=%s simfd=%d, HwAddr",
232	       dev->name, simeth_device, local->simfd);
233	for(i = 0; i < ETH_ALEN; i++) {
234		printk(" %2.2x", dev->dev_addr[i]);
235	}
236	printk(", IRQ %d\n", dev->irq);
237
238	return 0;
239}
240
241/*
242 * actually binds the device to an interrupt vector
243 */
244static int
245simeth_open(struct net_device *dev)
246{
247	if (request_irq(dev->irq, simeth_interrupt, 0, "simeth", dev)) {
248		printk(KERN_WARNING "simeth: unable to get IRQ %d.\n", dev->irq);
249		return -EAGAIN;
250	}
251
252	netif_start_queue(dev);
253
254	return 0;
255}
256
257/* copied from lapbether.c */
258static __inline__ int dev_is_ethdev(struct net_device *dev)
259{
260       return ( dev->type == ARPHRD_ETHER && strncmp(dev->name, "dummy", 5));
261}
262
263
264/*
265 * Handler for IFF_UP or IFF_DOWN
266 *
267 * The reason for that is that we don't want to be interrupted when the
268 * interface is down. There is no way to unconnect in the simualtor. Instead
269 * we use this function to shutdown packet processing in the frame filter
270 * in the simulator. Thus no interrupts are generated
271 *
272 *
273 * That's also the place where we pass the IP address of this device to the
274 * simulator so that that we can start filtering packets for it
275 *
276 * There may be a better way of doing this, but I don't know which yet.
277 */
278static int
279simeth_device_event(struct notifier_block *this,unsigned long event, void *ptr)
280{
281	struct net_device *dev = ptr;
282	struct simeth_local *local;
283	struct in_device *in_dev;
284	struct in_ifaddr **ifap = NULL;
285	struct in_ifaddr *ifa = NULL;
286	int r;
287
288
289	if ( ! dev ) {
290		printk(KERN_WARNING "simeth_device_event dev=0\n");
291		return NOTIFY_DONE;
292	}
293
294	if (dev_net(dev) != &init_net)
295		return NOTIFY_DONE;
296
297	if ( event != NETDEV_UP && event != NETDEV_DOWN ) return NOTIFY_DONE;
298
299	if ( !dev_is_ethdev(dev) ) return NOTIFY_DONE;
300
301	if ((in_dev=dev->ip_ptr) != NULL) {
302		for (ifap=&in_dev->ifa_list; (ifa=*ifap) != NULL; ifap=&ifa->ifa_next)
303			if (strcmp(dev->name, ifa->ifa_label) == 0) break;
304	}
305	if ( ifa == NULL ) {
306		printk(KERN_ERR "simeth_open: can't find device %s's ifa\n", dev->name);
307		return NOTIFY_DONE;
308	}
309
310	printk(KERN_INFO "simeth_device_event: %s ipaddr=0x%x\n",
311	       dev->name, ntohl(ifa->ifa_local));
312
313
314	local = netdev_priv(dev);
315	/* now do it for real */
316	r = event == NETDEV_UP ?
317		netdev_attach(local->simfd, dev->irq, ntohl(ifa->ifa_local)):
318		netdev_detach(local->simfd);
319
320	printk(KERN_INFO "simeth: netdev_attach/detach: event=%s ->%d\n",
321	       event == NETDEV_UP ? "attach":"detach", r);
322
323	return NOTIFY_DONE;
324}
325
326static int
327simeth_close(struct net_device *dev)
328{
329	netif_stop_queue(dev);
330
331	free_irq(dev->irq, dev);
332
333	return 0;
334}
335
336/*
337 * Only used for debug
338 */
339static void
340frame_print(unsigned char *from, unsigned char *frame, int len)
341{
342	int i;
343
344	printk("%s: (%d) %02x", from, len, frame[0] & 0xff);
345	for(i=1; i < 6; i++ ) {
346		printk(":%02x", frame[i] &0xff);
347	}
348	printk(" %2x", frame[6] &0xff);
349	for(i=7; i < 12; i++ ) {
350		printk(":%02x", frame[i] &0xff);
351	}
352	printk(" [%02x%02x]\n", frame[12], frame[13]);
353
354	for(i=14; i < len; i++ ) {
355		printk("%02x ", frame[i] &0xff);
356		if ( (i%10)==0) printk("\n");
357	}
358	printk("\n");
359}
360
361
362/*
363 * Function used to transmit of frame, very last one on the path before
364 * going to the simulator.
365 */
366static int
367simeth_tx(struct sk_buff *skb, struct net_device *dev)
368{
369	struct simeth_local *local = netdev_priv(dev);
370
371	/* the real driver in the host system is going to take care of that
372	 * or maybe it's the NIC itself.
373	 */
374	unsigned int length = skb->len;
375
376	local->stats.tx_bytes += skb->len;
377	local->stats.tx_packets++;
378
379
380	if (simeth_debug > 5) frame_print("simeth_tx", skb->data, length);
381
382	netdev_send(local->simfd, skb->data, length);
383
384	/*
385	 * we are synchronous on write, so we don't simulate a
386	 * trasnmit complete interrupt, thus we don't need to arm a tx
387	 */
388
389	dev_kfree_skb(skb);
390	return NETDEV_TX_OK;
391}
392
393static inline struct sk_buff *
394make_new_skb(struct net_device *dev)
395{
396	struct sk_buff *nskb;
397
398	/*
399	 * The +2 is used to make sure that the IP header is nicely
400	 * aligned (on 4byte boundary I assume 14+2=16)
401	 */
402	nskb = dev_alloc_skb(SIMETH_FRAME_SIZE + 2);
403	if ( nskb == NULL ) {
404		printk(KERN_NOTICE "%s: memory squeeze. dropping packet.\n", dev->name);
405		return NULL;
406	}
407
408	skb_reserve(nskb, 2);	/* Align IP on 16 byte boundaries */
409
410	skb_put(nskb,SIMETH_FRAME_SIZE);
411
412	return nskb;
413}
414
415/*
416 * called from interrupt handler to process a received frame
417 */
418static int
419simeth_rx(struct net_device *dev)
420{
421	struct simeth_local	*local;
422	struct sk_buff		*skb;
423	int			len;
424	int			rcv_count = SIMETH_RECV_MAX;
425
426	local = netdev_priv(dev);
427	/*
428	 * the loop concept has been borrowed from other drivers
429	 * looks to me like it's a throttling thing to avoid pushing to many
430	 * packets at one time into the stack. Making sure we can process them
431	 * upstream and make forward progress overall
432	 */
433	do {
434		if ( (skb=make_new_skb(dev)) == NULL ) {
435			printk(KERN_NOTICE "%s: memory squeeze. dropping packet.\n", dev->name);
436			local->stats.rx_dropped++;
437			return 0;
438		}
439		/*
440		 * Read only one frame at a time
441		 */
442		len = netdev_read(local->simfd, skb->data, SIMETH_FRAME_SIZE);
443		if ( len == 0 ) {
444			if ( simeth_debug > 0 ) printk(KERN_WARNING "%s: count=%d netdev_read=0\n",
445						       dev->name, SIMETH_RECV_MAX-rcv_count);
446			break;
447		}
448		skb->protocol = eth_type_trans(skb, dev);
449
450		if ( simeth_debug > 6 ) frame_print("simeth_rx", skb->data, len);
451
452		/*
453		 * push the packet up & trigger software interrupt
454		 */
455		netif_rx(skb);
456
457		local->stats.rx_packets++;
458		local->stats.rx_bytes += len;
459
460	} while ( --rcv_count );
461
462	return len; /* 0 = nothing left to read, otherwise, we can try again */
463}
464
465/*
466 * Interrupt handler (Yes, we can do it too !!!)
467 */
468static irqreturn_t
469simeth_interrupt(int irq, void *dev_id)
470{
471	struct net_device *dev = dev_id;
472
473	/*
474	 * very simple loop because we get interrupts only when receiving
475	 */
476	while (simeth_rx(dev));
477	return IRQ_HANDLED;
478}
479
480static struct net_device_stats *
481simeth_get_stats(struct net_device *dev)
482{
483	struct simeth_local *local = netdev_priv(dev);
484
485	return &local->stats;
486}
487
488/* fake multicast ability */
489static void
490set_multicast_list(struct net_device *dev)
491{
492	printk(KERN_WARNING "%s: set_multicast_list called\n", dev->name);
493}
494
495__initcall(simeth_probe);
496