1 /* 2 * Copyright (c) 2004 Topspin Communications. All rights reserved. 3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. 4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved. 5 * 6 * This software is available to you under a choice of one of two 7 * licenses. You may choose to be licensed under the terms of the GNU 8 * General Public License (GPL) Version 2, available from the file 9 * COPYING in the main directory of this source tree, or the 10 * OpenIB.org BSD license below: 11 * 12 * Redistribution and use in source and binary forms, with or 13 * without modification, are permitted provided that the following 14 * conditions are met: 15 * 16 * - Redistributions of source code must retain the above 17 * copyright notice, this list of conditions and the following 18 * disclaimer. 19 * 20 * - Redistributions in binary form must reproduce the above 21 * copyright notice, this list of conditions and the following 22 * disclaimer in the documentation and/or other materials 23 * provided with the distribution. 24 * 25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 32 * SOFTWARE. 33 */ 34 35 #include "ipoib.h" 36 37 #include <linux/module.h> 38 39 #include <linux/init.h> 40 #include <linux/slab.h> 41 #include <linux/kernel.h> 42 #include <linux/vmalloc.h> 43 44 #include <linux/if_arp.h> /* For ARPHRD_xxx */ 45 46 #include <linux/ip.h> 47 #include <linux/in.h> 48 49 #include <linux/jhash.h> 50 #include <net/arp.h> 51 #include <net/addrconf.h> 52 #include <net/netdev_lock.h> 53 #include <net/pkt_sched.h> 54 #include <linux/inetdevice.h> 55 #include <rdma/ib_cache.h> 56 57 MODULE_AUTHOR("Roland Dreier"); 58 MODULE_DESCRIPTION("IP-over-InfiniBand net driver"); 59 MODULE_LICENSE("Dual BSD/GPL"); 60 61 int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE; 62 int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE; 63 64 module_param_named(send_queue_size, ipoib_sendq_size, int, 0444); 65 MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue"); 66 module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444); 67 MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue"); 68 69 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG 70 int ipoib_debug_level; 71 72 module_param_named(debug_level, ipoib_debug_level, int, 0644); 73 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0"); 74 #endif 75 76 struct ipoib_path_iter { 77 struct net_device *dev; 78 struct ipoib_path path; 79 }; 80 81 static const u8 ipv4_bcast_addr[] = { 82 0x00, 0xff, 0xff, 0xff, 83 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00, 84 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff 85 }; 86 87 struct workqueue_struct *ipoib_workqueue; 88 89 struct ib_sa_client ipoib_sa_client; 90 91 static int ipoib_add_one(struct ib_device *device); 92 static void ipoib_remove_one(struct ib_device *device, void *client_data); 93 static void ipoib_neigh_reclaim(struct rcu_head *rp); 94 static struct net_device *ipoib_get_net_dev_by_params( 95 struct ib_device *dev, u32 port, u16 pkey, 96 const union ib_gid *gid, const struct sockaddr *addr, 97 void *client_data); 98 static int ipoib_set_mac(struct net_device *dev, void *addr); 99 static int ipoib_ioctl(struct net_device *dev, struct ifreq *ifr, 100 int cmd); 101 102 static struct ib_client ipoib_client = { 103 .name = "ipoib", 104 .add = ipoib_add_one, 105 .remove = ipoib_remove_one, 106 .get_net_dev_by_params = ipoib_get_net_dev_by_params, 107 }; 108 109 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG 110 static int ipoib_netdev_event(struct notifier_block *this, 111 unsigned long event, void *ptr) 112 { 113 struct netdev_notifier_info *ni = ptr; 114 struct net_device *dev = ni->dev; 115 116 if (dev->netdev_ops->ndo_open != ipoib_open) 117 return NOTIFY_DONE; 118 119 switch (event) { 120 case NETDEV_REGISTER: 121 ipoib_create_debug_files(dev); 122 break; 123 case NETDEV_CHANGENAME: 124 ipoib_delete_debug_files(dev); 125 ipoib_create_debug_files(dev); 126 break; 127 case NETDEV_UNREGISTER: 128 ipoib_delete_debug_files(dev); 129 break; 130 } 131 132 return NOTIFY_DONE; 133 } 134 #endif 135 136 struct ipoib_ifupdown_work { 137 struct work_struct work; 138 struct net_device *dev; 139 netdevice_tracker dev_tracker; 140 bool up; 141 }; 142 143 static void ipoib_ifupdown_task(struct work_struct *work) 144 { 145 struct ipoib_ifupdown_work *pwork = 146 container_of(work, struct ipoib_ifupdown_work, work); 147 struct net_device *dev = pwork->dev; 148 unsigned int flags; 149 150 rtnl_lock(); 151 flags = dev->flags; 152 if (pwork->up) 153 flags |= IFF_UP; 154 else 155 flags &= ~IFF_UP; 156 157 if (dev->flags != flags) 158 dev_change_flags(dev, flags, NULL); 159 rtnl_unlock(); 160 netdev_put(dev, &pwork->dev_tracker); 161 kfree(pwork); 162 } 163 164 static void ipoib_schedule_ifupdown_task(struct net_device *dev, bool up) 165 { 166 struct ipoib_ifupdown_work *work; 167 168 if ((up && (dev->flags & IFF_UP)) || 169 (!up && !(dev->flags & IFF_UP))) 170 return; 171 172 work = kmalloc_obj(*work); 173 if (!work) 174 return; 175 work->dev = dev; 176 netdev_hold(dev, &work->dev_tracker, GFP_KERNEL); 177 work->up = up; 178 INIT_WORK(&work->work, ipoib_ifupdown_task); 179 queue_work(ipoib_workqueue, &work->work); 180 } 181 182 int ipoib_open(struct net_device *dev) 183 { 184 struct ipoib_dev_priv *priv = ipoib_priv(dev); 185 186 ipoib_dbg(priv, "bringing up interface\n"); 187 188 netif_carrier_off(dev); 189 190 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags); 191 192 if (ipoib_ib_dev_open(dev)) { 193 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) 194 return 0; 195 goto err_disable; 196 } 197 198 ipoib_ib_dev_up(dev); 199 200 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) { 201 struct ipoib_dev_priv *cpriv; 202 203 /* Bring up any child interfaces too */ 204 netdev_lock_ops_to_full(dev); 205 list_for_each_entry(cpriv, &priv->child_intfs, list) 206 ipoib_schedule_ifupdown_task(cpriv->dev, true); 207 netdev_unlock_full_to_ops(dev); 208 } else if (priv->parent) { 209 struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent); 210 211 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &ppriv->flags)) 212 ipoib_dbg(priv, "parent device %s is not up, so child device may be not functioning.\n", 213 ppriv->dev->name); 214 } 215 netif_start_queue(dev); 216 217 return 0; 218 219 err_disable: 220 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags); 221 222 return -EINVAL; 223 } 224 225 static int ipoib_stop(struct net_device *dev) 226 { 227 struct ipoib_dev_priv *priv = ipoib_priv(dev); 228 229 ipoib_dbg(priv, "stopping interface\n"); 230 231 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags); 232 233 netif_stop_queue(dev); 234 235 ipoib_ib_dev_down(dev); 236 ipoib_ib_dev_stop(dev); 237 238 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) { 239 struct ipoib_dev_priv *cpriv; 240 241 /* Bring down any child interfaces too */ 242 netdev_lock_ops_to_full(dev); 243 list_for_each_entry(cpriv, &priv->child_intfs, list) 244 ipoib_schedule_ifupdown_task(cpriv->dev, false); 245 netdev_unlock_full_to_ops(dev); 246 } 247 248 return 0; 249 } 250 251 static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features) 252 { 253 struct ipoib_dev_priv *priv = ipoib_priv(dev); 254 255 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags)) 256 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO); 257 258 return features; 259 } 260 261 static int ipoib_change_mtu(struct net_device *dev, int new_mtu) 262 { 263 struct ipoib_dev_priv *priv = ipoib_priv(dev); 264 int ret = 0; 265 266 /* dev->mtu > 2K ==> connected mode */ 267 if (ipoib_cm_admin_enabled(dev)) { 268 if (new_mtu > ipoib_cm_max_mtu(dev)) 269 return -EINVAL; 270 271 if (new_mtu > priv->mcast_mtu) 272 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n", 273 priv->mcast_mtu); 274 275 WRITE_ONCE(dev->mtu, new_mtu); 276 return 0; 277 } 278 279 if (new_mtu < (ETH_MIN_MTU + IPOIB_ENCAP_LEN) || 280 new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu)) 281 return -EINVAL; 282 283 priv->admin_mtu = new_mtu; 284 285 if (priv->mcast_mtu < priv->admin_mtu) 286 ipoib_dbg(priv, "MTU must be smaller than the underlying " 287 "link layer MTU - 4 (%u)\n", priv->mcast_mtu); 288 289 new_mtu = min(priv->mcast_mtu, priv->admin_mtu); 290 291 if (priv->rn_ops->ndo_change_mtu) { 292 bool carrier_status = netif_carrier_ok(dev); 293 294 netif_carrier_off(dev); 295 296 /* notify lower level on the real mtu */ 297 ret = priv->rn_ops->ndo_change_mtu(dev, new_mtu); 298 299 if (carrier_status) 300 netif_carrier_on(dev); 301 } else { 302 WRITE_ONCE(dev->mtu, new_mtu); 303 } 304 305 return ret; 306 } 307 308 static void ipoib_get_stats(struct net_device *dev, 309 struct rtnl_link_stats64 *stats) 310 { 311 struct ipoib_dev_priv *priv = ipoib_priv(dev); 312 313 if (priv->rn_ops->ndo_get_stats64) 314 priv->rn_ops->ndo_get_stats64(dev, stats); 315 else 316 netdev_stats_to_stats64(stats, &dev->stats); 317 } 318 319 /* Called with an RCU read lock taken */ 320 static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr, 321 struct net_device *dev) 322 { 323 struct net *net = dev_net(dev); 324 struct in_device *in_dev; 325 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr; 326 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr; 327 __be32 ret_addr; 328 329 switch (addr->sa_family) { 330 case AF_INET: 331 in_dev = in_dev_get(dev); 332 if (!in_dev) 333 return false; 334 335 ret_addr = inet_confirm_addr(net, in_dev, 0, 336 addr_in->sin_addr.s_addr, 337 RT_SCOPE_HOST); 338 in_dev_put(in_dev); 339 if (ret_addr) 340 return true; 341 342 break; 343 case AF_INET6: 344 if (IS_ENABLED(CONFIG_IPV6) && 345 ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1)) 346 return true; 347 348 break; 349 } 350 return false; 351 } 352 353 /* 354 * Find the L2 master net_device on top of the given net_device. 355 * @dev: base IPoIB net_device 356 * 357 * Returns the L2 master net_device with reference held if the L2 master 358 * exists (such as bond netdevice), or returns same netdev with reference 359 * held when master does not exist or when L3 master (such as VRF netdev). 360 */ 361 static struct net_device *ipoib_get_master_net_dev(struct net_device *dev) 362 { 363 struct net_device *master; 364 365 rcu_read_lock(); 366 367 master = netdev_master_upper_dev_get_rcu(dev); 368 if (!master || netif_is_l3_master(master)) 369 master = dev; 370 371 dev_hold(master); 372 rcu_read_unlock(); 373 374 return master; 375 } 376 377 struct ipoib_walk_data { 378 const struct sockaddr *addr; 379 struct net_device *result; 380 }; 381 382 static int ipoib_upper_walk(struct net_device *upper, 383 struct netdev_nested_priv *priv) 384 { 385 struct ipoib_walk_data *data = (struct ipoib_walk_data *)priv->data; 386 int ret = 0; 387 388 if (ipoib_is_dev_match_addr_rcu(data->addr, upper)) { 389 dev_hold(upper); 390 data->result = upper; 391 ret = 1; 392 } 393 394 return ret; 395 } 396 397 /** 398 * ipoib_get_net_dev_match_addr - Find a net_device matching 399 * the given address, which is an upper device of the given net_device. 400 * 401 * @addr: IP address to look for. 402 * @dev: base IPoIB net_device 403 * 404 * If found, returns the net_device with a reference held. Otherwise return 405 * NULL. 406 */ 407 static struct net_device *ipoib_get_net_dev_match_addr( 408 const struct sockaddr *addr, struct net_device *dev) 409 { 410 struct netdev_nested_priv priv; 411 struct ipoib_walk_data data = { 412 .addr = addr, 413 }; 414 415 priv.data = (void *)&data; 416 rcu_read_lock(); 417 if (ipoib_is_dev_match_addr_rcu(addr, dev)) { 418 dev_hold(dev); 419 data.result = dev; 420 goto out; 421 } 422 423 netdev_walk_all_upper_dev_rcu(dev, ipoib_upper_walk, &priv); 424 out: 425 rcu_read_unlock(); 426 return data.result; 427 } 428 429 /* returns the number of IPoIB netdevs on top a given ipoib device matching a 430 * pkey_index and address, if one exists. 431 * 432 * @found_net_dev: contains a matching net_device if the return value >= 1, 433 * with a reference held. */ 434 static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv, 435 const union ib_gid *gid, 436 u16 pkey_index, 437 const struct sockaddr *addr, 438 int nesting, 439 struct net_device **found_net_dev) 440 { 441 struct ipoib_dev_priv *child_priv; 442 struct net_device *net_dev = NULL; 443 int matches = 0; 444 445 if (priv->pkey_index == pkey_index && 446 (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) { 447 if (!addr) { 448 net_dev = ipoib_get_master_net_dev(priv->dev); 449 } else { 450 /* Verify the net_device matches the IP address, as 451 * IPoIB child devices currently share a GID. */ 452 net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev); 453 } 454 if (net_dev) { 455 if (!*found_net_dev) 456 *found_net_dev = net_dev; 457 else 458 dev_put(net_dev); 459 ++matches; 460 } 461 } 462 463 if (test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) 464 return matches; 465 466 /* Check child interfaces */ 467 netdev_lock(priv->dev); 468 list_for_each_entry(child_priv, &priv->child_intfs, list) { 469 matches += ipoib_match_gid_pkey_addr(child_priv, gid, 470 pkey_index, addr, 471 nesting + 1, 472 found_net_dev); 473 if (matches > 1) 474 break; 475 } 476 netdev_unlock(priv->dev); 477 478 return matches; 479 } 480 481 /* Returns the number of matching net_devs found (between 0 and 2). Also 482 * return the matching net_device in the @net_dev parameter, holding a 483 * reference to the net_device, if the number of matches >= 1 */ 484 static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u32 port, 485 u16 pkey_index, 486 const union ib_gid *gid, 487 const struct sockaddr *addr, 488 struct net_device **net_dev) 489 { 490 struct ipoib_dev_priv *priv; 491 int matches = 0; 492 493 *net_dev = NULL; 494 495 list_for_each_entry(priv, dev_list, list) { 496 if (priv->port != port) 497 continue; 498 499 matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index, 500 addr, 0, net_dev); 501 if (matches > 1) 502 break; 503 } 504 505 return matches; 506 } 507 508 static struct net_device *ipoib_get_net_dev_by_params( 509 struct ib_device *dev, u32 port, u16 pkey, 510 const union ib_gid *gid, const struct sockaddr *addr, 511 void *client_data) 512 { 513 struct net_device *net_dev; 514 struct list_head *dev_list = client_data; 515 u16 pkey_index; 516 int matches; 517 int ret; 518 519 if (!rdma_protocol_ib(dev, port)) 520 return NULL; 521 522 ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index); 523 if (ret) 524 return NULL; 525 526 /* See if we can find a unique device matching the pkey and GID */ 527 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index, 528 gid, NULL, &net_dev); 529 530 switch (matches) { 531 case 0: 532 return NULL; 533 case 1: 534 return net_dev; 535 } 536 537 dev_put(net_dev); 538 539 /* Couldn't find a unique device with pkey and GID only. Use L3 540 * address to uniquely match the net device */ 541 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index, 542 gid, addr, &net_dev); 543 switch (matches) { 544 case 0: 545 return NULL; 546 default: 547 dev_warn_ratelimited(&dev->dev, 548 "duplicate IP address detected\n"); 549 fallthrough; 550 case 1: 551 return net_dev; 552 } 553 } 554 555 int ipoib_set_mode(struct net_device *dev, const char *buf) 556 { 557 struct ipoib_dev_priv *priv = ipoib_priv(dev); 558 559 if ((test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags) && 560 !strcmp(buf, "connected\n")) || 561 (!test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags) && 562 !strcmp(buf, "datagram\n"))) { 563 return 0; 564 } 565 566 /* flush paths if we switch modes so that connections are restarted */ 567 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) { 568 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags); 569 ipoib_warn(priv, "enabling connected mode " 570 "will cause multicast packet drops\n"); 571 netdev_lock_ops(dev); 572 netdev_update_features(dev); 573 netif_set_mtu(dev, ipoib_cm_max_mtu(dev)); 574 netif_set_real_num_tx_queues(dev, 1); 575 netdev_unlock_ops(dev); 576 rtnl_unlock(); 577 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM; 578 579 ipoib_flush_paths(dev); 580 return (!rtnl_trylock()) ? -EBUSY : 0; 581 } 582 583 if (!strcmp(buf, "datagram\n")) { 584 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags); 585 netdev_lock_ops(dev); 586 netdev_update_features(dev); 587 netif_set_mtu(dev, min(priv->mcast_mtu, dev->mtu)); 588 netif_set_real_num_tx_queues(dev, dev->num_tx_queues); 589 netdev_unlock_ops(dev); 590 rtnl_unlock(); 591 ipoib_flush_paths(dev); 592 return (!rtnl_trylock()) ? -EBUSY : 0; 593 } 594 595 return -EINVAL; 596 } 597 598 struct ipoib_path *__path_find(struct net_device *dev, void *gid) 599 { 600 struct ipoib_dev_priv *priv = ipoib_priv(dev); 601 struct rb_node *n = priv->path_tree.rb_node; 602 struct ipoib_path *path; 603 int ret; 604 605 while (n) { 606 path = rb_entry(n, struct ipoib_path, rb_node); 607 608 ret = memcmp(gid, path->pathrec.dgid.raw, 609 sizeof (union ib_gid)); 610 611 if (ret < 0) 612 n = n->rb_left; 613 else if (ret > 0) 614 n = n->rb_right; 615 else 616 return path; 617 } 618 619 return NULL; 620 } 621 622 static int __path_add(struct net_device *dev, struct ipoib_path *path) 623 { 624 struct ipoib_dev_priv *priv = ipoib_priv(dev); 625 struct rb_node **n = &priv->path_tree.rb_node; 626 struct rb_node *pn = NULL; 627 struct ipoib_path *tpath; 628 int ret; 629 630 while (*n) { 631 pn = *n; 632 tpath = rb_entry(pn, struct ipoib_path, rb_node); 633 634 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw, 635 sizeof (union ib_gid)); 636 if (ret < 0) 637 n = &pn->rb_left; 638 else if (ret > 0) 639 n = &pn->rb_right; 640 else 641 return -EEXIST; 642 } 643 644 rb_link_node(&path->rb_node, pn, n); 645 rb_insert_color(&path->rb_node, &priv->path_tree); 646 647 list_add_tail(&path->list, &priv->path_list); 648 649 return 0; 650 } 651 652 static void path_free(struct net_device *dev, struct ipoib_path *path) 653 { 654 struct sk_buff *skb; 655 656 while ((skb = __skb_dequeue(&path->queue))) 657 dev_kfree_skb_irq(skb); 658 659 ipoib_dbg(ipoib_priv(dev), "%s\n", __func__); 660 661 /* remove all neigh connected to this path */ 662 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw); 663 664 if (path->ah) 665 ipoib_put_ah(path->ah); 666 667 kfree(path); 668 } 669 670 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG 671 672 struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev) 673 { 674 struct ipoib_path_iter *iter; 675 676 iter = kmalloc_obj(*iter); 677 if (!iter) 678 return NULL; 679 680 iter->dev = dev; 681 memset(iter->path.pathrec.dgid.raw, 0, 16); 682 683 if (ipoib_path_iter_next(iter)) { 684 kfree(iter); 685 return NULL; 686 } 687 688 return iter; 689 } 690 691 int ipoib_path_iter_next(struct ipoib_path_iter *iter) 692 { 693 struct ipoib_dev_priv *priv = ipoib_priv(iter->dev); 694 struct rb_node *n; 695 struct ipoib_path *path; 696 int ret = 1; 697 698 spin_lock_irq(&priv->lock); 699 700 n = rb_first(&priv->path_tree); 701 702 while (n) { 703 path = rb_entry(n, struct ipoib_path, rb_node); 704 705 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw, 706 sizeof (union ib_gid)) < 0) { 707 iter->path = *path; 708 ret = 0; 709 break; 710 } 711 712 n = rb_next(n); 713 } 714 715 spin_unlock_irq(&priv->lock); 716 717 return ret; 718 } 719 720 void ipoib_path_iter_read(struct ipoib_path_iter *iter, 721 struct ipoib_path *path) 722 { 723 *path = iter->path; 724 } 725 726 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */ 727 728 void ipoib_mark_paths_invalid(struct net_device *dev) 729 { 730 struct ipoib_dev_priv *priv = ipoib_priv(dev); 731 struct ipoib_path *path, *tp; 732 733 spin_lock_irq(&priv->lock); 734 735 list_for_each_entry_safe(path, tp, &priv->path_list, list) { 736 ipoib_dbg(priv, "mark path LID 0x%08x GID %pI6 invalid\n", 737 be32_to_cpu(sa_path_get_dlid(&path->pathrec)), 738 path->pathrec.dgid.raw); 739 if (path->ah) 740 path->ah->valid = 0; 741 } 742 743 spin_unlock_irq(&priv->lock); 744 } 745 746 static void push_pseudo_header(struct sk_buff *skb, const char *daddr) 747 { 748 struct ipoib_pseudo_header *phdr; 749 750 phdr = skb_push(skb, sizeof(*phdr)); 751 memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN); 752 } 753 754 void ipoib_flush_paths(struct net_device *dev) 755 { 756 struct ipoib_dev_priv *priv = ipoib_priv(dev); 757 struct ipoib_path *path, *tp; 758 LIST_HEAD(remove_list); 759 unsigned long flags; 760 761 netif_tx_lock_bh(dev); 762 spin_lock_irqsave(&priv->lock, flags); 763 764 list_splice_init(&priv->path_list, &remove_list); 765 766 list_for_each_entry(path, &remove_list, list) 767 rb_erase(&path->rb_node, &priv->path_tree); 768 769 list_for_each_entry_safe(path, tp, &remove_list, list) { 770 if (path->query) 771 ib_sa_cancel_query(path->query_id, path->query); 772 spin_unlock_irqrestore(&priv->lock, flags); 773 netif_tx_unlock_bh(dev); 774 wait_for_completion(&path->done); 775 path_free(dev, path); 776 netif_tx_lock_bh(dev); 777 spin_lock_irqsave(&priv->lock, flags); 778 } 779 780 spin_unlock_irqrestore(&priv->lock, flags); 781 netif_tx_unlock_bh(dev); 782 } 783 784 static void path_rec_completion(int status, 785 struct sa_path_rec *pathrec, 786 unsigned int num_prs, void *path_ptr) 787 { 788 struct ipoib_path *path = path_ptr; 789 struct net_device *dev = path->dev; 790 struct ipoib_dev_priv *priv = ipoib_priv(dev); 791 struct ipoib_ah *ah = NULL; 792 struct ipoib_ah *old_ah = NULL; 793 struct ipoib_neigh *neigh, *tn; 794 struct sk_buff_head skqueue; 795 struct sk_buff *skb; 796 unsigned long flags; 797 798 if (!status) 799 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n", 800 be32_to_cpu(sa_path_get_dlid(pathrec)), 801 pathrec->dgid.raw); 802 else 803 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n", 804 status, path->pathrec.dgid.raw); 805 806 skb_queue_head_init(&skqueue); 807 808 if (!status) { 809 struct rdma_ah_attr av; 810 811 if (!ib_init_ah_attr_from_path(priv->ca, priv->port, 812 pathrec, &av, NULL)) { 813 ah = ipoib_create_ah(dev, priv->pd, &av); 814 rdma_destroy_ah_attr(&av); 815 } 816 } 817 818 spin_lock_irqsave(&priv->lock, flags); 819 820 if (!IS_ERR_OR_NULL(ah)) { 821 /* 822 * pathrec.dgid is used as the database key from the LLADDR, 823 * it must remain unchanged even if the SA returns a different 824 * GID to use in the AH. 825 */ 826 if (memcmp(pathrec->dgid.raw, path->pathrec.dgid.raw, 827 sizeof(union ib_gid))) { 828 ipoib_dbg( 829 priv, 830 "%s got PathRec for gid %pI6 while asked for %pI6\n", 831 dev->name, pathrec->dgid.raw, 832 path->pathrec.dgid.raw); 833 memcpy(pathrec->dgid.raw, path->pathrec.dgid.raw, 834 sizeof(union ib_gid)); 835 } 836 837 path->pathrec = *pathrec; 838 839 old_ah = path->ah; 840 path->ah = ah; 841 842 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n", 843 ah, be32_to_cpu(sa_path_get_dlid(pathrec)), 844 pathrec->sl); 845 846 while ((skb = __skb_dequeue(&path->queue))) 847 __skb_queue_tail(&skqueue, skb); 848 849 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) { 850 if (neigh->ah) { 851 WARN_ON(neigh->ah != old_ah); 852 /* 853 * Dropping the ah reference inside 854 * priv->lock is safe here, because we 855 * will hold one more reference from 856 * the original value of path->ah (ie 857 * old_ah). 858 */ 859 ipoib_put_ah(neigh->ah); 860 } 861 kref_get(&path->ah->ref); 862 neigh->ah = path->ah; 863 864 if (ipoib_cm_enabled(dev, neigh->daddr)) { 865 if (!ipoib_cm_get(neigh)) 866 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, 867 path, 868 neigh)); 869 if (!ipoib_cm_get(neigh)) { 870 ipoib_neigh_free(neigh); 871 continue; 872 } 873 } 874 875 while ((skb = __skb_dequeue(&neigh->queue))) 876 __skb_queue_tail(&skqueue, skb); 877 } 878 path->ah->valid = 1; 879 } 880 881 path->query = NULL; 882 complete(&path->done); 883 884 spin_unlock_irqrestore(&priv->lock, flags); 885 886 if (IS_ERR_OR_NULL(ah)) 887 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw); 888 889 if (old_ah) 890 ipoib_put_ah(old_ah); 891 892 while ((skb = __skb_dequeue(&skqueue))) { 893 int ret; 894 skb->dev = dev; 895 ret = dev_queue_xmit(skb); 896 if (ret) 897 ipoib_warn(priv, "%s: dev_queue_xmit failed to re-queue packet, ret:%d\n", 898 __func__, ret); 899 } 900 } 901 902 static void init_path_rec(struct ipoib_dev_priv *priv, struct ipoib_path *path, 903 void *gid) 904 { 905 path->dev = priv->dev; 906 907 if (rdma_cap_opa_ah(priv->ca, priv->port)) 908 path->pathrec.rec_type = SA_PATH_REC_TYPE_OPA; 909 else 910 path->pathrec.rec_type = SA_PATH_REC_TYPE_IB; 911 912 memcpy(path->pathrec.dgid.raw, gid, sizeof(union ib_gid)); 913 path->pathrec.sgid = priv->local_gid; 914 path->pathrec.pkey = cpu_to_be16(priv->pkey); 915 path->pathrec.numb_path = 1; 916 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class; 917 } 918 919 static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid) 920 { 921 struct ipoib_dev_priv *priv = ipoib_priv(dev); 922 struct ipoib_path *path; 923 924 if (!priv->broadcast) 925 return NULL; 926 927 path = kzalloc_obj(*path, GFP_ATOMIC); 928 if (!path) 929 return NULL; 930 931 skb_queue_head_init(&path->queue); 932 933 INIT_LIST_HEAD(&path->neigh_list); 934 935 init_path_rec(priv, path, gid); 936 937 return path; 938 } 939 940 static int path_rec_start(struct net_device *dev, 941 struct ipoib_path *path) 942 { 943 struct ipoib_dev_priv *priv = ipoib_priv(dev); 944 945 ipoib_dbg(priv, "Start path record lookup for %pI6\n", 946 path->pathrec.dgid.raw); 947 948 init_completion(&path->done); 949 950 path->query_id = 951 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port, 952 &path->pathrec, 953 IB_SA_PATH_REC_DGID | 954 IB_SA_PATH_REC_SGID | 955 IB_SA_PATH_REC_NUMB_PATH | 956 IB_SA_PATH_REC_TRAFFIC_CLASS | 957 IB_SA_PATH_REC_PKEY, 958 1000, GFP_ATOMIC, 959 path_rec_completion, 960 path, &path->query); 961 if (path->query_id < 0) { 962 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id); 963 path->query = NULL; 964 complete(&path->done); 965 return path->query_id; 966 } 967 968 return 0; 969 } 970 971 static void neigh_refresh_path(struct ipoib_neigh *neigh, u8 *daddr, 972 struct net_device *dev) 973 { 974 struct ipoib_dev_priv *priv = ipoib_priv(dev); 975 struct ipoib_path *path; 976 unsigned long flags; 977 978 spin_lock_irqsave(&priv->lock, flags); 979 980 path = __path_find(dev, daddr + 4); 981 if (!path) 982 goto out; 983 if (!path->query) 984 path_rec_start(dev, path); 985 out: 986 spin_unlock_irqrestore(&priv->lock, flags); 987 } 988 989 static struct ipoib_neigh *neigh_add_path(struct sk_buff *skb, u8 *daddr, 990 struct net_device *dev) 991 { 992 struct ipoib_dev_priv *priv = ipoib_priv(dev); 993 struct rdma_netdev *rn = netdev_priv(dev); 994 struct ipoib_path *path; 995 struct ipoib_neigh *neigh; 996 unsigned long flags; 997 998 spin_lock_irqsave(&priv->lock, flags); 999 neigh = ipoib_neigh_alloc(daddr, dev); 1000 if (!neigh) { 1001 spin_unlock_irqrestore(&priv->lock, flags); 1002 ++dev->stats.tx_dropped; 1003 dev_kfree_skb_any(skb); 1004 return NULL; 1005 } 1006 1007 /* To avoid race condition, make sure that the 1008 * neigh will be added only once. 1009 */ 1010 if (unlikely(!list_empty(&neigh->list))) { 1011 spin_unlock_irqrestore(&priv->lock, flags); 1012 return neigh; 1013 } 1014 1015 path = __path_find(dev, daddr + 4); 1016 if (!path) { 1017 path = path_rec_create(dev, daddr + 4); 1018 if (!path) 1019 goto err_path; 1020 1021 __path_add(dev, path); 1022 } 1023 1024 list_add_tail(&neigh->list, &path->neigh_list); 1025 1026 if (path->ah && path->ah->valid) { 1027 kref_get(&path->ah->ref); 1028 neigh->ah = path->ah; 1029 1030 if (ipoib_cm_enabled(dev, neigh->daddr)) { 1031 if (!ipoib_cm_get(neigh)) 1032 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh)); 1033 if (!ipoib_cm_get(neigh)) { 1034 ipoib_neigh_free(neigh); 1035 goto err_drop; 1036 } 1037 if (skb_queue_len(&neigh->queue) < 1038 IPOIB_MAX_PATH_REC_QUEUE) { 1039 push_pseudo_header(skb, neigh->daddr); 1040 __skb_queue_tail(&neigh->queue, skb); 1041 } else { 1042 ipoib_warn(priv, "queue length limit %d. Packet drop.\n", 1043 skb_queue_len(&neigh->queue)); 1044 goto err_drop; 1045 } 1046 } else { 1047 spin_unlock_irqrestore(&priv->lock, flags); 1048 path->ah->last_send = rn->send(dev, skb, path->ah->ah, 1049 IPOIB_QPN(daddr)); 1050 ipoib_neigh_put(neigh); 1051 return NULL; 1052 } 1053 } else { 1054 neigh->ah = NULL; 1055 1056 if (!path->query && path_rec_start(dev, path)) 1057 goto err_path; 1058 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) { 1059 push_pseudo_header(skb, neigh->daddr); 1060 __skb_queue_tail(&neigh->queue, skb); 1061 } else { 1062 goto err_drop; 1063 } 1064 } 1065 1066 spin_unlock_irqrestore(&priv->lock, flags); 1067 ipoib_neigh_put(neigh); 1068 return NULL; 1069 1070 err_path: 1071 ipoib_neigh_free(neigh); 1072 err_drop: 1073 ++dev->stats.tx_dropped; 1074 dev_kfree_skb_any(skb); 1075 1076 spin_unlock_irqrestore(&priv->lock, flags); 1077 ipoib_neigh_put(neigh); 1078 1079 return NULL; 1080 } 1081 1082 static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev, 1083 struct ipoib_pseudo_header *phdr) 1084 { 1085 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1086 struct rdma_netdev *rn = netdev_priv(dev); 1087 struct ipoib_path *path; 1088 unsigned long flags; 1089 1090 spin_lock_irqsave(&priv->lock, flags); 1091 1092 /* no broadcast means that all paths are (going to be) not valid */ 1093 if (!priv->broadcast) 1094 goto drop_and_unlock; 1095 1096 path = __path_find(dev, phdr->hwaddr + 4); 1097 if (!path || !path->ah || !path->ah->valid) { 1098 if (!path) { 1099 path = path_rec_create(dev, phdr->hwaddr + 4); 1100 if (!path) 1101 goto drop_and_unlock; 1102 __path_add(dev, path); 1103 } else { 1104 /* 1105 * make sure there are no changes in the existing 1106 * path record 1107 */ 1108 init_path_rec(priv, path, phdr->hwaddr + 4); 1109 } 1110 if (!path->query && path_rec_start(dev, path)) { 1111 goto drop_and_unlock; 1112 } 1113 1114 if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) { 1115 push_pseudo_header(skb, phdr->hwaddr); 1116 __skb_queue_tail(&path->queue, skb); 1117 goto unlock; 1118 } else { 1119 goto drop_and_unlock; 1120 } 1121 } 1122 1123 spin_unlock_irqrestore(&priv->lock, flags); 1124 ipoib_dbg(priv, "Send unicast ARP to %08x\n", 1125 be32_to_cpu(sa_path_get_dlid(&path->pathrec))); 1126 path->ah->last_send = rn->send(dev, skb, path->ah->ah, 1127 IPOIB_QPN(phdr->hwaddr)); 1128 return; 1129 1130 drop_and_unlock: 1131 ++dev->stats.tx_dropped; 1132 dev_kfree_skb_any(skb); 1133 unlock: 1134 spin_unlock_irqrestore(&priv->lock, flags); 1135 } 1136 1137 static netdev_tx_t ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev) 1138 { 1139 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1140 struct rdma_netdev *rn = netdev_priv(dev); 1141 struct ipoib_neigh *neigh; 1142 struct ipoib_pseudo_header *phdr; 1143 struct ipoib_header *header; 1144 unsigned long flags; 1145 1146 phdr = (struct ipoib_pseudo_header *) skb->data; 1147 skb_pull(skb, sizeof(*phdr)); 1148 header = (struct ipoib_header *) skb->data; 1149 1150 if (unlikely(phdr->hwaddr[4] == 0xff)) { 1151 /* multicast, arrange "if" according to probability */ 1152 if ((header->proto != htons(ETH_P_IP)) && 1153 (header->proto != htons(ETH_P_IPV6)) && 1154 (header->proto != htons(ETH_P_ARP)) && 1155 (header->proto != htons(ETH_P_RARP)) && 1156 (header->proto != htons(ETH_P_TIPC))) { 1157 /* ethertype not supported by IPoIB */ 1158 ++dev->stats.tx_dropped; 1159 dev_kfree_skb_any(skb); 1160 return NETDEV_TX_OK; 1161 } 1162 /* Add in the P_Key for multicast*/ 1163 phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff; 1164 phdr->hwaddr[9] = priv->pkey & 0xff; 1165 1166 neigh = ipoib_neigh_get(dev, phdr->hwaddr); 1167 if (likely(neigh)) 1168 goto send_using_neigh; 1169 ipoib_mcast_send(dev, phdr->hwaddr, skb); 1170 return NETDEV_TX_OK; 1171 } 1172 1173 /* unicast, arrange "switch" according to probability */ 1174 switch (header->proto) { 1175 case htons(ETH_P_IP): 1176 case htons(ETH_P_IPV6): 1177 case htons(ETH_P_TIPC): 1178 neigh = ipoib_neigh_get(dev, phdr->hwaddr); 1179 if (unlikely(!neigh)) { 1180 neigh = neigh_add_path(skb, phdr->hwaddr, dev); 1181 if (likely(!neigh)) 1182 return NETDEV_TX_OK; 1183 } 1184 break; 1185 case htons(ETH_P_ARP): 1186 case htons(ETH_P_RARP): 1187 /* for unicast ARP and RARP should always perform path find */ 1188 unicast_arp_send(skb, dev, phdr); 1189 return NETDEV_TX_OK; 1190 default: 1191 /* ethertype not supported by IPoIB */ 1192 ++dev->stats.tx_dropped; 1193 dev_kfree_skb_any(skb); 1194 return NETDEV_TX_OK; 1195 } 1196 1197 send_using_neigh: 1198 /* note we now hold a ref to neigh */ 1199 if (ipoib_cm_get(neigh)) { 1200 if (ipoib_cm_up(neigh)) { 1201 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh)); 1202 goto unref; 1203 } 1204 } else if (neigh->ah && neigh->ah->valid) { 1205 neigh->ah->last_send = rn->send(dev, skb, neigh->ah->ah, 1206 IPOIB_QPN(phdr->hwaddr)); 1207 goto unref; 1208 } else if (neigh->ah) { 1209 neigh_refresh_path(neigh, phdr->hwaddr, dev); 1210 } 1211 1212 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) { 1213 push_pseudo_header(skb, phdr->hwaddr); 1214 spin_lock_irqsave(&priv->lock, flags); 1215 __skb_queue_tail(&neigh->queue, skb); 1216 spin_unlock_irqrestore(&priv->lock, flags); 1217 } else { 1218 ++dev->stats.tx_dropped; 1219 dev_kfree_skb_any(skb); 1220 } 1221 1222 unref: 1223 ipoib_neigh_put(neigh); 1224 1225 return NETDEV_TX_OK; 1226 } 1227 1228 static void ipoib_timeout(struct net_device *dev, unsigned int txqueue) 1229 { 1230 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1231 struct rdma_netdev *rn = netdev_priv(dev); 1232 1233 if (rn->tx_timeout) { 1234 rn->tx_timeout(dev, txqueue); 1235 return; 1236 } 1237 ipoib_warn(priv, "transmit timeout: latency %d msecs\n", 1238 jiffies_to_msecs(jiffies - dev_trans_start(dev))); 1239 ipoib_warn(priv, 1240 "queue stopped %d, tx_head %u, tx_tail %u, global_tx_head %u, global_tx_tail %u\n", 1241 netif_queue_stopped(dev), priv->tx_head, priv->tx_tail, 1242 priv->global_tx_head, priv->global_tx_tail); 1243 1244 1245 schedule_work(&priv->tx_timeout_work); 1246 } 1247 1248 void ipoib_ib_tx_timeout_work(struct work_struct *work) 1249 { 1250 struct ipoib_dev_priv *priv = container_of(work, 1251 struct ipoib_dev_priv, 1252 tx_timeout_work); 1253 int err; 1254 1255 rtnl_lock(); 1256 netdev_lock_ops(priv->dev); 1257 1258 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags)) 1259 goto unlock; 1260 1261 ipoib_stop(priv->dev); 1262 err = ipoib_open(priv->dev); 1263 if (err) { 1264 ipoib_warn(priv, "ipoib_open failed recovering from a tx_timeout, err(%d).\n", 1265 err); 1266 goto unlock; 1267 } 1268 1269 netif_tx_wake_all_queues(priv->dev); 1270 unlock: 1271 netdev_unlock_ops(priv->dev); 1272 rtnl_unlock(); 1273 1274 } 1275 1276 static int ipoib_hard_header(struct sk_buff *skb, 1277 struct net_device *dev, 1278 unsigned short type, 1279 const void *daddr, 1280 const void *saddr, 1281 unsigned int len) 1282 { 1283 struct ipoib_header *header; 1284 1285 header = skb_push(skb, sizeof(*header)); 1286 1287 header->proto = htons(type); 1288 header->reserved = 0; 1289 1290 /* 1291 * we don't rely on dst_entry structure, always stuff the 1292 * destination address into skb hard header so we can figure out where 1293 * to send the packet later. 1294 */ 1295 push_pseudo_header(skb, daddr); 1296 1297 return IPOIB_HARD_LEN; 1298 } 1299 1300 static void ipoib_set_mcast_list(struct net_device *dev) 1301 { 1302 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1303 1304 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) { 1305 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set"); 1306 return; 1307 } 1308 1309 queue_work(priv->wq, &priv->restart_task); 1310 } 1311 1312 static int ipoib_get_iflink(const struct net_device *dev) 1313 { 1314 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1315 1316 /* parent interface */ 1317 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) 1318 return READ_ONCE(dev->ifindex); 1319 1320 /* child/vlan interface */ 1321 return READ_ONCE(priv->parent->ifindex); 1322 } 1323 1324 static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr) 1325 { 1326 /* 1327 * Use only the address parts that contributes to spreading 1328 * The subnet prefix is not used as one can not connect to 1329 * same remote port (GUID) using the same remote QPN via two 1330 * different subnets. 1331 */ 1332 /* qpn octets[1:4) & port GUID octets[12:20) */ 1333 u32 *d32 = (u32 *) daddr; 1334 u32 hv; 1335 1336 hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0); 1337 return hv & htbl->mask; 1338 } 1339 1340 struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr) 1341 { 1342 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1343 struct ipoib_neigh_table *ntbl = &priv->ntbl; 1344 struct ipoib_neigh_hash *htbl; 1345 struct ipoib_neigh *neigh = NULL; 1346 u32 hash_val; 1347 1348 rcu_read_lock_bh(); 1349 1350 htbl = rcu_dereference_bh(ntbl->htbl); 1351 1352 if (!htbl) 1353 goto out_unlock; 1354 1355 hash_val = ipoib_addr_hash(htbl, daddr); 1356 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]); 1357 neigh != NULL; 1358 neigh = rcu_dereference_bh(neigh->hnext)) { 1359 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) { 1360 /* found, take one ref on behalf of the caller */ 1361 if (!refcount_inc_not_zero(&neigh->refcnt)) { 1362 /* deleted */ 1363 neigh = NULL; 1364 goto out_unlock; 1365 } 1366 1367 if (likely(skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)) 1368 neigh->alive = jiffies; 1369 goto out_unlock; 1370 } 1371 } 1372 1373 out_unlock: 1374 rcu_read_unlock_bh(); 1375 return neigh; 1376 } 1377 1378 static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv) 1379 { 1380 struct ipoib_neigh_table *ntbl = &priv->ntbl; 1381 struct ipoib_neigh_hash *htbl; 1382 unsigned long neigh_obsolete; 1383 unsigned long dt; 1384 unsigned long flags; 1385 int i; 1386 LIST_HEAD(remove_list); 1387 1388 spin_lock_irqsave(&priv->lock, flags); 1389 1390 htbl = rcu_dereference_protected(ntbl->htbl, 1391 lockdep_is_held(&priv->lock)); 1392 1393 if (!htbl) 1394 goto out_unlock; 1395 1396 /* neigh is obsolete if it was idle for two GC periods */ 1397 dt = 2 * arp_tbl.gc_interval; 1398 neigh_obsolete = jiffies - dt; 1399 1400 for (i = 0; i < htbl->size; i++) { 1401 struct ipoib_neigh *neigh; 1402 struct ipoib_neigh __rcu **np = &htbl->buckets[i]; 1403 1404 while ((neigh = rcu_dereference_protected(*np, 1405 lockdep_is_held(&priv->lock))) != NULL) { 1406 /* was the neigh idle for two GC periods */ 1407 if (time_after(neigh_obsolete, neigh->alive)) { 1408 1409 ipoib_check_and_add_mcast_sendonly(priv, neigh->daddr + 4, &remove_list); 1410 1411 rcu_assign_pointer(*np, 1412 rcu_dereference_protected(neigh->hnext, 1413 lockdep_is_held(&priv->lock))); 1414 /* remove from path/mc list */ 1415 list_del_init(&neigh->list); 1416 call_rcu(&neigh->rcu, ipoib_neigh_reclaim); 1417 } else { 1418 np = &neigh->hnext; 1419 } 1420 1421 } 1422 } 1423 1424 out_unlock: 1425 spin_unlock_irqrestore(&priv->lock, flags); 1426 ipoib_mcast_remove_list(&remove_list); 1427 } 1428 1429 static void ipoib_reap_neigh(struct work_struct *work) 1430 { 1431 struct ipoib_dev_priv *priv = 1432 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work); 1433 1434 __ipoib_reap_neigh(priv); 1435 1436 queue_delayed_work(priv->wq, &priv->neigh_reap_task, 1437 arp_tbl.gc_interval); 1438 } 1439 1440 1441 static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr, 1442 struct net_device *dev) 1443 { 1444 struct ipoib_neigh *neigh; 1445 1446 neigh = kzalloc_obj(*neigh, GFP_ATOMIC); 1447 if (!neigh) 1448 return NULL; 1449 1450 neigh->dev = dev; 1451 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr)); 1452 skb_queue_head_init(&neigh->queue); 1453 INIT_LIST_HEAD(&neigh->list); 1454 ipoib_cm_set(neigh, NULL); 1455 /* one ref on behalf of the caller */ 1456 refcount_set(&neigh->refcnt, 1); 1457 1458 return neigh; 1459 } 1460 1461 struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr, 1462 struct net_device *dev) 1463 { 1464 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1465 struct ipoib_neigh_table *ntbl = &priv->ntbl; 1466 struct ipoib_neigh_hash *htbl; 1467 struct ipoib_neigh *neigh; 1468 u32 hash_val; 1469 1470 htbl = rcu_dereference_protected(ntbl->htbl, 1471 lockdep_is_held(&priv->lock)); 1472 if (!htbl) { 1473 neigh = NULL; 1474 goto out_unlock; 1475 } 1476 1477 /* need to add a new neigh, but maybe some other thread succeeded? 1478 * recalc hash, maybe hash resize took place so we do a search 1479 */ 1480 hash_val = ipoib_addr_hash(htbl, daddr); 1481 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val], 1482 lockdep_is_held(&priv->lock)); 1483 neigh != NULL; 1484 neigh = rcu_dereference_protected(neigh->hnext, 1485 lockdep_is_held(&priv->lock))) { 1486 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) { 1487 /* found, take one ref on behalf of the caller */ 1488 if (!refcount_inc_not_zero(&neigh->refcnt)) { 1489 /* deleted */ 1490 neigh = NULL; 1491 break; 1492 } 1493 neigh->alive = jiffies; 1494 goto out_unlock; 1495 } 1496 } 1497 1498 neigh = ipoib_neigh_ctor(daddr, dev); 1499 if (!neigh) 1500 goto out_unlock; 1501 1502 /* one ref on behalf of the hash table */ 1503 refcount_inc(&neigh->refcnt); 1504 neigh->alive = jiffies; 1505 /* put in hash */ 1506 rcu_assign_pointer(neigh->hnext, 1507 rcu_dereference_protected(htbl->buckets[hash_val], 1508 lockdep_is_held(&priv->lock))); 1509 rcu_assign_pointer(htbl->buckets[hash_val], neigh); 1510 atomic_inc(&ntbl->entries); 1511 1512 out_unlock: 1513 1514 return neigh; 1515 } 1516 1517 void ipoib_neigh_dtor(struct ipoib_neigh *neigh) 1518 { 1519 /* neigh reference count was dropprd to zero */ 1520 struct net_device *dev = neigh->dev; 1521 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1522 struct sk_buff *skb; 1523 if (neigh->ah) 1524 ipoib_put_ah(neigh->ah); 1525 while ((skb = __skb_dequeue(&neigh->queue))) { 1526 ++dev->stats.tx_dropped; 1527 dev_kfree_skb_any(skb); 1528 } 1529 if (ipoib_cm_get(neigh)) 1530 ipoib_cm_destroy_tx(ipoib_cm_get(neigh)); 1531 ipoib_dbg(ipoib_priv(dev), 1532 "neigh free for %06x %pI6\n", 1533 IPOIB_QPN(neigh->daddr), 1534 neigh->daddr + 4); 1535 kfree(neigh); 1536 if (atomic_dec_and_test(&priv->ntbl.entries)) { 1537 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags)) 1538 complete(&priv->ntbl.flushed); 1539 } 1540 } 1541 1542 static void ipoib_neigh_reclaim(struct rcu_head *rp) 1543 { 1544 /* Called as a result of removal from hash table */ 1545 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu); 1546 /* note TX context may hold another ref */ 1547 ipoib_neigh_put(neigh); 1548 } 1549 1550 void ipoib_neigh_free(struct ipoib_neigh *neigh) 1551 { 1552 struct net_device *dev = neigh->dev; 1553 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1554 struct ipoib_neigh_table *ntbl = &priv->ntbl; 1555 struct ipoib_neigh_hash *htbl; 1556 struct ipoib_neigh __rcu **np; 1557 struct ipoib_neigh *n; 1558 u32 hash_val; 1559 1560 htbl = rcu_dereference_protected(ntbl->htbl, 1561 lockdep_is_held(&priv->lock)); 1562 if (!htbl) 1563 return; 1564 1565 hash_val = ipoib_addr_hash(htbl, neigh->daddr); 1566 np = &htbl->buckets[hash_val]; 1567 for (n = rcu_dereference_protected(*np, 1568 lockdep_is_held(&priv->lock)); 1569 n != NULL; 1570 n = rcu_dereference_protected(*np, 1571 lockdep_is_held(&priv->lock))) { 1572 if (n == neigh) { 1573 /* found */ 1574 rcu_assign_pointer(*np, 1575 rcu_dereference_protected(neigh->hnext, 1576 lockdep_is_held(&priv->lock))); 1577 /* remove from parent list */ 1578 list_del_init(&neigh->list); 1579 call_rcu(&neigh->rcu, ipoib_neigh_reclaim); 1580 return; 1581 } else { 1582 np = &n->hnext; 1583 } 1584 } 1585 } 1586 1587 static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv) 1588 { 1589 struct ipoib_neigh_table *ntbl = &priv->ntbl; 1590 struct ipoib_neigh_hash *htbl; 1591 struct ipoib_neigh __rcu **buckets; 1592 u32 size; 1593 1594 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags); 1595 ntbl->htbl = NULL; 1596 htbl = kzalloc_obj(*htbl); 1597 if (!htbl) 1598 return -ENOMEM; 1599 size = roundup_pow_of_two(arp_tbl.gc_thresh3); 1600 buckets = kvzalloc_objs(*buckets, size); 1601 if (!buckets) { 1602 kfree(htbl); 1603 return -ENOMEM; 1604 } 1605 htbl->size = size; 1606 htbl->mask = (size - 1); 1607 htbl->buckets = buckets; 1608 RCU_INIT_POINTER(ntbl->htbl, htbl); 1609 htbl->ntbl = ntbl; 1610 atomic_set(&ntbl->entries, 0); 1611 1612 /* start garbage collection */ 1613 queue_delayed_work(priv->wq, &priv->neigh_reap_task, 1614 arp_tbl.gc_interval); 1615 1616 return 0; 1617 } 1618 1619 static void neigh_hash_free_rcu(struct rcu_head *head) 1620 { 1621 struct ipoib_neigh_hash *htbl = container_of(head, 1622 struct ipoib_neigh_hash, 1623 rcu); 1624 struct ipoib_neigh __rcu **buckets = htbl->buckets; 1625 struct ipoib_neigh_table *ntbl = htbl->ntbl; 1626 1627 kvfree(buckets); 1628 kfree(htbl); 1629 complete(&ntbl->deleted); 1630 } 1631 1632 void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid) 1633 { 1634 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1635 struct ipoib_neigh_table *ntbl = &priv->ntbl; 1636 struct ipoib_neigh_hash *htbl; 1637 unsigned long flags; 1638 int i; 1639 1640 /* remove all neigh connected to a given path or mcast */ 1641 spin_lock_irqsave(&priv->lock, flags); 1642 1643 htbl = rcu_dereference_protected(ntbl->htbl, 1644 lockdep_is_held(&priv->lock)); 1645 1646 if (!htbl) 1647 goto out_unlock; 1648 1649 for (i = 0; i < htbl->size; i++) { 1650 struct ipoib_neigh *neigh; 1651 struct ipoib_neigh __rcu **np = &htbl->buckets[i]; 1652 1653 while ((neigh = rcu_dereference_protected(*np, 1654 lockdep_is_held(&priv->lock))) != NULL) { 1655 /* delete neighs belong to this parent */ 1656 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) { 1657 rcu_assign_pointer(*np, 1658 rcu_dereference_protected(neigh->hnext, 1659 lockdep_is_held(&priv->lock))); 1660 /* remove from parent list */ 1661 list_del_init(&neigh->list); 1662 call_rcu(&neigh->rcu, ipoib_neigh_reclaim); 1663 } else { 1664 np = &neigh->hnext; 1665 } 1666 1667 } 1668 } 1669 out_unlock: 1670 spin_unlock_irqrestore(&priv->lock, flags); 1671 } 1672 1673 static void ipoib_flush_neighs(struct ipoib_dev_priv *priv) 1674 { 1675 struct ipoib_neigh_table *ntbl = &priv->ntbl; 1676 struct ipoib_neigh_hash *htbl; 1677 unsigned long flags; 1678 int i, wait_flushed = 0; 1679 1680 init_completion(&priv->ntbl.flushed); 1681 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags); 1682 1683 spin_lock_irqsave(&priv->lock, flags); 1684 1685 htbl = rcu_dereference_protected(ntbl->htbl, 1686 lockdep_is_held(&priv->lock)); 1687 if (!htbl) 1688 goto out_unlock; 1689 1690 wait_flushed = atomic_read(&priv->ntbl.entries); 1691 if (!wait_flushed) 1692 goto free_htbl; 1693 1694 for (i = 0; i < htbl->size; i++) { 1695 struct ipoib_neigh *neigh; 1696 struct ipoib_neigh __rcu **np = &htbl->buckets[i]; 1697 1698 while ((neigh = rcu_dereference_protected(*np, 1699 lockdep_is_held(&priv->lock))) != NULL) { 1700 rcu_assign_pointer(*np, 1701 rcu_dereference_protected(neigh->hnext, 1702 lockdep_is_held(&priv->lock))); 1703 /* remove from path/mc list */ 1704 list_del_init(&neigh->list); 1705 call_rcu(&neigh->rcu, ipoib_neigh_reclaim); 1706 } 1707 } 1708 1709 free_htbl: 1710 rcu_assign_pointer(ntbl->htbl, NULL); 1711 call_rcu(&htbl->rcu, neigh_hash_free_rcu); 1712 1713 out_unlock: 1714 spin_unlock_irqrestore(&priv->lock, flags); 1715 if (wait_flushed) 1716 wait_for_completion(&priv->ntbl.flushed); 1717 } 1718 1719 static void ipoib_neigh_hash_uninit(struct net_device *dev) 1720 { 1721 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1722 1723 ipoib_dbg(priv, "%s\n", __func__); 1724 init_completion(&priv->ntbl.deleted); 1725 1726 cancel_delayed_work_sync(&priv->neigh_reap_task); 1727 1728 ipoib_flush_neighs(priv); 1729 1730 wait_for_completion(&priv->ntbl.deleted); 1731 } 1732 1733 static void ipoib_napi_add(struct net_device *dev) 1734 { 1735 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1736 1737 netif_napi_add_weight(dev, &priv->recv_napi, ipoib_rx_poll, 1738 IPOIB_NUM_WC); 1739 netif_napi_add_weight(dev, &priv->send_napi, ipoib_tx_poll, 1740 MAX_SEND_CQE); 1741 } 1742 1743 static void ipoib_napi_del(struct net_device *dev) 1744 { 1745 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1746 1747 netif_napi_del(&priv->recv_napi); 1748 netif_napi_del(&priv->send_napi); 1749 } 1750 1751 static void ipoib_dev_uninit_default(struct net_device *dev) 1752 { 1753 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1754 1755 ipoib_transport_dev_cleanup(dev); 1756 1757 ipoib_napi_del(dev); 1758 1759 ipoib_cm_dev_cleanup(dev); 1760 1761 kfree(priv->rx_ring); 1762 vfree(priv->tx_ring); 1763 1764 priv->rx_ring = NULL; 1765 priv->tx_ring = NULL; 1766 } 1767 1768 static int ipoib_dev_init_default(struct net_device *dev) 1769 { 1770 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1771 u8 addr_mod[3]; 1772 1773 ipoib_napi_add(dev); 1774 1775 /* Allocate RX/TX "rings" to hold queued skbs */ 1776 priv->rx_ring = kzalloc_objs(*priv->rx_ring, ipoib_recvq_size); 1777 if (!priv->rx_ring) 1778 goto out; 1779 1780 priv->tx_ring = vzalloc(array_size(ipoib_sendq_size, 1781 sizeof(*priv->tx_ring))); 1782 if (!priv->tx_ring) { 1783 pr_warn("%s: failed to allocate TX ring (%d entries)\n", 1784 priv->ca->name, ipoib_sendq_size); 1785 goto out_rx_ring_cleanup; 1786 } 1787 1788 /* priv->tx_head, tx_tail and global_tx_tail/head are already 0 */ 1789 1790 if (ipoib_transport_dev_init(dev, priv->ca)) { 1791 pr_warn("%s: ipoib_transport_dev_init failed\n", 1792 priv->ca->name); 1793 goto out_tx_ring_cleanup; 1794 } 1795 1796 /* after qp created set dev address */ 1797 addr_mod[0] = (priv->qp->qp_num >> 16) & 0xff; 1798 addr_mod[1] = (priv->qp->qp_num >> 8) & 0xff; 1799 addr_mod[2] = (priv->qp->qp_num) & 0xff; 1800 dev_addr_mod(priv->dev, 1, addr_mod, sizeof(addr_mod)); 1801 1802 return 0; 1803 1804 out_tx_ring_cleanup: 1805 vfree(priv->tx_ring); 1806 1807 out_rx_ring_cleanup: 1808 kfree(priv->rx_ring); 1809 1810 out: 1811 ipoib_napi_del(dev); 1812 return -ENOMEM; 1813 } 1814 1815 static int ipoib_ioctl(struct net_device *dev, struct ifreq *ifr, 1816 int cmd) 1817 { 1818 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1819 1820 if (!priv->rn_ops->ndo_eth_ioctl) 1821 return -EOPNOTSUPP; 1822 1823 return priv->rn_ops->ndo_eth_ioctl(dev, ifr, cmd); 1824 } 1825 1826 static int ipoib_hwtstamp_get(struct net_device *dev, 1827 struct kernel_hwtstamp_config *config) 1828 { 1829 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1830 1831 if (!priv->rn_ops->ndo_hwtstamp_get) 1832 return -EOPNOTSUPP; 1833 1834 return priv->rn_ops->ndo_hwtstamp_get(dev, config); 1835 } 1836 1837 static int ipoib_hwtstamp_set(struct net_device *dev, 1838 struct kernel_hwtstamp_config *config, 1839 struct netlink_ext_ack *extack) 1840 { 1841 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1842 1843 if (!priv->rn_ops->ndo_hwtstamp_set) 1844 return -EOPNOTSUPP; 1845 1846 return priv->rn_ops->ndo_hwtstamp_set(dev, config, extack); 1847 } 1848 1849 static int ipoib_dev_init(struct net_device *dev) 1850 { 1851 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1852 int ret = -ENOMEM; 1853 1854 priv->qp = NULL; 1855 1856 /* 1857 * the various IPoIB tasks assume they will never race against 1858 * themselves, so always use a single thread workqueue 1859 */ 1860 priv->wq = alloc_ordered_workqueue("ipoib_wq", WQ_MEM_RECLAIM); 1861 if (!priv->wq) { 1862 pr_warn("%s: failed to allocate device WQ\n", dev->name); 1863 goto out; 1864 } 1865 1866 /* create pd, which used both for control and datapath*/ 1867 priv->pd = ib_alloc_pd(priv->ca, 0); 1868 if (IS_ERR(priv->pd)) { 1869 pr_warn("%s: failed to allocate PD\n", priv->ca->name); 1870 goto clean_wq; 1871 } 1872 1873 ret = priv->rn_ops->ndo_init(dev); 1874 if (ret) { 1875 pr_warn("%s failed to init HW resource\n", dev->name); 1876 goto out_free_pd; 1877 } 1878 1879 ret = ipoib_neigh_hash_init(priv); 1880 if (ret) { 1881 pr_warn("%s failed to init neigh hash\n", dev->name); 1882 goto out_dev_uninit; 1883 } 1884 1885 if (dev->flags & IFF_UP) { 1886 if (ipoib_ib_dev_open(dev)) { 1887 pr_warn("%s failed to open device\n", dev->name); 1888 ret = -ENODEV; 1889 goto out_hash_uninit; 1890 } 1891 } 1892 1893 return 0; 1894 1895 out_hash_uninit: 1896 ipoib_neigh_hash_uninit(dev); 1897 1898 out_dev_uninit: 1899 ipoib_ib_dev_cleanup(dev); 1900 1901 out_free_pd: 1902 if (priv->pd) { 1903 ib_dealloc_pd(priv->pd); 1904 priv->pd = NULL; 1905 } 1906 1907 clean_wq: 1908 if (priv->wq) { 1909 destroy_workqueue(priv->wq); 1910 priv->wq = NULL; 1911 } 1912 1913 out: 1914 return ret; 1915 } 1916 1917 /* 1918 * This must be called before doing an unregister_netdev on a parent device to 1919 * shutdown the IB event handler. 1920 */ 1921 static void ipoib_parent_unregister_pre(struct net_device *ndev) 1922 { 1923 struct ipoib_dev_priv *priv = ipoib_priv(ndev); 1924 1925 /* 1926 * ipoib_set_mac checks netif_running before pushing work, clearing 1927 * running ensures the it will not add more work. 1928 */ 1929 rtnl_lock(); 1930 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP, NULL); 1931 rtnl_unlock(); 1932 1933 /* ipoib_event() cannot be running once this returns */ 1934 ib_unregister_event_handler(&priv->event_handler); 1935 1936 /* 1937 * Work on the queue grabs the rtnl lock, so this cannot be done while 1938 * also holding it. 1939 */ 1940 flush_workqueue(ipoib_workqueue); 1941 } 1942 1943 static void ipoib_set_dev_features(struct ipoib_dev_priv *priv) 1944 { 1945 priv->hca_caps = priv->ca->attrs.device_cap_flags; 1946 priv->kernel_caps = priv->ca->attrs.kernel_cap_flags; 1947 1948 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) { 1949 priv->dev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM; 1950 1951 if (priv->kernel_caps & IBK_UD_TSO) 1952 priv->dev->hw_features |= NETIF_F_TSO; 1953 1954 priv->dev->features |= priv->dev->hw_features; 1955 } 1956 } 1957 1958 static int ipoib_parent_init(struct net_device *ndev) 1959 { 1960 struct ipoib_dev_priv *priv = ipoib_priv(ndev); 1961 struct ib_port_attr attr; 1962 int result; 1963 1964 result = ib_query_port(priv->ca, priv->port, &attr); 1965 if (result) { 1966 pr_warn("%s: ib_query_port %d failed\n", priv->ca->name, 1967 priv->port); 1968 return result; 1969 } 1970 priv->max_ib_mtu = rdma_mtu_from_attr(priv->ca, priv->port, &attr); 1971 1972 result = ib_query_pkey(priv->ca, priv->port, 0, &priv->pkey); 1973 if (result) { 1974 pr_warn("%s: ib_query_pkey port %d failed (ret = %d)\n", 1975 priv->ca->name, priv->port, result); 1976 return result; 1977 } 1978 1979 result = rdma_query_gid(priv->ca, priv->port, 0, &priv->local_gid); 1980 if (result) { 1981 pr_warn("%s: rdma_query_gid port %d failed (ret = %d)\n", 1982 priv->ca->name, priv->port, result); 1983 return result; 1984 } 1985 dev_addr_mod(priv->dev, 4, priv->local_gid.raw, sizeof(union ib_gid)); 1986 1987 SET_NETDEV_DEV(priv->dev, priv->ca->dev.parent); 1988 priv->dev->dev_port = priv->port - 1; 1989 /* Let's set this one too for backwards compatibility. */ 1990 priv->dev->dev_id = priv->port - 1; 1991 1992 return 0; 1993 } 1994 1995 static void ipoib_child_init(struct net_device *ndev) 1996 { 1997 struct ipoib_dev_priv *priv = ipoib_priv(ndev); 1998 struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent); 1999 2000 priv->max_ib_mtu = ppriv->max_ib_mtu; 2001 set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags); 2002 if (memchr_inv(priv->dev->dev_addr, 0, INFINIBAND_ALEN)) 2003 memcpy(&priv->local_gid, priv->dev->dev_addr + 4, 2004 sizeof(priv->local_gid)); 2005 else { 2006 __dev_addr_set(priv->dev, ppriv->dev->dev_addr, 2007 INFINIBAND_ALEN); 2008 memcpy(&priv->local_gid, &ppriv->local_gid, 2009 sizeof(priv->local_gid)); 2010 } 2011 } 2012 2013 static int ipoib_ndo_init(struct net_device *ndev) 2014 { 2015 struct ipoib_dev_priv *priv = ipoib_priv(ndev); 2016 int rc; 2017 struct rdma_netdev *rn = netdev_priv(ndev); 2018 2019 if (priv->parent) { 2020 ipoib_child_init(ndev); 2021 } else { 2022 rc = ipoib_parent_init(ndev); 2023 if (rc) 2024 return rc; 2025 } 2026 2027 /* MTU will be reset when mcast join happens */ 2028 ndev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu); 2029 priv->mcast_mtu = priv->admin_mtu = ndev->mtu; 2030 rn->mtu = priv->mcast_mtu; 2031 ndev->max_mtu = IPOIB_CM_MTU; 2032 2033 ndev->neigh_priv_len = sizeof(struct ipoib_neigh); 2034 2035 /* 2036 * Set the full membership bit, so that we join the right 2037 * broadcast group, etc. 2038 */ 2039 priv->pkey |= 0x8000; 2040 2041 ndev->broadcast[8] = priv->pkey >> 8; 2042 ndev->broadcast[9] = priv->pkey & 0xff; 2043 set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags); 2044 2045 ipoib_set_dev_features(priv); 2046 2047 rc = ipoib_dev_init(ndev); 2048 if (rc) { 2049 pr_warn("%s: failed to initialize device: %s port %d (ret = %d)\n", 2050 priv->ca->name, priv->dev->name, priv->port, rc); 2051 return rc; 2052 } 2053 2054 if (priv->parent) { 2055 struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent); 2056 2057 dev_hold(priv->parent); 2058 2059 netdev_lock(priv->parent); 2060 list_add_tail(&priv->list, &ppriv->child_intfs); 2061 netdev_unlock(priv->parent); 2062 } 2063 2064 return 0; 2065 } 2066 2067 static void ipoib_ndo_uninit(struct net_device *dev) 2068 { 2069 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2070 2071 /* 2072 * ipoib_remove_one guarantees the children are removed before the 2073 * parent, and that is the only place where a parent can be removed. 2074 */ 2075 WARN_ON(!list_empty(&priv->child_intfs)); 2076 2077 if (priv->parent) { 2078 struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent); 2079 2080 netdev_lock(ppriv->dev); 2081 list_del(&priv->list); 2082 netdev_unlock(ppriv->dev); 2083 } 2084 2085 ipoib_neigh_hash_uninit(dev); 2086 2087 ipoib_ib_dev_cleanup(dev); 2088 2089 /* no more works over the priv->wq */ 2090 if (priv->wq) { 2091 /* See ipoib_mcast_carrier_on_task() */ 2092 WARN_ON(test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)); 2093 destroy_workqueue(priv->wq); 2094 priv->wq = NULL; 2095 } 2096 2097 dev_put(priv->parent); 2098 } 2099 2100 static int ipoib_set_vf_link_state(struct net_device *dev, int vf, int link_state) 2101 { 2102 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2103 2104 return ib_set_vf_link_state(priv->ca, vf, priv->port, link_state); 2105 } 2106 2107 static int ipoib_get_vf_config(struct net_device *dev, int vf, 2108 struct ifla_vf_info *ivf) 2109 { 2110 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2111 int err; 2112 2113 err = ib_get_vf_config(priv->ca, vf, priv->port, ivf); 2114 if (err) 2115 return err; 2116 2117 ivf->vf = vf; 2118 memcpy(ivf->mac, dev->dev_addr, dev->addr_len); 2119 2120 return 0; 2121 } 2122 2123 static int ipoib_set_vf_guid(struct net_device *dev, int vf, u64 guid, int type) 2124 { 2125 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2126 2127 if (type != IFLA_VF_IB_NODE_GUID && type != IFLA_VF_IB_PORT_GUID) 2128 return -EINVAL; 2129 2130 return ib_set_vf_guid(priv->ca, vf, priv->port, guid, type); 2131 } 2132 2133 static int ipoib_get_vf_guid(struct net_device *dev, int vf, 2134 struct ifla_vf_guid *node_guid, 2135 struct ifla_vf_guid *port_guid) 2136 { 2137 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2138 2139 return ib_get_vf_guid(priv->ca, vf, priv->port, node_guid, port_guid); 2140 } 2141 2142 static int ipoib_get_vf_stats(struct net_device *dev, int vf, 2143 struct ifla_vf_stats *vf_stats) 2144 { 2145 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2146 2147 return ib_get_vf_stats(priv->ca, vf, priv->port, vf_stats); 2148 } 2149 2150 static const struct header_ops ipoib_header_ops = { 2151 .create = ipoib_hard_header, 2152 }; 2153 2154 static const struct net_device_ops ipoib_netdev_ops_pf = { 2155 .ndo_init = ipoib_ndo_init, 2156 .ndo_uninit = ipoib_ndo_uninit, 2157 .ndo_open = ipoib_open, 2158 .ndo_stop = ipoib_stop, 2159 .ndo_change_mtu = ipoib_change_mtu, 2160 .ndo_fix_features = ipoib_fix_features, 2161 .ndo_start_xmit = ipoib_start_xmit, 2162 .ndo_tx_timeout = ipoib_timeout, 2163 .ndo_set_rx_mode = ipoib_set_mcast_list, 2164 .ndo_get_iflink = ipoib_get_iflink, 2165 .ndo_set_vf_link_state = ipoib_set_vf_link_state, 2166 .ndo_get_vf_config = ipoib_get_vf_config, 2167 .ndo_get_vf_stats = ipoib_get_vf_stats, 2168 .ndo_get_vf_guid = ipoib_get_vf_guid, 2169 .ndo_set_vf_guid = ipoib_set_vf_guid, 2170 .ndo_set_mac_address = ipoib_set_mac, 2171 .ndo_get_stats64 = ipoib_get_stats, 2172 .ndo_eth_ioctl = ipoib_ioctl, 2173 .ndo_hwtstamp_get = ipoib_hwtstamp_get, 2174 .ndo_hwtstamp_set = ipoib_hwtstamp_set, 2175 }; 2176 2177 static const struct net_device_ops ipoib_netdev_ops_vf = { 2178 .ndo_init = ipoib_ndo_init, 2179 .ndo_uninit = ipoib_ndo_uninit, 2180 .ndo_open = ipoib_open, 2181 .ndo_stop = ipoib_stop, 2182 .ndo_change_mtu = ipoib_change_mtu, 2183 .ndo_fix_features = ipoib_fix_features, 2184 .ndo_start_xmit = ipoib_start_xmit, 2185 .ndo_tx_timeout = ipoib_timeout, 2186 .ndo_set_rx_mode = ipoib_set_mcast_list, 2187 .ndo_get_iflink = ipoib_get_iflink, 2188 .ndo_get_stats64 = ipoib_get_stats, 2189 .ndo_eth_ioctl = ipoib_ioctl, 2190 .ndo_hwtstamp_get = ipoib_hwtstamp_get, 2191 .ndo_hwtstamp_set = ipoib_hwtstamp_set, 2192 }; 2193 2194 static const struct net_device_ops ipoib_netdev_default_pf = { 2195 .ndo_init = ipoib_dev_init_default, 2196 .ndo_uninit = ipoib_dev_uninit_default, 2197 .ndo_open = ipoib_ib_dev_open_default, 2198 .ndo_stop = ipoib_ib_dev_stop_default, 2199 }; 2200 2201 void ipoib_setup_common(struct net_device *dev) 2202 { 2203 dev->header_ops = &ipoib_header_ops; 2204 dev->netdev_ops = &ipoib_netdev_default_pf; 2205 2206 ipoib_set_ethtool_ops(dev); 2207 2208 dev->watchdog_timeo = 10 * HZ; 2209 2210 dev->flags |= IFF_BROADCAST | IFF_MULTICAST; 2211 2212 dev->hard_header_len = IPOIB_HARD_LEN; 2213 dev->addr_len = INFINIBAND_ALEN; 2214 dev->type = ARPHRD_INFINIBAND; 2215 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN; 2216 dev->features = (NETIF_F_VLAN_CHALLENGED | 2217 NETIF_F_HIGHDMA); 2218 netif_keep_dst(dev); 2219 2220 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN); 2221 2222 /* 2223 * unregister_netdev always frees the netdev, we use this mode 2224 * consistently to unify all the various unregister paths, including 2225 * those connected to rtnl_link_ops which require it. 2226 */ 2227 dev->needs_free_netdev = true; 2228 } 2229 2230 static void ipoib_build_priv(struct net_device *dev) 2231 { 2232 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2233 2234 priv->dev = dev; 2235 spin_lock_init(&priv->lock); 2236 mutex_init(&priv->mcast_mutex); 2237 2238 INIT_LIST_HEAD(&priv->path_list); 2239 INIT_LIST_HEAD(&priv->child_intfs); 2240 INIT_LIST_HEAD(&priv->dead_ahs); 2241 INIT_LIST_HEAD(&priv->multicast_list); 2242 2243 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task); 2244 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task); 2245 INIT_WORK(&priv->reschedule_napi_work, ipoib_napi_schedule_work); 2246 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light); 2247 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal); 2248 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy); 2249 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task); 2250 INIT_WORK(&priv->tx_timeout_work, ipoib_ib_tx_timeout_work); 2251 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah); 2252 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh); 2253 } 2254 2255 static struct net_device *ipoib_alloc_netdev(struct ib_device *hca, u32 port, 2256 const char *name) 2257 { 2258 struct net_device *dev; 2259 2260 dev = rdma_alloc_netdev(hca, port, RDMA_NETDEV_IPOIB, name, 2261 NET_NAME_UNKNOWN, ipoib_setup_common); 2262 if (!IS_ERR(dev) || PTR_ERR(dev) != -EOPNOTSUPP) 2263 return dev; 2264 2265 dev = alloc_netdev(sizeof(struct rdma_netdev), name, NET_NAME_UNKNOWN, 2266 ipoib_setup_common); 2267 if (!dev) 2268 return ERR_PTR(-ENOMEM); 2269 return dev; 2270 } 2271 2272 int ipoib_intf_init(struct ib_device *hca, u32 port, const char *name, 2273 struct net_device *dev) 2274 { 2275 struct rdma_netdev *rn = netdev_priv(dev); 2276 struct ipoib_dev_priv *priv; 2277 int rc; 2278 2279 priv = kzalloc_obj(*priv); 2280 if (!priv) 2281 return -ENOMEM; 2282 2283 priv->ca = hca; 2284 priv->port = port; 2285 2286 rc = rdma_init_netdev(hca, port, RDMA_NETDEV_IPOIB, name, 2287 NET_NAME_UNKNOWN, ipoib_setup_common, dev); 2288 if (rc) { 2289 if (rc != -EOPNOTSUPP) 2290 goto out; 2291 2292 rn->send = ipoib_send; 2293 rn->attach_mcast = ipoib_mcast_attach; 2294 rn->detach_mcast = ipoib_mcast_detach; 2295 rn->hca = hca; 2296 2297 rc = netif_set_real_num_tx_queues(dev, 1); 2298 if (rc) 2299 goto out; 2300 2301 rc = netif_set_real_num_rx_queues(dev, 1); 2302 if (rc) 2303 goto out; 2304 } 2305 2306 priv->rn_ops = dev->netdev_ops; 2307 2308 if (hca->attrs.kernel_cap_flags & IBK_VIRTUAL_FUNCTION) 2309 dev->netdev_ops = &ipoib_netdev_ops_vf; 2310 else 2311 dev->netdev_ops = &ipoib_netdev_ops_pf; 2312 2313 rn->clnt_priv = priv; 2314 /* 2315 * Only the child register_netdev flows can handle priv_destructor 2316 * being set, so we force it to NULL here and handle manually until it 2317 * is safe to turn on. 2318 */ 2319 priv->next_priv_destructor = dev->priv_destructor; 2320 dev->priv_destructor = NULL; 2321 2322 ipoib_build_priv(dev); 2323 2324 return 0; 2325 2326 out: 2327 kfree(priv); 2328 return rc; 2329 } 2330 2331 struct net_device *ipoib_intf_alloc(struct ib_device *hca, u32 port, 2332 const char *name) 2333 { 2334 struct net_device *dev; 2335 int rc; 2336 2337 dev = ipoib_alloc_netdev(hca, port, name); 2338 if (IS_ERR(dev)) 2339 return dev; 2340 2341 rc = ipoib_intf_init(hca, port, name, dev); 2342 if (rc) { 2343 free_netdev(dev); 2344 return ERR_PTR(rc); 2345 } 2346 2347 /* 2348 * Upon success the caller must ensure ipoib_intf_free is called or 2349 * register_netdevice succeed'd and priv_destructor is set to 2350 * ipoib_intf_free. 2351 */ 2352 return dev; 2353 } 2354 2355 void ipoib_intf_free(struct net_device *dev) 2356 { 2357 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2358 struct rdma_netdev *rn = netdev_priv(dev); 2359 2360 dev->priv_destructor = priv->next_priv_destructor; 2361 if (dev->priv_destructor) 2362 dev->priv_destructor(dev); 2363 2364 /* 2365 * There are some error flows around register_netdev failing that may 2366 * attempt to call priv_destructor twice, prevent that from happening. 2367 */ 2368 dev->priv_destructor = NULL; 2369 2370 /* unregister/destroy is very complicated. Make bugs more obvious. */ 2371 rn->clnt_priv = NULL; 2372 2373 kfree(priv); 2374 } 2375 2376 static ssize_t pkey_show(struct device *dev, struct device_attribute *attr, 2377 char *buf) 2378 { 2379 struct net_device *ndev = to_net_dev(dev); 2380 struct ipoib_dev_priv *priv = ipoib_priv(ndev); 2381 2382 return sysfs_emit(buf, "0x%04x\n", priv->pkey); 2383 } 2384 static DEVICE_ATTR_RO(pkey); 2385 2386 static ssize_t umcast_show(struct device *dev, struct device_attribute *attr, 2387 char *buf) 2388 { 2389 struct net_device *ndev = to_net_dev(dev); 2390 struct ipoib_dev_priv *priv = ipoib_priv(ndev); 2391 2392 return sysfs_emit(buf, "%d\n", 2393 test_bit(IPOIB_FLAG_UMCAST, &priv->flags)); 2394 } 2395 2396 void ipoib_set_umcast(struct net_device *ndev, int umcast_val) 2397 { 2398 struct ipoib_dev_priv *priv = ipoib_priv(ndev); 2399 2400 if (umcast_val > 0) { 2401 set_bit(IPOIB_FLAG_UMCAST, &priv->flags); 2402 ipoib_warn(priv, "ignoring multicast groups joined directly " 2403 "by userspace\n"); 2404 } else 2405 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags); 2406 } 2407 2408 static ssize_t umcast_store(struct device *dev, struct device_attribute *attr, 2409 const char *buf, size_t count) 2410 { 2411 unsigned long umcast_val = simple_strtoul(buf, NULL, 0); 2412 2413 ipoib_set_umcast(to_net_dev(dev), umcast_val); 2414 2415 return count; 2416 } 2417 static DEVICE_ATTR_RW(umcast); 2418 2419 int ipoib_add_umcast_attr(struct net_device *dev) 2420 { 2421 return device_create_file(&dev->dev, &dev_attr_umcast); 2422 } 2423 2424 static void set_base_guid(struct ipoib_dev_priv *priv, union ib_gid *gid) 2425 { 2426 struct ipoib_dev_priv *child_priv; 2427 struct net_device *netdev = priv->dev; 2428 2429 netif_addr_lock_bh(netdev); 2430 2431 memcpy(&priv->local_gid.global.interface_id, 2432 &gid->global.interface_id, 2433 sizeof(gid->global.interface_id)); 2434 dev_addr_mod(netdev, 4, (u8 *)&priv->local_gid, sizeof(priv->local_gid)); 2435 clear_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags); 2436 2437 netif_addr_unlock_bh(netdev); 2438 2439 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) { 2440 netdev_lock_ops_to_full(priv->dev); 2441 list_for_each_entry(child_priv, &priv->child_intfs, list) 2442 set_base_guid(child_priv, gid); 2443 netdev_unlock_full_to_ops(priv->dev); 2444 } 2445 } 2446 2447 static int ipoib_check_lladdr(struct net_device *dev, 2448 struct sockaddr_storage *ss) 2449 { 2450 union ib_gid *gid = (union ib_gid *)(ss->__data + 4); 2451 int ret = 0; 2452 2453 netif_addr_lock_bh(dev); 2454 2455 /* Make sure the QPN, reserved and subnet prefix match the current 2456 * lladdr, it also makes sure the lladdr is unicast. 2457 */ 2458 if (memcmp(dev->dev_addr, ss->__data, 2459 4 + sizeof(gid->global.subnet_prefix)) || 2460 gid->global.interface_id == 0) 2461 ret = -EINVAL; 2462 2463 netif_addr_unlock_bh(dev); 2464 2465 return ret; 2466 } 2467 2468 static int ipoib_set_mac(struct net_device *dev, void *addr) 2469 { 2470 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2471 struct sockaddr_storage *ss = addr; 2472 int ret; 2473 2474 if (!(dev->priv_flags & IFF_LIVE_ADDR_CHANGE) && netif_running(dev)) 2475 return -EBUSY; 2476 2477 ret = ipoib_check_lladdr(dev, ss); 2478 if (ret) 2479 return ret; 2480 2481 set_base_guid(priv, (union ib_gid *)(ss->__data + 4)); 2482 2483 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) { 2484 struct ipoib_dev_priv *cpriv; 2485 2486 netdev_lock_ops_to_full(dev); 2487 list_for_each_entry(cpriv, &priv->child_intfs, list) 2488 queue_work(ipoib_workqueue, &cpriv->flush_light); 2489 netdev_unlock_full_to_ops(dev); 2490 } 2491 queue_work(ipoib_workqueue, &priv->flush_light); 2492 2493 return 0; 2494 } 2495 2496 static ssize_t create_child_store(struct device *dev, 2497 struct device_attribute *attr, 2498 const char *buf, size_t count) 2499 { 2500 int pkey; 2501 int ret; 2502 2503 if (sscanf(buf, "%i", &pkey) != 1) 2504 return -EINVAL; 2505 2506 if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000) 2507 return -EINVAL; 2508 2509 ret = ipoib_vlan_add(to_net_dev(dev), pkey); 2510 2511 return ret ? ret : count; 2512 } 2513 static DEVICE_ATTR_WO(create_child); 2514 2515 static ssize_t delete_child_store(struct device *dev, 2516 struct device_attribute *attr, 2517 const char *buf, size_t count) 2518 { 2519 int pkey; 2520 int ret; 2521 2522 if (sscanf(buf, "%i", &pkey) != 1) 2523 return -EINVAL; 2524 2525 if (pkey < 0 || pkey > 0xffff) 2526 return -EINVAL; 2527 2528 ret = ipoib_vlan_delete(to_net_dev(dev), pkey); 2529 2530 return ret ? ret : count; 2531 2532 } 2533 static DEVICE_ATTR_WO(delete_child); 2534 2535 int ipoib_add_pkey_attr(struct net_device *dev) 2536 { 2537 return device_create_file(&dev->dev, &dev_attr_pkey); 2538 } 2539 2540 /* 2541 * We erroneously exposed the iface's port number in the dev_id 2542 * sysfs field long after dev_port was introduced for that purpose[1], 2543 * and we need to stop everyone from relying on that. 2544 * Let's overload the shower routine for the dev_id file here 2545 * to gently bring the issue up. 2546 * 2547 * [1] https://www.spinics.net/lists/netdev/msg272123.html 2548 */ 2549 static ssize_t dev_id_show(struct device *dev, 2550 struct device_attribute *attr, char *buf) 2551 { 2552 struct net_device *ndev = to_net_dev(dev); 2553 2554 /* 2555 * ndev->dev_port will be equal to 0 in old kernel prior to commit 2556 * 9b8b2a323008 ("IB/ipoib: Use dev_port to expose network interface 2557 * port numbers") Zero was chosen as special case for user space 2558 * applications to fallback and query dev_id to check if it has 2559 * different value or not. 2560 * 2561 * Don't print warning in such scenario. 2562 * 2563 * https://github.com/systemd/systemd/blob/master/src/udev/udev-builtin-net_id.c#L358 2564 */ 2565 if (ndev->dev_port && ndev->dev_id == ndev->dev_port) 2566 netdev_info_once(ndev, 2567 "\"%s\" wants to know my dev_id. Should it look at dev_port instead? See Documentation/ABI/testing/sysfs-class-net for more info.\n", 2568 current->comm); 2569 2570 return sysfs_emit(buf, "%#x\n", ndev->dev_id); 2571 } 2572 static DEVICE_ATTR_RO(dev_id); 2573 2574 static int ipoib_intercept_dev_id_attr(struct net_device *dev) 2575 { 2576 device_remove_file(&dev->dev, &dev_attr_dev_id); 2577 return device_create_file(&dev->dev, &dev_attr_dev_id); 2578 } 2579 2580 static struct net_device *ipoib_add_port(const char *format, 2581 struct ib_device *hca, u32 port) 2582 { 2583 struct rtnl_link_ops *ops = ipoib_get_link_ops(); 2584 struct rdma_netdev_alloc_params params; 2585 struct ipoib_dev_priv *priv; 2586 struct net_device *ndev; 2587 int result; 2588 2589 ndev = ipoib_intf_alloc(hca, port, format); 2590 if (IS_ERR(ndev)) { 2591 pr_warn("%s, %d: ipoib_intf_alloc failed %ld\n", hca->name, port, 2592 PTR_ERR(ndev)); 2593 return ndev; 2594 } 2595 priv = ipoib_priv(ndev); 2596 2597 INIT_IB_EVENT_HANDLER(&priv->event_handler, 2598 priv->ca, ipoib_event); 2599 ib_register_event_handler(&priv->event_handler); 2600 2601 /* call event handler to ensure pkey in sync */ 2602 ipoib_queue_work(priv, IPOIB_FLUSH_HEAVY); 2603 2604 ndev->rtnl_link_ops = ipoib_get_link_ops(); 2605 2606 dev_net_set(ndev, rdma_dev_net(hca)); 2607 2608 result = register_netdev(ndev); 2609 if (result) { 2610 pr_warn("%s: couldn't register ipoib port %d; error %d\n", 2611 hca->name, port, result); 2612 2613 ipoib_parent_unregister_pre(ndev); 2614 ipoib_intf_free(ndev); 2615 free_netdev(ndev); 2616 2617 return ERR_PTR(result); 2618 } 2619 2620 if (hca->ops.rdma_netdev_get_params) { 2621 int rc = hca->ops.rdma_netdev_get_params(hca, port, 2622 RDMA_NETDEV_IPOIB, 2623 ¶ms); 2624 2625 if (!rc && ops->priv_size < params.sizeof_priv) 2626 ops->priv_size = params.sizeof_priv; 2627 } 2628 /* 2629 * We cannot set priv_destructor before register_netdev because we 2630 * need priv to be always valid during the error flow to execute 2631 * ipoib_parent_unregister_pre(). Instead handle it manually and only 2632 * enter priv_destructor mode once we are completely registered. 2633 */ 2634 ndev->priv_destructor = ipoib_intf_free; 2635 2636 if (ipoib_intercept_dev_id_attr(ndev)) 2637 goto sysfs_failed; 2638 if (ipoib_cm_add_mode_attr(ndev)) 2639 goto sysfs_failed; 2640 if (ipoib_add_pkey_attr(ndev)) 2641 goto sysfs_failed; 2642 if (ipoib_add_umcast_attr(ndev)) 2643 goto sysfs_failed; 2644 if (device_create_file(&ndev->dev, &dev_attr_create_child)) 2645 goto sysfs_failed; 2646 if (device_create_file(&ndev->dev, &dev_attr_delete_child)) 2647 goto sysfs_failed; 2648 2649 return ndev; 2650 2651 sysfs_failed: 2652 ipoib_parent_unregister_pre(ndev); 2653 unregister_netdev(ndev); 2654 return ERR_PTR(-ENOMEM); 2655 } 2656 2657 static int ipoib_add_one(struct ib_device *device) 2658 { 2659 struct list_head *dev_list; 2660 struct net_device *dev; 2661 struct ipoib_dev_priv *priv; 2662 unsigned int p; 2663 int count = 0; 2664 2665 dev_list = kmalloc_obj(*dev_list); 2666 if (!dev_list) 2667 return -ENOMEM; 2668 2669 INIT_LIST_HEAD(dev_list); 2670 2671 rdma_for_each_port (device, p) { 2672 if (!rdma_protocol_ib(device, p)) 2673 continue; 2674 dev = ipoib_add_port("ib%d", device, p); 2675 if (!IS_ERR(dev)) { 2676 priv = ipoib_priv(dev); 2677 list_add_tail(&priv->list, dev_list); 2678 count++; 2679 } 2680 } 2681 2682 if (!count) { 2683 kfree(dev_list); 2684 return -EOPNOTSUPP; 2685 } 2686 2687 ib_set_client_data(device, &ipoib_client, dev_list); 2688 return 0; 2689 } 2690 2691 static void ipoib_remove_one(struct ib_device *device, void *client_data) 2692 { 2693 struct ipoib_dev_priv *priv, *tmp, *cpriv, *tcpriv; 2694 struct list_head *dev_list = client_data; 2695 2696 list_for_each_entry_safe(priv, tmp, dev_list, list) { 2697 LIST_HEAD(head); 2698 ipoib_parent_unregister_pre(priv->dev); 2699 2700 rtnl_lock(); 2701 2702 netdev_lock(priv->dev); 2703 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, 2704 list) 2705 unregister_netdevice_queue(cpriv->dev, &head); 2706 netdev_unlock(priv->dev); 2707 unregister_netdevice_queue(priv->dev, &head); 2708 unregister_netdevice_many(&head); 2709 2710 rtnl_unlock(); 2711 } 2712 2713 kfree(dev_list); 2714 } 2715 2716 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG 2717 static struct notifier_block ipoib_netdev_notifier = { 2718 .notifier_call = ipoib_netdev_event, 2719 }; 2720 #endif 2721 2722 static int __init ipoib_init_module(void) 2723 { 2724 int ret; 2725 2726 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size); 2727 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE); 2728 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE); 2729 2730 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size); 2731 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE); 2732 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE); 2733 #ifdef CONFIG_INFINIBAND_IPOIB_CM 2734 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP); 2735 ipoib_max_conn_qp = max(ipoib_max_conn_qp, 0); 2736 #endif 2737 2738 /* 2739 * When copying small received packets, we only copy from the 2740 * linear data part of the SKB, so we rely on this condition. 2741 */ 2742 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE); 2743 2744 ipoib_register_debugfs(); 2745 2746 /* 2747 * We create a global workqueue here that is used for all flush 2748 * operations. However, if you attempt to flush a workqueue 2749 * from a task on that same workqueue, it deadlocks the system. 2750 * We want to be able to flush the tasks associated with a 2751 * specific net device, so we also create a workqueue for each 2752 * netdevice. We queue up the tasks for that device only on 2753 * its private workqueue, and we only queue up flush events 2754 * on our global flush workqueue. This avoids the deadlocks. 2755 */ 2756 ipoib_workqueue = alloc_ordered_workqueue("ipoib_flush", 0); 2757 if (!ipoib_workqueue) { 2758 ret = -ENOMEM; 2759 goto err_fs; 2760 } 2761 2762 ib_sa_register_client(&ipoib_sa_client); 2763 2764 ret = ib_register_client(&ipoib_client); 2765 if (ret) 2766 goto err_sa; 2767 2768 ret = ipoib_netlink_init(); 2769 if (ret) 2770 goto err_client; 2771 2772 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG 2773 register_netdevice_notifier(&ipoib_netdev_notifier); 2774 #endif 2775 return 0; 2776 2777 err_client: 2778 ib_unregister_client(&ipoib_client); 2779 2780 err_sa: 2781 ib_sa_unregister_client(&ipoib_sa_client); 2782 destroy_workqueue(ipoib_workqueue); 2783 2784 err_fs: 2785 ipoib_unregister_debugfs(); 2786 2787 return ret; 2788 } 2789 2790 static void __exit ipoib_cleanup_module(void) 2791 { 2792 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG 2793 unregister_netdevice_notifier(&ipoib_netdev_notifier); 2794 #endif 2795 ipoib_netlink_fini(); 2796 ib_unregister_client(&ipoib_client); 2797 ib_sa_unregister_client(&ipoib_sa_client); 2798 ipoib_unregister_debugfs(); 2799 destroy_workqueue(ipoib_workqueue); 2800 } 2801 2802 module_init(ipoib_init_module); 2803 module_exit(ipoib_cleanup_module); 2804