Linux 6.16-rc6
[linux-block.git] / drivers / infiniband / ulp / ipoib / ipoib_main.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
2a1d9b7f
RD
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
1da177e4
LT
5 *
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.
1da177e4
LT
33 */
34
35#include "ipoib.h"
36
1da177e4
LT
37#include <linux/module.h>
38
39#include <linux/init.h>
40#include <linux/slab.h>
0f485251 41#include <linux/kernel.h>
10313cbb 42#include <linux/vmalloc.h>
1da177e4
LT
43
44#include <linux/if_arp.h> /* For ARPHRD_xxx */
45
46#include <linux/ip.h>
47#include <linux/in.h>
48
b63b70d8
SP
49#include <linux/jhash.h>
50#include <net/arp.h>
ddde896e 51#include <net/addrconf.h>
fd07ba16 52#include <net/netdev_lock.h>
c11db1bf 53#include <net/pkt_sched.h>
ddde896e
GS
54#include <linux/inetdevice.h>
55#include <rdma/ib_cache.h>
14c85021 56
1da177e4
LT
57MODULE_AUTHOR("Roland Dreier");
58MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
59MODULE_LICENSE("Dual BSD/GPL");
60
0f485251
SM
61int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
62int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
63
64module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
65MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
66module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
67MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
68
1da177e4
LT
69#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
70int ipoib_debug_level;
71
72module_param_named(debug_level, ipoib_debug_level, int, 0644);
73MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
74#endif
75
1732b0ef
RD
76struct ipoib_path_iter {
77 struct net_device *dev;
78 struct ipoib_path path;
79};
80
1da177e4
LT
81static const u8 ipv4_bcast_addr[] = {
82 0x00, 0xff, 0xff, 0xff,
83 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
84 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
85};
86
87struct workqueue_struct *ipoib_workqueue;
88
c1a0b23b
MT
89struct ib_sa_client ipoib_sa_client;
90
11a0ae4c 91static int ipoib_add_one(struct ib_device *device);
7c1eb45a 92static void ipoib_remove_one(struct ib_device *device, void *client_data);
b63b70d8 93static void ipoib_neigh_reclaim(struct rcu_head *rp);
ddde896e 94static struct net_device *ipoib_get_net_dev_by_params(
1fb7f897 95 struct ib_device *dev, u32 port, u16 pkey,
ddde896e
GS
96 const union ib_gid *gid, const struct sockaddr *addr,
97 void *client_data);
492a7e67 98static int ipoib_set_mac(struct net_device *dev, void *addr);
31a82362
FD
99static int ipoib_ioctl(struct net_device *dev, struct ifreq *ifr,
100 int cmd);
1da177e4
LT
101
102static struct ib_client ipoib_client = {
103 .name = "ipoib",
104 .add = ipoib_add_one,
ddde896e
GS
105 .remove = ipoib_remove_one,
106 .get_net_dev_by_params = ipoib_get_net_dev_by_params,
1da177e4
LT
107};
108
771a5258
SR
109#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
110static int ipoib_netdev_event(struct notifier_block *this,
111 unsigned long event, void *ptr)
112{
113 struct netdev_notifier_info *ni = ptr;
114 struct net_device *dev = ni->dev;
115
116 if (dev->netdev_ops->ndo_open != ipoib_open)
117 return NOTIFY_DONE;
118
119 switch (event) {
120 case NETDEV_REGISTER:
121 ipoib_create_debug_files(dev);
122 break;
123 case NETDEV_CHANGENAME:
124 ipoib_delete_debug_files(dev);
125 ipoib_create_debug_files(dev);
126 break;
127 case NETDEV_UNREGISTER:
128 ipoib_delete_debug_files(dev);
129 break;
130 }
131
132 return NOTIFY_DONE;
133}
134#endif
135
463e5176
CR
136struct ipoib_ifupdown_work {
137 struct work_struct work;
138 struct net_device *dev;
139 netdevice_tracker dev_tracker;
140 bool up;
141};
142
143static void ipoib_ifupdown_task(struct work_struct *work)
144{
145 struct ipoib_ifupdown_work *pwork =
146 container_of(work, struct ipoib_ifupdown_work, work);
147 struct net_device *dev = pwork->dev;
148 unsigned int flags;
149
150 rtnl_lock();
151 flags = dev->flags;
152 if (pwork->up)
153 flags |= IFF_UP;
154 else
155 flags &= ~IFF_UP;
156
157 if (dev->flags != flags)
158 dev_change_flags(dev, flags, NULL);
159 rtnl_unlock();
160 netdev_put(dev, &pwork->dev_tracker);
161 kfree(pwork);
162}
163
164static void ipoib_schedule_ifupdown_task(struct net_device *dev, bool up)
165{
166 struct ipoib_ifupdown_work *work;
167
168 if ((up && (dev->flags & IFF_UP)) ||
169 (!up && !(dev->flags & IFF_UP)))
170 return;
171
172 work = kmalloc(sizeof(*work), GFP_KERNEL);
173 if (!work)
174 return;
175 work->dev = dev;
176 netdev_hold(dev, &work->dev_tracker, GFP_KERNEL);
177 work->up = up;
178 INIT_WORK(&work->work, ipoib_ifupdown_task);
179 queue_work(ipoib_workqueue, &work->work);
180}
181
1da177e4
LT
182int ipoib_open(struct net_device *dev)
183{
c1048aff 184 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1da177e4
LT
185
186 ipoib_dbg(priv, "bringing up interface\n");
187
437708c4
MS
188 netif_carrier_off(dev);
189
e028cc55 190 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
1da177e4 191
efc82eee 192 if (ipoib_ib_dev_open(dev)) {
dd57c930
AE
193 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
194 return 0;
b8a1b1ce 195 goto err_disable;
dd57c930 196 }
fe25c561 197
5c37077f 198 ipoib_ib_dev_up(dev);
1da177e4
LT
199
200 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
201 struct ipoib_dev_priv *cpriv;
202
203 /* Bring up any child interfaces too */
fd07ba16 204 netdev_lock_ops_to_full(dev);
463e5176
CR
205 list_for_each_entry(cpriv, &priv->child_intfs, list)
206 ipoib_schedule_ifupdown_task(cpriv->dev, true);
fd07ba16 207 netdev_unlock_full_to_ops(dev);
3ccbd933
JW
208 } else if (priv->parent) {
209 struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
1da177e4 210
3ccbd933
JW
211 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &ppriv->flags))
212 ipoib_dbg(priv, "parent device %s is not up, so child device may be not functioning.\n",
213 ppriv->dev->name);
214 }
1da177e4
LT
215 netif_start_queue(dev);
216
217 return 0;
b8a1b1ce 218
b8a1b1ce 219err_disable:
b8a1b1ce
RD
220 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
221
222 return -EINVAL;
1da177e4
LT
223}
224
225static int ipoib_stop(struct net_device *dev)
226{
c1048aff 227 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1da177e4
LT
228
229 ipoib_dbg(priv, "stopping interface\n");
230
231 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
232
233 netif_stop_queue(dev);
234
efc82eee 235 ipoib_ib_dev_down(dev);
cd565b4b 236 ipoib_ib_dev_stop(dev);
1da177e4
LT
237
238 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
239 struct ipoib_dev_priv *cpriv;
240
241 /* Bring down any child interfaces too */
fd07ba16 242 netdev_lock_ops_to_full(dev);
463e5176
CR
243 list_for_each_entry(cpriv, &priv->child_intfs, list)
244 ipoib_schedule_ifupdown_task(cpriv->dev, false);
fd07ba16 245 netdev_unlock_full_to_ops(dev);
1da177e4
LT
246 }
247
248 return 0;
249}
250
9ca36f7d 251static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
3d96c74d 252{
c1048aff 253 struct ipoib_dev_priv *priv = ipoib_priv(dev);
3d96c74d
MM
254
255 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
c4268778 256 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3d96c74d
MM
257
258 return features;
259}
260
1da177e4
LT
261static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
262{
c1048aff 263 struct ipoib_dev_priv *priv = ipoib_priv(dev);
ed7b521d 264 int ret = 0;
1da177e4 265
839fcaba 266 /* dev->mtu > 2K ==> connected mode */
586a6934
PS
267 if (ipoib_cm_admin_enabled(dev)) {
268 if (new_mtu > ipoib_cm_max_mtu(dev))
269 return -EINVAL;
270
839fcaba
MT
271 if (new_mtu > priv->mcast_mtu)
272 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
273 priv->mcast_mtu);
586a6934 274
1eb2cded 275 WRITE_ONCE(dev->mtu, new_mtu);
839fcaba
MT
276 return 0;
277 }
278
142a9c28
MS
279 if (new_mtu < (ETH_MIN_MTU + IPOIB_ENCAP_LEN) ||
280 new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
1da177e4
LT
281 return -EINVAL;
282
283 priv->admin_mtu = new_mtu;
284
29da686d
FD
285 if (priv->mcast_mtu < priv->admin_mtu)
286 ipoib_dbg(priv, "MTU must be smaller than the underlying "
287 "link layer MTU - 4 (%u)\n", priv->mcast_mtu);
288
ed7b521d 289 new_mtu = min(priv->mcast_mtu, priv->admin_mtu);
1da177e4 290
ed7b521d
ES
291 if (priv->rn_ops->ndo_change_mtu) {
292 bool carrier_status = netif_carrier_ok(dev);
293
294 netif_carrier_off(dev);
295
296 /* notify lower level on the real mtu */
297 ret = priv->rn_ops->ndo_change_mtu(dev, new_mtu);
298
299 if (carrier_status)
300 netif_carrier_on(dev);
301 } else {
1eb2cded 302 WRITE_ONCE(dev->mtu, new_mtu);
ed7b521d
ES
303 }
304
305 return ret;
1da177e4
LT
306}
307
b6c871e5
ES
308static void ipoib_get_stats(struct net_device *dev,
309 struct rtnl_link_stats64 *stats)
310{
311 struct ipoib_dev_priv *priv = ipoib_priv(dev);
312
313 if (priv->rn_ops->ndo_get_stats64)
314 priv->rn_ops->ndo_get_stats64(dev, stats);
315 else
316 netdev_stats_to_stats64(stats, &dev->stats);
317}
318
ddde896e
GS
319/* Called with an RCU read lock taken */
320static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr,
321 struct net_device *dev)
322{
323 struct net *net = dev_net(dev);
324 struct in_device *in_dev;
325 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
326 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
327 __be32 ret_addr;
328
329 switch (addr->sa_family) {
330 case AF_INET:
331 in_dev = in_dev_get(dev);
332 if (!in_dev)
333 return false;
334
335 ret_addr = inet_confirm_addr(net, in_dev, 0,
336 addr_in->sin_addr.s_addr,
337 RT_SCOPE_HOST);
338 in_dev_put(in_dev);
339 if (ret_addr)
340 return true;
341
342 break;
343 case AF_INET6:
344 if (IS_ENABLED(CONFIG_IPV6) &&
345 ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1))
346 return true;
347
348 break;
349 }
350 return false;
351}
352
bf194997 353/*
ddde896e
GS
354 * Find the master net_device on top of the given net_device.
355 * @dev: base IPoIB net_device
356 *
357 * Returns the master net_device with a reference held, or the same net_device
358 * if no master exists.
359 */
360static struct net_device *ipoib_get_master_net_dev(struct net_device *dev)
361{
362 struct net_device *master;
363
364 rcu_read_lock();
365 master = netdev_master_upper_dev_get_rcu(dev);
e4e40a87 366 dev_hold(master);
ddde896e
GS
367 rcu_read_unlock();
368
369 if (master)
370 return master;
371
372 dev_hold(dev);
373 return dev;
374}
375
e0e79c8e
DA
376struct ipoib_walk_data {
377 const struct sockaddr *addr;
378 struct net_device *result;
379};
380
eff74233
TY
381static int ipoib_upper_walk(struct net_device *upper,
382 struct netdev_nested_priv *priv)
e0e79c8e 383{
eff74233 384 struct ipoib_walk_data *data = (struct ipoib_walk_data *)priv->data;
e0e79c8e
DA
385 int ret = 0;
386
387 if (ipoib_is_dev_match_addr_rcu(data->addr, upper)) {
388 dev_hold(upper);
389 data->result = upper;
390 ret = 1;
391 }
392
393 return ret;
394}
395
ddde896e 396/**
bf194997
LR
397 * ipoib_get_net_dev_match_addr - Find a net_device matching
398 * the given address, which is an upper device of the given net_device.
399 *
ddde896e
GS
400 * @addr: IP address to look for.
401 * @dev: base IPoIB net_device
402 *
403 * If found, returns the net_device with a reference held. Otherwise return
404 * NULL.
405 */
406static struct net_device *ipoib_get_net_dev_match_addr(
407 const struct sockaddr *addr, struct net_device *dev)
408{
eff74233 409 struct netdev_nested_priv priv;
e0e79c8e
DA
410 struct ipoib_walk_data data = {
411 .addr = addr,
412 };
ddde896e 413
eff74233 414 priv.data = (void *)&data;
ddde896e
GS
415 rcu_read_lock();
416 if (ipoib_is_dev_match_addr_rcu(addr, dev)) {
417 dev_hold(dev);
e0e79c8e 418 data.result = dev;
ddde896e
GS
419 goto out;
420 }
421
eff74233 422 netdev_walk_all_upper_dev_rcu(dev, ipoib_upper_walk, &priv);
ddde896e
GS
423out:
424 rcu_read_unlock();
e0e79c8e 425 return data.result;
ddde896e
GS
426}
427
428/* returns the number of IPoIB netdevs on top a given ipoib device matching a
429 * pkey_index and address, if one exists.
430 *
431 * @found_net_dev: contains a matching net_device if the return value >= 1,
432 * with a reference held. */
433static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
434 const union ib_gid *gid,
435 u16 pkey_index,
436 const struct sockaddr *addr,
437 int nesting,
438 struct net_device **found_net_dev)
439{
440 struct ipoib_dev_priv *child_priv;
441 struct net_device *net_dev = NULL;
442 int matches = 0;
443
444 if (priv->pkey_index == pkey_index &&
445 (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
446 if (!addr) {
447 net_dev = ipoib_get_master_net_dev(priv->dev);
448 } else {
449 /* Verify the net_device matches the IP address, as
450 * IPoIB child devices currently share a GID. */
451 net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev);
452 }
453 if (net_dev) {
454 if (!*found_net_dev)
455 *found_net_dev = net_dev;
456 else
457 dev_put(net_dev);
458 ++matches;
459 }
460 }
461
463e5176
CR
462 if (test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
463 return matches;
464
ddde896e 465 /* Check child interfaces */
463e5176 466 netdev_lock(priv->dev);
ddde896e
GS
467 list_for_each_entry(child_priv, &priv->child_intfs, list) {
468 matches += ipoib_match_gid_pkey_addr(child_priv, gid,
463e5176
CR
469 pkey_index, addr,
470 nesting + 1,
471 found_net_dev);
ddde896e
GS
472 if (matches > 1)
473 break;
474 }
463e5176 475 netdev_unlock(priv->dev);
ddde896e
GS
476
477 return matches;
478}
479
480/* Returns the number of matching net_devs found (between 0 and 2). Also
481 * return the matching net_device in the @net_dev parameter, holding a
482 * reference to the net_device, if the number of matches >= 1 */
1fb7f897 483static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u32 port,
ddde896e
GS
484 u16 pkey_index,
485 const union ib_gid *gid,
486 const struct sockaddr *addr,
487 struct net_device **net_dev)
488{
489 struct ipoib_dev_priv *priv;
490 int matches = 0;
491
492 *net_dev = NULL;
493
494 list_for_each_entry(priv, dev_list, list) {
495 if (priv->port != port)
496 continue;
497
498 matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
499 addr, 0, net_dev);
500 if (matches > 1)
501 break;
502 }
503
504 return matches;
505}
506
507static struct net_device *ipoib_get_net_dev_by_params(
1fb7f897 508 struct ib_device *dev, u32 port, u16 pkey,
ddde896e
GS
509 const union ib_gid *gid, const struct sockaddr *addr,
510 void *client_data)
511{
512 struct net_device *net_dev;
513 struct list_head *dev_list = client_data;
514 u16 pkey_index;
515 int matches;
516 int ret;
517
518 if (!rdma_protocol_ib(dev, port))
519 return NULL;
520
521 ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
522 if (ret)
523 return NULL;
524
ddde896e
GS
525 /* See if we can find a unique device matching the L2 parameters */
526 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
527 gid, NULL, &net_dev);
528
529 switch (matches) {
530 case 0:
531 return NULL;
532 case 1:
533 return net_dev;
534 }
535
536 dev_put(net_dev);
537
538 /* Couldn't find a unique device with L2 parameters only. Use L3
539 * address to uniquely match the net device */
540 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
541 gid, addr, &net_dev);
542 switch (matches) {
543 case 0:
544 return NULL;
545 default:
546 dev_warn_ratelimited(&dev->dev,
547 "duplicate IP address detected\n");
df561f66 548 fallthrough;
ddde896e
GS
549 case 1:
550 return net_dev;
551 }
552}
553
71d9c5f9
RD
554int ipoib_set_mode(struct net_device *dev, const char *buf)
555{
c1048aff 556 struct ipoib_dev_priv *priv = ipoib_priv(dev);
71d9c5f9 557
80b5b35a
FD
558 if ((test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags) &&
559 !strcmp(buf, "connected\n")) ||
560 (!test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags) &&
561 !strcmp(buf, "datagram\n"))) {
562 return 0;
563 }
564
71d9c5f9
RD
565 /* flush paths if we switch modes so that connections are restarted */
566 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
567 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
568 ipoib_warn(priv, "enabling connected mode "
569 "will cause multicast packet drops\n");
fd07ba16 570 netdev_lock_ops(dev);
71d9c5f9 571 netdev_update_features(dev);
fd07ba16 572 netif_set_mtu(dev, ipoib_cm_max_mtu(dev));
8f149b68 573 netif_set_real_num_tx_queues(dev, 1);
fd07ba16 574 netdev_unlock_ops(dev);
71d9c5f9 575 rtnl_unlock();
e622f2f4 576 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
71d9c5f9
RD
577
578 ipoib_flush_paths(dev);
0a0007f2 579 return (!rtnl_trylock()) ? -EBUSY : 0;
71d9c5f9
RD
580 }
581
582 if (!strcmp(buf, "datagram\n")) {
583 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
fd07ba16 584 netdev_lock_ops(dev);
71d9c5f9 585 netdev_update_features(dev);
fd07ba16 586 netif_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
8f149b68 587 netif_set_real_num_tx_queues(dev, dev->num_tx_queues);
fd07ba16 588 netdev_unlock_ops(dev);
71d9c5f9
RD
589 rtnl_unlock();
590 ipoib_flush_paths(dev);
0a0007f2 591 return (!rtnl_trylock()) ? -EBUSY : 0;
71d9c5f9
RD
592 }
593
594 return -EINVAL;
595}
596
546481c2 597struct ipoib_path *__path_find(struct net_device *dev, void *gid)
1da177e4 598{
c1048aff 599 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1da177e4
LT
600 struct rb_node *n = priv->path_tree.rb_node;
601 struct ipoib_path *path;
602 int ret;
603
604 while (n) {
605 path = rb_entry(n, struct ipoib_path, rb_node);
606
37c22a77 607 ret = memcmp(gid, path->pathrec.dgid.raw,
1da177e4
LT
608 sizeof (union ib_gid));
609
610 if (ret < 0)
611 n = n->rb_left;
612 else if (ret > 0)
613 n = n->rb_right;
614 else
615 return path;
616 }
617
618 return NULL;
619}
620
621static int __path_add(struct net_device *dev, struct ipoib_path *path)
622{
c1048aff 623 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1da177e4
LT
624 struct rb_node **n = &priv->path_tree.rb_node;
625 struct rb_node *pn = NULL;
626 struct ipoib_path *tpath;
627 int ret;
628
629 while (*n) {
630 pn = *n;
631 tpath = rb_entry(pn, struct ipoib_path, rb_node);
632
633 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
634 sizeof (union ib_gid));
635 if (ret < 0)
636 n = &pn->rb_left;
637 else if (ret > 0)
638 n = &pn->rb_right;
639 else
640 return -EEXIST;
641 }
642
643 rb_link_node(&path->rb_node, pn, n);
644 rb_insert_color(&path->rb_node, &priv->path_tree);
645
646 list_add_tail(&path->list, &priv->path_list);
647
648 return 0;
649}
650
651static void path_free(struct net_device *dev, struct ipoib_path *path)
652{
1da177e4 653 struct sk_buff *skb;
1da177e4
LT
654
655 while ((skb = __skb_dequeue(&path->queue)))
656 dev_kfree_skb_irq(skb);
657
0dd9ce18 658 ipoib_dbg(ipoib_priv(dev), "%s\n", __func__);
1da177e4 659
b63b70d8
SP
660 /* remove all neigh connected to this path */
661 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
1da177e4
LT
662
663 if (path->ah)
664 ipoib_put_ah(path->ah);
665
666 kfree(path);
667}
668
1732b0ef
RD
669#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
670
671struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
672{
673 struct ipoib_path_iter *iter;
674
b1b63970 675 iter = kmalloc(sizeof(*iter), GFP_KERNEL);
1732b0ef
RD
676 if (!iter)
677 return NULL;
678
679 iter->dev = dev;
680 memset(iter->path.pathrec.dgid.raw, 0, 16);
681
682 if (ipoib_path_iter_next(iter)) {
683 kfree(iter);
684 return NULL;
685 }
686
687 return iter;
688}
689
690int ipoib_path_iter_next(struct ipoib_path_iter *iter)
691{
c1048aff 692 struct ipoib_dev_priv *priv = ipoib_priv(iter->dev);
1732b0ef
RD
693 struct rb_node *n;
694 struct ipoib_path *path;
695 int ret = 1;
696
697 spin_lock_irq(&priv->lock);
698
699 n = rb_first(&priv->path_tree);
700
701 while (n) {
702 path = rb_entry(n, struct ipoib_path, rb_node);
703
704 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
705 sizeof (union ib_gid)) < 0) {
706 iter->path = *path;
707 ret = 0;
708 break;
709 }
710
711 n = rb_next(n);
712 }
713
714 spin_unlock_irq(&priv->lock);
715
716 return ret;
717}
718
719void ipoib_path_iter_read(struct ipoib_path_iter *iter,
720 struct ipoib_path *path)
721{
722 *path = iter->path;
723}
724
725#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
726
ee1e2c82
MS
727void ipoib_mark_paths_invalid(struct net_device *dev)
728{
c1048aff 729 struct ipoib_dev_priv *priv = ipoib_priv(dev);
ee1e2c82
MS
730 struct ipoib_path *path, *tp;
731
732 spin_lock_irq(&priv->lock);
733
734 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
57520751
DC
735 ipoib_dbg(priv, "mark path LID 0x%08x GID %pI6 invalid\n",
736 be32_to_cpu(sa_path_get_dlid(&path->pathrec)),
737 path->pathrec.dgid.raw);
fa9391db
DL
738 if (path->ah)
739 path->ah->valid = 0;
ee1e2c82
MS
740 }
741
742 spin_unlock_irq(&priv->lock);
743}
744
2b084176
ES
745static void push_pseudo_header(struct sk_buff *skb, const char *daddr)
746{
747 struct ipoib_pseudo_header *phdr;
748
d58ff351 749 phdr = skb_push(skb, sizeof(*phdr));
2b084176
ES
750 memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
751}
752
1da177e4
LT
753void ipoib_flush_paths(struct net_device *dev)
754{
c1048aff 755 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1da177e4
LT
756 struct ipoib_path *path, *tp;
757 LIST_HEAD(remove_list);
943c246e 758 unsigned long flags;
1da177e4 759
943c246e
RD
760 netif_tx_lock_bh(dev);
761 spin_lock_irqsave(&priv->lock, flags);
1da177e4 762
157de229 763 list_splice_init(&priv->path_list, &remove_list);
1da177e4
LT
764
765 list_for_each_entry(path, &remove_list, list)
766 rb_erase(&path->rb_node, &priv->path_tree);
767
1da177e4
LT
768 list_for_each_entry_safe(path, tp, &remove_list, list) {
769 if (path->query)
770 ib_sa_cancel_query(path->query_id, path->query);
943c246e
RD
771 spin_unlock_irqrestore(&priv->lock, flags);
772 netif_tx_unlock_bh(dev);
1da177e4
LT
773 wait_for_completion(&path->done);
774 path_free(dev, path);
943c246e
RD
775 netif_tx_lock_bh(dev);
776 spin_lock_irqsave(&priv->lock, flags);
1da177e4 777 }
943c246e
RD
778
779 spin_unlock_irqrestore(&priv->lock, flags);
780 netif_tx_unlock_bh(dev);
1da177e4
LT
781}
782
783static void path_rec_completion(int status,
c2f8fc4e 784 struct sa_path_rec *pathrec,
ccae0447 785 unsigned int num_prs, void *path_ptr)
1da177e4
LT
786{
787 struct ipoib_path *path = path_ptr;
788 struct net_device *dev = path->dev;
c1048aff 789 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1da177e4 790 struct ipoib_ah *ah = NULL;
c9da4bad 791 struct ipoib_ah *old_ah = NULL;
d04d01b1 792 struct ipoib_neigh *neigh, *tn;
1da177e4
LT
793 struct sk_buff_head skqueue;
794 struct sk_buff *skb;
795 unsigned long flags;
796
843613b0 797 if (!status)
5b095d98 798 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
57520751 799 be32_to_cpu(sa_path_get_dlid(pathrec)),
9fdca4da 800 pathrec->dgid.raw);
1da177e4 801 else
5b095d98 802 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
fcace2fe 803 status, path->pathrec.dgid.raw);
1da177e4
LT
804
805 skb_queue_head_init(&skqueue);
806
807 if (!status) {
90898850 808 struct rdma_ah_attr av;
46f1b3d7 809
4ad6a024 810 if (!ib_init_ah_attr_from_path(priv->ca, priv->port,
39839107 811 pathrec, &av, NULL)) {
46f1b3d7 812 ah = ipoib_create_ah(dev, priv->pd, &av);
aa74f487
PP
813 rdma_destroy_ah_attr(&av);
814 }
1da177e4
LT
815 }
816
817 spin_lock_irqsave(&priv->lock, flags);
818
3874397c 819 if (!IS_ERR_OR_NULL(ah)) {
43900089
ES
820 /*
821 * pathrec.dgid is used as the database key from the LLADDR,
822 * it must remain unchanged even if the SA returns a different
823 * GID to use in the AH.
824 */
825 if (memcmp(pathrec->dgid.raw, path->pathrec.dgid.raw,
826 sizeof(union ib_gid))) {
827 ipoib_dbg(
828 priv,
829 "%s got PathRec for gid %pI6 while asked for %pI6\n",
830 dev->name, pathrec->dgid.raw,
831 path->pathrec.dgid.raw);
832 memcpy(pathrec->dgid.raw, path->pathrec.dgid.raw,
833 sizeof(union ib_gid));
834 }
835
1da177e4
LT
836 path->pathrec = *pathrec;
837
c9da4bad
RD
838 old_ah = path->ah;
839 path->ah = ah;
840
1da177e4 841 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
57520751 842 ah, be32_to_cpu(sa_path_get_dlid(pathrec)),
9fdca4da 843 pathrec->sl);
1da177e4
LT
844
845 while ((skb = __skb_dequeue(&path->queue)))
846 __skb_queue_tail(&skqueue, skb);
847
d04d01b1 848 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
ee1e2c82
MS
849 if (neigh->ah) {
850 WARN_ON(neigh->ah != old_ah);
851 /*
852 * Dropping the ah reference inside
853 * priv->lock is safe here, because we
854 * will hold one more reference from
855 * the original value of path->ah (ie
856 * old_ah).
857 */
858 ipoib_put_ah(neigh->ah);
859 }
1da177e4
LT
860 kref_get(&path->ah->ref);
861 neigh->ah = path->ah;
862
b63b70d8 863 if (ipoib_cm_enabled(dev, neigh->daddr)) {
839fcaba
MT
864 if (!ipoib_cm_get(neigh))
865 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
866 path,
867 neigh));
868 if (!ipoib_cm_get(neigh)) {
b63b70d8 869 ipoib_neigh_free(neigh);
839fcaba
MT
870 continue;
871 }
872 }
873
1da177e4
LT
874 while ((skb = __skb_dequeue(&neigh->queue)))
875 __skb_queue_tail(&skqueue, skb);
876 }
fa9391db 877 path->ah->valid = 1;
5872a9fc 878 }
1da177e4 879
5872a9fc 880 path->query = NULL;
1da177e4
LT
881 complete(&path->done);
882
883 spin_unlock_irqrestore(&priv->lock, flags);
884
f72dd566
RD
885 if (IS_ERR_OR_NULL(ah))
886 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
887
ee1e2c82
MS
888 if (old_ah)
889 ipoib_put_ah(old_ah);
890
1da177e4 891 while ((skb = __skb_dequeue(&skqueue))) {
d32b9a81 892 int ret;
1da177e4 893 skb->dev = dev;
d32b9a81
FD
894 ret = dev_queue_xmit(skb);
895 if (ret)
896 ipoib_warn(priv, "%s: dev_queue_xmit failed to re-queue packet, ret:%d\n",
897 __func__, ret);
1da177e4
LT
898 }
899}
900
98aebc55
ES
901static void init_path_rec(struct ipoib_dev_priv *priv, struct ipoib_path *path,
902 void *gid)
903{
904 path->dev = priv->dev;
905
906 if (rdma_cap_opa_ah(priv->ca, priv->port))
907 path->pathrec.rec_type = SA_PATH_REC_TYPE_OPA;
908 else
909 path->pathrec.rec_type = SA_PATH_REC_TYPE_IB;
910
911 memcpy(path->pathrec.dgid.raw, gid, sizeof(union ib_gid));
912 path->pathrec.sgid = priv->local_gid;
913 path->pathrec.pkey = cpu_to_be16(priv->pkey);
914 path->pathrec.numb_path = 1;
915 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
916}
917
37c22a77 918static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
1da177e4 919{
c1048aff 920 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1da177e4
LT
921 struct ipoib_path *path;
922
1401b53a
JM
923 if (!priv->broadcast)
924 return NULL;
925
b1b63970 926 path = kzalloc(sizeof(*path), GFP_ATOMIC);
1da177e4
LT
927 if (!path)
928 return NULL;
929
1da177e4
LT
930 skb_queue_head_init(&path->queue);
931
932 INIT_LIST_HEAD(&path->neigh_list);
1da177e4 933
98aebc55 934 init_path_rec(priv, path, gid);
1da177e4
LT
935
936 return path;
937}
938
939static int path_rec_start(struct net_device *dev,
940 struct ipoib_path *path)
941{
c1048aff 942 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1da177e4 943
5b095d98 944 ipoib_dbg(priv, "Start path record lookup for %pI6\n",
fcace2fe 945 path->pathrec.dgid.raw);
1da177e4 946
65c7edda
RD
947 init_completion(&path->done);
948
1da177e4 949 path->query_id =
c1a0b23b 950 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
1da177e4
LT
951 &path->pathrec,
952 IB_SA_PATH_REC_DGID |
953 IB_SA_PATH_REC_SGID |
954 IB_SA_PATH_REC_NUMB_PATH |
81668838 955 IB_SA_PATH_REC_TRAFFIC_CLASS |
1da177e4
LT
956 IB_SA_PATH_REC_PKEY,
957 1000, GFP_ATOMIC,
958 path_rec_completion,
959 path, &path->query);
960 if (path->query_id < 0) {
01b3fc8b 961 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
1da177e4 962 path->query = NULL;
93a3ab93 963 complete(&path->done);
1da177e4
LT
964 return path->query_id;
965 }
966
967 return 0;
968}
969
fa9391db
DL
970static void neigh_refresh_path(struct ipoib_neigh *neigh, u8 *daddr,
971 struct net_device *dev)
972{
973 struct ipoib_dev_priv *priv = ipoib_priv(dev);
974 struct ipoib_path *path;
975 unsigned long flags;
976
977 spin_lock_irqsave(&priv->lock, flags);
978
979 path = __path_find(dev, daddr + 4);
980 if (!path)
981 goto out;
982 if (!path->query)
983 path_rec_start(dev, path);
984out:
985 spin_unlock_irqrestore(&priv->lock, flags);
986}
987
16ba3def
ES
988static struct ipoib_neigh *neigh_add_path(struct sk_buff *skb, u8 *daddr,
989 struct net_device *dev)
1da177e4 990{
c1048aff 991 struct ipoib_dev_priv *priv = ipoib_priv(dev);
cd565b4b 992 struct rdma_netdev *rn = netdev_priv(dev);
1da177e4
LT
993 struct ipoib_path *path;
994 struct ipoib_neigh *neigh;
943c246e 995 unsigned long flags;
1da177e4 996
b5120a6e 997 spin_lock_irqsave(&priv->lock, flags);
b63b70d8 998 neigh = ipoib_neigh_alloc(daddr, dev);
1da177e4 999 if (!neigh) {
b5120a6e 1000 spin_unlock_irqrestore(&priv->lock, flags);
de903512 1001 ++dev->stats.tx_dropped;
1da177e4 1002 dev_kfree_skb_any(skb);
16ba3def
ES
1003 return NULL;
1004 }
1005
1006 /* To avoid race condition, make sure that the
1007 * neigh will be added only once.
1008 */
1009 if (unlikely(!list_empty(&neigh->list))) {
1010 spin_unlock_irqrestore(&priv->lock, flags);
1011 return neigh;
1da177e4
LT
1012 }
1013
b63b70d8 1014 path = __path_find(dev, daddr + 4);
1da177e4 1015 if (!path) {
b63b70d8 1016 path = path_rec_create(dev, daddr + 4);
1da177e4 1017 if (!path)
d2e0655e 1018 goto err_path;
1da177e4
LT
1019
1020 __path_add(dev, path);
1021 }
1022
1023 list_add_tail(&neigh->list, &path->neigh_list);
1024
fa9391db 1025 if (path->ah && path->ah->valid) {
1da177e4
LT
1026 kref_get(&path->ah->ref);
1027 neigh->ah = path->ah;
1028
b63b70d8 1029 if (ipoib_cm_enabled(dev, neigh->daddr)) {
839fcaba
MT
1030 if (!ipoib_cm_get(neigh))
1031 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
1032 if (!ipoib_cm_get(neigh)) {
b63b70d8 1033 ipoib_neigh_free(neigh);
839fcaba
MT
1034 goto err_drop;
1035 }
fc791b63
PA
1036 if (skb_queue_len(&neigh->queue) <
1037 IPOIB_MAX_PATH_REC_QUEUE) {
2b084176 1038 push_pseudo_header(skb, neigh->daddr);
839fcaba 1039 __skb_queue_tail(&neigh->queue, skb);
fc791b63 1040 } else {
839fcaba
MT
1041 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
1042 skb_queue_len(&neigh->queue));
1043 goto err_drop;
1044 }
721d67cd
RD
1045 } else {
1046 spin_unlock_irqrestore(&priv->lock, flags);
cd565b4b
ES
1047 path->ah->last_send = rn->send(dev, skb, path->ah->ah,
1048 IPOIB_QPN(daddr));
b63b70d8 1049 ipoib_neigh_put(neigh);
16ba3def 1050 return NULL;
721d67cd 1051 }
1da177e4
LT
1052 } else {
1053 neigh->ah = NULL;
1da177e4
LT
1054
1055 if (!path->query && path_rec_start(dev, path))
49b8e744 1056 goto err_path;
2b084176
ES
1057 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1058 push_pseudo_header(skb, neigh->daddr);
1e85b806 1059 __skb_queue_tail(&neigh->queue, skb);
2b084176 1060 } else {
1e85b806 1061 goto err_drop;
2b084176 1062 }
1da177e4
LT
1063 }
1064
943c246e 1065 spin_unlock_irqrestore(&priv->lock, flags);
b63b70d8 1066 ipoib_neigh_put(neigh);
16ba3def 1067 return NULL;
1da177e4 1068
d2e0655e 1069err_path:
b63b70d8 1070 ipoib_neigh_free(neigh);
839fcaba 1071err_drop:
de903512 1072 ++dev->stats.tx_dropped;
1da177e4
LT
1073 dev_kfree_skb_any(skb);
1074
943c246e 1075 spin_unlock_irqrestore(&priv->lock, flags);
b63b70d8 1076 ipoib_neigh_put(neigh);
16ba3def
ES
1077
1078 return NULL;
1da177e4
LT
1079}
1080
1081static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
fc791b63 1082 struct ipoib_pseudo_header *phdr)
1da177e4 1083{
c1048aff 1084 struct ipoib_dev_priv *priv = ipoib_priv(dev);
cd565b4b 1085 struct rdma_netdev *rn = netdev_priv(dev);
1da177e4 1086 struct ipoib_path *path;
943c246e 1087 unsigned long flags;
1da177e4 1088
943c246e 1089 spin_lock_irqsave(&priv->lock, flags);
1da177e4 1090
98aebc55
ES
1091 /* no broadcast means that all paths are (going to be) not valid */
1092 if (!priv->broadcast)
1093 goto drop_and_unlock;
1094
fc791b63 1095 path = __path_find(dev, phdr->hwaddr + 4);
fa9391db 1096 if (!path || !path->ah || !path->ah->valid) {
71d98b46 1097 if (!path) {
fc791b63 1098 path = path_rec_create(dev, phdr->hwaddr + 4);
15517080
ES
1099 if (!path)
1100 goto drop_and_unlock;
1101 __path_add(dev, path);
1102 } else {
1103 /*
1104 * make sure there are no changes in the existing
1105 * path record
1106 */
1107 init_path_rec(priv, path, phdr->hwaddr + 4);
1108 }
1109 if (!path->query && path_rec_start(dev, path)) {
1110 goto drop_and_unlock;
71d98b46 1111 }
1da177e4 1112
15517080
ES
1113 if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1114 push_pseudo_header(skb, phdr->hwaddr);
1115 __skb_queue_tail(&path->queue, skb);
1116 goto unlock;
1da177e4 1117 } else {
98aebc55 1118 goto drop_and_unlock;
1da177e4 1119 }
1da177e4
LT
1120 }
1121
943c246e 1122 spin_unlock_irqrestore(&priv->lock, flags);
15517080
ES
1123 ipoib_dbg(priv, "Send unicast ARP to %08x\n",
1124 be32_to_cpu(sa_path_get_dlid(&path->pathrec)));
1125 path->ah->last_send = rn->send(dev, skb, path->ah->ah,
1126 IPOIB_QPN(phdr->hwaddr));
98aebc55
ES
1127 return;
1128
1129drop_and_unlock:
1130 ++dev->stats.tx_dropped;
1131 dev_kfree_skb_any(skb);
15517080 1132unlock:
98aebc55 1133 spin_unlock_irqrestore(&priv->lock, flags);
1da177e4
LT
1134}
1135
47a3968a 1136static netdev_tx_t ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
1da177e4 1137{
c1048aff 1138 struct ipoib_dev_priv *priv = ipoib_priv(dev);
cd565b4b 1139 struct rdma_netdev *rn = netdev_priv(dev);
1da177e4 1140 struct ipoib_neigh *neigh;
fc791b63 1141 struct ipoib_pseudo_header *phdr;
b63b70d8 1142 struct ipoib_header *header;
1da177e4
LT
1143 unsigned long flags;
1144
fc791b63
PA
1145 phdr = (struct ipoib_pseudo_header *) skb->data;
1146 skb_pull(skb, sizeof(*phdr));
b63b70d8
SP
1147 header = (struct ipoib_header *) skb->data;
1148
fc791b63 1149 if (unlikely(phdr->hwaddr[4] == 0xff)) {
b63b70d8
SP
1150 /* multicast, arrange "if" according to probability */
1151 if ((header->proto != htons(ETH_P_IP)) &&
1152 (header->proto != htons(ETH_P_IPV6)) &&
1153 (header->proto != htons(ETH_P_ARP)) &&
dc850b0e
PM
1154 (header->proto != htons(ETH_P_RARP)) &&
1155 (header->proto != htons(ETH_P_TIPC))) {
b63b70d8 1156 /* ethertype not supported by IPoIB */
17e6abee
DM
1157 ++dev->stats.tx_dropped;
1158 dev_kfree_skb_any(skb);
b63b70d8 1159 return NETDEV_TX_OK;
17e6abee 1160 }
b63b70d8 1161 /* Add in the P_Key for multicast*/
fc791b63
PA
1162 phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
1163 phdr->hwaddr[9] = priv->pkey & 0xff;
b63b70d8 1164
fc791b63 1165 neigh = ipoib_neigh_get(dev, phdr->hwaddr);
b63b70d8
SP
1166 if (likely(neigh))
1167 goto send_using_neigh;
fc791b63 1168 ipoib_mcast_send(dev, phdr->hwaddr, skb);
b63b70d8 1169 return NETDEV_TX_OK;
17e6abee 1170 }
1da177e4 1171
b63b70d8
SP
1172 /* unicast, arrange "switch" according to probability */
1173 switch (header->proto) {
1174 case htons(ETH_P_IP):
1175 case htons(ETH_P_IPV6):
dc850b0e 1176 case htons(ETH_P_TIPC):
fc791b63 1177 neigh = ipoib_neigh_get(dev, phdr->hwaddr);
b63b70d8 1178 if (unlikely(!neigh)) {
16ba3def
ES
1179 neigh = neigh_add_path(skb, phdr->hwaddr, dev);
1180 if (likely(!neigh))
1181 return NETDEV_TX_OK;
a50df398 1182 }
b63b70d8
SP
1183 break;
1184 case htons(ETH_P_ARP):
1185 case htons(ETH_P_RARP):
1186 /* for unicast ARP and RARP should always perform path find */
fc791b63 1187 unicast_arp_send(skb, dev, phdr);
b63b70d8
SP
1188 return NETDEV_TX_OK;
1189 default:
1190 /* ethertype not supported by IPoIB */
1191 ++dev->stats.tx_dropped;
1192 dev_kfree_skb_any(skb);
1193 return NETDEV_TX_OK;
1194 }
8a7f7521 1195
b63b70d8
SP
1196send_using_neigh:
1197 /* note we now hold a ref to neigh */
1198 if (ipoib_cm_get(neigh)) {
1199 if (ipoib_cm_up(neigh)) {
1200 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
1201 goto unref;
1da177e4 1202 }
fa9391db 1203 } else if (neigh->ah && neigh->ah->valid) {
cd565b4b
ES
1204 neigh->ah->last_send = rn->send(dev, skb, neigh->ah->ah,
1205 IPOIB_QPN(phdr->hwaddr));
b63b70d8 1206 goto unref;
fa9391db
DL
1207 } else if (neigh->ah) {
1208 neigh_refresh_path(neigh, phdr->hwaddr, dev);
b63b70d8 1209 }
1da177e4 1210
b63b70d8 1211 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
2b084176 1212 push_pseudo_header(skb, phdr->hwaddr);
b63b70d8
SP
1213 spin_lock_irqsave(&priv->lock, flags);
1214 __skb_queue_tail(&neigh->queue, skb);
1215 spin_unlock_irqrestore(&priv->lock, flags);
1da177e4 1216 } else {
b63b70d8
SP
1217 ++dev->stats.tx_dropped;
1218 dev_kfree_skb_any(skb);
1219 }
1da177e4 1220
b63b70d8
SP
1221unref:
1222 ipoib_neigh_put(neigh);
1da177e4 1223
1da177e4
LT
1224 return NETDEV_TX_OK;
1225}
1226
0290bd29 1227static void ipoib_timeout(struct net_device *dev, unsigned int txqueue)
1da177e4 1228{
c1048aff 1229 struct ipoib_dev_priv *priv = ipoib_priv(dev);
042a00f9 1230 struct rdma_netdev *rn = netdev_priv(dev);
1da177e4 1231
042a00f9
MM
1232 if (rn->tx_timeout) {
1233 rn->tx_timeout(dev, txqueue);
1234 return;
1235 }
4b2d319b 1236 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
4d0e9657 1237 jiffies_to_msecs(jiffies - dev_trans_start(dev)));
1acba6a8
VF
1238 ipoib_warn(priv,
1239 "queue stopped %d, tx_head %u, tx_tail %u, global_tx_head %u, global_tx_tail %u\n",
1240 netif_queue_stopped(dev), priv->tx_head, priv->tx_tail,
1241 priv->global_tx_head, priv->global_tx_tail);
1242
50af5d12
JW
1243
1244 schedule_work(&priv->tx_timeout_work);
1245}
1246
1247void ipoib_ib_tx_timeout_work(struct work_struct *work)
1248{
1249 struct ipoib_dev_priv *priv = container_of(work,
1250 struct ipoib_dev_priv,
1251 tx_timeout_work);
1252 int err;
1253
1254 rtnl_lock();
fd07ba16 1255 netdev_lock_ops(priv->dev);
50af5d12
JW
1256
1257 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
1258 goto unlock;
1259
1260 ipoib_stop(priv->dev);
1261 err = ipoib_open(priv->dev);
1262 if (err) {
1263 ipoib_warn(priv, "ipoib_open failed recovering from a tx_timeout, err(%d).\n",
1264 err);
1265 goto unlock;
1266 }
1267
1268 netif_tx_wake_all_queues(priv->dev);
1269unlock:
fd07ba16 1270 netdev_unlock_ops(priv->dev);
50af5d12
JW
1271 rtnl_unlock();
1272
1da177e4
LT
1273}
1274
1275static int ipoib_hard_header(struct sk_buff *skb,
1276 struct net_device *dev,
1277 unsigned short type,
0578cdad
KH
1278 const void *daddr,
1279 const void *saddr,
1280 unsigned int len)
1da177e4
LT
1281{
1282 struct ipoib_header *header;
1283
b1b63970 1284 header = skb_push(skb, sizeof(*header));
1da177e4
LT
1285
1286 header->proto = htons(type);
1287 header->reserved = 0;
1288
1289 /*
b63b70d8 1290 * we don't rely on dst_entry structure, always stuff the
fc791b63 1291 * destination address into skb hard header so we can figure out where
936d7de3 1292 * to send the packet later.
1da177e4 1293 */
2b084176 1294 push_pseudo_header(skb, daddr);
1da177e4 1295
fc791b63 1296 return IPOIB_HARD_LEN;
1da177e4
LT
1297}
1298
1299static void ipoib_set_mcast_list(struct net_device *dev)
1300{
c1048aff 1301 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1da177e4 1302
7a343d4c
LA
1303 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
1304 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
1305 return;
1306 }
1307
0b39578b 1308 queue_work(priv->wq, &priv->restart_task);
1da177e4
LT
1309}
1310
5aa7add8
ND
1311static int ipoib_get_iflink(const struct net_device *dev)
1312{
c1048aff 1313 struct ipoib_dev_priv *priv = ipoib_priv(dev);
5aa7add8 1314
2c153959
ES
1315 /* parent interface */
1316 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
e353ea9c 1317 return READ_ONCE(dev->ifindex);
2c153959
ES
1318
1319 /* child/vlan interface */
e353ea9c 1320 return READ_ONCE(priv->parent->ifindex);
5aa7add8
ND
1321}
1322
b63b70d8 1323static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
1da177e4 1324{
b63b70d8
SP
1325 /*
1326 * Use only the address parts that contributes to spreading
1327 * The subnet prefix is not used as one can not connect to
1328 * same remote port (GUID) using the same remote QPN via two
1329 * different subnets.
1330 */
1331 /* qpn octets[1:4) & port GUID octets[12:20) */
9d1ad66e 1332 u32 *d32 = (u32 *) daddr;
b63b70d8
SP
1333 u32 hv;
1334
9d1ad66e 1335 hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
b63b70d8
SP
1336 return hv & htbl->mask;
1337}
1338
1339struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
1340{
c1048aff 1341 struct ipoib_dev_priv *priv = ipoib_priv(dev);
b63b70d8
SP
1342 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1343 struct ipoib_neigh_hash *htbl;
1344 struct ipoib_neigh *neigh = NULL;
1345 u32 hash_val;
1346
1347 rcu_read_lock_bh();
1348
1349 htbl = rcu_dereference_bh(ntbl->htbl);
1350
1351 if (!htbl)
1352 goto out_unlock;
1353
1354 hash_val = ipoib_addr_hash(htbl, daddr);
1355 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
1356 neigh != NULL;
1357 neigh = rcu_dereference_bh(neigh->hnext)) {
1358 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1359 /* found, take one ref on behalf of the caller */
a5e27fb6 1360 if (!refcount_inc_not_zero(&neigh->refcnt)) {
b63b70d8
SP
1361 /* deleted */
1362 neigh = NULL;
1363 goto out_unlock;
1364 }
61c78eea
ES
1365
1366 if (likely(skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE))
1367 neigh->alive = jiffies;
b63b70d8
SP
1368 goto out_unlock;
1369 }
1370 }
1371
1372out_unlock:
1373 rcu_read_unlock_bh();
1374 return neigh;
1375}
1376
1377static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
1378{
1379 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1380 struct ipoib_neigh_hash *htbl;
1381 unsigned long neigh_obsolete;
1382 unsigned long dt;
1da177e4 1383 unsigned long flags;
b63b70d8 1384 int i;
bd99b2e0 1385 LIST_HEAD(remove_list);
1da177e4 1386
b5120a6e 1387 spin_lock_irqsave(&priv->lock, flags);
b63b70d8
SP
1388
1389 htbl = rcu_dereference_protected(ntbl->htbl,
b5120a6e 1390 lockdep_is_held(&priv->lock));
b63b70d8
SP
1391
1392 if (!htbl)
1393 goto out_unlock;
1394
1395 /* neigh is obsolete if it was idle for two GC periods */
1396 dt = 2 * arp_tbl.gc_interval;
1397 neigh_obsolete = jiffies - dt;
b63b70d8
SP
1398
1399 for (i = 0; i < htbl->size; i++) {
1400 struct ipoib_neigh *neigh;
1401 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1402
1403 while ((neigh = rcu_dereference_protected(*np,
b5120a6e 1404 lockdep_is_held(&priv->lock))) != NULL) {
b63b70d8
SP
1405 /* was the neigh idle for two GC periods */
1406 if (time_after(neigh_obsolete, neigh->alive)) {
bd99b2e0 1407
432c55ff 1408 ipoib_check_and_add_mcast_sendonly(priv, neigh->daddr + 4, &remove_list);
bd99b2e0 1409
b63b70d8
SP
1410 rcu_assign_pointer(*np,
1411 rcu_dereference_protected(neigh->hnext,
b5120a6e 1412 lockdep_is_held(&priv->lock)));
b63b70d8 1413 /* remove from path/mc list */
c586071d 1414 list_del_init(&neigh->list);
b63b70d8
SP
1415 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1416 } else {
1417 np = &neigh->hnext;
1418 }
1da177e4 1419
b63b70d8
SP
1420 }
1421 }
1da177e4 1422
b63b70d8 1423out_unlock:
b5120a6e 1424 spin_unlock_irqrestore(&priv->lock, flags);
50be28de 1425 ipoib_mcast_remove_list(&remove_list);
b63b70d8 1426}
1da177e4 1427
b63b70d8
SP
1428static void ipoib_reap_neigh(struct work_struct *work)
1429{
1430 struct ipoib_dev_priv *priv =
1431 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
1432
1433 __ipoib_reap_neigh(priv);
1434
cda8daf1
ES
1435 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1436 arp_tbl.gc_interval);
1da177e4
LT
1437}
1438
b63b70d8
SP
1439
1440static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
732a2170 1441 struct net_device *dev)
d2e0655e
MT
1442{
1443 struct ipoib_neigh *neigh;
1444
b1b63970 1445 neigh = kzalloc(sizeof(*neigh), GFP_ATOMIC);
d2e0655e
MT
1446 if (!neigh)
1447 return NULL;
1448
732a2170 1449 neigh->dev = dev;
b63b70d8 1450 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
82b39913 1451 skb_queue_head_init(&neigh->queue);
b63b70d8 1452 INIT_LIST_HEAD(&neigh->list);
839fcaba 1453 ipoib_cm_set(neigh, NULL);
b63b70d8 1454 /* one ref on behalf of the caller */
a5e27fb6 1455 refcount_set(&neigh->refcnt, 1);
b63b70d8
SP
1456
1457 return neigh;
1458}
1459
1460struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
1461 struct net_device *dev)
1462{
c1048aff 1463 struct ipoib_dev_priv *priv = ipoib_priv(dev);
b63b70d8
SP
1464 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1465 struct ipoib_neigh_hash *htbl;
1466 struct ipoib_neigh *neigh;
1467 u32 hash_val;
1468
b63b70d8 1469 htbl = rcu_dereference_protected(ntbl->htbl,
b5120a6e 1470 lockdep_is_held(&priv->lock));
b63b70d8
SP
1471 if (!htbl) {
1472 neigh = NULL;
1473 goto out_unlock;
1474 }
1475
1476 /* need to add a new neigh, but maybe some other thread succeeded?
1477 * recalc hash, maybe hash resize took place so we do a search
1478 */
1479 hash_val = ipoib_addr_hash(htbl, daddr);
1480 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
b5120a6e 1481 lockdep_is_held(&priv->lock));
b63b70d8
SP
1482 neigh != NULL;
1483 neigh = rcu_dereference_protected(neigh->hnext,
b5120a6e 1484 lockdep_is_held(&priv->lock))) {
b63b70d8
SP
1485 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1486 /* found, take one ref on behalf of the caller */
a5e27fb6 1487 if (!refcount_inc_not_zero(&neigh->refcnt)) {
b63b70d8
SP
1488 /* deleted */
1489 neigh = NULL;
1490 break;
1491 }
1492 neigh->alive = jiffies;
1493 goto out_unlock;
1494 }
1495 }
1496
1497 neigh = ipoib_neigh_ctor(daddr, dev);
1498 if (!neigh)
1499 goto out_unlock;
1500
1501 /* one ref on behalf of the hash table */
a5e27fb6 1502 refcount_inc(&neigh->refcnt);
b63b70d8
SP
1503 neigh->alive = jiffies;
1504 /* put in hash */
1505 rcu_assign_pointer(neigh->hnext,
1506 rcu_dereference_protected(htbl->buckets[hash_val],
b5120a6e 1507 lockdep_is_held(&priv->lock)));
b63b70d8
SP
1508 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1509 atomic_inc(&ntbl->entries);
1510
1511out_unlock:
d2e0655e
MT
1512
1513 return neigh;
1514}
1515
b63b70d8 1516void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
d2e0655e 1517{
b63b70d8
SP
1518 /* neigh reference count was dropprd to zero */
1519 struct net_device *dev = neigh->dev;
c1048aff 1520 struct ipoib_dev_priv *priv = ipoib_priv(dev);
2745b5b7 1521 struct sk_buff *skb;
b63b70d8
SP
1522 if (neigh->ah)
1523 ipoib_put_ah(neigh->ah);
2745b5b7 1524 while ((skb = __skb_dequeue(&neigh->queue))) {
de903512 1525 ++dev->stats.tx_dropped;
2745b5b7
MT
1526 dev_kfree_skb_any(skb);
1527 }
839fcaba
MT
1528 if (ipoib_cm_get(neigh))
1529 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
c1048aff 1530 ipoib_dbg(ipoib_priv(dev),
b63b70d8
SP
1531 "neigh free for %06x %pI6\n",
1532 IPOIB_QPN(neigh->daddr),
1533 neigh->daddr + 4);
d2e0655e 1534 kfree(neigh);
b63b70d8
SP
1535 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1536 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1537 complete(&priv->ntbl.flushed);
1538 }
1539}
1540
1541static void ipoib_neigh_reclaim(struct rcu_head *rp)
1542{
1543 /* Called as a result of removal from hash table */
1544 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1545 /* note TX context may hold another ref */
1546 ipoib_neigh_put(neigh);
d2e0655e
MT
1547}
1548
b63b70d8 1549void ipoib_neigh_free(struct ipoib_neigh *neigh)
1da177e4 1550{
b63b70d8 1551 struct net_device *dev = neigh->dev;
c1048aff 1552 struct ipoib_dev_priv *priv = ipoib_priv(dev);
b63b70d8
SP
1553 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1554 struct ipoib_neigh_hash *htbl;
1555 struct ipoib_neigh __rcu **np;
1556 struct ipoib_neigh *n;
1557 u32 hash_val;
1558
b63b70d8 1559 htbl = rcu_dereference_protected(ntbl->htbl,
b5120a6e 1560 lockdep_is_held(&priv->lock));
b63b70d8 1561 if (!htbl)
b5120a6e 1562 return;
b63b70d8
SP
1563
1564 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1565 np = &htbl->buckets[hash_val];
1566 for (n = rcu_dereference_protected(*np,
b5120a6e 1567 lockdep_is_held(&priv->lock));
b63b70d8 1568 n != NULL;
6c723a68 1569 n = rcu_dereference_protected(*np,
b5120a6e 1570 lockdep_is_held(&priv->lock))) {
b63b70d8
SP
1571 if (n == neigh) {
1572 /* found */
1573 rcu_assign_pointer(*np,
1574 rcu_dereference_protected(neigh->hnext,
b5120a6e 1575 lockdep_is_held(&priv->lock)));
49b8e744 1576 /* remove from parent list */
c586071d 1577 list_del_init(&neigh->list);
b63b70d8 1578 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
b5120a6e 1579 return;
b63b70d8
SP
1580 } else {
1581 np = &n->hnext;
1582 }
1583 }
b63b70d8
SP
1584}
1585
1586static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1587{
1588 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1589 struct ipoib_neigh_hash *htbl;
52374967 1590 struct ipoib_neigh __rcu **buckets;
b63b70d8
SP
1591 u32 size;
1592
1593 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1594 ntbl->htbl = NULL;
b63b70d8
SP
1595 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1596 if (!htbl)
1597 return -ENOMEM;
b63b70d8 1598 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
259e1914 1599 buckets = kvcalloc(size, sizeof(*buckets), GFP_KERNEL);
b63b70d8
SP
1600 if (!buckets) {
1601 kfree(htbl);
1602 return -ENOMEM;
1603 }
1604 htbl->size = size;
1605 htbl->mask = (size - 1);
1606 htbl->buckets = buckets;
52374967 1607 RCU_INIT_POINTER(ntbl->htbl, htbl);
66172c09 1608 htbl->ntbl = ntbl;
b63b70d8
SP
1609 atomic_set(&ntbl->entries, 0);
1610
1611 /* start garbage collection */
0b39578b 1612 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
b63b70d8 1613 arp_tbl.gc_interval);
1da177e4
LT
1614
1615 return 0;
1616}
1617
b63b70d8
SP
1618static void neigh_hash_free_rcu(struct rcu_head *head)
1619{
1620 struct ipoib_neigh_hash *htbl = container_of(head,
1621 struct ipoib_neigh_hash,
1622 rcu);
1623 struct ipoib_neigh __rcu **buckets = htbl->buckets;
66172c09 1624 struct ipoib_neigh_table *ntbl = htbl->ntbl;
b63b70d8 1625
259e1914 1626 kvfree(buckets);
b63b70d8 1627 kfree(htbl);
66172c09 1628 complete(&ntbl->deleted);
b63b70d8
SP
1629}
1630
1631void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1632{
c1048aff 1633 struct ipoib_dev_priv *priv = ipoib_priv(dev);
b63b70d8
SP
1634 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1635 struct ipoib_neigh_hash *htbl;
1636 unsigned long flags;
1637 int i;
1638
1639 /* remove all neigh connected to a given path or mcast */
b5120a6e 1640 spin_lock_irqsave(&priv->lock, flags);
b63b70d8
SP
1641
1642 htbl = rcu_dereference_protected(ntbl->htbl,
b5120a6e 1643 lockdep_is_held(&priv->lock));
b63b70d8
SP
1644
1645 if (!htbl)
1646 goto out_unlock;
1647
1648 for (i = 0; i < htbl->size; i++) {
1649 struct ipoib_neigh *neigh;
1650 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1651
1652 while ((neigh = rcu_dereference_protected(*np,
b5120a6e 1653 lockdep_is_held(&priv->lock))) != NULL) {
b63b70d8
SP
1654 /* delete neighs belong to this parent */
1655 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1656 rcu_assign_pointer(*np,
1657 rcu_dereference_protected(neigh->hnext,
b5120a6e 1658 lockdep_is_held(&priv->lock)));
b63b70d8 1659 /* remove from parent list */
c586071d 1660 list_del_init(&neigh->list);
b63b70d8
SP
1661 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1662 } else {
1663 np = &neigh->hnext;
1664 }
1665
1666 }
1667 }
1668out_unlock:
b5120a6e 1669 spin_unlock_irqrestore(&priv->lock, flags);
b63b70d8
SP
1670}
1671
1672static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1673{
1674 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1675 struct ipoib_neigh_hash *htbl;
1676 unsigned long flags;
66172c09 1677 int i, wait_flushed = 0;
b63b70d8 1678
66172c09 1679 init_completion(&priv->ntbl.flushed);
d2e46fcc 1680 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
b63b70d8 1681
b5120a6e 1682 spin_lock_irqsave(&priv->lock, flags);
b63b70d8
SP
1683
1684 htbl = rcu_dereference_protected(ntbl->htbl,
b5120a6e 1685 lockdep_is_held(&priv->lock));
b63b70d8
SP
1686 if (!htbl)
1687 goto out_unlock;
1688
66172c09
SP
1689 wait_flushed = atomic_read(&priv->ntbl.entries);
1690 if (!wait_flushed)
1691 goto free_htbl;
1692
b63b70d8
SP
1693 for (i = 0; i < htbl->size; i++) {
1694 struct ipoib_neigh *neigh;
1695 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1696
1697 while ((neigh = rcu_dereference_protected(*np,
b5120a6e 1698 lockdep_is_held(&priv->lock))) != NULL) {
b63b70d8
SP
1699 rcu_assign_pointer(*np,
1700 rcu_dereference_protected(neigh->hnext,
b5120a6e 1701 lockdep_is_held(&priv->lock)));
b63b70d8 1702 /* remove from path/mc list */
c586071d 1703 list_del_init(&neigh->list);
b63b70d8
SP
1704 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1705 }
1706 }
1707
66172c09 1708free_htbl:
b63b70d8
SP
1709 rcu_assign_pointer(ntbl->htbl, NULL);
1710 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1711
1712out_unlock:
b5120a6e 1713 spin_unlock_irqrestore(&priv->lock, flags);
66172c09
SP
1714 if (wait_flushed)
1715 wait_for_completion(&priv->ntbl.flushed);
b63b70d8
SP
1716}
1717
1718static void ipoib_neigh_hash_uninit(struct net_device *dev)
1719{
c1048aff 1720 struct ipoib_dev_priv *priv = ipoib_priv(dev);
b63b70d8 1721
0dd9ce18 1722 ipoib_dbg(priv, "%s\n", __func__);
66172c09 1723 init_completion(&priv->ntbl.deleted);
b63b70d8 1724
cda8daf1 1725 cancel_delayed_work_sync(&priv->neigh_reap_task);
b63b70d8 1726
66172c09
SP
1727 ipoib_flush_neighs(priv);
1728
1729 wait_for_completion(&priv->ntbl.deleted);
b63b70d8
SP
1730}
1731
8966e28d
ES
1732static void ipoib_napi_add(struct net_device *dev)
1733{
1734 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1735
2157f5ca
JK
1736 netif_napi_add_weight(dev, &priv->recv_napi, ipoib_rx_poll,
1737 IPOIB_NUM_WC);
1738 netif_napi_add_weight(dev, &priv->send_napi, ipoib_tx_poll,
1739 MAX_SEND_CQE);
8966e28d
ES
1740}
1741
1742static void ipoib_napi_del(struct net_device *dev)
1743{
1744 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1745
1746 netif_napi_del(&priv->recv_napi);
1747 netif_napi_del(&priv->send_napi);
1748}
1749
0a1a9726 1750static void ipoib_dev_uninit_default(struct net_device *dev)
515ed4f3 1751{
c1048aff 1752 struct ipoib_dev_priv *priv = ipoib_priv(dev);
b63b70d8 1753
515ed4f3
ES
1754 ipoib_transport_dev_cleanup(dev);
1755
8966e28d 1756 ipoib_napi_del(dev);
b53d4566 1757
515ed4f3
ES
1758 ipoib_cm_dev_cleanup(dev);
1759
1760 kfree(priv->rx_ring);
1761 vfree(priv->tx_ring);
1762
1763 priv->rx_ring = NULL;
1764 priv->tx_ring = NULL;
1765}
1766
cd565b4b 1767static int ipoib_dev_init_default(struct net_device *dev)
1da177e4 1768{
c1048aff 1769 struct ipoib_dev_priv *priv = ipoib_priv(dev);
10f7b9bc 1770 u8 addr_mod[3];
1da177e4 1771
8966e28d 1772 ipoib_napi_add(dev);
cd565b4b 1773
1da177e4 1774 /* Allocate RX/TX "rings" to hold queued skbs */
6396bb22
KC
1775 priv->rx_ring = kcalloc(ipoib_recvq_size,
1776 sizeof(*priv->rx_ring),
1777 GFP_KERNEL);
74226649 1778 if (!priv->rx_ring)
be7aa663 1779 goto out;
1da177e4 1780
fad953ce
KC
1781 priv->tx_ring = vzalloc(array_size(ipoib_sendq_size,
1782 sizeof(*priv->tx_ring)));
1da177e4 1783 if (!priv->tx_ring) {
c55359a2
YS
1784 pr_warn("%s: failed to allocate TX ring (%d entries)\n",
1785 priv->ca->name, ipoib_sendq_size);
1da177e4
LT
1786 goto out_rx_ring_cleanup;
1787 }
1da177e4 1788
1acba6a8 1789 /* priv->tx_head, tx_tail and global_tx_tail/head are already 0 */
1da177e4 1790
cd565b4b
ES
1791 if (ipoib_transport_dev_init(dev, priv->ca)) {
1792 pr_warn("%s: ipoib_transport_dev_init failed\n",
1793 priv->ca->name);
1da177e4 1794 goto out_tx_ring_cleanup;
515ed4f3
ES
1795 }
1796
cd565b4b 1797 /* after qp created set dev address */
10f7b9bc
JK
1798 addr_mod[0] = (priv->qp->qp_num >> 16) & 0xff;
1799 addr_mod[1] = (priv->qp->qp_num >> 8) & 0xff;
1800 addr_mod[2] = (priv->qp->qp_num) & 0xff;
1801 dev_addr_mod(priv->dev, 1, addr_mod, sizeof(addr_mod));
cd565b4b 1802
515ed4f3
ES
1803 return 0;
1804
1805out_tx_ring_cleanup:
1806 vfree(priv->tx_ring);
1807
1808out_rx_ring_cleanup:
1809 kfree(priv->rx_ring);
1810
1811out:
8966e28d 1812 ipoib_napi_del(dev);
515ed4f3
ES
1813 return -ENOMEM;
1814}
1815
31a82362
FD
1816static int ipoib_ioctl(struct net_device *dev, struct ifreq *ifr,
1817 int cmd)
1818{
1819 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1820
a7605370 1821 if (!priv->rn_ops->ndo_eth_ioctl)
31a82362
FD
1822 return -EOPNOTSUPP;
1823
a7605370 1824 return priv->rn_ops->ndo_eth_ioctl(dev, ifr, cmd);
31a82362
FD
1825}
1826
eaeb3984 1827static int ipoib_dev_init(struct net_device *dev)
515ed4f3 1828{
c1048aff 1829 struct ipoib_dev_priv *priv = ipoib_priv(dev);
515ed4f3
ES
1830 int ret = -ENOMEM;
1831
515ed4f3 1832 priv->qp = NULL;
1da177e4 1833
be7aa663 1834 /*
515ed4f3
ES
1835 * the various IPoIB tasks assume they will never race against
1836 * themselves, so always use a single thread workqueue
be7aa663 1837 */
515ed4f3
ES
1838 priv->wq = alloc_ordered_workqueue("ipoib_wq", WQ_MEM_RECLAIM);
1839 if (!priv->wq) {
1840 pr_warn("%s: failed to allocate device WQ\n", dev->name);
1841 goto out;
1842 }
1843
1844 /* create pd, which used both for control and datapath*/
1845 priv->pd = ib_alloc_pd(priv->ca, 0);
1846 if (IS_ERR(priv->pd)) {
eaeb3984 1847 pr_warn("%s: failed to allocate PD\n", priv->ca->name);
515ed4f3
ES
1848 goto clean_wq;
1849 }
1850
cd565b4b 1851 ret = priv->rn_ops->ndo_init(dev);
515ed4f3
ES
1852 if (ret) {
1853 pr_warn("%s failed to init HW resource\n", dev->name);
1854 goto out_free_pd;
1855 }
1856
99a7e2bf
WY
1857 ret = ipoib_neigh_hash_init(priv);
1858 if (ret) {
515ed4f3 1859 pr_warn("%s failed to init neigh hash\n", dev->name);
be7aa663 1860 goto out_dev_uninit;
515ed4f3
ES
1861 }
1862
1863 if (dev->flags & IFF_UP) {
1864 if (ipoib_ib_dev_open(dev)) {
1865 pr_warn("%s failed to open device\n", dev->name);
1866 ret = -ENODEV;
cda8daf1 1867 goto out_hash_uninit;
515ed4f3
ES
1868 }
1869 }
be7aa663 1870
1da177e4
LT
1871 return 0;
1872
cda8daf1
ES
1873out_hash_uninit:
1874 ipoib_neigh_hash_uninit(dev);
1875
be7aa663
DL
1876out_dev_uninit:
1877 ipoib_ib_dev_cleanup(dev);
1878
515ed4f3
ES
1879out_free_pd:
1880 if (priv->pd) {
1881 ib_dealloc_pd(priv->pd);
1882 priv->pd = NULL;
1883 }
1da177e4 1884
515ed4f3
ES
1885clean_wq:
1886 if (priv->wq) {
1887 destroy_workqueue(priv->wq);
1888 priv->wq = NULL;
1889 }
1da177e4
LT
1890
1891out:
515ed4f3 1892 return ret;
1da177e4
LT
1893}
1894
7cbee87c
JG
1895/*
1896 * This must be called before doing an unregister_netdev on a parent device to
1897 * shutdown the IB event handler.
1898 */
1899static void ipoib_parent_unregister_pre(struct net_device *ndev)
1900{
1901 struct ipoib_dev_priv *priv = ipoib_priv(ndev);
1902
1903 /*
1904 * ipoib_set_mac checks netif_running before pushing work, clearing
1905 * running ensures the it will not add more work.
1906 */
1907 rtnl_lock();
567c5e13 1908 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP, NULL);
7cbee87c
JG
1909 rtnl_unlock();
1910
1911 /* ipoib_event() cannot be running once this returns */
1912 ib_unregister_event_handler(&priv->event_handler);
1913
1914 /*
1915 * Work on the queue grabs the rtnl lock, so this cannot be done while
1916 * also holding it.
1917 */
1918 flush_workqueue(ipoib_workqueue);
1919}
1920
eaeb3984
JG
1921static void ipoib_set_dev_features(struct ipoib_dev_priv *priv)
1922{
1923 priv->hca_caps = priv->ca->attrs.device_cap_flags;
e945c653 1924 priv->kernel_caps = priv->ca->attrs.kernel_cap_flags;
eaeb3984
JG
1925
1926 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
1927 priv->dev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1928
e945c653 1929 if (priv->kernel_caps & IBK_UD_TSO)
eaeb3984
JG
1930 priv->dev->hw_features |= NETIF_F_TSO;
1931
1932 priv->dev->features |= priv->dev->hw_features;
1933 }
1934}
1935
1936static int ipoib_parent_init(struct net_device *ndev)
1937{
1938 struct ipoib_dev_priv *priv = ipoib_priv(ndev);
1939 struct ib_port_attr attr;
1940 int result;
1941
1942 result = ib_query_port(priv->ca, priv->port, &attr);
1943 if (result) {
1944 pr_warn("%s: ib_query_port %d failed\n", priv->ca->name,
1945 priv->port);
1946 return result;
1947 }
6d72344c 1948 priv->max_ib_mtu = rdma_mtu_from_attr(priv->ca, priv->port, &attr);
eaeb3984
JG
1949
1950 result = ib_query_pkey(priv->ca, priv->port, 0, &priv->pkey);
1951 if (result) {
1952 pr_warn("%s: ib_query_pkey port %d failed (ret = %d)\n",
1953 priv->ca->name, priv->port, result);
1954 return result;
1955 }
1956
1957 result = rdma_query_gid(priv->ca, priv->port, 0, &priv->local_gid);
1958 if (result) {
1959 pr_warn("%s: rdma_query_gid port %d failed (ret = %d)\n",
1960 priv->ca->name, priv->port, result);
1961 return result;
1962 }
10f7b9bc 1963 dev_addr_mod(priv->dev, 4, priv->local_gid.raw, sizeof(union ib_gid));
eaeb3984
JG
1964
1965 SET_NETDEV_DEV(priv->dev, priv->ca->dev.parent);
9b8b2a32
AM
1966 priv->dev->dev_port = priv->port - 1;
1967 /* Let's set this one too for backwards compatibility. */
eaeb3984
JG
1968 priv->dev->dev_id = priv->port - 1;
1969
1970 return 0;
1971}
1972
1973static void ipoib_child_init(struct net_device *ndev)
1974{
1975 struct ipoib_dev_priv *priv = ipoib_priv(ndev);
1976 struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
1977
1978 priv->max_ib_mtu = ppriv->max_ib_mtu;
1979 set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
87fb5c1c
MG
1980 if (memchr_inv(priv->dev->dev_addr, 0, INFINIBAND_ALEN))
1981 memcpy(&priv->local_gid, priv->dev->dev_addr + 4,
1982 sizeof(priv->local_gid));
1983 else {
10f7b9bc
JK
1984 __dev_addr_set(priv->dev, ppriv->dev->dev_addr,
1985 INFINIBAND_ALEN);
87fb5c1c
MG
1986 memcpy(&priv->local_gid, &ppriv->local_gid,
1987 sizeof(priv->local_gid));
1988 }
eaeb3984
JG
1989}
1990
1991static int ipoib_ndo_init(struct net_device *ndev)
1992{
1993 struct ipoib_dev_priv *priv = ipoib_priv(ndev);
1994 int rc;
b7e159eb 1995 struct rdma_netdev *rn = netdev_priv(ndev);
eaeb3984
JG
1996
1997 if (priv->parent) {
1998 ipoib_child_init(ndev);
1999 } else {
2000 rc = ipoib_parent_init(ndev);
2001 if (rc)
2002 return rc;
2003 }
2004
2005 /* MTU will be reset when mcast join happens */
2006 ndev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
2007 priv->mcast_mtu = priv->admin_mtu = ndev->mtu;
b7e159eb 2008 rn->mtu = priv->mcast_mtu;
eaeb3984
JG
2009 ndev->max_mtu = IPOIB_CM_MTU;
2010
2011 ndev->neigh_priv_len = sizeof(struct ipoib_neigh);
2012
2013 /*
2014 * Set the full membership bit, so that we join the right
2015 * broadcast group, etc.
2016 */
2017 priv->pkey |= 0x8000;
2018
2019 ndev->broadcast[8] = priv->pkey >> 8;
2020 ndev->broadcast[9] = priv->pkey & 0xff;
2021 set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
2022
2023 ipoib_set_dev_features(priv);
2024
2025 rc = ipoib_dev_init(ndev);
2026 if (rc) {
2027 pr_warn("%s: failed to initialize device: %s port %d (ret = %d)\n",
2028 priv->ca->name, priv->dev->name, priv->port, rc);
91b01061
VF
2029 return rc;
2030 }
2031
2032 if (priv->parent) {
2033 struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
2034
2035 dev_hold(priv->parent);
2036
463e5176 2037 netdev_lock(priv->parent);
91b01061 2038 list_add_tail(&priv->list, &ppriv->child_intfs);
463e5176 2039 netdev_unlock(priv->parent);
eaeb3984
JG
2040 }
2041
2042 return 0;
2043}
2044
7cbee87c 2045static void ipoib_ndo_uninit(struct net_device *dev)
1da177e4 2046{
25405d98 2047 struct ipoib_dev_priv *priv = ipoib_priv(dev);
9baa0b03 2048
25405d98
JG
2049 /*
2050 * ipoib_remove_one guarantees the children are removed before the
2051 * parent, and that is the only place where a parent can be removed.
2052 */
2053 WARN_ON(!list_empty(&priv->child_intfs));
1da177e4 2054
91b01061
VF
2055 if (priv->parent) {
2056 struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
2057
463e5176 2058 netdev_lock(ppriv->dev);
91b01061 2059 list_del(&priv->list);
463e5176 2060 netdev_unlock(ppriv->dev);
91b01061
VF
2061 }
2062
be7aa663
DL
2063 ipoib_neigh_hash_uninit(dev);
2064
1da177e4
LT
2065 ipoib_ib_dev_cleanup(dev);
2066
515ed4f3
ES
2067 /* no more works over the priv->wq */
2068 if (priv->wq) {
65936bf2
JG
2069 /* See ipoib_mcast_carrier_on_task() */
2070 WARN_ON(test_bit(IPOIB_FLAG_OPER_UP, &priv->flags));
515ed4f3
ES
2071 destroy_workqueue(priv->wq);
2072 priv->wq = NULL;
2073 }
13476d35 2074
7a1c2abf 2075 dev_put(priv->parent);
1da177e4
LT
2076}
2077
9c3c5f8e
EC
2078static int ipoib_set_vf_link_state(struct net_device *dev, int vf, int link_state)
2079{
c1048aff 2080 struct ipoib_dev_priv *priv = ipoib_priv(dev);
9c3c5f8e
EC
2081
2082 return ib_set_vf_link_state(priv->ca, vf, priv->port, link_state);
2083}
2084
2085static int ipoib_get_vf_config(struct net_device *dev, int vf,
2086 struct ifla_vf_info *ivf)
2087{
c1048aff 2088 struct ipoib_dev_priv *priv = ipoib_priv(dev);
9c3c5f8e
EC
2089 int err;
2090
2091 err = ib_get_vf_config(priv->ca, vf, priv->port, ivf);
2092 if (err)
2093 return err;
2094
2095 ivf->vf = vf;
64d701c6 2096 memcpy(ivf->mac, dev->dev_addr, dev->addr_len);
9c3c5f8e
EC
2097
2098 return 0;
2099}
2100
2101static int ipoib_set_vf_guid(struct net_device *dev, int vf, u64 guid, int type)
2102{
c1048aff 2103 struct ipoib_dev_priv *priv = ipoib_priv(dev);
9c3c5f8e
EC
2104
2105 if (type != IFLA_VF_IB_NODE_GUID && type != IFLA_VF_IB_PORT_GUID)
2106 return -EINVAL;
2107
2108 return ib_set_vf_guid(priv->ca, vf, priv->port, guid, type);
2109}
2110
2446887e
DG
2111static int ipoib_get_vf_guid(struct net_device *dev, int vf,
2112 struct ifla_vf_guid *node_guid,
2113 struct ifla_vf_guid *port_guid)
2114{
2115 struct ipoib_dev_priv *priv = ipoib_priv(dev);
2116
2117 return ib_get_vf_guid(priv->ca, vf, priv->port, node_guid, port_guid);
2118}
2119
9c3c5f8e
EC
2120static int ipoib_get_vf_stats(struct net_device *dev, int vf,
2121 struct ifla_vf_stats *vf_stats)
2122{
c1048aff 2123 struct ipoib_dev_priv *priv = ipoib_priv(dev);
9c3c5f8e
EC
2124
2125 return ib_get_vf_stats(priv->ca, vf, priv->port, vf_stats);
2126}
2127
3b04ddde
SH
2128static const struct header_ops ipoib_header_ops = {
2129 .create = ipoib_hard_header,
2130};
2131
9c3c5f8e 2132static const struct net_device_ops ipoib_netdev_ops_pf = {
eaeb3984 2133 .ndo_init = ipoib_ndo_init,
7cbee87c 2134 .ndo_uninit = ipoib_ndo_uninit,
9c3c5f8e
EC
2135 .ndo_open = ipoib_open,
2136 .ndo_stop = ipoib_stop,
2137 .ndo_change_mtu = ipoib_change_mtu,
2138 .ndo_fix_features = ipoib_fix_features,
2139 .ndo_start_xmit = ipoib_start_xmit,
2140 .ndo_tx_timeout = ipoib_timeout,
2141 .ndo_set_rx_mode = ipoib_set_mcast_list,
2142 .ndo_get_iflink = ipoib_get_iflink,
2143 .ndo_set_vf_link_state = ipoib_set_vf_link_state,
2144 .ndo_get_vf_config = ipoib_get_vf_config,
2145 .ndo_get_vf_stats = ipoib_get_vf_stats,
2446887e 2146 .ndo_get_vf_guid = ipoib_get_vf_guid,
9c3c5f8e 2147 .ndo_set_vf_guid = ipoib_set_vf_guid,
492a7e67 2148 .ndo_set_mac_address = ipoib_set_mac,
b6c871e5 2149 .ndo_get_stats64 = ipoib_get_stats,
a7605370 2150 .ndo_eth_ioctl = ipoib_ioctl,
9c3c5f8e
EC
2151};
2152
2153static const struct net_device_ops ipoib_netdev_ops_vf = {
eaeb3984 2154 .ndo_init = ipoib_ndo_init,
7cbee87c 2155 .ndo_uninit = ipoib_ndo_uninit,
fe8114e8
SH
2156 .ndo_open = ipoib_open,
2157 .ndo_stop = ipoib_stop,
2158 .ndo_change_mtu = ipoib_change_mtu,
3d96c74d 2159 .ndo_fix_features = ipoib_fix_features,
fe8114e8
SH
2160 .ndo_start_xmit = ipoib_start_xmit,
2161 .ndo_tx_timeout = ipoib_timeout,
afc4b13d 2162 .ndo_set_rx_mode = ipoib_set_mcast_list,
5aa7add8 2163 .ndo_get_iflink = ipoib_get_iflink,
eb54714d 2164 .ndo_get_stats64 = ipoib_get_stats,
a7605370 2165 .ndo_eth_ioctl = ipoib_ioctl,
fe8114e8
SH
2166};
2167
8f149b68
GL
2168static const struct net_device_ops ipoib_netdev_default_pf = {
2169 .ndo_init = ipoib_dev_init_default,
2170 .ndo_uninit = ipoib_dev_uninit_default,
2171 .ndo_open = ipoib_ib_dev_open_default,
2172 .ndo_stop = ipoib_ib_dev_stop_default,
2173};
2174
cd565b4b 2175void ipoib_setup_common(struct net_device *dev)
1da177e4 2176{
2337f809 2177 dev->header_ops = &ipoib_header_ops;
8f149b68 2178 dev->netdev_ops = &ipoib_netdev_default_pf;
bea3348e 2179
82c24c18
EC
2180 ipoib_set_ethtool_ops(dev);
2181
50af5d12 2182 dev->watchdog_timeo = 10 * HZ;
1da177e4 2183
2337f809 2184 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1da177e4 2185
fc791b63 2186 dev->hard_header_len = IPOIB_HARD_LEN;
2337f809
RD
2187 dev->addr_len = INFINIBAND_ALEN;
2188 dev->type = ARPHRD_INFINIBAND;
c11db1bf 2189 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
eb14032f 2190 dev->features = (NETIF_F_VLAN_CHALLENGED |
eb14032f 2191 NETIF_F_HIGHDMA);
02875878 2192 netif_keep_dst(dev);
1da177e4 2193
1da177e4 2194 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
9f49a5b5
JG
2195
2196 /*
2197 * unregister_netdev always frees the netdev, we use this mode
2198 * consistently to unify all the various unregister paths, including
2199 * those connected to rtnl_link_ops which require it.
2200 */
2201 dev->needs_free_netdev = true;
cd565b4b 2202}
1da177e4 2203
cd565b4b
ES
2204static void ipoib_build_priv(struct net_device *dev)
2205{
2206 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1da177e4 2207
cd565b4b 2208 priv->dev = dev;
1da177e4 2209 spin_lock_init(&priv->lock);
edf3f301 2210 mutex_init(&priv->mcast_mutex);
1da177e4
LT
2211
2212 INIT_LIST_HEAD(&priv->path_list);
2213 INIT_LIST_HEAD(&priv->child_intfs);
2214 INIT_LIST_HEAD(&priv->dead_ahs);
2215 INIT_LIST_HEAD(&priv->multicast_list);
2216
c4028958 2217 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
e8224e4b 2218 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
50af5d12 2219 INIT_WORK(&priv->reschedule_napi_work, ipoib_napi_schedule_work);
ee1e2c82
MS
2220 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
2221 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
2222 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
c4028958 2223 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
50af5d12 2224 INIT_WORK(&priv->tx_timeout_work, ipoib_ib_tx_timeout_work);
c4028958 2225 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
b63b70d8 2226 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
1da177e4
LT
2227}
2228
1fb7f897 2229static struct net_device *ipoib_alloc_netdev(struct ib_device *hca, u32 port,
5d6b0cb3 2230 const char *name)
cd565b4b
ES
2231{
2232 struct net_device *dev;
2233
f6a8a19b
DD
2234 dev = rdma_alloc_netdev(hca, port, RDMA_NETDEV_IPOIB, name,
2235 NET_NAME_UNKNOWN, ipoib_setup_common);
5d6b0cb3 2236 if (!IS_ERR(dev) || PTR_ERR(dev) != -EOPNOTSUPP)
f6a8a19b 2237 return dev;
cd565b4b 2238
5d6b0cb3
DD
2239 dev = alloc_netdev(sizeof(struct rdma_netdev), name, NET_NAME_UNKNOWN,
2240 ipoib_setup_common);
2241 if (!dev)
2242 return ERR_PTR(-ENOMEM);
2243 return dev;
cd565b4b
ES
2244}
2245
1fb7f897 2246int ipoib_intf_init(struct ib_device *hca, u32 port, const char *name,
5d6b0cb3 2247 struct net_device *dev)
cd565b4b 2248{
5d6b0cb3 2249 struct rdma_netdev *rn = netdev_priv(dev);
cd565b4b 2250 struct ipoib_dev_priv *priv;
5d6b0cb3 2251 int rc;
cd565b4b
ES
2252
2253 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
2254 if (!priv)
5d6b0cb3 2255 return -ENOMEM;
cd565b4b 2256
eaeb3984
JG
2257 priv->ca = hca;
2258 priv->port = port;
2259
5d6b0cb3
DD
2260 rc = rdma_init_netdev(hca, port, RDMA_NETDEV_IPOIB, name,
2261 NET_NAME_UNKNOWN, ipoib_setup_common, dev);
2262 if (rc) {
2263 if (rc != -EOPNOTSUPP)
2264 goto out;
2265
5d6b0cb3
DD
2266 rn->send = ipoib_send;
2267 rn->attach_mcast = ipoib_mcast_attach;
2268 rn->detach_mcast = ipoib_mcast_detach;
2269 rn->hca = hca;
e632291a
DT
2270
2271 rc = netif_set_real_num_tx_queues(dev, 1);
2272 if (rc)
2273 goto out;
2274
2275 rc = netif_set_real_num_rx_queues(dev, 1);
2276 if (rc)
2277 goto out;
5d6b0cb3 2278 }
cd565b4b
ES
2279
2280 priv->rn_ops = dev->netdev_ops;
2281
e945c653 2282 if (hca->attrs.kernel_cap_flags & IBK_VIRTUAL_FUNCTION)
cd565b4b
ES
2283 dev->netdev_ops = &ipoib_netdev_ops_vf;
2284 else
2285 dev->netdev_ops = &ipoib_netdev_ops_pf;
2286
cd565b4b 2287 rn->clnt_priv = priv;
9f49a5b5
JG
2288 /*
2289 * Only the child register_netdev flows can handle priv_destructor
2290 * being set, so we force it to NULL here and handle manually until it
2291 * is safe to turn on.
2292 */
2293 priv->next_priv_destructor = dev->priv_destructor;
2294 dev->priv_destructor = NULL;
2295
cd565b4b
ES
2296 ipoib_build_priv(dev);
2297
5d6b0cb3
DD
2298 return 0;
2299
2300out:
cd565b4b 2301 kfree(priv);
5d6b0cb3
DD
2302 return rc;
2303}
2304
1fb7f897 2305struct net_device *ipoib_intf_alloc(struct ib_device *hca, u32 port,
5d6b0cb3
DD
2306 const char *name)
2307{
2308 struct net_device *dev;
2309 int rc;
2310
2311 dev = ipoib_alloc_netdev(hca, port, name);
2312 if (IS_ERR(dev))
2313 return dev;
2314
2315 rc = ipoib_intf_init(hca, port, name, dev);
2316 if (rc) {
2317 free_netdev(dev);
2318 return ERR_PTR(rc);
2319 }
2320
2321 /*
2322 * Upon success the caller must ensure ipoib_intf_free is called or
2323 * register_netdevice succeed'd and priv_destructor is set to
2324 * ipoib_intf_free.
2325 */
2326 return dev;
1da177e4
LT
2327}
2328
9f49a5b5
JG
2329void ipoib_intf_free(struct net_device *dev)
2330{
2331 struct ipoib_dev_priv *priv = ipoib_priv(dev);
2332 struct rdma_netdev *rn = netdev_priv(dev);
2333
2334 dev->priv_destructor = priv->next_priv_destructor;
2335 if (dev->priv_destructor)
2336 dev->priv_destructor(dev);
2337
2338 /*
2339 * There are some error flows around register_netdev failing that may
2340 * attempt to call priv_destructor twice, prevent that from happening.
2341 */
2342 dev->priv_destructor = NULL;
2343
2344 /* unregister/destroy is very complicated. Make bugs more obvious. */
2345 rn->clnt_priv = NULL;
2346
2347 kfree(priv);
2348}
2349
1f8f60f3
Y
2350static ssize_t pkey_show(struct device *dev, struct device_attribute *attr,
2351 char *buf)
1da177e4 2352{
c1048aff
ES
2353 struct net_device *ndev = to_net_dev(dev);
2354 struct ipoib_dev_priv *priv = ipoib_priv(ndev);
1da177e4 2355
1c7fd726 2356 return sysfs_emit(buf, "0x%04x\n", priv->pkey);
1da177e4 2357}
1f8f60f3 2358static DEVICE_ATTR_RO(pkey);
1da177e4 2359
1f8f60f3
Y
2360static ssize_t umcast_show(struct device *dev, struct device_attribute *attr,
2361 char *buf)
335a64a5 2362{
c1048aff
ES
2363 struct net_device *ndev = to_net_dev(dev);
2364 struct ipoib_dev_priv *priv = ipoib_priv(ndev);
335a64a5 2365
1c7fd726
JP
2366 return sysfs_emit(buf, "%d\n",
2367 test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
335a64a5
OG
2368}
2369
862096a8 2370void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
335a64a5 2371{
c1048aff 2372 struct ipoib_dev_priv *priv = ipoib_priv(ndev);
335a64a5
OG
2373
2374 if (umcast_val > 0) {
2375 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
2376 ipoib_warn(priv, "ignoring multicast groups joined directly "
2377 "by userspace\n");
2378 } else
2379 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
862096a8
OG
2380}
2381
1f8f60f3
Y
2382static ssize_t umcast_store(struct device *dev, struct device_attribute *attr,
2383 const char *buf, size_t count)
862096a8
OG
2384{
2385 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
2386
2387 ipoib_set_umcast(to_net_dev(dev), umcast_val);
335a64a5
OG
2388
2389 return count;
2390}
1f8f60f3 2391static DEVICE_ATTR_RW(umcast);
335a64a5
OG
2392
2393int ipoib_add_umcast_attr(struct net_device *dev)
2394{
2395 return device_create_file(&dev->dev, &dev_attr_umcast);
2396}
2397
492a7e67
MB
2398static void set_base_guid(struct ipoib_dev_priv *priv, union ib_gid *gid)
2399{
2400 struct ipoib_dev_priv *child_priv;
2401 struct net_device *netdev = priv->dev;
2402
9b29953b 2403 netif_addr_lock_bh(netdev);
492a7e67
MB
2404
2405 memcpy(&priv->local_gid.global.interface_id,
2406 &gid->global.interface_id,
2407 sizeof(gid->global.interface_id));
10f7b9bc 2408 dev_addr_mod(netdev, 4, (u8 *)&priv->local_gid, sizeof(priv->local_gid));
492a7e67
MB
2409 clear_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
2410
9b29953b 2411 netif_addr_unlock_bh(netdev);
492a7e67
MB
2412
2413 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
fd07ba16 2414 netdev_lock_ops_to_full(priv->dev);
492a7e67
MB
2415 list_for_each_entry(child_priv, &priv->child_intfs, list)
2416 set_base_guid(child_priv, gid);
fd07ba16 2417 netdev_unlock_full_to_ops(priv->dev);
492a7e67
MB
2418 }
2419}
2420
2421static int ipoib_check_lladdr(struct net_device *dev,
2422 struct sockaddr_storage *ss)
2423{
2424 union ib_gid *gid = (union ib_gid *)(ss->__data + 4);
2425 int ret = 0;
2426
9b29953b 2427 netif_addr_lock_bh(dev);
492a7e67
MB
2428
2429 /* Make sure the QPN, reserved and subnet prefix match the current
2430 * lladdr, it also makes sure the lladdr is unicast.
2431 */
2432 if (memcmp(dev->dev_addr, ss->__data,
2433 4 + sizeof(gid->global.subnet_prefix)) ||
2434 gid->global.interface_id == 0)
2435 ret = -EINVAL;
2436
9b29953b 2437 netif_addr_unlock_bh(dev);
492a7e67
MB
2438
2439 return ret;
2440}
2441
2442static int ipoib_set_mac(struct net_device *dev, void *addr)
2443{
c1048aff 2444 struct ipoib_dev_priv *priv = ipoib_priv(dev);
492a7e67
MB
2445 struct sockaddr_storage *ss = addr;
2446 int ret;
2447
2448 if (!(dev->priv_flags & IFF_LIVE_ADDR_CHANGE) && netif_running(dev))
2449 return -EBUSY;
2450
2451 ret = ipoib_check_lladdr(dev, ss);
2452 if (ret)
2453 return ret;
2454
2455 set_base_guid(priv, (union ib_gid *)(ss->__data + 4));
2456
5f85120e
CR
2457 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
2458 struct ipoib_dev_priv *cpriv;
2459
fd07ba16 2460 netdev_lock_ops_to_full(dev);
5f85120e
CR
2461 list_for_each_entry(cpriv, &priv->child_intfs, list)
2462 queue_work(ipoib_workqueue, &cpriv->flush_light);
fd07ba16 2463 netdev_unlock_full_to_ops(dev);
5f85120e 2464 }
492a7e67
MB
2465 queue_work(ipoib_workqueue, &priv->flush_light);
2466
2467 return 0;
2468}
2469
1f8f60f3
Y
2470static ssize_t create_child_store(struct device *dev,
2471 struct device_attribute *attr,
2472 const char *buf, size_t count)
1da177e4
LT
2473{
2474 int pkey;
2475 int ret;
2476
2477 if (sscanf(buf, "%i", &pkey) != 1)
2478 return -EINVAL;
2479
3d790a4c 2480 if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
1da177e4
LT
2481 return -EINVAL;
2482
43cb76d9 2483 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
1da177e4
LT
2484
2485 return ret ? ret : count;
2486}
1f8f60f3 2487static DEVICE_ATTR_WO(create_child);
1da177e4 2488
1f8f60f3
Y
2489static ssize_t delete_child_store(struct device *dev,
2490 struct device_attribute *attr,
2491 const char *buf, size_t count)
1da177e4
LT
2492{
2493 int pkey;
2494 int ret;
2495
2496 if (sscanf(buf, "%i", &pkey) != 1)
2497 return -EINVAL;
2498
2499 if (pkey < 0 || pkey > 0xffff)
2500 return -EINVAL;
2501
43cb76d9 2502 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
1da177e4
LT
2503
2504 return ret ? ret : count;
2505
2506}
1f8f60f3 2507static DEVICE_ATTR_WO(delete_child);
1da177e4
LT
2508
2509int ipoib_add_pkey_attr(struct net_device *dev)
2510{
43cb76d9 2511 return device_create_file(&dev->dev, &dev_attr_pkey);
1da177e4
LT
2512}
2513
f6350da4
AM
2514/*
2515 * We erroneously exposed the iface's port number in the dev_id
2516 * sysfs field long after dev_port was introduced for that purpose[1],
2517 * and we need to stop everyone from relying on that.
2518 * Let's overload the shower routine for the dev_id file here
2519 * to gently bring the issue up.
2520 *
2521 * [1] https://www.spinics.net/lists/netdev/msg272123.html
2522 */
2523static ssize_t dev_id_show(struct device *dev,
2524 struct device_attribute *attr, char *buf)
2525{
2526 struct net_device *ndev = to_net_dev(dev);
2527
b79656ed
LR
2528 /*
2529 * ndev->dev_port will be equal to 0 in old kernel prior to commit
2530 * 9b8b2a323008 ("IB/ipoib: Use dev_port to expose network interface
2531 * port numbers") Zero was chosen as special case for user space
2532 * applications to fallback and query dev_id to check if it has
2533 * different value or not.
2534 *
2535 * Don't print warning in such scenario.
2536 *
2537 * https://github.com/systemd/systemd/blob/master/src/udev/udev-builtin-net_id.c#L358
2538 */
2539 if (ndev->dev_port && ndev->dev_id == ndev->dev_port)
f6350da4
AM
2540 netdev_info_once(ndev,
2541 "\"%s\" wants to know my dev_id. Should it look at dev_port instead? See Documentation/ABI/testing/sysfs-class-net for more info.\n",
2542 current->comm);
2543
1c7fd726 2544 return sysfs_emit(buf, "%#x\n", ndev->dev_id);
f6350da4
AM
2545}
2546static DEVICE_ATTR_RO(dev_id);
2547
ed0bc265 2548static int ipoib_intercept_dev_id_attr(struct net_device *dev)
f6350da4
AM
2549{
2550 device_remove_file(&dev->dev, &dev_attr_dev_id);
2551 return device_create_file(&dev->dev, &dev_attr_dev_id);
2552}
2553
1da177e4 2554static struct net_device *ipoib_add_port(const char *format,
1fb7f897 2555 struct ib_device *hca, u32 port)
1da177e4 2556{
5d6b0cb3
DD
2557 struct rtnl_link_ops *ops = ipoib_get_link_ops();
2558 struct rdma_netdev_alloc_params params;
1da177e4 2559 struct ipoib_dev_priv *priv;
9f49a5b5 2560 struct net_device *ndev;
eaeb3984 2561 int result;
1da177e4 2562
5d6b0cb3
DD
2563 ndev = ipoib_intf_alloc(hca, port, format);
2564 if (IS_ERR(ndev)) {
2565 pr_warn("%s, %d: ipoib_intf_alloc failed %ld\n", hca->name, port,
2566 PTR_ERR(ndev));
2567 return ndev;
1da177e4 2568 }
5d6b0cb3 2569 priv = ipoib_priv(ndev);
1da177e4
LT
2570
2571 INIT_IB_EVENT_HANDLER(&priv->event_handler,
2572 priv->ca, ipoib_event);
dcc9881e 2573 ib_register_event_handler(&priv->event_handler);
1da177e4 2574
10293610 2575 /* call event handler to ensure pkey in sync */
5f85120e 2576 ipoib_queue_work(priv, IPOIB_FLUSH_HEAVY);
10293610 2577
5ce2dced
KH
2578 ndev->rtnl_link_ops = ipoib_get_link_ops();
2579
9f49a5b5 2580 result = register_netdev(ndev);
1da177e4 2581 if (result) {
c55359a2
YS
2582 pr_warn("%s: couldn't register ipoib port %d; error %d\n",
2583 hca->name, port, result);
9f49a5b5
JG
2584
2585 ipoib_parent_unregister_pre(ndev);
2586 ipoib_intf_free(ndev);
2587 free_netdev(ndev);
2588
2589 return ERR_PTR(result);
1da177e4
LT
2590 }
2591
3023a1e9
KH
2592 if (hca->ops.rdma_netdev_get_params) {
2593 int rc = hca->ops.rdma_netdev_get_params(hca, port,
5d6b0cb3
DD
2594 RDMA_NETDEV_IPOIB,
2595 &params);
2596
2597 if (!rc && ops->priv_size < params.sizeof_priv)
2598 ops->priv_size = params.sizeof_priv;
2599 }
9f49a5b5
JG
2600 /*
2601 * We cannot set priv_destructor before register_netdev because we
2602 * need priv to be always valid during the error flow to execute
2603 * ipoib_parent_unregister_pre(). Instead handle it manually and only
2604 * enter priv_destructor mode once we are completely registered.
2605 */
2606 ndev->priv_destructor = ipoib_intf_free;
2607
f6350da4
AM
2608 if (ipoib_intercept_dev_id_attr(ndev))
2609 goto sysfs_failed;
9f49a5b5 2610 if (ipoib_cm_add_mode_attr(ndev))
839fcaba 2611 goto sysfs_failed;
9f49a5b5 2612 if (ipoib_add_pkey_attr(ndev))
1da177e4 2613 goto sysfs_failed;
9f49a5b5 2614 if (ipoib_add_umcast_attr(ndev))
335a64a5 2615 goto sysfs_failed;
9f49a5b5 2616 if (device_create_file(&ndev->dev, &dev_attr_create_child))
1da177e4 2617 goto sysfs_failed;
9f49a5b5 2618 if (device_create_file(&ndev->dev, &dev_attr_delete_child))
1da177e4
LT
2619 goto sysfs_failed;
2620
9f49a5b5 2621 return ndev;
1da177e4
LT
2622
2623sysfs_failed:
9f49a5b5
JG
2624 ipoib_parent_unregister_pre(ndev);
2625 unregister_netdev(ndev);
2626 return ERR_PTR(-ENOMEM);
1da177e4
LT
2627}
2628
11a0ae4c 2629static int ipoib_add_one(struct ib_device *device)
1da177e4
LT
2630{
2631 struct list_head *dev_list;
2632 struct net_device *dev;
2633 struct ipoib_dev_priv *priv;
ea1075ed 2634 unsigned int p;
8e37ab68 2635 int count = 0;
07ebafba 2636
b1b63970 2637 dev_list = kmalloc(sizeof(*dev_list), GFP_KERNEL);
1da177e4 2638 if (!dev_list)
11a0ae4c 2639 return -ENOMEM;
1da177e4
LT
2640
2641 INIT_LIST_HEAD(dev_list);
2642
ea1075ed 2643 rdma_for_each_port (device, p) {
8e37ab68 2644 if (!rdma_protocol_ib(device, p))
7b4c8769 2645 continue;
1da177e4
LT
2646 dev = ipoib_add_port("ib%d", device, p);
2647 if (!IS_ERR(dev)) {
c1048aff 2648 priv = ipoib_priv(dev);
1da177e4 2649 list_add_tail(&priv->list, dev_list);
8e37ab68 2650 count++;
1da177e4
LT
2651 }
2652 }
2653
8e37ab68 2654 if (!count) {
ac6dbf7f 2655 kfree(dev_list);
11a0ae4c 2656 return -EOPNOTSUPP;
8e37ab68
MW
2657 }
2658
1da177e4 2659 ib_set_client_data(device, &ipoib_client, dev_list);
11a0ae4c 2660 return 0;
1da177e4
LT
2661}
2662
7c1eb45a 2663static void ipoib_remove_one(struct ib_device *device, void *client_data)
1da177e4 2664{
25405d98 2665 struct ipoib_dev_priv *priv, *tmp, *cpriv, *tcpriv;
7c1eb45a 2666 struct list_head *dev_list = client_data;
1da177e4 2667
1da177e4 2668 list_for_each_entry_safe(priv, tmp, dev_list, list) {
25405d98 2669 LIST_HEAD(head);
7cbee87c 2670 ipoib_parent_unregister_pre(priv->dev);
1da177e4 2671
25405d98
JG
2672 rtnl_lock();
2673
463e5176 2674 netdev_lock(priv->dev);
25405d98
JG
2675 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs,
2676 list)
2677 unregister_netdevice_queue(cpriv->dev, &head);
463e5176 2678 netdev_unlock(priv->dev);
25405d98
JG
2679 unregister_netdevice_queue(priv->dev, &head);
2680 unregister_netdevice_many(&head);
2681
2682 rtnl_unlock();
1da177e4 2683 }
06c56e44
MT
2684
2685 kfree(dev_list);
1da177e4
LT
2686}
2687
771a5258
SR
2688#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2689static struct notifier_block ipoib_netdev_notifier = {
2690 .notifier_call = ipoib_netdev_event,
2691};
2692#endif
2693
1da177e4
LT
2694static int __init ipoib_init_module(void)
2695{
2696 int ret;
2697
0f485251
SM
2698 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
2699 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
2700 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
2701
2702 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
2703 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
732eacc0 2704 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
68e995a2
PS
2705#ifdef CONFIG_INFINIBAND_IPOIB_CM
2706 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
11f74b40 2707 ipoib_max_conn_qp = max(ipoib_max_conn_qp, 0);
68e995a2 2708#endif
0f485251 2709
f89271da
EC
2710 /*
2711 * When copying small received packets, we only copy from the
2712 * linear data part of the SKB, so we rely on this condition.
2713 */
2714 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
2715
2e061c69 2716 ipoib_register_debugfs();
1da177e4
LT
2717
2718 /*
0b39578b
DL
2719 * We create a global workqueue here that is used for all flush
2720 * operations. However, if you attempt to flush a workqueue
2721 * from a task on that same workqueue, it deadlocks the system.
2722 * We want to be able to flush the tasks associated with a
2723 * specific net device, so we also create a workqueue for each
2724 * netdevice. We queue up the tasks for that device only on
2725 * its private workqueue, and we only queue up flush events
2726 * on our global flush workqueue. This avoids the deadlocks.
1da177e4 2727 */
ee190ab7 2728 ipoib_workqueue = alloc_ordered_workqueue("ipoib_flush", 0);
1da177e4
LT
2729 if (!ipoib_workqueue) {
2730 ret = -ENOMEM;
2731 goto err_fs;
2732 }
2733
c1a0b23b
MT
2734 ib_sa_register_client(&ipoib_sa_client);
2735
1da177e4
LT
2736 ret = ib_register_client(&ipoib_client);
2737 if (ret)
c1a0b23b 2738 goto err_sa;
1da177e4 2739
9baa0b03
OG
2740 ret = ipoib_netlink_init();
2741 if (ret)
2742 goto err_client;
2743
771a5258
SR
2744#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2745 register_netdevice_notifier(&ipoib_netdev_notifier);
2746#endif
1da177e4
LT
2747 return 0;
2748
9baa0b03
OG
2749err_client:
2750 ib_unregister_client(&ipoib_client);
2751
c1a0b23b
MT
2752err_sa:
2753 ib_sa_unregister_client(&ipoib_sa_client);
1da177e4
LT
2754 destroy_workqueue(ipoib_workqueue);
2755
9adec1a8
RD
2756err_fs:
2757 ipoib_unregister_debugfs();
2758
1da177e4
LT
2759 return ret;
2760}
2761
2762static void __exit ipoib_cleanup_module(void)
2763{
771a5258
SR
2764#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2765 unregister_netdevice_notifier(&ipoib_netdev_notifier);
2766#endif
9baa0b03 2767 ipoib_netlink_fini();
1da177e4 2768 ib_unregister_client(&ipoib_client);
c1a0b23b 2769 ib_sa_unregister_client(&ipoib_sa_client);
9adec1a8 2770 ipoib_unregister_debugfs();
1da177e4
LT
2771 destroy_workqueue(ipoib_workqueue);
2772}
2773
2774module_init(ipoib_init_module);
2775module_exit(ipoib_cleanup_module);