]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/infiniband/ulp/ipoib/ipoib_main.c
Merge tag 'rdma-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/roland...
[karo-tx-linux.git] / 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 #include <linux/module.h>
38
39 #include <linux/init.h>
40 #include <linux/slab.h>
41 #include <linux/kernel.h>
42 #include <linux/vmalloc.h>
43
44 #include <linux/if_arp.h>       /* For ARPHRD_xxx */
45
46 #include <linux/ip.h>
47 #include <linux/in.h>
48
49 #include <linux/jhash.h>
50 #include <net/arp.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 __read_mostly = IPOIB_TX_RING_SIZE;
57 int ipoib_recvq_size __read_mostly = 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;
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 net_device *dev;
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_neigh_reclaim(struct rcu_head *rp);
89
90 static struct ib_client ipoib_client = {
91         .name   = "ipoib",
92         .add    = ipoib_add_one,
93         .remove = ipoib_remove_one
94 };
95
96 int ipoib_open(struct net_device *dev)
97 {
98         struct ipoib_dev_priv *priv = netdev_priv(dev);
99
100         ipoib_dbg(priv, "bringing up interface\n");
101
102         set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
103
104         if (ipoib_pkey_dev_delay_open(dev))
105                 return 0;
106
107         if (ipoib_ib_dev_open(dev))
108                 goto err_disable;
109
110         if (ipoib_ib_dev_up(dev))
111                 goto err_stop;
112
113         if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
114                 struct ipoib_dev_priv *cpriv;
115
116                 /* Bring up any child interfaces too */
117                 mutex_lock(&priv->vlan_mutex);
118                 list_for_each_entry(cpriv, &priv->child_intfs, list) {
119                         int flags;
120
121                         flags = cpriv->dev->flags;
122                         if (flags & IFF_UP)
123                                 continue;
124
125                         dev_change_flags(cpriv->dev, flags | IFF_UP);
126                 }
127                 mutex_unlock(&priv->vlan_mutex);
128         }
129
130         netif_start_queue(dev);
131
132         return 0;
133
134 err_stop:
135         ipoib_ib_dev_stop(dev, 1);
136
137 err_disable:
138         clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
139
140         return -EINVAL;
141 }
142
143 static int ipoib_stop(struct net_device *dev)
144 {
145         struct ipoib_dev_priv *priv = netdev_priv(dev);
146
147         ipoib_dbg(priv, "stopping interface\n");
148
149         clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
150
151         netif_stop_queue(dev);
152
153         ipoib_ib_dev_down(dev, 1);
154         ipoib_ib_dev_stop(dev, 0);
155
156         if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
157                 struct ipoib_dev_priv *cpriv;
158
159                 /* Bring down any child interfaces too */
160                 mutex_lock(&priv->vlan_mutex);
161                 list_for_each_entry(cpriv, &priv->child_intfs, list) {
162                         int flags;
163
164                         flags = cpriv->dev->flags;
165                         if (!(flags & IFF_UP))
166                                 continue;
167
168                         dev_change_flags(cpriv->dev, flags & ~IFF_UP);
169                 }
170                 mutex_unlock(&priv->vlan_mutex);
171         }
172
173         return 0;
174 }
175
176 static void ipoib_uninit(struct net_device *dev)
177 {
178         ipoib_dev_cleanup(dev);
179 }
180
181 static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
182 {
183         struct ipoib_dev_priv *priv = netdev_priv(dev);
184
185         if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
186                 features &= ~(NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
187
188         return features;
189 }
190
191 static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
192 {
193         struct ipoib_dev_priv *priv = netdev_priv(dev);
194
195         /* dev->mtu > 2K ==> connected mode */
196         if (ipoib_cm_admin_enabled(dev)) {
197                 if (new_mtu > ipoib_cm_max_mtu(dev))
198                         return -EINVAL;
199
200                 if (new_mtu > priv->mcast_mtu)
201                         ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
202                                    priv->mcast_mtu);
203
204                 dev->mtu = new_mtu;
205                 return 0;
206         }
207
208         if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
209                 return -EINVAL;
210
211         priv->admin_mtu = new_mtu;
212
213         dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
214
215         return 0;
216 }
217
218 static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
219 {
220         struct ipoib_dev_priv *priv = netdev_priv(dev);
221         struct rb_node *n = priv->path_tree.rb_node;
222         struct ipoib_path *path;
223         int ret;
224
225         while (n) {
226                 path = rb_entry(n, struct ipoib_path, rb_node);
227
228                 ret = memcmp(gid, path->pathrec.dgid.raw,
229                              sizeof (union ib_gid));
230
231                 if (ret < 0)
232                         n = n->rb_left;
233                 else if (ret > 0)
234                         n = n->rb_right;
235                 else
236                         return path;
237         }
238
239         return NULL;
240 }
241
242 static int __path_add(struct net_device *dev, struct ipoib_path *path)
243 {
244         struct ipoib_dev_priv *priv = netdev_priv(dev);
245         struct rb_node **n = &priv->path_tree.rb_node;
246         struct rb_node *pn = NULL;
247         struct ipoib_path *tpath;
248         int ret;
249
250         while (*n) {
251                 pn = *n;
252                 tpath = rb_entry(pn, struct ipoib_path, rb_node);
253
254                 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
255                              sizeof (union ib_gid));
256                 if (ret < 0)
257                         n = &pn->rb_left;
258                 else if (ret > 0)
259                         n = &pn->rb_right;
260                 else
261                         return -EEXIST;
262         }
263
264         rb_link_node(&path->rb_node, pn, n);
265         rb_insert_color(&path->rb_node, &priv->path_tree);
266
267         list_add_tail(&path->list, &priv->path_list);
268
269         return 0;
270 }
271
272 static void path_free(struct net_device *dev, struct ipoib_path *path)
273 {
274         struct sk_buff *skb;
275
276         while ((skb = __skb_dequeue(&path->queue)))
277                 dev_kfree_skb_irq(skb);
278
279         ipoib_dbg(netdev_priv(dev), "path_free\n");
280
281         /* remove all neigh connected to this path */
282         ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
283
284         if (path->ah)
285                 ipoib_put_ah(path->ah);
286
287         kfree(path);
288 }
289
290 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
291
292 struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
293 {
294         struct ipoib_path_iter *iter;
295
296         iter = kmalloc(sizeof *iter, GFP_KERNEL);
297         if (!iter)
298                 return NULL;
299
300         iter->dev = dev;
301         memset(iter->path.pathrec.dgid.raw, 0, 16);
302
303         if (ipoib_path_iter_next(iter)) {
304                 kfree(iter);
305                 return NULL;
306         }
307
308         return iter;
309 }
310
311 int ipoib_path_iter_next(struct ipoib_path_iter *iter)
312 {
313         struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
314         struct rb_node *n;
315         struct ipoib_path *path;
316         int ret = 1;
317
318         spin_lock_irq(&priv->lock);
319
320         n = rb_first(&priv->path_tree);
321
322         while (n) {
323                 path = rb_entry(n, struct ipoib_path, rb_node);
324
325                 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
326                            sizeof (union ib_gid)) < 0) {
327                         iter->path = *path;
328                         ret = 0;
329                         break;
330                 }
331
332                 n = rb_next(n);
333         }
334
335         spin_unlock_irq(&priv->lock);
336
337         return ret;
338 }
339
340 void ipoib_path_iter_read(struct ipoib_path_iter *iter,
341                           struct ipoib_path *path)
342 {
343         *path = iter->path;
344 }
345
346 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
347
348 void ipoib_mark_paths_invalid(struct net_device *dev)
349 {
350         struct ipoib_dev_priv *priv = netdev_priv(dev);
351         struct ipoib_path *path, *tp;
352
353         spin_lock_irq(&priv->lock);
354
355         list_for_each_entry_safe(path, tp, &priv->path_list, list) {
356                 ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
357                         be16_to_cpu(path->pathrec.dlid),
358                         path->pathrec.dgid.raw);
359                 path->valid =  0;
360         }
361
362         spin_unlock_irq(&priv->lock);
363 }
364
365 void ipoib_flush_paths(struct net_device *dev)
366 {
367         struct ipoib_dev_priv *priv = netdev_priv(dev);
368         struct ipoib_path *path, *tp;
369         LIST_HEAD(remove_list);
370         unsigned long flags;
371
372         netif_tx_lock_bh(dev);
373         spin_lock_irqsave(&priv->lock, flags);
374
375         list_splice_init(&priv->path_list, &remove_list);
376
377         list_for_each_entry(path, &remove_list, list)
378                 rb_erase(&path->rb_node, &priv->path_tree);
379
380         list_for_each_entry_safe(path, tp, &remove_list, list) {
381                 if (path->query)
382                         ib_sa_cancel_query(path->query_id, path->query);
383                 spin_unlock_irqrestore(&priv->lock, flags);
384                 netif_tx_unlock_bh(dev);
385                 wait_for_completion(&path->done);
386                 path_free(dev, path);
387                 netif_tx_lock_bh(dev);
388                 spin_lock_irqsave(&priv->lock, flags);
389         }
390
391         spin_unlock_irqrestore(&priv->lock, flags);
392         netif_tx_unlock_bh(dev);
393 }
394
395 static void path_rec_completion(int status,
396                                 struct ib_sa_path_rec *pathrec,
397                                 void *path_ptr)
398 {
399         struct ipoib_path *path = path_ptr;
400         struct net_device *dev = path->dev;
401         struct ipoib_dev_priv *priv = netdev_priv(dev);
402         struct ipoib_ah *ah = NULL;
403         struct ipoib_ah *old_ah = NULL;
404         struct ipoib_neigh *neigh, *tn;
405         struct sk_buff_head skqueue;
406         struct sk_buff *skb;
407         unsigned long flags;
408
409         if (!status)
410                 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
411                           be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
412         else
413                 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
414                           status, path->pathrec.dgid.raw);
415
416         skb_queue_head_init(&skqueue);
417
418         if (!status) {
419                 struct ib_ah_attr av;
420
421                 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
422                         ah = ipoib_create_ah(dev, priv->pd, &av);
423         }
424
425         spin_lock_irqsave(&priv->lock, flags);
426
427         if (!IS_ERR_OR_NULL(ah)) {
428                 path->pathrec = *pathrec;
429
430                 old_ah   = path->ah;
431                 path->ah = ah;
432
433                 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
434                           ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
435
436                 while ((skb = __skb_dequeue(&path->queue)))
437                         __skb_queue_tail(&skqueue, skb);
438
439                 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
440                         if (neigh->ah) {
441                                 WARN_ON(neigh->ah != old_ah);
442                                 /*
443                                  * Dropping the ah reference inside
444                                  * priv->lock is safe here, because we
445                                  * will hold one more reference from
446                                  * the original value of path->ah (ie
447                                  * old_ah).
448                                  */
449                                 ipoib_put_ah(neigh->ah);
450                         }
451                         kref_get(&path->ah->ref);
452                         neigh->ah = path->ah;
453
454                         if (ipoib_cm_enabled(dev, neigh->daddr)) {
455                                 if (!ipoib_cm_get(neigh))
456                                         ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
457                                                                                path,
458                                                                                neigh));
459                                 if (!ipoib_cm_get(neigh)) {
460                                         list_del(&neigh->list);
461                                         ipoib_neigh_free(neigh);
462                                         continue;
463                                 }
464                         }
465
466                         while ((skb = __skb_dequeue(&neigh->queue)))
467                                 __skb_queue_tail(&skqueue, skb);
468                 }
469                 path->valid = 1;
470         }
471
472         path->query = NULL;
473         complete(&path->done);
474
475         spin_unlock_irqrestore(&priv->lock, flags);
476
477         if (old_ah)
478                 ipoib_put_ah(old_ah);
479
480         while ((skb = __skb_dequeue(&skqueue))) {
481                 skb->dev = dev;
482                 if (dev_queue_xmit(skb))
483                         ipoib_warn(priv, "dev_queue_xmit failed "
484                                    "to requeue packet\n");
485         }
486 }
487
488 static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
489 {
490         struct ipoib_dev_priv *priv = netdev_priv(dev);
491         struct ipoib_path *path;
492
493         if (!priv->broadcast)
494                 return NULL;
495
496         path = kzalloc(sizeof *path, GFP_ATOMIC);
497         if (!path)
498                 return NULL;
499
500         path->dev = dev;
501
502         skb_queue_head_init(&path->queue);
503
504         INIT_LIST_HEAD(&path->neigh_list);
505
506         memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
507         path->pathrec.sgid          = priv->local_gid;
508         path->pathrec.pkey          = cpu_to_be16(priv->pkey);
509         path->pathrec.numb_path     = 1;
510         path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
511
512         return path;
513 }
514
515 static int path_rec_start(struct net_device *dev,
516                           struct ipoib_path *path)
517 {
518         struct ipoib_dev_priv *priv = netdev_priv(dev);
519
520         ipoib_dbg(priv, "Start path record lookup for %pI6\n",
521                   path->pathrec.dgid.raw);
522
523         init_completion(&path->done);
524
525         path->query_id =
526                 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
527                                    &path->pathrec,
528                                    IB_SA_PATH_REC_DGID          |
529                                    IB_SA_PATH_REC_SGID          |
530                                    IB_SA_PATH_REC_NUMB_PATH     |
531                                    IB_SA_PATH_REC_TRAFFIC_CLASS |
532                                    IB_SA_PATH_REC_PKEY,
533                                    1000, GFP_ATOMIC,
534                                    path_rec_completion,
535                                    path, &path->query);
536         if (path->query_id < 0) {
537                 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
538                 path->query = NULL;
539                 complete(&path->done);
540                 return path->query_id;
541         }
542
543         return 0;
544 }
545
546 static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
547                            struct net_device *dev)
548 {
549         struct ipoib_dev_priv *priv = netdev_priv(dev);
550         struct ipoib_path *path;
551         struct ipoib_neigh *neigh;
552         unsigned long flags;
553
554         spin_lock_irqsave(&priv->lock, flags);
555         neigh = ipoib_neigh_alloc(daddr, dev);
556         if (!neigh) {
557                 spin_unlock_irqrestore(&priv->lock, flags);
558                 ++dev->stats.tx_dropped;
559                 dev_kfree_skb_any(skb);
560                 return;
561         }
562
563         path = __path_find(dev, daddr + 4);
564         if (!path) {
565                 path = path_rec_create(dev, daddr + 4);
566                 if (!path)
567                         goto err_path;
568
569                 __path_add(dev, path);
570         }
571
572         list_add_tail(&neigh->list, &path->neigh_list);
573
574         if (path->ah) {
575                 kref_get(&path->ah->ref);
576                 neigh->ah = path->ah;
577
578                 if (ipoib_cm_enabled(dev, neigh->daddr)) {
579                         if (!ipoib_cm_get(neigh))
580                                 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
581                         if (!ipoib_cm_get(neigh)) {
582                                 list_del(&neigh->list);
583                                 ipoib_neigh_free(neigh);
584                                 goto err_drop;
585                         }
586                         if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
587                                 __skb_queue_tail(&neigh->queue, skb);
588                         else {
589                                 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
590                                            skb_queue_len(&neigh->queue));
591                                 goto err_drop;
592                         }
593                 } else {
594                         spin_unlock_irqrestore(&priv->lock, flags);
595                         ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
596                         ipoib_neigh_put(neigh);
597                         return;
598                 }
599         } else {
600                 neigh->ah  = NULL;
601
602                 if (!path->query && path_rec_start(dev, path))
603                         goto err_list;
604
605                 __skb_queue_tail(&neigh->queue, skb);
606         }
607
608         spin_unlock_irqrestore(&priv->lock, flags);
609         ipoib_neigh_put(neigh);
610         return;
611
612 err_list:
613         list_del(&neigh->list);
614
615 err_path:
616         ipoib_neigh_free(neigh);
617 err_drop:
618         ++dev->stats.tx_dropped;
619         dev_kfree_skb_any(skb);
620
621         spin_unlock_irqrestore(&priv->lock, flags);
622         ipoib_neigh_put(neigh);
623 }
624
625 static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
626                              struct ipoib_cb *cb)
627 {
628         struct ipoib_dev_priv *priv = netdev_priv(dev);
629         struct ipoib_path *path;
630         unsigned long flags;
631
632         spin_lock_irqsave(&priv->lock, flags);
633
634         path = __path_find(dev, cb->hwaddr + 4);
635         if (!path || !path->valid) {
636                 int new_path = 0;
637
638                 if (!path) {
639                         path = path_rec_create(dev, cb->hwaddr + 4);
640                         new_path = 1;
641                 }
642                 if (path) {
643                         __skb_queue_tail(&path->queue, skb);
644
645                         if (!path->query && path_rec_start(dev, path)) {
646                                 spin_unlock_irqrestore(&priv->lock, flags);
647                                 if (new_path)
648                                         path_free(dev, path);
649                                 return;
650                         } else
651                                 __path_add(dev, path);
652                 } else {
653                         ++dev->stats.tx_dropped;
654                         dev_kfree_skb_any(skb);
655                 }
656
657                 spin_unlock_irqrestore(&priv->lock, flags);
658                 return;
659         }
660
661         if (path->ah) {
662                 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
663                           be16_to_cpu(path->pathrec.dlid));
664
665                 spin_unlock_irqrestore(&priv->lock, flags);
666                 ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr));
667                 return;
668         } else if ((path->query || !path_rec_start(dev, path)) &&
669                    skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
670                 __skb_queue_tail(&path->queue, skb);
671         } else {
672                 ++dev->stats.tx_dropped;
673                 dev_kfree_skb_any(skb);
674         }
675
676         spin_unlock_irqrestore(&priv->lock, flags);
677 }
678
679 static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
680 {
681         struct ipoib_dev_priv *priv = netdev_priv(dev);
682         struct ipoib_neigh *neigh;
683         struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
684         struct ipoib_header *header;
685         unsigned long flags;
686
687         header = (struct ipoib_header *) skb->data;
688
689         if (unlikely(cb->hwaddr[4] == 0xff)) {
690                 /* multicast, arrange "if" according to probability */
691                 if ((header->proto != htons(ETH_P_IP)) &&
692                     (header->proto != htons(ETH_P_IPV6)) &&
693                     (header->proto != htons(ETH_P_ARP)) &&
694                     (header->proto != htons(ETH_P_RARP))) {
695                         /* ethertype not supported by IPoIB */
696                         ++dev->stats.tx_dropped;
697                         dev_kfree_skb_any(skb);
698                         return NETDEV_TX_OK;
699                 }
700                 /* Add in the P_Key for multicast*/
701                 cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
702                 cb->hwaddr[9] = priv->pkey & 0xff;
703
704                 neigh = ipoib_neigh_get(dev, cb->hwaddr);
705                 if (likely(neigh))
706                         goto send_using_neigh;
707                 ipoib_mcast_send(dev, cb->hwaddr, skb);
708                 return NETDEV_TX_OK;
709         }
710
711         /* unicast, arrange "switch" according to probability */
712         switch (header->proto) {
713         case htons(ETH_P_IP):
714         case htons(ETH_P_IPV6):
715                 neigh = ipoib_neigh_get(dev, cb->hwaddr);
716                 if (unlikely(!neigh)) {
717                         neigh_add_path(skb, cb->hwaddr, dev);
718                         return NETDEV_TX_OK;
719                 }
720                 break;
721         case htons(ETH_P_ARP):
722         case htons(ETH_P_RARP):
723                 /* for unicast ARP and RARP should always perform path find */
724                 unicast_arp_send(skb, dev, cb);
725                 return NETDEV_TX_OK;
726         default:
727                 /* ethertype not supported by IPoIB */
728                 ++dev->stats.tx_dropped;
729                 dev_kfree_skb_any(skb);
730                 return NETDEV_TX_OK;
731         }
732
733 send_using_neigh:
734         /* note we now hold a ref to neigh */
735         if (ipoib_cm_get(neigh)) {
736                 if (ipoib_cm_up(neigh)) {
737                         ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
738                         goto unref;
739                 }
740         } else if (neigh->ah) {
741                 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
742                 goto unref;
743         }
744
745         if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
746                 spin_lock_irqsave(&priv->lock, flags);
747                 __skb_queue_tail(&neigh->queue, skb);
748                 spin_unlock_irqrestore(&priv->lock, flags);
749         } else {
750                 ++dev->stats.tx_dropped;
751                 dev_kfree_skb_any(skb);
752         }
753
754 unref:
755         ipoib_neigh_put(neigh);
756
757         return NETDEV_TX_OK;
758 }
759
760 static void ipoib_timeout(struct net_device *dev)
761 {
762         struct ipoib_dev_priv *priv = netdev_priv(dev);
763
764         ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
765                    jiffies_to_msecs(jiffies - dev->trans_start));
766         ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
767                    netif_queue_stopped(dev),
768                    priv->tx_head, priv->tx_tail);
769         /* XXX reset QP, etc. */
770 }
771
772 static int ipoib_hard_header(struct sk_buff *skb,
773                              struct net_device *dev,
774                              unsigned short type,
775                              const void *daddr, const void *saddr, unsigned len)
776 {
777         struct ipoib_header *header;
778         struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
779
780         header = (struct ipoib_header *) skb_push(skb, sizeof *header);
781
782         header->proto = htons(type);
783         header->reserved = 0;
784
785         /*
786          * we don't rely on dst_entry structure,  always stuff the
787          * destination address into skb->cb so we can figure out where
788          * to send the packet later.
789          */
790         memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
791
792         return 0;
793 }
794
795 static void ipoib_set_mcast_list(struct net_device *dev)
796 {
797         struct ipoib_dev_priv *priv = netdev_priv(dev);
798
799         if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
800                 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
801                 return;
802         }
803
804         queue_work(ipoib_workqueue, &priv->restart_task);
805 }
806
807 static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
808 {
809         /*
810          * Use only the address parts that contributes to spreading
811          * The subnet prefix is not used as one can not connect to
812          * same remote port (GUID) using the same remote QPN via two
813          * different subnets.
814          */
815          /* qpn octets[1:4) & port GUID octets[12:20) */
816         u32 *daddr_32 = (u32 *) daddr;
817         u32 hv;
818
819         hv = jhash_3words(daddr_32[3], daddr_32[4], 0xFFFFFF & daddr_32[0], 0);
820         return hv & htbl->mask;
821 }
822
823 struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
824 {
825         struct ipoib_dev_priv *priv = netdev_priv(dev);
826         struct ipoib_neigh_table *ntbl = &priv->ntbl;
827         struct ipoib_neigh_hash *htbl;
828         struct ipoib_neigh *neigh = NULL;
829         u32 hash_val;
830
831         rcu_read_lock_bh();
832
833         htbl = rcu_dereference_bh(ntbl->htbl);
834
835         if (!htbl)
836                 goto out_unlock;
837
838         hash_val = ipoib_addr_hash(htbl, daddr);
839         for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
840              neigh != NULL;
841              neigh = rcu_dereference_bh(neigh->hnext)) {
842                 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
843                         /* found, take one ref on behalf of the caller */
844                         if (!atomic_inc_not_zero(&neigh->refcnt)) {
845                                 /* deleted */
846                                 neigh = NULL;
847                                 goto out_unlock;
848                         }
849                         neigh->alive = jiffies;
850                         goto out_unlock;
851                 }
852         }
853
854 out_unlock:
855         rcu_read_unlock_bh();
856         return neigh;
857 }
858
859 static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
860 {
861         struct ipoib_neigh_table *ntbl = &priv->ntbl;
862         struct ipoib_neigh_hash *htbl;
863         unsigned long neigh_obsolete;
864         unsigned long dt;
865         unsigned long flags;
866         int i;
867
868         if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
869                 return;
870
871         spin_lock_irqsave(&priv->lock, flags);
872
873         htbl = rcu_dereference_protected(ntbl->htbl,
874                                          lockdep_is_held(&priv->lock));
875
876         if (!htbl)
877                 goto out_unlock;
878
879         /* neigh is obsolete if it was idle for two GC periods */
880         dt = 2 * arp_tbl.gc_interval;
881         neigh_obsolete = jiffies - dt;
882         /* handle possible race condition */
883         if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
884                 goto out_unlock;
885
886         for (i = 0; i < htbl->size; i++) {
887                 struct ipoib_neigh *neigh;
888                 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
889
890                 while ((neigh = rcu_dereference_protected(*np,
891                                                           lockdep_is_held(&priv->lock))) != NULL) {
892                         /* was the neigh idle for two GC periods */
893                         if (time_after(neigh_obsolete, neigh->alive)) {
894                                 rcu_assign_pointer(*np,
895                                                    rcu_dereference_protected(neigh->hnext,
896                                                                              lockdep_is_held(&priv->lock)));
897                                 /* remove from path/mc list */
898                                 list_del(&neigh->list);
899                                 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
900                         } else {
901                                 np = &neigh->hnext;
902                         }
903
904                 }
905         }
906
907 out_unlock:
908         spin_unlock_irqrestore(&priv->lock, flags);
909 }
910
911 static void ipoib_reap_neigh(struct work_struct *work)
912 {
913         struct ipoib_dev_priv *priv =
914                 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
915
916         __ipoib_reap_neigh(priv);
917
918         if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
919                 queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
920                                    arp_tbl.gc_interval);
921 }
922
923
924 static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
925                                       struct net_device *dev)
926 {
927         struct ipoib_neigh *neigh;
928
929         neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
930         if (!neigh)
931                 return NULL;
932
933         neigh->dev = dev;
934         memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
935         skb_queue_head_init(&neigh->queue);
936         INIT_LIST_HEAD(&neigh->list);
937         ipoib_cm_set(neigh, NULL);
938         /* one ref on behalf of the caller */
939         atomic_set(&neigh->refcnt, 1);
940
941         return neigh;
942 }
943
944 struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
945                                       struct net_device *dev)
946 {
947         struct ipoib_dev_priv *priv = netdev_priv(dev);
948         struct ipoib_neigh_table *ntbl = &priv->ntbl;
949         struct ipoib_neigh_hash *htbl;
950         struct ipoib_neigh *neigh;
951         u32 hash_val;
952
953         htbl = rcu_dereference_protected(ntbl->htbl,
954                                          lockdep_is_held(&priv->lock));
955         if (!htbl) {
956                 neigh = NULL;
957                 goto out_unlock;
958         }
959
960         /* need to add a new neigh, but maybe some other thread succeeded?
961          * recalc hash, maybe hash resize took place so we do a search
962          */
963         hash_val = ipoib_addr_hash(htbl, daddr);
964         for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
965                                                lockdep_is_held(&priv->lock));
966              neigh != NULL;
967              neigh = rcu_dereference_protected(neigh->hnext,
968                                                lockdep_is_held(&priv->lock))) {
969                 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
970                         /* found, take one ref on behalf of the caller */
971                         if (!atomic_inc_not_zero(&neigh->refcnt)) {
972                                 /* deleted */
973                                 neigh = NULL;
974                                 break;
975                         }
976                         neigh->alive = jiffies;
977                         goto out_unlock;
978                 }
979         }
980
981         neigh = ipoib_neigh_ctor(daddr, dev);
982         if (!neigh)
983                 goto out_unlock;
984
985         /* one ref on behalf of the hash table */
986         atomic_inc(&neigh->refcnt);
987         neigh->alive = jiffies;
988         /* put in hash */
989         rcu_assign_pointer(neigh->hnext,
990                            rcu_dereference_protected(htbl->buckets[hash_val],
991                                                      lockdep_is_held(&priv->lock)));
992         rcu_assign_pointer(htbl->buckets[hash_val], neigh);
993         atomic_inc(&ntbl->entries);
994
995 out_unlock:
996
997         return neigh;
998 }
999
1000 void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1001 {
1002         /* neigh reference count was dropprd to zero */
1003         struct net_device *dev = neigh->dev;
1004         struct ipoib_dev_priv *priv = netdev_priv(dev);
1005         struct sk_buff *skb;
1006         if (neigh->ah)
1007                 ipoib_put_ah(neigh->ah);
1008         while ((skb = __skb_dequeue(&neigh->queue))) {
1009                 ++dev->stats.tx_dropped;
1010                 dev_kfree_skb_any(skb);
1011         }
1012         if (ipoib_cm_get(neigh))
1013                 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
1014         ipoib_dbg(netdev_priv(dev),
1015                   "neigh free for %06x %pI6\n",
1016                   IPOIB_QPN(neigh->daddr),
1017                   neigh->daddr + 4);
1018         kfree(neigh);
1019         if (atomic_dec_and_test(&priv->ntbl.entries)) {
1020                 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1021                         complete(&priv->ntbl.flushed);
1022         }
1023 }
1024
1025 static void ipoib_neigh_reclaim(struct rcu_head *rp)
1026 {
1027         /* Called as a result of removal from hash table */
1028         struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1029         /* note TX context may hold another ref */
1030         ipoib_neigh_put(neigh);
1031 }
1032
1033 void ipoib_neigh_free(struct ipoib_neigh *neigh)
1034 {
1035         struct net_device *dev = neigh->dev;
1036         struct ipoib_dev_priv *priv = netdev_priv(dev);
1037         struct ipoib_neigh_table *ntbl = &priv->ntbl;
1038         struct ipoib_neigh_hash *htbl;
1039         struct ipoib_neigh __rcu **np;
1040         struct ipoib_neigh *n;
1041         u32 hash_val;
1042
1043         htbl = rcu_dereference_protected(ntbl->htbl,
1044                                         lockdep_is_held(&priv->lock));
1045         if (!htbl)
1046                 return;
1047
1048         hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1049         np = &htbl->buckets[hash_val];
1050         for (n = rcu_dereference_protected(*np,
1051                                             lockdep_is_held(&priv->lock));
1052              n != NULL;
1053              n = rcu_dereference_protected(*np,
1054                                         lockdep_is_held(&priv->lock))) {
1055                 if (n == neigh) {
1056                         /* found */
1057                         rcu_assign_pointer(*np,
1058                                            rcu_dereference_protected(neigh->hnext,
1059                                                                      lockdep_is_held(&priv->lock)));
1060                         call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1061                         return;
1062                 } else {
1063                         np = &n->hnext;
1064                 }
1065         }
1066 }
1067
1068 static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1069 {
1070         struct ipoib_neigh_table *ntbl = &priv->ntbl;
1071         struct ipoib_neigh_hash *htbl;
1072         struct ipoib_neigh **buckets;
1073         u32 size;
1074
1075         clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1076         ntbl->htbl = NULL;
1077         htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1078         if (!htbl)
1079                 return -ENOMEM;
1080         set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1081         size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1082         buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1083         if (!buckets) {
1084                 kfree(htbl);
1085                 return -ENOMEM;
1086         }
1087         htbl->size = size;
1088         htbl->mask = (size - 1);
1089         htbl->buckets = buckets;
1090         ntbl->htbl = htbl;
1091         htbl->ntbl = ntbl;
1092         atomic_set(&ntbl->entries, 0);
1093
1094         /* start garbage collection */
1095         clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1096         queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
1097                            arp_tbl.gc_interval);
1098
1099         return 0;
1100 }
1101
1102 static void neigh_hash_free_rcu(struct rcu_head *head)
1103 {
1104         struct ipoib_neigh_hash *htbl = container_of(head,
1105                                                     struct ipoib_neigh_hash,
1106                                                     rcu);
1107         struct ipoib_neigh __rcu **buckets = htbl->buckets;
1108         struct ipoib_neigh_table *ntbl = htbl->ntbl;
1109
1110         kfree(buckets);
1111         kfree(htbl);
1112         complete(&ntbl->deleted);
1113 }
1114
1115 void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1116 {
1117         struct ipoib_dev_priv *priv = netdev_priv(dev);
1118         struct ipoib_neigh_table *ntbl = &priv->ntbl;
1119         struct ipoib_neigh_hash *htbl;
1120         unsigned long flags;
1121         int i;
1122
1123         /* remove all neigh connected to a given path or mcast */
1124         spin_lock_irqsave(&priv->lock, flags);
1125
1126         htbl = rcu_dereference_protected(ntbl->htbl,
1127                                          lockdep_is_held(&priv->lock));
1128
1129         if (!htbl)
1130                 goto out_unlock;
1131
1132         for (i = 0; i < htbl->size; i++) {
1133                 struct ipoib_neigh *neigh;
1134                 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1135
1136                 while ((neigh = rcu_dereference_protected(*np,
1137                                                           lockdep_is_held(&priv->lock))) != NULL) {
1138                         /* delete neighs belong to this parent */
1139                         if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1140                                 rcu_assign_pointer(*np,
1141                                                    rcu_dereference_protected(neigh->hnext,
1142                                                                              lockdep_is_held(&priv->lock)));
1143                                 /* remove from parent list */
1144                                 list_del(&neigh->list);
1145                                 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1146                         } else {
1147                                 np = &neigh->hnext;
1148                         }
1149
1150                 }
1151         }
1152 out_unlock:
1153         spin_unlock_irqrestore(&priv->lock, flags);
1154 }
1155
1156 static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1157 {
1158         struct ipoib_neigh_table *ntbl = &priv->ntbl;
1159         struct ipoib_neigh_hash *htbl;
1160         unsigned long flags;
1161         int i, wait_flushed = 0;
1162
1163         init_completion(&priv->ntbl.flushed);
1164
1165         spin_lock_irqsave(&priv->lock, flags);
1166
1167         htbl = rcu_dereference_protected(ntbl->htbl,
1168                                         lockdep_is_held(&priv->lock));
1169         if (!htbl)
1170                 goto out_unlock;
1171
1172         wait_flushed = atomic_read(&priv->ntbl.entries);
1173         if (!wait_flushed)
1174                 goto free_htbl;
1175
1176         for (i = 0; i < htbl->size; i++) {
1177                 struct ipoib_neigh *neigh;
1178                 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1179
1180                 while ((neigh = rcu_dereference_protected(*np,
1181                                        lockdep_is_held(&priv->lock))) != NULL) {
1182                         rcu_assign_pointer(*np,
1183                                            rcu_dereference_protected(neigh->hnext,
1184                                                                      lockdep_is_held(&priv->lock)));
1185                         /* remove from path/mc list */
1186                         list_del(&neigh->list);
1187                         call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1188                 }
1189         }
1190
1191 free_htbl:
1192         rcu_assign_pointer(ntbl->htbl, NULL);
1193         call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1194
1195 out_unlock:
1196         spin_unlock_irqrestore(&priv->lock, flags);
1197         if (wait_flushed)
1198                 wait_for_completion(&priv->ntbl.flushed);
1199 }
1200
1201 static void ipoib_neigh_hash_uninit(struct net_device *dev)
1202 {
1203         struct ipoib_dev_priv *priv = netdev_priv(dev);
1204         int stopped;
1205
1206         ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
1207         init_completion(&priv->ntbl.deleted);
1208         set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1209
1210         /* Stop GC if called at init fail need to cancel work */
1211         stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1212         if (!stopped)
1213                 cancel_delayed_work(&priv->neigh_reap_task);
1214
1215         ipoib_flush_neighs(priv);
1216
1217         wait_for_completion(&priv->ntbl.deleted);
1218 }
1219
1220
1221 int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1222 {
1223         struct ipoib_dev_priv *priv = netdev_priv(dev);
1224
1225         if (ipoib_neigh_hash_init(priv) < 0)
1226                 goto out;
1227         /* Allocate RX/TX "rings" to hold queued skbs */
1228         priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
1229                                 GFP_KERNEL);
1230         if (!priv->rx_ring) {
1231                 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
1232                        ca->name, ipoib_recvq_size);
1233                 goto out_neigh_hash_cleanup;
1234         }
1235
1236         priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
1237         if (!priv->tx_ring) {
1238                 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
1239                        ca->name, ipoib_sendq_size);
1240                 goto out_rx_ring_cleanup;
1241         }
1242
1243         /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
1244
1245         if (ipoib_ib_dev_init(dev, ca, port))
1246                 goto out_tx_ring_cleanup;
1247
1248         return 0;
1249
1250 out_tx_ring_cleanup:
1251         vfree(priv->tx_ring);
1252
1253 out_rx_ring_cleanup:
1254         kfree(priv->rx_ring);
1255
1256 out_neigh_hash_cleanup:
1257         ipoib_neigh_hash_uninit(dev);
1258 out:
1259         return -ENOMEM;
1260 }
1261
1262 void ipoib_dev_cleanup(struct net_device *dev)
1263 {
1264         struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
1265         LIST_HEAD(head);
1266
1267         ASSERT_RTNL();
1268
1269         ipoib_delete_debug_files(dev);
1270
1271         /* Delete any child interfaces first */
1272         list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
1273                 /* Stop GC on child */
1274                 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1275                 cancel_delayed_work(&cpriv->neigh_reap_task);
1276                 unregister_netdevice_queue(cpriv->dev, &head);
1277         }
1278         unregister_netdevice_many(&head);
1279
1280         ipoib_ib_dev_cleanup(dev);
1281
1282         kfree(priv->rx_ring);
1283         vfree(priv->tx_ring);
1284
1285         priv->rx_ring = NULL;
1286         priv->tx_ring = NULL;
1287
1288         ipoib_neigh_hash_uninit(dev);
1289 }
1290
1291 static const struct header_ops ipoib_header_ops = {
1292         .create = ipoib_hard_header,
1293 };
1294
1295 static const struct net_device_ops ipoib_netdev_ops = {
1296         .ndo_uninit              = ipoib_uninit,
1297         .ndo_open                = ipoib_open,
1298         .ndo_stop                = ipoib_stop,
1299         .ndo_change_mtu          = ipoib_change_mtu,
1300         .ndo_fix_features        = ipoib_fix_features,
1301         .ndo_start_xmit          = ipoib_start_xmit,
1302         .ndo_tx_timeout          = ipoib_timeout,
1303         .ndo_set_rx_mode         = ipoib_set_mcast_list,
1304 };
1305
1306 void ipoib_setup(struct net_device *dev)
1307 {
1308         struct ipoib_dev_priv *priv = netdev_priv(dev);
1309
1310         dev->netdev_ops          = &ipoib_netdev_ops;
1311         dev->header_ops          = &ipoib_header_ops;
1312
1313         ipoib_set_ethtool_ops(dev);
1314
1315         netif_napi_add(dev, &priv->napi, ipoib_poll, 100);
1316
1317         dev->watchdog_timeo      = HZ;
1318
1319         dev->flags              |= IFF_BROADCAST | IFF_MULTICAST;
1320
1321         dev->hard_header_len     = IPOIB_ENCAP_LEN;
1322         dev->addr_len            = INFINIBAND_ALEN;
1323         dev->type                = ARPHRD_INFINIBAND;
1324         dev->tx_queue_len        = ipoib_sendq_size * 2;
1325         dev->features            = (NETIF_F_VLAN_CHALLENGED     |
1326                                     NETIF_F_HIGHDMA);
1327         dev->priv_flags         &= ~IFF_XMIT_DST_RELEASE;
1328
1329         memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
1330
1331         netif_carrier_off(dev);
1332
1333         priv->dev = dev;
1334
1335         spin_lock_init(&priv->lock);
1336
1337         mutex_init(&priv->vlan_mutex);
1338
1339         INIT_LIST_HEAD(&priv->path_list);
1340         INIT_LIST_HEAD(&priv->child_intfs);
1341         INIT_LIST_HEAD(&priv->dead_ahs);
1342         INIT_LIST_HEAD(&priv->multicast_list);
1343
1344         INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
1345         INIT_DELAYED_WORK(&priv->mcast_task,   ipoib_mcast_join_task);
1346         INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
1347         INIT_WORK(&priv->flush_light,   ipoib_ib_dev_flush_light);
1348         INIT_WORK(&priv->flush_normal,   ipoib_ib_dev_flush_normal);
1349         INIT_WORK(&priv->flush_heavy,   ipoib_ib_dev_flush_heavy);
1350         INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1351         INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
1352         INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
1353 }
1354
1355 struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
1356 {
1357         struct net_device *dev;
1358
1359         dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
1360                            ipoib_setup);
1361         if (!dev)
1362                 return NULL;
1363
1364         return netdev_priv(dev);
1365 }
1366
1367 static ssize_t show_pkey(struct device *dev,
1368                          struct device_attribute *attr, char *buf)
1369 {
1370         struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1371
1372         return sprintf(buf, "0x%04x\n", priv->pkey);
1373 }
1374 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1375
1376 static ssize_t show_umcast(struct device *dev,
1377                            struct device_attribute *attr, char *buf)
1378 {
1379         struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1380
1381         return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1382 }
1383
1384 void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
1385 {
1386         struct ipoib_dev_priv *priv = netdev_priv(ndev);
1387
1388         if (umcast_val > 0) {
1389                 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1390                 ipoib_warn(priv, "ignoring multicast groups joined directly "
1391                                 "by userspace\n");
1392         } else
1393                 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1394 }
1395
1396 static ssize_t set_umcast(struct device *dev,
1397                           struct device_attribute *attr,
1398                           const char *buf, size_t count)
1399 {
1400         unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1401
1402         ipoib_set_umcast(to_net_dev(dev), umcast_val);
1403
1404         return count;
1405 }
1406 static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1407
1408 int ipoib_add_umcast_attr(struct net_device *dev)
1409 {
1410         return device_create_file(&dev->dev, &dev_attr_umcast);
1411 }
1412
1413 static ssize_t create_child(struct device *dev,
1414                             struct device_attribute *attr,
1415                             const char *buf, size_t count)
1416 {
1417         int pkey;
1418         int ret;
1419
1420         if (sscanf(buf, "%i", &pkey) != 1)
1421                 return -EINVAL;
1422
1423         if (pkey < 0 || pkey > 0xffff)
1424                 return -EINVAL;
1425
1426         /*
1427          * Set the full membership bit, so that we join the right
1428          * broadcast group, etc.
1429          */
1430         pkey |= 0x8000;
1431
1432         ret = ipoib_vlan_add(to_net_dev(dev), pkey);
1433
1434         return ret ? ret : count;
1435 }
1436 static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
1437
1438 static ssize_t delete_child(struct device *dev,
1439                             struct device_attribute *attr,
1440                             const char *buf, size_t count)
1441 {
1442         int pkey;
1443         int ret;
1444
1445         if (sscanf(buf, "%i", &pkey) != 1)
1446                 return -EINVAL;
1447
1448         if (pkey < 0 || pkey > 0xffff)
1449                 return -EINVAL;
1450
1451         ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
1452
1453         return ret ? ret : count;
1454
1455 }
1456 static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
1457
1458 int ipoib_add_pkey_attr(struct net_device *dev)
1459 {
1460         return device_create_file(&dev->dev, &dev_attr_pkey);
1461 }
1462
1463 int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
1464 {
1465         struct ib_device_attr *device_attr;
1466         int result = -ENOMEM;
1467
1468         device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
1469         if (!device_attr) {
1470                 printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
1471                        hca->name, sizeof *device_attr);
1472                 return result;
1473         }
1474
1475         result = ib_query_device(hca, device_attr);
1476         if (result) {
1477                 printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
1478                        hca->name, result);
1479                 kfree(device_attr);
1480                 return result;
1481         }
1482         priv->hca_caps = device_attr->device_cap_flags;
1483
1484         kfree(device_attr);
1485
1486         if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
1487                 priv->dev->hw_features = NETIF_F_SG |
1488                         NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1489
1490                 if (priv->hca_caps & IB_DEVICE_UD_TSO)
1491                         priv->dev->hw_features |= NETIF_F_TSO;
1492
1493                 priv->dev->features |= priv->dev->hw_features;
1494         }
1495
1496         return 0;
1497 }
1498
1499 static struct net_device *ipoib_add_port(const char *format,
1500                                          struct ib_device *hca, u8 port)
1501 {
1502         struct ipoib_dev_priv *priv;
1503         struct ib_port_attr attr;
1504         int result = -ENOMEM;
1505
1506         priv = ipoib_intf_alloc(format);
1507         if (!priv)
1508                 goto alloc_mem_failed;
1509
1510         SET_NETDEV_DEV(priv->dev, hca->dma_device);
1511         priv->dev->dev_id = port - 1;
1512
1513         if (!ib_query_port(hca, port, &attr))
1514                 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1515         else {
1516                 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
1517                        hca->name, port);
1518                 goto device_init_failed;
1519         }
1520
1521         /* MTU will be reset when mcast join happens */
1522         priv->dev->mtu  = IPOIB_UD_MTU(priv->max_ib_mtu);
1523         priv->mcast_mtu  = priv->admin_mtu = priv->dev->mtu;
1524
1525         priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
1526
1527         result = ib_query_pkey(hca, port, 0, &priv->pkey);
1528         if (result) {
1529                 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1530                        hca->name, port, result);
1531                 goto device_init_failed;
1532         }
1533
1534         if (ipoib_set_dev_features(priv, hca))
1535                 goto device_init_failed;
1536
1537         /*
1538          * Set the full membership bit, so that we join the right
1539          * broadcast group, etc.
1540          */
1541         priv->pkey |= 0x8000;
1542
1543         priv->dev->broadcast[8] = priv->pkey >> 8;
1544         priv->dev->broadcast[9] = priv->pkey & 0xff;
1545
1546         result = ib_query_gid(hca, port, 0, &priv->local_gid);
1547         if (result) {
1548                 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1549                        hca->name, port, result);
1550                 goto device_init_failed;
1551         } else
1552                 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1553
1554         result = ipoib_dev_init(priv->dev, hca, port);
1555         if (result < 0) {
1556                 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1557                        hca->name, port, result);
1558                 goto device_init_failed;
1559         }
1560
1561         INIT_IB_EVENT_HANDLER(&priv->event_handler,
1562                               priv->ca, ipoib_event);
1563         result = ib_register_event_handler(&priv->event_handler);
1564         if (result < 0) {
1565                 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1566                        "port %d (ret = %d)\n",
1567                        hca->name, port, result);
1568                 goto event_failed;
1569         }
1570
1571         result = register_netdev(priv->dev);
1572         if (result) {
1573                 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1574                        hca->name, port, result);
1575                 goto register_failed;
1576         }
1577
1578         ipoib_create_debug_files(priv->dev);
1579
1580         if (ipoib_cm_add_mode_attr(priv->dev))
1581                 goto sysfs_failed;
1582         if (ipoib_add_pkey_attr(priv->dev))
1583                 goto sysfs_failed;
1584         if (ipoib_add_umcast_attr(priv->dev))
1585                 goto sysfs_failed;
1586         if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
1587                 goto sysfs_failed;
1588         if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
1589                 goto sysfs_failed;
1590
1591         return priv->dev;
1592
1593 sysfs_failed:
1594         ipoib_delete_debug_files(priv->dev);
1595         unregister_netdev(priv->dev);
1596
1597 register_failed:
1598         ib_unregister_event_handler(&priv->event_handler);
1599         /* Stop GC if started before flush */
1600         set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1601         cancel_delayed_work(&priv->neigh_reap_task);
1602         flush_workqueue(ipoib_workqueue);
1603
1604 event_failed:
1605         ipoib_dev_cleanup(priv->dev);
1606
1607 device_init_failed:
1608         free_netdev(priv->dev);
1609
1610 alloc_mem_failed:
1611         return ERR_PTR(result);
1612 }
1613
1614 static void ipoib_add_one(struct ib_device *device)
1615 {
1616         struct list_head *dev_list;
1617         struct net_device *dev;
1618         struct ipoib_dev_priv *priv;
1619         int s, e, p;
1620
1621         if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1622                 return;
1623
1624         dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1625         if (!dev_list)
1626                 return;
1627
1628         INIT_LIST_HEAD(dev_list);
1629
1630         if (device->node_type == RDMA_NODE_IB_SWITCH) {
1631                 s = 0;
1632                 e = 0;
1633         } else {
1634                 s = 1;
1635                 e = device->phys_port_cnt;
1636         }
1637
1638         for (p = s; p <= e; ++p) {
1639                 if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
1640                         continue;
1641                 dev = ipoib_add_port("ib%d", device, p);
1642                 if (!IS_ERR(dev)) {
1643                         priv = netdev_priv(dev);
1644                         list_add_tail(&priv->list, dev_list);
1645                 }
1646         }
1647
1648         ib_set_client_data(device, &ipoib_client, dev_list);
1649 }
1650
1651 static void ipoib_remove_one(struct ib_device *device)
1652 {
1653         struct ipoib_dev_priv *priv, *tmp;
1654         struct list_head *dev_list;
1655
1656         if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1657                 return;
1658
1659         dev_list = ib_get_client_data(device, &ipoib_client);
1660
1661         list_for_each_entry_safe(priv, tmp, dev_list, list) {
1662                 ib_unregister_event_handler(&priv->event_handler);
1663
1664                 rtnl_lock();
1665                 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
1666                 rtnl_unlock();
1667
1668                 /* Stop GC */
1669                 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1670                 cancel_delayed_work(&priv->neigh_reap_task);
1671                 flush_workqueue(ipoib_workqueue);
1672
1673                 unregister_netdev(priv->dev);
1674                 free_netdev(priv->dev);
1675         }
1676
1677         kfree(dev_list);
1678 }
1679
1680 static int __init ipoib_init_module(void)
1681 {
1682         int ret;
1683
1684         ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1685         ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1686         ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1687
1688         ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1689         ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1690         ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
1691 #ifdef CONFIG_INFINIBAND_IPOIB_CM
1692         ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1693 #endif
1694
1695         /*
1696          * When copying small received packets, we only copy from the
1697          * linear data part of the SKB, so we rely on this condition.
1698          */
1699         BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
1700
1701         ret = ipoib_register_debugfs();
1702         if (ret)
1703                 return ret;
1704
1705         /*
1706          * We create our own workqueue mainly because we want to be
1707          * able to flush it when devices are being removed.  We can't
1708          * use schedule_work()/flush_scheduled_work() because both
1709          * unregister_netdev() and linkwatch_event take the rtnl lock,
1710          * so flush_scheduled_work() can deadlock during device
1711          * removal.
1712          */
1713         ipoib_workqueue = create_singlethread_workqueue("ipoib");
1714         if (!ipoib_workqueue) {
1715                 ret = -ENOMEM;
1716                 goto err_fs;
1717         }
1718
1719         ib_sa_register_client(&ipoib_sa_client);
1720
1721         ret = ib_register_client(&ipoib_client);
1722         if (ret)
1723                 goto err_sa;
1724
1725         ret = ipoib_netlink_init();
1726         if (ret)
1727                 goto err_client;
1728
1729         return 0;
1730
1731 err_client:
1732         ib_unregister_client(&ipoib_client);
1733
1734 err_sa:
1735         ib_sa_unregister_client(&ipoib_sa_client);
1736         destroy_workqueue(ipoib_workqueue);
1737
1738 err_fs:
1739         ipoib_unregister_debugfs();
1740
1741         return ret;
1742 }
1743
1744 static void __exit ipoib_cleanup_module(void)
1745 {
1746         ipoib_netlink_fini();
1747         ib_unregister_client(&ipoib_client);
1748         ib_sa_unregister_client(&ipoib_sa_client);
1749         ipoib_unregister_debugfs();
1750         destroy_workqueue(ipoib_workqueue);
1751 }
1752
1753 module_init(ipoib_init_module);
1754 module_exit(ipoib_cleanup_module);