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 #include "ipoib.h"
39 #include <sys/eventhandler.h>
40
41 #include <linux/module.h>
42
43 #include <linux/slab.h>
44 #include <linux/kernel.h>
45 #include <linux/vmalloc.h>
46
47 #include <linux/if_vlan.h>
48
49 #include <net/infiniband.h>
50
51 #include <rdma/ib_addr.h>
52 #include <rdma/ib_cache.h>
53
54 MODULE_AUTHOR("Roland Dreier");
55 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
56 MODULE_LICENSE("Dual BSD/GPL");
57
58 int ipoib_sendq_size = IPOIB_TX_RING_SIZE;
59 int ipoib_recvq_size = IPOIB_RX_RING_SIZE;
60
61 module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
62 MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
63 module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
64 MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
65
66 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
67 int ipoib_debug_level = 1;
68
69 module_param_named(debug_level, ipoib_debug_level, int, 0644);
70 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
71 #endif
72
73 struct ipoib_path_iter {
74 struct ipoib_dev_priv *priv;
75 struct ipoib_path path;
76 };
77
78 static const u8 ipv4_bcast_addr[] = {
79 0x00, 0xff, 0xff, 0xff,
80 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
81 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
82 };
83
84 struct workqueue_struct *ipoib_workqueue;
85
86 struct ib_sa_client ipoib_sa_client;
87
88 static void ipoib_add_one(struct ib_device *device);
89 static void ipoib_remove_one(struct ib_device *device, void *client_data);
90 static if_t ipoib_get_net_dev_by_params(
91 struct ib_device *dev, u8 port, u16 pkey,
92 const union ib_gid *gid, const struct sockaddr *addr,
93 void *client_data);
94 static void ipoib_start(if_t dev);
95 static int ipoib_ioctl(if_t ifp, u_long command, caddr_t data);
96
97 static struct unrhdr *ipoib_unrhdr;
98
99 static void
ipoib_unrhdr_init(void * arg)100 ipoib_unrhdr_init(void *arg)
101 {
102
103 ipoib_unrhdr = new_unrhdr(0, 65535, NULL);
104 }
105 SYSINIT(ipoib_unrhdr_init, SI_SUB_KLD - 1, SI_ORDER_ANY, ipoib_unrhdr_init, NULL);
106
107 static void
ipoib_unrhdr_uninit(void * arg)108 ipoib_unrhdr_uninit(void *arg)
109 {
110
111 if (ipoib_unrhdr != NULL) {
112 struct unrhdr *hdr;
113
114 hdr = ipoib_unrhdr;
115 ipoib_unrhdr = NULL;
116
117 delete_unrhdr(hdr);
118 }
119 }
120 SYSUNINIT(ipoib_unrhdr_uninit, SI_SUB_KLD - 1, SI_ORDER_ANY, ipoib_unrhdr_uninit, NULL);
121
122 static struct ib_client ipoib_client = {
123 .name = "ipoib",
124 .add = ipoib_add_one,
125 .remove = ipoib_remove_one,
126 .get_net_dev_by_params = ipoib_get_net_dev_by_params,
127 };
128
129 int
ipoib_open(struct ipoib_dev_priv * priv)130 ipoib_open(struct ipoib_dev_priv *priv)
131 {
132 if_t dev = priv->dev;
133
134 ipoib_dbg(priv, "bringing up interface\n");
135
136 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
137
138 if (ipoib_pkey_dev_delay_open(priv))
139 return 0;
140
141 if (ipoib_ib_dev_open(priv))
142 goto err_disable;
143
144 if (ipoib_ib_dev_up(priv))
145 goto err_stop;
146
147 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
148 struct ipoib_dev_priv *cpriv;
149
150 /* Bring up any child interfaces too */
151 mutex_lock(&priv->vlan_mutex);
152 list_for_each_entry(cpriv, &priv->child_intfs, list)
153 if ((if_getdrvflags(cpriv->dev) & IFF_DRV_RUNNING) == 0)
154 ipoib_open(cpriv);
155 mutex_unlock(&priv->vlan_mutex);
156 }
157 if_setdrvflagbits(dev, IFF_DRV_RUNNING, IFF_DRV_OACTIVE);
158
159 return 0;
160
161 err_stop:
162 ipoib_ib_dev_stop(priv, 1);
163
164 err_disable:
165 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
166
167 return -EINVAL;
168 }
169
170 static void
ipoib_init(void * arg)171 ipoib_init(void *arg)
172 {
173 if_t dev;
174 struct ipoib_dev_priv *priv;
175
176 priv = arg;
177 dev = priv->dev;
178 if ((if_getdrvflags(dev) & IFF_DRV_RUNNING) == 0)
179 ipoib_open(priv);
180 queue_work(ipoib_workqueue, &priv->flush_light);
181 }
182
183
184 static int
ipoib_stop(struct ipoib_dev_priv * priv)185 ipoib_stop(struct ipoib_dev_priv *priv)
186 {
187 if_t dev = priv->dev;
188
189 ipoib_dbg(priv, "stopping interface\n");
190
191 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
192
193 if_setdrvflagbits(dev, 0, IFF_DRV_RUNNING | IFF_DRV_OACTIVE);
194
195 ipoib_ib_dev_down(priv, 0);
196 ipoib_ib_dev_stop(priv, 0);
197
198 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
199 struct ipoib_dev_priv *cpriv;
200
201 /* Bring down any child interfaces too */
202 mutex_lock(&priv->vlan_mutex);
203 list_for_each_entry(cpriv, &priv->child_intfs, list)
204 if ((if_getdrvflags(cpriv->dev) & IFF_DRV_RUNNING) != 0)
205 ipoib_stop(cpriv);
206 mutex_unlock(&priv->vlan_mutex);
207 }
208
209 return 0;
210 }
211
212 static int
ipoib_propagate_ifnet_mtu(struct ipoib_dev_priv * priv,int new_mtu,bool propagate)213 ipoib_propagate_ifnet_mtu(struct ipoib_dev_priv *priv, int new_mtu,
214 bool propagate)
215 {
216 if_t ifp;
217 struct ifreq ifr;
218 int error;
219
220 ifp = priv->dev;
221 if (if_getmtu(ifp) == new_mtu)
222 return (0);
223 if (propagate) {
224 strlcpy(ifr.ifr_name, if_name(ifp), IFNAMSIZ);
225 ifr.ifr_mtu = new_mtu;
226 CURVNET_SET(if_getvnet(ifp));
227 error = ifhwioctl(SIOCSIFMTU, ifp, (caddr_t)&ifr, curthread);
228 CURVNET_RESTORE();
229 } else {
230 if_setmtu(ifp, new_mtu);
231 error = 0;
232 }
233 return (error);
234 }
235
236 int
ipoib_change_mtu(struct ipoib_dev_priv * priv,int new_mtu,bool propagate)237 ipoib_change_mtu(struct ipoib_dev_priv *priv, int new_mtu, bool propagate)
238 {
239 int error, prev_admin_mtu;
240
241 /* dev->if_mtu > 2K ==> connected mode */
242 if (ipoib_cm_admin_enabled(priv)) {
243 if (new_mtu > IPOIB_CM_MTU(ipoib_cm_max_mtu(priv)))
244 return -EINVAL;
245
246 if (new_mtu > priv->mcast_mtu)
247 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
248 priv->mcast_mtu);
249
250 return (ipoib_propagate_ifnet_mtu(priv, new_mtu, propagate));
251 }
252
253 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
254 return -EINVAL;
255
256 prev_admin_mtu = priv->admin_mtu;
257 priv->admin_mtu = new_mtu;
258 error = ipoib_propagate_ifnet_mtu(priv, min(priv->mcast_mtu,
259 priv->admin_mtu), propagate);
260 if (error == 0) {
261 /* check for MTU change to avoid infinite loop */
262 if (prev_admin_mtu != new_mtu)
263 queue_work(ipoib_workqueue, &priv->flush_light);
264 } else
265 priv->admin_mtu = prev_admin_mtu;
266 return (error);
267 }
268
269 static int
ipoib_ioctl(if_t ifp,u_long command,caddr_t data)270 ipoib_ioctl(if_t ifp, u_long command, caddr_t data)
271 {
272 struct ipoib_dev_priv *priv = if_getsoftc(ifp);
273 struct ifaddr *ifa = (struct ifaddr *) data;
274 struct ifreq *ifr = (struct ifreq *) data;
275 int error = 0;
276
277 /* check if detaching */
278 if (priv == NULL)
279 return (ENXIO);
280 /* wait for device to become ready, if any */
281 while (priv->gone == 2)
282 pause("W", 1);
283 /* check for device gone */
284 if (priv->gone != 0)
285 return (ENXIO);
286
287 switch (command) {
288 case SIOCSIFFLAGS:
289 if (if_getflags(ifp) & IFF_UP) {
290 if ((if_getdrvflags(ifp) & IFF_DRV_RUNNING) == 0)
291 error = -ipoib_open(priv);
292 } else
293 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
294 ipoib_stop(priv);
295 break;
296 case SIOCADDMULTI:
297 case SIOCDELMULTI:
298 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
299 queue_work(ipoib_workqueue, &priv->restart_task);
300 break;
301 case SIOCSIFADDR:
302 if_setflagbits(ifp, IFF_UP, 0);
303
304 switch (ifa->ifa_addr->sa_family) {
305 #ifdef INET
306 case AF_INET:
307 if_init(ifp, if_getsoftc(ifp)); /* before arpwhohas */
308 arp_ifinit(ifp, ifa);
309 break;
310 #endif
311 default:
312 if_init(ifp, if_getsoftc(ifp));
313 break;
314 }
315 break;
316
317 case SIOCGIFADDR:
318 bcopy(if_getlladdr(ifp), &ifr->ifr_addr.sa_data[0],
319 INFINIBAND_ALEN);
320 break;
321
322 case SIOCSIFMTU:
323 /*
324 * Set the interface MTU.
325 */
326 error = -ipoib_change_mtu(priv, ifr->ifr_mtu, false);
327 break;
328 default:
329 error = EINVAL;
330 break;
331 }
332 return (error);
333 }
334
335
336 static struct ipoib_path *
__path_find(struct ipoib_dev_priv * priv,void * gid)337 __path_find(struct ipoib_dev_priv *priv, void *gid)
338 {
339 struct rb_node *n = priv->path_tree.rb_node;
340 struct ipoib_path *path;
341 int ret;
342
343 while (n) {
344 path = rb_entry(n, struct ipoib_path, rb_node);
345
346 ret = memcmp(gid, path->pathrec.dgid.raw,
347 sizeof (union ib_gid));
348
349 if (ret < 0)
350 n = n->rb_left;
351 else if (ret > 0)
352 n = n->rb_right;
353 else
354 return path;
355 }
356
357 return NULL;
358 }
359
360 static int
__path_add(struct ipoib_dev_priv * priv,struct ipoib_path * path)361 __path_add(struct ipoib_dev_priv *priv, struct ipoib_path *path)
362 {
363 struct rb_node **n = &priv->path_tree.rb_node;
364 struct rb_node *pn = NULL;
365 struct ipoib_path *tpath;
366 int ret;
367
368 while (*n) {
369 pn = *n;
370 tpath = rb_entry(pn, struct ipoib_path, rb_node);
371
372 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
373 sizeof (union ib_gid));
374 if (ret < 0)
375 n = &pn->rb_left;
376 else if (ret > 0)
377 n = &pn->rb_right;
378 else
379 return -EEXIST;
380 }
381
382 rb_link_node(&path->rb_node, pn, n);
383 rb_insert_color(&path->rb_node, &priv->path_tree);
384
385 list_add_tail(&path->list, &priv->path_list);
386
387 return 0;
388 }
389
390 void
ipoib_path_free(struct ipoib_dev_priv * priv,struct ipoib_path * path)391 ipoib_path_free(struct ipoib_dev_priv *priv, struct ipoib_path *path)
392 {
393
394 _IF_DRAIN(&path->queue);
395
396 if (path->ah)
397 ipoib_put_ah(path->ah);
398 if (ipoib_cm_get(path))
399 ipoib_cm_destroy_tx(ipoib_cm_get(path));
400
401 kfree(path);
402 }
403
404 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
405
406 struct ipoib_path_iter *
ipoib_path_iter_init(struct ipoib_dev_priv * priv)407 ipoib_path_iter_init(struct ipoib_dev_priv *priv)
408 {
409 struct ipoib_path_iter *iter;
410
411 iter = kmalloc(sizeof *iter, GFP_KERNEL);
412 if (!iter)
413 return NULL;
414
415 iter->priv = priv;
416 memset(iter->path.pathrec.dgid.raw, 0, 16);
417
418 if (ipoib_path_iter_next(iter)) {
419 kfree(iter);
420 return NULL;
421 }
422
423 return iter;
424 }
425
426 int
ipoib_path_iter_next(struct ipoib_path_iter * iter)427 ipoib_path_iter_next(struct ipoib_path_iter *iter)
428 {
429 struct ipoib_dev_priv *priv = iter->priv;
430 struct rb_node *n;
431 struct ipoib_path *path;
432 int ret = 1;
433
434 spin_lock_irq(&priv->lock);
435
436 n = rb_first(&priv->path_tree);
437
438 while (n) {
439 path = rb_entry(n, struct ipoib_path, rb_node);
440
441 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
442 sizeof (union ib_gid)) < 0) {
443 iter->path = *path;
444 ret = 0;
445 break;
446 }
447
448 n = rb_next(n);
449 }
450
451 spin_unlock_irq(&priv->lock);
452
453 return ret;
454 }
455
456 void
ipoib_path_iter_read(struct ipoib_path_iter * iter,struct ipoib_path * path)457 ipoib_path_iter_read(struct ipoib_path_iter *iter, struct ipoib_path *path)
458 {
459 *path = iter->path;
460 }
461
462 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
463
464 void
ipoib_mark_paths_invalid(struct ipoib_dev_priv * priv)465 ipoib_mark_paths_invalid(struct ipoib_dev_priv *priv)
466 {
467 struct ipoib_path *path, *tp;
468
469 spin_lock_irq(&priv->lock);
470
471 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
472 ipoib_dbg(priv, "mark path LID 0x%08x GID %16D invalid\n",
473 be32_to_cpu(sa_path_get_dlid(&path->pathrec)),
474 path->pathrec.dgid.raw, ":");
475 path->valid = 0;
476 }
477
478 spin_unlock_irq(&priv->lock);
479 }
480
481 void
ipoib_flush_paths(struct ipoib_dev_priv * priv)482 ipoib_flush_paths(struct ipoib_dev_priv *priv)
483 {
484 struct ipoib_path *path, *tp;
485 LIST_HEAD(remove_list);
486 unsigned long flags;
487
488 spin_lock_irqsave(&priv->lock, flags);
489
490 list_splice_init(&priv->path_list, &remove_list);
491
492 list_for_each_entry(path, &remove_list, list)
493 rb_erase(&path->rb_node, &priv->path_tree);
494
495 list_for_each_entry_safe(path, tp, &remove_list, list) {
496 if (path->query)
497 ib_sa_cancel_query(path->query_id, path->query);
498 spin_unlock_irqrestore(&priv->lock, flags);
499 wait_for_completion(&path->done);
500 ipoib_path_free(priv, path);
501 spin_lock_irqsave(&priv->lock, flags);
502 }
503
504 spin_unlock_irqrestore(&priv->lock, flags);
505 }
506
507 static void
path_rec_completion(int status,struct sa_path_rec * pathrec,void * path_ptr)508 path_rec_completion(int status, struct sa_path_rec *pathrec, void *path_ptr)
509 {
510 struct ipoib_path *path = path_ptr;
511 struct ipoib_dev_priv *priv = path->priv;
512 if_t dev = priv->dev;
513 struct ipoib_ah *ah = NULL;
514 struct ipoib_ah *old_ah = NULL;
515 struct epoch_tracker et;
516 struct ifqueue mbqueue;
517 struct mbuf *mb;
518 unsigned long flags;
519
520 if (!status)
521 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %16D\n",
522 be32_to_cpu(sa_path_get_dlid(pathrec)),
523 pathrec->dgid.raw, ":");
524 else
525 ipoib_dbg(priv, "PathRec status %d for GID %16D\n",
526 status, path->pathrec.dgid.raw, ":");
527
528 bzero(&mbqueue, sizeof(mbqueue));
529
530 if (!status) {
531 struct rdma_ah_attr av;
532
533 if (!ib_init_ah_attr_from_path(priv->ca, priv->port,
534 pathrec, &av, NULL)) {
535 ah = ipoib_create_ah(priv, priv->pd, &av);
536 rdma_destroy_ah_attr(&av);
537 }
538 }
539
540 spin_lock_irqsave(&priv->lock, flags);
541
542 if (!IS_ERR_OR_NULL(ah)) {
543 path->pathrec = *pathrec;
544
545 old_ah = path->ah;
546 path->ah = ah;
547
548 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
549 ah, be32_to_cpu(sa_path_get_dlid(pathrec)),
550 pathrec->sl);
551
552 for (;;) {
553 _IF_DEQUEUE(&path->queue, mb);
554 if (mb == NULL)
555 break;
556 _IF_ENQUEUE(&mbqueue, mb);
557 }
558
559 #ifdef CONFIG_INFINIBAND_IPOIB_CM
560 if (ipoib_cm_enabled(priv, path->hwaddr) && !ipoib_cm_get(path))
561 ipoib_cm_set(path, ipoib_cm_create_tx(priv, path));
562 #endif
563
564 path->valid = 1;
565 }
566
567 path->query = NULL;
568 complete(&path->done);
569
570 spin_unlock_irqrestore(&priv->lock, flags);
571
572 if (old_ah)
573 ipoib_put_ah(old_ah);
574
575 NET_EPOCH_ENTER(et);
576 for (;;) {
577 _IF_DEQUEUE(&mbqueue, mb);
578 if (mb == NULL)
579 break;
580 mb->m_pkthdr.rcvif = dev;
581 if (if_transmit(dev, mb))
582 ipoib_warn(priv, "dev_queue_xmit failed "
583 "to requeue packet\n");
584 }
585 NET_EPOCH_EXIT(et);
586 }
587
588 static struct ipoib_path *
path_rec_create(struct ipoib_dev_priv * priv,uint8_t * hwaddr)589 path_rec_create(struct ipoib_dev_priv *priv, uint8_t *hwaddr)
590 {
591 struct ipoib_path *path;
592
593 if (!priv->broadcast)
594 return NULL;
595
596 path = kzalloc(sizeof *path, GFP_ATOMIC);
597 if (!path)
598 return NULL;
599
600 path->priv = priv;
601
602 bzero(&path->queue, sizeof(path->queue));
603
604 #ifdef CONFIG_INFINIBAND_IPOIB_CM
605 memcpy(&path->hwaddr, hwaddr, INFINIBAND_ALEN);
606 #endif
607 if (rdma_cap_opa_ah(priv->ca, priv->port))
608 path->pathrec.rec_type = SA_PATH_REC_TYPE_OPA;
609 else
610 path->pathrec.rec_type = SA_PATH_REC_TYPE_IB;
611 memcpy(path->pathrec.dgid.raw, &hwaddr[4], sizeof (union ib_gid));
612 path->pathrec.sgid = priv->local_gid;
613 path->pathrec.pkey = cpu_to_be16(priv->pkey);
614 path->pathrec.numb_path = 1;
615 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
616
617 return path;
618 }
619
620 static int
path_rec_start(struct ipoib_dev_priv * priv,struct ipoib_path * path)621 path_rec_start(struct ipoib_dev_priv *priv, struct ipoib_path *path)
622 {
623 if_t dev = priv->dev;
624
625 ib_sa_comp_mask comp_mask = IB_SA_PATH_REC_MTU_SELECTOR | IB_SA_PATH_REC_MTU;
626 struct sa_path_rec p_rec;
627
628 p_rec = path->pathrec;
629 p_rec.mtu_selector = IB_SA_GT;
630
631 switch (roundup_pow_of_two(if_getmtu(dev) + IPOIB_ENCAP_LEN)) {
632 case 512:
633 p_rec.mtu = IB_MTU_256;
634 break;
635 case 1024:
636 p_rec.mtu = IB_MTU_512;
637 break;
638 case 2048:
639 p_rec.mtu = IB_MTU_1024;
640 break;
641 case 4096:
642 p_rec.mtu = IB_MTU_2048;
643 break;
644 default:
645 /* Wildcard everything */
646 comp_mask = 0;
647 p_rec.mtu = 0;
648 p_rec.mtu_selector = 0;
649 }
650
651 ipoib_dbg(priv, "Start path record lookup for %16D MTU > %d\n",
652 p_rec.dgid.raw, ":",
653 comp_mask ? ib_mtu_enum_to_int(p_rec.mtu) : 0);
654
655 init_completion(&path->done);
656
657 path->query_id =
658 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
659 &p_rec, comp_mask |
660 IB_SA_PATH_REC_DGID |
661 IB_SA_PATH_REC_SGID |
662 IB_SA_PATH_REC_NUMB_PATH |
663 IB_SA_PATH_REC_TRAFFIC_CLASS |
664 IB_SA_PATH_REC_PKEY,
665 1000, GFP_ATOMIC,
666 path_rec_completion,
667 path, &path->query);
668 if (path->query_id < 0) {
669 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
670 path->query = NULL;
671 complete(&path->done);
672 return path->query_id;
673 }
674
675 return 0;
676 }
677
678 static void
ipoib_unicast_send(struct mbuf * mb,struct ipoib_dev_priv * priv,struct ipoib_header * eh)679 ipoib_unicast_send(struct mbuf *mb, struct ipoib_dev_priv *priv, struct ipoib_header *eh)
680 {
681 struct ipoib_path *path;
682
683 path = __path_find(priv, eh->hwaddr + 4);
684 if (!path || !path->valid) {
685 int new_path = 0;
686
687 if (!path) {
688 path = path_rec_create(priv, eh->hwaddr);
689 new_path = 1;
690 }
691 if (path) {
692 if (_IF_QLEN(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE)
693 _IF_ENQUEUE(&path->queue, mb);
694 else {
695 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
696 m_freem(mb);
697 }
698
699 if (!path->query && path_rec_start(priv, path)) {
700 if (new_path)
701 ipoib_path_free(priv, path);
702 return;
703 } else
704 __path_add(priv, path);
705 } else {
706 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
707 m_freem(mb);
708 }
709
710 return;
711 }
712
713 if (ipoib_cm_get(path) && ipoib_cm_up(path)) {
714 ipoib_cm_send(priv, mb, ipoib_cm_get(path));
715 } else if (path->ah) {
716 ipoib_send(priv, mb, path->ah, IPOIB_QPN(eh->hwaddr));
717 } else if ((path->query || !path_rec_start(priv, path)) &&
718 path->queue.ifq_len < IPOIB_MAX_PATH_REC_QUEUE) {
719 _IF_ENQUEUE(&path->queue, mb);
720 } else {
721 if_inc_counter(priv->dev, IFCOUNTER_OERRORS, 1);
722 m_freem(mb);
723 }
724 }
725
726 static int
ipoib_send_one(struct ipoib_dev_priv * priv,struct mbuf * mb)727 ipoib_send_one(struct ipoib_dev_priv *priv, struct mbuf *mb)
728 {
729 struct ipoib_header *eh;
730
731 eh = mtod(mb, struct ipoib_header *);
732 if (IPOIB_IS_MULTICAST(eh->hwaddr)) {
733 /* Add in the P_Key for multicast*/
734 eh->hwaddr[8] = (priv->pkey >> 8) & 0xff;
735 eh->hwaddr[9] = priv->pkey & 0xff;
736
737 ipoib_mcast_send(priv, eh->hwaddr + 4, mb);
738 } else
739 ipoib_unicast_send(mb, priv, eh);
740
741 return 0;
742 }
743
744 void
ipoib_start_locked(if_t dev,struct ipoib_dev_priv * priv)745 ipoib_start_locked(if_t dev, struct ipoib_dev_priv *priv)
746 {
747 struct mbuf *mb;
748
749 assert_spin_locked(&priv->lock);
750
751 while (!if_sendq_empty(dev) &&
752 (if_getdrvflags(dev) & IFF_DRV_OACTIVE) == 0) {
753 mb = if_dequeue(dev);
754 if (mb == NULL)
755 break;
756 infiniband_bpf_mtap(dev, mb);
757 ipoib_send_one(priv, mb);
758 }
759 }
760
761 static void
_ipoib_start(if_t dev,struct ipoib_dev_priv * priv)762 _ipoib_start(if_t dev, struct ipoib_dev_priv *priv)
763 {
764
765 if ((if_getdrvflags(dev) & (IFF_DRV_RUNNING|IFF_DRV_OACTIVE)) !=
766 IFF_DRV_RUNNING)
767 return;
768
769 spin_lock(&priv->lock);
770 ipoib_start_locked(dev, priv);
771 spin_unlock(&priv->lock);
772 }
773
774 static void
ipoib_start(if_t dev)775 ipoib_start(if_t dev)
776 {
777 _ipoib_start(dev, if_getsoftc(dev));
778 }
779
780 static void
ipoib_vlan_start(if_t dev)781 ipoib_vlan_start(if_t dev)
782 {
783 struct ipoib_dev_priv *priv;
784 struct mbuf *mb;
785
786 priv = VLAN_COOKIE(dev);
787 if (priv != NULL)
788 return _ipoib_start(dev, priv);
789 while (!if_sendq_empty(dev)) {
790 mb = if_dequeue(dev);
791 if (mb == NULL)
792 break;
793 m_freem(mb);
794 if_inc_counter(dev, IFCOUNTER_OERRORS, 1);
795 }
796 }
797
798 int
ipoib_dev_init(struct ipoib_dev_priv * priv,struct ib_device * ca,int port)799 ipoib_dev_init(struct ipoib_dev_priv *priv, struct ib_device *ca, int port)
800 {
801
802 /* Allocate RX/TX "rings" to hold queued mbs */
803 priv->rx_ring = kcalloc(ipoib_recvq_size,
804 sizeof(*priv->rx_ring),
805 GFP_KERNEL);
806 if (!priv->rx_ring)
807 goto out;
808
809 priv->tx_ring = vzalloc(array_size(ipoib_sendq_size,
810 sizeof(*priv->tx_ring)));
811 if (!priv->tx_ring) {
812 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
813 ca->name, ipoib_sendq_size);
814 goto out_rx_ring_cleanup;
815 }
816
817 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
818
819 if (ipoib_ib_dev_init(priv, ca, port))
820 goto out_tx_ring_cleanup;
821
822 return 0;
823
824 out_tx_ring_cleanup:
825 vfree(priv->tx_ring);
826
827 out_rx_ring_cleanup:
828 kfree(priv->rx_ring);
829
830 out:
831 return -ENOMEM;
832 }
833
834 static void
ipoib_ifdetach(struct ipoib_dev_priv * priv)835 ipoib_ifdetach(struct ipoib_dev_priv *priv)
836 {
837 if_t dev;
838
839 dev = priv->dev;
840 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
841 priv->gone = 1;
842 infiniband_ifdetach(dev);
843 }
844 }
845
846 static void
ipoib_detach(struct ipoib_dev_priv * priv)847 ipoib_detach(struct ipoib_dev_priv *priv)
848 {
849 if_t dev;
850
851 dev = priv->dev;
852 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
853 if_free(dev);
854 free_unr(ipoib_unrhdr, priv->unit);
855 } else
856 VLAN_SETCOOKIE(priv->dev, NULL);
857
858 free(priv, M_TEMP);
859 }
860
861 void
ipoib_dev_cleanup(struct ipoib_dev_priv * priv)862 ipoib_dev_cleanup(struct ipoib_dev_priv *priv)
863 {
864 struct ipoib_dev_priv *cpriv, *tcpriv;
865
866 /* Delete any child interfaces first */
867 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
868 ipoib_ifdetach(cpriv);
869 ipoib_dev_cleanup(cpriv);
870 ipoib_detach(cpriv);
871 }
872
873 ipoib_ib_dev_cleanup(priv);
874
875 kfree(priv->rx_ring);
876 vfree(priv->tx_ring);
877
878 priv->rx_ring = NULL;
879 priv->tx_ring = NULL;
880 }
881
882 static struct ipoib_dev_priv *
ipoib_priv_alloc(void)883 ipoib_priv_alloc(void)
884 {
885 struct ipoib_dev_priv *priv;
886
887 priv = malloc(sizeof(struct ipoib_dev_priv), M_TEMP, M_ZERO|M_WAITOK);
888 spin_lock_init(&priv->lock);
889 spin_lock_init(&priv->drain_lock);
890 mutex_init(&priv->vlan_mutex);
891 INIT_LIST_HEAD(&priv->path_list);
892 INIT_LIST_HEAD(&priv->child_intfs);
893 INIT_LIST_HEAD(&priv->dead_ahs);
894 INIT_LIST_HEAD(&priv->multicast_list);
895 INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
896 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
897 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
898 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
899 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
900 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
901 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
902 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
903 memcpy(priv->broadcastaddr, ipv4_bcast_addr, INFINIBAND_ALEN);
904
905 return (priv);
906 }
907
908 struct ipoib_dev_priv *
ipoib_intf_alloc(const char * name,struct ib_device * hca)909 ipoib_intf_alloc(const char *name, struct ib_device *hca)
910 {
911 struct ipoib_dev_priv *priv;
912 if_t dev;
913
914 priv = ipoib_priv_alloc();
915 dev = priv->dev = if_alloc(IFT_INFINIBAND);
916 if_setsoftc(dev, priv);
917 priv->gone = 2; /* initializing */
918 priv->unit = alloc_unr(ipoib_unrhdr);
919 if (priv->unit == -1) {
920 if_free(dev);
921 free(priv, M_TEMP);
922 return NULL;
923 }
924 if_initname(dev, name, priv->unit);
925 if_setflags(dev, IFF_BROADCAST | IFF_MULTICAST);
926 if ((hca->attrs.device_cap_flags & IB_DEVICE_KNOWSEPOCH) == 0)
927 if_setflagbits(dev, IFF_NEEDSEPOCH, 0);
928
929 infiniband_ifattach(priv->dev, NULL, priv->broadcastaddr);
930
931 if_setinitfn(dev, ipoib_init);
932 if_setioctlfn(dev, ipoib_ioctl);
933 if_setstartfn(dev, ipoib_start);
934
935 if_setsendqlen(dev, ipoib_sendq_size * 2);
936
937 priv->dev = dev;
938 if_link_state_change(priv->dev, LINK_STATE_DOWN);
939
940 return if_getsoftc(dev);
941 }
942
943 int
ipoib_set_dev_features(struct ipoib_dev_priv * priv,struct ib_device * hca)944 ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
945 {
946 struct ib_device_attr *device_attr = &hca->attrs;
947
948 priv->hca_caps = device_attr->device_cap_flags;
949
950 if_sethwassist(priv->dev, 0);
951 if_setcapabilities(priv->dev, 0);
952
953 #ifndef CONFIG_INFINIBAND_IPOIB_CM
954 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
955 set_bit(IPOIB_FLAG_CSUM, &priv->flags);
956 if_sethwassist(priv->dev, CSUM_IP | CSUM_TCP | CSUM_UDP);
957 if_setcapabilities(priv->dev, IFCAP_HWCSUM | IFCAP_VLAN_HWCSUM);
958 }
959
960 #if 0
961 if (priv->dev->features & NETIF_F_SG && priv->hca_caps & IB_DEVICE_UD_TSO) {
962 priv->dev->if_capabilities |= IFCAP_TSO4;
963 priv->dev->if_hwassist |= CSUM_TSO;
964 }
965 #endif
966 #endif
967 if_setcapabilitiesbit(priv->dev,
968 IFCAP_VLAN_HWTAGGING | IFCAP_VLAN_MTU | IFCAP_LINKSTATE, 0);
969 if_setcapenable(priv->dev, if_getcapabilities(priv->dev));
970
971 return 0;
972 }
973
974
975 static if_t
ipoib_add_port(const char * format,struct ib_device * hca,u8 port)976 ipoib_add_port(const char *format, struct ib_device *hca, u8 port)
977 {
978 struct ipoib_dev_priv *priv;
979 struct ib_port_attr attr;
980 int result = -ENOMEM;
981
982 priv = ipoib_intf_alloc(format, hca);
983 if (!priv)
984 goto alloc_mem_failed;
985
986 if (!ib_query_port(hca, port, &attr))
987 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
988 else {
989 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
990 hca->name, port);
991 goto device_init_failed;
992 }
993
994 /* MTU will be reset when mcast join happens */
995 if_setmtu(priv->dev, IPOIB_UD_MTU(priv->max_ib_mtu));
996 priv->mcast_mtu = priv->admin_mtu = if_getmtu(priv->dev);
997
998 result = ib_query_pkey(hca, port, 0, &priv->pkey);
999 if (result) {
1000 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1001 hca->name, port, result);
1002 goto device_init_failed;
1003 }
1004
1005 if (ipoib_set_dev_features(priv, hca))
1006 goto device_init_failed;
1007
1008 /*
1009 * Set the full membership bit, so that we join the right
1010 * broadcast group, etc.
1011 */
1012 priv->pkey |= 0x8000;
1013
1014 priv->broadcastaddr[8] = priv->pkey >> 8;
1015 priv->broadcastaddr[9] = priv->pkey & 0xff;
1016
1017 result = rdma_query_gid(hca, port, 0, &priv->local_gid);
1018 if (result) {
1019 printk(KERN_WARNING "%s: rdma_query_gid port %d failed (ret = %d)\n",
1020 hca->name, port, result);
1021 goto device_init_failed;
1022 }
1023 memcpy(if_getlladdr(priv->dev) + 4, priv->local_gid.raw, sizeof(union ib_gid));
1024
1025 result = ipoib_dev_init(priv, hca, port);
1026 if (result < 0) {
1027 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1028 hca->name, port, result);
1029 goto device_init_failed;
1030 }
1031 if (ipoib_cm_admin_enabled(priv))
1032 if_setmtu(priv->dev, IPOIB_CM_MTU(ipoib_cm_max_mtu(priv)));
1033
1034 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1035 priv->ca, ipoib_event);
1036 ib_register_event_handler(&priv->event_handler);
1037 if_printf(priv->dev, "Attached to %s port %d\n", hca->name, port);
1038
1039 priv->gone = 0; /* ready */
1040
1041 return priv->dev;
1042
1043 device_init_failed:
1044 ipoib_ifdetach(priv);
1045 ipoib_detach(priv);
1046
1047 alloc_mem_failed:
1048 return ERR_PTR(result);
1049 }
1050
1051 static void
ipoib_add_one(struct ib_device * device)1052 ipoib_add_one(struct ib_device *device)
1053 {
1054 struct list_head *dev_list;
1055 if_t dev;
1056 struct ipoib_dev_priv *priv;
1057 int s, e, p;
1058
1059 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1060 return;
1061
1062 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1063 if (!dev_list)
1064 return;
1065
1066 INIT_LIST_HEAD(dev_list);
1067
1068 if (device->node_type == RDMA_NODE_IB_SWITCH) {
1069 s = 0;
1070 e = 0;
1071 } else {
1072 s = 1;
1073 e = device->phys_port_cnt;
1074 }
1075
1076 for (p = s; p <= e; ++p) {
1077 if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
1078 continue;
1079 dev = ipoib_add_port("ib", device, p);
1080 if (!IS_ERR(dev)) {
1081 priv = if_getsoftc(dev);
1082 list_add_tail(&priv->list, dev_list);
1083 }
1084 }
1085
1086 ib_set_client_data(device, &ipoib_client, dev_list);
1087 }
1088
1089 static void
ipoib_remove_one(struct ib_device * device,void * client_data)1090 ipoib_remove_one(struct ib_device *device, void *client_data)
1091 {
1092 struct ipoib_dev_priv *priv, *tmp;
1093 struct list_head *dev_list = client_data;
1094
1095 if (!dev_list)
1096 return;
1097
1098 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1099 return;
1100
1101 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1102 if (rdma_port_get_link_layer(device, priv->port) != IB_LINK_LAYER_INFINIBAND)
1103 continue;
1104
1105 ipoib_ifdetach(priv);
1106 ipoib_stop(priv);
1107
1108 ib_unregister_event_handler(&priv->event_handler);
1109
1110 flush_workqueue(ipoib_workqueue);
1111
1112 ipoib_dev_cleanup(priv);
1113 ipoib_detach(priv);
1114 }
1115
1116 kfree(dev_list);
1117 }
1118
1119 static u_int
ipoib_match_dev_addr_cb(void * arg,struct ifaddr * ifa,u_int count)1120 ipoib_match_dev_addr_cb(void *arg, struct ifaddr *ifa, u_int count)
1121 {
1122 struct sockaddr *addr = arg;
1123
1124 /* If a match is already found, skip this. */
1125 if (count > 0)
1126 return (0);
1127
1128 if (ifa->ifa_addr->sa_len != addr->sa_len)
1129 return (0);
1130
1131 if (memcmp(ifa->ifa_addr, addr, addr->sa_len) == 0)
1132 return (1);
1133
1134 return (0);
1135 }
1136
1137 static int
ipoib_match_dev_addr(const struct sockaddr * addr,if_t dev)1138 ipoib_match_dev_addr(const struct sockaddr *addr, if_t dev)
1139 {
1140 struct epoch_tracker et;
1141 int retval = 0;
1142
1143 NET_EPOCH_ENTER(et);
1144 retval = if_foreach_addr_type(dev, addr->sa_family,
1145 ipoib_match_dev_addr_cb, __DECONST(void *, addr));
1146 NET_EPOCH_EXIT(et);
1147
1148 return (retval);
1149 }
1150
1151 /*
1152 * ipoib_match_gid_pkey_addr - returns the number of IPoIB netdevs on
1153 * top a given ipoib device matching a pkey_index and address, if one
1154 * exists.
1155 *
1156 * @found_net_dev: contains a matching net_device if the return value
1157 * >= 1, with a reference held.
1158 */
1159 static int
ipoib_match_gid_pkey_addr(struct ipoib_dev_priv * priv,const union ib_gid * gid,u16 pkey_index,const struct sockaddr * addr,if_t * found_net_dev)1160 ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
1161 const union ib_gid *gid, u16 pkey_index, const struct sockaddr *addr,
1162 if_t *found_net_dev)
1163 {
1164 struct ipoib_dev_priv *child_priv;
1165 int matches = 0;
1166
1167 if (priv->pkey_index == pkey_index &&
1168 (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
1169 if (addr == NULL || ipoib_match_dev_addr(addr, priv->dev) != 0) {
1170 if (*found_net_dev == NULL) {
1171 if_t net_dev;
1172
1173 if (priv->parent != NULL)
1174 net_dev = priv->parent;
1175 else
1176 net_dev = priv->dev;
1177 *found_net_dev = net_dev;
1178 dev_hold(net_dev);
1179 }
1180 matches++;
1181 }
1182 }
1183
1184 /* Check child interfaces */
1185 mutex_lock(&priv->vlan_mutex);
1186 list_for_each_entry(child_priv, &priv->child_intfs, list) {
1187 matches += ipoib_match_gid_pkey_addr(child_priv, gid,
1188 pkey_index, addr, found_net_dev);
1189 if (matches > 1)
1190 break;
1191 }
1192 mutex_unlock(&priv->vlan_mutex);
1193
1194 return matches;
1195 }
1196
1197 /*
1198 * __ipoib_get_net_dev_by_params - returns the number of matching
1199 * net_devs found (between 0 and 2). Also return the matching
1200 * net_device in the @net_dev parameter, holding a reference to the
1201 * net_device, if the number of matches >= 1
1202 */
1203 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,if_t * net_dev)1204 __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port,
1205 u16 pkey_index, const union ib_gid *gid,
1206 const struct sockaddr *addr, if_t *net_dev)
1207 {
1208 struct ipoib_dev_priv *priv;
1209 int matches = 0;
1210
1211 *net_dev = NULL;
1212
1213 list_for_each_entry(priv, dev_list, list) {
1214 if (priv->port != port)
1215 continue;
1216
1217 matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
1218 addr, net_dev);
1219
1220 if (matches > 1)
1221 break;
1222 }
1223
1224 return matches;
1225 }
1226
1227 static if_t
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)1228 ipoib_get_net_dev_by_params(struct ib_device *dev, u8 port, u16 pkey,
1229 const union ib_gid *gid, const struct sockaddr *addr, void *client_data)
1230 {
1231 if_t net_dev;
1232 struct list_head *dev_list = client_data;
1233 u16 pkey_index;
1234 int matches;
1235 int ret;
1236
1237 if (!rdma_protocol_ib(dev, port))
1238 return NULL;
1239
1240 ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
1241 if (ret)
1242 return NULL;
1243
1244 if (!dev_list)
1245 return NULL;
1246
1247 /* See if we can find a unique device matching the L2 parameters */
1248 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
1249 gid, NULL, &net_dev);
1250
1251 switch (matches) {
1252 case 0:
1253 return NULL;
1254 case 1:
1255 return net_dev;
1256 }
1257
1258 dev_put(net_dev);
1259
1260 /* Couldn't find a unique device with L2 parameters only. Use L3
1261 * address to uniquely match the net device */
1262 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
1263 gid, addr, &net_dev);
1264 switch (matches) {
1265 case 0:
1266 return NULL;
1267 default:
1268 dev_warn_ratelimited(&dev->dev,
1269 "duplicate IP address detected\n");
1270 /* Fall through */
1271 case 1:
1272 return net_dev;
1273 }
1274 }
1275
1276 static void
ipoib_config_vlan(void * arg,if_t ifp,uint16_t vtag)1277 ipoib_config_vlan(void *arg, if_t ifp, uint16_t vtag)
1278 {
1279 struct ipoib_dev_priv *parent;
1280 struct ipoib_dev_priv *priv;
1281 struct epoch_tracker et;
1282 if_t dev;
1283 uint16_t pkey;
1284 int error;
1285
1286 if (if_gettype(ifp) != IFT_INFINIBAND)
1287 return;
1288 NET_EPOCH_ENTER(et);
1289 dev = VLAN_DEVAT(ifp, vtag);
1290 NET_EPOCH_EXIT(et);
1291 if (dev == NULL)
1292 return;
1293 priv = NULL;
1294 error = 0;
1295 parent = if_getsoftc(ifp);
1296 /* We only support 15 bits of pkey. */
1297 if (vtag & 0x8000)
1298 return;
1299 pkey = vtag | 0x8000; /* Set full membership bit. */
1300 if (pkey == parent->pkey)
1301 return;
1302 /* Check for dups */
1303 mutex_lock(&parent->vlan_mutex);
1304 list_for_each_entry(priv, &parent->child_intfs, list) {
1305 if (priv->pkey == pkey) {
1306 priv = NULL;
1307 error = EBUSY;
1308 goto out;
1309 }
1310 }
1311 priv = ipoib_priv_alloc();
1312 priv->dev = dev;
1313 priv->max_ib_mtu = parent->max_ib_mtu;
1314 priv->mcast_mtu = priv->admin_mtu = if_getmtu(parent->dev);
1315 set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
1316 error = ipoib_set_dev_features(priv, parent->ca);
1317 if (error)
1318 goto out;
1319 priv->pkey = pkey;
1320 priv->broadcastaddr[8] = pkey >> 8;
1321 priv->broadcastaddr[9] = pkey & 0xff;
1322 if_setbroadcastaddr(dev, priv->broadcastaddr);
1323 error = ipoib_dev_init(priv, parent->ca, parent->port);
1324 if (error)
1325 goto out;
1326 priv->parent = parent->dev;
1327 list_add_tail(&priv->list, &parent->child_intfs);
1328 VLAN_SETCOOKIE(dev, priv);
1329 if_setstartfn(dev, ipoib_vlan_start);
1330 if_setdrvflagbits(dev, 0, IFF_DRV_RUNNING);
1331 if_setifheaderlen(dev, IPOIB_HEADER_LEN);
1332 if (if_getdrvflags(ifp) & IFF_DRV_RUNNING)
1333 ipoib_open(priv);
1334 mutex_unlock(&parent->vlan_mutex);
1335 return;
1336 out:
1337 mutex_unlock(&parent->vlan_mutex);
1338 if (priv)
1339 free(priv, M_TEMP);
1340 if (error)
1341 ipoib_warn(parent,
1342 "failed to initialize subinterface: device %s, port %d vtag 0x%X",
1343 parent->ca->name, parent->port, vtag);
1344 return;
1345 }
1346
1347 static void
ipoib_unconfig_vlan(void * arg,if_t ifp,uint16_t vtag)1348 ipoib_unconfig_vlan(void *arg, if_t ifp, uint16_t vtag)
1349 {
1350 struct ipoib_dev_priv *parent;
1351 struct ipoib_dev_priv *priv;
1352 struct epoch_tracker et;
1353 if_t dev;
1354 uint16_t pkey;
1355
1356 if (if_gettype(ifp) != IFT_INFINIBAND)
1357 return;
1358
1359 NET_EPOCH_ENTER(et);
1360 dev = VLAN_DEVAT(ifp, vtag);
1361 NET_EPOCH_EXIT(et);
1362 if (dev)
1363 VLAN_SETCOOKIE(dev, NULL);
1364 pkey = vtag | 0x8000;
1365 parent = if_getsoftc(ifp);
1366 mutex_lock(&parent->vlan_mutex);
1367 list_for_each_entry(priv, &parent->child_intfs, list) {
1368 if (priv->pkey == pkey) {
1369 ipoib_dev_cleanup(priv);
1370 list_del(&priv->list);
1371 break;
1372 }
1373 }
1374 mutex_unlock(&parent->vlan_mutex);
1375 }
1376
1377 eventhandler_tag ipoib_vlan_attach;
1378 eventhandler_tag ipoib_vlan_detach;
1379
1380 static int __init
ipoib_init_module(void)1381 ipoib_init_module(void)
1382 {
1383 int ret;
1384
1385 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1386 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1387 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1388
1389 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1390 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1391 ipoib_sendq_size = max(ipoib_sendq_size, max(2 * MAX_SEND_CQE,
1392 IPOIB_MIN_QUEUE_SIZE));
1393 #ifdef CONFIG_INFINIBAND_IPOIB_CM
1394 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1395 #endif
1396
1397 ipoib_vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
1398 ipoib_config_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1399 ipoib_vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
1400 ipoib_unconfig_vlan, NULL, EVENTHANDLER_PRI_FIRST);
1401
1402 /*
1403 * We create our own workqueue mainly because we want to be
1404 * able to flush it when devices are being removed. We can't
1405 * use schedule_work()/flush_scheduled_work() because both
1406 * unregister_netdev() and linkwatch_event take the rtnl lock,
1407 * so flush_scheduled_work() can deadlock during device
1408 * removal.
1409 */
1410 ipoib_workqueue = create_singlethread_workqueue("ipoib");
1411 if (!ipoib_workqueue) {
1412 ret = -ENOMEM;
1413 goto err_fs;
1414 }
1415
1416 ib_sa_register_client(&ipoib_sa_client);
1417
1418 ret = ib_register_client(&ipoib_client);
1419 if (ret)
1420 goto err_sa;
1421
1422 return 0;
1423
1424 err_sa:
1425 ib_sa_unregister_client(&ipoib_sa_client);
1426 destroy_workqueue(ipoib_workqueue);
1427
1428 err_fs:
1429 return ret;
1430 }
1431
1432 static void __exit
ipoib_cleanup_module(void)1433 ipoib_cleanup_module(void)
1434 {
1435
1436 EVENTHANDLER_DEREGISTER(vlan_config, ipoib_vlan_attach);
1437 EVENTHANDLER_DEREGISTER(vlan_unconfig, ipoib_vlan_detach);
1438 ib_unregister_client(&ipoib_client);
1439 ib_sa_unregister_client(&ipoib_sa_client);
1440 destroy_workqueue(ipoib_workqueue);
1441 }
1442 module_init_order(ipoib_init_module, SI_ORDER_FIFTH);
1443 module_exit_order(ipoib_cleanup_module, SI_ORDER_FIFTH);
1444
1445 static int
ipoib_evhand(module_t mod,int event,void * arg)1446 ipoib_evhand(module_t mod, int event, void *arg)
1447 {
1448 return (0);
1449 }
1450
1451 static moduledata_t ipoib_mod = {
1452 .name = "ipoib",
1453 .evhand = ipoib_evhand,
1454 };
1455
1456 DECLARE_MODULE(ipoib, ipoib_mod, SI_SUB_LAST, SI_ORDER_ANY);
1457 MODULE_DEPEND(ipoib, ibcore, 1, 1, 1);
1458 MODULE_DEPEND(ipoib, if_infiniband, 1, 1, 1);
1459 MODULE_DEPEND(ipoib, linuxkpi, 1, 1, 1);
1460