ldmvsw.c 11 KB

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