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