• 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/xtensa/platforms/iss/
1/*
2 *
3 * arch/xtensa/platforms/iss/network.c
4 *
5 * Platform specific initialization.
6 *
7 * Authors: Chris Zankel <chris@zankel.net>
8 * Based on work form the UML team.
9 *
10 * Copyright 2005 Tensilica Inc.
11 *
12 * This program is free software; you can redistribute  it and/or modify it
13 * under  the terms of  the GNU General  Public License as published by the
14 * Free Software Foundation;  either version 2 of the  License, or (at your
15 * option) any later version.
16 *
17 */
18
19#include <linux/list.h>
20#include <linux/irq.h>
21#include <linux/spinlock.h>
22#include <linux/slab.h>
23#include <linux/timer.h>
24#include <linux/if_ether.h>
25#include <linux/inetdevice.h>
26#include <linux/init.h>
27#include <linux/if_tun.h>
28#include <linux/etherdevice.h>
29#include <linux/interrupt.h>
30#include <linux/ioctl.h>
31#include <linux/bootmem.h>
32#include <linux/ethtool.h>
33#include <linux/rtnetlink.h>
34#include <linux/platform_device.h>
35
36#include <platform/simcall.h>
37
38#define DRIVER_NAME "iss-netdev"
39#define ETH_MAX_PACKET 1500
40#define ETH_HEADER_OTHER 14
41#define ISS_NET_TIMER_VALUE (2 * HZ)
42
43
44static DEFINE_SPINLOCK(opened_lock);
45static LIST_HEAD(opened);
46
47static DEFINE_SPINLOCK(devices_lock);
48static LIST_HEAD(devices);
49
50/* ------------------------------------------------------------------------- */
51
52/* We currently only support the TUNTAP transport protocol. */
53
54#define TRANSPORT_TUNTAP_NAME "tuntap"
55#define TRANSPORT_TUNTAP_MTU ETH_MAX_PACKET
56
57struct tuntap_info {
58	char dev_name[IFNAMSIZ];
59	int fixed_config;
60	unsigned char gw[ETH_ALEN];
61	int fd;
62};
63
64/* ------------------------------------------------------------------------- */
65
66
67/* This structure contains out private information for the driver. */
68
69struct iss_net_private {
70
71	struct list_head device_list;
72	struct list_head opened_list;
73
74	spinlock_t lock;
75	struct net_device *dev;
76	struct platform_device pdev;
77	struct timer_list tl;
78	struct net_device_stats stats;
79
80	struct timer_list timer;
81	unsigned int timer_val;
82
83	int index;
84	int mtu;
85
86	unsigned char mac[ETH_ALEN];
87	int have_mac;
88
89	struct {
90		union {
91			struct tuntap_info tuntap;
92		} info;
93
94		int (*open)(struct iss_net_private *lp);
95		void (*close)(struct iss_net_private *lp);
96		int (*read)(struct iss_net_private *lp, struct sk_buff **skb);
97		int (*write)(struct iss_net_private *lp, struct sk_buff **skb);
98		unsigned short (*protocol)(struct sk_buff *skb);
99		int (*poll)(struct iss_net_private *lp);
100	} tp;
101
102};
103
104/* ======================= ISS SIMCALL INTERFACE =========================== */
105
106/* Note: __simc must _not_ be declared inline! */
107
108static int errno;
109
110static int __simc (int a, int b, int c, int d, int e, int f) __attribute__((__noinline__));
111static int __simc (int a, int b, int c, int d, int e, int f)
112{
113	int ret;
114	__asm__ __volatile__ ("simcall\n"
115	    		      "mov %0, a2\n"
116			      "mov %1, a3\n" : "=a" (ret), "=a" (errno)
117			      : : "a2", "a3");
118	return ret;
119}
120
121static int inline simc_open(char *file, int flags, int mode)
122{
123	return __simc(SYS_open, (int) file, flags, mode, 0, 0);
124}
125
126static int inline simc_close(int fd)
127{
128	return __simc(SYS_close, fd, 0, 0, 0, 0);
129}
130
131static int inline simc_ioctl(int fd, int request, void *arg)
132{
133	return __simc(SYS_ioctl, fd, request, (int) arg, 0, 0);
134}
135
136static int inline simc_read(int fd, void *buf, size_t count)
137{
138	return __simc(SYS_read, fd, (int) buf, count, 0, 0);
139}
140
141static int inline simc_write(int fd, void *buf, size_t count)
142{
143	return __simc(SYS_write, fd, (int) buf, count, 0, 0);
144}
145
146static int inline simc_poll(int fd)
147{
148	struct timeval tv = { .tv_sec = 0, .tv_usec = 0 };
149
150	return __simc(SYS_select_one, fd, XTISS_SELECT_ONE_READ, (int)&tv,0,0);
151}
152
153/* ================================ HELPERS ================================ */
154
155
156static char *split_if_spec(char *str, ...)
157{
158	char **arg, *end;
159	va_list ap;
160
161	va_start(ap, str);
162	while ((arg = va_arg(ap, char**)) != NULL) {
163		if (*str == '\0')
164			return NULL;
165		end = strchr(str, ',');
166		if (end != str)
167			*arg = str;
168		if (end == NULL)
169			return NULL;
170		*end ++ = '\0';
171		str = end;
172	}
173	va_end(ap);
174	return str;
175}
176
177
178
179/* Return the IP address as a string for a given device. */
180
181static void dev_ip_addr(void *d, char *buf, char *bin_buf)
182{
183	struct net_device *dev = d;
184	struct in_device *ip = dev->ip_ptr;
185	struct in_ifaddr *in;
186	__be32 addr;
187
188	if ((ip == NULL) || ((in = ip->ifa_list) == NULL)) {
189		printk(KERN_WARNING "Device not assigned an IP address!\n");
190		return;
191	}
192
193	addr = in->ifa_address;
194	sprintf(buf, "%d.%d.%d.%d", addr & 0xff, (addr >> 8) & 0xff,
195		(addr >> 16) & 0xff, addr >> 24);
196
197	if (bin_buf) {
198		bin_buf[0] = addr & 0xff;
199		bin_buf[1] = (addr >> 8) & 0xff;
200		bin_buf[2] = (addr >> 16) & 0xff;
201		bin_buf[3] = addr >> 24;
202	}
203}
204
205/* Set Ethernet address of the specified device. */
206
207static void inline set_ether_mac(void *d, unsigned char *addr)
208{
209	struct net_device *dev = d;
210	memcpy(dev->dev_addr, addr, ETH_ALEN);
211}
212
213
214/* ======================= TUNTAP TRANSPORT INTERFACE ====================== */
215
216static int tuntap_open(struct iss_net_private *lp)
217{
218	struct ifreq ifr;
219	char *dev_name = lp->tp.info.tuntap.dev_name;
220	int err = -EINVAL;
221	int fd;
222
223	/* We currently only support a fixed configuration. */
224
225	if (!lp->tp.info.tuntap.fixed_config)
226		return -EINVAL;
227
228	if ((fd = simc_open("/dev/net/tun", 02, 0)) < 0) {	/* O_RDWR */
229		printk("Failed to open /dev/net/tun, returned %d "
230		       "(errno = %d)\n", fd, errno);
231		return fd;
232	}
233
234	memset(&ifr, 0, sizeof ifr);
235	ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
236	strlcpy(ifr.ifr_name, dev_name, sizeof ifr.ifr_name);
237
238	if ((err = simc_ioctl(fd, TUNSETIFF, (void*) &ifr)) < 0) {
239		printk("Failed to set interface, returned %d "
240		       "(errno = %d)\n", err, errno);
241		simc_close(fd);
242		return err;
243	}
244
245	lp->tp.info.tuntap.fd = fd;
246	return err;
247}
248
249static void tuntap_close(struct iss_net_private *lp)
250{
251	simc_close(lp->tp.info.tuntap.fd);
252	lp->tp.info.tuntap.fd = -1;
253}
254
255static int tuntap_read (struct iss_net_private *lp, struct sk_buff **skb)
256{
257
258	return simc_read(lp->tp.info.tuntap.fd,
259			(*skb)->data, (*skb)->dev->mtu + ETH_HEADER_OTHER);
260}
261
262static int tuntap_write (struct iss_net_private *lp, struct sk_buff **skb)
263{
264	return simc_write(lp->tp.info.tuntap.fd, (*skb)->data, (*skb)->len);
265}
266
267unsigned short tuntap_protocol(struct sk_buff *skb)
268{
269	return eth_type_trans(skb, skb->dev);
270}
271
272static int tuntap_poll(struct iss_net_private *lp)
273{
274	return simc_poll(lp->tp.info.tuntap.fd);
275}
276
277/*
278 * Currently only a device name is supported.
279 * ethX=tuntap[,[mac address][,[device name]]]
280 */
281
282static int tuntap_probe(struct iss_net_private *lp, int index, char *init)
283{
284	const int len = strlen(TRANSPORT_TUNTAP_NAME);
285	char *dev_name = NULL, *mac_str = NULL, *rem = NULL;
286
287	/* Transport should be 'tuntap': ethX=tuntap,mac,dev_name */
288
289	if (strncmp(init, TRANSPORT_TUNTAP_NAME, len))
290		return 0;
291
292	if (*(init += strlen(TRANSPORT_TUNTAP_NAME)) == ',') {
293		if ((rem=split_if_spec(init+1, &mac_str, &dev_name)) != NULL) {
294			printk("Extra garbage on specification : '%s'\n", rem);
295			return 0;
296		}
297	} else if (*init != '\0') {
298		printk("Invalid argument: %s. Skipping device!\n", init);
299		return 0;
300	}
301
302	if (dev_name) {
303		strncpy(lp->tp.info.tuntap.dev_name, dev_name,
304			 sizeof lp->tp.info.tuntap.dev_name);
305		lp->tp.info.tuntap.fixed_config = 1;
306	} else
307		strcpy(lp->tp.info.tuntap.dev_name, TRANSPORT_TUNTAP_NAME);
308
309
310	lp->mtu = TRANSPORT_TUNTAP_MTU;
311
312	//lp->info.tuntap.gate_addr = gate_addr;
313
314	lp->tp.info.tuntap.fd = -1;
315
316	lp->tp.open = tuntap_open;
317	lp->tp.close = tuntap_close;
318	lp->tp.read = tuntap_read;
319	lp->tp.write = tuntap_write;
320	lp->tp.protocol = tuntap_protocol;
321	lp->tp.poll = tuntap_poll;
322
323	printk("TUN/TAP backend - ");
324	printk("\n");
325
326	return 1;
327}
328
329/* ================================ ISS NET ================================ */
330
331static int iss_net_rx(struct net_device *dev)
332{
333	struct iss_net_private *lp = netdev_priv(dev);
334	int pkt_len;
335	struct sk_buff *skb;
336
337	/* Check if there is any new data. */
338
339	if (lp->tp.poll(lp) == 0)
340		return 0;
341
342	/* Try to allocate memory, if it fails, try again next round. */
343
344	if ((skb = dev_alloc_skb(dev->mtu + 2 + ETH_HEADER_OTHER)) == NULL) {
345		lp->stats.rx_dropped++;
346		return 0;
347	}
348
349	skb_reserve(skb, 2);
350
351	/* Setup skb */
352
353	skb->dev = dev;
354	skb_reset_mac_header(skb);
355	pkt_len = lp->tp.read(lp, &skb);
356	skb_put(skb, pkt_len);
357
358	if (pkt_len > 0) {
359		skb_trim(skb, pkt_len);
360		skb->protocol = lp->tp.protocol(skb);
361
362		lp->stats.rx_bytes += skb->len;
363		lp->stats.rx_packets++;
364	//	netif_rx(skb);
365		netif_rx_ni(skb);
366		return pkt_len;
367	}
368	kfree_skb(skb);
369	return pkt_len;
370}
371
372static int iss_net_poll(void)
373{
374	struct list_head *ele;
375	int err, ret = 0;
376
377	spin_lock(&opened_lock);
378
379	list_for_each(ele, &opened) {
380		struct iss_net_private *lp;
381
382		lp = list_entry(ele, struct iss_net_private, opened_list);
383
384		if (!netif_running(lp->dev))
385			break;
386
387		spin_lock(&lp->lock);
388
389		while ((err = iss_net_rx(lp->dev)) > 0)
390			ret++;
391
392		spin_unlock(&lp->lock);
393
394		if (err < 0) {
395			printk(KERN_ERR "Device '%s' read returned %d, "
396			       "shutting it down\n", lp->dev->name, err);
397			dev_close(lp->dev);
398		} else {
399		}
400	}
401
402	spin_unlock(&opened_lock);
403	return ret;
404}
405
406
407static void iss_net_timer(unsigned long priv)
408{
409	struct iss_net_private* lp = (struct iss_net_private*) priv;
410
411	spin_lock(&lp->lock);
412
413	iss_net_poll();
414
415	mod_timer(&lp->timer, jiffies + lp->timer_val);
416
417	spin_unlock(&lp->lock);
418}
419
420
421static int iss_net_open(struct net_device *dev)
422{
423	struct iss_net_private *lp = netdev_priv(dev);
424	char addr[sizeof "255.255.255.255\0"];
425	int err;
426
427	spin_lock(&lp->lock);
428
429	if ((err = lp->tp.open(lp)) < 0)
430		goto out;
431
432	if (!lp->have_mac) {
433		dev_ip_addr(dev, addr, &lp->mac[2]);
434		set_ether_mac(dev, lp->mac);
435	}
436
437	netif_start_queue(dev);
438
439	/* clear buffer - it can happen that the host side of the interface
440	 * is full when we get here. In this case, new data is never queued,
441	 * SIGIOs never arrive, and the net never works.
442	 */
443	while ((err = iss_net_rx(dev)) > 0)
444		;
445
446	spin_lock(&opened_lock);
447	list_add(&lp->opened_list, &opened);
448	spin_unlock(&opened_lock);
449
450	init_timer(&lp->timer);
451	lp->timer_val = ISS_NET_TIMER_VALUE;
452	lp->timer.data = (unsigned long) lp;
453	lp->timer.function = iss_net_timer;
454	mod_timer(&lp->timer, jiffies + lp->timer_val);
455
456out:
457	spin_unlock(&lp->lock);
458	return err;
459}
460
461static int iss_net_close(struct net_device *dev)
462{
463	struct iss_net_private *lp = netdev_priv(dev);
464printk("iss_net_close!\n");
465	netif_stop_queue(dev);
466	spin_lock(&lp->lock);
467
468	spin_lock(&opened_lock);
469	list_del(&opened);
470	spin_unlock(&opened_lock);
471
472	del_timer_sync(&lp->timer);
473
474	lp->tp.close(lp);
475
476	spin_unlock(&lp->lock);
477	return 0;
478}
479
480static int iss_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
481{
482	struct iss_net_private *lp = netdev_priv(dev);
483	unsigned long flags;
484	int len;
485
486	netif_stop_queue(dev);
487	spin_lock_irqsave(&lp->lock, flags);
488
489	len = lp->tp.write(lp, &skb);
490
491	if (len == skb->len) {
492		lp->stats.tx_packets++;
493		lp->stats.tx_bytes += skb->len;
494		dev->trans_start = jiffies;
495		netif_start_queue(dev);
496
497		/* this is normally done in the interrupt when tx finishes */
498		netif_wake_queue(dev);
499
500	} else if (len == 0) {
501		netif_start_queue(dev);
502		lp->stats.tx_dropped++;
503
504	} else {
505		netif_start_queue(dev);
506		printk(KERN_ERR "iss_net_start_xmit: failed(%d)\n", len);
507	}
508
509	spin_unlock_irqrestore(&lp->lock, flags);
510
511	dev_kfree_skb(skb);
512	return NETDEV_TX_OK;
513}
514
515
516static struct net_device_stats *iss_net_get_stats(struct net_device *dev)
517{
518	struct iss_net_private *lp = netdev_priv(dev);
519	return &lp->stats;
520}
521
522static void iss_net_set_multicast_list(struct net_device *dev)
523{
524}
525
526static void iss_net_tx_timeout(struct net_device *dev)
527{
528}
529
530static int iss_net_set_mac(struct net_device *dev, void *addr)
531{
532
533	return 0;
534}
535
536static int iss_net_change_mtu(struct net_device *dev, int new_mtu)
537{
538	return -EINVAL;
539}
540
541void iss_net_user_timer_expire(unsigned long _conn)
542{
543}
544
545
546static struct platform_driver iss_net_driver = {
547	.driver = {
548		.name  = DRIVER_NAME,
549	},
550};
551
552static int driver_registered;
553
554static const struct net_device_ops iss_netdev_ops = {
555	.ndo_open		= iss_net_open,
556	.ndo_stop		= iss_net_close,
557	.ndo_get_stats		= iss_net_get_stats,
558	.ndo_start_xmit		= iss_net_start_xmit,
559	.ndo_validate_addr	= eth_validate_addr,
560	.ndo_change_mtu		= iss_net_change_mtu,
561	.ndo_set_mac_address	= iss_net_set_mac,
562	//.ndo_do_ioctl		= iss_net_ioctl,
563	.ndo_tx_timeout		= iss_net_tx_timeout,
564	.ndo_set_multicast_list = iss_net_set_multicast_list,
565};
566
567static int iss_net_configure(int index, char *init)
568{
569	struct net_device *dev;
570	struct iss_net_private *lp;
571	int err;
572
573	if ((dev = alloc_etherdev(sizeof *lp)) == NULL) {
574		printk(KERN_ERR "eth_configure: failed to allocate device\n");
575		return 1;
576	}
577
578	/* Initialize private element. */
579
580	lp = netdev_priv(dev);
581	*lp = ((struct iss_net_private) {
582		.device_list		= LIST_HEAD_INIT(lp->device_list),
583		.opened_list		= LIST_HEAD_INIT(lp->opened_list),
584		.lock			= __SPIN_LOCK_UNLOCKED(lp.lock),
585		.dev			= dev,
586		.index			= index,
587		//.fd                   = -1,
588		.mac			= { 0xfe, 0xfd, 0x0, 0x0, 0x0, 0x0 },
589		.have_mac		= 0,
590		});
591
592	/*
593	 * Try all transport protocols.
594	 * Note: more protocols can be added by adding '&& !X_init(lp, eth)'.
595	 */
596
597	if (!tuntap_probe(lp, index, init)) {
598		printk("Invalid arguments. Skipping device!\n");
599		goto errout;
600	}
601
602	printk(KERN_INFO "Netdevice %d ", index);
603	if (lp->have_mac)
604		printk("(%pM) ", lp->mac);
605	printk(": ");
606
607	/* sysfs register */
608
609	if (!driver_registered) {
610		platform_driver_register(&iss_net_driver);
611		driver_registered = 1;
612	}
613
614	spin_lock(&devices_lock);
615	list_add(&lp->device_list, &devices);
616	spin_unlock(&devices_lock);
617
618	lp->pdev.id = index;
619	lp->pdev.name = DRIVER_NAME;
620	platform_device_register(&lp->pdev);
621	SET_NETDEV_DEV(dev,&lp->pdev.dev);
622
623	/*
624	 * If this name ends up conflicting with an existing registered
625	 * netdevice, that is OK, register_netdev{,ice}() will notice this
626	 * and fail.
627	 */
628	snprintf(dev->name, sizeof dev->name, "eth%d", index);
629
630	dev->netdev_ops = &iss_netdev_ops;
631	dev->mtu = lp->mtu;
632	dev->watchdog_timeo = (HZ >> 1);
633	dev->irq = -1;
634
635	rtnl_lock();
636	err = register_netdevice(dev);
637	rtnl_unlock();
638
639	if (err) {
640		printk("Error registering net device!\n");
641		free_netdev(dev);
642		return 1;
643	}
644
645	init_timer(&lp->tl);
646	lp->tl.function = iss_net_user_timer_expire;
647
648	return 0;
649
650errout:
651	return -EIO;
652
653}
654
655/* ------------------------------------------------------------------------- */
656
657/* Filled in during early boot */
658
659struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
660
661struct iss_net_init {
662	struct list_head list;
663	char *init;		/* init string */
664	int index;
665};
666
667/*
668 * Parse the command line and look for 'ethX=...' fields, and register all
669 * those fields. They will be later initialized in iss_net_init.
670 */
671
672#define ERR KERN_ERR "iss_net_setup: "
673
674static int iss_net_setup(char *str)
675{
676	struct iss_net_private *device = NULL;
677	struct iss_net_init *new;
678	struct list_head *ele;
679	char *end;
680	int n;
681
682	n = simple_strtoul(str, &end, 0);
683	if (end == str) {
684		printk(ERR "Failed to parse '%s'\n", str);
685		return 1;
686	}
687	if (n < 0) {
688		printk(ERR "Device %d is negative\n", n);
689		return 1;
690	}
691	if (*(str = end) != '=') {
692		printk(ERR "Expected '=' after device number\n");
693		return 1;
694	}
695
696	spin_lock(&devices_lock);
697
698	list_for_each(ele, &devices) {
699		device = list_entry(ele, struct iss_net_private, device_list);
700		if (device->index == n)
701			break;
702	}
703
704	spin_unlock(&devices_lock);
705
706	if (device && device->index == n) {
707		printk(ERR "Device %d already configured\n", n);
708		return 1;
709	}
710
711	if ((new = alloc_bootmem(sizeof new)) == NULL) {
712		printk("Alloc_bootmem failed\n");
713		return 1;
714	}
715
716	INIT_LIST_HEAD(&new->list);
717	new->index = n;
718	new->init = str + 1;
719
720	list_add_tail(&new->list, &eth_cmd_line);
721	return 1;
722}
723
724#undef ERR
725
726__setup("eth=", iss_net_setup);
727
728/*
729 * Initialize all ISS Ethernet devices previously registered in iss_net_setup.
730 */
731
732static int iss_net_init(void)
733{
734	struct list_head *ele, *next;
735
736	/* Walk through all Ethernet devices specified in the command line. */
737
738	list_for_each_safe(ele, next, &eth_cmd_line) {
739		struct iss_net_init *eth;
740		eth = list_entry(ele, struct iss_net_init, list);
741		iss_net_configure(eth->index, eth->init);
742	}
743
744	return 1;
745}
746
747module_init(iss_net_init);
748