IB/core: Add RoCE GID table management
[cascardo/linux.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
41 #include <rdma/ib_cache.h>
42 #include <rdma/ib_addr.h>
43
44 enum gid_op_type {
45         GID_DEL = 0,
46         GID_ADD
47 };
48
49 struct update_gid_event_work {
50         struct work_struct work;
51         union ib_gid       gid;
52         struct ib_gid_attr gid_attr;
53         enum gid_op_type gid_op;
54 };
55
56 #define ROCE_NETDEV_CALLBACK_SZ         2
57 struct netdev_event_work_cmd {
58         roce_netdev_callback    cb;
59         roce_netdev_filter      filter;
60 };
61
62 struct netdev_event_work {
63         struct work_struct              work;
64         struct netdev_event_work_cmd    cmds[ROCE_NETDEV_CALLBACK_SZ];
65         struct net_device               *ndev;
66 };
67
68 static void update_gid(enum gid_op_type gid_op, struct ib_device *ib_dev,
69                        u8 port, union ib_gid *gid,
70                        struct ib_gid_attr *gid_attr)
71 {
72         switch (gid_op) {
73         case GID_ADD:
74                 ib_cache_gid_add(ib_dev, port, gid, gid_attr);
75                 break;
76         case GID_DEL:
77                 ib_cache_gid_del(ib_dev, port, gid, gid_attr);
78                 break;
79         }
80 }
81
82 static int is_eth_port_of_netdev(struct ib_device *ib_dev, u8 port,
83                                  struct net_device *rdma_ndev, void *cookie)
84 {
85         struct net_device *real_dev;
86         struct net_device *master_dev;
87         struct net_device *event_ndev = (struct net_device *)cookie;
88         int res;
89
90         if (!rdma_ndev)
91                 return 0;
92
93         rcu_read_lock();
94         master_dev = netdev_master_upper_dev_get_rcu(rdma_ndev);
95         real_dev = rdma_vlan_dev_real_dev(event_ndev);
96         res = (real_dev ? real_dev : event_ndev) ==
97                 (master_dev ? master_dev : rdma_ndev);
98         rcu_read_unlock();
99
100         return res;
101 }
102
103 static int pass_all_filter(struct ib_device *ib_dev, u8 port,
104                            struct net_device *rdma_ndev, void *cookie)
105 {
106         return 1;
107 }
108
109 static void update_gid_ip(enum gid_op_type gid_op,
110                           struct ib_device *ib_dev,
111                           u8 port, struct net_device *ndev,
112                           struct sockaddr *addr)
113 {
114         union ib_gid gid;
115         struct ib_gid_attr gid_attr;
116
117         rdma_ip2gid(addr, &gid);
118         memset(&gid_attr, 0, sizeof(gid_attr));
119         gid_attr.ndev = ndev;
120
121         update_gid(gid_op, ib_dev, port, &gid, &gid_attr);
122 }
123
124 static void enum_netdev_default_gids(struct ib_device *ib_dev,
125                                      u8 port, struct net_device *event_ndev,
126                                      struct net_device *rdma_ndev)
127 {
128         if (rdma_ndev != event_ndev)
129                 return;
130
131         ib_cache_gid_set_default_gid(ib_dev, port, rdma_ndev,
132                                      IB_CACHE_GID_DEFAULT_MODE_SET);
133 }
134
135 static void enum_netdev_ipv4_ips(struct ib_device *ib_dev,
136                                  u8 port, struct net_device *ndev)
137 {
138         struct in_device *in_dev;
139
140         if (ndev->reg_state >= NETREG_UNREGISTERING)
141                 return;
142
143         in_dev = in_dev_get(ndev);
144         if (!in_dev)
145                 return;
146
147         for_ifa(in_dev) {
148                 struct sockaddr_in ip;
149
150                 ip.sin_family = AF_INET;
151                 ip.sin_addr.s_addr = ifa->ifa_address;
152                 update_gid_ip(GID_ADD, ib_dev, port, ndev,
153                               (struct sockaddr *)&ip);
154         }
155         endfor_ifa(in_dev);
156
157         in_dev_put(in_dev);
158 }
159
160 static void enum_netdev_ipv6_ips(struct ib_device *ib_dev,
161                                  u8 port, struct net_device *ndev)
162 {
163         struct inet6_ifaddr *ifp;
164         struct inet6_dev *in6_dev;
165         struct sin6_list {
166                 struct list_head        list;
167                 struct sockaddr_in6     sin6;
168         };
169         struct sin6_list *sin6_iter;
170         struct sin6_list *sin6_temp;
171         struct ib_gid_attr gid_attr = {.ndev = ndev};
172         LIST_HEAD(sin6_list);
173
174         if (ndev->reg_state >= NETREG_UNREGISTERING)
175                 return;
176
177         in6_dev = in6_dev_get(ndev);
178         if (!in6_dev)
179                 return;
180
181         read_lock_bh(&in6_dev->lock);
182         list_for_each_entry(ifp, &in6_dev->addr_list, if_list) {
183                 struct sin6_list *entry = kzalloc(sizeof(*entry), GFP_ATOMIC);
184
185                 if (!entry) {
186                         pr_warn("roce_gid_mgmt: couldn't allocate entry for IPv6 update\n");
187                         continue;
188                 }
189
190                 entry->sin6.sin6_family = AF_INET6;
191                 entry->sin6.sin6_addr = ifp->addr;
192                 list_add_tail(&entry->list, &sin6_list);
193         }
194         read_unlock_bh(&in6_dev->lock);
195
196         in6_dev_put(in6_dev);
197
198         list_for_each_entry_safe(sin6_iter, sin6_temp, &sin6_list, list) {
199                 union ib_gid    gid;
200
201                 rdma_ip2gid((struct sockaddr *)&sin6_iter->sin6, &gid);
202                 update_gid(GID_ADD, ib_dev, port, &gid, &gid_attr);
203                 list_del(&sin6_iter->list);
204                 kfree(sin6_iter);
205         }
206 }
207
208 static void add_netdev_ips(struct ib_device *ib_dev, u8 port,
209                            struct net_device *rdma_ndev, void *cookie)
210 {
211         struct net_device *event_ndev = (struct net_device *)cookie;
212
213         enum_netdev_default_gids(ib_dev, port, event_ndev, rdma_ndev);
214         enum_netdev_ipv4_ips(ib_dev, port, event_ndev);
215         if (IS_ENABLED(CONFIG_IPV6))
216                 enum_netdev_ipv6_ips(ib_dev, port, event_ndev);
217 }
218
219 static void del_netdev_ips(struct ib_device *ib_dev, u8 port,
220                            struct net_device *rdma_ndev, void *cookie)
221 {
222         struct net_device *event_ndev = (struct net_device *)cookie;
223
224         ib_cache_gid_del_all_netdev_gids(ib_dev, port, event_ndev);
225 }
226
227 static void enum_all_gids_of_dev_cb(struct ib_device *ib_dev,
228                                     u8 port,
229                                     struct net_device *rdma_ndev,
230                                     void *cookie)
231 {
232         struct net *net;
233         struct net_device *ndev;
234
235         /* Lock the rtnl to make sure the netdevs does not move under
236          * our feet
237          */
238         rtnl_lock();
239         for_each_net(net)
240                 for_each_netdev(net, ndev)
241                         if (is_eth_port_of_netdev(ib_dev, port, rdma_ndev, ndev))
242                                 add_netdev_ips(ib_dev, port, rdma_ndev, ndev);
243         rtnl_unlock();
244 }
245
246 /* This function will rescan all of the network devices in the system
247  * and add their gids, as needed, to the relevant RoCE devices. */
248 int roce_rescan_device(struct ib_device *ib_dev)
249 {
250         ib_enum_roce_netdev(ib_dev, pass_all_filter, NULL,
251                             enum_all_gids_of_dev_cb, NULL);
252
253         return 0;
254 }
255
256 static void callback_for_addr_gid_device_scan(struct ib_device *device,
257                                               u8 port,
258                                               struct net_device *rdma_ndev,
259                                               void *cookie)
260 {
261         struct update_gid_event_work *parsed = cookie;
262
263         return update_gid(parsed->gid_op, device,
264                           port, &parsed->gid,
265                           &parsed->gid_attr);
266 }
267
268 /* The following functions operate on all IB devices. netdevice_event and
269  * addr_event execute ib_enum_all_roce_netdevs through a work.
270  * ib_enum_all_roce_netdevs iterates through all IB devices.
271  */
272
273 static void netdevice_event_work_handler(struct work_struct *_work)
274 {
275         struct netdev_event_work *work =
276                 container_of(_work, struct netdev_event_work, work);
277         unsigned int i;
278
279         for (i = 0; i < ARRAY_SIZE(work->cmds) && work->cmds[i].cb; i++)
280                 ib_enum_all_roce_netdevs(work->cmds[i].filter, work->ndev,
281                                          work->cmds[i].cb, work->ndev);
282
283         dev_put(work->ndev);
284         kfree(work);
285 }
286
287 static int netdevice_queue_work(struct netdev_event_work_cmd *cmds,
288                                 struct net_device *ndev)
289 {
290         struct netdev_event_work *ndev_work =
291                 kmalloc(sizeof(*ndev_work), GFP_KERNEL);
292
293         if (!ndev_work) {
294                 pr_warn("roce_gid_mgmt: can't allocate work for netdevice_event\n");
295                 return NOTIFY_DONE;
296         }
297
298         memcpy(ndev_work->cmds, cmds, sizeof(ndev_work->cmds));
299         ndev_work->ndev = ndev;
300         dev_hold(ndev);
301         INIT_WORK(&ndev_work->work, netdevice_event_work_handler);
302
303         queue_work(ib_wq, &ndev_work->work);
304
305         return NOTIFY_DONE;
306 }
307
308 static int netdevice_event(struct notifier_block *this, unsigned long event,
309                            void *ptr)
310 {
311         static const struct netdev_event_work_cmd add_cmd = {
312                 .cb = add_netdev_ips, .filter = is_eth_port_of_netdev};
313         static const struct netdev_event_work_cmd del_cmd = {
314                 .cb = del_netdev_ips, .filter = pass_all_filter};
315         struct net_device *ndev = netdev_notifier_info_to_dev(ptr);
316         struct netdev_event_work_cmd cmds[ROCE_NETDEV_CALLBACK_SZ] = { {NULL} };
317
318         if (ndev->type != ARPHRD_ETHER)
319                 return NOTIFY_DONE;
320
321         switch (event) {
322         case NETDEV_REGISTER:
323         case NETDEV_UP:
324                 cmds[0] = add_cmd;
325                 break;
326
327         case NETDEV_UNREGISTER:
328                 if (ndev->reg_state < NETREG_UNREGISTERED)
329                         cmds[0] = del_cmd;
330                 else
331                         return NOTIFY_DONE;
332                 break;
333
334         case NETDEV_CHANGEADDR:
335                 cmds[0] = del_cmd;
336                 cmds[1] = add_cmd;
337                 break;
338         default:
339                 return NOTIFY_DONE;
340         }
341
342         return netdevice_queue_work(cmds, ndev);
343 }
344
345 static void update_gid_event_work_handler(struct work_struct *_work)
346 {
347         struct update_gid_event_work *work =
348                 container_of(_work, struct update_gid_event_work, work);
349
350         ib_enum_all_roce_netdevs(is_eth_port_of_netdev, work->gid_attr.ndev,
351                                  callback_for_addr_gid_device_scan, work);
352
353         dev_put(work->gid_attr.ndev);
354         kfree(work);
355 }
356
357 static int addr_event(struct notifier_block *this, unsigned long event,
358                       struct sockaddr *sa, struct net_device *ndev)
359 {
360         struct update_gid_event_work *work;
361         enum gid_op_type gid_op;
362
363         if (ndev->type != ARPHRD_ETHER)
364                 return NOTIFY_DONE;
365
366         switch (event) {
367         case NETDEV_UP:
368                 gid_op = GID_ADD;
369                 break;
370
371         case NETDEV_DOWN:
372                 gid_op = GID_DEL;
373                 break;
374
375         default:
376                 return NOTIFY_DONE;
377         }
378
379         work = kmalloc(sizeof(*work), GFP_ATOMIC);
380         if (!work) {
381                 pr_warn("roce_gid_mgmt: Couldn't allocate work for addr_event\n");
382                 return NOTIFY_DONE;
383         }
384
385         INIT_WORK(&work->work, update_gid_event_work_handler);
386
387         rdma_ip2gid(sa, &work->gid);
388         work->gid_op = gid_op;
389
390         memset(&work->gid_attr, 0, sizeof(work->gid_attr));
391         dev_hold(ndev);
392         work->gid_attr.ndev   = ndev;
393
394         queue_work(ib_wq, &work->work);
395
396         return NOTIFY_DONE;
397 }
398
399 static int inetaddr_event(struct notifier_block *this, unsigned long event,
400                           void *ptr)
401 {
402         struct sockaddr_in      in;
403         struct net_device       *ndev;
404         struct in_ifaddr        *ifa = ptr;
405
406         in.sin_family = AF_INET;
407         in.sin_addr.s_addr = ifa->ifa_address;
408         ndev = ifa->ifa_dev->dev;
409
410         return addr_event(this, event, (struct sockaddr *)&in, ndev);
411 }
412
413 static int inet6addr_event(struct notifier_block *this, unsigned long event,
414                            void *ptr)
415 {
416         struct sockaddr_in6     in6;
417         struct net_device       *ndev;
418         struct inet6_ifaddr     *ifa6 = ptr;
419
420         in6.sin6_family = AF_INET6;
421         in6.sin6_addr = ifa6->addr;
422         ndev = ifa6->idev->dev;
423
424         return addr_event(this, event, (struct sockaddr *)&in6, ndev);
425 }
426
427 static struct notifier_block nb_netdevice = {
428         .notifier_call = netdevice_event
429 };
430
431 static struct notifier_block nb_inetaddr = {
432         .notifier_call = inetaddr_event
433 };
434
435 static struct notifier_block nb_inet6addr = {
436         .notifier_call = inet6addr_event
437 };
438
439 int __init roce_gid_mgmt_init(void)
440 {
441         register_inetaddr_notifier(&nb_inetaddr);
442         if (IS_ENABLED(CONFIG_IPV6))
443                 register_inet6addr_notifier(&nb_inet6addr);
444         /* We relay on the netdevice notifier to enumerate all
445          * existing devices in the system. Register to this notifier
446          * last to make sure we will not miss any IP add/del
447          * callbacks.
448          */
449         register_netdevice_notifier(&nb_netdevice);
450
451         return 0;
452 }
453
454 void __exit roce_gid_mgmt_cleanup(void)
455 {
456         if (IS_ENABLED(CONFIG_IPV6))
457                 unregister_inet6addr_notifier(&nb_inet6addr);
458         unregister_inetaddr_notifier(&nb_inetaddr);
459         unregister_netdevice_notifier(&nb_netdevice);
460         /* Ensure all gid deletion tasks complete before we go down,
461          * to avoid any reference to free'd memory. By the time
462          * ib-core is removed, all physical devices have been removed,
463          * so no issue with remaining hardware contexts.
464          */
465 }