RDMA: Remove subversion $Id tags
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / infiniband / ulp / ipoib / ipoib_main.c
blobf217b1edd0acef1600d5fb18f78f9921aad2fab7
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.
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:
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
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.
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.
35 #include "ipoib.h"
37 #include <linux/module.h>
39 #include <linux/init.h>
40 #include <linux/slab.h>
41 #include <linux/kernel.h>
42 #include <linux/vmalloc.h>
44 #include <linux/if_arp.h> /* For ARPHRD_xxx */
46 #include <linux/ip.h>
47 #include <linux/in.h>
49 #include <net/dst.h>
51 MODULE_AUTHOR("Roland Dreier");
52 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
53 MODULE_LICENSE("Dual BSD/GPL");
55 int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
56 int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
58 module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
59 MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
60 module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
61 MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
63 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
64 int ipoib_debug_level;
66 module_param_named(debug_level, ipoib_debug_level, int, 0644);
67 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
68 #endif
70 struct ipoib_path_iter {
71 struct net_device *dev;
72 struct ipoib_path path;
75 static const u8 ipv4_bcast_addr[] = {
76 0x00, 0xff, 0xff, 0xff,
77 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
78 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
81 struct workqueue_struct *ipoib_workqueue;
83 struct ib_sa_client ipoib_sa_client;
85 static void ipoib_add_one(struct ib_device *device);
86 static void ipoib_remove_one(struct ib_device *device);
88 static struct ib_client ipoib_client = {
89 .name = "ipoib",
90 .add = ipoib_add_one,
91 .remove = ipoib_remove_one
94 int ipoib_open(struct net_device *dev)
96 struct ipoib_dev_priv *priv = netdev_priv(dev);
98 ipoib_dbg(priv, "bringing up interface\n");
100 napi_enable(&priv->napi);
101 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
103 if (ipoib_pkey_dev_delay_open(dev))
104 return 0;
106 if (ipoib_ib_dev_open(dev)) {
107 napi_disable(&priv->napi);
108 return -EINVAL;
111 if (ipoib_ib_dev_up(dev)) {
112 ipoib_ib_dev_stop(dev, 1);
113 napi_disable(&priv->napi);
114 return -EINVAL;
117 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
118 struct ipoib_dev_priv *cpriv;
120 /* Bring up any child interfaces too */
121 mutex_lock(&priv->vlan_mutex);
122 list_for_each_entry(cpriv, &priv->child_intfs, list) {
123 int flags;
125 flags = cpriv->dev->flags;
126 if (flags & IFF_UP)
127 continue;
129 dev_change_flags(cpriv->dev, flags | IFF_UP);
131 mutex_unlock(&priv->vlan_mutex);
134 netif_start_queue(dev);
136 return 0;
139 static int ipoib_stop(struct net_device *dev)
141 struct ipoib_dev_priv *priv = netdev_priv(dev);
143 ipoib_dbg(priv, "stopping interface\n");
145 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
146 napi_disable(&priv->napi);
148 netif_stop_queue(dev);
151 * Now flush workqueue to make sure a scheduled task doesn't
152 * bring our internal state back up.
154 flush_workqueue(ipoib_workqueue);
156 ipoib_ib_dev_down(dev, 1);
157 ipoib_ib_dev_stop(dev, 1);
159 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
160 struct ipoib_dev_priv *cpriv;
162 /* Bring down any child interfaces too */
163 mutex_lock(&priv->vlan_mutex);
164 list_for_each_entry(cpriv, &priv->child_intfs, list) {
165 int flags;
167 flags = cpriv->dev->flags;
168 if (!(flags & IFF_UP))
169 continue;
171 dev_change_flags(cpriv->dev, flags & ~IFF_UP);
173 mutex_unlock(&priv->vlan_mutex);
176 return 0;
179 static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
181 struct ipoib_dev_priv *priv = netdev_priv(dev);
183 /* dev->mtu > 2K ==> connected mode */
184 if (ipoib_cm_admin_enabled(dev)) {
185 if (new_mtu > ipoib_cm_max_mtu(dev))
186 return -EINVAL;
188 if (new_mtu > priv->mcast_mtu)
189 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
190 priv->mcast_mtu);
192 dev->mtu = new_mtu;
193 return 0;
196 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
197 return -EINVAL;
199 priv->admin_mtu = new_mtu;
201 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
203 return 0;
206 static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
208 struct ipoib_dev_priv *priv = netdev_priv(dev);
209 struct rb_node *n = priv->path_tree.rb_node;
210 struct ipoib_path *path;
211 int ret;
213 while (n) {
214 path = rb_entry(n, struct ipoib_path, rb_node);
216 ret = memcmp(gid, path->pathrec.dgid.raw,
217 sizeof (union ib_gid));
219 if (ret < 0)
220 n = n->rb_left;
221 else if (ret > 0)
222 n = n->rb_right;
223 else
224 return path;
227 return NULL;
230 static int __path_add(struct net_device *dev, struct ipoib_path *path)
232 struct ipoib_dev_priv *priv = netdev_priv(dev);
233 struct rb_node **n = &priv->path_tree.rb_node;
234 struct rb_node *pn = NULL;
235 struct ipoib_path *tpath;
236 int ret;
238 while (*n) {
239 pn = *n;
240 tpath = rb_entry(pn, struct ipoib_path, rb_node);
242 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
243 sizeof (union ib_gid));
244 if (ret < 0)
245 n = &pn->rb_left;
246 else if (ret > 0)
247 n = &pn->rb_right;
248 else
249 return -EEXIST;
252 rb_link_node(&path->rb_node, pn, n);
253 rb_insert_color(&path->rb_node, &priv->path_tree);
255 list_add_tail(&path->list, &priv->path_list);
257 return 0;
260 static void path_free(struct net_device *dev, struct ipoib_path *path)
262 struct ipoib_dev_priv *priv = netdev_priv(dev);
263 struct ipoib_neigh *neigh, *tn;
264 struct sk_buff *skb;
265 unsigned long flags;
267 while ((skb = __skb_dequeue(&path->queue)))
268 dev_kfree_skb_irq(skb);
270 spin_lock_irqsave(&priv->lock, flags);
272 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
274 * It's safe to call ipoib_put_ah() inside priv->lock
275 * here, because we know that path->ah will always
276 * hold one more reference, so ipoib_put_ah() will
277 * never do more than decrement the ref count.
279 if (neigh->ah)
280 ipoib_put_ah(neigh->ah);
282 ipoib_neigh_free(dev, neigh);
285 spin_unlock_irqrestore(&priv->lock, flags);
287 if (path->ah)
288 ipoib_put_ah(path->ah);
290 kfree(path);
293 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
295 struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
297 struct ipoib_path_iter *iter;
299 iter = kmalloc(sizeof *iter, GFP_KERNEL);
300 if (!iter)
301 return NULL;
303 iter->dev = dev;
304 memset(iter->path.pathrec.dgid.raw, 0, 16);
306 if (ipoib_path_iter_next(iter)) {
307 kfree(iter);
308 return NULL;
311 return iter;
314 int ipoib_path_iter_next(struct ipoib_path_iter *iter)
316 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
317 struct rb_node *n;
318 struct ipoib_path *path;
319 int ret = 1;
321 spin_lock_irq(&priv->lock);
323 n = rb_first(&priv->path_tree);
325 while (n) {
326 path = rb_entry(n, struct ipoib_path, rb_node);
328 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
329 sizeof (union ib_gid)) < 0) {
330 iter->path = *path;
331 ret = 0;
332 break;
335 n = rb_next(n);
338 spin_unlock_irq(&priv->lock);
340 return ret;
343 void ipoib_path_iter_read(struct ipoib_path_iter *iter,
344 struct ipoib_path *path)
346 *path = iter->path;
349 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
351 void ipoib_flush_paths(struct net_device *dev)
353 struct ipoib_dev_priv *priv = netdev_priv(dev);
354 struct ipoib_path *path, *tp;
355 LIST_HEAD(remove_list);
357 spin_lock_irq(&priv->tx_lock);
358 spin_lock(&priv->lock);
360 list_splice_init(&priv->path_list, &remove_list);
362 list_for_each_entry(path, &remove_list, list)
363 rb_erase(&path->rb_node, &priv->path_tree);
365 list_for_each_entry_safe(path, tp, &remove_list, list) {
366 if (path->query)
367 ib_sa_cancel_query(path->query_id, path->query);
368 spin_unlock(&priv->lock);
369 spin_unlock_irq(&priv->tx_lock);
370 wait_for_completion(&path->done);
371 path_free(dev, path);
372 spin_lock_irq(&priv->tx_lock);
373 spin_lock(&priv->lock);
375 spin_unlock(&priv->lock);
376 spin_unlock_irq(&priv->tx_lock);
379 static void path_rec_completion(int status,
380 struct ib_sa_path_rec *pathrec,
381 void *path_ptr)
383 struct ipoib_path *path = path_ptr;
384 struct net_device *dev = path->dev;
385 struct ipoib_dev_priv *priv = netdev_priv(dev);
386 struct ipoib_ah *ah = NULL;
387 struct ipoib_neigh *neigh, *tn;
388 struct sk_buff_head skqueue;
389 struct sk_buff *skb;
390 unsigned long flags;
392 if (!status)
393 ipoib_dbg(priv, "PathRec LID 0x%04x for GID " IPOIB_GID_FMT "\n",
394 be16_to_cpu(pathrec->dlid), IPOIB_GID_ARG(pathrec->dgid));
395 else
396 ipoib_dbg(priv, "PathRec status %d for GID " IPOIB_GID_FMT "\n",
397 status, IPOIB_GID_ARG(path->pathrec.dgid));
399 skb_queue_head_init(&skqueue);
401 if (!status) {
402 struct ib_ah_attr av;
404 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
405 ah = ipoib_create_ah(dev, priv->pd, &av);
408 spin_lock_irqsave(&priv->lock, flags);
410 path->ah = ah;
412 if (ah) {
413 path->pathrec = *pathrec;
415 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
416 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
418 while ((skb = __skb_dequeue(&path->queue)))
419 __skb_queue_tail(&skqueue, skb);
421 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
422 kref_get(&path->ah->ref);
423 neigh->ah = path->ah;
424 memcpy(&neigh->dgid.raw, &path->pathrec.dgid.raw,
425 sizeof(union ib_gid));
427 if (ipoib_cm_enabled(dev, neigh->neighbour)) {
428 if (!ipoib_cm_get(neigh))
429 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
430 path,
431 neigh));
432 if (!ipoib_cm_get(neigh)) {
433 list_del(&neigh->list);
434 if (neigh->ah)
435 ipoib_put_ah(neigh->ah);
436 ipoib_neigh_free(dev, neigh);
437 continue;
441 while ((skb = __skb_dequeue(&neigh->queue)))
442 __skb_queue_tail(&skqueue, skb);
446 path->query = NULL;
447 complete(&path->done);
449 spin_unlock_irqrestore(&priv->lock, flags);
451 while ((skb = __skb_dequeue(&skqueue))) {
452 skb->dev = dev;
453 if (dev_queue_xmit(skb))
454 ipoib_warn(priv, "dev_queue_xmit failed "
455 "to requeue packet\n");
459 static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
461 struct ipoib_dev_priv *priv = netdev_priv(dev);
462 struct ipoib_path *path;
464 if (!priv->broadcast)
465 return NULL;
467 path = kzalloc(sizeof *path, GFP_ATOMIC);
468 if (!path)
469 return NULL;
471 path->dev = dev;
473 skb_queue_head_init(&path->queue);
475 INIT_LIST_HEAD(&path->neigh_list);
477 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
478 path->pathrec.sgid = priv->local_gid;
479 path->pathrec.pkey = cpu_to_be16(priv->pkey);
480 path->pathrec.numb_path = 1;
481 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
483 return path;
486 static int path_rec_start(struct net_device *dev,
487 struct ipoib_path *path)
489 struct ipoib_dev_priv *priv = netdev_priv(dev);
491 ipoib_dbg(priv, "Start path record lookup for " IPOIB_GID_FMT "\n",
492 IPOIB_GID_ARG(path->pathrec.dgid));
494 init_completion(&path->done);
496 path->query_id =
497 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
498 &path->pathrec,
499 IB_SA_PATH_REC_DGID |
500 IB_SA_PATH_REC_SGID |
501 IB_SA_PATH_REC_NUMB_PATH |
502 IB_SA_PATH_REC_TRAFFIC_CLASS |
503 IB_SA_PATH_REC_PKEY,
504 1000, GFP_ATOMIC,
505 path_rec_completion,
506 path, &path->query);
507 if (path->query_id < 0) {
508 ipoib_warn(priv, "ib_sa_path_rec_get failed\n");
509 path->query = NULL;
510 return path->query_id;
513 return 0;
516 static void neigh_add_path(struct sk_buff *skb, struct net_device *dev)
518 struct ipoib_dev_priv *priv = netdev_priv(dev);
519 struct ipoib_path *path;
520 struct ipoib_neigh *neigh;
522 neigh = ipoib_neigh_alloc(skb->dst->neighbour, skb->dev);
523 if (!neigh) {
524 ++dev->stats.tx_dropped;
525 dev_kfree_skb_any(skb);
526 return;
530 * We can only be called from ipoib_start_xmit, so we're
531 * inside tx_lock -- no need to save/restore flags.
533 spin_lock(&priv->lock);
535 path = __path_find(dev, skb->dst->neighbour->ha + 4);
536 if (!path) {
537 path = path_rec_create(dev, skb->dst->neighbour->ha + 4);
538 if (!path)
539 goto err_path;
541 __path_add(dev, path);
544 list_add_tail(&neigh->list, &path->neigh_list);
546 if (path->ah) {
547 kref_get(&path->ah->ref);
548 neigh->ah = path->ah;
549 memcpy(&neigh->dgid.raw, &path->pathrec.dgid.raw,
550 sizeof(union ib_gid));
552 if (ipoib_cm_enabled(dev, neigh->neighbour)) {
553 if (!ipoib_cm_get(neigh))
554 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
555 if (!ipoib_cm_get(neigh)) {
556 list_del(&neigh->list);
557 if (neigh->ah)
558 ipoib_put_ah(neigh->ah);
559 ipoib_neigh_free(dev, neigh);
560 goto err_drop;
562 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
563 __skb_queue_tail(&neigh->queue, skb);
564 else {
565 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
566 skb_queue_len(&neigh->queue));
567 goto err_drop;
569 } else
570 ipoib_send(dev, skb, path->ah, IPOIB_QPN(skb->dst->neighbour->ha));
571 } else {
572 neigh->ah = NULL;
574 if (!path->query && path_rec_start(dev, path))
575 goto err_list;
577 __skb_queue_tail(&neigh->queue, skb);
580 spin_unlock(&priv->lock);
581 return;
583 err_list:
584 list_del(&neigh->list);
586 err_path:
587 ipoib_neigh_free(dev, neigh);
588 err_drop:
589 ++dev->stats.tx_dropped;
590 dev_kfree_skb_any(skb);
592 spin_unlock(&priv->lock);
595 static void ipoib_path_lookup(struct sk_buff *skb, struct net_device *dev)
597 struct ipoib_dev_priv *priv = netdev_priv(skb->dev);
599 /* Look up path record for unicasts */
600 if (skb->dst->neighbour->ha[4] != 0xff) {
601 neigh_add_path(skb, dev);
602 return;
605 /* Add in the P_Key for multicasts */
606 skb->dst->neighbour->ha[8] = (priv->pkey >> 8) & 0xff;
607 skb->dst->neighbour->ha[9] = priv->pkey & 0xff;
608 ipoib_mcast_send(dev, skb->dst->neighbour->ha + 4, skb);
611 static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
612 struct ipoib_pseudoheader *phdr)
614 struct ipoib_dev_priv *priv = netdev_priv(dev);
615 struct ipoib_path *path;
618 * We can only be called from ipoib_start_xmit, so we're
619 * inside tx_lock -- no need to save/restore flags.
621 spin_lock(&priv->lock);
623 path = __path_find(dev, phdr->hwaddr + 4);
624 if (!path) {
625 path = path_rec_create(dev, phdr->hwaddr + 4);
626 if (path) {
627 /* put pseudoheader back on for next time */
628 skb_push(skb, sizeof *phdr);
629 __skb_queue_tail(&path->queue, skb);
631 if (path_rec_start(dev, path)) {
632 spin_unlock(&priv->lock);
633 path_free(dev, path);
634 return;
635 } else
636 __path_add(dev, path);
637 } else {
638 ++dev->stats.tx_dropped;
639 dev_kfree_skb_any(skb);
642 spin_unlock(&priv->lock);
643 return;
646 if (path->ah) {
647 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
648 be16_to_cpu(path->pathrec.dlid));
650 ipoib_send(dev, skb, path->ah, IPOIB_QPN(phdr->hwaddr));
651 } else if ((path->query || !path_rec_start(dev, path)) &&
652 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
653 /* put pseudoheader back on for next time */
654 skb_push(skb, sizeof *phdr);
655 __skb_queue_tail(&path->queue, skb);
656 } else {
657 ++dev->stats.tx_dropped;
658 dev_kfree_skb_any(skb);
661 spin_unlock(&priv->lock);
664 static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
666 struct ipoib_dev_priv *priv = netdev_priv(dev);
667 struct ipoib_neigh *neigh;
668 unsigned long flags;
670 if (unlikely(!spin_trylock_irqsave(&priv->tx_lock, flags)))
671 return NETDEV_TX_LOCKED;
673 if (likely(skb->dst && skb->dst->neighbour)) {
674 if (unlikely(!*to_ipoib_neigh(skb->dst->neighbour))) {
675 ipoib_path_lookup(skb, dev);
676 goto out;
679 neigh = *to_ipoib_neigh(skb->dst->neighbour);
681 if (neigh->ah)
682 if (unlikely((memcmp(&neigh->dgid.raw,
683 skb->dst->neighbour->ha + 4,
684 sizeof(union ib_gid))) ||
685 (neigh->dev != dev))) {
686 spin_lock(&priv->lock);
688 * It's safe to call ipoib_put_ah() inside
689 * priv->lock here, because we know that
690 * path->ah will always hold one more reference,
691 * so ipoib_put_ah() will never do more than
692 * decrement the ref count.
694 ipoib_put_ah(neigh->ah);
695 list_del(&neigh->list);
696 ipoib_neigh_free(dev, neigh);
697 spin_unlock(&priv->lock);
698 ipoib_path_lookup(skb, dev);
699 goto out;
702 if (ipoib_cm_get(neigh)) {
703 if (ipoib_cm_up(neigh)) {
704 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
705 goto out;
707 } else if (neigh->ah) {
708 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(skb->dst->neighbour->ha));
709 goto out;
712 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
713 spin_lock(&priv->lock);
714 __skb_queue_tail(&neigh->queue, skb);
715 spin_unlock(&priv->lock);
716 } else {
717 ++dev->stats.tx_dropped;
718 dev_kfree_skb_any(skb);
720 } else {
721 struct ipoib_pseudoheader *phdr =
722 (struct ipoib_pseudoheader *) skb->data;
723 skb_pull(skb, sizeof *phdr);
725 if (phdr->hwaddr[4] == 0xff) {
726 /* Add in the P_Key for multicast*/
727 phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
728 phdr->hwaddr[9] = priv->pkey & 0xff;
730 ipoib_mcast_send(dev, phdr->hwaddr + 4, skb);
731 } else {
732 /* unicast GID -- should be ARP or RARP reply */
734 if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
735 (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
736 ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x "
737 IPOIB_GID_FMT "\n",
738 skb->dst ? "neigh" : "dst",
739 be16_to_cpup((__be16 *) skb->data),
740 IPOIB_QPN(phdr->hwaddr),
741 IPOIB_GID_RAW_ARG(phdr->hwaddr + 4));
742 dev_kfree_skb_any(skb);
743 ++dev->stats.tx_dropped;
744 goto out;
747 unicast_arp_send(skb, dev, phdr);
751 out:
752 spin_unlock_irqrestore(&priv->tx_lock, flags);
754 return NETDEV_TX_OK;
757 static void ipoib_timeout(struct net_device *dev)
759 struct ipoib_dev_priv *priv = netdev_priv(dev);
761 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
762 jiffies_to_msecs(jiffies - dev->trans_start));
763 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
764 netif_queue_stopped(dev),
765 priv->tx_head, priv->tx_tail);
766 /* XXX reset QP, etc. */
769 static int ipoib_hard_header(struct sk_buff *skb,
770 struct net_device *dev,
771 unsigned short type,
772 const void *daddr, const void *saddr, unsigned len)
774 struct ipoib_header *header;
776 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
778 header->proto = htons(type);
779 header->reserved = 0;
782 * If we don't have a neighbour structure, stuff the
783 * destination address onto the front of the skb so we can
784 * figure out where to send the packet later.
786 if ((!skb->dst || !skb->dst->neighbour) && daddr) {
787 struct ipoib_pseudoheader *phdr =
788 (struct ipoib_pseudoheader *) skb_push(skb, sizeof *phdr);
789 memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
792 return 0;
795 static void ipoib_set_mcast_list(struct net_device *dev)
797 struct ipoib_dev_priv *priv = netdev_priv(dev);
799 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
800 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
801 return;
804 queue_work(ipoib_workqueue, &priv->restart_task);
807 static void ipoib_neigh_cleanup(struct neighbour *n)
809 struct ipoib_neigh *neigh;
810 struct ipoib_dev_priv *priv = netdev_priv(n->dev);
811 unsigned long flags;
812 struct ipoib_ah *ah = NULL;
814 neigh = *to_ipoib_neigh(n);
815 if (neigh)
816 priv = netdev_priv(neigh->dev);
817 else
818 return;
819 ipoib_dbg(priv,
820 "neigh_cleanup for %06x " IPOIB_GID_FMT "\n",
821 IPOIB_QPN(n->ha),
822 IPOIB_GID_RAW_ARG(n->ha + 4));
824 spin_lock_irqsave(&priv->lock, flags);
826 if (neigh->ah)
827 ah = neigh->ah;
828 list_del(&neigh->list);
829 ipoib_neigh_free(n->dev, neigh);
831 spin_unlock_irqrestore(&priv->lock, flags);
833 if (ah)
834 ipoib_put_ah(ah);
837 struct ipoib_neigh *ipoib_neigh_alloc(struct neighbour *neighbour,
838 struct net_device *dev)
840 struct ipoib_neigh *neigh;
842 neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
843 if (!neigh)
844 return NULL;
846 neigh->neighbour = neighbour;
847 neigh->dev = dev;
848 *to_ipoib_neigh(neighbour) = neigh;
849 skb_queue_head_init(&neigh->queue);
850 ipoib_cm_set(neigh, NULL);
852 return neigh;
855 void ipoib_neigh_free(struct net_device *dev, struct ipoib_neigh *neigh)
857 struct sk_buff *skb;
858 *to_ipoib_neigh(neigh->neighbour) = NULL;
859 while ((skb = __skb_dequeue(&neigh->queue))) {
860 ++dev->stats.tx_dropped;
861 dev_kfree_skb_any(skb);
863 if (ipoib_cm_get(neigh))
864 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
865 kfree(neigh);
868 static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
870 parms->neigh_cleanup = ipoib_neigh_cleanup;
872 return 0;
875 int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
877 struct ipoib_dev_priv *priv = netdev_priv(dev);
879 /* Allocate RX/TX "rings" to hold queued skbs */
880 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
881 GFP_KERNEL);
882 if (!priv->rx_ring) {
883 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
884 ca->name, ipoib_recvq_size);
885 goto out;
888 priv->tx_ring = vmalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
889 if (!priv->tx_ring) {
890 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
891 ca->name, ipoib_sendq_size);
892 goto out_rx_ring_cleanup;
894 memset(priv->tx_ring, 0, ipoib_sendq_size * sizeof *priv->tx_ring);
896 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
898 if (ipoib_ib_dev_init(dev, ca, port))
899 goto out_tx_ring_cleanup;
901 return 0;
903 out_tx_ring_cleanup:
904 vfree(priv->tx_ring);
906 out_rx_ring_cleanup:
907 kfree(priv->rx_ring);
909 out:
910 return -ENOMEM;
913 void ipoib_dev_cleanup(struct net_device *dev)
915 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
917 ipoib_delete_debug_files(dev);
919 /* Delete any child interfaces first */
920 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
921 unregister_netdev(cpriv->dev);
922 ipoib_dev_cleanup(cpriv->dev);
923 free_netdev(cpriv->dev);
926 ipoib_ib_dev_cleanup(dev);
928 kfree(priv->rx_ring);
929 vfree(priv->tx_ring);
931 priv->rx_ring = NULL;
932 priv->tx_ring = NULL;
935 static const struct header_ops ipoib_header_ops = {
936 .create = ipoib_hard_header,
939 static void ipoib_setup(struct net_device *dev)
941 struct ipoib_dev_priv *priv = netdev_priv(dev);
943 dev->open = ipoib_open;
944 dev->stop = ipoib_stop;
945 dev->change_mtu = ipoib_change_mtu;
946 dev->hard_start_xmit = ipoib_start_xmit;
947 dev->tx_timeout = ipoib_timeout;
948 dev->header_ops = &ipoib_header_ops;
949 dev->set_multicast_list = ipoib_set_mcast_list;
950 dev->neigh_setup = ipoib_neigh_setup_dev;
952 ipoib_set_ethtool_ops(dev);
954 netif_napi_add(dev, &priv->napi, ipoib_poll, 100);
956 dev->watchdog_timeo = HZ;
958 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
961 * We add in INFINIBAND_ALEN to allow for the destination
962 * address "pseudoheader" for skbs without neighbour struct.
964 dev->hard_header_len = IPOIB_ENCAP_LEN + INFINIBAND_ALEN;
965 dev->addr_len = INFINIBAND_ALEN;
966 dev->type = ARPHRD_INFINIBAND;
967 dev->tx_queue_len = ipoib_sendq_size * 2;
968 dev->features = (NETIF_F_VLAN_CHALLENGED |
969 NETIF_F_LLTX |
970 NETIF_F_HIGHDMA);
972 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
974 netif_carrier_off(dev);
976 priv->dev = dev;
978 spin_lock_init(&priv->lock);
979 spin_lock_init(&priv->tx_lock);
981 mutex_init(&priv->mcast_mutex);
982 mutex_init(&priv->vlan_mutex);
984 INIT_LIST_HEAD(&priv->path_list);
985 INIT_LIST_HEAD(&priv->child_intfs);
986 INIT_LIST_HEAD(&priv->dead_ahs);
987 INIT_LIST_HEAD(&priv->multicast_list);
989 INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
990 INIT_WORK(&priv->pkey_event_task, ipoib_pkey_event);
991 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
992 INIT_WORK(&priv->flush_task, ipoib_ib_dev_flush);
993 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
994 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
997 struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
999 struct net_device *dev;
1001 dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
1002 ipoib_setup);
1003 if (!dev)
1004 return NULL;
1006 return netdev_priv(dev);
1009 static ssize_t show_pkey(struct device *dev,
1010 struct device_attribute *attr, char *buf)
1012 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1014 return sprintf(buf, "0x%04x\n", priv->pkey);
1016 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1018 static ssize_t show_umcast(struct device *dev,
1019 struct device_attribute *attr, char *buf)
1021 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1023 return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1026 static ssize_t set_umcast(struct device *dev,
1027 struct device_attribute *attr,
1028 const char *buf, size_t count)
1030 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1031 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1033 if (umcast_val > 0) {
1034 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1035 ipoib_warn(priv, "ignoring multicast groups joined directly "
1036 "by userspace\n");
1037 } else
1038 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1040 return count;
1042 static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1044 int ipoib_add_umcast_attr(struct net_device *dev)
1046 return device_create_file(&dev->dev, &dev_attr_umcast);
1049 static ssize_t create_child(struct device *dev,
1050 struct device_attribute *attr,
1051 const char *buf, size_t count)
1053 int pkey;
1054 int ret;
1056 if (sscanf(buf, "%i", &pkey) != 1)
1057 return -EINVAL;
1059 if (pkey < 0 || pkey > 0xffff)
1060 return -EINVAL;
1063 * Set the full membership bit, so that we join the right
1064 * broadcast group, etc.
1066 pkey |= 0x8000;
1068 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
1070 return ret ? ret : count;
1072 static DEVICE_ATTR(create_child, S_IWUGO, NULL, create_child);
1074 static ssize_t delete_child(struct device *dev,
1075 struct device_attribute *attr,
1076 const char *buf, size_t count)
1078 int pkey;
1079 int ret;
1081 if (sscanf(buf, "%i", &pkey) != 1)
1082 return -EINVAL;
1084 if (pkey < 0 || pkey > 0xffff)
1085 return -EINVAL;
1087 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
1089 return ret ? ret : count;
1092 static DEVICE_ATTR(delete_child, S_IWUGO, NULL, delete_child);
1094 int ipoib_add_pkey_attr(struct net_device *dev)
1096 return device_create_file(&dev->dev, &dev_attr_pkey);
1099 static struct net_device *ipoib_add_port(const char *format,
1100 struct ib_device *hca, u8 port)
1102 struct ipoib_dev_priv *priv;
1103 struct ib_device_attr *device_attr;
1104 struct ib_port_attr attr;
1105 int result = -ENOMEM;
1107 priv = ipoib_intf_alloc(format);
1108 if (!priv)
1109 goto alloc_mem_failed;
1111 SET_NETDEV_DEV(priv->dev, hca->dma_device);
1113 if (!ib_query_port(hca, port, &attr))
1114 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1115 else {
1116 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
1117 hca->name, port);
1118 goto device_init_failed;
1121 /* MTU will be reset when mcast join happens */
1122 priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
1123 priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
1125 result = ib_query_pkey(hca, port, 0, &priv->pkey);
1126 if (result) {
1127 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1128 hca->name, port, result);
1129 goto device_init_failed;
1132 device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
1133 if (!device_attr) {
1134 printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
1135 hca->name, sizeof *device_attr);
1136 goto device_init_failed;
1139 result = ib_query_device(hca, device_attr);
1140 if (result) {
1141 printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
1142 hca->name, result);
1143 kfree(device_attr);
1144 goto device_init_failed;
1146 priv->hca_caps = device_attr->device_cap_flags;
1148 kfree(device_attr);
1150 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
1151 set_bit(IPOIB_FLAG_CSUM, &priv->flags);
1152 priv->dev->features |= NETIF_F_SG | NETIF_F_IP_CSUM;
1156 * Set the full membership bit, so that we join the right
1157 * broadcast group, etc.
1159 priv->pkey |= 0x8000;
1161 priv->dev->broadcast[8] = priv->pkey >> 8;
1162 priv->dev->broadcast[9] = priv->pkey & 0xff;
1164 result = ib_query_gid(hca, port, 0, &priv->local_gid);
1165 if (result) {
1166 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1167 hca->name, port, result);
1168 goto device_init_failed;
1169 } else
1170 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1172 result = ipoib_dev_init(priv->dev, hca, port);
1173 if (result < 0) {
1174 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1175 hca->name, port, result);
1176 goto device_init_failed;
1179 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1180 priv->ca, ipoib_event);
1181 result = ib_register_event_handler(&priv->event_handler);
1182 if (result < 0) {
1183 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1184 "port %d (ret = %d)\n",
1185 hca->name, port, result);
1186 goto event_failed;
1189 if (priv->dev->features & NETIF_F_SG && priv->hca_caps & IB_DEVICE_UD_TSO)
1190 priv->dev->features |= NETIF_F_TSO;
1192 result = register_netdev(priv->dev);
1193 if (result) {
1194 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1195 hca->name, port, result);
1196 goto register_failed;
1199 ipoib_create_debug_files(priv->dev);
1201 if (ipoib_cm_add_mode_attr(priv->dev))
1202 goto sysfs_failed;
1203 if (ipoib_add_pkey_attr(priv->dev))
1204 goto sysfs_failed;
1205 if (ipoib_add_umcast_attr(priv->dev))
1206 goto sysfs_failed;
1207 if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
1208 goto sysfs_failed;
1209 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
1210 goto sysfs_failed;
1212 return priv->dev;
1214 sysfs_failed:
1215 ipoib_delete_debug_files(priv->dev);
1216 unregister_netdev(priv->dev);
1218 register_failed:
1219 ib_unregister_event_handler(&priv->event_handler);
1220 flush_scheduled_work();
1222 event_failed:
1223 ipoib_dev_cleanup(priv->dev);
1225 device_init_failed:
1226 free_netdev(priv->dev);
1228 alloc_mem_failed:
1229 return ERR_PTR(result);
1232 static void ipoib_add_one(struct ib_device *device)
1234 struct list_head *dev_list;
1235 struct net_device *dev;
1236 struct ipoib_dev_priv *priv;
1237 int s, e, p;
1239 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1240 return;
1242 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1243 if (!dev_list)
1244 return;
1246 INIT_LIST_HEAD(dev_list);
1248 if (device->node_type == RDMA_NODE_IB_SWITCH) {
1249 s = 0;
1250 e = 0;
1251 } else {
1252 s = 1;
1253 e = device->phys_port_cnt;
1256 for (p = s; p <= e; ++p) {
1257 dev = ipoib_add_port("ib%d", device, p);
1258 if (!IS_ERR(dev)) {
1259 priv = netdev_priv(dev);
1260 list_add_tail(&priv->list, dev_list);
1264 ib_set_client_data(device, &ipoib_client, dev_list);
1267 static void ipoib_remove_one(struct ib_device *device)
1269 struct ipoib_dev_priv *priv, *tmp;
1270 struct list_head *dev_list;
1272 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1273 return;
1275 dev_list = ib_get_client_data(device, &ipoib_client);
1277 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1278 ib_unregister_event_handler(&priv->event_handler);
1279 flush_scheduled_work();
1281 unregister_netdev(priv->dev);
1282 ipoib_dev_cleanup(priv->dev);
1283 free_netdev(priv->dev);
1286 kfree(dev_list);
1289 static int __init ipoib_init_module(void)
1291 int ret;
1293 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1294 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1295 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1297 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1298 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1299 ipoib_sendq_size = max(ipoib_sendq_size, max(2 * MAX_SEND_CQE,
1300 IPOIB_MIN_QUEUE_SIZE));
1301 #ifdef CONFIG_INFINIBAND_IPOIB_CM
1302 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1303 #endif
1305 ret = ipoib_register_debugfs();
1306 if (ret)
1307 return ret;
1310 * We create our own workqueue mainly because we want to be
1311 * able to flush it when devices are being removed. We can't
1312 * use schedule_work()/flush_scheduled_work() because both
1313 * unregister_netdev() and linkwatch_event take the rtnl lock,
1314 * so flush_scheduled_work() can deadlock during device
1315 * removal.
1317 ipoib_workqueue = create_singlethread_workqueue("ipoib");
1318 if (!ipoib_workqueue) {
1319 ret = -ENOMEM;
1320 goto err_fs;
1323 ib_sa_register_client(&ipoib_sa_client);
1325 ret = ib_register_client(&ipoib_client);
1326 if (ret)
1327 goto err_sa;
1329 return 0;
1331 err_sa:
1332 ib_sa_unregister_client(&ipoib_sa_client);
1333 destroy_workqueue(ipoib_workqueue);
1335 err_fs:
1336 ipoib_unregister_debugfs();
1338 return ret;
1341 static void __exit ipoib_cleanup_module(void)
1343 ib_unregister_client(&ipoib_client);
1344 ib_sa_unregister_client(&ipoib_sa_client);
1345 ipoib_unregister_debugfs();
1346 destroy_workqueue(ipoib_workqueue);
1349 module_init(ipoib_init_module);
1350 module_exit(ipoib_cleanup_module);