blob: 17c5bc7e8957446b4f31aa76cd19bb99501d198b [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
Shamir Rabinovitchd20bfe22017-03-29 06:21:59 -0400111#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
112static int ipoib_netdev_event(struct notifier_block *this,
113 unsigned long event, void *ptr)
114{
115 struct netdev_notifier_info *ni = ptr;
116 struct net_device *dev = ni->dev;
117
118 if (dev->netdev_ops->ndo_open != ipoib_open)
119 return NOTIFY_DONE;
120
121 switch (event) {
122 case NETDEV_REGISTER:
123 ipoib_create_debug_files(dev);
124 break;
125 case NETDEV_CHANGENAME:
126 ipoib_delete_debug_files(dev);
127 ipoib_create_debug_files(dev);
128 break;
129 case NETDEV_UNREGISTER:
130 ipoib_delete_debug_files(dev);
131 break;
132 }
133
134 return NOTIFY_DONE;
135}
136#endif
137
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138int ipoib_open(struct net_device *dev)
139{
140 struct ipoib_dev_priv *priv = netdev_priv(dev);
141
142 ipoib_dbg(priv, "bringing up interface\n");
143
Michal Schmidt437708c2014-01-17 19:47:25 +0100144 netif_carrier_off(dev);
145
Yossi Etigine028cc52009-04-20 13:58:08 -0700146 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Erez Shitrit3b561132016-05-25 22:02:07 +0300148 priv->sm_fullmember_sendonly_support = false;
149
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500150 if (ipoib_ib_dev_open(dev)) {
Alex Estrindd57c932014-08-06 14:40:32 -0400151 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
152 return 0;
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800153 goto err_disable;
Alex Estrindd57c932014-08-06 14:40:32 -0400154 }
Yossi Etiginfe25c562008-11-12 10:24:36 -0800155
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800156 if (ipoib_ib_dev_up(dev))
157 goto err_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
160 struct ipoib_dev_priv *cpriv;
161
162 /* Bring up any child interfaces too */
Erez Shitritf47944c2013-10-16 17:37:49 +0300163 down_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 list_for_each_entry(cpriv, &priv->child_intfs, list) {
165 int flags;
166
167 flags = cpriv->dev->flags;
168 if (flags & IFF_UP)
169 continue;
170
171 dev_change_flags(cpriv->dev, flags | IFF_UP);
172 }
Erez Shitritf47944c2013-10-16 17:37:49 +0300173 up_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175
176 netif_start_queue(dev);
177
178 return 0;
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800179
180err_stop:
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500181 ipoib_ib_dev_stop(dev);
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800182
183err_disable:
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800184 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
185
186 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
189static int ipoib_stop(struct net_device *dev)
190{
191 struct ipoib_dev_priv *priv = netdev_priv(dev);
192
193 ipoib_dbg(priv, "stopping interface\n");
194
195 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
196
197 netif_stop_queue(dev);
198
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500199 ipoib_ib_dev_down(dev);
200 ipoib_ib_dev_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201
202 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
203 struct ipoib_dev_priv *cpriv;
204
205 /* Bring down any child interfaces too */
Erez Shitritf47944c2013-10-16 17:37:49 +0300206 down_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 list_for_each_entry(cpriv, &priv->child_intfs, list) {
208 int flags;
209
210 flags = cpriv->dev->flags;
211 if (!(flags & IFF_UP))
212 continue;
213
214 dev_change_flags(cpriv->dev, flags & ~IFF_UP);
215 }
Erez Shitritf47944c2013-10-16 17:37:49 +0300216 up_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 }
218
219 return 0;
220}
221
Or Gerlitz9baa0b02012-09-13 05:56:36 +0000222static void ipoib_uninit(struct net_device *dev)
223{
224 ipoib_dev_cleanup(dev);
225}
226
David S. Miller9ca36f72011-11-16 18:05:50 -0500227static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
Michał Mirosław3d96c742011-04-19 00:43:20 +0000228{
229 struct ipoib_dev_priv *priv = netdev_priv(dev);
230
231 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
Yuval Shaiac4268772015-07-12 01:24:09 -0700232 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
Michał Mirosław3d96c742011-04-19 00:43:20 +0000233
234 return features;
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
238{
239 struct ipoib_dev_priv *priv = netdev_priv(dev);
240
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200241 /* dev->mtu > 2K ==> connected mode */
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800242 if (ipoib_cm_admin_enabled(dev)) {
243 if (new_mtu > ipoib_cm_max_mtu(dev))
244 return -EINVAL;
245
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200246 if (new_mtu > priv->mcast_mtu)
247 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
248 priv->mcast_mtu);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800249
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200250 dev->mtu = new_mtu;
251 return 0;
252 }
253
Shirley Mabc7b3a32008-04-23 11:55:45 -0700254 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -EINVAL;
256
257 priv->admin_mtu = new_mtu;
258
259 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
260
261 return 0;
262}
263
Guy Shapiroddde8962015-07-30 17:50:16 +0300264/* Called with an RCU read lock taken */
265static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr,
266 struct net_device *dev)
267{
268 struct net *net = dev_net(dev);
269 struct in_device *in_dev;
270 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
271 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
272 __be32 ret_addr;
273
274 switch (addr->sa_family) {
275 case AF_INET:
276 in_dev = in_dev_get(dev);
277 if (!in_dev)
278 return false;
279
280 ret_addr = inet_confirm_addr(net, in_dev, 0,
281 addr_in->sin_addr.s_addr,
282 RT_SCOPE_HOST);
283 in_dev_put(in_dev);
284 if (ret_addr)
285 return true;
286
287 break;
288 case AF_INET6:
289 if (IS_ENABLED(CONFIG_IPV6) &&
290 ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1))
291 return true;
292
293 break;
294 }
295 return false;
296}
297
298/**
299 * Find the master net_device on top of the given net_device.
300 * @dev: base IPoIB net_device
301 *
302 * Returns the master net_device with a reference held, or the same net_device
303 * if no master exists.
304 */
305static struct net_device *ipoib_get_master_net_dev(struct net_device *dev)
306{
307 struct net_device *master;
308
309 rcu_read_lock();
310 master = netdev_master_upper_dev_get_rcu(dev);
311 if (master)
312 dev_hold(master);
313 rcu_read_unlock();
314
315 if (master)
316 return master;
317
318 dev_hold(dev);
319 return dev;
320}
321
322/**
323 * Find a net_device matching the given address, which is an upper device of
324 * the given net_device.
325 * @addr: IP address to look for.
326 * @dev: base IPoIB net_device
327 *
328 * If found, returns the net_device with a reference held. Otherwise return
329 * NULL.
330 */
331static struct net_device *ipoib_get_net_dev_match_addr(
332 const struct sockaddr *addr, struct net_device *dev)
333{
334 struct net_device *upper,
335 *result = NULL;
336 struct list_head *iter;
337
338 rcu_read_lock();
339 if (ipoib_is_dev_match_addr_rcu(addr, dev)) {
340 dev_hold(dev);
341 result = dev;
342 goto out;
343 }
344
345 netdev_for_each_all_upper_dev_rcu(dev, upper, iter) {
346 if (ipoib_is_dev_match_addr_rcu(addr, upper)) {
347 dev_hold(upper);
348 result = upper;
349 break;
350 }
351 }
352out:
353 rcu_read_unlock();
354 return result;
355}
356
357/* returns the number of IPoIB netdevs on top a given ipoib device matching a
358 * pkey_index and address, if one exists.
359 *
360 * @found_net_dev: contains a matching net_device if the return value >= 1,
361 * with a reference held. */
362static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
363 const union ib_gid *gid,
364 u16 pkey_index,
365 const struct sockaddr *addr,
366 int nesting,
367 struct net_device **found_net_dev)
368{
369 struct ipoib_dev_priv *child_priv;
370 struct net_device *net_dev = NULL;
371 int matches = 0;
372
373 if (priv->pkey_index == pkey_index &&
374 (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
375 if (!addr) {
376 net_dev = ipoib_get_master_net_dev(priv->dev);
377 } else {
378 /* Verify the net_device matches the IP address, as
379 * IPoIB child devices currently share a GID. */
380 net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev);
381 }
382 if (net_dev) {
383 if (!*found_net_dev)
384 *found_net_dev = net_dev;
385 else
386 dev_put(net_dev);
387 ++matches;
388 }
389 }
390
391 /* Check child interfaces */
392 down_read_nested(&priv->vlan_rwsem, nesting);
393 list_for_each_entry(child_priv, &priv->child_intfs, list) {
394 matches += ipoib_match_gid_pkey_addr(child_priv, gid,
395 pkey_index, addr,
396 nesting + 1,
397 found_net_dev);
398 if (matches > 1)
399 break;
400 }
401 up_read(&priv->vlan_rwsem);
402
403 return matches;
404}
405
406/* Returns the number of matching net_devs found (between 0 and 2). Also
407 * return the matching net_device in the @net_dev parameter, holding a
408 * reference to the net_device, if the number of matches >= 1 */
409static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port,
410 u16 pkey_index,
411 const union ib_gid *gid,
412 const struct sockaddr *addr,
413 struct net_device **net_dev)
414{
415 struct ipoib_dev_priv *priv;
416 int matches = 0;
417
418 *net_dev = NULL;
419
420 list_for_each_entry(priv, dev_list, list) {
421 if (priv->port != port)
422 continue;
423
424 matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
425 addr, 0, net_dev);
426 if (matches > 1)
427 break;
428 }
429
430 return matches;
431}
432
433static struct net_device *ipoib_get_net_dev_by_params(
434 struct ib_device *dev, u8 port, u16 pkey,
435 const union ib_gid *gid, const struct sockaddr *addr,
436 void *client_data)
437{
438 struct net_device *net_dev;
439 struct list_head *dev_list = client_data;
440 u16 pkey_index;
441 int matches;
442 int ret;
443
444 if (!rdma_protocol_ib(dev, port))
445 return NULL;
446
447 ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
448 if (ret)
449 return NULL;
450
451 if (!dev_list)
452 return NULL;
453
454 /* See if we can find a unique device matching the L2 parameters */
455 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
456 gid, NULL, &net_dev);
457
458 switch (matches) {
459 case 0:
460 return NULL;
461 case 1:
462 return net_dev;
463 }
464
465 dev_put(net_dev);
466
467 /* Couldn't find a unique device with L2 parameters only. Use L3
468 * address to uniquely match the net device */
469 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
470 gid, addr, &net_dev);
471 switch (matches) {
472 case 0:
473 return NULL;
474 default:
475 dev_warn_ratelimited(&dev->dev,
476 "duplicate IP address detected\n");
477 /* Fall through */
478 case 1:
479 return net_dev;
480 }
481}
482
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700483int ipoib_set_mode(struct net_device *dev, const char *buf)
484{
485 struct ipoib_dev_priv *priv = netdev_priv(dev);
486
487 /* flush paths if we switch modes so that connections are restarted */
488 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
489 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
490 ipoib_warn(priv, "enabling connected mode "
491 "will cause multicast packet drops\n");
492 netdev_update_features(dev);
Erez Shitritedcd2a72015-06-07 13:36:11 +0300493 dev_set_mtu(dev, ipoib_cm_max_mtu(dev));
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700494 rtnl_unlock();
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100495 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700496
497 ipoib_flush_paths(dev);
Feras Daoud16260762016-12-28 14:47:23 +0200498 return (!rtnl_trylock()) ? -EBUSY : 0;
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700499 }
500
501 if (!strcmp(buf, "datagram\n")) {
502 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
503 netdev_update_features(dev);
504 dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
505 rtnl_unlock();
506 ipoib_flush_paths(dev);
Feras Daoud16260762016-12-28 14:47:23 +0200507 return (!rtnl_trylock()) ? -EBUSY : 0;
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700508 }
509
510 return -EINVAL;
511}
512
Erez Shitrit546481c2016-08-28 10:58:31 +0300513struct ipoib_path *__path_find(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514{
515 struct ipoib_dev_priv *priv = netdev_priv(dev);
516 struct rb_node *n = priv->path_tree.rb_node;
517 struct ipoib_path *path;
518 int ret;
519
520 while (n) {
521 path = rb_entry(n, struct ipoib_path, rb_node);
522
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300523 ret = memcmp(gid, path->pathrec.dgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 sizeof (union ib_gid));
525
526 if (ret < 0)
527 n = n->rb_left;
528 else if (ret > 0)
529 n = n->rb_right;
530 else
531 return path;
532 }
533
534 return NULL;
535}
536
537static int __path_add(struct net_device *dev, struct ipoib_path *path)
538{
539 struct ipoib_dev_priv *priv = netdev_priv(dev);
540 struct rb_node **n = &priv->path_tree.rb_node;
541 struct rb_node *pn = NULL;
542 struct ipoib_path *tpath;
543 int ret;
544
545 while (*n) {
546 pn = *n;
547 tpath = rb_entry(pn, struct ipoib_path, rb_node);
548
549 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
550 sizeof (union ib_gid));
551 if (ret < 0)
552 n = &pn->rb_left;
553 else if (ret > 0)
554 n = &pn->rb_right;
555 else
556 return -EEXIST;
557 }
558
559 rb_link_node(&path->rb_node, pn, n);
560 rb_insert_color(&path->rb_node, &priv->path_tree);
561
562 list_add_tail(&path->list, &priv->path_list);
563
564 return 0;
565}
566
567static void path_free(struct net_device *dev, struct ipoib_path *path)
568{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570
571 while ((skb = __skb_dequeue(&path->queue)))
572 dev_kfree_skb_irq(skb);
573
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000574 ipoib_dbg(netdev_priv(dev), "path_free\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000576 /* remove all neigh connected to this path */
577 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579 if (path->ah)
580 ipoib_put_ah(path->ah);
581
582 kfree(path);
583}
584
Roland Dreier1732b0e2005-11-07 10:33:11 -0800585#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
586
587struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
588{
589 struct ipoib_path_iter *iter;
590
591 iter = kmalloc(sizeof *iter, GFP_KERNEL);
592 if (!iter)
593 return NULL;
594
595 iter->dev = dev;
596 memset(iter->path.pathrec.dgid.raw, 0, 16);
597
598 if (ipoib_path_iter_next(iter)) {
599 kfree(iter);
600 return NULL;
601 }
602
603 return iter;
604}
605
606int ipoib_path_iter_next(struct ipoib_path_iter *iter)
607{
608 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
609 struct rb_node *n;
610 struct ipoib_path *path;
611 int ret = 1;
612
613 spin_lock_irq(&priv->lock);
614
615 n = rb_first(&priv->path_tree);
616
617 while (n) {
618 path = rb_entry(n, struct ipoib_path, rb_node);
619
620 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
621 sizeof (union ib_gid)) < 0) {
622 iter->path = *path;
623 ret = 0;
624 break;
625 }
626
627 n = rb_next(n);
628 }
629
630 spin_unlock_irq(&priv->lock);
631
632 return ret;
633}
634
635void ipoib_path_iter_read(struct ipoib_path_iter *iter,
636 struct ipoib_path *path)
637{
638 *path = iter->path;
639}
640
641#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
642
Moni Shouaee1e2c82008-07-14 23:48:49 -0700643void ipoib_mark_paths_invalid(struct net_device *dev)
644{
645 struct ipoib_dev_priv *priv = netdev_priv(dev);
646 struct ipoib_path *path, *tp;
647
648 spin_lock_irq(&priv->lock);
649
650 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700651 ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
Moni Shouaee1e2c82008-07-14 23:48:49 -0700652 be16_to_cpu(path->pathrec.dlid),
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700653 path->pathrec.dgid.raw);
Moni Shouaee1e2c82008-07-14 23:48:49 -0700654 path->valid = 0;
655 }
656
657 spin_unlock_irq(&priv->lock);
658}
659
Erez Shitrit3b561132016-05-25 22:02:07 +0300660struct classport_info_context {
661 struct ipoib_dev_priv *priv;
662 struct completion done;
663 struct ib_sa_query *sa_query;
664};
665
666static void classport_info_query_cb(int status, struct ib_class_port_info *rec,
667 void *context)
668{
669 struct classport_info_context *cb_ctx = context;
670 struct ipoib_dev_priv *priv;
671
672 WARN_ON(!context);
673
674 priv = cb_ctx->priv;
675
676 if (status || !rec) {
677 pr_debug("device: %s failed query classport_info status: %d\n",
678 priv->dev->name, status);
679 /* keeps the default, will try next mcast_restart */
680 priv->sm_fullmember_sendonly_support = false;
681 goto out;
682 }
683
684 if (ib_get_cpi_capmask2(rec) &
685 IB_SA_CAP_MASK2_SENDONLY_FULL_MEM_SUPPORT) {
686 pr_debug("device: %s enabled fullmember-sendonly for sendonly MCG\n",
687 priv->dev->name);
688 priv->sm_fullmember_sendonly_support = true;
689 } else {
690 pr_debug("device: %s disabled fullmember-sendonly for sendonly MCG\n",
691 priv->dev->name);
692 priv->sm_fullmember_sendonly_support = false;
693 }
694
695out:
696 complete(&cb_ctx->done);
697}
698
699int ipoib_check_sm_sendonly_fullmember_support(struct ipoib_dev_priv *priv)
700{
701 struct classport_info_context *callback_context;
702 int ret;
703
704 callback_context = kmalloc(sizeof(*callback_context), GFP_KERNEL);
705 if (!callback_context)
706 return -ENOMEM;
707
708 callback_context->priv = priv;
709 init_completion(&callback_context->done);
710
711 ret = ib_sa_classport_info_rec_query(&ipoib_sa_client,
712 priv->ca, priv->port, 3000,
713 GFP_KERNEL,
714 classport_info_query_cb,
715 callback_context,
716 &callback_context->sa_query);
717 if (ret < 0) {
718 pr_info("%s failed to send ib_sa_classport_info query, ret: %d\n",
719 priv->dev->name, ret);
720 kfree(callback_context);
721 return ret;
722 }
723
724 /* waiting for the callback to finish before returnning */
725 wait_for_completion(&callback_context->done);
726 kfree(callback_context);
727
728 return ret;
729}
730
Erez Shitrit2e539fa2017-02-01 19:10:05 +0200731static void push_pseudo_header(struct sk_buff *skb, const char *daddr)
732{
733 struct ipoib_pseudo_header *phdr;
734
735 phdr = (struct ipoib_pseudo_header *)skb_push(skb, sizeof(*phdr));
736 memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
737}
738
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739void ipoib_flush_paths(struct net_device *dev)
740{
741 struct ipoib_dev_priv *priv = netdev_priv(dev);
742 struct ipoib_path *path, *tp;
743 LIST_HEAD(remove_list);
Roland Dreier943c2462008-09-30 10:36:21 -0700744 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745
Roland Dreier943c2462008-09-30 10:36:21 -0700746 netif_tx_lock_bh(dev);
747 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748
Robert P. J. Day157de222008-04-16 21:09:26 -0700749 list_splice_init(&priv->path_list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
751 list_for_each_entry(path, &remove_list, list)
752 rb_erase(&path->rb_node, &priv->path_tree);
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 list_for_each_entry_safe(path, tp, &remove_list, list) {
755 if (path->query)
756 ib_sa_cancel_query(path->query_id, path->query);
Roland Dreier943c2462008-09-30 10:36:21 -0700757 spin_unlock_irqrestore(&priv->lock, flags);
758 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 wait_for_completion(&path->done);
760 path_free(dev, path);
Roland Dreier943c2462008-09-30 10:36:21 -0700761 netif_tx_lock_bh(dev);
762 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
Roland Dreier943c2462008-09-30 10:36:21 -0700764
765 spin_unlock_irqrestore(&priv->lock, flags);
766 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767}
768
769static void path_rec_completion(int status,
770 struct ib_sa_path_rec *pathrec,
771 void *path_ptr)
772{
773 struct ipoib_path *path = path_ptr;
774 struct net_device *dev = path->dev;
775 struct ipoib_dev_priv *priv = netdev_priv(dev);
776 struct ipoib_ah *ah = NULL;
Roland Dreierc9da4ba2008-09-25 15:26:15 -0700777 struct ipoib_ah *old_ah = NULL;
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700778 struct ipoib_neigh *neigh, *tn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779 struct sk_buff_head skqueue;
780 struct sk_buff *skb;
781 unsigned long flags;
782
Roland Dreier843613b2007-02-26 12:57:08 -0800783 if (!status)
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700784 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700785 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 else
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700787 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700788 status, path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789
790 skb_queue_head_init(&skqueue);
791
792 if (!status) {
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700793 struct ib_ah_attr av;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700795 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
796 ah = ipoib_create_ah(dev, priv->pd, &av);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 }
798
799 spin_lock_irqsave(&priv->lock, flags);
800
Mike Marciniszyn38743972011-11-21 08:43:54 -0500801 if (!IS_ERR_OR_NULL(ah)) {
Erez Shitrit15e113d2017-11-14 14:51:53 +0200802 /*
803 * pathrec.dgid is used as the database key from the LLADDR,
804 * it must remain unchanged even if the SA returns a different
805 * GID to use in the AH.
806 */
807 if (memcmp(pathrec->dgid.raw, path->pathrec.dgid.raw,
808 sizeof(union ib_gid))) {
809 ipoib_dbg(
810 priv,
811 "%s got PathRec for gid %pI6 while asked for %pI6\n",
812 dev->name, pathrec->dgid.raw,
813 path->pathrec.dgid.raw);
814 memcpy(pathrec->dgid.raw, path->pathrec.dgid.raw,
815 sizeof(union ib_gid));
816 }
817
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818 path->pathrec = *pathrec;
819
Roland Dreierc9da4ba2008-09-25 15:26:15 -0700820 old_ah = path->ah;
821 path->ah = ah;
822
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
824 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
825
826 while ((skb = __skb_dequeue(&path->queue)))
827 __skb_queue_tail(&skqueue, skb);
828
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700829 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
Moni Shouaee1e2c82008-07-14 23:48:49 -0700830 if (neigh->ah) {
831 WARN_ON(neigh->ah != old_ah);
832 /*
833 * Dropping the ah reference inside
834 * priv->lock is safe here, because we
835 * will hold one more reference from
836 * the original value of path->ah (ie
837 * old_ah).
838 */
839 ipoib_put_ah(neigh->ah);
840 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 kref_get(&path->ah->ref);
842 neigh->ah = path->ah;
843
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000844 if (ipoib_cm_enabled(dev, neigh->daddr)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200845 if (!ipoib_cm_get(neigh))
846 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
847 path,
848 neigh));
849 if (!ipoib_cm_get(neigh)) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000850 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200851 continue;
852 }
853 }
854
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 while ((skb = __skb_dequeue(&neigh->queue)))
856 __skb_queue_tail(&skqueue, skb);
857 }
Moni Shouaee1e2c82008-07-14 23:48:49 -0700858 path->valid = 1;
Roland Dreier5872a9f2005-11-29 10:13:54 -0800859 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700860
Roland Dreier5872a9f2005-11-29 10:13:54 -0800861 path->query = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 complete(&path->done);
863
864 spin_unlock_irqrestore(&priv->lock, flags);
865
Roland Dreierf72dd562013-02-25 09:42:15 -0800866 if (IS_ERR_OR_NULL(ah))
867 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
868
Moni Shouaee1e2c82008-07-14 23:48:49 -0700869 if (old_ah)
870 ipoib_put_ah(old_ah);
871
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872 while ((skb = __skb_dequeue(&skqueue))) {
873 skb->dev = dev;
874 if (dev_queue_xmit(skb))
875 ipoib_warn(priv, "dev_queue_xmit failed "
876 "to requeue packet\n");
877 }
878}
879
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300880static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881{
882 struct ipoib_dev_priv *priv = netdev_priv(dev);
883 struct ipoib_path *path;
884
Jack Morgenstein1401b532007-11-26 10:41:19 +0200885 if (!priv->broadcast)
886 return NULL;
887
Roland Dreier21a38482005-11-02 10:07:59 -0800888 path = kzalloc(sizeof *path, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (!path)
890 return NULL;
891
Roland Dreier21a38482005-11-02 10:07:59 -0800892 path->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893
894 skb_queue_head_init(&path->queue);
895
896 INIT_LIST_HEAD(&path->neigh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300898 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
Roland Dreier2337f802007-10-23 19:57:54 -0700899 path->pathrec.sgid = priv->local_gid;
900 path->pathrec.pkey = cpu_to_be16(priv->pkey);
Sean Hefty81668832007-08-02 12:21:31 -0700901 path->pathrec.numb_path = 1;
902 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700903
904 return path;
905}
906
907static int path_rec_start(struct net_device *dev,
908 struct ipoib_path *path)
909{
910 struct ipoib_dev_priv *priv = netdev_priv(dev);
911
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700912 ipoib_dbg(priv, "Start path record lookup for %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700913 path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700914
Roland Dreier65c7edd2005-11-28 21:20:34 -0800915 init_completion(&path->done);
916
Linus Torvalds1da177e2005-04-16 15:20:36 -0700917 path->query_id =
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700918 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700919 &path->pathrec,
920 IB_SA_PATH_REC_DGID |
921 IB_SA_PATH_REC_SGID |
922 IB_SA_PATH_REC_NUMB_PATH |
Sean Hefty81668832007-08-02 12:21:31 -0700923 IB_SA_PATH_REC_TRAFFIC_CLASS |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700924 IB_SA_PATH_REC_PKEY,
925 1000, GFP_ATOMIC,
926 path_rec_completion,
927 path, &path->query);
928 if (path->query_id < 0) {
Or Gerlitz01b3fc82008-07-22 14:18:34 -0700929 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 path->query = NULL;
Yossi Etigin93a3ab92008-11-12 10:24:38 -0800931 complete(&path->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 return path->query_id;
933 }
934
935 return 0;
936}
937
Erez Shitritab43aaa2017-12-31 15:33:15 +0200938static struct ipoib_neigh *neigh_add_path(struct sk_buff *skb, u8 *daddr,
939 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940{
941 struct ipoib_dev_priv *priv = netdev_priv(dev);
942 struct ipoib_path *path;
943 struct ipoib_neigh *neigh;
Roland Dreier943c2462008-09-30 10:36:21 -0700944 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700945
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000946 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000947 neigh = ipoib_neigh_alloc(daddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 if (!neigh) {
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000949 spin_unlock_irqrestore(&priv->lock, flags);
Roland Dreierde903512007-09-28 15:33:51 -0700950 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 dev_kfree_skb_any(skb);
Erez Shitritab43aaa2017-12-31 15:33:15 +0200952 return NULL;
953 }
954
955 /* To avoid race condition, make sure that the
956 * neigh will be added only once.
957 */
958 if (unlikely(!list_empty(&neigh->list))) {
959 spin_unlock_irqrestore(&priv->lock, flags);
960 return neigh;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961 }
962
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000963 path = __path_find(dev, daddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964 if (!path) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000965 path = path_rec_create(dev, daddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700966 if (!path)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300967 goto err_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968
969 __path_add(dev, path);
970 }
971
972 list_add_tail(&neigh->list, &path->neigh_list);
973
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -0800974 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 kref_get(&path->ah->ref);
976 neigh->ah = path->ah;
977
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000978 if (ipoib_cm_enabled(dev, neigh->daddr)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200979 if (!ipoib_cm_get(neigh))
980 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
981 if (!ipoib_cm_get(neigh)) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000982 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200983 goto err_drop;
984 }
Paolo Abenifc791b62016-10-13 18:26:56 +0200985 if (skb_queue_len(&neigh->queue) <
986 IPOIB_MAX_PATH_REC_QUEUE) {
Erez Shitrit2e539fa2017-02-01 19:10:05 +0200987 push_pseudo_header(skb, neigh->daddr);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200988 __skb_queue_tail(&neigh->queue, skb);
Paolo Abenifc791b62016-10-13 18:26:56 +0200989 } else {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200990 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
991 skb_queue_len(&neigh->queue));
992 goto err_drop;
993 }
Roland Dreier721d67c2009-09-05 20:23:40 -0700994 } else {
995 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000996 ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
997 ipoib_neigh_put(neigh);
Erez Shitritab43aaa2017-12-31 15:33:15 +0200998 return NULL;
Roland Dreier721d67c2009-09-05 20:23:40 -0700999 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 } else {
1001 neigh->ah = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002
1003 if (!path->query && path_rec_start(dev, path))
Jim Foraker49b8e742013-08-08 17:44:22 -07001004 goto err_path;
Erez Shitrit2e539fa2017-02-01 19:10:05 +02001005 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1006 push_pseudo_header(skb, neigh->daddr);
Erez Shitrit1e85b802015-04-02 13:39:03 +03001007 __skb_queue_tail(&neigh->queue, skb);
Erez Shitrit2e539fa2017-02-01 19:10:05 +02001008 } else {
Erez Shitrit1e85b802015-04-02 13:39:03 +03001009 goto err_drop;
Erez Shitrit2e539fa2017-02-01 19:10:05 +02001010 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 }
1012
Roland Dreier943c2462008-09-30 10:36:21 -07001013 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001014 ipoib_neigh_put(neigh);
Erez Shitritab43aaa2017-12-31 15:33:15 +02001015 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001016
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001017err_path:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001018 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001019err_drop:
Roland Dreierde903512007-09-28 15:33:51 -07001020 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 dev_kfree_skb_any(skb);
1022
Roland Dreier943c2462008-09-30 10:36:21 -07001023 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001024 ipoib_neigh_put(neigh);
Erez Shitritab43aaa2017-12-31 15:33:15 +02001025
1026 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
1028
1029static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
Paolo Abenifc791b62016-10-13 18:26:56 +02001030 struct ipoib_pseudo_header *phdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031{
1032 struct ipoib_dev_priv *priv = netdev_priv(dev);
1033 struct ipoib_path *path;
Roland Dreier943c2462008-09-30 10:36:21 -07001034 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035
Roland Dreier943c2462008-09-30 10:36:21 -07001036 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Paolo Abenifc791b62016-10-13 18:26:56 +02001038 path = __path_find(dev, phdr->hwaddr + 4);
Moni Shouaee1e2c82008-07-14 23:48:49 -07001039 if (!path || !path->valid) {
Jack Morgenstein71d98b42009-02-17 14:51:47 -08001040 int new_path = 0;
1041
1042 if (!path) {
Paolo Abenifc791b62016-10-13 18:26:56 +02001043 path = path_rec_create(dev, phdr->hwaddr + 4);
Jack Morgenstein71d98b42009-02-17 14:51:47 -08001044 new_path = 1;
1045 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 if (path) {
Erez Shitrit1e85b802015-04-02 13:39:03 +03001047 if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
Erez Shitrit2e539fa2017-02-01 19:10:05 +02001048 push_pseudo_header(skb, phdr->hwaddr);
Erez Shitrit1e85b802015-04-02 13:39:03 +03001049 __skb_queue_tail(&path->queue, skb);
1050 } else {
1051 ++dev->stats.tx_dropped;
1052 dev_kfree_skb_any(skb);
1053 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054
Yossi Etiginff79ae82008-11-12 10:24:39 -08001055 if (!path->query && path_rec_start(dev, path)) {
Roland Dreier943c2462008-09-30 10:36:21 -07001056 spin_unlock_irqrestore(&priv->lock, flags);
Jack Morgenstein71d98b42009-02-17 14:51:47 -08001057 if (new_path)
1058 path_free(dev, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 return;
1060 } else
1061 __path_add(dev, path);
1062 } else {
Roland Dreierde903512007-09-28 15:33:51 -07001063 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 dev_kfree_skb_any(skb);
1065 }
1066
Roland Dreier943c2462008-09-30 10:36:21 -07001067 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 return;
1069 }
1070
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -08001071 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
1073 be16_to_cpu(path->pathrec.dlid));
1074
Roland Dreier721d67c2009-09-05 20:23:40 -07001075 spin_unlock_irqrestore(&priv->lock, flags);
Paolo Abenifc791b62016-10-13 18:26:56 +02001076 ipoib_send(dev, skb, path->ah, IPOIB_QPN(phdr->hwaddr));
Roland Dreier721d67c2009-09-05 20:23:40 -07001077 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 } else if ((path->query || !path_rec_start(dev, path)) &&
1079 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
Erez Shitrit2e539fa2017-02-01 19:10:05 +02001080 push_pseudo_header(skb, phdr->hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081 __skb_queue_tail(&path->queue, skb);
1082 } else {
Roland Dreierde903512007-09-28 15:33:51 -07001083 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 dev_kfree_skb_any(skb);
1085 }
1086
Roland Dreier943c2462008-09-30 10:36:21 -07001087 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
1090static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
1091{
1092 struct ipoib_dev_priv *priv = netdev_priv(dev);
1093 struct ipoib_neigh *neigh;
Paolo Abenifc791b62016-10-13 18:26:56 +02001094 struct ipoib_pseudo_header *phdr;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001095 struct ipoib_header *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 unsigned long flags;
1097
Paolo Abenifc791b62016-10-13 18:26:56 +02001098 phdr = (struct ipoib_pseudo_header *) skb->data;
1099 skb_pull(skb, sizeof(*phdr));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001100 header = (struct ipoib_header *) skb->data;
1101
Paolo Abenifc791b62016-10-13 18:26:56 +02001102 if (unlikely(phdr->hwaddr[4] == 0xff)) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001103 /* multicast, arrange "if" according to probability */
1104 if ((header->proto != htons(ETH_P_IP)) &&
1105 (header->proto != htons(ETH_P_IPV6)) &&
1106 (header->proto != htons(ETH_P_ARP)) &&
Patrick McHardydc850b02013-04-17 06:18:29 +00001107 (header->proto != htons(ETH_P_RARP)) &&
1108 (header->proto != htons(ETH_P_TIPC))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001109 /* ethertype not supported by IPoIB */
David Miller17e6abe2011-12-02 16:52:44 +00001110 ++dev->stats.tx_dropped;
1111 dev_kfree_skb_any(skb);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001112 return NETDEV_TX_OK;
David Miller17e6abe2011-12-02 16:52:44 +00001113 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001114 /* Add in the P_Key for multicast*/
Paolo Abenifc791b62016-10-13 18:26:56 +02001115 phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
1116 phdr->hwaddr[9] = priv->pkey & 0xff;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001117
Paolo Abenifc791b62016-10-13 18:26:56 +02001118 neigh = ipoib_neigh_get(dev, phdr->hwaddr);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001119 if (likely(neigh))
1120 goto send_using_neigh;
Paolo Abenifc791b62016-10-13 18:26:56 +02001121 ipoib_mcast_send(dev, phdr->hwaddr, skb);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001122 return NETDEV_TX_OK;
David Miller17e6abe2011-12-02 16:52:44 +00001123 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001125 /* unicast, arrange "switch" according to probability */
1126 switch (header->proto) {
1127 case htons(ETH_P_IP):
1128 case htons(ETH_P_IPV6):
Patrick McHardydc850b02013-04-17 06:18:29 +00001129 case htons(ETH_P_TIPC):
Paolo Abenifc791b62016-10-13 18:26:56 +02001130 neigh = ipoib_neigh_get(dev, phdr->hwaddr);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001131 if (unlikely(!neigh)) {
Erez Shitritab43aaa2017-12-31 15:33:15 +02001132 neigh = neigh_add_path(skb, phdr->hwaddr, dev);
1133 if (likely(!neigh))
1134 return NETDEV_TX_OK;
Yossi Etigina50df392009-01-09 14:05:11 -08001135 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001136 break;
1137 case htons(ETH_P_ARP):
1138 case htons(ETH_P_RARP):
1139 /* for unicast ARP and RARP should always perform path find */
Paolo Abenifc791b62016-10-13 18:26:56 +02001140 unicast_arp_send(skb, dev, phdr);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001141 return NETDEV_TX_OK;
1142 default:
1143 /* ethertype not supported by IPoIB */
1144 ++dev->stats.tx_dropped;
1145 dev_kfree_skb_any(skb);
1146 return NETDEV_TX_OK;
1147 }
Michael S. Tsirkin8a7f7522006-07-19 17:44:37 +03001148
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001149send_using_neigh:
1150 /* note we now hold a ref to neigh */
1151 if (ipoib_cm_get(neigh)) {
1152 if (ipoib_cm_up(neigh)) {
1153 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
1154 goto unref;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001156 } else if (neigh->ah) {
Paolo Abenifc791b62016-10-13 18:26:56 +02001157 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(phdr->hwaddr));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001158 goto unref;
1159 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001160
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001161 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
Erez Shitrit2e539fa2017-02-01 19:10:05 +02001162 push_pseudo_header(skb, phdr->hwaddr);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001163 spin_lock_irqsave(&priv->lock, flags);
1164 __skb_queue_tail(&neigh->queue, skb);
1165 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 } else {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001167 ++dev->stats.tx_dropped;
1168 dev_kfree_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001170
1171unref:
1172 ipoib_neigh_put(neigh);
1173
Linus Torvalds1da177e2005-04-16 15:20:36 -07001174 return NETDEV_TX_OK;
1175}
1176
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177static void ipoib_timeout(struct net_device *dev)
1178{
1179 struct ipoib_dev_priv *priv = netdev_priv(dev);
1180
Roland Dreier4b2d3192005-10-18 12:20:06 -07001181 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
Florian Westphal4d0e9652016-05-03 16:30:59 +02001182 jiffies_to_msecs(jiffies - dev_trans_start(dev)));
Roland Dreier4b2d3192005-10-18 12:20:06 -07001183 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
1184 netif_queue_stopped(dev),
1185 priv->tx_head, priv->tx_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186 /* XXX reset QP, etc. */
1187}
1188
1189static int ipoib_hard_header(struct sk_buff *skb,
1190 struct net_device *dev,
1191 unsigned short type,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001192 const void *daddr, const void *saddr, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193{
1194 struct ipoib_header *header;
1195
1196 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
1197
1198 header->proto = htons(type);
1199 header->reserved = 0;
1200
1201 /*
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001202 * we don't rely on dst_entry structure, always stuff the
Paolo Abenifc791b62016-10-13 18:26:56 +02001203 * destination address into skb hard header so we can figure out where
Roland Dreier936d7de2012-02-07 14:51:21 +00001204 * to send the packet later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001205 */
Erez Shitrit2e539fa2017-02-01 19:10:05 +02001206 push_pseudo_header(skb, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Paolo Abenifc791b62016-10-13 18:26:56 +02001208 return IPOIB_HARD_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209}
1210
1211static void ipoib_set_mcast_list(struct net_device *dev)
1212{
1213 struct ipoib_dev_priv *priv = netdev_priv(dev);
1214
Leonid Arsh7a343d42006-03-23 19:52:51 +02001215 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
1216 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
1217 return;
1218 }
1219
Doug Ledford0b395782015-02-21 19:27:03 -05001220 queue_work(priv->wq, &priv->restart_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221}
1222
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +02001223static int ipoib_get_iflink(const struct net_device *dev)
1224{
1225 struct ipoib_dev_priv *priv = netdev_priv(dev);
1226
Erez Shitrit2c153952015-04-16 16:34:34 +03001227 /* parent interface */
1228 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
1229 return dev->ifindex;
1230
1231 /* child/vlan interface */
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +02001232 return priv->parent->ifindex;
1233}
1234
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001235static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001236{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001237 /*
1238 * Use only the address parts that contributes to spreading
1239 * The subnet prefix is not used as one can not connect to
1240 * same remote port (GUID) using the same remote QPN via two
1241 * different subnets.
1242 */
1243 /* qpn octets[1:4) & port GUID octets[12:20) */
Shlomo Pongratz9d1ad662013-02-19 15:40:22 +00001244 u32 *d32 = (u32 *) daddr;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001245 u32 hv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Shlomo Pongratz9d1ad662013-02-19 15:40:22 +00001247 hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001248 return hv & htbl->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249}
1250
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001251struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
1252{
1253 struct ipoib_dev_priv *priv = netdev_priv(dev);
1254 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1255 struct ipoib_neigh_hash *htbl;
1256 struct ipoib_neigh *neigh = NULL;
1257 u32 hash_val;
1258
1259 rcu_read_lock_bh();
1260
1261 htbl = rcu_dereference_bh(ntbl->htbl);
1262
1263 if (!htbl)
1264 goto out_unlock;
1265
1266 hash_val = ipoib_addr_hash(htbl, daddr);
1267 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
1268 neigh != NULL;
1269 neigh = rcu_dereference_bh(neigh->hnext)) {
1270 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1271 /* found, take one ref on behalf of the caller */
1272 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1273 /* deleted */
1274 neigh = NULL;
1275 goto out_unlock;
1276 }
Erez Shitrit61c78eea2016-06-04 15:15:19 +03001277
1278 if (likely(skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE))
1279 neigh->alive = jiffies;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001280 goto out_unlock;
1281 }
1282 }
1283
1284out_unlock:
1285 rcu_read_unlock_bh();
1286 return neigh;
1287}
1288
1289static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
1290{
1291 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1292 struct ipoib_neigh_hash *htbl;
1293 unsigned long neigh_obsolete;
1294 unsigned long dt;
1295 unsigned long flags;
1296 int i;
Christoph Lameterbd99b2e2015-09-24 12:00:05 -05001297 LIST_HEAD(remove_list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001298
1299 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1300 return;
1301
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001302 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001303
1304 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001305 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001306
1307 if (!htbl)
1308 goto out_unlock;
1309
1310 /* neigh is obsolete if it was idle for two GC periods */
1311 dt = 2 * arp_tbl.gc_interval;
1312 neigh_obsolete = jiffies - dt;
1313 /* handle possible race condition */
1314 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1315 goto out_unlock;
1316
1317 for (i = 0; i < htbl->size; i++) {
1318 struct ipoib_neigh *neigh;
1319 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1320
1321 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001322 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001323 /* was the neigh idle for two GC periods */
1324 if (time_after(neigh_obsolete, neigh->alive)) {
Christoph Lameterbd99b2e2015-09-24 12:00:05 -05001325
Christoph Lameter432c55f2015-12-21 08:42:54 -06001326 ipoib_check_and_add_mcast_sendonly(priv, neigh->daddr + 4, &remove_list);
Christoph Lameterbd99b2e2015-09-24 12:00:05 -05001327
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001328 rcu_assign_pointer(*np,
1329 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001330 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001331 /* remove from path/mc list */
Feras Daoudb2e7d1f2016-12-28 14:47:27 +02001332 list_del_init(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001333 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1334 } else {
1335 np = &neigh->hnext;
1336 }
1337
1338 }
1339 }
1340
1341out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001342 spin_unlock_irqrestore(&priv->lock, flags);
Erez Shitrit50be28d2016-01-07 09:28:08 +02001343 ipoib_mcast_remove_list(&remove_list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001344}
1345
1346static void ipoib_reap_neigh(struct work_struct *work)
1347{
1348 struct ipoib_dev_priv *priv =
1349 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
1350
1351 __ipoib_reap_neigh(priv);
1352
1353 if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
Doug Ledford0b395782015-02-21 19:27:03 -05001354 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001355 arp_tbl.gc_interval);
1356}
1357
1358
1359static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
Moni Shoua732a2172007-10-09 19:43:36 -07001360 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001361{
1362 struct ipoib_neigh *neigh;
1363
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001364 neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001365 if (!neigh)
1366 return NULL;
1367
Moni Shoua732a2172007-10-09 19:43:36 -07001368 neigh->dev = dev;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001369 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
Roland Dreier82b39912006-12-12 14:48:18 -08001370 skb_queue_head_init(&neigh->queue);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001371 INIT_LIST_HEAD(&neigh->list);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001372 ipoib_cm_set(neigh, NULL);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001373 /* one ref on behalf of the caller */
1374 atomic_set(&neigh->refcnt, 1);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001375
1376 return neigh;
1377}
1378
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001379struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
1380 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001381{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001382 struct ipoib_dev_priv *priv = netdev_priv(dev);
1383 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1384 struct ipoib_neigh_hash *htbl;
1385 struct ipoib_neigh *neigh;
1386 u32 hash_val;
1387
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001388 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001389 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001390 if (!htbl) {
1391 neigh = NULL;
1392 goto out_unlock;
1393 }
1394
1395 /* need to add a new neigh, but maybe some other thread succeeded?
1396 * recalc hash, maybe hash resize took place so we do a search
1397 */
1398 hash_val = ipoib_addr_hash(htbl, daddr);
1399 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001400 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001401 neigh != NULL;
1402 neigh = rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001403 lockdep_is_held(&priv->lock))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001404 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1405 /* found, take one ref on behalf of the caller */
1406 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1407 /* deleted */
1408 neigh = NULL;
1409 break;
1410 }
1411 neigh->alive = jiffies;
1412 goto out_unlock;
1413 }
1414 }
1415
1416 neigh = ipoib_neigh_ctor(daddr, dev);
1417 if (!neigh)
1418 goto out_unlock;
1419
1420 /* one ref on behalf of the hash table */
1421 atomic_inc(&neigh->refcnt);
1422 neigh->alive = jiffies;
1423 /* put in hash */
1424 rcu_assign_pointer(neigh->hnext,
1425 rcu_dereference_protected(htbl->buckets[hash_val],
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001426 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001427 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1428 atomic_inc(&ntbl->entries);
1429
1430out_unlock:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001431
1432 return neigh;
1433}
1434
1435void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1436{
1437 /* neigh reference count was dropprd to zero */
1438 struct net_device *dev = neigh->dev;
1439 struct ipoib_dev_priv *priv = netdev_priv(dev);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001440 struct sk_buff *skb;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001441 if (neigh->ah)
1442 ipoib_put_ah(neigh->ah);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001443 while ((skb = __skb_dequeue(&neigh->queue))) {
Roland Dreierde903512007-09-28 15:33:51 -07001444 ++dev->stats.tx_dropped;
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001445 dev_kfree_skb_any(skb);
1446 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001447 if (ipoib_cm_get(neigh))
1448 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001449 ipoib_dbg(netdev_priv(dev),
1450 "neigh free for %06x %pI6\n",
1451 IPOIB_QPN(neigh->daddr),
1452 neigh->daddr + 4);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001453 kfree(neigh);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001454 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1455 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1456 complete(&priv->ntbl.flushed);
1457 }
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001458}
1459
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001460static void ipoib_neigh_reclaim(struct rcu_head *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001461{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001462 /* Called as a result of removal from hash table */
1463 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1464 /* note TX context may hold another ref */
1465 ipoib_neigh_put(neigh);
1466}
1467
1468void ipoib_neigh_free(struct ipoib_neigh *neigh)
1469{
1470 struct net_device *dev = neigh->dev;
1471 struct ipoib_dev_priv *priv = netdev_priv(dev);
1472 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1473 struct ipoib_neigh_hash *htbl;
1474 struct ipoib_neigh __rcu **np;
1475 struct ipoib_neigh *n;
1476 u32 hash_val;
1477
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001478 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001479 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001480 if (!htbl)
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001481 return;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001482
1483 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1484 np = &htbl->buckets[hash_val];
1485 for (n = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001486 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001487 n != NULL;
Shlomo Pongratz6c723a62012-08-13 14:39:50 +00001488 n = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001489 lockdep_is_held(&priv->lock))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001490 if (n == neigh) {
1491 /* found */
1492 rcu_assign_pointer(*np,
1493 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001494 lockdep_is_held(&priv->lock)));
Jim Foraker49b8e742013-08-08 17:44:22 -07001495 /* remove from parent list */
Feras Daoudb2e7d1f2016-12-28 14:47:27 +02001496 list_del_init(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001497 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001498 return;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001499 } else {
1500 np = &n->hnext;
1501 }
1502 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001503}
1504
1505static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1506{
1507 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1508 struct ipoib_neigh_hash *htbl;
Bart Van Assche52374962015-05-26 15:03:48 +02001509 struct ipoib_neigh __rcu **buckets;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001510 u32 size;
1511
1512 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1513 ntbl->htbl = NULL;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001514 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1515 if (!htbl)
1516 return -ENOMEM;
1517 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1518 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1519 buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1520 if (!buckets) {
1521 kfree(htbl);
1522 return -ENOMEM;
1523 }
1524 htbl->size = size;
1525 htbl->mask = (size - 1);
1526 htbl->buckets = buckets;
Bart Van Assche52374962015-05-26 15:03:48 +02001527 RCU_INIT_POINTER(ntbl->htbl, htbl);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001528 htbl->ntbl = ntbl;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001529 atomic_set(&ntbl->entries, 0);
1530
1531 /* start garbage collection */
1532 clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
Doug Ledford0b395782015-02-21 19:27:03 -05001533 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001534 arp_tbl.gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535
1536 return 0;
1537}
1538
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001539static void neigh_hash_free_rcu(struct rcu_head *head)
1540{
1541 struct ipoib_neigh_hash *htbl = container_of(head,
1542 struct ipoib_neigh_hash,
1543 rcu);
1544 struct ipoib_neigh __rcu **buckets = htbl->buckets;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001545 struct ipoib_neigh_table *ntbl = htbl->ntbl;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001546
1547 kfree(buckets);
1548 kfree(htbl);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001549 complete(&ntbl->deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001550}
1551
1552void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1553{
1554 struct ipoib_dev_priv *priv = netdev_priv(dev);
1555 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1556 struct ipoib_neigh_hash *htbl;
1557 unsigned long flags;
1558 int i;
1559
1560 /* remove all neigh connected to a given path or mcast */
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001561 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001562
1563 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001564 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001565
1566 if (!htbl)
1567 goto out_unlock;
1568
1569 for (i = 0; i < htbl->size; i++) {
1570 struct ipoib_neigh *neigh;
1571 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1572
1573 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001574 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001575 /* delete neighs belong to this parent */
1576 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1577 rcu_assign_pointer(*np,
1578 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001579 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001580 /* remove from parent list */
Feras Daoudb2e7d1f2016-12-28 14:47:27 +02001581 list_del_init(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001582 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1583 } else {
1584 np = &neigh->hnext;
1585 }
1586
1587 }
1588 }
1589out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001590 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001591}
1592
1593static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1594{
1595 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1596 struct ipoib_neigh_hash *htbl;
1597 unsigned long flags;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001598 int i, wait_flushed = 0;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001599
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001600 init_completion(&priv->ntbl.flushed);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001601
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001602 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001603
1604 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001605 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001606 if (!htbl)
1607 goto out_unlock;
1608
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001609 wait_flushed = atomic_read(&priv->ntbl.entries);
1610 if (!wait_flushed)
1611 goto free_htbl;
1612
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001613 for (i = 0; i < htbl->size; i++) {
1614 struct ipoib_neigh *neigh;
1615 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1616
1617 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001618 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001619 rcu_assign_pointer(*np,
1620 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001621 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001622 /* remove from path/mc list */
Feras Daoudb2e7d1f2016-12-28 14:47:27 +02001623 list_del_init(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001624 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1625 }
1626 }
1627
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001628free_htbl:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001629 rcu_assign_pointer(ntbl->htbl, NULL);
1630 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1631
1632out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001633 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001634 if (wait_flushed)
1635 wait_for_completion(&priv->ntbl.flushed);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001636}
1637
1638static void ipoib_neigh_hash_uninit(struct net_device *dev)
1639{
1640 struct ipoib_dev_priv *priv = netdev_priv(dev);
1641 int stopped;
1642
1643 ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001644 init_completion(&priv->ntbl.deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001645 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1646
1647 /* Stop GC if called at init fail need to cancel work */
1648 stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1649 if (!stopped)
1650 cancel_delayed_work(&priv->neigh_reap_task);
1651
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001652 ipoib_flush_neighs(priv);
1653
1654 wait_for_completion(&priv->ntbl.deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001655}
1656
1657
Linus Torvalds1da177e2005-04-16 15:20:36 -07001658int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1659{
1660 struct ipoib_dev_priv *priv = netdev_priv(dev);
1661
1662 /* Allocate RX/TX "rings" to hold queued skbs */
Shirley Ma0f485252006-04-10 09:43:58 -07001663 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 GFP_KERNEL);
1665 if (!priv->rx_ring) {
1666 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
Shirley Ma0f485252006-04-10 09:43:58 -07001667 ca->name, ipoib_recvq_size);
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001668 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670
Joe Perches948579c2010-11-05 03:07:36 +00001671 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 if (!priv->tx_ring) {
1673 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
Shirley Ma0f485252006-04-10 09:43:58 -07001674 ca->name, ipoib_sendq_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 goto out_rx_ring_cleanup;
1676 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001678 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679
1680 if (ipoib_ib_dev_init(dev, ca, port))
1681 goto out_tx_ring_cleanup;
1682
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001683 /*
1684 * Must be after ipoib_ib_dev_init so we can allocate a per
1685 * device wq there and use it here
1686 */
1687 if (ipoib_neigh_hash_init(priv) < 0)
1688 goto out_dev_uninit;
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 return 0;
1691
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001692out_dev_uninit:
1693 ipoib_ib_dev_cleanup(dev);
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695out_tx_ring_cleanup:
Roland Dreier10313cb2008-03-12 07:51:03 -07001696 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001697
1698out_rx_ring_cleanup:
1699 kfree(priv->rx_ring);
1700
1701out:
1702 return -ENOMEM;
1703}
1704
1705void ipoib_dev_cleanup(struct net_device *dev)
1706{
1707 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001708 LIST_HEAD(head);
1709
1710 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 /* Delete any child interfaces first */
1713 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001714 /* Stop GC on child */
1715 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1716 cancel_delayed_work(&cpriv->neigh_reap_task);
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001717 unregister_netdevice_queue(cpriv->dev, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 }
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001719 unregister_netdevice_many(&head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001721 /*
1722 * Must be before ipoib_ib_dev_cleanup or we delete an in use
1723 * work queue
1724 */
1725 ipoib_neigh_hash_uninit(dev);
1726
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727 ipoib_ib_dev_cleanup(dev);
1728
Hal Rosenstock92a6b342005-08-13 20:50:27 -07001729 kfree(priv->rx_ring);
Roland Dreier10313cb2008-03-12 07:51:03 -07001730 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
Hal Rosenstock92a6b342005-08-13 20:50:27 -07001732 priv->rx_ring = NULL;
1733 priv->tx_ring = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734}
1735
Eli Cohen9c3c5f82016-03-11 22:58:39 +02001736static int ipoib_set_vf_link_state(struct net_device *dev, int vf, int link_state)
1737{
1738 struct ipoib_dev_priv *priv = netdev_priv(dev);
1739
1740 return ib_set_vf_link_state(priv->ca, vf, priv->port, link_state);
1741}
1742
1743static int ipoib_get_vf_config(struct net_device *dev, int vf,
1744 struct ifla_vf_info *ivf)
1745{
1746 struct ipoib_dev_priv *priv = netdev_priv(dev);
1747 int err;
1748
1749 err = ib_get_vf_config(priv->ca, vf, priv->port, ivf);
1750 if (err)
1751 return err;
1752
1753 ivf->vf = vf;
1754
1755 return 0;
1756}
1757
1758static int ipoib_set_vf_guid(struct net_device *dev, int vf, u64 guid, int type)
1759{
1760 struct ipoib_dev_priv *priv = netdev_priv(dev);
1761
1762 if (type != IFLA_VF_IB_NODE_GUID && type != IFLA_VF_IB_PORT_GUID)
1763 return -EINVAL;
1764
1765 return ib_set_vf_guid(priv->ca, vf, priv->port, guid, type);
1766}
1767
1768static int ipoib_get_vf_stats(struct net_device *dev, int vf,
1769 struct ifla_vf_stats *vf_stats)
1770{
1771 struct ipoib_dev_priv *priv = netdev_priv(dev);
1772
1773 return ib_get_vf_stats(priv->ca, vf, priv->port, vf_stats);
1774}
1775
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001776static const struct header_ops ipoib_header_ops = {
1777 .create = ipoib_hard_header,
1778};
1779
Eli Cohen9c3c5f82016-03-11 22:58:39 +02001780static const struct net_device_ops ipoib_netdev_ops_pf = {
1781 .ndo_uninit = ipoib_uninit,
1782 .ndo_open = ipoib_open,
1783 .ndo_stop = ipoib_stop,
1784 .ndo_change_mtu = ipoib_change_mtu,
1785 .ndo_fix_features = ipoib_fix_features,
1786 .ndo_start_xmit = ipoib_start_xmit,
1787 .ndo_tx_timeout = ipoib_timeout,
1788 .ndo_set_rx_mode = ipoib_set_mcast_list,
1789 .ndo_get_iflink = ipoib_get_iflink,
1790 .ndo_set_vf_link_state = ipoib_set_vf_link_state,
1791 .ndo_get_vf_config = ipoib_get_vf_config,
1792 .ndo_get_vf_stats = ipoib_get_vf_stats,
1793 .ndo_set_vf_guid = ipoib_set_vf_guid,
Mark Bloch492a7e62016-05-18 16:42:43 +03001794 .ndo_set_mac_address = ipoib_set_mac,
Eli Cohen9c3c5f82016-03-11 22:58:39 +02001795};
1796
1797static const struct net_device_ops ipoib_netdev_ops_vf = {
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001798 .ndo_uninit = ipoib_uninit,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001799 .ndo_open = ipoib_open,
1800 .ndo_stop = ipoib_stop,
1801 .ndo_change_mtu = ipoib_change_mtu,
Michał Mirosław3d96c742011-04-19 00:43:20 +00001802 .ndo_fix_features = ipoib_fix_features,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001803 .ndo_start_xmit = ipoib_start_xmit,
1804 .ndo_tx_timeout = ipoib_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001805 .ndo_set_rx_mode = ipoib_set_mcast_list,
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +02001806 .ndo_get_iflink = ipoib_get_iflink,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001807};
1808
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001809void ipoib_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810{
1811 struct ipoib_dev_priv *priv = netdev_priv(dev);
1812
Eli Cohen9c3c5f82016-03-11 22:58:39 +02001813 if (priv->hca_caps & IB_DEVICE_VIRTUAL_FUNCTION)
1814 dev->netdev_ops = &ipoib_netdev_ops_vf;
1815 else
1816 dev->netdev_ops = &ipoib_netdev_ops_pf;
1817
Roland Dreier2337f802007-10-23 19:57:54 -07001818 dev->header_ops = &ipoib_header_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001819
Eli Cohen82c24c12008-04-16 21:09:32 -07001820 ipoib_set_ethtool_ops(dev);
1821
Michal Schmidt7f1a3862013-08-21 18:50:22 +02001822 netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001823
Roland Dreier2337f802007-10-23 19:57:54 -07001824 dev->watchdog_timeo = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Roland Dreier2337f802007-10-23 19:57:54 -07001826 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001827
Paolo Abenifc791b62016-10-13 18:26:56 +02001828 dev->hard_header_len = IPOIB_HARD_LEN;
Roland Dreier2337f802007-10-23 19:57:54 -07001829 dev->addr_len = INFINIBAND_ALEN;
1830 dev->type = ARPHRD_INFINIBAND;
1831 dev->tx_queue_len = ipoib_sendq_size * 2;
Eli Coheneb14032f2008-01-30 18:30:46 +02001832 dev->features = (NETIF_F_VLAN_CHALLENGED |
Eli Coheneb14032f2008-01-30 18:30:46 +02001833 NETIF_F_HIGHDMA);
Eric Dumazet02875872014-10-05 18:38:35 -07001834 netif_keep_dst(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
Linus Torvalds1da177e2005-04-16 15:20:36 -07001836 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
1837
Linus Torvalds1da177e2005-04-16 15:20:36 -07001838 priv->dev = dev;
1839
1840 spin_lock_init(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841
Erez Shitritf47944c2013-10-16 17:37:49 +03001842 init_rwsem(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843
1844 INIT_LIST_HEAD(&priv->path_list);
1845 INIT_LIST_HEAD(&priv->child_intfs);
1846 INIT_LIST_HEAD(&priv->dead_ahs);
1847 INIT_LIST_HEAD(&priv->multicast_list);
1848
David Howellsc4028952006-11-22 14:57:56 +00001849 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
Yossi Etigine8224e42008-09-16 11:57:45 -07001850 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
Moni Shouaee1e2c82008-07-14 23:48:49 -07001851 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
1852 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
1853 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
David Howellsc4028952006-11-22 14:57:56 +00001854 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1855 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001856 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001857}
1858
1859struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
1860{
1861 struct net_device *dev;
1862
Tom Gundersenc835a672014-07-14 16:37:24 +02001863 dev = alloc_netdev((int)sizeof(struct ipoib_dev_priv), name,
1864 NET_NAME_UNKNOWN, ipoib_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 if (!dev)
1866 return NULL;
1867
1868 return netdev_priv(dev);
1869}
1870
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001871static ssize_t show_pkey(struct device *dev,
1872 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001874 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001875
1876 return sprintf(buf, "0x%04x\n", priv->pkey);
1877}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001878static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001880static ssize_t show_umcast(struct device *dev,
1881 struct device_attribute *attr, char *buf)
1882{
1883 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1884
1885 return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1886}
1887
Or Gerlitz862096a2012-09-27 12:06:02 +00001888void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001889{
Or Gerlitz862096a2012-09-27 12:06:02 +00001890 struct ipoib_dev_priv *priv = netdev_priv(ndev);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001891
1892 if (umcast_val > 0) {
1893 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1894 ipoib_warn(priv, "ignoring multicast groups joined directly "
1895 "by userspace\n");
1896 } else
1897 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
Or Gerlitz862096a2012-09-27 12:06:02 +00001898}
1899
1900static ssize_t set_umcast(struct device *dev,
1901 struct device_attribute *attr,
1902 const char *buf, size_t count)
1903{
1904 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1905
1906 ipoib_set_umcast(to_net_dev(dev), umcast_val);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001907
1908 return count;
1909}
1910static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1911
1912int ipoib_add_umcast_attr(struct net_device *dev)
1913{
1914 return device_create_file(&dev->dev, &dev_attr_umcast);
1915}
1916
Mark Bloch492a7e62016-05-18 16:42:43 +03001917static void set_base_guid(struct ipoib_dev_priv *priv, union ib_gid *gid)
1918{
1919 struct ipoib_dev_priv *child_priv;
1920 struct net_device *netdev = priv->dev;
1921
Mark Bloch9b299532016-06-04 15:15:22 +03001922 netif_addr_lock_bh(netdev);
Mark Bloch492a7e62016-05-18 16:42:43 +03001923
1924 memcpy(&priv->local_gid.global.interface_id,
1925 &gid->global.interface_id,
1926 sizeof(gid->global.interface_id));
1927 memcpy(netdev->dev_addr + 4, &priv->local_gid, sizeof(priv->local_gid));
1928 clear_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
1929
Mark Bloch9b299532016-06-04 15:15:22 +03001930 netif_addr_unlock_bh(netdev);
Mark Bloch492a7e62016-05-18 16:42:43 +03001931
1932 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
1933 down_read(&priv->vlan_rwsem);
1934 list_for_each_entry(child_priv, &priv->child_intfs, list)
1935 set_base_guid(child_priv, gid);
1936 up_read(&priv->vlan_rwsem);
1937 }
1938}
1939
1940static int ipoib_check_lladdr(struct net_device *dev,
1941 struct sockaddr_storage *ss)
1942{
1943 union ib_gid *gid = (union ib_gid *)(ss->__data + 4);
1944 int ret = 0;
1945
Mark Bloch9b299532016-06-04 15:15:22 +03001946 netif_addr_lock_bh(dev);
Mark Bloch492a7e62016-05-18 16:42:43 +03001947
1948 /* Make sure the QPN, reserved and subnet prefix match the current
1949 * lladdr, it also makes sure the lladdr is unicast.
1950 */
1951 if (memcmp(dev->dev_addr, ss->__data,
1952 4 + sizeof(gid->global.subnet_prefix)) ||
1953 gid->global.interface_id == 0)
1954 ret = -EINVAL;
1955
Mark Bloch9b299532016-06-04 15:15:22 +03001956 netif_addr_unlock_bh(dev);
Mark Bloch492a7e62016-05-18 16:42:43 +03001957
1958 return ret;
1959}
1960
1961static int ipoib_set_mac(struct net_device *dev, void *addr)
1962{
1963 struct ipoib_dev_priv *priv = netdev_priv(dev);
1964 struct sockaddr_storage *ss = addr;
1965 int ret;
1966
1967 if (!(dev->priv_flags & IFF_LIVE_ADDR_CHANGE) && netif_running(dev))
1968 return -EBUSY;
1969
1970 ret = ipoib_check_lladdr(dev, ss);
1971 if (ret)
1972 return ret;
1973
1974 set_base_guid(priv, (union ib_gid *)(ss->__data + 4));
1975
1976 queue_work(ipoib_workqueue, &priv->flush_light);
1977
1978 return 0;
1979}
1980
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001981static ssize_t create_child(struct device *dev,
1982 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 const char *buf, size_t count)
1984{
1985 int pkey;
1986 int ret;
1987
1988 if (sscanf(buf, "%i", &pkey) != 1)
1989 return -EINVAL;
1990
Or Gerlitz3d790a42013-07-18 14:02:31 +03001991 if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992 return -EINVAL;
1993
Roland Dreier4ce05932005-08-19 12:03:17 -07001994 /*
1995 * Set the full membership bit, so that we join the right
1996 * broadcast group, etc.
1997 */
1998 pkey |= 0x8000;
1999
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002000 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 return ret ? ret : count;
2003}
Or Gerlitz7a52b342010-06-06 04:59:16 +00002004static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002005
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002006static ssize_t delete_child(struct device *dev,
2007 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 const char *buf, size_t count)
2009{
2010 int pkey;
2011 int ret;
2012
2013 if (sscanf(buf, "%i", &pkey) != 1)
2014 return -EINVAL;
2015
2016 if (pkey < 0 || pkey > 0xffff)
2017 return -EINVAL;
2018
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002019 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020
2021 return ret ? ret : count;
2022
2023}
Or Gerlitz7a52b342010-06-06 04:59:16 +00002024static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025
2026int ipoib_add_pkey_attr(struct net_device *dev)
2027{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002028 return device_create_file(&dev->dev, &dev_attr_pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029}
2030
Or Gerlitz83bb63f2008-10-22 15:49:49 -07002031int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
2032{
Or Gerlitz4a061b22015-12-18 10:59:46 +02002033 priv->hca_caps = hca->attrs.device_cap_flags;
Or Gerlitz83bb63f2008-10-22 15:49:49 -07002034
2035 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
Yuval Shaia5faba542016-07-20 01:30:06 -07002036 priv->dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
Michał Mirosław3d96c742011-04-19 00:43:20 +00002037
2038 if (priv->hca_caps & IB_DEVICE_UD_TSO)
2039 priv->dev->hw_features |= NETIF_F_TSO;
2040
2041 priv->dev->features |= priv->dev->hw_features;
Or Gerlitz83bb63f2008-10-22 15:49:49 -07002042 }
2043
Or Gerlitz83bb63f2008-10-22 15:49:49 -07002044 return 0;
2045}
2046
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047static struct net_device *ipoib_add_port(const char *format,
2048 struct ib_device *hca, u8 port)
2049{
2050 struct ipoib_dev_priv *priv;
Shirley Mabc7b3a32008-04-23 11:55:45 -07002051 struct ib_port_attr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 int result = -ENOMEM;
2053
2054 priv = ipoib_intf_alloc(format);
2055 if (!priv)
2056 goto alloc_mem_failed;
2057
2058 SET_NETDEV_DEV(priv->dev, hca->dma_device);
Eli Cohenc3aa9b12010-09-20 07:05:06 +00002059 priv->dev->dev_id = port - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060
Amir Vadai58e9cc92015-07-01 14:31:01 +03002061 result = ib_query_port(hca, port, &attr);
2062 if (!result)
Shirley Mabc7b3a32008-04-23 11:55:45 -07002063 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
2064 else {
2065 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
2066 hca->name, port);
2067 goto device_init_failed;
2068 }
2069
2070 /* MTU will be reset when mcast join happens */
2071 priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
2072 priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
2073
David Miller596b9b62011-07-25 00:01:25 +00002074 priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 result = ib_query_pkey(hca, port, 0, &priv->pkey);
2077 if (result) {
2078 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
2079 hca->name, port, result);
Eli Cohenca6de172007-08-21 18:46:10 +03002080 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 }
2082
Amir Vadai58e9cc92015-07-01 14:31:01 +03002083 result = ipoib_set_dev_features(priv, hca);
2084 if (result)
Eli Cohen60461362008-04-16 21:01:10 -07002085 goto device_init_failed;
Vladimir Sokolovskyaf40da82008-07-14 23:48:48 -07002086
Roland Dreier4ce05932005-08-19 12:03:17 -07002087 /*
2088 * Set the full membership bit, so that we join the right
2089 * broadcast group, etc.
2090 */
2091 priv->pkey |= 0x8000;
2092
Linus Torvalds1da177e2005-04-16 15:20:36 -07002093 priv->dev->broadcast[8] = priv->pkey >> 8;
2094 priv->dev->broadcast[9] = priv->pkey & 0xff;
2095
Matan Barak55ee3ab2015-10-15 18:38:45 +03002096 result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002097 if (result) {
2098 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
2099 hca->name, port, result);
Eli Cohenca6de172007-08-21 18:46:10 +03002100 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 } else
2102 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
Mark Bloch492a7e62016-05-18 16:42:43 +03002103 set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105 result = ipoib_dev_init(priv->dev, hca, port);
2106 if (result < 0) {
2107 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
2108 hca->name, port, result);
2109 goto device_init_failed;
2110 }
2111
2112 INIT_IB_EVENT_HANDLER(&priv->event_handler,
2113 priv->ca, ipoib_event);
2114 result = ib_register_event_handler(&priv->event_handler);
2115 if (result < 0) {
2116 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
2117 "port %d (ret = %d)\n",
2118 hca->name, port, result);
2119 goto event_failed;
2120 }
2121
Alex Estrine405d2e2018-02-01 10:55:41 -08002122 /* call event handler to ensure pkey in sync */
2123 queue_work(ipoib_workqueue, &priv->flush_heavy);
2124
Linus Torvalds1da177e2005-04-16 15:20:36 -07002125 result = register_netdev(priv->dev);
2126 if (result) {
2127 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
2128 hca->name, port, result);
2129 goto register_failed;
2130 }
2131
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02002132 if (ipoib_cm_add_mode_attr(priv->dev))
2133 goto sysfs_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134 if (ipoib_add_pkey_attr(priv->dev))
2135 goto sysfs_failed;
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02002136 if (ipoib_add_umcast_attr(priv->dev))
2137 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002138 if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002139 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002140 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141 goto sysfs_failed;
2142
2143 return priv->dev;
2144
2145sysfs_failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146 unregister_netdev(priv->dev);
2147
2148register_failed:
2149 ib_unregister_event_handler(&priv->event_handler);
Doug Ledford0b395782015-02-21 19:27:03 -05002150 flush_workqueue(ipoib_workqueue);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00002151 /* Stop GC if started before flush */
2152 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
2153 cancel_delayed_work(&priv->neigh_reap_task);
Doug Ledford0b395782015-02-21 19:27:03 -05002154 flush_workqueue(priv->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002155
2156event_failed:
2157 ipoib_dev_cleanup(priv->dev);
2158
2159device_init_failed:
2160 free_netdev(priv->dev);
2161
2162alloc_mem_failed:
2163 return ERR_PTR(result);
2164}
2165
2166static void ipoib_add_one(struct ib_device *device)
2167{
2168 struct list_head *dev_list;
2169 struct net_device *dev;
2170 struct ipoib_dev_priv *priv;
Hal Rosenstock41390322015-06-29 09:57:00 -04002171 int p;
Michael Wang8e37ab62015-05-05 14:50:24 +02002172 int count = 0;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
2175 if (!dev_list)
2176 return;
2177
2178 INIT_LIST_HEAD(dev_list);
2179
Hal Rosenstock41390322015-06-29 09:57:00 -04002180 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
Michael Wang8e37ab62015-05-05 14:50:24 +02002181 if (!rdma_protocol_ib(device, p))
Eli Cohen7b4c8762010-09-27 17:51:11 -07002182 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 dev = ipoib_add_port("ib%d", device, p);
2184 if (!IS_ERR(dev)) {
2185 priv = netdev_priv(dev);
2186 list_add_tail(&priv->list, dev_list);
Michael Wang8e37ab62015-05-05 14:50:24 +02002187 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 }
2189 }
2190
Michael Wang8e37ab62015-05-05 14:50:24 +02002191 if (!count) {
2192 kfree(dev_list);
2193 return;
2194 }
2195
Linus Torvalds1da177e2005-04-16 15:20:36 -07002196 ib_set_client_data(device, &ipoib_client, dev_list);
2197}
2198
Haggai Eran7c1eb452015-07-30 17:50:14 +03002199static void ipoib_remove_one(struct ib_device *device, void *client_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002200{
2201 struct ipoib_dev_priv *priv, *tmp;
Haggai Eran7c1eb452015-07-30 17:50:14 +03002202 struct list_head *dev_list = client_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203
Itai Garbi5a2815f2013-02-19 15:40:24 +00002204 if (!dev_list)
2205 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206
2207 list_for_each_entry_safe(priv, tmp, dev_list, list) {
2208 ib_unregister_event_handler(&priv->event_handler);
Doug Ledford0b395782015-02-21 19:27:03 -05002209 flush_workqueue(ipoib_workqueue);
Roland Dreiera77a57a2008-08-19 15:01:32 -07002210
Erez Shitrit198b12f2016-06-04 15:15:20 +03002211 /* mark interface in the middle of destruction */
2212 set_bit(IPOIB_FLAG_GOING_DOWN, &priv->flags);
2213
Roland Dreiera77a57a2008-08-19 15:01:32 -07002214 rtnl_lock();
2215 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
2216 rtnl_unlock();
2217
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00002218 /* Stop GC */
2219 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
2220 cancel_delayed_work(&priv->neigh_reap_task);
Doug Ledford0b395782015-02-21 19:27:03 -05002221 flush_workqueue(priv->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222
2223 unregister_netdev(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 free_netdev(priv->dev);
2225 }
Michael S. Tsirkin06c56e42005-09-01 09:19:02 -07002226
2227 kfree(dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002228}
2229
Shamir Rabinovitchd20bfe22017-03-29 06:21:59 -04002230#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2231static struct notifier_block ipoib_netdev_notifier = {
2232 .notifier_call = ipoib_netdev_event,
2233};
2234#endif
2235
Linus Torvalds1da177e2005-04-16 15:20:36 -07002236static int __init ipoib_init_module(void)
2237{
2238 int ret;
2239
Shirley Ma0f485252006-04-10 09:43:58 -07002240 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
2241 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
2242 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
2243
2244 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
2245 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -07002246 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08002247#ifdef CONFIG_INFINIBAND_IPOIB_CM
2248 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
2249#endif
Shirley Ma0f485252006-04-10 09:43:58 -07002250
Eli Cohenf89271da2008-07-14 23:48:44 -07002251 /*
2252 * When copying small received packets, we only copy from the
2253 * linear data part of the SKB, so we rely on this condition.
2254 */
2255 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
2256
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257 ret = ipoib_register_debugfs();
2258 if (ret)
2259 return ret;
2260
2261 /*
Doug Ledford0b395782015-02-21 19:27:03 -05002262 * We create a global workqueue here that is used for all flush
2263 * operations. However, if you attempt to flush a workqueue
2264 * from a task on that same workqueue, it deadlocks the system.
2265 * We want to be able to flush the tasks associated with a
2266 * specific net device, so we also create a workqueue for each
2267 * netdevice. We queue up the tasks for that device only on
2268 * its private workqueue, and we only queue up flush events
2269 * on our global flush workqueue. This avoids the deadlocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270 */
Bhaktipriya Shridhar855cda62016-08-15 23:44:26 +05302271 ipoib_workqueue = alloc_ordered_workqueue("ipoib_flush",
2272 WQ_MEM_RECLAIM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273 if (!ipoib_workqueue) {
2274 ret = -ENOMEM;
2275 goto err_fs;
2276 }
2277
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002278 ib_sa_register_client(&ipoib_sa_client);
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280 ret = ib_register_client(&ipoib_client);
2281 if (ret)
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002282 goto err_sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283
Or Gerlitz9baa0b02012-09-13 05:56:36 +00002284 ret = ipoib_netlink_init();
2285 if (ret)
2286 goto err_client;
2287
Shamir Rabinovitchd20bfe22017-03-29 06:21:59 -04002288#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2289 register_netdevice_notifier(&ipoib_netdev_notifier);
2290#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 return 0;
2292
Or Gerlitz9baa0b02012-09-13 05:56:36 +00002293err_client:
2294 ib_unregister_client(&ipoib_client);
2295
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002296err_sa:
2297 ib_sa_unregister_client(&ipoib_sa_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002298 destroy_workqueue(ipoib_workqueue);
2299
Roland Dreier9adec1a2005-04-16 15:26:07 -07002300err_fs:
2301 ipoib_unregister_debugfs();
2302
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 return ret;
2304}
2305
2306static void __exit ipoib_cleanup_module(void)
2307{
Shamir Rabinovitchd20bfe22017-03-29 06:21:59 -04002308#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2309 unregister_netdevice_notifier(&ipoib_netdev_notifier);
2310#endif
Or Gerlitz9baa0b02012-09-13 05:56:36 +00002311 ipoib_netlink_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002312 ib_unregister_client(&ipoib_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002313 ib_sa_unregister_client(&ipoib_sa_client);
Roland Dreier9adec1a2005-04-16 15:26:07 -07002314 ipoib_unregister_debugfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002315 destroy_workqueue(ipoib_workqueue);
2316}
2317
2318module_init(ipoib_init_module);
2319module_exit(ipoib_cleanup_module);