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);
326 sa = (struct sockaddr *) & ifr->ifr_data;
327 bcopy(IF_LLADDR(ifp),
328 (caddr_t) sa->sa_data, INFINIBAND_ALEN);
334 * Set the interface MTU.
336 error = -ipoib_change_mtu(priv, ifr->ifr_mtu);
346 static struct ipoib_path *
347 __path_find(struct ipoib_dev_priv *priv, void *gid)
349 struct rb_node *n = priv->path_tree.rb_node;
350 struct ipoib_path *path;
354 path = rb_entry(n, struct ipoib_path, rb_node);
356 ret = memcmp(gid, path->pathrec.dgid.raw,
357 sizeof (union ib_gid));
371 __path_add(struct ipoib_dev_priv *priv, struct ipoib_path *path)
373 struct rb_node **n = &priv->path_tree.rb_node;
374 struct rb_node *pn = NULL;
375 struct ipoib_path *tpath;
380 tpath = rb_entry(pn, struct ipoib_path, rb_node);
382 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
383 sizeof (union ib_gid));
392 rb_link_node(&path->rb_node, pn, n);
393 rb_insert_color(&path->rb_node, &priv->path_tree);
395 list_add_tail(&path->list, &priv->path_list);
401 ipoib_path_free(struct ipoib_dev_priv *priv, struct ipoib_path *path)
404 _IF_DRAIN(&path->queue);
407 ipoib_put_ah(path->ah);
408 if (ipoib_cm_get(path))
409 ipoib_cm_destroy_tx(ipoib_cm_get(path));
414 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
416 struct ipoib_path_iter *
417 ipoib_path_iter_init(struct ipoib_dev_priv *priv)
419 struct ipoib_path_iter *iter;
421 iter = kmalloc(sizeof *iter, GFP_KERNEL);
426 memset(iter->path.pathrec.dgid.raw, 0, 16);
428 if (ipoib_path_iter_next(iter)) {
437 ipoib_path_iter_next(struct ipoib_path_iter *iter)
439 struct ipoib_dev_priv *priv = iter->priv;
441 struct ipoib_path *path;
444 spin_lock_irq(&priv->lock);
446 n = rb_first(&priv->path_tree);
449 path = rb_entry(n, struct ipoib_path, rb_node);
451 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
452 sizeof (union ib_gid)) < 0) {
461 spin_unlock_irq(&priv->lock);
467 ipoib_path_iter_read(struct ipoib_path_iter *iter, struct ipoib_path *path)
472 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
475 ipoib_mark_paths_invalid(struct ipoib_dev_priv *priv)
477 struct ipoib_path *path, *tp;
479 spin_lock_irq(&priv->lock);
481 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
482 ipoib_dbg(priv, "mark path LID 0x%04x GID %16D invalid\n",
483 be16_to_cpu(path->pathrec.dlid),
484 path->pathrec.dgid.raw, ":");
488 spin_unlock_irq(&priv->lock);
492 ipoib_flush_paths(struct ipoib_dev_priv *priv)
494 struct ipoib_path *path, *tp;
495 LIST_HEAD(remove_list);
498 spin_lock_irqsave(&priv->lock, flags);
500 list_splice_init(&priv->path_list, &remove_list);
502 list_for_each_entry(path, &remove_list, list)
503 rb_erase(&path->rb_node, &priv->path_tree);
505 list_for_each_entry_safe(path, tp, &remove_list, list) {
507 ib_sa_cancel_query(path->query_id, path->query);
508 spin_unlock_irqrestore(&priv->lock, flags);
509 wait_for_completion(&path->done);
510 ipoib_path_free(priv, path);
511 spin_lock_irqsave(&priv->lock, flags);
514 spin_unlock_irqrestore(&priv->lock, flags);
518 path_rec_completion(int status, struct ib_sa_path_rec *pathrec, void *path_ptr)
520 struct ipoib_path *path = path_ptr;
521 struct ipoib_dev_priv *priv = path->priv;
522 struct ifnet *dev = priv->dev;
523 struct ipoib_ah *ah = NULL;
524 struct ipoib_ah *old_ah = NULL;
525 struct ifqueue mbqueue;
530 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %16D\n",
531 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw, ":");
533 ipoib_dbg(priv, "PathRec status %d for GID %16D\n",
534 status, path->pathrec.dgid.raw, ":");
536 bzero(&mbqueue, sizeof(mbqueue));
539 struct ib_ah_attr av;
541 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
542 ah = ipoib_create_ah(priv, priv->pd, &av);
545 spin_lock_irqsave(&priv->lock, flags);
548 path->pathrec = *pathrec;
553 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
554 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
557 _IF_DEQUEUE(&path->queue, mb);
560 _IF_ENQUEUE(&mbqueue, mb);
563 #ifdef CONFIG_INFINIBAND_IPOIB_CM
564 if (ipoib_cm_enabled(priv, path->hwaddr) && !ipoib_cm_get(path))
565 ipoib_cm_set(path, ipoib_cm_create_tx(priv, path));
572 complete(&path->done);
574 spin_unlock_irqrestore(&priv->lock, flags);
577 ipoib_put_ah(old_ah);
580 _IF_DEQUEUE(&mbqueue, mb);
583 mb->m_pkthdr.rcvif = dev;
584 if (dev->if_transmit(dev, mb))
585 ipoib_warn(priv, "dev_queue_xmit failed "
586 "to requeue packet\n");
590 static struct ipoib_path *
591 path_rec_create(struct ipoib_dev_priv *priv, uint8_t *hwaddr)
593 struct ipoib_path *path;
595 if (!priv->broadcast)
598 path = kzalloc(sizeof *path, GFP_ATOMIC);
604 bzero(&path->queue, sizeof(path->queue));
606 #ifdef CONFIG_INFINIBAND_IPOIB_CM
607 memcpy(&path->hwaddr, hwaddr, INFINIBAND_ALEN);
609 memcpy(path->pathrec.dgid.raw, &hwaddr[4], sizeof (union ib_gid));
610 path->pathrec.sgid = priv->local_gid;
611 path->pathrec.pkey = cpu_to_be16(priv->pkey);
612 path->pathrec.numb_path = 1;
613 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
619 path_rec_start(struct ipoib_dev_priv *priv, struct ipoib_path *path)
621 struct ifnet *dev = priv->dev;
623 ib_sa_comp_mask comp_mask = IB_SA_PATH_REC_MTU_SELECTOR | IB_SA_PATH_REC_MTU;
624 struct ib_sa_path_rec p_rec;
626 p_rec = path->pathrec;
627 p_rec.mtu_selector = IB_SA_GT;
629 switch (roundup_pow_of_two(dev->if_mtu + IPOIB_ENCAP_LEN)) {
631 p_rec.mtu = IB_MTU_256;
634 p_rec.mtu = IB_MTU_512;
637 p_rec.mtu = IB_MTU_1024;
640 p_rec.mtu = IB_MTU_2048;
643 /* Wildcard everything */
646 p_rec.mtu_selector = 0;
649 ipoib_dbg(priv, "Start path record lookup for %16D MTU > %d\n",
651 comp_mask ? ib_mtu_enum_to_int(p_rec.mtu) : 0);
653 init_completion(&path->done);
656 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
658 IB_SA_PATH_REC_DGID |
659 IB_SA_PATH_REC_SGID |
660 IB_SA_PATH_REC_NUMB_PATH |
661 IB_SA_PATH_REC_TRAFFIC_CLASS |
666 if (path->query_id < 0) {
667 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
669 complete(&path->done);
670 return path->query_id;
677 ipoib_unicast_send(struct mbuf *mb, struct ipoib_dev_priv *priv, struct ipoib_header *eh)
679 struct ipoib_path *path;
681 path = __path_find(priv, eh->hwaddr + 4);
682 if (!path || !path->valid) {
686 path = path_rec_create(priv, eh->hwaddr);
690 if (_IF_QLEN(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE)
691 _IF_ENQUEUE(&path->queue, mb);
693 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
697 if (!path->query && path_rec_start(priv, path)) {
698 spin_unlock_irqrestore(&priv->lock, flags);
700 ipoib_path_free(priv, path);
703 __path_add(priv, path);
705 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
712 if (ipoib_cm_get(path) && ipoib_cm_up(path)) {
713 ipoib_cm_send(priv, mb, ipoib_cm_get(path));
714 } else if (path->ah) {
715 ipoib_send(priv, mb, path->ah, IPOIB_QPN(eh->hwaddr));
716 } else if ((path->query || !path_rec_start(priv, path)) &&
717 path->queue.ifq_len < IPOIB_MAX_PATH_REC_QUEUE) {
718 _IF_ENQUEUE(&path->queue, mb);
720 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
726 ipoib_send_one(struct ipoib_dev_priv *priv, struct mbuf *mb)
728 struct ipoib_header *eh;
730 eh = mtod(mb, struct ipoib_header *);
731 if (IPOIB_IS_MULTICAST(eh->hwaddr)) {
732 /* Add in the P_Key for multicast*/
733 eh->hwaddr[8] = (priv->pkey >> 8) & 0xff;
734 eh->hwaddr[9] = priv->pkey & 0xff;
736 ipoib_mcast_send(priv, eh->hwaddr + 4, mb);
738 ipoib_unicast_send(mb, priv, eh);
745 _ipoib_start(struct ifnet *dev, struct ipoib_dev_priv *priv)
749 if ((dev->if_drv_flags & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
753 spin_lock(&priv->lock);
754 while (!IFQ_DRV_IS_EMPTY(&dev->if_snd) &&
755 (dev->if_drv_flags & IFF_DRV_OACTIVE) == 0) {
756 IFQ_DRV_DEQUEUE(&dev->if_snd, mb);
760 ipoib_send_one(priv, mb);
762 spin_unlock(&priv->lock);
766 ipoib_start(struct ifnet *dev)
768 _ipoib_start(dev, dev->if_softc);
772 ipoib_vlan_start(struct ifnet *dev)
774 struct ipoib_dev_priv *priv;
777 priv = VLAN_COOKIE(dev);
779 return _ipoib_start(dev, priv);
780 while (!IFQ_DRV_IS_EMPTY(&dev->if_snd)) {
781 IFQ_DRV_DEQUEUE(&dev->if_snd, mb);
785 if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
790 ipoib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca, int port)
793 /* Allocate RX/TX "rings" to hold queued mbs */
794 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
796 if (!priv->rx_ring) {
797 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
798 ca->name, ipoib_recvq_size);
802 priv->tx_ring = kzalloc(ipoib_sendq_size * sizeof *priv->tx_ring, GFP_KERNEL);
803 if (!priv->tx_ring) {
804 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
805 ca->name, ipoib_sendq_size);
806 goto out_rx_ring_cleanup;
808 memset(priv->tx_ring, 0, ipoib_sendq_size * sizeof *priv->tx_ring);
810 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
812 if (ipoib_ib_dev_init(priv, ca, port))
813 goto out_tx_ring_cleanup;
818 kfree(priv->tx_ring);
821 kfree(priv->rx_ring);
828 ipoib_detach(struct ipoib_dev_priv *priv)
833 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
838 free_unr(ipoib_unrhdr, priv->unit);
840 VLAN_SETCOOKIE(priv->dev, NULL);
846 ipoib_dev_cleanup(struct ipoib_dev_priv *priv)
848 struct ipoib_dev_priv *cpriv, *tcpriv;
850 /* Delete any child interfaces first */
851 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
852 ipoib_dev_cleanup(cpriv);
856 ipoib_ib_dev_cleanup(priv);
858 kfree(priv->rx_ring);
859 kfree(priv->tx_ring);
861 priv->rx_ring = NULL;
862 priv->tx_ring = NULL;
865 static struct ipoib_dev_priv *
866 ipoib_priv_alloc(void)
868 struct ipoib_dev_priv *priv;
870 priv = malloc(sizeof(struct ipoib_dev_priv), M_TEMP, M_ZERO|M_WAITOK);
871 spin_lock_init(&priv->lock);
872 spin_lock_init(&priv->drain_lock);
873 mutex_init(&priv->vlan_mutex);
874 INIT_LIST_HEAD(&priv->path_list);
875 INIT_LIST_HEAD(&priv->child_intfs);
876 INIT_LIST_HEAD(&priv->dead_ahs);
877 INIT_LIST_HEAD(&priv->multicast_list);
878 INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
879 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
880 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
881 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
882 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
883 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
884 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
885 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
886 memcpy(priv->broadcastaddr, ipv4_bcast_addr, INFINIBAND_ALEN);
891 struct ipoib_dev_priv *
892 ipoib_intf_alloc(const char *name)
894 struct ipoib_dev_priv *priv;
895 struct sockaddr_dl *sdl;
898 priv = ipoib_priv_alloc();
899 dev = priv->dev = if_alloc(IFT_INFINIBAND);
904 dev->if_softc = priv;
905 priv->unit = alloc_unr(ipoib_unrhdr);
906 if (priv->unit == -1) {
911 if_initname(dev, name, priv->unit);
912 dev->if_flags = IFF_BROADCAST | IFF_MULTICAST;
913 dev->if_addrlen = INFINIBAND_ALEN;
914 dev->if_hdrlen = IPOIB_HEADER_LEN;
916 dev->if_init = ipoib_init;
917 dev->if_ioctl = ipoib_ioctl;
918 dev->if_start = ipoib_start;
919 dev->if_output = ipoib_output;
920 dev->if_input = ipoib_input;
921 dev->if_resolvemulti = ipoib_resolvemulti;
922 dev->if_baudrate = IF_Gbps(10);
923 dev->if_broadcastaddr = priv->broadcastaddr;
924 dev->if_snd.ifq_maxlen = ipoib_sendq_size * 2;
925 sdl = (struct sockaddr_dl *)dev->if_addr->ifa_addr;
926 sdl->sdl_type = IFT_INFINIBAND;
927 sdl->sdl_alen = dev->if_addrlen;
929 if_link_state_change(dev, LINK_STATE_DOWN);
930 bpfattach(dev, DLT_EN10MB, ETHER_HDR_LEN);
932 return dev->if_softc;
936 ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
938 struct ib_device_attr *device_attr = &hca->attrs;
940 priv->hca_caps = device_attr->device_cap_flags;
942 priv->dev->if_hwassist = 0;
943 priv->dev->if_capabilities = 0;
945 #ifndef CONFIG_INFINIBAND_IPOIB_CM
946 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
947 set_bit(IPOIB_FLAG_CSUM, &priv->flags);
948 priv->dev->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP;
949 priv->dev->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM;
953 if (priv->dev->features & NETIF_F_SG && priv->hca_caps & IB_DEVICE_UD_TSO) {
954 priv->dev->if_capabilities |= IFCAP_TSO4;
955 priv->dev->if_hwassist |= CSUM_TSO;
959 priv->dev->if_capabilities |=
960 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_LINKSTATE;
961 priv->dev->if_capenable = priv->dev->if_capabilities;
967 static struct ifnet *
968 ipoib_add_port(const char *format, struct ib_device *hca, u8 port)
970 struct ipoib_dev_priv *priv;
971 struct ib_port_attr attr;
972 int result = -ENOMEM;
974 priv = ipoib_intf_alloc(format);
976 goto alloc_mem_failed;
978 if (!ib_query_port(hca, port, &attr))
979 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
981 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
983 goto device_init_failed;
986 /* MTU will be reset when mcast join happens */
987 priv->dev->if_mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
988 priv->mcast_mtu = priv->admin_mtu = priv->dev->if_mtu;
990 result = ib_query_pkey(hca, port, 0, &priv->pkey);
992 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
993 hca->name, port, result);
994 goto device_init_failed;
997 if (ipoib_set_dev_features(priv, hca))
998 goto device_init_failed;
1001 * Set the full membership bit, so that we join the right
1002 * broadcast group, etc.
1004 priv->pkey |= 0x8000;
1006 priv->broadcastaddr[8] = priv->pkey >> 8;
1007 priv->broadcastaddr[9] = priv->pkey & 0xff;
1009 result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
1011 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1012 hca->name, port, result);
1013 goto device_init_failed;
1015 memcpy(IF_LLADDR(priv->dev) + 4, priv->local_gid.raw, sizeof (union ib_gid));
1017 result = ipoib_dev_init(priv, hca, port);
1019 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1020 hca->name, port, result);
1021 goto device_init_failed;
1023 if (ipoib_cm_admin_enabled(priv))
1024 priv->dev->if_mtu = IPOIB_CM_MTU(ipoib_cm_max_mtu(priv));
1026 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1027 priv->ca, ipoib_event);
1028 result = ib_register_event_handler(&priv->event_handler);
1030 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1031 "port %d (ret = %d)\n",
1032 hca->name, port, result);
1035 if_printf(priv->dev, "Attached to %s port %d\n", hca->name, port);
1040 ipoib_dev_cleanup(priv);
1046 return ERR_PTR(result);
1050 ipoib_add_one(struct ib_device *device)
1052 struct list_head *dev_list;
1054 struct ipoib_dev_priv *priv;
1057 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1060 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1064 INIT_LIST_HEAD(dev_list);
1066 if (device->node_type == RDMA_NODE_IB_SWITCH) {
1071 e = device->phys_port_cnt;
1074 for (p = s; p <= e; ++p) {
1075 if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
1077 dev = ipoib_add_port("ib", device, p);
1079 priv = dev->if_softc;
1080 list_add_tail(&priv->list, dev_list);
1084 ib_set_client_data(device, &ipoib_client, dev_list);
1088 ipoib_remove_one(struct ib_device *device, void *client_data)
1090 struct ipoib_dev_priv *priv, *tmp;
1091 struct list_head *dev_list = client_data;
1096 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1099 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1100 if (rdma_port_get_link_layer(device, priv->port) != IB_LINK_LAYER_INFINIBAND)
1105 ib_unregister_event_handler(&priv->event_handler);
1107 /* dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP); */
1109 flush_workqueue(ipoib_workqueue);
1111 ipoib_dev_cleanup(priv);
1119 ipoib_config_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
1121 struct ipoib_dev_priv *parent;
1122 struct ipoib_dev_priv *priv;
1127 if (ifp->if_type != IFT_INFINIBAND)
1129 dev = VLAN_DEVAT(ifp, vtag);
1134 parent = ifp->if_softc;
1135 /* We only support 15 bits of pkey. */
1138 pkey = vtag | 0x8000; /* Set full membership bit. */
1139 if (pkey == parent->pkey)
1141 /* Check for dups */
1142 mutex_lock(&parent->vlan_mutex);
1143 list_for_each_entry(priv, &parent->child_intfs, list) {
1144 if (priv->pkey == pkey) {
1150 priv = ipoib_priv_alloc();
1152 priv->max_ib_mtu = parent->max_ib_mtu;
1153 priv->mcast_mtu = priv->admin_mtu = parent->dev->if_mtu;
1154 set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
1155 error = ipoib_set_dev_features(priv, parent->ca);
1159 priv->broadcastaddr[8] = pkey >> 8;
1160 priv->broadcastaddr[9] = pkey & 0xff;
1161 dev->if_broadcastaddr = priv->broadcastaddr;
1162 error = ipoib_dev_init(priv, parent->ca, parent->port);
1165 priv->parent = parent->dev;
1166 list_add_tail(&priv->list, &parent->child_intfs);
1167 VLAN_SETCOOKIE(dev, priv);
1168 dev->if_start = ipoib_vlan_start;
1169 dev->if_drv_flags &= ~IFF_DRV_RUNNING;
1170 dev->if_hdrlen = IPOIB_HEADER_LEN;
1171 if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1173 mutex_unlock(&parent->vlan_mutex);
1176 mutex_unlock(&parent->vlan_mutex);
1181 "failed to initialize subinterface: device %s, port %d vtag 0x%X",
1182 parent->ca->name, parent->port, vtag);
1187 ipoib_unconfig_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
1189 struct ipoib_dev_priv *parent;
1190 struct ipoib_dev_priv *priv;
1194 if (ifp->if_type != IFT_INFINIBAND)
1197 dev = VLAN_DEVAT(ifp, vtag);
1199 VLAN_SETCOOKIE(dev, NULL);
1200 pkey = vtag | 0x8000;
1201 parent = ifp->if_softc;
1202 mutex_lock(&parent->vlan_mutex);
1203 list_for_each_entry(priv, &parent->child_intfs, list) {
1204 if (priv->pkey == pkey) {
1205 ipoib_dev_cleanup(priv);
1206 list_del(&priv->list);
1210 mutex_unlock(&parent->vlan_mutex);
1213 eventhandler_tag ipoib_vlan_attach;
1214 eventhandler_tag ipoib_vlan_detach;
1217 ipoib_init_module(void)
1221 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1222 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1223 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1225 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1226 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1227 ipoib_sendq_size = max(ipoib_sendq_size, max(2 * MAX_SEND_CQE,
1228 IPOIB_MIN_QUEUE_SIZE));
1229 #ifdef CONFIG_INFINIBAND_IPOIB_CM
1230 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1233 ipoib_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
1234 ipoib_config_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1235 ipoib_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
1236 ipoib_unconfig_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1239 * We create our own workqueue mainly because we want to be
1240 * able to flush it when devices are being removed. We can't
1241 * use schedule_work()/flush_scheduled_work() because both
1242 * unregister_netdev() and linkwatch_event take the rtnl lock,
1243 * so flush_scheduled_work() can deadlock during device
1246 ipoib_workqueue = create_singlethread_workqueue("ipoib");
1247 if (!ipoib_workqueue) {
1252 ib_sa_register_client(&ipoib_sa_client);
1254 ret = ib_register_client(&ipoib_client);
1261 ib_sa_unregister_client(&ipoib_sa_client);
1262 destroy_workqueue(ipoib_workqueue);
1269 ipoib_cleanup_module(void)
1272 EVENTHANDLER_DEREGISTER(vlan_config, ipoib_vlan_attach);
1273 EVENTHANDLER_DEREGISTER(vlan_unconfig, ipoib_vlan_detach);
1274 ib_unregister_client(&ipoib_client);
1275 ib_sa_unregister_client(&ipoib_sa_client);
1276 destroy_workqueue(ipoib_workqueue);
1280 * Infiniband output routine.
1283 ipoib_output(struct ifnet *ifp, struct mbuf *m,
1284 const struct sockaddr *dst, struct route *ro)
1286 u_char edst[INFINIBAND_ALEN];
1287 #if defined(INET) || defined(INET6)
1288 struct llentry *lle = NULL;
1290 struct ipoib_header *eh;
1291 int error = 0, is_gw = 0;
1295 is_gw = (ro->ro_flags & RT_HAS_GW) != 0;
1297 error = mac_ifnet_check_transmit(ifp, m);
1303 if (ifp->if_flags & IFF_MONITOR) {
1307 if (!((ifp->if_flags & IFF_UP) &&
1308 (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
1313 switch (dst->sa_family) {
1316 if (lle != NULL && (lle->la_flags & LLE_VALID))
1317 memcpy(edst, lle->ll_addr, sizeof(edst));
1318 else if (m->m_flags & M_MCAST)
1319 ip_ib_mc_map(((struct sockaddr_in *)dst)->sin_addr.s_addr, ifp->if_broadcastaddr, edst);
1321 error = arpresolve(ifp, is_gw, m, dst, edst, NULL, NULL);
1323 return (error == EWOULDBLOCK ? 0 : error);
1324 type = htons(ETHERTYPE_IP);
1329 ah = mtod(m, struct arphdr *);
1330 ah->ar_hrd = htons(ARPHRD_INFINIBAND);
1332 switch(ntohs(ah->ar_op)) {
1333 case ARPOP_REVREQUEST:
1334 case ARPOP_REVREPLY:
1335 type = htons(ETHERTYPE_REVARP);
1340 type = htons(ETHERTYPE_ARP);
1344 if (m->m_flags & M_BCAST)
1345 bcopy(ifp->if_broadcastaddr, edst, INFINIBAND_ALEN);
1347 bcopy(ar_tha(ah), edst, INFINIBAND_ALEN);
1354 if (lle != NULL && (lle->la_flags & LLE_VALID))
1355 memcpy(edst, lle->ll_addr, sizeof(edst));
1356 else if (m->m_flags & M_MCAST)
1357 ipv6_ib_mc_map(&((struct sockaddr_in6 *)dst)->sin6_addr, ifp->if_broadcastaddr, edst);
1359 error = nd6_resolve(ifp, is_gw, m, dst, edst, NULL, NULL);
1362 type = htons(ETHERTYPE_IPV6);
1367 if_printf(ifp, "can't handle af%d\n", dst->sa_family);
1368 error = EAFNOSUPPORT;
1373 * Add local net header. If no space in first mbuf,
1376 M_PREPEND(m, IPOIB_HEADER_LEN, M_NOWAIT);
1381 eh = mtod(m, struct ipoib_header *);
1382 (void)memcpy(&eh->proto, &type, sizeof(eh->proto));
1383 (void)memcpy(&eh->hwaddr, edst, sizeof (edst));
1386 * Queue message on interface, update output statistics if
1387 * successful, and start output if interface not yet active.
1389 return ((ifp->if_transmit)(ifp, m));
1397 * Upper layer processing for a received Infiniband packet.
1400 ipoib_demux(struct ifnet *ifp, struct mbuf *m, u_short proto)
1406 * Tag the mbuf with an appropriate MAC label before any other
1407 * consumers can get to it.
1409 mac_ifnet_create_mbuf(ifp, m);
1411 /* Allow monitor mode to claim this frame, after stats are updated. */
1412 if (ifp->if_flags & IFF_MONITOR) {
1413 if_printf(ifp, "discard frame at IFF_MONITOR\n");
1418 * Dispatch frame to upper layer.
1427 if (ifp->if_flags & IFF_NOARP) {
1428 /* Discard packet if ARP is disabled on interface */
1436 case ETHERTYPE_IPV6:
1443 netisr_dispatch(isr, m);
1451 * Process a received Infiniband packet.
1454 ipoib_input(struct ifnet *ifp, struct mbuf *m)
1456 struct ipoib_header *eh;
1458 if ((ifp->if_flags & IFF_UP) == 0) {
1462 CURVNET_SET_QUIET(ifp->if_vnet);
1464 /* Let BPF have it before we strip the header. */
1466 eh = mtod(m, struct ipoib_header *);
1468 * Reset layer specific mbuf flags to avoid confusing upper layers.
1469 * Strip off Infiniband header.
1471 m->m_flags &= ~M_VLANTAG;
1473 m_adj(m, IPOIB_HEADER_LEN);
1475 if (IPOIB_IS_MULTICAST(eh->hwaddr)) {
1476 if (memcmp(eh->hwaddr, ifp->if_broadcastaddr,
1477 ifp->if_addrlen) == 0)
1478 m->m_flags |= M_BCAST;
1480 m->m_flags |= M_MCAST;
1481 if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
1484 ipoib_demux(ifp, m, ntohs(eh->proto));
1489 ipoib_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1490 struct sockaddr *sa)
1492 struct sockaddr_dl *sdl;
1494 struct sockaddr_in *sin;
1497 struct sockaddr_in6 *sin6;
1501 switch(sa->sa_family) {
1504 * No mapping needed. Just check that it's a valid MC address.
1506 sdl = (struct sockaddr_dl *)sa;
1507 e_addr = LLADDR(sdl);
1508 if (!IPOIB_IS_MULTICAST(e_addr))
1509 return EADDRNOTAVAIL;
1515 sin = (struct sockaddr_in *)sa;
1516 if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1517 return EADDRNOTAVAIL;
1518 sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND);
1519 sdl->sdl_alen = INFINIBAND_ALEN;
1520 e_addr = LLADDR(sdl);
1521 ip_ib_mc_map(sin->sin_addr.s_addr, ifp->if_broadcastaddr,
1523 *llsa = (struct sockaddr *)sdl;
1528 sin6 = (struct sockaddr_in6 *)sa;
1530 * An IP6 address of 0 means listen to all
1531 * of the multicast address used for IP6.
1532 * This has no meaning in ipoib.
1534 if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
1535 return EADDRNOTAVAIL;
1536 if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1537 return EADDRNOTAVAIL;
1538 sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND);
1539 sdl->sdl_alen = INFINIBAND_ALEN;
1540 e_addr = LLADDR(sdl);
1541 ipv6_ib_mc_map(&sin6->sin6_addr, ifp->if_broadcastaddr, e_addr);
1542 *llsa = (struct sockaddr *)sdl;
1547 return EAFNOSUPPORT;
1551 module_init(ipoib_init_module);
1552 module_exit(ipoib_cleanup_module);
1555 ipoib_evhand(module_t mod, int event, void *arg)
1560 static moduledata_t ipoib_mod = {
1562 .evhand = ipoib_evhand,
1565 DECLARE_MODULE(ipoib, ipoib_mod, SI_SUB_LAST, SI_ORDER_ANY);
1566 MODULE_DEPEND(ipoib, ibcore, 1, 1, 1);
1567 MODULE_DEPEND(ipoib, linuxkpi, 1, 1, 1);