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