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