1 /*
2 * Copyright (c) 2004 Topspin Communications. All rights reserved.
3 * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved.
4 * Copyright (c) 2004 Voltaire, Inc. All rights reserved.
5 *
6 * This software is available to you under a choice of one of two
7 * licenses. You may choose to be licensed under the terms of the GNU
8 * General Public License (GPL) Version 2, available from the file
9 * COPYING in the main directory of this source tree, or the
10 * OpenIB.org BSD license below:
11 *
12 * Redistribution and use in source and binary forms, with or
13 * without modification, are permitted provided that the following
14 * conditions are met:
15 *
16 * - Redistributions of source code must retain the above
17 * copyright notice, this list of conditions and the following
18 * disclaimer.
19 *
20 * - Redistributions in binary form must reproduce the above
21 * copyright notice, this list of conditions and the following
22 * disclaimer in the documentation and/or other materials
23 * provided with the distribution.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
29 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
30 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
31 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
32 * SOFTWARE.
33 */
34
35 #include "ipoib.h"
36
37 #include <linux/module.h>
38
39 #include <linux/init.h>
40 #include <linux/slab.h>
41 #include <linux/kernel.h>
42 #include <linux/vmalloc.h>
43
44 #include <linux/if_arp.h> /* For ARPHRD_xxx */
45
46 #include <linux/ip.h>
47 #include <linux/in.h>
48
49 #include <linux/jhash.h>
50 #include <net/arp.h>
51 #include <net/addrconf.h>
52 #include <net/netdev_lock.h>
53 #include <net/pkt_sched.h>
54 #include <linux/inetdevice.h>
55 #include <rdma/ib_cache.h>
56
57 MODULE_AUTHOR("Roland Dreier");
58 MODULE_DESCRIPTION("IP-over-InfiniBand net driver");
59 MODULE_LICENSE("Dual BSD/GPL");
60
61 int ipoib_sendq_size __read_mostly = IPOIB_TX_RING_SIZE;
62 int ipoib_recvq_size __read_mostly = IPOIB_RX_RING_SIZE;
63
64 module_param_named(send_queue_size, ipoib_sendq_size, int, 0444);
65 MODULE_PARM_DESC(send_queue_size, "Number of descriptors in send queue");
66 module_param_named(recv_queue_size, ipoib_recvq_size, int, 0444);
67 MODULE_PARM_DESC(recv_queue_size, "Number of descriptors in receive queue");
68
69 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
70 int ipoib_debug_level;
71
72 module_param_named(debug_level, ipoib_debug_level, int, 0644);
73 MODULE_PARM_DESC(debug_level, "Enable debug tracing if > 0");
74 #endif
75
76 struct ipoib_path_iter {
77 struct net_device *dev;
78 struct ipoib_path path;
79 };
80
81 static const u8 ipv4_bcast_addr[] = {
82 0x00, 0xff, 0xff, 0xff,
83 0xff, 0x12, 0x40, 0x1b, 0x00, 0x00, 0x00, 0x00,
84 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff
85 };
86
87 struct workqueue_struct *ipoib_workqueue;
88
89 struct ib_sa_client ipoib_sa_client;
90
91 static int ipoib_add_one(struct ib_device *device);
92 static void ipoib_remove_one(struct ib_device *device, void *client_data);
93 static void ipoib_neigh_reclaim(struct rcu_head *rp);
94 static struct net_device *ipoib_get_net_dev_by_params(
95 struct ib_device *dev, u32 port, u16 pkey,
96 const union ib_gid *gid, const struct sockaddr *addr,
97 void *client_data);
98 static int ipoib_set_mac(struct net_device *dev, void *addr);
99 static int ipoib_ioctl(struct net_device *dev, struct ifreq *ifr,
100 int cmd);
101
102 static struct ib_client ipoib_client = {
103 .name = "ipoib",
104 .add = ipoib_add_one,
105 .remove = ipoib_remove_one,
106 .get_net_dev_by_params = ipoib_get_net_dev_by_params,
107 };
108
109 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
ipoib_netdev_event(struct notifier_block * this,unsigned long event,void * ptr)110 static int ipoib_netdev_event(struct notifier_block *this,
111 unsigned long event, void *ptr)
112 {
113 struct netdev_notifier_info *ni = ptr;
114 struct net_device *dev = ni->dev;
115
116 if (dev->netdev_ops->ndo_open != ipoib_open)
117 return NOTIFY_DONE;
118
119 switch (event) {
120 case NETDEV_REGISTER:
121 ipoib_create_debug_files(dev);
122 break;
123 case NETDEV_CHANGENAME:
124 ipoib_delete_debug_files(dev);
125 ipoib_create_debug_files(dev);
126 break;
127 case NETDEV_UNREGISTER:
128 ipoib_delete_debug_files(dev);
129 break;
130 }
131
132 return NOTIFY_DONE;
133 }
134 #endif
135
136 struct ipoib_ifupdown_work {
137 struct work_struct work;
138 struct net_device *dev;
139 netdevice_tracker dev_tracker;
140 bool up;
141 };
142
ipoib_ifupdown_task(struct work_struct * work)143 static void ipoib_ifupdown_task(struct work_struct *work)
144 {
145 struct ipoib_ifupdown_work *pwork =
146 container_of(work, struct ipoib_ifupdown_work, work);
147 struct net_device *dev = pwork->dev;
148 unsigned int flags;
149
150 rtnl_lock();
151 flags = dev->flags;
152 if (pwork->up)
153 flags |= IFF_UP;
154 else
155 flags &= ~IFF_UP;
156
157 if (dev->flags != flags)
158 dev_change_flags(dev, flags, NULL);
159 rtnl_unlock();
160 netdev_put(dev, &pwork->dev_tracker);
161 kfree(pwork);
162 }
163
ipoib_schedule_ifupdown_task(struct net_device * dev,bool up)164 static void ipoib_schedule_ifupdown_task(struct net_device *dev, bool up)
165 {
166 struct ipoib_ifupdown_work *work;
167
168 if ((up && (dev->flags & IFF_UP)) ||
169 (!up && !(dev->flags & IFF_UP)))
170 return;
171
172 work = kmalloc_obj(*work);
173 if (!work)
174 return;
175 work->dev = dev;
176 netdev_hold(dev, &work->dev_tracker, GFP_KERNEL);
177 work->up = up;
178 INIT_WORK(&work->work, ipoib_ifupdown_task);
179 queue_work(ipoib_workqueue, &work->work);
180 }
181
ipoib_open(struct net_device * dev)182 int ipoib_open(struct net_device *dev)
183 {
184 struct ipoib_dev_priv *priv = ipoib_priv(dev);
185
186 ipoib_dbg(priv, "bringing up interface\n");
187
188 netif_carrier_off(dev);
189
190 set_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
191
192 if (ipoib_ib_dev_open(dev)) {
193 if (!test_bit(IPOIB_PKEY_ASSIGNED, &priv->flags))
194 return 0;
195 goto err_disable;
196 }
197
198 ipoib_ib_dev_up(dev);
199
200 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
201 struct ipoib_dev_priv *cpriv;
202
203 /* Bring up any child interfaces too */
204 netdev_lock_ops_to_full(dev);
205 list_for_each_entry(cpriv, &priv->child_intfs, list)
206 ipoib_schedule_ifupdown_task(cpriv->dev, true);
207 netdev_unlock_full_to_ops(dev);
208 } else if (priv->parent) {
209 struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
210
211 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &ppriv->flags))
212 ipoib_dbg(priv, "parent device %s is not up, so child device may be not functioning.\n",
213 ppriv->dev->name);
214 }
215 netif_start_queue(dev);
216
217 return 0;
218
219 err_disable:
220 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
221
222 return -EINVAL;
223 }
224
ipoib_stop(struct net_device * dev)225 static int ipoib_stop(struct net_device *dev)
226 {
227 struct ipoib_dev_priv *priv = ipoib_priv(dev);
228
229 ipoib_dbg(priv, "stopping interface\n");
230
231 clear_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags);
232
233 netif_stop_queue(dev);
234
235 ipoib_ib_dev_down(dev);
236 ipoib_ib_dev_stop(dev);
237
238 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
239 struct ipoib_dev_priv *cpriv;
240
241 /* Bring down any child interfaces too */
242 netdev_lock_ops_to_full(dev);
243 list_for_each_entry(cpriv, &priv->child_intfs, list)
244 ipoib_schedule_ifupdown_task(cpriv->dev, false);
245 netdev_unlock_full_to_ops(dev);
246 }
247
248 return 0;
249 }
250
ipoib_fix_features(struct net_device * dev,netdev_features_t features)251 static netdev_features_t ipoib_fix_features(struct net_device *dev, netdev_features_t features)
252 {
253 struct ipoib_dev_priv *priv = ipoib_priv(dev);
254
255 if (test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags))
256 features &= ~(NETIF_F_IP_CSUM | NETIF_F_TSO);
257
258 return features;
259 }
260
ipoib_change_mtu(struct net_device * dev,int new_mtu)261 static int ipoib_change_mtu(struct net_device *dev, int new_mtu)
262 {
263 struct ipoib_dev_priv *priv = ipoib_priv(dev);
264 int ret = 0;
265
266 /* dev->mtu > 2K ==> connected mode */
267 if (ipoib_cm_admin_enabled(dev)) {
268 if (new_mtu > ipoib_cm_max_mtu(dev))
269 return -EINVAL;
270
271 if (new_mtu > priv->mcast_mtu)
272 ipoib_warn(priv, "mtu > %d will cause multicast packet drops.\n",
273 priv->mcast_mtu);
274
275 WRITE_ONCE(dev->mtu, new_mtu);
276 return 0;
277 }
278
279 if (new_mtu < (ETH_MIN_MTU + IPOIB_ENCAP_LEN) ||
280 new_mtu > IPOIB_UD_MTU(priv->max_ib_mtu))
281 return -EINVAL;
282
283 priv->admin_mtu = new_mtu;
284
285 if (priv->mcast_mtu < priv->admin_mtu)
286 ipoib_dbg(priv, "MTU must be smaller than the underlying "
287 "link layer MTU - 4 (%u)\n", priv->mcast_mtu);
288
289 new_mtu = min(priv->mcast_mtu, priv->admin_mtu);
290
291 if (priv->rn_ops->ndo_change_mtu) {
292 bool carrier_status = netif_carrier_ok(dev);
293
294 netif_carrier_off(dev);
295
296 /* notify lower level on the real mtu */
297 ret = priv->rn_ops->ndo_change_mtu(dev, new_mtu);
298
299 if (carrier_status)
300 netif_carrier_on(dev);
301 } else {
302 WRITE_ONCE(dev->mtu, new_mtu);
303 }
304
305 return ret;
306 }
307
ipoib_get_stats(struct net_device * dev,struct rtnl_link_stats64 * stats)308 static void ipoib_get_stats(struct net_device *dev,
309 struct rtnl_link_stats64 *stats)
310 {
311 struct ipoib_dev_priv *priv = ipoib_priv(dev);
312
313 if (priv->rn_ops->ndo_get_stats64)
314 priv->rn_ops->ndo_get_stats64(dev, stats);
315 else
316 netdev_stats_to_stats64(stats, &dev->stats);
317 }
318
319 /* Called with an RCU read lock taken */
ipoib_is_dev_match_addr_rcu(const struct sockaddr * addr,struct net_device * dev)320 static bool ipoib_is_dev_match_addr_rcu(const struct sockaddr *addr,
321 struct net_device *dev)
322 {
323 struct net *net = dev_net(dev);
324 struct in_device *in_dev;
325 struct sockaddr_in *addr_in = (struct sockaddr_in *)addr;
326 struct sockaddr_in6 *addr_in6 = (struct sockaddr_in6 *)addr;
327 __be32 ret_addr;
328
329 switch (addr->sa_family) {
330 case AF_INET:
331 in_dev = in_dev_get(dev);
332 if (!in_dev)
333 return false;
334
335 ret_addr = inet_confirm_addr(net, in_dev, 0,
336 addr_in->sin_addr.s_addr,
337 RT_SCOPE_HOST);
338 in_dev_put(in_dev);
339 if (ret_addr)
340 return true;
341
342 break;
343 case AF_INET6:
344 if (IS_ENABLED(CONFIG_IPV6) &&
345 ipv6_chk_addr(net, &addr_in6->sin6_addr, dev, 1))
346 return true;
347
348 break;
349 }
350 return false;
351 }
352
353 /*
354 * Find the L2 master net_device on top of the given net_device.
355 * @dev: base IPoIB net_device
356 *
357 * Returns the L2 master net_device with reference held if the L2 master
358 * exists (such as bond netdevice), or returns same netdev with reference
359 * held when master does not exist or when L3 master (such as VRF netdev).
360 */
ipoib_get_master_net_dev(struct net_device * dev)361 static struct net_device *ipoib_get_master_net_dev(struct net_device *dev)
362 {
363 struct net_device *master;
364
365 rcu_read_lock();
366
367 master = netdev_master_upper_dev_get_rcu(dev);
368 if (!master || netif_is_l3_master(master))
369 master = dev;
370
371 dev_hold(master);
372 rcu_read_unlock();
373
374 return master;
375 }
376
377 struct ipoib_walk_data {
378 const struct sockaddr *addr;
379 struct net_device *result;
380 };
381
ipoib_upper_walk(struct net_device * upper,struct netdev_nested_priv * priv)382 static int ipoib_upper_walk(struct net_device *upper,
383 struct netdev_nested_priv *priv)
384 {
385 struct ipoib_walk_data *data = (struct ipoib_walk_data *)priv->data;
386 int ret = 0;
387
388 if (ipoib_is_dev_match_addr_rcu(data->addr, upper)) {
389 dev_hold(upper);
390 data->result = upper;
391 ret = 1;
392 }
393
394 return ret;
395 }
396
397 /**
398 * ipoib_get_net_dev_match_addr - Find a net_device matching
399 * the given address, which is an upper device of the given net_device.
400 *
401 * @addr: IP address to look for.
402 * @dev: base IPoIB net_device
403 *
404 * If found, returns the net_device with a reference held. Otherwise return
405 * NULL.
406 */
ipoib_get_net_dev_match_addr(const struct sockaddr * addr,struct net_device * dev)407 static struct net_device *ipoib_get_net_dev_match_addr(
408 const struct sockaddr *addr, struct net_device *dev)
409 {
410 struct netdev_nested_priv priv;
411 struct ipoib_walk_data data = {
412 .addr = addr,
413 };
414
415 priv.data = (void *)&data;
416 rcu_read_lock();
417 if (ipoib_is_dev_match_addr_rcu(addr, dev)) {
418 dev_hold(dev);
419 data.result = dev;
420 goto out;
421 }
422
423 netdev_walk_all_upper_dev_rcu(dev, ipoib_upper_walk, &priv);
424 out:
425 rcu_read_unlock();
426 return data.result;
427 }
428
429 /* returns the number of IPoIB netdevs on top a given ipoib device matching a
430 * pkey_index and address, if one exists.
431 *
432 * @found_net_dev: contains a matching net_device if the return value >= 1,
433 * with a reference held. */
ipoib_match_gid_pkey_addr(struct ipoib_dev_priv * priv,const union ib_gid * gid,u16 pkey_index,const struct sockaddr * addr,int nesting,struct net_device ** found_net_dev)434 static int ipoib_match_gid_pkey_addr(struct ipoib_dev_priv *priv,
435 const union ib_gid *gid,
436 u16 pkey_index,
437 const struct sockaddr *addr,
438 int nesting,
439 struct net_device **found_net_dev)
440 {
441 struct ipoib_dev_priv *child_priv;
442 struct net_device *net_dev = NULL;
443 int matches = 0;
444
445 if (priv->pkey_index == pkey_index &&
446 (!gid || !memcmp(gid, &priv->local_gid, sizeof(*gid)))) {
447 if (!addr) {
448 net_dev = ipoib_get_master_net_dev(priv->dev);
449 } else {
450 /* Verify the net_device matches the IP address, as
451 * IPoIB child devices currently share a GID. */
452 net_dev = ipoib_get_net_dev_match_addr(addr, priv->dev);
453 }
454 if (net_dev) {
455 if (!*found_net_dev)
456 *found_net_dev = net_dev;
457 else
458 dev_put(net_dev);
459 ++matches;
460 }
461 }
462
463 if (test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
464 return matches;
465
466 /* Check child interfaces */
467 netdev_lock(priv->dev);
468 list_for_each_entry(child_priv, &priv->child_intfs, list) {
469 matches += ipoib_match_gid_pkey_addr(child_priv, gid,
470 pkey_index, addr,
471 nesting + 1,
472 found_net_dev);
473 if (matches > 1)
474 break;
475 }
476 netdev_unlock(priv->dev);
477
478 return matches;
479 }
480
481 /* Returns the number of matching net_devs found (between 0 and 2). Also
482 * return the matching net_device in the @net_dev parameter, holding a
483 * reference to the net_device, if the number of matches >= 1 */
__ipoib_get_net_dev_by_params(struct list_head * dev_list,u32 port,u16 pkey_index,const union ib_gid * gid,const struct sockaddr * addr,struct net_device ** net_dev)484 static int __ipoib_get_net_dev_by_params(struct list_head *dev_list, u32 port,
485 u16 pkey_index,
486 const union ib_gid *gid,
487 const struct sockaddr *addr,
488 struct net_device **net_dev)
489 {
490 struct ipoib_dev_priv *priv;
491 int matches = 0;
492
493 *net_dev = NULL;
494
495 list_for_each_entry(priv, dev_list, list) {
496 if (priv->port != port)
497 continue;
498
499 matches += ipoib_match_gid_pkey_addr(priv, gid, pkey_index,
500 addr, 0, net_dev);
501 if (matches > 1)
502 break;
503 }
504
505 return matches;
506 }
507
ipoib_get_net_dev_by_params(struct ib_device * dev,u32 port,u16 pkey,const union ib_gid * gid,const struct sockaddr * addr,void * client_data)508 static struct net_device *ipoib_get_net_dev_by_params(
509 struct ib_device *dev, u32 port, u16 pkey,
510 const union ib_gid *gid, const struct sockaddr *addr,
511 void *client_data)
512 {
513 struct net_device *net_dev;
514 struct list_head *dev_list = client_data;
515 u16 pkey_index;
516 int matches;
517 int ret;
518
519 if (!rdma_protocol_ib(dev, port))
520 return NULL;
521
522 ret = ib_find_cached_pkey(dev, port, pkey, &pkey_index);
523 if (ret)
524 return NULL;
525
526 /* See if we can find a unique device matching the pkey and GID */
527 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
528 gid, NULL, &net_dev);
529
530 switch (matches) {
531 case 0:
532 return NULL;
533 case 1:
534 return net_dev;
535 }
536
537 dev_put(net_dev);
538
539 /* Couldn't find a unique device with pkey and GID only. Use L3
540 * address to uniquely match the net device */
541 matches = __ipoib_get_net_dev_by_params(dev_list, port, pkey_index,
542 gid, addr, &net_dev);
543 switch (matches) {
544 case 0:
545 return NULL;
546 default:
547 dev_warn_ratelimited(&dev->dev,
548 "duplicate IP address detected\n");
549 fallthrough;
550 case 1:
551 return net_dev;
552 }
553 }
554
ipoib_set_mode(struct net_device * dev,const char * buf)555 int ipoib_set_mode(struct net_device *dev, const char *buf)
556 {
557 struct ipoib_dev_priv *priv = ipoib_priv(dev);
558
559 if ((test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags) &&
560 !strcmp(buf, "connected\n")) ||
561 (!test_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags) &&
562 !strcmp(buf, "datagram\n"))) {
563 return 0;
564 }
565
566 /* flush paths if we switch modes so that connections are restarted */
567 if (IPOIB_CM_SUPPORTED(dev->dev_addr) && !strcmp(buf, "connected\n")) {
568 set_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
569 ipoib_warn(priv, "enabling connected mode "
570 "will cause multicast packet drops\n");
571 netdev_lock_ops(dev);
572 netdev_update_features(dev);
573 netif_set_mtu(dev, ipoib_cm_max_mtu(dev));
574 netif_set_real_num_tx_queues(dev, 1);
575 netdev_unlock_ops(dev);
576 rtnl_unlock();
577 priv->tx_wr.wr.send_flags &= ~IB_SEND_IP_CSUM;
578
579 ipoib_flush_paths(dev);
580 return (!rtnl_trylock()) ? -EBUSY : 0;
581 }
582
583 if (!strcmp(buf, "datagram\n")) {
584 clear_bit(IPOIB_FLAG_ADMIN_CM, &priv->flags);
585 netdev_lock_ops(dev);
586 netdev_update_features(dev);
587 netif_set_mtu(dev, min(priv->mcast_mtu, dev->mtu));
588 netif_set_real_num_tx_queues(dev, dev->num_tx_queues);
589 netdev_unlock_ops(dev);
590 rtnl_unlock();
591 ipoib_flush_paths(dev);
592 return (!rtnl_trylock()) ? -EBUSY : 0;
593 }
594
595 return -EINVAL;
596 }
597
__path_find(struct net_device * dev,void * gid)598 struct ipoib_path *__path_find(struct net_device *dev, void *gid)
599 {
600 struct ipoib_dev_priv *priv = ipoib_priv(dev);
601 struct rb_node *n = priv->path_tree.rb_node;
602 struct ipoib_path *path;
603 int ret;
604
605 while (n) {
606 path = rb_entry(n, struct ipoib_path, rb_node);
607
608 ret = memcmp(gid, path->pathrec.dgid.raw,
609 sizeof (union ib_gid));
610
611 if (ret < 0)
612 n = n->rb_left;
613 else if (ret > 0)
614 n = n->rb_right;
615 else
616 return path;
617 }
618
619 return NULL;
620 }
621
__path_add(struct net_device * dev,struct ipoib_path * path)622 static int __path_add(struct net_device *dev, struct ipoib_path *path)
623 {
624 struct ipoib_dev_priv *priv = ipoib_priv(dev);
625 struct rb_node **n = &priv->path_tree.rb_node;
626 struct rb_node *pn = NULL;
627 struct ipoib_path *tpath;
628 int ret;
629
630 while (*n) {
631 pn = *n;
632 tpath = rb_entry(pn, struct ipoib_path, rb_node);
633
634 ret = memcmp(path->pathrec.dgid.raw, tpath->pathrec.dgid.raw,
635 sizeof (union ib_gid));
636 if (ret < 0)
637 n = &pn->rb_left;
638 else if (ret > 0)
639 n = &pn->rb_right;
640 else
641 return -EEXIST;
642 }
643
644 rb_link_node(&path->rb_node, pn, n);
645 rb_insert_color(&path->rb_node, &priv->path_tree);
646
647 list_add_tail(&path->list, &priv->path_list);
648
649 return 0;
650 }
651
path_free(struct net_device * dev,struct ipoib_path * path)652 static void path_free(struct net_device *dev, struct ipoib_path *path)
653 {
654 struct sk_buff *skb;
655
656 while ((skb = __skb_dequeue(&path->queue)))
657 dev_kfree_skb_irq(skb);
658
659 ipoib_dbg(ipoib_priv(dev), "%s\n", __func__);
660
661 /* remove all neigh connected to this path */
662 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
663
664 if (path->ah)
665 ipoib_put_ah(path->ah);
666
667 kfree(path);
668 }
669
670 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
671
ipoib_path_iter_init(struct net_device * dev)672 struct ipoib_path_iter *ipoib_path_iter_init(struct net_device *dev)
673 {
674 struct ipoib_path_iter *iter;
675
676 iter = kmalloc_obj(*iter);
677 if (!iter)
678 return NULL;
679
680 iter->dev = dev;
681 memset(iter->path.pathrec.dgid.raw, 0, 16);
682
683 if (ipoib_path_iter_next(iter)) {
684 kfree(iter);
685 return NULL;
686 }
687
688 return iter;
689 }
690
ipoib_path_iter_next(struct ipoib_path_iter * iter)691 int ipoib_path_iter_next(struct ipoib_path_iter *iter)
692 {
693 struct ipoib_dev_priv *priv = ipoib_priv(iter->dev);
694 struct rb_node *n;
695 struct ipoib_path *path;
696 int ret = 1;
697
698 spin_lock_irq(&priv->lock);
699
700 n = rb_first(&priv->path_tree);
701
702 while (n) {
703 path = rb_entry(n, struct ipoib_path, rb_node);
704
705 if (memcmp(iter->path.pathrec.dgid.raw, path->pathrec.dgid.raw,
706 sizeof (union ib_gid)) < 0) {
707 iter->path = *path;
708 ret = 0;
709 break;
710 }
711
712 n = rb_next(n);
713 }
714
715 spin_unlock_irq(&priv->lock);
716
717 return ret;
718 }
719
ipoib_path_iter_read(struct ipoib_path_iter * iter,struct ipoib_path * path)720 void ipoib_path_iter_read(struct ipoib_path_iter *iter,
721 struct ipoib_path *path)
722 {
723 *path = iter->path;
724 }
725
726 #endif /* CONFIG_INFINIBAND_IPOIB_DEBUG */
727
ipoib_mark_paths_invalid(struct net_device * dev)728 void ipoib_mark_paths_invalid(struct net_device *dev)
729 {
730 struct ipoib_dev_priv *priv = ipoib_priv(dev);
731 struct ipoib_path *path, *tp;
732
733 spin_lock_irq(&priv->lock);
734
735 list_for_each_entry_safe(path, tp, &priv->path_list, list) {
736 ipoib_dbg(priv, "mark path LID 0x%08x GID %pI6 invalid\n",
737 be32_to_cpu(sa_path_get_dlid(&path->pathrec)),
738 path->pathrec.dgid.raw);
739 if (path->ah)
740 path->ah->valid = 0;
741 }
742
743 spin_unlock_irq(&priv->lock);
744 }
745
push_pseudo_header(struct sk_buff * skb,const char * daddr)746 static void push_pseudo_header(struct sk_buff *skb, const char *daddr)
747 {
748 struct ipoib_pseudo_header *phdr;
749
750 phdr = skb_push(skb, sizeof(*phdr));
751 memcpy(phdr->hwaddr, daddr, INFINIBAND_ALEN);
752 }
753
ipoib_flush_paths(struct net_device * dev)754 void ipoib_flush_paths(struct net_device *dev)
755 {
756 struct ipoib_dev_priv *priv = ipoib_priv(dev);
757 struct ipoib_path *path, *tp;
758 LIST_HEAD(remove_list);
759 unsigned long flags;
760
761 netif_tx_lock_bh(dev);
762 spin_lock_irqsave(&priv->lock, flags);
763
764 list_splice_init(&priv->path_list, &remove_list);
765
766 list_for_each_entry(path, &remove_list, list)
767 rb_erase(&path->rb_node, &priv->path_tree);
768
769 list_for_each_entry_safe(path, tp, &remove_list, list) {
770 if (path->query)
771 ib_sa_cancel_query(path->query_id, path->query);
772 spin_unlock_irqrestore(&priv->lock, flags);
773 netif_tx_unlock_bh(dev);
774 wait_for_completion(&path->done);
775 path_free(dev, path);
776 netif_tx_lock_bh(dev);
777 spin_lock_irqsave(&priv->lock, flags);
778 }
779
780 spin_unlock_irqrestore(&priv->lock, flags);
781 netif_tx_unlock_bh(dev);
782 }
783
path_rec_completion(int status,struct sa_path_rec * pathrec,unsigned int num_prs,void * path_ptr)784 static void path_rec_completion(int status,
785 struct sa_path_rec *pathrec,
786 unsigned int num_prs, void *path_ptr)
787 {
788 struct ipoib_path *path = path_ptr;
789 struct net_device *dev = path->dev;
790 struct ipoib_dev_priv *priv = ipoib_priv(dev);
791 struct ipoib_ah *ah = NULL;
792 struct ipoib_ah *old_ah = NULL;
793 struct ipoib_neigh *neigh, *tn;
794 struct sk_buff_head skqueue;
795 struct sk_buff *skb;
796 unsigned long flags;
797
798 if (!status)
799 ipoib_dbg(priv, "PathRec LID 0x%04x for GID %pI6\n",
800 be32_to_cpu(sa_path_get_dlid(pathrec)),
801 pathrec->dgid.raw);
802 else
803 ipoib_dbg(priv, "PathRec status %d for GID %pI6\n",
804 status, path->pathrec.dgid.raw);
805
806 skb_queue_head_init(&skqueue);
807
808 if (!status) {
809 struct rdma_ah_attr av;
810
811 if (!ib_init_ah_attr_from_path(priv->ca, priv->port,
812 pathrec, &av, NULL)) {
813 ah = ipoib_create_ah(dev, priv->pd, &av);
814 rdma_destroy_ah_attr(&av);
815 }
816 }
817
818 spin_lock_irqsave(&priv->lock, flags);
819
820 if (!IS_ERR_OR_NULL(ah)) {
821 /*
822 * pathrec.dgid is used as the database key from the LLADDR,
823 * it must remain unchanged even if the SA returns a different
824 * GID to use in the AH.
825 */
826 if (memcmp(pathrec->dgid.raw, path->pathrec.dgid.raw,
827 sizeof(union ib_gid))) {
828 ipoib_dbg(
829 priv,
830 "%s got PathRec for gid %pI6 while asked for %pI6\n",
831 dev->name, pathrec->dgid.raw,
832 path->pathrec.dgid.raw);
833 memcpy(pathrec->dgid.raw, path->pathrec.dgid.raw,
834 sizeof(union ib_gid));
835 }
836
837 path->pathrec = *pathrec;
838
839 old_ah = path->ah;
840 path->ah = ah;
841
842 ipoib_dbg(priv, "created address handle %p for LID 0x%04x, SL %d\n",
843 ah, be32_to_cpu(sa_path_get_dlid(pathrec)),
844 pathrec->sl);
845
846 while ((skb = __skb_dequeue(&path->queue)))
847 __skb_queue_tail(&skqueue, skb);
848
849 list_for_each_entry_safe(neigh, tn, &path->neigh_list, list) {
850 if (neigh->ah) {
851 WARN_ON(neigh->ah != old_ah);
852 /*
853 * Dropping the ah reference inside
854 * priv->lock is safe here, because we
855 * will hold one more reference from
856 * the original value of path->ah (ie
857 * old_ah).
858 */
859 ipoib_put_ah(neigh->ah);
860 }
861 kref_get(&path->ah->ref);
862 neigh->ah = path->ah;
863
864 if (ipoib_cm_enabled(dev, neigh->daddr)) {
865 if (!ipoib_cm_get(neigh))
866 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev,
867 path,
868 neigh));
869 if (!ipoib_cm_get(neigh)) {
870 ipoib_neigh_free(neigh);
871 continue;
872 }
873 }
874
875 while ((skb = __skb_dequeue(&neigh->queue)))
876 __skb_queue_tail(&skqueue, skb);
877 }
878 path->ah->valid = 1;
879 }
880
881 path->query = NULL;
882 complete(&path->done);
883
884 spin_unlock_irqrestore(&priv->lock, flags);
885
886 if (IS_ERR_OR_NULL(ah))
887 ipoib_del_neighs_by_gid(dev, path->pathrec.dgid.raw);
888
889 if (old_ah)
890 ipoib_put_ah(old_ah);
891
892 while ((skb = __skb_dequeue(&skqueue))) {
893 int ret;
894 skb->dev = dev;
895 ret = dev_queue_xmit(skb);
896 if (ret)
897 ipoib_warn(priv, "%s: dev_queue_xmit failed to re-queue packet, ret:%d\n",
898 __func__, ret);
899 }
900 }
901
init_path_rec(struct ipoib_dev_priv * priv,struct ipoib_path * path,void * gid)902 static void init_path_rec(struct ipoib_dev_priv *priv, struct ipoib_path *path,
903 void *gid)
904 {
905 path->dev = priv->dev;
906
907 if (rdma_cap_opa_ah(priv->ca, priv->port))
908 path->pathrec.rec_type = SA_PATH_REC_TYPE_OPA;
909 else
910 path->pathrec.rec_type = SA_PATH_REC_TYPE_IB;
911
912 memcpy(path->pathrec.dgid.raw, gid, sizeof(union ib_gid));
913 path->pathrec.sgid = priv->local_gid;
914 path->pathrec.pkey = cpu_to_be16(priv->pkey);
915 path->pathrec.numb_path = 1;
916 path->pathrec.traffic_class = priv->broadcast->mcmember.traffic_class;
917 }
918
path_rec_create(struct net_device * dev,void * gid)919 static struct ipoib_path *path_rec_create(struct net_device *dev, void *gid)
920 {
921 struct ipoib_dev_priv *priv = ipoib_priv(dev);
922 struct ipoib_path *path;
923
924 if (!priv->broadcast)
925 return NULL;
926
927 path = kzalloc_obj(*path, GFP_ATOMIC);
928 if (!path)
929 return NULL;
930
931 skb_queue_head_init(&path->queue);
932
933 INIT_LIST_HEAD(&path->neigh_list);
934
935 init_path_rec(priv, path, gid);
936
937 return path;
938 }
939
path_rec_start(struct net_device * dev,struct ipoib_path * path)940 static int path_rec_start(struct net_device *dev,
941 struct ipoib_path *path)
942 {
943 struct ipoib_dev_priv *priv = ipoib_priv(dev);
944
945 ipoib_dbg(priv, "Start path record lookup for %pI6\n",
946 path->pathrec.dgid.raw);
947
948 init_completion(&path->done);
949
950 path->query_id =
951 ib_sa_path_rec_get(&ipoib_sa_client, priv->ca, priv->port,
952 &path->pathrec,
953 IB_SA_PATH_REC_DGID |
954 IB_SA_PATH_REC_SGID |
955 IB_SA_PATH_REC_NUMB_PATH |
956 IB_SA_PATH_REC_TRAFFIC_CLASS |
957 IB_SA_PATH_REC_PKEY,
958 1000, GFP_ATOMIC,
959 path_rec_completion,
960 path, &path->query);
961 if (path->query_id < 0) {
962 ipoib_warn(priv, "ib_sa_path_rec_get failed: %d\n", path->query_id);
963 path->query = NULL;
964 complete(&path->done);
965 return path->query_id;
966 }
967
968 return 0;
969 }
970
neigh_refresh_path(struct ipoib_neigh * neigh,u8 * daddr,struct net_device * dev)971 static void neigh_refresh_path(struct ipoib_neigh *neigh, u8 *daddr,
972 struct net_device *dev)
973 {
974 struct ipoib_dev_priv *priv = ipoib_priv(dev);
975 struct ipoib_path *path;
976 unsigned long flags;
977
978 spin_lock_irqsave(&priv->lock, flags);
979
980 path = __path_find(dev, daddr + 4);
981 if (!path)
982 goto out;
983 if (!path->query)
984 path_rec_start(dev, path);
985 out:
986 spin_unlock_irqrestore(&priv->lock, flags);
987 }
988
neigh_add_path(struct sk_buff * skb,u8 * daddr,struct net_device * dev)989 static struct ipoib_neigh *neigh_add_path(struct sk_buff *skb, u8 *daddr,
990 struct net_device *dev)
991 {
992 struct ipoib_dev_priv *priv = ipoib_priv(dev);
993 struct rdma_netdev *rn = netdev_priv(dev);
994 struct ipoib_path *path;
995 struct ipoib_neigh *neigh;
996 unsigned long flags;
997
998 spin_lock_irqsave(&priv->lock, flags);
999 neigh = ipoib_neigh_alloc(daddr, dev);
1000 if (!neigh) {
1001 spin_unlock_irqrestore(&priv->lock, flags);
1002 ++dev->stats.tx_dropped;
1003 dev_kfree_skb_any(skb);
1004 return NULL;
1005 }
1006
1007 /* To avoid race condition, make sure that the
1008 * neigh will be added only once.
1009 */
1010 if (unlikely(!list_empty(&neigh->list))) {
1011 spin_unlock_irqrestore(&priv->lock, flags);
1012 return neigh;
1013 }
1014
1015 path = __path_find(dev, daddr + 4);
1016 if (!path) {
1017 path = path_rec_create(dev, daddr + 4);
1018 if (!path)
1019 goto err_path;
1020
1021 __path_add(dev, path);
1022 }
1023
1024 list_add_tail(&neigh->list, &path->neigh_list);
1025
1026 if (path->ah && path->ah->valid) {
1027 kref_get(&path->ah->ref);
1028 neigh->ah = path->ah;
1029
1030 if (ipoib_cm_enabled(dev, neigh->daddr)) {
1031 if (!ipoib_cm_get(neigh))
1032 ipoib_cm_set(neigh, ipoib_cm_create_tx(dev, path, neigh));
1033 if (!ipoib_cm_get(neigh)) {
1034 ipoib_neigh_free(neigh);
1035 goto err_drop;
1036 }
1037 if (skb_queue_len(&neigh->queue) <
1038 IPOIB_MAX_PATH_REC_QUEUE) {
1039 push_pseudo_header(skb, neigh->daddr);
1040 __skb_queue_tail(&neigh->queue, skb);
1041 } else {
1042 ipoib_warn(priv, "queue length limit %d. Packet drop.\n",
1043 skb_queue_len(&neigh->queue));
1044 goto err_drop;
1045 }
1046 } else {
1047 spin_unlock_irqrestore(&priv->lock, flags);
1048 path->ah->last_send = rn->send(dev, skb, path->ah->ah,
1049 IPOIB_QPN(daddr));
1050 ipoib_neigh_put(neigh);
1051 return NULL;
1052 }
1053 } else {
1054 neigh->ah = NULL;
1055
1056 if (!path->query && path_rec_start(dev, path))
1057 goto err_path;
1058 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1059 push_pseudo_header(skb, neigh->daddr);
1060 __skb_queue_tail(&neigh->queue, skb);
1061 } else {
1062 goto err_drop;
1063 }
1064 }
1065
1066 spin_unlock_irqrestore(&priv->lock, flags);
1067 ipoib_neigh_put(neigh);
1068 return NULL;
1069
1070 err_path:
1071 ipoib_neigh_free(neigh);
1072 err_drop:
1073 ++dev->stats.tx_dropped;
1074 dev_kfree_skb_any(skb);
1075
1076 spin_unlock_irqrestore(&priv->lock, flags);
1077 ipoib_neigh_put(neigh);
1078
1079 return NULL;
1080 }
1081
unicast_arp_send(struct sk_buff * skb,struct net_device * dev,struct ipoib_pseudo_header * phdr)1082 static void unicast_arp_send(struct sk_buff *skb, struct net_device *dev,
1083 struct ipoib_pseudo_header *phdr)
1084 {
1085 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1086 struct rdma_netdev *rn = netdev_priv(dev);
1087 struct ipoib_path *path;
1088 unsigned long flags;
1089
1090 spin_lock_irqsave(&priv->lock, flags);
1091
1092 /* no broadcast means that all paths are (going to be) not valid */
1093 if (!priv->broadcast)
1094 goto drop_and_unlock;
1095
1096 path = __path_find(dev, phdr->hwaddr + 4);
1097 if (!path || !path->ah || !path->ah->valid) {
1098 if (!path) {
1099 path = path_rec_create(dev, phdr->hwaddr + 4);
1100 if (!path)
1101 goto drop_and_unlock;
1102 __path_add(dev, path);
1103 } else {
1104 /*
1105 * make sure there are no changes in the existing
1106 * path record
1107 */
1108 init_path_rec(priv, path, phdr->hwaddr + 4);
1109 }
1110 if (!path->query && path_rec_start(dev, path)) {
1111 goto drop_and_unlock;
1112 }
1113
1114 if (skb_queue_len(&path->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1115 push_pseudo_header(skb, phdr->hwaddr);
1116 __skb_queue_tail(&path->queue, skb);
1117 goto unlock;
1118 } else {
1119 goto drop_and_unlock;
1120 }
1121 }
1122
1123 spin_unlock_irqrestore(&priv->lock, flags);
1124 ipoib_dbg(priv, "Send unicast ARP to %08x\n",
1125 be32_to_cpu(sa_path_get_dlid(&path->pathrec)));
1126 path->ah->last_send = rn->send(dev, skb, path->ah->ah,
1127 IPOIB_QPN(phdr->hwaddr));
1128 return;
1129
1130 drop_and_unlock:
1131 ++dev->stats.tx_dropped;
1132 dev_kfree_skb_any(skb);
1133 unlock:
1134 spin_unlock_irqrestore(&priv->lock, flags);
1135 }
1136
ipoib_start_xmit(struct sk_buff * skb,struct net_device * dev)1137 static netdev_tx_t ipoib_start_xmit(struct sk_buff *skb, struct net_device *dev)
1138 {
1139 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1140 struct rdma_netdev *rn = netdev_priv(dev);
1141 struct ipoib_neigh *neigh;
1142 struct ipoib_pseudo_header *phdr;
1143 struct ipoib_header *header;
1144 unsigned long flags;
1145
1146 phdr = (struct ipoib_pseudo_header *) skb->data;
1147 skb_pull(skb, sizeof(*phdr));
1148 header = (struct ipoib_header *) skb->data;
1149
1150 if (unlikely(phdr->hwaddr[4] == 0xff)) {
1151 /* multicast, arrange "if" according to probability */
1152 if ((header->proto != htons(ETH_P_IP)) &&
1153 (header->proto != htons(ETH_P_IPV6)) &&
1154 (header->proto != htons(ETH_P_ARP)) &&
1155 (header->proto != htons(ETH_P_RARP)) &&
1156 (header->proto != htons(ETH_P_TIPC))) {
1157 /* ethertype not supported by IPoIB */
1158 ++dev->stats.tx_dropped;
1159 dev_kfree_skb_any(skb);
1160 return NETDEV_TX_OK;
1161 }
1162 /* Add in the P_Key for multicast*/
1163 phdr->hwaddr[8] = (priv->pkey >> 8) & 0xff;
1164 phdr->hwaddr[9] = priv->pkey & 0xff;
1165
1166 neigh = ipoib_neigh_get(dev, phdr->hwaddr);
1167 if (likely(neigh))
1168 goto send_using_neigh;
1169 ipoib_mcast_send(dev, phdr->hwaddr, skb);
1170 return NETDEV_TX_OK;
1171 }
1172
1173 /* unicast, arrange "switch" according to probability */
1174 switch (header->proto) {
1175 case htons(ETH_P_IP):
1176 case htons(ETH_P_IPV6):
1177 case htons(ETH_P_TIPC):
1178 neigh = ipoib_neigh_get(dev, phdr->hwaddr);
1179 if (unlikely(!neigh)) {
1180 neigh = neigh_add_path(skb, phdr->hwaddr, dev);
1181 if (likely(!neigh))
1182 return NETDEV_TX_OK;
1183 }
1184 break;
1185 case htons(ETH_P_ARP):
1186 case htons(ETH_P_RARP):
1187 /* for unicast ARP and RARP should always perform path find */
1188 unicast_arp_send(skb, dev, phdr);
1189 return NETDEV_TX_OK;
1190 default:
1191 /* ethertype not supported by IPoIB */
1192 ++dev->stats.tx_dropped;
1193 dev_kfree_skb_any(skb);
1194 return NETDEV_TX_OK;
1195 }
1196
1197 send_using_neigh:
1198 /* note we now hold a ref to neigh */
1199 if (ipoib_cm_get(neigh)) {
1200 if (ipoib_cm_up(neigh)) {
1201 ipoib_cm_send(dev, skb, ipoib_cm_get(neigh));
1202 goto unref;
1203 }
1204 } else if (neigh->ah && neigh->ah->valid) {
1205 neigh->ah->last_send = rn->send(dev, skb, neigh->ah->ah,
1206 IPOIB_QPN(phdr->hwaddr));
1207 goto unref;
1208 } else if (neigh->ah) {
1209 neigh_refresh_path(neigh, phdr->hwaddr, dev);
1210 }
1211
1212 if (skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE) {
1213 push_pseudo_header(skb, phdr->hwaddr);
1214 spin_lock_irqsave(&priv->lock, flags);
1215 __skb_queue_tail(&neigh->queue, skb);
1216 spin_unlock_irqrestore(&priv->lock, flags);
1217 } else {
1218 ++dev->stats.tx_dropped;
1219 dev_kfree_skb_any(skb);
1220 }
1221
1222 unref:
1223 ipoib_neigh_put(neigh);
1224
1225 return NETDEV_TX_OK;
1226 }
1227
ipoib_timeout(struct net_device * dev,unsigned int txqueue)1228 static void ipoib_timeout(struct net_device *dev, unsigned int txqueue)
1229 {
1230 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1231 struct rdma_netdev *rn = netdev_priv(dev);
1232
1233 if (rn->tx_timeout) {
1234 rn->tx_timeout(dev, txqueue);
1235 return;
1236 }
1237 ipoib_warn(priv, "transmit timeout: latency %d msecs\n",
1238 jiffies_to_msecs(jiffies - dev_trans_start(dev)));
1239 ipoib_warn(priv,
1240 "queue stopped %d, tx_head %u, tx_tail %u, global_tx_head %u, global_tx_tail %u\n",
1241 netif_queue_stopped(dev), priv->tx_head, priv->tx_tail,
1242 priv->global_tx_head, priv->global_tx_tail);
1243
1244
1245 schedule_work(&priv->tx_timeout_work);
1246 }
1247
ipoib_ib_tx_timeout_work(struct work_struct * work)1248 void ipoib_ib_tx_timeout_work(struct work_struct *work)
1249 {
1250 struct ipoib_dev_priv *priv = container_of(work,
1251 struct ipoib_dev_priv,
1252 tx_timeout_work);
1253 int err;
1254
1255 rtnl_lock();
1256 netdev_lock_ops(priv->dev);
1257
1258 if (!test_bit(IPOIB_FLAG_ADMIN_UP, &priv->flags))
1259 goto unlock;
1260
1261 ipoib_stop(priv->dev);
1262 err = ipoib_open(priv->dev);
1263 if (err) {
1264 ipoib_warn(priv, "ipoib_open failed recovering from a tx_timeout, err(%d).\n",
1265 err);
1266 goto unlock;
1267 }
1268
1269 netif_tx_wake_all_queues(priv->dev);
1270 unlock:
1271 netdev_unlock_ops(priv->dev);
1272 rtnl_unlock();
1273
1274 }
1275
ipoib_hard_header(struct sk_buff * skb,struct net_device * dev,unsigned short type,const void * daddr,const void * saddr,unsigned int len)1276 static int ipoib_hard_header(struct sk_buff *skb,
1277 struct net_device *dev,
1278 unsigned short type,
1279 const void *daddr,
1280 const void *saddr,
1281 unsigned int len)
1282 {
1283 struct ipoib_header *header;
1284
1285 header = skb_push(skb, sizeof(*header));
1286
1287 header->proto = htons(type);
1288 header->reserved = 0;
1289
1290 /*
1291 * we don't rely on dst_entry structure, always stuff the
1292 * destination address into skb hard header so we can figure out where
1293 * to send the packet later.
1294 */
1295 push_pseudo_header(skb, daddr);
1296
1297 return IPOIB_HARD_LEN;
1298 }
1299
ipoib_set_rx_mode_async(struct net_device * dev,struct netdev_hw_addr_list * uc,struct netdev_hw_addr_list * mc)1300 static void ipoib_set_rx_mode_async(struct net_device *dev,
1301 struct netdev_hw_addr_list *uc,
1302 struct netdev_hw_addr_list *mc)
1303 {
1304 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1305
1306 if (!test_bit(IPOIB_FLAG_OPER_UP, &priv->flags)) {
1307 ipoib_dbg(priv, "IPOIB_FLAG_OPER_UP not set");
1308 return;
1309 }
1310
1311 queue_work(priv->wq, &priv->restart_task);
1312 }
1313
ipoib_get_iflink(const struct net_device * dev)1314 static int ipoib_get_iflink(const struct net_device *dev)
1315 {
1316 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1317
1318 /* parent interface */
1319 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags))
1320 return READ_ONCE(dev->ifindex);
1321
1322 /* child/vlan interface */
1323 return READ_ONCE(priv->parent->ifindex);
1324 }
1325
ipoib_addr_hash(struct ipoib_neigh_hash * htbl,u8 * daddr)1326 static u32 ipoib_addr_hash(struct ipoib_neigh_hash *htbl, u8 *daddr)
1327 {
1328 /*
1329 * Use only the address parts that contributes to spreading
1330 * The subnet prefix is not used as one can not connect to
1331 * same remote port (GUID) using the same remote QPN via two
1332 * different subnets.
1333 */
1334 /* qpn octets[1:4) & port GUID octets[12:20) */
1335 u32 *d32 = (u32 *) daddr;
1336 u32 hv;
1337
1338 hv = jhash_3words(d32[3], d32[4], IPOIB_QPN_MASK & d32[0], 0);
1339 return hv & htbl->mask;
1340 }
1341
ipoib_neigh_get(struct net_device * dev,u8 * daddr)1342 struct ipoib_neigh *ipoib_neigh_get(struct net_device *dev, u8 *daddr)
1343 {
1344 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1345 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1346 struct ipoib_neigh_hash *htbl;
1347 struct ipoib_neigh *neigh = NULL;
1348 u32 hash_val;
1349
1350 rcu_read_lock_bh();
1351
1352 htbl = rcu_dereference_bh(ntbl->htbl);
1353
1354 if (!htbl)
1355 goto out_unlock;
1356
1357 hash_val = ipoib_addr_hash(htbl, daddr);
1358 for (neigh = rcu_dereference_bh(htbl->buckets[hash_val]);
1359 neigh != NULL;
1360 neigh = rcu_dereference_bh(neigh->hnext)) {
1361 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1362 /* found, take one ref on behalf of the caller */
1363 if (!refcount_inc_not_zero(&neigh->refcnt)) {
1364 /* deleted */
1365 neigh = NULL;
1366 goto out_unlock;
1367 }
1368
1369 if (likely(skb_queue_len(&neigh->queue) < IPOIB_MAX_PATH_REC_QUEUE))
1370 neigh->alive = jiffies;
1371 goto out_unlock;
1372 }
1373 }
1374
1375 out_unlock:
1376 rcu_read_unlock_bh();
1377 return neigh;
1378 }
1379
__ipoib_reap_neigh(struct ipoib_dev_priv * priv)1380 static void __ipoib_reap_neigh(struct ipoib_dev_priv *priv)
1381 {
1382 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1383 struct ipoib_neigh_hash *htbl;
1384 unsigned long neigh_obsolete;
1385 unsigned long dt;
1386 unsigned long flags;
1387 int i;
1388 LIST_HEAD(remove_list);
1389
1390 spin_lock_irqsave(&priv->lock, flags);
1391
1392 htbl = rcu_dereference_protected(ntbl->htbl,
1393 lockdep_is_held(&priv->lock));
1394
1395 if (!htbl)
1396 goto out_unlock;
1397
1398 /* neigh is obsolete if it was idle for two GC periods */
1399 dt = 2 * arp_tbl.gc_interval;
1400 neigh_obsolete = jiffies - dt;
1401
1402 for (i = 0; i < htbl->size; i++) {
1403 struct ipoib_neigh *neigh;
1404 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1405
1406 while ((neigh = rcu_dereference_protected(*np,
1407 lockdep_is_held(&priv->lock))) != NULL) {
1408 /* was the neigh idle for two GC periods */
1409 if (time_after(neigh_obsolete, neigh->alive)) {
1410
1411 ipoib_check_and_add_mcast_sendonly(priv, neigh->daddr + 4, &remove_list);
1412
1413 rcu_assign_pointer(*np,
1414 rcu_dereference_protected(neigh->hnext,
1415 lockdep_is_held(&priv->lock)));
1416 /* remove from path/mc list */
1417 list_del_init(&neigh->list);
1418 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1419 } else {
1420 np = &neigh->hnext;
1421 }
1422
1423 }
1424 }
1425
1426 out_unlock:
1427 spin_unlock_irqrestore(&priv->lock, flags);
1428 ipoib_mcast_remove_list(&remove_list);
1429 }
1430
ipoib_reap_neigh(struct work_struct * work)1431 static void ipoib_reap_neigh(struct work_struct *work)
1432 {
1433 struct ipoib_dev_priv *priv =
1434 container_of(work, struct ipoib_dev_priv, neigh_reap_task.work);
1435
1436 __ipoib_reap_neigh(priv);
1437
1438 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1439 arp_tbl.gc_interval);
1440 }
1441
1442
ipoib_neigh_ctor(u8 * daddr,struct net_device * dev)1443 static struct ipoib_neigh *ipoib_neigh_ctor(u8 *daddr,
1444 struct net_device *dev)
1445 {
1446 struct ipoib_neigh *neigh;
1447
1448 neigh = kzalloc_obj(*neigh, GFP_ATOMIC);
1449 if (!neigh)
1450 return NULL;
1451
1452 neigh->dev = dev;
1453 memcpy(&neigh->daddr, daddr, sizeof(neigh->daddr));
1454 skb_queue_head_init(&neigh->queue);
1455 INIT_LIST_HEAD(&neigh->list);
1456 ipoib_cm_set(neigh, NULL);
1457 /* one ref on behalf of the caller */
1458 refcount_set(&neigh->refcnt, 1);
1459
1460 return neigh;
1461 }
1462
ipoib_neigh_alloc(u8 * daddr,struct net_device * dev)1463 struct ipoib_neigh *ipoib_neigh_alloc(u8 *daddr,
1464 struct net_device *dev)
1465 {
1466 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1467 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1468 struct ipoib_neigh_hash *htbl;
1469 struct ipoib_neigh *neigh;
1470 u32 hash_val;
1471
1472 htbl = rcu_dereference_protected(ntbl->htbl,
1473 lockdep_is_held(&priv->lock));
1474 if (!htbl) {
1475 neigh = NULL;
1476 goto out_unlock;
1477 }
1478
1479 /* need to add a new neigh, but maybe some other thread succeeded?
1480 * recalc hash, maybe hash resize took place so we do a search
1481 */
1482 hash_val = ipoib_addr_hash(htbl, daddr);
1483 for (neigh = rcu_dereference_protected(htbl->buckets[hash_val],
1484 lockdep_is_held(&priv->lock));
1485 neigh != NULL;
1486 neigh = rcu_dereference_protected(neigh->hnext,
1487 lockdep_is_held(&priv->lock))) {
1488 if (memcmp(daddr, neigh->daddr, INFINIBAND_ALEN) == 0) {
1489 /* found, take one ref on behalf of the caller */
1490 if (!refcount_inc_not_zero(&neigh->refcnt)) {
1491 /* deleted */
1492 neigh = NULL;
1493 break;
1494 }
1495 neigh->alive = jiffies;
1496 goto out_unlock;
1497 }
1498 }
1499
1500 neigh = ipoib_neigh_ctor(daddr, dev);
1501 if (!neigh)
1502 goto out_unlock;
1503
1504 /* one ref on behalf of the hash table */
1505 refcount_inc(&neigh->refcnt);
1506 neigh->alive = jiffies;
1507 /* put in hash */
1508 rcu_assign_pointer(neigh->hnext,
1509 rcu_dereference_protected(htbl->buckets[hash_val],
1510 lockdep_is_held(&priv->lock)));
1511 rcu_assign_pointer(htbl->buckets[hash_val], neigh);
1512 atomic_inc(&ntbl->entries);
1513
1514 out_unlock:
1515
1516 return neigh;
1517 }
1518
ipoib_neigh_dtor(struct ipoib_neigh * neigh)1519 void ipoib_neigh_dtor(struct ipoib_neigh *neigh)
1520 {
1521 /* neigh reference count was dropprd to zero */
1522 struct net_device *dev = neigh->dev;
1523 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1524 struct sk_buff *skb;
1525 if (neigh->ah)
1526 ipoib_put_ah(neigh->ah);
1527 while ((skb = __skb_dequeue(&neigh->queue))) {
1528 ++dev->stats.tx_dropped;
1529 dev_kfree_skb_any(skb);
1530 }
1531 if (ipoib_cm_get(neigh))
1532 ipoib_cm_destroy_tx(ipoib_cm_get(neigh));
1533 ipoib_dbg(ipoib_priv(dev),
1534 "neigh free for %06x %pI6\n",
1535 IPOIB_QPN(neigh->daddr),
1536 neigh->daddr + 4);
1537 kfree(neigh);
1538 if (atomic_dec_and_test(&priv->ntbl.entries)) {
1539 if (test_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags))
1540 complete(&priv->ntbl.flushed);
1541 }
1542 }
1543
ipoib_neigh_reclaim(struct rcu_head * rp)1544 static void ipoib_neigh_reclaim(struct rcu_head *rp)
1545 {
1546 /* Called as a result of removal from hash table */
1547 struct ipoib_neigh *neigh = container_of(rp, struct ipoib_neigh, rcu);
1548 /* note TX context may hold another ref */
1549 ipoib_neigh_put(neigh);
1550 }
1551
ipoib_neigh_free(struct ipoib_neigh * neigh)1552 void ipoib_neigh_free(struct ipoib_neigh *neigh)
1553 {
1554 struct net_device *dev = neigh->dev;
1555 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1556 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1557 struct ipoib_neigh_hash *htbl;
1558 struct ipoib_neigh __rcu **np;
1559 struct ipoib_neigh *n;
1560 u32 hash_val;
1561
1562 htbl = rcu_dereference_protected(ntbl->htbl,
1563 lockdep_is_held(&priv->lock));
1564 if (!htbl)
1565 return;
1566
1567 hash_val = ipoib_addr_hash(htbl, neigh->daddr);
1568 np = &htbl->buckets[hash_val];
1569 for (n = rcu_dereference_protected(*np,
1570 lockdep_is_held(&priv->lock));
1571 n != NULL;
1572 n = rcu_dereference_protected(*np,
1573 lockdep_is_held(&priv->lock))) {
1574 if (n == neigh) {
1575 /* found */
1576 rcu_assign_pointer(*np,
1577 rcu_dereference_protected(neigh->hnext,
1578 lockdep_is_held(&priv->lock)));
1579 /* remove from parent list */
1580 list_del_init(&neigh->list);
1581 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1582 return;
1583 } else {
1584 np = &n->hnext;
1585 }
1586 }
1587 }
1588
ipoib_neigh_hash_init(struct ipoib_dev_priv * priv)1589 static int ipoib_neigh_hash_init(struct ipoib_dev_priv *priv)
1590 {
1591 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1592 struct ipoib_neigh_hash *htbl;
1593 struct ipoib_neigh __rcu **buckets;
1594 u32 size;
1595
1596 clear_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1597 ntbl->htbl = NULL;
1598 htbl = kzalloc_obj(*htbl);
1599 if (!htbl)
1600 return -ENOMEM;
1601 size = roundup_pow_of_two(arp_tbl.gc_thresh3);
1602 buckets = kvzalloc_objs(*buckets, size);
1603 if (!buckets) {
1604 kfree(htbl);
1605 return -ENOMEM;
1606 }
1607 htbl->size = size;
1608 htbl->mask = (size - 1);
1609 htbl->buckets = buckets;
1610 RCU_INIT_POINTER(ntbl->htbl, htbl);
1611 htbl->ntbl = ntbl;
1612 atomic_set(&ntbl->entries, 0);
1613
1614 /* start garbage collection */
1615 queue_delayed_work(priv->wq, &priv->neigh_reap_task,
1616 arp_tbl.gc_interval);
1617
1618 return 0;
1619 }
1620
neigh_hash_free_rcu(struct rcu_head * head)1621 static void neigh_hash_free_rcu(struct rcu_head *head)
1622 {
1623 struct ipoib_neigh_hash *htbl = container_of(head,
1624 struct ipoib_neigh_hash,
1625 rcu);
1626 struct ipoib_neigh __rcu **buckets = htbl->buckets;
1627 struct ipoib_neigh_table *ntbl = htbl->ntbl;
1628
1629 kvfree(buckets);
1630 kfree(htbl);
1631 complete(&ntbl->deleted);
1632 }
1633
ipoib_del_neighs_by_gid(struct net_device * dev,u8 * gid)1634 void ipoib_del_neighs_by_gid(struct net_device *dev, u8 *gid)
1635 {
1636 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1637 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1638 struct ipoib_neigh_hash *htbl;
1639 unsigned long flags;
1640 int i;
1641
1642 /* remove all neigh connected to a given path or mcast */
1643 spin_lock_irqsave(&priv->lock, flags);
1644
1645 htbl = rcu_dereference_protected(ntbl->htbl,
1646 lockdep_is_held(&priv->lock));
1647
1648 if (!htbl)
1649 goto out_unlock;
1650
1651 for (i = 0; i < htbl->size; i++) {
1652 struct ipoib_neigh *neigh;
1653 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1654
1655 while ((neigh = rcu_dereference_protected(*np,
1656 lockdep_is_held(&priv->lock))) != NULL) {
1657 /* delete neighs belong to this parent */
1658 if (!memcmp(gid, neigh->daddr + 4, sizeof (union ib_gid))) {
1659 rcu_assign_pointer(*np,
1660 rcu_dereference_protected(neigh->hnext,
1661 lockdep_is_held(&priv->lock)));
1662 /* remove from parent list */
1663 list_del_init(&neigh->list);
1664 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1665 } else {
1666 np = &neigh->hnext;
1667 }
1668
1669 }
1670 }
1671 out_unlock:
1672 spin_unlock_irqrestore(&priv->lock, flags);
1673 }
1674
ipoib_flush_neighs(struct ipoib_dev_priv * priv)1675 static void ipoib_flush_neighs(struct ipoib_dev_priv *priv)
1676 {
1677 struct ipoib_neigh_table *ntbl = &priv->ntbl;
1678 struct ipoib_neigh_hash *htbl;
1679 unsigned long flags;
1680 int i, wait_flushed = 0;
1681
1682 init_completion(&priv->ntbl.flushed);
1683 set_bit(IPOIB_NEIGH_TBL_FLUSH, &priv->flags);
1684
1685 spin_lock_irqsave(&priv->lock, flags);
1686
1687 htbl = rcu_dereference_protected(ntbl->htbl,
1688 lockdep_is_held(&priv->lock));
1689 if (!htbl)
1690 goto out_unlock;
1691
1692 wait_flushed = atomic_read(&priv->ntbl.entries);
1693 if (!wait_flushed)
1694 goto free_htbl;
1695
1696 for (i = 0; i < htbl->size; i++) {
1697 struct ipoib_neigh *neigh;
1698 struct ipoib_neigh __rcu **np = &htbl->buckets[i];
1699
1700 while ((neigh = rcu_dereference_protected(*np,
1701 lockdep_is_held(&priv->lock))) != NULL) {
1702 rcu_assign_pointer(*np,
1703 rcu_dereference_protected(neigh->hnext,
1704 lockdep_is_held(&priv->lock)));
1705 /* remove from path/mc list */
1706 list_del_init(&neigh->list);
1707 call_rcu(&neigh->rcu, ipoib_neigh_reclaim);
1708 }
1709 }
1710
1711 free_htbl:
1712 rcu_assign_pointer(ntbl->htbl, NULL);
1713 call_rcu(&htbl->rcu, neigh_hash_free_rcu);
1714
1715 out_unlock:
1716 spin_unlock_irqrestore(&priv->lock, flags);
1717 if (wait_flushed)
1718 wait_for_completion(&priv->ntbl.flushed);
1719 }
1720
ipoib_neigh_hash_uninit(struct net_device * dev)1721 static void ipoib_neigh_hash_uninit(struct net_device *dev)
1722 {
1723 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1724
1725 ipoib_dbg(priv, "%s\n", __func__);
1726 init_completion(&priv->ntbl.deleted);
1727
1728 cancel_delayed_work_sync(&priv->neigh_reap_task);
1729
1730 ipoib_flush_neighs(priv);
1731
1732 wait_for_completion(&priv->ntbl.deleted);
1733 }
1734
ipoib_napi_add(struct net_device * dev)1735 static void ipoib_napi_add(struct net_device *dev)
1736 {
1737 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1738
1739 netif_napi_add_weight(dev, &priv->recv_napi, ipoib_rx_poll,
1740 IPOIB_NUM_WC);
1741 netif_napi_add_weight(dev, &priv->send_napi, ipoib_tx_poll,
1742 MAX_SEND_CQE);
1743 }
1744
ipoib_napi_del(struct net_device * dev)1745 static void ipoib_napi_del(struct net_device *dev)
1746 {
1747 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1748
1749 netif_napi_del(&priv->recv_napi);
1750 netif_napi_del(&priv->send_napi);
1751 }
1752
ipoib_dev_uninit_default(struct net_device * dev)1753 static void ipoib_dev_uninit_default(struct net_device *dev)
1754 {
1755 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1756
1757 ipoib_transport_dev_cleanup(dev);
1758
1759 ipoib_napi_del(dev);
1760
1761 ipoib_cm_dev_cleanup(dev);
1762
1763 kfree(priv->rx_ring);
1764 vfree(priv->tx_ring);
1765
1766 priv->rx_ring = NULL;
1767 priv->tx_ring = NULL;
1768 }
1769
ipoib_dev_init_default(struct net_device * dev)1770 static int ipoib_dev_init_default(struct net_device *dev)
1771 {
1772 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1773 u8 addr_mod[3];
1774
1775 ipoib_napi_add(dev);
1776
1777 /* Allocate RX/TX "rings" to hold queued skbs */
1778 priv->rx_ring = kzalloc_objs(*priv->rx_ring, ipoib_recvq_size);
1779 if (!priv->rx_ring)
1780 goto out;
1781
1782 priv->tx_ring = vzalloc(array_size(ipoib_sendq_size,
1783 sizeof(*priv->tx_ring)));
1784 if (!priv->tx_ring) {
1785 pr_warn("%s: failed to allocate TX ring (%d entries)\n",
1786 priv->ca->name, ipoib_sendq_size);
1787 goto out_rx_ring_cleanup;
1788 }
1789
1790 /* priv->tx_head, tx_tail and global_tx_tail/head are already 0 */
1791
1792 if (ipoib_transport_dev_init(dev, priv->ca)) {
1793 pr_warn("%s: ipoib_transport_dev_init failed\n",
1794 priv->ca->name);
1795 goto out_tx_ring_cleanup;
1796 }
1797
1798 /* after qp created set dev address */
1799 addr_mod[0] = (priv->qp->qp_num >> 16) & 0xff;
1800 addr_mod[1] = (priv->qp->qp_num >> 8) & 0xff;
1801 addr_mod[2] = (priv->qp->qp_num) & 0xff;
1802 dev_addr_mod(priv->dev, 1, addr_mod, sizeof(addr_mod));
1803
1804 return 0;
1805
1806 out_tx_ring_cleanup:
1807 vfree(priv->tx_ring);
1808
1809 out_rx_ring_cleanup:
1810 kfree(priv->rx_ring);
1811
1812 out:
1813 ipoib_napi_del(dev);
1814 return -ENOMEM;
1815 }
1816
ipoib_ioctl(struct net_device * dev,struct ifreq * ifr,int cmd)1817 static int ipoib_ioctl(struct net_device *dev, struct ifreq *ifr,
1818 int cmd)
1819 {
1820 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1821
1822 if (!priv->rn_ops->ndo_eth_ioctl)
1823 return -EOPNOTSUPP;
1824
1825 return priv->rn_ops->ndo_eth_ioctl(dev, ifr, cmd);
1826 }
1827
ipoib_hwtstamp_get(struct net_device * dev,struct kernel_hwtstamp_config * config)1828 static int ipoib_hwtstamp_get(struct net_device *dev,
1829 struct kernel_hwtstamp_config *config)
1830 {
1831 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1832
1833 if (!priv->rn_ops->ndo_hwtstamp_get)
1834 return -EOPNOTSUPP;
1835
1836 return priv->rn_ops->ndo_hwtstamp_get(dev, config);
1837 }
1838
ipoib_hwtstamp_set(struct net_device * dev,struct kernel_hwtstamp_config * config,struct netlink_ext_ack * extack)1839 static int ipoib_hwtstamp_set(struct net_device *dev,
1840 struct kernel_hwtstamp_config *config,
1841 struct netlink_ext_ack *extack)
1842 {
1843 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1844
1845 if (!priv->rn_ops->ndo_hwtstamp_set)
1846 return -EOPNOTSUPP;
1847
1848 return priv->rn_ops->ndo_hwtstamp_set(dev, config, extack);
1849 }
1850
ipoib_dev_init(struct net_device * dev)1851 static int ipoib_dev_init(struct net_device *dev)
1852 {
1853 struct ipoib_dev_priv *priv = ipoib_priv(dev);
1854 int ret = -ENOMEM;
1855
1856 priv->qp = NULL;
1857
1858 /*
1859 * the various IPoIB tasks assume they will never race against
1860 * themselves, so always use a single thread workqueue
1861 */
1862 priv->wq = alloc_ordered_workqueue("ipoib_wq", WQ_MEM_RECLAIM);
1863 if (!priv->wq) {
1864 pr_warn("%s: failed to allocate device WQ\n", dev->name);
1865 goto out;
1866 }
1867
1868 /* create pd, which used both for control and datapath*/
1869 priv->pd = ib_alloc_pd(priv->ca, 0);
1870 if (IS_ERR(priv->pd)) {
1871 pr_warn("%s: failed to allocate PD\n", priv->ca->name);
1872 goto clean_wq;
1873 }
1874
1875 ret = priv->rn_ops->ndo_init(dev);
1876 if (ret) {
1877 pr_warn("%s failed to init HW resource\n", dev->name);
1878 goto out_free_pd;
1879 }
1880
1881 ret = ipoib_neigh_hash_init(priv);
1882 if (ret) {
1883 pr_warn("%s failed to init neigh hash\n", dev->name);
1884 goto out_dev_uninit;
1885 }
1886
1887 if (dev->flags & IFF_UP) {
1888 if (ipoib_ib_dev_open(dev)) {
1889 pr_warn("%s failed to open device\n", dev->name);
1890 ret = -ENODEV;
1891 goto out_hash_uninit;
1892 }
1893 }
1894
1895 return 0;
1896
1897 out_hash_uninit:
1898 ipoib_neigh_hash_uninit(dev);
1899
1900 out_dev_uninit:
1901 ipoib_ib_dev_cleanup(dev);
1902
1903 out_free_pd:
1904 if (priv->pd) {
1905 ib_dealloc_pd(priv->pd);
1906 priv->pd = NULL;
1907 }
1908
1909 clean_wq:
1910 if (priv->wq) {
1911 destroy_workqueue(priv->wq);
1912 priv->wq = NULL;
1913 }
1914
1915 out:
1916 return ret;
1917 }
1918
1919 /*
1920 * This must be called before doing an unregister_netdev on a parent device to
1921 * shutdown the IB event handler.
1922 */
ipoib_parent_unregister_pre(struct net_device * ndev)1923 static void ipoib_parent_unregister_pre(struct net_device *ndev)
1924 {
1925 struct ipoib_dev_priv *priv = ipoib_priv(ndev);
1926
1927 /*
1928 * ipoib_set_mac checks netif_running before pushing work, clearing
1929 * running ensures the it will not add more work.
1930 */
1931 rtnl_lock();
1932 dev_change_flags(priv->dev, priv->dev->flags & ~IFF_UP, NULL);
1933 rtnl_unlock();
1934
1935 /* ipoib_event() cannot be running once this returns */
1936 ib_unregister_event_handler(&priv->event_handler);
1937
1938 /*
1939 * Work on the queue grabs the rtnl lock, so this cannot be done while
1940 * also holding it.
1941 */
1942 flush_workqueue(ipoib_workqueue);
1943 }
1944
ipoib_set_dev_features(struct ipoib_dev_priv * priv)1945 static void ipoib_set_dev_features(struct ipoib_dev_priv *priv)
1946 {
1947 priv->hca_caps = priv->ca->attrs.device_cap_flags;
1948 priv->kernel_caps = priv->ca->attrs.kernel_cap_flags;
1949
1950 if (priv->hca_caps & IB_DEVICE_UD_IP_CSUM) {
1951 priv->dev->hw_features |= NETIF_F_IP_CSUM | NETIF_F_RXCSUM;
1952
1953 if (priv->kernel_caps & IBK_UD_TSO)
1954 priv->dev->hw_features |= NETIF_F_TSO;
1955
1956 priv->dev->features |= priv->dev->hw_features;
1957 }
1958 }
1959
ipoib_parent_init(struct net_device * ndev)1960 static int ipoib_parent_init(struct net_device *ndev)
1961 {
1962 struct ipoib_dev_priv *priv = ipoib_priv(ndev);
1963 struct ib_port_attr attr;
1964 int result;
1965
1966 result = ib_query_port(priv->ca, priv->port, &attr);
1967 if (result) {
1968 pr_warn("%s: ib_query_port %d failed\n", priv->ca->name,
1969 priv->port);
1970 return result;
1971 }
1972 priv->max_ib_mtu = rdma_mtu_from_attr(priv->ca, priv->port, &attr);
1973
1974 result = ib_query_pkey(priv->ca, priv->port, 0, &priv->pkey);
1975 if (result) {
1976 pr_warn("%s: ib_query_pkey port %d failed (ret = %d)\n",
1977 priv->ca->name, priv->port, result);
1978 return result;
1979 }
1980
1981 result = rdma_query_gid(priv->ca, priv->port, 0, &priv->local_gid);
1982 if (result) {
1983 pr_warn("%s: rdma_query_gid port %d failed (ret = %d)\n",
1984 priv->ca->name, priv->port, result);
1985 return result;
1986 }
1987 dev_addr_mod(priv->dev, 4, priv->local_gid.raw, sizeof(union ib_gid));
1988
1989 SET_NETDEV_DEV(priv->dev, priv->ca->dev.parent);
1990 priv->dev->dev_port = priv->port - 1;
1991 /* Let's set this one too for backwards compatibility. */
1992 priv->dev->dev_id = priv->port - 1;
1993
1994 return 0;
1995 }
1996
ipoib_child_init(struct net_device * ndev)1997 static void ipoib_child_init(struct net_device *ndev)
1998 {
1999 struct ipoib_dev_priv *priv = ipoib_priv(ndev);
2000 struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
2001
2002 priv->max_ib_mtu = ppriv->max_ib_mtu;
2003 set_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags);
2004 if (memchr_inv(priv->dev->dev_addr, 0, INFINIBAND_ALEN))
2005 memcpy(&priv->local_gid, priv->dev->dev_addr + 4,
2006 sizeof(priv->local_gid));
2007 else {
2008 __dev_addr_set(priv->dev, ppriv->dev->dev_addr,
2009 INFINIBAND_ALEN);
2010 memcpy(&priv->local_gid, &ppriv->local_gid,
2011 sizeof(priv->local_gid));
2012 }
2013 }
2014
ipoib_ndo_init(struct net_device * ndev)2015 static int ipoib_ndo_init(struct net_device *ndev)
2016 {
2017 struct ipoib_dev_priv *priv = ipoib_priv(ndev);
2018 int rc;
2019 struct rdma_netdev *rn = netdev_priv(ndev);
2020
2021 if (priv->parent) {
2022 ipoib_child_init(ndev);
2023 } else {
2024 rc = ipoib_parent_init(ndev);
2025 if (rc)
2026 return rc;
2027 }
2028
2029 /* MTU will be reset when mcast join happens */
2030 ndev->mtu = IPOIB_UD_MTU(priv->max_ib_mtu);
2031 priv->mcast_mtu = priv->admin_mtu = ndev->mtu;
2032 rn->mtu = priv->mcast_mtu;
2033 ndev->max_mtu = IPOIB_CM_MTU;
2034
2035 ndev->neigh_priv_len = sizeof(struct ipoib_neigh);
2036
2037 /*
2038 * Set the full membership bit, so that we join the right
2039 * broadcast group, etc.
2040 */
2041 priv->pkey |= 0x8000;
2042
2043 ndev->broadcast[8] = priv->pkey >> 8;
2044 ndev->broadcast[9] = priv->pkey & 0xff;
2045 set_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
2046
2047 ipoib_set_dev_features(priv);
2048
2049 rc = ipoib_dev_init(ndev);
2050 if (rc) {
2051 pr_warn("%s: failed to initialize device: %s port %d (ret = %d)\n",
2052 priv->ca->name, priv->dev->name, priv->port, rc);
2053 return rc;
2054 }
2055
2056 if (priv->parent) {
2057 struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
2058
2059 dev_hold(priv->parent);
2060
2061 netdev_lock(priv->parent);
2062 list_add_tail(&priv->list, &ppriv->child_intfs);
2063 netdev_unlock(priv->parent);
2064 }
2065
2066 return 0;
2067 }
2068
ipoib_ndo_uninit(struct net_device * dev)2069 static void ipoib_ndo_uninit(struct net_device *dev)
2070 {
2071 struct ipoib_dev_priv *priv = ipoib_priv(dev);
2072
2073 /*
2074 * ipoib_remove_one guarantees the children are removed before the
2075 * parent, and that is the only place where a parent can be removed.
2076 */
2077 WARN_ON(!list_empty(&priv->child_intfs));
2078
2079 if (priv->parent) {
2080 struct ipoib_dev_priv *ppriv = ipoib_priv(priv->parent);
2081
2082 netdev_lock(ppriv->dev);
2083 list_del(&priv->list);
2084 netdev_unlock(ppriv->dev);
2085 }
2086
2087 ipoib_neigh_hash_uninit(dev);
2088
2089 ipoib_ib_dev_cleanup(dev);
2090
2091 /* no more works over the priv->wq */
2092 if (priv->wq) {
2093 /* See ipoib_mcast_carrier_on_task() */
2094 WARN_ON(test_bit(IPOIB_FLAG_OPER_UP, &priv->flags));
2095 destroy_workqueue(priv->wq);
2096 priv->wq = NULL;
2097 }
2098
2099 dev_put(priv->parent);
2100 }
2101
ipoib_set_vf_link_state(struct net_device * dev,int vf,int link_state)2102 static int ipoib_set_vf_link_state(struct net_device *dev, int vf, int link_state)
2103 {
2104 struct ipoib_dev_priv *priv = ipoib_priv(dev);
2105
2106 return ib_set_vf_link_state(priv->ca, vf, priv->port, link_state);
2107 }
2108
ipoib_get_vf_config(struct net_device * dev,int vf,struct ifla_vf_info * ivf)2109 static int ipoib_get_vf_config(struct net_device *dev, int vf,
2110 struct ifla_vf_info *ivf)
2111 {
2112 struct ipoib_dev_priv *priv = ipoib_priv(dev);
2113 int err;
2114
2115 err = ib_get_vf_config(priv->ca, vf, priv->port, ivf);
2116 if (err)
2117 return err;
2118
2119 ivf->vf = vf;
2120 memcpy(ivf->mac, dev->dev_addr, dev->addr_len);
2121
2122 return 0;
2123 }
2124
ipoib_set_vf_guid(struct net_device * dev,int vf,u64 guid,int type)2125 static int ipoib_set_vf_guid(struct net_device *dev, int vf, u64 guid, int type)
2126 {
2127 struct ipoib_dev_priv *priv = ipoib_priv(dev);
2128
2129 if (type != IFLA_VF_IB_NODE_GUID && type != IFLA_VF_IB_PORT_GUID)
2130 return -EINVAL;
2131
2132 return ib_set_vf_guid(priv->ca, vf, priv->port, guid, type);
2133 }
2134
ipoib_get_vf_guid(struct net_device * dev,int vf,struct ifla_vf_guid * node_guid,struct ifla_vf_guid * port_guid)2135 static int ipoib_get_vf_guid(struct net_device *dev, int vf,
2136 struct ifla_vf_guid *node_guid,
2137 struct ifla_vf_guid *port_guid)
2138 {
2139 struct ipoib_dev_priv *priv = ipoib_priv(dev);
2140
2141 return ib_get_vf_guid(priv->ca, vf, priv->port, node_guid, port_guid);
2142 }
2143
ipoib_get_vf_stats(struct net_device * dev,int vf,struct ifla_vf_stats * vf_stats)2144 static int ipoib_get_vf_stats(struct net_device *dev, int vf,
2145 struct ifla_vf_stats *vf_stats)
2146 {
2147 struct ipoib_dev_priv *priv = ipoib_priv(dev);
2148
2149 return ib_get_vf_stats(priv->ca, vf, priv->port, vf_stats);
2150 }
2151
2152 static const struct header_ops ipoib_header_ops = {
2153 .create = ipoib_hard_header,
2154 };
2155
2156 static const struct net_device_ops ipoib_netdev_ops_pf = {
2157 .ndo_init = ipoib_ndo_init,
2158 .ndo_uninit = ipoib_ndo_uninit,
2159 .ndo_open = ipoib_open,
2160 .ndo_stop = ipoib_stop,
2161 .ndo_change_mtu = ipoib_change_mtu,
2162 .ndo_fix_features = ipoib_fix_features,
2163 .ndo_start_xmit = ipoib_start_xmit,
2164 .ndo_tx_timeout = ipoib_timeout,
2165 .ndo_set_rx_mode_async = ipoib_set_rx_mode_async,
2166 .ndo_get_iflink = ipoib_get_iflink,
2167 .ndo_set_vf_link_state = ipoib_set_vf_link_state,
2168 .ndo_get_vf_config = ipoib_get_vf_config,
2169 .ndo_get_vf_stats = ipoib_get_vf_stats,
2170 .ndo_get_vf_guid = ipoib_get_vf_guid,
2171 .ndo_set_vf_guid = ipoib_set_vf_guid,
2172 .ndo_set_mac_address = ipoib_set_mac,
2173 .ndo_get_stats64 = ipoib_get_stats,
2174 .ndo_eth_ioctl = ipoib_ioctl,
2175 .ndo_hwtstamp_get = ipoib_hwtstamp_get,
2176 .ndo_hwtstamp_set = ipoib_hwtstamp_set,
2177 };
2178
2179 static const struct net_device_ops ipoib_netdev_ops_vf = {
2180 .ndo_init = ipoib_ndo_init,
2181 .ndo_uninit = ipoib_ndo_uninit,
2182 .ndo_open = ipoib_open,
2183 .ndo_stop = ipoib_stop,
2184 .ndo_change_mtu = ipoib_change_mtu,
2185 .ndo_fix_features = ipoib_fix_features,
2186 .ndo_start_xmit = ipoib_start_xmit,
2187 .ndo_tx_timeout = ipoib_timeout,
2188 .ndo_set_rx_mode_async = ipoib_set_rx_mode_async,
2189 .ndo_get_iflink = ipoib_get_iflink,
2190 .ndo_get_stats64 = ipoib_get_stats,
2191 .ndo_eth_ioctl = ipoib_ioctl,
2192 .ndo_hwtstamp_get = ipoib_hwtstamp_get,
2193 .ndo_hwtstamp_set = ipoib_hwtstamp_set,
2194 };
2195
2196 static const struct net_device_ops ipoib_netdev_default_pf = {
2197 .ndo_init = ipoib_dev_init_default,
2198 .ndo_uninit = ipoib_dev_uninit_default,
2199 .ndo_open = ipoib_ib_dev_open_default,
2200 .ndo_stop = ipoib_ib_dev_stop_default,
2201 };
2202
ipoib_setup_common(struct net_device * dev)2203 void ipoib_setup_common(struct net_device *dev)
2204 {
2205 dev->header_ops = &ipoib_header_ops;
2206 dev->netdev_ops = &ipoib_netdev_default_pf;
2207
2208 ipoib_set_ethtool_ops(dev);
2209
2210 dev->watchdog_timeo = 10 * HZ;
2211
2212 dev->flags |= IFF_BROADCAST | IFF_MULTICAST;
2213
2214 dev->hard_header_len = IPOIB_HARD_LEN;
2215 dev->addr_len = INFINIBAND_ALEN;
2216 dev->type = ARPHRD_INFINIBAND;
2217 dev->tx_queue_len = DEFAULT_TX_QUEUE_LEN;
2218 dev->features = (NETIF_F_VLAN_CHALLENGED |
2219 NETIF_F_HIGHDMA);
2220 netif_keep_dst(dev);
2221
2222 memcpy(dev->broadcast, ipv4_bcast_addr, INFINIBAND_ALEN);
2223
2224 /*
2225 * unregister_netdev always frees the netdev, we use this mode
2226 * consistently to unify all the various unregister paths, including
2227 * those connected to rtnl_link_ops which require it.
2228 */
2229 dev->needs_free_netdev = true;
2230 }
2231
ipoib_build_priv(struct net_device * dev)2232 static void ipoib_build_priv(struct net_device *dev)
2233 {
2234 struct ipoib_dev_priv *priv = ipoib_priv(dev);
2235
2236 priv->dev = dev;
2237 spin_lock_init(&priv->lock);
2238 mutex_init(&priv->mcast_mutex);
2239
2240 INIT_LIST_HEAD(&priv->path_list);
2241 INIT_LIST_HEAD(&priv->child_intfs);
2242 INIT_LIST_HEAD(&priv->dead_ahs);
2243 INIT_LIST_HEAD(&priv->multicast_list);
2244
2245 INIT_DELAYED_WORK(&priv->mcast_task, ipoib_mcast_join_task);
2246 INIT_WORK(&priv->carrier_on_task, ipoib_mcast_carrier_on_task);
2247 INIT_WORK(&priv->reschedule_napi_work, ipoib_napi_schedule_work);
2248 INIT_WORK(&priv->flush_light, ipoib_ib_dev_flush_light);
2249 INIT_WORK(&priv->flush_normal, ipoib_ib_dev_flush_normal);
2250 INIT_WORK(&priv->flush_heavy, ipoib_ib_dev_flush_heavy);
2251 INIT_WORK(&priv->restart_task, ipoib_mcast_restart_task);
2252 INIT_WORK(&priv->tx_timeout_work, ipoib_ib_tx_timeout_work);
2253 INIT_DELAYED_WORK(&priv->ah_reap_task, ipoib_reap_ah);
2254 INIT_DELAYED_WORK(&priv->neigh_reap_task, ipoib_reap_neigh);
2255 }
2256
ipoib_alloc_netdev(struct ib_device * hca,u32 port,const char * name)2257 static struct net_device *ipoib_alloc_netdev(struct ib_device *hca, u32 port,
2258 const char *name)
2259 {
2260 struct net_device *dev;
2261
2262 dev = rdma_alloc_netdev(hca, port, RDMA_NETDEV_IPOIB, name,
2263 NET_NAME_UNKNOWN, ipoib_setup_common);
2264 if (!IS_ERR(dev) || PTR_ERR(dev) != -EOPNOTSUPP)
2265 return dev;
2266
2267 dev = alloc_netdev(sizeof(struct rdma_netdev), name, NET_NAME_UNKNOWN,
2268 ipoib_setup_common);
2269 if (!dev)
2270 return ERR_PTR(-ENOMEM);
2271 return dev;
2272 }
2273
ipoib_intf_init(struct ib_device * hca,u32 port,const char * name,struct net_device * dev)2274 int ipoib_intf_init(struct ib_device *hca, u32 port, const char *name,
2275 struct net_device *dev)
2276 {
2277 struct rdma_netdev *rn = netdev_priv(dev);
2278 struct ipoib_dev_priv *priv;
2279 int rc;
2280
2281 priv = kzalloc_obj(*priv);
2282 if (!priv)
2283 return -ENOMEM;
2284
2285 priv->ca = hca;
2286 priv->port = port;
2287
2288 rc = rdma_init_netdev(hca, port, RDMA_NETDEV_IPOIB, name,
2289 NET_NAME_UNKNOWN, ipoib_setup_common, dev);
2290 if (rc) {
2291 if (rc != -EOPNOTSUPP)
2292 goto out;
2293
2294 rn->send = ipoib_send;
2295 rn->attach_mcast = ipoib_mcast_attach;
2296 rn->detach_mcast = ipoib_mcast_detach;
2297 rn->hca = hca;
2298
2299 rc = netif_set_real_num_tx_queues(dev, 1);
2300 if (rc)
2301 goto out;
2302
2303 rc = netif_set_real_num_rx_queues(dev, 1);
2304 if (rc)
2305 goto out;
2306 }
2307
2308 priv->rn_ops = dev->netdev_ops;
2309
2310 if (hca->attrs.kernel_cap_flags & IBK_VIRTUAL_FUNCTION)
2311 dev->netdev_ops = &ipoib_netdev_ops_vf;
2312 else
2313 dev->netdev_ops = &ipoib_netdev_ops_pf;
2314
2315 rn->clnt_priv = priv;
2316 /*
2317 * Only the child register_netdev flows can handle priv_destructor
2318 * being set, so we force it to NULL here and handle manually until it
2319 * is safe to turn on.
2320 */
2321 priv->next_priv_destructor = dev->priv_destructor;
2322 dev->priv_destructor = NULL;
2323
2324 ipoib_build_priv(dev);
2325
2326 return 0;
2327
2328 out:
2329 kfree(priv);
2330 return rc;
2331 }
2332
ipoib_intf_alloc(struct ib_device * hca,u32 port,const char * name)2333 struct net_device *ipoib_intf_alloc(struct ib_device *hca, u32 port,
2334 const char *name)
2335 {
2336 struct net_device *dev;
2337 int rc;
2338
2339 dev = ipoib_alloc_netdev(hca, port, name);
2340 if (IS_ERR(dev))
2341 return dev;
2342
2343 rc = ipoib_intf_init(hca, port, name, dev);
2344 if (rc) {
2345 free_netdev(dev);
2346 return ERR_PTR(rc);
2347 }
2348
2349 /*
2350 * Upon success the caller must ensure ipoib_intf_free is called or
2351 * register_netdevice succeed'd and priv_destructor is set to
2352 * ipoib_intf_free.
2353 */
2354 return dev;
2355 }
2356
ipoib_intf_free(struct net_device * dev)2357 void ipoib_intf_free(struct net_device *dev)
2358 {
2359 struct ipoib_dev_priv *priv = ipoib_priv(dev);
2360 struct rdma_netdev *rn = netdev_priv(dev);
2361
2362 dev->priv_destructor = priv->next_priv_destructor;
2363 if (dev->priv_destructor)
2364 dev->priv_destructor(dev);
2365
2366 /*
2367 * There are some error flows around register_netdev failing that may
2368 * attempt to call priv_destructor twice, prevent that from happening.
2369 */
2370 dev->priv_destructor = NULL;
2371
2372 /* unregister/destroy is very complicated. Make bugs more obvious. */
2373 rn->clnt_priv = NULL;
2374
2375 kfree(priv);
2376 }
2377
pkey_show(struct device * dev,struct device_attribute * attr,char * buf)2378 static ssize_t pkey_show(struct device *dev, struct device_attribute *attr,
2379 char *buf)
2380 {
2381 struct net_device *ndev = to_net_dev(dev);
2382 struct ipoib_dev_priv *priv = ipoib_priv(ndev);
2383
2384 return sysfs_emit(buf, "0x%04x\n", priv->pkey);
2385 }
2386 static DEVICE_ATTR_RO(pkey);
2387
umcast_show(struct device * dev,struct device_attribute * attr,char * buf)2388 static ssize_t umcast_show(struct device *dev, struct device_attribute *attr,
2389 char *buf)
2390 {
2391 struct net_device *ndev = to_net_dev(dev);
2392 struct ipoib_dev_priv *priv = ipoib_priv(ndev);
2393
2394 return sysfs_emit(buf, "%d\n",
2395 test_bit(IPOIB_FLAG_UMCAST, &priv->flags));
2396 }
2397
ipoib_set_umcast(struct net_device * ndev,int umcast_val)2398 void ipoib_set_umcast(struct net_device *ndev, int umcast_val)
2399 {
2400 struct ipoib_dev_priv *priv = ipoib_priv(ndev);
2401
2402 if (umcast_val > 0) {
2403 set_bit(IPOIB_FLAG_UMCAST, &priv->flags);
2404 ipoib_warn(priv, "ignoring multicast groups joined directly "
2405 "by userspace\n");
2406 } else
2407 clear_bit(IPOIB_FLAG_UMCAST, &priv->flags);
2408 }
2409
umcast_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2410 static ssize_t umcast_store(struct device *dev, struct device_attribute *attr,
2411 const char *buf, size_t count)
2412 {
2413 unsigned long umcast_val = simple_strtoul(buf, NULL, 0);
2414
2415 ipoib_set_umcast(to_net_dev(dev), umcast_val);
2416
2417 return count;
2418 }
2419 static DEVICE_ATTR_RW(umcast);
2420
ipoib_add_umcast_attr(struct net_device * dev)2421 int ipoib_add_umcast_attr(struct net_device *dev)
2422 {
2423 return device_create_file(&dev->dev, &dev_attr_umcast);
2424 }
2425
set_base_guid(struct ipoib_dev_priv * priv,union ib_gid * gid)2426 static void set_base_guid(struct ipoib_dev_priv *priv, union ib_gid *gid)
2427 {
2428 struct ipoib_dev_priv *child_priv;
2429 struct net_device *netdev = priv->dev;
2430
2431 netif_addr_lock_bh(netdev);
2432
2433 memcpy(&priv->local_gid.global.interface_id,
2434 &gid->global.interface_id,
2435 sizeof(gid->global.interface_id));
2436 dev_addr_mod(netdev, 4, (u8 *)&priv->local_gid, sizeof(priv->local_gid));
2437 clear_bit(IPOIB_FLAG_DEV_ADDR_SET, &priv->flags);
2438
2439 netif_addr_unlock_bh(netdev);
2440
2441 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
2442 netdev_lock_ops_to_full(priv->dev);
2443 list_for_each_entry(child_priv, &priv->child_intfs, list)
2444 set_base_guid(child_priv, gid);
2445 netdev_unlock_full_to_ops(priv->dev);
2446 }
2447 }
2448
ipoib_check_lladdr(struct net_device * dev,struct sockaddr_storage * ss)2449 static int ipoib_check_lladdr(struct net_device *dev,
2450 struct sockaddr_storage *ss)
2451 {
2452 union ib_gid *gid = (union ib_gid *)(ss->__data + 4);
2453 int ret = 0;
2454
2455 netif_addr_lock_bh(dev);
2456
2457 /* Make sure the QPN, reserved and subnet prefix match the current
2458 * lladdr, it also makes sure the lladdr is unicast.
2459 */
2460 if (memcmp(dev->dev_addr, ss->__data,
2461 4 + sizeof(gid->global.subnet_prefix)) ||
2462 gid->global.interface_id == 0)
2463 ret = -EINVAL;
2464
2465 netif_addr_unlock_bh(dev);
2466
2467 return ret;
2468 }
2469
ipoib_set_mac(struct net_device * dev,void * addr)2470 static int ipoib_set_mac(struct net_device *dev, void *addr)
2471 {
2472 struct ipoib_dev_priv *priv = ipoib_priv(dev);
2473 struct sockaddr_storage *ss = addr;
2474 int ret;
2475
2476 if (!(dev->priv_flags & IFF_LIVE_ADDR_CHANGE) && netif_running(dev))
2477 return -EBUSY;
2478
2479 ret = ipoib_check_lladdr(dev, ss);
2480 if (ret)
2481 return ret;
2482
2483 set_base_guid(priv, (union ib_gid *)(ss->__data + 4));
2484
2485 if (!test_bit(IPOIB_FLAG_SUBINTERFACE, &priv->flags)) {
2486 struct ipoib_dev_priv *cpriv;
2487
2488 netdev_lock_ops_to_full(dev);
2489 list_for_each_entry(cpriv, &priv->child_intfs, list)
2490 queue_work(ipoib_workqueue, &cpriv->flush_light);
2491 netdev_unlock_full_to_ops(dev);
2492 }
2493 queue_work(ipoib_workqueue, &priv->flush_light);
2494
2495 return 0;
2496 }
2497
create_child_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2498 static ssize_t create_child_store(struct device *dev,
2499 struct device_attribute *attr,
2500 const char *buf, size_t count)
2501 {
2502 int pkey;
2503 int ret;
2504
2505 if (sscanf(buf, "%i", &pkey) != 1)
2506 return -EINVAL;
2507
2508 if (pkey <= 0 || pkey > 0xffff || pkey == 0x8000)
2509 return -EINVAL;
2510
2511 ret = ipoib_vlan_add(to_net_dev(dev), pkey);
2512
2513 return ret ? ret : count;
2514 }
2515 static DEVICE_ATTR_WO(create_child);
2516
delete_child_store(struct device * dev,struct device_attribute * attr,const char * buf,size_t count)2517 static ssize_t delete_child_store(struct device *dev,
2518 struct device_attribute *attr,
2519 const char *buf, size_t count)
2520 {
2521 int pkey;
2522 int ret;
2523
2524 if (sscanf(buf, "%i", &pkey) != 1)
2525 return -EINVAL;
2526
2527 if (pkey < 0 || pkey > 0xffff)
2528 return -EINVAL;
2529
2530 ret = ipoib_vlan_delete(to_net_dev(dev), pkey);
2531
2532 return ret ? ret : count;
2533
2534 }
2535 static DEVICE_ATTR_WO(delete_child);
2536
ipoib_add_pkey_attr(struct net_device * dev)2537 int ipoib_add_pkey_attr(struct net_device *dev)
2538 {
2539 return device_create_file(&dev->dev, &dev_attr_pkey);
2540 }
2541
2542 /*
2543 * We erroneously exposed the iface's port number in the dev_id
2544 * sysfs field long after dev_port was introduced for that purpose[1],
2545 * and we need to stop everyone from relying on that.
2546 * Let's overload the shower routine for the dev_id file here
2547 * to gently bring the issue up.
2548 *
2549 * [1] https://www.spinics.net/lists/netdev/msg272123.html
2550 */
dev_id_show(struct device * dev,struct device_attribute * attr,char * buf)2551 static ssize_t dev_id_show(struct device *dev,
2552 struct device_attribute *attr, char *buf)
2553 {
2554 struct net_device *ndev = to_net_dev(dev);
2555
2556 /*
2557 * ndev->dev_port will be equal to 0 in old kernel prior to commit
2558 * 9b8b2a323008 ("IB/ipoib: Use dev_port to expose network interface
2559 * port numbers") Zero was chosen as special case for user space
2560 * applications to fallback and query dev_id to check if it has
2561 * different value or not.
2562 *
2563 * Don't print warning in such scenario.
2564 *
2565 * https://github.com/systemd/systemd/blob/master/src/udev/udev-builtin-net_id.c#L358
2566 */
2567 if (ndev->dev_port && ndev->dev_id == ndev->dev_port)
2568 netdev_info_once(ndev,
2569 "\"%s\" wants to know my dev_id. Should it look at dev_port instead? See Documentation/ABI/testing/sysfs-class-net for more info.\n",
2570 current->comm);
2571
2572 return sysfs_emit(buf, "%#x\n", ndev->dev_id);
2573 }
2574 static DEVICE_ATTR_RO(dev_id);
2575
ipoib_intercept_dev_id_attr(struct net_device * dev)2576 static int ipoib_intercept_dev_id_attr(struct net_device *dev)
2577 {
2578 device_remove_file(&dev->dev, &dev_attr_dev_id);
2579 return device_create_file(&dev->dev, &dev_attr_dev_id);
2580 }
2581
ipoib_add_port(const char * format,struct ib_device * hca,u32 port)2582 static struct net_device *ipoib_add_port(const char *format,
2583 struct ib_device *hca, u32 port)
2584 {
2585 struct rtnl_link_ops *ops = ipoib_get_link_ops();
2586 struct rdma_netdev_alloc_params params;
2587 struct ipoib_dev_priv *priv;
2588 struct net_device *ndev;
2589 int result;
2590
2591 ndev = ipoib_intf_alloc(hca, port, format);
2592 if (IS_ERR(ndev)) {
2593 pr_warn("%s, %d: ipoib_intf_alloc failed %ld\n", hca->name, port,
2594 PTR_ERR(ndev));
2595 return ndev;
2596 }
2597 priv = ipoib_priv(ndev);
2598
2599 INIT_IB_EVENT_HANDLER(&priv->event_handler,
2600 priv->ca, ipoib_event);
2601 ib_register_event_handler(&priv->event_handler);
2602
2603 /* call event handler to ensure pkey in sync */
2604 ipoib_queue_work(priv, IPOIB_FLUSH_HEAVY);
2605
2606 ndev->rtnl_link_ops = ipoib_get_link_ops();
2607
2608 dev_net_set(ndev, rdma_dev_net(hca));
2609
2610 result = register_netdev(ndev);
2611 if (result) {
2612 pr_warn("%s: couldn't register ipoib port %d; error %d\n",
2613 hca->name, port, result);
2614
2615 ipoib_parent_unregister_pre(ndev);
2616 ipoib_intf_free(ndev);
2617 free_netdev(ndev);
2618
2619 return ERR_PTR(result);
2620 }
2621
2622 if (hca->ops.rdma_netdev_get_params) {
2623 int rc = hca->ops.rdma_netdev_get_params(hca, port,
2624 RDMA_NETDEV_IPOIB,
2625 ¶ms);
2626
2627 if (!rc && ops->priv_size < params.sizeof_priv)
2628 ops->priv_size = params.sizeof_priv;
2629 }
2630 /*
2631 * We cannot set priv_destructor before register_netdev because we
2632 * need priv to be always valid during the error flow to execute
2633 * ipoib_parent_unregister_pre(). Instead handle it manually and only
2634 * enter priv_destructor mode once we are completely registered.
2635 */
2636 ndev->priv_destructor = ipoib_intf_free;
2637
2638 if (ipoib_intercept_dev_id_attr(ndev))
2639 goto sysfs_failed;
2640 if (ipoib_cm_add_mode_attr(ndev))
2641 goto sysfs_failed;
2642 if (ipoib_add_pkey_attr(ndev))
2643 goto sysfs_failed;
2644 if (ipoib_add_umcast_attr(ndev))
2645 goto sysfs_failed;
2646 if (device_create_file(&ndev->dev, &dev_attr_create_child))
2647 goto sysfs_failed;
2648 if (device_create_file(&ndev->dev, &dev_attr_delete_child))
2649 goto sysfs_failed;
2650
2651 return ndev;
2652
2653 sysfs_failed:
2654 ipoib_parent_unregister_pre(ndev);
2655 unregister_netdev(ndev);
2656 return ERR_PTR(-ENOMEM);
2657 }
2658
ipoib_add_one(struct ib_device * device)2659 static int ipoib_add_one(struct ib_device *device)
2660 {
2661 struct list_head *dev_list;
2662 struct net_device *dev;
2663 struct ipoib_dev_priv *priv;
2664 unsigned int p;
2665 int count = 0;
2666
2667 dev_list = kmalloc_obj(*dev_list);
2668 if (!dev_list)
2669 return -ENOMEM;
2670
2671 INIT_LIST_HEAD(dev_list);
2672
2673 rdma_for_each_port (device, p) {
2674 if (!rdma_protocol_ib(device, p))
2675 continue;
2676 dev = ipoib_add_port("ib%d", device, p);
2677 if (!IS_ERR(dev)) {
2678 priv = ipoib_priv(dev);
2679 list_add_tail(&priv->list, dev_list);
2680 count++;
2681 }
2682 }
2683
2684 if (!count) {
2685 kfree(dev_list);
2686 return -EOPNOTSUPP;
2687 }
2688
2689 ib_set_client_data(device, &ipoib_client, dev_list);
2690 return 0;
2691 }
2692
ipoib_remove_one(struct ib_device * device,void * client_data)2693 static void ipoib_remove_one(struct ib_device *device, void *client_data)
2694 {
2695 struct ipoib_dev_priv *priv, *tmp, *cpriv, *tcpriv;
2696 struct list_head *dev_list = client_data;
2697
2698 list_for_each_entry_safe(priv, tmp, dev_list, list) {
2699 LIST_HEAD(head);
2700 ipoib_parent_unregister_pre(priv->dev);
2701
2702 rtnl_lock();
2703
2704 netdev_lock(priv->dev);
2705 list_for_each_entry_safe(cpriv, tcpriv, &priv->child_intfs,
2706 list)
2707 unregister_netdevice_queue(cpriv->dev, &head);
2708 netdev_unlock(priv->dev);
2709 unregister_netdevice_queue(priv->dev, &head);
2710 unregister_netdevice_many(&head);
2711
2712 rtnl_unlock();
2713 }
2714
2715 kfree(dev_list);
2716 }
2717
2718 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2719 static struct notifier_block ipoib_netdev_notifier = {
2720 .notifier_call = ipoib_netdev_event,
2721 };
2722 #endif
2723
ipoib_init_module(void)2724 static int __init ipoib_init_module(void)
2725 {
2726 int ret;
2727
2728 ipoib_recvq_size = roundup_pow_of_two(ipoib_recvq_size);
2729 ipoib_recvq_size = min(ipoib_recvq_size, IPOIB_MAX_QUEUE_SIZE);
2730 ipoib_recvq_size = max(ipoib_recvq_size, IPOIB_MIN_QUEUE_SIZE);
2731
2732 ipoib_sendq_size = roundup_pow_of_two(ipoib_sendq_size);
2733 ipoib_sendq_size = min(ipoib_sendq_size, IPOIB_MAX_QUEUE_SIZE);
2734 ipoib_sendq_size = max3(ipoib_sendq_size, 2 * MAX_SEND_CQE, IPOIB_MIN_QUEUE_SIZE);
2735 #ifdef CONFIG_INFINIBAND_IPOIB_CM
2736 ipoib_max_conn_qp = min(ipoib_max_conn_qp, IPOIB_CM_MAX_CONN_QP);
2737 ipoib_max_conn_qp = max(ipoib_max_conn_qp, 0);
2738 #endif
2739
2740 /*
2741 * When copying small received packets, we only copy from the
2742 * linear data part of the SKB, so we rely on this condition.
2743 */
2744 BUILD_BUG_ON(IPOIB_CM_COPYBREAK > IPOIB_CM_HEAD_SIZE);
2745
2746 ipoib_register_debugfs();
2747
2748 /*
2749 * We create a global workqueue here that is used for all flush
2750 * operations. However, if you attempt to flush a workqueue
2751 * from a task on that same workqueue, it deadlocks the system.
2752 * We want to be able to flush the tasks associated with a
2753 * specific net device, so we also create a workqueue for each
2754 * netdevice. We queue up the tasks for that device only on
2755 * its private workqueue, and we only queue up flush events
2756 * on our global flush workqueue. This avoids the deadlocks.
2757 */
2758 ipoib_workqueue = alloc_ordered_workqueue("ipoib_flush", 0);
2759 if (!ipoib_workqueue) {
2760 ret = -ENOMEM;
2761 goto err_fs;
2762 }
2763
2764 ib_sa_register_client(&ipoib_sa_client);
2765
2766 ret = ib_register_client(&ipoib_client);
2767 if (ret)
2768 goto err_sa;
2769
2770 ret = ipoib_netlink_init();
2771 if (ret)
2772 goto err_client;
2773
2774 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2775 register_netdevice_notifier(&ipoib_netdev_notifier);
2776 #endif
2777 return 0;
2778
2779 err_client:
2780 ib_unregister_client(&ipoib_client);
2781
2782 err_sa:
2783 ib_sa_unregister_client(&ipoib_sa_client);
2784 destroy_workqueue(ipoib_workqueue);
2785
2786 err_fs:
2787 ipoib_unregister_debugfs();
2788
2789 return ret;
2790 }
2791
ipoib_cleanup_module(void)2792 static void __exit ipoib_cleanup_module(void)
2793 {
2794 #ifdef CONFIG_INFINIBAND_IPOIB_DEBUG
2795 unregister_netdevice_notifier(&ipoib_netdev_notifier);
2796 #endif
2797 ipoib_netlink_fini();
2798 ib_unregister_client(&ipoib_client);
2799 ib_sa_unregister_client(&ipoib_sa_client);
2800 ipoib_unregister_debugfs();
2801 destroy_workqueue(ipoib_workqueue);
2802 }
2803
2804 module_init(ipoib_init_module);
2805 module_exit(ipoib_cleanup_module);
2806