blob: f74316e679d2fc2b7b27212d47fc806e95844f01 [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>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020054
Yan Burman4b486802013-02-19 15:40:23 +000055#define DRV_VERSION "1.0.0"
56
57const char ipoib_driver_version[] = DRV_VERSION;
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059MODULE_AUTHOR("Roland Dreier");
60MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
61MODULE_LICENSE("Dual BSD/GPL");
Yan Burman4b486802013-02-19 15:40:23 +000062MODULE_VERSION(DRV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102static struct ib_client ipoib_client = {
103 .name = "ipoib",
104 .add = ipoib_add_one,
Guy Shapiroddde8962015-07-30 17:50:16 +0300105 .remove = ipoib_remove_one,
106 .get_net_dev_by_params = ipoib_get_net_dev_by_params,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107};
108
109int ipoib_open(struct net_device *dev)
110{
111 struct ipoib_dev_priv *priv = netdev_priv(dev);
112
113 ipoib_dbg(priv, "bringing up interface\n");
114
Michal Schmidt437708c2014-01-17 19:47:25 +0100115 netif_carrier_off(dev);
116
Yossi Etigine028cc52009-04-20 13:58:08 -0700117 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500119 if (ipoib_ib_dev_open(dev)) {
Alex Estrindd57c932014-08-06 14:40:32 -0400120 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
121 return 0;
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800122 goto err_disable;
Alex Estrindd57c932014-08-06 14:40:32 -0400123 }
Yossi Etiginfe25c562008-11-12 10:24:36 -0800124
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800125 if (ipoib_ib_dev_up(dev))
126 goto err_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127
128 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
129 struct ipoib_dev_priv *cpriv;
130
131 /* Bring up any child interfaces too */
Erez Shitritf47944c2013-10-16 17:37:49 +0300132 down_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 list_for_each_entry(cpriv, &priv->child_intfs, list) {
134 int flags;
135
136 flags = cpriv->dev->flags;
137 if (flags & IFF_UP)
138 continue;
139
140 dev_change_flags(cpriv->dev, flags | IFF_UP);
141 }
Erez Shitritf47944c2013-10-16 17:37:49 +0300142 up_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 }
144
145 netif_start_queue(dev);
146
147 return 0;
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800148
149err_stop:
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500150 ipoib_ib_dev_stop(dev);
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800151
152err_disable:
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800153 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
154
155 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158static int ipoib_stop(struct net_device *dev)
159{
160 struct ipoib_dev_priv *priv = netdev_priv(dev);
161
162 ipoib_dbg(priv, "stopping interface\n");
163
164 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
165
166 netif_stop_queue(dev);
167
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500168 ipoib_ib_dev_down(dev);
169 ipoib_ib_dev_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170
171 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
172 struct ipoib_dev_priv *cpriv;
173
174 /* Bring down any child interfaces too */
Erez Shitritf47944c2013-10-16 17:37:49 +0300175 down_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 list_for_each_entry(cpriv, &priv->child_intfs, list) {
177 int flags;
178
179 flags = cpriv->dev->flags;
180 if (!(flags & IFF_UP))
181 continue;
182
183 dev_change_flags(cpriv->dev, flags & ~IFF_UP);
184 }
Erez Shitritf47944c2013-10-16 17:37:49 +0300185 up_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 }
187
188 return 0;
189}
190
Or Gerlitz9baa0b02012-09-13 05:56:36 +0000191static void ipoib_uninit(struct net_device *dev)
192{
193 ipoib_dev_cleanup(dev);
194}
195
David S. Miller9ca36f72011-11-16 18:05:50 -0500196static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
Michał Mirosław3d96c742011-04-19 00:43:20 +0000197{
198 struct ipoib_dev_priv *priv = netdev_priv(dev);
199
200 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
Yuval Shaiac4268772015-07-12 01:24:09 -0700201 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
Michał Mirosław3d96c742011-04-19 00:43:20 +0000202
203 return features;
204}
205
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
207{
208 struct ipoib_dev_priv *priv = netdev_priv(dev);
209
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200210 /* dev->mtu > 2K ==> connected mode */
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800211 if (ipoib_cm_admin_enabled(dev)) {
212 if (new_mtu > ipoib_cm_max_mtu(dev))
213 return -EINVAL;
214
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200215 if (new_mtu > priv->mcast_mtu)
216 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
217 priv->mcast_mtu);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800218
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200219 dev->mtu = new_mtu;
220 return 0;
221 }
222
Shirley Mabc7b3a32008-04-23 11:55:45 -0700223 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 return -EINVAL;
225
226 priv->admin_mtu = new_mtu;
227
228 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
229
230 return 0;
231}
232
Guy Shapiroddde8962015-07-30 17:50:16 +0300233/* Called with an RCU read lock taken */
234static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr,
235 struct net_device *dev)
236{
237 struct net *net = dev_net(dev);
238 struct in_device *in_dev;
239 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
240 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
241 __be32 ret_addr;
242
243 switch (addr->sa_family) {
244 case AF_INET:
245 in_dev = in_dev_get(dev);
246 if (!in_dev)
247 return false;
248
249 ret_addr = inet_confirm_addr(net, in_dev, 0,
250 addr_in->sin_addr.s_addr,
251 RT_SCOPE_HOST);
252 in_dev_put(in_dev);
253 if (ret_addr)
254 return true;
255
256 break;
257 case AF_INET6:
258 if (IS_ENABLED(CONFIG_IPV6) &&
259 ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1))
260 return true;
261
262 break;
263 }
264 return false;
265}
266
267/**
268 * Find the master net_device on top of the given net_device.
269 * @dev: base IPoIB net_device
270 *
271 * Returns the master net_device with a reference held, or the same net_device
272 * if no master exists.
273 */
274static struct net_device *ipoib_get_master_net_dev(struct net_device *dev)
275{
276 struct net_device *master;
277
278 rcu_read_lock();
279 master = netdev_master_upper_dev_get_rcu(dev);
280 if (master)
281 dev_hold(master);
282 rcu_read_unlock();
283
284 if (master)
285 return master;
286
287 dev_hold(dev);
288 return dev;
289}
290
291/**
292 * Find a net_device matching the given address, which is an upper device of
293 * the given net_device.
294 * @addr: IP address to look for.
295 * @dev: base IPoIB net_device
296 *
297 * If found, returns the net_device with a reference held. Otherwise return
298 * NULL.
299 */
300static struct net_device *ipoib_get_net_dev_match_addr(
301 const struct sockaddr *addr, struct net_device *dev)
302{
303 struct net_device *upper,
304 *result = NULL;
305 struct list_head *iter;
306
307 rcu_read_lock();
308 if (ipoib_is_dev_match_addr_rcu(addr, dev)) {
309 dev_hold(dev);
310 result = dev;
311 goto out;
312 }
313
314 netdev_for_each_all_upper_dev_rcu(dev, upper, iter) {
315 if (ipoib_is_dev_match_addr_rcu(addr, upper)) {
316 dev_hold(upper);
317 result = upper;
318 break;
319 }
320 }
321out:
322 rcu_read_unlock();
323 return result;
324}
325
326/* returns the number of IPoIB netdevs on top a given ipoib device matching a
327 * pkey_index and address, if one exists.
328 *
329 * @found_net_dev: contains a matching net_device if the return value >= 1,
330 * with a reference held. */
331static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
332 const union ib_gid *gid,
333 u16 pkey_index,
334 const struct sockaddr *addr,
335 int nesting,
336 struct net_device **found_net_dev)
337{
338 struct ipoib_dev_priv *child_priv;
339 struct net_device *net_dev = NULL;
340 int matches = 0;
341
342 if (priv->pkey_index == pkey_index &&
343 (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
344 if (!addr) {
345 net_dev = ipoib_get_master_net_dev(priv->dev);
346 } else {
347 /* Verify the net_device matches the IP address, as
348 * IPoIB child devices currently share a GID. */
349 net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev);
350 }
351 if (net_dev) {
352 if (!*found_net_dev)
353 *found_net_dev = net_dev;
354 else
355 dev_put(net_dev);
356 ++matches;
357 }
358 }
359
360 /* Check child interfaces */
361 down_read_nested(&priv->vlan_rwsem, nesting);
362 list_for_each_entry(child_priv, &priv->child_intfs, list) {
363 matches += ipoib_match_gid_pkey_addr(child_priv, gid,
364 pkey_index, addr,
365 nesting + 1,
366 found_net_dev);
367 if (matches > 1)
368 break;
369 }
370 up_read(&priv->vlan_rwsem);
371
372 return matches;
373}
374
375/* Returns the number of matching net_devs found (between 0 and 2). Also
376 * return the matching net_device in the @net_dev parameter, holding a
377 * reference to the net_device, if the number of matches >= 1 */
378static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port,
379 u16 pkey_index,
380 const union ib_gid *gid,
381 const struct sockaddr *addr,
382 struct net_device **net_dev)
383{
384 struct ipoib_dev_priv *priv;
385 int matches = 0;
386
387 *net_dev = NULL;
388
389 list_for_each_entry(priv, dev_list, list) {
390 if (priv->port != port)
391 continue;
392
393 matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
394 addr, 0, net_dev);
395 if (matches > 1)
396 break;
397 }
398
399 return matches;
400}
401
402static struct net_device *ipoib_get_net_dev_by_params(
403 struct ib_device *dev, u8 port, u16 pkey,
404 const union ib_gid *gid, const struct sockaddr *addr,
405 void *client_data)
406{
407 struct net_device *net_dev;
408 struct list_head *dev_list = client_data;
409 u16 pkey_index;
410 int matches;
411 int ret;
412
413 if (!rdma_protocol_ib(dev, port))
414 return NULL;
415
416 ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
417 if (ret)
418 return NULL;
419
420 if (!dev_list)
421 return NULL;
422
423 /* See if we can find a unique device matching the L2 parameters */
424 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
425 gid, NULL, &net_dev);
426
427 switch (matches) {
428 case 0:
429 return NULL;
430 case 1:
431 return net_dev;
432 }
433
434 dev_put(net_dev);
435
436 /* Couldn't find a unique device with L2 parameters only. Use L3
437 * address to uniquely match the net device */
438 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
439 gid, addr, &net_dev);
440 switch (matches) {
441 case 0:
442 return NULL;
443 default:
444 dev_warn_ratelimited(&dev->dev,
445 "duplicate IP address detected\n");
446 /* Fall through */
447 case 1:
448 return net_dev;
449 }
450}
451
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700452int ipoib_set_mode(struct net_device *dev, const char *buf)
453{
454 struct ipoib_dev_priv *priv = netdev_priv(dev);
455
456 /* flush paths if we switch modes so that connections are restarted */
457 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
458 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
459 ipoib_warn(priv, "enabling connected mode "
460 "will cause multicast packet drops\n");
461 netdev_update_features(dev);
Erez Shitritedcd2a72015-06-07 13:36:11 +0300462 dev_set_mtu(dev, ipoib_cm_max_mtu(dev));
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700463 rtnl_unlock();
464 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
465
466 ipoib_flush_paths(dev);
467 rtnl_lock();
468 return 0;
469 }
470
471 if (!strcmp(buf, "datagram\n")) {
472 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
473 netdev_update_features(dev);
474 dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
475 rtnl_unlock();
476 ipoib_flush_paths(dev);
477 rtnl_lock();
478 return 0;
479 }
480
481 return -EINVAL;
482}
483
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300484static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485{
486 struct ipoib_dev_priv *priv = netdev_priv(dev);
487 struct rb_node *n = priv->path_tree.rb_node;
488 struct ipoib_path *path;
489 int ret;
490
491 while (n) {
492 path = rb_entry(n, struct ipoib_path, rb_node);
493
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300494 ret = memcmp(gid, path->pathrec.dgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 sizeof (union ib_gid));
496
497 if (ret < 0)
498 n = n->rb_left;
499 else if (ret > 0)
500 n = n->rb_right;
501 else
502 return path;
503 }
504
505 return NULL;
506}
507
508static int __path_add(struct net_device *dev, struct ipoib_path *path)
509{
510 struct ipoib_dev_priv *priv = netdev_priv(dev);
511 struct rb_node **n = &priv->path_tree.rb_node;
512 struct rb_node *pn = NULL;
513 struct ipoib_path *tpath;
514 int ret;
515
516 while (*n) {
517 pn = *n;
518 tpath = rb_entry(pn, struct ipoib_path, rb_node);
519
520 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
521 sizeof (union ib_gid));
522 if (ret < 0)
523 n = &pn->rb_left;
524 else if (ret > 0)
525 n = &pn->rb_right;
526 else
527 return -EEXIST;
528 }
529
530 rb_link_node(&path->rb_node, pn, n);
531 rb_insert_color(&path->rb_node, &priv->path_tree);
532
533 list_add_tail(&path->list, &priv->path_list);
534
535 return 0;
536}
537
538static void path_free(struct net_device *dev, struct ipoib_path *path)
539{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
542 while ((skb = __skb_dequeue(&path->queue)))
543 dev_kfree_skb_irq(skb);
544
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000545 ipoib_dbg(netdev_priv(dev), "path_free\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000547 /* remove all neigh connected to this path */
548 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 if (path->ah)
551 ipoib_put_ah(path->ah);
552
553 kfree(path);
554}
555
Roland Dreier1732b0e2005-11-07 10:33:11 -0800556#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
557
558struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
559{
560 struct ipoib_path_iter *iter;
561
562 iter = kmalloc(sizeof *iter, GFP_KERNEL);
563 if (!iter)
564 return NULL;
565
566 iter->dev = dev;
567 memset(iter->path.pathrec.dgid.raw, 0, 16);
568
569 if (ipoib_path_iter_next(iter)) {
570 kfree(iter);
571 return NULL;
572 }
573
574 return iter;
575}
576
577int ipoib_path_iter_next(struct ipoib_path_iter *iter)
578{
579 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
580 struct rb_node *n;
581 struct ipoib_path *path;
582 int ret = 1;
583
584 spin_lock_irq(&priv->lock);
585
586 n = rb_first(&priv->path_tree);
587
588 while (n) {
589 path = rb_entry(n, struct ipoib_path, rb_node);
590
591 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
592 sizeof (union ib_gid)) < 0) {
593 iter->path = *path;
594 ret = 0;
595 break;
596 }
597
598 n = rb_next(n);
599 }
600
601 spin_unlock_irq(&priv->lock);
602
603 return ret;
604}
605
606void ipoib_path_iter_read(struct ipoib_path_iter *iter,
607 struct ipoib_path *path)
608{
609 *path = iter->path;
610}
611
612#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
613
Moni Shouaee1e2c82008-07-14 23:48:49 -0700614void ipoib_mark_paths_invalid(struct net_device *dev)
615{
616 struct ipoib_dev_priv *priv = netdev_priv(dev);
617 struct ipoib_path *path, *tp;
618
619 spin_lock_irq(&priv->lock);
620
621 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700622 ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
Moni Shouaee1e2c82008-07-14 23:48:49 -0700623 be16_to_cpu(path->pathrec.dlid),
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700624 path->pathrec.dgid.raw);
Moni Shouaee1e2c82008-07-14 23:48:49 -0700625 path->valid = 0;
626 }
627
628 spin_unlock_irq(&priv->lock);
629}
630
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631void ipoib_flush_paths(struct net_device *dev)
632{
633 struct ipoib_dev_priv *priv = netdev_priv(dev);
634 struct ipoib_path *path, *tp;
635 LIST_HEAD(remove_list);
Roland Dreier943c2462008-09-30 10:36:21 -0700636 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Roland Dreier943c2462008-09-30 10:36:21 -0700638 netif_tx_lock_bh(dev);
639 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Robert P. J. Day157de222008-04-16 21:09:26 -0700641 list_splice_init(&priv->path_list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642
643 list_for_each_entry(path, &remove_list, list)
644 rb_erase(&path->rb_node, &priv->path_tree);
645
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 list_for_each_entry_safe(path, tp, &remove_list, list) {
647 if (path->query)
648 ib_sa_cancel_query(path->query_id, path->query);
Roland Dreier943c2462008-09-30 10:36:21 -0700649 spin_unlock_irqrestore(&priv->lock, flags);
650 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 wait_for_completion(&path->done);
652 path_free(dev, path);
Roland Dreier943c2462008-09-30 10:36:21 -0700653 netif_tx_lock_bh(dev);
654 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 }
Roland Dreier943c2462008-09-30 10:36:21 -0700656
657 spin_unlock_irqrestore(&priv->lock, flags);
658 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661static void path_rec_completion(int status,
662 struct ib_sa_path_rec *pathrec,
663 void *path_ptr)
664{
665 struct ipoib_path *path = path_ptr;
666 struct net_device *dev = path->dev;
667 struct ipoib_dev_priv *priv = netdev_priv(dev);
668 struct ipoib_ah *ah = NULL;
Roland Dreierc9da4ba2008-09-25 15:26:15 -0700669 struct ipoib_ah *old_ah = NULL;
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700670 struct ipoib_neigh *neigh, *tn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 struct sk_buff_head skqueue;
672 struct sk_buff *skb;
673 unsigned long flags;
674
Roland Dreier843613b2007-02-26 12:57:08 -0800675 if (!status)
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700676 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700677 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 else
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700679 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700680 status, path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682 skb_queue_head_init(&skqueue);
683
684 if (!status) {
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700685 struct ib_ah_attr av;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700687 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
688 ah = ipoib_create_ah(dev, priv->pd, &av);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 }
690
691 spin_lock_irqsave(&priv->lock, flags);
692
Mike Marciniszyn38743972011-11-21 08:43:54 -0500693 if (!IS_ERR_OR_NULL(ah)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 path->pathrec = *pathrec;
695
Roland Dreierc9da4ba2008-09-25 15:26:15 -0700696 old_ah = path->ah;
697 path->ah = ah;
698
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
700 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
701
702 while ((skb = __skb_dequeue(&path->queue)))
703 __skb_queue_tail(&skqueue, skb);
704
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700705 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
Moni Shouaee1e2c82008-07-14 23:48:49 -0700706 if (neigh->ah) {
707 WARN_ON(neigh->ah != old_ah);
708 /*
709 * Dropping the ah reference inside
710 * priv->lock is safe here, because we
711 * will hold one more reference from
712 * the original value of path->ah (ie
713 * old_ah).
714 */
715 ipoib_put_ah(neigh->ah);
716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 kref_get(&path->ah->ref);
718 neigh->ah = path->ah;
719
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000720 if (ipoib_cm_enabled(dev, neigh->daddr)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200721 if (!ipoib_cm_get(neigh))
722 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
723 path,
724 neigh));
725 if (!ipoib_cm_get(neigh)) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000726 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200727 continue;
728 }
729 }
730
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 while ((skb = __skb_dequeue(&neigh->queue)))
732 __skb_queue_tail(&skqueue, skb);
733 }
Moni Shouaee1e2c82008-07-14 23:48:49 -0700734 path->valid = 1;
Roland Dreier5872a9f2005-11-29 10:13:54 -0800735 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736
Roland Dreier5872a9f2005-11-29 10:13:54 -0800737 path->query = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 complete(&path->done);
739
740 spin_unlock_irqrestore(&priv->lock, flags);
741
Roland Dreierf72dd562013-02-25 09:42:15 -0800742 if (IS_ERR_OR_NULL(ah))
743 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
744
Moni Shouaee1e2c82008-07-14 23:48:49 -0700745 if (old_ah)
746 ipoib_put_ah(old_ah);
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 while ((skb = __skb_dequeue(&skqueue))) {
749 skb->dev = dev;
750 if (dev_queue_xmit(skb))
751 ipoib_warn(priv, "dev_queue_xmit failed "
752 "to requeue packet\n");
753 }
754}
755
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300756static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 struct ipoib_dev_priv *priv = netdev_priv(dev);
759 struct ipoib_path *path;
760
Jack Morgenstein1401b532007-11-26 10:41:19 +0200761 if (!priv->broadcast)
762 return NULL;
763
Roland Dreier21a38482005-11-02 10:07:59 -0800764 path = kzalloc(sizeof *path, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765 if (!path)
766 return NULL;
767
Roland Dreier21a38482005-11-02 10:07:59 -0800768 path->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770 skb_queue_head_init(&path->queue);
771
772 INIT_LIST_HEAD(&path->neigh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300774 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
Roland Dreier2337f802007-10-23 19:57:54 -0700775 path->pathrec.sgid = priv->local_gid;
776 path->pathrec.pkey = cpu_to_be16(priv->pkey);
Sean Hefty81668832007-08-02 12:21:31 -0700777 path->pathrec.numb_path = 1;
778 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700779
780 return path;
781}
782
783static int path_rec_start(struct net_device *dev,
784 struct ipoib_path *path)
785{
786 struct ipoib_dev_priv *priv = netdev_priv(dev);
787
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700788 ipoib_dbg(priv, "Start path record lookup for %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700789 path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Roland Dreier65c7edd2005-11-28 21:20:34 -0800791 init_completion(&path->done);
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 path->query_id =
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700794 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 &path->pathrec,
796 IB_SA_PATH_REC_DGID |
797 IB_SA_PATH_REC_SGID |
798 IB_SA_PATH_REC_NUMB_PATH |
Sean Hefty81668832007-08-02 12:21:31 -0700799 IB_SA_PATH_REC_TRAFFIC_CLASS |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 IB_SA_PATH_REC_PKEY,
801 1000, GFP_ATOMIC,
802 path_rec_completion,
803 path, &path->query);
804 if (path->query_id < 0) {
Or Gerlitz01b3fc82008-07-22 14:18:34 -0700805 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806 path->query = NULL;
Yossi Etigin93a3ab92008-11-12 10:24:38 -0800807 complete(&path->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 return path->query_id;
809 }
810
811 return 0;
812}
813
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000814static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
815 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816{
817 struct ipoib_dev_priv *priv = netdev_priv(dev);
818 struct ipoib_path *path;
819 struct ipoib_neigh *neigh;
Roland Dreier943c2462008-09-30 10:36:21 -0700820 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000822 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000823 neigh = ipoib_neigh_alloc(daddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700824 if (!neigh) {
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000825 spin_unlock_irqrestore(&priv->lock, flags);
Roland Dreierde903512007-09-28 15:33:51 -0700826 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827 dev_kfree_skb_any(skb);
828 return;
829 }
830
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000831 path = __path_find(dev, daddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700832 if (!path) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000833 path = path_rec_create(dev, daddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 if (!path)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300835 goto err_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836
837 __path_add(dev, path);
838 }
839
840 list_add_tail(&neigh->list, &path->neigh_list);
841
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -0800842 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 kref_get(&path->ah->ref);
844 neigh->ah = path->ah;
845
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000846 if (ipoib_cm_enabled(dev, neigh->daddr)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200847 if (!ipoib_cm_get(neigh))
848 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
849 if (!ipoib_cm_get(neigh)) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000850 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200851 goto err_drop;
852 }
853 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
854 __skb_queue_tail(&neigh->queue, skb);
855 else {
856 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
857 skb_queue_len(&neigh->queue));
858 goto err_drop;
859 }
Roland Dreier721d67c2009-09-05 20:23:40 -0700860 } else {
861 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000862 ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
863 ipoib_neigh_put(neigh);
Roland Dreier721d67c2009-09-05 20:23:40 -0700864 return;
865 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866 } else {
867 neigh->ah = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868
869 if (!path->query && path_rec_start(dev, path))
Jim Foraker49b8e742013-08-08 17:44:22 -0700870 goto err_path;
Erez Shitrit1e85b802015-04-02 13:39:03 +0300871 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
872 __skb_queue_tail(&neigh->queue, skb);
873 else
874 goto err_drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875 }
876
Roland Dreier943c2462008-09-30 10:36:21 -0700877 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000878 ipoib_neigh_put(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879 return;
880
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300881err_path:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000882 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200883err_drop:
Roland Dreierde903512007-09-28 15:33:51 -0700884 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 dev_kfree_skb_any(skb);
886
Roland Dreier943c2462008-09-30 10:36:21 -0700887 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000888 ipoib_neigh_put(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889}
890
891static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
Roland Dreier936d7de2012-02-07 14:51:21 +0000892 struct ipoib_cb *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700893{
894 struct ipoib_dev_priv *priv = netdev_priv(dev);
895 struct ipoib_path *path;
Roland Dreier943c2462008-09-30 10:36:21 -0700896 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
Roland Dreier943c2462008-09-30 10:36:21 -0700898 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899
Roland Dreier936d7de2012-02-07 14:51:21 +0000900 path = __path_find(dev, cb->hwaddr + 4);
Moni Shouaee1e2c82008-07-14 23:48:49 -0700901 if (!path || !path->valid) {
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800902 int new_path = 0;
903
904 if (!path) {
Roland Dreier936d7de2012-02-07 14:51:21 +0000905 path = path_rec_create(dev, cb->hwaddr + 4);
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800906 new_path = 1;
907 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700908 if (path) {
Erez Shitrit1e85b802015-04-02 13:39:03 +0300909 if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
910 __skb_queue_tail(&path->queue, skb);
911 } else {
912 ++dev->stats.tx_dropped;
913 dev_kfree_skb_any(skb);
914 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700915
Yossi Etiginff79ae82008-11-12 10:24:39 -0800916 if (!path->query && path_rec_start(dev, path)) {
Roland Dreier943c2462008-09-30 10:36:21 -0700917 spin_unlock_irqrestore(&priv->lock, flags);
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800918 if (new_path)
919 path_free(dev, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920 return;
921 } else
922 __path_add(dev, path);
923 } else {
Roland Dreierde903512007-09-28 15:33:51 -0700924 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700925 dev_kfree_skb_any(skb);
926 }
927
Roland Dreier943c2462008-09-30 10:36:21 -0700928 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700929 return;
930 }
931
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -0800932 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
934 be16_to_cpu(path->pathrec.dlid));
935
Roland Dreier721d67c2009-09-05 20:23:40 -0700936 spin_unlock_irqrestore(&priv->lock, flags);
Roland Dreier936d7de2012-02-07 14:51:21 +0000937 ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr));
Roland Dreier721d67c2009-09-05 20:23:40 -0700938 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 } else if ((path->query || !path_rec_start(dev, path)) &&
940 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700941 __skb_queue_tail(&path->queue, skb);
942 } else {
Roland Dreierde903512007-09-28 15:33:51 -0700943 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 dev_kfree_skb_any(skb);
945 }
946
Roland Dreier943c2462008-09-30 10:36:21 -0700947 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948}
949
950static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
951{
952 struct ipoib_dev_priv *priv = netdev_priv(dev);
953 struct ipoib_neigh *neigh;
Eric Dumazetb49fe362014-09-18 11:00:27 -0700954 struct ipoib_cb *cb = ipoib_skb_cb(skb);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000955 struct ipoib_header *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700956 unsigned long flags;
957
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000958 header = (struct ipoib_header *) skb->data;
959
960 if (unlikely(cb->hwaddr[4] == 0xff)) {
961 /* multicast, arrange "if" according to probability */
962 if ((header->proto != htons(ETH_P_IP)) &&
963 (header->proto != htons(ETH_P_IPV6)) &&
964 (header->proto != htons(ETH_P_ARP)) &&
Patrick McHardydc850b02013-04-17 06:18:29 +0000965 (header->proto != htons(ETH_P_RARP)) &&
966 (header->proto != htons(ETH_P_TIPC))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000967 /* ethertype not supported by IPoIB */
David Miller17e6abe2011-12-02 16:52:44 +0000968 ++dev->stats.tx_dropped;
969 dev_kfree_skb_any(skb);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000970 return NETDEV_TX_OK;
David Miller17e6abe2011-12-02 16:52:44 +0000971 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000972 /* Add in the P_Key for multicast*/
973 cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
974 cb->hwaddr[9] = priv->pkey & 0xff;
975
976 neigh = ipoib_neigh_get(dev, cb->hwaddr);
977 if (likely(neigh))
978 goto send_using_neigh;
979 ipoib_mcast_send(dev, cb->hwaddr, skb);
980 return NETDEV_TX_OK;
David Miller17e6abe2011-12-02 16:52:44 +0000981 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000983 /* unicast, arrange "switch" according to probability */
984 switch (header->proto) {
985 case htons(ETH_P_IP):
986 case htons(ETH_P_IPV6):
Patrick McHardydc850b02013-04-17 06:18:29 +0000987 case htons(ETH_P_TIPC):
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000988 neigh = ipoib_neigh_get(dev, cb->hwaddr);
989 if (unlikely(!neigh)) {
990 neigh_add_path(skb, cb->hwaddr, dev);
991 return NETDEV_TX_OK;
Yossi Etigina50df392009-01-09 14:05:11 -0800992 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000993 break;
994 case htons(ETH_P_ARP):
995 case htons(ETH_P_RARP):
996 /* for unicast ARP and RARP should always perform path find */
997 unicast_arp_send(skb, dev, cb);
998 return NETDEV_TX_OK;
999 default:
1000 /* ethertype not supported by IPoIB */
1001 ++dev->stats.tx_dropped;
1002 dev_kfree_skb_any(skb);
1003 return NETDEV_TX_OK;
1004 }
Michael S. Tsirkin8a7f7522006-07-19 17:44:37 +03001005
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001006send_using_neigh:
1007 /* note we now hold a ref to neigh */
1008 if (ipoib_cm_get(neigh)) {
1009 if (ipoib_cm_up(neigh)) {
1010 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
1011 goto unref;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001012 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001013 } else if (neigh->ah) {
1014 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
1015 goto unref;
1016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001018 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1019 spin_lock_irqsave(&priv->lock, flags);
1020 __skb_queue_tail(&neigh->queue, skb);
1021 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 } else {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001023 ++dev->stats.tx_dropped;
1024 dev_kfree_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001026
1027unref:
1028 ipoib_neigh_put(neigh);
1029
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030 return NETDEV_TX_OK;
1031}
1032
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033static void ipoib_timeout(struct net_device *dev)
1034{
1035 struct ipoib_dev_priv *priv = netdev_priv(dev);
1036
Roland Dreier4b2d3192005-10-18 12:20:06 -07001037 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
1038 jiffies_to_msecs(jiffies - dev->trans_start));
1039 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
1040 netif_queue_stopped(dev),
1041 priv->tx_head, priv->tx_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 /* XXX reset QP, etc. */
1043}
1044
1045static int ipoib_hard_header(struct sk_buff *skb,
1046 struct net_device *dev,
1047 unsigned short type,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001048 const void *daddr, const void *saddr, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
1050 struct ipoib_header *header;
Eric Dumazetb49fe362014-09-18 11:00:27 -07001051 struct ipoib_cb *cb = ipoib_skb_cb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001052
1053 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
1054
1055 header->proto = htons(type);
1056 header->reserved = 0;
1057
1058 /*
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001059 * we don't rely on dst_entry structure, always stuff the
Roland Dreier936d7de2012-02-07 14:51:21 +00001060 * destination address into skb->cb so we can figure out where
1061 * to send the packet later.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001063 memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Doug Ledford83bdd3b2013-04-01 21:25:30 +00001065 return sizeof *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066}
1067
1068static void ipoib_set_mcast_list(struct net_device *dev)
1069{
1070 struct ipoib_dev_priv *priv = netdev_priv(dev);
1071
Leonid Arsh7a343d42006-03-23 19:52:51 +02001072 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
1073 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
1074 return;
1075 }
1076
Doug Ledford0b395782015-02-21 19:27:03 -05001077 queue_work(priv->wq, &priv->restart_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078}
1079
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +02001080static int ipoib_get_iflink(const struct net_device *dev)
1081{
1082 struct ipoib_dev_priv *priv = netdev_priv(dev);
1083
Erez Shitrit2c153952015-04-16 16:34:34 +03001084 /* parent interface */
1085 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
1086 return dev->ifindex;
1087
1088 /* child/vlan interface */
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +02001089 return priv->parent->ifindex;
1090}
1091
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001092static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001094 /*
1095 * Use only the address parts that contributes to spreading
1096 * The subnet prefix is not used as one can not connect to
1097 * same remote port (GUID) using the same remote QPN via two
1098 * different subnets.
1099 */
1100 /* qpn octets[1:4) & port GUID octets[12:20) */
Shlomo Pongratz9d1ad662013-02-19 15:40:22 +00001101 u32 *d32 = (u32 *) daddr;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001102 u32 hv;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Shlomo Pongratz9d1ad662013-02-19 15:40:22 +00001104 hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001105 return hv & htbl->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106}
1107
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001108struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
1109{
1110 struct ipoib_dev_priv *priv = netdev_priv(dev);
1111 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1112 struct ipoib_neigh_hash *htbl;
1113 struct ipoib_neigh *neigh = NULL;
1114 u32 hash_val;
1115
1116 rcu_read_lock_bh();
1117
1118 htbl = rcu_dereference_bh(ntbl->htbl);
1119
1120 if (!htbl)
1121 goto out_unlock;
1122
1123 hash_val = ipoib_addr_hash(htbl, daddr);
1124 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
1125 neigh != NULL;
1126 neigh = rcu_dereference_bh(neigh->hnext)) {
1127 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1128 /* found, take one ref on behalf of the caller */
1129 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1130 /* deleted */
1131 neigh = NULL;
1132 goto out_unlock;
1133 }
1134 neigh->alive = jiffies;
1135 goto out_unlock;
1136 }
1137 }
1138
1139out_unlock:
1140 rcu_read_unlock_bh();
1141 return neigh;
1142}
1143
1144static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
1145{
1146 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1147 struct ipoib_neigh_hash *htbl;
1148 unsigned long neigh_obsolete;
1149 unsigned long dt;
1150 unsigned long flags;
1151 int i;
Christoph Lameterbd99b2e2015-09-24 12:00:05 -05001152 LIST_HEAD(remove_list);
1153 struct ipoib_mcast *mcast, *tmcast;
1154 struct net_device *dev = priv->dev;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001155
1156 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1157 return;
1158
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001159 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001160
1161 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001162 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001163
1164 if (!htbl)
1165 goto out_unlock;
1166
1167 /* neigh is obsolete if it was idle for two GC periods */
1168 dt = 2 * arp_tbl.gc_interval;
1169 neigh_obsolete = jiffies - dt;
1170 /* handle possible race condition */
1171 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
1172 goto out_unlock;
1173
1174 for (i = 0; i < htbl->size; i++) {
1175 struct ipoib_neigh *neigh;
1176 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1177
1178 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001179 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001180 /* was the neigh idle for two GC periods */
1181 if (time_after(neigh_obsolete, neigh->alive)) {
Christoph Lameterbd99b2e2015-09-24 12:00:05 -05001182 u8 *mgid = neigh->daddr + 4;
1183
1184 /* Is this multicast ? */
1185 if (*mgid == 0xff) {
1186 mcast = __ipoib_mcast_find(dev, mgid);
1187
1188 if (mcast && test_bit(IPOIB_MCAST_FLAG_SENDONLY, &mcast->flags)) {
1189 list_del(&mcast->list);
1190 rb_erase(&mcast->rb_node, &priv->multicast_tree);
1191 list_add_tail(&mcast->list, &remove_list);
1192 }
1193 }
1194
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001195 rcu_assign_pointer(*np,
1196 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001197 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001198 /* remove from path/mc list */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001199 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001200 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1201 } else {
1202 np = &neigh->hnext;
1203 }
1204
1205 }
1206 }
1207
1208out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001209 spin_unlock_irqrestore(&priv->lock, flags);
Christoph Lameterbd99b2e2015-09-24 12:00:05 -05001210 list_for_each_entry_safe(mcast, tmcast, &remove_list, list)
1211 ipoib_mcast_leave(dev, mcast);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001212}
1213
1214static void ipoib_reap_neigh(struct work_struct *work)
1215{
1216 struct ipoib_dev_priv *priv =
1217 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
1218
1219 __ipoib_reap_neigh(priv);
1220
1221 if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
Doug Ledford0b395782015-02-21 19:27:03 -05001222 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001223 arp_tbl.gc_interval);
1224}
1225
1226
1227static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
Moni Shoua732a2172007-10-09 19:43:36 -07001228 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001229{
1230 struct ipoib_neigh *neigh;
1231
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001232 neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001233 if (!neigh)
1234 return NULL;
1235
Moni Shoua732a2172007-10-09 19:43:36 -07001236 neigh->dev = dev;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001237 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
Roland Dreier82b39912006-12-12 14:48:18 -08001238 skb_queue_head_init(&neigh->queue);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001239 INIT_LIST_HEAD(&neigh->list);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001240 ipoib_cm_set(neigh, NULL);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001241 /* one ref on behalf of the caller */
1242 atomic_set(&neigh->refcnt, 1);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001243
1244 return neigh;
1245}
1246
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001247struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
1248 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001249{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001250 struct ipoib_dev_priv *priv = netdev_priv(dev);
1251 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1252 struct ipoib_neigh_hash *htbl;
1253 struct ipoib_neigh *neigh;
1254 u32 hash_val;
1255
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001256 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001257 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001258 if (!htbl) {
1259 neigh = NULL;
1260 goto out_unlock;
1261 }
1262
1263 /* need to add a new neigh, but maybe some other thread succeeded?
1264 * recalc hash, maybe hash resize took place so we do a search
1265 */
1266 hash_val = ipoib_addr_hash(htbl, daddr);
1267 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001268 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001269 neigh != NULL;
1270 neigh = rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001271 lockdep_is_held(&priv->lock))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001272 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1273 /* found, take one ref on behalf of the caller */
1274 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1275 /* deleted */
1276 neigh = NULL;
1277 break;
1278 }
1279 neigh->alive = jiffies;
1280 goto out_unlock;
1281 }
1282 }
1283
1284 neigh = ipoib_neigh_ctor(daddr, dev);
1285 if (!neigh)
1286 goto out_unlock;
1287
1288 /* one ref on behalf of the hash table */
1289 atomic_inc(&neigh->refcnt);
1290 neigh->alive = jiffies;
1291 /* put in hash */
1292 rcu_assign_pointer(neigh->hnext,
1293 rcu_dereference_protected(htbl->buckets[hash_val],
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001294 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001295 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1296 atomic_inc(&ntbl->entries);
1297
1298out_unlock:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001299
1300 return neigh;
1301}
1302
1303void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1304{
1305 /* neigh reference count was dropprd to zero */
1306 struct net_device *dev = neigh->dev;
1307 struct ipoib_dev_priv *priv = netdev_priv(dev);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001308 struct sk_buff *skb;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001309 if (neigh->ah)
1310 ipoib_put_ah(neigh->ah);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001311 while ((skb = __skb_dequeue(&neigh->queue))) {
Roland Dreierde903512007-09-28 15:33:51 -07001312 ++dev->stats.tx_dropped;
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001313 dev_kfree_skb_any(skb);
1314 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001315 if (ipoib_cm_get(neigh))
1316 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001317 ipoib_dbg(netdev_priv(dev),
1318 "neigh free for %06x %pI6\n",
1319 IPOIB_QPN(neigh->daddr),
1320 neigh->daddr + 4);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001321 kfree(neigh);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001322 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1323 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1324 complete(&priv->ntbl.flushed);
1325 }
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001326}
1327
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001328static void ipoib_neigh_reclaim(struct rcu_head *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001330 /* Called as a result of removal from hash table */
1331 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1332 /* note TX context may hold another ref */
1333 ipoib_neigh_put(neigh);
1334}
1335
1336void ipoib_neigh_free(struct ipoib_neigh *neigh)
1337{
1338 struct net_device *dev = neigh->dev;
1339 struct ipoib_dev_priv *priv = netdev_priv(dev);
1340 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1341 struct ipoib_neigh_hash *htbl;
1342 struct ipoib_neigh __rcu **np;
1343 struct ipoib_neigh *n;
1344 u32 hash_val;
1345
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001346 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001347 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001348 if (!htbl)
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001349 return;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001350
1351 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1352 np = &htbl->buckets[hash_val];
1353 for (n = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001354 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001355 n != NULL;
Shlomo Pongratz6c723a62012-08-13 14:39:50 +00001356 n = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001357 lockdep_is_held(&priv->lock))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001358 if (n == neigh) {
1359 /* found */
1360 rcu_assign_pointer(*np,
1361 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001362 lockdep_is_held(&priv->lock)));
Jim Foraker49b8e742013-08-08 17:44:22 -07001363 /* remove from parent list */
1364 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001365 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001366 return;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001367 } else {
1368 np = &n->hnext;
1369 }
1370 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001371}
1372
1373static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1374{
1375 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1376 struct ipoib_neigh_hash *htbl;
Bart Van Assche52374962015-05-26 15:03:48 +02001377 struct ipoib_neigh __rcu **buckets;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001378 u32 size;
1379
1380 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1381 ntbl->htbl = NULL;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001382 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1383 if (!htbl)
1384 return -ENOMEM;
1385 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1386 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1387 buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1388 if (!buckets) {
1389 kfree(htbl);
1390 return -ENOMEM;
1391 }
1392 htbl->size = size;
1393 htbl->mask = (size - 1);
1394 htbl->buckets = buckets;
Bart Van Assche52374962015-05-26 15:03:48 +02001395 RCU_INIT_POINTER(ntbl->htbl, htbl);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001396 htbl->ntbl = ntbl;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001397 atomic_set(&ntbl->entries, 0);
1398
1399 /* start garbage collection */
1400 clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
Doug Ledford0b395782015-02-21 19:27:03 -05001401 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001402 arp_tbl.gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403
1404 return 0;
1405}
1406
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001407static void neigh_hash_free_rcu(struct rcu_head *head)
1408{
1409 struct ipoib_neigh_hash *htbl = container_of(head,
1410 struct ipoib_neigh_hash,
1411 rcu);
1412 struct ipoib_neigh __rcu **buckets = htbl->buckets;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001413 struct ipoib_neigh_table *ntbl = htbl->ntbl;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001414
1415 kfree(buckets);
1416 kfree(htbl);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001417 complete(&ntbl->deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001418}
1419
1420void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1421{
1422 struct ipoib_dev_priv *priv = netdev_priv(dev);
1423 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1424 struct ipoib_neigh_hash *htbl;
1425 unsigned long flags;
1426 int i;
1427
1428 /* remove all neigh connected to a given path or mcast */
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001429 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001430
1431 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001432 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001433
1434 if (!htbl)
1435 goto out_unlock;
1436
1437 for (i = 0; i < htbl->size; i++) {
1438 struct ipoib_neigh *neigh;
1439 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1440
1441 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001442 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001443 /* delete neighs belong to this parent */
1444 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1445 rcu_assign_pointer(*np,
1446 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001447 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001448 /* remove from parent list */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001449 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001450 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1451 } else {
1452 np = &neigh->hnext;
1453 }
1454
1455 }
1456 }
1457out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001458 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001459}
1460
1461static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1462{
1463 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1464 struct ipoib_neigh_hash *htbl;
1465 unsigned long flags;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001466 int i, wait_flushed = 0;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001467
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001468 init_completion(&priv->ntbl.flushed);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001469
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001470 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001471
1472 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001473 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001474 if (!htbl)
1475 goto out_unlock;
1476
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001477 wait_flushed = atomic_read(&priv->ntbl.entries);
1478 if (!wait_flushed)
1479 goto free_htbl;
1480
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001481 for (i = 0; i < htbl->size; i++) {
1482 struct ipoib_neigh *neigh;
1483 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1484
1485 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001486 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001487 rcu_assign_pointer(*np,
1488 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001489 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001490 /* remove from path/mc list */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001491 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001492 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1493 }
1494 }
1495
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001496free_htbl:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001497 rcu_assign_pointer(ntbl->htbl, NULL);
1498 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1499
1500out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001501 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001502 if (wait_flushed)
1503 wait_for_completion(&priv->ntbl.flushed);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001504}
1505
1506static void ipoib_neigh_hash_uninit(struct net_device *dev)
1507{
1508 struct ipoib_dev_priv *priv = netdev_priv(dev);
1509 int stopped;
1510
1511 ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001512 init_completion(&priv->ntbl.deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001513 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1514
1515 /* Stop GC if called at init fail need to cancel work */
1516 stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1517 if (!stopped)
1518 cancel_delayed_work(&priv->neigh_reap_task);
1519
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001520 ipoib_flush_neighs(priv);
1521
1522 wait_for_completion(&priv->ntbl.deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001523}
1524
1525
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1527{
1528 struct ipoib_dev_priv *priv = netdev_priv(dev);
1529
1530 /* Allocate RX/TX "rings" to hold queued skbs */
Shirley Ma0f485252006-04-10 09:43:58 -07001531 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 GFP_KERNEL);
1533 if (!priv->rx_ring) {
1534 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
Shirley Ma0f485252006-04-10 09:43:58 -07001535 ca->name, ipoib_recvq_size);
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001536 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001537 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538
Joe Perches948579c2010-11-05 03:07:36 +00001539 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 if (!priv->tx_ring) {
1541 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
Shirley Ma0f485252006-04-10 09:43:58 -07001542 ca->name, ipoib_sendq_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543 goto out_rx_ring_cleanup;
1544 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001546 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001547
1548 if (ipoib_ib_dev_init(dev, ca, port))
1549 goto out_tx_ring_cleanup;
1550
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001551 /*
1552 * Must be after ipoib_ib_dev_init so we can allocate a per
1553 * device wq there and use it here
1554 */
1555 if (ipoib_neigh_hash_init(priv) < 0)
1556 goto out_dev_uninit;
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 return 0;
1559
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001560out_dev_uninit:
1561 ipoib_ib_dev_cleanup(dev);
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563out_tx_ring_cleanup:
Roland Dreier10313cb2008-03-12 07:51:03 -07001564 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001565
1566out_rx_ring_cleanup:
1567 kfree(priv->rx_ring);
1568
1569out:
1570 return -ENOMEM;
1571}
1572
1573void ipoib_dev_cleanup(struct net_device *dev)
1574{
1575 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001576 LIST_HEAD(head);
1577
1578 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579
Roland Dreier1732b0e2005-11-07 10:33:11 -08001580 ipoib_delete_debug_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
1582 /* Delete any child interfaces first */
1583 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001584 /* Stop GC on child */
1585 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1586 cancel_delayed_work(&cpriv->neigh_reap_task);
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001587 unregister_netdevice_queue(cpriv->dev, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 }
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001589 unregister_netdevice_many(&head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001591 /*
1592 * Must be before ipoib_ib_dev_cleanup or we delete an in use
1593 * work queue
1594 */
1595 ipoib_neigh_hash_uninit(dev);
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 ipoib_ib_dev_cleanup(dev);
1598
Hal Rosenstock92a6b342005-08-13 20:50:27 -07001599 kfree(priv->rx_ring);
Roland Dreier10313cb2008-03-12 07:51:03 -07001600 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601
Hal Rosenstock92a6b342005-08-13 20:50:27 -07001602 priv->rx_ring = NULL;
1603 priv->tx_ring = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001604}
1605
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001606static const struct header_ops ipoib_header_ops = {
1607 .create = ipoib_hard_header,
1608};
1609
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001610static const struct net_device_ops ipoib_netdev_ops = {
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001611 .ndo_uninit = ipoib_uninit,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001612 .ndo_open = ipoib_open,
1613 .ndo_stop = ipoib_stop,
1614 .ndo_change_mtu = ipoib_change_mtu,
Michał Mirosław3d96c742011-04-19 00:43:20 +00001615 .ndo_fix_features = ipoib_fix_features,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001616 .ndo_start_xmit = ipoib_start_xmit,
1617 .ndo_tx_timeout = ipoib_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001618 .ndo_set_rx_mode = ipoib_set_mcast_list,
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +02001619 .ndo_get_iflink = ipoib_get_iflink,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001620};
1621
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001622void ipoib_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623{
1624 struct ipoib_dev_priv *priv = netdev_priv(dev);
1625
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001626 dev->netdev_ops = &ipoib_netdev_ops;
Roland Dreier2337f802007-10-23 19:57:54 -07001627 dev->header_ops = &ipoib_header_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001628
Eli Cohen82c24c12008-04-16 21:09:32 -07001629 ipoib_set_ethtool_ops(dev);
1630
Michal Schmidt7f1a3862013-08-21 18:50:22 +02001631 netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001632
Roland Dreier2337f802007-10-23 19:57:54 -07001633 dev->watchdog_timeo = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
Roland Dreier2337f802007-10-23 19:57:54 -07001635 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636
Roland Dreier936d7de2012-02-07 14:51:21 +00001637 dev->hard_header_len = IPOIB_ENCAP_LEN;
Roland Dreier2337f802007-10-23 19:57:54 -07001638 dev->addr_len = INFINIBAND_ALEN;
1639 dev->type = ARPHRD_INFINIBAND;
1640 dev->tx_queue_len = ipoib_sendq_size * 2;
Eli Coheneb14032f2008-01-30 18:30:46 +02001641 dev->features = (NETIF_F_VLAN_CHALLENGED |
Eli Coheneb14032f2008-01-30 18:30:46 +02001642 NETIF_F_HIGHDMA);
Eric Dumazet02875872014-10-05 18:38:35 -07001643 netif_keep_dst(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001644
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
1646
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 priv->dev = dev;
1648
1649 spin_lock_init(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650
Erez Shitritf47944c2013-10-16 17:37:49 +03001651 init_rwsem(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652
1653 INIT_LIST_HEAD(&priv->path_list);
1654 INIT_LIST_HEAD(&priv->child_intfs);
1655 INIT_LIST_HEAD(&priv->dead_ahs);
1656 INIT_LIST_HEAD(&priv->multicast_list);
1657
David Howellsc4028952006-11-22 14:57:56 +00001658 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
Yossi Etigine8224e42008-09-16 11:57:45 -07001659 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
Moni Shouaee1e2c82008-07-14 23:48:49 -07001660 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
1661 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
1662 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
David Howellsc4028952006-11-22 14:57:56 +00001663 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1664 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001665 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666}
1667
1668struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
1669{
1670 struct net_device *dev;
1671
Tom Gundersenc835a672014-07-14 16:37:24 +02001672 dev = alloc_netdev((int)sizeof(struct ipoib_dev_priv), name,
1673 NET_NAME_UNKNOWN, ipoib_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674 if (!dev)
1675 return NULL;
1676
1677 return netdev_priv(dev);
1678}
1679
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001680static ssize_t show_pkey(struct device *dev,
1681 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001683 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684
1685 return sprintf(buf, "0x%04x\n", priv->pkey);
1686}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001687static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001689static ssize_t show_umcast(struct device *dev,
1690 struct device_attribute *attr, char *buf)
1691{
1692 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1693
1694 return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1695}
1696
Or Gerlitz862096a2012-09-27 12:06:02 +00001697void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001698{
Or Gerlitz862096a2012-09-27 12:06:02 +00001699 struct ipoib_dev_priv *priv = netdev_priv(ndev);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001700
1701 if (umcast_val > 0) {
1702 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1703 ipoib_warn(priv, "ignoring multicast groups joined directly "
1704 "by userspace\n");
1705 } else
1706 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
Or Gerlitz862096a2012-09-27 12:06:02 +00001707}
1708
1709static ssize_t set_umcast(struct device *dev,
1710 struct device_attribute *attr,
1711 const char *buf, size_t count)
1712{
1713 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1714
1715 ipoib_set_umcast(to_net_dev(dev), umcast_val);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001716
1717 return count;
1718}
1719static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1720
1721int ipoib_add_umcast_attr(struct net_device *dev)
1722{
1723 return device_create_file(&dev->dev, &dev_attr_umcast);
1724}
1725
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001726static ssize_t create_child(struct device *dev,
1727 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001728 const char *buf, size_t count)
1729{
1730 int pkey;
1731 int ret;
1732
1733 if (sscanf(buf, "%i", &pkey) != 1)
1734 return -EINVAL;
1735
Or Gerlitz3d790a42013-07-18 14:02:31 +03001736 if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return -EINVAL;
1738
Roland Dreier4ce05932005-08-19 12:03:17 -07001739 /*
1740 * Set the full membership bit, so that we join the right
1741 * broadcast group, etc.
1742 */
1743 pkey |= 0x8000;
1744
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001745 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747 return ret ? ret : count;
1748}
Or Gerlitz7a52b342010-06-06 04:59:16 +00001749static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001751static ssize_t delete_child(struct device *dev,
1752 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753 const char *buf, size_t count)
1754{
1755 int pkey;
1756 int ret;
1757
1758 if (sscanf(buf, "%i", &pkey) != 1)
1759 return -EINVAL;
1760
1761 if (pkey < 0 || pkey > 0xffff)
1762 return -EINVAL;
1763
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001764 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001765
1766 return ret ? ret : count;
1767
1768}
Or Gerlitz7a52b342010-06-06 04:59:16 +00001769static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001770
1771int ipoib_add_pkey_attr(struct net_device *dev)
1772{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001773 return device_create_file(&dev->dev, &dev_attr_pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774}
1775
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001776int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
1777{
1778 struct ib_device_attr *device_attr;
1779 int result = -ENOMEM;
1780
1781 device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
1782 if (!device_attr) {
1783 printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
1784 hca->name, sizeof *device_attr);
1785 return result;
1786 }
1787
1788 result = ib_query_device(hca, device_attr);
1789 if (result) {
1790 printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
1791 hca->name, result);
1792 kfree(device_attr);
1793 return result;
1794 }
1795 priv->hca_caps = device_attr->device_cap_flags;
1796
1797 kfree(device_attr);
1798
1799 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
Michał Mirosław3d96c742011-04-19 00:43:20 +00001800 priv->dev->hw_features = NETIF_F_SG |
1801 NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1802
1803 if (priv->hca_caps & IB_DEVICE_UD_TSO)
1804 priv->dev->hw_features |= NETIF_F_TSO;
1805
1806 priv->dev->features |= priv->dev->hw_features;
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001807 }
1808
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001809 return 0;
1810}
1811
Linus Torvalds1da177e2005-04-16 15:20:36 -07001812static struct net_device *ipoib_add_port(const char *format,
1813 struct ib_device *hca, u8 port)
1814{
1815 struct ipoib_dev_priv *priv;
Shirley Mabc7b3a32008-04-23 11:55:45 -07001816 struct ib_port_attr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001817 int result = -ENOMEM;
1818
1819 priv = ipoib_intf_alloc(format);
1820 if (!priv)
1821 goto alloc_mem_failed;
1822
1823 SET_NETDEV_DEV(priv->dev, hca->dma_device);
Eli Cohenc3aa9b12010-09-20 07:05:06 +00001824 priv->dev->dev_id = port - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001825
Amir Vadai58e9cc92015-07-01 14:31:01 +03001826 result = ib_query_port(hca, port, &attr);
1827 if (!result)
Shirley Mabc7b3a32008-04-23 11:55:45 -07001828 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1829 else {
1830 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
1831 hca->name, port);
1832 goto device_init_failed;
1833 }
1834
1835 /* MTU will be reset when mcast join happens */
1836 priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
1837 priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
1838
David Miller596b9b62011-07-25 00:01:25 +00001839 priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
1840
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 result = ib_query_pkey(hca, port, 0, &priv->pkey);
1842 if (result) {
1843 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1844 hca->name, port, result);
Eli Cohenca6de172007-08-21 18:46:10 +03001845 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846 }
1847
Amir Vadai58e9cc92015-07-01 14:31:01 +03001848 result = ipoib_set_dev_features(priv, hca);
1849 if (result)
Eli Cohen60461362008-04-16 21:01:10 -07001850 goto device_init_failed;
Vladimir Sokolovskyaf40da82008-07-14 23:48:48 -07001851
Roland Dreier4ce05932005-08-19 12:03:17 -07001852 /*
1853 * Set the full membership bit, so that we join the right
1854 * broadcast group, etc.
1855 */
1856 priv->pkey |= 0x8000;
1857
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 priv->dev->broadcast[8] = priv->pkey >> 8;
1859 priv->dev->broadcast[9] = priv->pkey & 0xff;
1860
1861 result = ib_query_gid(hca, port, 0, &priv->local_gid);
1862 if (result) {
1863 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1864 hca->name, port, result);
Eli Cohenca6de172007-08-21 18:46:10 +03001865 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 } else
1867 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1868
Linus Torvalds1da177e2005-04-16 15:20:36 -07001869 result = ipoib_dev_init(priv->dev, hca, port);
1870 if (result < 0) {
1871 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1872 hca->name, port, result);
1873 goto device_init_failed;
1874 }
1875
1876 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1877 priv->ca, ipoib_event);
1878 result = ib_register_event_handler(&priv->event_handler);
1879 if (result < 0) {
1880 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1881 "port %d (ret = %d)\n",
1882 hca->name, port, result);
1883 goto event_failed;
1884 }
1885
1886 result = register_netdev(priv->dev);
1887 if (result) {
1888 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1889 hca->name, port, result);
1890 goto register_failed;
1891 }
1892
Roland Dreier1732b0e2005-11-07 10:33:11 -08001893 ipoib_create_debug_files(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001895 if (ipoib_cm_add_mode_attr(priv->dev))
1896 goto sysfs_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001897 if (ipoib_add_pkey_attr(priv->dev))
1898 goto sysfs_failed;
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001899 if (ipoib_add_umcast_attr(priv->dev))
1900 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001901 if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001902 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001903 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001904 goto sysfs_failed;
1905
1906 return priv->dev;
1907
1908sysfs_failed:
Roland Dreier1732b0e2005-11-07 10:33:11 -08001909 ipoib_delete_debug_files(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 unregister_netdev(priv->dev);
1911
1912register_failed:
1913 ib_unregister_event_handler(&priv->event_handler);
Doug Ledford0b395782015-02-21 19:27:03 -05001914 flush_workqueue(ipoib_workqueue);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001915 /* Stop GC if started before flush */
1916 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1917 cancel_delayed_work(&priv->neigh_reap_task);
Doug Ledford0b395782015-02-21 19:27:03 -05001918 flush_workqueue(priv->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919
1920event_failed:
1921 ipoib_dev_cleanup(priv->dev);
1922
1923device_init_failed:
1924 free_netdev(priv->dev);
1925
1926alloc_mem_failed:
1927 return ERR_PTR(result);
1928}
1929
1930static void ipoib_add_one(struct ib_device *device)
1931{
1932 struct list_head *dev_list;
1933 struct net_device *dev;
1934 struct ipoib_dev_priv *priv;
Hal Rosenstock41390322015-06-29 09:57:00 -04001935 int p;
Michael Wang8e37ab62015-05-05 14:50:24 +02001936 int count = 0;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001937
Linus Torvalds1da177e2005-04-16 15:20:36 -07001938 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1939 if (!dev_list)
1940 return;
1941
1942 INIT_LIST_HEAD(dev_list);
1943
Hal Rosenstock41390322015-06-29 09:57:00 -04001944 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
Michael Wang8e37ab62015-05-05 14:50:24 +02001945 if (!rdma_protocol_ib(device, p))
Eli Cohen7b4c8762010-09-27 17:51:11 -07001946 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001947 dev = ipoib_add_port("ib%d", device, p);
1948 if (!IS_ERR(dev)) {
1949 priv = netdev_priv(dev);
1950 list_add_tail(&priv->list, dev_list);
Michael Wang8e37ab62015-05-05 14:50:24 +02001951 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001952 }
1953 }
1954
Michael Wang8e37ab62015-05-05 14:50:24 +02001955 if (!count) {
1956 kfree(dev_list);
1957 return;
1958 }
1959
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 ib_set_client_data(device, &ipoib_client, dev_list);
1961}
1962
Haggai Eran7c1eb452015-07-30 17:50:14 +03001963static void ipoib_remove_one(struct ib_device *device, void *client_data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964{
1965 struct ipoib_dev_priv *priv, *tmp;
Haggai Eran7c1eb452015-07-30 17:50:14 +03001966 struct list_head *dev_list = client_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967
Itai Garbi5a2815f2013-02-19 15:40:24 +00001968 if (!dev_list)
1969 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970
1971 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1972 ib_unregister_event_handler(&priv->event_handler);
Doug Ledford0b395782015-02-21 19:27:03 -05001973 flush_workqueue(ipoib_workqueue);
Roland Dreiera77a57a2008-08-19 15:01:32 -07001974
1975 rtnl_lock();
1976 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
1977 rtnl_unlock();
1978
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001979 /* Stop GC */
1980 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1981 cancel_delayed_work(&priv->neigh_reap_task);
Doug Ledford0b395782015-02-21 19:27:03 -05001982 flush_workqueue(priv->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983
1984 unregister_netdev(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001985 free_netdev(priv->dev);
1986 }
Michael S. Tsirkin06c56e42005-09-01 09:19:02 -07001987
1988 kfree(dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989}
1990
1991static int __init ipoib_init_module(void)
1992{
1993 int ret;
1994
Shirley Ma0f485252006-04-10 09:43:58 -07001995 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1996 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1997 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1998
1999 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
2000 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -07002001 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08002002#ifdef CONFIG_INFINIBAND_IPOIB_CM
2003 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
2004#endif
Shirley Ma0f485252006-04-10 09:43:58 -07002005
Eli Cohenf89271da2008-07-14 23:48:44 -07002006 /*
2007 * When copying small received packets, we only copy from the
2008 * linear data part of the SKB, so we rely on this condition.
2009 */
2010 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
2011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 ret = ipoib_register_debugfs();
2013 if (ret)
2014 return ret;
2015
2016 /*
Doug Ledford0b395782015-02-21 19:27:03 -05002017 * We create a global workqueue here that is used for all flush
2018 * operations. However, if you attempt to flush a workqueue
2019 * from a task on that same workqueue, it deadlocks the system.
2020 * We want to be able to flush the tasks associated with a
2021 * specific net device, so we also create a workqueue for each
2022 * netdevice. We queue up the tasks for that device only on
2023 * its private workqueue, and we only queue up flush events
2024 * on our global flush workqueue. This avoids the deadlocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 */
Doug Ledford0b395782015-02-21 19:27:03 -05002026 ipoib_workqueue = create_singlethread_workqueue("ipoib_flush");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 if (!ipoib_workqueue) {
2028 ret = -ENOMEM;
2029 goto err_fs;
2030 }
2031
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002032 ib_sa_register_client(&ipoib_sa_client);
2033
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 ret = ib_register_client(&ipoib_client);
2035 if (ret)
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002036 goto err_sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037
Or Gerlitz9baa0b02012-09-13 05:56:36 +00002038 ret = ipoib_netlink_init();
2039 if (ret)
2040 goto err_client;
2041
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042 return 0;
2043
Or Gerlitz9baa0b02012-09-13 05:56:36 +00002044err_client:
2045 ib_unregister_client(&ipoib_client);
2046
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002047err_sa:
2048 ib_sa_unregister_client(&ipoib_sa_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049 destroy_workqueue(ipoib_workqueue);
2050
Roland Dreier9adec1a2005-04-16 15:26:07 -07002051err_fs:
2052 ipoib_unregister_debugfs();
2053
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 return ret;
2055}
2056
2057static void __exit ipoib_cleanup_module(void)
2058{
Or Gerlitz9baa0b02012-09-13 05:56:36 +00002059 ipoib_netlink_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002060 ib_unregister_client(&ipoib_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07002061 ib_sa_unregister_client(&ipoib_sa_client);
Roland Dreier9adec1a2005-04-16 15:26:07 -07002062 ipoib_unregister_debugfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002063 destroy_workqueue(ipoib_workqueue);
2064}
2065
2066module_init(ipoib_init_module);
2067module_exit(ipoib_cleanup_module);