blob: 66d6da90982f2764e08dc3d0a6895a63049aa92d [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
Yossi Etigine028cc52009-04-20 13:58:08 -0700107 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
109 if (ipoib_pkey_dev_delay_open(dev))
110 return 0;
111
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800112 if (ipoib_ib_dev_open(dev))
113 goto err_disable;
Yossi Etiginfe25c562008-11-12 10:24:36 -0800114
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800115 if (ipoib_ib_dev_up(dev))
116 goto err_stop;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
119 struct ipoib_dev_priv *cpriv;
120
121 /* Bring up any child interfaces too */
Ingo Molnar95ed6442006-01-13 14:51:39 -0800122 mutex_lock(&priv->vlan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 list_for_each_entry(cpriv, &priv->child_intfs, list) {
124 int flags;
125
126 flags = cpriv->dev->flags;
127 if (flags & IFF_UP)
128 continue;
129
130 dev_change_flags(cpriv->dev, flags | IFF_UP);
131 }
Ingo Molnar95ed6442006-01-13 14:51:39 -0800132 mutex_unlock(&priv->vlan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 }
134
135 netif_start_queue(dev);
136
137 return 0;
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800138
139err_stop:
140 ipoib_ib_dev_stop(dev, 1);
141
142err_disable:
Roland Dreierb8a1b1c2009-01-14 14:55:41 -0800143 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
144
145 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146}
147
148static int ipoib_stop(struct net_device *dev)
149{
150 struct ipoib_dev_priv *priv = netdev_priv(dev);
151
152 ipoib_dbg(priv, "stopping interface\n");
153
154 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
155
156 netif_stop_queue(dev);
157
Patrick McHardybea1e222012-08-30 07:01:30 +0000158 ipoib_ib_dev_down(dev, 1);
Roland Dreiera77a57a2008-08-19 15:01:32 -0700159 ipoib_ib_dev_stop(dev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160
161 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
162 struct ipoib_dev_priv *cpriv;
163
164 /* Bring down any child interfaces too */
Ingo Molnar95ed6442006-01-13 14:51:39 -0800165 mutex_lock(&priv->vlan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 list_for_each_entry(cpriv, &priv->child_intfs, list) {
167 int flags;
168
169 flags = cpriv->dev->flags;
170 if (!(flags & IFF_UP))
171 continue;
172
173 dev_change_flags(cpriv->dev, flags & ~IFF_UP);
174 }
Ingo Molnar95ed6442006-01-13 14:51:39 -0800175 mutex_unlock(&priv->vlan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 }
177
178 return 0;
179}
180
Or Gerlitz9baa0b02012-09-13 05:56:36 +0000181static void ipoib_uninit(struct net_device *dev)
182{
183 ipoib_dev_cleanup(dev);
184}
185
David S. Miller9ca36f72011-11-16 18:05:50 -0500186static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
Michał Mirosław3d96c742011-04-19 00:43:20 +0000187{
188 struct ipoib_dev_priv *priv = netdev_priv(dev);
189
190 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
191 features &= ~(NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
192
193 return features;
194}
195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
197{
198 struct ipoib_dev_priv *priv = netdev_priv(dev);
199
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200200 /* dev->mtu > 2K ==> connected mode */
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800201 if (ipoib_cm_admin_enabled(dev)) {
202 if (new_mtu > ipoib_cm_max_mtu(dev))
203 return -EINVAL;
204
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200205 if (new_mtu > priv->mcast_mtu)
206 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
207 priv->mcast_mtu);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800208
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200209 dev->mtu = new_mtu;
210 return 0;
211 }
212
Shirley Mabc7b3a32008-04-23 11:55:45 -0700213 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 return -EINVAL;
215
216 priv->admin_mtu = new_mtu;
217
218 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
219
220 return 0;
221}
222
Roland Dreier71d9c5f2012-10-02 21:23:43 -0700223int ipoib_set_mode(struct net_device *dev, const char *buf)
224{
225 struct ipoib_dev_priv *priv = netdev_priv(dev);
226
227 /* flush paths if we switch modes so that connections are restarted */
228 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
229 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
230 ipoib_warn(priv, "enabling connected mode "
231 "will cause multicast packet drops\n");
232 netdev_update_features(dev);
233 rtnl_unlock();
234 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
235
236 ipoib_flush_paths(dev);
237 rtnl_lock();
238 return 0;
239 }
240
241 if (!strcmp(buf, "datagram\n")) {
242 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
243 netdev_update_features(dev);
244 dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
245 rtnl_unlock();
246 ipoib_flush_paths(dev);
247 rtnl_lock();
248 return 0;
249 }
250
251 return -EINVAL;
252}
253
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300254static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255{
256 struct ipoib_dev_priv *priv = netdev_priv(dev);
257 struct rb_node *n = priv->path_tree.rb_node;
258 struct ipoib_path *path;
259 int ret;
260
261 while (n) {
262 path = rb_entry(n, struct ipoib_path, rb_node);
263
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300264 ret = memcmp(gid, path->pathrec.dgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 sizeof (union ib_gid));
266
267 if (ret < 0)
268 n = n->rb_left;
269 else if (ret > 0)
270 n = n->rb_right;
271 else
272 return path;
273 }
274
275 return NULL;
276}
277
278static int __path_add(struct net_device *dev, struct ipoib_path *path)
279{
280 struct ipoib_dev_priv *priv = netdev_priv(dev);
281 struct rb_node **n = &priv->path_tree.rb_node;
282 struct rb_node *pn = NULL;
283 struct ipoib_path *tpath;
284 int ret;
285
286 while (*n) {
287 pn = *n;
288 tpath = rb_entry(pn, struct ipoib_path, rb_node);
289
290 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
291 sizeof (union ib_gid));
292 if (ret < 0)
293 n = &pn->rb_left;
294 else if (ret > 0)
295 n = &pn->rb_right;
296 else
297 return -EEXIST;
298 }
299
300 rb_link_node(&path->rb_node, pn, n);
301 rb_insert_color(&path->rb_node, &priv->path_tree);
302
303 list_add_tail(&path->list, &priv->path_list);
304
305 return 0;
306}
307
308static void path_free(struct net_device *dev, struct ipoib_path *path)
309{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
312 while ((skb = __skb_dequeue(&path->queue)))
313 dev_kfree_skb_irq(skb);
314
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000315 ipoib_dbg(netdev_priv(dev), "path_free\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000317 /* remove all neigh connected to this path */
318 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
320 if (path->ah)
321 ipoib_put_ah(path->ah);
322
323 kfree(path);
324}
325
Roland Dreier1732b0e2005-11-07 10:33:11 -0800326#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
327
328struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
329{
330 struct ipoib_path_iter *iter;
331
332 iter = kmalloc(sizeof *iter, GFP_KERNEL);
333 if (!iter)
334 return NULL;
335
336 iter->dev = dev;
337 memset(iter->path.pathrec.dgid.raw, 0, 16);
338
339 if (ipoib_path_iter_next(iter)) {
340 kfree(iter);
341 return NULL;
342 }
343
344 return iter;
345}
346
347int ipoib_path_iter_next(struct ipoib_path_iter *iter)
348{
349 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
350 struct rb_node *n;
351 struct ipoib_path *path;
352 int ret = 1;
353
354 spin_lock_irq(&priv->lock);
355
356 n = rb_first(&priv->path_tree);
357
358 while (n) {
359 path = rb_entry(n, struct ipoib_path, rb_node);
360
361 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
362 sizeof (union ib_gid)) < 0) {
363 iter->path = *path;
364 ret = 0;
365 break;
366 }
367
368 n = rb_next(n);
369 }
370
371 spin_unlock_irq(&priv->lock);
372
373 return ret;
374}
375
376void ipoib_path_iter_read(struct ipoib_path_iter *iter,
377 struct ipoib_path *path)
378{
379 *path = iter->path;
380}
381
382#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
383
Moni Shouaee1e2c82008-07-14 23:48:49 -0700384void ipoib_mark_paths_invalid(struct net_device *dev)
385{
386 struct ipoib_dev_priv *priv = netdev_priv(dev);
387 struct ipoib_path *path, *tp;
388
389 spin_lock_irq(&priv->lock);
390
391 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700392 ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
Moni Shouaee1e2c82008-07-14 23:48:49 -0700393 be16_to_cpu(path->pathrec.dlid),
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700394 path->pathrec.dgid.raw);
Moni Shouaee1e2c82008-07-14 23:48:49 -0700395 path->valid = 0;
396 }
397
398 spin_unlock_irq(&priv->lock);
399}
400
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401void ipoib_flush_paths(struct net_device *dev)
402{
403 struct ipoib_dev_priv *priv = netdev_priv(dev);
404 struct ipoib_path *path, *tp;
405 LIST_HEAD(remove_list);
Roland Dreier943c2462008-09-30 10:36:21 -0700406 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407
Roland Dreier943c2462008-09-30 10:36:21 -0700408 netif_tx_lock_bh(dev);
409 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
Robert P. J. Day157de222008-04-16 21:09:26 -0700411 list_splice_init(&priv->path_list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412
413 list_for_each_entry(path, &remove_list, list)
414 rb_erase(&path->rb_node, &priv->path_tree);
415
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 list_for_each_entry_safe(path, tp, &remove_list, list) {
417 if (path->query)
418 ib_sa_cancel_query(path->query_id, path->query);
Roland Dreier943c2462008-09-30 10:36:21 -0700419 spin_unlock_irqrestore(&priv->lock, flags);
420 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 wait_for_completion(&path->done);
422 path_free(dev, path);
Roland Dreier943c2462008-09-30 10:36:21 -0700423 netif_tx_lock_bh(dev);
424 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 }
Roland Dreier943c2462008-09-30 10:36:21 -0700426
427 spin_unlock_irqrestore(&priv->lock, flags);
428 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429}
430
431static void path_rec_completion(int status,
432 struct ib_sa_path_rec *pathrec,
433 void *path_ptr)
434{
435 struct ipoib_path *path = path_ptr;
436 struct net_device *dev = path->dev;
437 struct ipoib_dev_priv *priv = netdev_priv(dev);
438 struct ipoib_ah *ah = NULL;
Roland Dreierc9da4ba2008-09-25 15:26:15 -0700439 struct ipoib_ah *old_ah = NULL;
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700440 struct ipoib_neigh *neigh, *tn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 struct sk_buff_head skqueue;
442 struct sk_buff *skb;
443 unsigned long flags;
444
Roland Dreier843613b2007-02-26 12:57:08 -0800445 if (!status)
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700446 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700447 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 else
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700449 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700450 status, path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451
452 skb_queue_head_init(&skqueue);
453
454 if (!status) {
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700455 struct ib_ah_attr av;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700457 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
458 ah = ipoib_create_ah(dev, priv->pd, &av);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 }
460
461 spin_lock_irqsave(&priv->lock, flags);
462
Mike Marciniszyn38743972011-11-21 08:43:54 -0500463 if (!IS_ERR_OR_NULL(ah)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 path->pathrec = *pathrec;
465
Roland Dreierc9da4ba2008-09-25 15:26:15 -0700466 old_ah = path->ah;
467 path->ah = ah;
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
470 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
471
472 while ((skb = __skb_dequeue(&path->queue)))
473 __skb_queue_tail(&skqueue, skb);
474
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700475 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
Moni Shouaee1e2c82008-07-14 23:48:49 -0700476 if (neigh->ah) {
477 WARN_ON(neigh->ah != old_ah);
478 /*
479 * Dropping the ah reference inside
480 * priv->lock is safe here, because we
481 * will hold one more reference from
482 * the original value of path->ah (ie
483 * old_ah).
484 */
485 ipoib_put_ah(neigh->ah);
486 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 kref_get(&path->ah->ref);
488 neigh->ah = path->ah;
489
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000490 if (ipoib_cm_enabled(dev, neigh->daddr)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200491 if (!ipoib_cm_get(neigh))
492 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
493 path,
494 neigh));
495 if (!ipoib_cm_get(neigh)) {
496 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000497 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200498 continue;
499 }
500 }
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502 while ((skb = __skb_dequeue(&neigh->queue)))
503 __skb_queue_tail(&skqueue, skb);
504 }
Moni Shouaee1e2c82008-07-14 23:48:49 -0700505 path->valid = 1;
Roland Dreier5872a9f2005-11-29 10:13:54 -0800506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Roland Dreier5872a9f2005-11-29 10:13:54 -0800508 path->query = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 complete(&path->done);
510
511 spin_unlock_irqrestore(&priv->lock, flags);
512
Moni Shouaee1e2c82008-07-14 23:48:49 -0700513 if (old_ah)
514 ipoib_put_ah(old_ah);
515
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 while ((skb = __skb_dequeue(&skqueue))) {
517 skb->dev = dev;
518 if (dev_queue_xmit(skb))
519 ipoib_warn(priv, "dev_queue_xmit failed "
520 "to requeue packet\n");
521 }
522}
523
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300524static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525{
526 struct ipoib_dev_priv *priv = netdev_priv(dev);
527 struct ipoib_path *path;
528
Jack Morgenstein1401b532007-11-26 10:41:19 +0200529 if (!priv->broadcast)
530 return NULL;
531
Roland Dreier21a38482005-11-02 10:07:59 -0800532 path = kzalloc(sizeof *path, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 if (!path)
534 return NULL;
535
Roland Dreier21a38482005-11-02 10:07:59 -0800536 path->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
538 skb_queue_head_init(&path->queue);
539
540 INIT_LIST_HEAD(&path->neigh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300542 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
Roland Dreier2337f802007-10-23 19:57:54 -0700543 path->pathrec.sgid = priv->local_gid;
544 path->pathrec.pkey = cpu_to_be16(priv->pkey);
Sean Hefty81668832007-08-02 12:21:31 -0700545 path->pathrec.numb_path = 1;
546 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
548 return path;
549}
550
551static int path_rec_start(struct net_device *dev,
552 struct ipoib_path *path)
553{
554 struct ipoib_dev_priv *priv = netdev_priv(dev);
555
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700556 ipoib_dbg(priv, "Start path record lookup for %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700557 path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558
Roland Dreier65c7edd2005-11-28 21:20:34 -0800559 init_completion(&path->done);
560
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 path->query_id =
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700562 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563 &path->pathrec,
564 IB_SA_PATH_REC_DGID |
565 IB_SA_PATH_REC_SGID |
566 IB_SA_PATH_REC_NUMB_PATH |
Sean Hefty81668832007-08-02 12:21:31 -0700567 IB_SA_PATH_REC_TRAFFIC_CLASS |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 IB_SA_PATH_REC_PKEY,
569 1000, GFP_ATOMIC,
570 path_rec_completion,
571 path, &path->query);
572 if (path->query_id < 0) {
Or Gerlitz01b3fc82008-07-22 14:18:34 -0700573 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 path->query = NULL;
Yossi Etigin93a3ab92008-11-12 10:24:38 -0800575 complete(&path->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 return path->query_id;
577 }
578
579 return 0;
580}
581
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000582static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
583 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584{
585 struct ipoib_dev_priv *priv = netdev_priv(dev);
586 struct ipoib_path *path;
587 struct ipoib_neigh *neigh;
Roland Dreier943c2462008-09-30 10:36:21 -0700588 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000590 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000591 neigh = ipoib_neigh_alloc(daddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (!neigh) {
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000593 spin_unlock_irqrestore(&priv->lock, flags);
Roland Dreierde903512007-09-28 15:33:51 -0700594 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 dev_kfree_skb_any(skb);
596 return;
597 }
598
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000599 path = __path_find(dev, daddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (!path) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000601 path = path_rec_create(dev, daddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 if (!path)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300603 goto err_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700604
605 __path_add(dev, path);
606 }
607
608 list_add_tail(&neigh->list, &path->neigh_list);
609
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -0800610 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 kref_get(&path->ah->ref);
612 neigh->ah = path->ah;
613
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000614 if (ipoib_cm_enabled(dev, neigh->daddr)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200615 if (!ipoib_cm_get(neigh))
616 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
617 if (!ipoib_cm_get(neigh)) {
618 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000619 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200620 goto err_drop;
621 }
622 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
623 __skb_queue_tail(&neigh->queue, skb);
624 else {
625 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
626 skb_queue_len(&neigh->queue));
627 goto err_drop;
628 }
Roland Dreier721d67c2009-09-05 20:23:40 -0700629 } else {
630 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000631 ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
632 ipoib_neigh_put(neigh);
Roland Dreier721d67c2009-09-05 20:23:40 -0700633 return;
634 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635 } else {
636 neigh->ah = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
638 if (!path->query && path_rec_start(dev, path))
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300639 goto err_list;
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +0200640
641 __skb_queue_tail(&neigh->queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642 }
643
Roland Dreier943c2462008-09-30 10:36:21 -0700644 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000645 ipoib_neigh_put(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646 return;
647
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300648err_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 list_del(&neigh->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300651err_path:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000652 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200653err_drop:
Roland Dreierde903512007-09-28 15:33:51 -0700654 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 dev_kfree_skb_any(skb);
656
Roland Dreier943c2462008-09-30 10:36:21 -0700657 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000658 ipoib_neigh_put(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659}
660
661static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
Roland Dreier936d7de2012-02-07 14:51:21 +0000662 struct ipoib_cb *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663{
664 struct ipoib_dev_priv *priv = netdev_priv(dev);
665 struct ipoib_path *path;
Roland Dreier943c2462008-09-30 10:36:21 -0700666 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
Roland Dreier943c2462008-09-30 10:36:21 -0700668 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
Roland Dreier936d7de2012-02-07 14:51:21 +0000670 path = __path_find(dev, cb->hwaddr + 4);
Moni Shouaee1e2c82008-07-14 23:48:49 -0700671 if (!path || !path->valid) {
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800672 int new_path = 0;
673
674 if (!path) {
Roland Dreier936d7de2012-02-07 14:51:21 +0000675 path = path_rec_create(dev, cb->hwaddr + 4);
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800676 new_path = 1;
677 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678 if (path) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 __skb_queue_tail(&path->queue, skb);
680
Yossi Etiginff79ae82008-11-12 10:24:39 -0800681 if (!path->query && path_rec_start(dev, path)) {
Roland Dreier943c2462008-09-30 10:36:21 -0700682 spin_unlock_irqrestore(&priv->lock, flags);
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800683 if (new_path)
684 path_free(dev, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 return;
686 } else
687 __path_add(dev, path);
688 } else {
Roland Dreierde903512007-09-28 15:33:51 -0700689 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690 dev_kfree_skb_any(skb);
691 }
692
Roland Dreier943c2462008-09-30 10:36:21 -0700693 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 return;
695 }
696
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -0800697 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
699 be16_to_cpu(path->pathrec.dlid));
700
Roland Dreier721d67c2009-09-05 20:23:40 -0700701 spin_unlock_irqrestore(&priv->lock, flags);
Roland Dreier936d7de2012-02-07 14:51:21 +0000702 ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr));
Roland Dreier721d67c2009-09-05 20:23:40 -0700703 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704 } else if ((path->query || !path_rec_start(dev, path)) &&
705 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706 __skb_queue_tail(&path->queue, skb);
707 } else {
Roland Dreierde903512007-09-28 15:33:51 -0700708 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 dev_kfree_skb_any(skb);
710 }
711
Roland Dreier943c2462008-09-30 10:36:21 -0700712 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713}
714
715static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
716{
717 struct ipoib_dev_priv *priv = netdev_priv(dev);
718 struct ipoib_neigh *neigh;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000719 struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
720 struct ipoib_header *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 unsigned long flags;
722
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000723 header = (struct ipoib_header *) skb->data;
724
725 if (unlikely(cb->hwaddr[4] == 0xff)) {
726 /* multicast, arrange "if" according to probability */
727 if ((header->proto != htons(ETH_P_IP)) &&
728 (header->proto != htons(ETH_P_IPV6)) &&
729 (header->proto != htons(ETH_P_ARP)) &&
730 (header->proto != htons(ETH_P_RARP))) {
731 /* ethertype not supported by IPoIB */
David Miller17e6abe2011-12-02 16:52:44 +0000732 ++dev->stats.tx_dropped;
733 dev_kfree_skb_any(skb);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000734 return NETDEV_TX_OK;
David Miller17e6abe2011-12-02 16:52:44 +0000735 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000736 /* Add in the P_Key for multicast*/
737 cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
738 cb->hwaddr[9] = priv->pkey & 0xff;
739
740 neigh = ipoib_neigh_get(dev, cb->hwaddr);
741 if (likely(neigh))
742 goto send_using_neigh;
743 ipoib_mcast_send(dev, cb->hwaddr, skb);
744 return NETDEV_TX_OK;
David Miller17e6abe2011-12-02 16:52:44 +0000745 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000747 /* unicast, arrange "switch" according to probability */
748 switch (header->proto) {
749 case htons(ETH_P_IP):
750 case htons(ETH_P_IPV6):
751 neigh = ipoib_neigh_get(dev, cb->hwaddr);
752 if (unlikely(!neigh)) {
753 neigh_add_path(skb, cb->hwaddr, dev);
754 return NETDEV_TX_OK;
Yossi Etigina50df392009-01-09 14:05:11 -0800755 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000756 break;
757 case htons(ETH_P_ARP):
758 case htons(ETH_P_RARP):
759 /* for unicast ARP and RARP should always perform path find */
760 unicast_arp_send(skb, dev, cb);
761 return NETDEV_TX_OK;
762 default:
763 /* ethertype not supported by IPoIB */
764 ++dev->stats.tx_dropped;
765 dev_kfree_skb_any(skb);
766 return NETDEV_TX_OK;
767 }
Michael S. Tsirkin8a7f7522006-07-19 17:44:37 +0300768
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000769send_using_neigh:
770 /* note we now hold a ref to neigh */
771 if (ipoib_cm_get(neigh)) {
772 if (ipoib_cm_up(neigh)) {
773 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
774 goto unref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000776 } else if (neigh->ah) {
777 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
778 goto unref;
779 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000781 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
782 spin_lock_irqsave(&priv->lock, flags);
783 __skb_queue_tail(&neigh->queue, skb);
784 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 } else {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000786 ++dev->stats.tx_dropped;
787 dev_kfree_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000789
790unref:
791 ipoib_neigh_put(neigh);
792
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 return NETDEV_TX_OK;
794}
795
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796static void ipoib_timeout(struct net_device *dev)
797{
798 struct ipoib_dev_priv *priv = netdev_priv(dev);
799
Roland Dreier4b2d3192005-10-18 12:20:06 -0700800 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
801 jiffies_to_msecs(jiffies - dev->trans_start));
802 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
803 netif_queue_stopped(dev),
804 priv->tx_head, priv->tx_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 /* XXX reset QP, etc. */
806}
807
808static int ipoib_hard_header(struct sk_buff *skb,
809 struct net_device *dev,
810 unsigned short type,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700811 const void *daddr, const void *saddr, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812{
813 struct ipoib_header *header;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000814 struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815
816 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
817
818 header->proto = htons(type);
819 header->reserved = 0;
820
821 /*
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000822 * we don't rely on dst_entry structure, always stuff the
Roland Dreier936d7de2012-02-07 14:51:21 +0000823 * destination address into skb->cb so we can figure out where
824 * to send the packet later.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000826 memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
828 return 0;
829}
830
831static void ipoib_set_mcast_list(struct net_device *dev)
832{
833 struct ipoib_dev_priv *priv = netdev_priv(dev);
834
Leonid Arsh7a343d42006-03-23 19:52:51 +0200835 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
836 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
837 return;
838 }
839
Michael S. Tsirkin1ad62a12005-08-24 14:41:51 -0700840 queue_work(ipoib_workqueue, &priv->restart_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841}
842
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000843static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000845 /*
846 * Use only the address parts that contributes to spreading
847 * The subnet prefix is not used as one can not connect to
848 * same remote port (GUID) using the same remote QPN via two
849 * different subnets.
850 */
851 /* qpn octets[1:4) & port GUID octets[12:20) */
Shlomo Pongratz9d1ad662013-02-19 15:40:22 +0000852 u32 *d32 = (u32 *) daddr;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000853 u32 hv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854
Shlomo Pongratz9d1ad662013-02-19 15:40:22 +0000855 hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000856 return hv & htbl->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857}
858
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000859struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
860{
861 struct ipoib_dev_priv *priv = netdev_priv(dev);
862 struct ipoib_neigh_table *ntbl = &priv->ntbl;
863 struct ipoib_neigh_hash *htbl;
864 struct ipoib_neigh *neigh = NULL;
865 u32 hash_val;
866
867 rcu_read_lock_bh();
868
869 htbl = rcu_dereference_bh(ntbl->htbl);
870
871 if (!htbl)
872 goto out_unlock;
873
874 hash_val = ipoib_addr_hash(htbl, daddr);
875 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
876 neigh != NULL;
877 neigh = rcu_dereference_bh(neigh->hnext)) {
878 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
879 /* found, take one ref on behalf of the caller */
880 if (!atomic_inc_not_zero(&neigh->refcnt)) {
881 /* deleted */
882 neigh = NULL;
883 goto out_unlock;
884 }
885 neigh->alive = jiffies;
886 goto out_unlock;
887 }
888 }
889
890out_unlock:
891 rcu_read_unlock_bh();
892 return neigh;
893}
894
895static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
896{
897 struct ipoib_neigh_table *ntbl = &priv->ntbl;
898 struct ipoib_neigh_hash *htbl;
899 unsigned long neigh_obsolete;
900 unsigned long dt;
901 unsigned long flags;
902 int i;
903
904 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
905 return;
906
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000907 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000908
909 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000910 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000911
912 if (!htbl)
913 goto out_unlock;
914
915 /* neigh is obsolete if it was idle for two GC periods */
916 dt = 2 * arp_tbl.gc_interval;
917 neigh_obsolete = jiffies - dt;
918 /* handle possible race condition */
919 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
920 goto out_unlock;
921
922 for (i = 0; i < htbl->size; i++) {
923 struct ipoib_neigh *neigh;
924 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
925
926 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000927 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000928 /* was the neigh idle for two GC periods */
929 if (time_after(neigh_obsolete, neigh->alive)) {
930 rcu_assign_pointer(*np,
931 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000932 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000933 /* remove from path/mc list */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000934 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000935 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
936 } else {
937 np = &neigh->hnext;
938 }
939
940 }
941 }
942
943out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000944 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000945}
946
947static void ipoib_reap_neigh(struct work_struct *work)
948{
949 struct ipoib_dev_priv *priv =
950 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
951
952 __ipoib_reap_neigh(priv);
953
954 if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
955 queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
956 arp_tbl.gc_interval);
957}
958
959
960static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
Moni Shoua732a2172007-10-09 19:43:36 -0700961 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300962{
963 struct ipoib_neigh *neigh;
964
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000965 neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300966 if (!neigh)
967 return NULL;
968
Moni Shoua732a2172007-10-09 19:43:36 -0700969 neigh->dev = dev;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000970 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
Roland Dreier82b39912006-12-12 14:48:18 -0800971 skb_queue_head_init(&neigh->queue);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000972 INIT_LIST_HEAD(&neigh->list);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200973 ipoib_cm_set(neigh, NULL);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000974 /* one ref on behalf of the caller */
975 atomic_set(&neigh->refcnt, 1);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300976
977 return neigh;
978}
979
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000980struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
981 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300982{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000983 struct ipoib_dev_priv *priv = netdev_priv(dev);
984 struct ipoib_neigh_table *ntbl = &priv->ntbl;
985 struct ipoib_neigh_hash *htbl;
986 struct ipoib_neigh *neigh;
987 u32 hash_val;
988
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000989 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +0000990 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000991 if (!htbl) {
992 neigh = NULL;
993 goto out_unlock;
994 }
995
996 /* need to add a new neigh, but maybe some other thread succeeded?
997 * recalc hash, maybe hash resize took place so we do a search
998 */
999 hash_val = ipoib_addr_hash(htbl, daddr);
1000 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001001 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001002 neigh != NULL;
1003 neigh = rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001004 lockdep_is_held(&priv->lock))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001005 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1006 /* found, take one ref on behalf of the caller */
1007 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1008 /* deleted */
1009 neigh = NULL;
1010 break;
1011 }
1012 neigh->alive = jiffies;
1013 goto out_unlock;
1014 }
1015 }
1016
1017 neigh = ipoib_neigh_ctor(daddr, dev);
1018 if (!neigh)
1019 goto out_unlock;
1020
1021 /* one ref on behalf of the hash table */
1022 atomic_inc(&neigh->refcnt);
1023 neigh->alive = jiffies;
1024 /* put in hash */
1025 rcu_assign_pointer(neigh->hnext,
1026 rcu_dereference_protected(htbl->buckets[hash_val],
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001027 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001028 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1029 atomic_inc(&ntbl->entries);
1030
1031out_unlock:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001032
1033 return neigh;
1034}
1035
1036void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1037{
1038 /* neigh reference count was dropprd to zero */
1039 struct net_device *dev = neigh->dev;
1040 struct ipoib_dev_priv *priv = netdev_priv(dev);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001041 struct sk_buff *skb;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001042 if (neigh->ah)
1043 ipoib_put_ah(neigh->ah);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001044 while ((skb = __skb_dequeue(&neigh->queue))) {
Roland Dreierde903512007-09-28 15:33:51 -07001045 ++dev->stats.tx_dropped;
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001046 dev_kfree_skb_any(skb);
1047 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001048 if (ipoib_cm_get(neigh))
1049 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001050 ipoib_dbg(netdev_priv(dev),
1051 "neigh free for %06x %pI6\n",
1052 IPOIB_QPN(neigh->daddr),
1053 neigh->daddr + 4);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001054 kfree(neigh);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001055 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1056 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1057 complete(&priv->ntbl.flushed);
1058 }
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001059}
1060
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001061static void ipoib_neigh_reclaim(struct rcu_head *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001063 /* Called as a result of removal from hash table */
1064 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1065 /* note TX context may hold another ref */
1066 ipoib_neigh_put(neigh);
1067}
1068
1069void ipoib_neigh_free(struct ipoib_neigh *neigh)
1070{
1071 struct net_device *dev = neigh->dev;
1072 struct ipoib_dev_priv *priv = netdev_priv(dev);
1073 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1074 struct ipoib_neigh_hash *htbl;
1075 struct ipoib_neigh __rcu **np;
1076 struct ipoib_neigh *n;
1077 u32 hash_val;
1078
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001079 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001080 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001081 if (!htbl)
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001082 return;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001083
1084 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1085 np = &htbl->buckets[hash_val];
1086 for (n = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001087 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001088 n != NULL;
Shlomo Pongratz6c723a62012-08-13 14:39:50 +00001089 n = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001090 lockdep_is_held(&priv->lock))) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001091 if (n == neigh) {
1092 /* found */
1093 rcu_assign_pointer(*np,
1094 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001095 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001096 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001097 return;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001098 } else {
1099 np = &n->hnext;
1100 }
1101 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001102}
1103
1104static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1105{
1106 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1107 struct ipoib_neigh_hash *htbl;
1108 struct ipoib_neigh **buckets;
1109 u32 size;
1110
1111 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1112 ntbl->htbl = NULL;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001113 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1114 if (!htbl)
1115 return -ENOMEM;
1116 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1117 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1118 buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1119 if (!buckets) {
1120 kfree(htbl);
1121 return -ENOMEM;
1122 }
1123 htbl->size = size;
1124 htbl->mask = (size - 1);
1125 htbl->buckets = buckets;
1126 ntbl->htbl = htbl;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001127 htbl->ntbl = ntbl;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001128 atomic_set(&ntbl->entries, 0);
1129
1130 /* start garbage collection */
1131 clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1132 queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
1133 arp_tbl.gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001134
1135 return 0;
1136}
1137
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001138static void neigh_hash_free_rcu(struct rcu_head *head)
1139{
1140 struct ipoib_neigh_hash *htbl = container_of(head,
1141 struct ipoib_neigh_hash,
1142 rcu);
1143 struct ipoib_neigh __rcu **buckets = htbl->buckets;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001144 struct ipoib_neigh_table *ntbl = htbl->ntbl;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001145
1146 kfree(buckets);
1147 kfree(htbl);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001148 complete(&ntbl->deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001149}
1150
1151void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1152{
1153 struct ipoib_dev_priv *priv = netdev_priv(dev);
1154 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1155 struct ipoib_neigh_hash *htbl;
1156 unsigned long flags;
1157 int i;
1158
1159 /* remove all neigh connected to a given path or mcast */
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001160 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001161
1162 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001163 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001164
1165 if (!htbl)
1166 goto out_unlock;
1167
1168 for (i = 0; i < htbl->size; i++) {
1169 struct ipoib_neigh *neigh;
1170 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1171
1172 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001173 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001174 /* delete neighs belong to this parent */
1175 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1176 rcu_assign_pointer(*np,
1177 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001178 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001179 /* remove from parent list */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001180 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001181 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1182 } else {
1183 np = &neigh->hnext;
1184 }
1185
1186 }
1187 }
1188out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001189 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001190}
1191
1192static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1193{
1194 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1195 struct ipoib_neigh_hash *htbl;
1196 unsigned long flags;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001197 int i, wait_flushed = 0;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001198
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001199 init_completion(&priv->ntbl.flushed);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001200
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001201 spin_lock_irqsave(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001202
1203 htbl = rcu_dereference_protected(ntbl->htbl,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001204 lockdep_is_held(&priv->lock));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001205 if (!htbl)
1206 goto out_unlock;
1207
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001208 wait_flushed = atomic_read(&priv->ntbl.entries);
1209 if (!wait_flushed)
1210 goto free_htbl;
1211
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001212 for (i = 0; i < htbl->size; i++) {
1213 struct ipoib_neigh *neigh;
1214 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1215
1216 while ((neigh = rcu_dereference_protected(*np,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001217 lockdep_is_held(&priv->lock))) != NULL) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001218 rcu_assign_pointer(*np,
1219 rcu_dereference_protected(neigh->hnext,
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001220 lockdep_is_held(&priv->lock)));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001221 /* remove from path/mc list */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001222 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001223 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1224 }
1225 }
1226
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001227free_htbl:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001228 rcu_assign_pointer(ntbl->htbl, NULL);
1229 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1230
1231out_unlock:
Shlomo Pongratzb5120a62012-08-29 15:14:34 +00001232 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001233 if (wait_flushed)
1234 wait_for_completion(&priv->ntbl.flushed);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001235}
1236
1237static void ipoib_neigh_hash_uninit(struct net_device *dev)
1238{
1239 struct ipoib_dev_priv *priv = netdev_priv(dev);
1240 int stopped;
1241
1242 ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001243 init_completion(&priv->ntbl.deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001244 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1245
1246 /* Stop GC if called at init fail need to cancel work */
1247 stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1248 if (!stopped)
1249 cancel_delayed_work(&priv->neigh_reap_task);
1250
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001251 ipoib_flush_neighs(priv);
1252
1253 wait_for_completion(&priv->ntbl.deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001254}
1255
1256
Linus Torvalds1da177e2005-04-16 15:20:36 -07001257int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1258{
1259 struct ipoib_dev_priv *priv = netdev_priv(dev);
1260
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001261 if (ipoib_neigh_hash_init(priv) < 0)
1262 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 /* Allocate RX/TX "rings" to hold queued skbs */
Shirley Ma0f485252006-04-10 09:43:58 -07001264 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 GFP_KERNEL);
1266 if (!priv->rx_ring) {
1267 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
Shirley Ma0f485252006-04-10 09:43:58 -07001268 ca->name, ipoib_recvq_size);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001269 goto out_neigh_hash_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
Joe Perches948579c2010-11-05 03:07:36 +00001272 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001273 if (!priv->tx_ring) {
1274 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
Shirley Ma0f485252006-04-10 09:43:58 -07001275 ca->name, ipoib_sendq_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276 goto out_rx_ring_cleanup;
1277 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001279 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 if (ipoib_ib_dev_init(dev, ca, port))
1282 goto out_tx_ring_cleanup;
1283
1284 return 0;
1285
1286out_tx_ring_cleanup:
Roland Dreier10313cb2008-03-12 07:51:03 -07001287 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289out_rx_ring_cleanup:
1290 kfree(priv->rx_ring);
1291
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001292out_neigh_hash_cleanup:
1293 ipoib_neigh_hash_uninit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294out:
1295 return -ENOMEM;
1296}
1297
1298void ipoib_dev_cleanup(struct net_device *dev)
1299{
1300 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001301 LIST_HEAD(head);
1302
1303 ASSERT_RTNL();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304
Roland Dreier1732b0e2005-11-07 10:33:11 -08001305 ipoib_delete_debug_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001306
1307 /* Delete any child interfaces first */
1308 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001309 /* Stop GC on child */
1310 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1311 cancel_delayed_work(&cpriv->neigh_reap_task);
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001312 unregister_netdevice_queue(cpriv->dev, &head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 }
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001314 unregister_netdevice_many(&head);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001315
1316 ipoib_ib_dev_cleanup(dev);
1317
Hal Rosenstock92a6b342005-08-13 20:50:27 -07001318 kfree(priv->rx_ring);
Roland Dreier10313cb2008-03-12 07:51:03 -07001319 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
Hal Rosenstock92a6b342005-08-13 20:50:27 -07001321 priv->rx_ring = NULL;
1322 priv->tx_ring = NULL;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001323
1324 ipoib_neigh_hash_uninit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001325}
1326
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001327static const struct header_ops ipoib_header_ops = {
1328 .create = ipoib_hard_header,
1329};
1330
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001331static const struct net_device_ops ipoib_netdev_ops = {
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001332 .ndo_uninit = ipoib_uninit,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001333 .ndo_open = ipoib_open,
1334 .ndo_stop = ipoib_stop,
1335 .ndo_change_mtu = ipoib_change_mtu,
Michał Mirosław3d96c742011-04-19 00:43:20 +00001336 .ndo_fix_features = ipoib_fix_features,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001337 .ndo_start_xmit = ipoib_start_xmit,
1338 .ndo_tx_timeout = ipoib_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001339 .ndo_set_rx_mode = ipoib_set_mcast_list,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001340};
1341
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001342void ipoib_setup(struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001343{
1344 struct ipoib_dev_priv *priv = netdev_priv(dev);
1345
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001346 dev->netdev_ops = &ipoib_netdev_ops;
Roland Dreier2337f802007-10-23 19:57:54 -07001347 dev->header_ops = &ipoib_header_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001348
Eli Cohen82c24c12008-04-16 21:09:32 -07001349 ipoib_set_ethtool_ops(dev);
1350
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001351 netif_napi_add(dev, &priv->napi, ipoib_poll, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352
Roland Dreier2337f802007-10-23 19:57:54 -07001353 dev->watchdog_timeo = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354
Roland Dreier2337f802007-10-23 19:57:54 -07001355 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001356
Roland Dreier936d7de2012-02-07 14:51:21 +00001357 dev->hard_header_len = IPOIB_ENCAP_LEN;
Roland Dreier2337f802007-10-23 19:57:54 -07001358 dev->addr_len = INFINIBAND_ALEN;
1359 dev->type = ARPHRD_INFINIBAND;
1360 dev->tx_queue_len = ipoib_sendq_size * 2;
Eli Coheneb14032f2008-01-30 18:30:46 +02001361 dev->features = (NETIF_F_VLAN_CHALLENGED |
Eli Coheneb14032f2008-01-30 18:30:46 +02001362 NETIF_F_HIGHDMA);
Eric Dumazet86d15cd2009-05-30 23:04:46 -07001363 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
1366
1367 netif_carrier_off(dev);
1368
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 priv->dev = dev;
1370
1371 spin_lock_init(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001372
Ingo Molnar95ed6442006-01-13 14:51:39 -08001373 mutex_init(&priv->vlan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374
1375 INIT_LIST_HEAD(&priv->path_list);
1376 INIT_LIST_HEAD(&priv->child_intfs);
1377 INIT_LIST_HEAD(&priv->dead_ahs);
1378 INIT_LIST_HEAD(&priv->multicast_list);
1379
Yosef Etigin26bbf132007-05-19 08:51:54 -07001380 INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
David Howellsc4028952006-11-22 14:57:56 +00001381 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
Yossi Etigine8224e42008-09-16 11:57:45 -07001382 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
Moni Shouaee1e2c82008-07-14 23:48:49 -07001383 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
1384 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
1385 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
David Howellsc4028952006-11-22 14:57:56 +00001386 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1387 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001388 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001389}
1390
1391struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
1392{
1393 struct net_device *dev;
1394
1395 dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
1396 ipoib_setup);
1397 if (!dev)
1398 return NULL;
1399
1400 return netdev_priv(dev);
1401}
1402
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001403static ssize_t show_pkey(struct device *dev,
1404 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001406 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407
1408 return sprintf(buf, "0x%04x\n", priv->pkey);
1409}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001410static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001411
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001412static ssize_t show_umcast(struct device *dev,
1413 struct device_attribute *attr, char *buf)
1414{
1415 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1416
1417 return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1418}
1419
Or Gerlitz862096a2012-09-27 12:06:02 +00001420void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001421{
Or Gerlitz862096a2012-09-27 12:06:02 +00001422 struct ipoib_dev_priv *priv = netdev_priv(ndev);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001423
1424 if (umcast_val > 0) {
1425 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1426 ipoib_warn(priv, "ignoring multicast groups joined directly "
1427 "by userspace\n");
1428 } else
1429 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
Or Gerlitz862096a2012-09-27 12:06:02 +00001430}
1431
1432static ssize_t set_umcast(struct device *dev,
1433 struct device_attribute *attr,
1434 const char *buf, size_t count)
1435{
1436 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1437
1438 ipoib_set_umcast(to_net_dev(dev), umcast_val);
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001439
1440 return count;
1441}
1442static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1443
1444int ipoib_add_umcast_attr(struct net_device *dev)
1445{
1446 return device_create_file(&dev->dev, &dev_attr_umcast);
1447}
1448
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001449static ssize_t create_child(struct device *dev,
1450 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001451 const char *buf, size_t count)
1452{
1453 int pkey;
1454 int ret;
1455
1456 if (sscanf(buf, "%i", &pkey) != 1)
1457 return -EINVAL;
1458
1459 if (pkey < 0 || pkey > 0xffff)
1460 return -EINVAL;
1461
Roland Dreier4ce05932005-08-19 12:03:17 -07001462 /*
1463 * Set the full membership bit, so that we join the right
1464 * broadcast group, etc.
1465 */
1466 pkey |= 0x8000;
1467
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001468 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470 return ret ? ret : count;
1471}
Or Gerlitz7a52b342010-06-06 04:59:16 +00001472static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001473
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001474static ssize_t delete_child(struct device *dev,
1475 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001476 const char *buf, size_t count)
1477{
1478 int pkey;
1479 int ret;
1480
1481 if (sscanf(buf, "%i", &pkey) != 1)
1482 return -EINVAL;
1483
1484 if (pkey < 0 || pkey > 0xffff)
1485 return -EINVAL;
1486
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001487 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488
1489 return ret ? ret : count;
1490
1491}
Or Gerlitz7a52b342010-06-06 04:59:16 +00001492static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001493
1494int ipoib_add_pkey_attr(struct net_device *dev)
1495{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001496 return device_create_file(&dev->dev, &dev_attr_pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497}
1498
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001499int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
1500{
1501 struct ib_device_attr *device_attr;
1502 int result = -ENOMEM;
1503
1504 device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
1505 if (!device_attr) {
1506 printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
1507 hca->name, sizeof *device_attr);
1508 return result;
1509 }
1510
1511 result = ib_query_device(hca, device_attr);
1512 if (result) {
1513 printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
1514 hca->name, result);
1515 kfree(device_attr);
1516 return result;
1517 }
1518 priv->hca_caps = device_attr->device_cap_flags;
1519
1520 kfree(device_attr);
1521
1522 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
Michał Mirosław3d96c742011-04-19 00:43:20 +00001523 priv->dev->hw_features = NETIF_F_SG |
1524 NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1525
1526 if (priv->hca_caps & IB_DEVICE_UD_TSO)
1527 priv->dev->hw_features |= NETIF_F_TSO;
1528
1529 priv->dev->features |= priv->dev->hw_features;
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001530 }
1531
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001532 return 0;
1533}
1534
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535static struct net_device *ipoib_add_port(const char *format,
1536 struct ib_device *hca, u8 port)
1537{
1538 struct ipoib_dev_priv *priv;
Shirley Mabc7b3a32008-04-23 11:55:45 -07001539 struct ib_port_attr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 int result = -ENOMEM;
1541
1542 priv = ipoib_intf_alloc(format);
1543 if (!priv)
1544 goto alloc_mem_failed;
1545
1546 SET_NETDEV_DEV(priv->dev, hca->dma_device);
Eli Cohenc3aa9b12010-09-20 07:05:06 +00001547 priv->dev->dev_id = port - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001548
Shirley Mabc7b3a32008-04-23 11:55:45 -07001549 if (!ib_query_port(hca, port, &attr))
1550 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1551 else {
1552 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
1553 hca->name, port);
1554 goto device_init_failed;
1555 }
1556
1557 /* MTU will be reset when mcast join happens */
1558 priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
1559 priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
1560
David Miller596b9b62011-07-25 00:01:25 +00001561 priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
1562
Linus Torvalds1da177e2005-04-16 15:20:36 -07001563 result = ib_query_pkey(hca, port, 0, &priv->pkey);
1564 if (result) {
1565 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1566 hca->name, port, result);
Eli Cohenca6de172007-08-21 18:46:10 +03001567 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001568 }
1569
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001570 if (ipoib_set_dev_features(priv, hca))
Eli Cohen60461362008-04-16 21:01:10 -07001571 goto device_init_failed;
Vladimir Sokolovskyaf40da82008-07-14 23:48:48 -07001572
Roland Dreier4ce05932005-08-19 12:03:17 -07001573 /*
1574 * Set the full membership bit, so that we join the right
1575 * broadcast group, etc.
1576 */
1577 priv->pkey |= 0x8000;
1578
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 priv->dev->broadcast[8] = priv->pkey >> 8;
1580 priv->dev->broadcast[9] = priv->pkey & 0xff;
1581
1582 result = ib_query_gid(hca, port, 0, &priv->local_gid);
1583 if (result) {
1584 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1585 hca->name, port, result);
Eli Cohenca6de172007-08-21 18:46:10 +03001586 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001587 } else
1588 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1589
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590 result = ipoib_dev_init(priv->dev, hca, port);
1591 if (result < 0) {
1592 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1593 hca->name, port, result);
1594 goto device_init_failed;
1595 }
1596
1597 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1598 priv->ca, ipoib_event);
1599 result = ib_register_event_handler(&priv->event_handler);
1600 if (result < 0) {
1601 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1602 "port %d (ret = %d)\n",
1603 hca->name, port, result);
1604 goto event_failed;
1605 }
1606
1607 result = register_netdev(priv->dev);
1608 if (result) {
1609 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1610 hca->name, port, result);
1611 goto register_failed;
1612 }
1613
Roland Dreier1732b0e2005-11-07 10:33:11 -08001614 ipoib_create_debug_files(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001615
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001616 if (ipoib_cm_add_mode_attr(priv->dev))
1617 goto sysfs_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 if (ipoib_add_pkey_attr(priv->dev))
1619 goto sysfs_failed;
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001620 if (ipoib_add_umcast_attr(priv->dev))
1621 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001622 if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001624 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 goto sysfs_failed;
1626
1627 return priv->dev;
1628
1629sysfs_failed:
Roland Dreier1732b0e2005-11-07 10:33:11 -08001630 ipoib_delete_debug_files(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001631 unregister_netdev(priv->dev);
1632
1633register_failed:
1634 ib_unregister_event_handler(&priv->event_handler);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001635 /* Stop GC if started before flush */
1636 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1637 cancel_delayed_work(&priv->neigh_reap_task);
Roland Dreiera77a57a2008-08-19 15:01:32 -07001638 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639
1640event_failed:
1641 ipoib_dev_cleanup(priv->dev);
1642
1643device_init_failed:
1644 free_netdev(priv->dev);
1645
1646alloc_mem_failed:
1647 return ERR_PTR(result);
1648}
1649
1650static void ipoib_add_one(struct ib_device *device)
1651{
1652 struct list_head *dev_list;
1653 struct net_device *dev;
1654 struct ipoib_dev_priv *priv;
1655 int s, e, p;
1656
Tom Tucker07ebafb2006-08-03 16:02:42 -05001657 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1658 return;
1659
Linus Torvalds1da177e2005-04-16 15:20:36 -07001660 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1661 if (!dev_list)
1662 return;
1663
1664 INIT_LIST_HEAD(dev_list);
1665
Tom Tucker07ebafb2006-08-03 16:02:42 -05001666 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001667 s = 0;
1668 e = 0;
1669 } else {
1670 s = 1;
1671 e = device->phys_port_cnt;
1672 }
1673
1674 for (p = s; p <= e; ++p) {
Eli Cohen7b4c8762010-09-27 17:51:11 -07001675 if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
1676 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 dev = ipoib_add_port("ib%d", device, p);
1678 if (!IS_ERR(dev)) {
1679 priv = netdev_priv(dev);
1680 list_add_tail(&priv->list, dev_list);
1681 }
1682 }
1683
1684 ib_set_client_data(device, &ipoib_client, dev_list);
1685}
1686
1687static void ipoib_remove_one(struct ib_device *device)
1688{
1689 struct ipoib_dev_priv *priv, *tmp;
1690 struct list_head *dev_list;
1691
Tom Tucker07ebafb2006-08-03 16:02:42 -05001692 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1693 return;
1694
Linus Torvalds1da177e2005-04-16 15:20:36 -07001695 dev_list = ib_get_client_data(device, &ipoib_client);
Itai Garbi5a2815f2013-02-19 15:40:24 +00001696 if (!dev_list)
1697 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698
1699 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1700 ib_unregister_event_handler(&priv->event_handler);
Roland Dreiera77a57a2008-08-19 15:01:32 -07001701
1702 rtnl_lock();
1703 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
1704 rtnl_unlock();
1705
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001706 /* Stop GC */
1707 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1708 cancel_delayed_work(&priv->neigh_reap_task);
Roland Dreiera77a57a2008-08-19 15:01:32 -07001709 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710
1711 unregister_netdev(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 free_netdev(priv->dev);
1713 }
Michael S. Tsirkin06c56e42005-09-01 09:19:02 -07001714
1715 kfree(dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001716}
1717
1718static int __init ipoib_init_module(void)
1719{
1720 int ret;
1721
Shirley Ma0f485252006-04-10 09:43:58 -07001722 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1723 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1724 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1725
1726 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1727 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -07001728 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001729#ifdef CONFIG_INFINIBAND_IPOIB_CM
1730 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1731#endif
Shirley Ma0f485252006-04-10 09:43:58 -07001732
Eli Cohenf89271da2008-07-14 23:48:44 -07001733 /*
1734 * When copying small received packets, we only copy from the
1735 * linear data part of the SKB, so we rely on this condition.
1736 */
1737 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
1738
Linus Torvalds1da177e2005-04-16 15:20:36 -07001739 ret = ipoib_register_debugfs();
1740 if (ret)
1741 return ret;
1742
1743 /*
1744 * We create our own workqueue mainly because we want to be
1745 * able to flush it when devices are being removed. We can't
1746 * use schedule_work()/flush_scheduled_work() because both
1747 * unregister_netdev() and linkwatch_event take the rtnl lock,
1748 * so flush_scheduled_work() can deadlock during device
1749 * removal.
1750 */
1751 ipoib_workqueue = create_singlethread_workqueue("ipoib");
1752 if (!ipoib_workqueue) {
1753 ret = -ENOMEM;
1754 goto err_fs;
1755 }
1756
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001757 ib_sa_register_client(&ipoib_sa_client);
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759 ret = ib_register_client(&ipoib_client);
1760 if (ret)
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001761 goto err_sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001763 ret = ipoib_netlink_init();
1764 if (ret)
1765 goto err_client;
1766
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767 return 0;
1768
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001769err_client:
1770 ib_unregister_client(&ipoib_client);
1771
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001772err_sa:
1773 ib_sa_unregister_client(&ipoib_sa_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 destroy_workqueue(ipoib_workqueue);
1775
Roland Dreier9adec1a2005-04-16 15:26:07 -07001776err_fs:
1777 ipoib_unregister_debugfs();
1778
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 return ret;
1780}
1781
1782static void __exit ipoib_cleanup_module(void)
1783{
Or Gerlitz9baa0b02012-09-13 05:56:36 +00001784 ipoib_netlink_fini();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 ib_unregister_client(&ipoib_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001786 ib_sa_unregister_client(&ipoib_sa_client);
Roland Dreier9adec1a2005-04-16 15:26:07 -07001787 ipoib_unregister_debugfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 destroy_workqueue(ipoib_workqueue);
1789}
1790
1791module_init(ipoib_init_module);
1792module_exit(ipoib_cleanup_module);