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