blob: c0e4fd55e2ccb77104b7a62cdde18440ab9c0f47 [file] [log] [blame]
Matan Barak03db3a22015-07-30 18:33:26 +03001/*
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>
Matan Barak238fdf42015-07-30 18:33:27 +030040#include <net/bonding.h>
Matan Barak03db3a22015-07-30 18:33:26 +030041
42#include <rdma/ib_cache.h>
43#include <rdma/ib_addr.h>
44
Majd Dibbiny8fe8bac2017-05-30 09:58:06 +030045static struct workqueue_struct *gid_cache_wq;
46
Yuval Shaiad4186192017-06-14 23:13:34 +030047static struct workqueue_struct *gid_cache_wq;
48
Matan Barak03db3a22015-07-30 18:33:26 +030049enum gid_op_type {
50 GID_DEL = 0,
51 GID_ADD
52};
53
54struct update_gid_event_work {
55 struct work_struct work;
56 union ib_gid gid;
57 struct ib_gid_attr gid_attr;
58 enum gid_op_type gid_op;
59};
60
Matan Barak238fdf42015-07-30 18:33:27 +030061#define ROCE_NETDEV_CALLBACK_SZ 3
Matan Barak03db3a22015-07-30 18:33:26 +030062struct netdev_event_work_cmd {
63 roce_netdev_callback cb;
64 roce_netdev_filter filter;
Matan Barak238fdf42015-07-30 18:33:27 +030065 struct net_device *ndev;
66 struct net_device *filter_ndev;
Matan Barak03db3a22015-07-30 18:33:26 +030067};
68
69struct netdev_event_work {
70 struct work_struct work;
71 struct netdev_event_work_cmd cmds[ROCE_NETDEV_CALLBACK_SZ];
Matan Barak03db3a22015-07-30 18:33:26 +030072};
73
Matan Barakb39ffa12015-12-23 14:56:47 +020074static const struct {
75 bool (*is_supported)(const struct ib_device *device, u8 port_num);
76 enum ib_gid_type gid_type;
77} PORT_CAP_TO_GID_TYPE[] = {
Matan Barak7766a992015-12-23 14:56:50 +020078 {rdma_protocol_roce_eth_encap, IB_GID_TYPE_ROCE},
79 {rdma_protocol_roce_udp_encap, IB_GID_TYPE_ROCE_UDP_ENCAP},
Matan Barakb39ffa12015-12-23 14:56:47 +020080};
81
82#define CAP_TO_GID_TABLE_SIZE ARRAY_SIZE(PORT_CAP_TO_GID_TYPE)
83
84unsigned long roce_gid_type_mask_support(struct ib_device *ib_dev, u8 port)
85{
86 int i;
87 unsigned int ret_flags = 0;
88
89 if (!rdma_protocol_roce(ib_dev, port))
90 return 1UL << IB_GID_TYPE_IB;
91
92 for (i = 0; i < CAP_TO_GID_TABLE_SIZE; i++)
93 if (PORT_CAP_TO_GID_TYPE[i].is_supported(ib_dev, port))
94 ret_flags |= 1UL << PORT_CAP_TO_GID_TYPE[i].gid_type;
95
96 return ret_flags;
97}
98EXPORT_SYMBOL(roce_gid_type_mask_support);
99
Matan Barak03db3a22015-07-30 18:33:26 +0300100static void update_gid(enum gid_op_type gid_op, struct ib_device *ib_dev,
101 u8 port, union ib_gid *gid,
102 struct ib_gid_attr *gid_attr)
103{
Matan Barakb39ffa12015-12-23 14:56:47 +0200104 int i;
105 unsigned long gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
106
107 for (i = 0; i < IB_GID_TYPE_SIZE; i++) {
108 if ((1UL << i) & gid_type_mask) {
109 gid_attr->gid_type = i;
110 switch (gid_op) {
111 case GID_ADD:
112 ib_cache_gid_add(ib_dev, port,
113 gid, gid_attr);
114 break;
115 case GID_DEL:
116 ib_cache_gid_del(ib_dev, port,
117 gid, gid_attr);
118 break;
119 }
120 }
Matan Barak03db3a22015-07-30 18:33:26 +0300121 }
122}
123
Matan Barak238fdf42015-07-30 18:33:27 +0300124enum bonding_slave_state {
125 BONDING_SLAVE_STATE_ACTIVE = 1UL << 0,
126 BONDING_SLAVE_STATE_INACTIVE = 1UL << 1,
127 /* No primary slave or the device isn't a slave in bonding */
128 BONDING_SLAVE_STATE_NA = 1UL << 2,
129};
130
131static enum bonding_slave_state is_eth_active_slave_of_bonding_rcu(struct net_device *dev,
132 struct net_device *upper)
133{
134 if (upper && netif_is_bond_master(upper)) {
135 struct net_device *pdev =
136 bond_option_active_slave_get_rcu(netdev_priv(upper));
137
138 if (pdev)
139 return dev == pdev ? BONDING_SLAVE_STATE_ACTIVE :
140 BONDING_SLAVE_STATE_INACTIVE;
141 }
142
143 return BONDING_SLAVE_STATE_NA;
144}
145
Matan Barak238fdf42015-07-30 18:33:27 +0300146#define REQUIRED_BOND_STATES (BONDING_SLAVE_STATE_ACTIVE | \
147 BONDING_SLAVE_STATE_NA)
Matan Barak03db3a22015-07-30 18:33:26 +0300148static int is_eth_port_of_netdev(struct ib_device *ib_dev, u8 port,
149 struct net_device *rdma_ndev, void *cookie)
150{
Matan Barak238fdf42015-07-30 18:33:27 +0300151 struct net_device *real_dev;
152 int res;
153
154 if (!rdma_ndev)
155 return 0;
156
157 rcu_read_lock();
Parav Pandit59039602017-02-02 07:14:08 +0200158 real_dev = rdma_vlan_dev_real_dev(cookie);
Matan Barak238fdf42015-07-30 18:33:27 +0300159 if (!real_dev)
Parav Pandit59039602017-02-02 07:14:08 +0200160 real_dev = cookie;
Matan Barak238fdf42015-07-30 18:33:27 +0300161
Parav Pandit59039602017-02-02 07:14:08 +0200162 res = ((rdma_is_upper_dev_rcu(rdma_ndev, cookie) &&
Matan Barak238fdf42015-07-30 18:33:27 +0300163 (is_eth_active_slave_of_bonding_rcu(rdma_ndev, real_dev) &
164 REQUIRED_BOND_STATES)) ||
165 real_dev == rdma_ndev);
166
167 rcu_read_unlock();
168 return res;
169}
170
171static int is_eth_port_inactive_slave(struct ib_device *ib_dev, u8 port,
172 struct net_device *rdma_ndev, void *cookie)
173{
174 struct net_device *master_dev;
Matan Barak03db3a22015-07-30 18:33:26 +0300175 int res;
176
177 if (!rdma_ndev)
178 return 0;
179
180 rcu_read_lock();
181 master_dev = netdev_master_upper_dev_get_rcu(rdma_ndev);
Matan Barak238fdf42015-07-30 18:33:27 +0300182 res = is_eth_active_slave_of_bonding_rcu(rdma_ndev, master_dev) ==
183 BONDING_SLAVE_STATE_INACTIVE;
Matan Barak03db3a22015-07-30 18:33:26 +0300184 rcu_read_unlock();
185
186 return res;
187}
188
189static int pass_all_filter(struct ib_device *ib_dev, u8 port,
190 struct net_device *rdma_ndev, void *cookie)
191{
192 return 1;
193}
194
Matan Barak238fdf42015-07-30 18:33:27 +0300195static int upper_device_filter(struct ib_device *ib_dev, u8 port,
196 struct net_device *rdma_ndev, void *cookie)
197{
Matan Barak238fdf42015-07-30 18:33:27 +0300198 int res;
199
200 if (!rdma_ndev)
201 return 0;
202
Parav Pandit59039602017-02-02 07:14:08 +0200203 if (rdma_ndev == cookie)
Matan Barak238fdf42015-07-30 18:33:27 +0300204 return 1;
205
206 rcu_read_lock();
Parav Pandit59039602017-02-02 07:14:08 +0200207 res = rdma_is_upper_dev_rcu(rdma_ndev, cookie);
Matan Barak238fdf42015-07-30 18:33:27 +0300208 rcu_read_unlock();
209
210 return res;
211}
212
Matan Barak03db3a22015-07-30 18:33:26 +0300213static void update_gid_ip(enum gid_op_type gid_op,
214 struct ib_device *ib_dev,
215 u8 port, struct net_device *ndev,
216 struct sockaddr *addr)
217{
218 union ib_gid gid;
219 struct ib_gid_attr gid_attr;
220
221 rdma_ip2gid(addr, &gid);
222 memset(&gid_attr, 0, sizeof(gid_attr));
223 gid_attr.ndev = ndev;
224
225 update_gid(gid_op, ib_dev, port, &gid, &gid_attr);
226}
227
228static void enum_netdev_default_gids(struct ib_device *ib_dev,
229 u8 port, struct net_device *event_ndev,
230 struct net_device *rdma_ndev)
231{
Matan Barakb39ffa12015-12-23 14:56:47 +0200232 unsigned long gid_type_mask;
233
Matan Barak238fdf42015-07-30 18:33:27 +0300234 rcu_read_lock();
235 if (!rdma_ndev ||
236 ((rdma_ndev != event_ndev &&
Matan Barak6020d7e2015-12-23 14:56:52 +0200237 !rdma_is_upper_dev_rcu(rdma_ndev, event_ndev)) ||
Matan Barak238fdf42015-07-30 18:33:27 +0300238 is_eth_active_slave_of_bonding_rcu(rdma_ndev,
239 netdev_master_upper_dev_get_rcu(rdma_ndev)) ==
240 BONDING_SLAVE_STATE_INACTIVE)) {
241 rcu_read_unlock();
Matan Barak03db3a22015-07-30 18:33:26 +0300242 return;
Matan Barak238fdf42015-07-30 18:33:27 +0300243 }
244 rcu_read_unlock();
Matan Barak03db3a22015-07-30 18:33:26 +0300245
Matan Barakb39ffa12015-12-23 14:56:47 +0200246 gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
247
248 ib_cache_gid_set_default_gid(ib_dev, port, rdma_ndev, gid_type_mask,
Matan Barak03db3a22015-07-30 18:33:26 +0300249 IB_CACHE_GID_DEFAULT_MODE_SET);
250}
251
Matan Barak238fdf42015-07-30 18:33:27 +0300252static void bond_delete_netdev_default_gids(struct ib_device *ib_dev,
253 u8 port,
254 struct net_device *event_ndev,
255 struct net_device *rdma_ndev)
256{
257 struct net_device *real_dev = rdma_vlan_dev_real_dev(event_ndev);
Parav Panditdc5640f2018-04-23 16:58:19 +0300258 unsigned long gid_type_mask;
Matan Barak238fdf42015-07-30 18:33:27 +0300259
260 if (!rdma_ndev)
261 return;
262
263 if (!real_dev)
264 real_dev = event_ndev;
265
266 rcu_read_lock();
267
Parav Panditdc5640f2018-04-23 16:58:19 +0300268 if (((rdma_ndev != event_ndev &&
269 !rdma_is_upper_dev_rcu(rdma_ndev, event_ndev)) ||
270 is_eth_active_slave_of_bonding_rcu(rdma_ndev, real_dev)
271 ==
272 BONDING_SLAVE_STATE_INACTIVE)) {
Matan Barak238fdf42015-07-30 18:33:27 +0300273 rcu_read_unlock();
Parav Panditdc5640f2018-04-23 16:58:19 +0300274 return;
Matan Barak238fdf42015-07-30 18:33:27 +0300275 }
Parav Panditdc5640f2018-04-23 16:58:19 +0300276
277 rcu_read_unlock();
278
279 gid_type_mask = roce_gid_type_mask_support(ib_dev, port);
280
281 ib_cache_gid_set_default_gid(ib_dev, port, rdma_ndev,
282 gid_type_mask,
283 IB_CACHE_GID_DEFAULT_MODE_DELETE);
Matan Barak238fdf42015-07-30 18:33:27 +0300284}
285
Matan Barak03db3a22015-07-30 18:33:26 +0300286static void enum_netdev_ipv4_ips(struct ib_device *ib_dev,
287 u8 port, struct net_device *ndev)
288{
289 struct in_device *in_dev;
Matan Barak39096422015-10-15 15:01:03 +0300290 struct sin_list {
291 struct list_head list;
292 struct sockaddr_in ip;
293 };
294 struct sin_list *sin_iter;
295 struct sin_list *sin_temp;
Matan Barak03db3a22015-07-30 18:33:26 +0300296
Matan Barak39096422015-10-15 15:01:03 +0300297 LIST_HEAD(sin_list);
Matan Barak03db3a22015-07-30 18:33:26 +0300298 if (ndev->reg_state >= NETREG_UNREGISTERING)
299 return;
300
Matan Barak39096422015-10-15 15:01:03 +0300301 rcu_read_lock();
302 in_dev = __in_dev_get_rcu(ndev);
303 if (!in_dev) {
304 rcu_read_unlock();
Matan Barak03db3a22015-07-30 18:33:26 +0300305 return;
Matan Barak39096422015-10-15 15:01:03 +0300306 }
Matan Barak03db3a22015-07-30 18:33:26 +0300307
308 for_ifa(in_dev) {
Matan Barak39096422015-10-15 15:01:03 +0300309 struct sin_list *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
Matan Barak03db3a22015-07-30 18:33:26 +0300310
Leon Romanovskya0b34552016-11-03 16:44:10 +0200311 if (!entry)
Matan Barak39096422015-10-15 15:01:03 +0300312 continue;
Leon Romanovskya0b34552016-11-03 16:44:10 +0200313
Matan Barak39096422015-10-15 15:01:03 +0300314 entry->ip.sin_family = AF_INET;
315 entry->ip.sin_addr.s_addr = ifa->ifa_address;
316 list_add_tail(&entry->list, &sin_list);
Matan Barak03db3a22015-07-30 18:33:26 +0300317 }
318 endfor_ifa(in_dev);
Matan Barak39096422015-10-15 15:01:03 +0300319 rcu_read_unlock();
Matan Barak03db3a22015-07-30 18:33:26 +0300320
Matan Barak39096422015-10-15 15:01:03 +0300321 list_for_each_entry_safe(sin_iter, sin_temp, &sin_list, list) {
322 update_gid_ip(GID_ADD, ib_dev, port, ndev,
323 (struct sockaddr *)&sin_iter->ip);
324 list_del(&sin_iter->list);
325 kfree(sin_iter);
326 }
Matan Barak03db3a22015-07-30 18:33:26 +0300327}
328
329static void enum_netdev_ipv6_ips(struct ib_device *ib_dev,
330 u8 port, struct net_device *ndev)
331{
332 struct inet6_ifaddr *ifp;
333 struct inet6_dev *in6_dev;
334 struct sin6_list {
335 struct list_head list;
336 struct sockaddr_in6 sin6;
337 };
338 struct sin6_list *sin6_iter;
339 struct sin6_list *sin6_temp;
340 struct ib_gid_attr gid_attr = {.ndev = ndev};
341 LIST_HEAD(sin6_list);
342
343 if (ndev->reg_state >= NETREG_UNREGISTERING)
344 return;
345
346 in6_dev = in6_dev_get(ndev);
347 if (!in6_dev)
348 return;
349
350 read_lock_bh(&in6_dev->lock);
351 list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
352 struct sin6_list *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
353
Leon Romanovskya0b34552016-11-03 16:44:10 +0200354 if (!entry)
Matan Barak03db3a22015-07-30 18:33:26 +0300355 continue;
Matan Barak03db3a22015-07-30 18:33:26 +0300356
357 entry->sin6.sin6_family = AF_INET6;
358 entry->sin6.sin6_addr = ifp->addr;
359 list_add_tail(&entry->list, &sin6_list);
360 }
361 read_unlock_bh(&in6_dev->lock);
362
363 in6_dev_put(in6_dev);
364
365 list_for_each_entry_safe(sin6_iter, sin6_temp, &sin6_list, list) {
366 union ib_gid gid;
367
368 rdma_ip2gid((struct sockaddr *)&sin6_iter->sin6, &gid);
369 update_gid(GID_ADD, ib_dev, port, &gid, &gid_attr);
370 list_del(&sin6_iter->list);
371 kfree(sin6_iter);
372 }
373}
374
Matan Barak238fdf42015-07-30 18:33:27 +0300375static void _add_netdev_ips(struct ib_device *ib_dev, u8 port,
376 struct net_device *ndev)
377{
378 enum_netdev_ipv4_ips(ib_dev, port, ndev);
379 if (IS_ENABLED(CONFIG_IPV6))
380 enum_netdev_ipv6_ips(ib_dev, port, ndev);
381}
382
Matan Barak03db3a22015-07-30 18:33:26 +0300383static void add_netdev_ips(struct ib_device *ib_dev, u8 port,
384 struct net_device *rdma_ndev, void *cookie)
385{
Parav Pandit59039602017-02-02 07:14:08 +0200386 enum_netdev_default_gids(ib_dev, port, cookie, rdma_ndev);
387 _add_netdev_ips(ib_dev, port, cookie);
Matan Barak03db3a22015-07-30 18:33:26 +0300388}
389
390static void del_netdev_ips(struct ib_device *ib_dev, u8 port,
391 struct net_device *rdma_ndev, void *cookie)
392{
Parav Pandit59039602017-02-02 07:14:08 +0200393 ib_cache_gid_del_all_netdev_gids(ib_dev, port, cookie);
Matan Barak03db3a22015-07-30 18:33:26 +0300394}
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();
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +0300408 down_read(&net_rwsem);
Matan Barak03db3a22015-07-30 18:33:26 +0300409 for_each_net(net)
410 for_each_netdev(net, ndev)
411 if (is_eth_port_of_netdev(ib_dev, port, rdma_ndev, ndev))
412 add_netdev_ips(ib_dev, port, rdma_ndev, ndev);
Kirill Tkhaif0b07bb12018-03-29 19:20:32 +0300413 up_read(&net_rwsem);
Matan Barak03db3a22015-07-30 18:33:26 +0300414 rtnl_unlock();
415}
416
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200417/**
418 * rdma_roce_rescan_device - Rescan all of the network devices in the system
419 * and add their gids, as needed, to the relevant RoCE devices.
420 *
421 * @device: the rdma device
422 */
423void rdma_roce_rescan_device(struct ib_device *ib_dev)
Matan Barak03db3a22015-07-30 18:33:26 +0300424{
425 ib_enum_roce_netdev(ib_dev, pass_all_filter, NULL,
426 enum_all_gids_of_dev_cb, NULL);
Matan Barak03db3a22015-07-30 18:33:26 +0300427}
Daniel Jurgens32f69e42018-01-04 17:25:36 +0200428EXPORT_SYMBOL(rdma_roce_rescan_device);
Matan Barak03db3a22015-07-30 18:33:26 +0300429
430static void callback_for_addr_gid_device_scan(struct ib_device *device,
431 u8 port,
432 struct net_device *rdma_ndev,
433 void *cookie)
434{
435 struct update_gid_event_work *parsed = cookie;
436
437 return update_gid(parsed->gid_op, device,
438 port, &parsed->gid,
439 &parsed->gid_attr);
440}
441
David Ahern453d3932016-10-17 19:15:46 -0700442struct upper_list {
443 struct list_head list;
444 struct net_device *upper;
445};
446
447static int netdev_upper_walk(struct net_device *upper, void *data)
448{
449 struct upper_list *entry = kmalloc(sizeof(*entry), GFP_ATOMIC);
450 struct list_head *upper_list = data;
451
Linus Torvalds4d5b57e2016-12-15 12:03:32 -0800452 if (!entry)
David Ahern453d3932016-10-17 19:15:46 -0700453 return 0;
David Ahern453d3932016-10-17 19:15:46 -0700454
455 list_add_tail(&entry->list, upper_list);
456 dev_hold(upper);
457 entry->upper = upper;
458
459 return 0;
460}
461
Matan Barak238fdf42015-07-30 18:33:27 +0300462static 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{
Parav Pandit59039602017-02-02 07:14:08 +0200468 struct net_device *ndev = cookie;
Matan Barak238fdf42015-07-30 18:33:27 +0300469 struct upper_list *upper_iter;
470 struct upper_list *upper_temp;
471 LIST_HEAD(upper_list);
472
473 rcu_read_lock();
David Ahern453d3932016-10-17 19:15:46 -0700474 netdev_walk_all_upper_dev_rcu(ndev, netdev_upper_walk, &upper_list);
Matan Barak238fdf42015-07-30 18:33:27 +0300475 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
487static 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
493static 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
499static 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
505static 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
524static void del_netdev_default_ips(struct ib_device *ib_dev, u8 port,
525 struct net_device *rdma_ndev, void *cookie)
526{
Parav Pandit59039602017-02-02 07:14:08 +0200527 bond_delete_netdev_default_gids(ib_dev, port, cookie, rdma_ndev);
Matan Barak238fdf42015-07-30 18:33:27 +0300528}
529
Matan Barak03db3a22015-07-30 18:33:26 +0300530/* The following functions operate on all IB devices. netdevice_event and
531 * addr_event execute ib_enum_all_roce_netdevs through a work.
532 * ib_enum_all_roce_netdevs iterates through all IB devices.
533 */
534
535static void netdevice_event_work_handler(struct work_struct *_work)
536{
537 struct netdev_event_work *work =
538 container_of(_work, struct netdev_event_work, work);
539 unsigned int i;
540
Matan Barak238fdf42015-07-30 18:33:27 +0300541 for (i = 0; i < ARRAY_SIZE(work->cmds) && work->cmds[i].cb; i++) {
542 ib_enum_all_roce_netdevs(work->cmds[i].filter,
543 work->cmds[i].filter_ndev,
544 work->cmds[i].cb,
545 work->cmds[i].ndev);
546 dev_put(work->cmds[i].ndev);
547 dev_put(work->cmds[i].filter_ndev);
548 }
Matan Barak03db3a22015-07-30 18:33:26 +0300549
Matan Barak03db3a22015-07-30 18:33:26 +0300550 kfree(work);
551}
552
553static int netdevice_queue_work(struct netdev_event_work_cmd *cmds,
554 struct net_device *ndev)
555{
Matan Barak238fdf42015-07-30 18:33:27 +0300556 unsigned int i;
Matan Barak03db3a22015-07-30 18:33:26 +0300557 struct netdev_event_work *ndev_work =
558 kmalloc(sizeof(*ndev_work), GFP_KERNEL);
559
Leon Romanovskya0b34552016-11-03 16:44:10 +0200560 if (!ndev_work)
Matan Barak03db3a22015-07-30 18:33:26 +0300561 return NOTIFY_DONE;
Matan Barak03db3a22015-07-30 18:33:26 +0300562
563 memcpy(ndev_work->cmds, cmds, sizeof(ndev_work->cmds));
Matan Barak238fdf42015-07-30 18:33:27 +0300564 for (i = 0; i < ARRAY_SIZE(ndev_work->cmds) && ndev_work->cmds[i].cb; i++) {
565 if (!ndev_work->cmds[i].ndev)
566 ndev_work->cmds[i].ndev = ndev;
567 if (!ndev_work->cmds[i].filter_ndev)
568 ndev_work->cmds[i].filter_ndev = ndev;
569 dev_hold(ndev_work->cmds[i].ndev);
570 dev_hold(ndev_work->cmds[i].filter_ndev);
571 }
Matan Barak03db3a22015-07-30 18:33:26 +0300572 INIT_WORK(&ndev_work->work, netdevice_event_work_handler);
573
Majd Dibbiny8fe8bac2017-05-30 09:58:06 +0300574 queue_work(gid_cache_wq, &ndev_work->work);
Matan Barak03db3a22015-07-30 18:33:26 +0300575
576 return NOTIFY_DONE;
577}
578
Matan Barak238fdf42015-07-30 18:33:27 +0300579static const struct netdev_event_work_cmd add_cmd = {
580 .cb = add_netdev_ips, .filter = is_eth_port_of_netdev};
581static const struct netdev_event_work_cmd add_cmd_upper_ips = {
582 .cb = add_netdev_upper_ips, .filter = is_eth_port_of_netdev};
583
Linus Torvalds26d21772015-09-09 08:33:31 -0700584static void netdevice_event_changeupper(struct netdev_notifier_changeupper_info *changeupper_info,
Matan Barak238fdf42015-07-30 18:33:27 +0300585 struct netdev_event_work_cmd *cmds)
586{
587 static const struct netdev_event_work_cmd upper_ips_del_cmd = {
588 .cb = del_netdev_upper_ips, .filter = upper_device_filter};
589 static const struct netdev_event_work_cmd bonding_default_del_cmd = {
590 .cb = del_netdev_default_ips, .filter = is_eth_port_inactive_slave};
591
Linus Torvalds26d21772015-09-09 08:33:31 -0700592 if (changeupper_info->linking == false) {
Matan Barak238fdf42015-07-30 18:33:27 +0300593 cmds[0] = upper_ips_del_cmd;
Linus Torvalds26d21772015-09-09 08:33:31 -0700594 cmds[0].ndev = changeupper_info->upper_dev;
Matan Barak238fdf42015-07-30 18:33:27 +0300595 cmds[1] = add_cmd;
Linus Torvalds26d21772015-09-09 08:33:31 -0700596 } else {
Matan Barak238fdf42015-07-30 18:33:27 +0300597 cmds[0] = bonding_default_del_cmd;
Linus Torvalds26d21772015-09-09 08:33:31 -0700598 cmds[0].ndev = changeupper_info->upper_dev;
Matan Barak238fdf42015-07-30 18:33:27 +0300599 cmds[1] = add_cmd_upper_ips;
Linus Torvalds26d21772015-09-09 08:33:31 -0700600 cmds[1].ndev = changeupper_info->upper_dev;
601 cmds[1].filter_ndev = changeupper_info->upper_dev;
Matan Barak238fdf42015-07-30 18:33:27 +0300602 }
603}
604
Matan Barak03db3a22015-07-30 18:33:26 +0300605static int netdevice_event(struct notifier_block *this, unsigned long event,
606 void *ptr)
607{
Matan Barak03db3a22015-07-30 18:33:26 +0300608 static const struct netdev_event_work_cmd del_cmd = {
609 .cb = del_netdev_ips, .filter = pass_all_filter};
Matan Barak238fdf42015-07-30 18:33:27 +0300610 static const struct netdev_event_work_cmd bonding_default_del_cmd_join = {
611 .cb = del_netdev_default_ips_join, .filter = is_eth_port_inactive_slave};
612 static const struct netdev_event_work_cmd default_del_cmd = {
613 .cb = del_netdev_default_ips, .filter = pass_all_filter};
614 static const struct netdev_event_work_cmd bonding_event_ips_del_cmd = {
615 .cb = del_netdev_upper_ips, .filter = upper_device_filter};
Matan Barak03db3a22015-07-30 18:33:26 +0300616 struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
617 struct netdev_event_work_cmd cmds[ROCE_NETDEV_CALLBACK_SZ] = { {NULL} };
618
619 if (ndev->type != ARPHRD_ETHER)
620 return NOTIFY_DONE;
621
622 switch (event) {
623 case NETDEV_REGISTER:
624 case NETDEV_UP:
Matan Barak238fdf42015-07-30 18:33:27 +0300625 cmds[0] = bonding_default_del_cmd_join;
626 cmds[1] = add_cmd;
Matan Barak03db3a22015-07-30 18:33:26 +0300627 break;
628
629 case NETDEV_UNREGISTER:
630 if (ndev->reg_state < NETREG_UNREGISTERED)
631 cmds[0] = del_cmd;
632 else
633 return NOTIFY_DONE;
634 break;
635
636 case NETDEV_CHANGEADDR:
Matan Barak238fdf42015-07-30 18:33:27 +0300637 cmds[0] = default_del_cmd;
Matan Barak03db3a22015-07-30 18:33:26 +0300638 cmds[1] = add_cmd;
639 break;
Matan Barak238fdf42015-07-30 18:33:27 +0300640
641 case NETDEV_CHANGEUPPER:
642 netdevice_event_changeupper(
Linus Torvalds26d21772015-09-09 08:33:31 -0700643 container_of(ptr, struct netdev_notifier_changeupper_info, info),
Matan Barak238fdf42015-07-30 18:33:27 +0300644 cmds);
645 break;
646
647 case NETDEV_BONDING_FAILOVER:
648 cmds[0] = bonding_event_ips_del_cmd;
649 cmds[1] = bonding_default_del_cmd_join;
650 cmds[2] = add_cmd_upper_ips;
651 break;
652
Matan Barak03db3a22015-07-30 18:33:26 +0300653 default:
654 return NOTIFY_DONE;
655 }
656
657 return netdevice_queue_work(cmds, ndev);
658}
659
660static void update_gid_event_work_handler(struct work_struct *_work)
661{
662 struct update_gid_event_work *work =
663 container_of(_work, struct update_gid_event_work, work);
664
665 ib_enum_all_roce_netdevs(is_eth_port_of_netdev, work->gid_attr.ndev,
666 callback_for_addr_gid_device_scan, work);
667
668 dev_put(work->gid_attr.ndev);
669 kfree(work);
670}
671
672static int addr_event(struct notifier_block *this, unsigned long event,
673 struct sockaddr *sa, struct net_device *ndev)
674{
675 struct update_gid_event_work *work;
676 enum gid_op_type gid_op;
677
678 if (ndev->type != ARPHRD_ETHER)
679 return NOTIFY_DONE;
680
681 switch (event) {
682 case NETDEV_UP:
683 gid_op = GID_ADD;
684 break;
685
686 case NETDEV_DOWN:
687 gid_op = GID_DEL;
688 break;
689
690 default:
691 return NOTIFY_DONE;
692 }
693
694 work = kmalloc(sizeof(*work), GFP_ATOMIC);
Leon Romanovskya0b34552016-11-03 16:44:10 +0200695 if (!work)
Matan Barak03db3a22015-07-30 18:33:26 +0300696 return NOTIFY_DONE;
Matan Barak03db3a22015-07-30 18:33:26 +0300697
698 INIT_WORK(&work->work, update_gid_event_work_handler);
699
700 rdma_ip2gid(sa, &work->gid);
701 work->gid_op = gid_op;
702
703 memset(&work->gid_attr, 0, sizeof(work->gid_attr));
704 dev_hold(ndev);
705 work->gid_attr.ndev = ndev;
706
Majd Dibbiny8fe8bac2017-05-30 09:58:06 +0300707 queue_work(gid_cache_wq, &work->work);
Matan Barak03db3a22015-07-30 18:33:26 +0300708
709 return NOTIFY_DONE;
710}
711
712static int inetaddr_event(struct notifier_block *this, unsigned long event,
713 void *ptr)
714{
715 struct sockaddr_in in;
716 struct net_device *ndev;
717 struct in_ifaddr *ifa = ptr;
718
719 in.sin_family = AF_INET;
720 in.sin_addr.s_addr = ifa->ifa_address;
721 ndev = ifa->ifa_dev->dev;
722
723 return addr_event(this, event, (struct sockaddr *)&in, ndev);
724}
725
726static int inet6addr_event(struct notifier_block *this, unsigned long event,
727 void *ptr)
728{
729 struct sockaddr_in6 in6;
730 struct net_device *ndev;
731 struct inet6_ifaddr *ifa6 = ptr;
732
733 in6.sin6_family = AF_INET6;
734 in6.sin6_addr = ifa6->addr;
735 ndev = ifa6->idev->dev;
736
737 return addr_event(this, event, (struct sockaddr *)&in6, ndev);
738}
739
740static struct notifier_block nb_netdevice = {
741 .notifier_call = netdevice_event
742};
743
744static struct notifier_block nb_inetaddr = {
745 .notifier_call = inetaddr_event
746};
747
748static struct notifier_block nb_inet6addr = {
749 .notifier_call = inet6addr_event
750};
751
752int __init roce_gid_mgmt_init(void)
753{
Majd Dibbiny8fe8bac2017-05-30 09:58:06 +0300754 gid_cache_wq = alloc_ordered_workqueue("gid-cache-wq", 0);
755 if (!gid_cache_wq)
756 return -ENOMEM;
757
Matan Barak03db3a22015-07-30 18:33:26 +0300758 register_inetaddr_notifier(&nb_inetaddr);
759 if (IS_ENABLED(CONFIG_IPV6))
760 register_inet6addr_notifier(&nb_inet6addr);
761 /* We relay on the netdevice notifier to enumerate all
762 * existing devices in the system. Register to this notifier
763 * last to make sure we will not miss any IP add/del
764 * callbacks.
765 */
766 register_netdevice_notifier(&nb_netdevice);
767
768 return 0;
769}
770
771void __exit roce_gid_mgmt_cleanup(void)
772{
773 if (IS_ENABLED(CONFIG_IPV6))
774 unregister_inet6addr_notifier(&nb_inet6addr);
775 unregister_inetaddr_notifier(&nb_inetaddr);
776 unregister_netdevice_notifier(&nb_netdevice);
777 /* Ensure all gid deletion tasks complete before we go down,
778 * to avoid any reference to free'd memory. By the time
779 * ib-core is removed, all physical devices have been removed,
780 * so no issue with remaining hardware contexts.
781 */
Majd Dibbiny8fe8bac2017-05-30 09:58:06 +0300782 destroy_workqueue(gid_cache_wq);
Matan Barak03db3a22015-07-30 18:33:26 +0300783}