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