ldmvsw.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* ldmvsw.c: Sun4v LDOM Virtual Switch Driver.
  3. *
  4. * Copyright (C) 2016-2017 Oracle. All rights reserved.
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/delay.h>
  8. #include <linux/etherdevice.h>
  9. #include <linux/ethtool.h>
  10. #include <linux/highmem.h>
  11. #include <linux/if_vlan.h>
  12. #include <linux/init.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/slab.h>
  18. #include <linux/types.h>
  19. #if defined(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. /* This driver makes use of the common code in sunvnet_common.c */
  28. #include "sunvnet_common.h"
  29. /* Length of time before we decide the hardware is hung,
  30. * and dev->tx_timeout() should be called to fix the problem.
  31. */
  32. #define VSW_TX_TIMEOUT (10 * HZ)
  33. /* Static HW Addr used for the network interfaces representing vsw ports */
  34. static u8 vsw_port_hwaddr[ETH_ALEN] = {0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
  35. #define DRV_MODULE_NAME "ldmvsw"
  36. #define DRV_MODULE_VERSION "1.2"
  37. #define DRV_MODULE_RELDATE "March 4, 2017"
  38. static char version[] =
  39. DRV_MODULE_NAME " " DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
  40. MODULE_AUTHOR("Oracle");
  41. MODULE_DESCRIPTION("Sun4v LDOM Virtual Switch Driver");
  42. MODULE_LICENSE("GPL");
  43. MODULE_VERSION(DRV_MODULE_VERSION);
  44. /* Ordered from largest major to lowest */
  45. static struct vio_version vsw_versions[] = {
  46. { .major = 1, .minor = 8 },
  47. { .major = 1, .minor = 7 },
  48. { .major = 1, .minor = 6 },
  49. { .major = 1, .minor = 0 },
  50. };
  51. static void vsw_get_drvinfo(struct net_device *dev,
  52. struct ethtool_drvinfo *info)
  53. {
  54. strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
  55. strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
  56. }
  57. static u32 vsw_get_msglevel(struct net_device *dev)
  58. {
  59. struct vnet_port *port = netdev_priv(dev);
  60. return port->vp->msg_enable;
  61. }
  62. static void vsw_set_msglevel(struct net_device *dev, u32 value)
  63. {
  64. struct vnet_port *port = netdev_priv(dev);
  65. port->vp->msg_enable = value;
  66. }
  67. static const struct ethtool_ops vsw_ethtool_ops = {
  68. .get_drvinfo = vsw_get_drvinfo,
  69. .get_msglevel = vsw_get_msglevel,
  70. .set_msglevel = vsw_set_msglevel,
  71. .get_link = ethtool_op_get_link,
  72. };
  73. static LIST_HEAD(vnet_list);
  74. static DEFINE_MUTEX(vnet_list_mutex);
  75. /* func arg to vnet_start_xmit_common() to get the proper tx port */
  76. static struct vnet_port *vsw_tx_port_find(struct sk_buff *skb,
  77. struct net_device *dev)
  78. {
  79. struct vnet_port *port = netdev_priv(dev);
  80. return port;
  81. }
  82. static u16 vsw_select_queue(struct net_device *dev, struct sk_buff *skb,
  83. struct net_device *sb_dev,
  84. select_queue_fallback_t fallback)
  85. {
  86. struct vnet_port *port = netdev_priv(dev);
  87. if (!port)
  88. return 0;
  89. return port->q_index;
  90. }
  91. /* Wrappers to common functions */
  92. static netdev_tx_t vsw_start_xmit(struct sk_buff *skb, struct net_device *dev)
  93. {
  94. return sunvnet_start_xmit_common(skb, dev, vsw_tx_port_find);
  95. }
  96. static void vsw_set_rx_mode(struct net_device *dev)
  97. {
  98. struct vnet_port *port = netdev_priv(dev);
  99. return sunvnet_set_rx_mode_common(dev, port->vp);
  100. }
  101. int ldmvsw_open(struct net_device *dev)
  102. {
  103. struct vnet_port *port = netdev_priv(dev);
  104. struct vio_driver_state *vio = &port->vio;
  105. /* reset the channel */
  106. vio_link_state_change(vio, LDC_EVENT_RESET);
  107. vnet_port_reset(port);
  108. vio_port_up(vio);
  109. return 0;
  110. }
  111. EXPORT_SYMBOL_GPL(ldmvsw_open);
  112. #ifdef CONFIG_NET_POLL_CONTROLLER
  113. static void vsw_poll_controller(struct net_device *dev)
  114. {
  115. struct vnet_port *port = netdev_priv(dev);
  116. return sunvnet_poll_controller_common(dev, port->vp);
  117. }
  118. #endif
  119. static const struct net_device_ops vsw_ops = {
  120. .ndo_open = ldmvsw_open,
  121. .ndo_stop = sunvnet_close_common,
  122. .ndo_set_rx_mode = vsw_set_rx_mode,
  123. .ndo_set_mac_address = sunvnet_set_mac_addr_common,
  124. .ndo_validate_addr = eth_validate_addr,
  125. .ndo_tx_timeout = sunvnet_tx_timeout_common,
  126. .ndo_start_xmit = vsw_start_xmit,
  127. .ndo_select_queue = vsw_select_queue,
  128. #ifdef CONFIG_NET_POLL_CONTROLLER
  129. .ndo_poll_controller = vsw_poll_controller,
  130. #endif
  131. };
  132. static const char *local_mac_prop = "local-mac-address";
  133. static const char *cfg_handle_prop = "cfg-handle";
  134. static struct vnet *vsw_get_vnet(struct mdesc_handle *hp,
  135. u64 port_node,
  136. u64 *handle)
  137. {
  138. struct vnet *vp;
  139. struct vnet *iter;
  140. const u64 *local_mac = NULL;
  141. const u64 *cfghandle = NULL;
  142. u64 a;
  143. /* Get the parent virtual-network-switch macaddr and cfghandle */
  144. mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) {
  145. u64 target = mdesc_arc_target(hp, a);
  146. const char *name;
  147. name = mdesc_get_property(hp, target, "name", NULL);
  148. if (!name || strcmp(name, "virtual-network-switch"))
  149. continue;
  150. local_mac = mdesc_get_property(hp, target,
  151. local_mac_prop, NULL);
  152. cfghandle = mdesc_get_property(hp, target,
  153. cfg_handle_prop, NULL);
  154. break;
  155. }
  156. if (!local_mac || !cfghandle)
  157. return ERR_PTR(-ENODEV);
  158. /* find or create associated vnet */
  159. vp = NULL;
  160. mutex_lock(&vnet_list_mutex);
  161. list_for_each_entry(iter, &vnet_list, list) {
  162. if (iter->local_mac == *local_mac) {
  163. vp = iter;
  164. break;
  165. }
  166. }
  167. if (!vp) {
  168. vp = kzalloc(sizeof(*vp), GFP_KERNEL);
  169. if (unlikely(!vp)) {
  170. mutex_unlock(&vnet_list_mutex);
  171. return ERR_PTR(-ENOMEM);
  172. }
  173. spin_lock_init(&vp->lock);
  174. INIT_LIST_HEAD(&vp->port_list);
  175. INIT_LIST_HEAD(&vp->list);
  176. vp->local_mac = *local_mac;
  177. list_add(&vp->list, &vnet_list);
  178. }
  179. mutex_unlock(&vnet_list_mutex);
  180. *handle = (u64)*cfghandle;
  181. return vp;
  182. }
  183. static struct net_device *vsw_alloc_netdev(u8 hwaddr[],
  184. struct vio_dev *vdev,
  185. u64 handle,
  186. u64 port_id)
  187. {
  188. struct net_device *dev;
  189. struct vnet_port *port;
  190. int i;
  191. dev = alloc_etherdev_mqs(sizeof(*port), VNET_MAX_TXQS, 1);
  192. if (!dev)
  193. return ERR_PTR(-ENOMEM);
  194. dev->needed_headroom = VNET_PACKET_SKIP + 8;
  195. dev->needed_tailroom = 8;
  196. for (i = 0; i < ETH_ALEN; i++) {
  197. dev->dev_addr[i] = hwaddr[i];
  198. dev->perm_addr[i] = dev->dev_addr[i];
  199. }
  200. sprintf(dev->name, "vif%d.%d", (int)handle, (int)port_id);
  201. dev->netdev_ops = &vsw_ops;
  202. dev->ethtool_ops = &vsw_ethtool_ops;
  203. dev->watchdog_timeo = VSW_TX_TIMEOUT;
  204. dev->hw_features = NETIF_F_HW_CSUM | NETIF_F_SG;
  205. dev->features = dev->hw_features;
  206. /* MTU range: 68 - 65535 */
  207. dev->min_mtu = ETH_MIN_MTU;
  208. dev->max_mtu = VNET_MAX_MTU;
  209. SET_NETDEV_DEV(dev, &vdev->dev);
  210. return dev;
  211. }
  212. static struct ldc_channel_config vsw_ldc_cfg = {
  213. .event = sunvnet_event_common,
  214. .mtu = 64,
  215. .mode = LDC_MODE_UNRELIABLE,
  216. };
  217. static struct vio_driver_ops vsw_vio_ops = {
  218. .send_attr = sunvnet_send_attr_common,
  219. .handle_attr = sunvnet_handle_attr_common,
  220. .handshake_complete = sunvnet_handshake_complete_common,
  221. };
  222. static const char *remote_macaddr_prop = "remote-mac-address";
  223. static const char *id_prop = "id";
  224. static int vsw_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
  225. {
  226. struct mdesc_handle *hp;
  227. struct vnet_port *port;
  228. unsigned long flags;
  229. struct vnet *vp;
  230. struct net_device *dev;
  231. const u64 *rmac;
  232. int len, i, err;
  233. const u64 *port_id;
  234. u64 handle;
  235. hp = mdesc_grab();
  236. rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len);
  237. err = -ENODEV;
  238. if (!rmac) {
  239. pr_err("Port lacks %s property\n", remote_macaddr_prop);
  240. mdesc_release(hp);
  241. return err;
  242. }
  243. port_id = mdesc_get_property(hp, vdev->mp, id_prop, NULL);
  244. err = -ENODEV;
  245. if (!port_id) {
  246. pr_err("Port lacks %s property\n", id_prop);
  247. mdesc_release(hp);
  248. return err;
  249. }
  250. /* Get (or create) the vnet associated with this port */
  251. vp = vsw_get_vnet(hp, vdev->mp, &handle);
  252. if (IS_ERR(vp)) {
  253. err = PTR_ERR(vp);
  254. pr_err("Failed to get vnet for vsw-port\n");
  255. mdesc_release(hp);
  256. return err;
  257. }
  258. mdesc_release(hp);
  259. dev = vsw_alloc_netdev(vsw_port_hwaddr, vdev, handle, *port_id);
  260. if (IS_ERR(dev)) {
  261. err = PTR_ERR(dev);
  262. pr_err("Failed to alloc netdev for vsw-port\n");
  263. return err;
  264. }
  265. port = netdev_priv(dev);
  266. INIT_LIST_HEAD(&port->list);
  267. for (i = 0; i < ETH_ALEN; i++)
  268. port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;
  269. port->vp = vp;
  270. port->dev = dev;
  271. port->switch_port = 1;
  272. port->tso = false; /* no tso in vsw, misbehaves in bridge */
  273. port->tsolen = 0;
  274. /* Mark the port as belonging to ldmvsw which directs the
  275. * the common code to use the net_device in the vnet_port
  276. * rather than the net_device in the vnet (which is used
  277. * by sunvnet). This bit is used by the VNET_PORT_TO_NET_DEVICE
  278. * macro.
  279. */
  280. port->vsw = 1;
  281. err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK,
  282. vsw_versions, ARRAY_SIZE(vsw_versions),
  283. &vsw_vio_ops, dev->name);
  284. if (err)
  285. goto err_out_free_dev;
  286. err = vio_ldc_alloc(&port->vio, &vsw_ldc_cfg, port);
  287. if (err)
  288. goto err_out_free_dev;
  289. dev_set_drvdata(&vdev->dev, port);
  290. netif_napi_add(dev, &port->napi, sunvnet_poll_common,
  291. NAPI_POLL_WEIGHT);
  292. spin_lock_irqsave(&vp->lock, flags);
  293. list_add_rcu(&port->list, &vp->port_list);
  294. spin_unlock_irqrestore(&vp->lock, flags);
  295. timer_setup(&port->clean_timer, sunvnet_clean_timer_expire_common, 0);
  296. err = register_netdev(dev);
  297. if (err) {
  298. pr_err("Cannot register net device, aborting\n");
  299. goto err_out_del_timer;
  300. }
  301. spin_lock_irqsave(&vp->lock, flags);
  302. sunvnet_port_add_txq_common(port);
  303. spin_unlock_irqrestore(&vp->lock, flags);
  304. napi_enable(&port->napi);
  305. vio_port_up(&port->vio);
  306. /* assure no carrier until we receive an LDC_EVENT_UP,
  307. * even if the vsw config script tries to force us up
  308. */
  309. netif_carrier_off(dev);
  310. netdev_info(dev, "LDOM vsw-port %pM\n", dev->dev_addr);
  311. pr_info("%s: PORT ( remote-mac %pM%s )\n", dev->name,
  312. port->raddr, " switch-port");
  313. return 0;
  314. err_out_del_timer:
  315. del_timer_sync(&port->clean_timer);
  316. list_del_rcu(&port->list);
  317. synchronize_rcu();
  318. netif_napi_del(&port->napi);
  319. dev_set_drvdata(&vdev->dev, NULL);
  320. vio_ldc_free(&port->vio);
  321. err_out_free_dev:
  322. free_netdev(dev);
  323. return err;
  324. }
  325. static int vsw_port_remove(struct vio_dev *vdev)
  326. {
  327. struct vnet_port *port = dev_get_drvdata(&vdev->dev);
  328. unsigned long flags;
  329. if (port) {
  330. del_timer_sync(&port->vio.timer);
  331. del_timer_sync(&port->clean_timer);
  332. napi_disable(&port->napi);
  333. unregister_netdev(port->dev);
  334. list_del_rcu(&port->list);
  335. synchronize_rcu();
  336. spin_lock_irqsave(&port->vp->lock, flags);
  337. sunvnet_port_rm_txq_common(port);
  338. spin_unlock_irqrestore(&port->vp->lock, flags);
  339. netif_napi_del(&port->napi);
  340. sunvnet_port_free_tx_bufs_common(port);
  341. vio_ldc_free(&port->vio);
  342. dev_set_drvdata(&vdev->dev, NULL);
  343. free_netdev(port->dev);
  344. }
  345. return 0;
  346. }
  347. static void vsw_cleanup(void)
  348. {
  349. struct vnet *vp;
  350. /* just need to free up the vnet list */
  351. mutex_lock(&vnet_list_mutex);
  352. while (!list_empty(&vnet_list)) {
  353. vp = list_first_entry(&vnet_list, struct vnet, list);
  354. list_del(&vp->list);
  355. /* vio_unregister_driver() should have cleaned up port_list */
  356. if (!list_empty(&vp->port_list))
  357. pr_err("Ports not removed by VIO subsystem!\n");
  358. kfree(vp);
  359. }
  360. mutex_unlock(&vnet_list_mutex);
  361. }
  362. static const struct vio_device_id vsw_port_match[] = {
  363. {
  364. .type = "vsw-port",
  365. },
  366. {},
  367. };
  368. MODULE_DEVICE_TABLE(vio, vsw_port_match);
  369. static struct vio_driver vsw_port_driver = {
  370. .id_table = vsw_port_match,
  371. .probe = vsw_port_probe,
  372. .remove = vsw_port_remove,
  373. .name = "vsw_port",
  374. };
  375. static int __init vsw_init(void)
  376. {
  377. pr_info("%s\n", version);
  378. return vio_register_driver(&vsw_port_driver);
  379. }
  380. static void __exit vsw_exit(void)
  381. {
  382. vio_unregister_driver(&vsw_port_driver);
  383. vsw_cleanup();
  384. }
  385. module_init(vsw_init);
  386. module_exit(vsw_exit);