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 set_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
);
109 if (ipoib_pkey_dev_delay_open(dev
))
112 if (ipoib_ib_dev_open(dev
))
115 if (ipoib_ib_dev_up(dev
))
118 if (!test_bit(IPOIB_FLAG_SUBINTERFACE
, &priv
->flags
)) {
119 struct ipoib_dev_priv
*cpriv
;
121 /* Bring up any child interfaces too */
122 mutex_lock(&priv
->vlan_mutex
);
123 list_for_each_entry(cpriv
, &priv
->child_intfs
, list
) {
126 flags
= cpriv
->dev
->flags
;
130 dev_change_flags(cpriv
->dev
, flags
| IFF_UP
);
132 mutex_unlock(&priv
->vlan_mutex
);
135 netif_start_queue(dev
);
140 ipoib_ib_dev_stop(dev
, 1);
143 clear_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
);
148 static int ipoib_stop(struct net_device
*dev
)
150 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
152 ipoib_dbg(priv
, "stopping interface\n");
154 clear_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
);
156 netif_stop_queue(dev
);
158 ipoib_ib_dev_down(dev
, 1);
159 ipoib_ib_dev_stop(dev
, 0);
161 if (!test_bit(IPOIB_FLAG_SUBINTERFACE
, &priv
->flags
)) {
162 struct ipoib_dev_priv
*cpriv
;
164 /* Bring down any child interfaces too */
165 mutex_lock(&priv
->vlan_mutex
);
166 list_for_each_entry(cpriv
, &priv
->child_intfs
, list
) {
169 flags
= cpriv
->dev
->flags
;
170 if (!(flags
& IFF_UP
))
173 dev_change_flags(cpriv
->dev
, flags
& ~IFF_UP
);
175 mutex_unlock(&priv
->vlan_mutex
);
181 static void ipoib_uninit(struct net_device
*dev
)
183 ipoib_dev_cleanup(dev
);
186 static netdev_features_t
ipoib_fix_features(struct net_device
*dev
, netdev_features_t features
)
188 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
190 if (test_bit(IPOIB_FLAG_ADMIN_CM
, &priv
->flags
))
191 features
&= ~(NETIF_F_SG
| NETIF_F_IP_CSUM
| NETIF_F_TSO
);
196 static int ipoib_change_mtu(struct net_device
*dev
, int new_mtu
)
198 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
200 /* dev->mtu > 2K ==> connected mode */
201 if (ipoib_cm_admin_enabled(dev
)) {
202 if (new_mtu
> ipoib_cm_max_mtu(dev
))
205 if (new_mtu
> priv
->mcast_mtu
)
206 ipoib_warn(priv
, "mtu > %d will cause multicast packet drops.\n",
213 if (new_mtu
> IPOIB_UD_MTU(priv
->max_ib_mtu
))
216 priv
->admin_mtu
= new_mtu
;
218 dev
->mtu
= min(priv
->mcast_mtu
, priv
->admin_mtu
);
223 int ipoib_set_mode(struct net_device
*dev
, const char *buf
)
225 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
227 /* flush paths if we switch modes so that connections are restarted */
228 if (IPOIB_CM_SUPPORTED(dev
->dev_addr
) && !strcmp(buf
, "connected\n")) {
229 set_bit(IPOIB_FLAG_ADMIN_CM
, &priv
->flags
);
230 ipoib_warn(priv
, "enabling connected mode "
231 "will cause multicast packet drops\n");
232 netdev_update_features(dev
);
234 priv
->tx_wr
.send_flags
&= ~IB_SEND_IP_CSUM
;
236 ipoib_flush_paths(dev
);
241 if (!strcmp(buf
, "datagram\n")) {
242 clear_bit(IPOIB_FLAG_ADMIN_CM
, &priv
->flags
);
243 netdev_update_features(dev
);
244 dev_set_mtu(dev
, min(priv
->mcast_mtu
, dev
->mtu
));
246 ipoib_flush_paths(dev
);
254 struct ipoib_path
*__path_find(struct net_device
*dev
, void *gid
)
256 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
257 struct rb_node
*n
= priv
->path_tree
.rb_node
;
258 struct ipoib_path
*path
;
262 path
= rb_entry(n
, struct ipoib_path
, rb_node
);
264 ret
= memcmp(gid
, path
->pathrec
.dgid
.raw
,
265 sizeof (union ib_gid
));
278 static int __path_add(struct net_device
*dev
, struct ipoib_path
*path
)
280 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
281 struct rb_node
**n
= &priv
->path_tree
.rb_node
;
282 struct rb_node
*pn
= NULL
;
283 struct ipoib_path
*tpath
;
288 tpath
= rb_entry(pn
, struct ipoib_path
, rb_node
);
290 ret
= memcmp(path
->pathrec
.dgid
.raw
, tpath
->pathrec
.dgid
.raw
,
291 sizeof (union ib_gid
));
300 rb_link_node(&path
->rb_node
, pn
, n
);
301 rb_insert_color(&path
->rb_node
, &priv
->path_tree
);
303 list_add_tail(&path
->list
, &priv
->path_list
);
308 static void path_free(struct net_device
*dev
, struct ipoib_path
*path
)
312 while ((skb
= __skb_dequeue(&path
->queue
)))
313 dev_kfree_skb_irq(skb
);
315 ipoib_dbg(netdev_priv(dev
), "path_free\n");
317 /* remove all neigh connected to this path */
318 ipoib_del_neighs_by_gid(dev
, path
->pathrec
.dgid
.raw
);
321 ipoib_put_ah(path
->ah
);
326 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
328 struct ipoib_path_iter
*ipoib_path_iter_init(struct net_device
*dev
)
330 struct ipoib_path_iter
*iter
;
332 iter
= kmalloc(sizeof *iter
, GFP_KERNEL
);
337 memset(iter
->path
.pathrec
.dgid
.raw
, 0, 16);
339 if (ipoib_path_iter_next(iter
)) {
347 int ipoib_path_iter_next(struct ipoib_path_iter
*iter
)
349 struct ipoib_dev_priv
*priv
= netdev_priv(iter
->dev
);
351 struct ipoib_path
*path
;
354 spin_lock_irq(&priv
->lock
);
356 n
= rb_first(&priv
->path_tree
);
359 path
= rb_entry(n
, struct ipoib_path
, rb_node
);
361 if (memcmp(iter
->path
.pathrec
.dgid
.raw
, path
->pathrec
.dgid
.raw
,
362 sizeof (union ib_gid
)) < 0) {
371 spin_unlock_irq(&priv
->lock
);
376 void ipoib_path_iter_read(struct ipoib_path_iter
*iter
,
377 struct ipoib_path
*path
)
382 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
384 void ipoib_mark_paths_invalid(struct net_device
*dev
)
386 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
387 struct ipoib_path
*path
, *tp
;
389 spin_lock_irq(&priv
->lock
);
391 list_for_each_entry_safe(path
, tp
, &priv
->path_list
, list
) {
392 ipoib_dbg(priv
, "mark path LID 0x%04x GID %pI6 invalid\n",
393 be16_to_cpu(path
->pathrec
.dlid
),
394 path
->pathrec
.dgid
.raw
);
398 spin_unlock_irq(&priv
->lock
);
401 void ipoib_flush_paths(struct net_device
*dev
)
403 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
404 struct ipoib_path
*path
, *tp
;
405 LIST_HEAD(remove_list
);
408 netif_tx_lock_bh(dev
);
409 spin_lock_irqsave(&priv
->lock
, flags
);
411 list_splice_init(&priv
->path_list
, &remove_list
);
413 list_for_each_entry(path
, &remove_list
, list
)
414 rb_erase(&path
->rb_node
, &priv
->path_tree
);
416 list_for_each_entry_safe(path
, tp
, &remove_list
, list
) {
418 ib_sa_cancel_query(path
->query_id
, path
->query
);
419 spin_unlock_irqrestore(&priv
->lock
, flags
);
420 netif_tx_unlock_bh(dev
);
421 wait_for_completion(&path
->done
);
422 path_free(dev
, path
);
423 netif_tx_lock_bh(dev
);
424 spin_lock_irqsave(&priv
->lock
, flags
);
427 spin_unlock_irqrestore(&priv
->lock
, flags
);
428 netif_tx_unlock_bh(dev
);
431 static void path_rec_completion(int status
,
432 struct ib_sa_path_rec
*pathrec
,
435 struct ipoib_path
*path
= path_ptr
;
436 struct net_device
*dev
= path
->dev
;
437 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
438 struct ipoib_ah
*ah
= NULL
;
439 struct ipoib_ah
*old_ah
= NULL
;
440 struct ipoib_neigh
*neigh
, *tn
;
441 struct sk_buff_head skqueue
;
446 ipoib_dbg(priv
, "PathRec LID 0x%04x for GID %pI6\n",
447 be16_to_cpu(pathrec
->dlid
), pathrec
->dgid
.raw
);
449 ipoib_dbg(priv
, "PathRec status %d for GID %pI6\n",
450 status
, path
->pathrec
.dgid
.raw
);
452 skb_queue_head_init(&skqueue
);
455 struct ib_ah_attr av
;
457 if (!ib_init_ah_from_path(priv
->ca
, priv
->port
, pathrec
, &av
))
458 ah
= ipoib_create_ah(dev
, priv
->pd
, &av
);
461 spin_lock_irqsave(&priv
->lock
, flags
);
463 if (!IS_ERR_OR_NULL(ah
)) {
464 path
->pathrec
= *pathrec
;
469 ipoib_dbg(priv
, "created address handle %p for LID 0x%04x, SL %d\n",
470 ah
, be16_to_cpu(pathrec
->dlid
), pathrec
->sl
);
472 while ((skb
= __skb_dequeue(&path
->queue
)))
473 __skb_queue_tail(&skqueue
, skb
);
475 list_for_each_entry_safe(neigh
, tn
, &path
->neigh_list
, list
) {
477 WARN_ON(neigh
->ah
!= old_ah
);
479 * Dropping the ah reference inside
480 * priv->lock is safe here, because we
481 * will hold one more reference from
482 * the original value of path->ah (ie
485 ipoib_put_ah(neigh
->ah
);
487 kref_get(&path
->ah
->ref
);
488 neigh
->ah
= path
->ah
;
490 if (ipoib_cm_enabled(dev
, neigh
->daddr
)) {
491 if (!ipoib_cm_get(neigh
))
492 ipoib_cm_set(neigh
, ipoib_cm_create_tx(dev
,
495 if (!ipoib_cm_get(neigh
)) {
496 ipoib_neigh_free(neigh
);
501 while ((skb
= __skb_dequeue(&neigh
->queue
)))
502 __skb_queue_tail(&skqueue
, skb
);
508 complete(&path
->done
);
510 spin_unlock_irqrestore(&priv
->lock
, flags
);
512 if (IS_ERR_OR_NULL(ah
))
513 ipoib_del_neighs_by_gid(dev
, path
->pathrec
.dgid
.raw
);
516 ipoib_put_ah(old_ah
);
518 while ((skb
= __skb_dequeue(&skqueue
))) {
520 if (dev_queue_xmit(skb
))
521 ipoib_warn(priv
, "dev_queue_xmit failed "
522 "to requeue packet\n");
526 static struct ipoib_path
*path_rec_create(struct net_device
*dev
, void *gid
)
528 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
529 struct ipoib_path
*path
;
531 if (!priv
->broadcast
)
534 path
= kzalloc(sizeof *path
, GFP_ATOMIC
);
540 skb_queue_head_init(&path
->queue
);
542 INIT_LIST_HEAD(&path
->neigh_list
);
544 memcpy(path
->pathrec
.dgid
.raw
, gid
, sizeof (union ib_gid
));
545 path
->pathrec
.sgid
= priv
->local_gid
;
546 path
->pathrec
.pkey
= cpu_to_be16(priv
->pkey
);
547 path
->pathrec
.numb_path
= 1;
548 path
->pathrec
.traffic_class
= priv
->broadcast
->mcmember
.traffic_class
;
553 static int path_rec_start(struct net_device
*dev
,
554 struct ipoib_path
*path
)
556 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
558 ipoib_dbg(priv
, "Start path record lookup for %pI6\n",
559 path
->pathrec
.dgid
.raw
);
561 init_completion(&path
->done
);
564 ib_sa_path_rec_get(&ipoib_sa_client
, priv
->ca
, priv
->port
,
566 IB_SA_PATH_REC_DGID
|
567 IB_SA_PATH_REC_SGID
|
568 IB_SA_PATH_REC_NUMB_PATH
|
569 IB_SA_PATH_REC_TRAFFIC_CLASS
|
574 if (path
->query_id
< 0) {
575 ipoib_warn(priv
, "ib_sa_path_rec_get failed: %d\n", path
->query_id
);
577 complete(&path
->done
);
578 return path
->query_id
;
584 static void neigh_add_path(struct sk_buff
*skb
, u8
*daddr
,
585 struct net_device
*dev
)
587 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
588 struct ipoib_path
*path
;
589 struct ipoib_neigh
*neigh
;
592 spin_lock_irqsave(&priv
->lock
, flags
);
593 neigh
= ipoib_neigh_alloc(daddr
, dev
);
595 spin_unlock_irqrestore(&priv
->lock
, flags
);
596 ++dev
->stats
.tx_dropped
;
597 dev_kfree_skb_any(skb
);
601 path
= __path_find(dev
, daddr
+ 4);
603 path
= path_rec_create(dev
, daddr
+ 4);
607 __path_add(dev
, path
);
610 list_add_tail(&neigh
->list
, &path
->neigh_list
);
613 kref_get(&path
->ah
->ref
);
614 neigh
->ah
= path
->ah
;
616 if (ipoib_cm_enabled(dev
, neigh
->daddr
)) {
617 if (!ipoib_cm_get(neigh
))
618 ipoib_cm_set(neigh
, ipoib_cm_create_tx(dev
, path
, neigh
));
619 if (!ipoib_cm_get(neigh
)) {
620 ipoib_neigh_free(neigh
);
623 if (skb_queue_len(&neigh
->queue
) < IPOIB_MAX_PATH_REC_QUEUE
)
624 __skb_queue_tail(&neigh
->queue
, skb
);
626 ipoib_warn(priv
, "queue length limit %d. Packet drop.\n",
627 skb_queue_len(&neigh
->queue
));
631 spin_unlock_irqrestore(&priv
->lock
, flags
);
632 ipoib_send(dev
, skb
, path
->ah
, IPOIB_QPN(daddr
));
633 ipoib_neigh_put(neigh
);
639 if (!path
->query
&& path_rec_start(dev
, path
))
642 __skb_queue_tail(&neigh
->queue
, skb
);
645 spin_unlock_irqrestore(&priv
->lock
, flags
);
646 ipoib_neigh_put(neigh
);
650 ipoib_neigh_free(neigh
);
652 ++dev
->stats
.tx_dropped
;
653 dev_kfree_skb_any(skb
);
655 spin_unlock_irqrestore(&priv
->lock
, flags
);
656 ipoib_neigh_put(neigh
);
659 static void unicast_arp_send(struct sk_buff
*skb
, struct net_device
*dev
,
662 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
663 struct ipoib_path
*path
;
666 spin_lock_irqsave(&priv
->lock
, flags
);
668 path
= __path_find(dev
, cb
->hwaddr
+ 4);
669 if (!path
|| !path
->valid
) {
673 path
= path_rec_create(dev
, cb
->hwaddr
+ 4);
677 __skb_queue_tail(&path
->queue
, skb
);
679 if (!path
->query
&& path_rec_start(dev
, path
)) {
680 spin_unlock_irqrestore(&priv
->lock
, flags
);
682 path_free(dev
, path
);
685 __path_add(dev
, path
);
687 ++dev
->stats
.tx_dropped
;
688 dev_kfree_skb_any(skb
);
691 spin_unlock_irqrestore(&priv
->lock
, flags
);
696 ipoib_dbg(priv
, "Send unicast ARP to %04x\n",
697 be16_to_cpu(path
->pathrec
.dlid
));
699 spin_unlock_irqrestore(&priv
->lock
, flags
);
700 ipoib_send(dev
, skb
, path
->ah
, IPOIB_QPN(cb
->hwaddr
));
702 } else if ((path
->query
|| !path_rec_start(dev
, path
)) &&
703 skb_queue_len(&path
->queue
) < IPOIB_MAX_PATH_REC_QUEUE
) {
704 __skb_queue_tail(&path
->queue
, skb
);
706 ++dev
->stats
.tx_dropped
;
707 dev_kfree_skb_any(skb
);
710 spin_unlock_irqrestore(&priv
->lock
, flags
);
713 static int ipoib_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
715 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
716 struct ipoib_neigh
*neigh
;
717 struct ipoib_cb
*cb
= (struct ipoib_cb
*) skb
->cb
;
718 struct ipoib_header
*header
;
721 header
= (struct ipoib_header
*) skb
->data
;
723 if (unlikely(cb
->hwaddr
[4] == 0xff)) {
724 /* multicast, arrange "if" according to probability */
725 if ((header
->proto
!= htons(ETH_P_IP
)) &&
726 (header
->proto
!= htons(ETH_P_IPV6
)) &&
727 (header
->proto
!= htons(ETH_P_ARP
)) &&
728 (header
->proto
!= htons(ETH_P_RARP
)) &&
729 (header
->proto
!= htons(ETH_P_TIPC
))) {
730 /* ethertype not supported by IPoIB */
731 ++dev
->stats
.tx_dropped
;
732 dev_kfree_skb_any(skb
);
735 /* Add in the P_Key for multicast*/
736 cb
->hwaddr
[8] = (priv
->pkey
>> 8) & 0xff;
737 cb
->hwaddr
[9] = priv
->pkey
& 0xff;
739 neigh
= ipoib_neigh_get(dev
, cb
->hwaddr
);
741 goto send_using_neigh
;
742 ipoib_mcast_send(dev
, cb
->hwaddr
, skb
);
746 /* unicast, arrange "switch" according to probability */
747 switch (header
->proto
) {
748 case htons(ETH_P_IP
):
749 case htons(ETH_P_IPV6
):
750 case htons(ETH_P_TIPC
):
751 neigh
= ipoib_neigh_get(dev
, cb
->hwaddr
);
752 if (unlikely(!neigh
)) {
753 neigh_add_path(skb
, cb
->hwaddr
, dev
);
757 case htons(ETH_P_ARP
):
758 case htons(ETH_P_RARP
):
759 /* for unicast ARP and RARP should always perform path find */
760 unicast_arp_send(skb
, dev
, cb
);
763 /* ethertype not supported by IPoIB */
764 ++dev
->stats
.tx_dropped
;
765 dev_kfree_skb_any(skb
);
770 /* note we now hold a ref to neigh */
771 if (ipoib_cm_get(neigh
)) {
772 if (ipoib_cm_up(neigh
)) {
773 ipoib_cm_send(dev
, skb
, ipoib_cm_get(neigh
));
776 } else if (neigh
->ah
) {
777 ipoib_send(dev
, skb
, neigh
->ah
, IPOIB_QPN(cb
->hwaddr
));
781 if (skb_queue_len(&neigh
->queue
) < IPOIB_MAX_PATH_REC_QUEUE
) {
782 spin_lock_irqsave(&priv
->lock
, flags
);
783 __skb_queue_tail(&neigh
->queue
, skb
);
784 spin_unlock_irqrestore(&priv
->lock
, flags
);
786 ++dev
->stats
.tx_dropped
;
787 dev_kfree_skb_any(skb
);
791 ipoib_neigh_put(neigh
);
796 static void ipoib_timeout(struct net_device
*dev
)
798 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
800 ipoib_warn(priv
, "transmit timeout: latency %d msecs\n",
801 jiffies_to_msecs(jiffies
- dev
->trans_start
));
802 ipoib_warn(priv
, "queue stopped %d, tx_head %u, tx_tail %u\n",
803 netif_queue_stopped(dev
),
804 priv
->tx_head
, priv
->tx_tail
);
805 /* XXX reset QP, etc. */
808 static int ipoib_hard_header(struct sk_buff
*skb
,
809 struct net_device
*dev
,
811 const void *daddr
, const void *saddr
, unsigned len
)
813 struct ipoib_header
*header
;
814 struct ipoib_cb
*cb
= (struct ipoib_cb
*) skb
->cb
;
816 header
= (struct ipoib_header
*) skb_push(skb
, sizeof *header
);
818 header
->proto
= htons(type
);
819 header
->reserved
= 0;
822 * we don't rely on dst_entry structure, always stuff the
823 * destination address into skb->cb so we can figure out where
824 * to send the packet later.
826 memcpy(cb
->hwaddr
, daddr
, INFINIBAND_ALEN
);
828 return sizeof *header
;
831 static void ipoib_set_mcast_list(struct net_device
*dev
)
833 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
835 if (!test_bit(IPOIB_FLAG_OPER_UP
, &priv
->flags
)) {
836 ipoib_dbg(priv
, "IPOIB_FLAG_OPER_UP not set");
840 queue_work(ipoib_workqueue
, &priv
->restart_task
);
843 static u32
ipoib_addr_hash(struct ipoib_neigh_hash
*htbl
, u8
*daddr
)
846 * Use only the address parts that contributes to spreading
847 * The subnet prefix is not used as one can not connect to
848 * same remote port (GUID) using the same remote QPN via two
851 /* qpn octets[1:4) & port GUID octets[12:20) */
852 u32
*d32
= (u32
*) daddr
;
855 hv
= jhash_3words(d32
[3], d32
[4], IPOIB_QPN_MASK
& d32
[0], 0);
856 return hv
& htbl
->mask
;
859 struct ipoib_neigh
*ipoib_neigh_get(struct net_device
*dev
, u8
*daddr
)
861 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
862 struct ipoib_neigh_table
*ntbl
= &priv
->ntbl
;
863 struct ipoib_neigh_hash
*htbl
;
864 struct ipoib_neigh
*neigh
= NULL
;
869 htbl
= rcu_dereference_bh(ntbl
->htbl
);
874 hash_val
= ipoib_addr_hash(htbl
, daddr
);
875 for (neigh
= rcu_dereference_bh(htbl
->buckets
[hash_val
]);
877 neigh
= rcu_dereference_bh(neigh
->hnext
)) {
878 if (memcmp(daddr
, neigh
->daddr
, INFINIBAND_ALEN
) == 0) {
879 /* found, take one ref on behalf of the caller */
880 if (!atomic_inc_not_zero(&neigh
->refcnt
)) {
886 if (likely(skb_queue_len(&neigh
->queue
) < IPOIB_MAX_PATH_REC_QUEUE
))
887 neigh
->alive
= jiffies
;
893 rcu_read_unlock_bh();
897 static void __ipoib_reap_neigh(struct ipoib_dev_priv
*priv
)
899 struct ipoib_neigh_table
*ntbl
= &priv
->ntbl
;
900 struct ipoib_neigh_hash
*htbl
;
901 unsigned long neigh_obsolete
;
906 if (test_bit(IPOIB_STOP_NEIGH_GC
, &priv
->flags
))
909 spin_lock_irqsave(&priv
->lock
, flags
);
911 htbl
= rcu_dereference_protected(ntbl
->htbl
,
912 lockdep_is_held(&priv
->lock
));
917 /* neigh is obsolete if it was idle for two GC periods */
918 dt
= 2 * arp_tbl
.gc_interval
;
919 neigh_obsolete
= jiffies
- dt
;
920 /* handle possible race condition */
921 if (test_bit(IPOIB_STOP_NEIGH_GC
, &priv
->flags
))
924 for (i
= 0; i
< htbl
->size
; i
++) {
925 struct ipoib_neigh
*neigh
;
926 struct ipoib_neigh __rcu
**np
= &htbl
->buckets
[i
];
928 while ((neigh
= rcu_dereference_protected(*np
,
929 lockdep_is_held(&priv
->lock
))) != NULL
) {
930 /* was the neigh idle for two GC periods */
931 if (time_after(neigh_obsolete
, neigh
->alive
)) {
932 rcu_assign_pointer(*np
,
933 rcu_dereference_protected(neigh
->hnext
,
934 lockdep_is_held(&priv
->lock
)));
935 /* remove from path/mc list */
936 list_del(&neigh
->list
);
937 call_rcu(&neigh
->rcu
, ipoib_neigh_reclaim
);
946 spin_unlock_irqrestore(&priv
->lock
, flags
);
949 static void ipoib_reap_neigh(struct work_struct
*work
)
951 struct ipoib_dev_priv
*priv
=
952 container_of(work
, struct ipoib_dev_priv
, neigh_reap_task
.work
);
954 __ipoib_reap_neigh(priv
);
956 if (!test_bit(IPOIB_STOP_NEIGH_GC
, &priv
->flags
))
957 queue_delayed_work(ipoib_workqueue
, &priv
->neigh_reap_task
,
958 arp_tbl
.gc_interval
);
962 static struct ipoib_neigh
*ipoib_neigh_ctor(u8
*daddr
,
963 struct net_device
*dev
)
965 struct ipoib_neigh
*neigh
;
967 neigh
= kzalloc(sizeof *neigh
, GFP_ATOMIC
);
972 memcpy(&neigh
->daddr
, daddr
, sizeof(neigh
->daddr
));
973 skb_queue_head_init(&neigh
->queue
);
974 INIT_LIST_HEAD(&neigh
->list
);
975 ipoib_cm_set(neigh
, NULL
);
976 /* one ref on behalf of the caller */
977 atomic_set(&neigh
->refcnt
, 1);
982 struct ipoib_neigh
*ipoib_neigh_alloc(u8
*daddr
,
983 struct net_device
*dev
)
985 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
986 struct ipoib_neigh_table
*ntbl
= &priv
->ntbl
;
987 struct ipoib_neigh_hash
*htbl
;
988 struct ipoib_neigh
*neigh
;
991 htbl
= rcu_dereference_protected(ntbl
->htbl
,
992 lockdep_is_held(&priv
->lock
));
998 /* need to add a new neigh, but maybe some other thread succeeded?
999 * recalc hash, maybe hash resize took place so we do a search
1001 hash_val
= ipoib_addr_hash(htbl
, daddr
);
1002 for (neigh
= rcu_dereference_protected(htbl
->buckets
[hash_val
],
1003 lockdep_is_held(&priv
->lock
));
1005 neigh
= rcu_dereference_protected(neigh
->hnext
,
1006 lockdep_is_held(&priv
->lock
))) {
1007 if (memcmp(daddr
, neigh
->daddr
, INFINIBAND_ALEN
) == 0) {
1008 /* found, take one ref on behalf of the caller */
1009 if (!atomic_inc_not_zero(&neigh
->refcnt
)) {
1014 neigh
->alive
= jiffies
;
1019 neigh
= ipoib_neigh_ctor(daddr
, dev
);
1023 /* one ref on behalf of the hash table */
1024 atomic_inc(&neigh
->refcnt
);
1025 neigh
->alive
= jiffies
;
1027 rcu_assign_pointer(neigh
->hnext
,
1028 rcu_dereference_protected(htbl
->buckets
[hash_val
],
1029 lockdep_is_held(&priv
->lock
)));
1030 rcu_assign_pointer(htbl
->buckets
[hash_val
], neigh
);
1031 atomic_inc(&ntbl
->entries
);
1038 void ipoib_neigh_dtor(struct ipoib_neigh
*neigh
)
1040 /* neigh reference count was dropprd to zero */
1041 struct net_device
*dev
= neigh
->dev
;
1042 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1043 struct sk_buff
*skb
;
1045 ipoib_put_ah(neigh
->ah
);
1046 while ((skb
= __skb_dequeue(&neigh
->queue
))) {
1047 ++dev
->stats
.tx_dropped
;
1048 dev_kfree_skb_any(skb
);
1050 if (ipoib_cm_get(neigh
))
1051 ipoib_cm_destroy_tx(ipoib_cm_get(neigh
));
1052 ipoib_dbg(netdev_priv(dev
),
1053 "neigh free for %06x %pI6\n",
1054 IPOIB_QPN(neigh
->daddr
),
1057 if (atomic_dec_and_test(&priv
->ntbl
.entries
)) {
1058 if (test_bit(IPOIB_NEIGH_TBL_FLUSH
, &priv
->flags
))
1059 complete(&priv
->ntbl
.flushed
);
1063 static void ipoib_neigh_reclaim(struct rcu_head
*rp
)
1065 /* Called as a result of removal from hash table */
1066 struct ipoib_neigh
*neigh
= container_of(rp
, struct ipoib_neigh
, rcu
);
1067 /* note TX context may hold another ref */
1068 ipoib_neigh_put(neigh
);
1071 void ipoib_neigh_free(struct ipoib_neigh
*neigh
)
1073 struct net_device
*dev
= neigh
->dev
;
1074 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1075 struct ipoib_neigh_table
*ntbl
= &priv
->ntbl
;
1076 struct ipoib_neigh_hash
*htbl
;
1077 struct ipoib_neigh __rcu
**np
;
1078 struct ipoib_neigh
*n
;
1081 htbl
= rcu_dereference_protected(ntbl
->htbl
,
1082 lockdep_is_held(&priv
->lock
));
1086 hash_val
= ipoib_addr_hash(htbl
, neigh
->daddr
);
1087 np
= &htbl
->buckets
[hash_val
];
1088 for (n
= rcu_dereference_protected(*np
,
1089 lockdep_is_held(&priv
->lock
));
1091 n
= rcu_dereference_protected(*np
,
1092 lockdep_is_held(&priv
->lock
))) {
1095 rcu_assign_pointer(*np
,
1096 rcu_dereference_protected(neigh
->hnext
,
1097 lockdep_is_held(&priv
->lock
)));
1098 /* remove from parent list */
1099 list_del(&neigh
->list
);
1100 call_rcu(&neigh
->rcu
, ipoib_neigh_reclaim
);
1108 static int ipoib_neigh_hash_init(struct ipoib_dev_priv
*priv
)
1110 struct ipoib_neigh_table
*ntbl
= &priv
->ntbl
;
1111 struct ipoib_neigh_hash
*htbl
;
1112 struct ipoib_neigh
**buckets
;
1115 clear_bit(IPOIB_NEIGH_TBL_FLUSH
, &priv
->flags
);
1117 htbl
= kzalloc(sizeof(*htbl
), GFP_KERNEL
);
1120 set_bit(IPOIB_STOP_NEIGH_GC
, &priv
->flags
);
1121 size
= roundup_pow_of_two(arp_tbl
.gc_thresh3
);
1122 buckets
= kzalloc(size
* sizeof(*buckets
), GFP_KERNEL
);
1128 htbl
->mask
= (size
- 1);
1129 htbl
->buckets
= buckets
;
1132 atomic_set(&ntbl
->entries
, 0);
1134 /* start garbage collection */
1135 clear_bit(IPOIB_STOP_NEIGH_GC
, &priv
->flags
);
1136 queue_delayed_work(ipoib_workqueue
, &priv
->neigh_reap_task
,
1137 arp_tbl
.gc_interval
);
1142 static void neigh_hash_free_rcu(struct rcu_head
*head
)
1144 struct ipoib_neigh_hash
*htbl
= container_of(head
,
1145 struct ipoib_neigh_hash
,
1147 struct ipoib_neigh __rcu
**buckets
= htbl
->buckets
;
1148 struct ipoib_neigh_table
*ntbl
= htbl
->ntbl
;
1152 complete(&ntbl
->deleted
);
1155 void ipoib_del_neighs_by_gid(struct net_device
*dev
, u8
*gid
)
1157 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1158 struct ipoib_neigh_table
*ntbl
= &priv
->ntbl
;
1159 struct ipoib_neigh_hash
*htbl
;
1160 unsigned long flags
;
1163 /* remove all neigh connected to a given path or mcast */
1164 spin_lock_irqsave(&priv
->lock
, flags
);
1166 htbl
= rcu_dereference_protected(ntbl
->htbl
,
1167 lockdep_is_held(&priv
->lock
));
1172 for (i
= 0; i
< htbl
->size
; i
++) {
1173 struct ipoib_neigh
*neigh
;
1174 struct ipoib_neigh __rcu
**np
= &htbl
->buckets
[i
];
1176 while ((neigh
= rcu_dereference_protected(*np
,
1177 lockdep_is_held(&priv
->lock
))) != NULL
) {
1178 /* delete neighs belong to this parent */
1179 if (!memcmp(gid
, neigh
->daddr
+ 4, sizeof (union ib_gid
))) {
1180 rcu_assign_pointer(*np
,
1181 rcu_dereference_protected(neigh
->hnext
,
1182 lockdep_is_held(&priv
->lock
)));
1183 /* remove from parent list */
1184 list_del(&neigh
->list
);
1185 call_rcu(&neigh
->rcu
, ipoib_neigh_reclaim
);
1193 spin_unlock_irqrestore(&priv
->lock
, flags
);
1196 static void ipoib_flush_neighs(struct ipoib_dev_priv
*priv
)
1198 struct ipoib_neigh_table
*ntbl
= &priv
->ntbl
;
1199 struct ipoib_neigh_hash
*htbl
;
1200 unsigned long flags
;
1201 int i
, wait_flushed
= 0;
1203 init_completion(&priv
->ntbl
.flushed
);
1205 spin_lock_irqsave(&priv
->lock
, flags
);
1207 htbl
= rcu_dereference_protected(ntbl
->htbl
,
1208 lockdep_is_held(&priv
->lock
));
1212 wait_flushed
= atomic_read(&priv
->ntbl
.entries
);
1216 for (i
= 0; i
< htbl
->size
; i
++) {
1217 struct ipoib_neigh
*neigh
;
1218 struct ipoib_neigh __rcu
**np
= &htbl
->buckets
[i
];
1220 while ((neigh
= rcu_dereference_protected(*np
,
1221 lockdep_is_held(&priv
->lock
))) != NULL
) {
1222 rcu_assign_pointer(*np
,
1223 rcu_dereference_protected(neigh
->hnext
,
1224 lockdep_is_held(&priv
->lock
)));
1225 /* remove from path/mc list */
1226 list_del(&neigh
->list
);
1227 call_rcu(&neigh
->rcu
, ipoib_neigh_reclaim
);
1232 rcu_assign_pointer(ntbl
->htbl
, NULL
);
1233 call_rcu(&htbl
->rcu
, neigh_hash_free_rcu
);
1236 spin_unlock_irqrestore(&priv
->lock
, flags
);
1238 wait_for_completion(&priv
->ntbl
.flushed
);
1241 static void ipoib_neigh_hash_uninit(struct net_device
*dev
)
1243 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1246 ipoib_dbg(priv
, "ipoib_neigh_hash_uninit\n");
1247 init_completion(&priv
->ntbl
.deleted
);
1248 set_bit(IPOIB_NEIGH_TBL_FLUSH
, &priv
->flags
);
1250 /* Stop GC if called at init fail need to cancel work */
1251 stopped
= test_and_set_bit(IPOIB_STOP_NEIGH_GC
, &priv
->flags
);
1253 cancel_delayed_work(&priv
->neigh_reap_task
);
1255 ipoib_flush_neighs(priv
);
1257 wait_for_completion(&priv
->ntbl
.deleted
);
1261 int ipoib_dev_init(struct net_device
*dev
, struct ib_device
*ca
, int port
)
1263 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1265 if (ipoib_neigh_hash_init(priv
) < 0)
1267 /* Allocate RX/TX "rings" to hold queued skbs */
1268 priv
->rx_ring
= kzalloc(ipoib_recvq_size
* sizeof *priv
->rx_ring
,
1270 if (!priv
->rx_ring
) {
1271 printk(KERN_WARNING
"%s: failed to allocate RX ring (%d entries)\n",
1272 ca
->name
, ipoib_recvq_size
);
1273 goto out_neigh_hash_cleanup
;
1276 priv
->tx_ring
= vzalloc(ipoib_sendq_size
* sizeof *priv
->tx_ring
);
1277 if (!priv
->tx_ring
) {
1278 printk(KERN_WARNING
"%s: failed to allocate TX ring (%d entries)\n",
1279 ca
->name
, ipoib_sendq_size
);
1280 goto out_rx_ring_cleanup
;
1283 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
1285 if (ipoib_ib_dev_init(dev
, ca
, port
))
1286 goto out_tx_ring_cleanup
;
1290 out_tx_ring_cleanup
:
1291 vfree(priv
->tx_ring
);
1293 out_rx_ring_cleanup
:
1294 kfree(priv
->rx_ring
);
1296 out_neigh_hash_cleanup
:
1297 ipoib_neigh_hash_uninit(dev
);
1302 void ipoib_dev_cleanup(struct net_device
*dev
)
1304 struct ipoib_dev_priv
*priv
= netdev_priv(dev
), *cpriv
, *tcpriv
;
1309 ipoib_delete_debug_files(dev
);
1311 /* Delete any child interfaces first */
1312 list_for_each_entry_safe(cpriv
, tcpriv
, &priv
->child_intfs
, list
) {
1313 /* Stop GC on child */
1314 set_bit(IPOIB_STOP_NEIGH_GC
, &cpriv
->flags
);
1315 cancel_delayed_work(&cpriv
->neigh_reap_task
);
1316 unregister_netdevice_queue(cpriv
->dev
, &head
);
1318 unregister_netdevice_many(&head
);
1320 ipoib_ib_dev_cleanup(dev
);
1322 kfree(priv
->rx_ring
);
1323 vfree(priv
->tx_ring
);
1325 priv
->rx_ring
= NULL
;
1326 priv
->tx_ring
= NULL
;
1328 ipoib_neigh_hash_uninit(dev
);
1331 static const struct header_ops ipoib_header_ops
= {
1332 .create
= ipoib_hard_header
,
1335 static const struct net_device_ops ipoib_netdev_ops
= {
1336 .ndo_uninit
= ipoib_uninit
,
1337 .ndo_open
= ipoib_open
,
1338 .ndo_stop
= ipoib_stop
,
1339 .ndo_change_mtu
= ipoib_change_mtu
,
1340 .ndo_fix_features
= ipoib_fix_features
,
1341 .ndo_start_xmit
= ipoib_start_xmit
,
1342 .ndo_tx_timeout
= ipoib_timeout
,
1343 .ndo_set_rx_mode
= ipoib_set_mcast_list
,
1346 void ipoib_setup(struct net_device
*dev
)
1348 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1350 dev
->netdev_ops
= &ipoib_netdev_ops
;
1351 dev
->header_ops
= &ipoib_header_ops
;
1353 ipoib_set_ethtool_ops(dev
);
1355 netif_napi_add(dev
, &priv
->napi
, ipoib_poll
, 100);
1357 dev
->watchdog_timeo
= HZ
;
1359 dev
->flags
|= IFF_BROADCAST
| IFF_MULTICAST
;
1361 dev
->hard_header_len
= IPOIB_ENCAP_LEN
;
1362 dev
->addr_len
= INFINIBAND_ALEN
;
1363 dev
->type
= ARPHRD_INFINIBAND
;
1364 dev
->tx_queue_len
= ipoib_sendq_size
* 2;
1365 dev
->features
= (NETIF_F_VLAN_CHALLENGED
|
1367 dev
->priv_flags
&= ~IFF_XMIT_DST_RELEASE
;
1369 memcpy(dev
->broadcast
, ipv4_bcast_addr
, INFINIBAND_ALEN
);
1371 netif_carrier_off(dev
);
1375 spin_lock_init(&priv
->lock
);
1377 mutex_init(&priv
->vlan_mutex
);
1379 INIT_LIST_HEAD(&priv
->path_list
);
1380 INIT_LIST_HEAD(&priv
->child_intfs
);
1381 INIT_LIST_HEAD(&priv
->dead_ahs
);
1382 INIT_LIST_HEAD(&priv
->multicast_list
);
1384 INIT_DELAYED_WORK(&priv
->pkey_poll_task
, ipoib_pkey_poll
);
1385 INIT_DELAYED_WORK(&priv
->mcast_task
, ipoib_mcast_join_task
);
1386 INIT_WORK(&priv
->carrier_on_task
, ipoib_mcast_carrier_on_task
);
1387 INIT_WORK(&priv
->flush_light
, ipoib_ib_dev_flush_light
);
1388 INIT_WORK(&priv
->flush_normal
, ipoib_ib_dev_flush_normal
);
1389 INIT_WORK(&priv
->flush_heavy
, ipoib_ib_dev_flush_heavy
);
1390 INIT_WORK(&priv
->restart_task
, ipoib_mcast_restart_task
);
1391 INIT_DELAYED_WORK(&priv
->ah_reap_task
, ipoib_reap_ah
);
1392 INIT_DELAYED_WORK(&priv
->neigh_reap_task
, ipoib_reap_neigh
);
1395 struct ipoib_dev_priv
*ipoib_intf_alloc(const char *name
)
1397 struct net_device
*dev
;
1399 dev
= alloc_netdev((int) sizeof (struct ipoib_dev_priv
), name
,
1404 return netdev_priv(dev
);
1407 static ssize_t
show_pkey(struct device
*dev
,
1408 struct device_attribute
*attr
, char *buf
)
1410 struct ipoib_dev_priv
*priv
= netdev_priv(to_net_dev(dev
));
1412 return sprintf(buf
, "0x%04x\n", priv
->pkey
);
1414 static DEVICE_ATTR(pkey
, S_IRUGO
, show_pkey
, NULL
);
1416 static ssize_t
show_umcast(struct device
*dev
,
1417 struct device_attribute
*attr
, char *buf
)
1419 struct ipoib_dev_priv
*priv
= netdev_priv(to_net_dev(dev
));
1421 return sprintf(buf
, "%d\n", test_bit(IPOIB_FLAG_UMCAST
, &priv
->flags
));
1424 void ipoib_set_umcast(struct net_device
*ndev
, int umcast_val
)
1426 struct ipoib_dev_priv
*priv
= netdev_priv(ndev
);
1428 if (umcast_val
> 0) {
1429 set_bit(IPOIB_FLAG_UMCAST
, &priv
->flags
);
1430 ipoib_warn(priv
, "ignoring multicast groups joined directly "
1433 clear_bit(IPOIB_FLAG_UMCAST
, &priv
->flags
);
1436 static ssize_t
set_umcast(struct device
*dev
,
1437 struct device_attribute
*attr
,
1438 const char *buf
, size_t count
)
1440 unsigned long umcast_val
= simple_strtoul(buf
, NULL
, 0);
1442 ipoib_set_umcast(to_net_dev(dev
), umcast_val
);
1446 static DEVICE_ATTR(umcast
, S_IWUSR
| S_IRUGO
, show_umcast
, set_umcast
);
1448 int ipoib_add_umcast_attr(struct net_device
*dev
)
1450 return device_create_file(&dev
->dev
, &dev_attr_umcast
);
1453 static ssize_t
create_child(struct device
*dev
,
1454 struct device_attribute
*attr
,
1455 const char *buf
, size_t count
)
1460 if (sscanf(buf
, "%i", &pkey
) != 1)
1463 if (pkey
<= 0 || pkey
> 0xffff || pkey
== 0x8000)
1467 * Set the full membership bit, so that we join the right
1468 * broadcast group, etc.
1472 ret
= ipoib_vlan_add(to_net_dev(dev
), pkey
);
1474 return ret
? ret
: count
;
1476 static DEVICE_ATTR(create_child
, S_IWUSR
, NULL
, create_child
);
1478 static ssize_t
delete_child(struct device
*dev
,
1479 struct device_attribute
*attr
,
1480 const char *buf
, size_t count
)
1485 if (sscanf(buf
, "%i", &pkey
) != 1)
1488 if (pkey
< 0 || pkey
> 0xffff)
1491 ret
= ipoib_vlan_delete(to_net_dev(dev
), pkey
);
1493 return ret
? ret
: count
;
1496 static DEVICE_ATTR(delete_child
, S_IWUSR
, NULL
, delete_child
);
1498 int ipoib_add_pkey_attr(struct net_device
*dev
)
1500 return device_create_file(&dev
->dev
, &dev_attr_pkey
);
1503 int ipoib_set_dev_features(struct ipoib_dev_priv
*priv
, struct ib_device
*hca
)
1505 struct ib_device_attr
*device_attr
;
1506 int result
= -ENOMEM
;
1508 device_attr
= kmalloc(sizeof *device_attr
, GFP_KERNEL
);
1510 printk(KERN_WARNING
"%s: allocation of %zu bytes failed\n",
1511 hca
->name
, sizeof *device_attr
);
1515 result
= ib_query_device(hca
, device_attr
);
1517 printk(KERN_WARNING
"%s: ib_query_device failed (ret = %d)\n",
1522 priv
->hca_caps
= device_attr
->device_cap_flags
;
1526 if (priv
->hca_caps
& IB_DEVICE_UD_IP_CSUM
) {
1527 priv
->dev
->hw_features
= NETIF_F_SG
|
1528 NETIF_F_IP_CSUM
| NETIF_F_RXCSUM
;
1530 if (priv
->hca_caps
& IB_DEVICE_UD_TSO
)
1531 priv
->dev
->hw_features
|= NETIF_F_TSO
;
1533 priv
->dev
->features
|= priv
->dev
->hw_features
;
1539 static struct net_device
*ipoib_add_port(const char *format
,
1540 struct ib_device
*hca
, u8 port
)
1542 struct ipoib_dev_priv
*priv
;
1543 struct ib_port_attr attr
;
1544 int result
= -ENOMEM
;
1546 priv
= ipoib_intf_alloc(format
);
1548 goto alloc_mem_failed
;
1550 SET_NETDEV_DEV(priv
->dev
, hca
->dma_device
);
1551 priv
->dev
->dev_id
= port
- 1;
1553 if (!ib_query_port(hca
, port
, &attr
))
1554 priv
->max_ib_mtu
= ib_mtu_enum_to_int(attr
.max_mtu
);
1556 printk(KERN_WARNING
"%s: ib_query_port %d failed\n",
1558 goto device_init_failed
;
1561 /* MTU will be reset when mcast join happens */
1562 priv
->dev
->mtu
= IPOIB_UD_MTU(priv
->max_ib_mtu
);
1563 priv
->mcast_mtu
= priv
->admin_mtu
= priv
->dev
->mtu
;
1565 priv
->dev
->neigh_priv_len
= sizeof(struct ipoib_neigh
);
1567 result
= ib_query_pkey(hca
, port
, 0, &priv
->pkey
);
1569 printk(KERN_WARNING
"%s: ib_query_pkey port %d failed (ret = %d)\n",
1570 hca
->name
, port
, result
);
1571 goto device_init_failed
;
1574 if (ipoib_set_dev_features(priv
, hca
))
1575 goto device_init_failed
;
1578 * Set the full membership bit, so that we join the right
1579 * broadcast group, etc.
1581 priv
->pkey
|= 0x8000;
1583 priv
->dev
->broadcast
[8] = priv
->pkey
>> 8;
1584 priv
->dev
->broadcast
[9] = priv
->pkey
& 0xff;
1586 result
= ib_query_gid(hca
, port
, 0, &priv
->local_gid
);
1588 printk(KERN_WARNING
"%s: ib_query_gid port %d failed (ret = %d)\n",
1589 hca
->name
, port
, result
);
1590 goto device_init_failed
;
1592 memcpy(priv
->dev
->dev_addr
+ 4, priv
->local_gid
.raw
, sizeof (union ib_gid
));
1594 result
= ipoib_dev_init(priv
->dev
, hca
, port
);
1596 printk(KERN_WARNING
"%s: failed to initialize port %d (ret = %d)\n",
1597 hca
->name
, port
, result
);
1598 goto device_init_failed
;
1601 INIT_IB_EVENT_HANDLER(&priv
->event_handler
,
1602 priv
->ca
, ipoib_event
);
1603 result
= ib_register_event_handler(&priv
->event_handler
);
1605 printk(KERN_WARNING
"%s: ib_register_event_handler failed for "
1606 "port %d (ret = %d)\n",
1607 hca
->name
, port
, result
);
1611 result
= register_netdev(priv
->dev
);
1613 printk(KERN_WARNING
"%s: couldn't register ipoib port %d; error %d\n",
1614 hca
->name
, port
, result
);
1615 goto register_failed
;
1618 ipoib_create_debug_files(priv
->dev
);
1620 if (ipoib_cm_add_mode_attr(priv
->dev
))
1622 if (ipoib_add_pkey_attr(priv
->dev
))
1624 if (ipoib_add_umcast_attr(priv
->dev
))
1626 if (device_create_file(&priv
->dev
->dev
, &dev_attr_create_child
))
1628 if (device_create_file(&priv
->dev
->dev
, &dev_attr_delete_child
))
1634 ipoib_delete_debug_files(priv
->dev
);
1635 unregister_netdev(priv
->dev
);
1638 ib_unregister_event_handler(&priv
->event_handler
);
1639 /* Stop GC if started before flush */
1640 set_bit(IPOIB_STOP_NEIGH_GC
, &priv
->flags
);
1641 cancel_delayed_work(&priv
->neigh_reap_task
);
1642 flush_workqueue(ipoib_workqueue
);
1645 ipoib_dev_cleanup(priv
->dev
);
1648 free_netdev(priv
->dev
);
1651 return ERR_PTR(result
);
1654 static void ipoib_add_one(struct ib_device
*device
)
1656 struct list_head
*dev_list
;
1657 struct net_device
*dev
;
1658 struct ipoib_dev_priv
*priv
;
1661 if (rdma_node_get_transport(device
->node_type
) != RDMA_TRANSPORT_IB
)
1664 dev_list
= kmalloc(sizeof *dev_list
, GFP_KERNEL
);
1668 INIT_LIST_HEAD(dev_list
);
1670 if (device
->node_type
== RDMA_NODE_IB_SWITCH
) {
1675 e
= device
->phys_port_cnt
;
1678 for (p
= s
; p
<= e
; ++p
) {
1679 if (rdma_port_get_link_layer(device
, p
) != IB_LINK_LAYER_INFINIBAND
)
1681 dev
= ipoib_add_port("ib%d", device
, p
);
1683 priv
= netdev_priv(dev
);
1684 list_add_tail(&priv
->list
, dev_list
);
1688 ib_set_client_data(device
, &ipoib_client
, dev_list
);
1691 static void ipoib_remove_one(struct ib_device
*device
)
1693 struct ipoib_dev_priv
*priv
, *tmp
;
1694 struct list_head
*dev_list
;
1696 if (rdma_node_get_transport(device
->node_type
) != RDMA_TRANSPORT_IB
)
1699 dev_list
= ib_get_client_data(device
, &ipoib_client
);
1703 list_for_each_entry_safe(priv
, tmp
, dev_list
, list
) {
1704 ib_unregister_event_handler(&priv
->event_handler
);
1707 dev_change_flags(priv
->dev
, priv
->dev
->flags
& ~IFF_UP
);
1711 set_bit(IPOIB_STOP_NEIGH_GC
, &priv
->flags
);
1712 cancel_delayed_work(&priv
->neigh_reap_task
);
1713 flush_workqueue(ipoib_workqueue
);
1715 unregister_netdev(priv
->dev
);
1716 free_netdev(priv
->dev
);
1722 static int __init
ipoib_init_module(void)
1726 ipoib_recvq_size
= roundup_pow_of_two(ipoib_recvq_size
);
1727 ipoib_recvq_size
= min(ipoib_recvq_size
, IPOIB_MAX_QUEUE_SIZE
);
1728 ipoib_recvq_size
= max(ipoib_recvq_size
, IPOIB_MIN_QUEUE_SIZE
);
1730 ipoib_sendq_size
= roundup_pow_of_two(ipoib_sendq_size
);
1731 ipoib_sendq_size
= min(ipoib_sendq_size
, IPOIB_MAX_QUEUE_SIZE
);
1732 ipoib_sendq_size
= max3(ipoib_sendq_size
, 2 * MAX_SEND_CQE
, IPOIB_MIN_QUEUE_SIZE
);
1733 #ifdef CONFIG_INFINIBAND_IPOIB_CM
1734 ipoib_max_conn_qp
= min(ipoib_max_conn_qp
, IPOIB_CM_MAX_CONN_QP
);
1738 * When copying small received packets, we only copy from the
1739 * linear data part of the SKB, so we rely on this condition.
1741 BUILD_BUG_ON(IPOIB_CM_COPYBREAK
> IPOIB_CM_HEAD_SIZE
);
1743 ret
= ipoib_register_debugfs();
1748 * We create our own workqueue mainly because we want to be
1749 * able to flush it when devices are being removed. We can't
1750 * use schedule_work()/flush_scheduled_work() because both
1751 * unregister_netdev() and linkwatch_event take the rtnl lock,
1752 * so flush_scheduled_work() can deadlock during device
1755 ipoib_workqueue
= create_singlethread_workqueue("ipoib");
1756 if (!ipoib_workqueue
) {
1761 ib_sa_register_client(&ipoib_sa_client
);
1763 ret
= ib_register_client(&ipoib_client
);
1767 ret
= ipoib_netlink_init();
1774 ib_unregister_client(&ipoib_client
);
1777 ib_sa_unregister_client(&ipoib_sa_client
);
1778 destroy_workqueue(ipoib_workqueue
);
1781 ipoib_unregister_debugfs();
1786 static void __exit
ipoib_cleanup_module(void)
1788 ipoib_netlink_fini();
1789 ib_unregister_client(&ipoib_client
);
1790 ib_sa_unregister_client(&ipoib_sa_client
);
1791 ipoib_unregister_debugfs();
1792 destroy_workqueue(ipoib_workqueue
);
1795 module_init(ipoib_init_module
);
1796 module_exit(ipoib_cleanup_module
);