blob: 72c1fc28f0799b63bfd209f25e45473f0f5e0606 [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
Roland Dreiera77a57a2008-08-19 15:01:32 -0700153 ipoib_ib_dev_down(dev, 0);
154 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
David S. Miller9ca36f72011-11-16 18:05:50 -0500176static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
Michał Mirosław3d96c742011-04-19 00:43:20 +0000177{
178 struct ipoib_dev_priv *priv = netdev_priv(dev);
179
180 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
181 features &= ~(NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
182
183 return features;
184}
185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
187{
188 struct ipoib_dev_priv *priv = netdev_priv(dev);
189
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200190 /* dev->mtu > 2K ==> connected mode */
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800191 if (ipoib_cm_admin_enabled(dev)) {
192 if (new_mtu > ipoib_cm_max_mtu(dev))
193 return -EINVAL;
194
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200195 if (new_mtu > priv->mcast_mtu)
196 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
197 priv->mcast_mtu);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800198
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200199 dev->mtu = new_mtu;
200 return 0;
201 }
202
Shirley Mabc7b3a32008-04-23 11:55:45 -0700203 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 return -EINVAL;
205
206 priv->admin_mtu = new_mtu;
207
208 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
209
210 return 0;
211}
212
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300213static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214{
215 struct ipoib_dev_priv *priv = netdev_priv(dev);
216 struct rb_node *n = priv->path_tree.rb_node;
217 struct ipoib_path *path;
218 int ret;
219
220 while (n) {
221 path = rb_entry(n, struct ipoib_path, rb_node);
222
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300223 ret = memcmp(gid, path->pathrec.dgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 sizeof (union ib_gid));
225
226 if (ret < 0)
227 n = n->rb_left;
228 else if (ret > 0)
229 n = n->rb_right;
230 else
231 return path;
232 }
233
234 return NULL;
235}
236
237static int __path_add(struct net_device *dev, struct ipoib_path *path)
238{
239 struct ipoib_dev_priv *priv = netdev_priv(dev);
240 struct rb_node **n = &priv->path_tree.rb_node;
241 struct rb_node *pn = NULL;
242 struct ipoib_path *tpath;
243 int ret;
244
245 while (*n) {
246 pn = *n;
247 tpath = rb_entry(pn, struct ipoib_path, rb_node);
248
249 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
250 sizeof (union ib_gid));
251 if (ret < 0)
252 n = &pn->rb_left;
253 else if (ret > 0)
254 n = &pn->rb_right;
255 else
256 return -EEXIST;
257 }
258
259 rb_link_node(&path->rb_node, pn, n);
260 rb_insert_color(&path->rb_node, &priv->path_tree);
261
262 list_add_tail(&path->list, &priv->path_list);
263
264 return 0;
265}
266
267static void path_free(struct net_device *dev, struct ipoib_path *path)
268{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 struct sk_buff *skb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270
271 while ((skb = __skb_dequeue(&path->queue)))
272 dev_kfree_skb_irq(skb);
273
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000274 ipoib_dbg(netdev_priv(dev), "path_free\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000276 /* remove all neigh connected to this path */
277 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278
279 if (path->ah)
280 ipoib_put_ah(path->ah);
281
282 kfree(path);
283}
284
Roland Dreier1732b0e2005-11-07 10:33:11 -0800285#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
286
287struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
288{
289 struct ipoib_path_iter *iter;
290
291 iter = kmalloc(sizeof *iter, GFP_KERNEL);
292 if (!iter)
293 return NULL;
294
295 iter->dev = dev;
296 memset(iter->path.pathrec.dgid.raw, 0, 16);
297
298 if (ipoib_path_iter_next(iter)) {
299 kfree(iter);
300 return NULL;
301 }
302
303 return iter;
304}
305
306int ipoib_path_iter_next(struct ipoib_path_iter *iter)
307{
308 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
309 struct rb_node *n;
310 struct ipoib_path *path;
311 int ret = 1;
312
313 spin_lock_irq(&priv->lock);
314
315 n = rb_first(&priv->path_tree);
316
317 while (n) {
318 path = rb_entry(n, struct ipoib_path, rb_node);
319
320 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
321 sizeof (union ib_gid)) < 0) {
322 iter->path = *path;
323 ret = 0;
324 break;
325 }
326
327 n = rb_next(n);
328 }
329
330 spin_unlock_irq(&priv->lock);
331
332 return ret;
333}
334
335void ipoib_path_iter_read(struct ipoib_path_iter *iter,
336 struct ipoib_path *path)
337{
338 *path = iter->path;
339}
340
341#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
342
Moni Shouaee1e2c82008-07-14 23:48:49 -0700343void ipoib_mark_paths_invalid(struct net_device *dev)
344{
345 struct ipoib_dev_priv *priv = netdev_priv(dev);
346 struct ipoib_path *path, *tp;
347
348 spin_lock_irq(&priv->lock);
349
350 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700351 ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
Moni Shouaee1e2c82008-07-14 23:48:49 -0700352 be16_to_cpu(path->pathrec.dlid),
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700353 path->pathrec.dgid.raw);
Moni Shouaee1e2c82008-07-14 23:48:49 -0700354 path->valid = 0;
355 }
356
357 spin_unlock_irq(&priv->lock);
358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360void ipoib_flush_paths(struct net_device *dev)
361{
362 struct ipoib_dev_priv *priv = netdev_priv(dev);
363 struct ipoib_path *path, *tp;
364 LIST_HEAD(remove_list);
Roland Dreier943c2462008-09-30 10:36:21 -0700365 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
Roland Dreier943c2462008-09-30 10:36:21 -0700367 netif_tx_lock_bh(dev);
368 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Robert P. J. Day157de222008-04-16 21:09:26 -0700370 list_splice_init(&priv->path_list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 list_for_each_entry(path, &remove_list, list)
373 rb_erase(&path->rb_node, &priv->path_tree);
374
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 list_for_each_entry_safe(path, tp, &remove_list, list) {
376 if (path->query)
377 ib_sa_cancel_query(path->query_id, path->query);
Roland Dreier943c2462008-09-30 10:36:21 -0700378 spin_unlock_irqrestore(&priv->lock, flags);
379 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 wait_for_completion(&path->done);
381 path_free(dev, path);
Roland Dreier943c2462008-09-30 10:36:21 -0700382 netif_tx_lock_bh(dev);
383 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
Roland Dreier943c2462008-09-30 10:36:21 -0700385
386 spin_unlock_irqrestore(&priv->lock, flags);
387 netif_tx_unlock_bh(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388}
389
390static void path_rec_completion(int status,
391 struct ib_sa_path_rec *pathrec,
392 void *path_ptr)
393{
394 struct ipoib_path *path = path_ptr;
395 struct net_device *dev = path->dev;
396 struct ipoib_dev_priv *priv = netdev_priv(dev);
397 struct ipoib_ah *ah = NULL;
Roland Dreierc9da4ba2008-09-25 15:26:15 -0700398 struct ipoib_ah *old_ah = NULL;
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700399 struct ipoib_neigh *neigh, *tn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 struct sk_buff_head skqueue;
401 struct sk_buff *skb;
402 unsigned long flags;
403
Roland Dreier843613b2007-02-26 12:57:08 -0800404 if (!status)
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700405 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700406 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 else
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700408 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700409 status, path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700410
411 skb_queue_head_init(&skqueue);
412
413 if (!status) {
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700414 struct ib_ah_attr av;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700416 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
417 ah = ipoib_create_ah(dev, priv->pd, &av);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419
420 spin_lock_irqsave(&priv->lock, flags);
421
Mike Marciniszyn38743972011-11-21 08:43:54 -0500422 if (!IS_ERR_OR_NULL(ah)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 path->pathrec = *pathrec;
424
Roland Dreierc9da4ba2008-09-25 15:26:15 -0700425 old_ah = path->ah;
426 path->ah = ah;
427
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
429 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
430
431 while ((skb = __skb_dequeue(&path->queue)))
432 __skb_queue_tail(&skqueue, skb);
433
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700434 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
Moni Shouaee1e2c82008-07-14 23:48:49 -0700435 if (neigh->ah) {
436 WARN_ON(neigh->ah != old_ah);
437 /*
438 * Dropping the ah reference inside
439 * priv->lock is safe here, because we
440 * will hold one more reference from
441 * the original value of path->ah (ie
442 * old_ah).
443 */
444 ipoib_put_ah(neigh->ah);
445 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446 kref_get(&path->ah->ref);
447 neigh->ah = path->ah;
448
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000449 if (ipoib_cm_enabled(dev, neigh->daddr)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200450 if (!ipoib_cm_get(neigh))
451 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
452 path,
453 neigh));
454 if (!ipoib_cm_get(neigh)) {
455 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000456 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200457 continue;
458 }
459 }
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 while ((skb = __skb_dequeue(&neigh->queue)))
462 __skb_queue_tail(&skqueue, skb);
463 }
Moni Shouaee1e2c82008-07-14 23:48:49 -0700464 path->valid = 1;
Roland Dreier5872a9f2005-11-29 10:13:54 -0800465 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Roland Dreier5872a9f2005-11-29 10:13:54 -0800467 path->query = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 complete(&path->done);
469
470 spin_unlock_irqrestore(&priv->lock, flags);
471
Moni Shouaee1e2c82008-07-14 23:48:49 -0700472 if (old_ah)
473 ipoib_put_ah(old_ah);
474
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475 while ((skb = __skb_dequeue(&skqueue))) {
476 skb->dev = dev;
477 if (dev_queue_xmit(skb))
478 ipoib_warn(priv, "dev_queue_xmit failed "
479 "to requeue packet\n");
480 }
481}
482
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300483static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484{
485 struct ipoib_dev_priv *priv = netdev_priv(dev);
486 struct ipoib_path *path;
487
Jack Morgenstein1401b532007-11-26 10:41:19 +0200488 if (!priv->broadcast)
489 return NULL;
490
Roland Dreier21a38482005-11-02 10:07:59 -0800491 path = kzalloc(sizeof *path, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 if (!path)
493 return NULL;
494
Roland Dreier21a38482005-11-02 10:07:59 -0800495 path->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 skb_queue_head_init(&path->queue);
498
499 INIT_LIST_HEAD(&path->neigh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300501 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
Roland Dreier2337f802007-10-23 19:57:54 -0700502 path->pathrec.sgid = priv->local_gid;
503 path->pathrec.pkey = cpu_to_be16(priv->pkey);
Sean Hefty81668832007-08-02 12:21:31 -0700504 path->pathrec.numb_path = 1;
505 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700506
507 return path;
508}
509
510static int path_rec_start(struct net_device *dev,
511 struct ipoib_path *path)
512{
513 struct ipoib_dev_priv *priv = netdev_priv(dev);
514
Harvey Harrison5b095d9892008-10-29 12:52:50 -0700515 ipoib_dbg(priv, "Start path record lookup for %pI6\n",
Harvey Harrisonfcace2f2008-10-28 22:37:22 -0700516 path->pathrec.dgid.raw);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517
Roland Dreier65c7edd2005-11-28 21:20:34 -0800518 init_completion(&path->done);
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 path->query_id =
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700521 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 &path->pathrec,
523 IB_SA_PATH_REC_DGID |
524 IB_SA_PATH_REC_SGID |
525 IB_SA_PATH_REC_NUMB_PATH |
Sean Hefty81668832007-08-02 12:21:31 -0700526 IB_SA_PATH_REC_TRAFFIC_CLASS |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 IB_SA_PATH_REC_PKEY,
528 1000, GFP_ATOMIC,
529 path_rec_completion,
530 path, &path->query);
531 if (path->query_id < 0) {
Or Gerlitz01b3fc82008-07-22 14:18:34 -0700532 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 path->query = NULL;
Yossi Etigin93a3ab92008-11-12 10:24:38 -0800534 complete(&path->done);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 return path->query_id;
536 }
537
538 return 0;
539}
540
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000541static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
542 struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
544 struct ipoib_dev_priv *priv = netdev_priv(dev);
545 struct ipoib_path *path;
546 struct ipoib_neigh *neigh;
Roland Dreier943c2462008-09-30 10:36:21 -0700547 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000549 neigh = ipoib_neigh_alloc(daddr, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 if (!neigh) {
Roland Dreierde903512007-09-28 15:33:51 -0700551 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 dev_kfree_skb_any(skb);
553 return;
554 }
555
Roland Dreier943c2462008-09-30 10:36:21 -0700556 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000558 path = __path_find(dev, daddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559 if (!path) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000560 path = path_rec_create(dev, daddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 if (!path)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300562 goto err_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 __path_add(dev, path);
565 }
566
567 list_add_tail(&neigh->list, &path->neigh_list);
568
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -0800569 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700570 kref_get(&path->ah->ref);
571 neigh->ah = path->ah;
572
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000573 if (ipoib_cm_enabled(dev, neigh->daddr)) {
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200574 if (!ipoib_cm_get(neigh))
575 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
576 if (!ipoib_cm_get(neigh)) {
577 list_del(&neigh->list);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000578 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200579 goto err_drop;
580 }
581 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
582 __skb_queue_tail(&neigh->queue, skb);
583 else {
584 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
585 skb_queue_len(&neigh->queue));
586 goto err_drop;
587 }
Roland Dreier721d67c2009-09-05 20:23:40 -0700588 } else {
589 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000590 ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
591 ipoib_neigh_put(neigh);
Roland Dreier721d67c2009-09-05 20:23:40 -0700592 return;
593 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 } else {
595 neigh->ah = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596
597 if (!path->query && path_rec_start(dev, path))
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300598 goto err_list;
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +0200599
600 __skb_queue_tail(&neigh->queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700601 }
602
Roland Dreier943c2462008-09-30 10:36:21 -0700603 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000604 ipoib_neigh_put(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 return;
606
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300607err_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608 list_del(&neigh->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300610err_path:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000611 ipoib_neigh_free(neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200612err_drop:
Roland Dreierde903512007-09-28 15:33:51 -0700613 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 dev_kfree_skb_any(skb);
615
Roland Dreier943c2462008-09-30 10:36:21 -0700616 spin_unlock_irqrestore(&priv->lock, flags);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000617 ipoib_neigh_put(neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618}
619
620static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
Roland Dreier936d7de2012-02-07 14:51:21 +0000621 struct ipoib_cb *cb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622{
623 struct ipoib_dev_priv *priv = netdev_priv(dev);
624 struct ipoib_path *path;
Roland Dreier943c2462008-09-30 10:36:21 -0700625 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626
Roland Dreier943c2462008-09-30 10:36:21 -0700627 spin_lock_irqsave(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628
Roland Dreier936d7de2012-02-07 14:51:21 +0000629 path = __path_find(dev, cb->hwaddr + 4);
Moni Shouaee1e2c82008-07-14 23:48:49 -0700630 if (!path || !path->valid) {
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800631 int new_path = 0;
632
633 if (!path) {
Roland Dreier936d7de2012-02-07 14:51:21 +0000634 path = path_rec_create(dev, cb->hwaddr + 4);
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800635 new_path = 1;
636 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 if (path) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638 __skb_queue_tail(&path->queue, skb);
639
Yossi Etiginff79ae82008-11-12 10:24:39 -0800640 if (!path->query && path_rec_start(dev, path)) {
Roland Dreier943c2462008-09-30 10:36:21 -0700641 spin_unlock_irqrestore(&priv->lock, flags);
Jack Morgenstein71d98b42009-02-17 14:51:47 -0800642 if (new_path)
643 path_free(dev, path);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 return;
645 } else
646 __path_add(dev, path);
647 } else {
Roland Dreierde903512007-09-28 15:33:51 -0700648 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 dev_kfree_skb_any(skb);
650 }
651
Roland Dreier943c2462008-09-30 10:36:21 -0700652 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653 return;
654 }
655
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -0800656 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
658 be16_to_cpu(path->pathrec.dlid));
659
Roland Dreier721d67c2009-09-05 20:23:40 -0700660 spin_unlock_irqrestore(&priv->lock, flags);
Roland Dreier936d7de2012-02-07 14:51:21 +0000661 ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr));
Roland Dreier721d67c2009-09-05 20:23:40 -0700662 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 } else if ((path->query || !path_rec_start(dev, path)) &&
664 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 __skb_queue_tail(&path->queue, skb);
666 } else {
Roland Dreierde903512007-09-28 15:33:51 -0700667 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 dev_kfree_skb_any(skb);
669 }
670
Roland Dreier943c2462008-09-30 10:36:21 -0700671 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672}
673
674static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
675{
676 struct ipoib_dev_priv *priv = netdev_priv(dev);
677 struct ipoib_neigh *neigh;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000678 struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
679 struct ipoib_header *header;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 unsigned long flags;
681
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000682 header = (struct ipoib_header *) skb->data;
683
684 if (unlikely(cb->hwaddr[4] == 0xff)) {
685 /* multicast, arrange "if" according to probability */
686 if ((header->proto != htons(ETH_P_IP)) &&
687 (header->proto != htons(ETH_P_IPV6)) &&
688 (header->proto != htons(ETH_P_ARP)) &&
689 (header->proto != htons(ETH_P_RARP))) {
690 /* ethertype not supported by IPoIB */
David Miller17e6abe2011-12-02 16:52:44 +0000691 ++dev->stats.tx_dropped;
692 dev_kfree_skb_any(skb);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000693 return NETDEV_TX_OK;
David Miller17e6abe2011-12-02 16:52:44 +0000694 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000695 /* Add in the P_Key for multicast*/
696 cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
697 cb->hwaddr[9] = priv->pkey & 0xff;
698
699 neigh = ipoib_neigh_get(dev, cb->hwaddr);
700 if (likely(neigh))
701 goto send_using_neigh;
702 ipoib_mcast_send(dev, cb->hwaddr, skb);
703 return NETDEV_TX_OK;
David Miller17e6abe2011-12-02 16:52:44 +0000704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000706 /* unicast, arrange "switch" according to probability */
707 switch (header->proto) {
708 case htons(ETH_P_IP):
709 case htons(ETH_P_IPV6):
710 neigh = ipoib_neigh_get(dev, cb->hwaddr);
711 if (unlikely(!neigh)) {
712 neigh_add_path(skb, cb->hwaddr, dev);
713 return NETDEV_TX_OK;
Yossi Etigina50df392009-01-09 14:05:11 -0800714 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000715 break;
716 case htons(ETH_P_ARP):
717 case htons(ETH_P_RARP):
718 /* for unicast ARP and RARP should always perform path find */
719 unicast_arp_send(skb, dev, cb);
720 return NETDEV_TX_OK;
721 default:
722 /* ethertype not supported by IPoIB */
723 ++dev->stats.tx_dropped;
724 dev_kfree_skb_any(skb);
725 return NETDEV_TX_OK;
726 }
Michael S. Tsirkin8a7f7522006-07-19 17:44:37 +0300727
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000728send_using_neigh:
729 /* note we now hold a ref to neigh */
730 if (ipoib_cm_get(neigh)) {
731 if (ipoib_cm_up(neigh)) {
732 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
733 goto unref;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000735 } else if (neigh->ah) {
736 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
737 goto unref;
738 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000740 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
741 spin_lock_irqsave(&priv->lock, flags);
742 __skb_queue_tail(&neigh->queue, skb);
743 spin_unlock_irqrestore(&priv->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 } else {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000745 ++dev->stats.tx_dropped;
746 dev_kfree_skb_any(skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000748
749unref:
750 ipoib_neigh_put(neigh);
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 return NETDEV_TX_OK;
753}
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755static void ipoib_timeout(struct net_device *dev)
756{
757 struct ipoib_dev_priv *priv = netdev_priv(dev);
758
Roland Dreier4b2d3192005-10-18 12:20:06 -0700759 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
760 jiffies_to_msecs(jiffies - dev->trans_start));
761 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
762 netif_queue_stopped(dev),
763 priv->tx_head, priv->tx_tail);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 /* XXX reset QP, etc. */
765}
766
767static int ipoib_hard_header(struct sk_buff *skb,
768 struct net_device *dev,
769 unsigned short type,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700770 const void *daddr, const void *saddr, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
772 struct ipoib_header *header;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000773 struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774
775 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
776
777 header->proto = htons(type);
778 header->reserved = 0;
779
780 /*
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000781 * we don't rely on dst_entry structure, always stuff the
Roland Dreier936d7de2012-02-07 14:51:21 +0000782 * destination address into skb->cb so we can figure out where
783 * to send the packet later.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784 */
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000785 memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
787 return 0;
788}
789
790static void ipoib_set_mcast_list(struct net_device *dev)
791{
792 struct ipoib_dev_priv *priv = netdev_priv(dev);
793
Leonid Arsh7a343d42006-03-23 19:52:51 +0200794 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
795 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
796 return;
797 }
798
Michael S. Tsirkin1ad62a12005-08-24 14:41:51 -0700799 queue_work(ipoib_workqueue, &priv->restart_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800}
801
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000802static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000804 /*
805 * Use only the address parts that contributes to spreading
806 * The subnet prefix is not used as one can not connect to
807 * same remote port (GUID) using the same remote QPN via two
808 * different subnets.
809 */
810 /* qpn octets[1:4) & port GUID octets[12:20) */
811 u32 *daddr_32 = (u32 *) daddr;
812 u32 hv;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000814 hv = jhash_3words(daddr_32[3], daddr_32[4], 0xFFFFFF & daddr_32[0], 0);
815 return hv & htbl->mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816}
817
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000818struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
819{
820 struct ipoib_dev_priv *priv = netdev_priv(dev);
821 struct ipoib_neigh_table *ntbl = &priv->ntbl;
822 struct ipoib_neigh_hash *htbl;
823 struct ipoib_neigh *neigh = NULL;
824 u32 hash_val;
825
826 rcu_read_lock_bh();
827
828 htbl = rcu_dereference_bh(ntbl->htbl);
829
830 if (!htbl)
831 goto out_unlock;
832
833 hash_val = ipoib_addr_hash(htbl, daddr);
834 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
835 neigh != NULL;
836 neigh = rcu_dereference_bh(neigh->hnext)) {
837 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
838 /* found, take one ref on behalf of the caller */
839 if (!atomic_inc_not_zero(&neigh->refcnt)) {
840 /* deleted */
841 neigh = NULL;
842 goto out_unlock;
843 }
844 neigh->alive = jiffies;
845 goto out_unlock;
846 }
847 }
848
849out_unlock:
850 rcu_read_unlock_bh();
851 return neigh;
852}
853
854static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
855{
856 struct ipoib_neigh_table *ntbl = &priv->ntbl;
857 struct ipoib_neigh_hash *htbl;
858 unsigned long neigh_obsolete;
859 unsigned long dt;
860 unsigned long flags;
861 int i;
862
863 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
864 return;
865
866 write_lock_bh(&ntbl->rwlock);
867
868 htbl = rcu_dereference_protected(ntbl->htbl,
869 lockdep_is_held(&ntbl->rwlock));
870
871 if (!htbl)
872 goto out_unlock;
873
874 /* neigh is obsolete if it was idle for two GC periods */
875 dt = 2 * arp_tbl.gc_interval;
876 neigh_obsolete = jiffies - dt;
877 /* handle possible race condition */
878 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
879 goto out_unlock;
880
881 for (i = 0; i < htbl->size; i++) {
882 struct ipoib_neigh *neigh;
883 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
884
885 while ((neigh = rcu_dereference_protected(*np,
886 lockdep_is_held(&ntbl->rwlock))) != NULL) {
887 /* was the neigh idle for two GC periods */
888 if (time_after(neigh_obsolete, neigh->alive)) {
889 rcu_assign_pointer(*np,
890 rcu_dereference_protected(neigh->hnext,
891 lockdep_is_held(&ntbl->rwlock)));
892 /* remove from path/mc list */
893 spin_lock_irqsave(&priv->lock, flags);
894 list_del(&neigh->list);
895 spin_unlock_irqrestore(&priv->lock, flags);
896 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
897 } else {
898 np = &neigh->hnext;
899 }
900
901 }
902 }
903
904out_unlock:
905 write_unlock_bh(&ntbl->rwlock);
906}
907
908static void ipoib_reap_neigh(struct work_struct *work)
909{
910 struct ipoib_dev_priv *priv =
911 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
912
913 __ipoib_reap_neigh(priv);
914
915 if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
916 queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
917 arp_tbl.gc_interval);
918}
919
920
921static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
Moni Shoua732a2172007-10-09 19:43:36 -0700922 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300923{
924 struct ipoib_neigh *neigh;
925
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000926 neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300927 if (!neigh)
928 return NULL;
929
Moni Shoua732a2172007-10-09 19:43:36 -0700930 neigh->dev = dev;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000931 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
Roland Dreier82b39912006-12-12 14:48:18 -0800932 skb_queue_head_init(&neigh->queue);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000933 INIT_LIST_HEAD(&neigh->list);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200934 ipoib_cm_set(neigh, NULL);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000935 /* one ref on behalf of the caller */
936 atomic_set(&neigh->refcnt, 1);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300937
938 return neigh;
939}
940
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000941struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
942 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300943{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +0000944 struct ipoib_dev_priv *priv = netdev_priv(dev);
945 struct ipoib_neigh_table *ntbl = &priv->ntbl;
946 struct ipoib_neigh_hash *htbl;
947 struct ipoib_neigh *neigh;
948 u32 hash_val;
949
950 write_lock_bh(&ntbl->rwlock);
951
952 htbl = rcu_dereference_protected(ntbl->htbl,
953 lockdep_is_held(&ntbl->rwlock));
954 if (!htbl) {
955 neigh = NULL;
956 goto out_unlock;
957 }
958
959 /* need to add a new neigh, but maybe some other thread succeeded?
960 * recalc hash, maybe hash resize took place so we do a search
961 */
962 hash_val = ipoib_addr_hash(htbl, daddr);
963 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
964 lockdep_is_held(&ntbl->rwlock));
965 neigh != NULL;
966 neigh = rcu_dereference_protected(neigh->hnext,
967 lockdep_is_held(&ntbl->rwlock))) {
968 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
969 /* found, take one ref on behalf of the caller */
970 if (!atomic_inc_not_zero(&neigh->refcnt)) {
971 /* deleted */
972 neigh = NULL;
973 break;
974 }
975 neigh->alive = jiffies;
976 goto out_unlock;
977 }
978 }
979
980 neigh = ipoib_neigh_ctor(daddr, dev);
981 if (!neigh)
982 goto out_unlock;
983
984 /* one ref on behalf of the hash table */
985 atomic_inc(&neigh->refcnt);
986 neigh->alive = jiffies;
987 /* put in hash */
988 rcu_assign_pointer(neigh->hnext,
989 rcu_dereference_protected(htbl->buckets[hash_val],
990 lockdep_is_held(&ntbl->rwlock)));
991 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
992 atomic_inc(&ntbl->entries);
993
994out_unlock:
995 write_unlock_bh(&ntbl->rwlock);
996
997 return neigh;
998}
999
1000void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1001{
1002 /* neigh reference count was dropprd to zero */
1003 struct net_device *dev = neigh->dev;
1004 struct ipoib_dev_priv *priv = netdev_priv(dev);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001005 struct sk_buff *skb;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001006 if (neigh->ah)
1007 ipoib_put_ah(neigh->ah);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001008 while ((skb = __skb_dequeue(&neigh->queue))) {
Roland Dreierde903512007-09-28 15:33:51 -07001009 ++dev->stats.tx_dropped;
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +02001010 dev_kfree_skb_any(skb);
1011 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001012 if (ipoib_cm_get(neigh))
1013 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001014 ipoib_dbg(netdev_priv(dev),
1015 "neigh free for %06x %pI6\n",
1016 IPOIB_QPN(neigh->daddr),
1017 neigh->daddr + 4);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001018 kfree(neigh);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001019 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1020 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1021 complete(&priv->ntbl.flushed);
1022 }
Michael S. Tsirkind2e06552006-04-04 19:59:40 +03001023}
1024
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001025static void ipoib_neigh_reclaim(struct rcu_head *rp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001026{
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001027 /* Called as a result of removal from hash table */
1028 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1029 /* note TX context may hold another ref */
1030 ipoib_neigh_put(neigh);
1031}
1032
1033void ipoib_neigh_free(struct ipoib_neigh *neigh)
1034{
1035 struct net_device *dev = neigh->dev;
1036 struct ipoib_dev_priv *priv = netdev_priv(dev);
1037 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1038 struct ipoib_neigh_hash *htbl;
1039 struct ipoib_neigh __rcu **np;
1040 struct ipoib_neigh *n;
1041 u32 hash_val;
1042
1043 write_lock_bh(&ntbl->rwlock);
1044
1045 htbl = rcu_dereference_protected(ntbl->htbl,
1046 lockdep_is_held(&ntbl->rwlock));
1047 if (!htbl)
1048 goto out_unlock;
1049
1050 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1051 np = &htbl->buckets[hash_val];
1052 for (n = rcu_dereference_protected(*np,
1053 lockdep_is_held(&ntbl->rwlock));
1054 n != NULL;
Shlomo Pongratz6c723a62012-08-13 14:39:50 +00001055 n = rcu_dereference_protected(*np,
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001056 lockdep_is_held(&ntbl->rwlock))) {
1057 if (n == neigh) {
1058 /* found */
1059 rcu_assign_pointer(*np,
1060 rcu_dereference_protected(neigh->hnext,
1061 lockdep_is_held(&ntbl->rwlock)));
1062 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1063 goto out_unlock;
1064 } else {
1065 np = &n->hnext;
1066 }
1067 }
1068
1069out_unlock:
1070 write_unlock_bh(&ntbl->rwlock);
1071
1072}
1073
1074static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1075{
1076 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1077 struct ipoib_neigh_hash *htbl;
1078 struct ipoib_neigh **buckets;
1079 u32 size;
1080
1081 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1082 ntbl->htbl = NULL;
1083 rwlock_init(&ntbl->rwlock);
1084 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1085 if (!htbl)
1086 return -ENOMEM;
1087 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1088 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1089 buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1090 if (!buckets) {
1091 kfree(htbl);
1092 return -ENOMEM;
1093 }
1094 htbl->size = size;
1095 htbl->mask = (size - 1);
1096 htbl->buckets = buckets;
1097 ntbl->htbl = htbl;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001098 htbl->ntbl = ntbl;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001099 atomic_set(&ntbl->entries, 0);
1100
1101 /* start garbage collection */
1102 clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1103 queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
1104 arp_tbl.gc_interval);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105
1106 return 0;
1107}
1108
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001109static void neigh_hash_free_rcu(struct rcu_head *head)
1110{
1111 struct ipoib_neigh_hash *htbl = container_of(head,
1112 struct ipoib_neigh_hash,
1113 rcu);
1114 struct ipoib_neigh __rcu **buckets = htbl->buckets;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001115 struct ipoib_neigh_table *ntbl = htbl->ntbl;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001116
1117 kfree(buckets);
1118 kfree(htbl);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001119 complete(&ntbl->deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001120}
1121
1122void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1123{
1124 struct ipoib_dev_priv *priv = netdev_priv(dev);
1125 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1126 struct ipoib_neigh_hash *htbl;
1127 unsigned long flags;
1128 int i;
1129
1130 /* remove all neigh connected to a given path or mcast */
1131 write_lock_bh(&ntbl->rwlock);
1132
1133 htbl = rcu_dereference_protected(ntbl->htbl,
1134 lockdep_is_held(&ntbl->rwlock));
1135
1136 if (!htbl)
1137 goto out_unlock;
1138
1139 for (i = 0; i < htbl->size; i++) {
1140 struct ipoib_neigh *neigh;
1141 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1142
1143 while ((neigh = rcu_dereference_protected(*np,
1144 lockdep_is_held(&ntbl->rwlock))) != NULL) {
1145 /* delete neighs belong to this parent */
1146 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1147 rcu_assign_pointer(*np,
1148 rcu_dereference_protected(neigh->hnext,
1149 lockdep_is_held(&ntbl->rwlock)));
1150 /* remove from parent list */
1151 spin_lock_irqsave(&priv->lock, flags);
1152 list_del(&neigh->list);
1153 spin_unlock_irqrestore(&priv->lock, flags);
1154 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1155 } else {
1156 np = &neigh->hnext;
1157 }
1158
1159 }
1160 }
1161out_unlock:
1162 write_unlock_bh(&ntbl->rwlock);
1163}
1164
1165static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1166{
1167 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1168 struct ipoib_neigh_hash *htbl;
1169 unsigned long flags;
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001170 int i, wait_flushed = 0;
1171
1172 init_completion(&priv->ntbl.flushed);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001173
1174 write_lock_bh(&ntbl->rwlock);
1175
1176 htbl = rcu_dereference_protected(ntbl->htbl,
1177 lockdep_is_held(&ntbl->rwlock));
1178 if (!htbl)
1179 goto out_unlock;
1180
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001181 wait_flushed = atomic_read(&priv->ntbl.entries);
1182 if (!wait_flushed)
1183 goto free_htbl;
1184
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001185 for (i = 0; i < htbl->size; i++) {
1186 struct ipoib_neigh *neigh;
1187 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1188
1189 while ((neigh = rcu_dereference_protected(*np,
1190 lockdep_is_held(&ntbl->rwlock))) != NULL) {
1191 rcu_assign_pointer(*np,
1192 rcu_dereference_protected(neigh->hnext,
1193 lockdep_is_held(&ntbl->rwlock)));
1194 /* remove from path/mc list */
1195 spin_lock_irqsave(&priv->lock, flags);
1196 list_del(&neigh->list);
1197 spin_unlock_irqrestore(&priv->lock, flags);
1198 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1199 }
1200 }
1201
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001202free_htbl:
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001203 rcu_assign_pointer(ntbl->htbl, NULL);
1204 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1205
1206out_unlock:
1207 write_unlock_bh(&ntbl->rwlock);
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001208 if (wait_flushed)
1209 wait_for_completion(&priv->ntbl.flushed);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001210}
1211
1212static void ipoib_neigh_hash_uninit(struct net_device *dev)
1213{
1214 struct ipoib_dev_priv *priv = netdev_priv(dev);
1215 int stopped;
1216
1217 ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001218 init_completion(&priv->ntbl.deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001219 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1220
1221 /* Stop GC if called at init fail need to cancel work */
1222 stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1223 if (!stopped)
1224 cancel_delayed_work(&priv->neigh_reap_task);
1225
Shlomo Pongratz66172c02012-08-29 15:14:33 +00001226 ipoib_flush_neighs(priv);
1227
1228 wait_for_completion(&priv->ntbl.deleted);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001229}
1230
1231
Linus Torvalds1da177e2005-04-16 15:20:36 -07001232int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1233{
1234 struct ipoib_dev_priv *priv = netdev_priv(dev);
1235
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001236 if (ipoib_neigh_hash_init(priv) < 0)
1237 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001238 /* Allocate RX/TX "rings" to hold queued skbs */
Shirley Ma0f485252006-04-10 09:43:58 -07001239 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 GFP_KERNEL);
1241 if (!priv->rx_ring) {
1242 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
Shirley Ma0f485252006-04-10 09:43:58 -07001243 ca->name, ipoib_recvq_size);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001244 goto out_neigh_hash_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001246
Joe Perches948579c2010-11-05 03:07:36 +00001247 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 if (!priv->tx_ring) {
1249 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
Shirley Ma0f485252006-04-10 09:43:58 -07001250 ca->name, ipoib_sendq_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 goto out_rx_ring_cleanup;
1252 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
Michael S. Tsirkin1b524962007-08-16 15:36:16 +03001254 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001255
1256 if (ipoib_ib_dev_init(dev, ca, port))
1257 goto out_tx_ring_cleanup;
1258
1259 return 0;
1260
1261out_tx_ring_cleanup:
Roland Dreier10313cb2008-03-12 07:51:03 -07001262 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263
1264out_rx_ring_cleanup:
1265 kfree(priv->rx_ring);
1266
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001267out_neigh_hash_cleanup:
1268 ipoib_neigh_hash_uninit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001269out:
1270 return -ENOMEM;
1271}
1272
1273void ipoib_dev_cleanup(struct net_device *dev)
1274{
1275 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
1276
Roland Dreier1732b0e2005-11-07 10:33:11 -08001277 ipoib_delete_debug_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001278
1279 /* Delete any child interfaces first */
1280 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001281 /* Stop GC on child */
1282 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1283 cancel_delayed_work(&cpriv->neigh_reap_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 unregister_netdev(cpriv->dev);
1285 ipoib_dev_cleanup(cpriv->dev);
1286 free_netdev(cpriv->dev);
1287 }
1288
1289 ipoib_ib_dev_cleanup(dev);
1290
Hal Rosenstock92a6b342005-08-13 20:50:27 -07001291 kfree(priv->rx_ring);
Roland Dreier10313cb2008-03-12 07:51:03 -07001292 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293
Hal Rosenstock92a6b342005-08-13 20:50:27 -07001294 priv->rx_ring = NULL;
1295 priv->tx_ring = NULL;
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001296
1297 ipoib_neigh_hash_uninit(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298}
1299
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -07001300static const struct header_ops ipoib_header_ops = {
1301 .create = ipoib_hard_header,
1302};
1303
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001304static const struct net_device_ops ipoib_netdev_ops = {
1305 .ndo_open = ipoib_open,
1306 .ndo_stop = ipoib_stop,
1307 .ndo_change_mtu = ipoib_change_mtu,
Michał Mirosław3d96c742011-04-19 00:43:20 +00001308 .ndo_fix_features = ipoib_fix_features,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001309 .ndo_start_xmit = ipoib_start_xmit,
1310 .ndo_tx_timeout = ipoib_timeout,
Jiri Pirkoafc4b132011-08-16 06:29:01 +00001311 .ndo_set_rx_mode = ipoib_set_mcast_list,
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001312};
1313
Linus Torvalds1da177e2005-04-16 15:20:36 -07001314static void ipoib_setup(struct net_device *dev)
1315{
1316 struct ipoib_dev_priv *priv = netdev_priv(dev);
1317
Stephen Hemmingerfe8114e2009-03-20 19:35:32 +00001318 dev->netdev_ops = &ipoib_netdev_ops;
Roland Dreier2337f802007-10-23 19:57:54 -07001319 dev->header_ops = &ipoib_header_ops;
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001320
Eli Cohen82c24c12008-04-16 21:09:32 -07001321 ipoib_set_ethtool_ops(dev);
1322
Stephen Hemmingerbea33482007-10-03 16:41:36 -07001323 netif_napi_add(dev, &priv->napi, ipoib_poll, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001324
Roland Dreier2337f802007-10-23 19:57:54 -07001325 dev->watchdog_timeo = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001326
Roland Dreier2337f802007-10-23 19:57:54 -07001327 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001328
Roland Dreier936d7de2012-02-07 14:51:21 +00001329 dev->hard_header_len = IPOIB_ENCAP_LEN;
Roland Dreier2337f802007-10-23 19:57:54 -07001330 dev->addr_len = INFINIBAND_ALEN;
1331 dev->type = ARPHRD_INFINIBAND;
1332 dev->tx_queue_len = ipoib_sendq_size * 2;
Eli Coheneb14032f2008-01-30 18:30:46 +02001333 dev->features = (NETIF_F_VLAN_CHALLENGED |
Eli Coheneb14032f2008-01-30 18:30:46 +02001334 NETIF_F_HIGHDMA);
Eric Dumazet86d15cd2009-05-30 23:04:46 -07001335 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
1338
1339 netif_carrier_off(dev);
1340
Linus Torvalds1da177e2005-04-16 15:20:36 -07001341 priv->dev = dev;
1342
1343 spin_lock_init(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344
Ingo Molnar95ed6442006-01-13 14:51:39 -08001345 mutex_init(&priv->vlan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001346
1347 INIT_LIST_HEAD(&priv->path_list);
1348 INIT_LIST_HEAD(&priv->child_intfs);
1349 INIT_LIST_HEAD(&priv->dead_ahs);
1350 INIT_LIST_HEAD(&priv->multicast_list);
1351
Yosef Etigin26bbf132007-05-19 08:51:54 -07001352 INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
David Howellsc4028952006-11-22 14:57:56 +00001353 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
Yossi Etigine8224e42008-09-16 11:57:45 -07001354 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
Moni Shouaee1e2c82008-07-14 23:48:49 -07001355 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
1356 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
1357 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
David Howellsc4028952006-11-22 14:57:56 +00001358 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1359 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001360 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361}
1362
1363struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
1364{
1365 struct net_device *dev;
1366
1367 dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
1368 ipoib_setup);
1369 if (!dev)
1370 return NULL;
1371
1372 return netdev_priv(dev);
1373}
1374
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001375static ssize_t show_pkey(struct device *dev,
1376 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001377{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001378 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379
1380 return sprintf(buf, "0x%04x\n", priv->pkey);
1381}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001382static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001383
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001384static ssize_t show_umcast(struct device *dev,
1385 struct device_attribute *attr, char *buf)
1386{
1387 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1388
1389 return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1390}
1391
1392static ssize_t set_umcast(struct device *dev,
1393 struct device_attribute *attr,
1394 const char *buf, size_t count)
1395{
1396 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1397 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1398
1399 if (umcast_val > 0) {
1400 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1401 ipoib_warn(priv, "ignoring multicast groups joined directly "
1402 "by userspace\n");
1403 } else
1404 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1405
1406 return count;
1407}
1408static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1409
1410int ipoib_add_umcast_attr(struct net_device *dev)
1411{
1412 return device_create_file(&dev->dev, &dev_attr_umcast);
1413}
1414
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001415static ssize_t create_child(struct device *dev,
1416 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 const char *buf, size_t count)
1418{
1419 int pkey;
1420 int ret;
1421
1422 if (sscanf(buf, "%i", &pkey) != 1)
1423 return -EINVAL;
1424
1425 if (pkey < 0 || pkey > 0xffff)
1426 return -EINVAL;
1427
Roland Dreier4ce05932005-08-19 12:03:17 -07001428 /*
1429 * Set the full membership bit, so that we join the right
1430 * broadcast group, etc.
1431 */
1432 pkey |= 0x8000;
1433
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001434 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001435
1436 return ret ? ret : count;
1437}
Or Gerlitz7a52b342010-06-06 04:59:16 +00001438static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001440static ssize_t delete_child(struct device *dev,
1441 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001442 const char *buf, size_t count)
1443{
1444 int pkey;
1445 int ret;
1446
1447 if (sscanf(buf, "%i", &pkey) != 1)
1448 return -EINVAL;
1449
1450 if (pkey < 0 || pkey > 0xffff)
1451 return -EINVAL;
1452
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001453 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001454
1455 return ret ? ret : count;
1456
1457}
Or Gerlitz7a52b342010-06-06 04:59:16 +00001458static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
1460int ipoib_add_pkey_attr(struct net_device *dev)
1461{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001462 return device_create_file(&dev->dev, &dev_attr_pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463}
1464
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001465int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
1466{
1467 struct ib_device_attr *device_attr;
1468 int result = -ENOMEM;
1469
1470 device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
1471 if (!device_attr) {
1472 printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
1473 hca->name, sizeof *device_attr);
1474 return result;
1475 }
1476
1477 result = ib_query_device(hca, device_attr);
1478 if (result) {
1479 printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
1480 hca->name, result);
1481 kfree(device_attr);
1482 return result;
1483 }
1484 priv->hca_caps = device_attr->device_cap_flags;
1485
1486 kfree(device_attr);
1487
1488 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
Michał Mirosław3d96c742011-04-19 00:43:20 +00001489 priv->dev->hw_features = NETIF_F_SG |
1490 NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1491
1492 if (priv->hca_caps & IB_DEVICE_UD_TSO)
1493 priv->dev->hw_features |= NETIF_F_TSO;
1494
1495 priv->dev->features |= priv->dev->hw_features;
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001496 }
1497
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001498 return 0;
1499}
1500
Linus Torvalds1da177e2005-04-16 15:20:36 -07001501static struct net_device *ipoib_add_port(const char *format,
1502 struct ib_device *hca, u8 port)
1503{
1504 struct ipoib_dev_priv *priv;
Shirley Mabc7b3a32008-04-23 11:55:45 -07001505 struct ib_port_attr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001506 int result = -ENOMEM;
1507
1508 priv = ipoib_intf_alloc(format);
1509 if (!priv)
1510 goto alloc_mem_failed;
1511
1512 SET_NETDEV_DEV(priv->dev, hca->dma_device);
Eli Cohenc3aa9b12010-09-20 07:05:06 +00001513 priv->dev->dev_id = port - 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
Shirley Mabc7b3a32008-04-23 11:55:45 -07001515 if (!ib_query_port(hca, port, &attr))
1516 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1517 else {
1518 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
1519 hca->name, port);
1520 goto device_init_failed;
1521 }
1522
1523 /* MTU will be reset when mcast join happens */
1524 priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
1525 priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
1526
David Miller596b9b62011-07-25 00:01:25 +00001527 priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
1528
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 result = ib_query_pkey(hca, port, 0, &priv->pkey);
1530 if (result) {
1531 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1532 hca->name, port, result);
Eli Cohenca6de172007-08-21 18:46:10 +03001533 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 }
1535
Or Gerlitz83bb63f2008-10-22 15:49:49 -07001536 if (ipoib_set_dev_features(priv, hca))
Eli Cohen60461362008-04-16 21:01:10 -07001537 goto device_init_failed;
Vladimir Sokolovskyaf40da82008-07-14 23:48:48 -07001538
Roland Dreier4ce05932005-08-19 12:03:17 -07001539 /*
1540 * Set the full membership bit, so that we join the right
1541 * broadcast group, etc.
1542 */
1543 priv->pkey |= 0x8000;
1544
Linus Torvalds1da177e2005-04-16 15:20:36 -07001545 priv->dev->broadcast[8] = priv->pkey >> 8;
1546 priv->dev->broadcast[9] = priv->pkey & 0xff;
1547
1548 result = ib_query_gid(hca, port, 0, &priv->local_gid);
1549 if (result) {
1550 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1551 hca->name, port, result);
Eli Cohenca6de172007-08-21 18:46:10 +03001552 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553 } else
1554 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1555
Linus Torvalds1da177e2005-04-16 15:20:36 -07001556 result = ipoib_dev_init(priv->dev, hca, port);
1557 if (result < 0) {
1558 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1559 hca->name, port, result);
1560 goto device_init_failed;
1561 }
1562
1563 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1564 priv->ca, ipoib_event);
1565 result = ib_register_event_handler(&priv->event_handler);
1566 if (result < 0) {
1567 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1568 "port %d (ret = %d)\n",
1569 hca->name, port, result);
1570 goto event_failed;
1571 }
1572
1573 result = register_netdev(priv->dev);
1574 if (result) {
1575 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1576 hca->name, port, result);
1577 goto register_failed;
1578 }
1579
Roland Dreier1732b0e2005-11-07 10:33:11 -08001580 ipoib_create_debug_files(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001581
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001582 if (ipoib_cm_add_mode_attr(priv->dev))
1583 goto sysfs_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001584 if (ipoib_add_pkey_attr(priv->dev))
1585 goto sysfs_failed;
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001586 if (ipoib_add_umcast_attr(priv->dev))
1587 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001588 if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001589 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001590 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 goto sysfs_failed;
1592
1593 return priv->dev;
1594
1595sysfs_failed:
Roland Dreier1732b0e2005-11-07 10:33:11 -08001596 ipoib_delete_debug_files(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 unregister_netdev(priv->dev);
1598
1599register_failed:
1600 ib_unregister_event_handler(&priv->event_handler);
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001601 /* Stop GC if started before flush */
1602 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1603 cancel_delayed_work(&priv->neigh_reap_task);
Roland Dreiera77a57a2008-08-19 15:01:32 -07001604 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605
1606event_failed:
1607 ipoib_dev_cleanup(priv->dev);
1608
1609device_init_failed:
1610 free_netdev(priv->dev);
1611
1612alloc_mem_failed:
1613 return ERR_PTR(result);
1614}
1615
1616static void ipoib_add_one(struct ib_device *device)
1617{
1618 struct list_head *dev_list;
1619 struct net_device *dev;
1620 struct ipoib_dev_priv *priv;
1621 int s, e, p;
1622
Tom Tucker07ebafb2006-08-03 16:02:42 -05001623 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1624 return;
1625
Linus Torvalds1da177e2005-04-16 15:20:36 -07001626 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1627 if (!dev_list)
1628 return;
1629
1630 INIT_LIST_HEAD(dev_list);
1631
Tom Tucker07ebafb2006-08-03 16:02:42 -05001632 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001633 s = 0;
1634 e = 0;
1635 } else {
1636 s = 1;
1637 e = device->phys_port_cnt;
1638 }
1639
1640 for (p = s; p <= e; ++p) {
Eli Cohen7b4c8762010-09-27 17:51:11 -07001641 if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
1642 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 dev = ipoib_add_port("ib%d", device, p);
1644 if (!IS_ERR(dev)) {
1645 priv = netdev_priv(dev);
1646 list_add_tail(&priv->list, dev_list);
1647 }
1648 }
1649
1650 ib_set_client_data(device, &ipoib_client, dev_list);
1651}
1652
1653static void ipoib_remove_one(struct ib_device *device)
1654{
1655 struct ipoib_dev_priv *priv, *tmp;
1656 struct list_head *dev_list;
1657
Tom Tucker07ebafb2006-08-03 16:02:42 -05001658 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1659 return;
1660
Linus Torvalds1da177e2005-04-16 15:20:36 -07001661 dev_list = ib_get_client_data(device, &ipoib_client);
1662
1663 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1664 ib_unregister_event_handler(&priv->event_handler);
Roland Dreiera77a57a2008-08-19 15:01:32 -07001665
1666 rtnl_lock();
1667 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
1668 rtnl_unlock();
1669
Shlomo Pongratzb63b70d2012-07-24 17:05:22 +00001670 /* Stop GC */
1671 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1672 cancel_delayed_work(&priv->neigh_reap_task);
Roland Dreiera77a57a2008-08-19 15:01:32 -07001673 flush_workqueue(ipoib_workqueue);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001674
1675 unregister_netdev(priv->dev);
1676 ipoib_dev_cleanup(priv->dev);
1677 free_netdev(priv->dev);
1678 }
Michael S. Tsirkin06c56e42005-09-01 09:19:02 -07001679
1680 kfree(dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
1683static int __init ipoib_init_module(void)
1684{
1685 int ret;
1686
Shirley Ma0f485252006-04-10 09:43:58 -07001687 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1688 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1689 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1690
1691 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1692 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
Hagen Paul Pfeifer732eacc2010-10-26 14:22:23 -07001693 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001694#ifdef CONFIG_INFINIBAND_IPOIB_CM
1695 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1696#endif
Shirley Ma0f485252006-04-10 09:43:58 -07001697
Eli Cohenf89271da2008-07-14 23:48:44 -07001698 /*
1699 * When copying small received packets, we only copy from the
1700 * linear data part of the SKB, so we rely on this condition.
1701 */
1702 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
1703
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 ret = ipoib_register_debugfs();
1705 if (ret)
1706 return ret;
1707
1708 /*
1709 * We create our own workqueue mainly because we want to be
1710 * able to flush it when devices are being removed. We can't
1711 * use schedule_work()/flush_scheduled_work() because both
1712 * unregister_netdev() and linkwatch_event take the rtnl lock,
1713 * so flush_scheduled_work() can deadlock during device
1714 * removal.
1715 */
1716 ipoib_workqueue = create_singlethread_workqueue("ipoib");
1717 if (!ipoib_workqueue) {
1718 ret = -ENOMEM;
1719 goto err_fs;
1720 }
1721
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001722 ib_sa_register_client(&ipoib_sa_client);
1723
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 ret = ib_register_client(&ipoib_client);
1725 if (ret)
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001726 goto err_sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001727
1728 return 0;
1729
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001730err_sa:
1731 ib_sa_unregister_client(&ipoib_sa_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 destroy_workqueue(ipoib_workqueue);
1733
Roland Dreier9adec1a2005-04-16 15:26:07 -07001734err_fs:
1735 ipoib_unregister_debugfs();
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737 return ret;
1738}
1739
1740static void __exit ipoib_cleanup_module(void)
1741{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 ib_unregister_client(&ipoib_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001743 ib_sa_unregister_client(&ipoib_sa_client);
Roland Dreier9adec1a2005-04-16 15:26:07 -07001744 ipoib_unregister_debugfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 destroy_workqueue(ipoib_workqueue);
1746}
1747
1748module_init(ipoib_init_module);
1749module_exit(ipoib_cleanup_module);