blob: 941acd7869377880b7baa7307ba9a2be833bba77 [file] [log] [blame]
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001/*
2 * drivers/net/ethernet/mellanox/mlxsw/spectrum_switchdev.c
3 * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
4 * Copyright (c) 2015 Jiri Pirko <jiri@mellanox.com>
5 * Copyright (c) 2015 Ido Schimmel <idosch@mellanox.com>
6 * Copyright (c) 2015 Elad Raz <eladr@mellanox.com>
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include <linux/kernel.h>
38#include <linux/types.h>
39#include <linux/netdevice.h>
40#include <linux/etherdevice.h>
41#include <linux/slab.h>
42#include <linux/device.h>
43#include <linux/skbuff.h>
44#include <linux/if_vlan.h>
45#include <linux/if_bridge.h>
46#include <linux/workqueue.h>
47#include <linux/jiffies.h>
Ido Schimmel4f2c6ae2016-01-27 15:16:43 +010048#include <linux/rtnetlink.h>
Jiri Pirko56ade8f2015-10-16 14:01:37 +020049#include <net/switchdev.h>
50
51#include "spectrum.h"
52#include "core.h"
53#include "reg.h"
54
Elad Raze4b6f692016-01-10 21:06:27 +010055static u16 mlxsw_sp_port_vid_to_fid_get(struct mlxsw_sp_port *mlxsw_sp_port,
56 u16 vid)
57{
Ido Schimmel56918b62016-06-20 23:04:18 +020058 struct mlxsw_sp_fid *f = mlxsw_sp_vport_fid_get(mlxsw_sp_port);
Elad Raze4b6f692016-01-10 21:06:27 +010059 u16 fid = vid;
60
Ido Schimmel56918b62016-06-20 23:04:18 +020061 fid = f ? f->fid : fid;
Elad Raze4b6f692016-01-10 21:06:27 +010062
63 if (!fid)
64 fid = mlxsw_sp_port->pvid;
65
66 return fid;
67}
68
Ido Schimmel54a73202015-12-15 16:03:41 +010069static struct mlxsw_sp_port *
70mlxsw_sp_port_orig_get(struct net_device *dev,
71 struct mlxsw_sp_port *mlxsw_sp_port)
72{
73 struct mlxsw_sp_port *mlxsw_sp_vport;
74 u16 vid;
75
76 if (!is_vlan_dev(dev))
77 return mlxsw_sp_port;
78
79 vid = vlan_dev_vlan_id(dev);
80 mlxsw_sp_vport = mlxsw_sp_port_vport_find(mlxsw_sp_port, vid);
81 WARN_ON(!mlxsw_sp_vport);
82
83 return mlxsw_sp_vport;
84}
85
Jiri Pirko56ade8f2015-10-16 14:01:37 +020086static int mlxsw_sp_port_attr_get(struct net_device *dev,
87 struct switchdev_attr *attr)
88{
89 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
90 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
91
Ido Schimmel54a73202015-12-15 16:03:41 +010092 mlxsw_sp_port = mlxsw_sp_port_orig_get(attr->orig_dev, mlxsw_sp_port);
93 if (!mlxsw_sp_port)
94 return -EINVAL;
95
Jiri Pirko56ade8f2015-10-16 14:01:37 +020096 switch (attr->id) {
97 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
98 attr->u.ppid.id_len = sizeof(mlxsw_sp->base_mac);
99 memcpy(&attr->u.ppid.id, &mlxsw_sp->base_mac,
100 attr->u.ppid.id_len);
101 break;
102 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
103 attr->u.brport_flags =
104 (mlxsw_sp_port->learning ? BR_LEARNING : 0) |
Ido Schimmel02930382015-10-28 10:16:58 +0100105 (mlxsw_sp_port->learning_sync ? BR_LEARNING_SYNC : 0) |
106 (mlxsw_sp_port->uc_flood ? BR_FLOOD : 0);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200107 break;
108 default:
109 return -EOPNOTSUPP;
110 }
111
112 return 0;
113}
114
115static int mlxsw_sp_port_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
116 u8 state)
117{
118 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
119 enum mlxsw_reg_spms_state spms_state;
120 char *spms_pl;
121 u16 vid;
122 int err;
123
124 switch (state) {
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200125 case BR_STATE_FORWARDING:
126 spms_state = MLXSW_REG_SPMS_STATE_FORWARDING;
127 break;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200128 case BR_STATE_LEARNING:
129 spms_state = MLXSW_REG_SPMS_STATE_LEARNING;
130 break;
Ido Schimmel45491132016-01-27 15:20:20 +0100131 case BR_STATE_LISTENING: /* fall-through */
Ido Schimmel9cb026e2016-01-27 15:20:19 +0100132 case BR_STATE_DISABLED: /* fall-through */
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200133 case BR_STATE_BLOCKING:
134 spms_state = MLXSW_REG_SPMS_STATE_DISCARDING;
135 break;
136 default:
137 BUG();
138 }
139
140 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
141 if (!spms_pl)
142 return -ENOMEM;
143 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
Ido Schimmel54a73202015-12-15 16:03:41 +0100144
145 if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
146 vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200147 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
Ido Schimmel54a73202015-12-15 16:03:41 +0100148 } else {
149 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID)
150 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
151 }
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200152
153 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
154 kfree(spms_pl);
155 return err;
156}
157
158static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
159 struct switchdev_trans *trans,
160 u8 state)
161{
162 if (switchdev_trans_ph_prepare(trans))
163 return 0;
164
165 mlxsw_sp_port->stp_state = state;
166 return mlxsw_sp_port_stp_state_set(mlxsw_sp_port, state);
167}
168
Ido Schimmel02930382015-10-28 10:16:58 +0100169static int __mlxsw_sp_port_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
Ido Schimmelc06a94e2015-12-15 16:03:38 +0100170 u16 idx_begin, u16 idx_end, bool set,
Ido Schimmel02930382015-10-28 10:16:58 +0100171 bool only_uc)
172{
173 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
Ido Schimmel7f71eb42015-12-15 16:03:37 +0100174 u16 local_port = mlxsw_sp_port->local_port;
175 enum mlxsw_flood_table_type table_type;
Ido Schimmelc06a94e2015-12-15 16:03:38 +0100176 u16 range = idx_end - idx_begin + 1;
Ido Schimmel02930382015-10-28 10:16:58 +0100177 char *sftr_pl;
178 int err;
179
Ido Schimmel99724c12016-07-04 08:23:14 +0200180 if (mlxsw_sp_port_is_vport(mlxsw_sp_port))
Ido Schimmel7f71eb42015-12-15 16:03:37 +0100181 table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID;
Ido Schimmel99724c12016-07-04 08:23:14 +0200182 else
Ido Schimmel7f71eb42015-12-15 16:03:37 +0100183 table_type = MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST;
Ido Schimmel7f71eb42015-12-15 16:03:37 +0100184
Ido Schimmel02930382015-10-28 10:16:58 +0100185 sftr_pl = kmalloc(MLXSW_REG_SFTR_LEN, GFP_KERNEL);
186 if (!sftr_pl)
187 return -ENOMEM;
188
Ido Schimmelc06a94e2015-12-15 16:03:38 +0100189 mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_UC, idx_begin,
Ido Schimmel7f71eb42015-12-15 16:03:37 +0100190 table_type, range, local_port, set);
Ido Schimmel02930382015-10-28 10:16:58 +0100191 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
192 if (err)
193 goto buffer_out;
194
195 /* Flooding control allows one to decide whether a given port will
196 * flood unicast traffic for which there is no FDB entry.
197 */
198 if (only_uc)
199 goto buffer_out;
200
Ido Schimmelc06a94e2015-12-15 16:03:38 +0100201 mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_BM, idx_begin,
Ido Schimmel7f71eb42015-12-15 16:03:37 +0100202 table_type, range, local_port, set);
Ido Schimmel02930382015-10-28 10:16:58 +0100203 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
Ido Schimmel28892862016-05-06 22:18:40 +0200204 if (err)
205 goto err_flood_bm_set;
206 else
207 goto buffer_out;
Ido Schimmel02930382015-10-28 10:16:58 +0100208
Ido Schimmel28892862016-05-06 22:18:40 +0200209err_flood_bm_set:
210 mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_UC, idx_begin,
211 table_type, range, local_port, !set);
212 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
Ido Schimmel02930382015-10-28 10:16:58 +0100213buffer_out:
214 kfree(sftr_pl);
215 return err;
216}
217
218static int mlxsw_sp_port_uc_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
219 bool set)
220{
221 struct net_device *dev = mlxsw_sp_port->dev;
222 u16 vid, last_visited_vid;
223 int err;
224
Ido Schimmel54a73202015-12-15 16:03:41 +0100225 if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
Ido Schimmel41b996c2016-06-20 23:04:17 +0200226 u16 fid = mlxsw_sp_vport_fid_get(mlxsw_sp_port)->fid;
227 u16 vfid = mlxsw_sp_fid_to_vfid(fid);
Ido Schimmel54a73202015-12-15 16:03:41 +0100228
229 return __mlxsw_sp_port_flood_set(mlxsw_sp_port, vfid, vfid,
230 set, true);
231 }
232
Ido Schimmel02930382015-10-28 10:16:58 +0100233 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
234 err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid, vid, set,
235 true);
236 if (err) {
237 last_visited_vid = vid;
238 goto err_port_flood_set;
239 }
240 }
241
242 return 0;
243
244err_port_flood_set:
245 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, last_visited_vid)
246 __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid, vid, !set, true);
247 netdev_err(dev, "Failed to configure unicast flooding\n");
248 return err;
249}
250
Ido Schimmele6060022016-06-20 23:04:11 +0200251int mlxsw_sp_vport_flood_set(struct mlxsw_sp_port *mlxsw_sp_vport, u16 fid,
Ido Schimmel47a0a9e2016-06-20 23:04:08 +0200252 bool set)
Ido Schimmel7f71eb42015-12-15 16:03:37 +0100253{
Ido Schimmele6060022016-06-20 23:04:11 +0200254 u16 vfid;
255
Ido Schimmel7f71eb42015-12-15 16:03:37 +0100256 /* In case of vFIDs, index into the flooding table is relative to
257 * the start of the vFIDs range.
258 */
Ido Schimmele6060022016-06-20 23:04:11 +0200259 vfid = mlxsw_sp_fid_to_vfid(fid);
Ido Schimmel19ae6122015-12-15 16:03:39 +0100260 return __mlxsw_sp_port_flood_set(mlxsw_sp_vport, vfid, vfid, set,
Ido Schimmel47a0a9e2016-06-20 23:04:08 +0200261 false);
Ido Schimmel7f71eb42015-12-15 16:03:37 +0100262}
263
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200264static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
265 struct switchdev_trans *trans,
266 unsigned long brport_flags)
267{
Ido Schimmel02930382015-10-28 10:16:58 +0100268 unsigned long uc_flood = mlxsw_sp_port->uc_flood ? BR_FLOOD : 0;
269 bool set;
270 int err;
271
Ido Schimmel6c72a3d2016-01-04 10:42:26 +0100272 if (!mlxsw_sp_port->bridged)
273 return -EINVAL;
274
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200275 if (switchdev_trans_ph_prepare(trans))
276 return 0;
277
Ido Schimmel02930382015-10-28 10:16:58 +0100278 if ((uc_flood ^ brport_flags) & BR_FLOOD) {
279 set = mlxsw_sp_port->uc_flood ? false : true;
280 err = mlxsw_sp_port_uc_flood_set(mlxsw_sp_port, set);
281 if (err)
282 return err;
283 }
284
285 mlxsw_sp_port->uc_flood = brport_flags & BR_FLOOD ? 1 : 0;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200286 mlxsw_sp_port->learning = brport_flags & BR_LEARNING ? 1 : 0;
287 mlxsw_sp_port->learning_sync = brport_flags & BR_LEARNING_SYNC ? 1 : 0;
Ido Schimmel02930382015-10-28 10:16:58 +0100288
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200289 return 0;
290}
291
292static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
293{
294 char sfdat_pl[MLXSW_REG_SFDAT_LEN];
295 int err;
296
297 mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
298 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
299 if (err)
300 return err;
301 mlxsw_sp->ageing_time = ageing_time;
302 return 0;
303}
304
305static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
306 struct switchdev_trans *trans,
Jiri Pirko135f9ec2015-10-28 10:17:02 +0100307 unsigned long ageing_clock_t)
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200308{
309 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
Jiri Pirko135f9ec2015-10-28 10:17:02 +0100310 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200311 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
312
Ido Schimmel869f63a2016-03-08 12:59:33 -0800313 if (switchdev_trans_ph_prepare(trans)) {
314 if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
315 ageing_time > MLXSW_SP_MAX_AGEING_TIME)
316 return -ERANGE;
317 else
318 return 0;
319 }
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200320
321 return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
322}
323
Elad Raz26a4ea02016-01-06 13:01:10 +0100324static int mlxsw_sp_port_attr_br_vlan_set(struct mlxsw_sp_port *mlxsw_sp_port,
325 struct switchdev_trans *trans,
326 struct net_device *orig_dev,
327 bool vlan_enabled)
328{
329 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
330
331 /* SWITCHDEV_TRANS_PREPARE phase */
332 if ((!vlan_enabled) && (mlxsw_sp->master_bridge.dev == orig_dev)) {
333 netdev_err(mlxsw_sp_port->dev, "Bridge must be vlan-aware\n");
334 return -EINVAL;
335 }
336
337 return 0;
338}
339
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200340static int mlxsw_sp_port_attr_set(struct net_device *dev,
341 const struct switchdev_attr *attr,
342 struct switchdev_trans *trans)
343{
344 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
345 int err = 0;
346
Ido Schimmel54a73202015-12-15 16:03:41 +0100347 mlxsw_sp_port = mlxsw_sp_port_orig_get(attr->orig_dev, mlxsw_sp_port);
348 if (!mlxsw_sp_port)
349 return -EINVAL;
350
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200351 switch (attr->id) {
352 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
353 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans,
354 attr->u.stp_state);
355 break;
356 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
357 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans,
358 attr->u.brport_flags);
359 break;
360 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
361 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans,
362 attr->u.ageing_time);
363 break;
Elad Raz26a4ea02016-01-06 13:01:10 +0100364 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
365 err = mlxsw_sp_port_attr_br_vlan_set(mlxsw_sp_port, trans,
366 attr->orig_dev,
367 attr->u.vlan_filtering);
368 break;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200369 default:
370 err = -EOPNOTSUPP;
371 break;
372 }
373
374 return err;
375}
376
Ido Schimmel14d39462016-06-20 23:04:15 +0200377static struct mlxsw_sp_fid *mlxsw_sp_fid_find(struct mlxsw_sp *mlxsw_sp,
378 u16 fid)
379{
380 struct mlxsw_sp_fid *f;
381
382 list_for_each_entry(f, &mlxsw_sp->fids, list)
383 if (f->fid == fid)
384 return f;
385
386 return NULL;
387}
388
389static int mlxsw_sp_fid_op(struct mlxsw_sp *mlxsw_sp, u16 fid, bool create)
390{
391 char sfmr_pl[MLXSW_REG_SFMR_LEN];
392
393 mlxsw_reg_sfmr_pack(sfmr_pl, !create, fid, fid);
394 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl);
395}
396
397static int mlxsw_sp_fid_map(struct mlxsw_sp *mlxsw_sp, u16 fid, bool valid)
398{
399 enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_VID_TO_FID;
400 char svfa_pl[MLXSW_REG_SVFA_LEN];
401
402 mlxsw_reg_svfa_pack(svfa_pl, 0, mt, valid, fid, fid);
403 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(svfa), svfa_pl);
404}
405
406static struct mlxsw_sp_fid *mlxsw_sp_fid_alloc(u16 fid)
407{
408 struct mlxsw_sp_fid *f;
409
410 f = kzalloc(sizeof(*f), GFP_KERNEL);
411 if (!f)
412 return NULL;
413
414 f->fid = fid;
415
416 return f;
417}
418
419static struct mlxsw_sp_fid *mlxsw_sp_fid_create(struct mlxsw_sp *mlxsw_sp,
420 u16 fid)
421{
422 struct mlxsw_sp_fid *f;
423 int err;
424
425 err = mlxsw_sp_fid_op(mlxsw_sp, fid, true);
426 if (err)
427 return ERR_PTR(err);
428
429 /* Although all the ports member in the FID might be using a
430 * {Port, VID} to FID mapping, we create a global VID-to-FID
431 * mapping. This allows a port to transition to VLAN mode,
432 * knowing the global mapping exists.
433 */
434 err = mlxsw_sp_fid_map(mlxsw_sp, fid, true);
435 if (err)
436 goto err_fid_map;
437
438 f = mlxsw_sp_fid_alloc(fid);
439 if (!f) {
440 err = -ENOMEM;
441 goto err_allocate_fid;
442 }
443
444 list_add(&f->list, &mlxsw_sp->fids);
445
446 return f;
447
448err_allocate_fid:
449 mlxsw_sp_fid_map(mlxsw_sp, fid, false);
450err_fid_map:
451 mlxsw_sp_fid_op(mlxsw_sp, fid, false);
452 return ERR_PTR(err);
453}
454
455static void mlxsw_sp_fid_destroy(struct mlxsw_sp *mlxsw_sp,
456 struct mlxsw_sp_fid *f)
457{
458 u16 fid = f->fid;
459
460 list_del(&f->list);
461
462 kfree(f);
463
464 mlxsw_sp_fid_op(mlxsw_sp, fid, false);
465}
466
467static int __mlxsw_sp_port_fid_join(struct mlxsw_sp_port *mlxsw_sp_port,
468 u16 fid)
469{
470 struct mlxsw_sp_fid *f;
471
472 f = mlxsw_sp_fid_find(mlxsw_sp_port->mlxsw_sp, fid);
473 if (!f) {
474 f = mlxsw_sp_fid_create(mlxsw_sp_port->mlxsw_sp, fid);
475 if (IS_ERR(f))
476 return PTR_ERR(f);
477 }
478
479 f->ref_count++;
480
Ido Schimmel22305372016-06-20 23:04:21 +0200481 netdev_dbg(mlxsw_sp_port->dev, "Joined FID=%d\n", fid);
482
Ido Schimmel14d39462016-06-20 23:04:15 +0200483 return 0;
484}
485
486static void __mlxsw_sp_port_fid_leave(struct mlxsw_sp_port *mlxsw_sp_port,
487 u16 fid)
488{
489 struct mlxsw_sp_fid *f;
490
491 f = mlxsw_sp_fid_find(mlxsw_sp_port->mlxsw_sp, fid);
492 if (WARN_ON(!f))
493 return;
494
Ido Schimmel22305372016-06-20 23:04:21 +0200495 netdev_dbg(mlxsw_sp_port->dev, "Left FID=%d\n", fid);
496
Ido Schimmelfe3f6d12016-06-20 23:04:19 +0200497 mlxsw_sp_port_fdb_flush(mlxsw_sp_port, fid);
498
Ido Schimmel14d39462016-06-20 23:04:15 +0200499 if (--f->ref_count == 0)
500 mlxsw_sp_fid_destroy(mlxsw_sp_port->mlxsw_sp, f);
501}
502
503static int mlxsw_sp_port_fid_map(struct mlxsw_sp_port *mlxsw_sp_port, u16 fid,
504 bool valid)
505{
506 enum mlxsw_reg_svfa_mt mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
507
508 /* If port doesn't have vPorts, then it can use the global
509 * VID-to-FID mapping.
510 */
511 if (list_empty(&mlxsw_sp_port->vports_list))
512 return 0;
513
514 return mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, valid, fid, fid);
515}
516
517static int mlxsw_sp_port_fid_join(struct mlxsw_sp_port *mlxsw_sp_port,
518 u16 fid_begin, u16 fid_end)
519{
520 int fid, err;
521
522 for (fid = fid_begin; fid <= fid_end; fid++) {
523 err = __mlxsw_sp_port_fid_join(mlxsw_sp_port, fid);
524 if (err)
525 goto err_port_fid_join;
526 }
527
528 err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, fid_begin, fid_end,
529 true, false);
530 if (err)
531 goto err_port_flood_set;
532
533 for (fid = fid_begin; fid <= fid_end; fid++) {
534 err = mlxsw_sp_port_fid_map(mlxsw_sp_port, fid, true);
535 if (err)
536 goto err_port_fid_map;
537 }
538
539 return 0;
540
541err_port_fid_map:
542 for (fid--; fid >= fid_begin; fid--)
543 mlxsw_sp_port_fid_map(mlxsw_sp_port, fid, false);
544 __mlxsw_sp_port_flood_set(mlxsw_sp_port, fid_begin, fid_end, false,
545 false);
546err_port_flood_set:
547 fid = fid_end;
548err_port_fid_join:
549 for (fid--; fid >= fid_begin; fid--)
550 __mlxsw_sp_port_fid_leave(mlxsw_sp_port, fid);
551 return err;
552}
553
554static void mlxsw_sp_port_fid_leave(struct mlxsw_sp_port *mlxsw_sp_port,
555 u16 fid_begin, u16 fid_end)
556{
557 int fid;
558
559 for (fid = fid_begin; fid <= fid_end; fid++)
560 mlxsw_sp_port_fid_map(mlxsw_sp_port, fid, false);
561
562 __mlxsw_sp_port_flood_set(mlxsw_sp_port, fid_begin, fid_end, false,
563 false);
564
565 for (fid = fid_begin; fid <= fid_end; fid++)
566 __mlxsw_sp_port_fid_leave(mlxsw_sp_port, fid);
567}
568
Ido Schimmel28a01d22016-02-18 11:30:02 +0100569static int __mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port,
570 u16 vid)
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200571{
572 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
573 char spvid_pl[MLXSW_REG_SPVID_LEN];
574
575 mlxsw_reg_spvid_pack(spvid_pl, mlxsw_sp_port->local_port, vid);
576 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvid), spvid_pl);
577}
578
Ido Schimmel28a01d22016-02-18 11:30:02 +0100579static int mlxsw_sp_port_allow_untagged_set(struct mlxsw_sp_port *mlxsw_sp_port,
580 bool allow)
581{
582 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
583 char spaft_pl[MLXSW_REG_SPAFT_LEN];
584
585 mlxsw_reg_spaft_pack(spaft_pl, mlxsw_sp_port->local_port, allow);
586 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spaft), spaft_pl);
587}
588
589int mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
590{
591 struct net_device *dev = mlxsw_sp_port->dev;
592 int err;
593
594 if (!vid) {
595 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port, false);
596 if (err) {
597 netdev_err(dev, "Failed to disallow untagged traffic\n");
598 return err;
599 }
600 } else {
601 err = __mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid);
602 if (err) {
603 netdev_err(dev, "Failed to set PVID\n");
604 return err;
605 }
606
607 /* Only allow if not already allowed. */
608 if (!mlxsw_sp_port->pvid) {
609 err = mlxsw_sp_port_allow_untagged_set(mlxsw_sp_port,
610 true);
611 if (err) {
612 netdev_err(dev, "Failed to allow untagged traffic\n");
613 goto err_port_allow_untagged_set;
614 }
615 }
616 }
617
618 mlxsw_sp_port->pvid = vid;
619 return 0;
620
621err_port_allow_untagged_set:
622 __mlxsw_sp_port_pvid_set(mlxsw_sp_port, mlxsw_sp_port->pvid);
623 return err;
624}
625
Ido Schimmel3b7ad5e2015-11-19 12:27:39 +0100626static int __mlxsw_sp_port_vlans_set(struct mlxsw_sp_port *mlxsw_sp_port,
627 u16 vid_begin, u16 vid_end, bool is_member,
628 bool untagged)
629{
630 u16 vid, vid_e;
631 int err;
632
633 for (vid = vid_begin; vid <= vid_end;
634 vid += MLXSW_REG_SPVM_REC_MAX_COUNT) {
635 vid_e = min((u16) (vid + MLXSW_REG_SPVM_REC_MAX_COUNT - 1),
636 vid_end);
637
638 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid_e,
639 is_member, untagged);
640 if (err)
641 return err;
642 }
643
644 return 0;
645}
646
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200647static int __mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
648 u16 vid_begin, u16 vid_end,
649 bool flag_untagged, bool flag_pvid)
650{
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200651 struct net_device *dev = mlxsw_sp_port->dev;
Ido Schimmel14d39462016-06-20 23:04:15 +0200652 u16 vid, old_pvid;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200653 int err;
654
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200655 if (!mlxsw_sp_port->bridged)
Ido Schimmel32d863f2016-07-02 11:00:10 +0200656 return -EINVAL;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200657
Ido Schimmel14d39462016-06-20 23:04:15 +0200658 err = mlxsw_sp_port_fid_join(mlxsw_sp_port, vid_begin, vid_end);
Ido Schimmel1b3433a2015-10-28 10:16:57 +0100659 if (err) {
Ido Schimmel14d39462016-06-20 23:04:15 +0200660 netdev_err(dev, "Failed to join FIDs\n");
661 return err;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200662 }
663
Ido Schimmel3b7ad5e2015-11-19 12:27:39 +0100664 err = __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end,
665 true, flag_untagged);
666 if (err) {
667 netdev_err(dev, "Unable to add VIDs %d-%d\n", vid_begin,
668 vid_end);
Ido Schimmelb07a9662015-11-19 12:27:40 +0100669 goto err_port_vlans_set;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200670 }
671
Ido Schimmelb07a9662015-11-19 12:27:40 +0100672 old_pvid = mlxsw_sp_port->pvid;
673 if (flag_pvid && old_pvid != vid_begin) {
674 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid_begin);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200675 if (err) {
Ido Schimmelb07a9662015-11-19 12:27:40 +0100676 netdev_err(dev, "Unable to add PVID %d\n", vid_begin);
677 goto err_port_pvid_set;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200678 }
Ido Schimmel28a01d22016-02-18 11:30:02 +0100679 } else if (!flag_pvid && old_pvid >= vid_begin && old_pvid <= vid_end) {
680 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, 0);
681 if (err) {
682 netdev_err(dev, "Unable to del PVID\n");
683 goto err_port_pvid_set;
684 }
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200685 }
686
687 /* Changing activity bits only if HW operation succeded */
Elad Razfc1273a2016-01-06 13:01:11 +0100688 for (vid = vid_begin; vid <= vid_end; vid++) {
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200689 set_bit(vid, mlxsw_sp_port->active_vlans);
Elad Razfc1273a2016-01-06 13:01:11 +0100690 if (flag_untagged)
691 set_bit(vid, mlxsw_sp_port->untagged_vlans);
692 else
693 clear_bit(vid, mlxsw_sp_port->untagged_vlans);
694 }
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200695
Ido Schimmelb07a9662015-11-19 12:27:40 +0100696 /* STP state change must be done after we set active VLANs */
697 err = mlxsw_sp_port_stp_state_set(mlxsw_sp_port,
698 mlxsw_sp_port->stp_state);
699 if (err) {
700 netdev_err(dev, "Failed to set STP state\n");
701 goto err_port_stp_state_set;
702 }
703
704 return 0;
705
Ido Schimmelb07a9662015-11-19 12:27:40 +0100706err_port_stp_state_set:
707 for (vid = vid_begin; vid <= vid_end; vid++)
708 clear_bit(vid, mlxsw_sp_port->active_vlans);
709 if (old_pvid != mlxsw_sp_port->pvid)
710 mlxsw_sp_port_pvid_set(mlxsw_sp_port, old_pvid);
711err_port_pvid_set:
712 __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end, false,
713 false);
714err_port_vlans_set:
Ido Schimmel14d39462016-06-20 23:04:15 +0200715 mlxsw_sp_port_fid_leave(mlxsw_sp_port, vid_begin, vid_end);
Ido Schimmelb07a9662015-11-19 12:27:40 +0100716 return err;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200717}
718
719static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
720 const struct switchdev_obj_port_vlan *vlan,
721 struct switchdev_trans *trans)
722{
Elad Raze4a13052016-01-06 13:01:09 +0100723 bool flag_untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
724 bool flag_pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200725
726 if (switchdev_trans_ph_prepare(trans))
727 return 0;
728
729 return __mlxsw_sp_port_vlans_add(mlxsw_sp_port,
730 vlan->vid_begin, vlan->vid_end,
Elad Raze4a13052016-01-06 13:01:09 +0100731 flag_untagged, flag_pvid);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200732}
733
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +0100734static enum mlxsw_reg_sfd_rec_policy mlxsw_sp_sfd_rec_policy(bool dynamic)
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200735{
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +0100736 return dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
737 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY;
738}
739
740static enum mlxsw_reg_sfd_op mlxsw_sp_sfd_op(bool adding)
741{
742 return adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
743 MLXSW_REG_SFD_OP_WRITE_REMOVE;
744}
745
Ido Schimmel6e095fd2016-07-04 08:23:13 +0200746static int __mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
747 const char *mac, u16 fid, bool adding,
748 enum mlxsw_reg_sfd_rec_action action,
749 bool dynamic)
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +0100750{
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200751 char *sfd_pl;
752 int err;
753
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200754 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
755 if (!sfd_pl)
756 return -ENOMEM;
757
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +0100758 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
759 mlxsw_reg_sfd_uc_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
Ido Schimmel6e095fd2016-07-04 08:23:13 +0200760 mac, fid, action, local_port);
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +0100761 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
762 kfree(sfd_pl);
763
764 return err;
765}
766
Ido Schimmel6e095fd2016-07-04 08:23:13 +0200767static int mlxsw_sp_port_fdb_uc_op(struct mlxsw_sp *mlxsw_sp, u8 local_port,
768 const char *mac, u16 fid, bool adding,
769 bool dynamic)
770{
771 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid, adding,
772 MLXSW_REG_SFD_REC_ACTION_NOP, dynamic);
773}
774
775int mlxsw_sp_rif_fdb_op(struct mlxsw_sp *mlxsw_sp, const char *mac, u16 fid,
776 bool adding)
777{
778 return __mlxsw_sp_port_fdb_uc_op(mlxsw_sp, 0, mac, fid, adding,
779 MLXSW_REG_SFD_REC_ACTION_FORWARD_IP_ROUTER,
780 false);
781}
782
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +0100783static int mlxsw_sp_port_fdb_uc_lag_op(struct mlxsw_sp *mlxsw_sp, u16 lag_id,
Ido Schimmel64771e32015-12-15 16:03:46 +0100784 const char *mac, u16 fid, u16 lag_vid,
785 bool adding, bool dynamic)
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +0100786{
787 char *sfd_pl;
788 int err;
789
790 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
791 if (!sfd_pl)
792 return -ENOMEM;
793
794 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
795 mlxsw_reg_sfd_uc_lag_pack(sfd_pl, 0, mlxsw_sp_sfd_rec_policy(dynamic),
Ido Schimmel64771e32015-12-15 16:03:46 +0100796 mac, fid, MLXSW_REG_SFD_REC_ACTION_NOP,
797 lag_vid, lag_id);
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +0100798 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200799 kfree(sfd_pl);
800
801 return err;
802}
803
804static int
805mlxsw_sp_port_fdb_static_add(struct mlxsw_sp_port *mlxsw_sp_port,
806 const struct switchdev_obj_port_fdb *fdb,
807 struct switchdev_trans *trans)
808{
Elad Raze4b6f692016-01-10 21:06:27 +0100809 u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, fdb->vid);
Ido Schimmel64771e32015-12-15 16:03:46 +0100810 u16 lag_vid = 0;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +0100811
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200812 if (switchdev_trans_ph_prepare(trans))
813 return 0;
814
Ido Schimmel54a73202015-12-15 16:03:41 +0100815 if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
Ido Schimmel64771e32015-12-15 16:03:46 +0100816 lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
Ido Schimmel54a73202015-12-15 16:03:41 +0100817 }
818
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +0100819 if (!mlxsw_sp_port->lagged)
Jiri Pirko2fa9d452016-01-07 11:50:29 +0100820 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp_port->mlxsw_sp,
821 mlxsw_sp_port->local_port,
Ido Schimmel9de6a802015-12-15 16:03:40 +0100822 fdb->addr, fid, true, false);
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +0100823 else
824 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp_port->mlxsw_sp,
825 mlxsw_sp_port->lag_id,
Ido Schimmel64771e32015-12-15 16:03:46 +0100826 fdb->addr, fid, lag_vid,
827 true, false);
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200828}
829
Elad Raz3a49b4f2016-01-10 21:06:28 +0100830static int mlxsw_sp_port_mdb_op(struct mlxsw_sp *mlxsw_sp, const char *addr,
831 u16 fid, u16 mid, bool adding)
832{
833 char *sfd_pl;
834 int err;
835
836 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
837 if (!sfd_pl)
838 return -ENOMEM;
839
840 mlxsw_reg_sfd_pack(sfd_pl, mlxsw_sp_sfd_op(adding), 0);
841 mlxsw_reg_sfd_mc_pack(sfd_pl, 0, addr, fid,
842 MLXSW_REG_SFD_REC_ACTION_NOP, mid);
843 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
844 kfree(sfd_pl);
845 return err;
846}
847
848static int mlxsw_sp_port_smid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 mid,
849 bool add, bool clear_all_ports)
850{
851 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
852 char *smid_pl;
853 int err, i;
854
855 smid_pl = kmalloc(MLXSW_REG_SMID_LEN, GFP_KERNEL);
856 if (!smid_pl)
857 return -ENOMEM;
858
859 mlxsw_reg_smid_pack(smid_pl, mid, mlxsw_sp_port->local_port, add);
860 if (clear_all_ports) {
861 for (i = 1; i < MLXSW_PORT_MAX_PORTS; i++)
862 if (mlxsw_sp->ports[i])
863 mlxsw_reg_smid_port_mask_set(smid_pl, i, 1);
864 }
865 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(smid), smid_pl);
866 kfree(smid_pl);
867 return err;
868}
869
870static struct mlxsw_sp_mid *__mlxsw_sp_mc_get(struct mlxsw_sp *mlxsw_sp,
871 const unsigned char *addr,
872 u16 vid)
873{
874 struct mlxsw_sp_mid *mid;
875
876 list_for_each_entry(mid, &mlxsw_sp->br_mids.list, list) {
877 if (ether_addr_equal(mid->addr, addr) && mid->vid == vid)
878 return mid;
879 }
880 return NULL;
881}
882
883static struct mlxsw_sp_mid *__mlxsw_sp_mc_alloc(struct mlxsw_sp *mlxsw_sp,
884 const unsigned char *addr,
885 u16 vid)
886{
887 struct mlxsw_sp_mid *mid;
888 u16 mid_idx;
889
890 mid_idx = find_first_zero_bit(mlxsw_sp->br_mids.mapped,
891 MLXSW_SP_MID_MAX);
892 if (mid_idx == MLXSW_SP_MID_MAX)
893 return NULL;
894
895 mid = kzalloc(sizeof(*mid), GFP_KERNEL);
896 if (!mid)
897 return NULL;
898
899 set_bit(mid_idx, mlxsw_sp->br_mids.mapped);
900 ether_addr_copy(mid->addr, addr);
901 mid->vid = vid;
902 mid->mid = mid_idx;
903 mid->ref_count = 0;
904 list_add_tail(&mid->list, &mlxsw_sp->br_mids.list);
905
906 return mid;
907}
908
909static int __mlxsw_sp_mc_dec_ref(struct mlxsw_sp *mlxsw_sp,
910 struct mlxsw_sp_mid *mid)
911{
912 if (--mid->ref_count == 0) {
913 list_del(&mid->list);
914 clear_bit(mid->mid, mlxsw_sp->br_mids.mapped);
915 kfree(mid);
916 return 1;
917 }
918 return 0;
919}
920
921static int mlxsw_sp_port_mdb_add(struct mlxsw_sp_port *mlxsw_sp_port,
922 const struct switchdev_obj_port_mdb *mdb,
923 struct switchdev_trans *trans)
924{
925 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
926 struct net_device *dev = mlxsw_sp_port->dev;
927 struct mlxsw_sp_mid *mid;
928 u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, mdb->vid);
929 int err = 0;
930
931 if (switchdev_trans_ph_prepare(trans))
932 return 0;
933
934 mid = __mlxsw_sp_mc_get(mlxsw_sp, mdb->addr, mdb->vid);
935 if (!mid) {
936 mid = __mlxsw_sp_mc_alloc(mlxsw_sp, mdb->addr, mdb->vid);
937 if (!mid) {
938 netdev_err(dev, "Unable to allocate MC group\n");
939 return -ENOMEM;
940 }
941 }
942 mid->ref_count++;
943
944 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, true,
945 mid->ref_count == 1);
946 if (err) {
947 netdev_err(dev, "Unable to set SMID\n");
948 goto err_out;
949 }
950
951 if (mid->ref_count == 1) {
952 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mdb->addr, fid, mid->mid,
953 true);
954 if (err) {
955 netdev_err(dev, "Unable to set MC SFD\n");
956 goto err_out;
957 }
958 }
959
960 return 0;
961
962err_out:
963 __mlxsw_sp_mc_dec_ref(mlxsw_sp, mid);
964 return err;
965}
966
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200967static int mlxsw_sp_port_obj_add(struct net_device *dev,
968 const struct switchdev_obj *obj,
969 struct switchdev_trans *trans)
970{
971 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
972 int err = 0;
973
Ido Schimmel54a73202015-12-15 16:03:41 +0100974 mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port);
975 if (!mlxsw_sp_port)
976 return -EINVAL;
977
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200978 switch (obj->id) {
979 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Ido Schimmel54a73202015-12-15 16:03:41 +0100980 if (mlxsw_sp_port_is_vport(mlxsw_sp_port))
981 return 0;
982
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200983 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port,
984 SWITCHDEV_OBJ_PORT_VLAN(obj),
985 trans);
986 break;
Jiri Pirko61c503f2016-07-04 08:23:11 +0200987 case SWITCHDEV_OBJ_ID_IPV4_FIB:
988 err = mlxsw_sp_router_fib4_add(mlxsw_sp_port,
989 SWITCHDEV_OBJ_IPV4_FIB(obj),
990 trans);
991 break;
Jiri Pirko56ade8f2015-10-16 14:01:37 +0200992 case SWITCHDEV_OBJ_ID_PORT_FDB:
993 err = mlxsw_sp_port_fdb_static_add(mlxsw_sp_port,
994 SWITCHDEV_OBJ_PORT_FDB(obj),
995 trans);
996 break;
Elad Raz3a49b4f2016-01-10 21:06:28 +0100997 case SWITCHDEV_OBJ_ID_PORT_MDB:
998 err = mlxsw_sp_port_mdb_add(mlxsw_sp_port,
999 SWITCHDEV_OBJ_PORT_MDB(obj),
1000 trans);
1001 break;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001002 default:
1003 err = -EOPNOTSUPP;
1004 break;
1005 }
1006
1007 return err;
1008}
1009
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001010static int __mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1011 u16 vid_begin, u16 vid_end, bool init)
1012{
1013 struct net_device *dev = mlxsw_sp_port->dev;
Ido Schimmel3b7ad5e2015-11-19 12:27:39 +01001014 u16 vid, pvid;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001015 int err;
1016
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001017 if (!init && !mlxsw_sp_port->bridged)
Ido Schimmel32d863f2016-07-02 11:00:10 +02001018 return -EINVAL;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001019
Ido Schimmel3b7ad5e2015-11-19 12:27:39 +01001020 err = __mlxsw_sp_port_vlans_set(mlxsw_sp_port, vid_begin, vid_end,
1021 false, false);
1022 if (err) {
1023 netdev_err(dev, "Unable to del VIDs %d-%d\n", vid_begin,
1024 vid_end);
1025 return err;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001026 }
1027
Ido Schimmel28a01d22016-02-18 11:30:02 +01001028 if (init)
1029 goto out;
1030
Ido Schimmel06c071f2015-11-19 12:27:38 +01001031 pvid = mlxsw_sp_port->pvid;
Ido Schimmel28a01d22016-02-18 11:30:02 +01001032 if (pvid >= vid_begin && pvid <= vid_end) {
1033 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, 0);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001034 if (err) {
Ido Schimmel06c071f2015-11-19 12:27:38 +01001035 netdev_err(dev, "Unable to del PVID %d\n", pvid);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001036 return err;
1037 }
1038 }
1039
Ido Schimmel14d39462016-06-20 23:04:15 +02001040 mlxsw_sp_port_fid_leave(mlxsw_sp_port, vid_begin, vid_end);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001041
1042out:
1043 /* Changing activity bits only if HW operation succeded */
1044 for (vid = vid_begin; vid <= vid_end; vid++)
1045 clear_bit(vid, mlxsw_sp_port->active_vlans);
1046
1047 return 0;
1048}
1049
1050static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
1051 const struct switchdev_obj_port_vlan *vlan)
1052{
1053 return __mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1054 vlan->vid_begin, vlan->vid_end, false);
1055}
1056
Ido Schimmel4dc236c2016-01-27 15:20:16 +01001057void mlxsw_sp_port_active_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port)
1058{
1059 u16 vid;
1060
1061 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID)
1062 __mlxsw_sp_port_vlans_del(mlxsw_sp_port, vid, vid, false);
1063}
1064
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001065static int
1066mlxsw_sp_port_fdb_static_del(struct mlxsw_sp_port *mlxsw_sp_port,
1067 const struct switchdev_obj_port_fdb *fdb)
1068{
Elad Raze4b6f692016-01-10 21:06:27 +01001069 u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, fdb->vid);
Ido Schimmel64771e32015-12-15 16:03:46 +01001070 u16 lag_vid = 0;
Ido Schimmel9de6a802015-12-15 16:03:40 +01001071
Ido Schimmel54a73202015-12-15 16:03:41 +01001072 if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
Ido Schimmel64771e32015-12-15 16:03:46 +01001073 lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
Ido Schimmel54a73202015-12-15 16:03:41 +01001074 }
1075
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001076 if (!mlxsw_sp_port->lagged)
Jiri Pirko2fa9d452016-01-07 11:50:29 +01001077 return mlxsw_sp_port_fdb_uc_op(mlxsw_sp_port->mlxsw_sp,
1078 mlxsw_sp_port->local_port,
Ido Schimmel9de6a802015-12-15 16:03:40 +01001079 fdb->addr, fid,
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001080 false, false);
1081 else
1082 return mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp_port->mlxsw_sp,
1083 mlxsw_sp_port->lag_id,
Ido Schimmel64771e32015-12-15 16:03:46 +01001084 fdb->addr, fid, lag_vid,
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001085 false, false);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001086}
1087
Elad Raz3a49b4f2016-01-10 21:06:28 +01001088static int mlxsw_sp_port_mdb_del(struct mlxsw_sp_port *mlxsw_sp_port,
1089 const struct switchdev_obj_port_mdb *mdb)
1090{
1091 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
1092 struct net_device *dev = mlxsw_sp_port->dev;
1093 struct mlxsw_sp_mid *mid;
1094 u16 fid = mlxsw_sp_port_vid_to_fid_get(mlxsw_sp_port, mdb->vid);
1095 u16 mid_idx;
1096 int err = 0;
1097
1098 mid = __mlxsw_sp_mc_get(mlxsw_sp, mdb->addr, mdb->vid);
1099 if (!mid) {
1100 netdev_err(dev, "Unable to remove port from MC DB\n");
1101 return -EINVAL;
1102 }
1103
1104 err = mlxsw_sp_port_smid_set(mlxsw_sp_port, mid->mid, false, false);
1105 if (err)
1106 netdev_err(dev, "Unable to remove port from SMID\n");
1107
1108 mid_idx = mid->mid;
1109 if (__mlxsw_sp_mc_dec_ref(mlxsw_sp, mid)) {
1110 err = mlxsw_sp_port_mdb_op(mlxsw_sp, mdb->addr, fid, mid_idx,
1111 false);
1112 if (err)
1113 netdev_err(dev, "Unable to remove MC SFD\n");
1114 }
1115
1116 return err;
1117}
1118
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001119static int mlxsw_sp_port_obj_del(struct net_device *dev,
1120 const struct switchdev_obj *obj)
1121{
1122 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1123 int err = 0;
1124
Ido Schimmel54a73202015-12-15 16:03:41 +01001125 mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port);
1126 if (!mlxsw_sp_port)
1127 return -EINVAL;
1128
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001129 switch (obj->id) {
1130 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Ido Schimmel54a73202015-12-15 16:03:41 +01001131 if (mlxsw_sp_port_is_vport(mlxsw_sp_port))
1132 return 0;
1133
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001134 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
1135 SWITCHDEV_OBJ_PORT_VLAN(obj));
1136 break;
Jiri Pirko61c503f2016-07-04 08:23:11 +02001137 case SWITCHDEV_OBJ_ID_IPV4_FIB:
1138 err = mlxsw_sp_router_fib4_del(mlxsw_sp_port,
1139 SWITCHDEV_OBJ_IPV4_FIB(obj));
1140 break;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001141 case SWITCHDEV_OBJ_ID_PORT_FDB:
1142 err = mlxsw_sp_port_fdb_static_del(mlxsw_sp_port,
1143 SWITCHDEV_OBJ_PORT_FDB(obj));
1144 break;
Elad Raz3a49b4f2016-01-10 21:06:28 +01001145 case SWITCHDEV_OBJ_ID_PORT_MDB:
1146 err = mlxsw_sp_port_mdb_del(mlxsw_sp_port,
1147 SWITCHDEV_OBJ_PORT_MDB(obj));
Dan Carpenter00ae40e2016-01-13 15:28:23 +03001148 break;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001149 default:
1150 err = -EOPNOTSUPP;
1151 break;
1152 }
1153
1154 return err;
1155}
1156
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001157static struct mlxsw_sp_port *mlxsw_sp_lag_rep_port(struct mlxsw_sp *mlxsw_sp,
1158 u16 lag_id)
1159{
1160 struct mlxsw_sp_port *mlxsw_sp_port;
1161 int i;
1162
1163 for (i = 0; i < MLXSW_SP_PORT_PER_LAG_MAX; i++) {
1164 mlxsw_sp_port = mlxsw_sp_port_lagged_get(mlxsw_sp, lag_id, i);
1165 if (mlxsw_sp_port)
1166 return mlxsw_sp_port;
1167 }
1168 return NULL;
1169}
1170
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001171static int mlxsw_sp_port_fdb_dump(struct mlxsw_sp_port *mlxsw_sp_port,
1172 struct switchdev_obj_port_fdb *fdb,
Ido Schimmel304f5152016-01-27 15:20:24 +01001173 switchdev_obj_dump_cb_t *cb,
1174 struct net_device *orig_dev)
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001175{
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001176 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
Ido Schimmel3f47f862016-01-27 15:20:25 +01001177 struct mlxsw_sp_port *tmp;
Ido Schimmel56918b62016-06-20 23:04:18 +02001178 struct mlxsw_sp_fid *f;
1179 u16 vport_fid;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001180 char *sfd_pl;
1181 char mac[ETH_ALEN];
Ido Schimmel9de6a802015-12-15 16:03:40 +01001182 u16 fid;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001183 u8 local_port;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001184 u16 lag_id;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001185 u8 num_rec;
1186 int stored_err = 0;
1187 int i;
1188 int err;
1189
1190 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
1191 if (!sfd_pl)
1192 return -ENOMEM;
1193
Ido Schimmel56918b62016-06-20 23:04:18 +02001194 f = mlxsw_sp_vport_fid_get(mlxsw_sp_port);
1195 vport_fid = f ? f->fid : 0;
Ido Schimmel54a73202015-12-15 16:03:41 +01001196
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001197 mlxsw_reg_sfd_pack(sfd_pl, MLXSW_REG_SFD_OP_QUERY_DUMP, 0);
1198 do {
1199 mlxsw_reg_sfd_num_rec_set(sfd_pl, MLXSW_REG_SFD_REC_MAX_COUNT);
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001200 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfd), sfd_pl);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001201 if (err)
1202 goto out;
1203
1204 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
1205
1206 /* Even in case of error, we have to run the dump to the end
1207 * so the session in firmware is finished.
1208 */
1209 if (stored_err)
1210 continue;
1211
1212 for (i = 0; i < num_rec; i++) {
1213 switch (mlxsw_reg_sfd_rec_type_get(sfd_pl, i)) {
1214 case MLXSW_REG_SFD_REC_TYPE_UNICAST:
Ido Schimmel9de6a802015-12-15 16:03:40 +01001215 mlxsw_reg_sfd_uc_unpack(sfd_pl, i, mac, &fid,
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001216 &local_port);
1217 if (local_port == mlxsw_sp_port->local_port) {
Ido Schimmel004f85e2016-01-27 15:20:22 +01001218 if (vport_fid && vport_fid == fid)
1219 fdb->vid = 0;
1220 else if (!vport_fid &&
1221 !mlxsw_sp_fid_is_vfid(fid))
Ido Schimmel54a73202015-12-15 16:03:41 +01001222 fdb->vid = fid;
Ido Schimmel004f85e2016-01-27 15:20:22 +01001223 else
1224 continue;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001225 ether_addr_copy(fdb->addr, mac);
1226 fdb->ndm_state = NUD_REACHABLE;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001227 err = cb(&fdb->obj);
1228 if (err)
1229 stored_err = err;
1230 }
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001231 break;
1232 case MLXSW_REG_SFD_REC_TYPE_UNICAST_LAG:
1233 mlxsw_reg_sfd_uc_lag_unpack(sfd_pl, i,
Ido Schimmel9de6a802015-12-15 16:03:40 +01001234 mac, &fid, &lag_id);
Ido Schimmel3f47f862016-01-27 15:20:25 +01001235 tmp = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
1236 if (tmp && tmp->local_port ==
1237 mlxsw_sp_port->local_port) {
Ido Schimmel304f5152016-01-27 15:20:24 +01001238 /* LAG records can only point to LAG
1239 * devices or VLAN devices on top.
1240 */
1241 if (!netif_is_lag_master(orig_dev) &&
1242 !is_vlan_dev(orig_dev))
1243 continue;
Ido Schimmel004f85e2016-01-27 15:20:22 +01001244 if (vport_fid && vport_fid == fid)
1245 fdb->vid = 0;
1246 else if (!vport_fid &&
1247 !mlxsw_sp_fid_is_vfid(fid))
Ido Schimmel54a73202015-12-15 16:03:41 +01001248 fdb->vid = fid;
Ido Schimmel004f85e2016-01-27 15:20:22 +01001249 else
1250 continue;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001251 ether_addr_copy(fdb->addr, mac);
1252 fdb->ndm_state = NUD_REACHABLE;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001253 err = cb(&fdb->obj);
1254 if (err)
1255 stored_err = err;
1256 }
1257 break;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001258 }
1259 }
1260 } while (num_rec == MLXSW_REG_SFD_REC_MAX_COUNT);
1261
1262out:
1263 kfree(sfd_pl);
1264 return stored_err ? stored_err : err;
1265}
1266
1267static int mlxsw_sp_port_vlan_dump(struct mlxsw_sp_port *mlxsw_sp_port,
1268 struct switchdev_obj_port_vlan *vlan,
1269 switchdev_obj_dump_cb_t *cb)
1270{
1271 u16 vid;
1272 int err = 0;
1273
Ido Schimmel54a73202015-12-15 16:03:41 +01001274 if (mlxsw_sp_port_is_vport(mlxsw_sp_port)) {
1275 vlan->flags = 0;
1276 vlan->vid_begin = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
1277 vlan->vid_end = mlxsw_sp_vport_vid_get(mlxsw_sp_port);
1278 return cb(&vlan->obj);
1279 }
1280
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001281 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
1282 vlan->flags = 0;
1283 if (vid == mlxsw_sp_port->pvid)
1284 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
Elad Razfc1273a2016-01-06 13:01:11 +01001285 if (test_bit(vid, mlxsw_sp_port->untagged_vlans))
1286 vlan->flags |= BRIDGE_VLAN_INFO_UNTAGGED;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001287 vlan->vid_begin = vid;
1288 vlan->vid_end = vid;
1289 err = cb(&vlan->obj);
1290 if (err)
1291 break;
1292 }
1293 return err;
1294}
1295
1296static int mlxsw_sp_port_obj_dump(struct net_device *dev,
1297 struct switchdev_obj *obj,
1298 switchdev_obj_dump_cb_t *cb)
1299{
1300 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
1301 int err = 0;
1302
Ido Schimmel54a73202015-12-15 16:03:41 +01001303 mlxsw_sp_port = mlxsw_sp_port_orig_get(obj->orig_dev, mlxsw_sp_port);
1304 if (!mlxsw_sp_port)
1305 return -EINVAL;
1306
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001307 switch (obj->id) {
1308 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1309 err = mlxsw_sp_port_vlan_dump(mlxsw_sp_port,
1310 SWITCHDEV_OBJ_PORT_VLAN(obj), cb);
1311 break;
1312 case SWITCHDEV_OBJ_ID_PORT_FDB:
1313 err = mlxsw_sp_port_fdb_dump(mlxsw_sp_port,
Ido Schimmel304f5152016-01-27 15:20:24 +01001314 SWITCHDEV_OBJ_PORT_FDB(obj), cb,
1315 obj->orig_dev);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001316 break;
1317 default:
1318 err = -EOPNOTSUPP;
1319 break;
1320 }
1321
1322 return err;
1323}
1324
Jiri Pirkoc7070fc2015-10-28 10:17:05 +01001325static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001326 .switchdev_port_attr_get = mlxsw_sp_port_attr_get,
1327 .switchdev_port_attr_set = mlxsw_sp_port_attr_set,
1328 .switchdev_port_obj_add = mlxsw_sp_port_obj_add,
1329 .switchdev_port_obj_del = mlxsw_sp_port_obj_del,
1330 .switchdev_port_obj_dump = mlxsw_sp_port_obj_dump,
1331};
1332
Ido Schimmel45827d72016-01-27 15:20:21 +01001333static void mlxsw_sp_fdb_call_notifiers(bool learning_sync, bool adding,
1334 char *mac, u16 vid,
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001335 struct net_device *dev)
1336{
1337 struct switchdev_notifier_fdb_info info;
1338 unsigned long notifier_type;
1339
Ido Schimmel45827d72016-01-27 15:20:21 +01001340 if (learning_sync) {
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001341 info.addr = mac;
1342 info.vid = vid;
1343 notifier_type = adding ? SWITCHDEV_FDB_ADD : SWITCHDEV_FDB_DEL;
1344 call_switchdev_notifiers(notifier_type, dev, &info.info);
1345 }
1346}
1347
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001348static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
1349 char *sfn_pl, int rec_index,
1350 bool adding)
1351{
1352 struct mlxsw_sp_port *mlxsw_sp_port;
1353 char mac[ETH_ALEN];
1354 u8 local_port;
Ido Schimmel9de6a802015-12-15 16:03:40 +01001355 u16 vid, fid;
Jiri Pirko12f15012016-01-07 11:50:30 +01001356 bool do_notification = true;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001357 int err;
1358
Ido Schimmel9de6a802015-12-15 16:03:40 +01001359 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &fid, &local_port);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001360 mlxsw_sp_port = mlxsw_sp->ports[local_port];
1361 if (!mlxsw_sp_port) {
1362 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
Jiri Pirko12f15012016-01-07 11:50:30 +01001363 goto just_remove;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001364 }
1365
Ido Schimmelaac78a42015-12-15 16:03:42 +01001366 if (mlxsw_sp_fid_is_vfid(fid)) {
Ido Schimmelaac78a42015-12-15 16:03:42 +01001367 struct mlxsw_sp_port *mlxsw_sp_vport;
1368
Ido Schimmeld0ec8752016-06-20 23:04:12 +02001369 mlxsw_sp_vport = mlxsw_sp_port_vport_find_by_fid(mlxsw_sp_port,
1370 fid);
Ido Schimmelaac78a42015-12-15 16:03:42 +01001371 if (!mlxsw_sp_vport) {
1372 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching vPort following FDB notification\n");
Jiri Pirko12f15012016-01-07 11:50:30 +01001373 goto just_remove;
Ido Schimmelaac78a42015-12-15 16:03:42 +01001374 }
Ido Schimmel004f85e2016-01-27 15:20:22 +01001375 vid = 0;
Ido Schimmelaac78a42015-12-15 16:03:42 +01001376 /* Override the physical port with the vPort. */
1377 mlxsw_sp_port = mlxsw_sp_vport;
1378 } else {
1379 vid = fid;
1380 }
1381
Jiri Pirko12f15012016-01-07 11:50:30 +01001382 adding = adding && mlxsw_sp_port->learning;
1383
1384do_fdb_op:
Jiri Pirko2fa9d452016-01-07 11:50:29 +01001385 err = mlxsw_sp_port_fdb_uc_op(mlxsw_sp, local_port, mac, fid,
Jiri Pirko12f15012016-01-07 11:50:30 +01001386 adding, true);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001387 if (err) {
1388 if (net_ratelimit())
1389 netdev_err(mlxsw_sp_port->dev, "Failed to set FDB entry\n");
1390 return;
1391 }
1392
Jiri Pirko12f15012016-01-07 11:50:30 +01001393 if (!do_notification)
1394 return;
Ido Schimmel45827d72016-01-27 15:20:21 +01001395 mlxsw_sp_fdb_call_notifiers(mlxsw_sp_port->learning_sync,
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001396 adding, mac, vid, mlxsw_sp_port->dev);
Jiri Pirko12f15012016-01-07 11:50:30 +01001397 return;
1398
1399just_remove:
1400 adding = false;
1401 do_notification = false;
1402 goto do_fdb_op;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001403}
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001404
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001405static void mlxsw_sp_fdb_notify_mac_lag_process(struct mlxsw_sp *mlxsw_sp,
1406 char *sfn_pl, int rec_index,
1407 bool adding)
1408{
1409 struct mlxsw_sp_port *mlxsw_sp_port;
Ido Schimmele43aca22016-01-27 15:20:23 +01001410 struct net_device *dev;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001411 char mac[ETH_ALEN];
Ido Schimmel64771e32015-12-15 16:03:46 +01001412 u16 lag_vid = 0;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001413 u16 lag_id;
Ido Schimmel9de6a802015-12-15 16:03:40 +01001414 u16 vid, fid;
Jiri Pirko12f15012016-01-07 11:50:30 +01001415 bool do_notification = true;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001416 int err;
1417
Ido Schimmel9de6a802015-12-15 16:03:40 +01001418 mlxsw_reg_sfn_mac_lag_unpack(sfn_pl, rec_index, mac, &fid, &lag_id);
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001419 mlxsw_sp_port = mlxsw_sp_lag_rep_port(mlxsw_sp, lag_id);
1420 if (!mlxsw_sp_port) {
1421 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Cannot find port representor for LAG\n");
Jiri Pirko12f15012016-01-07 11:50:30 +01001422 goto just_remove;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001423 }
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001424
Ido Schimmelaac78a42015-12-15 16:03:42 +01001425 if (mlxsw_sp_fid_is_vfid(fid)) {
Ido Schimmelaac78a42015-12-15 16:03:42 +01001426 struct mlxsw_sp_port *mlxsw_sp_vport;
1427
Ido Schimmeld0ec8752016-06-20 23:04:12 +02001428 mlxsw_sp_vport = mlxsw_sp_port_vport_find_by_fid(mlxsw_sp_port,
1429 fid);
Ido Schimmelaac78a42015-12-15 16:03:42 +01001430 if (!mlxsw_sp_vport) {
1431 netdev_err(mlxsw_sp_port->dev, "Failed to find a matching vPort following FDB notification\n");
Jiri Pirko12f15012016-01-07 11:50:30 +01001432 goto just_remove;
Ido Schimmelaac78a42015-12-15 16:03:42 +01001433 }
1434
Ido Schimmel004f85e2016-01-27 15:20:22 +01001435 lag_vid = mlxsw_sp_vport_vid_get(mlxsw_sp_vport);
Ido Schimmele43aca22016-01-27 15:20:23 +01001436 dev = mlxsw_sp_vport->dev;
Ido Schimmel004f85e2016-01-27 15:20:22 +01001437 vid = 0;
Ido Schimmelaac78a42015-12-15 16:03:42 +01001438 /* Override the physical port with the vPort. */
1439 mlxsw_sp_port = mlxsw_sp_vport;
1440 } else {
Ido Schimmele43aca22016-01-27 15:20:23 +01001441 dev = mlxsw_sp_lag_get(mlxsw_sp, lag_id)->dev;
Ido Schimmelaac78a42015-12-15 16:03:42 +01001442 vid = fid;
1443 }
1444
Jiri Pirko12f15012016-01-07 11:50:30 +01001445 adding = adding && mlxsw_sp_port->learning;
1446
1447do_fdb_op:
Ido Schimmel64771e32015-12-15 16:03:46 +01001448 err = mlxsw_sp_port_fdb_uc_lag_op(mlxsw_sp, lag_id, mac, fid, lag_vid,
Jiri Pirko12f15012016-01-07 11:50:30 +01001449 adding, true);
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001450 if (err) {
1451 if (net_ratelimit())
1452 netdev_err(mlxsw_sp_port->dev, "Failed to set FDB entry\n");
1453 return;
1454 }
1455
Jiri Pirko12f15012016-01-07 11:50:30 +01001456 if (!do_notification)
1457 return;
Ido Schimmel45827d72016-01-27 15:20:21 +01001458 mlxsw_sp_fdb_call_notifiers(mlxsw_sp_port->learning_sync, adding, mac,
Ido Schimmele43aca22016-01-27 15:20:23 +01001459 vid, dev);
Jiri Pirko12f15012016-01-07 11:50:30 +01001460 return;
1461
1462just_remove:
1463 adding = false;
1464 do_notification = false;
1465 goto do_fdb_op;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001466}
1467
1468static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
1469 char *sfn_pl, int rec_index)
1470{
1471 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
1472 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
1473 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
1474 rec_index, true);
1475 break;
1476 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
1477 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
1478 rec_index, false);
1479 break;
Jiri Pirko8a1ab5d2015-12-03 12:12:29 +01001480 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC_LAG:
1481 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
1482 rec_index, true);
1483 break;
1484 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC_LAG:
1485 mlxsw_sp_fdb_notify_mac_lag_process(mlxsw_sp, sfn_pl,
1486 rec_index, false);
1487 break;
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001488 }
1489}
1490
1491static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
1492{
Jiri Pirkodd9bdb02016-04-14 18:19:28 +02001493 mlxsw_core_schedule_dw(&mlxsw_sp->fdb_notify.dw,
1494 msecs_to_jiffies(mlxsw_sp->fdb_notify.interval));
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001495}
1496
1497static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
1498{
1499 struct mlxsw_sp *mlxsw_sp;
1500 char *sfn_pl;
1501 u8 num_rec;
1502 int i;
1503 int err;
1504
1505 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
1506 if (!sfn_pl)
1507 return;
1508
1509 mlxsw_sp = container_of(work, struct mlxsw_sp, fdb_notify.dw.work);
1510
Ido Schimmel4f2c6ae2016-01-27 15:16:43 +01001511 rtnl_lock();
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001512 do {
1513 mlxsw_reg_sfn_pack(sfn_pl);
1514 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
1515 if (err) {
1516 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
1517 break;
1518 }
1519 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
1520 for (i = 0; i < num_rec; i++)
1521 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
1522
1523 } while (num_rec);
Ido Schimmel4f2c6ae2016-01-27 15:16:43 +01001524 rtnl_unlock();
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001525
1526 kfree(sfn_pl);
1527 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
1528}
1529
1530static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
1531{
1532 int err;
1533
1534 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
1535 if (err) {
1536 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
1537 return err;
1538 }
1539 INIT_DELAYED_WORK(&mlxsw_sp->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
1540 mlxsw_sp->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
1541 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
1542 return 0;
1543}
1544
1545static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
1546{
1547 cancel_delayed_work_sync(&mlxsw_sp->fdb_notify.dw);
1548}
1549
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001550int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
1551{
1552 return mlxsw_sp_fdb_init(mlxsw_sp);
1553}
1554
1555void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
1556{
1557 mlxsw_sp_fdb_fini(mlxsw_sp);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001558}
1559
1560int mlxsw_sp_port_vlan_init(struct mlxsw_sp_port *mlxsw_sp_port)
1561{
1562 struct net_device *dev = mlxsw_sp_port->dev;
1563 int err;
1564
1565 /* Allow only untagged packets to ingress and tag them internally
1566 * with VID 1.
1567 */
1568 mlxsw_sp_port->pvid = 1;
Elad Raz29edf442016-01-06 13:01:08 +01001569 err = __mlxsw_sp_port_vlans_del(mlxsw_sp_port, 0, VLAN_N_VID - 1,
1570 true);
Jiri Pirko56ade8f2015-10-16 14:01:37 +02001571 if (err) {
1572 netdev_err(dev, "Unable to init VLANs\n");
1573 return err;
1574 }
1575
1576 /* Add implicit VLAN interface in the device, so that untagged
1577 * packets will be classified to the default vFID.
1578 */
1579 err = mlxsw_sp_port_add_vid(dev, 0, 1);
1580 if (err)
1581 netdev_err(dev, "Failed to configure default vFID\n");
1582
1583 return err;
1584}
1585
1586void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
1587{
1588 mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
1589}
1590
1591void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)
1592{
1593}