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
) {
110 end
= strchr(str
, ',');
122 /* Set Ethernet address of the specified device. */
124 static void setup_etheraddr(struct net_device
*dev
, char *str
)
126 unsigned char *addr
= dev
->dev_addr
;
131 if (!mac_pton(str
, addr
)) {
132 pr_err("%s: failed to parse '%s' as an ethernet address\n",
136 if (is_multicast_ether_addr(addr
)) {
137 pr_err("%s: attempt to assign a multicast ethernet address\n",
141 if (!is_valid_ether_addr(addr
)) {
142 pr_err("%s: attempt to assign an invalid ethernet address\n",
146 if (!is_local_ether_addr(addr
))
147 pr_warn("%s: assigning a globally valid ethernet address\n",
152 pr_info("%s: choosing a random ethernet address\n",
154 eth_hw_addr_random(dev
);
157 /* ======================= TUNTAP TRANSPORT INTERFACE ====================== */
159 static int tuntap_open(struct iss_net_private
*lp
)
162 char *dev_name
= lp
->tp
.info
.tuntap
.dev_name
;
166 fd
= simc_open("/dev/net/tun", 02, 0); /* O_RDWR */
168 pr_err("%s: failed to open /dev/net/tun, returned %d (errno = %d)\n",
169 lp
->dev
->name
, fd
, errno
);
173 memset(&ifr
, 0, sizeof(ifr
));
174 ifr
.ifr_flags
= IFF_TAP
| IFF_NO_PI
;
175 strlcpy(ifr
.ifr_name
, dev_name
, sizeof(ifr
.ifr_name
));
177 err
= simc_ioctl(fd
, TUNSETIFF
, &ifr
);
179 pr_err("%s: failed to set interface %s, returned %d (errno = %d)\n",
180 lp
->dev
->name
, dev_name
, err
, errno
);
185 lp
->tp
.info
.tuntap
.fd
= fd
;
189 static void tuntap_close(struct iss_net_private
*lp
)
191 simc_close(lp
->tp
.info
.tuntap
.fd
);
192 lp
->tp
.info
.tuntap
.fd
= -1;
195 static int tuntap_read(struct iss_net_private
*lp
, struct sk_buff
**skb
)
197 return simc_read(lp
->tp
.info
.tuntap
.fd
,
198 (*skb
)->data
, (*skb
)->dev
->mtu
+ ETH_HEADER_OTHER
);
201 static int tuntap_write(struct iss_net_private
*lp
, struct sk_buff
**skb
)
203 return simc_write(lp
->tp
.info
.tuntap
.fd
, (*skb
)->data
, (*skb
)->len
);
206 unsigned short tuntap_protocol(struct sk_buff
*skb
)
208 return eth_type_trans(skb
, skb
->dev
);
211 static int tuntap_poll(struct iss_net_private
*lp
)
213 return simc_poll(lp
->tp
.info
.tuntap
.fd
);
217 * ethX=tuntap,[mac address],device name
220 static int tuntap_probe(struct iss_net_private
*lp
, int index
, char *init
)
222 struct net_device
*dev
= lp
->dev
;
223 char *dev_name
= NULL
, *mac_str
= NULL
, *rem
= NULL
;
225 /* Transport should be 'tuntap': ethX=tuntap,mac,dev_name */
227 if (strncmp(init
, TRANSPORT_TUNTAP_NAME
,
228 sizeof(TRANSPORT_TUNTAP_NAME
) - 1))
231 init
+= sizeof(TRANSPORT_TUNTAP_NAME
) - 1;
233 rem
= split_if_spec(init
+ 1, &mac_str
, &dev_name
);
235 pr_err("%s: extra garbage on specification : '%s'\n",
239 } else if (*init
!= '\0') {
240 pr_err("%s: invalid argument: %s. Skipping device!\n",
246 pr_err("%s: missing tuntap device name\n", dev
->name
);
250 strlcpy(lp
->tp
.info
.tuntap
.dev_name
, dev_name
,
251 sizeof(lp
->tp
.info
.tuntap
.dev_name
));
253 setup_etheraddr(dev
, mac_str
);
255 lp
->mtu
= TRANSPORT_TUNTAP_MTU
;
257 lp
->tp
.info
.tuntap
.fd
= -1;
259 lp
->tp
.open
= tuntap_open
;
260 lp
->tp
.close
= tuntap_close
;
261 lp
->tp
.read
= tuntap_read
;
262 lp
->tp
.write
= tuntap_write
;
263 lp
->tp
.protocol
= tuntap_protocol
;
264 lp
->tp
.poll
= tuntap_poll
;
269 /* ================================ ISS NET ================================ */
271 static int iss_net_rx(struct net_device
*dev
)
273 struct iss_net_private
*lp
= netdev_priv(dev
);
277 /* Check if there is any new data. */
279 if (lp
->tp
.poll(lp
) == 0)
282 /* Try to allocate memory, if it fails, try again next round. */
284 skb
= dev_alloc_skb(dev
->mtu
+ 2 + ETH_HEADER_OTHER
);
286 lp
->stats
.rx_dropped
++;
295 skb_reset_mac_header(skb
);
296 pkt_len
= lp
->tp
.read(lp
, &skb
);
297 skb_put(skb
, pkt_len
);
300 skb_trim(skb
, pkt_len
);
301 skb
->protocol
= lp
->tp
.protocol(skb
);
303 lp
->stats
.rx_bytes
+= skb
->len
;
304 lp
->stats
.rx_packets
++;
312 static int iss_net_poll(void)
314 struct list_head
*ele
;
317 spin_lock(&opened_lock
);
319 list_for_each(ele
, &opened
) {
320 struct iss_net_private
*lp
;
322 lp
= list_entry(ele
, struct iss_net_private
, opened_list
);
324 if (!netif_running(lp
->dev
))
327 spin_lock(&lp
->lock
);
329 while ((err
= iss_net_rx(lp
->dev
)) > 0)
332 spin_unlock(&lp
->lock
);
335 pr_err("Device '%s' read returned %d, shutting it down\n",
339 /* FIXME reactivate_fd(lp->fd, ISS_ETH_IRQ); */
343 spin_unlock(&opened_lock
);
348 static void iss_net_timer(unsigned long priv
)
350 struct iss_net_private
*lp
= (struct iss_net_private
*)priv
;
352 spin_lock(&lp
->lock
);
354 mod_timer(&lp
->timer
, jiffies
+ lp
->timer_val
);
355 spin_unlock(&lp
->lock
);
359 static int iss_net_open(struct net_device
*dev
)
361 struct iss_net_private
*lp
= netdev_priv(dev
);
364 spin_lock(&lp
->lock
);
366 err
= lp
->tp
.open(lp
);
370 netif_start_queue(dev
);
372 /* clear buffer - it can happen that the host side of the interface
373 * is full when we get here. In this case, new data is never queued,
374 * SIGIOs never arrive, and the net never works.
376 while ((err
= iss_net_rx(dev
)) > 0)
379 spin_lock(&opened_lock
);
380 list_add(&lp
->opened_list
, &opened
);
381 spin_unlock(&opened_lock
);
383 init_timer(&lp
->timer
);
384 lp
->timer_val
= ISS_NET_TIMER_VALUE
;
385 lp
->timer
.data
= (unsigned long) lp
;
386 lp
->timer
.function
= iss_net_timer
;
387 mod_timer(&lp
->timer
, jiffies
+ lp
->timer_val
);
390 spin_unlock(&lp
->lock
);
394 static int iss_net_close(struct net_device
*dev
)
396 struct iss_net_private
*lp
= netdev_priv(dev
);
397 netif_stop_queue(dev
);
398 spin_lock(&lp
->lock
);
400 spin_lock(&opened_lock
);
402 spin_unlock(&opened_lock
);
404 del_timer_sync(&lp
->timer
);
408 spin_unlock(&lp
->lock
);
412 static int iss_net_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
414 struct iss_net_private
*lp
= netdev_priv(dev
);
418 netif_stop_queue(dev
);
419 spin_lock_irqsave(&lp
->lock
, flags
);
421 len
= lp
->tp
.write(lp
, &skb
);
423 if (len
== skb
->len
) {
424 lp
->stats
.tx_packets
++;
425 lp
->stats
.tx_bytes
+= skb
->len
;
426 dev
->trans_start
= jiffies
;
427 netif_start_queue(dev
);
429 /* this is normally done in the interrupt when tx finishes */
430 netif_wake_queue(dev
);
432 } else if (len
== 0) {
433 netif_start_queue(dev
);
434 lp
->stats
.tx_dropped
++;
437 netif_start_queue(dev
);
438 pr_err("%s: %s failed(%d)\n", dev
->name
, __func__
, len
);
441 spin_unlock_irqrestore(&lp
->lock
, flags
);
448 static struct net_device_stats
*iss_net_get_stats(struct net_device
*dev
)
450 struct iss_net_private
*lp
= netdev_priv(dev
);
454 static void iss_net_set_multicast_list(struct net_device
*dev
)
458 static void iss_net_tx_timeout(struct net_device
*dev
)
462 static int iss_net_set_mac(struct net_device
*dev
, void *addr
)
464 struct iss_net_private
*lp
= netdev_priv(dev
);
465 struct sockaddr
*hwaddr
= addr
;
467 if (!is_valid_ether_addr(hwaddr
->sa_data
))
468 return -EADDRNOTAVAIL
;
469 spin_lock(&lp
->lock
);
470 memcpy(dev
->dev_addr
, hwaddr
->sa_data
, ETH_ALEN
);
471 spin_unlock(&lp
->lock
);
475 static int iss_net_change_mtu(struct net_device
*dev
, int new_mtu
)
480 void iss_net_user_timer_expire(unsigned long _conn
)
485 static struct platform_driver iss_net_driver
= {
491 static int driver_registered
;
493 static const struct net_device_ops iss_netdev_ops
= {
494 .ndo_open
= iss_net_open
,
495 .ndo_stop
= iss_net_close
,
496 .ndo_get_stats
= iss_net_get_stats
,
497 .ndo_start_xmit
= iss_net_start_xmit
,
498 .ndo_validate_addr
= eth_validate_addr
,
499 .ndo_change_mtu
= iss_net_change_mtu
,
500 .ndo_set_mac_address
= iss_net_set_mac
,
501 .ndo_tx_timeout
= iss_net_tx_timeout
,
502 .ndo_set_rx_mode
= iss_net_set_multicast_list
,
505 static int iss_net_configure(int index
, char *init
)
507 struct net_device
*dev
;
508 struct iss_net_private
*lp
;
511 dev
= alloc_etherdev(sizeof(*lp
));
513 pr_err("eth_configure: failed to allocate device\n");
517 /* Initialize private element. */
519 lp
= netdev_priv(dev
);
520 *lp
= (struct iss_net_private
) {
521 .device_list
= LIST_HEAD_INIT(lp
->device_list
),
522 .opened_list
= LIST_HEAD_INIT(lp
->opened_list
),
523 .lock
= __SPIN_LOCK_UNLOCKED(lp
.lock
),
529 * If this name ends up conflicting with an existing registered
530 * netdevice, that is OK, register_netdev{,ice}() will notice this
533 snprintf(dev
->name
, sizeof(dev
->name
), "eth%d", index
);
536 * Try all transport protocols.
537 * Note: more protocols can be added by adding '&& !X_init(lp, eth)'.
540 if (!tuntap_probe(lp
, index
, init
)) {
541 pr_err("%s: invalid arguments. Skipping device!\n",
546 pr_info("Netdevice %d (%pM)\n", index
, dev
->dev_addr
);
550 if (!driver_registered
) {
551 platform_driver_register(&iss_net_driver
);
552 driver_registered
= 1;
555 spin_lock(&devices_lock
);
556 list_add(&lp
->device_list
, &devices
);
557 spin_unlock(&devices_lock
);
560 lp
->pdev
.name
= DRIVER_NAME
;
561 platform_device_register(&lp
->pdev
);
562 SET_NETDEV_DEV(dev
, &lp
->pdev
.dev
);
564 dev
->netdev_ops
= &iss_netdev_ops
;
566 dev
->watchdog_timeo
= (HZ
>> 1);
570 err
= register_netdevice(dev
);
574 pr_err("%s: error registering net device!\n", dev
->name
);
575 /* XXX: should we call ->remove() here? */
581 lp
->tl
.function
= iss_net_user_timer_expire
;
586 /* FIXME: unregister; free, etc.. */
590 /* ------------------------------------------------------------------------- */
592 /* Filled in during early boot */
594 struct list_head eth_cmd_line
= LIST_HEAD_INIT(eth_cmd_line
);
596 struct iss_net_init
{
597 struct list_head list
;
598 char *init
; /* init string */
603 * Parse the command line and look for 'ethX=...' fields, and register all
604 * those fields. They will be later initialized in iss_net_init.
607 #define ERR KERN_ERR "iss_net_setup: "
609 static int __init
iss_net_setup(char *str
)
611 struct iss_net_private
*device
= NULL
;
612 struct iss_net_init
*new;
613 struct list_head
*ele
;
618 end
= strchr(str
, '=');
620 printk(ERR
"Expected '=' after device number\n");
624 rc
= kstrtouint(str
, 0, &n
);
627 printk(ERR
"Failed to parse '%s'\n", str
);
632 spin_lock(&devices_lock
);
634 list_for_each(ele
, &devices
) {
635 device
= list_entry(ele
, struct iss_net_private
, device_list
);
636 if (device
->index
== n
)
640 spin_unlock(&devices_lock
);
642 if (device
&& device
->index
== n
) {
643 printk(ERR
"Device %u already configured\n", n
);
647 new = alloc_bootmem(sizeof(*new));
649 printk(ERR
"Alloc_bootmem failed\n");
653 INIT_LIST_HEAD(&new->list
);
657 list_add_tail(&new->list
, ð_cmd_line
);
663 __setup("eth", iss_net_setup
);
666 * Initialize all ISS Ethernet devices previously registered in iss_net_setup.
669 static int iss_net_init(void)
671 struct list_head
*ele
, *next
;
673 /* Walk through all Ethernet devices specified in the command line. */
675 list_for_each_safe(ele
, next
, ð_cmd_line
) {
676 struct iss_net_init
*eth
;
677 eth
= list_entry(ele
, struct iss_net_init
, list
);
678 iss_net_configure(eth
->index
, eth
->init
);
684 module_init(iss_net_init
);