blob: 045f844d7f4a05c0f0fa32d1c67314c43ab5779d [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);
Mark Bloch492a7e62016-05-18 16:42:43 +0300102static int ipoib_set_mac(struct net_device *dev, void *addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104static struct ib_client ipoib_client = {
105 .name = "ipoib",
106 .add = ipoib_add_one,
Guy Shapiroddde8962015-07-30 17:50:16 +0300107 .remove = ipoib_remove_one,
108 .get_net_dev_by_params = ipoib_get_net_dev_by_params,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109};
110
111int ipoib_open(struct net_device *dev)
112{
113 struct ipoib_dev_priv *priv = netdev_priv(dev);
114
115 ipoib_dbg(priv, "bringing up interface\n");
116
Michal Schmidt437708c2014-01-17 19:47:25 +0100117 netif_carrier_off(dev);
118
Yossi Etigine028cc52009-04-20 13:58:08 -0700119 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
Erez Shitrit3b561132016-05-25 22:02:07 +0300121 priv->sm_fullmember_sendonly_support = false;
122
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500123 if (ipoib_ib_dev_open(dev)) {
Alex Estrindd57c932014-08-06 14:40:32 -0400124 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
125 return 0;
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800126 goto err_disable;
Alex Estrindd57c932014-08-06 14:40:32 -0400127 }
Yossi Etiginfe25c562008-11-12 10:24:36 -0800128
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800129 if (ipoib_ib_dev_up(dev))
130 goto err_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
132 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
133 struct ipoib_dev_priv *cpriv;
134
135 /* Bring up any child interfaces too */
Erez Shitritf47944c2013-10-16 17:37:49 +0300136 down_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 list_for_each_entry(cpriv, &priv->child_intfs, list) {
138 int flags;
139
140 flags = cpriv->dev->flags;
141 if (flags & IFF_UP)
142 continue;
143
144 dev_change_flags(cpriv->dev, flags | IFF_UP);
145 }
Erez Shitritf47944c2013-10-16 17:37:49 +0300146 up_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 }
148
149 netif_start_queue(dev);
150
151 return 0;
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800152
153err_stop:
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500154 ipoib_ib_dev_stop(dev);
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800155
156err_disable:
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800157 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
158
159 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160}
161
162static int ipoib_stop(struct net_device *dev)
163{
164 struct ipoib_dev_priv *priv = netdev_priv(dev);
165
166 ipoib_dbg(priv, "stopping interface\n");
167
168 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
169
170 netif_stop_queue(dev);
171
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500172 ipoib_ib_dev_down(dev);
173 ipoib_ib_dev_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174
175 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
176 struct ipoib_dev_priv *cpriv;
177
178 /* Bring down any child interfaces too */
Erez Shitritf47944c2013-10-16 17:37:49 +0300179 down_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 list_for_each_entry(cpriv, &priv->child_intfs, list) {
181 int flags;
182
183 flags = cpriv->dev->flags;
184 if (!(flags & IFF_UP))
185 continue;
186
187 dev_change_flags(cpriv->dev, flags & ~IFF_UP);
188 }
Erez Shitritf47944c2013-10-16 17:37:49 +0300189 up_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 }
191
192 return 0;
193}
194
Or Gerlitz9baa0b02012-09-13 05:56:36 +0000195static void ipoib_uninit(struct net_device *dev)
196{
197 ipoib_dev_cleanup(dev);
198}
199
David S. Miller9ca36f72011-11-16 18:05:50 -0500200static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
Michał Mirosław3d96c742011-04-19 00:43:20 +0000201{
202 struct ipoib_dev_priv *priv = netdev_priv(dev);
203
204 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
Yuval Shaiac4268772015-07-12 01:24:09 -0700205 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
Michał Mirosław3d96c742011-04-19 00:43:20 +0000206
207 return features;
208}
209
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
211{
212 struct ipoib_dev_priv *priv = netdev_priv(dev);
213
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200214 /* dev->mtu > 2K ==> connected mode */
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800215 if (ipoib_cm_admin_enabled(dev)) {
216 if (new_mtu > ipoib_cm_max_mtu(dev))
217 return -EINVAL;
218
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200219 if (new_mtu > priv->mcast_mtu)
220 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
221 priv->mcast_mtu);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800222
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200223 dev->mtu = new_mtu;
224 return 0;
225 }
226
Shirley Mabc7b3a32008-04-23 11:55:45 -0700227 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 return -EINVAL;
229
230 priv->admin_mtu = new_mtu;
231
Feras Daoud29da6862016-12-28 14:47:20 +0200232 if (priv->mcast_mtu < priv->admin_mtu)
233 ipoib_dbg(priv, "MTU must be smaller than the underlying "
234 "link layer MTU - 4 (%u)\n", priv->mcast_mtu);
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
237
238 return 0;
239}
240
Guy Shapiroddde8962015-07-30 17:50:16 +0300241/* Called with an RCU read lock taken */
242static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr,
243 struct net_device *dev)
244{
245 struct net *net = dev_net(dev);
246 struct in_device *in_dev;
247 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
248 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
249 __be32 ret_addr;
250
251 switch (addr->sa_family) {
252 case AF_INET:
253 in_dev = in_dev_get(dev);
254 if (!in_dev)
255 return false;
256
257 ret_addr = inet_confirm_addr(net, in_dev, 0,
258 addr_in->sin_addr.s_addr,
259 RT_SCOPE_HOST);
260 in_dev_put(in_dev);
261 if (ret_addr)
262 return true;
263
264 break;
265 case AF_INET6:
266 if (IS_ENABLED(CONFIG_IPV6) &&
267 ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1))
268 return true;
269
270 break;
271 }
272 return false;
273}
274
275/**
276 * Find the master net_device on top of the given net_device.
277 * @dev: base IPoIB net_device
278 *
279 * Returns the master net_device with a reference held, or the same net_device
280 * if no master exists.
281 */
282static struct net_device *ipoib_get_master_net_dev(struct net_device *dev)
283{
284 struct net_device *master;
285
286 rcu_read_lock();
287 master = netdev_master_upper_dev_get_rcu(dev);
288 if (master)
289 dev_hold(master);
290 rcu_read_unlock();
291
292 if (master)
293 return master;
294
295 dev_hold(dev);
296 return dev;
297}
298
David Aherne0e79c82016-10-17 19:15:47 -0700299struct ipoib_walk_data {
300 const struct sockaddr *addr;
301 struct net_device *result;
302};
303
304static int ipoib_upper_walk(struct net_device *upper, void *_data)
305{
306 struct ipoib_walk_data *data = _data;
307 int ret = 0;
308
309 if (ipoib_is_dev_match_addr_rcu(data->addr, upper)) {
310 dev_hold(upper);
311 data->result = upper;
312 ret = 1;
313 }
314
315 return ret;
316}
317
Guy Shapiroddde8962015-07-30 17:50:16 +0300318/**
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{
David Aherne0e79c82016-10-17 19:15:47 -0700330 struct ipoib_walk_data data = {
331 .addr = addr,
332 };
Guy Shapiroddde8962015-07-30 17:50:16 +0300333
334 rcu_read_lock();
335 if (ipoib_is_dev_match_addr_rcu(addr, dev)) {
336 dev_hold(dev);
David Aherne0e79c82016-10-17 19:15:47 -0700337 data.result = dev;
Guy Shapiroddde8962015-07-30 17:50:16 +0300338 goto out;
339 }
340
David Aherne0e79c82016-10-17 19:15:47 -0700341 netdev_walk_all_upper_dev_rcu(dev, ipoib_upper_walk, &data);
Guy Shapiroddde8962015-07-30 17:50:16 +0300342out:
343 rcu_read_unlock();
David Aherne0e79c82016-10-17 19:15:47 -0700344 return data.result;
Guy Shapiroddde8962015-07-30 17:50:16 +0300345}
346
347/* returns the number of IPoIB netdevs on top a given ipoib device matching a
348 * pkey_index and address, if one exists.
349 *
350 * @found_net_dev: contains a matching net_device if the return value >= 1,
351 * with a reference held. */
352static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
353 const union ib_gid *gid,
354 u16 pkey_index,
355 const struct sockaddr *addr,
356 int nesting,
357 struct net_device **found_net_dev)
358{
359 struct ipoib_dev_priv *child_priv;
360 struct net_device *net_dev = NULL;
361 int matches = 0;
362
363 if (priv->pkey_index == pkey_index &&
364 (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
365 if (!addr) {
366 net_dev = ipoib_get_master_net_dev(priv->dev);
367 } else {
368 /* Verify the net_device matches the IP address, as
369 * IPoIB child devices currently share a GID. */
370 net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev);
371 }
372 if (net_dev) {
373 if (!*found_net_dev)
374 *found_net_dev = net_dev;
375 else
376 dev_put(net_dev);
377 ++matches;
378 }
379 }
380
381 /* Check child interfaces */
382 down_read_nested(&priv->vlan_rwsem, nesting);
383 list_for_each_entry(child_priv, &priv->child_intfs, list) {
384 matches += ipoib_match_gid_pkey_addr(child_priv, gid,
385 pkey_index, addr,
386 nesting + 1,
387 found_net_dev);
388 if (matches > 1)
389 break;
390 }
391 up_read(&priv->vlan_rwsem);
392
393 return matches;
394}
395
396/* Returns the number of matching net_devs found (between 0 and 2). Also
397 * return the matching net_device in the @net_dev parameter, holding a
398 * reference to the net_device, if the number of matches >= 1 */
399static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port,
400 u16 pkey_index,
401 const union ib_gid *gid,
402 const struct sockaddr *addr,
403 struct net_device **net_dev)
404{
405 struct ipoib_dev_priv *priv;
406 int matches = 0;
407
408 *net_dev = NULL;
409
410 list_for_each_entry(priv, dev_list, list) {
411 if (priv->port != port)
412 continue;
413
414 matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
415 addr, 0, net_dev);
416 if (matches > 1)
417 break;
418 }
419
420 return matches;
421}
422
423static struct net_device *ipoib_get_net_dev_by_params(
424 struct ib_device *dev, u8 port, u16 pkey,
425 const union ib_gid *gid, const struct sockaddr *addr,
426 void *client_data)
427{
428 struct net_device *net_dev;
429 struct list_head *dev_list = client_data;
430 u16 pkey_index;
431 int matches;
432 int ret;
433
434 if (!rdma_protocol_ib(dev, port))
435 return NULL;
436
437 ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
438 if (ret)
439 return NULL;
440
441 if (!dev_list)
442 return NULL;
443
444 /* See if we can find a unique device matching the L2 parameters */
445 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
446 gid, NULL, &net_dev);
447
448 switch (matches) {
449 case 0:
450 return NULL;
451 case 1:
452 return net_dev;
453 }
454
455 dev_put(net_dev);
456
457 /* Couldn't find a unique device with L2 parameters only. Use L3
458 * address to uniquely match the net device */
459 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
460 gid, addr, &net_dev);
461 switch (matches) {
462 case 0:
463 return NULL;
464 default:
465 dev_warn_ratelimited(&dev->dev,
466 "duplicate IP address detected\n");
467 /* Fall through */
468 case 1:
469 return net_dev;
470 }
471}
472
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700473int ipoib_set_mode(struct net_device *dev, const char *buf)
474{
475 struct ipoib_dev_priv *priv = netdev_priv(dev);
476
Feras Daoud80b5b352016-12-28 14:47:21 +0200477 if ((test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags) &&
478 !strcmp(buf, "connected\n")) ||
479 (!test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags) &&
480 !strcmp(buf, "datagram\n"))) {
481 return 0;
482 }
483
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700484 /* flush paths if we switch modes so that connections are restarted */
485 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
486 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
487 ipoib_warn(priv, "enabling connected mode "
488 "will cause multicast packet drops\n");
489 netdev_update_features(dev);
Erez Shitritedcd2a72015-06-07 13:36:11 +0300490 dev_set_mtu(dev, ipoib_cm_max_mtu(dev));
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700491 rtnl_unlock();
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100492 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700493
494 ipoib_flush_paths(dev);
Feras Daoud0a0007f2016-12-28 14:47:23 +0200495 return (!rtnl_trylock()) ? -EBUSY : 0;
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700496 }
497
498 if (!strcmp(buf, "datagram\n")) {
499 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
500 netdev_update_features(dev);
501 dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
502 rtnl_unlock();
503 ipoib_flush_paths(dev);
Feras Daoud0a0007f2016-12-28 14:47:23 +0200504 return (!rtnl_trylock()) ? -EBUSY : 0;
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700505 }
506
507 return -EINVAL;
508}
509
Erez Shitrit546481c2016-08-28 10:58:31 +0300510struct ipoib_path *__path_find(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511{
512 struct ipoib_dev_priv *priv = netdev_priv(dev);
513 struct rb_node *n = priv->path_tree.rb_node;
514 struct ipoib_path *path;
515 int ret;
516
517 while (n) {
518 path = rb_entry(n, struct ipoib_path, rb_node);
519
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300520 ret = memcmp(gid, path->pathrec.dgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 sizeof (union ib_gid));
522
523 if (ret < 0)
524 n = n->rb_left;
525 else if (ret > 0)
526 n = n->rb_right;
527 else
528 return path;
529 }
530
531 return NULL;
532}
533
534static int __path_add(struct net_device *dev, struct ipoib_path *path)
535{
536 struct ipoib_dev_priv *priv = netdev_priv(dev);
537 struct rb_node **n = &priv->path_tree.rb_node;
538 struct rb_node *pn = NULL;
539 struct ipoib_path *tpath;
540 int ret;
541
542 while (*n) {
543 pn = *n;
544 tpath = rb_entry(pn, struct ipoib_path, rb_node);
545
546 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
547 sizeof (union ib_gid));
548 if (ret < 0)
549 n = &pn->rb_left;
550 else if (ret > 0)
551 n = &pn->rb_right;
552 else
553 return -EEXIST;
554 }
555
556 rb_link_node(&path->rb_node, pn, n);
557 rb_insert_color(&path->rb_node, &priv->path_tree);
558
559 list_add_tail(&path->list, &priv->path_list);
560
561 return 0;
562}
563
564static void path_free(struct net_device *dev, struct ipoib_path *path)
565{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568 while ((skb = __skb_dequeue(&path->queue)))
569 dev_kfree_skb_irq(skb);
570
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000571 ipoib_dbg(netdev_priv(dev), "path_free\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000573 /* remove all neigh connected to this path */
574 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576 if (path->ah)
577 ipoib_put_ah(path->ah);
578
579 kfree(path);
580}
581
Roland Dreier1732b0e2005-11-07 10:33:11 -0800582#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
583
584struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
585{
586 struct ipoib_path_iter *iter;
587
588 iter = kmalloc(sizeof *iter, GFP_KERNEL);
589 if (!iter)
590 return NULL;
591
592 iter->dev = dev;
593 memset(iter->path.pathrec.dgid.raw, 0, 16);
594
595 if (ipoib_path_iter_next(iter)) {
596 kfree(iter);
597 return NULL;
598 }
599
600 return iter;
601}
602
603int ipoib_path_iter_next(struct ipoib_path_iter *iter)
604{
605 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
606 struct rb_node *n;
607 struct ipoib_path *path;
608 int ret = 1;
609
610 spin_lock_irq(&priv->lock);
611
612 n = rb_first(&priv->path_tree);
613
614 while (n) {
615 path = rb_entry(n, struct ipoib_path, rb_node);
616
617 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
618 sizeof (union ib_gid)) < 0) {
619 iter->path = *path;
620 ret = 0;
621 break;
622 }
623
624 n = rb_next(n);
625 }
626
627 spin_unlock_irq(&priv->lock);
628
629 return ret;
630}
631
632void ipoib_path_iter_read(struct ipoib_path_iter *iter,
633 struct ipoib_path *path)
634{
635 *path = iter->path;
636}
637
638#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
639
Moni Shouaee1e2c82008-07-14 23:48:49 -0700640void ipoib_mark_paths_invalid(struct net_device *dev)
641{
642 struct ipoib_dev_priv *priv = netdev_priv(dev);
643 struct ipoib_path *path, *tp;
644
645 spin_lock_irq(&priv->lock);
646
647 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700648 ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
Moni Shouaee1e2c82008-07-14 23:48:49 -0700649 be16_to_cpu(path->pathrec.dlid),
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700650 path->pathrec.dgid.raw);
Moni Shouaee1e2c82008-07-14 23:48:49 -0700651 path->valid = 0;
652 }
653
654 spin_unlock_irq(&priv->lock);
655}
656
Erez Shitrit3b561132016-05-25 22:02:07 +0300657struct classport_info_context {
658 struct ipoib_dev_priv *priv;
659 struct completion done;
660 struct ib_sa_query *sa_query;
661};
662
663static void classport_info_query_cb(int status, struct ib_class_port_info *rec,
664 void *context)
665{
666 struct classport_info_context *cb_ctx = context;
667 struct ipoib_dev_priv *priv;
668
669 WARN_ON(!context);
670
671 priv = cb_ctx->priv;
672
673 if (status || !rec) {
674 pr_debug("device: %s failed query classport_info status: %d\n",
675 priv->dev->name, status);
676 /* keeps the default, will try next mcast_restart */
677 priv->sm_fullmember_sendonly_support = false;
678 goto out;
679 }
680
681 if (ib_get_cpi_capmask2(rec) &
682 IB_SA_CAP_MASK2_SENDONLY_FULL_MEM_SUPPORT) {
683 pr_debug("device: %s enabled fullmember-sendonly for sendonly MCG\n",
684 priv->dev->name);
685 priv->sm_fullmember_sendonly_support = true;
686 } else {
687 pr_debug("device: %s disabled fullmember-sendonly for sendonly MCG\n",
688 priv->dev->name);
689 priv->sm_fullmember_sendonly_support = false;
690 }
691
692out:
693 complete(&cb_ctx->done);
694}
695
696int ipoib_check_sm_sendonly_fullmember_support(struct ipoib_dev_priv *priv)
697{
698 struct classport_info_context *callback_context;
699 int ret;
700
701 callback_context = kmalloc(sizeof(*callback_context), GFP_KERNEL);
702 if (!callback_context)
703 return -ENOMEM;
704
705 callback_context->priv = priv;
706 init_completion(&callback_context->done);
707
708 ret = ib_sa_classport_info_rec_query(&ipoib_sa_client,
709 priv->ca, priv->port, 3000,
710 GFP_KERNEL,
711 classport_info_query_cb,
712 callback_context,
713 &callback_context->sa_query);
714 if (ret < 0) {
715 pr_info("%s failed to send ib_sa_classport_info query, ret: %d\n",
716 priv->dev->name, ret);
717 kfree(callback_context);
718 return ret;
719 }
720
721 /* waiting for the callback to finish before returnning */
722 wait_for_completion(&callback_context->done);
723 kfree(callback_context);
724
725 return ret;
726}
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728void ipoib_flush_paths(struct net_device *dev)
729{
730 struct ipoib_dev_priv *priv = netdev_priv(dev);
731 struct ipoib_path *path, *tp;
732 LIST_HEAD(remove_list);
Roland Dreier943c2462008-09-30 10:36:21 -0700733 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734
Roland Dreier943c2462008-09-30 10:36:21 -0700735 netif_tx_lock_bh(dev);
736 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
Robert P. J. Day157de222008-04-16 21:09:26 -0700738 list_splice_init(&priv->path_list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
740 list_for_each_entry(path, &remove_list, list)
741 rb_erase(&path->rb_node, &priv->path_tree);
742
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 list_for_each_entry_safe(path, tp, &remove_list, list) {
744 if (path->query)
745 ib_sa_cancel_query(path->query_id, path->query);
Roland Dreier943c2462008-09-30 10:36:21 -0700746 spin_unlock_irqrestore(&priv->lock, flags);
747 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 wait_for_completion(&path->done);
749 path_free(dev, path);
Roland Dreier943c2462008-09-30 10:36:21 -0700750 netif_tx_lock_bh(dev);
751 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 }
Roland Dreier943c2462008-09-30 10:36:21 -0700753
754 spin_unlock_irqrestore(&priv->lock, flags);
755 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
758static void path_rec_completion(int status,
759 struct ib_sa_path_rec *pathrec,
760 void *path_ptr)
761{
762 struct ipoib_path *path = path_ptr;
763 struct net_device *dev = path->dev;
764 struct ipoib_dev_priv *priv = netdev_priv(dev);
765 struct ipoib_ah *ah = NULL;
Roland Dreierc9da4ba2008-09-25 15:26:15 -0700766 struct ipoib_ah *old_ah = NULL;
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700767 struct ipoib_neigh *neigh, *tn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 struct sk_buff_head skqueue;
769 struct sk_buff *skb;
770 unsigned long flags;
771
Roland Dreier843613b2007-02-26 12:57:08 -0800772 if (!status)
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700773 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700774 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 else
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700776 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700777 status, path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778
779 skb_queue_head_init(&skqueue);
780
781 if (!status) {
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700782 struct ib_ah_attr av;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700784 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
785 ah = ipoib_create_ah(dev, priv->pd, &av);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 }
787
788 spin_lock_irqsave(&priv->lock, flags);
789
Mike Marciniszyn38743972011-11-21 08:43:54 -0500790 if (!IS_ERR_OR_NULL(ah)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791 path->pathrec = *pathrec;
792
Roland Dreierc9da4ba2008-09-25 15:26:15 -0700793 old_ah = path->ah;
794 path->ah = ah;
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
797 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
798
799 while ((skb = __skb_dequeue(&path->queue)))
800 __skb_queue_tail(&skqueue, skb);
801
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700802 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
Moni Shouaee1e2c82008-07-14 23:48:49 -0700803 if (neigh->ah) {
804 WARN_ON(neigh->ah != old_ah);
805 /*
806 * Dropping the ah reference inside
807 * priv->lock is safe here, because we
808 * will hold one more reference from
809 * the original value of path->ah (ie
810 * old_ah).
811 */
812 ipoib_put_ah(neigh->ah);
813 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 kref_get(&path->ah->ref);
815 neigh->ah = path->ah;
816
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000817 if (ipoib_cm_enabled(dev, neigh->daddr)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200818 if (!ipoib_cm_get(neigh))
819 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
820 path,
821 neigh));
822 if (!ipoib_cm_get(neigh)) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000823 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200824 continue;
825 }
826 }
827
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 while ((skb = __skb_dequeue(&neigh->queue)))
829 __skb_queue_tail(&skqueue, skb);
830 }
Moni Shouaee1e2c82008-07-14 23:48:49 -0700831 path->valid = 1;
Roland Dreier5872a9f2005-11-29 10:13:54 -0800832 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833
Roland Dreier5872a9f2005-11-29 10:13:54 -0800834 path->query = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 complete(&path->done);
836
837 spin_unlock_irqrestore(&priv->lock, flags);
838
Roland Dreierf72dd562013-02-25 09:42:15 -0800839 if (IS_ERR_OR_NULL(ah))
840 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
841
Moni Shouaee1e2c82008-07-14 23:48:49 -0700842 if (old_ah)
843 ipoib_put_ah(old_ah);
844
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 while ((skb = __skb_dequeue(&skqueue))) {
846 skb->dev = dev;
847 if (dev_queue_xmit(skb))
848 ipoib_warn(priv, "dev_queue_xmit failed "
849 "to requeue packet\n");
850 }
851}
852
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300853static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854{
855 struct ipoib_dev_priv *priv = netdev_priv(dev);
856 struct ipoib_path *path;
857
Jack Morgenstein1401b532007-11-26 10:41:19 +0200858 if (!priv->broadcast)
859 return NULL;
860
Roland Dreier21a38482005-11-02 10:07:59 -0800861 path = kzalloc(sizeof *path, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (!path)
863 return NULL;
864
Roland Dreier21a38482005-11-02 10:07:59 -0800865 path->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866
867 skb_queue_head_init(&path->queue);
868
869 INIT_LIST_HEAD(&path->neigh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300871 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
Roland Dreier2337f802007-10-23 19:57:54 -0700872 path->pathrec.sgid = priv->local_gid;
873 path->pathrec.pkey = cpu_to_be16(priv->pkey);
Sean Hefty81668832007-08-02 12:21:31 -0700874 path->pathrec.numb_path = 1;
875 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
877 return path;
878}
879
880static int path_rec_start(struct net_device *dev,
881 struct ipoib_path *path)
882{
883 struct ipoib_dev_priv *priv = netdev_priv(dev);
884
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700885 ipoib_dbg(priv, "Start path record lookup for %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700886 path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Roland Dreier65c7edd2005-11-28 21:20:34 -0800888 init_completion(&path->done);
889
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 path->query_id =
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700891 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 &path->pathrec,
893 IB_SA_PATH_REC_DGID |
894 IB_SA_PATH_REC_SGID |
895 IB_SA_PATH_REC_NUMB_PATH |
Sean Hefty81668832007-08-02 12:21:31 -0700896 IB_SA_PATH_REC_TRAFFIC_CLASS |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 IB_SA_PATH_REC_PKEY,
898 1000, GFP_ATOMIC,
899 path_rec_completion,
900 path, &path->query);
901 if (path->query_id < 0) {
Or Gerlitz01b3fc82008-07-22 14:18:34 -0700902 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903 path->query = NULL;
Yossi Etigin93a3ab92008-11-12 10:24:38 -0800904 complete(&path->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905 return path->query_id;
906 }
907
908 return 0;
909}
910
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000911static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
912 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700913{
914 struct ipoib_dev_priv *priv = netdev_priv(dev);
915 struct ipoib_path *path;
916 struct ipoib_neigh *neigh;
Roland Dreier943c2462008-09-30 10:36:21 -0700917 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000919 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000920 neigh = ipoib_neigh_alloc(daddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 if (!neigh) {
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000922 spin_unlock_irqrestore(&priv->lock, flags);
Roland Dreierde903512007-09-28 15:33:51 -0700923 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 dev_kfree_skb_any(skb);
925 return;
926 }
927
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000928 path = __path_find(dev, daddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 if (!path) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000930 path = path_rec_create(dev, daddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 if (!path)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300932 goto err_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933
934 __path_add(dev, path);
935 }
936
937 list_add_tail(&neigh->list, &path->neigh_list);
938
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -0800939 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 kref_get(&path->ah->ref);
941 neigh->ah = path->ah;
942
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000943 if (ipoib_cm_enabled(dev, neigh->daddr)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200944 if (!ipoib_cm_get(neigh))
945 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
946 if (!ipoib_cm_get(neigh)) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000947 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200948 goto err_drop;
949 }
Paolo Abenifc791b62016-10-13 18:26:56 +0200950 if (skb_queue_len(&neigh->queue) <
951 IPOIB_MAX_PATH_REC_QUEUE) {
952 /* put pseudoheader back on for next time */
953 skb_push(skb, IPOIB_PSEUDO_LEN);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200954 __skb_queue_tail(&neigh->queue, skb);
Paolo Abenifc791b62016-10-13 18:26:56 +0200955 } else {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200956 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
957 skb_queue_len(&neigh->queue));
958 goto err_drop;
959 }
Roland Dreier721d67c2009-09-05 20:23:40 -0700960 } else {
961 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000962 ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
963 ipoib_neigh_put(neigh);
Roland Dreier721d67c2009-09-05 20:23:40 -0700964 return;
965 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 } else {
967 neigh->ah = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 if (!path->query && path_rec_start(dev, path))
Jim Foraker49b8e742013-08-08 17:44:22 -0700970 goto err_path;
Erez Shitrit1e85b802015-04-02 13:39:03 +0300971 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
972 __skb_queue_tail(&neigh->queue, skb);
973 else
974 goto err_drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 }
976
Roland Dreier943c2462008-09-30 10:36:21 -0700977 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000978 ipoib_neigh_put(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 return;
980
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300981err_path:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000982 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200983err_drop:
Roland Dreierde903512007-09-28 15:33:51 -0700984 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985 dev_kfree_skb_any(skb);
986
Roland Dreier943c2462008-09-30 10:36:21 -0700987 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000988 ipoib_neigh_put(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989}
990
991static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
Paolo Abenifc791b62016-10-13 18:26:56 +0200992 struct ipoib_pseudo_header *phdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993{
994 struct ipoib_dev_priv *priv = netdev_priv(dev);
995 struct ipoib_path *path;
Roland Dreier943c2462008-09-30 10:36:21 -0700996 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997
Roland Dreier943c2462008-09-30 10:36:21 -0700998 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999
Paolo Abenifc791b62016-10-13 18:26:56 +02001000 path = __path_find(dev, phdr->hwaddr + 4);
Moni Shouaee1e2c82008-07-14 23:48:49 -07001001 if (!path || !path->valid) {
Jack Morgenstein71d98b42009-02-17 14:51:47 -08001002 int new_path = 0;
1003
1004 if (!path) {
Paolo Abenifc791b62016-10-13 18:26:56 +02001005 path = path_rec_create(dev, phdr->hwaddr + 4);
Jack Morgenstein71d98b42009-02-17 14:51:47 -08001006 new_path = 1;
1007 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 if (path) {
Erez Shitrit1e85b802015-04-02 13:39:03 +03001009 if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
Paolo Abenifc791b62016-10-13 18:26:56 +02001010 /* put pseudoheader back on for next time */
1011 skb_push(skb, IPOIB_PSEUDO_LEN);
Erez Shitrit1e85b802015-04-02 13:39:03 +03001012 __skb_queue_tail(&path->queue, skb);
1013 } else {
1014 ++dev->stats.tx_dropped;
1015 dev_kfree_skb_any(skb);
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Yossi Etiginff79ae82008-11-12 10:24:39 -08001018 if (!path->query && path_rec_start(dev, path)) {
Roland Dreier943c2462008-09-30 10:36:21 -07001019 spin_unlock_irqrestore(&priv->lock, flags);
Jack Morgenstein71d98b42009-02-17 14:51:47 -08001020 if (new_path)
1021 path_free(dev, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 return;
1023 } else
1024 __path_add(dev, path);
1025 } else {
Roland Dreierde903512007-09-28 15:33:51 -07001026 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 dev_kfree_skb_any(skb);
1028 }
1029
Roland Dreier943c2462008-09-30 10:36:21 -07001030 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031 return;
1032 }
1033
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -08001034 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
1036 be16_to_cpu(path->pathrec.dlid));
1037
Roland Dreier721d67c2009-09-05 20:23:40 -07001038 spin_unlock_irqrestore(&priv->lock, flags);
Paolo Abenifc791b62016-10-13 18:26:56 +02001039 ipoib_send(dev, skb, path->ah, IPOIB_QPN(phdr->hwaddr));
Roland Dreier721d67c2009-09-05 20:23:40 -07001040 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 } else if ((path->query || !path_rec_start(dev, path)) &&
1042 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
Paolo Abenifc791b62016-10-13 18:26:56 +02001043 /* put pseudoheader back on for next time */
1044 skb_push(skb, IPOIB_PSEUDO_LEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 __skb_queue_tail(&path->queue, skb);
1046 } else {
Roland Dreierde903512007-09-28 15:33:51 -07001047 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 dev_kfree_skb_any(skb);
1049 }
1050
Roland Dreier943c2462008-09-30 10:36:21 -07001051 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052}
1053
1054static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
1055{
1056 struct ipoib_dev_priv *priv = netdev_priv(dev);
1057 struct ipoib_neigh *neigh;
Paolo Abenifc791b62016-10-13 18:26:56 +02001058 struct ipoib_pseudo_header *phdr;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001059 struct ipoib_header *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 unsigned long flags;
1061
Paolo Abenifc791b62016-10-13 18:26:56 +02001062 phdr = (struct ipoib_pseudo_header *) skb->data;
1063 skb_pull(skb, sizeof(*phdr));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001064 header = (struct ipoib_header *) skb->data;
1065
Paolo Abenifc791b62016-10-13 18:26:56 +02001066 if (unlikely(phdr->hwaddr[4] == 0xff)) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001067 /* multicast, arrange "if" according to probability */
1068 if ((header->proto != htons(ETH_P_IP)) &&
1069 (header->proto != htons(ETH_P_IPV6)) &&
1070 (header->proto != htons(ETH_P_ARP)) &&
Patrick McHardydc850b02013-04-17 06:18:29 +00001071 (header->proto != htons(ETH_P_RARP)) &&
1072 (header->proto != htons(ETH_P_TIPC))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001073 /* ethertype not supported by IPoIB */
David Miller17e6abe2011-12-02 16:52:44 +00001074 ++dev->stats.tx_dropped;
1075 dev_kfree_skb_any(skb);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001076 return NETDEV_TX_OK;
David Miller17e6abe2011-12-02 16:52:44 +00001077 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001078 /* Add in the P_Key for multicast*/
Paolo Abenifc791b62016-10-13 18:26:56 +02001079 phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
1080 phdr->hwaddr[9] = priv->pkey & 0xff;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001081
Paolo Abenifc791b62016-10-13 18:26:56 +02001082 neigh = ipoib_neigh_get(dev, phdr->hwaddr);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001083 if (likely(neigh))
1084 goto send_using_neigh;
Paolo Abenifc791b62016-10-13 18:26:56 +02001085 ipoib_mcast_send(dev, phdr->hwaddr, skb);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001086 return NETDEV_TX_OK;
David Miller17e6abe2011-12-02 16:52:44 +00001087 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001089 /* unicast, arrange "switch" according to probability */
1090 switch (header->proto) {
1091 case htons(ETH_P_IP):
1092 case htons(ETH_P_IPV6):
Patrick McHardydc850b02013-04-17 06:18:29 +00001093 case htons(ETH_P_TIPC):
Paolo Abenifc791b62016-10-13 18:26:56 +02001094 neigh = ipoib_neigh_get(dev, phdr->hwaddr);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001095 if (unlikely(!neigh)) {
Paolo Abenifc791b62016-10-13 18:26:56 +02001096 neigh_add_path(skb, phdr->hwaddr, dev);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001097 return NETDEV_TX_OK;
Yossi Etigina50df392009-01-09 14:05:11 -08001098 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001099 break;
1100 case htons(ETH_P_ARP):
1101 case htons(ETH_P_RARP):
1102 /* for unicast ARP and RARP should always perform path find */
Paolo Abenifc791b62016-10-13 18:26:56 +02001103 unicast_arp_send(skb, dev, phdr);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001104 return NETDEV_TX_OK;
1105 default:
1106 /* ethertype not supported by IPoIB */
1107 ++dev->stats.tx_dropped;
1108 dev_kfree_skb_any(skb);
1109 return NETDEV_TX_OK;
1110 }
Michael S. Tsirkin8a7f7522006-07-19 17:44:37 +03001111
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001112send_using_neigh:
1113 /* note we now hold a ref to neigh */
1114 if (ipoib_cm_get(neigh)) {
1115 if (ipoib_cm_up(neigh)) {
1116 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
1117 goto unref;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001119 } else if (neigh->ah) {
Paolo Abenifc791b62016-10-13 18:26:56 +02001120 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(phdr->hwaddr));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001121 goto unref;
1122 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001124 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
Paolo Abenifc791b62016-10-13 18:26:56 +02001125 /* put pseudoheader back on for next time */
1126 skb_push(skb, sizeof(*phdr));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001127 spin_lock_irqsave(&priv->lock, flags);
1128 __skb_queue_tail(&neigh->queue, skb);
1129 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 } else {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001131 ++dev->stats.tx_dropped;
1132 dev_kfree_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001134
1135unref:
1136 ipoib_neigh_put(neigh);
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138 return NETDEV_TX_OK;
1139}
1140
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141static void ipoib_timeout(struct net_device *dev)
1142{
1143 struct ipoib_dev_priv *priv = netdev_priv(dev);
1144
Roland Dreier4b2d3192005-10-18 12:20:06 -07001145 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
Florian Westphal4d0e9652016-05-03 16:30:59 +02001146 jiffies_to_msecs(jiffies - dev_trans_start(dev)));
Roland Dreier4b2d3192005-10-18 12:20:06 -07001147 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
1148 netif_queue_stopped(dev),
1149 priv->tx_head, priv->tx_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001150 /* XXX reset QP, etc. */
1151}
1152
1153static int ipoib_hard_header(struct sk_buff *skb,
1154 struct net_device *dev,
1155 unsigned short type,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001156 const void *daddr, const void *saddr, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157{
Paolo Abenifc791b62016-10-13 18:26:56 +02001158 struct ipoib_pseudo_header *phdr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001159 struct ipoib_header *header;
1160
1161 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
1162
1163 header->proto = htons(type);
1164 header->reserved = 0;
1165
1166 /*
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001167 * we don't rely on dst_entry structure, always stuff the
Paolo Abenifc791b62016-10-13 18:26:56 +02001168 * destination address into skb hard header so we can figure out where
Roland Dreier936d7de2012-02-07 14:51:21 +00001169 * to send the packet later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170 */
Paolo Abenifc791b62016-10-13 18:26:56 +02001171 phdr = (struct ipoib_pseudo_header *) skb_push(skb, sizeof(*phdr));
1172 memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173
Paolo Abenifc791b62016-10-13 18:26:56 +02001174 return IPOIB_HARD_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175}
1176
1177static void ipoib_set_mcast_list(struct net_device *dev)
1178{
1179 struct ipoib_dev_priv *priv = netdev_priv(dev);
1180
Leonid Arsh7a343d42006-03-23 19:52:51 +02001181 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
1182 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
1183 return;
1184 }
1185
Doug Ledford0b395782015-02-21 19:27:03 -05001186 queue_work(priv->wq, &priv->restart_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187}
1188
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +02001189static int ipoib_get_iflink(const struct net_device *dev)
1190{
1191 struct ipoib_dev_priv *priv = netdev_priv(dev);
1192
Erez Shitrit2c153952015-04-16 16:34:34 +03001193 /* parent interface */
1194 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
1195 return dev->ifindex;
1196
1197 /* child/vlan interface */
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +02001198 return priv->parent->ifindex;
1199}
1200
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001201static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001203 /*
1204 * Use only the address parts that contributes to spreading
1205 * The subnet prefix is not used as one can not connect to
1206 * same remote port (GUID) using the same remote QPN via two
1207 * different subnets.
1208 */
1209 /* qpn octets[1:4) & port GUID octets[12:20) */
Shlomo Pongratz9d1ad662013-02-19 15:40:22 +00001210 u32 *d32 = (u32 *) daddr;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001211 u32 hv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212
Shlomo Pongratz9d1ad662013-02-19 15:40:22 +00001213 hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001214 return hv & htbl->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215}
1216
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001217struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
1218{
1219 struct ipoib_dev_priv *priv = netdev_priv(dev);
1220 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1221 struct ipoib_neigh_hash *htbl;
1222 struct ipoib_neigh *neigh = NULL;
1223 u32 hash_val;
1224
1225 rcu_read_lock_bh();
1226
1227 htbl = rcu_dereference_bh(ntbl->htbl);
1228
1229 if (!htbl)
1230 goto out_unlock;
1231
1232 hash_val = ipoib_addr_hash(htbl, daddr);
1233 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
1234 neigh != NULL;
1235 neigh = rcu_dereference_bh(neigh->hnext)) {
1236 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1237 /* found, take one ref on behalf of the caller */
1238 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1239 /* deleted */
1240 neigh = NULL;
1241 goto out_unlock;
1242 }
Erez Shitrit61c78eea2016-06-04 15:15:19 +03001243
1244 if (likely(skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE))
1245 neigh->alive = jiffies;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001246 goto out_unlock;
1247 }
1248 }
1249
1250out_unlock:
1251 rcu_read_unlock_bh();
1252 return neigh;
1253}
1254
1255static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
1256{
1257 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1258 struct ipoib_neigh_hash *htbl;
1259 unsigned long neigh_obsolete;
1260 unsigned long dt;
1261 unsigned long flags;
1262 int i;
Christoph Lameterbd99b2e2015-09-24 12:00:05 -05001263 LIST_HEAD(remove_list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001264
1265 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1266 return;
1267
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001268 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001269
1270 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001271 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001272
1273 if (!htbl)
1274 goto out_unlock;
1275
1276 /* neigh is obsolete if it was idle for two GC periods */
1277 dt = 2 * arp_tbl.gc_interval;
1278 neigh_obsolete = jiffies - dt;
1279 /* handle possible race condition */
1280 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1281 goto out_unlock;
1282
1283 for (i = 0; i < htbl->size; i++) {
1284 struct ipoib_neigh *neigh;
1285 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1286
1287 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001288 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001289 /* was the neigh idle for two GC periods */
1290 if (time_after(neigh_obsolete, neigh->alive)) {
Christoph Lameterbd99b2e2015-09-24 12:00:05 -05001291
Christoph Lameter432c55f2015-12-21 08:42:54 -06001292 ipoib_check_and_add_mcast_sendonly(priv, neigh->daddr + 4, &remove_list);
Christoph Lameterbd99b2e2015-09-24 12:00:05 -05001293
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001294 rcu_assign_pointer(*np,
1295 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001296 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001297 /* remove from path/mc list */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001298 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001299 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1300 } else {
1301 np = &neigh->hnext;
1302 }
1303
1304 }
1305 }
1306
1307out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001308 spin_unlock_irqrestore(&priv->lock, flags);
Erez Shitrit50be28d2016-01-07 09:28:08 +02001309 ipoib_mcast_remove_list(&remove_list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001310}
1311
1312static void ipoib_reap_neigh(struct work_struct *work)
1313{
1314 struct ipoib_dev_priv *priv =
1315 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
1316
1317 __ipoib_reap_neigh(priv);
1318
1319 if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
Doug Ledford0b395782015-02-21 19:27:03 -05001320 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001321 arp_tbl.gc_interval);
1322}
1323
1324
1325static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
Moni Shoua732a2172007-10-09 19:43:36 -07001326 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001327{
1328 struct ipoib_neigh *neigh;
1329
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001330 neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001331 if (!neigh)
1332 return NULL;
1333
Moni Shoua732a2172007-10-09 19:43:36 -07001334 neigh->dev = dev;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001335 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
Roland Dreier82b39912006-12-12 14:48:18 -08001336 skb_queue_head_init(&neigh->queue);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001337 INIT_LIST_HEAD(&neigh->list);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001338 ipoib_cm_set(neigh, NULL);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001339 /* one ref on behalf of the caller */
1340 atomic_set(&neigh->refcnt, 1);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001341
1342 return neigh;
1343}
1344
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001345struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
1346 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001347{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001348 struct ipoib_dev_priv *priv = netdev_priv(dev);
1349 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1350 struct ipoib_neigh_hash *htbl;
1351 struct ipoib_neigh *neigh;
1352 u32 hash_val;
1353
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001354 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001355 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001356 if (!htbl) {
1357 neigh = NULL;
1358 goto out_unlock;
1359 }
1360
1361 /* need to add a new neigh, but maybe some other thread succeeded?
1362 * recalc hash, maybe hash resize took place so we do a search
1363 */
1364 hash_val = ipoib_addr_hash(htbl, daddr);
1365 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001366 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001367 neigh != NULL;
1368 neigh = rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001369 lockdep_is_held(&priv->lock))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001370 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1371 /* found, take one ref on behalf of the caller */
1372 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1373 /* deleted */
1374 neigh = NULL;
1375 break;
1376 }
1377 neigh->alive = jiffies;
1378 goto out_unlock;
1379 }
1380 }
1381
1382 neigh = ipoib_neigh_ctor(daddr, dev);
1383 if (!neigh)
1384 goto out_unlock;
1385
1386 /* one ref on behalf of the hash table */
1387 atomic_inc(&neigh->refcnt);
1388 neigh->alive = jiffies;
1389 /* put in hash */
1390 rcu_assign_pointer(neigh->hnext,
1391 rcu_dereference_protected(htbl->buckets[hash_val],
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001392 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001393 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1394 atomic_inc(&ntbl->entries);
1395
1396out_unlock:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001397
1398 return neigh;
1399}
1400
1401void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1402{
1403 /* neigh reference count was dropprd to zero */
1404 struct net_device *dev = neigh->dev;
1405 struct ipoib_dev_priv *priv = netdev_priv(dev);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001406 struct sk_buff *skb;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001407 if (neigh->ah)
1408 ipoib_put_ah(neigh->ah);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001409 while ((skb = __skb_dequeue(&neigh->queue))) {
Roland Dreierde903512007-09-28 15:33:51 -07001410 ++dev->stats.tx_dropped;
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001411 dev_kfree_skb_any(skb);
1412 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001413 if (ipoib_cm_get(neigh))
1414 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001415 ipoib_dbg(netdev_priv(dev),
1416 "neigh free for %06x %pI6\n",
1417 IPOIB_QPN(neigh->daddr),
1418 neigh->daddr + 4);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001419 kfree(neigh);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001420 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1421 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1422 complete(&priv->ntbl.flushed);
1423 }
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001424}
1425
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001426static void ipoib_neigh_reclaim(struct rcu_head *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001427{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001428 /* Called as a result of removal from hash table */
1429 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1430 /* note TX context may hold another ref */
1431 ipoib_neigh_put(neigh);
1432}
1433
1434void ipoib_neigh_free(struct ipoib_neigh *neigh)
1435{
1436 struct net_device *dev = neigh->dev;
1437 struct ipoib_dev_priv *priv = netdev_priv(dev);
1438 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1439 struct ipoib_neigh_hash *htbl;
1440 struct ipoib_neigh __rcu **np;
1441 struct ipoib_neigh *n;
1442 u32 hash_val;
1443
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001444 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001445 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001446 if (!htbl)
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001447 return;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001448
1449 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1450 np = &htbl->buckets[hash_val];
1451 for (n = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001452 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001453 n != NULL;
Shlomo Pongratz6c723a62012-08-13 14:39:50 +00001454 n = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001455 lockdep_is_held(&priv->lock))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001456 if (n == neigh) {
1457 /* found */
1458 rcu_assign_pointer(*np,
1459 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001460 lockdep_is_held(&priv->lock)));
Jim Foraker49b8e742013-08-08 17:44:22 -07001461 /* remove from parent list */
1462 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001463 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001464 return;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001465 } else {
1466 np = &n->hnext;
1467 }
1468 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001469}
1470
1471static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1472{
1473 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1474 struct ipoib_neigh_hash *htbl;
Bart Van Assche52374962015-05-26 15:03:48 +02001475 struct ipoib_neigh __rcu **buckets;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001476 u32 size;
1477
1478 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1479 ntbl->htbl = NULL;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001480 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1481 if (!htbl)
1482 return -ENOMEM;
1483 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1484 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1485 buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1486 if (!buckets) {
1487 kfree(htbl);
1488 return -ENOMEM;
1489 }
1490 htbl->size = size;
1491 htbl->mask = (size - 1);
1492 htbl->buckets = buckets;
Bart Van Assche52374962015-05-26 15:03:48 +02001493 RCU_INIT_POINTER(ntbl->htbl, htbl);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001494 htbl->ntbl = ntbl;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001495 atomic_set(&ntbl->entries, 0);
1496
1497 /* start garbage collection */
1498 clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
Doug Ledford0b395782015-02-21 19:27:03 -05001499 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001500 arp_tbl.gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 return 0;
1503}
1504
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001505static void neigh_hash_free_rcu(struct rcu_head *head)
1506{
1507 struct ipoib_neigh_hash *htbl = container_of(head,
1508 struct ipoib_neigh_hash,
1509 rcu);
1510 struct ipoib_neigh __rcu **buckets = htbl->buckets;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001511 struct ipoib_neigh_table *ntbl = htbl->ntbl;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001512
1513 kfree(buckets);
1514 kfree(htbl);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001515 complete(&ntbl->deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001516}
1517
1518void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1519{
1520 struct ipoib_dev_priv *priv = netdev_priv(dev);
1521 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1522 struct ipoib_neigh_hash *htbl;
1523 unsigned long flags;
1524 int i;
1525
1526 /* remove all neigh connected to a given path or mcast */
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001527 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001528
1529 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001530 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001531
1532 if (!htbl)
1533 goto out_unlock;
1534
1535 for (i = 0; i < htbl->size; i++) {
1536 struct ipoib_neigh *neigh;
1537 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1538
1539 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001540 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001541 /* delete neighs belong to this parent */
1542 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1543 rcu_assign_pointer(*np,
1544 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001545 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001546 /* remove from parent list */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001547 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001548 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1549 } else {
1550 np = &neigh->hnext;
1551 }
1552
1553 }
1554 }
1555out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001556 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001557}
1558
1559static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1560{
1561 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1562 struct ipoib_neigh_hash *htbl;
1563 unsigned long flags;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001564 int i, wait_flushed = 0;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001565
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001566 init_completion(&priv->ntbl.flushed);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001567
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001568 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001569
1570 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001571 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001572 if (!htbl)
1573 goto out_unlock;
1574
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001575 wait_flushed = atomic_read(&priv->ntbl.entries);
1576 if (!wait_flushed)
1577 goto free_htbl;
1578
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001579 for (i = 0; i < htbl->size; i++) {
1580 struct ipoib_neigh *neigh;
1581 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1582
1583 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001584 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001585 rcu_assign_pointer(*np,
1586 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001587 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001588 /* remove from path/mc list */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001589 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001590 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1591 }
1592 }
1593
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001594free_htbl:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001595 rcu_assign_pointer(ntbl->htbl, NULL);
1596 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1597
1598out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001599 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001600 if (wait_flushed)
1601 wait_for_completion(&priv->ntbl.flushed);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001602}
1603
1604static void ipoib_neigh_hash_uninit(struct net_device *dev)
1605{
1606 struct ipoib_dev_priv *priv = netdev_priv(dev);
1607 int stopped;
1608
1609 ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001610 init_completion(&priv->ntbl.deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001611 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1612
1613 /* Stop GC if called at init fail need to cancel work */
1614 stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1615 if (!stopped)
1616 cancel_delayed_work(&priv->neigh_reap_task);
1617
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001618 ipoib_flush_neighs(priv);
1619
1620 wait_for_completion(&priv->ntbl.deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001621}
1622
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1625{
1626 struct ipoib_dev_priv *priv = netdev_priv(dev);
1627
1628 /* Allocate RX/TX "rings" to hold queued skbs */
Shirley Ma0f485252006-04-10 09:43:58 -07001629 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001630 GFP_KERNEL);
Leon Romanovsky74226642016-11-03 16:44:25 +02001631 if (!priv->rx_ring)
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001632 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633
Joe Perches948579c2010-11-05 03:07:36 +00001634 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 if (!priv->tx_ring) {
1636 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
Shirley Ma0f485252006-04-10 09:43:58 -07001637 ca->name, ipoib_sendq_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638 goto out_rx_ring_cleanup;
1639 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001640
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001641 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643 if (ipoib_ib_dev_init(dev, ca, port))
1644 goto out_tx_ring_cleanup;
1645
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001646 /*
1647 * Must be after ipoib_ib_dev_init so we can allocate a per
1648 * device wq there and use it here
1649 */
1650 if (ipoib_neigh_hash_init(priv) < 0)
1651 goto out_dev_uninit;
1652
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 return 0;
1654
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001655out_dev_uninit:
1656 ipoib_ib_dev_cleanup(dev);
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658out_tx_ring_cleanup:
Roland Dreier10313cb2008-03-12 07:51:03 -07001659 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660
1661out_rx_ring_cleanup:
1662 kfree(priv->rx_ring);
1663
1664out:
1665 return -ENOMEM;
1666}
1667
1668void ipoib_dev_cleanup(struct net_device *dev)
1669{
1670 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001671 LIST_HEAD(head);
1672
1673 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
Roland Dreier1732b0e2005-11-07 10:33:11 -08001675 ipoib_delete_debug_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001676
1677 /* Delete any child interfaces first */
1678 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001679 /* Stop GC on child */
1680 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1681 cancel_delayed_work(&cpriv->neigh_reap_task);
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001682 unregister_netdevice_queue(cpriv->dev, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 }
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001684 unregister_netdevice_many(&head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001685
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001686 /*
1687 * Must be before ipoib_ib_dev_cleanup or we delete an in use
1688 * work queue
1689 */
1690 ipoib_neigh_hash_uninit(dev);
1691
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 ipoib_ib_dev_cleanup(dev);
1693
Hal Rosenstock92a6b342005-08-13 20:50:27 -07001694 kfree(priv->rx_ring);
Roland Dreier10313cb2008-03-12 07:51:03 -07001695 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696
Hal Rosenstock92a6b342005-08-13 20:50:27 -07001697 priv->rx_ring = NULL;
1698 priv->tx_ring = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699}
1700
Eli Cohen9c3c5f82016-03-11 22:58:39 +02001701static int ipoib_set_vf_link_state(struct net_device *dev, int vf, int link_state)
1702{
1703 struct ipoib_dev_priv *priv = netdev_priv(dev);
1704
1705 return ib_set_vf_link_state(priv->ca, vf, priv->port, link_state);
1706}
1707
1708static int ipoib_get_vf_config(struct net_device *dev, int vf,
1709 struct ifla_vf_info *ivf)
1710{
1711 struct ipoib_dev_priv *priv = netdev_priv(dev);
1712 int err;
1713
1714 err = ib_get_vf_config(priv->ca, vf, priv->port, ivf);
1715 if (err)
1716 return err;
1717
1718 ivf->vf = vf;
1719
1720 return 0;
1721}
1722
1723static int ipoib_set_vf_guid(struct net_device *dev, int vf, u64 guid, int type)
1724{
1725 struct ipoib_dev_priv *priv = netdev_priv(dev);
1726
1727 if (type != IFLA_VF_IB_NODE_GUID && type != IFLA_VF_IB_PORT_GUID)
1728 return -EINVAL;
1729
1730 return ib_set_vf_guid(priv->ca, vf, priv->port, guid, type);
1731}
1732
1733static int ipoib_get_vf_stats(struct net_device *dev, int vf,
1734 struct ifla_vf_stats *vf_stats)
1735{
1736 struct ipoib_dev_priv *priv = netdev_priv(dev);
1737
1738 return ib_get_vf_stats(priv->ca, vf, priv->port, vf_stats);
1739}
1740
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001741static const struct header_ops ipoib_header_ops = {
1742 .create = ipoib_hard_header,
1743};
1744
Eli Cohen9c3c5f82016-03-11 22:58:39 +02001745static const struct net_device_ops ipoib_netdev_ops_pf = {
1746 .ndo_uninit = ipoib_uninit,
1747 .ndo_open = ipoib_open,
1748 .ndo_stop = ipoib_stop,
1749 .ndo_change_mtu = ipoib_change_mtu,
1750 .ndo_fix_features = ipoib_fix_features,
1751 .ndo_start_xmit = ipoib_start_xmit,
1752 .ndo_tx_timeout = ipoib_timeout,
1753 .ndo_set_rx_mode = ipoib_set_mcast_list,
1754 .ndo_get_iflink = ipoib_get_iflink,
1755 .ndo_set_vf_link_state = ipoib_set_vf_link_state,
1756 .ndo_get_vf_config = ipoib_get_vf_config,
1757 .ndo_get_vf_stats = ipoib_get_vf_stats,
1758 .ndo_set_vf_guid = ipoib_set_vf_guid,
Mark Bloch492a7e62016-05-18 16:42:43 +03001759 .ndo_set_mac_address = ipoib_set_mac,
Eli Cohen9c3c5f82016-03-11 22:58:39 +02001760};
1761
1762static const struct net_device_ops ipoib_netdev_ops_vf = {
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001763 .ndo_uninit = ipoib_uninit,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001764 .ndo_open = ipoib_open,
1765 .ndo_stop = ipoib_stop,
1766 .ndo_change_mtu = ipoib_change_mtu,
Michał Mirosław3d96c742011-04-19 00:43:20 +00001767 .ndo_fix_features = ipoib_fix_features,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001768 .ndo_start_xmit = ipoib_start_xmit,
1769 .ndo_tx_timeout = ipoib_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001770 .ndo_set_rx_mode = ipoib_set_mcast_list,
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +02001771 .ndo_get_iflink = ipoib_get_iflink,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001772};
1773
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001774void ipoib_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775{
1776 struct ipoib_dev_priv *priv = netdev_priv(dev);
1777
Eli Cohen9c3c5f82016-03-11 22:58:39 +02001778 if (priv->hca_caps & IB_DEVICE_VIRTUAL_FUNCTION)
1779 dev->netdev_ops = &ipoib_netdev_ops_vf;
1780 else
1781 dev->netdev_ops = &ipoib_netdev_ops_pf;
1782
Roland Dreier2337f802007-10-23 19:57:54 -07001783 dev->header_ops = &ipoib_header_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001784
Eli Cohen82c24c12008-04-16 21:09:32 -07001785 ipoib_set_ethtool_ops(dev);
1786
Michal Schmidt7f1a3862013-08-21 18:50:22 +02001787 netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788
Roland Dreier2337f802007-10-23 19:57:54 -07001789 dev->watchdog_timeo = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790
Roland Dreier2337f802007-10-23 19:57:54 -07001791 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001792
Paolo Abenifc791b62016-10-13 18:26:56 +02001793 dev->hard_header_len = IPOIB_HARD_LEN;
Roland Dreier2337f802007-10-23 19:57:54 -07001794 dev->addr_len = INFINIBAND_ALEN;
1795 dev->type = ARPHRD_INFINIBAND;
1796 dev->tx_queue_len = ipoib_sendq_size * 2;
Eli Coheneb14032f2008-01-30 18:30:46 +02001797 dev->features = (NETIF_F_VLAN_CHALLENGED |
Eli Coheneb14032f2008-01-30 18:30:46 +02001798 NETIF_F_HIGHDMA);
Eric Dumazet02875872014-10-05 18:38:35 -07001799 netif_keep_dst(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001800
Linus Torvalds1da177e2005-04-16 15:20:36 -07001801 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
1802
Linus Torvalds1da177e2005-04-16 15:20:36 -07001803 priv->dev = dev;
1804
1805 spin_lock_init(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806
Erez Shitritf47944c2013-10-16 17:37:49 +03001807 init_rwsem(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808
1809 INIT_LIST_HEAD(&priv->path_list);
1810 INIT_LIST_HEAD(&priv->child_intfs);
1811 INIT_LIST_HEAD(&priv->dead_ahs);
1812 INIT_LIST_HEAD(&priv->multicast_list);
1813
David Howellsc4028952006-11-22 14:57:56 +00001814 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
Yossi Etigine8224e42008-09-16 11:57:45 -07001815 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
Moni Shouaee1e2c82008-07-14 23:48:49 -07001816 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
1817 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
1818 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
David Howellsc4028952006-11-22 14:57:56 +00001819 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1820 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001821 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001822}
1823
1824struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
1825{
1826 struct net_device *dev;
1827
Tom Gundersenc835a672014-07-14 16:37:24 +02001828 dev = alloc_netdev((int)sizeof(struct ipoib_dev_priv), name,
1829 NET_NAME_UNKNOWN, ipoib_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 if (!dev)
1831 return NULL;
1832
1833 return netdev_priv(dev);
1834}
1835
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001836static ssize_t show_pkey(struct device *dev,
1837 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001839 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840
1841 return sprintf(buf, "0x%04x\n", priv->pkey);
1842}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001843static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001845static ssize_t show_umcast(struct device *dev,
1846 struct device_attribute *attr, char *buf)
1847{
1848 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1849
1850 return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1851}
1852
Or Gerlitz862096a2012-09-27 12:06:02 +00001853void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001854{
Or Gerlitz862096a2012-09-27 12:06:02 +00001855 struct ipoib_dev_priv *priv = netdev_priv(ndev);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001856
1857 if (umcast_val > 0) {
1858 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1859 ipoib_warn(priv, "ignoring multicast groups joined directly "
1860 "by userspace\n");
1861 } else
1862 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
Or Gerlitz862096a2012-09-27 12:06:02 +00001863}
1864
1865static ssize_t set_umcast(struct device *dev,
1866 struct device_attribute *attr,
1867 const char *buf, size_t count)
1868{
1869 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1870
1871 ipoib_set_umcast(to_net_dev(dev), umcast_val);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001872
1873 return count;
1874}
1875static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1876
1877int ipoib_add_umcast_attr(struct net_device *dev)
1878{
1879 return device_create_file(&dev->dev, &dev_attr_umcast);
1880}
1881
Mark Bloch492a7e62016-05-18 16:42:43 +03001882static void set_base_guid(struct ipoib_dev_priv *priv, union ib_gid *gid)
1883{
1884 struct ipoib_dev_priv *child_priv;
1885 struct net_device *netdev = priv->dev;
1886
Mark Bloch9b299532016-06-04 15:15:22 +03001887 netif_addr_lock_bh(netdev);
Mark Bloch492a7e62016-05-18 16:42:43 +03001888
1889 memcpy(&priv->local_gid.global.interface_id,
1890 &gid->global.interface_id,
1891 sizeof(gid->global.interface_id));
1892 memcpy(netdev->dev_addr + 4, &priv->local_gid, sizeof(priv->local_gid));
1893 clear_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
1894
Mark Bloch9b299532016-06-04 15:15:22 +03001895 netif_addr_unlock_bh(netdev);
Mark Bloch492a7e62016-05-18 16:42:43 +03001896
1897 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
1898 down_read(&priv->vlan_rwsem);
1899 list_for_each_entry(child_priv, &priv->child_intfs, list)
1900 set_base_guid(child_priv, gid);
1901 up_read(&priv->vlan_rwsem);
1902 }
1903}
1904
1905static int ipoib_check_lladdr(struct net_device *dev,
1906 struct sockaddr_storage *ss)
1907{
1908 union ib_gid *gid = (union ib_gid *)(ss->__data + 4);
1909 int ret = 0;
1910
Mark Bloch9b299532016-06-04 15:15:22 +03001911 netif_addr_lock_bh(dev);
Mark Bloch492a7e62016-05-18 16:42:43 +03001912
1913 /* Make sure the QPN, reserved and subnet prefix match the current
1914 * lladdr, it also makes sure the lladdr is unicast.
1915 */
1916 if (memcmp(dev->dev_addr, ss->__data,
1917 4 + sizeof(gid->global.subnet_prefix)) ||
1918 gid->global.interface_id == 0)
1919 ret = -EINVAL;
1920
Mark Bloch9b299532016-06-04 15:15:22 +03001921 netif_addr_unlock_bh(dev);
Mark Bloch492a7e62016-05-18 16:42:43 +03001922
1923 return ret;
1924}
1925
1926static int ipoib_set_mac(struct net_device *dev, void *addr)
1927{
1928 struct ipoib_dev_priv *priv = netdev_priv(dev);
1929 struct sockaddr_storage *ss = addr;
1930 int ret;
1931
1932 if (!(dev->priv_flags & IFF_LIVE_ADDR_CHANGE) && netif_running(dev))
1933 return -EBUSY;
1934
1935 ret = ipoib_check_lladdr(dev, ss);
1936 if (ret)
1937 return ret;
1938
1939 set_base_guid(priv, (union ib_gid *)(ss->__data + 4));
1940
1941 queue_work(ipoib_workqueue, &priv->flush_light);
1942
1943 return 0;
1944}
1945
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001946static ssize_t create_child(struct device *dev,
1947 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 const char *buf, size_t count)
1949{
1950 int pkey;
1951 int ret;
1952
1953 if (sscanf(buf, "%i", &pkey) != 1)
1954 return -EINVAL;
1955
Or Gerlitz3d790a42013-07-18 14:02:31 +03001956 if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957 return -EINVAL;
1958
Roland Dreier4ce05932005-08-19 12:03:17 -07001959 /*
1960 * Set the full membership bit, so that we join the right
1961 * broadcast group, etc.
1962 */
1963 pkey |= 0x8000;
1964
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001965 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966
1967 return ret ? ret : count;
1968}
Or Gerlitz7a52b342010-06-06 04:59:16 +00001969static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001971static ssize_t delete_child(struct device *dev,
1972 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 const char *buf, size_t count)
1974{
1975 int pkey;
1976 int ret;
1977
1978 if (sscanf(buf, "%i", &pkey) != 1)
1979 return -EINVAL;
1980
1981 if (pkey < 0 || pkey > 0xffff)
1982 return -EINVAL;
1983
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001984 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985
1986 return ret ? ret : count;
1987
1988}
Or Gerlitz7a52b342010-06-06 04:59:16 +00001989static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990
1991int ipoib_add_pkey_attr(struct net_device *dev)
1992{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001993 return device_create_file(&dev->dev, &dev_attr_pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994}
1995
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001996int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
1997{
Or Gerlitz4a061b22015-12-18 10:59:46 +02001998 priv->hca_caps = hca->attrs.device_cap_flags;
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001999
2000 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
Yuval Shaia5faba542016-07-20 01:30:06 -07002001 priv->dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
Michał Mirosław3d96c742011-04-19 00:43:20 +00002002
2003 if (priv->hca_caps & IB_DEVICE_UD_TSO)
2004 priv->dev->hw_features |= NETIF_F_TSO;
2005
2006 priv->dev->features |= priv->dev->hw_features;
Or Gerlitz83bb63f2008-10-22 15:49:49 -07002007 }
2008
Or Gerlitz83bb63f2008-10-22 15:49:49 -07002009 return 0;
2010}
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012static struct net_device *ipoib_add_port(const char *format,
2013 struct ib_device *hca, u8 port)
2014{
2015 struct ipoib_dev_priv *priv;
Shirley Mabc7b3a32008-04-23 11:55:45 -07002016 struct ib_port_attr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 int result = -ENOMEM;
2018
2019 priv = ipoib_intf_alloc(format);
2020 if (!priv)
2021 goto alloc_mem_failed;
2022
2023 SET_NETDEV_DEV(priv->dev, hca->dma_device);
Eli Cohenc3aa9b12010-09-20 07:05:06 +00002024 priv->dev->dev_id = port - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
Amir Vadai58e9cc92015-07-01 14:31:01 +03002026 result = ib_query_port(hca, port, &attr);
2027 if (!result)
Shirley Mabc7b3a32008-04-23 11:55:45 -07002028 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
2029 else {
2030 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
2031 hca->name, port);
2032 goto device_init_failed;
2033 }
2034
2035 /* MTU will be reset when mcast join happens */
2036 priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
2037 priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
Jarod Wilsonb3e38932016-10-20 13:55:22 -04002038 priv->dev->max_mtu = IPOIB_CM_MTU;
Shirley Mabc7b3a32008-04-23 11:55:45 -07002039
David Miller596b9b62011-07-25 00:01:25 +00002040 priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 result = ib_query_pkey(hca, port, 0, &priv->pkey);
2043 if (result) {
2044 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
2045 hca->name, port, result);
Eli Cohenca6de172007-08-21 18:46:10 +03002046 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047 }
2048
Amir Vadai58e9cc92015-07-01 14:31:01 +03002049 result = ipoib_set_dev_features(priv, hca);
2050 if (result)
Eli Cohen60461362008-04-16 21:01:10 -07002051 goto device_init_failed;
Vladimir Sokolovskyaf40da82008-07-14 23:48:48 -07002052
Roland Dreier4ce05932005-08-19 12:03:17 -07002053 /*
2054 * Set the full membership bit, so that we join the right
2055 * broadcast group, etc.
2056 */
2057 priv->pkey |= 0x8000;
2058
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 priv->dev->broadcast[8] = priv->pkey >> 8;
2060 priv->dev->broadcast[9] = priv->pkey & 0xff;
2061
Matan Barak55ee3ab2015-10-15 18:38:45 +03002062 result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 if (result) {
2064 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
2065 hca->name, port, result);
Eli Cohenca6de172007-08-21 18:46:10 +03002066 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 } else
2068 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
Mark Bloch492a7e62016-05-18 16:42:43 +03002069 set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 result = ipoib_dev_init(priv->dev, hca, port);
2072 if (result < 0) {
2073 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
2074 hca->name, port, result);
2075 goto device_init_failed;
2076 }
2077
2078 INIT_IB_EVENT_HANDLER(&priv->event_handler,
2079 priv->ca, ipoib_event);
2080 result = ib_register_event_handler(&priv->event_handler);
2081 if (result < 0) {
2082 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
2083 "port %d (ret = %d)\n",
2084 hca->name, port, result);
2085 goto event_failed;
2086 }
2087
2088 result = register_netdev(priv->dev);
2089 if (result) {
2090 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
2091 hca->name, port, result);
2092 goto register_failed;
2093 }
2094
Roland Dreier1732b0e2005-11-07 10:33:11 -08002095 ipoib_create_debug_files(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02002097 if (ipoib_cm_add_mode_attr(priv->dev))
2098 goto sysfs_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099 if (ipoib_add_pkey_attr(priv->dev))
2100 goto sysfs_failed;
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02002101 if (ipoib_add_umcast_attr(priv->dev))
2102 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002103 if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002105 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002106 goto sysfs_failed;
2107
2108 return priv->dev;
2109
2110sysfs_failed:
Roland Dreier1732b0e2005-11-07 10:33:11 -08002111 ipoib_delete_debug_files(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 unregister_netdev(priv->dev);
2113
2114register_failed:
2115 ib_unregister_event_handler(&priv->event_handler);
Doug Ledford0b395782015-02-21 19:27:03 -05002116 flush_workqueue(ipoib_workqueue);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00002117 /* Stop GC if started before flush */
2118 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
2119 cancel_delayed_work(&priv->neigh_reap_task);
Doug Ledford0b395782015-02-21 19:27:03 -05002120 flush_workqueue(priv->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002121
2122event_failed:
2123 ipoib_dev_cleanup(priv->dev);
2124
2125device_init_failed:
2126 free_netdev(priv->dev);
2127
2128alloc_mem_failed:
2129 return ERR_PTR(result);
2130}
2131
2132static void ipoib_add_one(struct ib_device *device)
2133{
2134 struct list_head *dev_list;
2135 struct net_device *dev;
2136 struct ipoib_dev_priv *priv;
Hal Rosenstock41390322015-06-29 09:57:00 -04002137 int p;
Michael Wang8e37ab62015-05-05 14:50:24 +02002138 int count = 0;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002139
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
2141 if (!dev_list)
2142 return;
2143
2144 INIT_LIST_HEAD(dev_list);
2145
Hal Rosenstock41390322015-06-29 09:57:00 -04002146 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
Michael Wang8e37ab62015-05-05 14:50:24 +02002147 if (!rdma_protocol_ib(device, p))
Eli Cohen7b4c8762010-09-27 17:51:11 -07002148 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002149 dev = ipoib_add_port("ib%d", device, p);
2150 if (!IS_ERR(dev)) {
2151 priv = netdev_priv(dev);
2152 list_add_tail(&priv->list, dev_list);
Michael Wang8e37ab62015-05-05 14:50:24 +02002153 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002154 }
2155 }
2156
Michael Wang8e37ab62015-05-05 14:50:24 +02002157 if (!count) {
2158 kfree(dev_list);
2159 return;
2160 }
2161
Linus Torvalds1da177e2005-04-16 15:20:36 -07002162 ib_set_client_data(device, &ipoib_client, dev_list);
2163}
2164
Haggai Eran7c1eb452015-07-30 17:50:14 +03002165static void ipoib_remove_one(struct ib_device *device, void *client_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002166{
2167 struct ipoib_dev_priv *priv, *tmp;
Haggai Eran7c1eb452015-07-30 17:50:14 +03002168 struct list_head *dev_list = client_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002169
Itai Garbi5a2815f2013-02-19 15:40:24 +00002170 if (!dev_list)
2171 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002172
2173 list_for_each_entry_safe(priv, tmp, dev_list, list) {
2174 ib_unregister_event_handler(&priv->event_handler);
Doug Ledford0b395782015-02-21 19:27:03 -05002175 flush_workqueue(ipoib_workqueue);
Roland Dreiera77a57a2008-08-19 15:01:32 -07002176
Erez Shitrit198b12f2016-06-04 15:15:20 +03002177 /* mark interface in the middle of destruction */
2178 set_bit(IPOIB_FLAG_GOING_DOWN, &priv->flags);
2179
Roland Dreiera77a57a2008-08-19 15:01:32 -07002180 rtnl_lock();
2181 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
2182 rtnl_unlock();
2183
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00002184 /* Stop GC */
2185 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
2186 cancel_delayed_work(&priv->neigh_reap_task);
Doug Ledford0b395782015-02-21 19:27:03 -05002187 flush_workqueue(priv->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188
2189 unregister_netdev(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002190 free_netdev(priv->dev);
2191 }
Michael S. Tsirkin06c56e42005-09-01 09:19:02 -07002192
2193 kfree(dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002194}
2195
2196static int __init ipoib_init_module(void)
2197{
2198 int ret;
2199
Shirley Ma0f485252006-04-10 09:43:58 -07002200 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
2201 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
2202 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
2203
2204 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
2205 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -07002206 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08002207#ifdef CONFIG_INFINIBAND_IPOIB_CM
2208 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
2209#endif
Shirley Ma0f485252006-04-10 09:43:58 -07002210
Eli Cohenf89271da2008-07-14 23:48:44 -07002211 /*
2212 * When copying small received packets, we only copy from the
2213 * linear data part of the SKB, so we rely on this condition.
2214 */
2215 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
2216
Linus Torvalds1da177e2005-04-16 15:20:36 -07002217 ret = ipoib_register_debugfs();
2218 if (ret)
2219 return ret;
2220
2221 /*
Doug Ledford0b395782015-02-21 19:27:03 -05002222 * We create a global workqueue here that is used for all flush
2223 * operations. However, if you attempt to flush a workqueue
2224 * from a task on that same workqueue, it deadlocks the system.
2225 * We want to be able to flush the tasks associated with a
2226 * specific net device, so we also create a workqueue for each
2227 * netdevice. We queue up the tasks for that device only on
2228 * its private workqueue, and we only queue up flush events
2229 * on our global flush workqueue. This avoids the deadlocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230 */
Bhaktipriya Shridhar855cda62016-08-15 23:44:26 +05302231 ipoib_workqueue = alloc_ordered_workqueue("ipoib_flush",
2232 WQ_MEM_RECLAIM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233 if (!ipoib_workqueue) {
2234 ret = -ENOMEM;
2235 goto err_fs;
2236 }
2237
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002238 ib_sa_register_client(&ipoib_sa_client);
2239
Linus Torvalds1da177e2005-04-16 15:20:36 -07002240 ret = ib_register_client(&ipoib_client);
2241 if (ret)
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002242 goto err_sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Or Gerlitz9baa0b02012-09-13 05:56:36 +00002244 ret = ipoib_netlink_init();
2245 if (ret)
2246 goto err_client;
2247
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 return 0;
2249
Or Gerlitz9baa0b02012-09-13 05:56:36 +00002250err_client:
2251 ib_unregister_client(&ipoib_client);
2252
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002253err_sa:
2254 ib_sa_unregister_client(&ipoib_sa_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 destroy_workqueue(ipoib_workqueue);
2256
Roland Dreier9adec1a2005-04-16 15:26:07 -07002257err_fs:
2258 ipoib_unregister_debugfs();
2259
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 return ret;
2261}
2262
2263static void __exit ipoib_cleanup_module(void)
2264{
Or Gerlitz9baa0b02012-09-13 05:56:36 +00002265 ipoib_netlink_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002266 ib_unregister_client(&ipoib_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002267 ib_sa_unregister_client(&ipoib_sa_client);
Roland Dreier9adec1a2005-04-16 15:26:07 -07002268 ipoib_unregister_debugfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269 destroy_workqueue(ipoib_workqueue);
2270}
2271
2272module_init(ipoib_init_module);
2273module_exit(ipoib_cleanup_module);