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
39 static int ipoib_resolvemulti(struct ifnet *, struct sockaddr **,
43 #include <linux/module.h>
45 #include <linux/slab.h>
46 #include <linux/kernel.h>
47 #include <linux/vmalloc.h>
49 #include <linux/if_arp.h> /* For ARPHRD_xxx */
50 #include <linux/if_vlan.h>
54 MODULE_AUTHOR("Roland Dreier");
55 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
56 MODULE_LICENSE("Dual BSD/GPL");
58 int ipoib_sendq_size = IPOIB_TX_RING_SIZE;
59 int ipoib_recvq_size = IPOIB_RX_RING_SIZE;
61 module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
62 MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
63 module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
64 MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
66 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
67 int ipoib_debug_level = 1;
69 module_param_named(debug_level, ipoib_debug_level, int, 0644);
70 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
73 struct ipoib_path_iter {
74 struct ipoib_dev_priv *priv;
75 struct ipoib_path path;
78 static const u8 ipv4_bcast_addr[] = {
79 0x00, 0xff, 0xff, 0xff,
80 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
81 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
84 struct workqueue_struct *ipoib_workqueue;
86 struct ib_sa_client ipoib_sa_client;
88 static void ipoib_add_one(struct ib_device *device);
89 static void ipoib_remove_one(struct ib_device *device, void *client_data);
90 static void ipoib_start(struct ifnet *dev);
91 static int ipoib_output(struct ifnet *ifp, struct mbuf *m,
92 const struct sockaddr *dst, struct route *ro);
93 static int ipoib_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
94 static void ipoib_input(struct ifnet *ifp, struct mbuf *m);
96 #define IPOIB_MTAP(_ifp, _m) \
98 if (bpf_peers_present((_ifp)->if_bpf)) { \
100 ipoib_mtap_mb((_ifp), (_m)); \
104 static struct unrhdr *ipoib_unrhdr;
107 ipoib_unrhdr_init(void *arg)
110 ipoib_unrhdr = new_unrhdr(0, 65535, NULL);
112 SYSINIT(ipoib_unrhdr_init, SI_SUB_KLD - 1, SI_ORDER_ANY, ipoib_unrhdr_init, NULL);
115 ipoib_unrhdr_uninit(void *arg)
118 if (ipoib_unrhdr != NULL) {
127 SYSUNINIT(ipoib_unrhdr_uninit, SI_SUB_KLD - 1, SI_ORDER_ANY, ipoib_unrhdr_uninit, NULL);
130 * This is for clients that have an ipoib_header in the mbuf.
133 ipoib_mtap_mb(struct ifnet *ifp, struct mbuf *mb)
135 struct ipoib_header *ih;
136 struct ether_header eh;
138 ih = mtod(mb, struct ipoib_header *);
139 eh.ether_type = ih->proto;
140 bcopy(ih->hwaddr, &eh.ether_dhost, ETHER_ADDR_LEN);
141 bzero(&eh.ether_shost, ETHER_ADDR_LEN);
142 mb->m_data += sizeof(struct ipoib_header);
143 mb->m_len -= sizeof(struct ipoib_header);
144 bpf_mtap2(ifp->if_bpf, &eh, sizeof(eh), mb);
145 mb->m_data -= sizeof(struct ipoib_header);
146 mb->m_len += sizeof(struct ipoib_header);
150 ipoib_mtap_proto(struct ifnet *ifp, struct mbuf *mb, uint16_t proto)
152 struct ether_header eh;
154 eh.ether_type = proto;
155 bzero(&eh.ether_shost, ETHER_ADDR_LEN);
156 bzero(&eh.ether_dhost, ETHER_ADDR_LEN);
157 bpf_mtap2(ifp->if_bpf, &eh, sizeof(eh), mb);
160 static struct ib_client ipoib_client = {
162 .add = ipoib_add_one,
163 .remove = ipoib_remove_one
167 ipoib_open(struct ipoib_dev_priv *priv)
169 struct ifnet *dev = priv->dev;
171 ipoib_dbg(priv, "bringing up interface\n");
173 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
175 if (ipoib_pkey_dev_delay_open(priv))
178 if (ipoib_ib_dev_open(priv))
181 if (ipoib_ib_dev_up(priv))
184 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
185 struct ipoib_dev_priv *cpriv;
187 /* Bring up any child interfaces too */
188 mutex_lock(&priv->vlan_mutex);
189 list_for_each_entry(cpriv, &priv->child_intfs, list)
190 if ((cpriv->dev->if_drv_flags & IFF_DRV_RUNNING) == 0)
192 mutex_unlock(&priv->vlan_mutex);
194 dev->if_drv_flags |= IFF_DRV_RUNNING;
195 dev->if_drv_flags &= ~IFF_DRV_OACTIVE;
200 ipoib_ib_dev_stop(priv, 1);
203 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
209 ipoib_init(void *arg)
212 struct ipoib_dev_priv *priv;
216 if ((dev->if_drv_flags & IFF_DRV_RUNNING) == 0)
218 queue_work(ipoib_workqueue, &priv->flush_light);
223 ipoib_stop(struct ipoib_dev_priv *priv)
225 struct ifnet *dev = priv->dev;
227 ipoib_dbg(priv, "stopping interface\n");
229 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
231 dev->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
233 ipoib_ib_dev_down(priv, 0);
234 ipoib_ib_dev_stop(priv, 0);
236 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
237 struct ipoib_dev_priv *cpriv;
239 /* Bring down any child interfaces too */
240 mutex_lock(&priv->vlan_mutex);
241 list_for_each_entry(cpriv, &priv->child_intfs, list)
242 if ((cpriv->dev->if_drv_flags & IFF_DRV_RUNNING) != 0)
244 mutex_unlock(&priv->vlan_mutex);
251 ipoib_change_mtu(struct ipoib_dev_priv *priv, int new_mtu)
253 struct ifnet *dev = priv->dev;
255 /* dev->if_mtu > 2K ==> connected mode */
256 if (ipoib_cm_admin_enabled(priv)) {
257 if (new_mtu > IPOIB_CM_MTU(ipoib_cm_max_mtu(priv)))
260 if (new_mtu > priv->mcast_mtu)
261 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
264 dev->if_mtu = new_mtu;
268 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
271 priv->admin_mtu = new_mtu;
273 dev->if_mtu = min(priv->mcast_mtu, priv->admin_mtu);
275 queue_work(ipoib_workqueue, &priv->flush_light);
281 ipoib_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
283 struct ipoib_dev_priv *priv = ifp->if_softc;
284 struct ifaddr *ifa = (struct ifaddr *) data;
285 struct ifreq *ifr = (struct ifreq *) data;
288 /* check if detaching */
289 if (priv == NULL || priv->gone != 0)
294 if (ifp->if_flags & IFF_UP) {
295 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
296 error = -ipoib_open(priv);
298 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
303 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
304 queue_work(ipoib_workqueue, &priv->restart_task);
307 ifp->if_flags |= IFF_UP;
309 switch (ifa->ifa_addr->sa_family) {
312 ifp->if_init(ifp->if_softc); /* before arpwhohas */
313 arp_ifinit(ifp, ifa);
317 ifp->if_init(ifp->if_softc);
323 bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
329 * Set the interface MTU.
331 error = -ipoib_change_mtu(priv, ifr->ifr_mtu);
341 static struct ipoib_path *
342 __path_find(struct ipoib_dev_priv *priv, void *gid)
344 struct rb_node *n = priv->path_tree.rb_node;
345 struct ipoib_path *path;
349 path = rb_entry(n, struct ipoib_path, rb_node);
351 ret = memcmp(gid, path->pathrec.dgid.raw,
352 sizeof (union ib_gid));
366 __path_add(struct ipoib_dev_priv *priv, struct ipoib_path *path)
368 struct rb_node **n = &priv->path_tree.rb_node;
369 struct rb_node *pn = NULL;
370 struct ipoib_path *tpath;
375 tpath = rb_entry(pn, struct ipoib_path, rb_node);
377 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
378 sizeof (union ib_gid));
387 rb_link_node(&path->rb_node, pn, n);
388 rb_insert_color(&path->rb_node, &priv->path_tree);
390 list_add_tail(&path->list, &priv->path_list);
396 ipoib_path_free(struct ipoib_dev_priv *priv, struct ipoib_path *path)
399 _IF_DRAIN(&path->queue);
402 ipoib_put_ah(path->ah);
403 if (ipoib_cm_get(path))
404 ipoib_cm_destroy_tx(ipoib_cm_get(path));
409 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
411 struct ipoib_path_iter *
412 ipoib_path_iter_init(struct ipoib_dev_priv *priv)
414 struct ipoib_path_iter *iter;
416 iter = kmalloc(sizeof *iter, GFP_KERNEL);
421 memset(iter->path.pathrec.dgid.raw, 0, 16);
423 if (ipoib_path_iter_next(iter)) {
432 ipoib_path_iter_next(struct ipoib_path_iter *iter)
434 struct ipoib_dev_priv *priv = iter->priv;
436 struct ipoib_path *path;
439 spin_lock_irq(&priv->lock);
441 n = rb_first(&priv->path_tree);
444 path = rb_entry(n, struct ipoib_path, rb_node);
446 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
447 sizeof (union ib_gid)) < 0) {
456 spin_unlock_irq(&priv->lock);
462 ipoib_path_iter_read(struct ipoib_path_iter *iter, struct ipoib_path *path)
467 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
470 ipoib_mark_paths_invalid(struct ipoib_dev_priv *priv)
472 struct ipoib_path *path, *tp;
474 spin_lock_irq(&priv->lock);
476 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
477 ipoib_dbg(priv, "mark path LID 0x%04x GID %16D invalid\n",
478 be16_to_cpu(path->pathrec.dlid),
479 path->pathrec.dgid.raw, ":");
483 spin_unlock_irq(&priv->lock);
487 ipoib_flush_paths(struct ipoib_dev_priv *priv)
489 struct ipoib_path *path, *tp;
490 LIST_HEAD(remove_list);
493 spin_lock_irqsave(&priv->lock, flags);
495 list_splice_init(&priv->path_list, &remove_list);
497 list_for_each_entry(path, &remove_list, list)
498 rb_erase(&path->rb_node, &priv->path_tree);
500 list_for_each_entry_safe(path, tp, &remove_list, list) {
502 ib_sa_cancel_query(path->query_id, path->query);
503 spin_unlock_irqrestore(&priv->lock, flags);
504 wait_for_completion(&path->done);
505 ipoib_path_free(priv, path);
506 spin_lock_irqsave(&priv->lock, flags);
509 spin_unlock_irqrestore(&priv->lock, flags);
513 path_rec_completion(int status, struct ib_sa_path_rec *pathrec, void *path_ptr)
515 struct ipoib_path *path = path_ptr;
516 struct ipoib_dev_priv *priv = path->priv;
517 struct ifnet *dev = priv->dev;
518 struct ipoib_ah *ah = NULL;
519 struct ipoib_ah *old_ah = NULL;
520 struct ifqueue mbqueue;
525 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %16D\n",
526 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw, ":");
528 ipoib_dbg(priv, "PathRec status %d for GID %16D\n",
529 status, path->pathrec.dgid.raw, ":");
531 bzero(&mbqueue, sizeof(mbqueue));
534 struct ib_ah_attr av;
536 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
537 ah = ipoib_create_ah(priv, priv->pd, &av);
540 spin_lock_irqsave(&priv->lock, flags);
543 path->pathrec = *pathrec;
548 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
549 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
552 _IF_DEQUEUE(&path->queue, mb);
555 _IF_ENQUEUE(&mbqueue, mb);
558 #ifdef CONFIG_INFINIBAND_IPOIB_CM
559 if (ipoib_cm_enabled(priv, path->hwaddr) && !ipoib_cm_get(path))
560 ipoib_cm_set(path, ipoib_cm_create_tx(priv, path));
567 complete(&path->done);
569 spin_unlock_irqrestore(&priv->lock, flags);
572 ipoib_put_ah(old_ah);
575 _IF_DEQUEUE(&mbqueue, mb);
578 mb->m_pkthdr.rcvif = dev;
579 if (dev->if_transmit(dev, mb))
580 ipoib_warn(priv, "dev_queue_xmit failed "
581 "to requeue packet\n");
585 static struct ipoib_path *
586 path_rec_create(struct ipoib_dev_priv *priv, uint8_t *hwaddr)
588 struct ipoib_path *path;
590 if (!priv->broadcast)
593 path = kzalloc(sizeof *path, GFP_ATOMIC);
599 bzero(&path->queue, sizeof(path->queue));
601 #ifdef CONFIG_INFINIBAND_IPOIB_CM
602 memcpy(&path->hwaddr, hwaddr, INFINIBAND_ALEN);
604 memcpy(path->pathrec.dgid.raw, &hwaddr[4], sizeof (union ib_gid));
605 path->pathrec.sgid = priv->local_gid;
606 path->pathrec.pkey = cpu_to_be16(priv->pkey);
607 path->pathrec.numb_path = 1;
608 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
614 path_rec_start(struct ipoib_dev_priv *priv, struct ipoib_path *path)
616 struct ifnet *dev = priv->dev;
618 ib_sa_comp_mask comp_mask = IB_SA_PATH_REC_MTU_SELECTOR | IB_SA_PATH_REC_MTU;
619 struct ib_sa_path_rec p_rec;
621 p_rec = path->pathrec;
622 p_rec.mtu_selector = IB_SA_GT;
624 switch (roundup_pow_of_two(dev->if_mtu + IPOIB_ENCAP_LEN)) {
626 p_rec.mtu = IB_MTU_256;
629 p_rec.mtu = IB_MTU_512;
632 p_rec.mtu = IB_MTU_1024;
635 p_rec.mtu = IB_MTU_2048;
638 /* Wildcard everything */
641 p_rec.mtu_selector = 0;
644 ipoib_dbg(priv, "Start path record lookup for %16D MTU > %d\n",
646 comp_mask ? ib_mtu_enum_to_int(p_rec.mtu) : 0);
648 init_completion(&path->done);
651 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
653 IB_SA_PATH_REC_DGID |
654 IB_SA_PATH_REC_SGID |
655 IB_SA_PATH_REC_NUMB_PATH |
656 IB_SA_PATH_REC_TRAFFIC_CLASS |
661 if (path->query_id < 0) {
662 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
664 complete(&path->done);
665 return path->query_id;
672 ipoib_unicast_send(struct mbuf *mb, struct ipoib_dev_priv *priv, struct ipoib_header *eh)
674 struct ipoib_path *path;
676 path = __path_find(priv, eh->hwaddr + 4);
677 if (!path || !path->valid) {
681 path = path_rec_create(priv, eh->hwaddr);
685 if (_IF_QLEN(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE)
686 _IF_ENQUEUE(&path->queue, mb);
688 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
692 if (!path->query && path_rec_start(priv, path)) {
693 spin_unlock_irqrestore(&priv->lock, flags);
695 ipoib_path_free(priv, path);
698 __path_add(priv, path);
700 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
707 if (ipoib_cm_get(path) && ipoib_cm_up(path)) {
708 ipoib_cm_send(priv, mb, ipoib_cm_get(path));
709 } else if (path->ah) {
710 ipoib_send(priv, mb, path->ah, IPOIB_QPN(eh->hwaddr));
711 } else if ((path->query || !path_rec_start(priv, path)) &&
712 path->queue.ifq_len < IPOIB_MAX_PATH_REC_QUEUE) {
713 _IF_ENQUEUE(&path->queue, mb);
715 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
721 ipoib_send_one(struct ipoib_dev_priv *priv, struct mbuf *mb)
723 struct ipoib_header *eh;
725 eh = mtod(mb, struct ipoib_header *);
726 if (IPOIB_IS_MULTICAST(eh->hwaddr)) {
727 /* Add in the P_Key for multicast*/
728 eh->hwaddr[8] = (priv->pkey >> 8) & 0xff;
729 eh->hwaddr[9] = priv->pkey & 0xff;
731 ipoib_mcast_send(priv, eh->hwaddr + 4, mb);
733 ipoib_unicast_send(mb, priv, eh);
740 _ipoib_start(struct ifnet *dev, struct ipoib_dev_priv *priv)
744 if ((dev->if_drv_flags & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
748 spin_lock(&priv->lock);
749 while (!IFQ_DRV_IS_EMPTY(&dev->if_snd) &&
750 (dev->if_drv_flags & IFF_DRV_OACTIVE) == 0) {
751 IFQ_DRV_DEQUEUE(&dev->if_snd, mb);
755 ipoib_send_one(priv, mb);
757 spin_unlock(&priv->lock);
761 ipoib_start(struct ifnet *dev)
763 _ipoib_start(dev, dev->if_softc);
767 ipoib_vlan_start(struct ifnet *dev)
769 struct ipoib_dev_priv *priv;
772 priv = VLAN_COOKIE(dev);
774 return _ipoib_start(dev, priv);
775 while (!IFQ_DRV_IS_EMPTY(&dev->if_snd)) {
776 IFQ_DRV_DEQUEUE(&dev->if_snd, mb);
780 if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
785 ipoib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca, int port)
788 /* Allocate RX/TX "rings" to hold queued mbs */
789 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
791 if (!priv->rx_ring) {
792 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
793 ca->name, ipoib_recvq_size);
797 priv->tx_ring = kzalloc(ipoib_sendq_size * sizeof *priv->tx_ring, GFP_KERNEL);
798 if (!priv->tx_ring) {
799 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
800 ca->name, ipoib_sendq_size);
801 goto out_rx_ring_cleanup;
803 memset(priv->tx_ring, 0, ipoib_sendq_size * sizeof *priv->tx_ring);
805 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
807 if (ipoib_ib_dev_init(priv, ca, port))
808 goto out_tx_ring_cleanup;
813 kfree(priv->tx_ring);
816 kfree(priv->rx_ring);
823 ipoib_detach(struct ipoib_dev_priv *priv)
828 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
833 free_unr(ipoib_unrhdr, priv->unit);
835 VLAN_SETCOOKIE(priv->dev, NULL);
841 ipoib_dev_cleanup(struct ipoib_dev_priv *priv)
843 struct ipoib_dev_priv *cpriv, *tcpriv;
845 /* Delete any child interfaces first */
846 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
847 ipoib_dev_cleanup(cpriv);
851 ipoib_ib_dev_cleanup(priv);
853 kfree(priv->rx_ring);
854 kfree(priv->tx_ring);
856 priv->rx_ring = NULL;
857 priv->tx_ring = NULL;
860 static struct ipoib_dev_priv *
861 ipoib_priv_alloc(void)
863 struct ipoib_dev_priv *priv;
865 priv = malloc(sizeof(struct ipoib_dev_priv), M_TEMP, M_ZERO|M_WAITOK);
866 spin_lock_init(&priv->lock);
867 spin_lock_init(&priv->drain_lock);
868 mutex_init(&priv->vlan_mutex);
869 INIT_LIST_HEAD(&priv->path_list);
870 INIT_LIST_HEAD(&priv->child_intfs);
871 INIT_LIST_HEAD(&priv->dead_ahs);
872 INIT_LIST_HEAD(&priv->multicast_list);
873 INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
874 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
875 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
876 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
877 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
878 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
879 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
880 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
881 memcpy(priv->broadcastaddr, ipv4_bcast_addr, INFINIBAND_ALEN);
886 struct ipoib_dev_priv *
887 ipoib_intf_alloc(const char *name)
889 struct ipoib_dev_priv *priv;
890 struct sockaddr_dl *sdl;
893 priv = ipoib_priv_alloc();
894 dev = priv->dev = if_alloc(IFT_INFINIBAND);
899 dev->if_softc = priv;
900 priv->unit = alloc_unr(ipoib_unrhdr);
901 if (priv->unit == -1) {
906 if_initname(dev, name, priv->unit);
907 dev->if_flags = IFF_BROADCAST | IFF_MULTICAST;
908 dev->if_addrlen = INFINIBAND_ALEN;
909 dev->if_hdrlen = IPOIB_HEADER_LEN;
911 dev->if_init = ipoib_init;
912 dev->if_ioctl = ipoib_ioctl;
913 dev->if_start = ipoib_start;
914 dev->if_output = ipoib_output;
915 dev->if_input = ipoib_input;
916 dev->if_resolvemulti = ipoib_resolvemulti;
917 dev->if_baudrate = IF_Gbps(10);
918 dev->if_broadcastaddr = priv->broadcastaddr;
919 dev->if_snd.ifq_maxlen = ipoib_sendq_size * 2;
920 sdl = (struct sockaddr_dl *)dev->if_addr->ifa_addr;
921 sdl->sdl_type = IFT_INFINIBAND;
922 sdl->sdl_alen = dev->if_addrlen;
924 if_link_state_change(dev, LINK_STATE_DOWN);
925 bpfattach(dev, DLT_EN10MB, ETHER_HDR_LEN);
927 return dev->if_softc;
931 ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
933 struct ib_device_attr *device_attr = &hca->attrs;
935 priv->hca_caps = device_attr->device_cap_flags;
937 priv->dev->if_hwassist = 0;
938 priv->dev->if_capabilities = 0;
940 #ifndef CONFIG_INFINIBAND_IPOIB_CM
941 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
942 set_bit(IPOIB_FLAG_CSUM, &priv->flags);
943 priv->dev->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP;
944 priv->dev->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM;
948 if (priv->dev->features & NETIF_F_SG && priv->hca_caps & IB_DEVICE_UD_TSO) {
949 priv->dev->if_capabilities |= IFCAP_TSO4;
950 priv->dev->if_hwassist |= CSUM_TSO;
954 priv->dev->if_capabilities |=
955 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_LINKSTATE;
956 priv->dev->if_capenable = priv->dev->if_capabilities;
962 static struct ifnet *
963 ipoib_add_port(const char *format, struct ib_device *hca, u8 port)
965 struct ipoib_dev_priv *priv;
966 struct ib_port_attr attr;
967 int result = -ENOMEM;
969 priv = ipoib_intf_alloc(format);
971 goto alloc_mem_failed;
973 if (!ib_query_port(hca, port, &attr))
974 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
976 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
978 goto device_init_failed;
981 /* MTU will be reset when mcast join happens */
982 priv->dev->if_mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
983 priv->mcast_mtu = priv->admin_mtu = priv->dev->if_mtu;
985 result = ib_query_pkey(hca, port, 0, &priv->pkey);
987 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
988 hca->name, port, result);
989 goto device_init_failed;
992 if (ipoib_set_dev_features(priv, hca))
993 goto device_init_failed;
996 * Set the full membership bit, so that we join the right
997 * broadcast group, etc.
999 priv->pkey |= 0x8000;
1001 priv->broadcastaddr[8] = priv->pkey >> 8;
1002 priv->broadcastaddr[9] = priv->pkey & 0xff;
1004 result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
1006 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1007 hca->name, port, result);
1008 goto device_init_failed;
1010 memcpy(IF_LLADDR(priv->dev) + 4, priv->local_gid.raw, sizeof (union ib_gid));
1012 result = ipoib_dev_init(priv, hca, port);
1014 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1015 hca->name, port, result);
1016 goto device_init_failed;
1018 if (ipoib_cm_admin_enabled(priv))
1019 priv->dev->if_mtu = IPOIB_CM_MTU(ipoib_cm_max_mtu(priv));
1021 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1022 priv->ca, ipoib_event);
1023 result = ib_register_event_handler(&priv->event_handler);
1025 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1026 "port %d (ret = %d)\n",
1027 hca->name, port, result);
1030 if_printf(priv->dev, "Attached to %s port %d\n", hca->name, port);
1035 ipoib_dev_cleanup(priv);
1041 return ERR_PTR(result);
1045 ipoib_add_one(struct ib_device *device)
1047 struct list_head *dev_list;
1049 struct ipoib_dev_priv *priv;
1052 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1055 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1059 INIT_LIST_HEAD(dev_list);
1061 if (device->node_type == RDMA_NODE_IB_SWITCH) {
1066 e = device->phys_port_cnt;
1069 for (p = s; p <= e; ++p) {
1070 if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
1072 dev = ipoib_add_port("ib", device, p);
1074 priv = dev->if_softc;
1075 list_add_tail(&priv->list, dev_list);
1079 ib_set_client_data(device, &ipoib_client, dev_list);
1083 ipoib_remove_one(struct ib_device *device, void *client_data)
1085 struct ipoib_dev_priv *priv, *tmp;
1086 struct list_head *dev_list = client_data;
1091 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1094 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1095 if (rdma_port_get_link_layer(device, priv->port) != IB_LINK_LAYER_INFINIBAND)
1100 ib_unregister_event_handler(&priv->event_handler);
1102 /* dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP); */
1104 flush_workqueue(ipoib_workqueue);
1106 ipoib_dev_cleanup(priv);
1114 ipoib_config_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
1116 struct ipoib_dev_priv *parent;
1117 struct ipoib_dev_priv *priv;
1122 if (ifp->if_type != IFT_INFINIBAND)
1124 dev = VLAN_DEVAT(ifp, vtag);
1129 parent = ifp->if_softc;
1130 /* We only support 15 bits of pkey. */
1133 pkey = vtag | 0x8000; /* Set full membership bit. */
1134 if (pkey == parent->pkey)
1136 /* Check for dups */
1137 mutex_lock(&parent->vlan_mutex);
1138 list_for_each_entry(priv, &parent->child_intfs, list) {
1139 if (priv->pkey == pkey) {
1145 priv = ipoib_priv_alloc();
1147 priv->max_ib_mtu = parent->max_ib_mtu;
1148 priv->mcast_mtu = priv->admin_mtu = parent->dev->if_mtu;
1149 set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
1150 error = ipoib_set_dev_features(priv, parent->ca);
1154 priv->broadcastaddr[8] = pkey >> 8;
1155 priv->broadcastaddr[9] = pkey & 0xff;
1156 dev->if_broadcastaddr = priv->broadcastaddr;
1157 error = ipoib_dev_init(priv, parent->ca, parent->port);
1160 priv->parent = parent->dev;
1161 list_add_tail(&priv->list, &parent->child_intfs);
1162 VLAN_SETCOOKIE(dev, priv);
1163 dev->if_start = ipoib_vlan_start;
1164 dev->if_drv_flags &= ~IFF_DRV_RUNNING;
1165 dev->if_hdrlen = IPOIB_HEADER_LEN;
1166 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1168 mutex_unlock(&parent->vlan_mutex);
1171 mutex_unlock(&parent->vlan_mutex);
1176 "failed to initialize subinterface: device %s, port %d vtag 0x%X",
1177 parent->ca->name, parent->port, vtag);
1182 ipoib_unconfig_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
1184 struct ipoib_dev_priv *parent;
1185 struct ipoib_dev_priv *priv;
1189 if (ifp->if_type != IFT_INFINIBAND)
1192 dev = VLAN_DEVAT(ifp, vtag);
1194 VLAN_SETCOOKIE(dev, NULL);
1195 pkey = vtag | 0x8000;
1196 parent = ifp->if_softc;
1197 mutex_lock(&parent->vlan_mutex);
1198 list_for_each_entry(priv, &parent->child_intfs, list) {
1199 if (priv->pkey == pkey) {
1200 ipoib_dev_cleanup(priv);
1201 list_del(&priv->list);
1205 mutex_unlock(&parent->vlan_mutex);
1208 eventhandler_tag ipoib_vlan_attach;
1209 eventhandler_tag ipoib_vlan_detach;
1212 ipoib_init_module(void)
1216 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1217 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1218 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1220 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1221 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1222 ipoib_sendq_size = max(ipoib_sendq_size, max(2 * MAX_SEND_CQE,
1223 IPOIB_MIN_QUEUE_SIZE));
1224 #ifdef CONFIG_INFINIBAND_IPOIB_CM
1225 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1228 ipoib_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
1229 ipoib_config_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1230 ipoib_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
1231 ipoib_unconfig_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1234 * We create our own workqueue mainly because we want to be
1235 * able to flush it when devices are being removed. We can't
1236 * use schedule_work()/flush_scheduled_work() because both
1237 * unregister_netdev() and linkwatch_event take the rtnl lock,
1238 * so flush_scheduled_work() can deadlock during device
1241 ipoib_workqueue = create_singlethread_workqueue("ipoib");
1242 if (!ipoib_workqueue) {
1247 ib_sa_register_client(&ipoib_sa_client);
1249 ret = ib_register_client(&ipoib_client);
1256 ib_sa_unregister_client(&ipoib_sa_client);
1257 destroy_workqueue(ipoib_workqueue);
1264 ipoib_cleanup_module(void)
1267 EVENTHANDLER_DEREGISTER(vlan_config, ipoib_vlan_attach);
1268 EVENTHANDLER_DEREGISTER(vlan_unconfig, ipoib_vlan_detach);
1269 ib_unregister_client(&ipoib_client);
1270 ib_sa_unregister_client(&ipoib_sa_client);
1271 destroy_workqueue(ipoib_workqueue);
1275 * Infiniband output routine.
1278 ipoib_output(struct ifnet *ifp, struct mbuf *m,
1279 const struct sockaddr *dst, struct route *ro)
1281 u_char edst[INFINIBAND_ALEN];
1282 #if defined(INET) || defined(INET6)
1283 struct llentry *lle = NULL;
1285 struct ipoib_header *eh;
1286 int error = 0, is_gw = 0;
1290 is_gw = (ro->ro_flags & RT_HAS_GW) != 0;
1292 error = mac_ifnet_check_transmit(ifp, m);
1298 if (ifp->if_flags & IFF_MONITOR) {
1302 if (!((ifp->if_flags & IFF_UP) &&
1303 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
1308 switch (dst->sa_family) {
1311 if (lle != NULL && (lle->la_flags & LLE_VALID))
1312 memcpy(edst, lle->ll_addr, sizeof(edst));
1313 else if (m->m_flags & M_MCAST)
1314 ip_ib_mc_map(((struct sockaddr_in *)dst)->sin_addr.s_addr, ifp->if_broadcastaddr, edst);
1316 error = arpresolve(ifp, is_gw, m, dst, edst, NULL, NULL);
1318 return (error == EWOULDBLOCK ? 0 : error);
1319 type = htons(ETHERTYPE_IP);
1324 ah = mtod(m, struct arphdr *);
1325 ah->ar_hrd = htons(ARPHRD_INFINIBAND);
1327 switch(ntohs(ah->ar_op)) {
1328 case ARPOP_REVREQUEST:
1329 case ARPOP_REVREPLY:
1330 type = htons(ETHERTYPE_REVARP);
1335 type = htons(ETHERTYPE_ARP);
1339 if (m->m_flags & M_BCAST)
1340 bcopy(ifp->if_broadcastaddr, edst, INFINIBAND_ALEN);
1342 bcopy(ar_tha(ah), edst, INFINIBAND_ALEN);
1349 if (lle != NULL && (lle->la_flags & LLE_VALID))
1350 memcpy(edst, lle->ll_addr, sizeof(edst));
1351 else if (m->m_flags & M_MCAST)
1352 ipv6_ib_mc_map(&((struct sockaddr_in6 *)dst)->sin6_addr, ifp->if_broadcastaddr, edst);
1354 error = nd6_resolve(ifp, is_gw, m, dst, edst, NULL, NULL);
1357 type = htons(ETHERTYPE_IPV6);
1362 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
1363 error = EAFNOSUPPORT;
1368 * Add local net header. If no space in first mbuf,
1371 M_PREPEND(m, IPOIB_HEADER_LEN, M_NOWAIT);
1376 eh = mtod(m, struct ipoib_header *);
1377 (void)memcpy(&eh->proto, &type, sizeof(eh->proto));
1378 (void)memcpy(&eh->hwaddr, edst, sizeof (edst));
1381 * Queue message on interface, update output statistics if
1382 * successful, and start output if interface not yet active.
1384 return ((ifp->if_transmit)(ifp, m));
1392 * Upper layer processing for a received Infiniband packet.
1395 ipoib_demux(struct ifnet *ifp, struct mbuf *m, u_short proto)
1401 * Tag the mbuf with an appropriate MAC label before any other
1402 * consumers can get to it.
1404 mac_ifnet_create_mbuf(ifp, m);
1406 /* Allow monitor mode to claim this frame, after stats are updated. */
1407 if (ifp->if_flags & IFF_MONITOR) {
1408 if_printf(ifp, "discard frame at IFF_MONITOR\n");
1413 * Dispatch frame to upper layer.
1422 if (ifp->if_flags & IFF_NOARP) {
1423 /* Discard packet if ARP is disabled on interface */
1431 case ETHERTYPE_IPV6:
1438 netisr_dispatch(isr, m);
1446 * Process a received Infiniband packet.
1449 ipoib_input(struct ifnet *ifp, struct mbuf *m)
1451 struct ipoib_header *eh;
1453 if ((ifp->if_flags & IFF_UP) == 0) {
1457 CURVNET_SET_QUIET(ifp->if_vnet);
1459 /* Let BPF have it before we strip the header. */
1461 eh = mtod(m, struct ipoib_header *);
1463 * Reset layer specific mbuf flags to avoid confusing upper layers.
1464 * Strip off Infiniband header.
1466 m->m_flags &= ~M_VLANTAG;
1468 m_adj(m, IPOIB_HEADER_LEN);
1470 if (IPOIB_IS_MULTICAST(eh->hwaddr)) {
1471 if (memcmp(eh->hwaddr, ifp->if_broadcastaddr,
1472 ifp->if_addrlen) == 0)
1473 m->m_flags |= M_BCAST;
1475 m->m_flags |= M_MCAST;
1476 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
1479 ipoib_demux(ifp, m, ntohs(eh->proto));
1484 ipoib_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1485 struct sockaddr *sa)
1487 struct sockaddr_dl *sdl;
1489 struct sockaddr_in *sin;
1492 struct sockaddr_in6 *sin6;
1496 switch(sa->sa_family) {
1499 * No mapping needed. Just check that it's a valid MC address.
1501 sdl = (struct sockaddr_dl *)sa;
1502 e_addr = LLADDR(sdl);
1503 if (!IPOIB_IS_MULTICAST(e_addr))
1504 return EADDRNOTAVAIL;
1510 sin = (struct sockaddr_in *)sa;
1511 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1512 return EADDRNOTAVAIL;
1513 sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND);
1514 sdl->sdl_alen = INFINIBAND_ALEN;
1515 e_addr = LLADDR(sdl);
1516 ip_ib_mc_map(sin->sin_addr.s_addr, ifp->if_broadcastaddr,
1518 *llsa = (struct sockaddr *)sdl;
1523 sin6 = (struct sockaddr_in6 *)sa;
1525 * An IP6 address of 0 means listen to all
1526 * of the multicast address used for IP6.
1527 * This has no meaning in ipoib.
1529 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
1530 return EADDRNOTAVAIL;
1531 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1532 return EADDRNOTAVAIL;
1533 sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND);
1534 sdl->sdl_alen = INFINIBAND_ALEN;
1535 e_addr = LLADDR(sdl);
1536 ipv6_ib_mc_map(&sin6->sin6_addr, ifp->if_broadcastaddr, e_addr);
1537 *llsa = (struct sockaddr *)sdl;
1542 return EAFNOSUPPORT;
1546 module_init(ipoib_init_module);
1547 module_exit(ipoib_cleanup_module);
1550 ipoib_evhand(module_t mod, int event, void *arg)
1555 static moduledata_t ipoib_mod = {
1557 .evhand = ipoib_evhand,
1560 DECLARE_MODULE(ipoib, ipoib_mod, SI_SUB_LAST, SI_ORDER_ANY);
1561 MODULE_DEPEND(ipoib, ibcore, 1, 1, 1);
1562 MODULE_DEPEND(ipoib, linuxkpi, 1, 1, 1);