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