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>
54 #include <linux/pci.h>
56 #define DRV_VERSION "1.0.0"
58 const char ipoib_driver_version[] = DRV_VERSION;
60 MODULE_AUTHOR("Roland Dreier");
61 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
62 MODULE_LICENSE("Dual BSD/GPL");
63 MODULE_VERSION(DRV_VERSION);
65 int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
66 int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
68 module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
69 MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
70 module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
71 MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
73 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
74 int ipoib_debug_level;
76 module_param_named(debug_level, ipoib_debug_level, int, 0644);
77 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
80 struct ipoib_path_iter {
81 struct net_device *dev;
82 struct ipoib_path path;
85 static const u8 ipv4_bcast_addr[] = {
86 0x00, 0xff, 0xff, 0xff,
87 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
88 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
91 struct workqueue_struct *ipoib_workqueue;
93 struct ib_sa_client ipoib_sa_client;
95 static void ipoib_add_one(struct ib_device *device);
96 static void ipoib_remove_one(struct ib_device *device, void *client_data);
97 static void ipoib_neigh_reclaim(struct rcu_head *rp);
98 static struct net_device *ipoib_get_net_dev_by_params(
99 struct ib_device *dev, u8 port, u16 pkey,
100 const union ib_gid *gid, const struct sockaddr *addr,
102 static int ipoib_set_mac(struct net_device *dev, void *addr);
104 static struct ib_client ipoib_client = {
106 .add = ipoib_add_one,
107 .remove = ipoib_remove_one,
108 .get_net_dev_by_params = ipoib_get_net_dev_by_params,
111 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
112 static int ipoib_netdev_event(struct notifier_block *this,
113 unsigned long event, void *ptr)
115 struct netdev_notifier_info *ni = ptr;
116 struct net_device *dev = ni->dev;
118 if (dev->netdev_ops->ndo_open != ipoib_open)
122 case NETDEV_REGISTER:
123 ipoib_create_debug_files(dev);
125 case NETDEV_CHANGENAME:
126 ipoib_delete_debug_files(dev);
127 ipoib_create_debug_files(dev);
129 case NETDEV_UNREGISTER:
130 ipoib_delete_debug_files(dev);
138 int ipoib_open(struct net_device *dev)
140 struct ipoib_dev_priv *priv = netdev_priv(dev);
142 ipoib_dbg(priv, "bringing up interface\n");
144 netif_carrier_off(dev);
146 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
148 priv->sm_fullmember_sendonly_support = false;
150 if (ipoib_ib_dev_open(dev)) {
151 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
156 if (ipoib_ib_dev_up(dev))
159 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
160 struct ipoib_dev_priv *cpriv;
162 /* Bring up any child interfaces too */
163 down_read(&priv->vlan_rwsem);
164 list_for_each_entry(cpriv, &priv->child_intfs, list) {
167 flags = cpriv->dev->flags;
171 dev_change_flags(cpriv->dev, flags | IFF_UP);
173 up_read(&priv->vlan_rwsem);
176 netif_start_queue(dev);
181 ipoib_ib_dev_stop(dev);
184 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
189 static int ipoib_stop(struct net_device *dev)
191 struct ipoib_dev_priv *priv = netdev_priv(dev);
193 ipoib_dbg(priv, "stopping interface\n");
195 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
197 netif_stop_queue(dev);
199 ipoib_ib_dev_down(dev);
200 ipoib_ib_dev_stop(dev);
202 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
203 struct ipoib_dev_priv *cpriv;
205 /* Bring down any child interfaces too */
206 down_read(&priv->vlan_rwsem);
207 list_for_each_entry(cpriv, &priv->child_intfs, list) {
210 flags = cpriv->dev->flags;
211 if (!(flags & IFF_UP))
214 dev_change_flags(cpriv->dev, flags & ~IFF_UP);
216 up_read(&priv->vlan_rwsem);
222 static void ipoib_uninit(struct net_device *dev)
224 ipoib_dev_cleanup(dev);
227 static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
229 struct ipoib_dev_priv *priv = netdev_priv(dev);
231 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
232 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
237 static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
239 struct ipoib_dev_priv *priv = netdev_priv(dev);
241 /* dev->mtu > 2K ==> connected mode */
242 if (ipoib_cm_admin_enabled(dev)) {
243 if (new_mtu > ipoib_cm_max_mtu(dev))
246 if (new_mtu > priv->mcast_mtu)
247 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
254 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
257 priv->admin_mtu = new_mtu;
259 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
264 /* Called with an RCU read lock taken */
265 static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr,
266 struct net_device *dev)
268 struct net *net = dev_net(dev);
269 struct in_device *in_dev;
270 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
271 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
274 switch (addr->sa_family) {
276 in_dev = in_dev_get(dev);
280 ret_addr = inet_confirm_addr(net, in_dev, 0,
281 addr_in->sin_addr.s_addr,
289 if (IS_ENABLED(CONFIG_IPV6) &&
290 ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1))
299 * Find the master net_device on top of the given net_device.
300 * @dev: base IPoIB net_device
302 * Returns the master net_device with a reference held, or the same net_device
303 * if no master exists.
305 static struct net_device *ipoib_get_master_net_dev(struct net_device *dev)
307 struct net_device *master;
310 master = netdev_master_upper_dev_get_rcu(dev);
323 * Find a net_device matching the given address, which is an upper device of
324 * the given net_device.
325 * @addr: IP address to look for.
326 * @dev: base IPoIB net_device
328 * If found, returns the net_device with a reference held. Otherwise return
331 static struct net_device *ipoib_get_net_dev_match_addr(
332 const struct sockaddr *addr, struct net_device *dev)
334 struct net_device *upper,
336 struct list_head *iter;
339 if (ipoib_is_dev_match_addr_rcu(addr, dev)) {
345 netdev_for_each_all_upper_dev_rcu(dev, upper, iter) {
346 if (ipoib_is_dev_match_addr_rcu(addr, upper)) {
357 /* returns the number of IPoIB netdevs on top a given ipoib device matching a
358 * pkey_index and address, if one exists.
360 * @found_net_dev: contains a matching net_device if the return value >= 1,
361 * with a reference held. */
362 static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
363 const union ib_gid *gid,
365 const struct sockaddr *addr,
367 struct net_device **found_net_dev)
369 struct ipoib_dev_priv *child_priv;
370 struct net_device *net_dev = NULL;
373 if (priv->pkey_index == pkey_index &&
374 (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
376 net_dev = ipoib_get_master_net_dev(priv->dev);
378 /* Verify the net_device matches the IP address, as
379 * IPoIB child devices currently share a GID. */
380 net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev);
384 *found_net_dev = net_dev;
391 /* Check child interfaces */
392 down_read_nested(&priv->vlan_rwsem, nesting);
393 list_for_each_entry(child_priv, &priv->child_intfs, list) {
394 matches += ipoib_match_gid_pkey_addr(child_priv, gid,
401 up_read(&priv->vlan_rwsem);
406 /* Returns the number of matching net_devs found (between 0 and 2). Also
407 * return the matching net_device in the @net_dev parameter, holding a
408 * reference to the net_device, if the number of matches >= 1 */
409 static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port,
411 const union ib_gid *gid,
412 const struct sockaddr *addr,
413 struct net_device **net_dev)
415 struct ipoib_dev_priv *priv;
420 list_for_each_entry(priv, dev_list, list) {
421 if (priv->port != port)
424 matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
433 static struct net_device *ipoib_get_net_dev_by_params(
434 struct ib_device *dev, u8 port, u16 pkey,
435 const union ib_gid *gid, const struct sockaddr *addr,
438 struct net_device *net_dev;
439 struct list_head *dev_list = client_data;
444 if (!rdma_protocol_ib(dev, port))
447 ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
454 /* See if we can find a unique device matching the L2 parameters */
455 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
456 gid, NULL, &net_dev);
467 /* Couldn't find a unique device with L2 parameters only. Use L3
468 * address to uniquely match the net device */
469 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
470 gid, addr, &net_dev);
475 dev_warn_ratelimited(&dev->dev,
476 "duplicate IP address detected\n");
483 int ipoib_set_mode(struct net_device *dev, const char *buf)
485 struct ipoib_dev_priv *priv = netdev_priv(dev);
487 /* flush paths if we switch modes so that connections are restarted */
488 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
489 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
490 ipoib_warn(priv, "enabling connected mode "
491 "will cause multicast packet drops\n");
492 netdev_update_features(dev);
493 dev_set_mtu(dev, ipoib_cm_max_mtu(dev));
495 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
497 ipoib_flush_paths(dev);
498 return (!rtnl_trylock()) ? -EBUSY : 0;
501 if (!strcmp(buf, "datagram\n")) {
502 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
503 netdev_update_features(dev);
504 dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
506 ipoib_flush_paths(dev);
507 return (!rtnl_trylock()) ? -EBUSY : 0;
513 struct ipoib_path *__path_find(struct net_device *dev, void *gid)
515 struct ipoib_dev_priv *priv = netdev_priv(dev);
516 struct rb_node *n = priv->path_tree.rb_node;
517 struct ipoib_path *path;
521 path = rb_entry(n, struct ipoib_path, rb_node);
523 ret = memcmp(gid, path->pathrec.dgid.raw,
524 sizeof (union ib_gid));
537 static int __path_add(struct net_device *dev, struct ipoib_path *path)
539 struct ipoib_dev_priv *priv = netdev_priv(dev);
540 struct rb_node **n = &priv->path_tree.rb_node;
541 struct rb_node *pn = NULL;
542 struct ipoib_path *tpath;
547 tpath = rb_entry(pn, struct ipoib_path, rb_node);
549 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
550 sizeof (union ib_gid));
559 rb_link_node(&path->rb_node, pn, n);
560 rb_insert_color(&path->rb_node, &priv->path_tree);
562 list_add_tail(&path->list, &priv->path_list);
567 static void path_free(struct net_device *dev, struct ipoib_path *path)
571 while ((skb = __skb_dequeue(&path->queue)))
572 dev_kfree_skb_irq(skb);
574 ipoib_dbg(netdev_priv(dev), "path_free\n");
576 /* remove all neigh connected to this path */
577 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
580 ipoib_put_ah(path->ah);
585 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
587 struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
589 struct ipoib_path_iter *iter;
591 iter = kmalloc(sizeof *iter, GFP_KERNEL);
596 memset(iter->path.pathrec.dgid.raw, 0, 16);
598 if (ipoib_path_iter_next(iter)) {
606 int ipoib_path_iter_next(struct ipoib_path_iter *iter)
608 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
610 struct ipoib_path *path;
613 spin_lock_irq(&priv->lock);
615 n = rb_first(&priv->path_tree);
618 path = rb_entry(n, struct ipoib_path, rb_node);
620 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
621 sizeof (union ib_gid)) < 0) {
630 spin_unlock_irq(&priv->lock);
635 void ipoib_path_iter_read(struct ipoib_path_iter *iter,
636 struct ipoib_path *path)
641 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
643 void ipoib_mark_paths_invalid(struct net_device *dev)
645 struct ipoib_dev_priv *priv = netdev_priv(dev);
646 struct ipoib_path *path, *tp;
648 spin_lock_irq(&priv->lock);
650 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
651 ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
652 be16_to_cpu(path->pathrec.dlid),
653 path->pathrec.dgid.raw);
657 spin_unlock_irq(&priv->lock);
660 struct classport_info_context {
661 struct ipoib_dev_priv *priv;
662 struct completion done;
663 struct ib_sa_query *sa_query;
666 static void classport_info_query_cb(int status, struct ib_class_port_info *rec,
669 struct classport_info_context *cb_ctx = context;
670 struct ipoib_dev_priv *priv;
676 if (status || !rec) {
677 pr_debug("device: %s failed query classport_info status: %d\n",
678 priv->dev->name, status);
679 /* keeps the default, will try next mcast_restart */
680 priv->sm_fullmember_sendonly_support = false;
684 if (ib_get_cpi_capmask2(rec) &
685 IB_SA_CAP_MASK2_SENDONLY_FULL_MEM_SUPPORT) {
686 pr_debug("device: %s enabled fullmember-sendonly for sendonly MCG\n",
688 priv->sm_fullmember_sendonly_support = true;
690 pr_debug("device: %s disabled fullmember-sendonly for sendonly MCG\n",
692 priv->sm_fullmember_sendonly_support = false;
696 complete(&cb_ctx->done);
699 int ipoib_check_sm_sendonly_fullmember_support(struct ipoib_dev_priv *priv)
701 struct classport_info_context *callback_context;
704 callback_context = kmalloc(sizeof(*callback_context), GFP_KERNEL);
705 if (!callback_context)
708 callback_context->priv = priv;
709 init_completion(&callback_context->done);
711 ret = ib_sa_classport_info_rec_query(&ipoib_sa_client,
712 priv->ca, priv->port, 3000,
714 classport_info_query_cb,
716 &callback_context->sa_query);
718 pr_info("%s failed to send ib_sa_classport_info query, ret: %d\n",
719 priv->dev->name, ret);
720 kfree(callback_context);
724 /* waiting for the callback to finish before returnning */
725 wait_for_completion(&callback_context->done);
726 kfree(callback_context);
731 static void push_pseudo_header(struct sk_buff *skb, const char *daddr)
733 struct ipoib_pseudo_header *phdr;
735 phdr = (struct ipoib_pseudo_header *)skb_push(skb, sizeof(*phdr));
736 memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
739 void ipoib_flush_paths(struct net_device *dev)
741 struct ipoib_dev_priv *priv = netdev_priv(dev);
742 struct ipoib_path *path, *tp;
743 LIST_HEAD(remove_list);
746 netif_tx_lock_bh(dev);
747 spin_lock_irqsave(&priv->lock, flags);
749 list_splice_init(&priv->path_list, &remove_list);
751 list_for_each_entry(path, &remove_list, list)
752 rb_erase(&path->rb_node, &priv->path_tree);
754 list_for_each_entry_safe(path, tp, &remove_list, list) {
756 ib_sa_cancel_query(path->query_id, path->query);
757 spin_unlock_irqrestore(&priv->lock, flags);
758 netif_tx_unlock_bh(dev);
759 wait_for_completion(&path->done);
760 path_free(dev, path);
761 netif_tx_lock_bh(dev);
762 spin_lock_irqsave(&priv->lock, flags);
765 spin_unlock_irqrestore(&priv->lock, flags);
766 netif_tx_unlock_bh(dev);
769 static void path_rec_completion(int status,
770 struct ib_sa_path_rec *pathrec,
773 struct ipoib_path *path = path_ptr;
774 struct net_device *dev = path->dev;
775 struct ipoib_dev_priv *priv = netdev_priv(dev);
776 struct ipoib_ah *ah = NULL;
777 struct ipoib_ah *old_ah = NULL;
778 struct ipoib_neigh *neigh, *tn;
779 struct sk_buff_head skqueue;
784 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
785 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
787 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
788 status, path->pathrec.dgid.raw);
790 skb_queue_head_init(&skqueue);
793 struct ib_ah_attr av;
795 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
796 ah = ipoib_create_ah(dev, priv->pd, &av);
799 spin_lock_irqsave(&priv->lock, flags);
801 if (!IS_ERR_OR_NULL(ah)) {
803 * pathrec.dgid is used as the database key from the LLADDR,
804 * it must remain unchanged even if the SA returns a different
805 * GID to use in the AH.
807 if (memcmp(pathrec->dgid.raw, path->pathrec.dgid.raw,
808 sizeof(union ib_gid))) {
811 "%s got PathRec for gid %pI6 while asked for %pI6\n",
812 dev->name, pathrec->dgid.raw,
813 path->pathrec.dgid.raw);
814 memcpy(pathrec->dgid.raw, path->pathrec.dgid.raw,
815 sizeof(union ib_gid));
818 path->pathrec = *pathrec;
823 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
824 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
826 while ((skb = __skb_dequeue(&path->queue)))
827 __skb_queue_tail(&skqueue, skb);
829 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
831 WARN_ON(neigh->ah != old_ah);
833 * Dropping the ah reference inside
834 * priv->lock is safe here, because we
835 * will hold one more reference from
836 * the original value of path->ah (ie
839 ipoib_put_ah(neigh->ah);
841 kref_get(&path->ah->ref);
842 neigh->ah = path->ah;
844 if (ipoib_cm_enabled(dev, neigh->daddr)) {
845 if (!ipoib_cm_get(neigh))
846 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
849 if (!ipoib_cm_get(neigh)) {
850 ipoib_neigh_free(neigh);
855 while ((skb = __skb_dequeue(&neigh->queue)))
856 __skb_queue_tail(&skqueue, skb);
862 complete(&path->done);
864 spin_unlock_irqrestore(&priv->lock, flags);
866 if (IS_ERR_OR_NULL(ah))
867 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
870 ipoib_put_ah(old_ah);
872 while ((skb = __skb_dequeue(&skqueue))) {
874 if (dev_queue_xmit(skb))
875 ipoib_warn(priv, "dev_queue_xmit failed "
876 "to requeue packet\n");
880 static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
882 struct ipoib_dev_priv *priv = netdev_priv(dev);
883 struct ipoib_path *path;
885 if (!priv->broadcast)
888 path = kzalloc(sizeof *path, GFP_ATOMIC);
894 skb_queue_head_init(&path->queue);
896 INIT_LIST_HEAD(&path->neigh_list);
898 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
899 path->pathrec.sgid = priv->local_gid;
900 path->pathrec.pkey = cpu_to_be16(priv->pkey);
901 path->pathrec.numb_path = 1;
902 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
907 static int path_rec_start(struct net_device *dev,
908 struct ipoib_path *path)
910 struct ipoib_dev_priv *priv = netdev_priv(dev);
912 ipoib_dbg(priv, "Start path record lookup for %pI6\n",
913 path->pathrec.dgid.raw);
915 init_completion(&path->done);
918 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
920 IB_SA_PATH_REC_DGID |
921 IB_SA_PATH_REC_SGID |
922 IB_SA_PATH_REC_NUMB_PATH |
923 IB_SA_PATH_REC_TRAFFIC_CLASS |
928 if (path->query_id < 0) {
929 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
931 complete(&path->done);
932 return path->query_id;
938 static struct ipoib_neigh *neigh_add_path(struct sk_buff *skb, u8 *daddr,
939 struct net_device *dev)
941 struct ipoib_dev_priv *priv = netdev_priv(dev);
942 struct ipoib_path *path;
943 struct ipoib_neigh *neigh;
946 spin_lock_irqsave(&priv->lock, flags);
947 neigh = ipoib_neigh_alloc(daddr, dev);
949 spin_unlock_irqrestore(&priv->lock, flags);
950 ++dev->stats.tx_dropped;
951 dev_kfree_skb_any(skb);
955 /* To avoid race condition, make sure that the
956 * neigh will be added only once.
958 if (unlikely(!list_empty(&neigh->list))) {
959 spin_unlock_irqrestore(&priv->lock, flags);
963 path = __path_find(dev, daddr + 4);
965 path = path_rec_create(dev, daddr + 4);
969 __path_add(dev, path);
972 list_add_tail(&neigh->list, &path->neigh_list);
975 kref_get(&path->ah->ref);
976 neigh->ah = path->ah;
978 if (ipoib_cm_enabled(dev, neigh->daddr)) {
979 if (!ipoib_cm_get(neigh))
980 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
981 if (!ipoib_cm_get(neigh)) {
982 ipoib_neigh_free(neigh);
985 if (skb_queue_len(&neigh->queue) <
986 IPOIB_MAX_PATH_REC_QUEUE) {
987 push_pseudo_header(skb, neigh->daddr);
988 __skb_queue_tail(&neigh->queue, skb);
990 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
991 skb_queue_len(&neigh->queue));
995 spin_unlock_irqrestore(&priv->lock, flags);
996 ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
997 ipoib_neigh_put(neigh);
1003 if (!path->query && path_rec_start(dev, path))
1005 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1006 push_pseudo_header(skb, neigh->daddr);
1007 __skb_queue_tail(&neigh->queue, skb);
1013 spin_unlock_irqrestore(&priv->lock, flags);
1014 ipoib_neigh_put(neigh);
1018 ipoib_neigh_free(neigh);
1020 ++dev->stats.tx_dropped;
1021 dev_kfree_skb_any(skb);
1023 spin_unlock_irqrestore(&priv->lock, flags);
1024 ipoib_neigh_put(neigh);
1029 static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
1030 struct ipoib_pseudo_header *phdr)
1032 struct ipoib_dev_priv *priv = netdev_priv(dev);
1033 struct ipoib_path *path;
1034 unsigned long flags;
1036 spin_lock_irqsave(&priv->lock, flags);
1038 path = __path_find(dev, phdr->hwaddr + 4);
1039 if (!path || !path->valid) {
1043 path = path_rec_create(dev, phdr->hwaddr + 4);
1047 if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1048 push_pseudo_header(skb, phdr->hwaddr);
1049 __skb_queue_tail(&path->queue, skb);
1051 ++dev->stats.tx_dropped;
1052 dev_kfree_skb_any(skb);
1055 if (!path->query && path_rec_start(dev, path)) {
1056 spin_unlock_irqrestore(&priv->lock, flags);
1058 path_free(dev, path);
1061 __path_add(dev, path);
1063 ++dev->stats.tx_dropped;
1064 dev_kfree_skb_any(skb);
1067 spin_unlock_irqrestore(&priv->lock, flags);
1072 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
1073 be16_to_cpu(path->pathrec.dlid));
1075 spin_unlock_irqrestore(&priv->lock, flags);
1076 ipoib_send(dev, skb, path->ah, IPOIB_QPN(phdr->hwaddr));
1078 } else if ((path->query || !path_rec_start(dev, path)) &&
1079 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1080 push_pseudo_header(skb, phdr->hwaddr);
1081 __skb_queue_tail(&path->queue, skb);
1083 ++dev->stats.tx_dropped;
1084 dev_kfree_skb_any(skb);
1087 spin_unlock_irqrestore(&priv->lock, flags);
1090 static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
1092 struct ipoib_dev_priv *priv = netdev_priv(dev);
1093 struct ipoib_neigh *neigh;
1094 struct ipoib_pseudo_header *phdr;
1095 struct ipoib_header *header;
1096 unsigned long flags;
1098 phdr = (struct ipoib_pseudo_header *) skb->data;
1099 skb_pull(skb, sizeof(*phdr));
1100 header = (struct ipoib_header *) skb->data;
1102 if (unlikely(phdr->hwaddr[4] == 0xff)) {
1103 /* multicast, arrange "if" according to probability */
1104 if ((header->proto != htons(ETH_P_IP)) &&
1105 (header->proto != htons(ETH_P_IPV6)) &&
1106 (header->proto != htons(ETH_P_ARP)) &&
1107 (header->proto != htons(ETH_P_RARP)) &&
1108 (header->proto != htons(ETH_P_TIPC))) {
1109 /* ethertype not supported by IPoIB */
1110 ++dev->stats.tx_dropped;
1111 dev_kfree_skb_any(skb);
1112 return NETDEV_TX_OK;
1114 /* Add in the P_Key for multicast*/
1115 phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
1116 phdr->hwaddr[9] = priv->pkey & 0xff;
1118 neigh = ipoib_neigh_get(dev, phdr->hwaddr);
1120 goto send_using_neigh;
1121 ipoib_mcast_send(dev, phdr->hwaddr, skb);
1122 return NETDEV_TX_OK;
1125 /* unicast, arrange "switch" according to probability */
1126 switch (header->proto) {
1127 case htons(ETH_P_IP):
1128 case htons(ETH_P_IPV6):
1129 case htons(ETH_P_TIPC):
1130 neigh = ipoib_neigh_get(dev, phdr->hwaddr);
1131 if (unlikely(!neigh)) {
1132 neigh = neigh_add_path(skb, phdr->hwaddr, dev);
1134 return NETDEV_TX_OK;
1137 case htons(ETH_P_ARP):
1138 case htons(ETH_P_RARP):
1139 /* for unicast ARP and RARP should always perform path find */
1140 unicast_arp_send(skb, dev, phdr);
1141 return NETDEV_TX_OK;
1143 /* ethertype not supported by IPoIB */
1144 ++dev->stats.tx_dropped;
1145 dev_kfree_skb_any(skb);
1146 return NETDEV_TX_OK;
1150 /* note we now hold a ref to neigh */
1151 if (ipoib_cm_get(neigh)) {
1152 if (ipoib_cm_up(neigh)) {
1153 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
1156 } else if (neigh->ah) {
1157 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(phdr->hwaddr));
1161 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1162 push_pseudo_header(skb, phdr->hwaddr);
1163 spin_lock_irqsave(&priv->lock, flags);
1164 __skb_queue_tail(&neigh->queue, skb);
1165 spin_unlock_irqrestore(&priv->lock, flags);
1167 ++dev->stats.tx_dropped;
1168 dev_kfree_skb_any(skb);
1172 ipoib_neigh_put(neigh);
1174 return NETDEV_TX_OK;
1177 static void ipoib_timeout(struct net_device *dev)
1179 struct ipoib_dev_priv *priv = netdev_priv(dev);
1181 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
1182 jiffies_to_msecs(jiffies - dev_trans_start(dev)));
1183 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
1184 netif_queue_stopped(dev),
1185 priv->tx_head, priv->tx_tail);
1186 /* XXX reset QP, etc. */
1189 static int ipoib_hard_header(struct sk_buff *skb,
1190 struct net_device *dev,
1191 unsigned short type,
1192 const void *daddr, const void *saddr, unsigned len)
1194 struct ipoib_header *header;
1196 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
1198 header->proto = htons(type);
1199 header->reserved = 0;
1202 * we don't rely on dst_entry structure, always stuff the
1203 * destination address into skb hard header so we can figure out where
1204 * to send the packet later.
1206 push_pseudo_header(skb, daddr);
1208 return IPOIB_HARD_LEN;
1211 static void ipoib_set_mcast_list(struct net_device *dev)
1213 struct ipoib_dev_priv *priv = netdev_priv(dev);
1215 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
1216 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
1220 queue_work(priv->wq, &priv->restart_task);
1223 static int ipoib_get_iflink(const struct net_device *dev)
1225 struct ipoib_dev_priv *priv = netdev_priv(dev);
1227 /* parent interface */
1228 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
1229 return dev->ifindex;
1231 /* child/vlan interface */
1232 return priv->parent->ifindex;
1235 static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
1238 * Use only the address parts that contributes to spreading
1239 * The subnet prefix is not used as one can not connect to
1240 * same remote port (GUID) using the same remote QPN via two
1241 * different subnets.
1243 /* qpn octets[1:4) & port GUID octets[12:20) */
1244 u32 *d32 = (u32 *) daddr;
1247 hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
1248 return hv & htbl->mask;
1251 struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
1253 struct ipoib_dev_priv *priv = netdev_priv(dev);
1254 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1255 struct ipoib_neigh_hash *htbl;
1256 struct ipoib_neigh *neigh = NULL;
1261 htbl = rcu_dereference_bh(ntbl->htbl);
1266 hash_val = ipoib_addr_hash(htbl, daddr);
1267 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
1269 neigh = rcu_dereference_bh(neigh->hnext)) {
1270 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1271 /* found, take one ref on behalf of the caller */
1272 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1278 if (likely(skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE))
1279 neigh->alive = jiffies;
1285 rcu_read_unlock_bh();
1289 static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
1291 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1292 struct ipoib_neigh_hash *htbl;
1293 unsigned long neigh_obsolete;
1295 unsigned long flags;
1297 LIST_HEAD(remove_list);
1299 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1302 spin_lock_irqsave(&priv->lock, flags);
1304 htbl = rcu_dereference_protected(ntbl->htbl,
1305 lockdep_is_held(&priv->lock));
1310 /* neigh is obsolete if it was idle for two GC periods */
1311 dt = 2 * arp_tbl.gc_interval;
1312 neigh_obsolete = jiffies - dt;
1313 /* handle possible race condition */
1314 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1317 for (i = 0; i < htbl->size; i++) {
1318 struct ipoib_neigh *neigh;
1319 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1321 while ((neigh = rcu_dereference_protected(*np,
1322 lockdep_is_held(&priv->lock))) != NULL) {
1323 /* was the neigh idle for two GC periods */
1324 if (time_after(neigh_obsolete, neigh->alive)) {
1326 ipoib_check_and_add_mcast_sendonly(priv, neigh->daddr + 4, &remove_list);
1328 rcu_assign_pointer(*np,
1329 rcu_dereference_protected(neigh->hnext,
1330 lockdep_is_held(&priv->lock)));
1331 /* remove from path/mc list */
1332 list_del_init(&neigh->list);
1333 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1342 spin_unlock_irqrestore(&priv->lock, flags);
1343 ipoib_mcast_remove_list(&remove_list);
1346 static void ipoib_reap_neigh(struct work_struct *work)
1348 struct ipoib_dev_priv *priv =
1349 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
1351 __ipoib_reap_neigh(priv);
1353 if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1354 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1355 arp_tbl.gc_interval);
1359 static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
1360 struct net_device *dev)
1362 struct ipoib_neigh *neigh;
1364 neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
1369 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
1370 skb_queue_head_init(&neigh->queue);
1371 INIT_LIST_HEAD(&neigh->list);
1372 ipoib_cm_set(neigh, NULL);
1373 /* one ref on behalf of the caller */
1374 atomic_set(&neigh->refcnt, 1);
1379 struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
1380 struct net_device *dev)
1382 struct ipoib_dev_priv *priv = netdev_priv(dev);
1383 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1384 struct ipoib_neigh_hash *htbl;
1385 struct ipoib_neigh *neigh;
1388 htbl = rcu_dereference_protected(ntbl->htbl,
1389 lockdep_is_held(&priv->lock));
1395 /* need to add a new neigh, but maybe some other thread succeeded?
1396 * recalc hash, maybe hash resize took place so we do a search
1398 hash_val = ipoib_addr_hash(htbl, daddr);
1399 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
1400 lockdep_is_held(&priv->lock));
1402 neigh = rcu_dereference_protected(neigh->hnext,
1403 lockdep_is_held(&priv->lock))) {
1404 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1405 /* found, take one ref on behalf of the caller */
1406 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1411 neigh->alive = jiffies;
1416 neigh = ipoib_neigh_ctor(daddr, dev);
1420 /* one ref on behalf of the hash table */
1421 atomic_inc(&neigh->refcnt);
1422 neigh->alive = jiffies;
1424 rcu_assign_pointer(neigh->hnext,
1425 rcu_dereference_protected(htbl->buckets[hash_val],
1426 lockdep_is_held(&priv->lock)));
1427 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1428 atomic_inc(&ntbl->entries);
1435 void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1437 /* neigh reference count was dropprd to zero */
1438 struct net_device *dev = neigh->dev;
1439 struct ipoib_dev_priv *priv = netdev_priv(dev);
1440 struct sk_buff *skb;
1442 ipoib_put_ah(neigh->ah);
1443 while ((skb = __skb_dequeue(&neigh->queue))) {
1444 ++dev->stats.tx_dropped;
1445 dev_kfree_skb_any(skb);
1447 if (ipoib_cm_get(neigh))
1448 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
1449 ipoib_dbg(netdev_priv(dev),
1450 "neigh free for %06x %pI6\n",
1451 IPOIB_QPN(neigh->daddr),
1454 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1455 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1456 complete(&priv->ntbl.flushed);
1460 static void ipoib_neigh_reclaim(struct rcu_head *rp)
1462 /* Called as a result of removal from hash table */
1463 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1464 /* note TX context may hold another ref */
1465 ipoib_neigh_put(neigh);
1468 void ipoib_neigh_free(struct ipoib_neigh *neigh)
1470 struct net_device *dev = neigh->dev;
1471 struct ipoib_dev_priv *priv = netdev_priv(dev);
1472 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1473 struct ipoib_neigh_hash *htbl;
1474 struct ipoib_neigh __rcu **np;
1475 struct ipoib_neigh *n;
1478 htbl = rcu_dereference_protected(ntbl->htbl,
1479 lockdep_is_held(&priv->lock));
1483 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1484 np = &htbl->buckets[hash_val];
1485 for (n = rcu_dereference_protected(*np,
1486 lockdep_is_held(&priv->lock));
1488 n = rcu_dereference_protected(*np,
1489 lockdep_is_held(&priv->lock))) {
1492 rcu_assign_pointer(*np,
1493 rcu_dereference_protected(neigh->hnext,
1494 lockdep_is_held(&priv->lock)));
1495 /* remove from parent list */
1496 list_del_init(&neigh->list);
1497 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1505 static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1507 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1508 struct ipoib_neigh_hash *htbl;
1509 struct ipoib_neigh __rcu **buckets;
1512 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1514 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1517 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1518 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1519 buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1525 htbl->mask = (size - 1);
1526 htbl->buckets = buckets;
1527 RCU_INIT_POINTER(ntbl->htbl, htbl);
1529 atomic_set(&ntbl->entries, 0);
1531 /* start garbage collection */
1532 clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1533 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1534 arp_tbl.gc_interval);
1539 static void neigh_hash_free_rcu(struct rcu_head *head)
1541 struct ipoib_neigh_hash *htbl = container_of(head,
1542 struct ipoib_neigh_hash,
1544 struct ipoib_neigh __rcu **buckets = htbl->buckets;
1545 struct ipoib_neigh_table *ntbl = htbl->ntbl;
1549 complete(&ntbl->deleted);
1552 void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1554 struct ipoib_dev_priv *priv = netdev_priv(dev);
1555 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1556 struct ipoib_neigh_hash *htbl;
1557 unsigned long flags;
1560 /* remove all neigh connected to a given path or mcast */
1561 spin_lock_irqsave(&priv->lock, flags);
1563 htbl = rcu_dereference_protected(ntbl->htbl,
1564 lockdep_is_held(&priv->lock));
1569 for (i = 0; i < htbl->size; i++) {
1570 struct ipoib_neigh *neigh;
1571 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1573 while ((neigh = rcu_dereference_protected(*np,
1574 lockdep_is_held(&priv->lock))) != NULL) {
1575 /* delete neighs belong to this parent */
1576 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1577 rcu_assign_pointer(*np,
1578 rcu_dereference_protected(neigh->hnext,
1579 lockdep_is_held(&priv->lock)));
1580 /* remove from parent list */
1581 list_del_init(&neigh->list);
1582 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1590 spin_unlock_irqrestore(&priv->lock, flags);
1593 static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1595 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1596 struct ipoib_neigh_hash *htbl;
1597 unsigned long flags;
1598 int i, wait_flushed = 0;
1600 init_completion(&priv->ntbl.flushed);
1602 spin_lock_irqsave(&priv->lock, flags);
1604 htbl = rcu_dereference_protected(ntbl->htbl,
1605 lockdep_is_held(&priv->lock));
1609 wait_flushed = atomic_read(&priv->ntbl.entries);
1613 for (i = 0; i < htbl->size; i++) {
1614 struct ipoib_neigh *neigh;
1615 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1617 while ((neigh = rcu_dereference_protected(*np,
1618 lockdep_is_held(&priv->lock))) != NULL) {
1619 rcu_assign_pointer(*np,
1620 rcu_dereference_protected(neigh->hnext,
1621 lockdep_is_held(&priv->lock)));
1622 /* remove from path/mc list */
1623 list_del_init(&neigh->list);
1624 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1629 rcu_assign_pointer(ntbl->htbl, NULL);
1630 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1633 spin_unlock_irqrestore(&priv->lock, flags);
1635 wait_for_completion(&priv->ntbl.flushed);
1638 static void ipoib_neigh_hash_uninit(struct net_device *dev)
1640 struct ipoib_dev_priv *priv = netdev_priv(dev);
1643 ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
1644 init_completion(&priv->ntbl.deleted);
1645 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1647 /* Stop GC if called at init fail need to cancel work */
1648 stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1650 cancel_delayed_work(&priv->neigh_reap_task);
1652 ipoib_flush_neighs(priv);
1654 wait_for_completion(&priv->ntbl.deleted);
1658 int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1660 struct ipoib_dev_priv *priv = netdev_priv(dev);
1662 /* Allocate RX/TX "rings" to hold queued skbs */
1663 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
1665 if (!priv->rx_ring) {
1666 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
1667 ca->name, ipoib_recvq_size);
1671 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
1672 if (!priv->tx_ring) {
1673 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
1674 ca->name, ipoib_sendq_size);
1675 goto out_rx_ring_cleanup;
1678 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
1680 if (ipoib_ib_dev_init(dev, ca, port))
1681 goto out_tx_ring_cleanup;
1684 * Must be after ipoib_ib_dev_init so we can allocate a per
1685 * device wq there and use it here
1687 if (ipoib_neigh_hash_init(priv) < 0)
1688 goto out_dev_uninit;
1693 ipoib_ib_dev_cleanup(dev);
1695 out_tx_ring_cleanup:
1696 vfree(priv->tx_ring);
1698 out_rx_ring_cleanup:
1699 kfree(priv->rx_ring);
1705 void ipoib_dev_cleanup(struct net_device *dev)
1707 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
1712 /* Delete any child interfaces first */
1713 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
1714 /* Stop GC on child */
1715 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1716 cancel_delayed_work(&cpriv->neigh_reap_task);
1717 unregister_netdevice_queue(cpriv->dev, &head);
1719 unregister_netdevice_many(&head);
1722 * Must be before ipoib_ib_dev_cleanup or we delete an in use
1725 ipoib_neigh_hash_uninit(dev);
1727 ipoib_ib_dev_cleanup(dev);
1729 kfree(priv->rx_ring);
1730 vfree(priv->tx_ring);
1732 priv->rx_ring = NULL;
1733 priv->tx_ring = NULL;
1736 static int ipoib_set_vf_link_state(struct net_device *dev, int vf, int link_state)
1738 struct ipoib_dev_priv *priv = netdev_priv(dev);
1740 return ib_set_vf_link_state(priv->ca, vf, priv->port, link_state);
1743 static int ipoib_get_vf_config(struct net_device *dev, int vf,
1744 struct ifla_vf_info *ivf)
1746 struct ipoib_dev_priv *priv = netdev_priv(dev);
1749 err = ib_get_vf_config(priv->ca, vf, priv->port, ivf);
1754 memcpy(ivf->mac, dev->dev_addr, dev->addr_len);
1759 static int ipoib_set_vf_guid(struct net_device *dev, int vf, u64 guid, int type)
1761 struct ipoib_dev_priv *priv = netdev_priv(dev);
1763 if (type != IFLA_VF_IB_NODE_GUID && type != IFLA_VF_IB_PORT_GUID)
1766 return ib_set_vf_guid(priv->ca, vf, priv->port, guid, type);
1769 static int ipoib_get_vf_stats(struct net_device *dev, int vf,
1770 struct ifla_vf_stats *vf_stats)
1772 struct ipoib_dev_priv *priv = netdev_priv(dev);
1774 return ib_get_vf_stats(priv->ca, vf, priv->port, vf_stats);
1777 static const struct header_ops ipoib_header_ops = {
1778 .create = ipoib_hard_header,
1781 static const struct net_device_ops ipoib_netdev_ops_pf = {
1782 .ndo_uninit = ipoib_uninit,
1783 .ndo_open = ipoib_open,
1784 .ndo_stop = ipoib_stop,
1785 .ndo_change_mtu = ipoib_change_mtu,
1786 .ndo_fix_features = ipoib_fix_features,
1787 .ndo_start_xmit = ipoib_start_xmit,
1788 .ndo_tx_timeout = ipoib_timeout,
1789 .ndo_set_rx_mode = ipoib_set_mcast_list,
1790 .ndo_get_iflink = ipoib_get_iflink,
1791 .ndo_set_vf_link_state = ipoib_set_vf_link_state,
1792 .ndo_get_vf_config = ipoib_get_vf_config,
1793 .ndo_get_vf_stats = ipoib_get_vf_stats,
1794 .ndo_set_vf_guid = ipoib_set_vf_guid,
1795 .ndo_set_mac_address = ipoib_set_mac,
1798 static const struct net_device_ops ipoib_netdev_ops_vf = {
1799 .ndo_uninit = ipoib_uninit,
1800 .ndo_open = ipoib_open,
1801 .ndo_stop = ipoib_stop,
1802 .ndo_change_mtu = ipoib_change_mtu,
1803 .ndo_fix_features = ipoib_fix_features,
1804 .ndo_start_xmit = ipoib_start_xmit,
1805 .ndo_tx_timeout = ipoib_timeout,
1806 .ndo_set_rx_mode = ipoib_set_mcast_list,
1807 .ndo_get_iflink = ipoib_get_iflink,
1810 void ipoib_setup(struct net_device *dev)
1812 struct ipoib_dev_priv *priv = netdev_priv(dev);
1814 if (priv->hca_caps & IB_DEVICE_VIRTUAL_FUNCTION)
1815 dev->netdev_ops = &ipoib_netdev_ops_vf;
1817 dev->netdev_ops = &ipoib_netdev_ops_pf;
1819 dev->header_ops = &ipoib_header_ops;
1821 ipoib_set_ethtool_ops(dev);
1823 netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT);
1825 dev->watchdog_timeo = HZ;
1827 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1829 dev->hard_header_len = IPOIB_HARD_LEN;
1830 dev->addr_len = INFINIBAND_ALEN;
1831 dev->type = ARPHRD_INFINIBAND;
1832 dev->tx_queue_len = ipoib_sendq_size * 2;
1833 dev->features = (NETIF_F_VLAN_CHALLENGED |
1835 netif_keep_dst(dev);
1837 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
1841 spin_lock_init(&priv->lock);
1843 init_rwsem(&priv->vlan_rwsem);
1845 INIT_LIST_HEAD(&priv->path_list);
1846 INIT_LIST_HEAD(&priv->child_intfs);
1847 INIT_LIST_HEAD(&priv->dead_ahs);
1848 INIT_LIST_HEAD(&priv->multicast_list);
1850 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
1851 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
1852 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
1853 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
1854 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
1855 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1856 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
1857 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
1860 struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
1862 struct net_device *dev;
1864 dev = alloc_netdev((int)sizeof(struct ipoib_dev_priv), name,
1865 NET_NAME_UNKNOWN, ipoib_setup);
1869 return netdev_priv(dev);
1872 static ssize_t show_pkey(struct device *dev,
1873 struct device_attribute *attr, char *buf)
1875 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1877 return sprintf(buf, "0x%04x\n", priv->pkey);
1879 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1881 static ssize_t show_umcast(struct device *dev,
1882 struct device_attribute *attr, char *buf)
1884 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1886 return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1889 void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
1891 struct ipoib_dev_priv *priv = netdev_priv(ndev);
1893 if (umcast_val > 0) {
1894 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1895 ipoib_warn(priv, "ignoring multicast groups joined directly "
1898 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1901 static ssize_t set_umcast(struct device *dev,
1902 struct device_attribute *attr,
1903 const char *buf, size_t count)
1905 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1907 ipoib_set_umcast(to_net_dev(dev), umcast_val);
1911 static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1913 int ipoib_add_umcast_attr(struct net_device *dev)
1915 return device_create_file(&dev->dev, &dev_attr_umcast);
1918 static void set_base_guid(struct ipoib_dev_priv *priv, union ib_gid *gid)
1920 struct ipoib_dev_priv *child_priv;
1921 struct net_device *netdev = priv->dev;
1923 netif_addr_lock_bh(netdev);
1925 memcpy(&priv->local_gid.global.interface_id,
1926 &gid->global.interface_id,
1927 sizeof(gid->global.interface_id));
1928 memcpy(netdev->dev_addr + 4, &priv->local_gid, sizeof(priv->local_gid));
1929 clear_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
1931 netif_addr_unlock_bh(netdev);
1933 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
1934 down_read(&priv->vlan_rwsem);
1935 list_for_each_entry(child_priv, &priv->child_intfs, list)
1936 set_base_guid(child_priv, gid);
1937 up_read(&priv->vlan_rwsem);
1941 static int ipoib_check_lladdr(struct net_device *dev,
1942 struct sockaddr_storage *ss)
1944 union ib_gid *gid = (union ib_gid *)(ss->__data + 4);
1947 netif_addr_lock_bh(dev);
1949 /* Make sure the QPN, reserved and subnet prefix match the current
1950 * lladdr, it also makes sure the lladdr is unicast.
1952 if (memcmp(dev->dev_addr, ss->__data,
1953 4 + sizeof(gid->global.subnet_prefix)) ||
1954 gid->global.interface_id == 0)
1957 netif_addr_unlock_bh(dev);
1962 static int ipoib_set_mac(struct net_device *dev, void *addr)
1964 struct ipoib_dev_priv *priv = netdev_priv(dev);
1965 struct sockaddr_storage *ss = addr;
1968 if (!(dev->priv_flags & IFF_LIVE_ADDR_CHANGE) && netif_running(dev))
1971 ret = ipoib_check_lladdr(dev, ss);
1975 set_base_guid(priv, (union ib_gid *)(ss->__data + 4));
1977 queue_work(ipoib_workqueue, &priv->flush_light);
1982 static ssize_t create_child(struct device *dev,
1983 struct device_attribute *attr,
1984 const char *buf, size_t count)
1989 if (sscanf(buf, "%i", &pkey) != 1)
1992 if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
1996 * Set the full membership bit, so that we join the right
1997 * broadcast group, etc.
2001 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
2003 return ret ? ret : count;
2005 static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
2007 static ssize_t delete_child(struct device *dev,
2008 struct device_attribute *attr,
2009 const char *buf, size_t count)
2014 if (sscanf(buf, "%i", &pkey) != 1)
2017 if (pkey < 0 || pkey > 0xffff)
2020 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
2022 return ret ? ret : count;
2025 static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
2027 int ipoib_add_pkey_attr(struct net_device *dev)
2029 return device_create_file(&dev->dev, &dev_attr_pkey);
2032 int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
2034 priv->hca_caps = hca->attrs.device_cap_flags;
2036 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
2037 priv->dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
2039 if (priv->hca_caps & IB_DEVICE_UD_TSO)
2040 priv->dev->hw_features |= NETIF_F_TSO;
2042 priv->dev->features |= priv->dev->hw_features;
2048 static struct net_device *ipoib_add_port(const char *format,
2049 struct ib_device *hca, u8 port)
2051 struct ipoib_dev_priv *priv;
2052 struct ib_port_attr attr;
2053 int result = -ENOMEM;
2055 priv = ipoib_intf_alloc(format);
2057 goto alloc_mem_failed;
2059 SET_NETDEV_DEV(priv->dev, hca->dma_device);
2060 priv->dev->dev_id = port - 1;
2062 result = ib_query_port(hca, port, &attr);
2064 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
2066 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
2068 goto device_init_failed;
2071 /* MTU will be reset when mcast join happens */
2072 priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
2073 priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
2075 priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
2077 result = ib_query_pkey(hca, port, 0, &priv->pkey);
2079 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
2080 hca->name, port, result);
2081 goto device_init_failed;
2084 result = ipoib_set_dev_features(priv, hca);
2086 goto device_init_failed;
2089 * Set the full membership bit, so that we join the right
2090 * broadcast group, etc.
2092 priv->pkey |= 0x8000;
2094 priv->dev->broadcast[8] = priv->pkey >> 8;
2095 priv->dev->broadcast[9] = priv->pkey & 0xff;
2097 result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
2099 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
2100 hca->name, port, result);
2101 goto device_init_failed;
2103 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
2104 set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
2106 result = ipoib_dev_init(priv->dev, hca, port);
2108 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
2109 hca->name, port, result);
2110 goto device_init_failed;
2113 INIT_IB_EVENT_HANDLER(&priv->event_handler,
2114 priv->ca, ipoib_event);
2115 result = ib_register_event_handler(&priv->event_handler);
2117 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
2118 "port %d (ret = %d)\n",
2119 hca->name, port, result);
2123 /* call event handler to ensure pkey in sync */
2124 queue_work(ipoib_workqueue, &priv->flush_heavy);
2126 result = register_netdev(priv->dev);
2128 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
2129 hca->name, port, result);
2130 goto register_failed;
2133 if (ipoib_cm_add_mode_attr(priv->dev))
2135 if (ipoib_add_pkey_attr(priv->dev))
2137 if (ipoib_add_umcast_attr(priv->dev))
2139 if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
2141 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
2147 unregister_netdev(priv->dev);
2150 ib_unregister_event_handler(&priv->event_handler);
2151 flush_workqueue(ipoib_workqueue);
2152 /* Stop GC if started before flush */
2153 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
2154 cancel_delayed_work(&priv->neigh_reap_task);
2155 flush_workqueue(priv->wq);
2158 ipoib_dev_cleanup(priv->dev);
2161 free_netdev(priv->dev);
2164 return ERR_PTR(result);
2167 static void ipoib_add_one(struct ib_device *device)
2169 struct list_head *dev_list;
2170 struct net_device *dev;
2171 struct ipoib_dev_priv *priv;
2175 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
2179 INIT_LIST_HEAD(dev_list);
2181 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
2182 if (!rdma_protocol_ib(device, p))
2184 dev = ipoib_add_port("ib%d", device, p);
2186 priv = netdev_priv(dev);
2187 list_add_tail(&priv->list, dev_list);
2197 ib_set_client_data(device, &ipoib_client, dev_list);
2200 static void ipoib_remove_one(struct ib_device *device, void *client_data)
2202 struct ipoib_dev_priv *priv, *tmp;
2203 struct list_head *dev_list = client_data;
2208 list_for_each_entry_safe(priv, tmp, dev_list, list) {
2209 ib_unregister_event_handler(&priv->event_handler);
2210 flush_workqueue(ipoib_workqueue);
2212 /* mark interface in the middle of destruction */
2213 set_bit(IPOIB_FLAG_GOING_DOWN, &priv->flags);
2216 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
2220 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
2221 cancel_delayed_work(&priv->neigh_reap_task);
2222 flush_workqueue(priv->wq);
2224 unregister_netdev(priv->dev);
2225 free_netdev(priv->dev);
2231 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2232 static struct notifier_block ipoib_netdev_notifier = {
2233 .notifier_call = ipoib_netdev_event,
2237 static int __init ipoib_init_module(void)
2241 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
2242 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
2243 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
2245 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
2246 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
2247 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
2248 #ifdef CONFIG_INFINIBAND_IPOIB_CM
2249 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
2253 * When copying small received packets, we only copy from the
2254 * linear data part of the SKB, so we rely on this condition.
2256 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
2258 ret = ipoib_register_debugfs();
2263 * We create a global workqueue here that is used for all flush
2264 * operations. However, if you attempt to flush a workqueue
2265 * from a task on that same workqueue, it deadlocks the system.
2266 * We want to be able to flush the tasks associated with a
2267 * specific net device, so we also create a workqueue for each
2268 * netdevice. We queue up the tasks for that device only on
2269 * its private workqueue, and we only queue up flush events
2270 * on our global flush workqueue. This avoids the deadlocks.
2272 ipoib_workqueue = alloc_ordered_workqueue("ipoib_flush",
2274 if (!ipoib_workqueue) {
2279 ib_sa_register_client(&ipoib_sa_client);
2281 ret = ib_register_client(&ipoib_client);
2285 ret = ipoib_netlink_init();
2289 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2290 register_netdevice_notifier(&ipoib_netdev_notifier);
2295 ib_unregister_client(&ipoib_client);
2298 ib_sa_unregister_client(&ipoib_sa_client);
2299 destroy_workqueue(ipoib_workqueue);
2302 ipoib_unregister_debugfs();
2307 static void __exit ipoib_cleanup_module(void)
2309 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2310 unregister_netdevice_notifier(&ipoib_netdev_notifier);
2312 ipoib_netlink_fini();
2313 ib_unregister_client(&ipoib_client);
2314 ib_sa_unregister_client(&ipoib_sa_client);
2315 ipoib_unregister_debugfs();
2316 destroy_workqueue(ipoib_workqueue);
2319 module_init(ipoib_init_module);
2320 module_exit(ipoib_cleanup_module);