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