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 <linux/inetdevice.h> 53 #include <rdma/ib_cache.h> 54 #include <linux/pci.h> 55 56 #define DRV_VERSION "1.0.0" 57 58 const char ipoib_driver_version[] = DRV_VERSION; 59 60 MODULE_AUTHOR("Roland Dreier"); 61 MODULE_DESCRIPTION("IP-over-InfiniBand net driver"); 62 MODULE_LICENSE("Dual BSD/GPL"); 63 64 int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE; 65 int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE; 66 67 module_param_named(send_queue_size, ipoib_sendq_size, int, 0444); 68 MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue"); 69 module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444); 70 MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue"); 71 72 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG 73 int ipoib_debug_level; 74 75 module_param_named(debug_level, ipoib_debug_level, int, 0644); 76 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0"); 77 #endif 78 79 struct ipoib_path_iter { 80 struct net_device *dev; 81 struct ipoib_path path; 82 }; 83 84 static const u8 ipv4_bcast_addr[] = { 85 0x00, 0xff, 0xff, 0xff, 86 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00, 87 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff 88 }; 89 90 struct workqueue_struct *ipoib_workqueue; 91 92 struct ib_sa_client ipoib_sa_client; 93 94 static void ipoib_add_one(struct ib_device *device); 95 static void ipoib_remove_one(struct ib_device *device, void *client_data); 96 static void ipoib_neigh_reclaim(struct rcu_head *rp); 97 static struct net_device *ipoib_get_net_dev_by_params( 98 struct ib_device *dev, u8 port, u16 pkey, 99 const union ib_gid *gid, const struct sockaddr *addr, 100 void *client_data); 101 static int ipoib_set_mac(struct net_device *dev, void *addr); 102 static int ipoib_ioctl(struct net_device *dev, struct ifreq *ifr, 103 int cmd); 104 105 static struct ib_client ipoib_client = { 106 .name = "ipoib", 107 .add = ipoib_add_one, 108 .remove = ipoib_remove_one, 109 .get_net_dev_by_params = ipoib_get_net_dev_by_params, 110 }; 111 112 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG 113 static int ipoib_netdev_event(struct notifier_block *this, 114 unsigned long event, void *ptr) 115 { 116 struct netdev_notifier_info *ni = ptr; 117 struct net_device *dev = ni->dev; 118 119 if (dev->netdev_ops->ndo_open != ipoib_open) 120 return NOTIFY_DONE; 121 122 switch (event) { 123 case NETDEV_REGISTER: 124 ipoib_create_debug_files(dev); 125 break; 126 case NETDEV_CHANGENAME: 127 ipoib_delete_debug_files(dev); 128 ipoib_create_debug_files(dev); 129 break; 130 case NETDEV_UNREGISTER: 131 ipoib_delete_debug_files(dev); 132 break; 133 } 134 135 return NOTIFY_DONE; 136 } 137 #endif 138 139 int ipoib_open(struct net_device *dev) 140 { 141 struct ipoib_dev_priv *priv = ipoib_priv(dev); 142 143 ipoib_dbg(priv, "bringing up interface\n"); 144 145 netif_carrier_off(dev); 146 147 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags); 148 149 priv->sm_fullmember_sendonly_support = false; 150 151 if (ipoib_ib_dev_open(dev)) { 152 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) 153 return 0; 154 goto err_disable; 155 } 156 157 ipoib_ib_dev_up(dev); 158 159 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) { 160 struct ipoib_dev_priv *cpriv; 161 162 /* Bring up any child interfaces too */ 163 down_read(&priv->vlan_rwsem); 164 list_for_each_entry(cpriv, &priv->child_intfs, list) { 165 int flags; 166 167 flags = cpriv->dev->flags; 168 if (flags & IFF_UP) 169 continue; 170 171 dev_change_flags(cpriv->dev, flags | IFF_UP); 172 } 173 up_read(&priv->vlan_rwsem); 174 } 175 176 netif_start_queue(dev); 177 178 return 0; 179 180 err_disable: 181 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags); 182 183 return -EINVAL; 184 } 185 186 static int ipoib_stop(struct net_device *dev) 187 { 188 struct ipoib_dev_priv *priv = ipoib_priv(dev); 189 190 ipoib_dbg(priv, "stopping interface\n"); 191 192 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags); 193 194 netif_stop_queue(dev); 195 196 ipoib_ib_dev_down(dev); 197 ipoib_ib_dev_stop(dev); 198 199 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) { 200 struct ipoib_dev_priv *cpriv; 201 202 /* Bring down any child interfaces too */ 203 down_read(&priv->vlan_rwsem); 204 list_for_each_entry(cpriv, &priv->child_intfs, list) { 205 int flags; 206 207 flags = cpriv->dev->flags; 208 if (!(flags & IFF_UP)) 209 continue; 210 211 dev_change_flags(cpriv->dev, flags & ~IFF_UP); 212 } 213 up_read(&priv->vlan_rwsem); 214 } 215 216 return 0; 217 } 218 219 static void ipoib_uninit(struct net_device *dev) 220 { 221 ipoib_dev_cleanup(dev); 222 } 223 224 static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features) 225 { 226 struct ipoib_dev_priv *priv = ipoib_priv(dev); 227 228 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags)) 229 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO); 230 231 return features; 232 } 233 234 static int ipoib_change_mtu(struct net_device *dev, int new_mtu) 235 { 236 struct ipoib_dev_priv *priv = ipoib_priv(dev); 237 int ret = 0; 238 239 /* dev->mtu > 2K ==> connected mode */ 240 if (ipoib_cm_admin_enabled(dev)) { 241 if (new_mtu > ipoib_cm_max_mtu(dev)) 242 return -EINVAL; 243 244 if (new_mtu > priv->mcast_mtu) 245 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n", 246 priv->mcast_mtu); 247 248 dev->mtu = new_mtu; 249 return 0; 250 } 251 252 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu)) 253 return -EINVAL; 254 255 priv->admin_mtu = new_mtu; 256 257 if (priv->mcast_mtu < priv->admin_mtu) 258 ipoib_dbg(priv, "MTU must be smaller than the underlying " 259 "link layer MTU - 4 (%u)\n", priv->mcast_mtu); 260 261 new_mtu = min(priv->mcast_mtu, priv->admin_mtu); 262 263 if (priv->rn_ops->ndo_change_mtu) { 264 bool carrier_status = netif_carrier_ok(dev); 265 266 netif_carrier_off(dev); 267 268 /* notify lower level on the real mtu */ 269 ret = priv->rn_ops->ndo_change_mtu(dev, new_mtu); 270 271 if (carrier_status) 272 netif_carrier_on(dev); 273 } else { 274 dev->mtu = new_mtu; 275 } 276 277 return ret; 278 } 279 280 static void ipoib_get_stats(struct net_device *dev, 281 struct rtnl_link_stats64 *stats) 282 { 283 struct ipoib_dev_priv *priv = ipoib_priv(dev); 284 285 if (priv->rn_ops->ndo_get_stats64) 286 priv->rn_ops->ndo_get_stats64(dev, stats); 287 else 288 netdev_stats_to_stats64(stats, &dev->stats); 289 } 290 291 /* Called with an RCU read lock taken */ 292 static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr, 293 struct net_device *dev) 294 { 295 struct net *net = dev_net(dev); 296 struct in_device *in_dev; 297 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr; 298 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr; 299 __be32 ret_addr; 300 301 switch (addr->sa_family) { 302 case AF_INET: 303 in_dev = in_dev_get(dev); 304 if (!in_dev) 305 return false; 306 307 ret_addr = inet_confirm_addr(net, in_dev, 0, 308 addr_in->sin_addr.s_addr, 309 RT_SCOPE_HOST); 310 in_dev_put(in_dev); 311 if (ret_addr) 312 return true; 313 314 break; 315 case AF_INET6: 316 if (IS_ENABLED(CONFIG_IPV6) && 317 ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1)) 318 return true; 319 320 break; 321 } 322 return false; 323 } 324 325 /** 326 * Find the master net_device on top of the given net_device. 327 * @dev: base IPoIB net_device 328 * 329 * Returns the master net_device with a reference held, or the same net_device 330 * if no master exists. 331 */ 332 static struct net_device *ipoib_get_master_net_dev(struct net_device *dev) 333 { 334 struct net_device *master; 335 336 rcu_read_lock(); 337 master = netdev_master_upper_dev_get_rcu(dev); 338 if (master) 339 dev_hold(master); 340 rcu_read_unlock(); 341 342 if (master) 343 return master; 344 345 dev_hold(dev); 346 return dev; 347 } 348 349 struct ipoib_walk_data { 350 const struct sockaddr *addr; 351 struct net_device *result; 352 }; 353 354 static int ipoib_upper_walk(struct net_device *upper, void *_data) 355 { 356 struct ipoib_walk_data *data = _data; 357 int ret = 0; 358 359 if (ipoib_is_dev_match_addr_rcu(data->addr, upper)) { 360 dev_hold(upper); 361 data->result = upper; 362 ret = 1; 363 } 364 365 return ret; 366 } 367 368 /** 369 * Find a net_device matching the given address, which is an upper device of 370 * the given net_device. 371 * @addr: IP address to look for. 372 * @dev: base IPoIB net_device 373 * 374 * If found, returns the net_device with a reference held. Otherwise return 375 * NULL. 376 */ 377 static struct net_device *ipoib_get_net_dev_match_addr( 378 const struct sockaddr *addr, struct net_device *dev) 379 { 380 struct ipoib_walk_data data = { 381 .addr = addr, 382 }; 383 384 rcu_read_lock(); 385 if (ipoib_is_dev_match_addr_rcu(addr, dev)) { 386 dev_hold(dev); 387 data.result = dev; 388 goto out; 389 } 390 391 netdev_walk_all_upper_dev_rcu(dev, ipoib_upper_walk, &data); 392 out: 393 rcu_read_unlock(); 394 return data.result; 395 } 396 397 /* returns the number of IPoIB netdevs on top a given ipoib device matching a 398 * pkey_index and address, if one exists. 399 * 400 * @found_net_dev: contains a matching net_device if the return value >= 1, 401 * with a reference held. */ 402 static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv, 403 const union ib_gid *gid, 404 u16 pkey_index, 405 const struct sockaddr *addr, 406 int nesting, 407 struct net_device **found_net_dev) 408 { 409 struct ipoib_dev_priv *child_priv; 410 struct net_device *net_dev = NULL; 411 int matches = 0; 412 413 if (priv->pkey_index == pkey_index && 414 (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) { 415 if (!addr) { 416 net_dev = ipoib_get_master_net_dev(priv->dev); 417 } else { 418 /* Verify the net_device matches the IP address, as 419 * IPoIB child devices currently share a GID. */ 420 net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev); 421 } 422 if (net_dev) { 423 if (!*found_net_dev) 424 *found_net_dev = net_dev; 425 else 426 dev_put(net_dev); 427 ++matches; 428 } 429 } 430 431 /* Check child interfaces */ 432 down_read_nested(&priv->vlan_rwsem, nesting); 433 list_for_each_entry(child_priv, &priv->child_intfs, list) { 434 matches += ipoib_match_gid_pkey_addr(child_priv, gid, 435 pkey_index, addr, 436 nesting + 1, 437 found_net_dev); 438 if (matches > 1) 439 break; 440 } 441 up_read(&priv->vlan_rwsem); 442 443 return matches; 444 } 445 446 /* Returns the number of matching net_devs found (between 0 and 2). Also 447 * return the matching net_device in the @net_dev parameter, holding a 448 * reference to the net_device, if the number of matches >= 1 */ 449 static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port, 450 u16 pkey_index, 451 const union ib_gid *gid, 452 const struct sockaddr *addr, 453 struct net_device **net_dev) 454 { 455 struct ipoib_dev_priv *priv; 456 int matches = 0; 457 458 *net_dev = NULL; 459 460 list_for_each_entry(priv, dev_list, list) { 461 if (priv->port != port) 462 continue; 463 464 matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index, 465 addr, 0, net_dev); 466 if (matches > 1) 467 break; 468 } 469 470 return matches; 471 } 472 473 static struct net_device *ipoib_get_net_dev_by_params( 474 struct ib_device *dev, u8 port, u16 pkey, 475 const union ib_gid *gid, const struct sockaddr *addr, 476 void *client_data) 477 { 478 struct net_device *net_dev; 479 struct list_head *dev_list = client_data; 480 u16 pkey_index; 481 int matches; 482 int ret; 483 484 if (!rdma_protocol_ib(dev, port)) 485 return NULL; 486 487 ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index); 488 if (ret) 489 return NULL; 490 491 if (!dev_list) 492 return NULL; 493 494 /* See if we can find a unique device matching the L2 parameters */ 495 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index, 496 gid, NULL, &net_dev); 497 498 switch (matches) { 499 case 0: 500 return NULL; 501 case 1: 502 return net_dev; 503 } 504 505 dev_put(net_dev); 506 507 /* Couldn't find a unique device with L2 parameters only. Use L3 508 * address to uniquely match the net device */ 509 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index, 510 gid, addr, &net_dev); 511 switch (matches) { 512 case 0: 513 return NULL; 514 default: 515 dev_warn_ratelimited(&dev->dev, 516 "duplicate IP address detected\n"); 517 /* Fall through */ 518 case 1: 519 return net_dev; 520 } 521 } 522 523 int ipoib_set_mode(struct net_device *dev, const char *buf) 524 { 525 struct ipoib_dev_priv *priv = ipoib_priv(dev); 526 527 if ((test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags) && 528 !strcmp(buf, "connected\n")) || 529 (!test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags) && 530 !strcmp(buf, "datagram\n"))) { 531 return 0; 532 } 533 534 /* flush paths if we switch modes so that connections are restarted */ 535 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) { 536 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags); 537 ipoib_warn(priv, "enabling connected mode " 538 "will cause multicast packet drops\n"); 539 netdev_update_features(dev); 540 dev_set_mtu(dev, ipoib_cm_max_mtu(dev)); 541 rtnl_unlock(); 542 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM; 543 544 ipoib_flush_paths(dev); 545 return (!rtnl_trylock()) ? -EBUSY : 0; 546 } 547 548 if (!strcmp(buf, "datagram\n")) { 549 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags); 550 netdev_update_features(dev); 551 dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu)); 552 rtnl_unlock(); 553 ipoib_flush_paths(dev); 554 return (!rtnl_trylock()) ? -EBUSY : 0; 555 } 556 557 return -EINVAL; 558 } 559 560 struct ipoib_path *__path_find(struct net_device *dev, void *gid) 561 { 562 struct ipoib_dev_priv *priv = ipoib_priv(dev); 563 struct rb_node *n = priv->path_tree.rb_node; 564 struct ipoib_path *path; 565 int ret; 566 567 while (n) { 568 path = rb_entry(n, struct ipoib_path, rb_node); 569 570 ret = memcmp(gid, path->pathrec.dgid.raw, 571 sizeof (union ib_gid)); 572 573 if (ret < 0) 574 n = n->rb_left; 575 else if (ret > 0) 576 n = n->rb_right; 577 else 578 return path; 579 } 580 581 return NULL; 582 } 583 584 static int __path_add(struct net_device *dev, struct ipoib_path *path) 585 { 586 struct ipoib_dev_priv *priv = ipoib_priv(dev); 587 struct rb_node **n = &priv->path_tree.rb_node; 588 struct rb_node *pn = NULL; 589 struct ipoib_path *tpath; 590 int ret; 591 592 while (*n) { 593 pn = *n; 594 tpath = rb_entry(pn, struct ipoib_path, rb_node); 595 596 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw, 597 sizeof (union ib_gid)); 598 if (ret < 0) 599 n = &pn->rb_left; 600 else if (ret > 0) 601 n = &pn->rb_right; 602 else 603 return -EEXIST; 604 } 605 606 rb_link_node(&path->rb_node, pn, n); 607 rb_insert_color(&path->rb_node, &priv->path_tree); 608 609 list_add_tail(&path->list, &priv->path_list); 610 611 return 0; 612 } 613 614 static void path_free(struct net_device *dev, struct ipoib_path *path) 615 { 616 struct sk_buff *skb; 617 618 while ((skb = __skb_dequeue(&path->queue))) 619 dev_kfree_skb_irq(skb); 620 621 ipoib_dbg(ipoib_priv(dev), "path_free\n"); 622 623 /* remove all neigh connected to this path */ 624 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw); 625 626 if (path->ah) 627 ipoib_put_ah(path->ah); 628 629 kfree(path); 630 } 631 632 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG 633 634 struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev) 635 { 636 struct ipoib_path_iter *iter; 637 638 iter = kmalloc(sizeof *iter, GFP_KERNEL); 639 if (!iter) 640 return NULL; 641 642 iter->dev = dev; 643 memset(iter->path.pathrec.dgid.raw, 0, 16); 644 645 if (ipoib_path_iter_next(iter)) { 646 kfree(iter); 647 return NULL; 648 } 649 650 return iter; 651 } 652 653 int ipoib_path_iter_next(struct ipoib_path_iter *iter) 654 { 655 struct ipoib_dev_priv *priv = ipoib_priv(iter->dev); 656 struct rb_node *n; 657 struct ipoib_path *path; 658 int ret = 1; 659 660 spin_lock_irq(&priv->lock); 661 662 n = rb_first(&priv->path_tree); 663 664 while (n) { 665 path = rb_entry(n, struct ipoib_path, rb_node); 666 667 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw, 668 sizeof (union ib_gid)) < 0) { 669 iter->path = *path; 670 ret = 0; 671 break; 672 } 673 674 n = rb_next(n); 675 } 676 677 spin_unlock_irq(&priv->lock); 678 679 return ret; 680 } 681 682 void ipoib_path_iter_read(struct ipoib_path_iter *iter, 683 struct ipoib_path *path) 684 { 685 *path = iter->path; 686 } 687 688 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */ 689 690 void ipoib_mark_paths_invalid(struct net_device *dev) 691 { 692 struct ipoib_dev_priv *priv = ipoib_priv(dev); 693 struct ipoib_path *path, *tp; 694 695 spin_lock_irq(&priv->lock); 696 697 list_for_each_entry_safe(path, tp, &priv->path_list, list) { 698 ipoib_dbg(priv, "mark path LID 0x%08x GID %pI6 invalid\n", 699 be32_to_cpu(sa_path_get_dlid(&path->pathrec)), 700 path->pathrec.dgid.raw); 701 path->valid = 0; 702 } 703 704 spin_unlock_irq(&priv->lock); 705 } 706 707 static void push_pseudo_header(struct sk_buff *skb, const char *daddr) 708 { 709 struct ipoib_pseudo_header *phdr; 710 711 phdr = skb_push(skb, sizeof(*phdr)); 712 memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN); 713 } 714 715 void ipoib_flush_paths(struct net_device *dev) 716 { 717 struct ipoib_dev_priv *priv = ipoib_priv(dev); 718 struct ipoib_path *path, *tp; 719 LIST_HEAD(remove_list); 720 unsigned long flags; 721 722 netif_tx_lock_bh(dev); 723 spin_lock_irqsave(&priv->lock, flags); 724 725 list_splice_init(&priv->path_list, &remove_list); 726 727 list_for_each_entry(path, &remove_list, list) 728 rb_erase(&path->rb_node, &priv->path_tree); 729 730 list_for_each_entry_safe(path, tp, &remove_list, list) { 731 if (path->query) 732 ib_sa_cancel_query(path->query_id, path->query); 733 spin_unlock_irqrestore(&priv->lock, flags); 734 netif_tx_unlock_bh(dev); 735 wait_for_completion(&path->done); 736 path_free(dev, path); 737 netif_tx_lock_bh(dev); 738 spin_lock_irqsave(&priv->lock, flags); 739 } 740 741 spin_unlock_irqrestore(&priv->lock, flags); 742 netif_tx_unlock_bh(dev); 743 } 744 745 static void path_rec_completion(int status, 746 struct sa_path_rec *pathrec, 747 void *path_ptr) 748 { 749 struct ipoib_path *path = path_ptr; 750 struct net_device *dev = path->dev; 751 struct ipoib_dev_priv *priv = ipoib_priv(dev); 752 struct ipoib_ah *ah = NULL; 753 struct ipoib_ah *old_ah = NULL; 754 struct ipoib_neigh *neigh, *tn; 755 struct sk_buff_head skqueue; 756 struct sk_buff *skb; 757 unsigned long flags; 758 759 if (!status) 760 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n", 761 be32_to_cpu(sa_path_get_dlid(pathrec)), 762 pathrec->dgid.raw); 763 else 764 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n", 765 status, path->pathrec.dgid.raw); 766 767 skb_queue_head_init(&skqueue); 768 769 if (!status) { 770 struct rdma_ah_attr av; 771 772 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av)) 773 ah = ipoib_create_ah(dev, priv->pd, &av); 774 } 775 776 spin_lock_irqsave(&priv->lock, flags); 777 778 if (!IS_ERR_OR_NULL(ah)) { 779 path->pathrec = *pathrec; 780 781 old_ah = path->ah; 782 path->ah = ah; 783 784 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n", 785 ah, be32_to_cpu(sa_path_get_dlid(pathrec)), 786 pathrec->sl); 787 788 while ((skb = __skb_dequeue(&path->queue))) 789 __skb_queue_tail(&skqueue, skb); 790 791 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) { 792 if (neigh->ah) { 793 WARN_ON(neigh->ah != old_ah); 794 /* 795 * Dropping the ah reference inside 796 * priv->lock is safe here, because we 797 * will hold one more reference from 798 * the original value of path->ah (ie 799 * old_ah). 800 */ 801 ipoib_put_ah(neigh->ah); 802 } 803 kref_get(&path->ah->ref); 804 neigh->ah = path->ah; 805 806 if (ipoib_cm_enabled(dev, neigh->daddr)) { 807 if (!ipoib_cm_get(neigh)) 808 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, 809 path, 810 neigh)); 811 if (!ipoib_cm_get(neigh)) { 812 ipoib_neigh_free(neigh); 813 continue; 814 } 815 } 816 817 while ((skb = __skb_dequeue(&neigh->queue))) 818 __skb_queue_tail(&skqueue, skb); 819 } 820 path->valid = 1; 821 } 822 823 path->query = NULL; 824 complete(&path->done); 825 826 spin_unlock_irqrestore(&priv->lock, flags); 827 828 if (IS_ERR_OR_NULL(ah)) 829 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw); 830 831 if (old_ah) 832 ipoib_put_ah(old_ah); 833 834 while ((skb = __skb_dequeue(&skqueue))) { 835 int ret; 836 skb->dev = dev; 837 ret = dev_queue_xmit(skb); 838 if (ret) 839 ipoib_warn(priv, "%s: dev_queue_xmit failed to re-queue packet, ret:%d\n", 840 __func__, ret); 841 } 842 } 843 844 static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid) 845 { 846 struct ipoib_dev_priv *priv = ipoib_priv(dev); 847 struct ipoib_path *path; 848 849 if (!priv->broadcast) 850 return NULL; 851 852 path = kzalloc(sizeof *path, GFP_ATOMIC); 853 if (!path) 854 return NULL; 855 856 path->dev = dev; 857 858 skb_queue_head_init(&path->queue); 859 860 INIT_LIST_HEAD(&path->neigh_list); 861 862 if (rdma_cap_opa_ah(priv->ca, priv->port)) 863 path->pathrec.rec_type = SA_PATH_REC_TYPE_OPA; 864 else 865 path->pathrec.rec_type = SA_PATH_REC_TYPE_IB; 866 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid)); 867 path->pathrec.sgid = priv->local_gid; 868 path->pathrec.pkey = cpu_to_be16(priv->pkey); 869 path->pathrec.numb_path = 1; 870 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class; 871 872 return path; 873 } 874 875 static int path_rec_start(struct net_device *dev, 876 struct ipoib_path *path) 877 { 878 struct ipoib_dev_priv *priv = ipoib_priv(dev); 879 880 ipoib_dbg(priv, "Start path record lookup for %pI6\n", 881 path->pathrec.dgid.raw); 882 883 init_completion(&path->done); 884 885 path->query_id = 886 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port, 887 &path->pathrec, 888 IB_SA_PATH_REC_DGID | 889 IB_SA_PATH_REC_SGID | 890 IB_SA_PATH_REC_NUMB_PATH | 891 IB_SA_PATH_REC_TRAFFIC_CLASS | 892 IB_SA_PATH_REC_PKEY, 893 1000, GFP_ATOMIC, 894 path_rec_completion, 895 path, &path->query); 896 if (path->query_id < 0) { 897 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id); 898 path->query = NULL; 899 complete(&path->done); 900 return path->query_id; 901 } 902 903 return 0; 904 } 905 906 static void neigh_add_path(struct sk_buff *skb, u8 *daddr, 907 struct net_device *dev) 908 { 909 struct ipoib_dev_priv *priv = ipoib_priv(dev); 910 struct rdma_netdev *rn = netdev_priv(dev); 911 struct ipoib_path *path; 912 struct ipoib_neigh *neigh; 913 unsigned long flags; 914 915 spin_lock_irqsave(&priv->lock, flags); 916 neigh = ipoib_neigh_alloc(daddr, dev); 917 if (!neigh) { 918 spin_unlock_irqrestore(&priv->lock, flags); 919 ++dev->stats.tx_dropped; 920 dev_kfree_skb_any(skb); 921 return; 922 } 923 924 path = __path_find(dev, daddr + 4); 925 if (!path) { 926 path = path_rec_create(dev, daddr + 4); 927 if (!path) 928 goto err_path; 929 930 __path_add(dev, path); 931 } 932 933 list_add_tail(&neigh->list, &path->neigh_list); 934 935 if (path->ah) { 936 kref_get(&path->ah->ref); 937 neigh->ah = path->ah; 938 939 if (ipoib_cm_enabled(dev, neigh->daddr)) { 940 if (!ipoib_cm_get(neigh)) 941 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh)); 942 if (!ipoib_cm_get(neigh)) { 943 ipoib_neigh_free(neigh); 944 goto err_drop; 945 } 946 if (skb_queue_len(&neigh->queue) < 947 IPOIB_MAX_PATH_REC_QUEUE) { 948 push_pseudo_header(skb, neigh->daddr); 949 __skb_queue_tail(&neigh->queue, skb); 950 } else { 951 ipoib_warn(priv, "queue length limit %d. Packet drop.\n", 952 skb_queue_len(&neigh->queue)); 953 goto err_drop; 954 } 955 } else { 956 spin_unlock_irqrestore(&priv->lock, flags); 957 path->ah->last_send = rn->send(dev, skb, path->ah->ah, 958 IPOIB_QPN(daddr)); 959 ipoib_neigh_put(neigh); 960 return; 961 } 962 } else { 963 neigh->ah = NULL; 964 965 if (!path->query && path_rec_start(dev, path)) 966 goto err_path; 967 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) { 968 push_pseudo_header(skb, neigh->daddr); 969 __skb_queue_tail(&neigh->queue, skb); 970 } else { 971 goto err_drop; 972 } 973 } 974 975 spin_unlock_irqrestore(&priv->lock, flags); 976 ipoib_neigh_put(neigh); 977 return; 978 979 err_path: 980 ipoib_neigh_free(neigh); 981 err_drop: 982 ++dev->stats.tx_dropped; 983 dev_kfree_skb_any(skb); 984 985 spin_unlock_irqrestore(&priv->lock, flags); 986 ipoib_neigh_put(neigh); 987 } 988 989 static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev, 990 struct ipoib_pseudo_header *phdr) 991 { 992 struct ipoib_dev_priv *priv = ipoib_priv(dev); 993 struct rdma_netdev *rn = netdev_priv(dev); 994 struct ipoib_path *path; 995 unsigned long flags; 996 997 spin_lock_irqsave(&priv->lock, flags); 998 999 path = __path_find(dev, phdr->hwaddr + 4); 1000 if (!path || !path->valid) { 1001 int new_path = 0; 1002 1003 if (!path) { 1004 path = path_rec_create(dev, phdr->hwaddr + 4); 1005 new_path = 1; 1006 } 1007 if (path) { 1008 if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) { 1009 push_pseudo_header(skb, phdr->hwaddr); 1010 __skb_queue_tail(&path->queue, skb); 1011 } else { 1012 ++dev->stats.tx_dropped; 1013 dev_kfree_skb_any(skb); 1014 } 1015 1016 if (!path->query && path_rec_start(dev, path)) { 1017 spin_unlock_irqrestore(&priv->lock, flags); 1018 if (new_path) 1019 path_free(dev, path); 1020 return; 1021 } else 1022 __path_add(dev, path); 1023 } else { 1024 ++dev->stats.tx_dropped; 1025 dev_kfree_skb_any(skb); 1026 } 1027 1028 spin_unlock_irqrestore(&priv->lock, flags); 1029 return; 1030 } 1031 1032 if (path->ah) { 1033 ipoib_dbg(priv, "Send unicast ARP to %08x\n", 1034 be32_to_cpu(sa_path_get_dlid(&path->pathrec))); 1035 1036 spin_unlock_irqrestore(&priv->lock, flags); 1037 path->ah->last_send = rn->send(dev, skb, path->ah->ah, 1038 IPOIB_QPN(phdr->hwaddr)); 1039 return; 1040 } else if ((path->query || !path_rec_start(dev, path)) && 1041 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) { 1042 push_pseudo_header(skb, phdr->hwaddr); 1043 __skb_queue_tail(&path->queue, skb); 1044 } else { 1045 ++dev->stats.tx_dropped; 1046 dev_kfree_skb_any(skb); 1047 } 1048 1049 spin_unlock_irqrestore(&priv->lock, flags); 1050 } 1051 1052 static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev) 1053 { 1054 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1055 struct rdma_netdev *rn = netdev_priv(dev); 1056 struct ipoib_neigh *neigh; 1057 struct ipoib_pseudo_header *phdr; 1058 struct ipoib_header *header; 1059 unsigned long flags; 1060 1061 phdr = (struct ipoib_pseudo_header *) skb->data; 1062 skb_pull(skb, sizeof(*phdr)); 1063 header = (struct ipoib_header *) skb->data; 1064 1065 if (unlikely(phdr->hwaddr[4] == 0xff)) { 1066 /* multicast, arrange "if" according to probability */ 1067 if ((header->proto != htons(ETH_P_IP)) && 1068 (header->proto != htons(ETH_P_IPV6)) && 1069 (header->proto != htons(ETH_P_ARP)) && 1070 (header->proto != htons(ETH_P_RARP)) && 1071 (header->proto != htons(ETH_P_TIPC))) { 1072 /* ethertype not supported by IPoIB */ 1073 ++dev->stats.tx_dropped; 1074 dev_kfree_skb_any(skb); 1075 return NETDEV_TX_OK; 1076 } 1077 /* Add in the P_Key for multicast*/ 1078 phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff; 1079 phdr->hwaddr[9] = priv->pkey & 0xff; 1080 1081 neigh = ipoib_neigh_get(dev, phdr->hwaddr); 1082 if (likely(neigh)) 1083 goto send_using_neigh; 1084 ipoib_mcast_send(dev, phdr->hwaddr, skb); 1085 return NETDEV_TX_OK; 1086 } 1087 1088 /* unicast, arrange "switch" according to probability */ 1089 switch (header->proto) { 1090 case htons(ETH_P_IP): 1091 case htons(ETH_P_IPV6): 1092 case htons(ETH_P_TIPC): 1093 neigh = ipoib_neigh_get(dev, phdr->hwaddr); 1094 if (unlikely(!neigh)) { 1095 neigh_add_path(skb, phdr->hwaddr, dev); 1096 return NETDEV_TX_OK; 1097 } 1098 break; 1099 case htons(ETH_P_ARP): 1100 case htons(ETH_P_RARP): 1101 /* for unicast ARP and RARP should always perform path find */ 1102 unicast_arp_send(skb, dev, phdr); 1103 return NETDEV_TX_OK; 1104 default: 1105 /* ethertype not supported by IPoIB */ 1106 ++dev->stats.tx_dropped; 1107 dev_kfree_skb_any(skb); 1108 return NETDEV_TX_OK; 1109 } 1110 1111 send_using_neigh: 1112 /* note we now hold a ref to neigh */ 1113 if (ipoib_cm_get(neigh)) { 1114 if (ipoib_cm_up(neigh)) { 1115 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh)); 1116 goto unref; 1117 } 1118 } else if (neigh->ah) { 1119 neigh->ah->last_send = rn->send(dev, skb, neigh->ah->ah, 1120 IPOIB_QPN(phdr->hwaddr)); 1121 goto unref; 1122 } 1123 1124 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) { 1125 push_pseudo_header(skb, phdr->hwaddr); 1126 spin_lock_irqsave(&priv->lock, flags); 1127 __skb_queue_tail(&neigh->queue, skb); 1128 spin_unlock_irqrestore(&priv->lock, flags); 1129 } else { 1130 ++dev->stats.tx_dropped; 1131 dev_kfree_skb_any(skb); 1132 } 1133 1134 unref: 1135 ipoib_neigh_put(neigh); 1136 1137 return NETDEV_TX_OK; 1138 } 1139 1140 static void ipoib_timeout(struct net_device *dev) 1141 { 1142 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1143 1144 ipoib_warn(priv, "transmit timeout: latency %d msecs\n", 1145 jiffies_to_msecs(jiffies - dev_trans_start(dev))); 1146 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n", 1147 netif_queue_stopped(dev), 1148 priv->tx_head, priv->tx_tail); 1149 /* XXX reset QP, etc. */ 1150 } 1151 1152 static int ipoib_hard_header(struct sk_buff *skb, 1153 struct net_device *dev, 1154 unsigned short type, 1155 const void *daddr, const void *saddr, unsigned len) 1156 { 1157 struct ipoib_header *header; 1158 1159 header = skb_push(skb, sizeof *header); 1160 1161 header->proto = htons(type); 1162 header->reserved = 0; 1163 1164 /* 1165 * we don't rely on dst_entry structure, always stuff the 1166 * destination address into skb hard header so we can figure out where 1167 * to send the packet later. 1168 */ 1169 push_pseudo_header(skb, daddr); 1170 1171 return IPOIB_HARD_LEN; 1172 } 1173 1174 static void ipoib_set_mcast_list(struct net_device *dev) 1175 { 1176 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1177 1178 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) { 1179 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set"); 1180 return; 1181 } 1182 1183 queue_work(priv->wq, &priv->restart_task); 1184 } 1185 1186 static int ipoib_get_iflink(const struct net_device *dev) 1187 { 1188 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1189 1190 /* parent interface */ 1191 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) 1192 return dev->ifindex; 1193 1194 /* child/vlan interface */ 1195 return priv->parent->ifindex; 1196 } 1197 1198 static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr) 1199 { 1200 /* 1201 * Use only the address parts that contributes to spreading 1202 * The subnet prefix is not used as one can not connect to 1203 * same remote port (GUID) using the same remote QPN via two 1204 * different subnets. 1205 */ 1206 /* qpn octets[1:4) & port GUID octets[12:20) */ 1207 u32 *d32 = (u32 *) daddr; 1208 u32 hv; 1209 1210 hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0); 1211 return hv & htbl->mask; 1212 } 1213 1214 struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr) 1215 { 1216 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1217 struct ipoib_neigh_table *ntbl = &priv->ntbl; 1218 struct ipoib_neigh_hash *htbl; 1219 struct ipoib_neigh *neigh = NULL; 1220 u32 hash_val; 1221 1222 rcu_read_lock_bh(); 1223 1224 htbl = rcu_dereference_bh(ntbl->htbl); 1225 1226 if (!htbl) 1227 goto out_unlock; 1228 1229 hash_val = ipoib_addr_hash(htbl, daddr); 1230 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]); 1231 neigh != NULL; 1232 neigh = rcu_dereference_bh(neigh->hnext)) { 1233 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) { 1234 /* found, take one ref on behalf of the caller */ 1235 if (!atomic_inc_not_zero(&neigh->refcnt)) { 1236 /* deleted */ 1237 neigh = NULL; 1238 goto out_unlock; 1239 } 1240 1241 if (likely(skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)) 1242 neigh->alive = jiffies; 1243 goto out_unlock; 1244 } 1245 } 1246 1247 out_unlock: 1248 rcu_read_unlock_bh(); 1249 return neigh; 1250 } 1251 1252 static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv) 1253 { 1254 struct ipoib_neigh_table *ntbl = &priv->ntbl; 1255 struct ipoib_neigh_hash *htbl; 1256 unsigned long neigh_obsolete; 1257 unsigned long dt; 1258 unsigned long flags; 1259 int i; 1260 LIST_HEAD(remove_list); 1261 1262 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags)) 1263 return; 1264 1265 spin_lock_irqsave(&priv->lock, flags); 1266 1267 htbl = rcu_dereference_protected(ntbl->htbl, 1268 lockdep_is_held(&priv->lock)); 1269 1270 if (!htbl) 1271 goto out_unlock; 1272 1273 /* neigh is obsolete if it was idle for two GC periods */ 1274 dt = 2 * arp_tbl.gc_interval; 1275 neigh_obsolete = jiffies - dt; 1276 /* handle possible race condition */ 1277 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags)) 1278 goto out_unlock; 1279 1280 for (i = 0; i < htbl->size; i++) { 1281 struct ipoib_neigh *neigh; 1282 struct ipoib_neigh __rcu **np = &htbl->buckets[i]; 1283 1284 while ((neigh = rcu_dereference_protected(*np, 1285 lockdep_is_held(&priv->lock))) != NULL) { 1286 /* was the neigh idle for two GC periods */ 1287 if (time_after(neigh_obsolete, neigh->alive)) { 1288 1289 ipoib_check_and_add_mcast_sendonly(priv, neigh->daddr + 4, &remove_list); 1290 1291 rcu_assign_pointer(*np, 1292 rcu_dereference_protected(neigh->hnext, 1293 lockdep_is_held(&priv->lock))); 1294 /* remove from path/mc list */ 1295 list_del_init(&neigh->list); 1296 call_rcu(&neigh->rcu, ipoib_neigh_reclaim); 1297 } else { 1298 np = &neigh->hnext; 1299 } 1300 1301 } 1302 } 1303 1304 out_unlock: 1305 spin_unlock_irqrestore(&priv->lock, flags); 1306 ipoib_mcast_remove_list(&remove_list); 1307 } 1308 1309 static void ipoib_reap_neigh(struct work_struct *work) 1310 { 1311 struct ipoib_dev_priv *priv = 1312 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work); 1313 1314 __ipoib_reap_neigh(priv); 1315 1316 if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags)) 1317 queue_delayed_work(priv->wq, &priv->neigh_reap_task, 1318 arp_tbl.gc_interval); 1319 } 1320 1321 1322 static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr, 1323 struct net_device *dev) 1324 { 1325 struct ipoib_neigh *neigh; 1326 1327 neigh = kzalloc(sizeof *neigh, GFP_ATOMIC); 1328 if (!neigh) 1329 return NULL; 1330 1331 neigh->dev = dev; 1332 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr)); 1333 skb_queue_head_init(&neigh->queue); 1334 INIT_LIST_HEAD(&neigh->list); 1335 ipoib_cm_set(neigh, NULL); 1336 /* one ref on behalf of the caller */ 1337 atomic_set(&neigh->refcnt, 1); 1338 1339 return neigh; 1340 } 1341 1342 struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr, 1343 struct net_device *dev) 1344 { 1345 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1346 struct ipoib_neigh_table *ntbl = &priv->ntbl; 1347 struct ipoib_neigh_hash *htbl; 1348 struct ipoib_neigh *neigh; 1349 u32 hash_val; 1350 1351 htbl = rcu_dereference_protected(ntbl->htbl, 1352 lockdep_is_held(&priv->lock)); 1353 if (!htbl) { 1354 neigh = NULL; 1355 goto out_unlock; 1356 } 1357 1358 /* need to add a new neigh, but maybe some other thread succeeded? 1359 * recalc hash, maybe hash resize took place so we do a search 1360 */ 1361 hash_val = ipoib_addr_hash(htbl, daddr); 1362 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val], 1363 lockdep_is_held(&priv->lock)); 1364 neigh != NULL; 1365 neigh = rcu_dereference_protected(neigh->hnext, 1366 lockdep_is_held(&priv->lock))) { 1367 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) { 1368 /* found, take one ref on behalf of the caller */ 1369 if (!atomic_inc_not_zero(&neigh->refcnt)) { 1370 /* deleted */ 1371 neigh = NULL; 1372 break; 1373 } 1374 neigh->alive = jiffies; 1375 goto out_unlock; 1376 } 1377 } 1378 1379 neigh = ipoib_neigh_ctor(daddr, dev); 1380 if (!neigh) 1381 goto out_unlock; 1382 1383 /* one ref on behalf of the hash table */ 1384 atomic_inc(&neigh->refcnt); 1385 neigh->alive = jiffies; 1386 /* put in hash */ 1387 rcu_assign_pointer(neigh->hnext, 1388 rcu_dereference_protected(htbl->buckets[hash_val], 1389 lockdep_is_held(&priv->lock))); 1390 rcu_assign_pointer(htbl->buckets[hash_val], neigh); 1391 atomic_inc(&ntbl->entries); 1392 1393 out_unlock: 1394 1395 return neigh; 1396 } 1397 1398 void ipoib_neigh_dtor(struct ipoib_neigh *neigh) 1399 { 1400 /* neigh reference count was dropprd to zero */ 1401 struct net_device *dev = neigh->dev; 1402 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1403 struct sk_buff *skb; 1404 if (neigh->ah) 1405 ipoib_put_ah(neigh->ah); 1406 while ((skb = __skb_dequeue(&neigh->queue))) { 1407 ++dev->stats.tx_dropped; 1408 dev_kfree_skb_any(skb); 1409 } 1410 if (ipoib_cm_get(neigh)) 1411 ipoib_cm_destroy_tx(ipoib_cm_get(neigh)); 1412 ipoib_dbg(ipoib_priv(dev), 1413 "neigh free for %06x %pI6\n", 1414 IPOIB_QPN(neigh->daddr), 1415 neigh->daddr + 4); 1416 kfree(neigh); 1417 if (atomic_dec_and_test(&priv->ntbl.entries)) { 1418 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags)) 1419 complete(&priv->ntbl.flushed); 1420 } 1421 } 1422 1423 static void ipoib_neigh_reclaim(struct rcu_head *rp) 1424 { 1425 /* Called as a result of removal from hash table */ 1426 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu); 1427 /* note TX context may hold another ref */ 1428 ipoib_neigh_put(neigh); 1429 } 1430 1431 void ipoib_neigh_free(struct ipoib_neigh *neigh) 1432 { 1433 struct net_device *dev = neigh->dev; 1434 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1435 struct ipoib_neigh_table *ntbl = &priv->ntbl; 1436 struct ipoib_neigh_hash *htbl; 1437 struct ipoib_neigh __rcu **np; 1438 struct ipoib_neigh *n; 1439 u32 hash_val; 1440 1441 htbl = rcu_dereference_protected(ntbl->htbl, 1442 lockdep_is_held(&priv->lock)); 1443 if (!htbl) 1444 return; 1445 1446 hash_val = ipoib_addr_hash(htbl, neigh->daddr); 1447 np = &htbl->buckets[hash_val]; 1448 for (n = rcu_dereference_protected(*np, 1449 lockdep_is_held(&priv->lock)); 1450 n != NULL; 1451 n = rcu_dereference_protected(*np, 1452 lockdep_is_held(&priv->lock))) { 1453 if (n == neigh) { 1454 /* found */ 1455 rcu_assign_pointer(*np, 1456 rcu_dereference_protected(neigh->hnext, 1457 lockdep_is_held(&priv->lock))); 1458 /* remove from parent list */ 1459 list_del_init(&neigh->list); 1460 call_rcu(&neigh->rcu, ipoib_neigh_reclaim); 1461 return; 1462 } else { 1463 np = &n->hnext; 1464 } 1465 } 1466 } 1467 1468 static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv) 1469 { 1470 struct ipoib_neigh_table *ntbl = &priv->ntbl; 1471 struct ipoib_neigh_hash *htbl; 1472 struct ipoib_neigh __rcu **buckets; 1473 u32 size; 1474 1475 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags); 1476 ntbl->htbl = NULL; 1477 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL); 1478 if (!htbl) 1479 return -ENOMEM; 1480 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags); 1481 size = roundup_pow_of_two(arp_tbl.gc_thresh3); 1482 buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL); 1483 if (!buckets) { 1484 kfree(htbl); 1485 return -ENOMEM; 1486 } 1487 htbl->size = size; 1488 htbl->mask = (size - 1); 1489 htbl->buckets = buckets; 1490 RCU_INIT_POINTER(ntbl->htbl, htbl); 1491 htbl->ntbl = ntbl; 1492 atomic_set(&ntbl->entries, 0); 1493 1494 /* start garbage collection */ 1495 clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags); 1496 queue_delayed_work(priv->wq, &priv->neigh_reap_task, 1497 arp_tbl.gc_interval); 1498 1499 return 0; 1500 } 1501 1502 static void neigh_hash_free_rcu(struct rcu_head *head) 1503 { 1504 struct ipoib_neigh_hash *htbl = container_of(head, 1505 struct ipoib_neigh_hash, 1506 rcu); 1507 struct ipoib_neigh __rcu **buckets = htbl->buckets; 1508 struct ipoib_neigh_table *ntbl = htbl->ntbl; 1509 1510 kfree(buckets); 1511 kfree(htbl); 1512 complete(&ntbl->deleted); 1513 } 1514 1515 void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid) 1516 { 1517 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1518 struct ipoib_neigh_table *ntbl = &priv->ntbl; 1519 struct ipoib_neigh_hash *htbl; 1520 unsigned long flags; 1521 int i; 1522 1523 /* remove all neigh connected to a given path or mcast */ 1524 spin_lock_irqsave(&priv->lock, flags); 1525 1526 htbl = rcu_dereference_protected(ntbl->htbl, 1527 lockdep_is_held(&priv->lock)); 1528 1529 if (!htbl) 1530 goto out_unlock; 1531 1532 for (i = 0; i < htbl->size; i++) { 1533 struct ipoib_neigh *neigh; 1534 struct ipoib_neigh __rcu **np = &htbl->buckets[i]; 1535 1536 while ((neigh = rcu_dereference_protected(*np, 1537 lockdep_is_held(&priv->lock))) != NULL) { 1538 /* delete neighs belong to this parent */ 1539 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) { 1540 rcu_assign_pointer(*np, 1541 rcu_dereference_protected(neigh->hnext, 1542 lockdep_is_held(&priv->lock))); 1543 /* remove from parent list */ 1544 list_del_init(&neigh->list); 1545 call_rcu(&neigh->rcu, ipoib_neigh_reclaim); 1546 } else { 1547 np = &neigh->hnext; 1548 } 1549 1550 } 1551 } 1552 out_unlock: 1553 spin_unlock_irqrestore(&priv->lock, flags); 1554 } 1555 1556 static void ipoib_flush_neighs(struct ipoib_dev_priv *priv) 1557 { 1558 struct ipoib_neigh_table *ntbl = &priv->ntbl; 1559 struct ipoib_neigh_hash *htbl; 1560 unsigned long flags; 1561 int i, wait_flushed = 0; 1562 1563 init_completion(&priv->ntbl.flushed); 1564 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags); 1565 1566 spin_lock_irqsave(&priv->lock, flags); 1567 1568 htbl = rcu_dereference_protected(ntbl->htbl, 1569 lockdep_is_held(&priv->lock)); 1570 if (!htbl) 1571 goto out_unlock; 1572 1573 wait_flushed = atomic_read(&priv->ntbl.entries); 1574 if (!wait_flushed) 1575 goto free_htbl; 1576 1577 for (i = 0; i < htbl->size; i++) { 1578 struct ipoib_neigh *neigh; 1579 struct ipoib_neigh __rcu **np = &htbl->buckets[i]; 1580 1581 while ((neigh = rcu_dereference_protected(*np, 1582 lockdep_is_held(&priv->lock))) != NULL) { 1583 rcu_assign_pointer(*np, 1584 rcu_dereference_protected(neigh->hnext, 1585 lockdep_is_held(&priv->lock))); 1586 /* remove from path/mc list */ 1587 list_del_init(&neigh->list); 1588 call_rcu(&neigh->rcu, ipoib_neigh_reclaim); 1589 } 1590 } 1591 1592 free_htbl: 1593 rcu_assign_pointer(ntbl->htbl, NULL); 1594 call_rcu(&htbl->rcu, neigh_hash_free_rcu); 1595 1596 out_unlock: 1597 spin_unlock_irqrestore(&priv->lock, flags); 1598 if (wait_flushed) 1599 wait_for_completion(&priv->ntbl.flushed); 1600 } 1601 1602 static void ipoib_neigh_hash_uninit(struct net_device *dev) 1603 { 1604 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1605 int stopped; 1606 1607 ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n"); 1608 init_completion(&priv->ntbl.deleted); 1609 1610 /* Stop GC if called at init fail need to cancel work */ 1611 stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags); 1612 if (!stopped) 1613 cancel_delayed_work(&priv->neigh_reap_task); 1614 1615 ipoib_flush_neighs(priv); 1616 1617 wait_for_completion(&priv->ntbl.deleted); 1618 } 1619 1620 static void ipoib_dev_uninit_default(struct net_device *dev) 1621 { 1622 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1623 1624 ipoib_transport_dev_cleanup(dev); 1625 1626 netif_napi_del(&priv->napi); 1627 1628 ipoib_cm_dev_cleanup(dev); 1629 1630 kfree(priv->rx_ring); 1631 vfree(priv->tx_ring); 1632 1633 priv->rx_ring = NULL; 1634 priv->tx_ring = NULL; 1635 } 1636 1637 static int ipoib_dev_init_default(struct net_device *dev) 1638 { 1639 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1640 1641 netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT); 1642 1643 /* Allocate RX/TX "rings" to hold queued skbs */ 1644 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring, 1645 GFP_KERNEL); 1646 if (!priv->rx_ring) 1647 goto out; 1648 1649 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring); 1650 if (!priv->tx_ring) { 1651 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n", 1652 priv->ca->name, ipoib_sendq_size); 1653 goto out_rx_ring_cleanup; 1654 } 1655 1656 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */ 1657 1658 if (ipoib_transport_dev_init(dev, priv->ca)) { 1659 pr_warn("%s: ipoib_transport_dev_init failed\n", 1660 priv->ca->name); 1661 goto out_tx_ring_cleanup; 1662 } 1663 1664 /* after qp created set dev address */ 1665 priv->dev->dev_addr[1] = (priv->qp->qp_num >> 16) & 0xff; 1666 priv->dev->dev_addr[2] = (priv->qp->qp_num >> 8) & 0xff; 1667 priv->dev->dev_addr[3] = (priv->qp->qp_num) & 0xff; 1668 1669 setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func, 1670 (unsigned long)dev); 1671 1672 return 0; 1673 1674 out_tx_ring_cleanup: 1675 vfree(priv->tx_ring); 1676 1677 out_rx_ring_cleanup: 1678 kfree(priv->rx_ring); 1679 1680 out: 1681 netif_napi_del(&priv->napi); 1682 return -ENOMEM; 1683 } 1684 1685 static int ipoib_ioctl(struct net_device *dev, struct ifreq *ifr, 1686 int cmd) 1687 { 1688 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1689 1690 if (!priv->rn_ops->ndo_do_ioctl) 1691 return -EOPNOTSUPP; 1692 1693 return priv->rn_ops->ndo_do_ioctl(dev, ifr, cmd); 1694 } 1695 1696 int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port) 1697 { 1698 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1699 int ret = -ENOMEM; 1700 1701 priv->ca = ca; 1702 priv->port = port; 1703 priv->qp = NULL; 1704 1705 /* 1706 * the various IPoIB tasks assume they will never race against 1707 * themselves, so always use a single thread workqueue 1708 */ 1709 priv->wq = alloc_ordered_workqueue("ipoib_wq", WQ_MEM_RECLAIM); 1710 if (!priv->wq) { 1711 pr_warn("%s: failed to allocate device WQ\n", dev->name); 1712 goto out; 1713 } 1714 1715 /* create pd, which used both for control and datapath*/ 1716 priv->pd = ib_alloc_pd(priv->ca, 0); 1717 if (IS_ERR(priv->pd)) { 1718 pr_warn("%s: failed to allocate PD\n", ca->name); 1719 goto clean_wq; 1720 } 1721 1722 ret = priv->rn_ops->ndo_init(dev); 1723 if (ret) { 1724 pr_warn("%s failed to init HW resource\n", dev->name); 1725 goto out_free_pd; 1726 } 1727 1728 if (ipoib_neigh_hash_init(priv) < 0) { 1729 pr_warn("%s failed to init neigh hash\n", dev->name); 1730 goto out_dev_uninit; 1731 } 1732 1733 if (dev->flags & IFF_UP) { 1734 if (ipoib_ib_dev_open(dev)) { 1735 pr_warn("%s failed to open device\n", dev->name); 1736 ret = -ENODEV; 1737 goto out_dev_uninit; 1738 } 1739 } 1740 1741 return 0; 1742 1743 out_dev_uninit: 1744 ipoib_ib_dev_cleanup(dev); 1745 1746 out_free_pd: 1747 if (priv->pd) { 1748 ib_dealloc_pd(priv->pd); 1749 priv->pd = NULL; 1750 } 1751 1752 clean_wq: 1753 if (priv->wq) { 1754 destroy_workqueue(priv->wq); 1755 priv->wq = NULL; 1756 } 1757 1758 out: 1759 return ret; 1760 } 1761 1762 void ipoib_dev_cleanup(struct net_device *dev) 1763 { 1764 struct ipoib_dev_priv *priv = ipoib_priv(dev), *cpriv, *tcpriv; 1765 LIST_HEAD(head); 1766 1767 ASSERT_RTNL(); 1768 1769 /* Delete any child interfaces first */ 1770 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) { 1771 /* Stop GC on child */ 1772 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags); 1773 cancel_delayed_work(&cpriv->neigh_reap_task); 1774 unregister_netdevice_queue(cpriv->dev, &head); 1775 } 1776 unregister_netdevice_many(&head); 1777 1778 ipoib_neigh_hash_uninit(dev); 1779 1780 ipoib_ib_dev_cleanup(dev); 1781 1782 /* no more works over the priv->wq */ 1783 if (priv->wq) { 1784 flush_workqueue(priv->wq); 1785 destroy_workqueue(priv->wq); 1786 priv->wq = NULL; 1787 } 1788 } 1789 1790 static int ipoib_set_vf_link_state(struct net_device *dev, int vf, int link_state) 1791 { 1792 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1793 1794 return ib_set_vf_link_state(priv->ca, vf, priv->port, link_state); 1795 } 1796 1797 static int ipoib_get_vf_config(struct net_device *dev, int vf, 1798 struct ifla_vf_info *ivf) 1799 { 1800 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1801 int err; 1802 1803 err = ib_get_vf_config(priv->ca, vf, priv->port, ivf); 1804 if (err) 1805 return err; 1806 1807 ivf->vf = vf; 1808 1809 return 0; 1810 } 1811 1812 static int ipoib_set_vf_guid(struct net_device *dev, int vf, u64 guid, int type) 1813 { 1814 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1815 1816 if (type != IFLA_VF_IB_NODE_GUID && type != IFLA_VF_IB_PORT_GUID) 1817 return -EINVAL; 1818 1819 return ib_set_vf_guid(priv->ca, vf, priv->port, guid, type); 1820 } 1821 1822 static int ipoib_get_vf_stats(struct net_device *dev, int vf, 1823 struct ifla_vf_stats *vf_stats) 1824 { 1825 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1826 1827 return ib_get_vf_stats(priv->ca, vf, priv->port, vf_stats); 1828 } 1829 1830 static const struct header_ops ipoib_header_ops = { 1831 .create = ipoib_hard_header, 1832 }; 1833 1834 static const struct net_device_ops ipoib_netdev_ops_pf = { 1835 .ndo_uninit = ipoib_uninit, 1836 .ndo_open = ipoib_open, 1837 .ndo_stop = ipoib_stop, 1838 .ndo_change_mtu = ipoib_change_mtu, 1839 .ndo_fix_features = ipoib_fix_features, 1840 .ndo_start_xmit = ipoib_start_xmit, 1841 .ndo_tx_timeout = ipoib_timeout, 1842 .ndo_set_rx_mode = ipoib_set_mcast_list, 1843 .ndo_get_iflink = ipoib_get_iflink, 1844 .ndo_set_vf_link_state = ipoib_set_vf_link_state, 1845 .ndo_get_vf_config = ipoib_get_vf_config, 1846 .ndo_get_vf_stats = ipoib_get_vf_stats, 1847 .ndo_set_vf_guid = ipoib_set_vf_guid, 1848 .ndo_set_mac_address = ipoib_set_mac, 1849 .ndo_get_stats64 = ipoib_get_stats, 1850 .ndo_do_ioctl = ipoib_ioctl, 1851 }; 1852 1853 static const struct net_device_ops ipoib_netdev_ops_vf = { 1854 .ndo_uninit = ipoib_uninit, 1855 .ndo_open = ipoib_open, 1856 .ndo_stop = ipoib_stop, 1857 .ndo_change_mtu = ipoib_change_mtu, 1858 .ndo_fix_features = ipoib_fix_features, 1859 .ndo_start_xmit = ipoib_start_xmit, 1860 .ndo_tx_timeout = ipoib_timeout, 1861 .ndo_set_rx_mode = ipoib_set_mcast_list, 1862 .ndo_get_iflink = ipoib_get_iflink, 1863 .ndo_get_stats64 = ipoib_get_stats, 1864 .ndo_do_ioctl = ipoib_ioctl, 1865 }; 1866 1867 void ipoib_setup_common(struct net_device *dev) 1868 { 1869 dev->header_ops = &ipoib_header_ops; 1870 1871 ipoib_set_ethtool_ops(dev); 1872 1873 dev->watchdog_timeo = HZ; 1874 1875 dev->flags |= IFF_BROADCAST | IFF_MULTICAST; 1876 1877 dev->hard_header_len = IPOIB_HARD_LEN; 1878 dev->addr_len = INFINIBAND_ALEN; 1879 dev->type = ARPHRD_INFINIBAND; 1880 dev->tx_queue_len = ipoib_sendq_size * 2; 1881 dev->features = (NETIF_F_VLAN_CHALLENGED | 1882 NETIF_F_HIGHDMA); 1883 netif_keep_dst(dev); 1884 1885 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN); 1886 } 1887 1888 static void ipoib_build_priv(struct net_device *dev) 1889 { 1890 struct ipoib_dev_priv *priv = ipoib_priv(dev); 1891 1892 priv->dev = dev; 1893 spin_lock_init(&priv->lock); 1894 init_rwsem(&priv->vlan_rwsem); 1895 mutex_init(&priv->mcast_mutex); 1896 mutex_init(&priv->sysfs_mutex); 1897 1898 INIT_LIST_HEAD(&priv->path_list); 1899 INIT_LIST_HEAD(&priv->child_intfs); 1900 INIT_LIST_HEAD(&priv->dead_ahs); 1901 INIT_LIST_HEAD(&priv->multicast_list); 1902 1903 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task); 1904 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task); 1905 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light); 1906 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal); 1907 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy); 1908 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task); 1909 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah); 1910 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh); 1911 } 1912 1913 static const struct net_device_ops ipoib_netdev_default_pf = { 1914 .ndo_init = ipoib_dev_init_default, 1915 .ndo_uninit = ipoib_dev_uninit_default, 1916 .ndo_open = ipoib_ib_dev_open_default, 1917 .ndo_stop = ipoib_ib_dev_stop_default, 1918 }; 1919 1920 static struct net_device 1921 *ipoib_create_netdev_default(struct ib_device *hca, 1922 const char *name, 1923 unsigned char name_assign_type, 1924 void (*setup)(struct net_device *)) 1925 { 1926 struct net_device *dev; 1927 struct rdma_netdev *rn; 1928 1929 dev = alloc_netdev((int)sizeof(struct rdma_netdev), 1930 name, 1931 name_assign_type, setup); 1932 if (!dev) 1933 return NULL; 1934 1935 rn = netdev_priv(dev); 1936 1937 rn->send = ipoib_send; 1938 rn->attach_mcast = ipoib_mcast_attach; 1939 rn->detach_mcast = ipoib_mcast_detach; 1940 rn->free_rdma_netdev = free_netdev; 1941 rn->hca = hca; 1942 1943 dev->netdev_ops = &ipoib_netdev_default_pf; 1944 1945 return dev; 1946 } 1947 1948 static struct net_device *ipoib_get_netdev(struct ib_device *hca, u8 port, 1949 const char *name) 1950 { 1951 struct net_device *dev; 1952 1953 if (hca->alloc_rdma_netdev) { 1954 dev = hca->alloc_rdma_netdev(hca, port, 1955 RDMA_NETDEV_IPOIB, name, 1956 NET_NAME_UNKNOWN, 1957 ipoib_setup_common); 1958 if (IS_ERR_OR_NULL(dev) && PTR_ERR(dev) != -EOPNOTSUPP) 1959 return NULL; 1960 } 1961 1962 if (!hca->alloc_rdma_netdev || PTR_ERR(dev) == -EOPNOTSUPP) 1963 dev = ipoib_create_netdev_default(hca, name, NET_NAME_UNKNOWN, 1964 ipoib_setup_common); 1965 1966 return dev; 1967 } 1968 1969 struct ipoib_dev_priv *ipoib_intf_alloc(struct ib_device *hca, u8 port, 1970 const char *name) 1971 { 1972 struct net_device *dev; 1973 struct ipoib_dev_priv *priv; 1974 struct rdma_netdev *rn; 1975 1976 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 1977 if (!priv) 1978 return NULL; 1979 1980 dev = ipoib_get_netdev(hca, port, name); 1981 if (!dev) 1982 goto free_priv; 1983 1984 priv->rn_ops = dev->netdev_ops; 1985 1986 /* fixme : should be after the query_cap */ 1987 if (priv->hca_caps & IB_DEVICE_VIRTUAL_FUNCTION) 1988 dev->netdev_ops = &ipoib_netdev_ops_vf; 1989 else 1990 dev->netdev_ops = &ipoib_netdev_ops_pf; 1991 1992 rn = netdev_priv(dev); 1993 rn->clnt_priv = priv; 1994 ipoib_build_priv(dev); 1995 1996 return priv; 1997 free_priv: 1998 kfree(priv); 1999 return NULL; 2000 } 2001 2002 static ssize_t show_pkey(struct device *dev, 2003 struct device_attribute *attr, char *buf) 2004 { 2005 struct net_device *ndev = to_net_dev(dev); 2006 struct ipoib_dev_priv *priv = ipoib_priv(ndev); 2007 2008 return sprintf(buf, "0x%04x\n", priv->pkey); 2009 } 2010 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL); 2011 2012 static ssize_t show_umcast(struct device *dev, 2013 struct device_attribute *attr, char *buf) 2014 { 2015 struct net_device *ndev = to_net_dev(dev); 2016 struct ipoib_dev_priv *priv = ipoib_priv(ndev); 2017 2018 return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags)); 2019 } 2020 2021 void ipoib_set_umcast(struct net_device *ndev, int umcast_val) 2022 { 2023 struct ipoib_dev_priv *priv = ipoib_priv(ndev); 2024 2025 if (umcast_val > 0) { 2026 set_bit(IPOIB_FLAG_UMCAST, &priv->flags); 2027 ipoib_warn(priv, "ignoring multicast groups joined directly " 2028 "by userspace\n"); 2029 } else 2030 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags); 2031 } 2032 2033 static ssize_t set_umcast(struct device *dev, 2034 struct device_attribute *attr, 2035 const char *buf, size_t count) 2036 { 2037 unsigned long umcast_val = simple_strtoul(buf, NULL, 0); 2038 2039 ipoib_set_umcast(to_net_dev(dev), umcast_val); 2040 2041 return count; 2042 } 2043 static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast); 2044 2045 int ipoib_add_umcast_attr(struct net_device *dev) 2046 { 2047 return device_create_file(&dev->dev, &dev_attr_umcast); 2048 } 2049 2050 static void set_base_guid(struct ipoib_dev_priv *priv, union ib_gid *gid) 2051 { 2052 struct ipoib_dev_priv *child_priv; 2053 struct net_device *netdev = priv->dev; 2054 2055 netif_addr_lock_bh(netdev); 2056 2057 memcpy(&priv->local_gid.global.interface_id, 2058 &gid->global.interface_id, 2059 sizeof(gid->global.interface_id)); 2060 memcpy(netdev->dev_addr + 4, &priv->local_gid, sizeof(priv->local_gid)); 2061 clear_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags); 2062 2063 netif_addr_unlock_bh(netdev); 2064 2065 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) { 2066 down_read(&priv->vlan_rwsem); 2067 list_for_each_entry(child_priv, &priv->child_intfs, list) 2068 set_base_guid(child_priv, gid); 2069 up_read(&priv->vlan_rwsem); 2070 } 2071 } 2072 2073 static int ipoib_check_lladdr(struct net_device *dev, 2074 struct sockaddr_storage *ss) 2075 { 2076 union ib_gid *gid = (union ib_gid *)(ss->__data + 4); 2077 int ret = 0; 2078 2079 netif_addr_lock_bh(dev); 2080 2081 /* Make sure the QPN, reserved and subnet prefix match the current 2082 * lladdr, it also makes sure the lladdr is unicast. 2083 */ 2084 if (memcmp(dev->dev_addr, ss->__data, 2085 4 + sizeof(gid->global.subnet_prefix)) || 2086 gid->global.interface_id == 0) 2087 ret = -EINVAL; 2088 2089 netif_addr_unlock_bh(dev); 2090 2091 return ret; 2092 } 2093 2094 static int ipoib_set_mac(struct net_device *dev, void *addr) 2095 { 2096 struct ipoib_dev_priv *priv = ipoib_priv(dev); 2097 struct sockaddr_storage *ss = addr; 2098 int ret; 2099 2100 if (!(dev->priv_flags & IFF_LIVE_ADDR_CHANGE) && netif_running(dev)) 2101 return -EBUSY; 2102 2103 ret = ipoib_check_lladdr(dev, ss); 2104 if (ret) 2105 return ret; 2106 2107 set_base_guid(priv, (union ib_gid *)(ss->__data + 4)); 2108 2109 queue_work(ipoib_workqueue, &priv->flush_light); 2110 2111 return 0; 2112 } 2113 2114 static ssize_t create_child(struct device *dev, 2115 struct device_attribute *attr, 2116 const char *buf, size_t count) 2117 { 2118 int pkey; 2119 int ret; 2120 2121 if (sscanf(buf, "%i", &pkey) != 1) 2122 return -EINVAL; 2123 2124 if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000) 2125 return -EINVAL; 2126 2127 /* 2128 * Set the full membership bit, so that we join the right 2129 * broadcast group, etc. 2130 */ 2131 pkey |= 0x8000; 2132 2133 ret = ipoib_vlan_add(to_net_dev(dev), pkey); 2134 2135 return ret ? ret : count; 2136 } 2137 static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child); 2138 2139 static ssize_t delete_child(struct device *dev, 2140 struct device_attribute *attr, 2141 const char *buf, size_t count) 2142 { 2143 int pkey; 2144 int ret; 2145 2146 if (sscanf(buf, "%i", &pkey) != 1) 2147 return -EINVAL; 2148 2149 if (pkey < 0 || pkey > 0xffff) 2150 return -EINVAL; 2151 2152 ret = ipoib_vlan_delete(to_net_dev(dev), pkey); 2153 2154 return ret ? ret : count; 2155 2156 } 2157 static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child); 2158 2159 int ipoib_add_pkey_attr(struct net_device *dev) 2160 { 2161 return device_create_file(&dev->dev, &dev_attr_pkey); 2162 } 2163 2164 void ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca) 2165 { 2166 priv->hca_caps = hca->attrs.device_cap_flags; 2167 2168 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) { 2169 priv->dev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM; 2170 2171 if (priv->hca_caps & IB_DEVICE_UD_TSO) 2172 priv->dev->hw_features |= NETIF_F_TSO; 2173 2174 priv->dev->features |= priv->dev->hw_features; 2175 } 2176 } 2177 2178 static struct net_device *ipoib_add_port(const char *format, 2179 struct ib_device *hca, u8 port) 2180 { 2181 struct ipoib_dev_priv *priv; 2182 struct ib_port_attr attr; 2183 struct rdma_netdev *rn; 2184 int result = -ENOMEM; 2185 2186 priv = ipoib_intf_alloc(hca, port, format); 2187 if (!priv) 2188 goto alloc_mem_failed; 2189 2190 SET_NETDEV_DEV(priv->dev, hca->dev.parent); 2191 priv->dev->dev_id = port - 1; 2192 2193 result = ib_query_port(hca, port, &attr); 2194 if (result) { 2195 printk(KERN_WARNING "%s: ib_query_port %d failed\n", 2196 hca->name, port); 2197 goto device_init_failed; 2198 } 2199 2200 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu); 2201 2202 /* MTU will be reset when mcast join happens */ 2203 priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu); 2204 priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu; 2205 priv->dev->max_mtu = IPOIB_CM_MTU; 2206 2207 priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh); 2208 2209 result = ib_query_pkey(hca, port, 0, &priv->pkey); 2210 if (result) { 2211 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n", 2212 hca->name, port, result); 2213 goto device_init_failed; 2214 } 2215 2216 ipoib_set_dev_features(priv, hca); 2217 2218 /* 2219 * Set the full membership bit, so that we join the right 2220 * broadcast group, etc. 2221 */ 2222 priv->pkey |= 0x8000; 2223 2224 priv->dev->broadcast[8] = priv->pkey >> 8; 2225 priv->dev->broadcast[9] = priv->pkey & 0xff; 2226 2227 result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL); 2228 if (result) { 2229 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n", 2230 hca->name, port, result); 2231 goto device_init_failed; 2232 } 2233 2234 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, 2235 sizeof(union ib_gid)); 2236 set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags); 2237 2238 result = ipoib_dev_init(priv->dev, hca, port); 2239 if (result) { 2240 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n", 2241 hca->name, port, result); 2242 goto device_init_failed; 2243 } 2244 2245 INIT_IB_EVENT_HANDLER(&priv->event_handler, 2246 priv->ca, ipoib_event); 2247 ib_register_event_handler(&priv->event_handler); 2248 2249 result = register_netdev(priv->dev); 2250 if (result) { 2251 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n", 2252 hca->name, port, result); 2253 goto register_failed; 2254 } 2255 2256 result = -ENOMEM; 2257 if (ipoib_cm_add_mode_attr(priv->dev)) 2258 goto sysfs_failed; 2259 if (ipoib_add_pkey_attr(priv->dev)) 2260 goto sysfs_failed; 2261 if (ipoib_add_umcast_attr(priv->dev)) 2262 goto sysfs_failed; 2263 if (device_create_file(&priv->dev->dev, &dev_attr_create_child)) 2264 goto sysfs_failed; 2265 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child)) 2266 goto sysfs_failed; 2267 2268 return priv->dev; 2269 2270 sysfs_failed: 2271 unregister_netdev(priv->dev); 2272 2273 register_failed: 2274 ib_unregister_event_handler(&priv->event_handler); 2275 flush_workqueue(ipoib_workqueue); 2276 /* Stop GC if started before flush */ 2277 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags); 2278 cancel_delayed_work(&priv->neigh_reap_task); 2279 flush_workqueue(priv->wq); 2280 ipoib_dev_cleanup(priv->dev); 2281 2282 device_init_failed: 2283 rn = netdev_priv(priv->dev); 2284 rn->free_rdma_netdev(priv->dev); 2285 kfree(priv); 2286 2287 alloc_mem_failed: 2288 return ERR_PTR(result); 2289 } 2290 2291 static void ipoib_add_one(struct ib_device *device) 2292 { 2293 struct list_head *dev_list; 2294 struct net_device *dev; 2295 struct ipoib_dev_priv *priv; 2296 int p; 2297 int count = 0; 2298 2299 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL); 2300 if (!dev_list) 2301 return; 2302 2303 INIT_LIST_HEAD(dev_list); 2304 2305 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) { 2306 if (!rdma_protocol_ib(device, p)) 2307 continue; 2308 dev = ipoib_add_port("ib%d", device, p); 2309 if (!IS_ERR(dev)) { 2310 priv = ipoib_priv(dev); 2311 list_add_tail(&priv->list, dev_list); 2312 count++; 2313 } 2314 } 2315 2316 if (!count) { 2317 kfree(dev_list); 2318 return; 2319 } 2320 2321 ib_set_client_data(device, &ipoib_client, dev_list); 2322 } 2323 2324 static void ipoib_remove_one(struct ib_device *device, void *client_data) 2325 { 2326 struct ipoib_dev_priv *priv, *tmp, *cpriv, *tcpriv; 2327 struct list_head *dev_list = client_data; 2328 2329 if (!dev_list) 2330 return; 2331 2332 list_for_each_entry_safe(priv, tmp, dev_list, list) { 2333 struct rdma_netdev *parent_rn = netdev_priv(priv->dev); 2334 2335 ib_unregister_event_handler(&priv->event_handler); 2336 flush_workqueue(ipoib_workqueue); 2337 2338 /* mark interface in the middle of destruction */ 2339 set_bit(IPOIB_FLAG_GOING_DOWN, &priv->flags); 2340 2341 rtnl_lock(); 2342 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP); 2343 rtnl_unlock(); 2344 2345 /* Stop GC */ 2346 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags); 2347 cancel_delayed_work(&priv->neigh_reap_task); 2348 flush_workqueue(priv->wq); 2349 2350 /* Wrap rtnl_lock/unlock with mutex to protect sysfs calls */ 2351 mutex_lock(&priv->sysfs_mutex); 2352 unregister_netdev(priv->dev); 2353 mutex_unlock(&priv->sysfs_mutex); 2354 2355 parent_rn->free_rdma_netdev(priv->dev); 2356 2357 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) { 2358 struct rdma_netdev *child_rn; 2359 2360 child_rn = netdev_priv(cpriv->dev); 2361 child_rn->free_rdma_netdev(cpriv->dev); 2362 kfree(cpriv); 2363 } 2364 2365 kfree(priv); 2366 } 2367 2368 kfree(dev_list); 2369 } 2370 2371 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG 2372 static struct notifier_block ipoib_netdev_notifier = { 2373 .notifier_call = ipoib_netdev_event, 2374 }; 2375 #endif 2376 2377 static int __init ipoib_init_module(void) 2378 { 2379 int ret; 2380 2381 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size); 2382 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE); 2383 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE); 2384 2385 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size); 2386 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE); 2387 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE); 2388 #ifdef CONFIG_INFINIBAND_IPOIB_CM 2389 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP); 2390 ipoib_max_conn_qp = max(ipoib_max_conn_qp, 0); 2391 #endif 2392 2393 /* 2394 * When copying small received packets, we only copy from the 2395 * linear data part of the SKB, so we rely on this condition. 2396 */ 2397 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE); 2398 2399 ret = ipoib_register_debugfs(); 2400 if (ret) 2401 return ret; 2402 2403 /* 2404 * We create a global workqueue here that is used for all flush 2405 * operations. However, if you attempt to flush a workqueue 2406 * from a task on that same workqueue, it deadlocks the system. 2407 * We want to be able to flush the tasks associated with a 2408 * specific net device, so we also create a workqueue for each 2409 * netdevice. We queue up the tasks for that device only on 2410 * its private workqueue, and we only queue up flush events 2411 * on our global flush workqueue. This avoids the deadlocks. 2412 */ 2413 ipoib_workqueue = alloc_ordered_workqueue("ipoib_flush", 2414 WQ_MEM_RECLAIM); 2415 if (!ipoib_workqueue) { 2416 ret = -ENOMEM; 2417 goto err_fs; 2418 } 2419 2420 ib_sa_register_client(&ipoib_sa_client); 2421 2422 ret = ib_register_client(&ipoib_client); 2423 if (ret) 2424 goto err_sa; 2425 2426 ret = ipoib_netlink_init(); 2427 if (ret) 2428 goto err_client; 2429 2430 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG 2431 register_netdevice_notifier(&ipoib_netdev_notifier); 2432 #endif 2433 return 0; 2434 2435 err_client: 2436 ib_unregister_client(&ipoib_client); 2437 2438 err_sa: 2439 ib_sa_unregister_client(&ipoib_sa_client); 2440 destroy_workqueue(ipoib_workqueue); 2441 2442 err_fs: 2443 ipoib_unregister_debugfs(); 2444 2445 return ret; 2446 } 2447 2448 static void __exit ipoib_cleanup_module(void) 2449 { 2450 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG 2451 unregister_netdevice_notifier(&ipoib_netdev_notifier); 2452 #endif 2453 ipoib_netlink_fini(); 2454 ib_unregister_client(&ipoib_client); 2455 ib_sa_unregister_client(&ipoib_sa_client); 2456 ipoib_unregister_debugfs(); 2457 destroy_workqueue(ipoib_workqueue); 2458 } 2459 2460 module_init(ipoib_init_module); 2461 module_exit(ipoib_cleanup_module); 2462