network.c 15 KB

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