network.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  1. /*
  2. *
  3. * arch/xtensa/platforms/iss/network.c
  4. *
  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. #define pr_fmt(fmt) "%s: " fmt, __func__
  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>
  35. #include <platform/simcall.h>
  36. #define DRIVER_NAME "iss-netdev"
  37. #define ETH_MAX_PACKET 1500
  38. #define ETH_HEADER_OTHER 14
  39. #define ISS_NET_TIMER_VALUE (HZ / 10)
  40. static DEFINE_SPINLOCK(opened_lock);
  41. static LIST_HEAD(opened);
  42. static DEFINE_SPINLOCK(devices_lock);
  43. static LIST_HEAD(devices);
  44. /* ------------------------------------------------------------------------- */
  45. /* We currently only support the TUNTAP transport protocol. */
  46. #define TRANSPORT_TUNTAP_NAME "tuntap"
  47. #define TRANSPORT_TUNTAP_MTU ETH_MAX_PACKET
  48. struct tuntap_info {
  49. char dev_name[IFNAMSIZ];
  50. int fd;
  51. };
  52. /* ------------------------------------------------------------------------- */
  53. /* This structure contains out private information for the driver. */
  54. struct iss_net_private {
  55. struct list_head device_list;
  56. struct list_head opened_list;
  57. spinlock_t lock;
  58. struct net_device *dev;
  59. struct platform_device pdev;
  60. struct timer_list tl;
  61. struct net_device_stats stats;
  62. struct timer_list timer;
  63. unsigned int timer_val;
  64. int index;
  65. int mtu;
  66. struct {
  67. union {
  68. struct tuntap_info tuntap;
  69. } info;
  70. int (*open)(struct iss_net_private *lp);
  71. void (*close)(struct iss_net_private *lp);
  72. int (*read)(struct iss_net_private *lp, struct sk_buff **skb);
  73. int (*write)(struct iss_net_private *lp, struct sk_buff **skb);
  74. unsigned short (*protocol)(struct sk_buff *skb);
  75. int (*poll)(struct iss_net_private *lp);
  76. } tp;
  77. };
  78. /* ================================ HELPERS ================================ */
  79. static char *split_if_spec(char *str, ...)
  80. {
  81. char **arg, *end;
  82. va_list ap;
  83. va_start(ap, str);
  84. while ((arg = va_arg(ap, char**)) != NULL) {
  85. if (*str == '\0') {
  86. va_end(ap);
  87. return NULL;
  88. }
  89. end = strchr(str, ',');
  90. if (end != str)
  91. *arg = str;
  92. if (end == NULL) {
  93. va_end(ap);
  94. return NULL;
  95. }
  96. *end++ = '\0';
  97. str = end;
  98. }
  99. va_end(ap);
  100. return str;
  101. }
  102. /* Set Ethernet address of the specified device. */
  103. static void setup_etheraddr(struct net_device *dev, char *str)
  104. {
  105. unsigned char *addr = dev->dev_addr;
  106. if (str == NULL)
  107. goto random;
  108. if (!mac_pton(str, addr)) {
  109. pr_err("%s: failed to parse '%s' as an ethernet address\n",
  110. dev->name, str);
  111. goto random;
  112. }
  113. if (is_multicast_ether_addr(addr)) {
  114. pr_err("%s: attempt to assign a multicast ethernet address\n",
  115. dev->name);
  116. goto random;
  117. }
  118. if (!is_valid_ether_addr(addr)) {
  119. pr_err("%s: attempt to assign an invalid ethernet address\n",
  120. dev->name);
  121. goto random;
  122. }
  123. if (!is_local_ether_addr(addr))
  124. pr_warn("%s: assigning a globally valid ethernet address\n",
  125. dev->name);
  126. return;
  127. random:
  128. pr_info("%s: choosing a random ethernet address\n",
  129. dev->name);
  130. eth_hw_addr_random(dev);
  131. }
  132. /* ======================= TUNTAP TRANSPORT INTERFACE ====================== */
  133. static int tuntap_open(struct iss_net_private *lp)
  134. {
  135. struct ifreq ifr;
  136. char *dev_name = lp->tp.info.tuntap.dev_name;
  137. int err = -EINVAL;
  138. int fd;
  139. fd = simc_open("/dev/net/tun", 02, 0); /* O_RDWR */
  140. if (fd < 0) {
  141. pr_err("%s: failed to open /dev/net/tun, returned %d (errno = %d)\n",
  142. lp->dev->name, fd, errno);
  143. return fd;
  144. }
  145. memset(&ifr, 0, sizeof(ifr));
  146. ifr.ifr_flags = IFF_TAP | IFF_NO_PI;
  147. strlcpy(ifr.ifr_name, dev_name, sizeof(ifr.ifr_name));
  148. err = simc_ioctl(fd, TUNSETIFF, &ifr);
  149. if (err < 0) {
  150. pr_err("%s: failed to set interface %s, returned %d (errno = %d)\n",
  151. lp->dev->name, dev_name, err, errno);
  152. simc_close(fd);
  153. return err;
  154. }
  155. lp->tp.info.tuntap.fd = fd;
  156. return err;
  157. }
  158. static void tuntap_close(struct iss_net_private *lp)
  159. {
  160. simc_close(lp->tp.info.tuntap.fd);
  161. lp->tp.info.tuntap.fd = -1;
  162. }
  163. static int tuntap_read(struct iss_net_private *lp, struct sk_buff **skb)
  164. {
  165. return simc_read(lp->tp.info.tuntap.fd,
  166. (*skb)->data, (*skb)->dev->mtu + ETH_HEADER_OTHER);
  167. }
  168. static int tuntap_write(struct iss_net_private *lp, struct sk_buff **skb)
  169. {
  170. return simc_write(lp->tp.info.tuntap.fd, (*skb)->data, (*skb)->len);
  171. }
  172. unsigned short tuntap_protocol(struct sk_buff *skb)
  173. {
  174. return eth_type_trans(skb, skb->dev);
  175. }
  176. static int tuntap_poll(struct iss_net_private *lp)
  177. {
  178. return simc_poll(lp->tp.info.tuntap.fd);
  179. }
  180. /*
  181. * ethX=tuntap,[mac address],device name
  182. */
  183. static int tuntap_probe(struct iss_net_private *lp, int index, char *init)
  184. {
  185. struct net_device *dev = lp->dev;
  186. char *dev_name = NULL, *mac_str = NULL, *rem = NULL;
  187. /* Transport should be 'tuntap': ethX=tuntap,mac,dev_name */
  188. if (strncmp(init, TRANSPORT_TUNTAP_NAME,
  189. sizeof(TRANSPORT_TUNTAP_NAME) - 1))
  190. return 0;
  191. init += sizeof(TRANSPORT_TUNTAP_NAME) - 1;
  192. if (*init == ',') {
  193. rem = split_if_spec(init + 1, &mac_str, &dev_name);
  194. if (rem != NULL) {
  195. pr_err("%s: extra garbage on specification : '%s'\n",
  196. dev->name, rem);
  197. return 0;
  198. }
  199. } else if (*init != '\0') {
  200. pr_err("%s: invalid argument: %s. Skipping device!\n",
  201. dev->name, init);
  202. return 0;
  203. }
  204. if (!dev_name) {
  205. pr_err("%s: missing tuntap device name\n", dev->name);
  206. return 0;
  207. }
  208. strlcpy(lp->tp.info.tuntap.dev_name, dev_name,
  209. sizeof(lp->tp.info.tuntap.dev_name));
  210. setup_etheraddr(dev, mac_str);
  211. lp->mtu = TRANSPORT_TUNTAP_MTU;
  212. lp->tp.info.tuntap.fd = -1;
  213. lp->tp.open = tuntap_open;
  214. lp->tp.close = tuntap_close;
  215. lp->tp.read = tuntap_read;
  216. lp->tp.write = tuntap_write;
  217. lp->tp.protocol = tuntap_protocol;
  218. lp->tp.poll = tuntap_poll;
  219. return 1;
  220. }
  221. /* ================================ ISS NET ================================ */
  222. static int iss_net_rx(struct net_device *dev)
  223. {
  224. struct iss_net_private *lp = netdev_priv(dev);
  225. int pkt_len;
  226. struct sk_buff *skb;
  227. /* Check if there is any new data. */
  228. if (lp->tp.poll(lp) == 0)
  229. return 0;
  230. /* Try to allocate memory, if it fails, try again next round. */
  231. skb = dev_alloc_skb(dev->mtu + 2 + ETH_HEADER_OTHER);
  232. if (skb == NULL) {
  233. lp->stats.rx_dropped++;
  234. return 0;
  235. }
  236. skb_reserve(skb, 2);
  237. /* Setup skb */
  238. skb->dev = dev;
  239. skb_reset_mac_header(skb);
  240. pkt_len = lp->tp.read(lp, &skb);
  241. skb_put(skb, pkt_len);
  242. if (pkt_len > 0) {
  243. skb_trim(skb, pkt_len);
  244. skb->protocol = lp->tp.protocol(skb);
  245. lp->stats.rx_bytes += skb->len;
  246. lp->stats.rx_packets++;
  247. netif_rx_ni(skb);
  248. return pkt_len;
  249. }
  250. kfree_skb(skb);
  251. return pkt_len;
  252. }
  253. static int iss_net_poll(void)
  254. {
  255. struct list_head *ele;
  256. int err, ret = 0;
  257. spin_lock(&opened_lock);
  258. list_for_each(ele, &opened) {
  259. struct iss_net_private *lp;
  260. lp = list_entry(ele, struct iss_net_private, opened_list);
  261. if (!netif_running(lp->dev))
  262. break;
  263. spin_lock(&lp->lock);
  264. while ((err = iss_net_rx(lp->dev)) > 0)
  265. ret++;
  266. spin_unlock(&lp->lock);
  267. if (err < 0) {
  268. pr_err("Device '%s' read returned %d, shutting it down\n",
  269. lp->dev->name, err);
  270. dev_close(lp->dev);
  271. } else {
  272. /* FIXME reactivate_fd(lp->fd, ISS_ETH_IRQ); */
  273. }
  274. }
  275. spin_unlock(&opened_lock);
  276. return ret;
  277. }
  278. static void iss_net_timer(struct timer_list *t)
  279. {
  280. struct iss_net_private *lp = from_timer(lp, t, timer);
  281. iss_net_poll();
  282. spin_lock(&lp->lock);
  283. mod_timer(&lp->timer, jiffies + lp->timer_val);
  284. spin_unlock(&lp->lock);
  285. }
  286. static int iss_net_open(struct net_device *dev)
  287. {
  288. struct iss_net_private *lp = netdev_priv(dev);
  289. int err;
  290. spin_lock_bh(&lp->lock);
  291. err = lp->tp.open(lp);
  292. if (err < 0)
  293. goto out;
  294. netif_start_queue(dev);
  295. /* clear buffer - it can happen that the host side of the interface
  296. * is full when we get here. In this case, new data is never queued,
  297. * SIGIOs never arrive, and the net never works.
  298. */
  299. while ((err = iss_net_rx(dev)) > 0)
  300. ;
  301. spin_unlock_bh(&lp->lock);
  302. spin_lock_bh(&opened_lock);
  303. list_add(&lp->opened_list, &opened);
  304. spin_unlock_bh(&opened_lock);
  305. spin_lock_bh(&lp->lock);
  306. timer_setup(&lp->timer, iss_net_timer, 0);
  307. lp->timer_val = ISS_NET_TIMER_VALUE;
  308. mod_timer(&lp->timer, jiffies + lp->timer_val);
  309. out:
  310. spin_unlock_bh(&lp->lock);
  311. return err;
  312. }
  313. static int iss_net_close(struct net_device *dev)
  314. {
  315. struct iss_net_private *lp = netdev_priv(dev);
  316. netif_stop_queue(dev);
  317. spin_lock_bh(&lp->lock);
  318. spin_lock(&opened_lock);
  319. list_del(&opened);
  320. spin_unlock(&opened_lock);
  321. del_timer_sync(&lp->timer);
  322. lp->tp.close(lp);
  323. spin_unlock_bh(&lp->lock);
  324. return 0;
  325. }
  326. static int iss_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
  327. {
  328. struct iss_net_private *lp = netdev_priv(dev);
  329. int len;
  330. netif_stop_queue(dev);
  331. spin_lock_bh(&lp->lock);
  332. len = lp->tp.write(lp, &skb);
  333. if (len == skb->len) {
  334. lp->stats.tx_packets++;
  335. lp->stats.tx_bytes += skb->len;
  336. netif_trans_update(dev);
  337. netif_start_queue(dev);
  338. /* this is normally done in the interrupt when tx finishes */
  339. netif_wake_queue(dev);
  340. } else if (len == 0) {
  341. netif_start_queue(dev);
  342. lp->stats.tx_dropped++;
  343. } else {
  344. netif_start_queue(dev);
  345. pr_err("%s: %s failed(%d)\n", dev->name, __func__, len);
  346. }
  347. spin_unlock_bh(&lp->lock);
  348. dev_kfree_skb(skb);
  349. return NETDEV_TX_OK;
  350. }
  351. static struct net_device_stats *iss_net_get_stats(struct net_device *dev)
  352. {
  353. struct iss_net_private *lp = netdev_priv(dev);
  354. return &lp->stats;
  355. }
  356. static void iss_net_set_multicast_list(struct net_device *dev)
  357. {
  358. }
  359. static void iss_net_tx_timeout(struct net_device *dev)
  360. {
  361. }
  362. static int iss_net_set_mac(struct net_device *dev, void *addr)
  363. {
  364. struct iss_net_private *lp = netdev_priv(dev);
  365. struct sockaddr *hwaddr = addr;
  366. if (!is_valid_ether_addr(hwaddr->sa_data))
  367. return -EADDRNOTAVAIL;
  368. spin_lock_bh(&lp->lock);
  369. memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
  370. spin_unlock_bh(&lp->lock);
  371. return 0;
  372. }
  373. static int iss_net_change_mtu(struct net_device *dev, int new_mtu)
  374. {
  375. return -EINVAL;
  376. }
  377. void iss_net_user_timer_expire(struct timer_list *unused)
  378. {
  379. }
  380. static struct platform_driver iss_net_driver = {
  381. .driver = {
  382. .name = DRIVER_NAME,
  383. },
  384. };
  385. static int driver_registered;
  386. static const struct net_device_ops iss_netdev_ops = {
  387. .ndo_open = iss_net_open,
  388. .ndo_stop = iss_net_close,
  389. .ndo_get_stats = iss_net_get_stats,
  390. .ndo_start_xmit = iss_net_start_xmit,
  391. .ndo_validate_addr = eth_validate_addr,
  392. .ndo_change_mtu = iss_net_change_mtu,
  393. .ndo_set_mac_address = iss_net_set_mac,
  394. .ndo_tx_timeout = iss_net_tx_timeout,
  395. .ndo_set_rx_mode = iss_net_set_multicast_list,
  396. };
  397. static int iss_net_configure(int index, char *init)
  398. {
  399. struct net_device *dev;
  400. struct iss_net_private *lp;
  401. int err;
  402. dev = alloc_etherdev(sizeof(*lp));
  403. if (dev == NULL) {
  404. pr_err("eth_configure: failed to allocate device\n");
  405. return 1;
  406. }
  407. /* Initialize private element. */
  408. lp = netdev_priv(dev);
  409. *lp = (struct iss_net_private) {
  410. .device_list = LIST_HEAD_INIT(lp->device_list),
  411. .opened_list = LIST_HEAD_INIT(lp->opened_list),
  412. .dev = dev,
  413. .index = index,
  414. };
  415. spin_lock_init(&lp->lock);
  416. /*
  417. * If this name ends up conflicting with an existing registered
  418. * netdevice, that is OK, register_netdev{,ice}() will notice this
  419. * and fail.
  420. */
  421. snprintf(dev->name, sizeof(dev->name), "eth%d", index);
  422. /*
  423. * Try all transport protocols.
  424. * Note: more protocols can be added by adding '&& !X_init(lp, eth)'.
  425. */
  426. if (!tuntap_probe(lp, index, init)) {
  427. pr_err("%s: invalid arguments. Skipping device!\n",
  428. dev->name);
  429. goto errout;
  430. }
  431. pr_info("Netdevice %d (%pM)\n", index, dev->dev_addr);
  432. /* sysfs register */
  433. if (!driver_registered) {
  434. platform_driver_register(&iss_net_driver);
  435. driver_registered = 1;
  436. }
  437. spin_lock(&devices_lock);
  438. list_add(&lp->device_list, &devices);
  439. spin_unlock(&devices_lock);
  440. lp->pdev.id = index;
  441. lp->pdev.name = DRIVER_NAME;
  442. platform_device_register(&lp->pdev);
  443. SET_NETDEV_DEV(dev, &lp->pdev.dev);
  444. dev->netdev_ops = &iss_netdev_ops;
  445. dev->mtu = lp->mtu;
  446. dev->watchdog_timeo = (HZ >> 1);
  447. dev->irq = -1;
  448. rtnl_lock();
  449. err = register_netdevice(dev);
  450. rtnl_unlock();
  451. if (err) {
  452. pr_err("%s: error registering net device!\n", dev->name);
  453. /* XXX: should we call ->remove() here? */
  454. free_netdev(dev);
  455. return 1;
  456. }
  457. timer_setup(&lp->tl, iss_net_user_timer_expire, 0);
  458. return 0;
  459. errout:
  460. /* FIXME: unregister; free, etc.. */
  461. return -EIO;
  462. }
  463. /* ------------------------------------------------------------------------- */
  464. /* Filled in during early boot */
  465. struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
  466. struct iss_net_init {
  467. struct list_head list;
  468. char *init; /* init string */
  469. int index;
  470. };
  471. /*
  472. * Parse the command line and look for 'ethX=...' fields, and register all
  473. * those fields. They will be later initialized in iss_net_init.
  474. */
  475. static int __init iss_net_setup(char *str)
  476. {
  477. struct iss_net_private *device = NULL;
  478. struct iss_net_init *new;
  479. struct list_head *ele;
  480. char *end;
  481. int rc;
  482. unsigned n;
  483. end = strchr(str, '=');
  484. if (!end) {
  485. pr_err("Expected '=' after device number\n");
  486. return 1;
  487. }
  488. *end = 0;
  489. rc = kstrtouint(str, 0, &n);
  490. *end = '=';
  491. if (rc < 0) {
  492. pr_err("Failed to parse '%s'\n", str);
  493. return 1;
  494. }
  495. str = end;
  496. spin_lock(&devices_lock);
  497. list_for_each(ele, &devices) {
  498. device = list_entry(ele, struct iss_net_private, device_list);
  499. if (device->index == n)
  500. break;
  501. }
  502. spin_unlock(&devices_lock);
  503. if (device && device->index == n) {
  504. pr_err("Device %u already configured\n", n);
  505. return 1;
  506. }
  507. new = memblock_alloc(sizeof(*new), 0);
  508. if (new == NULL) {
  509. pr_err("Alloc_bootmem failed\n");
  510. return 1;
  511. }
  512. INIT_LIST_HEAD(&new->list);
  513. new->index = n;
  514. new->init = str + 1;
  515. list_add_tail(&new->list, &eth_cmd_line);
  516. return 1;
  517. }
  518. __setup("eth", iss_net_setup);
  519. /*
  520. * Initialize all ISS Ethernet devices previously registered in iss_net_setup.
  521. */
  522. static int iss_net_init(void)
  523. {
  524. struct list_head *ele, *next;
  525. /* Walk through all Ethernet devices specified in the command line. */
  526. list_for_each_safe(ele, next, &eth_cmd_line) {
  527. struct iss_net_init *eth;
  528. eth = list_entry(ele, struct iss_net_init, list);
  529. iss_net_configure(eth->index, eth->init);
  530. }
  531. return 1;
  532. }
  533. device_initcall(iss_net_init);