blob: 8bf1859ebed4fb7dd1e211ba9ddf7f6db44d5b9b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07003 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35#include "ipoib.h"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/module.h>
38
39#include <linux/init.h>
40#include <linux/slab.h>
Shirley Ma0f485252006-04-10 09:43:58 -070041#include <linux/kernel.h>
Roland Dreier10313cb2008-03-12 07:51:03 -070042#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <linux/if_arp.h> /* For ARPHRD_xxx */
45
46#include <linux/ip.h>
47#include <linux/in.h>
48
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +000049#include <linux/jhash.h>
50#include <net/arp.h>
Guy Shapiroddde8962015-07-30 17:50:16 +030051#include <net/addrconf.h>
52#include <linux/inetdevice.h>
53#include <rdma/ib_cache.h>
Eli Cohen9c3c5f82016-03-11 22:58:39 +020054#include <linux/pci.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020055
Yan Burman4b486802013-02-19 15:40:23 +000056#define DRV_VERSION "1.0.0"
57
58const char ipoib_driver_version[] = DRV_VERSION;
59
Linus Torvalds1da177e2005-04-16 15:20:36 -070060MODULE_AUTHOR("Roland Dreier");
61MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
62MODULE_LICENSE("Dual BSD/GPL");
Yan Burman4b486802013-02-19 15:40:23 +000063MODULE_VERSION(DRV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -070064
Shirley Ma0f485252006-04-10 09:43:58 -070065int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
66int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
67
68module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
69MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
70module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
71MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
74int ipoib_debug_level;
75
76module_param_named(debug_level, ipoib_debug_level, int, 0644);
77MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
78#endif
79
Roland Dreier1732b0e2005-11-07 10:33:11 -080080struct ipoib_path_iter {
81 struct net_device *dev;
82 struct ipoib_path path;
83};
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static const u8 ipv4_bcast_addr[] = {
86 0x00, 0xff, 0xff, 0xff,
87 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
88 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
89};
90
91struct workqueue_struct *ipoib_workqueue;
92
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070093struct ib_sa_client ipoib_sa_client;
94
Linus Torvalds1da177e2005-04-16 15:20:36 -070095static void ipoib_add_one(struct ib_device *device);
Haggai Eran7c1eb452015-07-30 17:50:14 +030096static void ipoib_remove_one(struct ib_device *device, void *client_data);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +000097static void ipoib_neigh_reclaim(struct rcu_head *rp);
Guy Shapiroddde8962015-07-30 17:50:16 +030098static struct net_device *ipoib_get_net_dev_by_params(
99 struct ib_device *dev, u8 port, u16 pkey,
100 const union ib_gid *gid, const struct sockaddr *addr,
101 void *client_data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103static struct ib_client ipoib_client = {
104 .name = "ipoib",
105 .add = ipoib_add_one,
Guy Shapiroddde8962015-07-30 17:50:16 +0300106 .remove = ipoib_remove_one,
107 .get_net_dev_by_params = ipoib_get_net_dev_by_params,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
110int ipoib_open(struct net_device *dev)
111{
112 struct ipoib_dev_priv *priv = netdev_priv(dev);
113
114 ipoib_dbg(priv, "bringing up interface\n");
115
Michal Schmidt437708c2014-01-17 19:47:25 +0100116 netif_carrier_off(dev);
117
Yossi Etigine028cc52009-04-20 13:58:08 -0700118 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
Erez Shitrit3b561132016-05-25 22:02:07 +0300120 priv->sm_fullmember_sendonly_support = false;
121
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500122 if (ipoib_ib_dev_open(dev)) {
Alex Estrindd57c932014-08-06 14:40:32 -0400123 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
124 return 0;
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800125 goto err_disable;
Alex Estrindd57c932014-08-06 14:40:32 -0400126 }
Yossi Etiginfe25c562008-11-12 10:24:36 -0800127
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800128 if (ipoib_ib_dev_up(dev))
129 goto err_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
132 struct ipoib_dev_priv *cpriv;
133
134 /* Bring up any child interfaces too */
Erez Shitritf47944c2013-10-16 17:37:49 +0300135 down_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 list_for_each_entry(cpriv, &priv->child_intfs, list) {
137 int flags;
138
139 flags = cpriv->dev->flags;
140 if (flags & IFF_UP)
141 continue;
142
143 dev_change_flags(cpriv->dev, flags | IFF_UP);
144 }
Erez Shitritf47944c2013-10-16 17:37:49 +0300145 up_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 }
147
148 netif_start_queue(dev);
149
150 return 0;
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800151
152err_stop:
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500153 ipoib_ib_dev_stop(dev);
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800154
155err_disable:
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800156 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
157
158 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161static int ipoib_stop(struct net_device *dev)
162{
163 struct ipoib_dev_priv *priv = netdev_priv(dev);
164
165 ipoib_dbg(priv, "stopping interface\n");
166
167 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
168
169 netif_stop_queue(dev);
170
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500171 ipoib_ib_dev_down(dev);
172 ipoib_ib_dev_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173
174 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
175 struct ipoib_dev_priv *cpriv;
176
177 /* Bring down any child interfaces too */
Erez Shitritf47944c2013-10-16 17:37:49 +0300178 down_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 list_for_each_entry(cpriv, &priv->child_intfs, list) {
180 int flags;
181
182 flags = cpriv->dev->flags;
183 if (!(flags & IFF_UP))
184 continue;
185
186 dev_change_flags(cpriv->dev, flags & ~IFF_UP);
187 }
Erez Shitritf47944c2013-10-16 17:37:49 +0300188 up_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190
191 return 0;
192}
193
Or Gerlitz9baa0b02012-09-13 05:56:36 +0000194static void ipoib_uninit(struct net_device *dev)
195{
196 ipoib_dev_cleanup(dev);
197}
198
David S. Miller9ca36f72011-11-16 18:05:50 -0500199static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
Michał Mirosław3d96c742011-04-19 00:43:20 +0000200{
201 struct ipoib_dev_priv *priv = netdev_priv(dev);
202
203 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
Yuval Shaiac4268772015-07-12 01:24:09 -0700204 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
Michał Mirosław3d96c742011-04-19 00:43:20 +0000205
206 return features;
207}
208
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
210{
211 struct ipoib_dev_priv *priv = netdev_priv(dev);
212
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200213 /* dev->mtu > 2K ==> connected mode */
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800214 if (ipoib_cm_admin_enabled(dev)) {
215 if (new_mtu > ipoib_cm_max_mtu(dev))
216 return -EINVAL;
217
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200218 if (new_mtu > priv->mcast_mtu)
219 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
220 priv->mcast_mtu);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800221
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200222 dev->mtu = new_mtu;
223 return 0;
224 }
225
Shirley Mabc7b3a32008-04-23 11:55:45 -0700226 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 return -EINVAL;
228
229 priv->admin_mtu = new_mtu;
230
231 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
232
233 return 0;
234}
235
Guy Shapiroddde8962015-07-30 17:50:16 +0300236/* Called with an RCU read lock taken */
237static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr,
238 struct net_device *dev)
239{
240 struct net *net = dev_net(dev);
241 struct in_device *in_dev;
242 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
243 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
244 __be32 ret_addr;
245
246 switch (addr->sa_family) {
247 case AF_INET:
248 in_dev = in_dev_get(dev);
249 if (!in_dev)
250 return false;
251
252 ret_addr = inet_confirm_addr(net, in_dev, 0,
253 addr_in->sin_addr.s_addr,
254 RT_SCOPE_HOST);
255 in_dev_put(in_dev);
256 if (ret_addr)
257 return true;
258
259 break;
260 case AF_INET6:
261 if (IS_ENABLED(CONFIG_IPV6) &&
262 ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1))
263 return true;
264
265 break;
266 }
267 return false;
268}
269
270/**
271 * Find the master net_device on top of the given net_device.
272 * @dev: base IPoIB net_device
273 *
274 * Returns the master net_device with a reference held, or the same net_device
275 * if no master exists.
276 */
277static struct net_device *ipoib_get_master_net_dev(struct net_device *dev)
278{
279 struct net_device *master;
280
281 rcu_read_lock();
282 master = netdev_master_upper_dev_get_rcu(dev);
283 if (master)
284 dev_hold(master);
285 rcu_read_unlock();
286
287 if (master)
288 return master;
289
290 dev_hold(dev);
291 return dev;
292}
293
294/**
295 * Find a net_device matching the given address, which is an upper device of
296 * the given net_device.
297 * @addr: IP address to look for.
298 * @dev: base IPoIB net_device
299 *
300 * If found, returns the net_device with a reference held. Otherwise return
301 * NULL.
302 */
303static struct net_device *ipoib_get_net_dev_match_addr(
304 const struct sockaddr *addr, struct net_device *dev)
305{
306 struct net_device *upper,
307 *result = NULL;
308 struct list_head *iter;
309
310 rcu_read_lock();
311 if (ipoib_is_dev_match_addr_rcu(addr, dev)) {
312 dev_hold(dev);
313 result = dev;
314 goto out;
315 }
316
317 netdev_for_each_all_upper_dev_rcu(dev, upper, iter) {
318 if (ipoib_is_dev_match_addr_rcu(addr, upper)) {
319 dev_hold(upper);
320 result = upper;
321 break;
322 }
323 }
324out:
325 rcu_read_unlock();
326 return result;
327}
328
329/* returns the number of IPoIB netdevs on top a given ipoib device matching a
330 * pkey_index and address, if one exists.
331 *
332 * @found_net_dev: contains a matching net_device if the return value >= 1,
333 * with a reference held. */
334static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
335 const union ib_gid *gid,
336 u16 pkey_index,
337 const struct sockaddr *addr,
338 int nesting,
339 struct net_device **found_net_dev)
340{
341 struct ipoib_dev_priv *child_priv;
342 struct net_device *net_dev = NULL;
343 int matches = 0;
344
345 if (priv->pkey_index == pkey_index &&
346 (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
347 if (!addr) {
348 net_dev = ipoib_get_master_net_dev(priv->dev);
349 } else {
350 /* Verify the net_device matches the IP address, as
351 * IPoIB child devices currently share a GID. */
352 net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev);
353 }
354 if (net_dev) {
355 if (!*found_net_dev)
356 *found_net_dev = net_dev;
357 else
358 dev_put(net_dev);
359 ++matches;
360 }
361 }
362
363 /* Check child interfaces */
364 down_read_nested(&priv->vlan_rwsem, nesting);
365 list_for_each_entry(child_priv, &priv->child_intfs, list) {
366 matches += ipoib_match_gid_pkey_addr(child_priv, gid,
367 pkey_index, addr,
368 nesting + 1,
369 found_net_dev);
370 if (matches > 1)
371 break;
372 }
373 up_read(&priv->vlan_rwsem);
374
375 return matches;
376}
377
378/* Returns the number of matching net_devs found (between 0 and 2). Also
379 * return the matching net_device in the @net_dev parameter, holding a
380 * reference to the net_device, if the number of matches >= 1 */
381static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port,
382 u16 pkey_index,
383 const union ib_gid *gid,
384 const struct sockaddr *addr,
385 struct net_device **net_dev)
386{
387 struct ipoib_dev_priv *priv;
388 int matches = 0;
389
390 *net_dev = NULL;
391
392 list_for_each_entry(priv, dev_list, list) {
393 if (priv->port != port)
394 continue;
395
396 matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
397 addr, 0, net_dev);
398 if (matches > 1)
399 break;
400 }
401
402 return matches;
403}
404
405static struct net_device *ipoib_get_net_dev_by_params(
406 struct ib_device *dev, u8 port, u16 pkey,
407 const union ib_gid *gid, const struct sockaddr *addr,
408 void *client_data)
409{
410 struct net_device *net_dev;
411 struct list_head *dev_list = client_data;
412 u16 pkey_index;
413 int matches;
414 int ret;
415
416 if (!rdma_protocol_ib(dev, port))
417 return NULL;
418
419 ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
420 if (ret)
421 return NULL;
422
423 if (!dev_list)
424 return NULL;
425
426 /* See if we can find a unique device matching the L2 parameters */
427 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
428 gid, NULL, &net_dev);
429
430 switch (matches) {
431 case 0:
432 return NULL;
433 case 1:
434 return net_dev;
435 }
436
437 dev_put(net_dev);
438
439 /* Couldn't find a unique device with L2 parameters only. Use L3
440 * address to uniquely match the net device */
441 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
442 gid, addr, &net_dev);
443 switch (matches) {
444 case 0:
445 return NULL;
446 default:
447 dev_warn_ratelimited(&dev->dev,
448 "duplicate IP address detected\n");
449 /* Fall through */
450 case 1:
451 return net_dev;
452 }
453}
454
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700455int ipoib_set_mode(struct net_device *dev, const char *buf)
456{
457 struct ipoib_dev_priv *priv = netdev_priv(dev);
458
459 /* flush paths if we switch modes so that connections are restarted */
460 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
461 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
462 ipoib_warn(priv, "enabling connected mode "
463 "will cause multicast packet drops\n");
464 netdev_update_features(dev);
Erez Shitritedcd2a72015-06-07 13:36:11 +0300465 dev_set_mtu(dev, ipoib_cm_max_mtu(dev));
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700466 rtnl_unlock();
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100467 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700468
469 ipoib_flush_paths(dev);
470 rtnl_lock();
471 return 0;
472 }
473
474 if (!strcmp(buf, "datagram\n")) {
475 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
476 netdev_update_features(dev);
477 dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
478 rtnl_unlock();
479 ipoib_flush_paths(dev);
480 rtnl_lock();
481 return 0;
482 }
483
484 return -EINVAL;
485}
486
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300487static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488{
489 struct ipoib_dev_priv *priv = netdev_priv(dev);
490 struct rb_node *n = priv->path_tree.rb_node;
491 struct ipoib_path *path;
492 int ret;
493
494 while (n) {
495 path = rb_entry(n, struct ipoib_path, rb_node);
496
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300497 ret = memcmp(gid, path->pathrec.dgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 sizeof (union ib_gid));
499
500 if (ret < 0)
501 n = n->rb_left;
502 else if (ret > 0)
503 n = n->rb_right;
504 else
505 return path;
506 }
507
508 return NULL;
509}
510
511static int __path_add(struct net_device *dev, struct ipoib_path *path)
512{
513 struct ipoib_dev_priv *priv = netdev_priv(dev);
514 struct rb_node **n = &priv->path_tree.rb_node;
515 struct rb_node *pn = NULL;
516 struct ipoib_path *tpath;
517 int ret;
518
519 while (*n) {
520 pn = *n;
521 tpath = rb_entry(pn, struct ipoib_path, rb_node);
522
523 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
524 sizeof (union ib_gid));
525 if (ret < 0)
526 n = &pn->rb_left;
527 else if (ret > 0)
528 n = &pn->rb_right;
529 else
530 return -EEXIST;
531 }
532
533 rb_link_node(&path->rb_node, pn, n);
534 rb_insert_color(&path->rb_node, &priv->path_tree);
535
536 list_add_tail(&path->list, &priv->path_list);
537
538 return 0;
539}
540
541static void path_free(struct net_device *dev, struct ipoib_path *path)
542{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545 while ((skb = __skb_dequeue(&path->queue)))
546 dev_kfree_skb_irq(skb);
547
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000548 ipoib_dbg(netdev_priv(dev), "path_free\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000550 /* remove all neigh connected to this path */
551 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 if (path->ah)
554 ipoib_put_ah(path->ah);
555
556 kfree(path);
557}
558
Roland Dreier1732b0e2005-11-07 10:33:11 -0800559#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
560
561struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
562{
563 struct ipoib_path_iter *iter;
564
565 iter = kmalloc(sizeof *iter, GFP_KERNEL);
566 if (!iter)
567 return NULL;
568
569 iter->dev = dev;
570 memset(iter->path.pathrec.dgid.raw, 0, 16);
571
572 if (ipoib_path_iter_next(iter)) {
573 kfree(iter);
574 return NULL;
575 }
576
577 return iter;
578}
579
580int ipoib_path_iter_next(struct ipoib_path_iter *iter)
581{
582 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
583 struct rb_node *n;
584 struct ipoib_path *path;
585 int ret = 1;
586
587 spin_lock_irq(&priv->lock);
588
589 n = rb_first(&priv->path_tree);
590
591 while (n) {
592 path = rb_entry(n, struct ipoib_path, rb_node);
593
594 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
595 sizeof (union ib_gid)) < 0) {
596 iter->path = *path;
597 ret = 0;
598 break;
599 }
600
601 n = rb_next(n);
602 }
603
604 spin_unlock_irq(&priv->lock);
605
606 return ret;
607}
608
609void ipoib_path_iter_read(struct ipoib_path_iter *iter,
610 struct ipoib_path *path)
611{
612 *path = iter->path;
613}
614
615#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
616
Moni Shouaee1e2c82008-07-14 23:48:49 -0700617void ipoib_mark_paths_invalid(struct net_device *dev)
618{
619 struct ipoib_dev_priv *priv = netdev_priv(dev);
620 struct ipoib_path *path, *tp;
621
622 spin_lock_irq(&priv->lock);
623
624 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700625 ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
Moni Shouaee1e2c82008-07-14 23:48:49 -0700626 be16_to_cpu(path->pathrec.dlid),
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700627 path->pathrec.dgid.raw);
Moni Shouaee1e2c82008-07-14 23:48:49 -0700628 path->valid = 0;
629 }
630
631 spin_unlock_irq(&priv->lock);
632}
633
Erez Shitrit3b561132016-05-25 22:02:07 +0300634struct classport_info_context {
635 struct ipoib_dev_priv *priv;
636 struct completion done;
637 struct ib_sa_query *sa_query;
638};
639
640static void classport_info_query_cb(int status, struct ib_class_port_info *rec,
641 void *context)
642{
643 struct classport_info_context *cb_ctx = context;
644 struct ipoib_dev_priv *priv;
645
646 WARN_ON(!context);
647
648 priv = cb_ctx->priv;
649
650 if (status || !rec) {
651 pr_debug("device: %s failed query classport_info status: %d\n",
652 priv->dev->name, status);
653 /* keeps the default, will try next mcast_restart */
654 priv->sm_fullmember_sendonly_support = false;
655 goto out;
656 }
657
658 if (ib_get_cpi_capmask2(rec) &
659 IB_SA_CAP_MASK2_SENDONLY_FULL_MEM_SUPPORT) {
660 pr_debug("device: %s enabled fullmember-sendonly for sendonly MCG\n",
661 priv->dev->name);
662 priv->sm_fullmember_sendonly_support = true;
663 } else {
664 pr_debug("device: %s disabled fullmember-sendonly for sendonly MCG\n",
665 priv->dev->name);
666 priv->sm_fullmember_sendonly_support = false;
667 }
668
669out:
670 complete(&cb_ctx->done);
671}
672
673int ipoib_check_sm_sendonly_fullmember_support(struct ipoib_dev_priv *priv)
674{
675 struct classport_info_context *callback_context;
676 int ret;
677
678 callback_context = kmalloc(sizeof(*callback_context), GFP_KERNEL);
679 if (!callback_context)
680 return -ENOMEM;
681
682 callback_context->priv = priv;
683 init_completion(&callback_context->done);
684
685 ret = ib_sa_classport_info_rec_query(&ipoib_sa_client,
686 priv->ca, priv->port, 3000,
687 GFP_KERNEL,
688 classport_info_query_cb,
689 callback_context,
690 &callback_context->sa_query);
691 if (ret < 0) {
692 pr_info("%s failed to send ib_sa_classport_info query, ret: %d\n",
693 priv->dev->name, ret);
694 kfree(callback_context);
695 return ret;
696 }
697
698 /* waiting for the callback to finish before returnning */
699 wait_for_completion(&callback_context->done);
700 kfree(callback_context);
701
702 return ret;
703}
704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705void ipoib_flush_paths(struct net_device *dev)
706{
707 struct ipoib_dev_priv *priv = netdev_priv(dev);
708 struct ipoib_path *path, *tp;
709 LIST_HEAD(remove_list);
Roland Dreier943c2462008-09-30 10:36:21 -0700710 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Roland Dreier943c2462008-09-30 10:36:21 -0700712 netif_tx_lock_bh(dev);
713 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Robert P. J. Day157de222008-04-16 21:09:26 -0700715 list_splice_init(&priv->path_list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717 list_for_each_entry(path, &remove_list, list)
718 rb_erase(&path->rb_node, &priv->path_tree);
719
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720 list_for_each_entry_safe(path, tp, &remove_list, list) {
721 if (path->query)
722 ib_sa_cancel_query(path->query_id, path->query);
Roland Dreier943c2462008-09-30 10:36:21 -0700723 spin_unlock_irqrestore(&priv->lock, flags);
724 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 wait_for_completion(&path->done);
726 path_free(dev, path);
Roland Dreier943c2462008-09-30 10:36:21 -0700727 netif_tx_lock_bh(dev);
728 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 }
Roland Dreier943c2462008-09-30 10:36:21 -0700730
731 spin_unlock_irqrestore(&priv->lock, flags);
732 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733}
734
735static void path_rec_completion(int status,
736 struct ib_sa_path_rec *pathrec,
737 void *path_ptr)
738{
739 struct ipoib_path *path = path_ptr;
740 struct net_device *dev = path->dev;
741 struct ipoib_dev_priv *priv = netdev_priv(dev);
742 struct ipoib_ah *ah = NULL;
Roland Dreierc9da4ba2008-09-25 15:26:15 -0700743 struct ipoib_ah *old_ah = NULL;
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700744 struct ipoib_neigh *neigh, *tn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 struct sk_buff_head skqueue;
746 struct sk_buff *skb;
747 unsigned long flags;
748
Roland Dreier843613b2007-02-26 12:57:08 -0800749 if (!status)
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700750 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700751 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 else
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700753 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700754 status, path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
756 skb_queue_head_init(&skqueue);
757
758 if (!status) {
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700759 struct ib_ah_attr av;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700761 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
762 ah = ipoib_create_ah(dev, priv->pd, &av);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764
765 spin_lock_irqsave(&priv->lock, flags);
766
Mike Marciniszyn38743972011-11-21 08:43:54 -0500767 if (!IS_ERR_OR_NULL(ah)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 path->pathrec = *pathrec;
769
Roland Dreierc9da4ba2008-09-25 15:26:15 -0700770 old_ah = path->ah;
771 path->ah = ah;
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
774 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
775
776 while ((skb = __skb_dequeue(&path->queue)))
777 __skb_queue_tail(&skqueue, skb);
778
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700779 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
Moni Shouaee1e2c82008-07-14 23:48:49 -0700780 if (neigh->ah) {
781 WARN_ON(neigh->ah != old_ah);
782 /*
783 * Dropping the ah reference inside
784 * priv->lock is safe here, because we
785 * will hold one more reference from
786 * the original value of path->ah (ie
787 * old_ah).
788 */
789 ipoib_put_ah(neigh->ah);
790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 kref_get(&path->ah->ref);
792 neigh->ah = path->ah;
793
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000794 if (ipoib_cm_enabled(dev, neigh->daddr)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200795 if (!ipoib_cm_get(neigh))
796 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
797 path,
798 neigh));
799 if (!ipoib_cm_get(neigh)) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000800 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200801 continue;
802 }
803 }
804
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 while ((skb = __skb_dequeue(&neigh->queue)))
806 __skb_queue_tail(&skqueue, skb);
807 }
Moni Shouaee1e2c82008-07-14 23:48:49 -0700808 path->valid = 1;
Roland Dreier5872a9f2005-11-29 10:13:54 -0800809 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
Roland Dreier5872a9f2005-11-29 10:13:54 -0800811 path->query = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 complete(&path->done);
813
814 spin_unlock_irqrestore(&priv->lock, flags);
815
Roland Dreierf72dd562013-02-25 09:42:15 -0800816 if (IS_ERR_OR_NULL(ah))
817 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
818
Moni Shouaee1e2c82008-07-14 23:48:49 -0700819 if (old_ah)
820 ipoib_put_ah(old_ah);
821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 while ((skb = __skb_dequeue(&skqueue))) {
823 skb->dev = dev;
824 if (dev_queue_xmit(skb))
825 ipoib_warn(priv, "dev_queue_xmit failed "
826 "to requeue packet\n");
827 }
828}
829
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300830static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
832 struct ipoib_dev_priv *priv = netdev_priv(dev);
833 struct ipoib_path *path;
834
Jack Morgenstein1401b532007-11-26 10:41:19 +0200835 if (!priv->broadcast)
836 return NULL;
837
Roland Dreier21a38482005-11-02 10:07:59 -0800838 path = kzalloc(sizeof *path, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 if (!path)
840 return NULL;
841
Roland Dreier21a38482005-11-02 10:07:59 -0800842 path->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843
844 skb_queue_head_init(&path->queue);
845
846 INIT_LIST_HEAD(&path->neigh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300848 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
Roland Dreier2337f802007-10-23 19:57:54 -0700849 path->pathrec.sgid = priv->local_gid;
850 path->pathrec.pkey = cpu_to_be16(priv->pkey);
Sean Hefty81668832007-08-02 12:21:31 -0700851 path->pathrec.numb_path = 1;
852 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853
854 return path;
855}
856
857static int path_rec_start(struct net_device *dev,
858 struct ipoib_path *path)
859{
860 struct ipoib_dev_priv *priv = netdev_priv(dev);
861
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700862 ipoib_dbg(priv, "Start path record lookup for %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700863 path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Roland Dreier65c7edd2005-11-28 21:20:34 -0800865 init_completion(&path->done);
866
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 path->query_id =
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700868 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869 &path->pathrec,
870 IB_SA_PATH_REC_DGID |
871 IB_SA_PATH_REC_SGID |
872 IB_SA_PATH_REC_NUMB_PATH |
Sean Hefty81668832007-08-02 12:21:31 -0700873 IB_SA_PATH_REC_TRAFFIC_CLASS |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874 IB_SA_PATH_REC_PKEY,
875 1000, GFP_ATOMIC,
876 path_rec_completion,
877 path, &path->query);
878 if (path->query_id < 0) {
Or Gerlitz01b3fc82008-07-22 14:18:34 -0700879 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880 path->query = NULL;
Yossi Etigin93a3ab92008-11-12 10:24:38 -0800881 complete(&path->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 return path->query_id;
883 }
884
885 return 0;
886}
887
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000888static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
889 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890{
891 struct ipoib_dev_priv *priv = netdev_priv(dev);
892 struct ipoib_path *path;
893 struct ipoib_neigh *neigh;
Roland Dreier943c2462008-09-30 10:36:21 -0700894 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000896 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000897 neigh = ipoib_neigh_alloc(daddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 if (!neigh) {
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000899 spin_unlock_irqrestore(&priv->lock, flags);
Roland Dreierde903512007-09-28 15:33:51 -0700900 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700901 dev_kfree_skb_any(skb);
902 return;
903 }
904
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000905 path = __path_find(dev, daddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906 if (!path) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000907 path = path_rec_create(dev, daddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (!path)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300909 goto err_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700910
911 __path_add(dev, path);
912 }
913
914 list_add_tail(&neigh->list, &path->neigh_list);
915
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -0800916 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 kref_get(&path->ah->ref);
918 neigh->ah = path->ah;
919
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000920 if (ipoib_cm_enabled(dev, neigh->daddr)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200921 if (!ipoib_cm_get(neigh))
922 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
923 if (!ipoib_cm_get(neigh)) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000924 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200925 goto err_drop;
926 }
927 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
928 __skb_queue_tail(&neigh->queue, skb);
929 else {
930 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
931 skb_queue_len(&neigh->queue));
932 goto err_drop;
933 }
Roland Dreier721d67c2009-09-05 20:23:40 -0700934 } else {
935 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000936 ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
937 ipoib_neigh_put(neigh);
Roland Dreier721d67c2009-09-05 20:23:40 -0700938 return;
939 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 } else {
941 neigh->ah = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942
943 if (!path->query && path_rec_start(dev, path))
Jim Foraker49b8e742013-08-08 17:44:22 -0700944 goto err_path;
Erez Shitrit1e85b802015-04-02 13:39:03 +0300945 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
946 __skb_queue_tail(&neigh->queue, skb);
947 else
948 goto err_drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 }
950
Roland Dreier943c2462008-09-30 10:36:21 -0700951 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000952 ipoib_neigh_put(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 return;
954
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300955err_path:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000956 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200957err_drop:
Roland Dreierde903512007-09-28 15:33:51 -0700958 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959 dev_kfree_skb_any(skb);
960
Roland Dreier943c2462008-09-30 10:36:21 -0700961 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000962 ipoib_neigh_put(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963}
964
965static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
Roland Dreier936d7de2012-02-07 14:51:21 +0000966 struct ipoib_cb *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967{
968 struct ipoib_dev_priv *priv = netdev_priv(dev);
969 struct ipoib_path *path;
Roland Dreier943c2462008-09-30 10:36:21 -0700970 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Roland Dreier943c2462008-09-30 10:36:21 -0700972 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973
Roland Dreier936d7de2012-02-07 14:51:21 +0000974 path = __path_find(dev, cb->hwaddr + 4);
Moni Shouaee1e2c82008-07-14 23:48:49 -0700975 if (!path || !path->valid) {
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800976 int new_path = 0;
977
978 if (!path) {
Roland Dreier936d7de2012-02-07 14:51:21 +0000979 path = path_rec_create(dev, cb->hwaddr + 4);
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800980 new_path = 1;
981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 if (path) {
Erez Shitrit1e85b802015-04-02 13:39:03 +0300983 if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
984 __skb_queue_tail(&path->queue, skb);
985 } else {
986 ++dev->stats.tx_dropped;
987 dev_kfree_skb_any(skb);
988 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989
Yossi Etiginff79ae82008-11-12 10:24:39 -0800990 if (!path->query && path_rec_start(dev, path)) {
Roland Dreier943c2462008-09-30 10:36:21 -0700991 spin_unlock_irqrestore(&priv->lock, flags);
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800992 if (new_path)
993 path_free(dev, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return;
995 } else
996 __path_add(dev, path);
997 } else {
Roland Dreierde903512007-09-28 15:33:51 -0700998 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 dev_kfree_skb_any(skb);
1000 }
1001
Roland Dreier943c2462008-09-30 10:36:21 -07001002 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001003 return;
1004 }
1005
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -08001006 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
1008 be16_to_cpu(path->pathrec.dlid));
1009
Roland Dreier721d67c2009-09-05 20:23:40 -07001010 spin_unlock_irqrestore(&priv->lock, flags);
Roland Dreier936d7de2012-02-07 14:51:21 +00001011 ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr));
Roland Dreier721d67c2009-09-05 20:23:40 -07001012 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 } else if ((path->query || !path_rec_start(dev, path)) &&
1014 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 __skb_queue_tail(&path->queue, skb);
1016 } else {
Roland Dreierde903512007-09-28 15:33:51 -07001017 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 dev_kfree_skb_any(skb);
1019 }
1020
Roland Dreier943c2462008-09-30 10:36:21 -07001021 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
1024static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
1025{
1026 struct ipoib_dev_priv *priv = netdev_priv(dev);
1027 struct ipoib_neigh *neigh;
Eric Dumazetb49fe362014-09-18 11:00:27 -07001028 struct ipoib_cb *cb = ipoib_skb_cb(skb);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001029 struct ipoib_header *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 unsigned long flags;
1031
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001032 header = (struct ipoib_header *) skb->data;
1033
1034 if (unlikely(cb->hwaddr[4] == 0xff)) {
1035 /* multicast, arrange "if" according to probability */
1036 if ((header->proto != htons(ETH_P_IP)) &&
1037 (header->proto != htons(ETH_P_IPV6)) &&
1038 (header->proto != htons(ETH_P_ARP)) &&
Patrick McHardydc850b02013-04-17 06:18:29 +00001039 (header->proto != htons(ETH_P_RARP)) &&
1040 (header->proto != htons(ETH_P_TIPC))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001041 /* ethertype not supported by IPoIB */
David Miller17e6abe2011-12-02 16:52:44 +00001042 ++dev->stats.tx_dropped;
1043 dev_kfree_skb_any(skb);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001044 return NETDEV_TX_OK;
David Miller17e6abe2011-12-02 16:52:44 +00001045 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001046 /* Add in the P_Key for multicast*/
1047 cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
1048 cb->hwaddr[9] = priv->pkey & 0xff;
1049
1050 neigh = ipoib_neigh_get(dev, cb->hwaddr);
1051 if (likely(neigh))
1052 goto send_using_neigh;
1053 ipoib_mcast_send(dev, cb->hwaddr, skb);
1054 return NETDEV_TX_OK;
David Miller17e6abe2011-12-02 16:52:44 +00001055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001057 /* unicast, arrange "switch" according to probability */
1058 switch (header->proto) {
1059 case htons(ETH_P_IP):
1060 case htons(ETH_P_IPV6):
Patrick McHardydc850b02013-04-17 06:18:29 +00001061 case htons(ETH_P_TIPC):
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001062 neigh = ipoib_neigh_get(dev, cb->hwaddr);
1063 if (unlikely(!neigh)) {
1064 neigh_add_path(skb, cb->hwaddr, dev);
1065 return NETDEV_TX_OK;
Yossi Etigina50df392009-01-09 14:05:11 -08001066 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001067 break;
1068 case htons(ETH_P_ARP):
1069 case htons(ETH_P_RARP):
1070 /* for unicast ARP and RARP should always perform path find */
1071 unicast_arp_send(skb, dev, cb);
1072 return NETDEV_TX_OK;
1073 default:
1074 /* ethertype not supported by IPoIB */
1075 ++dev->stats.tx_dropped;
1076 dev_kfree_skb_any(skb);
1077 return NETDEV_TX_OK;
1078 }
Michael S. Tsirkin8a7f7522006-07-19 17:44:37 +03001079
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001080send_using_neigh:
1081 /* note we now hold a ref to neigh */
1082 if (ipoib_cm_get(neigh)) {
1083 if (ipoib_cm_up(neigh)) {
1084 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
1085 goto unref;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001087 } else if (neigh->ah) {
1088 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
1089 goto unref;
1090 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001092 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1093 spin_lock_irqsave(&priv->lock, flags);
1094 __skb_queue_tail(&neigh->queue, skb);
1095 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 } else {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001097 ++dev->stats.tx_dropped;
1098 dev_kfree_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001100
1101unref:
1102 ipoib_neigh_put(neigh);
1103
Linus Torvalds1da177e2005-04-16 15:20:36 -07001104 return NETDEV_TX_OK;
1105}
1106
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107static void ipoib_timeout(struct net_device *dev)
1108{
1109 struct ipoib_dev_priv *priv = netdev_priv(dev);
1110
Roland Dreier4b2d3192005-10-18 12:20:06 -07001111 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
1112 jiffies_to_msecs(jiffies - dev->trans_start));
1113 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
1114 netif_queue_stopped(dev),
1115 priv->tx_head, priv->tx_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 /* XXX reset QP, etc. */
1117}
1118
1119static int ipoib_hard_header(struct sk_buff *skb,
1120 struct net_device *dev,
1121 unsigned short type,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001122 const void *daddr, const void *saddr, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123{
1124 struct ipoib_header *header;
Eric Dumazetb49fe362014-09-18 11:00:27 -07001125 struct ipoib_cb *cb = ipoib_skb_cb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
1128
1129 header->proto = htons(type);
1130 header->reserved = 0;
1131
1132 /*
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001133 * we don't rely on dst_entry structure, always stuff the
Roland Dreier936d7de2012-02-07 14:51:21 +00001134 * destination address into skb->cb so we can figure out where
1135 * to send the packet later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001137 memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138
Doug Ledford83bdd3b2013-04-01 21:25:30 +00001139 return sizeof *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140}
1141
1142static void ipoib_set_mcast_list(struct net_device *dev)
1143{
1144 struct ipoib_dev_priv *priv = netdev_priv(dev);
1145
Leonid Arsh7a343d42006-03-23 19:52:51 +02001146 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
1147 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
1148 return;
1149 }
1150
Doug Ledford0b395782015-02-21 19:27:03 -05001151 queue_work(priv->wq, &priv->restart_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001152}
1153
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +02001154static int ipoib_get_iflink(const struct net_device *dev)
1155{
1156 struct ipoib_dev_priv *priv = netdev_priv(dev);
1157
Erez Shitrit2c153952015-04-16 16:34:34 +03001158 /* parent interface */
1159 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
1160 return dev->ifindex;
1161
1162 /* child/vlan interface */
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +02001163 return priv->parent->ifindex;
1164}
1165
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001166static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001167{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001168 /*
1169 * Use only the address parts that contributes to spreading
1170 * The subnet prefix is not used as one can not connect to
1171 * same remote port (GUID) using the same remote QPN via two
1172 * different subnets.
1173 */
1174 /* qpn octets[1:4) & port GUID octets[12:20) */
Shlomo Pongratz9d1ad662013-02-19 15:40:22 +00001175 u32 *d32 = (u32 *) daddr;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001176 u32 hv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177
Shlomo Pongratz9d1ad662013-02-19 15:40:22 +00001178 hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001179 return hv & htbl->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001180}
1181
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001182struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
1183{
1184 struct ipoib_dev_priv *priv = netdev_priv(dev);
1185 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1186 struct ipoib_neigh_hash *htbl;
1187 struct ipoib_neigh *neigh = NULL;
1188 u32 hash_val;
1189
1190 rcu_read_lock_bh();
1191
1192 htbl = rcu_dereference_bh(ntbl->htbl);
1193
1194 if (!htbl)
1195 goto out_unlock;
1196
1197 hash_val = ipoib_addr_hash(htbl, daddr);
1198 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
1199 neigh != NULL;
1200 neigh = rcu_dereference_bh(neigh->hnext)) {
1201 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1202 /* found, take one ref on behalf of the caller */
1203 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1204 /* deleted */
1205 neigh = NULL;
1206 goto out_unlock;
1207 }
1208 neigh->alive = jiffies;
1209 goto out_unlock;
1210 }
1211 }
1212
1213out_unlock:
1214 rcu_read_unlock_bh();
1215 return neigh;
1216}
1217
1218static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
1219{
1220 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1221 struct ipoib_neigh_hash *htbl;
1222 unsigned long neigh_obsolete;
1223 unsigned long dt;
1224 unsigned long flags;
1225 int i;
Christoph Lameterbd99b2e2015-09-24 12:00:05 -05001226 LIST_HEAD(remove_list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001227
1228 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1229 return;
1230
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001231 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001232
1233 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001234 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001235
1236 if (!htbl)
1237 goto out_unlock;
1238
1239 /* neigh is obsolete if it was idle for two GC periods */
1240 dt = 2 * arp_tbl.gc_interval;
1241 neigh_obsolete = jiffies - dt;
1242 /* handle possible race condition */
1243 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1244 goto out_unlock;
1245
1246 for (i = 0; i < htbl->size; i++) {
1247 struct ipoib_neigh *neigh;
1248 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1249
1250 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001251 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001252 /* was the neigh idle for two GC periods */
1253 if (time_after(neigh_obsolete, neigh->alive)) {
Christoph Lameterbd99b2e2015-09-24 12:00:05 -05001254
Christoph Lameter432c55f2015-12-21 08:42:54 -06001255 ipoib_check_and_add_mcast_sendonly(priv, neigh->daddr + 4, &remove_list);
Christoph Lameterbd99b2e2015-09-24 12:00:05 -05001256
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001257 rcu_assign_pointer(*np,
1258 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001259 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001260 /* remove from path/mc list */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001261 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001262 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1263 } else {
1264 np = &neigh->hnext;
1265 }
1266
1267 }
1268 }
1269
1270out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001271 spin_unlock_irqrestore(&priv->lock, flags);
Erez Shitrit50be28d2016-01-07 09:28:08 +02001272 ipoib_mcast_remove_list(&remove_list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001273}
1274
1275static void ipoib_reap_neigh(struct work_struct *work)
1276{
1277 struct ipoib_dev_priv *priv =
1278 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
1279
1280 __ipoib_reap_neigh(priv);
1281
1282 if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
Doug Ledford0b395782015-02-21 19:27:03 -05001283 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001284 arp_tbl.gc_interval);
1285}
1286
1287
1288static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
Moni Shoua732a2172007-10-09 19:43:36 -07001289 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001290{
1291 struct ipoib_neigh *neigh;
1292
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001293 neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001294 if (!neigh)
1295 return NULL;
1296
Moni Shoua732a2172007-10-09 19:43:36 -07001297 neigh->dev = dev;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001298 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
Roland Dreier82b39912006-12-12 14:48:18 -08001299 skb_queue_head_init(&neigh->queue);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001300 INIT_LIST_HEAD(&neigh->list);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001301 ipoib_cm_set(neigh, NULL);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001302 /* one ref on behalf of the caller */
1303 atomic_set(&neigh->refcnt, 1);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001304
1305 return neigh;
1306}
1307
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001308struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
1309 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001310{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001311 struct ipoib_dev_priv *priv = netdev_priv(dev);
1312 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1313 struct ipoib_neigh_hash *htbl;
1314 struct ipoib_neigh *neigh;
1315 u32 hash_val;
1316
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001317 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001318 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001319 if (!htbl) {
1320 neigh = NULL;
1321 goto out_unlock;
1322 }
1323
1324 /* need to add a new neigh, but maybe some other thread succeeded?
1325 * recalc hash, maybe hash resize took place so we do a search
1326 */
1327 hash_val = ipoib_addr_hash(htbl, daddr);
1328 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001329 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001330 neigh != NULL;
1331 neigh = rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001332 lockdep_is_held(&priv->lock))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001333 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1334 /* found, take one ref on behalf of the caller */
1335 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1336 /* deleted */
1337 neigh = NULL;
1338 break;
1339 }
1340 neigh->alive = jiffies;
1341 goto out_unlock;
1342 }
1343 }
1344
1345 neigh = ipoib_neigh_ctor(daddr, dev);
1346 if (!neigh)
1347 goto out_unlock;
1348
1349 /* one ref on behalf of the hash table */
1350 atomic_inc(&neigh->refcnt);
1351 neigh->alive = jiffies;
1352 /* put in hash */
1353 rcu_assign_pointer(neigh->hnext,
1354 rcu_dereference_protected(htbl->buckets[hash_val],
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001355 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001356 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1357 atomic_inc(&ntbl->entries);
1358
1359out_unlock:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001360
1361 return neigh;
1362}
1363
1364void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1365{
1366 /* neigh reference count was dropprd to zero */
1367 struct net_device *dev = neigh->dev;
1368 struct ipoib_dev_priv *priv = netdev_priv(dev);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001369 struct sk_buff *skb;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001370 if (neigh->ah)
1371 ipoib_put_ah(neigh->ah);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001372 while ((skb = __skb_dequeue(&neigh->queue))) {
Roland Dreierde903512007-09-28 15:33:51 -07001373 ++dev->stats.tx_dropped;
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001374 dev_kfree_skb_any(skb);
1375 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001376 if (ipoib_cm_get(neigh))
1377 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001378 ipoib_dbg(netdev_priv(dev),
1379 "neigh free for %06x %pI6\n",
1380 IPOIB_QPN(neigh->daddr),
1381 neigh->daddr + 4);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001382 kfree(neigh);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001383 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1384 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1385 complete(&priv->ntbl.flushed);
1386 }
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001387}
1388
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001389static void ipoib_neigh_reclaim(struct rcu_head *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001391 /* Called as a result of removal from hash table */
1392 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1393 /* note TX context may hold another ref */
1394 ipoib_neigh_put(neigh);
1395}
1396
1397void ipoib_neigh_free(struct ipoib_neigh *neigh)
1398{
1399 struct net_device *dev = neigh->dev;
1400 struct ipoib_dev_priv *priv = netdev_priv(dev);
1401 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1402 struct ipoib_neigh_hash *htbl;
1403 struct ipoib_neigh __rcu **np;
1404 struct ipoib_neigh *n;
1405 u32 hash_val;
1406
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001407 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001408 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001409 if (!htbl)
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001410 return;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001411
1412 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1413 np = &htbl->buckets[hash_val];
1414 for (n = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001415 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001416 n != NULL;
Shlomo Pongratz6c723a62012-08-13 14:39:50 +00001417 n = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001418 lockdep_is_held(&priv->lock))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001419 if (n == neigh) {
1420 /* found */
1421 rcu_assign_pointer(*np,
1422 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001423 lockdep_is_held(&priv->lock)));
Jim Foraker49b8e742013-08-08 17:44:22 -07001424 /* remove from parent list */
1425 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001426 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001427 return;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001428 } else {
1429 np = &n->hnext;
1430 }
1431 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001432}
1433
1434static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1435{
1436 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1437 struct ipoib_neigh_hash *htbl;
Bart Van Assche52374962015-05-26 15:03:48 +02001438 struct ipoib_neigh __rcu **buckets;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001439 u32 size;
1440
1441 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1442 ntbl->htbl = NULL;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001443 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1444 if (!htbl)
1445 return -ENOMEM;
1446 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1447 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1448 buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1449 if (!buckets) {
1450 kfree(htbl);
1451 return -ENOMEM;
1452 }
1453 htbl->size = size;
1454 htbl->mask = (size - 1);
1455 htbl->buckets = buckets;
Bart Van Assche52374962015-05-26 15:03:48 +02001456 RCU_INIT_POINTER(ntbl->htbl, htbl);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001457 htbl->ntbl = ntbl;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001458 atomic_set(&ntbl->entries, 0);
1459
1460 /* start garbage collection */
1461 clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
Doug Ledford0b395782015-02-21 19:27:03 -05001462 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001463 arp_tbl.gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 return 0;
1466}
1467
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001468static void neigh_hash_free_rcu(struct rcu_head *head)
1469{
1470 struct ipoib_neigh_hash *htbl = container_of(head,
1471 struct ipoib_neigh_hash,
1472 rcu);
1473 struct ipoib_neigh __rcu **buckets = htbl->buckets;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001474 struct ipoib_neigh_table *ntbl = htbl->ntbl;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001475
1476 kfree(buckets);
1477 kfree(htbl);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001478 complete(&ntbl->deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001479}
1480
1481void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1482{
1483 struct ipoib_dev_priv *priv = netdev_priv(dev);
1484 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1485 struct ipoib_neigh_hash *htbl;
1486 unsigned long flags;
1487 int i;
1488
1489 /* remove all neigh connected to a given path or mcast */
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001490 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001491
1492 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001493 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001494
1495 if (!htbl)
1496 goto out_unlock;
1497
1498 for (i = 0; i < htbl->size; i++) {
1499 struct ipoib_neigh *neigh;
1500 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1501
1502 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001503 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001504 /* delete neighs belong to this parent */
1505 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1506 rcu_assign_pointer(*np,
1507 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001508 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001509 /* remove from parent list */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001510 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001511 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1512 } else {
1513 np = &neigh->hnext;
1514 }
1515
1516 }
1517 }
1518out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001519 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001520}
1521
1522static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1523{
1524 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1525 struct ipoib_neigh_hash *htbl;
1526 unsigned long flags;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001527 int i, wait_flushed = 0;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001528
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001529 init_completion(&priv->ntbl.flushed);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001530
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001531 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001532
1533 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001534 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001535 if (!htbl)
1536 goto out_unlock;
1537
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001538 wait_flushed = atomic_read(&priv->ntbl.entries);
1539 if (!wait_flushed)
1540 goto free_htbl;
1541
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001542 for (i = 0; i < htbl->size; i++) {
1543 struct ipoib_neigh *neigh;
1544 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1545
1546 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001547 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001548 rcu_assign_pointer(*np,
1549 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001550 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001551 /* remove from path/mc list */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001552 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001553 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1554 }
1555 }
1556
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001557free_htbl:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001558 rcu_assign_pointer(ntbl->htbl, NULL);
1559 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1560
1561out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001562 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001563 if (wait_flushed)
1564 wait_for_completion(&priv->ntbl.flushed);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001565}
1566
1567static void ipoib_neigh_hash_uninit(struct net_device *dev)
1568{
1569 struct ipoib_dev_priv *priv = netdev_priv(dev);
1570 int stopped;
1571
1572 ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001573 init_completion(&priv->ntbl.deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001574 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1575
1576 /* Stop GC if called at init fail need to cancel work */
1577 stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1578 if (!stopped)
1579 cancel_delayed_work(&priv->neigh_reap_task);
1580
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001581 ipoib_flush_neighs(priv);
1582
1583 wait_for_completion(&priv->ntbl.deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001584}
1585
1586
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1588{
1589 struct ipoib_dev_priv *priv = netdev_priv(dev);
1590
1591 /* Allocate RX/TX "rings" to hold queued skbs */
Shirley Ma0f485252006-04-10 09:43:58 -07001592 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001593 GFP_KERNEL);
1594 if (!priv->rx_ring) {
1595 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
Shirley Ma0f485252006-04-10 09:43:58 -07001596 ca->name, ipoib_recvq_size);
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001597 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001598 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001599
Joe Perches948579c2010-11-05 03:07:36 +00001600 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 if (!priv->tx_ring) {
1602 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
Shirley Ma0f485252006-04-10 09:43:58 -07001603 ca->name, ipoib_sendq_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604 goto out_rx_ring_cleanup;
1605 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001607 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608
1609 if (ipoib_ib_dev_init(dev, ca, port))
1610 goto out_tx_ring_cleanup;
1611
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001612 /*
1613 * Must be after ipoib_ib_dev_init so we can allocate a per
1614 * device wq there and use it here
1615 */
1616 if (ipoib_neigh_hash_init(priv) < 0)
1617 goto out_dev_uninit;
1618
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 return 0;
1620
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001621out_dev_uninit:
1622 ipoib_ib_dev_cleanup(dev);
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624out_tx_ring_cleanup:
Roland Dreier10313cb2008-03-12 07:51:03 -07001625 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626
1627out_rx_ring_cleanup:
1628 kfree(priv->rx_ring);
1629
1630out:
1631 return -ENOMEM;
1632}
1633
1634void ipoib_dev_cleanup(struct net_device *dev)
1635{
1636 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001637 LIST_HEAD(head);
1638
1639 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Roland Dreier1732b0e2005-11-07 10:33:11 -08001641 ipoib_delete_debug_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 /* Delete any child interfaces first */
1644 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001645 /* Stop GC on child */
1646 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1647 cancel_delayed_work(&cpriv->neigh_reap_task);
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001648 unregister_netdevice_queue(cpriv->dev, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649 }
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001650 unregister_netdevice_many(&head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001652 /*
1653 * Must be before ipoib_ib_dev_cleanup or we delete an in use
1654 * work queue
1655 */
1656 ipoib_neigh_hash_uninit(dev);
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658 ipoib_ib_dev_cleanup(dev);
1659
Hal Rosenstock92a6b342005-08-13 20:50:27 -07001660 kfree(priv->rx_ring);
Roland Dreier10313cb2008-03-12 07:51:03 -07001661 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662
Hal Rosenstock92a6b342005-08-13 20:50:27 -07001663 priv->rx_ring = NULL;
1664 priv->tx_ring = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665}
1666
Eli Cohen9c3c5f82016-03-11 22:58:39 +02001667static int ipoib_set_vf_link_state(struct net_device *dev, int vf, int link_state)
1668{
1669 struct ipoib_dev_priv *priv = netdev_priv(dev);
1670
1671 return ib_set_vf_link_state(priv->ca, vf, priv->port, link_state);
1672}
1673
1674static int ipoib_get_vf_config(struct net_device *dev, int vf,
1675 struct ifla_vf_info *ivf)
1676{
1677 struct ipoib_dev_priv *priv = netdev_priv(dev);
1678 int err;
1679
1680 err = ib_get_vf_config(priv->ca, vf, priv->port, ivf);
1681 if (err)
1682 return err;
1683
1684 ivf->vf = vf;
1685
1686 return 0;
1687}
1688
1689static int ipoib_set_vf_guid(struct net_device *dev, int vf, u64 guid, int type)
1690{
1691 struct ipoib_dev_priv *priv = netdev_priv(dev);
1692
1693 if (type != IFLA_VF_IB_NODE_GUID && type != IFLA_VF_IB_PORT_GUID)
1694 return -EINVAL;
1695
1696 return ib_set_vf_guid(priv->ca, vf, priv->port, guid, type);
1697}
1698
1699static int ipoib_get_vf_stats(struct net_device *dev, int vf,
1700 struct ifla_vf_stats *vf_stats)
1701{
1702 struct ipoib_dev_priv *priv = netdev_priv(dev);
1703
1704 return ib_get_vf_stats(priv->ca, vf, priv->port, vf_stats);
1705}
1706
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001707static const struct header_ops ipoib_header_ops = {
1708 .create = ipoib_hard_header,
1709};
1710
Eli Cohen9c3c5f82016-03-11 22:58:39 +02001711static const struct net_device_ops ipoib_netdev_ops_pf = {
1712 .ndo_uninit = ipoib_uninit,
1713 .ndo_open = ipoib_open,
1714 .ndo_stop = ipoib_stop,
1715 .ndo_change_mtu = ipoib_change_mtu,
1716 .ndo_fix_features = ipoib_fix_features,
1717 .ndo_start_xmit = ipoib_start_xmit,
1718 .ndo_tx_timeout = ipoib_timeout,
1719 .ndo_set_rx_mode = ipoib_set_mcast_list,
1720 .ndo_get_iflink = ipoib_get_iflink,
1721 .ndo_set_vf_link_state = ipoib_set_vf_link_state,
1722 .ndo_get_vf_config = ipoib_get_vf_config,
1723 .ndo_get_vf_stats = ipoib_get_vf_stats,
1724 .ndo_set_vf_guid = ipoib_set_vf_guid,
1725};
1726
1727static const struct net_device_ops ipoib_netdev_ops_vf = {
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001728 .ndo_uninit = ipoib_uninit,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001729 .ndo_open = ipoib_open,
1730 .ndo_stop = ipoib_stop,
1731 .ndo_change_mtu = ipoib_change_mtu,
Michał Mirosław3d96c742011-04-19 00:43:20 +00001732 .ndo_fix_features = ipoib_fix_features,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001733 .ndo_start_xmit = ipoib_start_xmit,
1734 .ndo_tx_timeout = ipoib_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001735 .ndo_set_rx_mode = ipoib_set_mcast_list,
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +02001736 .ndo_get_iflink = ipoib_get_iflink,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001737};
1738
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001739void ipoib_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740{
1741 struct ipoib_dev_priv *priv = netdev_priv(dev);
1742
Eli Cohen9c3c5f82016-03-11 22:58:39 +02001743 if (priv->hca_caps & IB_DEVICE_VIRTUAL_FUNCTION)
1744 dev->netdev_ops = &ipoib_netdev_ops_vf;
1745 else
1746 dev->netdev_ops = &ipoib_netdev_ops_pf;
1747
Roland Dreier2337f802007-10-23 19:57:54 -07001748 dev->header_ops = &ipoib_header_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001749
Eli Cohen82c24c12008-04-16 21:09:32 -07001750 ipoib_set_ethtool_ops(dev);
1751
Michal Schmidt7f1a3862013-08-21 18:50:22 +02001752 netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
Roland Dreier2337f802007-10-23 19:57:54 -07001754 dev->watchdog_timeo = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Roland Dreier2337f802007-10-23 19:57:54 -07001756 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757
Roland Dreier936d7de2012-02-07 14:51:21 +00001758 dev->hard_header_len = IPOIB_ENCAP_LEN;
Roland Dreier2337f802007-10-23 19:57:54 -07001759 dev->addr_len = INFINIBAND_ALEN;
1760 dev->type = ARPHRD_INFINIBAND;
1761 dev->tx_queue_len = ipoib_sendq_size * 2;
Eli Coheneb14032f2008-01-30 18:30:46 +02001762 dev->features = (NETIF_F_VLAN_CHALLENGED |
Eli Coheneb14032f2008-01-30 18:30:46 +02001763 NETIF_F_HIGHDMA);
Eric Dumazet02875872014-10-05 18:38:35 -07001764 netif_keep_dst(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 priv->dev = dev;
1769
1770 spin_lock_init(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
Erez Shitritf47944c2013-10-16 17:37:49 +03001772 init_rwsem(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773
1774 INIT_LIST_HEAD(&priv->path_list);
1775 INIT_LIST_HEAD(&priv->child_intfs);
1776 INIT_LIST_HEAD(&priv->dead_ahs);
1777 INIT_LIST_HEAD(&priv->multicast_list);
1778
David Howellsc4028952006-11-22 14:57:56 +00001779 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
Yossi Etigine8224e42008-09-16 11:57:45 -07001780 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
Moni Shouaee1e2c82008-07-14 23:48:49 -07001781 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
1782 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
1783 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
David Howellsc4028952006-11-22 14:57:56 +00001784 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1785 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001786 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787}
1788
1789struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
1790{
1791 struct net_device *dev;
1792
Tom Gundersenc835a672014-07-14 16:37:24 +02001793 dev = alloc_netdev((int)sizeof(struct ipoib_dev_priv), name,
1794 NET_NAME_UNKNOWN, ipoib_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 if (!dev)
1796 return NULL;
1797
1798 return netdev_priv(dev);
1799}
1800
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001801static ssize_t show_pkey(struct device *dev,
1802 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001804 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805
1806 return sprintf(buf, "0x%04x\n", priv->pkey);
1807}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001808static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001810static ssize_t show_umcast(struct device *dev,
1811 struct device_attribute *attr, char *buf)
1812{
1813 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1814
1815 return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1816}
1817
Or Gerlitz862096a2012-09-27 12:06:02 +00001818void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001819{
Or Gerlitz862096a2012-09-27 12:06:02 +00001820 struct ipoib_dev_priv *priv = netdev_priv(ndev);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001821
1822 if (umcast_val > 0) {
1823 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1824 ipoib_warn(priv, "ignoring multicast groups joined directly "
1825 "by userspace\n");
1826 } else
1827 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
Or Gerlitz862096a2012-09-27 12:06:02 +00001828}
1829
1830static ssize_t set_umcast(struct device *dev,
1831 struct device_attribute *attr,
1832 const char *buf, size_t count)
1833{
1834 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1835
1836 ipoib_set_umcast(to_net_dev(dev), umcast_val);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001837
1838 return count;
1839}
1840static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1841
1842int ipoib_add_umcast_attr(struct net_device *dev)
1843{
1844 return device_create_file(&dev->dev, &dev_attr_umcast);
1845}
1846
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001847static ssize_t create_child(struct device *dev,
1848 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 const char *buf, size_t count)
1850{
1851 int pkey;
1852 int ret;
1853
1854 if (sscanf(buf, "%i", &pkey) != 1)
1855 return -EINVAL;
1856
Or Gerlitz3d790a42013-07-18 14:02:31 +03001857 if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 return -EINVAL;
1859
Roland Dreier4ce05932005-08-19 12:03:17 -07001860 /*
1861 * Set the full membership bit, so that we join the right
1862 * broadcast group, etc.
1863 */
1864 pkey |= 0x8000;
1865
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001866 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867
1868 return ret ? ret : count;
1869}
Or Gerlitz7a52b342010-06-06 04:59:16 +00001870static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001872static ssize_t delete_child(struct device *dev,
1873 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001874 const char *buf, size_t count)
1875{
1876 int pkey;
1877 int ret;
1878
1879 if (sscanf(buf, "%i", &pkey) != 1)
1880 return -EINVAL;
1881
1882 if (pkey < 0 || pkey > 0xffff)
1883 return -EINVAL;
1884
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001885 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001886
1887 return ret ? ret : count;
1888
1889}
Or Gerlitz7a52b342010-06-06 04:59:16 +00001890static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001891
1892int ipoib_add_pkey_attr(struct net_device *dev)
1893{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001894 return device_create_file(&dev->dev, &dev_attr_pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895}
1896
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001897int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
1898{
Or Gerlitz4a061b22015-12-18 10:59:46 +02001899 priv->hca_caps = hca->attrs.device_cap_flags;
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001900
1901 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
Michał Mirosław3d96c742011-04-19 00:43:20 +00001902 priv->dev->hw_features = NETIF_F_SG |
1903 NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1904
1905 if (priv->hca_caps & IB_DEVICE_UD_TSO)
1906 priv->dev->hw_features |= NETIF_F_TSO;
1907
1908 priv->dev->features |= priv->dev->hw_features;
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001909 }
1910
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001911 return 0;
1912}
1913
Linus Torvalds1da177e2005-04-16 15:20:36 -07001914static struct net_device *ipoib_add_port(const char *format,
1915 struct ib_device *hca, u8 port)
1916{
1917 struct ipoib_dev_priv *priv;
Shirley Mabc7b3a32008-04-23 11:55:45 -07001918 struct ib_port_attr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 int result = -ENOMEM;
1920
1921 priv = ipoib_intf_alloc(format);
1922 if (!priv)
1923 goto alloc_mem_failed;
1924
1925 SET_NETDEV_DEV(priv->dev, hca->dma_device);
Eli Cohenc3aa9b12010-09-20 07:05:06 +00001926 priv->dev->dev_id = port - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927
Amir Vadai58e9cc92015-07-01 14:31:01 +03001928 result = ib_query_port(hca, port, &attr);
1929 if (!result)
Shirley Mabc7b3a32008-04-23 11:55:45 -07001930 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1931 else {
1932 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
1933 hca->name, port);
1934 goto device_init_failed;
1935 }
1936
1937 /* MTU will be reset when mcast join happens */
1938 priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
1939 priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
1940
David Miller596b9b62011-07-25 00:01:25 +00001941 priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
1942
Linus Torvalds1da177e2005-04-16 15:20:36 -07001943 result = ib_query_pkey(hca, port, 0, &priv->pkey);
1944 if (result) {
1945 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1946 hca->name, port, result);
Eli Cohenca6de172007-08-21 18:46:10 +03001947 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 }
1949
Amir Vadai58e9cc92015-07-01 14:31:01 +03001950 result = ipoib_set_dev_features(priv, hca);
1951 if (result)
Eli Cohen60461362008-04-16 21:01:10 -07001952 goto device_init_failed;
Vladimir Sokolovskyaf40da82008-07-14 23:48:48 -07001953
Roland Dreier4ce05932005-08-19 12:03:17 -07001954 /*
1955 * Set the full membership bit, so that we join the right
1956 * broadcast group, etc.
1957 */
1958 priv->pkey |= 0x8000;
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 priv->dev->broadcast[8] = priv->pkey >> 8;
1961 priv->dev->broadcast[9] = priv->pkey & 0xff;
1962
Matan Barak55ee3ab2015-10-15 18:38:45 +03001963 result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 if (result) {
1965 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1966 hca->name, port, result);
Eli Cohenca6de172007-08-21 18:46:10 +03001967 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968 } else
1969 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1970
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 result = ipoib_dev_init(priv->dev, hca, port);
1972 if (result < 0) {
1973 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1974 hca->name, port, result);
1975 goto device_init_failed;
1976 }
1977
1978 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1979 priv->ca, ipoib_event);
1980 result = ib_register_event_handler(&priv->event_handler);
1981 if (result < 0) {
1982 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1983 "port %d (ret = %d)\n",
1984 hca->name, port, result);
1985 goto event_failed;
1986 }
1987
1988 result = register_netdev(priv->dev);
1989 if (result) {
1990 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1991 hca->name, port, result);
1992 goto register_failed;
1993 }
1994
Roland Dreier1732b0e2005-11-07 10:33:11 -08001995 ipoib_create_debug_files(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001997 if (ipoib_cm_add_mode_attr(priv->dev))
1998 goto sysfs_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999 if (ipoib_add_pkey_attr(priv->dev))
2000 goto sysfs_failed;
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02002001 if (ipoib_add_umcast_attr(priv->dev))
2002 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002003 if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002005 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006 goto sysfs_failed;
2007
2008 return priv->dev;
2009
2010sysfs_failed:
Roland Dreier1732b0e2005-11-07 10:33:11 -08002011 ipoib_delete_debug_files(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 unregister_netdev(priv->dev);
2013
2014register_failed:
2015 ib_unregister_event_handler(&priv->event_handler);
Doug Ledford0b395782015-02-21 19:27:03 -05002016 flush_workqueue(ipoib_workqueue);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00002017 /* Stop GC if started before flush */
2018 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
2019 cancel_delayed_work(&priv->neigh_reap_task);
Doug Ledford0b395782015-02-21 19:27:03 -05002020 flush_workqueue(priv->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021
2022event_failed:
2023 ipoib_dev_cleanup(priv->dev);
2024
2025device_init_failed:
2026 free_netdev(priv->dev);
2027
2028alloc_mem_failed:
2029 return ERR_PTR(result);
2030}
2031
2032static void ipoib_add_one(struct ib_device *device)
2033{
2034 struct list_head *dev_list;
2035 struct net_device *dev;
2036 struct ipoib_dev_priv *priv;
Hal Rosenstock41390322015-06-29 09:57:00 -04002037 int p;
Michael Wang8e37ab62015-05-05 14:50:24 +02002038 int count = 0;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002039
Linus Torvalds1da177e2005-04-16 15:20:36 -07002040 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
2041 if (!dev_list)
2042 return;
2043
2044 INIT_LIST_HEAD(dev_list);
2045
Hal Rosenstock41390322015-06-29 09:57:00 -04002046 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
Michael Wang8e37ab62015-05-05 14:50:24 +02002047 if (!rdma_protocol_ib(device, p))
Eli Cohen7b4c8762010-09-27 17:51:11 -07002048 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 dev = ipoib_add_port("ib%d", device, p);
2050 if (!IS_ERR(dev)) {
2051 priv = netdev_priv(dev);
2052 list_add_tail(&priv->list, dev_list);
Michael Wang8e37ab62015-05-05 14:50:24 +02002053 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 }
2055 }
2056
Michael Wang8e37ab62015-05-05 14:50:24 +02002057 if (!count) {
2058 kfree(dev_list);
2059 return;
2060 }
2061
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 ib_set_client_data(device, &ipoib_client, dev_list);
2063}
2064
Haggai Eran7c1eb452015-07-30 17:50:14 +03002065static void ipoib_remove_one(struct ib_device *device, void *client_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066{
2067 struct ipoib_dev_priv *priv, *tmp;
Haggai Eran7c1eb452015-07-30 17:50:14 +03002068 struct list_head *dev_list = client_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
Itai Garbi5a2815f2013-02-19 15:40:24 +00002070 if (!dev_list)
2071 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073 list_for_each_entry_safe(priv, tmp, dev_list, list) {
2074 ib_unregister_event_handler(&priv->event_handler);
Doug Ledford0b395782015-02-21 19:27:03 -05002075 flush_workqueue(ipoib_workqueue);
Roland Dreiera77a57a2008-08-19 15:01:32 -07002076
2077 rtnl_lock();
2078 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
2079 rtnl_unlock();
2080
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00002081 /* Stop GC */
2082 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
2083 cancel_delayed_work(&priv->neigh_reap_task);
Doug Ledford0b395782015-02-21 19:27:03 -05002084 flush_workqueue(priv->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002085
2086 unregister_netdev(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 free_netdev(priv->dev);
2088 }
Michael S. Tsirkin06c56e42005-09-01 09:19:02 -07002089
2090 kfree(dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091}
2092
2093static int __init ipoib_init_module(void)
2094{
2095 int ret;
2096
Shirley Ma0f485252006-04-10 09:43:58 -07002097 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
2098 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
2099 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
2100
2101 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
2102 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -07002103 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08002104#ifdef CONFIG_INFINIBAND_IPOIB_CM
2105 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
2106#endif
Shirley Ma0f485252006-04-10 09:43:58 -07002107
Eli Cohenf89271da2008-07-14 23:48:44 -07002108 /*
2109 * When copying small received packets, we only copy from the
2110 * linear data part of the SKB, so we rely on this condition.
2111 */
2112 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
2113
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 ret = ipoib_register_debugfs();
2115 if (ret)
2116 return ret;
2117
2118 /*
Doug Ledford0b395782015-02-21 19:27:03 -05002119 * We create a global workqueue here that is used for all flush
2120 * operations. However, if you attempt to flush a workqueue
2121 * from a task on that same workqueue, it deadlocks the system.
2122 * We want to be able to flush the tasks associated with a
2123 * specific net device, so we also create a workqueue for each
2124 * netdevice. We queue up the tasks for that device only on
2125 * its private workqueue, and we only queue up flush events
2126 * on our global flush workqueue. This avoids the deadlocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002127 */
Doug Ledford0b395782015-02-21 19:27:03 -05002128 ipoib_workqueue = create_singlethread_workqueue("ipoib_flush");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002129 if (!ipoib_workqueue) {
2130 ret = -ENOMEM;
2131 goto err_fs;
2132 }
2133
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002134 ib_sa_register_client(&ipoib_sa_client);
2135
Linus Torvalds1da177e2005-04-16 15:20:36 -07002136 ret = ib_register_client(&ipoib_client);
2137 if (ret)
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002138 goto err_sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139
Or Gerlitz9baa0b02012-09-13 05:56:36 +00002140 ret = ipoib_netlink_init();
2141 if (ret)
2142 goto err_client;
2143
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 return 0;
2145
Or Gerlitz9baa0b02012-09-13 05:56:36 +00002146err_client:
2147 ib_unregister_client(&ipoib_client);
2148
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002149err_sa:
2150 ib_sa_unregister_client(&ipoib_sa_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151 destroy_workqueue(ipoib_workqueue);
2152
Roland Dreier9adec1a2005-04-16 15:26:07 -07002153err_fs:
2154 ipoib_unregister_debugfs();
2155
Linus Torvalds1da177e2005-04-16 15:20:36 -07002156 return ret;
2157}
2158
2159static void __exit ipoib_cleanup_module(void)
2160{
Or Gerlitz9baa0b02012-09-13 05:56:36 +00002161 ipoib_netlink_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 ib_unregister_client(&ipoib_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002163 ib_sa_unregister_client(&ipoib_sa_client);
Roland Dreier9adec1a2005-04-16 15:26:07 -07002164 ipoib_unregister_debugfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165 destroy_workqueue(ipoib_workqueue);
2166}
2167
2168module_init(ipoib_init_module);
2169module_exit(ipoib_cleanup_module);