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