]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/infiniband/ulp/ipoib/ipoib_main.c
[IPoIB] add path record information in debugfs
[mirror_ubuntu-bionic-kernel.git] / drivers / infiniband / ulp / ipoib / ipoib_main.c
1 /*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
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
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
50 MODULE_AUTHOR("Roland Dreier");
51 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
52 MODULE_LICENSE("Dual BSD/GPL");
53
54 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
55 int ipoib_debug_level;
56
57 module_param_named(debug_level, ipoib_debug_level, int, 0644);
58 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
59 #endif
60
61 struct ipoib_path_iter {
62 struct net_device *dev;
63 struct ipoib_path path;
64 };
65
66 static 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
72 struct workqueue_struct *ipoib_workqueue;
73
74 static void ipoib_add_one(struct ib_device *device);
75 static void ipoib_remove_one(struct ib_device *device);
76
77 static struct ib_client ipoib_client = {
78 .name = "ipoib",
79 .add = ipoib_add_one,
80 .remove = ipoib_remove_one
81 };
82
83 int 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
122 static 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
155 static 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
169 static 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
194 static 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
224 static 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
258 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
259
260 struct 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
279 int 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
308 void 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
316 void 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
341 static 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 };
369 int path_rate = ib_sa_rate_enum_to_int(pathrec->rate);
370
371 if (path_rate > 0 && priv->local_rate > path_rate)
372 av.static_rate = (priv->local_rate - 1) / path_rate;
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
416 static 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
422 path = kzalloc(sizeof *path, GFP_ATOMIC);
423 if (!path)
424 return NULL;
425
426 path->dev = dev;
427
428 skb_queue_head_init(&path->queue);
429
430 INIT_LIST_HEAD(&path->neigh_list);
431 init_completion(&path->done);
432
433 memcpy(path->pathrec.dgid.raw, gid->raw, sizeof (union ib_gid));
434 path->pathrec.sgid = priv->local_gid;
435 path->pathrec.pkey = cpu_to_be16(priv->pkey);
436 path->pathrec.numb_path = 1;
437
438 return path;
439 }
440
441 static int path_rec_start(struct net_device *dev,
442 struct ipoib_path *path)
443 {
444 struct ipoib_dev_priv *priv = netdev_priv(dev);
445
446 ipoib_dbg(priv, "Start path record lookup for " IPOIB_GID_FMT "\n",
447 IPOIB_GID_ARG(path->pathrec.dgid));
448
449 path->query_id =
450 ib_sa_path_rec_get(priv->ca, priv->port,
451 &path->pathrec,
452 IB_SA_PATH_REC_DGID |
453 IB_SA_PATH_REC_SGID |
454 IB_SA_PATH_REC_NUMB_PATH |
455 IB_SA_PATH_REC_PKEY,
456 1000, GFP_ATOMIC,
457 path_rec_completion,
458 path, &path->query);
459 if (path->query_id < 0) {
460 ipoib_warn(priv, "ib_sa_path_rec_get failed\n");
461 path->query = NULL;
462 return path->query_id;
463 }
464
465 return 0;
466 }
467
468 static void neigh_add_path(struct sk_buff *skb, struct net_device *dev)
469 {
470 struct ipoib_dev_priv *priv = netdev_priv(dev);
471 struct ipoib_path *path;
472 struct ipoib_neigh *neigh;
473
474 neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
475 if (!neigh) {
476 ++priv->stats.tx_dropped;
477 dev_kfree_skb_any(skb);
478 return;
479 }
480
481 skb_queue_head_init(&neigh->queue);
482 neigh->neighbour = skb->dst->neighbour;
483 *to_ipoib_neigh(skb->dst->neighbour) = neigh;
484
485 /*
486 * We can only be called from ipoib_start_xmit, so we're
487 * inside tx_lock -- no need to save/restore flags.
488 */
489 spin_lock(&priv->lock);
490
491 path = __path_find(dev, (union ib_gid *) (skb->dst->neighbour->ha + 4));
492 if (!path) {
493 path = path_rec_create(dev,
494 (union ib_gid *) (skb->dst->neighbour->ha + 4));
495 if (!path)
496 goto err;
497
498 __path_add(dev, path);
499 }
500
501 list_add_tail(&neigh->list, &path->neigh_list);
502
503 if (path->pathrec.dlid) {
504 kref_get(&path->ah->ref);
505 neigh->ah = path->ah;
506
507 ipoib_send(dev, skb, path->ah,
508 be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
509 } else {
510 neigh->ah = NULL;
511 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
512 __skb_queue_tail(&neigh->queue, skb);
513 } else {
514 ++priv->stats.tx_dropped;
515 dev_kfree_skb_any(skb);
516 }
517
518 if (!path->query && path_rec_start(dev, path))
519 goto err;
520 }
521
522 spin_unlock(&priv->lock);
523 return;
524
525 err:
526 *to_ipoib_neigh(skb->dst->neighbour) = NULL;
527 list_del(&neigh->list);
528 neigh->neighbour->ops->destructor = NULL;
529 kfree(neigh);
530
531 ++priv->stats.tx_dropped;
532 dev_kfree_skb_any(skb);
533
534 spin_unlock(&priv->lock);
535 }
536
537 static void ipoib_path_lookup(struct sk_buff *skb, struct net_device *dev)
538 {
539 struct ipoib_dev_priv *priv = netdev_priv(skb->dev);
540
541 /* Look up path record for unicasts */
542 if (skb->dst->neighbour->ha[4] != 0xff) {
543 neigh_add_path(skb, dev);
544 return;
545 }
546
547 /* Add in the P_Key for multicasts */
548 skb->dst->neighbour->ha[8] = (priv->pkey >> 8) & 0xff;
549 skb->dst->neighbour->ha[9] = priv->pkey & 0xff;
550 ipoib_mcast_send(dev, (union ib_gid *) (skb->dst->neighbour->ha + 4), skb);
551 }
552
553 static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
554 struct ipoib_pseudoheader *phdr)
555 {
556 struct ipoib_dev_priv *priv = netdev_priv(dev);
557 struct ipoib_path *path;
558
559 /*
560 * We can only be called from ipoib_start_xmit, so we're
561 * inside tx_lock -- no need to save/restore flags.
562 */
563 spin_lock(&priv->lock);
564
565 path = __path_find(dev, (union ib_gid *) (phdr->hwaddr + 4));
566 if (!path) {
567 path = path_rec_create(dev,
568 (union ib_gid *) (phdr->hwaddr + 4));
569 if (path) {
570 /* put pseudoheader back on for next time */
571 skb_push(skb, sizeof *phdr);
572 __skb_queue_tail(&path->queue, skb);
573
574 if (path_rec_start(dev, path)) {
575 spin_unlock(&priv->lock);
576 path_free(dev, path);
577 return;
578 } else
579 __path_add(dev, path);
580 } else {
581 ++priv->stats.tx_dropped;
582 dev_kfree_skb_any(skb);
583 }
584
585 spin_unlock(&priv->lock);
586 return;
587 }
588
589 if (path->pathrec.dlid) {
590 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
591 be16_to_cpu(path->pathrec.dlid));
592
593 ipoib_send(dev, skb, path->ah,
594 be32_to_cpup((__be32 *) phdr->hwaddr));
595 } else if ((path->query || !path_rec_start(dev, path)) &&
596 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
597 /* put pseudoheader back on for next time */
598 skb_push(skb, sizeof *phdr);
599 __skb_queue_tail(&path->queue, skb);
600 } else {
601 ++priv->stats.tx_dropped;
602 dev_kfree_skb_any(skb);
603 }
604
605 spin_unlock(&priv->lock);
606 }
607
608 static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
609 {
610 struct ipoib_dev_priv *priv = netdev_priv(dev);
611 struct ipoib_neigh *neigh;
612 unsigned long flags;
613
614 if (!spin_trylock_irqsave(&priv->tx_lock, flags))
615 return NETDEV_TX_LOCKED;
616
617 /*
618 * Check if our queue is stopped. Since we have the LLTX bit
619 * set, we can't rely on netif_stop_queue() preventing our
620 * xmit function from being called with a full queue.
621 */
622 if (unlikely(netif_queue_stopped(dev))) {
623 spin_unlock_irqrestore(&priv->tx_lock, flags);
624 return NETDEV_TX_BUSY;
625 }
626
627 if (skb->dst && skb->dst->neighbour) {
628 if (unlikely(!*to_ipoib_neigh(skb->dst->neighbour))) {
629 ipoib_path_lookup(skb, dev);
630 goto out;
631 }
632
633 neigh = *to_ipoib_neigh(skb->dst->neighbour);
634
635 if (likely(neigh->ah)) {
636 ipoib_send(dev, skb, neigh->ah,
637 be32_to_cpup((__be32 *) skb->dst->neighbour->ha));
638 goto out;
639 }
640
641 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
642 spin_lock(&priv->lock);
643 __skb_queue_tail(&neigh->queue, skb);
644 spin_unlock(&priv->lock);
645 } else {
646 ++priv->stats.tx_dropped;
647 dev_kfree_skb_any(skb);
648 }
649 } else {
650 struct ipoib_pseudoheader *phdr =
651 (struct ipoib_pseudoheader *) skb->data;
652 skb_pull(skb, sizeof *phdr);
653
654 if (phdr->hwaddr[4] == 0xff) {
655 /* Add in the P_Key for multicast*/
656 phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
657 phdr->hwaddr[9] = priv->pkey & 0xff;
658
659 ipoib_mcast_send(dev, (union ib_gid *) (phdr->hwaddr + 4), skb);
660 } else {
661 /* unicast GID -- should be ARP or RARP reply */
662
663 if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
664 (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
665 ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x "
666 IPOIB_GID_FMT "\n",
667 skb->dst ? "neigh" : "dst",
668 be16_to_cpup((__be16 *) skb->data),
669 be32_to_cpup((__be32 *) phdr->hwaddr),
670 IPOIB_GID_ARG(*(union ib_gid *) (phdr->hwaddr + 4)));
671 dev_kfree_skb_any(skb);
672 ++priv->stats.tx_dropped;
673 goto out;
674 }
675
676 unicast_arp_send(skb, dev, phdr);
677 }
678 }
679
680 out:
681 spin_unlock_irqrestore(&priv->tx_lock, flags);
682
683 return NETDEV_TX_OK;
684 }
685
686 static struct net_device_stats *ipoib_get_stats(struct net_device *dev)
687 {
688 struct ipoib_dev_priv *priv = netdev_priv(dev);
689
690 return &priv->stats;
691 }
692
693 static void ipoib_timeout(struct net_device *dev)
694 {
695 struct ipoib_dev_priv *priv = netdev_priv(dev);
696
697 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
698 jiffies_to_msecs(jiffies - dev->trans_start));
699 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
700 netif_queue_stopped(dev),
701 priv->tx_head, priv->tx_tail);
702 /* XXX reset QP, etc. */
703 }
704
705 static int ipoib_hard_header(struct sk_buff *skb,
706 struct net_device *dev,
707 unsigned short type,
708 void *daddr, void *saddr, unsigned len)
709 {
710 struct ipoib_header *header;
711
712 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
713
714 header->proto = htons(type);
715 header->reserved = 0;
716
717 /*
718 * If we don't have a neighbour structure, stuff the
719 * destination address onto the front of the skb so we can
720 * figure out where to send the packet later.
721 */
722 if (!skb->dst || !skb->dst->neighbour) {
723 struct ipoib_pseudoheader *phdr =
724 (struct ipoib_pseudoheader *) skb_push(skb, sizeof *phdr);
725 memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
726 }
727
728 return 0;
729 }
730
731 static void ipoib_set_mcast_list(struct net_device *dev)
732 {
733 struct ipoib_dev_priv *priv = netdev_priv(dev);
734
735 queue_work(ipoib_workqueue, &priv->restart_task);
736 }
737
738 static void ipoib_neigh_destructor(struct neighbour *n)
739 {
740 struct ipoib_neigh *neigh;
741 struct ipoib_dev_priv *priv = netdev_priv(n->dev);
742 unsigned long flags;
743 struct ipoib_ah *ah = NULL;
744
745 ipoib_dbg(priv,
746 "neigh_destructor for %06x " IPOIB_GID_FMT "\n",
747 be32_to_cpup((__be32 *) n->ha),
748 IPOIB_GID_ARG(*((union ib_gid *) (n->ha + 4))));
749
750 spin_lock_irqsave(&priv->lock, flags);
751
752 neigh = *to_ipoib_neigh(n);
753 if (neigh) {
754 if (neigh->ah)
755 ah = neigh->ah;
756 list_del(&neigh->list);
757 *to_ipoib_neigh(n) = NULL;
758 kfree(neigh);
759 }
760
761 spin_unlock_irqrestore(&priv->lock, flags);
762
763 if (ah)
764 ipoib_put_ah(ah);
765 }
766
767 static int ipoib_neigh_setup(struct neighbour *neigh)
768 {
769 /*
770 * Is this kosher? I can't find anybody in the kernel that
771 * sets neigh->destructor, so we should be able to set it here
772 * without trouble.
773 */
774 neigh->ops->destructor = ipoib_neigh_destructor;
775
776 return 0;
777 }
778
779 static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
780 {
781 parms->neigh_setup = ipoib_neigh_setup;
782
783 return 0;
784 }
785
786 int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
787 {
788 struct ipoib_dev_priv *priv = netdev_priv(dev);
789
790 /* Allocate RX/TX "rings" to hold queued skbs */
791
792 priv->rx_ring = kzalloc(IPOIB_RX_RING_SIZE * sizeof (struct ipoib_rx_buf),
793 GFP_KERNEL);
794 if (!priv->rx_ring) {
795 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
796 ca->name, IPOIB_RX_RING_SIZE);
797 goto out;
798 }
799
800 priv->tx_ring = kzalloc(IPOIB_TX_RING_SIZE * sizeof (struct ipoib_tx_buf),
801 GFP_KERNEL);
802 if (!priv->tx_ring) {
803 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
804 ca->name, IPOIB_TX_RING_SIZE);
805 goto out_rx_ring_cleanup;
806 }
807
808 /* priv->tx_head & tx_tail are already 0 */
809
810 if (ipoib_ib_dev_init(dev, ca, port))
811 goto out_tx_ring_cleanup;
812
813 return 0;
814
815 out_tx_ring_cleanup:
816 kfree(priv->tx_ring);
817
818 out_rx_ring_cleanup:
819 kfree(priv->rx_ring);
820
821 out:
822 return -ENOMEM;
823 }
824
825 void ipoib_dev_cleanup(struct net_device *dev)
826 {
827 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
828
829 ipoib_delete_debug_files(dev);
830
831 /* Delete any child interfaces first */
832 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
833 unregister_netdev(cpriv->dev);
834 ipoib_dev_cleanup(cpriv->dev);
835 free_netdev(cpriv->dev);
836 }
837
838 ipoib_ib_dev_cleanup(dev);
839
840 kfree(priv->rx_ring);
841 kfree(priv->tx_ring);
842
843 priv->rx_ring = NULL;
844 priv->tx_ring = NULL;
845 }
846
847 static void ipoib_setup(struct net_device *dev)
848 {
849 struct ipoib_dev_priv *priv = netdev_priv(dev);
850
851 dev->open = ipoib_open;
852 dev->stop = ipoib_stop;
853 dev->change_mtu = ipoib_change_mtu;
854 dev->hard_start_xmit = ipoib_start_xmit;
855 dev->get_stats = ipoib_get_stats;
856 dev->tx_timeout = ipoib_timeout;
857 dev->hard_header = ipoib_hard_header;
858 dev->set_multicast_list = ipoib_set_mcast_list;
859 dev->neigh_setup = ipoib_neigh_setup_dev;
860
861 dev->watchdog_timeo = HZ;
862
863 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
864
865 /*
866 * We add in INFINIBAND_ALEN to allow for the destination
867 * address "pseudoheader" for skbs without neighbour struct.
868 */
869 dev->hard_header_len = IPOIB_ENCAP_LEN + INFINIBAND_ALEN;
870 dev->addr_len = INFINIBAND_ALEN;
871 dev->type = ARPHRD_INFINIBAND;
872 dev->tx_queue_len = IPOIB_TX_RING_SIZE * 2;
873 dev->features = NETIF_F_VLAN_CHALLENGED | NETIF_F_LLTX;
874
875 /* MTU will be reset when mcast join happens */
876 dev->mtu = IPOIB_PACKET_SIZE - IPOIB_ENCAP_LEN;
877 priv->mcast_mtu = priv->admin_mtu = dev->mtu;
878
879 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
880
881 netif_carrier_off(dev);
882
883 SET_MODULE_OWNER(dev);
884
885 priv->dev = dev;
886
887 spin_lock_init(&priv->lock);
888 spin_lock_init(&priv->tx_lock);
889
890 init_MUTEX(&priv->mcast_mutex);
891 init_MUTEX(&priv->vlan_mutex);
892
893 INIT_LIST_HEAD(&priv->path_list);
894 INIT_LIST_HEAD(&priv->child_intfs);
895 INIT_LIST_HEAD(&priv->dead_ahs);
896 INIT_LIST_HEAD(&priv->multicast_list);
897
898 INIT_WORK(&priv->pkey_task, ipoib_pkey_poll, priv->dev);
899 INIT_WORK(&priv->mcast_task, ipoib_mcast_join_task, priv->dev);
900 INIT_WORK(&priv->flush_task, ipoib_ib_dev_flush, priv->dev);
901 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task, priv->dev);
902 INIT_WORK(&priv->ah_reap_task, ipoib_reap_ah, priv->dev);
903 }
904
905 struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
906 {
907 struct net_device *dev;
908
909 dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
910 ipoib_setup);
911 if (!dev)
912 return NULL;
913
914 return netdev_priv(dev);
915 }
916
917 static ssize_t show_pkey(struct class_device *cdev, char *buf)
918 {
919 struct ipoib_dev_priv *priv =
920 netdev_priv(container_of(cdev, struct net_device, class_dev));
921
922 return sprintf(buf, "0x%04x\n", priv->pkey);
923 }
924 static CLASS_DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
925
926 static ssize_t create_child(struct class_device *cdev,
927 const char *buf, size_t count)
928 {
929 int pkey;
930 int ret;
931
932 if (sscanf(buf, "%i", &pkey) != 1)
933 return -EINVAL;
934
935 if (pkey < 0 || pkey > 0xffff)
936 return -EINVAL;
937
938 /*
939 * Set the full membership bit, so that we join the right
940 * broadcast group, etc.
941 */
942 pkey |= 0x8000;
943
944 ret = ipoib_vlan_add(container_of(cdev, struct net_device, class_dev),
945 pkey);
946
947 return ret ? ret : count;
948 }
949 static CLASS_DEVICE_ATTR(create_child, S_IWUGO, NULL, create_child);
950
951 static ssize_t delete_child(struct class_device *cdev,
952 const char *buf, size_t count)
953 {
954 int pkey;
955 int ret;
956
957 if (sscanf(buf, "%i", &pkey) != 1)
958 return -EINVAL;
959
960 if (pkey < 0 || pkey > 0xffff)
961 return -EINVAL;
962
963 ret = ipoib_vlan_delete(container_of(cdev, struct net_device, class_dev),
964 pkey);
965
966 return ret ? ret : count;
967
968 }
969 static CLASS_DEVICE_ATTR(delete_child, S_IWUGO, NULL, delete_child);
970
971 int ipoib_add_pkey_attr(struct net_device *dev)
972 {
973 return class_device_create_file(&dev->class_dev,
974 &class_device_attr_pkey);
975 }
976
977 static struct net_device *ipoib_add_port(const char *format,
978 struct ib_device *hca, u8 port)
979 {
980 struct ipoib_dev_priv *priv;
981 int result = -ENOMEM;
982
983 priv = ipoib_intf_alloc(format);
984 if (!priv)
985 goto alloc_mem_failed;
986
987 SET_NETDEV_DEV(priv->dev, hca->dma_device);
988
989 result = ib_query_pkey(hca, port, 0, &priv->pkey);
990 if (result) {
991 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
992 hca->name, port, result);
993 goto alloc_mem_failed;
994 }
995
996 /*
997 * Set the full membership bit, so that we join the right
998 * broadcast group, etc.
999 */
1000 priv->pkey |= 0x8000;
1001
1002 priv->dev->broadcast[8] = priv->pkey >> 8;
1003 priv->dev->broadcast[9] = priv->pkey & 0xff;
1004
1005 result = ib_query_gid(hca, port, 0, &priv->local_gid);
1006 if (result) {
1007 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1008 hca->name, port, result);
1009 goto alloc_mem_failed;
1010 } else
1011 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1012
1013
1014 result = ipoib_dev_init(priv->dev, hca, port);
1015 if (result < 0) {
1016 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1017 hca->name, port, result);
1018 goto device_init_failed;
1019 }
1020
1021 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1022 priv->ca, ipoib_event);
1023 result = ib_register_event_handler(&priv->event_handler);
1024 if (result < 0) {
1025 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1026 "port %d (ret = %d)\n",
1027 hca->name, port, result);
1028 goto event_failed;
1029 }
1030
1031 result = register_netdev(priv->dev);
1032 if (result) {
1033 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1034 hca->name, port, result);
1035 goto register_failed;
1036 }
1037
1038 ipoib_create_debug_files(priv->dev);
1039
1040 if (ipoib_add_pkey_attr(priv->dev))
1041 goto sysfs_failed;
1042 if (class_device_create_file(&priv->dev->class_dev,
1043 &class_device_attr_create_child))
1044 goto sysfs_failed;
1045 if (class_device_create_file(&priv->dev->class_dev,
1046 &class_device_attr_delete_child))
1047 goto sysfs_failed;
1048
1049 return priv->dev;
1050
1051 sysfs_failed:
1052 ipoib_delete_debug_files(priv->dev);
1053 unregister_netdev(priv->dev);
1054
1055 register_failed:
1056 ib_unregister_event_handler(&priv->event_handler);
1057 flush_scheduled_work();
1058
1059 event_failed:
1060 ipoib_dev_cleanup(priv->dev);
1061
1062 device_init_failed:
1063 free_netdev(priv->dev);
1064
1065 alloc_mem_failed:
1066 return ERR_PTR(result);
1067 }
1068
1069 static void ipoib_add_one(struct ib_device *device)
1070 {
1071 struct list_head *dev_list;
1072 struct net_device *dev;
1073 struct ipoib_dev_priv *priv;
1074 int s, e, p;
1075
1076 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1077 if (!dev_list)
1078 return;
1079
1080 INIT_LIST_HEAD(dev_list);
1081
1082 if (device->node_type == IB_NODE_SWITCH) {
1083 s = 0;
1084 e = 0;
1085 } else {
1086 s = 1;
1087 e = device->phys_port_cnt;
1088 }
1089
1090 for (p = s; p <= e; ++p) {
1091 dev = ipoib_add_port("ib%d", device, p);
1092 if (!IS_ERR(dev)) {
1093 priv = netdev_priv(dev);
1094 list_add_tail(&priv->list, dev_list);
1095 }
1096 }
1097
1098 ib_set_client_data(device, &ipoib_client, dev_list);
1099 }
1100
1101 static void ipoib_remove_one(struct ib_device *device)
1102 {
1103 struct ipoib_dev_priv *priv, *tmp;
1104 struct list_head *dev_list;
1105
1106 dev_list = ib_get_client_data(device, &ipoib_client);
1107
1108 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1109 ib_unregister_event_handler(&priv->event_handler);
1110 flush_scheduled_work();
1111
1112 unregister_netdev(priv->dev);
1113 ipoib_dev_cleanup(priv->dev);
1114 free_netdev(priv->dev);
1115 }
1116
1117 kfree(dev_list);
1118 }
1119
1120 static int __init ipoib_init_module(void)
1121 {
1122 int ret;
1123
1124 ret = ipoib_register_debugfs();
1125 if (ret)
1126 return ret;
1127
1128 /*
1129 * We create our own workqueue mainly because we want to be
1130 * able to flush it when devices are being removed. We can't
1131 * use schedule_work()/flush_scheduled_work() because both
1132 * unregister_netdev() and linkwatch_event take the rtnl lock,
1133 * so flush_scheduled_work() can deadlock during device
1134 * removal.
1135 */
1136 ipoib_workqueue = create_singlethread_workqueue("ipoib");
1137 if (!ipoib_workqueue) {
1138 ret = -ENOMEM;
1139 goto err_fs;
1140 }
1141
1142 ret = ib_register_client(&ipoib_client);
1143 if (ret)
1144 goto err_wq;
1145
1146 return 0;
1147
1148 err_wq:
1149 destroy_workqueue(ipoib_workqueue);
1150
1151 err_fs:
1152 ipoib_unregister_debugfs();
1153
1154 return ret;
1155 }
1156
1157 static void __exit ipoib_cleanup_module(void)
1158 {
1159 ib_unregister_client(&ipoib_client);
1160 ipoib_unregister_debugfs();
1161 destroy_workqueue(ipoib_workqueue);
1162 }
1163
1164 module_init(ipoib_init_module);
1165 module_exit(ipoib_cleanup_module);