sunvnet.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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 "1.0"
  34. #define DRV_MODULE_RELDATE "June 25, 2007"
  35. static char version[] =
  36. DRV_MODULE_NAME ".c:v" DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")\n";
  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_change_mtu = sunvnet_change_mtu_common,
  133. .ndo_start_xmit = vnet_start_xmit,
  134. .ndo_select_queue = vnet_select_queue,
  135. #ifdef CONFIG_NET_POLL_CONTROLLER
  136. .ndo_poll_controller = vnet_poll_controller,
  137. #endif
  138. };
  139. static struct vnet *vnet_new(const u64 *local_mac,
  140. struct vio_dev *vdev)
  141. {
  142. struct net_device *dev;
  143. struct vnet *vp;
  144. int err, i;
  145. dev = alloc_etherdev_mqs(sizeof(*vp), VNET_MAX_TXQS, 1);
  146. if (!dev)
  147. return ERR_PTR(-ENOMEM);
  148. dev->needed_headroom = VNET_PACKET_SKIP + 8;
  149. dev->needed_tailroom = 8;
  150. for (i = 0; i < ETH_ALEN; i++)
  151. dev->dev_addr[i] = (*local_mac >> (5 - i) * 8) & 0xff;
  152. vp = netdev_priv(dev);
  153. spin_lock_init(&vp->lock);
  154. vp->dev = dev;
  155. INIT_LIST_HEAD(&vp->port_list);
  156. for (i = 0; i < VNET_PORT_HASH_SIZE; i++)
  157. INIT_HLIST_HEAD(&vp->port_hash[i]);
  158. INIT_LIST_HEAD(&vp->list);
  159. vp->local_mac = *local_mac;
  160. dev->netdev_ops = &vnet_ops;
  161. dev->ethtool_ops = &vnet_ethtool_ops;
  162. dev->watchdog_timeo = VNET_TX_TIMEOUT;
  163. dev->hw_features = NETIF_F_TSO | NETIF_F_GSO | NETIF_F_ALL_TSO |
  164. NETIF_F_HW_CSUM | NETIF_F_SG;
  165. dev->features = dev->hw_features;
  166. SET_NETDEV_DEV(dev, &vdev->dev);
  167. err = register_netdev(dev);
  168. if (err) {
  169. pr_err("Cannot register net device, aborting\n");
  170. goto err_out_free_dev;
  171. }
  172. netdev_info(dev, "Sun LDOM vnet %pM\n", dev->dev_addr);
  173. list_add(&vp->list, &vnet_list);
  174. return vp;
  175. err_out_free_dev:
  176. free_netdev(dev);
  177. return ERR_PTR(err);
  178. }
  179. static struct vnet *vnet_find_or_create(const u64 *local_mac,
  180. struct vio_dev *vdev)
  181. {
  182. struct vnet *iter, *vp;
  183. mutex_lock(&vnet_list_mutex);
  184. vp = NULL;
  185. list_for_each_entry(iter, &vnet_list, list) {
  186. if (iter->local_mac == *local_mac) {
  187. vp = iter;
  188. break;
  189. }
  190. }
  191. if (!vp)
  192. vp = vnet_new(local_mac, vdev);
  193. mutex_unlock(&vnet_list_mutex);
  194. return vp;
  195. }
  196. static void vnet_cleanup(void)
  197. {
  198. struct vnet *vp;
  199. struct net_device *dev;
  200. mutex_lock(&vnet_list_mutex);
  201. while (!list_empty(&vnet_list)) {
  202. vp = list_first_entry(&vnet_list, struct vnet, list);
  203. list_del(&vp->list);
  204. dev = vp->dev;
  205. /* vio_unregister_driver() should have cleaned up port_list */
  206. BUG_ON(!list_empty(&vp->port_list));
  207. unregister_netdev(dev);
  208. free_netdev(dev);
  209. }
  210. mutex_unlock(&vnet_list_mutex);
  211. }
  212. static const char *local_mac_prop = "local-mac-address";
  213. static struct vnet *vnet_find_parent(struct mdesc_handle *hp,
  214. u64 port_node,
  215. struct vio_dev *vdev)
  216. {
  217. const u64 *local_mac = NULL;
  218. u64 a;
  219. mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) {
  220. u64 target = mdesc_arc_target(hp, a);
  221. const char *name;
  222. name = mdesc_get_property(hp, target, "name", NULL);
  223. if (!name || strcmp(name, "network"))
  224. continue;
  225. local_mac = mdesc_get_property(hp, target,
  226. local_mac_prop, NULL);
  227. if (local_mac)
  228. break;
  229. }
  230. if (!local_mac)
  231. return ERR_PTR(-ENODEV);
  232. return vnet_find_or_create(local_mac, vdev);
  233. }
  234. static struct ldc_channel_config vnet_ldc_cfg = {
  235. .event = sunvnet_event_common,
  236. .mtu = 64,
  237. .mode = LDC_MODE_UNRELIABLE,
  238. };
  239. static struct vio_driver_ops vnet_vio_ops = {
  240. .send_attr = sunvnet_send_attr_common,
  241. .handle_attr = sunvnet_handle_attr_common,
  242. .handshake_complete = sunvnet_handshake_complete_common,
  243. };
  244. static void print_version(void)
  245. {
  246. printk_once(KERN_INFO "%s", version);
  247. }
  248. const char *remote_macaddr_prop = "remote-mac-address";
  249. static int vnet_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  250. {
  251. struct mdesc_handle *hp;
  252. struct vnet_port *port;
  253. unsigned long flags;
  254. struct vnet *vp;
  255. const u64 *rmac;
  256. int len, i, err, switch_port;
  257. print_version();
  258. hp = mdesc_grab();
  259. vp = vnet_find_parent(hp, vdev->mp, vdev);
  260. if (IS_ERR(vp)) {
  261. pr_err("Cannot find port parent vnet\n");
  262. err = PTR_ERR(vp);
  263. goto err_out_put_mdesc;
  264. }
  265. rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len);
  266. err = -ENODEV;
  267. if (!rmac) {
  268. pr_err("Port lacks %s property\n", remote_macaddr_prop);
  269. goto err_out_put_mdesc;
  270. }
  271. port = kzalloc(sizeof(*port), GFP_KERNEL);
  272. err = -ENOMEM;
  273. if (!port)
  274. goto err_out_put_mdesc;
  275. for (i = 0; i < ETH_ALEN; i++)
  276. port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;
  277. port->vp = vp;
  278. err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK,
  279. vnet_versions, ARRAY_SIZE(vnet_versions),
  280. &vnet_vio_ops, vp->dev->name);
  281. if (err)
  282. goto err_out_free_port;
  283. err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port);
  284. if (err)
  285. goto err_out_free_port;
  286. netif_napi_add(port->vp->dev, &port->napi, sunvnet_poll_common,
  287. NAPI_POLL_WEIGHT);
  288. INIT_HLIST_NODE(&port->hash);
  289. INIT_LIST_HEAD(&port->list);
  290. switch_port = 0;
  291. if (mdesc_get_property(hp, vdev->mp, "switch-port", NULL))
  292. switch_port = 1;
  293. port->switch_port = switch_port;
  294. port->tso = true;
  295. port->tsolen = 0;
  296. spin_lock_irqsave(&vp->lock, flags);
  297. if (switch_port)
  298. list_add_rcu(&port->list, &vp->port_list);
  299. else
  300. list_add_tail_rcu(&port->list, &vp->port_list);
  301. hlist_add_head_rcu(&port->hash,
  302. &vp->port_hash[vnet_hashfn(port->raddr)]);
  303. sunvnet_port_add_txq_common(port);
  304. spin_unlock_irqrestore(&vp->lock, flags);
  305. dev_set_drvdata(&vdev->dev, port);
  306. pr_info("%s: PORT ( remote-mac %pM%s )\n",
  307. vp->dev->name, port->raddr, switch_port ? " switch-port" : "");
  308. setup_timer(&port->clean_timer, sunvnet_clean_timer_expire_common,
  309. (unsigned long)port);
  310. napi_enable(&port->napi);
  311. vio_port_up(&port->vio);
  312. mdesc_release(hp);
  313. return 0;
  314. err_out_free_port:
  315. kfree(port);
  316. err_out_put_mdesc:
  317. mdesc_release(hp);
  318. return err;
  319. }
  320. static int vnet_port_remove(struct vio_dev *vdev)
  321. {
  322. struct vnet_port *port = dev_get_drvdata(&vdev->dev);
  323. if (port) {
  324. del_timer_sync(&port->vio.timer);
  325. napi_disable(&port->napi);
  326. list_del_rcu(&port->list);
  327. hlist_del_rcu(&port->hash);
  328. synchronize_rcu();
  329. del_timer_sync(&port->clean_timer);
  330. sunvnet_port_rm_txq_common(port);
  331. netif_napi_del(&port->napi);
  332. sunvnet_port_free_tx_bufs_common(port);
  333. vio_ldc_free(&port->vio);
  334. dev_set_drvdata(&vdev->dev, NULL);
  335. kfree(port);
  336. }
  337. return 0;
  338. }
  339. static const struct vio_device_id vnet_port_match[] = {
  340. {
  341. .type = "vnet-port",
  342. },
  343. {},
  344. };
  345. MODULE_DEVICE_TABLE(vio, vnet_port_match);
  346. static struct vio_driver vnet_port_driver = {
  347. .id_table = vnet_port_match,
  348. .probe = vnet_port_probe,
  349. .remove = vnet_port_remove,
  350. .name = "vnet_port",
  351. };
  352. static int __init vnet_init(void)
  353. {
  354. return vio_register_driver(&vnet_port_driver);
  355. }
  356. static void __exit vnet_exit(void)
  357. {
  358. vio_unregister_driver(&vnet_port_driver);
  359. vnet_cleanup();
  360. }
  361. module_init(vnet_init);
  362. module_exit(vnet_exit);