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
37 static int ipoib_resolvemulti(struct ifnet *, struct sockaddr **,
41 #include <linux/module.h>
43 #include <linux/slab.h>
44 #include <linux/kernel.h>
45 #include <linux/vmalloc.h>
47 #include <linux/if_arp.h> /* For ARPHRD_xxx */
48 #include <linux/if_vlan.h>
52 MODULE_AUTHOR("Roland Dreier");
53 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
54 MODULE_LICENSE("Dual BSD/GPL");
56 int ipoib_sendq_size = IPOIB_TX_RING_SIZE;
57 int ipoib_recvq_size = IPOIB_RX_RING_SIZE;
59 module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
60 MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
61 module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
62 MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
64 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
65 int ipoib_debug_level = 1;
67 module_param_named(debug_level, ipoib_debug_level, int, 0644);
68 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
71 struct ipoib_path_iter {
72 struct ipoib_dev_priv *priv;
73 struct ipoib_path path;
76 static const u8 ipv4_bcast_addr[] = {
77 0x00, 0xff, 0xff, 0xff,
78 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
79 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
82 struct workqueue_struct *ipoib_workqueue;
84 struct ib_sa_client ipoib_sa_client;
86 static void ipoib_add_one(struct ib_device *device);
87 static void ipoib_remove_one(struct ib_device *device);
88 static void ipoib_start(struct ifnet *dev);
89 static int ipoib_output(struct ifnet *ifp, struct mbuf *m,
90 const struct sockaddr *dst, struct route *ro);
91 static int ipoib_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
92 static void ipoib_input(struct ifnet *ifp, struct mbuf *m);
94 #define IPOIB_MTAP(_ifp, _m) \
96 if (bpf_peers_present((_ifp)->if_bpf)) { \
98 ipoib_mtap_mb((_ifp), (_m)); \
103 * This is for clients that have an ipoib_header in the mbuf.
106 ipoib_mtap_mb(struct ifnet *ifp, struct mbuf *mb)
108 struct ipoib_header *ih;
109 struct ether_header eh;
111 ih = mtod(mb, struct ipoib_header *);
112 eh.ether_type = ih->proto;
113 bcopy(ih->hwaddr, &eh.ether_dhost, ETHER_ADDR_LEN);
114 bzero(&eh.ether_shost, ETHER_ADDR_LEN);
115 mb->m_data += sizeof(struct ipoib_header);
116 mb->m_len -= sizeof(struct ipoib_header);
117 bpf_mtap2(ifp->if_bpf, &eh, sizeof(eh), mb);
118 mb->m_data -= sizeof(struct ipoib_header);
119 mb->m_len += sizeof(struct ipoib_header);
123 ipoib_mtap_proto(struct ifnet *ifp, struct mbuf *mb, uint16_t proto)
125 struct ether_header eh;
127 eh.ether_type = proto;
128 bzero(&eh.ether_shost, ETHER_ADDR_LEN);
129 bzero(&eh.ether_dhost, ETHER_ADDR_LEN);
130 bpf_mtap2(ifp->if_bpf, &eh, sizeof(eh), mb);
133 static struct ib_client ipoib_client = {
135 .add = ipoib_add_one,
136 .remove = ipoib_remove_one
140 ipoib_open(struct ipoib_dev_priv *priv)
142 struct ifnet *dev = priv->dev;
144 ipoib_dbg(priv, "bringing up interface\n");
146 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
148 if (ipoib_pkey_dev_delay_open(priv))
151 if (ipoib_ib_dev_open(priv))
154 if (ipoib_ib_dev_up(priv))
157 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
158 struct ipoib_dev_priv *cpriv;
160 /* Bring up any child interfaces too */
161 mutex_lock(&priv->vlan_mutex);
162 list_for_each_entry(cpriv, &priv->child_intfs, list)
163 if ((cpriv->dev->if_drv_flags & IFF_DRV_RUNNING) == 0)
165 mutex_unlock(&priv->vlan_mutex);
167 dev->if_drv_flags |= IFF_DRV_RUNNING;
168 dev->if_drv_flags &= ~IFF_DRV_OACTIVE;
173 ipoib_ib_dev_stop(priv, 1);
176 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
182 ipoib_init(void *arg)
185 struct ipoib_dev_priv *priv;
189 if ((dev->if_drv_flags & IFF_DRV_RUNNING) == 0)
191 queue_work(ipoib_workqueue, &priv->flush_light);
196 ipoib_stop(struct ipoib_dev_priv *priv)
198 struct ifnet *dev = priv->dev;
200 ipoib_dbg(priv, "stopping interface\n");
202 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
204 dev->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
206 ipoib_ib_dev_down(priv, 0);
207 ipoib_ib_dev_stop(priv, 0);
209 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
210 struct ipoib_dev_priv *cpriv;
212 /* Bring down any child interfaces too */
213 mutex_lock(&priv->vlan_mutex);
214 list_for_each_entry(cpriv, &priv->child_intfs, list)
215 if ((cpriv->dev->if_drv_flags & IFF_DRV_RUNNING) != 0)
217 mutex_unlock(&priv->vlan_mutex);
224 ipoib_change_mtu(struct ipoib_dev_priv *priv, int new_mtu)
226 struct ifnet *dev = priv->dev;
228 /* dev->if_mtu > 2K ==> connected mode */
229 if (ipoib_cm_admin_enabled(priv)) {
230 if (new_mtu > IPOIB_CM_MTU(ipoib_cm_max_mtu(priv)))
233 if (new_mtu > priv->mcast_mtu)
234 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
237 dev->if_mtu = new_mtu;
241 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
244 priv->admin_mtu = new_mtu;
246 dev->if_mtu = min(priv->mcast_mtu, priv->admin_mtu);
248 queue_work(ipoib_workqueue, &priv->flush_light);
254 ipoib_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
256 struct ipoib_dev_priv *priv = ifp->if_softc;
257 struct ifaddr *ifa = (struct ifaddr *) data;
258 struct ifreq *ifr = (struct ifreq *) data;
261 /* check if detaching */
262 if (priv == NULL || priv->gone != 0)
267 if (ifp->if_flags & IFF_UP) {
268 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
269 error = -ipoib_open(priv);
271 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
276 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
277 queue_work(ipoib_workqueue, &priv->restart_task);
280 ifp->if_flags |= IFF_UP;
282 switch (ifa->ifa_addr->sa_family) {
285 ifp->if_init(ifp->if_softc); /* before arpwhohas */
286 arp_ifinit(ifp, ifa);
290 ifp->if_init(ifp->if_softc);
299 sa = (struct sockaddr *) & ifr->ifr_data;
300 bcopy(IF_LLADDR(ifp),
301 (caddr_t) sa->sa_data, INFINIBAND_ALEN);
307 * Set the interface MTU.
309 error = -ipoib_change_mtu(priv, ifr->ifr_mtu);
319 static struct ipoib_path *
320 __path_find(struct ipoib_dev_priv *priv, void *gid)
322 struct rb_node *n = priv->path_tree.rb_node;
323 struct ipoib_path *path;
327 path = rb_entry(n, struct ipoib_path, rb_node);
329 ret = memcmp(gid, path->pathrec.dgid.raw,
330 sizeof (union ib_gid));
344 __path_add(struct ipoib_dev_priv *priv, struct ipoib_path *path)
346 struct rb_node **n = &priv->path_tree.rb_node;
347 struct rb_node *pn = NULL;
348 struct ipoib_path *tpath;
353 tpath = rb_entry(pn, struct ipoib_path, rb_node);
355 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
356 sizeof (union ib_gid));
365 rb_link_node(&path->rb_node, pn, n);
366 rb_insert_color(&path->rb_node, &priv->path_tree);
368 list_add_tail(&path->list, &priv->path_list);
374 ipoib_path_free(struct ipoib_dev_priv *priv, struct ipoib_path *path)
377 _IF_DRAIN(&path->queue);
380 ipoib_put_ah(path->ah);
381 if (ipoib_cm_get(path))
382 ipoib_cm_destroy_tx(ipoib_cm_get(path));
387 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
389 struct ipoib_path_iter *
390 ipoib_path_iter_init(struct ipoib_dev_priv *priv)
392 struct ipoib_path_iter *iter;
394 iter = kmalloc(sizeof *iter, GFP_KERNEL);
399 memset(iter->path.pathrec.dgid.raw, 0, 16);
401 if (ipoib_path_iter_next(iter)) {
410 ipoib_path_iter_next(struct ipoib_path_iter *iter)
412 struct ipoib_dev_priv *priv = iter->priv;
414 struct ipoib_path *path;
417 spin_lock_irq(&priv->lock);
419 n = rb_first(&priv->path_tree);
422 path = rb_entry(n, struct ipoib_path, rb_node);
424 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
425 sizeof (union ib_gid)) < 0) {
434 spin_unlock_irq(&priv->lock);
440 ipoib_path_iter_read(struct ipoib_path_iter *iter, struct ipoib_path *path)
445 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
448 ipoib_mark_paths_invalid(struct ipoib_dev_priv *priv)
450 struct ipoib_path *path, *tp;
452 spin_lock_irq(&priv->lock);
454 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
455 ipoib_dbg(priv, "mark path LID 0x%04x GID %16D invalid\n",
456 be16_to_cpu(path->pathrec.dlid),
457 path->pathrec.dgid.raw, ":");
461 spin_unlock_irq(&priv->lock);
465 ipoib_flush_paths(struct ipoib_dev_priv *priv)
467 struct ipoib_path *path, *tp;
468 LIST_HEAD(remove_list);
471 spin_lock_irqsave(&priv->lock, flags);
473 list_splice_init(&priv->path_list, &remove_list);
475 list_for_each_entry(path, &remove_list, list)
476 rb_erase(&path->rb_node, &priv->path_tree);
478 list_for_each_entry_safe(path, tp, &remove_list, list) {
480 ib_sa_cancel_query(path->query_id, path->query);
481 spin_unlock_irqrestore(&priv->lock, flags);
482 wait_for_completion(&path->done);
483 ipoib_path_free(priv, path);
484 spin_lock_irqsave(&priv->lock, flags);
487 spin_unlock_irqrestore(&priv->lock, flags);
491 path_rec_completion(int status, struct ib_sa_path_rec *pathrec, void *path_ptr)
493 struct ipoib_path *path = path_ptr;
494 struct ipoib_dev_priv *priv = path->priv;
495 struct ifnet *dev = priv->dev;
496 struct ipoib_ah *ah = NULL;
497 struct ipoib_ah *old_ah = NULL;
498 struct ifqueue mbqueue;
503 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %16D\n",
504 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw, ":");
506 ipoib_dbg(priv, "PathRec status %d for GID %16D\n",
507 status, path->pathrec.dgid.raw, ":");
509 bzero(&mbqueue, sizeof(mbqueue));
512 struct ib_ah_attr av;
514 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
515 ah = ipoib_create_ah(priv, priv->pd, &av);
518 spin_lock_irqsave(&priv->lock, flags);
521 path->pathrec = *pathrec;
526 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
527 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
530 _IF_DEQUEUE(&path->queue, mb);
533 _IF_ENQUEUE(&mbqueue, mb);
536 #ifdef CONFIG_INFINIBAND_IPOIB_CM
537 if (ipoib_cm_enabled(priv, path->hwaddr) && !ipoib_cm_get(path))
538 ipoib_cm_set(path, ipoib_cm_create_tx(priv, path));
545 complete(&path->done);
547 spin_unlock_irqrestore(&priv->lock, flags);
550 ipoib_put_ah(old_ah);
553 _IF_DEQUEUE(&mbqueue, mb);
556 mb->m_pkthdr.rcvif = dev;
557 if (dev->if_transmit(dev, mb))
558 ipoib_warn(priv, "dev_queue_xmit failed "
559 "to requeue packet\n");
563 static struct ipoib_path *
564 path_rec_create(struct ipoib_dev_priv *priv, uint8_t *hwaddr)
566 struct ipoib_path *path;
568 if (!priv->broadcast)
571 path = kzalloc(sizeof *path, GFP_ATOMIC);
577 bzero(&path->queue, sizeof(path->queue));
579 #ifdef CONFIG_INFINIBAND_IPOIB_CM
580 memcpy(&path->hwaddr, hwaddr, INFINIBAND_ALEN);
582 memcpy(path->pathrec.dgid.raw, &hwaddr[4], sizeof (union ib_gid));
583 path->pathrec.sgid = priv->local_gid;
584 path->pathrec.pkey = cpu_to_be16(priv->pkey);
585 path->pathrec.numb_path = 1;
586 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
592 path_rec_start(struct ipoib_dev_priv *priv, struct ipoib_path *path)
594 struct ifnet *dev = priv->dev;
596 ib_sa_comp_mask comp_mask = IB_SA_PATH_REC_MTU_SELECTOR | IB_SA_PATH_REC_MTU;
597 struct ib_sa_path_rec p_rec;
599 p_rec = path->pathrec;
600 p_rec.mtu_selector = IB_SA_GT;
602 switch (roundup_pow_of_two(dev->if_mtu + IPOIB_ENCAP_LEN)) {
604 p_rec.mtu = IB_MTU_256;
607 p_rec.mtu = IB_MTU_512;
610 p_rec.mtu = IB_MTU_1024;
613 p_rec.mtu = IB_MTU_2048;
616 /* Wildcard everything */
619 p_rec.mtu_selector = 0;
622 ipoib_dbg(priv, "Start path record lookup for %16D MTU > %d\n",
624 comp_mask ? ib_mtu_enum_to_int(p_rec.mtu) : 0);
626 init_completion(&path->done);
629 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
631 IB_SA_PATH_REC_DGID |
632 IB_SA_PATH_REC_SGID |
633 IB_SA_PATH_REC_NUMB_PATH |
634 IB_SA_PATH_REC_TRAFFIC_CLASS |
639 if (path->query_id < 0) {
640 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
642 complete(&path->done);
643 return path->query_id;
650 ipoib_unicast_send(struct mbuf *mb, struct ipoib_dev_priv *priv, struct ipoib_header *eh)
652 struct ipoib_path *path;
654 path = __path_find(priv, eh->hwaddr + 4);
655 if (!path || !path->valid) {
659 path = path_rec_create(priv, eh->hwaddr);
663 if (_IF_QLEN(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE)
664 _IF_ENQUEUE(&path->queue, mb);
666 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
670 if (!path->query && path_rec_start(priv, path)) {
671 spin_unlock_irqrestore(&priv->lock, flags);
673 ipoib_path_free(priv, path);
676 __path_add(priv, path);
678 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
685 if (ipoib_cm_get(path) && ipoib_cm_up(path)) {
686 ipoib_cm_send(priv, mb, ipoib_cm_get(path));
687 } else if (path->ah) {
688 ipoib_send(priv, mb, path->ah, IPOIB_QPN(eh->hwaddr));
689 } else if ((path->query || !path_rec_start(priv, path)) &&
690 path->queue.ifq_len < IPOIB_MAX_PATH_REC_QUEUE) {
691 _IF_ENQUEUE(&path->queue, mb);
693 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
699 ipoib_send_one(struct ipoib_dev_priv *priv, struct mbuf *mb)
701 struct ipoib_header *eh;
703 eh = mtod(mb, struct ipoib_header *);
704 if (IPOIB_IS_MULTICAST(eh->hwaddr)) {
705 /* Add in the P_Key for multicast*/
706 eh->hwaddr[8] = (priv->pkey >> 8) & 0xff;
707 eh->hwaddr[9] = priv->pkey & 0xff;
709 ipoib_mcast_send(priv, eh->hwaddr + 4, mb);
711 ipoib_unicast_send(mb, priv, eh);
718 _ipoib_start(struct ifnet *dev, struct ipoib_dev_priv *priv)
722 if ((dev->if_drv_flags & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
726 spin_lock(&priv->lock);
727 while (!IFQ_DRV_IS_EMPTY(&dev->if_snd) &&
728 (dev->if_drv_flags & IFF_DRV_OACTIVE) == 0) {
729 IFQ_DRV_DEQUEUE(&dev->if_snd, mb);
733 ipoib_send_one(priv, mb);
735 spin_unlock(&priv->lock);
739 ipoib_start(struct ifnet *dev)
741 _ipoib_start(dev, dev->if_softc);
745 ipoib_vlan_start(struct ifnet *dev)
747 struct ipoib_dev_priv *priv;
750 priv = VLAN_COOKIE(dev);
752 return _ipoib_start(dev, priv);
753 while (!IFQ_DRV_IS_EMPTY(&dev->if_snd)) {
754 IFQ_DRV_DEQUEUE(&dev->if_snd, mb);
758 if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
763 ipoib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca, int port)
766 /* Allocate RX/TX "rings" to hold queued mbs */
767 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
769 if (!priv->rx_ring) {
770 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
771 ca->name, ipoib_recvq_size);
775 priv->tx_ring = kzalloc(ipoib_sendq_size * sizeof *priv->tx_ring, GFP_KERNEL);
776 if (!priv->tx_ring) {
777 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
778 ca->name, ipoib_sendq_size);
779 goto out_rx_ring_cleanup;
781 memset(priv->tx_ring, 0, ipoib_sendq_size * sizeof *priv->tx_ring);
783 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
785 if (ipoib_ib_dev_init(priv, ca, port))
786 goto out_tx_ring_cleanup;
791 kfree(priv->tx_ring);
794 kfree(priv->rx_ring);
801 ipoib_detach(struct ipoib_dev_priv *priv)
806 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
812 VLAN_SETCOOKIE(priv->dev, NULL);
818 ipoib_dev_cleanup(struct ipoib_dev_priv *priv)
820 struct ipoib_dev_priv *cpriv, *tcpriv;
822 /* Delete any child interfaces first */
823 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
824 ipoib_dev_cleanup(cpriv);
828 ipoib_ib_dev_cleanup(priv);
830 kfree(priv->rx_ring);
831 kfree(priv->tx_ring);
833 priv->rx_ring = NULL;
834 priv->tx_ring = NULL;
837 static volatile int ipoib_unit;
839 static struct ipoib_dev_priv *
840 ipoib_priv_alloc(void)
842 struct ipoib_dev_priv *priv;
844 priv = malloc(sizeof(struct ipoib_dev_priv), M_TEMP, M_ZERO|M_WAITOK);
845 spin_lock_init(&priv->lock);
846 spin_lock_init(&priv->drain_lock);
847 mutex_init(&priv->vlan_mutex);
848 INIT_LIST_HEAD(&priv->path_list);
849 INIT_LIST_HEAD(&priv->child_intfs);
850 INIT_LIST_HEAD(&priv->dead_ahs);
851 INIT_LIST_HEAD(&priv->multicast_list);
852 INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
853 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
854 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
855 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
856 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
857 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
858 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
859 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
860 memcpy(priv->broadcastaddr, ipv4_bcast_addr, INFINIBAND_ALEN);
865 struct ipoib_dev_priv *
866 ipoib_intf_alloc(const char *name)
868 struct ipoib_dev_priv *priv;
869 struct sockaddr_dl *sdl;
872 priv = ipoib_priv_alloc();
873 dev = priv->dev = if_alloc(IFT_INFINIBAND);
878 dev->if_softc = priv;
879 if_initname(dev, name, atomic_fetchadd_int(&ipoib_unit, 1));
880 dev->if_flags = IFF_BROADCAST | IFF_MULTICAST;
881 dev->if_addrlen = INFINIBAND_ALEN;
882 dev->if_hdrlen = IPOIB_HEADER_LEN;
884 dev->if_init = ipoib_init;
885 dev->if_ioctl = ipoib_ioctl;
886 dev->if_start = ipoib_start;
887 dev->if_output = ipoib_output;
888 dev->if_input = ipoib_input;
889 dev->if_resolvemulti = ipoib_resolvemulti;
890 dev->if_baudrate = IF_Gbps(10);
891 dev->if_broadcastaddr = priv->broadcastaddr;
892 dev->if_snd.ifq_maxlen = ipoib_sendq_size * 2;
893 sdl = (struct sockaddr_dl *)dev->if_addr->ifa_addr;
894 sdl->sdl_type = IFT_INFINIBAND;
895 sdl->sdl_alen = dev->if_addrlen;
897 if_link_state_change(dev, LINK_STATE_DOWN);
898 bpfattach(dev, DLT_EN10MB, ETHER_HDR_LEN);
900 return dev->if_softc;
904 ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
906 struct ib_device_attr *device_attr;
907 int result = -ENOMEM;
909 device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
911 printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
912 hca->name, sizeof *device_attr);
916 result = ib_query_device(hca, device_attr);
918 printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
923 priv->hca_caps = device_attr->device_cap_flags;
927 priv->dev->if_hwassist = 0;
928 priv->dev->if_capabilities = 0;
930 #ifndef CONFIG_INFINIBAND_IPOIB_CM
931 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
932 set_bit(IPOIB_FLAG_CSUM, &priv->flags);
933 priv->dev->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP;
934 priv->dev->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM;
938 if (priv->dev->features & NETIF_F_SG && priv->hca_caps & IB_DEVICE_UD_TSO) {
939 priv->dev->if_capabilities |= IFCAP_TSO4;
940 priv->dev->if_hwassist |= CSUM_TSO;
944 priv->dev->if_capabilities |=
945 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_LINKSTATE;
946 priv->dev->if_capenable = priv->dev->if_capabilities;
952 static struct ifnet *
953 ipoib_add_port(const char *format, struct ib_device *hca, u8 port)
955 struct ipoib_dev_priv *priv;
956 struct ib_port_attr attr;
957 int result = -ENOMEM;
959 priv = ipoib_intf_alloc(format);
961 goto alloc_mem_failed;
963 if (!ib_query_port(hca, port, &attr))
964 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
966 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
968 goto device_init_failed;
971 /* MTU will be reset when mcast join happens */
972 priv->dev->if_mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
973 priv->mcast_mtu = priv->admin_mtu = priv->dev->if_mtu;
975 result = ib_query_pkey(hca, port, 0, &priv->pkey);
977 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
978 hca->name, port, result);
979 goto device_init_failed;
982 if (ipoib_set_dev_features(priv, hca))
983 goto device_init_failed;
986 * Set the full membership bit, so that we join the right
987 * broadcast group, etc.
989 priv->pkey |= 0x8000;
991 priv->broadcastaddr[8] = priv->pkey >> 8;
992 priv->broadcastaddr[9] = priv->pkey & 0xff;
994 result = ib_query_gid(hca, port, 0, &priv->local_gid);
996 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
997 hca->name, port, result);
998 goto device_init_failed;
1000 memcpy(IF_LLADDR(priv->dev) + 4, priv->local_gid.raw, sizeof (union ib_gid));
1002 result = ipoib_dev_init(priv, hca, port);
1004 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1005 hca->name, port, result);
1006 goto device_init_failed;
1008 if (ipoib_cm_admin_enabled(priv))
1009 priv->dev->if_mtu = IPOIB_CM_MTU(ipoib_cm_max_mtu(priv));
1011 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1012 priv->ca, ipoib_event);
1013 result = ib_register_event_handler(&priv->event_handler);
1015 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1016 "port %d (ret = %d)\n",
1017 hca->name, port, result);
1020 if_printf(priv->dev, "Attached to %s port %d\n", hca->name, port);
1025 ipoib_dev_cleanup(priv);
1031 return ERR_PTR(result);
1035 ipoib_add_one(struct ib_device *device)
1037 struct list_head *dev_list;
1039 struct ipoib_dev_priv *priv;
1042 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1045 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1049 INIT_LIST_HEAD(dev_list);
1051 if (device->node_type == RDMA_NODE_IB_SWITCH) {
1056 e = device->phys_port_cnt;
1059 for (p = s; p <= e; ++p) {
1060 if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
1062 dev = ipoib_add_port("ib", device, p);
1064 priv = dev->if_softc;
1065 list_add_tail(&priv->list, dev_list);
1069 ib_set_client_data(device, &ipoib_client, dev_list);
1073 ipoib_remove_one(struct ib_device *device)
1075 struct ipoib_dev_priv *priv, *tmp;
1076 struct list_head *dev_list;
1078 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1081 dev_list = ib_get_client_data(device, &ipoib_client);
1083 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1084 if (rdma_port_get_link_layer(device, priv->port) != IB_LINK_LAYER_INFINIBAND)
1089 ib_unregister_event_handler(&priv->event_handler);
1091 /* dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP); */
1093 flush_workqueue(ipoib_workqueue);
1095 ipoib_dev_cleanup(priv);
1103 ipoib_config_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
1105 struct ipoib_dev_priv *parent;
1106 struct ipoib_dev_priv *priv;
1111 if (ifp->if_type != IFT_INFINIBAND)
1113 dev = VLAN_DEVAT(ifp, vtag);
1118 parent = ifp->if_softc;
1119 /* We only support 15 bits of pkey. */
1122 pkey = vtag | 0x8000; /* Set full membership bit. */
1123 if (pkey == parent->pkey)
1125 /* Check for dups */
1126 mutex_lock(&parent->vlan_mutex);
1127 list_for_each_entry(priv, &parent->child_intfs, list) {
1128 if (priv->pkey == pkey) {
1134 priv = ipoib_priv_alloc();
1136 priv->max_ib_mtu = parent->max_ib_mtu;
1137 priv->mcast_mtu = priv->admin_mtu = parent->dev->if_mtu;
1138 set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
1139 error = ipoib_set_dev_features(priv, parent->ca);
1143 priv->broadcastaddr[8] = pkey >> 8;
1144 priv->broadcastaddr[9] = pkey & 0xff;
1145 dev->if_broadcastaddr = priv->broadcastaddr;
1146 error = ipoib_dev_init(priv, parent->ca, parent->port);
1149 priv->parent = parent->dev;
1150 list_add_tail(&priv->list, &parent->child_intfs);
1151 VLAN_SETCOOKIE(dev, priv);
1152 dev->if_start = ipoib_vlan_start;
1153 dev->if_drv_flags &= ~IFF_DRV_RUNNING;
1154 dev->if_hdrlen = IPOIB_HEADER_LEN;
1155 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1157 mutex_unlock(&parent->vlan_mutex);
1160 mutex_unlock(&parent->vlan_mutex);
1165 "failed to initialize subinterface: device %s, port %d vtag 0x%X",
1166 parent->ca->name, parent->port, vtag);
1171 ipoib_unconfig_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
1173 struct ipoib_dev_priv *parent;
1174 struct ipoib_dev_priv *priv;
1178 if (ifp->if_type != IFT_INFINIBAND)
1181 dev = VLAN_DEVAT(ifp, vtag);
1183 VLAN_SETCOOKIE(dev, NULL);
1184 pkey = vtag | 0x8000;
1185 parent = ifp->if_softc;
1186 mutex_lock(&parent->vlan_mutex);
1187 list_for_each_entry(priv, &parent->child_intfs, list) {
1188 if (priv->pkey == pkey) {
1189 ipoib_dev_cleanup(priv);
1190 list_del(&priv->list);
1194 mutex_unlock(&parent->vlan_mutex);
1197 eventhandler_tag ipoib_vlan_attach;
1198 eventhandler_tag ipoib_vlan_detach;
1201 ipoib_init_module(void)
1205 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1206 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1207 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1209 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1210 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1211 ipoib_sendq_size = max(ipoib_sendq_size, max(2 * MAX_SEND_CQE,
1212 IPOIB_MIN_QUEUE_SIZE));
1213 #ifdef CONFIG_INFINIBAND_IPOIB_CM
1214 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1217 ipoib_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
1218 ipoib_config_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1219 ipoib_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
1220 ipoib_unconfig_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1223 * We create our own workqueue mainly because we want to be
1224 * able to flush it when devices are being removed. We can't
1225 * use schedule_work()/flush_scheduled_work() because both
1226 * unregister_netdev() and linkwatch_event take the rtnl lock,
1227 * so flush_scheduled_work() can deadlock during device
1230 ipoib_workqueue = create_singlethread_workqueue("ipoib");
1231 if (!ipoib_workqueue) {
1236 ib_sa_register_client(&ipoib_sa_client);
1238 ret = ib_register_client(&ipoib_client);
1245 ib_sa_unregister_client(&ipoib_sa_client);
1246 destroy_workqueue(ipoib_workqueue);
1253 ipoib_cleanup_module(void)
1256 EVENTHANDLER_DEREGISTER(vlan_config, ipoib_vlan_attach);
1257 EVENTHANDLER_DEREGISTER(vlan_unconfig, ipoib_vlan_detach);
1258 ib_unregister_client(&ipoib_client);
1259 ib_sa_unregister_client(&ipoib_sa_client);
1260 destroy_workqueue(ipoib_workqueue);
1264 * Infiniband output routine.
1267 ipoib_output(struct ifnet *ifp, struct mbuf *m,
1268 const struct sockaddr *dst, struct route *ro)
1270 u_char edst[INFINIBAND_ALEN];
1271 #if defined(INET) || defined(INET6)
1272 struct llentry *lle = NULL;
1274 struct ipoib_header *eh;
1275 int error = 0, is_gw = 0;
1279 is_gw = (ro->ro_flags & RT_HAS_GW) != 0;
1281 error = mac_ifnet_check_transmit(ifp, m);
1287 if (ifp->if_flags & IFF_MONITOR) {
1291 if (!((ifp->if_flags & IFF_UP) &&
1292 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
1297 switch (dst->sa_family) {
1300 if (lle != NULL && (lle->la_flags & LLE_VALID))
1301 memcpy(edst, lle->ll_addr, sizeof(edst));
1302 else if (m->m_flags & M_MCAST)
1303 ip_ib_mc_map(((struct sockaddr_in *)dst)->sin_addr.s_addr, ifp->if_broadcastaddr, edst);
1305 error = arpresolve(ifp, is_gw, m, dst, edst, NULL, NULL);
1307 return (error == EWOULDBLOCK ? 0 : error);
1308 type = htons(ETHERTYPE_IP);
1313 ah = mtod(m, struct arphdr *);
1314 ah->ar_hrd = htons(ARPHRD_INFINIBAND);
1316 switch(ntohs(ah->ar_op)) {
1317 case ARPOP_REVREQUEST:
1318 case ARPOP_REVREPLY:
1319 type = htons(ETHERTYPE_REVARP);
1324 type = htons(ETHERTYPE_ARP);
1328 if (m->m_flags & M_BCAST)
1329 bcopy(ifp->if_broadcastaddr, edst, INFINIBAND_ALEN);
1331 bcopy(ar_tha(ah), edst, INFINIBAND_ALEN);
1338 if (lle != NULL && (lle->la_flags & LLE_VALID))
1339 memcpy(edst, lle->ll_addr, sizeof(edst));
1340 else if (m->m_flags & M_MCAST)
1341 ipv6_ib_mc_map(&((struct sockaddr_in6 *)dst)->sin6_addr, ifp->if_broadcastaddr, edst);
1343 error = nd6_resolve(ifp, is_gw, m, dst, edst, NULL, NULL);
1346 type = htons(ETHERTYPE_IPV6);
1351 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
1352 error = EAFNOSUPPORT;
1357 * Add local net header. If no space in first mbuf,
1360 M_PREPEND(m, IPOIB_HEADER_LEN, M_NOWAIT);
1365 eh = mtod(m, struct ipoib_header *);
1366 (void)memcpy(&eh->proto, &type, sizeof(eh->proto));
1367 (void)memcpy(&eh->hwaddr, edst, sizeof (edst));
1370 * Queue message on interface, update output statistics if
1371 * successful, and start output if interface not yet active.
1373 return ((ifp->if_transmit)(ifp, m));
1381 * Upper layer processing for a received Infiniband packet.
1384 ipoib_demux(struct ifnet *ifp, struct mbuf *m, u_short proto)
1390 * Tag the mbuf with an appropriate MAC label before any other
1391 * consumers can get to it.
1393 mac_ifnet_create_mbuf(ifp, m);
1395 /* Allow monitor mode to claim this frame, after stats are updated. */
1396 if (ifp->if_flags & IFF_MONITOR) {
1397 if_printf(ifp, "discard frame at IFF_MONITOR\n");
1402 * Dispatch frame to upper layer.
1411 if (ifp->if_flags & IFF_NOARP) {
1412 /* Discard packet if ARP is disabled on interface */
1420 case ETHERTYPE_IPV6:
1427 netisr_dispatch(isr, m);
1435 * Process a received Infiniband packet.
1438 ipoib_input(struct ifnet *ifp, struct mbuf *m)
1440 struct ipoib_header *eh;
1442 if ((ifp->if_flags & IFF_UP) == 0) {
1446 CURVNET_SET_QUIET(ifp->if_vnet);
1448 /* Let BPF have it before we strip the header. */
1450 eh = mtod(m, struct ipoib_header *);
1452 * Reset layer specific mbuf flags to avoid confusing upper layers.
1453 * Strip off Infiniband header.
1455 m->m_flags &= ~M_VLANTAG;
1457 m_adj(m, IPOIB_HEADER_LEN);
1459 if (IPOIB_IS_MULTICAST(eh->hwaddr)) {
1460 if (memcmp(eh->hwaddr, ifp->if_broadcastaddr,
1461 ifp->if_addrlen) == 0)
1462 m->m_flags |= M_BCAST;
1464 m->m_flags |= M_MCAST;
1465 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
1468 ipoib_demux(ifp, m, ntohs(eh->proto));
1473 ipoib_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1474 struct sockaddr *sa)
1476 struct sockaddr_dl *sdl;
1478 struct sockaddr_in *sin;
1481 struct sockaddr_in6 *sin6;
1485 switch(sa->sa_family) {
1488 * No mapping needed. Just check that it's a valid MC address.
1490 sdl = (struct sockaddr_dl *)sa;
1491 e_addr = LLADDR(sdl);
1492 if (!IPOIB_IS_MULTICAST(e_addr))
1493 return EADDRNOTAVAIL;
1499 sin = (struct sockaddr_in *)sa;
1500 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1501 return EADDRNOTAVAIL;
1502 sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND);
1503 sdl->sdl_alen = INFINIBAND_ALEN;
1504 e_addr = LLADDR(sdl);
1505 ip_ib_mc_map(sin->sin_addr.s_addr, ifp->if_broadcastaddr,
1507 *llsa = (struct sockaddr *)sdl;
1512 sin6 = (struct sockaddr_in6 *)sa;
1514 * An IP6 address of 0 means listen to all
1515 * of the multicast address used for IP6.
1516 * This has no meaning in ipoib.
1518 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
1519 return EADDRNOTAVAIL;
1520 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1521 return EADDRNOTAVAIL;
1522 sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND);
1523 sdl->sdl_alen = INFINIBAND_ALEN;
1524 e_addr = LLADDR(sdl);
1525 ipv6_ib_mc_map(&sin6->sin6_addr, ifp->if_broadcastaddr, e_addr);
1526 *llsa = (struct sockaddr *)sdl;
1531 return EAFNOSUPPORT;
1535 module_init(ipoib_init_module);
1536 module_exit(ipoib_cleanup_module);
1539 ipoib_evhand(module_t mod, int event, void *arg)
1544 static moduledata_t ipoib_mod = {
1546 .evhand = ipoib_evhand,
1549 DECLARE_MODULE(ipoib, ipoib_mod, SI_SUB_LAST, SI_ORDER_ANY);
1550 MODULE_DEPEND(ipoib, ibcore, 1, 1, 1);
1551 MODULE_DEPEND(ipoib, linuxkpi, 1, 1, 1);