network.c 14 KB

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