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.
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:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
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.
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
37 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/slab.h>
41 #include <linux/kernel.h>
42 #include <linux/vmalloc.h>
44 #include <linux/if_arp.h> /* For ARPHRD_xxx */
49 #include <linux/jhash.h>
51 #include <net/addrconf.h>
52 #include <linux/inetdevice.h>
53 #include <rdma/ib_cache.h>
55 #define DRV_VERSION "1.0.0"
57 const char ipoib_driver_version[] = DRV_VERSION;
59 MODULE_AUTHOR("Roland Dreier");
60 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
61 MODULE_LICENSE("Dual BSD/GPL");
62 MODULE_VERSION(DRV_VERSION);
64 int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
65 int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
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");
72 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
73 int ipoib_debug_level;
75 module_param_named(debug_level, ipoib_debug_level, int, 0644);
76 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
79 struct ipoib_path_iter {
80 struct net_device *dev;
81 struct ipoib_path path;
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
90 struct workqueue_struct *ipoib_workqueue;
92 struct ib_sa_client ipoib_sa_client;
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,
102 static struct ib_client ipoib_client = {
104 .add = ipoib_add_one,
105 .remove = ipoib_remove_one,
106 .get_net_dev_by_params = ipoib_get_net_dev_by_params,
109 int ipoib_open(struct net_device *dev)
111 struct ipoib_dev_priv *priv = netdev_priv(dev);
113 ipoib_dbg(priv, "bringing up interface\n");
115 netif_carrier_off(dev);
117 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
119 if (ipoib_ib_dev_open(dev)) {
120 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
125 if (ipoib_ib_dev_up(dev))
128 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
129 struct ipoib_dev_priv *cpriv;
131 /* Bring up any child interfaces too */
132 down_read(&priv->vlan_rwsem);
133 list_for_each_entry(cpriv, &priv->child_intfs, list) {
136 flags = cpriv->dev->flags;
140 dev_change_flags(cpriv->dev, flags | IFF_UP);
142 up_read(&priv->vlan_rwsem);
145 netif_start_queue(dev);
150 ipoib_ib_dev_stop(dev);
153 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
158 static int ipoib_stop(struct net_device *dev)
160 struct ipoib_dev_priv *priv = netdev_priv(dev);
162 ipoib_dbg(priv, "stopping interface\n");
164 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
166 netif_stop_queue(dev);
168 ipoib_ib_dev_down(dev);
169 ipoib_ib_dev_stop(dev);
171 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
172 struct ipoib_dev_priv *cpriv;
174 /* Bring down any child interfaces too */
175 down_read(&priv->vlan_rwsem);
176 list_for_each_entry(cpriv, &priv->child_intfs, list) {
179 flags = cpriv->dev->flags;
180 if (!(flags & IFF_UP))
183 dev_change_flags(cpriv->dev, flags & ~IFF_UP);
185 up_read(&priv->vlan_rwsem);
191 static void ipoib_uninit(struct net_device *dev)
193 ipoib_dev_cleanup(dev);
196 static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
198 struct ipoib_dev_priv *priv = netdev_priv(dev);
200 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
201 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
206 static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
208 struct ipoib_dev_priv *priv = netdev_priv(dev);
210 /* dev->mtu > 2K ==> connected mode */
211 if (ipoib_cm_admin_enabled(dev)) {
212 if (new_mtu > ipoib_cm_max_mtu(dev))
215 if (new_mtu > priv->mcast_mtu)
216 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
223 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
226 priv->admin_mtu = new_mtu;
228 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
233 /* Called with an RCU read lock taken */
234 static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr,
235 struct net_device *dev)
237 struct net *net = dev_net(dev);
238 struct in_device *in_dev;
239 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
240 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
243 switch (addr->sa_family) {
245 in_dev = in_dev_get(dev);
249 ret_addr = inet_confirm_addr(net, in_dev, 0,
250 addr_in->sin_addr.s_addr,
258 if (IS_ENABLED(CONFIG_IPV6) &&
259 ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1))
268 * Find the master net_device on top of the given net_device.
269 * @dev: base IPoIB net_device
271 * Returns the master net_device with a reference held, or the same net_device
272 * if no master exists.
274 static struct net_device *ipoib_get_master_net_dev(struct net_device *dev)
276 struct net_device *master;
279 master = netdev_master_upper_dev_get_rcu(dev);
292 * Find a net_device matching the given address, which is an upper device of
293 * the given net_device.
294 * @addr: IP address to look for.
295 * @dev: base IPoIB net_device
297 * If found, returns the net_device with a reference held. Otherwise return
300 static struct net_device *ipoib_get_net_dev_match_addr(
301 const struct sockaddr *addr, struct net_device *dev)
303 struct net_device *upper,
305 struct list_head *iter;
308 if (ipoib_is_dev_match_addr_rcu(addr, dev)) {
314 netdev_for_each_all_upper_dev_rcu(dev, upper, iter) {
315 if (ipoib_is_dev_match_addr_rcu(addr, upper)) {
326 /* returns the number of IPoIB netdevs on top a given ipoib device matching a
327 * pkey_index and address, if one exists.
329 * @found_net_dev: contains a matching net_device if the return value >= 1,
330 * with a reference held. */
331 static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
332 const union ib_gid *gid,
334 const struct sockaddr *addr,
336 struct net_device **found_net_dev)
338 struct ipoib_dev_priv *child_priv;
339 struct net_device *net_dev = NULL;
342 if (priv->pkey_index == pkey_index &&
343 (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
345 net_dev = ipoib_get_master_net_dev(priv->dev);
347 /* Verify the net_device matches the IP address, as
348 * IPoIB child devices currently share a GID. */
349 net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev);
353 *found_net_dev = net_dev;
360 /* Check child interfaces */
361 down_read_nested(&priv->vlan_rwsem, nesting);
362 list_for_each_entry(child_priv, &priv->child_intfs, list) {
363 matches += ipoib_match_gid_pkey_addr(child_priv, gid,
370 up_read(&priv->vlan_rwsem);
375 /* Returns the number of matching net_devs found (between 0 and 2). Also
376 * return the matching net_device in the @net_dev parameter, holding a
377 * reference to the net_device, if the number of matches >= 1 */
378 static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port,
380 const union ib_gid *gid,
381 const struct sockaddr *addr,
382 struct net_device **net_dev)
384 struct ipoib_dev_priv *priv;
389 list_for_each_entry(priv, dev_list, list) {
390 if (priv->port != port)
393 matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
402 static struct net_device *ipoib_get_net_dev_by_params(
403 struct ib_device *dev, u8 port, u16 pkey,
404 const union ib_gid *gid, const struct sockaddr *addr,
407 struct net_device *net_dev;
408 struct list_head *dev_list = client_data;
413 if (!rdma_protocol_ib(dev, port))
416 ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
423 /* See if we can find a unique device matching the L2 parameters */
424 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
425 gid, NULL, &net_dev);
436 /* Couldn't find a unique device with L2 parameters only. Use L3
437 * address to uniquely match the net device */
438 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
439 gid, addr, &net_dev);
444 dev_warn_ratelimited(&dev->dev,
445 "duplicate IP address detected\n");
452 int ipoib_set_mode(struct net_device *dev, const char *buf)
454 struct ipoib_dev_priv *priv = netdev_priv(dev);
456 /* flush paths if we switch modes so that connections are restarted */
457 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
458 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
459 ipoib_warn(priv, "enabling connected mode "
460 "will cause multicast packet drops\n");
461 netdev_update_features(dev);
462 dev_set_mtu(dev, ipoib_cm_max_mtu(dev));
464 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
466 ipoib_flush_paths(dev);
471 if (!strcmp(buf, "datagram\n")) {
472 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
473 netdev_update_features(dev);
474 dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
476 ipoib_flush_paths(dev);
484 struct ipoib_path *__path_find(struct net_device *dev, void *gid)
486 struct ipoib_dev_priv *priv = netdev_priv(dev);
487 struct rb_node *n = priv->path_tree.rb_node;
488 struct ipoib_path *path;
492 path = rb_entry(n, struct ipoib_path, rb_node);
494 ret = memcmp(gid, path->pathrec.dgid.raw,
495 sizeof (union ib_gid));
508 static int __path_add(struct net_device *dev, struct ipoib_path *path)
510 struct ipoib_dev_priv *priv = netdev_priv(dev);
511 struct rb_node **n = &priv->path_tree.rb_node;
512 struct rb_node *pn = NULL;
513 struct ipoib_path *tpath;
518 tpath = rb_entry(pn, struct ipoib_path, rb_node);
520 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
521 sizeof (union ib_gid));
530 rb_link_node(&path->rb_node, pn, n);
531 rb_insert_color(&path->rb_node, &priv->path_tree);
533 list_add_tail(&path->list, &priv->path_list);
538 static void path_free(struct net_device *dev, struct ipoib_path *path)
542 while ((skb = __skb_dequeue(&path->queue)))
543 dev_kfree_skb_irq(skb);
545 ipoib_dbg(netdev_priv(dev), "path_free\n");
547 /* remove all neigh connected to this path */
548 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
551 ipoib_put_ah(path->ah);
556 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
558 struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
560 struct ipoib_path_iter *iter;
562 iter = kmalloc(sizeof *iter, GFP_KERNEL);
567 memset(iter->path.pathrec.dgid.raw, 0, 16);
569 if (ipoib_path_iter_next(iter)) {
577 int ipoib_path_iter_next(struct ipoib_path_iter *iter)
579 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
581 struct ipoib_path *path;
584 spin_lock_irq(&priv->lock);
586 n = rb_first(&priv->path_tree);
589 path = rb_entry(n, struct ipoib_path, rb_node);
591 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
592 sizeof (union ib_gid)) < 0) {
601 spin_unlock_irq(&priv->lock);
606 void ipoib_path_iter_read(struct ipoib_path_iter *iter,
607 struct ipoib_path *path)
612 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
614 void ipoib_mark_paths_invalid(struct net_device *dev)
616 struct ipoib_dev_priv *priv = netdev_priv(dev);
617 struct ipoib_path *path, *tp;
619 spin_lock_irq(&priv->lock);
621 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
622 ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
623 be16_to_cpu(path->pathrec.dlid),
624 path->pathrec.dgid.raw);
628 spin_unlock_irq(&priv->lock);
631 void ipoib_flush_paths(struct net_device *dev)
633 struct ipoib_dev_priv *priv = netdev_priv(dev);
634 struct ipoib_path *path, *tp;
635 LIST_HEAD(remove_list);
638 netif_tx_lock_bh(dev);
639 spin_lock_irqsave(&priv->lock, flags);
641 list_splice_init(&priv->path_list, &remove_list);
643 list_for_each_entry(path, &remove_list, list)
644 rb_erase(&path->rb_node, &priv->path_tree);
646 list_for_each_entry_safe(path, tp, &remove_list, list) {
648 ib_sa_cancel_query(path->query_id, path->query);
649 spin_unlock_irqrestore(&priv->lock, flags);
650 netif_tx_unlock_bh(dev);
651 wait_for_completion(&path->done);
652 path_free(dev, path);
653 netif_tx_lock_bh(dev);
654 spin_lock_irqsave(&priv->lock, flags);
657 spin_unlock_irqrestore(&priv->lock, flags);
658 netif_tx_unlock_bh(dev);
661 static void path_rec_completion(int status,
662 struct ib_sa_path_rec *pathrec,
665 struct ipoib_path *path = path_ptr;
666 struct net_device *dev = path->dev;
667 struct ipoib_dev_priv *priv = netdev_priv(dev);
668 struct ipoib_ah *ah = NULL;
669 struct ipoib_ah *old_ah = NULL;
670 struct ipoib_neigh *neigh, *tn;
671 struct sk_buff_head skqueue;
676 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
677 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
679 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
680 status, path->pathrec.dgid.raw);
682 skb_queue_head_init(&skqueue);
685 struct ib_ah_attr av;
687 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
688 ah = ipoib_create_ah(dev, priv->pd, &av);
691 spin_lock_irqsave(&priv->lock, flags);
693 if (!IS_ERR_OR_NULL(ah)) {
694 path->pathrec = *pathrec;
699 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
700 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
702 while ((skb = __skb_dequeue(&path->queue)))
703 __skb_queue_tail(&skqueue, skb);
705 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
707 WARN_ON(neigh->ah != old_ah);
709 * Dropping the ah reference inside
710 * priv->lock is safe here, because we
711 * will hold one more reference from
712 * the original value of path->ah (ie
715 ipoib_put_ah(neigh->ah);
717 kref_get(&path->ah->ref);
718 neigh->ah = path->ah;
720 if (ipoib_cm_enabled(dev, neigh->daddr)) {
721 if (!ipoib_cm_get(neigh))
722 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
725 if (!ipoib_cm_get(neigh)) {
726 ipoib_neigh_free(neigh);
731 while ((skb = __skb_dequeue(&neigh->queue)))
732 __skb_queue_tail(&skqueue, skb);
738 complete(&path->done);
740 spin_unlock_irqrestore(&priv->lock, flags);
742 if (IS_ERR_OR_NULL(ah))
743 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
746 ipoib_put_ah(old_ah);
748 while ((skb = __skb_dequeue(&skqueue))) {
750 if (dev_queue_xmit(skb))
751 ipoib_warn(priv, "dev_queue_xmit failed "
752 "to requeue packet\n");
756 static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
758 struct ipoib_dev_priv *priv = netdev_priv(dev);
759 struct ipoib_path *path;
761 if (!priv->broadcast)
764 path = kzalloc(sizeof *path, GFP_ATOMIC);
770 skb_queue_head_init(&path->queue);
772 INIT_LIST_HEAD(&path->neigh_list);
774 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
775 path->pathrec.sgid = priv->local_gid;
776 path->pathrec.pkey = cpu_to_be16(priv->pkey);
777 path->pathrec.numb_path = 1;
778 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
783 static int path_rec_start(struct net_device *dev,
784 struct ipoib_path *path)
786 struct ipoib_dev_priv *priv = netdev_priv(dev);
788 ipoib_dbg(priv, "Start path record lookup for %pI6\n",
789 path->pathrec.dgid.raw);
791 init_completion(&path->done);
794 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
796 IB_SA_PATH_REC_DGID |
797 IB_SA_PATH_REC_SGID |
798 IB_SA_PATH_REC_NUMB_PATH |
799 IB_SA_PATH_REC_TRAFFIC_CLASS |
804 if (path->query_id < 0) {
805 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
807 complete(&path->done);
808 return path->query_id;
814 static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
815 struct net_device *dev)
817 struct ipoib_dev_priv *priv = netdev_priv(dev);
818 struct ipoib_path *path;
819 struct ipoib_neigh *neigh;
822 spin_lock_irqsave(&priv->lock, flags);
823 neigh = ipoib_neigh_alloc(daddr, dev);
825 spin_unlock_irqrestore(&priv->lock, flags);
826 ++dev->stats.tx_dropped;
827 dev_kfree_skb_any(skb);
831 path = __path_find(dev, daddr + 4);
833 path = path_rec_create(dev, daddr + 4);
837 __path_add(dev, path);
840 list_add_tail(&neigh->list, &path->neigh_list);
843 kref_get(&path->ah->ref);
844 neigh->ah = path->ah;
846 if (ipoib_cm_enabled(dev, neigh->daddr)) {
847 if (!ipoib_cm_get(neigh))
848 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
849 if (!ipoib_cm_get(neigh)) {
850 ipoib_neigh_free(neigh);
853 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
854 __skb_queue_tail(&neigh->queue, skb);
856 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
857 skb_queue_len(&neigh->queue));
861 spin_unlock_irqrestore(&priv->lock, flags);
862 ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
863 ipoib_neigh_put(neigh);
869 if (!path->query && path_rec_start(dev, path))
871 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
872 __skb_queue_tail(&neigh->queue, skb);
877 spin_unlock_irqrestore(&priv->lock, flags);
878 ipoib_neigh_put(neigh);
882 ipoib_neigh_free(neigh);
884 ++dev->stats.tx_dropped;
885 dev_kfree_skb_any(skb);
887 spin_unlock_irqrestore(&priv->lock, flags);
888 ipoib_neigh_put(neigh);
891 static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
894 struct ipoib_dev_priv *priv = netdev_priv(dev);
895 struct ipoib_path *path;
898 spin_lock_irqsave(&priv->lock, flags);
900 path = __path_find(dev, cb->hwaddr + 4);
901 if (!path || !path->valid) {
905 path = path_rec_create(dev, cb->hwaddr + 4);
909 if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
910 __skb_queue_tail(&path->queue, skb);
912 ++dev->stats.tx_dropped;
913 dev_kfree_skb_any(skb);
916 if (!path->query && path_rec_start(dev, path)) {
917 spin_unlock_irqrestore(&priv->lock, flags);
919 path_free(dev, path);
922 __path_add(dev, path);
924 ++dev->stats.tx_dropped;
925 dev_kfree_skb_any(skb);
928 spin_unlock_irqrestore(&priv->lock, flags);
933 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
934 be16_to_cpu(path->pathrec.dlid));
936 spin_unlock_irqrestore(&priv->lock, flags);
937 ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr));
939 } else if ((path->query || !path_rec_start(dev, path)) &&
940 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
941 __skb_queue_tail(&path->queue, skb);
943 ++dev->stats.tx_dropped;
944 dev_kfree_skb_any(skb);
947 spin_unlock_irqrestore(&priv->lock, flags);
950 static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
952 struct ipoib_dev_priv *priv = netdev_priv(dev);
953 struct ipoib_neigh *neigh;
954 struct ipoib_cb *cb = ipoib_skb_cb(skb);
955 struct ipoib_header *header;
958 header = (struct ipoib_header *) skb->data;
960 if (unlikely(cb->hwaddr[4] == 0xff)) {
961 /* multicast, arrange "if" according to probability */
962 if ((header->proto != htons(ETH_P_IP)) &&
963 (header->proto != htons(ETH_P_IPV6)) &&
964 (header->proto != htons(ETH_P_ARP)) &&
965 (header->proto != htons(ETH_P_RARP)) &&
966 (header->proto != htons(ETH_P_TIPC))) {
967 /* ethertype not supported by IPoIB */
968 ++dev->stats.tx_dropped;
969 dev_kfree_skb_any(skb);
972 /* Add in the P_Key for multicast*/
973 cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
974 cb->hwaddr[9] = priv->pkey & 0xff;
976 neigh = ipoib_neigh_get(dev, cb->hwaddr);
978 goto send_using_neigh;
979 ipoib_mcast_send(dev, cb->hwaddr, skb);
983 /* unicast, arrange "switch" according to probability */
984 switch (header->proto) {
985 case htons(ETH_P_IP):
986 case htons(ETH_P_IPV6):
987 case htons(ETH_P_TIPC):
988 neigh = ipoib_neigh_get(dev, cb->hwaddr);
989 if (unlikely(!neigh)) {
990 neigh_add_path(skb, cb->hwaddr, dev);
994 case htons(ETH_P_ARP):
995 case htons(ETH_P_RARP):
996 /* for unicast ARP and RARP should always perform path find */
997 unicast_arp_send(skb, dev, cb);
1000 /* ethertype not supported by IPoIB */
1001 ++dev->stats.tx_dropped;
1002 dev_kfree_skb_any(skb);
1003 return NETDEV_TX_OK;
1007 /* note we now hold a ref to neigh */
1008 if (ipoib_cm_get(neigh)) {
1009 if (ipoib_cm_up(neigh)) {
1010 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
1013 } else if (neigh->ah) {
1014 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
1018 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1019 spin_lock_irqsave(&priv->lock, flags);
1020 __skb_queue_tail(&neigh->queue, skb);
1021 spin_unlock_irqrestore(&priv->lock, flags);
1023 ++dev->stats.tx_dropped;
1024 dev_kfree_skb_any(skb);
1028 ipoib_neigh_put(neigh);
1030 return NETDEV_TX_OK;
1033 static void ipoib_timeout(struct net_device *dev)
1035 struct ipoib_dev_priv *priv = netdev_priv(dev);
1037 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
1038 jiffies_to_msecs(jiffies - dev->trans_start));
1039 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
1040 netif_queue_stopped(dev),
1041 priv->tx_head, priv->tx_tail);
1042 /* XXX reset QP, etc. */
1045 static int ipoib_hard_header(struct sk_buff *skb,
1046 struct net_device *dev,
1047 unsigned short type,
1048 const void *daddr, const void *saddr, unsigned len)
1050 struct ipoib_header *header;
1051 struct ipoib_cb *cb = ipoib_skb_cb(skb);
1053 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
1055 header->proto = htons(type);
1056 header->reserved = 0;
1059 * we don't rely on dst_entry structure, always stuff the
1060 * destination address into skb->cb so we can figure out where
1061 * to send the packet later.
1063 memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
1065 return sizeof *header;
1068 static void ipoib_set_mcast_list(struct net_device *dev)
1070 struct ipoib_dev_priv *priv = netdev_priv(dev);
1072 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
1073 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
1077 queue_work(priv->wq, &priv->restart_task);
1080 static int ipoib_get_iflink(const struct net_device *dev)
1082 struct ipoib_dev_priv *priv = netdev_priv(dev);
1084 /* parent interface */
1085 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
1086 return dev->ifindex;
1088 /* child/vlan interface */
1089 return priv->parent->ifindex;
1092 static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
1095 * Use only the address parts that contributes to spreading
1096 * The subnet prefix is not used as one can not connect to
1097 * same remote port (GUID) using the same remote QPN via two
1098 * different subnets.
1100 /* qpn octets[1:4) & port GUID octets[12:20) */
1101 u32 *d32 = (u32 *) daddr;
1104 hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
1105 return hv & htbl->mask;
1108 struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
1110 struct ipoib_dev_priv *priv = netdev_priv(dev);
1111 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1112 struct ipoib_neigh_hash *htbl;
1113 struct ipoib_neigh *neigh = NULL;
1118 htbl = rcu_dereference_bh(ntbl->htbl);
1123 hash_val = ipoib_addr_hash(htbl, daddr);
1124 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
1126 neigh = rcu_dereference_bh(neigh->hnext)) {
1127 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1128 /* found, take one ref on behalf of the caller */
1129 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1135 if (likely(skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE))
1136 neigh->alive = jiffies;
1142 rcu_read_unlock_bh();
1146 static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
1148 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1149 struct ipoib_neigh_hash *htbl;
1150 unsigned long neigh_obsolete;
1152 unsigned long flags;
1154 LIST_HEAD(remove_list);
1155 struct ipoib_mcast *mcast, *tmcast;
1156 struct net_device *dev = priv->dev;
1158 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1161 spin_lock_irqsave(&priv->lock, flags);
1163 htbl = rcu_dereference_protected(ntbl->htbl,
1164 lockdep_is_held(&priv->lock));
1169 /* neigh is obsolete if it was idle for two GC periods */
1170 dt = 2 * arp_tbl.gc_interval;
1171 neigh_obsolete = jiffies - dt;
1172 /* handle possible race condition */
1173 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1176 for (i = 0; i < htbl->size; i++) {
1177 struct ipoib_neigh *neigh;
1178 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1180 while ((neigh = rcu_dereference_protected(*np,
1181 lockdep_is_held(&priv->lock))) != NULL) {
1182 /* was the neigh idle for two GC periods */
1183 if (time_after(neigh_obsolete, neigh->alive)) {
1184 u8 *mgid = neigh->daddr + 4;
1186 /* Is this multicast ? */
1187 if (*mgid == 0xff) {
1188 mcast = __ipoib_mcast_find(dev, mgid);
1190 if (mcast && test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
1191 list_del(&mcast->list);
1192 rb_erase(&mcast->rb_node, &priv->multicast_tree);
1193 list_add_tail(&mcast->list, &remove_list);
1197 rcu_assign_pointer(*np,
1198 rcu_dereference_protected(neigh->hnext,
1199 lockdep_is_held(&priv->lock)));
1200 /* remove from path/mc list */
1201 list_del(&neigh->list);
1202 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1211 spin_unlock_irqrestore(&priv->lock, flags);
1212 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
1213 ipoib_mcast_leave(dev, mcast);
1214 ipoib_mcast_free(mcast);
1218 static void ipoib_reap_neigh(struct work_struct *work)
1220 struct ipoib_dev_priv *priv =
1221 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
1223 __ipoib_reap_neigh(priv);
1225 if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1226 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1227 arp_tbl.gc_interval);
1231 static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
1232 struct net_device *dev)
1234 struct ipoib_neigh *neigh;
1236 neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
1241 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
1242 skb_queue_head_init(&neigh->queue);
1243 INIT_LIST_HEAD(&neigh->list);
1244 ipoib_cm_set(neigh, NULL);
1245 /* one ref on behalf of the caller */
1246 atomic_set(&neigh->refcnt, 1);
1251 struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
1252 struct net_device *dev)
1254 struct ipoib_dev_priv *priv = netdev_priv(dev);
1255 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1256 struct ipoib_neigh_hash *htbl;
1257 struct ipoib_neigh *neigh;
1260 htbl = rcu_dereference_protected(ntbl->htbl,
1261 lockdep_is_held(&priv->lock));
1267 /* need to add a new neigh, but maybe some other thread succeeded?
1268 * recalc hash, maybe hash resize took place so we do a search
1270 hash_val = ipoib_addr_hash(htbl, daddr);
1271 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
1272 lockdep_is_held(&priv->lock));
1274 neigh = rcu_dereference_protected(neigh->hnext,
1275 lockdep_is_held(&priv->lock))) {
1276 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1277 /* found, take one ref on behalf of the caller */
1278 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1283 neigh->alive = jiffies;
1288 neigh = ipoib_neigh_ctor(daddr, dev);
1292 /* one ref on behalf of the hash table */
1293 atomic_inc(&neigh->refcnt);
1294 neigh->alive = jiffies;
1296 rcu_assign_pointer(neigh->hnext,
1297 rcu_dereference_protected(htbl->buckets[hash_val],
1298 lockdep_is_held(&priv->lock)));
1299 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1300 atomic_inc(&ntbl->entries);
1307 void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1309 /* neigh reference count was dropprd to zero */
1310 struct net_device *dev = neigh->dev;
1311 struct ipoib_dev_priv *priv = netdev_priv(dev);
1312 struct sk_buff *skb;
1314 ipoib_put_ah(neigh->ah);
1315 while ((skb = __skb_dequeue(&neigh->queue))) {
1316 ++dev->stats.tx_dropped;
1317 dev_kfree_skb_any(skb);
1319 if (ipoib_cm_get(neigh))
1320 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
1321 ipoib_dbg(netdev_priv(dev),
1322 "neigh free for %06x %pI6\n",
1323 IPOIB_QPN(neigh->daddr),
1326 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1327 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1328 complete(&priv->ntbl.flushed);
1332 static void ipoib_neigh_reclaim(struct rcu_head *rp)
1334 /* Called as a result of removal from hash table */
1335 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1336 /* note TX context may hold another ref */
1337 ipoib_neigh_put(neigh);
1340 void ipoib_neigh_free(struct ipoib_neigh *neigh)
1342 struct net_device *dev = neigh->dev;
1343 struct ipoib_dev_priv *priv = netdev_priv(dev);
1344 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1345 struct ipoib_neigh_hash *htbl;
1346 struct ipoib_neigh __rcu **np;
1347 struct ipoib_neigh *n;
1350 htbl = rcu_dereference_protected(ntbl->htbl,
1351 lockdep_is_held(&priv->lock));
1355 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1356 np = &htbl->buckets[hash_val];
1357 for (n = rcu_dereference_protected(*np,
1358 lockdep_is_held(&priv->lock));
1360 n = rcu_dereference_protected(*np,
1361 lockdep_is_held(&priv->lock))) {
1364 rcu_assign_pointer(*np,
1365 rcu_dereference_protected(neigh->hnext,
1366 lockdep_is_held(&priv->lock)));
1367 /* remove from parent list */
1368 list_del(&neigh->list);
1369 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1377 static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1379 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1380 struct ipoib_neigh_hash *htbl;
1381 struct ipoib_neigh __rcu **buckets;
1384 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1386 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1389 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1390 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1391 buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1397 htbl->mask = (size - 1);
1398 htbl->buckets = buckets;
1399 RCU_INIT_POINTER(ntbl->htbl, htbl);
1401 atomic_set(&ntbl->entries, 0);
1403 /* start garbage collection */
1404 clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1405 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1406 arp_tbl.gc_interval);
1411 static void neigh_hash_free_rcu(struct rcu_head *head)
1413 struct ipoib_neigh_hash *htbl = container_of(head,
1414 struct ipoib_neigh_hash,
1416 struct ipoib_neigh __rcu **buckets = htbl->buckets;
1417 struct ipoib_neigh_table *ntbl = htbl->ntbl;
1421 complete(&ntbl->deleted);
1424 void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1426 struct ipoib_dev_priv *priv = netdev_priv(dev);
1427 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1428 struct ipoib_neigh_hash *htbl;
1429 unsigned long flags;
1432 /* remove all neigh connected to a given path or mcast */
1433 spin_lock_irqsave(&priv->lock, flags);
1435 htbl = rcu_dereference_protected(ntbl->htbl,
1436 lockdep_is_held(&priv->lock));
1441 for (i = 0; i < htbl->size; i++) {
1442 struct ipoib_neigh *neigh;
1443 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1445 while ((neigh = rcu_dereference_protected(*np,
1446 lockdep_is_held(&priv->lock))) != NULL) {
1447 /* delete neighs belong to this parent */
1448 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1449 rcu_assign_pointer(*np,
1450 rcu_dereference_protected(neigh->hnext,
1451 lockdep_is_held(&priv->lock)));
1452 /* remove from parent list */
1453 list_del(&neigh->list);
1454 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1462 spin_unlock_irqrestore(&priv->lock, flags);
1465 static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1467 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1468 struct ipoib_neigh_hash *htbl;
1469 unsigned long flags;
1470 int i, wait_flushed = 0;
1472 init_completion(&priv->ntbl.flushed);
1474 spin_lock_irqsave(&priv->lock, flags);
1476 htbl = rcu_dereference_protected(ntbl->htbl,
1477 lockdep_is_held(&priv->lock));
1481 wait_flushed = atomic_read(&priv->ntbl.entries);
1485 for (i = 0; i < htbl->size; i++) {
1486 struct ipoib_neigh *neigh;
1487 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1489 while ((neigh = rcu_dereference_protected(*np,
1490 lockdep_is_held(&priv->lock))) != NULL) {
1491 rcu_assign_pointer(*np,
1492 rcu_dereference_protected(neigh->hnext,
1493 lockdep_is_held(&priv->lock)));
1494 /* remove from path/mc list */
1495 list_del(&neigh->list);
1496 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1501 rcu_assign_pointer(ntbl->htbl, NULL);
1502 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1505 spin_unlock_irqrestore(&priv->lock, flags);
1507 wait_for_completion(&priv->ntbl.flushed);
1510 static void ipoib_neigh_hash_uninit(struct net_device *dev)
1512 struct ipoib_dev_priv *priv = netdev_priv(dev);
1515 ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
1516 init_completion(&priv->ntbl.deleted);
1517 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1519 /* Stop GC if called at init fail need to cancel work */
1520 stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1522 cancel_delayed_work(&priv->neigh_reap_task);
1524 ipoib_flush_neighs(priv);
1526 wait_for_completion(&priv->ntbl.deleted);
1530 int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1532 struct ipoib_dev_priv *priv = netdev_priv(dev);
1534 /* Allocate RX/TX "rings" to hold queued skbs */
1535 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
1537 if (!priv->rx_ring) {
1538 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
1539 ca->name, ipoib_recvq_size);
1543 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
1544 if (!priv->tx_ring) {
1545 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
1546 ca->name, ipoib_sendq_size);
1547 goto out_rx_ring_cleanup;
1550 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
1552 if (ipoib_ib_dev_init(dev, ca, port))
1553 goto out_tx_ring_cleanup;
1556 * Must be after ipoib_ib_dev_init so we can allocate a per
1557 * device wq there and use it here
1559 if (ipoib_neigh_hash_init(priv) < 0)
1560 goto out_dev_uninit;
1565 ipoib_ib_dev_cleanup(dev);
1567 out_tx_ring_cleanup:
1568 vfree(priv->tx_ring);
1570 out_rx_ring_cleanup:
1571 kfree(priv->rx_ring);
1577 void ipoib_dev_cleanup(struct net_device *dev)
1579 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
1584 ipoib_delete_debug_files(dev);
1586 /* Delete any child interfaces first */
1587 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
1588 /* Stop GC on child */
1589 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1590 cancel_delayed_work(&cpriv->neigh_reap_task);
1591 unregister_netdevice_queue(cpriv->dev, &head);
1593 unregister_netdevice_many(&head);
1596 * Must be before ipoib_ib_dev_cleanup or we delete an in use
1599 ipoib_neigh_hash_uninit(dev);
1601 ipoib_ib_dev_cleanup(dev);
1603 kfree(priv->rx_ring);
1604 vfree(priv->tx_ring);
1606 priv->rx_ring = NULL;
1607 priv->tx_ring = NULL;
1610 static const struct header_ops ipoib_header_ops = {
1611 .create = ipoib_hard_header,
1614 static const struct net_device_ops ipoib_netdev_ops = {
1615 .ndo_uninit = ipoib_uninit,
1616 .ndo_open = ipoib_open,
1617 .ndo_stop = ipoib_stop,
1618 .ndo_change_mtu = ipoib_change_mtu,
1619 .ndo_fix_features = ipoib_fix_features,
1620 .ndo_start_xmit = ipoib_start_xmit,
1621 .ndo_tx_timeout = ipoib_timeout,
1622 .ndo_set_rx_mode = ipoib_set_mcast_list,
1623 .ndo_get_iflink = ipoib_get_iflink,
1626 void ipoib_setup(struct net_device *dev)
1628 struct ipoib_dev_priv *priv = netdev_priv(dev);
1630 dev->netdev_ops = &ipoib_netdev_ops;
1631 dev->header_ops = &ipoib_header_ops;
1633 ipoib_set_ethtool_ops(dev);
1635 netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT);
1637 dev->watchdog_timeo = HZ;
1639 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1641 dev->hard_header_len = IPOIB_ENCAP_LEN;
1642 dev->addr_len = INFINIBAND_ALEN;
1643 dev->type = ARPHRD_INFINIBAND;
1644 dev->tx_queue_len = ipoib_sendq_size * 2;
1645 dev->features = (NETIF_F_VLAN_CHALLENGED |
1647 netif_keep_dst(dev);
1649 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
1653 spin_lock_init(&priv->lock);
1655 init_rwsem(&priv->vlan_rwsem);
1657 INIT_LIST_HEAD(&priv->path_list);
1658 INIT_LIST_HEAD(&priv->child_intfs);
1659 INIT_LIST_HEAD(&priv->dead_ahs);
1660 INIT_LIST_HEAD(&priv->multicast_list);
1662 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
1663 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
1664 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
1665 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
1666 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
1667 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1668 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
1669 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
1672 struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
1674 struct net_device *dev;
1676 dev = alloc_netdev((int)sizeof(struct ipoib_dev_priv), name,
1677 NET_NAME_UNKNOWN, ipoib_setup);
1681 return netdev_priv(dev);
1684 static ssize_t show_pkey(struct device *dev,
1685 struct device_attribute *attr, char *buf)
1687 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1689 return sprintf(buf, "0x%04x\n", priv->pkey);
1691 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1693 static ssize_t show_umcast(struct device *dev,
1694 struct device_attribute *attr, char *buf)
1696 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1698 return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1701 void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
1703 struct ipoib_dev_priv *priv = netdev_priv(ndev);
1705 if (umcast_val > 0) {
1706 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1707 ipoib_warn(priv, "ignoring multicast groups joined directly "
1710 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1713 static ssize_t set_umcast(struct device *dev,
1714 struct device_attribute *attr,
1715 const char *buf, size_t count)
1717 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1719 ipoib_set_umcast(to_net_dev(dev), umcast_val);
1723 static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1725 int ipoib_add_umcast_attr(struct net_device *dev)
1727 return device_create_file(&dev->dev, &dev_attr_umcast);
1730 static ssize_t create_child(struct device *dev,
1731 struct device_attribute *attr,
1732 const char *buf, size_t count)
1737 if (sscanf(buf, "%i", &pkey) != 1)
1740 if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
1744 * Set the full membership bit, so that we join the right
1745 * broadcast group, etc.
1749 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
1751 return ret ? ret : count;
1753 static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
1755 static ssize_t delete_child(struct device *dev,
1756 struct device_attribute *attr,
1757 const char *buf, size_t count)
1762 if (sscanf(buf, "%i", &pkey) != 1)
1765 if (pkey < 0 || pkey > 0xffff)
1768 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
1770 return ret ? ret : count;
1773 static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
1775 int ipoib_add_pkey_attr(struct net_device *dev)
1777 return device_create_file(&dev->dev, &dev_attr_pkey);
1780 int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
1782 struct ib_device_attr *device_attr;
1783 int result = -ENOMEM;
1785 device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
1787 printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
1788 hca->name, sizeof *device_attr);
1792 result = ib_query_device(hca, device_attr);
1794 printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
1799 priv->hca_caps = device_attr->device_cap_flags;
1803 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
1804 priv->dev->hw_features = NETIF_F_SG |
1805 NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1807 if (priv->hca_caps & IB_DEVICE_UD_TSO)
1808 priv->dev->hw_features |= NETIF_F_TSO;
1810 priv->dev->features |= priv->dev->hw_features;
1816 static struct net_device *ipoib_add_port(const char *format,
1817 struct ib_device *hca, u8 port)
1819 struct ipoib_dev_priv *priv;
1820 struct ib_port_attr attr;
1821 int result = -ENOMEM;
1823 priv = ipoib_intf_alloc(format);
1825 goto alloc_mem_failed;
1827 SET_NETDEV_DEV(priv->dev, hca->dma_device);
1828 priv->dev->dev_id = port - 1;
1830 result = ib_query_port(hca, port, &attr);
1832 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1834 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
1836 goto device_init_failed;
1839 /* MTU will be reset when mcast join happens */
1840 priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
1841 priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
1843 priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
1845 result = ib_query_pkey(hca, port, 0, &priv->pkey);
1847 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1848 hca->name, port, result);
1849 goto device_init_failed;
1852 result = ipoib_set_dev_features(priv, hca);
1854 goto device_init_failed;
1857 * Set the full membership bit, so that we join the right
1858 * broadcast group, etc.
1860 priv->pkey |= 0x8000;
1862 priv->dev->broadcast[8] = priv->pkey >> 8;
1863 priv->dev->broadcast[9] = priv->pkey & 0xff;
1865 result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
1867 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1868 hca->name, port, result);
1869 goto device_init_failed;
1871 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1873 result = ipoib_dev_init(priv->dev, hca, port);
1875 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1876 hca->name, port, result);
1877 goto device_init_failed;
1880 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1881 priv->ca, ipoib_event);
1882 result = ib_register_event_handler(&priv->event_handler);
1884 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1885 "port %d (ret = %d)\n",
1886 hca->name, port, result);
1890 result = register_netdev(priv->dev);
1892 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1893 hca->name, port, result);
1894 goto register_failed;
1897 ipoib_create_debug_files(priv->dev);
1899 if (ipoib_cm_add_mode_attr(priv->dev))
1901 if (ipoib_add_pkey_attr(priv->dev))
1903 if (ipoib_add_umcast_attr(priv->dev))
1905 if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
1907 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
1913 ipoib_delete_debug_files(priv->dev);
1914 unregister_netdev(priv->dev);
1917 ib_unregister_event_handler(&priv->event_handler);
1918 flush_workqueue(ipoib_workqueue);
1919 /* Stop GC if started before flush */
1920 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1921 cancel_delayed_work(&priv->neigh_reap_task);
1922 flush_workqueue(priv->wq);
1925 ipoib_dev_cleanup(priv->dev);
1928 free_netdev(priv->dev);
1931 return ERR_PTR(result);
1934 static void ipoib_add_one(struct ib_device *device)
1936 struct list_head *dev_list;
1937 struct net_device *dev;
1938 struct ipoib_dev_priv *priv;
1942 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1946 INIT_LIST_HEAD(dev_list);
1948 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
1949 if (!rdma_protocol_ib(device, p))
1951 dev = ipoib_add_port("ib%d", device, p);
1953 priv = netdev_priv(dev);
1954 list_add_tail(&priv->list, dev_list);
1964 ib_set_client_data(device, &ipoib_client, dev_list);
1967 static void ipoib_remove_one(struct ib_device *device, void *client_data)
1969 struct ipoib_dev_priv *priv, *tmp;
1970 struct list_head *dev_list = client_data;
1975 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1976 ib_unregister_event_handler(&priv->event_handler);
1977 flush_workqueue(ipoib_workqueue);
1980 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
1984 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1985 cancel_delayed_work(&priv->neigh_reap_task);
1986 flush_workqueue(priv->wq);
1988 unregister_netdev(priv->dev);
1989 free_netdev(priv->dev);
1995 static int __init ipoib_init_module(void)
1999 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
2000 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
2001 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
2003 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
2004 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
2005 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
2006 #ifdef CONFIG_INFINIBAND_IPOIB_CM
2007 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
2011 * When copying small received packets, we only copy from the
2012 * linear data part of the SKB, so we rely on this condition.
2014 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
2016 ret = ipoib_register_debugfs();
2021 * We create a global workqueue here that is used for all flush
2022 * operations. However, if you attempt to flush a workqueue
2023 * from a task on that same workqueue, it deadlocks the system.
2024 * We want to be able to flush the tasks associated with a
2025 * specific net device, so we also create a workqueue for each
2026 * netdevice. We queue up the tasks for that device only on
2027 * its private workqueue, and we only queue up flush events
2028 * on our global flush workqueue. This avoids the deadlocks.
2030 ipoib_workqueue = create_singlethread_workqueue("ipoib_flush");
2031 if (!ipoib_workqueue) {
2036 ib_sa_register_client(&ipoib_sa_client);
2038 ret = ib_register_client(&ipoib_client);
2042 ret = ipoib_netlink_init();
2049 ib_unregister_client(&ipoib_client);
2052 ib_sa_unregister_client(&ipoib_sa_client);
2053 destroy_workqueue(ipoib_workqueue);
2056 ipoib_unregister_debugfs();
2061 static void __exit ipoib_cleanup_module(void)
2063 ipoib_netlink_fini();
2064 ib_unregister_client(&ipoib_client);
2065 ib_sa_unregister_client(&ipoib_sa_client);
2066 ipoib_unregister_debugfs();
2067 destroy_workqueue(ipoib_workqueue);
2070 module_init(ipoib_init_module);
2071 module_exit(ipoib_cleanup_module);