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
34 * $Id: ipoib_main.c 1377 2004-12-23 19:57:12Z roland $
39 #include <linux/module.h>
41 #include <linux/init.h>
42 #include <linux/slab.h>
43 #include <linux/vmalloc.h>
45 #include <linux/if_arp.h> /* For ARPHRD_xxx */
50 MODULE_AUTHOR("Roland Dreier");
51 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
52 MODULE_LICENSE("Dual BSD/GPL");
54 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
55 int ipoib_debug_level
;
57 module_param_named(debug_level
, ipoib_debug_level
, int, 0644);
58 MODULE_PARM_DESC(debug_level
, "Enable debug tracing if > 0");
61 struct ipoib_path_iter
{
62 struct net_device
*dev
;
63 struct ipoib_path path
;
66 static const u8 ipv4_bcast_addr
[] = {
67 0x00, 0xff, 0xff, 0xff,
68 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
69 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
72 struct workqueue_struct
*ipoib_workqueue
;
74 static void ipoib_add_one(struct ib_device
*device
);
75 static void ipoib_remove_one(struct ib_device
*device
);
77 static struct ib_client ipoib_client
= {
80 .remove
= ipoib_remove_one
83 int ipoib_open(struct net_device
*dev
)
85 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
87 ipoib_dbg(priv
, "bringing up interface\n");
89 set_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
);
91 if (ipoib_pkey_dev_delay_open(dev
))
94 if (ipoib_ib_dev_open(dev
))
97 if (ipoib_ib_dev_up(dev
))
100 if (!test_bit(IPOIB_FLAG_SUBINTERFACE
, &priv
->flags
)) {
101 struct ipoib_dev_priv
*cpriv
;
103 /* Bring up any child interfaces too */
104 down(&priv
->vlan_mutex
);
105 list_for_each_entry(cpriv
, &priv
->child_intfs
, list
) {
108 flags
= cpriv
->dev
->flags
;
112 dev_change_flags(cpriv
->dev
, flags
| IFF_UP
);
114 up(&priv
->vlan_mutex
);
117 netif_start_queue(dev
);
122 static int ipoib_stop(struct net_device
*dev
)
124 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
126 ipoib_dbg(priv
, "stopping interface\n");
128 clear_bit(IPOIB_FLAG_ADMIN_UP
, &priv
->flags
);
130 netif_stop_queue(dev
);
132 ipoib_ib_dev_down(dev
);
133 ipoib_ib_dev_stop(dev
);
135 if (!test_bit(IPOIB_FLAG_SUBINTERFACE
, &priv
->flags
)) {
136 struct ipoib_dev_priv
*cpriv
;
138 /* Bring down any child interfaces too */
139 down(&priv
->vlan_mutex
);
140 list_for_each_entry(cpriv
, &priv
->child_intfs
, list
) {
143 flags
= cpriv
->dev
->flags
;
144 if (!(flags
& IFF_UP
))
147 dev_change_flags(cpriv
->dev
, flags
& ~IFF_UP
);
149 up(&priv
->vlan_mutex
);
155 static int ipoib_change_mtu(struct net_device
*dev
, int new_mtu
)
157 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
159 if (new_mtu
> IPOIB_PACKET_SIZE
- IPOIB_ENCAP_LEN
)
162 priv
->admin_mtu
= new_mtu
;
164 dev
->mtu
= min(priv
->mcast_mtu
, priv
->admin_mtu
);
169 static struct ipoib_path
*__path_find(struct net_device
*dev
,
172 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
173 struct rb_node
*n
= priv
->path_tree
.rb_node
;
174 struct ipoib_path
*path
;
178 path
= rb_entry(n
, struct ipoib_path
, rb_node
);
180 ret
= memcmp(gid
->raw
, path
->pathrec
.dgid
.raw
,
181 sizeof (union ib_gid
));
194 static int __path_add(struct net_device
*dev
, struct ipoib_path
*path
)
196 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
197 struct rb_node
**n
= &priv
->path_tree
.rb_node
;
198 struct rb_node
*pn
= NULL
;
199 struct ipoib_path
*tpath
;
204 tpath
= rb_entry(pn
, struct ipoib_path
, rb_node
);
206 ret
= memcmp(path
->pathrec
.dgid
.raw
, tpath
->pathrec
.dgid
.raw
,
207 sizeof (union ib_gid
));
216 rb_link_node(&path
->rb_node
, pn
, n
);
217 rb_insert_color(&path
->rb_node
, &priv
->path_tree
);
219 list_add_tail(&path
->list
, &priv
->path_list
);
224 static void path_free(struct net_device
*dev
, struct ipoib_path
*path
)
226 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
227 struct ipoib_neigh
*neigh
, *tn
;
231 while ((skb
= __skb_dequeue(&path
->queue
)))
232 dev_kfree_skb_irq(skb
);
234 spin_lock_irqsave(&priv
->lock
, flags
);
236 list_for_each_entry_safe(neigh
, tn
, &path
->neigh_list
, list
) {
238 * It's safe to call ipoib_put_ah() inside priv->lock
239 * here, because we know that path->ah will always
240 * hold one more reference, so ipoib_put_ah() will
241 * never do more than decrement the ref count.
244 ipoib_put_ah(neigh
->ah
);
245 *to_ipoib_neigh(neigh
->neighbour
) = NULL
;
246 neigh
->neighbour
->ops
->destructor
= NULL
;
250 spin_unlock_irqrestore(&priv
->lock
, flags
);
253 ipoib_put_ah(path
->ah
);
258 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
260 struct ipoib_path_iter
*ipoib_path_iter_init(struct net_device
*dev
)
262 struct ipoib_path_iter
*iter
;
264 iter
= kmalloc(sizeof *iter
, GFP_KERNEL
);
269 memset(iter
->path
.pathrec
.dgid
.raw
, 0, 16);
271 if (ipoib_path_iter_next(iter
)) {
279 int ipoib_path_iter_next(struct ipoib_path_iter
*iter
)
281 struct ipoib_dev_priv
*priv
= netdev_priv(iter
->dev
);
283 struct ipoib_path
*path
;
286 spin_lock_irq(&priv
->lock
);
288 n
= rb_first(&priv
->path_tree
);
291 path
= rb_entry(n
, struct ipoib_path
, rb_node
);
293 if (memcmp(iter
->path
.pathrec
.dgid
.raw
, path
->pathrec
.dgid
.raw
,
294 sizeof (union ib_gid
)) < 0) {
303 spin_unlock_irq(&priv
->lock
);
308 void ipoib_path_iter_read(struct ipoib_path_iter
*iter
,
309 struct ipoib_path
*path
)
314 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
316 void ipoib_flush_paths(struct net_device
*dev
)
318 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
319 struct ipoib_path
*path
, *tp
;
320 LIST_HEAD(remove_list
);
323 spin_lock_irqsave(&priv
->lock
, flags
);
325 list_splice(&priv
->path_list
, &remove_list
);
326 INIT_LIST_HEAD(&priv
->path_list
);
328 list_for_each_entry(path
, &remove_list
, list
)
329 rb_erase(&path
->rb_node
, &priv
->path_tree
);
331 spin_unlock_irqrestore(&priv
->lock
, flags
);
333 list_for_each_entry_safe(path
, tp
, &remove_list
, list
) {
335 ib_sa_cancel_query(path
->query_id
, path
->query
);
336 wait_for_completion(&path
->done
);
337 path_free(dev
, path
);
341 static void path_rec_completion(int status
,
342 struct ib_sa_path_rec
*pathrec
,
345 struct ipoib_path
*path
= path_ptr
;
346 struct net_device
*dev
= path
->dev
;
347 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
348 struct ipoib_ah
*ah
= NULL
;
349 struct ipoib_neigh
*neigh
;
350 struct sk_buff_head skqueue
;
355 ipoib_dbg(priv
, "PathRec LID 0x%04x for GID " IPOIB_GID_FMT
"\n",
356 be16_to_cpu(pathrec
->dlid
), IPOIB_GID_ARG(pathrec
->dgid
));
358 ipoib_dbg(priv
, "PathRec status %d for GID " IPOIB_GID_FMT
"\n",
359 status
, IPOIB_GID_ARG(path
->pathrec
.dgid
));
361 skb_queue_head_init(&skqueue
);
364 struct ib_ah_attr av
= {
365 .dlid
= be16_to_cpu(pathrec
->dlid
),
367 .port_num
= priv
->port
369 int path_rate
= ib_sa_rate_enum_to_int(pathrec
->rate
);
371 if (path_rate
> 0 && priv
->local_rate
> path_rate
)
372 av
.static_rate
= (priv
->local_rate
- 1) / path_rate
;
374 ipoib_dbg(priv
, "static_rate %d for local port %dX, path %dX\n",
375 av
.static_rate
, priv
->local_rate
,
376 ib_sa_rate_enum_to_int(pathrec
->rate
));
378 ah
= ipoib_create_ah(dev
, priv
->pd
, &av
);
381 spin_lock_irqsave(&priv
->lock
, flags
);
386 path
->pathrec
= *pathrec
;
388 ipoib_dbg(priv
, "created address handle %p for LID 0x%04x, SL %d\n",
389 ah
, be16_to_cpu(pathrec
->dlid
), pathrec
->sl
);
391 while ((skb
= __skb_dequeue(&path
->queue
)))
392 __skb_queue_tail(&skqueue
, skb
);
394 list_for_each_entry(neigh
, &path
->neigh_list
, list
) {
395 kref_get(&path
->ah
->ref
);
396 neigh
->ah
= path
->ah
;
398 while ((skb
= __skb_dequeue(&neigh
->queue
)))
399 __skb_queue_tail(&skqueue
, skb
);
404 complete(&path
->done
);
406 spin_unlock_irqrestore(&priv
->lock
, flags
);
408 while ((skb
= __skb_dequeue(&skqueue
))) {
410 if (dev_queue_xmit(skb
))
411 ipoib_warn(priv
, "dev_queue_xmit failed "
412 "to requeue packet\n");
416 static struct ipoib_path
*path_rec_create(struct net_device
*dev
,
419 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
420 struct ipoib_path
*path
;
422 path
= kzalloc(sizeof *path
, GFP_ATOMIC
);
428 skb_queue_head_init(&path
->queue
);
430 INIT_LIST_HEAD(&path
->neigh_list
);
432 memcpy(path
->pathrec
.dgid
.raw
, gid
->raw
, sizeof (union ib_gid
));
433 path
->pathrec
.sgid
= priv
->local_gid
;
434 path
->pathrec
.pkey
= cpu_to_be16(priv
->pkey
);
435 path
->pathrec
.numb_path
= 1;
440 static int path_rec_start(struct net_device
*dev
,
441 struct ipoib_path
*path
)
443 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
445 ipoib_dbg(priv
, "Start path record lookup for " IPOIB_GID_FMT
"\n",
446 IPOIB_GID_ARG(path
->pathrec
.dgid
));
448 init_completion(&path
->done
);
451 ib_sa_path_rec_get(priv
->ca
, priv
->port
,
453 IB_SA_PATH_REC_DGID
|
454 IB_SA_PATH_REC_SGID
|
455 IB_SA_PATH_REC_NUMB_PATH
|
460 if (path
->query_id
< 0) {
461 ipoib_warn(priv
, "ib_sa_path_rec_get failed\n");
463 return path
->query_id
;
469 static void neigh_add_path(struct sk_buff
*skb
, struct net_device
*dev
)
471 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
472 struct ipoib_path
*path
;
473 struct ipoib_neigh
*neigh
;
475 neigh
= kmalloc(sizeof *neigh
, GFP_ATOMIC
);
477 ++priv
->stats
.tx_dropped
;
478 dev_kfree_skb_any(skb
);
482 skb_queue_head_init(&neigh
->queue
);
483 neigh
->neighbour
= skb
->dst
->neighbour
;
484 *to_ipoib_neigh(skb
->dst
->neighbour
) = neigh
;
487 * We can only be called from ipoib_start_xmit, so we're
488 * inside tx_lock -- no need to save/restore flags.
490 spin_lock(&priv
->lock
);
492 path
= __path_find(dev
, (union ib_gid
*) (skb
->dst
->neighbour
->ha
+ 4));
494 path
= path_rec_create(dev
,
495 (union ib_gid
*) (skb
->dst
->neighbour
->ha
+ 4));
499 __path_add(dev
, path
);
502 list_add_tail(&neigh
->list
, &path
->neigh_list
);
504 if (path
->pathrec
.dlid
) {
505 kref_get(&path
->ah
->ref
);
506 neigh
->ah
= path
->ah
;
508 ipoib_send(dev
, skb
, path
->ah
,
509 be32_to_cpup((__be32
*) skb
->dst
->neighbour
->ha
));
512 if (skb_queue_len(&neigh
->queue
) < IPOIB_MAX_PATH_REC_QUEUE
) {
513 __skb_queue_tail(&neigh
->queue
, skb
);
515 ++priv
->stats
.tx_dropped
;
516 dev_kfree_skb_any(skb
);
519 if (!path
->query
&& path_rec_start(dev
, path
))
523 spin_unlock(&priv
->lock
);
527 *to_ipoib_neigh(skb
->dst
->neighbour
) = NULL
;
528 list_del(&neigh
->list
);
529 neigh
->neighbour
->ops
->destructor
= NULL
;
532 ++priv
->stats
.tx_dropped
;
533 dev_kfree_skb_any(skb
);
535 spin_unlock(&priv
->lock
);
538 static void ipoib_path_lookup(struct sk_buff
*skb
, struct net_device
*dev
)
540 struct ipoib_dev_priv
*priv
= netdev_priv(skb
->dev
);
542 /* Look up path record for unicasts */
543 if (skb
->dst
->neighbour
->ha
[4] != 0xff) {
544 neigh_add_path(skb
, dev
);
548 /* Add in the P_Key for multicasts */
549 skb
->dst
->neighbour
->ha
[8] = (priv
->pkey
>> 8) & 0xff;
550 skb
->dst
->neighbour
->ha
[9] = priv
->pkey
& 0xff;
551 ipoib_mcast_send(dev
, (union ib_gid
*) (skb
->dst
->neighbour
->ha
+ 4), skb
);
554 static void unicast_arp_send(struct sk_buff
*skb
, struct net_device
*dev
,
555 struct ipoib_pseudoheader
*phdr
)
557 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
558 struct ipoib_path
*path
;
561 * We can only be called from ipoib_start_xmit, so we're
562 * inside tx_lock -- no need to save/restore flags.
564 spin_lock(&priv
->lock
);
566 path
= __path_find(dev
, (union ib_gid
*) (phdr
->hwaddr
+ 4));
568 path
= path_rec_create(dev
,
569 (union ib_gid
*) (phdr
->hwaddr
+ 4));
571 /* put pseudoheader back on for next time */
572 skb_push(skb
, sizeof *phdr
);
573 __skb_queue_tail(&path
->queue
, skb
);
575 if (path_rec_start(dev
, path
)) {
576 spin_unlock(&priv
->lock
);
577 path_free(dev
, path
);
580 __path_add(dev
, path
);
582 ++priv
->stats
.tx_dropped
;
583 dev_kfree_skb_any(skb
);
586 spin_unlock(&priv
->lock
);
590 if (path
->pathrec
.dlid
) {
591 ipoib_dbg(priv
, "Send unicast ARP to %04x\n",
592 be16_to_cpu(path
->pathrec
.dlid
));
594 ipoib_send(dev
, skb
, path
->ah
,
595 be32_to_cpup((__be32
*) phdr
->hwaddr
));
596 } else if ((path
->query
|| !path_rec_start(dev
, path
)) &&
597 skb_queue_len(&path
->queue
) < IPOIB_MAX_PATH_REC_QUEUE
) {
598 /* put pseudoheader back on for next time */
599 skb_push(skb
, sizeof *phdr
);
600 __skb_queue_tail(&path
->queue
, skb
);
602 ++priv
->stats
.tx_dropped
;
603 dev_kfree_skb_any(skb
);
606 spin_unlock(&priv
->lock
);
609 static int ipoib_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
611 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
612 struct ipoib_neigh
*neigh
;
615 if (!spin_trylock_irqsave(&priv
->tx_lock
, flags
))
616 return NETDEV_TX_LOCKED
;
619 * Check if our queue is stopped. Since we have the LLTX bit
620 * set, we can't rely on netif_stop_queue() preventing our
621 * xmit function from being called with a full queue.
623 if (unlikely(netif_queue_stopped(dev
))) {
624 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
625 return NETDEV_TX_BUSY
;
628 if (skb
->dst
&& skb
->dst
->neighbour
) {
629 if (unlikely(!*to_ipoib_neigh(skb
->dst
->neighbour
))) {
630 ipoib_path_lookup(skb
, dev
);
634 neigh
= *to_ipoib_neigh(skb
->dst
->neighbour
);
636 if (likely(neigh
->ah
)) {
637 ipoib_send(dev
, skb
, neigh
->ah
,
638 be32_to_cpup((__be32
*) skb
->dst
->neighbour
->ha
));
642 if (skb_queue_len(&neigh
->queue
) < IPOIB_MAX_PATH_REC_QUEUE
) {
643 spin_lock(&priv
->lock
);
644 __skb_queue_tail(&neigh
->queue
, skb
);
645 spin_unlock(&priv
->lock
);
647 ++priv
->stats
.tx_dropped
;
648 dev_kfree_skb_any(skb
);
651 struct ipoib_pseudoheader
*phdr
=
652 (struct ipoib_pseudoheader
*) skb
->data
;
653 skb_pull(skb
, sizeof *phdr
);
655 if (phdr
->hwaddr
[4] == 0xff) {
656 /* Add in the P_Key for multicast*/
657 phdr
->hwaddr
[8] = (priv
->pkey
>> 8) & 0xff;
658 phdr
->hwaddr
[9] = priv
->pkey
& 0xff;
660 ipoib_mcast_send(dev
, (union ib_gid
*) (phdr
->hwaddr
+ 4), skb
);
662 /* unicast GID -- should be ARP or RARP reply */
664 if ((be16_to_cpup((__be16
*) skb
->data
) != ETH_P_ARP
) &&
665 (be16_to_cpup((__be16
*) skb
->data
) != ETH_P_RARP
)) {
666 ipoib_warn(priv
, "Unicast, no %s: type %04x, QPN %06x "
668 skb
->dst
? "neigh" : "dst",
669 be16_to_cpup((__be16
*) skb
->data
),
670 be32_to_cpup((__be32
*) phdr
->hwaddr
),
671 IPOIB_GID_ARG(*(union ib_gid
*) (phdr
->hwaddr
+ 4)));
672 dev_kfree_skb_any(skb
);
673 ++priv
->stats
.tx_dropped
;
677 unicast_arp_send(skb
, dev
, phdr
);
682 spin_unlock_irqrestore(&priv
->tx_lock
, flags
);
687 static struct net_device_stats
*ipoib_get_stats(struct net_device
*dev
)
689 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
694 static void ipoib_timeout(struct net_device
*dev
)
696 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
698 ipoib_warn(priv
, "transmit timeout: latency %d msecs\n",
699 jiffies_to_msecs(jiffies
- dev
->trans_start
));
700 ipoib_warn(priv
, "queue stopped %d, tx_head %u, tx_tail %u\n",
701 netif_queue_stopped(dev
),
702 priv
->tx_head
, priv
->tx_tail
);
703 /* XXX reset QP, etc. */
706 static int ipoib_hard_header(struct sk_buff
*skb
,
707 struct net_device
*dev
,
709 void *daddr
, void *saddr
, unsigned len
)
711 struct ipoib_header
*header
;
713 header
= (struct ipoib_header
*) skb_push(skb
, sizeof *header
);
715 header
->proto
= htons(type
);
716 header
->reserved
= 0;
719 * If we don't have a neighbour structure, stuff the
720 * destination address onto the front of the skb so we can
721 * figure out where to send the packet later.
723 if (!skb
->dst
|| !skb
->dst
->neighbour
) {
724 struct ipoib_pseudoheader
*phdr
=
725 (struct ipoib_pseudoheader
*) skb_push(skb
, sizeof *phdr
);
726 memcpy(phdr
->hwaddr
, daddr
, INFINIBAND_ALEN
);
732 static void ipoib_set_mcast_list(struct net_device
*dev
)
734 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
736 queue_work(ipoib_workqueue
, &priv
->restart_task
);
739 static void ipoib_neigh_destructor(struct neighbour
*n
)
741 struct ipoib_neigh
*neigh
;
742 struct ipoib_dev_priv
*priv
= netdev_priv(n
->dev
);
744 struct ipoib_ah
*ah
= NULL
;
747 "neigh_destructor for %06x " IPOIB_GID_FMT
"\n",
748 be32_to_cpup((__be32
*) n
->ha
),
749 IPOIB_GID_ARG(*((union ib_gid
*) (n
->ha
+ 4))));
751 spin_lock_irqsave(&priv
->lock
, flags
);
753 neigh
= *to_ipoib_neigh(n
);
757 list_del(&neigh
->list
);
758 *to_ipoib_neigh(n
) = NULL
;
762 spin_unlock_irqrestore(&priv
->lock
, flags
);
768 static int ipoib_neigh_setup(struct neighbour
*neigh
)
771 * Is this kosher? I can't find anybody in the kernel that
772 * sets neigh->destructor, so we should be able to set it here
775 neigh
->ops
->destructor
= ipoib_neigh_destructor
;
780 static int ipoib_neigh_setup_dev(struct net_device
*dev
, struct neigh_parms
*parms
)
782 parms
->neigh_setup
= ipoib_neigh_setup
;
787 int ipoib_dev_init(struct net_device
*dev
, struct ib_device
*ca
, int port
)
789 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
791 /* Allocate RX/TX "rings" to hold queued skbs */
793 priv
->rx_ring
= kzalloc(IPOIB_RX_RING_SIZE
* sizeof (struct ipoib_rx_buf
),
795 if (!priv
->rx_ring
) {
796 printk(KERN_WARNING
"%s: failed to allocate RX ring (%d entries)\n",
797 ca
->name
, IPOIB_RX_RING_SIZE
);
801 priv
->tx_ring
= kzalloc(IPOIB_TX_RING_SIZE
* sizeof (struct ipoib_tx_buf
),
803 if (!priv
->tx_ring
) {
804 printk(KERN_WARNING
"%s: failed to allocate TX ring (%d entries)\n",
805 ca
->name
, IPOIB_TX_RING_SIZE
);
806 goto out_rx_ring_cleanup
;
809 /* priv->tx_head & tx_tail are already 0 */
811 if (ipoib_ib_dev_init(dev
, ca
, port
))
812 goto out_tx_ring_cleanup
;
817 kfree(priv
->tx_ring
);
820 kfree(priv
->rx_ring
);
826 void ipoib_dev_cleanup(struct net_device
*dev
)
828 struct ipoib_dev_priv
*priv
= netdev_priv(dev
), *cpriv
, *tcpriv
;
830 ipoib_delete_debug_files(dev
);
832 /* Delete any child interfaces first */
833 list_for_each_entry_safe(cpriv
, tcpriv
, &priv
->child_intfs
, list
) {
834 unregister_netdev(cpriv
->dev
);
835 ipoib_dev_cleanup(cpriv
->dev
);
836 free_netdev(cpriv
->dev
);
839 ipoib_ib_dev_cleanup(dev
);
841 kfree(priv
->rx_ring
);
842 kfree(priv
->tx_ring
);
844 priv
->rx_ring
= NULL
;
845 priv
->tx_ring
= NULL
;
848 static void ipoib_setup(struct net_device
*dev
)
850 struct ipoib_dev_priv
*priv
= netdev_priv(dev
);
852 dev
->open
= ipoib_open
;
853 dev
->stop
= ipoib_stop
;
854 dev
->change_mtu
= ipoib_change_mtu
;
855 dev
->hard_start_xmit
= ipoib_start_xmit
;
856 dev
->get_stats
= ipoib_get_stats
;
857 dev
->tx_timeout
= ipoib_timeout
;
858 dev
->hard_header
= ipoib_hard_header
;
859 dev
->set_multicast_list
= ipoib_set_mcast_list
;
860 dev
->neigh_setup
= ipoib_neigh_setup_dev
;
862 dev
->watchdog_timeo
= HZ
;
864 dev
->flags
|= IFF_BROADCAST
| IFF_MULTICAST
;
867 * We add in INFINIBAND_ALEN to allow for the destination
868 * address "pseudoheader" for skbs without neighbour struct.
870 dev
->hard_header_len
= IPOIB_ENCAP_LEN
+ INFINIBAND_ALEN
;
871 dev
->addr_len
= INFINIBAND_ALEN
;
872 dev
->type
= ARPHRD_INFINIBAND
;
873 dev
->tx_queue_len
= IPOIB_TX_RING_SIZE
* 2;
874 dev
->features
= NETIF_F_VLAN_CHALLENGED
| NETIF_F_LLTX
;
876 /* MTU will be reset when mcast join happens */
877 dev
->mtu
= IPOIB_PACKET_SIZE
- IPOIB_ENCAP_LEN
;
878 priv
->mcast_mtu
= priv
->admin_mtu
= dev
->mtu
;
880 memcpy(dev
->broadcast
, ipv4_bcast_addr
, INFINIBAND_ALEN
);
882 netif_carrier_off(dev
);
884 SET_MODULE_OWNER(dev
);
888 spin_lock_init(&priv
->lock
);
889 spin_lock_init(&priv
->tx_lock
);
891 init_MUTEX(&priv
->mcast_mutex
);
892 init_MUTEX(&priv
->vlan_mutex
);
894 INIT_LIST_HEAD(&priv
->path_list
);
895 INIT_LIST_HEAD(&priv
->child_intfs
);
896 INIT_LIST_HEAD(&priv
->dead_ahs
);
897 INIT_LIST_HEAD(&priv
->multicast_list
);
899 INIT_WORK(&priv
->pkey_task
, ipoib_pkey_poll
, priv
->dev
);
900 INIT_WORK(&priv
->mcast_task
, ipoib_mcast_join_task
, priv
->dev
);
901 INIT_WORK(&priv
->flush_task
, ipoib_ib_dev_flush
, priv
->dev
);
902 INIT_WORK(&priv
->restart_task
, ipoib_mcast_restart_task
, priv
->dev
);
903 INIT_WORK(&priv
->ah_reap_task
, ipoib_reap_ah
, priv
->dev
);
906 struct ipoib_dev_priv
*ipoib_intf_alloc(const char *name
)
908 struct net_device
*dev
;
910 dev
= alloc_netdev((int) sizeof (struct ipoib_dev_priv
), name
,
915 return netdev_priv(dev
);
918 static ssize_t
show_pkey(struct class_device
*cdev
, char *buf
)
920 struct ipoib_dev_priv
*priv
=
921 netdev_priv(container_of(cdev
, struct net_device
, class_dev
));
923 return sprintf(buf
, "0x%04x\n", priv
->pkey
);
925 static CLASS_DEVICE_ATTR(pkey
, S_IRUGO
, show_pkey
, NULL
);
927 static ssize_t
create_child(struct class_device
*cdev
,
928 const char *buf
, size_t count
)
933 if (sscanf(buf
, "%i", &pkey
) != 1)
936 if (pkey
< 0 || pkey
> 0xffff)
940 * Set the full membership bit, so that we join the right
941 * broadcast group, etc.
945 ret
= ipoib_vlan_add(container_of(cdev
, struct net_device
, class_dev
),
948 return ret
? ret
: count
;
950 static CLASS_DEVICE_ATTR(create_child
, S_IWUGO
, NULL
, create_child
);
952 static ssize_t
delete_child(struct class_device
*cdev
,
953 const char *buf
, size_t count
)
958 if (sscanf(buf
, "%i", &pkey
) != 1)
961 if (pkey
< 0 || pkey
> 0xffff)
964 ret
= ipoib_vlan_delete(container_of(cdev
, struct net_device
, class_dev
),
967 return ret
? ret
: count
;
970 static CLASS_DEVICE_ATTR(delete_child
, S_IWUGO
, NULL
, delete_child
);
972 int ipoib_add_pkey_attr(struct net_device
*dev
)
974 return class_device_create_file(&dev
->class_dev
,
975 &class_device_attr_pkey
);
978 static struct net_device
*ipoib_add_port(const char *format
,
979 struct ib_device
*hca
, u8 port
)
981 struct ipoib_dev_priv
*priv
;
982 int result
= -ENOMEM
;
984 priv
= ipoib_intf_alloc(format
);
986 goto alloc_mem_failed
;
988 SET_NETDEV_DEV(priv
->dev
, hca
->dma_device
);
990 result
= ib_query_pkey(hca
, port
, 0, &priv
->pkey
);
992 printk(KERN_WARNING
"%s: ib_query_pkey port %d failed (ret = %d)\n",
993 hca
->name
, port
, result
);
994 goto alloc_mem_failed
;
998 * Set the full membership bit, so that we join the right
999 * broadcast group, etc.
1001 priv
->pkey
|= 0x8000;
1003 priv
->dev
->broadcast
[8] = priv
->pkey
>> 8;
1004 priv
->dev
->broadcast
[9] = priv
->pkey
& 0xff;
1006 result
= ib_query_gid(hca
, port
, 0, &priv
->local_gid
);
1008 printk(KERN_WARNING
"%s: ib_query_gid port %d failed (ret = %d)\n",
1009 hca
->name
, port
, result
);
1010 goto alloc_mem_failed
;
1012 memcpy(priv
->dev
->dev_addr
+ 4, priv
->local_gid
.raw
, sizeof (union ib_gid
));
1015 result
= ipoib_dev_init(priv
->dev
, hca
, port
);
1017 printk(KERN_WARNING
"%s: failed to initialize port %d (ret = %d)\n",
1018 hca
->name
, port
, result
);
1019 goto device_init_failed
;
1022 INIT_IB_EVENT_HANDLER(&priv
->event_handler
,
1023 priv
->ca
, ipoib_event
);
1024 result
= ib_register_event_handler(&priv
->event_handler
);
1026 printk(KERN_WARNING
"%s: ib_register_event_handler failed for "
1027 "port %d (ret = %d)\n",
1028 hca
->name
, port
, result
);
1032 result
= register_netdev(priv
->dev
);
1034 printk(KERN_WARNING
"%s: couldn't register ipoib port %d; error %d\n",
1035 hca
->name
, port
, result
);
1036 goto register_failed
;
1039 ipoib_create_debug_files(priv
->dev
);
1041 if (ipoib_add_pkey_attr(priv
->dev
))
1043 if (class_device_create_file(&priv
->dev
->class_dev
,
1044 &class_device_attr_create_child
))
1046 if (class_device_create_file(&priv
->dev
->class_dev
,
1047 &class_device_attr_delete_child
))
1053 ipoib_delete_debug_files(priv
->dev
);
1054 unregister_netdev(priv
->dev
);
1057 ib_unregister_event_handler(&priv
->event_handler
);
1058 flush_scheduled_work();
1061 ipoib_dev_cleanup(priv
->dev
);
1064 free_netdev(priv
->dev
);
1067 return ERR_PTR(result
);
1070 static void ipoib_add_one(struct ib_device
*device
)
1072 struct list_head
*dev_list
;
1073 struct net_device
*dev
;
1074 struct ipoib_dev_priv
*priv
;
1077 dev_list
= kmalloc(sizeof *dev_list
, GFP_KERNEL
);
1081 INIT_LIST_HEAD(dev_list
);
1083 if (device
->node_type
== IB_NODE_SWITCH
) {
1088 e
= device
->phys_port_cnt
;
1091 for (p
= s
; p
<= e
; ++p
) {
1092 dev
= ipoib_add_port("ib%d", device
, p
);
1094 priv
= netdev_priv(dev
);
1095 list_add_tail(&priv
->list
, dev_list
);
1099 ib_set_client_data(device
, &ipoib_client
, dev_list
);
1102 static void ipoib_remove_one(struct ib_device
*device
)
1104 struct ipoib_dev_priv
*priv
, *tmp
;
1105 struct list_head
*dev_list
;
1107 dev_list
= ib_get_client_data(device
, &ipoib_client
);
1109 list_for_each_entry_safe(priv
, tmp
, dev_list
, list
) {
1110 ib_unregister_event_handler(&priv
->event_handler
);
1111 flush_scheduled_work();
1113 unregister_netdev(priv
->dev
);
1114 ipoib_dev_cleanup(priv
->dev
);
1115 free_netdev(priv
->dev
);
1121 static int __init
ipoib_init_module(void)
1125 ret
= ipoib_register_debugfs();
1130 * We create our own workqueue mainly because we want to be
1131 * able to flush it when devices are being removed. We can't
1132 * use schedule_work()/flush_scheduled_work() because both
1133 * unregister_netdev() and linkwatch_event take the rtnl lock,
1134 * so flush_scheduled_work() can deadlock during device
1137 ipoib_workqueue
= create_singlethread_workqueue("ipoib");
1138 if (!ipoib_workqueue
) {
1143 ret
= ib_register_client(&ipoib_client
);
1150 destroy_workqueue(ipoib_workqueue
);
1153 ipoib_unregister_debugfs();
1158 static void __exit
ipoib_cleanup_module(void)
1160 ib_unregister_client(&ipoib_client
);
1161 ipoib_unregister_debugfs();
1162 destroy_workqueue(ipoib_workqueue
);
1165 module_init(ipoib_init_module
);
1166 module_exit(ipoib_cleanup_module
);