GNU Linux-libre 4.14.259-gnu1
[releases.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 #include <net/addrconf.h>
52 #include <linux/inetdevice.h>
53 #include <rdma/ib_cache.h>
54 #include <linux/pci.h>
55
56 #define DRV_VERSION "1.0.0"
57
58 const char ipoib_driver_version[] = DRV_VERSION;
59
60 MODULE_AUTHOR("Roland Dreier");
61 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
62 MODULE_LICENSE("Dual BSD/GPL");
63
64 int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
65 int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
66
67 module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
68 MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
69 module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
70 MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
71
72 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
73 int ipoib_debug_level;
74
75 module_param_named(debug_level, ipoib_debug_level, int, 0644);
76 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
77 #endif
78
79 struct ipoib_path_iter {
80         struct net_device *dev;
81         struct ipoib_path  path;
82 };
83
84 static const u8 ipv4_bcast_addr[] = {
85         0x00, 0xff, 0xff, 0xff,
86         0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
87         0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
88 };
89
90 struct workqueue_struct *ipoib_workqueue;
91
92 struct ib_sa_client ipoib_sa_client;
93
94 static void ipoib_add_one(struct ib_device *device);
95 static void ipoib_remove_one(struct ib_device *device, void *client_data);
96 static void ipoib_neigh_reclaim(struct rcu_head *rp);
97 static struct net_device *ipoib_get_net_dev_by_params(
98                 struct ib_device *dev, u8 port, u16 pkey,
99                 const union ib_gid *gid, const struct sockaddr *addr,
100                 void *client_data);
101 static int ipoib_set_mac(struct net_device *dev, void *addr);
102 static int ipoib_ioctl(struct net_device *dev, struct ifreq *ifr,
103                        int cmd);
104
105 static struct ib_client ipoib_client = {
106         .name   = "ipoib",
107         .add    = ipoib_add_one,
108         .remove = ipoib_remove_one,
109         .get_net_dev_by_params = ipoib_get_net_dev_by_params,
110 };
111
112 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
113 static int ipoib_netdev_event(struct notifier_block *this,
114                               unsigned long event, void *ptr)
115 {
116         struct netdev_notifier_info *ni = ptr;
117         struct net_device *dev = ni->dev;
118
119         if (dev->netdev_ops->ndo_open != ipoib_open)
120                 return NOTIFY_DONE;
121
122         switch (event) {
123         case NETDEV_REGISTER:
124                 ipoib_create_debug_files(dev);
125                 break;
126         case NETDEV_CHANGENAME:
127                 ipoib_delete_debug_files(dev);
128                 ipoib_create_debug_files(dev);
129                 break;
130         case NETDEV_UNREGISTER:
131                 ipoib_delete_debug_files(dev);
132                 break;
133         }
134
135         return NOTIFY_DONE;
136 }
137 #endif
138
139 int ipoib_open(struct net_device *dev)
140 {
141         struct ipoib_dev_priv *priv = ipoib_priv(dev);
142
143         ipoib_dbg(priv, "bringing up interface\n");
144
145         netif_carrier_off(dev);
146
147         set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
148
149         priv->sm_fullmember_sendonly_support = false;
150
151         if (ipoib_ib_dev_open(dev)) {
152                 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
153                         return 0;
154                 goto err_disable;
155         }
156
157         ipoib_ib_dev_up(dev);
158
159         if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
160                 struct ipoib_dev_priv *cpriv;
161
162                 /* Bring up any child interfaces too */
163                 down_read(&priv->vlan_rwsem);
164                 list_for_each_entry(cpriv, &priv->child_intfs, list) {
165                         int flags;
166
167                         flags = cpriv->dev->flags;
168                         if (flags & IFF_UP)
169                                 continue;
170
171                         dev_change_flags(cpriv->dev, flags | IFF_UP);
172                 }
173                 up_read(&priv->vlan_rwsem);
174         }
175
176         netif_start_queue(dev);
177
178         return 0;
179
180 err_disable:
181         clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
182
183         return -EINVAL;
184 }
185
186 static int ipoib_stop(struct net_device *dev)
187 {
188         struct ipoib_dev_priv *priv = ipoib_priv(dev);
189
190         ipoib_dbg(priv, "stopping interface\n");
191
192         clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
193
194         netif_stop_queue(dev);
195
196         ipoib_ib_dev_down(dev);
197         ipoib_ib_dev_stop(dev);
198
199         if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
200                 struct ipoib_dev_priv *cpriv;
201
202                 /* Bring down any child interfaces too */
203                 down_read(&priv->vlan_rwsem);
204                 list_for_each_entry(cpriv, &priv->child_intfs, list) {
205                         int flags;
206
207                         flags = cpriv->dev->flags;
208                         if (!(flags & IFF_UP))
209                                 continue;
210
211                         dev_change_flags(cpriv->dev, flags & ~IFF_UP);
212                 }
213                 up_read(&priv->vlan_rwsem);
214         }
215
216         return 0;
217 }
218
219 static void ipoib_uninit(struct net_device *dev)
220 {
221         ipoib_dev_cleanup(dev);
222 }
223
224 static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
225 {
226         struct ipoib_dev_priv *priv = ipoib_priv(dev);
227
228         if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
229                 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
230
231         return features;
232 }
233
234 static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
235 {
236         struct ipoib_dev_priv *priv = ipoib_priv(dev);
237         int ret = 0;
238
239         /* dev->mtu > 2K ==> connected mode */
240         if (ipoib_cm_admin_enabled(dev)) {
241                 if (new_mtu > ipoib_cm_max_mtu(dev))
242                         return -EINVAL;
243
244                 if (new_mtu > priv->mcast_mtu)
245                         ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
246                                    priv->mcast_mtu);
247
248                 dev->mtu = new_mtu;
249                 return 0;
250         }
251
252         if (new_mtu < (ETH_MIN_MTU + IPOIB_ENCAP_LEN) ||
253             new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
254                 return -EINVAL;
255
256         priv->admin_mtu = new_mtu;
257
258         if (priv->mcast_mtu < priv->admin_mtu)
259                 ipoib_dbg(priv, "MTU must be smaller than the underlying "
260                                 "link layer MTU - 4 (%u)\n", priv->mcast_mtu);
261
262         new_mtu = min(priv->mcast_mtu, priv->admin_mtu);
263
264         if (priv->rn_ops->ndo_change_mtu) {
265                 bool carrier_status = netif_carrier_ok(dev);
266
267                 netif_carrier_off(dev);
268
269                 /* notify lower level on the real mtu */
270                 ret = priv->rn_ops->ndo_change_mtu(dev, new_mtu);
271
272                 if (carrier_status)
273                         netif_carrier_on(dev);
274         } else {
275                 dev->mtu = new_mtu;
276         }
277
278         return ret;
279 }
280
281 static void ipoib_get_stats(struct net_device *dev,
282                             struct rtnl_link_stats64 *stats)
283 {
284         struct ipoib_dev_priv *priv = ipoib_priv(dev);
285
286         if (priv->rn_ops->ndo_get_stats64)
287                 priv->rn_ops->ndo_get_stats64(dev, stats);
288         else
289                 netdev_stats_to_stats64(stats, &dev->stats);
290 }
291
292 /* Called with an RCU read lock taken */
293 static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr,
294                                         struct net_device *dev)
295 {
296         struct net *net = dev_net(dev);
297         struct in_device *in_dev;
298         struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
299         struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
300         __be32 ret_addr;
301
302         switch (addr->sa_family) {
303         case AF_INET:
304                 in_dev = in_dev_get(dev);
305                 if (!in_dev)
306                         return false;
307
308                 ret_addr = inet_confirm_addr(net, in_dev, 0,
309                                              addr_in->sin_addr.s_addr,
310                                              RT_SCOPE_HOST);
311                 in_dev_put(in_dev);
312                 if (ret_addr)
313                         return true;
314
315                 break;
316         case AF_INET6:
317                 if (IS_ENABLED(CONFIG_IPV6) &&
318                     ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1))
319                         return true;
320
321                 break;
322         }
323         return false;
324 }
325
326 /**
327  * Find the master net_device on top of the given net_device.
328  * @dev: base IPoIB net_device
329  *
330  * Returns the master net_device with a reference held, or the same net_device
331  * if no master exists.
332  */
333 static struct net_device *ipoib_get_master_net_dev(struct net_device *dev)
334 {
335         struct net_device *master;
336
337         rcu_read_lock();
338         master = netdev_master_upper_dev_get_rcu(dev);
339         if (master)
340                 dev_hold(master);
341         rcu_read_unlock();
342
343         if (master)
344                 return master;
345
346         dev_hold(dev);
347         return dev;
348 }
349
350 struct ipoib_walk_data {
351         const struct sockaddr *addr;
352         struct net_device *result;
353 };
354
355 static int ipoib_upper_walk(struct net_device *upper, void *_data)
356 {
357         struct ipoib_walk_data *data = _data;
358         int ret = 0;
359
360         if (ipoib_is_dev_match_addr_rcu(data->addr, upper)) {
361                 dev_hold(upper);
362                 data->result = upper;
363                 ret = 1;
364         }
365
366         return ret;
367 }
368
369 /**
370  * Find a net_device matching the given address, which is an upper device of
371  * the given net_device.
372  * @addr: IP address to look for.
373  * @dev: base IPoIB net_device
374  *
375  * If found, returns the net_device with a reference held. Otherwise return
376  * NULL.
377  */
378 static struct net_device *ipoib_get_net_dev_match_addr(
379                 const struct sockaddr *addr, struct net_device *dev)
380 {
381         struct ipoib_walk_data data = {
382                 .addr = addr,
383         };
384
385         rcu_read_lock();
386         if (ipoib_is_dev_match_addr_rcu(addr, dev)) {
387                 dev_hold(dev);
388                 data.result = dev;
389                 goto out;
390         }
391
392         netdev_walk_all_upper_dev_rcu(dev, ipoib_upper_walk, &data);
393 out:
394         rcu_read_unlock();
395         return data.result;
396 }
397
398 /* returns the number of IPoIB netdevs on top a given ipoib device matching a
399  * pkey_index and address, if one exists.
400  *
401  * @found_net_dev: contains a matching net_device if the return value >= 1,
402  * with a reference held. */
403 static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
404                                      const union ib_gid *gid,
405                                      u16 pkey_index,
406                                      const struct sockaddr *addr,
407                                      int nesting,
408                                      struct net_device **found_net_dev)
409 {
410         struct ipoib_dev_priv *child_priv;
411         struct net_device *net_dev = NULL;
412         int matches = 0;
413
414         if (priv->pkey_index == pkey_index &&
415             (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
416                 if (!addr) {
417                         net_dev = ipoib_get_master_net_dev(priv->dev);
418                 } else {
419                         /* Verify the net_device matches the IP address, as
420                          * IPoIB child devices currently share a GID. */
421                         net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev);
422                 }
423                 if (net_dev) {
424                         if (!*found_net_dev)
425                                 *found_net_dev = net_dev;
426                         else
427                                 dev_put(net_dev);
428                         ++matches;
429                 }
430         }
431
432         /* Check child interfaces */
433         down_read_nested(&priv->vlan_rwsem, nesting);
434         list_for_each_entry(child_priv, &priv->child_intfs, list) {
435                 matches += ipoib_match_gid_pkey_addr(child_priv, gid,
436                                                     pkey_index, addr,
437                                                     nesting + 1,
438                                                     found_net_dev);
439                 if (matches > 1)
440                         break;
441         }
442         up_read(&priv->vlan_rwsem);
443
444         return matches;
445 }
446
447 /* Returns the number of matching net_devs found (between 0 and 2). Also
448  * return the matching net_device in the @net_dev parameter, holding a
449  * reference to the net_device, if the number of matches >= 1 */
450 static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port,
451                                          u16 pkey_index,
452                                          const union ib_gid *gid,
453                                          const struct sockaddr *addr,
454                                          struct net_device **net_dev)
455 {
456         struct ipoib_dev_priv *priv;
457         int matches = 0;
458
459         *net_dev = NULL;
460
461         list_for_each_entry(priv, dev_list, list) {
462                 if (priv->port != port)
463                         continue;
464
465                 matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
466                                                      addr, 0, net_dev);
467                 if (matches > 1)
468                         break;
469         }
470
471         return matches;
472 }
473
474 static struct net_device *ipoib_get_net_dev_by_params(
475                 struct ib_device *dev, u8 port, u16 pkey,
476                 const union ib_gid *gid, const struct sockaddr *addr,
477                 void *client_data)
478 {
479         struct net_device *net_dev;
480         struct list_head *dev_list = client_data;
481         u16 pkey_index;
482         int matches;
483         int ret;
484
485         if (!rdma_protocol_ib(dev, port))
486                 return NULL;
487
488         ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
489         if (ret)
490                 return NULL;
491
492         if (!dev_list)
493                 return NULL;
494
495         /* See if we can find a unique device matching the L2 parameters */
496         matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
497                                                 gid, NULL, &net_dev);
498
499         switch (matches) {
500         case 0:
501                 return NULL;
502         case 1:
503                 return net_dev;
504         }
505
506         dev_put(net_dev);
507
508         /* Couldn't find a unique device with L2 parameters only. Use L3
509          * address to uniquely match the net device */
510         matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
511                                                 gid, addr, &net_dev);
512         switch (matches) {
513         case 0:
514                 return NULL;
515         default:
516                 dev_warn_ratelimited(&dev->dev,
517                                      "duplicate IP address detected\n");
518                 /* Fall through */
519         case 1:
520                 return net_dev;
521         }
522 }
523
524 int ipoib_set_mode(struct net_device *dev, const char *buf)
525 {
526         struct ipoib_dev_priv *priv = ipoib_priv(dev);
527
528         if ((test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags) &&
529              !strcmp(buf, "connected\n")) ||
530              (!test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags) &&
531              !strcmp(buf, "datagram\n"))) {
532                 return 0;
533         }
534
535         /* flush paths if we switch modes so that connections are restarted */
536         if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
537                 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
538                 ipoib_warn(priv, "enabling connected mode "
539                            "will cause multicast packet drops\n");
540                 netdev_update_features(dev);
541                 dev_set_mtu(dev, ipoib_cm_max_mtu(dev));
542                 rtnl_unlock();
543                 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
544
545                 ipoib_flush_paths(dev);
546                 return (!rtnl_trylock()) ? -EBUSY : 0;
547         }
548
549         if (!strcmp(buf, "datagram\n")) {
550                 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
551                 netdev_update_features(dev);
552                 dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
553                 rtnl_unlock();
554                 ipoib_flush_paths(dev);
555                 return (!rtnl_trylock()) ? -EBUSY : 0;
556         }
557
558         return -EINVAL;
559 }
560
561 struct ipoib_path *__path_find(struct net_device *dev, void *gid)
562 {
563         struct ipoib_dev_priv *priv = ipoib_priv(dev);
564         struct rb_node *n = priv->path_tree.rb_node;
565         struct ipoib_path *path;
566         int ret;
567
568         while (n) {
569                 path = rb_entry(n, struct ipoib_path, rb_node);
570
571                 ret = memcmp(gid, path->pathrec.dgid.raw,
572                              sizeof (union ib_gid));
573
574                 if (ret < 0)
575                         n = n->rb_left;
576                 else if (ret > 0)
577                         n = n->rb_right;
578                 else
579                         return path;
580         }
581
582         return NULL;
583 }
584
585 static int __path_add(struct net_device *dev, struct ipoib_path *path)
586 {
587         struct ipoib_dev_priv *priv = ipoib_priv(dev);
588         struct rb_node **n = &priv->path_tree.rb_node;
589         struct rb_node *pn = NULL;
590         struct ipoib_path *tpath;
591         int ret;
592
593         while (*n) {
594                 pn = *n;
595                 tpath = rb_entry(pn, struct ipoib_path, rb_node);
596
597                 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
598                              sizeof (union ib_gid));
599                 if (ret < 0)
600                         n = &pn->rb_left;
601                 else if (ret > 0)
602                         n = &pn->rb_right;
603                 else
604                         return -EEXIST;
605         }
606
607         rb_link_node(&path->rb_node, pn, n);
608         rb_insert_color(&path->rb_node, &priv->path_tree);
609
610         list_add_tail(&path->list, &priv->path_list);
611
612         return 0;
613 }
614
615 static void path_free(struct net_device *dev, struct ipoib_path *path)
616 {
617         struct sk_buff *skb;
618
619         while ((skb = __skb_dequeue(&path->queue)))
620                 dev_kfree_skb_irq(skb);
621
622         ipoib_dbg(ipoib_priv(dev), "path_free\n");
623
624         /* remove all neigh connected to this path */
625         ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
626
627         if (path->ah)
628                 ipoib_put_ah(path->ah);
629
630         kfree(path);
631 }
632
633 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
634
635 struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
636 {
637         struct ipoib_path_iter *iter;
638
639         iter = kmalloc(sizeof *iter, GFP_KERNEL);
640         if (!iter)
641                 return NULL;
642
643         iter->dev = dev;
644         memset(iter->path.pathrec.dgid.raw, 0, 16);
645
646         if (ipoib_path_iter_next(iter)) {
647                 kfree(iter);
648                 return NULL;
649         }
650
651         return iter;
652 }
653
654 int ipoib_path_iter_next(struct ipoib_path_iter *iter)
655 {
656         struct ipoib_dev_priv *priv = ipoib_priv(iter->dev);
657         struct rb_node *n;
658         struct ipoib_path *path;
659         int ret = 1;
660
661         spin_lock_irq(&priv->lock);
662
663         n = rb_first(&priv->path_tree);
664
665         while (n) {
666                 path = rb_entry(n, struct ipoib_path, rb_node);
667
668                 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
669                            sizeof (union ib_gid)) < 0) {
670                         iter->path = *path;
671                         ret = 0;
672                         break;
673                 }
674
675                 n = rb_next(n);
676         }
677
678         spin_unlock_irq(&priv->lock);
679
680         return ret;
681 }
682
683 void ipoib_path_iter_read(struct ipoib_path_iter *iter,
684                           struct ipoib_path *path)
685 {
686         *path = iter->path;
687 }
688
689 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
690
691 void ipoib_mark_paths_invalid(struct net_device *dev)
692 {
693         struct ipoib_dev_priv *priv = ipoib_priv(dev);
694         struct ipoib_path *path, *tp;
695
696         spin_lock_irq(&priv->lock);
697
698         list_for_each_entry_safe(path, tp, &priv->path_list, list) {
699                 ipoib_dbg(priv, "mark path LID 0x%08x GID %pI6 invalid\n",
700                           be32_to_cpu(sa_path_get_dlid(&path->pathrec)),
701                           path->pathrec.dgid.raw);
702                 path->valid =  0;
703         }
704
705         spin_unlock_irq(&priv->lock);
706 }
707
708 static void push_pseudo_header(struct sk_buff *skb, const char *daddr)
709 {
710         struct ipoib_pseudo_header *phdr;
711
712         phdr = skb_push(skb, sizeof(*phdr));
713         memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
714 }
715
716 void ipoib_flush_paths(struct net_device *dev)
717 {
718         struct ipoib_dev_priv *priv = ipoib_priv(dev);
719         struct ipoib_path *path, *tp;
720         LIST_HEAD(remove_list);
721         unsigned long flags;
722
723         netif_tx_lock_bh(dev);
724         spin_lock_irqsave(&priv->lock, flags);
725
726         list_splice_init(&priv->path_list, &remove_list);
727
728         list_for_each_entry(path, &remove_list, list)
729                 rb_erase(&path->rb_node, &priv->path_tree);
730
731         list_for_each_entry_safe(path, tp, &remove_list, list) {
732                 if (path->query)
733                         ib_sa_cancel_query(path->query_id, path->query);
734                 spin_unlock_irqrestore(&priv->lock, flags);
735                 netif_tx_unlock_bh(dev);
736                 wait_for_completion(&path->done);
737                 path_free(dev, path);
738                 netif_tx_lock_bh(dev);
739                 spin_lock_irqsave(&priv->lock, flags);
740         }
741
742         spin_unlock_irqrestore(&priv->lock, flags);
743         netif_tx_unlock_bh(dev);
744 }
745
746 static void path_rec_completion(int status,
747                                 struct sa_path_rec *pathrec,
748                                 void *path_ptr)
749 {
750         struct ipoib_path *path = path_ptr;
751         struct net_device *dev = path->dev;
752         struct ipoib_dev_priv *priv = ipoib_priv(dev);
753         struct ipoib_ah *ah = NULL;
754         struct ipoib_ah *old_ah = NULL;
755         struct ipoib_neigh *neigh, *tn;
756         struct sk_buff_head skqueue;
757         struct sk_buff *skb;
758         unsigned long flags;
759
760         if (!status)
761                 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
762                           be32_to_cpu(sa_path_get_dlid(pathrec)),
763                           pathrec->dgid.raw);
764         else
765                 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
766                           status, path->pathrec.dgid.raw);
767
768         skb_queue_head_init(&skqueue);
769
770         if (!status) {
771                 struct rdma_ah_attr av;
772
773                 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
774                         ah = ipoib_create_ah(dev, priv->pd, &av);
775         }
776
777         spin_lock_irqsave(&priv->lock, flags);
778
779         if (!IS_ERR_OR_NULL(ah)) {
780                 /*
781                  * pathrec.dgid is used as the database key from the LLADDR,
782                  * it must remain unchanged even if the SA returns a different
783                  * GID to use in the AH.
784                  */
785                 if (memcmp(pathrec->dgid.raw, path->pathrec.dgid.raw,
786                            sizeof(union ib_gid))) {
787                         ipoib_dbg(
788                                 priv,
789                                 "%s got PathRec for gid %pI6 while asked for %pI6\n",
790                                 dev->name, pathrec->dgid.raw,
791                                 path->pathrec.dgid.raw);
792                         memcpy(pathrec->dgid.raw, path->pathrec.dgid.raw,
793                                sizeof(union ib_gid));
794                 }
795
796                 path->pathrec = *pathrec;
797
798                 old_ah   = path->ah;
799                 path->ah = ah;
800
801                 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
802                           ah, be32_to_cpu(sa_path_get_dlid(pathrec)),
803                           pathrec->sl);
804
805                 while ((skb = __skb_dequeue(&path->queue)))
806                         __skb_queue_tail(&skqueue, skb);
807
808                 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
809                         if (neigh->ah) {
810                                 WARN_ON(neigh->ah != old_ah);
811                                 /*
812                                  * Dropping the ah reference inside
813                                  * priv->lock is safe here, because we
814                                  * will hold one more reference from
815                                  * the original value of path->ah (ie
816                                  * old_ah).
817                                  */
818                                 ipoib_put_ah(neigh->ah);
819                         }
820                         kref_get(&path->ah->ref);
821                         neigh->ah = path->ah;
822
823                         if (ipoib_cm_enabled(dev, neigh->daddr)) {
824                                 if (!ipoib_cm_get(neigh))
825                                         ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
826                                                                                path,
827                                                                                neigh));
828                                 if (!ipoib_cm_get(neigh)) {
829                                         ipoib_neigh_free(neigh);
830                                         continue;
831                                 }
832                         }
833
834                         while ((skb = __skb_dequeue(&neigh->queue)))
835                                 __skb_queue_tail(&skqueue, skb);
836                 }
837                 path->valid = 1;
838         }
839
840         path->query = NULL;
841         complete(&path->done);
842
843         spin_unlock_irqrestore(&priv->lock, flags);
844
845         if (IS_ERR_OR_NULL(ah))
846                 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
847
848         if (old_ah)
849                 ipoib_put_ah(old_ah);
850
851         while ((skb = __skb_dequeue(&skqueue))) {
852                 int ret;
853                 skb->dev = dev;
854                 ret = dev_queue_xmit(skb);
855                 if (ret)
856                         ipoib_warn(priv, "%s: dev_queue_xmit failed to re-queue packet, ret:%d\n",
857                                    __func__, ret);
858         }
859 }
860
861 static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
862 {
863         struct ipoib_dev_priv *priv = ipoib_priv(dev);
864         struct ipoib_path *path;
865
866         if (!priv->broadcast)
867                 return NULL;
868
869         path = kzalloc(sizeof *path, GFP_ATOMIC);
870         if (!path)
871                 return NULL;
872
873         path->dev = dev;
874
875         skb_queue_head_init(&path->queue);
876
877         INIT_LIST_HEAD(&path->neigh_list);
878
879         if (rdma_cap_opa_ah(priv->ca, priv->port))
880                 path->pathrec.rec_type = SA_PATH_REC_TYPE_OPA;
881         else
882                 path->pathrec.rec_type = SA_PATH_REC_TYPE_IB;
883         memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
884         path->pathrec.sgid          = priv->local_gid;
885         path->pathrec.pkey          = cpu_to_be16(priv->pkey);
886         path->pathrec.numb_path     = 1;
887         path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
888
889         return path;
890 }
891
892 static int path_rec_start(struct net_device *dev,
893                           struct ipoib_path *path)
894 {
895         struct ipoib_dev_priv *priv = ipoib_priv(dev);
896
897         ipoib_dbg(priv, "Start path record lookup for %pI6\n",
898                   path->pathrec.dgid.raw);
899
900         init_completion(&path->done);
901
902         path->query_id =
903                 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
904                                    &path->pathrec,
905                                    IB_SA_PATH_REC_DGID          |
906                                    IB_SA_PATH_REC_SGID          |
907                                    IB_SA_PATH_REC_NUMB_PATH     |
908                                    IB_SA_PATH_REC_TRAFFIC_CLASS |
909                                    IB_SA_PATH_REC_PKEY,
910                                    1000, GFP_ATOMIC,
911                                    path_rec_completion,
912                                    path, &path->query);
913         if (path->query_id < 0) {
914                 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
915                 path->query = NULL;
916                 complete(&path->done);
917                 return path->query_id;
918         }
919
920         return 0;
921 }
922
923 static struct ipoib_neigh *neigh_add_path(struct sk_buff *skb, u8 *daddr,
924                                           struct net_device *dev)
925 {
926         struct ipoib_dev_priv *priv = ipoib_priv(dev);
927         struct rdma_netdev *rn = netdev_priv(dev);
928         struct ipoib_path *path;
929         struct ipoib_neigh *neigh;
930         unsigned long flags;
931
932         spin_lock_irqsave(&priv->lock, flags);
933         neigh = ipoib_neigh_alloc(daddr, dev);
934         if (!neigh) {
935                 spin_unlock_irqrestore(&priv->lock, flags);
936                 ++dev->stats.tx_dropped;
937                 dev_kfree_skb_any(skb);
938                 return NULL;
939         }
940
941         /* To avoid race condition, make sure that the
942          * neigh will be added only once.
943          */
944         if (unlikely(!list_empty(&neigh->list))) {
945                 spin_unlock_irqrestore(&priv->lock, flags);
946                 return neigh;
947         }
948
949         path = __path_find(dev, daddr + 4);
950         if (!path) {
951                 path = path_rec_create(dev, daddr + 4);
952                 if (!path)
953                         goto err_path;
954
955                 __path_add(dev, path);
956         }
957
958         list_add_tail(&neigh->list, &path->neigh_list);
959
960         if (path->ah) {
961                 kref_get(&path->ah->ref);
962                 neigh->ah = path->ah;
963
964                 if (ipoib_cm_enabled(dev, neigh->daddr)) {
965                         if (!ipoib_cm_get(neigh))
966                                 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
967                         if (!ipoib_cm_get(neigh)) {
968                                 ipoib_neigh_free(neigh);
969                                 goto err_drop;
970                         }
971                         if (skb_queue_len(&neigh->queue) <
972                             IPOIB_MAX_PATH_REC_QUEUE) {
973                                 push_pseudo_header(skb, neigh->daddr);
974                                 __skb_queue_tail(&neigh->queue, skb);
975                         } else {
976                                 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
977                                            skb_queue_len(&neigh->queue));
978                                 goto err_drop;
979                         }
980                 } else {
981                         spin_unlock_irqrestore(&priv->lock, flags);
982                         path->ah->last_send = rn->send(dev, skb, path->ah->ah,
983                                                        IPOIB_QPN(daddr));
984                         ipoib_neigh_put(neigh);
985                         return NULL;
986                 }
987         } else {
988                 neigh->ah  = NULL;
989
990                 if (!path->query && path_rec_start(dev, path))
991                         goto err_path;
992                 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
993                         push_pseudo_header(skb, neigh->daddr);
994                         __skb_queue_tail(&neigh->queue, skb);
995                 } else {
996                         goto err_drop;
997                 }
998         }
999
1000         spin_unlock_irqrestore(&priv->lock, flags);
1001         ipoib_neigh_put(neigh);
1002         return NULL;
1003
1004 err_path:
1005         ipoib_neigh_free(neigh);
1006 err_drop:
1007         ++dev->stats.tx_dropped;
1008         dev_kfree_skb_any(skb);
1009
1010         spin_unlock_irqrestore(&priv->lock, flags);
1011         ipoib_neigh_put(neigh);
1012
1013         return NULL;
1014 }
1015
1016 static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
1017                              struct ipoib_pseudo_header *phdr)
1018 {
1019         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1020         struct rdma_netdev *rn = netdev_priv(dev);
1021         struct ipoib_path *path;
1022         unsigned long flags;
1023
1024         spin_lock_irqsave(&priv->lock, flags);
1025
1026         path = __path_find(dev, phdr->hwaddr + 4);
1027         if (!path || !path->valid) {
1028                 int new_path = 0;
1029
1030                 if (!path) {
1031                         path = path_rec_create(dev, phdr->hwaddr + 4);
1032                         new_path = 1;
1033                 }
1034                 if (path) {
1035                         if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1036                                 push_pseudo_header(skb, phdr->hwaddr);
1037                                 __skb_queue_tail(&path->queue, skb);
1038                         } else {
1039                                 ++dev->stats.tx_dropped;
1040                                 dev_kfree_skb_any(skb);
1041                         }
1042
1043                         if (!path->query && path_rec_start(dev, path)) {
1044                                 spin_unlock_irqrestore(&priv->lock, flags);
1045                                 if (new_path)
1046                                         path_free(dev, path);
1047                                 return;
1048                         } else
1049                                 __path_add(dev, path);
1050                 } else {
1051                         ++dev->stats.tx_dropped;
1052                         dev_kfree_skb_any(skb);
1053                 }
1054
1055                 spin_unlock_irqrestore(&priv->lock, flags);
1056                 return;
1057         }
1058
1059         if (path->ah) {
1060                 ipoib_dbg(priv, "Send unicast ARP to %08x\n",
1061                           be32_to_cpu(sa_path_get_dlid(&path->pathrec)));
1062
1063                 spin_unlock_irqrestore(&priv->lock, flags);
1064                 path->ah->last_send = rn->send(dev, skb, path->ah->ah,
1065                                                IPOIB_QPN(phdr->hwaddr));
1066                 return;
1067         } else if ((path->query || !path_rec_start(dev, path)) &&
1068                    skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1069                 push_pseudo_header(skb, phdr->hwaddr);
1070                 __skb_queue_tail(&path->queue, skb);
1071         } else {
1072                 ++dev->stats.tx_dropped;
1073                 dev_kfree_skb_any(skb);
1074         }
1075
1076         spin_unlock_irqrestore(&priv->lock, flags);
1077 }
1078
1079 static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
1080 {
1081         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1082         struct rdma_netdev *rn = netdev_priv(dev);
1083         struct ipoib_neigh *neigh;
1084         struct ipoib_pseudo_header *phdr;
1085         struct ipoib_header *header;
1086         unsigned long flags;
1087
1088         phdr = (struct ipoib_pseudo_header *) skb->data;
1089         skb_pull(skb, sizeof(*phdr));
1090         header = (struct ipoib_header *) skb->data;
1091
1092         if (unlikely(phdr->hwaddr[4] == 0xff)) {
1093                 /* multicast, arrange "if" according to probability */
1094                 if ((header->proto != htons(ETH_P_IP)) &&
1095                     (header->proto != htons(ETH_P_IPV6)) &&
1096                     (header->proto != htons(ETH_P_ARP)) &&
1097                     (header->proto != htons(ETH_P_RARP)) &&
1098                     (header->proto != htons(ETH_P_TIPC))) {
1099                         /* ethertype not supported by IPoIB */
1100                         ++dev->stats.tx_dropped;
1101                         dev_kfree_skb_any(skb);
1102                         return NETDEV_TX_OK;
1103                 }
1104                 /* Add in the P_Key for multicast*/
1105                 phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
1106                 phdr->hwaddr[9] = priv->pkey & 0xff;
1107
1108                 neigh = ipoib_neigh_get(dev, phdr->hwaddr);
1109                 if (likely(neigh))
1110                         goto send_using_neigh;
1111                 ipoib_mcast_send(dev, phdr->hwaddr, skb);
1112                 return NETDEV_TX_OK;
1113         }
1114
1115         /* unicast, arrange "switch" according to probability */
1116         switch (header->proto) {
1117         case htons(ETH_P_IP):
1118         case htons(ETH_P_IPV6):
1119         case htons(ETH_P_TIPC):
1120                 neigh = ipoib_neigh_get(dev, phdr->hwaddr);
1121                 if (unlikely(!neigh)) {
1122                         neigh = neigh_add_path(skb, phdr->hwaddr, dev);
1123                         if (likely(!neigh))
1124                                 return NETDEV_TX_OK;
1125                 }
1126                 break;
1127         case htons(ETH_P_ARP):
1128         case htons(ETH_P_RARP):
1129                 /* for unicast ARP and RARP should always perform path find */
1130                 unicast_arp_send(skb, dev, phdr);
1131                 return NETDEV_TX_OK;
1132         default:
1133                 /* ethertype not supported by IPoIB */
1134                 ++dev->stats.tx_dropped;
1135                 dev_kfree_skb_any(skb);
1136                 return NETDEV_TX_OK;
1137         }
1138
1139 send_using_neigh:
1140         /* note we now hold a ref to neigh */
1141         if (ipoib_cm_get(neigh)) {
1142                 if (ipoib_cm_up(neigh)) {
1143                         ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
1144                         goto unref;
1145                 }
1146         } else if (neigh->ah) {
1147                 neigh->ah->last_send = rn->send(dev, skb, neigh->ah->ah,
1148                                                 IPOIB_QPN(phdr->hwaddr));
1149                 goto unref;
1150         }
1151
1152         if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1153                 push_pseudo_header(skb, phdr->hwaddr);
1154                 spin_lock_irqsave(&priv->lock, flags);
1155                 __skb_queue_tail(&neigh->queue, skb);
1156                 spin_unlock_irqrestore(&priv->lock, flags);
1157         } else {
1158                 ++dev->stats.tx_dropped;
1159                 dev_kfree_skb_any(skb);
1160         }
1161
1162 unref:
1163         ipoib_neigh_put(neigh);
1164
1165         return NETDEV_TX_OK;
1166 }
1167
1168 static void ipoib_timeout(struct net_device *dev)
1169 {
1170         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1171
1172         ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
1173                    jiffies_to_msecs(jiffies - dev_trans_start(dev)));
1174         ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
1175                    netif_queue_stopped(dev),
1176                    priv->tx_head, priv->tx_tail);
1177         /* XXX reset QP, etc. */
1178 }
1179
1180 static int ipoib_hard_header(struct sk_buff *skb,
1181                              struct net_device *dev,
1182                              unsigned short type,
1183                              const void *daddr, const void *saddr, unsigned len)
1184 {
1185         struct ipoib_header *header;
1186
1187         header = skb_push(skb, sizeof *header);
1188
1189         header->proto = htons(type);
1190         header->reserved = 0;
1191
1192         /*
1193          * we don't rely on dst_entry structure,  always stuff the
1194          * destination address into skb hard header so we can figure out where
1195          * to send the packet later.
1196          */
1197         push_pseudo_header(skb, daddr);
1198
1199         return IPOIB_HARD_LEN;
1200 }
1201
1202 static void ipoib_set_mcast_list(struct net_device *dev)
1203 {
1204         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1205
1206         if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
1207                 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
1208                 return;
1209         }
1210
1211         queue_work(priv->wq, &priv->restart_task);
1212 }
1213
1214 static int ipoib_get_iflink(const struct net_device *dev)
1215 {
1216         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1217
1218         /* parent interface */
1219         if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
1220                 return dev->ifindex;
1221
1222         /* child/vlan interface */
1223         return priv->parent->ifindex;
1224 }
1225
1226 static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
1227 {
1228         /*
1229          * Use only the address parts that contributes to spreading
1230          * The subnet prefix is not used as one can not connect to
1231          * same remote port (GUID) using the same remote QPN via two
1232          * different subnets.
1233          */
1234          /* qpn octets[1:4) & port GUID octets[12:20) */
1235         u32 *d32 = (u32 *) daddr;
1236         u32 hv;
1237
1238         hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
1239         return hv & htbl->mask;
1240 }
1241
1242 struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
1243 {
1244         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1245         struct ipoib_neigh_table *ntbl = &priv->ntbl;
1246         struct ipoib_neigh_hash *htbl;
1247         struct ipoib_neigh *neigh = NULL;
1248         u32 hash_val;
1249
1250         rcu_read_lock_bh();
1251
1252         htbl = rcu_dereference_bh(ntbl->htbl);
1253
1254         if (!htbl)
1255                 goto out_unlock;
1256
1257         hash_val = ipoib_addr_hash(htbl, daddr);
1258         for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
1259              neigh != NULL;
1260              neigh = rcu_dereference_bh(neigh->hnext)) {
1261                 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1262                         /* found, take one ref on behalf of the caller */
1263                         if (!atomic_inc_not_zero(&neigh->refcnt)) {
1264                                 /* deleted */
1265                                 neigh = NULL;
1266                                 goto out_unlock;
1267                         }
1268
1269                         if (likely(skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE))
1270                                 neigh->alive = jiffies;
1271                         goto out_unlock;
1272                 }
1273         }
1274
1275 out_unlock:
1276         rcu_read_unlock_bh();
1277         return neigh;
1278 }
1279
1280 static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
1281 {
1282         struct ipoib_neigh_table *ntbl = &priv->ntbl;
1283         struct ipoib_neigh_hash *htbl;
1284         unsigned long neigh_obsolete;
1285         unsigned long dt;
1286         unsigned long flags;
1287         int i;
1288         LIST_HEAD(remove_list);
1289
1290         if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1291                 return;
1292
1293         spin_lock_irqsave(&priv->lock, flags);
1294
1295         htbl = rcu_dereference_protected(ntbl->htbl,
1296                                          lockdep_is_held(&priv->lock));
1297
1298         if (!htbl)
1299                 goto out_unlock;
1300
1301         /* neigh is obsolete if it was idle for two GC periods */
1302         dt = 2 * arp_tbl.gc_interval;
1303         neigh_obsolete = jiffies - dt;
1304         /* handle possible race condition */
1305         if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1306                 goto out_unlock;
1307
1308         for (i = 0; i < htbl->size; i++) {
1309                 struct ipoib_neigh *neigh;
1310                 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1311
1312                 while ((neigh = rcu_dereference_protected(*np,
1313                                                           lockdep_is_held(&priv->lock))) != NULL) {
1314                         /* was the neigh idle for two GC periods */
1315                         if (time_after(neigh_obsolete, neigh->alive)) {
1316
1317                                 ipoib_check_and_add_mcast_sendonly(priv, neigh->daddr + 4, &remove_list);
1318
1319                                 rcu_assign_pointer(*np,
1320                                                    rcu_dereference_protected(neigh->hnext,
1321                                                                              lockdep_is_held(&priv->lock)));
1322                                 /* remove from path/mc list */
1323                                 list_del_init(&neigh->list);
1324                                 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1325                         } else {
1326                                 np = &neigh->hnext;
1327                         }
1328
1329                 }
1330         }
1331
1332 out_unlock:
1333         spin_unlock_irqrestore(&priv->lock, flags);
1334         ipoib_mcast_remove_list(&remove_list);
1335 }
1336
1337 static void ipoib_reap_neigh(struct work_struct *work)
1338 {
1339         struct ipoib_dev_priv *priv =
1340                 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
1341
1342         __ipoib_reap_neigh(priv);
1343
1344         if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1345                 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1346                                    arp_tbl.gc_interval);
1347 }
1348
1349
1350 static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
1351                                       struct net_device *dev)
1352 {
1353         struct ipoib_neigh *neigh;
1354
1355         neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
1356         if (!neigh)
1357                 return NULL;
1358
1359         neigh->dev = dev;
1360         memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
1361         skb_queue_head_init(&neigh->queue);
1362         INIT_LIST_HEAD(&neigh->list);
1363         ipoib_cm_set(neigh, NULL);
1364         /* one ref on behalf of the caller */
1365         atomic_set(&neigh->refcnt, 1);
1366
1367         return neigh;
1368 }
1369
1370 struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
1371                                       struct net_device *dev)
1372 {
1373         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1374         struct ipoib_neigh_table *ntbl = &priv->ntbl;
1375         struct ipoib_neigh_hash *htbl;
1376         struct ipoib_neigh *neigh;
1377         u32 hash_val;
1378
1379         htbl = rcu_dereference_protected(ntbl->htbl,
1380                                          lockdep_is_held(&priv->lock));
1381         if (!htbl) {
1382                 neigh = NULL;
1383                 goto out_unlock;
1384         }
1385
1386         /* need to add a new neigh, but maybe some other thread succeeded?
1387          * recalc hash, maybe hash resize took place so we do a search
1388          */
1389         hash_val = ipoib_addr_hash(htbl, daddr);
1390         for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
1391                                                lockdep_is_held(&priv->lock));
1392              neigh != NULL;
1393              neigh = rcu_dereference_protected(neigh->hnext,
1394                                                lockdep_is_held(&priv->lock))) {
1395                 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1396                         /* found, take one ref on behalf of the caller */
1397                         if (!atomic_inc_not_zero(&neigh->refcnt)) {
1398                                 /* deleted */
1399                                 neigh = NULL;
1400                                 break;
1401                         }
1402                         neigh->alive = jiffies;
1403                         goto out_unlock;
1404                 }
1405         }
1406
1407         neigh = ipoib_neigh_ctor(daddr, dev);
1408         if (!neigh)
1409                 goto out_unlock;
1410
1411         /* one ref on behalf of the hash table */
1412         atomic_inc(&neigh->refcnt);
1413         neigh->alive = jiffies;
1414         /* put in hash */
1415         rcu_assign_pointer(neigh->hnext,
1416                            rcu_dereference_protected(htbl->buckets[hash_val],
1417                                                      lockdep_is_held(&priv->lock)));
1418         rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1419         atomic_inc(&ntbl->entries);
1420
1421 out_unlock:
1422
1423         return neigh;
1424 }
1425
1426 void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1427 {
1428         /* neigh reference count was dropprd to zero */
1429         struct net_device *dev = neigh->dev;
1430         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1431         struct sk_buff *skb;
1432         if (neigh->ah)
1433                 ipoib_put_ah(neigh->ah);
1434         while ((skb = __skb_dequeue(&neigh->queue))) {
1435                 ++dev->stats.tx_dropped;
1436                 dev_kfree_skb_any(skb);
1437         }
1438         if (ipoib_cm_get(neigh))
1439                 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
1440         ipoib_dbg(ipoib_priv(dev),
1441                   "neigh free for %06x %pI6\n",
1442                   IPOIB_QPN(neigh->daddr),
1443                   neigh->daddr + 4);
1444         kfree(neigh);
1445         if (atomic_dec_and_test(&priv->ntbl.entries)) {
1446                 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1447                         complete(&priv->ntbl.flushed);
1448         }
1449 }
1450
1451 static void ipoib_neigh_reclaim(struct rcu_head *rp)
1452 {
1453         /* Called as a result of removal from hash table */
1454         struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1455         /* note TX context may hold another ref */
1456         ipoib_neigh_put(neigh);
1457 }
1458
1459 void ipoib_neigh_free(struct ipoib_neigh *neigh)
1460 {
1461         struct net_device *dev = neigh->dev;
1462         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1463         struct ipoib_neigh_table *ntbl = &priv->ntbl;
1464         struct ipoib_neigh_hash *htbl;
1465         struct ipoib_neigh __rcu **np;
1466         struct ipoib_neigh *n;
1467         u32 hash_val;
1468
1469         htbl = rcu_dereference_protected(ntbl->htbl,
1470                                         lockdep_is_held(&priv->lock));
1471         if (!htbl)
1472                 return;
1473
1474         hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1475         np = &htbl->buckets[hash_val];
1476         for (n = rcu_dereference_protected(*np,
1477                                             lockdep_is_held(&priv->lock));
1478              n != NULL;
1479              n = rcu_dereference_protected(*np,
1480                                         lockdep_is_held(&priv->lock))) {
1481                 if (n == neigh) {
1482                         /* found */
1483                         rcu_assign_pointer(*np,
1484                                            rcu_dereference_protected(neigh->hnext,
1485                                                                      lockdep_is_held(&priv->lock)));
1486                         /* remove from parent list */
1487                         list_del_init(&neigh->list);
1488                         call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1489                         return;
1490                 } else {
1491                         np = &n->hnext;
1492                 }
1493         }
1494 }
1495
1496 static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1497 {
1498         struct ipoib_neigh_table *ntbl = &priv->ntbl;
1499         struct ipoib_neigh_hash *htbl;
1500         struct ipoib_neigh __rcu **buckets;
1501         u32 size;
1502
1503         clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1504         ntbl->htbl = NULL;
1505         htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1506         if (!htbl)
1507                 return -ENOMEM;
1508         set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1509         size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1510         buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1511         if (!buckets) {
1512                 kfree(htbl);
1513                 return -ENOMEM;
1514         }
1515         htbl->size = size;
1516         htbl->mask = (size - 1);
1517         htbl->buckets = buckets;
1518         RCU_INIT_POINTER(ntbl->htbl, htbl);
1519         htbl->ntbl = ntbl;
1520         atomic_set(&ntbl->entries, 0);
1521
1522         /* start garbage collection */
1523         clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1524         queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1525                            arp_tbl.gc_interval);
1526
1527         return 0;
1528 }
1529
1530 static void neigh_hash_free_rcu(struct rcu_head *head)
1531 {
1532         struct ipoib_neigh_hash *htbl = container_of(head,
1533                                                     struct ipoib_neigh_hash,
1534                                                     rcu);
1535         struct ipoib_neigh __rcu **buckets = htbl->buckets;
1536         struct ipoib_neigh_table *ntbl = htbl->ntbl;
1537
1538         kfree(buckets);
1539         kfree(htbl);
1540         complete(&ntbl->deleted);
1541 }
1542
1543 void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1544 {
1545         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1546         struct ipoib_neigh_table *ntbl = &priv->ntbl;
1547         struct ipoib_neigh_hash *htbl;
1548         unsigned long flags;
1549         int i;
1550
1551         /* remove all neigh connected to a given path or mcast */
1552         spin_lock_irqsave(&priv->lock, flags);
1553
1554         htbl = rcu_dereference_protected(ntbl->htbl,
1555                                          lockdep_is_held(&priv->lock));
1556
1557         if (!htbl)
1558                 goto out_unlock;
1559
1560         for (i = 0; i < htbl->size; i++) {
1561                 struct ipoib_neigh *neigh;
1562                 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1563
1564                 while ((neigh = rcu_dereference_protected(*np,
1565                                                           lockdep_is_held(&priv->lock))) != NULL) {
1566                         /* delete neighs belong to this parent */
1567                         if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1568                                 rcu_assign_pointer(*np,
1569                                                    rcu_dereference_protected(neigh->hnext,
1570                                                                              lockdep_is_held(&priv->lock)));
1571                                 /* remove from parent list */
1572                                 list_del_init(&neigh->list);
1573                                 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1574                         } else {
1575                                 np = &neigh->hnext;
1576                         }
1577
1578                 }
1579         }
1580 out_unlock:
1581         spin_unlock_irqrestore(&priv->lock, flags);
1582 }
1583
1584 static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1585 {
1586         struct ipoib_neigh_table *ntbl = &priv->ntbl;
1587         struct ipoib_neigh_hash *htbl;
1588         unsigned long flags;
1589         int i, wait_flushed = 0;
1590
1591         init_completion(&priv->ntbl.flushed);
1592         set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1593
1594         spin_lock_irqsave(&priv->lock, flags);
1595
1596         htbl = rcu_dereference_protected(ntbl->htbl,
1597                                         lockdep_is_held(&priv->lock));
1598         if (!htbl)
1599                 goto out_unlock;
1600
1601         wait_flushed = atomic_read(&priv->ntbl.entries);
1602         if (!wait_flushed)
1603                 goto free_htbl;
1604
1605         for (i = 0; i < htbl->size; i++) {
1606                 struct ipoib_neigh *neigh;
1607                 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1608
1609                 while ((neigh = rcu_dereference_protected(*np,
1610                                        lockdep_is_held(&priv->lock))) != NULL) {
1611                         rcu_assign_pointer(*np,
1612                                            rcu_dereference_protected(neigh->hnext,
1613                                                                      lockdep_is_held(&priv->lock)));
1614                         /* remove from path/mc list */
1615                         list_del_init(&neigh->list);
1616                         call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1617                 }
1618         }
1619
1620 free_htbl:
1621         rcu_assign_pointer(ntbl->htbl, NULL);
1622         call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1623
1624 out_unlock:
1625         spin_unlock_irqrestore(&priv->lock, flags);
1626         if (wait_flushed)
1627                 wait_for_completion(&priv->ntbl.flushed);
1628 }
1629
1630 static void ipoib_neigh_hash_uninit(struct net_device *dev)
1631 {
1632         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1633         int stopped;
1634
1635         ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
1636         init_completion(&priv->ntbl.deleted);
1637
1638         /* Stop GC if called at init fail need to cancel work */
1639         stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1640         if (!stopped)
1641                 cancel_delayed_work(&priv->neigh_reap_task);
1642
1643         ipoib_flush_neighs(priv);
1644
1645         wait_for_completion(&priv->ntbl.deleted);
1646 }
1647
1648 static void ipoib_dev_uninit_default(struct net_device *dev)
1649 {
1650         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1651
1652         ipoib_transport_dev_cleanup(dev);
1653
1654         netif_napi_del(&priv->napi);
1655
1656         ipoib_cm_dev_cleanup(dev);
1657
1658         kfree(priv->rx_ring);
1659         vfree(priv->tx_ring);
1660
1661         priv->rx_ring = NULL;
1662         priv->tx_ring = NULL;
1663 }
1664
1665 static int ipoib_dev_init_default(struct net_device *dev)
1666 {
1667         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1668
1669         netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT);
1670
1671         /* Allocate RX/TX "rings" to hold queued skbs */
1672         priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
1673                                 GFP_KERNEL);
1674         if (!priv->rx_ring)
1675                 goto out;
1676
1677         priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
1678         if (!priv->tx_ring) {
1679                 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
1680                        priv->ca->name, ipoib_sendq_size);
1681                 goto out_rx_ring_cleanup;
1682         }
1683
1684         /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
1685
1686         if (ipoib_transport_dev_init(dev, priv->ca)) {
1687                 pr_warn("%s: ipoib_transport_dev_init failed\n",
1688                         priv->ca->name);
1689                 goto out_tx_ring_cleanup;
1690         }
1691
1692         /* after qp created set dev address */
1693         priv->dev->dev_addr[1] = (priv->qp->qp_num >> 16) & 0xff;
1694         priv->dev->dev_addr[2] = (priv->qp->qp_num >>  8) & 0xff;
1695         priv->dev->dev_addr[3] = (priv->qp->qp_num) & 0xff;
1696
1697         setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
1698                     (unsigned long)dev);
1699
1700         return 0;
1701
1702 out_tx_ring_cleanup:
1703         vfree(priv->tx_ring);
1704
1705 out_rx_ring_cleanup:
1706         kfree(priv->rx_ring);
1707
1708 out:
1709         netif_napi_del(&priv->napi);
1710         return -ENOMEM;
1711 }
1712
1713 static int ipoib_ioctl(struct net_device *dev, struct ifreq *ifr,
1714                        int cmd)
1715 {
1716         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1717
1718         if (!priv->rn_ops->ndo_do_ioctl)
1719                 return -EOPNOTSUPP;
1720
1721         return priv->rn_ops->ndo_do_ioctl(dev, ifr, cmd);
1722 }
1723
1724 int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1725 {
1726         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1727         int ret = -ENOMEM;
1728
1729         priv->ca = ca;
1730         priv->port = port;
1731         priv->qp = NULL;
1732
1733         /*
1734          * the various IPoIB tasks assume they will never race against
1735          * themselves, so always use a single thread workqueue
1736          */
1737         priv->wq = alloc_ordered_workqueue("ipoib_wq", WQ_MEM_RECLAIM);
1738         if (!priv->wq) {
1739                 pr_warn("%s: failed to allocate device WQ\n", dev->name);
1740                 goto out;
1741         }
1742
1743         /* create pd, which used both for control and datapath*/
1744         priv->pd = ib_alloc_pd(priv->ca, 0);
1745         if (IS_ERR(priv->pd)) {
1746                 pr_warn("%s: failed to allocate PD\n", ca->name);
1747                 goto clean_wq;
1748         }
1749
1750         ret = priv->rn_ops->ndo_init(dev);
1751         if (ret) {
1752                 pr_warn("%s failed to init HW resource\n", dev->name);
1753                 goto out_free_pd;
1754         }
1755
1756         ret = ipoib_neigh_hash_init(priv);
1757         if (ret) {
1758                 pr_warn("%s failed to init neigh hash\n", dev->name);
1759                 goto out_dev_uninit;
1760         }
1761
1762         if (dev->flags & IFF_UP) {
1763                 if (ipoib_ib_dev_open(dev)) {
1764                         pr_warn("%s failed to open device\n", dev->name);
1765                         ret = -ENODEV;
1766                         goto out_dev_uninit;
1767                 }
1768         }
1769
1770         return 0;
1771
1772 out_dev_uninit:
1773         ipoib_ib_dev_cleanup(dev);
1774
1775 out_free_pd:
1776         if (priv->pd) {
1777                 ib_dealloc_pd(priv->pd);
1778                 priv->pd = NULL;
1779         }
1780
1781 clean_wq:
1782         if (priv->wq) {
1783                 destroy_workqueue(priv->wq);
1784                 priv->wq = NULL;
1785         }
1786
1787 out:
1788         return ret;
1789 }
1790
1791 void ipoib_dev_cleanup(struct net_device *dev)
1792 {
1793         struct ipoib_dev_priv *priv = ipoib_priv(dev), *cpriv, *tcpriv;
1794         LIST_HEAD(head);
1795
1796         ASSERT_RTNL();
1797
1798         /* Delete any child interfaces first */
1799         list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
1800                 /* Stop GC on child */
1801                 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1802                 cancel_delayed_work(&cpriv->neigh_reap_task);
1803                 unregister_netdevice_queue(cpriv->dev, &head);
1804         }
1805         unregister_netdevice_many(&head);
1806
1807         ipoib_neigh_hash_uninit(dev);
1808
1809         ipoib_ib_dev_cleanup(dev);
1810
1811         /* no more works over the priv->wq */
1812         if (priv->wq) {
1813                 flush_workqueue(priv->wq);
1814                 destroy_workqueue(priv->wq);
1815                 priv->wq = NULL;
1816         }
1817 }
1818
1819 static int ipoib_set_vf_link_state(struct net_device *dev, int vf, int link_state)
1820 {
1821         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1822
1823         return ib_set_vf_link_state(priv->ca, vf, priv->port, link_state);
1824 }
1825
1826 static int ipoib_get_vf_config(struct net_device *dev, int vf,
1827                                struct ifla_vf_info *ivf)
1828 {
1829         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1830         int err;
1831
1832         err = ib_get_vf_config(priv->ca, vf, priv->port, ivf);
1833         if (err)
1834                 return err;
1835
1836         ivf->vf = vf;
1837         memcpy(ivf->mac, dev->dev_addr, dev->addr_len);
1838
1839         return 0;
1840 }
1841
1842 static int ipoib_set_vf_guid(struct net_device *dev, int vf, u64 guid, int type)
1843 {
1844         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1845
1846         if (type != IFLA_VF_IB_NODE_GUID && type != IFLA_VF_IB_PORT_GUID)
1847                 return -EINVAL;
1848
1849         return ib_set_vf_guid(priv->ca, vf, priv->port, guid, type);
1850 }
1851
1852 static int ipoib_get_vf_stats(struct net_device *dev, int vf,
1853                               struct ifla_vf_stats *vf_stats)
1854 {
1855         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1856
1857         return ib_get_vf_stats(priv->ca, vf, priv->port, vf_stats);
1858 }
1859
1860 static const struct header_ops ipoib_header_ops = {
1861         .create = ipoib_hard_header,
1862 };
1863
1864 static const struct net_device_ops ipoib_netdev_ops_pf = {
1865         .ndo_uninit              = ipoib_uninit,
1866         .ndo_open                = ipoib_open,
1867         .ndo_stop                = ipoib_stop,
1868         .ndo_change_mtu          = ipoib_change_mtu,
1869         .ndo_fix_features        = ipoib_fix_features,
1870         .ndo_start_xmit          = ipoib_start_xmit,
1871         .ndo_tx_timeout          = ipoib_timeout,
1872         .ndo_set_rx_mode         = ipoib_set_mcast_list,
1873         .ndo_get_iflink          = ipoib_get_iflink,
1874         .ndo_set_vf_link_state   = ipoib_set_vf_link_state,
1875         .ndo_get_vf_config       = ipoib_get_vf_config,
1876         .ndo_get_vf_stats        = ipoib_get_vf_stats,
1877         .ndo_set_vf_guid         = ipoib_set_vf_guid,
1878         .ndo_set_mac_address     = ipoib_set_mac,
1879         .ndo_get_stats64         = ipoib_get_stats,
1880         .ndo_do_ioctl            = ipoib_ioctl,
1881 };
1882
1883 static const struct net_device_ops ipoib_netdev_ops_vf = {
1884         .ndo_uninit              = ipoib_uninit,
1885         .ndo_open                = ipoib_open,
1886         .ndo_stop                = ipoib_stop,
1887         .ndo_change_mtu          = ipoib_change_mtu,
1888         .ndo_fix_features        = ipoib_fix_features,
1889         .ndo_start_xmit          = ipoib_start_xmit,
1890         .ndo_tx_timeout          = ipoib_timeout,
1891         .ndo_set_rx_mode         = ipoib_set_mcast_list,
1892         .ndo_get_iflink          = ipoib_get_iflink,
1893         .ndo_get_stats64         = ipoib_get_stats,
1894         .ndo_do_ioctl            = ipoib_ioctl,
1895 };
1896
1897 void ipoib_setup_common(struct net_device *dev)
1898 {
1899         dev->header_ops          = &ipoib_header_ops;
1900
1901         ipoib_set_ethtool_ops(dev);
1902
1903         dev->watchdog_timeo      = HZ;
1904
1905         dev->flags              |= IFF_BROADCAST | IFF_MULTICAST;
1906
1907         dev->hard_header_len     = IPOIB_HARD_LEN;
1908         dev->addr_len            = INFINIBAND_ALEN;
1909         dev->type                = ARPHRD_INFINIBAND;
1910         dev->tx_queue_len        = ipoib_sendq_size * 2;
1911         dev->features            = (NETIF_F_VLAN_CHALLENGED     |
1912                                     NETIF_F_HIGHDMA);
1913         netif_keep_dst(dev);
1914
1915         memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
1916 }
1917
1918 static void ipoib_build_priv(struct net_device *dev)
1919 {
1920         struct ipoib_dev_priv *priv = ipoib_priv(dev);
1921
1922         priv->dev = dev;
1923         spin_lock_init(&priv->lock);
1924         init_rwsem(&priv->vlan_rwsem);
1925         mutex_init(&priv->mcast_mutex);
1926         mutex_init(&priv->sysfs_mutex);
1927
1928         INIT_LIST_HEAD(&priv->path_list);
1929         INIT_LIST_HEAD(&priv->child_intfs);
1930         INIT_LIST_HEAD(&priv->dead_ahs);
1931         INIT_LIST_HEAD(&priv->multicast_list);
1932
1933         INIT_DELAYED_WORK(&priv->mcast_task,   ipoib_mcast_join_task);
1934         INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
1935         INIT_WORK(&priv->flush_light,   ipoib_ib_dev_flush_light);
1936         INIT_WORK(&priv->flush_normal,   ipoib_ib_dev_flush_normal);
1937         INIT_WORK(&priv->flush_heavy,   ipoib_ib_dev_flush_heavy);
1938         INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1939         INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
1940         INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
1941 }
1942
1943 static const struct net_device_ops ipoib_netdev_default_pf = {
1944         .ndo_init                = ipoib_dev_init_default,
1945         .ndo_uninit              = ipoib_dev_uninit_default,
1946         .ndo_open                = ipoib_ib_dev_open_default,
1947         .ndo_stop                = ipoib_ib_dev_stop_default,
1948 };
1949
1950 static struct net_device
1951 *ipoib_create_netdev_default(struct ib_device *hca,
1952                              const char *name,
1953                              unsigned char name_assign_type,
1954                              void (*setup)(struct net_device *))
1955 {
1956         struct net_device *dev;
1957         struct rdma_netdev *rn;
1958
1959         dev = alloc_netdev((int)sizeof(struct rdma_netdev),
1960                            name,
1961                            name_assign_type, setup);
1962         if (!dev)
1963                 return NULL;
1964
1965         rn = netdev_priv(dev);
1966
1967         rn->send = ipoib_send;
1968         rn->attach_mcast = ipoib_mcast_attach;
1969         rn->detach_mcast = ipoib_mcast_detach;
1970         rn->free_rdma_netdev = free_netdev;
1971         rn->hca = hca;
1972
1973         dev->netdev_ops = &ipoib_netdev_default_pf;
1974
1975         return dev;
1976 }
1977
1978 static struct net_device *ipoib_get_netdev(struct ib_device *hca, u8 port,
1979                                            const char *name)
1980 {
1981         struct net_device *dev;
1982
1983         if (hca->alloc_rdma_netdev) {
1984                 dev = hca->alloc_rdma_netdev(hca, port,
1985                                              RDMA_NETDEV_IPOIB, name,
1986                                              NET_NAME_UNKNOWN,
1987                                              ipoib_setup_common);
1988                 if (IS_ERR_OR_NULL(dev) && PTR_ERR(dev) != -EOPNOTSUPP)
1989                         return NULL;
1990         }
1991
1992         if (!hca->alloc_rdma_netdev || PTR_ERR(dev) == -EOPNOTSUPP)
1993                 dev = ipoib_create_netdev_default(hca, name, NET_NAME_UNKNOWN,
1994                                                   ipoib_setup_common);
1995
1996         return dev;
1997 }
1998
1999 struct ipoib_dev_priv *ipoib_intf_alloc(struct ib_device *hca, u8 port,
2000                                         const char *name)
2001 {
2002         struct net_device *dev;
2003         struct ipoib_dev_priv *priv;
2004         struct rdma_netdev *rn;
2005
2006         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2007         if (!priv)
2008                 return NULL;
2009
2010         dev = ipoib_get_netdev(hca, port, name);
2011         if (!dev)
2012                 goto free_priv;
2013
2014         priv->rn_ops = dev->netdev_ops;
2015
2016         /* fixme : should be after the query_cap */
2017         if (priv->hca_caps & IB_DEVICE_VIRTUAL_FUNCTION)
2018                 dev->netdev_ops = &ipoib_netdev_ops_vf;
2019         else
2020                 dev->netdev_ops = &ipoib_netdev_ops_pf;
2021
2022         rn = netdev_priv(dev);
2023         rn->clnt_priv = priv;
2024         ipoib_build_priv(dev);
2025
2026         return priv;
2027 free_priv:
2028         kfree(priv);
2029         return NULL;
2030 }
2031
2032 static ssize_t show_pkey(struct device *dev,
2033                          struct device_attribute *attr, char *buf)
2034 {
2035         struct net_device *ndev = to_net_dev(dev);
2036         struct ipoib_dev_priv *priv = ipoib_priv(ndev);
2037
2038         return sprintf(buf, "0x%04x\n", priv->pkey);
2039 }
2040 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
2041
2042 static ssize_t show_umcast(struct device *dev,
2043                            struct device_attribute *attr, char *buf)
2044 {
2045         struct net_device *ndev = to_net_dev(dev);
2046         struct ipoib_dev_priv *priv = ipoib_priv(ndev);
2047
2048         return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
2049 }
2050
2051 void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
2052 {
2053         struct ipoib_dev_priv *priv = ipoib_priv(ndev);
2054
2055         if (umcast_val > 0) {
2056                 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
2057                 ipoib_warn(priv, "ignoring multicast groups joined directly "
2058                                 "by userspace\n");
2059         } else
2060                 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
2061 }
2062
2063 static ssize_t set_umcast(struct device *dev,
2064                           struct device_attribute *attr,
2065                           const char *buf, size_t count)
2066 {
2067         unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
2068
2069         ipoib_set_umcast(to_net_dev(dev), umcast_val);
2070
2071         return count;
2072 }
2073 static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
2074
2075 int ipoib_add_umcast_attr(struct net_device *dev)
2076 {
2077         return device_create_file(&dev->dev, &dev_attr_umcast);
2078 }
2079
2080 static void set_base_guid(struct ipoib_dev_priv *priv, union ib_gid *gid)
2081 {
2082         struct ipoib_dev_priv *child_priv;
2083         struct net_device *netdev = priv->dev;
2084
2085         netif_addr_lock_bh(netdev);
2086
2087         memcpy(&priv->local_gid.global.interface_id,
2088                &gid->global.interface_id,
2089                sizeof(gid->global.interface_id));
2090         memcpy(netdev->dev_addr + 4, &priv->local_gid, sizeof(priv->local_gid));
2091         clear_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
2092
2093         netif_addr_unlock_bh(netdev);
2094
2095         if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
2096                 down_read(&priv->vlan_rwsem);
2097                 list_for_each_entry(child_priv, &priv->child_intfs, list)
2098                         set_base_guid(child_priv, gid);
2099                 up_read(&priv->vlan_rwsem);
2100         }
2101 }
2102
2103 static int ipoib_check_lladdr(struct net_device *dev,
2104                               struct sockaddr_storage *ss)
2105 {
2106         union ib_gid *gid = (union ib_gid *)(ss->__data + 4);
2107         int ret = 0;
2108
2109         netif_addr_lock_bh(dev);
2110
2111         /* Make sure the QPN, reserved and subnet prefix match the current
2112          * lladdr, it also makes sure the lladdr is unicast.
2113          */
2114         if (memcmp(dev->dev_addr, ss->__data,
2115                    4 + sizeof(gid->global.subnet_prefix)) ||
2116             gid->global.interface_id == 0)
2117                 ret = -EINVAL;
2118
2119         netif_addr_unlock_bh(dev);
2120
2121         return ret;
2122 }
2123
2124 static int ipoib_set_mac(struct net_device *dev, void *addr)
2125 {
2126         struct ipoib_dev_priv *priv = ipoib_priv(dev);
2127         struct sockaddr_storage *ss = addr;
2128         int ret;
2129
2130         if (!(dev->priv_flags & IFF_LIVE_ADDR_CHANGE) && netif_running(dev))
2131                 return -EBUSY;
2132
2133         ret = ipoib_check_lladdr(dev, ss);
2134         if (ret)
2135                 return ret;
2136
2137         set_base_guid(priv, (union ib_gid *)(ss->__data + 4));
2138
2139         queue_work(ipoib_workqueue, &priv->flush_light);
2140
2141         return 0;
2142 }
2143
2144 static ssize_t create_child(struct device *dev,
2145                             struct device_attribute *attr,
2146                             const char *buf, size_t count)
2147 {
2148         int pkey;
2149         int ret;
2150
2151         if (sscanf(buf, "%i", &pkey) != 1)
2152                 return -EINVAL;
2153
2154         if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
2155                 return -EINVAL;
2156
2157         /*
2158          * Set the full membership bit, so that we join the right
2159          * broadcast group, etc.
2160          */
2161         pkey |= 0x8000;
2162
2163         ret = ipoib_vlan_add(to_net_dev(dev), pkey);
2164
2165         return ret ? ret : count;
2166 }
2167 static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
2168
2169 static ssize_t delete_child(struct device *dev,
2170                             struct device_attribute *attr,
2171                             const char *buf, size_t count)
2172 {
2173         int pkey;
2174         int ret;
2175
2176         if (sscanf(buf, "%i", &pkey) != 1)
2177                 return -EINVAL;
2178
2179         if (pkey < 0 || pkey > 0xffff)
2180                 return -EINVAL;
2181
2182         ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
2183
2184         return ret ? ret : count;
2185
2186 }
2187 static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
2188
2189 int ipoib_add_pkey_attr(struct net_device *dev)
2190 {
2191         return device_create_file(&dev->dev, &dev_attr_pkey);
2192 }
2193
2194 void ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
2195 {
2196         priv->hca_caps = hca->attrs.device_cap_flags;
2197
2198         if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
2199                 priv->dev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
2200
2201                 if (priv->hca_caps & IB_DEVICE_UD_TSO)
2202                         priv->dev->hw_features |= NETIF_F_TSO;
2203
2204                 priv->dev->features |= priv->dev->hw_features;
2205         }
2206 }
2207
2208 static struct net_device *ipoib_add_port(const char *format,
2209                                          struct ib_device *hca, u8 port)
2210 {
2211         struct ipoib_dev_priv *priv;
2212         struct ib_port_attr attr;
2213         struct rdma_netdev *rn;
2214         int result = -ENOMEM;
2215
2216         priv = ipoib_intf_alloc(hca, port, format);
2217         if (!priv)
2218                 goto alloc_mem_failed;
2219
2220         SET_NETDEV_DEV(priv->dev, hca->dev.parent);
2221         priv->dev->dev_id = port - 1;
2222
2223         result = ib_query_port(hca, port, &attr);
2224         if (result) {
2225                 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
2226                        hca->name, port);
2227                 goto device_init_failed;
2228         }
2229
2230         priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
2231
2232         /* MTU will be reset when mcast join happens */
2233         priv->dev->mtu  = IPOIB_UD_MTU(priv->max_ib_mtu);
2234         priv->mcast_mtu  = priv->admin_mtu = priv->dev->mtu;
2235         priv->dev->max_mtu = IPOIB_CM_MTU;
2236
2237         priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
2238
2239         result = ib_query_pkey(hca, port, 0, &priv->pkey);
2240         if (result) {
2241                 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
2242                        hca->name, port, result);
2243                 goto device_init_failed;
2244         }
2245
2246         ipoib_set_dev_features(priv, hca);
2247
2248         /*
2249          * Set the full membership bit, so that we join the right
2250          * broadcast group, etc.
2251          */
2252         priv->pkey |= 0x8000;
2253
2254         priv->dev->broadcast[8] = priv->pkey >> 8;
2255         priv->dev->broadcast[9] = priv->pkey & 0xff;
2256
2257         result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
2258         if (result) {
2259                 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
2260                        hca->name, port, result);
2261                 goto device_init_failed;
2262         }
2263
2264         memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw,
2265                sizeof(union ib_gid));
2266         set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
2267
2268         result = ipoib_dev_init(priv->dev, hca, port);
2269         if (result) {
2270                 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
2271                        hca->name, port, result);
2272                 goto device_init_failed;
2273         }
2274
2275         INIT_IB_EVENT_HANDLER(&priv->event_handler,
2276                               priv->ca, ipoib_event);
2277         ib_register_event_handler(&priv->event_handler);
2278
2279         /* call event handler to ensure pkey in sync */
2280         queue_work(ipoib_workqueue, &priv->flush_heavy);
2281
2282         result = register_netdev(priv->dev);
2283         if (result) {
2284                 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
2285                        hca->name, port, result);
2286                 goto register_failed;
2287         }
2288
2289         result = -ENOMEM;
2290         if (ipoib_cm_add_mode_attr(priv->dev))
2291                 goto sysfs_failed;
2292         if (ipoib_add_pkey_attr(priv->dev))
2293                 goto sysfs_failed;
2294         if (ipoib_add_umcast_attr(priv->dev))
2295                 goto sysfs_failed;
2296         if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
2297                 goto sysfs_failed;
2298         if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
2299                 goto sysfs_failed;
2300
2301         return priv->dev;
2302
2303 sysfs_failed:
2304         unregister_netdev(priv->dev);
2305
2306 register_failed:
2307         ib_unregister_event_handler(&priv->event_handler);
2308         flush_workqueue(ipoib_workqueue);
2309         /* Stop GC if started before flush */
2310         set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
2311         cancel_delayed_work(&priv->neigh_reap_task);
2312         flush_workqueue(priv->wq);
2313         ipoib_dev_cleanup(priv->dev);
2314
2315 device_init_failed:
2316         rn = netdev_priv(priv->dev);
2317         rn->free_rdma_netdev(priv->dev);
2318         kfree(priv);
2319
2320 alloc_mem_failed:
2321         return ERR_PTR(result);
2322 }
2323
2324 static void ipoib_add_one(struct ib_device *device)
2325 {
2326         struct list_head *dev_list;
2327         struct net_device *dev;
2328         struct ipoib_dev_priv *priv;
2329         int p;
2330         int count = 0;
2331
2332         dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
2333         if (!dev_list)
2334                 return;
2335
2336         INIT_LIST_HEAD(dev_list);
2337
2338         for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
2339                 if (!rdma_protocol_ib(device, p))
2340                         continue;
2341                 dev = ipoib_add_port("ib%d", device, p);
2342                 if (!IS_ERR(dev)) {
2343                         priv = ipoib_priv(dev);
2344                         list_add_tail(&priv->list, dev_list);
2345                         count++;
2346                 }
2347         }
2348
2349         if (!count) {
2350                 kfree(dev_list);
2351                 return;
2352         }
2353
2354         ib_set_client_data(device, &ipoib_client, dev_list);
2355 }
2356
2357 static void ipoib_remove_one(struct ib_device *device, void *client_data)
2358 {
2359         struct ipoib_dev_priv *priv, *tmp, *cpriv, *tcpriv;
2360         struct list_head *dev_list = client_data;
2361
2362         if (!dev_list)
2363                 return;
2364
2365         list_for_each_entry_safe(priv, tmp, dev_list, list) {
2366                 struct rdma_netdev *parent_rn = netdev_priv(priv->dev);
2367
2368                 ib_unregister_event_handler(&priv->event_handler);
2369                 flush_workqueue(ipoib_workqueue);
2370
2371                 /* mark interface in the middle of destruction */
2372                 set_bit(IPOIB_FLAG_GOING_DOWN, &priv->flags);
2373
2374                 rtnl_lock();
2375                 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
2376                 rtnl_unlock();
2377
2378                 /* Stop GC */
2379                 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
2380                 cancel_delayed_work(&priv->neigh_reap_task);
2381                 flush_workqueue(priv->wq);
2382
2383                 /* Wrap rtnl_lock/unlock with mutex to protect sysfs calls */
2384                 mutex_lock(&priv->sysfs_mutex);
2385                 unregister_netdev(priv->dev);
2386                 mutex_unlock(&priv->sysfs_mutex);
2387
2388                 parent_rn->free_rdma_netdev(priv->dev);
2389
2390                 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
2391                         struct rdma_netdev *child_rn;
2392
2393                         child_rn = netdev_priv(cpriv->dev);
2394                         child_rn->free_rdma_netdev(cpriv->dev);
2395                         kfree(cpriv);
2396                 }
2397
2398                 kfree(priv);
2399         }
2400
2401         kfree(dev_list);
2402 }
2403
2404 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2405 static struct notifier_block ipoib_netdev_notifier = {
2406         .notifier_call = ipoib_netdev_event,
2407 };
2408 #endif
2409
2410 static int __init ipoib_init_module(void)
2411 {
2412         int ret;
2413
2414         ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
2415         ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
2416         ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
2417
2418         ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
2419         ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
2420         ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
2421 #ifdef CONFIG_INFINIBAND_IPOIB_CM
2422         ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
2423         ipoib_max_conn_qp = max(ipoib_max_conn_qp, 0);
2424 #endif
2425
2426         /*
2427          * When copying small received packets, we only copy from the
2428          * linear data part of the SKB, so we rely on this condition.
2429          */
2430         BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
2431
2432         ret = ipoib_register_debugfs();
2433         if (ret)
2434                 return ret;
2435
2436         /*
2437          * We create a global workqueue here that is used for all flush
2438          * operations.  However, if you attempt to flush a workqueue
2439          * from a task on that same workqueue, it deadlocks the system.
2440          * We want to be able to flush the tasks associated with a
2441          * specific net device, so we also create a workqueue for each
2442          * netdevice.  We queue up the tasks for that device only on
2443          * its private workqueue, and we only queue up flush events
2444          * on our global flush workqueue.  This avoids the deadlocks.
2445          */
2446         ipoib_workqueue = alloc_ordered_workqueue("ipoib_flush",
2447                                                   WQ_MEM_RECLAIM);
2448         if (!ipoib_workqueue) {
2449                 ret = -ENOMEM;
2450                 goto err_fs;
2451         }
2452
2453         ib_sa_register_client(&ipoib_sa_client);
2454
2455         ret = ib_register_client(&ipoib_client);
2456         if (ret)
2457                 goto err_sa;
2458
2459         ret = ipoib_netlink_init();
2460         if (ret)
2461                 goto err_client;
2462
2463 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2464         register_netdevice_notifier(&ipoib_netdev_notifier);
2465 #endif
2466         return 0;
2467
2468 err_client:
2469         ib_unregister_client(&ipoib_client);
2470
2471 err_sa:
2472         ib_sa_unregister_client(&ipoib_sa_client);
2473         destroy_workqueue(ipoib_workqueue);
2474
2475 err_fs:
2476         ipoib_unregister_debugfs();
2477
2478         return ret;
2479 }
2480
2481 static void __exit ipoib_cleanup_module(void)
2482 {
2483 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2484         unregister_netdevice_notifier(&ipoib_netdev_notifier);
2485 #endif
2486         ipoib_netlink_fini();
2487         ib_unregister_client(&ipoib_client);
2488         ib_sa_unregister_client(&ipoib_sa_client);
2489         ipoib_unregister_debugfs();
2490         destroy_workqueue(ipoib_workqueue);
2491 }
2492
2493 module_init(ipoib_init_module);
2494 module_exit(ipoib_cleanup_module);