xref: /freebsd/sys/ofed/drivers/infiniband/ulp/ipoib/ipoib_main.c (revision fee65b7e21eeeb625dcaba8cf2b9e0cf5af83498)
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 #include <linux/module.h>
44 
45 #include <linux/slab.h>
46 #include <linux/kernel.h>
47 #include <linux/vmalloc.h>
48 
49 #include <linux/if_vlan.h>
50 
51 #include <net/infiniband.h>
52 
53 #include <rdma/ib_addr.h>
54 #include <rdma/ib_cache.h>
55 
56 MODULE_AUTHOR("Roland Dreier");
57 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
58 MODULE_LICENSE("Dual BSD/GPL");
59 
60 int ipoib_sendq_size = IPOIB_TX_RING_SIZE;
61 int ipoib_recvq_size = IPOIB_RX_RING_SIZE;
62 
63 module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
64 MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
65 module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
66 MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
67 
68 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
69 int ipoib_debug_level = 1;
70 
71 module_param_named(debug_level, ipoib_debug_level, int, 0644);
72 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
73 #endif
74 
75 struct ipoib_path_iter {
76 	struct ipoib_dev_priv *priv;
77 	struct ipoib_path  path;
78 };
79 
80 static const u8 ipv4_bcast_addr[] = {
81 	0x00, 0xff, 0xff, 0xff,
82 	0xff, 0x12, 0x40, 0x1b,	0x00, 0x00, 0x00, 0x00,
83 	0x00, 0x00, 0x00, 0x00,	0xff, 0xff, 0xff, 0xff
84 };
85 
86 struct workqueue_struct *ipoib_workqueue;
87 
88 struct ib_sa_client ipoib_sa_client;
89 
90 static void ipoib_add_one(struct ib_device *device);
91 static void ipoib_remove_one(struct ib_device *device, void *client_data);
92 static if_t ipoib_get_net_dev_by_params(
93 		struct ib_device *dev, u8 port, u16 pkey,
94 		const union ib_gid *gid, const struct sockaddr *addr,
95 		void *client_data);
96 static void ipoib_start(if_t dev);
97 static int ipoib_ioctl(if_t ifp, u_long command, caddr_t data);
98 
99 static struct unrhdr *ipoib_unrhdr;
100 
101 static void
102 ipoib_unrhdr_init(void *arg)
103 {
104 
105 	ipoib_unrhdr = new_unrhdr(0, 65535, NULL);
106 }
107 SYSINIT(ipoib_unrhdr_init, SI_SUB_KLD - 1, SI_ORDER_ANY, ipoib_unrhdr_init, NULL);
108 
109 static void
110 ipoib_unrhdr_uninit(void *arg)
111 {
112 
113 	if (ipoib_unrhdr != NULL) {
114 		struct unrhdr *hdr;
115 
116 		hdr = ipoib_unrhdr;
117 		ipoib_unrhdr = NULL;
118 
119 		delete_unrhdr(hdr);
120 	}
121 }
122 SYSUNINIT(ipoib_unrhdr_uninit, SI_SUB_KLD - 1, SI_ORDER_ANY, ipoib_unrhdr_uninit, NULL);
123 
124 static struct ib_client ipoib_client = {
125 	.name   = "ipoib",
126 	.add    = ipoib_add_one,
127 	.remove = ipoib_remove_one,
128 	.get_net_dev_by_params = ipoib_get_net_dev_by_params,
129 };
130 
131 int
132 ipoib_open(struct ipoib_dev_priv *priv)
133 {
134 	if_t dev = priv->dev;
135 
136 	ipoib_dbg(priv, "bringing up interface\n");
137 
138 	set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
139 
140 	if (ipoib_pkey_dev_delay_open(priv))
141 		return 0;
142 
143 	if (ipoib_ib_dev_open(priv))
144 		goto err_disable;
145 
146 	if (ipoib_ib_dev_up(priv))
147 		goto err_stop;
148 
149 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
150 		struct ipoib_dev_priv *cpriv;
151 
152 		/* Bring up any child interfaces too */
153 		mutex_lock(&priv->vlan_mutex);
154 		list_for_each_entry(cpriv, &priv->child_intfs, list)
155 			if ((if_getdrvflags(cpriv->dev) & IFF_DRV_RUNNING) == 0)
156 				ipoib_open(cpriv);
157 		mutex_unlock(&priv->vlan_mutex);
158 	}
159 	if_setdrvflagbits(dev, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
160 
161 	return 0;
162 
163 err_stop:
164 	ipoib_ib_dev_stop(priv, 1);
165 
166 err_disable:
167 	clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
168 
169 	return -EINVAL;
170 }
171 
172 static void
173 ipoib_init(void *arg)
174 {
175 	if_t dev;
176 	struct ipoib_dev_priv *priv;
177 
178 	priv = arg;
179 	dev = priv->dev;
180 	if ((if_getdrvflags(dev) & IFF_DRV_RUNNING) == 0)
181 		ipoib_open(priv);
182 	queue_work(ipoib_workqueue, &priv->flush_light);
183 }
184 
185 
186 static int
187 ipoib_stop(struct ipoib_dev_priv *priv)
188 {
189 	if_t dev = priv->dev;
190 
191 	ipoib_dbg(priv, "stopping interface\n");
192 
193 	clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
194 
195 	if_setdrvflagbits(dev, 0, IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
196 
197 	ipoib_ib_dev_down(priv, 0);
198 	ipoib_ib_dev_stop(priv, 0);
199 
200 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
201 		struct ipoib_dev_priv *cpriv;
202 
203 		/* Bring down any child interfaces too */
204 		mutex_lock(&priv->vlan_mutex);
205 		list_for_each_entry(cpriv, &priv->child_intfs, list)
206 			if ((if_getdrvflags(cpriv->dev) & IFF_DRV_RUNNING) != 0)
207 				ipoib_stop(cpriv);
208 		mutex_unlock(&priv->vlan_mutex);
209 	}
210 
211 	return 0;
212 }
213 
214 static int
215 ipoib_propagate_ifnet_mtu(struct ipoib_dev_priv *priv, int new_mtu,
216     bool propagate)
217 {
218 	if_t ifp;
219 	struct ifreq ifr;
220 	int error;
221 
222 	ifp = priv->dev;
223 	if (if_getmtu(ifp) == new_mtu)
224 		return (0);
225 	if (propagate) {
226 		strlcpy(ifr.ifr_name, if_name(ifp), IFNAMSIZ);
227 		ifr.ifr_mtu = new_mtu;
228 		CURVNET_SET(if_getvnet(ifp));
229 		error = ifhwioctl(SIOCSIFMTU, ifp, (caddr_t)&ifr, curthread);
230 		CURVNET_RESTORE();
231 	} else {
232 		if_setmtu(ifp, new_mtu);
233 		error = 0;
234 	}
235 	return (error);
236 }
237 
238 int
239 ipoib_change_mtu(struct ipoib_dev_priv *priv, int new_mtu, bool propagate)
240 {
241 	int error, prev_admin_mtu;
242 
243 	/* dev->if_mtu > 2K ==> connected mode */
244 	if (ipoib_cm_admin_enabled(priv)) {
245 		if (new_mtu > IPOIB_CM_MTU(ipoib_cm_max_mtu(priv)))
246 			return -EINVAL;
247 
248 		if (new_mtu > priv->mcast_mtu)
249 			ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
250 				   priv->mcast_mtu);
251 
252 		return (ipoib_propagate_ifnet_mtu(priv, new_mtu, propagate));
253 	}
254 
255 	if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
256 		return -EINVAL;
257 
258 	prev_admin_mtu = priv->admin_mtu;
259 	priv->admin_mtu = new_mtu;
260 	error = ipoib_propagate_ifnet_mtu(priv, min(priv->mcast_mtu,
261 	    priv->admin_mtu), propagate);
262 	if (error == 0) {
263 		/* check for MTU change to avoid infinite loop */
264 		if (prev_admin_mtu != new_mtu)
265 			queue_work(ipoib_workqueue, &priv->flush_light);
266 	} else
267 		priv->admin_mtu = prev_admin_mtu;
268 	return (error);
269 }
270 
271 static int
272 ipoib_ioctl(if_t ifp, u_long command, caddr_t data)
273 {
274 	struct ipoib_dev_priv *priv = if_getsoftc(ifp);
275 	struct ifaddr *ifa = (struct ifaddr *) data;
276 	struct ifreq *ifr = (struct ifreq *) data;
277 	int error = 0;
278 
279 	/* check if detaching */
280 	if (priv == NULL)
281 		return (ENXIO);
282 	/* wait for device to become ready, if any */
283 	while (priv->gone == 2)
284 		pause("W", 1);
285 	/* check for device gone */
286 	if (priv->gone != 0)
287 		return (ENXIO);
288 
289 	switch (command) {
290 	case SIOCSIFFLAGS:
291 		if (if_getflags(ifp) & IFF_UP) {
292 			if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
293 				error = -ipoib_open(priv);
294 		} else
295 			if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
296 				ipoib_stop(priv);
297 		break;
298 	case SIOCADDMULTI:
299 	case SIOCDELMULTI:
300 		if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
301 			queue_work(ipoib_workqueue, &priv->restart_task);
302 		break;
303 	case SIOCSIFADDR:
304 		if_setflagbits(ifp, IFF_UP, 0);
305 
306 		switch (ifa->ifa_addr->sa_family) {
307 #ifdef INET
308 		case AF_INET:
309 			if_init(ifp, if_getsoftc(ifp));	/* before arpwhohas */
310 			arp_ifinit(ifp, ifa);
311 			break;
312 #endif
313 		default:
314 			if_init(ifp, if_getsoftc(ifp));
315 			break;
316 		}
317 		break;
318 
319 	case SIOCGIFADDR:
320 			bcopy(if_getlladdr(ifp), &ifr->ifr_addr.sa_data[0],
321                             INFINIBAND_ALEN);
322 		break;
323 
324 	case SIOCSIFMTU:
325 		/*
326 		 * Set the interface MTU.
327 		 */
328 		error = -ipoib_change_mtu(priv, ifr->ifr_mtu, false);
329 		break;
330 	default:
331 		error = EINVAL;
332 		break;
333 	}
334 	return (error);
335 }
336 
337 
338 static struct ipoib_path *
339 __path_find(struct ipoib_dev_priv *priv, void *gid)
340 {
341 	struct rb_node *n = priv->path_tree.rb_node;
342 	struct ipoib_path *path;
343 	int ret;
344 
345 	while (n) {
346 		path = rb_entry(n, struct ipoib_path, rb_node);
347 
348 		ret = memcmp(gid, path->pathrec.dgid.raw,
349 			     sizeof (union ib_gid));
350 
351 		if (ret < 0)
352 			n = n->rb_left;
353 		else if (ret > 0)
354 			n = n->rb_right;
355 		else
356 			return path;
357 	}
358 
359 	return NULL;
360 }
361 
362 static int
363 __path_add(struct ipoib_dev_priv *priv, struct ipoib_path *path)
364 {
365 	struct rb_node **n = &priv->path_tree.rb_node;
366 	struct rb_node *pn = NULL;
367 	struct ipoib_path *tpath;
368 	int ret;
369 
370 	while (*n) {
371 		pn = *n;
372 		tpath = rb_entry(pn, struct ipoib_path, rb_node);
373 
374 		ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
375 			     sizeof (union ib_gid));
376 		if (ret < 0)
377 			n = &pn->rb_left;
378 		else if (ret > 0)
379 			n = &pn->rb_right;
380 		else
381 			return -EEXIST;
382 	}
383 
384 	rb_link_node(&path->rb_node, pn, n);
385 	rb_insert_color(&path->rb_node, &priv->path_tree);
386 
387 	list_add_tail(&path->list, &priv->path_list);
388 
389 	return 0;
390 }
391 
392 void
393 ipoib_path_free(struct ipoib_dev_priv *priv, struct ipoib_path *path)
394 {
395 
396 	_IF_DRAIN(&path->queue);
397 
398 	if (path->ah)
399 		ipoib_put_ah(path->ah);
400 	if (ipoib_cm_get(path))
401 		ipoib_cm_destroy_tx(ipoib_cm_get(path));
402 
403 	kfree(path);
404 }
405 
406 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
407 
408 struct ipoib_path_iter *
409 ipoib_path_iter_init(struct ipoib_dev_priv *priv)
410 {
411 	struct ipoib_path_iter *iter;
412 
413 	iter = kmalloc(sizeof *iter, GFP_KERNEL);
414 	if (!iter)
415 		return NULL;
416 
417 	iter->priv = priv;
418 	memset(iter->path.pathrec.dgid.raw, 0, 16);
419 
420 	if (ipoib_path_iter_next(iter)) {
421 		kfree(iter);
422 		return NULL;
423 	}
424 
425 	return iter;
426 }
427 
428 int
429 ipoib_path_iter_next(struct ipoib_path_iter *iter)
430 {
431 	struct ipoib_dev_priv *priv = iter->priv;
432 	struct rb_node *n;
433 	struct ipoib_path *path;
434 	int ret = 1;
435 
436 	spin_lock_irq(&priv->lock);
437 
438 	n = rb_first(&priv->path_tree);
439 
440 	while (n) {
441 		path = rb_entry(n, struct ipoib_path, rb_node);
442 
443 		if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
444 			   sizeof (union ib_gid)) < 0) {
445 			iter->path = *path;
446 			ret = 0;
447 			break;
448 		}
449 
450 		n = rb_next(n);
451 	}
452 
453 	spin_unlock_irq(&priv->lock);
454 
455 	return ret;
456 }
457 
458 void
459 ipoib_path_iter_read(struct ipoib_path_iter *iter, struct ipoib_path *path)
460 {
461 	*path = iter->path;
462 }
463 
464 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
465 
466 void
467 ipoib_mark_paths_invalid(struct ipoib_dev_priv *priv)
468 {
469 	struct ipoib_path *path, *tp;
470 
471 	spin_lock_irq(&priv->lock);
472 
473 	list_for_each_entry_safe(path, tp, &priv->path_list, list) {
474 		ipoib_dbg(priv, "mark path LID 0x%04x GID %16D invalid\n",
475 			be16_to_cpu(path->pathrec.dlid),
476 			path->pathrec.dgid.raw, ":");
477 		path->valid =  0;
478 	}
479 
480 	spin_unlock_irq(&priv->lock);
481 }
482 
483 void
484 ipoib_flush_paths(struct ipoib_dev_priv *priv)
485 {
486 	struct ipoib_path *path, *tp;
487 	LIST_HEAD(remove_list);
488 	unsigned long flags;
489 
490 	spin_lock_irqsave(&priv->lock, flags);
491 
492 	list_splice_init(&priv->path_list, &remove_list);
493 
494 	list_for_each_entry(path, &remove_list, list)
495 		rb_erase(&path->rb_node, &priv->path_tree);
496 
497 	list_for_each_entry_safe(path, tp, &remove_list, list) {
498 		if (path->query)
499 			ib_sa_cancel_query(path->query_id, path->query);
500 		spin_unlock_irqrestore(&priv->lock, flags);
501 		wait_for_completion(&path->done);
502 		ipoib_path_free(priv, path);
503 		spin_lock_irqsave(&priv->lock, flags);
504 	}
505 
506 	spin_unlock_irqrestore(&priv->lock, flags);
507 }
508 
509 static void
510 path_rec_completion(int status, struct ib_sa_path_rec *pathrec, void *path_ptr)
511 {
512 	struct ipoib_path *path = path_ptr;
513 	struct ipoib_dev_priv *priv = path->priv;
514 	if_t dev = priv->dev;
515 	struct ipoib_ah *ah = NULL;
516 	struct ipoib_ah *old_ah = NULL;
517 	struct epoch_tracker et;
518 	struct ifqueue mbqueue;
519 	struct mbuf *mb;
520 	unsigned long flags;
521 
522 	if (!status)
523 		ipoib_dbg(priv, "PathRec LID 0x%04x for GID %16D\n",
524 			  be16_to_cpu(pathrec->dlid), pathrec->dgid.raw, ":");
525 	else
526 		ipoib_dbg(priv, "PathRec status %d for GID %16D\n",
527 			  status, path->pathrec.dgid.raw, ":");
528 
529 	bzero(&mbqueue, sizeof(mbqueue));
530 
531 	if (!status) {
532 		struct ib_ah_attr av;
533 
534 		if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
535 			ah = ipoib_create_ah(priv, priv->pd, &av);
536 	}
537 
538 	spin_lock_irqsave(&priv->lock, flags);
539 
540 	if (ah) {
541 		path->pathrec = *pathrec;
542 
543 		old_ah   = path->ah;
544 		path->ah = ah;
545 
546 		ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
547 			  ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
548 
549 		for (;;) {
550 			_IF_DEQUEUE(&path->queue, mb);
551 			if (mb == NULL)
552 				break;
553 			_IF_ENQUEUE(&mbqueue, mb);
554 		}
555 
556 #ifdef CONFIG_INFINIBAND_IPOIB_CM
557 		if (ipoib_cm_enabled(priv, path->hwaddr) && !ipoib_cm_get(path))
558 			ipoib_cm_set(path, ipoib_cm_create_tx(priv, path));
559 #endif
560 
561 		path->valid = 1;
562 	}
563 
564 	path->query = NULL;
565 	complete(&path->done);
566 
567 	spin_unlock_irqrestore(&priv->lock, flags);
568 
569 	if (old_ah)
570 		ipoib_put_ah(old_ah);
571 
572 	NET_EPOCH_ENTER(et);
573 	for (;;) {
574 		_IF_DEQUEUE(&mbqueue, mb);
575 		if (mb == NULL)
576 			break;
577 		mb->m_pkthdr.rcvif = dev;
578 		if (if_transmit(dev, mb))
579 			ipoib_warn(priv, "dev_queue_xmit failed "
580 				   "to requeue packet\n");
581 	}
582 	NET_EPOCH_EXIT(et);
583 }
584 
585 static struct ipoib_path *
586 path_rec_create(struct ipoib_dev_priv *priv, uint8_t *hwaddr)
587 {
588 	struct ipoib_path *path;
589 
590 	if (!priv->broadcast)
591 		return NULL;
592 
593 	path = kzalloc(sizeof *path, GFP_ATOMIC);
594 	if (!path)
595 		return NULL;
596 
597 	path->priv = priv;
598 
599 	bzero(&path->queue, sizeof(path->queue));
600 
601 #ifdef CONFIG_INFINIBAND_IPOIB_CM
602 	memcpy(&path->hwaddr, hwaddr, INFINIBAND_ALEN);
603 #endif
604 	memcpy(path->pathrec.dgid.raw, &hwaddr[4], sizeof (union ib_gid));
605 	path->pathrec.sgid	    = priv->local_gid;
606 	path->pathrec.pkey	    = cpu_to_be16(priv->pkey);
607 	path->pathrec.numb_path     = 1;
608 	path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
609 
610 	return path;
611 }
612 
613 static int
614 path_rec_start(struct ipoib_dev_priv *priv, struct ipoib_path *path)
615 {
616 	if_t dev = priv->dev;
617 
618 	ib_sa_comp_mask comp_mask = IB_SA_PATH_REC_MTU_SELECTOR | IB_SA_PATH_REC_MTU;
619 	struct ib_sa_path_rec p_rec;
620 
621 	p_rec = path->pathrec;
622 	p_rec.mtu_selector = IB_SA_GT;
623 
624 	switch (roundup_pow_of_two(if_getmtu(dev) + IPOIB_ENCAP_LEN)) {
625 	case 512:
626 		p_rec.mtu = IB_MTU_256;
627 		break;
628 	case 1024:
629 		p_rec.mtu = IB_MTU_512;
630 		break;
631 	case 2048:
632 		p_rec.mtu = IB_MTU_1024;
633 		break;
634 	case 4096:
635 		p_rec.mtu = IB_MTU_2048;
636 		break;
637 	default:
638 		/* Wildcard everything */
639 		comp_mask = 0;
640 		p_rec.mtu = 0;
641 		p_rec.mtu_selector = 0;
642 	}
643 
644 	ipoib_dbg(priv, "Start path record lookup for %16D MTU > %d\n",
645 		  p_rec.dgid.raw, ":",
646 		  comp_mask ? ib_mtu_enum_to_int(p_rec.mtu) : 0);
647 
648 	init_completion(&path->done);
649 
650 	path->query_id =
651 		ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
652 				   &p_rec, comp_mask		|
653 				   IB_SA_PATH_REC_DGID		|
654 				   IB_SA_PATH_REC_SGID		|
655 				   IB_SA_PATH_REC_NUMB_PATH	|
656 				   IB_SA_PATH_REC_TRAFFIC_CLASS |
657 				   IB_SA_PATH_REC_PKEY,
658 				   1000, GFP_ATOMIC,
659 				   path_rec_completion,
660 				   path, &path->query);
661 	if (path->query_id < 0) {
662 		ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
663 		path->query = NULL;
664 		complete(&path->done);
665 		return path->query_id;
666 	}
667 
668 	return 0;
669 }
670 
671 static void
672 ipoib_unicast_send(struct mbuf *mb, struct ipoib_dev_priv *priv, struct ipoib_header *eh)
673 {
674 	struct ipoib_path *path;
675 
676 	path = __path_find(priv, eh->hwaddr + 4);
677 	if (!path || !path->valid) {
678 		int new_path = 0;
679 
680 		if (!path) {
681 			path = path_rec_create(priv, eh->hwaddr);
682 			new_path = 1;
683 		}
684 		if (path) {
685 			if (_IF_QLEN(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE)
686 				_IF_ENQUEUE(&path->queue, mb);
687 			else {
688 				if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
689 				m_freem(mb);
690 			}
691 
692 			if (!path->query && path_rec_start(priv, path)) {
693 				if (new_path)
694 					ipoib_path_free(priv, path);
695 				return;
696 			} else
697 				__path_add(priv, path);
698 		} else {
699 			if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
700 			m_freem(mb);
701 		}
702 
703 		return;
704 	}
705 
706 	if (ipoib_cm_get(path) && ipoib_cm_up(path)) {
707 		ipoib_cm_send(priv, mb, ipoib_cm_get(path));
708 	} else if (path->ah) {
709 		ipoib_send(priv, mb, path->ah, IPOIB_QPN(eh->hwaddr));
710 	} else if ((path->query || !path_rec_start(priv, path)) &&
711 		    path->queue.ifq_len < IPOIB_MAX_PATH_REC_QUEUE) {
712 		_IF_ENQUEUE(&path->queue, mb);
713 	} else {
714 		if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
715 		m_freem(mb);
716 	}
717 }
718 
719 static int
720 ipoib_send_one(struct ipoib_dev_priv *priv, struct mbuf *mb)
721 {
722 	struct ipoib_header *eh;
723 
724 	eh = mtod(mb, struct ipoib_header *);
725 	if (IPOIB_IS_MULTICAST(eh->hwaddr)) {
726 		/* Add in the P_Key for multicast*/
727 		eh->hwaddr[8] = (priv->pkey >> 8) & 0xff;
728 		eh->hwaddr[9] = priv->pkey & 0xff;
729 
730 		ipoib_mcast_send(priv, eh->hwaddr + 4, mb);
731 	} else
732 		ipoib_unicast_send(mb, priv, eh);
733 
734 	return 0;
735 }
736 
737 void
738 ipoib_start_locked(if_t dev, struct ipoib_dev_priv *priv)
739 {
740 	struct mbuf *mb;
741 
742 	assert_spin_locked(&priv->lock);
743 
744 	while (!if_sendq_empty(dev) &&
745 	    (if_getdrvflags(dev) & IFF_DRV_OACTIVE) == 0) {
746 		mb = if_dequeue(dev);
747 		if (mb == NULL)
748 			break;
749 		infiniband_bpf_mtap(dev, mb);
750 		ipoib_send_one(priv, mb);
751 	}
752 }
753 
754 static void
755 _ipoib_start(if_t dev, struct ipoib_dev_priv *priv)
756 {
757 
758 	if ((if_getdrvflags(dev) & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
759 	    IFF_DRV_RUNNING)
760 		return;
761 
762 	spin_lock(&priv->lock);
763 	ipoib_start_locked(dev, priv);
764 	spin_unlock(&priv->lock);
765 }
766 
767 static void
768 ipoib_start(if_t dev)
769 {
770 	_ipoib_start(dev, if_getsoftc(dev));
771 }
772 
773 static void
774 ipoib_vlan_start(if_t dev)
775 {
776 	struct ipoib_dev_priv *priv;
777 	struct mbuf *mb;
778 
779 	priv = VLAN_COOKIE(dev);
780 	if (priv != NULL)
781 		return _ipoib_start(dev, priv);
782 	while (!if_sendq_empty(dev)) {
783 		mb = if_dequeue(dev);
784 		if (mb == NULL)
785 			break;
786 		m_freem(mb);
787 		if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
788 	}
789 }
790 
791 int
792 ipoib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca, int port)
793 {
794 
795 	/* Allocate RX/TX "rings" to hold queued mbs */
796 	priv->rx_ring =	kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
797 				GFP_KERNEL);
798 	if (!priv->rx_ring) {
799 		printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
800 		       ca->name, ipoib_recvq_size);
801 		goto out;
802 	}
803 
804 	priv->tx_ring = kzalloc(ipoib_sendq_size * sizeof *priv->tx_ring, GFP_KERNEL);
805 	if (!priv->tx_ring) {
806 		printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
807 		       ca->name, ipoib_sendq_size);
808 		goto out_rx_ring_cleanup;
809 	}
810 	memset(priv->tx_ring, 0, ipoib_sendq_size * sizeof *priv->tx_ring);
811 
812 	/* priv->tx_head, tx_tail & tx_outstanding are already 0 */
813 
814 	if (ipoib_ib_dev_init(priv, ca, port))
815 		goto out_tx_ring_cleanup;
816 
817 	return 0;
818 
819 out_tx_ring_cleanup:
820 	kfree(priv->tx_ring);
821 
822 out_rx_ring_cleanup:
823 	kfree(priv->rx_ring);
824 
825 out:
826 	return -ENOMEM;
827 }
828 
829 static void
830 ipoib_ifdetach(struct ipoib_dev_priv *priv)
831 {
832 	if_t dev;
833 
834 	dev = priv->dev;
835 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
836 		priv->gone = 1;
837 		infiniband_ifdetach(dev);
838 	}
839 }
840 
841 static void
842 ipoib_detach(struct ipoib_dev_priv *priv)
843 {
844 	if_t dev;
845 
846 	dev = priv->dev;
847 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
848 		if_free(dev);
849 		free_unr(ipoib_unrhdr, priv->unit);
850 	} else
851 		VLAN_SETCOOKIE(priv->dev, NULL);
852 
853 	free(priv, M_TEMP);
854 }
855 
856 void
857 ipoib_dev_cleanup(struct ipoib_dev_priv *priv)
858 {
859 	struct ipoib_dev_priv *cpriv, *tcpriv;
860 
861 	/* Delete any child interfaces first */
862 	list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
863 		ipoib_ifdetach(cpriv);
864 		ipoib_dev_cleanup(cpriv);
865 		ipoib_detach(cpriv);
866 	}
867 
868 	ipoib_ib_dev_cleanup(priv);
869 
870 	kfree(priv->rx_ring);
871 	kfree(priv->tx_ring);
872 
873 	priv->rx_ring = NULL;
874 	priv->tx_ring = NULL;
875 }
876 
877 static struct ipoib_dev_priv *
878 ipoib_priv_alloc(void)
879 {
880 	struct ipoib_dev_priv *priv;
881 
882 	priv = malloc(sizeof(struct ipoib_dev_priv), M_TEMP, M_ZERO|M_WAITOK);
883 	spin_lock_init(&priv->lock);
884 	spin_lock_init(&priv->drain_lock);
885 	mutex_init(&priv->vlan_mutex);
886 	INIT_LIST_HEAD(&priv->path_list);
887 	INIT_LIST_HEAD(&priv->child_intfs);
888 	INIT_LIST_HEAD(&priv->dead_ahs);
889 	INIT_LIST_HEAD(&priv->multicast_list);
890 	INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
891 	INIT_DELAYED_WORK(&priv->mcast_task,   ipoib_mcast_join_task);
892 	INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
893 	INIT_WORK(&priv->flush_light,   ipoib_ib_dev_flush_light);
894 	INIT_WORK(&priv->flush_normal,   ipoib_ib_dev_flush_normal);
895 	INIT_WORK(&priv->flush_heavy,   ipoib_ib_dev_flush_heavy);
896 	INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
897 	INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
898 	memcpy(priv->broadcastaddr, ipv4_bcast_addr, INFINIBAND_ALEN);
899 
900 	return (priv);
901 }
902 
903 struct ipoib_dev_priv *
904 ipoib_intf_alloc(const char *name)
905 {
906 	struct ipoib_dev_priv *priv;
907 	if_t dev;
908 
909 	priv = ipoib_priv_alloc();
910 	dev = priv->dev = if_alloc(IFT_INFINIBAND);
911 	if (!dev) {
912 		free(priv, M_TEMP);
913 		return NULL;
914 	}
915 	if_setsoftc(dev, priv);
916 	priv->gone = 2; /* initializing */
917 	priv->unit = alloc_unr(ipoib_unrhdr);
918 	if (priv->unit == -1) {
919 		if_free(dev);
920 		free(priv, M_TEMP);
921 		return NULL;
922 	}
923 	if_initname(dev, name, priv->unit);
924 	if_setflags(dev, IFF_BROADCAST | IFF_MULTICAST);
925 
926 	infiniband_ifattach(priv->dev, NULL, priv->broadcastaddr);
927 
928 	if_setinitfn(dev, ipoib_init);
929 	if_setioctlfn(dev, ipoib_ioctl);
930 	if_setstartfn(dev, ipoib_start);
931 
932 	if_setsendqlen(dev, ipoib_sendq_size * 2);
933 
934 	priv->dev = dev;
935 	if_link_state_change(priv->dev, LINK_STATE_DOWN);
936 
937 	return if_getsoftc(dev);
938 }
939 
940 int
941 ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
942 {
943 	struct ib_device_attr *device_attr = &hca->attrs;
944 
945 	priv->hca_caps = device_attr->device_cap_flags;
946 
947 	if_sethwassist(priv->dev, 0);
948 	if_setcapabilities(priv->dev, 0);
949 
950 #ifndef CONFIG_INFINIBAND_IPOIB_CM
951 	if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
952 		set_bit(IPOIB_FLAG_CSUM, &priv->flags);
953 		if_sethwassist(priv->dev, CSUM_IP | CSUM_TCP | CSUM_UDP);
954 		if_setcapabilities(priv->dev, IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM);
955 	}
956 
957 #if 0
958 	if (priv->dev->features & NETIF_F_SG && priv->hca_caps & IB_DEVICE_UD_TSO) {
959 		priv->dev->if_capabilities |= IFCAP_TSO4;
960 		priv->dev->if_hwassist |= CSUM_TSO;
961 	}
962 #endif
963 #endif
964 	if_setcapabilitiesbit(priv->dev,
965 	    IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_LINKSTATE, 0);
966 	if_setcapenable(priv->dev, if_getcapabilities(priv->dev));
967 
968 	return 0;
969 }
970 
971 
972 static if_t
973 ipoib_add_port(const char *format, struct ib_device *hca, u8 port)
974 {
975 	struct ipoib_dev_priv *priv;
976 	struct ib_port_attr attr;
977 	int result = -ENOMEM;
978 
979 	priv = ipoib_intf_alloc(format);
980 	if (!priv)
981 		goto alloc_mem_failed;
982 
983 	if (!ib_query_port(hca, port, &attr))
984 		priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
985 	else {
986 		printk(KERN_WARNING "%s: ib_query_port %d failed\n",
987 		       hca->name, port);
988 		goto device_init_failed;
989 	}
990 
991 	/* MTU will be reset when mcast join happens */
992 	if_setmtu(priv->dev, IPOIB_UD_MTU(priv->max_ib_mtu));
993 	priv->mcast_mtu = priv->admin_mtu = if_getmtu(priv->dev);
994 
995 	result = ib_query_pkey(hca, port, 0, &priv->pkey);
996 	if (result) {
997 		printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
998 		       hca->name, port, result);
999 		goto device_init_failed;
1000 	}
1001 
1002 	if (ipoib_set_dev_features(priv, hca))
1003 		goto device_init_failed;
1004 
1005 	/*
1006 	 * Set the full membership bit, so that we join the right
1007 	 * broadcast group, etc.
1008 	 */
1009 	priv->pkey |= 0x8000;
1010 
1011 	priv->broadcastaddr[8] = priv->pkey >> 8;
1012 	priv->broadcastaddr[9] = priv->pkey & 0xff;
1013 
1014 	result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
1015 	if (result) {
1016 		printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1017 		       hca->name, port, result);
1018 		goto device_init_failed;
1019 	}
1020 	memcpy(if_getlladdr(priv->dev) + 4, priv->local_gid.raw, sizeof(union ib_gid));
1021 
1022 	result = ipoib_dev_init(priv, hca, port);
1023 	if (result < 0) {
1024 		printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1025 		       hca->name, port, result);
1026 		goto device_init_failed;
1027 	}
1028 	if (ipoib_cm_admin_enabled(priv))
1029 		if_setmtu(priv->dev, IPOIB_CM_MTU(ipoib_cm_max_mtu(priv)));
1030 
1031 	INIT_IB_EVENT_HANDLER(&priv->event_handler,
1032 			      priv->ca, ipoib_event);
1033 	result = ib_register_event_handler(&priv->event_handler);
1034 	if (result < 0) {
1035 		printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1036 		       "port %d (ret = %d)\n",
1037 		       hca->name, port, result);
1038 		goto event_failed;
1039 	}
1040 	if_printf(priv->dev, "Attached to %s port %d\n", hca->name, port);
1041 
1042 	priv->gone = 0;	/* ready */
1043 
1044 	return priv->dev;
1045 
1046 event_failed:
1047 	ipoib_dev_cleanup(priv);
1048 
1049 device_init_failed:
1050 	ipoib_ifdetach(priv);
1051 	ipoib_detach(priv);
1052 
1053 alloc_mem_failed:
1054 	return ERR_PTR(result);
1055 }
1056 
1057 static void
1058 ipoib_add_one(struct ib_device *device)
1059 {
1060 	struct list_head *dev_list;
1061 	if_t dev;
1062 	struct ipoib_dev_priv *priv;
1063 	int s, e, p;
1064 
1065 	if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1066 		return;
1067 
1068 	dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1069 	if (!dev_list)
1070 		return;
1071 
1072 	INIT_LIST_HEAD(dev_list);
1073 
1074 	if (device->node_type == RDMA_NODE_IB_SWITCH) {
1075 		s = 0;
1076 		e = 0;
1077 	} else {
1078 		s = 1;
1079 		e = device->phys_port_cnt;
1080 	}
1081 
1082 	for (p = s; p <= e; ++p) {
1083 		if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
1084 			continue;
1085 		dev = ipoib_add_port("ib", device, p);
1086 		if (!IS_ERR(dev)) {
1087 			priv = if_getsoftc(dev);
1088 			list_add_tail(&priv->list, dev_list);
1089 		}
1090 	}
1091 
1092 	ib_set_client_data(device, &ipoib_client, dev_list);
1093 }
1094 
1095 static void
1096 ipoib_remove_one(struct ib_device *device, void *client_data)
1097 {
1098 	struct ipoib_dev_priv *priv, *tmp;
1099 	struct list_head *dev_list = client_data;
1100 
1101 	if (!dev_list)
1102 		return;
1103 
1104 	if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1105 		return;
1106 
1107 	list_for_each_entry_safe(priv, tmp, dev_list, list) {
1108 		if (rdma_port_get_link_layer(device, priv->port) != IB_LINK_LAYER_INFINIBAND)
1109 			continue;
1110 
1111 		ipoib_ifdetach(priv);
1112 		ipoib_stop(priv);
1113 
1114 		ib_unregister_event_handler(&priv->event_handler);
1115 
1116 		flush_workqueue(ipoib_workqueue);
1117 
1118 		ipoib_dev_cleanup(priv);
1119 		ipoib_detach(priv);
1120 	}
1121 
1122 	kfree(dev_list);
1123 }
1124 
1125 static u_int
1126 ipoib_match_dev_addr_cb(void *arg, struct ifaddr *ifa, u_int count)
1127 {
1128 	struct sockaddr *addr = arg;
1129 
1130 	/* If a match is already found, skip this. */
1131 	if (count > 0)
1132 		return (0);
1133 
1134 	if (ifa->ifa_addr->sa_len != addr->sa_len)
1135 		return (0);
1136 
1137 	if (memcmp(ifa->ifa_addr, addr, addr->sa_len) == 0)
1138 		return (1);
1139 
1140 	return (0);
1141 }
1142 
1143 static int
1144 ipoib_match_dev_addr(const struct sockaddr *addr, if_t dev)
1145 {
1146 	struct epoch_tracker et;
1147 	int retval = 0;
1148 
1149 	NET_EPOCH_ENTER(et);
1150 	retval = if_foreach_addr_type(dev, addr->sa_family,
1151 	    ipoib_match_dev_addr_cb, __DECONST(void *, addr));
1152 	NET_EPOCH_EXIT(et);
1153 
1154 	return (retval);
1155 }
1156 
1157 /*
1158  * ipoib_match_gid_pkey_addr - returns the number of IPoIB netdevs on
1159  * top a given ipoib device matching a pkey_index and address, if one
1160  * exists.
1161  *
1162  * @found_net_dev: contains a matching net_device if the return value
1163  * >= 1, with a reference held.
1164  */
1165 static int
1166 ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
1167     const union ib_gid *gid, u16 pkey_index, const struct sockaddr *addr,
1168     if_t *found_net_dev)
1169 {
1170 	struct ipoib_dev_priv *child_priv;
1171 	int matches = 0;
1172 
1173 	if (priv->pkey_index == pkey_index &&
1174 	    (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
1175 		if (addr == NULL || ipoib_match_dev_addr(addr, priv->dev) != 0) {
1176 			if (*found_net_dev == NULL) {
1177 				if_t net_dev;
1178 
1179 				if (priv->parent != NULL)
1180 					net_dev = priv->parent;
1181 				else
1182 					net_dev = priv->dev;
1183 				*found_net_dev = net_dev;
1184 				dev_hold(net_dev);
1185 			}
1186 			matches++;
1187 		}
1188 	}
1189 
1190 	/* Check child interfaces */
1191 	mutex_lock(&priv->vlan_mutex);
1192 	list_for_each_entry(child_priv, &priv->child_intfs, list) {
1193 		matches += ipoib_match_gid_pkey_addr(child_priv, gid,
1194 		    pkey_index, addr, found_net_dev);
1195 		if (matches > 1)
1196 			break;
1197 	}
1198 	mutex_unlock(&priv->vlan_mutex);
1199 
1200 	return matches;
1201 }
1202 
1203 /*
1204  * __ipoib_get_net_dev_by_params - returns the number of matching
1205  * net_devs found (between 0 and 2). Also return the matching
1206  * net_device in the @net_dev parameter, holding a reference to the
1207  * net_device, if the number of matches >= 1
1208  */
1209 static int
1210 __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port,
1211     u16 pkey_index, const union ib_gid *gid,
1212     const struct sockaddr *addr, if_t *net_dev)
1213 {
1214 	struct ipoib_dev_priv *priv;
1215 	int matches = 0;
1216 
1217 	*net_dev = NULL;
1218 
1219 	list_for_each_entry(priv, dev_list, list) {
1220 		if (priv->port != port)
1221 			continue;
1222 
1223 		matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
1224 		    addr, net_dev);
1225 
1226 		if (matches > 1)
1227 			break;
1228 	}
1229 
1230 	return matches;
1231 }
1232 
1233 static if_t
1234 ipoib_get_net_dev_by_params(struct ib_device *dev, u8 port, u16 pkey,
1235     const union ib_gid *gid, const struct sockaddr *addr, void *client_data)
1236 {
1237 	if_t net_dev;
1238 	struct list_head *dev_list = client_data;
1239 	u16 pkey_index;
1240 	int matches;
1241 	int ret;
1242 
1243 	if (!rdma_protocol_ib(dev, port))
1244 		return NULL;
1245 
1246 	ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
1247 	if (ret)
1248 		return NULL;
1249 
1250 	if (!dev_list)
1251 		return NULL;
1252 
1253 	/* See if we can find a unique device matching the L2 parameters */
1254 	matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
1255 						gid, NULL, &net_dev);
1256 
1257 	switch (matches) {
1258 	case 0:
1259 		return NULL;
1260 	case 1:
1261 		return net_dev;
1262 	}
1263 
1264 	dev_put(net_dev);
1265 
1266 	/* Couldn't find a unique device with L2 parameters only. Use L3
1267 	 * address to uniquely match the net device */
1268 	matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
1269 						gid, addr, &net_dev);
1270 	switch (matches) {
1271 	case 0:
1272 		return NULL;
1273 	default:
1274 		dev_warn_ratelimited(&dev->dev,
1275 				     "duplicate IP address detected\n");
1276 		/* Fall through */
1277 	case 1:
1278 		return net_dev;
1279 	}
1280 }
1281 
1282 static void
1283 ipoib_config_vlan(void *arg, if_t ifp, uint16_t vtag)
1284 {
1285 	struct ipoib_dev_priv *parent;
1286 	struct ipoib_dev_priv *priv;
1287 	struct epoch_tracker et;
1288 	if_t dev;
1289 	uint16_t pkey;
1290 	int error;
1291 
1292 	if (if_gettype(ifp) != IFT_INFINIBAND)
1293 		return;
1294 	NET_EPOCH_ENTER(et);
1295 	dev = VLAN_DEVAT(ifp, vtag);
1296 	NET_EPOCH_EXIT(et);
1297 	if (dev == NULL)
1298 		return;
1299 	priv = NULL;
1300 	error = 0;
1301 	parent = if_getsoftc(ifp);
1302 	/* We only support 15 bits of pkey. */
1303 	if (vtag & 0x8000)
1304 		return;
1305 	pkey = vtag | 0x8000;	/* Set full membership bit. */
1306 	if (pkey == parent->pkey)
1307 		return;
1308 	/* Check for dups */
1309 	mutex_lock(&parent->vlan_mutex);
1310 	list_for_each_entry(priv, &parent->child_intfs, list) {
1311 		if (priv->pkey == pkey) {
1312 			priv = NULL;
1313 			error = EBUSY;
1314 			goto out;
1315 		}
1316 	}
1317 	priv = ipoib_priv_alloc();
1318 	priv->dev = dev;
1319 	priv->max_ib_mtu = parent->max_ib_mtu;
1320 	priv->mcast_mtu = priv->admin_mtu = if_getmtu(parent->dev);
1321 	set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
1322 	error = ipoib_set_dev_features(priv, parent->ca);
1323 	if (error)
1324 		goto out;
1325 	priv->pkey = pkey;
1326 	priv->broadcastaddr[8] = pkey >> 8;
1327 	priv->broadcastaddr[9] = pkey & 0xff;
1328 	if_setbroadcastaddr(dev, priv->broadcastaddr);
1329 	error = ipoib_dev_init(priv, parent->ca, parent->port);
1330 	if (error)
1331 		goto out;
1332 	priv->parent = parent->dev;
1333 	list_add_tail(&priv->list, &parent->child_intfs);
1334 	VLAN_SETCOOKIE(dev, priv);
1335 	if_setstartfn(dev, ipoib_vlan_start);
1336 	if_setdrvflagbits(dev, 0, IFF_DRV_RUNNING);
1337 	if_setifheaderlen(dev, IPOIB_HEADER_LEN);
1338 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
1339 		ipoib_open(priv);
1340 	mutex_unlock(&parent->vlan_mutex);
1341 	return;
1342 out:
1343 	mutex_unlock(&parent->vlan_mutex);
1344 	if (priv)
1345 		free(priv, M_TEMP);
1346 	if (error)
1347 		ipoib_warn(parent,
1348 		    "failed to initialize subinterface: device %s, port %d vtag 0x%X",
1349 		    parent->ca->name, parent->port, vtag);
1350 	return;
1351 }
1352 
1353 static void
1354 ipoib_unconfig_vlan(void *arg, if_t ifp, uint16_t vtag)
1355 {
1356 	struct ipoib_dev_priv *parent;
1357 	struct ipoib_dev_priv *priv;
1358 	struct epoch_tracker et;
1359 	if_t dev;
1360 	uint16_t pkey;
1361 
1362 	if (if_gettype(ifp) != IFT_INFINIBAND)
1363 		return;
1364 
1365 	NET_EPOCH_ENTER(et);
1366 	dev = VLAN_DEVAT(ifp, vtag);
1367 	NET_EPOCH_EXIT(et);
1368 	if (dev)
1369 		VLAN_SETCOOKIE(dev, NULL);
1370 	pkey = vtag | 0x8000;
1371 	parent = if_getsoftc(ifp);
1372 	mutex_lock(&parent->vlan_mutex);
1373 	list_for_each_entry(priv, &parent->child_intfs, list) {
1374 		if (priv->pkey == pkey) {
1375 			ipoib_dev_cleanup(priv);
1376 			list_del(&priv->list);
1377 			break;
1378 		}
1379 	}
1380 	mutex_unlock(&parent->vlan_mutex);
1381 }
1382 
1383 eventhandler_tag ipoib_vlan_attach;
1384 eventhandler_tag ipoib_vlan_detach;
1385 
1386 static int __init
1387 ipoib_init_module(void)
1388 {
1389 	int ret;
1390 
1391 	ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1392 	ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1393 	ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1394 
1395 	ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1396 	ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1397 	ipoib_sendq_size = max(ipoib_sendq_size, max(2 * MAX_SEND_CQE,
1398 						     IPOIB_MIN_QUEUE_SIZE));
1399 #ifdef CONFIG_INFINIBAND_IPOIB_CM
1400 	ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1401 #endif
1402 
1403 	ipoib_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
1404 		ipoib_config_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1405 	ipoib_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
1406 		ipoib_unconfig_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1407 
1408 	/*
1409 	 * We create our own workqueue mainly because we want to be
1410 	 * able to flush it when devices are being removed.  We can't
1411 	 * use schedule_work()/flush_scheduled_work() because both
1412 	 * unregister_netdev() and linkwatch_event take the rtnl lock,
1413 	 * so flush_scheduled_work() can deadlock during device
1414 	 * removal.
1415 	 */
1416 	ipoib_workqueue = create_singlethread_workqueue("ipoib");
1417 	if (!ipoib_workqueue) {
1418 		ret = -ENOMEM;
1419 		goto err_fs;
1420 	}
1421 
1422 	ib_sa_register_client(&ipoib_sa_client);
1423 
1424 	ret = ib_register_client(&ipoib_client);
1425 	if (ret)
1426 		goto err_sa;
1427 
1428 	return 0;
1429 
1430 err_sa:
1431 	ib_sa_unregister_client(&ipoib_sa_client);
1432 	destroy_workqueue(ipoib_workqueue);
1433 
1434 err_fs:
1435 	return ret;
1436 }
1437 
1438 static void __exit
1439 ipoib_cleanup_module(void)
1440 {
1441 
1442 	EVENTHANDLER_DEREGISTER(vlan_config, ipoib_vlan_attach);
1443 	EVENTHANDLER_DEREGISTER(vlan_unconfig, ipoib_vlan_detach);
1444 	ib_unregister_client(&ipoib_client);
1445 	ib_sa_unregister_client(&ipoib_sa_client);
1446 	destroy_workqueue(ipoib_workqueue);
1447 }
1448 module_init_order(ipoib_init_module, SI_ORDER_FIFTH);
1449 module_exit_order(ipoib_cleanup_module, SI_ORDER_FIFTH);
1450 
1451 static int
1452 ipoib_evhand(module_t mod, int event, void *arg)
1453 {
1454 	return (0);
1455 }
1456 
1457 static moduledata_t ipoib_mod = {
1458 	.name = "ipoib",
1459 	.evhand = ipoib_evhand,
1460 };
1461 
1462 DECLARE_MODULE(ipoib, ipoib_mod, SI_SUB_LAST, SI_ORDER_ANY);
1463 MODULE_DEPEND(ipoib, ibcore, 1, 1, 1);
1464 MODULE_DEPEND(ipoib, if_infiniband, 1, 1, 1);
1465 MODULE_DEPEND(ipoib, linuxkpi, 1, 1, 1);
1466