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