1 // SPDX-License-Identifier: GPL-2.0 2 /* sunvnet.c: Sun LDOM Virtual Network Driver. 3 * 4 * Copyright (C) 2007, 2008 David S. Miller <davem@davemloft.net> 5 * Copyright (C) 2016-2017 Oracle. All rights reserved. 6 */ 7 8 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt 9 10 #include <linux/module.h> 11 #include <linux/kernel.h> 12 #include <linux/types.h> 13 #include <linux/slab.h> 14 #include <linux/delay.h> 15 #include <linux/init.h> 16 #include <linux/netdevice.h> 17 #include <linux/ethtool.h> 18 #include <linux/etherdevice.h> 19 #include <linux/mutex.h> 20 #include <linux/highmem.h> 21 #include <linux/if_vlan.h> 22 23 #if IS_ENABLED(CONFIG_IPV6) 24 #include <linux/icmpv6.h> 25 #endif 26 27 #include <net/ip.h> 28 #include <net/icmp.h> 29 #include <net/route.h> 30 31 #include <asm/vio.h> 32 #include <asm/ldc.h> 33 34 #include "sunvnet_common.h" 35 36 /* length of time before we decide the hardware is borked, 37 * and dev->tx_timeout() should be called to fix the problem 38 */ 39 #define VNET_TX_TIMEOUT (5 * HZ) 40 41 #define DRV_MODULE_NAME "sunvnet" 42 #define DRV_MODULE_VERSION "2.0" 43 #define DRV_MODULE_RELDATE "February 3, 2017" 44 45 static char version[] = 46 DRV_MODULE_NAME " " DRV_MODULE_VERSION " (" DRV_MODULE_RELDATE ")"; 47 MODULE_AUTHOR("David S. Miller (davem@davemloft.net)"); 48 MODULE_DESCRIPTION("Sun LDOM virtual network driver"); 49 MODULE_LICENSE("GPL"); 50 MODULE_VERSION(DRV_MODULE_VERSION); 51 52 /* Ordered from largest major to lowest */ 53 static struct vio_version vnet_versions[] = { 54 { .major = 1, .minor = 8 }, 55 { .major = 1, .minor = 7 }, 56 { .major = 1, .minor = 6 }, 57 { .major = 1, .minor = 0 }, 58 }; 59 60 static void vnet_get_drvinfo(struct net_device *dev, 61 struct ethtool_drvinfo *info) 62 { 63 strlcpy(info->driver, DRV_MODULE_NAME, sizeof(info->driver)); 64 strlcpy(info->version, DRV_MODULE_VERSION, sizeof(info->version)); 65 } 66 67 static u32 vnet_get_msglevel(struct net_device *dev) 68 { 69 struct vnet *vp = netdev_priv(dev); 70 71 return vp->msg_enable; 72 } 73 74 static void vnet_set_msglevel(struct net_device *dev, u32 value) 75 { 76 struct vnet *vp = netdev_priv(dev); 77 78 vp->msg_enable = value; 79 } 80 81 static const struct { 82 const char string[ETH_GSTRING_LEN]; 83 } ethtool_stats_keys[] = { 84 { "rx_packets" }, 85 { "tx_packets" }, 86 { "rx_bytes" }, 87 { "tx_bytes" }, 88 { "rx_errors" }, 89 { "tx_errors" }, 90 { "rx_dropped" }, 91 { "tx_dropped" }, 92 { "multicast" }, 93 { "rx_length_errors" }, 94 { "rx_frame_errors" }, 95 { "rx_missed_errors" }, 96 { "tx_carrier_errors" }, 97 { "nports" }, 98 }; 99 100 static int vnet_get_sset_count(struct net_device *dev, int sset) 101 { 102 struct vnet *vp = (struct vnet *)netdev_priv(dev); 103 104 switch (sset) { 105 case ETH_SS_STATS: 106 return ARRAY_SIZE(ethtool_stats_keys) 107 + (NUM_VNET_PORT_STATS * vp->nports); 108 default: 109 return -EOPNOTSUPP; 110 } 111 } 112 113 static void vnet_get_strings(struct net_device *dev, u32 stringset, u8 *buf) 114 { 115 struct vnet *vp = (struct vnet *)netdev_priv(dev); 116 struct vnet_port *port; 117 char *p = (char *)buf; 118 119 switch (stringset) { 120 case ETH_SS_STATS: 121 memcpy(buf, ðtool_stats_keys, sizeof(ethtool_stats_keys)); 122 p += sizeof(ethtool_stats_keys); 123 124 rcu_read_lock(); 125 list_for_each_entry_rcu(port, &vp->port_list, list) { 126 snprintf(p, ETH_GSTRING_LEN, "p%u.%s-%pM", 127 port->q_index, port->switch_port ? "s" : "q", 128 port->raddr); 129 p += ETH_GSTRING_LEN; 130 snprintf(p, ETH_GSTRING_LEN, "p%u.rx_packets", 131 port->q_index); 132 p += ETH_GSTRING_LEN; 133 snprintf(p, ETH_GSTRING_LEN, "p%u.tx_packets", 134 port->q_index); 135 p += ETH_GSTRING_LEN; 136 snprintf(p, ETH_GSTRING_LEN, "p%u.rx_bytes", 137 port->q_index); 138 p += ETH_GSTRING_LEN; 139 snprintf(p, ETH_GSTRING_LEN, "p%u.tx_bytes", 140 port->q_index); 141 p += ETH_GSTRING_LEN; 142 snprintf(p, ETH_GSTRING_LEN, "p%u.event_up", 143 port->q_index); 144 p += ETH_GSTRING_LEN; 145 snprintf(p, ETH_GSTRING_LEN, "p%u.event_reset", 146 port->q_index); 147 p += ETH_GSTRING_LEN; 148 } 149 rcu_read_unlock(); 150 break; 151 default: 152 WARN_ON(1); 153 break; 154 } 155 } 156 157 static void vnet_get_ethtool_stats(struct net_device *dev, 158 struct ethtool_stats *estats, u64 *data) 159 { 160 struct vnet *vp = (struct vnet *)netdev_priv(dev); 161 struct vnet_port *port; 162 int i = 0; 163 164 data[i++] = dev->stats.rx_packets; 165 data[i++] = dev->stats.tx_packets; 166 data[i++] = dev->stats.rx_bytes; 167 data[i++] = dev->stats.tx_bytes; 168 data[i++] = dev->stats.rx_errors; 169 data[i++] = dev->stats.tx_errors; 170 data[i++] = dev->stats.rx_dropped; 171 data[i++] = dev->stats.tx_dropped; 172 data[i++] = dev->stats.multicast; 173 data[i++] = dev->stats.rx_length_errors; 174 data[i++] = dev->stats.rx_frame_errors; 175 data[i++] = dev->stats.rx_missed_errors; 176 data[i++] = dev->stats.tx_carrier_errors; 177 data[i++] = vp->nports; 178 179 rcu_read_lock(); 180 list_for_each_entry_rcu(port, &vp->port_list, list) { 181 data[i++] = port->q_index; 182 data[i++] = port->stats.rx_packets; 183 data[i++] = port->stats.tx_packets; 184 data[i++] = port->stats.rx_bytes; 185 data[i++] = port->stats.tx_bytes; 186 data[i++] = port->stats.event_up; 187 data[i++] = port->stats.event_reset; 188 } 189 rcu_read_unlock(); 190 } 191 192 static const struct ethtool_ops vnet_ethtool_ops = { 193 .get_drvinfo = vnet_get_drvinfo, 194 .get_msglevel = vnet_get_msglevel, 195 .set_msglevel = vnet_set_msglevel, 196 .get_link = ethtool_op_get_link, 197 .get_sset_count = vnet_get_sset_count, 198 .get_strings = vnet_get_strings, 199 .get_ethtool_stats = vnet_get_ethtool_stats, 200 }; 201 202 static LIST_HEAD(vnet_list); 203 static DEFINE_MUTEX(vnet_list_mutex); 204 205 static struct vnet_port *__tx_port_find(struct vnet *vp, struct sk_buff *skb) 206 { 207 unsigned int hash = vnet_hashfn(skb->data); 208 struct hlist_head *hp = &vp->port_hash[hash]; 209 struct vnet_port *port; 210 211 hlist_for_each_entry_rcu(port, hp, hash) { 212 if (!sunvnet_port_is_up_common(port)) 213 continue; 214 if (ether_addr_equal(port->raddr, skb->data)) 215 return port; 216 } 217 list_for_each_entry_rcu(port, &vp->port_list, list) { 218 if (!port->switch_port) 219 continue; 220 if (!sunvnet_port_is_up_common(port)) 221 continue; 222 return port; 223 } 224 return NULL; 225 } 226 227 /* func arg to vnet_start_xmit_common() to get the proper tx port */ 228 static struct vnet_port *vnet_tx_port_find(struct sk_buff *skb, 229 struct net_device *dev) 230 { 231 struct vnet *vp = netdev_priv(dev); 232 233 return __tx_port_find(vp, skb); 234 } 235 236 static u16 vnet_select_queue(struct net_device *dev, struct sk_buff *skb, 237 struct net_device *sb_dev, 238 select_queue_fallback_t fallback) 239 { 240 struct vnet *vp = netdev_priv(dev); 241 struct vnet_port *port = __tx_port_find(vp, skb); 242 243 if (!port) 244 return 0; 245 246 return port->q_index; 247 } 248 249 /* Wrappers to common functions */ 250 static int vnet_start_xmit(struct sk_buff *skb, struct net_device *dev) 251 { 252 return sunvnet_start_xmit_common(skb, dev, vnet_tx_port_find); 253 } 254 255 static void vnet_set_rx_mode(struct net_device *dev) 256 { 257 struct vnet *vp = netdev_priv(dev); 258 259 return sunvnet_set_rx_mode_common(dev, vp); 260 } 261 262 #ifdef CONFIG_NET_POLL_CONTROLLER 263 static void vnet_poll_controller(struct net_device *dev) 264 { 265 struct vnet *vp = netdev_priv(dev); 266 267 return sunvnet_poll_controller_common(dev, vp); 268 } 269 #endif 270 271 static const struct net_device_ops vnet_ops = { 272 .ndo_open = sunvnet_open_common, 273 .ndo_stop = sunvnet_close_common, 274 .ndo_set_rx_mode = vnet_set_rx_mode, 275 .ndo_set_mac_address = sunvnet_set_mac_addr_common, 276 .ndo_validate_addr = eth_validate_addr, 277 .ndo_tx_timeout = sunvnet_tx_timeout_common, 278 .ndo_start_xmit = vnet_start_xmit, 279 .ndo_select_queue = vnet_select_queue, 280 #ifdef CONFIG_NET_POLL_CONTROLLER 281 .ndo_poll_controller = vnet_poll_controller, 282 #endif 283 }; 284 285 static struct vnet *vnet_new(const u64 *local_mac, 286 struct vio_dev *vdev) 287 { 288 struct net_device *dev; 289 struct vnet *vp; 290 int err, i; 291 292 dev = alloc_etherdev_mqs(sizeof(*vp), VNET_MAX_TXQS, 1); 293 if (!dev) 294 return ERR_PTR(-ENOMEM); 295 dev->needed_headroom = VNET_PACKET_SKIP + 8; 296 dev->needed_tailroom = 8; 297 298 for (i = 0; i < ETH_ALEN; i++) 299 dev->dev_addr[i] = (*local_mac >> (5 - i) * 8) & 0xff; 300 301 vp = netdev_priv(dev); 302 303 spin_lock_init(&vp->lock); 304 vp->dev = dev; 305 306 INIT_LIST_HEAD(&vp->port_list); 307 for (i = 0; i < VNET_PORT_HASH_SIZE; i++) 308 INIT_HLIST_HEAD(&vp->port_hash[i]); 309 INIT_LIST_HEAD(&vp->list); 310 vp->local_mac = *local_mac; 311 312 dev->netdev_ops = &vnet_ops; 313 dev->ethtool_ops = &vnet_ethtool_ops; 314 dev->watchdog_timeo = VNET_TX_TIMEOUT; 315 316 dev->hw_features = NETIF_F_TSO | NETIF_F_GSO | NETIF_F_ALL_TSO | 317 NETIF_F_HW_CSUM | NETIF_F_SG; 318 dev->features = dev->hw_features; 319 320 /* MTU range: 68 - 65535 */ 321 dev->min_mtu = ETH_MIN_MTU; 322 dev->max_mtu = VNET_MAX_MTU; 323 324 SET_NETDEV_DEV(dev, &vdev->dev); 325 326 err = register_netdev(dev); 327 if (err) { 328 pr_err("Cannot register net device, aborting\n"); 329 goto err_out_free_dev; 330 } 331 332 netdev_info(dev, "Sun LDOM vnet %pM\n", dev->dev_addr); 333 334 list_add(&vp->list, &vnet_list); 335 336 return vp; 337 338 err_out_free_dev: 339 free_netdev(dev); 340 341 return ERR_PTR(err); 342 } 343 344 static struct vnet *vnet_find_or_create(const u64 *local_mac, 345 struct vio_dev *vdev) 346 { 347 struct vnet *iter, *vp; 348 349 mutex_lock(&vnet_list_mutex); 350 vp = NULL; 351 list_for_each_entry(iter, &vnet_list, list) { 352 if (iter->local_mac == *local_mac) { 353 vp = iter; 354 break; 355 } 356 } 357 if (!vp) 358 vp = vnet_new(local_mac, vdev); 359 mutex_unlock(&vnet_list_mutex); 360 361 return vp; 362 } 363 364 static void vnet_cleanup(void) 365 { 366 struct vnet *vp; 367 struct net_device *dev; 368 369 mutex_lock(&vnet_list_mutex); 370 while (!list_empty(&vnet_list)) { 371 vp = list_first_entry(&vnet_list, struct vnet, list); 372 list_del(&vp->list); 373 dev = vp->dev; 374 /* vio_unregister_driver() should have cleaned up port_list */ 375 BUG_ON(!list_empty(&vp->port_list)); 376 unregister_netdev(dev); 377 free_netdev(dev); 378 } 379 mutex_unlock(&vnet_list_mutex); 380 } 381 382 static const char *local_mac_prop = "local-mac-address"; 383 384 static struct vnet *vnet_find_parent(struct mdesc_handle *hp, 385 u64 port_node, 386 struct vio_dev *vdev) 387 { 388 const u64 *local_mac = NULL; 389 u64 a; 390 391 mdesc_for_each_arc(a, hp, port_node, MDESC_ARC_TYPE_BACK) { 392 u64 target = mdesc_arc_target(hp, a); 393 const char *name; 394 395 name = mdesc_get_property(hp, target, "name", NULL); 396 if (!name || strcmp(name, "network")) 397 continue; 398 399 local_mac = mdesc_get_property(hp, target, 400 local_mac_prop, NULL); 401 if (local_mac) 402 break; 403 } 404 if (!local_mac) 405 return ERR_PTR(-ENODEV); 406 407 return vnet_find_or_create(local_mac, vdev); 408 } 409 410 static struct ldc_channel_config vnet_ldc_cfg = { 411 .event = sunvnet_event_common, 412 .mtu = 64, 413 .mode = LDC_MODE_UNRELIABLE, 414 }; 415 416 static struct vio_driver_ops vnet_vio_ops = { 417 .send_attr = sunvnet_send_attr_common, 418 .handle_attr = sunvnet_handle_attr_common, 419 .handshake_complete = sunvnet_handshake_complete_common, 420 }; 421 422 const char *remote_macaddr_prop = "remote-mac-address"; 423 424 static int vnet_port_probe(struct vio_dev *vdev, const struct vio_device_id *id) 425 { 426 struct mdesc_handle *hp; 427 struct vnet_port *port; 428 unsigned long flags; 429 struct vnet *vp; 430 const u64 *rmac; 431 int len, i, err, switch_port; 432 433 hp = mdesc_grab(); 434 435 vp = vnet_find_parent(hp, vdev->mp, vdev); 436 if (IS_ERR(vp)) { 437 pr_err("Cannot find port parent vnet\n"); 438 err = PTR_ERR(vp); 439 goto err_out_put_mdesc; 440 } 441 442 rmac = mdesc_get_property(hp, vdev->mp, remote_macaddr_prop, &len); 443 err = -ENODEV; 444 if (!rmac) { 445 pr_err("Port lacks %s property\n", remote_macaddr_prop); 446 goto err_out_put_mdesc; 447 } 448 449 port = kzalloc(sizeof(*port), GFP_KERNEL); 450 err = -ENOMEM; 451 if (!port) 452 goto err_out_put_mdesc; 453 454 for (i = 0; i < ETH_ALEN; i++) 455 port->raddr[i] = (*rmac >> (5 - i) * 8) & 0xff; 456 457 port->vp = vp; 458 459 err = vio_driver_init(&port->vio, vdev, VDEV_NETWORK, 460 vnet_versions, ARRAY_SIZE(vnet_versions), 461 &vnet_vio_ops, vp->dev->name); 462 if (err) 463 goto err_out_free_port; 464 465 err = vio_ldc_alloc(&port->vio, &vnet_ldc_cfg, port); 466 if (err) 467 goto err_out_free_port; 468 469 netif_napi_add(port->vp->dev, &port->napi, sunvnet_poll_common, 470 NAPI_POLL_WEIGHT); 471 472 INIT_HLIST_NODE(&port->hash); 473 INIT_LIST_HEAD(&port->list); 474 475 switch_port = 0; 476 if (mdesc_get_property(hp, vdev->mp, "switch-port", NULL)) 477 switch_port = 1; 478 port->switch_port = switch_port; 479 port->tso = true; 480 port->tsolen = 0; 481 482 spin_lock_irqsave(&vp->lock, flags); 483 if (switch_port) 484 list_add_rcu(&port->list, &vp->port_list); 485 else 486 list_add_tail_rcu(&port->list, &vp->port_list); 487 hlist_add_head_rcu(&port->hash, 488 &vp->port_hash[vnet_hashfn(port->raddr)]); 489 sunvnet_port_add_txq_common(port); 490 spin_unlock_irqrestore(&vp->lock, flags); 491 492 dev_set_drvdata(&vdev->dev, port); 493 494 pr_info("%s: PORT ( remote-mac %pM%s )\n", 495 vp->dev->name, port->raddr, switch_port ? " switch-port" : ""); 496 497 timer_setup(&port->clean_timer, sunvnet_clean_timer_expire_common, 0); 498 499 napi_enable(&port->napi); 500 vio_port_up(&port->vio); 501 502 mdesc_release(hp); 503 504 return 0; 505 506 err_out_free_port: 507 kfree(port); 508 509 err_out_put_mdesc: 510 mdesc_release(hp); 511 return err; 512 } 513 514 static int vnet_port_remove(struct vio_dev *vdev) 515 { 516 struct vnet_port *port = dev_get_drvdata(&vdev->dev); 517 518 if (port) { 519 del_timer_sync(&port->vio.timer); 520 521 napi_disable(&port->napi); 522 523 list_del_rcu(&port->list); 524 hlist_del_rcu(&port->hash); 525 526 synchronize_rcu(); 527 del_timer_sync(&port->clean_timer); 528 sunvnet_port_rm_txq_common(port); 529 netif_napi_del(&port->napi); 530 sunvnet_port_free_tx_bufs_common(port); 531 vio_ldc_free(&port->vio); 532 533 dev_set_drvdata(&vdev->dev, NULL); 534 535 kfree(port); 536 } 537 return 0; 538 } 539 540 static const struct vio_device_id vnet_port_match[] = { 541 { 542 .type = "vnet-port", 543 }, 544 {}, 545 }; 546 MODULE_DEVICE_TABLE(vio, vnet_port_match); 547 548 static struct vio_driver vnet_port_driver = { 549 .id_table = vnet_port_match, 550 .probe = vnet_port_probe, 551 .remove = vnet_port_remove, 552 .name = "vnet_port", 553 }; 554 555 static int __init vnet_init(void) 556 { 557 pr_info("%s\n", version); 558 return vio_register_driver(&vnet_port_driver); 559 } 560 561 static void __exit vnet_exit(void) 562 { 563 vio_unregister_driver(&vnet_port_driver); 564 vnet_cleanup(); 565 } 566 567 module_init(vnet_init); 568 module_exit(vnet_exit); 569