Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /* ldmvsw.c: Sun4v LDOM Virtual Switch Driver.
0003  *
0004  * Copyright (C) 2016-2017 Oracle. All rights reserved.
0005  */
0006 
0007 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0008 
0009 #include <linux/delay.h>
0010 #include <linux/etherdevice.h>
0011 #include <linux/ethtool.h>
0012 #include <linux/highmem.h>
0013 #include <linux/if_vlan.h>
0014 #include <linux/init.h>
0015 #include <linux/kernel.h>
0016 #include <linux/module.h>
0017 #include <linux/mutex.h>
0018 #include <linux/netdevice.h>
0019 #include <linux/slab.h>
0020 #include <linux/types.h>
0021 
0022 #if defined(CONFIG_IPV6)
0023 #include <linux/icmpv6.h>
0024 #endif
0025 
0026 #include <net/ip.h>
0027 #include <net/icmp.h>
0028 #include <net/route.h>
0029 
0030 #include <asm/vio.h>
0031 #include <asm/ldc.h>
0032 
0033 /* This driver makes use of the common code in sunvnet_common.c */
0034 #include "sunvnet_common.h"
0035 
0036 /* Length of time before we decide the hardware is hung,
0037  * and dev->tx_timeout() should be called to fix the problem.
0038  */
0039 #define VSW_TX_TIMEOUT          (10 * HZ)
0040 
0041 /* Static HW Addr used for the network interfaces representing vsw ports */
0042 static u8 vsw_port_hwaddr[ETH_ALEN] = {0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
0043 
0044 #define DRV_MODULE_NAME     "ldmvsw"
0045 #define DRV_MODULE_VERSION  "1.2"
0046 #define DRV_MODULE_RELDATE  "March 4, 2017"
0047 
0048 static char version[] =
0049     DRV_MODULE_NAME " " DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")";
0050 MODULE_AUTHOR("Oracle");
0051 MODULE_DESCRIPTION("Sun4v LDOM Virtual Switch Driver");
0052 MODULE_LICENSE("GPL");
0053 MODULE_VERSION(DRV_MODULE_VERSION);
0054 
0055 /* Ordered from largest major to lowest */
0056 static struct vio_version vsw_versions[] = {
0057     { .major = 1, .minor = 8 },
0058     { .major = 1, .minor = 7 },
0059     { .major = 1, .minor = 6 },
0060     { .major = 1, .minor = 0 },
0061 };
0062 
0063 static void vsw_get_drvinfo(struct net_device *dev,
0064                 struct ethtool_drvinfo *info)
0065 {
0066     strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver));
0067     strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version));
0068 }
0069 
0070 static u32 vsw_get_msglevel(struct net_device *dev)
0071 {
0072     struct vnet_port *port = netdev_priv(dev);
0073 
0074     return port->vp->msg_enable;
0075 }
0076 
0077 static void vsw_set_msglevel(struct net_device *dev, u32 value)
0078 {
0079     struct vnet_port *port = netdev_priv(dev);
0080 
0081     port->vp->msg_enable = value;
0082 }
0083 
0084 static const struct ethtool_ops vsw_ethtool_ops = {
0085     .get_drvinfo        = vsw_get_drvinfo,
0086     .get_msglevel       = vsw_get_msglevel,
0087     .set_msglevel       = vsw_set_msglevel,
0088     .get_link       = ethtool_op_get_link,
0089 };
0090 
0091 static LIST_HEAD(vnet_list);
0092 static DEFINE_MUTEX(vnet_list_mutex);
0093 
0094 /* func arg to vnet_start_xmit_common() to get the proper tx port */
0095 static struct vnet_port *vsw_tx_port_find(struct sk_buff *skb,
0096                       struct net_device *dev)
0097 {
0098     struct vnet_port *port = netdev_priv(dev);
0099 
0100     return port;
0101 }
0102 
0103 static u16 vsw_select_queue(struct net_device *dev, struct sk_buff *skb,
0104                 struct net_device *sb_dev)
0105 {
0106     struct vnet_port *port = netdev_priv(dev);
0107 
0108     if (!port)
0109         return 0;
0110 
0111     return port->q_index;
0112 }
0113 
0114 /* Wrappers to common functions */
0115 static netdev_tx_t vsw_start_xmit(struct sk_buff *skb, struct net_device *dev)
0116 {
0117     return sunvnet_start_xmit_common(skb, dev, vsw_tx_port_find);
0118 }
0119 
0120 static void vsw_set_rx_mode(struct net_device *dev)
0121 {
0122     struct vnet_port *port = netdev_priv(dev);
0123 
0124     return sunvnet_set_rx_mode_common(dev, port->vp);
0125 }
0126 
0127 int ldmvsw_open(struct net_device *dev)
0128 {
0129     struct vnet_port *port = netdev_priv(dev);
0130     struct vio_driver_state *vio = &port->vio;
0131 
0132     /* reset the channel */
0133     vio_link_state_change(vio, LDC_EVENT_RESET);
0134     vnet_port_reset(port);
0135     vio_port_up(vio);
0136 
0137     return 0;
0138 }
0139 EXPORT_SYMBOL_GPL(ldmvsw_open);
0140 
0141 #ifdef CONFIG_NET_POLL_CONTROLLER
0142 static void vsw_poll_controller(struct net_device *dev)
0143 {
0144     struct vnet_port *port = netdev_priv(dev);
0145 
0146     return sunvnet_poll_controller_common(dev, port->vp);
0147 }
0148 #endif
0149 
0150 static const struct net_device_ops vsw_ops = {
0151     .ndo_open       = ldmvsw_open,
0152     .ndo_stop       = sunvnet_close_common,
0153     .ndo_set_rx_mode    = vsw_set_rx_mode,
0154     .ndo_set_mac_address    = sunvnet_set_mac_addr_common,
0155     .ndo_validate_addr  = eth_validate_addr,
0156     .ndo_tx_timeout     = sunvnet_tx_timeout_common,
0157     .ndo_start_xmit     = vsw_start_xmit,
0158     .ndo_select_queue   = vsw_select_queue,
0159 #ifdef CONFIG_NET_POLL_CONTROLLER
0160     .ndo_poll_controller    = vsw_poll_controller,
0161 #endif
0162 };
0163 
0164 static const char *local_mac_prop = "local-mac-address";
0165 static const char *cfg_handle_prop = "cfg-handle";
0166 
0167 static struct vnet *vsw_get_vnet(struct mdesc_handle *hp,
0168                  u64 port_node,
0169                  u64 *handle)
0170 {
0171     struct vnet *vp;
0172     struct vnet *iter;
0173     const u64 *local_mac = NULL;
0174     const u64 *cfghandle = NULL;
0175     u64 a;
0176 
0177     /* Get the parent virtual-network-switch macaddr and cfghandle */
0178     mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) {
0179         u64 target = mdesc_arc_target(hp, a);
0180         const char *name;
0181 
0182         name = mdesc_get_property(hp, target, "name", NULL);
0183         if (!name || strcmp(name, "virtual-network-switch"))
0184             continue;
0185 
0186         local_mac = mdesc_get_property(hp, target,
0187                            local_mac_prop, NULL);
0188         cfghandle = mdesc_get_property(hp, target,
0189                            cfg_handle_prop, NULL);
0190         break;
0191     }
0192     if (!local_mac || !cfghandle)
0193         return ERR_PTR(-ENODEV);
0194 
0195     /* find or create associated vnet */
0196     vp = NULL;
0197     mutex_lock(&vnet_list_mutex);
0198     list_for_each_entry(iter, &vnet_list, list) {
0199         if (iter->local_mac == *local_mac) {
0200             vp = iter;
0201             break;
0202         }
0203     }
0204 
0205     if (!vp) {
0206         vp = kzalloc(sizeof(*vp), GFP_KERNEL);
0207         if (unlikely(!vp)) {
0208             mutex_unlock(&vnet_list_mutex);
0209             return ERR_PTR(-ENOMEM);
0210         }
0211 
0212         spin_lock_init(&vp->lock);
0213         INIT_LIST_HEAD(&vp->port_list);
0214         INIT_LIST_HEAD(&vp->list);
0215         vp->local_mac = *local_mac;
0216         list_add(&vp->list, &vnet_list);
0217     }
0218 
0219     mutex_unlock(&vnet_list_mutex);
0220 
0221     *handle = (u64)*cfghandle;
0222 
0223     return vp;
0224 }
0225 
0226 static struct net_device *vsw_alloc_netdev(u8 hwaddr[],
0227                        struct vio_dev *vdev,
0228                        u64 handle,
0229                        u64 port_id)
0230 {
0231     struct net_device *dev;
0232     struct vnet_port *port;
0233 
0234     dev = alloc_etherdev_mqs(sizeof(*port), VNET_MAX_TXQS, 1);
0235     if (!dev)
0236         return ERR_PTR(-ENOMEM);
0237     dev->needed_headroom = VNET_PACKET_SKIP + 8;
0238     dev->needed_tailroom = 8;
0239 
0240     eth_hw_addr_set(dev, hwaddr);
0241     ether_addr_copy(dev->perm_addr, dev->dev_addr);
0242 
0243     sprintf(dev->name, "vif%d.%d", (int)handle, (int)port_id);
0244 
0245     dev->netdev_ops = &vsw_ops;
0246     dev->ethtool_ops = &vsw_ethtool_ops;
0247     dev->watchdog_timeo = VSW_TX_TIMEOUT;
0248 
0249     dev->hw_features = NETIF_F_HW_CSUM | NETIF_F_SG;
0250     dev->features = dev->hw_features;
0251 
0252     /* MTU range: 68 - 65535 */
0253     dev->min_mtu = ETH_MIN_MTU;
0254     dev->max_mtu = VNET_MAX_MTU;
0255 
0256     SET_NETDEV_DEV(dev, &vdev->dev);
0257 
0258     return dev;
0259 }
0260 
0261 static struct ldc_channel_config vsw_ldc_cfg = {
0262     .event      = sunvnet_event_common,
0263     .mtu        = 64,
0264     .mode       = LDC_MODE_UNRELIABLE,
0265 };
0266 
0267 static struct vio_driver_ops vsw_vio_ops = {
0268     .send_attr      = sunvnet_send_attr_common,
0269     .handle_attr        = sunvnet_handle_attr_common,
0270     .handshake_complete = sunvnet_handshake_complete_common,
0271 };
0272 
0273 static const char *remote_macaddr_prop = "remote-mac-address";
0274 static const char *id_prop = "id";
0275 
0276 static int vsw_port_probe(struct vio_dev *vdev, const struct vio_device_id *id)
0277 {
0278     struct mdesc_handle *hp;
0279     struct vnet_port *port;
0280     unsigned long flags;
0281     struct vnet *vp;
0282     struct net_device *dev;
0283     const u64 *rmac;
0284     int len, i, err;
0285     const u64 *port_id;
0286     u64 handle;
0287 
0288     hp = mdesc_grab();
0289 
0290     rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len);
0291     err = -ENODEV;
0292     if (!rmac) {
0293         pr_err("Port lacks %s property\n", remote_macaddr_prop);
0294         mdesc_release(hp);
0295         return err;
0296     }
0297 
0298     port_id = mdesc_get_property(hp, vdev->mp, id_prop, NULL);
0299     err = -ENODEV;
0300     if (!port_id) {
0301         pr_err("Port lacks %s property\n", id_prop);
0302         mdesc_release(hp);
0303         return err;
0304     }
0305 
0306     /* Get (or create) the vnet associated with this port */
0307     vp = vsw_get_vnet(hp, vdev->mp, &handle);
0308     if (IS_ERR(vp)) {
0309         err = PTR_ERR(vp);
0310         pr_err("Failed to get vnet for vsw-port\n");
0311         mdesc_release(hp);
0312         return err;
0313     }
0314 
0315     mdesc_release(hp);
0316 
0317     dev = vsw_alloc_netdev(vsw_port_hwaddr, vdev, handle, *port_id);
0318     if (IS_ERR(dev)) {
0319         err = PTR_ERR(dev);
0320         pr_err("Failed to alloc netdev for vsw-port\n");
0321         return err;
0322     }
0323 
0324     port = netdev_priv(dev);
0325 
0326     INIT_LIST_HEAD(&port->list);
0327 
0328     for (i = 0; i < ETH_ALEN; i++)
0329         port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff;
0330 
0331     port->vp = vp;
0332     port->dev = dev;
0333     port->switch_port = 1;
0334     port->tso = false; /* no tso in vsw, misbehaves in bridge */
0335     port->tsolen = 0;
0336 
0337     /* Mark the port as belonging to ldmvsw which directs the
0338      * common code to use the net_device in the vnet_port
0339      * rather than the net_device in the vnet (which is used
0340      * by sunvnet). This bit is used by the VNET_PORT_TO_NET_DEVICE
0341      * macro.
0342      */
0343     port->vsw = 1;
0344 
0345     err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK,
0346                   vsw_versions, ARRAY_SIZE(vsw_versions),
0347                   &vsw_vio_ops, dev->name);
0348     if (err)
0349         goto err_out_free_dev;
0350 
0351     err = vio_ldc_alloc(&port->vio, &vsw_ldc_cfg, port);
0352     if (err)
0353         goto err_out_free_dev;
0354 
0355     dev_set_drvdata(&vdev->dev, port);
0356 
0357     netif_napi_add(dev, &port->napi, sunvnet_poll_common,
0358                NAPI_POLL_WEIGHT);
0359 
0360     spin_lock_irqsave(&vp->lock, flags);
0361     list_add_rcu(&port->list, &vp->port_list);
0362     spin_unlock_irqrestore(&vp->lock, flags);
0363 
0364     timer_setup(&port->clean_timer, sunvnet_clean_timer_expire_common, 0);
0365 
0366     err = register_netdev(dev);
0367     if (err) {
0368         pr_err("Cannot register net device, aborting\n");
0369         goto err_out_del_timer;
0370     }
0371 
0372     spin_lock_irqsave(&vp->lock, flags);
0373     sunvnet_port_add_txq_common(port);
0374     spin_unlock_irqrestore(&vp->lock, flags);
0375 
0376     napi_enable(&port->napi);
0377     vio_port_up(&port->vio);
0378 
0379     /* assure no carrier until we receive an LDC_EVENT_UP,
0380      * even if the vsw config script tries to force us up
0381      */
0382     netif_carrier_off(dev);
0383 
0384     netdev_info(dev, "LDOM vsw-port %pM\n", dev->dev_addr);
0385 
0386     pr_info("%s: PORT ( remote-mac %pM%s )\n", dev->name,
0387         port->raddr, " switch-port");
0388 
0389     return 0;
0390 
0391 err_out_del_timer:
0392     del_timer_sync(&port->clean_timer);
0393     list_del_rcu(&port->list);
0394     synchronize_rcu();
0395     netif_napi_del(&port->napi);
0396     dev_set_drvdata(&vdev->dev, NULL);
0397     vio_ldc_free(&port->vio);
0398 
0399 err_out_free_dev:
0400     free_netdev(dev);
0401     return err;
0402 }
0403 
0404 static void vsw_port_remove(struct vio_dev *vdev)
0405 {
0406     struct vnet_port *port = dev_get_drvdata(&vdev->dev);
0407     unsigned long flags;
0408 
0409     if (port) {
0410         del_timer_sync(&port->vio.timer);
0411         del_timer_sync(&port->clean_timer);
0412 
0413         napi_disable(&port->napi);
0414         unregister_netdev(port->dev);
0415 
0416         list_del_rcu(&port->list);
0417 
0418         synchronize_rcu();
0419         spin_lock_irqsave(&port->vp->lock, flags);
0420         sunvnet_port_rm_txq_common(port);
0421         spin_unlock_irqrestore(&port->vp->lock, flags);
0422         netif_napi_del(&port->napi);
0423         sunvnet_port_free_tx_bufs_common(port);
0424         vio_ldc_free(&port->vio);
0425 
0426         dev_set_drvdata(&vdev->dev, NULL);
0427 
0428         free_netdev(port->dev);
0429     }
0430 }
0431 
0432 static void vsw_cleanup(void)
0433 {
0434     struct vnet *vp;
0435 
0436     /* just need to free up the vnet list */
0437     mutex_lock(&vnet_list_mutex);
0438     while (!list_empty(&vnet_list)) {
0439         vp = list_first_entry(&vnet_list, struct vnet, list);
0440         list_del(&vp->list);
0441         /* vio_unregister_driver() should have cleaned up port_list */
0442         if (!list_empty(&vp->port_list))
0443             pr_err("Ports not removed by VIO subsystem!\n");
0444         kfree(vp);
0445     }
0446     mutex_unlock(&vnet_list_mutex);
0447 }
0448 
0449 static const struct vio_device_id vsw_port_match[] = {
0450     {
0451         .type = "vsw-port",
0452     },
0453     {},
0454 };
0455 MODULE_DEVICE_TABLE(vio, vsw_port_match);
0456 
0457 static struct vio_driver vsw_port_driver = {
0458     .id_table   = vsw_port_match,
0459     .probe      = vsw_port_probe,
0460     .remove     = vsw_port_remove,
0461     .name       = "vsw_port",
0462 };
0463 
0464 static int __init vsw_init(void)
0465 {
0466     pr_info("%s\n", version);
0467     return vio_register_driver(&vsw_port_driver);
0468 }
0469 
0470 static void __exit vsw_exit(void)
0471 {
0472     vio_unregister_driver(&vsw_port_driver);
0473     vsw_cleanup();
0474 }
0475 
0476 module_init(vsw_init);
0477 module_exit(vsw_exit);