xref: /freebsd/sys/ofed/drivers/infiniband/ulp/ipoib/ipoib_main.c (revision 6966ac055c3b7a39266fb982493330df7a097997)
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause OR GPL-2.0
3  *
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.
7  *
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:
13  *
14  *     Redistribution and use in source and binary forms, with or
15  *     without modification, are permitted provided that the following
16  *     conditions are met:
17  *
18  *      - Redistributions of source code must retain the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer.
21  *
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.
26  *
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
34  * SOFTWARE.
35  */
36 
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
39 
40 #include "ipoib.h"
41 #include <sys/eventhandler.h>
42 
43 static	int ipoib_resolvemulti(struct ifnet *, struct sockaddr **,
44 		struct sockaddr *);
45 
46 
47 #include <linux/module.h>
48 
49 #include <linux/slab.h>
50 #include <linux/kernel.h>
51 #include <linux/vmalloc.h>
52 
53 #include <linux/if_arp.h>	/* For ARPHRD_xxx */
54 #include <linux/if_vlan.h>
55 #include <net/ip.h>
56 #include <net/ipv6.h>
57 
58 #include <rdma/ib_cache.h>
59 
60 MODULE_AUTHOR("Roland Dreier");
61 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
62 MODULE_LICENSE("Dual BSD/GPL");
63 
64 int ipoib_sendq_size = IPOIB_TX_RING_SIZE;
65 int ipoib_recvq_size = IPOIB_RX_RING_SIZE;
66 
67 module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
68 MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
69 module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
70 MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
71 
72 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
73 int ipoib_debug_level = 1;
74 
75 module_param_named(debug_level, ipoib_debug_level, int, 0644);
76 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
77 #endif
78 
79 struct ipoib_path_iter {
80 	struct ipoib_dev_priv *priv;
81 	struct ipoib_path  path;
82 };
83 
84 static const u8 ipv4_bcast_addr[] = {
85 	0x00, 0xff, 0xff, 0xff,
86 	0xff, 0x12, 0x40, 0x1b,	0x00, 0x00, 0x00, 0x00,
87 	0x00, 0x00, 0x00, 0x00,	0xff, 0xff, 0xff, 0xff
88 };
89 
90 struct workqueue_struct *ipoib_workqueue;
91 
92 struct ib_sa_client ipoib_sa_client;
93 
94 static void ipoib_add_one(struct ib_device *device);
95 static void ipoib_remove_one(struct ib_device *device, void *client_data);
96 static struct net_device *ipoib_get_net_dev_by_params(
97 		struct ib_device *dev, u8 port, u16 pkey,
98 		const union ib_gid *gid, const struct sockaddr *addr,
99 		void *client_data);
100 static void ipoib_start(struct ifnet *dev);
101 static int ipoib_output(struct ifnet *ifp, struct mbuf *m,
102 	    const struct sockaddr *dst, struct route *ro);
103 static int ipoib_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
104 static void ipoib_input(struct ifnet *ifp, struct mbuf *m);
105 
106 #define	IPOIB_MTAP(_ifp, _m)					\
107 do {								\
108 	if (bpf_peers_present((_ifp)->if_bpf)) {		\
109 		M_ASSERTVALID(_m);				\
110 		ipoib_mtap_mb((_ifp), (_m));			\
111 	}							\
112 } while (0)
113 
114 static struct unrhdr *ipoib_unrhdr;
115 
116 static void
117 ipoib_unrhdr_init(void *arg)
118 {
119 
120 	ipoib_unrhdr = new_unrhdr(0, 65535, NULL);
121 }
122 SYSINIT(ipoib_unrhdr_init, SI_SUB_KLD - 1, SI_ORDER_ANY, ipoib_unrhdr_init, NULL);
123 
124 static void
125 ipoib_unrhdr_uninit(void *arg)
126 {
127 
128 	if (ipoib_unrhdr != NULL) {
129 		struct unrhdr *hdr;
130 
131 		hdr = ipoib_unrhdr;
132 		ipoib_unrhdr = NULL;
133 
134 		delete_unrhdr(hdr);
135 	}
136 }
137 SYSUNINIT(ipoib_unrhdr_uninit, SI_SUB_KLD - 1, SI_ORDER_ANY, ipoib_unrhdr_uninit, NULL);
138 
139 /*
140  * This is for clients that have an ipoib_header in the mbuf.
141  */
142 static void
143 ipoib_mtap_mb(struct ifnet *ifp, struct mbuf *mb)
144 {
145 	struct ipoib_header *ih;
146 	struct ether_header eh;
147 
148 	ih = mtod(mb, struct ipoib_header *);
149 	eh.ether_type = ih->proto;
150 	bcopy(ih->hwaddr, &eh.ether_dhost, ETHER_ADDR_LEN);
151 	bzero(&eh.ether_shost, ETHER_ADDR_LEN);
152 	mb->m_data += sizeof(struct ipoib_header);
153 	mb->m_len -= sizeof(struct ipoib_header);
154 	bpf_mtap2(ifp->if_bpf, &eh, sizeof(eh), mb);
155 	mb->m_data -= sizeof(struct ipoib_header);
156 	mb->m_len += sizeof(struct ipoib_header);
157 }
158 
159 void
160 ipoib_mtap_proto(struct ifnet *ifp, struct mbuf *mb, uint16_t proto)
161 {
162 	struct ether_header eh;
163 
164 	eh.ether_type = proto;
165 	bzero(&eh.ether_shost, ETHER_ADDR_LEN);
166 	bzero(&eh.ether_dhost, ETHER_ADDR_LEN);
167 	bpf_mtap2(ifp->if_bpf, &eh, sizeof(eh), mb);
168 }
169 
170 static struct ib_client ipoib_client = {
171 	.name   = "ipoib",
172 	.add    = ipoib_add_one,
173 	.remove = ipoib_remove_one,
174 	.get_net_dev_by_params = ipoib_get_net_dev_by_params,
175 };
176 
177 int
178 ipoib_open(struct ipoib_dev_priv *priv)
179 {
180 	struct ifnet *dev = priv->dev;
181 
182 	ipoib_dbg(priv, "bringing up interface\n");
183 
184 	set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
185 
186 	if (ipoib_pkey_dev_delay_open(priv))
187 		return 0;
188 
189 	if (ipoib_ib_dev_open(priv))
190 		goto err_disable;
191 
192 	if (ipoib_ib_dev_up(priv))
193 		goto err_stop;
194 
195 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
196 		struct ipoib_dev_priv *cpriv;
197 
198 		/* Bring up any child interfaces too */
199 		mutex_lock(&priv->vlan_mutex);
200 		list_for_each_entry(cpriv, &priv->child_intfs, list)
201 			if ((cpriv->dev->if_drv_flags & IFF_DRV_RUNNING) == 0)
202 				ipoib_open(cpriv);
203 		mutex_unlock(&priv->vlan_mutex);
204 	}
205 	dev->if_drv_flags |= IFF_DRV_RUNNING;
206 	dev->if_drv_flags &= ~IFF_DRV_OACTIVE;
207 
208 	return 0;
209 
210 err_stop:
211 	ipoib_ib_dev_stop(priv, 1);
212 
213 err_disable:
214 	clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
215 
216 	return -EINVAL;
217 }
218 
219 static void
220 ipoib_init(void *arg)
221 {
222 	struct ifnet *dev;
223 	struct ipoib_dev_priv *priv;
224 
225 	priv = arg;
226 	dev = priv->dev;
227 	if ((dev->if_drv_flags & IFF_DRV_RUNNING) == 0)
228 		ipoib_open(priv);
229 	queue_work(ipoib_workqueue, &priv->flush_light);
230 }
231 
232 
233 static int
234 ipoib_stop(struct ipoib_dev_priv *priv)
235 {
236 	struct ifnet *dev = priv->dev;
237 
238 	ipoib_dbg(priv, "stopping interface\n");
239 
240 	clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
241 
242 	dev->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
243 
244 	ipoib_ib_dev_down(priv, 0);
245 	ipoib_ib_dev_stop(priv, 0);
246 
247 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
248 		struct ipoib_dev_priv *cpriv;
249 
250 		/* Bring down any child interfaces too */
251 		mutex_lock(&priv->vlan_mutex);
252 		list_for_each_entry(cpriv, &priv->child_intfs, list)
253 			if ((cpriv->dev->if_drv_flags & IFF_DRV_RUNNING) != 0)
254 				ipoib_stop(cpriv);
255 		mutex_unlock(&priv->vlan_mutex);
256 	}
257 
258 	return 0;
259 }
260 
261 static int
262 ipoib_propagate_ifnet_mtu(struct ipoib_dev_priv *priv, int new_mtu,
263     bool propagate)
264 {
265 	struct ifnet *ifp;
266 	struct ifreq ifr;
267 	int error;
268 
269 	ifp = priv->dev;
270 	if (ifp->if_mtu == new_mtu)
271 		return (0);
272 	if (propagate) {
273 		strlcpy(ifr.ifr_name, if_name(ifp), IFNAMSIZ);
274 		ifr.ifr_mtu = new_mtu;
275 		CURVNET_SET(ifp->if_vnet);
276 		error = ifhwioctl(SIOCSIFMTU, ifp, (caddr_t)&ifr, curthread);
277 		CURVNET_RESTORE();
278 	} else {
279 		ifp->if_mtu = new_mtu;
280 		error = 0;
281 	}
282 	return (error);
283 }
284 
285 int
286 ipoib_change_mtu(struct ipoib_dev_priv *priv, int new_mtu, bool propagate)
287 {
288 	int error, prev_admin_mtu;
289 
290 	/* dev->if_mtu > 2K ==> connected mode */
291 	if (ipoib_cm_admin_enabled(priv)) {
292 		if (new_mtu > IPOIB_CM_MTU(ipoib_cm_max_mtu(priv)))
293 			return -EINVAL;
294 
295 		if (new_mtu > priv->mcast_mtu)
296 			ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
297 				   priv->mcast_mtu);
298 
299 		return (ipoib_propagate_ifnet_mtu(priv, new_mtu, propagate));
300 	}
301 
302 	if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
303 		return -EINVAL;
304 
305 	prev_admin_mtu = priv->admin_mtu;
306 	priv->admin_mtu = new_mtu;
307 	error = ipoib_propagate_ifnet_mtu(priv, min(priv->mcast_mtu,
308 	    priv->admin_mtu), propagate);
309 	if (error == 0) {
310 		/* check for MTU change to avoid infinite loop */
311 		if (prev_admin_mtu != new_mtu)
312 			queue_work(ipoib_workqueue, &priv->flush_light);
313 	} else
314 		priv->admin_mtu = prev_admin_mtu;
315 	return (error);
316 }
317 
318 static int
319 ipoib_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
320 {
321 	struct ipoib_dev_priv *priv = ifp->if_softc;
322 	struct ifaddr *ifa = (struct ifaddr *) data;
323 	struct ifreq *ifr = (struct ifreq *) data;
324 	int error = 0;
325 
326 	/* check if detaching */
327 	if (priv == NULL || priv->gone != 0)
328 		return (ENXIO);
329 
330 	switch (command) {
331 	case SIOCSIFFLAGS:
332 		if (ifp->if_flags & IFF_UP) {
333 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
334 				error = -ipoib_open(priv);
335 		} else
336 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
337 				ipoib_stop(priv);
338 		break;
339 	case SIOCADDMULTI:
340 	case SIOCDELMULTI:
341 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
342 			queue_work(ipoib_workqueue, &priv->restart_task);
343 		break;
344 	case SIOCSIFADDR:
345 		ifp->if_flags |= IFF_UP;
346 
347 		switch (ifa->ifa_addr->sa_family) {
348 #ifdef INET
349 		case AF_INET:
350 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
351 			arp_ifinit(ifp, ifa);
352 			break;
353 #endif
354 		default:
355 			ifp->if_init(ifp->if_softc);
356 			break;
357 		}
358 		break;
359 
360 	case SIOCGIFADDR:
361 			bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
362                             INFINIBAND_ALEN);
363 		break;
364 
365 	case SIOCSIFMTU:
366 		/*
367 		 * Set the interface MTU.
368 		 */
369 		error = -ipoib_change_mtu(priv, ifr->ifr_mtu, false);
370 		break;
371 	default:
372 		error = EINVAL;
373 		break;
374 	}
375 	return (error);
376 }
377 
378 
379 static struct ipoib_path *
380 __path_find(struct ipoib_dev_priv *priv, void *gid)
381 {
382 	struct rb_node *n = priv->path_tree.rb_node;
383 	struct ipoib_path *path;
384 	int ret;
385 
386 	while (n) {
387 		path = rb_entry(n, struct ipoib_path, rb_node);
388 
389 		ret = memcmp(gid, path->pathrec.dgid.raw,
390 			     sizeof (union ib_gid));
391 
392 		if (ret < 0)
393 			n = n->rb_left;
394 		else if (ret > 0)
395 			n = n->rb_right;
396 		else
397 			return path;
398 	}
399 
400 	return NULL;
401 }
402 
403 static int
404 __path_add(struct ipoib_dev_priv *priv, struct ipoib_path *path)
405 {
406 	struct rb_node **n = &priv->path_tree.rb_node;
407 	struct rb_node *pn = NULL;
408 	struct ipoib_path *tpath;
409 	int ret;
410 
411 	while (*n) {
412 		pn = *n;
413 		tpath = rb_entry(pn, struct ipoib_path, rb_node);
414 
415 		ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
416 			     sizeof (union ib_gid));
417 		if (ret < 0)
418 			n = &pn->rb_left;
419 		else if (ret > 0)
420 			n = &pn->rb_right;
421 		else
422 			return -EEXIST;
423 	}
424 
425 	rb_link_node(&path->rb_node, pn, n);
426 	rb_insert_color(&path->rb_node, &priv->path_tree);
427 
428 	list_add_tail(&path->list, &priv->path_list);
429 
430 	return 0;
431 }
432 
433 void
434 ipoib_path_free(struct ipoib_dev_priv *priv, struct ipoib_path *path)
435 {
436 
437 	_IF_DRAIN(&path->queue);
438 
439 	if (path->ah)
440 		ipoib_put_ah(path->ah);
441 	if (ipoib_cm_get(path))
442 		ipoib_cm_destroy_tx(ipoib_cm_get(path));
443 
444 	kfree(path);
445 }
446 
447 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
448 
449 struct ipoib_path_iter *
450 ipoib_path_iter_init(struct ipoib_dev_priv *priv)
451 {
452 	struct ipoib_path_iter *iter;
453 
454 	iter = kmalloc(sizeof *iter, GFP_KERNEL);
455 	if (!iter)
456 		return NULL;
457 
458 	iter->priv = priv;
459 	memset(iter->path.pathrec.dgid.raw, 0, 16);
460 
461 	if (ipoib_path_iter_next(iter)) {
462 		kfree(iter);
463 		return NULL;
464 	}
465 
466 	return iter;
467 }
468 
469 int
470 ipoib_path_iter_next(struct ipoib_path_iter *iter)
471 {
472 	struct ipoib_dev_priv *priv = iter->priv;
473 	struct rb_node *n;
474 	struct ipoib_path *path;
475 	int ret = 1;
476 
477 	spin_lock_irq(&priv->lock);
478 
479 	n = rb_first(&priv->path_tree);
480 
481 	while (n) {
482 		path = rb_entry(n, struct ipoib_path, rb_node);
483 
484 		if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
485 			   sizeof (union ib_gid)) < 0) {
486 			iter->path = *path;
487 			ret = 0;
488 			break;
489 		}
490 
491 		n = rb_next(n);
492 	}
493 
494 	spin_unlock_irq(&priv->lock);
495 
496 	return ret;
497 }
498 
499 void
500 ipoib_path_iter_read(struct ipoib_path_iter *iter, struct ipoib_path *path)
501 {
502 	*path = iter->path;
503 }
504 
505 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
506 
507 void
508 ipoib_mark_paths_invalid(struct ipoib_dev_priv *priv)
509 {
510 	struct ipoib_path *path, *tp;
511 
512 	spin_lock_irq(&priv->lock);
513 
514 	list_for_each_entry_safe(path, tp, &priv->path_list, list) {
515 		ipoib_dbg(priv, "mark path LID 0x%04x GID %16D invalid\n",
516 			be16_to_cpu(path->pathrec.dlid),
517 			path->pathrec.dgid.raw, ":");
518 		path->valid =  0;
519 	}
520 
521 	spin_unlock_irq(&priv->lock);
522 }
523 
524 void
525 ipoib_flush_paths(struct ipoib_dev_priv *priv)
526 {
527 	struct ipoib_path *path, *tp;
528 	LIST_HEAD(remove_list);
529 	unsigned long flags;
530 
531 	spin_lock_irqsave(&priv->lock, flags);
532 
533 	list_splice_init(&priv->path_list, &remove_list);
534 
535 	list_for_each_entry(path, &remove_list, list)
536 		rb_erase(&path->rb_node, &priv->path_tree);
537 
538 	list_for_each_entry_safe(path, tp, &remove_list, list) {
539 		if (path->query)
540 			ib_sa_cancel_query(path->query_id, path->query);
541 		spin_unlock_irqrestore(&priv->lock, flags);
542 		wait_for_completion(&path->done);
543 		ipoib_path_free(priv, path);
544 		spin_lock_irqsave(&priv->lock, flags);
545 	}
546 
547 	spin_unlock_irqrestore(&priv->lock, flags);
548 }
549 
550 static void
551 path_rec_completion(int status, struct ib_sa_path_rec *pathrec, void *path_ptr)
552 {
553 	struct ipoib_path *path = path_ptr;
554 	struct ipoib_dev_priv *priv = path->priv;
555 	struct ifnet *dev = priv->dev;
556 	struct ipoib_ah *ah = NULL;
557 	struct ipoib_ah *old_ah = NULL;
558 	struct epoch_tracker et;
559 	struct ifqueue mbqueue;
560 	struct mbuf *mb;
561 	unsigned long flags;
562 
563 	if (!status)
564 		ipoib_dbg(priv, "PathRec LID 0x%04x for GID %16D\n",
565 			  be16_to_cpu(pathrec->dlid), pathrec->dgid.raw, ":");
566 	else
567 		ipoib_dbg(priv, "PathRec status %d for GID %16D\n",
568 			  status, path->pathrec.dgid.raw, ":");
569 
570 	bzero(&mbqueue, sizeof(mbqueue));
571 
572 	if (!status) {
573 		struct ib_ah_attr av;
574 
575 		if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
576 			ah = ipoib_create_ah(priv, priv->pd, &av);
577 	}
578 
579 	spin_lock_irqsave(&priv->lock, flags);
580 
581 	if (ah) {
582 		path->pathrec = *pathrec;
583 
584 		old_ah   = path->ah;
585 		path->ah = ah;
586 
587 		ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
588 			  ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
589 
590 		for (;;) {
591 			_IF_DEQUEUE(&path->queue, mb);
592 			if (mb == NULL)
593 				break;
594 			_IF_ENQUEUE(&mbqueue, mb);
595 		}
596 
597 #ifdef CONFIG_INFINIBAND_IPOIB_CM
598 		if (ipoib_cm_enabled(priv, path->hwaddr) && !ipoib_cm_get(path))
599 			ipoib_cm_set(path, ipoib_cm_create_tx(priv, path));
600 #endif
601 
602 		path->valid = 1;
603 	}
604 
605 	path->query = NULL;
606 	complete(&path->done);
607 
608 	spin_unlock_irqrestore(&priv->lock, flags);
609 
610 	if (old_ah)
611 		ipoib_put_ah(old_ah);
612 
613 	NET_EPOCH_ENTER(et);
614 	for (;;) {
615 		_IF_DEQUEUE(&mbqueue, mb);
616 		if (mb == NULL)
617 			break;
618 		mb->m_pkthdr.rcvif = dev;
619 		if (dev->if_transmit(dev, mb))
620 			ipoib_warn(priv, "dev_queue_xmit failed "
621 				   "to requeue packet\n");
622 	}
623 	NET_EPOCH_EXIT(et);
624 }
625 
626 static struct ipoib_path *
627 path_rec_create(struct ipoib_dev_priv *priv, uint8_t *hwaddr)
628 {
629 	struct ipoib_path *path;
630 
631 	if (!priv->broadcast)
632 		return NULL;
633 
634 	path = kzalloc(sizeof *path, GFP_ATOMIC);
635 	if (!path)
636 		return NULL;
637 
638 	path->priv = priv;
639 
640 	bzero(&path->queue, sizeof(path->queue));
641 
642 #ifdef CONFIG_INFINIBAND_IPOIB_CM
643 	memcpy(&path->hwaddr, hwaddr, INFINIBAND_ALEN);
644 #endif
645 	memcpy(path->pathrec.dgid.raw, &hwaddr[4], sizeof (union ib_gid));
646 	path->pathrec.sgid	    = priv->local_gid;
647 	path->pathrec.pkey	    = cpu_to_be16(priv->pkey);
648 	path->pathrec.numb_path     = 1;
649 	path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
650 
651 	return path;
652 }
653 
654 static int
655 path_rec_start(struct ipoib_dev_priv *priv, struct ipoib_path *path)
656 {
657 	struct ifnet *dev = priv->dev;
658 
659 	ib_sa_comp_mask comp_mask = IB_SA_PATH_REC_MTU_SELECTOR | IB_SA_PATH_REC_MTU;
660 	struct ib_sa_path_rec p_rec;
661 
662 	p_rec = path->pathrec;
663 	p_rec.mtu_selector = IB_SA_GT;
664 
665 	switch (roundup_pow_of_two(dev->if_mtu + IPOIB_ENCAP_LEN)) {
666 	case 512:
667 		p_rec.mtu = IB_MTU_256;
668 		break;
669 	case 1024:
670 		p_rec.mtu = IB_MTU_512;
671 		break;
672 	case 2048:
673 		p_rec.mtu = IB_MTU_1024;
674 		break;
675 	case 4096:
676 		p_rec.mtu = IB_MTU_2048;
677 		break;
678 	default:
679 		/* Wildcard everything */
680 		comp_mask = 0;
681 		p_rec.mtu = 0;
682 		p_rec.mtu_selector = 0;
683 	}
684 
685 	ipoib_dbg(priv, "Start path record lookup for %16D MTU > %d\n",
686 		  p_rec.dgid.raw, ":",
687 		  comp_mask ? ib_mtu_enum_to_int(p_rec.mtu) : 0);
688 
689 	init_completion(&path->done);
690 
691 	path->query_id =
692 		ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
693 				   &p_rec, comp_mask		|
694 				   IB_SA_PATH_REC_DGID		|
695 				   IB_SA_PATH_REC_SGID		|
696 				   IB_SA_PATH_REC_NUMB_PATH	|
697 				   IB_SA_PATH_REC_TRAFFIC_CLASS |
698 				   IB_SA_PATH_REC_PKEY,
699 				   1000, GFP_ATOMIC,
700 				   path_rec_completion,
701 				   path, &path->query);
702 	if (path->query_id < 0) {
703 		ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
704 		path->query = NULL;
705 		complete(&path->done);
706 		return path->query_id;
707 	}
708 
709 	return 0;
710 }
711 
712 static void
713 ipoib_unicast_send(struct mbuf *mb, struct ipoib_dev_priv *priv, struct ipoib_header *eh)
714 {
715 	struct ipoib_path *path;
716 
717 	path = __path_find(priv, eh->hwaddr + 4);
718 	if (!path || !path->valid) {
719 		int new_path = 0;
720 
721 		if (!path) {
722 			path = path_rec_create(priv, eh->hwaddr);
723 			new_path = 1;
724 		}
725 		if (path) {
726 			if (_IF_QLEN(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE)
727 				_IF_ENQUEUE(&path->queue, mb);
728 			else {
729 				if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
730 				m_freem(mb);
731 			}
732 
733 			if (!path->query && path_rec_start(priv, path)) {
734 				spin_unlock_irqrestore(&priv->lock, flags);
735 				if (new_path)
736 					ipoib_path_free(priv, path);
737 				return;
738 			} else
739 				__path_add(priv, path);
740 		} else {
741 			if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
742 			m_freem(mb);
743 		}
744 
745 		return;
746 	}
747 
748 	if (ipoib_cm_get(path) && ipoib_cm_up(path)) {
749 		ipoib_cm_send(priv, mb, ipoib_cm_get(path));
750 	} else if (path->ah) {
751 		ipoib_send(priv, mb, path->ah, IPOIB_QPN(eh->hwaddr));
752 	} else if ((path->query || !path_rec_start(priv, path)) &&
753 		    path->queue.ifq_len < IPOIB_MAX_PATH_REC_QUEUE) {
754 		_IF_ENQUEUE(&path->queue, mb);
755 	} else {
756 		if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
757 		m_freem(mb);
758 	}
759 }
760 
761 static int
762 ipoib_send_one(struct ipoib_dev_priv *priv, struct mbuf *mb)
763 {
764 	struct ipoib_header *eh;
765 
766 	eh = mtod(mb, struct ipoib_header *);
767 	if (IPOIB_IS_MULTICAST(eh->hwaddr)) {
768 		/* Add in the P_Key for multicast*/
769 		eh->hwaddr[8] = (priv->pkey >> 8) & 0xff;
770 		eh->hwaddr[9] = priv->pkey & 0xff;
771 
772 		ipoib_mcast_send(priv, eh->hwaddr + 4, mb);
773 	} else
774 		ipoib_unicast_send(mb, priv, eh);
775 
776 	return 0;
777 }
778 
779 void
780 ipoib_start_locked(struct ifnet *dev, struct ipoib_dev_priv *priv)
781 {
782 	struct mbuf *mb;
783 
784 	assert_spin_locked(&priv->lock);
785 
786 	while (!IFQ_DRV_IS_EMPTY(&dev->if_snd) &&
787 	    (dev->if_drv_flags & IFF_DRV_OACTIVE) == 0) {
788 		IFQ_DRV_DEQUEUE(&dev->if_snd, mb);
789 		if (mb == NULL)
790 			break;
791 		IPOIB_MTAP(dev, mb);
792 		ipoib_send_one(priv, mb);
793 	}
794 }
795 
796 static void
797 _ipoib_start(struct ifnet *dev, struct ipoib_dev_priv *priv)
798 {
799 
800 	if ((dev->if_drv_flags & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
801 	    IFF_DRV_RUNNING)
802 		return;
803 
804 	spin_lock(&priv->lock);
805 	ipoib_start_locked(dev, priv);
806 	spin_unlock(&priv->lock);
807 }
808 
809 static void
810 ipoib_start(struct ifnet *dev)
811 {
812 	_ipoib_start(dev, dev->if_softc);
813 }
814 
815 static void
816 ipoib_vlan_start(struct ifnet *dev)
817 {
818 	struct ipoib_dev_priv *priv;
819 	struct mbuf *mb;
820 
821 	priv = VLAN_COOKIE(dev);
822 	if (priv != NULL)
823 		return _ipoib_start(dev, priv);
824 	while (!IFQ_DRV_IS_EMPTY(&dev->if_snd)) {
825 		IFQ_DRV_DEQUEUE(&dev->if_snd, mb);
826 		if (mb == NULL)
827 			break;
828 		m_freem(mb);
829 		if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
830 	}
831 }
832 
833 int
834 ipoib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca, int port)
835 {
836 
837 	/* Allocate RX/TX "rings" to hold queued mbs */
838 	priv->rx_ring =	kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
839 				GFP_KERNEL);
840 	if (!priv->rx_ring) {
841 		printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
842 		       ca->name, ipoib_recvq_size);
843 		goto out;
844 	}
845 
846 	priv->tx_ring = kzalloc(ipoib_sendq_size * sizeof *priv->tx_ring, GFP_KERNEL);
847 	if (!priv->tx_ring) {
848 		printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
849 		       ca->name, ipoib_sendq_size);
850 		goto out_rx_ring_cleanup;
851 	}
852 	memset(priv->tx_ring, 0, ipoib_sendq_size * sizeof *priv->tx_ring);
853 
854 	/* priv->tx_head, tx_tail & tx_outstanding are already 0 */
855 
856 	if (ipoib_ib_dev_init(priv, ca, port))
857 		goto out_tx_ring_cleanup;
858 
859 	return 0;
860 
861 out_tx_ring_cleanup:
862 	kfree(priv->tx_ring);
863 
864 out_rx_ring_cleanup:
865 	kfree(priv->rx_ring);
866 
867 out:
868 	return -ENOMEM;
869 }
870 
871 static void
872 ipoib_detach(struct ipoib_dev_priv *priv)
873 {
874 	struct ifnet *dev;
875 
876 	dev = priv->dev;
877 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
878 		priv->gone = 1;
879 		bpfdetach(dev);
880 		if_detach(dev);
881 		if_free(dev);
882 		free_unr(ipoib_unrhdr, priv->unit);
883 	} else
884 		VLAN_SETCOOKIE(priv->dev, NULL);
885 
886 	free(priv, M_TEMP);
887 }
888 
889 void
890 ipoib_dev_cleanup(struct ipoib_dev_priv *priv)
891 {
892 	struct ipoib_dev_priv *cpriv, *tcpriv;
893 
894 	/* Delete any child interfaces first */
895 	list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
896 		ipoib_dev_cleanup(cpriv);
897 		ipoib_detach(cpriv);
898 	}
899 
900 	ipoib_ib_dev_cleanup(priv);
901 
902 	kfree(priv->rx_ring);
903 	kfree(priv->tx_ring);
904 
905 	priv->rx_ring = NULL;
906 	priv->tx_ring = NULL;
907 }
908 
909 static struct ipoib_dev_priv *
910 ipoib_priv_alloc(void)
911 {
912 	struct ipoib_dev_priv *priv;
913 
914 	priv = malloc(sizeof(struct ipoib_dev_priv), M_TEMP, M_ZERO|M_WAITOK);
915 	spin_lock_init(&priv->lock);
916 	spin_lock_init(&priv->drain_lock);
917 	mutex_init(&priv->vlan_mutex);
918 	INIT_LIST_HEAD(&priv->path_list);
919 	INIT_LIST_HEAD(&priv->child_intfs);
920 	INIT_LIST_HEAD(&priv->dead_ahs);
921 	INIT_LIST_HEAD(&priv->multicast_list);
922 	INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
923 	INIT_DELAYED_WORK(&priv->mcast_task,   ipoib_mcast_join_task);
924 	INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
925 	INIT_WORK(&priv->flush_light,   ipoib_ib_dev_flush_light);
926 	INIT_WORK(&priv->flush_normal,   ipoib_ib_dev_flush_normal);
927 	INIT_WORK(&priv->flush_heavy,   ipoib_ib_dev_flush_heavy);
928 	INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
929 	INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
930 	memcpy(priv->broadcastaddr, ipv4_bcast_addr, INFINIBAND_ALEN);
931 
932 	return (priv);
933 }
934 
935 struct ipoib_dev_priv *
936 ipoib_intf_alloc(const char *name)
937 {
938 	struct ipoib_dev_priv *priv;
939 	struct sockaddr_dl *sdl;
940 	struct ifnet *dev;
941 
942 	priv = ipoib_priv_alloc();
943 	dev = priv->dev = if_alloc(IFT_INFINIBAND);
944 	if (!dev) {
945 		free(priv, M_TEMP);
946 		return NULL;
947 	}
948 	dev->if_softc = priv;
949 	priv->unit = alloc_unr(ipoib_unrhdr);
950 	if (priv->unit == -1) {
951 		if_free(dev);
952 		free(priv, M_TEMP);
953 		return NULL;
954 	}
955 	if_initname(dev, name, priv->unit);
956 	dev->if_flags = IFF_BROADCAST | IFF_MULTICAST;
957 	dev->if_addrlen = INFINIBAND_ALEN;
958 	dev->if_hdrlen = IPOIB_HEADER_LEN;
959 	if_attach(dev);
960 	dev->if_init = ipoib_init;
961 	dev->if_ioctl = ipoib_ioctl;
962 	dev->if_start = ipoib_start;
963 	dev->if_output = ipoib_output;
964 	dev->if_input = ipoib_input;
965 	dev->if_resolvemulti = ipoib_resolvemulti;
966 	dev->if_baudrate = IF_Gbps(10);
967 	dev->if_broadcastaddr = priv->broadcastaddr;
968 	dev->if_snd.ifq_maxlen = ipoib_sendq_size * 2;
969 	sdl = (struct sockaddr_dl *)dev->if_addr->ifa_addr;
970 	sdl->sdl_type = IFT_INFINIBAND;
971 	sdl->sdl_alen = dev->if_addrlen;
972 	priv->dev = dev;
973 	if_link_state_change(dev, LINK_STATE_DOWN);
974 	bpfattach(dev, DLT_EN10MB, ETHER_HDR_LEN);
975 
976 	return dev->if_softc;
977 }
978 
979 int
980 ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
981 {
982 	struct ib_device_attr *device_attr = &hca->attrs;
983 
984 	priv->hca_caps = device_attr->device_cap_flags;
985 
986 	priv->dev->if_hwassist = 0;
987 	priv->dev->if_capabilities = 0;
988 
989 #ifndef CONFIG_INFINIBAND_IPOIB_CM
990 	if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
991 		set_bit(IPOIB_FLAG_CSUM, &priv->flags);
992 		priv->dev->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP;
993 		priv->dev->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM;
994 	}
995 
996 #if 0
997 	if (priv->dev->features & NETIF_F_SG && priv->hca_caps & IB_DEVICE_UD_TSO) {
998 		priv->dev->if_capabilities |= IFCAP_TSO4;
999 		priv->dev->if_hwassist |= CSUM_TSO;
1000 	}
1001 #endif
1002 #endif
1003 	priv->dev->if_capabilities |=
1004 	    IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_LINKSTATE;
1005 	priv->dev->if_capenable = priv->dev->if_capabilities;
1006 
1007 	return 0;
1008 }
1009 
1010 
1011 static struct ifnet *
1012 ipoib_add_port(const char *format, struct ib_device *hca, u8 port)
1013 {
1014 	struct ipoib_dev_priv *priv;
1015 	struct ib_port_attr attr;
1016 	int result = -ENOMEM;
1017 
1018 	priv = ipoib_intf_alloc(format);
1019 	if (!priv)
1020 		goto alloc_mem_failed;
1021 
1022 	if (!ib_query_port(hca, port, &attr))
1023 		priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1024 	else {
1025 		printk(KERN_WARNING "%s: ib_query_port %d failed\n",
1026 		       hca->name, port);
1027 		goto device_init_failed;
1028 	}
1029 
1030 	/* MTU will be reset when mcast join happens */
1031 	priv->dev->if_mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
1032 	priv->mcast_mtu = priv->admin_mtu = priv->dev->if_mtu;
1033 
1034 	result = ib_query_pkey(hca, port, 0, &priv->pkey);
1035 	if (result) {
1036 		printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1037 		       hca->name, port, result);
1038 		goto device_init_failed;
1039 	}
1040 
1041 	if (ipoib_set_dev_features(priv, hca))
1042 		goto device_init_failed;
1043 
1044 	/*
1045 	 * Set the full membership bit, so that we join the right
1046 	 * broadcast group, etc.
1047 	 */
1048 	priv->pkey |= 0x8000;
1049 
1050 	priv->broadcastaddr[8] = priv->pkey >> 8;
1051 	priv->broadcastaddr[9] = priv->pkey & 0xff;
1052 
1053 	result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
1054 	if (result) {
1055 		printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1056 		       hca->name, port, result);
1057 		goto device_init_failed;
1058 	}
1059 	memcpy(IF_LLADDR(priv->dev) + 4, priv->local_gid.raw, sizeof (union ib_gid));
1060 
1061 	result = ipoib_dev_init(priv, hca, port);
1062 	if (result < 0) {
1063 		printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1064 		       hca->name, port, result);
1065 		goto device_init_failed;
1066 	}
1067 	if (ipoib_cm_admin_enabled(priv))
1068 		priv->dev->if_mtu = IPOIB_CM_MTU(ipoib_cm_max_mtu(priv));
1069 
1070 	INIT_IB_EVENT_HANDLER(&priv->event_handler,
1071 			      priv->ca, ipoib_event);
1072 	result = ib_register_event_handler(&priv->event_handler);
1073 	if (result < 0) {
1074 		printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1075 		       "port %d (ret = %d)\n",
1076 		       hca->name, port, result);
1077 		goto event_failed;
1078 	}
1079 	if_printf(priv->dev, "Attached to %s port %d\n", hca->name, port);
1080 
1081 	return priv->dev;
1082 
1083 event_failed:
1084 	ipoib_dev_cleanup(priv);
1085 
1086 device_init_failed:
1087 	ipoib_detach(priv);
1088 
1089 alloc_mem_failed:
1090 	return ERR_PTR(result);
1091 }
1092 
1093 static void
1094 ipoib_add_one(struct ib_device *device)
1095 {
1096 	struct list_head *dev_list;
1097 	struct ifnet *dev;
1098 	struct ipoib_dev_priv *priv;
1099 	int s, e, p;
1100 
1101 	if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1102 		return;
1103 
1104 	dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1105 	if (!dev_list)
1106 		return;
1107 
1108 	INIT_LIST_HEAD(dev_list);
1109 
1110 	if (device->node_type == RDMA_NODE_IB_SWITCH) {
1111 		s = 0;
1112 		e = 0;
1113 	} else {
1114 		s = 1;
1115 		e = device->phys_port_cnt;
1116 	}
1117 
1118 	for (p = s; p <= e; ++p) {
1119 		if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
1120 			continue;
1121 		dev = ipoib_add_port("ib", device, p);
1122 		if (!IS_ERR(dev)) {
1123 			priv = dev->if_softc;
1124 			list_add_tail(&priv->list, dev_list);
1125 		}
1126 	}
1127 
1128 	ib_set_client_data(device, &ipoib_client, dev_list);
1129 }
1130 
1131 static void
1132 ipoib_remove_one(struct ib_device *device, void *client_data)
1133 {
1134 	struct ipoib_dev_priv *priv, *tmp;
1135 	struct list_head *dev_list = client_data;
1136 
1137 	if (!dev_list)
1138 		return;
1139 
1140 	if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1141 		return;
1142 
1143 	list_for_each_entry_safe(priv, tmp, dev_list, list) {
1144 		if (rdma_port_get_link_layer(device, priv->port) != IB_LINK_LAYER_INFINIBAND)
1145 			continue;
1146 
1147 		ipoib_stop(priv);
1148 
1149 		ib_unregister_event_handler(&priv->event_handler);
1150 
1151 		/* dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP); */
1152 
1153 		flush_workqueue(ipoib_workqueue);
1154 
1155 		ipoib_dev_cleanup(priv);
1156 		ipoib_detach(priv);
1157 	}
1158 
1159 	kfree(dev_list);
1160 }
1161 
1162 static int
1163 ipoib_match_dev_addr(const struct sockaddr *addr, struct net_device *dev)
1164 {
1165 	struct epoch_tracker et;
1166 	struct ifaddr *ifa;
1167 	int retval = 0;
1168 
1169 	CURVNET_SET(dev->if_vnet);
1170 	NET_EPOCH_ENTER(et);
1171 	CK_STAILQ_FOREACH(ifa, &dev->if_addrhead, ifa_link) {
1172 		if (ifa->ifa_addr == NULL ||
1173 		    ifa->ifa_addr->sa_family != addr->sa_family ||
1174 		    ifa->ifa_addr->sa_len != addr->sa_len) {
1175 			continue;
1176 		}
1177 		if (memcmp(ifa->ifa_addr, addr, addr->sa_len) == 0) {
1178 			retval = 1;
1179 			break;
1180 		}
1181 	}
1182 	NET_EPOCH_EXIT(et);
1183 	CURVNET_RESTORE();
1184 
1185 	return (retval);
1186 }
1187 
1188 /*
1189  * ipoib_match_gid_pkey_addr - returns the number of IPoIB netdevs on
1190  * top a given ipoib device matching a pkey_index and address, if one
1191  * exists.
1192  *
1193  * @found_net_dev: contains a matching net_device if the return value
1194  * >= 1, with a reference held.
1195  */
1196 static int
1197 ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
1198     const union ib_gid *gid, u16 pkey_index, const struct sockaddr *addr,
1199     struct net_device **found_net_dev)
1200 {
1201 	struct ipoib_dev_priv *child_priv;
1202 	int matches = 0;
1203 
1204 	if (priv->pkey_index == pkey_index &&
1205 	    (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
1206 		if (addr == NULL || ipoib_match_dev_addr(addr, priv->dev) != 0) {
1207 			if (*found_net_dev == NULL) {
1208 				struct net_device *net_dev;
1209 
1210 				if (priv->parent != NULL)
1211 					net_dev = priv->parent;
1212 				else
1213 					net_dev = priv->dev;
1214 				*found_net_dev = net_dev;
1215 				dev_hold(net_dev);
1216 			}
1217 			matches++;
1218 		}
1219 	}
1220 
1221 	/* Check child interfaces */
1222 	mutex_lock(&priv->vlan_mutex);
1223 	list_for_each_entry(child_priv, &priv->child_intfs, list) {
1224 		matches += ipoib_match_gid_pkey_addr(child_priv, gid,
1225 		    pkey_index, addr, found_net_dev);
1226 		if (matches > 1)
1227 			break;
1228 	}
1229 	mutex_unlock(&priv->vlan_mutex);
1230 
1231 	return matches;
1232 }
1233 
1234 /*
1235  * __ipoib_get_net_dev_by_params - returns the number of matching
1236  * net_devs found (between 0 and 2). Also return the matching
1237  * net_device in the @net_dev parameter, holding a reference to the
1238  * net_device, if the number of matches >= 1
1239  */
1240 static int
1241 __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port,
1242     u16 pkey_index, const union ib_gid *gid,
1243     const struct sockaddr *addr, struct net_device **net_dev)
1244 {
1245 	struct ipoib_dev_priv *priv;
1246 	int matches = 0;
1247 
1248 	*net_dev = NULL;
1249 
1250 	list_for_each_entry(priv, dev_list, list) {
1251 		if (priv->port != port)
1252 			continue;
1253 
1254 		matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
1255 		    addr, net_dev);
1256 
1257 		if (matches > 1)
1258 			break;
1259 	}
1260 
1261 	return matches;
1262 }
1263 
1264 static struct net_device *
1265 ipoib_get_net_dev_by_params(struct ib_device *dev, u8 port, u16 pkey,
1266     const union ib_gid *gid, const struct sockaddr *addr, void *client_data)
1267 {
1268 	struct net_device *net_dev;
1269 	struct list_head *dev_list = client_data;
1270 	u16 pkey_index;
1271 	int matches;
1272 	int ret;
1273 
1274 	if (!rdma_protocol_ib(dev, port))
1275 		return NULL;
1276 
1277 	ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
1278 	if (ret)
1279 		return NULL;
1280 
1281 	if (!dev_list)
1282 		return NULL;
1283 
1284 	/* See if we can find a unique device matching the L2 parameters */
1285 	matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
1286 						gid, NULL, &net_dev);
1287 
1288 	switch (matches) {
1289 	case 0:
1290 		return NULL;
1291 	case 1:
1292 		return net_dev;
1293 	}
1294 
1295 	dev_put(net_dev);
1296 
1297 	/* Couldn't find a unique device with L2 parameters only. Use L3
1298 	 * address to uniquely match the net device */
1299 	matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
1300 						gid, addr, &net_dev);
1301 	switch (matches) {
1302 	case 0:
1303 		return NULL;
1304 	default:
1305 		dev_warn_ratelimited(&dev->dev,
1306 				     "duplicate IP address detected\n");
1307 		/* Fall through */
1308 	case 1:
1309 		return net_dev;
1310 	}
1311 }
1312 
1313 static void
1314 ipoib_config_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
1315 {
1316 	struct ipoib_dev_priv *parent;
1317 	struct ipoib_dev_priv *priv;
1318 	struct epoch_tracker et;
1319 	struct ifnet *dev;
1320 	uint16_t pkey;
1321 	int error;
1322 
1323 	if (ifp->if_type != IFT_INFINIBAND)
1324 		return;
1325 	NET_EPOCH_ENTER(et);
1326 	dev = VLAN_DEVAT(ifp, vtag);
1327 	NET_EPOCH_EXIT(et);
1328 	if (dev == NULL)
1329 		return;
1330 	priv = NULL;
1331 	error = 0;
1332 	parent = ifp->if_softc;
1333 	/* We only support 15 bits of pkey. */
1334 	if (vtag & 0x8000)
1335 		return;
1336 	pkey = vtag | 0x8000;	/* Set full membership bit. */
1337 	if (pkey == parent->pkey)
1338 		return;
1339 	/* Check for dups */
1340 	mutex_lock(&parent->vlan_mutex);
1341 	list_for_each_entry(priv, &parent->child_intfs, list) {
1342 		if (priv->pkey == pkey) {
1343 			priv = NULL;
1344 			error = EBUSY;
1345 			goto out;
1346 		}
1347 	}
1348 	priv = ipoib_priv_alloc();
1349 	priv->dev = dev;
1350 	priv->max_ib_mtu = parent->max_ib_mtu;
1351 	priv->mcast_mtu = priv->admin_mtu = parent->dev->if_mtu;
1352 	set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
1353 	error = ipoib_set_dev_features(priv, parent->ca);
1354 	if (error)
1355 		goto out;
1356 	priv->pkey = pkey;
1357 	priv->broadcastaddr[8] = pkey >> 8;
1358 	priv->broadcastaddr[9] = pkey & 0xff;
1359 	dev->if_broadcastaddr = priv->broadcastaddr;
1360 	error = ipoib_dev_init(priv, parent->ca, parent->port);
1361 	if (error)
1362 		goto out;
1363 	priv->parent = parent->dev;
1364 	list_add_tail(&priv->list, &parent->child_intfs);
1365 	VLAN_SETCOOKIE(dev, priv);
1366 	dev->if_start = ipoib_vlan_start;
1367 	dev->if_drv_flags &= ~IFF_DRV_RUNNING;
1368 	dev->if_hdrlen = IPOIB_HEADER_LEN;
1369 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1370 		ipoib_open(priv);
1371 	mutex_unlock(&parent->vlan_mutex);
1372 	return;
1373 out:
1374 	mutex_unlock(&parent->vlan_mutex);
1375 	if (priv)
1376 		free(priv, M_TEMP);
1377 	if (error)
1378 		ipoib_warn(parent,
1379 		    "failed to initialize subinterface: device %s, port %d vtag 0x%X",
1380 		    parent->ca->name, parent->port, vtag);
1381 	return;
1382 }
1383 
1384 static void
1385 ipoib_unconfig_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
1386 {
1387 	struct ipoib_dev_priv *parent;
1388 	struct ipoib_dev_priv *priv;
1389 	struct epoch_tracker et;
1390 	struct ifnet *dev;
1391 	uint16_t pkey;
1392 
1393 	if (ifp->if_type != IFT_INFINIBAND)
1394 		return;
1395 
1396 	NET_EPOCH_ENTER(et);
1397 	dev = VLAN_DEVAT(ifp, vtag);
1398 	NET_EPOCH_EXIT(et);
1399 	if (dev)
1400 		VLAN_SETCOOKIE(dev, NULL);
1401 	pkey = vtag | 0x8000;
1402 	parent = ifp->if_softc;
1403 	mutex_lock(&parent->vlan_mutex);
1404 	list_for_each_entry(priv, &parent->child_intfs, list) {
1405 		if (priv->pkey == pkey) {
1406 			ipoib_dev_cleanup(priv);
1407 			list_del(&priv->list);
1408 			break;
1409 		}
1410 	}
1411 	mutex_unlock(&parent->vlan_mutex);
1412 }
1413 
1414 eventhandler_tag ipoib_vlan_attach;
1415 eventhandler_tag ipoib_vlan_detach;
1416 
1417 static int __init
1418 ipoib_init_module(void)
1419 {
1420 	int ret;
1421 
1422 	ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1423 	ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1424 	ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1425 
1426 	ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1427 	ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1428 	ipoib_sendq_size = max(ipoib_sendq_size, max(2 * MAX_SEND_CQE,
1429 						     IPOIB_MIN_QUEUE_SIZE));
1430 #ifdef CONFIG_INFINIBAND_IPOIB_CM
1431 	ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1432 #endif
1433 
1434 	ipoib_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
1435 		ipoib_config_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1436 	ipoib_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
1437 		ipoib_unconfig_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1438 
1439 	/*
1440 	 * We create our own workqueue mainly because we want to be
1441 	 * able to flush it when devices are being removed.  We can't
1442 	 * use schedule_work()/flush_scheduled_work() because both
1443 	 * unregister_netdev() and linkwatch_event take the rtnl lock,
1444 	 * so flush_scheduled_work() can deadlock during device
1445 	 * removal.
1446 	 */
1447 	ipoib_workqueue = create_singlethread_workqueue("ipoib");
1448 	if (!ipoib_workqueue) {
1449 		ret = -ENOMEM;
1450 		goto err_fs;
1451 	}
1452 
1453 	ib_sa_register_client(&ipoib_sa_client);
1454 
1455 	ret = ib_register_client(&ipoib_client);
1456 	if (ret)
1457 		goto err_sa;
1458 
1459 	return 0;
1460 
1461 err_sa:
1462 	ib_sa_unregister_client(&ipoib_sa_client);
1463 	destroy_workqueue(ipoib_workqueue);
1464 
1465 err_fs:
1466 	return ret;
1467 }
1468 
1469 static void __exit
1470 ipoib_cleanup_module(void)
1471 {
1472 
1473 	EVENTHANDLER_DEREGISTER(vlan_config, ipoib_vlan_attach);
1474 	EVENTHANDLER_DEREGISTER(vlan_unconfig, ipoib_vlan_detach);
1475 	ib_unregister_client(&ipoib_client);
1476 	ib_sa_unregister_client(&ipoib_sa_client);
1477 	destroy_workqueue(ipoib_workqueue);
1478 }
1479 
1480 /*
1481  * Infiniband output routine.
1482  */
1483 static int
1484 ipoib_output(struct ifnet *ifp, struct mbuf *m,
1485 	const struct sockaddr *dst, struct route *ro)
1486 {
1487 	u_char edst[INFINIBAND_ALEN];
1488 #if defined(INET) || defined(INET6)
1489 	struct llentry *lle = NULL;
1490 #endif
1491 	struct ipoib_header *eh;
1492 	int error = 0, is_gw = 0;
1493 	short type;
1494 
1495 	NET_EPOCH_ASSERT();
1496 
1497 	if (ro != NULL)
1498 		is_gw = (ro->ro_flags & RT_HAS_GW) != 0;
1499 #ifdef MAC
1500 	error = mac_ifnet_check_transmit(ifp, m);
1501 	if (error)
1502 		goto bad;
1503 #endif
1504 
1505 	M_PROFILE(m);
1506 	if (ifp->if_flags & IFF_MONITOR) {
1507 		error = ENETDOWN;
1508 		goto bad;
1509 	}
1510 	if (!((ifp->if_flags & IFF_UP) &&
1511 	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
1512 		error = ENETDOWN;
1513 		goto bad;
1514 	}
1515 
1516 	switch (dst->sa_family) {
1517 #ifdef INET
1518 	case AF_INET:
1519 		if (lle != NULL && (lle->la_flags & LLE_VALID))
1520 			memcpy(edst, lle->ll_addr, sizeof(edst));
1521 		else if (m->m_flags & M_MCAST)
1522 			ip_ib_mc_map(((struct sockaddr_in *)dst)->sin_addr.s_addr, ifp->if_broadcastaddr, edst);
1523 		else
1524 			error = arpresolve(ifp, is_gw, m, dst, edst, NULL, NULL);
1525 		if (error)
1526 			return (error == EWOULDBLOCK ? 0 : error);
1527 		type = htons(ETHERTYPE_IP);
1528 		break;
1529 	case AF_ARP:
1530 	{
1531 		struct arphdr *ah;
1532 		ah = mtod(m, struct arphdr *);
1533 		ah->ar_hrd = htons(ARPHRD_INFINIBAND);
1534 
1535 		switch(ntohs(ah->ar_op)) {
1536 		case ARPOP_REVREQUEST:
1537 		case ARPOP_REVREPLY:
1538 			type = htons(ETHERTYPE_REVARP);
1539 			break;
1540 		case ARPOP_REQUEST:
1541 		case ARPOP_REPLY:
1542 		default:
1543 			type = htons(ETHERTYPE_ARP);
1544 			break;
1545 		}
1546 
1547 		if (m->m_flags & M_BCAST)
1548 			bcopy(ifp->if_broadcastaddr, edst, INFINIBAND_ALEN);
1549 		else
1550 			bcopy(ar_tha(ah), edst, INFINIBAND_ALEN);
1551 
1552 	}
1553 	break;
1554 #endif
1555 #ifdef INET6
1556 	case AF_INET6:
1557 		if (lle != NULL && (lle->la_flags & LLE_VALID))
1558 			memcpy(edst, lle->ll_addr, sizeof(edst));
1559 		else if (m->m_flags & M_MCAST)
1560 			ipv6_ib_mc_map(&((struct sockaddr_in6 *)dst)->sin6_addr, ifp->if_broadcastaddr, edst);
1561 		else
1562 			error = nd6_resolve(ifp, is_gw, m, dst, edst, NULL, NULL);
1563 		if (error)
1564 			return error;
1565 		type = htons(ETHERTYPE_IPV6);
1566 		break;
1567 #endif
1568 
1569 	default:
1570 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
1571 		error = EAFNOSUPPORT;
1572 		goto bad;
1573 	}
1574 
1575 	/*
1576 	 * Add local net header.  If no space in first mbuf,
1577 	 * allocate another.
1578 	 */
1579 	M_PREPEND(m, IPOIB_HEADER_LEN, M_NOWAIT);
1580 	if (m == NULL) {
1581 		error = ENOBUFS;
1582 		goto bad;
1583 	}
1584 	eh = mtod(m, struct ipoib_header *);
1585 	(void)memcpy(&eh->proto, &type, sizeof(eh->proto));
1586 	(void)memcpy(&eh->hwaddr, edst, sizeof (edst));
1587 
1588 	/*
1589 	 * Queue message on interface, update output statistics if
1590 	 * successful, and start output if interface not yet active.
1591 	 */
1592 	return ((ifp->if_transmit)(ifp, m));
1593 bad:
1594 	if (m != NULL)
1595 		m_freem(m);
1596 	return (error);
1597 }
1598 
1599 /*
1600  * Upper layer processing for a received Infiniband packet.
1601  */
1602 void
1603 ipoib_demux(struct ifnet *ifp, struct mbuf *m, u_short proto)
1604 {
1605 	struct epoch_tracker et;
1606 	int isr;
1607 
1608 #ifdef MAC
1609 	/*
1610 	 * Tag the mbuf with an appropriate MAC label before any other
1611 	 * consumers can get to it.
1612 	 */
1613 	mac_ifnet_create_mbuf(ifp, m);
1614 #endif
1615 	/* Allow monitor mode to claim this frame, after stats are updated. */
1616 	if (ifp->if_flags & IFF_MONITOR) {
1617 		if_printf(ifp, "discard frame at IFF_MONITOR\n");
1618 		m_freem(m);
1619 		return;
1620 	}
1621 	/*
1622 	 * Dispatch frame to upper layer.
1623 	 */
1624 	switch (proto) {
1625 #ifdef INET
1626 	case ETHERTYPE_IP:
1627 		isr = NETISR_IP;
1628 		break;
1629 
1630 	case ETHERTYPE_ARP:
1631 		if (ifp->if_flags & IFF_NOARP) {
1632 			/* Discard packet if ARP is disabled on interface */
1633 			m_freem(m);
1634 			return;
1635 		}
1636 		isr = NETISR_ARP;
1637 		break;
1638 #endif
1639 #ifdef INET6
1640 	case ETHERTYPE_IPV6:
1641 		isr = NETISR_IPV6;
1642 		break;
1643 #endif
1644 	default:
1645 		goto discard;
1646 	}
1647 	NET_EPOCH_ENTER(et);
1648 	netisr_dispatch(isr, m);
1649 	NET_EPOCH_EXIT(et);
1650 	return;
1651 
1652 discard:
1653 	m_freem(m);
1654 }
1655 
1656 /*
1657  * Process a received Infiniband packet.
1658  */
1659 static void
1660 ipoib_input(struct ifnet *ifp, struct mbuf *m)
1661 {
1662 	struct ipoib_header *eh;
1663 
1664 	if ((ifp->if_flags & IFF_UP) == 0) {
1665 		m_freem(m);
1666 		return;
1667 	}
1668 	CURVNET_SET_QUIET(ifp->if_vnet);
1669 
1670 	/* Let BPF have it before we strip the header. */
1671 	IPOIB_MTAP(ifp, m);
1672 	eh = mtod(m, struct ipoib_header *);
1673 	/*
1674 	 * Reset layer specific mbuf flags to avoid confusing upper layers.
1675 	 * Strip off Infiniband header.
1676 	 */
1677 	m->m_flags &= ~M_VLANTAG;
1678 	m_clrprotoflags(m);
1679 	m_adj(m, IPOIB_HEADER_LEN);
1680 
1681 	if (IPOIB_IS_MULTICAST(eh->hwaddr)) {
1682 		if (memcmp(eh->hwaddr, ifp->if_broadcastaddr,
1683 		    ifp->if_addrlen) == 0)
1684 			m->m_flags |= M_BCAST;
1685 		else
1686 			m->m_flags |= M_MCAST;
1687 		if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
1688 	}
1689 
1690 	ipoib_demux(ifp, m, ntohs(eh->proto));
1691 	CURVNET_RESTORE();
1692 }
1693 
1694 static int
1695 ipoib_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1696 	struct sockaddr *sa)
1697 {
1698 	struct sockaddr_dl *sdl;
1699 #ifdef INET
1700 	struct sockaddr_in *sin;
1701 #endif
1702 #ifdef INET6
1703 	struct sockaddr_in6 *sin6;
1704 #endif
1705 	u_char *e_addr;
1706 
1707 	switch(sa->sa_family) {
1708 	case AF_LINK:
1709 		/*
1710 		 * No mapping needed. Just check that it's a valid MC address.
1711 		 */
1712 		sdl = (struct sockaddr_dl *)sa;
1713 		e_addr = LLADDR(sdl);
1714 		if (!IPOIB_IS_MULTICAST(e_addr))
1715 			return EADDRNOTAVAIL;
1716 		*llsa = NULL;
1717 		return 0;
1718 
1719 #ifdef INET
1720 	case AF_INET:
1721 		sin = (struct sockaddr_in *)sa;
1722 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1723 			return EADDRNOTAVAIL;
1724 		sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND);
1725 		sdl->sdl_alen = INFINIBAND_ALEN;
1726 		e_addr = LLADDR(sdl);
1727 		ip_ib_mc_map(sin->sin_addr.s_addr, ifp->if_broadcastaddr,
1728 		    e_addr);
1729 		*llsa = (struct sockaddr *)sdl;
1730 		return 0;
1731 #endif
1732 #ifdef INET6
1733 	case AF_INET6:
1734 		sin6 = (struct sockaddr_in6 *)sa;
1735 		/*
1736 		 * An IP6 address of 0 means listen to all
1737 		 * of the multicast address used for IP6.
1738 		 * This has no meaning in ipoib.
1739 		 */
1740 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
1741 			return EADDRNOTAVAIL;
1742 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1743 			return EADDRNOTAVAIL;
1744 		sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND);
1745 		sdl->sdl_alen = INFINIBAND_ALEN;
1746 		e_addr = LLADDR(sdl);
1747 		ipv6_ib_mc_map(&sin6->sin6_addr, ifp->if_broadcastaddr, e_addr);
1748 		*llsa = (struct sockaddr *)sdl;
1749 		return 0;
1750 #endif
1751 
1752 	default:
1753 		return EAFNOSUPPORT;
1754 	}
1755 }
1756 
1757 module_init(ipoib_init_module);
1758 module_exit(ipoib_cleanup_module);
1759 
1760 static int
1761 ipoib_evhand(module_t mod, int event, void *arg)
1762 {
1763 	                return (0);
1764 }
1765 
1766 static moduledata_t ipoib_mod = {
1767 	                .name = "ipoib",
1768 			                .evhand = ipoib_evhand,
1769 };
1770 
1771 DECLARE_MODULE(ipoib, ipoib_mod, SI_SUB_LAST, SI_ORDER_ANY);
1772 MODULE_DEPEND(ipoib, ibcore, 1, 1, 1);
1773 MODULE_DEPEND(ipoib, linuxkpi, 1, 1, 1);
1774