blob: bfe1dbf9920775d1a3632bd805d881e858eac99b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
Roland Dreier2a1d9b72005-08-10 23:03:10 -07003 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 *
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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 */
34
35#include "ipoib.h"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#include <linux/module.h>
38
39#include <linux/init.h>
40#include <linux/slab.h>
Shirley Ma0f485252006-04-10 09:43:58 -070041#include <linux/kernel.h>
Roland Dreier10313cb2008-03-12 07:51:03 -070042#include <linux/vmalloc.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44#include <linux/if_arp.h> /* For ARPHRD_xxx */
45
46#include <linux/ip.h>
47#include <linux/in.h>
48
Arnaldo Carvalho de Melo14c85022005-12-27 02:43:12 -020049#include <net/dst.h>
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051MODULE_AUTHOR("Roland Dreier");
52MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
53MODULE_LICENSE("Dual BSD/GPL");
54
Shirley Ma0f485252006-04-10 09:43:58 -070055int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
56int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
57
58module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
59MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
60module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
61MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
62
Linus Torvalds1da177e2005-04-16 15:20:36 -070063#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
64int ipoib_debug_level;
65
66module_param_named(debug_level, ipoib_debug_level, int, 0644);
67MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
68#endif
69
Roland Dreier1732b0e2005-11-07 10:33:11 -080070struct ipoib_path_iter {
71 struct net_device *dev;
72 struct ipoib_path path;
73};
74
Linus Torvalds1da177e2005-04-16 15:20:36 -070075static 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
81struct workqueue_struct *ipoib_workqueue;
82
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -070083struct ib_sa_client ipoib_sa_client;
84
Linus Torvalds1da177e2005-04-16 15:20:36 -070085static void ipoib_add_one(struct ib_device *device);
86static void ipoib_remove_one(struct ib_device *device);
87
88static struct ib_client ipoib_client = {
89 .name = "ipoib",
90 .add = ipoib_add_one,
91 .remove = ipoib_remove_one
92};
93
94int 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
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700100 napi_enable(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
102
103 if (ipoib_pkey_dev_delay_open(dev))
104 return 0;
105
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700106 if (ipoib_ib_dev_open(dev)) {
107 napi_disable(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 return -EINVAL;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700109 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Roland Dreier267ee882005-11-29 10:55:58 -0800111 if (ipoib_ib_dev_up(dev)) {
Yosef Etigin26bbf132007-05-19 08:51:54 -0700112 ipoib_ib_dev_stop(dev, 1);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700113 napi_disable(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 return -EINVAL;
Roland Dreier267ee882005-11-29 10:55:58 -0800115 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116
117 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
118 struct ipoib_dev_priv *cpriv;
119
120 /* Bring up any child interfaces too */
Ingo Molnar95ed6442006-01-13 14:51:39 -0800121 mutex_lock(&priv->vlan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 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 }
Ingo Molnar95ed6442006-01-13 14:51:39 -0800131 mutex_unlock(&priv->vlan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132 }
133
134 netif_start_queue(dev);
135
136 return 0;
137}
138
139static 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);
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700146 napi_disable(&priv->napi);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
148 netif_stop_queue(dev);
149
Jack Morgenstein0b3ea082006-03-20 10:08:24 -0800150 /*
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);
Yosef Etigin26bbf132007-05-19 08:51:54 -0700157 ipoib_ib_dev_stop(dev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158
159 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
160 struct ipoib_dev_priv *cpriv;
161
162 /* Bring down any child interfaces too */
Ingo Molnar95ed6442006-01-13 14:51:39 -0800163 mutex_lock(&priv->vlan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 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 }
Ingo Molnar95ed6442006-01-13 14:51:39 -0800173 mutex_unlock(&priv->vlan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175
176 return 0;
177}
178
179static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
180{
181 struct ipoib_dev_priv *priv = netdev_priv(dev);
182
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200183 /* dev->mtu > 2K ==> connected mode */
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800184 if (ipoib_cm_admin_enabled(dev)) {
185 if (new_mtu > ipoib_cm_max_mtu(dev))
186 return -EINVAL;
187
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200188 if (new_mtu > priv->mcast_mtu)
189 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
190 priv->mcast_mtu);
Pradeep Satyanarayana586a6932007-12-21 13:08:23 -0800191
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200192 dev->mtu = new_mtu;
193 return 0;
194 }
195
Shirley Mabc7b3a32008-04-23 11:55:45 -0700196 if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 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
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300206static struct ipoib_path *__path_find(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
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
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300216 ret = memcmp(gid, path->pathrec.dgid.raw,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 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
230static 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
260static 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);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300281
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +0200282 ipoib_neigh_free(dev, neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 }
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
Roland Dreier1732b0e2005-11-07 10:33:11 -0800293#ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
294
295struct 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
314int 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
343void 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351void 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Michael S. Tsirkin9217b272006-08-03 22:16:06 +0300357 spin_lock_irq(&priv->tx_lock);
358 spin_lock(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Robert P. J. Day157de222008-04-16 21:09:26 -0700360 list_splice_init(&priv->path_list, &remove_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
362 list_for_each_entry(path, &remove_list, list)
363 rb_erase(&path->rb_node, &priv->path_tree);
364
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 list_for_each_entry_safe(path, tp, &remove_list, list) {
366 if (path->query)
367 ib_sa_cancel_query(path->query_id, path->query);
Michael S. Tsirkin9217b272006-08-03 22:16:06 +0300368 spin_unlock(&priv->lock);
369 spin_unlock_irq(&priv->tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 wait_for_completion(&path->done);
371 path_free(dev, path);
Michael S. Tsirkin9217b272006-08-03 22:16:06 +0300372 spin_lock_irq(&priv->tx_lock);
373 spin_lock(&priv->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 }
Michael S. Tsirkin9217b272006-08-03 22:16:06 +0300375 spin_unlock(&priv->lock);
376 spin_unlock_irq(&priv->tx_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700377}
378
379static 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;
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700387 struct ipoib_neigh *neigh, *tn;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 struct sk_buff_head skqueue;
389 struct sk_buff *skb;
390 unsigned long flags;
391
Roland Dreier843613b2007-02-26 12:57:08 -0800392 if (!status)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 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) {
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700402 struct ib_ah_attr av;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
Sean Hefty46f1b3d2007-04-05 11:50:11 -0700404 if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av))
405 ah = ipoib_create_ah(dev, priv->pd, &av);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
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
Michael S. Tsirkind04d01b2007-03-22 14:40:16 -0700421 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 kref_get(&path->ah->ref);
423 neigh->ah = path->ah;
Michael S. Tsirkin8a7f7522006-07-19 17:44:37 +0300424 memcpy(&neigh->dgid.raw, &path->pathrec.dgid.raw,
425 sizeof(union ib_gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200427 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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441 while ((skb = __skb_dequeue(&neigh->queue)))
442 __skb_queue_tail(&skqueue, skb);
443 }
Roland Dreier5872a9f2005-11-29 10:13:54 -0800444 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
Roland Dreier5872a9f2005-11-29 10:13:54 -0800446 path->query = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 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
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300459static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460{
461 struct ipoib_dev_priv *priv = netdev_priv(dev);
462 struct ipoib_path *path;
463
Jack Morgenstein1401b532007-11-26 10:41:19 +0200464 if (!priv->broadcast)
465 return NULL;
466
Roland Dreier21a38482005-11-02 10:07:59 -0800467 path = kzalloc(sizeof *path, GFP_ATOMIC);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 if (!path)
469 return NULL;
470
Roland Dreier21a38482005-11-02 10:07:59 -0800471 path->dev = dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
473 skb_queue_head_init(&path->queue);
474
475 INIT_LIST_HEAD(&path->neigh_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300477 memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid));
Roland Dreier2337f802007-10-23 19:57:54 -0700478 path->pathrec.sgid = priv->local_gid;
479 path->pathrec.pkey = cpu_to_be16(priv->pkey);
Sean Hefty81668832007-08-02 12:21:31 -0700480 path->pathrec.numb_path = 1;
481 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
483 return path;
484}
485
486static 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
Roland Dreier65c7edd2005-11-28 21:20:34 -0800494 init_completion(&path->done);
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 path->query_id =
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -0700497 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 &path->pathrec,
499 IB_SA_PATH_REC_DGID |
500 IB_SA_PATH_REC_SGID |
501 IB_SA_PATH_REC_NUMB_PATH |
Sean Hefty81668832007-08-02 12:21:31 -0700502 IB_SA_PATH_REC_TRAFFIC_CLASS |
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 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
516static 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
Moni Shoua732a2172007-10-09 19:43:36 -0700522 neigh = ipoib_neigh_alloc(skb->dst->neighbour, skb->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 if (!neigh) {
Roland Dreierde903512007-09-28 15:33:51 -0700524 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 dev_kfree_skb_any(skb);
526 return;
527 }
528
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 /*
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
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300535 path = __path_find(dev, skb->dst->neighbour->ha + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 if (!path) {
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300537 path = path_rec_create(dev, skb->dst->neighbour->ha + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 if (!path)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300539 goto err_path;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540
541 __path_add(dev, path);
542 }
543
544 list_add_tail(&neigh->list, &path->neigh_list);
545
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -0800546 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 kref_get(&path->ah->ref);
548 neigh->ah = path->ah;
Michael S. Tsirkin8a7f7522006-07-19 17:44:37 +0300549 memcpy(&neigh->dgid.raw, &path->pathrec.dgid.raw,
550 sizeof(union ib_gid));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200552 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));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 } else {
572 neigh->ah = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
574 if (!path->query && path_rec_start(dev, path))
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300575 goto err_list;
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +0200576
577 __skb_queue_tail(&neigh->queue, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 }
579
580 spin_unlock(&priv->lock);
581 return;
582
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300583err_list:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 list_del(&neigh->list);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300586err_path:
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +0200587 ipoib_neigh_free(dev, neigh);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200588err_drop:
Roland Dreierde903512007-09-28 15:33:51 -0700589 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 dev_kfree_skb_any(skb);
591
592 spin_unlock(&priv->lock);
593}
594
Roland Dreierd70ed602005-09-28 19:56:57 -0700595static void ipoib_path_lookup(struct sk_buff *skb, struct net_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700596{
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;
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300608 ipoib_mcast_send(dev, skb->dst->neighbour->ha + 4, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609}
610
611static 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
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300623 path = __path_find(dev, phdr->hwaddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 if (!path) {
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300625 path = path_rec_create(dev, phdr->hwaddr + 4);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626 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 {
Roland Dreierde903512007-09-28 15:33:51 -0700638 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639 dev_kfree_skb_any(skb);
640 }
641
642 spin_unlock(&priv->lock);
643 return;
644 }
645
Michael S. Tsirkin47f7a072006-01-17 09:22:05 -0800646 if (path->ah) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 ipoib_dbg(priv, "Send unicast ARP to %04x\n",
648 be16_to_cpu(path->pathrec.dlid));
649
Michael S. Tsirkin073ae842006-11-16 10:59:12 +0200650 ipoib_send(dev, skb, path->ah, IPOIB_QPN(phdr->hwaddr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 } 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 {
Roland Dreierde903512007-09-28 15:33:51 -0700657 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 dev_kfree_skb_any(skb);
659 }
660
661 spin_unlock(&priv->lock);
662}
663
664static 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
Eli Cohena8bfca02006-09-22 15:22:58 -0700670 if (unlikely(!spin_trylock_irqsave(&priv->tx_lock, flags)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 return NETDEV_TX_LOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672
Eli Cohena8bfca02006-09-22 15:22:58 -0700673 if (likely(skb->dst && skb->dst->neighbour)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 if (unlikely(!*to_ipoib_neigh(skb->dst->neighbour))) {
Roland Dreierd70ed602005-09-28 19:56:57 -0700675 ipoib_path_lookup(skb, dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 goto out;
677 }
678
679 neigh = *to_ipoib_neigh(skb->dst->neighbour);
680
Or Gerlitzbafff972008-01-17 17:03:45 +0200681 if (neigh->ah)
Moni Shoua200d1712007-10-09 19:43:37 -0700682 if (unlikely((memcmp(&neigh->dgid.raw,
Michael S. Tsirkin8a7f7522006-07-19 17:44:37 +0300683 skb->dst->neighbour->ha + 4,
Moni Shoua200d1712007-10-09 19:43:37 -0700684 sizeof(union ib_gid))) ||
685 (neigh->dev != dev))) {
Michael S. Tsirkin8a7f7522006-07-19 17:44:37 +0300686 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);
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +0200696 ipoib_neigh_free(dev, neigh);
Michael S. Tsirkin8a7f7522006-07-19 17:44:37 +0300697 spin_unlock(&priv->lock);
698 ipoib_path_lookup(skb, dev);
699 goto out;
700 }
701
Or Gerlitzbafff972008-01-17 17:03:45 +0200702 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) {
Michael S. Tsirkin073ae842006-11-16 10:59:12 +0200708 ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(skb->dst->neighbour->ha));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 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 {
Roland Dreierde903512007-09-28 15:33:51 -0700717 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 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
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300730 ipoib_mcast_send(dev, phdr->hwaddr + 4, skb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 } else {
Hal Rosenstock0dca0f72005-07-28 13:17:26 -0700732 /* unicast GID -- should be ARP or RARP reply */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Hal Rosenstock0dca0f72005-07-28 13:17:26 -0700734 if ((be16_to_cpup((__be16 *) skb->data) != ETH_P_ARP) &&
735 (be16_to_cpup((__be16 *) skb->data) != ETH_P_RARP)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 ipoib_warn(priv, "Unicast, no %s: type %04x, QPN %06x "
737 IPOIB_GID_FMT "\n",
738 skb->dst ? "neigh" : "dst",
Sean Hefty97f52eb2005-08-13 21:05:57 -0700739 be16_to_cpup((__be16 *) skb->data),
Michael S. Tsirkin073ae842006-11-16 10:59:12 +0200740 IPOIB_QPN(phdr->hwaddr),
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300741 IPOIB_GID_RAW_ARG(phdr->hwaddr + 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 dev_kfree_skb_any(skb);
Roland Dreierde903512007-09-28 15:33:51 -0700743 ++dev->stats.tx_dropped;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 goto out;
745 }
746
747 unicast_arp_send(skb, dev, phdr);
748 }
749 }
750
751out:
752 spin_unlock_irqrestore(&priv->tx_lock, flags);
753
754 return NETDEV_TX_OK;
755}
756
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757static void ipoib_timeout(struct net_device *dev)
758{
759 struct ipoib_dev_priv *priv = netdev_priv(dev);
760
Roland Dreier4b2d3192005-10-18 12:20:06 -0700761 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 /* XXX reset QP, etc. */
767}
768
769static int ipoib_hard_header(struct sk_buff *skb,
770 struct net_device *dev,
771 unsigned short type,
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700772 const void *daddr, const void *saddr, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773{
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 */
Roland Dreieref12d452006-03-29 09:36:46 -0800786 if ((!skb->dst || !skb->dst->neighbour) && daddr) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 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
795static void ipoib_set_mcast_list(struct net_device *dev)
796{
797 struct ipoib_dev_priv *priv = netdev_priv(dev);
798
Leonid Arsh7a343d42006-03-23 19:52:51 +0200799 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
800 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
801 return;
802 }
803
Michael S. Tsirkin1ad62a12005-08-24 14:41:51 -0700804 queue_work(ipoib_workqueue, &priv->restart_task);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
806
Alexey Kuznetsovecbb4162007-03-24 12:52:16 -0700807static void ipoib_neigh_cleanup(struct neighbour *n)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808{
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
Moni Shoua732a2172007-10-09 19:43:36 -0700814 neigh = *to_ipoib_neigh(n);
Or Gerlitz7bc531d2008-01-29 12:57:56 +0200815 if (neigh)
Moni Shoua732a2172007-10-09 19:43:36 -0700816 priv = netdev_priv(neigh->dev);
Or Gerlitz7bc531d2008-01-29 12:57:56 +0200817 else
Moni Shoua732a2172007-10-09 19:43:36 -0700818 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 ipoib_dbg(priv,
Alexey Kuznetsovecbb4162007-03-24 12:52:16 -0700820 "neigh_cleanup for %06x " IPOIB_GID_FMT "\n",
Michael S. Tsirkin073ae842006-11-16 10:59:12 +0200821 IPOIB_QPN(n->ha),
Jack Morgenstein37c22a72006-05-29 19:14:05 +0300822 IPOIB_GID_RAW_ARG(n->ha + 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823
824 spin_lock_irqsave(&priv->lock, flags);
825
Moni Shoua732a2172007-10-09 19:43:36 -0700826 if (neigh->ah)
827 ah = neigh->ah;
828 list_del(&neigh->list);
829 ipoib_neigh_free(n->dev, neigh);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700830
831 spin_unlock_irqrestore(&priv->lock, flags);
832
833 if (ah)
834 ipoib_put_ah(ah);
835}
836
Moni Shoua732a2172007-10-09 19:43:36 -0700837struct ipoib_neigh *ipoib_neigh_alloc(struct neighbour *neighbour,
838 struct net_device *dev)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300839{
840 struct ipoib_neigh *neigh;
841
842 neigh = kmalloc(sizeof *neigh, GFP_ATOMIC);
843 if (!neigh)
844 return NULL;
845
846 neigh->neighbour = neighbour;
Moni Shoua732a2172007-10-09 19:43:36 -0700847 neigh->dev = dev;
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300848 *to_ipoib_neigh(neighbour) = neigh;
Roland Dreier82b39912006-12-12 14:48:18 -0800849 skb_queue_head_init(&neigh->queue);
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200850 ipoib_cm_set(neigh, NULL);
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300851
852 return neigh;
853}
854
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +0200855void ipoib_neigh_free(struct net_device *dev, struct ipoib_neigh *neigh)
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300856{
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +0200857 struct sk_buff *skb;
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300858 *to_ipoib_neigh(neigh->neighbour) = NULL;
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +0200859 while ((skb = __skb_dequeue(&neigh->queue))) {
Roland Dreierde903512007-09-28 15:33:51 -0700860 ++dev->stats.tx_dropped;
Michael S. Tsirkin2745b5b2006-11-16 14:16:47 +0200861 dev_kfree_skb_any(skb);
862 }
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +0200863 if (ipoib_cm_get(neigh))
864 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
Michael S. Tsirkind2e06552006-04-04 19:59:40 +0300865 kfree(neigh);
866}
867
Linus Torvalds1da177e2005-04-16 15:20:36 -0700868static int ipoib_neigh_setup_dev(struct net_device *dev, struct neigh_parms *parms)
869{
Alexey Kuznetsovecbb4162007-03-24 12:52:16 -0700870 parms->neigh_cleanup = ipoib_neigh_cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871
872 return 0;
873}
874
875int 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 */
Shirley Ma0f485252006-04-10 09:43:58 -0700880 priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700881 GFP_KERNEL);
882 if (!priv->rx_ring) {
883 printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n",
Shirley Ma0f485252006-04-10 09:43:58 -0700884 ca->name, ipoib_recvq_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700885 goto out;
886 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887
Roland Dreier10313cb2008-03-12 07:51:03 -0700888 priv->tx_ring = vmalloc(ipoib_sendq_size * sizeof *priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 if (!priv->tx_ring) {
890 printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n",
Shirley Ma0f485252006-04-10 09:43:58 -0700891 ca->name, ipoib_sendq_size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892 goto out_rx_ring_cleanup;
893 }
Roland Dreier10313cb2008-03-12 07:51:03 -0700894 memset(priv->tx_ring, 0, ipoib_sendq_size * sizeof *priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Michael S. Tsirkin1b524962007-08-16 15:36:16 +0300896 /* priv->tx_head, tx_tail & tx_outstanding are already 0 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897
898 if (ipoib_ib_dev_init(dev, ca, port))
899 goto out_tx_ring_cleanup;
900
901 return 0;
902
903out_tx_ring_cleanup:
Roland Dreier10313cb2008-03-12 07:51:03 -0700904 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700905
906out_rx_ring_cleanup:
907 kfree(priv->rx_ring);
908
909out:
910 return -ENOMEM;
911}
912
913void ipoib_dev_cleanup(struct net_device *dev)
914{
915 struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv;
916
Roland Dreier1732b0e2005-11-07 10:33:11 -0800917 ipoib_delete_debug_files(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700918
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
Hal Rosenstock92a6b342005-08-13 20:50:27 -0700928 kfree(priv->rx_ring);
Roland Dreier10313cb2008-03-12 07:51:03 -0700929 vfree(priv->tx_ring);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930
Hal Rosenstock92a6b342005-08-13 20:50:27 -0700931 priv->rx_ring = NULL;
932 priv->tx_ring = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933}
934
Stephen Hemminger3b04ddd2007-10-09 01:40:57 -0700935static const struct header_ops ipoib_header_ops = {
936 .create = ipoib_hard_header,
937};
938
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939static void ipoib_setup(struct net_device *dev)
940{
941 struct ipoib_dev_priv *priv = netdev_priv(dev);
942
Roland Dreier2337f802007-10-23 19:57:54 -0700943 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;
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700951
Eli Cohen82c24c12008-04-16 21:09:32 -0700952 ipoib_set_ethtool_ops(dev);
953
Stephen Hemmingerbea33482007-10-03 16:41:36 -0700954 netif_napi_add(dev, &priv->napi, ipoib_poll, 100);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955
Roland Dreier2337f802007-10-23 19:57:54 -0700956 dev->watchdog_timeo = HZ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Roland Dreier2337f802007-10-23 19:57:54 -0700958 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959
960 /*
961 * We add in INFINIBAND_ALEN to allow for the destination
962 * address "pseudoheader" for skbs without neighbour struct.
963 */
Roland Dreier2337f802007-10-23 19:57:54 -0700964 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;
Eli Coheneb14032f2008-01-30 18:30:46 +0200968 dev->features = (NETIF_F_VLAN_CHALLENGED |
969 NETIF_F_LLTX |
970 NETIF_F_HIGHDMA);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700971
Linus Torvalds1da177e2005-04-16 15:20:36 -0700972 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
973
974 netif_carrier_off(dev);
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 priv->dev = dev;
977
978 spin_lock_init(&priv->lock);
979 spin_lock_init(&priv->tx_lock);
980
Ingo Molnar95ed6442006-01-13 14:51:39 -0800981 mutex_init(&priv->mcast_mutex);
982 mutex_init(&priv->vlan_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983
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
Yosef Etigin26bbf132007-05-19 08:51:54 -0700989 INIT_DELAYED_WORK(&priv->pkey_poll_task, ipoib_pkey_poll);
990 INIT_WORK(&priv->pkey_event_task, ipoib_pkey_event);
David Howellsc4028952006-11-22 14:57:56 +0000991 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995}
996
997struct 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
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001009static ssize_t show_pkey(struct device *dev,
1010 struct device_attribute *attr, char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001012 struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013
1014 return sprintf(buf, "0x%04x\n", priv->pkey);
1015}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001016static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001018static 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
1026static 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}
1042static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast);
1043
1044int ipoib_add_umcast_attr(struct net_device *dev)
1045{
1046 return device_create_file(&dev->dev, &dev_attr_umcast);
1047}
1048
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001049static ssize_t create_child(struct device *dev,
1050 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 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
Roland Dreier4ce05932005-08-19 12:03:17 -07001062 /*
1063 * Set the full membership bit, so that we join the right
1064 * broadcast group, etc.
1065 */
1066 pkey |= 0x8000;
1067
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001068 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069
1070 return ret ? ret : count;
1071}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001072static DEVICE_ATTR(create_child, S_IWUGO, NULL, create_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001074static ssize_t delete_child(struct device *dev,
1075 struct device_attribute *attr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 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
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001087 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088
1089 return ret ? ret : count;
1090
1091}
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001092static DEVICE_ATTR(delete_child, S_IWUGO, NULL, delete_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093
1094int ipoib_add_pkey_attr(struct net_device *dev)
1095{
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001096 return device_create_file(&dev->dev, &dev_attr_pkey);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097}
1098
1099static struct net_device *ipoib_add_port(const char *format,
1100 struct ib_device *hca, u8 port)
1101{
1102 struct ipoib_dev_priv *priv;
Eli Cohen60461362008-04-16 21:01:10 -07001103 struct ib_device_attr *device_attr;
Shirley Mabc7b3a32008-04-23 11:55:45 -07001104 struct ib_port_attr attr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 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
Shirley Mabc7b3a32008-04-23 11:55:45 -07001113 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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001125 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);
Eli Cohenca6de172007-08-21 18:46:10 +03001129 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 }
1131
Eli Cohen60461362008-04-16 21:01:10 -07001132 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 }
Eli Cohen40ca1982008-04-16 21:09:27 -07001146 priv->hca_caps = device_attr->device_cap_flags;
Eli Cohen60461362008-04-16 21:01:10 -07001147
Eli Cohen40ca1982008-04-16 21:09:27 -07001148 kfree(device_attr);
1149
1150 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
Eli Cohen60461362008-04-16 21:01:10 -07001151 set_bit(IPOIB_FLAG_CSUM, &priv->flags);
1152 priv->dev->features |= NETIF_F_SG | NETIF_F_IP_CSUM;
1153 }
1154
Roland Dreier4ce05932005-08-19 12:03:17 -07001155 /*
1156 * Set the full membership bit, so that we join the right
1157 * broadcast group, etc.
1158 */
1159 priv->pkey |= 0x8000;
1160
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161 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);
Eli Cohenca6de172007-08-21 18:46:10 +03001168 goto device_init_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 } else
1170 memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid));
1171
Linus Torvalds1da177e2005-04-16 15:20:36 -07001172 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
Eli Cohen40ca1982008-04-16 21:09:27 -07001189 if (priv->dev->features & NETIF_F_SG && priv->hca_caps & IB_DEVICE_UD_TSO)
1190 priv->dev->features |= NETIF_F_TSO;
1191
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 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
Roland Dreier1732b0e2005-11-07 10:33:11 -08001199 ipoib_create_debug_files(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200
Michael S. Tsirkin839fcab2007-02-05 22:12:23 +02001201 if (ipoib_cm_add_mode_attr(priv->dev))
1202 goto sysfs_failed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 if (ipoib_add_pkey_attr(priv->dev))
1204 goto sysfs_failed;
Or Gerlitz335a64a5a2007-10-08 10:13:00 +02001205 if (ipoib_add_umcast_attr(priv->dev))
1206 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001207 if (device_create_file(&priv->dev->dev, &dev_attr_create_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001208 goto sysfs_failed;
Greg Kroah-Hartman43cb76d2002-04-09 12:14:34 -07001209 if (device_create_file(&priv->dev->dev, &dev_attr_delete_child))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001210 goto sysfs_failed;
1211
1212 return priv->dev;
1213
1214sysfs_failed:
Roland Dreier1732b0e2005-11-07 10:33:11 -08001215 ipoib_delete_debug_files(priv->dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216 unregister_netdev(priv->dev);
1217
1218register_failed:
1219 ib_unregister_event_handler(&priv->event_handler);
Michael S. Tsirkin51574e02005-09-12 09:52:28 -07001220 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222event_failed:
1223 ipoib_dev_cleanup(priv->dev);
1224
1225device_init_failed:
1226 free_netdev(priv->dev);
1227
1228alloc_mem_failed:
1229 return ERR_PTR(result);
1230}
1231
1232static 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
Tom Tucker07ebafb2006-08-03 16:02:42 -05001239 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1240 return;
1241
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242 dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL);
1243 if (!dev_list)
1244 return;
1245
1246 INIT_LIST_HEAD(dev_list);
1247
Tom Tucker07ebafb2006-08-03 16:02:42 -05001248 if (device->node_type == RDMA_NODE_IB_SWITCH) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249 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
1267static void ipoib_remove_one(struct ib_device *device)
1268{
1269 struct ipoib_dev_priv *priv, *tmp;
1270 struct list_head *dev_list;
1271
Tom Tucker07ebafb2006-08-03 16:02:42 -05001272 if (rdma_node_get_transport(device->node_type) != RDMA_TRANSPORT_IB)
1273 return;
1274
Linus Torvalds1da177e2005-04-16 15:20:36 -07001275 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);
Michael S. Tsirkin51574e02005-09-12 09:52:28 -07001279 flush_scheduled_work();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001280
1281 unregister_netdev(priv->dev);
1282 ipoib_dev_cleanup(priv->dev);
1283 free_netdev(priv->dev);
1284 }
Michael S. Tsirkin06c56e42005-09-01 09:19:02 -07001285
1286 kfree(dev_list);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001287}
1288
1289static int __init ipoib_init_module(void)
1290{
1291 int ret;
1292
Shirley Ma0f485252006-04-10 09:43:58 -07001293 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);
Eli Cohenf56bcd82008-04-29 13:46:53 -07001299 ipoib_sendq_size = max(ipoib_sendq_size, max(2 * MAX_SEND_CQE,
1300 IPOIB_MIN_QUEUE_SIZE));
Pradeep Satyanarayana68e995a2008-01-25 14:15:24 -08001301#ifdef CONFIG_INFINIBAND_IPOIB_CM
1302 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
1303#endif
Shirley Ma0f485252006-04-10 09:43:58 -07001304
Eli Cohenf89271da2008-07-14 23:48:44 -07001305 /*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -07001311 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
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001329 ib_sa_register_client(&ipoib_sa_client);
1330
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331 ret = ib_register_client(&ipoib_client);
1332 if (ret)
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001333 goto err_sa;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334
1335 return 0;
1336
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001337err_sa:
1338 ib_sa_unregister_client(&ipoib_sa_client);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339 destroy_workqueue(ipoib_workqueue);
1340
Roland Dreier9adec1a2005-04-16 15:26:07 -07001341err_fs:
1342 ipoib_unregister_debugfs();
1343
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 return ret;
1345}
1346
1347static void __exit ipoib_cleanup_module(void)
1348{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 ib_unregister_client(&ipoib_client);
Michael S. Tsirkinc1a0b232006-08-21 16:40:12 -07001350 ib_sa_unregister_client(&ipoib_sa_client);
Roland Dreier9adec1a2005-04-16 15:26:07 -07001351 ipoib_unregister_debugfs();
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 destroy_workqueue(ipoib_workqueue);
1353}
1354
1355module_init(ipoib_init_module);
1356module_exit(ipoib_cleanup_module);