]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blob - drivers/infiniband/core/roce_gid_mgmt.c
Merge tag 'devicetree-for-4.10' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mirror_ubuntu-artful-kernel.git] / drivers / infiniband / core / roce_gid_mgmt.c
1 /*
2 * Copyright (c) 2015, Mellanox Technologies inc. All rights reserved.
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32
33 #include "core_priv.h"
34
35 #include <linux/in.h>
36 #include <linux/in6.h>
37
38 /* For in6_dev_get/in6_dev_put */
39 #include <net/addrconf.h>
40 #include <net/bonding.h>
41
42 #include <rdma/ib_cache.h>
43 #include <rdma/ib_addr.h>
44
45 enum gid_op_type {
46 GID_DEL = 0,
47 GID_ADD
48 };
49
50 struct update_gid_event_work {
51 struct work_struct work;
52 union ib_gid gid;
53 struct ib_gid_attr gid_attr;
54 enum gid_op_type gid_op;
55 };
56
57 #define ROCE_NETDEV_CALLBACK_SZ 3
58 struct netdev_event_work_cmd {
59 roce_netdev_callback cb;
60 roce_netdev_filter filter;
61 struct net_device *ndev;
62 struct net_device *filter_ndev;
63 };
64
65 struct netdev_event_work {
66 struct work_struct work;
67 struct netdev_event_work_cmd cmds[ROCE_NETDEV_CALLBACK_SZ];
68 };
69
70 static const struct {
71 bool (*is_supported)(const struct ib_device *device, u8 port_num);
72 enum ib_gid_type gid_type;
73 } PORT_CAP_TO_GID_TYPE[] = {
74 {rdma_protocol_roce_eth_encap, IB_GID_TYPE_ROCE},
75 {rdma_protocol_roce_udp_encap, IB_GID_TYPE_ROCE_UDP_ENCAP},
76 };
77
78 #define CAP_TO_GID_TABLE_SIZE ARRAY_SIZE(PORT_CAP_TO_GID_TYPE)
79
80 unsigned long roce_gid_type_mask_support(struct ib_device *ib_dev, u8 port)
81 {
82 int i;
83 unsigned int ret_flags = 0;
84
85 if (!rdma_protocol_roce(ib_dev, port))
86 return 1UL << IB_GID_TYPE_IB;
87
88 for (i = 0; i < CAP_TO_GID_TABLE_SIZE; i++)
89 if (PORT_CAP_TO_GID_TYPE[i].is_supported(ib_dev, port))
90 ret_flags |= 1UL << PORT_CAP_TO_GID_TYPE[i].gid_type;
91
92 return ret_flags;
93 }
94 EXPORT_SYMBOL(roce_gid_type_mask_support);
95
96 static void update_gid(enum gid_op_type gid_op, struct ib_device *ib_dev,
97 u8 port, union ib_gid *gid,
98 struct ib_gid_attr *gid_attr)
99 {
100 int i;
101 unsigned long gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
102
103 for (i = 0; i < IB_GID_TYPE_SIZE; i++) {
104 if ((1UL << i) & gid_type_mask) {
105 gid_attr->gid_type = i;
106 switch (gid_op) {
107 case GID_ADD:
108 ib_cache_gid_add(ib_dev, port,
109 gid, gid_attr);
110 break;
111 case GID_DEL:
112 ib_cache_gid_del(ib_dev, port,
113 gid, gid_attr);
114 break;
115 }
116 }
117 }
118 }
119
120 enum bonding_slave_state {
121 BONDING_SLAVE_STATE_ACTIVE = 1UL << 0,
122 BONDING_SLAVE_STATE_INACTIVE = 1UL << 1,
123 /* No primary slave or the device isn't a slave in bonding */
124 BONDING_SLAVE_STATE_NA = 1UL << 2,
125 };
126
127 static enum bonding_slave_state is_eth_active_slave_of_bonding_rcu(struct net_device *dev,
128 struct net_device *upper)
129 {
130 if (upper && netif_is_bond_master(upper)) {
131 struct net_device *pdev =
132 bond_option_active_slave_get_rcu(netdev_priv(upper));
133
134 if (pdev)
135 return dev == pdev ? BONDING_SLAVE_STATE_ACTIVE :
136 BONDING_SLAVE_STATE_INACTIVE;
137 }
138
139 return BONDING_SLAVE_STATE_NA;
140 }
141
142 #define REQUIRED_BOND_STATES (BONDING_SLAVE_STATE_ACTIVE | \
143 BONDING_SLAVE_STATE_NA)
144 static int is_eth_port_of_netdev(struct ib_device *ib_dev, u8 port,
145 struct net_device *rdma_ndev, void *cookie)
146 {
147 struct net_device *event_ndev = (struct net_device *)cookie;
148 struct net_device *real_dev;
149 int res;
150
151 if (!rdma_ndev)
152 return 0;
153
154 rcu_read_lock();
155 real_dev = rdma_vlan_dev_real_dev(event_ndev);
156 if (!real_dev)
157 real_dev = event_ndev;
158
159 res = ((rdma_is_upper_dev_rcu(rdma_ndev, event_ndev) &&
160 (is_eth_active_slave_of_bonding_rcu(rdma_ndev, real_dev) &
161 REQUIRED_BOND_STATES)) ||
162 real_dev == rdma_ndev);
163
164 rcu_read_unlock();
165 return res;
166 }
167
168 static int is_eth_port_inactive_slave(struct ib_device *ib_dev, u8 port,
169 struct net_device *rdma_ndev, void *cookie)
170 {
171 struct net_device *master_dev;
172 int res;
173
174 if (!rdma_ndev)
175 return 0;
176
177 rcu_read_lock();
178 master_dev = netdev_master_upper_dev_get_rcu(rdma_ndev);
179 res = is_eth_active_slave_of_bonding_rcu(rdma_ndev, master_dev) ==
180 BONDING_SLAVE_STATE_INACTIVE;
181 rcu_read_unlock();
182
183 return res;
184 }
185
186 static int pass_all_filter(struct ib_device *ib_dev, u8 port,
187 struct net_device *rdma_ndev, void *cookie)
188 {
189 return 1;
190 }
191
192 static int upper_device_filter(struct ib_device *ib_dev, u8 port,
193 struct net_device *rdma_ndev, void *cookie)
194 {
195 struct net_device *event_ndev = (struct net_device *)cookie;
196 int res;
197
198 if (!rdma_ndev)
199 return 0;
200
201 if (rdma_ndev == event_ndev)
202 return 1;
203
204 rcu_read_lock();
205 res = rdma_is_upper_dev_rcu(rdma_ndev, event_ndev);
206 rcu_read_unlock();
207
208 return res;
209 }
210
211 static void update_gid_ip(enum gid_op_type gid_op,
212 struct ib_device *ib_dev,
213 u8 port, struct net_device *ndev,
214 struct sockaddr *addr)
215 {
216 union ib_gid gid;
217 struct ib_gid_attr gid_attr;
218
219 rdma_ip2gid(addr, &gid);
220 memset(&gid_attr, 0, sizeof(gid_attr));
221 gid_attr.ndev = ndev;
222
223 update_gid(gid_op, ib_dev, port, &gid, &gid_attr);
224 }
225
226 static void enum_netdev_default_gids(struct ib_device *ib_dev,
227 u8 port, struct net_device *event_ndev,
228 struct net_device *rdma_ndev)
229 {
230 unsigned long gid_type_mask;
231
232 rcu_read_lock();
233 if (!rdma_ndev ||
234 ((rdma_ndev != event_ndev &&
235 !rdma_is_upper_dev_rcu(rdma_ndev, event_ndev)) ||
236 is_eth_active_slave_of_bonding_rcu(rdma_ndev,
237 netdev_master_upper_dev_get_rcu(rdma_ndev)) ==
238 BONDING_SLAVE_STATE_INACTIVE)) {
239 rcu_read_unlock();
240 return;
241 }
242 rcu_read_unlock();
243
244 gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
245
246 ib_cache_gid_set_default_gid(ib_dev, port, rdma_ndev, gid_type_mask,
247 IB_CACHE_GID_DEFAULT_MODE_SET);
248 }
249
250 static void bond_delete_netdev_default_gids(struct ib_device *ib_dev,
251 u8 port,
252 struct net_device *event_ndev,
253 struct net_device *rdma_ndev)
254 {
255 struct net_device *real_dev = rdma_vlan_dev_real_dev(event_ndev);
256
257 if (!rdma_ndev)
258 return;
259
260 if (!real_dev)
261 real_dev = event_ndev;
262
263 rcu_read_lock();
264
265 if (rdma_is_upper_dev_rcu(rdma_ndev, event_ndev) &&
266 is_eth_active_slave_of_bonding_rcu(rdma_ndev, real_dev) ==
267 BONDING_SLAVE_STATE_INACTIVE) {
268 unsigned long gid_type_mask;
269
270 rcu_read_unlock();
271
272 gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
273
274 ib_cache_gid_set_default_gid(ib_dev, port, rdma_ndev,
275 gid_type_mask,
276 IB_CACHE_GID_DEFAULT_MODE_DELETE);
277 } else {
278 rcu_read_unlock();
279 }
280 }
281
282 static void enum_netdev_ipv4_ips(struct ib_device *ib_dev,
283 u8 port, struct net_device *ndev)
284 {
285 struct in_device *in_dev;
286 struct sin_list {
287 struct list_head list;
288 struct sockaddr_in ip;
289 };
290 struct sin_list *sin_iter;
291 struct sin_list *sin_temp;
292
293 LIST_HEAD(sin_list);
294 if (ndev->reg_state >= NETREG_UNREGISTERING)
295 return;
296
297 rcu_read_lock();
298 in_dev = __in_dev_get_rcu(ndev);
299 if (!in_dev) {
300 rcu_read_unlock();
301 return;
302 }
303
304 for_ifa(in_dev) {
305 struct sin_list *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
306
307 if (!entry) {
308 pr_warn("roce_gid_mgmt: couldn't allocate entry for IPv4 update\n");
309 continue;
310 }
311 entry->ip.sin_family = AF_INET;
312 entry->ip.sin_addr.s_addr = ifa->ifa_address;
313 list_add_tail(&entry->list, &sin_list);
314 }
315 endfor_ifa(in_dev);
316 rcu_read_unlock();
317
318 list_for_each_entry_safe(sin_iter, sin_temp, &sin_list, list) {
319 update_gid_ip(GID_ADD, ib_dev, port, ndev,
320 (struct sockaddr *)&sin_iter->ip);
321 list_del(&sin_iter->list);
322 kfree(sin_iter);
323 }
324 }
325
326 static void enum_netdev_ipv6_ips(struct ib_device *ib_dev,
327 u8 port, struct net_device *ndev)
328 {
329 struct inet6_ifaddr *ifp;
330 struct inet6_dev *in6_dev;
331 struct sin6_list {
332 struct list_head list;
333 struct sockaddr_in6 sin6;
334 };
335 struct sin6_list *sin6_iter;
336 struct sin6_list *sin6_temp;
337 struct ib_gid_attr gid_attr = {.ndev = ndev};
338 LIST_HEAD(sin6_list);
339
340 if (ndev->reg_state >= NETREG_UNREGISTERING)
341 return;
342
343 in6_dev = in6_dev_get(ndev);
344 if (!in6_dev)
345 return;
346
347 read_lock_bh(&in6_dev->lock);
348 list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
349 struct sin6_list *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
350
351 if (!entry) {
352 pr_warn("roce_gid_mgmt: couldn't allocate entry for IPv6 update\n");
353 continue;
354 }
355
356 entry->sin6.sin6_family = AF_INET6;
357 entry->sin6.sin6_addr = ifp->addr;
358 list_add_tail(&entry->list, &sin6_list);
359 }
360 read_unlock_bh(&in6_dev->lock);
361
362 in6_dev_put(in6_dev);
363
364 list_for_each_entry_safe(sin6_iter, sin6_temp, &sin6_list, list) {
365 union ib_gid gid;
366
367 rdma_ip2gid((struct sockaddr *)&sin6_iter->sin6, &gid);
368 update_gid(GID_ADD, ib_dev, port, &gid, &gid_attr);
369 list_del(&sin6_iter->list);
370 kfree(sin6_iter);
371 }
372 }
373
374 static void _add_netdev_ips(struct ib_device *ib_dev, u8 port,
375 struct net_device *ndev)
376 {
377 enum_netdev_ipv4_ips(ib_dev, port, ndev);
378 if (IS_ENABLED(CONFIG_IPV6))
379 enum_netdev_ipv6_ips(ib_dev, port, ndev);
380 }
381
382 static void add_netdev_ips(struct ib_device *ib_dev, u8 port,
383 struct net_device *rdma_ndev, void *cookie)
384 {
385 struct net_device *event_ndev = (struct net_device *)cookie;
386
387 enum_netdev_default_gids(ib_dev, port, event_ndev, rdma_ndev);
388 _add_netdev_ips(ib_dev, port, event_ndev);
389 }
390
391 static void del_netdev_ips(struct ib_device *ib_dev, u8 port,
392 struct net_device *rdma_ndev, void *cookie)
393 {
394 struct net_device *event_ndev = (struct net_device *)cookie;
395
396 ib_cache_gid_del_all_netdev_gids(ib_dev, port, event_ndev);
397 }
398
399 static void enum_all_gids_of_dev_cb(struct ib_device *ib_dev,
400 u8 port,
401 struct net_device *rdma_ndev,
402 void *cookie)
403 {
404 struct net *net;
405 struct net_device *ndev;
406
407 /* Lock the rtnl to make sure the netdevs does not move under
408 * our feet
409 */
410 rtnl_lock();
411 for_each_net(net)
412 for_each_netdev(net, ndev)
413 if (is_eth_port_of_netdev(ib_dev, port, rdma_ndev, ndev))
414 add_netdev_ips(ib_dev, port, rdma_ndev, ndev);
415 rtnl_unlock();
416 }
417
418 /* This function will rescan all of the network devices in the system
419 * and add their gids, as needed, to the relevant RoCE devices. */
420 int roce_rescan_device(struct ib_device *ib_dev)
421 {
422 ib_enum_roce_netdev(ib_dev, pass_all_filter, NULL,
423 enum_all_gids_of_dev_cb, NULL);
424
425 return 0;
426 }
427
428 static void callback_for_addr_gid_device_scan(struct ib_device *device,
429 u8 port,
430 struct net_device *rdma_ndev,
431 void *cookie)
432 {
433 struct update_gid_event_work *parsed = cookie;
434
435 return update_gid(parsed->gid_op, device,
436 port, &parsed->gid,
437 &parsed->gid_attr);
438 }
439
440 struct upper_list {
441 struct list_head list;
442 struct net_device *upper;
443 };
444
445 static int netdev_upper_walk(struct net_device *upper, void *data)
446 {
447 struct upper_list *entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
448 struct list_head *upper_list = data;
449
450 if (!entry) {
451 pr_info("roce_gid_mgmt: couldn't allocate entry to delete ndev\n");
452 return 0;
453 }
454
455 list_add_tail(&entry->list, upper_list);
456 dev_hold(upper);
457 entry->upper = upper;
458
459 return 0;
460 }
461
462 static void handle_netdev_upper(struct ib_device *ib_dev, u8 port,
463 void *cookie,
464 void (*handle_netdev)(struct ib_device *ib_dev,
465 u8 port,
466 struct net_device *ndev))
467 {
468 struct net_device *ndev = (struct net_device *)cookie;
469 struct upper_list *upper_iter;
470 struct upper_list *upper_temp;
471 LIST_HEAD(upper_list);
472
473 rcu_read_lock();
474 netdev_walk_all_upper_dev_rcu(ndev, netdev_upper_walk, &upper_list);
475 rcu_read_unlock();
476
477 handle_netdev(ib_dev, port, ndev);
478 list_for_each_entry_safe(upper_iter, upper_temp, &upper_list,
479 list) {
480 handle_netdev(ib_dev, port, upper_iter->upper);
481 dev_put(upper_iter->upper);
482 list_del(&upper_iter->list);
483 kfree(upper_iter);
484 }
485 }
486
487 static void _roce_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
488 struct net_device *event_ndev)
489 {
490 ib_cache_gid_del_all_netdev_gids(ib_dev, port, event_ndev);
491 }
492
493 static void del_netdev_upper_ips(struct ib_device *ib_dev, u8 port,
494 struct net_device *rdma_ndev, void *cookie)
495 {
496 handle_netdev_upper(ib_dev, port, cookie, _roce_del_all_netdev_gids);
497 }
498
499 static void add_netdev_upper_ips(struct ib_device *ib_dev, u8 port,
500 struct net_device *rdma_ndev, void *cookie)
501 {
502 handle_netdev_upper(ib_dev, port, cookie, _add_netdev_ips);
503 }
504
505 static void del_netdev_default_ips_join(struct ib_device *ib_dev, u8 port,
506 struct net_device *rdma_ndev,
507 void *cookie)
508 {
509 struct net_device *master_ndev;
510
511 rcu_read_lock();
512 master_ndev = netdev_master_upper_dev_get_rcu(rdma_ndev);
513 if (master_ndev)
514 dev_hold(master_ndev);
515 rcu_read_unlock();
516
517 if (master_ndev) {
518 bond_delete_netdev_default_gids(ib_dev, port, master_ndev,
519 rdma_ndev);
520 dev_put(master_ndev);
521 }
522 }
523
524 static void del_netdev_default_ips(struct ib_device *ib_dev, u8 port,
525 struct net_device *rdma_ndev, void *cookie)
526 {
527 struct net_device *event_ndev = (struct net_device *)cookie;
528
529 bond_delete_netdev_default_gids(ib_dev, port, event_ndev, rdma_ndev);
530 }
531
532 /* The following functions operate on all IB devices. netdevice_event and
533 * addr_event execute ib_enum_all_roce_netdevs through a work.
534 * ib_enum_all_roce_netdevs iterates through all IB devices.
535 */
536
537 static void netdevice_event_work_handler(struct work_struct *_work)
538 {
539 struct netdev_event_work *work =
540 container_of(_work, struct netdev_event_work, work);
541 unsigned int i;
542
543 for (i = 0; i < ARRAY_SIZE(work->cmds) && work->cmds[i].cb; i++) {
544 ib_enum_all_roce_netdevs(work->cmds[i].filter,
545 work->cmds[i].filter_ndev,
546 work->cmds[i].cb,
547 work->cmds[i].ndev);
548 dev_put(work->cmds[i].ndev);
549 dev_put(work->cmds[i].filter_ndev);
550 }
551
552 kfree(work);
553 }
554
555 static int netdevice_queue_work(struct netdev_event_work_cmd *cmds,
556 struct net_device *ndev)
557 {
558 unsigned int i;
559 struct netdev_event_work *ndev_work =
560 kmalloc(sizeof(*ndev_work), GFP_KERNEL);
561
562 if (!ndev_work) {
563 pr_warn("roce_gid_mgmt: can't allocate work for netdevice_event\n");
564 return NOTIFY_DONE;
565 }
566
567 memcpy(ndev_work->cmds, cmds, sizeof(ndev_work->cmds));
568 for (i = 0; i < ARRAY_SIZE(ndev_work->cmds) && ndev_work->cmds[i].cb; i++) {
569 if (!ndev_work->cmds[i].ndev)
570 ndev_work->cmds[i].ndev = ndev;
571 if (!ndev_work->cmds[i].filter_ndev)
572 ndev_work->cmds[i].filter_ndev = ndev;
573 dev_hold(ndev_work->cmds[i].ndev);
574 dev_hold(ndev_work->cmds[i].filter_ndev);
575 }
576 INIT_WORK(&ndev_work->work, netdevice_event_work_handler);
577
578 queue_work(ib_wq, &ndev_work->work);
579
580 return NOTIFY_DONE;
581 }
582
583 static const struct netdev_event_work_cmd add_cmd = {
584 .cb = add_netdev_ips, .filter = is_eth_port_of_netdev};
585 static const struct netdev_event_work_cmd add_cmd_upper_ips = {
586 .cb = add_netdev_upper_ips, .filter = is_eth_port_of_netdev};
587
588 static void netdevice_event_changeupper(struct netdev_notifier_changeupper_info *changeupper_info,
589 struct netdev_event_work_cmd *cmds)
590 {
591 static const struct netdev_event_work_cmd upper_ips_del_cmd = {
592 .cb = del_netdev_upper_ips, .filter = upper_device_filter};
593 static const struct netdev_event_work_cmd bonding_default_del_cmd = {
594 .cb = del_netdev_default_ips, .filter = is_eth_port_inactive_slave};
595
596 if (changeupper_info->linking == false) {
597 cmds[0] = upper_ips_del_cmd;
598 cmds[0].ndev = changeupper_info->upper_dev;
599 cmds[1] = add_cmd;
600 } else {
601 cmds[0] = bonding_default_del_cmd;
602 cmds[0].ndev = changeupper_info->upper_dev;
603 cmds[1] = add_cmd_upper_ips;
604 cmds[1].ndev = changeupper_info->upper_dev;
605 cmds[1].filter_ndev = changeupper_info->upper_dev;
606 }
607 }
608
609 static int netdevice_event(struct notifier_block *this, unsigned long event,
610 void *ptr)
611 {
612 static const struct netdev_event_work_cmd del_cmd = {
613 .cb = del_netdev_ips, .filter = pass_all_filter};
614 static const struct netdev_event_work_cmd bonding_default_del_cmd_join = {
615 .cb = del_netdev_default_ips_join, .filter = is_eth_port_inactive_slave};
616 static const struct netdev_event_work_cmd default_del_cmd = {
617 .cb = del_netdev_default_ips, .filter = pass_all_filter};
618 static const struct netdev_event_work_cmd bonding_event_ips_del_cmd = {
619 .cb = del_netdev_upper_ips, .filter = upper_device_filter};
620 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
621 struct netdev_event_work_cmd cmds[ROCE_NETDEV_CALLBACK_SZ] = { {NULL} };
622
623 if (ndev->type != ARPHRD_ETHER)
624 return NOTIFY_DONE;
625
626 switch (event) {
627 case NETDEV_REGISTER:
628 case NETDEV_UP:
629 cmds[0] = bonding_default_del_cmd_join;
630 cmds[1] = add_cmd;
631 break;
632
633 case NETDEV_UNREGISTER:
634 if (ndev->reg_state < NETREG_UNREGISTERED)
635 cmds[0] = del_cmd;
636 else
637 return NOTIFY_DONE;
638 break;
639
640 case NETDEV_CHANGEADDR:
641 cmds[0] = default_del_cmd;
642 cmds[1] = add_cmd;
643 break;
644
645 case NETDEV_CHANGEUPPER:
646 netdevice_event_changeupper(
647 container_of(ptr, struct netdev_notifier_changeupper_info, info),
648 cmds);
649 break;
650
651 case NETDEV_BONDING_FAILOVER:
652 cmds[0] = bonding_event_ips_del_cmd;
653 cmds[1] = bonding_default_del_cmd_join;
654 cmds[2] = add_cmd_upper_ips;
655 break;
656
657 default:
658 return NOTIFY_DONE;
659 }
660
661 return netdevice_queue_work(cmds, ndev);
662 }
663
664 static void update_gid_event_work_handler(struct work_struct *_work)
665 {
666 struct update_gid_event_work *work =
667 container_of(_work, struct update_gid_event_work, work);
668
669 ib_enum_all_roce_netdevs(is_eth_port_of_netdev, work->gid_attr.ndev,
670 callback_for_addr_gid_device_scan, work);
671
672 dev_put(work->gid_attr.ndev);
673 kfree(work);
674 }
675
676 static int addr_event(struct notifier_block *this, unsigned long event,
677 struct sockaddr *sa, struct net_device *ndev)
678 {
679 struct update_gid_event_work *work;
680 enum gid_op_type gid_op;
681
682 if (ndev->type != ARPHRD_ETHER)
683 return NOTIFY_DONE;
684
685 switch (event) {
686 case NETDEV_UP:
687 gid_op = GID_ADD;
688 break;
689
690 case NETDEV_DOWN:
691 gid_op = GID_DEL;
692 break;
693
694 default:
695 return NOTIFY_DONE;
696 }
697
698 work = kmalloc(sizeof(*work), GFP_ATOMIC);
699 if (!work) {
700 pr_warn("roce_gid_mgmt: Couldn't allocate work for addr_event\n");
701 return NOTIFY_DONE;
702 }
703
704 INIT_WORK(&work->work, update_gid_event_work_handler);
705
706 rdma_ip2gid(sa, &work->gid);
707 work->gid_op = gid_op;
708
709 memset(&work->gid_attr, 0, sizeof(work->gid_attr));
710 dev_hold(ndev);
711 work->gid_attr.ndev = ndev;
712
713 queue_work(ib_wq, &work->work);
714
715 return NOTIFY_DONE;
716 }
717
718 static int inetaddr_event(struct notifier_block *this, unsigned long event,
719 void *ptr)
720 {
721 struct sockaddr_in in;
722 struct net_device *ndev;
723 struct in_ifaddr *ifa = ptr;
724
725 in.sin_family = AF_INET;
726 in.sin_addr.s_addr = ifa->ifa_address;
727 ndev = ifa->ifa_dev->dev;
728
729 return addr_event(this, event, (struct sockaddr *)&in, ndev);
730 }
731
732 static int inet6addr_event(struct notifier_block *this, unsigned long event,
733 void *ptr)
734 {
735 struct sockaddr_in6 in6;
736 struct net_device *ndev;
737 struct inet6_ifaddr *ifa6 = ptr;
738
739 in6.sin6_family = AF_INET6;
740 in6.sin6_addr = ifa6->addr;
741 ndev = ifa6->idev->dev;
742
743 return addr_event(this, event, (struct sockaddr *)&in6, ndev);
744 }
745
746 static struct notifier_block nb_netdevice = {
747 .notifier_call = netdevice_event
748 };
749
750 static struct notifier_block nb_inetaddr = {
751 .notifier_call = inetaddr_event
752 };
753
754 static struct notifier_block nb_inet6addr = {
755 .notifier_call = inet6addr_event
756 };
757
758 int __init roce_gid_mgmt_init(void)
759 {
760 register_inetaddr_notifier(&nb_inetaddr);
761 if (IS_ENABLED(CONFIG_IPV6))
762 register_inet6addr_notifier(&nb_inet6addr);
763 /* We relay on the netdevice notifier to enumerate all
764 * existing devices in the system. Register to this notifier
765 * last to make sure we will not miss any IP add/del
766 * callbacks.
767 */
768 register_netdevice_notifier(&nb_netdevice);
769
770 return 0;
771 }
772
773 void __exit roce_gid_mgmt_cleanup(void)
774 {
775 if (IS_ENABLED(CONFIG_IPV6))
776 unregister_inet6addr_notifier(&nb_inet6addr);
777 unregister_inetaddr_notifier(&nb_inetaddr);
778 unregister_netdevice_notifier(&nb_netdevice);
779 /* Ensure all gid deletion tasks complete before we go down,
780 * to avoid any reference to free'd memory. By the time
781 * ib-core is removed, all physical devices have been removed,
782 * so no issue with remaining hardware contexts.
783 */
784 }