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