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