IB/sa: Require SA registration
[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.
33 *
34 * $Id: ipoib_main.c 1377 2004-12-23 19:57:12Z roland $
35 */
36
37#include "ipoib.h"
38
1da177e4
LT
39#include <linux/module.h>
40
41#include <linux/init.h>
42#include <linux/slab.h>
43#include <linux/vmalloc.h>
0f485251 44#include <linux/kernel.h>
1da177e4
LT
45
46#include <linux/if_arp.h> /* For ARPHRD_xxx */
47
48#include <linux/ip.h>
49#include <linux/in.h>
50
14c85021
ACM
51#include <net/dst.h>
52
1da177e4
LT
53MODULE_AUTHOR("Roland Dreier");
54MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
55MODULE_LICENSE("Dual BSD/GPL");
56
0f485251
SM
57int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
58int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
59
60module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
61MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
62module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
63MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
64
1da177e4
LT
65#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
66int ipoib_debug_level;
67
68module_param_named(debug_level, ipoib_debug_level, int, 0644);
69MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
70#endif
71
1732b0ef
RD
72struct ipoib_path_iter {
73 struct net_device *dev;
74 struct ipoib_path path;
75};
76
1da177e4
LT
77static const u8 ipv4_bcast_addr[] = {
78 0x00, 0xff, 0xff, 0xff,
79 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
80 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
81};
82
83struct workqueue_struct *ipoib_workqueue;
84
c1a0b23b
MT
85struct ib_sa_client ipoib_sa_client;
86
1da177e4
LT
87static void ipoib_add_one(struct ib_device *device);
88static void ipoib_remove_one(struct ib_device *device);
89
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
102 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
103
104 if (ipoib_pkey_dev_delay_open(dev))
105 return 0;
106
107 if (ipoib_ib_dev_open(dev))
108 return -EINVAL;
109
267ee88e
RD
110 if (ipoib_ib_dev_up(dev)) {
111 ipoib_ib_dev_stop(dev);
1da177e4 112 return -EINVAL;
267ee88e 113 }
1da177e4
LT
114
115 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
116 struct ipoib_dev_priv *cpriv;
117
118 /* Bring up any child interfaces too */
95ed644f 119 mutex_lock(&priv->vlan_mutex);
1da177e4
LT
120 list_for_each_entry(cpriv, &priv->child_intfs, list) {
121 int flags;
122
123 flags = cpriv->dev->flags;
124 if (flags & IFF_UP)
125 continue;
126
127 dev_change_flags(cpriv->dev, flags | IFF_UP);
128 }
95ed644f 129 mutex_unlock(&priv->vlan_mutex);
1da177e4
LT
130 }
131
132 netif_start_queue(dev);
133
134 return 0;
135}
136
137static int ipoib_stop(struct net_device *dev)
138{
139 struct ipoib_dev_priv *priv = netdev_priv(dev);
140
141 ipoib_dbg(priv, "stopping interface\n");
142
143 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
144
145 netif_stop_queue(dev);
146
0b3ea082
JM
147 /*
148 * Now flush workqueue to make sure a scheduled task doesn't
149 * bring our internal state back up.
150 */
151 flush_workqueue(ipoib_workqueue);
152
153 ipoib_ib_dev_down(dev, 1);
1da177e4
LT
154 ipoib_ib_dev_stop(dev);
155
156 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
157 struct ipoib_dev_priv *cpriv;
158
159 /* Bring down any child interfaces too */
95ed644f 160 mutex_lock(&priv->vlan_mutex);
1da177e4
LT
161 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 }
95ed644f 170 mutex_unlock(&priv->vlan_mutex);
1da177e4
LT
171 }
172
173 return 0;
174}
175
176static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
177{
178 struct ipoib_dev_priv *priv = netdev_priv(dev);
179
180 if (new_mtu > IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN)
181 return -EINVAL;
182
183 priv->admin_mtu = new_mtu;
184
185 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
186
187 return 0;
188}
189
37c22a77 190static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
1da177e4
LT
191{
192 struct ipoib_dev_priv *priv = netdev_priv(dev);
193 struct rb_node *n = priv->path_tree.rb_node;
194 struct ipoib_path *path;
195 int ret;
196
197 while (n) {
198 path = rb_entry(n, struct ipoib_path, rb_node);
199
37c22a77 200 ret = memcmp(gid, path->pathrec.dgid.raw,
1da177e4
LT
201 sizeof (union ib_gid));
202
203 if (ret < 0)
204 n = n->rb_left;
205 else if (ret > 0)
206 n = n->rb_right;
207 else
208 return path;
209 }
210
211 return NULL;
212}
213
214static int __path_add(struct net_device *dev, struct ipoib_path *path)
215{
216 struct ipoib_dev_priv *priv = netdev_priv(dev);
217 struct rb_node **n = &priv->path_tree.rb_node;
218 struct rb_node *pn = NULL;
219 struct ipoib_path *tpath;
220 int ret;
221
222 while (*n) {
223 pn = *n;
224 tpath = rb_entry(pn, struct ipoib_path, rb_node);
225
226 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
227 sizeof (union ib_gid));
228 if (ret < 0)
229 n = &pn->rb_left;
230 else if (ret > 0)
231 n = &pn->rb_right;
232 else
233 return -EEXIST;
234 }
235
236 rb_link_node(&path->rb_node, pn, n);
237 rb_insert_color(&path->rb_node, &priv->path_tree);
238
239 list_add_tail(&path->list, &priv->path_list);
240
241 return 0;
242}
243
244static void path_free(struct net_device *dev, struct ipoib_path *path)
245{
246 struct ipoib_dev_priv *priv = netdev_priv(dev);
247 struct ipoib_neigh *neigh, *tn;
248 struct sk_buff *skb;
249 unsigned long flags;
250
251 while ((skb = __skb_dequeue(&path->queue)))
252 dev_kfree_skb_irq(skb);
253
254 spin_lock_irqsave(&priv->lock, flags);
255
256 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
257 /*
258 * It's safe to call ipoib_put_ah() inside priv->lock
259 * here, because we know that path->ah will always
260 * hold one more reference, so ipoib_put_ah() will
261 * never do more than decrement the ref count.
262 */
263 if (neigh->ah)
264 ipoib_put_ah(neigh->ah);
d2e0655e
MT
265
266 ipoib_neigh_free(neigh);
1da177e4
LT
267 }
268
269 spin_unlock_irqrestore(&priv->lock, flags);
270
271 if (path->ah)
272 ipoib_put_ah(path->ah);
273
274 kfree(path);
275}
276
1732b0ef
RD
277#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
278
279struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
280{
281 struct ipoib_path_iter *iter;
282
283 iter = kmalloc(sizeof *iter, GFP_KERNEL);
284 if (!iter)
285 return NULL;
286
287 iter->dev = dev;
288 memset(iter->path.pathrec.dgid.raw, 0, 16);
289
290 if (ipoib_path_iter_next(iter)) {
291 kfree(iter);
292 return NULL;
293 }
294
295 return iter;
296}
297
298int ipoib_path_iter_next(struct ipoib_path_iter *iter)
299{
300 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
301 struct rb_node *n;
302 struct ipoib_path *path;
303 int ret = 1;
304
305 spin_lock_irq(&priv->lock);
306
307 n = rb_first(&priv->path_tree);
308
309 while (n) {
310 path = rb_entry(n, struct ipoib_path, rb_node);
311
312 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
313 sizeof (union ib_gid)) < 0) {
314 iter->path = *path;
315 ret = 0;
316 break;
317 }
318
319 n = rb_next(n);
320 }
321
322 spin_unlock_irq(&priv->lock);
323
324 return ret;
325}
326
327void ipoib_path_iter_read(struct ipoib_path_iter *iter,
328 struct ipoib_path *path)
329{
330 *path = iter->path;
331}
332
333#endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
334
1da177e4
LT
335void ipoib_flush_paths(struct net_device *dev)
336{
337 struct ipoib_dev_priv *priv = netdev_priv(dev);
338 struct ipoib_path *path, *tp;
339 LIST_HEAD(remove_list);
1da177e4 340
9217b27b
MT
341 spin_lock_irq(&priv->tx_lock);
342 spin_lock(&priv->lock);
1da177e4
LT
343
344 list_splice(&priv->path_list, &remove_list);
345 INIT_LIST_HEAD(&priv->path_list);
346
347 list_for_each_entry(path, &remove_list, list)
348 rb_erase(&path->rb_node, &priv->path_tree);
349
1da177e4
LT
350 list_for_each_entry_safe(path, tp, &remove_list, list) {
351 if (path->query)
352 ib_sa_cancel_query(path->query_id, path->query);
9217b27b
MT
353 spin_unlock(&priv->lock);
354 spin_unlock_irq(&priv->tx_lock);
1da177e4
LT
355 wait_for_completion(&path->done);
356 path_free(dev, path);
9217b27b
MT
357 spin_lock_irq(&priv->tx_lock);
358 spin_lock(&priv->lock);
1da177e4 359 }
9217b27b
MT
360 spin_unlock(&priv->lock);
361 spin_unlock_irq(&priv->tx_lock);
1da177e4
LT
362}
363
364static void path_rec_completion(int status,
365 struct ib_sa_path_rec *pathrec,
366 void *path_ptr)
367{
368 struct ipoib_path *path = path_ptr;
369 struct net_device *dev = path->dev;
370 struct ipoib_dev_priv *priv = netdev_priv(dev);
371 struct ipoib_ah *ah = NULL;
372 struct ipoib_neigh *neigh;
373 struct sk_buff_head skqueue;
374 struct sk_buff *skb;
375 unsigned long flags;
376
377 if (pathrec)
378 ipoib_dbg(priv, "PathRec LID 0x%04x for GID " IPOIB_GID_FMT "\n",
379 be16_to_cpu(pathrec->dlid), IPOIB_GID_ARG(pathrec->dgid));
380 else
381 ipoib_dbg(priv, "PathRec status %d for GID " IPOIB_GID_FMT "\n",
382 status, IPOIB_GID_ARG(path->pathrec.dgid));
383
384 skb_queue_head_init(&skqueue);
385
386 if (!status) {
387 struct ib_ah_attr av = {
388 .dlid = be16_to_cpu(pathrec->dlid),
389 .sl = pathrec->sl,
bf6a9e31
JM
390 .port_num = priv->port,
391 .static_rate = pathrec->rate
1da177e4 392 };
1da177e4
LT
393
394 ah = ipoib_create_ah(dev, priv->pd, &av);
395 }
396
397 spin_lock_irqsave(&priv->lock, flags);
398
399 path->ah = ah;
400
401 if (ah) {
402 path->pathrec = *pathrec;
403
404 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
405 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
406
407 while ((skb = __skb_dequeue(&path->queue)))
408 __skb_queue_tail(&skqueue, skb);
409
410 list_for_each_entry(neigh, &path->neigh_list, list) {
411 kref_get(&path->ah->ref);
412 neigh->ah = path->ah;
8a7f7521
MT
413 memcpy(&neigh->dgid.raw, &path->pathrec.dgid.raw,
414 sizeof(union ib_gid));
1da177e4
LT
415
416 while ((skb = __skb_dequeue(&neigh->queue)))
417 __skb_queue_tail(&skqueue, skb);
418 }
5872a9fc 419 }
1da177e4 420
5872a9fc 421 path->query = NULL;
1da177e4
LT
422 complete(&path->done);
423
424 spin_unlock_irqrestore(&priv->lock, flags);
425
426 while ((skb = __skb_dequeue(&skqueue))) {
427 skb->dev = dev;
428 if (dev_queue_xmit(skb))
429 ipoib_warn(priv, "dev_queue_xmit failed "
430 "to requeue packet\n");
431 }
432}
433
37c22a77 434static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
1da177e4
LT
435{
436 struct ipoib_dev_priv *priv = netdev_priv(dev);
437 struct ipoib_path *path;
438
21a38489 439 path = kzalloc(sizeof *path, GFP_ATOMIC);
1da177e4
LT
440 if (!path)
441 return NULL;
442
21a38489 443 path->dev = dev;
1da177e4
LT
444
445 skb_queue_head_init(&path->queue);
446
447 INIT_LIST_HEAD(&path->neigh_list);
1da177e4 448
37c22a77 449 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
1da177e4
LT
450 path->pathrec.sgid = priv->local_gid;
451 path->pathrec.pkey = cpu_to_be16(priv->pkey);
452 path->pathrec.numb_path = 1;
453
454 return path;
455}
456
457static int path_rec_start(struct net_device *dev,
458 struct ipoib_path *path)
459{
460 struct ipoib_dev_priv *priv = netdev_priv(dev);
461
462 ipoib_dbg(priv, "Start path record lookup for " IPOIB_GID_FMT "\n",
463 IPOIB_GID_ARG(path->pathrec.dgid));
464
65c7edda
RD
465 init_completion(&path->done);
466
1da177e4 467 path->query_id =
c1a0b23b 468 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
1da177e4
LT
469 &path->pathrec,
470 IB_SA_PATH_REC_DGID |
471 IB_SA_PATH_REC_SGID |
472 IB_SA_PATH_REC_NUMB_PATH |
473 IB_SA_PATH_REC_PKEY,
474 1000, GFP_ATOMIC,
475 path_rec_completion,
476 path, &path->query);
477 if (path->query_id < 0) {
478 ipoib_warn(priv, "ib_sa_path_rec_get failed\n");
479 path->query = NULL;
480 return path->query_id;
481 }
482
483 return 0;
484}
485
486static void neigh_add_path(struct sk_buff *skb, struct net_device *dev)
487{
488 struct ipoib_dev_priv *priv = netdev_priv(dev);
489 struct ipoib_path *path;
490 struct ipoib_neigh *neigh;
491
d2e0655e 492 neigh = ipoib_neigh_alloc(skb->dst->neighbour);
1da177e4
LT
493 if (!neigh) {
494 ++priv->stats.tx_dropped;
495 dev_kfree_skb_any(skb);
496 return;
497 }
498
499 skb_queue_head_init(&neigh->queue);
1da177e4
LT
500
501 /*
502 * We can only be called from ipoib_start_xmit, so we're
503 * inside tx_lock -- no need to save/restore flags.
504 */
505 spin_lock(&priv->lock);
506
37c22a77 507 path = __path_find(dev, skb->dst->neighbour->ha + 4);
1da177e4 508 if (!path) {
37c22a77 509 path = path_rec_create(dev, skb->dst->neighbour->ha + 4);
1da177e4 510 if (!path)
d2e0655e 511 goto err_path;
1da177e4
LT
512
513 __path_add(dev, path);
514 }
515
516 list_add_tail(&neigh->list, &path->neigh_list);
517
47f7a071 518 if (path->ah) {
1da177e4
LT
519 kref_get(&path->ah->ref);
520 neigh->ah = path->ah;
8a7f7521
MT
521 memcpy(&neigh->dgid.raw, &path->pathrec.dgid.raw,
522 sizeof(union ib_gid));
1da177e4
LT
523
524 ipoib_send(dev, skb, path->ah,
525 be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
526 } else {
527 neigh->ah = NULL;
bfef73fa 528 __skb_queue_tail(&neigh->queue, skb);
1da177e4
LT
529
530 if (!path->query && path_rec_start(dev, path))
d2e0655e 531 goto err_list;
1da177e4
LT
532 }
533
534 spin_unlock(&priv->lock);
535 return;
536
d2e0655e 537err_list:
1da177e4 538 list_del(&neigh->list);
1da177e4 539
d2e0655e
MT
540err_path:
541 ipoib_neigh_free(neigh);
1da177e4
LT
542 ++priv->stats.tx_dropped;
543 dev_kfree_skb_any(skb);
544
545 spin_unlock(&priv->lock);
546}
547
d70ed607 548static void ipoib_path_lookup(struct sk_buff *skb, struct net_device *dev)
1da177e4
LT
549{
550 struct ipoib_dev_priv *priv = netdev_priv(skb->dev);
551
552 /* Look up path record for unicasts */
553 if (skb->dst->neighbour->ha[4] != 0xff) {
554 neigh_add_path(skb, dev);
555 return;
556 }
557
558 /* Add in the P_Key for multicasts */
559 skb->dst->neighbour->ha[8] = (priv->pkey >> 8) & 0xff;
560 skb->dst->neighbour->ha[9] = priv->pkey & 0xff;
37c22a77 561 ipoib_mcast_send(dev, skb->dst->neighbour->ha + 4, skb);
1da177e4
LT
562}
563
564static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
565 struct ipoib_pseudoheader *phdr)
566{
567 struct ipoib_dev_priv *priv = netdev_priv(dev);
568 struct ipoib_path *path;
569
570 /*
571 * We can only be called from ipoib_start_xmit, so we're
572 * inside tx_lock -- no need to save/restore flags.
573 */
574 spin_lock(&priv->lock);
575
37c22a77 576 path = __path_find(dev, phdr->hwaddr + 4);
1da177e4 577 if (!path) {
37c22a77 578 path = path_rec_create(dev, phdr->hwaddr + 4);
1da177e4
LT
579 if (path) {
580 /* put pseudoheader back on for next time */
581 skb_push(skb, sizeof *phdr);
582 __skb_queue_tail(&path->queue, skb);
583
584 if (path_rec_start(dev, path)) {
585 spin_unlock(&priv->lock);
586 path_free(dev, path);
587 return;
588 } else
589 __path_add(dev, path);
590 } else {
591 ++priv->stats.tx_dropped;
592 dev_kfree_skb_any(skb);
593 }
594
595 spin_unlock(&priv->lock);
596 return;
597 }
598
47f7a071 599 if (path->ah) {
1da177e4
LT
600 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
601 be16_to_cpu(path->pathrec.dlid));
602
603 ipoib_send(dev, skb, path->ah,
604 be32_to_cpup((__be32 *) phdr->hwaddr));
605 } else if ((path->query || !path_rec_start(dev, path)) &&
606 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
607 /* put pseudoheader back on for next time */
608 skb_push(skb, sizeof *phdr);
609 __skb_queue_tail(&path->queue, skb);
610 } else {
611 ++priv->stats.tx_dropped;
612 dev_kfree_skb_any(skb);
613 }
614
615 spin_unlock(&priv->lock);
616}
617
618static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
619{
620 struct ipoib_dev_priv *priv = netdev_priv(dev);
621 struct ipoib_neigh *neigh;
622 unsigned long flags;
623
a20583a7 624 if (!spin_trylock_irqsave(&priv->tx_lock, flags))
1da177e4 625 return NETDEV_TX_LOCKED;
1da177e4
LT
626
627 /*
628 * Check if our queue is stopped. Since we have the LLTX bit
629 * set, we can't rely on netif_stop_queue() preventing our
630 * xmit function from being called with a full queue.
631 */
632 if (unlikely(netif_queue_stopped(dev))) {
633 spin_unlock_irqrestore(&priv->tx_lock, flags);
634 return NETDEV_TX_BUSY;
635 }
636
637 if (skb->dst && skb->dst->neighbour) {
638 if (unlikely(!*to_ipoib_neigh(skb->dst->neighbour))) {
d70ed607 639 ipoib_path_lookup(skb, dev);
1da177e4
LT
640 goto out;
641 }
642
643 neigh = *to_ipoib_neigh(skb->dst->neighbour);
644
645 if (likely(neigh->ah)) {
8a7f7521
MT
646 if (unlikely(memcmp(&neigh->dgid.raw,
647 skb->dst->neighbour->ha + 4,
648 sizeof(union ib_gid)))) {
649 spin_lock(&priv->lock);
650 /*
651 * It's safe to call ipoib_put_ah() inside
652 * priv->lock here, because we know that
653 * path->ah will always hold one more reference,
654 * so ipoib_put_ah() will never do more than
655 * decrement the ref count.
656 */
657 ipoib_put_ah(neigh->ah);
658 list_del(&neigh->list);
659 ipoib_neigh_free(neigh);
660 spin_unlock(&priv->lock);
661 ipoib_path_lookup(skb, dev);
662 goto out;
663 }
664
1da177e4
LT
665 ipoib_send(dev, skb, neigh->ah,
666 be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
667 goto out;
668 }
669
670 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
671 spin_lock(&priv->lock);
672 __skb_queue_tail(&neigh->queue, skb);
673 spin_unlock(&priv->lock);
674 } else {
675 ++priv->stats.tx_dropped;
676 dev_kfree_skb_any(skb);
677 }
678 } else {
679 struct ipoib_pseudoheader *phdr =
680 (struct ipoib_pseudoheader *) skb->data;
681 skb_pull(skb, sizeof *phdr);
682
683 if (phdr->hwaddr[4] == 0xff) {
684 /* Add in the P_Key for multicast*/
685 phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
686 phdr->hwaddr[9] = priv->pkey & 0xff;
687
37c22a77 688 ipoib_mcast_send(dev, phdr->hwaddr + 4, skb);
1da177e4 689 } else {
0dca0f7b 690 /* unicast GID -- should be ARP or RARP reply */
1da177e4 691
0dca0f7b
HR
692 if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
693 (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
1da177e4
LT
694 ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x "
695 IPOIB_GID_FMT "\n",
696 skb->dst ? "neigh" : "dst",
97f52eb4
SH
697 be16_to_cpup((__be16 *) skb->data),
698 be32_to_cpup((__be32 *) phdr->hwaddr),
37c22a77 699 IPOIB_GID_RAW_ARG(phdr->hwaddr + 4));
1da177e4
LT
700 dev_kfree_skb_any(skb);
701 ++priv->stats.tx_dropped;
702 goto out;
703 }
704
705 unicast_arp_send(skb, dev, phdr);
706 }
707 }
708
709out:
710 spin_unlock_irqrestore(&priv->tx_lock, flags);
711
712 return NETDEV_TX_OK;
713}
714
715static struct net_device_stats *ipoib_get_stats(struct net_device *dev)
716{
717 struct ipoib_dev_priv *priv = netdev_priv(dev);
718
719 return &priv->stats;
720}
721
722static void ipoib_timeout(struct net_device *dev)
723{
724 struct ipoib_dev_priv *priv = netdev_priv(dev);
725
4b2d319b
RD
726 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
727 jiffies_to_msecs(jiffies - dev->trans_start));
728 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
729 netif_queue_stopped(dev),
730 priv->tx_head, priv->tx_tail);
1da177e4
LT
731 /* XXX reset QP, etc. */
732}
733
734static int ipoib_hard_header(struct sk_buff *skb,
735 struct net_device *dev,
736 unsigned short type,
737 void *daddr, void *saddr, unsigned len)
738{
739 struct ipoib_header *header;
740
741 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
742
743 header->proto = htons(type);
744 header->reserved = 0;
745
746 /*
747 * If we don't have a neighbour structure, stuff the
748 * destination address onto the front of the skb so we can
749 * figure out where to send the packet later.
750 */
ef12d456 751 if ((!skb->dst || !skb->dst->neighbour) && daddr) {
1da177e4
LT
752 struct ipoib_pseudoheader *phdr =
753 (struct ipoib_pseudoheader *) skb_push(skb, sizeof *phdr);
754 memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
755 }
756
757 return 0;
758}
759
760static void ipoib_set_mcast_list(struct net_device *dev)
761{
762 struct ipoib_dev_priv *priv = netdev_priv(dev);
763
7a343d4c
LA
764 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
765 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
766 return;
767 }
768
1ad62a19 769 queue_work(ipoib_workqueue, &priv->restart_task);
1da177e4
LT
770}
771
772static void ipoib_neigh_destructor(struct neighbour *n)
773{
774 struct ipoib_neigh *neigh;
775 struct ipoib_dev_priv *priv = netdev_priv(n->dev);
776 unsigned long flags;
777 struct ipoib_ah *ah = NULL;
778
779 ipoib_dbg(priv,
780 "neigh_destructor for %06x " IPOIB_GID_FMT "\n",
781 be32_to_cpup((__be32 *) n->ha),
37c22a77 782 IPOIB_GID_RAW_ARG(n->ha + 4));
1da177e4
LT
783
784 spin_lock_irqsave(&priv->lock, flags);
785
786 neigh = *to_ipoib_neigh(n);
787 if (neigh) {
788 if (neigh->ah)
789 ah = neigh->ah;
790 list_del(&neigh->list);
d2e0655e 791 ipoib_neigh_free(neigh);
1da177e4
LT
792 }
793
794 spin_unlock_irqrestore(&priv->lock, flags);
795
796 if (ah)
797 ipoib_put_ah(ah);
798}
799
d2e0655e
MT
800struct ipoib_neigh *ipoib_neigh_alloc(struct neighbour *neighbour)
801{
802 struct ipoib_neigh *neigh;
803
804 neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
805 if (!neigh)
806 return NULL;
807
808 neigh->neighbour = neighbour;
809 *to_ipoib_neigh(neighbour) = neigh;
810
811 return neigh;
812}
813
814void ipoib_neigh_free(struct ipoib_neigh *neigh)
815{
816 *to_ipoib_neigh(neigh->neighbour) = NULL;
817 kfree(neigh);
818}
819
1da177e4
LT
820static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
821{
c5ecd62c 822 parms->neigh_destructor = ipoib_neigh_destructor;
1da177e4
LT
823
824 return 0;
825}
826
827int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
828{
829 struct ipoib_dev_priv *priv = netdev_priv(dev);
830
831 /* Allocate RX/TX "rings" to hold queued skbs */
0f485251 832 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
1da177e4
LT
833 GFP_KERNEL);
834 if (!priv->rx_ring) {
835 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
0f485251 836 ca->name, ipoib_recvq_size);
1da177e4
LT
837 goto out;
838 }
1da177e4 839
0f485251 840 priv->tx_ring = kzalloc(ipoib_sendq_size * sizeof *priv->tx_ring,
1da177e4
LT
841 GFP_KERNEL);
842 if (!priv->tx_ring) {
843 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
0f485251 844 ca->name, ipoib_sendq_size);
1da177e4
LT
845 goto out_rx_ring_cleanup;
846 }
1da177e4
LT
847
848 /* priv->tx_head & tx_tail are already 0 */
849
850 if (ipoib_ib_dev_init(dev, ca, port))
851 goto out_tx_ring_cleanup;
852
853 return 0;
854
855out_tx_ring_cleanup:
856 kfree(priv->tx_ring);
857
858out_rx_ring_cleanup:
859 kfree(priv->rx_ring);
860
861out:
862 return -ENOMEM;
863}
864
865void ipoib_dev_cleanup(struct net_device *dev)
866{
867 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
868
1732b0ef 869 ipoib_delete_debug_files(dev);
1da177e4
LT
870
871 /* Delete any child interfaces first */
872 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
873 unregister_netdev(cpriv->dev);
874 ipoib_dev_cleanup(cpriv->dev);
875 free_netdev(cpriv->dev);
876 }
877
878 ipoib_ib_dev_cleanup(dev);
879
92a6b34b
HR
880 kfree(priv->rx_ring);
881 kfree(priv->tx_ring);
1da177e4 882
92a6b34b
HR
883 priv->rx_ring = NULL;
884 priv->tx_ring = NULL;
1da177e4
LT
885}
886
887static void ipoib_setup(struct net_device *dev)
888{
889 struct ipoib_dev_priv *priv = netdev_priv(dev);
890
891 dev->open = ipoib_open;
892 dev->stop = ipoib_stop;
893 dev->change_mtu = ipoib_change_mtu;
894 dev->hard_start_xmit = ipoib_start_xmit;
895 dev->get_stats = ipoib_get_stats;
896 dev->tx_timeout = ipoib_timeout;
897 dev->hard_header = ipoib_hard_header;
898 dev->set_multicast_list = ipoib_set_mcast_list;
899 dev->neigh_setup = ipoib_neigh_setup_dev;
900
901 dev->watchdog_timeo = HZ;
902
1da177e4
LT
903 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
904
905 /*
906 * We add in INFINIBAND_ALEN to allow for the destination
907 * address "pseudoheader" for skbs without neighbour struct.
908 */
909 dev->hard_header_len = IPOIB_ENCAP_LEN + INFINIBAND_ALEN;
910 dev->addr_len = INFINIBAND_ALEN;
911 dev->type = ARPHRD_INFINIBAND;
0f485251 912 dev->tx_queue_len = ipoib_sendq_size * 2;
1da177e4
LT
913 dev->features = NETIF_F_VLAN_CHALLENGED | NETIF_F_LLTX;
914
915 /* MTU will be reset when mcast join happens */
916 dev->mtu = IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN;
917 priv->mcast_mtu = priv->admin_mtu = dev->mtu;
918
919 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
920
921 netif_carrier_off(dev);
922
923 SET_MODULE_OWNER(dev);
924
925 priv->dev = dev;
926
927 spin_lock_init(&priv->lock);
928 spin_lock_init(&priv->tx_lock);
929
95ed644f
IM
930 mutex_init(&priv->mcast_mutex);
931 mutex_init(&priv->vlan_mutex);
1da177e4
LT
932
933 INIT_LIST_HEAD(&priv->path_list);
934 INIT_LIST_HEAD(&priv->child_intfs);
935 INIT_LIST_HEAD(&priv->dead_ahs);
936 INIT_LIST_HEAD(&priv->multicast_list);
937
938 INIT_WORK(&priv->pkey_task, ipoib_pkey_poll, priv->dev);
939 INIT_WORK(&priv->mcast_task, ipoib_mcast_join_task, priv->dev);
940 INIT_WORK(&priv->flush_task, ipoib_ib_dev_flush, priv->dev);
941 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task, priv->dev);
942 INIT_WORK(&priv->ah_reap_task, ipoib_reap_ah, priv->dev);
943}
944
945struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
946{
947 struct net_device *dev;
948
949 dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
950 ipoib_setup);
951 if (!dev)
952 return NULL;
953
954 return netdev_priv(dev);
955}
956
957static ssize_t show_pkey(struct class_device *cdev, char *buf)
958{
959 struct ipoib_dev_priv *priv =
960 netdev_priv(container_of(cdev, struct net_device, class_dev));
961
962 return sprintf(buf, "0x%04x\n", priv->pkey);
963}
964static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
965
966static ssize_t create_child(struct class_device *cdev,
967 const char *buf, size_t count)
968{
969 int pkey;
970 int ret;
971
972 if (sscanf(buf, "%i", &pkey) != 1)
973 return -EINVAL;
974
975 if (pkey < 0 || pkey > 0xffff)
976 return -EINVAL;
977
4ce05937
RD
978 /*
979 * Set the full membership bit, so that we join the right
980 * broadcast group, etc.
981 */
982 pkey |= 0x8000;
983
1da177e4
LT
984 ret = ipoib_vlan_add(container_of(cdev, struct net_device, class_dev),
985 pkey);
986
987 return ret ? ret : count;
988}
989static CLASS_DEVICE_ATTR(create_child, S_IWUGO, NULL, create_child);
990
991static ssize_t delete_child(struct class_device *cdev,
992 const char *buf, size_t count)
993{
994 int pkey;
995 int ret;
996
997 if (sscanf(buf, "%i", &pkey) != 1)
998 return -EINVAL;
999
1000 if (pkey < 0 || pkey > 0xffff)
1001 return -EINVAL;
1002
1003 ret = ipoib_vlan_delete(container_of(cdev, struct net_device, class_dev),
1004 pkey);
1005
1006 return ret ? ret : count;
1007
1008}
1009static CLASS_DEVICE_ATTR(delete_child, S_IWUGO, NULL, delete_child);
1010
1011int ipoib_add_pkey_attr(struct net_device *dev)
1012{
1013 return class_device_create_file(&dev->class_dev,
1014 &class_device_attr_pkey);
1015}
1016
1017static struct net_device *ipoib_add_port(const char *format,
1018 struct ib_device *hca, u8 port)
1019{
1020 struct ipoib_dev_priv *priv;
1021 int result = -ENOMEM;
1022
1023 priv = ipoib_intf_alloc(format);
1024 if (!priv)
1025 goto alloc_mem_failed;
1026
1027 SET_NETDEV_DEV(priv->dev, hca->dma_device);
1028
1029 result = ib_query_pkey(hca, port, 0, &priv->pkey);
1030 if (result) {
1031 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1032 hca->name, port, result);
1033 goto alloc_mem_failed;
1034 }
1035
4ce05937
RD
1036 /*
1037 * Set the full membership bit, so that we join the right
1038 * broadcast group, etc.
1039 */
1040 priv->pkey |= 0x8000;
1041
1da177e4
LT
1042 priv->dev->broadcast[8] = priv->pkey >> 8;
1043 priv->dev->broadcast[9] = priv->pkey & 0xff;
1044
1045 result = ib_query_gid(hca, port, 0, &priv->local_gid);
1046 if (result) {
1047 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1048 hca->name, port, result);
1049 goto alloc_mem_failed;
1050 } else
1051 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1052
1053
1054 result = ipoib_dev_init(priv->dev, hca, port);
1055 if (result < 0) {
1056 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1057 hca->name, port, result);
1058 goto device_init_failed;
1059 }
1060
1061 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1062 priv->ca, ipoib_event);
1063 result = ib_register_event_handler(&priv->event_handler);
1064 if (result < 0) {
1065 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1066 "port %d (ret = %d)\n",
1067 hca->name, port, result);
1068 goto event_failed;
1069 }
1070
1071 result = register_netdev(priv->dev);
1072 if (result) {
1073 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1074 hca->name, port, result);
1075 goto register_failed;
1076 }
1077
1732b0ef 1078 ipoib_create_debug_files(priv->dev);
1da177e4
LT
1079
1080 if (ipoib_add_pkey_attr(priv->dev))
1081 goto sysfs_failed;
1082 if (class_device_create_file(&priv->dev->class_dev,
1083 &class_device_attr_create_child))
1084 goto sysfs_failed;
1085 if (class_device_create_file(&priv->dev->class_dev,
1086 &class_device_attr_delete_child))
1087 goto sysfs_failed;
1088
1089 return priv->dev;
1090
1091sysfs_failed:
1732b0ef 1092 ipoib_delete_debug_files(priv->dev);
1da177e4
LT
1093 unregister_netdev(priv->dev);
1094
1095register_failed:
1096 ib_unregister_event_handler(&priv->event_handler);
51574e03 1097 flush_scheduled_work();
1da177e4
LT
1098
1099event_failed:
1100 ipoib_dev_cleanup(priv->dev);
1101
1102device_init_failed:
1103 free_netdev(priv->dev);
1104
1105alloc_mem_failed:
1106 return ERR_PTR(result);
1107}
1108
1109static void ipoib_add_one(struct ib_device *device)
1110{
1111 struct list_head *dev_list;
1112 struct net_device *dev;
1113 struct ipoib_dev_priv *priv;
1114 int s, e, p;
1115
07ebafba
TT
1116 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1117 return;
1118
1da177e4
LT
1119 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1120 if (!dev_list)
1121 return;
1122
1123 INIT_LIST_HEAD(dev_list);
1124
07ebafba 1125 if (device->node_type == RDMA_NODE_IB_SWITCH) {
1da177e4
LT
1126 s = 0;
1127 e = 0;
1128 } else {
1129 s = 1;
1130 e = device->phys_port_cnt;
1131 }
1132
1133 for (p = s; p <= e; ++p) {
1134 dev = ipoib_add_port("ib%d", device, p);
1135 if (!IS_ERR(dev)) {
1136 priv = netdev_priv(dev);
1137 list_add_tail(&priv->list, dev_list);
1138 }
1139 }
1140
1141 ib_set_client_data(device, &ipoib_client, dev_list);
1142}
1143
1144static void ipoib_remove_one(struct ib_device *device)
1145{
1146 struct ipoib_dev_priv *priv, *tmp;
1147 struct list_head *dev_list;
1148
07ebafba
TT
1149 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1150 return;
1151
1da177e4
LT
1152 dev_list = ib_get_client_data(device, &ipoib_client);
1153
1154 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1155 ib_unregister_event_handler(&priv->event_handler);
51574e03 1156 flush_scheduled_work();
1da177e4
LT
1157
1158 unregister_netdev(priv->dev);
1159 ipoib_dev_cleanup(priv->dev);
1160 free_netdev(priv->dev);
1161 }
06c56e44
MT
1162
1163 kfree(dev_list);
1da177e4
LT
1164}
1165
1166static int __init ipoib_init_module(void)
1167{
1168 int ret;
1169
0f485251
SM
1170 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1171 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1172 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1173
1174 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1175 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1176 ipoib_sendq_size = max(ipoib_sendq_size, IPOIB_MIN_QUEUE_SIZE);
1177
1da177e4
LT
1178 ret = ipoib_register_debugfs();
1179 if (ret)
1180 return ret;
1181
1182 /*
1183 * We create our own workqueue mainly because we want to be
1184 * able to flush it when devices are being removed. We can't
1185 * use schedule_work()/flush_scheduled_work() because both
1186 * unregister_netdev() and linkwatch_event take the rtnl lock,
1187 * so flush_scheduled_work() can deadlock during device
1188 * removal.
1189 */
1190 ipoib_workqueue = create_singlethread_workqueue("ipoib");
1191 if (!ipoib_workqueue) {
1192 ret = -ENOMEM;
1193 goto err_fs;
1194 }
1195
c1a0b23b
MT
1196 ib_sa_register_client(&ipoib_sa_client);
1197
1da177e4
LT
1198 ret = ib_register_client(&ipoib_client);
1199 if (ret)
c1a0b23b 1200 goto err_sa;
1da177e4
LT
1201
1202 return 0;
1203
c1a0b23b
MT
1204err_sa:
1205 ib_sa_unregister_client(&ipoib_sa_client);
1da177e4
LT
1206 destroy_workqueue(ipoib_workqueue);
1207
9adec1a8
RD
1208err_fs:
1209 ipoib_unregister_debugfs();
1210
1da177e4
LT
1211 return ret;
1212}
1213
1214static void __exit ipoib_cleanup_module(void)
1215{
1da177e4 1216 ib_unregister_client(&ipoib_client);
c1a0b23b 1217 ib_sa_unregister_client(&ipoib_sa_client);
9adec1a8 1218 ipoib_unregister_debugfs();
1da177e4
LT
1219 destroy_workqueue(ipoib_workqueue);
1220}
1221
1222module_init(ipoib_init_module);
1223module_exit(ipoib_cleanup_module);
This page took 0.194263 seconds and 5 git commands to generate.