drm/tests: hdmi: Fix memory leaks in drm_display_mode_from_cea_vic()
[drm/drm-misc.git] / arch / xtensa / platforms / iss / network.c
blobe89f27f2bb18dabc13a7de5f3719cff77a2752c0
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
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)
40 /* ------------------------------------------------------------------------- */
42 /* We currently only support the TUNTAP transport protocol. */
44 #define TRANSPORT_TUNTAP_NAME "tuntap"
45 #define TRANSPORT_TUNTAP_MTU ETH_MAX_PACKET
47 struct tuntap_info {
48 char dev_name[IFNAMSIZ];
49 int fd;
52 /* ------------------------------------------------------------------------- */
55 struct iss_net_private;
57 struct iss_net_ops {
58 int (*open)(struct iss_net_private *lp);
59 void (*close)(struct iss_net_private *lp);
60 int (*read)(struct iss_net_private *lp, struct sk_buff **skb);
61 int (*write)(struct iss_net_private *lp, struct sk_buff **skb);
62 unsigned short (*protocol)(struct sk_buff *skb);
63 int (*poll)(struct iss_net_private *lp);
66 /* This structure contains out private information for the driver. */
68 struct iss_net_private {
69 spinlock_t lock;
70 struct net_device *dev;
71 struct platform_device pdev;
72 struct timer_list tl;
73 struct rtnl_link_stats64 stats;
75 struct timer_list timer;
76 unsigned int timer_val;
78 int index;
79 int mtu;
81 struct {
82 union {
83 struct tuntap_info tuntap;
84 } info;
86 const struct iss_net_ops *net_ops;
87 } tp;
91 /* ================================ HELPERS ================================ */
94 static char *split_if_spec(char *str, ...)
96 char **arg, *end;
97 va_list ap;
99 va_start(ap, str);
100 while ((arg = va_arg(ap, char**)) != NULL) {
101 if (*str == '\0') {
102 va_end(ap);
103 return NULL;
105 end = strchr(str, ',');
106 if (end != str)
107 *arg = str;
108 if (end == NULL) {
109 va_end(ap);
110 return NULL;
112 *end++ = '\0';
113 str = end;
115 va_end(ap);
116 return str;
119 /* Set Ethernet address of the specified device. */
121 static void setup_etheraddr(struct net_device *dev, char *str)
123 u8 addr[ETH_ALEN];
125 if (str == NULL)
126 goto random;
128 if (!mac_pton(str, addr)) {
129 pr_err("%s: failed to parse '%s' as an ethernet address\n",
130 dev->name, str);
131 goto random;
133 if (is_multicast_ether_addr(addr)) {
134 pr_err("%s: attempt to assign a multicast ethernet address\n",
135 dev->name);
136 goto random;
138 if (!is_valid_ether_addr(addr)) {
139 pr_err("%s: attempt to assign an invalid ethernet address\n",
140 dev->name);
141 goto random;
143 if (!is_local_ether_addr(addr))
144 pr_warn("%s: assigning a globally valid ethernet address\n",
145 dev->name);
146 eth_hw_addr_set(dev, addr);
147 return;
149 random:
150 pr_info("%s: choosing a random ethernet address\n",
151 dev->name);
152 eth_hw_addr_random(dev);
155 /* ======================= TUNTAP TRANSPORT INTERFACE ====================== */
157 static int tuntap_open(struct iss_net_private *lp)
159 struct ifreq ifr;
160 char *dev_name = lp->tp.info.tuntap.dev_name;
161 int err = -EINVAL;
162 int fd;
164 fd = simc_open("/dev/net/tun", 02, 0); /* O_RDWR */
165 if (fd < 0) {
166 pr_err("%s: failed to open /dev/net/tun, returned %d (errno = %d)\n",
167 lp->dev->name, fd, errno);
168 return fd;
171 memset(&ifr, 0, sizeof(ifr));
172 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
173 strscpy(ifr.ifr_name, dev_name, sizeof(ifr.ifr_name));
175 err = simc_ioctl(fd, TUNSETIFF, &ifr);
176 if (err < 0) {
177 pr_err("%s: failed to set interface %s, returned %d (errno = %d)\n",
178 lp->dev->name, dev_name, err, errno);
179 simc_close(fd);
180 return err;
183 lp->tp.info.tuntap.fd = fd;
184 return err;
187 static void tuntap_close(struct iss_net_private *lp)
189 simc_close(lp->tp.info.tuntap.fd);
190 lp->tp.info.tuntap.fd = -1;
193 static int tuntap_read(struct iss_net_private *lp, struct sk_buff **skb)
195 return simc_read(lp->tp.info.tuntap.fd,
196 (*skb)->data, (*skb)->dev->mtu + ETH_HEADER_OTHER);
199 static int tuntap_write(struct iss_net_private *lp, struct sk_buff **skb)
201 return simc_write(lp->tp.info.tuntap.fd, (*skb)->data, (*skb)->len);
204 static unsigned short tuntap_protocol(struct sk_buff *skb)
206 return eth_type_trans(skb, skb->dev);
209 static int tuntap_poll(struct iss_net_private *lp)
211 return simc_poll(lp->tp.info.tuntap.fd);
214 static const struct iss_net_ops tuntap_ops = {
215 .open = tuntap_open,
216 .close = tuntap_close,
217 .read = tuntap_read,
218 .write = tuntap_write,
219 .protocol = tuntap_protocol,
220 .poll = tuntap_poll,
224 * ethX=tuntap,[mac address],device name
227 static int tuntap_probe(struct iss_net_private *lp, int index, char *init)
229 struct net_device *dev = lp->dev;
230 char *dev_name = NULL, *mac_str = NULL, *rem = NULL;
232 /* Transport should be 'tuntap': ethX=tuntap,mac,dev_name */
234 if (strncmp(init, TRANSPORT_TUNTAP_NAME,
235 sizeof(TRANSPORT_TUNTAP_NAME) - 1))
236 return 0;
238 init += sizeof(TRANSPORT_TUNTAP_NAME) - 1;
239 if (*init == ',') {
240 rem = split_if_spec(init + 1, &mac_str, &dev_name, NULL);
241 if (rem != NULL) {
242 pr_err("%s: extra garbage on specification : '%s'\n",
243 dev->name, rem);
244 return 0;
246 } else if (*init != '\0') {
247 pr_err("%s: invalid argument: %s. Skipping device!\n",
248 dev->name, init);
249 return 0;
252 if (!dev_name) {
253 pr_err("%s: missing tuntap device name\n", dev->name);
254 return 0;
257 strscpy(lp->tp.info.tuntap.dev_name, dev_name,
258 sizeof(lp->tp.info.tuntap.dev_name));
260 setup_etheraddr(dev, mac_str);
262 lp->mtu = TRANSPORT_TUNTAP_MTU;
264 lp->tp.info.tuntap.fd = -1;
265 lp->tp.net_ops = &tuntap_ops;
267 return 1;
270 /* ================================ ISS NET ================================ */
272 static int iss_net_rx(struct net_device *dev)
274 struct iss_net_private *lp = netdev_priv(dev);
275 int pkt_len;
276 struct sk_buff *skb;
278 /* Check if there is any new data. */
280 if (lp->tp.net_ops->poll(lp) == 0)
281 return 0;
283 /* Try to allocate memory, if it fails, try again next round. */
285 skb = dev_alloc_skb(dev->mtu + 2 + ETH_HEADER_OTHER);
286 if (skb == NULL) {
287 spin_lock_bh(&lp->lock);
288 lp->stats.rx_dropped++;
289 spin_unlock_bh(&lp->lock);
290 return 0;
293 skb_reserve(skb, 2);
295 /* Setup skb */
297 skb->dev = dev;
298 skb_reset_mac_header(skb);
299 pkt_len = lp->tp.net_ops->read(lp, &skb);
300 skb_put(skb, pkt_len);
302 if (pkt_len > 0) {
303 skb_trim(skb, pkt_len);
304 skb->protocol = lp->tp.net_ops->protocol(skb);
306 spin_lock_bh(&lp->lock);
307 lp->stats.rx_bytes += skb->len;
308 lp->stats.rx_packets++;
309 spin_unlock_bh(&lp->lock);
310 netif_rx(skb);
311 return pkt_len;
313 kfree_skb(skb);
314 return pkt_len;
317 static int iss_net_poll(struct iss_net_private *lp)
319 int err, ret = 0;
321 if (!netif_running(lp->dev))
322 return 0;
324 while ((err = iss_net_rx(lp->dev)) > 0)
325 ret++;
327 if (err < 0) {
328 pr_err("Device '%s' read returned %d, shutting it down\n",
329 lp->dev->name, err);
330 dev_close(lp->dev);
331 } else {
332 /* FIXME reactivate_fd(lp->fd, ISS_ETH_IRQ); */
335 return ret;
339 static void iss_net_timer(struct timer_list *t)
341 struct iss_net_private *lp = from_timer(lp, t, timer);
343 iss_net_poll(lp);
344 mod_timer(&lp->timer, jiffies + lp->timer_val);
348 static int iss_net_open(struct net_device *dev)
350 struct iss_net_private *lp = netdev_priv(dev);
351 int err;
353 err = lp->tp.net_ops->open(lp);
354 if (err < 0)
355 return err;
357 netif_start_queue(dev);
359 /* clear buffer - it can happen that the host side of the interface
360 * is full when we get here. In this case, new data is never queued,
361 * SIGIOs never arrive, and the net never works.
363 while ((err = iss_net_rx(dev)) > 0)
366 timer_setup(&lp->timer, iss_net_timer, 0);
367 lp->timer_val = ISS_NET_TIMER_VALUE;
368 mod_timer(&lp->timer, jiffies + lp->timer_val);
370 return err;
373 static int iss_net_close(struct net_device *dev)
375 struct iss_net_private *lp = netdev_priv(dev);
377 netif_stop_queue(dev);
378 del_timer_sync(&lp->timer);
379 lp->tp.net_ops->close(lp);
381 return 0;
384 static int iss_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
386 struct iss_net_private *lp = netdev_priv(dev);
387 int len;
389 netif_stop_queue(dev);
391 len = lp->tp.net_ops->write(lp, &skb);
393 if (len == skb->len) {
394 spin_lock_bh(&lp->lock);
395 lp->stats.tx_packets++;
396 lp->stats.tx_bytes += skb->len;
397 spin_unlock_bh(&lp->lock);
398 netif_trans_update(dev);
399 netif_start_queue(dev);
401 /* this is normally done in the interrupt when tx finishes */
402 netif_wake_queue(dev);
404 } else if (len == 0) {
405 netif_start_queue(dev);
406 spin_lock_bh(&lp->lock);
407 lp->stats.tx_dropped++;
408 spin_unlock_bh(&lp->lock);
410 } else {
411 netif_start_queue(dev);
412 pr_err("%s: %s failed(%d)\n", dev->name, __func__, len);
416 dev_kfree_skb(skb);
417 return NETDEV_TX_OK;
421 static void iss_net_get_stats64(struct net_device *dev,
422 struct rtnl_link_stats64 *stats)
424 struct iss_net_private *lp = netdev_priv(dev);
426 spin_lock_bh(&lp->lock);
427 *stats = lp->stats;
428 spin_unlock_bh(&lp->lock);
431 static void iss_net_set_multicast_list(struct net_device *dev)
435 static void iss_net_tx_timeout(struct net_device *dev, unsigned int txqueue)
439 static int iss_net_change_mtu(struct net_device *dev, int new_mtu)
441 return -EINVAL;
444 static void iss_net_user_timer_expire(struct timer_list *unused)
449 static struct platform_driver iss_net_driver = {
450 .driver = {
451 .name = DRIVER_NAME,
455 static int driver_registered;
457 static const struct net_device_ops iss_netdev_ops = {
458 .ndo_open = iss_net_open,
459 .ndo_stop = iss_net_close,
460 .ndo_get_stats64 = iss_net_get_stats64,
461 .ndo_start_xmit = iss_net_start_xmit,
462 .ndo_validate_addr = eth_validate_addr,
463 .ndo_change_mtu = iss_net_change_mtu,
464 .ndo_set_mac_address = eth_mac_addr,
465 .ndo_tx_timeout = iss_net_tx_timeout,
466 .ndo_set_rx_mode = iss_net_set_multicast_list,
469 static void iss_net_pdev_release(struct device *dev)
471 struct platform_device *pdev = to_platform_device(dev);
472 struct iss_net_private *lp =
473 container_of(pdev, struct iss_net_private, pdev);
475 free_netdev(lp->dev);
478 static void iss_net_configure(int index, char *init)
480 struct net_device *dev;
481 struct iss_net_private *lp;
483 dev = alloc_etherdev(sizeof(*lp));
484 if (dev == NULL) {
485 pr_err("eth_configure: failed to allocate device\n");
486 return;
489 /* Initialize private element. */
491 lp = netdev_priv(dev);
492 *lp = (struct iss_net_private) {
493 .dev = dev,
494 .index = index,
497 spin_lock_init(&lp->lock);
499 * If this name ends up conflicting with an existing registered
500 * netdevice, that is OK, register_netdev{,ice}() will notice this
501 * and fail.
503 snprintf(dev->name, sizeof(dev->name), "eth%d", index);
506 * Try all transport protocols.
507 * Note: more protocols can be added by adding '&& !X_init(lp, eth)'.
510 if (!tuntap_probe(lp, index, init)) {
511 pr_err("%s: invalid arguments. Skipping device!\n",
512 dev->name);
513 goto err_free_netdev;
516 pr_info("Netdevice %d (%pM)\n", index, dev->dev_addr);
518 /* sysfs register */
520 if (!driver_registered) {
521 if (platform_driver_register(&iss_net_driver))
522 goto err_free_netdev;
523 driver_registered = 1;
526 lp->pdev.id = index;
527 lp->pdev.name = DRIVER_NAME;
528 lp->pdev.dev.release = iss_net_pdev_release;
529 if (platform_device_register(&lp->pdev))
530 goto err_free_netdev;
531 SET_NETDEV_DEV(dev, &lp->pdev.dev);
533 dev->netdev_ops = &iss_netdev_ops;
534 dev->mtu = lp->mtu;
535 dev->watchdog_timeo = (HZ >> 1);
536 dev->irq = -1;
538 rtnl_lock();
539 if (register_netdevice(dev)) {
540 rtnl_unlock();
541 pr_err("%s: error registering net device!\n", dev->name);
542 platform_device_unregister(&lp->pdev);
543 /* dev is freed by the iss_net_pdev_release callback */
544 return;
546 rtnl_unlock();
548 timer_setup(&lp->tl, iss_net_user_timer_expire, 0);
550 return;
552 err_free_netdev:
553 free_netdev(dev);
556 /* ------------------------------------------------------------------------- */
558 /* Filled in during early boot */
560 struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
562 struct iss_net_init {
563 struct list_head list;
564 char *init; /* init string */
565 int index;
569 * Parse the command line and look for 'ethX=...' fields, and register all
570 * those fields. They will be later initialized in iss_net_init.
573 static int __init iss_net_setup(char *str)
575 struct iss_net_init *device = NULL;
576 struct iss_net_init *new;
577 struct list_head *ele;
578 char *end;
579 int rc;
580 unsigned n;
582 end = strchr(str, '=');
583 if (!end) {
584 pr_err("Expected '=' after device number\n");
585 return 1;
587 *end = 0;
588 rc = kstrtouint(str, 0, &n);
589 *end = '=';
590 if (rc < 0) {
591 pr_err("Failed to parse '%s'\n", str);
592 return 1;
594 str = end;
596 list_for_each(ele, &eth_cmd_line) {
597 device = list_entry(ele, struct iss_net_init, list);
598 if (device->index == n)
599 break;
602 if (device && device->index == n) {
603 pr_err("Device %u already configured\n", n);
604 return 1;
607 new = memblock_alloc(sizeof(*new), SMP_CACHE_BYTES);
608 if (new == NULL) {
609 pr_err("Alloc_bootmem failed\n");
610 return 1;
613 INIT_LIST_HEAD(&new->list);
614 new->index = n;
615 new->init = str + 1;
617 list_add_tail(&new->list, &eth_cmd_line);
618 return 1;
621 __setup("eth", iss_net_setup);
624 * Initialize all ISS Ethernet devices previously registered in iss_net_setup.
627 static int iss_net_init(void)
629 struct list_head *ele, *next;
631 /* Walk through all Ethernet devices specified in the command line. */
633 list_for_each_safe(ele, next, &eth_cmd_line) {
634 struct iss_net_init *eth;
635 eth = list_entry(ele, struct iss_net_init, list);
636 iss_net_configure(eth->index, eth->init);
639 return 1;
641 device_initcall(iss_net_init);