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(sizeof(*work), GFP_KERNEL); 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(sizeof(*iter), GFP_KERNEL); 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(sizeof(*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(sizeof(*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(sizeof(*htbl), GFP_KERNEL); 1597 if (!htbl) 1598 return -ENOMEM; 1599 size = roundup_pow_of_two(arp_tbl.gc_thresh3); 1600 buckets = kvcalloc(size, sizeof(*buckets), GFP_KERNEL); 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 = kcalloc(ipoib_recvq_size, 1777 sizeof(*priv->rx_ring), 1778 GFP_KERNEL); 1779 if (!priv->rx_ring) 1780 goto out; 1781 1782 priv->tx_ring = vzalloc(array_size(ipoib_sendq_size, 1783 sizeof(*priv->tx_ring))); 1784 if (!priv->tx_ring) { 1785 pr_warn("%s: failed to allocate TX ring (%d entries)\n", 1786 priv->ca->name, ipoib_sendq_size); 1787 goto out_rx_ring_cleanup; 1788 } 1789 1790 /* priv->tx_head, tx_tail and global_tx_tail/head are already 0 */ 1791 1792 if (ipoib_transport_dev_init(dev, priv->ca)) { 1793 pr_warn("%s: ipoib_transport_dev_init failed\n", 1794 priv->ca->name); 1795 goto out_tx_ring_cleanup; 1796 } 1797 1798 /* after qp created set dev address */ 1799 addr_mod[0] = (priv->qp->qp_num >> 16) & 0xff; 1800 addr_mod[1] = (priv->qp->qp_num >> 8) & 0xff; 1801 addr_mod[2] = (priv->qp->qp_num) & 0xff; 1802 dev_addr_mod(priv->dev, 1, addr_mod, sizeof(addr_mod)); 1803 1804 return 0; 1805 1806 out_tx_ring_cleanup: 1807 vfree(priv->tx_ring); 1808 1809 out_rx_ring_cleanup: 1810 kfree(priv->rx_ring); 1811 1812 out: 1813 ipoib_napi_del(dev); 1814 return -ENOMEM; 1815 } 1816 1817 static int ipoib_ioctl(struct net_device *dev, struct ifreq *ifr, 1818 int cmd) 1819 { 1820 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1821 1822 if (!priv->rn_ops->ndo_eth_ioctl) 1823 return -EOPNOTSUPP; 1824 1825 return priv->rn_ops->ndo_eth_ioctl(dev, ifr, cmd); 1826 } 1827 1828 static int ipoib_dev_init(struct net_device *dev) 1829 { 1830 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1831 int ret = -ENOMEM; 1832 1833 priv->qp = NULL; 1834 1835 /* 1836 * the various IPoIB tasks assume they will never race against 1837 * themselves, so always use a single thread workqueue 1838 */ 1839 priv->wq = alloc_ordered_workqueue("ipoib_wq", WQ_MEM_RECLAIM); 1840 if (!priv->wq) { 1841 pr_warn("%s: failed to allocate device WQ\n", dev->name); 1842 goto out; 1843 } 1844 1845 /* create pd, which used both for control and datapath*/ 1846 priv->pd = ib_alloc_pd(priv->ca, 0); 1847 if (IS_ERR(priv->pd)) { 1848 pr_warn("%s: failed to allocate PD\n", priv->ca->name); 1849 goto clean_wq; 1850 } 1851 1852 ret = priv->rn_ops->ndo_init(dev); 1853 if (ret) { 1854 pr_warn("%s failed to init HW resource\n", dev->name); 1855 goto out_free_pd; 1856 } 1857 1858 ret = ipoib_neigh_hash_init(priv); 1859 if (ret) { 1860 pr_warn("%s failed to init neigh hash\n", dev->name); 1861 goto out_dev_uninit; 1862 } 1863 1864 if (dev->flags & IFF_UP) { 1865 if (ipoib_ib_dev_open(dev)) { 1866 pr_warn("%s failed to open device\n", dev->name); 1867 ret = -ENODEV; 1868 goto out_hash_uninit; 1869 } 1870 } 1871 1872 return 0; 1873 1874 out_hash_uninit: 1875 ipoib_neigh_hash_uninit(dev); 1876 1877 out_dev_uninit: 1878 ipoib_ib_dev_cleanup(dev); 1879 1880 out_free_pd: 1881 if (priv->pd) { 1882 ib_dealloc_pd(priv->pd); 1883 priv->pd = NULL; 1884 } 1885 1886 clean_wq: 1887 if (priv->wq) { 1888 destroy_workqueue(priv->wq); 1889 priv->wq = NULL; 1890 } 1891 1892 out: 1893 return ret; 1894 } 1895 1896 /* 1897 * This must be called before doing an unregister_netdev on a parent device to 1898 * shutdown the IB event handler. 1899 */ 1900 static void ipoib_parent_unregister_pre(struct net_device *ndev) 1901 { 1902 struct ipoib_dev_priv *priv = ipoib_priv(ndev); 1903 1904 /* 1905 * ipoib_set_mac checks netif_running before pushing work, clearing 1906 * running ensures the it will not add more work. 1907 */ 1908 rtnl_lock(); 1909 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP, NULL); 1910 rtnl_unlock(); 1911 1912 /* ipoib_event() cannot be running once this returns */ 1913 ib_unregister_event_handler(&priv->event_handler); 1914 1915 /* 1916 * Work on the queue grabs the rtnl lock, so this cannot be done while 1917 * also holding it. 1918 */ 1919 flush_workqueue(ipoib_workqueue); 1920 } 1921 1922 static void ipoib_set_dev_features(struct ipoib_dev_priv *priv) 1923 { 1924 priv->hca_caps = priv->ca->attrs.device_cap_flags; 1925 priv->kernel_caps = priv->ca->attrs.kernel_cap_flags; 1926 1927 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) { 1928 priv->dev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM; 1929 1930 if (priv->kernel_caps & IBK_UD_TSO) 1931 priv->dev->hw_features |= NETIF_F_TSO; 1932 1933 priv->dev->features |= priv->dev->hw_features; 1934 } 1935 } 1936 1937 static int ipoib_parent_init(struct net_device *ndev) 1938 { 1939 struct ipoib_dev_priv *priv = ipoib_priv(ndev); 1940 struct ib_port_attr attr; 1941 int result; 1942 1943 result = ib_query_port(priv->ca, priv->port, &attr); 1944 if (result) { 1945 pr_warn("%s: ib_query_port %d failed\n", priv->ca->name, 1946 priv->port); 1947 return result; 1948 } 1949 priv->max_ib_mtu = rdma_mtu_from_attr(priv->ca, priv->port, &attr); 1950 1951 result = ib_query_pkey(priv->ca, priv->port, 0, &priv->pkey); 1952 if (result) { 1953 pr_warn("%s: ib_query_pkey port %d failed (ret = %d)\n", 1954 priv->ca->name, priv->port, result); 1955 return result; 1956 } 1957 1958 result = rdma_query_gid(priv->ca, priv->port, 0, &priv->local_gid); 1959 if (result) { 1960 pr_warn("%s: rdma_query_gid port %d failed (ret = %d)\n", 1961 priv->ca->name, priv->port, result); 1962 return result; 1963 } 1964 dev_addr_mod(priv->dev, 4, priv->local_gid.raw, sizeof(union ib_gid)); 1965 1966 SET_NETDEV_DEV(priv->dev, priv->ca->dev.parent); 1967 priv->dev->dev_port = priv->port - 1; 1968 /* Let's set this one too for backwards compatibility. */ 1969 priv->dev->dev_id = priv->port - 1; 1970 1971 return 0; 1972 } 1973 1974 static void ipoib_child_init(struct net_device *ndev) 1975 { 1976 struct ipoib_dev_priv *priv = ipoib_priv(ndev); 1977 struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent); 1978 1979 priv->max_ib_mtu = ppriv->max_ib_mtu; 1980 set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags); 1981 if (memchr_inv(priv->dev->dev_addr, 0, INFINIBAND_ALEN)) 1982 memcpy(&priv->local_gid, priv->dev->dev_addr + 4, 1983 sizeof(priv->local_gid)); 1984 else { 1985 __dev_addr_set(priv->dev, ppriv->dev->dev_addr, 1986 INFINIBAND_ALEN); 1987 memcpy(&priv->local_gid, &ppriv->local_gid, 1988 sizeof(priv->local_gid)); 1989 } 1990 } 1991 1992 static int ipoib_ndo_init(struct net_device *ndev) 1993 { 1994 struct ipoib_dev_priv *priv = ipoib_priv(ndev); 1995 int rc; 1996 struct rdma_netdev *rn = netdev_priv(ndev); 1997 1998 if (priv->parent) { 1999 ipoib_child_init(ndev); 2000 } else { 2001 rc = ipoib_parent_init(ndev); 2002 if (rc) 2003 return rc; 2004 } 2005 2006 /* MTU will be reset when mcast join happens */ 2007 ndev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu); 2008 priv->mcast_mtu = priv->admin_mtu = ndev->mtu; 2009 rn->mtu = priv->mcast_mtu; 2010 ndev->max_mtu = IPOIB_CM_MTU; 2011 2012 ndev->neigh_priv_len = sizeof(struct ipoib_neigh); 2013 2014 /* 2015 * Set the full membership bit, so that we join the right 2016 * broadcast group, etc. 2017 */ 2018 priv->pkey |= 0x8000; 2019 2020 ndev->broadcast[8] = priv->pkey >> 8; 2021 ndev->broadcast[9] = priv->pkey & 0xff; 2022 set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags); 2023 2024 ipoib_set_dev_features(priv); 2025 2026 rc = ipoib_dev_init(ndev); 2027 if (rc) { 2028 pr_warn("%s: failed to initialize device: %s port %d (ret = %d)\n", 2029 priv->ca->name, priv->dev->name, priv->port, rc); 2030 return rc; 2031 } 2032 2033 if (priv->parent) { 2034 struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent); 2035 2036 dev_hold(priv->parent); 2037 2038 netdev_lock(priv->parent); 2039 list_add_tail(&priv->list, &ppriv->child_intfs); 2040 netdev_unlock(priv->parent); 2041 } 2042 2043 return 0; 2044 } 2045 2046 static void ipoib_ndo_uninit(struct net_device *dev) 2047 { 2048 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2049 2050 /* 2051 * ipoib_remove_one guarantees the children are removed before the 2052 * parent, and that is the only place where a parent can be removed. 2053 */ 2054 WARN_ON(!list_empty(&priv->child_intfs)); 2055 2056 if (priv->parent) { 2057 struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent); 2058 2059 netdev_lock(ppriv->dev); 2060 list_del(&priv->list); 2061 netdev_unlock(ppriv->dev); 2062 } 2063 2064 ipoib_neigh_hash_uninit(dev); 2065 2066 ipoib_ib_dev_cleanup(dev); 2067 2068 /* no more works over the priv->wq */ 2069 if (priv->wq) { 2070 /* See ipoib_mcast_carrier_on_task() */ 2071 WARN_ON(test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)); 2072 destroy_workqueue(priv->wq); 2073 priv->wq = NULL; 2074 } 2075 2076 dev_put(priv->parent); 2077 } 2078 2079 static int ipoib_set_vf_link_state(struct net_device *dev, int vf, int link_state) 2080 { 2081 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2082 2083 return ib_set_vf_link_state(priv->ca, vf, priv->port, link_state); 2084 } 2085 2086 static int ipoib_get_vf_config(struct net_device *dev, int vf, 2087 struct ifla_vf_info *ivf) 2088 { 2089 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2090 int err; 2091 2092 err = ib_get_vf_config(priv->ca, vf, priv->port, ivf); 2093 if (err) 2094 return err; 2095 2096 ivf->vf = vf; 2097 memcpy(ivf->mac, dev->dev_addr, dev->addr_len); 2098 2099 return 0; 2100 } 2101 2102 static int ipoib_set_vf_guid(struct net_device *dev, int vf, u64 guid, int type) 2103 { 2104 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2105 2106 if (type != IFLA_VF_IB_NODE_GUID && type != IFLA_VF_IB_PORT_GUID) 2107 return -EINVAL; 2108 2109 return ib_set_vf_guid(priv->ca, vf, priv->port, guid, type); 2110 } 2111 2112 static int ipoib_get_vf_guid(struct net_device *dev, int vf, 2113 struct ifla_vf_guid *node_guid, 2114 struct ifla_vf_guid *port_guid) 2115 { 2116 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2117 2118 return ib_get_vf_guid(priv->ca, vf, priv->port, node_guid, port_guid); 2119 } 2120 2121 static int ipoib_get_vf_stats(struct net_device *dev, int vf, 2122 struct ifla_vf_stats *vf_stats) 2123 { 2124 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2125 2126 return ib_get_vf_stats(priv->ca, vf, priv->port, vf_stats); 2127 } 2128 2129 static const struct header_ops ipoib_header_ops = { 2130 .create = ipoib_hard_header, 2131 }; 2132 2133 static const struct net_device_ops ipoib_netdev_ops_pf = { 2134 .ndo_init = ipoib_ndo_init, 2135 .ndo_uninit = ipoib_ndo_uninit, 2136 .ndo_open = ipoib_open, 2137 .ndo_stop = ipoib_stop, 2138 .ndo_change_mtu = ipoib_change_mtu, 2139 .ndo_fix_features = ipoib_fix_features, 2140 .ndo_start_xmit = ipoib_start_xmit, 2141 .ndo_tx_timeout = ipoib_timeout, 2142 .ndo_set_rx_mode = ipoib_set_mcast_list, 2143 .ndo_get_iflink = ipoib_get_iflink, 2144 .ndo_set_vf_link_state = ipoib_set_vf_link_state, 2145 .ndo_get_vf_config = ipoib_get_vf_config, 2146 .ndo_get_vf_stats = ipoib_get_vf_stats, 2147 .ndo_get_vf_guid = ipoib_get_vf_guid, 2148 .ndo_set_vf_guid = ipoib_set_vf_guid, 2149 .ndo_set_mac_address = ipoib_set_mac, 2150 .ndo_get_stats64 = ipoib_get_stats, 2151 .ndo_eth_ioctl = ipoib_ioctl, 2152 }; 2153 2154 static const struct net_device_ops ipoib_netdev_ops_vf = { 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_get_stats64 = ipoib_get_stats, 2166 .ndo_eth_ioctl = ipoib_ioctl, 2167 }; 2168 2169 static const struct net_device_ops ipoib_netdev_default_pf = { 2170 .ndo_init = ipoib_dev_init_default, 2171 .ndo_uninit = ipoib_dev_uninit_default, 2172 .ndo_open = ipoib_ib_dev_open_default, 2173 .ndo_stop = ipoib_ib_dev_stop_default, 2174 }; 2175 2176 void ipoib_setup_common(struct net_device *dev) 2177 { 2178 dev->header_ops = &ipoib_header_ops; 2179 dev->netdev_ops = &ipoib_netdev_default_pf; 2180 2181 ipoib_set_ethtool_ops(dev); 2182 2183 dev->watchdog_timeo = 10 * HZ; 2184 2185 dev->flags |= IFF_BROADCAST | IFF_MULTICAST; 2186 2187 dev->hard_header_len = IPOIB_HARD_LEN; 2188 dev->addr_len = INFINIBAND_ALEN; 2189 dev->type = ARPHRD_INFINIBAND; 2190 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN; 2191 dev->features = (NETIF_F_VLAN_CHALLENGED | 2192 NETIF_F_HIGHDMA); 2193 netif_keep_dst(dev); 2194 2195 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN); 2196 2197 /* 2198 * unregister_netdev always frees the netdev, we use this mode 2199 * consistently to unify all the various unregister paths, including 2200 * those connected to rtnl_link_ops which require it. 2201 */ 2202 dev->needs_free_netdev = true; 2203 } 2204 2205 static void ipoib_build_priv(struct net_device *dev) 2206 { 2207 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2208 2209 priv->dev = dev; 2210 spin_lock_init(&priv->lock); 2211 mutex_init(&priv->mcast_mutex); 2212 2213 INIT_LIST_HEAD(&priv->path_list); 2214 INIT_LIST_HEAD(&priv->child_intfs); 2215 INIT_LIST_HEAD(&priv->dead_ahs); 2216 INIT_LIST_HEAD(&priv->multicast_list); 2217 2218 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task); 2219 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task); 2220 INIT_WORK(&priv->reschedule_napi_work, ipoib_napi_schedule_work); 2221 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light); 2222 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal); 2223 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy); 2224 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task); 2225 INIT_WORK(&priv->tx_timeout_work, ipoib_ib_tx_timeout_work); 2226 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah); 2227 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh); 2228 } 2229 2230 static struct net_device *ipoib_alloc_netdev(struct ib_device *hca, u32 port, 2231 const char *name) 2232 { 2233 struct net_device *dev; 2234 2235 dev = rdma_alloc_netdev(hca, port, RDMA_NETDEV_IPOIB, name, 2236 NET_NAME_UNKNOWN, ipoib_setup_common); 2237 if (!IS_ERR(dev) || PTR_ERR(dev) != -EOPNOTSUPP) 2238 return dev; 2239 2240 dev = alloc_netdev(sizeof(struct rdma_netdev), name, NET_NAME_UNKNOWN, 2241 ipoib_setup_common); 2242 if (!dev) 2243 return ERR_PTR(-ENOMEM); 2244 return dev; 2245 } 2246 2247 int ipoib_intf_init(struct ib_device *hca, u32 port, const char *name, 2248 struct net_device *dev) 2249 { 2250 struct rdma_netdev *rn = netdev_priv(dev); 2251 struct ipoib_dev_priv *priv; 2252 int rc; 2253 2254 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 2255 if (!priv) 2256 return -ENOMEM; 2257 2258 priv->ca = hca; 2259 priv->port = port; 2260 2261 rc = rdma_init_netdev(hca, port, RDMA_NETDEV_IPOIB, name, 2262 NET_NAME_UNKNOWN, ipoib_setup_common, dev); 2263 if (rc) { 2264 if (rc != -EOPNOTSUPP) 2265 goto out; 2266 2267 rn->send = ipoib_send; 2268 rn->attach_mcast = ipoib_mcast_attach; 2269 rn->detach_mcast = ipoib_mcast_detach; 2270 rn->hca = hca; 2271 2272 rc = netif_set_real_num_tx_queues(dev, 1); 2273 if (rc) 2274 goto out; 2275 2276 rc = netif_set_real_num_rx_queues(dev, 1); 2277 if (rc) 2278 goto out; 2279 } 2280 2281 priv->rn_ops = dev->netdev_ops; 2282 2283 if (hca->attrs.kernel_cap_flags & IBK_VIRTUAL_FUNCTION) 2284 dev->netdev_ops = &ipoib_netdev_ops_vf; 2285 else 2286 dev->netdev_ops = &ipoib_netdev_ops_pf; 2287 2288 rn->clnt_priv = priv; 2289 /* 2290 * Only the child register_netdev flows can handle priv_destructor 2291 * being set, so we force it to NULL here and handle manually until it 2292 * is safe to turn on. 2293 */ 2294 priv->next_priv_destructor = dev->priv_destructor; 2295 dev->priv_destructor = NULL; 2296 2297 ipoib_build_priv(dev); 2298 2299 return 0; 2300 2301 out: 2302 kfree(priv); 2303 return rc; 2304 } 2305 2306 struct net_device *ipoib_intf_alloc(struct ib_device *hca, u32 port, 2307 const char *name) 2308 { 2309 struct net_device *dev; 2310 int rc; 2311 2312 dev = ipoib_alloc_netdev(hca, port, name); 2313 if (IS_ERR(dev)) 2314 return dev; 2315 2316 rc = ipoib_intf_init(hca, port, name, dev); 2317 if (rc) { 2318 free_netdev(dev); 2319 return ERR_PTR(rc); 2320 } 2321 2322 /* 2323 * Upon success the caller must ensure ipoib_intf_free is called or 2324 * register_netdevice succeed'd and priv_destructor is set to 2325 * ipoib_intf_free. 2326 */ 2327 return dev; 2328 } 2329 2330 void ipoib_intf_free(struct net_device *dev) 2331 { 2332 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2333 struct rdma_netdev *rn = netdev_priv(dev); 2334 2335 dev->priv_destructor = priv->next_priv_destructor; 2336 if (dev->priv_destructor) 2337 dev->priv_destructor(dev); 2338 2339 /* 2340 * There are some error flows around register_netdev failing that may 2341 * attempt to call priv_destructor twice, prevent that from happening. 2342 */ 2343 dev->priv_destructor = NULL; 2344 2345 /* unregister/destroy is very complicated. Make bugs more obvious. */ 2346 rn->clnt_priv = NULL; 2347 2348 kfree(priv); 2349 } 2350 2351 static ssize_t pkey_show(struct device *dev, struct device_attribute *attr, 2352 char *buf) 2353 { 2354 struct net_device *ndev = to_net_dev(dev); 2355 struct ipoib_dev_priv *priv = ipoib_priv(ndev); 2356 2357 return sysfs_emit(buf, "0x%04x\n", priv->pkey); 2358 } 2359 static DEVICE_ATTR_RO(pkey); 2360 2361 static ssize_t umcast_show(struct device *dev, struct device_attribute *attr, 2362 char *buf) 2363 { 2364 struct net_device *ndev = to_net_dev(dev); 2365 struct ipoib_dev_priv *priv = ipoib_priv(ndev); 2366 2367 return sysfs_emit(buf, "%d\n", 2368 test_bit(IPOIB_FLAG_UMCAST, &priv->flags)); 2369 } 2370 2371 void ipoib_set_umcast(struct net_device *ndev, int umcast_val) 2372 { 2373 struct ipoib_dev_priv *priv = ipoib_priv(ndev); 2374 2375 if (umcast_val > 0) { 2376 set_bit(IPOIB_FLAG_UMCAST, &priv->flags); 2377 ipoib_warn(priv, "ignoring multicast groups joined directly " 2378 "by userspace\n"); 2379 } else 2380 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags); 2381 } 2382 2383 static ssize_t umcast_store(struct device *dev, struct device_attribute *attr, 2384 const char *buf, size_t count) 2385 { 2386 unsigned long umcast_val = simple_strtoul(buf, NULL, 0); 2387 2388 ipoib_set_umcast(to_net_dev(dev), umcast_val); 2389 2390 return count; 2391 } 2392 static DEVICE_ATTR_RW(umcast); 2393 2394 int ipoib_add_umcast_attr(struct net_device *dev) 2395 { 2396 return device_create_file(&dev->dev, &dev_attr_umcast); 2397 } 2398 2399 static void set_base_guid(struct ipoib_dev_priv *priv, union ib_gid *gid) 2400 { 2401 struct ipoib_dev_priv *child_priv; 2402 struct net_device *netdev = priv->dev; 2403 2404 netif_addr_lock_bh(netdev); 2405 2406 memcpy(&priv->local_gid.global.interface_id, 2407 &gid->global.interface_id, 2408 sizeof(gid->global.interface_id)); 2409 dev_addr_mod(netdev, 4, (u8 *)&priv->local_gid, sizeof(priv->local_gid)); 2410 clear_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags); 2411 2412 netif_addr_unlock_bh(netdev); 2413 2414 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) { 2415 netdev_lock_ops_to_full(priv->dev); 2416 list_for_each_entry(child_priv, &priv->child_intfs, list) 2417 set_base_guid(child_priv, gid); 2418 netdev_unlock_full_to_ops(priv->dev); 2419 } 2420 } 2421 2422 static int ipoib_check_lladdr(struct net_device *dev, 2423 struct sockaddr_storage *ss) 2424 { 2425 union ib_gid *gid = (union ib_gid *)(ss->__data + 4); 2426 int ret = 0; 2427 2428 netif_addr_lock_bh(dev); 2429 2430 /* Make sure the QPN, reserved and subnet prefix match the current 2431 * lladdr, it also makes sure the lladdr is unicast. 2432 */ 2433 if (memcmp(dev->dev_addr, ss->__data, 2434 4 + sizeof(gid->global.subnet_prefix)) || 2435 gid->global.interface_id == 0) 2436 ret = -EINVAL; 2437 2438 netif_addr_unlock_bh(dev); 2439 2440 return ret; 2441 } 2442 2443 static int ipoib_set_mac(struct net_device *dev, void *addr) 2444 { 2445 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2446 struct sockaddr_storage *ss = addr; 2447 int ret; 2448 2449 if (!(dev->priv_flags & IFF_LIVE_ADDR_CHANGE) && netif_running(dev)) 2450 return -EBUSY; 2451 2452 ret = ipoib_check_lladdr(dev, ss); 2453 if (ret) 2454 return ret; 2455 2456 set_base_guid(priv, (union ib_gid *)(ss->__data + 4)); 2457 2458 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) { 2459 struct ipoib_dev_priv *cpriv; 2460 2461 netdev_lock_ops_to_full(dev); 2462 list_for_each_entry(cpriv, &priv->child_intfs, list) 2463 queue_work(ipoib_workqueue, &cpriv->flush_light); 2464 netdev_unlock_full_to_ops(dev); 2465 } 2466 queue_work(ipoib_workqueue, &priv->flush_light); 2467 2468 return 0; 2469 } 2470 2471 static ssize_t create_child_store(struct device *dev, 2472 struct device_attribute *attr, 2473 const char *buf, size_t count) 2474 { 2475 int pkey; 2476 int ret; 2477 2478 if (sscanf(buf, "%i", &pkey) != 1) 2479 return -EINVAL; 2480 2481 if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000) 2482 return -EINVAL; 2483 2484 ret = ipoib_vlan_add(to_net_dev(dev), pkey); 2485 2486 return ret ? ret : count; 2487 } 2488 static DEVICE_ATTR_WO(create_child); 2489 2490 static ssize_t delete_child_store(struct device *dev, 2491 struct device_attribute *attr, 2492 const char *buf, size_t count) 2493 { 2494 int pkey; 2495 int ret; 2496 2497 if (sscanf(buf, "%i", &pkey) != 1) 2498 return -EINVAL; 2499 2500 if (pkey < 0 || pkey > 0xffff) 2501 return -EINVAL; 2502 2503 ret = ipoib_vlan_delete(to_net_dev(dev), pkey); 2504 2505 return ret ? ret : count; 2506 2507 } 2508 static DEVICE_ATTR_WO(delete_child); 2509 2510 int ipoib_add_pkey_attr(struct net_device *dev) 2511 { 2512 return device_create_file(&dev->dev, &dev_attr_pkey); 2513 } 2514 2515 /* 2516 * We erroneously exposed the iface's port number in the dev_id 2517 * sysfs field long after dev_port was introduced for that purpose[1], 2518 * and we need to stop everyone from relying on that. 2519 * Let's overload the shower routine for the dev_id file here 2520 * to gently bring the issue up. 2521 * 2522 * [1] https://www.spinics.net/lists/netdev/msg272123.html 2523 */ 2524 static ssize_t dev_id_show(struct device *dev, 2525 struct device_attribute *attr, char *buf) 2526 { 2527 struct net_device *ndev = to_net_dev(dev); 2528 2529 /* 2530 * ndev->dev_port will be equal to 0 in old kernel prior to commit 2531 * 9b8b2a323008 ("IB/ipoib: Use dev_port to expose network interface 2532 * port numbers") Zero was chosen as special case for user space 2533 * applications to fallback and query dev_id to check if it has 2534 * different value or not. 2535 * 2536 * Don't print warning in such scenario. 2537 * 2538 * https://github.com/systemd/systemd/blob/master/src/udev/udev-builtin-net_id.c#L358 2539 */ 2540 if (ndev->dev_port && ndev->dev_id == ndev->dev_port) 2541 netdev_info_once(ndev, 2542 "\"%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", 2543 current->comm); 2544 2545 return sysfs_emit(buf, "%#x\n", ndev->dev_id); 2546 } 2547 static DEVICE_ATTR_RO(dev_id); 2548 2549 static int ipoib_intercept_dev_id_attr(struct net_device *dev) 2550 { 2551 device_remove_file(&dev->dev, &dev_attr_dev_id); 2552 return device_create_file(&dev->dev, &dev_attr_dev_id); 2553 } 2554 2555 static struct net_device *ipoib_add_port(const char *format, 2556 struct ib_device *hca, u32 port) 2557 { 2558 struct rtnl_link_ops *ops = ipoib_get_link_ops(); 2559 struct rdma_netdev_alloc_params params; 2560 struct ipoib_dev_priv *priv; 2561 struct net_device *ndev; 2562 int result; 2563 2564 ndev = ipoib_intf_alloc(hca, port, format); 2565 if (IS_ERR(ndev)) { 2566 pr_warn("%s, %d: ipoib_intf_alloc failed %ld\n", hca->name, port, 2567 PTR_ERR(ndev)); 2568 return ndev; 2569 } 2570 priv = ipoib_priv(ndev); 2571 2572 INIT_IB_EVENT_HANDLER(&priv->event_handler, 2573 priv->ca, ipoib_event); 2574 ib_register_event_handler(&priv->event_handler); 2575 2576 /* call event handler to ensure pkey in sync */ 2577 ipoib_queue_work(priv, IPOIB_FLUSH_HEAVY); 2578 2579 ndev->rtnl_link_ops = ipoib_get_link_ops(); 2580 2581 dev_net_set(ndev, rdma_dev_net(hca)); 2582 2583 result = register_netdev(ndev); 2584 if (result) { 2585 pr_warn("%s: couldn't register ipoib port %d; error %d\n", 2586 hca->name, port, result); 2587 2588 ipoib_parent_unregister_pre(ndev); 2589 ipoib_intf_free(ndev); 2590 free_netdev(ndev); 2591 2592 return ERR_PTR(result); 2593 } 2594 2595 if (hca->ops.rdma_netdev_get_params) { 2596 int rc = hca->ops.rdma_netdev_get_params(hca, port, 2597 RDMA_NETDEV_IPOIB, 2598 ¶ms); 2599 2600 if (!rc && ops->priv_size < params.sizeof_priv) 2601 ops->priv_size = params.sizeof_priv; 2602 } 2603 /* 2604 * We cannot set priv_destructor before register_netdev because we 2605 * need priv to be always valid during the error flow to execute 2606 * ipoib_parent_unregister_pre(). Instead handle it manually and only 2607 * enter priv_destructor mode once we are completely registered. 2608 */ 2609 ndev->priv_destructor = ipoib_intf_free; 2610 2611 if (ipoib_intercept_dev_id_attr(ndev)) 2612 goto sysfs_failed; 2613 if (ipoib_cm_add_mode_attr(ndev)) 2614 goto sysfs_failed; 2615 if (ipoib_add_pkey_attr(ndev)) 2616 goto sysfs_failed; 2617 if (ipoib_add_umcast_attr(ndev)) 2618 goto sysfs_failed; 2619 if (device_create_file(&ndev->dev, &dev_attr_create_child)) 2620 goto sysfs_failed; 2621 if (device_create_file(&ndev->dev, &dev_attr_delete_child)) 2622 goto sysfs_failed; 2623 2624 return ndev; 2625 2626 sysfs_failed: 2627 ipoib_parent_unregister_pre(ndev); 2628 unregister_netdev(ndev); 2629 return ERR_PTR(-ENOMEM); 2630 } 2631 2632 static int ipoib_add_one(struct ib_device *device) 2633 { 2634 struct list_head *dev_list; 2635 struct net_device *dev; 2636 struct ipoib_dev_priv *priv; 2637 unsigned int p; 2638 int count = 0; 2639 2640 dev_list = kmalloc(sizeof(*dev_list), GFP_KERNEL); 2641 if (!dev_list) 2642 return -ENOMEM; 2643 2644 INIT_LIST_HEAD(dev_list); 2645 2646 rdma_for_each_port (device, p) { 2647 if (!rdma_protocol_ib(device, p)) 2648 continue; 2649 dev = ipoib_add_port("ib%d", device, p); 2650 if (!IS_ERR(dev)) { 2651 priv = ipoib_priv(dev); 2652 list_add_tail(&priv->list, dev_list); 2653 count++; 2654 } 2655 } 2656 2657 if (!count) { 2658 kfree(dev_list); 2659 return -EOPNOTSUPP; 2660 } 2661 2662 ib_set_client_data(device, &ipoib_client, dev_list); 2663 return 0; 2664 } 2665 2666 static void ipoib_remove_one(struct ib_device *device, void *client_data) 2667 { 2668 struct ipoib_dev_priv *priv, *tmp, *cpriv, *tcpriv; 2669 struct list_head *dev_list = client_data; 2670 2671 list_for_each_entry_safe(priv, tmp, dev_list, list) { 2672 LIST_HEAD(head); 2673 ipoib_parent_unregister_pre(priv->dev); 2674 2675 rtnl_lock(); 2676 2677 netdev_lock(priv->dev); 2678 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, 2679 list) 2680 unregister_netdevice_queue(cpriv->dev, &head); 2681 netdev_unlock(priv->dev); 2682 unregister_netdevice_queue(priv->dev, &head); 2683 unregister_netdevice_many(&head); 2684 2685 rtnl_unlock(); 2686 } 2687 2688 kfree(dev_list); 2689 } 2690 2691 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG 2692 static struct notifier_block ipoib_netdev_notifier = { 2693 .notifier_call = ipoib_netdev_event, 2694 }; 2695 #endif 2696 2697 static int __init ipoib_init_module(void) 2698 { 2699 int ret; 2700 2701 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size); 2702 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE); 2703 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE); 2704 2705 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size); 2706 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE); 2707 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE); 2708 #ifdef CONFIG_INFINIBAND_IPOIB_CM 2709 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP); 2710 ipoib_max_conn_qp = max(ipoib_max_conn_qp, 0); 2711 #endif 2712 2713 /* 2714 * When copying small received packets, we only copy from the 2715 * linear data part of the SKB, so we rely on this condition. 2716 */ 2717 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE); 2718 2719 ipoib_register_debugfs(); 2720 2721 /* 2722 * We create a global workqueue here that is used for all flush 2723 * operations. However, if you attempt to flush a workqueue 2724 * from a task on that same workqueue, it deadlocks the system. 2725 * We want to be able to flush the tasks associated with a 2726 * specific net device, so we also create a workqueue for each 2727 * netdevice. We queue up the tasks for that device only on 2728 * its private workqueue, and we only queue up flush events 2729 * on our global flush workqueue. This avoids the deadlocks. 2730 */ 2731 ipoib_workqueue = alloc_ordered_workqueue("ipoib_flush", 0); 2732 if (!ipoib_workqueue) { 2733 ret = -ENOMEM; 2734 goto err_fs; 2735 } 2736 2737 ib_sa_register_client(&ipoib_sa_client); 2738 2739 ret = ib_register_client(&ipoib_client); 2740 if (ret) 2741 goto err_sa; 2742 2743 ret = ipoib_netlink_init(); 2744 if (ret) 2745 goto err_client; 2746 2747 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG 2748 register_netdevice_notifier(&ipoib_netdev_notifier); 2749 #endif 2750 return 0; 2751 2752 err_client: 2753 ib_unregister_client(&ipoib_client); 2754 2755 err_sa: 2756 ib_sa_unregister_client(&ipoib_sa_client); 2757 destroy_workqueue(ipoib_workqueue); 2758 2759 err_fs: 2760 ipoib_unregister_debugfs(); 2761 2762 return ret; 2763 } 2764 2765 static void __exit ipoib_cleanup_module(void) 2766 { 2767 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG 2768 unregister_netdevice_notifier(&ipoib_netdev_notifier); 2769 #endif 2770 ipoib_netlink_fini(); 2771 ib_unregister_client(&ipoib_client); 2772 ib_sa_unregister_client(&ipoib_sa_client); 2773 ipoib_unregister_debugfs(); 2774 destroy_workqueue(ipoib_workqueue); 2775 } 2776 2777 module_init(ipoib_init_module); 2778 module_exit(ipoib_cleanup_module); 2779