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>
44 #include <linux/kernel.h>
46 #include <linux/if_arp.h> /* For ARPHRD_xxx */
53 MODULE_AUTHOR("Roland Dreier");
54 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
55 MODULE_LICENSE("Dual BSD/GPL");
57 int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
58 int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
60 module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
61 MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
62 module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
63 MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
65 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
66 int ipoib_debug_level;
68 module_param_named(debug_level, ipoib_debug_level, int, 0644);
69 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
72 struct ipoib_path_iter {
73 struct net_device *dev;
74 struct ipoib_path path;
77 static const u8 ipv4_bcast_addr[] = {
78 0x00, 0xff, 0xff, 0xff,
79 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
80 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
83 struct workqueue_struct *ipoib_workqueue;
85 struct ib_sa_client ipoib_sa_client;
87 static void ipoib_add_one(struct ib_device *device);
88 static void ipoib_remove_one(struct ib_device *device);
90 static struct ib_client ipoib_client = {
93 .remove = ipoib_remove_one
96 int ipoib_open(struct net_device *dev)
98 struct ipoib_dev_priv *priv = netdev_priv(dev);
100 ipoib_dbg(priv, "bringing up interface\n");
102 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
104 if (ipoib_pkey_dev_delay_open(dev))
107 if (ipoib_ib_dev_open(dev))
110 if (ipoib_ib_dev_up(dev)) {
111 ipoib_ib_dev_stop(dev);
115 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
116 struct ipoib_dev_priv *cpriv;
118 /* Bring up any child interfaces too */
119 mutex_lock(&priv->vlan_mutex);
120 list_for_each_entry(cpriv, &priv->child_intfs, list) {
123 flags = cpriv->dev->flags;
127 dev_change_flags(cpriv->dev, flags | IFF_UP);
129 mutex_unlock(&priv->vlan_mutex);
132 netif_start_queue(dev);
137 static int ipoib_stop(struct net_device *dev)
139 struct ipoib_dev_priv *priv = netdev_priv(dev);
141 ipoib_dbg(priv, "stopping interface\n");
143 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
145 netif_stop_queue(dev);
148 * Now flush workqueue to make sure a scheduled task doesn't
149 * bring our internal state back up.
151 flush_workqueue(ipoib_workqueue);
153 ipoib_ib_dev_down(dev, 1);
154 ipoib_ib_dev_stop(dev);
156 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
157 struct ipoib_dev_priv *cpriv;
159 /* Bring down any child interfaces too */
160 mutex_lock(&priv->vlan_mutex);
161 list_for_each_entry(cpriv, &priv->child_intfs, list) {
164 flags = cpriv->dev->flags;
165 if (!(flags & IFF_UP))
168 dev_change_flags(cpriv->dev, flags & ~IFF_UP);
170 mutex_unlock(&priv->vlan_mutex);
176 static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
178 struct ipoib_dev_priv *priv = netdev_priv(dev);
180 if (new_mtu > IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN)
183 priv->admin_mtu = new_mtu;
185 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
190 static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
192 struct ipoib_dev_priv *priv = netdev_priv(dev);
193 struct rb_node *n = priv->path_tree.rb_node;
194 struct ipoib_path *path;
198 path = rb_entry(n, struct ipoib_path, rb_node);
200 ret = memcmp(gid, path->pathrec.dgid.raw,
201 sizeof (union ib_gid));
214 static int __path_add(struct net_device *dev, struct ipoib_path *path)
216 struct ipoib_dev_priv *priv = netdev_priv(dev);
217 struct rb_node **n = &priv->path_tree.rb_node;
218 struct rb_node *pn = NULL;
219 struct ipoib_path *tpath;
224 tpath = rb_entry(pn, struct ipoib_path, rb_node);
226 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
227 sizeof (union ib_gid));
236 rb_link_node(&path->rb_node, pn, n);
237 rb_insert_color(&path->rb_node, &priv->path_tree);
239 list_add_tail(&path->list, &priv->path_list);
244 static void path_free(struct net_device *dev, struct ipoib_path *path)
246 struct ipoib_dev_priv *priv = netdev_priv(dev);
247 struct ipoib_neigh *neigh, *tn;
251 while ((skb = __skb_dequeue(&path->queue)))
252 dev_kfree_skb_irq(skb);
254 spin_lock_irqsave(&priv->lock, flags);
256 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
258 * It's safe to call ipoib_put_ah() inside priv->lock
259 * here, because we know that path->ah will always
260 * hold one more reference, so ipoib_put_ah() will
261 * never do more than decrement the ref count.
264 ipoib_put_ah(neigh->ah);
266 ipoib_neigh_free(neigh);
269 spin_unlock_irqrestore(&priv->lock, flags);
272 ipoib_put_ah(path->ah);
277 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
279 struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
281 struct ipoib_path_iter *iter;
283 iter = kmalloc(sizeof *iter, GFP_KERNEL);
288 memset(iter->path.pathrec.dgid.raw, 0, 16);
290 if (ipoib_path_iter_next(iter)) {
298 int ipoib_path_iter_next(struct ipoib_path_iter *iter)
300 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
302 struct ipoib_path *path;
305 spin_lock_irq(&priv->lock);
307 n = rb_first(&priv->path_tree);
310 path = rb_entry(n, struct ipoib_path, rb_node);
312 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
313 sizeof (union ib_gid)) < 0) {
322 spin_unlock_irq(&priv->lock);
327 void ipoib_path_iter_read(struct ipoib_path_iter *iter,
328 struct ipoib_path *path)
333 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
335 void ipoib_flush_paths(struct net_device *dev)
337 struct ipoib_dev_priv *priv = netdev_priv(dev);
338 struct ipoib_path *path, *tp;
339 LIST_HEAD(remove_list);
341 spin_lock_irq(&priv->tx_lock);
342 spin_lock(&priv->lock);
344 list_splice(&priv->path_list, &remove_list);
345 INIT_LIST_HEAD(&priv->path_list);
347 list_for_each_entry(path, &remove_list, list)
348 rb_erase(&path->rb_node, &priv->path_tree);
350 list_for_each_entry_safe(path, tp, &remove_list, list) {
352 ib_sa_cancel_query(path->query_id, path->query);
353 spin_unlock(&priv->lock);
354 spin_unlock_irq(&priv->tx_lock);
355 wait_for_completion(&path->done);
356 path_free(dev, path);
357 spin_lock_irq(&priv->tx_lock);
358 spin_lock(&priv->lock);
360 spin_unlock(&priv->lock);
361 spin_unlock_irq(&priv->tx_lock);
364 static void path_rec_completion(int status,
365 struct ib_sa_path_rec *pathrec,
368 struct ipoib_path *path = path_ptr;
369 struct net_device *dev = path->dev;
370 struct ipoib_dev_priv *priv = netdev_priv(dev);
371 struct ipoib_ah *ah = NULL;
372 struct ipoib_neigh *neigh;
373 struct sk_buff_head skqueue;
378 ipoib_dbg(priv, "PathRec LID 0x%04x for GID " IPOIB_GID_FMT "\n",
379 be16_to_cpu(pathrec->dlid), IPOIB_GID_ARG(pathrec->dgid));
381 ipoib_dbg(priv, "PathRec status %d for GID " IPOIB_GID_FMT "\n",
382 status, IPOIB_GID_ARG(path->pathrec.dgid));
384 skb_queue_head_init(&skqueue);
387 struct ib_ah_attr av = {
388 .dlid = be16_to_cpu(pathrec->dlid),
390 .port_num = priv->port,
391 .static_rate = pathrec->rate
394 ah = ipoib_create_ah(dev, priv->pd, &av);
397 spin_lock_irqsave(&priv->lock, flags);
402 path->pathrec = *pathrec;
404 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
405 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
407 while ((skb = __skb_dequeue(&path->queue)))
408 __skb_queue_tail(&skqueue, skb);
410 list_for_each_entry(neigh, &path->neigh_list, list) {
411 kref_get(&path->ah->ref);
412 neigh->ah = path->ah;
413 memcpy(&neigh->dgid.raw, &path->pathrec.dgid.raw,
414 sizeof(union ib_gid));
416 while ((skb = __skb_dequeue(&neigh->queue)))
417 __skb_queue_tail(&skqueue, skb);
422 complete(&path->done);
424 spin_unlock_irqrestore(&priv->lock, flags);
426 while ((skb = __skb_dequeue(&skqueue))) {
428 if (dev_queue_xmit(skb))
429 ipoib_warn(priv, "dev_queue_xmit failed "
430 "to requeue packet\n");
434 static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
436 struct ipoib_dev_priv *priv = netdev_priv(dev);
437 struct ipoib_path *path;
439 path = kzalloc(sizeof *path, GFP_ATOMIC);
445 skb_queue_head_init(&path->queue);
447 INIT_LIST_HEAD(&path->neigh_list);
449 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
450 path->pathrec.sgid = priv->local_gid;
451 path->pathrec.pkey = cpu_to_be16(priv->pkey);
452 path->pathrec.numb_path = 1;
457 static int path_rec_start(struct net_device *dev,
458 struct ipoib_path *path)
460 struct ipoib_dev_priv *priv = netdev_priv(dev);
462 ipoib_dbg(priv, "Start path record lookup for " IPOIB_GID_FMT "\n",
463 IPOIB_GID_ARG(path->pathrec.dgid));
465 init_completion(&path->done);
468 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
470 IB_SA_PATH_REC_DGID |
471 IB_SA_PATH_REC_SGID |
472 IB_SA_PATH_REC_NUMB_PATH |
477 if (path->query_id < 0) {
478 ipoib_warn(priv, "ib_sa_path_rec_get failed\n");
480 return path->query_id;
486 static void neigh_add_path(struct sk_buff *skb, struct net_device *dev)
488 struct ipoib_dev_priv *priv = netdev_priv(dev);
489 struct ipoib_path *path;
490 struct ipoib_neigh *neigh;
492 neigh = ipoib_neigh_alloc(skb->dst->neighbour);
494 ++priv->stats.tx_dropped;
495 dev_kfree_skb_any(skb);
499 skb_queue_head_init(&neigh->queue);
502 * We can only be called from ipoib_start_xmit, so we're
503 * inside tx_lock -- no need to save/restore flags.
505 spin_lock(&priv->lock);
507 path = __path_find(dev, skb->dst->neighbour->ha + 4);
509 path = path_rec_create(dev, skb->dst->neighbour->ha + 4);
513 __path_add(dev, path);
516 list_add_tail(&neigh->list, &path->neigh_list);
519 kref_get(&path->ah->ref);
520 neigh->ah = path->ah;
521 memcpy(&neigh->dgid.raw, &path->pathrec.dgid.raw,
522 sizeof(union ib_gid));
524 ipoib_send(dev, skb, path->ah,
525 be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
528 __skb_queue_tail(&neigh->queue, skb);
530 if (!path->query && path_rec_start(dev, path))
534 spin_unlock(&priv->lock);
538 list_del(&neigh->list);
541 ipoib_neigh_free(neigh);
542 ++priv->stats.tx_dropped;
543 dev_kfree_skb_any(skb);
545 spin_unlock(&priv->lock);
548 static void ipoib_path_lookup(struct sk_buff *skb, struct net_device *dev)
550 struct ipoib_dev_priv *priv = netdev_priv(skb->dev);
552 /* Look up path record for unicasts */
553 if (skb->dst->neighbour->ha[4] != 0xff) {
554 neigh_add_path(skb, dev);
558 /* Add in the P_Key for multicasts */
559 skb->dst->neighbour->ha[8] = (priv->pkey >> 8) & 0xff;
560 skb->dst->neighbour->ha[9] = priv->pkey & 0xff;
561 ipoib_mcast_send(dev, skb->dst->neighbour->ha + 4, skb);
564 static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
565 struct ipoib_pseudoheader *phdr)
567 struct ipoib_dev_priv *priv = netdev_priv(dev);
568 struct ipoib_path *path;
571 * We can only be called from ipoib_start_xmit, so we're
572 * inside tx_lock -- no need to save/restore flags.
574 spin_lock(&priv->lock);
576 path = __path_find(dev, phdr->hwaddr + 4);
578 path = path_rec_create(dev, phdr->hwaddr + 4);
580 /* put pseudoheader back on for next time */
581 skb_push(skb, sizeof *phdr);
582 __skb_queue_tail(&path->queue, skb);
584 if (path_rec_start(dev, path)) {
585 spin_unlock(&priv->lock);
586 path_free(dev, path);
589 __path_add(dev, path);
591 ++priv->stats.tx_dropped;
592 dev_kfree_skb_any(skb);
595 spin_unlock(&priv->lock);
600 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
601 be16_to_cpu(path->pathrec.dlid));
603 ipoib_send(dev, skb, path->ah,
604 be32_to_cpup((__be32 *) phdr->hwaddr));
605 } else if ((path->query || !path_rec_start(dev, path)) &&
606 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
607 /* put pseudoheader back on for next time */
608 skb_push(skb, sizeof *phdr);
609 __skb_queue_tail(&path->queue, skb);
611 ++priv->stats.tx_dropped;
612 dev_kfree_skb_any(skb);
615 spin_unlock(&priv->lock);
618 static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
620 struct ipoib_dev_priv *priv = netdev_priv(dev);
621 struct ipoib_neigh *neigh;
624 if (!spin_trylock_irqsave(&priv->tx_lock, flags))
625 return NETDEV_TX_LOCKED;
628 * Check if our queue is stopped. Since we have the LLTX bit
629 * set, we can't rely on netif_stop_queue() preventing our
630 * xmit function from being called with a full queue.
632 if (unlikely(netif_queue_stopped(dev))) {
633 spin_unlock_irqrestore(&priv->tx_lock, flags);
634 return NETDEV_TX_BUSY;
637 if (skb->dst && skb->dst->neighbour) {
638 if (unlikely(!*to_ipoib_neigh(skb->dst->neighbour))) {
639 ipoib_path_lookup(skb, dev);
643 neigh = *to_ipoib_neigh(skb->dst->neighbour);
645 if (likely(neigh->ah)) {
646 if (unlikely(memcmp(&neigh->dgid.raw,
647 skb->dst->neighbour->ha + 4,
648 sizeof(union ib_gid)))) {
649 spin_lock(&priv->lock);
651 * It's safe to call ipoib_put_ah() inside
652 * priv->lock here, because we know that
653 * path->ah will always hold one more reference,
654 * so ipoib_put_ah() will never do more than
655 * decrement the ref count.
657 ipoib_put_ah(neigh->ah);
658 list_del(&neigh->list);
659 ipoib_neigh_free(neigh);
660 spin_unlock(&priv->lock);
661 ipoib_path_lookup(skb, dev);
665 ipoib_send(dev, skb, neigh->ah,
666 be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
670 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
671 spin_lock(&priv->lock);
672 __skb_queue_tail(&neigh->queue, skb);
673 spin_unlock(&priv->lock);
675 ++priv->stats.tx_dropped;
676 dev_kfree_skb_any(skb);
679 struct ipoib_pseudoheader *phdr =
680 (struct ipoib_pseudoheader *) skb->data;
681 skb_pull(skb, sizeof *phdr);
683 if (phdr->hwaddr[4] == 0xff) {
684 /* Add in the P_Key for multicast*/
685 phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
686 phdr->hwaddr[9] = priv->pkey & 0xff;
688 ipoib_mcast_send(dev, phdr->hwaddr + 4, skb);
690 /* unicast GID -- should be ARP or RARP reply */
692 if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
693 (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
694 ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x "
696 skb->dst ? "neigh" : "dst",
697 be16_to_cpup((__be16 *) skb->data),
698 be32_to_cpup((__be32 *) phdr->hwaddr),
699 IPOIB_GID_RAW_ARG(phdr->hwaddr + 4));
700 dev_kfree_skb_any(skb);
701 ++priv->stats.tx_dropped;
705 unicast_arp_send(skb, dev, phdr);
710 spin_unlock_irqrestore(&priv->tx_lock, flags);
715 static struct net_device_stats *ipoib_get_stats(struct net_device *dev)
717 struct ipoib_dev_priv *priv = netdev_priv(dev);
722 static void ipoib_timeout(struct net_device *dev)
724 struct ipoib_dev_priv *priv = netdev_priv(dev);
726 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
727 jiffies_to_msecs(jiffies - dev->trans_start));
728 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
729 netif_queue_stopped(dev),
730 priv->tx_head, priv->tx_tail);
731 /* XXX reset QP, etc. */
734 static int ipoib_hard_header(struct sk_buff *skb,
735 struct net_device *dev,
737 void *daddr, void *saddr, unsigned len)
739 struct ipoib_header *header;
741 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
743 header->proto = htons(type);
744 header->reserved = 0;
747 * If we don't have a neighbour structure, stuff the
748 * destination address onto the front of the skb so we can
749 * figure out where to send the packet later.
751 if ((!skb->dst || !skb->dst->neighbour) && daddr) {
752 struct ipoib_pseudoheader *phdr =
753 (struct ipoib_pseudoheader *) skb_push(skb, sizeof *phdr);
754 memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
760 static void ipoib_set_mcast_list(struct net_device *dev)
762 struct ipoib_dev_priv *priv = netdev_priv(dev);
764 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
765 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
769 queue_work(ipoib_workqueue, &priv->restart_task);
772 static void ipoib_neigh_destructor(struct neighbour *n)
774 struct ipoib_neigh *neigh;
775 struct ipoib_dev_priv *priv = netdev_priv(n->dev);
777 struct ipoib_ah *ah = NULL;
780 "neigh_destructor for %06x " IPOIB_GID_FMT "\n",
781 be32_to_cpup((__be32 *) n->ha),
782 IPOIB_GID_RAW_ARG(n->ha + 4));
784 spin_lock_irqsave(&priv->lock, flags);
786 neigh = *to_ipoib_neigh(n);
790 list_del(&neigh->list);
791 ipoib_neigh_free(neigh);
794 spin_unlock_irqrestore(&priv->lock, flags);
800 struct ipoib_neigh *ipoib_neigh_alloc(struct neighbour *neighbour)
802 struct ipoib_neigh *neigh;
804 neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
808 neigh->neighbour = neighbour;
809 *to_ipoib_neigh(neighbour) = neigh;
814 void ipoib_neigh_free(struct ipoib_neigh *neigh)
816 *to_ipoib_neigh(neigh->neighbour) = NULL;
820 static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
822 parms->neigh_destructor = ipoib_neigh_destructor;
827 int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
829 struct ipoib_dev_priv *priv = netdev_priv(dev);
831 /* Allocate RX/TX "rings" to hold queued skbs */
832 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
834 if (!priv->rx_ring) {
835 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
836 ca->name, ipoib_recvq_size);
840 priv->tx_ring = kzalloc(ipoib_sendq_size * sizeof *priv->tx_ring,
842 if (!priv->tx_ring) {
843 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
844 ca->name, ipoib_sendq_size);
845 goto out_rx_ring_cleanup;
848 /* priv->tx_head & tx_tail are already 0 */
850 if (ipoib_ib_dev_init(dev, ca, port))
851 goto out_tx_ring_cleanup;
856 kfree(priv->tx_ring);
859 kfree(priv->rx_ring);
865 void ipoib_dev_cleanup(struct net_device *dev)
867 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
869 ipoib_delete_debug_files(dev);
871 /* Delete any child interfaces first */
872 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
873 unregister_netdev(cpriv->dev);
874 ipoib_dev_cleanup(cpriv->dev);
875 free_netdev(cpriv->dev);
878 ipoib_ib_dev_cleanup(dev);
880 kfree(priv->rx_ring);
881 kfree(priv->tx_ring);
883 priv->rx_ring = NULL;
884 priv->tx_ring = NULL;
887 static void ipoib_setup(struct net_device *dev)
889 struct ipoib_dev_priv *priv = netdev_priv(dev);
891 dev->open = ipoib_open;
892 dev->stop = ipoib_stop;
893 dev->change_mtu = ipoib_change_mtu;
894 dev->hard_start_xmit = ipoib_start_xmit;
895 dev->get_stats = ipoib_get_stats;
896 dev->tx_timeout = ipoib_timeout;
897 dev->hard_header = ipoib_hard_header;
898 dev->set_multicast_list = ipoib_set_mcast_list;
899 dev->neigh_setup = ipoib_neigh_setup_dev;
901 dev->watchdog_timeo = HZ;
903 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
906 * We add in INFINIBAND_ALEN to allow for the destination
907 * address "pseudoheader" for skbs without neighbour struct.
909 dev->hard_header_len = IPOIB_ENCAP_LEN + INFINIBAND_ALEN;
910 dev->addr_len = INFINIBAND_ALEN;
911 dev->type = ARPHRD_INFINIBAND;
912 dev->tx_queue_len = ipoib_sendq_size * 2;
913 dev->features = NETIF_F_VLAN_CHALLENGED | NETIF_F_LLTX;
915 /* MTU will be reset when mcast join happens */
916 dev->mtu = IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN;
917 priv->mcast_mtu = priv->admin_mtu = dev->mtu;
919 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
921 netif_carrier_off(dev);
923 SET_MODULE_OWNER(dev);
927 spin_lock_init(&priv->lock);
928 spin_lock_init(&priv->tx_lock);
930 mutex_init(&priv->mcast_mutex);
931 mutex_init(&priv->vlan_mutex);
933 INIT_LIST_HEAD(&priv->path_list);
934 INIT_LIST_HEAD(&priv->child_intfs);
935 INIT_LIST_HEAD(&priv->dead_ahs);
936 INIT_LIST_HEAD(&priv->multicast_list);
938 INIT_WORK(&priv->pkey_task, ipoib_pkey_poll, priv->dev);
939 INIT_WORK(&priv->mcast_task, ipoib_mcast_join_task, priv->dev);
940 INIT_WORK(&priv->flush_task, ipoib_ib_dev_flush, priv->dev);
941 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task, priv->dev);
942 INIT_WORK(&priv->ah_reap_task, ipoib_reap_ah, priv->dev);
945 struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
947 struct net_device *dev;
949 dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
954 return netdev_priv(dev);
957 static ssize_t show_pkey(struct class_device *cdev, char *buf)
959 struct ipoib_dev_priv *priv =
960 netdev_priv(container_of(cdev, struct net_device, class_dev));
962 return sprintf(buf, "0x%04x\n", priv->pkey);
964 static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
966 static ssize_t create_child(struct class_device *cdev,
967 const char *buf, size_t count)
972 if (sscanf(buf, "%i", &pkey) != 1)
975 if (pkey < 0 || pkey > 0xffff)
979 * Set the full membership bit, so that we join the right
980 * broadcast group, etc.
984 ret = ipoib_vlan_add(container_of(cdev, struct net_device, class_dev),
987 return ret ? ret : count;
989 static CLASS_DEVICE_ATTR(create_child, S_IWUGO, NULL, create_child);
991 static ssize_t delete_child(struct class_device *cdev,
992 const char *buf, size_t count)
997 if (sscanf(buf, "%i", &pkey) != 1)
1000 if (pkey < 0 || pkey > 0xffff)
1003 ret = ipoib_vlan_delete(container_of(cdev, struct net_device, class_dev),
1006 return ret ? ret : count;
1009 static CLASS_DEVICE_ATTR(delete_child, S_IWUGO, NULL, delete_child);
1011 int ipoib_add_pkey_attr(struct net_device *dev)
1013 return class_device_create_file(&dev->class_dev,
1014 &class_device_attr_pkey);
1017 static struct net_device *ipoib_add_port(const char *format,
1018 struct ib_device *hca, u8 port)
1020 struct ipoib_dev_priv *priv;
1021 int result = -ENOMEM;
1023 priv = ipoib_intf_alloc(format);
1025 goto alloc_mem_failed;
1027 SET_NETDEV_DEV(priv->dev, hca->dma_device);
1029 result = ib_query_pkey(hca, port, 0, &priv->pkey);
1031 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1032 hca->name, port, result);
1033 goto alloc_mem_failed;
1037 * Set the full membership bit, so that we join the right
1038 * broadcast group, etc.
1040 priv->pkey |= 0x8000;
1042 priv->dev->broadcast[8] = priv->pkey >> 8;
1043 priv->dev->broadcast[9] = priv->pkey & 0xff;
1045 result = ib_query_gid(hca, port, 0, &priv->local_gid);
1047 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1048 hca->name, port, result);
1049 goto alloc_mem_failed;
1051 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1054 result = ipoib_dev_init(priv->dev, hca, port);
1056 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1057 hca->name, port, result);
1058 goto device_init_failed;
1061 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1062 priv->ca, ipoib_event);
1063 result = ib_register_event_handler(&priv->event_handler);
1065 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1066 "port %d (ret = %d)\n",
1067 hca->name, port, result);
1071 result = register_netdev(priv->dev);
1073 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1074 hca->name, port, result);
1075 goto register_failed;
1078 ipoib_create_debug_files(priv->dev);
1080 if (ipoib_add_pkey_attr(priv->dev))
1082 if (class_device_create_file(&priv->dev->class_dev,
1083 &class_device_attr_create_child))
1085 if (class_device_create_file(&priv->dev->class_dev,
1086 &class_device_attr_delete_child))
1092 ipoib_delete_debug_files(priv->dev);
1093 unregister_netdev(priv->dev);
1096 ib_unregister_event_handler(&priv->event_handler);
1097 flush_scheduled_work();
1100 ipoib_dev_cleanup(priv->dev);
1103 free_netdev(priv->dev);
1106 return ERR_PTR(result);
1109 static void ipoib_add_one(struct ib_device *device)
1111 struct list_head *dev_list;
1112 struct net_device *dev;
1113 struct ipoib_dev_priv *priv;
1116 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1119 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1123 INIT_LIST_HEAD(dev_list);
1125 if (device->node_type == RDMA_NODE_IB_SWITCH) {
1130 e = device->phys_port_cnt;
1133 for (p = s; p <= e; ++p) {
1134 dev = ipoib_add_port("ib%d", device, p);
1136 priv = netdev_priv(dev);
1137 list_add_tail(&priv->list, dev_list);
1141 ib_set_client_data(device, &ipoib_client, dev_list);
1144 static void ipoib_remove_one(struct ib_device *device)
1146 struct ipoib_dev_priv *priv, *tmp;
1147 struct list_head *dev_list;
1149 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1152 dev_list = ib_get_client_data(device, &ipoib_client);
1154 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1155 ib_unregister_event_handler(&priv->event_handler);
1156 flush_scheduled_work();
1158 unregister_netdev(priv->dev);
1159 ipoib_dev_cleanup(priv->dev);
1160 free_netdev(priv->dev);
1166 static int __init ipoib_init_module(void)
1170 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1171 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1172 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1174 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1175 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1176 ipoib_sendq_size = max(ipoib_sendq_size, IPOIB_MIN_QUEUE_SIZE);
1178 ret = ipoib_register_debugfs();
1183 * We create our own workqueue mainly because we want to be
1184 * able to flush it when devices are being removed. We can't
1185 * use schedule_work()/flush_scheduled_work() because both
1186 * unregister_netdev() and linkwatch_event take the rtnl lock,
1187 * so flush_scheduled_work() can deadlock during device
1190 ipoib_workqueue = create_singlethread_workqueue("ipoib");
1191 if (!ipoib_workqueue) {
1196 ib_sa_register_client(&ipoib_sa_client);
1198 ret = ib_register_client(&ipoib_client);
1205 ib_sa_unregister_client(&ipoib_sa_client);
1206 destroy_workqueue(ipoib_workqueue);
1209 ipoib_unregister_debugfs();
1214 static void __exit ipoib_cleanup_module(void)
1216 ib_unregister_client(&ipoib_client);
1217 ib_sa_unregister_client(&ipoib_sa_client);
1218 ipoib_unregister_debugfs();
1219 destroy_workqueue(ipoib_workqueue);
1222 module_init(ipoib_init_module);
1223 module_exit(ipoib_cleanup_module);