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