network.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686
  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(unsigned long priv)
  279. {
  280. struct iss_net_private *lp = (struct iss_net_private *)priv;
  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. init_timer(&lp->timer);
  307. lp->timer_val = ISS_NET_TIMER_VALUE;
  308. lp->timer.data = (unsigned long) lp;
  309. lp->timer.function = iss_net_timer;
  310. mod_timer(&lp->timer, jiffies + lp->timer_val);
  311. out:
  312. spin_unlock_bh(&lp->lock);
  313. return err;
  314. }
  315. static int iss_net_close(struct net_device *dev)
  316. {
  317. struct iss_net_private *lp = netdev_priv(dev);
  318. netif_stop_queue(dev);
  319. spin_lock_bh(&lp->lock);
  320. spin_lock(&opened_lock);
  321. list_del(&opened);
  322. spin_unlock(&opened_lock);
  323. del_timer_sync(&lp->timer);
  324. lp->tp.close(lp);
  325. spin_unlock_bh(&lp->lock);
  326. return 0;
  327. }
  328. static int iss_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
  329. {
  330. struct iss_net_private *lp = netdev_priv(dev);
  331. int len;
  332. netif_stop_queue(dev);
  333. spin_lock_bh(&lp->lock);
  334. len = lp->tp.write(lp, &skb);
  335. if (len == skb->len) {
  336. lp->stats.tx_packets++;
  337. lp->stats.tx_bytes += skb->len;
  338. netif_trans_update(dev);
  339. netif_start_queue(dev);
  340. /* this is normally done in the interrupt when tx finishes */
  341. netif_wake_queue(dev);
  342. } else if (len == 0) {
  343. netif_start_queue(dev);
  344. lp->stats.tx_dropped++;
  345. } else {
  346. netif_start_queue(dev);
  347. pr_err("%s: %s failed(%d)\n", dev->name, __func__, len);
  348. }
  349. spin_unlock_bh(&lp->lock);
  350. dev_kfree_skb(skb);
  351. return NETDEV_TX_OK;
  352. }
  353. static struct net_device_stats *iss_net_get_stats(struct net_device *dev)
  354. {
  355. struct iss_net_private *lp = netdev_priv(dev);
  356. return &lp->stats;
  357. }
  358. static void iss_net_set_multicast_list(struct net_device *dev)
  359. {
  360. }
  361. static void iss_net_tx_timeout(struct net_device *dev)
  362. {
  363. }
  364. static int iss_net_set_mac(struct net_device *dev, void *addr)
  365. {
  366. struct iss_net_private *lp = netdev_priv(dev);
  367. struct sockaddr *hwaddr = addr;
  368. if (!is_valid_ether_addr(hwaddr->sa_data))
  369. return -EADDRNOTAVAIL;
  370. spin_lock_bh(&lp->lock);
  371. memcpy(dev->dev_addr, hwaddr->sa_data, ETH_ALEN);
  372. spin_unlock_bh(&lp->lock);
  373. return 0;
  374. }
  375. static int iss_net_change_mtu(struct net_device *dev, int new_mtu)
  376. {
  377. return -EINVAL;
  378. }
  379. void iss_net_user_timer_expire(unsigned long _conn)
  380. {
  381. }
  382. static struct platform_driver iss_net_driver = {
  383. .driver = {
  384. .name = DRIVER_NAME,
  385. },
  386. };
  387. static int driver_registered;
  388. static const struct net_device_ops iss_netdev_ops = {
  389. .ndo_open = iss_net_open,
  390. .ndo_stop = iss_net_close,
  391. .ndo_get_stats = iss_net_get_stats,
  392. .ndo_start_xmit = iss_net_start_xmit,
  393. .ndo_validate_addr = eth_validate_addr,
  394. .ndo_change_mtu = iss_net_change_mtu,
  395. .ndo_set_mac_address = iss_net_set_mac,
  396. .ndo_tx_timeout = iss_net_tx_timeout,
  397. .ndo_set_rx_mode = iss_net_set_multicast_list,
  398. };
  399. static int iss_net_configure(int index, char *init)
  400. {
  401. struct net_device *dev;
  402. struct iss_net_private *lp;
  403. int err;
  404. dev = alloc_etherdev(sizeof(*lp));
  405. if (dev == NULL) {
  406. pr_err("eth_configure: failed to allocate device\n");
  407. return 1;
  408. }
  409. /* Initialize private element. */
  410. lp = netdev_priv(dev);
  411. *lp = (struct iss_net_private) {
  412. .device_list = LIST_HEAD_INIT(lp->device_list),
  413. .opened_list = LIST_HEAD_INIT(lp->opened_list),
  414. .dev = dev,
  415. .index = index,
  416. };
  417. spin_lock_init(&lp->lock);
  418. /*
  419. * If this name ends up conflicting with an existing registered
  420. * netdevice, that is OK, register_netdev{,ice}() will notice this
  421. * and fail.
  422. */
  423. snprintf(dev->name, sizeof(dev->name), "eth%d", index);
  424. /*
  425. * Try all transport protocols.
  426. * Note: more protocols can be added by adding '&& !X_init(lp, eth)'.
  427. */
  428. if (!tuntap_probe(lp, index, init)) {
  429. pr_err("%s: invalid arguments. Skipping device!\n",
  430. dev->name);
  431. goto errout;
  432. }
  433. pr_info("Netdevice %d (%pM)\n", index, dev->dev_addr);
  434. /* sysfs register */
  435. if (!driver_registered) {
  436. platform_driver_register(&iss_net_driver);
  437. driver_registered = 1;
  438. }
  439. spin_lock(&devices_lock);
  440. list_add(&lp->device_list, &devices);
  441. spin_unlock(&devices_lock);
  442. lp->pdev.id = index;
  443. lp->pdev.name = DRIVER_NAME;
  444. platform_device_register(&lp->pdev);
  445. SET_NETDEV_DEV(dev, &lp->pdev.dev);
  446. dev->netdev_ops = &iss_netdev_ops;
  447. dev->mtu = lp->mtu;
  448. dev->watchdog_timeo = (HZ >> 1);
  449. dev->irq = -1;
  450. rtnl_lock();
  451. err = register_netdevice(dev);
  452. rtnl_unlock();
  453. if (err) {
  454. pr_err("%s: error registering net device!\n", dev->name);
  455. /* XXX: should we call ->remove() here? */
  456. free_netdev(dev);
  457. return 1;
  458. }
  459. init_timer(&lp->tl);
  460. lp->tl.function = iss_net_user_timer_expire;
  461. return 0;
  462. errout:
  463. /* FIXME: unregister; free, etc.. */
  464. return -EIO;
  465. }
  466. /* ------------------------------------------------------------------------- */
  467. /* Filled in during early boot */
  468. struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
  469. struct iss_net_init {
  470. struct list_head list;
  471. char *init; /* init string */
  472. int index;
  473. };
  474. /*
  475. * Parse the command line and look for 'ethX=...' fields, and register all
  476. * those fields. They will be later initialized in iss_net_init.
  477. */
  478. static int __init iss_net_setup(char *str)
  479. {
  480. struct iss_net_private *device = NULL;
  481. struct iss_net_init *new;
  482. struct list_head *ele;
  483. char *end;
  484. int rc;
  485. unsigned n;
  486. end = strchr(str, '=');
  487. if (!end) {
  488. pr_err("Expected '=' after device number\n");
  489. return 1;
  490. }
  491. *end = 0;
  492. rc = kstrtouint(str, 0, &n);
  493. *end = '=';
  494. if (rc < 0) {
  495. pr_err("Failed to parse '%s'\n", str);
  496. return 1;
  497. }
  498. str = end;
  499. spin_lock(&devices_lock);
  500. list_for_each(ele, &devices) {
  501. device = list_entry(ele, struct iss_net_private, device_list);
  502. if (device->index == n)
  503. break;
  504. }
  505. spin_unlock(&devices_lock);
  506. if (device && device->index == n) {
  507. pr_err("Device %u already configured\n", n);
  508. return 1;
  509. }
  510. new = alloc_bootmem(sizeof(*new));
  511. if (new == NULL) {
  512. pr_err("Alloc_bootmem failed\n");
  513. return 1;
  514. }
  515. INIT_LIST_HEAD(&new->list);
  516. new->index = n;
  517. new->init = str + 1;
  518. list_add_tail(&new->list, &eth_cmd_line);
  519. return 1;
  520. }
  521. __setup("eth", iss_net_setup);
  522. /*
  523. * Initialize all ISS Ethernet devices previously registered in iss_net_setup.
  524. */
  525. static int iss_net_init(void)
  526. {
  527. struct list_head *ele, *next;
  528. /* Walk through all Ethernet devices specified in the command line. */
  529. list_for_each_safe(ele, next, &eth_cmd_line) {
  530. struct iss_net_init *eth;
  531. eth = list_entry(ele, struct iss_net_init, list);
  532. iss_net_configure(eth->index, eth->init);
  533. }
  534. return 1;
  535. }
  536. device_initcall(iss_net_init);