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/version.h>
40 #include <linux/module.h>
42 #include <linux/init.h>
43 #include <linux/slab.h>
44 #include <linux/vmalloc.h>
46 #include <linux/if_arp.h> /* For ARPHRD_xxx */
51 MODULE_AUTHOR("Roland Dreier");
52 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
53 MODULE_LICENSE("Dual BSD/GPL");
55 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
56 int ipoib_debug_level;
58 module_param_named(debug_level, ipoib_debug_level, int, 0644);
59 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
62 static const u8 ipv4_bcast_addr[] = {
63 0x00, 0xff, 0xff, 0xff,
64 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
68 struct workqueue_struct *ipoib_workqueue;
70 static void ipoib_add_one(struct ib_device *device);
71 static void ipoib_remove_one(struct ib_device *device);
73 static struct ib_client ipoib_client = {
76 .remove = ipoib_remove_one
79 int ipoib_open(struct net_device *dev)
81 struct ipoib_dev_priv *priv = netdev_priv(dev);
83 ipoib_dbg(priv, "bringing up interface\n");
85 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
87 if (ipoib_pkey_dev_delay_open(dev))
90 if (ipoib_ib_dev_open(dev))
93 if (ipoib_ib_dev_up(dev))
96 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
97 struct ipoib_dev_priv *cpriv;
99 /* Bring up any child interfaces too */
100 down(&priv->vlan_mutex);
101 list_for_each_entry(cpriv, &priv->child_intfs, list) {
104 flags = cpriv->dev->flags;
108 dev_change_flags(cpriv->dev, flags | IFF_UP);
110 up(&priv->vlan_mutex);
113 netif_start_queue(dev);
118 static int ipoib_stop(struct net_device *dev)
120 struct ipoib_dev_priv *priv = netdev_priv(dev);
122 ipoib_dbg(priv, "stopping interface\n");
124 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
126 netif_stop_queue(dev);
128 ipoib_ib_dev_down(dev);
129 ipoib_ib_dev_stop(dev);
131 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
132 struct ipoib_dev_priv *cpriv;
134 /* Bring down any child interfaces too */
135 down(&priv->vlan_mutex);
136 list_for_each_entry(cpriv, &priv->child_intfs, list) {
139 flags = cpriv->dev->flags;
140 if (!(flags & IFF_UP))
143 dev_change_flags(cpriv->dev, flags & ~IFF_UP);
145 up(&priv->vlan_mutex);
151 static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
153 struct ipoib_dev_priv *priv = netdev_priv(dev);
155 if (new_mtu > IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN)
158 priv->admin_mtu = new_mtu;
160 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
165 static struct ipoib_path *__path_find(struct net_device *dev,
168 struct ipoib_dev_priv *priv = netdev_priv(dev);
169 struct rb_node *n = priv->path_tree.rb_node;
170 struct ipoib_path *path;
174 path = rb_entry(n, struct ipoib_path, rb_node);
176 ret = memcmp(gid->raw, path->pathrec.dgid.raw,
177 sizeof (union ib_gid));
190 static int __path_add(struct net_device *dev, struct ipoib_path *path)
192 struct ipoib_dev_priv *priv = netdev_priv(dev);
193 struct rb_node **n = &priv->path_tree.rb_node;
194 struct rb_node *pn = NULL;
195 struct ipoib_path *tpath;
200 tpath = rb_entry(pn, struct ipoib_path, rb_node);
202 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
203 sizeof (union ib_gid));
212 rb_link_node(&path->rb_node, pn, n);
213 rb_insert_color(&path->rb_node, &priv->path_tree);
215 list_add_tail(&path->list, &priv->path_list);
220 static void path_free(struct net_device *dev, struct ipoib_path *path)
222 struct ipoib_dev_priv *priv = netdev_priv(dev);
223 struct ipoib_neigh *neigh, *tn;
227 while ((skb = __skb_dequeue(&path->queue)))
228 dev_kfree_skb_irq(skb);
230 spin_lock_irqsave(&priv->lock, flags);
232 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
234 * It's safe to call ipoib_put_ah() inside priv->lock
235 * here, because we know that path->ah will always
236 * hold one more reference, so ipoib_put_ah() will
237 * never do more than decrement the ref count.
240 ipoib_put_ah(neigh->ah);
241 *to_ipoib_neigh(neigh->neighbour) = NULL;
242 neigh->neighbour->ops->destructor = NULL;
246 spin_unlock_irqrestore(&priv->lock, flags);
249 ipoib_put_ah(path->ah);
254 void ipoib_flush_paths(struct net_device *dev)
256 struct ipoib_dev_priv *priv = netdev_priv(dev);
257 struct ipoib_path *path, *tp;
258 LIST_HEAD(remove_list);
261 spin_lock_irqsave(&priv->lock, flags);
263 list_splice(&priv->path_list, &remove_list);
264 INIT_LIST_HEAD(&priv->path_list);
266 list_for_each_entry(path, &remove_list, list)
267 rb_erase(&path->rb_node, &priv->path_tree);
269 spin_unlock_irqrestore(&priv->lock, flags);
271 list_for_each_entry_safe(path, tp, &remove_list, list) {
273 ib_sa_cancel_query(path->query_id, path->query);
274 wait_for_completion(&path->done);
275 path_free(dev, path);
279 static void path_rec_completion(int status,
280 struct ib_sa_path_rec *pathrec,
283 struct ipoib_path *path = path_ptr;
284 struct net_device *dev = path->dev;
285 struct ipoib_dev_priv *priv = netdev_priv(dev);
286 struct ipoib_ah *ah = NULL;
287 struct ipoib_neigh *neigh;
288 struct sk_buff_head skqueue;
293 ipoib_dbg(priv, "PathRec LID 0x%04x for GID " IPOIB_GID_FMT "\n",
294 be16_to_cpu(pathrec->dlid), IPOIB_GID_ARG(pathrec->dgid));
296 ipoib_dbg(priv, "PathRec status %d for GID " IPOIB_GID_FMT "\n",
297 status, IPOIB_GID_ARG(path->pathrec.dgid));
299 skb_queue_head_init(&skqueue);
302 struct ib_ah_attr av = {
303 .dlid = be16_to_cpu(pathrec->dlid),
305 .port_num = priv->port
307 int path_rate = ib_sa_rate_enum_to_int(pathrec->rate);
309 if (path_rate > 0 && priv->local_rate > path_rate)
310 av.static_rate = (priv->local_rate - 1) / path_rate;
312 ipoib_dbg(priv, "static_rate %d for local port %dX, path %dX\n",
313 av.static_rate, priv->local_rate,
314 ib_sa_rate_enum_to_int(pathrec->rate));
316 ah = ipoib_create_ah(dev, priv->pd, &av);
319 spin_lock_irqsave(&priv->lock, flags);
324 path->pathrec = *pathrec;
326 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
327 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
329 while ((skb = __skb_dequeue(&path->queue)))
330 __skb_queue_tail(&skqueue, skb);
332 list_for_each_entry(neigh, &path->neigh_list, list) {
333 kref_get(&path->ah->ref);
334 neigh->ah = path->ah;
336 while ((skb = __skb_dequeue(&neigh->queue)))
337 __skb_queue_tail(&skqueue, skb);
342 complete(&path->done);
344 spin_unlock_irqrestore(&priv->lock, flags);
346 while ((skb = __skb_dequeue(&skqueue))) {
348 if (dev_queue_xmit(skb))
349 ipoib_warn(priv, "dev_queue_xmit failed "
350 "to requeue packet\n");
354 static struct ipoib_path *path_rec_create(struct net_device *dev,
357 struct ipoib_dev_priv *priv = netdev_priv(dev);
358 struct ipoib_path *path;
360 path = kmalloc(sizeof *path, GFP_ATOMIC);
365 path->pathrec.dlid = 0;
368 skb_queue_head_init(&path->queue);
370 INIT_LIST_HEAD(&path->neigh_list);
372 init_completion(&path->done);
374 memcpy(path->pathrec.dgid.raw, gid->raw, sizeof (union ib_gid));
375 path->pathrec.sgid = priv->local_gid;
376 path->pathrec.pkey = cpu_to_be16(priv->pkey);
377 path->pathrec.numb_path = 1;
382 static int path_rec_start(struct net_device *dev,
383 struct ipoib_path *path)
385 struct ipoib_dev_priv *priv = netdev_priv(dev);
387 ipoib_dbg(priv, "Start path record lookup for " IPOIB_GID_FMT "\n",
388 IPOIB_GID_ARG(path->pathrec.dgid));
391 ib_sa_path_rec_get(priv->ca, priv->port,
393 IB_SA_PATH_REC_DGID |
394 IB_SA_PATH_REC_SGID |
395 IB_SA_PATH_REC_NUMB_PATH |
400 if (path->query_id < 0) {
401 ipoib_warn(priv, "ib_sa_path_rec_get failed\n");
403 return path->query_id;
409 static void neigh_add_path(struct sk_buff *skb, struct net_device *dev)
411 struct ipoib_dev_priv *priv = netdev_priv(dev);
412 struct ipoib_path *path;
413 struct ipoib_neigh *neigh;
415 neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
417 ++priv->stats.tx_dropped;
418 dev_kfree_skb_any(skb);
422 skb_queue_head_init(&neigh->queue);
423 neigh->neighbour = skb->dst->neighbour;
424 *to_ipoib_neigh(skb->dst->neighbour) = neigh;
427 * We can only be called from ipoib_start_xmit, so we're
428 * inside tx_lock -- no need to save/restore flags.
430 spin_lock(&priv->lock);
432 path = __path_find(dev, (union ib_gid *) (skb->dst->neighbour->ha + 4));
434 path = path_rec_create(dev,
435 (union ib_gid *) (skb->dst->neighbour->ha + 4));
439 __path_add(dev, path);
442 list_add_tail(&neigh->list, &path->neigh_list);
444 if (path->pathrec.dlid) {
445 kref_get(&path->ah->ref);
446 neigh->ah = path->ah;
448 ipoib_send(dev, skb, path->ah,
449 be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
452 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
453 __skb_queue_tail(&neigh->queue, skb);
455 ++priv->stats.tx_dropped;
456 dev_kfree_skb_any(skb);
459 if (!path->query && path_rec_start(dev, path))
463 spin_unlock(&priv->lock);
467 *to_ipoib_neigh(skb->dst->neighbour) = NULL;
468 list_del(&neigh->list);
469 neigh->neighbour->ops->destructor = NULL;
472 ++priv->stats.tx_dropped;
473 dev_kfree_skb_any(skb);
475 spin_unlock(&priv->lock);
478 static void path_lookup(struct sk_buff *skb, struct net_device *dev)
480 struct ipoib_dev_priv *priv = netdev_priv(skb->dev);
482 /* Look up path record for unicasts */
483 if (skb->dst->neighbour->ha[4] != 0xff) {
484 neigh_add_path(skb, dev);
488 /* Add in the P_Key for multicasts */
489 skb->dst->neighbour->ha[8] = (priv->pkey >> 8) & 0xff;
490 skb->dst->neighbour->ha[9] = priv->pkey & 0xff;
491 ipoib_mcast_send(dev, (union ib_gid *) (skb->dst->neighbour->ha + 4), skb);
494 static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
495 struct ipoib_pseudoheader *phdr)
497 struct ipoib_dev_priv *priv = netdev_priv(dev);
498 struct ipoib_path *path;
501 * We can only be called from ipoib_start_xmit, so we're
502 * inside tx_lock -- no need to save/restore flags.
504 spin_lock(&priv->lock);
506 path = __path_find(dev, (union ib_gid *) (phdr->hwaddr + 4));
508 path = path_rec_create(dev,
509 (union ib_gid *) (phdr->hwaddr + 4));
511 /* put pseudoheader back on for next time */
512 skb_push(skb, sizeof *phdr);
513 __skb_queue_tail(&path->queue, skb);
515 if (path_rec_start(dev, path)) {
516 spin_unlock(&priv->lock);
517 path_free(dev, path);
520 __path_add(dev, path);
522 ++priv->stats.tx_dropped;
523 dev_kfree_skb_any(skb);
526 spin_unlock(&priv->lock);
530 if (path->pathrec.dlid) {
531 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
532 be16_to_cpu(path->pathrec.dlid));
534 ipoib_send(dev, skb, path->ah,
535 be32_to_cpup((__be32 *) phdr->hwaddr));
536 } else if ((path->query || !path_rec_start(dev, path)) &&
537 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
538 /* put pseudoheader back on for next time */
539 skb_push(skb, sizeof *phdr);
540 __skb_queue_tail(&path->queue, skb);
542 ++priv->stats.tx_dropped;
543 dev_kfree_skb_any(skb);
546 spin_unlock(&priv->lock);
549 static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
551 struct ipoib_dev_priv *priv = netdev_priv(dev);
552 struct ipoib_neigh *neigh;
555 local_irq_save(flags);
556 if (!spin_trylock(&priv->tx_lock)) {
557 local_irq_restore(flags);
558 return NETDEV_TX_LOCKED;
562 * Check if our queue is stopped. Since we have the LLTX bit
563 * set, we can't rely on netif_stop_queue() preventing our
564 * xmit function from being called with a full queue.
566 if (unlikely(netif_queue_stopped(dev))) {
567 spin_unlock_irqrestore(&priv->tx_lock, flags);
568 return NETDEV_TX_BUSY;
571 if (skb->dst && skb->dst->neighbour) {
572 if (unlikely(!*to_ipoib_neigh(skb->dst->neighbour))) {
573 path_lookup(skb, dev);
577 neigh = *to_ipoib_neigh(skb->dst->neighbour);
579 if (likely(neigh->ah)) {
580 ipoib_send(dev, skb, neigh->ah,
581 be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
585 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
586 spin_lock(&priv->lock);
587 __skb_queue_tail(&neigh->queue, skb);
588 spin_unlock(&priv->lock);
590 ++priv->stats.tx_dropped;
591 dev_kfree_skb_any(skb);
594 struct ipoib_pseudoheader *phdr =
595 (struct ipoib_pseudoheader *) skb->data;
596 skb_pull(skb, sizeof *phdr);
598 if (phdr->hwaddr[4] == 0xff) {
599 /* Add in the P_Key for multicast*/
600 phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
601 phdr->hwaddr[9] = priv->pkey & 0xff;
603 ipoib_mcast_send(dev, (union ib_gid *) (phdr->hwaddr + 4), skb);
605 /* unicast GID -- should be ARP or RARP reply */
607 if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
608 (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
609 ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x "
611 skb->dst ? "neigh" : "dst",
612 be16_to_cpup((u16 *) skb->data),
613 be32_to_cpup((u32 *) phdr->hwaddr),
614 IPOIB_GID_ARG(*(union ib_gid *) (phdr->hwaddr + 4)));
615 dev_kfree_skb_any(skb);
616 ++priv->stats.tx_dropped;
620 unicast_arp_send(skb, dev, phdr);
625 spin_unlock_irqrestore(&priv->tx_lock, flags);
630 static struct net_device_stats *ipoib_get_stats(struct net_device *dev)
632 struct ipoib_dev_priv *priv = netdev_priv(dev);
637 static void ipoib_timeout(struct net_device *dev)
639 struct ipoib_dev_priv *priv = netdev_priv(dev);
641 ipoib_warn(priv, "transmit timeout: latency %ld\n",
642 jiffies - dev->trans_start);
643 /* XXX reset QP, etc. */
646 static int ipoib_hard_header(struct sk_buff *skb,
647 struct net_device *dev,
649 void *daddr, void *saddr, unsigned len)
651 struct ipoib_header *header;
653 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
655 header->proto = htons(type);
656 header->reserved = 0;
659 * If we don't have a neighbour structure, stuff the
660 * destination address onto the front of the skb so we can
661 * figure out where to send the packet later.
663 if (!skb->dst || !skb->dst->neighbour) {
664 struct ipoib_pseudoheader *phdr =
665 (struct ipoib_pseudoheader *) skb_push(skb, sizeof *phdr);
666 memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
672 static void ipoib_set_mcast_list(struct net_device *dev)
674 struct ipoib_dev_priv *priv = netdev_priv(dev);
676 schedule_work(&priv->restart_task);
679 static void ipoib_neigh_destructor(struct neighbour *n)
681 struct ipoib_neigh *neigh;
682 struct ipoib_dev_priv *priv = netdev_priv(n->dev);
684 struct ipoib_ah *ah = NULL;
687 "neigh_destructor for %06x " IPOIB_GID_FMT "\n",
688 be32_to_cpup((__be32 *) n->ha),
689 IPOIB_GID_ARG(*((union ib_gid *) (n->ha + 4))));
691 spin_lock_irqsave(&priv->lock, flags);
693 neigh = *to_ipoib_neigh(n);
697 list_del(&neigh->list);
698 *to_ipoib_neigh(n) = NULL;
702 spin_unlock_irqrestore(&priv->lock, flags);
708 static int ipoib_neigh_setup(struct neighbour *neigh)
711 * Is this kosher? I can't find anybody in the kernel that
712 * sets neigh->destructor, so we should be able to set it here
715 neigh->ops->destructor = ipoib_neigh_destructor;
720 static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
722 parms->neigh_setup = ipoib_neigh_setup;
727 int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
729 struct ipoib_dev_priv *priv = netdev_priv(dev);
731 /* Allocate RX/TX "rings" to hold queued skbs */
733 priv->rx_ring = kmalloc(IPOIB_RX_RING_SIZE * sizeof (struct ipoib_buf),
735 if (!priv->rx_ring) {
736 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
737 ca->name, IPOIB_RX_RING_SIZE);
740 memset(priv->rx_ring, 0,
741 IPOIB_RX_RING_SIZE * sizeof (struct ipoib_buf));
743 priv->tx_ring = kmalloc(IPOIB_TX_RING_SIZE * sizeof (struct ipoib_buf),
745 if (!priv->tx_ring) {
746 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
747 ca->name, IPOIB_TX_RING_SIZE);
748 goto out_rx_ring_cleanup;
750 memset(priv->tx_ring, 0,
751 IPOIB_TX_RING_SIZE * sizeof (struct ipoib_buf));
753 /* priv->tx_head & tx_tail are already 0 */
755 if (ipoib_ib_dev_init(dev, ca, port))
756 goto out_tx_ring_cleanup;
761 kfree(priv->tx_ring);
764 kfree(priv->rx_ring);
770 void ipoib_dev_cleanup(struct net_device *dev)
772 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
774 ipoib_delete_debug_file(dev);
776 /* Delete any child interfaces first */
777 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
778 unregister_netdev(cpriv->dev);
779 ipoib_dev_cleanup(cpriv->dev);
780 free_netdev(cpriv->dev);
783 ipoib_ib_dev_cleanup(dev);
786 kfree(priv->rx_ring);
787 priv->rx_ring = NULL;
791 kfree(priv->tx_ring);
792 priv->tx_ring = NULL;
796 static void ipoib_setup(struct net_device *dev)
798 struct ipoib_dev_priv *priv = netdev_priv(dev);
800 dev->open = ipoib_open;
801 dev->stop = ipoib_stop;
802 dev->change_mtu = ipoib_change_mtu;
803 dev->hard_start_xmit = ipoib_start_xmit;
804 dev->get_stats = ipoib_get_stats;
805 dev->tx_timeout = ipoib_timeout;
806 dev->hard_header = ipoib_hard_header;
807 dev->set_multicast_list = ipoib_set_mcast_list;
808 dev->neigh_setup = ipoib_neigh_setup_dev;
810 dev->watchdog_timeo = HZ;
812 dev->rebuild_header = NULL;
813 dev->set_mac_address = NULL;
814 dev->header_cache_update = NULL;
816 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
819 * We add in INFINIBAND_ALEN to allow for the destination
820 * address "pseudoheader" for skbs without neighbour struct.
822 dev->hard_header_len = IPOIB_ENCAP_LEN + INFINIBAND_ALEN;
823 dev->addr_len = INFINIBAND_ALEN;
824 dev->type = ARPHRD_INFINIBAND;
825 dev->tx_queue_len = IPOIB_TX_RING_SIZE * 2;
826 dev->features = NETIF_F_VLAN_CHALLENGED | NETIF_F_LLTX;
828 /* MTU will be reset when mcast join happens */
829 dev->mtu = IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN;
830 priv->mcast_mtu = priv->admin_mtu = dev->mtu;
832 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
834 netif_carrier_off(dev);
836 SET_MODULE_OWNER(dev);
840 spin_lock_init(&priv->lock);
841 spin_lock_init(&priv->tx_lock);
843 init_MUTEX(&priv->mcast_mutex);
844 init_MUTEX(&priv->vlan_mutex);
846 INIT_LIST_HEAD(&priv->path_list);
847 INIT_LIST_HEAD(&priv->child_intfs);
848 INIT_LIST_HEAD(&priv->dead_ahs);
849 INIT_LIST_HEAD(&priv->multicast_list);
851 INIT_WORK(&priv->pkey_task, ipoib_pkey_poll, priv->dev);
852 INIT_WORK(&priv->mcast_task, ipoib_mcast_join_task, priv->dev);
853 INIT_WORK(&priv->flush_task, ipoib_ib_dev_flush, priv->dev);
854 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task, priv->dev);
855 INIT_WORK(&priv->ah_reap_task, ipoib_reap_ah, priv->dev);
858 struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
860 struct net_device *dev;
862 dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
867 return netdev_priv(dev);
870 static ssize_t show_pkey(struct class_device *cdev, char *buf)
872 struct ipoib_dev_priv *priv =
873 netdev_priv(container_of(cdev, struct net_device, class_dev));
875 return sprintf(buf, "0x%04x\n", priv->pkey);
877 static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
879 static ssize_t create_child(struct class_device *cdev,
880 const char *buf, size_t count)
885 if (sscanf(buf, "%i", &pkey) != 1)
888 if (pkey < 0 || pkey > 0xffff)
891 ret = ipoib_vlan_add(container_of(cdev, struct net_device, class_dev),
894 return ret ? ret : count;
896 static CLASS_DEVICE_ATTR(create_child, S_IWUGO, NULL, create_child);
898 static ssize_t delete_child(struct class_device *cdev,
899 const char *buf, size_t count)
904 if (sscanf(buf, "%i", &pkey) != 1)
907 if (pkey < 0 || pkey > 0xffff)
910 ret = ipoib_vlan_delete(container_of(cdev, struct net_device, class_dev),
913 return ret ? ret : count;
916 static CLASS_DEVICE_ATTR(delete_child, S_IWUGO, NULL, delete_child);
918 int ipoib_add_pkey_attr(struct net_device *dev)
920 return class_device_create_file(&dev->class_dev,
921 &class_device_attr_pkey);
924 static struct net_device *ipoib_add_port(const char *format,
925 struct ib_device *hca, u8 port)
927 struct ipoib_dev_priv *priv;
928 int result = -ENOMEM;
930 priv = ipoib_intf_alloc(format);
932 goto alloc_mem_failed;
934 SET_NETDEV_DEV(priv->dev, hca->dma_device);
936 result = ib_query_pkey(hca, port, 0, &priv->pkey);
938 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
939 hca->name, port, result);
940 goto alloc_mem_failed;
943 priv->dev->broadcast[8] = priv->pkey >> 8;
944 priv->dev->broadcast[9] = priv->pkey & 0xff;
946 result = ib_query_gid(hca, port, 0, &priv->local_gid);
948 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
949 hca->name, port, result);
950 goto alloc_mem_failed;
952 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
955 result = ipoib_dev_init(priv->dev, hca, port);
957 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
958 hca->name, port, result);
959 goto device_init_failed;
962 INIT_IB_EVENT_HANDLER(&priv->event_handler,
963 priv->ca, ipoib_event);
964 result = ib_register_event_handler(&priv->event_handler);
966 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
967 "port %d (ret = %d)\n",
968 hca->name, port, result);
972 result = register_netdev(priv->dev);
974 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
975 hca->name, port, result);
976 goto register_failed;
979 if (ipoib_create_debug_file(priv->dev))
982 if (ipoib_add_pkey_attr(priv->dev))
984 if (class_device_create_file(&priv->dev->class_dev,
985 &class_device_attr_create_child))
987 if (class_device_create_file(&priv->dev->class_dev,
988 &class_device_attr_delete_child))
994 ipoib_delete_debug_file(priv->dev);
997 unregister_netdev(priv->dev);
1000 ib_unregister_event_handler(&priv->event_handler);
1003 ipoib_dev_cleanup(priv->dev);
1006 free_netdev(priv->dev);
1009 return ERR_PTR(result);
1012 static void ipoib_add_one(struct ib_device *device)
1014 struct list_head *dev_list;
1015 struct net_device *dev;
1016 struct ipoib_dev_priv *priv;
1019 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1023 INIT_LIST_HEAD(dev_list);
1025 if (device->node_type == IB_NODE_SWITCH) {
1030 e = device->phys_port_cnt;
1033 for (p = s; p <= e; ++p) {
1034 dev = ipoib_add_port("ib%d", device, p);
1036 priv = netdev_priv(dev);
1037 list_add_tail(&priv->list, dev_list);
1041 ib_set_client_data(device, &ipoib_client, dev_list);
1044 static void ipoib_remove_one(struct ib_device *device)
1046 struct ipoib_dev_priv *priv, *tmp;
1047 struct list_head *dev_list;
1049 dev_list = ib_get_client_data(device, &ipoib_client);
1051 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1052 ib_unregister_event_handler(&priv->event_handler);
1054 unregister_netdev(priv->dev);
1055 ipoib_dev_cleanup(priv->dev);
1056 free_netdev(priv->dev);
1060 static int __init ipoib_init_module(void)
1064 ret = ipoib_register_debugfs();
1069 * We create our own workqueue mainly because we want to be
1070 * able to flush it when devices are being removed. We can't
1071 * use schedule_work()/flush_scheduled_work() because both
1072 * unregister_netdev() and linkwatch_event take the rtnl lock,
1073 * so flush_scheduled_work() can deadlock during device
1076 ipoib_workqueue = create_singlethread_workqueue("ipoib");
1077 if (!ipoib_workqueue) {
1082 ret = ib_register_client(&ipoib_client);
1089 destroy_workqueue(ipoib_workqueue);
1092 ipoib_unregister_debugfs();
1097 static void __exit ipoib_cleanup_module(void)
1099 ib_unregister_client(&ipoib_client);
1100 ipoib_unregister_debugfs();
1101 destroy_workqueue(ipoib_workqueue);
1104 module_init(ipoib_init_module);
1105 module_exit(ipoib_cleanup_module);