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 MODULE_AUTHOR("Roland Dreier");
53 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
54 MODULE_LICENSE("Dual BSD/GPL");
56 int ipoib_sendq_size __read_mostly
= IPOIB_TX_RING_SIZE
;
57 int ipoib_recvq_size __read_mostly
= IPOIB_RX_RING_SIZE
;
59 module_param_named(send_queue_size
, ipoib_sendq_size
, int, 0444);
60 MODULE_PARM_DESC(send_queue_size
, "Number of descriptors in send queue");
61 module_param_named(recv_queue_size
, ipoib_recvq_size
, int, 0444);
62 MODULE_PARM_DESC(recv_queue_size
, "Number of descriptors in receive queue");
64 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
65 int ipoib_debug_level
;
67 module_param_named(debug_level
, ipoib_debug_level
, int, 0644);
68 MODULE_PARM_DESC(debug_level
, "Enable debug tracing if > 0");
71 struct ipoib_path_iter
{
72 struct net_device
*dev
;
73 struct ipoib_path path
;
76 static const u8 ipv4_bcast_addr
[] = {
77 0x00, 0xff, 0xff, 0xff,
78 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
79 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
82 struct workqueue_struct
*ipoib_workqueue
;
84 struct ib_sa_client ipoib_sa_client
;
86 static void ipoib_add_one(struct ib_device
*device
);
87 static void ipoib_remove_one(struct ib_device
*device
);
88 static void ipoib_neigh_reclaim(struct rcu_head
*rp
);
90 static struct ib_client ipoib_client
= {
93 .remove
= ipoib_remove_one
96 int ipoib_open(struct net_device
*dev
)
98 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
100 ipoib_dbg(priv
, "bringing up interface\n");
102 set_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
);
104 if (ipoib_pkey_dev_delay_open(dev
))
107 if (ipoib_ib_dev_open(dev
))
110 if (ipoib_ib_dev_up(dev
))
113 if (!test_bit(IPOIB_FLAG_SUBINTERFACE
, &priv
->flags
)) {
114 struct ipoib_dev_priv
*cpriv
;
116 /* Bring up any child interfaces too */
117 mutex_lock(&priv
->vlan_mutex
);
118 list_for_each_entry(cpriv
, &priv
->child_intfs
, list
) {
121 flags
= cpriv
->dev
->flags
;
125 dev_change_flags(cpriv
->dev
, flags
| IFF_UP
);
127 mutex_unlock(&priv
->vlan_mutex
);
130 netif_start_queue(dev
);
135 ipoib_ib_dev_stop(dev
, 1);
138 clear_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
);
143 static int ipoib_stop(struct net_device
*dev
)
145 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
147 ipoib_dbg(priv
, "stopping interface\n");
149 clear_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
);
151 netif_stop_queue(dev
);
153 ipoib_ib_dev_down(dev
, 0);
154 ipoib_ib_dev_stop(dev
, 0);
156 if (!test_bit(IPOIB_FLAG_SUBINTERFACE
, &priv
->flags
)) {
157 struct ipoib_dev_priv
*cpriv
;
159 /* Bring down any child interfaces too */
160 mutex_lock(&priv
->vlan_mutex
);
161 list_for_each_entry(cpriv
, &priv
->child_intfs
, list
) {
164 flags
= cpriv
->dev
->flags
;
165 if (!(flags
& IFF_UP
))
168 dev_change_flags(cpriv
->dev
, flags
& ~IFF_UP
);
170 mutex_unlock(&priv
->vlan_mutex
);
176 static void ipoib_uninit(struct net_device
*dev
)
178 ipoib_dev_cleanup(dev
);
181 static netdev_features_t
ipoib_fix_features(struct net_device
*dev
, netdev_features_t features
)
183 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
185 if (test_bit(IPOIB_FLAG_ADMIN_CM
, &priv
->flags
))
186 features
&= ~(NETIF_F_SG
| NETIF_F_IP_CSUM
| NETIF_F_TSO
);
191 static int ipoib_change_mtu(struct net_device
*dev
, int new_mtu
)
193 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
195 /* dev->mtu > 2K ==> connected mode */
196 if (ipoib_cm_admin_enabled(dev
)) {
197 if (new_mtu
> ipoib_cm_max_mtu(dev
))
200 if (new_mtu
> priv
->mcast_mtu
)
201 ipoib_warn(priv
, "mtu > %d will cause multicast packet drops.\n",
208 if (new_mtu
> IPOIB_UD_MTU(priv
->max_ib_mtu
))
211 priv
->admin_mtu
= new_mtu
;
213 dev
->mtu
= min(priv
->mcast_mtu
, priv
->admin_mtu
);
218 static struct ipoib_path
*__path_find(struct net_device
*dev
, void *gid
)
220 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
221 struct rb_node
*n
= priv
->path_tree
.rb_node
;
222 struct ipoib_path
*path
;
226 path
= rb_entry(n
, struct ipoib_path
, rb_node
);
228 ret
= memcmp(gid
, path
->pathrec
.dgid
.raw
,
229 sizeof (union ib_gid
));
242 static int __path_add(struct net_device
*dev
, struct ipoib_path
*path
)
244 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
245 struct rb_node
**n
= &priv
->path_tree
.rb_node
;
246 struct rb_node
*pn
= NULL
;
247 struct ipoib_path
*tpath
;
252 tpath
= rb_entry(pn
, struct ipoib_path
, rb_node
);
254 ret
= memcmp(path
->pathrec
.dgid
.raw
, tpath
->pathrec
.dgid
.raw
,
255 sizeof (union ib_gid
));
264 rb_link_node(&path
->rb_node
, pn
, n
);
265 rb_insert_color(&path
->rb_node
, &priv
->path_tree
);
267 list_add_tail(&path
->list
, &priv
->path_list
);
272 static void path_free(struct net_device
*dev
, struct ipoib_path
*path
)
276 while ((skb
= __skb_dequeue(&path
->queue
)))
277 dev_kfree_skb_irq(skb
);
279 ipoib_dbg(netdev_priv(dev
), "path_free\n");
281 /* remove all neigh connected to this path */
282 ipoib_del_neighs_by_gid(dev
, path
->pathrec
.dgid
.raw
);
285 ipoib_put_ah(path
->ah
);
290 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
292 struct ipoib_path_iter
*ipoib_path_iter_init(struct net_device
*dev
)
294 struct ipoib_path_iter
*iter
;
296 iter
= kmalloc(sizeof *iter
, GFP_KERNEL
);
301 memset(iter
->path
.pathrec
.dgid
.raw
, 0, 16);
303 if (ipoib_path_iter_next(iter
)) {
311 int ipoib_path_iter_next(struct ipoib_path_iter
*iter
)
313 struct ipoib_dev_priv
*priv
= netdev_priv(iter
->dev
);
315 struct ipoib_path
*path
;
318 spin_lock_irq(&priv
->lock
);
320 n
= rb_first(&priv
->path_tree
);
323 path
= rb_entry(n
, struct ipoib_path
, rb_node
);
325 if (memcmp(iter
->path
.pathrec
.dgid
.raw
, path
->pathrec
.dgid
.raw
,
326 sizeof (union ib_gid
)) < 0) {
335 spin_unlock_irq(&priv
->lock
);
340 void ipoib_path_iter_read(struct ipoib_path_iter
*iter
,
341 struct ipoib_path
*path
)
346 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
348 void ipoib_mark_paths_invalid(struct net_device
*dev
)
350 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
351 struct ipoib_path
*path
, *tp
;
353 spin_lock_irq(&priv
->lock
);
355 list_for_each_entry_safe(path
, tp
, &priv
->path_list
, list
) {
356 ipoib_dbg(priv
, "mark path LID 0x%04x GID %pI6 invalid\n",
357 be16_to_cpu(path
->pathrec
.dlid
),
358 path
->pathrec
.dgid
.raw
);
362 spin_unlock_irq(&priv
->lock
);
365 void ipoib_flush_paths(struct net_device
*dev
)
367 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
368 struct ipoib_path
*path
, *tp
;
369 LIST_HEAD(remove_list
);
372 netif_tx_lock_bh(dev
);
373 spin_lock_irqsave(&priv
->lock
, flags
);
375 list_splice_init(&priv
->path_list
, &remove_list
);
377 list_for_each_entry(path
, &remove_list
, list
)
378 rb_erase(&path
->rb_node
, &priv
->path_tree
);
380 list_for_each_entry_safe(path
, tp
, &remove_list
, list
) {
382 ib_sa_cancel_query(path
->query_id
, path
->query
);
383 spin_unlock_irqrestore(&priv
->lock
, flags
);
384 netif_tx_unlock_bh(dev
);
385 wait_for_completion(&path
->done
);
386 path_free(dev
, path
);
387 netif_tx_lock_bh(dev
);
388 spin_lock_irqsave(&priv
->lock
, flags
);
391 spin_unlock_irqrestore(&priv
->lock
, flags
);
392 netif_tx_unlock_bh(dev
);
395 static void path_rec_completion(int status
,
396 struct ib_sa_path_rec
*pathrec
,
399 struct ipoib_path
*path
= path_ptr
;
400 struct net_device
*dev
= path
->dev
;
401 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
402 struct ipoib_ah
*ah
= NULL
;
403 struct ipoib_ah
*old_ah
= NULL
;
404 struct ipoib_neigh
*neigh
, *tn
;
405 struct sk_buff_head skqueue
;
410 ipoib_dbg(priv
, "PathRec LID 0x%04x for GID %pI6\n",
411 be16_to_cpu(pathrec
->dlid
), pathrec
->dgid
.raw
);
413 ipoib_dbg(priv
, "PathRec status %d for GID %pI6\n",
414 status
, path
->pathrec
.dgid
.raw
);
416 skb_queue_head_init(&skqueue
);
419 struct ib_ah_attr av
;
421 if (!ib_init_ah_from_path(priv
->ca
, priv
->port
, pathrec
, &av
))
422 ah
= ipoib_create_ah(dev
, priv
->pd
, &av
);
425 spin_lock_irqsave(&priv
->lock
, flags
);
427 if (!IS_ERR_OR_NULL(ah
)) {
428 path
->pathrec
= *pathrec
;
433 ipoib_dbg(priv
, "created address handle %p for LID 0x%04x, SL %d\n",
434 ah
, be16_to_cpu(pathrec
->dlid
), pathrec
->sl
);
436 while ((skb
= __skb_dequeue(&path
->queue
)))
437 __skb_queue_tail(&skqueue
, skb
);
439 list_for_each_entry_safe(neigh
, tn
, &path
->neigh_list
, list
) {
441 WARN_ON(neigh
->ah
!= old_ah
);
443 * Dropping the ah reference inside
444 * priv->lock is safe here, because we
445 * will hold one more reference from
446 * the original value of path->ah (ie
449 ipoib_put_ah(neigh
->ah
);
451 kref_get(&path
->ah
->ref
);
452 neigh
->ah
= path
->ah
;
454 if (ipoib_cm_enabled(dev
, neigh
->daddr
)) {
455 if (!ipoib_cm_get(neigh
))
456 ipoib_cm_set(neigh
, ipoib_cm_create_tx(dev
,
459 if (!ipoib_cm_get(neigh
)) {
460 list_del(&neigh
->list
);
461 ipoib_neigh_free(neigh
);
466 while ((skb
= __skb_dequeue(&neigh
->queue
)))
467 __skb_queue_tail(&skqueue
, skb
);
473 complete(&path
->done
);
475 spin_unlock_irqrestore(&priv
->lock
, flags
);
478 ipoib_put_ah(old_ah
);
480 while ((skb
= __skb_dequeue(&skqueue
))) {
482 if (dev_queue_xmit(skb
))
483 ipoib_warn(priv
, "dev_queue_xmit failed "
484 "to requeue packet\n");
488 static struct ipoib_path
*path_rec_create(struct net_device
*dev
, void *gid
)
490 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
491 struct ipoib_path
*path
;
493 if (!priv
->broadcast
)
496 path
= kzalloc(sizeof *path
, GFP_ATOMIC
);
502 skb_queue_head_init(&path
->queue
);
504 INIT_LIST_HEAD(&path
->neigh_list
);
506 memcpy(path
->pathrec
.dgid
.raw
, gid
, sizeof (union ib_gid
));
507 path
->pathrec
.sgid
= priv
->local_gid
;
508 path
->pathrec
.pkey
= cpu_to_be16(priv
->pkey
);
509 path
->pathrec
.numb_path
= 1;
510 path
->pathrec
.traffic_class
= priv
->broadcast
->mcmember
.traffic_class
;
515 static int path_rec_start(struct net_device
*dev
,
516 struct ipoib_path
*path
)
518 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
520 ipoib_dbg(priv
, "Start path record lookup for %pI6\n",
521 path
->pathrec
.dgid
.raw
);
523 init_completion(&path
->done
);
526 ib_sa_path_rec_get(&ipoib_sa_client
, priv
->ca
, priv
->port
,
528 IB_SA_PATH_REC_DGID
|
529 IB_SA_PATH_REC_SGID
|
530 IB_SA_PATH_REC_NUMB_PATH
|
531 IB_SA_PATH_REC_TRAFFIC_CLASS
|
536 if (path
->query_id
< 0) {
537 ipoib_warn(priv
, "ib_sa_path_rec_get failed: %d\n", path
->query_id
);
539 complete(&path
->done
);
540 return path
->query_id
;
546 static void neigh_add_path(struct sk_buff
*skb
, u8
*daddr
,
547 struct net_device
*dev
)
549 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
550 struct ipoib_path
*path
;
551 struct ipoib_neigh
*neigh
;
554 neigh
= ipoib_neigh_alloc(daddr
, dev
);
556 ++dev
->stats
.tx_dropped
;
557 dev_kfree_skb_any(skb
);
561 spin_lock_irqsave(&priv
->lock
, flags
);
563 path
= __path_find(dev
, daddr
+ 4);
565 path
= path_rec_create(dev
, daddr
+ 4);
569 __path_add(dev
, path
);
572 list_add_tail(&neigh
->list
, &path
->neigh_list
);
575 kref_get(&path
->ah
->ref
);
576 neigh
->ah
= path
->ah
;
578 if (ipoib_cm_enabled(dev
, neigh
->daddr
)) {
579 if (!ipoib_cm_get(neigh
))
580 ipoib_cm_set(neigh
, ipoib_cm_create_tx(dev
, path
, neigh
));
581 if (!ipoib_cm_get(neigh
)) {
582 list_del(&neigh
->list
);
583 ipoib_neigh_free(neigh
);
586 if (skb_queue_len(&neigh
->queue
) < IPOIB_MAX_PATH_REC_QUEUE
)
587 __skb_queue_tail(&neigh
->queue
, skb
);
589 ipoib_warn(priv
, "queue length limit %d. Packet drop.\n",
590 skb_queue_len(&neigh
->queue
));
594 spin_unlock_irqrestore(&priv
->lock
, flags
);
595 ipoib_send(dev
, skb
, path
->ah
, IPOIB_QPN(daddr
));
596 ipoib_neigh_put(neigh
);
602 if (!path
->query
&& path_rec_start(dev
, path
))
605 __skb_queue_tail(&neigh
->queue
, skb
);
608 spin_unlock_irqrestore(&priv
->lock
, flags
);
609 ipoib_neigh_put(neigh
);
613 list_del(&neigh
->list
);
616 ipoib_neigh_free(neigh
);
618 ++dev
->stats
.tx_dropped
;
619 dev_kfree_skb_any(skb
);
621 spin_unlock_irqrestore(&priv
->lock
, flags
);
622 ipoib_neigh_put(neigh
);
625 static void unicast_arp_send(struct sk_buff
*skb
, struct net_device
*dev
,
628 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
629 struct ipoib_path
*path
;
632 spin_lock_irqsave(&priv
->lock
, flags
);
634 path
= __path_find(dev
, cb
->hwaddr
+ 4);
635 if (!path
|| !path
->valid
) {
639 path
= path_rec_create(dev
, cb
->hwaddr
+ 4);
643 __skb_queue_tail(&path
->queue
, skb
);
645 if (!path
->query
&& path_rec_start(dev
, path
)) {
646 spin_unlock_irqrestore(&priv
->lock
, flags
);
648 path_free(dev
, path
);
651 __path_add(dev
, path
);
653 ++dev
->stats
.tx_dropped
;
654 dev_kfree_skb_any(skb
);
657 spin_unlock_irqrestore(&priv
->lock
, flags
);
662 ipoib_dbg(priv
, "Send unicast ARP to %04x\n",
663 be16_to_cpu(path
->pathrec
.dlid
));
665 spin_unlock_irqrestore(&priv
->lock
, flags
);
666 ipoib_send(dev
, skb
, path
->ah
, IPOIB_QPN(cb
->hwaddr
));
668 } else if ((path
->query
|| !path_rec_start(dev
, path
)) &&
669 skb_queue_len(&path
->queue
) < IPOIB_MAX_PATH_REC_QUEUE
) {
670 __skb_queue_tail(&path
->queue
, skb
);
672 ++dev
->stats
.tx_dropped
;
673 dev_kfree_skb_any(skb
);
676 spin_unlock_irqrestore(&priv
->lock
, flags
);
679 static int ipoib_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
681 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
682 struct ipoib_neigh
*neigh
;
683 struct ipoib_cb
*cb
= (struct ipoib_cb
*) skb
->cb
;
684 struct ipoib_header
*header
;
687 header
= (struct ipoib_header
*) skb
->data
;
689 if (unlikely(cb
->hwaddr
[4] == 0xff)) {
690 /* multicast, arrange "if" according to probability */
691 if ((header
->proto
!= htons(ETH_P_IP
)) &&
692 (header
->proto
!= htons(ETH_P_IPV6
)) &&
693 (header
->proto
!= htons(ETH_P_ARP
)) &&
694 (header
->proto
!= htons(ETH_P_RARP
))) {
695 /* ethertype not supported by IPoIB */
696 ++dev
->stats
.tx_dropped
;
697 dev_kfree_skb_any(skb
);
700 /* Add in the P_Key for multicast*/
701 cb
->hwaddr
[8] = (priv
->pkey
>> 8) & 0xff;
702 cb
->hwaddr
[9] = priv
->pkey
& 0xff;
704 neigh
= ipoib_neigh_get(dev
, cb
->hwaddr
);
706 goto send_using_neigh
;
707 ipoib_mcast_send(dev
, cb
->hwaddr
, skb
);
711 /* unicast, arrange "switch" according to probability */
712 switch (header
->proto
) {
713 case htons(ETH_P_IP
):
714 case htons(ETH_P_IPV6
):
715 neigh
= ipoib_neigh_get(dev
, cb
->hwaddr
);
716 if (unlikely(!neigh
)) {
717 neigh_add_path(skb
, cb
->hwaddr
, dev
);
721 case htons(ETH_P_ARP
):
722 case htons(ETH_P_RARP
):
723 /* for unicast ARP and RARP should always perform path find */
724 unicast_arp_send(skb
, dev
, cb
);
727 /* ethertype not supported by IPoIB */
728 ++dev
->stats
.tx_dropped
;
729 dev_kfree_skb_any(skb
);
734 /* note we now hold a ref to neigh */
735 if (ipoib_cm_get(neigh
)) {
736 if (ipoib_cm_up(neigh
)) {
737 ipoib_cm_send(dev
, skb
, ipoib_cm_get(neigh
));
740 } else if (neigh
->ah
) {
741 ipoib_send(dev
, skb
, neigh
->ah
, IPOIB_QPN(cb
->hwaddr
));
745 if (skb_queue_len(&neigh
->queue
) < IPOIB_MAX_PATH_REC_QUEUE
) {
746 spin_lock_irqsave(&priv
->lock
, flags
);
747 __skb_queue_tail(&neigh
->queue
, skb
);
748 spin_unlock_irqrestore(&priv
->lock
, flags
);
750 ++dev
->stats
.tx_dropped
;
751 dev_kfree_skb_any(skb
);
755 ipoib_neigh_put(neigh
);
760 static void ipoib_timeout(struct net_device
*dev
)
762 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
764 ipoib_warn(priv
, "transmit timeout: latency %d msecs\n",
765 jiffies_to_msecs(jiffies
- dev
->trans_start
));
766 ipoib_warn(priv
, "queue stopped %d, tx_head %u, tx_tail %u\n",
767 netif_queue_stopped(dev
),
768 priv
->tx_head
, priv
->tx_tail
);
769 /* XXX reset QP, etc. */
772 static int ipoib_hard_header(struct sk_buff
*skb
,
773 struct net_device
*dev
,
775 const void *daddr
, const void *saddr
, unsigned len
)
777 struct ipoib_header
*header
;
778 struct ipoib_cb
*cb
= (struct ipoib_cb
*) skb
->cb
;
780 header
= (struct ipoib_header
*) skb_push(skb
, sizeof *header
);
782 header
->proto
= htons(type
);
783 header
->reserved
= 0;
786 * we don't rely on dst_entry structure, always stuff the
787 * destination address into skb->cb so we can figure out where
788 * to send the packet later.
790 memcpy(cb
->hwaddr
, daddr
, INFINIBAND_ALEN
);
795 static void ipoib_set_mcast_list(struct net_device
*dev
)
797 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
799 if (!test_bit(IPOIB_FLAG_OPER_UP
, &priv
->flags
)) {
800 ipoib_dbg(priv
, "IPOIB_FLAG_OPER_UP not set");
804 queue_work(ipoib_workqueue
, &priv
->restart_task
);
807 static u32
ipoib_addr_hash(struct ipoib_neigh_hash
*htbl
, u8
*daddr
)
810 * Use only the address parts that contributes to spreading
811 * The subnet prefix is not used as one can not connect to
812 * same remote port (GUID) using the same remote QPN via two
815 /* qpn octets[1:4) & port GUID octets[12:20) */
816 u32
*daddr_32
= (u32
*) daddr
;
819 hv
= jhash_3words(daddr_32
[3], daddr_32
[4], 0xFFFFFF & daddr_32
[0], 0);
820 return hv
& htbl
->mask
;
823 struct ipoib_neigh
*ipoib_neigh_get(struct net_device
*dev
, u8
*daddr
)
825 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
826 struct ipoib_neigh_table
*ntbl
= &priv
->ntbl
;
827 struct ipoib_neigh_hash
*htbl
;
828 struct ipoib_neigh
*neigh
= NULL
;
833 htbl
= rcu_dereference_bh(ntbl
->htbl
);
838 hash_val
= ipoib_addr_hash(htbl
, daddr
);
839 for (neigh
= rcu_dereference_bh(htbl
->buckets
[hash_val
]);
841 neigh
= rcu_dereference_bh(neigh
->hnext
)) {
842 if (memcmp(daddr
, neigh
->daddr
, INFINIBAND_ALEN
) == 0) {
843 /* found, take one ref on behalf of the caller */
844 if (!atomic_inc_not_zero(&neigh
->refcnt
)) {
849 neigh
->alive
= jiffies
;
855 rcu_read_unlock_bh();
859 static void __ipoib_reap_neigh(struct ipoib_dev_priv
*priv
)
861 struct ipoib_neigh_table
*ntbl
= &priv
->ntbl
;
862 struct ipoib_neigh_hash
*htbl
;
863 unsigned long neigh_obsolete
;
868 if (test_bit(IPOIB_STOP_NEIGH_GC
, &priv
->flags
))
871 write_lock_bh(&ntbl
->rwlock
);
873 htbl
= rcu_dereference_protected(ntbl
->htbl
,
874 lockdep_is_held(&ntbl
->rwlock
));
879 /* neigh is obsolete if it was idle for two GC periods */
880 dt
= 2 * arp_tbl
.gc_interval
;
881 neigh_obsolete
= jiffies
- dt
;
882 /* handle possible race condition */
883 if (test_bit(IPOIB_STOP_NEIGH_GC
, &priv
->flags
))
886 for (i
= 0; i
< htbl
->size
; i
++) {
887 struct ipoib_neigh
*neigh
;
888 struct ipoib_neigh __rcu
**np
= &htbl
->buckets
[i
];
890 while ((neigh
= rcu_dereference_protected(*np
,
891 lockdep_is_held(&ntbl
->rwlock
))) != NULL
) {
892 /* was the neigh idle for two GC periods */
893 if (time_after(neigh_obsolete
, neigh
->alive
)) {
894 rcu_assign_pointer(*np
,
895 rcu_dereference_protected(neigh
->hnext
,
896 lockdep_is_held(&ntbl
->rwlock
)));
897 /* remove from path/mc list */
898 spin_lock_irqsave(&priv
->lock
, flags
);
899 list_del(&neigh
->list
);
900 spin_unlock_irqrestore(&priv
->lock
, flags
);
901 call_rcu(&neigh
->rcu
, ipoib_neigh_reclaim
);
910 write_unlock_bh(&ntbl
->rwlock
);
913 static void ipoib_reap_neigh(struct work_struct
*work
)
915 struct ipoib_dev_priv
*priv
=
916 container_of(work
, struct ipoib_dev_priv
, neigh_reap_task
.work
);
918 __ipoib_reap_neigh(priv
);
920 if (!test_bit(IPOIB_STOP_NEIGH_GC
, &priv
->flags
))
921 queue_delayed_work(ipoib_workqueue
, &priv
->neigh_reap_task
,
922 arp_tbl
.gc_interval
);
926 static struct ipoib_neigh
*ipoib_neigh_ctor(u8
*daddr
,
927 struct net_device
*dev
)
929 struct ipoib_neigh
*neigh
;
931 neigh
= kzalloc(sizeof *neigh
, GFP_ATOMIC
);
936 memcpy(&neigh
->daddr
, daddr
, sizeof(neigh
->daddr
));
937 skb_queue_head_init(&neigh
->queue
);
938 INIT_LIST_HEAD(&neigh
->list
);
939 ipoib_cm_set(neigh
, NULL
);
940 /* one ref on behalf of the caller */
941 atomic_set(&neigh
->refcnt
, 1);
946 struct ipoib_neigh
*ipoib_neigh_alloc(u8
*daddr
,
947 struct net_device
*dev
)
949 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
950 struct ipoib_neigh_table
*ntbl
= &priv
->ntbl
;
951 struct ipoib_neigh_hash
*htbl
;
952 struct ipoib_neigh
*neigh
;
955 write_lock_bh(&ntbl
->rwlock
);
957 htbl
= rcu_dereference_protected(ntbl
->htbl
,
958 lockdep_is_held(&ntbl
->rwlock
));
964 /* need to add a new neigh, but maybe some other thread succeeded?
965 * recalc hash, maybe hash resize took place so we do a search
967 hash_val
= ipoib_addr_hash(htbl
, daddr
);
968 for (neigh
= rcu_dereference_protected(htbl
->buckets
[hash_val
],
969 lockdep_is_held(&ntbl
->rwlock
));
971 neigh
= rcu_dereference_protected(neigh
->hnext
,
972 lockdep_is_held(&ntbl
->rwlock
))) {
973 if (memcmp(daddr
, neigh
->daddr
, INFINIBAND_ALEN
) == 0) {
974 /* found, take one ref on behalf of the caller */
975 if (!atomic_inc_not_zero(&neigh
->refcnt
)) {
980 neigh
->alive
= jiffies
;
985 neigh
= ipoib_neigh_ctor(daddr
, dev
);
989 /* one ref on behalf of the hash table */
990 atomic_inc(&neigh
->refcnt
);
991 neigh
->alive
= jiffies
;
993 rcu_assign_pointer(neigh
->hnext
,
994 rcu_dereference_protected(htbl
->buckets
[hash_val
],
995 lockdep_is_held(&ntbl
->rwlock
)));
996 rcu_assign_pointer(htbl
->buckets
[hash_val
], neigh
);
997 atomic_inc(&ntbl
->entries
);
1000 write_unlock_bh(&ntbl
->rwlock
);
1005 void ipoib_neigh_dtor(struct ipoib_neigh
*neigh
)
1007 /* neigh reference count was dropprd to zero */
1008 struct net_device
*dev
= neigh
->dev
;
1009 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1010 struct sk_buff
*skb
;
1012 ipoib_put_ah(neigh
->ah
);
1013 while ((skb
= __skb_dequeue(&neigh
->queue
))) {
1014 ++dev
->stats
.tx_dropped
;
1015 dev_kfree_skb_any(skb
);
1017 if (ipoib_cm_get(neigh
))
1018 ipoib_cm_destroy_tx(ipoib_cm_get(neigh
));
1019 ipoib_dbg(netdev_priv(dev
),
1020 "neigh free for %06x %pI6\n",
1021 IPOIB_QPN(neigh
->daddr
),
1024 if (atomic_dec_and_test(&priv
->ntbl
.entries
)) {
1025 if (test_bit(IPOIB_NEIGH_TBL_FLUSH
, &priv
->flags
))
1026 complete(&priv
->ntbl
.flushed
);
1030 static void ipoib_neigh_reclaim(struct rcu_head
*rp
)
1032 /* Called as a result of removal from hash table */
1033 struct ipoib_neigh
*neigh
= container_of(rp
, struct ipoib_neigh
, rcu
);
1034 /* note TX context may hold another ref */
1035 ipoib_neigh_put(neigh
);
1038 void ipoib_neigh_free(struct ipoib_neigh
*neigh
)
1040 struct net_device
*dev
= neigh
->dev
;
1041 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1042 struct ipoib_neigh_table
*ntbl
= &priv
->ntbl
;
1043 struct ipoib_neigh_hash
*htbl
;
1044 struct ipoib_neigh __rcu
**np
;
1045 struct ipoib_neigh
*n
;
1048 write_lock_bh(&ntbl
->rwlock
);
1050 htbl
= rcu_dereference_protected(ntbl
->htbl
,
1051 lockdep_is_held(&ntbl
->rwlock
));
1055 hash_val
= ipoib_addr_hash(htbl
, neigh
->daddr
);
1056 np
= &htbl
->buckets
[hash_val
];
1057 for (n
= rcu_dereference_protected(*np
,
1058 lockdep_is_held(&ntbl
->rwlock
));
1060 n
= rcu_dereference_protected(*np
,
1061 lockdep_is_held(&ntbl
->rwlock
))) {
1064 rcu_assign_pointer(*np
,
1065 rcu_dereference_protected(neigh
->hnext
,
1066 lockdep_is_held(&ntbl
->rwlock
)));
1067 call_rcu(&neigh
->rcu
, ipoib_neigh_reclaim
);
1075 write_unlock_bh(&ntbl
->rwlock
);
1079 static int ipoib_neigh_hash_init(struct ipoib_dev_priv
*priv
)
1081 struct ipoib_neigh_table
*ntbl
= &priv
->ntbl
;
1082 struct ipoib_neigh_hash
*htbl
;
1083 struct ipoib_neigh
**buckets
;
1086 clear_bit(IPOIB_NEIGH_TBL_FLUSH
, &priv
->flags
);
1088 rwlock_init(&ntbl
->rwlock
);
1089 htbl
= kzalloc(sizeof(*htbl
), GFP_KERNEL
);
1092 set_bit(IPOIB_STOP_NEIGH_GC
, &priv
->flags
);
1093 size
= roundup_pow_of_two(arp_tbl
.gc_thresh3
);
1094 buckets
= kzalloc(size
* sizeof(*buckets
), GFP_KERNEL
);
1100 htbl
->mask
= (size
- 1);
1101 htbl
->buckets
= buckets
;
1103 atomic_set(&ntbl
->entries
, 0);
1105 /* start garbage collection */
1106 clear_bit(IPOIB_STOP_NEIGH_GC
, &priv
->flags
);
1107 queue_delayed_work(ipoib_workqueue
, &priv
->neigh_reap_task
,
1108 arp_tbl
.gc_interval
);
1113 static void neigh_hash_free_rcu(struct rcu_head
*head
)
1115 struct ipoib_neigh_hash
*htbl
= container_of(head
,
1116 struct ipoib_neigh_hash
,
1118 struct ipoib_neigh __rcu
**buckets
= htbl
->buckets
;
1124 void ipoib_del_neighs_by_gid(struct net_device
*dev
, u8
*gid
)
1126 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1127 struct ipoib_neigh_table
*ntbl
= &priv
->ntbl
;
1128 struct ipoib_neigh_hash
*htbl
;
1129 unsigned long flags
;
1132 /* remove all neigh connected to a given path or mcast */
1133 write_lock_bh(&ntbl
->rwlock
);
1135 htbl
= rcu_dereference_protected(ntbl
->htbl
,
1136 lockdep_is_held(&ntbl
->rwlock
));
1141 for (i
= 0; i
< htbl
->size
; i
++) {
1142 struct ipoib_neigh
*neigh
;
1143 struct ipoib_neigh __rcu
**np
= &htbl
->buckets
[i
];
1145 while ((neigh
= rcu_dereference_protected(*np
,
1146 lockdep_is_held(&ntbl
->rwlock
))) != NULL
) {
1147 /* delete neighs belong to this parent */
1148 if (!memcmp(gid
, neigh
->daddr
+ 4, sizeof (union ib_gid
))) {
1149 rcu_assign_pointer(*np
,
1150 rcu_dereference_protected(neigh
->hnext
,
1151 lockdep_is_held(&ntbl
->rwlock
)));
1152 /* remove from parent list */
1153 spin_lock_irqsave(&priv
->lock
, flags
);
1154 list_del(&neigh
->list
);
1155 spin_unlock_irqrestore(&priv
->lock
, flags
);
1156 call_rcu(&neigh
->rcu
, ipoib_neigh_reclaim
);
1164 write_unlock_bh(&ntbl
->rwlock
);
1167 static void ipoib_flush_neighs(struct ipoib_dev_priv
*priv
)
1169 struct ipoib_neigh_table
*ntbl
= &priv
->ntbl
;
1170 struct ipoib_neigh_hash
*htbl
;
1171 unsigned long flags
;
1174 write_lock_bh(&ntbl
->rwlock
);
1176 htbl
= rcu_dereference_protected(ntbl
->htbl
,
1177 lockdep_is_held(&ntbl
->rwlock
));
1181 for (i
= 0; i
< htbl
->size
; i
++) {
1182 struct ipoib_neigh
*neigh
;
1183 struct ipoib_neigh __rcu
**np
= &htbl
->buckets
[i
];
1185 while ((neigh
= rcu_dereference_protected(*np
,
1186 lockdep_is_held(&ntbl
->rwlock
))) != NULL
) {
1187 rcu_assign_pointer(*np
,
1188 rcu_dereference_protected(neigh
->hnext
,
1189 lockdep_is_held(&ntbl
->rwlock
)));
1190 /* remove from path/mc list */
1191 spin_lock_irqsave(&priv
->lock
, flags
);
1192 list_del(&neigh
->list
);
1193 spin_unlock_irqrestore(&priv
->lock
, flags
);
1194 call_rcu(&neigh
->rcu
, ipoib_neigh_reclaim
);
1198 rcu_assign_pointer(ntbl
->htbl
, NULL
);
1199 call_rcu(&htbl
->rcu
, neigh_hash_free_rcu
);
1202 write_unlock_bh(&ntbl
->rwlock
);
1205 static void ipoib_neigh_hash_uninit(struct net_device
*dev
)
1207 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1210 ipoib_dbg(priv
, "ipoib_neigh_hash_uninit\n");
1211 init_completion(&priv
->ntbl
.flushed
);
1212 set_bit(IPOIB_NEIGH_TBL_FLUSH
, &priv
->flags
);
1214 /* Stop GC if called at init fail need to cancel work */
1215 stopped
= test_and_set_bit(IPOIB_STOP_NEIGH_GC
, &priv
->flags
);
1217 cancel_delayed_work(&priv
->neigh_reap_task
);
1219 if (atomic_read(&priv
->ntbl
.entries
)) {
1220 ipoib_flush_neighs(priv
);
1221 wait_for_completion(&priv
->ntbl
.flushed
);
1226 int ipoib_dev_init(struct net_device
*dev
, struct ib_device
*ca
, int port
)
1228 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1230 if (ipoib_neigh_hash_init(priv
) < 0)
1232 /* Allocate RX/TX "rings" to hold queued skbs */
1233 priv
->rx_ring
= kzalloc(ipoib_recvq_size
* sizeof *priv
->rx_ring
,
1235 if (!priv
->rx_ring
) {
1236 printk(KERN_WARNING
"%s: failed to allocate RX ring (%d entries)\n",
1237 ca
->name
, ipoib_recvq_size
);
1238 goto out_neigh_hash_cleanup
;
1241 priv
->tx_ring
= vzalloc(ipoib_sendq_size
* sizeof *priv
->tx_ring
);
1242 if (!priv
->tx_ring
) {
1243 printk(KERN_WARNING
"%s: failed to allocate TX ring (%d entries)\n",
1244 ca
->name
, ipoib_sendq_size
);
1245 goto out_rx_ring_cleanup
;
1248 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
1250 if (ipoib_ib_dev_init(dev
, ca
, port
))
1251 goto out_tx_ring_cleanup
;
1255 out_tx_ring_cleanup
:
1256 vfree(priv
->tx_ring
);
1258 out_rx_ring_cleanup
:
1259 kfree(priv
->rx_ring
);
1261 out_neigh_hash_cleanup
:
1262 ipoib_neigh_hash_uninit(dev
);
1267 void ipoib_dev_cleanup(struct net_device
*dev
)
1269 struct ipoib_dev_priv
*priv
= netdev_priv(dev
), *cpriv
, *tcpriv
;
1274 ipoib_delete_debug_files(dev
);
1276 /* Delete any child interfaces first */
1277 list_for_each_entry_safe(cpriv
, tcpriv
, &priv
->child_intfs
, list
) {
1278 /* Stop GC on child */
1279 set_bit(IPOIB_STOP_NEIGH_GC
, &cpriv
->flags
);
1280 cancel_delayed_work(&cpriv
->neigh_reap_task
);
1281 unregister_netdevice_queue(cpriv
->dev
, &head
);
1283 unregister_netdevice_many(&head
);
1285 ipoib_ib_dev_cleanup(dev
);
1287 kfree(priv
->rx_ring
);
1288 vfree(priv
->tx_ring
);
1290 priv
->rx_ring
= NULL
;
1291 priv
->tx_ring
= NULL
;
1293 ipoib_neigh_hash_uninit(dev
);
1296 static const struct header_ops ipoib_header_ops
= {
1297 .create
= ipoib_hard_header
,
1300 static const struct net_device_ops ipoib_netdev_ops
= {
1301 .ndo_uninit
= ipoib_uninit
,
1302 .ndo_open
= ipoib_open
,
1303 .ndo_stop
= ipoib_stop
,
1304 .ndo_change_mtu
= ipoib_change_mtu
,
1305 .ndo_fix_features
= ipoib_fix_features
,
1306 .ndo_start_xmit
= ipoib_start_xmit
,
1307 .ndo_tx_timeout
= ipoib_timeout
,
1308 .ndo_set_rx_mode
= ipoib_set_mcast_list
,
1311 void ipoib_setup(struct net_device
*dev
)
1313 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
1315 dev
->netdev_ops
= &ipoib_netdev_ops
;
1316 dev
->header_ops
= &ipoib_header_ops
;
1318 ipoib_set_ethtool_ops(dev
);
1320 netif_napi_add(dev
, &priv
->napi
, ipoib_poll
, 100);
1322 dev
->watchdog_timeo
= HZ
;
1324 dev
->flags
|= IFF_BROADCAST
| IFF_MULTICAST
;
1326 dev
->hard_header_len
= IPOIB_ENCAP_LEN
;
1327 dev
->addr_len
= INFINIBAND_ALEN
;
1328 dev
->type
= ARPHRD_INFINIBAND
;
1329 dev
->tx_queue_len
= ipoib_sendq_size
* 2;
1330 dev
->features
= (NETIF_F_VLAN_CHALLENGED
|
1332 dev
->priv_flags
&= ~IFF_XMIT_DST_RELEASE
;
1334 memcpy(dev
->broadcast
, ipv4_bcast_addr
, INFINIBAND_ALEN
);
1336 netif_carrier_off(dev
);
1340 spin_lock_init(&priv
->lock
);
1342 mutex_init(&priv
->vlan_mutex
);
1344 INIT_LIST_HEAD(&priv
->path_list
);
1345 INIT_LIST_HEAD(&priv
->child_intfs
);
1346 INIT_LIST_HEAD(&priv
->dead_ahs
);
1347 INIT_LIST_HEAD(&priv
->multicast_list
);
1349 INIT_DELAYED_WORK(&priv
->pkey_poll_task
, ipoib_pkey_poll
);
1350 INIT_DELAYED_WORK(&priv
->mcast_task
, ipoib_mcast_join_task
);
1351 INIT_WORK(&priv
->carrier_on_task
, ipoib_mcast_carrier_on_task
);
1352 INIT_WORK(&priv
->flush_light
, ipoib_ib_dev_flush_light
);
1353 INIT_WORK(&priv
->flush_normal
, ipoib_ib_dev_flush_normal
);
1354 INIT_WORK(&priv
->flush_heavy
, ipoib_ib_dev_flush_heavy
);
1355 INIT_WORK(&priv
->restart_task
, ipoib_mcast_restart_task
);
1356 INIT_DELAYED_WORK(&priv
->ah_reap_task
, ipoib_reap_ah
);
1357 INIT_DELAYED_WORK(&priv
->neigh_reap_task
, ipoib_reap_neigh
);
1360 struct ipoib_dev_priv
*ipoib_intf_alloc(const char *name
)
1362 struct net_device
*dev
;
1364 dev
= alloc_netdev((int) sizeof (struct ipoib_dev_priv
), name
,
1369 return netdev_priv(dev
);
1372 static ssize_t
show_pkey(struct device
*dev
,
1373 struct device_attribute
*attr
, char *buf
)
1375 struct ipoib_dev_priv
*priv
= netdev_priv(to_net_dev(dev
));
1377 return sprintf(buf
, "0x%04x\n", priv
->pkey
);
1379 static DEVICE_ATTR(pkey
, S_IRUGO
, show_pkey
, NULL
);
1381 static ssize_t
show_umcast(struct device
*dev
,
1382 struct device_attribute
*attr
, char *buf
)
1384 struct ipoib_dev_priv
*priv
= netdev_priv(to_net_dev(dev
));
1386 return sprintf(buf
, "%d\n", test_bit(IPOIB_FLAG_UMCAST
, &priv
->flags
));
1389 static ssize_t
set_umcast(struct device
*dev
,
1390 struct device_attribute
*attr
,
1391 const char *buf
, size_t count
)
1393 struct ipoib_dev_priv
*priv
= netdev_priv(to_net_dev(dev
));
1394 unsigned long umcast_val
= simple_strtoul(buf
, NULL
, 0);
1396 if (umcast_val
> 0) {
1397 set_bit(IPOIB_FLAG_UMCAST
, &priv
->flags
);
1398 ipoib_warn(priv
, "ignoring multicast groups joined directly "
1401 clear_bit(IPOIB_FLAG_UMCAST
, &priv
->flags
);
1405 static DEVICE_ATTR(umcast
, S_IWUSR
| S_IRUGO
, show_umcast
, set_umcast
);
1407 int ipoib_add_umcast_attr(struct net_device
*dev
)
1409 return device_create_file(&dev
->dev
, &dev_attr_umcast
);
1412 static ssize_t
create_child(struct device
*dev
,
1413 struct device_attribute
*attr
,
1414 const char *buf
, size_t count
)
1419 if (sscanf(buf
, "%i", &pkey
) != 1)
1422 if (pkey
< 0 || pkey
> 0xffff)
1426 * Set the full membership bit, so that we join the right
1427 * broadcast group, etc.
1431 ret
= ipoib_vlan_add(to_net_dev(dev
), pkey
);
1433 return ret
? ret
: count
;
1435 static DEVICE_ATTR(create_child
, S_IWUSR
, NULL
, create_child
);
1437 static ssize_t
delete_child(struct device
*dev
,
1438 struct device_attribute
*attr
,
1439 const char *buf
, size_t count
)
1444 if (sscanf(buf
, "%i", &pkey
) != 1)
1447 if (pkey
< 0 || pkey
> 0xffff)
1450 ret
= ipoib_vlan_delete(to_net_dev(dev
), pkey
);
1452 return ret
? ret
: count
;
1455 static DEVICE_ATTR(delete_child
, S_IWUSR
, NULL
, delete_child
);
1457 int ipoib_add_pkey_attr(struct net_device
*dev
)
1459 return device_create_file(&dev
->dev
, &dev_attr_pkey
);
1462 int ipoib_set_dev_features(struct ipoib_dev_priv
*priv
, struct ib_device
*hca
)
1464 struct ib_device_attr
*device_attr
;
1465 int result
= -ENOMEM
;
1467 device_attr
= kmalloc(sizeof *device_attr
, GFP_KERNEL
);
1469 printk(KERN_WARNING
"%s: allocation of %zu bytes failed\n",
1470 hca
->name
, sizeof *device_attr
);
1474 result
= ib_query_device(hca
, device_attr
);
1476 printk(KERN_WARNING
"%s: ib_query_device failed (ret = %d)\n",
1481 priv
->hca_caps
= device_attr
->device_cap_flags
;
1485 if (priv
->hca_caps
& IB_DEVICE_UD_IP_CSUM
) {
1486 priv
->dev
->hw_features
= NETIF_F_SG
|
1487 NETIF_F_IP_CSUM
| NETIF_F_RXCSUM
;
1489 if (priv
->hca_caps
& IB_DEVICE_UD_TSO
)
1490 priv
->dev
->hw_features
|= NETIF_F_TSO
;
1492 priv
->dev
->features
|= priv
->dev
->hw_features
;
1498 static struct net_device
*ipoib_add_port(const char *format
,
1499 struct ib_device
*hca
, u8 port
)
1501 struct ipoib_dev_priv
*priv
;
1502 struct ib_port_attr attr
;
1503 int result
= -ENOMEM
;
1505 priv
= ipoib_intf_alloc(format
);
1507 goto alloc_mem_failed
;
1509 SET_NETDEV_DEV(priv
->dev
, hca
->dma_device
);
1510 priv
->dev
->dev_id
= port
- 1;
1512 if (!ib_query_port(hca
, port
, &attr
))
1513 priv
->max_ib_mtu
= ib_mtu_enum_to_int(attr
.max_mtu
);
1515 printk(KERN_WARNING
"%s: ib_query_port %d failed\n",
1517 goto device_init_failed
;
1520 /* MTU will be reset when mcast join happens */
1521 priv
->dev
->mtu
= IPOIB_UD_MTU(priv
->max_ib_mtu
);
1522 priv
->mcast_mtu
= priv
->admin_mtu
= priv
->dev
->mtu
;
1524 priv
->dev
->neigh_priv_len
= sizeof(struct ipoib_neigh
);
1526 result
= ib_query_pkey(hca
, port
, 0, &priv
->pkey
);
1528 printk(KERN_WARNING
"%s: ib_query_pkey port %d failed (ret = %d)\n",
1529 hca
->name
, port
, result
);
1530 goto device_init_failed
;
1533 if (ipoib_set_dev_features(priv
, hca
))
1534 goto device_init_failed
;
1537 * Set the full membership bit, so that we join the right
1538 * broadcast group, etc.
1540 priv
->pkey
|= 0x8000;
1542 priv
->dev
->broadcast
[8] = priv
->pkey
>> 8;
1543 priv
->dev
->broadcast
[9] = priv
->pkey
& 0xff;
1545 result
= ib_query_gid(hca
, port
, 0, &priv
->local_gid
);
1547 printk(KERN_WARNING
"%s: ib_query_gid port %d failed (ret = %d)\n",
1548 hca
->name
, port
, result
);
1549 goto device_init_failed
;
1551 memcpy(priv
->dev
->dev_addr
+ 4, priv
->local_gid
.raw
, sizeof (union ib_gid
));
1553 result
= ipoib_dev_init(priv
->dev
, hca
, port
);
1555 printk(KERN_WARNING
"%s: failed to initialize port %d (ret = %d)\n",
1556 hca
->name
, port
, result
);
1557 goto device_init_failed
;
1560 INIT_IB_EVENT_HANDLER(&priv
->event_handler
,
1561 priv
->ca
, ipoib_event
);
1562 result
= ib_register_event_handler(&priv
->event_handler
);
1564 printk(KERN_WARNING
"%s: ib_register_event_handler failed for "
1565 "port %d (ret = %d)\n",
1566 hca
->name
, port
, result
);
1570 result
= register_netdev(priv
->dev
);
1572 printk(KERN_WARNING
"%s: couldn't register ipoib port %d; error %d\n",
1573 hca
->name
, port
, result
);
1574 goto register_failed
;
1577 ipoib_create_debug_files(priv
->dev
);
1579 if (ipoib_cm_add_mode_attr(priv
->dev
))
1581 if (ipoib_add_pkey_attr(priv
->dev
))
1583 if (ipoib_add_umcast_attr(priv
->dev
))
1585 if (device_create_file(&priv
->dev
->dev
, &dev_attr_create_child
))
1587 if (device_create_file(&priv
->dev
->dev
, &dev_attr_delete_child
))
1593 ipoib_delete_debug_files(priv
->dev
);
1594 unregister_netdev(priv
->dev
);
1597 ib_unregister_event_handler(&priv
->event_handler
);
1598 /* Stop GC if started before flush */
1599 set_bit(IPOIB_STOP_NEIGH_GC
, &priv
->flags
);
1600 cancel_delayed_work(&priv
->neigh_reap_task
);
1601 flush_workqueue(ipoib_workqueue
);
1604 ipoib_dev_cleanup(priv
->dev
);
1607 free_netdev(priv
->dev
);
1610 return ERR_PTR(result
);
1613 static void ipoib_add_one(struct ib_device
*device
)
1615 struct list_head
*dev_list
;
1616 struct net_device
*dev
;
1617 struct ipoib_dev_priv
*priv
;
1620 if (rdma_node_get_transport(device
->node_type
) != RDMA_TRANSPORT_IB
)
1623 dev_list
= kmalloc(sizeof *dev_list
, GFP_KERNEL
);
1627 INIT_LIST_HEAD(dev_list
);
1629 if (device
->node_type
== RDMA_NODE_IB_SWITCH
) {
1634 e
= device
->phys_port_cnt
;
1637 for (p
= s
; p
<= e
; ++p
) {
1638 if (rdma_port_get_link_layer(device
, p
) != IB_LINK_LAYER_INFINIBAND
)
1640 dev
= ipoib_add_port("ib%d", device
, p
);
1642 priv
= netdev_priv(dev
);
1643 list_add_tail(&priv
->list
, dev_list
);
1647 ib_set_client_data(device
, &ipoib_client
, dev_list
);
1650 static void ipoib_remove_one(struct ib_device
*device
)
1652 struct ipoib_dev_priv
*priv
, *tmp
;
1653 struct list_head
*dev_list
;
1655 if (rdma_node_get_transport(device
->node_type
) != RDMA_TRANSPORT_IB
)
1658 dev_list
= ib_get_client_data(device
, &ipoib_client
);
1660 list_for_each_entry_safe(priv
, tmp
, dev_list
, list
) {
1661 ib_unregister_event_handler(&priv
->event_handler
);
1664 dev_change_flags(priv
->dev
, priv
->dev
->flags
& ~IFF_UP
);
1668 set_bit(IPOIB_STOP_NEIGH_GC
, &priv
->flags
);
1669 cancel_delayed_work(&priv
->neigh_reap_task
);
1670 flush_workqueue(ipoib_workqueue
);
1672 unregister_netdev(priv
->dev
);
1673 free_netdev(priv
->dev
);
1679 static int __init
ipoib_init_module(void)
1683 ipoib_recvq_size
= roundup_pow_of_two(ipoib_recvq_size
);
1684 ipoib_recvq_size
= min(ipoib_recvq_size
, IPOIB_MAX_QUEUE_SIZE
);
1685 ipoib_recvq_size
= max(ipoib_recvq_size
, IPOIB_MIN_QUEUE_SIZE
);
1687 ipoib_sendq_size
= roundup_pow_of_two(ipoib_sendq_size
);
1688 ipoib_sendq_size
= min(ipoib_sendq_size
, IPOIB_MAX_QUEUE_SIZE
);
1689 ipoib_sendq_size
= max3(ipoib_sendq_size
, 2 * MAX_SEND_CQE
, IPOIB_MIN_QUEUE_SIZE
);
1690 #ifdef CONFIG_INFINIBAND_IPOIB_CM
1691 ipoib_max_conn_qp
= min(ipoib_max_conn_qp
, IPOIB_CM_MAX_CONN_QP
);
1695 * When copying small received packets, we only copy from the
1696 * linear data part of the SKB, so we rely on this condition.
1698 BUILD_BUG_ON(IPOIB_CM_COPYBREAK
> IPOIB_CM_HEAD_SIZE
);
1700 ret
= ipoib_register_debugfs();
1705 * We create our own workqueue mainly because we want to be
1706 * able to flush it when devices are being removed. We can't
1707 * use schedule_work()/flush_scheduled_work() because both
1708 * unregister_netdev() and linkwatch_event take the rtnl lock,
1709 * so flush_scheduled_work() can deadlock during device
1712 ipoib_workqueue
= create_singlethread_workqueue("ipoib");
1713 if (!ipoib_workqueue
) {
1718 ib_sa_register_client(&ipoib_sa_client
);
1720 ret
= ib_register_client(&ipoib_client
);
1724 ret
= ipoib_netlink_init();
1731 ib_unregister_client(&ipoib_client
);
1734 ib_sa_unregister_client(&ipoib_sa_client
);
1735 destroy_workqueue(ipoib_workqueue
);
1738 ipoib_unregister_debugfs();
1743 static void __exit
ipoib_cleanup_module(void)
1745 ipoib_netlink_fini();
1746 ib_unregister_client(&ipoib_client
);
1747 ib_sa_unregister_client(&ipoib_sa_client
);
1748 ipoib_unregister_debugfs();
1749 destroy_workqueue(ipoib_workqueue
);
1752 module_init(ipoib_init_module
);
1753 module_exit(ipoib_cleanup_module
);