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>
52 #define DRV_VERSION "1.0.0"
54 const char ipoib_driver_version[] = DRV_VERSION;
56 MODULE_AUTHOR("Roland Dreier");
57 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
58 MODULE_LICENSE("Dual BSD/GPL");
59 MODULE_VERSION(DRV_VERSION);
61 int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
62 int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
64 module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
65 MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
66 module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
67 MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
69 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
70 int ipoib_debug_level;
72 module_param_named(debug_level, ipoib_debug_level, int, 0644);
73 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
76 struct ipoib_path_iter {
77 struct net_device *dev;
78 struct ipoib_path path;
81 static const u8 ipv4_bcast_addr[] = {
82 0x00, 0xff, 0xff, 0xff,
83 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
84 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
87 struct workqueue_struct *ipoib_workqueue;
89 struct ib_sa_client ipoib_sa_client;
91 static void ipoib_add_one(struct ib_device *device);
92 static void ipoib_remove_one(struct ib_device *device);
93 static void ipoib_neigh_reclaim(struct rcu_head *rp);
95 static struct ib_client ipoib_client = {
98 .remove = ipoib_remove_one
101 int ipoib_open(struct net_device *dev)
103 struct ipoib_dev_priv *priv = netdev_priv(dev);
105 ipoib_dbg(priv, "bringing up interface\n");
107 netif_carrier_off(dev);
109 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
111 if (ipoib_ib_dev_open(dev, 1)) {
112 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
117 if (ipoib_ib_dev_up(dev))
120 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
121 struct ipoib_dev_priv *cpriv;
123 /* Bring up any child interfaces too */
124 down_read(&priv->vlan_rwsem);
125 list_for_each_entry(cpriv, &priv->child_intfs, list) {
128 flags = cpriv->dev->flags;
132 dev_change_flags(cpriv->dev, flags | IFF_UP);
134 up_read(&priv->vlan_rwsem);
137 netif_start_queue(dev);
142 ipoib_ib_dev_stop(dev, 1);
145 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
150 static int ipoib_stop(struct net_device *dev)
152 struct ipoib_dev_priv *priv = netdev_priv(dev);
154 ipoib_dbg(priv, "stopping interface\n");
156 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
158 netif_stop_queue(dev);
160 ipoib_ib_dev_down(dev, 1);
161 ipoib_ib_dev_stop(dev, 0);
163 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
164 struct ipoib_dev_priv *cpriv;
166 /* Bring down any child interfaces too */
167 down_read(&priv->vlan_rwsem);
168 list_for_each_entry(cpriv, &priv->child_intfs, list) {
171 flags = cpriv->dev->flags;
172 if (!(flags & IFF_UP))
175 dev_change_flags(cpriv->dev, flags & ~IFF_UP);
177 up_read(&priv->vlan_rwsem);
183 static void ipoib_uninit(struct net_device *dev)
185 ipoib_dev_cleanup(dev);
188 static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
190 struct ipoib_dev_priv *priv = netdev_priv(dev);
192 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
193 features &= ~(NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
198 static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
200 struct ipoib_dev_priv *priv = netdev_priv(dev);
202 /* dev->mtu > 2K ==> connected mode */
203 if (ipoib_cm_admin_enabled(dev)) {
204 if (new_mtu > ipoib_cm_max_mtu(dev))
207 if (new_mtu > priv->mcast_mtu)
208 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
215 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
218 priv->admin_mtu = new_mtu;
220 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
225 int ipoib_set_mode(struct net_device *dev, const char *buf)
227 struct ipoib_dev_priv *priv = netdev_priv(dev);
229 /* flush paths if we switch modes so that connections are restarted */
230 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
231 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
232 ipoib_warn(priv, "enabling connected mode "
233 "will cause multicast packet drops\n");
234 netdev_update_features(dev);
236 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
238 ipoib_flush_paths(dev);
243 if (!strcmp(buf, "datagram\n")) {
244 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
245 netdev_update_features(dev);
246 dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
248 ipoib_flush_paths(dev);
256 static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
258 struct ipoib_dev_priv *priv = netdev_priv(dev);
259 struct rb_node *n = priv->path_tree.rb_node;
260 struct ipoib_path *path;
264 path = rb_entry(n, struct ipoib_path, rb_node);
266 ret = memcmp(gid, path->pathrec.dgid.raw,
267 sizeof (union ib_gid));
280 static int __path_add(struct net_device *dev, struct ipoib_path *path)
282 struct ipoib_dev_priv *priv = netdev_priv(dev);
283 struct rb_node **n = &priv->path_tree.rb_node;
284 struct rb_node *pn = NULL;
285 struct ipoib_path *tpath;
290 tpath = rb_entry(pn, struct ipoib_path, rb_node);
292 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
293 sizeof (union ib_gid));
302 rb_link_node(&path->rb_node, pn, n);
303 rb_insert_color(&path->rb_node, &priv->path_tree);
305 list_add_tail(&path->list, &priv->path_list);
310 static void path_free(struct net_device *dev, struct ipoib_path *path)
314 while ((skb = __skb_dequeue(&path->queue)))
315 dev_kfree_skb_irq(skb);
317 ipoib_dbg(netdev_priv(dev), "path_free\n");
319 /* remove all neigh connected to this path */
320 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
323 ipoib_put_ah(path->ah);
328 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
330 struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
332 struct ipoib_path_iter *iter;
334 iter = kmalloc(sizeof *iter, GFP_KERNEL);
339 memset(iter->path.pathrec.dgid.raw, 0, 16);
341 if (ipoib_path_iter_next(iter)) {
349 int ipoib_path_iter_next(struct ipoib_path_iter *iter)
351 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
353 struct ipoib_path *path;
356 spin_lock_irq(&priv->lock);
358 n = rb_first(&priv->path_tree);
361 path = rb_entry(n, struct ipoib_path, rb_node);
363 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
364 sizeof (union ib_gid)) < 0) {
373 spin_unlock_irq(&priv->lock);
378 void ipoib_path_iter_read(struct ipoib_path_iter *iter,
379 struct ipoib_path *path)
384 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
386 void ipoib_mark_paths_invalid(struct net_device *dev)
388 struct ipoib_dev_priv *priv = netdev_priv(dev);
389 struct ipoib_path *path, *tp;
391 spin_lock_irq(&priv->lock);
393 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
394 ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
395 be16_to_cpu(path->pathrec.dlid),
396 path->pathrec.dgid.raw);
400 spin_unlock_irq(&priv->lock);
403 void ipoib_flush_paths(struct net_device *dev)
405 struct ipoib_dev_priv *priv = netdev_priv(dev);
406 struct ipoib_path *path, *tp;
407 LIST_HEAD(remove_list);
410 netif_tx_lock_bh(dev);
411 spin_lock_irqsave(&priv->lock, flags);
413 list_splice_init(&priv->path_list, &remove_list);
415 list_for_each_entry(path, &remove_list, list)
416 rb_erase(&path->rb_node, &priv->path_tree);
418 list_for_each_entry_safe(path, tp, &remove_list, list) {
420 ib_sa_cancel_query(path->query_id, path->query);
421 spin_unlock_irqrestore(&priv->lock, flags);
422 netif_tx_unlock_bh(dev);
423 wait_for_completion(&path->done);
424 path_free(dev, path);
425 netif_tx_lock_bh(dev);
426 spin_lock_irqsave(&priv->lock, flags);
429 spin_unlock_irqrestore(&priv->lock, flags);
430 netif_tx_unlock_bh(dev);
433 static void path_rec_completion(int status,
434 struct ib_sa_path_rec *pathrec,
437 struct ipoib_path *path = path_ptr;
438 struct net_device *dev = path->dev;
439 struct ipoib_dev_priv *priv = netdev_priv(dev);
440 struct ipoib_ah *ah = NULL;
441 struct ipoib_ah *old_ah = NULL;
442 struct ipoib_neigh *neigh, *tn;
443 struct sk_buff_head skqueue;
448 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
449 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
451 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
452 status, path->pathrec.dgid.raw);
454 skb_queue_head_init(&skqueue);
457 struct ib_ah_attr av;
459 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
460 ah = ipoib_create_ah(dev, priv->pd, &av);
463 spin_lock_irqsave(&priv->lock, flags);
465 if (!IS_ERR_OR_NULL(ah)) {
466 path->pathrec = *pathrec;
471 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
472 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
474 while ((skb = __skb_dequeue(&path->queue)))
475 __skb_queue_tail(&skqueue, skb);
477 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
479 WARN_ON(neigh->ah != old_ah);
481 * Dropping the ah reference inside
482 * priv->lock is safe here, because we
483 * will hold one more reference from
484 * the original value of path->ah (ie
487 ipoib_put_ah(neigh->ah);
489 kref_get(&path->ah->ref);
490 neigh->ah = path->ah;
492 if (ipoib_cm_enabled(dev, neigh->daddr)) {
493 if (!ipoib_cm_get(neigh))
494 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
497 if (!ipoib_cm_get(neigh)) {
498 ipoib_neigh_free(neigh);
503 while ((skb = __skb_dequeue(&neigh->queue)))
504 __skb_queue_tail(&skqueue, skb);
510 complete(&path->done);
512 spin_unlock_irqrestore(&priv->lock, flags);
514 if (IS_ERR_OR_NULL(ah))
515 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
518 ipoib_put_ah(old_ah);
520 while ((skb = __skb_dequeue(&skqueue))) {
522 if (dev_queue_xmit(skb))
523 ipoib_warn(priv, "dev_queue_xmit failed "
524 "to requeue packet\n");
528 static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
530 struct ipoib_dev_priv *priv = netdev_priv(dev);
531 struct ipoib_path *path;
533 if (!priv->broadcast)
536 path = kzalloc(sizeof *path, GFP_ATOMIC);
542 skb_queue_head_init(&path->queue);
544 INIT_LIST_HEAD(&path->neigh_list);
546 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
547 path->pathrec.sgid = priv->local_gid;
548 path->pathrec.pkey = cpu_to_be16(priv->pkey);
549 path->pathrec.numb_path = 1;
550 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
555 static int path_rec_start(struct net_device *dev,
556 struct ipoib_path *path)
558 struct ipoib_dev_priv *priv = netdev_priv(dev);
560 ipoib_dbg(priv, "Start path record lookup for %pI6\n",
561 path->pathrec.dgid.raw);
563 init_completion(&path->done);
566 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
568 IB_SA_PATH_REC_DGID |
569 IB_SA_PATH_REC_SGID |
570 IB_SA_PATH_REC_NUMB_PATH |
571 IB_SA_PATH_REC_TRAFFIC_CLASS |
576 if (path->query_id < 0) {
577 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
579 complete(&path->done);
580 return path->query_id;
586 static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
587 struct net_device *dev)
589 struct ipoib_dev_priv *priv = netdev_priv(dev);
590 struct ipoib_path *path;
591 struct ipoib_neigh *neigh;
594 spin_lock_irqsave(&priv->lock, flags);
595 neigh = ipoib_neigh_alloc(daddr, dev);
597 spin_unlock_irqrestore(&priv->lock, flags);
598 ++dev->stats.tx_dropped;
599 dev_kfree_skb_any(skb);
603 path = __path_find(dev, daddr + 4);
605 path = path_rec_create(dev, daddr + 4);
609 __path_add(dev, path);
612 list_add_tail(&neigh->list, &path->neigh_list);
615 kref_get(&path->ah->ref);
616 neigh->ah = path->ah;
618 if (ipoib_cm_enabled(dev, neigh->daddr)) {
619 if (!ipoib_cm_get(neigh))
620 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
621 if (!ipoib_cm_get(neigh)) {
622 ipoib_neigh_free(neigh);
625 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
626 __skb_queue_tail(&neigh->queue, skb);
628 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
629 skb_queue_len(&neigh->queue));
633 spin_unlock_irqrestore(&priv->lock, flags);
634 ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
635 ipoib_neigh_put(neigh);
641 if (!path->query && path_rec_start(dev, path))
644 __skb_queue_tail(&neigh->queue, skb);
647 spin_unlock_irqrestore(&priv->lock, flags);
648 ipoib_neigh_put(neigh);
652 ipoib_neigh_free(neigh);
654 ++dev->stats.tx_dropped;
655 dev_kfree_skb_any(skb);
657 spin_unlock_irqrestore(&priv->lock, flags);
658 ipoib_neigh_put(neigh);
661 static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
664 struct ipoib_dev_priv *priv = netdev_priv(dev);
665 struct ipoib_path *path;
668 spin_lock_irqsave(&priv->lock, flags);
670 path = __path_find(dev, cb->hwaddr + 4);
671 if (!path || !path->valid) {
675 path = path_rec_create(dev, cb->hwaddr + 4);
679 __skb_queue_tail(&path->queue, skb);
681 if (!path->query && path_rec_start(dev, path)) {
682 spin_unlock_irqrestore(&priv->lock, flags);
684 path_free(dev, path);
687 __path_add(dev, path);
689 ++dev->stats.tx_dropped;
690 dev_kfree_skb_any(skb);
693 spin_unlock_irqrestore(&priv->lock, flags);
698 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
699 be16_to_cpu(path->pathrec.dlid));
701 spin_unlock_irqrestore(&priv->lock, flags);
702 ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr));
704 } else if ((path->query || !path_rec_start(dev, path)) &&
705 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
706 __skb_queue_tail(&path->queue, skb);
708 ++dev->stats.tx_dropped;
709 dev_kfree_skb_any(skb);
712 spin_unlock_irqrestore(&priv->lock, flags);
715 static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
717 struct ipoib_dev_priv *priv = netdev_priv(dev);
718 struct ipoib_neigh *neigh;
719 struct ipoib_cb *cb = ipoib_skb_cb(skb);
720 struct ipoib_header *header;
723 header = (struct ipoib_header *) skb->data;
725 if (unlikely(cb->hwaddr[4] == 0xff)) {
726 /* multicast, arrange "if" according to probability */
727 if ((header->proto != htons(ETH_P_IP)) &&
728 (header->proto != htons(ETH_P_IPV6)) &&
729 (header->proto != htons(ETH_P_ARP)) &&
730 (header->proto != htons(ETH_P_RARP)) &&
731 (header->proto != htons(ETH_P_TIPC))) {
732 /* ethertype not supported by IPoIB */
733 ++dev->stats.tx_dropped;
734 dev_kfree_skb_any(skb);
737 /* Add in the P_Key for multicast*/
738 cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
739 cb->hwaddr[9] = priv->pkey & 0xff;
741 neigh = ipoib_neigh_get(dev, cb->hwaddr);
743 goto send_using_neigh;
744 ipoib_mcast_send(dev, cb->hwaddr, skb);
748 /* unicast, arrange "switch" according to probability */
749 switch (header->proto) {
750 case htons(ETH_P_IP):
751 case htons(ETH_P_IPV6):
752 case htons(ETH_P_TIPC):
753 neigh = ipoib_neigh_get(dev, cb->hwaddr);
754 if (unlikely(!neigh)) {
755 neigh_add_path(skb, cb->hwaddr, dev);
759 case htons(ETH_P_ARP):
760 case htons(ETH_P_RARP):
761 /* for unicast ARP and RARP should always perform path find */
762 unicast_arp_send(skb, dev, cb);
765 /* ethertype not supported by IPoIB */
766 ++dev->stats.tx_dropped;
767 dev_kfree_skb_any(skb);
772 /* note we now hold a ref to neigh */
773 if (ipoib_cm_get(neigh)) {
774 if (ipoib_cm_up(neigh)) {
775 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
778 } else if (neigh->ah) {
779 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
783 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
784 spin_lock_irqsave(&priv->lock, flags);
785 __skb_queue_tail(&neigh->queue, skb);
786 spin_unlock_irqrestore(&priv->lock, flags);
788 ++dev->stats.tx_dropped;
789 dev_kfree_skb_any(skb);
793 ipoib_neigh_put(neigh);
798 static void ipoib_timeout(struct net_device *dev)
800 struct ipoib_dev_priv *priv = netdev_priv(dev);
802 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
803 jiffies_to_msecs(jiffies - dev->trans_start));
804 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
805 netif_queue_stopped(dev),
806 priv->tx_head, priv->tx_tail);
807 /* XXX reset QP, etc. */
810 static int ipoib_hard_header(struct sk_buff *skb,
811 struct net_device *dev,
813 const void *daddr, const void *saddr, unsigned len)
815 struct ipoib_header *header;
816 struct ipoib_cb *cb = ipoib_skb_cb(skb);
818 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
820 header->proto = htons(type);
821 header->reserved = 0;
824 * we don't rely on dst_entry structure, always stuff the
825 * destination address into skb->cb so we can figure out where
826 * to send the packet later.
828 memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
830 return sizeof *header;
833 static void ipoib_set_mcast_list(struct net_device *dev)
835 struct ipoib_dev_priv *priv = netdev_priv(dev);
837 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
838 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
842 queue_work(ipoib_workqueue, &priv->restart_task);
845 static int ipoib_get_iflink(const struct net_device *dev)
847 struct ipoib_dev_priv *priv = netdev_priv(dev);
849 return priv->parent->ifindex;
852 static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
855 * Use only the address parts that contributes to spreading
856 * The subnet prefix is not used as one can not connect to
857 * same remote port (GUID) using the same remote QPN via two
860 /* qpn octets[1:4) & port GUID octets[12:20) */
861 u32 *d32 = (u32 *) daddr;
864 hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
865 return hv & htbl->mask;
868 struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
870 struct ipoib_dev_priv *priv = netdev_priv(dev);
871 struct ipoib_neigh_table *ntbl = &priv->ntbl;
872 struct ipoib_neigh_hash *htbl;
873 struct ipoib_neigh *neigh = NULL;
878 htbl = rcu_dereference_bh(ntbl->htbl);
883 hash_val = ipoib_addr_hash(htbl, daddr);
884 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
886 neigh = rcu_dereference_bh(neigh->hnext)) {
887 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
888 /* found, take one ref on behalf of the caller */
889 if (!atomic_inc_not_zero(&neigh->refcnt)) {
894 neigh->alive = jiffies;
900 rcu_read_unlock_bh();
904 static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
906 struct ipoib_neigh_table *ntbl = &priv->ntbl;
907 struct ipoib_neigh_hash *htbl;
908 unsigned long neigh_obsolete;
913 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
916 spin_lock_irqsave(&priv->lock, flags);
918 htbl = rcu_dereference_protected(ntbl->htbl,
919 lockdep_is_held(&priv->lock));
924 /* neigh is obsolete if it was idle for two GC periods */
925 dt = 2 * arp_tbl.gc_interval;
926 neigh_obsolete = jiffies - dt;
927 /* handle possible race condition */
928 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
931 for (i = 0; i < htbl->size; i++) {
932 struct ipoib_neigh *neigh;
933 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
935 while ((neigh = rcu_dereference_protected(*np,
936 lockdep_is_held(&priv->lock))) != NULL) {
937 /* was the neigh idle for two GC periods */
938 if (time_after(neigh_obsolete, neigh->alive)) {
939 rcu_assign_pointer(*np,
940 rcu_dereference_protected(neigh->hnext,
941 lockdep_is_held(&priv->lock)));
942 /* remove from path/mc list */
943 list_del(&neigh->list);
944 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
953 spin_unlock_irqrestore(&priv->lock, flags);
956 static void ipoib_reap_neigh(struct work_struct *work)
958 struct ipoib_dev_priv *priv =
959 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
961 __ipoib_reap_neigh(priv);
963 if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
964 queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
965 arp_tbl.gc_interval);
969 static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
970 struct net_device *dev)
972 struct ipoib_neigh *neigh;
974 neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
979 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
980 skb_queue_head_init(&neigh->queue);
981 INIT_LIST_HEAD(&neigh->list);
982 ipoib_cm_set(neigh, NULL);
983 /* one ref on behalf of the caller */
984 atomic_set(&neigh->refcnt, 1);
989 struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
990 struct net_device *dev)
992 struct ipoib_dev_priv *priv = netdev_priv(dev);
993 struct ipoib_neigh_table *ntbl = &priv->ntbl;
994 struct ipoib_neigh_hash *htbl;
995 struct ipoib_neigh *neigh;
998 htbl = rcu_dereference_protected(ntbl->htbl,
999 lockdep_is_held(&priv->lock));
1005 /* need to add a new neigh, but maybe some other thread succeeded?
1006 * recalc hash, maybe hash resize took place so we do a search
1008 hash_val = ipoib_addr_hash(htbl, daddr);
1009 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
1010 lockdep_is_held(&priv->lock));
1012 neigh = rcu_dereference_protected(neigh->hnext,
1013 lockdep_is_held(&priv->lock))) {
1014 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1015 /* found, take one ref on behalf of the caller */
1016 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1021 neigh->alive = jiffies;
1026 neigh = ipoib_neigh_ctor(daddr, dev);
1030 /* one ref on behalf of the hash table */
1031 atomic_inc(&neigh->refcnt);
1032 neigh->alive = jiffies;
1034 rcu_assign_pointer(neigh->hnext,
1035 rcu_dereference_protected(htbl->buckets[hash_val],
1036 lockdep_is_held(&priv->lock)));
1037 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1038 atomic_inc(&ntbl->entries);
1045 void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1047 /* neigh reference count was dropprd to zero */
1048 struct net_device *dev = neigh->dev;
1049 struct ipoib_dev_priv *priv = netdev_priv(dev);
1050 struct sk_buff *skb;
1052 ipoib_put_ah(neigh->ah);
1053 while ((skb = __skb_dequeue(&neigh->queue))) {
1054 ++dev->stats.tx_dropped;
1055 dev_kfree_skb_any(skb);
1057 if (ipoib_cm_get(neigh))
1058 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
1059 ipoib_dbg(netdev_priv(dev),
1060 "neigh free for %06x %pI6\n",
1061 IPOIB_QPN(neigh->daddr),
1064 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1065 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1066 complete(&priv->ntbl.flushed);
1070 static void ipoib_neigh_reclaim(struct rcu_head *rp)
1072 /* Called as a result of removal from hash table */
1073 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1074 /* note TX context may hold another ref */
1075 ipoib_neigh_put(neigh);
1078 void ipoib_neigh_free(struct ipoib_neigh *neigh)
1080 struct net_device *dev = neigh->dev;
1081 struct ipoib_dev_priv *priv = netdev_priv(dev);
1082 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1083 struct ipoib_neigh_hash *htbl;
1084 struct ipoib_neigh __rcu **np;
1085 struct ipoib_neigh *n;
1088 htbl = rcu_dereference_protected(ntbl->htbl,
1089 lockdep_is_held(&priv->lock));
1093 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1094 np = &htbl->buckets[hash_val];
1095 for (n = rcu_dereference_protected(*np,
1096 lockdep_is_held(&priv->lock));
1098 n = rcu_dereference_protected(*np,
1099 lockdep_is_held(&priv->lock))) {
1102 rcu_assign_pointer(*np,
1103 rcu_dereference_protected(neigh->hnext,
1104 lockdep_is_held(&priv->lock)));
1105 /* remove from parent list */
1106 list_del(&neigh->list);
1107 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1115 static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1117 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1118 struct ipoib_neigh_hash *htbl;
1119 struct ipoib_neigh **buckets;
1122 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1124 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1127 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1128 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1129 buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1135 htbl->mask = (size - 1);
1136 htbl->buckets = buckets;
1139 atomic_set(&ntbl->entries, 0);
1141 /* start garbage collection */
1142 clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1143 queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
1144 arp_tbl.gc_interval);
1149 static void neigh_hash_free_rcu(struct rcu_head *head)
1151 struct ipoib_neigh_hash *htbl = container_of(head,
1152 struct ipoib_neigh_hash,
1154 struct ipoib_neigh __rcu **buckets = htbl->buckets;
1155 struct ipoib_neigh_table *ntbl = htbl->ntbl;
1159 complete(&ntbl->deleted);
1162 void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1164 struct ipoib_dev_priv *priv = netdev_priv(dev);
1165 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1166 struct ipoib_neigh_hash *htbl;
1167 unsigned long flags;
1170 /* remove all neigh connected to a given path or mcast */
1171 spin_lock_irqsave(&priv->lock, flags);
1173 htbl = rcu_dereference_protected(ntbl->htbl,
1174 lockdep_is_held(&priv->lock));
1179 for (i = 0; i < htbl->size; i++) {
1180 struct ipoib_neigh *neigh;
1181 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1183 while ((neigh = rcu_dereference_protected(*np,
1184 lockdep_is_held(&priv->lock))) != NULL) {
1185 /* delete neighs belong to this parent */
1186 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1187 rcu_assign_pointer(*np,
1188 rcu_dereference_protected(neigh->hnext,
1189 lockdep_is_held(&priv->lock)));
1190 /* remove from parent list */
1191 list_del(&neigh->list);
1192 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1200 spin_unlock_irqrestore(&priv->lock, flags);
1203 static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1205 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1206 struct ipoib_neigh_hash *htbl;
1207 unsigned long flags;
1208 int i, wait_flushed = 0;
1210 init_completion(&priv->ntbl.flushed);
1212 spin_lock_irqsave(&priv->lock, flags);
1214 htbl = rcu_dereference_protected(ntbl->htbl,
1215 lockdep_is_held(&priv->lock));
1219 wait_flushed = atomic_read(&priv->ntbl.entries);
1223 for (i = 0; i < htbl->size; i++) {
1224 struct ipoib_neigh *neigh;
1225 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1227 while ((neigh = rcu_dereference_protected(*np,
1228 lockdep_is_held(&priv->lock))) != NULL) {
1229 rcu_assign_pointer(*np,
1230 rcu_dereference_protected(neigh->hnext,
1231 lockdep_is_held(&priv->lock)));
1232 /* remove from path/mc list */
1233 list_del(&neigh->list);
1234 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1239 rcu_assign_pointer(ntbl->htbl, NULL);
1240 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1243 spin_unlock_irqrestore(&priv->lock, flags);
1245 wait_for_completion(&priv->ntbl.flushed);
1248 static void ipoib_neigh_hash_uninit(struct net_device *dev)
1250 struct ipoib_dev_priv *priv = netdev_priv(dev);
1253 ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
1254 init_completion(&priv->ntbl.deleted);
1255 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1257 /* Stop GC if called at init fail need to cancel work */
1258 stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1260 cancel_delayed_work(&priv->neigh_reap_task);
1262 ipoib_flush_neighs(priv);
1264 wait_for_completion(&priv->ntbl.deleted);
1268 int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1270 struct ipoib_dev_priv *priv = netdev_priv(dev);
1272 if (ipoib_neigh_hash_init(priv) < 0)
1274 /* Allocate RX/TX "rings" to hold queued skbs */
1275 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
1277 if (!priv->rx_ring) {
1278 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
1279 ca->name, ipoib_recvq_size);
1280 goto out_neigh_hash_cleanup;
1283 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
1284 if (!priv->tx_ring) {
1285 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
1286 ca->name, ipoib_sendq_size);
1287 goto out_rx_ring_cleanup;
1290 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
1292 if (ipoib_ib_dev_init(dev, ca, port))
1293 goto out_tx_ring_cleanup;
1297 out_tx_ring_cleanup:
1298 vfree(priv->tx_ring);
1300 out_rx_ring_cleanup:
1301 kfree(priv->rx_ring);
1303 out_neigh_hash_cleanup:
1304 ipoib_neigh_hash_uninit(dev);
1309 void ipoib_dev_cleanup(struct net_device *dev)
1311 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
1316 ipoib_delete_debug_files(dev);
1318 /* Delete any child interfaces first */
1319 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
1320 /* Stop GC on child */
1321 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1322 cancel_delayed_work(&cpriv->neigh_reap_task);
1323 unregister_netdevice_queue(cpriv->dev, &head);
1325 unregister_netdevice_many(&head);
1327 ipoib_ib_dev_cleanup(dev);
1329 kfree(priv->rx_ring);
1330 vfree(priv->tx_ring);
1332 priv->rx_ring = NULL;
1333 priv->tx_ring = NULL;
1335 ipoib_neigh_hash_uninit(dev);
1338 static const struct header_ops ipoib_header_ops = {
1339 .create = ipoib_hard_header,
1342 static const struct net_device_ops ipoib_netdev_ops = {
1343 .ndo_uninit = ipoib_uninit,
1344 .ndo_open = ipoib_open,
1345 .ndo_stop = ipoib_stop,
1346 .ndo_change_mtu = ipoib_change_mtu,
1347 .ndo_fix_features = ipoib_fix_features,
1348 .ndo_start_xmit = ipoib_start_xmit,
1349 .ndo_tx_timeout = ipoib_timeout,
1350 .ndo_set_rx_mode = ipoib_set_mcast_list,
1351 .ndo_get_iflink = ipoib_get_iflink,
1354 void ipoib_setup(struct net_device *dev)
1356 struct ipoib_dev_priv *priv = netdev_priv(dev);
1358 dev->netdev_ops = &ipoib_netdev_ops;
1359 dev->header_ops = &ipoib_header_ops;
1361 ipoib_set_ethtool_ops(dev);
1363 netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT);
1365 dev->watchdog_timeo = HZ;
1367 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1369 dev->hard_header_len = IPOIB_ENCAP_LEN;
1370 dev->addr_len = INFINIBAND_ALEN;
1371 dev->type = ARPHRD_INFINIBAND;
1372 dev->tx_queue_len = ipoib_sendq_size * 2;
1373 dev->features = (NETIF_F_VLAN_CHALLENGED |
1375 netif_keep_dst(dev);
1377 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
1381 spin_lock_init(&priv->lock);
1383 init_rwsem(&priv->vlan_rwsem);
1385 INIT_LIST_HEAD(&priv->path_list);
1386 INIT_LIST_HEAD(&priv->child_intfs);
1387 INIT_LIST_HEAD(&priv->dead_ahs);
1388 INIT_LIST_HEAD(&priv->multicast_list);
1390 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
1391 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
1392 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
1393 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
1394 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
1395 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1396 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
1397 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
1400 struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
1402 struct net_device *dev;
1404 dev = alloc_netdev((int)sizeof(struct ipoib_dev_priv), name,
1405 NET_NAME_UNKNOWN, ipoib_setup);
1409 return netdev_priv(dev);
1412 static ssize_t show_pkey(struct device *dev,
1413 struct device_attribute *attr, char *buf)
1415 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1417 return sprintf(buf, "0x%04x\n", priv->pkey);
1419 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1421 static ssize_t show_umcast(struct device *dev,
1422 struct device_attribute *attr, char *buf)
1424 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1426 return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1429 void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
1431 struct ipoib_dev_priv *priv = netdev_priv(ndev);
1433 if (umcast_val > 0) {
1434 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1435 ipoib_warn(priv, "ignoring multicast groups joined directly "
1438 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1441 static ssize_t set_umcast(struct device *dev,
1442 struct device_attribute *attr,
1443 const char *buf, size_t count)
1445 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1447 ipoib_set_umcast(to_net_dev(dev), umcast_val);
1451 static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1453 int ipoib_add_umcast_attr(struct net_device *dev)
1455 return device_create_file(&dev->dev, &dev_attr_umcast);
1458 static ssize_t create_child(struct device *dev,
1459 struct device_attribute *attr,
1460 const char *buf, size_t count)
1465 if (sscanf(buf, "%i", &pkey) != 1)
1468 if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
1472 * Set the full membership bit, so that we join the right
1473 * broadcast group, etc.
1477 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
1479 return ret ? ret : count;
1481 static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
1483 static ssize_t delete_child(struct device *dev,
1484 struct device_attribute *attr,
1485 const char *buf, size_t count)
1490 if (sscanf(buf, "%i", &pkey) != 1)
1493 if (pkey < 0 || pkey > 0xffff)
1496 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
1498 return ret ? ret : count;
1501 static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
1503 int ipoib_add_pkey_attr(struct net_device *dev)
1505 return device_create_file(&dev->dev, &dev_attr_pkey);
1508 int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
1510 struct ib_device_attr *device_attr;
1511 int result = -ENOMEM;
1513 device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
1515 printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
1516 hca->name, sizeof *device_attr);
1520 result = ib_query_device(hca, device_attr);
1522 printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
1527 priv->hca_caps = device_attr->device_cap_flags;
1531 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
1532 priv->dev->hw_features = NETIF_F_SG |
1533 NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1535 if (priv->hca_caps & IB_DEVICE_UD_TSO)
1536 priv->dev->hw_features |= NETIF_F_TSO;
1538 priv->dev->features |= priv->dev->hw_features;
1544 static struct net_device *ipoib_add_port(const char *format,
1545 struct ib_device *hca, u8 port)
1547 struct ipoib_dev_priv *priv;
1548 struct ib_port_attr attr;
1549 int result = -ENOMEM;
1551 priv = ipoib_intf_alloc(format);
1553 goto alloc_mem_failed;
1555 SET_NETDEV_DEV(priv->dev, hca->dma_device);
1556 priv->dev->dev_id = port - 1;
1558 if (!ib_query_port(hca, port, &attr))
1559 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1561 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
1563 goto device_init_failed;
1566 /* MTU will be reset when mcast join happens */
1567 priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
1568 priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
1570 priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
1572 result = ib_query_pkey(hca, port, 0, &priv->pkey);
1574 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1575 hca->name, port, result);
1576 goto device_init_failed;
1579 if (ipoib_set_dev_features(priv, hca))
1580 goto device_init_failed;
1583 * Set the full membership bit, so that we join the right
1584 * broadcast group, etc.
1586 priv->pkey |= 0x8000;
1588 priv->dev->broadcast[8] = priv->pkey >> 8;
1589 priv->dev->broadcast[9] = priv->pkey & 0xff;
1591 result = ib_query_gid(hca, port, 0, &priv->local_gid);
1593 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1594 hca->name, port, result);
1595 goto device_init_failed;
1597 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1599 result = ipoib_dev_init(priv->dev, hca, port);
1601 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1602 hca->name, port, result);
1603 goto device_init_failed;
1606 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1607 priv->ca, ipoib_event);
1608 result = ib_register_event_handler(&priv->event_handler);
1610 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1611 "port %d (ret = %d)\n",
1612 hca->name, port, result);
1616 result = register_netdev(priv->dev);
1618 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1619 hca->name, port, result);
1620 goto register_failed;
1623 ipoib_create_debug_files(priv->dev);
1625 if (ipoib_cm_add_mode_attr(priv->dev))
1627 if (ipoib_add_pkey_attr(priv->dev))
1629 if (ipoib_add_umcast_attr(priv->dev))
1631 if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
1633 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
1639 ipoib_delete_debug_files(priv->dev);
1640 unregister_netdev(priv->dev);
1643 ib_unregister_event_handler(&priv->event_handler);
1644 /* Stop GC if started before flush */
1645 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1646 cancel_delayed_work(&priv->neigh_reap_task);
1647 flush_workqueue(ipoib_workqueue);
1650 ipoib_dev_cleanup(priv->dev);
1653 free_netdev(priv->dev);
1656 return ERR_PTR(result);
1659 static void ipoib_add_one(struct ib_device *device)
1661 struct list_head *dev_list;
1662 struct net_device *dev;
1663 struct ipoib_dev_priv *priv;
1666 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1669 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1673 INIT_LIST_HEAD(dev_list);
1675 if (device->node_type == RDMA_NODE_IB_SWITCH) {
1680 e = device->phys_port_cnt;
1683 for (p = s; p <= e; ++p) {
1684 if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
1686 dev = ipoib_add_port("ib%d", device, p);
1688 priv = netdev_priv(dev);
1689 list_add_tail(&priv->list, dev_list);
1693 ib_set_client_data(device, &ipoib_client, dev_list);
1696 static void ipoib_remove_one(struct ib_device *device)
1698 struct ipoib_dev_priv *priv, *tmp;
1699 struct list_head *dev_list;
1701 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1704 dev_list = ib_get_client_data(device, &ipoib_client);
1708 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1709 ib_unregister_event_handler(&priv->event_handler);
1712 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
1716 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1717 cancel_delayed_work(&priv->neigh_reap_task);
1718 flush_workqueue(ipoib_workqueue);
1720 unregister_netdev(priv->dev);
1721 free_netdev(priv->dev);
1727 static int __init ipoib_init_module(void)
1731 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1732 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1733 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1735 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1736 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1737 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
1738 #ifdef CONFIG_INFINIBAND_IPOIB_CM
1739 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1743 * When copying small received packets, we only copy from the
1744 * linear data part of the SKB, so we rely on this condition.
1746 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
1748 ret = ipoib_register_debugfs();
1753 * We create our own workqueue mainly because we want to be
1754 * able to flush it when devices are being removed. We can't
1755 * use schedule_work()/flush_scheduled_work() because both
1756 * unregister_netdev() and linkwatch_event take the rtnl lock,
1757 * so flush_scheduled_work() can deadlock during device
1760 ipoib_workqueue = create_singlethread_workqueue("ipoib");
1761 if (!ipoib_workqueue) {
1766 ib_sa_register_client(&ipoib_sa_client);
1768 ret = ib_register_client(&ipoib_client);
1772 ret = ipoib_netlink_init();
1779 ib_unregister_client(&ipoib_client);
1782 ib_sa_unregister_client(&ipoib_sa_client);
1783 destroy_workqueue(ipoib_workqueue);
1786 ipoib_unregister_debugfs();
1791 static void __exit ipoib_cleanup_module(void)
1793 ipoib_netlink_fini();
1794 ib_unregister_client(&ipoib_client);
1795 ib_sa_unregister_client(&ipoib_sa_client);
1796 ipoib_unregister_debugfs();
1797 destroy_workqueue(ipoib_workqueue);
1800 module_init(ipoib_init_module);
1801 module_exit(ipoib_cleanup_module);