blob: b2943c84a5dda0aecdd8904917f2ebbb02b9b013 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07003 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35#include "ipoib.h"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/module.h>
38
39#include <linux/init.h>
40#include <linux/slab.h>
Shirley Ma0f485252006-04-10 09:43:58 -070041#include <linux/kernel.h>
Roland Dreier10313cb2008-03-12 07:51:03 -070042#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <linux/if_arp.h> /* For ARPHRD_xxx */
45
46#include <linux/ip.h>
47#include <linux/in.h>
48
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +000049#include <linux/jhash.h>
50#include <net/arp.h>
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020051
Yan Burman4b486802013-02-19 15:40:23 +000052#define DRV_VERSION "1.0.0"
53
54const char ipoib_driver_version[] = DRV_VERSION;
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056MODULE_AUTHOR("Roland Dreier");
57MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
58MODULE_LICENSE("Dual BSD/GPL");
Yan Burman4b486802013-02-19 15:40:23 +000059MODULE_VERSION(DRV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -070060
Shirley Ma0f485252006-04-10 09:43:58 -070061int 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070069#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
Roland Dreier1732b0e2005-11-07 10:33:11 -080076struct ipoib_path_iter {
77 struct net_device *dev;
78 struct ipoib_path path;
79};
80
Linus Torvalds1da177e2005-04-16 15:20:36 -070081static 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
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070089struct ib_sa_client ipoib_sa_client;
90
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static void ipoib_add_one(struct ib_device *device);
92static void ipoib_remove_one(struct ib_device *device);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +000093static void ipoib_neigh_reclaim(struct rcu_head *rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
95static struct ib_client ipoib_client = {
96 .name = "ipoib",
97 .add = ipoib_add_one,
98 .remove = ipoib_remove_one
99};
100
101int ipoib_open(struct net_device *dev)
102{
103 struct ipoib_dev_priv *priv = netdev_priv(dev);
104
105 ipoib_dbg(priv, "bringing up interface\n");
106
Michal Schmidt437708c2014-01-17 19:47:25 +0100107 netif_carrier_off(dev);
108
Yossi Etigine028cc52009-04-20 13:58:08 -0700109 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500111 if (ipoib_ib_dev_open(dev)) {
Alex Estrindd57c932014-08-06 14:40:32 -0400112 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
113 return 0;
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800114 goto err_disable;
Alex Estrindd57c932014-08-06 14:40:32 -0400115 }
Yossi Etiginfe25c562008-11-12 10:24:36 -0800116
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800117 if (ipoib_ib_dev_up(dev))
118 goto err_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119
120 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
121 struct ipoib_dev_priv *cpriv;
122
123 /* Bring up any child interfaces too */
Erez Shitritf47944c2013-10-16 17:37:49 +0300124 down_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125 list_for_each_entry(cpriv, &priv->child_intfs, list) {
126 int flags;
127
128 flags = cpriv->dev->flags;
129 if (flags & IFF_UP)
130 continue;
131
132 dev_change_flags(cpriv->dev, flags | IFF_UP);
133 }
Erez Shitritf47944c2013-10-16 17:37:49 +0300134 up_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 }
136
137 netif_start_queue(dev);
138
139 return 0;
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800140
141err_stop:
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500142 ipoib_ib_dev_stop(dev);
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800143
144err_disable:
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800145 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
146
147 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148}
149
150static int ipoib_stop(struct net_device *dev)
151{
152 struct ipoib_dev_priv *priv = netdev_priv(dev);
153
154 ipoib_dbg(priv, "stopping interface\n");
155
156 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
157
158 netif_stop_queue(dev);
159
Doug Ledfordefc82ee2015-02-21 19:27:04 -0500160 ipoib_ib_dev_down(dev);
161 ipoib_ib_dev_stop(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162
163 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
164 struct ipoib_dev_priv *cpriv;
165
166 /* Bring down any child interfaces too */
Erez Shitritf47944c2013-10-16 17:37:49 +0300167 down_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 list_for_each_entry(cpriv, &priv->child_intfs, list) {
169 int flags;
170
171 flags = cpriv->dev->flags;
172 if (!(flags & IFF_UP))
173 continue;
174
175 dev_change_flags(cpriv->dev, flags & ~IFF_UP);
176 }
Erez Shitritf47944c2013-10-16 17:37:49 +0300177 up_read(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179
180 return 0;
181}
182
Or Gerlitz9baa0b02012-09-13 05:56:36 +0000183static void ipoib_uninit(struct net_device *dev)
184{
185 ipoib_dev_cleanup(dev);
186}
187
David S. Miller9ca36f72011-11-16 18:05:50 -0500188static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
Michał Mirosław3d96c742011-04-19 00:43:20 +0000189{
190 struct ipoib_dev_priv *priv = netdev_priv(dev);
191
192 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
Yuval Shaiac4268772015-07-12 01:24:09 -0700193 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
Michał Mirosław3d96c742011-04-19 00:43:20 +0000194
195 return features;
196}
197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
199{
200 struct ipoib_dev_priv *priv = netdev_priv(dev);
201
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200202 /* dev->mtu > 2K ==> connected mode */
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800203 if (ipoib_cm_admin_enabled(dev)) {
204 if (new_mtu > ipoib_cm_max_mtu(dev))
205 return -EINVAL;
206
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200207 if (new_mtu > priv->mcast_mtu)
208 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
209 priv->mcast_mtu);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800210
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200211 dev->mtu = new_mtu;
212 return 0;
213 }
214
Shirley Mabc7b3a32008-04-23 11:55:45 -0700215 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 return -EINVAL;
217
218 priv->admin_mtu = new_mtu;
219
220 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
221
222 return 0;
223}
224
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700225int ipoib_set_mode(struct net_device *dev, const char *buf)
226{
227 struct ipoib_dev_priv *priv = netdev_priv(dev);
228
229 /* flush paths if we switch modes so that connections are restarted */
230 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
231 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
232 ipoib_warn(priv, "enabling connected mode "
233 "will cause multicast packet drops\n");
234 netdev_update_features(dev);
Erez Shitritedcd2a72015-06-07 13:36:11 +0300235 dev_set_mtu(dev, ipoib_cm_max_mtu(dev));
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700236 rtnl_unlock();
237 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
238
239 ipoib_flush_paths(dev);
240 rtnl_lock();
241 return 0;
242 }
243
244 if (!strcmp(buf, "datagram\n")) {
245 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
246 netdev_update_features(dev);
247 dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
248 rtnl_unlock();
249 ipoib_flush_paths(dev);
250 rtnl_lock();
251 return 0;
252 }
253
254 return -EINVAL;
255}
256
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300257static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct ipoib_dev_priv *priv = netdev_priv(dev);
260 struct rb_node *n = priv->path_tree.rb_node;
261 struct ipoib_path *path;
262 int ret;
263
264 while (n) {
265 path = rb_entry(n, struct ipoib_path, rb_node);
266
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300267 ret = memcmp(gid, path->pathrec.dgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 sizeof (union ib_gid));
269
270 if (ret < 0)
271 n = n->rb_left;
272 else if (ret > 0)
273 n = n->rb_right;
274 else
275 return path;
276 }
277
278 return NULL;
279}
280
281static int __path_add(struct net_device *dev, struct ipoib_path *path)
282{
283 struct ipoib_dev_priv *priv = netdev_priv(dev);
284 struct rb_node **n = &priv->path_tree.rb_node;
285 struct rb_node *pn = NULL;
286 struct ipoib_path *tpath;
287 int ret;
288
289 while (*n) {
290 pn = *n;
291 tpath = rb_entry(pn, struct ipoib_path, rb_node);
292
293 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
294 sizeof (union ib_gid));
295 if (ret < 0)
296 n = &pn->rb_left;
297 else if (ret > 0)
298 n = &pn->rb_right;
299 else
300 return -EEXIST;
301 }
302
303 rb_link_node(&path->rb_node, pn, n);
304 rb_insert_color(&path->rb_node, &priv->path_tree);
305
306 list_add_tail(&path->list, &priv->path_list);
307
308 return 0;
309}
310
311static void path_free(struct net_device *dev, struct ipoib_path *path)
312{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 while ((skb = __skb_dequeue(&path->queue)))
316 dev_kfree_skb_irq(skb);
317
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000318 ipoib_dbg(netdev_priv(dev), "path_free\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000320 /* remove all neigh connected to this path */
321 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322
323 if (path->ah)
324 ipoib_put_ah(path->ah);
325
326 kfree(path);
327}
328
Roland Dreier1732b0e2005-11-07 10:33:11 -0800329#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
330
331struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
332{
333 struct ipoib_path_iter *iter;
334
335 iter = kmalloc(sizeof *iter, GFP_KERNEL);
336 if (!iter)
337 return NULL;
338
339 iter->dev = dev;
340 memset(iter->path.pathrec.dgid.raw, 0, 16);
341
342 if (ipoib_path_iter_next(iter)) {
343 kfree(iter);
344 return NULL;
345 }
346
347 return iter;
348}
349
350int ipoib_path_iter_next(struct ipoib_path_iter *iter)
351{
352 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
353 struct rb_node *n;
354 struct ipoib_path *path;
355 int ret = 1;
356
357 spin_lock_irq(&priv->lock);
358
359 n = rb_first(&priv->path_tree);
360
361 while (n) {
362 path = rb_entry(n, struct ipoib_path, rb_node);
363
364 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
365 sizeof (union ib_gid)) < 0) {
366 iter->path = *path;
367 ret = 0;
368 break;
369 }
370
371 n = rb_next(n);
372 }
373
374 spin_unlock_irq(&priv->lock);
375
376 return ret;
377}
378
379void ipoib_path_iter_read(struct ipoib_path_iter *iter,
380 struct ipoib_path *path)
381{
382 *path = iter->path;
383}
384
385#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
386
Moni Shouaee1e2c82008-07-14 23:48:49 -0700387void ipoib_mark_paths_invalid(struct net_device *dev)
388{
389 struct ipoib_dev_priv *priv = netdev_priv(dev);
390 struct ipoib_path *path, *tp;
391
392 spin_lock_irq(&priv->lock);
393
394 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700395 ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
Moni Shouaee1e2c82008-07-14 23:48:49 -0700396 be16_to_cpu(path->pathrec.dlid),
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700397 path->pathrec.dgid.raw);
Moni Shouaee1e2c82008-07-14 23:48:49 -0700398 path->valid = 0;
399 }
400
401 spin_unlock_irq(&priv->lock);
402}
403
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404void ipoib_flush_paths(struct net_device *dev)
405{
406 struct ipoib_dev_priv *priv = netdev_priv(dev);
407 struct ipoib_path *path, *tp;
408 LIST_HEAD(remove_list);
Roland Dreier943c2462008-09-30 10:36:21 -0700409 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Roland Dreier943c2462008-09-30 10:36:21 -0700411 netif_tx_lock_bh(dev);
412 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413
Robert P. J. Day157de222008-04-16 21:09:26 -0700414 list_splice_init(&priv->path_list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
416 list_for_each_entry(path, &remove_list, list)
417 rb_erase(&path->rb_node, &priv->path_tree);
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 list_for_each_entry_safe(path, tp, &remove_list, list) {
420 if (path->query)
421 ib_sa_cancel_query(path->query_id, path->query);
Roland Dreier943c2462008-09-30 10:36:21 -0700422 spin_unlock_irqrestore(&priv->lock, flags);
423 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 wait_for_completion(&path->done);
425 path_free(dev, path);
Roland Dreier943c2462008-09-30 10:36:21 -0700426 netif_tx_lock_bh(dev);
427 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
Roland Dreier943c2462008-09-30 10:36:21 -0700429
430 spin_unlock_irqrestore(&priv->lock, flags);
431 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432}
433
434static void path_rec_completion(int status,
435 struct ib_sa_path_rec *pathrec,
436 void *path_ptr)
437{
438 struct ipoib_path *path = path_ptr;
439 struct net_device *dev = path->dev;
440 struct ipoib_dev_priv *priv = netdev_priv(dev);
441 struct ipoib_ah *ah = NULL;
Roland Dreierc9da4ba2008-09-25 15:26:15 -0700442 struct ipoib_ah *old_ah = NULL;
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700443 struct ipoib_neigh *neigh, *tn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 struct sk_buff_head skqueue;
445 struct sk_buff *skb;
446 unsigned long flags;
447
Roland Dreier843613b2007-02-26 12:57:08 -0800448 if (!status)
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700449 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700450 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 else
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700452 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700453 status, path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454
455 skb_queue_head_init(&skqueue);
456
457 if (!status) {
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700458 struct ib_ah_attr av;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700460 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
461 ah = ipoib_create_ah(dev, priv->pd, &av);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 }
463
464 spin_lock_irqsave(&priv->lock, flags);
465
Mike Marciniszyn38743972011-11-21 08:43:54 -0500466 if (!IS_ERR_OR_NULL(ah)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 path->pathrec = *pathrec;
468
Roland Dreierc9da4ba2008-09-25 15:26:15 -0700469 old_ah = path->ah;
470 path->ah = ah;
471
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
473 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
474
475 while ((skb = __skb_dequeue(&path->queue)))
476 __skb_queue_tail(&skqueue, skb);
477
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700478 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
Moni Shouaee1e2c82008-07-14 23:48:49 -0700479 if (neigh->ah) {
480 WARN_ON(neigh->ah != old_ah);
481 /*
482 * Dropping the ah reference inside
483 * priv->lock is safe here, because we
484 * will hold one more reference from
485 * the original value of path->ah (ie
486 * old_ah).
487 */
488 ipoib_put_ah(neigh->ah);
489 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 kref_get(&path->ah->ref);
491 neigh->ah = path->ah;
492
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000493 if (ipoib_cm_enabled(dev, neigh->daddr)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200494 if (!ipoib_cm_get(neigh))
495 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
496 path,
497 neigh));
498 if (!ipoib_cm_get(neigh)) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000499 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200500 continue;
501 }
502 }
503
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 while ((skb = __skb_dequeue(&neigh->queue)))
505 __skb_queue_tail(&skqueue, skb);
506 }
Moni Shouaee1e2c82008-07-14 23:48:49 -0700507 path->valid = 1;
Roland Dreier5872a9f2005-11-29 10:13:54 -0800508 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Roland Dreier5872a9f2005-11-29 10:13:54 -0800510 path->query = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 complete(&path->done);
512
513 spin_unlock_irqrestore(&priv->lock, flags);
514
Roland Dreierf72dd562013-02-25 09:42:15 -0800515 if (IS_ERR_OR_NULL(ah))
516 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
517
Moni Shouaee1e2c82008-07-14 23:48:49 -0700518 if (old_ah)
519 ipoib_put_ah(old_ah);
520
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 while ((skb = __skb_dequeue(&skqueue))) {
522 skb->dev = dev;
523 if (dev_queue_xmit(skb))
524 ipoib_warn(priv, "dev_queue_xmit failed "
525 "to requeue packet\n");
526 }
527}
528
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300529static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530{
531 struct ipoib_dev_priv *priv = netdev_priv(dev);
532 struct ipoib_path *path;
533
Jack Morgenstein1401b532007-11-26 10:41:19 +0200534 if (!priv->broadcast)
535 return NULL;
536
Roland Dreier21a38482005-11-02 10:07:59 -0800537 path = kzalloc(sizeof *path, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (!path)
539 return NULL;
540
Roland Dreier21a38482005-11-02 10:07:59 -0800541 path->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 skb_queue_head_init(&path->queue);
544
545 INIT_LIST_HEAD(&path->neigh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300547 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
Roland Dreier2337f802007-10-23 19:57:54 -0700548 path->pathrec.sgid = priv->local_gid;
549 path->pathrec.pkey = cpu_to_be16(priv->pkey);
Sean Hefty81668832007-08-02 12:21:31 -0700550 path->pathrec.numb_path = 1;
551 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552
553 return path;
554}
555
556static int path_rec_start(struct net_device *dev,
557 struct ipoib_path *path)
558{
559 struct ipoib_dev_priv *priv = netdev_priv(dev);
560
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700561 ipoib_dbg(priv, "Start path record lookup for %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700562 path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Roland Dreier65c7edd2005-11-28 21:20:34 -0800564 init_completion(&path->done);
565
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 path->query_id =
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700567 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 &path->pathrec,
569 IB_SA_PATH_REC_DGID |
570 IB_SA_PATH_REC_SGID |
571 IB_SA_PATH_REC_NUMB_PATH |
Sean Hefty81668832007-08-02 12:21:31 -0700572 IB_SA_PATH_REC_TRAFFIC_CLASS |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 IB_SA_PATH_REC_PKEY,
574 1000, GFP_ATOMIC,
575 path_rec_completion,
576 path, &path->query);
577 if (path->query_id < 0) {
Or Gerlitz01b3fc82008-07-22 14:18:34 -0700578 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579 path->query = NULL;
Yossi Etigin93a3ab92008-11-12 10:24:38 -0800580 complete(&path->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 return path->query_id;
582 }
583
584 return 0;
585}
586
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000587static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
588 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
590 struct ipoib_dev_priv *priv = netdev_priv(dev);
591 struct ipoib_path *path;
592 struct ipoib_neigh *neigh;
Roland Dreier943c2462008-09-30 10:36:21 -0700593 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000595 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000596 neigh = ipoib_neigh_alloc(daddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (!neigh) {
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000598 spin_unlock_irqrestore(&priv->lock, flags);
Roland Dreierde903512007-09-28 15:33:51 -0700599 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 dev_kfree_skb_any(skb);
601 return;
602 }
603
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000604 path = __path_find(dev, daddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 if (!path) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000606 path = path_rec_create(dev, daddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 if (!path)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300608 goto err_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 __path_add(dev, path);
611 }
612
613 list_add_tail(&neigh->list, &path->neigh_list);
614
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -0800615 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700616 kref_get(&path->ah->ref);
617 neigh->ah = path->ah;
618
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000619 if (ipoib_cm_enabled(dev, neigh->daddr)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200620 if (!ipoib_cm_get(neigh))
621 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
622 if (!ipoib_cm_get(neigh)) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000623 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200624 goto err_drop;
625 }
626 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
627 __skb_queue_tail(&neigh->queue, skb);
628 else {
629 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
630 skb_queue_len(&neigh->queue));
631 goto err_drop;
632 }
Roland Dreier721d67c2009-09-05 20:23:40 -0700633 } else {
634 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000635 ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
636 ipoib_neigh_put(neigh);
Roland Dreier721d67c2009-09-05 20:23:40 -0700637 return;
638 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 } else {
640 neigh->ah = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641
642 if (!path->query && path_rec_start(dev, path))
Jim Foraker49b8e742013-08-08 17:44:22 -0700643 goto err_path;
Erez Shitrit1e85b802015-04-02 13:39:03 +0300644 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
645 __skb_queue_tail(&neigh->queue, skb);
646 else
647 goto err_drop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
649
Roland Dreier943c2462008-09-30 10:36:21 -0700650 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000651 ipoib_neigh_put(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 return;
653
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300654err_path:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000655 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200656err_drop:
Roland Dreierde903512007-09-28 15:33:51 -0700657 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 dev_kfree_skb_any(skb);
659
Roland Dreier943c2462008-09-30 10:36:21 -0700660 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000661 ipoib_neigh_put(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
664static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
Roland Dreier936d7de2012-02-07 14:51:21 +0000665 struct ipoib_cb *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666{
667 struct ipoib_dev_priv *priv = netdev_priv(dev);
668 struct ipoib_path *path;
Roland Dreier943c2462008-09-30 10:36:21 -0700669 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
Roland Dreier943c2462008-09-30 10:36:21 -0700671 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Roland Dreier936d7de2012-02-07 14:51:21 +0000673 path = __path_find(dev, cb->hwaddr + 4);
Moni Shouaee1e2c82008-07-14 23:48:49 -0700674 if (!path || !path->valid) {
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800675 int new_path = 0;
676
677 if (!path) {
Roland Dreier936d7de2012-02-07 14:51:21 +0000678 path = path_rec_create(dev, cb->hwaddr + 4);
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800679 new_path = 1;
680 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 if (path) {
Erez Shitrit1e85b802015-04-02 13:39:03 +0300682 if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
683 __skb_queue_tail(&path->queue, skb);
684 } else {
685 ++dev->stats.tx_dropped;
686 dev_kfree_skb_any(skb);
687 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688
Yossi Etiginff79ae82008-11-12 10:24:39 -0800689 if (!path->query && path_rec_start(dev, path)) {
Roland Dreier943c2462008-09-30 10:36:21 -0700690 spin_unlock_irqrestore(&priv->lock, flags);
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800691 if (new_path)
692 path_free(dev, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 return;
694 } else
695 __path_add(dev, path);
696 } else {
Roland Dreierde903512007-09-28 15:33:51 -0700697 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 dev_kfree_skb_any(skb);
699 }
700
Roland Dreier943c2462008-09-30 10:36:21 -0700701 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 return;
703 }
704
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -0800705 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
707 be16_to_cpu(path->pathrec.dlid));
708
Roland Dreier721d67c2009-09-05 20:23:40 -0700709 spin_unlock_irqrestore(&priv->lock, flags);
Roland Dreier936d7de2012-02-07 14:51:21 +0000710 ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr));
Roland Dreier721d67c2009-09-05 20:23:40 -0700711 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 } else if ((path->query || !path_rec_start(dev, path)) &&
713 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 __skb_queue_tail(&path->queue, skb);
715 } else {
Roland Dreierde903512007-09-28 15:33:51 -0700716 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700717 dev_kfree_skb_any(skb);
718 }
719
Roland Dreier943c2462008-09-30 10:36:21 -0700720 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
722
723static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
724{
725 struct ipoib_dev_priv *priv = netdev_priv(dev);
726 struct ipoib_neigh *neigh;
Eric Dumazetb49fe362014-09-18 11:00:27 -0700727 struct ipoib_cb *cb = ipoib_skb_cb(skb);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000728 struct ipoib_header *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 unsigned long flags;
730
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000731 header = (struct ipoib_header *) skb->data;
732
733 if (unlikely(cb->hwaddr[4] == 0xff)) {
734 /* multicast, arrange "if" according to probability */
735 if ((header->proto != htons(ETH_P_IP)) &&
736 (header->proto != htons(ETH_P_IPV6)) &&
737 (header->proto != htons(ETH_P_ARP)) &&
Patrick McHardydc850b02013-04-17 06:18:29 +0000738 (header->proto != htons(ETH_P_RARP)) &&
739 (header->proto != htons(ETH_P_TIPC))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000740 /* ethertype not supported by IPoIB */
David Miller17e6abe2011-12-02 16:52:44 +0000741 ++dev->stats.tx_dropped;
742 dev_kfree_skb_any(skb);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000743 return NETDEV_TX_OK;
David Miller17e6abe2011-12-02 16:52:44 +0000744 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000745 /* Add in the P_Key for multicast*/
746 cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
747 cb->hwaddr[9] = priv->pkey & 0xff;
748
749 neigh = ipoib_neigh_get(dev, cb->hwaddr);
750 if (likely(neigh))
751 goto send_using_neigh;
752 ipoib_mcast_send(dev, cb->hwaddr, skb);
753 return NETDEV_TX_OK;
David Miller17e6abe2011-12-02 16:52:44 +0000754 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000756 /* unicast, arrange "switch" according to probability */
757 switch (header->proto) {
758 case htons(ETH_P_IP):
759 case htons(ETH_P_IPV6):
Patrick McHardydc850b02013-04-17 06:18:29 +0000760 case htons(ETH_P_TIPC):
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000761 neigh = ipoib_neigh_get(dev, cb->hwaddr);
762 if (unlikely(!neigh)) {
763 neigh_add_path(skb, cb->hwaddr, dev);
764 return NETDEV_TX_OK;
Yossi Etigina50df392009-01-09 14:05:11 -0800765 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000766 break;
767 case htons(ETH_P_ARP):
768 case htons(ETH_P_RARP):
769 /* for unicast ARP and RARP should always perform path find */
770 unicast_arp_send(skb, dev, cb);
771 return NETDEV_TX_OK;
772 default:
773 /* ethertype not supported by IPoIB */
774 ++dev->stats.tx_dropped;
775 dev_kfree_skb_any(skb);
776 return NETDEV_TX_OK;
777 }
Michael S. Tsirkin8a7f7522006-07-19 17:44:37 +0300778
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000779send_using_neigh:
780 /* note we now hold a ref to neigh */
781 if (ipoib_cm_get(neigh)) {
782 if (ipoib_cm_up(neigh)) {
783 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
784 goto unref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000786 } else if (neigh->ah) {
787 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
788 goto unref;
789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000791 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
792 spin_lock_irqsave(&priv->lock, flags);
793 __skb_queue_tail(&neigh->queue, skb);
794 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 } else {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000796 ++dev->stats.tx_dropped;
797 dev_kfree_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000799
800unref:
801 ipoib_neigh_put(neigh);
802
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 return NETDEV_TX_OK;
804}
805
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806static void ipoib_timeout(struct net_device *dev)
807{
808 struct ipoib_dev_priv *priv = netdev_priv(dev);
809
Roland Dreier4b2d3192005-10-18 12:20:06 -0700810 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
811 jiffies_to_msecs(jiffies - dev->trans_start));
812 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
813 netif_queue_stopped(dev),
814 priv->tx_head, priv->tx_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815 /* XXX reset QP, etc. */
816}
817
818static int ipoib_hard_header(struct sk_buff *skb,
819 struct net_device *dev,
820 unsigned short type,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700821 const void *daddr, const void *saddr, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822{
823 struct ipoib_header *header;
Eric Dumazetb49fe362014-09-18 11:00:27 -0700824 struct ipoib_cb *cb = ipoib_skb_cb(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825
826 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
827
828 header->proto = htons(type);
829 header->reserved = 0;
830
831 /*
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000832 * we don't rely on dst_entry structure, always stuff the
Roland Dreier936d7de2012-02-07 14:51:21 +0000833 * destination address into skb->cb so we can figure out where
834 * to send the packet later.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000836 memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700837
Doug Ledford83bdd3b2013-04-01 21:25:30 +0000838 return sizeof *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839}
840
841static void ipoib_set_mcast_list(struct net_device *dev)
842{
843 struct ipoib_dev_priv *priv = netdev_priv(dev);
844
Leonid Arsh7a343d42006-03-23 19:52:51 +0200845 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
846 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
847 return;
848 }
849
Doug Ledford0b395782015-02-21 19:27:03 -0500850 queue_work(priv->wq, &priv->restart_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700851}
852
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +0200853static int ipoib_get_iflink(const struct net_device *dev)
854{
855 struct ipoib_dev_priv *priv = netdev_priv(dev);
856
Erez Shitrit2c153952015-04-16 16:34:34 +0300857 /* parent interface */
858 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
859 return dev->ifindex;
860
861 /* child/vlan interface */
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +0200862 return priv->parent->ifindex;
863}
864
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000865static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700866{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000867 /*
868 * Use only the address parts that contributes to spreading
869 * The subnet prefix is not used as one can not connect to
870 * same remote port (GUID) using the same remote QPN via two
871 * different subnets.
872 */
873 /* qpn octets[1:4) & port GUID octets[12:20) */
Shlomo Pongratz9d1ad662013-02-19 15:40:22 +0000874 u32 *d32 = (u32 *) daddr;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000875 u32 hv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700876
Shlomo Pongratz9d1ad662013-02-19 15:40:22 +0000877 hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000878 return hv & htbl->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879}
880
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000881struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
882{
883 struct ipoib_dev_priv *priv = netdev_priv(dev);
884 struct ipoib_neigh_table *ntbl = &priv->ntbl;
885 struct ipoib_neigh_hash *htbl;
886 struct ipoib_neigh *neigh = NULL;
887 u32 hash_val;
888
889 rcu_read_lock_bh();
890
891 htbl = rcu_dereference_bh(ntbl->htbl);
892
893 if (!htbl)
894 goto out_unlock;
895
896 hash_val = ipoib_addr_hash(htbl, daddr);
897 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
898 neigh != NULL;
899 neigh = rcu_dereference_bh(neigh->hnext)) {
900 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
901 /* found, take one ref on behalf of the caller */
902 if (!atomic_inc_not_zero(&neigh->refcnt)) {
903 /* deleted */
904 neigh = NULL;
905 goto out_unlock;
906 }
907 neigh->alive = jiffies;
908 goto out_unlock;
909 }
910 }
911
912out_unlock:
913 rcu_read_unlock_bh();
914 return neigh;
915}
916
917static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
918{
919 struct ipoib_neigh_table *ntbl = &priv->ntbl;
920 struct ipoib_neigh_hash *htbl;
921 unsigned long neigh_obsolete;
922 unsigned long dt;
923 unsigned long flags;
924 int i;
925
926 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
927 return;
928
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000929 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000930
931 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000932 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000933
934 if (!htbl)
935 goto out_unlock;
936
937 /* neigh is obsolete if it was idle for two GC periods */
938 dt = 2 * arp_tbl.gc_interval;
939 neigh_obsolete = jiffies - dt;
940 /* handle possible race condition */
941 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
942 goto out_unlock;
943
944 for (i = 0; i < htbl->size; i++) {
945 struct ipoib_neigh *neigh;
946 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
947
948 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000949 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000950 /* was the neigh idle for two GC periods */
951 if (time_after(neigh_obsolete, neigh->alive)) {
952 rcu_assign_pointer(*np,
953 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000954 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000955 /* remove from path/mc list */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000956 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000957 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
958 } else {
959 np = &neigh->hnext;
960 }
961
962 }
963 }
964
965out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000966 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000967}
968
969static void ipoib_reap_neigh(struct work_struct *work)
970{
971 struct ipoib_dev_priv *priv =
972 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
973
974 __ipoib_reap_neigh(priv);
975
976 if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
Doug Ledford0b395782015-02-21 19:27:03 -0500977 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000978 arp_tbl.gc_interval);
979}
980
981
982static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
Moni Shoua732a2172007-10-09 19:43:36 -0700983 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300984{
985 struct ipoib_neigh *neigh;
986
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000987 neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300988 if (!neigh)
989 return NULL;
990
Moni Shoua732a2172007-10-09 19:43:36 -0700991 neigh->dev = dev;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000992 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
Roland Dreier82b39912006-12-12 14:48:18 -0800993 skb_queue_head_init(&neigh->queue);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000994 INIT_LIST_HEAD(&neigh->list);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200995 ipoib_cm_set(neigh, NULL);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000996 /* one ref on behalf of the caller */
997 atomic_set(&neigh->refcnt, 1);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300998
999 return neigh;
1000}
1001
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001002struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
1003 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001004{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001005 struct ipoib_dev_priv *priv = netdev_priv(dev);
1006 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1007 struct ipoib_neigh_hash *htbl;
1008 struct ipoib_neigh *neigh;
1009 u32 hash_val;
1010
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001011 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001012 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001013 if (!htbl) {
1014 neigh = NULL;
1015 goto out_unlock;
1016 }
1017
1018 /* need to add a new neigh, but maybe some other thread succeeded?
1019 * recalc hash, maybe hash resize took place so we do a search
1020 */
1021 hash_val = ipoib_addr_hash(htbl, daddr);
1022 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001023 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001024 neigh != NULL;
1025 neigh = rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001026 lockdep_is_held(&priv->lock))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001027 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1028 /* found, take one ref on behalf of the caller */
1029 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1030 /* deleted */
1031 neigh = NULL;
1032 break;
1033 }
1034 neigh->alive = jiffies;
1035 goto out_unlock;
1036 }
1037 }
1038
1039 neigh = ipoib_neigh_ctor(daddr, dev);
1040 if (!neigh)
1041 goto out_unlock;
1042
1043 /* one ref on behalf of the hash table */
1044 atomic_inc(&neigh->refcnt);
1045 neigh->alive = jiffies;
1046 /* put in hash */
1047 rcu_assign_pointer(neigh->hnext,
1048 rcu_dereference_protected(htbl->buckets[hash_val],
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001049 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001050 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1051 atomic_inc(&ntbl->entries);
1052
1053out_unlock:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001054
1055 return neigh;
1056}
1057
1058void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1059{
1060 /* neigh reference count was dropprd to zero */
1061 struct net_device *dev = neigh->dev;
1062 struct ipoib_dev_priv *priv = netdev_priv(dev);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001063 struct sk_buff *skb;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001064 if (neigh->ah)
1065 ipoib_put_ah(neigh->ah);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001066 while ((skb = __skb_dequeue(&neigh->queue))) {
Roland Dreierde903512007-09-28 15:33:51 -07001067 ++dev->stats.tx_dropped;
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001068 dev_kfree_skb_any(skb);
1069 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001070 if (ipoib_cm_get(neigh))
1071 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001072 ipoib_dbg(netdev_priv(dev),
1073 "neigh free for %06x %pI6\n",
1074 IPOIB_QPN(neigh->daddr),
1075 neigh->daddr + 4);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001076 kfree(neigh);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001077 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1078 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1079 complete(&priv->ntbl.flushed);
1080 }
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001081}
1082
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001083static void ipoib_neigh_reclaim(struct rcu_head *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001085 /* Called as a result of removal from hash table */
1086 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1087 /* note TX context may hold another ref */
1088 ipoib_neigh_put(neigh);
1089}
1090
1091void ipoib_neigh_free(struct ipoib_neigh *neigh)
1092{
1093 struct net_device *dev = neigh->dev;
1094 struct ipoib_dev_priv *priv = netdev_priv(dev);
1095 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1096 struct ipoib_neigh_hash *htbl;
1097 struct ipoib_neigh __rcu **np;
1098 struct ipoib_neigh *n;
1099 u32 hash_val;
1100
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001101 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001102 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001103 if (!htbl)
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001104 return;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001105
1106 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1107 np = &htbl->buckets[hash_val];
1108 for (n = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001109 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001110 n != NULL;
Shlomo Pongratz6c723a62012-08-13 14:39:50 +00001111 n = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001112 lockdep_is_held(&priv->lock))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001113 if (n == neigh) {
1114 /* found */
1115 rcu_assign_pointer(*np,
1116 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001117 lockdep_is_held(&priv->lock)));
Jim Foraker49b8e742013-08-08 17:44:22 -07001118 /* remove from parent list */
1119 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001120 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001121 return;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001122 } else {
1123 np = &n->hnext;
1124 }
1125 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001126}
1127
1128static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1129{
1130 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1131 struct ipoib_neigh_hash *htbl;
Bart Van Assche52374962015-05-26 15:03:48 +02001132 struct ipoib_neigh __rcu **buckets;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001133 u32 size;
1134
1135 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1136 ntbl->htbl = NULL;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001137 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1138 if (!htbl)
1139 return -ENOMEM;
1140 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1141 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1142 buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1143 if (!buckets) {
1144 kfree(htbl);
1145 return -ENOMEM;
1146 }
1147 htbl->size = size;
1148 htbl->mask = (size - 1);
1149 htbl->buckets = buckets;
Bart Van Assche52374962015-05-26 15:03:48 +02001150 RCU_INIT_POINTER(ntbl->htbl, htbl);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001151 htbl->ntbl = ntbl;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001152 atomic_set(&ntbl->entries, 0);
1153
1154 /* start garbage collection */
1155 clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
Doug Ledford0b395782015-02-21 19:27:03 -05001156 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001157 arp_tbl.gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158
1159 return 0;
1160}
1161
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001162static void neigh_hash_free_rcu(struct rcu_head *head)
1163{
1164 struct ipoib_neigh_hash *htbl = container_of(head,
1165 struct ipoib_neigh_hash,
1166 rcu);
1167 struct ipoib_neigh __rcu **buckets = htbl->buckets;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001168 struct ipoib_neigh_table *ntbl = htbl->ntbl;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001169
1170 kfree(buckets);
1171 kfree(htbl);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001172 complete(&ntbl->deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001173}
1174
1175void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1176{
1177 struct ipoib_dev_priv *priv = netdev_priv(dev);
1178 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1179 struct ipoib_neigh_hash *htbl;
1180 unsigned long flags;
1181 int i;
1182
1183 /* remove all neigh connected to a given path or mcast */
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001184 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001185
1186 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001187 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001188
1189 if (!htbl)
1190 goto out_unlock;
1191
1192 for (i = 0; i < htbl->size; i++) {
1193 struct ipoib_neigh *neigh;
1194 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1195
1196 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001197 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001198 /* delete neighs belong to this parent */
1199 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1200 rcu_assign_pointer(*np,
1201 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001202 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001203 /* remove from parent list */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001204 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001205 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1206 } else {
1207 np = &neigh->hnext;
1208 }
1209
1210 }
1211 }
1212out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001213 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001214}
1215
1216static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1217{
1218 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1219 struct ipoib_neigh_hash *htbl;
1220 unsigned long flags;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001221 int i, wait_flushed = 0;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001222
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001223 init_completion(&priv->ntbl.flushed);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001224
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001225 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001226
1227 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001228 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001229 if (!htbl)
1230 goto out_unlock;
1231
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001232 wait_flushed = atomic_read(&priv->ntbl.entries);
1233 if (!wait_flushed)
1234 goto free_htbl;
1235
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001236 for (i = 0; i < htbl->size; i++) {
1237 struct ipoib_neigh *neigh;
1238 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1239
1240 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001241 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001242 rcu_assign_pointer(*np,
1243 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001244 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001245 /* remove from path/mc list */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001246 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001247 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1248 }
1249 }
1250
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001251free_htbl:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001252 rcu_assign_pointer(ntbl->htbl, NULL);
1253 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1254
1255out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001256 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001257 if (wait_flushed)
1258 wait_for_completion(&priv->ntbl.flushed);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001259}
1260
1261static void ipoib_neigh_hash_uninit(struct net_device *dev)
1262{
1263 struct ipoib_dev_priv *priv = netdev_priv(dev);
1264 int stopped;
1265
1266 ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001267 init_completion(&priv->ntbl.deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001268 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1269
1270 /* Stop GC if called at init fail need to cancel work */
1271 stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1272 if (!stopped)
1273 cancel_delayed_work(&priv->neigh_reap_task);
1274
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001275 ipoib_flush_neighs(priv);
1276
1277 wait_for_completion(&priv->ntbl.deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001278}
1279
1280
Linus Torvalds1da177e2005-04-16 15:20:36 -07001281int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1282{
1283 struct ipoib_dev_priv *priv = netdev_priv(dev);
1284
1285 /* Allocate RX/TX "rings" to hold queued skbs */
Shirley Ma0f485252006-04-10 09:43:58 -07001286 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287 GFP_KERNEL);
1288 if (!priv->rx_ring) {
1289 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
Shirley Ma0f485252006-04-10 09:43:58 -07001290 ca->name, ipoib_recvq_size);
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001291 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001292 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Joe Perches948579c2010-11-05 03:07:36 +00001294 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001295 if (!priv->tx_ring) {
1296 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
Shirley Ma0f485252006-04-10 09:43:58 -07001297 ca->name, ipoib_sendq_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298 goto out_rx_ring_cleanup;
1299 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001301 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001302
1303 if (ipoib_ib_dev_init(dev, ca, port))
1304 goto out_tx_ring_cleanup;
1305
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001306 /*
1307 * Must be after ipoib_ib_dev_init so we can allocate a per
1308 * device wq there and use it here
1309 */
1310 if (ipoib_neigh_hash_init(priv) < 0)
1311 goto out_dev_uninit;
1312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return 0;
1314
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001315out_dev_uninit:
1316 ipoib_ib_dev_cleanup(dev);
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318out_tx_ring_cleanup:
Roland Dreier10313cb2008-03-12 07:51:03 -07001319 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321out_rx_ring_cleanup:
1322 kfree(priv->rx_ring);
1323
1324out:
1325 return -ENOMEM;
1326}
1327
1328void ipoib_dev_cleanup(struct net_device *dev)
1329{
1330 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001331 LIST_HEAD(head);
1332
1333 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
Roland Dreier1732b0e2005-11-07 10:33:11 -08001335 ipoib_delete_debug_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
1337 /* Delete any child interfaces first */
1338 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001339 /* Stop GC on child */
1340 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1341 cancel_delayed_work(&cpriv->neigh_reap_task);
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001342 unregister_netdevice_queue(cpriv->dev, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343 }
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001344 unregister_netdevice_many(&head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001345
Doug Ledfordbe7aa662015-02-21 19:27:00 -05001346 /*
1347 * Must be before ipoib_ib_dev_cleanup or we delete an in use
1348 * work queue
1349 */
1350 ipoib_neigh_hash_uninit(dev);
1351
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 ipoib_ib_dev_cleanup(dev);
1353
Hal Rosenstock92a6b342005-08-13 20:50:27 -07001354 kfree(priv->rx_ring);
Roland Dreier10313cb2008-03-12 07:51:03 -07001355 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Hal Rosenstock92a6b342005-08-13 20:50:27 -07001357 priv->rx_ring = NULL;
1358 priv->tx_ring = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359}
1360
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001361static const struct header_ops ipoib_header_ops = {
1362 .create = ipoib_hard_header,
1363};
1364
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001365static const struct net_device_ops ipoib_netdev_ops = {
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001366 .ndo_uninit = ipoib_uninit,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001367 .ndo_open = ipoib_open,
1368 .ndo_stop = ipoib_stop,
1369 .ndo_change_mtu = ipoib_change_mtu,
Michał Mirosław3d96c742011-04-19 00:43:20 +00001370 .ndo_fix_features = ipoib_fix_features,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001371 .ndo_start_xmit = ipoib_start_xmit,
1372 .ndo_tx_timeout = ipoib_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001373 .ndo_set_rx_mode = ipoib_set_mcast_list,
Nicolas Dichtel5aa7add2015-04-02 17:07:07 +02001374 .ndo_get_iflink = ipoib_get_iflink,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001375};
1376
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001377void ipoib_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001378{
1379 struct ipoib_dev_priv *priv = netdev_priv(dev);
1380
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001381 dev->netdev_ops = &ipoib_netdev_ops;
Roland Dreier2337f802007-10-23 19:57:54 -07001382 dev->header_ops = &ipoib_header_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001383
Eli Cohen82c24c12008-04-16 21:09:32 -07001384 ipoib_set_ethtool_ops(dev);
1385
Michal Schmidt7f1a3862013-08-21 18:50:22 +02001386 netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001387
Roland Dreier2337f802007-10-23 19:57:54 -07001388 dev->watchdog_timeo = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389
Roland Dreier2337f802007-10-23 19:57:54 -07001390 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391
Roland Dreier936d7de2012-02-07 14:51:21 +00001392 dev->hard_header_len = IPOIB_ENCAP_LEN;
Roland Dreier2337f802007-10-23 19:57:54 -07001393 dev->addr_len = INFINIBAND_ALEN;
1394 dev->type = ARPHRD_INFINIBAND;
1395 dev->tx_queue_len = ipoib_sendq_size * 2;
Eli Coheneb14032f2008-01-30 18:30:46 +02001396 dev->features = (NETIF_F_VLAN_CHALLENGED |
Eli Coheneb14032f2008-01-30 18:30:46 +02001397 NETIF_F_HIGHDMA);
Eric Dumazet02875872014-10-05 18:38:35 -07001398 netif_keep_dst(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001399
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
1401
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 priv->dev = dev;
1403
1404 spin_lock_init(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405
Erez Shitritf47944c2013-10-16 17:37:49 +03001406 init_rwsem(&priv->vlan_rwsem);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 INIT_LIST_HEAD(&priv->path_list);
1409 INIT_LIST_HEAD(&priv->child_intfs);
1410 INIT_LIST_HEAD(&priv->dead_ahs);
1411 INIT_LIST_HEAD(&priv->multicast_list);
1412
David Howellsc4028952006-11-22 14:57:56 +00001413 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
Yossi Etigine8224e42008-09-16 11:57:45 -07001414 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
Moni Shouaee1e2c82008-07-14 23:48:49 -07001415 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
1416 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
1417 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
David Howellsc4028952006-11-22 14:57:56 +00001418 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1419 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001420 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001421}
1422
1423struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
1424{
1425 struct net_device *dev;
1426
Tom Gundersenc835a672014-07-14 16:37:24 +02001427 dev = alloc_netdev((int)sizeof(struct ipoib_dev_priv), name,
1428 NET_NAME_UNKNOWN, ipoib_setup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001429 if (!dev)
1430 return NULL;
1431
1432 return netdev_priv(dev);
1433}
1434
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001435static ssize_t show_pkey(struct device *dev,
1436 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001438 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
1440 return sprintf(buf, "0x%04x\n", priv->pkey);
1441}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001442static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001443
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001444static ssize_t show_umcast(struct device *dev,
1445 struct device_attribute *attr, char *buf)
1446{
1447 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1448
1449 return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1450}
1451
Or Gerlitz862096a2012-09-27 12:06:02 +00001452void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001453{
Or Gerlitz862096a2012-09-27 12:06:02 +00001454 struct ipoib_dev_priv *priv = netdev_priv(ndev);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001455
1456 if (umcast_val > 0) {
1457 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1458 ipoib_warn(priv, "ignoring multicast groups joined directly "
1459 "by userspace\n");
1460 } else
1461 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
Or Gerlitz862096a2012-09-27 12:06:02 +00001462}
1463
1464static ssize_t set_umcast(struct device *dev,
1465 struct device_attribute *attr,
1466 const char *buf, size_t count)
1467{
1468 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1469
1470 ipoib_set_umcast(to_net_dev(dev), umcast_val);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001471
1472 return count;
1473}
1474static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1475
1476int ipoib_add_umcast_attr(struct net_device *dev)
1477{
1478 return device_create_file(&dev->dev, &dev_attr_umcast);
1479}
1480
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001481static ssize_t create_child(struct device *dev,
1482 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 const char *buf, size_t count)
1484{
1485 int pkey;
1486 int ret;
1487
1488 if (sscanf(buf, "%i", &pkey) != 1)
1489 return -EINVAL;
1490
Or Gerlitz3d790a42013-07-18 14:02:31 +03001491 if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492 return -EINVAL;
1493
Roland Dreier4ce05932005-08-19 12:03:17 -07001494 /*
1495 * Set the full membership bit, so that we join the right
1496 * broadcast group, etc.
1497 */
1498 pkey |= 0x8000;
1499
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001500 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501
1502 return ret ? ret : count;
1503}
Or Gerlitz7a52b342010-06-06 04:59:16 +00001504static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001505
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001506static ssize_t delete_child(struct device *dev,
1507 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 const char *buf, size_t count)
1509{
1510 int pkey;
1511 int ret;
1512
1513 if (sscanf(buf, "%i", &pkey) != 1)
1514 return -EINVAL;
1515
1516 if (pkey < 0 || pkey > 0xffff)
1517 return -EINVAL;
1518
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001519 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001520
1521 return ret ? ret : count;
1522
1523}
Or Gerlitz7a52b342010-06-06 04:59:16 +00001524static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525
1526int ipoib_add_pkey_attr(struct net_device *dev)
1527{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001528 return device_create_file(&dev->dev, &dev_attr_pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529}
1530
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001531int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
1532{
1533 struct ib_device_attr *device_attr;
1534 int result = -ENOMEM;
1535
1536 device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
1537 if (!device_attr) {
1538 printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
1539 hca->name, sizeof *device_attr);
1540 return result;
1541 }
1542
1543 result = ib_query_device(hca, device_attr);
1544 if (result) {
1545 printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
1546 hca->name, result);
1547 kfree(device_attr);
1548 return result;
1549 }
1550 priv->hca_caps = device_attr->device_cap_flags;
1551
1552 kfree(device_attr);
1553
1554 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
Michał Mirosław3d96c742011-04-19 00:43:20 +00001555 priv->dev->hw_features = NETIF_F_SG |
1556 NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1557
1558 if (priv->hca_caps & IB_DEVICE_UD_TSO)
1559 priv->dev->hw_features |= NETIF_F_TSO;
1560
1561 priv->dev->features |= priv->dev->hw_features;
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001562 }
1563
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001564 return 0;
1565}
1566
Linus Torvalds1da177e2005-04-16 15:20:36 -07001567static struct net_device *ipoib_add_port(const char *format,
1568 struct ib_device *hca, u8 port)
1569{
1570 struct ipoib_dev_priv *priv;
Shirley Mabc7b3a32008-04-23 11:55:45 -07001571 struct ib_port_attr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 int result = -ENOMEM;
1573
1574 priv = ipoib_intf_alloc(format);
1575 if (!priv)
1576 goto alloc_mem_failed;
1577
1578 SET_NETDEV_DEV(priv->dev, hca->dma_device);
Eli Cohenc3aa9b12010-09-20 07:05:06 +00001579 priv->dev->dev_id = port - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580
Amir Vadai58e9cc92015-07-01 14:31:01 +03001581 result = ib_query_port(hca, port, &attr);
1582 if (!result)
Shirley Mabc7b3a32008-04-23 11:55:45 -07001583 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1584 else {
1585 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
1586 hca->name, port);
1587 goto device_init_failed;
1588 }
1589
1590 /* MTU will be reset when mcast join happens */
1591 priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
1592 priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
1593
David Miller596b9b62011-07-25 00:01:25 +00001594 priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
1595
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 result = ib_query_pkey(hca, port, 0, &priv->pkey);
1597 if (result) {
1598 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1599 hca->name, port, result);
Eli Cohenca6de172007-08-21 18:46:10 +03001600 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001601 }
1602
Amir Vadai58e9cc92015-07-01 14:31:01 +03001603 result = ipoib_set_dev_features(priv, hca);
1604 if (result)
Eli Cohen60461362008-04-16 21:01:10 -07001605 goto device_init_failed;
Vladimir Sokolovskyaf40da82008-07-14 23:48:48 -07001606
Roland Dreier4ce05932005-08-19 12:03:17 -07001607 /*
1608 * Set the full membership bit, so that we join the right
1609 * broadcast group, etc.
1610 */
1611 priv->pkey |= 0x8000;
1612
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 priv->dev->broadcast[8] = priv->pkey >> 8;
1614 priv->dev->broadcast[9] = priv->pkey & 0xff;
1615
1616 result = ib_query_gid(hca, port, 0, &priv->local_gid);
1617 if (result) {
1618 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1619 hca->name, port, result);
Eli Cohenca6de172007-08-21 18:46:10 +03001620 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001621 } else
1622 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1623
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624 result = ipoib_dev_init(priv->dev, hca, port);
1625 if (result < 0) {
1626 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1627 hca->name, port, result);
1628 goto device_init_failed;
1629 }
1630
1631 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1632 priv->ca, ipoib_event);
1633 result = ib_register_event_handler(&priv->event_handler);
1634 if (result < 0) {
1635 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1636 "port %d (ret = %d)\n",
1637 hca->name, port, result);
1638 goto event_failed;
1639 }
1640
1641 result = register_netdev(priv->dev);
1642 if (result) {
1643 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1644 hca->name, port, result);
1645 goto register_failed;
1646 }
1647
Roland Dreier1732b0e2005-11-07 10:33:11 -08001648 ipoib_create_debug_files(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001650 if (ipoib_cm_add_mode_attr(priv->dev))
1651 goto sysfs_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001652 if (ipoib_add_pkey_attr(priv->dev))
1653 goto sysfs_failed;
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001654 if (ipoib_add_umcast_attr(priv->dev))
1655 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001656 if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001658 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 goto sysfs_failed;
1660
1661 return priv->dev;
1662
1663sysfs_failed:
Roland Dreier1732b0e2005-11-07 10:33:11 -08001664 ipoib_delete_debug_files(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001665 unregister_netdev(priv->dev);
1666
1667register_failed:
1668 ib_unregister_event_handler(&priv->event_handler);
Doug Ledford0b395782015-02-21 19:27:03 -05001669 flush_workqueue(ipoib_workqueue);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001670 /* Stop GC if started before flush */
1671 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1672 cancel_delayed_work(&priv->neigh_reap_task);
Doug Ledford0b395782015-02-21 19:27:03 -05001673 flush_workqueue(priv->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675event_failed:
1676 ipoib_dev_cleanup(priv->dev);
1677
1678device_init_failed:
1679 free_netdev(priv->dev);
1680
1681alloc_mem_failed:
1682 return ERR_PTR(result);
1683}
1684
1685static void ipoib_add_one(struct ib_device *device)
1686{
1687 struct list_head *dev_list;
1688 struct net_device *dev;
1689 struct ipoib_dev_priv *priv;
Hal Rosenstock41390322015-06-29 09:57:00 -04001690 int p;
Michael Wang8e37ab62015-05-05 14:50:24 +02001691 int count = 0;
Tom Tucker07ebafb2006-08-03 16:02:42 -05001692
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1694 if (!dev_list)
1695 return;
1696
1697 INIT_LIST_HEAD(dev_list);
1698
Hal Rosenstock41390322015-06-29 09:57:00 -04001699 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
Michael Wang8e37ab62015-05-05 14:50:24 +02001700 if (!rdma_protocol_ib(device, p))
Eli Cohen7b4c8762010-09-27 17:51:11 -07001701 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 dev = ipoib_add_port("ib%d", device, p);
1703 if (!IS_ERR(dev)) {
1704 priv = netdev_priv(dev);
1705 list_add_tail(&priv->list, dev_list);
Michael Wang8e37ab62015-05-05 14:50:24 +02001706 count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 }
1708 }
1709
Michael Wang8e37ab62015-05-05 14:50:24 +02001710 if (!count) {
1711 kfree(dev_list);
1712 return;
1713 }
1714
Linus Torvalds1da177e2005-04-16 15:20:36 -07001715 ib_set_client_data(device, &ipoib_client, dev_list);
1716}
1717
1718static void ipoib_remove_one(struct ib_device *device)
1719{
1720 struct ipoib_dev_priv *priv, *tmp;
1721 struct list_head *dev_list;
1722
1723 dev_list = ib_get_client_data(device, &ipoib_client);
Itai Garbi5a2815f2013-02-19 15:40:24 +00001724 if (!dev_list)
1725 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1728 ib_unregister_event_handler(&priv->event_handler);
Doug Ledford0b395782015-02-21 19:27:03 -05001729 flush_workqueue(ipoib_workqueue);
Roland Dreiera77a57a2008-08-19 15:01:32 -07001730
1731 rtnl_lock();
1732 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
1733 rtnl_unlock();
1734
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001735 /* Stop GC */
1736 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1737 cancel_delayed_work(&priv->neigh_reap_task);
Doug Ledford0b395782015-02-21 19:27:03 -05001738 flush_workqueue(priv->wq);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739
1740 unregister_netdev(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001741 free_netdev(priv->dev);
1742 }
Michael S. Tsirkin06c56e42005-09-01 09:19:02 -07001743
1744 kfree(dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745}
1746
1747static int __init ipoib_init_module(void)
1748{
1749 int ret;
1750
Shirley Ma0f485252006-04-10 09:43:58 -07001751 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1752 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1753 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1754
1755 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1756 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -07001757 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001758#ifdef CONFIG_INFINIBAND_IPOIB_CM
1759 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1760#endif
Shirley Ma0f485252006-04-10 09:43:58 -07001761
Eli Cohenf89271da2008-07-14 23:48:44 -07001762 /*
1763 * When copying small received packets, we only copy from the
1764 * linear data part of the SKB, so we rely on this condition.
1765 */
1766 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
1767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 ret = ipoib_register_debugfs();
1769 if (ret)
1770 return ret;
1771
1772 /*
Doug Ledford0b395782015-02-21 19:27:03 -05001773 * We create a global workqueue here that is used for all flush
1774 * operations. However, if you attempt to flush a workqueue
1775 * from a task on that same workqueue, it deadlocks the system.
1776 * We want to be able to flush the tasks associated with a
1777 * specific net device, so we also create a workqueue for each
1778 * netdevice. We queue up the tasks for that device only on
1779 * its private workqueue, and we only queue up flush events
1780 * on our global flush workqueue. This avoids the deadlocks.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 */
Doug Ledford0b395782015-02-21 19:27:03 -05001782 ipoib_workqueue = create_singlethread_workqueue("ipoib_flush");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 if (!ipoib_workqueue) {
1784 ret = -ENOMEM;
1785 goto err_fs;
1786 }
1787
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001788 ib_sa_register_client(&ipoib_sa_client);
1789
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 ret = ib_register_client(&ipoib_client);
1791 if (ret)
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001792 goto err_sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001794 ret = ipoib_netlink_init();
1795 if (ret)
1796 goto err_client;
1797
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798 return 0;
1799
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001800err_client:
1801 ib_unregister_client(&ipoib_client);
1802
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001803err_sa:
1804 ib_sa_unregister_client(&ipoib_sa_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 destroy_workqueue(ipoib_workqueue);
1806
Roland Dreier9adec1a2005-04-16 15:26:07 -07001807err_fs:
1808 ipoib_unregister_debugfs();
1809
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 return ret;
1811}
1812
1813static void __exit ipoib_cleanup_module(void)
1814{
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001815 ipoib_netlink_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 ib_unregister_client(&ipoib_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001817 ib_sa_unregister_client(&ipoib_sa_client);
Roland Dreier9adec1a2005-04-16 15:26:07 -07001818 ipoib_unregister_debugfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819 destroy_workqueue(ipoib_workqueue);
1820}
1821
1822module_init(ipoib_init_module);
1823module_exit(ipoib_cleanup_module);