Merge branch 'master' of git://git.kernel.org/pub/scm/linux/kernel/git/jkirsher/net...
[deliverable/linux.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
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 <linux/jhash.h>
50 #include <net/arp.h>
51
52 MODULE_AUTHOR("Roland Dreier");
53 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
54 MODULE_LICENSE("Dual BSD/GPL");
55
56 int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
57 int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
58
59 module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
60 MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
61 module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
62 MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
63
64 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
65 int ipoib_debug_level;
66
67 module_param_named(debug_level, ipoib_debug_level, int, 0644);
68 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
69 #endif
70
71 struct ipoib_path_iter {
72 struct net_device *dev;
73 struct ipoib_path path;
74 };
75
76 static const u8 ipv4_bcast_addr[] = {
77 0x00, 0xff, 0xff, 0xff,
78 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
79 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
80 };
81
82 struct workqueue_struct *ipoib_workqueue;
83
84 struct ib_sa_client ipoib_sa_client;
85
86 static void ipoib_add_one(struct ib_device *device);
87 static void ipoib_remove_one(struct ib_device *device);
88 static void ipoib_neigh_reclaim(struct rcu_head *rp);
89
90 static struct ib_client ipoib_client = {
91 .name = "ipoib",
92 .add = ipoib_add_one,
93 .remove = ipoib_remove_one
94 };
95
96 int ipoib_open(struct net_device *dev)
97 {
98 struct ipoib_dev_priv *priv = netdev_priv(dev);
99
100 ipoib_dbg(priv, "bringing up interface\n");
101
102 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
103
104 if (ipoib_pkey_dev_delay_open(dev))
105 return 0;
106
107 if (ipoib_ib_dev_open(dev))
108 goto err_disable;
109
110 if (ipoib_ib_dev_up(dev))
111 goto err_stop;
112
113 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
114 struct ipoib_dev_priv *cpriv;
115
116 /* Bring up any child interfaces too */
117 mutex_lock(&priv->vlan_mutex);
118 list_for_each_entry(cpriv, &priv->child_intfs, list) {
119 int flags;
120
121 flags = cpriv->dev->flags;
122 if (flags & IFF_UP)
123 continue;
124
125 dev_change_flags(cpriv->dev, flags | IFF_UP);
126 }
127 mutex_unlock(&priv->vlan_mutex);
128 }
129
130 netif_start_queue(dev);
131
132 return 0;
133
134 err_stop:
135 ipoib_ib_dev_stop(dev, 1);
136
137 err_disable:
138 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
139
140 return -EINVAL;
141 }
142
143 static int ipoib_stop(struct net_device *dev)
144 {
145 struct ipoib_dev_priv *priv = netdev_priv(dev);
146
147 ipoib_dbg(priv, "stopping interface\n");
148
149 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
150
151 netif_stop_queue(dev);
152
153 ipoib_ib_dev_down(dev, 0);
154 ipoib_ib_dev_stop(dev, 0);
155
156 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
157 struct ipoib_dev_priv *cpriv;
158
159 /* Bring down any child interfaces too */
160 mutex_lock(&priv->vlan_mutex);
161 list_for_each_entry(cpriv, &priv->child_intfs, list) {
162 int flags;
163
164 flags = cpriv->dev->flags;
165 if (!(flags & IFF_UP))
166 continue;
167
168 dev_change_flags(cpriv->dev, flags & ~IFF_UP);
169 }
170 mutex_unlock(&priv->vlan_mutex);
171 }
172
173 return 0;
174 }
175
176 static void ipoib_uninit(struct net_device *dev)
177 {
178 ipoib_dev_cleanup(dev);
179 }
180
181 static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
182 {
183 struct ipoib_dev_priv *priv = netdev_priv(dev);
184
185 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
186 features &= ~(NETIF_F_SG | NETIF_F_IP_CSUM | NETIF_F_TSO);
187
188 return features;
189 }
190
191 static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
192 {
193 struct ipoib_dev_priv *priv = netdev_priv(dev);
194
195 /* dev->mtu > 2K ==> connected mode */
196 if (ipoib_cm_admin_enabled(dev)) {
197 if (new_mtu > ipoib_cm_max_mtu(dev))
198 return -EINVAL;
199
200 if (new_mtu > priv->mcast_mtu)
201 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
202 priv->mcast_mtu);
203
204 dev->mtu = new_mtu;
205 return 0;
206 }
207
208 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
209 return -EINVAL;
210
211 priv->admin_mtu = new_mtu;
212
213 dev->mtu = min(priv->mcast_mtu, priv->admin_mtu);
214
215 return 0;
216 }
217
218 static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
219 {
220 struct ipoib_dev_priv *priv = netdev_priv(dev);
221 struct rb_node *n = priv->path_tree.rb_node;
222 struct ipoib_path *path;
223 int ret;
224
225 while (n) {
226 path = rb_entry(n, struct ipoib_path, rb_node);
227
228 ret = memcmp(gid, path->pathrec.dgid.raw,
229 sizeof (union ib_gid));
230
231 if (ret < 0)
232 n = n->rb_left;
233 else if (ret > 0)
234 n = n->rb_right;
235 else
236 return path;
237 }
238
239 return NULL;
240 }
241
242 static int __path_add(struct net_device *dev, struct ipoib_path *path)
243 {
244 struct ipoib_dev_priv *priv = netdev_priv(dev);
245 struct rb_node **n = &priv->path_tree.rb_node;
246 struct rb_node *pn = NULL;
247 struct ipoib_path *tpath;
248 int ret;
249
250 while (*n) {
251 pn = *n;
252 tpath = rb_entry(pn, struct ipoib_path, rb_node);
253
254 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
255 sizeof (union ib_gid));
256 if (ret < 0)
257 n = &pn->rb_left;
258 else if (ret > 0)
259 n = &pn->rb_right;
260 else
261 return -EEXIST;
262 }
263
264 rb_link_node(&path->rb_node, pn, n);
265 rb_insert_color(&path->rb_node, &priv->path_tree);
266
267 list_add_tail(&path->list, &priv->path_list);
268
269 return 0;
270 }
271
272 static void path_free(struct net_device *dev, struct ipoib_path *path)
273 {
274 struct sk_buff *skb;
275
276 while ((skb = __skb_dequeue(&path->queue)))
277 dev_kfree_skb_irq(skb);
278
279 ipoib_dbg(netdev_priv(dev), "path_free\n");
280
281 /* remove all neigh connected to this path */
282 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
283
284 if (path->ah)
285 ipoib_put_ah(path->ah);
286
287 kfree(path);
288 }
289
290 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
291
292 struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
293 {
294 struct ipoib_path_iter *iter;
295
296 iter = kmalloc(sizeof *iter, GFP_KERNEL);
297 if (!iter)
298 return NULL;
299
300 iter->dev = dev;
301 memset(iter->path.pathrec.dgid.raw, 0, 16);
302
303 if (ipoib_path_iter_next(iter)) {
304 kfree(iter);
305 return NULL;
306 }
307
308 return iter;
309 }
310
311 int ipoib_path_iter_next(struct ipoib_path_iter *iter)
312 {
313 struct ipoib_dev_priv *priv = netdev_priv(iter->dev);
314 struct rb_node *n;
315 struct ipoib_path *path;
316 int ret = 1;
317
318 spin_lock_irq(&priv->lock);
319
320 n = rb_first(&priv->path_tree);
321
322 while (n) {
323 path = rb_entry(n, struct ipoib_path, rb_node);
324
325 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
326 sizeof (union ib_gid)) < 0) {
327 iter->path = *path;
328 ret = 0;
329 break;
330 }
331
332 n = rb_next(n);
333 }
334
335 spin_unlock_irq(&priv->lock);
336
337 return ret;
338 }
339
340 void ipoib_path_iter_read(struct ipoib_path_iter *iter,
341 struct ipoib_path *path)
342 {
343 *path = iter->path;
344 }
345
346 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
347
348 void ipoib_mark_paths_invalid(struct net_device *dev)
349 {
350 struct ipoib_dev_priv *priv = netdev_priv(dev);
351 struct ipoib_path *path, *tp;
352
353 spin_lock_irq(&priv->lock);
354
355 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
356 ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n",
357 be16_to_cpu(path->pathrec.dlid),
358 path->pathrec.dgid.raw);
359 path->valid = 0;
360 }
361
362 spin_unlock_irq(&priv->lock);
363 }
364
365 void ipoib_flush_paths(struct net_device *dev)
366 {
367 struct ipoib_dev_priv *priv = netdev_priv(dev);
368 struct ipoib_path *path, *tp;
369 LIST_HEAD(remove_list);
370 unsigned long flags;
371
372 netif_tx_lock_bh(dev);
373 spin_lock_irqsave(&priv->lock, flags);
374
375 list_splice_init(&priv->path_list, &remove_list);
376
377 list_for_each_entry(path, &remove_list, list)
378 rb_erase(&path->rb_node, &priv->path_tree);
379
380 list_for_each_entry_safe(path, tp, &remove_list, list) {
381 if (path->query)
382 ib_sa_cancel_query(path->query_id, path->query);
383 spin_unlock_irqrestore(&priv->lock, flags);
384 netif_tx_unlock_bh(dev);
385 wait_for_completion(&path->done);
386 path_free(dev, path);
387 netif_tx_lock_bh(dev);
388 spin_lock_irqsave(&priv->lock, flags);
389 }
390
391 spin_unlock_irqrestore(&priv->lock, flags);
392 netif_tx_unlock_bh(dev);
393 }
394
395 static void path_rec_completion(int status,
396 struct ib_sa_path_rec *pathrec,
397 void *path_ptr)
398 {
399 struct ipoib_path *path = path_ptr;
400 struct net_device *dev = path->dev;
401 struct ipoib_dev_priv *priv = netdev_priv(dev);
402 struct ipoib_ah *ah = NULL;
403 struct ipoib_ah *old_ah = NULL;
404 struct ipoib_neigh *neigh, *tn;
405 struct sk_buff_head skqueue;
406 struct sk_buff *skb;
407 unsigned long flags;
408
409 if (!status)
410 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
411 be16_to_cpu(pathrec->dlid), pathrec->dgid.raw);
412 else
413 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
414 status, path->pathrec.dgid.raw);
415
416 skb_queue_head_init(&skqueue);
417
418 if (!status) {
419 struct ib_ah_attr av;
420
421 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
422 ah = ipoib_create_ah(dev, priv->pd, &av);
423 }
424
425 spin_lock_irqsave(&priv->lock, flags);
426
427 if (!IS_ERR_OR_NULL(ah)) {
428 path->pathrec = *pathrec;
429
430 old_ah = path->ah;
431 path->ah = ah;
432
433 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
434 ah, be16_to_cpu(pathrec->dlid), pathrec->sl);
435
436 while ((skb = __skb_dequeue(&path->queue)))
437 __skb_queue_tail(&skqueue, skb);
438
439 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
440 if (neigh->ah) {
441 WARN_ON(neigh->ah != old_ah);
442 /*
443 * Dropping the ah reference inside
444 * priv->lock is safe here, because we
445 * will hold one more reference from
446 * the original value of path->ah (ie
447 * old_ah).
448 */
449 ipoib_put_ah(neigh->ah);
450 }
451 kref_get(&path->ah->ref);
452 neigh->ah = path->ah;
453
454 if (ipoib_cm_enabled(dev, neigh->daddr)) {
455 if (!ipoib_cm_get(neigh))
456 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
457 path,
458 neigh));
459 if (!ipoib_cm_get(neigh)) {
460 list_del(&neigh->list);
461 ipoib_neigh_free(neigh);
462 continue;
463 }
464 }
465
466 while ((skb = __skb_dequeue(&neigh->queue)))
467 __skb_queue_tail(&skqueue, skb);
468 }
469 path->valid = 1;
470 }
471
472 path->query = NULL;
473 complete(&path->done);
474
475 spin_unlock_irqrestore(&priv->lock, flags);
476
477 if (old_ah)
478 ipoib_put_ah(old_ah);
479
480 while ((skb = __skb_dequeue(&skqueue))) {
481 skb->dev = dev;
482 if (dev_queue_xmit(skb))
483 ipoib_warn(priv, "dev_queue_xmit failed "
484 "to requeue packet\n");
485 }
486 }
487
488 static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
489 {
490 struct ipoib_dev_priv *priv = netdev_priv(dev);
491 struct ipoib_path *path;
492
493 if (!priv->broadcast)
494 return NULL;
495
496 path = kzalloc(sizeof *path, GFP_ATOMIC);
497 if (!path)
498 return NULL;
499
500 path->dev = dev;
501
502 skb_queue_head_init(&path->queue);
503
504 INIT_LIST_HEAD(&path->neigh_list);
505
506 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
507 path->pathrec.sgid = priv->local_gid;
508 path->pathrec.pkey = cpu_to_be16(priv->pkey);
509 path->pathrec.numb_path = 1;
510 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
511
512 return path;
513 }
514
515 static int path_rec_start(struct net_device *dev,
516 struct ipoib_path *path)
517 {
518 struct ipoib_dev_priv *priv = netdev_priv(dev);
519
520 ipoib_dbg(priv, "Start path record lookup for %pI6\n",
521 path->pathrec.dgid.raw);
522
523 init_completion(&path->done);
524
525 path->query_id =
526 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
527 &path->pathrec,
528 IB_SA_PATH_REC_DGID |
529 IB_SA_PATH_REC_SGID |
530 IB_SA_PATH_REC_NUMB_PATH |
531 IB_SA_PATH_REC_TRAFFIC_CLASS |
532 IB_SA_PATH_REC_PKEY,
533 1000, GFP_ATOMIC,
534 path_rec_completion,
535 path, &path->query);
536 if (path->query_id < 0) {
537 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
538 path->query = NULL;
539 complete(&path->done);
540 return path->query_id;
541 }
542
543 return 0;
544 }
545
546 static void neigh_add_path(struct sk_buff *skb, u8 *daddr,
547 struct net_device *dev)
548 {
549 struct ipoib_dev_priv *priv = netdev_priv(dev);
550 struct ipoib_path *path;
551 struct ipoib_neigh *neigh;
552 unsigned long flags;
553
554 neigh = ipoib_neigh_alloc(daddr, dev);
555 if (!neigh) {
556 ++dev->stats.tx_dropped;
557 dev_kfree_skb_any(skb);
558 return;
559 }
560
561 spin_lock_irqsave(&priv->lock, flags);
562
563 path = __path_find(dev, daddr + 4);
564 if (!path) {
565 path = path_rec_create(dev, daddr + 4);
566 if (!path)
567 goto err_path;
568
569 __path_add(dev, path);
570 }
571
572 list_add_tail(&neigh->list, &path->neigh_list);
573
574 if (path->ah) {
575 kref_get(&path->ah->ref);
576 neigh->ah = path->ah;
577
578 if (ipoib_cm_enabled(dev, neigh->daddr)) {
579 if (!ipoib_cm_get(neigh))
580 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
581 if (!ipoib_cm_get(neigh)) {
582 list_del(&neigh->list);
583 ipoib_neigh_free(neigh);
584 goto err_drop;
585 }
586 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)
587 __skb_queue_tail(&neigh->queue, skb);
588 else {
589 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
590 skb_queue_len(&neigh->queue));
591 goto err_drop;
592 }
593 } else {
594 spin_unlock_irqrestore(&priv->lock, flags);
595 ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr));
596 ipoib_neigh_put(neigh);
597 return;
598 }
599 } else {
600 neigh->ah = NULL;
601
602 if (!path->query && path_rec_start(dev, path))
603 goto err_list;
604
605 __skb_queue_tail(&neigh->queue, skb);
606 }
607
608 spin_unlock_irqrestore(&priv->lock, flags);
609 ipoib_neigh_put(neigh);
610 return;
611
612 err_list:
613 list_del(&neigh->list);
614
615 err_path:
616 ipoib_neigh_free(neigh);
617 err_drop:
618 ++dev->stats.tx_dropped;
619 dev_kfree_skb_any(skb);
620
621 spin_unlock_irqrestore(&priv->lock, flags);
622 ipoib_neigh_put(neigh);
623 }
624
625 static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
626 struct ipoib_cb *cb)
627 {
628 struct ipoib_dev_priv *priv = netdev_priv(dev);
629 struct ipoib_path *path;
630 unsigned long flags;
631
632 spin_lock_irqsave(&priv->lock, flags);
633
634 path = __path_find(dev, cb->hwaddr + 4);
635 if (!path || !path->valid) {
636 int new_path = 0;
637
638 if (!path) {
639 path = path_rec_create(dev, cb->hwaddr + 4);
640 new_path = 1;
641 }
642 if (path) {
643 __skb_queue_tail(&path->queue, skb);
644
645 if (!path->query && path_rec_start(dev, path)) {
646 spin_unlock_irqrestore(&priv->lock, flags);
647 if (new_path)
648 path_free(dev, path);
649 return;
650 } else
651 __path_add(dev, path);
652 } else {
653 ++dev->stats.tx_dropped;
654 dev_kfree_skb_any(skb);
655 }
656
657 spin_unlock_irqrestore(&priv->lock, flags);
658 return;
659 }
660
661 if (path->ah) {
662 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
663 be16_to_cpu(path->pathrec.dlid));
664
665 spin_unlock_irqrestore(&priv->lock, flags);
666 ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr));
667 return;
668 } else if ((path->query || !path_rec_start(dev, path)) &&
669 skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
670 __skb_queue_tail(&path->queue, skb);
671 } else {
672 ++dev->stats.tx_dropped;
673 dev_kfree_skb_any(skb);
674 }
675
676 spin_unlock_irqrestore(&priv->lock, flags);
677 }
678
679 static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
680 {
681 struct ipoib_dev_priv *priv = netdev_priv(dev);
682 struct ipoib_neigh *neigh;
683 struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
684 struct ipoib_header *header;
685 unsigned long flags;
686
687 header = (struct ipoib_header *) skb->data;
688
689 if (unlikely(cb->hwaddr[4] == 0xff)) {
690 /* multicast, arrange "if" according to probability */
691 if ((header->proto != htons(ETH_P_IP)) &&
692 (header->proto != htons(ETH_P_IPV6)) &&
693 (header->proto != htons(ETH_P_ARP)) &&
694 (header->proto != htons(ETH_P_RARP))) {
695 /* ethertype not supported by IPoIB */
696 ++dev->stats.tx_dropped;
697 dev_kfree_skb_any(skb);
698 return NETDEV_TX_OK;
699 }
700 /* Add in the P_Key for multicast*/
701 cb->hwaddr[8] = (priv->pkey >> 8) & 0xff;
702 cb->hwaddr[9] = priv->pkey & 0xff;
703
704 neigh = ipoib_neigh_get(dev, cb->hwaddr);
705 if (likely(neigh))
706 goto send_using_neigh;
707 ipoib_mcast_send(dev, cb->hwaddr, skb);
708 return NETDEV_TX_OK;
709 }
710
711 /* unicast, arrange "switch" according to probability */
712 switch (header->proto) {
713 case htons(ETH_P_IP):
714 case htons(ETH_P_IPV6):
715 neigh = ipoib_neigh_get(dev, cb->hwaddr);
716 if (unlikely(!neigh)) {
717 neigh_add_path(skb, cb->hwaddr, dev);
718 return NETDEV_TX_OK;
719 }
720 break;
721 case htons(ETH_P_ARP):
722 case htons(ETH_P_RARP):
723 /* for unicast ARP and RARP should always perform path find */
724 unicast_arp_send(skb, dev, cb);
725 return NETDEV_TX_OK;
726 default:
727 /* ethertype not supported by IPoIB */
728 ++dev->stats.tx_dropped;
729 dev_kfree_skb_any(skb);
730 return NETDEV_TX_OK;
731 }
732
733 send_using_neigh:
734 /* note we now hold a ref to neigh */
735 if (ipoib_cm_get(neigh)) {
736 if (ipoib_cm_up(neigh)) {
737 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
738 goto unref;
739 }
740 } else if (neigh->ah) {
741 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr));
742 goto unref;
743 }
744
745 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
746 spin_lock_irqsave(&priv->lock, flags);
747 __skb_queue_tail(&neigh->queue, skb);
748 spin_unlock_irqrestore(&priv->lock, flags);
749 } else {
750 ++dev->stats.tx_dropped;
751 dev_kfree_skb_any(skb);
752 }
753
754 unref:
755 ipoib_neigh_put(neigh);
756
757 return NETDEV_TX_OK;
758 }
759
760 static void ipoib_timeout(struct net_device *dev)
761 {
762 struct ipoib_dev_priv *priv = netdev_priv(dev);
763
764 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
765 jiffies_to_msecs(jiffies - dev->trans_start));
766 ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n",
767 netif_queue_stopped(dev),
768 priv->tx_head, priv->tx_tail);
769 /* XXX reset QP, etc. */
770 }
771
772 static int ipoib_hard_header(struct sk_buff *skb,
773 struct net_device *dev,
774 unsigned short type,
775 const void *daddr, const void *saddr, unsigned len)
776 {
777 struct ipoib_header *header;
778 struct ipoib_cb *cb = (struct ipoib_cb *) skb->cb;
779
780 header = (struct ipoib_header *) skb_push(skb, sizeof *header);
781
782 header->proto = htons(type);
783 header->reserved = 0;
784
785 /*
786 * we don't rely on dst_entry structure, always stuff the
787 * destination address into skb->cb so we can figure out where
788 * to send the packet later.
789 */
790 memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN);
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 u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
808 {
809 /*
810 * Use only the address parts that contributes to spreading
811 * The subnet prefix is not used as one can not connect to
812 * same remote port (GUID) using the same remote QPN via two
813 * different subnets.
814 */
815 /* qpn octets[1:4) & port GUID octets[12:20) */
816 u32 *daddr_32 = (u32 *) daddr;
817 u32 hv;
818
819 hv = jhash_3words(daddr_32[3], daddr_32[4], 0xFFFFFF & daddr_32[0], 0);
820 return hv & htbl->mask;
821 }
822
823 struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
824 {
825 struct ipoib_dev_priv *priv = netdev_priv(dev);
826 struct ipoib_neigh_table *ntbl = &priv->ntbl;
827 struct ipoib_neigh_hash *htbl;
828 struct ipoib_neigh *neigh = NULL;
829 u32 hash_val;
830
831 rcu_read_lock_bh();
832
833 htbl = rcu_dereference_bh(ntbl->htbl);
834
835 if (!htbl)
836 goto out_unlock;
837
838 hash_val = ipoib_addr_hash(htbl, daddr);
839 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
840 neigh != NULL;
841 neigh = rcu_dereference_bh(neigh->hnext)) {
842 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
843 /* found, take one ref on behalf of the caller */
844 if (!atomic_inc_not_zero(&neigh->refcnt)) {
845 /* deleted */
846 neigh = NULL;
847 goto out_unlock;
848 }
849 neigh->alive = jiffies;
850 goto out_unlock;
851 }
852 }
853
854 out_unlock:
855 rcu_read_unlock_bh();
856 return neigh;
857 }
858
859 static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
860 {
861 struct ipoib_neigh_table *ntbl = &priv->ntbl;
862 struct ipoib_neigh_hash *htbl;
863 unsigned long neigh_obsolete;
864 unsigned long dt;
865 unsigned long flags;
866 int i;
867
868 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
869 return;
870
871 write_lock_bh(&ntbl->rwlock);
872
873 htbl = rcu_dereference_protected(ntbl->htbl,
874 lockdep_is_held(&ntbl->rwlock));
875
876 if (!htbl)
877 goto out_unlock;
878
879 /* neigh is obsolete if it was idle for two GC periods */
880 dt = 2 * arp_tbl.gc_interval;
881 neigh_obsolete = jiffies - dt;
882 /* handle possible race condition */
883 if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
884 goto out_unlock;
885
886 for (i = 0; i < htbl->size; i++) {
887 struct ipoib_neigh *neigh;
888 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
889
890 while ((neigh = rcu_dereference_protected(*np,
891 lockdep_is_held(&ntbl->rwlock))) != NULL) {
892 /* was the neigh idle for two GC periods */
893 if (time_after(neigh_obsolete, neigh->alive)) {
894 rcu_assign_pointer(*np,
895 rcu_dereference_protected(neigh->hnext,
896 lockdep_is_held(&ntbl->rwlock)));
897 /* remove from path/mc list */
898 spin_lock_irqsave(&priv->lock, flags);
899 list_del(&neigh->list);
900 spin_unlock_irqrestore(&priv->lock, flags);
901 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
902 } else {
903 np = &neigh->hnext;
904 }
905
906 }
907 }
908
909 out_unlock:
910 write_unlock_bh(&ntbl->rwlock);
911 }
912
913 static void ipoib_reap_neigh(struct work_struct *work)
914 {
915 struct ipoib_dev_priv *priv =
916 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
917
918 __ipoib_reap_neigh(priv);
919
920 if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags))
921 queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
922 arp_tbl.gc_interval);
923 }
924
925
926 static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
927 struct net_device *dev)
928 {
929 struct ipoib_neigh *neigh;
930
931 neigh = kzalloc(sizeof *neigh, GFP_ATOMIC);
932 if (!neigh)
933 return NULL;
934
935 neigh->dev = dev;
936 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
937 skb_queue_head_init(&neigh->queue);
938 INIT_LIST_HEAD(&neigh->list);
939 ipoib_cm_set(neigh, NULL);
940 /* one ref on behalf of the caller */
941 atomic_set(&neigh->refcnt, 1);
942
943 return neigh;
944 }
945
946 struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
947 struct net_device *dev)
948 {
949 struct ipoib_dev_priv *priv = netdev_priv(dev);
950 struct ipoib_neigh_table *ntbl = &priv->ntbl;
951 struct ipoib_neigh_hash *htbl;
952 struct ipoib_neigh *neigh;
953 u32 hash_val;
954
955 write_lock_bh(&ntbl->rwlock);
956
957 htbl = rcu_dereference_protected(ntbl->htbl,
958 lockdep_is_held(&ntbl->rwlock));
959 if (!htbl) {
960 neigh = NULL;
961 goto out_unlock;
962 }
963
964 /* need to add a new neigh, but maybe some other thread succeeded?
965 * recalc hash, maybe hash resize took place so we do a search
966 */
967 hash_val = ipoib_addr_hash(htbl, daddr);
968 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
969 lockdep_is_held(&ntbl->rwlock));
970 neigh != NULL;
971 neigh = rcu_dereference_protected(neigh->hnext,
972 lockdep_is_held(&ntbl->rwlock))) {
973 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
974 /* found, take one ref on behalf of the caller */
975 if (!atomic_inc_not_zero(&neigh->refcnt)) {
976 /* deleted */
977 neigh = NULL;
978 break;
979 }
980 neigh->alive = jiffies;
981 goto out_unlock;
982 }
983 }
984
985 neigh = ipoib_neigh_ctor(daddr, dev);
986 if (!neigh)
987 goto out_unlock;
988
989 /* one ref on behalf of the hash table */
990 atomic_inc(&neigh->refcnt);
991 neigh->alive = jiffies;
992 /* put in hash */
993 rcu_assign_pointer(neigh->hnext,
994 rcu_dereference_protected(htbl->buckets[hash_val],
995 lockdep_is_held(&ntbl->rwlock)));
996 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
997 atomic_inc(&ntbl->entries);
998
999 out_unlock:
1000 write_unlock_bh(&ntbl->rwlock);
1001
1002 return neigh;
1003 }
1004
1005 void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1006 {
1007 /* neigh reference count was dropprd to zero */
1008 struct net_device *dev = neigh->dev;
1009 struct ipoib_dev_priv *priv = netdev_priv(dev);
1010 struct sk_buff *skb;
1011 if (neigh->ah)
1012 ipoib_put_ah(neigh->ah);
1013 while ((skb = __skb_dequeue(&neigh->queue))) {
1014 ++dev->stats.tx_dropped;
1015 dev_kfree_skb_any(skb);
1016 }
1017 if (ipoib_cm_get(neigh))
1018 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
1019 ipoib_dbg(netdev_priv(dev),
1020 "neigh free for %06x %pI6\n",
1021 IPOIB_QPN(neigh->daddr),
1022 neigh->daddr + 4);
1023 kfree(neigh);
1024 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1025 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1026 complete(&priv->ntbl.flushed);
1027 }
1028 }
1029
1030 static void ipoib_neigh_reclaim(struct rcu_head *rp)
1031 {
1032 /* Called as a result of removal from hash table */
1033 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1034 /* note TX context may hold another ref */
1035 ipoib_neigh_put(neigh);
1036 }
1037
1038 void ipoib_neigh_free(struct ipoib_neigh *neigh)
1039 {
1040 struct net_device *dev = neigh->dev;
1041 struct ipoib_dev_priv *priv = netdev_priv(dev);
1042 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1043 struct ipoib_neigh_hash *htbl;
1044 struct ipoib_neigh __rcu **np;
1045 struct ipoib_neigh *n;
1046 u32 hash_val;
1047
1048 write_lock_bh(&ntbl->rwlock);
1049
1050 htbl = rcu_dereference_protected(ntbl->htbl,
1051 lockdep_is_held(&ntbl->rwlock));
1052 if (!htbl)
1053 goto out_unlock;
1054
1055 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1056 np = &htbl->buckets[hash_val];
1057 for (n = rcu_dereference_protected(*np,
1058 lockdep_is_held(&ntbl->rwlock));
1059 n != NULL;
1060 n = rcu_dereference_protected(*np,
1061 lockdep_is_held(&ntbl->rwlock))) {
1062 if (n == neigh) {
1063 /* found */
1064 rcu_assign_pointer(*np,
1065 rcu_dereference_protected(neigh->hnext,
1066 lockdep_is_held(&ntbl->rwlock)));
1067 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1068 goto out_unlock;
1069 } else {
1070 np = &n->hnext;
1071 }
1072 }
1073
1074 out_unlock:
1075 write_unlock_bh(&ntbl->rwlock);
1076
1077 }
1078
1079 static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1080 {
1081 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1082 struct ipoib_neigh_hash *htbl;
1083 struct ipoib_neigh **buckets;
1084 u32 size;
1085
1086 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1087 ntbl->htbl = NULL;
1088 rwlock_init(&ntbl->rwlock);
1089 htbl = kzalloc(sizeof(*htbl), GFP_KERNEL);
1090 if (!htbl)
1091 return -ENOMEM;
1092 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1093 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1094 buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL);
1095 if (!buckets) {
1096 kfree(htbl);
1097 return -ENOMEM;
1098 }
1099 htbl->size = size;
1100 htbl->mask = (size - 1);
1101 htbl->buckets = buckets;
1102 ntbl->htbl = htbl;
1103 atomic_set(&ntbl->entries, 0);
1104
1105 /* start garbage collection */
1106 clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1107 queue_delayed_work(ipoib_workqueue, &priv->neigh_reap_task,
1108 arp_tbl.gc_interval);
1109
1110 return 0;
1111 }
1112
1113 static void neigh_hash_free_rcu(struct rcu_head *head)
1114 {
1115 struct ipoib_neigh_hash *htbl = container_of(head,
1116 struct ipoib_neigh_hash,
1117 rcu);
1118 struct ipoib_neigh __rcu **buckets = htbl->buckets;
1119
1120 kfree(buckets);
1121 kfree(htbl);
1122 }
1123
1124 void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1125 {
1126 struct ipoib_dev_priv *priv = netdev_priv(dev);
1127 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1128 struct ipoib_neigh_hash *htbl;
1129 unsigned long flags;
1130 int i;
1131
1132 /* remove all neigh connected to a given path or mcast */
1133 write_lock_bh(&ntbl->rwlock);
1134
1135 htbl = rcu_dereference_protected(ntbl->htbl,
1136 lockdep_is_held(&ntbl->rwlock));
1137
1138 if (!htbl)
1139 goto out_unlock;
1140
1141 for (i = 0; i < htbl->size; i++) {
1142 struct ipoib_neigh *neigh;
1143 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1144
1145 while ((neigh = rcu_dereference_protected(*np,
1146 lockdep_is_held(&ntbl->rwlock))) != NULL) {
1147 /* delete neighs belong to this parent */
1148 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1149 rcu_assign_pointer(*np,
1150 rcu_dereference_protected(neigh->hnext,
1151 lockdep_is_held(&ntbl->rwlock)));
1152 /* remove from parent list */
1153 spin_lock_irqsave(&priv->lock, flags);
1154 list_del(&neigh->list);
1155 spin_unlock_irqrestore(&priv->lock, flags);
1156 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1157 } else {
1158 np = &neigh->hnext;
1159 }
1160
1161 }
1162 }
1163 out_unlock:
1164 write_unlock_bh(&ntbl->rwlock);
1165 }
1166
1167 static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1168 {
1169 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1170 struct ipoib_neigh_hash *htbl;
1171 unsigned long flags;
1172 int i;
1173
1174 write_lock_bh(&ntbl->rwlock);
1175
1176 htbl = rcu_dereference_protected(ntbl->htbl,
1177 lockdep_is_held(&ntbl->rwlock));
1178 if (!htbl)
1179 goto out_unlock;
1180
1181 for (i = 0; i < htbl->size; i++) {
1182 struct ipoib_neigh *neigh;
1183 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1184
1185 while ((neigh = rcu_dereference_protected(*np,
1186 lockdep_is_held(&ntbl->rwlock))) != NULL) {
1187 rcu_assign_pointer(*np,
1188 rcu_dereference_protected(neigh->hnext,
1189 lockdep_is_held(&ntbl->rwlock)));
1190 /* remove from path/mc list */
1191 spin_lock_irqsave(&priv->lock, flags);
1192 list_del(&neigh->list);
1193 spin_unlock_irqrestore(&priv->lock, flags);
1194 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1195 }
1196 }
1197
1198 rcu_assign_pointer(ntbl->htbl, NULL);
1199 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1200
1201 out_unlock:
1202 write_unlock_bh(&ntbl->rwlock);
1203 }
1204
1205 static void ipoib_neigh_hash_uninit(struct net_device *dev)
1206 {
1207 struct ipoib_dev_priv *priv = netdev_priv(dev);
1208 int stopped;
1209
1210 ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n");
1211 init_completion(&priv->ntbl.flushed);
1212 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1213
1214 /* Stop GC if called at init fail need to cancel work */
1215 stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1216 if (!stopped)
1217 cancel_delayed_work(&priv->neigh_reap_task);
1218
1219 if (atomic_read(&priv->ntbl.entries)) {
1220 ipoib_flush_neighs(priv);
1221 wait_for_completion(&priv->ntbl.flushed);
1222 }
1223 }
1224
1225
1226 int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port)
1227 {
1228 struct ipoib_dev_priv *priv = netdev_priv(dev);
1229
1230 if (ipoib_neigh_hash_init(priv) < 0)
1231 goto out;
1232 /* Allocate RX/TX "rings" to hold queued skbs */
1233 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
1234 GFP_KERNEL);
1235 if (!priv->rx_ring) {
1236 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
1237 ca->name, ipoib_recvq_size);
1238 goto out_neigh_hash_cleanup;
1239 }
1240
1241 priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
1242 if (!priv->tx_ring) {
1243 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
1244 ca->name, ipoib_sendq_size);
1245 goto out_rx_ring_cleanup;
1246 }
1247
1248 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
1249
1250 if (ipoib_ib_dev_init(dev, ca, port))
1251 goto out_tx_ring_cleanup;
1252
1253 return 0;
1254
1255 out_tx_ring_cleanup:
1256 vfree(priv->tx_ring);
1257
1258 out_rx_ring_cleanup:
1259 kfree(priv->rx_ring);
1260
1261 out_neigh_hash_cleanup:
1262 ipoib_neigh_hash_uninit(dev);
1263 out:
1264 return -ENOMEM;
1265 }
1266
1267 void ipoib_dev_cleanup(struct net_device *dev)
1268 {
1269 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
1270 LIST_HEAD(head);
1271
1272 ASSERT_RTNL();
1273
1274 ipoib_delete_debug_files(dev);
1275
1276 /* Delete any child interfaces first */
1277 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) {
1278 /* Stop GC on child */
1279 set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags);
1280 cancel_delayed_work(&cpriv->neigh_reap_task);
1281 unregister_netdevice_queue(cpriv->dev, &head);
1282 }
1283 unregister_netdevice_many(&head);
1284
1285 ipoib_ib_dev_cleanup(dev);
1286
1287 kfree(priv->rx_ring);
1288 vfree(priv->tx_ring);
1289
1290 priv->rx_ring = NULL;
1291 priv->tx_ring = NULL;
1292
1293 ipoib_neigh_hash_uninit(dev);
1294 }
1295
1296 static const struct header_ops ipoib_header_ops = {
1297 .create = ipoib_hard_header,
1298 };
1299
1300 static const struct net_device_ops ipoib_netdev_ops = {
1301 .ndo_uninit = ipoib_uninit,
1302 .ndo_open = ipoib_open,
1303 .ndo_stop = ipoib_stop,
1304 .ndo_change_mtu = ipoib_change_mtu,
1305 .ndo_fix_features = ipoib_fix_features,
1306 .ndo_start_xmit = ipoib_start_xmit,
1307 .ndo_tx_timeout = ipoib_timeout,
1308 .ndo_set_rx_mode = ipoib_set_mcast_list,
1309 };
1310
1311 void ipoib_setup(struct net_device *dev)
1312 {
1313 struct ipoib_dev_priv *priv = netdev_priv(dev);
1314
1315 dev->netdev_ops = &ipoib_netdev_ops;
1316 dev->header_ops = &ipoib_header_ops;
1317
1318 ipoib_set_ethtool_ops(dev);
1319
1320 netif_napi_add(dev, &priv->napi, ipoib_poll, 100);
1321
1322 dev->watchdog_timeo = HZ;
1323
1324 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
1325
1326 dev->hard_header_len = IPOIB_ENCAP_LEN;
1327 dev->addr_len = INFINIBAND_ALEN;
1328 dev->type = ARPHRD_INFINIBAND;
1329 dev->tx_queue_len = ipoib_sendq_size * 2;
1330 dev->features = (NETIF_F_VLAN_CHALLENGED |
1331 NETIF_F_HIGHDMA);
1332 dev->priv_flags &= ~IFF_XMIT_DST_RELEASE;
1333
1334 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
1335
1336 netif_carrier_off(dev);
1337
1338 priv->dev = dev;
1339
1340 spin_lock_init(&priv->lock);
1341
1342 mutex_init(&priv->vlan_mutex);
1343
1344 INIT_LIST_HEAD(&priv->path_list);
1345 INIT_LIST_HEAD(&priv->child_intfs);
1346 INIT_LIST_HEAD(&priv->dead_ahs);
1347 INIT_LIST_HEAD(&priv->multicast_list);
1348
1349 INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
1350 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
1351 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
1352 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
1353 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
1354 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
1355 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
1356 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
1357 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
1358 }
1359
1360 struct ipoib_dev_priv *ipoib_intf_alloc(const char *name)
1361 {
1362 struct net_device *dev;
1363
1364 dev = alloc_netdev((int) sizeof (struct ipoib_dev_priv), name,
1365 ipoib_setup);
1366 if (!dev)
1367 return NULL;
1368
1369 return netdev_priv(dev);
1370 }
1371
1372 static ssize_t show_pkey(struct device *dev,
1373 struct device_attribute *attr, char *buf)
1374 {
1375 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1376
1377 return sprintf(buf, "0x%04x\n", priv->pkey);
1378 }
1379 static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
1380
1381 static ssize_t show_umcast(struct device *dev,
1382 struct device_attribute *attr, char *buf)
1383 {
1384 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1385
1386 return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
1387 }
1388
1389 static ssize_t set_umcast(struct device *dev,
1390 struct device_attribute *attr,
1391 const char *buf, size_t count)
1392 {
1393 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
1394 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
1395
1396 if (umcast_val > 0) {
1397 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1398 ipoib_warn(priv, "ignoring multicast groups joined directly "
1399 "by userspace\n");
1400 } else
1401 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
1402
1403 return count;
1404 }
1405 static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1406
1407 int ipoib_add_umcast_attr(struct net_device *dev)
1408 {
1409 return device_create_file(&dev->dev, &dev_attr_umcast);
1410 }
1411
1412 static ssize_t create_child(struct device *dev,
1413 struct device_attribute *attr,
1414 const char *buf, size_t count)
1415 {
1416 int pkey;
1417 int ret;
1418
1419 if (sscanf(buf, "%i", &pkey) != 1)
1420 return -EINVAL;
1421
1422 if (pkey < 0 || pkey > 0xffff)
1423 return -EINVAL;
1424
1425 /*
1426 * Set the full membership bit, so that we join the right
1427 * broadcast group, etc.
1428 */
1429 pkey |= 0x8000;
1430
1431 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
1432
1433 return ret ? ret : count;
1434 }
1435 static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child);
1436
1437 static ssize_t delete_child(struct device *dev,
1438 struct device_attribute *attr,
1439 const char *buf, size_t count)
1440 {
1441 int pkey;
1442 int ret;
1443
1444 if (sscanf(buf, "%i", &pkey) != 1)
1445 return -EINVAL;
1446
1447 if (pkey < 0 || pkey > 0xffff)
1448 return -EINVAL;
1449
1450 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
1451
1452 return ret ? ret : count;
1453
1454 }
1455 static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child);
1456
1457 int ipoib_add_pkey_attr(struct net_device *dev)
1458 {
1459 return device_create_file(&dev->dev, &dev_attr_pkey);
1460 }
1461
1462 int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca)
1463 {
1464 struct ib_device_attr *device_attr;
1465 int result = -ENOMEM;
1466
1467 device_attr = kmalloc(sizeof *device_attr, GFP_KERNEL);
1468 if (!device_attr) {
1469 printk(KERN_WARNING "%s: allocation of %zu bytes failed\n",
1470 hca->name, sizeof *device_attr);
1471 return result;
1472 }
1473
1474 result = ib_query_device(hca, device_attr);
1475 if (result) {
1476 printk(KERN_WARNING "%s: ib_query_device failed (ret = %d)\n",
1477 hca->name, result);
1478 kfree(device_attr);
1479 return result;
1480 }
1481 priv->hca_caps = device_attr->device_cap_flags;
1482
1483 kfree(device_attr);
1484
1485 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
1486 priv->dev->hw_features = NETIF_F_SG |
1487 NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1488
1489 if (priv->hca_caps & IB_DEVICE_UD_TSO)
1490 priv->dev->hw_features |= NETIF_F_TSO;
1491
1492 priv->dev->features |= priv->dev->hw_features;
1493 }
1494
1495 return 0;
1496 }
1497
1498 static struct net_device *ipoib_add_port(const char *format,
1499 struct ib_device *hca, u8 port)
1500 {
1501 struct ipoib_dev_priv *priv;
1502 struct ib_port_attr attr;
1503 int result = -ENOMEM;
1504
1505 priv = ipoib_intf_alloc(format);
1506 if (!priv)
1507 goto alloc_mem_failed;
1508
1509 SET_NETDEV_DEV(priv->dev, hca->dma_device);
1510 priv->dev->dev_id = port - 1;
1511
1512 if (!ib_query_port(hca, port, &attr))
1513 priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu);
1514 else {
1515 printk(KERN_WARNING "%s: ib_query_port %d failed\n",
1516 hca->name, port);
1517 goto device_init_failed;
1518 }
1519
1520 /* MTU will be reset when mcast join happens */
1521 priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
1522 priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu;
1523
1524 priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh);
1525
1526 result = ib_query_pkey(hca, port, 0, &priv->pkey);
1527 if (result) {
1528 printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n",
1529 hca->name, port, result);
1530 goto device_init_failed;
1531 }
1532
1533 if (ipoib_set_dev_features(priv, hca))
1534 goto device_init_failed;
1535
1536 /*
1537 * Set the full membership bit, so that we join the right
1538 * broadcast group, etc.
1539 */
1540 priv->pkey |= 0x8000;
1541
1542 priv->dev->broadcast[8] = priv->pkey >> 8;
1543 priv->dev->broadcast[9] = priv->pkey & 0xff;
1544
1545 result = ib_query_gid(hca, port, 0, &priv->local_gid);
1546 if (result) {
1547 printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n",
1548 hca->name, port, result);
1549 goto device_init_failed;
1550 } else
1551 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1552
1553 result = ipoib_dev_init(priv->dev, hca, port);
1554 if (result < 0) {
1555 printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n",
1556 hca->name, port, result);
1557 goto device_init_failed;
1558 }
1559
1560 INIT_IB_EVENT_HANDLER(&priv->event_handler,
1561 priv->ca, ipoib_event);
1562 result = ib_register_event_handler(&priv->event_handler);
1563 if (result < 0) {
1564 printk(KERN_WARNING "%s: ib_register_event_handler failed for "
1565 "port %d (ret = %d)\n",
1566 hca->name, port, result);
1567 goto event_failed;
1568 }
1569
1570 result = register_netdev(priv->dev);
1571 if (result) {
1572 printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n",
1573 hca->name, port, result);
1574 goto register_failed;
1575 }
1576
1577 ipoib_create_debug_files(priv->dev);
1578
1579 if (ipoib_cm_add_mode_attr(priv->dev))
1580 goto sysfs_failed;
1581 if (ipoib_add_pkey_attr(priv->dev))
1582 goto sysfs_failed;
1583 if (ipoib_add_umcast_attr(priv->dev))
1584 goto sysfs_failed;
1585 if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
1586 goto sysfs_failed;
1587 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
1588 goto sysfs_failed;
1589
1590 return priv->dev;
1591
1592 sysfs_failed:
1593 ipoib_delete_debug_files(priv->dev);
1594 unregister_netdev(priv->dev);
1595
1596 register_failed:
1597 ib_unregister_event_handler(&priv->event_handler);
1598 /* Stop GC if started before flush */
1599 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1600 cancel_delayed_work(&priv->neigh_reap_task);
1601 flush_workqueue(ipoib_workqueue);
1602
1603 event_failed:
1604 ipoib_dev_cleanup(priv->dev);
1605
1606 device_init_failed:
1607 free_netdev(priv->dev);
1608
1609 alloc_mem_failed:
1610 return ERR_PTR(result);
1611 }
1612
1613 static void ipoib_add_one(struct ib_device *device)
1614 {
1615 struct list_head *dev_list;
1616 struct net_device *dev;
1617 struct ipoib_dev_priv *priv;
1618 int s, e, p;
1619
1620 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1621 return;
1622
1623 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1624 if (!dev_list)
1625 return;
1626
1627 INIT_LIST_HEAD(dev_list);
1628
1629 if (device->node_type == RDMA_NODE_IB_SWITCH) {
1630 s = 0;
1631 e = 0;
1632 } else {
1633 s = 1;
1634 e = device->phys_port_cnt;
1635 }
1636
1637 for (p = s; p <= e; ++p) {
1638 if (rdma_port_get_link_layer(device, p) != IB_LINK_LAYER_INFINIBAND)
1639 continue;
1640 dev = ipoib_add_port("ib%d", device, p);
1641 if (!IS_ERR(dev)) {
1642 priv = netdev_priv(dev);
1643 list_add_tail(&priv->list, dev_list);
1644 }
1645 }
1646
1647 ib_set_client_data(device, &ipoib_client, dev_list);
1648 }
1649
1650 static void ipoib_remove_one(struct ib_device *device)
1651 {
1652 struct ipoib_dev_priv *priv, *tmp;
1653 struct list_head *dev_list;
1654
1655 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1656 return;
1657
1658 dev_list = ib_get_client_data(device, &ipoib_client);
1659
1660 list_for_each_entry_safe(priv, tmp, dev_list, list) {
1661 ib_unregister_event_handler(&priv->event_handler);
1662
1663 rtnl_lock();
1664 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP);
1665 rtnl_unlock();
1666
1667 /* Stop GC */
1668 set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags);
1669 cancel_delayed_work(&priv->neigh_reap_task);
1670 flush_workqueue(ipoib_workqueue);
1671
1672 unregister_netdev(priv->dev);
1673 free_netdev(priv->dev);
1674 }
1675
1676 kfree(dev_list);
1677 }
1678
1679 static int __init ipoib_init_module(void)
1680 {
1681 int ret;
1682
1683 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
1684 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
1685 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
1686
1687 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
1688 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
1689 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
1690 #ifdef CONFIG_INFINIBAND_IPOIB_CM
1691 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1692 #endif
1693
1694 /*
1695 * When copying small received packets, we only copy from the
1696 * linear data part of the SKB, so we rely on this condition.
1697 */
1698 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
1699
1700 ret = ipoib_register_debugfs();
1701 if (ret)
1702 return ret;
1703
1704 /*
1705 * We create our own workqueue mainly because we want to be
1706 * able to flush it when devices are being removed. We can't
1707 * use schedule_work()/flush_scheduled_work() because both
1708 * unregister_netdev() and linkwatch_event take the rtnl lock,
1709 * so flush_scheduled_work() can deadlock during device
1710 * removal.
1711 */
1712 ipoib_workqueue = create_singlethread_workqueue("ipoib");
1713 if (!ipoib_workqueue) {
1714 ret = -ENOMEM;
1715 goto err_fs;
1716 }
1717
1718 ib_sa_register_client(&ipoib_sa_client);
1719
1720 ret = ib_register_client(&ipoib_client);
1721 if (ret)
1722 goto err_sa;
1723
1724 ret = ipoib_netlink_init();
1725 if (ret)
1726 goto err_client;
1727
1728 return 0;
1729
1730 err_client:
1731 ib_unregister_client(&ipoib_client);
1732
1733 err_sa:
1734 ib_sa_unregister_client(&ipoib_sa_client);
1735 destroy_workqueue(ipoib_workqueue);
1736
1737 err_fs:
1738 ipoib_unregister_debugfs();
1739
1740 return ret;
1741 }
1742
1743 static void __exit ipoib_cleanup_module(void)
1744 {
1745 ipoib_netlink_fini();
1746 ib_unregister_client(&ipoib_client);
1747 ib_sa_unregister_client(&ipoib_sa_client);
1748 ipoib_unregister_debugfs();
1749 destroy_workqueue(ipoib_workqueue);
1750 }
1751
1752 module_init(ipoib_init_module);
1753 module_exit(ipoib_cleanup_module);
This page took 0.068668 seconds and 6 git commands to generate.