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)) {
98 ipoib_ib_dev_stop(dev);
102 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
103 struct ipoib_dev_priv *cpriv;
105 /* Bring up any child interfaces too */
106 down(&priv->vlan_mutex);
107 list_for_each_entry(cpriv, &priv->child_intfs, list) {
110 flags = cpriv->dev->flags;
114 dev_change_flags(cpriv->dev, flags | IFF_UP);
116 up(&priv->vlan_mutex);
119 netif_start_queue(dev);
124 static int ipoib_stop(struct net_device *dev)
126 struct ipoib_dev_priv *priv = netdev_priv(dev);
128 ipoib_dbg(priv, "stopping interface\n");
130 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
132 netif_stop_queue(dev);
134 ipoib_ib_dev_down(dev);
135 ipoib_ib_dev_stop(dev);
137 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
138 struct ipoib_dev_priv *cpriv;
140 /* Bring down any child interfaces too */
141 down(&priv->vlan_mutex);
142 list_for_each_entry(cpriv, &priv->child_intfs, list) {
145 flags = cpriv->dev->flags;
146 if (!(flags & IFF_UP))
149 dev_change_flags(cpriv->dev, flags & ~IFF_UP);
151 up(&priv->vlan_mutex);
157 static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
159 struct ipoib_dev_priv *priv = netdev_priv(dev);
161 if (new_mtu > IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN)
164 priv->admin_mtu = new_mtu;
166 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
171 static struct ipoib_path *__path_find(struct net_device *dev,
174 struct ipoib_dev_priv *priv = netdev_priv(dev);
175 struct rb_node *n = priv->path_tree.rb_node;
176 struct ipoib_path *path;
180 path = rb_entry(n, struct ipoib_path, rb_node);
182 ret = memcmp(gid->raw, path->pathrec.dgid.raw,
183 sizeof (union ib_gid));
196 static int __path_add(struct net_device *dev, struct ipoib_path *path)
198 struct ipoib_dev_priv *priv = netdev_priv(dev);
199 struct rb_node **n = &priv->path_tree.rb_node;
200 struct rb_node *pn = NULL;
201 struct ipoib_path *tpath;
206 tpath = rb_entry(pn, struct ipoib_path, rb_node);
208 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
209 sizeof (union ib_gid));
218 rb_link_node(&path->rb_node, pn, n);
219 rb_insert_color(&path->rb_node, &priv->path_tree);
221 list_add_tail(&path->list, &priv->path_list);
226 static void path_free(struct net_device *dev, struct ipoib_path *path)
228 struct ipoib_dev_priv *priv = netdev_priv(dev);
229 struct ipoib_neigh *neigh, *tn;
233 while ((skb = __skb_dequeue(&path->queue)))
234 dev_kfree_skb_irq(skb);
236 spin_lock_irqsave(&priv->lock, flags);
238 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
240 * It's safe to call ipoib_put_ah() inside priv->lock
241 * here, because we know that path->ah will always
242 * hold one more reference, so ipoib_put_ah() will
243 * never do more than decrement the ref count.
246 ipoib_put_ah(neigh->ah);
247 *to_ipoib_neigh(neigh->neighbour) = NULL;
248 neigh->neighbour->ops->destructor = NULL;
252 spin_unlock_irqrestore(&priv->lock, flags);
255 ipoib_put_ah(path->ah);
260 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
262 struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
264 struct ipoib_path_iter *iter;
266 iter = kmalloc(sizeof *iter, GFP_KERNEL);
271 memset(iter->path.pathrec.dgid.raw, 0, 16);
273 if (ipoib_path_iter_next(iter)) {
281 int ipoib_path_iter_next(struct ipoib_path_iter *iter)
283 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
285 struct ipoib_path *path;
288 spin_lock_irq(&priv->lock);
290 n = rb_first(&priv->path_tree);
293 path = rb_entry(n, struct ipoib_path, rb_node);
295 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
296 sizeof (union ib_gid)) < 0) {
305 spin_unlock_irq(&priv->lock);
310 void ipoib_path_iter_read(struct ipoib_path_iter *iter,
311 struct ipoib_path *path)
316 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
318 void ipoib_flush_paths(struct net_device *dev)
320 struct ipoib_dev_priv *priv = netdev_priv(dev);
321 struct ipoib_path *path, *tp;
322 LIST_HEAD(remove_list);
325 spin_lock_irqsave(&priv->lock, flags);
327 list_splice(&priv->path_list, &remove_list);
328 INIT_LIST_HEAD(&priv->path_list);
330 list_for_each_entry(path, &remove_list, list)
331 rb_erase(&path->rb_node, &priv->path_tree);
333 spin_unlock_irqrestore(&priv->lock, flags);
335 list_for_each_entry_safe(path, tp, &remove_list, list) {
337 ib_sa_cancel_query(path->query_id, path->query);
338 wait_for_completion(&path->done);
339 path_free(dev, path);
343 static void path_rec_completion(int status,
344 struct ib_sa_path_rec *pathrec,
347 struct ipoib_path *path = path_ptr;
348 struct net_device *dev = path->dev;
349 struct ipoib_dev_priv *priv = netdev_priv(dev);
350 struct ipoib_ah *ah = NULL;
351 struct ipoib_neigh *neigh;
352 struct sk_buff_head skqueue;
357 ipoib_dbg(priv, "PathRec LID 0x%04x for GID " IPOIB_GID_FMT "\n",
358 be16_to_cpu(pathrec->dlid), IPOIB_GID_ARG(pathrec->dgid));
360 ipoib_dbg(priv, "PathRec status %d for GID " IPOIB_GID_FMT "\n",
361 status, IPOIB_GID_ARG(path->pathrec.dgid));
363 skb_queue_head_init(&skqueue);
366 struct ib_ah_attr av = {
367 .dlid = be16_to_cpu(pathrec->dlid),
369 .port_num = priv->port
371 int path_rate = ib_sa_rate_enum_to_int(pathrec->rate);
373 if (path_rate > 0 && priv->local_rate > path_rate)
374 av.static_rate = (priv->local_rate - 1) / path_rate;
376 ipoib_dbg(priv, "static_rate %d for local port %dX, path %dX\n",
377 av.static_rate, priv->local_rate,
378 ib_sa_rate_enum_to_int(pathrec->rate));
380 ah = ipoib_create_ah(dev, priv->pd, &av);
383 spin_lock_irqsave(&priv->lock, flags);
388 path->pathrec = *pathrec;
390 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
391 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
393 while ((skb = __skb_dequeue(&path->queue)))
394 __skb_queue_tail(&skqueue, skb);
396 list_for_each_entry(neigh, &path->neigh_list, list) {
397 kref_get(&path->ah->ref);
398 neigh->ah = path->ah;
400 while ((skb = __skb_dequeue(&neigh->queue)))
401 __skb_queue_tail(&skqueue, skb);
406 complete(&path->done);
408 spin_unlock_irqrestore(&priv->lock, flags);
410 while ((skb = __skb_dequeue(&skqueue))) {
412 if (dev_queue_xmit(skb))
413 ipoib_warn(priv, "dev_queue_xmit failed "
414 "to requeue packet\n");
418 static struct ipoib_path *path_rec_create(struct net_device *dev,
421 struct ipoib_dev_priv *priv = netdev_priv(dev);
422 struct ipoib_path *path;
424 path = kzalloc(sizeof *path, GFP_ATOMIC);
430 skb_queue_head_init(&path->queue);
432 INIT_LIST_HEAD(&path->neigh_list);
434 memcpy(path->pathrec.dgid.raw, gid->raw, sizeof (union ib_gid));
435 path->pathrec.sgid = priv->local_gid;
436 path->pathrec.pkey = cpu_to_be16(priv->pkey);
437 path->pathrec.numb_path = 1;
442 static int path_rec_start(struct net_device *dev,
443 struct ipoib_path *path)
445 struct ipoib_dev_priv *priv = netdev_priv(dev);
447 ipoib_dbg(priv, "Start path record lookup for " IPOIB_GID_FMT "\n",
448 IPOIB_GID_ARG(path->pathrec.dgid));
450 init_completion(&path->done);
453 ib_sa_path_rec_get(priv->ca, priv->port,
455 IB_SA_PATH_REC_DGID |
456 IB_SA_PATH_REC_SGID |
457 IB_SA_PATH_REC_NUMB_PATH |
462 if (path->query_id < 0) {
463 ipoib_warn(priv, "ib_sa_path_rec_get failed\n");
465 return path->query_id;
471 static void neigh_add_path(struct sk_buff *skb, struct net_device *dev)
473 struct ipoib_dev_priv *priv = netdev_priv(dev);
474 struct ipoib_path *path;
475 struct ipoib_neigh *neigh;
477 neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
479 ++priv->stats.tx_dropped;
480 dev_kfree_skb_any(skb);
484 skb_queue_head_init(&neigh->queue);
485 neigh->neighbour = skb->dst->neighbour;
486 *to_ipoib_neigh(skb->dst->neighbour) = neigh;
489 * We can only be called from ipoib_start_xmit, so we're
490 * inside tx_lock -- no need to save/restore flags.
492 spin_lock(&priv->lock);
494 path = __path_find(dev, (union ib_gid *) (skb->dst->neighbour->ha + 4));
496 path = path_rec_create(dev,
497 (union ib_gid *) (skb->dst->neighbour->ha + 4));
501 __path_add(dev, path);
504 list_add_tail(&neigh->list, &path->neigh_list);
506 if (path->pathrec.dlid) {
507 kref_get(&path->ah->ref);
508 neigh->ah = path->ah;
510 ipoib_send(dev, skb, path->ah,
511 be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
514 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
515 __skb_queue_tail(&neigh->queue, skb);
517 ++priv->stats.tx_dropped;
518 dev_kfree_skb_any(skb);
521 if (!path->query && path_rec_start(dev, path))
525 spin_unlock(&priv->lock);
529 *to_ipoib_neigh(skb->dst->neighbour) = NULL;
530 list_del(&neigh->list);
531 neigh->neighbour->ops->destructor = NULL;
534 ++priv->stats.tx_dropped;
535 dev_kfree_skb_any(skb);
537 spin_unlock(&priv->lock);
540 static void ipoib_path_lookup(struct sk_buff *skb, struct net_device *dev)
542 struct ipoib_dev_priv *priv = netdev_priv(skb->dev);
544 /* Look up path record for unicasts */
545 if (skb->dst->neighbour->ha[4] != 0xff) {
546 neigh_add_path(skb, dev);
550 /* Add in the P_Key for multicasts */
551 skb->dst->neighbour->ha[8] = (priv->pkey >> 8) & 0xff;
552 skb->dst->neighbour->ha[9] = priv->pkey & 0xff;
553 ipoib_mcast_send(dev, (union ib_gid *) (skb->dst->neighbour->ha + 4), skb);
556 static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
557 struct ipoib_pseudoheader *phdr)
559 struct ipoib_dev_priv *priv = netdev_priv(dev);
560 struct ipoib_path *path;
563 * We can only be called from ipoib_start_xmit, so we're
564 * inside tx_lock -- no need to save/restore flags.
566 spin_lock(&priv->lock);
568 path = __path_find(dev, (union ib_gid *) (phdr->hwaddr + 4));
570 path = path_rec_create(dev,
571 (union ib_gid *) (phdr->hwaddr + 4));
573 /* put pseudoheader back on for next time */
574 skb_push(skb, sizeof *phdr);
575 __skb_queue_tail(&path->queue, skb);
577 if (path_rec_start(dev, path)) {
578 spin_unlock(&priv->lock);
579 path_free(dev, path);
582 __path_add(dev, path);
584 ++priv->stats.tx_dropped;
585 dev_kfree_skb_any(skb);
588 spin_unlock(&priv->lock);
592 if (path->pathrec.dlid) {
593 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
594 be16_to_cpu(path->pathrec.dlid));
596 ipoib_send(dev, skb, path->ah,
597 be32_to_cpup((__be32 *) phdr->hwaddr));
598 } else if ((path->query || !path_rec_start(dev, path)) &&
599 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
600 /* put pseudoheader back on for next time */
601 skb_push(skb, sizeof *phdr);
602 __skb_queue_tail(&path->queue, skb);
604 ++priv->stats.tx_dropped;
605 dev_kfree_skb_any(skb);
608 spin_unlock(&priv->lock);
611 static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
613 struct ipoib_dev_priv *priv = netdev_priv(dev);
614 struct ipoib_neigh *neigh;
617 if (!spin_trylock_irqsave(&priv->tx_lock, flags))
618 return NETDEV_TX_LOCKED;
621 * Check if our queue is stopped. Since we have the LLTX bit
622 * set, we can't rely on netif_stop_queue() preventing our
623 * xmit function from being called with a full queue.
625 if (unlikely(netif_queue_stopped(dev))) {
626 spin_unlock_irqrestore(&priv->tx_lock, flags);
627 return NETDEV_TX_BUSY;
630 if (skb->dst && skb->dst->neighbour) {
631 if (unlikely(!*to_ipoib_neigh(skb->dst->neighbour))) {
632 ipoib_path_lookup(skb, dev);
636 neigh = *to_ipoib_neigh(skb->dst->neighbour);
638 if (likely(neigh->ah)) {
639 ipoib_send(dev, skb, neigh->ah,
640 be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
644 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
645 spin_lock(&priv->lock);
646 __skb_queue_tail(&neigh->queue, skb);
647 spin_unlock(&priv->lock);
649 ++priv->stats.tx_dropped;
650 dev_kfree_skb_any(skb);
653 struct ipoib_pseudoheader *phdr =
654 (struct ipoib_pseudoheader *) skb->data;
655 skb_pull(skb, sizeof *phdr);
657 if (phdr->hwaddr[4] == 0xff) {
658 /* Add in the P_Key for multicast*/
659 phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
660 phdr->hwaddr[9] = priv->pkey & 0xff;
662 ipoib_mcast_send(dev, (union ib_gid *) (phdr->hwaddr + 4), skb);
664 /* unicast GID -- should be ARP or RARP reply */
666 if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
667 (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
668 ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x "
670 skb->dst ? "neigh" : "dst",
671 be16_to_cpup((__be16 *) skb->data),
672 be32_to_cpup((__be32 *) phdr->hwaddr),
673 IPOIB_GID_ARG(*(union ib_gid *) (phdr->hwaddr + 4)));
674 dev_kfree_skb_any(skb);
675 ++priv->stats.tx_dropped;
679 unicast_arp_send(skb, dev, phdr);
684 spin_unlock_irqrestore(&priv->tx_lock, flags);
689 static struct net_device_stats *ipoib_get_stats(struct net_device *dev)
691 struct ipoib_dev_priv *priv = netdev_priv(dev);
696 static void ipoib_timeout(struct net_device *dev)
698 struct ipoib_dev_priv *priv = netdev_priv(dev);
700 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
701 jiffies_to_msecs(jiffies - dev->trans_start));
702 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
703 netif_queue_stopped(dev),
704 priv->tx_head, priv->tx_tail);
705 /* XXX reset QP, etc. */
708 static int ipoib_hard_header(struct sk_buff *skb,
709 struct net_device *dev,
711 void *daddr, void *saddr, unsigned len)
713 struct ipoib_header *header;
715 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
717 header->proto = htons(type);
718 header->reserved = 0;
721 * If we don't have a neighbour structure, stuff the
722 * destination address onto the front of the skb so we can
723 * figure out where to send the packet later.
725 if (!skb->dst || !skb->dst->neighbour) {
726 struct ipoib_pseudoheader *phdr =
727 (struct ipoib_pseudoheader *) skb_push(skb, sizeof *phdr);
728 memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
734 static void ipoib_set_mcast_list(struct net_device *dev)
736 struct ipoib_dev_priv *priv = netdev_priv(dev);
738 queue_work(ipoib_workqueue, &priv->restart_task);
741 static void ipoib_neigh_destructor(struct neighbour *n)
743 struct ipoib_neigh *neigh;
744 struct ipoib_dev_priv *priv = netdev_priv(n->dev);
746 struct ipoib_ah *ah = NULL;
749 "neigh_destructor for %06x " IPOIB_GID_FMT "\n",
750 be32_to_cpup((__be32 *) n->ha),
751 IPOIB_GID_ARG(*((union ib_gid *) (n->ha + 4))));
753 spin_lock_irqsave(&priv->lock, flags);
755 neigh = *to_ipoib_neigh(n);
759 list_del(&neigh->list);
760 *to_ipoib_neigh(n) = NULL;
764 spin_unlock_irqrestore(&priv->lock, flags);
770 static int ipoib_neigh_setup(struct neighbour *neigh)
773 * Is this kosher? I can't find anybody in the kernel that
774 * sets neigh->destructor, so we should be able to set it here
777 neigh->ops->destructor = ipoib_neigh_destructor;
782 static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
784 parms->neigh_setup = ipoib_neigh_setup;
789 int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
791 struct ipoib_dev_priv *priv = netdev_priv(dev);
793 /* Allocate RX/TX "rings" to hold queued skbs */
795 priv->rx_ring = kzalloc(IPOIB_RX_RING_SIZE * sizeof (struct ipoib_rx_buf),
797 if (!priv->rx_ring) {
798 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
799 ca->name, IPOIB_RX_RING_SIZE);
803 priv->tx_ring = kzalloc(IPOIB_TX_RING_SIZE * sizeof (struct ipoib_tx_buf),
805 if (!priv->tx_ring) {
806 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
807 ca->name, IPOIB_TX_RING_SIZE);
808 goto out_rx_ring_cleanup;
811 /* priv->tx_head & tx_tail are already 0 */
813 if (ipoib_ib_dev_init(dev, ca, port))
814 goto out_tx_ring_cleanup;
819 kfree(priv->tx_ring);
822 kfree(priv->rx_ring);
828 void ipoib_dev_cleanup(struct net_device *dev)
830 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
832 ipoib_delete_debug_files(dev);
834 /* Delete any child interfaces first */
835 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
836 unregister_netdev(cpriv->dev);
837 ipoib_dev_cleanup(cpriv->dev);
838 free_netdev(cpriv->dev);
841 ipoib_ib_dev_cleanup(dev);
843 kfree(priv->rx_ring);
844 kfree(priv->tx_ring);
846 priv->rx_ring = NULL;
847 priv->tx_ring = NULL;
850 static void ipoib_setup(struct net_device *dev)
852 struct ipoib_dev_priv *priv = netdev_priv(dev);
854 dev->open = ipoib_open;
855 dev->stop = ipoib_stop;
856 dev->change_mtu = ipoib_change_mtu;
857 dev->hard_start_xmit = ipoib_start_xmit;
858 dev->get_stats = ipoib_get_stats;
859 dev->tx_timeout = ipoib_timeout;
860 dev->hard_header = ipoib_hard_header;
861 dev->set_multicast_list = ipoib_set_mcast_list;
862 dev->neigh_setup = ipoib_neigh_setup_dev;
864 dev->watchdog_timeo = HZ;
866 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
869 * We add in INFINIBAND_ALEN to allow for the destination
870 * address "pseudoheader" for skbs without neighbour struct.
872 dev->hard_header_len = IPOIB_ENCAP_LEN + INFINIBAND_ALEN;
873 dev->addr_len = INFINIBAND_ALEN;
874 dev->type = ARPHRD_INFINIBAND;
875 dev->tx_queue_len = IPOIB_TX_RING_SIZE * 2;
876 dev->features = NETIF_F_VLAN_CHALLENGED | NETIF_F_LLTX;
878 /* MTU will be reset when mcast join happens */
879 dev->mtu = IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN;
880 priv->mcast_mtu = priv->admin_mtu = dev->mtu;
882 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
884 netif_carrier_off(dev);
886 SET_MODULE_OWNER(dev);
890 spin_lock_init(&priv->lock);
891 spin_lock_init(&priv->tx_lock);
893 init_MUTEX(&priv->mcast_mutex);
894 init_MUTEX(&priv->vlan_mutex);
896 INIT_LIST_HEAD(&priv->path_list);
897 INIT_LIST_HEAD(&priv->child_intfs);
898 INIT_LIST_HEAD(&priv->dead_ahs);
899 INIT_LIST_HEAD(&priv->multicast_list);
901 INIT_WORK(&priv->pkey_task, ipoib_pkey_poll, priv->dev);
902 INIT_WORK(&priv->mcast_task, ipoib_mcast_join_task, priv->dev);
903 INIT_WORK(&priv->flush_task, ipoib_ib_dev_flush, priv->dev);
904 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task, priv->dev);
905 INIT_WORK(&priv->ah_reap_task, ipoib_reap_ah, priv->dev);
908 struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
910 struct net_device *dev;
912 dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
917 return netdev_priv(dev);
920 static ssize_t show_pkey(struct class_device *cdev, char *buf)
922 struct ipoib_dev_priv *priv =
923 netdev_priv(container_of(cdev, struct net_device, class_dev));
925 return sprintf(buf, "0x%04x\n", priv->pkey);
927 static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
929 static ssize_t create_child(struct class_device *cdev,
930 const char *buf, size_t count)
935 if (sscanf(buf, "%i", &pkey) != 1)
938 if (pkey < 0 || pkey > 0xffff)
942 * Set the full membership bit, so that we join the right
943 * broadcast group, etc.
947 ret = ipoib_vlan_add(container_of(cdev, struct net_device, class_dev),
950 return ret ? ret : count;
952 static CLASS_DEVICE_ATTR(create_child, S_IWUGO, NULL, create_child);
954 static ssize_t delete_child(struct class_device *cdev,
955 const char *buf, size_t count)
960 if (sscanf(buf, "%i", &pkey) != 1)
963 if (pkey < 0 || pkey > 0xffff)
966 ret = ipoib_vlan_delete(container_of(cdev, struct net_device, class_dev),
969 return ret ? ret : count;
972 static CLASS_DEVICE_ATTR(delete_child, S_IWUGO, NULL, delete_child);
974 int ipoib_add_pkey_attr(struct net_device *dev)
976 return class_device_create_file(&dev->class_dev,
977 &class_device_attr_pkey);
980 static struct net_device *ipoib_add_port(const char *format,
981 struct ib_device *hca, u8 port)
983 struct ipoib_dev_priv *priv;
984 int result = -ENOMEM;
986 priv = ipoib_intf_alloc(format);
988 goto alloc_mem_failed;
990 SET_NETDEV_DEV(priv->dev, hca->dma_device);
992 result = ib_query_pkey(hca, port, 0, &priv->pkey);
994 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
995 hca->name, port, result);
996 goto alloc_mem_failed;
1000 * Set the full membership bit, so that we join the right
1001 * broadcast group, etc.
1003 priv->pkey |= 0x8000;
1005 priv->dev->broadcast[8] = priv->pkey >> 8;
1006 priv->dev->broadcast[9] = priv->pkey & 0xff;
1008 result = ib_query_gid(hca, port, 0, &priv->local_gid);
1010 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1011 hca->name, port, result);
1012 goto alloc_mem_failed;
1014 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1017 result = ipoib_dev_init(priv->dev, hca, port);
1019 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1020 hca->name, port, result);
1021 goto device_init_failed;
1024 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1025 priv->ca, ipoib_event);
1026 result = ib_register_event_handler(&priv->event_handler);
1028 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1029 "port %d (ret = %d)\n",
1030 hca->name, port, result);
1034 result = register_netdev(priv->dev);
1036 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1037 hca->name, port, result);
1038 goto register_failed;
1041 ipoib_create_debug_files(priv->dev);
1043 if (ipoib_add_pkey_attr(priv->dev))
1045 if (class_device_create_file(&priv->dev->class_dev,
1046 &class_device_attr_create_child))
1048 if (class_device_create_file(&priv->dev->class_dev,
1049 &class_device_attr_delete_child))
1055 ipoib_delete_debug_files(priv->dev);
1056 unregister_netdev(priv->dev);
1059 ib_unregister_event_handler(&priv->event_handler);
1060 flush_scheduled_work();
1063 ipoib_dev_cleanup(priv->dev);
1066 free_netdev(priv->dev);
1069 return ERR_PTR(result);
1072 static void ipoib_add_one(struct ib_device *device)
1074 struct list_head *dev_list;
1075 struct net_device *dev;
1076 struct ipoib_dev_priv *priv;
1079 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1083 INIT_LIST_HEAD(dev_list);
1085 if (device->node_type == IB_NODE_SWITCH) {
1090 e = device->phys_port_cnt;
1093 for (p = s; p <= e; ++p) {
1094 dev = ipoib_add_port("ib%d", device, p);
1096 priv = netdev_priv(dev);
1097 list_add_tail(&priv->list, dev_list);
1101 ib_set_client_data(device, &ipoib_client, dev_list);
1104 static void ipoib_remove_one(struct ib_device *device)
1106 struct ipoib_dev_priv *priv, *tmp;
1107 struct list_head *dev_list;
1109 dev_list = ib_get_client_data(device, &ipoib_client);
1111 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1112 ib_unregister_event_handler(&priv->event_handler);
1113 flush_scheduled_work();
1115 unregister_netdev(priv->dev);
1116 ipoib_dev_cleanup(priv->dev);
1117 free_netdev(priv->dev);
1123 static int __init ipoib_init_module(void)
1127 ret = ipoib_register_debugfs();
1132 * We create our own workqueue mainly because we want to be
1133 * able to flush it when devices are being removed. We can't
1134 * use schedule_work()/flush_scheduled_work() because both
1135 * unregister_netdev() and linkwatch_event take the rtnl lock,
1136 * so flush_scheduled_work() can deadlock during device
1139 ipoib_workqueue = create_singlethread_workqueue("ipoib");
1140 if (!ipoib_workqueue) {
1145 ret = ib_register_client(&ipoib_client);
1152 destroy_workqueue(ipoib_workqueue);
1155 ipoib_unregister_debugfs();
1160 static void __exit ipoib_cleanup_module(void)
1162 ib_unregister_client(&ipoib_client);
1163 ipoib_unregister_debugfs();
1164 destroy_workqueue(ipoib_workqueue);
1167 module_init(ipoib_init_module);
1168 module_exit(ipoib_cleanup_module);