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