1c861ef83SShannon Nelson // SPDX-License-Identifier: GPL-2.0 2e689cf4aSJeff Kirsher /* sunvnet.c: Sun LDOM Virtual Network Driver. 3e689cf4aSJeff Kirsher * 4e689cf4aSJeff Kirsher * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net> 50f512c84SShannon Nelson * Copyright (C) 2016-2017 Oracle. All rights reserved. 6e689cf4aSJeff Kirsher */ 7e689cf4aSJeff Kirsher 8e689cf4aSJeff Kirsher #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9e689cf4aSJeff Kirsher 10e689cf4aSJeff Kirsher #include <linux/module.h> 11e689cf4aSJeff Kirsher #include <linux/kernel.h> 12e689cf4aSJeff Kirsher #include <linux/types.h> 13e689cf4aSJeff Kirsher #include <linux/slab.h> 14e689cf4aSJeff Kirsher #include <linux/delay.h> 15e689cf4aSJeff Kirsher #include <linux/init.h> 16e689cf4aSJeff Kirsher #include <linux/netdevice.h> 17e689cf4aSJeff Kirsher #include <linux/ethtool.h> 18e689cf4aSJeff Kirsher #include <linux/etherdevice.h> 19e689cf4aSJeff Kirsher #include <linux/mutex.h> 20da38c564SDavid L Stevens #include <linux/highmem.h> 21e4defc77SDavid L Stevens #include <linux/if_vlan.h> 22e689cf4aSJeff Kirsher 23a2b78e9bSDavid L Stevens #if IS_ENABLED(CONFIG_IPV6) 24a2b78e9bSDavid L Stevens #include <linux/icmpv6.h> 25a2b78e9bSDavid L Stevens #endif 26a2b78e9bSDavid L Stevens 276d0ba919SDavid L Stevens #include <net/ip.h> 28a2b78e9bSDavid L Stevens #include <net/icmp.h> 29a2b78e9bSDavid L Stevens #include <net/route.h> 30a2b78e9bSDavid L Stevens 31e689cf4aSJeff Kirsher #include <asm/vio.h> 32e689cf4aSJeff Kirsher #include <asm/ldc.h> 33e689cf4aSJeff Kirsher 3431762eaaSAaron Young #include "sunvnet_common.h" 3531762eaaSAaron Young 3631762eaaSAaron Young /* length of time before we decide the hardware is borked, 3731762eaaSAaron Young * and dev->tx_timeout() should be called to fix the problem 3831762eaaSAaron Young */ 3931762eaaSAaron Young #define VNET_TX_TIMEOUT (5 * HZ) 40e689cf4aSJeff Kirsher 41e689cf4aSJeff Kirsher #define DRV_MODULE_NAME "sunvnet" 42f2f3e210SShannon Nelson #define DRV_MODULE_VERSION "2.0" 43f2f3e210SShannon Nelson #define DRV_MODULE_RELDATE "February 3, 2017" 44e689cf4aSJeff Kirsher 45f73d12bdSBill Pemberton static char version[] = 46f2f3e210SShannon Nelson DRV_MODULE_NAME " " DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")"; 47*6a57a219SAhelenia Ziemiańska MODULE_AUTHOR("David S. Miller <davem@davemloft.net>"); 48e689cf4aSJeff Kirsher MODULE_DESCRIPTION("Sun LDOM virtual network driver"); 49e689cf4aSJeff Kirsher MODULE_LICENSE("GPL"); 50e689cf4aSJeff Kirsher MODULE_VERSION(DRV_MODULE_VERSION); 51e689cf4aSJeff Kirsher 52e689cf4aSJeff Kirsher /* Ordered from largest major to lowest */ 53e689cf4aSJeff Kirsher static struct vio_version vnet_versions[] = { 546d0ba919SDavid L Stevens { .major = 1, .minor = 8 }, 556d0ba919SDavid L Stevens { .major = 1, .minor = 7 }, 56e4defc77SDavid L Stevens { .major = 1, .minor = 6 }, 57e689cf4aSJeff Kirsher { .major = 1, .minor = 0 }, 58e689cf4aSJeff Kirsher }; 59e689cf4aSJeff Kirsher 60e689cf4aSJeff Kirsher static void vnet_get_drvinfo(struct net_device *dev, 61e689cf4aSJeff Kirsher struct ethtool_drvinfo *info) 62e689cf4aSJeff Kirsher { 63f029c781SWolfram Sang strscpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver)); 64f029c781SWolfram Sang strscpy(info->version, DRV_MODULE_VERSION, sizeof(info->version)); 65e689cf4aSJeff Kirsher } 66e689cf4aSJeff Kirsher 67e689cf4aSJeff Kirsher static u32 vnet_get_msglevel(struct net_device *dev) 68e689cf4aSJeff Kirsher { 69e689cf4aSJeff Kirsher struct vnet *vp = netdev_priv(dev); 70dc153f85SAaron Young 71e689cf4aSJeff Kirsher return vp->msg_enable; 72e689cf4aSJeff Kirsher } 73e689cf4aSJeff Kirsher 74e689cf4aSJeff Kirsher static void vnet_set_msglevel(struct net_device *dev, u32 value) 75e689cf4aSJeff Kirsher { 76e689cf4aSJeff Kirsher struct vnet *vp = netdev_priv(dev); 77dc153f85SAaron Young 78e689cf4aSJeff Kirsher vp->msg_enable = value; 79e689cf4aSJeff Kirsher } 80e689cf4aSJeff Kirsher 810f512c84SShannon Nelson static const struct { 820f512c84SShannon Nelson const char string[ETH_GSTRING_LEN]; 830f512c84SShannon Nelson } ethtool_stats_keys[] = { 840f512c84SShannon Nelson { "rx_packets" }, 850f512c84SShannon Nelson { "tx_packets" }, 860f512c84SShannon Nelson { "rx_bytes" }, 870f512c84SShannon Nelson { "tx_bytes" }, 880f512c84SShannon Nelson { "rx_errors" }, 890f512c84SShannon Nelson { "tx_errors" }, 900f512c84SShannon Nelson { "rx_dropped" }, 910f512c84SShannon Nelson { "tx_dropped" }, 920f512c84SShannon Nelson { "multicast" }, 930f512c84SShannon Nelson { "rx_length_errors" }, 940f512c84SShannon Nelson { "rx_frame_errors" }, 950f512c84SShannon Nelson { "rx_missed_errors" }, 960f512c84SShannon Nelson { "tx_carrier_errors" }, 970f512c84SShannon Nelson { "nports" }, 980f512c84SShannon Nelson }; 990f512c84SShannon Nelson 1000f512c84SShannon Nelson static int vnet_get_sset_count(struct net_device *dev, int sset) 1010f512c84SShannon Nelson { 1020f512c84SShannon Nelson struct vnet *vp = (struct vnet *)netdev_priv(dev); 1030f512c84SShannon Nelson 1040f512c84SShannon Nelson switch (sset) { 1050f512c84SShannon Nelson case ETH_SS_STATS: 1060f512c84SShannon Nelson return ARRAY_SIZE(ethtool_stats_keys) 1070f512c84SShannon Nelson + (NUM_VNET_PORT_STATS * vp->nports); 1080f512c84SShannon Nelson default: 1090f512c84SShannon Nelson return -EOPNOTSUPP; 1100f512c84SShannon Nelson } 1110f512c84SShannon Nelson } 1120f512c84SShannon Nelson 1130f512c84SShannon Nelson static void vnet_get_strings(struct net_device *dev, u32 stringset, u8 *buf) 1140f512c84SShannon Nelson { 1150f512c84SShannon Nelson struct vnet *vp = (struct vnet *)netdev_priv(dev); 1160f512c84SShannon Nelson struct vnet_port *port; 1170f512c84SShannon Nelson char *p = (char *)buf; 1180f512c84SShannon Nelson 1190f512c84SShannon Nelson switch (stringset) { 1200f512c84SShannon Nelson case ETH_SS_STATS: 1210f512c84SShannon Nelson memcpy(buf, ðtool_stats_keys, sizeof(ethtool_stats_keys)); 1220f512c84SShannon Nelson p += sizeof(ethtool_stats_keys); 1230f512c84SShannon Nelson 1240f512c84SShannon Nelson rcu_read_lock(); 1250f512c84SShannon Nelson list_for_each_entry_rcu(port, &vp->port_list, list) { 1260f512c84SShannon Nelson snprintf(p, ETH_GSTRING_LEN, "p%u.%s-%pM", 1270f512c84SShannon Nelson port->q_index, port->switch_port ? "s" : "q", 1280f512c84SShannon Nelson port->raddr); 1290f512c84SShannon Nelson p += ETH_GSTRING_LEN; 1300f512c84SShannon Nelson snprintf(p, ETH_GSTRING_LEN, "p%u.rx_packets", 1310f512c84SShannon Nelson port->q_index); 1320f512c84SShannon Nelson p += ETH_GSTRING_LEN; 1330f512c84SShannon Nelson snprintf(p, ETH_GSTRING_LEN, "p%u.tx_packets", 1340f512c84SShannon Nelson port->q_index); 1350f512c84SShannon Nelson p += ETH_GSTRING_LEN; 1360f512c84SShannon Nelson snprintf(p, ETH_GSTRING_LEN, "p%u.rx_bytes", 1370f512c84SShannon Nelson port->q_index); 1380f512c84SShannon Nelson p += ETH_GSTRING_LEN; 1390f512c84SShannon Nelson snprintf(p, ETH_GSTRING_LEN, "p%u.tx_bytes", 1400f512c84SShannon Nelson port->q_index); 1410f512c84SShannon Nelson p += ETH_GSTRING_LEN; 1420f512c84SShannon Nelson snprintf(p, ETH_GSTRING_LEN, "p%u.event_up", 1430f512c84SShannon Nelson port->q_index); 1440f512c84SShannon Nelson p += ETH_GSTRING_LEN; 1450f512c84SShannon Nelson snprintf(p, ETH_GSTRING_LEN, "p%u.event_reset", 1460f512c84SShannon Nelson port->q_index); 1470f512c84SShannon Nelson p += ETH_GSTRING_LEN; 1480f512c84SShannon Nelson } 1490f512c84SShannon Nelson rcu_read_unlock(); 1500f512c84SShannon Nelson break; 1510f512c84SShannon Nelson default: 1520f512c84SShannon Nelson WARN_ON(1); 1530f512c84SShannon Nelson break; 1540f512c84SShannon Nelson } 1550f512c84SShannon Nelson } 1560f512c84SShannon Nelson 1570f512c84SShannon Nelson static void vnet_get_ethtool_stats(struct net_device *dev, 1580f512c84SShannon Nelson struct ethtool_stats *estats, u64 *data) 1590f512c84SShannon Nelson { 1600f512c84SShannon Nelson struct vnet *vp = (struct vnet *)netdev_priv(dev); 1610f512c84SShannon Nelson struct vnet_port *port; 1620f512c84SShannon Nelson int i = 0; 1630f512c84SShannon Nelson 1640f512c84SShannon Nelson data[i++] = dev->stats.rx_packets; 1650f512c84SShannon Nelson data[i++] = dev->stats.tx_packets; 1660f512c84SShannon Nelson data[i++] = dev->stats.rx_bytes; 1670f512c84SShannon Nelson data[i++] = dev->stats.tx_bytes; 1680f512c84SShannon Nelson data[i++] = dev->stats.rx_errors; 1690f512c84SShannon Nelson data[i++] = dev->stats.tx_errors; 1700f512c84SShannon Nelson data[i++] = dev->stats.rx_dropped; 1710f512c84SShannon Nelson data[i++] = dev->stats.tx_dropped; 1720f512c84SShannon Nelson data[i++] = dev->stats.multicast; 1730f512c84SShannon Nelson data[i++] = dev->stats.rx_length_errors; 1740f512c84SShannon Nelson data[i++] = dev->stats.rx_frame_errors; 1750f512c84SShannon Nelson data[i++] = dev->stats.rx_missed_errors; 1760f512c84SShannon Nelson data[i++] = dev->stats.tx_carrier_errors; 1770f512c84SShannon Nelson data[i++] = vp->nports; 1780f512c84SShannon Nelson 1790f512c84SShannon Nelson rcu_read_lock(); 1800f512c84SShannon Nelson list_for_each_entry_rcu(port, &vp->port_list, list) { 1810f512c84SShannon Nelson data[i++] = port->q_index; 1820f512c84SShannon Nelson data[i++] = port->stats.rx_packets; 1830f512c84SShannon Nelson data[i++] = port->stats.tx_packets; 1840f512c84SShannon Nelson data[i++] = port->stats.rx_bytes; 1850f512c84SShannon Nelson data[i++] = port->stats.tx_bytes; 1860f512c84SShannon Nelson data[i++] = port->stats.event_up; 1870f512c84SShannon Nelson data[i++] = port->stats.event_reset; 1880f512c84SShannon Nelson } 1890f512c84SShannon Nelson rcu_read_unlock(); 1900f512c84SShannon Nelson } 1910f512c84SShannon Nelson 192e689cf4aSJeff Kirsher static const struct ethtool_ops vnet_ethtool_ops = { 193e689cf4aSJeff Kirsher .get_drvinfo = vnet_get_drvinfo, 194e689cf4aSJeff Kirsher .get_msglevel = vnet_get_msglevel, 195e689cf4aSJeff Kirsher .set_msglevel = vnet_set_msglevel, 196e689cf4aSJeff Kirsher .get_link = ethtool_op_get_link, 1970f512c84SShannon Nelson .get_sset_count = vnet_get_sset_count, 1980f512c84SShannon Nelson .get_strings = vnet_get_strings, 1990f512c84SShannon Nelson .get_ethtool_stats = vnet_get_ethtool_stats, 200e689cf4aSJeff Kirsher }; 201e689cf4aSJeff Kirsher 202e689cf4aSJeff Kirsher static LIST_HEAD(vnet_list); 203e689cf4aSJeff Kirsher static DEFINE_MUTEX(vnet_list_mutex); 204e689cf4aSJeff Kirsher 20567d0719fSAaron Young static struct vnet_port *__tx_port_find(struct vnet *vp, struct sk_buff *skb) 20667d0719fSAaron Young { 20767d0719fSAaron Young unsigned int hash = vnet_hashfn(skb->data); 20867d0719fSAaron Young struct hlist_head *hp = &vp->port_hash[hash]; 20967d0719fSAaron Young struct vnet_port *port; 21067d0719fSAaron Young 21167d0719fSAaron Young hlist_for_each_entry_rcu(port, hp, hash) { 21267d0719fSAaron Young if (!sunvnet_port_is_up_common(port)) 21367d0719fSAaron Young continue; 21467d0719fSAaron Young if (ether_addr_equal(port->raddr, skb->data)) 21567d0719fSAaron Young return port; 21667d0719fSAaron Young } 21767d0719fSAaron Young list_for_each_entry_rcu(port, &vp->port_list, list) { 21867d0719fSAaron Young if (!port->switch_port) 21967d0719fSAaron Young continue; 22067d0719fSAaron Young if (!sunvnet_port_is_up_common(port)) 22167d0719fSAaron Young continue; 22267d0719fSAaron Young return port; 22367d0719fSAaron Young } 22467d0719fSAaron Young return NULL; 22567d0719fSAaron Young } 22667d0719fSAaron Young 22767d0719fSAaron Young /* func arg to vnet_start_xmit_common() to get the proper tx port */ 22867d0719fSAaron Young static struct vnet_port *vnet_tx_port_find(struct sk_buff *skb, 22967d0719fSAaron Young struct net_device *dev) 23067d0719fSAaron Young { 23167d0719fSAaron Young struct vnet *vp = netdev_priv(dev); 23267d0719fSAaron Young 23367d0719fSAaron Young return __tx_port_find(vp, skb); 23467d0719fSAaron Young } 23567d0719fSAaron Young 23667d0719fSAaron Young static u16 vnet_select_queue(struct net_device *dev, struct sk_buff *skb, 237a350ecceSPaolo Abeni struct net_device *sb_dev) 23867d0719fSAaron Young { 23967d0719fSAaron Young struct vnet *vp = netdev_priv(dev); 24067d0719fSAaron Young struct vnet_port *port = __tx_port_find(vp, skb); 24167d0719fSAaron Young 24267d0719fSAaron Young if (!port) 24367d0719fSAaron Young return 0; 24467d0719fSAaron Young 24567d0719fSAaron Young return port->q_index; 24667d0719fSAaron Young } 24767d0719fSAaron Young 24867d0719fSAaron Young /* Wrappers to common functions */ 2490e0cc31fSYueHaibing static netdev_tx_t vnet_start_xmit(struct sk_buff *skb, struct net_device *dev) 25067d0719fSAaron Young { 25167d0719fSAaron Young return sunvnet_start_xmit_common(skb, dev, vnet_tx_port_find); 25267d0719fSAaron Young } 25367d0719fSAaron Young 25467d0719fSAaron Young static void vnet_set_rx_mode(struct net_device *dev) 25567d0719fSAaron Young { 25667d0719fSAaron Young struct vnet *vp = netdev_priv(dev); 25767d0719fSAaron Young 25867d0719fSAaron Young return sunvnet_set_rx_mode_common(dev, vp); 25967d0719fSAaron Young } 26067d0719fSAaron Young 26167d0719fSAaron Young #ifdef CONFIG_NET_POLL_CONTROLLER 26267d0719fSAaron Young static void vnet_poll_controller(struct net_device *dev) 26367d0719fSAaron Young { 26467d0719fSAaron Young struct vnet *vp = netdev_priv(dev); 26567d0719fSAaron Young 26667d0719fSAaron Young return sunvnet_poll_controller_common(dev, vp); 26767d0719fSAaron Young } 26867d0719fSAaron Young #endif 26967d0719fSAaron Young 270e689cf4aSJeff Kirsher static const struct net_device_ops vnet_ops = { 27131762eaaSAaron Young .ndo_open = sunvnet_open_common, 27231762eaaSAaron Young .ndo_stop = sunvnet_close_common, 27367d0719fSAaron Young .ndo_set_rx_mode = vnet_set_rx_mode, 27431762eaaSAaron Young .ndo_set_mac_address = sunvnet_set_mac_addr_common, 275e689cf4aSJeff Kirsher .ndo_validate_addr = eth_validate_addr, 27631762eaaSAaron Young .ndo_tx_timeout = sunvnet_tx_timeout_common, 27767d0719fSAaron Young .ndo_start_xmit = vnet_start_xmit, 27867d0719fSAaron Young .ndo_select_queue = vnet_select_queue, 27969088822SSowmini Varadhan #ifdef CONFIG_NET_POLL_CONTROLLER 28067d0719fSAaron Young .ndo_poll_controller = vnet_poll_controller, 28169088822SSowmini Varadhan #endif 282e689cf4aSJeff Kirsher }; 283e689cf4aSJeff Kirsher 2844c5d283aSSowmini Varadhan static struct vnet *vnet_new(const u64 *local_mac, 2854c5d283aSSowmini Varadhan struct vio_dev *vdev) 286e689cf4aSJeff Kirsher { 287e689cf4aSJeff Kirsher struct net_device *dev; 2884abd7cffSJakub Kicinski u8 addr[ETH_ALEN]; 289e689cf4aSJeff Kirsher struct vnet *vp; 290e689cf4aSJeff Kirsher int err, i; 291e689cf4aSJeff Kirsher 292d51bffd1SSowmini Varadhan dev = alloc_etherdev_mqs(sizeof(*vp), VNET_MAX_TXQS, 1); 29341de8d4cSJoe Perches if (!dev) 294e689cf4aSJeff Kirsher return ERR_PTR(-ENOMEM); 2958e845f4cSDavid L Stevens dev->needed_headroom = VNET_PACKET_SKIP + 8; 2968e845f4cSDavid L Stevens dev->needed_tailroom = 8; 297e689cf4aSJeff Kirsher 298e689cf4aSJeff Kirsher for (i = 0; i < ETH_ALEN; i++) 2994abd7cffSJakub Kicinski addr[i] = (*local_mac >> (5 - i) * 8) & 0xff; 3004abd7cffSJakub Kicinski eth_hw_addr_set(dev, addr); 301e689cf4aSJeff Kirsher 302e689cf4aSJeff Kirsher vp = netdev_priv(dev); 303e689cf4aSJeff Kirsher 304e689cf4aSJeff Kirsher spin_lock_init(&vp->lock); 305e689cf4aSJeff Kirsher vp->dev = dev; 306e689cf4aSJeff Kirsher 307e689cf4aSJeff Kirsher INIT_LIST_HEAD(&vp->port_list); 308e689cf4aSJeff Kirsher for (i = 0; i < VNET_PORT_HASH_SIZE; i++) 309e689cf4aSJeff Kirsher INIT_HLIST_HEAD(&vp->port_hash[i]); 310e689cf4aSJeff Kirsher INIT_LIST_HEAD(&vp->list); 311e689cf4aSJeff Kirsher vp->local_mac = *local_mac; 312e689cf4aSJeff Kirsher 313e689cf4aSJeff Kirsher dev->netdev_ops = &vnet_ops; 314e689cf4aSJeff Kirsher dev->ethtool_ops = &vnet_ethtool_ops; 315e689cf4aSJeff Kirsher dev->watchdog_timeo = VNET_TX_TIMEOUT; 316e689cf4aSJeff Kirsher 317cf55612aSCathy Zhou dev->hw_features = NETIF_F_TSO | NETIF_F_GSO | NETIF_F_ALL_TSO | 31898524e04SShannon Nelson NETIF_F_HW_CSUM | NETIF_F_SG; 319da38c564SDavid L Stevens dev->features = dev->hw_features; 320da38c564SDavid L Stevens 321540bfe30SJarod Wilson /* MTU range: 68 - 65535 */ 322540bfe30SJarod Wilson dev->min_mtu = ETH_MIN_MTU; 323540bfe30SJarod Wilson dev->max_mtu = VNET_MAX_MTU; 324540bfe30SJarod Wilson 3254c5d283aSSowmini Varadhan SET_NETDEV_DEV(dev, &vdev->dev); 3264c5d283aSSowmini Varadhan 327e689cf4aSJeff Kirsher err = register_netdev(dev); 328e689cf4aSJeff Kirsher if (err) { 329e689cf4aSJeff Kirsher pr_err("Cannot register net device, aborting\n"); 330e689cf4aSJeff Kirsher goto err_out_free_dev; 331e689cf4aSJeff Kirsher } 332e689cf4aSJeff Kirsher 333e689cf4aSJeff Kirsher netdev_info(dev, "Sun LDOM vnet %pM\n", dev->dev_addr); 334e689cf4aSJeff Kirsher 335e689cf4aSJeff Kirsher list_add(&vp->list, &vnet_list); 336e689cf4aSJeff Kirsher 337e689cf4aSJeff Kirsher return vp; 338e689cf4aSJeff Kirsher 339e689cf4aSJeff Kirsher err_out_free_dev: 340e689cf4aSJeff Kirsher free_netdev(dev); 341e689cf4aSJeff Kirsher 342e689cf4aSJeff Kirsher return ERR_PTR(err); 343e689cf4aSJeff Kirsher } 344e689cf4aSJeff Kirsher 3454c5d283aSSowmini Varadhan static struct vnet *vnet_find_or_create(const u64 *local_mac, 3464c5d283aSSowmini Varadhan struct vio_dev *vdev) 347e689cf4aSJeff Kirsher { 348e689cf4aSJeff Kirsher struct vnet *iter, *vp; 349e689cf4aSJeff Kirsher 350e689cf4aSJeff Kirsher mutex_lock(&vnet_list_mutex); 351e689cf4aSJeff Kirsher vp = NULL; 352e689cf4aSJeff Kirsher list_for_each_entry(iter, &vnet_list, list) { 353e689cf4aSJeff Kirsher if (iter->local_mac == *local_mac) { 354e689cf4aSJeff Kirsher vp = iter; 355e689cf4aSJeff Kirsher break; 356e689cf4aSJeff Kirsher } 357e689cf4aSJeff Kirsher } 358e689cf4aSJeff Kirsher if (!vp) 3594c5d283aSSowmini Varadhan vp = vnet_new(local_mac, vdev); 360e689cf4aSJeff Kirsher mutex_unlock(&vnet_list_mutex); 361e689cf4aSJeff Kirsher 362e689cf4aSJeff Kirsher return vp; 363e689cf4aSJeff Kirsher } 364e689cf4aSJeff Kirsher 365a4b70a07SSowmini Varadhan static void vnet_cleanup(void) 366a4b70a07SSowmini Varadhan { 367a4b70a07SSowmini Varadhan struct vnet *vp; 368a4b70a07SSowmini Varadhan struct net_device *dev; 369a4b70a07SSowmini Varadhan 370a4b70a07SSowmini Varadhan mutex_lock(&vnet_list_mutex); 371a4b70a07SSowmini Varadhan while (!list_empty(&vnet_list)) { 372a4b70a07SSowmini Varadhan vp = list_first_entry(&vnet_list, struct vnet, list); 373a4b70a07SSowmini Varadhan list_del(&vp->list); 374a4b70a07SSowmini Varadhan dev = vp->dev; 375a4b70a07SSowmini Varadhan /* vio_unregister_driver() should have cleaned up port_list */ 376a4b70a07SSowmini Varadhan BUG_ON(!list_empty(&vp->port_list)); 377a4b70a07SSowmini Varadhan unregister_netdev(dev); 378a4b70a07SSowmini Varadhan free_netdev(dev); 379a4b70a07SSowmini Varadhan } 380a4b70a07SSowmini Varadhan mutex_unlock(&vnet_list_mutex); 381a4b70a07SSowmini Varadhan } 382a4b70a07SSowmini Varadhan 383e689cf4aSJeff Kirsher static const char *local_mac_prop = "local-mac-address"; 384e689cf4aSJeff Kirsher 385f73d12bdSBill Pemberton static struct vnet *vnet_find_parent(struct mdesc_handle *hp, 3864c5d283aSSowmini Varadhan u64 port_node, 3874c5d283aSSowmini Varadhan struct vio_dev *vdev) 388e689cf4aSJeff Kirsher { 389e689cf4aSJeff Kirsher const u64 *local_mac = NULL; 390e689cf4aSJeff Kirsher u64 a; 391e689cf4aSJeff Kirsher 392e689cf4aSJeff Kirsher mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) { 393e689cf4aSJeff Kirsher u64 target = mdesc_arc_target(hp, a); 394e689cf4aSJeff Kirsher const char *name; 395e689cf4aSJeff Kirsher 396e689cf4aSJeff Kirsher name = mdesc_get_property(hp, target, "name", NULL); 397e689cf4aSJeff Kirsher if (!name || strcmp(name, "network")) 398e689cf4aSJeff Kirsher continue; 399e689cf4aSJeff Kirsher 400e689cf4aSJeff Kirsher local_mac = mdesc_get_property(hp, target, 401e689cf4aSJeff Kirsher local_mac_prop, NULL); 402e689cf4aSJeff Kirsher if (local_mac) 403e689cf4aSJeff Kirsher break; 404e689cf4aSJeff Kirsher } 405e689cf4aSJeff Kirsher if (!local_mac) 406e689cf4aSJeff Kirsher return ERR_PTR(-ENODEV); 407e689cf4aSJeff Kirsher 4084c5d283aSSowmini Varadhan return vnet_find_or_create(local_mac, vdev); 409e689cf4aSJeff Kirsher } 410e689cf4aSJeff Kirsher 411e689cf4aSJeff Kirsher static struct ldc_channel_config vnet_ldc_cfg = { 41231762eaaSAaron Young .event = sunvnet_event_common, 413e689cf4aSJeff Kirsher .mtu = 64, 414e689cf4aSJeff Kirsher .mode = LDC_MODE_UNRELIABLE, 415e689cf4aSJeff Kirsher }; 416e689cf4aSJeff Kirsher 417e689cf4aSJeff Kirsher static struct vio_driver_ops vnet_vio_ops = { 41831762eaaSAaron Young .send_attr = sunvnet_send_attr_common, 41931762eaaSAaron Young .handle_attr = sunvnet_handle_attr_common, 42031762eaaSAaron Young .handshake_complete = sunvnet_handshake_complete_common, 421e689cf4aSJeff Kirsher }; 422e689cf4aSJeff Kirsher 423e689cf4aSJeff Kirsher const char *remote_macaddr_prop = "remote-mac-address"; 424e689cf4aSJeff Kirsher 4251dd06ae8SGreg Kroah-Hartman static int vnet_port_probe(struct vio_dev *vdev, const struct vio_device_id *id) 426e689cf4aSJeff Kirsher { 427e689cf4aSJeff Kirsher struct mdesc_handle *hp; 428e689cf4aSJeff Kirsher struct vnet_port *port; 429e689cf4aSJeff Kirsher unsigned long flags; 430e689cf4aSJeff Kirsher struct vnet *vp; 431e689cf4aSJeff Kirsher const u64 *rmac; 432e689cf4aSJeff Kirsher int len, i, err, switch_port; 433e689cf4aSJeff Kirsher 434e689cf4aSJeff Kirsher hp = mdesc_grab(); 435e689cf4aSJeff Kirsher 43690de546dSLiang He if (!hp) 43790de546dSLiang He return -ENODEV; 43890de546dSLiang He 4394c5d283aSSowmini Varadhan vp = vnet_find_parent(hp, vdev->mp, vdev); 440e689cf4aSJeff Kirsher if (IS_ERR(vp)) { 441e689cf4aSJeff Kirsher pr_err("Cannot find port parent vnet\n"); 442e689cf4aSJeff Kirsher err = PTR_ERR(vp); 443e689cf4aSJeff Kirsher goto err_out_put_mdesc; 444e689cf4aSJeff Kirsher } 445e689cf4aSJeff Kirsher 446e689cf4aSJeff Kirsher rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len); 447e689cf4aSJeff Kirsher err = -ENODEV; 448e689cf4aSJeff Kirsher if (!rmac) { 449e689cf4aSJeff Kirsher pr_err("Port lacks %s property\n", remote_macaddr_prop); 450e689cf4aSJeff Kirsher goto err_out_put_mdesc; 451e689cf4aSJeff Kirsher } 452e689cf4aSJeff Kirsher 453e689cf4aSJeff Kirsher port = kzalloc(sizeof(*port), GFP_KERNEL); 454e689cf4aSJeff Kirsher err = -ENOMEM; 455e404decbSJoe Perches if (!port) 456e689cf4aSJeff Kirsher goto err_out_put_mdesc; 457e689cf4aSJeff Kirsher 458e689cf4aSJeff Kirsher for (i = 0; i < ETH_ALEN; i++) 459e689cf4aSJeff Kirsher port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff; 460e689cf4aSJeff Kirsher 461e689cf4aSJeff Kirsher port->vp = vp; 462e689cf4aSJeff Kirsher 463e689cf4aSJeff Kirsher err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK, 464e689cf4aSJeff Kirsher vnet_versions, ARRAY_SIZE(vnet_versions), 465e689cf4aSJeff Kirsher &vnet_vio_ops, vp->dev->name); 466e689cf4aSJeff Kirsher if (err) 467e689cf4aSJeff Kirsher goto err_out_free_port; 468e689cf4aSJeff Kirsher 469e689cf4aSJeff Kirsher err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port); 470e689cf4aSJeff Kirsher if (err) 471e689cf4aSJeff Kirsher goto err_out_free_port; 472e689cf4aSJeff Kirsher 473b48b89f9SJakub Kicinski netif_napi_add(port->vp->dev, &port->napi, sunvnet_poll_common); 47469088822SSowmini Varadhan 475e689cf4aSJeff Kirsher INIT_HLIST_NODE(&port->hash); 476e689cf4aSJeff Kirsher INIT_LIST_HEAD(&port->list); 477e689cf4aSJeff Kirsher 478e689cf4aSJeff Kirsher switch_port = 0; 479dc153f85SAaron Young if (mdesc_get_property(hp, vdev->mp, "switch-port", NULL)) 480e689cf4aSJeff Kirsher switch_port = 1; 481e689cf4aSJeff Kirsher port->switch_port = switch_port; 482368e36edSDavid L Stevens port->tso = true; 483368e36edSDavid L Stevens port->tsolen = 0; 484e689cf4aSJeff Kirsher 485e689cf4aSJeff Kirsher spin_lock_irqsave(&vp->lock, flags); 486e689cf4aSJeff Kirsher if (switch_port) 4872a968dd8SSowmini Varadhan list_add_rcu(&port->list, &vp->port_list); 488e689cf4aSJeff Kirsher else 4892a968dd8SSowmini Varadhan list_add_tail_rcu(&port->list, &vp->port_list); 4902a968dd8SSowmini Varadhan hlist_add_head_rcu(&port->hash, 4912a968dd8SSowmini Varadhan &vp->port_hash[vnet_hashfn(port->raddr)]); 49231762eaaSAaron Young sunvnet_port_add_txq_common(port); 493e689cf4aSJeff Kirsher spin_unlock_irqrestore(&vp->lock, flags); 494e689cf4aSJeff Kirsher 495e689cf4aSJeff Kirsher dev_set_drvdata(&vdev->dev, port); 496e689cf4aSJeff Kirsher 497e689cf4aSJeff Kirsher pr_info("%s: PORT ( remote-mac %pM%s )\n", 498e689cf4aSJeff Kirsher vp->dev->name, port->raddr, switch_port ? " switch-port" : ""); 499e689cf4aSJeff Kirsher 5000822c5d9SKees Cook timer_setup(&port->clean_timer, sunvnet_clean_timer_expire_common, 0); 5018e845f4cSDavid L Stevens 50269088822SSowmini Varadhan napi_enable(&port->napi); 503e689cf4aSJeff Kirsher vio_port_up(&port->vio); 504e689cf4aSJeff Kirsher 505e689cf4aSJeff Kirsher mdesc_release(hp); 506e689cf4aSJeff Kirsher 507e689cf4aSJeff Kirsher return 0; 508e689cf4aSJeff Kirsher 509e689cf4aSJeff Kirsher err_out_free_port: 510e689cf4aSJeff Kirsher kfree(port); 511e689cf4aSJeff Kirsher 512e689cf4aSJeff Kirsher err_out_put_mdesc: 513e689cf4aSJeff Kirsher mdesc_release(hp); 514e689cf4aSJeff Kirsher return err; 515e689cf4aSJeff Kirsher } 516e689cf4aSJeff Kirsher 5171553573cSUwe Kleine-König static void vnet_port_remove(struct vio_dev *vdev) 518e689cf4aSJeff Kirsher { 519e689cf4aSJeff Kirsher struct vnet_port *port = dev_get_drvdata(&vdev->dev); 520e689cf4aSJeff Kirsher 521e689cf4aSJeff Kirsher if (port) { 522e689cf4aSJeff Kirsher del_timer_sync(&port->vio.timer); 523e689cf4aSJeff Kirsher 52469088822SSowmini Varadhan napi_disable(&port->napi); 525e689cf4aSJeff Kirsher 5262a968dd8SSowmini Varadhan list_del_rcu(&port->list); 5272a968dd8SSowmini Varadhan hlist_del_rcu(&port->hash); 5282a968dd8SSowmini Varadhan 5292a968dd8SSowmini Varadhan synchronize_rcu(); 530292a089dSSteven Rostedt (Google) timer_shutdown_sync(&port->clean_timer); 53131762eaaSAaron Young sunvnet_port_rm_txq_common(port); 53269088822SSowmini Varadhan netif_napi_del(&port->napi); 53331762eaaSAaron Young sunvnet_port_free_tx_bufs_common(port); 534e689cf4aSJeff Kirsher vio_ldc_free(&port->vio); 535e689cf4aSJeff Kirsher 536e689cf4aSJeff Kirsher dev_set_drvdata(&vdev->dev, NULL); 537e689cf4aSJeff Kirsher 538e689cf4aSJeff Kirsher kfree(port); 539e689cf4aSJeff Kirsher } 540e689cf4aSJeff Kirsher } 541e689cf4aSJeff Kirsher 542e689cf4aSJeff Kirsher static const struct vio_device_id vnet_port_match[] = { 543e689cf4aSJeff Kirsher { 544e689cf4aSJeff Kirsher .type = "vnet-port", 545e689cf4aSJeff Kirsher }, 546e689cf4aSJeff Kirsher {}, 547e689cf4aSJeff Kirsher }; 548e689cf4aSJeff Kirsher MODULE_DEVICE_TABLE(vio, vnet_port_match); 549e689cf4aSJeff Kirsher 550e689cf4aSJeff Kirsher static struct vio_driver vnet_port_driver = { 551e689cf4aSJeff Kirsher .id_table = vnet_port_match, 552e689cf4aSJeff Kirsher .probe = vnet_port_probe, 553e689cf4aSJeff Kirsher .remove = vnet_port_remove, 554e689cf4aSJeff Kirsher .name = "vnet_port", 555e689cf4aSJeff Kirsher }; 556e689cf4aSJeff Kirsher 557e689cf4aSJeff Kirsher static int __init vnet_init(void) 558e689cf4aSJeff Kirsher { 559f2f3e210SShannon Nelson pr_info("%s\n", version); 560e689cf4aSJeff Kirsher return vio_register_driver(&vnet_port_driver); 561e689cf4aSJeff Kirsher } 562e689cf4aSJeff Kirsher 563e689cf4aSJeff Kirsher static void __exit vnet_exit(void) 564e689cf4aSJeff Kirsher { 565e689cf4aSJeff Kirsher vio_unregister_driver(&vnet_port_driver); 566a4b70a07SSowmini Varadhan vnet_cleanup(); 567e689cf4aSJeff Kirsher } 568e689cf4aSJeff Kirsher 569e689cf4aSJeff Kirsher module_init(vnet_init); 570e689cf4aSJeff Kirsher module_exit(vnet_exit); 571