blob: bad76eed06b3e04ee20087b9dcc59523361a81e7 [file] [log] [blame]
Kyle Swenson8d8f6542021-03-15 11:02:55 -06001/*
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
57const char ipoib_driver_version[] = DRV_VERSION;
58
59MODULE_AUTHOR("Roland Dreier");
60MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
61MODULE_LICENSE("Dual BSD/GPL");
62MODULE_VERSION(DRV_VERSION);
63
64int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
65int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
66
67module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
68MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
69module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
70MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
71
72#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
73int ipoib_debug_level;
74
75module_param_named(debug_level, ipoib_debug_level, int, 0644);
76MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
77#endif
78
79struct ipoib_path_iter {
80 struct net_device *dev;
81 struct ipoib_path path;
82};
83
84static 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
90struct workqueue_struct *ipoib_workqueue;
91
92struct ib_sa_client ipoib_sa_client;
93
94static void ipoib_add_one(struct ib_device *device);
95static void ipoib_remove_one(struct ib_device *device, void *client_data);
96static void ipoib_neigh_reclaim(struct rcu_head *rp);
97static 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
102static 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
110static 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
136int 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
176err_stop:
177 ipoib_ib_dev_stop(dev);
178
179err_disable:
180 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
181
182 return -EINVAL;
183}
184
185static 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
218static void ipoib_uninit(struct net_device *dev)
219{
220 ipoib_dev_cleanup(dev);
221}
222
223static 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
233static 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 */
261static 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 */
301static 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 */
327static 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 }
348out:
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. */
358static 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 */
405static 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
429static 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
479int 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
509struct 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
533static 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
563static 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
583struct 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
602int 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
631void 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
639void 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
656static 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
664void 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
694static 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 path->pathrec = *pathrec;
728
729 old_ah = path->ah;
730 path->ah = ah;
731
732 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
733 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
734
735 while ((skb = __skb_dequeue(&path->queue)))
736 __skb_queue_tail(&skqueue, skb);
737
738 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
739 if (neigh->ah) {
740 WARN_ON(neigh->ah != old_ah);
741 /*
742 * Dropping the ah reference inside
743 * priv->lock is safe here, because we
744 * will hold one more reference from
745 * the original value of path->ah (ie
746 * old_ah).
747 */
748 ipoib_put_ah(neigh->ah);
749 }
750 kref_get(&path->ah->ref);
751 neigh->ah = path->ah;
752
753 if (ipoib_cm_enabled(dev, neigh->daddr)) {
754 if (!ipoib_cm_get(neigh))
755 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
756 path,
757 neigh));
758 if (!ipoib_cm_get(neigh)) {
759 ipoib_neigh_free(neigh);
760 continue;
761 }
762 }
763
764 while ((skb = __skb_dequeue(&neigh->queue)))
765 __skb_queue_tail(&skqueue, skb);
766 }
767 path->valid = 1;
768 }
769
770 path->query = NULL;
771 complete(&path->done);
772
773 spin_unlock_irqrestore(&priv->lock, flags);
774
775 if (IS_ERR_OR_NULL(ah))
776 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
777
778 if (old_ah)
779 ipoib_put_ah(old_ah);
780
781 while ((skb = __skb_dequeue(&skqueue))) {
782 skb->dev = dev;
783 if (dev_queue_xmit(skb))
784 ipoib_warn(priv, "dev_queue_xmit failed "
785 "to requeue packet\n");
786 }
787}
788
789static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
790{
791 struct ipoib_dev_priv *priv = netdev_priv(dev);
792 struct ipoib_path *path;
793
794 if (!priv->broadcast)
795 return NULL;
796
797 path = kzalloc(sizeof *path, GFP_ATOMIC);
798 if (!path)
799 return NULL;
800
801 path->dev = dev;
802
803 skb_queue_head_init(&path->queue);
804
805 INIT_LIST_HEAD(&path->neigh_list);
806
807 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
808 path->pathrec.sgid = priv->local_gid;
809 path->pathrec.pkey = cpu_to_be16(priv->pkey);
810 path->pathrec.numb_path = 1;
811 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
812
813 return path;
814}
815
816static int path_rec_start(struct net_device *dev,
817 struct ipoib_path *path)
818{
819 struct ipoib_dev_priv *priv = netdev_priv(dev);
820
821 ipoib_dbg(priv, "Start path record lookup for %pI6\n",
822 path->pathrec.dgid.raw);
823
824 init_completion(&path->done);
825
826 path->query_id =
827 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
828 &path->pathrec,
829 IB_SA_PATH_REC_DGID |
830 IB_SA_PATH_REC_SGID |
831 IB_SA_PATH_REC_NUMB_PATH |
832 IB_SA_PATH_REC_TRAFFIC_CLASS |
833 IB_SA_PATH_REC_PKEY,
834 1000, GFP_ATOMIC,
835 path_rec_completion,
836 path, &path->query);
837 if (path->query_id < 0) {
838 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
839 path->query = NULL;
840 complete(&path->done);
841 return path->query_id;
842 }
843
844 return 0;
845}
846
847static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
848 struct net_device *dev)
849{
850 struct ipoib_dev_priv *priv = netdev_priv(dev);
851 struct ipoib_path *path;
852 struct ipoib_neigh *neigh;
853 unsigned long flags;
854
855 spin_lock_irqsave(&priv->lock, flags);
856 neigh = ipoib_neigh_alloc(daddr, dev);
857 if (!neigh) {
858 spin_unlock_irqrestore(&priv->lock, flags);
859 ++dev->stats.tx_dropped;
860 dev_kfree_skb_any(skb);
861 return;
862 }
863
864 path = __path_find(dev, daddr + 4);
865 if (!path) {
866 path = path_rec_create(dev, daddr + 4);
867 if (!path)
868 goto err_path;
869
870 __path_add(dev, path);
871 }
872
873 list_add_tail(&neigh->list, &path->neigh_list);
874
875 if (path->ah) {
876 kref_get(&path->ah->ref);
877 neigh->ah = path->ah;
878
879 if (ipoib_cm_enabled(dev, neigh->daddr)) {
880 if (!ipoib_cm_get(neigh))
881 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
882 if (!ipoib_cm_get(neigh)) {
883 ipoib_neigh_free(neigh);
884 goto err_drop;
885 }
886 if (skb_queue_len(&neigh->queue) <
887 IPOIB_MAX_PATH_REC_QUEUE) {
888 push_pseudo_header(skb, neigh->daddr);
889 __skb_queue_tail(&neigh->queue, skb);
890 } else {
891 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
892 skb_queue_len(&neigh->queue));
893 goto err_drop;
894 }
895 } else {
896 spin_unlock_irqrestore(&priv->lock, flags);
897 ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
898 ipoib_neigh_put(neigh);
899 return;
900 }
901 } else {
902 neigh->ah = NULL;
903
904 if (!path->query && path_rec_start(dev, path))
905 goto err_path;
906 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
907 push_pseudo_header(skb, neigh->daddr);
908 __skb_queue_tail(&neigh->queue, skb);
909 } else {
910 goto err_drop;
911 }
912 }
913
914 spin_unlock_irqrestore(&priv->lock, flags);
915 ipoib_neigh_put(neigh);
916 return;
917
918err_path:
919 ipoib_neigh_free(neigh);
920err_drop:
921 ++dev->stats.tx_dropped;
922 dev_kfree_skb_any(skb);
923
924 spin_unlock_irqrestore(&priv->lock, flags);
925 ipoib_neigh_put(neigh);
926}
927
928static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
929 struct ipoib_pseudo_header *phdr)
930{
931 struct ipoib_dev_priv *priv = netdev_priv(dev);
932 struct ipoib_path *path;
933 unsigned long flags;
934
935 spin_lock_irqsave(&priv->lock, flags);
936
937 path = __path_find(dev, phdr->hwaddr + 4);
938 if (!path || !path->valid) {
939 int new_path = 0;
940
941 if (!path) {
942 path = path_rec_create(dev, phdr->hwaddr + 4);
943 new_path = 1;
944 }
945 if (path) {
946 if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
947 push_pseudo_header(skb, phdr->hwaddr);
948 __skb_queue_tail(&path->queue, skb);
949 } else {
950 ++dev->stats.tx_dropped;
951 dev_kfree_skb_any(skb);
952 }
953
954 if (!path->query && path_rec_start(dev, path)) {
955 spin_unlock_irqrestore(&priv->lock, flags);
956 if (new_path)
957 path_free(dev, path);
958 return;
959 } else
960 __path_add(dev, path);
961 } else {
962 ++dev->stats.tx_dropped;
963 dev_kfree_skb_any(skb);
964 }
965
966 spin_unlock_irqrestore(&priv->lock, flags);
967 return;
968 }
969
970 if (path->ah) {
971 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
972 be16_to_cpu(path->pathrec.dlid));
973
974 spin_unlock_irqrestore(&priv->lock, flags);
975 ipoib_send(dev, skb, path->ah, IPOIB_QPN(phdr->hwaddr));
976 return;
977 } else if ((path->query || !path_rec_start(dev, path)) &&
978 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
979 push_pseudo_header(skb, phdr->hwaddr);
980 __skb_queue_tail(&path->queue, skb);
981 } else {
982 ++dev->stats.tx_dropped;
983 dev_kfree_skb_any(skb);
984 }
985
986 spin_unlock_irqrestore(&priv->lock, flags);
987}
988
989static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
990{
991 struct ipoib_dev_priv *priv = netdev_priv(dev);
992 struct ipoib_neigh *neigh;
993 struct ipoib_pseudo_header *phdr;
994 struct ipoib_header *header;
995 unsigned long flags;
996
997 phdr = (struct ipoib_pseudo_header *) skb->data;
998 skb_pull(skb, sizeof(*phdr));
999 header = (struct ipoib_header *) skb->data;
1000
1001 if (unlikely(phdr->hwaddr[4] == 0xff)) {
1002 /* multicast, arrange "if" according to probability */
1003 if ((header->proto != htons(ETH_P_IP)) &&
1004 (header->proto != htons(ETH_P_IPV6)) &&
1005 (header->proto != htons(ETH_P_ARP)) &&
1006 (header->proto != htons(ETH_P_RARP)) &&
1007 (header->proto != htons(ETH_P_TIPC))) {
1008 /* ethertype not supported by IPoIB */
1009 ++dev->stats.tx_dropped;
1010 dev_kfree_skb_any(skb);
1011 return NETDEV_TX_OK;
1012 }
1013 /* Add in the P_Key for multicast*/
1014 phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
1015 phdr->hwaddr[9] = priv->pkey & 0xff;
1016
1017 neigh = ipoib_neigh_get(dev, phdr->hwaddr);
1018 if (likely(neigh))
1019 goto send_using_neigh;
1020 ipoib_mcast_send(dev, phdr->hwaddr, skb);
1021 return NETDEV_TX_OK;
1022 }
1023
1024 /* unicast, arrange "switch" according to probability */
1025 switch (header->proto) {
1026 case htons(ETH_P_IP):
1027 case htons(ETH_P_IPV6):
1028 case htons(ETH_P_TIPC):
1029 neigh = ipoib_neigh_get(dev, phdr->hwaddr);
1030 if (unlikely(!neigh)) {
1031 neigh_add_path(skb, phdr->hwaddr, dev);
1032 return NETDEV_TX_OK;
1033 }
1034 break;
1035 case htons(ETH_P_ARP):
1036 case htons(ETH_P_RARP):
1037 /* for unicast ARP and RARP should always perform path find */
1038 unicast_arp_send(skb, dev, phdr);
1039 return NETDEV_TX_OK;
1040 default:
1041 /* ethertype not supported by IPoIB */
1042 ++dev->stats.tx_dropped;
1043 dev_kfree_skb_any(skb);
1044 return NETDEV_TX_OK;
1045 }
1046
1047send_using_neigh:
1048 /* note we now hold a ref to neigh */
1049 if (ipoib_cm_get(neigh)) {
1050 if (ipoib_cm_up(neigh)) {
1051 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
1052 goto unref;
1053 }
1054 } else if (neigh->ah) {
1055 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(phdr->hwaddr));
1056 goto unref;
1057 }
1058
1059 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1060 push_pseudo_header(skb, phdr->hwaddr);
1061 spin_lock_irqsave(&priv->lock, flags);
1062 __skb_queue_tail(&neigh->queue, skb);
1063 spin_unlock_irqrestore(&priv->lock, flags);
1064 } else {
1065 ++dev->stats.tx_dropped;
1066 dev_kfree_skb_any(skb);
1067 }
1068
1069unref:
1070 ipoib_neigh_put(neigh);
1071
1072 return NETDEV_TX_OK;
1073}
1074
1075static void ipoib_timeout(struct net_device *dev)
1076{
1077 struct ipoib_dev_priv *priv = netdev_priv(dev);
1078
1079 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
1080 jiffies_to_msecs(jiffies - dev->trans_start));
1081 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
1082 netif_queue_stopped(dev),
1083 priv->tx_head, priv->tx_tail);
1084 /* XXX reset QP, etc. */
1085}
1086
1087static int ipoib_hard_header(struct sk_buff *skb,
1088 struct net_device *dev,
1089 unsigned short type,
1090 const void *daddr, const void *saddr, unsigned len)
1091{
1092 struct ipoib_header *header;
1093
1094 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
1095
1096 header->proto = htons(type);
1097 header->reserved = 0;
1098
1099 /*
1100 * we don't rely on dst_entry structure, always stuff the
1101 * destination address into skb hard header so we can figure out where
1102 * to send the packet later.
1103 */
1104 push_pseudo_header(skb, daddr);
1105
1106 return IPOIB_HARD_LEN;
1107}
1108
1109static void ipoib_set_mcast_list(struct net_device *dev)
1110{
1111 struct ipoib_dev_priv *priv = netdev_priv(dev);
1112
1113 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
1114 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
1115 return;
1116 }
1117
1118 queue_work(priv->wq, &priv->restart_task);
1119}
1120
1121static int ipoib_get_iflink(const struct net_device *dev)
1122{
1123 struct ipoib_dev_priv *priv = netdev_priv(dev);
1124
1125 /* parent interface */
1126 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
1127 return dev->ifindex;
1128
1129 /* child/vlan interface */
1130 return priv->parent->ifindex;
1131}
1132
1133static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
1134{
1135 /*
1136 * Use only the address parts that contributes to spreading
1137 * The subnet prefix is not used as one can not connect to
1138 * same remote port (GUID) using the same remote QPN via two
1139 * different subnets.
1140 */
1141 /* qpn octets[1:4) & port GUID octets[12:20) */
1142 u32 *d32 = (u32 *) daddr;
1143 u32 hv;
1144
1145 hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
1146 return hv & htbl->mask;
1147}
1148
1149struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
1150{
1151 struct ipoib_dev_priv *priv = netdev_priv(dev);
1152 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1153 struct ipoib_neigh_hash *htbl;
1154 struct ipoib_neigh *neigh = NULL;
1155 u32 hash_val;
1156
1157 rcu_read_lock_bh();
1158
1159 htbl = rcu_dereference_bh(ntbl->htbl);
1160
1161 if (!htbl)
1162 goto out_unlock;
1163
1164 hash_val = ipoib_addr_hash(htbl, daddr);
1165 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
1166 neigh != NULL;
1167 neigh = rcu_dereference_bh(neigh->hnext)) {
1168 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1169 /* found, take one ref on behalf of the caller */
1170 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1171 /* deleted */
1172 neigh = NULL;
1173 goto out_unlock;
1174 }
1175
1176 if (likely(skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE))
1177 neigh->alive = jiffies;
1178 goto out_unlock;
1179 }
1180 }
1181
1182out_unlock:
1183 rcu_read_unlock_bh();
1184 return neigh;
1185}
1186
1187static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
1188{
1189 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1190 struct ipoib_neigh_hash *htbl;
1191 unsigned long neigh_obsolete;
1192 unsigned long dt;
1193 unsigned long flags;
1194 int i;
1195 LIST_HEAD(remove_list);
1196 struct ipoib_mcast *mcast, *tmcast;
1197 struct net_device *dev = priv->dev;
1198
1199 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1200 return;
1201
1202 spin_lock_irqsave(&priv->lock, flags);
1203
1204 htbl = rcu_dereference_protected(ntbl->htbl,
1205 lockdep_is_held(&priv->lock));
1206
1207 if (!htbl)
1208 goto out_unlock;
1209
1210 /* neigh is obsolete if it was idle for two GC periods */
1211 dt = 2 * arp_tbl.gc_interval;
1212 neigh_obsolete = jiffies - dt;
1213 /* handle possible race condition */
1214 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1215 goto out_unlock;
1216
1217 for (i = 0; i < htbl->size; i++) {
1218 struct ipoib_neigh *neigh;
1219 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1220
1221 while ((neigh = rcu_dereference_protected(*np,
1222 lockdep_is_held(&priv->lock))) != NULL) {
1223 /* was the neigh idle for two GC periods */
1224 if (time_after(neigh_obsolete, neigh->alive)) {
1225 u8 *mgid = neigh->daddr + 4;
1226
1227 /* Is this multicast ? */
1228 if (*mgid == 0xff) {
1229 mcast = __ipoib_mcast_find(dev, mgid);
1230
1231 if (mcast && test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
1232 list_del(&mcast->list);
1233 rb_erase(&mcast->rb_node, &priv->multicast_tree);
1234 list_add_tail(&mcast->list, &remove_list);
1235 }
1236 }
1237
1238 rcu_assign_pointer(*np,
1239 rcu_dereference_protected(neigh->hnext,
1240 lockdep_is_held(&priv->lock)));
1241 /* remove from path/mc list */
1242 list_del_init(&neigh->list);
1243 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1244 } else {
1245 np = &neigh->hnext;
1246 }
1247
1248 }
1249 }
1250
1251out_unlock:
1252 spin_unlock_irqrestore(&priv->lock, flags);
1253 list_for_each_entry_safe(mcast, tmcast, &remove_list, list) {
1254 ipoib_mcast_leave(dev, mcast);
1255 ipoib_mcast_free(mcast);
1256 }
1257}
1258
1259static void ipoib_reap_neigh(struct work_struct *work)
1260{
1261 struct ipoib_dev_priv *priv =
1262 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
1263
1264 __ipoib_reap_neigh(priv);
1265
1266 if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1267 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1268 arp_tbl.gc_interval);
1269}
1270
1271
1272static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
1273 struct net_device *dev)
1274{
1275 struct ipoib_neigh *neigh;
1276
1277 neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
1278 if (!neigh)
1279 return NULL;
1280
1281 neigh->dev = dev;
1282 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
1283 skb_queue_head_init(&neigh->queue);
1284 INIT_LIST_HEAD(&neigh->list);
1285 ipoib_cm_set(neigh, NULL);
1286 /* one ref on behalf of the caller */
1287 atomic_set(&neigh->refcnt, 1);
1288
1289 return neigh;
1290}
1291
1292struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
1293 struct net_device *dev)
1294{
1295 struct ipoib_dev_priv *priv = netdev_priv(dev);
1296 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1297 struct ipoib_neigh_hash *htbl;
1298 struct ipoib_neigh *neigh;
1299 u32 hash_val;
1300
1301 htbl = rcu_dereference_protected(ntbl->htbl,
1302 lockdep_is_held(&priv->lock));
1303 if (!htbl) {
1304 neigh = NULL;
1305 goto out_unlock;
1306 }
1307
1308 /* need to add a new neigh, but maybe some other thread succeeded?
1309 * recalc hash, maybe hash resize took place so we do a search
1310 */
1311 hash_val = ipoib_addr_hash(htbl, daddr);
1312 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
1313 lockdep_is_held(&priv->lock));
1314 neigh != NULL;
1315 neigh = rcu_dereference_protected(neigh->hnext,
1316 lockdep_is_held(&priv->lock))) {
1317 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1318 /* found, take one ref on behalf of the caller */
1319 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1320 /* deleted */
1321 neigh = NULL;
1322 break;
1323 }
1324 neigh->alive = jiffies;
1325 goto out_unlock;
1326 }
1327 }
1328
1329 neigh = ipoib_neigh_ctor(daddr, dev);
1330 if (!neigh)
1331 goto out_unlock;
1332
1333 /* one ref on behalf of the hash table */
1334 atomic_inc(&neigh->refcnt);
1335 neigh->alive = jiffies;
1336 /* put in hash */
1337 rcu_assign_pointer(neigh->hnext,
1338 rcu_dereference_protected(htbl->buckets[hash_val],
1339 lockdep_is_held(&priv->lock)));
1340 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1341 atomic_inc(&ntbl->entries);
1342
1343out_unlock:
1344
1345 return neigh;
1346}
1347
1348void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1349{
1350 /* neigh reference count was dropprd to zero */
1351 struct net_device *dev = neigh->dev;
1352 struct ipoib_dev_priv *priv = netdev_priv(dev);
1353 struct sk_buff *skb;
1354 if (neigh->ah)
1355 ipoib_put_ah(neigh->ah);
1356 while ((skb = __skb_dequeue(&neigh->queue))) {
1357 ++dev->stats.tx_dropped;
1358 dev_kfree_skb_any(skb);
1359 }
1360 if (ipoib_cm_get(neigh))
1361 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
1362 ipoib_dbg(netdev_priv(dev),
1363 "neigh free for %06x %pI6\n",
1364 IPOIB_QPN(neigh->daddr),
1365 neigh->daddr + 4);
1366 kfree(neigh);
1367 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1368 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1369 complete(&priv->ntbl.flushed);
1370 }
1371}
1372
1373static void ipoib_neigh_reclaim(struct rcu_head *rp)
1374{
1375 /* Called as a result of removal from hash table */
1376 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1377 /* note TX context may hold another ref */
1378 ipoib_neigh_put(neigh);
1379}
1380
1381void ipoib_neigh_free(struct ipoib_neigh *neigh)
1382{
1383 struct net_device *dev = neigh->dev;
1384 struct ipoib_dev_priv *priv = netdev_priv(dev);
1385 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1386 struct ipoib_neigh_hash *htbl;
1387 struct ipoib_neigh __rcu **np;
1388 struct ipoib_neigh *n;
1389 u32 hash_val;
1390
1391 htbl = rcu_dereference_protected(ntbl->htbl,
1392 lockdep_is_held(&priv->lock));
1393 if (!htbl)
1394 return;
1395
1396 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1397 np = &htbl->buckets[hash_val];
1398 for (n = rcu_dereference_protected(*np,
1399 lockdep_is_held(&priv->lock));
1400 n != NULL;
1401 n = rcu_dereference_protected(*np,
1402 lockdep_is_held(&priv->lock))) {
1403 if (n == neigh) {
1404 /* found */
1405 rcu_assign_pointer(*np,
1406 rcu_dereference_protected(neigh->hnext,
1407 lockdep_is_held(&priv->lock)));
1408 /* remove from parent list */
1409 list_del_init(&neigh->list);
1410 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1411 return;
1412 } else {
1413 np = &n->hnext;
1414 }
1415 }
1416}
1417
1418static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1419{
1420 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1421 struct ipoib_neigh_hash *htbl;
1422 struct ipoib_neigh __rcu **buckets;
1423 u32 size;
1424
1425 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1426 ntbl->htbl = NULL;
1427 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1428 if (!htbl)
1429 return -ENOMEM;
1430 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1431 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1432 buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1433 if (!buckets) {
1434 kfree(htbl);
1435 return -ENOMEM;
1436 }
1437 htbl->size = size;
1438 htbl->mask = (size - 1);
1439 htbl->buckets = buckets;
1440 RCU_INIT_POINTER(ntbl->htbl, htbl);
1441 htbl->ntbl = ntbl;
1442 atomic_set(&ntbl->entries, 0);
1443
1444 /* start garbage collection */
1445 clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1446 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1447 arp_tbl.gc_interval);
1448
1449 return 0;
1450}
1451
1452static void neigh_hash_free_rcu(struct rcu_head *head)
1453{
1454 struct ipoib_neigh_hash *htbl = container_of(head,
1455 struct ipoib_neigh_hash,
1456 rcu);
1457 struct ipoib_neigh __rcu **buckets = htbl->buckets;
1458 struct ipoib_neigh_table *ntbl = htbl->ntbl;
1459
1460 kfree(buckets);
1461 kfree(htbl);
1462 complete(&ntbl->deleted);
1463}
1464
1465void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1466{
1467 struct ipoib_dev_priv *priv = netdev_priv(dev);
1468 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1469 struct ipoib_neigh_hash *htbl;
1470 unsigned long flags;
1471 int i;
1472
1473 /* remove all neigh connected to a given path or mcast */
1474 spin_lock_irqsave(&priv->lock, flags);
1475
1476 htbl = rcu_dereference_protected(ntbl->htbl,
1477 lockdep_is_held(&priv->lock));
1478
1479 if (!htbl)
1480 goto out_unlock;
1481
1482 for (i = 0; i < htbl->size; i++) {
1483 struct ipoib_neigh *neigh;
1484 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1485
1486 while ((neigh = rcu_dereference_protected(*np,
1487 lockdep_is_held(&priv->lock))) != NULL) {
1488 /* delete neighs belong to this parent */
1489 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1490 rcu_assign_pointer(*np,
1491 rcu_dereference_protected(neigh->hnext,
1492 lockdep_is_held(&priv->lock)));
1493 /* remove from parent list */
1494 list_del_init(&neigh->list);
1495 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1496 } else {
1497 np = &neigh->hnext;
1498 }
1499
1500 }
1501 }
1502out_unlock:
1503 spin_unlock_irqrestore(&priv->lock, flags);
1504}
1505
1506static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1507{
1508 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1509 struct ipoib_neigh_hash *htbl;
1510 unsigned long flags;
1511 int i, wait_flushed = 0;
1512
1513 init_completion(&priv->ntbl.flushed);
1514
1515 spin_lock_irqsave(&priv->lock, flags);
1516
1517 htbl = rcu_dereference_protected(ntbl->htbl,
1518 lockdep_is_held(&priv->lock));
1519 if (!htbl)
1520 goto out_unlock;
1521
1522 wait_flushed = atomic_read(&priv->ntbl.entries);
1523 if (!wait_flushed)
1524 goto free_htbl;
1525
1526 for (i = 0; i < htbl->size; i++) {
1527 struct ipoib_neigh *neigh;
1528 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1529
1530 while ((neigh = rcu_dereference_protected(*np,
1531 lockdep_is_held(&priv->lock))) != NULL) {
1532 rcu_assign_pointer(*np,
1533 rcu_dereference_protected(neigh->hnext,
1534 lockdep_is_held(&priv->lock)));
1535 /* remove from path/mc list */
1536 list_del_init(&neigh->list);
1537 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1538 }
1539 }
1540
1541free_htbl:
1542 rcu_assign_pointer(ntbl->htbl, NULL);
1543 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1544
1545out_unlock:
1546 spin_unlock_irqrestore(&priv->lock, flags);
1547 if (wait_flushed)
1548 wait_for_completion(&priv->ntbl.flushed);
1549}
1550
1551static void ipoib_neigh_hash_uninit(struct net_device *dev)
1552{
1553 struct ipoib_dev_priv *priv = netdev_priv(dev);
1554 int stopped;
1555
1556 ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
1557 init_completion(&priv->ntbl.deleted);
1558 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1559
1560 /* Stop GC if called at init fail need to cancel work */
1561 stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1562 if (!stopped)
1563 cancel_delayed_work(&priv->neigh_reap_task);
1564
1565 ipoib_flush_neighs(priv);
1566
1567 wait_for_completion(&priv->ntbl.deleted);
1568}
1569
1570
1571int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1572{
1573 struct ipoib_dev_priv *priv = netdev_priv(dev);
1574
1575 /* Allocate RX/TX "rings" to hold queued skbs */
1576 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
1577 GFP_KERNEL);
1578 if (!priv->rx_ring) {
1579 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
1580 ca->name, ipoib_recvq_size);
1581 goto out;
1582 }
1583
1584 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
1585 if (!priv->tx_ring) {
1586 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
1587 ca->name, ipoib_sendq_size);
1588 goto out_rx_ring_cleanup;
1589 }
1590
1591 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
1592
1593 if (ipoib_ib_dev_init(dev, ca, port))
1594 goto out_tx_ring_cleanup;
1595
1596 /*
1597 * Must be after ipoib_ib_dev_init so we can allocate a per
1598 * device wq there and use it here
1599 */
1600 if (ipoib_neigh_hash_init(priv) < 0)
1601 goto out_dev_uninit;
1602
1603 return 0;
1604
1605out_dev_uninit:
1606 ipoib_ib_dev_cleanup(dev);
1607
1608out_tx_ring_cleanup:
1609 vfree(priv->tx_ring);
1610
1611out_rx_ring_cleanup:
1612 kfree(priv->rx_ring);
1613
1614out:
1615 return -ENOMEM;
1616}
1617
1618void ipoib_dev_cleanup(struct net_device *dev)
1619{
1620 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
1621 LIST_HEAD(head);
1622
1623 ASSERT_RTNL();
1624
1625 /* Delete any child interfaces first */
1626 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
1627 /* Stop GC on child */
1628 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1629 cancel_delayed_work(&cpriv->neigh_reap_task);
1630 unregister_netdevice_queue(cpriv->dev, &head);
1631 }
1632 unregister_netdevice_many(&head);
1633
1634 /*
1635 * Must be before ipoib_ib_dev_cleanup or we delete an in use
1636 * work queue
1637 */
1638 ipoib_neigh_hash_uninit(dev);
1639
1640 ipoib_ib_dev_cleanup(dev);
1641
1642 kfree(priv->rx_ring);
1643 vfree(priv->tx_ring);
1644
1645 priv->rx_ring = NULL;
1646 priv->tx_ring = NULL;
1647}
1648
1649static const struct header_ops ipoib_header_ops = {
1650 .create = ipoib_hard_header,
1651};
1652
1653static const struct net_device_ops ipoib_netdev_ops = {
1654 .ndo_uninit = ipoib_uninit,
1655 .ndo_open = ipoib_open,
1656 .ndo_stop = ipoib_stop,
1657 .ndo_change_mtu = ipoib_change_mtu,
1658 .ndo_fix_features = ipoib_fix_features,
1659 .ndo_start_xmit = ipoib_start_xmit,
1660 .ndo_tx_timeout = ipoib_timeout,
1661 .ndo_set_rx_mode = ipoib_set_mcast_list,
1662 .ndo_get_iflink = ipoib_get_iflink,
1663};
1664
1665void ipoib_setup(struct net_device *dev)
1666{
1667 struct ipoib_dev_priv *priv = netdev_priv(dev);
1668
1669 dev->netdev_ops = &ipoib_netdev_ops;
1670 dev->header_ops = &ipoib_header_ops;
1671
1672 ipoib_set_ethtool_ops(dev);
1673
1674 netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT);
1675
1676 dev->watchdog_timeo = HZ;
1677
1678 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1679
1680 dev->hard_header_len = IPOIB_HARD_LEN;
1681 dev->addr_len = INFINIBAND_ALEN;
1682 dev->type = ARPHRD_INFINIBAND;
1683 dev->tx_queue_len = ipoib_sendq_size * 2;
1684 dev->features = (NETIF_F_VLAN_CHALLENGED |
1685 NETIF_F_HIGHDMA);
1686 netif_keep_dst(dev);
1687
1688 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
1689
1690 priv->dev = dev;
1691
1692 spin_lock_init(&priv->lock);
1693
1694 init_rwsem(&priv->vlan_rwsem);
1695
1696 INIT_LIST_HEAD(&priv->path_list);
1697 INIT_LIST_HEAD(&priv->child_intfs);
1698 INIT_LIST_HEAD(&priv->dead_ahs);
1699 INIT_LIST_HEAD(&priv->multicast_list);
1700
1701 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
1702 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
1703 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
1704 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
1705 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
1706 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1707 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
1708 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
1709}
1710
1711struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
1712{
1713 struct net_device *dev;
1714
1715 dev = alloc_netdev((int)sizeof(struct ipoib_dev_priv), name,
1716 NET_NAME_UNKNOWN, ipoib_setup);
1717 if (!dev)
1718 return NULL;
1719
1720 return netdev_priv(dev);
1721}
1722
1723static ssize_t show_pkey(struct device *dev,
1724 struct device_attribute *attr, char *buf)
1725{
1726 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1727
1728 return sprintf(buf, "0x%04x\n", priv->pkey);
1729}
1730static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1731
1732static ssize_t show_umcast(struct device *dev,
1733 struct device_attribute *attr, char *buf)
1734{
1735 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1736
1737 return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1738}
1739
1740void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
1741{
1742 struct ipoib_dev_priv *priv = netdev_priv(ndev);
1743
1744 if (umcast_val > 0) {
1745 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1746 ipoib_warn(priv, "ignoring multicast groups joined directly "
1747 "by userspace\n");
1748 } else
1749 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1750}
1751
1752static ssize_t set_umcast(struct device *dev,
1753 struct device_attribute *attr,
1754 const char *buf, size_t count)
1755{
1756 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1757
1758 ipoib_set_umcast(to_net_dev(dev), umcast_val);
1759
1760 return count;
1761}
1762static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1763
1764int ipoib_add_umcast_attr(struct net_device *dev)
1765{
1766 return device_create_file(&dev->dev, &dev_attr_umcast);
1767}
1768
1769static ssize_t create_child(struct device *dev,
1770 struct device_attribute *attr,
1771 const char *buf, size_t count)
1772{
1773 int pkey;
1774 int ret;
1775
1776 if (sscanf(buf, "%i", &pkey) != 1)
1777 return -EINVAL;
1778
1779 if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
1780 return -EINVAL;
1781
1782 /*
1783 * Set the full membership bit, so that we join the right
1784 * broadcast group, etc.
1785 */
1786 pkey |= 0x8000;
1787
1788 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
1789
1790 return ret ? ret : count;
1791}
1792static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
1793
1794static ssize_t delete_child(struct device *dev,
1795 struct device_attribute *attr,
1796 const char *buf, size_t count)
1797{
1798 int pkey;
1799 int ret;
1800
1801 if (sscanf(buf, "%i", &pkey) != 1)
1802 return -EINVAL;
1803
1804 if (pkey < 0 || pkey > 0xffff)
1805 return -EINVAL;
1806
1807 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
1808
1809 return ret ? ret : count;
1810
1811}
1812static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
1813
1814int ipoib_add_pkey_attr(struct net_device *dev)
1815{
1816 return device_create_file(&dev->dev, &dev_attr_pkey);
1817}
1818
1819int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
1820{
1821 struct ib_device_attr *device_attr;
1822 int result = -ENOMEM;
1823
1824 device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
1825 if (!device_attr) {
1826 printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
1827 hca->name, sizeof *device_attr);
1828 return result;
1829 }
1830
1831 result = ib_query_device(hca, device_attr);
1832 if (result) {
1833 printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
1834 hca->name, result);
1835 kfree(device_attr);
1836 return result;
1837 }
1838 priv->hca_caps = device_attr->device_cap_flags;
1839
1840 kfree(device_attr);
1841
1842 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
1843 priv->dev->hw_features = NETIF_F_SG |
1844 NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1845
1846 if (priv->hca_caps & IB_DEVICE_UD_TSO)
1847 priv->dev->hw_features |= NETIF_F_TSO;
1848
1849 priv->dev->features |= priv->dev->hw_features;
1850 }
1851
1852 return 0;
1853}
1854
1855static struct net_device *ipoib_add_port(const char *format,
1856 struct ib_device *hca, u8 port)
1857{
1858 struct ipoib_dev_priv *priv;
1859 struct ib_port_attr attr;
1860 int result = -ENOMEM;
1861
1862 priv = ipoib_intf_alloc(format);
1863 if (!priv)
1864 goto alloc_mem_failed;
1865
1866 SET_NETDEV_DEV(priv->dev, hca->dma_device);
1867 priv->dev->dev_id = port - 1;
1868
1869 result = ib_query_port(hca, port, &attr);
1870 if (!result)
1871 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1872 else {
1873 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
1874 hca->name, port);
1875 goto device_init_failed;
1876 }
1877
1878 /* MTU will be reset when mcast join happens */
1879 priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
1880 priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
1881
1882 priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
1883
1884 result = ib_query_pkey(hca, port, 0, &priv->pkey);
1885 if (result) {
1886 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1887 hca->name, port, result);
1888 goto device_init_failed;
1889 }
1890
1891 result = ipoib_set_dev_features(priv, hca);
1892 if (result)
1893 goto device_init_failed;
1894
1895 /*
1896 * Set the full membership bit, so that we join the right
1897 * broadcast group, etc.
1898 */
1899 priv->pkey |= 0x8000;
1900
1901 priv->dev->broadcast[8] = priv->pkey >> 8;
1902 priv->dev->broadcast[9] = priv->pkey & 0xff;
1903
1904 result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
1905 if (result) {
1906 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1907 hca->name, port, result);
1908 goto device_init_failed;
1909 } else
1910 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1911
1912 result = ipoib_dev_init(priv->dev, hca, port);
1913 if (result < 0) {
1914 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1915 hca->name, port, result);
1916 goto device_init_failed;
1917 }
1918
1919 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1920 priv->ca, ipoib_event);
1921 result = ib_register_event_handler(&priv->event_handler);
1922 if (result < 0) {
1923 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1924 "port %d (ret = %d)\n",
1925 hca->name, port, result);
1926 goto event_failed;
1927 }
1928
1929 result = register_netdev(priv->dev);
1930 if (result) {
1931 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1932 hca->name, port, result);
1933 goto register_failed;
1934 }
1935
1936 if (ipoib_cm_add_mode_attr(priv->dev))
1937 goto sysfs_failed;
1938 if (ipoib_add_pkey_attr(priv->dev))
1939 goto sysfs_failed;
1940 if (ipoib_add_umcast_attr(priv->dev))
1941 goto sysfs_failed;
1942 if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
1943 goto sysfs_failed;
1944 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
1945 goto sysfs_failed;
1946
1947 return priv->dev;
1948
1949sysfs_failed:
1950 unregister_netdev(priv->dev);
1951
1952register_failed:
1953 ib_unregister_event_handler(&priv->event_handler);
1954 flush_workqueue(ipoib_workqueue);
1955 /* Stop GC if started before flush */
1956 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1957 cancel_delayed_work(&priv->neigh_reap_task);
1958 flush_workqueue(priv->wq);
1959
1960event_failed:
1961 ipoib_dev_cleanup(priv->dev);
1962
1963device_init_failed:
1964 free_netdev(priv->dev);
1965
1966alloc_mem_failed:
1967 return ERR_PTR(result);
1968}
1969
1970static void ipoib_add_one(struct ib_device *device)
1971{
1972 struct list_head *dev_list;
1973 struct net_device *dev;
1974 struct ipoib_dev_priv *priv;
1975 int p;
1976 int count = 0;
1977
1978 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1979 if (!dev_list)
1980 return;
1981
1982 INIT_LIST_HEAD(dev_list);
1983
1984 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
1985 if (!rdma_protocol_ib(device, p))
1986 continue;
1987 dev = ipoib_add_port("ib%d", device, p);
1988 if (!IS_ERR(dev)) {
1989 priv = netdev_priv(dev);
1990 list_add_tail(&priv->list, dev_list);
1991 count++;
1992 }
1993 }
1994
1995 if (!count) {
1996 kfree(dev_list);
1997 return;
1998 }
1999
2000 ib_set_client_data(device, &ipoib_client, dev_list);
2001}
2002
2003static void ipoib_remove_one(struct ib_device *device, void *client_data)
2004{
2005 struct ipoib_dev_priv *priv, *tmp;
2006 struct list_head *dev_list = client_data;
2007
2008 if (!dev_list)
2009 return;
2010
2011 list_for_each_entry_safe(priv, tmp, dev_list, list) {
2012 ib_unregister_event_handler(&priv->event_handler);
2013 flush_workqueue(ipoib_workqueue);
2014
2015 rtnl_lock();
2016 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
2017 rtnl_unlock();
2018
2019 /* Stop GC */
2020 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
2021 cancel_delayed_work(&priv->neigh_reap_task);
2022 flush_workqueue(priv->wq);
2023
2024 unregister_netdev(priv->dev);
2025 free_netdev(priv->dev);
2026 }
2027
2028 kfree(dev_list);
2029}
2030
2031#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2032static struct notifier_block ipoib_netdev_notifier = {
2033 .notifier_call = ipoib_netdev_event,
2034};
2035#endif
2036
2037static int __init ipoib_init_module(void)
2038{
2039 int ret;
2040
2041 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
2042 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
2043 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
2044
2045 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
2046 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
2047 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
2048#ifdef CONFIG_INFINIBAND_IPOIB_CM
2049 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
2050#endif
2051
2052 /*
2053 * When copying small received packets, we only copy from the
2054 * linear data part of the SKB, so we rely on this condition.
2055 */
2056 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
2057
2058 ret = ipoib_register_debugfs();
2059 if (ret)
2060 return ret;
2061
2062 /*
2063 * We create a global workqueue here that is used for all flush
2064 * operations. However, if you attempt to flush a workqueue
2065 * from a task on that same workqueue, it deadlocks the system.
2066 * We want to be able to flush the tasks associated with a
2067 * specific net device, so we also create a workqueue for each
2068 * netdevice. We queue up the tasks for that device only on
2069 * its private workqueue, and we only queue up flush events
2070 * on our global flush workqueue. This avoids the deadlocks.
2071 */
2072 ipoib_workqueue = create_singlethread_workqueue("ipoib_flush");
2073 if (!ipoib_workqueue) {
2074 ret = -ENOMEM;
2075 goto err_fs;
2076 }
2077
2078 ib_sa_register_client(&ipoib_sa_client);
2079
2080 ret = ib_register_client(&ipoib_client);
2081 if (ret)
2082 goto err_sa;
2083
2084 ret = ipoib_netlink_init();
2085 if (ret)
2086 goto err_client;
2087
2088#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2089 register_netdevice_notifier(&ipoib_netdev_notifier);
2090#endif
2091 return 0;
2092
2093err_client:
2094 ib_unregister_client(&ipoib_client);
2095
2096err_sa:
2097 ib_sa_unregister_client(&ipoib_sa_client);
2098 destroy_workqueue(ipoib_workqueue);
2099
2100err_fs:
2101 ipoib_unregister_debugfs();
2102
2103 return ret;
2104}
2105
2106static void __exit ipoib_cleanup_module(void)
2107{
2108#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2109 unregister_netdevice_notifier(&ipoib_netdev_notifier);
2110#endif
2111 ipoib_netlink_fini();
2112 ib_unregister_client(&ipoib_client);
2113 ib_sa_unregister_client(&ipoib_sa_client);
2114 ipoib_unregister_debugfs();
2115 destroy_workqueue(ipoib_workqueue);
2116}
2117
2118module_init(ipoib_init_module);
2119module_exit(ipoib_cleanup_module);