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 "ipoib.h"
38 
39 static	int ipoib_resolvemulti(struct ifnet *, struct sockaddr **,
40 		struct sockaddr *);
41 
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_arp.h>	/* For ARPHRD_xxx */
50 #include <linux/if_vlan.h>
51 #include <net/ip.h>
52 #include <net/ipv6.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 void ipoib_start(struct ifnet *dev);
91 static int ipoib_output(struct ifnet *ifp, struct mbuf *m,
92 	    const struct sockaddr *dst, struct route *ro);
93 static int ipoib_ioctl(struct ifnet *ifp, u_long command, caddr_t data);
94 static void ipoib_input(struct ifnet *ifp, struct mbuf *m);
95 
96 #define	IPOIB_MTAP(_ifp, _m)					\
97 do {								\
98 	if (bpf_peers_present((_ifp)->if_bpf)) {		\
99 		M_ASSERTVALID(_m);				\
100 		ipoib_mtap_mb((_ifp), (_m));			\
101 	}							\
102 } while (0)
103 
104 static struct unrhdr *ipoib_unrhdr;
105 
106 static void
107 ipoib_unrhdr_init(void *arg)
108 {
109 
110 	ipoib_unrhdr = new_unrhdr(0, 65535, NULL);
111 }
112 SYSINIT(ipoib_unrhdr_init, SI_SUB_KLD - 1, SI_ORDER_ANY, ipoib_unrhdr_init, NULL);
113 
114 static void
115 ipoib_unrhdr_uninit(void *arg)
116 {
117 
118 	if (ipoib_unrhdr != NULL) {
119 		struct unrhdr *hdr;
120 
121 		hdr = ipoib_unrhdr;
122 		ipoib_unrhdr = NULL;
123 
124 		delete_unrhdr(hdr);
125 	}
126 }
127 SYSUNINIT(ipoib_unrhdr_uninit, SI_SUB_KLD - 1, SI_ORDER_ANY, ipoib_unrhdr_uninit, NULL);
128 
129 /*
130  * This is for clients that have an ipoib_header in the mbuf.
131  */
132 static void
133 ipoib_mtap_mb(struct ifnet *ifp, struct mbuf *mb)
134 {
135 	struct ipoib_header *ih;
136 	struct ether_header eh;
137 
138 	ih = mtod(mb, struct ipoib_header *);
139 	eh.ether_type = ih->proto;
140 	bcopy(ih->hwaddr, &eh.ether_dhost, ETHER_ADDR_LEN);
141 	bzero(&eh.ether_shost, ETHER_ADDR_LEN);
142 	mb->m_data += sizeof(struct ipoib_header);
143 	mb->m_len -= sizeof(struct ipoib_header);
144 	bpf_mtap2(ifp->if_bpf, &eh, sizeof(eh), mb);
145 	mb->m_data -= sizeof(struct ipoib_header);
146 	mb->m_len += sizeof(struct ipoib_header);
147 }
148 
149 void
150 ipoib_mtap_proto(struct ifnet *ifp, struct mbuf *mb, uint16_t proto)
151 {
152 	struct ether_header eh;
153 
154 	eh.ether_type = proto;
155 	bzero(&eh.ether_shost, ETHER_ADDR_LEN);
156 	bzero(&eh.ether_dhost, ETHER_ADDR_LEN);
157 	bpf_mtap2(ifp->if_bpf, &eh, sizeof(eh), mb);
158 }
159 
160 static struct ib_client ipoib_client = {
161 	.name   = "ipoib",
162 	.add    = ipoib_add_one,
163 	.remove = ipoib_remove_one
164 };
165 
166 int
167 ipoib_open(struct ipoib_dev_priv *priv)
168 {
169 	struct ifnet *dev = priv->dev;
170 
171 	ipoib_dbg(priv, "bringing up interface\n");
172 
173 	set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
174 
175 	if (ipoib_pkey_dev_delay_open(priv))
176 		return 0;
177 
178 	if (ipoib_ib_dev_open(priv))
179 		goto err_disable;
180 
181 	if (ipoib_ib_dev_up(priv))
182 		goto err_stop;
183 
184 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
185 		struct ipoib_dev_priv *cpriv;
186 
187 		/* Bring up any child interfaces too */
188 		mutex_lock(&priv->vlan_mutex);
189 		list_for_each_entry(cpriv, &priv->child_intfs, list)
190 			if ((cpriv->dev->if_drv_flags & IFF_DRV_RUNNING) == 0)
191 				ipoib_open(cpriv);
192 		mutex_unlock(&priv->vlan_mutex);
193 	}
194 	dev->if_drv_flags |= IFF_DRV_RUNNING;
195 	dev->if_drv_flags &= ~IFF_DRV_OACTIVE;
196 
197 	return 0;
198 
199 err_stop:
200 	ipoib_ib_dev_stop(priv, 1);
201 
202 err_disable:
203 	clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
204 
205 	return -EINVAL;
206 }
207 
208 static void
209 ipoib_init(void *arg)
210 {
211 	struct ifnet *dev;
212 	struct ipoib_dev_priv *priv;
213 
214 	priv = arg;
215 	dev = priv->dev;
216 	if ((dev->if_drv_flags & IFF_DRV_RUNNING) == 0)
217 		ipoib_open(priv);
218 	queue_work(ipoib_workqueue, &priv->flush_light);
219 }
220 
221 
222 static int
223 ipoib_stop(struct ipoib_dev_priv *priv)
224 {
225 	struct ifnet *dev = priv->dev;
226 
227 	ipoib_dbg(priv, "stopping interface\n");
228 
229 	clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
230 
231 	dev->if_drv_flags &= ~(IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
232 
233 	ipoib_ib_dev_down(priv, 0);
234 	ipoib_ib_dev_stop(priv, 0);
235 
236 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
237 		struct ipoib_dev_priv *cpriv;
238 
239 		/* Bring down any child interfaces too */
240 		mutex_lock(&priv->vlan_mutex);
241 		list_for_each_entry(cpriv, &priv->child_intfs, list)
242 			if ((cpriv->dev->if_drv_flags & IFF_DRV_RUNNING) != 0)
243 				ipoib_stop(cpriv);
244 		mutex_unlock(&priv->vlan_mutex);
245 	}
246 
247 	return 0;
248 }
249 
250 int
251 ipoib_change_mtu(struct ipoib_dev_priv *priv, int new_mtu)
252 {
253 	struct ifnet *dev = priv->dev;
254 
255 	/* dev->if_mtu > 2K ==> connected mode */
256 	if (ipoib_cm_admin_enabled(priv)) {
257 		if (new_mtu > IPOIB_CM_MTU(ipoib_cm_max_mtu(priv)))
258 			return -EINVAL;
259 
260 		if (new_mtu > priv->mcast_mtu)
261 			ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
262 				   priv->mcast_mtu);
263 
264 		dev->if_mtu = new_mtu;
265 		return 0;
266 	}
267 
268 	if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
269 		return -EINVAL;
270 
271 	priv->admin_mtu = new_mtu;
272 
273 	dev->if_mtu = min(priv->mcast_mtu, priv->admin_mtu);
274 
275 	queue_work(ipoib_workqueue, &priv->flush_light);
276 
277 	return 0;
278 }
279 
280 static int
281 ipoib_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
282 {
283 	struct ipoib_dev_priv *priv = ifp->if_softc;
284 	struct ifaddr *ifa = (struct ifaddr *) data;
285 	struct ifreq *ifr = (struct ifreq *) data;
286 	int error = 0;
287 
288 	/* check if detaching */
289 	if (priv == NULL || priv->gone != 0)
290 		return (ENXIO);
291 
292 	switch (command) {
293 	case SIOCSIFFLAGS:
294 		if (ifp->if_flags & IFF_UP) {
295 			if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0)
296 				error = -ipoib_open(priv);
297 		} else
298 			if (ifp->if_drv_flags & IFF_DRV_RUNNING)
299 				ipoib_stop(priv);
300 		break;
301 	case SIOCADDMULTI:
302 	case SIOCDELMULTI:
303 		if (ifp->if_drv_flags & IFF_DRV_RUNNING)
304 			queue_work(ipoib_workqueue, &priv->restart_task);
305 		break;
306 	case SIOCSIFADDR:
307 		ifp->if_flags |= IFF_UP;
308 
309 		switch (ifa->ifa_addr->sa_family) {
310 #ifdef INET
311 		case AF_INET:
312 			ifp->if_init(ifp->if_softc);	/* before arpwhohas */
313 			arp_ifinit(ifp, ifa);
314 			break;
315 #endif
316 		default:
317 			ifp->if_init(ifp->if_softc);
318 			break;
319 		}
320 		break;
321 
322 	case SIOCGIFADDR:
323 		bcopy(IF_LLADDR(ifp), &ifr->ifr_addr.sa_data[0],
324 		    INFINIBAND_ALEN);
325 		break;
326 
327 	case SIOCSIFMTU:
328 		/*
329 		 * Set the interface MTU.
330 		 */
331 		error = -ipoib_change_mtu(priv, ifr->ifr_mtu);
332 		break;
333 	default:
334 		error = EINVAL;
335 		break;
336 	}
337 	return (error);
338 }
339 
340 
341 static struct ipoib_path *
342 __path_find(struct ipoib_dev_priv *priv, void *gid)
343 {
344 	struct rb_node *n = priv->path_tree.rb_node;
345 	struct ipoib_path *path;
346 	int ret;
347 
348 	while (n) {
349 		path = rb_entry(n, struct ipoib_path, rb_node);
350 
351 		ret = memcmp(gid, path->pathrec.dgid.raw,
352 			     sizeof (union ib_gid));
353 
354 		if (ret < 0)
355 			n = n->rb_left;
356 		else if (ret > 0)
357 			n = n->rb_right;
358 		else
359 			return path;
360 	}
361 
362 	return NULL;
363 }
364 
365 static int
366 __path_add(struct ipoib_dev_priv *priv, struct ipoib_path *path)
367 {
368 	struct rb_node **n = &priv->path_tree.rb_node;
369 	struct rb_node *pn = NULL;
370 	struct ipoib_path *tpath;
371 	int ret;
372 
373 	while (*n) {
374 		pn = *n;
375 		tpath = rb_entry(pn, struct ipoib_path, rb_node);
376 
377 		ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
378 			     sizeof (union ib_gid));
379 		if (ret < 0)
380 			n = &pn->rb_left;
381 		else if (ret > 0)
382 			n = &pn->rb_right;
383 		else
384 			return -EEXIST;
385 	}
386 
387 	rb_link_node(&path->rb_node, pn, n);
388 	rb_insert_color(&path->rb_node, &priv->path_tree);
389 
390 	list_add_tail(&path->list, &priv->path_list);
391 
392 	return 0;
393 }
394 
395 void
396 ipoib_path_free(struct ipoib_dev_priv *priv, struct ipoib_path *path)
397 {
398 
399 	_IF_DRAIN(&path->queue);
400 
401 	if (path->ah)
402 		ipoib_put_ah(path->ah);
403 	if (ipoib_cm_get(path))
404 		ipoib_cm_destroy_tx(ipoib_cm_get(path));
405 
406 	kfree(path);
407 }
408 
409 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
410 
411 struct ipoib_path_iter *
412 ipoib_path_iter_init(struct ipoib_dev_priv *priv)
413 {
414 	struct ipoib_path_iter *iter;
415 
416 	iter = kmalloc(sizeof *iter, GFP_KERNEL);
417 	if (!iter)
418 		return NULL;
419 
420 	iter->priv = priv;
421 	memset(iter->path.pathrec.dgid.raw, 0, 16);
422 
423 	if (ipoib_path_iter_next(iter)) {
424 		kfree(iter);
425 		return NULL;
426 	}
427 
428 	return iter;
429 }
430 
431 int
432 ipoib_path_iter_next(struct ipoib_path_iter *iter)
433 {
434 	struct ipoib_dev_priv *priv = iter->priv;
435 	struct rb_node *n;
436 	struct ipoib_path *path;
437 	int ret = 1;
438 
439 	spin_lock_irq(&priv->lock);
440 
441 	n = rb_first(&priv->path_tree);
442 
443 	while (n) {
444 		path = rb_entry(n, struct ipoib_path, rb_node);
445 
446 		if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
447 			   sizeof (union ib_gid)) < 0) {
448 			iter->path = *path;
449 			ret = 0;
450 			break;
451 		}
452 
453 		n = rb_next(n);
454 	}
455 
456 	spin_unlock_irq(&priv->lock);
457 
458 	return ret;
459 }
460 
461 void
462 ipoib_path_iter_read(struct ipoib_path_iter *iter, struct ipoib_path *path)
463 {
464 	*path = iter->path;
465 }
466 
467 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
468 
469 void
470 ipoib_mark_paths_invalid(struct ipoib_dev_priv *priv)
471 {
472 	struct ipoib_path *path, *tp;
473 
474 	spin_lock_irq(&priv->lock);
475 
476 	list_for_each_entry_safe(path, tp, &priv->path_list, list) {
477 		ipoib_dbg(priv, "mark path LID 0x%04x GID %16D invalid\n",
478 			be16_to_cpu(path->pathrec.dlid),
479 			path->pathrec.dgid.raw, ":");
480 		path->valid =  0;
481 	}
482 
483 	spin_unlock_irq(&priv->lock);
484 }
485 
486 void
487 ipoib_flush_paths(struct ipoib_dev_priv *priv)
488 {
489 	struct ipoib_path *path, *tp;
490 	LIST_HEAD(remove_list);
491 	unsigned long flags;
492 
493 	spin_lock_irqsave(&priv->lock, flags);
494 
495 	list_splice_init(&priv->path_list, &remove_list);
496 
497 	list_for_each_entry(path, &remove_list, list)
498 		rb_erase(&path->rb_node, &priv->path_tree);
499 
500 	list_for_each_entry_safe(path, tp, &remove_list, list) {
501 		if (path->query)
502 			ib_sa_cancel_query(path->query_id, path->query);
503 		spin_unlock_irqrestore(&priv->lock, flags);
504 		wait_for_completion(&path->done);
505 		ipoib_path_free(priv, path);
506 		spin_lock_irqsave(&priv->lock, flags);
507 	}
508 
509 	spin_unlock_irqrestore(&priv->lock, flags);
510 }
511 
512 static void
513 path_rec_completion(int status, struct ib_sa_path_rec *pathrec, void *path_ptr)
514 {
515 	struct ipoib_path *path = path_ptr;
516 	struct ipoib_dev_priv *priv = path->priv;
517 	struct ifnet *dev = priv->dev;
518 	struct ipoib_ah *ah = NULL;
519 	struct ipoib_ah *old_ah = NULL;
520 	struct ifqueue mbqueue;
521 	struct mbuf *mb;
522 	unsigned long flags;
523 
524 	if (!status)
525 		ipoib_dbg(priv, "PathRec LID 0x%04x for GID %16D\n",
526 			  be16_to_cpu(pathrec->dlid), pathrec->dgid.raw, ":");
527 	else
528 		ipoib_dbg(priv, "PathRec status %d for GID %16D\n",
529 			  status, path->pathrec.dgid.raw, ":");
530 
531 	bzero(&mbqueue, sizeof(mbqueue));
532 
533 	if (!status) {
534 		struct ib_ah_attr av;
535 
536 		if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
537 			ah = ipoib_create_ah(priv, priv->pd, &av);
538 	}
539 
540 	spin_lock_irqsave(&priv->lock, flags);
541 
542 	if (ah) {
543 		path->pathrec = *pathrec;
544 
545 		old_ah   = path->ah;
546 		path->ah = ah;
547 
548 		ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
549 			  ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
550 
551 		for (;;) {
552 			_IF_DEQUEUE(&path->queue, mb);
553 			if (mb == NULL)
554 				break;
555 			_IF_ENQUEUE(&mbqueue, mb);
556 		}
557 
558 #ifdef CONFIG_INFINIBAND_IPOIB_CM
559 		if (ipoib_cm_enabled(priv, path->hwaddr) && !ipoib_cm_get(path))
560 			ipoib_cm_set(path, ipoib_cm_create_tx(priv, path));
561 #endif
562 
563 		path->valid = 1;
564 	}
565 
566 	path->query = NULL;
567 	complete(&path->done);
568 
569 	spin_unlock_irqrestore(&priv->lock, flags);
570 
571 	if (old_ah)
572 		ipoib_put_ah(old_ah);
573 
574 	for (;;) {
575 		_IF_DEQUEUE(&mbqueue, mb);
576 		if (mb == NULL)
577 			break;
578 		mb->m_pkthdr.rcvif = dev;
579 		if (dev->if_transmit(dev, mb))
580 			ipoib_warn(priv, "dev_queue_xmit failed "
581 				   "to requeue packet\n");
582 	}
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 	struct ifnet *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(dev->if_mtu + 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 				spin_unlock_irqrestore(&priv->lock, flags);
694 				if (new_path)
695 					ipoib_path_free(priv, path);
696 				return;
697 			} else
698 				__path_add(priv, path);
699 		} else {
700 			if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
701 			m_freem(mb);
702 		}
703 
704 		return;
705 	}
706 
707 	if (ipoib_cm_get(path) && ipoib_cm_up(path)) {
708 		ipoib_cm_send(priv, mb, ipoib_cm_get(path));
709 	} else if (path->ah) {
710 		ipoib_send(priv, mb, path->ah, IPOIB_QPN(eh->hwaddr));
711 	} else if ((path->query || !path_rec_start(priv, path)) &&
712 		    path->queue.ifq_len < IPOIB_MAX_PATH_REC_QUEUE) {
713 		_IF_ENQUEUE(&path->queue, mb);
714 	} else {
715 		if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
716 		m_freem(mb);
717 	}
718 }
719 
720 static int
721 ipoib_send_one(struct ipoib_dev_priv *priv, struct mbuf *mb)
722 {
723 	struct ipoib_header *eh;
724 
725 	eh = mtod(mb, struct ipoib_header *);
726 	if (IPOIB_IS_MULTICAST(eh->hwaddr)) {
727 		/* Add in the P_Key for multicast*/
728 		eh->hwaddr[8] = (priv->pkey >> 8) & 0xff;
729 		eh->hwaddr[9] = priv->pkey & 0xff;
730 
731 		ipoib_mcast_send(priv, eh->hwaddr + 4, mb);
732 	} else
733 		ipoib_unicast_send(mb, priv, eh);
734 
735 	return 0;
736 }
737 
738 
739 static void
740 _ipoib_start(struct ifnet *dev, struct ipoib_dev_priv *priv)
741 {
742 	struct mbuf *mb;
743 
744 	if ((dev->if_drv_flags & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
745 	    IFF_DRV_RUNNING)
746 		return;
747 
748 	spin_lock(&priv->lock);
749 	while (!IFQ_DRV_IS_EMPTY(&dev->if_snd) &&
750 	    (dev->if_drv_flags & IFF_DRV_OACTIVE) == 0) {
751 		IFQ_DRV_DEQUEUE(&dev->if_snd, mb);
752 		if (mb == NULL)
753 			break;
754 		IPOIB_MTAP(dev, mb);
755 		ipoib_send_one(priv, mb);
756 	}
757 	spin_unlock(&priv->lock);
758 }
759 
760 static void
761 ipoib_start(struct ifnet *dev)
762 {
763 	_ipoib_start(dev, dev->if_softc);
764 }
765 
766 static void
767 ipoib_vlan_start(struct ifnet *dev)
768 {
769 	struct ipoib_dev_priv *priv;
770 	struct mbuf *mb;
771 
772 	priv = VLAN_COOKIE(dev);
773 	if (priv != NULL)
774 		return _ipoib_start(dev, priv);
775 	while (!IFQ_DRV_IS_EMPTY(&dev->if_snd)) {
776 		IFQ_DRV_DEQUEUE(&dev->if_snd, mb);
777 		if (mb == NULL)
778 			break;
779 		m_freem(mb);
780 		if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
781 	}
782 }
783 
784 int
785 ipoib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca, int port)
786 {
787 
788 	/* Allocate RX/TX "rings" to hold queued mbs */
789 	priv->rx_ring =	kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
790 				GFP_KERNEL);
791 	if (!priv->rx_ring) {
792 		printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
793 		       ca->name, ipoib_recvq_size);
794 		goto out;
795 	}
796 
797 	priv->tx_ring = kzalloc(ipoib_sendq_size * sizeof *priv->tx_ring, GFP_KERNEL);
798 	if (!priv->tx_ring) {
799 		printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
800 		       ca->name, ipoib_sendq_size);
801 		goto out_rx_ring_cleanup;
802 	}
803 	memset(priv->tx_ring, 0, ipoib_sendq_size * sizeof *priv->tx_ring);
804 
805 	/* priv->tx_head, tx_tail & tx_outstanding are already 0 */
806 
807 	if (ipoib_ib_dev_init(priv, ca, port))
808 		goto out_tx_ring_cleanup;
809 
810 	return 0;
811 
812 out_tx_ring_cleanup:
813 	kfree(priv->tx_ring);
814 
815 out_rx_ring_cleanup:
816 	kfree(priv->rx_ring);
817 
818 out:
819 	return -ENOMEM;
820 }
821 
822 static void
823 ipoib_detach(struct ipoib_dev_priv *priv)
824 {
825 	struct ifnet *dev;
826 
827 	dev = priv->dev;
828 	if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
829 		priv->gone = 1;
830 		bpfdetach(dev);
831 		if_detach(dev);
832 		if_free(dev);
833 		free_unr(ipoib_unrhdr, priv->unit);
834 	} else
835 		VLAN_SETCOOKIE(priv->dev, NULL);
836 
837 	free(priv, M_TEMP);
838 }
839 
840 void
841 ipoib_dev_cleanup(struct ipoib_dev_priv *priv)
842 {
843 	struct ipoib_dev_priv *cpriv, *tcpriv;
844 
845 	/* Delete any child interfaces first */
846 	list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
847 		ipoib_dev_cleanup(cpriv);
848 		ipoib_detach(cpriv);
849 	}
850 
851 	ipoib_ib_dev_cleanup(priv);
852 
853 	kfree(priv->rx_ring);
854 	kfree(priv->tx_ring);
855 
856 	priv->rx_ring = NULL;
857 	priv->tx_ring = NULL;
858 }
859 
860 static struct ipoib_dev_priv *
861 ipoib_priv_alloc(void)
862 {
863 	struct ipoib_dev_priv *priv;
864 
865 	priv = malloc(sizeof(struct ipoib_dev_priv), M_TEMP, M_ZERO|M_WAITOK);
866 	spin_lock_init(&priv->lock);
867 	spin_lock_init(&priv->drain_lock);
868 	mutex_init(&priv->vlan_mutex);
869 	INIT_LIST_HEAD(&priv->path_list);
870 	INIT_LIST_HEAD(&priv->child_intfs);
871 	INIT_LIST_HEAD(&priv->dead_ahs);
872 	INIT_LIST_HEAD(&priv->multicast_list);
873 	INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
874 	INIT_DELAYED_WORK(&priv->mcast_task,   ipoib_mcast_join_task);
875 	INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
876 	INIT_WORK(&priv->flush_light,   ipoib_ib_dev_flush_light);
877 	INIT_WORK(&priv->flush_normal,   ipoib_ib_dev_flush_normal);
878 	INIT_WORK(&priv->flush_heavy,   ipoib_ib_dev_flush_heavy);
879 	INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
880 	INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
881 	memcpy(priv->broadcastaddr, ipv4_bcast_addr, INFINIBAND_ALEN);
882 
883 	return (priv);
884 }
885 
886 struct ipoib_dev_priv *
887 ipoib_intf_alloc(const char *name)
888 {
889 	struct ipoib_dev_priv *priv;
890 	struct sockaddr_dl *sdl;
891 	struct ifnet *dev;
892 
893 	priv = ipoib_priv_alloc();
894 	dev = priv->dev = if_alloc(IFT_INFINIBAND);
895 	if (!dev) {
896 		free(priv, M_TEMP);
897 		return NULL;
898 	}
899 	dev->if_softc = priv;
900 	priv->unit = alloc_unr(ipoib_unrhdr);
901 	if (priv->unit == -1) {
902 		if_free(dev);
903 		free(priv, M_TEMP);
904 		return NULL;
905 	}
906 	if_initname(dev, name, priv->unit);
907 	dev->if_flags = IFF_BROADCAST | IFF_MULTICAST;
908 	dev->if_addrlen = INFINIBAND_ALEN;
909 	dev->if_hdrlen = IPOIB_HEADER_LEN;
910 	if_attach(dev);
911 	dev->if_init = ipoib_init;
912 	dev->if_ioctl = ipoib_ioctl;
913 	dev->if_start = ipoib_start;
914 	dev->if_output = ipoib_output;
915 	dev->if_input = ipoib_input;
916 	dev->if_resolvemulti = ipoib_resolvemulti;
917 	dev->if_baudrate = IF_Gbps(10);
918 	dev->if_broadcastaddr = priv->broadcastaddr;
919 	dev->if_snd.ifq_maxlen = ipoib_sendq_size * 2;
920 	sdl = (struct sockaddr_dl *)dev->if_addr->ifa_addr;
921 	sdl->sdl_type = IFT_INFINIBAND;
922 	sdl->sdl_alen = dev->if_addrlen;
923 	priv->dev = dev;
924 	if_link_state_change(dev, LINK_STATE_DOWN);
925 	bpfattach(dev, DLT_EN10MB, ETHER_HDR_LEN);
926 
927 	return dev->if_softc;
928 }
929 
930 int
931 ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
932 {
933 	struct ib_device_attr *device_attr = &hca->attrs;
934 
935 	priv->hca_caps = device_attr->device_cap_flags;
936 
937 	priv->dev->if_hwassist = 0;
938 	priv->dev->if_capabilities = 0;
939 
940 #ifndef CONFIG_INFINIBAND_IPOIB_CM
941 	if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
942 		set_bit(IPOIB_FLAG_CSUM, &priv->flags);
943 		priv->dev->if_hwassist = CSUM_IP | CSUM_TCP | CSUM_UDP;
944 		priv->dev->if_capabilities = IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM;
945 	}
946 
947 #if 0
948 	if (priv->dev->features & NETIF_F_SG && priv->hca_caps & IB_DEVICE_UD_TSO) {
949 		priv->dev->if_capabilities |= IFCAP_TSO4;
950 		priv->dev->if_hwassist |= CSUM_TSO;
951 	}
952 #endif
953 #endif
954 	priv->dev->if_capabilities |=
955 	    IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_LINKSTATE;
956 	priv->dev->if_capenable = priv->dev->if_capabilities;
957 
958 	return 0;
959 }
960 
961 
962 static struct ifnet *
963 ipoib_add_port(const char *format, struct ib_device *hca, u8 port)
964 {
965 	struct ipoib_dev_priv *priv;
966 	struct ib_port_attr attr;
967 	int result = -ENOMEM;
968 
969 	priv = ipoib_intf_alloc(format);
970 	if (!priv)
971 		goto alloc_mem_failed;
972 
973 	if (!ib_query_port(hca, port, &attr))
974 		priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
975 	else {
976 		printk(KERN_WARNING "%s: ib_query_port %d failed\n",
977 		       hca->name, port);
978 		goto device_init_failed;
979 	}
980 
981 	/* MTU will be reset when mcast join happens */
982 	priv->dev->if_mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
983 	priv->mcast_mtu = priv->admin_mtu = priv->dev->if_mtu;
984 
985 	result = ib_query_pkey(hca, port, 0, &priv->pkey);
986 	if (result) {
987 		printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
988 		       hca->name, port, result);
989 		goto device_init_failed;
990 	}
991 
992 	if (ipoib_set_dev_features(priv, hca))
993 		goto device_init_failed;
994 
995 	/*
996 	 * Set the full membership bit, so that we join the right
997 	 * broadcast group, etc.
998 	 */
999 	priv->pkey |= 0x8000;
1000 
1001 	priv->broadcastaddr[8] = priv->pkey >> 8;
1002 	priv->broadcastaddr[9] = priv->pkey & 0xff;
1003 
1004 	result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
1005 	if (result) {
1006 		printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1007 		       hca->name, port, result);
1008 		goto device_init_failed;
1009 	}
1010 	memcpy(IF_LLADDR(priv->dev) + 4, priv->local_gid.raw, sizeof (union ib_gid));
1011 
1012 	result = ipoib_dev_init(priv, hca, port);
1013 	if (result < 0) {
1014 		printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1015 		       hca->name, port, result);
1016 		goto device_init_failed;
1017 	}
1018 	if (ipoib_cm_admin_enabled(priv))
1019 		priv->dev->if_mtu = IPOIB_CM_MTU(ipoib_cm_max_mtu(priv));
1020 
1021 	INIT_IB_EVENT_HANDLER(&priv->event_handler,
1022 			      priv->ca, ipoib_event);
1023 	result = ib_register_event_handler(&priv->event_handler);
1024 	if (result < 0) {
1025 		printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1026 		       "port %d (ret = %d)\n",
1027 		       hca->name, port, result);
1028 		goto event_failed;
1029 	}
1030 	if_printf(priv->dev, "Attached to %s port %d\n", hca->name, port);
1031 
1032 	return priv->dev;
1033 
1034 event_failed:
1035 	ipoib_dev_cleanup(priv);
1036 
1037 device_init_failed:
1038 	ipoib_detach(priv);
1039 
1040 alloc_mem_failed:
1041 	return ERR_PTR(result);
1042 }
1043 
1044 static void
1045 ipoib_add_one(struct ib_device *device)
1046 {
1047 	struct list_head *dev_list;
1048 	struct ifnet *dev;
1049 	struct ipoib_dev_priv *priv;
1050 	int s, e, p;
1051 
1052 	if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1053 		return;
1054 
1055 	dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1056 	if (!dev_list)
1057 		return;
1058 
1059 	INIT_LIST_HEAD(dev_list);
1060 
1061 	if (device->node_type == RDMA_NODE_IB_SWITCH) {
1062 		s = 0;
1063 		e = 0;
1064 	} else {
1065 		s = 1;
1066 		e = device->phys_port_cnt;
1067 	}
1068 
1069 	for (p = s; p <= e; ++p) {
1070 		if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
1071 			continue;
1072 		dev = ipoib_add_port("ib", device, p);
1073 		if (!IS_ERR(dev)) {
1074 			priv = dev->if_softc;
1075 			list_add_tail(&priv->list, dev_list);
1076 		}
1077 	}
1078 
1079 	ib_set_client_data(device, &ipoib_client, dev_list);
1080 }
1081 
1082 static void
1083 ipoib_remove_one(struct ib_device *device, void *client_data)
1084 {
1085 	struct ipoib_dev_priv *priv, *tmp;
1086 	struct list_head *dev_list = client_data;
1087 
1088 	if (!dev_list)
1089 		return;
1090 
1091 	if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1092 		return;
1093 
1094 	list_for_each_entry_safe(priv, tmp, dev_list, list) {
1095 		if (rdma_port_get_link_layer(device, priv->port) != IB_LINK_LAYER_INFINIBAND)
1096 			continue;
1097 
1098 		ipoib_stop(priv);
1099 
1100 		ib_unregister_event_handler(&priv->event_handler);
1101 
1102 		/* dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP); */
1103 
1104 		flush_workqueue(ipoib_workqueue);
1105 
1106 		ipoib_dev_cleanup(priv);
1107 		ipoib_detach(priv);
1108 	}
1109 
1110 	kfree(dev_list);
1111 }
1112 
1113 static void
1114 ipoib_config_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
1115 {
1116 	struct ipoib_dev_priv *parent;
1117 	struct ipoib_dev_priv *priv;
1118 	struct ifnet *dev;
1119 	uint16_t pkey;
1120 	int error;
1121 
1122 	if (ifp->if_type != IFT_INFINIBAND)
1123 		return;
1124 	dev = VLAN_DEVAT(ifp, vtag);
1125 	if (dev == NULL)
1126 		return;
1127 	priv = NULL;
1128 	error = 0;
1129 	parent = ifp->if_softc;
1130 	/* We only support 15 bits of pkey. */
1131 	if (vtag & 0x8000)
1132 		return;
1133 	pkey = vtag | 0x8000;	/* Set full membership bit. */
1134 	if (pkey == parent->pkey)
1135 		return;
1136 	/* Check for dups */
1137 	mutex_lock(&parent->vlan_mutex);
1138 	list_for_each_entry(priv, &parent->child_intfs, list) {
1139 		if (priv->pkey == pkey) {
1140 			priv = NULL;
1141 			error = EBUSY;
1142 			goto out;
1143 		}
1144 	}
1145 	priv = ipoib_priv_alloc();
1146 	priv->dev = dev;
1147 	priv->max_ib_mtu = parent->max_ib_mtu;
1148 	priv->mcast_mtu = priv->admin_mtu = parent->dev->if_mtu;
1149 	set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
1150 	error = ipoib_set_dev_features(priv, parent->ca);
1151 	if (error)
1152 		goto out;
1153 	priv->pkey = pkey;
1154 	priv->broadcastaddr[8] = pkey >> 8;
1155 	priv->broadcastaddr[9] = pkey & 0xff;
1156 	dev->if_broadcastaddr = priv->broadcastaddr;
1157 	error = ipoib_dev_init(priv, parent->ca, parent->port);
1158 	if (error)
1159 		goto out;
1160 	priv->parent = parent->dev;
1161 	list_add_tail(&priv->list, &parent->child_intfs);
1162 	VLAN_SETCOOKIE(dev, priv);
1163 	dev->if_start = ipoib_vlan_start;
1164 	dev->if_drv_flags &= ~IFF_DRV_RUNNING;
1165 	dev->if_hdrlen = IPOIB_HEADER_LEN;
1166 	if (ifp->if_drv_flags & IFF_DRV_RUNNING)
1167 		ipoib_open(priv);
1168 	mutex_unlock(&parent->vlan_mutex);
1169 	return;
1170 out:
1171 	mutex_unlock(&parent->vlan_mutex);
1172 	if (priv)
1173 		free(priv, M_TEMP);
1174 	if (error)
1175 		ipoib_warn(parent,
1176 		    "failed to initialize subinterface: device %s, port %d vtag 0x%X",
1177 		    parent->ca->name, parent->port, vtag);
1178 	return;
1179 }
1180 
1181 static void
1182 ipoib_unconfig_vlan(void *arg, struct ifnet *ifp, u_int16_t vtag)
1183 {
1184 	struct ipoib_dev_priv *parent;
1185 	struct ipoib_dev_priv *priv;
1186 	struct ifnet *dev;
1187 	uint16_t pkey;
1188 
1189 	if (ifp->if_type != IFT_INFINIBAND)
1190 		return;
1191 
1192 	dev = VLAN_DEVAT(ifp, vtag);
1193 	if (dev)
1194 		VLAN_SETCOOKIE(dev, NULL);
1195 	pkey = vtag | 0x8000;
1196 	parent = ifp->if_softc;
1197 	mutex_lock(&parent->vlan_mutex);
1198 	list_for_each_entry(priv, &parent->child_intfs, list) {
1199 		if (priv->pkey == pkey) {
1200 			ipoib_dev_cleanup(priv);
1201 			list_del(&priv->list);
1202 			break;
1203 		}
1204 	}
1205 	mutex_unlock(&parent->vlan_mutex);
1206 }
1207 
1208 eventhandler_tag ipoib_vlan_attach;
1209 eventhandler_tag ipoib_vlan_detach;
1210 
1211 static int __init
1212 ipoib_init_module(void)
1213 {
1214 	int ret;
1215 
1216 	ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1217 	ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1218 	ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1219 
1220 	ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1221 	ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1222 	ipoib_sendq_size = max(ipoib_sendq_size, max(2 * MAX_SEND_CQE,
1223 						     IPOIB_MIN_QUEUE_SIZE));
1224 #ifdef CONFIG_INFINIBAND_IPOIB_CM
1225 	ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1226 #endif
1227 
1228 	ipoib_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
1229 		ipoib_config_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1230 	ipoib_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
1231 		ipoib_unconfig_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1232 
1233 	/*
1234 	 * We create our own workqueue mainly because we want to be
1235 	 * able to flush it when devices are being removed.  We can't
1236 	 * use schedule_work()/flush_scheduled_work() because both
1237 	 * unregister_netdev() and linkwatch_event take the rtnl lock,
1238 	 * so flush_scheduled_work() can deadlock during device
1239 	 * removal.
1240 	 */
1241 	ipoib_workqueue = create_singlethread_workqueue("ipoib");
1242 	if (!ipoib_workqueue) {
1243 		ret = -ENOMEM;
1244 		goto err_fs;
1245 	}
1246 
1247 	ib_sa_register_client(&ipoib_sa_client);
1248 
1249 	ret = ib_register_client(&ipoib_client);
1250 	if (ret)
1251 		goto err_sa;
1252 
1253 	return 0;
1254 
1255 err_sa:
1256 	ib_sa_unregister_client(&ipoib_sa_client);
1257 	destroy_workqueue(ipoib_workqueue);
1258 
1259 err_fs:
1260 	return ret;
1261 }
1262 
1263 static void __exit
1264 ipoib_cleanup_module(void)
1265 {
1266 
1267 	EVENTHANDLER_DEREGISTER(vlan_config, ipoib_vlan_attach);
1268 	EVENTHANDLER_DEREGISTER(vlan_unconfig, ipoib_vlan_detach);
1269 	ib_unregister_client(&ipoib_client);
1270 	ib_sa_unregister_client(&ipoib_sa_client);
1271 	destroy_workqueue(ipoib_workqueue);
1272 }
1273 
1274 /*
1275  * Infiniband output routine.
1276  */
1277 static int
1278 ipoib_output(struct ifnet *ifp, struct mbuf *m,
1279 	const struct sockaddr *dst, struct route *ro)
1280 {
1281 	u_char edst[INFINIBAND_ALEN];
1282 #if defined(INET) || defined(INET6)
1283 	struct llentry *lle = NULL;
1284 #endif
1285 	struct ipoib_header *eh;
1286 	int error = 0, is_gw = 0;
1287 	short type;
1288 
1289 	if (ro != NULL)
1290 		is_gw = (ro->ro_flags & RT_HAS_GW) != 0;
1291 #ifdef MAC
1292 	error = mac_ifnet_check_transmit(ifp, m);
1293 	if (error)
1294 		goto bad;
1295 #endif
1296 
1297 	M_PROFILE(m);
1298 	if (ifp->if_flags & IFF_MONITOR) {
1299 		error = ENETDOWN;
1300 		goto bad;
1301 	}
1302 	if (!((ifp->if_flags & IFF_UP) &&
1303 	    (ifp->if_drv_flags & IFF_DRV_RUNNING))) {
1304 		error = ENETDOWN;
1305 		goto bad;
1306 	}
1307 
1308 	switch (dst->sa_family) {
1309 #ifdef INET
1310 	case AF_INET:
1311 		if (lle != NULL && (lle->la_flags & LLE_VALID))
1312 			memcpy(edst, lle->ll_addr, sizeof(edst));
1313 		else if (m->m_flags & M_MCAST)
1314 			ip_ib_mc_map(((struct sockaddr_in *)dst)->sin_addr.s_addr, ifp->if_broadcastaddr, edst);
1315 		else
1316 			error = arpresolve(ifp, is_gw, m, dst, edst, NULL, NULL);
1317 		if (error)
1318 			return (error == EWOULDBLOCK ? 0 : error);
1319 		type = htons(ETHERTYPE_IP);
1320 		break;
1321 	case AF_ARP:
1322 	{
1323 		struct arphdr *ah;
1324 		ah = mtod(m, struct arphdr *);
1325 		ah->ar_hrd = htons(ARPHRD_INFINIBAND);
1326 
1327 		switch(ntohs(ah->ar_op)) {
1328 		case ARPOP_REVREQUEST:
1329 		case ARPOP_REVREPLY:
1330 			type = htons(ETHERTYPE_REVARP);
1331 			break;
1332 		case ARPOP_REQUEST:
1333 		case ARPOP_REPLY:
1334 		default:
1335 			type = htons(ETHERTYPE_ARP);
1336 			break;
1337 		}
1338 
1339 		if (m->m_flags & M_BCAST)
1340 			bcopy(ifp->if_broadcastaddr, edst, INFINIBAND_ALEN);
1341 		else
1342 			bcopy(ar_tha(ah), edst, INFINIBAND_ALEN);
1343 
1344 	}
1345 	break;
1346 #endif
1347 #ifdef INET6
1348 	case AF_INET6:
1349 		if (lle != NULL && (lle->la_flags & LLE_VALID))
1350 			memcpy(edst, lle->ll_addr, sizeof(edst));
1351 		else if (m->m_flags & M_MCAST)
1352 			ipv6_ib_mc_map(&((struct sockaddr_in6 *)dst)->sin6_addr, ifp->if_broadcastaddr, edst);
1353 		else
1354 			error = nd6_resolve(ifp, is_gw, m, dst, edst, NULL, NULL);
1355 		if (error)
1356 			return error;
1357 		type = htons(ETHERTYPE_IPV6);
1358 		break;
1359 #endif
1360 
1361 	default:
1362 		if_printf(ifp, "can't handle af%d\n", dst->sa_family);
1363 		error = EAFNOSUPPORT;
1364 		goto bad;
1365 	}
1366 
1367 	/*
1368 	 * Add local net header.  If no space in first mbuf,
1369 	 * allocate another.
1370 	 */
1371 	M_PREPEND(m, IPOIB_HEADER_LEN, M_NOWAIT);
1372 	if (m == NULL) {
1373 		error = ENOBUFS;
1374 		goto bad;
1375 	}
1376 	eh = mtod(m, struct ipoib_header *);
1377 	(void)memcpy(&eh->proto, &type, sizeof(eh->proto));
1378 	(void)memcpy(&eh->hwaddr, edst, sizeof (edst));
1379 
1380 	/*
1381 	 * Queue message on interface, update output statistics if
1382 	 * successful, and start output if interface not yet active.
1383 	 */
1384 	return ((ifp->if_transmit)(ifp, m));
1385 bad:
1386 	if (m != NULL)
1387 		m_freem(m);
1388 	return (error);
1389 }
1390 
1391 /*
1392  * Upper layer processing for a received Infiniband packet.
1393  */
1394 void
1395 ipoib_demux(struct ifnet *ifp, struct mbuf *m, u_short proto)
1396 {
1397 	int isr;
1398 
1399 #ifdef MAC
1400 	/*
1401 	 * Tag the mbuf with an appropriate MAC label before any other
1402 	 * consumers can get to it.
1403 	 */
1404 	mac_ifnet_create_mbuf(ifp, m);
1405 #endif
1406 	/* Allow monitor mode to claim this frame, after stats are updated. */
1407 	if (ifp->if_flags & IFF_MONITOR) {
1408 		if_printf(ifp, "discard frame at IFF_MONITOR\n");
1409 		m_freem(m);
1410 		return;
1411 	}
1412 	/*
1413 	 * Dispatch frame to upper layer.
1414 	 */
1415 	switch (proto) {
1416 #ifdef INET
1417 	case ETHERTYPE_IP:
1418 		isr = NETISR_IP;
1419 		break;
1420 
1421 	case ETHERTYPE_ARP:
1422 		if (ifp->if_flags & IFF_NOARP) {
1423 			/* Discard packet if ARP is disabled on interface */
1424 			m_freem(m);
1425 			return;
1426 		}
1427 		isr = NETISR_ARP;
1428 		break;
1429 #endif
1430 #ifdef INET6
1431 	case ETHERTYPE_IPV6:
1432 		isr = NETISR_IPV6;
1433 		break;
1434 #endif
1435 	default:
1436 		goto discard;
1437 	}
1438 	netisr_dispatch(isr, m);
1439 	return;
1440 
1441 discard:
1442 	m_freem(m);
1443 }
1444 
1445 /*
1446  * Process a received Infiniband packet.
1447  */
1448 static void
1449 ipoib_input(struct ifnet *ifp, struct mbuf *m)
1450 {
1451 	struct ipoib_header *eh;
1452 
1453 	if ((ifp->if_flags & IFF_UP) == 0) {
1454 		m_freem(m);
1455 		return;
1456 	}
1457 	CURVNET_SET_QUIET(ifp->if_vnet);
1458 
1459 	/* Let BPF have it before we strip the header. */
1460 	IPOIB_MTAP(ifp, m);
1461 	eh = mtod(m, struct ipoib_header *);
1462 	/*
1463 	 * Reset layer specific mbuf flags to avoid confusing upper layers.
1464 	 * Strip off Infiniband header.
1465 	 */
1466 	m->m_flags &= ~M_VLANTAG;
1467 	m_clrprotoflags(m);
1468 	m_adj(m, IPOIB_HEADER_LEN);
1469 
1470 	if (IPOIB_IS_MULTICAST(eh->hwaddr)) {
1471 		if (memcmp(eh->hwaddr, ifp->if_broadcastaddr,
1472 		    ifp->if_addrlen) == 0)
1473 			m->m_flags |= M_BCAST;
1474 		else
1475 			m->m_flags |= M_MCAST;
1476 		if_inc_counter(ifp, IFCOUNTER_IMCASTS, 1);
1477 	}
1478 
1479 	ipoib_demux(ifp, m, ntohs(eh->proto));
1480 	CURVNET_RESTORE();
1481 }
1482 
1483 static int
1484 ipoib_resolvemulti(struct ifnet *ifp, struct sockaddr **llsa,
1485 	struct sockaddr *sa)
1486 {
1487 	struct sockaddr_dl *sdl;
1488 #ifdef INET
1489 	struct sockaddr_in *sin;
1490 #endif
1491 #ifdef INET6
1492 	struct sockaddr_in6 *sin6;
1493 #endif
1494 	u_char *e_addr;
1495 
1496 	switch(sa->sa_family) {
1497 	case AF_LINK:
1498 		/*
1499 		 * No mapping needed. Just check that it's a valid MC address.
1500 		 */
1501 		sdl = (struct sockaddr_dl *)sa;
1502 		e_addr = LLADDR(sdl);
1503 		if (!IPOIB_IS_MULTICAST(e_addr))
1504 			return EADDRNOTAVAIL;
1505 		*llsa = NULL;
1506 		return 0;
1507 
1508 #ifdef INET
1509 	case AF_INET:
1510 		sin = (struct sockaddr_in *)sa;
1511 		if (!IN_MULTICAST(ntohl(sin->sin_addr.s_addr)))
1512 			return EADDRNOTAVAIL;
1513 		sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND);
1514 		sdl->sdl_alen = INFINIBAND_ALEN;
1515 		e_addr = LLADDR(sdl);
1516 		ip_ib_mc_map(sin->sin_addr.s_addr, ifp->if_broadcastaddr,
1517 		    e_addr);
1518 		*llsa = (struct sockaddr *)sdl;
1519 		return 0;
1520 #endif
1521 #ifdef INET6
1522 	case AF_INET6:
1523 		sin6 = (struct sockaddr_in6 *)sa;
1524 		/*
1525 		 * An IP6 address of 0 means listen to all
1526 		 * of the multicast address used for IP6.
1527 		 * This has no meaning in ipoib.
1528 		 */
1529 		if (IN6_IS_ADDR_UNSPECIFIED(&sin6->sin6_addr))
1530 			return EADDRNOTAVAIL;
1531 		if (!IN6_IS_ADDR_MULTICAST(&sin6->sin6_addr))
1532 			return EADDRNOTAVAIL;
1533 		sdl = link_init_sdl(ifp, *llsa, IFT_INFINIBAND);
1534 		sdl->sdl_alen = INFINIBAND_ALEN;
1535 		e_addr = LLADDR(sdl);
1536 		ipv6_ib_mc_map(&sin6->sin6_addr, ifp->if_broadcastaddr, e_addr);
1537 		*llsa = (struct sockaddr *)sdl;
1538 		return 0;
1539 #endif
1540 
1541 	default:
1542 		return EAFNOSUPPORT;
1543 	}
1544 }
1545 
1546 module_init(ipoib_init_module);
1547 module_exit(ipoib_cleanup_module);
1548 
1549 static int
1550 ipoib_evhand(module_t mod, int event, void *arg)
1551 {
1552 	                return (0);
1553 }
1554 
1555 static moduledata_t ipoib_mod = {
1556 	                .name = "ipoib",
1557 			                .evhand = ipoib_evhand,
1558 };
1559 
1560 DECLARE_MODULE(ipoib, ipoib_mod, SI_SUB_LAST, SI_ORDER_ANY);
1561 MODULE_DEPEND(ipoib, ibcore, 1, 1, 1);
1562 MODULE_DEPEND(ipoib, linuxkpi, 1, 1, 1);
1563