xref: /freebsd/sys/ofed/drivers/infiniband/ulp/ipoib/ipoib_main.c (revision d65cd7a57bf0600b722afc770838a5d0c1c3a8e1)
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 				if (new_path)
735 					ipoib_path_free(priv, path);
736 				return;
737 			} else
738 				__path_add(priv, path);
739 		} else {
740 			if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
741 			m_freem(mb);
742 		}
743 
744 		return;
745 	}
746 
747 	if (ipoib_cm_get(path) && ipoib_cm_up(path)) {
748 		ipoib_cm_send(priv, mb, ipoib_cm_get(path));
749 	} else if (path->ah) {
750 		ipoib_send(priv, mb, path->ah, IPOIB_QPN(eh->hwaddr));
751 	} else if ((path->query || !path_rec_start(priv, path)) &&
752 		    path->queue.ifq_len < IPOIB_MAX_PATH_REC_QUEUE) {
753 		_IF_ENQUEUE(&path->queue, mb);
754 	} else {
755 		if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
756 		m_freem(mb);
757 	}
758 }
759 
760 static int
761 ipoib_send_one(struct ipoib_dev_priv *priv, struct mbuf *mb)
762 {
763 	struct ipoib_header *eh;
764 
765 	eh = mtod(mb, struct ipoib_header *);
766 	if (IPOIB_IS_MULTICAST(eh->hwaddr)) {
767 		/* Add in the P_Key for multicast*/
768 		eh->hwaddr[8] = (priv->pkey >> 8) & 0xff;
769 		eh->hwaddr[9] = priv->pkey & 0xff;
770 
771 		ipoib_mcast_send(priv, eh->hwaddr + 4, mb);
772 	} else
773 		ipoib_unicast_send(mb, priv, eh);
774 
775 	return 0;
776 }
777 
778 void
779 ipoib_start_locked(struct ifnet *dev, struct ipoib_dev_priv *priv)
780 {
781 	struct mbuf *mb;
782 
783 	assert_spin_locked(&priv->lock);
784 
785 	while (!IFQ_DRV_IS_EMPTY(&dev->if_snd) &&
786 	    (dev->if_drv_flags & IFF_DRV_OACTIVE) == 0) {
787 		IFQ_DRV_DEQUEUE(&dev->if_snd, mb);
788 		if (mb == NULL)
789 			break;
790 		IPOIB_MTAP(dev, mb);
791 		ipoib_send_one(priv, mb);
792 	}
793 }
794 
795 static void
796 _ipoib_start(struct ifnet *dev, struct ipoib_dev_priv *priv)
797 {
798 
799 	if ((dev->if_drv_flags & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
800 	    IFF_DRV_RUNNING)
801 		return;
802 
803 	spin_lock(&priv->lock);
804 	ipoib_start_locked(dev, priv);
805 	spin_unlock(&priv->lock);
806 }
807 
808 static void
809 ipoib_start(struct ifnet *dev)
810 {
811 	_ipoib_start(dev, dev->if_softc);
812 }
813 
814 static void
815 ipoib_vlan_start(struct ifnet *dev)
816 {
817 	struct ipoib_dev_priv *priv;
818 	struct mbuf *mb;
819 
820 	priv = VLAN_COOKIE(dev);
821 	if (priv != NULL)
822 		return _ipoib_start(dev, priv);
823 	while (!IFQ_DRV_IS_EMPTY(&dev->if_snd)) {
824 		IFQ_DRV_DEQUEUE(&dev->if_snd, mb);
825 		if (mb == NULL)
826 			break;
827 		m_freem(mb);
828 		if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
829 	}
830 }
831 
832 int
833 ipoib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca, int port)
834 {
835 
836 	/* Allocate RX/TX "rings" to hold queued mbs */
837 	priv->rx_ring =	kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
838 				GFP_KERNEL);
839 	if (!priv->rx_ring) {
840 		printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
841 		       ca->name, ipoib_recvq_size);
842 		goto out;
843 	}
844 
845 	priv->tx_ring = kzalloc(ipoib_sendq_size * sizeof *priv->tx_ring, GFP_KERNEL);
846 	if (!priv->tx_ring) {
847 		printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
848 		       ca->name, ipoib_sendq_size);
849 		goto out_rx_ring_cleanup;
850 	}
851 	memset(priv->tx_ring, 0, ipoib_sendq_size * sizeof *priv->tx_ring);
852 
853 	/* priv->tx_head, tx_tail & tx_outstanding are already 0 */
854 
855 	if (ipoib_ib_dev_init(priv, ca, port))
856 		goto out_tx_ring_cleanup;
857 
858 	return 0;
859 
860 out_tx_ring_cleanup:
861 	kfree(priv->tx_ring);
862 
863 out_rx_ring_cleanup:
864 	kfree(priv->rx_ring);
865 
866 out:
867 	return -ENOMEM;
868 }
869 
870 static void
871 ipoib_detach(struct ipoib_dev_priv *priv)
872 {
873 	struct ifnet *dev;
874 
875 	dev = priv->dev;
876 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
877 		priv->gone = 1;
878 		bpfdetach(dev);
879 		if_detach(dev);
880 		if_free(dev);
881 		free_unr(ipoib_unrhdr, priv->unit);
882 	} else
883 		VLAN_SETCOOKIE(priv->dev, NULL);
884 
885 	free(priv, M_TEMP);
886 }
887 
888 void
889 ipoib_dev_cleanup(struct ipoib_dev_priv *priv)
890 {
891 	struct ipoib_dev_priv *cpriv, *tcpriv;
892 
893 	/* Delete any child interfaces first */
894 	list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
895 		ipoib_dev_cleanup(cpriv);
896 		ipoib_detach(cpriv);
897 	}
898 
899 	ipoib_ib_dev_cleanup(priv);
900 
901 	kfree(priv->rx_ring);
902 	kfree(priv->tx_ring);
903 
904 	priv->rx_ring = NULL;
905 	priv->tx_ring = NULL;
906 }
907 
908 static struct ipoib_dev_priv *
909 ipoib_priv_alloc(void)
910 {
911 	struct ipoib_dev_priv *priv;
912 
913 	priv = malloc(sizeof(struct ipoib_dev_priv), M_TEMP, M_ZERO|M_WAITOK);
914 	spin_lock_init(&priv->lock);
915 	spin_lock_init(&priv->drain_lock);
916 	mutex_init(&priv->vlan_mutex);
917 	INIT_LIST_HEAD(&priv->path_list);
918 	INIT_LIST_HEAD(&priv->child_intfs);
919 	INIT_LIST_HEAD(&priv->dead_ahs);
920 	INIT_LIST_HEAD(&priv->multicast_list);
921 	INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
922 	INIT_DELAYED_WORK(&priv->mcast_task,   ipoib_mcast_join_task);
923 	INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
924 	INIT_WORK(&priv->flush_light,   ipoib_ib_dev_flush_light);
925 	INIT_WORK(&priv->flush_normal,   ipoib_ib_dev_flush_normal);
926 	INIT_WORK(&priv->flush_heavy,   ipoib_ib_dev_flush_heavy);
927 	INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
928 	INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
929 	memcpy(priv->broadcastaddr, ipv4_bcast_addr, INFINIBAND_ALEN);
930 
931 	return (priv);
932 }
933 
934 struct ipoib_dev_priv *
935 ipoib_intf_alloc(const char *name)
936 {
937 	struct ipoib_dev_priv *priv;
938 	struct sockaddr_dl *sdl;
939 	struct ifnet *dev;
940 
941 	priv = ipoib_priv_alloc();
942 	dev = priv->dev = if_alloc(IFT_INFINIBAND);
943 	if (!dev) {
944 		free(priv, M_TEMP);
945 		return NULL;
946 	}
947 	dev->if_softc = priv;
948 	priv->unit = alloc_unr(ipoib_unrhdr);
949 	if (priv->unit == -1) {
950 		if_free(dev);
951 		free(priv, M_TEMP);
952 		return NULL;
953 	}
954 	if_initname(dev, name, priv->unit);
955 	dev->if_flags = IFF_BROADCAST | IFF_MULTICAST;
956 	dev->if_addrlen = INFINIBAND_ALEN;
957 	dev->if_hdrlen = IPOIB_HEADER_LEN;
958 	if_attach(dev);
959 	dev->if_init = ipoib_init;
960 	dev->if_ioctl = ipoib_ioctl;
961 	dev->if_start = ipoib_start;
962 	dev->if_output = ipoib_output;
963 	dev->if_input = ipoib_input;
964 	dev->if_resolvemulti = ipoib_resolvemulti;
965 	dev->if_baudrate = IF_Gbps(10);
966 	dev->if_broadcastaddr = priv->broadcastaddr;
967 	dev->if_snd.ifq_maxlen = ipoib_sendq_size * 2;
968 	sdl = (struct sockaddr_dl *)dev->if_addr->ifa_addr;
969 	sdl->sdl_type = IFT_INFINIBAND;
970 	sdl->sdl_alen = dev->if_addrlen;
971 	priv->dev = dev;
972 	if_link_state_change(dev, LINK_STATE_DOWN);
973 	bpfattach(dev, DLT_EN10MB, ETHER_HDR_LEN);
974 
975 	return dev->if_softc;
976 }
977 
978 int
979 ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
980 {
981 	struct ib_device_attr *device_attr = &hca->attrs;
982 
983 	priv->hca_caps = device_attr->device_cap_flags;
984 
985 	priv->dev->if_hwassist = 0;
986 	priv->dev->if_capabilities = 0;
987 
988 #ifndef CONFIG_INFINIBAND_IPOIB_CM
989 	if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
990 		set_bit(IPOIB_FLAG_CSUM, &priv->flags);
991 		priv->dev->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP;
992 		priv->dev->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM;
993 	}
994 
995 #if 0
996 	if (priv->dev->features & NETIF_F_SG && priv->hca_caps & IB_DEVICE_UD_TSO) {
997 		priv->dev->if_capabilities |= IFCAP_TSO4;
998 		priv->dev->if_hwassist |= CSUM_TSO;
999 	}
1000 #endif
1001 #endif
1002 	priv->dev->if_capabilities |=
1003 	    IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_LINKSTATE;
1004 	priv->dev->if_capenable = priv->dev->if_capabilities;
1005 
1006 	return 0;
1007 }
1008 
1009 
1010 static struct ifnet *
1011 ipoib_add_port(const char *format, struct ib_device *hca, u8 port)
1012 {
1013 	struct ipoib_dev_priv *priv;
1014 	struct ib_port_attr attr;
1015 	int result = -ENOMEM;
1016 
1017 	priv = ipoib_intf_alloc(format);
1018 	if (!priv)
1019 		goto alloc_mem_failed;
1020 
1021 	if (!ib_query_port(hca, port, &attr))
1022 		priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1023 	else {
1024 		printk(KERN_WARNING "%s: ib_query_port %d failed\n",
1025 		       hca->name, port);
1026 		goto device_init_failed;
1027 	}
1028 
1029 	/* MTU will be reset when mcast join happens */
1030 	priv->dev->if_mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
1031 	priv->mcast_mtu = priv->admin_mtu = priv->dev->if_mtu;
1032 
1033 	result = ib_query_pkey(hca, port, 0, &priv->pkey);
1034 	if (result) {
1035 		printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1036 		       hca->name, port, result);
1037 		goto device_init_failed;
1038 	}
1039 
1040 	if (ipoib_set_dev_features(priv, hca))
1041 		goto device_init_failed;
1042 
1043 	/*
1044 	 * Set the full membership bit, so that we join the right
1045 	 * broadcast group, etc.
1046 	 */
1047 	priv->pkey |= 0x8000;
1048 
1049 	priv->broadcastaddr[8] = priv->pkey >> 8;
1050 	priv->broadcastaddr[9] = priv->pkey & 0xff;
1051 
1052 	result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
1053 	if (result) {
1054 		printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1055 		       hca->name, port, result);
1056 		goto device_init_failed;
1057 	}
1058 	memcpy(IF_LLADDR(priv->dev) + 4, priv->local_gid.raw, sizeof (union ib_gid));
1059 
1060 	result = ipoib_dev_init(priv, hca, port);
1061 	if (result < 0) {
1062 		printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1063 		       hca->name, port, result);
1064 		goto device_init_failed;
1065 	}
1066 	if (ipoib_cm_admin_enabled(priv))
1067 		priv->dev->if_mtu = IPOIB_CM_MTU(ipoib_cm_max_mtu(priv));
1068 
1069 	INIT_IB_EVENT_HANDLER(&priv->event_handler,
1070 			      priv->ca, ipoib_event);
1071 	result = ib_register_event_handler(&priv->event_handler);
1072 	if (result < 0) {
1073 		printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1074 		       "port %d (ret = %d)\n",
1075 		       hca->name, port, result);
1076 		goto event_failed;
1077 	}
1078 	if_printf(priv->dev, "Attached to %s port %d\n", hca->name, port);
1079 
1080 	return priv->dev;
1081 
1082 event_failed:
1083 	ipoib_dev_cleanup(priv);
1084 
1085 device_init_failed:
1086 	ipoib_detach(priv);
1087 
1088 alloc_mem_failed:
1089 	return ERR_PTR(result);
1090 }
1091 
1092 static void
1093 ipoib_add_one(struct ib_device *device)
1094 {
1095 	struct list_head *dev_list;
1096 	struct ifnet *dev;
1097 	struct ipoib_dev_priv *priv;
1098 	int s, e, p;
1099 
1100 	if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1101 		return;
1102 
1103 	dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1104 	if (!dev_list)
1105 		return;
1106 
1107 	INIT_LIST_HEAD(dev_list);
1108 
1109 	if (device->node_type == RDMA_NODE_IB_SWITCH) {
1110 		s = 0;
1111 		e = 0;
1112 	} else {
1113 		s = 1;
1114 		e = device->phys_port_cnt;
1115 	}
1116 
1117 	for (p = s; p <= e; ++p) {
1118 		if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
1119 			continue;
1120 		dev = ipoib_add_port("ib", device, p);
1121 		if (!IS_ERR(dev)) {
1122 			priv = dev->if_softc;
1123 			list_add_tail(&priv->list, dev_list);
1124 		}
1125 	}
1126 
1127 	ib_set_client_data(device, &ipoib_client, dev_list);
1128 }
1129 
1130 static void
1131 ipoib_remove_one(struct ib_device *device, void *client_data)
1132 {
1133 	struct ipoib_dev_priv *priv, *tmp;
1134 	struct list_head *dev_list = client_data;
1135 
1136 	if (!dev_list)
1137 		return;
1138 
1139 	if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1140 		return;
1141 
1142 	list_for_each_entry_safe(priv, tmp, dev_list, list) {
1143 		if (rdma_port_get_link_layer(device, priv->port) != IB_LINK_LAYER_INFINIBAND)
1144 			continue;
1145 
1146 		ipoib_stop(priv);
1147 
1148 		ib_unregister_event_handler(&priv->event_handler);
1149 
1150 		/* dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP); */
1151 
1152 		flush_workqueue(ipoib_workqueue);
1153 
1154 		ipoib_dev_cleanup(priv);
1155 		ipoib_detach(priv);
1156 	}
1157 
1158 	kfree(dev_list);
1159 }
1160 
1161 static int
1162 ipoib_match_dev_addr(const struct sockaddr *addr, struct net_device *dev)
1163 {
1164 	struct epoch_tracker et;
1165 	struct ifaddr *ifa;
1166 	int retval = 0;
1167 
1168 	CURVNET_SET(dev->if_vnet);
1169 	NET_EPOCH_ENTER(et);
1170 	CK_STAILQ_FOREACH(ifa, &dev->if_addrhead, ifa_link) {
1171 		if (ifa->ifa_addr == NULL ||
1172 		    ifa->ifa_addr->sa_family != addr->sa_family ||
1173 		    ifa->ifa_addr->sa_len != addr->sa_len) {
1174 			continue;
1175 		}
1176 		if (memcmp(ifa->ifa_addr, addr, addr->sa_len) == 0) {
1177 			retval = 1;
1178 			break;
1179 		}
1180 	}
1181 	NET_EPOCH_EXIT(et);
1182 	CURVNET_RESTORE();
1183 
1184 	return (retval);
1185 }
1186 
1187 /*
1188  * ipoib_match_gid_pkey_addr - returns the number of IPoIB netdevs on
1189  * top a given ipoib device matching a pkey_index and address, if one
1190  * exists.
1191  *
1192  * @found_net_dev: contains a matching net_device if the return value
1193  * >= 1, with a reference held.
1194  */
1195 static int
1196 ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
1197     const union ib_gid *gid, u16 pkey_index, const struct sockaddr *addr,
1198     struct net_device **found_net_dev)
1199 {
1200 	struct ipoib_dev_priv *child_priv;
1201 	int matches = 0;
1202 
1203 	if (priv->pkey_index == pkey_index &&
1204 	    (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
1205 		if (addr == NULL || ipoib_match_dev_addr(addr, priv->dev) != 0) {
1206 			if (*found_net_dev == NULL) {
1207 				struct net_device *net_dev;
1208 
1209 				if (priv->parent != NULL)
1210 					net_dev = priv->parent;
1211 				else
1212 					net_dev = priv->dev;
1213 				*found_net_dev = net_dev;
1214 				dev_hold(net_dev);
1215 			}
1216 			matches++;
1217 		}
1218 	}
1219 
1220 	/* Check child interfaces */
1221 	mutex_lock(&priv->vlan_mutex);
1222 	list_for_each_entry(child_priv, &priv->child_intfs, list) {
1223 		matches += ipoib_match_gid_pkey_addr(child_priv, gid,
1224 		    pkey_index, addr, found_net_dev);
1225 		if (matches > 1)
1226 			break;
1227 	}
1228 	mutex_unlock(&priv->vlan_mutex);
1229 
1230 	return matches;
1231 }
1232 
1233 /*
1234  * __ipoib_get_net_dev_by_params - returns the number of matching
1235  * net_devs found (between 0 and 2). Also return the matching
1236  * net_device in the @net_dev parameter, holding a reference to the
1237  * net_device, if the number of matches >= 1
1238  */
1239 static int
1240 __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port,
1241     u16 pkey_index, const union ib_gid *gid,
1242     const struct sockaddr *addr, struct net_device **net_dev)
1243 {
1244 	struct ipoib_dev_priv *priv;
1245 	int matches = 0;
1246 
1247 	*net_dev = NULL;
1248 
1249 	list_for_each_entry(priv, dev_list, list) {
1250 		if (priv->port != port)
1251 			continue;
1252 
1253 		matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
1254 		    addr, net_dev);
1255 
1256 		if (matches > 1)
1257 			break;
1258 	}
1259 
1260 	return matches;
1261 }
1262 
1263 static struct net_device *
1264 ipoib_get_net_dev_by_params(struct ib_device *dev, u8 port, u16 pkey,
1265     const union ib_gid *gid, const struct sockaddr *addr, void *client_data)
1266 {
1267 	struct net_device *net_dev;
1268 	struct list_head *dev_list = client_data;
1269 	u16 pkey_index;
1270 	int matches;
1271 	int ret;
1272 
1273 	if (!rdma_protocol_ib(dev, port))
1274 		return NULL;
1275 
1276 	ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
1277 	if (ret)
1278 		return NULL;
1279 
1280 	if (!dev_list)
1281 		return NULL;
1282 
1283 	/* See if we can find a unique device matching the L2 parameters */
1284 	matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
1285 						gid, NULL, &net_dev);
1286 
1287 	switch (matches) {
1288 	case 0:
1289 		return NULL;
1290 	case 1:
1291 		return net_dev;
1292 	}
1293 
1294 	dev_put(net_dev);
1295 
1296 	/* Couldn't find a unique device with L2 parameters only. Use L3
1297 	 * address to uniquely match the net device */
1298 	matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
1299 						gid, addr, &net_dev);
1300 	switch (matches) {
1301 	case 0:
1302 		return NULL;
1303 	default:
1304 		dev_warn_ratelimited(&dev->dev,
1305 				     "duplicate IP address detected\n");
1306 		/* Fall through */
1307 	case 1:
1308 		return net_dev;
1309 	}
1310 }
1311 
1312 static void
1313 ipoib_config_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
1314 {
1315 	struct ipoib_dev_priv *parent;
1316 	struct ipoib_dev_priv *priv;
1317 	struct epoch_tracker et;
1318 	struct ifnet *dev;
1319 	uint16_t pkey;
1320 	int error;
1321 
1322 	if (ifp->if_type != IFT_INFINIBAND)
1323 		return;
1324 	NET_EPOCH_ENTER(et);
1325 	dev = VLAN_DEVAT(ifp, vtag);
1326 	NET_EPOCH_EXIT(et);
1327 	if (dev == NULL)
1328 		return;
1329 	priv = NULL;
1330 	error = 0;
1331 	parent = ifp->if_softc;
1332 	/* We only support 15 bits of pkey. */
1333 	if (vtag & 0x8000)
1334 		return;
1335 	pkey = vtag | 0x8000;	/* Set full membership bit. */
1336 	if (pkey == parent->pkey)
1337 		return;
1338 	/* Check for dups */
1339 	mutex_lock(&parent->vlan_mutex);
1340 	list_for_each_entry(priv, &parent->child_intfs, list) {
1341 		if (priv->pkey == pkey) {
1342 			priv = NULL;
1343 			error = EBUSY;
1344 			goto out;
1345 		}
1346 	}
1347 	priv = ipoib_priv_alloc();
1348 	priv->dev = dev;
1349 	priv->max_ib_mtu = parent->max_ib_mtu;
1350 	priv->mcast_mtu = priv->admin_mtu = parent->dev->if_mtu;
1351 	set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
1352 	error = ipoib_set_dev_features(priv, parent->ca);
1353 	if (error)
1354 		goto out;
1355 	priv->pkey = pkey;
1356 	priv->broadcastaddr[8] = pkey >> 8;
1357 	priv->broadcastaddr[9] = pkey & 0xff;
1358 	dev->if_broadcastaddr = priv->broadcastaddr;
1359 	error = ipoib_dev_init(priv, parent->ca, parent->port);
1360 	if (error)
1361 		goto out;
1362 	priv->parent = parent->dev;
1363 	list_add_tail(&priv->list, &parent->child_intfs);
1364 	VLAN_SETCOOKIE(dev, priv);
1365 	dev->if_start = ipoib_vlan_start;
1366 	dev->if_drv_flags &= ~IFF_DRV_RUNNING;
1367 	dev->if_hdrlen = IPOIB_HEADER_LEN;
1368 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1369 		ipoib_open(priv);
1370 	mutex_unlock(&parent->vlan_mutex);
1371 	return;
1372 out:
1373 	mutex_unlock(&parent->vlan_mutex);
1374 	if (priv)
1375 		free(priv, M_TEMP);
1376 	if (error)
1377 		ipoib_warn(parent,
1378 		    "failed to initialize subinterface: device %s, port %d vtag 0x%X",
1379 		    parent->ca->name, parent->port, vtag);
1380 	return;
1381 }
1382 
1383 static void
1384 ipoib_unconfig_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
1385 {
1386 	struct ipoib_dev_priv *parent;
1387 	struct ipoib_dev_priv *priv;
1388 	struct epoch_tracker et;
1389 	struct ifnet *dev;
1390 	uint16_t pkey;
1391 
1392 	if (ifp->if_type != IFT_INFINIBAND)
1393 		return;
1394 
1395 	NET_EPOCH_ENTER(et);
1396 	dev = VLAN_DEVAT(ifp, vtag);
1397 	NET_EPOCH_EXIT(et);
1398 	if (dev)
1399 		VLAN_SETCOOKIE(dev, NULL);
1400 	pkey = vtag | 0x8000;
1401 	parent = ifp->if_softc;
1402 	mutex_lock(&parent->vlan_mutex);
1403 	list_for_each_entry(priv, &parent->child_intfs, list) {
1404 		if (priv->pkey == pkey) {
1405 			ipoib_dev_cleanup(priv);
1406 			list_del(&priv->list);
1407 			break;
1408 		}
1409 	}
1410 	mutex_unlock(&parent->vlan_mutex);
1411 }
1412 
1413 eventhandler_tag ipoib_vlan_attach;
1414 eventhandler_tag ipoib_vlan_detach;
1415 
1416 static int __init
1417 ipoib_init_module(void)
1418 {
1419 	int ret;
1420 
1421 	ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1422 	ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1423 	ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1424 
1425 	ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1426 	ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1427 	ipoib_sendq_size = max(ipoib_sendq_size, max(2 * MAX_SEND_CQE,
1428 						     IPOIB_MIN_QUEUE_SIZE));
1429 #ifdef CONFIG_INFINIBAND_IPOIB_CM
1430 	ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1431 #endif
1432 
1433 	ipoib_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
1434 		ipoib_config_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1435 	ipoib_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
1436 		ipoib_unconfig_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1437 
1438 	/*
1439 	 * We create our own workqueue mainly because we want to be
1440 	 * able to flush it when devices are being removed.  We can't
1441 	 * use schedule_work()/flush_scheduled_work() because both
1442 	 * unregister_netdev() and linkwatch_event take the rtnl lock,
1443 	 * so flush_scheduled_work() can deadlock during device
1444 	 * removal.
1445 	 */
1446 	ipoib_workqueue = create_singlethread_workqueue("ipoib");
1447 	if (!ipoib_workqueue) {
1448 		ret = -ENOMEM;
1449 		goto err_fs;
1450 	}
1451 
1452 	ib_sa_register_client(&ipoib_sa_client);
1453 
1454 	ret = ib_register_client(&ipoib_client);
1455 	if (ret)
1456 		goto err_sa;
1457 
1458 	return 0;
1459 
1460 err_sa:
1461 	ib_sa_unregister_client(&ipoib_sa_client);
1462 	destroy_workqueue(ipoib_workqueue);
1463 
1464 err_fs:
1465 	return ret;
1466 }
1467 
1468 static void __exit
1469 ipoib_cleanup_module(void)
1470 {
1471 
1472 	EVENTHANDLER_DEREGISTER(vlan_config, ipoib_vlan_attach);
1473 	EVENTHANDLER_DEREGISTER(vlan_unconfig, ipoib_vlan_detach);
1474 	ib_unregister_client(&ipoib_client);
1475 	ib_sa_unregister_client(&ipoib_sa_client);
1476 	destroy_workqueue(ipoib_workqueue);
1477 }
1478 
1479 /*
1480  * Infiniband output routine.
1481  */
1482 static int
1483 ipoib_output(struct ifnet *ifp, struct mbuf *m,
1484 	const struct sockaddr *dst, struct route *ro)
1485 {
1486 	u_char edst[INFINIBAND_ALEN];
1487 #if defined(INET) || defined(INET6)
1488 	struct llentry *lle = NULL;
1489 #endif
1490 	struct ipoib_header *eh;
1491 	int error = 0, is_gw = 0;
1492 	short type;
1493 
1494 	NET_EPOCH_ASSERT();
1495 
1496 	if (ro != NULL)
1497 		is_gw = (ro->ro_flags & RT_HAS_GW) != 0;
1498 #ifdef MAC
1499 	error = mac_ifnet_check_transmit(ifp, m);
1500 	if (error)
1501 		goto bad;
1502 #endif
1503 
1504 	M_PROFILE(m);
1505 	if (ifp->if_flags & IFF_MONITOR) {
1506 		error = ENETDOWN;
1507 		goto bad;
1508 	}
1509 	if (!((ifp->if_flags & IFF_UP) &&
1510 	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
1511 		error = ENETDOWN;
1512 		goto bad;
1513 	}
1514 
1515 	switch (dst->sa_family) {
1516 #ifdef INET
1517 	case AF_INET:
1518 		if (lle != NULL && (lle->la_flags & LLE_VALID))
1519 			memcpy(edst, lle->ll_addr, sizeof(edst));
1520 		else if (m->m_flags & M_MCAST)
1521 			ip_ib_mc_map(((struct sockaddr_in *)dst)->sin_addr.s_addr, ifp->if_broadcastaddr, edst);
1522 		else
1523 			error = arpresolve(ifp, is_gw, m, dst, edst, NULL, NULL);
1524 		if (error)
1525 			return (error == EWOULDBLOCK ? 0 : error);
1526 		type = htons(ETHERTYPE_IP);
1527 		break;
1528 	case AF_ARP:
1529 	{
1530 		struct arphdr *ah;
1531 		ah = mtod(m, struct arphdr *);
1532 		ah->ar_hrd = htons(ARPHRD_INFINIBAND);
1533 
1534 		switch(ntohs(ah->ar_op)) {
1535 		case ARPOP_REVREQUEST:
1536 		case ARPOP_REVREPLY:
1537 			type = htons(ETHERTYPE_REVARP);
1538 			break;
1539 		case ARPOP_REQUEST:
1540 		case ARPOP_REPLY:
1541 		default:
1542 			type = htons(ETHERTYPE_ARP);
1543 			break;
1544 		}
1545 
1546 		if (m->m_flags & M_BCAST)
1547 			bcopy(ifp->if_broadcastaddr, edst, INFINIBAND_ALEN);
1548 		else
1549 			bcopy(ar_tha(ah), edst, INFINIBAND_ALEN);
1550 
1551 	}
1552 	break;
1553 #endif
1554 #ifdef INET6
1555 	case AF_INET6:
1556 		if (lle != NULL && (lle->la_flags & LLE_VALID))
1557 			memcpy(edst, lle->ll_addr, sizeof(edst));
1558 		else if (m->m_flags & M_MCAST)
1559 			ipv6_ib_mc_map(&((struct sockaddr_in6 *)dst)->sin6_addr, ifp->if_broadcastaddr, edst);
1560 		else
1561 			error = nd6_resolve(ifp, is_gw, m, dst, edst, NULL, NULL);
1562 		if (error)
1563 			return error;
1564 		type = htons(ETHERTYPE_IPV6);
1565 		break;
1566 #endif
1567 
1568 	default:
1569 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
1570 		error = EAFNOSUPPORT;
1571 		goto bad;
1572 	}
1573 
1574 	/*
1575 	 * Add local net header.  If no space in first mbuf,
1576 	 * allocate another.
1577 	 */
1578 	M_PREPEND(m, IPOIB_HEADER_LEN, M_NOWAIT);
1579 	if (m == NULL) {
1580 		error = ENOBUFS;
1581 		goto bad;
1582 	}
1583 	eh = mtod(m, struct ipoib_header *);
1584 	(void)memcpy(&eh->proto, &type, sizeof(eh->proto));
1585 	(void)memcpy(&eh->hwaddr, edst, sizeof (edst));
1586 
1587 	/*
1588 	 * Queue message on interface, update output statistics if
1589 	 * successful, and start output if interface not yet active.
1590 	 */
1591 	return ((ifp->if_transmit)(ifp, m));
1592 bad:
1593 	if (m != NULL)
1594 		m_freem(m);
1595 	return (error);
1596 }
1597 
1598 /*
1599  * Upper layer processing for a received Infiniband packet.
1600  */
1601 void
1602 ipoib_demux(struct ifnet *ifp, struct mbuf *m, u_short proto)
1603 {
1604 	struct epoch_tracker et;
1605 	int isr;
1606 
1607 #ifdef MAC
1608 	/*
1609 	 * Tag the mbuf with an appropriate MAC label before any other
1610 	 * consumers can get to it.
1611 	 */
1612 	mac_ifnet_create_mbuf(ifp, m);
1613 #endif
1614 	/* Allow monitor mode to claim this frame, after stats are updated. */
1615 	if (ifp->if_flags & IFF_MONITOR) {
1616 		if_printf(ifp, "discard frame at IFF_MONITOR\n");
1617 		m_freem(m);
1618 		return;
1619 	}
1620 	/*
1621 	 * Dispatch frame to upper layer.
1622 	 */
1623 	switch (proto) {
1624 #ifdef INET
1625 	case ETHERTYPE_IP:
1626 		isr = NETISR_IP;
1627 		break;
1628 
1629 	case ETHERTYPE_ARP:
1630 		if (ifp->if_flags & IFF_NOARP) {
1631 			/* Discard packet if ARP is disabled on interface */
1632 			m_freem(m);
1633 			return;
1634 		}
1635 		isr = NETISR_ARP;
1636 		break;
1637 #endif
1638 #ifdef INET6
1639 	case ETHERTYPE_IPV6:
1640 		isr = NETISR_IPV6;
1641 		break;
1642 #endif
1643 	default:
1644 		goto discard;
1645 	}
1646 	NET_EPOCH_ENTER(et);
1647 	netisr_dispatch(isr, m);
1648 	NET_EPOCH_EXIT(et);
1649 	return;
1650 
1651 discard:
1652 	m_freem(m);
1653 }
1654 
1655 /*
1656  * Process a received Infiniband packet.
1657  */
1658 static void
1659 ipoib_input(struct ifnet *ifp, struct mbuf *m)
1660 {
1661 	struct ipoib_header *eh;
1662 
1663 	if ((ifp->if_flags & IFF_UP) == 0) {
1664 		m_freem(m);
1665 		return;
1666 	}
1667 	CURVNET_SET_QUIET(ifp->if_vnet);
1668 
1669 	/* Let BPF have it before we strip the header. */
1670 	IPOIB_MTAP(ifp, m);
1671 	eh = mtod(m, struct ipoib_header *);
1672 	/*
1673 	 * Reset layer specific mbuf flags to avoid confusing upper layers.
1674 	 * Strip off Infiniband header.
1675 	 */
1676 	m->m_flags &= ~M_VLANTAG;
1677 	m_clrprotoflags(m);
1678 	m_adj(m, IPOIB_HEADER_LEN);
1679 
1680 	if (IPOIB_IS_MULTICAST(eh->hwaddr)) {
1681 		if (memcmp(eh->hwaddr, ifp->if_broadcastaddr,
1682 		    ifp->if_addrlen) == 0)
1683 			m->m_flags |= M_BCAST;
1684 		else
1685 			m->m_flags |= M_MCAST;
1686 		if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
1687 	}
1688 
1689 	ipoib_demux(ifp, m, ntohs(eh->proto));
1690 	CURVNET_RESTORE();
1691 }
1692 
1693 static int
1694 ipoib_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1695 	struct sockaddr *sa)
1696 {
1697 	struct sockaddr_dl *sdl;
1698 #ifdef INET
1699 	struct sockaddr_in *sin;
1700 #endif
1701 #ifdef INET6
1702 	struct sockaddr_in6 *sin6;
1703 #endif
1704 	u_char *e_addr;
1705 
1706 	switch(sa->sa_family) {
1707 	case AF_LINK:
1708 		/*
1709 		 * No mapping needed. Just check that it's a valid MC address.
1710 		 */
1711 		sdl = (struct sockaddr_dl *)sa;
1712 		e_addr = LLADDR(sdl);
1713 		if (!IPOIB_IS_MULTICAST(e_addr))
1714 			return EADDRNOTAVAIL;
1715 		*llsa = NULL;
1716 		return 0;
1717 
1718 #ifdef INET
1719 	case AF_INET:
1720 		sin = (struct sockaddr_in *)sa;
1721 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1722 			return EADDRNOTAVAIL;
1723 		sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND);
1724 		sdl->sdl_alen = INFINIBAND_ALEN;
1725 		e_addr = LLADDR(sdl);
1726 		ip_ib_mc_map(sin->sin_addr.s_addr, ifp->if_broadcastaddr,
1727 		    e_addr);
1728 		*llsa = (struct sockaddr *)sdl;
1729 		return 0;
1730 #endif
1731 #ifdef INET6
1732 	case AF_INET6:
1733 		sin6 = (struct sockaddr_in6 *)sa;
1734 		/*
1735 		 * An IP6 address of 0 means listen to all
1736 		 * of the multicast address used for IP6.
1737 		 * This has no meaning in ipoib.
1738 		 */
1739 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
1740 			return EADDRNOTAVAIL;
1741 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1742 			return EADDRNOTAVAIL;
1743 		sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND);
1744 		sdl->sdl_alen = INFINIBAND_ALEN;
1745 		e_addr = LLADDR(sdl);
1746 		ipv6_ib_mc_map(&sin6->sin6_addr, ifp->if_broadcastaddr, e_addr);
1747 		*llsa = (struct sockaddr *)sdl;
1748 		return 0;
1749 #endif
1750 
1751 	default:
1752 		return EAFNOSUPPORT;
1753 	}
1754 }
1755 
1756 module_init(ipoib_init_module);
1757 module_exit(ipoib_cleanup_module);
1758 
1759 static int
1760 ipoib_evhand(module_t mod, int event, void *arg)
1761 {
1762 	return (0);
1763 }
1764 
1765 static moduledata_t ipoib_mod = {
1766 	.name = "ipoib",
1767 	.evhand = ipoib_evhand,
1768 };
1769 
1770 DECLARE_MODULE(ipoib, ipoib_mod, SI_SUB_LAST, SI_ORDER_ANY);
1771 MODULE_DEPEND(ipoib, ibcore, 1, 1, 1);
1772 MODULE_DEPEND(ipoib, linuxkpi, 1, 1, 1);
1773