3 * arch/xtensa/platforms/iss/network.c
5 * Platform specific initialization.
7 * Authors: Chris Zankel <chris@zankel.net>
8 * Based on work form the UML team.
10 * Copyright 2005 Tensilica Inc.
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.
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>
36 #include <platform/simcall.h>
38 #define DRIVER_NAME "iss-netdev"
39 #define ETH_MAX_PACKET 1500
40 #define ETH_HEADER_OTHER 14
41 #define ISS_NET_TIMER_VALUE (HZ / 10)
44 static DEFINE_SPINLOCK(opened_lock
);
45 static LIST_HEAD(opened
);
47 static DEFINE_SPINLOCK(devices_lock
);
48 static LIST_HEAD(devices
);
50 /* ------------------------------------------------------------------------- */
52 /* We currently only support the TUNTAP transport protocol. */
54 #define TRANSPORT_TUNTAP_NAME "tuntap"
55 #define TRANSPORT_TUNTAP_MTU ETH_MAX_PACKET
58 char dev_name
[IFNAMSIZ
];
62 /* ------------------------------------------------------------------------- */
65 /* This structure contains out private information for the driver. */
67 struct iss_net_private
{
68 struct list_head device_list
;
69 struct list_head opened_list
;
72 struct net_device
*dev
;
73 struct platform_device pdev
;
75 struct net_device_stats stats
;
77 struct timer_list timer
;
78 unsigned int timer_val
;
85 struct tuntap_info tuntap
;
88 int (*open
)(struct iss_net_private
*lp
);
89 void (*close
)(struct iss_net_private
*lp
);
90 int (*read
)(struct iss_net_private
*lp
, struct sk_buff
**skb
);
91 int (*write
)(struct iss_net_private
*lp
, struct sk_buff
**skb
);
92 unsigned short (*protocol
)(struct sk_buff
*skb
);
93 int (*poll
)(struct iss_net_private
*lp
);
98 /* ================================ HELPERS ================================ */
101 static char *split_if_spec(char *str
, ...)
107 while ((arg
= va_arg(ap
, char**)) != NULL
) {
112 end
= strchr(str
, ',');
126 /* Set Ethernet address of the specified device. */
128 static void setup_etheraddr(struct net_device
*dev
, char *str
)
130 unsigned char *addr
= dev
->dev_addr
;
135 if (!mac_pton(str
, addr
)) {
136 pr_err("%s: failed to parse '%s' as an ethernet address\n",
140 if (is_multicast_ether_addr(addr
)) {
141 pr_err("%s: attempt to assign a multicast ethernet address\n",
145 if (!is_valid_ether_addr(addr
)) {
146 pr_err("%s: attempt to assign an invalid ethernet address\n",
150 if (!is_local_ether_addr(addr
))
151 pr_warn("%s: assigning a globally valid ethernet address\n",
156 pr_info("%s: choosing a random ethernet address\n",
158 eth_hw_addr_random(dev
);
161 /* ======================= TUNTAP TRANSPORT INTERFACE ====================== */
163 static int tuntap_open(struct iss_net_private
*lp
)
166 char *dev_name
= lp
->tp
.info
.tuntap
.dev_name
;
170 fd
= simc_open("/dev/net/tun", 02, 0); /* O_RDWR */
172 pr_err("%s: failed to open /dev/net/tun, returned %d (errno = %d)\n",
173 lp
->dev
->name
, fd
, errno
);
177 memset(&ifr
, 0, sizeof(ifr
));
178 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
179 strlcpy(ifr
.ifr_name
, dev_name
, sizeof(ifr
.ifr_name
));
181 err
= simc_ioctl(fd
, TUNSETIFF
, &ifr
);
183 pr_err("%s: failed to set interface %s, returned %d (errno = %d)\n",
184 lp
->dev
->name
, dev_name
, err
, errno
);
189 lp
->tp
.info
.tuntap
.fd
= fd
;
193 static void tuntap_close(struct iss_net_private
*lp
)
195 simc_close(lp
->tp
.info
.tuntap
.fd
);
196 lp
->tp
.info
.tuntap
.fd
= -1;
199 static int tuntap_read(struct iss_net_private
*lp
, struct sk_buff
**skb
)
201 return simc_read(lp
->tp
.info
.tuntap
.fd
,
202 (*skb
)->data
, (*skb
)->dev
->mtu
+ ETH_HEADER_OTHER
);
205 static int tuntap_write(struct iss_net_private
*lp
, struct sk_buff
**skb
)
207 return simc_write(lp
->tp
.info
.tuntap
.fd
, (*skb
)->data
, (*skb
)->len
);
210 unsigned short tuntap_protocol(struct sk_buff
*skb
)
212 return eth_type_trans(skb
, skb
->dev
);
215 static int tuntap_poll(struct iss_net_private
*lp
)
217 return simc_poll(lp
->tp
.info
.tuntap
.fd
);
221 * ethX=tuntap,[mac address],device name
224 static int tuntap_probe(struct iss_net_private
*lp
, int index
, char *init
)
226 struct net_device
*dev
= lp
->dev
;
227 char *dev_name
= NULL
, *mac_str
= NULL
, *rem
= NULL
;
229 /* Transport should be 'tuntap': ethX=tuntap,mac,dev_name */
231 if (strncmp(init
, TRANSPORT_TUNTAP_NAME
,
232 sizeof(TRANSPORT_TUNTAP_NAME
) - 1))
235 init
+= sizeof(TRANSPORT_TUNTAP_NAME
) - 1;
237 rem
= split_if_spec(init
+ 1, &mac_str
, &dev_name
);
239 pr_err("%s: extra garbage on specification : '%s'\n",
243 } else if (*init
!= '\0') {
244 pr_err("%s: invalid argument: %s. Skipping device!\n",
250 pr_err("%s: missing tuntap device name\n", dev
->name
);
254 strlcpy(lp
->tp
.info
.tuntap
.dev_name
, dev_name
,
255 sizeof(lp
->tp
.info
.tuntap
.dev_name
));
257 setup_etheraddr(dev
, mac_str
);
259 lp
->mtu
= TRANSPORT_TUNTAP_MTU
;
261 lp
->tp
.info
.tuntap
.fd
= -1;
263 lp
->tp
.open
= tuntap_open
;
264 lp
->tp
.close
= tuntap_close
;
265 lp
->tp
.read
= tuntap_read
;
266 lp
->tp
.write
= tuntap_write
;
267 lp
->tp
.protocol
= tuntap_protocol
;
268 lp
->tp
.poll
= tuntap_poll
;
273 /* ================================ ISS NET ================================ */
275 static int iss_net_rx(struct net_device
*dev
)
277 struct iss_net_private
*lp
= netdev_priv(dev
);
281 /* Check if there is any new data. */
283 if (lp
->tp
.poll(lp
) == 0)
286 /* Try to allocate memory, if it fails, try again next round. */
288 skb
= dev_alloc_skb(dev
->mtu
+ 2 + ETH_HEADER_OTHER
);
290 lp
->stats
.rx_dropped
++;
299 skb_reset_mac_header(skb
);
300 pkt_len
= lp
->tp
.read(lp
, &skb
);
301 skb_put(skb
, pkt_len
);
304 skb_trim(skb
, pkt_len
);
305 skb
->protocol
= lp
->tp
.protocol(skb
);
307 lp
->stats
.rx_bytes
+= skb
->len
;
308 lp
->stats
.rx_packets
++;
316 static int iss_net_poll(void)
318 struct list_head
*ele
;
321 spin_lock(&opened_lock
);
323 list_for_each(ele
, &opened
) {
324 struct iss_net_private
*lp
;
326 lp
= list_entry(ele
, struct iss_net_private
, opened_list
);
328 if (!netif_running(lp
->dev
))
331 spin_lock(&lp
->lock
);
333 while ((err
= iss_net_rx(lp
->dev
)) > 0)
336 spin_unlock(&lp
->lock
);
339 pr_err("Device '%s' read returned %d, shutting it down\n",
343 /* FIXME reactivate_fd(lp->fd, ISS_ETH_IRQ); */
347 spin_unlock(&opened_lock
);
352 static void iss_net_timer(unsigned long priv
)
354 struct iss_net_private
*lp
= (struct iss_net_private
*)priv
;
357 spin_lock(&lp
->lock
);
358 mod_timer(&lp
->timer
, jiffies
+ lp
->timer_val
);
359 spin_unlock(&lp
->lock
);
363 static int iss_net_open(struct net_device
*dev
)
365 struct iss_net_private
*lp
= netdev_priv(dev
);
368 spin_lock_bh(&lp
->lock
);
370 err
= lp
->tp
.open(lp
);
374 netif_start_queue(dev
);
376 /* clear buffer - it can happen that the host side of the interface
377 * is full when we get here. In this case, new data is never queued,
378 * SIGIOs never arrive, and the net never works.
380 while ((err
= iss_net_rx(dev
)) > 0)
383 spin_unlock_bh(&lp
->lock
);
384 spin_lock_bh(&opened_lock
);
385 list_add(&lp
->opened_list
, &opened
);
386 spin_unlock_bh(&opened_lock
);
387 spin_lock_bh(&lp
->lock
);
389 init_timer(&lp
->timer
);
390 lp
->timer_val
= ISS_NET_TIMER_VALUE
;
391 lp
->timer
.data
= (unsigned long) lp
;
392 lp
->timer
.function
= iss_net_timer
;
393 mod_timer(&lp
->timer
, jiffies
+ lp
->timer_val
);
396 spin_unlock_bh(&lp
->lock
);
400 static int iss_net_close(struct net_device
*dev
)
402 struct iss_net_private
*lp
= netdev_priv(dev
);
403 netif_stop_queue(dev
);
404 spin_lock_bh(&lp
->lock
);
406 spin_lock(&opened_lock
);
408 spin_unlock(&opened_lock
);
410 del_timer_sync(&lp
->timer
);
414 spin_unlock_bh(&lp
->lock
);
418 static int iss_net_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
420 struct iss_net_private
*lp
= netdev_priv(dev
);
423 netif_stop_queue(dev
);
424 spin_lock_bh(&lp
->lock
);
426 len
= lp
->tp
.write(lp
, &skb
);
428 if (len
== skb
->len
) {
429 lp
->stats
.tx_packets
++;
430 lp
->stats
.tx_bytes
+= skb
->len
;
431 netif_trans_update(dev
);
432 netif_start_queue(dev
);
434 /* this is normally done in the interrupt when tx finishes */
435 netif_wake_queue(dev
);
437 } else if (len
== 0) {
438 netif_start_queue(dev
);
439 lp
->stats
.tx_dropped
++;
442 netif_start_queue(dev
);
443 pr_err("%s: %s failed(%d)\n", dev
->name
, __func__
, len
);
446 spin_unlock_bh(&lp
->lock
);
453 static struct net_device_stats
*iss_net_get_stats(struct net_device
*dev
)
455 struct iss_net_private
*lp
= netdev_priv(dev
);
459 static void iss_net_set_multicast_list(struct net_device
*dev
)
463 static void iss_net_tx_timeout(struct net_device
*dev
)
467 static int iss_net_set_mac(struct net_device
*dev
, void *addr
)
469 struct iss_net_private
*lp
= netdev_priv(dev
);
470 struct sockaddr
*hwaddr
= addr
;
472 if (!is_valid_ether_addr(hwaddr
->sa_data
))
473 return -EADDRNOTAVAIL
;
474 spin_lock_bh(&lp
->lock
);
475 memcpy(dev
->dev_addr
, hwaddr
->sa_data
, ETH_ALEN
);
476 spin_unlock_bh(&lp
->lock
);
480 static int iss_net_change_mtu(struct net_device
*dev
, int new_mtu
)
485 void iss_net_user_timer_expire(unsigned long _conn
)
490 static struct platform_driver iss_net_driver
= {
496 static int driver_registered
;
498 static const struct net_device_ops iss_netdev_ops
= {
499 .ndo_open
= iss_net_open
,
500 .ndo_stop
= iss_net_close
,
501 .ndo_get_stats
= iss_net_get_stats
,
502 .ndo_start_xmit
= iss_net_start_xmit
,
503 .ndo_validate_addr
= eth_validate_addr
,
504 .ndo_change_mtu
= iss_net_change_mtu
,
505 .ndo_set_mac_address
= iss_net_set_mac
,
506 .ndo_tx_timeout
= iss_net_tx_timeout
,
507 .ndo_set_rx_mode
= iss_net_set_multicast_list
,
510 static int iss_net_configure(int index
, char *init
)
512 struct net_device
*dev
;
513 struct iss_net_private
*lp
;
516 dev
= alloc_etherdev(sizeof(*lp
));
518 pr_err("eth_configure: failed to allocate device\n");
522 /* Initialize private element. */
524 lp
= netdev_priv(dev
);
525 *lp
= (struct iss_net_private
) {
526 .device_list
= LIST_HEAD_INIT(lp
->device_list
),
527 .opened_list
= LIST_HEAD_INIT(lp
->opened_list
),
532 spin_lock_init(&lp
->lock
);
534 * If this name ends up conflicting with an existing registered
535 * netdevice, that is OK, register_netdev{,ice}() will notice this
538 snprintf(dev
->name
, sizeof(dev
->name
), "eth%d", index
);
541 * Try all transport protocols.
542 * Note: more protocols can be added by adding '&& !X_init(lp, eth)'.
545 if (!tuntap_probe(lp
, index
, init
)) {
546 pr_err("%s: invalid arguments. Skipping device!\n",
551 pr_info("Netdevice %d (%pM)\n", index
, dev
->dev_addr
);
555 if (!driver_registered
) {
556 platform_driver_register(&iss_net_driver
);
557 driver_registered
= 1;
560 spin_lock(&devices_lock
);
561 list_add(&lp
->device_list
, &devices
);
562 spin_unlock(&devices_lock
);
565 lp
->pdev
.name
= DRIVER_NAME
;
566 platform_device_register(&lp
->pdev
);
567 SET_NETDEV_DEV(dev
, &lp
->pdev
.dev
);
569 dev
->netdev_ops
= &iss_netdev_ops
;
571 dev
->watchdog_timeo
= (HZ
>> 1);
575 err
= register_netdevice(dev
);
579 pr_err("%s: error registering net device!\n", dev
->name
);
580 /* XXX: should we call ->remove() here? */
586 lp
->tl
.function
= iss_net_user_timer_expire
;
591 /* FIXME: unregister; free, etc.. */
595 /* ------------------------------------------------------------------------- */
597 /* Filled in during early boot */
599 struct list_head eth_cmd_line
= LIST_HEAD_INIT(eth_cmd_line
);
601 struct iss_net_init
{
602 struct list_head list
;
603 char *init
; /* init string */
608 * Parse the command line and look for 'ethX=...' fields, and register all
609 * those fields. They will be later initialized in iss_net_init.
612 #define ERR KERN_ERR "iss_net_setup: "
614 static int __init
iss_net_setup(char *str
)
616 struct iss_net_private
*device
= NULL
;
617 struct iss_net_init
*new;
618 struct list_head
*ele
;
623 end
= strchr(str
, '=');
625 printk(ERR
"Expected '=' after device number\n");
629 rc
= kstrtouint(str
, 0, &n
);
632 printk(ERR
"Failed to parse '%s'\n", str
);
637 spin_lock(&devices_lock
);
639 list_for_each(ele
, &devices
) {
640 device
= list_entry(ele
, struct iss_net_private
, device_list
);
641 if (device
->index
== n
)
645 spin_unlock(&devices_lock
);
647 if (device
&& device
->index
== n
) {
648 printk(ERR
"Device %u already configured\n", n
);
652 new = alloc_bootmem(sizeof(*new));
654 printk(ERR
"Alloc_bootmem failed\n");
658 INIT_LIST_HEAD(&new->list
);
662 list_add_tail(&new->list
, ð_cmd_line
);
668 __setup("eth", iss_net_setup
);
671 * Initialize all ISS Ethernet devices previously registered in iss_net_setup.
674 static int iss_net_init(void)
676 struct list_head
*ele
, *next
;
678 /* Walk through all Ethernet devices specified in the command line. */
680 list_for_each_safe(ele
, next
, ð_cmd_line
) {
681 struct iss_net_init
*eth
;
682 eth
= list_entry(ele
, struct iss_net_init
, list
);
683 iss_net_configure(eth
->index
, eth
->init
);
688 device_initcall(iss_net_init
);