xref: /freebsd/sys/ofed/drivers/infiniband/ulp/ipoib/ipoib_main.c (revision 9f23cbd6cae82fd77edfad7173432fa8dccd0a95)
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, struct ib_device *hca)
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 	if ((hca->attrs.device_cap_flags & IB_DEVICE_KNOWSEPOCH) == 0)
926 		if_setflagbits(dev, IFF_NEEDSEPOCH, 0);
927 
928 	infiniband_ifattach(priv->dev, NULL, priv->broadcastaddr);
929 
930 	if_setinitfn(dev, ipoib_init);
931 	if_setioctlfn(dev, ipoib_ioctl);
932 	if_setstartfn(dev, ipoib_start);
933 
934 	if_setsendqlen(dev, ipoib_sendq_size * 2);
935 
936 	priv->dev = dev;
937 	if_link_state_change(priv->dev, LINK_STATE_DOWN);
938 
939 	return if_getsoftc(dev);
940 }
941 
942 int
943 ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
944 {
945 	struct ib_device_attr *device_attr = &hca->attrs;
946 
947 	priv->hca_caps = device_attr->device_cap_flags;
948 
949 	if_sethwassist(priv->dev, 0);
950 	if_setcapabilities(priv->dev, 0);
951 
952 #ifndef CONFIG_INFINIBAND_IPOIB_CM
953 	if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
954 		set_bit(IPOIB_FLAG_CSUM, &priv->flags);
955 		if_sethwassist(priv->dev, CSUM_IP | CSUM_TCP | CSUM_UDP);
956 		if_setcapabilities(priv->dev, IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM);
957 	}
958 
959 #if 0
960 	if (priv->dev->features & NETIF_F_SG && priv->hca_caps & IB_DEVICE_UD_TSO) {
961 		priv->dev->if_capabilities |= IFCAP_TSO4;
962 		priv->dev->if_hwassist |= CSUM_TSO;
963 	}
964 #endif
965 #endif
966 	if_setcapabilitiesbit(priv->dev,
967 	    IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_LINKSTATE, 0);
968 	if_setcapenable(priv->dev, if_getcapabilities(priv->dev));
969 
970 	return 0;
971 }
972 
973 
974 static if_t
975 ipoib_add_port(const char *format, struct ib_device *hca, u8 port)
976 {
977 	struct ipoib_dev_priv *priv;
978 	struct ib_port_attr attr;
979 	int result = -ENOMEM;
980 
981 	priv = ipoib_intf_alloc(format, hca);
982 	if (!priv)
983 		goto alloc_mem_failed;
984 
985 	if (!ib_query_port(hca, port, &attr))
986 		priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
987 	else {
988 		printk(KERN_WARNING "%s: ib_query_port %d failed\n",
989 		       hca->name, port);
990 		goto device_init_failed;
991 	}
992 
993 	/* MTU will be reset when mcast join happens */
994 	if_setmtu(priv->dev, IPOIB_UD_MTU(priv->max_ib_mtu));
995 	priv->mcast_mtu = priv->admin_mtu = if_getmtu(priv->dev);
996 
997 	result = ib_query_pkey(hca, port, 0, &priv->pkey);
998 	if (result) {
999 		printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1000 		       hca->name, port, result);
1001 		goto device_init_failed;
1002 	}
1003 
1004 	if (ipoib_set_dev_features(priv, hca))
1005 		goto device_init_failed;
1006 
1007 	/*
1008 	 * Set the full membership bit, so that we join the right
1009 	 * broadcast group, etc.
1010 	 */
1011 	priv->pkey |= 0x8000;
1012 
1013 	priv->broadcastaddr[8] = priv->pkey >> 8;
1014 	priv->broadcastaddr[9] = priv->pkey & 0xff;
1015 
1016 	result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
1017 	if (result) {
1018 		printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1019 		       hca->name, port, result);
1020 		goto device_init_failed;
1021 	}
1022 	memcpy(if_getlladdr(priv->dev) + 4, priv->local_gid.raw, sizeof(union ib_gid));
1023 
1024 	result = ipoib_dev_init(priv, hca, port);
1025 	if (result < 0) {
1026 		printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1027 		       hca->name, port, result);
1028 		goto device_init_failed;
1029 	}
1030 	if (ipoib_cm_admin_enabled(priv))
1031 		if_setmtu(priv->dev, IPOIB_CM_MTU(ipoib_cm_max_mtu(priv)));
1032 
1033 	INIT_IB_EVENT_HANDLER(&priv->event_handler,
1034 			      priv->ca, ipoib_event);
1035 	result = ib_register_event_handler(&priv->event_handler);
1036 	if (result < 0) {
1037 		printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1038 		       "port %d (ret = %d)\n",
1039 		       hca->name, port, result);
1040 		goto event_failed;
1041 	}
1042 	if_printf(priv->dev, "Attached to %s port %d\n", hca->name, port);
1043 
1044 	priv->gone = 0;	/* ready */
1045 
1046 	return priv->dev;
1047 
1048 event_failed:
1049 	ipoib_dev_cleanup(priv);
1050 
1051 device_init_failed:
1052 	ipoib_ifdetach(priv);
1053 	ipoib_detach(priv);
1054 
1055 alloc_mem_failed:
1056 	return ERR_PTR(result);
1057 }
1058 
1059 static void
1060 ipoib_add_one(struct ib_device *device)
1061 {
1062 	struct list_head *dev_list;
1063 	if_t dev;
1064 	struct ipoib_dev_priv *priv;
1065 	int s, e, p;
1066 
1067 	if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1068 		return;
1069 
1070 	dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1071 	if (!dev_list)
1072 		return;
1073 
1074 	INIT_LIST_HEAD(dev_list);
1075 
1076 	if (device->node_type == RDMA_NODE_IB_SWITCH) {
1077 		s = 0;
1078 		e = 0;
1079 	} else {
1080 		s = 1;
1081 		e = device->phys_port_cnt;
1082 	}
1083 
1084 	for (p = s; p <= e; ++p) {
1085 		if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
1086 			continue;
1087 		dev = ipoib_add_port("ib", device, p);
1088 		if (!IS_ERR(dev)) {
1089 			priv = if_getsoftc(dev);
1090 			list_add_tail(&priv->list, dev_list);
1091 		}
1092 	}
1093 
1094 	ib_set_client_data(device, &ipoib_client, dev_list);
1095 }
1096 
1097 static void
1098 ipoib_remove_one(struct ib_device *device, void *client_data)
1099 {
1100 	struct ipoib_dev_priv *priv, *tmp;
1101 	struct list_head *dev_list = client_data;
1102 
1103 	if (!dev_list)
1104 		return;
1105 
1106 	if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1107 		return;
1108 
1109 	list_for_each_entry_safe(priv, tmp, dev_list, list) {
1110 		if (rdma_port_get_link_layer(device, priv->port) != IB_LINK_LAYER_INFINIBAND)
1111 			continue;
1112 
1113 		ipoib_ifdetach(priv);
1114 		ipoib_stop(priv);
1115 
1116 		ib_unregister_event_handler(&priv->event_handler);
1117 
1118 		flush_workqueue(ipoib_workqueue);
1119 
1120 		ipoib_dev_cleanup(priv);
1121 		ipoib_detach(priv);
1122 	}
1123 
1124 	kfree(dev_list);
1125 }
1126 
1127 static u_int
1128 ipoib_match_dev_addr_cb(void *arg, struct ifaddr *ifa, u_int count)
1129 {
1130 	struct sockaddr *addr = arg;
1131 
1132 	/* If a match is already found, skip this. */
1133 	if (count > 0)
1134 		return (0);
1135 
1136 	if (ifa->ifa_addr->sa_len != addr->sa_len)
1137 		return (0);
1138 
1139 	if (memcmp(ifa->ifa_addr, addr, addr->sa_len) == 0)
1140 		return (1);
1141 
1142 	return (0);
1143 }
1144 
1145 static int
1146 ipoib_match_dev_addr(const struct sockaddr *addr, if_t dev)
1147 {
1148 	struct epoch_tracker et;
1149 	int retval = 0;
1150 
1151 	NET_EPOCH_ENTER(et);
1152 	retval = if_foreach_addr_type(dev, addr->sa_family,
1153 	    ipoib_match_dev_addr_cb, __DECONST(void *, addr));
1154 	NET_EPOCH_EXIT(et);
1155 
1156 	return (retval);
1157 }
1158 
1159 /*
1160  * ipoib_match_gid_pkey_addr - returns the number of IPoIB netdevs on
1161  * top a given ipoib device matching a pkey_index and address, if one
1162  * exists.
1163  *
1164  * @found_net_dev: contains a matching net_device if the return value
1165  * >= 1, with a reference held.
1166  */
1167 static int
1168 ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
1169     const union ib_gid *gid, u16 pkey_index, const struct sockaddr *addr,
1170     if_t *found_net_dev)
1171 {
1172 	struct ipoib_dev_priv *child_priv;
1173 	int matches = 0;
1174 
1175 	if (priv->pkey_index == pkey_index &&
1176 	    (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
1177 		if (addr == NULL || ipoib_match_dev_addr(addr, priv->dev) != 0) {
1178 			if (*found_net_dev == NULL) {
1179 				if_t net_dev;
1180 
1181 				if (priv->parent != NULL)
1182 					net_dev = priv->parent;
1183 				else
1184 					net_dev = priv->dev;
1185 				*found_net_dev = net_dev;
1186 				dev_hold(net_dev);
1187 			}
1188 			matches++;
1189 		}
1190 	}
1191 
1192 	/* Check child interfaces */
1193 	mutex_lock(&priv->vlan_mutex);
1194 	list_for_each_entry(child_priv, &priv->child_intfs, list) {
1195 		matches += ipoib_match_gid_pkey_addr(child_priv, gid,
1196 		    pkey_index, addr, found_net_dev);
1197 		if (matches > 1)
1198 			break;
1199 	}
1200 	mutex_unlock(&priv->vlan_mutex);
1201 
1202 	return matches;
1203 }
1204 
1205 /*
1206  * __ipoib_get_net_dev_by_params - returns the number of matching
1207  * net_devs found (between 0 and 2). Also return the matching
1208  * net_device in the @net_dev parameter, holding a reference to the
1209  * net_device, if the number of matches >= 1
1210  */
1211 static int
1212 __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port,
1213     u16 pkey_index, const union ib_gid *gid,
1214     const struct sockaddr *addr, if_t *net_dev)
1215 {
1216 	struct ipoib_dev_priv *priv;
1217 	int matches = 0;
1218 
1219 	*net_dev = NULL;
1220 
1221 	list_for_each_entry(priv, dev_list, list) {
1222 		if (priv->port != port)
1223 			continue;
1224 
1225 		matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
1226 		    addr, net_dev);
1227 
1228 		if (matches > 1)
1229 			break;
1230 	}
1231 
1232 	return matches;
1233 }
1234 
1235 static if_t
1236 ipoib_get_net_dev_by_params(struct ib_device *dev, u8 port, u16 pkey,
1237     const union ib_gid *gid, const struct sockaddr *addr, void *client_data)
1238 {
1239 	if_t net_dev;
1240 	struct list_head *dev_list = client_data;
1241 	u16 pkey_index;
1242 	int matches;
1243 	int ret;
1244 
1245 	if (!rdma_protocol_ib(dev, port))
1246 		return NULL;
1247 
1248 	ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
1249 	if (ret)
1250 		return NULL;
1251 
1252 	if (!dev_list)
1253 		return NULL;
1254 
1255 	/* See if we can find a unique device matching the L2 parameters */
1256 	matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
1257 						gid, NULL, &net_dev);
1258 
1259 	switch (matches) {
1260 	case 0:
1261 		return NULL;
1262 	case 1:
1263 		return net_dev;
1264 	}
1265 
1266 	dev_put(net_dev);
1267 
1268 	/* Couldn't find a unique device with L2 parameters only. Use L3
1269 	 * address to uniquely match the net device */
1270 	matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
1271 						gid, addr, &net_dev);
1272 	switch (matches) {
1273 	case 0:
1274 		return NULL;
1275 	default:
1276 		dev_warn_ratelimited(&dev->dev,
1277 				     "duplicate IP address detected\n");
1278 		/* Fall through */
1279 	case 1:
1280 		return net_dev;
1281 	}
1282 }
1283 
1284 static void
1285 ipoib_config_vlan(void *arg, if_t ifp, uint16_t vtag)
1286 {
1287 	struct ipoib_dev_priv *parent;
1288 	struct ipoib_dev_priv *priv;
1289 	struct epoch_tracker et;
1290 	if_t dev;
1291 	uint16_t pkey;
1292 	int error;
1293 
1294 	if (if_gettype(ifp) != IFT_INFINIBAND)
1295 		return;
1296 	NET_EPOCH_ENTER(et);
1297 	dev = VLAN_DEVAT(ifp, vtag);
1298 	NET_EPOCH_EXIT(et);
1299 	if (dev == NULL)
1300 		return;
1301 	priv = NULL;
1302 	error = 0;
1303 	parent = if_getsoftc(ifp);
1304 	/* We only support 15 bits of pkey. */
1305 	if (vtag & 0x8000)
1306 		return;
1307 	pkey = vtag | 0x8000;	/* Set full membership bit. */
1308 	if (pkey == parent->pkey)
1309 		return;
1310 	/* Check for dups */
1311 	mutex_lock(&parent->vlan_mutex);
1312 	list_for_each_entry(priv, &parent->child_intfs, list) {
1313 		if (priv->pkey == pkey) {
1314 			priv = NULL;
1315 			error = EBUSY;
1316 			goto out;
1317 		}
1318 	}
1319 	priv = ipoib_priv_alloc();
1320 	priv->dev = dev;
1321 	priv->max_ib_mtu = parent->max_ib_mtu;
1322 	priv->mcast_mtu = priv->admin_mtu = if_getmtu(parent->dev);
1323 	set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
1324 	error = ipoib_set_dev_features(priv, parent->ca);
1325 	if (error)
1326 		goto out;
1327 	priv->pkey = pkey;
1328 	priv->broadcastaddr[8] = pkey >> 8;
1329 	priv->broadcastaddr[9] = pkey & 0xff;
1330 	if_setbroadcastaddr(dev, priv->broadcastaddr);
1331 	error = ipoib_dev_init(priv, parent->ca, parent->port);
1332 	if (error)
1333 		goto out;
1334 	priv->parent = parent->dev;
1335 	list_add_tail(&priv->list, &parent->child_intfs);
1336 	VLAN_SETCOOKIE(dev, priv);
1337 	if_setstartfn(dev, ipoib_vlan_start);
1338 	if_setdrvflagbits(dev, 0, IFF_DRV_RUNNING);
1339 	if_setifheaderlen(dev, IPOIB_HEADER_LEN);
1340 	if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
1341 		ipoib_open(priv);
1342 	mutex_unlock(&parent->vlan_mutex);
1343 	return;
1344 out:
1345 	mutex_unlock(&parent->vlan_mutex);
1346 	if (priv)
1347 		free(priv, M_TEMP);
1348 	if (error)
1349 		ipoib_warn(parent,
1350 		    "failed to initialize subinterface: device %s, port %d vtag 0x%X",
1351 		    parent->ca->name, parent->port, vtag);
1352 	return;
1353 }
1354 
1355 static void
1356 ipoib_unconfig_vlan(void *arg, if_t ifp, uint16_t vtag)
1357 {
1358 	struct ipoib_dev_priv *parent;
1359 	struct ipoib_dev_priv *priv;
1360 	struct epoch_tracker et;
1361 	if_t dev;
1362 	uint16_t pkey;
1363 
1364 	if (if_gettype(ifp) != IFT_INFINIBAND)
1365 		return;
1366 
1367 	NET_EPOCH_ENTER(et);
1368 	dev = VLAN_DEVAT(ifp, vtag);
1369 	NET_EPOCH_EXIT(et);
1370 	if (dev)
1371 		VLAN_SETCOOKIE(dev, NULL);
1372 	pkey = vtag | 0x8000;
1373 	parent = if_getsoftc(ifp);
1374 	mutex_lock(&parent->vlan_mutex);
1375 	list_for_each_entry(priv, &parent->child_intfs, list) {
1376 		if (priv->pkey == pkey) {
1377 			ipoib_dev_cleanup(priv);
1378 			list_del(&priv->list);
1379 			break;
1380 		}
1381 	}
1382 	mutex_unlock(&parent->vlan_mutex);
1383 }
1384 
1385 eventhandler_tag ipoib_vlan_attach;
1386 eventhandler_tag ipoib_vlan_detach;
1387 
1388 static int __init
1389 ipoib_init_module(void)
1390 {
1391 	int ret;
1392 
1393 	ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1394 	ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1395 	ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1396 
1397 	ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1398 	ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1399 	ipoib_sendq_size = max(ipoib_sendq_size, max(2 * MAX_SEND_CQE,
1400 						     IPOIB_MIN_QUEUE_SIZE));
1401 #ifdef CONFIG_INFINIBAND_IPOIB_CM
1402 	ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1403 #endif
1404 
1405 	ipoib_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
1406 		ipoib_config_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1407 	ipoib_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
1408 		ipoib_unconfig_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1409 
1410 	/*
1411 	 * We create our own workqueue mainly because we want to be
1412 	 * able to flush it when devices are being removed.  We can't
1413 	 * use schedule_work()/flush_scheduled_work() because both
1414 	 * unregister_netdev() and linkwatch_event take the rtnl lock,
1415 	 * so flush_scheduled_work() can deadlock during device
1416 	 * removal.
1417 	 */
1418 	ipoib_workqueue = create_singlethread_workqueue("ipoib");
1419 	if (!ipoib_workqueue) {
1420 		ret = -ENOMEM;
1421 		goto err_fs;
1422 	}
1423 
1424 	ib_sa_register_client(&ipoib_sa_client);
1425 
1426 	ret = ib_register_client(&ipoib_client);
1427 	if (ret)
1428 		goto err_sa;
1429 
1430 	return 0;
1431 
1432 err_sa:
1433 	ib_sa_unregister_client(&ipoib_sa_client);
1434 	destroy_workqueue(ipoib_workqueue);
1435 
1436 err_fs:
1437 	return ret;
1438 }
1439 
1440 static void __exit
1441 ipoib_cleanup_module(void)
1442 {
1443 
1444 	EVENTHANDLER_DEREGISTER(vlan_config, ipoib_vlan_attach);
1445 	EVENTHANDLER_DEREGISTER(vlan_unconfig, ipoib_vlan_detach);
1446 	ib_unregister_client(&ipoib_client);
1447 	ib_sa_unregister_client(&ipoib_sa_client);
1448 	destroy_workqueue(ipoib_workqueue);
1449 }
1450 module_init_order(ipoib_init_module, SI_ORDER_FIFTH);
1451 module_exit_order(ipoib_cleanup_module, SI_ORDER_FIFTH);
1452 
1453 static int
1454 ipoib_evhand(module_t mod, int event, void *arg)
1455 {
1456 	return (0);
1457 }
1458 
1459 static moduledata_t ipoib_mod = {
1460 	.name = "ipoib",
1461 	.evhand = ipoib_evhand,
1462 };
1463 
1464 DECLARE_MODULE(ipoib, ipoib_mod, SI_SUB_LAST, SI_ORDER_ANY);
1465 MODULE_DEPEND(ipoib, ibcore, 1, 1, 1);
1466 MODULE_DEPEND(ipoib, if_infiniband, 1, 1, 1);
1467 MODULE_DEPEND(ipoib, linuxkpi, 1, 1, 1);
1468