network.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * arch/xtensa/platforms/iss/network.c
  5. *
  6. * Platform specific initialization.
  7. *
  8. * Authors: Chris Zankel <chris@zankel.net>
  9. * Based on work form the UML team.
  10. *
  11. * Copyright 2005 Tensilica Inc.
  12. */
  13. #define pr_fmt(fmt) "%s: " fmt, __func__
  14. #include <linux/hex.h>
  15. #include <linux/list.h>
  16. #include <linux/irq.h>
  17. #include <linux/spinlock.h>
  18. #include <linux/slab.h>
  19. #include <linux/timer.h>
  20. #include <linux/if_ether.h>
  21. #include <linux/inetdevice.h>
  22. #include <linux/init.h>
  23. #include <linux/if_tun.h>
  24. #include <linux/etherdevice.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/ioctl.h>
  27. #include <linux/memblock.h>
  28. #include <linux/ethtool.h>
  29. #include <linux/rtnetlink.h>
  30. #include <linux/platform_device.h>
  31. #include <platform/simcall.h>
  32. #define DRIVER_NAME "iss-netdev"
  33. #define ETH_MAX_PACKET 1500
  34. #define ETH_HEADER_OTHER 14
  35. #define ISS_NET_TIMER_VALUE (HZ / 10)
  36. /* ------------------------------------------------------------------------- */
  37. /* We currently only support the TUNTAP transport protocol. */
  38. #define TRANSPORT_TUNTAP_NAME "tuntap"
  39. #define TRANSPORT_TUNTAP_MTU ETH_MAX_PACKET
  40. struct tuntap_info {
  41. char dev_name[IFNAMSIZ];
  42. int fd;
  43. };
  44. /* ------------------------------------------------------------------------- */
  45. struct iss_net_private;
  46. struct iss_net_ops {
  47. int (*open)(struct iss_net_private *lp);
  48. void (*close)(struct iss_net_private *lp);
  49. int (*read)(struct iss_net_private *lp, struct sk_buff **skb);
  50. int (*write)(struct iss_net_private *lp, struct sk_buff **skb);
  51. unsigned short (*protocol)(struct sk_buff *skb);
  52. int (*poll)(struct iss_net_private *lp);
  53. };
  54. /* This structure contains out private information for the driver. */
  55. struct iss_net_private {
  56. spinlock_t lock;
  57. struct net_device *dev;
  58. struct platform_device pdev;
  59. struct timer_list tl;
  60. struct rtnl_link_stats64 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. const struct iss_net_ops *net_ops;
  70. } tp;
  71. };
  72. /* ================================ HELPERS ================================ */
  73. static char *split_if_spec(char *str, ...)
  74. {
  75. char **arg, *end;
  76. va_list ap;
  77. va_start(ap, str);
  78. while ((arg = va_arg(ap, char**)) != NULL) {
  79. if (*str == '\0') {
  80. va_end(ap);
  81. return NULL;
  82. }
  83. end = strchr(str, ',');
  84. if (end != str)
  85. *arg = str;
  86. if (end == NULL) {
  87. va_end(ap);
  88. return NULL;
  89. }
  90. *end++ = '\0';
  91. str = end;
  92. }
  93. va_end(ap);
  94. return str;
  95. }
  96. /* Set Ethernet address of the specified device. */
  97. static void setup_etheraddr(struct net_device *dev, char *str)
  98. {
  99. u8 addr[ETH_ALEN];
  100. if (str == NULL)
  101. goto random;
  102. if (!mac_pton(str, addr)) {
  103. pr_err("%s: failed to parse '%s' as an ethernet address\n",
  104. dev->name, str);
  105. goto random;
  106. }
  107. if (is_multicast_ether_addr(addr)) {
  108. pr_err("%s: attempt to assign a multicast ethernet address\n",
  109. dev->name);
  110. goto random;
  111. }
  112. if (!is_valid_ether_addr(addr)) {
  113. pr_err("%s: attempt to assign an invalid ethernet address\n",
  114. dev->name);
  115. goto random;
  116. }
  117. if (!is_local_ether_addr(addr))
  118. pr_warn("%s: assigning a globally valid ethernet address\n",
  119. dev->name);
  120. eth_hw_addr_set(dev, addr);
  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. strscpy(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. static 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. static const struct iss_net_ops tuntap_ops = {
  176. .open = tuntap_open,
  177. .close = tuntap_close,
  178. .read = tuntap_read,
  179. .write = tuntap_write,
  180. .protocol = tuntap_protocol,
  181. .poll = tuntap_poll,
  182. };
  183. /*
  184. * ethX=tuntap,[mac address],device name
  185. */
  186. static int tuntap_probe(struct iss_net_private *lp, int index, char *init)
  187. {
  188. struct net_device *dev = lp->dev;
  189. char *dev_name = NULL, *mac_str = NULL, *rem = NULL;
  190. /* Transport should be 'tuntap': ethX=tuntap,mac,dev_name */
  191. if (strncmp(init, TRANSPORT_TUNTAP_NAME,
  192. sizeof(TRANSPORT_TUNTAP_NAME) - 1))
  193. return 0;
  194. init += sizeof(TRANSPORT_TUNTAP_NAME) - 1;
  195. if (*init == ',') {
  196. rem = split_if_spec(init + 1, &mac_str, &dev_name, NULL);
  197. if (rem != NULL) {
  198. pr_err("%s: extra garbage on specification : '%s'\n",
  199. dev->name, rem);
  200. return 0;
  201. }
  202. } else if (*init != '\0') {
  203. pr_err("%s: invalid argument: %s. Skipping device!\n",
  204. dev->name, init);
  205. return 0;
  206. }
  207. if (!dev_name) {
  208. pr_err("%s: missing tuntap device name\n", dev->name);
  209. return 0;
  210. }
  211. strscpy(lp->tp.info.tuntap.dev_name, dev_name,
  212. sizeof(lp->tp.info.tuntap.dev_name));
  213. setup_etheraddr(dev, mac_str);
  214. lp->mtu = TRANSPORT_TUNTAP_MTU;
  215. lp->tp.info.tuntap.fd = -1;
  216. lp->tp.net_ops = &tuntap_ops;
  217. return 1;
  218. }
  219. /* ================================ ISS NET ================================ */
  220. static int iss_net_rx(struct net_device *dev)
  221. {
  222. struct iss_net_private *lp = netdev_priv(dev);
  223. int pkt_len;
  224. struct sk_buff *skb;
  225. /* Check if there is any new data. */
  226. if (lp->tp.net_ops->poll(lp) == 0)
  227. return 0;
  228. /* Try to allocate memory, if it fails, try again next round. */
  229. skb = dev_alloc_skb(dev->mtu + 2 + ETH_HEADER_OTHER);
  230. if (skb == NULL) {
  231. spin_lock_bh(&lp->lock);
  232. lp->stats.rx_dropped++;
  233. spin_unlock_bh(&lp->lock);
  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.net_ops->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.net_ops->protocol(skb);
  245. spin_lock_bh(&lp->lock);
  246. lp->stats.rx_bytes += skb->len;
  247. lp->stats.rx_packets++;
  248. spin_unlock_bh(&lp->lock);
  249. netif_rx(skb);
  250. return pkt_len;
  251. }
  252. kfree_skb(skb);
  253. return pkt_len;
  254. }
  255. static int iss_net_poll(struct iss_net_private *lp)
  256. {
  257. int err, ret = 0;
  258. if (!netif_running(lp->dev))
  259. return 0;
  260. while ((err = iss_net_rx(lp->dev)) > 0)
  261. ret++;
  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. return ret;
  270. }
  271. static void iss_net_timer(struct timer_list *t)
  272. {
  273. struct iss_net_private *lp = timer_container_of(lp, t, timer);
  274. iss_net_poll(lp);
  275. mod_timer(&lp->timer, jiffies + lp->timer_val);
  276. }
  277. static int iss_net_open(struct net_device *dev)
  278. {
  279. struct iss_net_private *lp = netdev_priv(dev);
  280. int err;
  281. err = lp->tp.net_ops->open(lp);
  282. if (err < 0)
  283. return err;
  284. netif_start_queue(dev);
  285. /* clear buffer - it can happen that the host side of the interface
  286. * is full when we get here. In this case, new data is never queued,
  287. * SIGIOs never arrive, and the net never works.
  288. */
  289. while ((err = iss_net_rx(dev)) > 0)
  290. ;
  291. timer_setup(&lp->timer, iss_net_timer, 0);
  292. lp->timer_val = ISS_NET_TIMER_VALUE;
  293. mod_timer(&lp->timer, jiffies + lp->timer_val);
  294. return err;
  295. }
  296. static int iss_net_close(struct net_device *dev)
  297. {
  298. struct iss_net_private *lp = netdev_priv(dev);
  299. netif_stop_queue(dev);
  300. timer_delete_sync(&lp->timer);
  301. lp->tp.net_ops->close(lp);
  302. return 0;
  303. }
  304. static int iss_net_start_xmit(struct sk_buff *skb, struct net_device *dev)
  305. {
  306. struct iss_net_private *lp = netdev_priv(dev);
  307. int len;
  308. netif_stop_queue(dev);
  309. len = lp->tp.net_ops->write(lp, &skb);
  310. if (len == skb->len) {
  311. spin_lock_bh(&lp->lock);
  312. lp->stats.tx_packets++;
  313. lp->stats.tx_bytes += skb->len;
  314. spin_unlock_bh(&lp->lock);
  315. netif_trans_update(dev);
  316. netif_start_queue(dev);
  317. /* this is normally done in the interrupt when tx finishes */
  318. netif_wake_queue(dev);
  319. } else if (len == 0) {
  320. netif_start_queue(dev);
  321. spin_lock_bh(&lp->lock);
  322. lp->stats.tx_dropped++;
  323. spin_unlock_bh(&lp->lock);
  324. } else {
  325. netif_start_queue(dev);
  326. pr_err("%s: %s failed(%d)\n", dev->name, __func__, len);
  327. }
  328. dev_kfree_skb(skb);
  329. return NETDEV_TX_OK;
  330. }
  331. static void iss_net_get_stats64(struct net_device *dev,
  332. struct rtnl_link_stats64 *stats)
  333. {
  334. struct iss_net_private *lp = netdev_priv(dev);
  335. spin_lock_bh(&lp->lock);
  336. *stats = lp->stats;
  337. spin_unlock_bh(&lp->lock);
  338. }
  339. static void iss_net_set_multicast_list(struct net_device *dev)
  340. {
  341. }
  342. static void iss_net_tx_timeout(struct net_device *dev, unsigned int txqueue)
  343. {
  344. }
  345. static int iss_net_change_mtu(struct net_device *dev, int new_mtu)
  346. {
  347. return -EINVAL;
  348. }
  349. static void iss_net_user_timer_expire(struct timer_list *unused)
  350. {
  351. }
  352. static struct platform_driver iss_net_driver = {
  353. .driver = {
  354. .name = DRIVER_NAME,
  355. },
  356. };
  357. static int driver_registered;
  358. static const struct net_device_ops iss_netdev_ops = {
  359. .ndo_open = iss_net_open,
  360. .ndo_stop = iss_net_close,
  361. .ndo_get_stats64 = iss_net_get_stats64,
  362. .ndo_start_xmit = iss_net_start_xmit,
  363. .ndo_validate_addr = eth_validate_addr,
  364. .ndo_change_mtu = iss_net_change_mtu,
  365. .ndo_set_mac_address = eth_mac_addr,
  366. .ndo_tx_timeout = iss_net_tx_timeout,
  367. .ndo_set_rx_mode = iss_net_set_multicast_list,
  368. };
  369. static void iss_net_pdev_release(struct device *dev)
  370. {
  371. struct platform_device *pdev = to_platform_device(dev);
  372. struct iss_net_private *lp =
  373. container_of(pdev, struct iss_net_private, pdev);
  374. free_netdev(lp->dev);
  375. }
  376. static void iss_net_configure(int index, char *init)
  377. {
  378. struct net_device *dev;
  379. struct iss_net_private *lp;
  380. dev = alloc_etherdev(sizeof(*lp));
  381. if (dev == NULL) {
  382. pr_err("eth_configure: failed to allocate device\n");
  383. return;
  384. }
  385. /* Initialize private element. */
  386. lp = netdev_priv(dev);
  387. *lp = (struct iss_net_private) {
  388. .dev = dev,
  389. .index = index,
  390. };
  391. spin_lock_init(&lp->lock);
  392. /*
  393. * If this name ends up conflicting with an existing registered
  394. * netdevice, that is OK, register_netdev{,ice}() will notice this
  395. * and fail.
  396. */
  397. snprintf(dev->name, sizeof(dev->name), "eth%d", index);
  398. /*
  399. * Try all transport protocols.
  400. * Note: more protocols can be added by adding '&& !X_init(lp, eth)'.
  401. */
  402. if (!tuntap_probe(lp, index, init)) {
  403. pr_err("%s: invalid arguments. Skipping device!\n",
  404. dev->name);
  405. goto err_free_netdev;
  406. }
  407. pr_info("Netdevice %d (%pM)\n", index, dev->dev_addr);
  408. /* sysfs register */
  409. if (!driver_registered) {
  410. if (platform_driver_register(&iss_net_driver))
  411. goto err_free_netdev;
  412. driver_registered = 1;
  413. }
  414. lp->pdev.id = index;
  415. lp->pdev.name = DRIVER_NAME;
  416. lp->pdev.dev.release = iss_net_pdev_release;
  417. if (platform_device_register(&lp->pdev))
  418. goto err_free_netdev;
  419. SET_NETDEV_DEV(dev, &lp->pdev.dev);
  420. dev->netdev_ops = &iss_netdev_ops;
  421. dev->mtu = lp->mtu;
  422. dev->watchdog_timeo = (HZ >> 1);
  423. dev->irq = -1;
  424. rtnl_lock();
  425. if (register_netdevice(dev)) {
  426. rtnl_unlock();
  427. pr_err("%s: error registering net device!\n", dev->name);
  428. platform_device_unregister(&lp->pdev);
  429. /* dev is freed by the iss_net_pdev_release callback */
  430. return;
  431. }
  432. rtnl_unlock();
  433. timer_setup(&lp->tl, iss_net_user_timer_expire, 0);
  434. return;
  435. err_free_netdev:
  436. free_netdev(dev);
  437. }
  438. /* ------------------------------------------------------------------------- */
  439. /* Filled in during early boot */
  440. struct list_head eth_cmd_line = LIST_HEAD_INIT(eth_cmd_line);
  441. struct iss_net_init {
  442. struct list_head list;
  443. char *init; /* init string */
  444. int index;
  445. };
  446. /*
  447. * Parse the command line and look for 'ethX=...' fields, and register all
  448. * those fields. They will be later initialized in iss_net_init.
  449. */
  450. static int __init iss_net_setup(char *str)
  451. {
  452. struct iss_net_init *device = NULL;
  453. struct iss_net_init *new;
  454. struct list_head *ele;
  455. char *end;
  456. int rc;
  457. unsigned n;
  458. end = strchr(str, '=');
  459. if (!end) {
  460. pr_err("Expected '=' after device number\n");
  461. return 1;
  462. }
  463. *end = 0;
  464. rc = kstrtouint(str, 0, &n);
  465. *end = '=';
  466. if (rc < 0) {
  467. pr_err("Failed to parse '%s'\n", str);
  468. return 1;
  469. }
  470. str = end;
  471. list_for_each(ele, &eth_cmd_line) {
  472. device = list_entry(ele, struct iss_net_init, list);
  473. if (device->index == n)
  474. break;
  475. }
  476. if (device && device->index == n) {
  477. pr_err("Device %u already configured\n", n);
  478. return 1;
  479. }
  480. new = memblock_alloc(sizeof(*new), SMP_CACHE_BYTES);
  481. if (new == NULL) {
  482. pr_err("Alloc_bootmem failed\n");
  483. return 1;
  484. }
  485. INIT_LIST_HEAD(&new->list);
  486. new->index = n;
  487. new->init = str + 1;
  488. list_add_tail(&new->list, &eth_cmd_line);
  489. return 1;
  490. }
  491. __setup("eth", iss_net_setup);
  492. /*
  493. * Initialize all ISS Ethernet devices previously registered in iss_net_setup.
  494. */
  495. static int iss_net_init(void)
  496. {
  497. struct list_head *ele, *next;
  498. /* Walk through all Ethernet devices specified in the command line. */
  499. list_for_each_safe(ele, next, &eth_cmd_line) {
  500. struct iss_net_init *eth;
  501. eth = list_entry(ele, struct iss_net_init, list);
  502. iss_net_configure(eth->index, eth->init);
  503. }
  504. return 1;
  505. }
  506. device_initcall(iss_net_init);