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