cxgb4/cxgb4vf: check fw caps to set link mode mask
[linux-2.6-block.git] / drivers / net / ethernet / mellanox / mlxsw / spectrum_router.c
CommitLineData
464dce18
IS
1/*
2 * drivers/net/ethernet/mellanox/mlxsw/spectrum_router.c
e437f3b6 3 * Copyright (c) 2016-2018 Mellanox Technologies. All rights reserved.
464dce18
IS
4 * Copyright (c) 2016 Jiri Pirko <jiri@mellanox.com>
5 * Copyright (c) 2016 Ido Schimmel <idosch@mellanox.com>
c723c735 6 * Copyright (c) 2016 Yotam Gigi <yotamg@mellanox.com>
e437f3b6 7 * Copyright (c) 2017-2018 Petr Machata <petrm@mellanox.com>
464dce18
IS
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
20 *
21 * Alternatively, this software may be distributed under the terms of the
22 * GNU General Public License ("GPL") version 2 as published by the Free
23 * Software Foundation.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/kernel.h>
39#include <linux/types.h>
5e9c16cc
JP
40#include <linux/rhashtable.h>
41#include <linux/bitops.h>
42#include <linux/in6.h>
c723c735 43#include <linux/notifier.h>
df6dd79b 44#include <linux/inetdevice.h>
9db032bb 45#include <linux/netdevice.h>
03ea01e9 46#include <linux/if_bridge.h>
b5f3e0d4 47#include <linux/socket.h>
428b851f 48#include <linux/route.h>
eb789980 49#include <linux/gcd.h>
af658b6a 50#include <linux/random.h>
c723c735 51#include <net/netevent.h>
6cf3c971
JP
52#include <net/neighbour.h>
53#include <net/arp.h>
b45f64d1 54#include <net/ip_fib.h>
583419fd 55#include <net/ip6_fib.h>
5d7bfd14 56#include <net/fib_rules.h>
6ddb7426 57#include <net/ip_tunnels.h>
57837885 58#include <net/l3mdev.h>
5ea1237f 59#include <net/addrconf.h>
d5eb89cf
AS
60#include <net/ndisc.h>
61#include <net/ipv6.h>
04b1d4e5 62#include <net/fib_notifier.h>
464dce18
IS
63
64#include "spectrum.h"
65#include "core.h"
66#include "reg.h"
e0c0afd8
AS
67#include "spectrum_cnt.h"
68#include "spectrum_dpipe.h"
38ebc0f4 69#include "spectrum_ipip.h"
d42b0965
YG
70#include "spectrum_mr.h"
71#include "spectrum_mr_tcam.h"
e0c0afd8 72#include "spectrum_router.h"
803335ac 73#include "spectrum_span.h"
464dce18 74
2b52ce02 75struct mlxsw_sp_fib;
9011b677
IS
76struct mlxsw_sp_vr;
77struct mlxsw_sp_lpm_tree;
e4f3c1c1 78struct mlxsw_sp_rif_ops;
9011b677
IS
79
80struct mlxsw_sp_router {
81 struct mlxsw_sp *mlxsw_sp;
5f9efffb 82 struct mlxsw_sp_rif **rifs;
9011b677
IS
83 struct mlxsw_sp_vr *vrs;
84 struct rhashtable neigh_ht;
85 struct rhashtable nexthop_group_ht;
86 struct rhashtable nexthop_ht;
dbe4598c 87 struct list_head nexthop_list;
9011b677 88 struct {
2b52ce02
IS
89 /* One tree for each protocol: IPv4 and IPv6 */
90 struct mlxsw_sp_lpm_tree *proto_trees[2];
9011b677
IS
91 struct mlxsw_sp_lpm_tree *trees;
92 unsigned int tree_count;
93 } lpm;
94 struct {
95 struct delayed_work dw;
96 unsigned long interval; /* ms */
97 } neighs_update;
98 struct delayed_work nexthop_probe_dw;
99#define MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL 5000 /* ms */
100 struct list_head nexthop_neighs_list;
1012b9ac 101 struct list_head ipip_list;
9011b677 102 bool aborted;
7e39d115 103 struct notifier_block fib_nb;
48fac885 104 struct notifier_block netevent_nb;
e4f3c1c1 105 const struct mlxsw_sp_rif_ops **rif_ops_arr;
38ebc0f4 106 const struct mlxsw_sp_ipip_ops **ipip_ops_arr;
9011b677
IS
107};
108
4724ba56
IS
109struct mlxsw_sp_rif {
110 struct list_head nexthop_list;
111 struct list_head neigh_list;
112 struct net_device *dev;
a1107487 113 struct mlxsw_sp_fid *fid;
4724ba56
IS
114 unsigned char addr[ETH_ALEN];
115 int mtu;
bf95233e 116 u16 rif_index;
6913229e 117 u16 vr_id;
e4f3c1c1
IS
118 const struct mlxsw_sp_rif_ops *ops;
119 struct mlxsw_sp *mlxsw_sp;
120
e0c0afd8
AS
121 unsigned int counter_ingress;
122 bool counter_ingress_valid;
123 unsigned int counter_egress;
124 bool counter_egress_valid;
4724ba56
IS
125};
126
e4f3c1c1
IS
127struct mlxsw_sp_rif_params {
128 struct net_device *dev;
129 union {
130 u16 system_port;
131 u16 lag_id;
132 };
133 u16 vid;
134 bool lag;
135};
136
4d93ceeb
IS
137struct mlxsw_sp_rif_subport {
138 struct mlxsw_sp_rif common;
139 union {
140 u16 system_port;
141 u16 lag_id;
142 };
143 u16 vid;
144 bool lag;
145};
146
6ddb7426
PM
147struct mlxsw_sp_rif_ipip_lb {
148 struct mlxsw_sp_rif common;
149 struct mlxsw_sp_rif_ipip_lb_config lb_config;
150 u16 ul_vr_id; /* Reserved for Spectrum-2. */
151};
152
153struct mlxsw_sp_rif_params_ipip_lb {
154 struct mlxsw_sp_rif_params common;
155 struct mlxsw_sp_rif_ipip_lb_config lb_config;
156};
157
e4f3c1c1
IS
158struct mlxsw_sp_rif_ops {
159 enum mlxsw_sp_rif_type type;
160 size_t rif_size;
161
162 void (*setup)(struct mlxsw_sp_rif *rif,
163 const struct mlxsw_sp_rif_params *params);
164 int (*configure)(struct mlxsw_sp_rif *rif);
165 void (*deconfigure)(struct mlxsw_sp_rif *rif);
166 struct mlxsw_sp_fid * (*fid_get)(struct mlxsw_sp_rif *rif);
167};
168
2b52ce02
IS
169static void mlxsw_sp_lpm_tree_hold(struct mlxsw_sp_lpm_tree *lpm_tree);
170static void mlxsw_sp_lpm_tree_put(struct mlxsw_sp *mlxsw_sp,
171 struct mlxsw_sp_lpm_tree *lpm_tree);
172static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp *mlxsw_sp,
173 const struct mlxsw_sp_fib *fib,
174 u8 tree_id);
175static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp *mlxsw_sp,
176 const struct mlxsw_sp_fib *fib);
177
e0c0afd8
AS
178static unsigned int *
179mlxsw_sp_rif_p_counter_get(struct mlxsw_sp_rif *rif,
180 enum mlxsw_sp_rif_counter_dir dir)
181{
182 switch (dir) {
183 case MLXSW_SP_RIF_COUNTER_EGRESS:
184 return &rif->counter_egress;
185 case MLXSW_SP_RIF_COUNTER_INGRESS:
186 return &rif->counter_ingress;
187 }
188 return NULL;
189}
190
191static bool
192mlxsw_sp_rif_counter_valid_get(struct mlxsw_sp_rif *rif,
193 enum mlxsw_sp_rif_counter_dir dir)
194{
195 switch (dir) {
196 case MLXSW_SP_RIF_COUNTER_EGRESS:
197 return rif->counter_egress_valid;
198 case MLXSW_SP_RIF_COUNTER_INGRESS:
199 return rif->counter_ingress_valid;
200 }
201 return false;
202}
203
204static void
205mlxsw_sp_rif_counter_valid_set(struct mlxsw_sp_rif *rif,
206 enum mlxsw_sp_rif_counter_dir dir,
207 bool valid)
208{
209 switch (dir) {
210 case MLXSW_SP_RIF_COUNTER_EGRESS:
211 rif->counter_egress_valid = valid;
212 break;
213 case MLXSW_SP_RIF_COUNTER_INGRESS:
214 rif->counter_ingress_valid = valid;
215 break;
216 }
217}
218
219static int mlxsw_sp_rif_counter_edit(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
220 unsigned int counter_index, bool enable,
221 enum mlxsw_sp_rif_counter_dir dir)
222{
223 char ritr_pl[MLXSW_REG_RITR_LEN];
224 bool is_egress = false;
225 int err;
226
227 if (dir == MLXSW_SP_RIF_COUNTER_EGRESS)
228 is_egress = true;
229 mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
230 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
231 if (err)
232 return err;
233
234 mlxsw_reg_ritr_counter_pack(ritr_pl, counter_index, enable,
235 is_egress);
236 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
237}
238
239int mlxsw_sp_rif_counter_value_get(struct mlxsw_sp *mlxsw_sp,
240 struct mlxsw_sp_rif *rif,
241 enum mlxsw_sp_rif_counter_dir dir, u64 *cnt)
242{
243 char ricnt_pl[MLXSW_REG_RICNT_LEN];
244 unsigned int *p_counter_index;
245 bool valid;
246 int err;
247
248 valid = mlxsw_sp_rif_counter_valid_get(rif, dir);
249 if (!valid)
250 return -EINVAL;
251
252 p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
253 if (!p_counter_index)
254 return -EINVAL;
255 mlxsw_reg_ricnt_pack(ricnt_pl, *p_counter_index,
256 MLXSW_REG_RICNT_OPCODE_NOP);
257 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ricnt), ricnt_pl);
258 if (err)
259 return err;
260 *cnt = mlxsw_reg_ricnt_good_unicast_packets_get(ricnt_pl);
261 return 0;
262}
263
264static int mlxsw_sp_rif_counter_clear(struct mlxsw_sp *mlxsw_sp,
265 unsigned int counter_index)
266{
267 char ricnt_pl[MLXSW_REG_RICNT_LEN];
268
269 mlxsw_reg_ricnt_pack(ricnt_pl, counter_index,
270 MLXSW_REG_RICNT_OPCODE_CLEAR);
271 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ricnt), ricnt_pl);
272}
273
274int mlxsw_sp_rif_counter_alloc(struct mlxsw_sp *mlxsw_sp,
275 struct mlxsw_sp_rif *rif,
276 enum mlxsw_sp_rif_counter_dir dir)
277{
278 unsigned int *p_counter_index;
279 int err;
280
281 p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
282 if (!p_counter_index)
283 return -EINVAL;
284 err = mlxsw_sp_counter_alloc(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
285 p_counter_index);
286 if (err)
287 return err;
288
289 err = mlxsw_sp_rif_counter_clear(mlxsw_sp, *p_counter_index);
290 if (err)
291 goto err_counter_clear;
292
293 err = mlxsw_sp_rif_counter_edit(mlxsw_sp, rif->rif_index,
294 *p_counter_index, true, dir);
295 if (err)
296 goto err_counter_edit;
297 mlxsw_sp_rif_counter_valid_set(rif, dir, true);
298 return 0;
299
300err_counter_edit:
301err_counter_clear:
302 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
303 *p_counter_index);
304 return err;
305}
306
307void mlxsw_sp_rif_counter_free(struct mlxsw_sp *mlxsw_sp,
308 struct mlxsw_sp_rif *rif,
309 enum mlxsw_sp_rif_counter_dir dir)
310{
311 unsigned int *p_counter_index;
312
6b1206bb
AS
313 if (!mlxsw_sp_rif_counter_valid_get(rif, dir))
314 return;
315
e0c0afd8
AS
316 p_counter_index = mlxsw_sp_rif_p_counter_get(rif, dir);
317 if (WARN_ON(!p_counter_index))
318 return;
319 mlxsw_sp_rif_counter_edit(mlxsw_sp, rif->rif_index,
320 *p_counter_index, false, dir);
321 mlxsw_sp_counter_free(mlxsw_sp, MLXSW_SP_COUNTER_SUB_POOL_RIF,
322 *p_counter_index);
323 mlxsw_sp_rif_counter_valid_set(rif, dir, false);
324}
325
e4f3c1c1
IS
326static void mlxsw_sp_rif_counters_alloc(struct mlxsw_sp_rif *rif)
327{
328 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
329 struct devlink *devlink;
330
331 devlink = priv_to_devlink(mlxsw_sp->core);
332 if (!devlink_dpipe_table_counter_enabled(devlink,
333 MLXSW_SP_DPIPE_TABLE_NAME_ERIF))
334 return;
335 mlxsw_sp_rif_counter_alloc(mlxsw_sp, rif, MLXSW_SP_RIF_COUNTER_EGRESS);
336}
337
338static void mlxsw_sp_rif_counters_free(struct mlxsw_sp_rif *rif)
339{
340 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
341
342 mlxsw_sp_rif_counter_free(mlxsw_sp, rif, MLXSW_SP_RIF_COUNTER_EGRESS);
343}
344
4724ba56
IS
345static struct mlxsw_sp_rif *
346mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
347 const struct net_device *dev);
348
7dcc18ad 349#define MLXSW_SP_PREFIX_COUNT (sizeof(struct in6_addr) * BITS_PER_BYTE + 1)
9011b677
IS
350
351struct mlxsw_sp_prefix_usage {
352 DECLARE_BITMAP(b, MLXSW_SP_PREFIX_COUNT);
353};
354
53342023
JP
355#define mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) \
356 for_each_set_bit(prefix, (prefix_usage)->b, MLXSW_SP_PREFIX_COUNT)
357
358static bool
359mlxsw_sp_prefix_usage_eq(struct mlxsw_sp_prefix_usage *prefix_usage1,
360 struct mlxsw_sp_prefix_usage *prefix_usage2)
361{
362 return !memcmp(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
363}
364
6b75c480
JP
365static void
366mlxsw_sp_prefix_usage_cpy(struct mlxsw_sp_prefix_usage *prefix_usage1,
367 struct mlxsw_sp_prefix_usage *prefix_usage2)
368{
369 memcpy(prefix_usage1, prefix_usage2, sizeof(*prefix_usage1));
370}
371
5e9c16cc
JP
372static void
373mlxsw_sp_prefix_usage_set(struct mlxsw_sp_prefix_usage *prefix_usage,
374 unsigned char prefix_len)
375{
376 set_bit(prefix_len, prefix_usage->b);
377}
378
379static void
380mlxsw_sp_prefix_usage_clear(struct mlxsw_sp_prefix_usage *prefix_usage,
381 unsigned char prefix_len)
382{
383 clear_bit(prefix_len, prefix_usage->b);
384}
385
386struct mlxsw_sp_fib_key {
387 unsigned char addr[sizeof(struct in6_addr)];
388 unsigned char prefix_len;
389};
390
61c503f9
JP
391enum mlxsw_sp_fib_entry_type {
392 MLXSW_SP_FIB_ENTRY_TYPE_REMOTE,
393 MLXSW_SP_FIB_ENTRY_TYPE_LOCAL,
394 MLXSW_SP_FIB_ENTRY_TYPE_TRAP,
4607f6d2
PM
395
396 /* This is a special case of local delivery, where a packet should be
397 * decapsulated on reception. Note that there is no corresponding ENCAP,
398 * because that's a type of next hop, not of FIB entry. (There can be
399 * several next hops in a REMOTE entry, and some of them may be
400 * encapsulating entries.)
401 */
402 MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP,
61c503f9
JP
403};
404
a7ff87ac
JP
405struct mlxsw_sp_nexthop_group;
406
9aecce1c
IS
407struct mlxsw_sp_fib_node {
408 struct list_head entry_list;
b45f64d1 409 struct list_head list;
9aecce1c 410 struct rhash_head ht_node;
76610ebb 411 struct mlxsw_sp_fib *fib;
5e9c16cc 412 struct mlxsw_sp_fib_key key;
9aecce1c
IS
413};
414
4607f6d2
PM
415struct mlxsw_sp_fib_entry_decap {
416 struct mlxsw_sp_ipip_entry *ipip_entry;
417 u32 tunnel_index;
418};
419
9aecce1c
IS
420struct mlxsw_sp_fib_entry {
421 struct list_head list;
422 struct mlxsw_sp_fib_node *fib_node;
61c503f9 423 enum mlxsw_sp_fib_entry_type type;
a7ff87ac
JP
424 struct list_head nexthop_group_node;
425 struct mlxsw_sp_nexthop_group *nh_group;
4607f6d2 426 struct mlxsw_sp_fib_entry_decap decap; /* Valid for decap entries. */
5e9c16cc
JP
427};
428
4f1c7f1f
IS
429struct mlxsw_sp_fib4_entry {
430 struct mlxsw_sp_fib_entry common;
431 u32 tb_id;
432 u32 prio;
433 u8 tos;
434 u8 type;
435};
436
428b851f
IS
437struct mlxsw_sp_fib6_entry {
438 struct mlxsw_sp_fib_entry common;
439 struct list_head rt6_list;
440 unsigned int nrt6;
441};
442
443struct mlxsw_sp_rt6 {
444 struct list_head list;
445 struct rt6_info *rt;
446};
447
9011b677
IS
448struct mlxsw_sp_lpm_tree {
449 u8 id; /* tree ID */
450 unsigned int ref_count;
451 enum mlxsw_sp_l3proto proto;
2b52ce02 452 unsigned long prefix_ref_count[MLXSW_SP_PREFIX_COUNT];
9011b677
IS
453 struct mlxsw_sp_prefix_usage prefix_usage;
454};
455
5e9c16cc
JP
456struct mlxsw_sp_fib {
457 struct rhashtable ht;
9aecce1c 458 struct list_head node_list;
76610ebb
IS
459 struct mlxsw_sp_vr *vr;
460 struct mlxsw_sp_lpm_tree *lpm_tree;
76610ebb 461 enum mlxsw_sp_l3proto proto;
5e9c16cc
JP
462};
463
9011b677
IS
464struct mlxsw_sp_vr {
465 u16 id; /* virtual router ID */
466 u32 tb_id; /* kernel fib table id */
467 unsigned int rif_count;
468 struct mlxsw_sp_fib *fib4;
a3d9bc50 469 struct mlxsw_sp_fib *fib6;
d42b0965 470 struct mlxsw_sp_mr_table *mr4_table;
9011b677
IS
471};
472
9aecce1c 473static const struct rhashtable_params mlxsw_sp_fib_ht_params;
5e9c16cc 474
2b52ce02
IS
475static struct mlxsw_sp_fib *mlxsw_sp_fib_create(struct mlxsw_sp *mlxsw_sp,
476 struct mlxsw_sp_vr *vr,
76610ebb 477 enum mlxsw_sp_l3proto proto)
5e9c16cc 478{
2b52ce02 479 struct mlxsw_sp_lpm_tree *lpm_tree;
5e9c16cc
JP
480 struct mlxsw_sp_fib *fib;
481 int err;
482
2b52ce02 483 lpm_tree = mlxsw_sp->router->lpm.proto_trees[proto];
5e9c16cc
JP
484 fib = kzalloc(sizeof(*fib), GFP_KERNEL);
485 if (!fib)
486 return ERR_PTR(-ENOMEM);
487 err = rhashtable_init(&fib->ht, &mlxsw_sp_fib_ht_params);
488 if (err)
489 goto err_rhashtable_init;
9aecce1c 490 INIT_LIST_HEAD(&fib->node_list);
76610ebb
IS
491 fib->proto = proto;
492 fib->vr = vr;
2b52ce02
IS
493 fib->lpm_tree = lpm_tree;
494 mlxsw_sp_lpm_tree_hold(lpm_tree);
495 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, fib, lpm_tree->id);
496 if (err)
497 goto err_lpm_tree_bind;
5e9c16cc
JP
498 return fib;
499
2b52ce02
IS
500err_lpm_tree_bind:
501 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
5e9c16cc
JP
502err_rhashtable_init:
503 kfree(fib);
504 return ERR_PTR(err);
505}
506
2b52ce02
IS
507static void mlxsw_sp_fib_destroy(struct mlxsw_sp *mlxsw_sp,
508 struct mlxsw_sp_fib *fib)
5e9c16cc 509{
2b52ce02
IS
510 mlxsw_sp_vr_lpm_tree_unbind(mlxsw_sp, fib);
511 mlxsw_sp_lpm_tree_put(mlxsw_sp, fib->lpm_tree);
9aecce1c 512 WARN_ON(!list_empty(&fib->node_list));
5e9c16cc
JP
513 rhashtable_destroy(&fib->ht);
514 kfree(fib);
515}
516
53342023 517static struct mlxsw_sp_lpm_tree *
382dbb40 518mlxsw_sp_lpm_tree_find_unused(struct mlxsw_sp *mlxsw_sp)
53342023
JP
519{
520 static struct mlxsw_sp_lpm_tree *lpm_tree;
521 int i;
522
9011b677
IS
523 for (i = 0; i < mlxsw_sp->router->lpm.tree_count; i++) {
524 lpm_tree = &mlxsw_sp->router->lpm.trees[i];
382dbb40
IS
525 if (lpm_tree->ref_count == 0)
526 return lpm_tree;
53342023
JP
527 }
528 return NULL;
529}
530
531static int mlxsw_sp_lpm_tree_alloc(struct mlxsw_sp *mlxsw_sp,
532 struct mlxsw_sp_lpm_tree *lpm_tree)
533{
534 char ralta_pl[MLXSW_REG_RALTA_LEN];
535
1a9234e6
IS
536 mlxsw_reg_ralta_pack(ralta_pl, true,
537 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
538 lpm_tree->id);
53342023
JP
539 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
540}
541
cc702670
IS
542static void mlxsw_sp_lpm_tree_free(struct mlxsw_sp *mlxsw_sp,
543 struct mlxsw_sp_lpm_tree *lpm_tree)
53342023
JP
544{
545 char ralta_pl[MLXSW_REG_RALTA_LEN];
546
1a9234e6
IS
547 mlxsw_reg_ralta_pack(ralta_pl, false,
548 (enum mlxsw_reg_ralxx_protocol) lpm_tree->proto,
549 lpm_tree->id);
cc702670 550 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
53342023
JP
551}
552
553static int
554mlxsw_sp_lpm_tree_left_struct_set(struct mlxsw_sp *mlxsw_sp,
555 struct mlxsw_sp_prefix_usage *prefix_usage,
556 struct mlxsw_sp_lpm_tree *lpm_tree)
557{
558 char ralst_pl[MLXSW_REG_RALST_LEN];
559 u8 root_bin = 0;
560 u8 prefix;
561 u8 last_prefix = MLXSW_REG_RALST_BIN_NO_CHILD;
562
563 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage)
564 root_bin = prefix;
565
566 mlxsw_reg_ralst_pack(ralst_pl, root_bin, lpm_tree->id);
567 mlxsw_sp_prefix_usage_for_each(prefix, prefix_usage) {
568 if (prefix == 0)
569 continue;
570 mlxsw_reg_ralst_bin_pack(ralst_pl, prefix, last_prefix,
571 MLXSW_REG_RALST_BIN_NO_CHILD);
572 last_prefix = prefix;
573 }
574 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
575}
576
577static struct mlxsw_sp_lpm_tree *
578mlxsw_sp_lpm_tree_create(struct mlxsw_sp *mlxsw_sp,
579 struct mlxsw_sp_prefix_usage *prefix_usage,
382dbb40 580 enum mlxsw_sp_l3proto proto)
53342023
JP
581{
582 struct mlxsw_sp_lpm_tree *lpm_tree;
583 int err;
584
382dbb40 585 lpm_tree = mlxsw_sp_lpm_tree_find_unused(mlxsw_sp);
53342023
JP
586 if (!lpm_tree)
587 return ERR_PTR(-EBUSY);
588 lpm_tree->proto = proto;
589 err = mlxsw_sp_lpm_tree_alloc(mlxsw_sp, lpm_tree);
590 if (err)
591 return ERR_PTR(err);
592
593 err = mlxsw_sp_lpm_tree_left_struct_set(mlxsw_sp, prefix_usage,
594 lpm_tree);
595 if (err)
596 goto err_left_struct_set;
2083d367
JP
597 memcpy(&lpm_tree->prefix_usage, prefix_usage,
598 sizeof(lpm_tree->prefix_usage));
2b52ce02
IS
599 memset(&lpm_tree->prefix_ref_count, 0,
600 sizeof(lpm_tree->prefix_ref_count));
601 lpm_tree->ref_count = 1;
53342023
JP
602 return lpm_tree;
603
604err_left_struct_set:
605 mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
606 return ERR_PTR(err);
607}
608
cc702670
IS
609static void mlxsw_sp_lpm_tree_destroy(struct mlxsw_sp *mlxsw_sp,
610 struct mlxsw_sp_lpm_tree *lpm_tree)
53342023 611{
cc702670 612 mlxsw_sp_lpm_tree_free(mlxsw_sp, lpm_tree);
53342023
JP
613}
614
615static struct mlxsw_sp_lpm_tree *
616mlxsw_sp_lpm_tree_get(struct mlxsw_sp *mlxsw_sp,
617 struct mlxsw_sp_prefix_usage *prefix_usage,
382dbb40 618 enum mlxsw_sp_l3proto proto)
53342023
JP
619{
620 struct mlxsw_sp_lpm_tree *lpm_tree;
621 int i;
622
9011b677
IS
623 for (i = 0; i < mlxsw_sp->router->lpm.tree_count; i++) {
624 lpm_tree = &mlxsw_sp->router->lpm.trees[i];
8b99becd
JP
625 if (lpm_tree->ref_count != 0 &&
626 lpm_tree->proto == proto &&
53342023 627 mlxsw_sp_prefix_usage_eq(&lpm_tree->prefix_usage,
2b52ce02
IS
628 prefix_usage)) {
629 mlxsw_sp_lpm_tree_hold(lpm_tree);
fc922bb0 630 return lpm_tree;
2b52ce02 631 }
53342023 632 }
fc922bb0
IS
633 return mlxsw_sp_lpm_tree_create(mlxsw_sp, prefix_usage, proto);
634}
53342023 635
fc922bb0
IS
636static void mlxsw_sp_lpm_tree_hold(struct mlxsw_sp_lpm_tree *lpm_tree)
637{
53342023 638 lpm_tree->ref_count++;
53342023
JP
639}
640
cc702670
IS
641static void mlxsw_sp_lpm_tree_put(struct mlxsw_sp *mlxsw_sp,
642 struct mlxsw_sp_lpm_tree *lpm_tree)
53342023
JP
643{
644 if (--lpm_tree->ref_count == 0)
cc702670 645 mlxsw_sp_lpm_tree_destroy(mlxsw_sp, lpm_tree);
53342023
JP
646}
647
d7a60306 648#define MLXSW_SP_LPM_TREE_MIN 1 /* tree 0 is reserved */
8494ab06
IS
649
650static int mlxsw_sp_lpm_init(struct mlxsw_sp *mlxsw_sp)
53342023 651{
2b52ce02 652 struct mlxsw_sp_prefix_usage req_prefix_usage = {{ 0 } };
53342023 653 struct mlxsw_sp_lpm_tree *lpm_tree;
8494ab06 654 u64 max_trees;
2b52ce02 655 int err, i;
53342023 656
8494ab06
IS
657 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_LPM_TREES))
658 return -EIO;
659
660 max_trees = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_LPM_TREES);
9011b677
IS
661 mlxsw_sp->router->lpm.tree_count = max_trees - MLXSW_SP_LPM_TREE_MIN;
662 mlxsw_sp->router->lpm.trees = kcalloc(mlxsw_sp->router->lpm.tree_count,
8494ab06
IS
663 sizeof(struct mlxsw_sp_lpm_tree),
664 GFP_KERNEL);
9011b677 665 if (!mlxsw_sp->router->lpm.trees)
8494ab06
IS
666 return -ENOMEM;
667
9011b677
IS
668 for (i = 0; i < mlxsw_sp->router->lpm.tree_count; i++) {
669 lpm_tree = &mlxsw_sp->router->lpm.trees[i];
53342023
JP
670 lpm_tree->id = i + MLXSW_SP_LPM_TREE_MIN;
671 }
8494ab06 672
2b52ce02
IS
673 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
674 MLXSW_SP_L3_PROTO_IPV4);
675 if (IS_ERR(lpm_tree)) {
676 err = PTR_ERR(lpm_tree);
677 goto err_ipv4_tree_get;
678 }
679 mlxsw_sp->router->lpm.proto_trees[MLXSW_SP_L3_PROTO_IPV4] = lpm_tree;
680
681 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
682 MLXSW_SP_L3_PROTO_IPV6);
683 if (IS_ERR(lpm_tree)) {
684 err = PTR_ERR(lpm_tree);
685 goto err_ipv6_tree_get;
686 }
687 mlxsw_sp->router->lpm.proto_trees[MLXSW_SP_L3_PROTO_IPV6] = lpm_tree;
688
8494ab06 689 return 0;
2b52ce02
IS
690
691err_ipv6_tree_get:
692 lpm_tree = mlxsw_sp->router->lpm.proto_trees[MLXSW_SP_L3_PROTO_IPV4];
693 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
694err_ipv4_tree_get:
695 kfree(mlxsw_sp->router->lpm.trees);
696 return err;
8494ab06
IS
697}
698
699static void mlxsw_sp_lpm_fini(struct mlxsw_sp *mlxsw_sp)
700{
2b52ce02
IS
701 struct mlxsw_sp_lpm_tree *lpm_tree;
702
703 lpm_tree = mlxsw_sp->router->lpm.proto_trees[MLXSW_SP_L3_PROTO_IPV6];
704 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
705
706 lpm_tree = mlxsw_sp->router->lpm.proto_trees[MLXSW_SP_L3_PROTO_IPV4];
707 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
708
9011b677 709 kfree(mlxsw_sp->router->lpm.trees);
53342023
JP
710}
711
76610ebb
IS
712static bool mlxsw_sp_vr_is_used(const struct mlxsw_sp_vr *vr)
713{
d42b0965 714 return !!vr->fib4 || !!vr->fib6 || !!vr->mr4_table;
76610ebb
IS
715}
716
6b75c480
JP
717static struct mlxsw_sp_vr *mlxsw_sp_vr_find_unused(struct mlxsw_sp *mlxsw_sp)
718{
719 struct mlxsw_sp_vr *vr;
720 int i;
721
c1a38311 722 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
9011b677 723 vr = &mlxsw_sp->router->vrs[i];
76610ebb 724 if (!mlxsw_sp_vr_is_used(vr))
6b75c480
JP
725 return vr;
726 }
727 return NULL;
728}
729
730static int mlxsw_sp_vr_lpm_tree_bind(struct mlxsw_sp *mlxsw_sp,
0adb214b 731 const struct mlxsw_sp_fib *fib, u8 tree_id)
6b75c480
JP
732{
733 char raltb_pl[MLXSW_REG_RALTB_LEN];
734
76610ebb
IS
735 mlxsw_reg_raltb_pack(raltb_pl, fib->vr->id,
736 (enum mlxsw_reg_ralxx_protocol) fib->proto,
0adb214b 737 tree_id);
6b75c480
JP
738 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
739}
740
741static int mlxsw_sp_vr_lpm_tree_unbind(struct mlxsw_sp *mlxsw_sp,
76610ebb 742 const struct mlxsw_sp_fib *fib)
6b75c480
JP
743{
744 char raltb_pl[MLXSW_REG_RALTB_LEN];
745
746 /* Bind to tree 0 which is default */
76610ebb
IS
747 mlxsw_reg_raltb_pack(raltb_pl, fib->vr->id,
748 (enum mlxsw_reg_ralxx_protocol) fib->proto, 0);
6b75c480
JP
749 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb), raltb_pl);
750}
751
752static u32 mlxsw_sp_fix_tb_id(u32 tb_id)
753{
7e50d435
YG
754 /* For our purpose, squash main, default and local tables into one */
755 if (tb_id == RT_TABLE_LOCAL || tb_id == RT_TABLE_DEFAULT)
6b75c480
JP
756 tb_id = RT_TABLE_MAIN;
757 return tb_id;
758}
759
760static struct mlxsw_sp_vr *mlxsw_sp_vr_find(struct mlxsw_sp *mlxsw_sp,
76610ebb 761 u32 tb_id)
6b75c480
JP
762{
763 struct mlxsw_sp_vr *vr;
764 int i;
765
766 tb_id = mlxsw_sp_fix_tb_id(tb_id);
9497c042 767
c1a38311 768 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
9011b677 769 vr = &mlxsw_sp->router->vrs[i];
76610ebb 770 if (mlxsw_sp_vr_is_used(vr) && vr->tb_id == tb_id)
6b75c480
JP
771 return vr;
772 }
773 return NULL;
774}
775
76610ebb
IS
776static struct mlxsw_sp_fib *mlxsw_sp_vr_fib(const struct mlxsw_sp_vr *vr,
777 enum mlxsw_sp_l3proto proto)
778{
779 switch (proto) {
780 case MLXSW_SP_L3_PROTO_IPV4:
781 return vr->fib4;
782 case MLXSW_SP_L3_PROTO_IPV6:
a3d9bc50 783 return vr->fib6;
76610ebb
IS
784 }
785 return NULL;
786}
787
6b75c480 788static struct mlxsw_sp_vr *mlxsw_sp_vr_create(struct mlxsw_sp *mlxsw_sp,
f8fa9b4e
DA
789 u32 tb_id,
790 struct netlink_ext_ack *extack)
6b75c480 791{
0f2d2b27
JP
792 struct mlxsw_sp_mr_table *mr4_table;
793 struct mlxsw_sp_fib *fib4;
794 struct mlxsw_sp_fib *fib6;
6b75c480 795 struct mlxsw_sp_vr *vr;
a3d9bc50 796 int err;
6b75c480
JP
797
798 vr = mlxsw_sp_vr_find_unused(mlxsw_sp);
f8fa9b4e 799 if (!vr) {
6c677750 800 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported virtual routers");
6b75c480 801 return ERR_PTR(-EBUSY);
f8fa9b4e 802 }
0f2d2b27
JP
803 fib4 = mlxsw_sp_fib_create(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV4);
804 if (IS_ERR(fib4))
805 return ERR_CAST(fib4);
806 fib6 = mlxsw_sp_fib_create(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV6);
807 if (IS_ERR(fib6)) {
808 err = PTR_ERR(fib6);
a3d9bc50
IS
809 goto err_fib6_create;
810 }
0f2d2b27
JP
811 mr4_table = mlxsw_sp_mr_table_create(mlxsw_sp, vr->id,
812 MLXSW_SP_L3_PROTO_IPV4);
813 if (IS_ERR(mr4_table)) {
814 err = PTR_ERR(mr4_table);
d42b0965
YG
815 goto err_mr_table_create;
816 }
0f2d2b27
JP
817 vr->fib4 = fib4;
818 vr->fib6 = fib6;
819 vr->mr4_table = mr4_table;
6b75c480 820 vr->tb_id = tb_id;
6b75c480 821 return vr;
a3d9bc50 822
d42b0965 823err_mr_table_create:
0f2d2b27 824 mlxsw_sp_fib_destroy(mlxsw_sp, fib6);
a3d9bc50 825err_fib6_create:
0f2d2b27 826 mlxsw_sp_fib_destroy(mlxsw_sp, fib4);
a3d9bc50 827 return ERR_PTR(err);
6b75c480
JP
828}
829
2b52ce02
IS
830static void mlxsw_sp_vr_destroy(struct mlxsw_sp *mlxsw_sp,
831 struct mlxsw_sp_vr *vr)
6b75c480 832{
d42b0965
YG
833 mlxsw_sp_mr_table_destroy(vr->mr4_table);
834 vr->mr4_table = NULL;
2b52ce02 835 mlxsw_sp_fib_destroy(mlxsw_sp, vr->fib6);
a3d9bc50 836 vr->fib6 = NULL;
2b52ce02 837 mlxsw_sp_fib_destroy(mlxsw_sp, vr->fib4);
76610ebb 838 vr->fib4 = NULL;
6b75c480
JP
839}
840
f8fa9b4e
DA
841static struct mlxsw_sp_vr *mlxsw_sp_vr_get(struct mlxsw_sp *mlxsw_sp, u32 tb_id,
842 struct netlink_ext_ack *extack)
6b75c480
JP
843{
844 struct mlxsw_sp_vr *vr;
6b75c480
JP
845
846 tb_id = mlxsw_sp_fix_tb_id(tb_id);
76610ebb
IS
847 vr = mlxsw_sp_vr_find(mlxsw_sp, tb_id);
848 if (!vr)
f8fa9b4e 849 vr = mlxsw_sp_vr_create(mlxsw_sp, tb_id, extack);
6b75c480
JP
850 return vr;
851}
852
2b52ce02 853static void mlxsw_sp_vr_put(struct mlxsw_sp *mlxsw_sp, struct mlxsw_sp_vr *vr)
6b75c480 854{
a3d9bc50 855 if (!vr->rif_count && list_empty(&vr->fib4->node_list) &&
d42b0965
YG
856 list_empty(&vr->fib6->node_list) &&
857 mlxsw_sp_mr_table_empty(vr->mr4_table))
2b52ce02 858 mlxsw_sp_vr_destroy(mlxsw_sp, vr);
6b75c480
JP
859}
860
fc922bb0
IS
861static bool
862mlxsw_sp_vr_lpm_tree_should_replace(struct mlxsw_sp_vr *vr,
863 enum mlxsw_sp_l3proto proto, u8 tree_id)
864{
865 struct mlxsw_sp_fib *fib = mlxsw_sp_vr_fib(vr, proto);
866
867 if (!mlxsw_sp_vr_is_used(vr))
868 return false;
2b52ce02 869 if (fib->lpm_tree->id == tree_id)
fc922bb0
IS
870 return true;
871 return false;
872}
873
874static int mlxsw_sp_vr_lpm_tree_replace(struct mlxsw_sp *mlxsw_sp,
875 struct mlxsw_sp_fib *fib,
876 struct mlxsw_sp_lpm_tree *new_tree)
877{
878 struct mlxsw_sp_lpm_tree *old_tree = fib->lpm_tree;
879 int err;
880
fc922bb0
IS
881 fib->lpm_tree = new_tree;
882 mlxsw_sp_lpm_tree_hold(new_tree);
ed604c5d
IS
883 err = mlxsw_sp_vr_lpm_tree_bind(mlxsw_sp, fib, new_tree->id);
884 if (err)
885 goto err_tree_bind;
fc922bb0
IS
886 mlxsw_sp_lpm_tree_put(mlxsw_sp, old_tree);
887 return 0;
ed604c5d
IS
888
889err_tree_bind:
890 mlxsw_sp_lpm_tree_put(mlxsw_sp, new_tree);
891 fib->lpm_tree = old_tree;
892 return err;
fc922bb0
IS
893}
894
895static int mlxsw_sp_vrs_lpm_tree_replace(struct mlxsw_sp *mlxsw_sp,
896 struct mlxsw_sp_fib *fib,
897 struct mlxsw_sp_lpm_tree *new_tree)
898{
fc922bb0 899 enum mlxsw_sp_l3proto proto = fib->proto;
2b52ce02 900 struct mlxsw_sp_lpm_tree *old_tree;
fc922bb0
IS
901 u8 old_id, new_id = new_tree->id;
902 struct mlxsw_sp_vr *vr;
903 int i, err;
904
2b52ce02 905 old_tree = mlxsw_sp->router->lpm.proto_trees[proto];
fc922bb0
IS
906 old_id = old_tree->id;
907
908 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
909 vr = &mlxsw_sp->router->vrs[i];
910 if (!mlxsw_sp_vr_lpm_tree_should_replace(vr, proto, old_id))
911 continue;
912 err = mlxsw_sp_vr_lpm_tree_replace(mlxsw_sp,
913 mlxsw_sp_vr_fib(vr, proto),
914 new_tree);
915 if (err)
916 goto err_tree_replace;
917 }
918
2b52ce02
IS
919 memcpy(new_tree->prefix_ref_count, old_tree->prefix_ref_count,
920 sizeof(new_tree->prefix_ref_count));
921 mlxsw_sp->router->lpm.proto_trees[proto] = new_tree;
922 mlxsw_sp_lpm_tree_put(mlxsw_sp, old_tree);
923
fc922bb0
IS
924 return 0;
925
926err_tree_replace:
927 for (i--; i >= 0; i--) {
928 if (!mlxsw_sp_vr_lpm_tree_should_replace(vr, proto, new_id))
929 continue;
930 mlxsw_sp_vr_lpm_tree_replace(mlxsw_sp,
931 mlxsw_sp_vr_fib(vr, proto),
932 old_tree);
933 }
934 return err;
fc922bb0
IS
935}
936
9497c042 937static int mlxsw_sp_vrs_init(struct mlxsw_sp *mlxsw_sp)
6b75c480
JP
938{
939 struct mlxsw_sp_vr *vr;
c1a38311 940 u64 max_vrs;
6b75c480
JP
941 int i;
942
c1a38311 943 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_VRS))
9497c042
NF
944 return -EIO;
945
c1a38311 946 max_vrs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS);
9011b677
IS
947 mlxsw_sp->router->vrs = kcalloc(max_vrs, sizeof(struct mlxsw_sp_vr),
948 GFP_KERNEL);
949 if (!mlxsw_sp->router->vrs)
9497c042
NF
950 return -ENOMEM;
951
c1a38311 952 for (i = 0; i < max_vrs; i++) {
9011b677 953 vr = &mlxsw_sp->router->vrs[i];
6b75c480
JP
954 vr->id = i;
955 }
9497c042
NF
956
957 return 0;
958}
959
ac571de9
IS
960static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp);
961
9497c042
NF
962static void mlxsw_sp_vrs_fini(struct mlxsw_sp *mlxsw_sp)
963{
3057224e
IS
964 /* At this stage we're guaranteed not to have new incoming
965 * FIB notifications and the work queue is free from FIBs
966 * sitting on top of mlxsw netdevs. However, we can still
967 * have other FIBs queued. Flush the queue before flushing
968 * the device's tables. No need for locks, as we're the only
969 * writer.
970 */
971 mlxsw_core_flush_owq();
ac571de9 972 mlxsw_sp_router_fib_flush(mlxsw_sp);
9011b677 973 kfree(mlxsw_sp->router->vrs);
6b75c480
JP
974}
975
6ddb7426
PM
976static struct net_device *
977__mlxsw_sp_ipip_netdev_ul_dev_get(const struct net_device *ol_dev)
978{
979 struct ip_tunnel *tun = netdev_priv(ol_dev);
980 struct net *net = dev_net(ol_dev);
981
982 return __dev_get_by_index(net, tun->parms.link);
983}
984
4cf04f3f 985u32 mlxsw_sp_ipip_dev_ul_tb_id(const struct net_device *ol_dev)
6ddb7426
PM
986{
987 struct net_device *d = __mlxsw_sp_ipip_netdev_ul_dev_get(ol_dev);
988
989 if (d)
990 return l3mdev_fib_table(d) ? : RT_TABLE_MAIN;
991 else
992 return l3mdev_fib_table(ol_dev) ? : RT_TABLE_MAIN;
993}
994
1012b9ac
PM
995static struct mlxsw_sp_rif *
996mlxsw_sp_rif_create(struct mlxsw_sp *mlxsw_sp,
f8fa9b4e
DA
997 const struct mlxsw_sp_rif_params *params,
998 struct netlink_ext_ack *extack);
1012b9ac
PM
999
1000static struct mlxsw_sp_rif_ipip_lb *
1001mlxsw_sp_ipip_ol_ipip_lb_create(struct mlxsw_sp *mlxsw_sp,
1002 enum mlxsw_sp_ipip_type ipipt,
7e75af63
PM
1003 struct net_device *ol_dev,
1004 struct netlink_ext_ack *extack)
1012b9ac
PM
1005{
1006 struct mlxsw_sp_rif_params_ipip_lb lb_params;
1007 const struct mlxsw_sp_ipip_ops *ipip_ops;
1008 struct mlxsw_sp_rif *rif;
1009
1010 ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipipt];
1011 lb_params = (struct mlxsw_sp_rif_params_ipip_lb) {
1012 .common.dev = ol_dev,
1013 .common.lag = false,
1014 .lb_config = ipip_ops->ol_loopback_config(mlxsw_sp, ol_dev),
1015 };
1016
7e75af63 1017 rif = mlxsw_sp_rif_create(mlxsw_sp, &lb_params.common, extack);
1012b9ac
PM
1018 if (IS_ERR(rif))
1019 return ERR_CAST(rif);
1020 return container_of(rif, struct mlxsw_sp_rif_ipip_lb, common);
1021}
1022
1023static struct mlxsw_sp_ipip_entry *
1024mlxsw_sp_ipip_entry_alloc(struct mlxsw_sp *mlxsw_sp,
1025 enum mlxsw_sp_ipip_type ipipt,
1026 struct net_device *ol_dev)
1027{
e437f3b6 1028 const struct mlxsw_sp_ipip_ops *ipip_ops;
1012b9ac
PM
1029 struct mlxsw_sp_ipip_entry *ipip_entry;
1030 struct mlxsw_sp_ipip_entry *ret = NULL;
1031
e437f3b6 1032 ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipipt];
1012b9ac
PM
1033 ipip_entry = kzalloc(sizeof(*ipip_entry), GFP_KERNEL);
1034 if (!ipip_entry)
1035 return ERR_PTR(-ENOMEM);
1036
1037 ipip_entry->ol_lb = mlxsw_sp_ipip_ol_ipip_lb_create(mlxsw_sp, ipipt,
7e75af63 1038 ol_dev, NULL);
1012b9ac
PM
1039 if (IS_ERR(ipip_entry->ol_lb)) {
1040 ret = ERR_CAST(ipip_entry->ol_lb);
1041 goto err_ol_ipip_lb_create;
1042 }
1043
1044 ipip_entry->ipipt = ipipt;
1045 ipip_entry->ol_dev = ol_dev;
e437f3b6
PM
1046
1047 switch (ipip_ops->ul_proto) {
1048 case MLXSW_SP_L3_PROTO_IPV4:
1049 ipip_entry->parms4 = mlxsw_sp_ipip_netdev_parms4(ol_dev);
1050 break;
1051 case MLXSW_SP_L3_PROTO_IPV6:
1052 WARN_ON(1);
1053 break;
1054 }
1012b9ac
PM
1055
1056 return ipip_entry;
1057
1058err_ol_ipip_lb_create:
1059 kfree(ipip_entry);
1060 return ret;
1061}
1062
1063static void
4cccb737 1064mlxsw_sp_ipip_entry_dealloc(struct mlxsw_sp_ipip_entry *ipip_entry)
1012b9ac 1065{
1012b9ac
PM
1066 mlxsw_sp_rif_destroy(&ipip_entry->ol_lb->common);
1067 kfree(ipip_entry);
1068}
1069
1012b9ac
PM
1070static bool
1071mlxsw_sp_ipip_entry_saddr_matches(struct mlxsw_sp *mlxsw_sp,
1072 const enum mlxsw_sp_l3proto ul_proto,
1073 union mlxsw_sp_l3addr saddr,
1074 u32 ul_tb_id,
1075 struct mlxsw_sp_ipip_entry *ipip_entry)
1076{
1077 u32 tun_ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(ipip_entry->ol_dev);
1078 enum mlxsw_sp_ipip_type ipipt = ipip_entry->ipipt;
1079 union mlxsw_sp_l3addr tun_saddr;
1080
1081 if (mlxsw_sp->router->ipip_ops_arr[ipipt]->ul_proto != ul_proto)
1082 return false;
1083
1084 tun_saddr = mlxsw_sp_ipip_netdev_saddr(ul_proto, ipip_entry->ol_dev);
1085 return tun_ul_tb_id == ul_tb_id &&
1086 mlxsw_sp_l3addr_eq(&tun_saddr, &saddr);
1087}
1088
4607f6d2
PM
1089static int
1090mlxsw_sp_fib_entry_decap_init(struct mlxsw_sp *mlxsw_sp,
1091 struct mlxsw_sp_fib_entry *fib_entry,
1092 struct mlxsw_sp_ipip_entry *ipip_entry)
1093{
1094 u32 tunnel_index;
1095 int err;
1096
1097 err = mlxsw_sp_kvdl_alloc(mlxsw_sp, 1, &tunnel_index);
1098 if (err)
1099 return err;
1100
1101 ipip_entry->decap_fib_entry = fib_entry;
1102 fib_entry->decap.ipip_entry = ipip_entry;
1103 fib_entry->decap.tunnel_index = tunnel_index;
1104 return 0;
1105}
1106
1107static void mlxsw_sp_fib_entry_decap_fini(struct mlxsw_sp *mlxsw_sp,
1108 struct mlxsw_sp_fib_entry *fib_entry)
1109{
1110 /* Unlink this node from the IPIP entry that it's the decap entry of. */
1111 fib_entry->decap.ipip_entry->decap_fib_entry = NULL;
1112 fib_entry->decap.ipip_entry = NULL;
1113 mlxsw_sp_kvdl_free(mlxsw_sp, fib_entry->decap.tunnel_index);
1114}
1115
1cc38fb1
PM
1116static struct mlxsw_sp_fib_node *
1117mlxsw_sp_fib_node_lookup(struct mlxsw_sp_fib *fib, const void *addr,
1118 size_t addr_len, unsigned char prefix_len);
4607f6d2
PM
1119static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
1120 struct mlxsw_sp_fib_entry *fib_entry);
1121
1122static void
1123mlxsw_sp_ipip_entry_demote_decap(struct mlxsw_sp *mlxsw_sp,
1124 struct mlxsw_sp_ipip_entry *ipip_entry)
1125{
1126 struct mlxsw_sp_fib_entry *fib_entry = ipip_entry->decap_fib_entry;
1127
1128 mlxsw_sp_fib_entry_decap_fini(mlxsw_sp, fib_entry);
1129 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
1130
1131 mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
1132}
1133
1cc38fb1
PM
1134static void
1135mlxsw_sp_ipip_entry_promote_decap(struct mlxsw_sp *mlxsw_sp,
1136 struct mlxsw_sp_ipip_entry *ipip_entry,
1137 struct mlxsw_sp_fib_entry *decap_fib_entry)
1138{
1139 if (mlxsw_sp_fib_entry_decap_init(mlxsw_sp, decap_fib_entry,
1140 ipip_entry))
1141 return;
1142 decap_fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP;
1143
1144 if (mlxsw_sp_fib_entry_update(mlxsw_sp, decap_fib_entry))
1145 mlxsw_sp_ipip_entry_demote_decap(mlxsw_sp, ipip_entry);
1146}
1147
1148/* Given an IPIP entry, find the corresponding decap route. */
1149static struct mlxsw_sp_fib_entry *
1150mlxsw_sp_ipip_entry_find_decap(struct mlxsw_sp *mlxsw_sp,
1151 struct mlxsw_sp_ipip_entry *ipip_entry)
1152{
1153 static struct mlxsw_sp_fib_node *fib_node;
1154 const struct mlxsw_sp_ipip_ops *ipip_ops;
1155 struct mlxsw_sp_fib_entry *fib_entry;
1156 unsigned char saddr_prefix_len;
1157 union mlxsw_sp_l3addr saddr;
1158 struct mlxsw_sp_fib *ul_fib;
1159 struct mlxsw_sp_vr *ul_vr;
1160 const void *saddrp;
1161 size_t saddr_len;
1162 u32 ul_tb_id;
1163 u32 saddr4;
1164
1165 ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt];
1166
1167 ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(ipip_entry->ol_dev);
1168 ul_vr = mlxsw_sp_vr_find(mlxsw_sp, ul_tb_id);
1169 if (!ul_vr)
1170 return NULL;
1171
1172 ul_fib = mlxsw_sp_vr_fib(ul_vr, ipip_ops->ul_proto);
1173 saddr = mlxsw_sp_ipip_netdev_saddr(ipip_ops->ul_proto,
1174 ipip_entry->ol_dev);
1175
1176 switch (ipip_ops->ul_proto) {
1177 case MLXSW_SP_L3_PROTO_IPV4:
1178 saddr4 = be32_to_cpu(saddr.addr4);
1179 saddrp = &saddr4;
1180 saddr_len = 4;
1181 saddr_prefix_len = 32;
1182 break;
1183 case MLXSW_SP_L3_PROTO_IPV6:
1184 WARN_ON(1);
1185 return NULL;
1186 }
1187
1188 fib_node = mlxsw_sp_fib_node_lookup(ul_fib, saddrp, saddr_len,
1189 saddr_prefix_len);
1190 if (!fib_node || list_empty(&fib_node->entry_list))
1191 return NULL;
1192
1193 fib_entry = list_first_entry(&fib_node->entry_list,
1194 struct mlxsw_sp_fib_entry, list);
1195 if (fib_entry->type != MLXSW_SP_FIB_ENTRY_TYPE_TRAP)
1196 return NULL;
1197
1198 return fib_entry;
1199}
1200
1012b9ac 1201static struct mlxsw_sp_ipip_entry *
4cccb737
PM
1202mlxsw_sp_ipip_entry_create(struct mlxsw_sp *mlxsw_sp,
1203 enum mlxsw_sp_ipip_type ipipt,
1204 struct net_device *ol_dev)
1012b9ac 1205{
1012b9ac 1206 struct mlxsw_sp_ipip_entry *ipip_entry;
1012b9ac
PM
1207
1208 ipip_entry = mlxsw_sp_ipip_entry_alloc(mlxsw_sp, ipipt, ol_dev);
1209 if (IS_ERR(ipip_entry))
1210 return ipip_entry;
1211
1212 list_add_tail(&ipip_entry->ipip_list_node,
1213 &mlxsw_sp->router->ipip_list);
1214
1012b9ac
PM
1215 return ipip_entry;
1216}
1217
1218static void
4cccb737
PM
1219mlxsw_sp_ipip_entry_destroy(struct mlxsw_sp *mlxsw_sp,
1220 struct mlxsw_sp_ipip_entry *ipip_entry)
1012b9ac 1221{
4cccb737
PM
1222 list_del(&ipip_entry->ipip_list_node);
1223 mlxsw_sp_ipip_entry_dealloc(ipip_entry);
1012b9ac
PM
1224}
1225
4607f6d2
PM
1226static bool
1227mlxsw_sp_ipip_entry_matches_decap(struct mlxsw_sp *mlxsw_sp,
1228 const struct net_device *ul_dev,
1229 enum mlxsw_sp_l3proto ul_proto,
1230 union mlxsw_sp_l3addr ul_dip,
1231 struct mlxsw_sp_ipip_entry *ipip_entry)
1232{
1233 u32 ul_tb_id = l3mdev_fib_table(ul_dev) ? : RT_TABLE_MAIN;
1234 enum mlxsw_sp_ipip_type ipipt = ipip_entry->ipipt;
1235 struct net_device *ipip_ul_dev;
1236
1237 if (mlxsw_sp->router->ipip_ops_arr[ipipt]->ul_proto != ul_proto)
1238 return false;
1239
1240 ipip_ul_dev = __mlxsw_sp_ipip_netdev_ul_dev_get(ipip_entry->ol_dev);
1241 return mlxsw_sp_ipip_entry_saddr_matches(mlxsw_sp, ul_proto, ul_dip,
1242 ul_tb_id, ipip_entry) &&
1243 (!ipip_ul_dev || ipip_ul_dev == ul_dev);
1244}
1245
1246/* Given decap parameters, find the corresponding IPIP entry. */
1247static struct mlxsw_sp_ipip_entry *
1248mlxsw_sp_ipip_entry_find_by_decap(struct mlxsw_sp *mlxsw_sp,
1249 const struct net_device *ul_dev,
1250 enum mlxsw_sp_l3proto ul_proto,
1251 union mlxsw_sp_l3addr ul_dip)
1252{
1253 struct mlxsw_sp_ipip_entry *ipip_entry;
1254
1255 list_for_each_entry(ipip_entry, &mlxsw_sp->router->ipip_list,
1256 ipip_list_node)
1257 if (mlxsw_sp_ipip_entry_matches_decap(mlxsw_sp, ul_dev,
1258 ul_proto, ul_dip,
1259 ipip_entry))
1260 return ipip_entry;
1261
1262 return NULL;
1263}
1264
6698c168
PM
1265static bool mlxsw_sp_netdev_ipip_type(const struct mlxsw_sp *mlxsw_sp,
1266 const struct net_device *dev,
1267 enum mlxsw_sp_ipip_type *p_type)
1268{
1269 struct mlxsw_sp_router *router = mlxsw_sp->router;
1270 const struct mlxsw_sp_ipip_ops *ipip_ops;
1271 enum mlxsw_sp_ipip_type ipipt;
1272
1273 for (ipipt = 0; ipipt < MLXSW_SP_IPIP_TYPE_MAX; ++ipipt) {
1274 ipip_ops = router->ipip_ops_arr[ipipt];
1275 if (dev->type == ipip_ops->dev_type) {
1276 if (p_type)
1277 *p_type = ipipt;
1278 return true;
1279 }
1280 }
1281 return false;
1282}
1283
796ec776
PM
1284bool mlxsw_sp_netdev_is_ipip_ol(const struct mlxsw_sp *mlxsw_sp,
1285 const struct net_device *dev)
0063587d
PM
1286{
1287 return mlxsw_sp_netdev_ipip_type(mlxsw_sp, dev, NULL);
1288}
1289
1290static struct mlxsw_sp_ipip_entry *
1291mlxsw_sp_ipip_entry_find_by_ol_dev(struct mlxsw_sp *mlxsw_sp,
1292 const struct net_device *ol_dev)
1293{
1294 struct mlxsw_sp_ipip_entry *ipip_entry;
1295
1296 list_for_each_entry(ipip_entry, &mlxsw_sp->router->ipip_list,
1297 ipip_list_node)
1298 if (ipip_entry->ol_dev == ol_dev)
1299 return ipip_entry;
1300
1301 return NULL;
1302}
1303
61481f2f
PM
1304static struct mlxsw_sp_ipip_entry *
1305mlxsw_sp_ipip_entry_find_by_ul_dev(const struct mlxsw_sp *mlxsw_sp,
1306 const struct net_device *ul_dev,
1307 struct mlxsw_sp_ipip_entry *start)
1308{
1309 struct mlxsw_sp_ipip_entry *ipip_entry;
1310
1311 ipip_entry = list_prepare_entry(start, &mlxsw_sp->router->ipip_list,
1312 ipip_list_node);
1313 list_for_each_entry_continue(ipip_entry, &mlxsw_sp->router->ipip_list,
1314 ipip_list_node) {
1315 struct net_device *ipip_ul_dev =
1316 __mlxsw_sp_ipip_netdev_ul_dev_get(ipip_entry->ol_dev);
1317
1318 if (ipip_ul_dev == ul_dev)
1319 return ipip_entry;
1320 }
1321
1322 return NULL;
1323}
1324
1325bool mlxsw_sp_netdev_is_ipip_ul(const struct mlxsw_sp *mlxsw_sp,
1326 const struct net_device *dev)
1327{
1328 return mlxsw_sp_ipip_entry_find_by_ul_dev(mlxsw_sp, dev, NULL);
1329}
1330
cafdb2a0
PM
1331static bool mlxsw_sp_netdevice_ipip_can_offload(struct mlxsw_sp *mlxsw_sp,
1332 const struct net_device *ol_dev,
1333 enum mlxsw_sp_ipip_type ipipt)
1334{
1335 const struct mlxsw_sp_ipip_ops *ops
1336 = mlxsw_sp->router->ipip_ops_arr[ipipt];
1337
1338 /* For deciding whether decap should be offloaded, we don't care about
1339 * overlay protocol, so ask whether either one is supported.
1340 */
1341 return ops->can_offload(mlxsw_sp, ol_dev, MLXSW_SP_L3_PROTO_IPV4) ||
1342 ops->can_offload(mlxsw_sp, ol_dev, MLXSW_SP_L3_PROTO_IPV6);
1343}
1344
796ec776
PM
1345static int mlxsw_sp_netdevice_ipip_ol_reg_event(struct mlxsw_sp *mlxsw_sp,
1346 struct net_device *ol_dev)
0063587d 1347{
0063587d 1348 struct mlxsw_sp_ipip_entry *ipip_entry;
af641713 1349 enum mlxsw_sp_l3proto ul_proto;
0063587d 1350 enum mlxsw_sp_ipip_type ipipt;
af641713
PM
1351 union mlxsw_sp_l3addr saddr;
1352 u32 ul_tb_id;
0063587d
PM
1353
1354 mlxsw_sp_netdev_ipip_type(mlxsw_sp, ol_dev, &ipipt);
cafdb2a0 1355 if (mlxsw_sp_netdevice_ipip_can_offload(mlxsw_sp, ol_dev, ipipt)) {
af641713
PM
1356 ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(ol_dev);
1357 ul_proto = mlxsw_sp->router->ipip_ops_arr[ipipt]->ul_proto;
1358 saddr = mlxsw_sp_ipip_netdev_saddr(ul_proto, ol_dev);
1359 if (!mlxsw_sp_ipip_demote_tunnel_by_saddr(mlxsw_sp, ul_proto,
1360 saddr, ul_tb_id,
1361 NULL)) {
1362 ipip_entry = mlxsw_sp_ipip_entry_create(mlxsw_sp, ipipt,
1363 ol_dev);
1364 if (IS_ERR(ipip_entry))
1365 return PTR_ERR(ipip_entry);
1366 }
0063587d
PM
1367 }
1368
1369 return 0;
1370}
1371
796ec776
PM
1372static void mlxsw_sp_netdevice_ipip_ol_unreg_event(struct mlxsw_sp *mlxsw_sp,
1373 struct net_device *ol_dev)
0063587d
PM
1374{
1375 struct mlxsw_sp_ipip_entry *ipip_entry;
1376
1377 ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
1378 if (ipip_entry)
4cccb737 1379 mlxsw_sp_ipip_entry_destroy(mlxsw_sp, ipip_entry);
0063587d
PM
1380}
1381
47518ca5
PM
1382static void
1383mlxsw_sp_ipip_entry_ol_up_event(struct mlxsw_sp *mlxsw_sp,
1384 struct mlxsw_sp_ipip_entry *ipip_entry)
1385{
1386 struct mlxsw_sp_fib_entry *decap_fib_entry;
1387
1388 decap_fib_entry = mlxsw_sp_ipip_entry_find_decap(mlxsw_sp, ipip_entry);
1389 if (decap_fib_entry)
1390 mlxsw_sp_ipip_entry_promote_decap(mlxsw_sp, ipip_entry,
1391 decap_fib_entry);
1392}
1393
6d4de445
PM
1394static void mlxsw_sp_netdevice_ipip_ol_up_event(struct mlxsw_sp *mlxsw_sp,
1395 struct net_device *ol_dev)
0063587d 1396{
0063587d
PM
1397 struct mlxsw_sp_ipip_entry *ipip_entry;
1398
1399 ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
47518ca5
PM
1400 if (ipip_entry)
1401 mlxsw_sp_ipip_entry_ol_up_event(mlxsw_sp, ipip_entry);
0063587d
PM
1402}
1403
a3fe198e
PM
1404static void
1405mlxsw_sp_ipip_entry_ol_down_event(struct mlxsw_sp *mlxsw_sp,
1406 struct mlxsw_sp_ipip_entry *ipip_entry)
1407{
1408 if (ipip_entry->decap_fib_entry)
1409 mlxsw_sp_ipip_entry_demote_decap(mlxsw_sp, ipip_entry);
1410}
1411
796ec776
PM
1412static void mlxsw_sp_netdevice_ipip_ol_down_event(struct mlxsw_sp *mlxsw_sp,
1413 struct net_device *ol_dev)
0063587d
PM
1414{
1415 struct mlxsw_sp_ipip_entry *ipip_entry;
1416
1417 ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
a3fe198e
PM
1418 if (ipip_entry)
1419 mlxsw_sp_ipip_entry_ol_down_event(mlxsw_sp, ipip_entry);
0063587d
PM
1420}
1421
09dbf629
PM
1422static void mlxsw_sp_nexthop_rif_migrate(struct mlxsw_sp *mlxsw_sp,
1423 struct mlxsw_sp_rif *old_rif,
1424 struct mlxsw_sp_rif *new_rif);
65a6121b
PM
1425static int
1426mlxsw_sp_ipip_entry_ol_lb_update(struct mlxsw_sp *mlxsw_sp,
1427 struct mlxsw_sp_ipip_entry *ipip_entry,
0c5f1cd5 1428 bool keep_encap,
65a6121b
PM
1429 struct netlink_ext_ack *extack)
1430{
1431 struct mlxsw_sp_rif_ipip_lb *old_lb_rif = ipip_entry->ol_lb;
1432 struct mlxsw_sp_rif_ipip_lb *new_lb_rif;
1433
1434 new_lb_rif = mlxsw_sp_ipip_ol_ipip_lb_create(mlxsw_sp,
1435 ipip_entry->ipipt,
1436 ipip_entry->ol_dev,
1437 extack);
1438 if (IS_ERR(new_lb_rif))
1439 return PTR_ERR(new_lb_rif);
1440 ipip_entry->ol_lb = new_lb_rif;
0c5f1cd5 1441
09dbf629
PM
1442 if (keep_encap)
1443 mlxsw_sp_nexthop_rif_migrate(mlxsw_sp, &old_lb_rif->common,
1444 &new_lb_rif->common);
0c5f1cd5 1445
65a6121b 1446 mlxsw_sp_rif_destroy(&old_lb_rif->common);
f63ce4e5 1447
65a6121b
PM
1448 return 0;
1449}
1450
09dbf629
PM
1451static void mlxsw_sp_nexthop_rif_update(struct mlxsw_sp *mlxsw_sp,
1452 struct mlxsw_sp_rif *rif);
1453
0c5f1cd5
PM
1454/**
1455 * Update the offload related to an IPIP entry. This always updates decap, and
1456 * in addition to that it also:
1457 * @recreate_loopback: recreates the associated loopback RIF
1458 * @keep_encap: updates next hops that use the tunnel netdevice. This is only
1459 * relevant when recreate_loopback is true.
1460 * @update_nexthops: updates next hops, keeping the current loopback RIF. This
1461 * is only relevant when recreate_loopback is false.
1462 */
65a6121b
PM
1463int __mlxsw_sp_ipip_entry_update_tunnel(struct mlxsw_sp *mlxsw_sp,
1464 struct mlxsw_sp_ipip_entry *ipip_entry,
0c5f1cd5
PM
1465 bool recreate_loopback,
1466 bool keep_encap,
1467 bool update_nexthops,
65a6121b
PM
1468 struct netlink_ext_ack *extack)
1469{
1470 int err;
f63ce4e5 1471
65a6121b
PM
1472 /* RIFs can't be edited, so to update loopback, we need to destroy and
1473 * recreate it. That creates a window of opportunity where RALUE and
1474 * RATR registers end up referencing a RIF that's already gone. RATRs
1475 * are handled in mlxsw_sp_ipip_entry_ol_lb_update(), and to take care
f63ce4e5
PM
1476 * of RALUE, demote the decap route back.
1477 */
1478 if (ipip_entry->decap_fib_entry)
1479 mlxsw_sp_ipip_entry_demote_decap(mlxsw_sp, ipip_entry);
1480
0c5f1cd5
PM
1481 if (recreate_loopback) {
1482 err = mlxsw_sp_ipip_entry_ol_lb_update(mlxsw_sp, ipip_entry,
1483 keep_encap, extack);
1484 if (err)
1485 return err;
1486 } else if (update_nexthops) {
1487 mlxsw_sp_nexthop_rif_update(mlxsw_sp,
1488 &ipip_entry->ol_lb->common);
1489 }
65a6121b
PM
1490
1491 if (ipip_entry->ol_dev->flags & IFF_UP)
1492 mlxsw_sp_ipip_entry_ol_up_event(mlxsw_sp, ipip_entry);
f63ce4e5
PM
1493
1494 return 0;
1495}
1496
65a6121b
PM
1497static int mlxsw_sp_netdevice_ipip_ol_vrf_event(struct mlxsw_sp *mlxsw_sp,
1498 struct net_device *ol_dev,
1499 struct netlink_ext_ack *extack)
1500{
1501 struct mlxsw_sp_ipip_entry *ipip_entry =
1502 mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
cab43d9c
PM
1503 enum mlxsw_sp_l3proto ul_proto;
1504 union mlxsw_sp_l3addr saddr;
1505 u32 ul_tb_id;
65a6121b
PM
1506
1507 if (!ipip_entry)
1508 return 0;
cab43d9c
PM
1509
1510 /* For flat configuration cases, moving overlay to a different VRF might
1511 * cause local address conflict, and the conflicting tunnels need to be
1512 * demoted.
1513 */
1514 ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(ol_dev);
1515 ul_proto = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt]->ul_proto;
1516 saddr = mlxsw_sp_ipip_netdev_saddr(ul_proto, ol_dev);
1517 if (mlxsw_sp_ipip_demote_tunnel_by_saddr(mlxsw_sp, ul_proto,
1518 saddr, ul_tb_id,
1519 ipip_entry)) {
1520 mlxsw_sp_ipip_entry_demote_tunnel(mlxsw_sp, ipip_entry);
1521 return 0;
1522 }
1523
65a6121b 1524 return __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
0c5f1cd5 1525 true, false, false, extack);
65a6121b
PM
1526}
1527
61481f2f
PM
1528static int
1529mlxsw_sp_netdevice_ipip_ul_vrf_event(struct mlxsw_sp *mlxsw_sp,
1530 struct mlxsw_sp_ipip_entry *ipip_entry,
1531 struct net_device *ul_dev,
1532 struct netlink_ext_ack *extack)
1533{
1534 return __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
1535 true, true, false, extack);
1536}
1537
44b0fff1
PM
1538static int
1539mlxsw_sp_netdevice_ipip_ul_up_event(struct mlxsw_sp *mlxsw_sp,
1540 struct mlxsw_sp_ipip_entry *ipip_entry,
1541 struct net_device *ul_dev)
1542{
1543 return __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
1544 false, false, true, NULL);
1545}
1546
1547static int
1548mlxsw_sp_netdevice_ipip_ul_down_event(struct mlxsw_sp *mlxsw_sp,
1549 struct mlxsw_sp_ipip_entry *ipip_entry,
1550 struct net_device *ul_dev)
1551{
1552 /* A down underlay device causes encapsulated packets to not be
1553 * forwarded, but decap still works. So refresh next hops without
1554 * touching anything else.
1555 */
1556 return __mlxsw_sp_ipip_entry_update_tunnel(mlxsw_sp, ipip_entry,
1557 false, false, true, NULL);
1558}
1559
4cf04f3f
PM
1560static int
1561mlxsw_sp_netdevice_ipip_ol_change_event(struct mlxsw_sp *mlxsw_sp,
1562 struct net_device *ol_dev,
1563 struct netlink_ext_ack *extack)
1564{
1565 const struct mlxsw_sp_ipip_ops *ipip_ops;
1566 struct mlxsw_sp_ipip_entry *ipip_entry;
1567 int err;
1568
1569 ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, ol_dev);
1570 if (!ipip_entry)
1571 /* A change might make a tunnel eligible for offloading, but
1572 * that is currently not implemented. What falls to slow path
1573 * stays there.
1574 */
1575 return 0;
1576
1577 /* A change might make a tunnel not eligible for offloading. */
1578 if (!mlxsw_sp_netdevice_ipip_can_offload(mlxsw_sp, ol_dev,
1579 ipip_entry->ipipt)) {
1580 mlxsw_sp_ipip_entry_demote_tunnel(mlxsw_sp, ipip_entry);
1581 return 0;
1582 }
1583
1584 ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt];
1585 err = ipip_ops->ol_netdev_change(mlxsw_sp, ipip_entry, extack);
1586 return err;
1587}
1588
af641713
PM
1589void mlxsw_sp_ipip_entry_demote_tunnel(struct mlxsw_sp *mlxsw_sp,
1590 struct mlxsw_sp_ipip_entry *ipip_entry)
1591{
1592 struct net_device *ol_dev = ipip_entry->ol_dev;
1593
1594 if (ol_dev->flags & IFF_UP)
1595 mlxsw_sp_ipip_entry_ol_down_event(mlxsw_sp, ipip_entry);
1596 mlxsw_sp_ipip_entry_destroy(mlxsw_sp, ipip_entry);
1597}
1598
1599/* The configuration where several tunnels have the same local address in the
1600 * same underlay table needs special treatment in the HW. That is currently not
1601 * implemented in the driver. This function finds and demotes the first tunnel
1602 * with a given source address, except the one passed in in the argument
1603 * `except'.
1604 */
1605bool
1606mlxsw_sp_ipip_demote_tunnel_by_saddr(struct mlxsw_sp *mlxsw_sp,
1607 enum mlxsw_sp_l3proto ul_proto,
1608 union mlxsw_sp_l3addr saddr,
1609 u32 ul_tb_id,
1610 const struct mlxsw_sp_ipip_entry *except)
1611{
1612 struct mlxsw_sp_ipip_entry *ipip_entry, *tmp;
1613
1614 list_for_each_entry_safe(ipip_entry, tmp, &mlxsw_sp->router->ipip_list,
1615 ipip_list_node) {
1616 if (ipip_entry != except &&
1617 mlxsw_sp_ipip_entry_saddr_matches(mlxsw_sp, ul_proto, saddr,
1618 ul_tb_id, ipip_entry)) {
1619 mlxsw_sp_ipip_entry_demote_tunnel(mlxsw_sp, ipip_entry);
1620 return true;
1621 }
1622 }
1623
1624 return false;
1625}
1626
61481f2f
PM
1627static void mlxsw_sp_ipip_demote_tunnel_by_ul_netdev(struct mlxsw_sp *mlxsw_sp,
1628 struct net_device *ul_dev)
1629{
1630 struct mlxsw_sp_ipip_entry *ipip_entry, *tmp;
1631
1632 list_for_each_entry_safe(ipip_entry, tmp, &mlxsw_sp->router->ipip_list,
1633 ipip_list_node) {
1634 struct net_device *ipip_ul_dev =
1635 __mlxsw_sp_ipip_netdev_ul_dev_get(ipip_entry->ol_dev);
1636
1637 if (ipip_ul_dev == ul_dev)
1638 mlxsw_sp_ipip_entry_demote_tunnel(mlxsw_sp, ipip_entry);
1639 }
1640}
1641
7e75af63
PM
1642int mlxsw_sp_netdevice_ipip_ol_event(struct mlxsw_sp *mlxsw_sp,
1643 struct net_device *ol_dev,
1644 unsigned long event,
1645 struct netdev_notifier_info *info)
0063587d 1646{
7e75af63
PM
1647 struct netdev_notifier_changeupper_info *chup;
1648 struct netlink_ext_ack *extack;
1649
0063587d
PM
1650 switch (event) {
1651 case NETDEV_REGISTER:
796ec776 1652 return mlxsw_sp_netdevice_ipip_ol_reg_event(mlxsw_sp, ol_dev);
0063587d 1653 case NETDEV_UNREGISTER:
796ec776 1654 mlxsw_sp_netdevice_ipip_ol_unreg_event(mlxsw_sp, ol_dev);
0063587d
PM
1655 return 0;
1656 case NETDEV_UP:
6d4de445
PM
1657 mlxsw_sp_netdevice_ipip_ol_up_event(mlxsw_sp, ol_dev);
1658 return 0;
0063587d 1659 case NETDEV_DOWN:
796ec776 1660 mlxsw_sp_netdevice_ipip_ol_down_event(mlxsw_sp, ol_dev);
0063587d 1661 return 0;
f63ce4e5 1662 case NETDEV_CHANGEUPPER:
7e75af63
PM
1663 chup = container_of(info, typeof(*chup), info);
1664 extack = info->extack;
1665 if (netif_is_l3_master(chup->upper_dev))
796ec776 1666 return mlxsw_sp_netdevice_ipip_ol_vrf_event(mlxsw_sp,
7e75af63
PM
1667 ol_dev,
1668 extack);
f63ce4e5 1669 return 0;
4cf04f3f
PM
1670 case NETDEV_CHANGE:
1671 extack = info->extack;
1672 return mlxsw_sp_netdevice_ipip_ol_change_event(mlxsw_sp,
1673 ol_dev, extack);
0063587d
PM
1674 }
1675 return 0;
1676}
1677
61481f2f
PM
1678static int
1679__mlxsw_sp_netdevice_ipip_ul_event(struct mlxsw_sp *mlxsw_sp,
1680 struct mlxsw_sp_ipip_entry *ipip_entry,
1681 struct net_device *ul_dev,
1682 unsigned long event,
1683 struct netdev_notifier_info *info)
1684{
1685 struct netdev_notifier_changeupper_info *chup;
1686 struct netlink_ext_ack *extack;
1687
1688 switch (event) {
1689 case NETDEV_CHANGEUPPER:
1690 chup = container_of(info, typeof(*chup), info);
1691 extack = info->extack;
1692 if (netif_is_l3_master(chup->upper_dev))
1693 return mlxsw_sp_netdevice_ipip_ul_vrf_event(mlxsw_sp,
1694 ipip_entry,
1695 ul_dev,
1696 extack);
1697 break;
44b0fff1
PM
1698
1699 case NETDEV_UP:
1700 return mlxsw_sp_netdevice_ipip_ul_up_event(mlxsw_sp, ipip_entry,
1701 ul_dev);
1702 case NETDEV_DOWN:
1703 return mlxsw_sp_netdevice_ipip_ul_down_event(mlxsw_sp,
1704 ipip_entry,
1705 ul_dev);
61481f2f
PM
1706 }
1707 return 0;
1708}
1709
1710int
1711mlxsw_sp_netdevice_ipip_ul_event(struct mlxsw_sp *mlxsw_sp,
1712 struct net_device *ul_dev,
1713 unsigned long event,
1714 struct netdev_notifier_info *info)
1715{
1716 struct mlxsw_sp_ipip_entry *ipip_entry = NULL;
1717 int err;
1718
1719 while ((ipip_entry = mlxsw_sp_ipip_entry_find_by_ul_dev(mlxsw_sp,
1720 ul_dev,
1721 ipip_entry))) {
1722 err = __mlxsw_sp_netdevice_ipip_ul_event(mlxsw_sp, ipip_entry,
1723 ul_dev, event, info);
1724 if (err) {
1725 mlxsw_sp_ipip_demote_tunnel_by_ul_netdev(mlxsw_sp,
1726 ul_dev);
1727 return err;
1728 }
1729 }
1730
1731 return 0;
1732}
1733
6cf3c971 1734struct mlxsw_sp_neigh_key {
33b1341c 1735 struct neighbour *n;
6cf3c971
JP
1736};
1737
1738struct mlxsw_sp_neigh_entry {
9665b745 1739 struct list_head rif_list_node;
6cf3c971
JP
1740 struct rhash_head ht_node;
1741 struct mlxsw_sp_neigh_key key;
1742 u16 rif;
5c8802f1 1743 bool connected;
a6bf9e93 1744 unsigned char ha[ETH_ALEN];
a7ff87ac
JP
1745 struct list_head nexthop_list; /* list of nexthops using
1746 * this neigh entry
1747 */
b2157149 1748 struct list_head nexthop_neighs_list_node;
7cfcbc75
AS
1749 unsigned int counter_index;
1750 bool counter_valid;
6cf3c971
JP
1751};
1752
1753static const struct rhashtable_params mlxsw_sp_neigh_ht_params = {
1754 .key_offset = offsetof(struct mlxsw_sp_neigh_entry, key),
1755 .head_offset = offsetof(struct mlxsw_sp_neigh_entry, ht_node),
1756 .key_len = sizeof(struct mlxsw_sp_neigh_key),
1757};
1758
f17cc84d
AS
1759struct mlxsw_sp_neigh_entry *
1760mlxsw_sp_rif_neigh_next(struct mlxsw_sp_rif *rif,
1761 struct mlxsw_sp_neigh_entry *neigh_entry)
1762{
1763 if (!neigh_entry) {
1764 if (list_empty(&rif->neigh_list))
1765 return NULL;
1766 else
1767 return list_first_entry(&rif->neigh_list,
1768 typeof(*neigh_entry),
1769 rif_list_node);
1770 }
ec2437f4 1771 if (list_is_last(&neigh_entry->rif_list_node, &rif->neigh_list))
f17cc84d
AS
1772 return NULL;
1773 return list_next_entry(neigh_entry, rif_list_node);
1774}
1775
1776int mlxsw_sp_neigh_entry_type(struct mlxsw_sp_neigh_entry *neigh_entry)
1777{
1778 return neigh_entry->key.n->tbl->family;
1779}
1780
1781unsigned char *
1782mlxsw_sp_neigh_entry_ha(struct mlxsw_sp_neigh_entry *neigh_entry)
1783{
1784 return neigh_entry->ha;
1785}
1786
1787u32 mlxsw_sp_neigh4_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry)
1788{
1789 struct neighbour *n;
1790
1791 n = neigh_entry->key.n;
1792 return ntohl(*((__be32 *) n->primary_key));
1793}
1794
0250768c
AS
1795struct in6_addr *
1796mlxsw_sp_neigh6_entry_dip(struct mlxsw_sp_neigh_entry *neigh_entry)
1797{
1798 struct neighbour *n;
1799
1800 n = neigh_entry->key.n;
1801 return (struct in6_addr *) &n->primary_key;
1802}
1803
7cfcbc75
AS
1804int mlxsw_sp_neigh_counter_get(struct mlxsw_sp *mlxsw_sp,
1805 struct mlxsw_sp_neigh_entry *neigh_entry,
1806 u64 *p_counter)
1807{
1808 if (!neigh_entry->counter_valid)
1809 return -EINVAL;
1810
1811 return mlxsw_sp_flow_counter_get(mlxsw_sp, neigh_entry->counter_index,
1812 p_counter, NULL);
1813}
1814
6cf3c971 1815static struct mlxsw_sp_neigh_entry *
5c8802f1
IS
1816mlxsw_sp_neigh_entry_alloc(struct mlxsw_sp *mlxsw_sp, struct neighbour *n,
1817 u16 rif)
6cf3c971
JP
1818{
1819 struct mlxsw_sp_neigh_entry *neigh_entry;
1820
5c8802f1 1821 neigh_entry = kzalloc(sizeof(*neigh_entry), GFP_KERNEL);
6cf3c971
JP
1822 if (!neigh_entry)
1823 return NULL;
5c8802f1 1824
33b1341c 1825 neigh_entry->key.n = n;
6cf3c971 1826 neigh_entry->rif = rif;
a7ff87ac 1827 INIT_LIST_HEAD(&neigh_entry->nexthop_list);
5c8802f1 1828
6cf3c971
JP
1829 return neigh_entry;
1830}
1831
5c8802f1 1832static void mlxsw_sp_neigh_entry_free(struct mlxsw_sp_neigh_entry *neigh_entry)
6cf3c971
JP
1833{
1834 kfree(neigh_entry);
1835}
1836
5c8802f1
IS
1837static int
1838mlxsw_sp_neigh_entry_insert(struct mlxsw_sp *mlxsw_sp,
1839 struct mlxsw_sp_neigh_entry *neigh_entry)
6cf3c971 1840{
9011b677 1841 return rhashtable_insert_fast(&mlxsw_sp->router->neigh_ht,
5c8802f1
IS
1842 &neigh_entry->ht_node,
1843 mlxsw_sp_neigh_ht_params);
1844}
6cf3c971 1845
5c8802f1
IS
1846static void
1847mlxsw_sp_neigh_entry_remove(struct mlxsw_sp *mlxsw_sp,
1848 struct mlxsw_sp_neigh_entry *neigh_entry)
1849{
9011b677 1850 rhashtable_remove_fast(&mlxsw_sp->router->neigh_ht,
5c8802f1
IS
1851 &neigh_entry->ht_node,
1852 mlxsw_sp_neigh_ht_params);
6cf3c971
JP
1853}
1854
7cfcbc75 1855static bool
1ed5574c
AS
1856mlxsw_sp_neigh_counter_should_alloc(struct mlxsw_sp *mlxsw_sp,
1857 struct mlxsw_sp_neigh_entry *neigh_entry)
7cfcbc75
AS
1858{
1859 struct devlink *devlink;
1ed5574c
AS
1860 const char *table_name;
1861
1862 switch (mlxsw_sp_neigh_entry_type(neigh_entry)) {
1863 case AF_INET:
1864 table_name = MLXSW_SP_DPIPE_TABLE_NAME_HOST4;
1865 break;
1866 case AF_INET6:
1867 table_name = MLXSW_SP_DPIPE_TABLE_NAME_HOST6;
1868 break;
1869 default:
1870 WARN_ON(1);
1871 return false;
1872 }
7cfcbc75
AS
1873
1874 devlink = priv_to_devlink(mlxsw_sp->core);
1ed5574c 1875 return devlink_dpipe_table_counter_enabled(devlink, table_name);
7cfcbc75
AS
1876}
1877
1878static void
1879mlxsw_sp_neigh_counter_alloc(struct mlxsw_sp *mlxsw_sp,
1880 struct mlxsw_sp_neigh_entry *neigh_entry)
1881{
1ed5574c 1882 if (!mlxsw_sp_neigh_counter_should_alloc(mlxsw_sp, neigh_entry))
7cfcbc75
AS
1883 return;
1884
1885 if (mlxsw_sp_flow_counter_alloc(mlxsw_sp, &neigh_entry->counter_index))
1886 return;
1887
1888 neigh_entry->counter_valid = true;
1889}
1890
1891static void
1892mlxsw_sp_neigh_counter_free(struct mlxsw_sp *mlxsw_sp,
1893 struct mlxsw_sp_neigh_entry *neigh_entry)
1894{
1895 if (!neigh_entry->counter_valid)
1896 return;
1897 mlxsw_sp_flow_counter_free(mlxsw_sp,
1898 neigh_entry->counter_index);
1899 neigh_entry->counter_valid = false;
1900}
1901
5c8802f1
IS
1902static struct mlxsw_sp_neigh_entry *
1903mlxsw_sp_neigh_entry_create(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
6cf3c971 1904{
6cf3c971 1905 struct mlxsw_sp_neigh_entry *neigh_entry;
bf95233e 1906 struct mlxsw_sp_rif *rif;
6cf3c971
JP
1907 int err;
1908
bf95233e
AS
1909 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, n->dev);
1910 if (!rif)
5c8802f1 1911 return ERR_PTR(-EINVAL);
6cf3c971 1912
bf95233e 1913 neigh_entry = mlxsw_sp_neigh_entry_alloc(mlxsw_sp, n, rif->rif_index);
6cf3c971 1914 if (!neigh_entry)
5c8802f1
IS
1915 return ERR_PTR(-ENOMEM);
1916
6cf3c971
JP
1917 err = mlxsw_sp_neigh_entry_insert(mlxsw_sp, neigh_entry);
1918 if (err)
1919 goto err_neigh_entry_insert;
5c8802f1 1920
7cfcbc75 1921 mlxsw_sp_neigh_counter_alloc(mlxsw_sp, neigh_entry);
bf95233e 1922 list_add(&neigh_entry->rif_list_node, &rif->neigh_list);
9665b745 1923
5c8802f1 1924 return neigh_entry;
6cf3c971
JP
1925
1926err_neigh_entry_insert:
5c8802f1
IS
1927 mlxsw_sp_neigh_entry_free(neigh_entry);
1928 return ERR_PTR(err);
6cf3c971
JP
1929}
1930
5c8802f1
IS
1931static void
1932mlxsw_sp_neigh_entry_destroy(struct mlxsw_sp *mlxsw_sp,
1933 struct mlxsw_sp_neigh_entry *neigh_entry)
6cf3c971 1934{
9665b745 1935 list_del(&neigh_entry->rif_list_node);
7cfcbc75 1936 mlxsw_sp_neigh_counter_free(mlxsw_sp, neigh_entry);
5c8802f1
IS
1937 mlxsw_sp_neigh_entry_remove(mlxsw_sp, neigh_entry);
1938 mlxsw_sp_neigh_entry_free(neigh_entry);
1939}
6cf3c971 1940
5c8802f1
IS
1941static struct mlxsw_sp_neigh_entry *
1942mlxsw_sp_neigh_entry_lookup(struct mlxsw_sp *mlxsw_sp, struct neighbour *n)
1943{
1944 struct mlxsw_sp_neigh_key key;
6cf3c971 1945
5c8802f1 1946 key.n = n;
9011b677 1947 return rhashtable_lookup_fast(&mlxsw_sp->router->neigh_ht,
5c8802f1 1948 &key, mlxsw_sp_neigh_ht_params);
6cf3c971
JP
1949}
1950
c723c735
YG
1951static void
1952mlxsw_sp_router_neighs_update_interval_init(struct mlxsw_sp *mlxsw_sp)
1953{
a6c9b5d1 1954 unsigned long interval;
c723c735 1955
b5f3e0d4 1956#if IS_ENABLED(CONFIG_IPV6)
a6c9b5d1
AS
1957 interval = min_t(unsigned long,
1958 NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME),
1959 NEIGH_VAR(&nd_tbl.parms, DELAY_PROBE_TIME));
b5f3e0d4
IS
1960#else
1961 interval = NEIGH_VAR(&arp_tbl.parms, DELAY_PROBE_TIME);
1962#endif
9011b677 1963 mlxsw_sp->router->neighs_update.interval = jiffies_to_msecs(interval);
c723c735
YG
1964}
1965
1966static void mlxsw_sp_router_neigh_ent_ipv4_process(struct mlxsw_sp *mlxsw_sp,
1967 char *rauhtd_pl,
1968 int ent_index)
1969{
1970 struct net_device *dev;
1971 struct neighbour *n;
1972 __be32 dipn;
1973 u32 dip;
1974 u16 rif;
1975
1976 mlxsw_reg_rauhtd_ent_ipv4_unpack(rauhtd_pl, ent_index, &rif, &dip);
1977
5f9efffb 1978 if (!mlxsw_sp->router->rifs[rif]) {
c723c735
YG
1979 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
1980 return;
1981 }
1982
1983 dipn = htonl(dip);
5f9efffb 1984 dev = mlxsw_sp->router->rifs[rif]->dev;
c723c735 1985 n = neigh_lookup(&arp_tbl, &dipn, dev);
1ecdaea0 1986 if (!n)
c723c735 1987 return;
c723c735
YG
1988
1989 netdev_dbg(dev, "Updating neighbour with IP=%pI4h\n", &dip);
1990 neigh_event_send(n, NULL);
1991 neigh_release(n);
1992}
1993
df9a21f1 1994#if IS_ENABLED(CONFIG_IPV6)
60f040ca
AS
1995static void mlxsw_sp_router_neigh_ent_ipv6_process(struct mlxsw_sp *mlxsw_sp,
1996 char *rauhtd_pl,
1997 int rec_index)
1998{
1999 struct net_device *dev;
2000 struct neighbour *n;
2001 struct in6_addr dip;
2002 u16 rif;
2003
2004 mlxsw_reg_rauhtd_ent_ipv6_unpack(rauhtd_pl, rec_index, &rif,
2005 (char *) &dip);
2006
2007 if (!mlxsw_sp->router->rifs[rif]) {
2008 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect RIF in neighbour entry\n");
2009 return;
2010 }
2011
2012 dev = mlxsw_sp->router->rifs[rif]->dev;
2013 n = neigh_lookup(&nd_tbl, &dip, dev);
1ecdaea0 2014 if (!n)
60f040ca 2015 return;
60f040ca
AS
2016
2017 netdev_dbg(dev, "Updating neighbour with IP=%pI6c\n", &dip);
2018 neigh_event_send(n, NULL);
2019 neigh_release(n);
2020}
b5f3e0d4
IS
2021#else
2022static void mlxsw_sp_router_neigh_ent_ipv6_process(struct mlxsw_sp *mlxsw_sp,
2023 char *rauhtd_pl,
2024 int rec_index)
2025{
2026}
2027#endif
60f040ca 2028
c723c735
YG
2029static void mlxsw_sp_router_neigh_rec_ipv4_process(struct mlxsw_sp *mlxsw_sp,
2030 char *rauhtd_pl,
2031 int rec_index)
2032{
2033 u8 num_entries;
2034 int i;
2035
2036 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
2037 rec_index);
2038 /* Hardware starts counting at 0, so add 1. */
2039 num_entries++;
2040
2041 /* Each record consists of several neighbour entries. */
2042 for (i = 0; i < num_entries; i++) {
2043 int ent_index;
2044
2045 ent_index = rec_index * MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC + i;
2046 mlxsw_sp_router_neigh_ent_ipv4_process(mlxsw_sp, rauhtd_pl,
2047 ent_index);
2048 }
2049
2050}
2051
60f040ca
AS
2052static void mlxsw_sp_router_neigh_rec_ipv6_process(struct mlxsw_sp *mlxsw_sp,
2053 char *rauhtd_pl,
2054 int rec_index)
2055{
2056 /* One record contains one entry. */
2057 mlxsw_sp_router_neigh_ent_ipv6_process(mlxsw_sp, rauhtd_pl,
2058 rec_index);
2059}
2060
c723c735
YG
2061static void mlxsw_sp_router_neigh_rec_process(struct mlxsw_sp *mlxsw_sp,
2062 char *rauhtd_pl, int rec_index)
2063{
2064 switch (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, rec_index)) {
2065 case MLXSW_REG_RAUHTD_TYPE_IPV4:
2066 mlxsw_sp_router_neigh_rec_ipv4_process(mlxsw_sp, rauhtd_pl,
2067 rec_index);
2068 break;
2069 case MLXSW_REG_RAUHTD_TYPE_IPV6:
60f040ca
AS
2070 mlxsw_sp_router_neigh_rec_ipv6_process(mlxsw_sp, rauhtd_pl,
2071 rec_index);
c723c735
YG
2072 break;
2073 }
2074}
2075
42cdb338
AS
2076static bool mlxsw_sp_router_rauhtd_is_full(char *rauhtd_pl)
2077{
2078 u8 num_rec, last_rec_index, num_entries;
2079
2080 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
2081 last_rec_index = num_rec - 1;
2082
2083 if (num_rec < MLXSW_REG_RAUHTD_REC_MAX_NUM)
2084 return false;
2085 if (mlxsw_reg_rauhtd_rec_type_get(rauhtd_pl, last_rec_index) ==
2086 MLXSW_REG_RAUHTD_TYPE_IPV6)
2087 return true;
2088
2089 num_entries = mlxsw_reg_rauhtd_ipv4_rec_num_entries_get(rauhtd_pl,
2090 last_rec_index);
2091 if (++num_entries == MLXSW_REG_RAUHTD_IPV4_ENT_PER_REC)
2092 return true;
2093 return false;
2094}
2095
60f040ca
AS
2096static int
2097__mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp *mlxsw_sp,
2098 char *rauhtd_pl,
2099 enum mlxsw_reg_rauhtd_type type)
c723c735 2100{
60f040ca
AS
2101 int i, num_rec;
2102 int err;
c723c735
YG
2103
2104 /* Make sure the neighbour's netdev isn't removed in the
2105 * process.
2106 */
2107 rtnl_lock();
2108 do {
60f040ca 2109 mlxsw_reg_rauhtd_pack(rauhtd_pl, type);
c723c735
YG
2110 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(rauhtd),
2111 rauhtd_pl);
2112 if (err) {
7ff176f8 2113 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to dump neighbour table\n");
c723c735
YG
2114 break;
2115 }
2116 num_rec = mlxsw_reg_rauhtd_num_rec_get(rauhtd_pl);
2117 for (i = 0; i < num_rec; i++)
2118 mlxsw_sp_router_neigh_rec_process(mlxsw_sp, rauhtd_pl,
2119 i);
42cdb338 2120 } while (mlxsw_sp_router_rauhtd_is_full(rauhtd_pl));
c723c735
YG
2121 rtnl_unlock();
2122
60f040ca
AS
2123 return err;
2124}
2125
2126static int mlxsw_sp_router_neighs_update_rauhtd(struct mlxsw_sp *mlxsw_sp)
2127{
2128 enum mlxsw_reg_rauhtd_type type;
2129 char *rauhtd_pl;
2130 int err;
2131
2132 rauhtd_pl = kmalloc(MLXSW_REG_RAUHTD_LEN, GFP_KERNEL);
2133 if (!rauhtd_pl)
2134 return -ENOMEM;
2135
2136 type = MLXSW_REG_RAUHTD_TYPE_IPV4;
2137 err = __mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp, rauhtd_pl, type);
2138 if (err)
2139 goto out;
2140
2141 type = MLXSW_REG_RAUHTD_TYPE_IPV6;
2142 err = __mlxsw_sp_router_neighs_update_rauhtd(mlxsw_sp, rauhtd_pl, type);
2143out:
c723c735 2144 kfree(rauhtd_pl);
b2157149
YG
2145 return err;
2146}
2147
2148static void mlxsw_sp_router_neighs_update_nh(struct mlxsw_sp *mlxsw_sp)
2149{
2150 struct mlxsw_sp_neigh_entry *neigh_entry;
2151
2152 /* Take RTNL mutex here to prevent lists from changes */
2153 rtnl_lock();
9011b677 2154 list_for_each_entry(neigh_entry, &mlxsw_sp->router->nexthop_neighs_list,
8a0b7275 2155 nexthop_neighs_list_node)
b2157149
YG
2156 /* If this neigh have nexthops, make the kernel think this neigh
2157 * is active regardless of the traffic.
2158 */
8a0b7275 2159 neigh_event_send(neigh_entry->key.n, NULL);
b2157149
YG
2160 rtnl_unlock();
2161}
2162
2163static void
2164mlxsw_sp_router_neighs_update_work_schedule(struct mlxsw_sp *mlxsw_sp)
2165{
9011b677 2166 unsigned long interval = mlxsw_sp->router->neighs_update.interval;
b2157149 2167
9011b677 2168 mlxsw_core_schedule_dw(&mlxsw_sp->router->neighs_update.dw,
b2157149
YG
2169 msecs_to_jiffies(interval));
2170}
2171
2172static void mlxsw_sp_router_neighs_update_work(struct work_struct *work)
2173{
9011b677 2174 struct mlxsw_sp_router *router;
b2157149
YG
2175 int err;
2176
9011b677
IS
2177 router = container_of(work, struct mlxsw_sp_router,
2178 neighs_update.dw.work);
2179 err = mlxsw_sp_router_neighs_update_rauhtd(router->mlxsw_sp);
b2157149 2180 if (err)
9011b677 2181 dev_err(router->mlxsw_sp->bus_info->dev, "Could not update kernel for neigh activity");
b2157149 2182
9011b677 2183 mlxsw_sp_router_neighs_update_nh(router->mlxsw_sp);
b2157149 2184
9011b677 2185 mlxsw_sp_router_neighs_update_work_schedule(router->mlxsw_sp);
c723c735
YG
2186}
2187
0b2361d9
YG
2188static void mlxsw_sp_router_probe_unresolved_nexthops(struct work_struct *work)
2189{
2190 struct mlxsw_sp_neigh_entry *neigh_entry;
9011b677 2191 struct mlxsw_sp_router *router;
0b2361d9 2192
9011b677
IS
2193 router = container_of(work, struct mlxsw_sp_router,
2194 nexthop_probe_dw.work);
0b2361d9
YG
2195 /* Iterate over nexthop neighbours, find those who are unresolved and
2196 * send arp on them. This solves the chicken-egg problem when
2197 * the nexthop wouldn't get offloaded until the neighbor is resolved
2198 * but it wouldn't get resolved ever in case traffic is flowing in HW
2199 * using different nexthop.
2200 *
2201 * Take RTNL mutex here to prevent lists from changes.
2202 */
2203 rtnl_lock();
9011b677 2204 list_for_each_entry(neigh_entry, &router->nexthop_neighs_list,
8a0b7275 2205 nexthop_neighs_list_node)
01b1aa35 2206 if (!neigh_entry->connected)
33b1341c 2207 neigh_event_send(neigh_entry->key.n, NULL);
0b2361d9
YG
2208 rtnl_unlock();
2209
9011b677 2210 mlxsw_core_schedule_dw(&router->nexthop_probe_dw,
0b2361d9
YG
2211 MLXSW_SP_UNRESOLVED_NH_PROBE_INTERVAL);
2212}
2213
a7ff87ac
JP
2214static void
2215mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
2216 struct mlxsw_sp_neigh_entry *neigh_entry,
2217 bool removing);
2218
5c8802f1
IS
2219static enum mlxsw_reg_rauht_op mlxsw_sp_rauht_op(bool adding)
2220{
2221 return adding ? MLXSW_REG_RAUHT_OP_WRITE_ADD :
2222 MLXSW_REG_RAUHT_OP_WRITE_DELETE;
2223}
2224
2225static void
2226mlxsw_sp_router_neigh_entry_op4(struct mlxsw_sp *mlxsw_sp,
2227 struct mlxsw_sp_neigh_entry *neigh_entry,
2228 enum mlxsw_reg_rauht_op op)
a6bf9e93 2229{
33b1341c 2230 struct neighbour *n = neigh_entry->key.n;
5c8802f1 2231 u32 dip = ntohl(*((__be32 *) n->primary_key));
a6bf9e93 2232 char rauht_pl[MLXSW_REG_RAUHT_LEN];
5c8802f1
IS
2233
2234 mlxsw_reg_rauht_pack4(rauht_pl, op, neigh_entry->rif, neigh_entry->ha,
2235 dip);
7cfcbc75
AS
2236 if (neigh_entry->counter_valid)
2237 mlxsw_reg_rauht_pack_counter(rauht_pl,
2238 neigh_entry->counter_index);
5c8802f1
IS
2239 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
2240}
2241
d5eb89cf
AS
2242static void
2243mlxsw_sp_router_neigh_entry_op6(struct mlxsw_sp *mlxsw_sp,
2244 struct mlxsw_sp_neigh_entry *neigh_entry,
2245 enum mlxsw_reg_rauht_op op)
2246{
2247 struct neighbour *n = neigh_entry->key.n;
2248 char rauht_pl[MLXSW_REG_RAUHT_LEN];
2249 const char *dip = n->primary_key;
2250
2251 mlxsw_reg_rauht_pack6(rauht_pl, op, neigh_entry->rif, neigh_entry->ha,
2252 dip);
7cfcbc75
AS
2253 if (neigh_entry->counter_valid)
2254 mlxsw_reg_rauht_pack_counter(rauht_pl,
2255 neigh_entry->counter_index);
d5eb89cf
AS
2256 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rauht), rauht_pl);
2257}
2258
1d1056d8 2259bool mlxsw_sp_neigh_ipv6_ignore(struct mlxsw_sp_neigh_entry *neigh_entry)
d5eb89cf 2260{
1d1056d8
AS
2261 struct neighbour *n = neigh_entry->key.n;
2262
d5eb89cf
AS
2263 /* Packets with a link-local destination address are trapped
2264 * after LPM lookup and never reach the neighbour table, so
2265 * there is no need to program such neighbours to the device.
2266 */
2267 if (ipv6_addr_type((struct in6_addr *) &n->primary_key) &
2268 IPV6_ADDR_LINKLOCAL)
2269 return true;
2270 return false;
2271}
2272
5c8802f1
IS
2273static void
2274mlxsw_sp_neigh_entry_update(struct mlxsw_sp *mlxsw_sp,
2275 struct mlxsw_sp_neigh_entry *neigh_entry,
2276 bool adding)
2277{
2278 if (!adding && !neigh_entry->connected)
2279 return;
2280 neigh_entry->connected = adding;
b5f3e0d4 2281 if (neigh_entry->key.n->tbl->family == AF_INET) {
5c8802f1
IS
2282 mlxsw_sp_router_neigh_entry_op4(mlxsw_sp, neigh_entry,
2283 mlxsw_sp_rauht_op(adding));
b5f3e0d4 2284 } else if (neigh_entry->key.n->tbl->family == AF_INET6) {
1d1056d8 2285 if (mlxsw_sp_neigh_ipv6_ignore(neigh_entry))
d5eb89cf
AS
2286 return;
2287 mlxsw_sp_router_neigh_entry_op6(mlxsw_sp, neigh_entry,
2288 mlxsw_sp_rauht_op(adding));
2289 } else {
5c8802f1 2290 WARN_ON_ONCE(1);
d5eb89cf 2291 }
5c8802f1
IS
2292}
2293
a481d713
AS
2294void
2295mlxsw_sp_neigh_entry_counter_update(struct mlxsw_sp *mlxsw_sp,
2296 struct mlxsw_sp_neigh_entry *neigh_entry,
2297 bool adding)
2298{
2299 if (adding)
2300 mlxsw_sp_neigh_counter_alloc(mlxsw_sp, neigh_entry);
2301 else
2302 mlxsw_sp_neigh_counter_free(mlxsw_sp, neigh_entry);
2303 mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, true);
2304}
2305
ceb8881d 2306struct mlxsw_sp_netevent_work {
5c8802f1
IS
2307 struct work_struct work;
2308 struct mlxsw_sp *mlxsw_sp;
2309 struct neighbour *n;
2310};
2311
2312static void mlxsw_sp_router_neigh_event_work(struct work_struct *work)
2313{
ceb8881d
IS
2314 struct mlxsw_sp_netevent_work *net_work =
2315 container_of(work, struct mlxsw_sp_netevent_work, work);
2316 struct mlxsw_sp *mlxsw_sp = net_work->mlxsw_sp;
5c8802f1 2317 struct mlxsw_sp_neigh_entry *neigh_entry;
ceb8881d 2318 struct neighbour *n = net_work->n;
5c8802f1 2319 unsigned char ha[ETH_ALEN];
a6bf9e93 2320 bool entry_connected;
93a87e5e 2321 u8 nud_state, dead;
a6bf9e93 2322
5c8802f1
IS
2323 /* If these parameters are changed after we release the lock,
2324 * then we are guaranteed to receive another event letting us
2325 * know about it.
2326 */
a6bf9e93 2327 read_lock_bh(&n->lock);
5c8802f1 2328 memcpy(ha, n->ha, ETH_ALEN);
a6bf9e93 2329 nud_state = n->nud_state;
93a87e5e 2330 dead = n->dead;
a6bf9e93
YG
2331 read_unlock_bh(&n->lock);
2332
5c8802f1 2333 rtnl_lock();
803335ac
PM
2334 mlxsw_sp_span_respin(mlxsw_sp);
2335
93a87e5e 2336 entry_connected = nud_state & NUD_VALID && !dead;
5c8802f1
IS
2337 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
2338 if (!entry_connected && !neigh_entry)
2339 goto out;
2340 if (!neigh_entry) {
2341 neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
2342 if (IS_ERR(neigh_entry))
2343 goto out;
a6bf9e93
YG
2344 }
2345
5c8802f1
IS
2346 memcpy(neigh_entry->ha, ha, ETH_ALEN);
2347 mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, entry_connected);
2348 mlxsw_sp_nexthop_neigh_update(mlxsw_sp, neigh_entry, !entry_connected);
2349
2350 if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
2351 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
2352
2353out:
2354 rtnl_unlock();
a6bf9e93 2355 neigh_release(n);
ceb8881d 2356 kfree(net_work);
a6bf9e93
YG
2357}
2358
28678f07
IS
2359static int mlxsw_sp_mp_hash_init(struct mlxsw_sp *mlxsw_sp);
2360
2361static void mlxsw_sp_router_mp_hash_event_work(struct work_struct *work)
2362{
2363 struct mlxsw_sp_netevent_work *net_work =
2364 container_of(work, struct mlxsw_sp_netevent_work, work);
2365 struct mlxsw_sp *mlxsw_sp = net_work->mlxsw_sp;
2366
2367 mlxsw_sp_mp_hash_init(mlxsw_sp);
2368 kfree(net_work);
2369}
2370
2371static int mlxsw_sp_router_netevent_event(struct notifier_block *nb,
48fac885 2372 unsigned long event, void *ptr)
c723c735 2373{
ceb8881d 2374 struct mlxsw_sp_netevent_work *net_work;
c723c735 2375 struct mlxsw_sp_port *mlxsw_sp_port;
28678f07 2376 struct mlxsw_sp_router *router;
c723c735
YG
2377 struct mlxsw_sp *mlxsw_sp;
2378 unsigned long interval;
2379 struct neigh_parms *p;
a6bf9e93 2380 struct neighbour *n;
28678f07 2381 struct net *net;
c723c735
YG
2382
2383 switch (event) {
2384 case NETEVENT_DELAY_PROBE_TIME_UPDATE:
2385 p = ptr;
2386
2387 /* We don't care about changes in the default table. */
b5f3e0d4
IS
2388 if (!p->dev || (p->tbl->family != AF_INET &&
2389 p->tbl->family != AF_INET6))
c723c735
YG
2390 return NOTIFY_DONE;
2391
2392 /* We are in atomic context and can't take RTNL mutex,
2393 * so use RCU variant to walk the device chain.
2394 */
2395 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(p->dev);
2396 if (!mlxsw_sp_port)
2397 return NOTIFY_DONE;
2398
2399 mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2400 interval = jiffies_to_msecs(NEIGH_VAR(p, DELAY_PROBE_TIME));
9011b677 2401 mlxsw_sp->router->neighs_update.interval = interval;
c723c735
YG
2402
2403 mlxsw_sp_port_dev_put(mlxsw_sp_port);
2404 break;
a6bf9e93
YG
2405 case NETEVENT_NEIGH_UPDATE:
2406 n = ptr;
a6bf9e93 2407
b5f3e0d4 2408 if (n->tbl->family != AF_INET && n->tbl->family != AF_INET6)
a6bf9e93
YG
2409 return NOTIFY_DONE;
2410
5c8802f1 2411 mlxsw_sp_port = mlxsw_sp_port_lower_dev_hold(n->dev);
a6bf9e93
YG
2412 if (!mlxsw_sp_port)
2413 return NOTIFY_DONE;
2414
ceb8881d
IS
2415 net_work = kzalloc(sizeof(*net_work), GFP_ATOMIC);
2416 if (!net_work) {
a6bf9e93 2417 mlxsw_sp_port_dev_put(mlxsw_sp_port);
5c8802f1 2418 return NOTIFY_BAD;
a6bf9e93 2419 }
5c8802f1 2420
ceb8881d
IS
2421 INIT_WORK(&net_work->work, mlxsw_sp_router_neigh_event_work);
2422 net_work->mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
2423 net_work->n = n;
a6bf9e93
YG
2424
2425 /* Take a reference to ensure the neighbour won't be
2426 * destructed until we drop the reference in delayed
2427 * work.
2428 */
2429 neigh_clone(n);
ceb8881d 2430 mlxsw_core_schedule_work(&net_work->work);
5c8802f1 2431 mlxsw_sp_port_dev_put(mlxsw_sp_port);
a6bf9e93 2432 break;
3192dac6 2433 case NETEVENT_IPV4_MPATH_HASH_UPDATE:
5e18b9c5 2434 case NETEVENT_IPV6_MPATH_HASH_UPDATE:
28678f07
IS
2435 net = ptr;
2436
2437 if (!net_eq(net, &init_net))
2438 return NOTIFY_DONE;
2439
2440 net_work = kzalloc(sizeof(*net_work), GFP_ATOMIC);
2441 if (!net_work)
2442 return NOTIFY_BAD;
2443
2444 router = container_of(nb, struct mlxsw_sp_router, netevent_nb);
2445 INIT_WORK(&net_work->work, mlxsw_sp_router_mp_hash_event_work);
2446 net_work->mlxsw_sp = router->mlxsw_sp;
2447 mlxsw_core_schedule_work(&net_work->work);
2448 break;
c723c735
YG
2449 }
2450
2451 return NOTIFY_DONE;
2452}
2453
6cf3c971
JP
2454static int mlxsw_sp_neigh_init(struct mlxsw_sp *mlxsw_sp)
2455{
c723c735
YG
2456 int err;
2457
9011b677 2458 err = rhashtable_init(&mlxsw_sp->router->neigh_ht,
c723c735
YG
2459 &mlxsw_sp_neigh_ht_params);
2460 if (err)
2461 return err;
2462
2463 /* Initialize the polling interval according to the default
2464 * table.
2465 */
2466 mlxsw_sp_router_neighs_update_interval_init(mlxsw_sp);
2467
0b2361d9 2468 /* Create the delayed works for the activity_update */
9011b677 2469 INIT_DELAYED_WORK(&mlxsw_sp->router->neighs_update.dw,
c723c735 2470 mlxsw_sp_router_neighs_update_work);
9011b677 2471 INIT_DELAYED_WORK(&mlxsw_sp->router->nexthop_probe_dw,
0b2361d9 2472 mlxsw_sp_router_probe_unresolved_nexthops);
9011b677
IS
2473 mlxsw_core_schedule_dw(&mlxsw_sp->router->neighs_update.dw, 0);
2474 mlxsw_core_schedule_dw(&mlxsw_sp->router->nexthop_probe_dw, 0);
c723c735 2475 return 0;
6cf3c971
JP
2476}
2477
2478static void mlxsw_sp_neigh_fini(struct mlxsw_sp *mlxsw_sp)
2479{
9011b677
IS
2480 cancel_delayed_work_sync(&mlxsw_sp->router->neighs_update.dw);
2481 cancel_delayed_work_sync(&mlxsw_sp->router->nexthop_probe_dw);
2482 rhashtable_destroy(&mlxsw_sp->router->neigh_ht);
6cf3c971
JP
2483}
2484
9665b745 2485static void mlxsw_sp_neigh_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
bf95233e 2486 struct mlxsw_sp_rif *rif)
9665b745
IS
2487{
2488 struct mlxsw_sp_neigh_entry *neigh_entry, *tmp;
2489
bf95233e 2490 list_for_each_entry_safe(neigh_entry, tmp, &rif->neigh_list,
8ba6b30e
PM
2491 rif_list_node) {
2492 mlxsw_sp_neigh_entry_update(mlxsw_sp, neigh_entry, false);
9665b745 2493 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
8ba6b30e 2494 }
9665b745
IS
2495}
2496
35225e47
PM
2497enum mlxsw_sp_nexthop_type {
2498 MLXSW_SP_NEXTHOP_TYPE_ETH,
1012b9ac 2499 MLXSW_SP_NEXTHOP_TYPE_IPIP,
35225e47
PM
2500};
2501
c53b8e1b
IS
2502struct mlxsw_sp_nexthop_key {
2503 struct fib_nh *fib_nh;
2504};
2505
a7ff87ac
JP
2506struct mlxsw_sp_nexthop {
2507 struct list_head neigh_list_node; /* member of neigh entry list */
9665b745 2508 struct list_head rif_list_node;
dbe4598c 2509 struct list_head router_list_node;
a7ff87ac
JP
2510 struct mlxsw_sp_nexthop_group *nh_grp; /* pointer back to the group
2511 * this belongs to
2512 */
c53b8e1b
IS
2513 struct rhash_head ht_node;
2514 struct mlxsw_sp_nexthop_key key;
58adf2c4 2515 unsigned char gw_addr[sizeof(struct in6_addr)];
e6f3b379 2516 int ifindex;
408bd946 2517 int nh_weight;
eb789980
IS
2518 int norm_nh_weight;
2519 int num_adj_entries;
bf95233e 2520 struct mlxsw_sp_rif *rif;
a7ff87ac
JP
2521 u8 should_offload:1, /* set indicates this neigh is connected and
2522 * should be put to KVD linear area of this group.
2523 */
2524 offloaded:1, /* set in case the neigh is actually put into
2525 * KVD linear area of this group.
2526 */
2527 update:1; /* set indicates that MAC of this neigh should be
2528 * updated in HW
2529 */
35225e47
PM
2530 enum mlxsw_sp_nexthop_type type;
2531 union {
2532 struct mlxsw_sp_neigh_entry *neigh_entry;
1012b9ac 2533 struct mlxsw_sp_ipip_entry *ipip_entry;
35225e47 2534 };
a5390278
AS
2535 unsigned int counter_index;
2536 bool counter_valid;
a7ff87ac
JP
2537};
2538
2539struct mlxsw_sp_nexthop_group {
ba31d366 2540 void *priv;
e9ad5e7d 2541 struct rhash_head ht_node;
a7ff87ac 2542 struct list_head fib_list; /* list of fib entries that use this group */
58adf2c4 2543 struct neigh_table *neigh_tbl;
b3e8d1eb
IS
2544 u8 adj_index_valid:1,
2545 gateway:1; /* routes using the group use a gateway */
a7ff87ac
JP
2546 u32 adj_index;
2547 u16 ecmp_size;
2548 u16 count;
eb789980 2549 int sum_norm_weight;
a7ff87ac 2550 struct mlxsw_sp_nexthop nexthops[0];
bf95233e 2551#define nh_rif nexthops[0].rif
a7ff87ac
JP
2552};
2553
427e652a
AS
2554void mlxsw_sp_nexthop_counter_alloc(struct mlxsw_sp *mlxsw_sp,
2555 struct mlxsw_sp_nexthop *nh)
a5390278
AS
2556{
2557 struct devlink *devlink;
2558
2559 devlink = priv_to_devlink(mlxsw_sp->core);
2560 if (!devlink_dpipe_table_counter_enabled(devlink,
2561 MLXSW_SP_DPIPE_TABLE_NAME_ADJ))
2562 return;
2563
2564 if (mlxsw_sp_flow_counter_alloc(mlxsw_sp, &nh->counter_index))
2565 return;
2566
2567 nh->counter_valid = true;
2568}
2569
427e652a
AS
2570void mlxsw_sp_nexthop_counter_free(struct mlxsw_sp *mlxsw_sp,
2571 struct mlxsw_sp_nexthop *nh)
a5390278
AS
2572{
2573 if (!nh->counter_valid)
2574 return;
2575 mlxsw_sp_flow_counter_free(mlxsw_sp, nh->counter_index);
2576 nh->counter_valid = false;
2577}
2578
2579int mlxsw_sp_nexthop_counter_get(struct mlxsw_sp *mlxsw_sp,
2580 struct mlxsw_sp_nexthop *nh, u64 *p_counter)
2581{
2582 if (!nh->counter_valid)
2583 return -EINVAL;
2584
2585 return mlxsw_sp_flow_counter_get(mlxsw_sp, nh->counter_index,
2586 p_counter, NULL);
2587}
2588
c556cd28
AS
2589struct mlxsw_sp_nexthop *mlxsw_sp_nexthop_next(struct mlxsw_sp_router *router,
2590 struct mlxsw_sp_nexthop *nh)
2591{
2592 if (!nh) {
2593 if (list_empty(&router->nexthop_list))
2594 return NULL;
2595 else
2596 return list_first_entry(&router->nexthop_list,
2597 typeof(*nh), router_list_node);
2598 }
2599 if (list_is_last(&nh->router_list_node, &router->nexthop_list))
2600 return NULL;
2601 return list_next_entry(nh, router_list_node);
2602}
2603
2604bool mlxsw_sp_nexthop_offload(struct mlxsw_sp_nexthop *nh)
2605{
2606 return nh->offloaded;
2607}
2608
2609unsigned char *mlxsw_sp_nexthop_ha(struct mlxsw_sp_nexthop *nh)
2610{
2611 if (!nh->offloaded)
2612 return NULL;
2613 return nh->neigh_entry->ha;
2614}
2615
2616int mlxsw_sp_nexthop_indexes(struct mlxsw_sp_nexthop *nh, u32 *p_adj_index,
e69cd9d7 2617 u32 *p_adj_size, u32 *p_adj_hash_index)
c556cd28
AS
2618{
2619 struct mlxsw_sp_nexthop_group *nh_grp = nh->nh_grp;
2620 u32 adj_hash_index = 0;
2621 int i;
2622
2623 if (!nh->offloaded || !nh_grp->adj_index_valid)
2624 return -EINVAL;
2625
2626 *p_adj_index = nh_grp->adj_index;
e69cd9d7 2627 *p_adj_size = nh_grp->ecmp_size;
c556cd28
AS
2628
2629 for (i = 0; i < nh_grp->count; i++) {
2630 struct mlxsw_sp_nexthop *nh_iter = &nh_grp->nexthops[i];
2631
2632 if (nh_iter == nh)
2633 break;
2634 if (nh_iter->offloaded)
eb789980 2635 adj_hash_index += nh_iter->num_adj_entries;
c556cd28
AS
2636 }
2637
2638 *p_adj_hash_index = adj_hash_index;
2639 return 0;
2640}
2641
2642struct mlxsw_sp_rif *mlxsw_sp_nexthop_rif(struct mlxsw_sp_nexthop *nh)
2643{
2644 return nh->rif;
2645}
2646
2647bool mlxsw_sp_nexthop_group_has_ipip(struct mlxsw_sp_nexthop *nh)
2648{
2649 struct mlxsw_sp_nexthop_group *nh_grp = nh->nh_grp;
2650 int i;
2651
2652 for (i = 0; i < nh_grp->count; i++) {
2653 struct mlxsw_sp_nexthop *nh_iter = &nh_grp->nexthops[i];
2654
2655 if (nh_iter->type == MLXSW_SP_NEXTHOP_TYPE_IPIP)
2656 return true;
2657 }
2658 return false;
2659}
2660
ba31d366
AS
2661static struct fib_info *
2662mlxsw_sp_nexthop4_group_fi(const struct mlxsw_sp_nexthop_group *nh_grp)
2663{
2664 return nh_grp->priv;
2665}
2666
2667struct mlxsw_sp_nexthop_group_cmp_arg {
e6f3b379
AS
2668 enum mlxsw_sp_l3proto proto;
2669 union {
2670 struct fib_info *fi;
2671 struct mlxsw_sp_fib6_entry *fib6_entry;
2672 };
ba31d366
AS
2673};
2674
e6f3b379
AS
2675static bool
2676mlxsw_sp_nexthop6_group_has_nexthop(const struct mlxsw_sp_nexthop_group *nh_grp,
3743d88a
IS
2677 const struct in6_addr *gw, int ifindex,
2678 int weight)
e6f3b379
AS
2679{
2680 int i;
2681
2682 for (i = 0; i < nh_grp->count; i++) {
2683 const struct mlxsw_sp_nexthop *nh;
2684
2685 nh = &nh_grp->nexthops[i];
3743d88a 2686 if (nh->ifindex == ifindex && nh->nh_weight == weight &&
e6f3b379
AS
2687 ipv6_addr_equal(gw, (struct in6_addr *) nh->gw_addr))
2688 return true;
2689 }
2690
2691 return false;
2692}
2693
2694static bool
2695mlxsw_sp_nexthop6_group_cmp(const struct mlxsw_sp_nexthop_group *nh_grp,
2696 const struct mlxsw_sp_fib6_entry *fib6_entry)
2697{
2698 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
2699
2700 if (nh_grp->count != fib6_entry->nrt6)
2701 return false;
2702
2703 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
2704 struct in6_addr *gw;
3743d88a 2705 int ifindex, weight;
e6f3b379
AS
2706
2707 ifindex = mlxsw_sp_rt6->rt->dst.dev->ifindex;
3743d88a 2708 weight = mlxsw_sp_rt6->rt->rt6i_nh_weight;
e6f3b379 2709 gw = &mlxsw_sp_rt6->rt->rt6i_gateway;
3743d88a
IS
2710 if (!mlxsw_sp_nexthop6_group_has_nexthop(nh_grp, gw, ifindex,
2711 weight))
e6f3b379
AS
2712 return false;
2713 }
2714
2715 return true;
2716}
2717
ba31d366
AS
2718static int
2719mlxsw_sp_nexthop_group_cmp(struct rhashtable_compare_arg *arg, const void *ptr)
2720{
2721 const struct mlxsw_sp_nexthop_group_cmp_arg *cmp_arg = arg->key;
2722 const struct mlxsw_sp_nexthop_group *nh_grp = ptr;
2723
e6f3b379
AS
2724 switch (cmp_arg->proto) {
2725 case MLXSW_SP_L3_PROTO_IPV4:
2726 return cmp_arg->fi != mlxsw_sp_nexthop4_group_fi(nh_grp);
2727 case MLXSW_SP_L3_PROTO_IPV6:
2728 return !mlxsw_sp_nexthop6_group_cmp(nh_grp,
2729 cmp_arg->fib6_entry);
2730 default:
2731 WARN_ON(1);
2732 return 1;
2733 }
2734}
2735
2736static int
2737mlxsw_sp_nexthop_group_type(const struct mlxsw_sp_nexthop_group *nh_grp)
2738{
2739 return nh_grp->neigh_tbl->family;
ba31d366
AS
2740}
2741
2742static u32 mlxsw_sp_nexthop_group_hash_obj(const void *data, u32 len, u32 seed)
2743{
2744 const struct mlxsw_sp_nexthop_group *nh_grp = data;
e6f3b379
AS
2745 const struct mlxsw_sp_nexthop *nh;
2746 struct fib_info *fi;
2747 unsigned int val;
2748 int i;
ba31d366 2749
e6f3b379
AS
2750 switch (mlxsw_sp_nexthop_group_type(nh_grp)) {
2751 case AF_INET:
2752 fi = mlxsw_sp_nexthop4_group_fi(nh_grp);
2753 return jhash(&fi, sizeof(fi), seed);
2754 case AF_INET6:
2755 val = nh_grp->count;
2756 for (i = 0; i < nh_grp->count; i++) {
2757 nh = &nh_grp->nexthops[i];
2758 val ^= nh->ifindex;
2759 }
2760 return jhash(&val, sizeof(val), seed);
2761 default:
2762 WARN_ON(1);
2763 return 0;
2764 }
2765}
2766
2767static u32
2768mlxsw_sp_nexthop6_group_hash(struct mlxsw_sp_fib6_entry *fib6_entry, u32 seed)
2769{
2770 unsigned int val = fib6_entry->nrt6;
2771 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
2772 struct net_device *dev;
2773
2774 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
2775 dev = mlxsw_sp_rt6->rt->dst.dev;
2776 val ^= dev->ifindex;
2777 }
2778
2779 return jhash(&val, sizeof(val), seed);
ba31d366
AS
2780}
2781
2782static u32
2783mlxsw_sp_nexthop_group_hash(const void *data, u32 len, u32 seed)
2784{
2785 const struct mlxsw_sp_nexthop_group_cmp_arg *cmp_arg = data;
2786
e6f3b379
AS
2787 switch (cmp_arg->proto) {
2788 case MLXSW_SP_L3_PROTO_IPV4:
2789 return jhash(&cmp_arg->fi, sizeof(cmp_arg->fi), seed);
2790 case MLXSW_SP_L3_PROTO_IPV6:
2791 return mlxsw_sp_nexthop6_group_hash(cmp_arg->fib6_entry, seed);
2792 default:
2793 WARN_ON(1);
2794 return 0;
2795 }
ba31d366
AS
2796}
2797
e9ad5e7d 2798static const struct rhashtable_params mlxsw_sp_nexthop_group_ht_params = {
e9ad5e7d 2799 .head_offset = offsetof(struct mlxsw_sp_nexthop_group, ht_node),
ba31d366
AS
2800 .hashfn = mlxsw_sp_nexthop_group_hash,
2801 .obj_hashfn = mlxsw_sp_nexthop_group_hash_obj,
2802 .obj_cmpfn = mlxsw_sp_nexthop_group_cmp,
e9ad5e7d
IS
2803};
2804
2805static int mlxsw_sp_nexthop_group_insert(struct mlxsw_sp *mlxsw_sp,
2806 struct mlxsw_sp_nexthop_group *nh_grp)
2807{
e6f3b379
AS
2808 if (mlxsw_sp_nexthop_group_type(nh_grp) == AF_INET6 &&
2809 !nh_grp->gateway)
2810 return 0;
2811
9011b677 2812 return rhashtable_insert_fast(&mlxsw_sp->router->nexthop_group_ht,
e9ad5e7d
IS
2813 &nh_grp->ht_node,
2814 mlxsw_sp_nexthop_group_ht_params);
2815}
2816
2817static void mlxsw_sp_nexthop_group_remove(struct mlxsw_sp *mlxsw_sp,
2818 struct mlxsw_sp_nexthop_group *nh_grp)
2819{
e6f3b379
AS
2820 if (mlxsw_sp_nexthop_group_type(nh_grp) == AF_INET6 &&
2821 !nh_grp->gateway)
2822 return;
2823
9011b677 2824 rhashtable_remove_fast(&mlxsw_sp->router->nexthop_group_ht,
e9ad5e7d
IS
2825 &nh_grp->ht_node,
2826 mlxsw_sp_nexthop_group_ht_params);
2827}
2828
2829static struct mlxsw_sp_nexthop_group *
ba31d366
AS
2830mlxsw_sp_nexthop4_group_lookup(struct mlxsw_sp *mlxsw_sp,
2831 struct fib_info *fi)
e9ad5e7d 2832{
ba31d366
AS
2833 struct mlxsw_sp_nexthop_group_cmp_arg cmp_arg;
2834
e6f3b379 2835 cmp_arg.proto = MLXSW_SP_L3_PROTO_IPV4;
ba31d366
AS
2836 cmp_arg.fi = fi;
2837 return rhashtable_lookup_fast(&mlxsw_sp->router->nexthop_group_ht,
2838 &cmp_arg,
e9ad5e7d
IS
2839 mlxsw_sp_nexthop_group_ht_params);
2840}
2841
e6f3b379
AS
2842static struct mlxsw_sp_nexthop_group *
2843mlxsw_sp_nexthop6_group_lookup(struct mlxsw_sp *mlxsw_sp,
2844 struct mlxsw_sp_fib6_entry *fib6_entry)
2845{
2846 struct mlxsw_sp_nexthop_group_cmp_arg cmp_arg;
2847
2848 cmp_arg.proto = MLXSW_SP_L3_PROTO_IPV6;
2849 cmp_arg.fib6_entry = fib6_entry;
2850 return rhashtable_lookup_fast(&mlxsw_sp->router->nexthop_group_ht,
2851 &cmp_arg,
2852 mlxsw_sp_nexthop_group_ht_params);
2853}
2854
c53b8e1b
IS
2855static const struct rhashtable_params mlxsw_sp_nexthop_ht_params = {
2856 .key_offset = offsetof(struct mlxsw_sp_nexthop, key),
2857 .head_offset = offsetof(struct mlxsw_sp_nexthop, ht_node),
2858 .key_len = sizeof(struct mlxsw_sp_nexthop_key),
2859};
2860
2861static int mlxsw_sp_nexthop_insert(struct mlxsw_sp *mlxsw_sp,
2862 struct mlxsw_sp_nexthop *nh)
2863{
9011b677 2864 return rhashtable_insert_fast(&mlxsw_sp->router->nexthop_ht,
c53b8e1b
IS
2865 &nh->ht_node, mlxsw_sp_nexthop_ht_params);
2866}
2867
2868static void mlxsw_sp_nexthop_remove(struct mlxsw_sp *mlxsw_sp,
2869 struct mlxsw_sp_nexthop *nh)
2870{
9011b677 2871 rhashtable_remove_fast(&mlxsw_sp->router->nexthop_ht, &nh->ht_node,
c53b8e1b
IS
2872 mlxsw_sp_nexthop_ht_params);
2873}
2874
ad178c8e
IS
2875static struct mlxsw_sp_nexthop *
2876mlxsw_sp_nexthop_lookup(struct mlxsw_sp *mlxsw_sp,
2877 struct mlxsw_sp_nexthop_key key)
2878{
9011b677 2879 return rhashtable_lookup_fast(&mlxsw_sp->router->nexthop_ht, &key,
ad178c8e
IS
2880 mlxsw_sp_nexthop_ht_params);
2881}
2882
a7ff87ac 2883static int mlxsw_sp_adj_index_mass_update_vr(struct mlxsw_sp *mlxsw_sp,
76610ebb 2884 const struct mlxsw_sp_fib *fib,
a7ff87ac
JP
2885 u32 adj_index, u16 ecmp_size,
2886 u32 new_adj_index,
2887 u16 new_ecmp_size)
2888{
2889 char raleu_pl[MLXSW_REG_RALEU_LEN];
2890
1a9234e6 2891 mlxsw_reg_raleu_pack(raleu_pl,
76610ebb
IS
2892 (enum mlxsw_reg_ralxx_protocol) fib->proto,
2893 fib->vr->id, adj_index, ecmp_size, new_adj_index,
1a9234e6 2894 new_ecmp_size);
a7ff87ac
JP
2895 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raleu), raleu_pl);
2896}
2897
2898static int mlxsw_sp_adj_index_mass_update(struct mlxsw_sp *mlxsw_sp,
2899 struct mlxsw_sp_nexthop_group *nh_grp,
2900 u32 old_adj_index, u16 old_ecmp_size)
2901{
2902 struct mlxsw_sp_fib_entry *fib_entry;
76610ebb 2903 struct mlxsw_sp_fib *fib = NULL;
a7ff87ac
JP
2904 int err;
2905
2906 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
76610ebb 2907 if (fib == fib_entry->fib_node->fib)
a7ff87ac 2908 continue;
76610ebb
IS
2909 fib = fib_entry->fib_node->fib;
2910 err = mlxsw_sp_adj_index_mass_update_vr(mlxsw_sp, fib,
a7ff87ac
JP
2911 old_adj_index,
2912 old_ecmp_size,
2913 nh_grp->adj_index,
2914 nh_grp->ecmp_size);
2915 if (err)
2916 return err;
2917 }
2918 return 0;
2919}
2920
eb789980
IS
2921static int __mlxsw_sp_nexthop_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
2922 struct mlxsw_sp_nexthop *nh)
a7ff87ac
JP
2923{
2924 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
2925 char ratr_pl[MLXSW_REG_RATR_LEN];
2926
2927 mlxsw_reg_ratr_pack(ratr_pl, MLXSW_REG_RATR_OP_WRITE_WRITE_ENTRY,
89e41982
PM
2928 true, MLXSW_REG_RATR_TYPE_ETHERNET,
2929 adj_index, neigh_entry->rif);
a7ff87ac 2930 mlxsw_reg_ratr_eth_entry_pack(ratr_pl, neigh_entry->ha);
a5390278
AS
2931 if (nh->counter_valid)
2932 mlxsw_reg_ratr_counter_pack(ratr_pl, nh->counter_index, true);
2933 else
2934 mlxsw_reg_ratr_counter_pack(ratr_pl, 0, false);
2935
a7ff87ac
JP
2936 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ratr), ratr_pl);
2937}
2938
eb789980
IS
2939int mlxsw_sp_nexthop_update(struct mlxsw_sp *mlxsw_sp, u32 adj_index,
2940 struct mlxsw_sp_nexthop *nh)
2941{
2942 int i;
2943
2944 for (i = 0; i < nh->num_adj_entries; i++) {
2945 int err;
2946
2947 err = __mlxsw_sp_nexthop_update(mlxsw_sp, adj_index + i, nh);
2948 if (err)
2949 return err;
2950 }
2951
2952 return 0;
2953}
2954
2955static int __mlxsw_sp_nexthop_ipip_update(struct mlxsw_sp *mlxsw_sp,
2956 u32 adj_index,
2957 struct mlxsw_sp_nexthop *nh)
1012b9ac
PM
2958{
2959 const struct mlxsw_sp_ipip_ops *ipip_ops;
2960
2961 ipip_ops = mlxsw_sp->router->ipip_ops_arr[nh->ipip_entry->ipipt];
2962 return ipip_ops->nexthop_update(mlxsw_sp, adj_index, nh->ipip_entry);
2963}
2964
eb789980
IS
2965static int mlxsw_sp_nexthop_ipip_update(struct mlxsw_sp *mlxsw_sp,
2966 u32 adj_index,
2967 struct mlxsw_sp_nexthop *nh)
2968{
2969 int i;
2970
2971 for (i = 0; i < nh->num_adj_entries; i++) {
2972 int err;
2973
2974 err = __mlxsw_sp_nexthop_ipip_update(mlxsw_sp, adj_index + i,
2975 nh);
2976 if (err)
2977 return err;
2978 }
2979
2980 return 0;
2981}
2982
a7ff87ac 2983static int
35225e47
PM
2984mlxsw_sp_nexthop_group_update(struct mlxsw_sp *mlxsw_sp,
2985 struct mlxsw_sp_nexthop_group *nh_grp,
2986 bool reallocate)
a7ff87ac
JP
2987{
2988 u32 adj_index = nh_grp->adj_index; /* base */
2989 struct mlxsw_sp_nexthop *nh;
2990 int i;
2991 int err;
2992
2993 for (i = 0; i < nh_grp->count; i++) {
2994 nh = &nh_grp->nexthops[i];
2995
2996 if (!nh->should_offload) {
2997 nh->offloaded = 0;
2998 continue;
2999 }
3000
a59b7e02 3001 if (nh->update || reallocate) {
35225e47
PM
3002 switch (nh->type) {
3003 case MLXSW_SP_NEXTHOP_TYPE_ETH:
a5390278 3004 err = mlxsw_sp_nexthop_update
35225e47
PM
3005 (mlxsw_sp, adj_index, nh);
3006 break;
1012b9ac
PM
3007 case MLXSW_SP_NEXTHOP_TYPE_IPIP:
3008 err = mlxsw_sp_nexthop_ipip_update
3009 (mlxsw_sp, adj_index, nh);
3010 break;
35225e47 3011 }
a7ff87ac
JP
3012 if (err)
3013 return err;
3014 nh->update = 0;
3015 nh->offloaded = 1;
3016 }
eb789980 3017 adj_index += nh->num_adj_entries;
a7ff87ac
JP
3018 }
3019 return 0;
3020}
3021
1819ae3d
IS
3022static bool
3023mlxsw_sp_fib_node_entry_is_first(const struct mlxsw_sp_fib_node *fib_node,
3024 const struct mlxsw_sp_fib_entry *fib_entry);
3025
a7ff87ac
JP
3026static int
3027mlxsw_sp_nexthop_fib_entries_update(struct mlxsw_sp *mlxsw_sp,
3028 struct mlxsw_sp_nexthop_group *nh_grp)
3029{
3030 struct mlxsw_sp_fib_entry *fib_entry;
3031 int err;
3032
3033 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
1819ae3d
IS
3034 if (!mlxsw_sp_fib_node_entry_is_first(fib_entry->fib_node,
3035 fib_entry))
3036 continue;
a7ff87ac
JP
3037 err = mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
3038 if (err)
3039 return err;
3040 }
3041 return 0;
3042}
3043
77d964e6
IS
3044static void
3045mlxsw_sp_fib_entry_offload_refresh(struct mlxsw_sp_fib_entry *fib_entry,
3046 enum mlxsw_reg_ralue_op op, int err);
3047
3048static void
3049mlxsw_sp_nexthop_fib_entries_refresh(struct mlxsw_sp_nexthop_group *nh_grp)
3050{
3051 enum mlxsw_reg_ralue_op op = MLXSW_REG_RALUE_OP_WRITE_WRITE;
3052 struct mlxsw_sp_fib_entry *fib_entry;
3053
3054 list_for_each_entry(fib_entry, &nh_grp->fib_list, nexthop_group_node) {
3055 if (!mlxsw_sp_fib_node_entry_is_first(fib_entry->fib_node,
3056 fib_entry))
3057 continue;
3058 mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, 0);
3059 }
3060}
3061
425a08c6
IS
3062static void mlxsw_sp_adj_grp_size_round_up(u16 *p_adj_grp_size)
3063{
3064 /* Valid sizes for an adjacency group are:
3065 * 1-64, 512, 1024, 2048 and 4096.
3066 */
3067 if (*p_adj_grp_size <= 64)
3068 return;
3069 else if (*p_adj_grp_size <= 512)
3070 *p_adj_grp_size = 512;
3071 else if (*p_adj_grp_size <= 1024)
3072 *p_adj_grp_size = 1024;
3073 else if (*p_adj_grp_size <= 2048)
3074 *p_adj_grp_size = 2048;
3075 else
3076 *p_adj_grp_size = 4096;
3077}
3078
3079static void mlxsw_sp_adj_grp_size_round_down(u16 *p_adj_grp_size,
3080 unsigned int alloc_size)
3081{
3082 if (alloc_size >= 4096)
3083 *p_adj_grp_size = 4096;
3084 else if (alloc_size >= 2048)
3085 *p_adj_grp_size = 2048;
3086 else if (alloc_size >= 1024)
3087 *p_adj_grp_size = 1024;
3088 else if (alloc_size >= 512)
3089 *p_adj_grp_size = 512;
3090}
3091
3092static int mlxsw_sp_fix_adj_grp_size(struct mlxsw_sp *mlxsw_sp,
3093 u16 *p_adj_grp_size)
3094{
3095 unsigned int alloc_size;
3096 int err;
3097
3098 /* Round up the requested group size to the next size supported
3099 * by the device and make sure the request can be satisfied.
3100 */
3101 mlxsw_sp_adj_grp_size_round_up(p_adj_grp_size);
3102 err = mlxsw_sp_kvdl_alloc_size_query(mlxsw_sp, *p_adj_grp_size,
3103 &alloc_size);
3104 if (err)
3105 return err;
3106 /* It is possible the allocation results in more allocated
3107 * entries than requested. Try to use as much of them as
3108 * possible.
3109 */
3110 mlxsw_sp_adj_grp_size_round_down(p_adj_grp_size, alloc_size);
3111
3112 return 0;
3113}
3114
eb789980
IS
3115static void
3116mlxsw_sp_nexthop_group_normalize(struct mlxsw_sp_nexthop_group *nh_grp)
3117{
3118 int i, g = 0, sum_norm_weight = 0;
3119 struct mlxsw_sp_nexthop *nh;
3120
3121 for (i = 0; i < nh_grp->count; i++) {
3122 nh = &nh_grp->nexthops[i];
3123
3124 if (!nh->should_offload)
3125 continue;
3126 if (g > 0)
3127 g = gcd(nh->nh_weight, g);
3128 else
3129 g = nh->nh_weight;
3130 }
3131
3132 for (i = 0; i < nh_grp->count; i++) {
3133 nh = &nh_grp->nexthops[i];
3134
3135 if (!nh->should_offload)
3136 continue;
3137 nh->norm_nh_weight = nh->nh_weight / g;
3138 sum_norm_weight += nh->norm_nh_weight;
3139 }
3140
3141 nh_grp->sum_norm_weight = sum_norm_weight;
3142}
3143
3144static void
3145mlxsw_sp_nexthop_group_rebalance(struct mlxsw_sp_nexthop_group *nh_grp)
3146{
3147 int total = nh_grp->sum_norm_weight;
3148 u16 ecmp_size = nh_grp->ecmp_size;
3149 int i, weight = 0, lower_bound = 0;
3150
3151 for (i = 0; i < nh_grp->count; i++) {
3152 struct mlxsw_sp_nexthop *nh = &nh_grp->nexthops[i];
3153 int upper_bound;
3154
3155 if (!nh->should_offload)
3156 continue;
3157 weight += nh->norm_nh_weight;
3158 upper_bound = DIV_ROUND_CLOSEST(ecmp_size * weight, total);
3159 nh->num_adj_entries = upper_bound - lower_bound;
3160 lower_bound = upper_bound;
3161 }
3162}
3163
a7ff87ac
JP
3164static void
3165mlxsw_sp_nexthop_group_refresh(struct mlxsw_sp *mlxsw_sp,
3166 struct mlxsw_sp_nexthop_group *nh_grp)
3167{
eb789980 3168 u16 ecmp_size, old_ecmp_size;
a7ff87ac
JP
3169 struct mlxsw_sp_nexthop *nh;
3170 bool offload_change = false;
3171 u32 adj_index;
a7ff87ac
JP
3172 bool old_adj_index_valid;
3173 u32 old_adj_index;
a7ff87ac
JP
3174 int i;
3175 int err;
3176
b3e8d1eb
IS
3177 if (!nh_grp->gateway) {
3178 mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
3179 return;
3180 }
3181
a7ff87ac
JP
3182 for (i = 0; i < nh_grp->count; i++) {
3183 nh = &nh_grp->nexthops[i];
3184
56b8a9ed 3185 if (nh->should_offload != nh->offloaded) {
a7ff87ac
JP
3186 offload_change = true;
3187 if (nh->should_offload)
3188 nh->update = 1;
3189 }
a7ff87ac
JP
3190 }
3191 if (!offload_change) {
3192 /* Nothing was added or removed, so no need to reallocate. Just
3193 * update MAC on existing adjacency indexes.
3194 */
35225e47 3195 err = mlxsw_sp_nexthop_group_update(mlxsw_sp, nh_grp, false);
a7ff87ac
JP
3196 if (err) {
3197 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
3198 goto set_trap;
3199 }
3200 return;
3201 }
eb789980
IS
3202 mlxsw_sp_nexthop_group_normalize(nh_grp);
3203 if (!nh_grp->sum_norm_weight)
a7ff87ac
JP
3204 /* No neigh of this group is connected so we just set
3205 * the trap and let everthing flow through kernel.
3206 */
3207 goto set_trap;
3208
eb789980 3209 ecmp_size = nh_grp->sum_norm_weight;
425a08c6
IS
3210 err = mlxsw_sp_fix_adj_grp_size(mlxsw_sp, &ecmp_size);
3211 if (err)
3212 /* No valid allocation size available. */
3213 goto set_trap;
3214
13124443
AS
3215 err = mlxsw_sp_kvdl_alloc(mlxsw_sp, ecmp_size, &adj_index);
3216 if (err) {
a7ff87ac
JP
3217 /* We ran out of KVD linear space, just set the
3218 * trap and let everything flow through kernel.
3219 */
3220 dev_warn(mlxsw_sp->bus_info->dev, "Failed to allocate KVD linear area for nexthop group.\n");
3221 goto set_trap;
3222 }
a7ff87ac
JP
3223 old_adj_index_valid = nh_grp->adj_index_valid;
3224 old_adj_index = nh_grp->adj_index;
3225 old_ecmp_size = nh_grp->ecmp_size;
3226 nh_grp->adj_index_valid = 1;
3227 nh_grp->adj_index = adj_index;
3228 nh_grp->ecmp_size = ecmp_size;
eb789980 3229 mlxsw_sp_nexthop_group_rebalance(nh_grp);
35225e47 3230 err = mlxsw_sp_nexthop_group_update(mlxsw_sp, nh_grp, true);
a7ff87ac
JP
3231 if (err) {
3232 dev_warn(mlxsw_sp->bus_info->dev, "Failed to update neigh MAC in adjacency table.\n");
3233 goto set_trap;
3234 }
3235
3236 if (!old_adj_index_valid) {
3237 /* The trap was set for fib entries, so we have to call
3238 * fib entry update to unset it and use adjacency index.
3239 */
3240 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
3241 if (err) {
3242 dev_warn(mlxsw_sp->bus_info->dev, "Failed to add adjacency index to fib entries.\n");
3243 goto set_trap;
3244 }
3245 return;
3246 }
3247
3248 err = mlxsw_sp_adj_index_mass_update(mlxsw_sp, nh_grp,
3249 old_adj_index, old_ecmp_size);
3250 mlxsw_sp_kvdl_free(mlxsw_sp, old_adj_index);
3251 if (err) {
3252 dev_warn(mlxsw_sp->bus_info->dev, "Failed to mass-update adjacency index for nexthop group.\n");
3253 goto set_trap;
3254 }
77d964e6
IS
3255
3256 /* Offload state within the group changed, so update the flags. */
3257 mlxsw_sp_nexthop_fib_entries_refresh(nh_grp);
3258
a7ff87ac
JP
3259 return;
3260
3261set_trap:
3262 old_adj_index_valid = nh_grp->adj_index_valid;
3263 nh_grp->adj_index_valid = 0;
3264 for (i = 0; i < nh_grp->count; i++) {
3265 nh = &nh_grp->nexthops[i];
3266 nh->offloaded = 0;
3267 }
3268 err = mlxsw_sp_nexthop_fib_entries_update(mlxsw_sp, nh_grp);
3269 if (err)
3270 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set traps for fib entries.\n");
3271 if (old_adj_index_valid)
3272 mlxsw_sp_kvdl_free(mlxsw_sp, nh_grp->adj_index);
3273}
3274
3275static void __mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp_nexthop *nh,
3276 bool removing)
3277{
213666a3 3278 if (!removing)
a7ff87ac 3279 nh->should_offload = 1;
8764a826 3280 else
a7ff87ac
JP
3281 nh->should_offload = 0;
3282 nh->update = 1;
3283}
3284
3285static void
3286mlxsw_sp_nexthop_neigh_update(struct mlxsw_sp *mlxsw_sp,
3287 struct mlxsw_sp_neigh_entry *neigh_entry,
3288 bool removing)
3289{
3290 struct mlxsw_sp_nexthop *nh;
3291
a7ff87ac
JP
3292 list_for_each_entry(nh, &neigh_entry->nexthop_list,
3293 neigh_list_node) {
3294 __mlxsw_sp_nexthop_neigh_update(nh, removing);
3295 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
3296 }
a7ff87ac
JP
3297}
3298
9665b745 3299static void mlxsw_sp_nexthop_rif_init(struct mlxsw_sp_nexthop *nh,
bf95233e 3300 struct mlxsw_sp_rif *rif)
9665b745 3301{
bf95233e 3302 if (nh->rif)
9665b745
IS
3303 return;
3304
bf95233e
AS
3305 nh->rif = rif;
3306 list_add(&nh->rif_list_node, &rif->nexthop_list);
9665b745
IS
3307}
3308
3309static void mlxsw_sp_nexthop_rif_fini(struct mlxsw_sp_nexthop *nh)
3310{
bf95233e 3311 if (!nh->rif)
9665b745
IS
3312 return;
3313
3314 list_del(&nh->rif_list_node);
bf95233e 3315 nh->rif = NULL;
9665b745
IS
3316}
3317
a8c97014
IS
3318static int mlxsw_sp_nexthop_neigh_init(struct mlxsw_sp *mlxsw_sp,
3319 struct mlxsw_sp_nexthop *nh)
a7ff87ac
JP
3320{
3321 struct mlxsw_sp_neigh_entry *neigh_entry;
a7ff87ac 3322 struct neighbour *n;
93a87e5e 3323 u8 nud_state, dead;
c53b8e1b
IS
3324 int err;
3325
ad178c8e 3326 if (!nh->nh_grp->gateway || nh->neigh_entry)
b8399a1e
IS
3327 return 0;
3328
33b1341c 3329 /* Take a reference of neigh here ensuring that neigh would
8de3c178 3330 * not be destructed before the nexthop entry is finished.
33b1341c 3331 * The reference is taken either in neigh_lookup() or
fd76d910 3332 * in neigh_create() in case n is not found.
33b1341c 3333 */
58adf2c4 3334 n = neigh_lookup(nh->nh_grp->neigh_tbl, &nh->gw_addr, nh->rif->dev);
33b1341c 3335 if (!n) {
58adf2c4
IS
3336 n = neigh_create(nh->nh_grp->neigh_tbl, &nh->gw_addr,
3337 nh->rif->dev);
a8c97014
IS
3338 if (IS_ERR(n))
3339 return PTR_ERR(n);
a7ff87ac 3340 neigh_event_send(n, NULL);
33b1341c
JP
3341 }
3342 neigh_entry = mlxsw_sp_neigh_entry_lookup(mlxsw_sp, n);
3343 if (!neigh_entry) {
5c8802f1
IS
3344 neigh_entry = mlxsw_sp_neigh_entry_create(mlxsw_sp, n);
3345 if (IS_ERR(neigh_entry)) {
c53b8e1b
IS
3346 err = -EINVAL;
3347 goto err_neigh_entry_create;
5c8802f1 3348 }
a7ff87ac 3349 }
b2157149
YG
3350
3351 /* If that is the first nexthop connected to that neigh, add to
3352 * nexthop_neighs_list
3353 */
3354 if (list_empty(&neigh_entry->nexthop_list))
3355 list_add_tail(&neigh_entry->nexthop_neighs_list_node,
9011b677 3356 &mlxsw_sp->router->nexthop_neighs_list);
b2157149 3357
a7ff87ac
JP
3358 nh->neigh_entry = neigh_entry;
3359 list_add_tail(&nh->neigh_list_node, &neigh_entry->nexthop_list);
3360 read_lock_bh(&n->lock);
3361 nud_state = n->nud_state;
93a87e5e 3362 dead = n->dead;
a7ff87ac 3363 read_unlock_bh(&n->lock);
93a87e5e 3364 __mlxsw_sp_nexthop_neigh_update(nh, !(nud_state & NUD_VALID && !dead));
a7ff87ac
JP
3365
3366 return 0;
c53b8e1b
IS
3367
3368err_neigh_entry_create:
3369 neigh_release(n);
c53b8e1b 3370 return err;
a7ff87ac
JP
3371}
3372
a8c97014
IS
3373static void mlxsw_sp_nexthop_neigh_fini(struct mlxsw_sp *mlxsw_sp,
3374 struct mlxsw_sp_nexthop *nh)
a7ff87ac
JP
3375{
3376 struct mlxsw_sp_neigh_entry *neigh_entry = nh->neigh_entry;
a8c97014 3377 struct neighbour *n;
a7ff87ac 3378
b8399a1e 3379 if (!neigh_entry)
a8c97014
IS
3380 return;
3381 n = neigh_entry->key.n;
b8399a1e 3382
58312125 3383 __mlxsw_sp_nexthop_neigh_update(nh, true);
a7ff87ac 3384 list_del(&nh->neigh_list_node);
e58be79e 3385 nh->neigh_entry = NULL;
b2157149
YG
3386
3387 /* If that is the last nexthop connected to that neigh, remove from
3388 * nexthop_neighs_list
3389 */
e58be79e
IS
3390 if (list_empty(&neigh_entry->nexthop_list))
3391 list_del(&neigh_entry->nexthop_neighs_list_node);
b2157149 3392
5c8802f1
IS
3393 if (!neigh_entry->connected && list_empty(&neigh_entry->nexthop_list))
3394 mlxsw_sp_neigh_entry_destroy(mlxsw_sp, neigh_entry);
3395
3396 neigh_release(n);
a8c97014 3397}
c53b8e1b 3398
44b0fff1
PM
3399static bool mlxsw_sp_ipip_netdev_ul_up(struct net_device *ol_dev)
3400{
3401 struct net_device *ul_dev = __mlxsw_sp_ipip_netdev_ul_dev_get(ol_dev);
3402
3403 return ul_dev ? (ul_dev->flags & IFF_UP) : true;
3404}
3405
d97cda5f
PM
3406static void mlxsw_sp_nexthop_ipip_init(struct mlxsw_sp *mlxsw_sp,
3407 struct mlxsw_sp_nexthop *nh,
3408 struct mlxsw_sp_ipip_entry *ipip_entry)
1012b9ac 3409{
44b0fff1
PM
3410 bool removing;
3411
1012b9ac 3412 if (!nh->nh_grp->gateway || nh->ipip_entry)
d97cda5f 3413 return;
1012b9ac 3414
d97cda5f
PM
3415 nh->ipip_entry = ipip_entry;
3416 removing = !mlxsw_sp_ipip_netdev_ul_up(ipip_entry->ol_dev);
44b0fff1 3417 __mlxsw_sp_nexthop_neigh_update(nh, removing);
d97cda5f 3418 mlxsw_sp_nexthop_rif_init(nh, &ipip_entry->ol_lb->common);
1012b9ac
PM
3419}
3420
3421static void mlxsw_sp_nexthop_ipip_fini(struct mlxsw_sp *mlxsw_sp,
3422 struct mlxsw_sp_nexthop *nh)
3423{
3424 struct mlxsw_sp_ipip_entry *ipip_entry = nh->ipip_entry;
3425
3426 if (!ipip_entry)
3427 return;
3428
3429 __mlxsw_sp_nexthop_neigh_update(nh, true);
1012b9ac
PM
3430 nh->ipip_entry = NULL;
3431}
3432
3433static bool mlxsw_sp_nexthop4_ipip_type(const struct mlxsw_sp *mlxsw_sp,
3434 const struct fib_nh *fib_nh,
3435 enum mlxsw_sp_ipip_type *p_ipipt)
3436{
3437 struct net_device *dev = fib_nh->nh_dev;
3438
3439 return dev &&
3440 fib_nh->nh_parent->fib_type == RTN_UNICAST &&
3441 mlxsw_sp_netdev_ipip_type(mlxsw_sp, dev, p_ipipt);
3442}
3443
35225e47
PM
3444static void mlxsw_sp_nexthop_type_fini(struct mlxsw_sp *mlxsw_sp,
3445 struct mlxsw_sp_nexthop *nh)
3446{
3447 switch (nh->type) {
3448 case MLXSW_SP_NEXTHOP_TYPE_ETH:
3449 mlxsw_sp_nexthop_neigh_fini(mlxsw_sp, nh);
3450 mlxsw_sp_nexthop_rif_fini(nh);
3451 break;
1012b9ac 3452 case MLXSW_SP_NEXTHOP_TYPE_IPIP:
de0f43c0 3453 mlxsw_sp_nexthop_rif_fini(nh);
1012b9ac
PM
3454 mlxsw_sp_nexthop_ipip_fini(mlxsw_sp, nh);
3455 break;
35225e47
PM
3456 }
3457}
3458
3459static int mlxsw_sp_nexthop4_type_init(struct mlxsw_sp *mlxsw_sp,
3460 struct mlxsw_sp_nexthop *nh,
3461 struct fib_nh *fib_nh)
3462{
d97cda5f 3463 const struct mlxsw_sp_ipip_ops *ipip_ops;
35225e47 3464 struct net_device *dev = fib_nh->nh_dev;
d97cda5f 3465 struct mlxsw_sp_ipip_entry *ipip_entry;
35225e47
PM
3466 struct mlxsw_sp_rif *rif;
3467 int err;
3468
d97cda5f
PM
3469 ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, dev);
3470 if (ipip_entry) {
3471 ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt];
3472 if (ipip_ops->can_offload(mlxsw_sp, dev,
3473 MLXSW_SP_L3_PROTO_IPV4)) {
3474 nh->type = MLXSW_SP_NEXTHOP_TYPE_IPIP;
3475 mlxsw_sp_nexthop_ipip_init(mlxsw_sp, nh, ipip_entry);
3476 return 0;
3477 }
1012b9ac
PM
3478 }
3479
35225e47
PM
3480 nh->type = MLXSW_SP_NEXTHOP_TYPE_ETH;
3481 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
3482 if (!rif)
3483 return 0;
3484
3485 mlxsw_sp_nexthop_rif_init(nh, rif);
3486 err = mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh);
3487 if (err)
3488 goto err_neigh_init;
3489
3490 return 0;
3491
3492err_neigh_init:
3493 mlxsw_sp_nexthop_rif_fini(nh);
3494 return err;
3495}
3496
3497static void mlxsw_sp_nexthop4_type_fini(struct mlxsw_sp *mlxsw_sp,
3498 struct mlxsw_sp_nexthop *nh)
3499{
3500 mlxsw_sp_nexthop_type_fini(mlxsw_sp, nh);
3501}
3502
0e6ea2a4
IS
3503static int mlxsw_sp_nexthop4_init(struct mlxsw_sp *mlxsw_sp,
3504 struct mlxsw_sp_nexthop_group *nh_grp,
3505 struct mlxsw_sp_nexthop *nh,
3506 struct fib_nh *fib_nh)
a8c97014
IS
3507{
3508 struct net_device *dev = fib_nh->nh_dev;
df6dd79b 3509 struct in_device *in_dev;
a8c97014
IS
3510 int err;
3511
3512 nh->nh_grp = nh_grp;
3513 nh->key.fib_nh = fib_nh;
408bd946
IS
3514#ifdef CONFIG_IP_ROUTE_MULTIPATH
3515 nh->nh_weight = fib_nh->nh_weight;
3516#else
3517 nh->nh_weight = 1;
3518#endif
58adf2c4 3519 memcpy(&nh->gw_addr, &fib_nh->nh_gw, sizeof(fib_nh->nh_gw));
a8c97014
IS
3520 err = mlxsw_sp_nexthop_insert(mlxsw_sp, nh);
3521 if (err)
3522 return err;
3523
a5390278 3524 mlxsw_sp_nexthop_counter_alloc(mlxsw_sp, nh);
dbe4598c
AS
3525 list_add_tail(&nh->router_list_node, &mlxsw_sp->router->nexthop_list);
3526
97989ee0
IS
3527 if (!dev)
3528 return 0;
3529
df6dd79b
IS
3530 in_dev = __in_dev_get_rtnl(dev);
3531 if (in_dev && IN_DEV_IGNORE_ROUTES_WITH_LINKDOWN(in_dev) &&
3532 fib_nh->nh_flags & RTNH_F_LINKDOWN)
3533 return 0;
3534
35225e47 3535 err = mlxsw_sp_nexthop4_type_init(mlxsw_sp, nh, fib_nh);
a8c97014
IS
3536 if (err)
3537 goto err_nexthop_neigh_init;
3538
3539 return 0;
3540
3541err_nexthop_neigh_init:
3542 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
3543 return err;
3544}
3545
0e6ea2a4
IS
3546static void mlxsw_sp_nexthop4_fini(struct mlxsw_sp *mlxsw_sp,
3547 struct mlxsw_sp_nexthop *nh)
a8c97014 3548{
35225e47 3549 mlxsw_sp_nexthop4_type_fini(mlxsw_sp, nh);
dbe4598c 3550 list_del(&nh->router_list_node);
a5390278 3551 mlxsw_sp_nexthop_counter_free(mlxsw_sp, nh);
c53b8e1b 3552 mlxsw_sp_nexthop_remove(mlxsw_sp, nh);
a7ff87ac
JP
3553}
3554
0e6ea2a4
IS
3555static void mlxsw_sp_nexthop4_event(struct mlxsw_sp *mlxsw_sp,
3556 unsigned long event, struct fib_nh *fib_nh)
ad178c8e
IS
3557{
3558 struct mlxsw_sp_nexthop_key key;
3559 struct mlxsw_sp_nexthop *nh;
ad178c8e 3560
9011b677 3561 if (mlxsw_sp->router->aborted)
ad178c8e
IS
3562 return;
3563
3564 key.fib_nh = fib_nh;
3565 nh = mlxsw_sp_nexthop_lookup(mlxsw_sp, key);
3566 if (WARN_ON_ONCE(!nh))
3567 return;
3568
ad178c8e
IS
3569 switch (event) {
3570 case FIB_EVENT_NH_ADD:
35225e47 3571 mlxsw_sp_nexthop4_type_init(mlxsw_sp, nh, fib_nh);
ad178c8e
IS
3572 break;
3573 case FIB_EVENT_NH_DEL:
35225e47 3574 mlxsw_sp_nexthop4_type_fini(mlxsw_sp, nh);
ad178c8e
IS
3575 break;
3576 }
3577
3578 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
3579}
3580
0c5f1cd5
PM
3581static void mlxsw_sp_nexthop_rif_update(struct mlxsw_sp *mlxsw_sp,
3582 struct mlxsw_sp_rif *rif)
3583{
3584 struct mlxsw_sp_nexthop *nh;
44b0fff1 3585 bool removing;
0c5f1cd5
PM
3586
3587 list_for_each_entry(nh, &rif->nexthop_list, rif_list_node) {
44b0fff1
PM
3588 switch (nh->type) {
3589 case MLXSW_SP_NEXTHOP_TYPE_ETH:
3590 removing = false;
3591 break;
3592 case MLXSW_SP_NEXTHOP_TYPE_IPIP:
3593 removing = !mlxsw_sp_ipip_netdev_ul_up(rif->dev);
3594 break;
3595 default:
3596 WARN_ON(1);
3597 continue;
3598 }
3599
3600 __mlxsw_sp_nexthop_neigh_update(nh, removing);
0c5f1cd5
PM
3601 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
3602 }
3603}
3604
09dbf629
PM
3605static void mlxsw_sp_nexthop_rif_migrate(struct mlxsw_sp *mlxsw_sp,
3606 struct mlxsw_sp_rif *old_rif,
3607 struct mlxsw_sp_rif *new_rif)
3608{
3609 struct mlxsw_sp_nexthop *nh;
3610
3611 list_splice_init(&old_rif->nexthop_list, &new_rif->nexthop_list);
3612 list_for_each_entry(nh, &new_rif->nexthop_list, rif_list_node)
3613 nh->rif = new_rif;
3614 mlxsw_sp_nexthop_rif_update(mlxsw_sp, new_rif);
3615}
3616
9665b745 3617static void mlxsw_sp_nexthop_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
bf95233e 3618 struct mlxsw_sp_rif *rif)
9665b745
IS
3619{
3620 struct mlxsw_sp_nexthop *nh, *tmp;
3621
bf95233e 3622 list_for_each_entry_safe(nh, tmp, &rif->nexthop_list, rif_list_node) {
35225e47 3623 mlxsw_sp_nexthop_type_fini(mlxsw_sp, nh);
9665b745
IS
3624 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh->nh_grp);
3625 }
3626}
3627
9b01451a
PM
3628static bool mlxsw_sp_fi_is_gateway(const struct mlxsw_sp *mlxsw_sp,
3629 const struct fib_info *fi)
3630{
1012b9ac
PM
3631 return fi->fib_nh->nh_scope == RT_SCOPE_LINK ||
3632 mlxsw_sp_nexthop4_ipip_type(mlxsw_sp, fi->fib_nh, NULL);
9b01451a
PM
3633}
3634
a7ff87ac 3635static struct mlxsw_sp_nexthop_group *
0e6ea2a4 3636mlxsw_sp_nexthop4_group_create(struct mlxsw_sp *mlxsw_sp, struct fib_info *fi)
a7ff87ac
JP
3637{
3638 struct mlxsw_sp_nexthop_group *nh_grp;
3639 struct mlxsw_sp_nexthop *nh;
3640 struct fib_nh *fib_nh;
3641 size_t alloc_size;
3642 int i;
3643 int err;
3644
3645 alloc_size = sizeof(*nh_grp) +
3646 fi->fib_nhs * sizeof(struct mlxsw_sp_nexthop);
3647 nh_grp = kzalloc(alloc_size, GFP_KERNEL);
3648 if (!nh_grp)
3649 return ERR_PTR(-ENOMEM);
ba31d366 3650 nh_grp->priv = fi;
a7ff87ac 3651 INIT_LIST_HEAD(&nh_grp->fib_list);
58adf2c4
IS
3652 nh_grp->neigh_tbl = &arp_tbl;
3653
9b01451a 3654 nh_grp->gateway = mlxsw_sp_fi_is_gateway(mlxsw_sp, fi);
a7ff87ac 3655 nh_grp->count = fi->fib_nhs;
7387dbbc 3656 fib_info_hold(fi);
a7ff87ac
JP
3657 for (i = 0; i < nh_grp->count; i++) {
3658 nh = &nh_grp->nexthops[i];
3659 fib_nh = &fi->fib_nh[i];
0e6ea2a4 3660 err = mlxsw_sp_nexthop4_init(mlxsw_sp, nh_grp, nh, fib_nh);
a7ff87ac 3661 if (err)
0e6ea2a4 3662 goto err_nexthop4_init;
a7ff87ac 3663 }
e9ad5e7d
IS
3664 err = mlxsw_sp_nexthop_group_insert(mlxsw_sp, nh_grp);
3665 if (err)
3666 goto err_nexthop_group_insert;
a7ff87ac
JP
3667 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
3668 return nh_grp;
3669
e9ad5e7d 3670err_nexthop_group_insert:
0e6ea2a4 3671err_nexthop4_init:
df6dd79b
IS
3672 for (i--; i >= 0; i--) {
3673 nh = &nh_grp->nexthops[i];
0e6ea2a4 3674 mlxsw_sp_nexthop4_fini(mlxsw_sp, nh);
df6dd79b 3675 }
ba31d366 3676 fib_info_put(fi);
a7ff87ac
JP
3677 kfree(nh_grp);
3678 return ERR_PTR(err);
3679}
3680
3681static void
0e6ea2a4
IS
3682mlxsw_sp_nexthop4_group_destroy(struct mlxsw_sp *mlxsw_sp,
3683 struct mlxsw_sp_nexthop_group *nh_grp)
a7ff87ac
JP
3684{
3685 struct mlxsw_sp_nexthop *nh;
3686 int i;
3687
e9ad5e7d 3688 mlxsw_sp_nexthop_group_remove(mlxsw_sp, nh_grp);
a7ff87ac
JP
3689 for (i = 0; i < nh_grp->count; i++) {
3690 nh = &nh_grp->nexthops[i];
0e6ea2a4 3691 mlxsw_sp_nexthop4_fini(mlxsw_sp, nh);
a7ff87ac 3692 }
58312125
IS
3693 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
3694 WARN_ON_ONCE(nh_grp->adj_index_valid);
ba31d366 3695 fib_info_put(mlxsw_sp_nexthop4_group_fi(nh_grp));
a7ff87ac
JP
3696 kfree(nh_grp);
3697}
3698
0e6ea2a4
IS
3699static int mlxsw_sp_nexthop4_group_get(struct mlxsw_sp *mlxsw_sp,
3700 struct mlxsw_sp_fib_entry *fib_entry,
3701 struct fib_info *fi)
a7ff87ac
JP
3702{
3703 struct mlxsw_sp_nexthop_group *nh_grp;
3704
ba31d366 3705 nh_grp = mlxsw_sp_nexthop4_group_lookup(mlxsw_sp, fi);
a7ff87ac 3706 if (!nh_grp) {
0e6ea2a4 3707 nh_grp = mlxsw_sp_nexthop4_group_create(mlxsw_sp, fi);
a7ff87ac
JP
3708 if (IS_ERR(nh_grp))
3709 return PTR_ERR(nh_grp);
3710 }
3711 list_add_tail(&fib_entry->nexthop_group_node, &nh_grp->fib_list);
3712 fib_entry->nh_group = nh_grp;
3713 return 0;
3714}
3715
0e6ea2a4
IS
3716static void mlxsw_sp_nexthop4_group_put(struct mlxsw_sp *mlxsw_sp,
3717 struct mlxsw_sp_fib_entry *fib_entry)
a7ff87ac
JP
3718{
3719 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
3720
3721 list_del(&fib_entry->nexthop_group_node);
3722 if (!list_empty(&nh_grp->fib_list))
3723 return;
0e6ea2a4 3724 mlxsw_sp_nexthop4_group_destroy(mlxsw_sp, nh_grp);
a7ff87ac
JP
3725}
3726
4f1c7f1f
IS
3727static bool
3728mlxsw_sp_fib4_entry_should_offload(const struct mlxsw_sp_fib_entry *fib_entry)
3729{
3730 struct mlxsw_sp_fib4_entry *fib4_entry;
3731
3732 fib4_entry = container_of(fib_entry, struct mlxsw_sp_fib4_entry,
3733 common);
3734 return !fib4_entry->tos;
3735}
3736
013b20f9
IS
3737static bool
3738mlxsw_sp_fib_entry_should_offload(const struct mlxsw_sp_fib_entry *fib_entry)
3739{
3740 struct mlxsw_sp_nexthop_group *nh_group = fib_entry->nh_group;
3741
4f1c7f1f
IS
3742 switch (fib_entry->fib_node->fib->proto) {
3743 case MLXSW_SP_L3_PROTO_IPV4:
3744 if (!mlxsw_sp_fib4_entry_should_offload(fib_entry))
3745 return false;
3746 break;
3747 case MLXSW_SP_L3_PROTO_IPV6:
3748 break;
3749 }
9aecce1c 3750
013b20f9
IS
3751 switch (fib_entry->type) {
3752 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
3753 return !!nh_group->adj_index_valid;
3754 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
70ad3506 3755 return !!nh_group->nh_rif;
4607f6d2
PM
3756 case MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP:
3757 return true;
013b20f9
IS
3758 default:
3759 return false;
3760 }
3761}
3762
428b851f
IS
3763static struct mlxsw_sp_nexthop *
3764mlxsw_sp_rt6_nexthop(struct mlxsw_sp_nexthop_group *nh_grp,
3765 const struct mlxsw_sp_rt6 *mlxsw_sp_rt6)
3766{
3767 int i;
3768
3769 for (i = 0; i < nh_grp->count; i++) {
3770 struct mlxsw_sp_nexthop *nh = &nh_grp->nexthops[i];
3771 struct rt6_info *rt = mlxsw_sp_rt6->rt;
3772
3773 if (nh->rif && nh->rif->dev == rt->dst.dev &&
3774 ipv6_addr_equal((const struct in6_addr *) &nh->gw_addr,
3775 &rt->rt6i_gateway))
3776 return nh;
3777 continue;
3778 }
3779
3780 return NULL;
3781}
3782
3984d1a8
IS
3783static void
3784mlxsw_sp_fib4_entry_offload_set(struct mlxsw_sp_fib_entry *fib_entry)
3785{
3786 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
3787 int i;
3788
4607f6d2
PM
3789 if (fib_entry->type == MLXSW_SP_FIB_ENTRY_TYPE_LOCAL ||
3790 fib_entry->type == MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP) {
3984d1a8
IS
3791 nh_grp->nexthops->key.fib_nh->nh_flags |= RTNH_F_OFFLOAD;
3792 return;
3793 }
3794
3795 for (i = 0; i < nh_grp->count; i++) {
3796 struct mlxsw_sp_nexthop *nh = &nh_grp->nexthops[i];
3797
3798 if (nh->offloaded)
3799 nh->key.fib_nh->nh_flags |= RTNH_F_OFFLOAD;
3800 else
3801 nh->key.fib_nh->nh_flags &= ~RTNH_F_OFFLOAD;
3802 }
3803}
3804
3805static void
3806mlxsw_sp_fib4_entry_offload_unset(struct mlxsw_sp_fib_entry *fib_entry)
3807{
3808 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
3809 int i;
3810
d1c95af3
IS
3811 if (!list_is_singular(&nh_grp->fib_list))
3812 return;
3813
3984d1a8
IS
3814 for (i = 0; i < nh_grp->count; i++) {
3815 struct mlxsw_sp_nexthop *nh = &nh_grp->nexthops[i];
3816
3817 nh->key.fib_nh->nh_flags &= ~RTNH_F_OFFLOAD;
3818 }
3819}
3820
428b851f
IS
3821static void
3822mlxsw_sp_fib6_entry_offload_set(struct mlxsw_sp_fib_entry *fib_entry)
3823{
3824 struct mlxsw_sp_fib6_entry *fib6_entry;
3825 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
3826
3827 fib6_entry = container_of(fib_entry, struct mlxsw_sp_fib6_entry,
3828 common);
3829
3830 if (fib_entry->type == MLXSW_SP_FIB_ENTRY_TYPE_LOCAL) {
3831 list_first_entry(&fib6_entry->rt6_list, struct mlxsw_sp_rt6,
fe400799 3832 list)->rt->rt6i_nh_flags |= RTNH_F_OFFLOAD;
428b851f
IS
3833 return;
3834 }
3835
3836 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
3837 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
3838 struct mlxsw_sp_nexthop *nh;
3839
3840 nh = mlxsw_sp_rt6_nexthop(nh_grp, mlxsw_sp_rt6);
3841 if (nh && nh->offloaded)
fe400799 3842 mlxsw_sp_rt6->rt->rt6i_nh_flags |= RTNH_F_OFFLOAD;
428b851f 3843 else
fe400799 3844 mlxsw_sp_rt6->rt->rt6i_nh_flags &= ~RTNH_F_OFFLOAD;
428b851f
IS
3845 }
3846}
3847
3848static void
3849mlxsw_sp_fib6_entry_offload_unset(struct mlxsw_sp_fib_entry *fib_entry)
3850{
3851 struct mlxsw_sp_fib6_entry *fib6_entry;
3852 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
3853
3854 fib6_entry = container_of(fib_entry, struct mlxsw_sp_fib6_entry,
3855 common);
3856 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
3857 struct rt6_info *rt = mlxsw_sp_rt6->rt;
3858
fe400799 3859 rt->rt6i_nh_flags &= ~RTNH_F_OFFLOAD;
428b851f
IS
3860 }
3861}
3862
013b20f9
IS
3863static void mlxsw_sp_fib_entry_offload_set(struct mlxsw_sp_fib_entry *fib_entry)
3864{
76610ebb 3865 switch (fib_entry->fib_node->fib->proto) {
013b20f9 3866 case MLXSW_SP_L3_PROTO_IPV4:
3984d1a8 3867 mlxsw_sp_fib4_entry_offload_set(fib_entry);
013b20f9
IS
3868 break;
3869 case MLXSW_SP_L3_PROTO_IPV6:
428b851f
IS
3870 mlxsw_sp_fib6_entry_offload_set(fib_entry);
3871 break;
013b20f9
IS
3872 }
3873}
3874
3875static void
3876mlxsw_sp_fib_entry_offload_unset(struct mlxsw_sp_fib_entry *fib_entry)
3877{
76610ebb 3878 switch (fib_entry->fib_node->fib->proto) {
013b20f9 3879 case MLXSW_SP_L3_PROTO_IPV4:
3984d1a8 3880 mlxsw_sp_fib4_entry_offload_unset(fib_entry);
013b20f9
IS
3881 break;
3882 case MLXSW_SP_L3_PROTO_IPV6:
428b851f
IS
3883 mlxsw_sp_fib6_entry_offload_unset(fib_entry);
3884 break;
013b20f9 3885 }
013b20f9
IS
3886}
3887
3888static void
3889mlxsw_sp_fib_entry_offload_refresh(struct mlxsw_sp_fib_entry *fib_entry,
3890 enum mlxsw_reg_ralue_op op, int err)
3891{
3892 switch (op) {
3893 case MLXSW_REG_RALUE_OP_WRITE_DELETE:
013b20f9
IS
3894 return mlxsw_sp_fib_entry_offload_unset(fib_entry);
3895 case MLXSW_REG_RALUE_OP_WRITE_WRITE:
3896 if (err)
3897 return;
1353ee70 3898 if (mlxsw_sp_fib_entry_should_offload(fib_entry))
013b20f9 3899 mlxsw_sp_fib_entry_offload_set(fib_entry);
85f44a15 3900 else
013b20f9
IS
3901 mlxsw_sp_fib_entry_offload_unset(fib_entry);
3902 return;
3903 default:
3904 return;
3905 }
3906}
3907
9dbf4d76
IS
3908static void
3909mlxsw_sp_fib_entry_ralue_pack(char *ralue_pl,
3910 const struct mlxsw_sp_fib_entry *fib_entry,
3911 enum mlxsw_reg_ralue_op op)
a7ff87ac 3912{
76610ebb 3913 struct mlxsw_sp_fib *fib = fib_entry->fib_node->fib;
9dbf4d76
IS
3914 enum mlxsw_reg_ralxx_protocol proto;
3915 u32 *p_dip;
3916
3917 proto = (enum mlxsw_reg_ralxx_protocol) fib->proto;
3918
3919 switch (fib->proto) {
3920 case MLXSW_SP_L3_PROTO_IPV4:
3921 p_dip = (u32 *) fib_entry->fib_node->key.addr;
3922 mlxsw_reg_ralue_pack4(ralue_pl, proto, op, fib->vr->id,
3923 fib_entry->fib_node->key.prefix_len,
3924 *p_dip);
3925 break;
3926 case MLXSW_SP_L3_PROTO_IPV6:
3927 mlxsw_reg_ralue_pack6(ralue_pl, proto, op, fib->vr->id,
3928 fib_entry->fib_node->key.prefix_len,
3929 fib_entry->fib_node->key.addr);
3930 break;
3931 }
3932}
3933
3934static int mlxsw_sp_fib_entry_op_remote(struct mlxsw_sp *mlxsw_sp,
3935 struct mlxsw_sp_fib_entry *fib_entry,
3936 enum mlxsw_reg_ralue_op op)
3937{
3938 char ralue_pl[MLXSW_REG_RALUE_LEN];
a7ff87ac
JP
3939 enum mlxsw_reg_ralue_trap_action trap_action;
3940 u16 trap_id = 0;
3941 u32 adjacency_index = 0;
3942 u16 ecmp_size = 0;
3943
3944 /* In case the nexthop group adjacency index is valid, use it
3945 * with provided ECMP size. Otherwise, setup trap and pass
3946 * traffic to kernel.
3947 */
4b411477 3948 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
a7ff87ac
JP
3949 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
3950 adjacency_index = fib_entry->nh_group->adj_index;
3951 ecmp_size = fib_entry->nh_group->ecmp_size;
3952 } else {
3953 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
3954 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
3955 }
3956
9dbf4d76 3957 mlxsw_sp_fib_entry_ralue_pack(ralue_pl, fib_entry, op);
a7ff87ac
JP
3958 mlxsw_reg_ralue_act_remote_pack(ralue_pl, trap_action, trap_id,
3959 adjacency_index, ecmp_size);
3960 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
3961}
3962
9dbf4d76
IS
3963static int mlxsw_sp_fib_entry_op_local(struct mlxsw_sp *mlxsw_sp,
3964 struct mlxsw_sp_fib_entry *fib_entry,
3965 enum mlxsw_reg_ralue_op op)
61c503f9 3966{
bf95233e 3967 struct mlxsw_sp_rif *rif = fib_entry->nh_group->nh_rif;
70ad3506 3968 enum mlxsw_reg_ralue_trap_action trap_action;
61c503f9 3969 char ralue_pl[MLXSW_REG_RALUE_LEN];
70ad3506 3970 u16 trap_id = 0;
bf95233e 3971 u16 rif_index = 0;
70ad3506
IS
3972
3973 if (mlxsw_sp_fib_entry_should_offload(fib_entry)) {
3974 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_NOP;
bf95233e 3975 rif_index = rif->rif_index;
70ad3506
IS
3976 } else {
3977 trap_action = MLXSW_REG_RALUE_TRAP_ACTION_TRAP;
3978 trap_id = MLXSW_TRAP_ID_RTR_INGRESS0;
3979 }
61c503f9 3980
9dbf4d76 3981 mlxsw_sp_fib_entry_ralue_pack(ralue_pl, fib_entry, op);
bf95233e
AS
3982 mlxsw_reg_ralue_act_local_pack(ralue_pl, trap_action, trap_id,
3983 rif_index);
61c503f9
JP
3984 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
3985}
3986
9dbf4d76
IS
3987static int mlxsw_sp_fib_entry_op_trap(struct mlxsw_sp *mlxsw_sp,
3988 struct mlxsw_sp_fib_entry *fib_entry,
3989 enum mlxsw_reg_ralue_op op)
61c503f9
JP
3990{
3991 char ralue_pl[MLXSW_REG_RALUE_LEN];
61c503f9 3992
9dbf4d76 3993 mlxsw_sp_fib_entry_ralue_pack(ralue_pl, fib_entry, op);
61c503f9
JP
3994 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
3995 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue), ralue_pl);
3996}
3997
4607f6d2
PM
3998static int
3999mlxsw_sp_fib_entry_op_ipip_decap(struct mlxsw_sp *mlxsw_sp,
4000 struct mlxsw_sp_fib_entry *fib_entry,
4001 enum mlxsw_reg_ralue_op op)
4002{
4003 struct mlxsw_sp_ipip_entry *ipip_entry = fib_entry->decap.ipip_entry;
4004 const struct mlxsw_sp_ipip_ops *ipip_ops;
4005
4006 if (WARN_ON(!ipip_entry))
4007 return -EINVAL;
4008
4009 ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt];
4010 return ipip_ops->fib_entry_op(mlxsw_sp, ipip_entry, op,
4011 fib_entry->decap.tunnel_index);
4012}
4013
9dbf4d76
IS
4014static int __mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
4015 struct mlxsw_sp_fib_entry *fib_entry,
4016 enum mlxsw_reg_ralue_op op)
61c503f9
JP
4017{
4018 switch (fib_entry->type) {
4019 case MLXSW_SP_FIB_ENTRY_TYPE_REMOTE:
9dbf4d76 4020 return mlxsw_sp_fib_entry_op_remote(mlxsw_sp, fib_entry, op);
61c503f9 4021 case MLXSW_SP_FIB_ENTRY_TYPE_LOCAL:
9dbf4d76 4022 return mlxsw_sp_fib_entry_op_local(mlxsw_sp, fib_entry, op);
61c503f9 4023 case MLXSW_SP_FIB_ENTRY_TYPE_TRAP:
9dbf4d76 4024 return mlxsw_sp_fib_entry_op_trap(mlxsw_sp, fib_entry, op);
4607f6d2
PM
4025 case MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP:
4026 return mlxsw_sp_fib_entry_op_ipip_decap(mlxsw_sp,
4027 fib_entry, op);
61c503f9
JP
4028 }
4029 return -EINVAL;
4030}
4031
4032static int mlxsw_sp_fib_entry_op(struct mlxsw_sp *mlxsw_sp,
4033 struct mlxsw_sp_fib_entry *fib_entry,
4034 enum mlxsw_reg_ralue_op op)
4035{
9dbf4d76 4036 int err = __mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry, op);
013b20f9 4037
013b20f9 4038 mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, err);
9dbf4d76 4039
013b20f9 4040 return err;
61c503f9
JP
4041}
4042
4043static int mlxsw_sp_fib_entry_update(struct mlxsw_sp *mlxsw_sp,
4044 struct mlxsw_sp_fib_entry *fib_entry)
4045{
7146da31
JP
4046 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
4047 MLXSW_REG_RALUE_OP_WRITE_WRITE);
61c503f9
JP
4048}
4049
4050static int mlxsw_sp_fib_entry_del(struct mlxsw_sp *mlxsw_sp,
4051 struct mlxsw_sp_fib_entry *fib_entry)
4052{
4053 return mlxsw_sp_fib_entry_op(mlxsw_sp, fib_entry,
4054 MLXSW_REG_RALUE_OP_WRITE_DELETE);
4055}
4056
61c503f9 4057static int
013b20f9
IS
4058mlxsw_sp_fib4_entry_type_set(struct mlxsw_sp *mlxsw_sp,
4059 const struct fib_entry_notifier_info *fen_info,
4060 struct mlxsw_sp_fib_entry *fib_entry)
61c503f9 4061{
4607f6d2
PM
4062 union mlxsw_sp_l3addr dip = { .addr4 = htonl(fen_info->dst) };
4063 struct net_device *dev = fen_info->fi->fib_dev;
4064 struct mlxsw_sp_ipip_entry *ipip_entry;
b45f64d1 4065 struct fib_info *fi = fen_info->fi;
61c503f9 4066
97989ee0 4067 switch (fen_info->type) {
97989ee0 4068 case RTN_LOCAL:
4607f6d2
PM
4069 ipip_entry = mlxsw_sp_ipip_entry_find_by_decap(mlxsw_sp, dev,
4070 MLXSW_SP_L3_PROTO_IPV4, dip);
57c77ce4 4071 if (ipip_entry && ipip_entry->ol_dev->flags & IFF_UP) {
4607f6d2
PM
4072 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP;
4073 return mlxsw_sp_fib_entry_decap_init(mlxsw_sp,
4074 fib_entry,
4075 ipip_entry);
4076 }
4077 /* fall through */
4078 case RTN_BROADCAST:
61c503f9
JP
4079 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
4080 return 0;
97989ee0
IS
4081 case RTN_UNREACHABLE: /* fall through */
4082 case RTN_BLACKHOLE: /* fall through */
4083 case RTN_PROHIBIT:
4084 /* Packets hitting these routes need to be trapped, but
4085 * can do so with a lower priority than packets directed
4086 * at the host, so use action type local instead of trap.
4087 */
61c503f9 4088 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
97989ee0
IS
4089 return 0;
4090 case RTN_UNICAST:
9b01451a 4091 if (mlxsw_sp_fi_is_gateway(mlxsw_sp, fi))
97989ee0 4092 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
9b01451a
PM
4093 else
4094 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
97989ee0
IS
4095 return 0;
4096 default:
4097 return -EINVAL;
4098 }
a7ff87ac
JP
4099}
4100
4f1c7f1f 4101static struct mlxsw_sp_fib4_entry *
9aecce1c
IS
4102mlxsw_sp_fib4_entry_create(struct mlxsw_sp *mlxsw_sp,
4103 struct mlxsw_sp_fib_node *fib_node,
4104 const struct fib_entry_notifier_info *fen_info)
61c503f9 4105{
4f1c7f1f 4106 struct mlxsw_sp_fib4_entry *fib4_entry;
61c503f9 4107 struct mlxsw_sp_fib_entry *fib_entry;
61c503f9
JP
4108 int err;
4109
4f1c7f1f
IS
4110 fib4_entry = kzalloc(sizeof(*fib4_entry), GFP_KERNEL);
4111 if (!fib4_entry)
4112 return ERR_PTR(-ENOMEM);
4113 fib_entry = &fib4_entry->common;
61c503f9 4114
013b20f9 4115 err = mlxsw_sp_fib4_entry_type_set(mlxsw_sp, fen_info, fib_entry);
61c503f9 4116 if (err)
013b20f9 4117 goto err_fib4_entry_type_set;
61c503f9 4118
0e6ea2a4 4119 err = mlxsw_sp_nexthop4_group_get(mlxsw_sp, fib_entry, fen_info->fi);
b8399a1e 4120 if (err)
0e6ea2a4 4121 goto err_nexthop4_group_get;
b8399a1e 4122
4f1c7f1f
IS
4123 fib4_entry->prio = fen_info->fi->fib_priority;
4124 fib4_entry->tb_id = fen_info->tb_id;
4125 fib4_entry->type = fen_info->type;
4126 fib4_entry->tos = fen_info->tos;
9aecce1c
IS
4127
4128 fib_entry->fib_node = fib_node;
4129
4f1c7f1f 4130 return fib4_entry;
5b004412 4131
0e6ea2a4 4132err_nexthop4_group_get:
013b20f9 4133err_fib4_entry_type_set:
4f1c7f1f 4134 kfree(fib4_entry);
5b004412
JP
4135 return ERR_PTR(err);
4136}
4137
9aecce1c 4138static void mlxsw_sp_fib4_entry_destroy(struct mlxsw_sp *mlxsw_sp,
4f1c7f1f 4139 struct mlxsw_sp_fib4_entry *fib4_entry)
9aecce1c 4140{
0e6ea2a4 4141 mlxsw_sp_nexthop4_group_put(mlxsw_sp, &fib4_entry->common);
4f1c7f1f 4142 kfree(fib4_entry);
9aecce1c
IS
4143}
4144
4f1c7f1f 4145static struct mlxsw_sp_fib4_entry *
9aecce1c
IS
4146mlxsw_sp_fib4_entry_lookup(struct mlxsw_sp *mlxsw_sp,
4147 const struct fib_entry_notifier_info *fen_info)
5b004412 4148{
4f1c7f1f 4149 struct mlxsw_sp_fib4_entry *fib4_entry;
9aecce1c 4150 struct mlxsw_sp_fib_node *fib_node;
160e22aa
IS
4151 struct mlxsw_sp_fib *fib;
4152 struct mlxsw_sp_vr *vr;
4153
4154 vr = mlxsw_sp_vr_find(mlxsw_sp, fen_info->tb_id);
4155 if (!vr)
4156 return NULL;
4157 fib = mlxsw_sp_vr_fib(vr, MLXSW_SP_L3_PROTO_IPV4);
5b004412 4158
160e22aa
IS
4159 fib_node = mlxsw_sp_fib_node_lookup(fib, &fen_info->dst,
4160 sizeof(fen_info->dst),
4161 fen_info->dst_len);
4162 if (!fib_node)
9aecce1c
IS
4163 return NULL;
4164
4f1c7f1f
IS
4165 list_for_each_entry(fib4_entry, &fib_node->entry_list, common.list) {
4166 if (fib4_entry->tb_id == fen_info->tb_id &&
4167 fib4_entry->tos == fen_info->tos &&
4168 fib4_entry->type == fen_info->type &&
ba31d366
AS
4169 mlxsw_sp_nexthop4_group_fi(fib4_entry->common.nh_group) ==
4170 fen_info->fi) {
4f1c7f1f 4171 return fib4_entry;
9aecce1c
IS
4172 }
4173 }
4174
4175 return NULL;
4176}
4177
4178static const struct rhashtable_params mlxsw_sp_fib_ht_params = {
4179 .key_offset = offsetof(struct mlxsw_sp_fib_node, key),
4180 .head_offset = offsetof(struct mlxsw_sp_fib_node, ht_node),
4181 .key_len = sizeof(struct mlxsw_sp_fib_key),
4182 .automatic_shrinking = true,
4183};
4184
4185static int mlxsw_sp_fib_node_insert(struct mlxsw_sp_fib *fib,
4186 struct mlxsw_sp_fib_node *fib_node)
4187{
4188 return rhashtable_insert_fast(&fib->ht, &fib_node->ht_node,
4189 mlxsw_sp_fib_ht_params);
4190}
4191
4192static void mlxsw_sp_fib_node_remove(struct mlxsw_sp_fib *fib,
4193 struct mlxsw_sp_fib_node *fib_node)
4194{
4195 rhashtable_remove_fast(&fib->ht, &fib_node->ht_node,
4196 mlxsw_sp_fib_ht_params);
4197}
4198
4199static struct mlxsw_sp_fib_node *
4200mlxsw_sp_fib_node_lookup(struct mlxsw_sp_fib *fib, const void *addr,
4201 size_t addr_len, unsigned char prefix_len)
4202{
4203 struct mlxsw_sp_fib_key key;
4204
4205 memset(&key, 0, sizeof(key));
4206 memcpy(key.addr, addr, addr_len);
4207 key.prefix_len = prefix_len;
4208 return rhashtable_lookup_fast(&fib->ht, &key, mlxsw_sp_fib_ht_params);
4209}
4210
4211static struct mlxsw_sp_fib_node *
76610ebb 4212mlxsw_sp_fib_node_create(struct mlxsw_sp_fib *fib, const void *addr,
9aecce1c
IS
4213 size_t addr_len, unsigned char prefix_len)
4214{
4215 struct mlxsw_sp_fib_node *fib_node;
4216
4217 fib_node = kzalloc(sizeof(*fib_node), GFP_KERNEL);
4218 if (!fib_node)
5b004412
JP
4219 return NULL;
4220
9aecce1c 4221 INIT_LIST_HEAD(&fib_node->entry_list);
76610ebb 4222 list_add(&fib_node->list, &fib->node_list);
9aecce1c
IS
4223 memcpy(fib_node->key.addr, addr, addr_len);
4224 fib_node->key.prefix_len = prefix_len;
9aecce1c
IS
4225
4226 return fib_node;
4227}
4228
4229static void mlxsw_sp_fib_node_destroy(struct mlxsw_sp_fib_node *fib_node)
4230{
9aecce1c
IS
4231 list_del(&fib_node->list);
4232 WARN_ON(!list_empty(&fib_node->entry_list));
4233 kfree(fib_node);
4234}
4235
4236static bool
4237mlxsw_sp_fib_node_entry_is_first(const struct mlxsw_sp_fib_node *fib_node,
4238 const struct mlxsw_sp_fib_entry *fib_entry)
4239{
4240 return list_first_entry(&fib_node->entry_list,
4241 struct mlxsw_sp_fib_entry, list) == fib_entry;
4242}
4243
fc922bb0 4244static int mlxsw_sp_fib_lpm_tree_link(struct mlxsw_sp *mlxsw_sp,
fc922bb0
IS
4245 struct mlxsw_sp_fib_node *fib_node)
4246{
2b52ce02 4247 struct mlxsw_sp_prefix_usage req_prefix_usage;
3aad95df 4248 struct mlxsw_sp_fib *fib = fib_node->fib;
fc922bb0
IS
4249 struct mlxsw_sp_lpm_tree *lpm_tree;
4250 int err;
4251
2b52ce02
IS
4252 lpm_tree = mlxsw_sp->router->lpm.proto_trees[fib->proto];
4253 if (lpm_tree->prefix_ref_count[fib_node->key.prefix_len] != 0)
4254 goto out;
fc922bb0 4255
2b52ce02
IS
4256 mlxsw_sp_prefix_usage_cpy(&req_prefix_usage, &lpm_tree->prefix_usage);
4257 mlxsw_sp_prefix_usage_set(&req_prefix_usage, fib_node->key.prefix_len);
fc922bb0
IS
4258 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
4259 fib->proto);
4260 if (IS_ERR(lpm_tree))
4261 return PTR_ERR(lpm_tree);
4262
fc922bb0
IS
4263 err = mlxsw_sp_vrs_lpm_tree_replace(mlxsw_sp, fib, lpm_tree);
4264 if (err)
2b52ce02 4265 goto err_lpm_tree_replace;
fc922bb0 4266
2b52ce02
IS
4267out:
4268 lpm_tree->prefix_ref_count[fib_node->key.prefix_len]++;
fc922bb0 4269 return 0;
2b52ce02
IS
4270
4271err_lpm_tree_replace:
4272 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
4273 return err;
fc922bb0
IS
4274}
4275
4276static void mlxsw_sp_fib_lpm_tree_unlink(struct mlxsw_sp *mlxsw_sp,
3aad95df 4277 struct mlxsw_sp_fib_node *fib_node)
fc922bb0 4278{
2b52ce02
IS
4279 struct mlxsw_sp_lpm_tree *lpm_tree = fib_node->fib->lpm_tree;
4280 struct mlxsw_sp_prefix_usage req_prefix_usage;
3aad95df 4281 struct mlxsw_sp_fib *fib = fib_node->fib;
2b52ce02 4282 int err;
3aad95df 4283
2b52ce02 4284 if (--lpm_tree->prefix_ref_count[fib_node->key.prefix_len] != 0)
fc922bb0 4285 return;
2b52ce02
IS
4286 /* Try to construct a new LPM tree from the current prefix usage
4287 * minus the unused one. If we fail, continue using the old one.
4fd00312 4288 */
2b52ce02
IS
4289 mlxsw_sp_prefix_usage_cpy(&req_prefix_usage, &lpm_tree->prefix_usage);
4290 mlxsw_sp_prefix_usage_clear(&req_prefix_usage,
4291 fib_node->key.prefix_len);
4292 lpm_tree = mlxsw_sp_lpm_tree_get(mlxsw_sp, &req_prefix_usage,
4293 fib->proto);
4294 if (IS_ERR(lpm_tree))
4295 return;
9aecce1c 4296
2b52ce02
IS
4297 err = mlxsw_sp_vrs_lpm_tree_replace(mlxsw_sp, fib, lpm_tree);
4298 if (err)
4299 goto err_lpm_tree_replace;
9aecce1c 4300
2b52ce02 4301 return;
9aecce1c 4302
2b52ce02
IS
4303err_lpm_tree_replace:
4304 mlxsw_sp_lpm_tree_put(mlxsw_sp, lpm_tree);
5b004412
JP
4305}
4306
76610ebb
IS
4307static int mlxsw_sp_fib_node_init(struct mlxsw_sp *mlxsw_sp,
4308 struct mlxsw_sp_fib_node *fib_node,
4309 struct mlxsw_sp_fib *fib)
4310{
76610ebb
IS
4311 int err;
4312
4313 err = mlxsw_sp_fib_node_insert(fib, fib_node);
4314 if (err)
4315 return err;
4316 fib_node->fib = fib;
4317
3aad95df 4318 err = mlxsw_sp_fib_lpm_tree_link(mlxsw_sp, fib_node);
fc922bb0
IS
4319 if (err)
4320 goto err_fib_lpm_tree_link;
76610ebb 4321
76610ebb
IS
4322 return 0;
4323
fc922bb0 4324err_fib_lpm_tree_link:
76610ebb
IS
4325 fib_node->fib = NULL;
4326 mlxsw_sp_fib_node_remove(fib, fib_node);
4327 return err;
4328}
4329
4330static void mlxsw_sp_fib_node_fini(struct mlxsw_sp *mlxsw_sp,
4331 struct mlxsw_sp_fib_node *fib_node)
4332{
76610ebb
IS
4333 struct mlxsw_sp_fib *fib = fib_node->fib;
4334
3aad95df 4335 mlxsw_sp_fib_lpm_tree_unlink(mlxsw_sp, fib_node);
76610ebb
IS
4336 fib_node->fib = NULL;
4337 mlxsw_sp_fib_node_remove(fib, fib_node);
4338}
4339
9aecce1c 4340static struct mlxsw_sp_fib_node *
731ea1ca
IS
4341mlxsw_sp_fib_node_get(struct mlxsw_sp *mlxsw_sp, u32 tb_id, const void *addr,
4342 size_t addr_len, unsigned char prefix_len,
4343 enum mlxsw_sp_l3proto proto)
5b004412 4344{
9aecce1c 4345 struct mlxsw_sp_fib_node *fib_node;
76610ebb 4346 struct mlxsw_sp_fib *fib;
9aecce1c
IS
4347 struct mlxsw_sp_vr *vr;
4348 int err;
4349
f8fa9b4e 4350 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id, NULL);
9aecce1c
IS
4351 if (IS_ERR(vr))
4352 return ERR_CAST(vr);
731ea1ca 4353 fib = mlxsw_sp_vr_fib(vr, proto);
9aecce1c 4354
731ea1ca 4355 fib_node = mlxsw_sp_fib_node_lookup(fib, addr, addr_len, prefix_len);
9aecce1c
IS
4356 if (fib_node)
4357 return fib_node;
5b004412 4358
731ea1ca 4359 fib_node = mlxsw_sp_fib_node_create(fib, addr, addr_len, prefix_len);
9aecce1c
IS
4360 if (!fib_node) {
4361 err = -ENOMEM;
4362 goto err_fib_node_create;
5b004412 4363 }
9aecce1c 4364
76610ebb
IS
4365 err = mlxsw_sp_fib_node_init(mlxsw_sp, fib_node, fib);
4366 if (err)
4367 goto err_fib_node_init;
4368
9aecce1c
IS
4369 return fib_node;
4370
76610ebb
IS
4371err_fib_node_init:
4372 mlxsw_sp_fib_node_destroy(fib_node);
9aecce1c 4373err_fib_node_create:
2b52ce02 4374 mlxsw_sp_vr_put(mlxsw_sp, vr);
9aecce1c 4375 return ERR_PTR(err);
5b004412
JP
4376}
4377
731ea1ca
IS
4378static void mlxsw_sp_fib_node_put(struct mlxsw_sp *mlxsw_sp,
4379 struct mlxsw_sp_fib_node *fib_node)
5b004412 4380{
76610ebb 4381 struct mlxsw_sp_vr *vr = fib_node->fib->vr;
5b004412 4382
9aecce1c
IS
4383 if (!list_empty(&fib_node->entry_list))
4384 return;
76610ebb 4385 mlxsw_sp_fib_node_fini(mlxsw_sp, fib_node);
9aecce1c 4386 mlxsw_sp_fib_node_destroy(fib_node);
2b52ce02 4387 mlxsw_sp_vr_put(mlxsw_sp, vr);
61c503f9
JP
4388}
4389
4f1c7f1f 4390static struct mlxsw_sp_fib4_entry *
9aecce1c 4391mlxsw_sp_fib4_node_entry_find(const struct mlxsw_sp_fib_node *fib_node,
4f1c7f1f 4392 const struct mlxsw_sp_fib4_entry *new4_entry)
61c503f9 4393{
4f1c7f1f 4394 struct mlxsw_sp_fib4_entry *fib4_entry;
9aecce1c 4395
4f1c7f1f
IS
4396 list_for_each_entry(fib4_entry, &fib_node->entry_list, common.list) {
4397 if (fib4_entry->tb_id > new4_entry->tb_id)
9aecce1c 4398 continue;
4f1c7f1f 4399 if (fib4_entry->tb_id != new4_entry->tb_id)
9aecce1c 4400 break;
4f1c7f1f 4401 if (fib4_entry->tos > new4_entry->tos)
9aecce1c 4402 continue;
4f1c7f1f
IS
4403 if (fib4_entry->prio >= new4_entry->prio ||
4404 fib4_entry->tos < new4_entry->tos)
4405 return fib4_entry;
9aecce1c
IS
4406 }
4407
4408 return NULL;
4409}
4410
4f1c7f1f
IS
4411static int
4412mlxsw_sp_fib4_node_list_append(struct mlxsw_sp_fib4_entry *fib4_entry,
4413 struct mlxsw_sp_fib4_entry *new4_entry)
4283bce5
IS
4414{
4415 struct mlxsw_sp_fib_node *fib_node;
4416
4f1c7f1f 4417 if (WARN_ON(!fib4_entry))
4283bce5
IS
4418 return -EINVAL;
4419
4f1c7f1f
IS
4420 fib_node = fib4_entry->common.fib_node;
4421 list_for_each_entry_from(fib4_entry, &fib_node->entry_list,
4422 common.list) {
4423 if (fib4_entry->tb_id != new4_entry->tb_id ||
4424 fib4_entry->tos != new4_entry->tos ||
4425 fib4_entry->prio != new4_entry->prio)
4283bce5
IS
4426 break;
4427 }
4428
4f1c7f1f 4429 list_add_tail(&new4_entry->common.list, &fib4_entry->common.list);
4283bce5
IS
4430 return 0;
4431}
4432
9aecce1c 4433static int
9efbee6f 4434mlxsw_sp_fib4_node_list_insert(struct mlxsw_sp_fib4_entry *new4_entry,
599cf8f9 4435 bool replace, bool append)
9aecce1c 4436{
9efbee6f 4437 struct mlxsw_sp_fib_node *fib_node = new4_entry->common.fib_node;
4f1c7f1f 4438 struct mlxsw_sp_fib4_entry *fib4_entry;
9aecce1c 4439
4f1c7f1f 4440 fib4_entry = mlxsw_sp_fib4_node_entry_find(fib_node, new4_entry);
9aecce1c 4441
4283bce5 4442 if (append)
4f1c7f1f
IS
4443 return mlxsw_sp_fib4_node_list_append(fib4_entry, new4_entry);
4444 if (replace && WARN_ON(!fib4_entry))
599cf8f9 4445 return -EINVAL;
4283bce5 4446
599cf8f9
IS
4447 /* Insert new entry before replaced one, so that we can later
4448 * remove the second.
4449 */
4f1c7f1f
IS
4450 if (fib4_entry) {
4451 list_add_tail(&new4_entry->common.list,
4452 &fib4_entry->common.list);
9aecce1c 4453 } else {
4f1c7f1f 4454 struct mlxsw_sp_fib4_entry *last;
9aecce1c 4455
4f1c7f1f
IS
4456 list_for_each_entry(last, &fib_node->entry_list, common.list) {
4457 if (new4_entry->tb_id > last->tb_id)
9aecce1c 4458 break;
4f1c7f1f 4459 fib4_entry = last;
9aecce1c
IS
4460 }
4461
4f1c7f1f
IS
4462 if (fib4_entry)
4463 list_add(&new4_entry->common.list,
4464 &fib4_entry->common.list);
9aecce1c 4465 else
4f1c7f1f
IS
4466 list_add(&new4_entry->common.list,
4467 &fib_node->entry_list);
9aecce1c
IS
4468 }
4469
4470 return 0;
4471}
4472
4473static void
4f1c7f1f 4474mlxsw_sp_fib4_node_list_remove(struct mlxsw_sp_fib4_entry *fib4_entry)
9aecce1c 4475{
4f1c7f1f 4476 list_del(&fib4_entry->common.list);
9aecce1c
IS
4477}
4478
80c238f9
IS
4479static int mlxsw_sp_fib_node_entry_add(struct mlxsw_sp *mlxsw_sp,
4480 struct mlxsw_sp_fib_entry *fib_entry)
9aecce1c 4481{
9efbee6f
IS
4482 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
4483
9aecce1c
IS
4484 if (!mlxsw_sp_fib_node_entry_is_first(fib_node, fib_entry))
4485 return 0;
4486
4487 /* To prevent packet loss, overwrite the previously offloaded
4488 * entry.
4489 */
4490 if (!list_is_singular(&fib_node->entry_list)) {
4491 enum mlxsw_reg_ralue_op op = MLXSW_REG_RALUE_OP_WRITE_DELETE;
4492 struct mlxsw_sp_fib_entry *n = list_next_entry(fib_entry, list);
4493
4494 mlxsw_sp_fib_entry_offload_refresh(n, op, 0);
4495 }
4496
4497 return mlxsw_sp_fib_entry_update(mlxsw_sp, fib_entry);
4498}
4499
80c238f9
IS
4500static void mlxsw_sp_fib_node_entry_del(struct mlxsw_sp *mlxsw_sp,
4501 struct mlxsw_sp_fib_entry *fib_entry)
9aecce1c 4502{
9efbee6f
IS
4503 struct mlxsw_sp_fib_node *fib_node = fib_entry->fib_node;
4504
9aecce1c
IS
4505 if (!mlxsw_sp_fib_node_entry_is_first(fib_node, fib_entry))
4506 return;
4507
4508 /* Promote the next entry by overwriting the deleted entry */
4509 if (!list_is_singular(&fib_node->entry_list)) {
4510 struct mlxsw_sp_fib_entry *n = list_next_entry(fib_entry, list);
4511 enum mlxsw_reg_ralue_op op = MLXSW_REG_RALUE_OP_WRITE_DELETE;
4512
4513 mlxsw_sp_fib_entry_update(mlxsw_sp, n);
4514 mlxsw_sp_fib_entry_offload_refresh(fib_entry, op, 0);
4515 return;
4516 }
4517
4518 mlxsw_sp_fib_entry_del(mlxsw_sp, fib_entry);
4519}
4520
4521static int mlxsw_sp_fib4_node_entry_link(struct mlxsw_sp *mlxsw_sp,
4f1c7f1f 4522 struct mlxsw_sp_fib4_entry *fib4_entry,
599cf8f9 4523 bool replace, bool append)
9aecce1c 4524{
9aecce1c
IS
4525 int err;
4526
9efbee6f 4527 err = mlxsw_sp_fib4_node_list_insert(fib4_entry, replace, append);
9aecce1c
IS
4528 if (err)
4529 return err;
4530
80c238f9 4531 err = mlxsw_sp_fib_node_entry_add(mlxsw_sp, &fib4_entry->common);
9aecce1c 4532 if (err)
80c238f9 4533 goto err_fib_node_entry_add;
9aecce1c 4534
9aecce1c
IS
4535 return 0;
4536
80c238f9 4537err_fib_node_entry_add:
4f1c7f1f 4538 mlxsw_sp_fib4_node_list_remove(fib4_entry);
9aecce1c
IS
4539 return err;
4540}
4541
4542static void
4543mlxsw_sp_fib4_node_entry_unlink(struct mlxsw_sp *mlxsw_sp,
4f1c7f1f 4544 struct mlxsw_sp_fib4_entry *fib4_entry)
9aecce1c 4545{
80c238f9 4546 mlxsw_sp_fib_node_entry_del(mlxsw_sp, &fib4_entry->common);
4f1c7f1f 4547 mlxsw_sp_fib4_node_list_remove(fib4_entry);
4607f6d2
PM
4548
4549 if (fib4_entry->common.type == MLXSW_SP_FIB_ENTRY_TYPE_IPIP_DECAP)
4550 mlxsw_sp_fib_entry_decap_fini(mlxsw_sp, &fib4_entry->common);
9aecce1c
IS
4551}
4552
599cf8f9 4553static void mlxsw_sp_fib4_entry_replace(struct mlxsw_sp *mlxsw_sp,
4f1c7f1f 4554 struct mlxsw_sp_fib4_entry *fib4_entry,
599cf8f9
IS
4555 bool replace)
4556{
4f1c7f1f
IS
4557 struct mlxsw_sp_fib_node *fib_node = fib4_entry->common.fib_node;
4558 struct mlxsw_sp_fib4_entry *replaced;
599cf8f9
IS
4559
4560 if (!replace)
4561 return;
4562
4563 /* We inserted the new entry before replaced one */
4f1c7f1f 4564 replaced = list_next_entry(fib4_entry, common.list);
599cf8f9
IS
4565
4566 mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, replaced);
4567 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, replaced);
731ea1ca 4568 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
599cf8f9
IS
4569}
4570
9aecce1c
IS
4571static int
4572mlxsw_sp_router_fib4_add(struct mlxsw_sp *mlxsw_sp,
4283bce5 4573 const struct fib_entry_notifier_info *fen_info,
599cf8f9 4574 bool replace, bool append)
9aecce1c 4575{
4f1c7f1f 4576 struct mlxsw_sp_fib4_entry *fib4_entry;
9aecce1c 4577 struct mlxsw_sp_fib_node *fib_node;
61c503f9
JP
4578 int err;
4579
9011b677 4580 if (mlxsw_sp->router->aborted)
b45f64d1
JP
4581 return 0;
4582
731ea1ca
IS
4583 fib_node = mlxsw_sp_fib_node_get(mlxsw_sp, fen_info->tb_id,
4584 &fen_info->dst, sizeof(fen_info->dst),
4585 fen_info->dst_len,
4586 MLXSW_SP_L3_PROTO_IPV4);
9aecce1c
IS
4587 if (IS_ERR(fib_node)) {
4588 dev_warn(mlxsw_sp->bus_info->dev, "Failed to get FIB node\n");
4589 return PTR_ERR(fib_node);
b45f64d1 4590 }
61c503f9 4591
4f1c7f1f
IS
4592 fib4_entry = mlxsw_sp_fib4_entry_create(mlxsw_sp, fib_node, fen_info);
4593 if (IS_ERR(fib4_entry)) {
9aecce1c 4594 dev_warn(mlxsw_sp->bus_info->dev, "Failed to create FIB entry\n");
4f1c7f1f 4595 err = PTR_ERR(fib4_entry);
9aecce1c
IS
4596 goto err_fib4_entry_create;
4597 }
5b004412 4598
4f1c7f1f 4599 err = mlxsw_sp_fib4_node_entry_link(mlxsw_sp, fib4_entry, replace,
599cf8f9 4600 append);
b45f64d1 4601 if (err) {
9aecce1c
IS
4602 dev_warn(mlxsw_sp->bus_info->dev, "Failed to link FIB entry to node\n");
4603 goto err_fib4_node_entry_link;
b45f64d1 4604 }
9aecce1c 4605
4f1c7f1f 4606 mlxsw_sp_fib4_entry_replace(mlxsw_sp, fib4_entry, replace);
599cf8f9 4607
61c503f9
JP
4608 return 0;
4609
9aecce1c 4610err_fib4_node_entry_link:
4f1c7f1f 4611 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
9aecce1c 4612err_fib4_entry_create:
731ea1ca 4613 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
61c503f9
JP
4614 return err;
4615}
4616
37956d78
JP
4617static void mlxsw_sp_router_fib4_del(struct mlxsw_sp *mlxsw_sp,
4618 struct fib_entry_notifier_info *fen_info)
61c503f9 4619{
4f1c7f1f 4620 struct mlxsw_sp_fib4_entry *fib4_entry;
9aecce1c 4621 struct mlxsw_sp_fib_node *fib_node;
61c503f9 4622
9011b677 4623 if (mlxsw_sp->router->aborted)
37956d78 4624 return;
b45f64d1 4625
4f1c7f1f
IS
4626 fib4_entry = mlxsw_sp_fib4_entry_lookup(mlxsw_sp, fen_info);
4627 if (WARN_ON(!fib4_entry))
37956d78 4628 return;
4f1c7f1f 4629 fib_node = fib4_entry->common.fib_node;
5b004412 4630
4f1c7f1f
IS
4631 mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, fib4_entry);
4632 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
731ea1ca 4633 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
61c503f9 4634}
b45f64d1 4635
428b851f
IS
4636static bool mlxsw_sp_fib6_rt_should_ignore(const struct rt6_info *rt)
4637{
4638 /* Packets with link-local destination IP arriving to the router
4639 * are trapped to the CPU, so no need to program specific routes
4640 * for them.
4641 */
4642 if (ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_LINKLOCAL)
4643 return true;
4644
4645 /* Multicast routes aren't supported, so ignore them. Neighbour
4646 * Discovery packets are specifically trapped.
4647 */
4648 if (ipv6_addr_type(&rt->rt6i_dst.addr) & IPV6_ADDR_MULTICAST)
4649 return true;
4650
4651 /* Cloned routes are irrelevant in the forwarding path. */
4652 if (rt->rt6i_flags & RTF_CACHE)
4653 return true;
4654
4655 return false;
4656}
4657
4658static struct mlxsw_sp_rt6 *mlxsw_sp_rt6_create(struct rt6_info *rt)
4659{
4660 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
4661
4662 mlxsw_sp_rt6 = kzalloc(sizeof(*mlxsw_sp_rt6), GFP_KERNEL);
4663 if (!mlxsw_sp_rt6)
4664 return ERR_PTR(-ENOMEM);
4665
4666 /* In case of route replace, replaced route is deleted with
4667 * no notification. Take reference to prevent accessing freed
4668 * memory.
4669 */
4670 mlxsw_sp_rt6->rt = rt;
4671 rt6_hold(rt);
4672
4673 return mlxsw_sp_rt6;
4674}
4675
4676#if IS_ENABLED(CONFIG_IPV6)
4677static void mlxsw_sp_rt6_release(struct rt6_info *rt)
4678{
4679 rt6_release(rt);
4680}
4681#else
4682static void mlxsw_sp_rt6_release(struct rt6_info *rt)
4683{
4684}
4685#endif
4686
4687static void mlxsw_sp_rt6_destroy(struct mlxsw_sp_rt6 *mlxsw_sp_rt6)
4688{
4689 mlxsw_sp_rt6_release(mlxsw_sp_rt6->rt);
4690 kfree(mlxsw_sp_rt6);
4691}
4692
4693static bool mlxsw_sp_fib6_rt_can_mp(const struct rt6_info *rt)
4694{
4695 /* RTF_CACHE routes are ignored */
4696 return (rt->rt6i_flags & (RTF_GATEWAY | RTF_ADDRCONF)) == RTF_GATEWAY;
4697}
4698
4699static struct rt6_info *
4700mlxsw_sp_fib6_entry_rt(const struct mlxsw_sp_fib6_entry *fib6_entry)
4701{
4702 return list_first_entry(&fib6_entry->rt6_list, struct mlxsw_sp_rt6,
4703 list)->rt;
4704}
4705
4706static struct mlxsw_sp_fib6_entry *
4707mlxsw_sp_fib6_node_mp_entry_find(const struct mlxsw_sp_fib_node *fib_node,
0a7fd1ac 4708 const struct rt6_info *nrt, bool replace)
428b851f
IS
4709{
4710 struct mlxsw_sp_fib6_entry *fib6_entry;
4711
0a7fd1ac 4712 if (!mlxsw_sp_fib6_rt_can_mp(nrt) || replace)
428b851f
IS
4713 return NULL;
4714
4715 list_for_each_entry(fib6_entry, &fib_node->entry_list, common.list) {
4716 struct rt6_info *rt = mlxsw_sp_fib6_entry_rt(fib6_entry);
4717
4718 /* RT6_TABLE_LOCAL and RT6_TABLE_MAIN share the same
4719 * virtual router.
4720 */
4721 if (rt->rt6i_table->tb6_id > nrt->rt6i_table->tb6_id)
4722 continue;
4723 if (rt->rt6i_table->tb6_id != nrt->rt6i_table->tb6_id)
4724 break;
4725 if (rt->rt6i_metric < nrt->rt6i_metric)
4726 continue;
4727 if (rt->rt6i_metric == nrt->rt6i_metric &&
4728 mlxsw_sp_fib6_rt_can_mp(rt))
4729 return fib6_entry;
4730 if (rt->rt6i_metric > nrt->rt6i_metric)
4731 break;
4732 }
4733
4734 return NULL;
4735}
4736
4737static struct mlxsw_sp_rt6 *
4738mlxsw_sp_fib6_entry_rt_find(const struct mlxsw_sp_fib6_entry *fib6_entry,
4739 const struct rt6_info *rt)
4740{
4741 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
4742
4743 list_for_each_entry(mlxsw_sp_rt6, &fib6_entry->rt6_list, list) {
4744 if (mlxsw_sp_rt6->rt == rt)
4745 return mlxsw_sp_rt6;
4746 }
4747
4748 return NULL;
4749}
4750
8f28a309
PM
4751static bool mlxsw_sp_nexthop6_ipip_type(const struct mlxsw_sp *mlxsw_sp,
4752 const struct rt6_info *rt,
4753 enum mlxsw_sp_ipip_type *ret)
4754{
4755 return rt->dst.dev &&
4756 mlxsw_sp_netdev_ipip_type(mlxsw_sp, rt->dst.dev, ret);
4757}
4758
35225e47
PM
4759static int mlxsw_sp_nexthop6_type_init(struct mlxsw_sp *mlxsw_sp,
4760 struct mlxsw_sp_nexthop_group *nh_grp,
4761 struct mlxsw_sp_nexthop *nh,
4762 const struct rt6_info *rt)
428b851f 4763{
d97cda5f
PM
4764 const struct mlxsw_sp_ipip_ops *ipip_ops;
4765 struct mlxsw_sp_ipip_entry *ipip_entry;
428b851f
IS
4766 struct net_device *dev = rt->dst.dev;
4767 struct mlxsw_sp_rif *rif;
4768 int err;
4769
d97cda5f
PM
4770 ipip_entry = mlxsw_sp_ipip_entry_find_by_ol_dev(mlxsw_sp, dev);
4771 if (ipip_entry) {
4772 ipip_ops = mlxsw_sp->router->ipip_ops_arr[ipip_entry->ipipt];
4773 if (ipip_ops->can_offload(mlxsw_sp, dev,
4774 MLXSW_SP_L3_PROTO_IPV6)) {
4775 nh->type = MLXSW_SP_NEXTHOP_TYPE_IPIP;
4776 mlxsw_sp_nexthop_ipip_init(mlxsw_sp, nh, ipip_entry);
4777 return 0;
4778 }
8f28a309
PM
4779 }
4780
35225e47 4781 nh->type = MLXSW_SP_NEXTHOP_TYPE_ETH;
428b851f
IS
4782 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
4783 if (!rif)
4784 return 0;
4785 mlxsw_sp_nexthop_rif_init(nh, rif);
4786
4787 err = mlxsw_sp_nexthop_neigh_init(mlxsw_sp, nh);
4788 if (err)
4789 goto err_nexthop_neigh_init;
4790
4791 return 0;
4792
4793err_nexthop_neigh_init:
4794 mlxsw_sp_nexthop_rif_fini(nh);
4795 return err;
4796}
4797
35225e47
PM
4798static void mlxsw_sp_nexthop6_type_fini(struct mlxsw_sp *mlxsw_sp,
4799 struct mlxsw_sp_nexthop *nh)
4800{
4801 mlxsw_sp_nexthop_type_fini(mlxsw_sp, nh);
4802}
4803
4804static int mlxsw_sp_nexthop6_init(struct mlxsw_sp *mlxsw_sp,
4805 struct mlxsw_sp_nexthop_group *nh_grp,
4806 struct mlxsw_sp_nexthop *nh,
4807 const struct rt6_info *rt)
4808{
4809 struct net_device *dev = rt->dst.dev;
4810
4811 nh->nh_grp = nh_grp;
3743d88a 4812 nh->nh_weight = rt->rt6i_nh_weight;
35225e47 4813 memcpy(&nh->gw_addr, &rt->rt6i_gateway, sizeof(nh->gw_addr));
a5390278 4814 mlxsw_sp_nexthop_counter_alloc(mlxsw_sp, nh);
35225e47 4815
dbe4598c
AS
4816 list_add_tail(&nh->router_list_node, &mlxsw_sp->router->nexthop_list);
4817
35225e47
PM
4818 if (!dev)
4819 return 0;
4820 nh->ifindex = dev->ifindex;
4821
4822 return mlxsw_sp_nexthop6_type_init(mlxsw_sp, nh_grp, nh, rt);
4823}
4824
428b851f
IS
4825static void mlxsw_sp_nexthop6_fini(struct mlxsw_sp *mlxsw_sp,
4826 struct mlxsw_sp_nexthop *nh)
4827{
35225e47 4828 mlxsw_sp_nexthop6_type_fini(mlxsw_sp, nh);
dbe4598c 4829 list_del(&nh->router_list_node);
a5390278 4830 mlxsw_sp_nexthop_counter_free(mlxsw_sp, nh);
428b851f
IS
4831}
4832
f6050ee6
PM
4833static bool mlxsw_sp_rt6_is_gateway(const struct mlxsw_sp *mlxsw_sp,
4834 const struct rt6_info *rt)
4835{
8f28a309
PM
4836 return rt->rt6i_flags & RTF_GATEWAY ||
4837 mlxsw_sp_nexthop6_ipip_type(mlxsw_sp, rt, NULL);
f6050ee6
PM
4838}
4839
428b851f
IS
4840static struct mlxsw_sp_nexthop_group *
4841mlxsw_sp_nexthop6_group_create(struct mlxsw_sp *mlxsw_sp,
4842 struct mlxsw_sp_fib6_entry *fib6_entry)
4843{
4844 struct mlxsw_sp_nexthop_group *nh_grp;
4845 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
4846 struct mlxsw_sp_nexthop *nh;
4847 size_t alloc_size;
4848 int i = 0;
4849 int err;
4850
4851 alloc_size = sizeof(*nh_grp) +
4852 fib6_entry->nrt6 * sizeof(struct mlxsw_sp_nexthop);
4853 nh_grp = kzalloc(alloc_size, GFP_KERNEL);
4854 if (!nh_grp)
4855 return ERR_PTR(-ENOMEM);
4856 INIT_LIST_HEAD(&nh_grp->fib_list);
4857#if IS_ENABLED(CONFIG_IPV6)
4858 nh_grp->neigh_tbl = &nd_tbl;
4859#endif
4860 mlxsw_sp_rt6 = list_first_entry(&fib6_entry->rt6_list,
4861 struct mlxsw_sp_rt6, list);
f6050ee6 4862 nh_grp->gateway = mlxsw_sp_rt6_is_gateway(mlxsw_sp, mlxsw_sp_rt6->rt);
428b851f
IS
4863 nh_grp->count = fib6_entry->nrt6;
4864 for (i = 0; i < nh_grp->count; i++) {
4865 struct rt6_info *rt = mlxsw_sp_rt6->rt;
4866
4867 nh = &nh_grp->nexthops[i];
4868 err = mlxsw_sp_nexthop6_init(mlxsw_sp, nh_grp, nh, rt);
4869 if (err)
4870 goto err_nexthop6_init;
4871 mlxsw_sp_rt6 = list_next_entry(mlxsw_sp_rt6, list);
4872 }
e6f3b379
AS
4873
4874 err = mlxsw_sp_nexthop_group_insert(mlxsw_sp, nh_grp);
4875 if (err)
4876 goto err_nexthop_group_insert;
4877
428b851f
IS
4878 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
4879 return nh_grp;
4880
e6f3b379 4881err_nexthop_group_insert:
428b851f
IS
4882err_nexthop6_init:
4883 for (i--; i >= 0; i--) {
4884 nh = &nh_grp->nexthops[i];
4885 mlxsw_sp_nexthop6_fini(mlxsw_sp, nh);
4886 }
4887 kfree(nh_grp);
4888 return ERR_PTR(err);
4889}
4890
4891static void
4892mlxsw_sp_nexthop6_group_destroy(struct mlxsw_sp *mlxsw_sp,
4893 struct mlxsw_sp_nexthop_group *nh_grp)
4894{
4895 struct mlxsw_sp_nexthop *nh;
4896 int i = nh_grp->count;
4897
e6f3b379 4898 mlxsw_sp_nexthop_group_remove(mlxsw_sp, nh_grp);
428b851f
IS
4899 for (i--; i >= 0; i--) {
4900 nh = &nh_grp->nexthops[i];
4901 mlxsw_sp_nexthop6_fini(mlxsw_sp, nh);
4902 }
4903 mlxsw_sp_nexthop_group_refresh(mlxsw_sp, nh_grp);
4904 WARN_ON(nh_grp->adj_index_valid);
4905 kfree(nh_grp);
4906}
4907
4908static int mlxsw_sp_nexthop6_group_get(struct mlxsw_sp *mlxsw_sp,
4909 struct mlxsw_sp_fib6_entry *fib6_entry)
4910{
4911 struct mlxsw_sp_nexthop_group *nh_grp;
4912
e6f3b379
AS
4913 nh_grp = mlxsw_sp_nexthop6_group_lookup(mlxsw_sp, fib6_entry);
4914 if (!nh_grp) {
4915 nh_grp = mlxsw_sp_nexthop6_group_create(mlxsw_sp, fib6_entry);
4916 if (IS_ERR(nh_grp))
4917 return PTR_ERR(nh_grp);
4918 }
428b851f
IS
4919
4920 list_add_tail(&fib6_entry->common.nexthop_group_node,
4921 &nh_grp->fib_list);
4922 fib6_entry->common.nh_group = nh_grp;
4923
4924 return 0;
4925}
4926
4927static void mlxsw_sp_nexthop6_group_put(struct mlxsw_sp *mlxsw_sp,
4928 struct mlxsw_sp_fib_entry *fib_entry)
4929{
4930 struct mlxsw_sp_nexthop_group *nh_grp = fib_entry->nh_group;
4931
4932 list_del(&fib_entry->nexthop_group_node);
4933 if (!list_empty(&nh_grp->fib_list))
4934 return;
4935 mlxsw_sp_nexthop6_group_destroy(mlxsw_sp, nh_grp);
4936}
4937
4938static int
4939mlxsw_sp_nexthop6_group_update(struct mlxsw_sp *mlxsw_sp,
4940 struct mlxsw_sp_fib6_entry *fib6_entry)
4941{
4942 struct mlxsw_sp_nexthop_group *old_nh_grp = fib6_entry->common.nh_group;
4943 int err;
4944
4945 fib6_entry->common.nh_group = NULL;
4946 list_del(&fib6_entry->common.nexthop_group_node);
4947
4948 err = mlxsw_sp_nexthop6_group_get(mlxsw_sp, fib6_entry);
4949 if (err)
4950 goto err_nexthop6_group_get;
4951
4952 /* In case this entry is offloaded, then the adjacency index
4953 * currently associated with it in the device's table is that
4954 * of the old group. Start using the new one instead.
4955 */
4956 err = mlxsw_sp_fib_node_entry_add(mlxsw_sp, &fib6_entry->common);
4957 if (err)
4958 goto err_fib_node_entry_add;
4959
4960 if (list_empty(&old_nh_grp->fib_list))
4961 mlxsw_sp_nexthop6_group_destroy(mlxsw_sp, old_nh_grp);
4962
4963 return 0;
4964
4965err_fib_node_entry_add:
4966 mlxsw_sp_nexthop6_group_put(mlxsw_sp, &fib6_entry->common);
4967err_nexthop6_group_get:
4968 list_add_tail(&fib6_entry->common.nexthop_group_node,
4969 &old_nh_grp->fib_list);
4970 fib6_entry->common.nh_group = old_nh_grp;
4971 return err;
4972}
4973
4974static int
4975mlxsw_sp_fib6_entry_nexthop_add(struct mlxsw_sp *mlxsw_sp,
4976 struct mlxsw_sp_fib6_entry *fib6_entry,
4977 struct rt6_info *rt)
4978{
4979 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
4980 int err;
4981
4982 mlxsw_sp_rt6 = mlxsw_sp_rt6_create(rt);
4983 if (IS_ERR(mlxsw_sp_rt6))
4984 return PTR_ERR(mlxsw_sp_rt6);
4985
4986 list_add_tail(&mlxsw_sp_rt6->list, &fib6_entry->rt6_list);
4987 fib6_entry->nrt6++;
4988
4989 err = mlxsw_sp_nexthop6_group_update(mlxsw_sp, fib6_entry);
4990 if (err)
4991 goto err_nexthop6_group_update;
4992
4993 return 0;
4994
4995err_nexthop6_group_update:
4996 fib6_entry->nrt6--;
4997 list_del(&mlxsw_sp_rt6->list);
4998 mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
4999 return err;
5000}
5001
5002static void
5003mlxsw_sp_fib6_entry_nexthop_del(struct mlxsw_sp *mlxsw_sp,
5004 struct mlxsw_sp_fib6_entry *fib6_entry,
5005 struct rt6_info *rt)
5006{
5007 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
5008
5009 mlxsw_sp_rt6 = mlxsw_sp_fib6_entry_rt_find(fib6_entry, rt);
5010 if (WARN_ON(!mlxsw_sp_rt6))
5011 return;
5012
5013 fib6_entry->nrt6--;
5014 list_del(&mlxsw_sp_rt6->list);
5015 mlxsw_sp_nexthop6_group_update(mlxsw_sp, fib6_entry);
5016 mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
5017}
5018
f6050ee6
PM
5019static void mlxsw_sp_fib6_entry_type_set(struct mlxsw_sp *mlxsw_sp,
5020 struct mlxsw_sp_fib_entry *fib_entry,
428b851f
IS
5021 const struct rt6_info *rt)
5022{
5023 /* Packets hitting RTF_REJECT routes need to be discarded by the
5024 * stack. We can rely on their destination device not having a
5025 * RIF (it's the loopback device) and can thus use action type
5026 * local, which will cause them to be trapped with a lower
5027 * priority than packets that need to be locally received.
5028 */
d3b6d377 5029 if (rt->rt6i_flags & (RTF_LOCAL | RTF_ANYCAST))
428b851f
IS
5030 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_TRAP;
5031 else if (rt->rt6i_flags & RTF_REJECT)
5032 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
f6050ee6 5033 else if (mlxsw_sp_rt6_is_gateway(mlxsw_sp, rt))
428b851f
IS
5034 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_REMOTE;
5035 else
5036 fib_entry->type = MLXSW_SP_FIB_ENTRY_TYPE_LOCAL;
5037}
5038
5039static void
5040mlxsw_sp_fib6_entry_rt_destroy_all(struct mlxsw_sp_fib6_entry *fib6_entry)
5041{
5042 struct mlxsw_sp_rt6 *mlxsw_sp_rt6, *tmp;
5043
5044 list_for_each_entry_safe(mlxsw_sp_rt6, tmp, &fib6_entry->rt6_list,
5045 list) {
5046 fib6_entry->nrt6--;
5047 list_del(&mlxsw_sp_rt6->list);
5048 mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
5049 }
5050}
5051
5052static struct mlxsw_sp_fib6_entry *
5053mlxsw_sp_fib6_entry_create(struct mlxsw_sp *mlxsw_sp,
5054 struct mlxsw_sp_fib_node *fib_node,
5055 struct rt6_info *rt)
5056{
5057 struct mlxsw_sp_fib6_entry *fib6_entry;
5058 struct mlxsw_sp_fib_entry *fib_entry;
5059 struct mlxsw_sp_rt6 *mlxsw_sp_rt6;
5060 int err;
5061
5062 fib6_entry = kzalloc(sizeof(*fib6_entry), GFP_KERNEL);
5063 if (!fib6_entry)
5064 return ERR_PTR(-ENOMEM);
5065 fib_entry = &fib6_entry->common;
5066
5067 mlxsw_sp_rt6 = mlxsw_sp_rt6_create(rt);
5068 if (IS_ERR(mlxsw_sp_rt6)) {
5069 err = PTR_ERR(mlxsw_sp_rt6);
5070 goto err_rt6_create;
5071 }
5072
f6050ee6 5073 mlxsw_sp_fib6_entry_type_set(mlxsw_sp, fib_entry, mlxsw_sp_rt6->rt);
428b851f
IS
5074
5075 INIT_LIST_HEAD(&fib6_entry->rt6_list);
5076 list_add_tail(&mlxsw_sp_rt6->list, &fib6_entry->rt6_list);
5077 fib6_entry->nrt6 = 1;
5078 err = mlxsw_sp_nexthop6_group_get(mlxsw_sp, fib6_entry);
5079 if (err)
5080 goto err_nexthop6_group_get;
5081
5082 fib_entry->fib_node = fib_node;
5083
5084 return fib6_entry;
5085
5086err_nexthop6_group_get:
5087 list_del(&mlxsw_sp_rt6->list);
5088 mlxsw_sp_rt6_destroy(mlxsw_sp_rt6);
5089err_rt6_create:
5090 kfree(fib6_entry);
5091 return ERR_PTR(err);
5092}
5093
5094static void mlxsw_sp_fib6_entry_destroy(struct mlxsw_sp *mlxsw_sp,
5095 struct mlxsw_sp_fib6_entry *fib6_entry)
5096{
5097 mlxsw_sp_nexthop6_group_put(mlxsw_sp, &fib6_entry->common);
5098 mlxsw_sp_fib6_entry_rt_destroy_all(fib6_entry);
5099 WARN_ON(fib6_entry->nrt6);
5100 kfree(fib6_entry);
5101}
5102
5103static struct mlxsw_sp_fib6_entry *
5104mlxsw_sp_fib6_node_entry_find(const struct mlxsw_sp_fib_node *fib_node,
0a7fd1ac 5105 const struct rt6_info *nrt, bool replace)
428b851f 5106{
0a7fd1ac 5107 struct mlxsw_sp_fib6_entry *fib6_entry, *fallback = NULL;
428b851f
IS
5108
5109 list_for_each_entry(fib6_entry, &fib_node->entry_list, common.list) {
5110 struct rt6_info *rt = mlxsw_sp_fib6_entry_rt(fib6_entry);
5111
5112 if (rt->rt6i_table->tb6_id > nrt->rt6i_table->tb6_id)
5113 continue;
5114 if (rt->rt6i_table->tb6_id != nrt->rt6i_table->tb6_id)
5115 break;
0a7fd1ac
IS
5116 if (replace && rt->rt6i_metric == nrt->rt6i_metric) {
5117 if (mlxsw_sp_fib6_rt_can_mp(rt) ==
5118 mlxsw_sp_fib6_rt_can_mp(nrt))
5119 return fib6_entry;
5120 if (mlxsw_sp_fib6_rt_can_mp(nrt))
5121 fallback = fallback ?: fib6_entry;
5122 }
428b851f 5123 if (rt->rt6i_metric > nrt->rt6i_metric)
0a7fd1ac 5124 return fallback ?: fib6_entry;
428b851f
IS
5125 }
5126
0a7fd1ac 5127 return fallback;
428b851f
IS
5128}
5129
5130static int
0a7fd1ac
IS
5131mlxsw_sp_fib6_node_list_insert(struct mlxsw_sp_fib6_entry *new6_entry,
5132 bool replace)
428b851f
IS
5133{
5134 struct mlxsw_sp_fib_node *fib_node = new6_entry->common.fib_node;
5135 struct rt6_info *nrt = mlxsw_sp_fib6_entry_rt(new6_entry);
5136 struct mlxsw_sp_fib6_entry *fib6_entry;
5137
0a7fd1ac
IS
5138 fib6_entry = mlxsw_sp_fib6_node_entry_find(fib_node, nrt, replace);
5139
5140 if (replace && WARN_ON(!fib6_entry))
5141 return -EINVAL;
428b851f
IS
5142
5143 if (fib6_entry) {
5144 list_add_tail(&new6_entry->common.list,
5145 &fib6_entry->common.list);
5146 } else {
5147 struct mlxsw_sp_fib6_entry *last;
5148
5149 list_for_each_entry(last, &fib_node->entry_list, common.list) {
5150 struct rt6_info *rt = mlxsw_sp_fib6_entry_rt(last);
5151
5152 if (nrt->rt6i_table->tb6_id > rt->rt6i_table->tb6_id)
5153 break;
5154 fib6_entry = last;
5155 }
5156
5157 if (fib6_entry)
5158 list_add(&new6_entry->common.list,
5159 &fib6_entry->common.list);
5160 else
5161 list_add(&new6_entry->common.list,
5162 &fib_node->entry_list);
5163 }
5164
5165 return 0;
5166}
5167
5168static void
5169mlxsw_sp_fib6_node_list_remove(struct mlxsw_sp_fib6_entry *fib6_entry)
5170{
5171 list_del(&fib6_entry->common.list);
5172}
5173
5174static int mlxsw_sp_fib6_node_entry_link(struct mlxsw_sp *mlxsw_sp,
0a7fd1ac
IS
5175 struct mlxsw_sp_fib6_entry *fib6_entry,
5176 bool replace)
428b851f
IS
5177{
5178 int err;
5179
0a7fd1ac 5180 err = mlxsw_sp_fib6_node_list_insert(fib6_entry, replace);
428b851f
IS
5181 if (err)
5182 return err;
5183
5184 err = mlxsw_sp_fib_node_entry_add(mlxsw_sp, &fib6_entry->common);
5185 if (err)
5186 goto err_fib_node_entry_add;
5187
5188 return 0;
5189
5190err_fib_node_entry_add:
5191 mlxsw_sp_fib6_node_list_remove(fib6_entry);
5192 return err;
5193}
5194
5195static void
5196mlxsw_sp_fib6_node_entry_unlink(struct mlxsw_sp *mlxsw_sp,
5197 struct mlxsw_sp_fib6_entry *fib6_entry)
5198{
5199 mlxsw_sp_fib_node_entry_del(mlxsw_sp, &fib6_entry->common);
5200 mlxsw_sp_fib6_node_list_remove(fib6_entry);
5201}
5202
5203static struct mlxsw_sp_fib6_entry *
5204mlxsw_sp_fib6_entry_lookup(struct mlxsw_sp *mlxsw_sp,
5205 const struct rt6_info *rt)
5206{
5207 struct mlxsw_sp_fib6_entry *fib6_entry;
5208 struct mlxsw_sp_fib_node *fib_node;
5209 struct mlxsw_sp_fib *fib;
5210 struct mlxsw_sp_vr *vr;
5211
5212 vr = mlxsw_sp_vr_find(mlxsw_sp, rt->rt6i_table->tb6_id);
5213 if (!vr)
5214 return NULL;
5215 fib = mlxsw_sp_vr_fib(vr, MLXSW_SP_L3_PROTO_IPV6);
5216
5217 fib_node = mlxsw_sp_fib_node_lookup(fib, &rt->rt6i_dst.addr,
5218 sizeof(rt->rt6i_dst.addr),
5219 rt->rt6i_dst.plen);
5220 if (!fib_node)
5221 return NULL;
5222
5223 list_for_each_entry(fib6_entry, &fib_node->entry_list, common.list) {
5224 struct rt6_info *iter_rt = mlxsw_sp_fib6_entry_rt(fib6_entry);
5225
5226 if (rt->rt6i_table->tb6_id == iter_rt->rt6i_table->tb6_id &&
5227 rt->rt6i_metric == iter_rt->rt6i_metric &&
5228 mlxsw_sp_fib6_entry_rt_find(fib6_entry, rt))
5229 return fib6_entry;
5230 }
5231
5232 return NULL;
5233}
5234
0a7fd1ac
IS
5235static void mlxsw_sp_fib6_entry_replace(struct mlxsw_sp *mlxsw_sp,
5236 struct mlxsw_sp_fib6_entry *fib6_entry,
5237 bool replace)
5238{
5239 struct mlxsw_sp_fib_node *fib_node = fib6_entry->common.fib_node;
5240 struct mlxsw_sp_fib6_entry *replaced;
5241
5242 if (!replace)
5243 return;
5244
5245 replaced = list_next_entry(fib6_entry, common.list);
5246
5247 mlxsw_sp_fib6_node_entry_unlink(mlxsw_sp, replaced);
5248 mlxsw_sp_fib6_entry_destroy(mlxsw_sp, replaced);
5249 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5250}
5251
428b851f 5252static int mlxsw_sp_router_fib6_add(struct mlxsw_sp *mlxsw_sp,
0a7fd1ac 5253 struct rt6_info *rt, bool replace)
428b851f
IS
5254{
5255 struct mlxsw_sp_fib6_entry *fib6_entry;
5256 struct mlxsw_sp_fib_node *fib_node;
5257 int err;
5258
5259 if (mlxsw_sp->router->aborted)
5260 return 0;
5261
f36f5ac6
IS
5262 if (rt->rt6i_src.plen)
5263 return -EINVAL;
5264
428b851f
IS
5265 if (mlxsw_sp_fib6_rt_should_ignore(rt))
5266 return 0;
5267
5268 fib_node = mlxsw_sp_fib_node_get(mlxsw_sp, rt->rt6i_table->tb6_id,
5269 &rt->rt6i_dst.addr,
5270 sizeof(rt->rt6i_dst.addr),
5271 rt->rt6i_dst.plen,
5272 MLXSW_SP_L3_PROTO_IPV6);
5273 if (IS_ERR(fib_node))
5274 return PTR_ERR(fib_node);
5275
5276 /* Before creating a new entry, try to append route to an existing
5277 * multipath entry.
5278 */
0a7fd1ac 5279 fib6_entry = mlxsw_sp_fib6_node_mp_entry_find(fib_node, rt, replace);
428b851f
IS
5280 if (fib6_entry) {
5281 err = mlxsw_sp_fib6_entry_nexthop_add(mlxsw_sp, fib6_entry, rt);
5282 if (err)
5283 goto err_fib6_entry_nexthop_add;
5284 return 0;
5285 }
5286
5287 fib6_entry = mlxsw_sp_fib6_entry_create(mlxsw_sp, fib_node, rt);
5288 if (IS_ERR(fib6_entry)) {
5289 err = PTR_ERR(fib6_entry);
5290 goto err_fib6_entry_create;
5291 }
5292
0a7fd1ac 5293 err = mlxsw_sp_fib6_node_entry_link(mlxsw_sp, fib6_entry, replace);
428b851f
IS
5294 if (err)
5295 goto err_fib6_node_entry_link;
5296
0a7fd1ac
IS
5297 mlxsw_sp_fib6_entry_replace(mlxsw_sp, fib6_entry, replace);
5298
428b851f
IS
5299 return 0;
5300
5301err_fib6_node_entry_link:
5302 mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_entry);
5303err_fib6_entry_create:
5304err_fib6_entry_nexthop_add:
5305 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5306 return err;
5307}
5308
5309static void mlxsw_sp_router_fib6_del(struct mlxsw_sp *mlxsw_sp,
5310 struct rt6_info *rt)
5311{
5312 struct mlxsw_sp_fib6_entry *fib6_entry;
5313 struct mlxsw_sp_fib_node *fib_node;
5314
5315 if (mlxsw_sp->router->aborted)
5316 return;
5317
5318 if (mlxsw_sp_fib6_rt_should_ignore(rt))
5319 return;
5320
5321 fib6_entry = mlxsw_sp_fib6_entry_lookup(mlxsw_sp, rt);
5322 if (WARN_ON(!fib6_entry))
5323 return;
5324
5325 /* If route is part of a multipath entry, but not the last one
5326 * removed, then only reduce its nexthop group.
5327 */
5328 if (!list_is_singular(&fib6_entry->rt6_list)) {
5329 mlxsw_sp_fib6_entry_nexthop_del(mlxsw_sp, fib6_entry, rt);
5330 return;
5331 }
5332
5333 fib_node = fib6_entry->common.fib_node;
5334
5335 mlxsw_sp_fib6_node_entry_unlink(mlxsw_sp, fib6_entry);
5336 mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_entry);
5337 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5338}
5339
bc65a8a4
IS
5340static int __mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp,
5341 enum mlxsw_reg_ralxx_protocol proto,
5342 u8 tree_id)
b45f64d1
JP
5343{
5344 char ralta_pl[MLXSW_REG_RALTA_LEN];
5345 char ralst_pl[MLXSW_REG_RALST_LEN];
b5d90e6d 5346 int i, err;
b45f64d1 5347
bc65a8a4 5348 mlxsw_reg_ralta_pack(ralta_pl, true, proto, tree_id);
b45f64d1
JP
5349 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralta), ralta_pl);
5350 if (err)
5351 return err;
5352
bc65a8a4 5353 mlxsw_reg_ralst_pack(ralst_pl, 0xff, tree_id);
b45f64d1
JP
5354 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralst), ralst_pl);
5355 if (err)
5356 return err;
5357
b5d90e6d 5358 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
9011b677 5359 struct mlxsw_sp_vr *vr = &mlxsw_sp->router->vrs[i];
b5d90e6d
IS
5360 char raltb_pl[MLXSW_REG_RALTB_LEN];
5361 char ralue_pl[MLXSW_REG_RALUE_LEN];
b45f64d1 5362
bc65a8a4 5363 mlxsw_reg_raltb_pack(raltb_pl, vr->id, proto, tree_id);
b5d90e6d
IS
5364 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(raltb),
5365 raltb_pl);
5366 if (err)
5367 return err;
5368
bc65a8a4
IS
5369 mlxsw_reg_ralue_pack(ralue_pl, proto,
5370 MLXSW_REG_RALUE_OP_WRITE_WRITE, vr->id, 0);
b5d90e6d
IS
5371 mlxsw_reg_ralue_act_ip2me_pack(ralue_pl);
5372 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ralue),
5373 ralue_pl);
5374 if (err)
5375 return err;
5376 }
5377
5378 return 0;
b45f64d1
JP
5379}
5380
d42b0965
YG
5381static int mlxsw_sp_router_fibmr_add(struct mlxsw_sp *mlxsw_sp,
5382 struct mfc_entry_notifier_info *men_info,
5383 bool replace)
5384{
5385 struct mlxsw_sp_vr *vr;
5386
5387 if (mlxsw_sp->router->aborted)
5388 return 0;
5389
f8fa9b4e 5390 vr = mlxsw_sp_vr_get(mlxsw_sp, men_info->tb_id, NULL);
d42b0965
YG
5391 if (IS_ERR(vr))
5392 return PTR_ERR(vr);
5393
5394 return mlxsw_sp_mr_route4_add(vr->mr4_table, men_info->mfc, replace);
5395}
5396
5397static void mlxsw_sp_router_fibmr_del(struct mlxsw_sp *mlxsw_sp,
5398 struct mfc_entry_notifier_info *men_info)
5399{
5400 struct mlxsw_sp_vr *vr;
5401
5402 if (mlxsw_sp->router->aborted)
5403 return;
5404
5405 vr = mlxsw_sp_vr_find(mlxsw_sp, men_info->tb_id);
5406 if (WARN_ON(!vr))
5407 return;
5408
5409 mlxsw_sp_mr_route4_del(vr->mr4_table, men_info->mfc);
2b52ce02 5410 mlxsw_sp_vr_put(mlxsw_sp, vr);
d42b0965
YG
5411}
5412
5413static int
5414mlxsw_sp_router_fibmr_vif_add(struct mlxsw_sp *mlxsw_sp,
5415 struct vif_entry_notifier_info *ven_info)
5416{
5417 struct mlxsw_sp_rif *rif;
5418 struct mlxsw_sp_vr *vr;
5419
5420 if (mlxsw_sp->router->aborted)
5421 return 0;
5422
f8fa9b4e 5423 vr = mlxsw_sp_vr_get(mlxsw_sp, ven_info->tb_id, NULL);
d42b0965
YG
5424 if (IS_ERR(vr))
5425 return PTR_ERR(vr);
5426
5427 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, ven_info->dev);
5428 return mlxsw_sp_mr_vif_add(vr->mr4_table, ven_info->dev,
5429 ven_info->vif_index,
5430 ven_info->vif_flags, rif);
5431}
5432
5433static void
5434mlxsw_sp_router_fibmr_vif_del(struct mlxsw_sp *mlxsw_sp,
5435 struct vif_entry_notifier_info *ven_info)
5436{
5437 struct mlxsw_sp_vr *vr;
5438
5439 if (mlxsw_sp->router->aborted)
5440 return;
5441
5442 vr = mlxsw_sp_vr_find(mlxsw_sp, ven_info->tb_id);
5443 if (WARN_ON(!vr))
5444 return;
5445
5446 mlxsw_sp_mr_vif_del(vr->mr4_table, ven_info->vif_index);
2b52ce02 5447 mlxsw_sp_vr_put(mlxsw_sp, vr);
d42b0965
YG
5448}
5449
bc65a8a4
IS
5450static int mlxsw_sp_router_set_abort_trap(struct mlxsw_sp *mlxsw_sp)
5451{
5452 enum mlxsw_reg_ralxx_protocol proto = MLXSW_REG_RALXX_PROTOCOL_IPV4;
5453 int err;
5454
5455 err = __mlxsw_sp_router_set_abort_trap(mlxsw_sp, proto,
5456 MLXSW_SP_LPM_TREE_MIN);
5457 if (err)
5458 return err;
5459
d42b0965
YG
5460 /* The multicast router code does not need an abort trap as by default,
5461 * packets that don't match any routes are trapped to the CPU.
5462 */
5463
bc65a8a4
IS
5464 proto = MLXSW_REG_RALXX_PROTOCOL_IPV6;
5465 return __mlxsw_sp_router_set_abort_trap(mlxsw_sp, proto,
5466 MLXSW_SP_LPM_TREE_MIN + 1);
5467}
5468
9aecce1c
IS
5469static void mlxsw_sp_fib4_node_flush(struct mlxsw_sp *mlxsw_sp,
5470 struct mlxsw_sp_fib_node *fib_node)
5471{
4f1c7f1f 5472 struct mlxsw_sp_fib4_entry *fib4_entry, *tmp;
9aecce1c 5473
4f1c7f1f
IS
5474 list_for_each_entry_safe(fib4_entry, tmp, &fib_node->entry_list,
5475 common.list) {
5476 bool do_break = &tmp->common.list == &fib_node->entry_list;
9aecce1c 5477
4f1c7f1f
IS
5478 mlxsw_sp_fib4_node_entry_unlink(mlxsw_sp, fib4_entry);
5479 mlxsw_sp_fib4_entry_destroy(mlxsw_sp, fib4_entry);
731ea1ca 5480 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
9aecce1c
IS
5481 /* Break when entry list is empty and node was freed.
5482 * Otherwise, we'll access freed memory in the next
5483 * iteration.
5484 */
5485 if (do_break)
5486 break;
5487 }
5488}
5489
428b851f
IS
5490static void mlxsw_sp_fib6_node_flush(struct mlxsw_sp *mlxsw_sp,
5491 struct mlxsw_sp_fib_node *fib_node)
5492{
5493 struct mlxsw_sp_fib6_entry *fib6_entry, *tmp;
5494
5495 list_for_each_entry_safe(fib6_entry, tmp, &fib_node->entry_list,
5496 common.list) {
5497 bool do_break = &tmp->common.list == &fib_node->entry_list;
5498
5499 mlxsw_sp_fib6_node_entry_unlink(mlxsw_sp, fib6_entry);
5500 mlxsw_sp_fib6_entry_destroy(mlxsw_sp, fib6_entry);
5501 mlxsw_sp_fib_node_put(mlxsw_sp, fib_node);
5502 if (do_break)
5503 break;
5504 }
5505}
5506
9aecce1c
IS
5507static void mlxsw_sp_fib_node_flush(struct mlxsw_sp *mlxsw_sp,
5508 struct mlxsw_sp_fib_node *fib_node)
5509{
76610ebb 5510 switch (fib_node->fib->proto) {
9aecce1c
IS
5511 case MLXSW_SP_L3_PROTO_IPV4:
5512 mlxsw_sp_fib4_node_flush(mlxsw_sp, fib_node);
5513 break;
5514 case MLXSW_SP_L3_PROTO_IPV6:
428b851f 5515 mlxsw_sp_fib6_node_flush(mlxsw_sp, fib_node);
9aecce1c
IS
5516 break;
5517 }
5518}
5519
76610ebb
IS
5520static void mlxsw_sp_vr_fib_flush(struct mlxsw_sp *mlxsw_sp,
5521 struct mlxsw_sp_vr *vr,
5522 enum mlxsw_sp_l3proto proto)
b45f64d1 5523{
76610ebb 5524 struct mlxsw_sp_fib *fib = mlxsw_sp_vr_fib(vr, proto);
9aecce1c 5525 struct mlxsw_sp_fib_node *fib_node, *tmp;
76610ebb
IS
5526
5527 list_for_each_entry_safe(fib_node, tmp, &fib->node_list, list) {
5528 bool do_break = &tmp->list == &fib->node_list;
5529
5530 mlxsw_sp_fib_node_flush(mlxsw_sp, fib_node);
5531 if (do_break)
5532 break;
5533 }
5534}
5535
5536static void mlxsw_sp_router_fib_flush(struct mlxsw_sp *mlxsw_sp)
5537{
b45f64d1 5538 int i;
b45f64d1 5539
c1a38311 5540 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_VRS); i++) {
9011b677 5541 struct mlxsw_sp_vr *vr = &mlxsw_sp->router->vrs[i];
ac571de9 5542
76610ebb 5543 if (!mlxsw_sp_vr_is_used(vr))
b45f64d1 5544 continue;
d42b0965
YG
5545
5546 mlxsw_sp_mr_table_flush(vr->mr4_table);
76610ebb 5547 mlxsw_sp_vr_fib_flush(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV4);
a3d9bc50
IS
5548
5549 /* If virtual router was only used for IPv4, then it's no
5550 * longer used.
5551 */
5552 if (!mlxsw_sp_vr_is_used(vr))
5553 continue;
5554 mlxsw_sp_vr_fib_flush(mlxsw_sp, vr, MLXSW_SP_L3_PROTO_IPV6);
b45f64d1 5555 }
ac571de9
IS
5556}
5557
bc65a8a4 5558static void mlxsw_sp_router_fib_abort(struct mlxsw_sp *mlxsw_sp)
ac571de9
IS
5559{
5560 int err;
5561
9011b677 5562 if (mlxsw_sp->router->aborted)
d331d303
IS
5563 return;
5564 dev_warn(mlxsw_sp->bus_info->dev, "FIB abort triggered. Note that FIB entries are no longer being offloaded to this device.\n");
ac571de9 5565 mlxsw_sp_router_fib_flush(mlxsw_sp);
9011b677 5566 mlxsw_sp->router->aborted = true;
b45f64d1
JP
5567 err = mlxsw_sp_router_set_abort_trap(mlxsw_sp);
5568 if (err)
5569 dev_warn(mlxsw_sp->bus_info->dev, "Failed to set abort trap.\n");
5570}
5571
3057224e 5572struct mlxsw_sp_fib_event_work {
a0e4761d 5573 struct work_struct work;
ad178c8e 5574 union {
428b851f 5575 struct fib6_entry_notifier_info fen6_info;
ad178c8e 5576 struct fib_entry_notifier_info fen_info;
5d7bfd14 5577 struct fib_rule_notifier_info fr_info;
ad178c8e 5578 struct fib_nh_notifier_info fnh_info;
d42b0965
YG
5579 struct mfc_entry_notifier_info men_info;
5580 struct vif_entry_notifier_info ven_info;
ad178c8e 5581 };
3057224e
IS
5582 struct mlxsw_sp *mlxsw_sp;
5583 unsigned long event;
5584};
5585
66a5763a 5586static void mlxsw_sp_router_fib4_event_work(struct work_struct *work)
b45f64d1 5587{
3057224e 5588 struct mlxsw_sp_fib_event_work *fib_work =
a0e4761d 5589 container_of(work, struct mlxsw_sp_fib_event_work, work);
3057224e 5590 struct mlxsw_sp *mlxsw_sp = fib_work->mlxsw_sp;
599cf8f9 5591 bool replace, append;
b45f64d1
JP
5592 int err;
5593
3057224e
IS
5594 /* Protect internal structures from changes */
5595 rtnl_lock();
803335ac
PM
5596 mlxsw_sp_span_respin(mlxsw_sp);
5597
3057224e 5598 switch (fib_work->event) {
599cf8f9 5599 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
4283bce5 5600 case FIB_EVENT_ENTRY_APPEND: /* fall through */
b45f64d1 5601 case FIB_EVENT_ENTRY_ADD:
599cf8f9 5602 replace = fib_work->event == FIB_EVENT_ENTRY_REPLACE;
4283bce5
IS
5603 append = fib_work->event == FIB_EVENT_ENTRY_APPEND;
5604 err = mlxsw_sp_router_fib4_add(mlxsw_sp, &fib_work->fen_info,
599cf8f9 5605 replace, append);
b45f64d1 5606 if (err)
bc65a8a4 5607 mlxsw_sp_router_fib_abort(mlxsw_sp);
3057224e 5608 fib_info_put(fib_work->fen_info.fi);
b45f64d1
JP
5609 break;
5610 case FIB_EVENT_ENTRY_DEL:
3057224e
IS
5611 mlxsw_sp_router_fib4_del(mlxsw_sp, &fib_work->fen_info);
5612 fib_info_put(fib_work->fen_info.fi);
b45f64d1 5613 break;
1f279233
DA
5614 case FIB_EVENT_RULE_ADD:
5615 /* if we get here, a rule was added that we do not support.
5616 * just do the fib_abort
5617 */
5618 mlxsw_sp_router_fib_abort(mlxsw_sp);
b45f64d1 5619 break;
ad178c8e
IS
5620 case FIB_EVENT_NH_ADD: /* fall through */
5621 case FIB_EVENT_NH_DEL:
0e6ea2a4
IS
5622 mlxsw_sp_nexthop4_event(mlxsw_sp, fib_work->event,
5623 fib_work->fnh_info.fib_nh);
ad178c8e
IS
5624 fib_info_put(fib_work->fnh_info.fib_nh->nh_parent);
5625 break;
b45f64d1 5626 }
3057224e
IS
5627 rtnl_unlock();
5628 kfree(fib_work);
5629}
5630
66a5763a
IS
5631static void mlxsw_sp_router_fib6_event_work(struct work_struct *work)
5632{
583419fd
IS
5633 struct mlxsw_sp_fib_event_work *fib_work =
5634 container_of(work, struct mlxsw_sp_fib_event_work, work);
5635 struct mlxsw_sp *mlxsw_sp = fib_work->mlxsw_sp;
0a7fd1ac 5636 bool replace;
428b851f 5637 int err;
583419fd
IS
5638
5639 rtnl_lock();
803335ac
PM
5640 mlxsw_sp_span_respin(mlxsw_sp);
5641
583419fd 5642 switch (fib_work->event) {
0a7fd1ac 5643 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
428b851f 5644 case FIB_EVENT_ENTRY_ADD:
0a7fd1ac 5645 replace = fib_work->event == FIB_EVENT_ENTRY_REPLACE;
428b851f 5646 err = mlxsw_sp_router_fib6_add(mlxsw_sp,
0a7fd1ac 5647 fib_work->fen6_info.rt, replace);
428b851f
IS
5648 if (err)
5649 mlxsw_sp_router_fib_abort(mlxsw_sp);
5650 mlxsw_sp_rt6_release(fib_work->fen6_info.rt);
5651 break;
5652 case FIB_EVENT_ENTRY_DEL:
5653 mlxsw_sp_router_fib6_del(mlxsw_sp, fib_work->fen6_info.rt);
5654 mlxsw_sp_rt6_release(fib_work->fen6_info.rt);
5655 break;
1f279233
DA
5656 case FIB_EVENT_RULE_ADD:
5657 /* if we get here, a rule was added that we do not support.
5658 * just do the fib_abort
5659 */
5660 mlxsw_sp_router_fib_abort(mlxsw_sp);
583419fd
IS
5661 break;
5662 }
5663 rtnl_unlock();
5664 kfree(fib_work);
66a5763a
IS
5665}
5666
d42b0965
YG
5667static void mlxsw_sp_router_fibmr_event_work(struct work_struct *work)
5668{
5669 struct mlxsw_sp_fib_event_work *fib_work =
5670 container_of(work, struct mlxsw_sp_fib_event_work, work);
5671 struct mlxsw_sp *mlxsw_sp = fib_work->mlxsw_sp;
d42b0965
YG
5672 bool replace;
5673 int err;
5674
5675 rtnl_lock();
5676 switch (fib_work->event) {
5677 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
5678 case FIB_EVENT_ENTRY_ADD:
5679 replace = fib_work->event == FIB_EVENT_ENTRY_REPLACE;
5680
5681 err = mlxsw_sp_router_fibmr_add(mlxsw_sp, &fib_work->men_info,
5682 replace);
5683 if (err)
5684 mlxsw_sp_router_fib_abort(mlxsw_sp);
5685 ipmr_cache_put(fib_work->men_info.mfc);
5686 break;
5687 case FIB_EVENT_ENTRY_DEL:
5688 mlxsw_sp_router_fibmr_del(mlxsw_sp, &fib_work->men_info);
5689 ipmr_cache_put(fib_work->men_info.mfc);
5690 break;
5691 case FIB_EVENT_VIF_ADD:
5692 err = mlxsw_sp_router_fibmr_vif_add(mlxsw_sp,
5693 &fib_work->ven_info);
5694 if (err)
5695 mlxsw_sp_router_fib_abort(mlxsw_sp);
5696 dev_put(fib_work->ven_info.dev);
5697 break;
5698 case FIB_EVENT_VIF_DEL:
5699 mlxsw_sp_router_fibmr_vif_del(mlxsw_sp,
5700 &fib_work->ven_info);
5701 dev_put(fib_work->ven_info.dev);
5702 break;
1f279233
DA
5703 case FIB_EVENT_RULE_ADD:
5704 /* if we get here, a rule was added that we do not support.
5705 * just do the fib_abort
5706 */
5707 mlxsw_sp_router_fib_abort(mlxsw_sp);
d42b0965
YG
5708 break;
5709 }
5710 rtnl_unlock();
5711 kfree(fib_work);
5712}
5713
66a5763a
IS
5714static void mlxsw_sp_router_fib4_event(struct mlxsw_sp_fib_event_work *fib_work,
5715 struct fib_notifier_info *info)
5716{
3c75f9b1 5717 struct fib_entry_notifier_info *fen_info;
3c75f9b1
DA
5718 struct fib_nh_notifier_info *fnh_info;
5719
66a5763a
IS
5720 switch (fib_work->event) {
5721 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
5722 case FIB_EVENT_ENTRY_APPEND: /* fall through */
5723 case FIB_EVENT_ENTRY_ADD: /* fall through */
5724 case FIB_EVENT_ENTRY_DEL:
3c75f9b1
DA
5725 fen_info = container_of(info, struct fib_entry_notifier_info,
5726 info);
5727 fib_work->fen_info = *fen_info;
5728 /* Take reference on fib_info to prevent it from being
66a5763a
IS
5729 * freed while work is queued. Release it afterwards.
5730 */
5731 fib_info_hold(fib_work->fen_info.fi);
5732 break;
66a5763a
IS
5733 case FIB_EVENT_NH_ADD: /* fall through */
5734 case FIB_EVENT_NH_DEL:
3c75f9b1
DA
5735 fnh_info = container_of(info, struct fib_nh_notifier_info,
5736 info);
5737 fib_work->fnh_info = *fnh_info;
66a5763a
IS
5738 fib_info_hold(fib_work->fnh_info.fib_nh->nh_parent);
5739 break;
5740 }
5741}
5742
5743static void mlxsw_sp_router_fib6_event(struct mlxsw_sp_fib_event_work *fib_work,
5744 struct fib_notifier_info *info)
5745{
3c75f9b1 5746 struct fib6_entry_notifier_info *fen6_info;
3c75f9b1 5747
583419fd 5748 switch (fib_work->event) {
0a7fd1ac 5749 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
428b851f
IS
5750 case FIB_EVENT_ENTRY_ADD: /* fall through */
5751 case FIB_EVENT_ENTRY_DEL:
3c75f9b1
DA
5752 fen6_info = container_of(info, struct fib6_entry_notifier_info,
5753 info);
5754 fib_work->fen6_info = *fen6_info;
428b851f
IS
5755 rt6_hold(fib_work->fen6_info.rt);
5756 break;
583419fd 5757 }
66a5763a
IS
5758}
5759
d42b0965
YG
5760static void
5761mlxsw_sp_router_fibmr_event(struct mlxsw_sp_fib_event_work *fib_work,
5762 struct fib_notifier_info *info)
5763{
5764 switch (fib_work->event) {
5765 case FIB_EVENT_ENTRY_REPLACE: /* fall through */
5766 case FIB_EVENT_ENTRY_ADD: /* fall through */
5767 case FIB_EVENT_ENTRY_DEL:
5768 memcpy(&fib_work->men_info, info, sizeof(fib_work->men_info));
5769 ipmr_cache_hold(fib_work->men_info.mfc);
5770 break;
5771 case FIB_EVENT_VIF_ADD: /* fall through */
5772 case FIB_EVENT_VIF_DEL:
5773 memcpy(&fib_work->ven_info, info, sizeof(fib_work->ven_info));
5774 dev_hold(fib_work->ven_info.dev);
5775 break;
1f279233
DA
5776 }
5777}
5778
5779static int mlxsw_sp_router_fib_rule_event(unsigned long event,
5780 struct fib_notifier_info *info,
5781 struct mlxsw_sp *mlxsw_sp)
5782{
5783 struct netlink_ext_ack *extack = info->extack;
5784 struct fib_rule_notifier_info *fr_info;
5785 struct fib_rule *rule;
5786 int err = 0;
5787
5788 /* nothing to do at the moment */
5789 if (event == FIB_EVENT_RULE_DEL)
5790 return 0;
5791
5792 if (mlxsw_sp->router->aborted)
5793 return 0;
5794
5795 fr_info = container_of(info, struct fib_rule_notifier_info, info);
5796 rule = fr_info->rule;
5797
5798 switch (info->family) {
5799 case AF_INET:
5800 if (!fib4_rule_default(rule) && !rule->l3mdev)
5801 err = -1;
5802 break;
5803 case AF_INET6:
5804 if (!fib6_rule_default(rule) && !rule->l3mdev)
5805 err = -1;
5806 break;
5807 case RTNL_FAMILY_IPMR:
5808 if (!ipmr_rule_default(rule) && !rule->l3mdev)
5809 err = -1;
d42b0965
YG
5810 break;
5811 }
1f279233
DA
5812
5813 if (err < 0)
6c677750 5814 NL_SET_ERR_MSG_MOD(extack, "FIB rules not supported. Aborting offload");
1f279233
DA
5815
5816 return err;
d42b0965
YG
5817}
5818
3057224e
IS
5819/* Called with rcu_read_lock() */
5820static int mlxsw_sp_router_fib_event(struct notifier_block *nb,
5821 unsigned long event, void *ptr)
5822{
3057224e
IS
5823 struct mlxsw_sp_fib_event_work *fib_work;
5824 struct fib_notifier_info *info = ptr;
7e39d115 5825 struct mlxsw_sp_router *router;
1f279233 5826 int err;
3057224e 5827
8e29f979 5828 if (!net_eq(info->net, &init_net) ||
664375e9
YG
5829 (info->family != AF_INET && info->family != AF_INET6 &&
5830 info->family != RTNL_FAMILY_IPMR))
3057224e
IS
5831 return NOTIFY_DONE;
5832
1f279233
DA
5833 router = container_of(nb, struct mlxsw_sp_router, fib_nb);
5834
5835 switch (event) {
5836 case FIB_EVENT_RULE_ADD: /* fall through */
5837 case FIB_EVENT_RULE_DEL:
5838 err = mlxsw_sp_router_fib_rule_event(event, info,
5839 router->mlxsw_sp);
5840 if (!err)
5841 return NOTIFY_DONE;
5842 }
5843
3057224e
IS
5844 fib_work = kzalloc(sizeof(*fib_work), GFP_ATOMIC);
5845 if (WARN_ON(!fib_work))
5846 return NOTIFY_BAD;
5847
7e39d115 5848 fib_work->mlxsw_sp = router->mlxsw_sp;
3057224e
IS
5849 fib_work->event = event;
5850
66a5763a
IS
5851 switch (info->family) {
5852 case AF_INET:
5853 INIT_WORK(&fib_work->work, mlxsw_sp_router_fib4_event_work);
5854 mlxsw_sp_router_fib4_event(fib_work, info);
3057224e 5855 break;
66a5763a
IS
5856 case AF_INET6:
5857 INIT_WORK(&fib_work->work, mlxsw_sp_router_fib6_event_work);
5858 mlxsw_sp_router_fib6_event(fib_work, info);
ad178c8e 5859 break;
d42b0965
YG
5860 case RTNL_FAMILY_IPMR:
5861 INIT_WORK(&fib_work->work, mlxsw_sp_router_fibmr_event_work);
5862 mlxsw_sp_router_fibmr_event(fib_work, info);
5863 break;
3057224e
IS
5864 }
5865
a0e4761d 5866 mlxsw_core_schedule_work(&fib_work->work);
3057224e 5867
b45f64d1
JP
5868 return NOTIFY_DONE;
5869}
5870
4724ba56
IS
5871static struct mlxsw_sp_rif *
5872mlxsw_sp_rif_find_by_dev(const struct mlxsw_sp *mlxsw_sp,
5873 const struct net_device *dev)
5874{
5875 int i;
5876
5877 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
5f9efffb
IS
5878 if (mlxsw_sp->router->rifs[i] &&
5879 mlxsw_sp->router->rifs[i]->dev == dev)
5880 return mlxsw_sp->router->rifs[i];
4724ba56
IS
5881
5882 return NULL;
5883}
5884
5885static int mlxsw_sp_router_rif_disable(struct mlxsw_sp *mlxsw_sp, u16 rif)
5886{
5887 char ritr_pl[MLXSW_REG_RITR_LEN];
5888 int err;
5889
5890 mlxsw_reg_ritr_rif_pack(ritr_pl, rif);
5891 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
5892 if (WARN_ON_ONCE(err))
5893 return err;
5894
5895 mlxsw_reg_ritr_enable_set(ritr_pl, false);
5896 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
5897}
5898
5899static void mlxsw_sp_router_rif_gone_sync(struct mlxsw_sp *mlxsw_sp,
bf95233e 5900 struct mlxsw_sp_rif *rif)
4724ba56 5901{
bf95233e
AS
5902 mlxsw_sp_router_rif_disable(mlxsw_sp, rif->rif_index);
5903 mlxsw_sp_nexthop_rif_gone_sync(mlxsw_sp, rif);
5904 mlxsw_sp_neigh_rif_gone_sync(mlxsw_sp, rif);
4724ba56
IS
5905}
5906
5ea1237f
AS
5907static bool
5908mlxsw_sp_rif_should_config(struct mlxsw_sp_rif *rif, struct net_device *dev,
5909 unsigned long event)
4724ba56 5910{
5ea1237f
AS
5911 struct inet6_dev *inet6_dev;
5912 bool addr_list_empty = true;
5913 struct in_device *idev;
5914
4724ba56
IS
5915 switch (event) {
5916 case NETDEV_UP:
f1b1f273 5917 return rif == NULL;
4724ba56 5918 case NETDEV_DOWN:
5ea1237f
AS
5919 idev = __in_dev_get_rtnl(dev);
5920 if (idev && idev->ifa_list)
5921 addr_list_empty = false;
5922
5923 inet6_dev = __in6_dev_get(dev);
5924 if (addr_list_empty && inet6_dev &&
5925 !list_empty(&inet6_dev->addr_list))
5926 addr_list_empty = false;
5927
5928 if (rif && addr_list_empty &&
bf95233e 5929 !netif_is_l3_slave(rif->dev))
4724ba56
IS
5930 return true;
5931 /* It is possible we already removed the RIF ourselves
5932 * if it was assigned to a netdev that is now a bridge
5933 * or LAG slave.
5934 */
5935 return false;
5936 }
5937
5938 return false;
5939}
5940
e4f3c1c1
IS
5941static enum mlxsw_sp_rif_type
5942mlxsw_sp_dev_rif_type(const struct mlxsw_sp *mlxsw_sp,
5943 const struct net_device *dev)
5944{
5945 enum mlxsw_sp_fid_type type;
5946
6ddb7426
PM
5947 if (mlxsw_sp_netdev_ipip_type(mlxsw_sp, dev, NULL))
5948 return MLXSW_SP_RIF_TYPE_IPIP_LB;
5949
5950 /* Otherwise RIF type is derived from the type of the underlying FID. */
e4f3c1c1
IS
5951 if (is_vlan_dev(dev) && netif_is_bridge_master(vlan_dev_real_dev(dev)))
5952 type = MLXSW_SP_FID_TYPE_8021Q;
5953 else if (netif_is_bridge_master(dev) && br_vlan_enabled(dev))
5954 type = MLXSW_SP_FID_TYPE_8021Q;
5955 else if (netif_is_bridge_master(dev))
5956 type = MLXSW_SP_FID_TYPE_8021D;
5957 else
5958 type = MLXSW_SP_FID_TYPE_RFID;
5959
5960 return mlxsw_sp_fid_type_rif_type(mlxsw_sp, type);
5961}
5962
de5ed99e 5963static int mlxsw_sp_rif_index_alloc(struct mlxsw_sp *mlxsw_sp, u16 *p_rif_index)
4724ba56
IS
5964{
5965 int i;
5966
de5ed99e
IS
5967 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++) {
5968 if (!mlxsw_sp->router->rifs[i]) {
5969 *p_rif_index = i;
5970 return 0;
5971 }
5972 }
4724ba56 5973
de5ed99e 5974 return -ENOBUFS;
4724ba56
IS
5975}
5976
e4f3c1c1
IS
5977static struct mlxsw_sp_rif *mlxsw_sp_rif_alloc(size_t rif_size, u16 rif_index,
5978 u16 vr_id,
5979 struct net_device *l3_dev)
4724ba56 5980{
bf95233e 5981 struct mlxsw_sp_rif *rif;
4724ba56 5982
e4f3c1c1 5983 rif = kzalloc(rif_size, GFP_KERNEL);
bf95233e 5984 if (!rif)
4724ba56
IS
5985 return NULL;
5986
bf95233e
AS
5987 INIT_LIST_HEAD(&rif->nexthop_list);
5988 INIT_LIST_HEAD(&rif->neigh_list);
5989 ether_addr_copy(rif->addr, l3_dev->dev_addr);
5990 rif->mtu = l3_dev->mtu;
5991 rif->vr_id = vr_id;
5992 rif->dev = l3_dev;
5993 rif->rif_index = rif_index;
4724ba56 5994
bf95233e 5995 return rif;
4724ba56
IS
5996}
5997
5f9efffb
IS
5998struct mlxsw_sp_rif *mlxsw_sp_rif_by_index(const struct mlxsw_sp *mlxsw_sp,
5999 u16 rif_index)
6000{
6001 return mlxsw_sp->router->rifs[rif_index];
6002}
6003
fd1b9d41
AS
6004u16 mlxsw_sp_rif_index(const struct mlxsw_sp_rif *rif)
6005{
6006 return rif->rif_index;
6007}
6008
92107cfb
PM
6009u16 mlxsw_sp_ipip_lb_rif_index(const struct mlxsw_sp_rif_ipip_lb *lb_rif)
6010{
6011 return lb_rif->common.rif_index;
6012}
6013
6014u16 mlxsw_sp_ipip_lb_ul_vr_id(const struct mlxsw_sp_rif_ipip_lb *lb_rif)
6015{
6016 return lb_rif->ul_vr_id;
6017}
6018
fd1b9d41
AS
6019int mlxsw_sp_rif_dev_ifindex(const struct mlxsw_sp_rif *rif)
6020{
6021 return rif->dev->ifindex;
6022}
6023
91e4d59a
YG
6024const struct net_device *mlxsw_sp_rif_dev(const struct mlxsw_sp_rif *rif)
6025{
6026 return rif->dev;
6027}
6028
4724ba56 6029static struct mlxsw_sp_rif *
e4f3c1c1 6030mlxsw_sp_rif_create(struct mlxsw_sp *mlxsw_sp,
f8fa9b4e
DA
6031 const struct mlxsw_sp_rif_params *params,
6032 struct netlink_ext_ack *extack)
4724ba56 6033{
e4f3c1c1
IS
6034 u32 tb_id = l3mdev_fib_table(params->dev);
6035 const struct mlxsw_sp_rif_ops *ops;
010cadf9 6036 struct mlxsw_sp_fid *fid = NULL;
e4f3c1c1 6037 enum mlxsw_sp_rif_type type;
bf95233e 6038 struct mlxsw_sp_rif *rif;
a1107487
IS
6039 struct mlxsw_sp_vr *vr;
6040 u16 rif_index;
4724ba56
IS
6041 int err;
6042
e4f3c1c1
IS
6043 type = mlxsw_sp_dev_rif_type(mlxsw_sp, params->dev);
6044 ops = mlxsw_sp->router->rif_ops_arr[type];
6045
f8fa9b4e 6046 vr = mlxsw_sp_vr_get(mlxsw_sp, tb_id ? : RT_TABLE_MAIN, extack);
c9ec53f0
IS
6047 if (IS_ERR(vr))
6048 return ERR_CAST(vr);
28a04c7b 6049 vr->rif_count++;
c9ec53f0 6050
de5ed99e 6051 err = mlxsw_sp_rif_index_alloc(mlxsw_sp, &rif_index);
f8fa9b4e 6052 if (err) {
6c677750 6053 NL_SET_ERR_MSG_MOD(extack, "Exceeded number of supported router interfaces");
de5ed99e 6054 goto err_rif_index_alloc;
f8fa9b4e 6055 }
4724ba56 6056
e4f3c1c1 6057 rif = mlxsw_sp_rif_alloc(ops->rif_size, rif_index, vr->id, params->dev);
a13a594d
IS
6058 if (!rif) {
6059 err = -ENOMEM;
6060 goto err_rif_alloc;
6061 }
e4f3c1c1
IS
6062 rif->mlxsw_sp = mlxsw_sp;
6063 rif->ops = ops;
a13a594d 6064
010cadf9
PM
6065 if (ops->fid_get) {
6066 fid = ops->fid_get(rif);
6067 if (IS_ERR(fid)) {
6068 err = PTR_ERR(fid);
6069 goto err_fid_get;
6070 }
6071 rif->fid = fid;
4d93ceeb
IS
6072 }
6073
e4f3c1c1
IS
6074 if (ops->setup)
6075 ops->setup(rif, params);
6076
6077 err = ops->configure(rif);
4724ba56 6078 if (err)
e4f3c1c1 6079 goto err_configure;
4724ba56 6080
d42b0965
YG
6081 err = mlxsw_sp_mr_rif_add(vr->mr4_table, rif);
6082 if (err)
6083 goto err_mr_rif_add;
6084
e4f3c1c1 6085 mlxsw_sp_rif_counters_alloc(rif);
5f9efffb 6086 mlxsw_sp->router->rifs[rif_index] = rif;
4724ba56 6087
bf95233e 6088 return rif;
4724ba56 6089
d42b0965
YG
6090err_mr_rif_add:
6091 ops->deconfigure(rif);
e4f3c1c1 6092err_configure:
010cadf9
PM
6093 if (fid)
6094 mlxsw_sp_fid_put(fid);
a1107487 6095err_fid_get:
e4f3c1c1
IS
6096 kfree(rif);
6097err_rif_alloc:
de5ed99e 6098err_rif_index_alloc:
28a04c7b 6099 vr->rif_count--;
2b52ce02 6100 mlxsw_sp_vr_put(mlxsw_sp, vr);
4724ba56
IS
6101 return ERR_PTR(err);
6102}
6103
e4f3c1c1 6104void mlxsw_sp_rif_destroy(struct mlxsw_sp_rif *rif)
4724ba56 6105{
e4f3c1c1
IS
6106 const struct mlxsw_sp_rif_ops *ops = rif->ops;
6107 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
a1107487 6108 struct mlxsw_sp_fid *fid = rif->fid;
e4f3c1c1 6109 struct mlxsw_sp_vr *vr;
4724ba56 6110
bf95233e 6111 mlxsw_sp_router_rif_gone_sync(mlxsw_sp, rif);
e4f3c1c1 6112 vr = &mlxsw_sp->router->vrs[rif->vr_id];
e0c0afd8 6113
e4f3c1c1 6114 mlxsw_sp->router->rifs[rif->rif_index] = NULL;
e4f3c1c1 6115 mlxsw_sp_rif_counters_free(rif);
d42b0965 6116 mlxsw_sp_mr_rif_del(vr->mr4_table, rif);
e4f3c1c1 6117 ops->deconfigure(rif);
010cadf9
PM
6118 if (fid)
6119 /* Loopback RIFs are not associated with a FID. */
6120 mlxsw_sp_fid_put(fid);
e4f3c1c1 6121 kfree(rif);
28a04c7b 6122 vr->rif_count--;
2b52ce02 6123 mlxsw_sp_vr_put(mlxsw_sp, vr);
4724ba56
IS
6124}
6125
e4f3c1c1
IS
6126static void
6127mlxsw_sp_rif_subport_params_init(struct mlxsw_sp_rif_params *params,
6128 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
6129{
6130 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
6131
6132 params->vid = mlxsw_sp_port_vlan->vid;
6133 params->lag = mlxsw_sp_port->lagged;
6134 if (params->lag)
6135 params->lag_id = mlxsw_sp_port->lag_id;
6136 else
6137 params->system_port = mlxsw_sp_port->local_port;
6138}
6139
7cbecf24 6140static int
a1107487 6141mlxsw_sp_port_vlan_router_join(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan,
f8fa9b4e
DA
6142 struct net_device *l3_dev,
6143 struct netlink_ext_ack *extack)
4724ba56 6144{
7cbecf24 6145 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
1b8f09a0 6146 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
7cbecf24 6147 u16 vid = mlxsw_sp_port_vlan->vid;
bf95233e 6148 struct mlxsw_sp_rif *rif;
a1107487 6149 struct mlxsw_sp_fid *fid;
03ea01e9 6150 int err;
4724ba56 6151
1b8f09a0 6152 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
bf95233e 6153 if (!rif) {
e4f3c1c1
IS
6154 struct mlxsw_sp_rif_params params = {
6155 .dev = l3_dev,
6156 };
6157
6158 mlxsw_sp_rif_subport_params_init(&params, mlxsw_sp_port_vlan);
f8fa9b4e 6159 rif = mlxsw_sp_rif_create(mlxsw_sp, &params, extack);
bf95233e
AS
6160 if (IS_ERR(rif))
6161 return PTR_ERR(rif);
4724ba56
IS
6162 }
6163
a1107487 6164 /* FID was already created, just take a reference */
e4f3c1c1 6165 fid = rif->ops->fid_get(rif);
a1107487
IS
6166 err = mlxsw_sp_fid_port_vid_map(fid, mlxsw_sp_port, vid);
6167 if (err)
6168 goto err_fid_port_vid_map;
6169
7cbecf24 6170 err = mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, false);
03ea01e9
IS
6171 if (err)
6172 goto err_port_vid_learning_set;
6173
7cbecf24 6174 err = mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid,
03ea01e9
IS
6175 BR_STATE_FORWARDING);
6176 if (err)
6177 goto err_port_vid_stp_set;
6178
a1107487 6179 mlxsw_sp_port_vlan->fid = fid;
4724ba56 6180
4724ba56 6181 return 0;
03ea01e9
IS
6182
6183err_port_vid_stp_set:
7cbecf24 6184 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
03ea01e9 6185err_port_vid_learning_set:
a1107487
IS
6186 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
6187err_fid_port_vid_map:
6188 mlxsw_sp_fid_put(fid);
03ea01e9 6189 return err;
4724ba56
IS
6190}
6191
a1107487
IS
6192void
6193mlxsw_sp_port_vlan_router_leave(struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan)
4724ba56 6194{
ce95e154 6195 struct mlxsw_sp_port *mlxsw_sp_port = mlxsw_sp_port_vlan->mlxsw_sp_port;
7cbecf24 6196 struct mlxsw_sp_fid *fid = mlxsw_sp_port_vlan->fid;
ce95e154 6197 u16 vid = mlxsw_sp_port_vlan->vid;
ce95e154 6198
a1107487
IS
6199 if (WARN_ON(mlxsw_sp_fid_type(fid) != MLXSW_SP_FID_TYPE_RFID))
6200 return;
4aafc368 6201
a1107487 6202 mlxsw_sp_port_vlan->fid = NULL;
7cbecf24
IS
6203 mlxsw_sp_port_vid_stp_set(mlxsw_sp_port, vid, BR_STATE_BLOCKING);
6204 mlxsw_sp_port_vid_learning_set(mlxsw_sp_port, vid, true);
a1107487
IS
6205 mlxsw_sp_fid_port_vid_unmap(fid, mlxsw_sp_port, vid);
6206 /* If router port holds the last reference on the rFID, then the
6207 * associated Sub-port RIF will be destroyed.
6208 */
6209 mlxsw_sp_fid_put(fid);
4724ba56
IS
6210}
6211
7cbecf24
IS
6212static int mlxsw_sp_inetaddr_port_vlan_event(struct net_device *l3_dev,
6213 struct net_device *port_dev,
f8fa9b4e
DA
6214 unsigned long event, u16 vid,
6215 struct netlink_ext_ack *extack)
4724ba56
IS
6216{
6217 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(port_dev);
ce95e154 6218 struct mlxsw_sp_port_vlan *mlxsw_sp_port_vlan;
4724ba56 6219
ce95e154 6220 mlxsw_sp_port_vlan = mlxsw_sp_port_vlan_find_by_vid(mlxsw_sp_port, vid);
7cbecf24
IS
6221 if (WARN_ON(!mlxsw_sp_port_vlan))
6222 return -EINVAL;
4724ba56
IS
6223
6224 switch (event) {
6225 case NETDEV_UP:
a1107487 6226 return mlxsw_sp_port_vlan_router_join(mlxsw_sp_port_vlan,
f8fa9b4e 6227 l3_dev, extack);
4724ba56 6228 case NETDEV_DOWN:
a1107487 6229 mlxsw_sp_port_vlan_router_leave(mlxsw_sp_port_vlan);
4724ba56
IS
6230 break;
6231 }
6232
6233 return 0;
6234}
6235
6236static int mlxsw_sp_inetaddr_port_event(struct net_device *port_dev,
f8fa9b4e
DA
6237 unsigned long event,
6238 struct netlink_ext_ack *extack)
4724ba56 6239{
2b94e58d
JP
6240 if (netif_is_bridge_port(port_dev) ||
6241 netif_is_lag_port(port_dev) ||
6242 netif_is_ovs_port(port_dev))
4724ba56
IS
6243 return 0;
6244
f8fa9b4e
DA
6245 return mlxsw_sp_inetaddr_port_vlan_event(port_dev, port_dev, event, 1,
6246 extack);
4724ba56
IS
6247}
6248
6249static int __mlxsw_sp_inetaddr_lag_event(struct net_device *l3_dev,
6250 struct net_device *lag_dev,
f8fa9b4e
DA
6251 unsigned long event, u16 vid,
6252 struct netlink_ext_ack *extack)
4724ba56
IS
6253{
6254 struct net_device *port_dev;
6255 struct list_head *iter;
6256 int err;
6257
6258 netdev_for_each_lower_dev(lag_dev, port_dev, iter) {
6259 if (mlxsw_sp_port_dev_check(port_dev)) {
7cbecf24
IS
6260 err = mlxsw_sp_inetaddr_port_vlan_event(l3_dev,
6261 port_dev,
f8fa9b4e
DA
6262 event, vid,
6263 extack);
4724ba56
IS
6264 if (err)
6265 return err;
6266 }
6267 }
6268
6269 return 0;
6270}
6271
6272static int mlxsw_sp_inetaddr_lag_event(struct net_device *lag_dev,
f8fa9b4e
DA
6273 unsigned long event,
6274 struct netlink_ext_ack *extack)
4724ba56
IS
6275{
6276 if (netif_is_bridge_port(lag_dev))
6277 return 0;
6278
f8fa9b4e
DA
6279 return __mlxsw_sp_inetaddr_lag_event(lag_dev, lag_dev, event, 1,
6280 extack);
4724ba56
IS
6281}
6282
4724ba56 6283static int mlxsw_sp_inetaddr_bridge_event(struct net_device *l3_dev,
f8fa9b4e
DA
6284 unsigned long event,
6285 struct netlink_ext_ack *extack)
4724ba56
IS
6286{
6287 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev);
e4f3c1c1
IS
6288 struct mlxsw_sp_rif_params params = {
6289 .dev = l3_dev,
6290 };
a1107487 6291 struct mlxsw_sp_rif *rif;
4724ba56
IS
6292
6293 switch (event) {
6294 case NETDEV_UP:
f8fa9b4e 6295 rif = mlxsw_sp_rif_create(mlxsw_sp, &params, extack);
e4f3c1c1
IS
6296 if (IS_ERR(rif))
6297 return PTR_ERR(rif);
6298 break;
4724ba56 6299 case NETDEV_DOWN:
a1107487 6300 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
e4f3c1c1 6301 mlxsw_sp_rif_destroy(rif);
4724ba56
IS
6302 break;
6303 }
6304
6305 return 0;
6306}
6307
6308static int mlxsw_sp_inetaddr_vlan_event(struct net_device *vlan_dev,
f8fa9b4e
DA
6309 unsigned long event,
6310 struct netlink_ext_ack *extack)
4724ba56
IS
6311{
6312 struct net_device *real_dev = vlan_dev_real_dev(vlan_dev);
4724ba56
IS
6313 u16 vid = vlan_dev_vlan_id(vlan_dev);
6314
6b27c8ad
IS
6315 if (netif_is_bridge_port(vlan_dev))
6316 return 0;
6317
4724ba56 6318 if (mlxsw_sp_port_dev_check(real_dev))
7cbecf24 6319 return mlxsw_sp_inetaddr_port_vlan_event(vlan_dev, real_dev,
f8fa9b4e 6320 event, vid, extack);
4724ba56
IS
6321 else if (netif_is_lag_master(real_dev))
6322 return __mlxsw_sp_inetaddr_lag_event(vlan_dev, real_dev, event,
f8fa9b4e 6323 vid, extack);
c57529e1 6324 else if (netif_is_bridge_master(real_dev) && br_vlan_enabled(real_dev))
f8fa9b4e 6325 return mlxsw_sp_inetaddr_bridge_event(vlan_dev, event, extack);
4724ba56
IS
6326
6327 return 0;
6328}
6329
b1e45526 6330static int __mlxsw_sp_inetaddr_event(struct net_device *dev,
f8fa9b4e
DA
6331 unsigned long event,
6332 struct netlink_ext_ack *extack)
b1e45526
IS
6333{
6334 if (mlxsw_sp_port_dev_check(dev))
f8fa9b4e 6335 return mlxsw_sp_inetaddr_port_event(dev, event, extack);
b1e45526 6336 else if (netif_is_lag_master(dev))
f8fa9b4e 6337 return mlxsw_sp_inetaddr_lag_event(dev, event, extack);
b1e45526 6338 else if (netif_is_bridge_master(dev))
f8fa9b4e 6339 return mlxsw_sp_inetaddr_bridge_event(dev, event, extack);
b1e45526 6340 else if (is_vlan_dev(dev))
f8fa9b4e 6341 return mlxsw_sp_inetaddr_vlan_event(dev, event, extack);
b1e45526
IS
6342 else
6343 return 0;
6344}
6345
4724ba56
IS
6346int mlxsw_sp_inetaddr_event(struct notifier_block *unused,
6347 unsigned long event, void *ptr)
6348{
6349 struct in_ifaddr *ifa = (struct in_ifaddr *) ptr;
6350 struct net_device *dev = ifa->ifa_dev->dev;
6351 struct mlxsw_sp *mlxsw_sp;
bf95233e 6352 struct mlxsw_sp_rif *rif;
4724ba56
IS
6353 int err = 0;
6354
89d5dd2e
DA
6355 /* NETDEV_UP event is handled by mlxsw_sp_inetaddr_valid_event */
6356 if (event == NETDEV_UP)
6357 goto out;
6358
6359 mlxsw_sp = mlxsw_sp_lower_get(dev);
6360 if (!mlxsw_sp)
6361 goto out;
6362
6363 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
6364 if (!mlxsw_sp_rif_should_config(rif, dev, event))
6365 goto out;
6366
f8fa9b4e 6367 err = __mlxsw_sp_inetaddr_event(dev, event, NULL);
89d5dd2e
DA
6368out:
6369 return notifier_from_errno(err);
6370}
6371
6372int mlxsw_sp_inetaddr_valid_event(struct notifier_block *unused,
6373 unsigned long event, void *ptr)
6374{
6375 struct in_validator_info *ivi = (struct in_validator_info *) ptr;
6376 struct net_device *dev = ivi->ivi_dev->dev;
6377 struct mlxsw_sp *mlxsw_sp;
6378 struct mlxsw_sp_rif *rif;
6379 int err = 0;
6380
4724ba56
IS
6381 mlxsw_sp = mlxsw_sp_lower_get(dev);
6382 if (!mlxsw_sp)
6383 goto out;
6384
bf95233e 6385 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
5ea1237f 6386 if (!mlxsw_sp_rif_should_config(rif, dev, event))
4724ba56
IS
6387 goto out;
6388
f8fa9b4e 6389 err = __mlxsw_sp_inetaddr_event(dev, event, ivi->extack);
4724ba56
IS
6390out:
6391 return notifier_from_errno(err);
6392}
6393
5ea1237f
AS
6394struct mlxsw_sp_inet6addr_event_work {
6395 struct work_struct work;
6396 struct net_device *dev;
6397 unsigned long event;
6398};
6399
6400static void mlxsw_sp_inet6addr_event_work(struct work_struct *work)
6401{
6402 struct mlxsw_sp_inet6addr_event_work *inet6addr_work =
6403 container_of(work, struct mlxsw_sp_inet6addr_event_work, work);
6404 struct net_device *dev = inet6addr_work->dev;
6405 unsigned long event = inet6addr_work->event;
6406 struct mlxsw_sp *mlxsw_sp;
6407 struct mlxsw_sp_rif *rif;
6408
6409 rtnl_lock();
6410 mlxsw_sp = mlxsw_sp_lower_get(dev);
6411 if (!mlxsw_sp)
6412 goto out;
6413
6414 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
6415 if (!mlxsw_sp_rif_should_config(rif, dev, event))
6416 goto out;
6417
f8fa9b4e 6418 __mlxsw_sp_inetaddr_event(dev, event, NULL);
5ea1237f
AS
6419out:
6420 rtnl_unlock();
6421 dev_put(dev);
6422 kfree(inet6addr_work);
6423}
6424
6425/* Called with rcu_read_lock() */
6426int mlxsw_sp_inet6addr_event(struct notifier_block *unused,
6427 unsigned long event, void *ptr)
6428{
6429 struct inet6_ifaddr *if6 = (struct inet6_ifaddr *) ptr;
6430 struct mlxsw_sp_inet6addr_event_work *inet6addr_work;
6431 struct net_device *dev = if6->idev->dev;
6432
89d5dd2e
DA
6433 /* NETDEV_UP event is handled by mlxsw_sp_inet6addr_valid_event */
6434 if (event == NETDEV_UP)
6435 return NOTIFY_DONE;
6436
5ea1237f
AS
6437 if (!mlxsw_sp_port_dev_lower_find_rcu(dev))
6438 return NOTIFY_DONE;
6439
6440 inet6addr_work = kzalloc(sizeof(*inet6addr_work), GFP_ATOMIC);
6441 if (!inet6addr_work)
6442 return NOTIFY_BAD;
6443
6444 INIT_WORK(&inet6addr_work->work, mlxsw_sp_inet6addr_event_work);
6445 inet6addr_work->dev = dev;
6446 inet6addr_work->event = event;
6447 dev_hold(dev);
6448 mlxsw_core_schedule_work(&inet6addr_work->work);
6449
6450 return NOTIFY_DONE;
6451}
6452
89d5dd2e
DA
6453int mlxsw_sp_inet6addr_valid_event(struct notifier_block *unused,
6454 unsigned long event, void *ptr)
6455{
6456 struct in6_validator_info *i6vi = (struct in6_validator_info *) ptr;
6457 struct net_device *dev = i6vi->i6vi_dev->dev;
6458 struct mlxsw_sp *mlxsw_sp;
6459 struct mlxsw_sp_rif *rif;
6460 int err = 0;
6461
6462 mlxsw_sp = mlxsw_sp_lower_get(dev);
6463 if (!mlxsw_sp)
6464 goto out;
6465
6466 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
6467 if (!mlxsw_sp_rif_should_config(rif, dev, event))
6468 goto out;
6469
f8fa9b4e 6470 err = __mlxsw_sp_inetaddr_event(dev, event, i6vi->extack);
89d5dd2e
DA
6471out:
6472 return notifier_from_errno(err);
6473}
6474
bf95233e 6475static int mlxsw_sp_rif_edit(struct mlxsw_sp *mlxsw_sp, u16 rif_index,
4724ba56
IS
6476 const char *mac, int mtu)
6477{
6478 char ritr_pl[MLXSW_REG_RITR_LEN];
6479 int err;
6480
bf95233e 6481 mlxsw_reg_ritr_rif_pack(ritr_pl, rif_index);
4724ba56
IS
6482 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
6483 if (err)
6484 return err;
6485
6486 mlxsw_reg_ritr_mtu_set(ritr_pl, mtu);
6487 mlxsw_reg_ritr_if_mac_memcpy_to(ritr_pl, mac);
6488 mlxsw_reg_ritr_op_set(ritr_pl, MLXSW_REG_RITR_RIF_CREATE);
6489 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
6490}
6491
6492int mlxsw_sp_netdevice_router_port_event(struct net_device *dev)
6493{
6494 struct mlxsw_sp *mlxsw_sp;
bf95233e 6495 struct mlxsw_sp_rif *rif;
a1107487 6496 u16 fid_index;
4724ba56
IS
6497 int err;
6498
6499 mlxsw_sp = mlxsw_sp_lower_get(dev);
6500 if (!mlxsw_sp)
6501 return 0;
6502
bf95233e
AS
6503 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, dev);
6504 if (!rif)
4724ba56 6505 return 0;
a1107487 6506 fid_index = mlxsw_sp_fid_index(rif->fid);
4724ba56 6507
a1107487 6508 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->addr, fid_index, false);
4724ba56
IS
6509 if (err)
6510 return err;
6511
bf95233e
AS
6512 err = mlxsw_sp_rif_edit(mlxsw_sp, rif->rif_index, dev->dev_addr,
6513 dev->mtu);
4724ba56
IS
6514 if (err)
6515 goto err_rif_edit;
6516
a1107487 6517 err = mlxsw_sp_rif_fdb_op(mlxsw_sp, dev->dev_addr, fid_index, true);
4724ba56
IS
6518 if (err)
6519 goto err_rif_fdb_op;
6520
fd890fe9
YG
6521 if (rif->mtu != dev->mtu) {
6522 struct mlxsw_sp_vr *vr;
6523
6524 /* The RIF is relevant only to its mr_table instance, as unlike
6525 * unicast routing, in multicast routing a RIF cannot be shared
6526 * between several multicast routing tables.
6527 */
6528 vr = &mlxsw_sp->router->vrs[rif->vr_id];
6529 mlxsw_sp_mr_rif_mtu_update(vr->mr4_table, rif, dev->mtu);
6530 }
6531
bf95233e
AS
6532 ether_addr_copy(rif->addr, dev->dev_addr);
6533 rif->mtu = dev->mtu;
4724ba56 6534
bf95233e 6535 netdev_dbg(dev, "Updated RIF=%d\n", rif->rif_index);
4724ba56
IS
6536
6537 return 0;
6538
6539err_rif_fdb_op:
bf95233e 6540 mlxsw_sp_rif_edit(mlxsw_sp, rif->rif_index, rif->addr, rif->mtu);
4724ba56 6541err_rif_edit:
a1107487 6542 mlxsw_sp_rif_fdb_op(mlxsw_sp, rif->addr, fid_index, true);
4724ba56
IS
6543 return err;
6544}
6545
b1e45526 6546static int mlxsw_sp_port_vrf_join(struct mlxsw_sp *mlxsw_sp,
f8fa9b4e
DA
6547 struct net_device *l3_dev,
6548 struct netlink_ext_ack *extack)
7179eb5a 6549{
b1e45526 6550 struct mlxsw_sp_rif *rif;
7179eb5a 6551
b1e45526
IS
6552 /* If netdev is already associated with a RIF, then we need to
6553 * destroy it and create a new one with the new virtual router ID.
7179eb5a 6554 */
b1e45526
IS
6555 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
6556 if (rif)
f8fa9b4e 6557 __mlxsw_sp_inetaddr_event(l3_dev, NETDEV_DOWN, extack);
7179eb5a 6558
f8fa9b4e 6559 return __mlxsw_sp_inetaddr_event(l3_dev, NETDEV_UP, extack);
7179eb5a
IS
6560}
6561
b1e45526
IS
6562static void mlxsw_sp_port_vrf_leave(struct mlxsw_sp *mlxsw_sp,
6563 struct net_device *l3_dev)
7179eb5a 6564{
b1e45526 6565 struct mlxsw_sp_rif *rif;
7179eb5a 6566
b1e45526
IS
6567 rif = mlxsw_sp_rif_find_by_dev(mlxsw_sp, l3_dev);
6568 if (!rif)
7179eb5a 6569 return;
f8fa9b4e 6570 __mlxsw_sp_inetaddr_event(l3_dev, NETDEV_DOWN, NULL);
7179eb5a
IS
6571}
6572
b1e45526
IS
6573int mlxsw_sp_netdevice_vrf_event(struct net_device *l3_dev, unsigned long event,
6574 struct netdev_notifier_changeupper_info *info)
3d70e458 6575{
b1e45526
IS
6576 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_lower_get(l3_dev);
6577 int err = 0;
3d70e458 6578
b1e45526
IS
6579 if (!mlxsw_sp)
6580 return 0;
3d70e458 6581
b1e45526
IS
6582 switch (event) {
6583 case NETDEV_PRECHANGEUPPER:
6584 return 0;
6585 case NETDEV_CHANGEUPPER:
f8fa9b4e
DA
6586 if (info->linking) {
6587 struct netlink_ext_ack *extack;
6588
6589 extack = netdev_notifier_info_to_extack(&info->info);
6590 err = mlxsw_sp_port_vrf_join(mlxsw_sp, l3_dev, extack);
6591 } else {
b1e45526 6592 mlxsw_sp_port_vrf_leave(mlxsw_sp, l3_dev);
f8fa9b4e 6593 }
b1e45526
IS
6594 break;
6595 }
3d70e458 6596
b1e45526 6597 return err;
3d70e458
IS
6598}
6599
e4f3c1c1
IS
6600static struct mlxsw_sp_rif_subport *
6601mlxsw_sp_rif_subport_rif(const struct mlxsw_sp_rif *rif)
a1107487 6602{
e4f3c1c1
IS
6603 return container_of(rif, struct mlxsw_sp_rif_subport, common);
6604}
6605
6606static void mlxsw_sp_rif_subport_setup(struct mlxsw_sp_rif *rif,
6607 const struct mlxsw_sp_rif_params *params)
6608{
6609 struct mlxsw_sp_rif_subport *rif_subport;
6610
6611 rif_subport = mlxsw_sp_rif_subport_rif(rif);
6612 rif_subport->vid = params->vid;
6613 rif_subport->lag = params->lag;
6614 if (params->lag)
6615 rif_subport->lag_id = params->lag_id;
a1107487 6616 else
e4f3c1c1
IS
6617 rif_subport->system_port = params->system_port;
6618}
6619
6620static int mlxsw_sp_rif_subport_op(struct mlxsw_sp_rif *rif, bool enable)
6621{
6622 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6623 struct mlxsw_sp_rif_subport *rif_subport;
6624 char ritr_pl[MLXSW_REG_RITR_LEN];
6625
6626 rif_subport = mlxsw_sp_rif_subport_rif(rif);
6627 mlxsw_reg_ritr_pack(ritr_pl, enable, MLXSW_REG_RITR_SP_IF,
9571e828
PM
6628 rif->rif_index, rif->vr_id, rif->dev->mtu);
6629 mlxsw_reg_ritr_mac_pack(ritr_pl, rif->dev->dev_addr);
e4f3c1c1
IS
6630 mlxsw_reg_ritr_sp_if_pack(ritr_pl, rif_subport->lag,
6631 rif_subport->lag ? rif_subport->lag_id :
6632 rif_subport->system_port,
6633 rif_subport->vid);
6634
6635 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
6636}
6637
6638static int mlxsw_sp_rif_subport_configure(struct mlxsw_sp_rif *rif)
6639{
010cadf9
PM
6640 int err;
6641
6642 err = mlxsw_sp_rif_subport_op(rif, true);
6643 if (err)
6644 return err;
6645
6646 err = mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
6647 mlxsw_sp_fid_index(rif->fid), true);
6648 if (err)
6649 goto err_rif_fdb_op;
6650
6651 mlxsw_sp_fid_rif_set(rif->fid, rif);
6652 return 0;
6653
6654err_rif_fdb_op:
6655 mlxsw_sp_rif_subport_op(rif, false);
6656 return err;
a1107487
IS
6657}
6658
e4f3c1c1
IS
6659static void mlxsw_sp_rif_subport_deconfigure(struct mlxsw_sp_rif *rif)
6660{
010cadf9
PM
6661 struct mlxsw_sp_fid *fid = rif->fid;
6662
6663 mlxsw_sp_fid_rif_set(fid, NULL);
6664 mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
6665 mlxsw_sp_fid_index(fid), false);
e4f3c1c1
IS
6666 mlxsw_sp_rif_subport_op(rif, false);
6667}
6668
6669static struct mlxsw_sp_fid *
6670mlxsw_sp_rif_subport_fid_get(struct mlxsw_sp_rif *rif)
6671{
6672 return mlxsw_sp_fid_rfid_get(rif->mlxsw_sp, rif->rif_index);
6673}
6674
6675static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_subport_ops = {
6676 .type = MLXSW_SP_RIF_TYPE_SUBPORT,
6677 .rif_size = sizeof(struct mlxsw_sp_rif_subport),
6678 .setup = mlxsw_sp_rif_subport_setup,
6679 .configure = mlxsw_sp_rif_subport_configure,
6680 .deconfigure = mlxsw_sp_rif_subport_deconfigure,
6681 .fid_get = mlxsw_sp_rif_subport_fid_get,
6682};
6683
6684static int mlxsw_sp_rif_vlan_fid_op(struct mlxsw_sp_rif *rif,
6685 enum mlxsw_reg_ritr_if_type type,
6686 u16 vid_fid, bool enable)
6687{
6688 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6689 char ritr_pl[MLXSW_REG_RITR_LEN];
6690
6691 mlxsw_reg_ritr_pack(ritr_pl, enable, type, rif->rif_index, rif->vr_id,
9571e828
PM
6692 rif->dev->mtu);
6693 mlxsw_reg_ritr_mac_pack(ritr_pl, rif->dev->dev_addr);
e4f3c1c1
IS
6694 mlxsw_reg_ritr_fid_set(ritr_pl, type, vid_fid);
6695
6696 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
6697}
6698
b35750f1 6699u8 mlxsw_sp_router_port(const struct mlxsw_sp *mlxsw_sp)
e4f3c1c1
IS
6700{
6701 return mlxsw_core_max_ports(mlxsw_sp->core) + 1;
6702}
6703
6704static int mlxsw_sp_rif_vlan_configure(struct mlxsw_sp_rif *rif)
6705{
6706 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6707 u16 vid = mlxsw_sp_fid_8021q_vid(rif->fid);
6708 int err;
6709
6710 err = mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_VLAN_IF, vid, true);
6711 if (err)
6712 return err;
6713
0d284818
IS
6714 err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
6715 mlxsw_sp_router_port(mlxsw_sp), true);
6716 if (err)
6717 goto err_fid_mc_flood_set;
6718
e4f3c1c1
IS
6719 err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
6720 mlxsw_sp_router_port(mlxsw_sp), true);
6721 if (err)
6722 goto err_fid_bc_flood_set;
6723
010cadf9
PM
6724 err = mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
6725 mlxsw_sp_fid_index(rif->fid), true);
6726 if (err)
6727 goto err_rif_fdb_op;
6728
6729 mlxsw_sp_fid_rif_set(rif->fid, rif);
e4f3c1c1
IS
6730 return 0;
6731
010cadf9
PM
6732err_rif_fdb_op:
6733 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
6734 mlxsw_sp_router_port(mlxsw_sp), false);
e4f3c1c1 6735err_fid_bc_flood_set:
0d284818
IS
6736 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
6737 mlxsw_sp_router_port(mlxsw_sp), false);
6738err_fid_mc_flood_set:
e4f3c1c1
IS
6739 mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_VLAN_IF, vid, false);
6740 return err;
6741}
6742
6743static void mlxsw_sp_rif_vlan_deconfigure(struct mlxsw_sp_rif *rif)
6744{
e4f3c1c1 6745 u16 vid = mlxsw_sp_fid_8021q_vid(rif->fid);
010cadf9
PM
6746 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6747 struct mlxsw_sp_fid *fid = rif->fid;
e4f3c1c1 6748
010cadf9
PM
6749 mlxsw_sp_fid_rif_set(fid, NULL);
6750 mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
6751 mlxsw_sp_fid_index(fid), false);
e4f3c1c1
IS
6752 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
6753 mlxsw_sp_router_port(mlxsw_sp), false);
0d284818
IS
6754 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
6755 mlxsw_sp_router_port(mlxsw_sp), false);
e4f3c1c1
IS
6756 mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_VLAN_IF, vid, false);
6757}
6758
6759static struct mlxsw_sp_fid *
6760mlxsw_sp_rif_vlan_fid_get(struct mlxsw_sp_rif *rif)
6761{
6762 u16 vid = is_vlan_dev(rif->dev) ? vlan_dev_vlan_id(rif->dev) : 1;
6763
6764 return mlxsw_sp_fid_8021q_get(rif->mlxsw_sp, vid);
6765}
6766
6767static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_vlan_ops = {
6768 .type = MLXSW_SP_RIF_TYPE_VLAN,
6769 .rif_size = sizeof(struct mlxsw_sp_rif),
6770 .configure = mlxsw_sp_rif_vlan_configure,
6771 .deconfigure = mlxsw_sp_rif_vlan_deconfigure,
6772 .fid_get = mlxsw_sp_rif_vlan_fid_get,
6773};
6774
6775static int mlxsw_sp_rif_fid_configure(struct mlxsw_sp_rif *rif)
6776{
6777 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6778 u16 fid_index = mlxsw_sp_fid_index(rif->fid);
6779 int err;
6780
6781 err = mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_FID_IF, fid_index,
6782 true);
6783 if (err)
6784 return err;
6785
0d284818
IS
6786 err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
6787 mlxsw_sp_router_port(mlxsw_sp), true);
6788 if (err)
6789 goto err_fid_mc_flood_set;
6790
e4f3c1c1
IS
6791 err = mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
6792 mlxsw_sp_router_port(mlxsw_sp), true);
6793 if (err)
6794 goto err_fid_bc_flood_set;
6795
010cadf9
PM
6796 err = mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
6797 mlxsw_sp_fid_index(rif->fid), true);
6798 if (err)
6799 goto err_rif_fdb_op;
6800
6801 mlxsw_sp_fid_rif_set(rif->fid, rif);
e4f3c1c1
IS
6802 return 0;
6803
010cadf9
PM
6804err_rif_fdb_op:
6805 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
6806 mlxsw_sp_router_port(mlxsw_sp), false);
e4f3c1c1 6807err_fid_bc_flood_set:
0d284818
IS
6808 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
6809 mlxsw_sp_router_port(mlxsw_sp), false);
6810err_fid_mc_flood_set:
e4f3c1c1
IS
6811 mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_FID_IF, fid_index, false);
6812 return err;
6813}
6814
6815static void mlxsw_sp_rif_fid_deconfigure(struct mlxsw_sp_rif *rif)
6816{
e4f3c1c1 6817 u16 fid_index = mlxsw_sp_fid_index(rif->fid);
010cadf9
PM
6818 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6819 struct mlxsw_sp_fid *fid = rif->fid;
e4f3c1c1 6820
010cadf9
PM
6821 mlxsw_sp_fid_rif_set(fid, NULL);
6822 mlxsw_sp_rif_fdb_op(rif->mlxsw_sp, rif->dev->dev_addr,
6823 mlxsw_sp_fid_index(fid), false);
e4f3c1c1
IS
6824 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_BC,
6825 mlxsw_sp_router_port(mlxsw_sp), false);
0d284818
IS
6826 mlxsw_sp_fid_flood_set(rif->fid, MLXSW_SP_FLOOD_TYPE_MC,
6827 mlxsw_sp_router_port(mlxsw_sp), false);
e4f3c1c1
IS
6828 mlxsw_sp_rif_vlan_fid_op(rif, MLXSW_REG_RITR_FID_IF, fid_index, false);
6829}
6830
6831static struct mlxsw_sp_fid *
6832mlxsw_sp_rif_fid_fid_get(struct mlxsw_sp_rif *rif)
6833{
6834 return mlxsw_sp_fid_8021d_get(rif->mlxsw_sp, rif->dev->ifindex);
6835}
6836
6837static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_fid_ops = {
6838 .type = MLXSW_SP_RIF_TYPE_FID,
6839 .rif_size = sizeof(struct mlxsw_sp_rif),
6840 .configure = mlxsw_sp_rif_fid_configure,
6841 .deconfigure = mlxsw_sp_rif_fid_deconfigure,
6842 .fid_get = mlxsw_sp_rif_fid_fid_get,
6843};
6844
6ddb7426
PM
6845static struct mlxsw_sp_rif_ipip_lb *
6846mlxsw_sp_rif_ipip_lb_rif(struct mlxsw_sp_rif *rif)
6847{
6848 return container_of(rif, struct mlxsw_sp_rif_ipip_lb, common);
6849}
6850
6851static void
6852mlxsw_sp_rif_ipip_lb_setup(struct mlxsw_sp_rif *rif,
6853 const struct mlxsw_sp_rif_params *params)
6854{
6855 struct mlxsw_sp_rif_params_ipip_lb *params_lb;
6856 struct mlxsw_sp_rif_ipip_lb *rif_lb;
6857
6858 params_lb = container_of(params, struct mlxsw_sp_rif_params_ipip_lb,
6859 common);
6860 rif_lb = mlxsw_sp_rif_ipip_lb_rif(rif);
6861 rif_lb->lb_config = params_lb->lb_config;
6862}
6863
6864static int
6865mlxsw_sp_rif_ipip_lb_op(struct mlxsw_sp_rif_ipip_lb *lb_rif,
6866 struct mlxsw_sp_vr *ul_vr, bool enable)
6867{
6868 struct mlxsw_sp_rif_ipip_lb_config lb_cf = lb_rif->lb_config;
6869 struct mlxsw_sp_rif *rif = &lb_rif->common;
6870 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6871 char ritr_pl[MLXSW_REG_RITR_LEN];
6872 u32 saddr4;
6873
6874 switch (lb_cf.ul_protocol) {
6875 case MLXSW_SP_L3_PROTO_IPV4:
6876 saddr4 = be32_to_cpu(lb_cf.saddr.addr4);
6877 mlxsw_reg_ritr_pack(ritr_pl, enable, MLXSW_REG_RITR_LOOPBACK_IF,
6878 rif->rif_index, rif->vr_id, rif->dev->mtu);
6879 mlxsw_reg_ritr_loopback_ipip4_pack(ritr_pl, lb_cf.lb_ipipt,
6880 MLXSW_REG_RITR_LOOPBACK_IPIP_OPTIONS_GRE_KEY_PRESET,
6881 ul_vr->id, saddr4, lb_cf.okey);
6882 break;
6883
6884 case MLXSW_SP_L3_PROTO_IPV6:
6885 return -EAFNOSUPPORT;
6886 }
6887
6888 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(ritr), ritr_pl);
6889}
6890
6891static int
6892mlxsw_sp_rif_ipip_lb_configure(struct mlxsw_sp_rif *rif)
6893{
6894 struct mlxsw_sp_rif_ipip_lb *lb_rif = mlxsw_sp_rif_ipip_lb_rif(rif);
6895 u32 ul_tb_id = mlxsw_sp_ipip_dev_ul_tb_id(rif->dev);
6896 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6897 struct mlxsw_sp_vr *ul_vr;
6898 int err;
6899
f8fa9b4e 6900 ul_vr = mlxsw_sp_vr_get(mlxsw_sp, ul_tb_id, NULL);
6ddb7426
PM
6901 if (IS_ERR(ul_vr))
6902 return PTR_ERR(ul_vr);
6903
6904 err = mlxsw_sp_rif_ipip_lb_op(lb_rif, ul_vr, true);
6905 if (err)
6906 goto err_loopback_op;
6907
6908 lb_rif->ul_vr_id = ul_vr->id;
6909 ++ul_vr->rif_count;
6910 return 0;
6911
6912err_loopback_op:
2b52ce02 6913 mlxsw_sp_vr_put(mlxsw_sp, ul_vr);
6ddb7426
PM
6914 return err;
6915}
6916
6917static void mlxsw_sp_rif_ipip_lb_deconfigure(struct mlxsw_sp_rif *rif)
6918{
6919 struct mlxsw_sp_rif_ipip_lb *lb_rif = mlxsw_sp_rif_ipip_lb_rif(rif);
6920 struct mlxsw_sp *mlxsw_sp = rif->mlxsw_sp;
6921 struct mlxsw_sp_vr *ul_vr;
6922
6923 ul_vr = &mlxsw_sp->router->vrs[lb_rif->ul_vr_id];
6924 mlxsw_sp_rif_ipip_lb_op(lb_rif, ul_vr, false);
6925
6926 --ul_vr->rif_count;
2b52ce02 6927 mlxsw_sp_vr_put(mlxsw_sp, ul_vr);
6ddb7426
PM
6928}
6929
6930static const struct mlxsw_sp_rif_ops mlxsw_sp_rif_ipip_lb_ops = {
6931 .type = MLXSW_SP_RIF_TYPE_IPIP_LB,
6932 .rif_size = sizeof(struct mlxsw_sp_rif_ipip_lb),
6933 .setup = mlxsw_sp_rif_ipip_lb_setup,
6934 .configure = mlxsw_sp_rif_ipip_lb_configure,
6935 .deconfigure = mlxsw_sp_rif_ipip_lb_deconfigure,
6936};
6937
e4f3c1c1
IS
6938static const struct mlxsw_sp_rif_ops *mlxsw_sp_rif_ops_arr[] = {
6939 [MLXSW_SP_RIF_TYPE_SUBPORT] = &mlxsw_sp_rif_subport_ops,
6940 [MLXSW_SP_RIF_TYPE_VLAN] = &mlxsw_sp_rif_vlan_ops,
6941 [MLXSW_SP_RIF_TYPE_FID] = &mlxsw_sp_rif_fid_ops,
6ddb7426 6942 [MLXSW_SP_RIF_TYPE_IPIP_LB] = &mlxsw_sp_rif_ipip_lb_ops,
e4f3c1c1
IS
6943};
6944
348b8fc3
IS
6945static int mlxsw_sp_rifs_init(struct mlxsw_sp *mlxsw_sp)
6946{
6947 u64 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
6948
6949 mlxsw_sp->router->rifs = kcalloc(max_rifs,
6950 sizeof(struct mlxsw_sp_rif *),
6951 GFP_KERNEL);
6952 if (!mlxsw_sp->router->rifs)
6953 return -ENOMEM;
e4f3c1c1
IS
6954
6955 mlxsw_sp->router->rif_ops_arr = mlxsw_sp_rif_ops_arr;
6956
348b8fc3
IS
6957 return 0;
6958}
6959
6960static void mlxsw_sp_rifs_fini(struct mlxsw_sp *mlxsw_sp)
6961{
6962 int i;
6963
6964 for (i = 0; i < MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS); i++)
6965 WARN_ON_ONCE(mlxsw_sp->router->rifs[i]);
6966
6967 kfree(mlxsw_sp->router->rifs);
6968}
6969
dcbda282
PM
6970static int
6971mlxsw_sp_ipip_config_tigcr(struct mlxsw_sp *mlxsw_sp)
6972{
6973 char tigcr_pl[MLXSW_REG_TIGCR_LEN];
6974
6975 mlxsw_reg_tigcr_pack(tigcr_pl, true, 0);
6976 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(tigcr), tigcr_pl);
6977}
6978
38ebc0f4
PM
6979static int mlxsw_sp_ipips_init(struct mlxsw_sp *mlxsw_sp)
6980{
6981 mlxsw_sp->router->ipip_ops_arr = mlxsw_sp_ipip_ops_arr;
1012b9ac 6982 INIT_LIST_HEAD(&mlxsw_sp->router->ipip_list);
dcbda282 6983 return mlxsw_sp_ipip_config_tigcr(mlxsw_sp);
38ebc0f4
PM
6984}
6985
6986static void mlxsw_sp_ipips_fini(struct mlxsw_sp *mlxsw_sp)
6987{
1012b9ac 6988 WARN_ON(!list_empty(&mlxsw_sp->router->ipip_list));
38ebc0f4
PM
6989}
6990
c3852ef7
IS
6991static void mlxsw_sp_router_fib_dump_flush(struct notifier_block *nb)
6992{
7e39d115 6993 struct mlxsw_sp_router *router;
c3852ef7
IS
6994
6995 /* Flush pending FIB notifications and then flush the device's
6996 * table before requesting another dump. The FIB notification
6997 * block is unregistered, so no need to take RTNL.
6998 */
6999 mlxsw_core_flush_owq();
7e39d115
IS
7000 router = container_of(nb, struct mlxsw_sp_router, fib_nb);
7001 mlxsw_sp_router_fib_flush(router->mlxsw_sp);
c3852ef7
IS
7002}
7003
af658b6a
IS
7004#ifdef CONFIG_IP_ROUTE_MULTIPATH
7005static void mlxsw_sp_mp_hash_header_set(char *recr2_pl, int header)
7006{
7007 mlxsw_reg_recr2_outer_header_enables_set(recr2_pl, header, true);
7008}
7009
7010static void mlxsw_sp_mp_hash_field_set(char *recr2_pl, int field)
7011{
7012 mlxsw_reg_recr2_outer_header_fields_enable_set(recr2_pl, field, true);
7013}
7014
7015static void mlxsw_sp_mp4_hash_init(char *recr2_pl)
7016{
7017 bool only_l3 = !init_net.ipv4.sysctl_fib_multipath_hash_policy;
7018
7019 mlxsw_sp_mp_hash_header_set(recr2_pl,
7020 MLXSW_REG_RECR2_IPV4_EN_NOT_TCP_NOT_UDP);
7021 mlxsw_sp_mp_hash_header_set(recr2_pl, MLXSW_REG_RECR2_IPV4_EN_TCP_UDP);
7022 mlxsw_reg_recr2_ipv4_sip_enable(recr2_pl);
7023 mlxsw_reg_recr2_ipv4_dip_enable(recr2_pl);
7024 if (only_l3)
7025 return;
7026 mlxsw_sp_mp_hash_header_set(recr2_pl, MLXSW_REG_RECR2_TCP_UDP_EN_IPV4);
7027 mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_IPV4_PROTOCOL);
7028 mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_TCP_UDP_SPORT);
7029 mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_TCP_UDP_DPORT);
7030}
7031
7032static void mlxsw_sp_mp6_hash_init(char *recr2_pl)
7033{
5e18b9c5
DA
7034 bool only_l3 = !init_net.ipv6.sysctl.multipath_hash_policy;
7035
af658b6a
IS
7036 mlxsw_sp_mp_hash_header_set(recr2_pl,
7037 MLXSW_REG_RECR2_IPV6_EN_NOT_TCP_NOT_UDP);
7038 mlxsw_sp_mp_hash_header_set(recr2_pl, MLXSW_REG_RECR2_IPV6_EN_TCP_UDP);
7039 mlxsw_reg_recr2_ipv6_sip_enable(recr2_pl);
7040 mlxsw_reg_recr2_ipv6_dip_enable(recr2_pl);
af658b6a 7041 mlxsw_sp_mp_hash_field_set(recr2_pl, MLXSW_REG_RECR2_IPV6_NEXT_HEADER);
5e18b9c5
DA
7042 if (only_l3) {
7043 mlxsw_sp_mp_hash_field_set(recr2_pl,
7044 MLXSW_REG_RECR2_IPV6_FLOW_LABEL);
7045 } else {
7046 mlxsw_sp_mp_hash_header_set(recr2_pl,
7047 MLXSW_REG_RECR2_TCP_UDP_EN_IPV6);
7048 mlxsw_sp_mp_hash_field_set(recr2_pl,
7049 MLXSW_REG_RECR2_TCP_UDP_SPORT);
7050 mlxsw_sp_mp_hash_field_set(recr2_pl,
7051 MLXSW_REG_RECR2_TCP_UDP_DPORT);
7052 }
af658b6a
IS
7053}
7054
7055static int mlxsw_sp_mp_hash_init(struct mlxsw_sp *mlxsw_sp)
7056{
7057 char recr2_pl[MLXSW_REG_RECR2_LEN];
7058 u32 seed;
7059
7060 get_random_bytes(&seed, sizeof(seed));
7061 mlxsw_reg_recr2_pack(recr2_pl, seed);
7062 mlxsw_sp_mp4_hash_init(recr2_pl);
7063 mlxsw_sp_mp6_hash_init(recr2_pl);
7064
7065 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(recr2), recr2_pl);
7066}
7067#else
7068static int mlxsw_sp_mp_hash_init(struct mlxsw_sp *mlxsw_sp)
7069{
7070 return 0;
7071}
7072#endif
7073
48276a29
YM
7074static int mlxsw_sp_dscp_init(struct mlxsw_sp *mlxsw_sp)
7075{
7076 char rdpm_pl[MLXSW_REG_RDPM_LEN];
7077 unsigned int i;
7078
7079 MLXSW_REG_ZERO(rdpm, rdpm_pl);
7080
7081 /* HW is determining switch priority based on DSCP-bits, but the
7082 * kernel is still doing that based on the ToS. Since there's a
7083 * mismatch in bits we need to make sure to translate the right
7084 * value ToS would observe, skipping the 2 least-significant ECN bits.
7085 */
7086 for (i = 0; i < MLXSW_REG_RDPM_DSCP_ENTRY_REC_MAX_COUNT; i++)
7087 mlxsw_reg_rdpm_pack(rdpm_pl, i, rt_tos2priority(i << 2));
7088
7089 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rdpm), rdpm_pl);
7090}
7091
4724ba56
IS
7092static int __mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
7093{
7094 char rgcr_pl[MLXSW_REG_RGCR_LEN];
7095 u64 max_rifs;
7096 int err;
7097
7098 if (!MLXSW_CORE_RES_VALID(mlxsw_sp->core, MAX_RIFS))
7099 return -EIO;
4724ba56 7100 max_rifs = MLXSW_CORE_RES_GET(mlxsw_sp->core, MAX_RIFS);
4724ba56 7101
e29237e7 7102 mlxsw_reg_rgcr_pack(rgcr_pl, true, true);
4724ba56 7103 mlxsw_reg_rgcr_max_router_interfaces_set(rgcr_pl, max_rifs);
48276a29 7104 mlxsw_reg_rgcr_usp_set(rgcr_pl, true);
4724ba56
IS
7105 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
7106 if (err)
348b8fc3 7107 return err;
4724ba56 7108 return 0;
4724ba56
IS
7109}
7110
7111static void __mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
7112{
7113 char rgcr_pl[MLXSW_REG_RGCR_LEN];
4724ba56 7114
e29237e7 7115 mlxsw_reg_rgcr_pack(rgcr_pl, false, false);
4724ba56 7116 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(rgcr), rgcr_pl);
4724ba56
IS
7117}
7118
b45f64d1
JP
7119int mlxsw_sp_router_init(struct mlxsw_sp *mlxsw_sp)
7120{
9011b677 7121 struct mlxsw_sp_router *router;
b45f64d1
JP
7122 int err;
7123
9011b677
IS
7124 router = kzalloc(sizeof(*mlxsw_sp->router), GFP_KERNEL);
7125 if (!router)
7126 return -ENOMEM;
7127 mlxsw_sp->router = router;
7128 router->mlxsw_sp = mlxsw_sp;
7129
7130 INIT_LIST_HEAD(&mlxsw_sp->router->nexthop_neighs_list);
b45f64d1
JP
7131 err = __mlxsw_sp_router_init(mlxsw_sp);
7132 if (err)
9011b677 7133 goto err_router_init;
b45f64d1 7134
348b8fc3
IS
7135 err = mlxsw_sp_rifs_init(mlxsw_sp);
7136 if (err)
7137 goto err_rifs_init;
7138
38ebc0f4
PM
7139 err = mlxsw_sp_ipips_init(mlxsw_sp);
7140 if (err)
7141 goto err_ipips_init;
7142
9011b677 7143 err = rhashtable_init(&mlxsw_sp->router->nexthop_ht,
c53b8e1b
IS
7144 &mlxsw_sp_nexthop_ht_params);
7145 if (err)
7146 goto err_nexthop_ht_init;
7147
9011b677 7148 err = rhashtable_init(&mlxsw_sp->router->nexthop_group_ht,
e9ad5e7d
IS
7149 &mlxsw_sp_nexthop_group_ht_params);
7150 if (err)
7151 goto err_nexthop_group_ht_init;
7152
dbe4598c 7153 INIT_LIST_HEAD(&mlxsw_sp->router->nexthop_list);
8494ab06
IS
7154 err = mlxsw_sp_lpm_init(mlxsw_sp);
7155 if (err)
7156 goto err_lpm_init;
7157
d42b0965
YG
7158 err = mlxsw_sp_mr_init(mlxsw_sp, &mlxsw_sp_mr_tcam_ops);
7159 if (err)
7160 goto err_mr_init;
7161
b45f64d1
JP
7162 err = mlxsw_sp_vrs_init(mlxsw_sp);
7163 if (err)
7164 goto err_vrs_init;
7165
8c9583a8 7166 err = mlxsw_sp_neigh_init(mlxsw_sp);
b45f64d1
JP
7167 if (err)
7168 goto err_neigh_init;
7169
48fac885
IS
7170 mlxsw_sp->router->netevent_nb.notifier_call =
7171 mlxsw_sp_router_netevent_event;
7172 err = register_netevent_notifier(&mlxsw_sp->router->netevent_nb);
7173 if (err)
7174 goto err_register_netevent_notifier;
7175
af658b6a
IS
7176 err = mlxsw_sp_mp_hash_init(mlxsw_sp);
7177 if (err)
7178 goto err_mp_hash_init;
7179
48276a29
YM
7180 err = mlxsw_sp_dscp_init(mlxsw_sp);
7181 if (err)
7182 goto err_dscp_init;
7183
7e39d115
IS
7184 mlxsw_sp->router->fib_nb.notifier_call = mlxsw_sp_router_fib_event;
7185 err = register_fib_notifier(&mlxsw_sp->router->fib_nb,
c3852ef7
IS
7186 mlxsw_sp_router_fib_dump_flush);
7187 if (err)
7188 goto err_register_fib_notifier;
7189
b45f64d1
JP
7190 return 0;
7191
c3852ef7 7192err_register_fib_notifier:
48276a29 7193err_dscp_init:
af658b6a 7194err_mp_hash_init:
48fac885
IS
7195 unregister_netevent_notifier(&mlxsw_sp->router->netevent_nb);
7196err_register_netevent_notifier:
c3852ef7 7197 mlxsw_sp_neigh_fini(mlxsw_sp);
b45f64d1
JP
7198err_neigh_init:
7199 mlxsw_sp_vrs_fini(mlxsw_sp);
7200err_vrs_init:
d42b0965
YG
7201 mlxsw_sp_mr_fini(mlxsw_sp);
7202err_mr_init:
8494ab06
IS
7203 mlxsw_sp_lpm_fini(mlxsw_sp);
7204err_lpm_init:
9011b677 7205 rhashtable_destroy(&mlxsw_sp->router->nexthop_group_ht);
e9ad5e7d 7206err_nexthop_group_ht_init:
9011b677 7207 rhashtable_destroy(&mlxsw_sp->router->nexthop_ht);
c53b8e1b 7208err_nexthop_ht_init:
38ebc0f4
PM
7209 mlxsw_sp_ipips_fini(mlxsw_sp);
7210err_ipips_init:
348b8fc3
IS
7211 mlxsw_sp_rifs_fini(mlxsw_sp);
7212err_rifs_init:
b45f64d1 7213 __mlxsw_sp_router_fini(mlxsw_sp);
9011b677
IS
7214err_router_init:
7215 kfree(mlxsw_sp->router);
b45f64d1
JP
7216 return err;
7217}
7218
7219void mlxsw_sp_router_fini(struct mlxsw_sp *mlxsw_sp)
7220{
7e39d115 7221 unregister_fib_notifier(&mlxsw_sp->router->fib_nb);
48fac885 7222 unregister_netevent_notifier(&mlxsw_sp->router->netevent_nb);
b45f64d1
JP
7223 mlxsw_sp_neigh_fini(mlxsw_sp);
7224 mlxsw_sp_vrs_fini(mlxsw_sp);
d42b0965 7225 mlxsw_sp_mr_fini(mlxsw_sp);
8494ab06 7226 mlxsw_sp_lpm_fini(mlxsw_sp);
9011b677
IS
7227 rhashtable_destroy(&mlxsw_sp->router->nexthop_group_ht);
7228 rhashtable_destroy(&mlxsw_sp->router->nexthop_ht);
38ebc0f4 7229 mlxsw_sp_ipips_fini(mlxsw_sp);
348b8fc3 7230 mlxsw_sp_rifs_fini(mlxsw_sp);
b45f64d1 7231 __mlxsw_sp_router_fini(mlxsw_sp);
9011b677 7232 kfree(mlxsw_sp->router);
b45f64d1 7233}