]>
Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /* |
2 | * Copyright (c) 2004 Topspin Communications. All rights reserved. | |
2a1d9b7f RD |
3 | * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. |
4 | * Copyright (c) 2004 Voltaire, Inc. All rights reserved. | |
1da177e4 LT |
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. | |
1da177e4 LT |
33 | */ |
34 | ||
35 | #include "ipoib.h" | |
36 | ||
1da177e4 LT |
37 | #include <linux/module.h> |
38 | ||
39 | #include <linux/init.h> | |
40 | #include <linux/slab.h> | |
0f485251 | 41 | #include <linux/kernel.h> |
10313cbb | 42 | #include <linux/vmalloc.h> |
1da177e4 LT |
43 | |
44 | #include <linux/if_arp.h> /* For ARPHRD_xxx */ | |
45 | ||
46 | #include <linux/ip.h> | |
47 | #include <linux/in.h> | |
48 | ||
b63b70d8 SP |
49 | #include <linux/jhash.h> |
50 | #include <net/arp.h> | |
ddde896e GS |
51 | #include <net/addrconf.h> |
52 | #include <linux/inetdevice.h> | |
53 | #include <rdma/ib_cache.h> | |
9c3c5f8e | 54 | #include <linux/pci.h> |
14c85021 | 55 | |
4b48680b YB |
56 | #define DRV_VERSION "1.0.0" |
57 | ||
58 | const char ipoib_driver_version[] = DRV_VERSION; | |
59 | ||
1da177e4 LT |
60 | MODULE_AUTHOR("Roland Dreier"); |
61 | MODULE_DESCRIPTION("IP-over-InfiniBand net driver"); | |
62 | MODULE_LICENSE("Dual BSD/GPL"); | |
4b48680b | 63 | MODULE_VERSION(DRV_VERSION); |
1da177e4 | 64 | |
0f485251 SM |
65 | int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE; |
66 | int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE; | |
67 | ||
68 | module_param_named(send_queue_size, ipoib_sendq_size, int, 0444); | |
69 | MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue"); | |
70 | module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444); | |
71 | MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue"); | |
72 | ||
1da177e4 LT |
73 | #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG |
74 | int ipoib_debug_level; | |
75 | ||
76 | module_param_named(debug_level, ipoib_debug_level, int, 0644); | |
77 | MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0"); | |
78 | #endif | |
79 | ||
1732b0ef RD |
80 | struct ipoib_path_iter { |
81 | struct net_device *dev; | |
82 | struct ipoib_path path; | |
83 | }; | |
84 | ||
1da177e4 LT |
85 | static const u8 ipv4_bcast_addr[] = { |
86 | 0x00, 0xff, 0xff, 0xff, | |
87 | 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00, | |
88 | 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff | |
89 | }; | |
90 | ||
91 | struct workqueue_struct *ipoib_workqueue; | |
92 | ||
c1a0b23b MT |
93 | struct ib_sa_client ipoib_sa_client; |
94 | ||
1da177e4 | 95 | static void ipoib_add_one(struct ib_device *device); |
7c1eb45a | 96 | static void ipoib_remove_one(struct ib_device *device, void *client_data); |
b63b70d8 | 97 | static void ipoib_neigh_reclaim(struct rcu_head *rp); |
ddde896e GS |
98 | static struct net_device *ipoib_get_net_dev_by_params( |
99 | struct ib_device *dev, u8 port, u16 pkey, | |
100 | const union ib_gid *gid, const struct sockaddr *addr, | |
101 | void *client_data); | |
492a7e67 | 102 | static int ipoib_set_mac(struct net_device *dev, void *addr); |
1da177e4 LT |
103 | |
104 | static struct ib_client ipoib_client = { | |
105 | .name = "ipoib", | |
106 | .add = ipoib_add_one, | |
ddde896e GS |
107 | .remove = ipoib_remove_one, |
108 | .get_net_dev_by_params = ipoib_get_net_dev_by_params, | |
1da177e4 LT |
109 | }; |
110 | ||
111 | int ipoib_open(struct net_device *dev) | |
112 | { | |
113 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
114 | ||
115 | ipoib_dbg(priv, "bringing up interface\n"); | |
116 | ||
437708c4 MS |
117 | netif_carrier_off(dev); |
118 | ||
e028cc55 | 119 | set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags); |
1da177e4 | 120 | |
3b561130 ES |
121 | priv->sm_fullmember_sendonly_support = false; |
122 | ||
efc82eee | 123 | if (ipoib_ib_dev_open(dev)) { |
dd57c930 AE |
124 | if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags)) |
125 | return 0; | |
b8a1b1ce | 126 | goto err_disable; |
dd57c930 | 127 | } |
fe25c561 | 128 | |
b8a1b1ce RD |
129 | if (ipoib_ib_dev_up(dev)) |
130 | goto err_stop; | |
1da177e4 LT |
131 | |
132 | if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) { | |
133 | struct ipoib_dev_priv *cpriv; | |
134 | ||
135 | /* Bring up any child interfaces too */ | |
f47944cc | 136 | down_read(&priv->vlan_rwsem); |
1da177e4 LT |
137 | list_for_each_entry(cpriv, &priv->child_intfs, list) { |
138 | int flags; | |
139 | ||
140 | flags = cpriv->dev->flags; | |
141 | if (flags & IFF_UP) | |
142 | continue; | |
143 | ||
144 | dev_change_flags(cpriv->dev, flags | IFF_UP); | |
145 | } | |
f47944cc | 146 | up_read(&priv->vlan_rwsem); |
1da177e4 LT |
147 | } |
148 | ||
149 | netif_start_queue(dev); | |
150 | ||
151 | return 0; | |
b8a1b1ce RD |
152 | |
153 | err_stop: | |
efc82eee | 154 | ipoib_ib_dev_stop(dev); |
b8a1b1ce RD |
155 | |
156 | err_disable: | |
b8a1b1ce RD |
157 | clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags); |
158 | ||
159 | return -EINVAL; | |
1da177e4 LT |
160 | } |
161 | ||
162 | static int ipoib_stop(struct net_device *dev) | |
163 | { | |
164 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
165 | ||
166 | ipoib_dbg(priv, "stopping interface\n"); | |
167 | ||
168 | clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags); | |
169 | ||
170 | netif_stop_queue(dev); | |
171 | ||
efc82eee DL |
172 | ipoib_ib_dev_down(dev); |
173 | ipoib_ib_dev_stop(dev); | |
1da177e4 LT |
174 | |
175 | if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) { | |
176 | struct ipoib_dev_priv *cpriv; | |
177 | ||
178 | /* Bring down any child interfaces too */ | |
f47944cc | 179 | down_read(&priv->vlan_rwsem); |
1da177e4 LT |
180 | list_for_each_entry(cpriv, &priv->child_intfs, list) { |
181 | int flags; | |
182 | ||
183 | flags = cpriv->dev->flags; | |
184 | if (!(flags & IFF_UP)) | |
185 | continue; | |
186 | ||
187 | dev_change_flags(cpriv->dev, flags & ~IFF_UP); | |
188 | } | |
f47944cc | 189 | up_read(&priv->vlan_rwsem); |
1da177e4 LT |
190 | } |
191 | ||
192 | return 0; | |
193 | } | |
194 | ||
9baa0b03 OG |
195 | static void ipoib_uninit(struct net_device *dev) |
196 | { | |
197 | ipoib_dev_cleanup(dev); | |
198 | } | |
199 | ||
9ca36f7d | 200 | static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features) |
3d96c74d MM |
201 | { |
202 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
203 | ||
204 | if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags)) | |
c4268778 | 205 | features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO); |
3d96c74d MM |
206 | |
207 | return features; | |
208 | } | |
209 | ||
1da177e4 LT |
210 | static int ipoib_change_mtu(struct net_device *dev, int new_mtu) |
211 | { | |
212 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
213 | ||
839fcaba | 214 | /* dev->mtu > 2K ==> connected mode */ |
586a6934 PS |
215 | if (ipoib_cm_admin_enabled(dev)) { |
216 | if (new_mtu > ipoib_cm_max_mtu(dev)) | |
217 | return -EINVAL; | |
218 | ||
839fcaba MT |
219 | if (new_mtu > priv->mcast_mtu) |
220 | ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n", | |
221 | priv->mcast_mtu); | |
586a6934 | 222 | |
839fcaba MT |
223 | dev->mtu = new_mtu; |
224 | return 0; | |
225 | } | |
226 | ||
bc7b3a36 | 227 | if (new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu)) |
1da177e4 LT |
228 | return -EINVAL; |
229 | ||
230 | priv->admin_mtu = new_mtu; | |
231 | ||
232 | dev->mtu = min(priv->mcast_mtu, priv->admin_mtu); | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
ddde896e GS |
237 | /* Called with an RCU read lock taken */ |
238 | static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr, | |
239 | struct net_device *dev) | |
240 | { | |
241 | struct net *net = dev_net(dev); | |
242 | struct in_device *in_dev; | |
243 | struct sockaddr_in *addr_in = (struct sockaddr_in *)addr; | |
244 | struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr; | |
245 | __be32 ret_addr; | |
246 | ||
247 | switch (addr->sa_family) { | |
248 | case AF_INET: | |
249 | in_dev = in_dev_get(dev); | |
250 | if (!in_dev) | |
251 | return false; | |
252 | ||
253 | ret_addr = inet_confirm_addr(net, in_dev, 0, | |
254 | addr_in->sin_addr.s_addr, | |
255 | RT_SCOPE_HOST); | |
256 | in_dev_put(in_dev); | |
257 | if (ret_addr) | |
258 | return true; | |
259 | ||
260 | break; | |
261 | case AF_INET6: | |
262 | if (IS_ENABLED(CONFIG_IPV6) && | |
263 | ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1)) | |
264 | return true; | |
265 | ||
266 | break; | |
267 | } | |
268 | return false; | |
269 | } | |
270 | ||
271 | /** | |
272 | * Find the master net_device on top of the given net_device. | |
273 | * @dev: base IPoIB net_device | |
274 | * | |
275 | * Returns the master net_device with a reference held, or the same net_device | |
276 | * if no master exists. | |
277 | */ | |
278 | static struct net_device *ipoib_get_master_net_dev(struct net_device *dev) | |
279 | { | |
280 | struct net_device *master; | |
281 | ||
282 | rcu_read_lock(); | |
283 | master = netdev_master_upper_dev_get_rcu(dev); | |
284 | if (master) | |
285 | dev_hold(master); | |
286 | rcu_read_unlock(); | |
287 | ||
288 | if (master) | |
289 | return master; | |
290 | ||
291 | dev_hold(dev); | |
292 | return dev; | |
293 | } | |
294 | ||
e0e79c8e DA |
295 | struct ipoib_walk_data { |
296 | const struct sockaddr *addr; | |
297 | struct net_device *result; | |
298 | }; | |
299 | ||
300 | static int ipoib_upper_walk(struct net_device *upper, void *_data) | |
301 | { | |
302 | struct ipoib_walk_data *data = _data; | |
303 | int ret = 0; | |
304 | ||
305 | if (ipoib_is_dev_match_addr_rcu(data->addr, upper)) { | |
306 | dev_hold(upper); | |
307 | data->result = upper; | |
308 | ret = 1; | |
309 | } | |
310 | ||
311 | return ret; | |
312 | } | |
313 | ||
ddde896e GS |
314 | /** |
315 | * Find a net_device matching the given address, which is an upper device of | |
316 | * the given net_device. | |
317 | * @addr: IP address to look for. | |
318 | * @dev: base IPoIB net_device | |
319 | * | |
320 | * If found, returns the net_device with a reference held. Otherwise return | |
321 | * NULL. | |
322 | */ | |
323 | static struct net_device *ipoib_get_net_dev_match_addr( | |
324 | const struct sockaddr *addr, struct net_device *dev) | |
325 | { | |
e0e79c8e DA |
326 | struct ipoib_walk_data data = { |
327 | .addr = addr, | |
328 | }; | |
ddde896e GS |
329 | |
330 | rcu_read_lock(); | |
331 | if (ipoib_is_dev_match_addr_rcu(addr, dev)) { | |
332 | dev_hold(dev); | |
e0e79c8e | 333 | data.result = dev; |
ddde896e GS |
334 | goto out; |
335 | } | |
336 | ||
e0e79c8e | 337 | netdev_walk_all_upper_dev_rcu(dev, ipoib_upper_walk, &data); |
ddde896e GS |
338 | out: |
339 | rcu_read_unlock(); | |
e0e79c8e | 340 | return data.result; |
ddde896e GS |
341 | } |
342 | ||
343 | /* returns the number of IPoIB netdevs on top a given ipoib device matching a | |
344 | * pkey_index and address, if one exists. | |
345 | * | |
346 | * @found_net_dev: contains a matching net_device if the return value >= 1, | |
347 | * with a reference held. */ | |
348 | static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv, | |
349 | const union ib_gid *gid, | |
350 | u16 pkey_index, | |
351 | const struct sockaddr *addr, | |
352 | int nesting, | |
353 | struct net_device **found_net_dev) | |
354 | { | |
355 | struct ipoib_dev_priv *child_priv; | |
356 | struct net_device *net_dev = NULL; | |
357 | int matches = 0; | |
358 | ||
359 | if (priv->pkey_index == pkey_index && | |
360 | (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) { | |
361 | if (!addr) { | |
362 | net_dev = ipoib_get_master_net_dev(priv->dev); | |
363 | } else { | |
364 | /* Verify the net_device matches the IP address, as | |
365 | * IPoIB child devices currently share a GID. */ | |
366 | net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev); | |
367 | } | |
368 | if (net_dev) { | |
369 | if (!*found_net_dev) | |
370 | *found_net_dev = net_dev; | |
371 | else | |
372 | dev_put(net_dev); | |
373 | ++matches; | |
374 | } | |
375 | } | |
376 | ||
377 | /* Check child interfaces */ | |
378 | down_read_nested(&priv->vlan_rwsem, nesting); | |
379 | list_for_each_entry(child_priv, &priv->child_intfs, list) { | |
380 | matches += ipoib_match_gid_pkey_addr(child_priv, gid, | |
381 | pkey_index, addr, | |
382 | nesting + 1, | |
383 | found_net_dev); | |
384 | if (matches > 1) | |
385 | break; | |
386 | } | |
387 | up_read(&priv->vlan_rwsem); | |
388 | ||
389 | return matches; | |
390 | } | |
391 | ||
392 | /* Returns the number of matching net_devs found (between 0 and 2). Also | |
393 | * return the matching net_device in the @net_dev parameter, holding a | |
394 | * reference to the net_device, if the number of matches >= 1 */ | |
395 | static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u8 port, | |
396 | u16 pkey_index, | |
397 | const union ib_gid *gid, | |
398 | const struct sockaddr *addr, | |
399 | struct net_device **net_dev) | |
400 | { | |
401 | struct ipoib_dev_priv *priv; | |
402 | int matches = 0; | |
403 | ||
404 | *net_dev = NULL; | |
405 | ||
406 | list_for_each_entry(priv, dev_list, list) { | |
407 | if (priv->port != port) | |
408 | continue; | |
409 | ||
410 | matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index, | |
411 | addr, 0, net_dev); | |
412 | if (matches > 1) | |
413 | break; | |
414 | } | |
415 | ||
416 | return matches; | |
417 | } | |
418 | ||
419 | static struct net_device *ipoib_get_net_dev_by_params( | |
420 | struct ib_device *dev, u8 port, u16 pkey, | |
421 | const union ib_gid *gid, const struct sockaddr *addr, | |
422 | void *client_data) | |
423 | { | |
424 | struct net_device *net_dev; | |
425 | struct list_head *dev_list = client_data; | |
426 | u16 pkey_index; | |
427 | int matches; | |
428 | int ret; | |
429 | ||
430 | if (!rdma_protocol_ib(dev, port)) | |
431 | return NULL; | |
432 | ||
433 | ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index); | |
434 | if (ret) | |
435 | return NULL; | |
436 | ||
437 | if (!dev_list) | |
438 | return NULL; | |
439 | ||
440 | /* See if we can find a unique device matching the L2 parameters */ | |
441 | matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index, | |
442 | gid, NULL, &net_dev); | |
443 | ||
444 | switch (matches) { | |
445 | case 0: | |
446 | return NULL; | |
447 | case 1: | |
448 | return net_dev; | |
449 | } | |
450 | ||
451 | dev_put(net_dev); | |
452 | ||
453 | /* Couldn't find a unique device with L2 parameters only. Use L3 | |
454 | * address to uniquely match the net device */ | |
455 | matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index, | |
456 | gid, addr, &net_dev); | |
457 | switch (matches) { | |
458 | case 0: | |
459 | return NULL; | |
460 | default: | |
461 | dev_warn_ratelimited(&dev->dev, | |
462 | "duplicate IP address detected\n"); | |
463 | /* Fall through */ | |
464 | case 1: | |
465 | return net_dev; | |
466 | } | |
467 | } | |
468 | ||
71d9c5f9 RD |
469 | int ipoib_set_mode(struct net_device *dev, const char *buf) |
470 | { | |
471 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
472 | ||
473 | /* flush paths if we switch modes so that connections are restarted */ | |
474 | if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) { | |
475 | set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags); | |
476 | ipoib_warn(priv, "enabling connected mode " | |
477 | "will cause multicast packet drops\n"); | |
478 | netdev_update_features(dev); | |
edcd2a74 | 479 | dev_set_mtu(dev, ipoib_cm_max_mtu(dev)); |
71d9c5f9 | 480 | rtnl_unlock(); |
e622f2f4 | 481 | priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM; |
71d9c5f9 RD |
482 | |
483 | ipoib_flush_paths(dev); | |
484 | rtnl_lock(); | |
485 | return 0; | |
486 | } | |
487 | ||
488 | if (!strcmp(buf, "datagram\n")) { | |
489 | clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags); | |
490 | netdev_update_features(dev); | |
491 | dev_set_mtu(dev, min(priv->mcast_mtu, dev->mtu)); | |
492 | rtnl_unlock(); | |
493 | ipoib_flush_paths(dev); | |
494 | rtnl_lock(); | |
495 | return 0; | |
496 | } | |
497 | ||
498 | return -EINVAL; | |
499 | } | |
500 | ||
546481c2 | 501 | struct ipoib_path *__path_find(struct net_device *dev, void *gid) |
1da177e4 LT |
502 | { |
503 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
504 | struct rb_node *n = priv->path_tree.rb_node; | |
505 | struct ipoib_path *path; | |
506 | int ret; | |
507 | ||
508 | while (n) { | |
509 | path = rb_entry(n, struct ipoib_path, rb_node); | |
510 | ||
37c22a77 | 511 | ret = memcmp(gid, path->pathrec.dgid.raw, |
1da177e4 LT |
512 | sizeof (union ib_gid)); |
513 | ||
514 | if (ret < 0) | |
515 | n = n->rb_left; | |
516 | else if (ret > 0) | |
517 | n = n->rb_right; | |
518 | else | |
519 | return path; | |
520 | } | |
521 | ||
522 | return NULL; | |
523 | } | |
524 | ||
525 | static int __path_add(struct net_device *dev, struct ipoib_path *path) | |
526 | { | |
527 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
528 | struct rb_node **n = &priv->path_tree.rb_node; | |
529 | struct rb_node *pn = NULL; | |
530 | struct ipoib_path *tpath; | |
531 | int ret; | |
532 | ||
533 | while (*n) { | |
534 | pn = *n; | |
535 | tpath = rb_entry(pn, struct ipoib_path, rb_node); | |
536 | ||
537 | ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw, | |
538 | sizeof (union ib_gid)); | |
539 | if (ret < 0) | |
540 | n = &pn->rb_left; | |
541 | else if (ret > 0) | |
542 | n = &pn->rb_right; | |
543 | else | |
544 | return -EEXIST; | |
545 | } | |
546 | ||
547 | rb_link_node(&path->rb_node, pn, n); | |
548 | rb_insert_color(&path->rb_node, &priv->path_tree); | |
549 | ||
550 | list_add_tail(&path->list, &priv->path_list); | |
551 | ||
552 | return 0; | |
553 | } | |
554 | ||
555 | static void path_free(struct net_device *dev, struct ipoib_path *path) | |
556 | { | |
1da177e4 | 557 | struct sk_buff *skb; |
1da177e4 LT |
558 | |
559 | while ((skb = __skb_dequeue(&path->queue))) | |
560 | dev_kfree_skb_irq(skb); | |
561 | ||
b63b70d8 | 562 | ipoib_dbg(netdev_priv(dev), "path_free\n"); |
1da177e4 | 563 | |
b63b70d8 SP |
564 | /* remove all neigh connected to this path */ |
565 | ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw); | |
1da177e4 LT |
566 | |
567 | if (path->ah) | |
568 | ipoib_put_ah(path->ah); | |
569 | ||
570 | kfree(path); | |
571 | } | |
572 | ||
1732b0ef RD |
573 | #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG |
574 | ||
575 | struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev) | |
576 | { | |
577 | struct ipoib_path_iter *iter; | |
578 | ||
579 | iter = kmalloc(sizeof *iter, GFP_KERNEL); | |
580 | if (!iter) | |
581 | return NULL; | |
582 | ||
583 | iter->dev = dev; | |
584 | memset(iter->path.pathrec.dgid.raw, 0, 16); | |
585 | ||
586 | if (ipoib_path_iter_next(iter)) { | |
587 | kfree(iter); | |
588 | return NULL; | |
589 | } | |
590 | ||
591 | return iter; | |
592 | } | |
593 | ||
594 | int ipoib_path_iter_next(struct ipoib_path_iter *iter) | |
595 | { | |
596 | struct ipoib_dev_priv *priv = netdev_priv(iter->dev); | |
597 | struct rb_node *n; | |
598 | struct ipoib_path *path; | |
599 | int ret = 1; | |
600 | ||
601 | spin_lock_irq(&priv->lock); | |
602 | ||
603 | n = rb_first(&priv->path_tree); | |
604 | ||
605 | while (n) { | |
606 | path = rb_entry(n, struct ipoib_path, rb_node); | |
607 | ||
608 | if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw, | |
609 | sizeof (union ib_gid)) < 0) { | |
610 | iter->path = *path; | |
611 | ret = 0; | |
612 | break; | |
613 | } | |
614 | ||
615 | n = rb_next(n); | |
616 | } | |
617 | ||
618 | spin_unlock_irq(&priv->lock); | |
619 | ||
620 | return ret; | |
621 | } | |
622 | ||
623 | void ipoib_path_iter_read(struct ipoib_path_iter *iter, | |
624 | struct ipoib_path *path) | |
625 | { | |
626 | *path = iter->path; | |
627 | } | |
628 | ||
629 | #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */ | |
630 | ||
ee1e2c82 MS |
631 | void ipoib_mark_paths_invalid(struct net_device *dev) |
632 | { | |
633 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
634 | struct ipoib_path *path, *tp; | |
635 | ||
636 | spin_lock_irq(&priv->lock); | |
637 | ||
638 | list_for_each_entry_safe(path, tp, &priv->path_list, list) { | |
5b095d98 | 639 | ipoib_dbg(priv, "mark path LID 0x%04x GID %pI6 invalid\n", |
ee1e2c82 | 640 | be16_to_cpu(path->pathrec.dlid), |
fcace2fe | 641 | path->pathrec.dgid.raw); |
ee1e2c82 MS |
642 | path->valid = 0; |
643 | } | |
644 | ||
645 | spin_unlock_irq(&priv->lock); | |
646 | } | |
647 | ||
3b561130 ES |
648 | struct classport_info_context { |
649 | struct ipoib_dev_priv *priv; | |
650 | struct completion done; | |
651 | struct ib_sa_query *sa_query; | |
652 | }; | |
653 | ||
654 | static void classport_info_query_cb(int status, struct ib_class_port_info *rec, | |
655 | void *context) | |
656 | { | |
657 | struct classport_info_context *cb_ctx = context; | |
658 | struct ipoib_dev_priv *priv; | |
659 | ||
660 | WARN_ON(!context); | |
661 | ||
662 | priv = cb_ctx->priv; | |
663 | ||
664 | if (status || !rec) { | |
665 | pr_debug("device: %s failed query classport_info status: %d\n", | |
666 | priv->dev->name, status); | |
667 | /* keeps the default, will try next mcast_restart */ | |
668 | priv->sm_fullmember_sendonly_support = false; | |
669 | goto out; | |
670 | } | |
671 | ||
672 | if (ib_get_cpi_capmask2(rec) & | |
673 | IB_SA_CAP_MASK2_SENDONLY_FULL_MEM_SUPPORT) { | |
674 | pr_debug("device: %s enabled fullmember-sendonly for sendonly MCG\n", | |
675 | priv->dev->name); | |
676 | priv->sm_fullmember_sendonly_support = true; | |
677 | } else { | |
678 | pr_debug("device: %s disabled fullmember-sendonly for sendonly MCG\n", | |
679 | priv->dev->name); | |
680 | priv->sm_fullmember_sendonly_support = false; | |
681 | } | |
682 | ||
683 | out: | |
684 | complete(&cb_ctx->done); | |
685 | } | |
686 | ||
687 | int ipoib_check_sm_sendonly_fullmember_support(struct ipoib_dev_priv *priv) | |
688 | { | |
689 | struct classport_info_context *callback_context; | |
690 | int ret; | |
691 | ||
692 | callback_context = kmalloc(sizeof(*callback_context), GFP_KERNEL); | |
693 | if (!callback_context) | |
694 | return -ENOMEM; | |
695 | ||
696 | callback_context->priv = priv; | |
697 | init_completion(&callback_context->done); | |
698 | ||
699 | ret = ib_sa_classport_info_rec_query(&ipoib_sa_client, | |
700 | priv->ca, priv->port, 3000, | |
701 | GFP_KERNEL, | |
702 | classport_info_query_cb, | |
703 | callback_context, | |
704 | &callback_context->sa_query); | |
705 | if (ret < 0) { | |
706 | pr_info("%s failed to send ib_sa_classport_info query, ret: %d\n", | |
707 | priv->dev->name, ret); | |
708 | kfree(callback_context); | |
709 | return ret; | |
710 | } | |
711 | ||
712 | /* waiting for the callback to finish before returnning */ | |
713 | wait_for_completion(&callback_context->done); | |
714 | kfree(callback_context); | |
715 | ||
716 | return ret; | |
717 | } | |
718 | ||
1da177e4 LT |
719 | void ipoib_flush_paths(struct net_device *dev) |
720 | { | |
721 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
722 | struct ipoib_path *path, *tp; | |
723 | LIST_HEAD(remove_list); | |
943c246e | 724 | unsigned long flags; |
1da177e4 | 725 | |
943c246e RD |
726 | netif_tx_lock_bh(dev); |
727 | spin_lock_irqsave(&priv->lock, flags); | |
1da177e4 | 728 | |
157de229 | 729 | list_splice_init(&priv->path_list, &remove_list); |
1da177e4 LT |
730 | |
731 | list_for_each_entry(path, &remove_list, list) | |
732 | rb_erase(&path->rb_node, &priv->path_tree); | |
733 | ||
1da177e4 LT |
734 | list_for_each_entry_safe(path, tp, &remove_list, list) { |
735 | if (path->query) | |
736 | ib_sa_cancel_query(path->query_id, path->query); | |
943c246e RD |
737 | spin_unlock_irqrestore(&priv->lock, flags); |
738 | netif_tx_unlock_bh(dev); | |
1da177e4 LT |
739 | wait_for_completion(&path->done); |
740 | path_free(dev, path); | |
943c246e RD |
741 | netif_tx_lock_bh(dev); |
742 | spin_lock_irqsave(&priv->lock, flags); | |
1da177e4 | 743 | } |
943c246e RD |
744 | |
745 | spin_unlock_irqrestore(&priv->lock, flags); | |
746 | netif_tx_unlock_bh(dev); | |
1da177e4 LT |
747 | } |
748 | ||
749 | static void path_rec_completion(int status, | |
750 | struct ib_sa_path_rec *pathrec, | |
751 | void *path_ptr) | |
752 | { | |
753 | struct ipoib_path *path = path_ptr; | |
754 | struct net_device *dev = path->dev; | |
755 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
756 | struct ipoib_ah *ah = NULL; | |
c9da4bad | 757 | struct ipoib_ah *old_ah = NULL; |
d04d01b1 | 758 | struct ipoib_neigh *neigh, *tn; |
1da177e4 LT |
759 | struct sk_buff_head skqueue; |
760 | struct sk_buff *skb; | |
761 | unsigned long flags; | |
762 | ||
843613b0 | 763 | if (!status) |
5b095d98 | 764 | ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n", |
fcace2fe | 765 | be16_to_cpu(pathrec->dlid), pathrec->dgid.raw); |
1da177e4 | 766 | else |
5b095d98 | 767 | ipoib_dbg(priv, "PathRec status %d for GID %pI6\n", |
fcace2fe | 768 | status, path->pathrec.dgid.raw); |
1da177e4 LT |
769 | |
770 | skb_queue_head_init(&skqueue); | |
771 | ||
772 | if (!status) { | |
46f1b3d7 SH |
773 | struct ib_ah_attr av; |
774 | ||
775 | if (!ib_init_ah_from_path(priv->ca, priv->port, pathrec, &av)) | |
776 | ah = ipoib_create_ah(dev, priv->pd, &av); | |
1da177e4 LT |
777 | } |
778 | ||
779 | spin_lock_irqsave(&priv->lock, flags); | |
780 | ||
3874397c | 781 | if (!IS_ERR_OR_NULL(ah)) { |
1da177e4 LT |
782 | path->pathrec = *pathrec; |
783 | ||
c9da4bad RD |
784 | old_ah = path->ah; |
785 | path->ah = ah; | |
786 | ||
1da177e4 LT |
787 | ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n", |
788 | ah, be16_to_cpu(pathrec->dlid), pathrec->sl); | |
789 | ||
790 | while ((skb = __skb_dequeue(&path->queue))) | |
791 | __skb_queue_tail(&skqueue, skb); | |
792 | ||
d04d01b1 | 793 | list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) { |
ee1e2c82 MS |
794 | if (neigh->ah) { |
795 | WARN_ON(neigh->ah != old_ah); | |
796 | /* | |
797 | * Dropping the ah reference inside | |
798 | * priv->lock is safe here, because we | |
799 | * will hold one more reference from | |
800 | * the original value of path->ah (ie | |
801 | * old_ah). | |
802 | */ | |
803 | ipoib_put_ah(neigh->ah); | |
804 | } | |
1da177e4 LT |
805 | kref_get(&path->ah->ref); |
806 | neigh->ah = path->ah; | |
807 | ||
b63b70d8 | 808 | if (ipoib_cm_enabled(dev, neigh->daddr)) { |
839fcaba MT |
809 | if (!ipoib_cm_get(neigh)) |
810 | ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, | |
811 | path, | |
812 | neigh)); | |
813 | if (!ipoib_cm_get(neigh)) { | |
b63b70d8 | 814 | ipoib_neigh_free(neigh); |
839fcaba MT |
815 | continue; |
816 | } | |
817 | } | |
818 | ||
1da177e4 LT |
819 | while ((skb = __skb_dequeue(&neigh->queue))) |
820 | __skb_queue_tail(&skqueue, skb); | |
821 | } | |
ee1e2c82 | 822 | path->valid = 1; |
5872a9fc | 823 | } |
1da177e4 | 824 | |
5872a9fc | 825 | path->query = NULL; |
1da177e4 LT |
826 | complete(&path->done); |
827 | ||
828 | spin_unlock_irqrestore(&priv->lock, flags); | |
829 | ||
f72dd566 RD |
830 | if (IS_ERR_OR_NULL(ah)) |
831 | ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw); | |
832 | ||
ee1e2c82 MS |
833 | if (old_ah) |
834 | ipoib_put_ah(old_ah); | |
835 | ||
1da177e4 LT |
836 | while ((skb = __skb_dequeue(&skqueue))) { |
837 | skb->dev = dev; | |
838 | if (dev_queue_xmit(skb)) | |
839 | ipoib_warn(priv, "dev_queue_xmit failed " | |
840 | "to requeue packet\n"); | |
841 | } | |
842 | } | |
843 | ||
37c22a77 | 844 | static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid) |
1da177e4 LT |
845 | { |
846 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
847 | struct ipoib_path *path; | |
848 | ||
1401b53a JM |
849 | if (!priv->broadcast) |
850 | return NULL; | |
851 | ||
21a38489 | 852 | path = kzalloc(sizeof *path, GFP_ATOMIC); |
1da177e4 LT |
853 | if (!path) |
854 | return NULL; | |
855 | ||
21a38489 | 856 | path->dev = dev; |
1da177e4 LT |
857 | |
858 | skb_queue_head_init(&path->queue); | |
859 | ||
860 | INIT_LIST_HEAD(&path->neigh_list); | |
1da177e4 | 861 | |
37c22a77 | 862 | memcpy(path->pathrec.dgid.raw, gid, sizeof (union ib_gid)); |
2337f809 RD |
863 | path->pathrec.sgid = priv->local_gid; |
864 | path->pathrec.pkey = cpu_to_be16(priv->pkey); | |
81668838 SH |
865 | path->pathrec.numb_path = 1; |
866 | path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class; | |
1da177e4 LT |
867 | |
868 | return path; | |
869 | } | |
870 | ||
871 | static int path_rec_start(struct net_device *dev, | |
872 | struct ipoib_path *path) | |
873 | { | |
874 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
875 | ||
5b095d98 | 876 | ipoib_dbg(priv, "Start path record lookup for %pI6\n", |
fcace2fe | 877 | path->pathrec.dgid.raw); |
1da177e4 | 878 | |
65c7edda RD |
879 | init_completion(&path->done); |
880 | ||
1da177e4 | 881 | path->query_id = |
c1a0b23b | 882 | ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port, |
1da177e4 LT |
883 | &path->pathrec, |
884 | IB_SA_PATH_REC_DGID | | |
885 | IB_SA_PATH_REC_SGID | | |
886 | IB_SA_PATH_REC_NUMB_PATH | | |
81668838 | 887 | IB_SA_PATH_REC_TRAFFIC_CLASS | |
1da177e4 LT |
888 | IB_SA_PATH_REC_PKEY, |
889 | 1000, GFP_ATOMIC, | |
890 | path_rec_completion, | |
891 | path, &path->query); | |
892 | if (path->query_id < 0) { | |
01b3fc8b | 893 | ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id); |
1da177e4 | 894 | path->query = NULL; |
93a3ab93 | 895 | complete(&path->done); |
1da177e4 LT |
896 | return path->query_id; |
897 | } | |
898 | ||
899 | return 0; | |
900 | } | |
901 | ||
b63b70d8 SP |
902 | static void neigh_add_path(struct sk_buff *skb, u8 *daddr, |
903 | struct net_device *dev) | |
1da177e4 LT |
904 | { |
905 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
906 | struct ipoib_path *path; | |
907 | struct ipoib_neigh *neigh; | |
943c246e | 908 | unsigned long flags; |
1da177e4 | 909 | |
b5120a6e | 910 | spin_lock_irqsave(&priv->lock, flags); |
b63b70d8 | 911 | neigh = ipoib_neigh_alloc(daddr, dev); |
1da177e4 | 912 | if (!neigh) { |
b5120a6e | 913 | spin_unlock_irqrestore(&priv->lock, flags); |
de903512 | 914 | ++dev->stats.tx_dropped; |
1da177e4 LT |
915 | dev_kfree_skb_any(skb); |
916 | return; | |
917 | } | |
918 | ||
b63b70d8 | 919 | path = __path_find(dev, daddr + 4); |
1da177e4 | 920 | if (!path) { |
b63b70d8 | 921 | path = path_rec_create(dev, daddr + 4); |
1da177e4 | 922 | if (!path) |
d2e0655e | 923 | goto err_path; |
1da177e4 LT |
924 | |
925 | __path_add(dev, path); | |
926 | } | |
927 | ||
928 | list_add_tail(&neigh->list, &path->neigh_list); | |
929 | ||
47f7a071 | 930 | if (path->ah) { |
1da177e4 LT |
931 | kref_get(&path->ah->ref); |
932 | neigh->ah = path->ah; | |
933 | ||
b63b70d8 | 934 | if (ipoib_cm_enabled(dev, neigh->daddr)) { |
839fcaba MT |
935 | if (!ipoib_cm_get(neigh)) |
936 | ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh)); | |
937 | if (!ipoib_cm_get(neigh)) { | |
b63b70d8 | 938 | ipoib_neigh_free(neigh); |
839fcaba MT |
939 | goto err_drop; |
940 | } | |
941 | if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) | |
942 | __skb_queue_tail(&neigh->queue, skb); | |
943 | else { | |
944 | ipoib_warn(priv, "queue length limit %d. Packet drop.\n", | |
945 | skb_queue_len(&neigh->queue)); | |
946 | goto err_drop; | |
947 | } | |
721d67cd RD |
948 | } else { |
949 | spin_unlock_irqrestore(&priv->lock, flags); | |
b63b70d8 SP |
950 | ipoib_send(dev, skb, path->ah, IPOIB_QPN(daddr)); |
951 | ipoib_neigh_put(neigh); | |
721d67cd RD |
952 | return; |
953 | } | |
1da177e4 LT |
954 | } else { |
955 | neigh->ah = NULL; | |
1da177e4 LT |
956 | |
957 | if (!path->query && path_rec_start(dev, path)) | |
49b8e744 | 958 | goto err_path; |
1e85b806 ES |
959 | if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) |
960 | __skb_queue_tail(&neigh->queue, skb); | |
961 | else | |
962 | goto err_drop; | |
1da177e4 LT |
963 | } |
964 | ||
943c246e | 965 | spin_unlock_irqrestore(&priv->lock, flags); |
b63b70d8 | 966 | ipoib_neigh_put(neigh); |
1da177e4 LT |
967 | return; |
968 | ||
d2e0655e | 969 | err_path: |
b63b70d8 | 970 | ipoib_neigh_free(neigh); |
839fcaba | 971 | err_drop: |
de903512 | 972 | ++dev->stats.tx_dropped; |
1da177e4 LT |
973 | dev_kfree_skb_any(skb); |
974 | ||
943c246e | 975 | spin_unlock_irqrestore(&priv->lock, flags); |
b63b70d8 | 976 | ipoib_neigh_put(neigh); |
1da177e4 LT |
977 | } |
978 | ||
979 | static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev, | |
936d7de3 | 980 | struct ipoib_cb *cb) |
1da177e4 LT |
981 | { |
982 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
983 | struct ipoib_path *path; | |
943c246e | 984 | unsigned long flags; |
1da177e4 | 985 | |
943c246e | 986 | spin_lock_irqsave(&priv->lock, flags); |
1da177e4 | 987 | |
936d7de3 | 988 | path = __path_find(dev, cb->hwaddr + 4); |
ee1e2c82 | 989 | if (!path || !path->valid) { |
71d98b46 JM |
990 | int new_path = 0; |
991 | ||
992 | if (!path) { | |
936d7de3 | 993 | path = path_rec_create(dev, cb->hwaddr + 4); |
71d98b46 JM |
994 | new_path = 1; |
995 | } | |
1da177e4 | 996 | if (path) { |
1e85b806 ES |
997 | if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) { |
998 | __skb_queue_tail(&path->queue, skb); | |
999 | } else { | |
1000 | ++dev->stats.tx_dropped; | |
1001 | dev_kfree_skb_any(skb); | |
1002 | } | |
1da177e4 | 1003 | |
ff79ae80 | 1004 | if (!path->query && path_rec_start(dev, path)) { |
943c246e | 1005 | spin_unlock_irqrestore(&priv->lock, flags); |
71d98b46 JM |
1006 | if (new_path) |
1007 | path_free(dev, path); | |
1da177e4 LT |
1008 | return; |
1009 | } else | |
1010 | __path_add(dev, path); | |
1011 | } else { | |
de903512 | 1012 | ++dev->stats.tx_dropped; |
1da177e4 LT |
1013 | dev_kfree_skb_any(skb); |
1014 | } | |
1015 | ||
943c246e | 1016 | spin_unlock_irqrestore(&priv->lock, flags); |
1da177e4 LT |
1017 | return; |
1018 | } | |
1019 | ||
47f7a071 | 1020 | if (path->ah) { |
1da177e4 LT |
1021 | ipoib_dbg(priv, "Send unicast ARP to %04x\n", |
1022 | be16_to_cpu(path->pathrec.dlid)); | |
1023 | ||
721d67cd | 1024 | spin_unlock_irqrestore(&priv->lock, flags); |
936d7de3 | 1025 | ipoib_send(dev, skb, path->ah, IPOIB_QPN(cb->hwaddr)); |
721d67cd | 1026 | return; |
1da177e4 LT |
1027 | } else if ((path->query || !path_rec_start(dev, path)) && |
1028 | skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) { | |
1da177e4 LT |
1029 | __skb_queue_tail(&path->queue, skb); |
1030 | } else { | |
de903512 | 1031 | ++dev->stats.tx_dropped; |
1da177e4 LT |
1032 | dev_kfree_skb_any(skb); |
1033 | } | |
1034 | ||
943c246e | 1035 | spin_unlock_irqrestore(&priv->lock, flags); |
1da177e4 LT |
1036 | } |
1037 | ||
1038 | static int ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev) | |
1039 | { | |
1040 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1041 | struct ipoib_neigh *neigh; | |
b49fe362 | 1042 | struct ipoib_cb *cb = ipoib_skb_cb(skb); |
b63b70d8 | 1043 | struct ipoib_header *header; |
1da177e4 LT |
1044 | unsigned long flags; |
1045 | ||
b63b70d8 SP |
1046 | header = (struct ipoib_header *) skb->data; |
1047 | ||
1048 | if (unlikely(cb->hwaddr[4] == 0xff)) { | |
1049 | /* multicast, arrange "if" according to probability */ | |
1050 | if ((header->proto != htons(ETH_P_IP)) && | |
1051 | (header->proto != htons(ETH_P_IPV6)) && | |
1052 | (header->proto != htons(ETH_P_ARP)) && | |
dc850b0e PM |
1053 | (header->proto != htons(ETH_P_RARP)) && |
1054 | (header->proto != htons(ETH_P_TIPC))) { | |
b63b70d8 | 1055 | /* ethertype not supported by IPoIB */ |
17e6abee DM |
1056 | ++dev->stats.tx_dropped; |
1057 | dev_kfree_skb_any(skb); | |
b63b70d8 | 1058 | return NETDEV_TX_OK; |
17e6abee | 1059 | } |
b63b70d8 SP |
1060 | /* Add in the P_Key for multicast*/ |
1061 | cb->hwaddr[8] = (priv->pkey >> 8) & 0xff; | |
1062 | cb->hwaddr[9] = priv->pkey & 0xff; | |
1063 | ||
1064 | neigh = ipoib_neigh_get(dev, cb->hwaddr); | |
1065 | if (likely(neigh)) | |
1066 | goto send_using_neigh; | |
1067 | ipoib_mcast_send(dev, cb->hwaddr, skb); | |
1068 | return NETDEV_TX_OK; | |
17e6abee | 1069 | } |
1da177e4 | 1070 | |
b63b70d8 SP |
1071 | /* unicast, arrange "switch" according to probability */ |
1072 | switch (header->proto) { | |
1073 | case htons(ETH_P_IP): | |
1074 | case htons(ETH_P_IPV6): | |
dc850b0e | 1075 | case htons(ETH_P_TIPC): |
b63b70d8 SP |
1076 | neigh = ipoib_neigh_get(dev, cb->hwaddr); |
1077 | if (unlikely(!neigh)) { | |
1078 | neigh_add_path(skb, cb->hwaddr, dev); | |
1079 | return NETDEV_TX_OK; | |
a50df398 | 1080 | } |
b63b70d8 SP |
1081 | break; |
1082 | case htons(ETH_P_ARP): | |
1083 | case htons(ETH_P_RARP): | |
1084 | /* for unicast ARP and RARP should always perform path find */ | |
1085 | unicast_arp_send(skb, dev, cb); | |
1086 | return NETDEV_TX_OK; | |
1087 | default: | |
1088 | /* ethertype not supported by IPoIB */ | |
1089 | ++dev->stats.tx_dropped; | |
1090 | dev_kfree_skb_any(skb); | |
1091 | return NETDEV_TX_OK; | |
1092 | } | |
8a7f7521 | 1093 | |
b63b70d8 SP |
1094 | send_using_neigh: |
1095 | /* note we now hold a ref to neigh */ | |
1096 | if (ipoib_cm_get(neigh)) { | |
1097 | if (ipoib_cm_up(neigh)) { | |
1098 | ipoib_cm_send(dev, skb, ipoib_cm_get(neigh)); | |
1099 | goto unref; | |
1da177e4 | 1100 | } |
b63b70d8 SP |
1101 | } else if (neigh->ah) { |
1102 | ipoib_send(dev, skb, neigh->ah, IPOIB_QPN(cb->hwaddr)); | |
1103 | goto unref; | |
1104 | } | |
1da177e4 | 1105 | |
b63b70d8 SP |
1106 | if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) { |
1107 | spin_lock_irqsave(&priv->lock, flags); | |
1108 | __skb_queue_tail(&neigh->queue, skb); | |
1109 | spin_unlock_irqrestore(&priv->lock, flags); | |
1da177e4 | 1110 | } else { |
b63b70d8 SP |
1111 | ++dev->stats.tx_dropped; |
1112 | dev_kfree_skb_any(skb); | |
1113 | } | |
1da177e4 | 1114 | |
b63b70d8 SP |
1115 | unref: |
1116 | ipoib_neigh_put(neigh); | |
1da177e4 | 1117 | |
1da177e4 LT |
1118 | return NETDEV_TX_OK; |
1119 | } | |
1120 | ||
1da177e4 LT |
1121 | static void ipoib_timeout(struct net_device *dev) |
1122 | { | |
1123 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1124 | ||
4b2d319b | 1125 | ipoib_warn(priv, "transmit timeout: latency %d msecs\n", |
4d0e9657 | 1126 | jiffies_to_msecs(jiffies - dev_trans_start(dev))); |
4b2d319b RD |
1127 | ipoib_warn(priv, "queue stopped %d, tx_head %u, tx_tail %u\n", |
1128 | netif_queue_stopped(dev), | |
1129 | priv->tx_head, priv->tx_tail); | |
1da177e4 LT |
1130 | /* XXX reset QP, etc. */ |
1131 | } | |
1132 | ||
1133 | static int ipoib_hard_header(struct sk_buff *skb, | |
1134 | struct net_device *dev, | |
1135 | unsigned short type, | |
3b04ddde | 1136 | const void *daddr, const void *saddr, unsigned len) |
1da177e4 LT |
1137 | { |
1138 | struct ipoib_header *header; | |
b49fe362 | 1139 | struct ipoib_cb *cb = ipoib_skb_cb(skb); |
1da177e4 LT |
1140 | |
1141 | header = (struct ipoib_header *) skb_push(skb, sizeof *header); | |
1142 | ||
1143 | header->proto = htons(type); | |
1144 | header->reserved = 0; | |
1145 | ||
1146 | /* | |
b63b70d8 | 1147 | * we don't rely on dst_entry structure, always stuff the |
936d7de3 RD |
1148 | * destination address into skb->cb so we can figure out where |
1149 | * to send the packet later. | |
1da177e4 | 1150 | */ |
b63b70d8 | 1151 | memcpy(cb->hwaddr, daddr, INFINIBAND_ALEN); |
1da177e4 | 1152 | |
83bdd3b9 | 1153 | return sizeof *header; |
1da177e4 LT |
1154 | } |
1155 | ||
1156 | static void ipoib_set_mcast_list(struct net_device *dev) | |
1157 | { | |
1158 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1159 | ||
7a343d4c LA |
1160 | if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) { |
1161 | ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set"); | |
1162 | return; | |
1163 | } | |
1164 | ||
0b39578b | 1165 | queue_work(priv->wq, &priv->restart_task); |
1da177e4 LT |
1166 | } |
1167 | ||
5aa7add8 ND |
1168 | static int ipoib_get_iflink(const struct net_device *dev) |
1169 | { | |
1170 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1171 | ||
2c153959 ES |
1172 | /* parent interface */ |
1173 | if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) | |
1174 | return dev->ifindex; | |
1175 | ||
1176 | /* child/vlan interface */ | |
5aa7add8 ND |
1177 | return priv->parent->ifindex; |
1178 | } | |
1179 | ||
b63b70d8 | 1180 | static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr) |
1da177e4 | 1181 | { |
b63b70d8 SP |
1182 | /* |
1183 | * Use only the address parts that contributes to spreading | |
1184 | * The subnet prefix is not used as one can not connect to | |
1185 | * same remote port (GUID) using the same remote QPN via two | |
1186 | * different subnets. | |
1187 | */ | |
1188 | /* qpn octets[1:4) & port GUID octets[12:20) */ | |
9d1ad66e | 1189 | u32 *d32 = (u32 *) daddr; |
b63b70d8 SP |
1190 | u32 hv; |
1191 | ||
9d1ad66e | 1192 | hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0); |
b63b70d8 SP |
1193 | return hv & htbl->mask; |
1194 | } | |
1195 | ||
1196 | struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr) | |
1197 | { | |
1198 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1199 | struct ipoib_neigh_table *ntbl = &priv->ntbl; | |
1200 | struct ipoib_neigh_hash *htbl; | |
1201 | struct ipoib_neigh *neigh = NULL; | |
1202 | u32 hash_val; | |
1203 | ||
1204 | rcu_read_lock_bh(); | |
1205 | ||
1206 | htbl = rcu_dereference_bh(ntbl->htbl); | |
1207 | ||
1208 | if (!htbl) | |
1209 | goto out_unlock; | |
1210 | ||
1211 | hash_val = ipoib_addr_hash(htbl, daddr); | |
1212 | for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]); | |
1213 | neigh != NULL; | |
1214 | neigh = rcu_dereference_bh(neigh->hnext)) { | |
1215 | if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) { | |
1216 | /* found, take one ref on behalf of the caller */ | |
1217 | if (!atomic_inc_not_zero(&neigh->refcnt)) { | |
1218 | /* deleted */ | |
1219 | neigh = NULL; | |
1220 | goto out_unlock; | |
1221 | } | |
61c78eea ES |
1222 | |
1223 | if (likely(skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE)) | |
1224 | neigh->alive = jiffies; | |
b63b70d8 SP |
1225 | goto out_unlock; |
1226 | } | |
1227 | } | |
1228 | ||
1229 | out_unlock: | |
1230 | rcu_read_unlock_bh(); | |
1231 | return neigh; | |
1232 | } | |
1233 | ||
1234 | static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv) | |
1235 | { | |
1236 | struct ipoib_neigh_table *ntbl = &priv->ntbl; | |
1237 | struct ipoib_neigh_hash *htbl; | |
1238 | unsigned long neigh_obsolete; | |
1239 | unsigned long dt; | |
1da177e4 | 1240 | unsigned long flags; |
b63b70d8 | 1241 | int i; |
bd99b2e0 | 1242 | LIST_HEAD(remove_list); |
1da177e4 | 1243 | |
b63b70d8 | 1244 | if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags)) |
732a2170 | 1245 | return; |
1da177e4 | 1246 | |
b5120a6e | 1247 | spin_lock_irqsave(&priv->lock, flags); |
b63b70d8 SP |
1248 | |
1249 | htbl = rcu_dereference_protected(ntbl->htbl, | |
b5120a6e | 1250 | lockdep_is_held(&priv->lock)); |
b63b70d8 SP |
1251 | |
1252 | if (!htbl) | |
1253 | goto out_unlock; | |
1254 | ||
1255 | /* neigh is obsolete if it was idle for two GC periods */ | |
1256 | dt = 2 * arp_tbl.gc_interval; | |
1257 | neigh_obsolete = jiffies - dt; | |
1258 | /* handle possible race condition */ | |
1259 | if (test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags)) | |
1260 | goto out_unlock; | |
1261 | ||
1262 | for (i = 0; i < htbl->size; i++) { | |
1263 | struct ipoib_neigh *neigh; | |
1264 | struct ipoib_neigh __rcu **np = &htbl->buckets[i]; | |
1265 | ||
1266 | while ((neigh = rcu_dereference_protected(*np, | |
b5120a6e | 1267 | lockdep_is_held(&priv->lock))) != NULL) { |
b63b70d8 SP |
1268 | /* was the neigh idle for two GC periods */ |
1269 | if (time_after(neigh_obsolete, neigh->alive)) { | |
bd99b2e0 | 1270 | |
432c55ff | 1271 | ipoib_check_and_add_mcast_sendonly(priv, neigh->daddr + 4, &remove_list); |
bd99b2e0 | 1272 | |
b63b70d8 SP |
1273 | rcu_assign_pointer(*np, |
1274 | rcu_dereference_protected(neigh->hnext, | |
b5120a6e | 1275 | lockdep_is_held(&priv->lock))); |
b63b70d8 | 1276 | /* remove from path/mc list */ |
b63b70d8 | 1277 | list_del(&neigh->list); |
b63b70d8 SP |
1278 | call_rcu(&neigh->rcu, ipoib_neigh_reclaim); |
1279 | } else { | |
1280 | np = &neigh->hnext; | |
1281 | } | |
1da177e4 | 1282 | |
b63b70d8 SP |
1283 | } |
1284 | } | |
1da177e4 | 1285 | |
b63b70d8 | 1286 | out_unlock: |
b5120a6e | 1287 | spin_unlock_irqrestore(&priv->lock, flags); |
50be28de | 1288 | ipoib_mcast_remove_list(&remove_list); |
b63b70d8 | 1289 | } |
1da177e4 | 1290 | |
b63b70d8 SP |
1291 | static void ipoib_reap_neigh(struct work_struct *work) |
1292 | { | |
1293 | struct ipoib_dev_priv *priv = | |
1294 | container_of(work, struct ipoib_dev_priv, neigh_reap_task.work); | |
1295 | ||
1296 | __ipoib_reap_neigh(priv); | |
1297 | ||
1298 | if (!test_bit(IPOIB_STOP_NEIGH_GC, &priv->flags)) | |
0b39578b | 1299 | queue_delayed_work(priv->wq, &priv->neigh_reap_task, |
b63b70d8 | 1300 | arp_tbl.gc_interval); |
1da177e4 LT |
1301 | } |
1302 | ||
b63b70d8 SP |
1303 | |
1304 | static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr, | |
732a2170 | 1305 | struct net_device *dev) |
d2e0655e MT |
1306 | { |
1307 | struct ipoib_neigh *neigh; | |
1308 | ||
b63b70d8 | 1309 | neigh = kzalloc(sizeof *neigh, GFP_ATOMIC); |
d2e0655e MT |
1310 | if (!neigh) |
1311 | return NULL; | |
1312 | ||
732a2170 | 1313 | neigh->dev = dev; |
b63b70d8 | 1314 | memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr)); |
82b39913 | 1315 | skb_queue_head_init(&neigh->queue); |
b63b70d8 | 1316 | INIT_LIST_HEAD(&neigh->list); |
839fcaba | 1317 | ipoib_cm_set(neigh, NULL); |
b63b70d8 SP |
1318 | /* one ref on behalf of the caller */ |
1319 | atomic_set(&neigh->refcnt, 1); | |
1320 | ||
1321 | return neigh; | |
1322 | } | |
1323 | ||
1324 | struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr, | |
1325 | struct net_device *dev) | |
1326 | { | |
1327 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1328 | struct ipoib_neigh_table *ntbl = &priv->ntbl; | |
1329 | struct ipoib_neigh_hash *htbl; | |
1330 | struct ipoib_neigh *neigh; | |
1331 | u32 hash_val; | |
1332 | ||
b63b70d8 | 1333 | htbl = rcu_dereference_protected(ntbl->htbl, |
b5120a6e | 1334 | lockdep_is_held(&priv->lock)); |
b63b70d8 SP |
1335 | if (!htbl) { |
1336 | neigh = NULL; | |
1337 | goto out_unlock; | |
1338 | } | |
1339 | ||
1340 | /* need to add a new neigh, but maybe some other thread succeeded? | |
1341 | * recalc hash, maybe hash resize took place so we do a search | |
1342 | */ | |
1343 | hash_val = ipoib_addr_hash(htbl, daddr); | |
1344 | for (neigh = rcu_dereference_protected(htbl->buckets[hash_val], | |
b5120a6e | 1345 | lockdep_is_held(&priv->lock)); |
b63b70d8 SP |
1346 | neigh != NULL; |
1347 | neigh = rcu_dereference_protected(neigh->hnext, | |
b5120a6e | 1348 | lockdep_is_held(&priv->lock))) { |
b63b70d8 SP |
1349 | if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) { |
1350 | /* found, take one ref on behalf of the caller */ | |
1351 | if (!atomic_inc_not_zero(&neigh->refcnt)) { | |
1352 | /* deleted */ | |
1353 | neigh = NULL; | |
1354 | break; | |
1355 | } | |
1356 | neigh->alive = jiffies; | |
1357 | goto out_unlock; | |
1358 | } | |
1359 | } | |
1360 | ||
1361 | neigh = ipoib_neigh_ctor(daddr, dev); | |
1362 | if (!neigh) | |
1363 | goto out_unlock; | |
1364 | ||
1365 | /* one ref on behalf of the hash table */ | |
1366 | atomic_inc(&neigh->refcnt); | |
1367 | neigh->alive = jiffies; | |
1368 | /* put in hash */ | |
1369 | rcu_assign_pointer(neigh->hnext, | |
1370 | rcu_dereference_protected(htbl->buckets[hash_val], | |
b5120a6e | 1371 | lockdep_is_held(&priv->lock))); |
b63b70d8 SP |
1372 | rcu_assign_pointer(htbl->buckets[hash_val], neigh); |
1373 | atomic_inc(&ntbl->entries); | |
1374 | ||
1375 | out_unlock: | |
d2e0655e MT |
1376 | |
1377 | return neigh; | |
1378 | } | |
1379 | ||
b63b70d8 | 1380 | void ipoib_neigh_dtor(struct ipoib_neigh *neigh) |
d2e0655e | 1381 | { |
b63b70d8 SP |
1382 | /* neigh reference count was dropprd to zero */ |
1383 | struct net_device *dev = neigh->dev; | |
1384 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
2745b5b7 | 1385 | struct sk_buff *skb; |
b63b70d8 SP |
1386 | if (neigh->ah) |
1387 | ipoib_put_ah(neigh->ah); | |
2745b5b7 | 1388 | while ((skb = __skb_dequeue(&neigh->queue))) { |
de903512 | 1389 | ++dev->stats.tx_dropped; |
2745b5b7 MT |
1390 | dev_kfree_skb_any(skb); |
1391 | } | |
839fcaba MT |
1392 | if (ipoib_cm_get(neigh)) |
1393 | ipoib_cm_destroy_tx(ipoib_cm_get(neigh)); | |
b63b70d8 SP |
1394 | ipoib_dbg(netdev_priv(dev), |
1395 | "neigh free for %06x %pI6\n", | |
1396 | IPOIB_QPN(neigh->daddr), | |
1397 | neigh->daddr + 4); | |
d2e0655e | 1398 | kfree(neigh); |
b63b70d8 SP |
1399 | if (atomic_dec_and_test(&priv->ntbl.entries)) { |
1400 | if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags)) | |
1401 | complete(&priv->ntbl.flushed); | |
1402 | } | |
1403 | } | |
1404 | ||
1405 | static void ipoib_neigh_reclaim(struct rcu_head *rp) | |
1406 | { | |
1407 | /* Called as a result of removal from hash table */ | |
1408 | struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu); | |
1409 | /* note TX context may hold another ref */ | |
1410 | ipoib_neigh_put(neigh); | |
d2e0655e MT |
1411 | } |
1412 | ||
b63b70d8 | 1413 | void ipoib_neigh_free(struct ipoib_neigh *neigh) |
1da177e4 | 1414 | { |
b63b70d8 SP |
1415 | struct net_device *dev = neigh->dev; |
1416 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1417 | struct ipoib_neigh_table *ntbl = &priv->ntbl; | |
1418 | struct ipoib_neigh_hash *htbl; | |
1419 | struct ipoib_neigh __rcu **np; | |
1420 | struct ipoib_neigh *n; | |
1421 | u32 hash_val; | |
1422 | ||
b63b70d8 | 1423 | htbl = rcu_dereference_protected(ntbl->htbl, |
b5120a6e | 1424 | lockdep_is_held(&priv->lock)); |
b63b70d8 | 1425 | if (!htbl) |
b5120a6e | 1426 | return; |
b63b70d8 SP |
1427 | |
1428 | hash_val = ipoib_addr_hash(htbl, neigh->daddr); | |
1429 | np = &htbl->buckets[hash_val]; | |
1430 | for (n = rcu_dereference_protected(*np, | |
b5120a6e | 1431 | lockdep_is_held(&priv->lock)); |
b63b70d8 | 1432 | n != NULL; |
6c723a68 | 1433 | n = rcu_dereference_protected(*np, |
b5120a6e | 1434 | lockdep_is_held(&priv->lock))) { |
b63b70d8 SP |
1435 | if (n == neigh) { |
1436 | /* found */ | |
1437 | rcu_assign_pointer(*np, | |
1438 | rcu_dereference_protected(neigh->hnext, | |
b5120a6e | 1439 | lockdep_is_held(&priv->lock))); |
49b8e744 JF |
1440 | /* remove from parent list */ |
1441 | list_del(&neigh->list); | |
b63b70d8 | 1442 | call_rcu(&neigh->rcu, ipoib_neigh_reclaim); |
b5120a6e | 1443 | return; |
b63b70d8 SP |
1444 | } else { |
1445 | np = &n->hnext; | |
1446 | } | |
1447 | } | |
b63b70d8 SP |
1448 | } |
1449 | ||
1450 | static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv) | |
1451 | { | |
1452 | struct ipoib_neigh_table *ntbl = &priv->ntbl; | |
1453 | struct ipoib_neigh_hash *htbl; | |
52374967 | 1454 | struct ipoib_neigh __rcu **buckets; |
b63b70d8 SP |
1455 | u32 size; |
1456 | ||
1457 | clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags); | |
1458 | ntbl->htbl = NULL; | |
b63b70d8 SP |
1459 | htbl = kzalloc(sizeof(*htbl), GFP_KERNEL); |
1460 | if (!htbl) | |
1461 | return -ENOMEM; | |
1462 | set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags); | |
1463 | size = roundup_pow_of_two(arp_tbl.gc_thresh3); | |
1464 | buckets = kzalloc(size * sizeof(*buckets), GFP_KERNEL); | |
1465 | if (!buckets) { | |
1466 | kfree(htbl); | |
1467 | return -ENOMEM; | |
1468 | } | |
1469 | htbl->size = size; | |
1470 | htbl->mask = (size - 1); | |
1471 | htbl->buckets = buckets; | |
52374967 | 1472 | RCU_INIT_POINTER(ntbl->htbl, htbl); |
66172c09 | 1473 | htbl->ntbl = ntbl; |
b63b70d8 SP |
1474 | atomic_set(&ntbl->entries, 0); |
1475 | ||
1476 | /* start garbage collection */ | |
1477 | clear_bit(IPOIB_STOP_NEIGH_GC, &priv->flags); | |
0b39578b | 1478 | queue_delayed_work(priv->wq, &priv->neigh_reap_task, |
b63b70d8 | 1479 | arp_tbl.gc_interval); |
1da177e4 LT |
1480 | |
1481 | return 0; | |
1482 | } | |
1483 | ||
b63b70d8 SP |
1484 | static void neigh_hash_free_rcu(struct rcu_head *head) |
1485 | { | |
1486 | struct ipoib_neigh_hash *htbl = container_of(head, | |
1487 | struct ipoib_neigh_hash, | |
1488 | rcu); | |
1489 | struct ipoib_neigh __rcu **buckets = htbl->buckets; | |
66172c09 | 1490 | struct ipoib_neigh_table *ntbl = htbl->ntbl; |
b63b70d8 SP |
1491 | |
1492 | kfree(buckets); | |
1493 | kfree(htbl); | |
66172c09 | 1494 | complete(&ntbl->deleted); |
b63b70d8 SP |
1495 | } |
1496 | ||
1497 | void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid) | |
1498 | { | |
1499 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1500 | struct ipoib_neigh_table *ntbl = &priv->ntbl; | |
1501 | struct ipoib_neigh_hash *htbl; | |
1502 | unsigned long flags; | |
1503 | int i; | |
1504 | ||
1505 | /* remove all neigh connected to a given path or mcast */ | |
b5120a6e | 1506 | spin_lock_irqsave(&priv->lock, flags); |
b63b70d8 SP |
1507 | |
1508 | htbl = rcu_dereference_protected(ntbl->htbl, | |
b5120a6e | 1509 | lockdep_is_held(&priv->lock)); |
b63b70d8 SP |
1510 | |
1511 | if (!htbl) | |
1512 | goto out_unlock; | |
1513 | ||
1514 | for (i = 0; i < htbl->size; i++) { | |
1515 | struct ipoib_neigh *neigh; | |
1516 | struct ipoib_neigh __rcu **np = &htbl->buckets[i]; | |
1517 | ||
1518 | while ((neigh = rcu_dereference_protected(*np, | |
b5120a6e | 1519 | lockdep_is_held(&priv->lock))) != NULL) { |
b63b70d8 SP |
1520 | /* delete neighs belong to this parent */ |
1521 | if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) { | |
1522 | rcu_assign_pointer(*np, | |
1523 | rcu_dereference_protected(neigh->hnext, | |
b5120a6e | 1524 | lockdep_is_held(&priv->lock))); |
b63b70d8 | 1525 | /* remove from parent list */ |
b63b70d8 | 1526 | list_del(&neigh->list); |
b63b70d8 SP |
1527 | call_rcu(&neigh->rcu, ipoib_neigh_reclaim); |
1528 | } else { | |
1529 | np = &neigh->hnext; | |
1530 | } | |
1531 | ||
1532 | } | |
1533 | } | |
1534 | out_unlock: | |
b5120a6e | 1535 | spin_unlock_irqrestore(&priv->lock, flags); |
b63b70d8 SP |
1536 | } |
1537 | ||
1538 | static void ipoib_flush_neighs(struct ipoib_dev_priv *priv) | |
1539 | { | |
1540 | struct ipoib_neigh_table *ntbl = &priv->ntbl; | |
1541 | struct ipoib_neigh_hash *htbl; | |
1542 | unsigned long flags; | |
66172c09 | 1543 | int i, wait_flushed = 0; |
b63b70d8 | 1544 | |
66172c09 | 1545 | init_completion(&priv->ntbl.flushed); |
b63b70d8 | 1546 | |
b5120a6e | 1547 | spin_lock_irqsave(&priv->lock, flags); |
b63b70d8 SP |
1548 | |
1549 | htbl = rcu_dereference_protected(ntbl->htbl, | |
b5120a6e | 1550 | lockdep_is_held(&priv->lock)); |
b63b70d8 SP |
1551 | if (!htbl) |
1552 | goto out_unlock; | |
1553 | ||
66172c09 SP |
1554 | wait_flushed = atomic_read(&priv->ntbl.entries); |
1555 | if (!wait_flushed) | |
1556 | goto free_htbl; | |
1557 | ||
b63b70d8 SP |
1558 | for (i = 0; i < htbl->size; i++) { |
1559 | struct ipoib_neigh *neigh; | |
1560 | struct ipoib_neigh __rcu **np = &htbl->buckets[i]; | |
1561 | ||
1562 | while ((neigh = rcu_dereference_protected(*np, | |
b5120a6e | 1563 | lockdep_is_held(&priv->lock))) != NULL) { |
b63b70d8 SP |
1564 | rcu_assign_pointer(*np, |
1565 | rcu_dereference_protected(neigh->hnext, | |
b5120a6e | 1566 | lockdep_is_held(&priv->lock))); |
b63b70d8 | 1567 | /* remove from path/mc list */ |
b63b70d8 | 1568 | list_del(&neigh->list); |
b63b70d8 SP |
1569 | call_rcu(&neigh->rcu, ipoib_neigh_reclaim); |
1570 | } | |
1571 | } | |
1572 | ||
66172c09 | 1573 | free_htbl: |
b63b70d8 SP |
1574 | rcu_assign_pointer(ntbl->htbl, NULL); |
1575 | call_rcu(&htbl->rcu, neigh_hash_free_rcu); | |
1576 | ||
1577 | out_unlock: | |
b5120a6e | 1578 | spin_unlock_irqrestore(&priv->lock, flags); |
66172c09 SP |
1579 | if (wait_flushed) |
1580 | wait_for_completion(&priv->ntbl.flushed); | |
b63b70d8 SP |
1581 | } |
1582 | ||
1583 | static void ipoib_neigh_hash_uninit(struct net_device *dev) | |
1584 | { | |
1585 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1586 | int stopped; | |
1587 | ||
1588 | ipoib_dbg(priv, "ipoib_neigh_hash_uninit\n"); | |
66172c09 | 1589 | init_completion(&priv->ntbl.deleted); |
b63b70d8 SP |
1590 | set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags); |
1591 | ||
1592 | /* Stop GC if called at init fail need to cancel work */ | |
1593 | stopped = test_and_set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags); | |
1594 | if (!stopped) | |
1595 | cancel_delayed_work(&priv->neigh_reap_task); | |
1596 | ||
66172c09 SP |
1597 | ipoib_flush_neighs(priv); |
1598 | ||
1599 | wait_for_completion(&priv->ntbl.deleted); | |
b63b70d8 SP |
1600 | } |
1601 | ||
1602 | ||
1da177e4 LT |
1603 | int ipoib_dev_init(struct net_device *dev, struct ib_device *ca, int port) |
1604 | { | |
1605 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1606 | ||
1607 | /* Allocate RX/TX "rings" to hold queued skbs */ | |
0f485251 | 1608 | priv->rx_ring = kzalloc(ipoib_recvq_size * sizeof *priv->rx_ring, |
1da177e4 LT |
1609 | GFP_KERNEL); |
1610 | if (!priv->rx_ring) { | |
1611 | printk(KERN_WARNING "%s: failed to allocate RX ring (%d entries)\n", | |
0f485251 | 1612 | ca->name, ipoib_recvq_size); |
be7aa663 | 1613 | goto out; |
1da177e4 | 1614 | } |
1da177e4 | 1615 | |
948579cd | 1616 | priv->tx_ring = vzalloc(ipoib_sendq_size * sizeof *priv->tx_ring); |
1da177e4 LT |
1617 | if (!priv->tx_ring) { |
1618 | printk(KERN_WARNING "%s: failed to allocate TX ring (%d entries)\n", | |
0f485251 | 1619 | ca->name, ipoib_sendq_size); |
1da177e4 LT |
1620 | goto out_rx_ring_cleanup; |
1621 | } | |
1da177e4 | 1622 | |
1b524963 | 1623 | /* priv->tx_head, tx_tail & tx_outstanding are already 0 */ |
1da177e4 LT |
1624 | |
1625 | if (ipoib_ib_dev_init(dev, ca, port)) | |
1626 | goto out_tx_ring_cleanup; | |
1627 | ||
be7aa663 DL |
1628 | /* |
1629 | * Must be after ipoib_ib_dev_init so we can allocate a per | |
1630 | * device wq there and use it here | |
1631 | */ | |
1632 | if (ipoib_neigh_hash_init(priv) < 0) | |
1633 | goto out_dev_uninit; | |
1634 | ||
1da177e4 LT |
1635 | return 0; |
1636 | ||
be7aa663 DL |
1637 | out_dev_uninit: |
1638 | ipoib_ib_dev_cleanup(dev); | |
1639 | ||
1da177e4 | 1640 | out_tx_ring_cleanup: |
10313cbb | 1641 | vfree(priv->tx_ring); |
1da177e4 LT |
1642 | |
1643 | out_rx_ring_cleanup: | |
1644 | kfree(priv->rx_ring); | |
1645 | ||
1646 | out: | |
1647 | return -ENOMEM; | |
1648 | } | |
1649 | ||
1650 | void ipoib_dev_cleanup(struct net_device *dev) | |
1651 | { | |
1652 | struct ipoib_dev_priv *priv = netdev_priv(dev), *cpriv, *tcpriv; | |
9baa0b03 OG |
1653 | LIST_HEAD(head); |
1654 | ||
1655 | ASSERT_RTNL(); | |
1da177e4 | 1656 | |
1732b0ef | 1657 | ipoib_delete_debug_files(dev); |
1da177e4 LT |
1658 | |
1659 | /* Delete any child interfaces first */ | |
1660 | list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs, list) { | |
b63b70d8 SP |
1661 | /* Stop GC on child */ |
1662 | set_bit(IPOIB_STOP_NEIGH_GC, &cpriv->flags); | |
1663 | cancel_delayed_work(&cpriv->neigh_reap_task); | |
9baa0b03 | 1664 | unregister_netdevice_queue(cpriv->dev, &head); |
1da177e4 | 1665 | } |
9baa0b03 | 1666 | unregister_netdevice_many(&head); |
1da177e4 | 1667 | |
be7aa663 DL |
1668 | /* |
1669 | * Must be before ipoib_ib_dev_cleanup or we delete an in use | |
1670 | * work queue | |
1671 | */ | |
1672 | ipoib_neigh_hash_uninit(dev); | |
1673 | ||
1da177e4 LT |
1674 | ipoib_ib_dev_cleanup(dev); |
1675 | ||
92a6b34b | 1676 | kfree(priv->rx_ring); |
10313cbb | 1677 | vfree(priv->tx_ring); |
1da177e4 | 1678 | |
92a6b34b HR |
1679 | priv->rx_ring = NULL; |
1680 | priv->tx_ring = NULL; | |
1da177e4 LT |
1681 | } |
1682 | ||
9c3c5f8e EC |
1683 | static int ipoib_set_vf_link_state(struct net_device *dev, int vf, int link_state) |
1684 | { | |
1685 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1686 | ||
1687 | return ib_set_vf_link_state(priv->ca, vf, priv->port, link_state); | |
1688 | } | |
1689 | ||
1690 | static int ipoib_get_vf_config(struct net_device *dev, int vf, | |
1691 | struct ifla_vf_info *ivf) | |
1692 | { | |
1693 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1694 | int err; | |
1695 | ||
1696 | err = ib_get_vf_config(priv->ca, vf, priv->port, ivf); | |
1697 | if (err) | |
1698 | return err; | |
1699 | ||
1700 | ivf->vf = vf; | |
1701 | ||
1702 | return 0; | |
1703 | } | |
1704 | ||
1705 | static int ipoib_set_vf_guid(struct net_device *dev, int vf, u64 guid, int type) | |
1706 | { | |
1707 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1708 | ||
1709 | if (type != IFLA_VF_IB_NODE_GUID && type != IFLA_VF_IB_PORT_GUID) | |
1710 | return -EINVAL; | |
1711 | ||
1712 | return ib_set_vf_guid(priv->ca, vf, priv->port, guid, type); | |
1713 | } | |
1714 | ||
1715 | static int ipoib_get_vf_stats(struct net_device *dev, int vf, | |
1716 | struct ifla_vf_stats *vf_stats) | |
1717 | { | |
1718 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1719 | ||
1720 | return ib_get_vf_stats(priv->ca, vf, priv->port, vf_stats); | |
1721 | } | |
1722 | ||
3b04ddde SH |
1723 | static const struct header_ops ipoib_header_ops = { |
1724 | .create = ipoib_hard_header, | |
1725 | }; | |
1726 | ||
9c3c5f8e EC |
1727 | static const struct net_device_ops ipoib_netdev_ops_pf = { |
1728 | .ndo_uninit = ipoib_uninit, | |
1729 | .ndo_open = ipoib_open, | |
1730 | .ndo_stop = ipoib_stop, | |
1731 | .ndo_change_mtu = ipoib_change_mtu, | |
1732 | .ndo_fix_features = ipoib_fix_features, | |
1733 | .ndo_start_xmit = ipoib_start_xmit, | |
1734 | .ndo_tx_timeout = ipoib_timeout, | |
1735 | .ndo_set_rx_mode = ipoib_set_mcast_list, | |
1736 | .ndo_get_iflink = ipoib_get_iflink, | |
1737 | .ndo_set_vf_link_state = ipoib_set_vf_link_state, | |
1738 | .ndo_get_vf_config = ipoib_get_vf_config, | |
1739 | .ndo_get_vf_stats = ipoib_get_vf_stats, | |
1740 | .ndo_set_vf_guid = ipoib_set_vf_guid, | |
492a7e67 | 1741 | .ndo_set_mac_address = ipoib_set_mac, |
9c3c5f8e EC |
1742 | }; |
1743 | ||
1744 | static const struct net_device_ops ipoib_netdev_ops_vf = { | |
9baa0b03 | 1745 | .ndo_uninit = ipoib_uninit, |
fe8114e8 SH |
1746 | .ndo_open = ipoib_open, |
1747 | .ndo_stop = ipoib_stop, | |
1748 | .ndo_change_mtu = ipoib_change_mtu, | |
3d96c74d | 1749 | .ndo_fix_features = ipoib_fix_features, |
fe8114e8 SH |
1750 | .ndo_start_xmit = ipoib_start_xmit, |
1751 | .ndo_tx_timeout = ipoib_timeout, | |
afc4b13d | 1752 | .ndo_set_rx_mode = ipoib_set_mcast_list, |
5aa7add8 | 1753 | .ndo_get_iflink = ipoib_get_iflink, |
fe8114e8 SH |
1754 | }; |
1755 | ||
9baa0b03 | 1756 | void ipoib_setup(struct net_device *dev) |
1da177e4 LT |
1757 | { |
1758 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1759 | ||
9c3c5f8e EC |
1760 | if (priv->hca_caps & IB_DEVICE_VIRTUAL_FUNCTION) |
1761 | dev->netdev_ops = &ipoib_netdev_ops_vf; | |
1762 | else | |
1763 | dev->netdev_ops = &ipoib_netdev_ops_pf; | |
1764 | ||
2337f809 | 1765 | dev->header_ops = &ipoib_header_ops; |
bea3348e | 1766 | |
82c24c18 EC |
1767 | ipoib_set_ethtool_ops(dev); |
1768 | ||
7f1a3867 | 1769 | netif_napi_add(dev, &priv->napi, ipoib_poll, NAPI_POLL_WEIGHT); |
1da177e4 | 1770 | |
2337f809 | 1771 | dev->watchdog_timeo = HZ; |
1da177e4 | 1772 | |
2337f809 | 1773 | dev->flags |= IFF_BROADCAST | IFF_MULTICAST; |
1da177e4 | 1774 | |
936d7de3 | 1775 | dev->hard_header_len = IPOIB_ENCAP_LEN; |
2337f809 RD |
1776 | dev->addr_len = INFINIBAND_ALEN; |
1777 | dev->type = ARPHRD_INFINIBAND; | |
1778 | dev->tx_queue_len = ipoib_sendq_size * 2; | |
eb14032f | 1779 | dev->features = (NETIF_F_VLAN_CHALLENGED | |
eb14032f | 1780 | NETIF_F_HIGHDMA); |
02875878 | 1781 | netif_keep_dst(dev); |
1da177e4 | 1782 | |
1da177e4 LT |
1783 | memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN); |
1784 | ||
1da177e4 LT |
1785 | priv->dev = dev; |
1786 | ||
1787 | spin_lock_init(&priv->lock); | |
1da177e4 | 1788 | |
f47944cc | 1789 | init_rwsem(&priv->vlan_rwsem); |
1da177e4 LT |
1790 | |
1791 | INIT_LIST_HEAD(&priv->path_list); | |
1792 | INIT_LIST_HEAD(&priv->child_intfs); | |
1793 | INIT_LIST_HEAD(&priv->dead_ahs); | |
1794 | INIT_LIST_HEAD(&priv->multicast_list); | |
1795 | ||
c4028958 | 1796 | INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task); |
e8224e4b | 1797 | INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task); |
ee1e2c82 MS |
1798 | INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light); |
1799 | INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal); | |
1800 | INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy); | |
c4028958 DH |
1801 | INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task); |
1802 | INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah); | |
b63b70d8 | 1803 | INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh); |
1da177e4 LT |
1804 | } |
1805 | ||
1806 | struct ipoib_dev_priv *ipoib_intf_alloc(const char *name) | |
1807 | { | |
1808 | struct net_device *dev; | |
1809 | ||
c835a677 TG |
1810 | dev = alloc_netdev((int)sizeof(struct ipoib_dev_priv), name, |
1811 | NET_NAME_UNKNOWN, ipoib_setup); | |
1da177e4 LT |
1812 | if (!dev) |
1813 | return NULL; | |
1814 | ||
1815 | return netdev_priv(dev); | |
1816 | } | |
1817 | ||
43cb76d9 GKH |
1818 | static ssize_t show_pkey(struct device *dev, |
1819 | struct device_attribute *attr, char *buf) | |
1da177e4 | 1820 | { |
43cb76d9 | 1821 | struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev)); |
1da177e4 LT |
1822 | |
1823 | return sprintf(buf, "0x%04x\n", priv->pkey); | |
1824 | } | |
43cb76d9 | 1825 | static DEVICE_ATTR(pkey, S_IRUGO, show_pkey, NULL); |
1da177e4 | 1826 | |
335a64a5 OG |
1827 | static ssize_t show_umcast(struct device *dev, |
1828 | struct device_attribute *attr, char *buf) | |
1829 | { | |
1830 | struct ipoib_dev_priv *priv = netdev_priv(to_net_dev(dev)); | |
1831 | ||
1832 | return sprintf(buf, "%d\n", test_bit(IPOIB_FLAG_UMCAST, &priv->flags)); | |
1833 | } | |
1834 | ||
862096a8 | 1835 | void ipoib_set_umcast(struct net_device *ndev, int umcast_val) |
335a64a5 | 1836 | { |
862096a8 | 1837 | struct ipoib_dev_priv *priv = netdev_priv(ndev); |
335a64a5 OG |
1838 | |
1839 | if (umcast_val > 0) { | |
1840 | set_bit(IPOIB_FLAG_UMCAST, &priv->flags); | |
1841 | ipoib_warn(priv, "ignoring multicast groups joined directly " | |
1842 | "by userspace\n"); | |
1843 | } else | |
1844 | clear_bit(IPOIB_FLAG_UMCAST, &priv->flags); | |
862096a8 OG |
1845 | } |
1846 | ||
1847 | static ssize_t set_umcast(struct device *dev, | |
1848 | struct device_attribute *attr, | |
1849 | const char *buf, size_t count) | |
1850 | { | |
1851 | unsigned long umcast_val = simple_strtoul(buf, NULL, 0); | |
1852 | ||
1853 | ipoib_set_umcast(to_net_dev(dev), umcast_val); | |
335a64a5 OG |
1854 | |
1855 | return count; | |
1856 | } | |
1857 | static DEVICE_ATTR(umcast, S_IWUSR | S_IRUGO, show_umcast, set_umcast); | |
1858 | ||
1859 | int ipoib_add_umcast_attr(struct net_device *dev) | |
1860 | { | |
1861 | return device_create_file(&dev->dev, &dev_attr_umcast); | |
1862 | } | |
1863 | ||
492a7e67 MB |
1864 | static void set_base_guid(struct ipoib_dev_priv *priv, union ib_gid *gid) |
1865 | { | |
1866 | struct ipoib_dev_priv *child_priv; | |
1867 | struct net_device *netdev = priv->dev; | |
1868 | ||
9b29953b | 1869 | netif_addr_lock_bh(netdev); |
492a7e67 MB |
1870 | |
1871 | memcpy(&priv->local_gid.global.interface_id, | |
1872 | &gid->global.interface_id, | |
1873 | sizeof(gid->global.interface_id)); | |
1874 | memcpy(netdev->dev_addr + 4, &priv->local_gid, sizeof(priv->local_gid)); | |
1875 | clear_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags); | |
1876 | ||
9b29953b | 1877 | netif_addr_unlock_bh(netdev); |
492a7e67 MB |
1878 | |
1879 | if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) { | |
1880 | down_read(&priv->vlan_rwsem); | |
1881 | list_for_each_entry(child_priv, &priv->child_intfs, list) | |
1882 | set_base_guid(child_priv, gid); | |
1883 | up_read(&priv->vlan_rwsem); | |
1884 | } | |
1885 | } | |
1886 | ||
1887 | static int ipoib_check_lladdr(struct net_device *dev, | |
1888 | struct sockaddr_storage *ss) | |
1889 | { | |
1890 | union ib_gid *gid = (union ib_gid *)(ss->__data + 4); | |
1891 | int ret = 0; | |
1892 | ||
9b29953b | 1893 | netif_addr_lock_bh(dev); |
492a7e67 MB |
1894 | |
1895 | /* Make sure the QPN, reserved and subnet prefix match the current | |
1896 | * lladdr, it also makes sure the lladdr is unicast. | |
1897 | */ | |
1898 | if (memcmp(dev->dev_addr, ss->__data, | |
1899 | 4 + sizeof(gid->global.subnet_prefix)) || | |
1900 | gid->global.interface_id == 0) | |
1901 | ret = -EINVAL; | |
1902 | ||
9b29953b | 1903 | netif_addr_unlock_bh(dev); |
492a7e67 MB |
1904 | |
1905 | return ret; | |
1906 | } | |
1907 | ||
1908 | static int ipoib_set_mac(struct net_device *dev, void *addr) | |
1909 | { | |
1910 | struct ipoib_dev_priv *priv = netdev_priv(dev); | |
1911 | struct sockaddr_storage *ss = addr; | |
1912 | int ret; | |
1913 | ||
1914 | if (!(dev->priv_flags & IFF_LIVE_ADDR_CHANGE) && netif_running(dev)) | |
1915 | return -EBUSY; | |
1916 | ||
1917 | ret = ipoib_check_lladdr(dev, ss); | |
1918 | if (ret) | |
1919 | return ret; | |
1920 | ||
1921 | set_base_guid(priv, (union ib_gid *)(ss->__data + 4)); | |
1922 | ||
1923 | queue_work(ipoib_workqueue, &priv->flush_light); | |
1924 | ||
1925 | return 0; | |
1926 | } | |
1927 | ||
43cb76d9 GKH |
1928 | static ssize_t create_child(struct device *dev, |
1929 | struct device_attribute *attr, | |
1da177e4 LT |
1930 | const char *buf, size_t count) |
1931 | { | |
1932 | int pkey; | |
1933 | int ret; | |
1934 | ||
1935 | if (sscanf(buf, "%i", &pkey) != 1) | |
1936 | return -EINVAL; | |
1937 | ||
3d790a4c | 1938 | if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000) |
1da177e4 LT |
1939 | return -EINVAL; |
1940 | ||
4ce05937 RD |
1941 | /* |
1942 | * Set the full membership bit, so that we join the right | |
1943 | * broadcast group, etc. | |
1944 | */ | |
1945 | pkey |= 0x8000; | |
1946 | ||
43cb76d9 | 1947 | ret = ipoib_vlan_add(to_net_dev(dev), pkey); |
1da177e4 LT |
1948 | |
1949 | return ret ? ret : count; | |
1950 | } | |
7a52b34b | 1951 | static DEVICE_ATTR(create_child, S_IWUSR, NULL, create_child); |
1da177e4 | 1952 | |
43cb76d9 GKH |
1953 | static ssize_t delete_child(struct device *dev, |
1954 | struct device_attribute *attr, | |
1da177e4 LT |
1955 | const char *buf, size_t count) |
1956 | { | |
1957 | int pkey; | |
1958 | int ret; | |
1959 | ||
1960 | if (sscanf(buf, "%i", &pkey) != 1) | |
1961 | return -EINVAL; | |
1962 | ||
1963 | if (pkey < 0 || pkey > 0xffff) | |
1964 | return -EINVAL; | |
1965 | ||
43cb76d9 | 1966 | ret = ipoib_vlan_delete(to_net_dev(dev), pkey); |
1da177e4 LT |
1967 | |
1968 | return ret ? ret : count; | |
1969 | ||
1970 | } | |
7a52b34b | 1971 | static DEVICE_ATTR(delete_child, S_IWUSR, NULL, delete_child); |
1da177e4 LT |
1972 | |
1973 | int ipoib_add_pkey_attr(struct net_device *dev) | |
1974 | { | |
43cb76d9 | 1975 | return device_create_file(&dev->dev, &dev_attr_pkey); |
1da177e4 LT |
1976 | } |
1977 | ||
83bb63f6 OG |
1978 | int ipoib_set_dev_features(struct ipoib_dev_priv *priv, struct ib_device *hca) |
1979 | { | |
4a061b28 | 1980 | priv->hca_caps = hca->attrs.device_cap_flags; |
83bb63f6 OG |
1981 | |
1982 | if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) { | |
5faba546 | 1983 | priv->dev->hw_features = NETIF_F_IP_CSUM | NETIF_F_RXCSUM; |
83bb63f6 | 1984 | |
3d96c74d MM |
1985 | if (priv->hca_caps & IB_DEVICE_UD_TSO) |
1986 | priv->dev->hw_features |= NETIF_F_TSO; | |
8ae31e5b | 1987 | |
3d96c74d MM |
1988 | priv->dev->features |= priv->dev->hw_features; |
1989 | } | |
83bb63f6 OG |
1990 | |
1991 | return 0; | |
1992 | } | |
1993 | ||
1da177e4 LT |
1994 | static struct net_device *ipoib_add_port(const char *format, |
1995 | struct ib_device *hca, u8 port) | |
1996 | { | |
1997 | struct ipoib_dev_priv *priv; | |
bc7b3a36 | 1998 | struct ib_port_attr attr; |
1da177e4 LT |
1999 | int result = -ENOMEM; |
2000 | ||
2001 | priv = ipoib_intf_alloc(format); | |
2002 | if (!priv) | |
2003 | goto alloc_mem_failed; | |
2004 | ||
2005 | SET_NETDEV_DEV(priv->dev, hca->dma_device); | |
c3aa9b18 | 2006 | priv->dev->dev_id = port - 1; |
1da177e4 | 2007 | |
58e9cc90 AV |
2008 | result = ib_query_port(hca, port, &attr); |
2009 | if (!result) | |
bc7b3a36 SM |
2010 | priv->max_ib_mtu = ib_mtu_enum_to_int(attr.max_mtu); |
2011 | else { | |
2012 | printk(KERN_WARNING "%s: ib_query_port %d failed\n", | |
2013 | hca->name, port); | |
2014 | goto device_init_failed; | |
2015 | } | |
2016 | ||
2017 | /* MTU will be reset when mcast join happens */ | |
2018 | priv->dev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu); | |
2019 | priv->mcast_mtu = priv->admin_mtu = priv->dev->mtu; | |
2020 | ||
596b9b68 DM |
2021 | priv->dev->neigh_priv_len = sizeof(struct ipoib_neigh); |
2022 | ||
1da177e4 LT |
2023 | result = ib_query_pkey(hca, port, 0, &priv->pkey); |
2024 | if (result) { | |
2025 | printk(KERN_WARNING "%s: ib_query_pkey port %d failed (ret = %d)\n", | |
2026 | hca->name, port, result); | |
ca6de177 | 2027 | goto device_init_failed; |
1da177e4 LT |
2028 | } |
2029 | ||
58e9cc90 AV |
2030 | result = ipoib_set_dev_features(priv, hca); |
2031 | if (result) | |
6046136c | 2032 | goto device_init_failed; |
af40da89 | 2033 | |
4ce05937 RD |
2034 | /* |
2035 | * Set the full membership bit, so that we join the right | |
2036 | * broadcast group, etc. | |
2037 | */ | |
2038 | priv->pkey |= 0x8000; | |
2039 | ||
1da177e4 LT |
2040 | priv->dev->broadcast[8] = priv->pkey >> 8; |
2041 | priv->dev->broadcast[9] = priv->pkey & 0xff; | |
2042 | ||
55ee3ab2 | 2043 | result = ib_query_gid(hca, port, 0, &priv->local_gid, NULL); |
1da177e4 LT |
2044 | if (result) { |
2045 | printk(KERN_WARNING "%s: ib_query_gid port %d failed (ret = %d)\n", | |
2046 | hca->name, port, result); | |
ca6de177 | 2047 | goto device_init_failed; |
1da177e4 LT |
2048 | } else |
2049 | memcpy(priv->dev->dev_addr + 4, priv->local_gid.raw, sizeof (union ib_gid)); | |
492a7e67 | 2050 | set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags); |
1da177e4 | 2051 | |
1da177e4 LT |
2052 | result = ipoib_dev_init(priv->dev, hca, port); |
2053 | if (result < 0) { | |
2054 | printk(KERN_WARNING "%s: failed to initialize port %d (ret = %d)\n", | |
2055 | hca->name, port, result); | |
2056 | goto device_init_failed; | |
2057 | } | |
2058 | ||
2059 | INIT_IB_EVENT_HANDLER(&priv->event_handler, | |
2060 | priv->ca, ipoib_event); | |
2061 | result = ib_register_event_handler(&priv->event_handler); | |
2062 | if (result < 0) { | |
2063 | printk(KERN_WARNING "%s: ib_register_event_handler failed for " | |
2064 | "port %d (ret = %d)\n", | |
2065 | hca->name, port, result); | |
2066 | goto event_failed; | |
2067 | } | |
2068 | ||
2069 | result = register_netdev(priv->dev); | |
2070 | if (result) { | |
2071 | printk(KERN_WARNING "%s: couldn't register ipoib port %d; error %d\n", | |
2072 | hca->name, port, result); | |
2073 | goto register_failed; | |
2074 | } | |
2075 | ||
1732b0ef | 2076 | ipoib_create_debug_files(priv->dev); |
1da177e4 | 2077 | |
839fcaba MT |
2078 | if (ipoib_cm_add_mode_attr(priv->dev)) |
2079 | goto sysfs_failed; | |
1da177e4 LT |
2080 | if (ipoib_add_pkey_attr(priv->dev)) |
2081 | goto sysfs_failed; | |
335a64a5 OG |
2082 | if (ipoib_add_umcast_attr(priv->dev)) |
2083 | goto sysfs_failed; | |
43cb76d9 | 2084 | if (device_create_file(&priv->dev->dev, &dev_attr_create_child)) |
1da177e4 | 2085 | goto sysfs_failed; |
43cb76d9 | 2086 | if (device_create_file(&priv->dev->dev, &dev_attr_delete_child)) |
1da177e4 LT |
2087 | goto sysfs_failed; |
2088 | ||
2089 | return priv->dev; | |
2090 | ||
2091 | sysfs_failed: | |
1732b0ef | 2092 | ipoib_delete_debug_files(priv->dev); |
1da177e4 LT |
2093 | unregister_netdev(priv->dev); |
2094 | ||
2095 | register_failed: | |
2096 | ib_unregister_event_handler(&priv->event_handler); | |
0b39578b | 2097 | flush_workqueue(ipoib_workqueue); |
b63b70d8 SP |
2098 | /* Stop GC if started before flush */ |
2099 | set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags); | |
2100 | cancel_delayed_work(&priv->neigh_reap_task); | |
0b39578b | 2101 | flush_workqueue(priv->wq); |
1da177e4 LT |
2102 | |
2103 | event_failed: | |
2104 | ipoib_dev_cleanup(priv->dev); | |
2105 | ||
2106 | device_init_failed: | |
2107 | free_netdev(priv->dev); | |
2108 | ||
2109 | alloc_mem_failed: | |
2110 | return ERR_PTR(result); | |
2111 | } | |
2112 | ||
2113 | static void ipoib_add_one(struct ib_device *device) | |
2114 | { | |
2115 | struct list_head *dev_list; | |
2116 | struct net_device *dev; | |
2117 | struct ipoib_dev_priv *priv; | |
4139032b | 2118 | int p; |
8e37ab68 | 2119 | int count = 0; |
07ebafba | 2120 | |
1da177e4 LT |
2121 | dev_list = kmalloc(sizeof *dev_list, GFP_KERNEL); |
2122 | if (!dev_list) | |
2123 | return; | |
2124 | ||
2125 | INIT_LIST_HEAD(dev_list); | |
2126 | ||
4139032b | 2127 | for (p = rdma_start_port(device); p <= rdma_end_port(device); ++p) { |
8e37ab68 | 2128 | if (!rdma_protocol_ib(device, p)) |
7b4c8769 | 2129 | continue; |
1da177e4 LT |
2130 | dev = ipoib_add_port("ib%d", device, p); |
2131 | if (!IS_ERR(dev)) { | |
2132 | priv = netdev_priv(dev); | |
2133 | list_add_tail(&priv->list, dev_list); | |
8e37ab68 | 2134 | count++; |
1da177e4 LT |
2135 | } |
2136 | } | |
2137 | ||
8e37ab68 MW |
2138 | if (!count) { |
2139 | kfree(dev_list); | |
2140 | return; | |
2141 | } | |
2142 | ||
1da177e4 LT |
2143 | ib_set_client_data(device, &ipoib_client, dev_list); |
2144 | } | |
2145 | ||
7c1eb45a | 2146 | static void ipoib_remove_one(struct ib_device *device, void *client_data) |
1da177e4 LT |
2147 | { |
2148 | struct ipoib_dev_priv *priv, *tmp; | |
7c1eb45a | 2149 | struct list_head *dev_list = client_data; |
1da177e4 | 2150 | |
5a2815f0 IG |
2151 | if (!dev_list) |
2152 | return; | |
1da177e4 LT |
2153 | |
2154 | list_for_each_entry_safe(priv, tmp, dev_list, list) { | |
2155 | ib_unregister_event_handler(&priv->event_handler); | |
0b39578b | 2156 | flush_workqueue(ipoib_workqueue); |
a77a57a1 | 2157 | |
198b12f7 ES |
2158 | /* mark interface in the middle of destruction */ |
2159 | set_bit(IPOIB_FLAG_GOING_DOWN, &priv->flags); | |
2160 | ||
a77a57a1 RD |
2161 | rtnl_lock(); |
2162 | dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP); | |
2163 | rtnl_unlock(); | |
2164 | ||
b63b70d8 SP |
2165 | /* Stop GC */ |
2166 | set_bit(IPOIB_STOP_NEIGH_GC, &priv->flags); | |
2167 | cancel_delayed_work(&priv->neigh_reap_task); | |
0b39578b | 2168 | flush_workqueue(priv->wq); |
1da177e4 LT |
2169 | |
2170 | unregister_netdev(priv->dev); | |
1da177e4 LT |
2171 | free_netdev(priv->dev); |
2172 | } | |
06c56e44 MT |
2173 | |
2174 | kfree(dev_list); | |
1da177e4 LT |
2175 | } |
2176 | ||
2177 | static int __init ipoib_init_module(void) | |
2178 | { | |
2179 | int ret; | |
2180 | ||
0f485251 SM |
2181 | ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size); |
2182 | ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE); | |
2183 | ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE); | |
2184 | ||
2185 | ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size); | |
2186 | ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE); | |
732eacc0 | 2187 | ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE); |
68e995a2 PS |
2188 | #ifdef CONFIG_INFINIBAND_IPOIB_CM |
2189 | ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP); | |
2190 | #endif | |
0f485251 | 2191 | |
f89271da EC |
2192 | /* |
2193 | * When copying small received packets, we only copy from the | |
2194 | * linear data part of the SKB, so we rely on this condition. | |
2195 | */ | |
2196 | BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE); | |
2197 | ||
1da177e4 LT |
2198 | ret = ipoib_register_debugfs(); |
2199 | if (ret) | |
2200 | return ret; | |
2201 | ||
2202 | /* | |
0b39578b DL |
2203 | * We create a global workqueue here that is used for all flush |
2204 | * operations. However, if you attempt to flush a workqueue | |
2205 | * from a task on that same workqueue, it deadlocks the system. | |
2206 | * We want to be able to flush the tasks associated with a | |
2207 | * specific net device, so we also create a workqueue for each | |
2208 | * netdevice. We queue up the tasks for that device only on | |
2209 | * its private workqueue, and we only queue up flush events | |
2210 | * on our global flush workqueue. This avoids the deadlocks. | |
1da177e4 | 2211 | */ |
855cda68 BS |
2212 | ipoib_workqueue = alloc_ordered_workqueue("ipoib_flush", |
2213 | WQ_MEM_RECLAIM); | |
1da177e4 LT |
2214 | if (!ipoib_workqueue) { |
2215 | ret = -ENOMEM; | |
2216 | goto err_fs; | |
2217 | } | |
2218 | ||
c1a0b23b MT |
2219 | ib_sa_register_client(&ipoib_sa_client); |
2220 | ||
1da177e4 LT |
2221 | ret = ib_register_client(&ipoib_client); |
2222 | if (ret) | |
c1a0b23b | 2223 | goto err_sa; |
1da177e4 | 2224 | |
9baa0b03 OG |
2225 | ret = ipoib_netlink_init(); |
2226 | if (ret) | |
2227 | goto err_client; | |
2228 | ||
1da177e4 LT |
2229 | return 0; |
2230 | ||
9baa0b03 OG |
2231 | err_client: |
2232 | ib_unregister_client(&ipoib_client); | |
2233 | ||
c1a0b23b MT |
2234 | err_sa: |
2235 | ib_sa_unregister_client(&ipoib_sa_client); | |
1da177e4 LT |
2236 | destroy_workqueue(ipoib_workqueue); |
2237 | ||
9adec1a8 RD |
2238 | err_fs: |
2239 | ipoib_unregister_debugfs(); | |
2240 | ||
1da177e4 LT |
2241 | return ret; |
2242 | } | |
2243 | ||
2244 | static void __exit ipoib_cleanup_module(void) | |
2245 | { | |
9baa0b03 | 2246 | ipoib_netlink_fini(); |
1da177e4 | 2247 | ib_unregister_client(&ipoib_client); |
c1a0b23b | 2248 | ib_sa_unregister_client(&ipoib_sa_client); |
9adec1a8 | 2249 | ipoib_unregister_debugfs(); |
1da177e4 LT |
2250 | destroy_workqueue(ipoib_workqueue); |
2251 | } | |
2252 | ||
2253 | module_init(ipoib_init_module); | |
2254 | module_exit(ipoib_cleanup_module); |