blob: a080c95fed6bccba69b9cc49b47977524ee564b2 [file] [log] [blame]
Ido Schimmel464dce12016-07-02 11:00:15 +02001/*
2 * drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c
3 * Copyright (c) 2016 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
5 * Copyright (c) 2016 Ido Schimmel <idosch@mellanox.com>
Yotam Gigic723c7352016-07-05 11:27:43 +02006 * Copyright (c) 2016 Yotam Gigi <yotamg@mellanox.com>
Ido Schimmel464dce12016-07-02 11:00:15 +02007 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include <linux/kernel.h>
38#include <linux/types.h>
Jiri Pirko5e9c16c2016-07-04 08:23:04 +020039#include <linux/rhashtable.h>
40#include <linux/bitops.h>
41#include <linux/in6.h>
Yotam Gigic723c7352016-07-05 11:27:43 +020042#include <linux/notifier.h>
43#include <net/netevent.h>
Jiri Pirko6cf3c972016-07-05 11:27:39 +020044#include <net/neighbour.h>
45#include <net/arp.h>
Jiri Pirkob45f64d2016-09-26 12:52:31 +020046#include <net/ip_fib.h>
Ido Schimmel464dce12016-07-02 11:00:15 +020047
48#include "spectrum.h"
49#include "core.h"
50#include "reg.h"
51
Jiri Pirko53342022016-07-04 08:23:08 +020052#define mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) \
53 for_each_set_bit(prefix, (prefix_usage)->b, MLXSW_SP_PREFIX_COUNT)
54
55static bool
Jiri Pirko6b75c482016-07-04 08:23:09 +020056mlxsw_sp_prefix_usage_subset(struct mlxsw_sp_prefix_usage *prefix_usage1,
57 struct mlxsw_sp_prefix_usage *prefix_usage2)
58{
59 unsigned char prefix;
60
61 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage1) {
62 if (!test_bit(prefix, prefix_usage2->b))
63 return false;
64 }
65 return true;
66}
67
68static bool
Jiri Pirko53342022016-07-04 08:23:08 +020069mlxsw_sp_prefix_usage_eq(struct mlxsw_sp_prefix_usage *prefix_usage1,
70 struct mlxsw_sp_prefix_usage *prefix_usage2)
71{
72 return !memcmp(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
73}
74
Jiri Pirko6b75c482016-07-04 08:23:09 +020075static bool
76mlxsw_sp_prefix_usage_none(struct mlxsw_sp_prefix_usage *prefix_usage)
77{
78 struct mlxsw_sp_prefix_usage prefix_usage_none = {{ 0 } };
79
80 return mlxsw_sp_prefix_usage_eq(prefix_usage, &prefix_usage_none);
81}
82
83static void
84mlxsw_sp_prefix_usage_cpy(struct mlxsw_sp_prefix_usage *prefix_usage1,
85 struct mlxsw_sp_prefix_usage *prefix_usage2)
86{
87 memcpy(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
88}
89
90static void
91mlxsw_sp_prefix_usage_zero(struct mlxsw_sp_prefix_usage *prefix_usage)
92{
93 memset(prefix_usage, 0, sizeof(*prefix_usage));
94}
95
Jiri Pirko5e9c16c2016-07-04 08:23:04 +020096static void
97mlxsw_sp_prefix_usage_set(struct mlxsw_sp_prefix_usage *prefix_usage,
98 unsigned char prefix_len)
99{
100 set_bit(prefix_len, prefix_usage->b);
101}
102
103static void
104mlxsw_sp_prefix_usage_clear(struct mlxsw_sp_prefix_usage *prefix_usage,
105 unsigned char prefix_len)
106{
107 clear_bit(prefix_len, prefix_usage->b);
108}
109
110struct mlxsw_sp_fib_key {
Jiri Pirko5b004412016-09-01 10:37:40 +0200111 struct net_device *dev;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200112 unsigned char addr[sizeof(struct in6_addr)];
113 unsigned char prefix_len;
114};
115
Jiri Pirko61c503f2016-07-04 08:23:11 +0200116enum mlxsw_sp_fib_entry_type {
117 MLXSW_SP_FIB_ENTRY_TYPE_REMOTE,
118 MLXSW_SP_FIB_ENTRY_TYPE_LOCAL,
119 MLXSW_SP_FIB_ENTRY_TYPE_TRAP,
120};
121
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200122struct mlxsw_sp_nexthop_group;
123
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200124struct mlxsw_sp_fib_entry {
125 struct rhash_head ht_node;
Jiri Pirkob45f64d2016-09-26 12:52:31 +0200126 struct list_head list;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200127 struct mlxsw_sp_fib_key key;
Jiri Pirko61c503f2016-07-04 08:23:11 +0200128 enum mlxsw_sp_fib_entry_type type;
Jiri Pirko5b004412016-09-01 10:37:40 +0200129 unsigned int ref_count;
Jiri Pirko61c503f2016-07-04 08:23:11 +0200130 struct mlxsw_sp_vr *vr;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200131 struct list_head nexthop_group_node;
132 struct mlxsw_sp_nexthop_group *nh_group;
Ido Schimmel013b20f2017-02-08 11:16:36 +0100133 bool offloaded;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200134};
135
136struct mlxsw_sp_fib {
137 struct rhashtable ht;
Jiri Pirkob45f64d2016-09-26 12:52:31 +0200138 struct list_head entry_list;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200139 unsigned long prefix_ref_count[MLXSW_SP_PREFIX_COUNT];
140 struct mlxsw_sp_prefix_usage prefix_usage;
141};
142
143static const struct rhashtable_params mlxsw_sp_fib_ht_params = {
144 .key_offset = offsetof(struct mlxsw_sp_fib_entry, key),
145 .head_offset = offsetof(struct mlxsw_sp_fib_entry, ht_node),
146 .key_len = sizeof(struct mlxsw_sp_fib_key),
147 .automatic_shrinking = true,
148};
149
150static int mlxsw_sp_fib_entry_insert(struct mlxsw_sp_fib *fib,
151 struct mlxsw_sp_fib_entry *fib_entry)
152{
153 unsigned char prefix_len = fib_entry->key.prefix_len;
154 int err;
155
156 err = rhashtable_insert_fast(&fib->ht, &fib_entry->ht_node,
157 mlxsw_sp_fib_ht_params);
158 if (err)
159 return err;
Jiri Pirkob45f64d2016-09-26 12:52:31 +0200160 list_add_tail(&fib_entry->list, &fib->entry_list);
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200161 if (fib->prefix_ref_count[prefix_len]++ == 0)
162 mlxsw_sp_prefix_usage_set(&fib->prefix_usage, prefix_len);
163 return 0;
164}
165
166static void mlxsw_sp_fib_entry_remove(struct mlxsw_sp_fib *fib,
167 struct mlxsw_sp_fib_entry *fib_entry)
168{
169 unsigned char prefix_len = fib_entry->key.prefix_len;
170
171 if (--fib->prefix_ref_count[prefix_len] == 0)
172 mlxsw_sp_prefix_usage_clear(&fib->prefix_usage, prefix_len);
Jiri Pirkob45f64d2016-09-26 12:52:31 +0200173 list_del(&fib_entry->list);
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200174 rhashtable_remove_fast(&fib->ht, &fib_entry->ht_node,
175 mlxsw_sp_fib_ht_params);
176}
177
178static struct mlxsw_sp_fib_entry *
179mlxsw_sp_fib_entry_create(struct mlxsw_sp_fib *fib, const void *addr,
Jiri Pirko5b004412016-09-01 10:37:40 +0200180 size_t addr_len, unsigned char prefix_len,
181 struct net_device *dev)
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200182{
183 struct mlxsw_sp_fib_entry *fib_entry;
184
185 fib_entry = kzalloc(sizeof(*fib_entry), GFP_KERNEL);
186 if (!fib_entry)
187 return NULL;
Jiri Pirko5b004412016-09-01 10:37:40 +0200188 fib_entry->key.dev = dev;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200189 memcpy(fib_entry->key.addr, addr, addr_len);
190 fib_entry->key.prefix_len = prefix_len;
191 return fib_entry;
192}
193
194static void mlxsw_sp_fib_entry_destroy(struct mlxsw_sp_fib_entry *fib_entry)
195{
196 kfree(fib_entry);
197}
198
199static struct mlxsw_sp_fib_entry *
200mlxsw_sp_fib_entry_lookup(struct mlxsw_sp_fib *fib, const void *addr,
Jiri Pirko5b004412016-09-01 10:37:40 +0200201 size_t addr_len, unsigned char prefix_len,
202 struct net_device *dev)
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200203{
Jiri Pirko5b004412016-09-01 10:37:40 +0200204 struct mlxsw_sp_fib_key key;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200205
Jiri Pirko5b004412016-09-01 10:37:40 +0200206 memset(&key, 0, sizeof(key));
207 key.dev = dev;
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200208 memcpy(key.addr, addr, addr_len);
209 key.prefix_len = prefix_len;
210 return rhashtable_lookup_fast(&fib->ht, &key, mlxsw_sp_fib_ht_params);
211}
212
213static struct mlxsw_sp_fib *mlxsw_sp_fib_create(void)
214{
215 struct mlxsw_sp_fib *fib;
216 int err;
217
218 fib = kzalloc(sizeof(*fib), GFP_KERNEL);
219 if (!fib)
220 return ERR_PTR(-ENOMEM);
221 err = rhashtable_init(&fib->ht, &mlxsw_sp_fib_ht_params);
222 if (err)
223 goto err_rhashtable_init;
Jiri Pirkob45f64d2016-09-26 12:52:31 +0200224 INIT_LIST_HEAD(&fib->entry_list);
Jiri Pirko5e9c16c2016-07-04 08:23:04 +0200225 return fib;
226
227err_rhashtable_init:
228 kfree(fib);
229 return ERR_PTR(err);
230}
231
232static void mlxsw_sp_fib_destroy(struct mlxsw_sp_fib *fib)
233{
234 rhashtable_destroy(&fib->ht);
235 kfree(fib);
236}
237
Jiri Pirko53342022016-07-04 08:23:08 +0200238static struct mlxsw_sp_lpm_tree *
239mlxsw_sp_lpm_tree_find_unused(struct mlxsw_sp *mlxsw_sp, bool one_reserved)
240{
241 static struct mlxsw_sp_lpm_tree *lpm_tree;
242 int i;
243
244 for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) {
245 lpm_tree = &mlxsw_sp->router.lpm_trees[i];
246 if (lpm_tree->ref_count == 0) {
247 if (one_reserved)
248 one_reserved = false;
249 else
250 return lpm_tree;
251 }
252 }
253 return NULL;
254}
255
256static int mlxsw_sp_lpm_tree_alloc(struct mlxsw_sp *mlxsw_sp,
257 struct mlxsw_sp_lpm_tree *lpm_tree)
258{
259 char ralta_pl[MLXSW_REG_RALTA_LEN];
260
Ido Schimmel1a9234e662016-09-19 08:29:26 +0200261 mlxsw_reg_ralta_pack(ralta_pl, true,
262 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
263 lpm_tree->id);
Jiri Pirko53342022016-07-04 08:23:08 +0200264 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
265}
266
267static int mlxsw_sp_lpm_tree_free(struct mlxsw_sp *mlxsw_sp,
268 struct mlxsw_sp_lpm_tree *lpm_tree)
269{
270 char ralta_pl[MLXSW_REG_RALTA_LEN];
271
Ido Schimmel1a9234e662016-09-19 08:29:26 +0200272 mlxsw_reg_ralta_pack(ralta_pl, false,
273 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
274 lpm_tree->id);
Jiri Pirko53342022016-07-04 08:23:08 +0200275 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
276}
277
278static int
279mlxsw_sp_lpm_tree_left_struct_set(struct mlxsw_sp *mlxsw_sp,
280 struct mlxsw_sp_prefix_usage *prefix_usage,
281 struct mlxsw_sp_lpm_tree *lpm_tree)
282{
283 char ralst_pl[MLXSW_REG_RALST_LEN];
284 u8 root_bin = 0;
285 u8 prefix;
286 u8 last_prefix = MLXSW_REG_RALST_BIN_NO_CHILD;
287
288 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage)
289 root_bin = prefix;
290
291 mlxsw_reg_ralst_pack(ralst_pl, root_bin, lpm_tree->id);
292 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) {
293 if (prefix == 0)
294 continue;
295 mlxsw_reg_ralst_bin_pack(ralst_pl, prefix, last_prefix,
296 MLXSW_REG_RALST_BIN_NO_CHILD);
297 last_prefix = prefix;
298 }
299 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
300}
301
302static struct mlxsw_sp_lpm_tree *
303mlxsw_sp_lpm_tree_create(struct mlxsw_sp *mlxsw_sp,
304 struct mlxsw_sp_prefix_usage *prefix_usage,
305 enum mlxsw_sp_l3proto proto, bool one_reserved)
306{
307 struct mlxsw_sp_lpm_tree *lpm_tree;
308 int err;
309
310 lpm_tree = mlxsw_sp_lpm_tree_find_unused(mlxsw_sp, one_reserved);
311 if (!lpm_tree)
312 return ERR_PTR(-EBUSY);
313 lpm_tree->proto = proto;
314 err = mlxsw_sp_lpm_tree_alloc(mlxsw_sp, lpm_tree);
315 if (err)
316 return ERR_PTR(err);
317
318 err = mlxsw_sp_lpm_tree_left_struct_set(mlxsw_sp, prefix_usage,
319 lpm_tree);
320 if (err)
321 goto err_left_struct_set;
Jiri Pirko2083d362016-10-25 11:25:56 +0200322 memcpy(&lpm_tree->prefix_usage, prefix_usage,
323 sizeof(lpm_tree->prefix_usage));
Jiri Pirko53342022016-07-04 08:23:08 +0200324 return lpm_tree;
325
326err_left_struct_set:
327 mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
328 return ERR_PTR(err);
329}
330
331static int mlxsw_sp_lpm_tree_destroy(struct mlxsw_sp *mlxsw_sp,
332 struct mlxsw_sp_lpm_tree *lpm_tree)
333{
334 return mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
335}
336
337static struct mlxsw_sp_lpm_tree *
338mlxsw_sp_lpm_tree_get(struct mlxsw_sp *mlxsw_sp,
339 struct mlxsw_sp_prefix_usage *prefix_usage,
340 enum mlxsw_sp_l3proto proto, bool one_reserved)
341{
342 struct mlxsw_sp_lpm_tree *lpm_tree;
343 int i;
344
345 for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) {
346 lpm_tree = &mlxsw_sp->router.lpm_trees[i];
Jiri Pirko8b99bec2016-10-25 11:25:57 +0200347 if (lpm_tree->ref_count != 0 &&
348 lpm_tree->proto == proto &&
Jiri Pirko53342022016-07-04 08:23:08 +0200349 mlxsw_sp_prefix_usage_eq(&lpm_tree->prefix_usage,
350 prefix_usage))
351 goto inc_ref_count;
352 }
353 lpm_tree = mlxsw_sp_lpm_tree_create(mlxsw_sp, prefix_usage,
354 proto, one_reserved);
355 if (IS_ERR(lpm_tree))
356 return lpm_tree;
357
358inc_ref_count:
359 lpm_tree->ref_count++;
360 return lpm_tree;
361}
362
363static int mlxsw_sp_lpm_tree_put(struct mlxsw_sp *mlxsw_sp,
364 struct mlxsw_sp_lpm_tree *lpm_tree)
365{
366 if (--lpm_tree->ref_count == 0)
367 return mlxsw_sp_lpm_tree_destroy(mlxsw_sp, lpm_tree);
368 return 0;
369}
370
371static void mlxsw_sp_lpm_init(struct mlxsw_sp *mlxsw_sp)
372{
373 struct mlxsw_sp_lpm_tree *lpm_tree;
374 int i;
375
376 for (i = 0; i < MLXSW_SP_LPM_TREE_COUNT; i++) {
377 lpm_tree = &mlxsw_sp->router.lpm_trees[i];
378 lpm_tree->id = i + MLXSW_SP_LPM_TREE_MIN;
379 }
380}
381
Jiri Pirko6b75c482016-07-04 08:23:09 +0200382static struct mlxsw_sp_vr *mlxsw_sp_vr_find_unused(struct mlxsw_sp *mlxsw_sp)
383{
384 struct mlxsw_sp_vr *vr;
385 int i;
386
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200387 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
Jiri Pirko6b75c482016-07-04 08:23:09 +0200388 vr = &mlxsw_sp->router.vrs[i];
389 if (!vr->used)
390 return vr;
391 }
392 return NULL;
393}
394
395static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp *mlxsw_sp,
396 struct mlxsw_sp_vr *vr)
397{
398 char raltb_pl[MLXSW_REG_RALTB_LEN];
399
Ido Schimmel1a9234e662016-09-19 08:29:26 +0200400 mlxsw_reg_raltb_pack(raltb_pl, vr->id,
401 (enum mlxsw_reg_ralxx_protocol) vr->proto,
402 vr->lpm_tree->id);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200403 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
404}
405
406static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp *mlxsw_sp,
407 struct mlxsw_sp_vr *vr)
408{
409 char raltb_pl[MLXSW_REG_RALTB_LEN];
410
411 /* Bind to tree 0 which is default */
Ido Schimmel1a9234e662016-09-19 08:29:26 +0200412 mlxsw_reg_raltb_pack(raltb_pl, vr->id,
413 (enum mlxsw_reg_ralxx_protocol) vr->proto, 0);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200414 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
415}
416
417static u32 mlxsw_sp_fix_tb_id(u32 tb_id)
418{
419 /* For our purpose, squash main and local table into one */
420 if (tb_id == RT_TABLE_LOCAL)
421 tb_id = RT_TABLE_MAIN;
422 return tb_id;
423}
424
425static struct mlxsw_sp_vr *mlxsw_sp_vr_find(struct mlxsw_sp *mlxsw_sp,
426 u32 tb_id,
427 enum mlxsw_sp_l3proto proto)
428{
429 struct mlxsw_sp_vr *vr;
430 int i;
431
432 tb_id = mlxsw_sp_fix_tb_id(tb_id);
Nogah Frankel9497c042016-09-20 11:16:54 +0200433
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200434 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
Jiri Pirko6b75c482016-07-04 08:23:09 +0200435 vr = &mlxsw_sp->router.vrs[i];
436 if (vr->used && vr->proto == proto && vr->tb_id == tb_id)
437 return vr;
438 }
439 return NULL;
440}
441
442static struct mlxsw_sp_vr *mlxsw_sp_vr_create(struct mlxsw_sp *mlxsw_sp,
443 unsigned char prefix_len,
444 u32 tb_id,
445 enum mlxsw_sp_l3proto proto)
446{
447 struct mlxsw_sp_prefix_usage req_prefix_usage;
448 struct mlxsw_sp_lpm_tree *lpm_tree;
449 struct mlxsw_sp_vr *vr;
450 int err;
451
452 vr = mlxsw_sp_vr_find_unused(mlxsw_sp);
453 if (!vr)
454 return ERR_PTR(-EBUSY);
455 vr->fib = mlxsw_sp_fib_create();
456 if (IS_ERR(vr->fib))
457 return ERR_CAST(vr->fib);
458
459 vr->proto = proto;
460 vr->tb_id = tb_id;
461 mlxsw_sp_prefix_usage_zero(&req_prefix_usage);
462 mlxsw_sp_prefix_usage_set(&req_prefix_usage, prefix_len);
463 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
464 proto, true);
465 if (IS_ERR(lpm_tree)) {
466 err = PTR_ERR(lpm_tree);
467 goto err_tree_get;
468 }
469 vr->lpm_tree = lpm_tree;
470 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, vr);
471 if (err)
472 goto err_tree_bind;
473
474 vr->used = true;
475 return vr;
476
477err_tree_bind:
478 mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree);
479err_tree_get:
480 mlxsw_sp_fib_destroy(vr->fib);
481
482 return ERR_PTR(err);
483}
484
485static void mlxsw_sp_vr_destroy(struct mlxsw_sp *mlxsw_sp,
486 struct mlxsw_sp_vr *vr)
487{
488 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, vr);
489 mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree);
490 mlxsw_sp_fib_destroy(vr->fib);
491 vr->used = false;
492}
493
494static int
495mlxsw_sp_vr_lpm_tree_check(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr,
496 struct mlxsw_sp_prefix_usage *req_prefix_usage)
497{
498 struct mlxsw_sp_lpm_tree *lpm_tree;
499
500 if (mlxsw_sp_prefix_usage_eq(req_prefix_usage,
501 &vr->lpm_tree->prefix_usage))
502 return 0;
503
504 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, req_prefix_usage,
505 vr->proto, false);
506 if (IS_ERR(lpm_tree)) {
507 /* We failed to get a tree according to the required
508 * prefix usage. However, the current tree might be still good
509 * for us if our requirement is subset of the prefixes used
510 * in the tree.
511 */
512 if (mlxsw_sp_prefix_usage_subset(req_prefix_usage,
513 &vr->lpm_tree->prefix_usage))
514 return 0;
515 return PTR_ERR(lpm_tree);
516 }
517
518 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, vr);
519 mlxsw_sp_lpm_tree_put(mlxsw_sp, vr->lpm_tree);
520 vr->lpm_tree = lpm_tree;
521 return mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, vr);
522}
523
524static struct mlxsw_sp_vr *mlxsw_sp_vr_get(struct mlxsw_sp *mlxsw_sp,
525 unsigned char prefix_len,
526 u32 tb_id,
527 enum mlxsw_sp_l3proto proto)
528{
529 struct mlxsw_sp_vr *vr;
530 int err;
531
532 tb_id = mlxsw_sp_fix_tb_id(tb_id);
533 vr = mlxsw_sp_vr_find(mlxsw_sp, tb_id, proto);
534 if (!vr) {
535 vr = mlxsw_sp_vr_create(mlxsw_sp, prefix_len, tb_id, proto);
536 if (IS_ERR(vr))
537 return vr;
538 } else {
539 struct mlxsw_sp_prefix_usage req_prefix_usage;
540
541 mlxsw_sp_prefix_usage_cpy(&req_prefix_usage,
542 &vr->fib->prefix_usage);
543 mlxsw_sp_prefix_usage_set(&req_prefix_usage, prefix_len);
544 /* Need to replace LPM tree in case new prefix is required. */
545 err = mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, vr,
546 &req_prefix_usage);
547 if (err)
548 return ERR_PTR(err);
549 }
550 return vr;
551}
552
553static void mlxsw_sp_vr_put(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr)
554{
555 /* Destroy virtual router entity in case the associated FIB is empty
556 * and allow it to be used for other tables in future. Otherwise,
557 * check if some prefix usage did not disappear and change tree if
558 * that is the case. Note that in case new, smaller tree cannot be
559 * allocated, the original one will be kept being used.
560 */
561 if (mlxsw_sp_prefix_usage_none(&vr->fib->prefix_usage))
562 mlxsw_sp_vr_destroy(mlxsw_sp, vr);
563 else
564 mlxsw_sp_vr_lpm_tree_check(mlxsw_sp, vr,
565 &vr->fib->prefix_usage);
566}
567
Nogah Frankel9497c042016-09-20 11:16:54 +0200568static int mlxsw_sp_vrs_init(struct mlxsw_sp *mlxsw_sp)
Jiri Pirko6b75c482016-07-04 08:23:09 +0200569{
570 struct mlxsw_sp_vr *vr;
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200571 u64 max_vrs;
Jiri Pirko6b75c482016-07-04 08:23:09 +0200572 int i;
573
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200574 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_VRS))
Nogah Frankel9497c042016-09-20 11:16:54 +0200575 return -EIO;
576
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200577 max_vrs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS);
578 mlxsw_sp->router.vrs = kcalloc(max_vrs, sizeof(struct mlxsw_sp_vr),
Nogah Frankel9497c042016-09-20 11:16:54 +0200579 GFP_KERNEL);
580 if (!mlxsw_sp->router.vrs)
581 return -ENOMEM;
582
Jiri Pirkoc1a38312016-10-21 16:07:23 +0200583 for (i = 0; i < max_vrs; i++) {
Jiri Pirko6b75c482016-07-04 08:23:09 +0200584 vr = &mlxsw_sp->router.vrs[i];
585 vr->id = i;
586 }
Nogah Frankel9497c042016-09-20 11:16:54 +0200587
588 return 0;
589}
590
Ido Schimmelac571de2016-11-14 11:26:32 +0100591static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp);
592
Nogah Frankel9497c042016-09-20 11:16:54 +0200593static void mlxsw_sp_vrs_fini(struct mlxsw_sp *mlxsw_sp)
594{
Ido Schimmel30572242016-12-03 16:45:01 +0100595 /* At this stage we're guaranteed not to have new incoming
596 * FIB notifications and the work queue is free from FIBs
597 * sitting on top of mlxsw netdevs. However, we can still
598 * have other FIBs queued. Flush the queue before flushing
599 * the device's tables. No need for locks, as we're the only
600 * writer.
601 */
602 mlxsw_core_flush_owq();
Ido Schimmelac571de2016-11-14 11:26:32 +0100603 mlxsw_sp_router_fib_flush(mlxsw_sp);
Nogah Frankel9497c042016-09-20 11:16:54 +0200604 kfree(mlxsw_sp->router.vrs);
Jiri Pirko6b75c482016-07-04 08:23:09 +0200605}
606
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200607struct mlxsw_sp_neigh_key {
Jiri Pirko33b13412016-11-10 12:31:04 +0100608 struct neighbour *n;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200609};
610
611struct mlxsw_sp_neigh_entry {
Ido Schimmel9665b742017-02-08 11:16:42 +0100612 struct list_head rif_list_node;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200613 struct rhash_head ht_node;
614 struct mlxsw_sp_neigh_key key;
615 u16 rif;
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100616 bool connected;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200617 unsigned char ha[ETH_ALEN];
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200618 struct list_head nexthop_list; /* list of nexthops using
619 * this neigh entry
620 */
Yotam Gigib2157142016-07-05 11:27:51 +0200621 struct list_head nexthop_neighs_list_node;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200622};
623
624static const struct rhashtable_params mlxsw_sp_neigh_ht_params = {
625 .key_offset = offsetof(struct mlxsw_sp_neigh_entry, key),
626 .head_offset = offsetof(struct mlxsw_sp_neigh_entry, ht_node),
627 .key_len = sizeof(struct mlxsw_sp_neigh_key),
628};
629
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100630static struct mlxsw_sp_neigh_entry *
631mlxsw_sp_neigh_entry_alloc(struct mlxsw_sp *mlxsw_sp, struct neighbour *n,
632 u16 rif)
633{
634 struct mlxsw_sp_neigh_entry *neigh_entry;
635
636 neigh_entry = kzalloc(sizeof(*neigh_entry), GFP_KERNEL);
637 if (!neigh_entry)
638 return NULL;
639
640 neigh_entry->key.n = n;
641 neigh_entry->rif = rif;
642 INIT_LIST_HEAD(&neigh_entry->nexthop_list);
643
644 return neigh_entry;
645}
646
647static void mlxsw_sp_neigh_entry_free(struct mlxsw_sp_neigh_entry *neigh_entry)
648{
649 kfree(neigh_entry);
650}
651
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200652static int
653mlxsw_sp_neigh_entry_insert(struct mlxsw_sp *mlxsw_sp,
654 struct mlxsw_sp_neigh_entry *neigh_entry)
655{
656 return rhashtable_insert_fast(&mlxsw_sp->router.neigh_ht,
657 &neigh_entry->ht_node,
658 mlxsw_sp_neigh_ht_params);
659}
660
661static void
662mlxsw_sp_neigh_entry_remove(struct mlxsw_sp *mlxsw_sp,
663 struct mlxsw_sp_neigh_entry *neigh_entry)
664{
665 rhashtable_remove_fast(&mlxsw_sp->router.neigh_ht,
666 &neigh_entry->ht_node,
667 mlxsw_sp_neigh_ht_params);
668}
669
670static struct mlxsw_sp_neigh_entry *
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100671mlxsw_sp_neigh_entry_create(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200672{
673 struct mlxsw_sp_neigh_entry *neigh_entry;
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100674 struct mlxsw_sp_rif *r;
675 int err;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200676
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100677 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, n->dev);
678 if (!r)
679 return ERR_PTR(-EINVAL);
680
681 neigh_entry = mlxsw_sp_neigh_entry_alloc(mlxsw_sp, n, r->rif);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200682 if (!neigh_entry)
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100683 return ERR_PTR(-ENOMEM);
684
685 err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
686 if (err)
687 goto err_neigh_entry_insert;
688
Ido Schimmel9665b742017-02-08 11:16:42 +0100689 list_add(&neigh_entry->rif_list_node, &r->neigh_list);
690
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200691 return neigh_entry;
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100692
693err_neigh_entry_insert:
694 mlxsw_sp_neigh_entry_free(neigh_entry);
695 return ERR_PTR(err);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200696}
697
698static void
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100699mlxsw_sp_neigh_entry_destroy(struct mlxsw_sp *mlxsw_sp,
700 struct mlxsw_sp_neigh_entry *neigh_entry)
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200701{
Ido Schimmel9665b742017-02-08 11:16:42 +0100702 list_del(&neigh_entry->rif_list_node);
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100703 mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry);
704 mlxsw_sp_neigh_entry_free(neigh_entry);
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200705}
706
707static struct mlxsw_sp_neigh_entry *
Jiri Pirko33b13412016-11-10 12:31:04 +0100708mlxsw_sp_neigh_entry_lookup(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200709{
Jiri Pirko33b13412016-11-10 12:31:04 +0100710 struct mlxsw_sp_neigh_key key;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200711
Jiri Pirko33b13412016-11-10 12:31:04 +0100712 key.n = n;
Jiri Pirko6cf3c972016-07-05 11:27:39 +0200713 return rhashtable_lookup_fast(&mlxsw_sp->router.neigh_ht,
714 &key, mlxsw_sp_neigh_ht_params);
715}
716
Yotam Gigic723c7352016-07-05 11:27:43 +0200717static void
718mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp *mlxsw_sp)
719{
720 unsigned long interval = NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME);
721
722 mlxsw_sp->router.neighs_update.interval = jiffies_to_msecs(interval);
723}
724
725static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
726 char *rauhtd_pl,
727 int ent_index)
728{
729 struct net_device *dev;
730 struct neighbour *n;
731 __be32 dipn;
732 u32 dip;
733 u16 rif;
734
735 mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl, ent_index, &rif, &dip);
736
737 if (!mlxsw_sp->rifs[rif]) {
738 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
739 return;
740 }
741
742 dipn = htonl(dip);
743 dev = mlxsw_sp->rifs[rif]->dev;
744 n = neigh_lookup(&arp_tbl, &dipn, dev);
745 if (!n) {
746 netdev_err(dev, "Failed to find matching neighbour for IP=%pI4h\n",
747 &dip);
748 return;
749 }
750
751 netdev_dbg(dev, "Updating neighbour with IP=%pI4h\n", &dip);
752 neigh_event_send(n, NULL);
753 neigh_release(n);
754}
755
756static void mlxsw_sp_router_neigh_rec_ipv4_process(struct mlxsw_sp *mlxsw_sp,
757 char *rauhtd_pl,
758 int rec_index)
759{
760 u8 num_entries;
761 int i;
762
763 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
764 rec_index);
765 /* Hardware starts counting at 0, so add 1. */
766 num_entries++;
767
768 /* Each record consists of several neighbour entries. */
769 for (i = 0; i < num_entries; i++) {
770 int ent_index;
771
772 ent_index = rec_index * MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC + i;
773 mlxsw_sp_router_neigh_ent_ipv4_process(mlxsw_sp, rauhtd_pl,
774 ent_index);
775 }
776
777}
778
779static void mlxsw_sp_router_neigh_rec_process(struct mlxsw_sp *mlxsw_sp,
780 char *rauhtd_pl, int rec_index)
781{
782 switch (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, rec_index)) {
783 case MLXSW_REG_RAUHTD_TYPE_IPV4:
784 mlxsw_sp_router_neigh_rec_ipv4_process(mlxsw_sp, rauhtd_pl,
785 rec_index);
786 break;
787 case MLXSW_REG_RAUHTD_TYPE_IPV6:
788 WARN_ON_ONCE(1);
789 break;
790 }
791}
792
Arkadi Sharshevsky42cdb332016-11-11 16:34:26 +0100793static bool mlxsw_sp_router_rauhtd_is_full(char *rauhtd_pl)
794{
795 u8 num_rec, last_rec_index, num_entries;
796
797 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
798 last_rec_index = num_rec - 1;
799
800 if (num_rec < MLXSW_REG_RAUHTD_REC_MAX_NUM)
801 return false;
802 if (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, last_rec_index) ==
803 MLXSW_REG_RAUHTD_TYPE_IPV6)
804 return true;
805
806 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
807 last_rec_index);
808 if (++num_entries == MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC)
809 return true;
810 return false;
811}
812
Yotam Gigib2157142016-07-05 11:27:51 +0200813static int mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp *mlxsw_sp)
Yotam Gigic723c7352016-07-05 11:27:43 +0200814{
Yotam Gigic723c7352016-07-05 11:27:43 +0200815 char *rauhtd_pl;
816 u8 num_rec;
817 int i, err;
818
819 rauhtd_pl = kmalloc(MLXSW_REG_RAUHTD_LEN, GFP_KERNEL);
820 if (!rauhtd_pl)
Yotam Gigib2157142016-07-05 11:27:51 +0200821 return -ENOMEM;
Yotam Gigic723c7352016-07-05 11:27:43 +0200822
823 /* Make sure the neighbour's netdev isn't removed in the
824 * process.
825 */
826 rtnl_lock();
827 do {
828 mlxsw_reg_rauhtd_pack(rauhtd_pl, MLXSW_REG_RAUHTD_TYPE_IPV4);
829 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(rauhtd),
830 rauhtd_pl);
831 if (err) {
832 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to dump neighbour talbe\n");
833 break;
834 }
835 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
836 for (i = 0; i < num_rec; i++)
837 mlxsw_sp_router_neigh_rec_process(mlxsw_sp, rauhtd_pl,
838 i);
Arkadi Sharshevsky42cdb332016-11-11 16:34:26 +0100839 } while (mlxsw_sp_router_rauhtd_is_full(rauhtd_pl));
Yotam Gigic723c7352016-07-05 11:27:43 +0200840 rtnl_unlock();
841
842 kfree(rauhtd_pl);
Yotam Gigib2157142016-07-05 11:27:51 +0200843 return err;
844}
845
846static void mlxsw_sp_router_neighs_update_nh(struct mlxsw_sp *mlxsw_sp)
847{
848 struct mlxsw_sp_neigh_entry *neigh_entry;
849
850 /* Take RTNL mutex here to prevent lists from changes */
851 rtnl_lock();
852 list_for_each_entry(neigh_entry, &mlxsw_sp->router.nexthop_neighs_list,
Ido Schimmel8a0b7272017-02-06 16:20:15 +0100853 nexthop_neighs_list_node)
Yotam Gigib2157142016-07-05 11:27:51 +0200854 /* If this neigh have nexthops, make the kernel think this neigh
855 * is active regardless of the traffic.
856 */
Ido Schimmel8a0b7272017-02-06 16:20:15 +0100857 neigh_event_send(neigh_entry->key.n, NULL);
Yotam Gigib2157142016-07-05 11:27:51 +0200858 rtnl_unlock();
859}
860
861static void
862mlxsw_sp_router_neighs_update_work_schedule(struct mlxsw_sp *mlxsw_sp)
863{
864 unsigned long interval = mlxsw_sp->router.neighs_update.interval;
865
866 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw,
867 msecs_to_jiffies(interval));
868}
869
870static void mlxsw_sp_router_neighs_update_work(struct work_struct *work)
871{
872 struct mlxsw_sp *mlxsw_sp = container_of(work, struct mlxsw_sp,
873 router.neighs_update.dw.work);
874 int err;
875
876 err = mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp);
877 if (err)
878 dev_err(mlxsw_sp->bus_info->dev, "Could not update kernel for neigh activity");
879
880 mlxsw_sp_router_neighs_update_nh(mlxsw_sp);
881
Yotam Gigic723c7352016-07-05 11:27:43 +0200882 mlxsw_sp_router_neighs_update_work_schedule(mlxsw_sp);
883}
884
Yotam Gigi0b2361d2016-07-05 11:27:52 +0200885static void mlxsw_sp_router_probe_unresolved_nexthops(struct work_struct *work)
886{
887 struct mlxsw_sp_neigh_entry *neigh_entry;
888 struct mlxsw_sp *mlxsw_sp = container_of(work, struct mlxsw_sp,
889 router.nexthop_probe_dw.work);
890
891 /* Iterate over nexthop neighbours, find those who are unresolved and
892 * send arp on them. This solves the chicken-egg problem when
893 * the nexthop wouldn't get offloaded until the neighbor is resolved
894 * but it wouldn't get resolved ever in case traffic is flowing in HW
895 * using different nexthop.
896 *
897 * Take RTNL mutex here to prevent lists from changes.
898 */
899 rtnl_lock();
900 list_for_each_entry(neigh_entry, &mlxsw_sp->router.nexthop_neighs_list,
Ido Schimmel8a0b7272017-02-06 16:20:15 +0100901 nexthop_neighs_list_node)
Ido Schimmel01b1aa32017-02-06 16:20:16 +0100902 if (!neigh_entry->connected)
Jiri Pirko33b13412016-11-10 12:31:04 +0100903 neigh_event_send(neigh_entry->key.n, NULL);
Yotam Gigi0b2361d2016-07-05 11:27:52 +0200904 rtnl_unlock();
905
906 mlxsw_core_schedule_dw(&mlxsw_sp->router.nexthop_probe_dw,
907 MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL);
908}
909
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +0200910static void
911mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
912 struct mlxsw_sp_neigh_entry *neigh_entry,
913 bool removing);
914
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100915static enum mlxsw_reg_rauht_op mlxsw_sp_rauht_op(bool adding)
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200916{
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100917 return adding ? MLXSW_REG_RAUHT_OP_WRITE_ADD :
918 MLXSW_REG_RAUHT_OP_WRITE_DELETE;
919}
920
921static void
922mlxsw_sp_router_neigh_entry_op4(struct mlxsw_sp *mlxsw_sp,
923 struct mlxsw_sp_neigh_entry *neigh_entry,
924 enum mlxsw_reg_rauht_op op)
925{
Jiri Pirko33b13412016-11-10 12:31:04 +0100926 struct neighbour *n = neigh_entry->key.n;
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100927 u32 dip = ntohl(*((__be32 *) n->primary_key));
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200928 char rauht_pl[MLXSW_REG_RAUHT_LEN];
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100929
930 mlxsw_reg_rauht_pack4(rauht_pl, op, neigh_entry->rif, neigh_entry->ha,
931 dip);
932 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
933}
934
935static void
936mlxsw_sp_neigh_entry_update(struct mlxsw_sp *mlxsw_sp,
937 struct mlxsw_sp_neigh_entry *neigh_entry,
938 bool adding)
939{
940 if (!adding && !neigh_entry->connected)
941 return;
942 neigh_entry->connected = adding;
943 if (neigh_entry->key.n->tbl == &arp_tbl)
944 mlxsw_sp_router_neigh_entry_op4(mlxsw_sp, neigh_entry,
945 mlxsw_sp_rauht_op(adding));
946 else
947 WARN_ON_ONCE(1);
948}
949
950struct mlxsw_sp_neigh_event_work {
951 struct work_struct work;
952 struct mlxsw_sp *mlxsw_sp;
953 struct neighbour *n;
954};
955
956static void mlxsw_sp_router_neigh_event_work(struct work_struct *work)
957{
958 struct mlxsw_sp_neigh_event_work *neigh_work =
959 container_of(work, struct mlxsw_sp_neigh_event_work, work);
960 struct mlxsw_sp *mlxsw_sp = neigh_work->mlxsw_sp;
961 struct mlxsw_sp_neigh_entry *neigh_entry;
962 struct neighbour *n = neigh_work->n;
963 unsigned char ha[ETH_ALEN];
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200964 bool entry_connected;
Ido Schimmel93a87e52016-12-23 09:32:49 +0100965 u8 nud_state, dead;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200966
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100967 /* If these parameters are changed after we release the lock,
968 * then we are guaranteed to receive another event letting us
969 * know about it.
970 */
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200971 read_lock_bh(&n->lock);
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100972 memcpy(ha, n->ha, ETH_ALEN);
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200973 nud_state = n->nud_state;
Ido Schimmel93a87e52016-12-23 09:32:49 +0100974 dead = n->dead;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200975 read_unlock_bh(&n->lock);
976
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100977 rtnl_lock();
Ido Schimmel93a87e52016-12-23 09:32:49 +0100978 entry_connected = nud_state & NUD_VALID && !dead;
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100979 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
980 if (!entry_connected && !neigh_entry)
981 goto out;
982 if (!neigh_entry) {
983 neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
984 if (IS_ERR(neigh_entry))
985 goto out;
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200986 }
987
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100988 memcpy(neigh_entry->ha, ha, ETH_ALEN);
989 mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, entry_connected);
990 mlxsw_sp_nexthop_neigh_update(mlxsw_sp, neigh_entry, !entry_connected);
991
992 if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
993 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
994
995out:
996 rtnl_unlock();
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200997 neigh_release(n);
Ido Schimmel5c8802f2017-02-06 16:20:13 +0100998 kfree(neigh_work);
Yotam Gigia6bf9e92016-07-05 11:27:44 +0200999}
1000
Jiri Pirkoe7322632016-09-01 10:37:43 +02001001int mlxsw_sp_router_netevent_event(struct notifier_block *unused,
1002 unsigned long event, void *ptr)
Yotam Gigic723c7352016-07-05 11:27:43 +02001003{
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001004 struct mlxsw_sp_neigh_event_work *neigh_work;
Yotam Gigic723c7352016-07-05 11:27:43 +02001005 struct mlxsw_sp_port *mlxsw_sp_port;
1006 struct mlxsw_sp *mlxsw_sp;
1007 unsigned long interval;
1008 struct neigh_parms *p;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001009 struct neighbour *n;
Yotam Gigic723c7352016-07-05 11:27:43 +02001010
1011 switch (event) {
1012 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
1013 p = ptr;
1014
1015 /* We don't care about changes in the default table. */
1016 if (!p->dev || p->tbl != &arp_tbl)
1017 return NOTIFY_DONE;
1018
1019 /* We are in atomic context and can't take RTNL mutex,
1020 * so use RCU variant to walk the device chain.
1021 */
1022 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(p->dev);
1023 if (!mlxsw_sp_port)
1024 return NOTIFY_DONE;
1025
1026 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1027 interval = jiffies_to_msecs(NEIGH_VAR(p, DELAY_PROBE_TIME));
1028 mlxsw_sp->router.neighs_update.interval = interval;
1029
1030 mlxsw_sp_port_dev_put(mlxsw_sp_port);
1031 break;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001032 case NETEVENT_NEIGH_UPDATE:
1033 n = ptr;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001034
1035 if (n->tbl != &arp_tbl)
1036 return NOTIFY_DONE;
1037
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001038 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(n->dev);
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001039 if (!mlxsw_sp_port)
1040 return NOTIFY_DONE;
1041
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001042 neigh_work = kzalloc(sizeof(*neigh_work), GFP_ATOMIC);
1043 if (!neigh_work) {
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001044 mlxsw_sp_port_dev_put(mlxsw_sp_port);
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001045 return NOTIFY_BAD;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001046 }
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001047
1048 INIT_WORK(&neigh_work->work, mlxsw_sp_router_neigh_event_work);
1049 neigh_work->mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1050 neigh_work->n = n;
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001051
1052 /* Take a reference to ensure the neighbour won't be
1053 * destructed until we drop the reference in delayed
1054 * work.
1055 */
1056 neigh_clone(n);
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001057 mlxsw_core_schedule_work(&neigh_work->work);
1058 mlxsw_sp_port_dev_put(mlxsw_sp_port);
Yotam Gigia6bf9e92016-07-05 11:27:44 +02001059 break;
Yotam Gigic723c7352016-07-05 11:27:43 +02001060 }
1061
1062 return NOTIFY_DONE;
1063}
1064
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001065static int mlxsw_sp_neigh_init(struct mlxsw_sp *mlxsw_sp)
1066{
Yotam Gigic723c7352016-07-05 11:27:43 +02001067 int err;
1068
1069 err = rhashtable_init(&mlxsw_sp->router.neigh_ht,
1070 &mlxsw_sp_neigh_ht_params);
1071 if (err)
1072 return err;
1073
1074 /* Initialize the polling interval according to the default
1075 * table.
1076 */
1077 mlxsw_sp_router_neighs_update_interval_init(mlxsw_sp);
1078
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001079 /* Create the delayed works for the activity_update */
Yotam Gigic723c7352016-07-05 11:27:43 +02001080 INIT_DELAYED_WORK(&mlxsw_sp->router.neighs_update.dw,
1081 mlxsw_sp_router_neighs_update_work);
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001082 INIT_DELAYED_WORK(&mlxsw_sp->router.nexthop_probe_dw,
1083 mlxsw_sp_router_probe_unresolved_nexthops);
Yotam Gigic723c7352016-07-05 11:27:43 +02001084 mlxsw_core_schedule_dw(&mlxsw_sp->router.neighs_update.dw, 0);
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001085 mlxsw_core_schedule_dw(&mlxsw_sp->router.nexthop_probe_dw, 0);
Yotam Gigic723c7352016-07-05 11:27:43 +02001086 return 0;
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001087}
1088
1089static void mlxsw_sp_neigh_fini(struct mlxsw_sp *mlxsw_sp)
1090{
Yotam Gigic723c7352016-07-05 11:27:43 +02001091 cancel_delayed_work_sync(&mlxsw_sp->router.neighs_update.dw);
Yotam Gigi0b2361d2016-07-05 11:27:52 +02001092 cancel_delayed_work_sync(&mlxsw_sp->router.nexthop_probe_dw);
Jiri Pirko6cf3c972016-07-05 11:27:39 +02001093 rhashtable_destroy(&mlxsw_sp->router.neigh_ht);
1094}
1095
Ido Schimmel9665b742017-02-08 11:16:42 +01001096static int mlxsw_sp_neigh_rif_flush(struct mlxsw_sp *mlxsw_sp,
1097 const struct mlxsw_sp_rif *r)
1098{
1099 char rauht_pl[MLXSW_REG_RAUHT_LEN];
1100
1101 mlxsw_reg_rauht_pack(rauht_pl, MLXSW_REG_RAUHT_OP_WRITE_DELETE_ALL,
1102 r->rif, r->addr);
1103 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
1104}
1105
1106static void mlxsw_sp_neigh_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
1107 struct mlxsw_sp_rif *r)
1108{
1109 struct mlxsw_sp_neigh_entry *neigh_entry, *tmp;
1110
1111 mlxsw_sp_neigh_rif_flush(mlxsw_sp, r);
1112 list_for_each_entry_safe(neigh_entry, tmp, &r->neigh_list,
1113 rif_list_node)
1114 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
1115}
1116
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001117struct mlxsw_sp_nexthop_key {
1118 struct fib_nh *fib_nh;
1119};
1120
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001121struct mlxsw_sp_nexthop {
1122 struct list_head neigh_list_node; /* member of neigh entry list */
Ido Schimmel9665b742017-02-08 11:16:42 +01001123 struct list_head rif_list_node;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001124 struct mlxsw_sp_nexthop_group *nh_grp; /* pointer back to the group
1125 * this belongs to
1126 */
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001127 struct rhash_head ht_node;
1128 struct mlxsw_sp_nexthop_key key;
Ido Schimmelb8399a12017-02-08 11:16:33 +01001129 struct mlxsw_sp_rif *r;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001130 u8 should_offload:1, /* set indicates this neigh is connected and
1131 * should be put to KVD linear area of this group.
1132 */
1133 offloaded:1, /* set in case the neigh is actually put into
1134 * KVD linear area of this group.
1135 */
1136 update:1; /* set indicates that MAC of this neigh should be
1137 * updated in HW
1138 */
1139 struct mlxsw_sp_neigh_entry *neigh_entry;
1140};
1141
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001142struct mlxsw_sp_nexthop_group_key {
1143 struct fib_info *fi;
1144};
1145
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001146struct mlxsw_sp_nexthop_group {
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001147 struct rhash_head ht_node;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001148 struct list_head fib_list; /* list of fib entries that use this group */
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001149 struct mlxsw_sp_nexthop_group_key key;
Ido Schimmelb3e8d1e2017-02-08 11:16:32 +01001150 u8 adj_index_valid:1,
1151 gateway:1; /* routes using the group use a gateway */
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001152 u32 adj_index;
1153 u16 ecmp_size;
1154 u16 count;
1155 struct mlxsw_sp_nexthop nexthops[0];
Ido Schimmelb8399a12017-02-08 11:16:33 +01001156#define nh_rif nexthops[0].r
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001157};
1158
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001159static const struct rhashtable_params mlxsw_sp_nexthop_group_ht_params = {
1160 .key_offset = offsetof(struct mlxsw_sp_nexthop_group, key),
1161 .head_offset = offsetof(struct mlxsw_sp_nexthop_group, ht_node),
1162 .key_len = sizeof(struct mlxsw_sp_nexthop_group_key),
1163};
1164
1165static int mlxsw_sp_nexthop_group_insert(struct mlxsw_sp *mlxsw_sp,
1166 struct mlxsw_sp_nexthop_group *nh_grp)
1167{
1168 return rhashtable_insert_fast(&mlxsw_sp->router.nexthop_group_ht,
1169 &nh_grp->ht_node,
1170 mlxsw_sp_nexthop_group_ht_params);
1171}
1172
1173static void mlxsw_sp_nexthop_group_remove(struct mlxsw_sp *mlxsw_sp,
1174 struct mlxsw_sp_nexthop_group *nh_grp)
1175{
1176 rhashtable_remove_fast(&mlxsw_sp->router.nexthop_group_ht,
1177 &nh_grp->ht_node,
1178 mlxsw_sp_nexthop_group_ht_params);
1179}
1180
1181static struct mlxsw_sp_nexthop_group *
1182mlxsw_sp_nexthop_group_lookup(struct mlxsw_sp *mlxsw_sp,
1183 struct mlxsw_sp_nexthop_group_key key)
1184{
1185 return rhashtable_lookup_fast(&mlxsw_sp->router.nexthop_group_ht, &key,
1186 mlxsw_sp_nexthop_group_ht_params);
1187}
1188
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001189static const struct rhashtable_params mlxsw_sp_nexthop_ht_params = {
1190 .key_offset = offsetof(struct mlxsw_sp_nexthop, key),
1191 .head_offset = offsetof(struct mlxsw_sp_nexthop, ht_node),
1192 .key_len = sizeof(struct mlxsw_sp_nexthop_key),
1193};
1194
1195static int mlxsw_sp_nexthop_insert(struct mlxsw_sp *mlxsw_sp,
1196 struct mlxsw_sp_nexthop *nh)
1197{
1198 return rhashtable_insert_fast(&mlxsw_sp->router.nexthop_ht,
1199 &nh->ht_node, mlxsw_sp_nexthop_ht_params);
1200}
1201
1202static void mlxsw_sp_nexthop_remove(struct mlxsw_sp *mlxsw_sp,
1203 struct mlxsw_sp_nexthop *nh)
1204{
1205 rhashtable_remove_fast(&mlxsw_sp->router.nexthop_ht, &nh->ht_node,
1206 mlxsw_sp_nexthop_ht_params);
1207}
1208
Ido Schimmelad178c82017-02-08 11:16:40 +01001209static struct mlxsw_sp_nexthop *
1210mlxsw_sp_nexthop_lookup(struct mlxsw_sp *mlxsw_sp,
1211 struct mlxsw_sp_nexthop_key key)
1212{
1213 return rhashtable_lookup_fast(&mlxsw_sp->router.nexthop_ht, &key,
1214 mlxsw_sp_nexthop_ht_params);
1215}
1216
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001217static int mlxsw_sp_adj_index_mass_update_vr(struct mlxsw_sp *mlxsw_sp,
1218 struct mlxsw_sp_vr *vr,
1219 u32 adj_index, u16 ecmp_size,
1220 u32 new_adj_index,
1221 u16 new_ecmp_size)
1222{
1223 char raleu_pl[MLXSW_REG_RALEU_LEN];
1224
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001225 mlxsw_reg_raleu_pack(raleu_pl,
1226 (enum mlxsw_reg_ralxx_protocol) vr->proto, vr->id,
1227 adj_index, ecmp_size, new_adj_index,
1228 new_ecmp_size);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001229 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raleu), raleu_pl);
1230}
1231
1232static int mlxsw_sp_adj_index_mass_update(struct mlxsw_sp *mlxsw_sp,
1233 struct mlxsw_sp_nexthop_group *nh_grp,
1234 u32 old_adj_index, u16 old_ecmp_size)
1235{
1236 struct mlxsw_sp_fib_entry *fib_entry;
1237 struct mlxsw_sp_vr *vr = NULL;
1238 int err;
1239
1240 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
1241 if (vr == fib_entry->vr)
1242 continue;
1243 vr = fib_entry->vr;
1244 err = mlxsw_sp_adj_index_mass_update_vr(mlxsw_sp, vr,
1245 old_adj_index,
1246 old_ecmp_size,
1247 nh_grp->adj_index,
1248 nh_grp->ecmp_size);
1249 if (err)
1250 return err;
1251 }
1252 return 0;
1253}
1254
1255static int mlxsw_sp_nexthop_mac_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
1256 struct mlxsw_sp_nexthop *nh)
1257{
1258 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
1259 char ratr_pl[MLXSW_REG_RATR_LEN];
1260
1261 mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY,
1262 true, adj_index, neigh_entry->rif);
1263 mlxsw_reg_ratr_eth_entry_pack(ratr_pl, neigh_entry->ha);
1264 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl);
1265}
1266
1267static int
1268mlxsw_sp_nexthop_group_mac_update(struct mlxsw_sp *mlxsw_sp,
Ido Schimmela59b7e02017-01-23 11:11:42 +01001269 struct mlxsw_sp_nexthop_group *nh_grp,
1270 bool reallocate)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001271{
1272 u32 adj_index = nh_grp->adj_index; /* base */
1273 struct mlxsw_sp_nexthop *nh;
1274 int i;
1275 int err;
1276
1277 for (i = 0; i < nh_grp->count; i++) {
1278 nh = &nh_grp->nexthops[i];
1279
1280 if (!nh->should_offload) {
1281 nh->offloaded = 0;
1282 continue;
1283 }
1284
Ido Schimmela59b7e02017-01-23 11:11:42 +01001285 if (nh->update || reallocate) {
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001286 err = mlxsw_sp_nexthop_mac_update(mlxsw_sp,
1287 adj_index, nh);
1288 if (err)
1289 return err;
1290 nh->update = 0;
1291 nh->offloaded = 1;
1292 }
1293 adj_index++;
1294 }
1295 return 0;
1296}
1297
1298static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1299 struct mlxsw_sp_fib_entry *fib_entry);
1300
1301static int
1302mlxsw_sp_nexthop_fib_entries_update(struct mlxsw_sp *mlxsw_sp,
1303 struct mlxsw_sp_nexthop_group *nh_grp)
1304{
1305 struct mlxsw_sp_fib_entry *fib_entry;
1306 int err;
1307
1308 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
1309 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
1310 if (err)
1311 return err;
1312 }
1313 return 0;
1314}
1315
1316static void
1317mlxsw_sp_nexthop_group_refresh(struct mlxsw_sp *mlxsw_sp,
1318 struct mlxsw_sp_nexthop_group *nh_grp)
1319{
1320 struct mlxsw_sp_nexthop *nh;
1321 bool offload_change = false;
1322 u32 adj_index;
1323 u16 ecmp_size = 0;
1324 bool old_adj_index_valid;
1325 u32 old_adj_index;
1326 u16 old_ecmp_size;
1327 int ret;
1328 int i;
1329 int err;
1330
Ido Schimmelb3e8d1e2017-02-08 11:16:32 +01001331 if (!nh_grp->gateway) {
1332 mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1333 return;
1334 }
1335
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001336 for (i = 0; i < nh_grp->count; i++) {
1337 nh = &nh_grp->nexthops[i];
1338
1339 if (nh->should_offload ^ nh->offloaded) {
1340 offload_change = true;
1341 if (nh->should_offload)
1342 nh->update = 1;
1343 }
1344 if (nh->should_offload)
1345 ecmp_size++;
1346 }
1347 if (!offload_change) {
1348 /* Nothing was added or removed, so no need to reallocate. Just
1349 * update MAC on existing adjacency indexes.
1350 */
Ido Schimmela59b7e02017-01-23 11:11:42 +01001351 err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp,
1352 false);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001353 if (err) {
1354 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
1355 goto set_trap;
1356 }
1357 return;
1358 }
1359 if (!ecmp_size)
1360 /* No neigh of this group is connected so we just set
1361 * the trap and let everthing flow through kernel.
1362 */
1363 goto set_trap;
1364
1365 ret = mlxsw_sp_kvdl_alloc(mlxsw_sp, ecmp_size);
1366 if (ret < 0) {
1367 /* We ran out of KVD linear space, just set the
1368 * trap and let everything flow through kernel.
1369 */
1370 dev_warn(mlxsw_sp->bus_info->dev, "Failed to allocate KVD linear area for nexthop group.\n");
1371 goto set_trap;
1372 }
1373 adj_index = ret;
1374 old_adj_index_valid = nh_grp->adj_index_valid;
1375 old_adj_index = nh_grp->adj_index;
1376 old_ecmp_size = nh_grp->ecmp_size;
1377 nh_grp->adj_index_valid = 1;
1378 nh_grp->adj_index = adj_index;
1379 nh_grp->ecmp_size = ecmp_size;
Ido Schimmela59b7e02017-01-23 11:11:42 +01001380 err = mlxsw_sp_nexthop_group_mac_update(mlxsw_sp, nh_grp, true);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001381 if (err) {
1382 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
1383 goto set_trap;
1384 }
1385
1386 if (!old_adj_index_valid) {
1387 /* The trap was set for fib entries, so we have to call
1388 * fib entry update to unset it and use adjacency index.
1389 */
1390 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1391 if (err) {
1392 dev_warn(mlxsw_sp->bus_info->dev, "Failed to add adjacency index to fib entries.\n");
1393 goto set_trap;
1394 }
1395 return;
1396 }
1397
1398 err = mlxsw_sp_adj_index_mass_update(mlxsw_sp, nh_grp,
1399 old_adj_index, old_ecmp_size);
1400 mlxsw_sp_kvdl_free(mlxsw_sp, old_adj_index);
1401 if (err) {
1402 dev_warn(mlxsw_sp->bus_info->dev, "Failed to mass-update adjacency index for nexthop group.\n");
1403 goto set_trap;
1404 }
1405 return;
1406
1407set_trap:
1408 old_adj_index_valid = nh_grp->adj_index_valid;
1409 nh_grp->adj_index_valid = 0;
1410 for (i = 0; i < nh_grp->count; i++) {
1411 nh = &nh_grp->nexthops[i];
1412 nh->offloaded = 0;
1413 }
1414 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
1415 if (err)
1416 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set traps for fib entries.\n");
1417 if (old_adj_index_valid)
1418 mlxsw_sp_kvdl_free(mlxsw_sp, nh_grp->adj_index);
1419}
1420
1421static void __mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp_nexthop *nh,
1422 bool removing)
1423{
1424 if (!removing && !nh->should_offload)
1425 nh->should_offload = 1;
1426 else if (removing && nh->offloaded)
1427 nh->should_offload = 0;
1428 nh->update = 1;
1429}
1430
1431static void
1432mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
1433 struct mlxsw_sp_neigh_entry *neigh_entry,
1434 bool removing)
1435{
1436 struct mlxsw_sp_nexthop *nh;
1437
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001438 list_for_each_entry(nh, &neigh_entry->nexthop_list,
1439 neigh_list_node) {
1440 __mlxsw_sp_nexthop_neigh_update(nh, removing);
1441 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
1442 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001443}
1444
Ido Schimmel9665b742017-02-08 11:16:42 +01001445static void mlxsw_sp_nexthop_rif_init(struct mlxsw_sp_nexthop *nh,
1446 struct mlxsw_sp_rif *r)
1447{
1448 if (nh->r)
1449 return;
1450
1451 nh->r = r;
1452 list_add(&nh->rif_list_node, &r->nexthop_list);
1453}
1454
1455static void mlxsw_sp_nexthop_rif_fini(struct mlxsw_sp_nexthop *nh)
1456{
1457 if (!nh->r)
1458 return;
1459
1460 list_del(&nh->rif_list_node);
1461 nh->r = NULL;
1462}
1463
Ido Schimmela8c97012017-02-08 11:16:35 +01001464static int mlxsw_sp_nexthop_neigh_init(struct mlxsw_sp *mlxsw_sp,
1465 struct mlxsw_sp_nexthop *nh)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001466{
1467 struct mlxsw_sp_neigh_entry *neigh_entry;
Ido Schimmela8c97012017-02-08 11:16:35 +01001468 struct fib_nh *fib_nh = nh->key.fib_nh;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001469 struct neighbour *n;
Ido Schimmel93a87e52016-12-23 09:32:49 +01001470 u8 nud_state, dead;
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001471 int err;
1472
Ido Schimmelad178c82017-02-08 11:16:40 +01001473 if (!nh->nh_grp->gateway || nh->neigh_entry)
Ido Schimmelb8399a12017-02-08 11:16:33 +01001474 return 0;
1475
Jiri Pirko33b13412016-11-10 12:31:04 +01001476 /* Take a reference of neigh here ensuring that neigh would
1477 * not be detructed before the nexthop entry is finished.
1478 * The reference is taken either in neigh_lookup() or
Ido Schimmelfd76d912017-02-06 16:20:17 +01001479 * in neigh_create() in case n is not found.
Jiri Pirko33b13412016-11-10 12:31:04 +01001480 */
Ido Schimmela8c97012017-02-08 11:16:35 +01001481 n = neigh_lookup(&arp_tbl, &fib_nh->nh_gw, fib_nh->nh_dev);
Jiri Pirko33b13412016-11-10 12:31:04 +01001482 if (!n) {
Ido Schimmela8c97012017-02-08 11:16:35 +01001483 n = neigh_create(&arp_tbl, &fib_nh->nh_gw, fib_nh->nh_dev);
1484 if (IS_ERR(n))
1485 return PTR_ERR(n);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001486 neigh_event_send(n, NULL);
Jiri Pirko33b13412016-11-10 12:31:04 +01001487 }
1488 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
1489 if (!neigh_entry) {
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001490 neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
1491 if (IS_ERR(neigh_entry)) {
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001492 err = -EINVAL;
1493 goto err_neigh_entry_create;
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001494 }
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001495 }
Yotam Gigib2157142016-07-05 11:27:51 +02001496
1497 /* If that is the first nexthop connected to that neigh, add to
1498 * nexthop_neighs_list
1499 */
1500 if (list_empty(&neigh_entry->nexthop_list))
1501 list_add_tail(&neigh_entry->nexthop_neighs_list_node,
1502 &mlxsw_sp->router.nexthop_neighs_list);
1503
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001504 nh->neigh_entry = neigh_entry;
1505 list_add_tail(&nh->neigh_list_node, &neigh_entry->nexthop_list);
1506 read_lock_bh(&n->lock);
1507 nud_state = n->nud_state;
Ido Schimmel93a87e52016-12-23 09:32:49 +01001508 dead = n->dead;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001509 read_unlock_bh(&n->lock);
Ido Schimmel93a87e52016-12-23 09:32:49 +01001510 __mlxsw_sp_nexthop_neigh_update(nh, !(nud_state & NUD_VALID && !dead));
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001511
1512 return 0;
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001513
1514err_neigh_entry_create:
1515 neigh_release(n);
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001516 return err;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001517}
1518
Ido Schimmela8c97012017-02-08 11:16:35 +01001519static void mlxsw_sp_nexthop_neigh_fini(struct mlxsw_sp *mlxsw_sp,
1520 struct mlxsw_sp_nexthop *nh)
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001521{
1522 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
Ido Schimmela8c97012017-02-08 11:16:35 +01001523 struct neighbour *n;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001524
Ido Schimmelb8399a12017-02-08 11:16:33 +01001525 if (!neigh_entry)
Ido Schimmela8c97012017-02-08 11:16:35 +01001526 return;
1527 n = neigh_entry->key.n;
Ido Schimmelb8399a12017-02-08 11:16:33 +01001528
Ido Schimmel58312122016-12-23 09:32:50 +01001529 __mlxsw_sp_nexthop_neigh_update(nh, true);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001530 list_del(&nh->neigh_list_node);
Ido Schimmele58be792017-02-08 11:16:28 +01001531 nh->neigh_entry = NULL;
Yotam Gigib2157142016-07-05 11:27:51 +02001532
1533 /* If that is the last nexthop connected to that neigh, remove from
1534 * nexthop_neighs_list
1535 */
Ido Schimmele58be792017-02-08 11:16:28 +01001536 if (list_empty(&neigh_entry->nexthop_list))
1537 list_del(&neigh_entry->nexthop_neighs_list_node);
Yotam Gigib2157142016-07-05 11:27:51 +02001538
Ido Schimmel5c8802f2017-02-06 16:20:13 +01001539 if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
1540 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
1541
1542 neigh_release(n);
Ido Schimmela8c97012017-02-08 11:16:35 +01001543}
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001544
Ido Schimmela8c97012017-02-08 11:16:35 +01001545static int mlxsw_sp_nexthop_init(struct mlxsw_sp *mlxsw_sp,
1546 struct mlxsw_sp_nexthop_group *nh_grp,
1547 struct mlxsw_sp_nexthop *nh,
1548 struct fib_nh *fib_nh)
1549{
1550 struct net_device *dev = fib_nh->nh_dev;
1551 struct mlxsw_sp_rif *r;
1552 int err;
1553
1554 nh->nh_grp = nh_grp;
1555 nh->key.fib_nh = fib_nh;
1556 err = mlxsw_sp_nexthop_insert(mlxsw_sp, nh);
1557 if (err)
1558 return err;
1559
1560 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
1561 if (!r)
1562 return 0;
Ido Schimmel9665b742017-02-08 11:16:42 +01001563 mlxsw_sp_nexthop_rif_init(nh, r);
Ido Schimmela8c97012017-02-08 11:16:35 +01001564
1565 err = mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh);
1566 if (err)
1567 goto err_nexthop_neigh_init;
1568
1569 return 0;
1570
1571err_nexthop_neigh_init:
1572 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
1573 return err;
1574}
1575
1576static void mlxsw_sp_nexthop_fini(struct mlxsw_sp *mlxsw_sp,
1577 struct mlxsw_sp_nexthop *nh)
1578{
1579 mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh);
Ido Schimmel9665b742017-02-08 11:16:42 +01001580 mlxsw_sp_nexthop_rif_fini(nh);
Ido Schimmelc53b8e12017-02-08 11:16:30 +01001581 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001582}
1583
Ido Schimmelad178c82017-02-08 11:16:40 +01001584static void mlxsw_sp_nexthop_event(struct mlxsw_sp *mlxsw_sp,
1585 unsigned long event, struct fib_nh *fib_nh)
1586{
1587 struct mlxsw_sp_nexthop_key key;
1588 struct mlxsw_sp_nexthop *nh;
1589 struct mlxsw_sp_rif *r;
1590
1591 if (mlxsw_sp->router.aborted)
1592 return;
1593
1594 key.fib_nh = fib_nh;
1595 nh = mlxsw_sp_nexthop_lookup(mlxsw_sp, key);
1596 if (WARN_ON_ONCE(!nh))
1597 return;
1598
1599 r = mlxsw_sp_rif_find_by_dev(mlxsw_sp, fib_nh->nh_dev);
1600 if (!r)
1601 return;
1602
1603 switch (event) {
1604 case FIB_EVENT_NH_ADD:
Ido Schimmel9665b742017-02-08 11:16:42 +01001605 mlxsw_sp_nexthop_rif_init(nh, r);
Ido Schimmelad178c82017-02-08 11:16:40 +01001606 mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh);
1607 break;
1608 case FIB_EVENT_NH_DEL:
1609 mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh);
Ido Schimmel9665b742017-02-08 11:16:42 +01001610 mlxsw_sp_nexthop_rif_fini(nh);
Ido Schimmelad178c82017-02-08 11:16:40 +01001611 break;
1612 }
1613
1614 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
1615}
1616
Ido Schimmel9665b742017-02-08 11:16:42 +01001617static void mlxsw_sp_nexthop_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
1618 struct mlxsw_sp_rif *r)
1619{
1620 struct mlxsw_sp_nexthop *nh, *tmp;
1621
1622 list_for_each_entry_safe(nh, tmp, &r->nexthop_list, rif_list_node) {
1623 mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh);
1624 mlxsw_sp_nexthop_rif_fini(nh);
1625 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
1626 }
1627}
1628
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001629static struct mlxsw_sp_nexthop_group *
1630mlxsw_sp_nexthop_group_create(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi)
1631{
1632 struct mlxsw_sp_nexthop_group *nh_grp;
1633 struct mlxsw_sp_nexthop *nh;
1634 struct fib_nh *fib_nh;
1635 size_t alloc_size;
1636 int i;
1637 int err;
1638
1639 alloc_size = sizeof(*nh_grp) +
1640 fi->fib_nhs * sizeof(struct mlxsw_sp_nexthop);
1641 nh_grp = kzalloc(alloc_size, GFP_KERNEL);
1642 if (!nh_grp)
1643 return ERR_PTR(-ENOMEM);
1644 INIT_LIST_HEAD(&nh_grp->fib_list);
Ido Schimmelb3e8d1e2017-02-08 11:16:32 +01001645 nh_grp->gateway = fi->fib_nh->nh_scope == RT_SCOPE_LINK;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001646 nh_grp->count = fi->fib_nhs;
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001647 nh_grp->key.fi = fi;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001648 for (i = 0; i < nh_grp->count; i++) {
1649 nh = &nh_grp->nexthops[i];
1650 fib_nh = &fi->fib_nh[i];
1651 err = mlxsw_sp_nexthop_init(mlxsw_sp, nh_grp, nh, fib_nh);
1652 if (err)
1653 goto err_nexthop_init;
1654 }
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001655 err = mlxsw_sp_nexthop_group_insert(mlxsw_sp, nh_grp);
1656 if (err)
1657 goto err_nexthop_group_insert;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001658 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
1659 return nh_grp;
1660
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001661err_nexthop_group_insert:
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001662err_nexthop_init:
1663 for (i--; i >= 0; i--)
1664 mlxsw_sp_nexthop_fini(mlxsw_sp, nh);
1665 kfree(nh_grp);
1666 return ERR_PTR(err);
1667}
1668
1669static void
1670mlxsw_sp_nexthop_group_destroy(struct mlxsw_sp *mlxsw_sp,
1671 struct mlxsw_sp_nexthop_group *nh_grp)
1672{
1673 struct mlxsw_sp_nexthop *nh;
1674 int i;
1675
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001676 mlxsw_sp_nexthop_group_remove(mlxsw_sp, nh_grp);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001677 for (i = 0; i < nh_grp->count; i++) {
1678 nh = &nh_grp->nexthops[i];
1679 mlxsw_sp_nexthop_fini(mlxsw_sp, nh);
1680 }
Ido Schimmel58312122016-12-23 09:32:50 +01001681 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
1682 WARN_ON_ONCE(nh_grp->adj_index_valid);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001683 kfree(nh_grp);
1684}
1685
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001686static int mlxsw_sp_nexthop_group_get(struct mlxsw_sp *mlxsw_sp,
1687 struct mlxsw_sp_fib_entry *fib_entry,
1688 struct fib_info *fi)
1689{
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001690 struct mlxsw_sp_nexthop_group_key key;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001691 struct mlxsw_sp_nexthop_group *nh_grp;
1692
Ido Schimmele9ad5e72017-02-08 11:16:29 +01001693 key.fi = fi;
1694 nh_grp = mlxsw_sp_nexthop_group_lookup(mlxsw_sp, key);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001695 if (!nh_grp) {
1696 nh_grp = mlxsw_sp_nexthop_group_create(mlxsw_sp, fi);
1697 if (IS_ERR(nh_grp))
1698 return PTR_ERR(nh_grp);
1699 }
1700 list_add_tail(&fib_entry->nexthop_group_node, &nh_grp->fib_list);
1701 fib_entry->nh_group = nh_grp;
1702 return 0;
1703}
1704
1705static void mlxsw_sp_nexthop_group_put(struct mlxsw_sp *mlxsw_sp,
1706 struct mlxsw_sp_fib_entry *fib_entry)
1707{
1708 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
1709
1710 list_del(&fib_entry->nexthop_group_node);
1711 if (!list_empty(&nh_grp->fib_list))
1712 return;
1713 mlxsw_sp_nexthop_group_destroy(mlxsw_sp, nh_grp);
1714}
1715
Ido Schimmel013b20f2017-02-08 11:16:36 +01001716static bool
1717mlxsw_sp_fib_entry_should_offload(const struct mlxsw_sp_fib_entry *fib_entry)
1718{
1719 struct mlxsw_sp_nexthop_group *nh_group = fib_entry->nh_group;
1720
1721 switch (fib_entry->type) {
1722 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
1723 return !!nh_group->adj_index_valid;
1724 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
Ido Schimmel70ad3502017-02-08 11:16:38 +01001725 return !!nh_group->nh_rif;
Ido Schimmel013b20f2017-02-08 11:16:36 +01001726 default:
1727 return false;
1728 }
1729}
1730
1731static void mlxsw_sp_fib_entry_offload_set(struct mlxsw_sp_fib_entry *fib_entry)
1732{
1733 fib_entry->offloaded = true;
1734
1735 switch (fib_entry->vr->proto) {
1736 case MLXSW_SP_L3_PROTO_IPV4:
1737 fib_info_offload_inc(fib_entry->nh_group->key.fi);
1738 break;
1739 case MLXSW_SP_L3_PROTO_IPV6:
1740 WARN_ON_ONCE(1);
1741 }
1742}
1743
1744static void
1745mlxsw_sp_fib_entry_offload_unset(struct mlxsw_sp_fib_entry *fib_entry)
1746{
1747 switch (fib_entry->vr->proto) {
1748 case MLXSW_SP_L3_PROTO_IPV4:
1749 fib_info_offload_dec(fib_entry->nh_group->key.fi);
1750 break;
1751 case MLXSW_SP_L3_PROTO_IPV6:
1752 WARN_ON_ONCE(1);
1753 }
1754
1755 fib_entry->offloaded = false;
1756}
1757
1758static void
1759mlxsw_sp_fib_entry_offload_refresh(struct mlxsw_sp_fib_entry *fib_entry,
1760 enum mlxsw_reg_ralue_op op, int err)
1761{
1762 switch (op) {
1763 case MLXSW_REG_RALUE_OP_WRITE_DELETE:
1764 if (!fib_entry->offloaded)
1765 return;
1766 return mlxsw_sp_fib_entry_offload_unset(fib_entry);
1767 case MLXSW_REG_RALUE_OP_WRITE_WRITE:
1768 if (err)
1769 return;
1770 if (mlxsw_sp_fib_entry_should_offload(fib_entry) &&
1771 !fib_entry->offloaded)
1772 mlxsw_sp_fib_entry_offload_set(fib_entry);
1773 else if (!mlxsw_sp_fib_entry_should_offload(fib_entry) &&
1774 fib_entry->offloaded)
1775 mlxsw_sp_fib_entry_offload_unset(fib_entry);
1776 return;
1777 default:
1778 return;
1779 }
1780}
1781
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001782static int mlxsw_sp_fib_entry_op4_remote(struct mlxsw_sp *mlxsw_sp,
1783 struct mlxsw_sp_fib_entry *fib_entry,
1784 enum mlxsw_reg_ralue_op op)
1785{
1786 char ralue_pl[MLXSW_REG_RALUE_LEN];
1787 u32 *p_dip = (u32 *) fib_entry->key.addr;
1788 struct mlxsw_sp_vr *vr = fib_entry->vr;
1789 enum mlxsw_reg_ralue_trap_action trap_action;
1790 u16 trap_id = 0;
1791 u32 adjacency_index = 0;
1792 u16 ecmp_size = 0;
1793
1794 /* In case the nexthop group adjacency index is valid, use it
1795 * with provided ECMP size. Otherwise, setup trap and pass
1796 * traffic to kernel.
1797 */
Ido Schimmel4b411472017-02-08 11:16:37 +01001798 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001799 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
1800 adjacency_index = fib_entry->nh_group->adj_index;
1801 ecmp_size = fib_entry->nh_group->ecmp_size;
1802 } else {
1803 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
1804 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
1805 }
1806
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001807 mlxsw_reg_ralue_pack4(ralue_pl,
1808 (enum mlxsw_reg_ralxx_protocol) vr->proto, op,
1809 vr->id, fib_entry->key.prefix_len, *p_dip);
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001810 mlxsw_reg_ralue_act_remote_pack(ralue_pl, trap_action, trap_id,
1811 adjacency_index, ecmp_size);
1812 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1813}
1814
Jiri Pirko61c503f2016-07-04 08:23:11 +02001815static int mlxsw_sp_fib_entry_op4_local(struct mlxsw_sp *mlxsw_sp,
1816 struct mlxsw_sp_fib_entry *fib_entry,
1817 enum mlxsw_reg_ralue_op op)
1818{
Ido Schimmelb8399a12017-02-08 11:16:33 +01001819 struct mlxsw_sp_rif *r = fib_entry->nh_group->nh_rif;
Ido Schimmel70ad3502017-02-08 11:16:38 +01001820 enum mlxsw_reg_ralue_trap_action trap_action;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001821 char ralue_pl[MLXSW_REG_RALUE_LEN];
1822 u32 *p_dip = (u32 *) fib_entry->key.addr;
1823 struct mlxsw_sp_vr *vr = fib_entry->vr;
Ido Schimmel70ad3502017-02-08 11:16:38 +01001824 u16 trap_id = 0;
1825 u16 rif = 0;
1826
1827 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
1828 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
1829 rif = r->rif;
1830 } else {
1831 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
1832 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
1833 }
Jiri Pirko61c503f2016-07-04 08:23:11 +02001834
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001835 mlxsw_reg_ralue_pack4(ralue_pl,
1836 (enum mlxsw_reg_ralxx_protocol) vr->proto, op,
1837 vr->id, fib_entry->key.prefix_len, *p_dip);
Ido Schimmel70ad3502017-02-08 11:16:38 +01001838 mlxsw_reg_ralue_act_local_pack(ralue_pl, trap_action, trap_id, rif);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001839 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1840}
1841
1842static int mlxsw_sp_fib_entry_op4_trap(struct mlxsw_sp *mlxsw_sp,
1843 struct mlxsw_sp_fib_entry *fib_entry,
1844 enum mlxsw_reg_ralue_op op)
1845{
1846 char ralue_pl[MLXSW_REG_RALUE_LEN];
1847 u32 *p_dip = (u32 *) fib_entry->key.addr;
1848 struct mlxsw_sp_vr *vr = fib_entry->vr;
1849
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001850 mlxsw_reg_ralue_pack4(ralue_pl,
1851 (enum mlxsw_reg_ralxx_protocol) vr->proto, op,
1852 vr->id, fib_entry->key.prefix_len, *p_dip);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001853 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
1854 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
1855}
1856
1857static int mlxsw_sp_fib_entry_op4(struct mlxsw_sp *mlxsw_sp,
1858 struct mlxsw_sp_fib_entry *fib_entry,
1859 enum mlxsw_reg_ralue_op op)
1860{
1861 switch (fib_entry->type) {
1862 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001863 return mlxsw_sp_fib_entry_op4_remote(mlxsw_sp, fib_entry, op);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001864 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
1865 return mlxsw_sp_fib_entry_op4_local(mlxsw_sp, fib_entry, op);
1866 case MLXSW_SP_FIB_ENTRY_TYPE_TRAP:
1867 return mlxsw_sp_fib_entry_op4_trap(mlxsw_sp, fib_entry, op);
1868 }
1869 return -EINVAL;
1870}
1871
1872static int mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
1873 struct mlxsw_sp_fib_entry *fib_entry,
1874 enum mlxsw_reg_ralue_op op)
1875{
Ido Schimmel013b20f2017-02-08 11:16:36 +01001876 int err = -EINVAL;
1877
Jiri Pirko61c503f2016-07-04 08:23:11 +02001878 switch (fib_entry->vr->proto) {
1879 case MLXSW_SP_L3_PROTO_IPV4:
Ido Schimmel013b20f2017-02-08 11:16:36 +01001880 err = mlxsw_sp_fib_entry_op4(mlxsw_sp, fib_entry, op);
1881 break;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001882 case MLXSW_SP_L3_PROTO_IPV6:
Ido Schimmel013b20f2017-02-08 11:16:36 +01001883 return err;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001884 }
Ido Schimmel013b20f2017-02-08 11:16:36 +01001885 mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, err);
1886 return err;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001887}
1888
1889static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1890 struct mlxsw_sp_fib_entry *fib_entry)
1891{
Jiri Pirko7146da32016-09-01 10:37:41 +02001892 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
1893 MLXSW_REG_RALUE_OP_WRITE_WRITE);
Jiri Pirko61c503f2016-07-04 08:23:11 +02001894}
1895
1896static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp,
1897 struct mlxsw_sp_fib_entry *fib_entry)
1898{
1899 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
1900 MLXSW_REG_RALUE_OP_WRITE_DELETE);
1901}
1902
Jiri Pirko61c503f2016-07-04 08:23:11 +02001903static int
Ido Schimmel013b20f2017-02-08 11:16:36 +01001904mlxsw_sp_fib4_entry_type_set(struct mlxsw_sp *mlxsw_sp,
1905 const struct fib_entry_notifier_info *fen_info,
1906 struct mlxsw_sp_fib_entry *fib_entry)
Jiri Pirko61c503f2016-07-04 08:23:11 +02001907{
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001908 struct fib_info *fi = fen_info->fi;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001909
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001910 if (fen_info->type == RTN_LOCAL || fen_info->type == RTN_BROADCAST) {
Jiri Pirko61c503f2016-07-04 08:23:11 +02001911 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
1912 return 0;
1913 }
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001914 if (fen_info->type != RTN_UNICAST)
Jiri Pirko61c503f2016-07-04 08:23:11 +02001915 return -EINVAL;
Ido Schimmelb8399a12017-02-08 11:16:33 +01001916 if (fi->fib_nh->nh_scope != RT_SCOPE_LINK)
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001917 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
Ido Schimmelb8399a12017-02-08 11:16:33 +01001918 else
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001919 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001920 return 0;
Jiri Pirkoa7ff87a2016-07-05 11:27:50 +02001921}
1922
Jiri Pirko5b004412016-09-01 10:37:40 +02001923static struct mlxsw_sp_fib_entry *
1924mlxsw_sp_fib_entry_get(struct mlxsw_sp *mlxsw_sp,
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001925 const struct fib_entry_notifier_info *fen_info)
Jiri Pirko5b004412016-09-01 10:37:40 +02001926{
1927 struct mlxsw_sp_fib_entry *fib_entry;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001928 struct fib_info *fi = fen_info->fi;
Jiri Pirko5b004412016-09-01 10:37:40 +02001929 struct mlxsw_sp_vr *vr;
1930 int err;
1931
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001932 vr = mlxsw_sp_vr_get(mlxsw_sp, fen_info->dst_len, fen_info->tb_id,
Jiri Pirko5b004412016-09-01 10:37:40 +02001933 MLXSW_SP_L3_PROTO_IPV4);
1934 if (IS_ERR(vr))
1935 return ERR_CAST(vr);
1936
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001937 fib_entry = mlxsw_sp_fib_entry_lookup(vr->fib, &fen_info->dst,
1938 sizeof(fen_info->dst),
1939 fen_info->dst_len, fi->fib_dev);
Jiri Pirko5b004412016-09-01 10:37:40 +02001940 if (fib_entry) {
1941 /* Already exists, just take a reference */
1942 fib_entry->ref_count++;
1943 return fib_entry;
1944 }
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001945 fib_entry = mlxsw_sp_fib_entry_create(vr->fib, &fen_info->dst,
1946 sizeof(fen_info->dst),
1947 fen_info->dst_len, fi->fib_dev);
Jiri Pirko5b004412016-09-01 10:37:40 +02001948 if (!fib_entry) {
1949 err = -ENOMEM;
1950 goto err_fib_entry_create;
1951 }
1952 fib_entry->vr = vr;
1953 fib_entry->ref_count = 1;
1954
Ido Schimmel013b20f2017-02-08 11:16:36 +01001955 err = mlxsw_sp_fib4_entry_type_set(mlxsw_sp, fen_info, fib_entry);
Jiri Pirko5b004412016-09-01 10:37:40 +02001956 if (err)
Ido Schimmel013b20f2017-02-08 11:16:36 +01001957 goto err_fib4_entry_type_set;
Jiri Pirko5b004412016-09-01 10:37:40 +02001958
Ido Schimmelb8399a12017-02-08 11:16:33 +01001959 err = mlxsw_sp_nexthop_group_get(mlxsw_sp, fib_entry, fi);
1960 if (err)
1961 goto err_nexthop_group_get;
1962
Jiri Pirko5b004412016-09-01 10:37:40 +02001963 return fib_entry;
1964
Ido Schimmelb8399a12017-02-08 11:16:33 +01001965err_nexthop_group_get:
Ido Schimmel013b20f2017-02-08 11:16:36 +01001966err_fib4_entry_type_set:
Jiri Pirko5b004412016-09-01 10:37:40 +02001967 mlxsw_sp_fib_entry_destroy(fib_entry);
1968err_fib_entry_create:
1969 mlxsw_sp_vr_put(mlxsw_sp, vr);
1970
1971 return ERR_PTR(err);
1972}
1973
1974static struct mlxsw_sp_fib_entry *
1975mlxsw_sp_fib_entry_find(struct mlxsw_sp *mlxsw_sp,
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001976 const struct fib_entry_notifier_info *fen_info)
Jiri Pirko5b004412016-09-01 10:37:40 +02001977{
1978 struct mlxsw_sp_vr *vr;
1979
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001980 vr = mlxsw_sp_vr_find(mlxsw_sp, fen_info->tb_id,
1981 MLXSW_SP_L3_PROTO_IPV4);
Jiri Pirko5b004412016-09-01 10:37:40 +02001982 if (!vr)
1983 return NULL;
1984
Jiri Pirkob45f64d2016-09-26 12:52:31 +02001985 return mlxsw_sp_fib_entry_lookup(vr->fib, &fen_info->dst,
1986 sizeof(fen_info->dst),
1987 fen_info->dst_len,
1988 fen_info->fi->fib_dev);
Jiri Pirko5b004412016-09-01 10:37:40 +02001989}
1990
Ido Schimmel1a9234e662016-09-19 08:29:26 +02001991static void mlxsw_sp_fib_entry_put(struct mlxsw_sp *mlxsw_sp,
1992 struct mlxsw_sp_fib_entry *fib_entry)
Jiri Pirko5b004412016-09-01 10:37:40 +02001993{
1994 struct mlxsw_sp_vr *vr = fib_entry->vr;
1995
1996 if (--fib_entry->ref_count == 0) {
Ido Schimmelb8399a12017-02-08 11:16:33 +01001997 mlxsw_sp_nexthop_group_put(mlxsw_sp, fib_entry);
Jiri Pirko5b004412016-09-01 10:37:40 +02001998 mlxsw_sp_fib_entry_destroy(fib_entry);
1999 }
2000 mlxsw_sp_vr_put(mlxsw_sp, vr);
2001}
2002
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002003static void mlxsw_sp_fib_entry_put_all(struct mlxsw_sp *mlxsw_sp,
2004 struct mlxsw_sp_fib_entry *fib_entry)
Jiri Pirko61c503f2016-07-04 08:23:11 +02002005{
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002006 unsigned int last_ref_count;
Jiri Pirko61c503f2016-07-04 08:23:11 +02002007
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002008 do {
2009 last_ref_count = fib_entry->ref_count;
2010 mlxsw_sp_fib_entry_put(mlxsw_sp, fib_entry);
2011 } while (last_ref_count != 1);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002012}
2013
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002014static int mlxsw_sp_router_fib4_add(struct mlxsw_sp *mlxsw_sp,
2015 struct fib_entry_notifier_info *fen_info)
Jiri Pirko61c503f2016-07-04 08:23:11 +02002016{
Jiri Pirko61c503f2016-07-04 08:23:11 +02002017 struct mlxsw_sp_fib_entry *fib_entry;
2018 struct mlxsw_sp_vr *vr;
2019 int err;
2020
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002021 if (mlxsw_sp->router.aborted)
2022 return 0;
2023
2024 fib_entry = mlxsw_sp_fib_entry_get(mlxsw_sp, fen_info);
2025 if (IS_ERR(fib_entry)) {
2026 dev_warn(mlxsw_sp->bus_info->dev, "Failed to get FIB4 entry being added.\n");
2027 return PTR_ERR(fib_entry);
2028 }
Jiri Pirko61c503f2016-07-04 08:23:11 +02002029
Jiri Pirko5b004412016-09-01 10:37:40 +02002030 if (fib_entry->ref_count != 1)
2031 return 0;
2032
Jiri Pirko61c503f2016-07-04 08:23:11 +02002033 vr = fib_entry->vr;
Jiri Pirko5b004412016-09-01 10:37:40 +02002034 err = mlxsw_sp_fib_entry_insert(vr->fib, fib_entry);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002035 if (err) {
2036 dev_warn(mlxsw_sp->bus_info->dev, "Failed to insert FIB4 entry being added.\n");
Jiri Pirko61c503f2016-07-04 08:23:11 +02002037 goto err_fib_entry_insert;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002038 }
2039 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002040 if (err)
2041 goto err_fib_entry_add;
2042 return 0;
2043
2044err_fib_entry_add:
2045 mlxsw_sp_fib_entry_remove(vr->fib, fib_entry);
2046err_fib_entry_insert:
Jiri Pirko5b004412016-09-01 10:37:40 +02002047 mlxsw_sp_fib_entry_put(mlxsw_sp, fib_entry);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002048 return err;
2049}
2050
Jiri Pirko37956d72016-10-20 16:05:43 +02002051static void mlxsw_sp_router_fib4_del(struct mlxsw_sp *mlxsw_sp,
2052 struct fib_entry_notifier_info *fen_info)
Jiri Pirko61c503f2016-07-04 08:23:11 +02002053{
Jiri Pirko61c503f2016-07-04 08:23:11 +02002054 struct mlxsw_sp_fib_entry *fib_entry;
Jiri Pirko61c503f2016-07-04 08:23:11 +02002055
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002056 if (mlxsw_sp->router.aborted)
Jiri Pirko37956d72016-10-20 16:05:43 +02002057 return;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002058
2059 fib_entry = mlxsw_sp_fib_entry_find(mlxsw_sp, fen_info);
Jiri Pirko37956d72016-10-20 16:05:43 +02002060 if (!fib_entry)
2061 return;
Jiri Pirko5b004412016-09-01 10:37:40 +02002062
2063 if (fib_entry->ref_count == 1) {
2064 mlxsw_sp_fib_entry_del(mlxsw_sp, fib_entry);
2065 mlxsw_sp_fib_entry_remove(fib_entry->vr->fib, fib_entry);
2066 }
2067
2068 mlxsw_sp_fib_entry_put(mlxsw_sp, fib_entry);
Jiri Pirko61c503f2016-07-04 08:23:11 +02002069}
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002070
2071static int mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp)
2072{
2073 char ralta_pl[MLXSW_REG_RALTA_LEN];
2074 char ralst_pl[MLXSW_REG_RALST_LEN];
2075 char raltb_pl[MLXSW_REG_RALTB_LEN];
2076 char ralue_pl[MLXSW_REG_RALUE_LEN];
2077 int err;
2078
2079 mlxsw_reg_ralta_pack(ralta_pl, true, MLXSW_REG_RALXX_PROTOCOL_IPV4,
2080 MLXSW_SP_LPM_TREE_MIN);
2081 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
2082 if (err)
2083 return err;
2084
2085 mlxsw_reg_ralst_pack(ralst_pl, 0xff, MLXSW_SP_LPM_TREE_MIN);
2086 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
2087 if (err)
2088 return err;
2089
Jiri Pirko19271c12016-10-20 16:05:42 +02002090 mlxsw_reg_raltb_pack(raltb_pl, 0, MLXSW_REG_RALXX_PROTOCOL_IPV4,
2091 MLXSW_SP_LPM_TREE_MIN);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002092 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
2093 if (err)
2094 return err;
2095
2096 mlxsw_reg_ralue_pack4(ralue_pl, MLXSW_SP_L3_PROTO_IPV4,
2097 MLXSW_REG_RALUE_OP_WRITE_WRITE, 0, 0, 0);
2098 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
2099 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
2100}
2101
Ido Schimmelac571de2016-11-14 11:26:32 +01002102static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp)
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002103{
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002104 struct mlxsw_sp_fib_entry *fib_entry;
2105 struct mlxsw_sp_fib_entry *tmp;
2106 struct mlxsw_sp_vr *vr;
2107 int i;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002108
Jiri Pirkoc1a38312016-10-21 16:07:23 +02002109 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002110 vr = &mlxsw_sp->router.vrs[i];
Ido Schimmelac571de2016-11-14 11:26:32 +01002111
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002112 if (!vr->used)
2113 continue;
2114
2115 list_for_each_entry_safe(fib_entry, tmp,
2116 &vr->fib->entry_list, list) {
2117 bool do_break = &tmp->list == &vr->fib->entry_list;
2118
2119 mlxsw_sp_fib_entry_del(mlxsw_sp, fib_entry);
2120 mlxsw_sp_fib_entry_remove(fib_entry->vr->fib,
2121 fib_entry);
2122 mlxsw_sp_fib_entry_put_all(mlxsw_sp, fib_entry);
2123 if (do_break)
2124 break;
2125 }
2126 }
Ido Schimmelac571de2016-11-14 11:26:32 +01002127}
2128
2129static void mlxsw_sp_router_fib4_abort(struct mlxsw_sp *mlxsw_sp)
2130{
2131 int err;
2132
Ido Schimmeld331d302016-11-16 09:51:58 +01002133 if (mlxsw_sp->router.aborted)
2134 return;
2135 dev_warn(mlxsw_sp->bus_info->dev, "FIB abort triggered. Note that FIB entries are no longer being offloaded to this device.\n");
Ido Schimmelac571de2016-11-14 11:26:32 +01002136 mlxsw_sp_router_fib_flush(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002137 mlxsw_sp->router.aborted = true;
2138 err = mlxsw_sp_router_set_abort_trap(mlxsw_sp);
2139 if (err)
2140 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set abort trap.\n");
2141}
2142
Ido Schimmel9665b742017-02-08 11:16:42 +01002143static int mlxsw_sp_router_rif_disable(struct mlxsw_sp *mlxsw_sp, u16 rif)
2144{
2145 char ritr_pl[MLXSW_REG_RITR_LEN];
2146 int err;
2147
2148 mlxsw_reg_ritr_rif_pack(ritr_pl, rif);
2149 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
2150 if (WARN_ON_ONCE(err))
2151 return err;
2152
2153 mlxsw_reg_ritr_enable_set(ritr_pl, false);
2154 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
2155}
2156
2157void mlxsw_sp_router_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
2158 struct mlxsw_sp_rif *r)
2159{
2160 mlxsw_sp_router_rif_disable(mlxsw_sp, r->rif);
2161 mlxsw_sp_nexthop_rif_gone_sync(mlxsw_sp, r);
2162 mlxsw_sp_neigh_rif_gone_sync(mlxsw_sp, r);
2163}
2164
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002165static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
2166{
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002167 char rgcr_pl[MLXSW_REG_RGCR_LEN];
Jiri Pirkoc1a38312016-10-21 16:07:23 +02002168 u64 max_rifs;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002169 int err;
2170
Jiri Pirkoc1a38312016-10-21 16:07:23 +02002171 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_RIFS))
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002172 return -EIO;
2173
Jiri Pirkoc1a38312016-10-21 16:07:23 +02002174 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
2175 mlxsw_sp->rifs = kcalloc(max_rifs, sizeof(struct mlxsw_sp_rif *),
2176 GFP_KERNEL);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002177 if (!mlxsw_sp->rifs)
2178 return -ENOMEM;
2179
2180 mlxsw_reg_rgcr_pack(rgcr_pl, true);
Jiri Pirkoc1a38312016-10-21 16:07:23 +02002181 mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, max_rifs);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002182 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
2183 if (err)
2184 goto err_rgcr_fail;
2185
2186 return 0;
2187
2188err_rgcr_fail:
2189 kfree(mlxsw_sp->rifs);
2190 return err;
2191}
2192
2193static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
2194{
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002195 char rgcr_pl[MLXSW_REG_RGCR_LEN];
2196 int i;
2197
2198 mlxsw_reg_rgcr_pack(rgcr_pl, false);
2199 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
2200
Jiri Pirkoc1a38312016-10-21 16:07:23 +02002201 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002202 WARN_ON_ONCE(mlxsw_sp->rifs[i]);
2203
2204 kfree(mlxsw_sp->rifs);
2205}
2206
Ido Schimmel30572242016-12-03 16:45:01 +01002207struct mlxsw_sp_fib_event_work {
Ido Schimmela0e47612017-02-06 16:20:10 +01002208 struct work_struct work;
Ido Schimmelad178c82017-02-08 11:16:40 +01002209 union {
2210 struct fib_entry_notifier_info fen_info;
2211 struct fib_nh_notifier_info fnh_info;
2212 };
Ido Schimmel30572242016-12-03 16:45:01 +01002213 struct mlxsw_sp *mlxsw_sp;
2214 unsigned long event;
2215};
2216
2217static void mlxsw_sp_router_fib_event_work(struct work_struct *work)
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002218{
Ido Schimmel30572242016-12-03 16:45:01 +01002219 struct mlxsw_sp_fib_event_work *fib_work =
Ido Schimmela0e47612017-02-06 16:20:10 +01002220 container_of(work, struct mlxsw_sp_fib_event_work, work);
Ido Schimmel30572242016-12-03 16:45:01 +01002221 struct mlxsw_sp *mlxsw_sp = fib_work->mlxsw_sp;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002222 int err;
2223
Ido Schimmel30572242016-12-03 16:45:01 +01002224 /* Protect internal structures from changes */
2225 rtnl_lock();
2226 switch (fib_work->event) {
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002227 case FIB_EVENT_ENTRY_ADD:
Ido Schimmel30572242016-12-03 16:45:01 +01002228 err = mlxsw_sp_router_fib4_add(mlxsw_sp, &fib_work->fen_info);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002229 if (err)
2230 mlxsw_sp_router_fib4_abort(mlxsw_sp);
Ido Schimmel30572242016-12-03 16:45:01 +01002231 fib_info_put(fib_work->fen_info.fi);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002232 break;
2233 case FIB_EVENT_ENTRY_DEL:
Ido Schimmel30572242016-12-03 16:45:01 +01002234 mlxsw_sp_router_fib4_del(mlxsw_sp, &fib_work->fen_info);
2235 fib_info_put(fib_work->fen_info.fi);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002236 break;
2237 case FIB_EVENT_RULE_ADD: /* fall through */
2238 case FIB_EVENT_RULE_DEL:
2239 mlxsw_sp_router_fib4_abort(mlxsw_sp);
2240 break;
Ido Schimmelad178c82017-02-08 11:16:40 +01002241 case FIB_EVENT_NH_ADD: /* fall through */
2242 case FIB_EVENT_NH_DEL:
2243 mlxsw_sp_nexthop_event(mlxsw_sp, fib_work->event,
2244 fib_work->fnh_info.fib_nh);
2245 fib_info_put(fib_work->fnh_info.fib_nh->nh_parent);
2246 break;
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002247 }
Ido Schimmel30572242016-12-03 16:45:01 +01002248 rtnl_unlock();
2249 kfree(fib_work);
2250}
2251
2252/* Called with rcu_read_lock() */
2253static int mlxsw_sp_router_fib_event(struct notifier_block *nb,
2254 unsigned long event, void *ptr)
2255{
2256 struct mlxsw_sp *mlxsw_sp = container_of(nb, struct mlxsw_sp, fib_nb);
2257 struct mlxsw_sp_fib_event_work *fib_work;
2258 struct fib_notifier_info *info = ptr;
2259
2260 if (!net_eq(info->net, &init_net))
2261 return NOTIFY_DONE;
2262
2263 fib_work = kzalloc(sizeof(*fib_work), GFP_ATOMIC);
2264 if (WARN_ON(!fib_work))
2265 return NOTIFY_BAD;
2266
Ido Schimmela0e47612017-02-06 16:20:10 +01002267 INIT_WORK(&fib_work->work, mlxsw_sp_router_fib_event_work);
Ido Schimmel30572242016-12-03 16:45:01 +01002268 fib_work->mlxsw_sp = mlxsw_sp;
2269 fib_work->event = event;
2270
2271 switch (event) {
2272 case FIB_EVENT_ENTRY_ADD: /* fall through */
2273 case FIB_EVENT_ENTRY_DEL:
2274 memcpy(&fib_work->fen_info, ptr, sizeof(fib_work->fen_info));
2275 /* Take referece on fib_info to prevent it from being
2276 * freed while work is queued. Release it afterwards.
2277 */
2278 fib_info_hold(fib_work->fen_info.fi);
2279 break;
Ido Schimmelad178c82017-02-08 11:16:40 +01002280 case FIB_EVENT_NH_ADD: /* fall through */
2281 case FIB_EVENT_NH_DEL:
2282 memcpy(&fib_work->fnh_info, ptr, sizeof(fib_work->fnh_info));
2283 fib_info_hold(fib_work->fnh_info.fib_nh->nh_parent);
2284 break;
Ido Schimmel30572242016-12-03 16:45:01 +01002285 }
2286
Ido Schimmela0e47612017-02-06 16:20:10 +01002287 mlxsw_core_schedule_work(&fib_work->work);
Ido Schimmel30572242016-12-03 16:45:01 +01002288
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002289 return NOTIFY_DONE;
2290}
2291
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002292static void mlxsw_sp_router_fib_dump_flush(struct notifier_block *nb)
2293{
2294 struct mlxsw_sp *mlxsw_sp = container_of(nb, struct mlxsw_sp, fib_nb);
2295
2296 /* Flush pending FIB notifications and then flush the device's
2297 * table before requesting another dump. The FIB notification
2298 * block is unregistered, so no need to take RTNL.
2299 */
2300 mlxsw_core_flush_owq();
2301 mlxsw_sp_router_fib_flush(mlxsw_sp);
2302}
2303
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002304int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
2305{
2306 int err;
2307
2308 INIT_LIST_HEAD(&mlxsw_sp->router.nexthop_neighs_list);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002309 err = __mlxsw_sp_router_init(mlxsw_sp);
2310 if (err)
2311 return err;
2312
Ido Schimmelc53b8e12017-02-08 11:16:30 +01002313 err = rhashtable_init(&mlxsw_sp->router.nexthop_ht,
2314 &mlxsw_sp_nexthop_ht_params);
2315 if (err)
2316 goto err_nexthop_ht_init;
2317
Ido Schimmele9ad5e72017-02-08 11:16:29 +01002318 err = rhashtable_init(&mlxsw_sp->router.nexthop_group_ht,
2319 &mlxsw_sp_nexthop_group_ht_params);
2320 if (err)
2321 goto err_nexthop_group_ht_init;
2322
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002323 mlxsw_sp_lpm_init(mlxsw_sp);
2324 err = mlxsw_sp_vrs_init(mlxsw_sp);
2325 if (err)
2326 goto err_vrs_init;
2327
Ido Schimmel8c9583a2016-10-27 15:12:57 +02002328 err = mlxsw_sp_neigh_init(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002329 if (err)
2330 goto err_neigh_init;
2331
2332 mlxsw_sp->fib_nb.notifier_call = mlxsw_sp_router_fib_event;
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002333 err = register_fib_notifier(&mlxsw_sp->fib_nb,
2334 mlxsw_sp_router_fib_dump_flush);
2335 if (err)
2336 goto err_register_fib_notifier;
2337
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002338 return 0;
2339
Ido Schimmelc3852ef2016-12-03 16:45:07 +01002340err_register_fib_notifier:
2341 mlxsw_sp_neigh_fini(mlxsw_sp);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002342err_neigh_init:
2343 mlxsw_sp_vrs_fini(mlxsw_sp);
2344err_vrs_init:
Ido Schimmele9ad5e72017-02-08 11:16:29 +01002345 rhashtable_destroy(&mlxsw_sp->router.nexthop_group_ht);
2346err_nexthop_group_ht_init:
Ido Schimmelc53b8e12017-02-08 11:16:30 +01002347 rhashtable_destroy(&mlxsw_sp->router.nexthop_ht);
2348err_nexthop_ht_init:
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002349 __mlxsw_sp_router_fini(mlxsw_sp);
2350 return err;
2351}
2352
2353void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
2354{
2355 unregister_fib_notifier(&mlxsw_sp->fib_nb);
2356 mlxsw_sp_neigh_fini(mlxsw_sp);
2357 mlxsw_sp_vrs_fini(mlxsw_sp);
Ido Schimmele9ad5e72017-02-08 11:16:29 +01002358 rhashtable_destroy(&mlxsw_sp->router.nexthop_group_ht);
Ido Schimmelc53b8e12017-02-08 11:16:30 +01002359 rhashtable_destroy(&mlxsw_sp->router.nexthop_ht);
Jiri Pirkob45f64d2016-09-26 12:52:31 +02002360 __mlxsw_sp_router_fini(mlxsw_sp);
2361}