blob: 5f143445daa979ef60a3d71a42997403e2c8c39b [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");
63
Shirley Ma0f485252006-04-10 09:43:58 -070064int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
65int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
66
67module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
68MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
69module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
70MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
73int ipoib_debug_level;
74
75module_param_named(debug_level, ipoib_debug_level, int, 0644);
76MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
77#endif
78
Roland Dreier1732b0e2005-11-07 10:33:11 -080079struct ipoib_path_iter {
80 struct net_device *dev;
81 struct ipoib_path path;
82};
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084static const u8 ipv4_bcast_addr[] = {
85 0x00, 0xff, 0xff, 0xff,
86 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
87 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
88};
89
90struct workqueue_struct *ipoib_workqueue;
91
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070092struct ib_sa_client ipoib_sa_client;
93
Linus Torvalds1da177e2005-04-16 15:20:36 -070094static void ipoib_add_one(struct ib_device *device);
Haggai Eran7c1eb452015-07-30 17:50:14 +030095static void ipoib_remove_one(struct ib_device *device, void *client_data);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +000096static void ipoib_neigh_reclaim(struct rcu_head *rp);
Guy Shapiroddde8962015-07-30 17:50:16 +030097static struct net_device *ipoib_get_net_dev_by_params(
98 struct ib_device *dev, u8 port, u16 pkey,
99 const union ib_gid *gid, const struct sockaddr *addr,
100 void *client_data);
Mark Bloch492a7e62016-05-18 16:42:43 +0300101static int ipoib_set_mac(struct net_device *dev, void *addr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103static struct ib_client ipoib_client = {
104 .name = "ipoib",
105 .add = ipoib_add_one,
Guy Shapiroddde8962015-07-30 17:50:16 +0300106 .remove = ipoib_remove_one,
107 .get_net_dev_by_params = ipoib_get_net_dev_by_params,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108};
109
Shamir Rabinovitch771a5252017-03-29 06:21:59 -0400110#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
111static int ipoib_netdev_event(struct notifier_block *this,
112 unsigned long event, void *ptr)
113{
114 struct netdev_notifier_info *ni = ptr;
115 struct net_device *dev = ni->dev;
116
117 if (dev->netdev_ops->ndo_open != ipoib_open)
118 return NOTIFY_DONE;
119
120 switch (event) {
121 case NETDEV_REGISTER:
122 ipoib_create_debug_files(dev);
123 break;
124 case NETDEV_CHANGENAME:
125 ipoib_delete_debug_files(dev);
126 ipoib_create_debug_files(dev);
127 break;
128 case NETDEV_UNREGISTER:
129 ipoib_delete_debug_files(dev);
130 break;
131 }
132
133 return NOTIFY_DONE;
134}
135#endif
136
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137int ipoib_open(struct net_device *dev)
138{
Erez Shitritc1048af2017-04-10 11:22:29 +0300139 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140
141 ipoib_dbg(priv, "bringing up interface\n");
142
Michal Schmidt437708c2014-01-17 19:47:25 +0100143 netif_carrier_off(dev);
144
Yossi Etigine028cc52009-04-20 13:58:08 -0700145 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Erez Shitrit3b561132016-05-25 22:02:07 +0300147 priv->sm_fullmember_sendonly_support = false;
148
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500149 if (ipoib_ib_dev_open(dev)) {
Alex Estrindd57c932014-08-06 14:40:32 -0400150 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
151 return 0;
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800152 goto err_disable;
Alex Estrindd57c932014-08-06 14:40:32 -0400153 }
Yossi Etiginfe25c562008-11-12 10:24:36 -0800154
Zhu Yanjun5c370772017-01-18 23:16:06 -0500155 ipoib_ib_dev_up(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156
157 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
158 struct ipoib_dev_priv *cpriv;
159
160 /* Bring up any child interfaces too */
Erez Shitritf47944c2013-10-16 17:37:49 +0300161 down_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 list_for_each_entry(cpriv, &priv->child_intfs, list) {
163 int flags;
164
165 flags = cpriv->dev->flags;
166 if (flags & IFF_UP)
167 continue;
168
169 dev_change_flags(cpriv->dev, flags | IFF_UP);
170 }
Erez Shitritf47944c2013-10-16 17:37:49 +0300171 up_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173
174 netif_start_queue(dev);
175
176 return 0;
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800177
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800178err_disable:
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800179 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
180
181 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182}
183
184static int ipoib_stop(struct net_device *dev)
185{
Erez Shitritc1048af2017-04-10 11:22:29 +0300186 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
188 ipoib_dbg(priv, "stopping interface\n");
189
190 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
191
192 netif_stop_queue(dev);
193
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500194 ipoib_ib_dev_down(dev);
Erez Shitritcd565b42017-04-10 11:22:30 +0300195 ipoib_ib_dev_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196
197 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
198 struct ipoib_dev_priv *cpriv;
199
200 /* Bring down any child interfaces too */
Erez Shitritf47944c2013-10-16 17:37:49 +0300201 down_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 list_for_each_entry(cpriv, &priv->child_intfs, list) {
203 int flags;
204
205 flags = cpriv->dev->flags;
206 if (!(flags & IFF_UP))
207 continue;
208
209 dev_change_flags(cpriv->dev, flags & ~IFF_UP);
210 }
Erez Shitritf47944c2013-10-16 17:37:49 +0300211 up_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213
214 return 0;
215}
216
Or Gerlitz9baa0b02012-09-13 05:56:36 +0000217static void ipoib_uninit(struct net_device *dev)
218{
219 ipoib_dev_cleanup(dev);
220}
221
David S. Miller9ca36f72011-11-16 18:05:50 -0500222static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
Michał Mirosław3d96c742011-04-19 00:43:20 +0000223{
Erez Shitritc1048af2017-04-10 11:22:29 +0300224 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Michał Mirosław3d96c742011-04-19 00:43:20 +0000225
226 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
Yuval Shaiac4268772015-07-12 01:24:09 -0700227 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
Michał Mirosław3d96c742011-04-19 00:43:20 +0000228
229 return features;
230}
231
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
233{
Erez Shitritc1048af2017-04-10 11:22:29 +0300234 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Erez Shitrited7b5212017-05-23 11:42:52 +0300235 int ret = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200237 /* dev->mtu > 2K ==> connected mode */
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800238 if (ipoib_cm_admin_enabled(dev)) {
239 if (new_mtu > ipoib_cm_max_mtu(dev))
240 return -EINVAL;
241
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200242 if (new_mtu > priv->mcast_mtu)
243 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
244 priv->mcast_mtu);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800245
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200246 dev->mtu = new_mtu;
247 return 0;
248 }
249
Shirley Mabc7b3a32008-04-23 11:55:45 -0700250 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251 return -EINVAL;
252
253 priv->admin_mtu = new_mtu;
254
Feras Daoud29da6862016-12-28 14:47:20 +0200255 if (priv->mcast_mtu < priv->admin_mtu)
256 ipoib_dbg(priv, "MTU must be smaller than the underlying "
257 "link layer MTU - 4 (%u)\n", priv->mcast_mtu);
258
Erez Shitrited7b5212017-05-23 11:42:52 +0300259 new_mtu = min(priv->mcast_mtu, priv->admin_mtu);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
Erez Shitrited7b5212017-05-23 11:42:52 +0300261 if (priv->rn_ops->ndo_change_mtu) {
262 bool carrier_status = netif_carrier_ok(dev);
263
264 netif_carrier_off(dev);
265
266 /* notify lower level on the real mtu */
267 ret = priv->rn_ops->ndo_change_mtu(dev, new_mtu);
268
269 if (carrier_status)
270 netif_carrier_on(dev);
271 } else {
272 dev->mtu = new_mtu;
273 }
274
275 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Erez Shitritb6c871e2017-06-12 10:45:21 +0300278static void ipoib_get_stats(struct net_device *dev,
279 struct rtnl_link_stats64 *stats)
280{
281 struct ipoib_dev_priv *priv = ipoib_priv(dev);
282
283 if (priv->rn_ops->ndo_get_stats64)
284 priv->rn_ops->ndo_get_stats64(dev, stats);
285 else
286 netdev_stats_to_stats64(stats, &dev->stats);
287}
288
Guy Shapiroddde8962015-07-30 17:50:16 +0300289/* Called with an RCU read lock taken */
290static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr,
291 struct net_device *dev)
292{
293 struct net *net = dev_net(dev);
294 struct in_device *in_dev;
295 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
296 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
297 __be32 ret_addr;
298
299 switch (addr->sa_family) {
300 case AF_INET:
301 in_dev = in_dev_get(dev);
302 if (!in_dev)
303 return false;
304
305 ret_addr = inet_confirm_addr(net, in_dev, 0,
306 addr_in->sin_addr.s_addr,
307 RT_SCOPE_HOST);
308 in_dev_put(in_dev);
309 if (ret_addr)
310 return true;
311
312 break;
313 case AF_INET6:
314 if (IS_ENABLED(CONFIG_IPV6) &&
315 ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1))
316 return true;
317
318 break;
319 }
320 return false;
321}
322
323/**
324 * Find the master net_device on top of the given net_device.
325 * @dev: base IPoIB net_device
326 *
327 * Returns the master net_device with a reference held, or the same net_device
328 * if no master exists.
329 */
330static struct net_device *ipoib_get_master_net_dev(struct net_device *dev)
331{
332 struct net_device *master;
333
334 rcu_read_lock();
335 master = netdev_master_upper_dev_get_rcu(dev);
336 if (master)
337 dev_hold(master);
338 rcu_read_unlock();
339
340 if (master)
341 return master;
342
343 dev_hold(dev);
344 return dev;
345}
346
David Aherne0e79c82016-10-17 19:15:47 -0700347struct ipoib_walk_data {
348 const struct sockaddr *addr;
349 struct net_device *result;
350};
351
352static int ipoib_upper_walk(struct net_device *upper, void *_data)
353{
354 struct ipoib_walk_data *data = _data;
355 int ret = 0;
356
357 if (ipoib_is_dev_match_addr_rcu(data->addr, upper)) {
358 dev_hold(upper);
359 data->result = upper;
360 ret = 1;
361 }
362
363 return ret;
364}
365
Guy Shapiroddde8962015-07-30 17:50:16 +0300366/**
367 * Find a net_device matching the given address, which is an upper device of
368 * the given net_device.
369 * @addr: IP address to look for.
370 * @dev: base IPoIB net_device
371 *
372 * If found, returns the net_device with a reference held. Otherwise return
373 * NULL.
374 */
375static struct net_device *ipoib_get_net_dev_match_addr(
376 const struct sockaddr *addr, struct net_device *dev)
377{
David Aherne0e79c82016-10-17 19:15:47 -0700378 struct ipoib_walk_data data = {
379 .addr = addr,
380 };
Guy Shapiroddde8962015-07-30 17:50:16 +0300381
382 rcu_read_lock();
383 if (ipoib_is_dev_match_addr_rcu(addr, dev)) {
384 dev_hold(dev);
David Aherne0e79c82016-10-17 19:15:47 -0700385 data.result = dev;
Guy Shapiroddde8962015-07-30 17:50:16 +0300386 goto out;
387 }
388
David Aherne0e79c82016-10-17 19:15:47 -0700389 netdev_walk_all_upper_dev_rcu(dev, ipoib_upper_walk, &data);
Guy Shapiroddde8962015-07-30 17:50:16 +0300390out:
391 rcu_read_unlock();
David Aherne0e79c82016-10-17 19:15:47 -0700392 return data.result;
Guy Shapiroddde8962015-07-30 17:50:16 +0300393}
394
395/* returns the number of IPoIB netdevs on top a given ipoib device matching a
396 * pkey_index and address, if one exists.
397 *
398 * @found_net_dev: contains a matching net_device if the return value >= 1,
399 * with a reference held. */
400static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
401 const union ib_gid *gid,
402 u16 pkey_index,
403 const struct sockaddr *addr,
404 int nesting,
405 struct net_device **found_net_dev)
406{
407 struct ipoib_dev_priv *child_priv;
408 struct net_device *net_dev = NULL;
409 int matches = 0;
410
411 if (priv->pkey_index == pkey_index &&
412 (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
413 if (!addr) {
414 net_dev = ipoib_get_master_net_dev(priv->dev);
415 } else {
416 /* Verify the net_device matches the IP address, as
417 * IPoIB child devices currently share a GID. */
418 net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev);
419 }
420 if (net_dev) {
421 if (!*found_net_dev)
422 *found_net_dev = net_dev;
423 else
424 dev_put(net_dev);
425 ++matches;
426 }
427 }
428
429 /* Check child interfaces */
430 down_read_nested(&priv->vlan_rwsem, nesting);
431 list_for_each_entry(child_priv, &priv->child_intfs, list) {
432 matches += ipoib_match_gid_pkey_addr(child_priv, gid,
433 pkey_index, addr,
434 nesting + 1,
435 found_net_dev);
436 if (matches > 1)
437 break;
438 }
439 up_read(&priv->vlan_rwsem);
440
441 return matches;
442}
443
444/* Returns the number of matching net_devs found (between 0 and 2). Also
445 * return the matching net_device in the @net_dev parameter, holding a
446 * reference to the net_device, if the number of matches >= 1 */
447static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port,
448 u16 pkey_index,
449 const union ib_gid *gid,
450 const struct sockaddr *addr,
451 struct net_device **net_dev)
452{
453 struct ipoib_dev_priv *priv;
454 int matches = 0;
455
456 *net_dev = NULL;
457
458 list_for_each_entry(priv, dev_list, list) {
459 if (priv->port != port)
460 continue;
461
462 matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
463 addr, 0, net_dev);
464 if (matches > 1)
465 break;
466 }
467
468 return matches;
469}
470
471static struct net_device *ipoib_get_net_dev_by_params(
472 struct ib_device *dev, u8 port, u16 pkey,
473 const union ib_gid *gid, const struct sockaddr *addr,
474 void *client_data)
475{
476 struct net_device *net_dev;
477 struct list_head *dev_list = client_data;
478 u16 pkey_index;
479 int matches;
480 int ret;
481
482 if (!rdma_protocol_ib(dev, port))
483 return NULL;
484
485 ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
486 if (ret)
487 return NULL;
488
489 if (!dev_list)
490 return NULL;
491
492 /* See if we can find a unique device matching the L2 parameters */
493 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
494 gid, NULL, &net_dev);
495
496 switch (matches) {
497 case 0:
498 return NULL;
499 case 1:
500 return net_dev;
501 }
502
503 dev_put(net_dev);
504
505 /* Couldn't find a unique device with L2 parameters only. Use L3
506 * address to uniquely match the net device */
507 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
508 gid, addr, &net_dev);
509 switch (matches) {
510 case 0:
511 return NULL;
512 default:
513 dev_warn_ratelimited(&dev->dev,
514 "duplicate IP address detected\n");
515 /* Fall through */
516 case 1:
517 return net_dev;
518 }
519}
520
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700521int ipoib_set_mode(struct net_device *dev, const char *buf)
522{
Erez Shitritc1048af2017-04-10 11:22:29 +0300523 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700524
Feras Daoud80b5b352016-12-28 14:47:21 +0200525 if ((test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags) &&
526 !strcmp(buf, "connected\n")) ||
527 (!test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags) &&
528 !strcmp(buf, "datagram\n"))) {
529 return 0;
530 }
531
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700532 /* flush paths if we switch modes so that connections are restarted */
533 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
534 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
535 ipoib_warn(priv, "enabling connected mode "
536 "will cause multicast packet drops\n");
537 netdev_update_features(dev);
Erez Shitritedcd2a72015-06-07 13:36:11 +0300538 dev_set_mtu(dev, ipoib_cm_max_mtu(dev));
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700539 rtnl_unlock();
Christoph Hellwige622f2f2015-10-08 09:16:33 +0100540 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700541
542 ipoib_flush_paths(dev);
Feras Daoud0a0007f2016-12-28 14:47:23 +0200543 return (!rtnl_trylock()) ? -EBUSY : 0;
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700544 }
545
546 if (!strcmp(buf, "datagram\n")) {
547 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
548 netdev_update_features(dev);
549 dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
550 rtnl_unlock();
551 ipoib_flush_paths(dev);
Feras Daoud0a0007f2016-12-28 14:47:23 +0200552 return (!rtnl_trylock()) ? -EBUSY : 0;
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700553 }
554
555 return -EINVAL;
556}
557
Erez Shitrit546481c2016-08-28 10:58:31 +0300558struct ipoib_path *__path_find(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Erez Shitritc1048af2017-04-10 11:22:29 +0300560 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 struct rb_node *n = priv->path_tree.rb_node;
562 struct ipoib_path *path;
563 int ret;
564
565 while (n) {
566 path = rb_entry(n, struct ipoib_path, rb_node);
567
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300568 ret = memcmp(gid, path->pathrec.dgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 sizeof (union ib_gid));
570
571 if (ret < 0)
572 n = n->rb_left;
573 else if (ret > 0)
574 n = n->rb_right;
575 else
576 return path;
577 }
578
579 return NULL;
580}
581
582static int __path_add(struct net_device *dev, struct ipoib_path *path)
583{
Erez Shitritc1048af2017-04-10 11:22:29 +0300584 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct rb_node **n = &priv->path_tree.rb_node;
586 struct rb_node *pn = NULL;
587 struct ipoib_path *tpath;
588 int ret;
589
590 while (*n) {
591 pn = *n;
592 tpath = rb_entry(pn, struct ipoib_path, rb_node);
593
594 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
595 sizeof (union ib_gid));
596 if (ret < 0)
597 n = &pn->rb_left;
598 else if (ret > 0)
599 n = &pn->rb_right;
600 else
601 return -EEXIST;
602 }
603
604 rb_link_node(&path->rb_node, pn, n);
605 rb_insert_color(&path->rb_node, &priv->path_tree);
606
607 list_add_tail(&path->list, &priv->path_list);
608
609 return 0;
610}
611
612static void path_free(struct net_device *dev, struct ipoib_path *path)
613{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616 while ((skb = __skb_dequeue(&path->queue)))
617 dev_kfree_skb_irq(skb);
618
Erez Shitritc1048af2017-04-10 11:22:29 +0300619 ipoib_dbg(ipoib_priv(dev), "path_free\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700620
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000621 /* remove all neigh connected to this path */
622 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623
624 if (path->ah)
625 ipoib_put_ah(path->ah);
626
627 kfree(path);
628}
629
Roland Dreier1732b0e2005-11-07 10:33:11 -0800630#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
631
632struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
633{
634 struct ipoib_path_iter *iter;
635
636 iter = kmalloc(sizeof *iter, GFP_KERNEL);
637 if (!iter)
638 return NULL;
639
640 iter->dev = dev;
641 memset(iter->path.pathrec.dgid.raw, 0, 16);
642
643 if (ipoib_path_iter_next(iter)) {
644 kfree(iter);
645 return NULL;
646 }
647
648 return iter;
649}
650
651int ipoib_path_iter_next(struct ipoib_path_iter *iter)
652{
Erez Shitritc1048af2017-04-10 11:22:29 +0300653 struct ipoib_dev_priv *priv = ipoib_priv(iter->dev);
Roland Dreier1732b0e2005-11-07 10:33:11 -0800654 struct rb_node *n;
655 struct ipoib_path *path;
656 int ret = 1;
657
658 spin_lock_irq(&priv->lock);
659
660 n = rb_first(&priv->path_tree);
661
662 while (n) {
663 path = rb_entry(n, struct ipoib_path, rb_node);
664
665 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
666 sizeof (union ib_gid)) < 0) {
667 iter->path = *path;
668 ret = 0;
669 break;
670 }
671
672 n = rb_next(n);
673 }
674
675 spin_unlock_irq(&priv->lock);
676
677 return ret;
678}
679
680void ipoib_path_iter_read(struct ipoib_path_iter *iter,
681 struct ipoib_path *path)
682{
683 *path = iter->path;
684}
685
686#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
687
Moni Shouaee1e2c82008-07-14 23:48:49 -0700688void ipoib_mark_paths_invalid(struct net_device *dev)
689{
Erez Shitritc1048af2017-04-10 11:22:29 +0300690 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Moni Shouaee1e2c82008-07-14 23:48:49 -0700691 struct ipoib_path *path, *tp;
692
693 spin_lock_irq(&priv->lock);
694
695 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
Dasaratharaman Chandramouli57520752017-04-27 19:06:01 -0400696 ipoib_dbg(priv, "mark path LID 0x%08x GID %pI6 invalid\n",
697 be32_to_cpu(sa_path_get_dlid(&path->pathrec)),
698 path->pathrec.dgid.raw);
Moni Shouaee1e2c82008-07-14 23:48:49 -0700699 path->valid = 0;
700 }
701
702 spin_unlock_irq(&priv->lock);
703}
704
Erez Shitrit2b084172017-02-01 19:10:05 +0200705static void push_pseudo_header(struct sk_buff *skb, const char *daddr)
706{
707 struct ipoib_pseudo_header *phdr;
708
Johannes Bergd58ff352017-06-16 14:29:23 +0200709 phdr = skb_push(skb, sizeof(*phdr));
Erez Shitrit2b084172017-02-01 19:10:05 +0200710 memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
711}
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713void ipoib_flush_paths(struct net_device *dev)
714{
Erez Shitritc1048af2017-04-10 11:22:29 +0300715 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 struct ipoib_path *path, *tp;
717 LIST_HEAD(remove_list);
Roland Dreier943c2462008-09-30 10:36:21 -0700718 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719
Roland Dreier943c2462008-09-30 10:36:21 -0700720 netif_tx_lock_bh(dev);
721 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722
Robert P. J. Day157de222008-04-16 21:09:26 -0700723 list_splice_init(&priv->path_list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724
725 list_for_each_entry(path, &remove_list, list)
726 rb_erase(&path->rb_node, &priv->path_tree);
727
Linus Torvalds1da177e2005-04-16 15:20:36 -0700728 list_for_each_entry_safe(path, tp, &remove_list, list) {
729 if (path->query)
730 ib_sa_cancel_query(path->query_id, path->query);
Roland Dreier943c2462008-09-30 10:36:21 -0700731 spin_unlock_irqrestore(&priv->lock, flags);
732 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733 wait_for_completion(&path->done);
734 path_free(dev, path);
Roland Dreier943c2462008-09-30 10:36:21 -0700735 netif_tx_lock_bh(dev);
736 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 }
Roland Dreier943c2462008-09-30 10:36:21 -0700738
739 spin_unlock_irqrestore(&priv->lock, flags);
740 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741}
742
743static void path_rec_completion(int status,
Dasaratharaman Chandramoulic2f8fc42017-04-27 19:05:58 -0400744 struct sa_path_rec *pathrec,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 void *path_ptr)
746{
747 struct ipoib_path *path = path_ptr;
748 struct net_device *dev = path->dev;
Erez Shitritc1048af2017-04-10 11:22:29 +0300749 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 struct ipoib_ah *ah = NULL;
Roland Dreierc9da4ba2008-09-25 15:26:15 -0700751 struct ipoib_ah *old_ah = NULL;
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700752 struct ipoib_neigh *neigh, *tn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 struct sk_buff_head skqueue;
754 struct sk_buff *skb;
755 unsigned long flags;
756
Roland Dreier843613b2007-02-26 12:57:08 -0800757 if (!status)
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700758 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
Dasaratharaman Chandramouli57520752017-04-27 19:06:01 -0400759 be32_to_cpu(sa_path_get_dlid(pathrec)),
Dasaratharaman Chandramouli9fdca4d2017-04-27 19:06:00 -0400760 pathrec->dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761 else
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700762 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700763 status, path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764
765 skb_queue_head_init(&skqueue);
766
767 if (!status) {
Dasaratharaman Chandramouli90898852017-04-29 14:41:18 -0400768 struct rdma_ah_attr av;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700770 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
771 ah = ipoib_create_ah(dev, priv->pd, &av);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 }
773
774 spin_lock_irqsave(&priv->lock, flags);
775
Mike Marciniszyn38743972011-11-21 08:43:54 -0500776 if (!IS_ERR_OR_NULL(ah)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 path->pathrec = *pathrec;
778
Roland Dreierc9da4ba2008-09-25 15:26:15 -0700779 old_ah = path->ah;
780 path->ah = ah;
781
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
Dasaratharaman Chandramouli57520752017-04-27 19:06:01 -0400783 ah, be32_to_cpu(sa_path_get_dlid(pathrec)),
Dasaratharaman Chandramouli9fdca4d2017-04-27 19:06:00 -0400784 pathrec->sl);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786 while ((skb = __skb_dequeue(&path->queue)))
787 __skb_queue_tail(&skqueue, skb);
788
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700789 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
Moni Shouaee1e2c82008-07-14 23:48:49 -0700790 if (neigh->ah) {
791 WARN_ON(neigh->ah != old_ah);
792 /*
793 * Dropping the ah reference inside
794 * priv->lock is safe here, because we
795 * will hold one more reference from
796 * the original value of path->ah (ie
797 * old_ah).
798 */
799 ipoib_put_ah(neigh->ah);
800 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 kref_get(&path->ah->ref);
802 neigh->ah = path->ah;
803
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000804 if (ipoib_cm_enabled(dev, neigh->daddr)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200805 if (!ipoib_cm_get(neigh))
806 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
807 path,
808 neigh));
809 if (!ipoib_cm_get(neigh)) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000810 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200811 continue;
812 }
813 }
814
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 while ((skb = __skb_dequeue(&neigh->queue)))
816 __skb_queue_tail(&skqueue, skb);
817 }
Moni Shouaee1e2c82008-07-14 23:48:49 -0700818 path->valid = 1;
Roland Dreier5872a9f2005-11-29 10:13:54 -0800819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
Roland Dreier5872a9f2005-11-29 10:13:54 -0800821 path->query = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 complete(&path->done);
823
824 spin_unlock_irqrestore(&priv->lock, flags);
825
Roland Dreierf72dd562013-02-25 09:42:15 -0800826 if (IS_ERR_OR_NULL(ah))
827 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
828
Moni Shouaee1e2c82008-07-14 23:48:49 -0700829 if (old_ah)
830 ipoib_put_ah(old_ah);
831
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 while ((skb = __skb_dequeue(&skqueue))) {
Feras Daoudd32b9a82016-12-28 14:47:25 +0200833 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 skb->dev = dev;
Feras Daoudd32b9a82016-12-28 14:47:25 +0200835 ret = dev_queue_xmit(skb);
836 if (ret)
837 ipoib_warn(priv, "%s: dev_queue_xmit failed to re-queue packet, ret:%d\n",
838 __func__, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839 }
840}
841
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300842static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Erez Shitritc1048af2017-04-10 11:22:29 +0300844 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700845 struct ipoib_path *path;
846
Jack Morgenstein1401b532007-11-26 10:41:19 +0200847 if (!priv->broadcast)
848 return NULL;
849
Roland Dreier21a38482005-11-02 10:07:59 -0800850 path = kzalloc(sizeof *path, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851 if (!path)
852 return NULL;
853
Roland Dreier21a38482005-11-02 10:07:59 -0800854 path->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855
856 skb_queue_head_init(&path->queue);
857
858 INIT_LIST_HEAD(&path->neigh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859
Dasaratharaman Chandramouli4c33bd12017-04-27 19:06:02 -0400860 if (rdma_cap_opa_ah(priv->ca, priv->port))
861 path->pathrec.rec_type = SA_PATH_REC_TYPE_OPA;
862 else
863 path->pathrec.rec_type = SA_PATH_REC_TYPE_IB;
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300864 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
Roland Dreier2337f802007-10-23 19:57:54 -0700865 path->pathrec.sgid = priv->local_gid;
866 path->pathrec.pkey = cpu_to_be16(priv->pkey);
Sean Hefty81668832007-08-02 12:21:31 -0700867 path->pathrec.numb_path = 1;
868 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869
870 return path;
871}
872
873static int path_rec_start(struct net_device *dev,
874 struct ipoib_path *path)
875{
Erez Shitritc1048af2017-04-10 11:22:29 +0300876 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700878 ipoib_dbg(priv, "Start path record lookup for %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700879 path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700880
Roland Dreier65c7edd2005-11-28 21:20:34 -0800881 init_completion(&path->done);
882
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 path->query_id =
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700884 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 &path->pathrec,
886 IB_SA_PATH_REC_DGID |
887 IB_SA_PATH_REC_SGID |
888 IB_SA_PATH_REC_NUMB_PATH |
Sean Hefty81668832007-08-02 12:21:31 -0700889 IB_SA_PATH_REC_TRAFFIC_CLASS |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890 IB_SA_PATH_REC_PKEY,
891 1000, GFP_ATOMIC,
892 path_rec_completion,
893 path, &path->query);
894 if (path->query_id < 0) {
Or Gerlitz01b3fc82008-07-22 14:18:34 -0700895 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700896 path->query = NULL;
Yossi Etigin93a3ab92008-11-12 10:24:38 -0800897 complete(&path->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return path->query_id;
899 }
900
901 return 0;
902}
903
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000904static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
905 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
Erez Shitritc1048af2017-04-10 11:22:29 +0300907 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Erez Shitritcd565b42017-04-10 11:22:30 +0300908 struct rdma_netdev *rn = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700909 struct ipoib_path *path;
910 struct ipoib_neigh *neigh;
Roland Dreier943c2462008-09-30 10:36:21 -0700911 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700912
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000913 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000914 neigh = ipoib_neigh_alloc(daddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915 if (!neigh) {
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000916 spin_unlock_irqrestore(&priv->lock, flags);
Roland Dreierde903512007-09-28 15:33:51 -0700917 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918 dev_kfree_skb_any(skb);
919 return;
920 }
921
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000922 path = __path_find(dev, daddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700923 if (!path) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000924 path = path_rec_create(dev, daddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 if (!path)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300926 goto err_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700927
928 __path_add(dev, path);
929 }
930
931 list_add_tail(&neigh->list, &path->neigh_list);
932
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -0800933 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 kref_get(&path->ah->ref);
935 neigh->ah = path->ah;
936
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000937 if (ipoib_cm_enabled(dev, neigh->daddr)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200938 if (!ipoib_cm_get(neigh))
939 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
940 if (!ipoib_cm_get(neigh)) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000941 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200942 goto err_drop;
943 }
Paolo Abenifc791b62016-10-13 18:26:56 +0200944 if (skb_queue_len(&neigh->queue) <
945 IPOIB_MAX_PATH_REC_QUEUE) {
Erez Shitrit2b084172017-02-01 19:10:05 +0200946 push_pseudo_header(skb, neigh->daddr);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200947 __skb_queue_tail(&neigh->queue, skb);
Paolo Abenifc791b62016-10-13 18:26:56 +0200948 } else {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200949 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
950 skb_queue_len(&neigh->queue));
951 goto err_drop;
952 }
Roland Dreier721d67c2009-09-05 20:23:40 -0700953 } else {
954 spin_unlock_irqrestore(&priv->lock, flags);
Erez Shitritcd565b42017-04-10 11:22:30 +0300955 path->ah->last_send = rn->send(dev, skb, path->ah->ah,
956 IPOIB_QPN(daddr));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000957 ipoib_neigh_put(neigh);
Roland Dreier721d67c2009-09-05 20:23:40 -0700958 return;
959 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960 } else {
961 neigh->ah = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962
963 if (!path->query && path_rec_start(dev, path))
Jim Foraker49b8e742013-08-08 17:44:22 -0700964 goto err_path;
Erez Shitrit2b084172017-02-01 19:10:05 +0200965 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
966 push_pseudo_header(skb, neigh->daddr);
Erez Shitrit1e85b802015-04-02 13:39:03 +0300967 __skb_queue_tail(&neigh->queue, skb);
Erez Shitrit2b084172017-02-01 19:10:05 +0200968 } else {
Erez Shitrit1e85b802015-04-02 13:39:03 +0300969 goto err_drop;
Erez Shitrit2b084172017-02-01 19:10:05 +0200970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971 }
972
Roland Dreier943c2462008-09-30 10:36:21 -0700973 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000974 ipoib_neigh_put(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700975 return;
976
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300977err_path:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000978 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200979err_drop:
Roland Dreierde903512007-09-28 15:33:51 -0700980 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 dev_kfree_skb_any(skb);
982
Roland Dreier943c2462008-09-30 10:36:21 -0700983 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000984 ipoib_neigh_put(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700985}
986
987static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
Paolo Abenifc791b62016-10-13 18:26:56 +0200988 struct ipoib_pseudo_header *phdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700989{
Erez Shitritc1048af2017-04-10 11:22:29 +0300990 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Erez Shitritcd565b42017-04-10 11:22:30 +0300991 struct rdma_netdev *rn = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 struct ipoib_path *path;
Roland Dreier943c2462008-09-30 10:36:21 -0700993 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994
Roland Dreier943c2462008-09-30 10:36:21 -0700995 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700996
Paolo Abenifc791b62016-10-13 18:26:56 +0200997 path = __path_find(dev, phdr->hwaddr + 4);
Moni Shouaee1e2c82008-07-14 23:48:49 -0700998 if (!path || !path->valid) {
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800999 int new_path = 0;
1000
1001 if (!path) {
Paolo Abenifc791b62016-10-13 18:26:56 +02001002 path = path_rec_create(dev, phdr->hwaddr + 4);
Jack Morgenstein71d98b42009-02-17 14:51:47 -08001003 new_path = 1;
1004 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001005 if (path) {
Erez Shitrit1e85b802015-04-02 13:39:03 +03001006 if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
Erez Shitrit2b084172017-02-01 19:10:05 +02001007 push_pseudo_header(skb, phdr->hwaddr);
Erez Shitrit1e85b802015-04-02 13:39:03 +03001008 __skb_queue_tail(&path->queue, skb);
1009 } else {
1010 ++dev->stats.tx_dropped;
1011 dev_kfree_skb_any(skb);
1012 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
Yossi Etiginff79ae82008-11-12 10:24:39 -08001014 if (!path->query && path_rec_start(dev, path)) {
Roland Dreier943c2462008-09-30 10:36:21 -07001015 spin_unlock_irqrestore(&priv->lock, flags);
Jack Morgenstein71d98b42009-02-17 14:51:47 -08001016 if (new_path)
1017 path_free(dev, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001018 return;
1019 } else
1020 __path_add(dev, path);
1021 } else {
Roland Dreierde903512007-09-28 15:33:51 -07001022 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 dev_kfree_skb_any(skb);
1024 }
1025
Roland Dreier943c2462008-09-30 10:36:21 -07001026 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027 return;
1028 }
1029
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -08001030 if (path->ah) {
Dasaratharaman Chandramouli57520752017-04-27 19:06:01 -04001031 ipoib_dbg(priv, "Send unicast ARP to %08x\n",
1032 be32_to_cpu(sa_path_get_dlid(&path->pathrec)));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033
Roland Dreier721d67c2009-09-05 20:23:40 -07001034 spin_unlock_irqrestore(&priv->lock, flags);
Erez Shitritcd565b42017-04-10 11:22:30 +03001035 path->ah->last_send = rn->send(dev, skb, path->ah->ah,
1036 IPOIB_QPN(phdr->hwaddr));
Roland Dreier721d67c2009-09-05 20:23:40 -07001037 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 } else if ((path->query || !path_rec_start(dev, path)) &&
1039 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
Erez Shitrit2b084172017-02-01 19:10:05 +02001040 push_pseudo_header(skb, phdr->hwaddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041 __skb_queue_tail(&path->queue, skb);
1042 } else {
Roland Dreierde903512007-09-28 15:33:51 -07001043 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044 dev_kfree_skb_any(skb);
1045 }
1046
Roland Dreier943c2462008-09-30 10:36:21 -07001047 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048}
1049
1050static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
1051{
Erez Shitritc1048af2017-04-10 11:22:29 +03001052 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Erez Shitritcd565b42017-04-10 11:22:30 +03001053 struct rdma_netdev *rn = netdev_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 struct ipoib_neigh *neigh;
Paolo Abenifc791b62016-10-13 18:26:56 +02001055 struct ipoib_pseudo_header *phdr;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001056 struct ipoib_header *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 unsigned long flags;
1058
Paolo Abenifc791b62016-10-13 18:26:56 +02001059 phdr = (struct ipoib_pseudo_header *) skb->data;
1060 skb_pull(skb, sizeof(*phdr));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001061 header = (struct ipoib_header *) skb->data;
1062
Paolo Abenifc791b62016-10-13 18:26:56 +02001063 if (unlikely(phdr->hwaddr[4] == 0xff)) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001064 /* multicast, arrange "if" according to probability */
1065 if ((header->proto != htons(ETH_P_IP)) &&
1066 (header->proto != htons(ETH_P_IPV6)) &&
1067 (header->proto != htons(ETH_P_ARP)) &&
Patrick McHardydc850b02013-04-17 06:18:29 +00001068 (header->proto != htons(ETH_P_RARP)) &&
1069 (header->proto != htons(ETH_P_TIPC))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001070 /* ethertype not supported by IPoIB */
David Miller17e6abe2011-12-02 16:52:44 +00001071 ++dev->stats.tx_dropped;
1072 dev_kfree_skb_any(skb);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001073 return NETDEV_TX_OK;
David Miller17e6abe2011-12-02 16:52:44 +00001074 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001075 /* Add in the P_Key for multicast*/
Paolo Abenifc791b62016-10-13 18:26:56 +02001076 phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
1077 phdr->hwaddr[9] = priv->pkey & 0xff;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001078
Paolo Abenifc791b62016-10-13 18:26:56 +02001079 neigh = ipoib_neigh_get(dev, phdr->hwaddr);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001080 if (likely(neigh))
1081 goto send_using_neigh;
Paolo Abenifc791b62016-10-13 18:26:56 +02001082 ipoib_mcast_send(dev, phdr->hwaddr, skb);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001083 return NETDEV_TX_OK;
David Miller17e6abe2011-12-02 16:52:44 +00001084 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001086 /* unicast, arrange "switch" according to probability */
1087 switch (header->proto) {
1088 case htons(ETH_P_IP):
1089 case htons(ETH_P_IPV6):
Patrick McHardydc850b02013-04-17 06:18:29 +00001090 case htons(ETH_P_TIPC):
Paolo Abenifc791b62016-10-13 18:26:56 +02001091 neigh = ipoib_neigh_get(dev, phdr->hwaddr);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001092 if (unlikely(!neigh)) {
Paolo Abenifc791b62016-10-13 18:26:56 +02001093 neigh_add_path(skb, phdr->hwaddr, dev);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001094 return NETDEV_TX_OK;
Yossi Etigina50df392009-01-09 14:05:11 -08001095 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001096 break;
1097 case htons(ETH_P_ARP):
1098 case htons(ETH_P_RARP):
1099 /* for unicast ARP and RARP should always perform path find */
Paolo Abenifc791b62016-10-13 18:26:56 +02001100 unicast_arp_send(skb, dev, phdr);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001101 return NETDEV_TX_OK;
1102 default:
1103 /* ethertype not supported by IPoIB */
1104 ++dev->stats.tx_dropped;
1105 dev_kfree_skb_any(skb);
1106 return NETDEV_TX_OK;
1107 }
Michael S. Tsirkin8a7f7522006-07-19 17:44:37 +03001108
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001109send_using_neigh:
1110 /* note we now hold a ref to neigh */
1111 if (ipoib_cm_get(neigh)) {
1112 if (ipoib_cm_up(neigh)) {
1113 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
1114 goto unref;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001116 } else if (neigh->ah) {
Erez Shitritcd565b42017-04-10 11:22:30 +03001117 neigh->ah->last_send = rn->send(dev, skb, neigh->ah->ah,
1118 IPOIB_QPN(phdr->hwaddr));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001119 goto unref;
1120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001121
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001122 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
Erez Shitrit2b084172017-02-01 19:10:05 +02001123 push_pseudo_header(skb, phdr->hwaddr);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001124 spin_lock_irqsave(&priv->lock, flags);
1125 __skb_queue_tail(&neigh->queue, skb);
1126 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 } else {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001128 ++dev->stats.tx_dropped;
1129 dev_kfree_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001131
1132unref:
1133 ipoib_neigh_put(neigh);
1134
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 return NETDEV_TX_OK;
1136}
1137
Linus Torvalds1da177e2005-04-16 15:20:36 -07001138static void ipoib_timeout(struct net_device *dev)
1139{
Erez Shitritc1048af2017-04-10 11:22:29 +03001140 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141
Roland Dreier4b2d3192005-10-18 12:20:06 -07001142 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
Florian Westphal4d0e9652016-05-03 16:30:59 +02001143 jiffies_to_msecs(jiffies - dev_trans_start(dev)));
Roland Dreier4b2d3192005-10-18 12:20:06 -07001144 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
1145 netif_queue_stopped(dev),
1146 priv->tx_head, priv->tx_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 /* XXX reset QP, etc. */
1148}
1149
1150static int ipoib_hard_header(struct sk_buff *skb,
1151 struct net_device *dev,
1152 unsigned short type,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001153 const void *daddr, const void *saddr, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154{
1155 struct ipoib_header *header;
1156
Johannes Bergd58ff352017-06-16 14:29:23 +02001157 header = skb_push(skb, sizeof *header);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 header->proto = htons(type);
1160 header->reserved = 0;
1161
1162 /*
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001163 * we don't rely on dst_entry structure, always stuff the
Paolo Abenifc791b62016-10-13 18:26:56 +02001164 * destination address into skb hard header so we can figure out where
Roland Dreier936d7de2012-02-07 14:51:21 +00001165 * to send the packet later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 */
Erez Shitrit2b084172017-02-01 19:10:05 +02001167 push_pseudo_header(skb, daddr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001168
Paolo Abenifc791b62016-10-13 18:26:56 +02001169 return IPOIB_HARD_LEN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001170}
1171
1172static void ipoib_set_mcast_list(struct net_device *dev)
1173{
Erez Shitritc1048af2017-04-10 11:22:29 +03001174 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Leonid Arsh7a343d42006-03-23 19:52:51 +02001176 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
1177 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
1178 return;
1179 }
1180
Doug Ledford0b395782015-02-21 19:27:03 -05001181 queue_work(priv->wq, &priv->restart_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001182}
1183
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +02001184static int ipoib_get_iflink(const struct net_device *dev)
1185{
Erez Shitritc1048af2017-04-10 11:22:29 +03001186 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +02001187
Erez Shitrit2c153952015-04-16 16:34:34 +03001188 /* parent interface */
1189 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
1190 return dev->ifindex;
1191
1192 /* child/vlan interface */
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +02001193 return priv->parent->ifindex;
1194}
1195
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001196static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001198 /*
1199 * Use only the address parts that contributes to spreading
1200 * The subnet prefix is not used as one can not connect to
1201 * same remote port (GUID) using the same remote QPN via two
1202 * different subnets.
1203 */
1204 /* qpn octets[1:4) & port GUID octets[12:20) */
Shlomo Pongratz9d1ad662013-02-19 15:40:22 +00001205 u32 *d32 = (u32 *) daddr;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001206 u32 hv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001207
Shlomo Pongratz9d1ad662013-02-19 15:40:22 +00001208 hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001209 return hv & htbl->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210}
1211
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001212struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
1213{
Erez Shitritc1048af2017-04-10 11:22:29 +03001214 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001215 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1216 struct ipoib_neigh_hash *htbl;
1217 struct ipoib_neigh *neigh = NULL;
1218 u32 hash_val;
1219
1220 rcu_read_lock_bh();
1221
1222 htbl = rcu_dereference_bh(ntbl->htbl);
1223
1224 if (!htbl)
1225 goto out_unlock;
1226
1227 hash_val = ipoib_addr_hash(htbl, daddr);
1228 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
1229 neigh != NULL;
1230 neigh = rcu_dereference_bh(neigh->hnext)) {
1231 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1232 /* found, take one ref on behalf of the caller */
1233 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1234 /* deleted */
1235 neigh = NULL;
1236 goto out_unlock;
1237 }
Erez Shitrit61c78eea2016-06-04 15:15:19 +03001238
1239 if (likely(skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE))
1240 neigh->alive = jiffies;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001241 goto out_unlock;
1242 }
1243 }
1244
1245out_unlock:
1246 rcu_read_unlock_bh();
1247 return neigh;
1248}
1249
1250static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
1251{
1252 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1253 struct ipoib_neigh_hash *htbl;
1254 unsigned long neigh_obsolete;
1255 unsigned long dt;
1256 unsigned long flags;
1257 int i;
Christoph Lameterbd99b2e2015-09-24 12:00:05 -05001258 LIST_HEAD(remove_list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001259
1260 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1261 return;
1262
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001263 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001264
1265 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001266 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001267
1268 if (!htbl)
1269 goto out_unlock;
1270
1271 /* neigh is obsolete if it was idle for two GC periods */
1272 dt = 2 * arp_tbl.gc_interval;
1273 neigh_obsolete = jiffies - dt;
1274 /* handle possible race condition */
1275 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1276 goto out_unlock;
1277
1278 for (i = 0; i < htbl->size; i++) {
1279 struct ipoib_neigh *neigh;
1280 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1281
1282 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001283 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001284 /* was the neigh idle for two GC periods */
1285 if (time_after(neigh_obsolete, neigh->alive)) {
Christoph Lameterbd99b2e2015-09-24 12:00:05 -05001286
Christoph Lameter432c55f2015-12-21 08:42:54 -06001287 ipoib_check_and_add_mcast_sendonly(priv, neigh->daddr + 4, &remove_list);
Christoph Lameterbd99b2e2015-09-24 12:00:05 -05001288
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001289 rcu_assign_pointer(*np,
1290 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001291 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001292 /* remove from path/mc list */
Feras Daoudc5860712016-12-28 14:47:27 +02001293 list_del_init(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001294 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1295 } else {
1296 np = &neigh->hnext;
1297 }
1298
1299 }
1300 }
1301
1302out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001303 spin_unlock_irqrestore(&priv->lock, flags);
Erez Shitrit50be28d2016-01-07 09:28:08 +02001304 ipoib_mcast_remove_list(&remove_list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001305}
1306
1307static void ipoib_reap_neigh(struct work_struct *work)
1308{
1309 struct ipoib_dev_priv *priv =
1310 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
1311
1312 __ipoib_reap_neigh(priv);
1313
1314 if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
Doug Ledford0b395782015-02-21 19:27:03 -05001315 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001316 arp_tbl.gc_interval);
1317}
1318
1319
1320static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
Moni Shoua732a2172007-10-09 19:43:36 -07001321 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001322{
1323 struct ipoib_neigh *neigh;
1324
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001325 neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001326 if (!neigh)
1327 return NULL;
1328
Moni Shoua732a2172007-10-09 19:43:36 -07001329 neigh->dev = dev;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001330 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
Roland Dreier82b39912006-12-12 14:48:18 -08001331 skb_queue_head_init(&neigh->queue);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001332 INIT_LIST_HEAD(&neigh->list);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001333 ipoib_cm_set(neigh, NULL);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001334 /* one ref on behalf of the caller */
1335 atomic_set(&neigh->refcnt, 1);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001336
1337 return neigh;
1338}
1339
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001340struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
1341 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001342{
Erez Shitritc1048af2017-04-10 11:22:29 +03001343 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001344 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1345 struct ipoib_neigh_hash *htbl;
1346 struct ipoib_neigh *neigh;
1347 u32 hash_val;
1348
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001349 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001350 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001351 if (!htbl) {
1352 neigh = NULL;
1353 goto out_unlock;
1354 }
1355
1356 /* need to add a new neigh, but maybe some other thread succeeded?
1357 * recalc hash, maybe hash resize took place so we do a search
1358 */
1359 hash_val = ipoib_addr_hash(htbl, daddr);
1360 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001361 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001362 neigh != NULL;
1363 neigh = rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001364 lockdep_is_held(&priv->lock))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001365 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1366 /* found, take one ref on behalf of the caller */
1367 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1368 /* deleted */
1369 neigh = NULL;
1370 break;
1371 }
1372 neigh->alive = jiffies;
1373 goto out_unlock;
1374 }
1375 }
1376
1377 neigh = ipoib_neigh_ctor(daddr, dev);
1378 if (!neigh)
1379 goto out_unlock;
1380
1381 /* one ref on behalf of the hash table */
1382 atomic_inc(&neigh->refcnt);
1383 neigh->alive = jiffies;
1384 /* put in hash */
1385 rcu_assign_pointer(neigh->hnext,
1386 rcu_dereference_protected(htbl->buckets[hash_val],
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001387 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001388 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1389 atomic_inc(&ntbl->entries);
1390
1391out_unlock:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001392
1393 return neigh;
1394}
1395
1396void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1397{
1398 /* neigh reference count was dropprd to zero */
1399 struct net_device *dev = neigh->dev;
Erez Shitritc1048af2017-04-10 11:22:29 +03001400 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001401 struct sk_buff *skb;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001402 if (neigh->ah)
1403 ipoib_put_ah(neigh->ah);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001404 while ((skb = __skb_dequeue(&neigh->queue))) {
Roland Dreierde903512007-09-28 15:33:51 -07001405 ++dev->stats.tx_dropped;
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001406 dev_kfree_skb_any(skb);
1407 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001408 if (ipoib_cm_get(neigh))
1409 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
Erez Shitritc1048af2017-04-10 11:22:29 +03001410 ipoib_dbg(ipoib_priv(dev),
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001411 "neigh free for %06x %pI6\n",
1412 IPOIB_QPN(neigh->daddr),
1413 neigh->daddr + 4);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001414 kfree(neigh);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001415 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1416 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1417 complete(&priv->ntbl.flushed);
1418 }
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001419}
1420
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001421static void ipoib_neigh_reclaim(struct rcu_head *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001422{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001423 /* Called as a result of removal from hash table */
1424 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1425 /* note TX context may hold another ref */
1426 ipoib_neigh_put(neigh);
1427}
1428
1429void ipoib_neigh_free(struct ipoib_neigh *neigh)
1430{
1431 struct net_device *dev = neigh->dev;
Erez Shitritc1048af2017-04-10 11:22:29 +03001432 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001433 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1434 struct ipoib_neigh_hash *htbl;
1435 struct ipoib_neigh __rcu **np;
1436 struct ipoib_neigh *n;
1437 u32 hash_val;
1438
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001439 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001440 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001441 if (!htbl)
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001442 return;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001443
1444 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1445 np = &htbl->buckets[hash_val];
1446 for (n = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001447 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001448 n != NULL;
Shlomo Pongratz6c723a62012-08-13 14:39:50 +00001449 n = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001450 lockdep_is_held(&priv->lock))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001451 if (n == neigh) {
1452 /* found */
1453 rcu_assign_pointer(*np,
1454 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001455 lockdep_is_held(&priv->lock)));
Jim Foraker49b8e742013-08-08 17:44:22 -07001456 /* remove from parent list */
Feras Daoudc5860712016-12-28 14:47:27 +02001457 list_del_init(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001458 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001459 return;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001460 } else {
1461 np = &n->hnext;
1462 }
1463 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001464}
1465
1466static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1467{
1468 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1469 struct ipoib_neigh_hash *htbl;
Bart Van Assche52374962015-05-26 15:03:48 +02001470 struct ipoib_neigh __rcu **buckets;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001471 u32 size;
1472
1473 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1474 ntbl->htbl = NULL;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001475 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1476 if (!htbl)
1477 return -ENOMEM;
1478 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1479 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1480 buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1481 if (!buckets) {
1482 kfree(htbl);
1483 return -ENOMEM;
1484 }
1485 htbl->size = size;
1486 htbl->mask = (size - 1);
1487 htbl->buckets = buckets;
Bart Van Assche52374962015-05-26 15:03:48 +02001488 RCU_INIT_POINTER(ntbl->htbl, htbl);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001489 htbl->ntbl = ntbl;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001490 atomic_set(&ntbl->entries, 0);
1491
1492 /* start garbage collection */
1493 clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
Doug Ledford0b395782015-02-21 19:27:03 -05001494 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001495 arp_tbl.gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001496
1497 return 0;
1498}
1499
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001500static void neigh_hash_free_rcu(struct rcu_head *head)
1501{
1502 struct ipoib_neigh_hash *htbl = container_of(head,
1503 struct ipoib_neigh_hash,
1504 rcu);
1505 struct ipoib_neigh __rcu **buckets = htbl->buckets;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001506 struct ipoib_neigh_table *ntbl = htbl->ntbl;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001507
1508 kfree(buckets);
1509 kfree(htbl);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001510 complete(&ntbl->deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001511}
1512
1513void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1514{
Erez Shitritc1048af2017-04-10 11:22:29 +03001515 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001516 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1517 struct ipoib_neigh_hash *htbl;
1518 unsigned long flags;
1519 int i;
1520
1521 /* remove all neigh connected to a given path or mcast */
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001522 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001523
1524 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001525 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001526
1527 if (!htbl)
1528 goto out_unlock;
1529
1530 for (i = 0; i < htbl->size; i++) {
1531 struct ipoib_neigh *neigh;
1532 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1533
1534 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001535 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001536 /* delete neighs belong to this parent */
1537 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1538 rcu_assign_pointer(*np,
1539 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001540 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001541 /* remove from parent list */
Feras Daoudc5860712016-12-28 14:47:27 +02001542 list_del_init(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001543 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1544 } else {
1545 np = &neigh->hnext;
1546 }
1547
1548 }
1549 }
1550out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001551 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001552}
1553
1554static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1555{
1556 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1557 struct ipoib_neigh_hash *htbl;
1558 unsigned long flags;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001559 int i, wait_flushed = 0;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001560
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001561 init_completion(&priv->ntbl.flushed);
Feras Daoudd2e46fc2017-07-16 11:33:01 +03001562 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001563
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001564 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001565
1566 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001567 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001568 if (!htbl)
1569 goto out_unlock;
1570
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001571 wait_flushed = atomic_read(&priv->ntbl.entries);
1572 if (!wait_flushed)
1573 goto free_htbl;
1574
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001575 for (i = 0; i < htbl->size; i++) {
1576 struct ipoib_neigh *neigh;
1577 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1578
1579 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001580 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001581 rcu_assign_pointer(*np,
1582 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001583 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001584 /* remove from path/mc list */
Feras Daoudc5860712016-12-28 14:47:27 +02001585 list_del_init(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001586 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1587 }
1588 }
1589
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001590free_htbl:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001591 rcu_assign_pointer(ntbl->htbl, NULL);
1592 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1593
1594out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001595 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001596 if (wait_flushed)
1597 wait_for_completion(&priv->ntbl.flushed);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001598}
1599
1600static void ipoib_neigh_hash_uninit(struct net_device *dev)
1601{
Erez Shitritc1048af2017-04-10 11:22:29 +03001602 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001603 int stopped;
1604
1605 ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001606 init_completion(&priv->ntbl.deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001607
1608 /* Stop GC if called at init fail need to cancel work */
1609 stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1610 if (!stopped)
1611 cancel_delayed_work(&priv->neigh_reap_task);
1612
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001613 ipoib_flush_neighs(priv);
1614
1615 wait_for_completion(&priv->ntbl.deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001616}
1617
Leon Romanovsky0a1a9722017-05-14 13:32:06 +03001618static void ipoib_dev_uninit_default(struct net_device *dev)
Erez Shitrit515ed4f2017-04-10 11:22:26 +03001619{
Erez Shitritc1048af2017-04-10 11:22:29 +03001620 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001621
Erez Shitrit515ed4f2017-04-10 11:22:26 +03001622 ipoib_transport_dev_cleanup(dev);
1623
Alex Veskerb53d4562017-06-14 09:59:07 +03001624 netif_napi_del(&priv->napi);
1625
Erez Shitrit515ed4f2017-04-10 11:22:26 +03001626 ipoib_cm_dev_cleanup(dev);
1627
1628 kfree(priv->rx_ring);
1629 vfree(priv->tx_ring);
1630
1631 priv->rx_ring = NULL;
1632 priv->tx_ring = NULL;
1633}
1634
Erez Shitritcd565b42017-04-10 11:22:30 +03001635static int ipoib_dev_init_default(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636{
Erez Shitritc1048af2017-04-10 11:22:29 +03001637 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Erez Shitritcd565b42017-04-10 11:22:30 +03001639 netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT);
1640
Linus Torvalds1da177e2005-04-16 15:20:36 -07001641 /* Allocate RX/TX "rings" to hold queued skbs */
Shirley Ma0f485252006-04-10 09:43:58 -07001642 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 GFP_KERNEL);
Leon Romanovsky74226642016-11-03 16:44:25 +02001644 if (!priv->rx_ring)
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001645 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001646
Joe Perches948579c2010-11-05 03:07:36 +00001647 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 if (!priv->tx_ring) {
1649 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
Erez Shitritcd565b42017-04-10 11:22:30 +03001650 priv->ca->name, ipoib_sendq_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 goto out_rx_ring_cleanup;
1652 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001654 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655
Erez Shitritcd565b42017-04-10 11:22:30 +03001656 if (ipoib_transport_dev_init(dev, priv->ca)) {
1657 pr_warn("%s: ipoib_transport_dev_init failed\n",
1658 priv->ca->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 goto out_tx_ring_cleanup;
Erez Shitrit515ed4f2017-04-10 11:22:26 +03001660 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661
Erez Shitritcd565b42017-04-10 11:22:30 +03001662 /* after qp created set dev address */
1663 priv->dev->dev_addr[1] = (priv->qp->qp_num >> 16) & 0xff;
1664 priv->dev->dev_addr[2] = (priv->qp->qp_num >> 8) & 0xff;
1665 priv->dev->dev_addr[3] = (priv->qp->qp_num) & 0xff;
1666
Erez Shitrit515ed4f2017-04-10 11:22:26 +03001667 setup_timer(&priv->poll_timer, ipoib_ib_tx_timer_func,
1668 (unsigned long)dev);
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001669
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 return 0;
1671
1672out_tx_ring_cleanup:
Roland Dreier10313cb2008-03-12 07:51:03 -07001673 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675out_rx_ring_cleanup:
1676 kfree(priv->rx_ring);
1677
1678out:
Alex Veskerb53d4562017-06-14 09:59:07 +03001679 netif_napi_del(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 return -ENOMEM;
1681}
1682
Erez Shitrit515ed4f2017-04-10 11:22:26 +03001683int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1684{
Erez Shitritc1048af2017-04-10 11:22:29 +03001685 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Erez Shitrit515ed4f2017-04-10 11:22:26 +03001686 int ret = -ENOMEM;
1687
1688 priv->ca = ca;
1689 priv->port = port;
1690 priv->qp = NULL;
1691
1692 /*
1693 * the various IPoIB tasks assume they will never race against
1694 * themselves, so always use a single thread workqueue
1695 */
1696 priv->wq = alloc_ordered_workqueue("ipoib_wq", WQ_MEM_RECLAIM);
1697 if (!priv->wq) {
1698 pr_warn("%s: failed to allocate device WQ\n", dev->name);
1699 goto out;
1700 }
1701
1702 /* create pd, which used both for control and datapath*/
1703 priv->pd = ib_alloc_pd(priv->ca, 0);
1704 if (IS_ERR(priv->pd)) {
1705 pr_warn("%s: failed to allocate PD\n", ca->name);
1706 goto clean_wq;
1707 }
1708
Erez Shitritcd565b42017-04-10 11:22:30 +03001709 ret = priv->rn_ops->ndo_init(dev);
Erez Shitrit515ed4f2017-04-10 11:22:26 +03001710 if (ret) {
1711 pr_warn("%s failed to init HW resource\n", dev->name);
1712 goto out_free_pd;
1713 }
1714
Erez Shitrit515ed4f2017-04-10 11:22:26 +03001715 if (ipoib_neigh_hash_init(priv) < 0) {
1716 pr_warn("%s failed to init neigh hash\n", dev->name);
1717 goto out_dev_uninit;
1718 }
1719
1720 if (dev->flags & IFF_UP) {
1721 if (ipoib_ib_dev_open(dev)) {
1722 pr_warn("%s failed to open device\n", dev->name);
1723 ret = -ENODEV;
1724 goto out_dev_uninit;
1725 }
1726 }
1727
1728 return 0;
1729
1730out_dev_uninit:
1731 ipoib_ib_dev_cleanup(dev);
1732
1733out_free_pd:
1734 if (priv->pd) {
1735 ib_dealloc_pd(priv->pd);
1736 priv->pd = NULL;
1737 }
1738
1739clean_wq:
1740 if (priv->wq) {
1741 destroy_workqueue(priv->wq);
1742 priv->wq = NULL;
1743 }
1744
1745out:
1746 return ret;
1747}
1748
Linus Torvalds1da177e2005-04-16 15:20:36 -07001749void ipoib_dev_cleanup(struct net_device *dev)
1750{
Erez Shitritc1048af2017-04-10 11:22:29 +03001751 struct ipoib_dev_priv *priv = ipoib_priv(dev), *cpriv, *tcpriv;
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001752 LIST_HEAD(head);
1753
1754 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 /* Delete any child interfaces first */
1757 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001758 /* Stop GC on child */
1759 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1760 cancel_delayed_work(&cpriv->neigh_reap_task);
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001761 unregister_netdevice_queue(cpriv->dev, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 }
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001763 unregister_netdevice_many(&head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001765 ipoib_neigh_hash_uninit(dev);
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 ipoib_ib_dev_cleanup(dev);
1768
Erez Shitrit515ed4f2017-04-10 11:22:26 +03001769 /* no more works over the priv->wq */
1770 if (priv->wq) {
1771 flush_workqueue(priv->wq);
1772 destroy_workqueue(priv->wq);
1773 priv->wq = NULL;
1774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775}
1776
Eli Cohen9c3c5f82016-03-11 22:58:39 +02001777static int ipoib_set_vf_link_state(struct net_device *dev, int vf, int link_state)
1778{
Erez Shitritc1048af2017-04-10 11:22:29 +03001779 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Eli Cohen9c3c5f82016-03-11 22:58:39 +02001780
1781 return ib_set_vf_link_state(priv->ca, vf, priv->port, link_state);
1782}
1783
1784static int ipoib_get_vf_config(struct net_device *dev, int vf,
1785 struct ifla_vf_info *ivf)
1786{
Erez Shitritc1048af2017-04-10 11:22:29 +03001787 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Eli Cohen9c3c5f82016-03-11 22:58:39 +02001788 int err;
1789
1790 err = ib_get_vf_config(priv->ca, vf, priv->port, ivf);
1791 if (err)
1792 return err;
1793
1794 ivf->vf = vf;
1795
1796 return 0;
1797}
1798
1799static int ipoib_set_vf_guid(struct net_device *dev, int vf, u64 guid, int type)
1800{
Erez Shitritc1048af2017-04-10 11:22:29 +03001801 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Eli Cohen9c3c5f82016-03-11 22:58:39 +02001802
1803 if (type != IFLA_VF_IB_NODE_GUID && type != IFLA_VF_IB_PORT_GUID)
1804 return -EINVAL;
1805
1806 return ib_set_vf_guid(priv->ca, vf, priv->port, guid, type);
1807}
1808
1809static int ipoib_get_vf_stats(struct net_device *dev, int vf,
1810 struct ifla_vf_stats *vf_stats)
1811{
Erez Shitritc1048af2017-04-10 11:22:29 +03001812 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Eli Cohen9c3c5f82016-03-11 22:58:39 +02001813
1814 return ib_get_vf_stats(priv->ca, vf, priv->port, vf_stats);
1815}
1816
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001817static const struct header_ops ipoib_header_ops = {
1818 .create = ipoib_hard_header,
1819};
1820
Eli Cohen9c3c5f82016-03-11 22:58:39 +02001821static const struct net_device_ops ipoib_netdev_ops_pf = {
1822 .ndo_uninit = ipoib_uninit,
1823 .ndo_open = ipoib_open,
1824 .ndo_stop = ipoib_stop,
1825 .ndo_change_mtu = ipoib_change_mtu,
1826 .ndo_fix_features = ipoib_fix_features,
1827 .ndo_start_xmit = ipoib_start_xmit,
1828 .ndo_tx_timeout = ipoib_timeout,
1829 .ndo_set_rx_mode = ipoib_set_mcast_list,
1830 .ndo_get_iflink = ipoib_get_iflink,
1831 .ndo_set_vf_link_state = ipoib_set_vf_link_state,
1832 .ndo_get_vf_config = ipoib_get_vf_config,
1833 .ndo_get_vf_stats = ipoib_get_vf_stats,
1834 .ndo_set_vf_guid = ipoib_set_vf_guid,
Mark Bloch492a7e62016-05-18 16:42:43 +03001835 .ndo_set_mac_address = ipoib_set_mac,
Erez Shitritb6c871e2017-06-12 10:45:21 +03001836 .ndo_get_stats64 = ipoib_get_stats,
Eli Cohen9c3c5f82016-03-11 22:58:39 +02001837};
1838
1839static const struct net_device_ops ipoib_netdev_ops_vf = {
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001840 .ndo_uninit = ipoib_uninit,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001841 .ndo_open = ipoib_open,
1842 .ndo_stop = ipoib_stop,
1843 .ndo_change_mtu = ipoib_change_mtu,
Michał Mirosław3d96c742011-04-19 00:43:20 +00001844 .ndo_fix_features = ipoib_fix_features,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001845 .ndo_start_xmit = ipoib_start_xmit,
1846 .ndo_tx_timeout = ipoib_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001847 .ndo_set_rx_mode = ipoib_set_mcast_list,
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +02001848 .ndo_get_iflink = ipoib_get_iflink,
Feras Daoudeb547142017-07-02 15:05:59 +03001849 .ndo_get_stats64 = ipoib_get_stats,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001850};
1851
Erez Shitritcd565b42017-04-10 11:22:30 +03001852void ipoib_setup_common(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853{
Roland Dreier2337f802007-10-23 19:57:54 -07001854 dev->header_ops = &ipoib_header_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001855
Eli Cohen82c24c12008-04-16 21:09:32 -07001856 ipoib_set_ethtool_ops(dev);
1857
Roland Dreier2337f802007-10-23 19:57:54 -07001858 dev->watchdog_timeo = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859
Roland Dreier2337f802007-10-23 19:57:54 -07001860 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861
Paolo Abenifc791b62016-10-13 18:26:56 +02001862 dev->hard_header_len = IPOIB_HARD_LEN;
Roland Dreier2337f802007-10-23 19:57:54 -07001863 dev->addr_len = INFINIBAND_ALEN;
1864 dev->type = ARPHRD_INFINIBAND;
1865 dev->tx_queue_len = ipoib_sendq_size * 2;
Eli Coheneb14032f2008-01-30 18:30:46 +02001866 dev->features = (NETIF_F_VLAN_CHALLENGED |
Eli Coheneb14032f2008-01-30 18:30:46 +02001867 NETIF_F_HIGHDMA);
Eric Dumazet02875872014-10-05 18:38:35 -07001868 netif_keep_dst(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869
Linus Torvalds1da177e2005-04-16 15:20:36 -07001870 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
Erez Shitritcd565b42017-04-10 11:22:30 +03001871}
1872
1873static void ipoib_build_priv(struct net_device *dev)
1874{
1875 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877 priv->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878 spin_lock_init(&priv->lock);
Erez Shitritf47944c2013-10-16 17:37:49 +03001879 init_rwsem(&priv->vlan_rwsem);
Feras Daoudedf3f302017-07-10 18:45:41 +03001880 mutex_init(&priv->mcast_mutex);
Erez Shitrit69956d82017-08-17 15:50:50 +03001881 mutex_init(&priv->sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882
1883 INIT_LIST_HEAD(&priv->path_list);
1884 INIT_LIST_HEAD(&priv->child_intfs);
1885 INIT_LIST_HEAD(&priv->dead_ahs);
1886 INIT_LIST_HEAD(&priv->multicast_list);
1887
David Howellsc4028952006-11-22 14:57:56 +00001888 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
Yossi Etigine8224e42008-09-16 11:57:45 -07001889 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
Moni Shouaee1e2c82008-07-14 23:48:49 -07001890 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
1891 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
1892 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
David Howellsc4028952006-11-22 14:57:56 +00001893 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1894 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001895 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001896}
1897
Erez Shitritcd565b42017-04-10 11:22:30 +03001898static const struct net_device_ops ipoib_netdev_default_pf = {
1899 .ndo_init = ipoib_dev_init_default,
1900 .ndo_uninit = ipoib_dev_uninit_default,
1901 .ndo_open = ipoib_ib_dev_open_default,
1902 .ndo_stop = ipoib_ib_dev_stop_default,
1903};
1904
1905static struct net_device
1906*ipoib_create_netdev_default(struct ib_device *hca,
1907 const char *name,
1908 unsigned char name_assign_type,
1909 void (*setup)(struct net_device *))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910{
1911 struct net_device *dev;
Erez Shitritcd565b42017-04-10 11:22:30 +03001912 struct rdma_netdev *rn;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
Erez Shitritcd565b42017-04-10 11:22:30 +03001914 dev = alloc_netdev((int)sizeof(struct rdma_netdev),
1915 name,
1916 name_assign_type, setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001917 if (!dev)
1918 return NULL;
1919
Erez Shitritcd565b42017-04-10 11:22:30 +03001920 rn = netdev_priv(dev);
1921
1922 rn->send = ipoib_send;
1923 rn->attach_mcast = ipoib_mcast_attach;
1924 rn->detach_mcast = ipoib_mcast_detach;
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07001925 rn->free_rdma_netdev = free_netdev;
Erez Shitritcd565b42017-04-10 11:22:30 +03001926 rn->hca = hca;
1927
1928 dev->netdev_ops = &ipoib_netdev_default_pf;
1929
1930 return dev;
1931}
1932
1933static struct net_device *ipoib_get_netdev(struct ib_device *hca, u8 port,
1934 const char *name)
1935{
1936 struct net_device *dev;
1937
1938 if (hca->alloc_rdma_netdev) {
1939 dev = hca->alloc_rdma_netdev(hca, port,
1940 RDMA_NETDEV_IPOIB, name,
1941 NET_NAME_UNKNOWN,
1942 ipoib_setup_common);
1943 if (IS_ERR_OR_NULL(dev) && PTR_ERR(dev) != -EOPNOTSUPP)
1944 return NULL;
1945 }
1946
1947 if (!hca->alloc_rdma_netdev || PTR_ERR(dev) == -EOPNOTSUPP)
1948 dev = ipoib_create_netdev_default(hca, name, NET_NAME_UNKNOWN,
1949 ipoib_setup_common);
1950
1951 return dev;
1952}
1953
1954struct ipoib_dev_priv *ipoib_intf_alloc(struct ib_device *hca, u8 port,
1955 const char *name)
1956{
1957 struct net_device *dev;
1958 struct ipoib_dev_priv *priv;
1959 struct rdma_netdev *rn;
1960
1961 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
1962 if (!priv)
1963 return NULL;
1964
1965 dev = ipoib_get_netdev(hca, port, name);
1966 if (!dev)
1967 goto free_priv;
1968
1969 priv->rn_ops = dev->netdev_ops;
1970
1971 /* fixme : should be after the query_cap */
1972 if (priv->hca_caps & IB_DEVICE_VIRTUAL_FUNCTION)
1973 dev->netdev_ops = &ipoib_netdev_ops_vf;
1974 else
1975 dev->netdev_ops = &ipoib_netdev_ops_pf;
1976
1977 rn = netdev_priv(dev);
1978 rn->clnt_priv = priv;
1979 ipoib_build_priv(dev);
1980
1981 return priv;
1982free_priv:
1983 kfree(priv);
1984 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985}
1986
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001987static ssize_t show_pkey(struct device *dev,
1988 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
Erez Shitritc1048af2017-04-10 11:22:29 +03001990 struct net_device *ndev = to_net_dev(dev);
1991 struct ipoib_dev_priv *priv = ipoib_priv(ndev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001992
1993 return sprintf(buf, "0x%04x\n", priv->pkey);
1994}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001995static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001997static ssize_t show_umcast(struct device *dev,
1998 struct device_attribute *attr, char *buf)
1999{
Erez Shitritc1048af2017-04-10 11:22:29 +03002000 struct net_device *ndev = to_net_dev(dev);
2001 struct ipoib_dev_priv *priv = ipoib_priv(ndev);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02002002
2003 return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
2004}
2005
Or Gerlitz862096a2012-09-27 12:06:02 +00002006void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02002007{
Erez Shitritc1048af2017-04-10 11:22:29 +03002008 struct ipoib_dev_priv *priv = ipoib_priv(ndev);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02002009
2010 if (umcast_val > 0) {
2011 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
2012 ipoib_warn(priv, "ignoring multicast groups joined directly "
2013 "by userspace\n");
2014 } else
2015 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
Or Gerlitz862096a2012-09-27 12:06:02 +00002016}
2017
2018static ssize_t set_umcast(struct device *dev,
2019 struct device_attribute *attr,
2020 const char *buf, size_t count)
2021{
2022 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
2023
2024 ipoib_set_umcast(to_net_dev(dev), umcast_val);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02002025
2026 return count;
2027}
2028static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
2029
2030int ipoib_add_umcast_attr(struct net_device *dev)
2031{
2032 return device_create_file(&dev->dev, &dev_attr_umcast);
2033}
2034
Mark Bloch492a7e62016-05-18 16:42:43 +03002035static void set_base_guid(struct ipoib_dev_priv *priv, union ib_gid *gid)
2036{
2037 struct ipoib_dev_priv *child_priv;
2038 struct net_device *netdev = priv->dev;
2039
Mark Bloch9b299532016-06-04 15:15:22 +03002040 netif_addr_lock_bh(netdev);
Mark Bloch492a7e62016-05-18 16:42:43 +03002041
2042 memcpy(&priv->local_gid.global.interface_id,
2043 &gid->global.interface_id,
2044 sizeof(gid->global.interface_id));
2045 memcpy(netdev->dev_addr + 4, &priv->local_gid, sizeof(priv->local_gid));
2046 clear_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
2047
Mark Bloch9b299532016-06-04 15:15:22 +03002048 netif_addr_unlock_bh(netdev);
Mark Bloch492a7e62016-05-18 16:42:43 +03002049
2050 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
2051 down_read(&priv->vlan_rwsem);
2052 list_for_each_entry(child_priv, &priv->child_intfs, list)
2053 set_base_guid(child_priv, gid);
2054 up_read(&priv->vlan_rwsem);
2055 }
2056}
2057
2058static int ipoib_check_lladdr(struct net_device *dev,
2059 struct sockaddr_storage *ss)
2060{
2061 union ib_gid *gid = (union ib_gid *)(ss->__data + 4);
2062 int ret = 0;
2063
Mark Bloch9b299532016-06-04 15:15:22 +03002064 netif_addr_lock_bh(dev);
Mark Bloch492a7e62016-05-18 16:42:43 +03002065
2066 /* Make sure the QPN, reserved and subnet prefix match the current
2067 * lladdr, it also makes sure the lladdr is unicast.
2068 */
2069 if (memcmp(dev->dev_addr, ss->__data,
2070 4 + sizeof(gid->global.subnet_prefix)) ||
2071 gid->global.interface_id == 0)
2072 ret = -EINVAL;
2073
Mark Bloch9b299532016-06-04 15:15:22 +03002074 netif_addr_unlock_bh(dev);
Mark Bloch492a7e62016-05-18 16:42:43 +03002075
2076 return ret;
2077}
2078
2079static int ipoib_set_mac(struct net_device *dev, void *addr)
2080{
Erez Shitritc1048af2017-04-10 11:22:29 +03002081 struct ipoib_dev_priv *priv = ipoib_priv(dev);
Mark Bloch492a7e62016-05-18 16:42:43 +03002082 struct sockaddr_storage *ss = addr;
2083 int ret;
2084
2085 if (!(dev->priv_flags & IFF_LIVE_ADDR_CHANGE) && netif_running(dev))
2086 return -EBUSY;
2087
2088 ret = ipoib_check_lladdr(dev, ss);
2089 if (ret)
2090 return ret;
2091
2092 set_base_guid(priv, (union ib_gid *)(ss->__data + 4));
2093
2094 queue_work(ipoib_workqueue, &priv->flush_light);
2095
2096 return 0;
2097}
2098
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002099static ssize_t create_child(struct device *dev,
2100 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002101 const char *buf, size_t count)
2102{
2103 int pkey;
2104 int ret;
2105
2106 if (sscanf(buf, "%i", &pkey) != 1)
2107 return -EINVAL;
2108
Or Gerlitz3d790a42013-07-18 14:02:31 +03002109 if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002110 return -EINVAL;
2111
Roland Dreier4ce05932005-08-19 12:03:17 -07002112 /*
2113 * Set the full membership bit, so that we join the right
2114 * broadcast group, etc.
2115 */
2116 pkey |= 0x8000;
2117
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002118 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119
2120 return ret ? ret : count;
2121}
Or Gerlitz7a52b342010-06-06 04:59:16 +00002122static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002124static ssize_t delete_child(struct device *dev,
2125 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002126 const char *buf, size_t count)
2127{
2128 int pkey;
2129 int ret;
2130
2131 if (sscanf(buf, "%i", &pkey) != 1)
2132 return -EINVAL;
2133
2134 if (pkey < 0 || pkey > 0xffff)
2135 return -EINVAL;
2136
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002137 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002138
2139 return ret ? ret : count;
2140
2141}
Or Gerlitz7a52b342010-06-06 04:59:16 +00002142static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144int ipoib_add_pkey_attr(struct net_device *dev)
2145{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002146 return device_create_file(&dev->dev, &dev_attr_pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147}
2148
Zhu Yanjunf7534f42017-01-05 03:56:08 -05002149void ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
Or Gerlitz83bb63f2008-10-22 15:49:49 -07002150{
Or Gerlitz4a061b22015-12-18 10:59:46 +02002151 priv->hca_caps = hca->attrs.device_cap_flags;
Or Gerlitz83bb63f2008-10-22 15:49:49 -07002152
2153 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
Erez Shitritcd565b42017-04-10 11:22:30 +03002154 priv->dev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
Michał Mirosław3d96c742011-04-19 00:43:20 +00002155
2156 if (priv->hca_caps & IB_DEVICE_UD_TSO)
2157 priv->dev->hw_features |= NETIF_F_TSO;
2158
2159 priv->dev->features |= priv->dev->hw_features;
Or Gerlitz83bb63f2008-10-22 15:49:49 -07002160 }
Or Gerlitz83bb63f2008-10-22 15:49:49 -07002161}
2162
Linus Torvalds1da177e2005-04-16 15:20:36 -07002163static struct net_device *ipoib_add_port(const char *format,
2164 struct ib_device *hca, u8 port)
2165{
2166 struct ipoib_dev_priv *priv;
Shirley Mabc7b3a32008-04-23 11:55:45 -07002167 struct ib_port_attr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168 int result = -ENOMEM;
2169
Erez Shitritcd565b42017-04-10 11:22:30 +03002170 priv = ipoib_intf_alloc(hca, port, format);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 if (!priv)
2172 goto alloc_mem_failed;
2173
Bart Van Asschedb97ed02017-01-20 13:04:30 -08002174 SET_NETDEV_DEV(priv->dev, hca->dev.parent);
Eli Cohenc3aa9b12010-09-20 07:05:06 +00002175 priv->dev->dev_id = port - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176
Amir Vadai58e9cc92015-07-01 14:31:01 +03002177 result = ib_query_port(hca, port, &attr);
Leon Romanovskydc892e12017-07-13 13:34:19 +03002178 if (result) {
Shirley Mabc7b3a32008-04-23 11:55:45 -07002179 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
2180 hca->name, port);
2181 goto device_init_failed;
2182 }
2183
Leon Romanovskydc892e12017-07-13 13:34:19 +03002184 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
2185
Shirley Mabc7b3a32008-04-23 11:55:45 -07002186 /* MTU will be reset when mcast join happens */
2187 priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
2188 priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
Jarod Wilsonb3e38932016-10-20 13:55:22 -04002189 priv->dev->max_mtu = IPOIB_CM_MTU;
Shirley Mabc7b3a32008-04-23 11:55:45 -07002190
David Miller596b9b62011-07-25 00:01:25 +00002191 priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
2192
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193 result = ib_query_pkey(hca, port, 0, &priv->pkey);
2194 if (result) {
2195 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
2196 hca->name, port, result);
Eli Cohenca6de172007-08-21 18:46:10 +03002197 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002198 }
2199
Zhu Yanjunf7534f42017-01-05 03:56:08 -05002200 ipoib_set_dev_features(priv, hca);
Vladimir Sokolovskyaf40da82008-07-14 23:48:48 -07002201
Roland Dreier4ce05932005-08-19 12:03:17 -07002202 /*
2203 * Set the full membership bit, so that we join the right
2204 * broadcast group, etc.
2205 */
2206 priv->pkey |= 0x8000;
2207
Linus Torvalds1da177e2005-04-16 15:20:36 -07002208 priv->dev->broadcast[8] = priv->pkey >> 8;
2209 priv->dev->broadcast[9] = priv->pkey & 0xff;
2210
Matan Barak55ee3ab2015-10-15 18:38:45 +03002211 result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002212 if (result) {
2213 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
2214 hca->name, port, result);
Eli Cohenca6de172007-08-21 18:46:10 +03002215 goto device_init_failed;
Leon Romanovskydc892e12017-07-13 13:34:19 +03002216 }
2217
2218 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw,
2219 sizeof(union ib_gid));
Mark Bloch492a7e62016-05-18 16:42:43 +03002220 set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221
Linus Torvalds1da177e2005-04-16 15:20:36 -07002222 result = ipoib_dev_init(priv->dev, hca, port);
Leon Romanovskydc892e12017-07-13 13:34:19 +03002223 if (result) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
2225 hca->name, port, result);
2226 goto device_init_failed;
2227 }
2228
2229 INIT_IB_EVENT_HANDLER(&priv->event_handler,
2230 priv->ca, ipoib_event);
Leon Romanovskydcc98812017-08-17 15:50:36 +03002231 ib_register_event_handler(&priv->event_handler);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002232
2233 result = register_netdev(priv->dev);
2234 if (result) {
2235 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
2236 hca->name, port, result);
2237 goto register_failed;
2238 }
2239
Dan Carpenter5c8857b2017-07-13 10:45:48 +03002240 result = -ENOMEM;
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02002241 if (ipoib_cm_add_mode_attr(priv->dev))
2242 goto sysfs_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243 if (ipoib_add_pkey_attr(priv->dev))
2244 goto sysfs_failed;
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02002245 if (ipoib_add_umcast_attr(priv->dev))
2246 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002247 if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002248 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07002249 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002250 goto sysfs_failed;
2251
2252 return priv->dev;
2253
2254sysfs_failed:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002255 unregister_netdev(priv->dev);
2256
2257register_failed:
2258 ib_unregister_event_handler(&priv->event_handler);
Doug Ledford0b395782015-02-21 19:27:03 -05002259 flush_workqueue(ipoib_workqueue);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00002260 /* Stop GC if started before flush */
2261 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
2262 cancel_delayed_work(&priv->neigh_reap_task);
Doug Ledford0b395782015-02-21 19:27:03 -05002263 flush_workqueue(priv->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002264 ipoib_dev_cleanup(priv->dev);
2265
2266device_init_failed:
2267 free_netdev(priv->dev);
Alex Veskerab156af2017-06-14 09:59:05 +03002268 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
2270alloc_mem_failed:
2271 return ERR_PTR(result);
2272}
2273
2274static void ipoib_add_one(struct ib_device *device)
2275{
2276 struct list_head *dev_list;
2277 struct net_device *dev;
2278 struct ipoib_dev_priv *priv;
Hal Rosenstock41390322015-06-29 09:57:00 -04002279 int p;
Michael Wang8e37ab62015-05-05 14:50:24 +02002280 int count = 0;
Tom Tucker07ebafb2006-08-03 16:02:42 -05002281
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
2283 if (!dev_list)
2284 return;
2285
2286 INIT_LIST_HEAD(dev_list);
2287
Hal Rosenstock41390322015-06-29 09:57:00 -04002288 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
Michael Wang8e37ab62015-05-05 14:50:24 +02002289 if (!rdma_protocol_ib(device, p))
Eli Cohen7b4c8762010-09-27 17:51:11 -07002290 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002291 dev = ipoib_add_port("ib%d", device, p);
2292 if (!IS_ERR(dev)) {
Erez Shitritc1048af2017-04-10 11:22:29 +03002293 priv = ipoib_priv(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294 list_add_tail(&priv->list, dev_list);
Michael Wang8e37ab62015-05-05 14:50:24 +02002295 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296 }
2297 }
2298
Michael Wang8e37ab62015-05-05 14:50:24 +02002299 if (!count) {
2300 kfree(dev_list);
2301 return;
2302 }
2303
Linus Torvalds1da177e2005-04-16 15:20:36 -07002304 ib_set_client_data(device, &ipoib_client, dev_list);
2305}
2306
Haggai Eran7c1eb452015-07-30 17:50:14 +03002307static void ipoib_remove_one(struct ib_device *device, void *client_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002308{
Alex Veskerab156af2017-06-14 09:59:05 +03002309 struct ipoib_dev_priv *priv, *tmp, *cpriv, *tcpriv;
Haggai Eran7c1eb452015-07-30 17:50:14 +03002310 struct list_head *dev_list = client_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311
Itai Garbi5a2815f2013-02-19 15:40:24 +00002312 if (!dev_list)
2313 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314
2315 list_for_each_entry_safe(priv, tmp, dev_list, list) {
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07002316 struct rdma_netdev *rn = netdev_priv(priv->dev);
2317
Linus Torvalds1da177e2005-04-16 15:20:36 -07002318 ib_unregister_event_handler(&priv->event_handler);
Doug Ledford0b395782015-02-21 19:27:03 -05002319 flush_workqueue(ipoib_workqueue);
Roland Dreiera77a57a2008-08-19 15:01:32 -07002320
Erez Shitrit198b12f2016-06-04 15:15:20 +03002321 /* mark interface in the middle of destruction */
2322 set_bit(IPOIB_FLAG_GOING_DOWN, &priv->flags);
2323
Roland Dreiera77a57a2008-08-19 15:01:32 -07002324 rtnl_lock();
2325 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
2326 rtnl_unlock();
2327
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00002328 /* Stop GC */
2329 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
2330 cancel_delayed_work(&priv->neigh_reap_task);
Doug Ledford0b395782015-02-21 19:27:03 -05002331 flush_workqueue(priv->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332
Erez Shitrit69956d82017-08-17 15:50:50 +03002333 /* Wrap rtnl_lock/unlock with mutex to protect sysfs calls */
2334 mutex_lock(&priv->sysfs_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335 unregister_netdev(priv->dev);
Erez Shitrit69956d82017-08-17 15:50:50 +03002336 mutex_unlock(&priv->sysfs_mutex);
2337
Niranjana Vishwanathapura8e959602017-06-30 13:14:46 -07002338 rn->free_rdma_netdev(priv->dev);
Alex Veskerab156af2017-06-14 09:59:05 +03002339
2340 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list)
2341 kfree(cpriv);
2342
Erez Shitritcd565b42017-04-10 11:22:30 +03002343 kfree(priv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344 }
Michael S. Tsirkin06c56e42005-09-01 09:19:02 -07002345
2346 kfree(dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347}
2348
Shamir Rabinovitch771a5252017-03-29 06:21:59 -04002349#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2350static struct notifier_block ipoib_netdev_notifier = {
2351 .notifier_call = ipoib_netdev_event,
2352};
2353#endif
2354
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355static int __init ipoib_init_module(void)
2356{
2357 int ret;
2358
Shirley Ma0f485252006-04-10 09:43:58 -07002359 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
2360 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
2361 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
2362
2363 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
2364 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -07002365 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08002366#ifdef CONFIG_INFINIBAND_IPOIB_CM
2367 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
Alex Vesker11f74b42017-07-13 11:27:12 +03002368 ipoib_max_conn_qp = max(ipoib_max_conn_qp, 0);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08002369#endif
Shirley Ma0f485252006-04-10 09:43:58 -07002370
Eli Cohenf89271da2008-07-14 23:48:44 -07002371 /*
2372 * When copying small received packets, we only copy from the
2373 * linear data part of the SKB, so we rely on this condition.
2374 */
2375 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
2376
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377 ret = ipoib_register_debugfs();
2378 if (ret)
2379 return ret;
2380
2381 /*
Doug Ledford0b395782015-02-21 19:27:03 -05002382 * We create a global workqueue here that is used for all flush
2383 * operations. However, if you attempt to flush a workqueue
2384 * from a task on that same workqueue, it deadlocks the system.
2385 * We want to be able to flush the tasks associated with a
2386 * specific net device, so we also create a workqueue for each
2387 * netdevice. We queue up the tasks for that device only on
2388 * its private workqueue, and we only queue up flush events
2389 * on our global flush workqueue. This avoids the deadlocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 */
Bhaktipriya Shridhar855cda62016-08-15 23:44:26 +05302391 ipoib_workqueue = alloc_ordered_workqueue("ipoib_flush",
2392 WQ_MEM_RECLAIM);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 if (!ipoib_workqueue) {
2394 ret = -ENOMEM;
2395 goto err_fs;
2396 }
2397
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002398 ib_sa_register_client(&ipoib_sa_client);
2399
Linus Torvalds1da177e2005-04-16 15:20:36 -07002400 ret = ib_register_client(&ipoib_client);
2401 if (ret)
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002402 goto err_sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002403
Or Gerlitz9baa0b02012-09-13 05:56:36 +00002404 ret = ipoib_netlink_init();
2405 if (ret)
2406 goto err_client;
2407
Shamir Rabinovitch771a5252017-03-29 06:21:59 -04002408#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2409 register_netdevice_notifier(&ipoib_netdev_notifier);
2410#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -07002411 return 0;
2412
Or Gerlitz9baa0b02012-09-13 05:56:36 +00002413err_client:
2414 ib_unregister_client(&ipoib_client);
2415
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002416err_sa:
2417 ib_sa_unregister_client(&ipoib_sa_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418 destroy_workqueue(ipoib_workqueue);
2419
Roland Dreier9adec1a2005-04-16 15:26:07 -07002420err_fs:
2421 ipoib_unregister_debugfs();
2422
Linus Torvalds1da177e2005-04-16 15:20:36 -07002423 return ret;
2424}
2425
2426static void __exit ipoib_cleanup_module(void)
2427{
Shamir Rabinovitch771a5252017-03-29 06:21:59 -04002428#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2429 unregister_netdevice_notifier(&ipoib_netdev_notifier);
2430#endif
Or Gerlitz9baa0b02012-09-13 05:56:36 +00002431 ipoib_netlink_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002432 ib_unregister_client(&ipoib_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002433 ib_sa_unregister_client(&ipoib_sa_client);
Roland Dreier9adec1a2005-04-16 15:26:07 -07002434 ipoib_unregister_debugfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002435 destroy_workqueue(ipoib_workqueue);
2436}
2437
2438module_init(ipoib_init_module);
2439module_exit(ipoib_cleanup_module);