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);
431 init_completion(&path->done);
433 memcpy(path->pathrec.dgid.raw, gid->raw, sizeof (union ib_gid));
434 path->pathrec.sgid = priv->local_gid;
435 path->pathrec.pkey = cpu_to_be16(priv->pkey);
436 path->pathrec.numb_path = 1;
441 static int path_rec_start(struct net_device *dev,
442 struct ipoib_path *path)
444 struct ipoib_dev_priv *priv = netdev_priv(dev);
446 ipoib_dbg(priv, "Start path record lookup for " IPOIB_GID_FMT "\n",
447 IPOIB_GID_ARG(path->pathrec.dgid));
450 ib_sa_path_rec_get(priv->ca, priv->port,
452 IB_SA_PATH_REC_DGID |
453 IB_SA_PATH_REC_SGID |
454 IB_SA_PATH_REC_NUMB_PATH |
459 if (path->query_id < 0) {
460 ipoib_warn(priv, "ib_sa_path_rec_get failed\n");
462 return path->query_id;
468 static void neigh_add_path(struct sk_buff *skb, struct net_device *dev)
470 struct ipoib_dev_priv *priv = netdev_priv(dev);
471 struct ipoib_path *path;
472 struct ipoib_neigh *neigh;
474 neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
476 ++priv->stats.tx_dropped;
477 dev_kfree_skb_any(skb);
481 skb_queue_head_init(&neigh->queue);
482 neigh->neighbour = skb->dst->neighbour;
483 *to_ipoib_neigh(skb->dst->neighbour) = neigh;
486 * We can only be called from ipoib_start_xmit, so we're
487 * inside tx_lock -- no need to save/restore flags.
489 spin_lock(&priv->lock);
491 path = __path_find(dev, (union ib_gid *) (skb->dst->neighbour->ha + 4));
493 path = path_rec_create(dev,
494 (union ib_gid *) (skb->dst->neighbour->ha + 4));
498 __path_add(dev, path);
501 list_add_tail(&neigh->list, &path->neigh_list);
503 if (path->pathrec.dlid) {
504 kref_get(&path->ah->ref);
505 neigh->ah = path->ah;
507 ipoib_send(dev, skb, path->ah,
508 be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
511 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
512 __skb_queue_tail(&neigh->queue, skb);
514 ++priv->stats.tx_dropped;
515 dev_kfree_skb_any(skb);
518 if (!path->query && path_rec_start(dev, path))
522 spin_unlock(&priv->lock);
526 *to_ipoib_neigh(skb->dst->neighbour) = NULL;
527 list_del(&neigh->list);
528 neigh->neighbour->ops->destructor = NULL;
531 ++priv->stats.tx_dropped;
532 dev_kfree_skb_any(skb);
534 spin_unlock(&priv->lock);
537 static void ipoib_path_lookup(struct sk_buff *skb, struct net_device *dev)
539 struct ipoib_dev_priv *priv = netdev_priv(skb->dev);
541 /* Look up path record for unicasts */
542 if (skb->dst->neighbour->ha[4] != 0xff) {
543 neigh_add_path(skb, dev);
547 /* Add in the P_Key for multicasts */
548 skb->dst->neighbour->ha[8] = (priv->pkey >> 8) & 0xff;
549 skb->dst->neighbour->ha[9] = priv->pkey & 0xff;
550 ipoib_mcast_send(dev, (union ib_gid *) (skb->dst->neighbour->ha + 4), skb);
553 static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
554 struct ipoib_pseudoheader *phdr)
556 struct ipoib_dev_priv *priv = netdev_priv(dev);
557 struct ipoib_path *path;
560 * We can only be called from ipoib_start_xmit, so we're
561 * inside tx_lock -- no need to save/restore flags.
563 spin_lock(&priv->lock);
565 path = __path_find(dev, (union ib_gid *) (phdr->hwaddr + 4));
567 path = path_rec_create(dev,
568 (union ib_gid *) (phdr->hwaddr + 4));
570 /* put pseudoheader back on for next time */
571 skb_push(skb, sizeof *phdr);
572 __skb_queue_tail(&path->queue, skb);
574 if (path_rec_start(dev, path)) {
575 spin_unlock(&priv->lock);
576 path_free(dev, path);
579 __path_add(dev, path);
581 ++priv->stats.tx_dropped;
582 dev_kfree_skb_any(skb);
585 spin_unlock(&priv->lock);
589 if (path->pathrec.dlid) {
590 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
591 be16_to_cpu(path->pathrec.dlid));
593 ipoib_send(dev, skb, path->ah,
594 be32_to_cpup((__be32 *) phdr->hwaddr));
595 } else if ((path->query || !path_rec_start(dev, path)) &&
596 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
597 /* put pseudoheader back on for next time */
598 skb_push(skb, sizeof *phdr);
599 __skb_queue_tail(&path->queue, skb);
601 ++priv->stats.tx_dropped;
602 dev_kfree_skb_any(skb);
605 spin_unlock(&priv->lock);
608 static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
610 struct ipoib_dev_priv *priv = netdev_priv(dev);
611 struct ipoib_neigh *neigh;
614 if (!spin_trylock_irqsave(&priv->tx_lock, flags))
615 return NETDEV_TX_LOCKED;
618 * Check if our queue is stopped. Since we have the LLTX bit
619 * set, we can't rely on netif_stop_queue() preventing our
620 * xmit function from being called with a full queue.
622 if (unlikely(netif_queue_stopped(dev))) {
623 spin_unlock_irqrestore(&priv->tx_lock, flags);
624 return NETDEV_TX_BUSY;
627 if (skb->dst && skb->dst->neighbour) {
628 if (unlikely(!*to_ipoib_neigh(skb->dst->neighbour))) {
629 ipoib_path_lookup(skb, dev);
633 neigh = *to_ipoib_neigh(skb->dst->neighbour);
635 if (likely(neigh->ah)) {
636 ipoib_send(dev, skb, neigh->ah,
637 be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
641 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
642 spin_lock(&priv->lock);
643 __skb_queue_tail(&neigh->queue, skb);
644 spin_unlock(&priv->lock);
646 ++priv->stats.tx_dropped;
647 dev_kfree_skb_any(skb);
650 struct ipoib_pseudoheader *phdr =
651 (struct ipoib_pseudoheader *) skb->data;
652 skb_pull(skb, sizeof *phdr);
654 if (phdr->hwaddr[4] == 0xff) {
655 /* Add in the P_Key for multicast*/
656 phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
657 phdr->hwaddr[9] = priv->pkey & 0xff;
659 ipoib_mcast_send(dev, (union ib_gid *) (phdr->hwaddr + 4), skb);
661 /* unicast GID -- should be ARP or RARP reply */
663 if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
664 (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
665 ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x "
667 skb->dst ? "neigh" : "dst",
668 be16_to_cpup((__be16 *) skb->data),
669 be32_to_cpup((__be32 *) phdr->hwaddr),
670 IPOIB_GID_ARG(*(union ib_gid *) (phdr->hwaddr + 4)));
671 dev_kfree_skb_any(skb);
672 ++priv->stats.tx_dropped;
676 unicast_arp_send(skb, dev, phdr);
681 spin_unlock_irqrestore(&priv->tx_lock, flags);
686 static struct net_device_stats *ipoib_get_stats(struct net_device *dev)
688 struct ipoib_dev_priv *priv = netdev_priv(dev);
693 static void ipoib_timeout(struct net_device *dev)
695 struct ipoib_dev_priv *priv = netdev_priv(dev);
697 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
698 jiffies_to_msecs(jiffies - dev->trans_start));
699 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
700 netif_queue_stopped(dev),
701 priv->tx_head, priv->tx_tail);
702 /* XXX reset QP, etc. */
705 static int ipoib_hard_header(struct sk_buff *skb,
706 struct net_device *dev,
708 void *daddr, void *saddr, unsigned len)
710 struct ipoib_header *header;
712 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
714 header->proto = htons(type);
715 header->reserved = 0;
718 * If we don't have a neighbour structure, stuff the
719 * destination address onto the front of the skb so we can
720 * figure out where to send the packet later.
722 if (!skb->dst || !skb->dst->neighbour) {
723 struct ipoib_pseudoheader *phdr =
724 (struct ipoib_pseudoheader *) skb_push(skb, sizeof *phdr);
725 memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
731 static void ipoib_set_mcast_list(struct net_device *dev)
733 struct ipoib_dev_priv *priv = netdev_priv(dev);
735 queue_work(ipoib_workqueue, &priv->restart_task);
738 static void ipoib_neigh_destructor(struct neighbour *n)
740 struct ipoib_neigh *neigh;
741 struct ipoib_dev_priv *priv = netdev_priv(n->dev);
743 struct ipoib_ah *ah = NULL;
746 "neigh_destructor for %06x " IPOIB_GID_FMT "\n",
747 be32_to_cpup((__be32 *) n->ha),
748 IPOIB_GID_ARG(*((union ib_gid *) (n->ha + 4))));
750 spin_lock_irqsave(&priv->lock, flags);
752 neigh = *to_ipoib_neigh(n);
756 list_del(&neigh->list);
757 *to_ipoib_neigh(n) = NULL;
761 spin_unlock_irqrestore(&priv->lock, flags);
767 static int ipoib_neigh_setup(struct neighbour *neigh)
770 * Is this kosher? I can't find anybody in the kernel that
771 * sets neigh->destructor, so we should be able to set it here
774 neigh->ops->destructor = ipoib_neigh_destructor;
779 static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
781 parms->neigh_setup = ipoib_neigh_setup;
786 int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
788 struct ipoib_dev_priv *priv = netdev_priv(dev);
790 /* Allocate RX/TX "rings" to hold queued skbs */
792 priv->rx_ring = kzalloc(IPOIB_RX_RING_SIZE * sizeof (struct ipoib_rx_buf),
794 if (!priv->rx_ring) {
795 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
796 ca->name, IPOIB_RX_RING_SIZE);
800 priv->tx_ring = kzalloc(IPOIB_TX_RING_SIZE * sizeof (struct ipoib_tx_buf),
802 if (!priv->tx_ring) {
803 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
804 ca->name, IPOIB_TX_RING_SIZE);
805 goto out_rx_ring_cleanup;
808 /* priv->tx_head & tx_tail are already 0 */
810 if (ipoib_ib_dev_init(dev, ca, port))
811 goto out_tx_ring_cleanup;
816 kfree(priv->tx_ring);
819 kfree(priv->rx_ring);
825 void ipoib_dev_cleanup(struct net_device *dev)
827 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
829 ipoib_delete_debug_files(dev);
831 /* Delete any child interfaces first */
832 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
833 unregister_netdev(cpriv->dev);
834 ipoib_dev_cleanup(cpriv->dev);
835 free_netdev(cpriv->dev);
838 ipoib_ib_dev_cleanup(dev);
840 kfree(priv->rx_ring);
841 kfree(priv->tx_ring);
843 priv->rx_ring = NULL;
844 priv->tx_ring = NULL;
847 static void ipoib_setup(struct net_device *dev)
849 struct ipoib_dev_priv *priv = netdev_priv(dev);
851 dev->open = ipoib_open;
852 dev->stop = ipoib_stop;
853 dev->change_mtu = ipoib_change_mtu;
854 dev->hard_start_xmit = ipoib_start_xmit;
855 dev->get_stats = ipoib_get_stats;
856 dev->tx_timeout = ipoib_timeout;
857 dev->hard_header = ipoib_hard_header;
858 dev->set_multicast_list = ipoib_set_mcast_list;
859 dev->neigh_setup = ipoib_neigh_setup_dev;
861 dev->watchdog_timeo = HZ;
863 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
866 * We add in INFINIBAND_ALEN to allow for the destination
867 * address "pseudoheader" for skbs without neighbour struct.
869 dev->hard_header_len = IPOIB_ENCAP_LEN + INFINIBAND_ALEN;
870 dev->addr_len = INFINIBAND_ALEN;
871 dev->type = ARPHRD_INFINIBAND;
872 dev->tx_queue_len = IPOIB_TX_RING_SIZE * 2;
873 dev->features = NETIF_F_VLAN_CHALLENGED | NETIF_F_LLTX;
875 /* MTU will be reset when mcast join happens */
876 dev->mtu = IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN;
877 priv->mcast_mtu = priv->admin_mtu = dev->mtu;
879 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
881 netif_carrier_off(dev);
883 SET_MODULE_OWNER(dev);
887 spin_lock_init(&priv->lock);
888 spin_lock_init(&priv->tx_lock);
890 init_MUTEX(&priv->mcast_mutex);
891 init_MUTEX(&priv->vlan_mutex);
893 INIT_LIST_HEAD(&priv->path_list);
894 INIT_LIST_HEAD(&priv->child_intfs);
895 INIT_LIST_HEAD(&priv->dead_ahs);
896 INIT_LIST_HEAD(&priv->multicast_list);
898 INIT_WORK(&priv->pkey_task, ipoib_pkey_poll, priv->dev);
899 INIT_WORK(&priv->mcast_task, ipoib_mcast_join_task, priv->dev);
900 INIT_WORK(&priv->flush_task, ipoib_ib_dev_flush, priv->dev);
901 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task, priv->dev);
902 INIT_WORK(&priv->ah_reap_task, ipoib_reap_ah, priv->dev);
905 struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
907 struct net_device *dev;
909 dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
914 return netdev_priv(dev);
917 static ssize_t show_pkey(struct class_device *cdev, char *buf)
919 struct ipoib_dev_priv *priv =
920 netdev_priv(container_of(cdev, struct net_device, class_dev));
922 return sprintf(buf, "0x%04x\n", priv->pkey);
924 static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
926 static ssize_t create_child(struct class_device *cdev,
927 const char *buf, size_t count)
932 if (sscanf(buf, "%i", &pkey) != 1)
935 if (pkey < 0 || pkey > 0xffff)
939 * Set the full membership bit, so that we join the right
940 * broadcast group, etc.
944 ret = ipoib_vlan_add(container_of(cdev, struct net_device, class_dev),
947 return ret ? ret : count;
949 static CLASS_DEVICE_ATTR(create_child, S_IWUGO, NULL, create_child);
951 static ssize_t delete_child(struct class_device *cdev,
952 const char *buf, size_t count)
957 if (sscanf(buf, "%i", &pkey) != 1)
960 if (pkey < 0 || pkey > 0xffff)
963 ret = ipoib_vlan_delete(container_of(cdev, struct net_device, class_dev),
966 return ret ? ret : count;
969 static CLASS_DEVICE_ATTR(delete_child, S_IWUGO, NULL, delete_child);
971 int ipoib_add_pkey_attr(struct net_device *dev)
973 return class_device_create_file(&dev->class_dev,
974 &class_device_attr_pkey);
977 static struct net_device *ipoib_add_port(const char *format,
978 struct ib_device *hca, u8 port)
980 struct ipoib_dev_priv *priv;
981 int result = -ENOMEM;
983 priv = ipoib_intf_alloc(format);
985 goto alloc_mem_failed;
987 SET_NETDEV_DEV(priv->dev, hca->dma_device);
989 result = ib_query_pkey(hca, port, 0, &priv->pkey);
991 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
992 hca->name, port, result);
993 goto alloc_mem_failed;
997 * Set the full membership bit, so that we join the right
998 * broadcast group, etc.
1000 priv->pkey |= 0x8000;
1002 priv->dev->broadcast[8] = priv->pkey >> 8;
1003 priv->dev->broadcast[9] = priv->pkey & 0xff;
1005 result = ib_query_gid(hca, port, 0, &priv->local_gid);
1007 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1008 hca->name, port, result);
1009 goto alloc_mem_failed;
1011 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1014 result = ipoib_dev_init(priv->dev, hca, port);
1016 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1017 hca->name, port, result);
1018 goto device_init_failed;
1021 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1022 priv->ca, ipoib_event);
1023 result = ib_register_event_handler(&priv->event_handler);
1025 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1026 "port %d (ret = %d)\n",
1027 hca->name, port, result);
1031 result = register_netdev(priv->dev);
1033 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1034 hca->name, port, result);
1035 goto register_failed;
1038 ipoib_create_debug_files(priv->dev);
1040 if (ipoib_add_pkey_attr(priv->dev))
1042 if (class_device_create_file(&priv->dev->class_dev,
1043 &class_device_attr_create_child))
1045 if (class_device_create_file(&priv->dev->class_dev,
1046 &class_device_attr_delete_child))
1052 ipoib_delete_debug_files(priv->dev);
1053 unregister_netdev(priv->dev);
1056 ib_unregister_event_handler(&priv->event_handler);
1057 flush_scheduled_work();
1060 ipoib_dev_cleanup(priv->dev);
1063 free_netdev(priv->dev);
1066 return ERR_PTR(result);
1069 static void ipoib_add_one(struct ib_device *device)
1071 struct list_head *dev_list;
1072 struct net_device *dev;
1073 struct ipoib_dev_priv *priv;
1076 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1080 INIT_LIST_HEAD(dev_list);
1082 if (device->node_type == IB_NODE_SWITCH) {
1087 e = device->phys_port_cnt;
1090 for (p = s; p <= e; ++p) {
1091 dev = ipoib_add_port("ib%d", device, p);
1093 priv = netdev_priv(dev);
1094 list_add_tail(&priv->list, dev_list);
1098 ib_set_client_data(device, &ipoib_client, dev_list);
1101 static void ipoib_remove_one(struct ib_device *device)
1103 struct ipoib_dev_priv *priv, *tmp;
1104 struct list_head *dev_list;
1106 dev_list = ib_get_client_data(device, &ipoib_client);
1108 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1109 ib_unregister_event_handler(&priv->event_handler);
1110 flush_scheduled_work();
1112 unregister_netdev(priv->dev);
1113 ipoib_dev_cleanup(priv->dev);
1114 free_netdev(priv->dev);
1120 static int __init ipoib_init_module(void)
1124 ret = ipoib_register_debugfs();
1129 * We create our own workqueue mainly because we want to be
1130 * able to flush it when devices are being removed. We can't
1131 * use schedule_work()/flush_scheduled_work() because both
1132 * unregister_netdev() and linkwatch_event take the rtnl lock,
1133 * so flush_scheduled_work() can deadlock during device
1136 ipoib_workqueue = create_singlethread_workqueue("ipoib");
1137 if (!ipoib_workqueue) {
1142 ret = ib_register_client(&ipoib_client);
1149 destroy_workqueue(ipoib_workqueue);
1152 ipoib_unregister_debugfs();
1157 static void __exit ipoib_cleanup_module(void)
1159 ib_unregister_client(&ipoib_client);
1160 ipoib_unregister_debugfs();
1161 destroy_workqueue(ipoib_workqueue);
1164 module_init(ipoib_init_module);
1165 module_exit(ipoib_cleanup_module);