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 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
110 static int ipoib_netdev_event(struct notifier_block *this,
111 unsigned long event, void *ptr)
113 struct netdev_notifier_info *ni = ptr;
114 struct net_device *dev = ni->dev;
116 if (dev->netdev_ops->ndo_open != ipoib_open)
120 case NETDEV_REGISTER:
121 ipoib_create_debug_files(dev);
123 case NETDEV_CHANGENAME:
124 ipoib_delete_debug_files(dev);
125 ipoib_create_debug_files(dev);
127 case NETDEV_UNREGISTER:
128 ipoib_delete_debug_files(dev);
136 int ipoib_open(struct net_device *dev)
138 struct ipoib_dev_priv *priv = netdev_priv(dev);
140 ipoib_dbg(priv, "bringing up interface\n");
142 netif_carrier_off(dev);
144 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
146 if (ipoib_ib_dev_open(dev)) {
147 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
152 if (ipoib_ib_dev_up(dev))
155 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
156 struct ipoib_dev_priv *cpriv;
158 /* Bring up any child interfaces too */
159 down_read(&priv->vlan_rwsem);
160 list_for_each_entry(cpriv, &priv->child_intfs, list) {
163 flags = cpriv->dev->flags;
167 dev_change_flags(cpriv->dev, flags | IFF_UP);
169 up_read(&priv->vlan_rwsem);
172 netif_start_queue(dev);
177 ipoib_ib_dev_stop(dev);
180 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
185 static int ipoib_stop(struct net_device *dev)
187 struct ipoib_dev_priv *priv = netdev_priv(dev);
189 ipoib_dbg(priv, "stopping interface\n");
191 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
193 netif_stop_queue(dev);
195 ipoib_ib_dev_down(dev);
196 ipoib_ib_dev_stop(dev);
198 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
199 struct ipoib_dev_priv *cpriv;
201 /* Bring down any child interfaces too */
202 down_read(&priv->vlan_rwsem);
203 list_for_each_entry(cpriv, &priv->child_intfs, list) {
206 flags = cpriv->dev->flags;
207 if (!(flags & IFF_UP))
210 dev_change_flags(cpriv->dev, flags & ~IFF_UP);
212 up_read(&priv->vlan_rwsem);
218 static void ipoib_uninit(struct net_device *dev)
220 ipoib_dev_cleanup(dev);
223 static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
225 struct ipoib_dev_priv *priv = netdev_priv(dev);
227 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
228 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
233 static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
235 struct ipoib_dev_priv *priv = netdev_priv(dev);
237 /* dev->mtu > 2K ==> connected mode */
238 if (ipoib_cm_admin_enabled(dev)) {
239 if (new_mtu > ipoib_cm_max_mtu(dev))
242 if (new_mtu > priv->mcast_mtu)
243 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
250 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
253 priv->admin_mtu = new_mtu;
255 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
260 /* Called with an RCU read lock taken */
261 static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr,
262 struct net_device *dev)
264 struct net *net = dev_net(dev);
265 struct in_device *in_dev;
266 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
267 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
270 switch (addr->sa_family) {
272 in_dev = in_dev_get(dev);
276 ret_addr = inet_confirm_addr(net, in_dev, 0,
277 addr_in->sin_addr.s_addr,
285 if (IS_ENABLED(CONFIG_IPV6) &&
286 ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1))
295 * Find the master net_device on top of the given net_device.
296 * @dev: base IPoIB net_device
298 * Returns the master net_device with a reference held, or the same net_device
299 * if no master exists.
301 static struct net_device *ipoib_get_master_net_dev(struct net_device *dev)
303 struct net_device *master;
306 master = netdev_master_upper_dev_get_rcu(dev);
319 * Find a net_device matching the given address, which is an upper device of
320 * the given net_device.
321 * @addr: IP address to look for.
322 * @dev: base IPoIB net_device
324 * If found, returns the net_device with a reference held. Otherwise return
327 static struct net_device *ipoib_get_net_dev_match_addr(
328 const struct sockaddr *addr, struct net_device *dev)
330 struct net_device *upper,
332 struct list_head *iter;
335 if (ipoib_is_dev_match_addr_rcu(addr, dev)) {
341 netdev_for_each_all_upper_dev_rcu(dev, upper, iter) {
342 if (ipoib_is_dev_match_addr_rcu(addr, upper)) {
353 /* returns the number of IPoIB netdevs on top a given ipoib device matching a
354 * pkey_index and address, if one exists.
356 * @found_net_dev: contains a matching net_device if the return value >= 1,
357 * with a reference held. */
358 static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
359 const union ib_gid *gid,
361 const struct sockaddr *addr,
363 struct net_device **found_net_dev)
365 struct ipoib_dev_priv *child_priv;
366 struct net_device *net_dev = NULL;
369 if (priv->pkey_index == pkey_index &&
370 (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
372 net_dev = ipoib_get_master_net_dev(priv->dev);
374 /* Verify the net_device matches the IP address, as
375 * IPoIB child devices currently share a GID. */
376 net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev);
380 *found_net_dev = net_dev;
387 /* Check child interfaces */
388 down_read_nested(&priv->vlan_rwsem, nesting);
389 list_for_each_entry(child_priv, &priv->child_intfs, list) {
390 matches += ipoib_match_gid_pkey_addr(child_priv, gid,
397 up_read(&priv->vlan_rwsem);
402 /* Returns the number of matching net_devs found (between 0 and 2). Also
403 * return the matching net_device in the @net_dev parameter, holding a
404 * reference to the net_device, if the number of matches >= 1 */
405 static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port,
407 const union ib_gid *gid,
408 const struct sockaddr *addr,
409 struct net_device **net_dev)
411 struct ipoib_dev_priv *priv;
416 list_for_each_entry(priv, dev_list, list) {
417 if (priv->port != port)
420 matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
429 static struct net_device *ipoib_get_net_dev_by_params(
430 struct ib_device *dev, u8 port, u16 pkey,
431 const union ib_gid *gid, const struct sockaddr *addr,
434 struct net_device *net_dev;
435 struct list_head *dev_list = client_data;
440 if (!rdma_protocol_ib(dev, port))
443 ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
450 /* See if we can find a unique device matching the L2 parameters */
451 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
452 gid, NULL, &net_dev);
463 /* Couldn't find a unique device with L2 parameters only. Use L3
464 * address to uniquely match the net device */
465 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
466 gid, addr, &net_dev);
471 dev_warn_ratelimited(&dev->dev,
472 "duplicate IP address detected\n");
479 int ipoib_set_mode(struct net_device *dev, const char *buf)
481 struct ipoib_dev_priv *priv = netdev_priv(dev);
483 /* flush paths if we switch modes so that connections are restarted */
484 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
485 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
486 ipoib_warn(priv, "enabling connected mode "
487 "will cause multicast packet drops\n");
488 netdev_update_features(dev);
489 dev_set_mtu(dev, ipoib_cm_max_mtu(dev));
491 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
493 ipoib_flush_paths(dev);
494 return (!rtnl_trylock()) ? -EBUSY : 0;
497 if (!strcmp(buf, "datagram\n")) {
498 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
499 netdev_update_features(dev);
500 dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
502 ipoib_flush_paths(dev);
503 return (!rtnl_trylock()) ? -EBUSY : 0;
509 struct ipoib_path *__path_find(struct net_device *dev, void *gid)
511 struct ipoib_dev_priv *priv = netdev_priv(dev);
512 struct rb_node *n = priv->path_tree.rb_node;
513 struct ipoib_path *path;
517 path = rb_entry(n, struct ipoib_path, rb_node);
519 ret = memcmp(gid, path->pathrec.dgid.raw,
520 sizeof (union ib_gid));
533 static int __path_add(struct net_device *dev, struct ipoib_path *path)
535 struct ipoib_dev_priv *priv = netdev_priv(dev);
536 struct rb_node **n = &priv->path_tree.rb_node;
537 struct rb_node *pn = NULL;
538 struct ipoib_path *tpath;
543 tpath = rb_entry(pn, struct ipoib_path, rb_node);
545 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
546 sizeof (union ib_gid));
555 rb_link_node(&path->rb_node, pn, n);
556 rb_insert_color(&path->rb_node, &priv->path_tree);
558 list_add_tail(&path->list, &priv->path_list);
563 static void path_free(struct net_device *dev, struct ipoib_path *path)
567 while ((skb = __skb_dequeue(&path->queue)))
568 dev_kfree_skb_irq(skb);
570 ipoib_dbg(netdev_priv(dev), "path_free\n");
572 /* remove all neigh connected to this path */
573 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
576 ipoib_put_ah(path->ah);
581 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
583 struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
585 struct ipoib_path_iter *iter;
587 iter = kmalloc(sizeof *iter, GFP_KERNEL);
592 memset(iter->path.pathrec.dgid.raw, 0, 16);
594 if (ipoib_path_iter_next(iter)) {
602 int ipoib_path_iter_next(struct ipoib_path_iter *iter)
604 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
606 struct ipoib_path *path;
609 spin_lock_irq(&priv->lock);
611 n = rb_first(&priv->path_tree);
614 path = rb_entry(n, struct ipoib_path, rb_node);
616 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
617 sizeof (union ib_gid)) < 0) {
626 spin_unlock_irq(&priv->lock);
631 void ipoib_path_iter_read(struct ipoib_path_iter *iter,
632 struct ipoib_path *path)
637 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
639 void ipoib_mark_paths_invalid(struct net_device *dev)
641 struct ipoib_dev_priv *priv = netdev_priv(dev);
642 struct ipoib_path *path, *tp;
644 spin_lock_irq(&priv->lock);
646 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
647 ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
648 be16_to_cpu(path->pathrec.dlid),
649 path->pathrec.dgid.raw);
653 spin_unlock_irq(&priv->lock);
656 static void push_pseudo_header(struct sk_buff *skb, const char *daddr)
658 struct ipoib_pseudo_header *phdr;
660 phdr = (struct ipoib_pseudo_header *)skb_push(skb, sizeof(*phdr));
661 memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
664 void ipoib_flush_paths(struct net_device *dev)
666 struct ipoib_dev_priv *priv = netdev_priv(dev);
667 struct ipoib_path *path, *tp;
668 LIST_HEAD(remove_list);
671 netif_tx_lock_bh(dev);
672 spin_lock_irqsave(&priv->lock, flags);
674 list_splice_init(&priv->path_list, &remove_list);
676 list_for_each_entry(path, &remove_list, list)
677 rb_erase(&path->rb_node, &priv->path_tree);
679 list_for_each_entry_safe(path, tp, &remove_list, list) {
681 ib_sa_cancel_query(path->query_id, path->query);
682 spin_unlock_irqrestore(&priv->lock, flags);
683 netif_tx_unlock_bh(dev);
684 wait_for_completion(&path->done);
685 path_free(dev, path);
686 netif_tx_lock_bh(dev);
687 spin_lock_irqsave(&priv->lock, flags);
690 spin_unlock_irqrestore(&priv->lock, flags);
691 netif_tx_unlock_bh(dev);
694 static void path_rec_completion(int status,
695 struct ib_sa_path_rec *pathrec,
698 struct ipoib_path *path = path_ptr;
699 struct net_device *dev = path->dev;
700 struct ipoib_dev_priv *priv = netdev_priv(dev);
701 struct ipoib_ah *ah = NULL;
702 struct ipoib_ah *old_ah = NULL;
703 struct ipoib_neigh *neigh, *tn;
704 struct sk_buff_head skqueue;
709 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
710 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
712 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
713 status, path->pathrec.dgid.raw);
715 skb_queue_head_init(&skqueue);
718 struct ib_ah_attr av;
720 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
721 ah = ipoib_create_ah(dev, priv->pd, &av);
724 spin_lock_irqsave(&priv->lock, flags);
726 if (!IS_ERR_OR_NULL(ah)) {
728 * pathrec.dgid is used as the database key from the LLADDR,
729 * it must remain unchanged even if the SA returns a different
730 * GID to use in the AH.
732 if (memcmp(pathrec->dgid.raw, path->pathrec.dgid.raw,
733 sizeof(union ib_gid))) {
736 "%s got PathRec for gid %pI6 while asked for %pI6\n",
737 dev->name, pathrec->dgid.raw,
738 path->pathrec.dgid.raw);
739 memcpy(pathrec->dgid.raw, path->pathrec.dgid.raw,
740 sizeof(union ib_gid));
743 path->pathrec = *pathrec;
748 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
749 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
751 while ((skb = __skb_dequeue(&path->queue)))
752 __skb_queue_tail(&skqueue, skb);
754 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
756 WARN_ON(neigh->ah != old_ah);
758 * Dropping the ah reference inside
759 * priv->lock is safe here, because we
760 * will hold one more reference from
761 * the original value of path->ah (ie
764 ipoib_put_ah(neigh->ah);
766 kref_get(&path->ah->ref);
767 neigh->ah = path->ah;
769 if (ipoib_cm_enabled(dev, neigh->daddr)) {
770 if (!ipoib_cm_get(neigh))
771 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
774 if (!ipoib_cm_get(neigh)) {
775 ipoib_neigh_free(neigh);
780 while ((skb = __skb_dequeue(&neigh->queue)))
781 __skb_queue_tail(&skqueue, skb);
787 complete(&path->done);
789 spin_unlock_irqrestore(&priv->lock, flags);
791 if (IS_ERR_OR_NULL(ah))
792 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
795 ipoib_put_ah(old_ah);
797 while ((skb = __skb_dequeue(&skqueue))) {
799 if (dev_queue_xmit(skb))
800 ipoib_warn(priv, "dev_queue_xmit failed "
801 "to requeue packet\n");
805 static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
807 struct ipoib_dev_priv *priv = netdev_priv(dev);
808 struct ipoib_path *path;
810 if (!priv->broadcast)
813 path = kzalloc(sizeof *path, GFP_ATOMIC);
819 skb_queue_head_init(&path->queue);
821 INIT_LIST_HEAD(&path->neigh_list);
823 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
824 path->pathrec.sgid = priv->local_gid;
825 path->pathrec.pkey = cpu_to_be16(priv->pkey);
826 path->pathrec.numb_path = 1;
827 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
832 static int path_rec_start(struct net_device *dev,
833 struct ipoib_path *path)
835 struct ipoib_dev_priv *priv = netdev_priv(dev);
837 ipoib_dbg(priv, "Start path record lookup for %pI6\n",
838 path->pathrec.dgid.raw);
840 init_completion(&path->done);
843 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
845 IB_SA_PATH_REC_DGID |
846 IB_SA_PATH_REC_SGID |
847 IB_SA_PATH_REC_NUMB_PATH |
848 IB_SA_PATH_REC_TRAFFIC_CLASS |
853 if (path->query_id < 0) {
854 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
856 complete(&path->done);
857 return path->query_id;
863 static struct ipoib_neigh *neigh_add_path(struct sk_buff *skb, u8 *daddr,
864 struct net_device *dev)
866 struct ipoib_dev_priv *priv = netdev_priv(dev);
867 struct ipoib_path *path;
868 struct ipoib_neigh *neigh;
871 spin_lock_irqsave(&priv->lock, flags);
872 neigh = ipoib_neigh_alloc(daddr, dev);
874 spin_unlock_irqrestore(&priv->lock, flags);
875 ++dev->stats.tx_dropped;
876 dev_kfree_skb_any(skb);
880 /* To avoid race condition, make sure that the
881 * neigh will be added only once.
883 if (unlikely(!list_empty(&neigh->list))) {
884 spin_unlock_irqrestore(&priv->lock, flags);
888 path = __path_find(dev, daddr + 4);
890 path = path_rec_create(dev, daddr + 4);
894 __path_add(dev, path);
897 list_add_tail(&neigh->list, &path->neigh_list);
900 kref_get(&path->ah->ref);
901 neigh->ah = path->ah;
903 if (ipoib_cm_enabled(dev, neigh->daddr)) {
904 if (!ipoib_cm_get(neigh))
905 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
906 if (!ipoib_cm_get(neigh)) {
907 ipoib_neigh_free(neigh);
910 if (skb_queue_len(&neigh->queue) <
911 IPOIB_MAX_PATH_REC_QUEUE) {
912 push_pseudo_header(skb, neigh->daddr);
913 __skb_queue_tail(&neigh->queue, skb);
915 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
916 skb_queue_len(&neigh->queue));
920 spin_unlock_irqrestore(&priv->lock, flags);
921 ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
922 ipoib_neigh_put(neigh);
928 if (!path->query && path_rec_start(dev, path))
930 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
931 push_pseudo_header(skb, neigh->daddr);
932 __skb_queue_tail(&neigh->queue, skb);
938 spin_unlock_irqrestore(&priv->lock, flags);
939 ipoib_neigh_put(neigh);
943 ipoib_neigh_free(neigh);
945 ++dev->stats.tx_dropped;
946 dev_kfree_skb_any(skb);
948 spin_unlock_irqrestore(&priv->lock, flags);
949 ipoib_neigh_put(neigh);
954 static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
955 struct ipoib_pseudo_header *phdr)
957 struct ipoib_dev_priv *priv = netdev_priv(dev);
958 struct ipoib_path *path;
961 spin_lock_irqsave(&priv->lock, flags);
963 path = __path_find(dev, phdr->hwaddr + 4);
964 if (!path || !path->valid) {
968 path = path_rec_create(dev, phdr->hwaddr + 4);
972 if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
973 push_pseudo_header(skb, phdr->hwaddr);
974 __skb_queue_tail(&path->queue, skb);
976 ++dev->stats.tx_dropped;
977 dev_kfree_skb_any(skb);
980 if (!path->query && path_rec_start(dev, path)) {
981 spin_unlock_irqrestore(&priv->lock, flags);
983 path_free(dev, path);
986 __path_add(dev, path);
988 ++dev->stats.tx_dropped;
989 dev_kfree_skb_any(skb);
992 spin_unlock_irqrestore(&priv->lock, flags);
997 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
998 be16_to_cpu(path->pathrec.dlid));
1000 spin_unlock_irqrestore(&priv->lock, flags);
1001 ipoib_send(dev, skb, path->ah, IPOIB_QPN(phdr->hwaddr));
1003 } else if ((path->query || !path_rec_start(dev, path)) &&
1004 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1005 push_pseudo_header(skb, phdr->hwaddr);
1006 __skb_queue_tail(&path->queue, skb);
1008 ++dev->stats.tx_dropped;
1009 dev_kfree_skb_any(skb);
1012 spin_unlock_irqrestore(&priv->lock, flags);
1015 static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
1017 struct ipoib_dev_priv *priv = netdev_priv(dev);
1018 struct ipoib_neigh *neigh;
1019 struct ipoib_pseudo_header *phdr;
1020 struct ipoib_header *header;
1021 unsigned long flags;
1023 phdr = (struct ipoib_pseudo_header *) skb->data;
1024 skb_pull(skb, sizeof(*phdr));
1025 header = (struct ipoib_header *) skb->data;
1027 if (unlikely(phdr->hwaddr[4] == 0xff)) {
1028 /* multicast, arrange "if" according to probability */
1029 if ((header->proto != htons(ETH_P_IP)) &&
1030 (header->proto != htons(ETH_P_IPV6)) &&
1031 (header->proto != htons(ETH_P_ARP)) &&
1032 (header->proto != htons(ETH_P_RARP)) &&
1033 (header->proto != htons(ETH_P_TIPC))) {
1034 /* ethertype not supported by IPoIB */
1035 ++dev->stats.tx_dropped;
1036 dev_kfree_skb_any(skb);
1037 return NETDEV_TX_OK;
1039 /* Add in the P_Key for multicast*/
1040 phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
1041 phdr->hwaddr[9] = priv->pkey & 0xff;
1043 neigh = ipoib_neigh_get(dev, phdr->hwaddr);
1045 goto send_using_neigh;
1046 ipoib_mcast_send(dev, phdr->hwaddr, skb);
1047 return NETDEV_TX_OK;
1050 /* unicast, arrange "switch" according to probability */
1051 switch (header->proto) {
1052 case htons(ETH_P_IP):
1053 case htons(ETH_P_IPV6):
1054 case htons(ETH_P_TIPC):
1055 neigh = ipoib_neigh_get(dev, phdr->hwaddr);
1056 if (unlikely(!neigh)) {
1057 neigh = neigh_add_path(skb, phdr->hwaddr, dev);
1059 return NETDEV_TX_OK;
1062 case htons(ETH_P_ARP):
1063 case htons(ETH_P_RARP):
1064 /* for unicast ARP and RARP should always perform path find */
1065 unicast_arp_send(skb, dev, phdr);
1066 return NETDEV_TX_OK;
1068 /* ethertype not supported by IPoIB */
1069 ++dev->stats.tx_dropped;
1070 dev_kfree_skb_any(skb);
1071 return NETDEV_TX_OK;
1075 /* note we now hold a ref to neigh */
1076 if (ipoib_cm_get(neigh)) {
1077 if (ipoib_cm_up(neigh)) {
1078 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
1081 } else if (neigh->ah) {
1082 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(phdr->hwaddr));
1086 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1087 push_pseudo_header(skb, phdr->hwaddr);
1088 spin_lock_irqsave(&priv->lock, flags);
1089 __skb_queue_tail(&neigh->queue, skb);
1090 spin_unlock_irqrestore(&priv->lock, flags);
1092 ++dev->stats.tx_dropped;
1093 dev_kfree_skb_any(skb);
1097 ipoib_neigh_put(neigh);
1099 return NETDEV_TX_OK;
1102 static void ipoib_timeout(struct net_device *dev)
1104 struct ipoib_dev_priv *priv = netdev_priv(dev);
1106 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
1107 jiffies_to_msecs(jiffies - dev->trans_start));
1108 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
1109 netif_queue_stopped(dev),
1110 priv->tx_head, priv->tx_tail);
1111 /* XXX reset QP, etc. */
1114 static int ipoib_hard_header(struct sk_buff *skb,
1115 struct net_device *dev,
1116 unsigned short type,
1117 const void *daddr, const void *saddr, unsigned len)
1119 struct ipoib_header *header;
1121 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
1123 header->proto = htons(type);
1124 header->reserved = 0;
1127 * we don't rely on dst_entry structure, always stuff the
1128 * destination address into skb hard header so we can figure out where
1129 * to send the packet later.
1131 push_pseudo_header(skb, daddr);
1133 return IPOIB_HARD_LEN;
1136 static void ipoib_set_mcast_list(struct net_device *dev)
1138 struct ipoib_dev_priv *priv = netdev_priv(dev);
1140 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
1141 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
1145 queue_work(priv->wq, &priv->restart_task);
1148 static int ipoib_get_iflink(const struct net_device *dev)
1150 struct ipoib_dev_priv *priv = netdev_priv(dev);
1152 /* parent interface */
1153 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
1154 return dev->ifindex;
1156 /* child/vlan interface */
1157 return priv->parent->ifindex;
1160 static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
1163 * Use only the address parts that contributes to spreading
1164 * The subnet prefix is not used as one can not connect to
1165 * same remote port (GUID) using the same remote QPN via two
1166 * different subnets.
1168 /* qpn octets[1:4) & port GUID octets[12:20) */
1169 u32 *d32 = (u32 *) daddr;
1172 hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
1173 return hv & htbl->mask;
1176 struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
1178 struct ipoib_dev_priv *priv = netdev_priv(dev);
1179 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1180 struct ipoib_neigh_hash *htbl;
1181 struct ipoib_neigh *neigh = NULL;
1186 htbl = rcu_dereference_bh(ntbl->htbl);
1191 hash_val = ipoib_addr_hash(htbl, daddr);
1192 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
1194 neigh = rcu_dereference_bh(neigh->hnext)) {
1195 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1196 /* found, take one ref on behalf of the caller */
1197 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1203 if (likely(skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE))
1204 neigh->alive = jiffies;
1210 rcu_read_unlock_bh();
1214 static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
1216 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1217 struct ipoib_neigh_hash *htbl;
1218 unsigned long neigh_obsolete;
1220 unsigned long flags;
1222 LIST_HEAD(remove_list);
1223 struct ipoib_mcast *mcast, *tmcast;
1224 struct net_device *dev = priv->dev;
1226 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1229 spin_lock_irqsave(&priv->lock, flags);
1231 htbl = rcu_dereference_protected(ntbl->htbl,
1232 lockdep_is_held(&priv->lock));
1237 /* neigh is obsolete if it was idle for two GC periods */
1238 dt = 2 * arp_tbl.gc_interval;
1239 neigh_obsolete = jiffies - dt;
1240 /* handle possible race condition */
1241 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1244 for (i = 0; i < htbl->size; i++) {
1245 struct ipoib_neigh *neigh;
1246 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1248 while ((neigh = rcu_dereference_protected(*np,
1249 lockdep_is_held(&priv->lock))) != NULL) {
1250 /* was the neigh idle for two GC periods */
1251 if (time_after(neigh_obsolete, neigh->alive)) {
1252 u8 *mgid = neigh->daddr + 4;
1254 /* Is this multicast ? */
1255 if (*mgid == 0xff) {
1256 mcast = __ipoib_mcast_find(dev, mgid);
1258 if (mcast && test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
1259 list_del(&mcast->list);
1260 rb_erase(&mcast->rb_node, &priv->multicast_tree);
1261 list_add_tail(&mcast->list, &remove_list);
1265 rcu_assign_pointer(*np,
1266 rcu_dereference_protected(neigh->hnext,
1267 lockdep_is_held(&priv->lock)));
1268 /* remove from path/mc list */
1269 list_del_init(&neigh->list);
1270 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1279 spin_unlock_irqrestore(&priv->lock, flags);
1280 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
1281 ipoib_mcast_leave(dev, mcast);
1282 ipoib_mcast_free(mcast);
1286 static void ipoib_reap_neigh(struct work_struct *work)
1288 struct ipoib_dev_priv *priv =
1289 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
1291 __ipoib_reap_neigh(priv);
1293 if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1294 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1295 arp_tbl.gc_interval);
1299 static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
1300 struct net_device *dev)
1302 struct ipoib_neigh *neigh;
1304 neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
1309 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
1310 skb_queue_head_init(&neigh->queue);
1311 INIT_LIST_HEAD(&neigh->list);
1312 ipoib_cm_set(neigh, NULL);
1313 /* one ref on behalf of the caller */
1314 atomic_set(&neigh->refcnt, 1);
1319 struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
1320 struct net_device *dev)
1322 struct ipoib_dev_priv *priv = netdev_priv(dev);
1323 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1324 struct ipoib_neigh_hash *htbl;
1325 struct ipoib_neigh *neigh;
1328 htbl = rcu_dereference_protected(ntbl->htbl,
1329 lockdep_is_held(&priv->lock));
1335 /* need to add a new neigh, but maybe some other thread succeeded?
1336 * recalc hash, maybe hash resize took place so we do a search
1338 hash_val = ipoib_addr_hash(htbl, daddr);
1339 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
1340 lockdep_is_held(&priv->lock));
1342 neigh = rcu_dereference_protected(neigh->hnext,
1343 lockdep_is_held(&priv->lock))) {
1344 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1345 /* found, take one ref on behalf of the caller */
1346 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1351 neigh->alive = jiffies;
1356 neigh = ipoib_neigh_ctor(daddr, dev);
1360 /* one ref on behalf of the hash table */
1361 atomic_inc(&neigh->refcnt);
1362 neigh->alive = jiffies;
1364 rcu_assign_pointer(neigh->hnext,
1365 rcu_dereference_protected(htbl->buckets[hash_val],
1366 lockdep_is_held(&priv->lock)));
1367 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1368 atomic_inc(&ntbl->entries);
1375 void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1377 /* neigh reference count was dropprd to zero */
1378 struct net_device *dev = neigh->dev;
1379 struct ipoib_dev_priv *priv = netdev_priv(dev);
1380 struct sk_buff *skb;
1382 ipoib_put_ah(neigh->ah);
1383 while ((skb = __skb_dequeue(&neigh->queue))) {
1384 ++dev->stats.tx_dropped;
1385 dev_kfree_skb_any(skb);
1387 if (ipoib_cm_get(neigh))
1388 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
1389 ipoib_dbg(netdev_priv(dev),
1390 "neigh free for %06x %pI6\n",
1391 IPOIB_QPN(neigh->daddr),
1394 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1395 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1396 complete(&priv->ntbl.flushed);
1400 static void ipoib_neigh_reclaim(struct rcu_head *rp)
1402 /* Called as a result of removal from hash table */
1403 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1404 /* note TX context may hold another ref */
1405 ipoib_neigh_put(neigh);
1408 void ipoib_neigh_free(struct ipoib_neigh *neigh)
1410 struct net_device *dev = neigh->dev;
1411 struct ipoib_dev_priv *priv = netdev_priv(dev);
1412 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1413 struct ipoib_neigh_hash *htbl;
1414 struct ipoib_neigh __rcu **np;
1415 struct ipoib_neigh *n;
1418 htbl = rcu_dereference_protected(ntbl->htbl,
1419 lockdep_is_held(&priv->lock));
1423 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1424 np = &htbl->buckets[hash_val];
1425 for (n = rcu_dereference_protected(*np,
1426 lockdep_is_held(&priv->lock));
1428 n = rcu_dereference_protected(*np,
1429 lockdep_is_held(&priv->lock))) {
1432 rcu_assign_pointer(*np,
1433 rcu_dereference_protected(neigh->hnext,
1434 lockdep_is_held(&priv->lock)));
1435 /* remove from parent list */
1436 list_del_init(&neigh->list);
1437 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1445 static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1447 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1448 struct ipoib_neigh_hash *htbl;
1449 struct ipoib_neigh __rcu **buckets;
1452 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1454 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1457 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1458 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1459 buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1465 htbl->mask = (size - 1);
1466 htbl->buckets = buckets;
1467 RCU_INIT_POINTER(ntbl->htbl, htbl);
1469 atomic_set(&ntbl->entries, 0);
1471 /* start garbage collection */
1472 clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1473 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1474 arp_tbl.gc_interval);
1479 static void neigh_hash_free_rcu(struct rcu_head *head)
1481 struct ipoib_neigh_hash *htbl = container_of(head,
1482 struct ipoib_neigh_hash,
1484 struct ipoib_neigh __rcu **buckets = htbl->buckets;
1485 struct ipoib_neigh_table *ntbl = htbl->ntbl;
1489 complete(&ntbl->deleted);
1492 void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1494 struct ipoib_dev_priv *priv = netdev_priv(dev);
1495 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1496 struct ipoib_neigh_hash *htbl;
1497 unsigned long flags;
1500 /* remove all neigh connected to a given path or mcast */
1501 spin_lock_irqsave(&priv->lock, flags);
1503 htbl = rcu_dereference_protected(ntbl->htbl,
1504 lockdep_is_held(&priv->lock));
1509 for (i = 0; i < htbl->size; i++) {
1510 struct ipoib_neigh *neigh;
1511 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1513 while ((neigh = rcu_dereference_protected(*np,
1514 lockdep_is_held(&priv->lock))) != NULL) {
1515 /* delete neighs belong to this parent */
1516 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1517 rcu_assign_pointer(*np,
1518 rcu_dereference_protected(neigh->hnext,
1519 lockdep_is_held(&priv->lock)));
1520 /* remove from parent list */
1521 list_del_init(&neigh->list);
1522 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1530 spin_unlock_irqrestore(&priv->lock, flags);
1533 static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1535 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1536 struct ipoib_neigh_hash *htbl;
1537 unsigned long flags;
1538 int i, wait_flushed = 0;
1540 init_completion(&priv->ntbl.flushed);
1542 spin_lock_irqsave(&priv->lock, flags);
1544 htbl = rcu_dereference_protected(ntbl->htbl,
1545 lockdep_is_held(&priv->lock));
1549 wait_flushed = atomic_read(&priv->ntbl.entries);
1553 for (i = 0; i < htbl->size; i++) {
1554 struct ipoib_neigh *neigh;
1555 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1557 while ((neigh = rcu_dereference_protected(*np,
1558 lockdep_is_held(&priv->lock))) != NULL) {
1559 rcu_assign_pointer(*np,
1560 rcu_dereference_protected(neigh->hnext,
1561 lockdep_is_held(&priv->lock)));
1562 /* remove from path/mc list */
1563 list_del_init(&neigh->list);
1564 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1569 rcu_assign_pointer(ntbl->htbl, NULL);
1570 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1573 spin_unlock_irqrestore(&priv->lock, flags);
1575 wait_for_completion(&priv->ntbl.flushed);
1578 static void ipoib_neigh_hash_uninit(struct net_device *dev)
1580 struct ipoib_dev_priv *priv = netdev_priv(dev);
1583 ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
1584 init_completion(&priv->ntbl.deleted);
1585 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1587 /* Stop GC if called at init fail need to cancel work */
1588 stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1590 cancel_delayed_work(&priv->neigh_reap_task);
1592 ipoib_flush_neighs(priv);
1594 wait_for_completion(&priv->ntbl.deleted);
1598 int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1600 struct ipoib_dev_priv *priv = netdev_priv(dev);
1602 /* Allocate RX/TX "rings" to hold queued skbs */
1603 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
1605 if (!priv->rx_ring) {
1606 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
1607 ca->name, ipoib_recvq_size);
1611 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
1612 if (!priv->tx_ring) {
1613 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
1614 ca->name, ipoib_sendq_size);
1615 goto out_rx_ring_cleanup;
1618 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
1620 if (ipoib_ib_dev_init(dev, ca, port))
1621 goto out_tx_ring_cleanup;
1624 * Must be after ipoib_ib_dev_init so we can allocate a per
1625 * device wq there and use it here
1627 if (ipoib_neigh_hash_init(priv) < 0)
1628 goto out_dev_uninit;
1633 ipoib_ib_dev_cleanup(dev);
1635 out_tx_ring_cleanup:
1636 vfree(priv->tx_ring);
1638 out_rx_ring_cleanup:
1639 kfree(priv->rx_ring);
1645 void ipoib_dev_cleanup(struct net_device *dev)
1647 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
1652 /* Delete any child interfaces first */
1653 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
1654 /* Stop GC on child */
1655 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1656 cancel_delayed_work(&cpriv->neigh_reap_task);
1657 unregister_netdevice_queue(cpriv->dev, &head);
1659 unregister_netdevice_many(&head);
1662 * Must be before ipoib_ib_dev_cleanup or we delete an in use
1665 ipoib_neigh_hash_uninit(dev);
1667 ipoib_ib_dev_cleanup(dev);
1669 kfree(priv->rx_ring);
1670 vfree(priv->tx_ring);
1672 priv->rx_ring = NULL;
1673 priv->tx_ring = NULL;
1676 static const struct header_ops ipoib_header_ops = {
1677 .create = ipoib_hard_header,
1680 static const struct net_device_ops ipoib_netdev_ops = {
1681 .ndo_uninit = ipoib_uninit,
1682 .ndo_open = ipoib_open,
1683 .ndo_stop = ipoib_stop,
1684 .ndo_change_mtu = ipoib_change_mtu,
1685 .ndo_fix_features = ipoib_fix_features,
1686 .ndo_start_xmit = ipoib_start_xmit,
1687 .ndo_tx_timeout = ipoib_timeout,
1688 .ndo_set_rx_mode = ipoib_set_mcast_list,
1689 .ndo_get_iflink = ipoib_get_iflink,
1692 void ipoib_setup(struct net_device *dev)
1694 struct ipoib_dev_priv *priv = netdev_priv(dev);
1696 dev->netdev_ops = &ipoib_netdev_ops;
1697 dev->header_ops = &ipoib_header_ops;
1699 ipoib_set_ethtool_ops(dev);
1701 netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT);
1703 dev->watchdog_timeo = HZ;
1705 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1707 dev->hard_header_len = IPOIB_HARD_LEN;
1708 dev->addr_len = INFINIBAND_ALEN;
1709 dev->type = ARPHRD_INFINIBAND;
1710 dev->tx_queue_len = ipoib_sendq_size * 2;
1711 dev->features = (NETIF_F_VLAN_CHALLENGED |
1713 netif_keep_dst(dev);
1715 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
1719 spin_lock_init(&priv->lock);
1721 init_rwsem(&priv->vlan_rwsem);
1723 INIT_LIST_HEAD(&priv->path_list);
1724 INIT_LIST_HEAD(&priv->child_intfs);
1725 INIT_LIST_HEAD(&priv->dead_ahs);
1726 INIT_LIST_HEAD(&priv->multicast_list);
1728 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
1729 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
1730 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
1731 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
1732 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
1733 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1734 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
1735 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
1738 struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
1740 struct net_device *dev;
1742 dev = alloc_netdev((int)sizeof(struct ipoib_dev_priv), name,
1743 NET_NAME_UNKNOWN, ipoib_setup);
1747 return netdev_priv(dev);
1750 static ssize_t show_pkey(struct device *dev,
1751 struct device_attribute *attr, char *buf)
1753 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1755 return sprintf(buf, "0x%04x\n", priv->pkey);
1757 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1759 static ssize_t show_umcast(struct device *dev,
1760 struct device_attribute *attr, char *buf)
1762 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1764 return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1767 void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
1769 struct ipoib_dev_priv *priv = netdev_priv(ndev);
1771 if (umcast_val > 0) {
1772 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1773 ipoib_warn(priv, "ignoring multicast groups joined directly "
1776 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1779 static ssize_t set_umcast(struct device *dev,
1780 struct device_attribute *attr,
1781 const char *buf, size_t count)
1783 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1785 ipoib_set_umcast(to_net_dev(dev), umcast_val);
1789 static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1791 int ipoib_add_umcast_attr(struct net_device *dev)
1793 return device_create_file(&dev->dev, &dev_attr_umcast);
1796 static ssize_t create_child(struct device *dev,
1797 struct device_attribute *attr,
1798 const char *buf, size_t count)
1803 if (sscanf(buf, "%i", &pkey) != 1)
1806 if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
1810 * Set the full membership bit, so that we join the right
1811 * broadcast group, etc.
1815 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
1817 return ret ? ret : count;
1819 static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
1821 static ssize_t delete_child(struct device *dev,
1822 struct device_attribute *attr,
1823 const char *buf, size_t count)
1828 if (sscanf(buf, "%i", &pkey) != 1)
1831 if (pkey < 0 || pkey > 0xffff)
1834 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
1836 return ret ? ret : count;
1839 static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
1841 int ipoib_add_pkey_attr(struct net_device *dev)
1843 return device_create_file(&dev->dev, &dev_attr_pkey);
1846 int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
1848 struct ib_device_attr *device_attr;
1849 int result = -ENOMEM;
1851 device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
1853 printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
1854 hca->name, sizeof *device_attr);
1858 result = ib_query_device(hca, device_attr);
1860 printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
1865 priv->hca_caps = device_attr->device_cap_flags;
1869 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
1870 priv->dev->hw_features = NETIF_F_SG |
1871 NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1873 if (priv->hca_caps & IB_DEVICE_UD_TSO)
1874 priv->dev->hw_features |= NETIF_F_TSO;
1876 priv->dev->features |= priv->dev->hw_features;
1882 static struct net_device *ipoib_add_port(const char *format,
1883 struct ib_device *hca, u8 port)
1885 struct ipoib_dev_priv *priv;
1886 struct ib_port_attr attr;
1887 int result = -ENOMEM;
1889 priv = ipoib_intf_alloc(format);
1891 goto alloc_mem_failed;
1893 SET_NETDEV_DEV(priv->dev, hca->dma_device);
1894 priv->dev->dev_id = port - 1;
1896 result = ib_query_port(hca, port, &attr);
1898 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1900 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
1902 goto device_init_failed;
1905 /* MTU will be reset when mcast join happens */
1906 priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
1907 priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
1909 priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
1911 result = ib_query_pkey(hca, port, 0, &priv->pkey);
1913 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1914 hca->name, port, result);
1915 goto device_init_failed;
1918 result = ipoib_set_dev_features(priv, hca);
1920 goto device_init_failed;
1923 * Set the full membership bit, so that we join the right
1924 * broadcast group, etc.
1926 priv->pkey |= 0x8000;
1928 priv->dev->broadcast[8] = priv->pkey >> 8;
1929 priv->dev->broadcast[9] = priv->pkey & 0xff;
1931 result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
1933 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1934 hca->name, port, result);
1935 goto device_init_failed;
1937 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1939 result = ipoib_dev_init(priv->dev, hca, port);
1941 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1942 hca->name, port, result);
1943 goto device_init_failed;
1946 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1947 priv->ca, ipoib_event);
1948 result = ib_register_event_handler(&priv->event_handler);
1950 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1951 "port %d (ret = %d)\n",
1952 hca->name, port, result);
1956 /* call event handler to ensure pkey in sync */
1957 queue_work(ipoib_workqueue, &priv->flush_heavy);
1959 result = register_netdev(priv->dev);
1961 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1962 hca->name, port, result);
1963 goto register_failed;
1966 if (ipoib_cm_add_mode_attr(priv->dev))
1968 if (ipoib_add_pkey_attr(priv->dev))
1970 if (ipoib_add_umcast_attr(priv->dev))
1972 if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
1974 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
1980 unregister_netdev(priv->dev);
1983 ib_unregister_event_handler(&priv->event_handler);
1984 flush_workqueue(ipoib_workqueue);
1985 /* Stop GC if started before flush */
1986 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1987 cancel_delayed_work(&priv->neigh_reap_task);
1988 flush_workqueue(priv->wq);
1991 ipoib_dev_cleanup(priv->dev);
1994 free_netdev(priv->dev);
1997 return ERR_PTR(result);
2000 static void ipoib_add_one(struct ib_device *device)
2002 struct list_head *dev_list;
2003 struct net_device *dev;
2004 struct ipoib_dev_priv *priv;
2008 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
2012 INIT_LIST_HEAD(dev_list);
2014 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
2015 if (!rdma_protocol_ib(device, p))
2017 dev = ipoib_add_port("ib%d", device, p);
2019 priv = netdev_priv(dev);
2020 list_add_tail(&priv->list, dev_list);
2030 ib_set_client_data(device, &ipoib_client, dev_list);
2033 static void ipoib_remove_one(struct ib_device *device, void *client_data)
2035 struct ipoib_dev_priv *priv, *tmp;
2036 struct list_head *dev_list = client_data;
2041 list_for_each_entry_safe(priv, tmp, dev_list, list) {
2042 ib_unregister_event_handler(&priv->event_handler);
2043 flush_workqueue(ipoib_workqueue);
2046 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
2050 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
2051 cancel_delayed_work(&priv->neigh_reap_task);
2052 flush_workqueue(priv->wq);
2054 unregister_netdev(priv->dev);
2055 free_netdev(priv->dev);
2061 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2062 static struct notifier_block ipoib_netdev_notifier = {
2063 .notifier_call = ipoib_netdev_event,
2067 static int __init ipoib_init_module(void)
2071 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
2072 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
2073 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
2075 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
2076 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
2077 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
2078 #ifdef CONFIG_INFINIBAND_IPOIB_CM
2079 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
2083 * When copying small received packets, we only copy from the
2084 * linear data part of the SKB, so we rely on this condition.
2086 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
2088 ret = ipoib_register_debugfs();
2093 * We create a global workqueue here that is used for all flush
2094 * operations. However, if you attempt to flush a workqueue
2095 * from a task on that same workqueue, it deadlocks the system.
2096 * We want to be able to flush the tasks associated with a
2097 * specific net device, so we also create a workqueue for each
2098 * netdevice. We queue up the tasks for that device only on
2099 * its private workqueue, and we only queue up flush events
2100 * on our global flush workqueue. This avoids the deadlocks.
2102 ipoib_workqueue = create_singlethread_workqueue("ipoib_flush");
2103 if (!ipoib_workqueue) {
2108 ib_sa_register_client(&ipoib_sa_client);
2110 ret = ib_register_client(&ipoib_client);
2114 ret = ipoib_netlink_init();
2118 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2119 register_netdevice_notifier(&ipoib_netdev_notifier);
2124 ib_unregister_client(&ipoib_client);
2127 ib_sa_unregister_client(&ipoib_sa_client);
2128 destroy_workqueue(ipoib_workqueue);
2131 ipoib_unregister_debugfs();
2136 static void __exit ipoib_cleanup_module(void)
2138 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2139 unregister_netdevice_notifier(&ipoib_netdev_notifier);
2141 ipoib_netlink_fini();
2142 ib_unregister_client(&ipoib_client);
2143 ib_sa_unregister_client(&ipoib_sa_client);
2144 ipoib_unregister_debugfs();
2145 destroy_workqueue(ipoib_workqueue);
2148 module_init(ipoib_init_module);
2149 module_exit(ipoib_cleanup_module);