1 // SPDX-License-Identifier: GPL-2.0-or-later
4 * arch/xtensa/platforms/iss/network.c
6 * Platform specific initialization.
8 * Authors: Chris Zankel <chris@zankel.net>
9 * Based on work form the UML team.
11 * Copyright 2005 Tensilica Inc.
14 #define pr_fmt(fmt) "%s: " fmt, __func__
16 #include <linux/list.h>
17 #include <linux/irq.h>
18 #include <linux/spinlock.h>
19 #include <linux/slab.h>
20 #include <linux/timer.h>
21 #include <linux/if_ether.h>
22 #include <linux/inetdevice.h>
23 #include <linux/init.h>
24 #include <linux/if_tun.h>
25 #include <linux/etherdevice.h>
26 #include <linux/interrupt.h>
27 #include <linux/ioctl.h>
28 #include <linux/memblock.h>
29 #include <linux/ethtool.h>
30 #include <linux/rtnetlink.h>
31 #include <linux/platform_device.h>
33 #include <platform/simcall.h>
35 #define DRIVER_NAME "iss-netdev"
36 #define ETH_MAX_PACKET 1500
37 #define ETH_HEADER_OTHER 14
38 #define ISS_NET_TIMER_VALUE (HZ / 10)
41 static DEFINE_SPINLOCK(devices_lock);
42 static LIST_HEAD(devices);
44 /* ------------------------------------------------------------------------- */
46 /* We currently only support the TUNTAP transport protocol. */
48 #define TRANSPORT_TUNTAP_NAME "tuntap"
49 #define TRANSPORT_TUNTAP_MTU ETH_MAX_PACKET
52 char dev_name[IFNAMSIZ];
56 /* ------------------------------------------------------------------------- */
59 struct iss_net_private;
62 int (*open)(struct iss_net_private *lp);
63 void (*close)(struct iss_net_private *lp);
64 int (*read)(struct iss_net_private *lp, struct sk_buff **skb);
65 int (*write)(struct iss_net_private *lp, struct sk_buff **skb);
66 unsigned short (*protocol)(struct sk_buff *skb);
67 int (*poll)(struct iss_net_private *lp);
70 /* This structure contains out private information for the driver. */
72 struct iss_net_private {
73 struct list_head device_list;
76 struct net_device *dev;
77 struct platform_device pdev;
79 struct rtnl_link_stats64 stats;
81 struct timer_list timer;
82 unsigned int timer_val;
89 struct tuntap_info tuntap;
92 const struct iss_net_ops *net_ops;
97 /* ================================ HELPERS ================================ */
100 static char *split_if_spec(char *str, ...)
106 while ((arg = va_arg(ap, char**)) != NULL) {
111 end = strchr(str, ',');
125 /* Set Ethernet address of the specified device. */
127 static void setup_etheraddr(struct net_device *dev, char *str)
134 if (!mac_pton(str, addr)) {
135 pr_err("%s: failed to parse '%s' as an ethernet address\n",
139 if (is_multicast_ether_addr(addr)) {
140 pr_err("%s: attempt to assign a multicast ethernet address\n",
144 if (!is_valid_ether_addr(addr)) {
145 pr_err("%s: attempt to assign an invalid ethernet address\n",
149 if (!is_local_ether_addr(addr))
150 pr_warn("%s: assigning a globally valid ethernet address\n",
152 eth_hw_addr_set(dev, addr);
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 strscpy(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);
220 static const struct iss_net_ops tuntap_ops = {
222 .close = tuntap_close,
224 .write = tuntap_write,
225 .protocol = tuntap_protocol,
230 * ethX=tuntap,[mac address],device name
233 static int tuntap_probe(struct iss_net_private *lp, int index, char *init)
235 struct net_device *dev = lp->dev;
236 char *dev_name = NULL, *mac_str = NULL, *rem = NULL;
238 /* Transport should be 'tuntap': ethX=tuntap,mac,dev_name */
240 if (strncmp(init, TRANSPORT_TUNTAP_NAME,
241 sizeof(TRANSPORT_TUNTAP_NAME) - 1))
244 init += sizeof(TRANSPORT_TUNTAP_NAME) - 1;
246 rem = split_if_spec(init + 1, &mac_str, &dev_name);
248 pr_err("%s: extra garbage on specification : '%s'\n",
252 } else if (*init != '\0') {
253 pr_err("%s: invalid argument: %s. Skipping device!\n",
259 pr_err("%s: missing tuntap device name\n", dev->name);
263 strscpy(lp->tp.info.tuntap.dev_name, dev_name,
264 sizeof(lp->tp.info.tuntap.dev_name));
266 setup_etheraddr(dev, mac_str);
268 lp->mtu = TRANSPORT_TUNTAP_MTU;
270 lp->tp.info.tuntap.fd = -1;
271 lp->tp.net_ops = &tuntap_ops;
276 /* ================================ ISS NET ================================ */
278 static int iss_net_rx(struct net_device *dev)
280 struct iss_net_private *lp = netdev_priv(dev);
284 /* Check if there is any new data. */
286 if (lp->tp.net_ops->poll(lp) == 0)
289 /* Try to allocate memory, if it fails, try again next round. */
291 skb = dev_alloc_skb(dev->mtu + 2 + ETH_HEADER_OTHER);
293 spin_lock_bh(&lp->lock);
294 lp->stats.rx_dropped++;
295 spin_unlock_bh(&lp->lock);
304 skb_reset_mac_header(skb);
305 pkt_len = lp->tp.net_ops->read(lp, &skb);
306 skb_put(skb, pkt_len);
309 skb_trim(skb, pkt_len);
310 skb->protocol = lp->tp.net_ops->protocol(skb);
312 spin_lock_bh(&lp->lock);
313 lp->stats.rx_bytes += skb->len;
314 lp->stats.rx_packets++;
315 spin_unlock_bh(&lp->lock);
323 static int iss_net_poll(struct iss_net_private *lp)
327 if (!netif_running(lp->dev))
330 while ((err = iss_net_rx(lp->dev)) > 0)
334 pr_err("Device '%s' read returned %d, shutting it down\n",
338 /* FIXME reactivate_fd(lp->fd, ISS_ETH_IRQ); */
345 static void iss_net_timer(struct timer_list *t)
347 struct iss_net_private *lp = from_timer(lp, t, timer);
350 mod_timer(&lp->timer, jiffies + lp->timer_val);
354 static int iss_net_open(struct net_device *dev)
356 struct iss_net_private *lp = netdev_priv(dev);
359 err = lp->tp.net_ops->open(lp);
363 netif_start_queue(dev);
365 /* clear buffer - it can happen that the host side of the interface
366 * is full when we get here. In this case, new data is never queued,
367 * SIGIOs never arrive, and the net never works.
369 while ((err = iss_net_rx(dev)) > 0)
372 timer_setup(&lp->timer, iss_net_timer, 0);
373 lp->timer_val = ISS_NET_TIMER_VALUE;
374 mod_timer(&lp->timer, jiffies + lp->timer_val);
379 static int iss_net_close(struct net_device *dev)
381 struct iss_net_private *lp = netdev_priv(dev);
383 netif_stop_queue(dev);
384 del_timer_sync(&lp->timer);
385 lp->tp.net_ops->close(lp);
390 static int iss_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
392 struct iss_net_private *lp = netdev_priv(dev);
395 netif_stop_queue(dev);
397 len = lp->tp.net_ops->write(lp, &skb);
399 if (len == skb->len) {
400 spin_lock_bh(&lp->lock);
401 lp->stats.tx_packets++;
402 lp->stats.tx_bytes += skb->len;
403 spin_unlock_bh(&lp->lock);
404 netif_trans_update(dev);
405 netif_start_queue(dev);
407 /* this is normally done in the interrupt when tx finishes */
408 netif_wake_queue(dev);
410 } else if (len == 0) {
411 netif_start_queue(dev);
412 spin_lock_bh(&lp->lock);
413 lp->stats.tx_dropped++;
414 spin_unlock_bh(&lp->lock);
417 netif_start_queue(dev);
418 pr_err("%s: %s failed(%d)\n", dev->name, __func__, len);
427 static void iss_net_get_stats64(struct net_device *dev,
428 struct rtnl_link_stats64 *stats)
430 struct iss_net_private *lp = netdev_priv(dev);
432 spin_lock_bh(&lp->lock);
434 spin_unlock_bh(&lp->lock);
437 static void iss_net_set_multicast_list(struct net_device *dev)
441 static void iss_net_tx_timeout(struct net_device *dev, unsigned int txqueue)
445 static int iss_net_change_mtu(struct net_device *dev, int new_mtu)
450 void iss_net_user_timer_expire(struct timer_list *unused)
455 static struct platform_driver iss_net_driver = {
461 static int driver_registered;
463 static const struct net_device_ops iss_netdev_ops = {
464 .ndo_open = iss_net_open,
465 .ndo_stop = iss_net_close,
466 .ndo_get_stats64 = iss_net_get_stats64,
467 .ndo_start_xmit = iss_net_start_xmit,
468 .ndo_validate_addr = eth_validate_addr,
469 .ndo_change_mtu = iss_net_change_mtu,
470 .ndo_set_mac_address = eth_mac_addr,
471 .ndo_tx_timeout = iss_net_tx_timeout,
472 .ndo_set_rx_mode = iss_net_set_multicast_list,
475 static int iss_net_configure(int index, char *init)
477 struct net_device *dev;
478 struct iss_net_private *lp;
481 dev = alloc_etherdev(sizeof(*lp));
483 pr_err("eth_configure: failed to allocate device\n");
487 /* Initialize private element. */
489 lp = netdev_priv(dev);
490 *lp = (struct iss_net_private) {
491 .device_list = LIST_HEAD_INIT(lp->device_list),
496 spin_lock_init(&lp->lock);
498 * If this name ends up conflicting with an existing registered
499 * netdevice, that is OK, register_netdev{,ice}() will notice this
502 snprintf(dev->name, sizeof(dev->name), "eth%d", index);
505 * Try all transport protocols.
506 * Note: more protocols can be added by adding '&& !X_init(lp, eth)'.
509 if (!tuntap_probe(lp, index, init)) {
510 pr_err("%s: invalid arguments. Skipping device!\n",
515 pr_info("Netdevice %d (%pM)\n", index, dev->dev_addr);
519 if (!driver_registered) {
520 platform_driver_register(&iss_net_driver);
521 driver_registered = 1;
524 spin_lock(&devices_lock);
525 list_add(&lp->device_list, &devices);
526 spin_unlock(&devices_lock);
529 lp->pdev.name = DRIVER_NAME;
530 platform_device_register(&lp->pdev);
531 SET_NETDEV_DEV(dev, &lp->pdev.dev);
533 dev->netdev_ops = &iss_netdev_ops;
535 dev->watchdog_timeo = (HZ >> 1);
539 err = register_netdevice(dev);
543 pr_err("%s: error registering net device!\n", dev->name);
544 /* XXX: should we call ->remove() here? */
549 timer_setup(&lp->tl, iss_net_user_timer_expire, 0);
554 /* FIXME: unregister; free, etc.. */
558 /* ------------------------------------------------------------------------- */
560 /* Filled in during early boot */
562 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
564 struct iss_net_init {
565 struct list_head list;
566 char *init; /* init string */
571 * Parse the command line and look for 'ethX=...' fields, and register all
572 * those fields. They will be later initialized in iss_net_init.
575 static int __init iss_net_setup(char *str)
577 struct iss_net_private *device = NULL;
578 struct iss_net_init *new;
579 struct list_head *ele;
584 end = strchr(str, '=');
586 pr_err("Expected '=' after device number\n");
590 rc = kstrtouint(str, 0, &n);
593 pr_err("Failed to parse '%s'\n", str);
598 spin_lock(&devices_lock);
600 list_for_each(ele, &devices) {
601 device = list_entry(ele, struct iss_net_private, device_list);
602 if (device->index == n)
606 spin_unlock(&devices_lock);
608 if (device && device->index == n) {
609 pr_err("Device %u already configured\n", n);
613 new = memblock_alloc(sizeof(*new), SMP_CACHE_BYTES);
615 pr_err("Alloc_bootmem failed\n");
619 INIT_LIST_HEAD(&new->list);
623 list_add_tail(&new->list, ð_cmd_line);
627 __setup("eth", iss_net_setup);
630 * Initialize all ISS Ethernet devices previously registered in iss_net_setup.
633 static int iss_net_init(void)
635 struct list_head *ele, *next;
637 /* Walk through all Ethernet devices specified in the command line. */
639 list_for_each_safe(ele, next, ð_cmd_line) {
640 struct iss_net_init *eth;
641 eth = list_entry(ele, struct iss_net_init, list);
642 iss_net_configure(eth->index, eth->init);
647 device_initcall(iss_net_init);