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