blob: 17b1ef3232e4833349c2f64ed19e08dede454b52 [file] [log] [blame]
Chris Zankel7282bee2005-06-23 22:01:33 -07001/*
2 *
Chris Zankel5fee3252008-12-04 09:21:20 -08003 * arch/xtensa/platforms/iss/network.c
Chris Zankel7282bee2005-06-23 22:01:33 -07004 *
5 * Platform specific initialization.
6 *
7 * Authors: Chris Zankel <chris@zankel.net>
8 * Based on work form the UML team.
9 *
10 * Copyright 2005 Tensilica Inc.
11 *
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.
16 *
17 */
18
Chris Zankel7282bee2005-06-23 22:01:33 -070019#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>
Russell Kingd052d1b2005-10-29 19:07:23 +010034#include <linux/platform_device.h>
Chris Zankel7282bee2005-06-23 22:01:33 -070035
Chris Zankel5fee3252008-12-04 09:21:20 -080036#include <platform/simcall.h>
Chris Zankel7282bee2005-06-23 22:01:33 -070037
38#define DRIVER_NAME "iss-netdev"
39#define ETH_MAX_PACKET 1500
40#define ETH_HEADER_OTHER 14
Max Filippov306ab542013-11-10 08:59:43 +040041#define ISS_NET_TIMER_VALUE (HZ / 10)
Chris Zankel7282bee2005-06-23 22:01:33 -070042
43
44static DEFINE_SPINLOCK(opened_lock);
45static LIST_HEAD(opened);
46
47static DEFINE_SPINLOCK(devices_lock);
48static LIST_HEAD(devices);
49
50/* ------------------------------------------------------------------------- */
51
52/* We currently only support the TUNTAP transport protocol. */
53
54#define TRANSPORT_TUNTAP_NAME "tuntap"
55#define TRANSPORT_TUNTAP_MTU ETH_MAX_PACKET
56
57struct tuntap_info {
58 char dev_name[IFNAMSIZ];
Chris Zankel7282bee2005-06-23 22:01:33 -070059 int fd;
60};
61
62/* ------------------------------------------------------------------------- */
63
64
65/* This structure contains out private information for the driver. */
66
67struct iss_net_private {
Chris Zankel7282bee2005-06-23 22:01:33 -070068 struct list_head device_list;
69 struct list_head opened_list;
70
71 spinlock_t lock;
72 struct net_device *dev;
73 struct platform_device pdev;
74 struct timer_list tl;
75 struct net_device_stats stats;
76
77 struct timer_list timer;
78 unsigned int timer_val;
79
80 int index;
81 int mtu;
82
Chris Zankel7282bee2005-06-23 22:01:33 -070083 struct {
84 union {
85 struct tuntap_info tuntap;
86 } info;
87
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);
94 } tp;
95
96};
97
Chris Zankel7282bee2005-06-23 22:01:33 -070098/* ================================ HELPERS ================================ */
99
100
101static char *split_if_spec(char *str, ...)
102{
103 char **arg, *end;
104 va_list ap;
105
106 va_start(ap, str);
107 while ((arg = va_arg(ap, char**)) != NULL) {
108 if (*str == '\0')
109 return NULL;
110 end = strchr(str, ',');
111 if (end != str)
112 *arg = str;
113 if (end == NULL)
114 return NULL;
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400115 *end++ = '\0';
Chris Zankel7282bee2005-06-23 22:01:33 -0700116 str = end;
117 }
118 va_end(ap);
119 return str;
120}
121
Chris Zankel7282bee2005-06-23 22:01:33 -0700122/* Set Ethernet address of the specified device. */
123
Max Filippov8991fd82013-11-10 08:48:34 +0400124static void setup_etheraddr(struct net_device *dev, char *str)
Chris Zankel7282bee2005-06-23 22:01:33 -0700125{
Max Filippov8991fd82013-11-10 08:48:34 +0400126 unsigned char *addr = dev->dev_addr;
Chris Zankel7282bee2005-06-23 22:01:33 -0700127
Max Filippov8991fd82013-11-10 08:48:34 +0400128 if (str == NULL)
129 goto random;
130
131 if (!mac_pton(str, addr)) {
132 pr_err("%s: failed to parse '%s' as an ethernet address\n",
133 dev->name, str);
134 goto random;
135 }
136 if (is_multicast_ether_addr(addr)) {
137 pr_err("%s: attempt to assign a multicast ethernet address\n",
138 dev->name);
139 goto random;
140 }
141 if (!is_valid_ether_addr(addr)) {
142 pr_err("%s: attempt to assign an invalid ethernet address\n",
143 dev->name);
144 goto random;
145 }
146 if (!is_local_ether_addr(addr))
147 pr_warn("%s: assigning a globally valid ethernet address\n",
148 dev->name);
149 return;
150
151random:
152 pr_info("%s: choosing a random ethernet address\n",
153 dev->name);
154 eth_hw_addr_random(dev);
155}
Chris Zankel7282bee2005-06-23 22:01:33 -0700156
157/* ======================= TUNTAP TRANSPORT INTERFACE ====================== */
158
159static int tuntap_open(struct iss_net_private *lp)
160{
161 struct ifreq ifr;
162 char *dev_name = lp->tp.info.tuntap.dev_name;
163 int err = -EINVAL;
164 int fd;
165
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400166 fd = simc_open("/dev/net/tun", 02, 0); /* O_RDWR */
167 if (fd < 0) {
Max Filippova6e16b92013-11-10 16:05:51 +0400168 pr_err("%s: failed to open /dev/net/tun, returned %d (errno = %d)\n",
169 lp->dev->name, fd, errno);
Chris Zankel7282bee2005-06-23 22:01:33 -0700170 return fd;
171 }
172
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400173 memset(&ifr, 0, sizeof(ifr));
Chris Zankel7282bee2005-06-23 22:01:33 -0700174 ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400175 strlcpy(ifr.ifr_name, dev_name, sizeof(ifr.ifr_name));
Chris Zankel7282bee2005-06-23 22:01:33 -0700176
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400177 err = simc_ioctl(fd, TUNSETIFF, &ifr);
178 if (err < 0) {
Max Filippova6e16b92013-11-10 16:05:51 +0400179 pr_err("%s: failed to set interface %s, returned %d (errno = %d)\n",
180 lp->dev->name, dev_name, err, errno);
Chris Zankel7282bee2005-06-23 22:01:33 -0700181 simc_close(fd);
182 return err;
183 }
184
185 lp->tp.info.tuntap.fd = fd;
186 return err;
187}
188
189static void tuntap_close(struct iss_net_private *lp)
190{
Chris Zankel7282bee2005-06-23 22:01:33 -0700191 simc_close(lp->tp.info.tuntap.fd);
192 lp->tp.info.tuntap.fd = -1;
193}
194
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400195static int tuntap_read(struct iss_net_private *lp, struct sk_buff **skb)
Chris Zankel7282bee2005-06-23 22:01:33 -0700196{
Chris Zankel7282bee2005-06-23 22:01:33 -0700197 return simc_read(lp->tp.info.tuntap.fd,
198 (*skb)->data, (*skb)->dev->mtu + ETH_HEADER_OTHER);
199}
200
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400201static int tuntap_write(struct iss_net_private *lp, struct sk_buff **skb)
Chris Zankel7282bee2005-06-23 22:01:33 -0700202{
203 return simc_write(lp->tp.info.tuntap.fd, (*skb)->data, (*skb)->len);
204}
205
206unsigned short tuntap_protocol(struct sk_buff *skb)
207{
208 return eth_type_trans(skb, skb->dev);
209}
210
211static int tuntap_poll(struct iss_net_private *lp)
212{
213 return simc_poll(lp->tp.info.tuntap.fd);
214}
215
216/*
Max Filippov35e14b42013-11-10 16:05:49 +0400217 * ethX=tuntap,[mac address],device name
Chris Zankel7282bee2005-06-23 22:01:33 -0700218 */
219
220static int tuntap_probe(struct iss_net_private *lp, int index, char *init)
221{
Max Filippov8991fd82013-11-10 08:48:34 +0400222 struct net_device *dev = lp->dev;
Chris Zankel7282bee2005-06-23 22:01:33 -0700223 char *dev_name = NULL, *mac_str = NULL, *rem = NULL;
224
225 /* Transport should be 'tuntap': ethX=tuntap,mac,dev_name */
226
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400227 if (strncmp(init, TRANSPORT_TUNTAP_NAME,
228 sizeof(TRANSPORT_TUNTAP_NAME) - 1))
Chris Zankel7282bee2005-06-23 22:01:33 -0700229 return 0;
230
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400231 init += sizeof(TRANSPORT_TUNTAP_NAME) - 1;
232 if (*init == ',') {
233 rem = split_if_spec(init + 1, &mac_str, &dev_name);
234 if (rem != NULL) {
Max Filippova6e16b92013-11-10 16:05:51 +0400235 pr_err("%s: extra garbage on specification : '%s'\n",
236 dev->name, rem);
Chris Zankel7282bee2005-06-23 22:01:33 -0700237 return 0;
238 }
239 } else if (*init != '\0') {
Max Filippova6e16b92013-11-10 16:05:51 +0400240 pr_err("%s: invalid argument: %s. Skipping device!\n",
241 dev->name, init);
Chris Zankel7282bee2005-06-23 22:01:33 -0700242 return 0;
243 }
244
Max Filippov35e14b42013-11-10 16:05:49 +0400245 if (!dev_name) {
246 pr_err("%s: missing tuntap device name\n", dev->name);
247 return 0;
248 }
249
250 strlcpy(lp->tp.info.tuntap.dev_name, dev_name,
251 sizeof(lp->tp.info.tuntap.dev_name));
Chris Zankel7282bee2005-06-23 22:01:33 -0700252
Max Filippov8991fd82013-11-10 08:48:34 +0400253 setup_etheraddr(dev, mac_str);
Chris Zankel7282bee2005-06-23 22:01:33 -0700254
Chris Zankel7282bee2005-06-23 22:01:33 -0700255 lp->mtu = TRANSPORT_TUNTAP_MTU;
256
Chris Zankel7282bee2005-06-23 22:01:33 -0700257 lp->tp.info.tuntap.fd = -1;
258
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;
265
Chris Zankel7282bee2005-06-23 22:01:33 -0700266 return 1;
267}
268
269/* ================================ ISS NET ================================ */
270
271static int iss_net_rx(struct net_device *dev)
272{
Wang Chen6cbeba52008-12-04 15:06:56 -0800273 struct iss_net_private *lp = netdev_priv(dev);
Chris Zankel7282bee2005-06-23 22:01:33 -0700274 int pkt_len;
275 struct sk_buff *skb;
276
277 /* Check if there is any new data. */
278
279 if (lp->tp.poll(lp) == 0)
280 return 0;
281
282 /* Try to allocate memory, if it fails, try again next round. */
283
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400284 skb = dev_alloc_skb(dev->mtu + 2 + ETH_HEADER_OTHER);
285 if (skb == NULL) {
Chris Zankel7282bee2005-06-23 22:01:33 -0700286 lp->stats.rx_dropped++;
287 return 0;
288 }
289
290 skb_reserve(skb, 2);
291
292 /* Setup skb */
293
294 skb->dev = dev;
Arnaldo Carvalho de Melo459a98e2007-03-19 15:30:44 -0700295 skb_reset_mac_header(skb);
Chris Zankel7282bee2005-06-23 22:01:33 -0700296 pkt_len = lp->tp.read(lp, &skb);
297 skb_put(skb, pkt_len);
298
299 if (pkt_len > 0) {
300 skb_trim(skb, pkt_len);
301 skb->protocol = lp->tp.protocol(skb);
Chris Zankel7282bee2005-06-23 22:01:33 -0700302
303 lp->stats.rx_bytes += skb->len;
304 lp->stats.rx_packets++;
Julia Lawall299f5902007-12-10 17:16:56 -0800305 netif_rx_ni(skb);
Chris Zankel7282bee2005-06-23 22:01:33 -0700306 return pkt_len;
307 }
308 kfree_skb(skb);
309 return pkt_len;
310}
311
312static int iss_net_poll(void)
313{
314 struct list_head *ele;
315 int err, ret = 0;
316
317 spin_lock(&opened_lock);
318
319 list_for_each(ele, &opened) {
320 struct iss_net_private *lp;
321
322 lp = list_entry(ele, struct iss_net_private, opened_list);
323
324 if (!netif_running(lp->dev))
325 break;
326
327 spin_lock(&lp->lock);
328
329 while ((err = iss_net_rx(lp->dev)) > 0)
330 ret++;
331
332 spin_unlock(&lp->lock);
333
334 if (err < 0) {
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400335 pr_err("Device '%s' read returned %d, shutting it down\n",
336 lp->dev->name, err);
Chris Zankel7282bee2005-06-23 22:01:33 -0700337 dev_close(lp->dev);
338 } else {
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400339 /* FIXME reactivate_fd(lp->fd, ISS_ETH_IRQ); */
Chris Zankel7282bee2005-06-23 22:01:33 -0700340 }
341 }
342
343 spin_unlock(&opened_lock);
344 return ret;
345}
346
347
348static void iss_net_timer(unsigned long priv)
349{
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400350 struct iss_net_private *lp = (struct iss_net_private *)priv;
Chris Zankel7282bee2005-06-23 22:01:33 -0700351
Chris Zankel7282bee2005-06-23 22:01:33 -0700352 iss_net_poll();
Max Filippov24e94452015-04-03 09:56:21 +0300353 spin_lock(&lp->lock);
Chris Zankel7282bee2005-06-23 22:01:33 -0700354 mod_timer(&lp->timer, jiffies + lp->timer_val);
Chris Zankel7282bee2005-06-23 22:01:33 -0700355 spin_unlock(&lp->lock);
356}
357
358
359static int iss_net_open(struct net_device *dev)
360{
Wang Chen6cbeba52008-12-04 15:06:56 -0800361 struct iss_net_private *lp = netdev_priv(dev);
Chris Zankel7282bee2005-06-23 22:01:33 -0700362 int err;
363
Max Filippov24e94452015-04-03 09:56:21 +0300364 spin_lock_bh(&lp->lock);
Chris Zankel7282bee2005-06-23 22:01:33 -0700365
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400366 err = lp->tp.open(lp);
367 if (err < 0)
Chris Zankel7282bee2005-06-23 22:01:33 -0700368 goto out;
369
Chris Zankel7282bee2005-06-23 22:01:33 -0700370 netif_start_queue(dev);
371
372 /* clear buffer - it can happen that the host side of the interface
Chris Zankel4af410a2007-05-31 17:43:40 -0700373 * is full when we get here. In this case, new data is never queued,
Chris Zankel7282bee2005-06-23 22:01:33 -0700374 * SIGIOs never arrive, and the net never works.
375 */
376 while ((err = iss_net_rx(dev)) > 0)
377 ;
378
Max Filippov24e94452015-04-03 09:56:21 +0300379 spin_unlock_bh(&lp->lock);
380 spin_lock_bh(&opened_lock);
Chris Zankel7282bee2005-06-23 22:01:33 -0700381 list_add(&lp->opened_list, &opened);
Max Filippov24e94452015-04-03 09:56:21 +0300382 spin_unlock_bh(&opened_lock);
383 spin_lock_bh(&lp->lock);
Chris Zankel7282bee2005-06-23 22:01:33 -0700384
385 init_timer(&lp->timer);
386 lp->timer_val = ISS_NET_TIMER_VALUE;
387 lp->timer.data = (unsigned long) lp;
388 lp->timer.function = iss_net_timer;
389 mod_timer(&lp->timer, jiffies + lp->timer_val);
390
391out:
Max Filippov24e94452015-04-03 09:56:21 +0300392 spin_unlock_bh(&lp->lock);
Chris Zankel7282bee2005-06-23 22:01:33 -0700393 return err;
394}
395
396static int iss_net_close(struct net_device *dev)
397{
Wang Chen6cbeba52008-12-04 15:06:56 -0800398 struct iss_net_private *lp = netdev_priv(dev);
Chris Zankel7282bee2005-06-23 22:01:33 -0700399 netif_stop_queue(dev);
Max Filippov24e94452015-04-03 09:56:21 +0300400 spin_lock_bh(&lp->lock);
Chris Zankel7282bee2005-06-23 22:01:33 -0700401
402 spin_lock(&opened_lock);
403 list_del(&opened);
404 spin_unlock(&opened_lock);
405
406 del_timer_sync(&lp->timer);
407
408 lp->tp.close(lp);
409
Max Filippov24e94452015-04-03 09:56:21 +0300410 spin_unlock_bh(&lp->lock);
Chris Zankel7282bee2005-06-23 22:01:33 -0700411 return 0;
412}
413
414static int iss_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
415{
Wang Chen6cbeba52008-12-04 15:06:56 -0800416 struct iss_net_private *lp = netdev_priv(dev);
Chris Zankel7282bee2005-06-23 22:01:33 -0700417 int len;
418
419 netif_stop_queue(dev);
Max Filippov24e94452015-04-03 09:56:21 +0300420 spin_lock_bh(&lp->lock);
Chris Zankel7282bee2005-06-23 22:01:33 -0700421
422 len = lp->tp.write(lp, &skb);
423
424 if (len == skb->len) {
425 lp->stats.tx_packets++;
426 lp->stats.tx_bytes += skb->len;
427 dev->trans_start = jiffies;
428 netif_start_queue(dev);
429
430 /* this is normally done in the interrupt when tx finishes */
431 netif_wake_queue(dev);
432
433 } else if (len == 0) {
434 netif_start_queue(dev);
435 lp->stats.tx_dropped++;
436
437 } else {
438 netif_start_queue(dev);
Max Filippova6e16b92013-11-10 16:05:51 +0400439 pr_err("%s: %s failed(%d)\n", dev->name, __func__, len);
Chris Zankel7282bee2005-06-23 22:01:33 -0700440 }
441
Max Filippov24e94452015-04-03 09:56:21 +0300442 spin_unlock_bh(&lp->lock);
Chris Zankel7282bee2005-06-23 22:01:33 -0700443
444 dev_kfree_skb(skb);
Patrick McHardyec634fe2009-07-05 19:23:38 -0700445 return NETDEV_TX_OK;
Chris Zankel7282bee2005-06-23 22:01:33 -0700446}
447
448
449static struct net_device_stats *iss_net_get_stats(struct net_device *dev)
450{
Wang Chen6cbeba52008-12-04 15:06:56 -0800451 struct iss_net_private *lp = netdev_priv(dev);
Chris Zankel7282bee2005-06-23 22:01:33 -0700452 return &lp->stats;
453}
454
455static void iss_net_set_multicast_list(struct net_device *dev)
456{
Chris Zankel7282bee2005-06-23 22:01:33 -0700457}
458
459static void iss_net_tx_timeout(struct net_device *dev)
460{
Chris Zankel7282bee2005-06-23 22:01:33 -0700461}
462
463static int iss_net_set_mac(struct net_device *dev, void *addr)
464{
Wang Chen6cbeba52008-12-04 15:06:56 -0800465 struct iss_net_private *lp = netdev_priv(dev);
Chris Zankel7282bee2005-06-23 22:01:33 -0700466 struct sockaddr *hwaddr = addr;
467
Max Filippovf6ac5a12013-11-10 16:05:48 +0400468 if (!is_valid_ether_addr(hwaddr->sa_data))
469 return -EADDRNOTAVAIL;
Max Filippov24e94452015-04-03 09:56:21 +0300470 spin_lock_bh(&lp->lock);
Chris Zankel7282bee2005-06-23 22:01:33 -0700471 memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
Max Filippov24e94452015-04-03 09:56:21 +0300472 spin_unlock_bh(&lp->lock);
Chris Zankel7282bee2005-06-23 22:01:33 -0700473 return 0;
474}
475
476static int iss_net_change_mtu(struct net_device *dev, int new_mtu)
477{
Chris Zankel7282bee2005-06-23 22:01:33 -0700478 return -EINVAL;
479}
480
Chris Zankel7282bee2005-06-23 22:01:33 -0700481void iss_net_user_timer_expire(unsigned long _conn)
482{
483}
484
485
Russell King3ae5eae2005-11-09 22:32:44 +0000486static struct platform_driver iss_net_driver = {
487 .driver = {
488 .name = DRIVER_NAME,
489 },
Chris Zankel7282bee2005-06-23 22:01:33 -0700490};
491
492static int driver_registered;
493
Chris Zankel0a972462010-05-02 08:37:20 -0700494static const struct net_device_ops iss_netdev_ops = {
495 .ndo_open = iss_net_open,
496 .ndo_stop = iss_net_close,
497 .ndo_get_stats = iss_net_get_stats,
498 .ndo_start_xmit = iss_net_start_xmit,
499 .ndo_validate_addr = eth_validate_addr,
500 .ndo_change_mtu = iss_net_change_mtu,
501 .ndo_set_mac_address = iss_net_set_mac,
Chris Zankel0a972462010-05-02 08:37:20 -0700502 .ndo_tx_timeout = iss_net_tx_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +0000503 .ndo_set_rx_mode = iss_net_set_multicast_list,
Chris Zankel0a972462010-05-02 08:37:20 -0700504};
505
Chris Zankel7282bee2005-06-23 22:01:33 -0700506static int iss_net_configure(int index, char *init)
507{
508 struct net_device *dev;
509 struct iss_net_private *lp;
510 int err;
511
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400512 dev = alloc_etherdev(sizeof(*lp));
513 if (dev == NULL) {
514 pr_err("eth_configure: failed to allocate device\n");
Chris Zankel7282bee2005-06-23 22:01:33 -0700515 return 1;
516 }
517
518 /* Initialize private element. */
519
Wang Chen6cbeba52008-12-04 15:06:56 -0800520 lp = netdev_priv(dev);
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400521 *lp = (struct iss_net_private) {
Chris Zankel7282bee2005-06-23 22:01:33 -0700522 .device_list = LIST_HEAD_INIT(lp->device_list),
523 .opened_list = LIST_HEAD_INIT(lp->opened_list),
Chris Zankel7282bee2005-06-23 22:01:33 -0700524 .dev = dev,
525 .index = index,
Max Filippov24e94452015-04-03 09:56:21 +0300526 };
Chris Zankel7282bee2005-06-23 22:01:33 -0700527
Max Filippov24e94452015-04-03 09:56:21 +0300528 spin_lock_init(&lp->lock);
Chris Zankel7282bee2005-06-23 22:01:33 -0700529 /*
Max Filippov358b1812013-11-10 16:05:46 +0400530 * If this name ends up conflicting with an existing registered
531 * netdevice, that is OK, register_netdev{,ice}() will notice this
532 * and fail.
533 */
534 snprintf(dev->name, sizeof(dev->name), "eth%d", index);
535
536 /*
Chris Zankel7282bee2005-06-23 22:01:33 -0700537 * Try all transport protocols.
538 * Note: more protocols can be added by adding '&& !X_init(lp, eth)'.
539 */
540
541 if (!tuntap_probe(lp, index, init)) {
Max Filippova6e16b92013-11-10 16:05:51 +0400542 pr_err("%s: invalid arguments. Skipping device!\n",
543 dev->name);
Chris Zankel7282bee2005-06-23 22:01:33 -0700544 goto errout;
545 }
546
Max Filippov8991fd82013-11-10 08:48:34 +0400547 pr_info("Netdevice %d (%pM)\n", index, dev->dev_addr);
Chris Zankel7282bee2005-06-23 22:01:33 -0700548
549 /* sysfs register */
550
551 if (!driver_registered) {
Russell King3ae5eae2005-11-09 22:32:44 +0000552 platform_driver_register(&iss_net_driver);
Chris Zankel7282bee2005-06-23 22:01:33 -0700553 driver_registered = 1;
554 }
555
556 spin_lock(&devices_lock);
557 list_add(&lp->device_list, &devices);
558 spin_unlock(&devices_lock);
559
560 lp->pdev.id = index;
561 lp->pdev.name = DRIVER_NAME;
562 platform_device_register(&lp->pdev);
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400563 SET_NETDEV_DEV(dev, &lp->pdev.dev);
Chris Zankel7282bee2005-06-23 22:01:33 -0700564
Chris Zankel0a972462010-05-02 08:37:20 -0700565 dev->netdev_ops = &iss_netdev_ops;
Chris Zankel7282bee2005-06-23 22:01:33 -0700566 dev->mtu = lp->mtu;
Chris Zankel7282bee2005-06-23 22:01:33 -0700567 dev->watchdog_timeo = (HZ >> 1);
568 dev->irq = -1;
569
570 rtnl_lock();
571 err = register_netdevice(dev);
572 rtnl_unlock();
573
574 if (err) {
Max Filippova6e16b92013-11-10 16:05:51 +0400575 pr_err("%s: error registering net device!\n", dev->name);
Chris Zankel7282bee2005-06-23 22:01:33 -0700576 /* XXX: should we call ->remove() here? */
577 free_netdev(dev);
578 return 1;
579 }
580
581 init_timer(&lp->tl);
582 lp->tl.function = iss_net_user_timer_expire;
583
Chris Zankel7282bee2005-06-23 22:01:33 -0700584 return 0;
585
586errout:
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400587 /* FIXME: unregister; free, etc.. */
Chris Zankel7282bee2005-06-23 22:01:33 -0700588 return -EIO;
Chris Zankel7282bee2005-06-23 22:01:33 -0700589}
590
591/* ------------------------------------------------------------------------- */
592
593/* Filled in during early boot */
594
595struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
596
597struct iss_net_init {
598 struct list_head list;
599 char *init; /* init string */
600 int index;
601};
602
603/*
604 * Parse the command line and look for 'ethX=...' fields, and register all
605 * those fields. They will be later initialized in iss_net_init.
606 */
607
608#define ERR KERN_ERR "iss_net_setup: "
609
Max Filippovdc2bffa2013-05-27 18:58:24 +0400610static int __init iss_net_setup(char *str)
Chris Zankel7282bee2005-06-23 22:01:33 -0700611{
612 struct iss_net_private *device = NULL;
613 struct iss_net_init *new;
614 struct list_head *ele;
615 char *end;
Max Filippov8be54d72013-12-01 10:16:56 +0400616 int rc;
617 unsigned n;
Chris Zankel7282bee2005-06-23 22:01:33 -0700618
Max Filippov8be54d72013-12-01 10:16:56 +0400619 end = strchr(str, '=');
620 if (!end) {
Chris Zankel7282bee2005-06-23 22:01:33 -0700621 printk(ERR "Expected '=' after device number\n");
622 return 1;
623 }
Max Filippov8be54d72013-12-01 10:16:56 +0400624 *end = 0;
625 rc = kstrtouint(str, 0, &n);
626 *end = '=';
627 if (rc < 0) {
628 printk(ERR "Failed to parse '%s'\n", str);
629 return 1;
630 }
631 str = end;
Chris Zankel7282bee2005-06-23 22:01:33 -0700632
633 spin_lock(&devices_lock);
634
635 list_for_each(ele, &devices) {
636 device = list_entry(ele, struct iss_net_private, device_list);
637 if (device->index == n)
638 break;
639 }
640
641 spin_unlock(&devices_lock);
642
643 if (device && device->index == n) {
Max Filippov8be54d72013-12-01 10:16:56 +0400644 printk(ERR "Device %u already configured\n", n);
Chris Zankel7282bee2005-06-23 22:01:33 -0700645 return 1;
646 }
647
Thomas Meyerf447fd32013-09-19 23:42:22 +0200648 new = alloc_bootmem(sizeof(*new));
649 if (new == NULL) {
Max Filippov3f3cd60b2013-11-10 16:05:44 +0400650 printk(ERR "Alloc_bootmem failed\n");
Chris Zankel7282bee2005-06-23 22:01:33 -0700651 return 1;
652 }
653
654 INIT_LIST_HEAD(&new->list);
655 new->index = n;
656 new->init = str + 1;
657
658 list_add_tail(&new->list, &eth_cmd_line);
659 return 1;
660}
661
662#undef ERR
663
Max Filippov8c8ad852013-11-10 16:05:45 +0400664__setup("eth", iss_net_setup);
Chris Zankel7282bee2005-06-23 22:01:33 -0700665
666/*
667 * Initialize all ISS Ethernet devices previously registered in iss_net_setup.
668 */
669
670static int iss_net_init(void)
671{
672 struct list_head *ele, *next;
673
674 /* Walk through all Ethernet devices specified in the command line. */
675
676 list_for_each_safe(ele, next, &eth_cmd_line) {
677 struct iss_net_init *eth;
678 eth = list_entry(ele, struct iss_net_init, list);
679 iss_net_configure(eth->index, eth->init);
680 }
681
682 return 1;
683}
684
685module_init(iss_net_init);
686