]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/infiniband/core/roce_gid_mgmt.c
Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[mirror_ubuntu-artful-kernel.git] / drivers / infiniband / core / roce_gid_mgmt.c
CommitLineData
03db3a2d
MB
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>
238fdf48 40#include <net/bonding.h>
03db3a2d
MB
41
42#include <rdma/ib_cache.h>
43#include <rdma/ib_addr.h>
44
45enum gid_op_type {
46 GID_DEL = 0,
47 GID_ADD
48};
49
50struct 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
238fdf48 57#define ROCE_NETDEV_CALLBACK_SZ 3
03db3a2d
MB
58struct netdev_event_work_cmd {
59 roce_netdev_callback cb;
60 roce_netdev_filter filter;
238fdf48
MB
61 struct net_device *ndev;
62 struct net_device *filter_ndev;
03db3a2d
MB
63};
64
65struct netdev_event_work {
66 struct work_struct work;
67 struct netdev_event_work_cmd cmds[ROCE_NETDEV_CALLBACK_SZ];
03db3a2d
MB
68};
69
b39ffa1d
MB
70static 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[] = {
7766a99f
MB
74 {rdma_protocol_roce_eth_encap, IB_GID_TYPE_ROCE},
75 {rdma_protocol_roce_udp_encap, IB_GID_TYPE_ROCE_UDP_ENCAP},
b39ffa1d
MB
76};
77
78#define CAP_TO_GID_TABLE_SIZE ARRAY_SIZE(PORT_CAP_TO_GID_TYPE)
79
80unsigned 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}
94EXPORT_SYMBOL(roce_gid_type_mask_support);
95
03db3a2d
MB
96static 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{
b39ffa1d
MB
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 }
03db3a2d
MB
117 }
118}
119
238fdf48
MB
120enum 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
127static 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
238fdf48
MB
142#define REQUIRED_BOND_STATES (BONDING_SLAVE_STATE_ACTIVE | \
143 BONDING_SLAVE_STATE_NA)
03db3a2d
MB
144static int is_eth_port_of_netdev(struct ib_device *ib_dev, u8 port,
145 struct net_device *rdma_ndev, void *cookie)
146{
238fdf48 147 struct net_device *event_ndev = (struct net_device *)cookie;
03db3a2d 148 struct net_device *real_dev;
238fdf48
MB
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
6020d7e5 159 res = ((rdma_is_upper_dev_rcu(rdma_ndev, event_ndev) &&
238fdf48
MB
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
168static int is_eth_port_inactive_slave(struct ib_device *ib_dev, u8 port,
169 struct net_device *rdma_ndev, void *cookie)
170{
03db3a2d 171 struct net_device *master_dev;
03db3a2d
MB
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);
238fdf48
MB
179 res = is_eth_active_slave_of_bonding_rcu(rdma_ndev, master_dev) ==
180 BONDING_SLAVE_STATE_INACTIVE;
03db3a2d
MB
181 rcu_read_unlock();
182
183 return res;
184}
185
186static 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
238fdf48
MB
192static 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();
6020d7e5 205 res = rdma_is_upper_dev_rcu(rdma_ndev, event_ndev);
238fdf48
MB
206 rcu_read_unlock();
207
208 return res;
209}
210
03db3a2d
MB
211static 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
226static 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{
b39ffa1d
MB
230 unsigned long gid_type_mask;
231
238fdf48
MB
232 rcu_read_lock();
233 if (!rdma_ndev ||
234 ((rdma_ndev != event_ndev &&
6020d7e5 235 !rdma_is_upper_dev_rcu(rdma_ndev, event_ndev)) ||
238fdf48
MB
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();
03db3a2d 240 return;
238fdf48
MB
241 }
242 rcu_read_unlock();
03db3a2d 243
b39ffa1d
MB
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,
03db3a2d
MB
247 IB_CACHE_GID_DEFAULT_MODE_SET);
248}
249
238fdf48
MB
250static 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
6020d7e5 265 if (rdma_is_upper_dev_rcu(rdma_ndev, event_ndev) &&
238fdf48
MB
266 is_eth_active_slave_of_bonding_rcu(rdma_ndev, real_dev) ==
267 BONDING_SLAVE_STATE_INACTIVE) {
b39ffa1d
MB
268 unsigned long gid_type_mask;
269
238fdf48
MB
270 rcu_read_unlock();
271
b39ffa1d
MB
272 gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
273
238fdf48 274 ib_cache_gid_set_default_gid(ib_dev, port, rdma_ndev,
b39ffa1d 275 gid_type_mask,
238fdf48
MB
276 IB_CACHE_GID_DEFAULT_MODE_DELETE);
277 } else {
278 rcu_read_unlock();
279 }
280}
281
03db3a2d
MB
282static void enum_netdev_ipv4_ips(struct ib_device *ib_dev,
283 u8 port, struct net_device *ndev)
284{
285 struct in_device *in_dev;
39096420
MB
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);
03db3a2d
MB
294 if (ndev->reg_state >= NETREG_UNREGISTERING)
295 return;
296
39096420
MB
297 rcu_read_lock();
298 in_dev = __in_dev_get_rcu(ndev);
299 if (!in_dev) {
300 rcu_read_unlock();
03db3a2d 301 return;
39096420 302 }
03db3a2d
MB
303
304 for_ifa(in_dev) {
39096420
MB
305 struct sin_list *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
306
a0b3455f 307 if (!entry)
39096420 308 continue;
a0b3455f 309
39096420
MB
310 entry->ip.sin_family = AF_INET;
311 entry->ip.sin_addr.s_addr = ifa->ifa_address;
312 list_add_tail(&entry->list, &sin_list);
03db3a2d
MB
313 }
314 endfor_ifa(in_dev);
39096420 315 rcu_read_unlock();
03db3a2d 316
39096420
MB
317 list_for_each_entry_safe(sin_iter, sin_temp, &sin_list, list) {
318 update_gid_ip(GID_ADD, ib_dev, port, ndev,
319 (struct sockaddr *)&sin_iter->ip);
320 list_del(&sin_iter->list);
321 kfree(sin_iter);
322 }
03db3a2d
MB
323}
324
325static void enum_netdev_ipv6_ips(struct ib_device *ib_dev,
326 u8 port, struct net_device *ndev)
327{
328 struct inet6_ifaddr *ifp;
329 struct inet6_dev *in6_dev;
330 struct sin6_list {
331 struct list_head list;
332 struct sockaddr_in6 sin6;
333 };
334 struct sin6_list *sin6_iter;
335 struct sin6_list *sin6_temp;
336 struct ib_gid_attr gid_attr = {.ndev = ndev};
337 LIST_HEAD(sin6_list);
338
339 if (ndev->reg_state >= NETREG_UNREGISTERING)
340 return;
341
342 in6_dev = in6_dev_get(ndev);
343 if (!in6_dev)
344 return;
345
346 read_lock_bh(&in6_dev->lock);
347 list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
348 struct sin6_list *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
349
a0b3455f 350 if (!entry)
03db3a2d 351 continue;
03db3a2d
MB
352
353 entry->sin6.sin6_family = AF_INET6;
354 entry->sin6.sin6_addr = ifp->addr;
355 list_add_tail(&entry->list, &sin6_list);
356 }
357 read_unlock_bh(&in6_dev->lock);
358
359 in6_dev_put(in6_dev);
360
361 list_for_each_entry_safe(sin6_iter, sin6_temp, &sin6_list, list) {
362 union ib_gid gid;
363
364 rdma_ip2gid((struct sockaddr *)&sin6_iter->sin6, &gid);
365 update_gid(GID_ADD, ib_dev, port, &gid, &gid_attr);
366 list_del(&sin6_iter->list);
367 kfree(sin6_iter);
368 }
369}
370
238fdf48
MB
371static void _add_netdev_ips(struct ib_device *ib_dev, u8 port,
372 struct net_device *ndev)
373{
374 enum_netdev_ipv4_ips(ib_dev, port, ndev);
375 if (IS_ENABLED(CONFIG_IPV6))
376 enum_netdev_ipv6_ips(ib_dev, port, ndev);
377}
378
03db3a2d
MB
379static void add_netdev_ips(struct ib_device *ib_dev, u8 port,
380 struct net_device *rdma_ndev, void *cookie)
381{
382 struct net_device *event_ndev = (struct net_device *)cookie;
383
384 enum_netdev_default_gids(ib_dev, port, event_ndev, rdma_ndev);
238fdf48 385 _add_netdev_ips(ib_dev, port, event_ndev);
03db3a2d
MB
386}
387
388static void del_netdev_ips(struct ib_device *ib_dev, u8 port,
389 struct net_device *rdma_ndev, void *cookie)
390{
391 struct net_device *event_ndev = (struct net_device *)cookie;
392
393 ib_cache_gid_del_all_netdev_gids(ib_dev, port, event_ndev);
394}
395
396static void enum_all_gids_of_dev_cb(struct ib_device *ib_dev,
397 u8 port,
398 struct net_device *rdma_ndev,
399 void *cookie)
400{
401 struct net *net;
402 struct net_device *ndev;
403
404 /* Lock the rtnl to make sure the netdevs does not move under
405 * our feet
406 */
407 rtnl_lock();
408 for_each_net(net)
409 for_each_netdev(net, ndev)
410 if (is_eth_port_of_netdev(ib_dev, port, rdma_ndev, ndev))
411 add_netdev_ips(ib_dev, port, rdma_ndev, ndev);
412 rtnl_unlock();
413}
414
415/* This function will rescan all of the network devices in the system
416 * and add their gids, as needed, to the relevant RoCE devices. */
417int roce_rescan_device(struct ib_device *ib_dev)
418{
419 ib_enum_roce_netdev(ib_dev, pass_all_filter, NULL,
420 enum_all_gids_of_dev_cb, NULL);
421
422 return 0;
423}
424
425static void callback_for_addr_gid_device_scan(struct ib_device *device,
426 u8 port,
427 struct net_device *rdma_ndev,
428 void *cookie)
429{
430 struct update_gid_event_work *parsed = cookie;
431
432 return update_gid(parsed->gid_op, device,
433 port, &parsed->gid,
434 &parsed->gid_attr);
435}
436
453d3932
DA
437struct upper_list {
438 struct list_head list;
439 struct net_device *upper;
440};
441
442static int netdev_upper_walk(struct net_device *upper, void *data)
443{
444 struct upper_list *entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
445 struct list_head *upper_list = data;
446
4d5b57e0 447 if (!entry)
453d3932 448 return 0;
453d3932
DA
449
450 list_add_tail(&entry->list, upper_list);
451 dev_hold(upper);
452 entry->upper = upper;
453
454 return 0;
455}
456
238fdf48
MB
457static void handle_netdev_upper(struct ib_device *ib_dev, u8 port,
458 void *cookie,
459 void (*handle_netdev)(struct ib_device *ib_dev,
460 u8 port,
461 struct net_device *ndev))
462{
463 struct net_device *ndev = (struct net_device *)cookie;
238fdf48
MB
464 struct upper_list *upper_iter;
465 struct upper_list *upper_temp;
466 LIST_HEAD(upper_list);
467
468 rcu_read_lock();
453d3932 469 netdev_walk_all_upper_dev_rcu(ndev, netdev_upper_walk, &upper_list);
238fdf48
MB
470 rcu_read_unlock();
471
472 handle_netdev(ib_dev, port, ndev);
473 list_for_each_entry_safe(upper_iter, upper_temp, &upper_list,
474 list) {
475 handle_netdev(ib_dev, port, upper_iter->upper);
476 dev_put(upper_iter->upper);
477 list_del(&upper_iter->list);
478 kfree(upper_iter);
479 }
480}
481
482static void _roce_del_all_netdev_gids(struct ib_device *ib_dev, u8 port,
483 struct net_device *event_ndev)
484{
485 ib_cache_gid_del_all_netdev_gids(ib_dev, port, event_ndev);
486}
487
488static void del_netdev_upper_ips(struct ib_device *ib_dev, u8 port,
489 struct net_device *rdma_ndev, void *cookie)
490{
491 handle_netdev_upper(ib_dev, port, cookie, _roce_del_all_netdev_gids);
492}
493
494static void add_netdev_upper_ips(struct ib_device *ib_dev, u8 port,
495 struct net_device *rdma_ndev, void *cookie)
496{
497 handle_netdev_upper(ib_dev, port, cookie, _add_netdev_ips);
498}
499
500static void del_netdev_default_ips_join(struct ib_device *ib_dev, u8 port,
501 struct net_device *rdma_ndev,
502 void *cookie)
503{
504 struct net_device *master_ndev;
505
506 rcu_read_lock();
507 master_ndev = netdev_master_upper_dev_get_rcu(rdma_ndev);
508 if (master_ndev)
509 dev_hold(master_ndev);
510 rcu_read_unlock();
511
512 if (master_ndev) {
513 bond_delete_netdev_default_gids(ib_dev, port, master_ndev,
514 rdma_ndev);
515 dev_put(master_ndev);
516 }
517}
518
519static void del_netdev_default_ips(struct ib_device *ib_dev, u8 port,
520 struct net_device *rdma_ndev, void *cookie)
521{
522 struct net_device *event_ndev = (struct net_device *)cookie;
523
524 bond_delete_netdev_default_gids(ib_dev, port, event_ndev, rdma_ndev);
525}
526
03db3a2d
MB
527/* The following functions operate on all IB devices. netdevice_event and
528 * addr_event execute ib_enum_all_roce_netdevs through a work.
529 * ib_enum_all_roce_netdevs iterates through all IB devices.
530 */
531
532static void netdevice_event_work_handler(struct work_struct *_work)
533{
534 struct netdev_event_work *work =
535 container_of(_work, struct netdev_event_work, work);
536 unsigned int i;
537
238fdf48
MB
538 for (i = 0; i < ARRAY_SIZE(work->cmds) && work->cmds[i].cb; i++) {
539 ib_enum_all_roce_netdevs(work->cmds[i].filter,
540 work->cmds[i].filter_ndev,
541 work->cmds[i].cb,
542 work->cmds[i].ndev);
543 dev_put(work->cmds[i].ndev);
544 dev_put(work->cmds[i].filter_ndev);
545 }
03db3a2d 546
03db3a2d
MB
547 kfree(work);
548}
549
550static int netdevice_queue_work(struct netdev_event_work_cmd *cmds,
551 struct net_device *ndev)
552{
238fdf48 553 unsigned int i;
03db3a2d
MB
554 struct netdev_event_work *ndev_work =
555 kmalloc(sizeof(*ndev_work), GFP_KERNEL);
556
a0b3455f 557 if (!ndev_work)
03db3a2d 558 return NOTIFY_DONE;
03db3a2d
MB
559
560 memcpy(ndev_work->cmds, cmds, sizeof(ndev_work->cmds));
238fdf48
MB
561 for (i = 0; i < ARRAY_SIZE(ndev_work->cmds) && ndev_work->cmds[i].cb; i++) {
562 if (!ndev_work->cmds[i].ndev)
563 ndev_work->cmds[i].ndev = ndev;
564 if (!ndev_work->cmds[i].filter_ndev)
565 ndev_work->cmds[i].filter_ndev = ndev;
566 dev_hold(ndev_work->cmds[i].ndev);
567 dev_hold(ndev_work->cmds[i].filter_ndev);
568 }
03db3a2d
MB
569 INIT_WORK(&ndev_work->work, netdevice_event_work_handler);
570
571 queue_work(ib_wq, &ndev_work->work);
572
573 return NOTIFY_DONE;
574}
575
238fdf48
MB
576static const struct netdev_event_work_cmd add_cmd = {
577 .cb = add_netdev_ips, .filter = is_eth_port_of_netdev};
578static const struct netdev_event_work_cmd add_cmd_upper_ips = {
579 .cb = add_netdev_upper_ips, .filter = is_eth_port_of_netdev};
580
26d2177e 581static void netdevice_event_changeupper(struct netdev_notifier_changeupper_info *changeupper_info,
238fdf48
MB
582 struct netdev_event_work_cmd *cmds)
583{
584 static const struct netdev_event_work_cmd upper_ips_del_cmd = {
585 .cb = del_netdev_upper_ips, .filter = upper_device_filter};
586 static const struct netdev_event_work_cmd bonding_default_del_cmd = {
587 .cb = del_netdev_default_ips, .filter = is_eth_port_inactive_slave};
588
26d2177e 589 if (changeupper_info->linking == false) {
238fdf48 590 cmds[0] = upper_ips_del_cmd;
26d2177e 591 cmds[0].ndev = changeupper_info->upper_dev;
238fdf48 592 cmds[1] = add_cmd;
26d2177e 593 } else {
238fdf48 594 cmds[0] = bonding_default_del_cmd;
26d2177e 595 cmds[0].ndev = changeupper_info->upper_dev;
238fdf48 596 cmds[1] = add_cmd_upper_ips;
26d2177e
LT
597 cmds[1].ndev = changeupper_info->upper_dev;
598 cmds[1].filter_ndev = changeupper_info->upper_dev;
238fdf48
MB
599 }
600}
601
03db3a2d
MB
602static int netdevice_event(struct notifier_block *this, unsigned long event,
603 void *ptr)
604{
03db3a2d
MB
605 static const struct netdev_event_work_cmd del_cmd = {
606 .cb = del_netdev_ips, .filter = pass_all_filter};
238fdf48
MB
607 static const struct netdev_event_work_cmd bonding_default_del_cmd_join = {
608 .cb = del_netdev_default_ips_join, .filter = is_eth_port_inactive_slave};
609 static const struct netdev_event_work_cmd default_del_cmd = {
610 .cb = del_netdev_default_ips, .filter = pass_all_filter};
611 static const struct netdev_event_work_cmd bonding_event_ips_del_cmd = {
612 .cb = del_netdev_upper_ips, .filter = upper_device_filter};
03db3a2d
MB
613 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
614 struct netdev_event_work_cmd cmds[ROCE_NETDEV_CALLBACK_SZ] = { {NULL} };
615
616 if (ndev->type != ARPHRD_ETHER)
617 return NOTIFY_DONE;
618
619 switch (event) {
620 case NETDEV_REGISTER:
621 case NETDEV_UP:
238fdf48
MB
622 cmds[0] = bonding_default_del_cmd_join;
623 cmds[1] = add_cmd;
03db3a2d
MB
624 break;
625
626 case NETDEV_UNREGISTER:
627 if (ndev->reg_state < NETREG_UNREGISTERED)
628 cmds[0] = del_cmd;
629 else
630 return NOTIFY_DONE;
631 break;
632
633 case NETDEV_CHANGEADDR:
238fdf48 634 cmds[0] = default_del_cmd;
03db3a2d
MB
635 cmds[1] = add_cmd;
636 break;
238fdf48
MB
637
638 case NETDEV_CHANGEUPPER:
639 netdevice_event_changeupper(
26d2177e 640 container_of(ptr, struct netdev_notifier_changeupper_info, info),
238fdf48
MB
641 cmds);
642 break;
643
644 case NETDEV_BONDING_FAILOVER:
645 cmds[0] = bonding_event_ips_del_cmd;
646 cmds[1] = bonding_default_del_cmd_join;
647 cmds[2] = add_cmd_upper_ips;
648 break;
649
03db3a2d
MB
650 default:
651 return NOTIFY_DONE;
652 }
653
654 return netdevice_queue_work(cmds, ndev);
655}
656
657static void update_gid_event_work_handler(struct work_struct *_work)
658{
659 struct update_gid_event_work *work =
660 container_of(_work, struct update_gid_event_work, work);
661
662 ib_enum_all_roce_netdevs(is_eth_port_of_netdev, work->gid_attr.ndev,
663 callback_for_addr_gid_device_scan, work);
664
665 dev_put(work->gid_attr.ndev);
666 kfree(work);
667}
668
669static int addr_event(struct notifier_block *this, unsigned long event,
670 struct sockaddr *sa, struct net_device *ndev)
671{
672 struct update_gid_event_work *work;
673 enum gid_op_type gid_op;
674
675 if (ndev->type != ARPHRD_ETHER)
676 return NOTIFY_DONE;
677
678 switch (event) {
679 case NETDEV_UP:
680 gid_op = GID_ADD;
681 break;
682
683 case NETDEV_DOWN:
684 gid_op = GID_DEL;
685 break;
686
687 default:
688 return NOTIFY_DONE;
689 }
690
691 work = kmalloc(sizeof(*work), GFP_ATOMIC);
a0b3455f 692 if (!work)
03db3a2d 693 return NOTIFY_DONE;
03db3a2d
MB
694
695 INIT_WORK(&work->work, update_gid_event_work_handler);
696
697 rdma_ip2gid(sa, &work->gid);
698 work->gid_op = gid_op;
699
700 memset(&work->gid_attr, 0, sizeof(work->gid_attr));
701 dev_hold(ndev);
702 work->gid_attr.ndev = ndev;
703
704 queue_work(ib_wq, &work->work);
705
706 return NOTIFY_DONE;
707}
708
709static int inetaddr_event(struct notifier_block *this, unsigned long event,
710 void *ptr)
711{
712 struct sockaddr_in in;
713 struct net_device *ndev;
714 struct in_ifaddr *ifa = ptr;
715
716 in.sin_family = AF_INET;
717 in.sin_addr.s_addr = ifa->ifa_address;
718 ndev = ifa->ifa_dev->dev;
719
720 return addr_event(this, event, (struct sockaddr *)&in, ndev);
721}
722
723static int inet6addr_event(struct notifier_block *this, unsigned long event,
724 void *ptr)
725{
726 struct sockaddr_in6 in6;
727 struct net_device *ndev;
728 struct inet6_ifaddr *ifa6 = ptr;
729
730 in6.sin6_family = AF_INET6;
731 in6.sin6_addr = ifa6->addr;
732 ndev = ifa6->idev->dev;
733
734 return addr_event(this, event, (struct sockaddr *)&in6, ndev);
735}
736
737static struct notifier_block nb_netdevice = {
738 .notifier_call = netdevice_event
739};
740
741static struct notifier_block nb_inetaddr = {
742 .notifier_call = inetaddr_event
743};
744
745static struct notifier_block nb_inet6addr = {
746 .notifier_call = inet6addr_event
747};
748
749int __init roce_gid_mgmt_init(void)
750{
751 register_inetaddr_notifier(&nb_inetaddr);
752 if (IS_ENABLED(CONFIG_IPV6))
753 register_inet6addr_notifier(&nb_inet6addr);
754 /* We relay on the netdevice notifier to enumerate all
755 * existing devices in the system. Register to this notifier
756 * last to make sure we will not miss any IP add/del
757 * callbacks.
758 */
759 register_netdevice_notifier(&nb_netdevice);
760
761 return 0;
762}
763
764void __exit roce_gid_mgmt_cleanup(void)
765{
766 if (IS_ENABLED(CONFIG_IPV6))
767 unregister_inet6addr_notifier(&nb_inet6addr);
768 unregister_inetaddr_notifier(&nb_inetaddr);
769 unregister_netdevice_notifier(&nb_netdevice);
770 /* Ensure all gid deletion tasks complete before we go down,
771 * to avoid any reference to free'd memory. By the time
772 * ib-core is removed, all physical devices have been removed,
773 * so no issue with remaining hardware contexts.
774 */
775}