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