sunvnet.c 10 KB

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