2 * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
4 * Copyright (c) 2004 Topspin Communications. All rights reserved.
5 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
6 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
8 * This software is available to you under a choice of one of two
9 * licenses. You may choose to be licensed under the terms of the GNU
10 * General Public License (GPL) Version 2, available from the file
11 * COPYING in the main directory of this source tree, or the
12 * OpenIB.org BSD license below:
14 * Redistribution and use in source and binary forms, with or
15 * without modification, are permitted provided that the following
18 * - Redistributions of source code must retain the above
19 * copyright notice, this list of conditions and the following
22 * - Redistributions in binary form must reproduce the above
23 * copyright notice, this list of conditions and the following
24 * disclaimer in the documentation and/or other materials
25 * provided with the distribution.
27 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
28 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
29 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
30 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
31 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
32 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
33 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
42 static int ipoib_resolvemulti(struct ifnet *, struct sockaddr **,
46 #include <linux/module.h>
48 #include <linux/slab.h>
49 #include <linux/kernel.h>
50 #include <linux/vmalloc.h>
52 #include <linux/if_arp.h> /* For ARPHRD_xxx */
53 #include <linux/if_vlan.h>
57 MODULE_AUTHOR("Roland Dreier");
58 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
59 MODULE_LICENSE("Dual BSD/GPL");
61 int ipoib_sendq_size = IPOIB_TX_RING_SIZE;
62 int ipoib_recvq_size = IPOIB_RX_RING_SIZE;
64 module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
65 MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
66 module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
67 MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
69 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
70 int ipoib_debug_level = 1;
72 module_param_named(debug_level, ipoib_debug_level, int, 0644);
73 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
76 struct ipoib_path_iter {
77 struct ipoib_dev_priv *priv;
78 struct ipoib_path path;
81 static const u8 ipv4_bcast_addr[] = {
82 0x00, 0xff, 0xff, 0xff,
83 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
84 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
87 struct workqueue_struct *ipoib_workqueue;
89 struct ib_sa_client ipoib_sa_client;
91 static void ipoib_add_one(struct ib_device *device);
92 static void ipoib_remove_one(struct ib_device *device, void *client_data);
93 static void ipoib_start(struct ifnet *dev);
94 static int ipoib_output(struct ifnet *ifp, struct mbuf *m,
95 const struct sockaddr *dst, struct route *ro);
96 static int ipoib_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
97 static void ipoib_input(struct ifnet *ifp, struct mbuf *m);
99 #define IPOIB_MTAP(_ifp, _m) \
101 if (bpf_peers_present((_ifp)->if_bpf)) { \
103 ipoib_mtap_mb((_ifp), (_m)); \
107 static struct unrhdr *ipoib_unrhdr;
110 ipoib_unrhdr_init(void *arg)
113 ipoib_unrhdr = new_unrhdr(0, 65535, NULL);
115 SYSINIT(ipoib_unrhdr_init, SI_SUB_KLD - 1, SI_ORDER_ANY, ipoib_unrhdr_init, NULL);
118 ipoib_unrhdr_uninit(void *arg)
121 if (ipoib_unrhdr != NULL) {
130 SYSUNINIT(ipoib_unrhdr_uninit, SI_SUB_KLD - 1, SI_ORDER_ANY, ipoib_unrhdr_uninit, NULL);
133 * This is for clients that have an ipoib_header in the mbuf.
136 ipoib_mtap_mb(struct ifnet *ifp, struct mbuf *mb)
138 struct ipoib_header *ih;
139 struct ether_header eh;
141 ih = mtod(mb, struct ipoib_header *);
142 eh.ether_type = ih->proto;
143 bcopy(ih->hwaddr, &eh.ether_dhost, ETHER_ADDR_LEN);
144 bzero(&eh.ether_shost, ETHER_ADDR_LEN);
145 mb->m_data += sizeof(struct ipoib_header);
146 mb->m_len -= sizeof(struct ipoib_header);
147 bpf_mtap2(ifp->if_bpf, &eh, sizeof(eh), mb);
148 mb->m_data -= sizeof(struct ipoib_header);
149 mb->m_len += sizeof(struct ipoib_header);
153 ipoib_mtap_proto(struct ifnet *ifp, struct mbuf *mb, uint16_t proto)
155 struct ether_header eh;
157 eh.ether_type = proto;
158 bzero(&eh.ether_shost, ETHER_ADDR_LEN);
159 bzero(&eh.ether_dhost, ETHER_ADDR_LEN);
160 bpf_mtap2(ifp->if_bpf, &eh, sizeof(eh), mb);
163 static struct ib_client ipoib_client = {
165 .add = ipoib_add_one,
166 .remove = ipoib_remove_one
170 ipoib_open(struct ipoib_dev_priv *priv)
172 struct ifnet *dev = priv->dev;
174 ipoib_dbg(priv, "bringing up interface\n");
176 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
178 if (ipoib_pkey_dev_delay_open(priv))
181 if (ipoib_ib_dev_open(priv))
184 if (ipoib_ib_dev_up(priv))
187 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
188 struct ipoib_dev_priv *cpriv;
190 /* Bring up any child interfaces too */
191 mutex_lock(&priv->vlan_mutex);
192 list_for_each_entry(cpriv, &priv->child_intfs, list)
193 if ((cpriv->dev->if_drv_flags & IFF_DRV_RUNNING) == 0)
195 mutex_unlock(&priv->vlan_mutex);
197 dev->if_drv_flags |= IFF_DRV_RUNNING;
198 dev->if_drv_flags &= ~IFF_DRV_OACTIVE;
203 ipoib_ib_dev_stop(priv, 1);
206 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
212 ipoib_init(void *arg)
215 struct ipoib_dev_priv *priv;
219 if ((dev->if_drv_flags & IFF_DRV_RUNNING) == 0)
221 queue_work(ipoib_workqueue, &priv->flush_light);
226 ipoib_stop(struct ipoib_dev_priv *priv)
228 struct ifnet *dev = priv->dev;
230 ipoib_dbg(priv, "stopping interface\n");
232 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
234 dev->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
236 ipoib_ib_dev_down(priv, 0);
237 ipoib_ib_dev_stop(priv, 0);
239 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
240 struct ipoib_dev_priv *cpriv;
242 /* Bring down any child interfaces too */
243 mutex_lock(&priv->vlan_mutex);
244 list_for_each_entry(cpriv, &priv->child_intfs, list)
245 if ((cpriv->dev->if_drv_flags & IFF_DRV_RUNNING) != 0)
247 mutex_unlock(&priv->vlan_mutex);
254 ipoib_change_mtu(struct ipoib_dev_priv *priv, int new_mtu)
256 struct ifnet *dev = priv->dev;
258 /* dev->if_mtu > 2K ==> connected mode */
259 if (ipoib_cm_admin_enabled(priv)) {
260 if (new_mtu > IPOIB_CM_MTU(ipoib_cm_max_mtu(priv)))
263 if (new_mtu > priv->mcast_mtu)
264 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
267 dev->if_mtu = new_mtu;
271 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
274 priv->admin_mtu = new_mtu;
276 dev->if_mtu = min(priv->mcast_mtu, priv->admin_mtu);
278 queue_work(ipoib_workqueue, &priv->flush_light);
284 ipoib_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
286 struct ipoib_dev_priv *priv = ifp->if_softc;
287 struct ifaddr *ifa = (struct ifaddr *) data;
288 struct ifreq *ifr = (struct ifreq *) data;
291 /* check if detaching */
292 if (priv == NULL || priv->gone != 0)
297 if (ifp->if_flags & IFF_UP) {
298 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
299 error = -ipoib_open(priv);
301 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
306 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
307 queue_work(ipoib_workqueue, &priv->restart_task);
310 ifp->if_flags |= IFF_UP;
312 switch (ifa->ifa_addr->sa_family) {
315 ifp->if_init(ifp->if_softc); /* before arpwhohas */
316 arp_ifinit(ifp, ifa);
320 ifp->if_init(ifp->if_softc);
326 bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
332 * Set the interface MTU.
334 error = -ipoib_change_mtu(priv, ifr->ifr_mtu);
344 static struct ipoib_path *
345 __path_find(struct ipoib_dev_priv *priv, void *gid)
347 struct rb_node *n = priv->path_tree.rb_node;
348 struct ipoib_path *path;
352 path = rb_entry(n, struct ipoib_path, rb_node);
354 ret = memcmp(gid, path->pathrec.dgid.raw,
355 sizeof (union ib_gid));
369 __path_add(struct ipoib_dev_priv *priv, struct ipoib_path *path)
371 struct rb_node **n = &priv->path_tree.rb_node;
372 struct rb_node *pn = NULL;
373 struct ipoib_path *tpath;
378 tpath = rb_entry(pn, struct ipoib_path, rb_node);
380 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
381 sizeof (union ib_gid));
390 rb_link_node(&path->rb_node, pn, n);
391 rb_insert_color(&path->rb_node, &priv->path_tree);
393 list_add_tail(&path->list, &priv->path_list);
399 ipoib_path_free(struct ipoib_dev_priv *priv, struct ipoib_path *path)
402 _IF_DRAIN(&path->queue);
405 ipoib_put_ah(path->ah);
406 if (ipoib_cm_get(path))
407 ipoib_cm_destroy_tx(ipoib_cm_get(path));
412 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
414 struct ipoib_path_iter *
415 ipoib_path_iter_init(struct ipoib_dev_priv *priv)
417 struct ipoib_path_iter *iter;
419 iter = kmalloc(sizeof *iter, GFP_KERNEL);
424 memset(iter->path.pathrec.dgid.raw, 0, 16);
426 if (ipoib_path_iter_next(iter)) {
435 ipoib_path_iter_next(struct ipoib_path_iter *iter)
437 struct ipoib_dev_priv *priv = iter->priv;
439 struct ipoib_path *path;
442 spin_lock_irq(&priv->lock);
444 n = rb_first(&priv->path_tree);
447 path = rb_entry(n, struct ipoib_path, rb_node);
449 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
450 sizeof (union ib_gid)) < 0) {
459 spin_unlock_irq(&priv->lock);
465 ipoib_path_iter_read(struct ipoib_path_iter *iter, struct ipoib_path *path)
470 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
473 ipoib_mark_paths_invalid(struct ipoib_dev_priv *priv)
475 struct ipoib_path *path, *tp;
477 spin_lock_irq(&priv->lock);
479 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
480 ipoib_dbg(priv, "mark path LID 0x%04x GID %16D invalid\n",
481 be16_to_cpu(path->pathrec.dlid),
482 path->pathrec.dgid.raw, ":");
486 spin_unlock_irq(&priv->lock);
490 ipoib_flush_paths(struct ipoib_dev_priv *priv)
492 struct ipoib_path *path, *tp;
493 LIST_HEAD(remove_list);
496 spin_lock_irqsave(&priv->lock, flags);
498 list_splice_init(&priv->path_list, &remove_list);
500 list_for_each_entry(path, &remove_list, list)
501 rb_erase(&path->rb_node, &priv->path_tree);
503 list_for_each_entry_safe(path, tp, &remove_list, list) {
505 ib_sa_cancel_query(path->query_id, path->query);
506 spin_unlock_irqrestore(&priv->lock, flags);
507 wait_for_completion(&path->done);
508 ipoib_path_free(priv, path);
509 spin_lock_irqsave(&priv->lock, flags);
512 spin_unlock_irqrestore(&priv->lock, flags);
516 path_rec_completion(int status, struct ib_sa_path_rec *pathrec, void *path_ptr)
518 struct ipoib_path *path = path_ptr;
519 struct ipoib_dev_priv *priv = path->priv;
520 struct ifnet *dev = priv->dev;
521 struct ipoib_ah *ah = NULL;
522 struct ipoib_ah *old_ah = NULL;
523 struct ifqueue mbqueue;
528 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %16D\n",
529 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw, ":");
531 ipoib_dbg(priv, "PathRec status %d for GID %16D\n",
532 status, path->pathrec.dgid.raw, ":");
534 bzero(&mbqueue, sizeof(mbqueue));
537 struct ib_ah_attr av;
539 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
540 ah = ipoib_create_ah(priv, priv->pd, &av);
543 spin_lock_irqsave(&priv->lock, flags);
546 path->pathrec = *pathrec;
551 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
552 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
555 _IF_DEQUEUE(&path->queue, mb);
558 _IF_ENQUEUE(&mbqueue, mb);
561 #ifdef CONFIG_INFINIBAND_IPOIB_CM
562 if (ipoib_cm_enabled(priv, path->hwaddr) && !ipoib_cm_get(path))
563 ipoib_cm_set(path, ipoib_cm_create_tx(priv, path));
570 complete(&path->done);
572 spin_unlock_irqrestore(&priv->lock, flags);
575 ipoib_put_ah(old_ah);
578 _IF_DEQUEUE(&mbqueue, mb);
581 mb->m_pkthdr.rcvif = dev;
582 if (dev->if_transmit(dev, mb))
583 ipoib_warn(priv, "dev_queue_xmit failed "
584 "to requeue packet\n");
588 static struct ipoib_path *
589 path_rec_create(struct ipoib_dev_priv *priv, uint8_t *hwaddr)
591 struct ipoib_path *path;
593 if (!priv->broadcast)
596 path = kzalloc(sizeof *path, GFP_ATOMIC);
602 bzero(&path->queue, sizeof(path->queue));
604 #ifdef CONFIG_INFINIBAND_IPOIB_CM
605 memcpy(&path->hwaddr, hwaddr, INFINIBAND_ALEN);
607 memcpy(path->pathrec.dgid.raw, &hwaddr[4], sizeof (union ib_gid));
608 path->pathrec.sgid = priv->local_gid;
609 path->pathrec.pkey = cpu_to_be16(priv->pkey);
610 path->pathrec.numb_path = 1;
611 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
617 path_rec_start(struct ipoib_dev_priv *priv, struct ipoib_path *path)
619 struct ifnet *dev = priv->dev;
621 ib_sa_comp_mask comp_mask = IB_SA_PATH_REC_MTU_SELECTOR | IB_SA_PATH_REC_MTU;
622 struct ib_sa_path_rec p_rec;
624 p_rec = path->pathrec;
625 p_rec.mtu_selector = IB_SA_GT;
627 switch (roundup_pow_of_two(dev->if_mtu + IPOIB_ENCAP_LEN)) {
629 p_rec.mtu = IB_MTU_256;
632 p_rec.mtu = IB_MTU_512;
635 p_rec.mtu = IB_MTU_1024;
638 p_rec.mtu = IB_MTU_2048;
641 /* Wildcard everything */
644 p_rec.mtu_selector = 0;
647 ipoib_dbg(priv, "Start path record lookup for %16D MTU > %d\n",
649 comp_mask ? ib_mtu_enum_to_int(p_rec.mtu) : 0);
651 init_completion(&path->done);
654 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
656 IB_SA_PATH_REC_DGID |
657 IB_SA_PATH_REC_SGID |
658 IB_SA_PATH_REC_NUMB_PATH |
659 IB_SA_PATH_REC_TRAFFIC_CLASS |
664 if (path->query_id < 0) {
665 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
667 complete(&path->done);
668 return path->query_id;
675 ipoib_unicast_send(struct mbuf *mb, struct ipoib_dev_priv *priv, struct ipoib_header *eh)
677 struct ipoib_path *path;
679 path = __path_find(priv, eh->hwaddr + 4);
680 if (!path || !path->valid) {
684 path = path_rec_create(priv, eh->hwaddr);
688 if (_IF_QLEN(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE)
689 _IF_ENQUEUE(&path->queue, mb);
691 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
695 if (!path->query && path_rec_start(priv, path)) {
696 spin_unlock_irqrestore(&priv->lock, flags);
698 ipoib_path_free(priv, path);
701 __path_add(priv, path);
703 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
710 if (ipoib_cm_get(path) && ipoib_cm_up(path)) {
711 ipoib_cm_send(priv, mb, ipoib_cm_get(path));
712 } else if (path->ah) {
713 ipoib_send(priv, mb, path->ah, IPOIB_QPN(eh->hwaddr));
714 } else if ((path->query || !path_rec_start(priv, path)) &&
715 path->queue.ifq_len < IPOIB_MAX_PATH_REC_QUEUE) {
716 _IF_ENQUEUE(&path->queue, mb);
718 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
724 ipoib_send_one(struct ipoib_dev_priv *priv, struct mbuf *mb)
726 struct ipoib_header *eh;
728 eh = mtod(mb, struct ipoib_header *);
729 if (IPOIB_IS_MULTICAST(eh->hwaddr)) {
730 /* Add in the P_Key for multicast*/
731 eh->hwaddr[8] = (priv->pkey >> 8) & 0xff;
732 eh->hwaddr[9] = priv->pkey & 0xff;
734 ipoib_mcast_send(priv, eh->hwaddr + 4, mb);
736 ipoib_unicast_send(mb, priv, eh);
743 _ipoib_start(struct ifnet *dev, struct ipoib_dev_priv *priv)
747 if ((dev->if_drv_flags & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
751 spin_lock(&priv->lock);
752 while (!IFQ_DRV_IS_EMPTY(&dev->if_snd) &&
753 (dev->if_drv_flags & IFF_DRV_OACTIVE) == 0) {
754 IFQ_DRV_DEQUEUE(&dev->if_snd, mb);
758 ipoib_send_one(priv, mb);
760 spin_unlock(&priv->lock);
764 ipoib_start(struct ifnet *dev)
766 _ipoib_start(dev, dev->if_softc);
770 ipoib_vlan_start(struct ifnet *dev)
772 struct ipoib_dev_priv *priv;
775 priv = VLAN_COOKIE(dev);
777 return _ipoib_start(dev, priv);
778 while (!IFQ_DRV_IS_EMPTY(&dev->if_snd)) {
779 IFQ_DRV_DEQUEUE(&dev->if_snd, mb);
783 if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
788 ipoib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca, int port)
791 /* Allocate RX/TX "rings" to hold queued mbs */
792 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
794 if (!priv->rx_ring) {
795 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
796 ca->name, ipoib_recvq_size);
800 priv->tx_ring = kzalloc(ipoib_sendq_size * sizeof *priv->tx_ring, GFP_KERNEL);
801 if (!priv->tx_ring) {
802 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
803 ca->name, ipoib_sendq_size);
804 goto out_rx_ring_cleanup;
806 memset(priv->tx_ring, 0, ipoib_sendq_size * sizeof *priv->tx_ring);
808 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
810 if (ipoib_ib_dev_init(priv, ca, port))
811 goto out_tx_ring_cleanup;
816 kfree(priv->tx_ring);
819 kfree(priv->rx_ring);
826 ipoib_detach(struct ipoib_dev_priv *priv)
831 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
836 free_unr(ipoib_unrhdr, priv->unit);
838 VLAN_SETCOOKIE(priv->dev, NULL);
844 ipoib_dev_cleanup(struct ipoib_dev_priv *priv)
846 struct ipoib_dev_priv *cpriv, *tcpriv;
848 /* Delete any child interfaces first */
849 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
850 ipoib_dev_cleanup(cpriv);
854 ipoib_ib_dev_cleanup(priv);
856 kfree(priv->rx_ring);
857 kfree(priv->tx_ring);
859 priv->rx_ring = NULL;
860 priv->tx_ring = NULL;
863 static struct ipoib_dev_priv *
864 ipoib_priv_alloc(void)
866 struct ipoib_dev_priv *priv;
868 priv = malloc(sizeof(struct ipoib_dev_priv), M_TEMP, M_ZERO|M_WAITOK);
869 spin_lock_init(&priv->lock);
870 spin_lock_init(&priv->drain_lock);
871 mutex_init(&priv->vlan_mutex);
872 INIT_LIST_HEAD(&priv->path_list);
873 INIT_LIST_HEAD(&priv->child_intfs);
874 INIT_LIST_HEAD(&priv->dead_ahs);
875 INIT_LIST_HEAD(&priv->multicast_list);
876 INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
877 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
878 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
879 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
880 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
881 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
882 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
883 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
884 memcpy(priv->broadcastaddr, ipv4_bcast_addr, INFINIBAND_ALEN);
889 struct ipoib_dev_priv *
890 ipoib_intf_alloc(const char *name)
892 struct ipoib_dev_priv *priv;
893 struct sockaddr_dl *sdl;
896 priv = ipoib_priv_alloc();
897 dev = priv->dev = if_alloc(IFT_INFINIBAND);
902 dev->if_softc = priv;
903 priv->unit = alloc_unr(ipoib_unrhdr);
904 if (priv->unit == -1) {
909 if_initname(dev, name, priv->unit);
910 dev->if_flags = IFF_BROADCAST | IFF_MULTICAST;
911 dev->if_addrlen = INFINIBAND_ALEN;
912 dev->if_hdrlen = IPOIB_HEADER_LEN;
914 dev->if_init = ipoib_init;
915 dev->if_ioctl = ipoib_ioctl;
916 dev->if_start = ipoib_start;
917 dev->if_output = ipoib_output;
918 dev->if_input = ipoib_input;
919 dev->if_resolvemulti = ipoib_resolvemulti;
920 dev->if_baudrate = IF_Gbps(10);
921 dev->if_broadcastaddr = priv->broadcastaddr;
922 dev->if_snd.ifq_maxlen = ipoib_sendq_size * 2;
923 sdl = (struct sockaddr_dl *)dev->if_addr->ifa_addr;
924 sdl->sdl_type = IFT_INFINIBAND;
925 sdl->sdl_alen = dev->if_addrlen;
927 if_link_state_change(dev, LINK_STATE_DOWN);
928 bpfattach(dev, DLT_EN10MB, ETHER_HDR_LEN);
930 return dev->if_softc;
934 ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
936 struct ib_device_attr *device_attr = &hca->attrs;
938 priv->hca_caps = device_attr->device_cap_flags;
940 priv->dev->if_hwassist = 0;
941 priv->dev->if_capabilities = 0;
943 #ifndef CONFIG_INFINIBAND_IPOIB_CM
944 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
945 set_bit(IPOIB_FLAG_CSUM, &priv->flags);
946 priv->dev->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP;
947 priv->dev->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM;
951 if (priv->dev->features & NETIF_F_SG && priv->hca_caps & IB_DEVICE_UD_TSO) {
952 priv->dev->if_capabilities |= IFCAP_TSO4;
953 priv->dev->if_hwassist |= CSUM_TSO;
957 priv->dev->if_capabilities |=
958 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_LINKSTATE;
959 priv->dev->if_capenable = priv->dev->if_capabilities;
965 static struct ifnet *
966 ipoib_add_port(const char *format, struct ib_device *hca, u8 port)
968 struct ipoib_dev_priv *priv;
969 struct ib_port_attr attr;
970 int result = -ENOMEM;
972 priv = ipoib_intf_alloc(format);
974 goto alloc_mem_failed;
976 if (!ib_query_port(hca, port, &attr))
977 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
979 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
981 goto device_init_failed;
984 /* MTU will be reset when mcast join happens */
985 priv->dev->if_mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
986 priv->mcast_mtu = priv->admin_mtu = priv->dev->if_mtu;
988 result = ib_query_pkey(hca, port, 0, &priv->pkey);
990 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
991 hca->name, port, result);
992 goto device_init_failed;
995 if (ipoib_set_dev_features(priv, hca))
996 goto device_init_failed;
999 * Set the full membership bit, so that we join the right
1000 * broadcast group, etc.
1002 priv->pkey |= 0x8000;
1004 priv->broadcastaddr[8] = priv->pkey >> 8;
1005 priv->broadcastaddr[9] = priv->pkey & 0xff;
1007 result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
1009 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1010 hca->name, port, result);
1011 goto device_init_failed;
1013 memcpy(IF_LLADDR(priv->dev) + 4, priv->local_gid.raw, sizeof (union ib_gid));
1015 result = ipoib_dev_init(priv, hca, port);
1017 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1018 hca->name, port, result);
1019 goto device_init_failed;
1021 if (ipoib_cm_admin_enabled(priv))
1022 priv->dev->if_mtu = IPOIB_CM_MTU(ipoib_cm_max_mtu(priv));
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);
1033 if_printf(priv->dev, "Attached to %s port %d\n", hca->name, port);
1038 ipoib_dev_cleanup(priv);
1044 return ERR_PTR(result);
1048 ipoib_add_one(struct ib_device *device)
1050 struct list_head *dev_list;
1052 struct ipoib_dev_priv *priv;
1055 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1058 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1062 INIT_LIST_HEAD(dev_list);
1064 if (device->node_type == RDMA_NODE_IB_SWITCH) {
1069 e = device->phys_port_cnt;
1072 for (p = s; p <= e; ++p) {
1073 if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
1075 dev = ipoib_add_port("ib", device, p);
1077 priv = dev->if_softc;
1078 list_add_tail(&priv->list, dev_list);
1082 ib_set_client_data(device, &ipoib_client, dev_list);
1086 ipoib_remove_one(struct ib_device *device, void *client_data)
1088 struct ipoib_dev_priv *priv, *tmp;
1089 struct list_head *dev_list = client_data;
1094 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1097 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1098 if (rdma_port_get_link_layer(device, priv->port) != IB_LINK_LAYER_INFINIBAND)
1103 ib_unregister_event_handler(&priv->event_handler);
1105 /* dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP); */
1107 flush_workqueue(ipoib_workqueue);
1109 ipoib_dev_cleanup(priv);
1117 ipoib_config_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
1119 struct ipoib_dev_priv *parent;
1120 struct ipoib_dev_priv *priv;
1125 if (ifp->if_type != IFT_INFINIBAND)
1127 dev = VLAN_DEVAT(ifp, vtag);
1132 parent = ifp->if_softc;
1133 /* We only support 15 bits of pkey. */
1136 pkey = vtag | 0x8000; /* Set full membership bit. */
1137 if (pkey == parent->pkey)
1139 /* Check for dups */
1140 mutex_lock(&parent->vlan_mutex);
1141 list_for_each_entry(priv, &parent->child_intfs, list) {
1142 if (priv->pkey == pkey) {
1148 priv = ipoib_priv_alloc();
1150 priv->max_ib_mtu = parent->max_ib_mtu;
1151 priv->mcast_mtu = priv->admin_mtu = parent->dev->if_mtu;
1152 set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
1153 error = ipoib_set_dev_features(priv, parent->ca);
1157 priv->broadcastaddr[8] = pkey >> 8;
1158 priv->broadcastaddr[9] = pkey & 0xff;
1159 dev->if_broadcastaddr = priv->broadcastaddr;
1160 error = ipoib_dev_init(priv, parent->ca, parent->port);
1163 priv->parent = parent->dev;
1164 list_add_tail(&priv->list, &parent->child_intfs);
1165 VLAN_SETCOOKIE(dev, priv);
1166 dev->if_start = ipoib_vlan_start;
1167 dev->if_drv_flags &= ~IFF_DRV_RUNNING;
1168 dev->if_hdrlen = IPOIB_HEADER_LEN;
1169 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1171 mutex_unlock(&parent->vlan_mutex);
1174 mutex_unlock(&parent->vlan_mutex);
1179 "failed to initialize subinterface: device %s, port %d vtag 0x%X",
1180 parent->ca->name, parent->port, vtag);
1185 ipoib_unconfig_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
1187 struct ipoib_dev_priv *parent;
1188 struct ipoib_dev_priv *priv;
1192 if (ifp->if_type != IFT_INFINIBAND)
1195 dev = VLAN_DEVAT(ifp, vtag);
1197 VLAN_SETCOOKIE(dev, NULL);
1198 pkey = vtag | 0x8000;
1199 parent = ifp->if_softc;
1200 mutex_lock(&parent->vlan_mutex);
1201 list_for_each_entry(priv, &parent->child_intfs, list) {
1202 if (priv->pkey == pkey) {
1203 ipoib_dev_cleanup(priv);
1204 list_del(&priv->list);
1208 mutex_unlock(&parent->vlan_mutex);
1211 eventhandler_tag ipoib_vlan_attach;
1212 eventhandler_tag ipoib_vlan_detach;
1215 ipoib_init_module(void)
1219 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1220 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1221 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1223 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1224 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1225 ipoib_sendq_size = max(ipoib_sendq_size, max(2 * MAX_SEND_CQE,
1226 IPOIB_MIN_QUEUE_SIZE));
1227 #ifdef CONFIG_INFINIBAND_IPOIB_CM
1228 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1231 ipoib_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
1232 ipoib_config_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1233 ipoib_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
1234 ipoib_unconfig_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1237 * We create our own workqueue mainly because we want to be
1238 * able to flush it when devices are being removed. We can't
1239 * use schedule_work()/flush_scheduled_work() because both
1240 * unregister_netdev() and linkwatch_event take the rtnl lock,
1241 * so flush_scheduled_work() can deadlock during device
1244 ipoib_workqueue = create_singlethread_workqueue("ipoib");
1245 if (!ipoib_workqueue) {
1250 ib_sa_register_client(&ipoib_sa_client);
1252 ret = ib_register_client(&ipoib_client);
1259 ib_sa_unregister_client(&ipoib_sa_client);
1260 destroy_workqueue(ipoib_workqueue);
1267 ipoib_cleanup_module(void)
1270 EVENTHANDLER_DEREGISTER(vlan_config, ipoib_vlan_attach);
1271 EVENTHANDLER_DEREGISTER(vlan_unconfig, ipoib_vlan_detach);
1272 ib_unregister_client(&ipoib_client);
1273 ib_sa_unregister_client(&ipoib_sa_client);
1274 destroy_workqueue(ipoib_workqueue);
1278 * Infiniband output routine.
1281 ipoib_output(struct ifnet *ifp, struct mbuf *m,
1282 const struct sockaddr *dst, struct route *ro)
1284 u_char edst[INFINIBAND_ALEN];
1285 #if defined(INET) || defined(INET6)
1286 struct llentry *lle = NULL;
1288 struct ipoib_header *eh;
1289 int error = 0, is_gw = 0;
1293 is_gw = (ro->ro_flags & RT_HAS_GW) != 0;
1295 error = mac_ifnet_check_transmit(ifp, m);
1301 if (ifp->if_flags & IFF_MONITOR) {
1305 if (!((ifp->if_flags & IFF_UP) &&
1306 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
1311 switch (dst->sa_family) {
1314 if (lle != NULL && (lle->la_flags & LLE_VALID))
1315 memcpy(edst, lle->ll_addr, sizeof(edst));
1316 else if (m->m_flags & M_MCAST)
1317 ip_ib_mc_map(((struct sockaddr_in *)dst)->sin_addr.s_addr, ifp->if_broadcastaddr, edst);
1319 error = arpresolve(ifp, is_gw, m, dst, edst, NULL, NULL);
1321 return (error == EWOULDBLOCK ? 0 : error);
1322 type = htons(ETHERTYPE_IP);
1327 ah = mtod(m, struct arphdr *);
1328 ah->ar_hrd = htons(ARPHRD_INFINIBAND);
1330 switch(ntohs(ah->ar_op)) {
1331 case ARPOP_REVREQUEST:
1332 case ARPOP_REVREPLY:
1333 type = htons(ETHERTYPE_REVARP);
1338 type = htons(ETHERTYPE_ARP);
1342 if (m->m_flags & M_BCAST)
1343 bcopy(ifp->if_broadcastaddr, edst, INFINIBAND_ALEN);
1345 bcopy(ar_tha(ah), edst, INFINIBAND_ALEN);
1352 if (lle != NULL && (lle->la_flags & LLE_VALID))
1353 memcpy(edst, lle->ll_addr, sizeof(edst));
1354 else if (m->m_flags & M_MCAST)
1355 ipv6_ib_mc_map(&((struct sockaddr_in6 *)dst)->sin6_addr, ifp->if_broadcastaddr, edst);
1357 error = nd6_resolve(ifp, is_gw, m, dst, edst, NULL, NULL);
1360 type = htons(ETHERTYPE_IPV6);
1365 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
1366 error = EAFNOSUPPORT;
1371 * Add local net header. If no space in first mbuf,
1374 M_PREPEND(m, IPOIB_HEADER_LEN, M_NOWAIT);
1379 eh = mtod(m, struct ipoib_header *);
1380 (void)memcpy(&eh->proto, &type, sizeof(eh->proto));
1381 (void)memcpy(&eh->hwaddr, edst, sizeof (edst));
1384 * Queue message on interface, update output statistics if
1385 * successful, and start output if interface not yet active.
1387 return ((ifp->if_transmit)(ifp, m));
1395 * Upper layer processing for a received Infiniband packet.
1398 ipoib_demux(struct ifnet *ifp, struct mbuf *m, u_short proto)
1404 * Tag the mbuf with an appropriate MAC label before any other
1405 * consumers can get to it.
1407 mac_ifnet_create_mbuf(ifp, m);
1409 /* Allow monitor mode to claim this frame, after stats are updated. */
1410 if (ifp->if_flags & IFF_MONITOR) {
1411 if_printf(ifp, "discard frame at IFF_MONITOR\n");
1416 * Dispatch frame to upper layer.
1425 if (ifp->if_flags & IFF_NOARP) {
1426 /* Discard packet if ARP is disabled on interface */
1434 case ETHERTYPE_IPV6:
1441 netisr_dispatch(isr, m);
1449 * Process a received Infiniband packet.
1452 ipoib_input(struct ifnet *ifp, struct mbuf *m)
1454 struct ipoib_header *eh;
1456 if ((ifp->if_flags & IFF_UP) == 0) {
1460 CURVNET_SET_QUIET(ifp->if_vnet);
1462 /* Let BPF have it before we strip the header. */
1464 eh = mtod(m, struct ipoib_header *);
1466 * Reset layer specific mbuf flags to avoid confusing upper layers.
1467 * Strip off Infiniband header.
1469 m->m_flags &= ~M_VLANTAG;
1471 m_adj(m, IPOIB_HEADER_LEN);
1473 if (IPOIB_IS_MULTICAST(eh->hwaddr)) {
1474 if (memcmp(eh->hwaddr, ifp->if_broadcastaddr,
1475 ifp->if_addrlen) == 0)
1476 m->m_flags |= M_BCAST;
1478 m->m_flags |= M_MCAST;
1479 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
1482 ipoib_demux(ifp, m, ntohs(eh->proto));
1487 ipoib_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1488 struct sockaddr *sa)
1490 struct sockaddr_dl *sdl;
1492 struct sockaddr_in *sin;
1495 struct sockaddr_in6 *sin6;
1499 switch(sa->sa_family) {
1502 * No mapping needed. Just check that it's a valid MC address.
1504 sdl = (struct sockaddr_dl *)sa;
1505 e_addr = LLADDR(sdl);
1506 if (!IPOIB_IS_MULTICAST(e_addr))
1507 return EADDRNOTAVAIL;
1513 sin = (struct sockaddr_in *)sa;
1514 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1515 return EADDRNOTAVAIL;
1516 sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND);
1517 sdl->sdl_alen = INFINIBAND_ALEN;
1518 e_addr = LLADDR(sdl);
1519 ip_ib_mc_map(sin->sin_addr.s_addr, ifp->if_broadcastaddr,
1521 *llsa = (struct sockaddr *)sdl;
1526 sin6 = (struct sockaddr_in6 *)sa;
1528 * An IP6 address of 0 means listen to all
1529 * of the multicast address used for IP6.
1530 * This has no meaning in ipoib.
1532 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
1533 return EADDRNOTAVAIL;
1534 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1535 return EADDRNOTAVAIL;
1536 sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND);
1537 sdl->sdl_alen = INFINIBAND_ALEN;
1538 e_addr = LLADDR(sdl);
1539 ipv6_ib_mc_map(&sin6->sin6_addr, ifp->if_broadcastaddr, e_addr);
1540 *llsa = (struct sockaddr *)sdl;
1545 return EAFNOSUPPORT;
1549 module_init(ipoib_init_module);
1550 module_exit(ipoib_cleanup_module);
1553 ipoib_evhand(module_t mod, int event, void *arg)
1558 static moduledata_t ipoib_mod = {
1560 .evhand = ipoib_evhand,
1563 DECLARE_MODULE(ipoib, ipoib_mod, SI_SUB_LAST, SI_ORDER_ANY);
1564 MODULE_DEPEND(ipoib, ibcore, 1, 1, 1);
1565 MODULE_DEPEND(ipoib, linuxkpi, 1, 1, 1);