blob: 6fdc9e78da0d454030496a3f5c048b993672aed6 [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
Linus Torvalds1da177e2005-04-16 15:20:36 -070052MODULE_AUTHOR("Roland Dreier");
53MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
54MODULE_LICENSE("Dual BSD/GPL");
55
Shirley Ma0f485252006-04-10 09:43:58 -070056int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
57int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
58
59module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
60MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
61module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
62MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
63
Linus Torvalds1da177e2005-04-16 15:20:36 -070064#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
65int ipoib_debug_level;
66
67module_param_named(debug_level, ipoib_debug_level, int, 0644);
68MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
69#endif
70
Roland Dreier1732b0e2005-11-07 10:33:11 -080071struct ipoib_path_iter {
72 struct net_device *dev;
73 struct ipoib_path path;
74};
75
Linus Torvalds1da177e2005-04-16 15:20:36 -070076static const u8 ipv4_bcast_addr[] = {
77 0x00, 0xff, 0xff, 0xff,
78 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
79 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
80};
81
82struct workqueue_struct *ipoib_workqueue;
83
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070084struct ib_sa_client ipoib_sa_client;
85
Linus Torvalds1da177e2005-04-16 15:20:36 -070086static void ipoib_add_one(struct ib_device *device);
87static void ipoib_remove_one(struct ib_device *device);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +000088static void ipoib_neigh_reclaim(struct rcu_head *rp);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
90static struct ib_client ipoib_client = {
91 .name = "ipoib",
92 .add = ipoib_add_one,
93 .remove = ipoib_remove_one
94};
95
96int ipoib_open(struct net_device *dev)
97{
98 struct ipoib_dev_priv *priv = netdev_priv(dev);
99
100 ipoib_dbg(priv, "bringing up interface\n");
101
Yossi Etigine028cc52009-04-20 13:58:08 -0700102 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103
104 if (ipoib_pkey_dev_delay_open(dev))
105 return 0;
106
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800107 if (ipoib_ib_dev_open(dev))
108 goto err_disable;
Yossi Etiginfe25c562008-11-12 10:24:36 -0800109
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800110 if (ipoib_ib_dev_up(dev))
111 goto err_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112
113 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
114 struct ipoib_dev_priv *cpriv;
115
116 /* Bring up any child interfaces too */
Ingo Molnar95ed6442006-01-13 14:51:39 -0800117 mutex_lock(&priv->vlan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118 list_for_each_entry(cpriv, &priv->child_intfs, list) {
119 int flags;
120
121 flags = cpriv->dev->flags;
122 if (flags & IFF_UP)
123 continue;
124
125 dev_change_flags(cpriv->dev, flags | IFF_UP);
126 }
Ingo Molnar95ed6442006-01-13 14:51:39 -0800127 mutex_unlock(&priv->vlan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129
130 netif_start_queue(dev);
131
132 return 0;
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800133
134err_stop:
135 ipoib_ib_dev_stop(dev, 1);
136
137err_disable:
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800138 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
139
140 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141}
142
143static int ipoib_stop(struct net_device *dev)
144{
145 struct ipoib_dev_priv *priv = netdev_priv(dev);
146
147 ipoib_dbg(priv, "stopping interface\n");
148
149 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
150
151 netif_stop_queue(dev);
152
Patrick McHardybea1e222012-08-30 07:01:30 +0000153 ipoib_ib_dev_down(dev, 1);
Roland Dreiera77a57a2008-08-19 15:01:32 -0700154 ipoib_ib_dev_stop(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
157 struct ipoib_dev_priv *cpriv;
158
159 /* Bring down any child interfaces too */
Ingo Molnar95ed6442006-01-13 14:51:39 -0800160 mutex_lock(&priv->vlan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 list_for_each_entry(cpriv, &priv->child_intfs, list) {
162 int flags;
163
164 flags = cpriv->dev->flags;
165 if (!(flags & IFF_UP))
166 continue;
167
168 dev_change_flags(cpriv->dev, flags & ~IFF_UP);
169 }
Ingo Molnar95ed6442006-01-13 14:51:39 -0800170 mutex_unlock(&priv->vlan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172
173 return 0;
174}
175
Or Gerlitz9baa0b02012-09-13 05:56:36 +0000176static void ipoib_uninit(struct net_device *dev)
177{
178 ipoib_dev_cleanup(dev);
179}
180
David S. Miller9ca36f72011-11-16 18:05:50 -0500181static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
Michał Mirosław3d96c742011-04-19 00:43:20 +0000182{
183 struct ipoib_dev_priv *priv = netdev_priv(dev);
184
185 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
186 features &= ~(NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
187
188 return features;
189}
190
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
192{
193 struct ipoib_dev_priv *priv = netdev_priv(dev);
194
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200195 /* dev->mtu > 2K ==> connected mode */
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800196 if (ipoib_cm_admin_enabled(dev)) {
197 if (new_mtu > ipoib_cm_max_mtu(dev))
198 return -EINVAL;
199
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200200 if (new_mtu > priv->mcast_mtu)
201 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
202 priv->mcast_mtu);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800203
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200204 dev->mtu = new_mtu;
205 return 0;
206 }
207
Shirley Mabc7b3a32008-04-23 11:55:45 -0700208 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209 return -EINVAL;
210
211 priv->admin_mtu = new_mtu;
212
213 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
214
215 return 0;
216}
217
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700218int ipoib_set_mode(struct net_device *dev, const char *buf)
219{
220 struct ipoib_dev_priv *priv = netdev_priv(dev);
221
222 /* flush paths if we switch modes so that connections are restarted */
223 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
224 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
225 ipoib_warn(priv, "enabling connected mode "
226 "will cause multicast packet drops\n");
227 netdev_update_features(dev);
228 rtnl_unlock();
229 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
230
231 ipoib_flush_paths(dev);
232 rtnl_lock();
233 return 0;
234 }
235
236 if (!strcmp(buf, "datagram\n")) {
237 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
238 netdev_update_features(dev);
239 dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
240 rtnl_unlock();
241 ipoib_flush_paths(dev);
242 rtnl_lock();
243 return 0;
244 }
245
246 return -EINVAL;
247}
248
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300249static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250{
251 struct ipoib_dev_priv *priv = netdev_priv(dev);
252 struct rb_node *n = priv->path_tree.rb_node;
253 struct ipoib_path *path;
254 int ret;
255
256 while (n) {
257 path = rb_entry(n, struct ipoib_path, rb_node);
258
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300259 ret = memcmp(gid, path->pathrec.dgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 sizeof (union ib_gid));
261
262 if (ret < 0)
263 n = n->rb_left;
264 else if (ret > 0)
265 n = n->rb_right;
266 else
267 return path;
268 }
269
270 return NULL;
271}
272
273static int __path_add(struct net_device *dev, struct ipoib_path *path)
274{
275 struct ipoib_dev_priv *priv = netdev_priv(dev);
276 struct rb_node **n = &priv->path_tree.rb_node;
277 struct rb_node *pn = NULL;
278 struct ipoib_path *tpath;
279 int ret;
280
281 while (*n) {
282 pn = *n;
283 tpath = rb_entry(pn, struct ipoib_path, rb_node);
284
285 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
286 sizeof (union ib_gid));
287 if (ret < 0)
288 n = &pn->rb_left;
289 else if (ret > 0)
290 n = &pn->rb_right;
291 else
292 return -EEXIST;
293 }
294
295 rb_link_node(&path->rb_node, pn, n);
296 rb_insert_color(&path->rb_node, &priv->path_tree);
297
298 list_add_tail(&path->list, &priv->path_list);
299
300 return 0;
301}
302
303static void path_free(struct net_device *dev, struct ipoib_path *path)
304{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
307 while ((skb = __skb_dequeue(&path->queue)))
308 dev_kfree_skb_irq(skb);
309
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000310 ipoib_dbg(netdev_priv(dev), "path_free\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000312 /* remove all neigh connected to this path */
313 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
315 if (path->ah)
316 ipoib_put_ah(path->ah);
317
318 kfree(path);
319}
320
Roland Dreier1732b0e2005-11-07 10:33:11 -0800321#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
322
323struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
324{
325 struct ipoib_path_iter *iter;
326
327 iter = kmalloc(sizeof *iter, GFP_KERNEL);
328 if (!iter)
329 return NULL;
330
331 iter->dev = dev;
332 memset(iter->path.pathrec.dgid.raw, 0, 16);
333
334 if (ipoib_path_iter_next(iter)) {
335 kfree(iter);
336 return NULL;
337 }
338
339 return iter;
340}
341
342int ipoib_path_iter_next(struct ipoib_path_iter *iter)
343{
344 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
345 struct rb_node *n;
346 struct ipoib_path *path;
347 int ret = 1;
348
349 spin_lock_irq(&priv->lock);
350
351 n = rb_first(&priv->path_tree);
352
353 while (n) {
354 path = rb_entry(n, struct ipoib_path, rb_node);
355
356 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
357 sizeof (union ib_gid)) < 0) {
358 iter->path = *path;
359 ret = 0;
360 break;
361 }
362
363 n = rb_next(n);
364 }
365
366 spin_unlock_irq(&priv->lock);
367
368 return ret;
369}
370
371void ipoib_path_iter_read(struct ipoib_path_iter *iter,
372 struct ipoib_path *path)
373{
374 *path = iter->path;
375}
376
377#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
378
Moni Shouaee1e2c82008-07-14 23:48:49 -0700379void ipoib_mark_paths_invalid(struct net_device *dev)
380{
381 struct ipoib_dev_priv *priv = netdev_priv(dev);
382 struct ipoib_path *path, *tp;
383
384 spin_lock_irq(&priv->lock);
385
386 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700387 ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
Moni Shouaee1e2c82008-07-14 23:48:49 -0700388 be16_to_cpu(path->pathrec.dlid),
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700389 path->pathrec.dgid.raw);
Moni Shouaee1e2c82008-07-14 23:48:49 -0700390 path->valid = 0;
391 }
392
393 spin_unlock_irq(&priv->lock);
394}
395
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396void ipoib_flush_paths(struct net_device *dev)
397{
398 struct ipoib_dev_priv *priv = netdev_priv(dev);
399 struct ipoib_path *path, *tp;
400 LIST_HEAD(remove_list);
Roland Dreier943c2462008-09-30 10:36:21 -0700401 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402
Roland Dreier943c2462008-09-30 10:36:21 -0700403 netif_tx_lock_bh(dev);
404 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405
Robert P. J. Day157de222008-04-16 21:09:26 -0700406 list_splice_init(&priv->path_list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
408 list_for_each_entry(path, &remove_list, list)
409 rb_erase(&path->rb_node, &priv->path_tree);
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 list_for_each_entry_safe(path, tp, &remove_list, list) {
412 if (path->query)
413 ib_sa_cancel_query(path->query_id, path->query);
Roland Dreier943c2462008-09-30 10:36:21 -0700414 spin_unlock_irqrestore(&priv->lock, flags);
415 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 wait_for_completion(&path->done);
417 path_free(dev, path);
Roland Dreier943c2462008-09-30 10:36:21 -0700418 netif_tx_lock_bh(dev);
419 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
Roland Dreier943c2462008-09-30 10:36:21 -0700421
422 spin_unlock_irqrestore(&priv->lock, flags);
423 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424}
425
426static void path_rec_completion(int status,
427 struct ib_sa_path_rec *pathrec,
428 void *path_ptr)
429{
430 struct ipoib_path *path = path_ptr;
431 struct net_device *dev = path->dev;
432 struct ipoib_dev_priv *priv = netdev_priv(dev);
433 struct ipoib_ah *ah = NULL;
Roland Dreierc9da4ba2008-09-25 15:26:15 -0700434 struct ipoib_ah *old_ah = NULL;
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700435 struct ipoib_neigh *neigh, *tn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 struct sk_buff_head skqueue;
437 struct sk_buff *skb;
438 unsigned long flags;
439
Roland Dreier843613b2007-02-26 12:57:08 -0800440 if (!status)
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700441 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700442 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 else
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700444 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700445 status, path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
447 skb_queue_head_init(&skqueue);
448
449 if (!status) {
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700450 struct ib_ah_attr av;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700452 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
453 ah = ipoib_create_ah(dev, priv->pd, &av);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454 }
455
456 spin_lock_irqsave(&priv->lock, flags);
457
Mike Marciniszyn38743972011-11-21 08:43:54 -0500458 if (!IS_ERR_OR_NULL(ah)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 path->pathrec = *pathrec;
460
Roland Dreierc9da4ba2008-09-25 15:26:15 -0700461 old_ah = path->ah;
462 path->ah = ah;
463
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
465 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
466
467 while ((skb = __skb_dequeue(&path->queue)))
468 __skb_queue_tail(&skqueue, skb);
469
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700470 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
Moni Shouaee1e2c82008-07-14 23:48:49 -0700471 if (neigh->ah) {
472 WARN_ON(neigh->ah != old_ah);
473 /*
474 * Dropping the ah reference inside
475 * priv->lock is safe here, because we
476 * will hold one more reference from
477 * the original value of path->ah (ie
478 * old_ah).
479 */
480 ipoib_put_ah(neigh->ah);
481 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 kref_get(&path->ah->ref);
483 neigh->ah = path->ah;
484
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000485 if (ipoib_cm_enabled(dev, neigh->daddr)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200486 if (!ipoib_cm_get(neigh))
487 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
488 path,
489 neigh));
490 if (!ipoib_cm_get(neigh)) {
491 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000492 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200493 continue;
494 }
495 }
496
Linus Torvalds1da177e2005-04-16 15:20:36 -0700497 while ((skb = __skb_dequeue(&neigh->queue)))
498 __skb_queue_tail(&skqueue, skb);
499 }
Moni Shouaee1e2c82008-07-14 23:48:49 -0700500 path->valid = 1;
Roland Dreier5872a9f2005-11-29 10:13:54 -0800501 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502
Roland Dreier5872a9f2005-11-29 10:13:54 -0800503 path->query = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 complete(&path->done);
505
506 spin_unlock_irqrestore(&priv->lock, flags);
507
Moni Shouaee1e2c82008-07-14 23:48:49 -0700508 if (old_ah)
509 ipoib_put_ah(old_ah);
510
Linus Torvalds1da177e2005-04-16 15:20:36 -0700511 while ((skb = __skb_dequeue(&skqueue))) {
512 skb->dev = dev;
513 if (dev_queue_xmit(skb))
514 ipoib_warn(priv, "dev_queue_xmit failed "
515 "to requeue packet\n");
516 }
517}
518
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300519static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520{
521 struct ipoib_dev_priv *priv = netdev_priv(dev);
522 struct ipoib_path *path;
523
Jack Morgenstein1401b532007-11-26 10:41:19 +0200524 if (!priv->broadcast)
525 return NULL;
526
Roland Dreier21a38482005-11-02 10:07:59 -0800527 path = kzalloc(sizeof *path, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 if (!path)
529 return NULL;
530
Roland Dreier21a38482005-11-02 10:07:59 -0800531 path->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700532
533 skb_queue_head_init(&path->queue);
534
535 INIT_LIST_HEAD(&path->neigh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300537 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
Roland Dreier2337f802007-10-23 19:57:54 -0700538 path->pathrec.sgid = priv->local_gid;
539 path->pathrec.pkey = cpu_to_be16(priv->pkey);
Sean Hefty81668832007-08-02 12:21:31 -0700540 path->pathrec.numb_path = 1;
541 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542
543 return path;
544}
545
546static int path_rec_start(struct net_device *dev,
547 struct ipoib_path *path)
548{
549 struct ipoib_dev_priv *priv = netdev_priv(dev);
550
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700551 ipoib_dbg(priv, "Start path record lookup for %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700552 path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Roland Dreier65c7edd2005-11-28 21:20:34 -0800554 init_completion(&path->done);
555
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556 path->query_id =
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700557 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 &path->pathrec,
559 IB_SA_PATH_REC_DGID |
560 IB_SA_PATH_REC_SGID |
561 IB_SA_PATH_REC_NUMB_PATH |
Sean Hefty81668832007-08-02 12:21:31 -0700562 IB_SA_PATH_REC_TRAFFIC_CLASS |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 IB_SA_PATH_REC_PKEY,
564 1000, GFP_ATOMIC,
565 path_rec_completion,
566 path, &path->query);
567 if (path->query_id < 0) {
Or Gerlitz01b3fc82008-07-22 14:18:34 -0700568 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 path->query = NULL;
Yossi Etigin93a3ab92008-11-12 10:24:38 -0800570 complete(&path->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 return path->query_id;
572 }
573
574 return 0;
575}
576
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000577static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
578 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700579{
580 struct ipoib_dev_priv *priv = netdev_priv(dev);
581 struct ipoib_path *path;
582 struct ipoib_neigh *neigh;
Roland Dreier943c2462008-09-30 10:36:21 -0700583 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000585 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000586 neigh = ipoib_neigh_alloc(daddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587 if (!neigh) {
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000588 spin_unlock_irqrestore(&priv->lock, flags);
Roland Dreierde903512007-09-28 15:33:51 -0700589 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 dev_kfree_skb_any(skb);
591 return;
592 }
593
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000594 path = __path_find(dev, daddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 if (!path) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000596 path = path_rec_create(dev, daddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 if (!path)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300598 goto err_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600 __path_add(dev, path);
601 }
602
603 list_add_tail(&neigh->list, &path->neigh_list);
604
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -0800605 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 kref_get(&path->ah->ref);
607 neigh->ah = path->ah;
608
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000609 if (ipoib_cm_enabled(dev, neigh->daddr)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200610 if (!ipoib_cm_get(neigh))
611 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
612 if (!ipoib_cm_get(neigh)) {
613 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000614 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200615 goto err_drop;
616 }
617 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
618 __skb_queue_tail(&neigh->queue, skb);
619 else {
620 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
621 skb_queue_len(&neigh->queue));
622 goto err_drop;
623 }
Roland Dreier721d67c2009-09-05 20:23:40 -0700624 } else {
625 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000626 ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
627 ipoib_neigh_put(neigh);
Roland Dreier721d67c2009-09-05 20:23:40 -0700628 return;
629 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 } else {
631 neigh->ah = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 if (!path->query && path_rec_start(dev, path))
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300634 goto err_list;
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +0200635
636 __skb_queue_tail(&neigh->queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 }
638
Roland Dreier943c2462008-09-30 10:36:21 -0700639 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000640 ipoib_neigh_put(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641 return;
642
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300643err_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 list_del(&neigh->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300646err_path:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000647 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200648err_drop:
Roland Dreierde903512007-09-28 15:33:51 -0700649 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 dev_kfree_skb_any(skb);
651
Roland Dreier943c2462008-09-30 10:36:21 -0700652 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000653 ipoib_neigh_put(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700654}
655
656static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
Roland Dreier936d7de2012-02-07 14:51:21 +0000657 struct ipoib_cb *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
659 struct ipoib_dev_priv *priv = netdev_priv(dev);
660 struct ipoib_path *path;
Roland Dreier943c2462008-09-30 10:36:21 -0700661 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662
Roland Dreier943c2462008-09-30 10:36:21 -0700663 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Roland Dreier936d7de2012-02-07 14:51:21 +0000665 path = __path_find(dev, cb->hwaddr + 4);
Moni Shouaee1e2c82008-07-14 23:48:49 -0700666 if (!path || !path->valid) {
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800667 int new_path = 0;
668
669 if (!path) {
Roland Dreier936d7de2012-02-07 14:51:21 +0000670 path = path_rec_create(dev, cb->hwaddr + 4);
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800671 new_path = 1;
672 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 if (path) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 __skb_queue_tail(&path->queue, skb);
675
Yossi Etiginff79ae82008-11-12 10:24:39 -0800676 if (!path->query && path_rec_start(dev, path)) {
Roland Dreier943c2462008-09-30 10:36:21 -0700677 spin_unlock_irqrestore(&priv->lock, flags);
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800678 if (new_path)
679 path_free(dev, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 return;
681 } else
682 __path_add(dev, path);
683 } else {
Roland Dreierde903512007-09-28 15:33:51 -0700684 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 dev_kfree_skb_any(skb);
686 }
687
Roland Dreier943c2462008-09-30 10:36:21 -0700688 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 return;
690 }
691
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -0800692 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
694 be16_to_cpu(path->pathrec.dlid));
695
Roland Dreier721d67c2009-09-05 20:23:40 -0700696 spin_unlock_irqrestore(&priv->lock, flags);
Roland Dreier936d7de2012-02-07 14:51:21 +0000697 ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr));
Roland Dreier721d67c2009-09-05 20:23:40 -0700698 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700699 } else if ((path->query || !path_rec_start(dev, path)) &&
700 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 __skb_queue_tail(&path->queue, skb);
702 } else {
Roland Dreierde903512007-09-28 15:33:51 -0700703 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 dev_kfree_skb_any(skb);
705 }
706
Roland Dreier943c2462008-09-30 10:36:21 -0700707 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708}
709
710static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
711{
712 struct ipoib_dev_priv *priv = netdev_priv(dev);
713 struct ipoib_neigh *neigh;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000714 struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
715 struct ipoib_header *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 unsigned long flags;
717
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000718 header = (struct ipoib_header *) skb->data;
719
720 if (unlikely(cb->hwaddr[4] == 0xff)) {
721 /* multicast, arrange "if" according to probability */
722 if ((header->proto != htons(ETH_P_IP)) &&
723 (header->proto != htons(ETH_P_IPV6)) &&
724 (header->proto != htons(ETH_P_ARP)) &&
725 (header->proto != htons(ETH_P_RARP))) {
726 /* ethertype not supported by IPoIB */
David Miller17e6abe2011-12-02 16:52:44 +0000727 ++dev->stats.tx_dropped;
728 dev_kfree_skb_any(skb);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000729 return NETDEV_TX_OK;
David Miller17e6abe2011-12-02 16:52:44 +0000730 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000731 /* Add in the P_Key for multicast*/
732 cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
733 cb->hwaddr[9] = priv->pkey & 0xff;
734
735 neigh = ipoib_neigh_get(dev, cb->hwaddr);
736 if (likely(neigh))
737 goto send_using_neigh;
738 ipoib_mcast_send(dev, cb->hwaddr, skb);
739 return NETDEV_TX_OK;
David Miller17e6abe2011-12-02 16:52:44 +0000740 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000742 /* unicast, arrange "switch" according to probability */
743 switch (header->proto) {
744 case htons(ETH_P_IP):
745 case htons(ETH_P_IPV6):
746 neigh = ipoib_neigh_get(dev, cb->hwaddr);
747 if (unlikely(!neigh)) {
748 neigh_add_path(skb, cb->hwaddr, dev);
749 return NETDEV_TX_OK;
Yossi Etigina50df392009-01-09 14:05:11 -0800750 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000751 break;
752 case htons(ETH_P_ARP):
753 case htons(ETH_P_RARP):
754 /* for unicast ARP and RARP should always perform path find */
755 unicast_arp_send(skb, dev, cb);
756 return NETDEV_TX_OK;
757 default:
758 /* ethertype not supported by IPoIB */
759 ++dev->stats.tx_dropped;
760 dev_kfree_skb_any(skb);
761 return NETDEV_TX_OK;
762 }
Michael S. Tsirkin8a7f7522006-07-19 17:44:37 +0300763
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000764send_using_neigh:
765 /* note we now hold a ref to neigh */
766 if (ipoib_cm_get(neigh)) {
767 if (ipoib_cm_up(neigh)) {
768 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
769 goto unref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000771 } else if (neigh->ah) {
772 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
773 goto unref;
774 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000776 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
777 spin_lock_irqsave(&priv->lock, flags);
778 __skb_queue_tail(&neigh->queue, skb);
779 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 } else {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000781 ++dev->stats.tx_dropped;
782 dev_kfree_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700783 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000784
785unref:
786 ipoib_neigh_put(neigh);
787
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 return NETDEV_TX_OK;
789}
790
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791static void ipoib_timeout(struct net_device *dev)
792{
793 struct ipoib_dev_priv *priv = netdev_priv(dev);
794
Roland Dreier4b2d3192005-10-18 12:20:06 -0700795 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
796 jiffies_to_msecs(jiffies - dev->trans_start));
797 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
798 netif_queue_stopped(dev),
799 priv->tx_head, priv->tx_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 /* XXX reset QP, etc. */
801}
802
803static int ipoib_hard_header(struct sk_buff *skb,
804 struct net_device *dev,
805 unsigned short type,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700806 const void *daddr, const void *saddr, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807{
808 struct ipoib_header *header;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000809 struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810
811 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
812
813 header->proto = htons(type);
814 header->reserved = 0;
815
816 /*
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000817 * we don't rely on dst_entry structure, always stuff the
Roland Dreier936d7de2012-02-07 14:51:21 +0000818 * destination address into skb->cb so we can figure out where
819 * to send the packet later.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820 */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000821 memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823 return 0;
824}
825
826static void ipoib_set_mcast_list(struct net_device *dev)
827{
828 struct ipoib_dev_priv *priv = netdev_priv(dev);
829
Leonid Arsh7a343d42006-03-23 19:52:51 +0200830 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
831 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
832 return;
833 }
834
Michael S. Tsirkin1ad62a12005-08-24 14:41:51 -0700835 queue_work(ipoib_workqueue, &priv->restart_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836}
837
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000838static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700839{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000840 /*
841 * Use only the address parts that contributes to spreading
842 * The subnet prefix is not used as one can not connect to
843 * same remote port (GUID) using the same remote QPN via two
844 * different subnets.
845 */
846 /* qpn octets[1:4) & port GUID octets[12:20) */
847 u32 *daddr_32 = (u32 *) daddr;
848 u32 hv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000850 hv = jhash_3words(daddr_32[3], daddr_32[4], 0xFFFFFF & daddr_32[0], 0);
851 return hv & htbl->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852}
853
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000854struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
855{
856 struct ipoib_dev_priv *priv = netdev_priv(dev);
857 struct ipoib_neigh_table *ntbl = &priv->ntbl;
858 struct ipoib_neigh_hash *htbl;
859 struct ipoib_neigh *neigh = NULL;
860 u32 hash_val;
861
862 rcu_read_lock_bh();
863
864 htbl = rcu_dereference_bh(ntbl->htbl);
865
866 if (!htbl)
867 goto out_unlock;
868
869 hash_val = ipoib_addr_hash(htbl, daddr);
870 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
871 neigh != NULL;
872 neigh = rcu_dereference_bh(neigh->hnext)) {
873 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
874 /* found, take one ref on behalf of the caller */
875 if (!atomic_inc_not_zero(&neigh->refcnt)) {
876 /* deleted */
877 neigh = NULL;
878 goto out_unlock;
879 }
880 neigh->alive = jiffies;
881 goto out_unlock;
882 }
883 }
884
885out_unlock:
886 rcu_read_unlock_bh();
887 return neigh;
888}
889
890static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
891{
892 struct ipoib_neigh_table *ntbl = &priv->ntbl;
893 struct ipoib_neigh_hash *htbl;
894 unsigned long neigh_obsolete;
895 unsigned long dt;
896 unsigned long flags;
897 int i;
898
899 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
900 return;
901
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000902 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000903
904 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000905 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000906
907 if (!htbl)
908 goto out_unlock;
909
910 /* neigh is obsolete if it was idle for two GC periods */
911 dt = 2 * arp_tbl.gc_interval;
912 neigh_obsolete = jiffies - dt;
913 /* handle possible race condition */
914 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
915 goto out_unlock;
916
917 for (i = 0; i < htbl->size; i++) {
918 struct ipoib_neigh *neigh;
919 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
920
921 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000922 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000923 /* was the neigh idle for two GC periods */
924 if (time_after(neigh_obsolete, neigh->alive)) {
925 rcu_assign_pointer(*np,
926 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000927 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000928 /* remove from path/mc list */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000929 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000930 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
931 } else {
932 np = &neigh->hnext;
933 }
934
935 }
936 }
937
938out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000939 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000940}
941
942static void ipoib_reap_neigh(struct work_struct *work)
943{
944 struct ipoib_dev_priv *priv =
945 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
946
947 __ipoib_reap_neigh(priv);
948
949 if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
950 queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
951 arp_tbl.gc_interval);
952}
953
954
955static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
Moni Shoua732a2172007-10-09 19:43:36 -0700956 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300957{
958 struct ipoib_neigh *neigh;
959
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000960 neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300961 if (!neigh)
962 return NULL;
963
Moni Shoua732a2172007-10-09 19:43:36 -0700964 neigh->dev = dev;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000965 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
Roland Dreier82b39912006-12-12 14:48:18 -0800966 skb_queue_head_init(&neigh->queue);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000967 INIT_LIST_HEAD(&neigh->list);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200968 ipoib_cm_set(neigh, NULL);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000969 /* one ref on behalf of the caller */
970 atomic_set(&neigh->refcnt, 1);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300971
972 return neigh;
973}
974
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000975struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
976 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300977{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000978 struct ipoib_dev_priv *priv = netdev_priv(dev);
979 struct ipoib_neigh_table *ntbl = &priv->ntbl;
980 struct ipoib_neigh_hash *htbl;
981 struct ipoib_neigh *neigh;
982 u32 hash_val;
983
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000984 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000985 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000986 if (!htbl) {
987 neigh = NULL;
988 goto out_unlock;
989 }
990
991 /* need to add a new neigh, but maybe some other thread succeeded?
992 * recalc hash, maybe hash resize took place so we do a search
993 */
994 hash_val = ipoib_addr_hash(htbl, daddr);
995 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000996 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000997 neigh != NULL;
998 neigh = rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000999 lockdep_is_held(&priv->lock))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001000 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1001 /* found, take one ref on behalf of the caller */
1002 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1003 /* deleted */
1004 neigh = NULL;
1005 break;
1006 }
1007 neigh->alive = jiffies;
1008 goto out_unlock;
1009 }
1010 }
1011
1012 neigh = ipoib_neigh_ctor(daddr, dev);
1013 if (!neigh)
1014 goto out_unlock;
1015
1016 /* one ref on behalf of the hash table */
1017 atomic_inc(&neigh->refcnt);
1018 neigh->alive = jiffies;
1019 /* put in hash */
1020 rcu_assign_pointer(neigh->hnext,
1021 rcu_dereference_protected(htbl->buckets[hash_val],
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001022 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001023 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1024 atomic_inc(&ntbl->entries);
1025
1026out_unlock:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001027
1028 return neigh;
1029}
1030
1031void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1032{
1033 /* neigh reference count was dropprd to zero */
1034 struct net_device *dev = neigh->dev;
1035 struct ipoib_dev_priv *priv = netdev_priv(dev);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001036 struct sk_buff *skb;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001037 if (neigh->ah)
1038 ipoib_put_ah(neigh->ah);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001039 while ((skb = __skb_dequeue(&neigh->queue))) {
Roland Dreierde903512007-09-28 15:33:51 -07001040 ++dev->stats.tx_dropped;
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001041 dev_kfree_skb_any(skb);
1042 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001043 if (ipoib_cm_get(neigh))
1044 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001045 ipoib_dbg(netdev_priv(dev),
1046 "neigh free for %06x %pI6\n",
1047 IPOIB_QPN(neigh->daddr),
1048 neigh->daddr + 4);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001049 kfree(neigh);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001050 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1051 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1052 complete(&priv->ntbl.flushed);
1053 }
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001054}
1055
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001056static void ipoib_neigh_reclaim(struct rcu_head *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001058 /* Called as a result of removal from hash table */
1059 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1060 /* note TX context may hold another ref */
1061 ipoib_neigh_put(neigh);
1062}
1063
1064void ipoib_neigh_free(struct ipoib_neigh *neigh)
1065{
1066 struct net_device *dev = neigh->dev;
1067 struct ipoib_dev_priv *priv = netdev_priv(dev);
1068 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1069 struct ipoib_neigh_hash *htbl;
1070 struct ipoib_neigh __rcu **np;
1071 struct ipoib_neigh *n;
1072 u32 hash_val;
1073
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001074 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001075 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001076 if (!htbl)
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001077 return;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001078
1079 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1080 np = &htbl->buckets[hash_val];
1081 for (n = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001082 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001083 n != NULL;
Shlomo Pongratz6c723a62012-08-13 14:39:50 +00001084 n = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001085 lockdep_is_held(&priv->lock))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001086 if (n == neigh) {
1087 /* found */
1088 rcu_assign_pointer(*np,
1089 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001090 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001091 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001092 return;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001093 } else {
1094 np = &n->hnext;
1095 }
1096 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001097}
1098
1099static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1100{
1101 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1102 struct ipoib_neigh_hash *htbl;
1103 struct ipoib_neigh **buckets;
1104 u32 size;
1105
1106 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1107 ntbl->htbl = NULL;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001108 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1109 if (!htbl)
1110 return -ENOMEM;
1111 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1112 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1113 buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1114 if (!buckets) {
1115 kfree(htbl);
1116 return -ENOMEM;
1117 }
1118 htbl->size = size;
1119 htbl->mask = (size - 1);
1120 htbl->buckets = buckets;
1121 ntbl->htbl = htbl;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001122 htbl->ntbl = ntbl;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001123 atomic_set(&ntbl->entries, 0);
1124
1125 /* start garbage collection */
1126 clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1127 queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
1128 arp_tbl.gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129
1130 return 0;
1131}
1132
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001133static void neigh_hash_free_rcu(struct rcu_head *head)
1134{
1135 struct ipoib_neigh_hash *htbl = container_of(head,
1136 struct ipoib_neigh_hash,
1137 rcu);
1138 struct ipoib_neigh __rcu **buckets = htbl->buckets;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001139 struct ipoib_neigh_table *ntbl = htbl->ntbl;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001140
1141 kfree(buckets);
1142 kfree(htbl);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001143 complete(&ntbl->deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001144}
1145
1146void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1147{
1148 struct ipoib_dev_priv *priv = netdev_priv(dev);
1149 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1150 struct ipoib_neigh_hash *htbl;
1151 unsigned long flags;
1152 int i;
1153
1154 /* remove all neigh connected to a given path or mcast */
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001155 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001156
1157 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001158 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001159
1160 if (!htbl)
1161 goto out_unlock;
1162
1163 for (i = 0; i < htbl->size; i++) {
1164 struct ipoib_neigh *neigh;
1165 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1166
1167 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001168 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001169 /* delete neighs belong to this parent */
1170 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1171 rcu_assign_pointer(*np,
1172 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001173 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001174 /* remove from parent list */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001175 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001176 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1177 } else {
1178 np = &neigh->hnext;
1179 }
1180
1181 }
1182 }
1183out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001184 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001185}
1186
1187static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1188{
1189 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1190 struct ipoib_neigh_hash *htbl;
1191 unsigned long flags;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001192 int i, wait_flushed = 0;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001193
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001194 init_completion(&priv->ntbl.flushed);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001195
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001196 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001197
1198 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001199 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001200 if (!htbl)
1201 goto out_unlock;
1202
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001203 wait_flushed = atomic_read(&priv->ntbl.entries);
1204 if (!wait_flushed)
1205 goto free_htbl;
1206
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001207 for (i = 0; i < htbl->size; i++) {
1208 struct ipoib_neigh *neigh;
1209 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1210
1211 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001212 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001213 rcu_assign_pointer(*np,
1214 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001215 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001216 /* remove from path/mc list */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001217 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001218 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1219 }
1220 }
1221
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001222free_htbl:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001223 rcu_assign_pointer(ntbl->htbl, NULL);
1224 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1225
1226out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001227 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001228 if (wait_flushed)
1229 wait_for_completion(&priv->ntbl.flushed);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001230}
1231
1232static void ipoib_neigh_hash_uninit(struct net_device *dev)
1233{
1234 struct ipoib_dev_priv *priv = netdev_priv(dev);
1235 int stopped;
1236
1237 ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001238 init_completion(&priv->ntbl.deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001239 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1240
1241 /* Stop GC if called at init fail need to cancel work */
1242 stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1243 if (!stopped)
1244 cancel_delayed_work(&priv->neigh_reap_task);
1245
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001246 ipoib_flush_neighs(priv);
1247
1248 wait_for_completion(&priv->ntbl.deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001249}
1250
1251
Linus Torvalds1da177e2005-04-16 15:20:36 -07001252int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1253{
1254 struct ipoib_dev_priv *priv = netdev_priv(dev);
1255
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001256 if (ipoib_neigh_hash_init(priv) < 0)
1257 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001258 /* Allocate RX/TX "rings" to hold queued skbs */
Shirley Ma0f485252006-04-10 09:43:58 -07001259 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 GFP_KERNEL);
1261 if (!priv->rx_ring) {
1262 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
Shirley Ma0f485252006-04-10 09:43:58 -07001263 ca->name, ipoib_recvq_size);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001264 goto out_neigh_hash_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266
Joe Perches948579c2010-11-05 03:07:36 +00001267 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 if (!priv->tx_ring) {
1269 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
Shirley Ma0f485252006-04-10 09:43:58 -07001270 ca->name, ipoib_sendq_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271 goto out_rx_ring_cleanup;
1272 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001274 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275
1276 if (ipoib_ib_dev_init(dev, ca, port))
1277 goto out_tx_ring_cleanup;
1278
1279 return 0;
1280
1281out_tx_ring_cleanup:
Roland Dreier10313cb2008-03-12 07:51:03 -07001282 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001283
1284out_rx_ring_cleanup:
1285 kfree(priv->rx_ring);
1286
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001287out_neigh_hash_cleanup:
1288 ipoib_neigh_hash_uninit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001289out:
1290 return -ENOMEM;
1291}
1292
1293void ipoib_dev_cleanup(struct net_device *dev)
1294{
1295 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001296 LIST_HEAD(head);
1297
1298 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001299
Roland Dreier1732b0e2005-11-07 10:33:11 -08001300 ipoib_delete_debug_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001301
1302 /* Delete any child interfaces first */
1303 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001304 /* Stop GC on child */
1305 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1306 cancel_delayed_work(&cpriv->neigh_reap_task);
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001307 unregister_netdevice_queue(cpriv->dev, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001308 }
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001309 unregister_netdevice_many(&head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
1311 ipoib_ib_dev_cleanup(dev);
1312
Hal Rosenstock92a6b342005-08-13 20:50:27 -07001313 kfree(priv->rx_ring);
Roland Dreier10313cb2008-03-12 07:51:03 -07001314 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
Hal Rosenstock92a6b342005-08-13 20:50:27 -07001316 priv->rx_ring = NULL;
1317 priv->tx_ring = NULL;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001318
1319 ipoib_neigh_hash_uninit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320}
1321
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001322static const struct header_ops ipoib_header_ops = {
1323 .create = ipoib_hard_header,
1324};
1325
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001326static const struct net_device_ops ipoib_netdev_ops = {
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001327 .ndo_uninit = ipoib_uninit,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001328 .ndo_open = ipoib_open,
1329 .ndo_stop = ipoib_stop,
1330 .ndo_change_mtu = ipoib_change_mtu,
Michał Mirosław3d96c742011-04-19 00:43:20 +00001331 .ndo_fix_features = ipoib_fix_features,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001332 .ndo_start_xmit = ipoib_start_xmit,
1333 .ndo_tx_timeout = ipoib_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001334 .ndo_set_rx_mode = ipoib_set_mcast_list,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001335};
1336
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001337void ipoib_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338{
1339 struct ipoib_dev_priv *priv = netdev_priv(dev);
1340
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001341 dev->netdev_ops = &ipoib_netdev_ops;
Roland Dreier2337f802007-10-23 19:57:54 -07001342 dev->header_ops = &ipoib_header_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001343
Eli Cohen82c24c12008-04-16 21:09:32 -07001344 ipoib_set_ethtool_ops(dev);
1345
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001346 netif_napi_add(dev, &priv->napi, ipoib_poll, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347
Roland Dreier2337f802007-10-23 19:57:54 -07001348 dev->watchdog_timeo = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349
Roland Dreier2337f802007-10-23 19:57:54 -07001350 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351
Roland Dreier936d7de2012-02-07 14:51:21 +00001352 dev->hard_header_len = IPOIB_ENCAP_LEN;
Roland Dreier2337f802007-10-23 19:57:54 -07001353 dev->addr_len = INFINIBAND_ALEN;
1354 dev->type = ARPHRD_INFINIBAND;
1355 dev->tx_queue_len = ipoib_sendq_size * 2;
Eli Coheneb14032f2008-01-30 18:30:46 +02001356 dev->features = (NETIF_F_VLAN_CHALLENGED |
Eli Coheneb14032f2008-01-30 18:30:46 +02001357 NETIF_F_HIGHDMA);
Eric Dumazet86d15cd2009-05-30 23:04:46 -07001358 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001359
Linus Torvalds1da177e2005-04-16 15:20:36 -07001360 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
1361
1362 netif_carrier_off(dev);
1363
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 priv->dev = dev;
1365
1366 spin_lock_init(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367
Ingo Molnar95ed6442006-01-13 14:51:39 -08001368 mutex_init(&priv->vlan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369
1370 INIT_LIST_HEAD(&priv->path_list);
1371 INIT_LIST_HEAD(&priv->child_intfs);
1372 INIT_LIST_HEAD(&priv->dead_ahs);
1373 INIT_LIST_HEAD(&priv->multicast_list);
1374
Yosef Etigin26bbf132007-05-19 08:51:54 -07001375 INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
David Howellsc4028952006-11-22 14:57:56 +00001376 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
Yossi Etigine8224e42008-09-16 11:57:45 -07001377 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
Moni Shouaee1e2c82008-07-14 23:48:49 -07001378 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
1379 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
1380 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
David Howellsc4028952006-11-22 14:57:56 +00001381 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1382 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001383 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384}
1385
1386struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
1387{
1388 struct net_device *dev;
1389
1390 dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
1391 ipoib_setup);
1392 if (!dev)
1393 return NULL;
1394
1395 return netdev_priv(dev);
1396}
1397
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001398static ssize_t show_pkey(struct device *dev,
1399 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001400{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001401 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403 return sprintf(buf, "0x%04x\n", priv->pkey);
1404}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001405static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001406
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001407static ssize_t show_umcast(struct device *dev,
1408 struct device_attribute *attr, char *buf)
1409{
1410 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1411
1412 return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1413}
1414
Or Gerlitz862096a2012-09-27 12:06:02 +00001415void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001416{
Or Gerlitz862096a2012-09-27 12:06:02 +00001417 struct ipoib_dev_priv *priv = netdev_priv(ndev);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001418
1419 if (umcast_val > 0) {
1420 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1421 ipoib_warn(priv, "ignoring multicast groups joined directly "
1422 "by userspace\n");
1423 } else
1424 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
Or Gerlitz862096a2012-09-27 12:06:02 +00001425}
1426
1427static ssize_t set_umcast(struct device *dev,
1428 struct device_attribute *attr,
1429 const char *buf, size_t count)
1430{
1431 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1432
1433 ipoib_set_umcast(to_net_dev(dev), umcast_val);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001434
1435 return count;
1436}
1437static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1438
1439int ipoib_add_umcast_attr(struct net_device *dev)
1440{
1441 return device_create_file(&dev->dev, &dev_attr_umcast);
1442}
1443
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001444static ssize_t create_child(struct device *dev,
1445 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 const char *buf, size_t count)
1447{
1448 int pkey;
1449 int ret;
1450
1451 if (sscanf(buf, "%i", &pkey) != 1)
1452 return -EINVAL;
1453
1454 if (pkey < 0 || pkey > 0xffff)
1455 return -EINVAL;
1456
Roland Dreier4ce05932005-08-19 12:03:17 -07001457 /*
1458 * Set the full membership bit, so that we join the right
1459 * broadcast group, etc.
1460 */
1461 pkey |= 0x8000;
1462
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001463 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001464
1465 return ret ? ret : count;
1466}
Or Gerlitz7a52b342010-06-06 04:59:16 +00001467static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001468
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001469static ssize_t delete_child(struct device *dev,
1470 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001471 const char *buf, size_t count)
1472{
1473 int pkey;
1474 int ret;
1475
1476 if (sscanf(buf, "%i", &pkey) != 1)
1477 return -EINVAL;
1478
1479 if (pkey < 0 || pkey > 0xffff)
1480 return -EINVAL;
1481
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001482 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483
1484 return ret ? ret : count;
1485
1486}
Or Gerlitz7a52b342010-06-06 04:59:16 +00001487static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489int ipoib_add_pkey_attr(struct net_device *dev)
1490{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001491 return device_create_file(&dev->dev, &dev_attr_pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001492}
1493
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001494int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
1495{
1496 struct ib_device_attr *device_attr;
1497 int result = -ENOMEM;
1498
1499 device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
1500 if (!device_attr) {
1501 printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
1502 hca->name, sizeof *device_attr);
1503 return result;
1504 }
1505
1506 result = ib_query_device(hca, device_attr);
1507 if (result) {
1508 printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
1509 hca->name, result);
1510 kfree(device_attr);
1511 return result;
1512 }
1513 priv->hca_caps = device_attr->device_cap_flags;
1514
1515 kfree(device_attr);
1516
1517 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
Michał Mirosław3d96c742011-04-19 00:43:20 +00001518 priv->dev->hw_features = NETIF_F_SG |
1519 NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1520
1521 if (priv->hca_caps & IB_DEVICE_UD_TSO)
1522 priv->dev->hw_features |= NETIF_F_TSO;
1523
1524 priv->dev->features |= priv->dev->hw_features;
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001525 }
1526
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001527 return 0;
1528}
1529
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530static struct net_device *ipoib_add_port(const char *format,
1531 struct ib_device *hca, u8 port)
1532{
1533 struct ipoib_dev_priv *priv;
Shirley Mabc7b3a32008-04-23 11:55:45 -07001534 struct ib_port_attr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 int result = -ENOMEM;
1536
1537 priv = ipoib_intf_alloc(format);
1538 if (!priv)
1539 goto alloc_mem_failed;
1540
1541 SET_NETDEV_DEV(priv->dev, hca->dma_device);
Eli Cohenc3aa9b12010-09-20 07:05:06 +00001542 priv->dev->dev_id = port - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001543
Shirley Mabc7b3a32008-04-23 11:55:45 -07001544 if (!ib_query_port(hca, port, &attr))
1545 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1546 else {
1547 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
1548 hca->name, port);
1549 goto device_init_failed;
1550 }
1551
1552 /* MTU will be reset when mcast join happens */
1553 priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
1554 priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
1555
David Miller596b9b62011-07-25 00:01:25 +00001556 priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
1557
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 result = ib_query_pkey(hca, port, 0, &priv->pkey);
1559 if (result) {
1560 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1561 hca->name, port, result);
Eli Cohenca6de172007-08-21 18:46:10 +03001562 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 }
1564
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001565 if (ipoib_set_dev_features(priv, hca))
Eli Cohen60461362008-04-16 21:01:10 -07001566 goto device_init_failed;
Vladimir Sokolovskyaf40da82008-07-14 23:48:48 -07001567
Roland Dreier4ce05932005-08-19 12:03:17 -07001568 /*
1569 * Set the full membership bit, so that we join the right
1570 * broadcast group, etc.
1571 */
1572 priv->pkey |= 0x8000;
1573
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 priv->dev->broadcast[8] = priv->pkey >> 8;
1575 priv->dev->broadcast[9] = priv->pkey & 0xff;
1576
1577 result = ib_query_gid(hca, port, 0, &priv->local_gid);
1578 if (result) {
1579 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1580 hca->name, port, result);
Eli Cohenca6de172007-08-21 18:46:10 +03001581 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001582 } else
1583 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1584
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 result = ipoib_dev_init(priv->dev, hca, port);
1586 if (result < 0) {
1587 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1588 hca->name, port, result);
1589 goto device_init_failed;
1590 }
1591
1592 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1593 priv->ca, ipoib_event);
1594 result = ib_register_event_handler(&priv->event_handler);
1595 if (result < 0) {
1596 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1597 "port %d (ret = %d)\n",
1598 hca->name, port, result);
1599 goto event_failed;
1600 }
1601
1602 result = register_netdev(priv->dev);
1603 if (result) {
1604 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1605 hca->name, port, result);
1606 goto register_failed;
1607 }
1608
Roland Dreier1732b0e2005-11-07 10:33:11 -08001609 ipoib_create_debug_files(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001610
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001611 if (ipoib_cm_add_mode_attr(priv->dev))
1612 goto sysfs_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001613 if (ipoib_add_pkey_attr(priv->dev))
1614 goto sysfs_failed;
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001615 if (ipoib_add_umcast_attr(priv->dev))
1616 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001617 if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001619 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620 goto sysfs_failed;
1621
1622 return priv->dev;
1623
1624sysfs_failed:
Roland Dreier1732b0e2005-11-07 10:33:11 -08001625 ipoib_delete_debug_files(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 unregister_netdev(priv->dev);
1627
1628register_failed:
1629 ib_unregister_event_handler(&priv->event_handler);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001630 /* Stop GC if started before flush */
1631 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1632 cancel_delayed_work(&priv->neigh_reap_task);
Roland Dreiera77a57a2008-08-19 15:01:32 -07001633 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001634
1635event_failed:
1636 ipoib_dev_cleanup(priv->dev);
1637
1638device_init_failed:
1639 free_netdev(priv->dev);
1640
1641alloc_mem_failed:
1642 return ERR_PTR(result);
1643}
1644
1645static void ipoib_add_one(struct ib_device *device)
1646{
1647 struct list_head *dev_list;
1648 struct net_device *dev;
1649 struct ipoib_dev_priv *priv;
1650 int s, e, p;
1651
Tom Tucker07ebafb2006-08-03 16:02:42 -05001652 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1653 return;
1654
Linus Torvalds1da177e2005-04-16 15:20:36 -07001655 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1656 if (!dev_list)
1657 return;
1658
1659 INIT_LIST_HEAD(dev_list);
1660
Tom Tucker07ebafb2006-08-03 16:02:42 -05001661 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 s = 0;
1663 e = 0;
1664 } else {
1665 s = 1;
1666 e = device->phys_port_cnt;
1667 }
1668
1669 for (p = s; p <= e; ++p) {
Eli Cohen7b4c8762010-09-27 17:51:11 -07001670 if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
1671 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 dev = ipoib_add_port("ib%d", device, p);
1673 if (!IS_ERR(dev)) {
1674 priv = netdev_priv(dev);
1675 list_add_tail(&priv->list, dev_list);
1676 }
1677 }
1678
1679 ib_set_client_data(device, &ipoib_client, dev_list);
1680}
1681
1682static void ipoib_remove_one(struct ib_device *device)
1683{
1684 struct ipoib_dev_priv *priv, *tmp;
1685 struct list_head *dev_list;
1686
Tom Tucker07ebafb2006-08-03 16:02:42 -05001687 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1688 return;
1689
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 dev_list = ib_get_client_data(device, &ipoib_client);
1691
1692 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1693 ib_unregister_event_handler(&priv->event_handler);
Roland Dreiera77a57a2008-08-19 15:01:32 -07001694
1695 rtnl_lock();
1696 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
1697 rtnl_unlock();
1698
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001699 /* Stop GC */
1700 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1701 cancel_delayed_work(&priv->neigh_reap_task);
Roland Dreiera77a57a2008-08-19 15:01:32 -07001702 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 unregister_netdev(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 free_netdev(priv->dev);
1706 }
Michael S. Tsirkin06c56e42005-09-01 09:19:02 -07001707
1708 kfree(dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001709}
1710
1711static int __init ipoib_init_module(void)
1712{
1713 int ret;
1714
Shirley Ma0f485252006-04-10 09:43:58 -07001715 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1716 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1717 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1718
1719 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1720 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -07001721 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001722#ifdef CONFIG_INFINIBAND_IPOIB_CM
1723 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1724#endif
Shirley Ma0f485252006-04-10 09:43:58 -07001725
Eli Cohenf89271da2008-07-14 23:48:44 -07001726 /*
1727 * When copying small received packets, we only copy from the
1728 * linear data part of the SKB, so we rely on this condition.
1729 */
1730 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
1731
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 ret = ipoib_register_debugfs();
1733 if (ret)
1734 return ret;
1735
1736 /*
1737 * We create our own workqueue mainly because we want to be
1738 * able to flush it when devices are being removed. We can't
1739 * use schedule_work()/flush_scheduled_work() because both
1740 * unregister_netdev() and linkwatch_event take the rtnl lock,
1741 * so flush_scheduled_work() can deadlock during device
1742 * removal.
1743 */
1744 ipoib_workqueue = create_singlethread_workqueue("ipoib");
1745 if (!ipoib_workqueue) {
1746 ret = -ENOMEM;
1747 goto err_fs;
1748 }
1749
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001750 ib_sa_register_client(&ipoib_sa_client);
1751
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 ret = ib_register_client(&ipoib_client);
1753 if (ret)
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001754 goto err_sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001756 ret = ipoib_netlink_init();
1757 if (ret)
1758 goto err_client;
1759
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 return 0;
1761
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001762err_client:
1763 ib_unregister_client(&ipoib_client);
1764
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001765err_sa:
1766 ib_sa_unregister_client(&ipoib_sa_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 destroy_workqueue(ipoib_workqueue);
1768
Roland Dreier9adec1a2005-04-16 15:26:07 -07001769err_fs:
1770 ipoib_unregister_debugfs();
1771
Linus Torvalds1da177e2005-04-16 15:20:36 -07001772 return ret;
1773}
1774
1775static void __exit ipoib_cleanup_module(void)
1776{
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001777 ipoib_netlink_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 ib_unregister_client(&ipoib_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001779 ib_sa_unregister_client(&ipoib_sa_client);
Roland Dreier9adec1a2005-04-16 15:26:07 -07001780 ipoib_unregister_debugfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001781 destroy_workqueue(ipoib_workqueue);
1782}
1783
1784module_init(ipoib_init_module);
1785module_exit(ipoib_cleanup_module);