IB/ipoib: Scatter-Gather support in connected mode
[deliverable/linux.git] / drivers / infiniband / ulp / ipoib / ipoib_main.c
CommitLineData
1da177e4
LT
1/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
2a1d9b7f
RD
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
1da177e4
LT
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
1da177e4
LT
33 */
34
35#include "ipoib.h"
36
1da177e4
LT
37#include <linux/module.h>
38
39#include <linux/init.h>
40#include <linux/slab.h>
0f485251 41#include <linux/kernel.h>
10313cbb 42#include <linux/vmalloc.h>
1da177e4
LT
43
44#include <linux/if_arp.h> /* For ARPHRD_xxx */
45
46#include <linux/ip.h>
47#include <linux/in.h>
48
b63b70d8
SP
49#include <linux/jhash.h>
50#include <net/arp.h>
14c85021 51
4b48680b
YB
52#define DRV_VERSION "1.0.0"
53
54const char ipoib_driver_version[] = DRV_VERSION;
55
1da177e4
LT
56MODULE_AUTHOR("Roland Dreier");
57MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
58MODULE_LICENSE("Dual BSD/GPL");
4b48680b 59MODULE_VERSION(DRV_VERSION);
1da177e4 60
0f485251
SM
61int 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
1da177e4
LT
69#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
1732b0ef
RD
76struct ipoib_path_iter {
77 struct net_device *dev;
78 struct ipoib_path path;
79};
80
1da177e4
LT
81static 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
c1a0b23b
MT
89struct ib_sa_client ipoib_sa_client;
90
1da177e4
LT
91static void ipoib_add_one(struct ib_device *device);
92static void ipoib_remove_one(struct ib_device *device);
b63b70d8 93static void ipoib_neigh_reclaim(struct rcu_head *rp);
1da177e4
LT
94
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
437708c4
MS
107 netif_carrier_off(dev);
108
e028cc55 109 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
1da177e4 110
efc82eee 111 if (ipoib_ib_dev_open(dev)) {
dd57c930
AE
112 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
113 return 0;
b8a1b1ce 114 goto err_disable;
dd57c930 115 }
fe25c561 116
b8a1b1ce
RD
117 if (ipoib_ib_dev_up(dev))
118 goto err_stop;
1da177e4
LT
119
120 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
121 struct ipoib_dev_priv *cpriv;
122
123 /* Bring up any child interfaces too */
f47944cc 124 down_read(&priv->vlan_rwsem);
1da177e4
LT
125 list_for_each_entry(cpriv, &priv->child_intfs, list) {
126 int flags;
127
128 flags = cpriv->dev->flags;
129 if (flags & IFF_UP)
130 continue;
131
132 dev_change_flags(cpriv->dev, flags | IFF_UP);
133 }
f47944cc 134 up_read(&priv->vlan_rwsem);
1da177e4
LT
135 }
136
137 netif_start_queue(dev);
138
139 return 0;
b8a1b1ce
RD
140
141err_stop:
efc82eee 142 ipoib_ib_dev_stop(dev);
b8a1b1ce
RD
143
144err_disable:
b8a1b1ce
RD
145 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
146
147 return -EINVAL;
1da177e4
LT
148}
149
150static int ipoib_stop(struct net_device *dev)
151{
152 struct ipoib_dev_priv *priv = netdev_priv(dev);
153
154 ipoib_dbg(priv, "stopping interface\n");
155
156 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
157
158 netif_stop_queue(dev);
159
efc82eee
DL
160 ipoib_ib_dev_down(dev);
161 ipoib_ib_dev_stop(dev);
1da177e4
LT
162
163 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
164 struct ipoib_dev_priv *cpriv;
165
166 /* Bring down any child interfaces too */
f47944cc 167 down_read(&priv->vlan_rwsem);
1da177e4
LT
168 list_for_each_entry(cpriv, &priv->child_intfs, list) {
169 int flags;
170
171 flags = cpriv->dev->flags;
172 if (!(flags & IFF_UP))
173 continue;
174
175 dev_change_flags(cpriv->dev, flags & ~IFF_UP);
176 }
f47944cc 177 up_read(&priv->vlan_rwsem);
1da177e4
LT
178 }
179
180 return 0;
181}
182
9baa0b03
OG
183static void ipoib_uninit(struct net_device *dev)
184{
185 ipoib_dev_cleanup(dev);
186}
187
9ca36f7d 188static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
3d96c74d
MM
189{
190 struct ipoib_dev_priv *priv = netdev_priv(dev);
191
192 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
c4268778 193 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
3d96c74d
MM
194
195 return features;
196}
197
1da177e4
LT
198static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
199{
200 struct ipoib_dev_priv *priv = netdev_priv(dev);
201
839fcaba 202 /* dev->mtu > 2K ==> connected mode */
586a6934
PS
203 if (ipoib_cm_admin_enabled(dev)) {
204 if (new_mtu > ipoib_cm_max_mtu(dev))
205 return -EINVAL;
206
839fcaba
MT
207 if (new_mtu > priv->mcast_mtu)
208 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
209 priv->mcast_mtu);
586a6934 210
839fcaba
MT
211 dev->mtu = new_mtu;
212 return 0;
213 }
214
bc7b3a36 215 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
1da177e4
LT
216 return -EINVAL;
217
218 priv->admin_mtu = new_mtu;
219
220 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
221
222 return 0;
223}
224
71d9c5f9
RD
225int ipoib_set_mode(struct net_device *dev, const char *buf)
226{
227 struct ipoib_dev_priv *priv = netdev_priv(dev);
228
229 /* flush paths if we switch modes so that connections are restarted */
230 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
231 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
232 ipoib_warn(priv, "enabling connected mode "
233 "will cause multicast packet drops\n");
234 netdev_update_features(dev);
235 rtnl_unlock();
236 priv->tx_wr.send_flags &= ~IB_SEND_IP_CSUM;
237
238 ipoib_flush_paths(dev);
239 rtnl_lock();
240 return 0;
241 }
242
243 if (!strcmp(buf, "datagram\n")) {
244 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
245 netdev_update_features(dev);
246 dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
247 rtnl_unlock();
248 ipoib_flush_paths(dev);
249 rtnl_lock();
250 return 0;
251 }
252
253 return -EINVAL;
254}
255
37c22a77 256static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
1da177e4
LT
257{
258 struct ipoib_dev_priv *priv = netdev_priv(dev);
259 struct rb_node *n = priv->path_tree.rb_node;
260 struct ipoib_path *path;
261 int ret;
262
263 while (n) {
264 path = rb_entry(n, struct ipoib_path, rb_node);
265
37c22a77 266 ret = memcmp(gid, path->pathrec.dgid.raw,
1da177e4
LT
267 sizeof (union ib_gid));
268
269 if (ret < 0)
270 n = n->rb_left;
271 else if (ret > 0)
272 n = n->rb_right;
273 else
274 return path;
275 }
276
277 return NULL;
278}
279
280static int __path_add(struct net_device *dev, struct ipoib_path *path)
281{
282 struct ipoib_dev_priv *priv = netdev_priv(dev);
283 struct rb_node **n = &priv->path_tree.rb_node;
284 struct rb_node *pn = NULL;
285 struct ipoib_path *tpath;
286 int ret;
287
288 while (*n) {
289 pn = *n;
290 tpath = rb_entry(pn, struct ipoib_path, rb_node);
291
292 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
293 sizeof (union ib_gid));
294 if (ret < 0)
295 n = &pn->rb_left;
296 else if (ret > 0)
297 n = &pn->rb_right;
298 else
299 return -EEXIST;
300 }
301
302 rb_link_node(&path->rb_node, pn, n);
303 rb_insert_color(&path->rb_node, &priv->path_tree);
304
305 list_add_tail(&path->list, &priv->path_list);
306
307 return 0;
308}
309
310static void path_free(struct net_device *dev, struct ipoib_path *path)
311{
1da177e4 312 struct sk_buff *skb;
1da177e4
LT
313
314 while ((skb = __skb_dequeue(&path->queue)))
315 dev_kfree_skb_irq(skb);
316
b63b70d8 317 ipoib_dbg(netdev_priv(dev), "path_free\n");
1da177e4 318
b63b70d8
SP
319 /* remove all neigh connected to this path */
320 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
1da177e4
LT
321
322 if (path->ah)
323 ipoib_put_ah(path->ah);
324
325 kfree(path);
326}
327
1732b0ef
RD
328#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
329
330struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
331{
332 struct ipoib_path_iter *iter;
333
334 iter = kmalloc(sizeof *iter, GFP_KERNEL);
335 if (!iter)
336 return NULL;
337
338 iter->dev = dev;
339 memset(iter->path.pathrec.dgid.raw, 0, 16);
340
341 if (ipoib_path_iter_next(iter)) {
342 kfree(iter);
343 return NULL;
344 }
345
346 return iter;
347}
348
349int ipoib_path_iter_next(struct ipoib_path_iter *iter)
350{
351 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
352 struct rb_node *n;
353 struct ipoib_path *path;
354 int ret = 1;
355
356 spin_lock_irq(&priv->lock);
357
358 n = rb_first(&priv->path_tree);
359
360 while (n) {
361 path = rb_entry(n, struct ipoib_path, rb_node);
362
363 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
364 sizeof (union ib_gid)) < 0) {
365 iter->path = *path;
366 ret = 0;
367 break;
368 }
369
370 n = rb_next(n);
371 }
372
373 spin_unlock_irq(&priv->lock);
374
375 return ret;
376}
377
378void ipoib_path_iter_read(struct ipoib_path_iter *iter,
379 struct ipoib_path *path)
380{
381 *path = iter->path;
382}
383
384#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
385
ee1e2c82
MS
386void ipoib_mark_paths_invalid(struct net_device *dev)
387{
388 struct ipoib_dev_priv *priv = netdev_priv(dev);
389 struct ipoib_path *path, *tp;
390
391 spin_lock_irq(&priv->lock);
392
393 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
5b095d98 394 ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
ee1e2c82 395 be16_to_cpu(path->pathrec.dlid),
fcace2fe 396 path->pathrec.dgid.raw);
ee1e2c82
MS
397 path->valid = 0;
398 }
399
400 spin_unlock_irq(&priv->lock);
401}
402
1da177e4
LT
403void ipoib_flush_paths(struct net_device *dev)
404{
405 struct ipoib_dev_priv *priv = netdev_priv(dev);
406 struct ipoib_path *path, *tp;
407 LIST_HEAD(remove_list);
943c246e 408 unsigned long flags;
1da177e4 409
943c246e
RD
410 netif_tx_lock_bh(dev);
411 spin_lock_irqsave(&priv->lock, flags);
1da177e4 412
157de229 413 list_splice_init(&priv->path_list, &remove_list);
1da177e4
LT
414
415 list_for_each_entry(path, &remove_list, list)
416 rb_erase(&path->rb_node, &priv->path_tree);
417
1da177e4
LT
418 list_for_each_entry_safe(path, tp, &remove_list, list) {
419 if (path->query)
420 ib_sa_cancel_query(path->query_id, path->query);
943c246e
RD
421 spin_unlock_irqrestore(&priv->lock, flags);
422 netif_tx_unlock_bh(dev);
1da177e4
LT
423 wait_for_completion(&path->done);
424 path_free(dev, path);
943c246e
RD
425 netif_tx_lock_bh(dev);
426 spin_lock_irqsave(&priv->lock, flags);
1da177e4 427 }
943c246e
RD
428
429 spin_unlock_irqrestore(&priv->lock, flags);
430 netif_tx_unlock_bh(dev);
1da177e4
LT
431}
432
433static void path_rec_completion(int status,
434 struct ib_sa_path_rec *pathrec,
435 void *path_ptr)
436{
437 struct ipoib_path *path = path_ptr;
438 struct net_device *dev = path->dev;
439 struct ipoib_dev_priv *priv = netdev_priv(dev);
440 struct ipoib_ah *ah = NULL;
c9da4bad 441 struct ipoib_ah *old_ah = NULL;
d04d01b1 442 struct ipoib_neigh *neigh, *tn;
1da177e4
LT
443 struct sk_buff_head skqueue;
444 struct sk_buff *skb;
445 unsigned long flags;
446
843613b0 447 if (!status)
5b095d98 448 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
fcace2fe 449 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
1da177e4 450 else
5b095d98 451 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
fcace2fe 452 status, path->pathrec.dgid.raw);
1da177e4
LT
453
454 skb_queue_head_init(&skqueue);
455
456 if (!status) {
46f1b3d7
SH
457 struct ib_ah_attr av;
458
459 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
460 ah = ipoib_create_ah(dev, priv->pd, &av);
1da177e4
LT
461 }
462
463 spin_lock_irqsave(&priv->lock, flags);
464
3874397c 465 if (!IS_ERR_OR_NULL(ah)) {
1da177e4
LT
466 path->pathrec = *pathrec;
467
c9da4bad
RD
468 old_ah = path->ah;
469 path->ah = ah;
470
1da177e4
LT
471 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
472 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
473
474 while ((skb = __skb_dequeue(&path->queue)))
475 __skb_queue_tail(&skqueue, skb);
476
d04d01b1 477 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
ee1e2c82
MS
478 if (neigh->ah) {
479 WARN_ON(neigh->ah != old_ah);
480 /*
481 * Dropping the ah reference inside
482 * priv->lock is safe here, because we
483 * will hold one more reference from
484 * the original value of path->ah (ie
485 * old_ah).
486 */
487 ipoib_put_ah(neigh->ah);
488 }
1da177e4
LT
489 kref_get(&path->ah->ref);
490 neigh->ah = path->ah;
491
b63b70d8 492 if (ipoib_cm_enabled(dev, neigh->daddr)) {
839fcaba
MT
493 if (!ipoib_cm_get(neigh))
494 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
495 path,
496 neigh));
497 if (!ipoib_cm_get(neigh)) {
b63b70d8 498 ipoib_neigh_free(neigh);
839fcaba
MT
499 continue;
500 }
501 }
502
1da177e4
LT
503 while ((skb = __skb_dequeue(&neigh->queue)))
504 __skb_queue_tail(&skqueue, skb);
505 }
ee1e2c82 506 path->valid = 1;
5872a9fc 507 }
1da177e4 508
5872a9fc 509 path->query = NULL;
1da177e4
LT
510 complete(&path->done);
511
512 spin_unlock_irqrestore(&priv->lock, flags);
513
f72dd566
RD
514 if (IS_ERR_OR_NULL(ah))
515 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
516
ee1e2c82
MS
517 if (old_ah)
518 ipoib_put_ah(old_ah);
519
1da177e4
LT
520 while ((skb = __skb_dequeue(&skqueue))) {
521 skb->dev = dev;
522 if (dev_queue_xmit(skb))
523 ipoib_warn(priv, "dev_queue_xmit failed "
524 "to requeue packet\n");
525 }
526}
527
37c22a77 528static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
1da177e4
LT
529{
530 struct ipoib_dev_priv *priv = netdev_priv(dev);
531 struct ipoib_path *path;
532
1401b53a
JM
533 if (!priv->broadcast)
534 return NULL;
535
21a38489 536 path = kzalloc(sizeof *path, GFP_ATOMIC);
1da177e4
LT
537 if (!path)
538 return NULL;
539
21a38489 540 path->dev = dev;
1da177e4
LT
541
542 skb_queue_head_init(&path->queue);
543
544 INIT_LIST_HEAD(&path->neigh_list);
1da177e4 545
37c22a77 546 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
2337f809
RD
547 path->pathrec.sgid = priv->local_gid;
548 path->pathrec.pkey = cpu_to_be16(priv->pkey);
81668838
SH
549 path->pathrec.numb_path = 1;
550 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
1da177e4
LT
551
552 return path;
553}
554
555static int path_rec_start(struct net_device *dev,
556 struct ipoib_path *path)
557{
558 struct ipoib_dev_priv *priv = netdev_priv(dev);
559
5b095d98 560 ipoib_dbg(priv, "Start path record lookup for %pI6\n",
fcace2fe 561 path->pathrec.dgid.raw);
1da177e4 562
65c7edda
RD
563 init_completion(&path->done);
564
1da177e4 565 path->query_id =
c1a0b23b 566 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
1da177e4
LT
567 &path->pathrec,
568 IB_SA_PATH_REC_DGID |
569 IB_SA_PATH_REC_SGID |
570 IB_SA_PATH_REC_NUMB_PATH |
81668838 571 IB_SA_PATH_REC_TRAFFIC_CLASS |
1da177e4
LT
572 IB_SA_PATH_REC_PKEY,
573 1000, GFP_ATOMIC,
574 path_rec_completion,
575 path, &path->query);
576 if (path->query_id < 0) {
01b3fc8b 577 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
1da177e4 578 path->query = NULL;
93a3ab93 579 complete(&path->done);
1da177e4
LT
580 return path->query_id;
581 }
582
583 return 0;
584}
585
b63b70d8
SP
586static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
587 struct net_device *dev)
1da177e4
LT
588{
589 struct ipoib_dev_priv *priv = netdev_priv(dev);
590 struct ipoib_path *path;
591 struct ipoib_neigh *neigh;
943c246e 592 unsigned long flags;
1da177e4 593
b5120a6e 594 spin_lock_irqsave(&priv->lock, flags);
b63b70d8 595 neigh = ipoib_neigh_alloc(daddr, dev);
1da177e4 596 if (!neigh) {
b5120a6e 597 spin_unlock_irqrestore(&priv->lock, flags);
de903512 598 ++dev->stats.tx_dropped;
1da177e4
LT
599 dev_kfree_skb_any(skb);
600 return;
601 }
602
b63b70d8 603 path = __path_find(dev, daddr + 4);
1da177e4 604 if (!path) {
b63b70d8 605 path = path_rec_create(dev, daddr + 4);
1da177e4 606 if (!path)
d2e0655e 607 goto err_path;
1da177e4
LT
608
609 __path_add(dev, path);
610 }
611
612 list_add_tail(&neigh->list, &path->neigh_list);
613
47f7a071 614 if (path->ah) {
1da177e4
LT
615 kref_get(&path->ah->ref);
616 neigh->ah = path->ah;
617
b63b70d8 618 if (ipoib_cm_enabled(dev, neigh->daddr)) {
839fcaba
MT
619 if (!ipoib_cm_get(neigh))
620 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
621 if (!ipoib_cm_get(neigh)) {
b63b70d8 622 ipoib_neigh_free(neigh);
839fcaba
MT
623 goto err_drop;
624 }
625 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
626 __skb_queue_tail(&neigh->queue, skb);
627 else {
628 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
629 skb_queue_len(&neigh->queue));
630 goto err_drop;
631 }
721d67cd
RD
632 } else {
633 spin_unlock_irqrestore(&priv->lock, flags);
b63b70d8
SP
634 ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
635 ipoib_neigh_put(neigh);
721d67cd
RD
636 return;
637 }
1da177e4
LT
638 } else {
639 neigh->ah = NULL;
1da177e4
LT
640
641 if (!path->query && path_rec_start(dev, path))
49b8e744 642 goto err_path;
1e85b806
ES
643 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
644 __skb_queue_tail(&neigh->queue, skb);
645 else
646 goto err_drop;
1da177e4
LT
647 }
648
943c246e 649 spin_unlock_irqrestore(&priv->lock, flags);
b63b70d8 650 ipoib_neigh_put(neigh);
1da177e4
LT
651 return;
652
d2e0655e 653err_path:
b63b70d8 654 ipoib_neigh_free(neigh);
839fcaba 655err_drop:
de903512 656 ++dev->stats.tx_dropped;
1da177e4
LT
657 dev_kfree_skb_any(skb);
658
943c246e 659 spin_unlock_irqrestore(&priv->lock, flags);
b63b70d8 660 ipoib_neigh_put(neigh);
1da177e4
LT
661}
662
663static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
936d7de3 664 struct ipoib_cb *cb)
1da177e4
LT
665{
666 struct ipoib_dev_priv *priv = netdev_priv(dev);
667 struct ipoib_path *path;
943c246e 668 unsigned long flags;
1da177e4 669
943c246e 670 spin_lock_irqsave(&priv->lock, flags);
1da177e4 671
936d7de3 672 path = __path_find(dev, cb->hwaddr + 4);
ee1e2c82 673 if (!path || !path->valid) {
71d98b46
JM
674 int new_path = 0;
675
676 if (!path) {
936d7de3 677 path = path_rec_create(dev, cb->hwaddr + 4);
71d98b46
JM
678 new_path = 1;
679 }
1da177e4 680 if (path) {
1e85b806
ES
681 if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
682 __skb_queue_tail(&path->queue, skb);
683 } else {
684 ++dev->stats.tx_dropped;
685 dev_kfree_skb_any(skb);
686 }
1da177e4 687
ff79ae80 688 if (!path->query && path_rec_start(dev, path)) {
943c246e 689 spin_unlock_irqrestore(&priv->lock, flags);
71d98b46
JM
690 if (new_path)
691 path_free(dev, path);
1da177e4
LT
692 return;
693 } else
694 __path_add(dev, path);
695 } else {
de903512 696 ++dev->stats.tx_dropped;
1da177e4
LT
697 dev_kfree_skb_any(skb);
698 }
699
943c246e 700 spin_unlock_irqrestore(&priv->lock, flags);
1da177e4
LT
701 return;
702 }
703
47f7a071 704 if (path->ah) {
1da177e4
LT
705 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
706 be16_to_cpu(path->pathrec.dlid));
707
721d67cd 708 spin_unlock_irqrestore(&priv->lock, flags);
936d7de3 709 ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr));
721d67cd 710 return;
1da177e4
LT
711 } else if ((path->query || !path_rec_start(dev, path)) &&
712 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1da177e4
LT
713 __skb_queue_tail(&path->queue, skb);
714 } else {
de903512 715 ++dev->stats.tx_dropped;
1da177e4
LT
716 dev_kfree_skb_any(skb);
717 }
718
943c246e 719 spin_unlock_irqrestore(&priv->lock, flags);
1da177e4
LT
720}
721
722static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
723{
724 struct ipoib_dev_priv *priv = netdev_priv(dev);
725 struct ipoib_neigh *neigh;
b49fe362 726 struct ipoib_cb *cb = ipoib_skb_cb(skb);
b63b70d8 727 struct ipoib_header *header;
1da177e4
LT
728 unsigned long flags;
729
b63b70d8
SP
730 header = (struct ipoib_header *) skb->data;
731
732 if (unlikely(cb->hwaddr[4] == 0xff)) {
733 /* multicast, arrange "if" according to probability */
734 if ((header->proto != htons(ETH_P_IP)) &&
735 (header->proto != htons(ETH_P_IPV6)) &&
736 (header->proto != htons(ETH_P_ARP)) &&
dc850b0e
PM
737 (header->proto != htons(ETH_P_RARP)) &&
738 (header->proto != htons(ETH_P_TIPC))) {
b63b70d8 739 /* ethertype not supported by IPoIB */
17e6abee
DM
740 ++dev->stats.tx_dropped;
741 dev_kfree_skb_any(skb);
b63b70d8 742 return NETDEV_TX_OK;
17e6abee 743 }
b63b70d8
SP
744 /* Add in the P_Key for multicast*/
745 cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
746 cb->hwaddr[9] = priv->pkey & 0xff;
747
748 neigh = ipoib_neigh_get(dev, cb->hwaddr);
749 if (likely(neigh))
750 goto send_using_neigh;
751 ipoib_mcast_send(dev, cb->hwaddr, skb);
752 return NETDEV_TX_OK;
17e6abee 753 }
1da177e4 754
b63b70d8
SP
755 /* unicast, arrange "switch" according to probability */
756 switch (header->proto) {
757 case htons(ETH_P_IP):
758 case htons(ETH_P_IPV6):
dc850b0e 759 case htons(ETH_P_TIPC):
b63b70d8
SP
760 neigh = ipoib_neigh_get(dev, cb->hwaddr);
761 if (unlikely(!neigh)) {
762 neigh_add_path(skb, cb->hwaddr, dev);
763 return NETDEV_TX_OK;
a50df398 764 }
b63b70d8
SP
765 break;
766 case htons(ETH_P_ARP):
767 case htons(ETH_P_RARP):
768 /* for unicast ARP and RARP should always perform path find */
769 unicast_arp_send(skb, dev, cb);
770 return NETDEV_TX_OK;
771 default:
772 /* ethertype not supported by IPoIB */
773 ++dev->stats.tx_dropped;
774 dev_kfree_skb_any(skb);
775 return NETDEV_TX_OK;
776 }
8a7f7521 777
b63b70d8
SP
778send_using_neigh:
779 /* note we now hold a ref to neigh */
780 if (ipoib_cm_get(neigh)) {
781 if (ipoib_cm_up(neigh)) {
782 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
783 goto unref;
1da177e4 784 }
b63b70d8
SP
785 } else if (neigh->ah) {
786 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
787 goto unref;
788 }
1da177e4 789
b63b70d8
SP
790 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
791 spin_lock_irqsave(&priv->lock, flags);
792 __skb_queue_tail(&neigh->queue, skb);
793 spin_unlock_irqrestore(&priv->lock, flags);
1da177e4 794 } else {
b63b70d8
SP
795 ++dev->stats.tx_dropped;
796 dev_kfree_skb_any(skb);
797 }
1da177e4 798
b63b70d8
SP
799unref:
800 ipoib_neigh_put(neigh);
1da177e4 801
1da177e4
LT
802 return NETDEV_TX_OK;
803}
804
1da177e4
LT
805static void ipoib_timeout(struct net_device *dev)
806{
807 struct ipoib_dev_priv *priv = netdev_priv(dev);
808
4b2d319b
RD
809 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
810 jiffies_to_msecs(jiffies - dev->trans_start));
811 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
812 netif_queue_stopped(dev),
813 priv->tx_head, priv->tx_tail);
1da177e4
LT
814 /* XXX reset QP, etc. */
815}
816
817static int ipoib_hard_header(struct sk_buff *skb,
818 struct net_device *dev,
819 unsigned short type,
3b04ddde 820 const void *daddr, const void *saddr, unsigned len)
1da177e4
LT
821{
822 struct ipoib_header *header;
b49fe362 823 struct ipoib_cb *cb = ipoib_skb_cb(skb);
1da177e4
LT
824
825 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
826
827 header->proto = htons(type);
828 header->reserved = 0;
829
830 /*
b63b70d8 831 * we don't rely on dst_entry structure, always stuff the
936d7de3
RD
832 * destination address into skb->cb so we can figure out where
833 * to send the packet later.
1da177e4 834 */
b63b70d8 835 memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
1da177e4 836
83bdd3b9 837 return sizeof *header;
1da177e4
LT
838}
839
840static void ipoib_set_mcast_list(struct net_device *dev)
841{
842 struct ipoib_dev_priv *priv = netdev_priv(dev);
843
7a343d4c
LA
844 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
845 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
846 return;
847 }
848
0b39578b 849 queue_work(priv->wq, &priv->restart_task);
1da177e4
LT
850}
851
5aa7add8
ND
852static int ipoib_get_iflink(const struct net_device *dev)
853{
854 struct ipoib_dev_priv *priv = netdev_priv(dev);
855
2c153959
ES
856 /* parent interface */
857 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
858 return dev->ifindex;
859
860 /* child/vlan interface */
5aa7add8
ND
861 return priv->parent->ifindex;
862}
863
b63b70d8 864static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
1da177e4 865{
b63b70d8
SP
866 /*
867 * Use only the address parts that contributes to spreading
868 * The subnet prefix is not used as one can not connect to
869 * same remote port (GUID) using the same remote QPN via two
870 * different subnets.
871 */
872 /* qpn octets[1:4) & port GUID octets[12:20) */
9d1ad66e 873 u32 *d32 = (u32 *) daddr;
b63b70d8
SP
874 u32 hv;
875
9d1ad66e 876 hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
b63b70d8
SP
877 return hv & htbl->mask;
878}
879
880struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
881{
882 struct ipoib_dev_priv *priv = netdev_priv(dev);
883 struct ipoib_neigh_table *ntbl = &priv->ntbl;
884 struct ipoib_neigh_hash *htbl;
885 struct ipoib_neigh *neigh = NULL;
886 u32 hash_val;
887
888 rcu_read_lock_bh();
889
890 htbl = rcu_dereference_bh(ntbl->htbl);
891
892 if (!htbl)
893 goto out_unlock;
894
895 hash_val = ipoib_addr_hash(htbl, daddr);
896 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
897 neigh != NULL;
898 neigh = rcu_dereference_bh(neigh->hnext)) {
899 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
900 /* found, take one ref on behalf of the caller */
901 if (!atomic_inc_not_zero(&neigh->refcnt)) {
902 /* deleted */
903 neigh = NULL;
904 goto out_unlock;
905 }
906 neigh->alive = jiffies;
907 goto out_unlock;
908 }
909 }
910
911out_unlock:
912 rcu_read_unlock_bh();
913 return neigh;
914}
915
916static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
917{
918 struct ipoib_neigh_table *ntbl = &priv->ntbl;
919 struct ipoib_neigh_hash *htbl;
920 unsigned long neigh_obsolete;
921 unsigned long dt;
1da177e4 922 unsigned long flags;
b63b70d8 923 int i;
1da177e4 924
b63b70d8 925 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
732a2170 926 return;
1da177e4 927
b5120a6e 928 spin_lock_irqsave(&priv->lock, flags);
b63b70d8
SP
929
930 htbl = rcu_dereference_protected(ntbl->htbl,
b5120a6e 931 lockdep_is_held(&priv->lock));
b63b70d8
SP
932
933 if (!htbl)
934 goto out_unlock;
935
936 /* neigh is obsolete if it was idle for two GC periods */
937 dt = 2 * arp_tbl.gc_interval;
938 neigh_obsolete = jiffies - dt;
939 /* handle possible race condition */
940 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
941 goto out_unlock;
942
943 for (i = 0; i < htbl->size; i++) {
944 struct ipoib_neigh *neigh;
945 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
946
947 while ((neigh = rcu_dereference_protected(*np,
b5120a6e 948 lockdep_is_held(&priv->lock))) != NULL) {
b63b70d8
SP
949 /* was the neigh idle for two GC periods */
950 if (time_after(neigh_obsolete, neigh->alive)) {
951 rcu_assign_pointer(*np,
952 rcu_dereference_protected(neigh->hnext,
b5120a6e 953 lockdep_is_held(&priv->lock)));
b63b70d8 954 /* remove from path/mc list */
b63b70d8 955 list_del(&neigh->list);
b63b70d8
SP
956 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
957 } else {
958 np = &neigh->hnext;
959 }
1da177e4 960
b63b70d8
SP
961 }
962 }
1da177e4 963
b63b70d8 964out_unlock:
b5120a6e 965 spin_unlock_irqrestore(&priv->lock, flags);
b63b70d8 966}
1da177e4 967
b63b70d8
SP
968static void ipoib_reap_neigh(struct work_struct *work)
969{
970 struct ipoib_dev_priv *priv =
971 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
972
973 __ipoib_reap_neigh(priv);
974
975 if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
0b39578b 976 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
b63b70d8 977 arp_tbl.gc_interval);
1da177e4
LT
978}
979
b63b70d8
SP
980
981static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
732a2170 982 struct net_device *dev)
d2e0655e
MT
983{
984 struct ipoib_neigh *neigh;
985
b63b70d8 986 neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
d2e0655e
MT
987 if (!neigh)
988 return NULL;
989
732a2170 990 neigh->dev = dev;
b63b70d8 991 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
82b39913 992 skb_queue_head_init(&neigh->queue);
b63b70d8 993 INIT_LIST_HEAD(&neigh->list);
839fcaba 994 ipoib_cm_set(neigh, NULL);
b63b70d8
SP
995 /* one ref on behalf of the caller */
996 atomic_set(&neigh->refcnt, 1);
997
998 return neigh;
999}
1000
1001struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
1002 struct net_device *dev)
1003{
1004 struct ipoib_dev_priv *priv = netdev_priv(dev);
1005 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1006 struct ipoib_neigh_hash *htbl;
1007 struct ipoib_neigh *neigh;
1008 u32 hash_val;
1009
b63b70d8 1010 htbl = rcu_dereference_protected(ntbl->htbl,
b5120a6e 1011 lockdep_is_held(&priv->lock));
b63b70d8
SP
1012 if (!htbl) {
1013 neigh = NULL;
1014 goto out_unlock;
1015 }
1016
1017 /* need to add a new neigh, but maybe some other thread succeeded?
1018 * recalc hash, maybe hash resize took place so we do a search
1019 */
1020 hash_val = ipoib_addr_hash(htbl, daddr);
1021 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
b5120a6e 1022 lockdep_is_held(&priv->lock));
b63b70d8
SP
1023 neigh != NULL;
1024 neigh = rcu_dereference_protected(neigh->hnext,
b5120a6e 1025 lockdep_is_held(&priv->lock))) {
b63b70d8
SP
1026 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1027 /* found, take one ref on behalf of the caller */
1028 if (!atomic_inc_not_zero(&neigh->refcnt)) {
1029 /* deleted */
1030 neigh = NULL;
1031 break;
1032 }
1033 neigh->alive = jiffies;
1034 goto out_unlock;
1035 }
1036 }
1037
1038 neigh = ipoib_neigh_ctor(daddr, dev);
1039 if (!neigh)
1040 goto out_unlock;
1041
1042 /* one ref on behalf of the hash table */
1043 atomic_inc(&neigh->refcnt);
1044 neigh->alive = jiffies;
1045 /* put in hash */
1046 rcu_assign_pointer(neigh->hnext,
1047 rcu_dereference_protected(htbl->buckets[hash_val],
b5120a6e 1048 lockdep_is_held(&priv->lock)));
b63b70d8
SP
1049 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1050 atomic_inc(&ntbl->entries);
1051
1052out_unlock:
d2e0655e
MT
1053
1054 return neigh;
1055}
1056
b63b70d8 1057void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
d2e0655e 1058{
b63b70d8
SP
1059 /* neigh reference count was dropprd to zero */
1060 struct net_device *dev = neigh->dev;
1061 struct ipoib_dev_priv *priv = netdev_priv(dev);
2745b5b7 1062 struct sk_buff *skb;
b63b70d8
SP
1063 if (neigh->ah)
1064 ipoib_put_ah(neigh->ah);
2745b5b7 1065 while ((skb = __skb_dequeue(&neigh->queue))) {
de903512 1066 ++dev->stats.tx_dropped;
2745b5b7
MT
1067 dev_kfree_skb_any(skb);
1068 }
839fcaba
MT
1069 if (ipoib_cm_get(neigh))
1070 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
b63b70d8
SP
1071 ipoib_dbg(netdev_priv(dev),
1072 "neigh free for %06x %pI6\n",
1073 IPOIB_QPN(neigh->daddr),
1074 neigh->daddr + 4);
d2e0655e 1075 kfree(neigh);
b63b70d8
SP
1076 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1077 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1078 complete(&priv->ntbl.flushed);
1079 }
1080}
1081
1082static void ipoib_neigh_reclaim(struct rcu_head *rp)
1083{
1084 /* Called as a result of removal from hash table */
1085 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1086 /* note TX context may hold another ref */
1087 ipoib_neigh_put(neigh);
d2e0655e
MT
1088}
1089
b63b70d8 1090void ipoib_neigh_free(struct ipoib_neigh *neigh)
1da177e4 1091{
b63b70d8
SP
1092 struct net_device *dev = neigh->dev;
1093 struct ipoib_dev_priv *priv = netdev_priv(dev);
1094 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1095 struct ipoib_neigh_hash *htbl;
1096 struct ipoib_neigh __rcu **np;
1097 struct ipoib_neigh *n;
1098 u32 hash_val;
1099
b63b70d8 1100 htbl = rcu_dereference_protected(ntbl->htbl,
b5120a6e 1101 lockdep_is_held(&priv->lock));
b63b70d8 1102 if (!htbl)
b5120a6e 1103 return;
b63b70d8
SP
1104
1105 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1106 np = &htbl->buckets[hash_val];
1107 for (n = rcu_dereference_protected(*np,
b5120a6e 1108 lockdep_is_held(&priv->lock));
b63b70d8 1109 n != NULL;
6c723a68 1110 n = rcu_dereference_protected(*np,
b5120a6e 1111 lockdep_is_held(&priv->lock))) {
b63b70d8
SP
1112 if (n == neigh) {
1113 /* found */
1114 rcu_assign_pointer(*np,
1115 rcu_dereference_protected(neigh->hnext,
b5120a6e 1116 lockdep_is_held(&priv->lock)));
49b8e744
JF
1117 /* remove from parent list */
1118 list_del(&neigh->list);
b63b70d8 1119 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
b5120a6e 1120 return;
b63b70d8
SP
1121 } else {
1122 np = &n->hnext;
1123 }
1124 }
b63b70d8
SP
1125}
1126
1127static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1128{
1129 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1130 struct ipoib_neigh_hash *htbl;
52374967 1131 struct ipoib_neigh __rcu **buckets;
b63b70d8
SP
1132 u32 size;
1133
1134 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1135 ntbl->htbl = NULL;
b63b70d8
SP
1136 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1137 if (!htbl)
1138 return -ENOMEM;
1139 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1140 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1141 buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1142 if (!buckets) {
1143 kfree(htbl);
1144 return -ENOMEM;
1145 }
1146 htbl->size = size;
1147 htbl->mask = (size - 1);
1148 htbl->buckets = buckets;
52374967 1149 RCU_INIT_POINTER(ntbl->htbl, htbl);
66172c09 1150 htbl->ntbl = ntbl;
b63b70d8
SP
1151 atomic_set(&ntbl->entries, 0);
1152
1153 /* start garbage collection */
1154 clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
0b39578b 1155 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
b63b70d8 1156 arp_tbl.gc_interval);
1da177e4
LT
1157
1158 return 0;
1159}
1160
b63b70d8
SP
1161static void neigh_hash_free_rcu(struct rcu_head *head)
1162{
1163 struct ipoib_neigh_hash *htbl = container_of(head,
1164 struct ipoib_neigh_hash,
1165 rcu);
1166 struct ipoib_neigh __rcu **buckets = htbl->buckets;
66172c09 1167 struct ipoib_neigh_table *ntbl = htbl->ntbl;
b63b70d8
SP
1168
1169 kfree(buckets);
1170 kfree(htbl);
66172c09 1171 complete(&ntbl->deleted);
b63b70d8
SP
1172}
1173
1174void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1175{
1176 struct ipoib_dev_priv *priv = netdev_priv(dev);
1177 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1178 struct ipoib_neigh_hash *htbl;
1179 unsigned long flags;
1180 int i;
1181
1182 /* remove all neigh connected to a given path or mcast */
b5120a6e 1183 spin_lock_irqsave(&priv->lock, flags);
b63b70d8
SP
1184
1185 htbl = rcu_dereference_protected(ntbl->htbl,
b5120a6e 1186 lockdep_is_held(&priv->lock));
b63b70d8
SP
1187
1188 if (!htbl)
1189 goto out_unlock;
1190
1191 for (i = 0; i < htbl->size; i++) {
1192 struct ipoib_neigh *neigh;
1193 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1194
1195 while ((neigh = rcu_dereference_protected(*np,
b5120a6e 1196 lockdep_is_held(&priv->lock))) != NULL) {
b63b70d8
SP
1197 /* delete neighs belong to this parent */
1198 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1199 rcu_assign_pointer(*np,
1200 rcu_dereference_protected(neigh->hnext,
b5120a6e 1201 lockdep_is_held(&priv->lock)));
b63b70d8 1202 /* remove from parent list */
b63b70d8 1203 list_del(&neigh->list);
b63b70d8
SP
1204 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1205 } else {
1206 np = &neigh->hnext;
1207 }
1208
1209 }
1210 }
1211out_unlock:
b5120a6e 1212 spin_unlock_irqrestore(&priv->lock, flags);
b63b70d8
SP
1213}
1214
1215static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1216{
1217 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1218 struct ipoib_neigh_hash *htbl;
1219 unsigned long flags;
66172c09 1220 int i, wait_flushed = 0;
b63b70d8 1221
66172c09 1222 init_completion(&priv->ntbl.flushed);
b63b70d8 1223
b5120a6e 1224 spin_lock_irqsave(&priv->lock, flags);
b63b70d8
SP
1225
1226 htbl = rcu_dereference_protected(ntbl->htbl,
b5120a6e 1227 lockdep_is_held(&priv->lock));
b63b70d8
SP
1228 if (!htbl)
1229 goto out_unlock;
1230
66172c09
SP
1231 wait_flushed = atomic_read(&priv->ntbl.entries);
1232 if (!wait_flushed)
1233 goto free_htbl;
1234
b63b70d8
SP
1235 for (i = 0; i < htbl->size; i++) {
1236 struct ipoib_neigh *neigh;
1237 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1238
1239 while ((neigh = rcu_dereference_protected(*np,
b5120a6e 1240 lockdep_is_held(&priv->lock))) != NULL) {
b63b70d8
SP
1241 rcu_assign_pointer(*np,
1242 rcu_dereference_protected(neigh->hnext,
b5120a6e 1243 lockdep_is_held(&priv->lock)));
b63b70d8 1244 /* remove from path/mc list */
b63b70d8 1245 list_del(&neigh->list);
b63b70d8
SP
1246 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1247 }
1248 }
1249
66172c09 1250free_htbl:
b63b70d8
SP
1251 rcu_assign_pointer(ntbl->htbl, NULL);
1252 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1253
1254out_unlock:
b5120a6e 1255 spin_unlock_irqrestore(&priv->lock, flags);
66172c09
SP
1256 if (wait_flushed)
1257 wait_for_completion(&priv->ntbl.flushed);
b63b70d8
SP
1258}
1259
1260static void ipoib_neigh_hash_uninit(struct net_device *dev)
1261{
1262 struct ipoib_dev_priv *priv = netdev_priv(dev);
1263 int stopped;
1264
1265 ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
66172c09 1266 init_completion(&priv->ntbl.deleted);
b63b70d8
SP
1267 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1268
1269 /* Stop GC if called at init fail need to cancel work */
1270 stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1271 if (!stopped)
1272 cancel_delayed_work(&priv->neigh_reap_task);
1273
66172c09
SP
1274 ipoib_flush_neighs(priv);
1275
1276 wait_for_completion(&priv->ntbl.deleted);
b63b70d8
SP
1277}
1278
1279
1da177e4
LT
1280int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1281{
1282 struct ipoib_dev_priv *priv = netdev_priv(dev);
1283
1284 /* Allocate RX/TX "rings" to hold queued skbs */
0f485251 1285 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
1da177e4
LT
1286 GFP_KERNEL);
1287 if (!priv->rx_ring) {
1288 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
0f485251 1289 ca->name, ipoib_recvq_size);
be7aa663 1290 goto out;
1da177e4 1291 }
1da177e4 1292
948579cd 1293 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
1da177e4
LT
1294 if (!priv->tx_ring) {
1295 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
0f485251 1296 ca->name, ipoib_sendq_size);
1da177e4
LT
1297 goto out_rx_ring_cleanup;
1298 }
1da177e4 1299
1b524963 1300 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
1da177e4
LT
1301
1302 if (ipoib_ib_dev_init(dev, ca, port))
1303 goto out_tx_ring_cleanup;
1304
be7aa663
DL
1305 /*
1306 * Must be after ipoib_ib_dev_init so we can allocate a per
1307 * device wq there and use it here
1308 */
1309 if (ipoib_neigh_hash_init(priv) < 0)
1310 goto out_dev_uninit;
1311
1da177e4
LT
1312 return 0;
1313
be7aa663
DL
1314out_dev_uninit:
1315 ipoib_ib_dev_cleanup(dev);
1316
1da177e4 1317out_tx_ring_cleanup:
10313cbb 1318 vfree(priv->tx_ring);
1da177e4
LT
1319
1320out_rx_ring_cleanup:
1321 kfree(priv->rx_ring);
1322
1323out:
1324 return -ENOMEM;
1325}
1326
1327void ipoib_dev_cleanup(struct net_device *dev)
1328{
1329 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
9baa0b03
OG
1330 LIST_HEAD(head);
1331
1332 ASSERT_RTNL();
1da177e4 1333
1732b0ef 1334 ipoib_delete_debug_files(dev);
1da177e4
LT
1335
1336 /* Delete any child interfaces first */
1337 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
b63b70d8
SP
1338 /* Stop GC on child */
1339 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1340 cancel_delayed_work(&cpriv->neigh_reap_task);
9baa0b03 1341 unregister_netdevice_queue(cpriv->dev, &head);
1da177e4 1342 }
9baa0b03 1343 unregister_netdevice_many(&head);
1da177e4 1344
be7aa663
DL
1345 /*
1346 * Must be before ipoib_ib_dev_cleanup or we delete an in use
1347 * work queue
1348 */
1349 ipoib_neigh_hash_uninit(dev);
1350
1da177e4
LT
1351 ipoib_ib_dev_cleanup(dev);
1352
92a6b34b 1353 kfree(priv->rx_ring);
10313cbb 1354 vfree(priv->tx_ring);
1da177e4 1355
92a6b34b
HR
1356 priv->rx_ring = NULL;
1357 priv->tx_ring = NULL;
1da177e4
LT
1358}
1359
3b04ddde
SH
1360static const struct header_ops ipoib_header_ops = {
1361 .create = ipoib_hard_header,
1362};
1363
fe8114e8 1364static const struct net_device_ops ipoib_netdev_ops = {
9baa0b03 1365 .ndo_uninit = ipoib_uninit,
fe8114e8
SH
1366 .ndo_open = ipoib_open,
1367 .ndo_stop = ipoib_stop,
1368 .ndo_change_mtu = ipoib_change_mtu,
3d96c74d 1369 .ndo_fix_features = ipoib_fix_features,
fe8114e8
SH
1370 .ndo_start_xmit = ipoib_start_xmit,
1371 .ndo_tx_timeout = ipoib_timeout,
afc4b13d 1372 .ndo_set_rx_mode = ipoib_set_mcast_list,
5aa7add8 1373 .ndo_get_iflink = ipoib_get_iflink,
fe8114e8
SH
1374};
1375
9baa0b03 1376void ipoib_setup(struct net_device *dev)
1da177e4
LT
1377{
1378 struct ipoib_dev_priv *priv = netdev_priv(dev);
1379
fe8114e8 1380 dev->netdev_ops = &ipoib_netdev_ops;
2337f809 1381 dev->header_ops = &ipoib_header_ops;
bea3348e 1382
82c24c18
EC
1383 ipoib_set_ethtool_ops(dev);
1384
7f1a3867 1385 netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT);
1da177e4 1386
2337f809 1387 dev->watchdog_timeo = HZ;
1da177e4 1388
2337f809 1389 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1da177e4 1390
936d7de3 1391 dev->hard_header_len = IPOIB_ENCAP_LEN;
2337f809
RD
1392 dev->addr_len = INFINIBAND_ALEN;
1393 dev->type = ARPHRD_INFINIBAND;
1394 dev->tx_queue_len = ipoib_sendq_size * 2;
eb14032f 1395 dev->features = (NETIF_F_VLAN_CHALLENGED |
eb14032f 1396 NETIF_F_HIGHDMA);
02875878 1397 netif_keep_dst(dev);
1da177e4 1398
1da177e4
LT
1399 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
1400
1da177e4
LT
1401 priv->dev = dev;
1402
1403 spin_lock_init(&priv->lock);
1da177e4 1404
f47944cc 1405 init_rwsem(&priv->vlan_rwsem);
1da177e4
LT
1406
1407 INIT_LIST_HEAD(&priv->path_list);
1408 INIT_LIST_HEAD(&priv->child_intfs);
1409 INIT_LIST_HEAD(&priv->dead_ahs);
1410 INIT_LIST_HEAD(&priv->multicast_list);
1411
c4028958 1412 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
e8224e4b 1413 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
ee1e2c82
MS
1414 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
1415 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
1416 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
c4028958
DH
1417 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1418 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
b63b70d8 1419 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
1da177e4
LT
1420}
1421
1422struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
1423{
1424 struct net_device *dev;
1425
c835a677
TG
1426 dev = alloc_netdev((int)sizeof(struct ipoib_dev_priv), name,
1427 NET_NAME_UNKNOWN, ipoib_setup);
1da177e4
LT
1428 if (!dev)
1429 return NULL;
1430
1431 return netdev_priv(dev);
1432}
1433
43cb76d9
GKH
1434static ssize_t show_pkey(struct device *dev,
1435 struct device_attribute *attr, char *buf)
1da177e4 1436{
43cb76d9 1437 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1da177e4
LT
1438
1439 return sprintf(buf, "0x%04x\n", priv->pkey);
1440}
43cb76d9 1441static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1da177e4 1442
335a64a5
OG
1443static ssize_t show_umcast(struct device *dev,
1444 struct device_attribute *attr, char *buf)
1445{
1446 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1447
1448 return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1449}
1450
862096a8 1451void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
335a64a5 1452{
862096a8 1453 struct ipoib_dev_priv *priv = netdev_priv(ndev);
335a64a5
OG
1454
1455 if (umcast_val > 0) {
1456 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1457 ipoib_warn(priv, "ignoring multicast groups joined directly "
1458 "by userspace\n");
1459 } else
1460 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
862096a8
OG
1461}
1462
1463static ssize_t set_umcast(struct device *dev,
1464 struct device_attribute *attr,
1465 const char *buf, size_t count)
1466{
1467 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1468
1469 ipoib_set_umcast(to_net_dev(dev), umcast_val);
335a64a5
OG
1470
1471 return count;
1472}
1473static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1474
1475int ipoib_add_umcast_attr(struct net_device *dev)
1476{
1477 return device_create_file(&dev->dev, &dev_attr_umcast);
1478}
1479
43cb76d9
GKH
1480static ssize_t create_child(struct device *dev,
1481 struct device_attribute *attr,
1da177e4
LT
1482 const char *buf, size_t count)
1483{
1484 int pkey;
1485 int ret;
1486
1487 if (sscanf(buf, "%i", &pkey) != 1)
1488 return -EINVAL;
1489
3d790a4c 1490 if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
1da177e4
LT
1491 return -EINVAL;
1492
4ce05937
RD
1493 /*
1494 * Set the full membership bit, so that we join the right
1495 * broadcast group, etc.
1496 */
1497 pkey |= 0x8000;
1498
43cb76d9 1499 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
1da177e4
LT
1500
1501 return ret ? ret : count;
1502}
7a52b34b 1503static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
1da177e4 1504
43cb76d9
GKH
1505static ssize_t delete_child(struct device *dev,
1506 struct device_attribute *attr,
1da177e4
LT
1507 const char *buf, size_t count)
1508{
1509 int pkey;
1510 int ret;
1511
1512 if (sscanf(buf, "%i", &pkey) != 1)
1513 return -EINVAL;
1514
1515 if (pkey < 0 || pkey > 0xffff)
1516 return -EINVAL;
1517
43cb76d9 1518 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
1da177e4
LT
1519
1520 return ret ? ret : count;
1521
1522}
7a52b34b 1523static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
1da177e4
LT
1524
1525int ipoib_add_pkey_attr(struct net_device *dev)
1526{
43cb76d9 1527 return device_create_file(&dev->dev, &dev_attr_pkey);
1da177e4
LT
1528}
1529
83bb63f6
OG
1530int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
1531{
1532 struct ib_device_attr *device_attr;
1533 int result = -ENOMEM;
1534
1535 device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
1536 if (!device_attr) {
1537 printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
1538 hca->name, sizeof *device_attr);
1539 return result;
1540 }
1541
1542 result = ib_query_device(hca, device_attr);
1543 if (result) {
1544 printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
1545 hca->name, result);
1546 kfree(device_attr);
1547 return result;
1548 }
1549 priv->hca_caps = device_attr->device_cap_flags;
1550
1551 kfree(device_attr);
1552
1553 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
3d96c74d
MM
1554 priv->dev->hw_features = NETIF_F_SG |
1555 NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
83bb63f6 1556
3d96c74d
MM
1557 if (priv->hca_caps & IB_DEVICE_UD_TSO)
1558 priv->dev->hw_features |= NETIF_F_TSO;
8ae31e5b 1559
3d96c74d
MM
1560 priv->dev->features |= priv->dev->hw_features;
1561 }
83bb63f6
OG
1562
1563 return 0;
1564}
1565
1da177e4
LT
1566static struct net_device *ipoib_add_port(const char *format,
1567 struct ib_device *hca, u8 port)
1568{
1569 struct ipoib_dev_priv *priv;
bc7b3a36 1570 struct ib_port_attr attr;
1da177e4
LT
1571 int result = -ENOMEM;
1572
1573 priv = ipoib_intf_alloc(format);
1574 if (!priv)
1575 goto alloc_mem_failed;
1576
1577 SET_NETDEV_DEV(priv->dev, hca->dma_device);
c3aa9b18 1578 priv->dev->dev_id = port - 1;
1da177e4 1579
58e9cc90
AV
1580 result = ib_query_port(hca, port, &attr);
1581 if (!result)
bc7b3a36
SM
1582 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1583 else {
1584 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
1585 hca->name, port);
1586 goto device_init_failed;
1587 }
1588
1589 /* MTU will be reset when mcast join happens */
1590 priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
1591 priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
1592
596b9b68
DM
1593 priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
1594
1da177e4
LT
1595 result = ib_query_pkey(hca, port, 0, &priv->pkey);
1596 if (result) {
1597 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1598 hca->name, port, result);
ca6de177 1599 goto device_init_failed;
1da177e4
LT
1600 }
1601
58e9cc90
AV
1602 result = ipoib_set_dev_features(priv, hca);
1603 if (result)
6046136c 1604 goto device_init_failed;
af40da89 1605
4ce05937
RD
1606 /*
1607 * Set the full membership bit, so that we join the right
1608 * broadcast group, etc.
1609 */
1610 priv->pkey |= 0x8000;
1611
1da177e4
LT
1612 priv->dev->broadcast[8] = priv->pkey >> 8;
1613 priv->dev->broadcast[9] = priv->pkey & 0xff;
1614
1615 result = ib_query_gid(hca, port, 0, &priv->local_gid);
1616 if (result) {
1617 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1618 hca->name, port, result);
ca6de177 1619 goto device_init_failed;
1da177e4
LT
1620 } else
1621 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1622
1da177e4
LT
1623 result = ipoib_dev_init(priv->dev, hca, port);
1624 if (result < 0) {
1625 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1626 hca->name, port, result);
1627 goto device_init_failed;
1628 }
1629
1630 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1631 priv->ca, ipoib_event);
1632 result = ib_register_event_handler(&priv->event_handler);
1633 if (result < 0) {
1634 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1635 "port %d (ret = %d)\n",
1636 hca->name, port, result);
1637 goto event_failed;
1638 }
1639
1640 result = register_netdev(priv->dev);
1641 if (result) {
1642 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1643 hca->name, port, result);
1644 goto register_failed;
1645 }
1646
1732b0ef 1647 ipoib_create_debug_files(priv->dev);
1da177e4 1648
839fcaba
MT
1649 if (ipoib_cm_add_mode_attr(priv->dev))
1650 goto sysfs_failed;
1da177e4
LT
1651 if (ipoib_add_pkey_attr(priv->dev))
1652 goto sysfs_failed;
335a64a5
OG
1653 if (ipoib_add_umcast_attr(priv->dev))
1654 goto sysfs_failed;
43cb76d9 1655 if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
1da177e4 1656 goto sysfs_failed;
43cb76d9 1657 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
1da177e4
LT
1658 goto sysfs_failed;
1659
1660 return priv->dev;
1661
1662sysfs_failed:
1732b0ef 1663 ipoib_delete_debug_files(priv->dev);
1da177e4
LT
1664 unregister_netdev(priv->dev);
1665
1666register_failed:
1667 ib_unregister_event_handler(&priv->event_handler);
0b39578b 1668 flush_workqueue(ipoib_workqueue);
b63b70d8
SP
1669 /* Stop GC if started before flush */
1670 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1671 cancel_delayed_work(&priv->neigh_reap_task);
0b39578b 1672 flush_workqueue(priv->wq);
1da177e4
LT
1673
1674event_failed:
1675 ipoib_dev_cleanup(priv->dev);
1676
1677device_init_failed:
1678 free_netdev(priv->dev);
1679
1680alloc_mem_failed:
1681 return ERR_PTR(result);
1682}
1683
1684static void ipoib_add_one(struct ib_device *device)
1685{
1686 struct list_head *dev_list;
1687 struct net_device *dev;
1688 struct ipoib_dev_priv *priv;
4139032b 1689 int p;
8e37ab68 1690 int count = 0;
07ebafba 1691
1da177e4
LT
1692 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1693 if (!dev_list)
1694 return;
1695
1696 INIT_LIST_HEAD(dev_list);
1697
4139032b 1698 for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) {
8e37ab68 1699 if (!rdma_protocol_ib(device, p))
7b4c8769 1700 continue;
1da177e4
LT
1701 dev = ipoib_add_port("ib%d", device, p);
1702 if (!IS_ERR(dev)) {
1703 priv = netdev_priv(dev);
1704 list_add_tail(&priv->list, dev_list);
8e37ab68 1705 count++;
1da177e4
LT
1706 }
1707 }
1708
8e37ab68
MW
1709 if (!count) {
1710 kfree(dev_list);
1711 return;
1712 }
1713
1da177e4
LT
1714 ib_set_client_data(device, &ipoib_client, dev_list);
1715}
1716
1717static void ipoib_remove_one(struct ib_device *device)
1718{
1719 struct ipoib_dev_priv *priv, *tmp;
1720 struct list_head *dev_list;
1721
1722 dev_list = ib_get_client_data(device, &ipoib_client);
5a2815f0
IG
1723 if (!dev_list)
1724 return;
1da177e4
LT
1725
1726 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1727 ib_unregister_event_handler(&priv->event_handler);
0b39578b 1728 flush_workqueue(ipoib_workqueue);
a77a57a1
RD
1729
1730 rtnl_lock();
1731 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
1732 rtnl_unlock();
1733
b63b70d8
SP
1734 /* Stop GC */
1735 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1736 cancel_delayed_work(&priv->neigh_reap_task);
0b39578b 1737 flush_workqueue(priv->wq);
1da177e4
LT
1738
1739 unregister_netdev(priv->dev);
1da177e4
LT
1740 free_netdev(priv->dev);
1741 }
06c56e44
MT
1742
1743 kfree(dev_list);
1da177e4
LT
1744}
1745
1746static int __init ipoib_init_module(void)
1747{
1748 int ret;
1749
0f485251
SM
1750 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1751 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1752 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1753
1754 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1755 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
732eacc0 1756 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
68e995a2
PS
1757#ifdef CONFIG_INFINIBAND_IPOIB_CM
1758 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1759#endif
0f485251 1760
f89271da
EC
1761 /*
1762 * When copying small received packets, we only copy from the
1763 * linear data part of the SKB, so we rely on this condition.
1764 */
1765 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
1766
1da177e4
LT
1767 ret = ipoib_register_debugfs();
1768 if (ret)
1769 return ret;
1770
1771 /*
0b39578b
DL
1772 * We create a global workqueue here that is used for all flush
1773 * operations. However, if you attempt to flush a workqueue
1774 * from a task on that same workqueue, it deadlocks the system.
1775 * We want to be able to flush the tasks associated with a
1776 * specific net device, so we also create a workqueue for each
1777 * netdevice. We queue up the tasks for that device only on
1778 * its private workqueue, and we only queue up flush events
1779 * on our global flush workqueue. This avoids the deadlocks.
1da177e4 1780 */
0b39578b 1781 ipoib_workqueue = create_singlethread_workqueue("ipoib_flush");
1da177e4
LT
1782 if (!ipoib_workqueue) {
1783 ret = -ENOMEM;
1784 goto err_fs;
1785 }
1786
c1a0b23b
MT
1787 ib_sa_register_client(&ipoib_sa_client);
1788
1da177e4
LT
1789 ret = ib_register_client(&ipoib_client);
1790 if (ret)
c1a0b23b 1791 goto err_sa;
1da177e4 1792
9baa0b03
OG
1793 ret = ipoib_netlink_init();
1794 if (ret)
1795 goto err_client;
1796
1da177e4
LT
1797 return 0;
1798
9baa0b03
OG
1799err_client:
1800 ib_unregister_client(&ipoib_client);
1801
c1a0b23b
MT
1802err_sa:
1803 ib_sa_unregister_client(&ipoib_sa_client);
1da177e4
LT
1804 destroy_workqueue(ipoib_workqueue);
1805
9adec1a8
RD
1806err_fs:
1807 ipoib_unregister_debugfs();
1808
1da177e4
LT
1809 return ret;
1810}
1811
1812static void __exit ipoib_cleanup_module(void)
1813{
9baa0b03 1814 ipoib_netlink_fini();
1da177e4 1815 ib_unregister_client(&ipoib_client);
c1a0b23b 1816 ib_sa_unregister_client(&ipoib_sa_client);
9adec1a8 1817 ipoib_unregister_debugfs();
1da177e4
LT
1818 destroy_workqueue(ipoib_workqueue);
1819}
1820
1821module_init(ipoib_init_module);
1822module_exit(ipoib_cleanup_module);
This page took 0.847381 seconds and 5 git commands to generate.