]> err.no Git - linux-2.6/blob - drivers/infiniband/ulp/ipoib/ipoib_main.c
IPoIB: Copy small received SKBs in connected mode
[linux-2.6] / 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
35 #include "ipoib.h"
36
37 #include <linux/module.h>
38
39 #include <linux/init.h>
40 #include <linux/slab.h>
41 #include <linux/kernel.h>
42 #include <linux/vmalloc.h>
43
44 #include <linux/if_arp.h>       /* For ARPHRD_xxx */
45
46 #include <linux/ip.h>
47 #include <linux/in.h>
48
49 #include <net/dst.h>
50
51 MODULE_AUTHOR("Roland Dreier");
52 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
53 MODULE_LICENSE("Dual BSD/GPL");
54
55 int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
56 int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
57
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");
62
63 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
64 int ipoib_debug_level;
65
66 module_param_named(debug_level, ipoib_debug_level, int, 0644);
67 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
68 #endif
69
70 struct ipoib_path_iter {
71         struct net_device *dev;
72         struct ipoib_path  path;
73 };
74
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
79 };
80
81 struct workqueue_struct *ipoib_workqueue;
82
83 struct ib_sa_client ipoib_sa_client;
84
85 static void ipoib_add_one(struct ib_device *device);
86 static void ipoib_remove_one(struct ib_device *device);
87
88 static struct ib_client ipoib_client = {
89         .name   = "ipoib",
90         .add    = ipoib_add_one,
91         .remove = ipoib_remove_one
92 };
93
94 int ipoib_open(struct net_device *dev)
95 {
96         struct ipoib_dev_priv *priv = netdev_priv(dev);
97
98         ipoib_dbg(priv, "bringing up interface\n");
99
100         napi_enable(&priv->napi);
101         set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
102
103         if (ipoib_pkey_dev_delay_open(dev))
104                 return 0;
105
106         if (ipoib_ib_dev_open(dev)) {
107                 napi_disable(&priv->napi);
108                 return -EINVAL;
109         }
110
111         if (ipoib_ib_dev_up(dev)) {
112                 ipoib_ib_dev_stop(dev, 1);
113                 napi_disable(&priv->napi);
114                 return -EINVAL;
115         }
116
117         if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
118                 struct ipoib_dev_priv *cpriv;
119
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;
124
125                         flags = cpriv->dev->flags;
126                         if (flags & IFF_UP)
127                                 continue;
128
129                         dev_change_flags(cpriv->dev, flags | IFF_UP);
130                 }
131                 mutex_unlock(&priv->vlan_mutex);
132         }
133
134         netif_start_queue(dev);
135
136         return 0;
137 }
138
139 static int ipoib_stop(struct net_device *dev)
140 {
141         struct ipoib_dev_priv *priv = netdev_priv(dev);
142
143         ipoib_dbg(priv, "stopping interface\n");
144
145         clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
146         napi_disable(&priv->napi);
147
148         netif_stop_queue(dev);
149
150         /*
151          * Now flush workqueue to make sure a scheduled task doesn't
152          * bring our internal state back up.
153          */
154         flush_workqueue(ipoib_workqueue);
155
156         ipoib_ib_dev_down(dev, 1);
157         ipoib_ib_dev_stop(dev, 1);
158
159         if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
160                 struct ipoib_dev_priv *cpriv;
161
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;
166
167                         flags = cpriv->dev->flags;
168                         if (!(flags & IFF_UP))
169                                 continue;
170
171                         dev_change_flags(cpriv->dev, flags & ~IFF_UP);
172                 }
173                 mutex_unlock(&priv->vlan_mutex);
174         }
175
176         return 0;
177 }
178
179 static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
180 {
181         struct ipoib_dev_priv *priv = netdev_priv(dev);
182
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;
187
188                 if (new_mtu > priv->mcast_mtu)
189                         ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
190                                    priv->mcast_mtu);
191
192                 dev->mtu = new_mtu;
193                 return 0;
194         }
195
196         if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
197                 return -EINVAL;
198
199         priv->admin_mtu = new_mtu;
200
201         dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
202
203         return 0;
204 }
205
206 static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
207 {
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;
212
213         while (n) {
214                 path = rb_entry(n, struct ipoib_path, rb_node);
215
216                 ret = memcmp(gid, path->pathrec.dgid.raw,
217                              sizeof (union ib_gid));
218
219                 if (ret < 0)
220                         n = n->rb_left;
221                 else if (ret > 0)
222                         n = n->rb_right;
223                 else
224                         return path;
225         }
226
227         return NULL;
228 }
229
230 static int __path_add(struct net_device *dev, struct ipoib_path *path)
231 {
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;
237
238         while (*n) {
239                 pn = *n;
240                 tpath = rb_entry(pn, struct ipoib_path, rb_node);
241
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;
250         }
251
252         rb_link_node(&path->rb_node, pn, n);
253         rb_insert_color(&path->rb_node, &priv->path_tree);
254
255         list_add_tail(&path->list, &priv->path_list);
256
257         return 0;
258 }
259
260 static void path_free(struct net_device *dev, struct ipoib_path *path)
261 {
262         struct ipoib_dev_priv *priv = netdev_priv(dev);
263         struct ipoib_neigh *neigh, *tn;
264         struct sk_buff *skb;
265         unsigned long flags;
266
267         while ((skb = __skb_dequeue(&path->queue)))
268                 dev_kfree_skb_irq(skb);
269
270         spin_lock_irqsave(&priv->lock, flags);
271
272         list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
273                 /*
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.
278                  */
279                 if (neigh->ah)
280                         ipoib_put_ah(neigh->ah);
281
282                 ipoib_neigh_free(dev, neigh);
283         }
284
285         spin_unlock_irqrestore(&priv->lock, flags);
286
287         if (path->ah)
288                 ipoib_put_ah(path->ah);
289
290         kfree(path);
291 }
292
293 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
294
295 struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
296 {
297         struct ipoib_path_iter *iter;
298
299         iter = kmalloc(sizeof *iter, GFP_KERNEL);
300         if (!iter)
301                 return NULL;
302
303         iter->dev = dev;
304         memset(iter->path.pathrec.dgid.raw, 0, 16);
305
306         if (ipoib_path_iter_next(iter)) {
307                 kfree(iter);
308                 return NULL;
309         }
310
311         return iter;
312 }
313
314 int ipoib_path_iter_next(struct ipoib_path_iter *iter)
315 {
316         struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
317         struct rb_node *n;
318         struct ipoib_path *path;
319         int ret = 1;
320
321         spin_lock_irq(&priv->lock);
322
323         n = rb_first(&priv->path_tree);
324
325         while (n) {
326                 path = rb_entry(n, struct ipoib_path, rb_node);
327
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;
333                 }
334
335                 n = rb_next(n);
336         }
337
338         spin_unlock_irq(&priv->lock);
339
340         return ret;
341 }
342
343 void ipoib_path_iter_read(struct ipoib_path_iter *iter,
344                           struct ipoib_path *path)
345 {
346         *path = iter->path;
347 }
348
349 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
350
351 void ipoib_flush_paths(struct net_device *dev)
352 {
353         struct ipoib_dev_priv *priv = netdev_priv(dev);
354         struct ipoib_path *path, *tp;
355         LIST_HEAD(remove_list);
356
357         spin_lock_irq(&priv->tx_lock);
358         spin_lock(&priv->lock);
359
360         list_splice_init(&priv->path_list, &remove_list);
361
362         list_for_each_entry(path, &remove_list, list)
363                 rb_erase(&path->rb_node, &priv->path_tree);
364
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);
374         }
375         spin_unlock(&priv->lock);
376         spin_unlock_irq(&priv->tx_lock);
377 }
378
379 static void path_rec_completion(int status,
380                                 struct ib_sa_path_rec *pathrec,
381                                 void *path_ptr)
382 {
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;
391
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));
398
399         skb_queue_head_init(&skqueue);
400
401         if (!status) {
402                 struct ib_ah_attr av;
403
404                 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
405                         ah = ipoib_create_ah(dev, priv->pd, &av);
406         }
407
408         spin_lock_irqsave(&priv->lock, flags);
409
410         path->ah = ah;
411
412         if (ah) {
413                 path->pathrec = *pathrec;
414
415                 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
416                           ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
417
418                 while ((skb = __skb_dequeue(&path->queue)))
419                         __skb_queue_tail(&skqueue, skb);
420
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));
426
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;
438                                 }
439                         }
440
441                         while ((skb = __skb_dequeue(&neigh->queue)))
442                                 __skb_queue_tail(&skqueue, skb);
443                 }
444         }
445
446         path->query = NULL;
447         complete(&path->done);
448
449         spin_unlock_irqrestore(&priv->lock, flags);
450
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");
456         }
457 }
458
459 static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
460 {
461         struct ipoib_dev_priv *priv = netdev_priv(dev);
462         struct ipoib_path *path;
463
464         if (!priv->broadcast)
465                 return NULL;
466
467         path = kzalloc(sizeof *path, GFP_ATOMIC);
468         if (!path)
469                 return NULL;
470
471         path->dev = dev;
472
473         skb_queue_head_init(&path->queue);
474
475         INIT_LIST_HEAD(&path->neigh_list);
476
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;
482
483         return path;
484 }
485
486 static int path_rec_start(struct net_device *dev,
487                           struct ipoib_path *path)
488 {
489         struct ipoib_dev_priv *priv = netdev_priv(dev);
490
491         ipoib_dbg(priv, "Start path record lookup for " IPOIB_GID_FMT "\n",
492                   IPOIB_GID_ARG(path->pathrec.dgid));
493
494         init_completion(&path->done);
495
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;
511         }
512
513         return 0;
514 }
515
516 static void neigh_add_path(struct sk_buff *skb, struct net_device *dev)
517 {
518         struct ipoib_dev_priv *priv = netdev_priv(dev);
519         struct ipoib_path *path;
520         struct ipoib_neigh *neigh;
521
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;
527         }
528
529         /*
530          * We can only be called from ipoib_start_xmit, so we're
531          * inside tx_lock -- no need to save/restore flags.
532          */
533         spin_lock(&priv->lock);
534
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;
540
541                 __path_add(dev, path);
542         }
543
544         list_add_tail(&neigh->list, &path->neigh_list);
545
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));
551
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;
561                         }
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;
568                         }
569                 } else
570                         ipoib_send(dev, skb, path->ah, IPOIB_QPN(skb->dst->neighbour->ha));
571         } else {
572                 neigh->ah  = NULL;
573
574                 if (!path->query && path_rec_start(dev, path))
575                         goto err_list;
576
577                 __skb_queue_tail(&neigh->queue, skb);
578         }
579
580         spin_unlock(&priv->lock);
581         return;
582
583 err_list:
584         list_del(&neigh->list);
585
586 err_path:
587         ipoib_neigh_free(dev, neigh);
588 err_drop:
589         ++dev->stats.tx_dropped;
590         dev_kfree_skb_any(skb);
591
592         spin_unlock(&priv->lock);
593 }
594
595 static void ipoib_path_lookup(struct sk_buff *skb, struct net_device *dev)
596 {
597         struct ipoib_dev_priv *priv = netdev_priv(skb->dev);
598
599         /* Look up path record for unicasts */
600         if (skb->dst->neighbour->ha[4] != 0xff) {
601                 neigh_add_path(skb, dev);
602                 return;
603         }
604
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);
609 }
610
611 static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
612                              struct ipoib_pseudoheader *phdr)
613 {
614         struct ipoib_dev_priv *priv = netdev_priv(dev);
615         struct ipoib_path *path;
616
617         /*
618          * We can only be called from ipoib_start_xmit, so we're
619          * inside tx_lock -- no need to save/restore flags.
620          */
621         spin_lock(&priv->lock);
622
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);
630
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);
640                 }
641
642                 spin_unlock(&priv->lock);
643                 return;
644         }
645
646         if (path->ah) {
647                 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
648                           be16_to_cpu(path->pathrec.dlid));
649
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);
659         }
660
661         spin_unlock(&priv->lock);
662 }
663
664 static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
665 {
666         struct ipoib_dev_priv *priv = netdev_priv(dev);
667         struct ipoib_neigh *neigh;
668         unsigned long flags;
669
670         if (unlikely(!spin_trylock_irqsave(&priv->tx_lock, flags)))
671                 return NETDEV_TX_LOCKED;
672
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;
677                 }
678
679                 neigh = *to_ipoib_neigh(skb->dst->neighbour);
680
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);
687                                 /*
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.
693                                  */
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;
700                         }
701
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;
706                         }
707                 } else if (neigh->ah) {
708                         ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(skb->dst->neighbour->ha));
709                         goto out;
710                 }
711
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);
719                 }
720         } else {
721                 struct ipoib_pseudoheader *phdr =
722                         (struct ipoib_pseudoheader *) skb->data;
723                 skb_pull(skb, sizeof *phdr);
724
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;
729
730                         ipoib_mcast_send(dev, phdr->hwaddr + 4, skb);
731                 } else {
732                         /* unicast GID -- should be ARP or RARP reply */
733
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;
745                         }
746
747                         unicast_arp_send(skb, dev, phdr);
748                 }
749         }
750
751 out:
752         spin_unlock_irqrestore(&priv->tx_lock, flags);
753
754         return NETDEV_TX_OK;
755 }
756
757 static void ipoib_timeout(struct net_device *dev)
758 {
759         struct ipoib_dev_priv *priv = netdev_priv(dev);
760
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. */
767 }
768
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)
773 {
774         struct ipoib_header *header;
775
776         header = (struct ipoib_header *) skb_push(skb, sizeof *header);
777
778         header->proto = htons(type);
779         header->reserved = 0;
780
781         /*
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.
785          */
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);
790         }
791
792         return 0;
793 }
794
795 static void ipoib_set_mcast_list(struct net_device *dev)
796 {
797         struct ipoib_dev_priv *priv = netdev_priv(dev);
798
799         if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
800                 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
801                 return;
802         }
803
804         queue_work(ipoib_workqueue, &priv->restart_task);
805 }
806
807 static void ipoib_neigh_cleanup(struct neighbour *n)
808 {
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;
813
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));
823
824         spin_lock_irqsave(&priv->lock, flags);
825
826         if (neigh->ah)
827                 ah = neigh->ah;
828         list_del(&neigh->list);
829         ipoib_neigh_free(n->dev, neigh);
830
831         spin_unlock_irqrestore(&priv->lock, flags);
832
833         if (ah)
834                 ipoib_put_ah(ah);
835 }
836
837 struct ipoib_neigh *ipoib_neigh_alloc(struct neighbour *neighbour,
838                                       struct net_device *dev)
839 {
840         struct ipoib_neigh *neigh;
841
842         neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
843         if (!neigh)
844                 return NULL;
845
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);
851
852         return neigh;
853 }
854
855 void ipoib_neigh_free(struct net_device *dev, struct ipoib_neigh *neigh)
856 {
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);
862         }
863         if (ipoib_cm_get(neigh))
864                 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
865         kfree(neigh);
866 }
867
868 static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
869 {
870         parms->neigh_cleanup = ipoib_neigh_cleanup;
871
872         return 0;
873 }
874
875 int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
876 {
877         struct ipoib_dev_priv *priv = netdev_priv(dev);
878
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;
886         }
887
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;
893         }
894         memset(priv->tx_ring, 0, ipoib_sendq_size * sizeof *priv->tx_ring);
895
896         /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
897
898         if (ipoib_ib_dev_init(dev, ca, port))
899                 goto out_tx_ring_cleanup;
900
901         return 0;
902
903 out_tx_ring_cleanup:
904         vfree(priv->tx_ring);
905
906 out_rx_ring_cleanup:
907         kfree(priv->rx_ring);
908
909 out:
910         return -ENOMEM;
911 }
912
913 void ipoib_dev_cleanup(struct net_device *dev)
914 {
915         struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
916
917         ipoib_delete_debug_files(dev);
918
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);
924         }
925
926         ipoib_ib_dev_cleanup(dev);
927
928         kfree(priv->rx_ring);
929         vfree(priv->tx_ring);
930
931         priv->rx_ring = NULL;
932         priv->tx_ring = NULL;
933 }
934
935 static const struct header_ops ipoib_header_ops = {
936         .create = ipoib_hard_header,
937 };
938
939 static void ipoib_setup(struct net_device *dev)
940 {
941         struct ipoib_dev_priv *priv = netdev_priv(dev);
942
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;
951
952         ipoib_set_ethtool_ops(dev);
953
954         netif_napi_add(dev, &priv->napi, ipoib_poll, 100);
955
956         dev->watchdog_timeo      = HZ;
957
958         dev->flags              |= IFF_BROADCAST | IFF_MULTICAST;
959
960         /*
961          * We add in INFINIBAND_ALEN to allow for the destination
962          * address "pseudoheader" for skbs without neighbour struct.
963          */
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);
971
972         memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
973
974         netif_carrier_off(dev);
975
976         priv->dev = dev;
977
978         spin_lock_init(&priv->lock);
979         spin_lock_init(&priv->tx_lock);
980
981         mutex_init(&priv->mcast_mutex);
982         mutex_init(&priv->vlan_mutex);
983
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);
988
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);
995 }
996
997 struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
998 {
999         struct net_device *dev;
1000
1001         dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
1002                            ipoib_setup);
1003         if (!dev)
1004                 return NULL;
1005
1006         return netdev_priv(dev);
1007 }
1008
1009 static ssize_t show_pkey(struct device *dev,
1010                          struct device_attribute *attr, char *buf)
1011 {
1012         struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1013
1014         return sprintf(buf, "0x%04x\n", priv->pkey);
1015 }
1016 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1017
1018 static ssize_t show_umcast(struct device *dev,
1019                            struct device_attribute *attr, char *buf)
1020 {
1021         struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1022
1023         return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1024 }
1025
1026 static ssize_t set_umcast(struct device *dev,
1027                           struct device_attribute *attr,
1028                           const char *buf, size_t count)
1029 {
1030         struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1031         unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1032
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);
1039
1040         return count;
1041 }
1042 static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1043
1044 int ipoib_add_umcast_attr(struct net_device *dev)
1045 {
1046         return device_create_file(&dev->dev, &dev_attr_umcast);
1047 }
1048
1049 static ssize_t create_child(struct device *dev,
1050                             struct device_attribute *attr,
1051                             const char *buf, size_t count)
1052 {
1053         int pkey;
1054         int ret;
1055
1056         if (sscanf(buf, "%i", &pkey) != 1)
1057                 return -EINVAL;
1058
1059         if (pkey < 0 || pkey > 0xffff)
1060                 return -EINVAL;
1061
1062         /*
1063          * Set the full membership bit, so that we join the right
1064          * broadcast group, etc.
1065          */
1066         pkey |= 0x8000;
1067
1068         ret = ipoib_vlan_add(to_net_dev(dev), pkey);
1069
1070         return ret ? ret : count;
1071 }
1072 static DEVICE_ATTR(create_child, S_IWUGO, NULL, create_child);
1073
1074 static ssize_t delete_child(struct device *dev,
1075                             struct device_attribute *attr,
1076                             const char *buf, size_t count)
1077 {
1078         int pkey;
1079         int ret;
1080
1081         if (sscanf(buf, "%i", &pkey) != 1)
1082                 return -EINVAL;
1083
1084         if (pkey < 0 || pkey > 0xffff)
1085                 return -EINVAL;
1086
1087         ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
1088
1089         return ret ? ret : count;
1090
1091 }
1092 static DEVICE_ATTR(delete_child, S_IWUGO, NULL, delete_child);
1093
1094 int ipoib_add_pkey_attr(struct net_device *dev)
1095 {
1096         return device_create_file(&dev->dev, &dev_attr_pkey);
1097 }
1098
1099 static struct net_device *ipoib_add_port(const char *format,
1100                                          struct ib_device *hca, u8 port)
1101 {
1102         struct ipoib_dev_priv *priv;
1103         struct ib_device_attr *device_attr;
1104         struct ib_port_attr attr;
1105         int result = -ENOMEM;
1106
1107         priv = ipoib_intf_alloc(format);
1108         if (!priv)
1109                 goto alloc_mem_failed;
1110
1111         SET_NETDEV_DEV(priv->dev, hca->dma_device);
1112
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;
1119         }
1120
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;
1124
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;
1130         }
1131
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;
1137         }
1138
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;
1145         }
1146         priv->hca_caps = device_attr->device_cap_flags;
1147
1148         kfree(device_attr);
1149
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;
1153         }
1154
1155         /*
1156          * Set the full membership bit, so that we join the right
1157          * broadcast group, etc.
1158          */
1159         priv->pkey |= 0x8000;
1160
1161         priv->dev->broadcast[8] = priv->pkey >> 8;
1162         priv->dev->broadcast[9] = priv->pkey & 0xff;
1163
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));
1171
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;
1177         }
1178
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;
1187         }
1188
1189         if (priv->dev->features & NETIF_F_SG && priv->hca_caps & IB_DEVICE_UD_TSO)
1190                 priv->dev->features |= NETIF_F_TSO;
1191
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;
1197         }
1198
1199         ipoib_create_debug_files(priv->dev);
1200
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;
1211
1212         return priv->dev;
1213
1214 sysfs_failed:
1215         ipoib_delete_debug_files(priv->dev);
1216         unregister_netdev(priv->dev);
1217
1218 register_failed:
1219         ib_unregister_event_handler(&priv->event_handler);
1220         flush_scheduled_work();
1221
1222 event_failed:
1223         ipoib_dev_cleanup(priv->dev);
1224
1225 device_init_failed:
1226         free_netdev(priv->dev);
1227
1228 alloc_mem_failed:
1229         return ERR_PTR(result);
1230 }
1231
1232 static void ipoib_add_one(struct ib_device *device)
1233 {
1234         struct list_head *dev_list;
1235         struct net_device *dev;
1236         struct ipoib_dev_priv *priv;
1237         int s, e, p;
1238
1239         if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1240                 return;
1241
1242         dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1243         if (!dev_list)
1244                 return;
1245
1246         INIT_LIST_HEAD(dev_list);
1247
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;
1254         }
1255
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);
1261                 }
1262         }
1263
1264         ib_set_client_data(device, &ipoib_client, dev_list);
1265 }
1266
1267 static void ipoib_remove_one(struct ib_device *device)
1268 {
1269         struct ipoib_dev_priv *priv, *tmp;
1270         struct list_head *dev_list;
1271
1272         if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1273                 return;
1274
1275         dev_list = ib_get_client_data(device, &ipoib_client);
1276
1277         list_for_each_entry_safe(priv, tmp, dev_list, list) {
1278                 ib_unregister_event_handler(&priv->event_handler);
1279                 flush_scheduled_work();
1280
1281                 unregister_netdev(priv->dev);
1282                 ipoib_dev_cleanup(priv->dev);
1283                 free_netdev(priv->dev);
1284         }
1285
1286         kfree(dev_list);
1287 }
1288
1289 static int __init ipoib_init_module(void)
1290 {
1291         int ret;
1292
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);
1296
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
1304
1305         /*
1306          * When copying small received packets, we only copy from the
1307          * linear data part of the SKB, so we rely on this condition.
1308          */
1309         BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
1310
1311         ret = ipoib_register_debugfs();
1312         if (ret)
1313                 return ret;
1314
1315         /*
1316          * We create our own workqueue mainly because we want to be
1317          * able to flush it when devices are being removed.  We can't
1318          * use schedule_work()/flush_scheduled_work() because both
1319          * unregister_netdev() and linkwatch_event take the rtnl lock,
1320          * so flush_scheduled_work() can deadlock during device
1321          * removal.
1322          */
1323         ipoib_workqueue = create_singlethread_workqueue("ipoib");
1324         if (!ipoib_workqueue) {
1325                 ret = -ENOMEM;
1326                 goto err_fs;
1327         }
1328
1329         ib_sa_register_client(&ipoib_sa_client);
1330
1331         ret = ib_register_client(&ipoib_client);
1332         if (ret)
1333                 goto err_sa;
1334
1335         return 0;
1336
1337 err_sa:
1338         ib_sa_unregister_client(&ipoib_sa_client);
1339         destroy_workqueue(ipoib_workqueue);
1340
1341 err_fs:
1342         ipoib_unregister_debugfs();
1343
1344         return ret;
1345 }
1346
1347 static void __exit ipoib_cleanup_module(void)
1348 {
1349         ib_unregister_client(&ipoib_client);
1350         ib_sa_unregister_client(&ipoib_sa_client);
1351         ipoib_unregister_debugfs();
1352         destroy_workqueue(ipoib_workqueue);
1353 }
1354
1355 module_init(ipoib_init_module);
1356 module_exit(ipoib_cleanup_module);