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