sunvnet.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* sunvnet.c: Sun LDOM Virtual Network Driver.
  3. *
  4. * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net>
  5. * Copyright (C) 2016-2017 Oracle. All rights reserved.
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <linux/module.h>
  9. #include <linux/kernel.h>
  10. #include <linux/types.h>
  11. #include <linux/slab.h>
  12. #include <linux/delay.h>
  13. #include <linux/init.h>
  14. #include <linux/netdevice.h>
  15. #include <linux/ethtool.h>
  16. #include <linux/etherdevice.h>
  17. #include <linux/mutex.h>
  18. #include <linux/highmem.h>
  19. #include <linux/if_vlan.h>
  20. #if IS_ENABLED(CONFIG_IPV6)
  21. #include <linux/icmpv6.h>
  22. #endif
  23. #include <net/ip.h>
  24. #include <net/icmp.h>
  25. #include <net/route.h>
  26. #include <asm/vio.h>
  27. #include <asm/ldc.h>
  28. #include "sunvnet_common.h"
  29. /* length of time before we decide the hardware is borked,
  30. * and dev->tx_timeout() should be called to fix the problem
  31. */
  32. #define VNET_TX_TIMEOUT (5 * HZ)
  33. #define DRV_MODULE_NAME "sunvnet"
  34. #define DRV_MODULE_VERSION "2.0"
  35. #define DRV_MODULE_RELDATE "February 3, 2017"
  36. static char version[] =
  37. DRV_MODULE_NAME " " DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
  38. MODULE_AUTHOR("David S. Miller (davem@davemloft.net)");
  39. MODULE_DESCRIPTION("Sun LDOM virtual network driver");
  40. MODULE_LICENSE("GPL");
  41. MODULE_VERSION(DRV_MODULE_VERSION);
  42. /* Ordered from largest major to lowest */
  43. static struct vio_version vnet_versions[] = {
  44. { .major = 1, .minor = 8 },
  45. { .major = 1, .minor = 7 },
  46. { .major = 1, .minor = 6 },
  47. { .major = 1, .minor = 0 },
  48. };
  49. static void vnet_get_drvinfo(struct net_device *dev,
  50. struct ethtool_drvinfo *info)
  51. {
  52. strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
  53. strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
  54. }
  55. static u32 vnet_get_msglevel(struct net_device *dev)
  56. {
  57. struct vnet *vp = netdev_priv(dev);
  58. return vp->msg_enable;
  59. }
  60. static void vnet_set_msglevel(struct net_device *dev, u32 value)
  61. {
  62. struct vnet *vp = netdev_priv(dev);
  63. vp->msg_enable = value;
  64. }
  65. static const struct {
  66. const char string[ETH_GSTRING_LEN];
  67. } ethtool_stats_keys[] = {
  68. { "rx_packets" },
  69. { "tx_packets" },
  70. { "rx_bytes" },
  71. { "tx_bytes" },
  72. { "rx_errors" },
  73. { "tx_errors" },
  74. { "rx_dropped" },
  75. { "tx_dropped" },
  76. { "multicast" },
  77. { "rx_length_errors" },
  78. { "rx_frame_errors" },
  79. { "rx_missed_errors" },
  80. { "tx_carrier_errors" },
  81. { "nports" },
  82. };
  83. static int vnet_get_sset_count(struct net_device *dev, int sset)
  84. {
  85. struct vnet *vp = (struct vnet *)netdev_priv(dev);
  86. switch (sset) {
  87. case ETH_SS_STATS:
  88. return ARRAY_SIZE(ethtool_stats_keys)
  89. + (NUM_VNET_PORT_STATS * vp->nports);
  90. default:
  91. return -EOPNOTSUPP;
  92. }
  93. }
  94. static void vnet_get_strings(struct net_device *dev, u32 stringset, u8 *buf)
  95. {
  96. struct vnet *vp = (struct vnet *)netdev_priv(dev);
  97. struct vnet_port *port;
  98. char *p = (char *)buf;
  99. switch (stringset) {
  100. case ETH_SS_STATS:
  101. memcpy(buf, &ethtool_stats_keys, sizeof(ethtool_stats_keys));
  102. p += sizeof(ethtool_stats_keys);
  103. rcu_read_lock();
  104. list_for_each_entry_rcu(port, &vp->port_list, list) {
  105. snprintf(p, ETH_GSTRING_LEN, "p%u.%s-%pM",
  106. port->q_index, port->switch_port ? "s" : "q",
  107. port->raddr);
  108. p += ETH_GSTRING_LEN;
  109. snprintf(p, ETH_GSTRING_LEN, "p%u.rx_packets",
  110. port->q_index);
  111. p += ETH_GSTRING_LEN;
  112. snprintf(p, ETH_GSTRING_LEN, "p%u.tx_packets",
  113. port->q_index);
  114. p += ETH_GSTRING_LEN;
  115. snprintf(p, ETH_GSTRING_LEN, "p%u.rx_bytes",
  116. port->q_index);
  117. p += ETH_GSTRING_LEN;
  118. snprintf(p, ETH_GSTRING_LEN, "p%u.tx_bytes",
  119. port->q_index);
  120. p += ETH_GSTRING_LEN;
  121. snprintf(p, ETH_GSTRING_LEN, "p%u.event_up",
  122. port->q_index);
  123. p += ETH_GSTRING_LEN;
  124. snprintf(p, ETH_GSTRING_LEN, "p%u.event_reset",
  125. port->q_index);
  126. p += ETH_GSTRING_LEN;
  127. }
  128. rcu_read_unlock();
  129. break;
  130. default:
  131. WARN_ON(1);
  132. break;
  133. }
  134. }
  135. static void vnet_get_ethtool_stats(struct net_device *dev,
  136. struct ethtool_stats *estats, u64 *data)
  137. {
  138. struct vnet *vp = (struct vnet *)netdev_priv(dev);
  139. struct vnet_port *port;
  140. int i = 0;
  141. data[i++] = dev->stats.rx_packets;
  142. data[i++] = dev->stats.tx_packets;
  143. data[i++] = dev->stats.rx_bytes;
  144. data[i++] = dev->stats.tx_bytes;
  145. data[i++] = dev->stats.rx_errors;
  146. data[i++] = dev->stats.tx_errors;
  147. data[i++] = dev->stats.rx_dropped;
  148. data[i++] = dev->stats.tx_dropped;
  149. data[i++] = dev->stats.multicast;
  150. data[i++] = dev->stats.rx_length_errors;
  151. data[i++] = dev->stats.rx_frame_errors;
  152. data[i++] = dev->stats.rx_missed_errors;
  153. data[i++] = dev->stats.tx_carrier_errors;
  154. data[i++] = vp->nports;
  155. rcu_read_lock();
  156. list_for_each_entry_rcu(port, &vp->port_list, list) {
  157. data[i++] = port->q_index;
  158. data[i++] = port->stats.rx_packets;
  159. data[i++] = port->stats.tx_packets;
  160. data[i++] = port->stats.rx_bytes;
  161. data[i++] = port->stats.tx_bytes;
  162. data[i++] = port->stats.event_up;
  163. data[i++] = port->stats.event_reset;
  164. }
  165. rcu_read_unlock();
  166. }
  167. static const struct ethtool_ops vnet_ethtool_ops = {
  168. .get_drvinfo = vnet_get_drvinfo,
  169. .get_msglevel = vnet_get_msglevel,
  170. .set_msglevel = vnet_set_msglevel,
  171. .get_link = ethtool_op_get_link,
  172. .get_sset_count = vnet_get_sset_count,
  173. .get_strings = vnet_get_strings,
  174. .get_ethtool_stats = vnet_get_ethtool_stats,
  175. };
  176. static LIST_HEAD(vnet_list);
  177. static DEFINE_MUTEX(vnet_list_mutex);
  178. static struct vnet_port *__tx_port_find(struct vnet *vp, struct sk_buff *skb)
  179. {
  180. unsigned int hash = vnet_hashfn(skb->data);
  181. struct hlist_head *hp = &vp->port_hash[hash];
  182. struct vnet_port *port;
  183. hlist_for_each_entry_rcu(port, hp, hash) {
  184. if (!sunvnet_port_is_up_common(port))
  185. continue;
  186. if (ether_addr_equal(port->raddr, skb->data))
  187. return port;
  188. }
  189. list_for_each_entry_rcu(port, &vp->port_list, list) {
  190. if (!port->switch_port)
  191. continue;
  192. if (!sunvnet_port_is_up_common(port))
  193. continue;
  194. return port;
  195. }
  196. return NULL;
  197. }
  198. /* func arg to vnet_start_xmit_common() to get the proper tx port */
  199. static struct vnet_port *vnet_tx_port_find(struct sk_buff *skb,
  200. struct net_device *dev)
  201. {
  202. struct vnet *vp = netdev_priv(dev);
  203. return __tx_port_find(vp, skb);
  204. }
  205. static u16 vnet_select_queue(struct net_device *dev, struct sk_buff *skb,
  206. struct net_device *sb_dev,
  207. select_queue_fallback_t fallback)
  208. {
  209. struct vnet *vp = netdev_priv(dev);
  210. struct vnet_port *port = __tx_port_find(vp, skb);
  211. if (!port)
  212. return 0;
  213. return port->q_index;
  214. }
  215. /* Wrappers to common functions */
  216. static netdev_tx_t vnet_start_xmit(struct sk_buff *skb, struct net_device *dev)
  217. {
  218. return sunvnet_start_xmit_common(skb, dev, vnet_tx_port_find);
  219. }
  220. static void vnet_set_rx_mode(struct net_device *dev)
  221. {
  222. struct vnet *vp = netdev_priv(dev);
  223. return sunvnet_set_rx_mode_common(dev, vp);
  224. }
  225. #ifdef CONFIG_NET_POLL_CONTROLLER
  226. static void vnet_poll_controller(struct net_device *dev)
  227. {
  228. struct vnet *vp = netdev_priv(dev);
  229. return sunvnet_poll_controller_common(dev, vp);
  230. }
  231. #endif
  232. static const struct net_device_ops vnet_ops = {
  233. .ndo_open = sunvnet_open_common,
  234. .ndo_stop = sunvnet_close_common,
  235. .ndo_set_rx_mode = vnet_set_rx_mode,
  236. .ndo_set_mac_address = sunvnet_set_mac_addr_common,
  237. .ndo_validate_addr = eth_validate_addr,
  238. .ndo_tx_timeout = sunvnet_tx_timeout_common,
  239. .ndo_start_xmit = vnet_start_xmit,
  240. .ndo_select_queue = vnet_select_queue,
  241. #ifdef CONFIG_NET_POLL_CONTROLLER
  242. .ndo_poll_controller = vnet_poll_controller,
  243. #endif
  244. };
  245. static struct vnet *vnet_new(const u64 *local_mac,
  246. struct vio_dev *vdev)
  247. {
  248. struct net_device *dev;
  249. struct vnet *vp;
  250. int err, i;
  251. dev = alloc_etherdev_mqs(sizeof(*vp), VNET_MAX_TXQS, 1);
  252. if (!dev)
  253. return ERR_PTR(-ENOMEM);
  254. dev->needed_headroom = VNET_PACKET_SKIP + 8;
  255. dev->needed_tailroom = 8;
  256. for (i = 0; i < ETH_ALEN; i++)
  257. dev->dev_addr[i] = (*local_mac >> (5 - i) * 8) & 0xff;
  258. vp = netdev_priv(dev);
  259. spin_lock_init(&vp->lock);
  260. vp->dev = dev;
  261. INIT_LIST_HEAD(&vp->port_list);
  262. for (i = 0; i < VNET_PORT_HASH_SIZE; i++)
  263. INIT_HLIST_HEAD(&vp->port_hash[i]);
  264. INIT_LIST_HEAD(&vp->list);
  265. vp->local_mac = *local_mac;
  266. dev->netdev_ops = &vnet_ops;
  267. dev->ethtool_ops = &vnet_ethtool_ops;
  268. dev->watchdog_timeo = VNET_TX_TIMEOUT;
  269. dev->hw_features = NETIF_F_TSO | NETIF_F_GSO | NETIF_F_ALL_TSO |
  270. NETIF_F_HW_CSUM | NETIF_F_SG;
  271. dev->features = dev->hw_features;
  272. /* MTU range: 68 - 65535 */
  273. dev->min_mtu = ETH_MIN_MTU;
  274. dev->max_mtu = VNET_MAX_MTU;
  275. SET_NETDEV_DEV(dev, &vdev->dev);
  276. err = register_netdev(dev);
  277. if (err) {
  278. pr_err("Cannot register net device, aborting\n");
  279. goto err_out_free_dev;
  280. }
  281. netdev_info(dev, "Sun LDOM vnet %pM\n", dev->dev_addr);
  282. list_add(&vp->list, &vnet_list);
  283. return vp;
  284. err_out_free_dev:
  285. free_netdev(dev);
  286. return ERR_PTR(err);
  287. }
  288. static struct vnet *vnet_find_or_create(const u64 *local_mac,
  289. struct vio_dev *vdev)
  290. {
  291. struct vnet *iter, *vp;
  292. mutex_lock(&vnet_list_mutex);
  293. vp = NULL;
  294. list_for_each_entry(iter, &vnet_list, list) {
  295. if (iter->local_mac == *local_mac) {
  296. vp = iter;
  297. break;
  298. }
  299. }
  300. if (!vp)
  301. vp = vnet_new(local_mac, vdev);
  302. mutex_unlock(&vnet_list_mutex);
  303. return vp;
  304. }
  305. static void vnet_cleanup(void)
  306. {
  307. struct vnet *vp;
  308. struct net_device *dev;
  309. mutex_lock(&vnet_list_mutex);
  310. while (!list_empty(&vnet_list)) {
  311. vp = list_first_entry(&vnet_list, struct vnet, list);
  312. list_del(&vp->list);
  313. dev = vp->dev;
  314. /* vio_unregister_driver() should have cleaned up port_list */
  315. BUG_ON(!list_empty(&vp->port_list));
  316. unregister_netdev(dev);
  317. free_netdev(dev);
  318. }
  319. mutex_unlock(&vnet_list_mutex);
  320. }
  321. static const char *local_mac_prop = "local-mac-address";
  322. static struct vnet *vnet_find_parent(struct mdesc_handle *hp,
  323. u64 port_node,
  324. struct vio_dev *vdev)
  325. {
  326. const u64 *local_mac = NULL;
  327. u64 a;
  328. mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) {
  329. u64 target = mdesc_arc_target(hp, a);
  330. const char *name;
  331. name = mdesc_get_property(hp, target, "name", NULL);
  332. if (!name || strcmp(name, "network"))
  333. continue;
  334. local_mac = mdesc_get_property(hp, target,
  335. local_mac_prop, NULL);
  336. if (local_mac)
  337. break;
  338. }
  339. if (!local_mac)
  340. return ERR_PTR(-ENODEV);
  341. return vnet_find_or_create(local_mac, vdev);
  342. }
  343. static struct ldc_channel_config vnet_ldc_cfg = {
  344. .event = sunvnet_event_common,
  345. .mtu = 64,
  346. .mode = LDC_MODE_UNRELIABLE,
  347. };
  348. static struct vio_driver_ops vnet_vio_ops = {
  349. .send_attr = sunvnet_send_attr_common,
  350. .handle_attr = sunvnet_handle_attr_common,
  351. .handshake_complete = sunvnet_handshake_complete_common,
  352. };
  353. const char *remote_macaddr_prop = "remote-mac-address";
  354. static int vnet_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  355. {
  356. struct mdesc_handle *hp;
  357. struct vnet_port *port;
  358. unsigned long flags;
  359. struct vnet *vp;
  360. const u64 *rmac;
  361. int len, i, err, switch_port;
  362. hp = mdesc_grab();
  363. vp = vnet_find_parent(hp, vdev->mp, vdev);
  364. if (IS_ERR(vp)) {
  365. pr_err("Cannot find port parent vnet\n");
  366. err = PTR_ERR(vp);
  367. goto err_out_put_mdesc;
  368. }
  369. rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len);
  370. err = -ENODEV;
  371. if (!rmac) {
  372. pr_err("Port lacks %s property\n", remote_macaddr_prop);
  373. goto err_out_put_mdesc;
  374. }
  375. port = kzalloc(sizeof(*port), GFP_KERNEL);
  376. err = -ENOMEM;
  377. if (!port)
  378. goto err_out_put_mdesc;
  379. for (i = 0; i < ETH_ALEN; i++)
  380. port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;
  381. port->vp = vp;
  382. err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK,
  383. vnet_versions, ARRAY_SIZE(vnet_versions),
  384. &vnet_vio_ops, vp->dev->name);
  385. if (err)
  386. goto err_out_free_port;
  387. err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port);
  388. if (err)
  389. goto err_out_free_port;
  390. netif_napi_add(port->vp->dev, &port->napi, sunvnet_poll_common,
  391. NAPI_POLL_WEIGHT);
  392. INIT_HLIST_NODE(&port->hash);
  393. INIT_LIST_HEAD(&port->list);
  394. switch_port = 0;
  395. if (mdesc_get_property(hp, vdev->mp, "switch-port", NULL))
  396. switch_port = 1;
  397. port->switch_port = switch_port;
  398. port->tso = true;
  399. port->tsolen = 0;
  400. spin_lock_irqsave(&vp->lock, flags);
  401. if (switch_port)
  402. list_add_rcu(&port->list, &vp->port_list);
  403. else
  404. list_add_tail_rcu(&port->list, &vp->port_list);
  405. hlist_add_head_rcu(&port->hash,
  406. &vp->port_hash[vnet_hashfn(port->raddr)]);
  407. sunvnet_port_add_txq_common(port);
  408. spin_unlock_irqrestore(&vp->lock, flags);
  409. dev_set_drvdata(&vdev->dev, port);
  410. pr_info("%s: PORT ( remote-mac %pM%s )\n",
  411. vp->dev->name, port->raddr, switch_port ? " switch-port" : "");
  412. timer_setup(&port->clean_timer, sunvnet_clean_timer_expire_common, 0);
  413. napi_enable(&port->napi);
  414. vio_port_up(&port->vio);
  415. mdesc_release(hp);
  416. return 0;
  417. err_out_free_port:
  418. kfree(port);
  419. err_out_put_mdesc:
  420. mdesc_release(hp);
  421. return err;
  422. }
  423. static int vnet_port_remove(struct vio_dev *vdev)
  424. {
  425. struct vnet_port *port = dev_get_drvdata(&vdev->dev);
  426. if (port) {
  427. del_timer_sync(&port->vio.timer);
  428. napi_disable(&port->napi);
  429. list_del_rcu(&port->list);
  430. hlist_del_rcu(&port->hash);
  431. synchronize_rcu();
  432. del_timer_sync(&port->clean_timer);
  433. sunvnet_port_rm_txq_common(port);
  434. netif_napi_del(&port->napi);
  435. sunvnet_port_free_tx_bufs_common(port);
  436. vio_ldc_free(&port->vio);
  437. dev_set_drvdata(&vdev->dev, NULL);
  438. kfree(port);
  439. }
  440. return 0;
  441. }
  442. static const struct vio_device_id vnet_port_match[] = {
  443. {
  444. .type = "vnet-port",
  445. },
  446. {},
  447. };
  448. MODULE_DEVICE_TABLE(vio, vnet_port_match);
  449. static struct vio_driver vnet_port_driver = {
  450. .id_table = vnet_port_match,
  451. .probe = vnet_port_probe,
  452. .remove = vnet_port_remove,
  453. .name = "vnet_port",
  454. };
  455. static int __init vnet_init(void)
  456. {
  457. pr_info("%s\n", version);
  458. return vio_register_driver(&vnet_port_driver);
  459. }
  460. static void __exit vnet_exit(void)
  461. {
  462. vio_unregister_driver(&vnet_port_driver);
  463. vnet_cleanup();
  464. }
  465. module_init(vnet_init);
  466. module_exit(vnet_exit);