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)
40 /* ------------------------------------------------------------------------- */
42 /* We currently only support the TUNTAP transport protocol. */
44 #define TRANSPORT_TUNTAP_NAME "tuntap"
45 #define TRANSPORT_TUNTAP_MTU ETH_MAX_PACKET
48 char dev_name
[IFNAMSIZ
];
52 /* ------------------------------------------------------------------------- */
55 struct iss_net_private
;
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
{
70 struct net_device
*dev
;
71 struct platform_device pdev
;
73 struct rtnl_link_stats64 stats
;
75 struct timer_list timer
;
76 unsigned int timer_val
;
83 struct tuntap_info tuntap
;
86 const struct iss_net_ops
*net_ops
;
91 /* ================================ HELPERS ================================ */
94 static char *split_if_spec(char *str
, ...)
100 while ((arg
= va_arg(ap
, char**)) != NULL
) {
105 end
= strchr(str
, ',');
119 /* Set Ethernet address of the specified device. */
121 static void setup_etheraddr(struct net_device
*dev
, char *str
)
128 if (!mac_pton(str
, addr
)) {
129 pr_err("%s: failed to parse '%s' as an ethernet address\n",
133 if (is_multicast_ether_addr(addr
)) {
134 pr_err("%s: attempt to assign a multicast ethernet address\n",
138 if (!is_valid_ether_addr(addr
)) {
139 pr_err("%s: attempt to assign an invalid ethernet address\n",
143 if (!is_local_ether_addr(addr
))
144 pr_warn("%s: assigning a globally valid ethernet address\n",
146 eth_hw_addr_set(dev
, addr
);
150 pr_info("%s: choosing a random ethernet address\n",
152 eth_hw_addr_random(dev
);
155 /* ======================= TUNTAP TRANSPORT INTERFACE ====================== */
157 static int tuntap_open(struct iss_net_private
*lp
)
160 char *dev_name
= lp
->tp
.info
.tuntap
.dev_name
;
164 fd
= simc_open("/dev/net/tun", 02, 0); /* O_RDWR */
166 pr_err("%s: failed to open /dev/net/tun, returned %d (errno = %d)\n",
167 lp
->dev
->name
, fd
, errno
);
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
);
177 pr_err("%s: failed to set interface %s, returned %d (errno = %d)\n",
178 lp
->dev
->name
, dev_name
, err
, errno
);
183 lp
->tp
.info
.tuntap
.fd
= fd
;
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
= {
216 .close
= tuntap_close
,
218 .write
= tuntap_write
,
219 .protocol
= tuntap_protocol
,
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))
238 init
+= sizeof(TRANSPORT_TUNTAP_NAME
) - 1;
240 rem
= split_if_spec(init
+ 1, &mac_str
, &dev_name
, NULL
);
242 pr_err("%s: extra garbage on specification : '%s'\n",
246 } else if (*init
!= '\0') {
247 pr_err("%s: invalid argument: %s. Skipping device!\n",
253 pr_err("%s: missing tuntap device name\n", dev
->name
);
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
;
270 /* ================================ ISS NET ================================ */
272 static int iss_net_rx(struct net_device
*dev
)
274 struct iss_net_private
*lp
= netdev_priv(dev
);
278 /* Check if there is any new data. */
280 if (lp
->tp
.net_ops
->poll(lp
) == 0)
283 /* Try to allocate memory, if it fails, try again next round. */
285 skb
= dev_alloc_skb(dev
->mtu
+ 2 + ETH_HEADER_OTHER
);
287 spin_lock_bh(&lp
->lock
);
288 lp
->stats
.rx_dropped
++;
289 spin_unlock_bh(&lp
->lock
);
298 skb_reset_mac_header(skb
);
299 pkt_len
= lp
->tp
.net_ops
->read(lp
, &skb
);
300 skb_put(skb
, pkt_len
);
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
);
317 static int iss_net_poll(struct iss_net_private
*lp
)
321 if (!netif_running(lp
->dev
))
324 while ((err
= iss_net_rx(lp
->dev
)) > 0)
328 pr_err("Device '%s' read returned %d, shutting it down\n",
332 /* FIXME reactivate_fd(lp->fd, ISS_ETH_IRQ); */
339 static void iss_net_timer(struct timer_list
*t
)
341 struct iss_net_private
*lp
= from_timer(lp
, t
, timer
);
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
);
353 err
= lp
->tp
.net_ops
->open(lp
);
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
);
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
);
384 static int iss_net_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
386 struct iss_net_private
*lp
= netdev_priv(dev
);
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
);
411 netif_start_queue(dev
);
412 pr_err("%s: %s failed(%d)\n", dev
->name
, __func__
, len
);
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
);
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
)
444 static void iss_net_user_timer_expire(struct timer_list
*unused
)
449 static struct platform_driver iss_net_driver
= {
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
));
485 pr_err("eth_configure: failed to allocate device\n");
489 /* Initialize private element. */
491 lp
= netdev_priv(dev
);
492 *lp
= (struct iss_net_private
) {
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
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",
513 goto err_free_netdev
;
516 pr_info("Netdevice %d (%pM)\n", index
, dev
->dev_addr
);
520 if (!driver_registered
) {
521 if (platform_driver_register(&iss_net_driver
))
522 goto err_free_netdev
;
523 driver_registered
= 1;
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
;
535 dev
->watchdog_timeo
= (HZ
>> 1);
539 if (register_netdevice(dev
)) {
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 */
548 timer_setup(&lp
->tl
, iss_net_user_timer_expire
, 0);
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 */
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
;
582 end
= strchr(str
, '=');
584 pr_err("Expected '=' after device number\n");
588 rc
= kstrtouint(str
, 0, &n
);
591 pr_err("Failed to parse '%s'\n", str
);
596 list_for_each(ele
, ð_cmd_line
) {
597 device
= list_entry(ele
, struct iss_net_init
, list
);
598 if (device
->index
== n
)
602 if (device
&& device
->index
== n
) {
603 pr_err("Device %u already configured\n", n
);
607 new = memblock_alloc(sizeof(*new), SMP_CACHE_BYTES
);
609 pr_err("Alloc_bootmem failed\n");
613 INIT_LIST_HEAD(&new->list
);
617 list_add_tail(&new->list
, ð_cmd_line
);
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
, ð_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
);
641 device_initcall(iss_net_init
);