1 /*
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>
48 #include <linux/rtnetlink.h>
49 #include <net/switchdev.h>
50
51 #include "spectrum.h"
52 #include "core.h"
53 #include "reg.h"
54
mlxsw_sp_port_attr_get(struct net_device * dev,struct switchdev_attr * attr)55 static int mlxsw_sp_port_attr_get(struct net_device *dev,
56 struct switchdev_attr *attr)
57 {
58 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
59 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
60
61 switch (attr->id) {
62 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
63 attr->u.ppid.id_len = sizeof(mlxsw_sp->base_mac);
64 memcpy(&attr->u.ppid.id, &mlxsw_sp->base_mac,
65 attr->u.ppid.id_len);
66 break;
67 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
68 attr->u.brport_flags =
69 (mlxsw_sp_port->learning ? BR_LEARNING : 0) |
70 (mlxsw_sp_port->learning_sync ? BR_LEARNING_SYNC : 0) |
71 (mlxsw_sp_port->uc_flood ? BR_FLOOD : 0);
72 break;
73 default:
74 return -EOPNOTSUPP;
75 }
76
77 return 0;
78 }
79
mlxsw_sp_port_stp_state_set(struct mlxsw_sp_port * mlxsw_sp_port,u8 state)80 static int mlxsw_sp_port_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
81 u8 state)
82 {
83 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
84 enum mlxsw_reg_spms_state spms_state;
85 char *spms_pl;
86 u16 vid;
87 int err;
88
89 switch (state) {
90 case BR_STATE_DISABLED: /* fall-through */
91 case BR_STATE_FORWARDING:
92 spms_state = MLXSW_REG_SPMS_STATE_FORWARDING;
93 break;
94 case BR_STATE_LISTENING: /* fall-through */
95 case BR_STATE_LEARNING:
96 spms_state = MLXSW_REG_SPMS_STATE_LEARNING;
97 break;
98 case BR_STATE_BLOCKING:
99 spms_state = MLXSW_REG_SPMS_STATE_DISCARDING;
100 break;
101 default:
102 BUG();
103 }
104
105 spms_pl = kmalloc(MLXSW_REG_SPMS_LEN, GFP_KERNEL);
106 if (!spms_pl)
107 return -ENOMEM;
108 mlxsw_reg_spms_pack(spms_pl, mlxsw_sp_port->local_port);
109 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID)
110 mlxsw_reg_spms_vid_pack(spms_pl, vid, spms_state);
111
112 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spms), spms_pl);
113 kfree(spms_pl);
114 return err;
115 }
116
mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port * mlxsw_sp_port,struct switchdev_trans * trans,u8 state)117 static int mlxsw_sp_port_attr_stp_state_set(struct mlxsw_sp_port *mlxsw_sp_port,
118 struct switchdev_trans *trans,
119 u8 state)
120 {
121 if (switchdev_trans_ph_prepare(trans))
122 return 0;
123
124 mlxsw_sp_port->stp_state = state;
125 return mlxsw_sp_port_stp_state_set(mlxsw_sp_port, state);
126 }
127
__mlxsw_sp_port_flood_set(struct mlxsw_sp_port * mlxsw_sp_port,u16 fid_begin,u16 fid_end,bool set,bool only_uc)128 static int __mlxsw_sp_port_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
129 u16 fid_begin, u16 fid_end, bool set,
130 bool only_uc)
131 {
132 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
133 u16 range = fid_end - fid_begin + 1;
134 char *sftr_pl;
135 int err;
136
137 sftr_pl = kmalloc(MLXSW_REG_SFTR_LEN, GFP_KERNEL);
138 if (!sftr_pl)
139 return -ENOMEM;
140
141 mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_UC, fid_begin,
142 MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST, range,
143 mlxsw_sp_port->local_port, set);
144 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
145 if (err)
146 goto buffer_out;
147
148 /* Flooding control allows one to decide whether a given port will
149 * flood unicast traffic for which there is no FDB entry.
150 */
151 if (only_uc)
152 goto buffer_out;
153
154 mlxsw_reg_sftr_pack(sftr_pl, MLXSW_SP_FLOOD_TABLE_BM, fid_begin,
155 MLXSW_REG_SFGC_TABLE_TYPE_FID_OFFEST, range,
156 mlxsw_sp_port->local_port, set);
157 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sftr), sftr_pl);
158
159 buffer_out:
160 kfree(sftr_pl);
161 return err;
162 }
163
mlxsw_sp_port_uc_flood_set(struct mlxsw_sp_port * mlxsw_sp_port,bool set)164 static int mlxsw_sp_port_uc_flood_set(struct mlxsw_sp_port *mlxsw_sp_port,
165 bool set)
166 {
167 struct net_device *dev = mlxsw_sp_port->dev;
168 u16 vid, last_visited_vid;
169 int err;
170
171 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
172 err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid, vid, set,
173 true);
174 if (err) {
175 last_visited_vid = vid;
176 goto err_port_flood_set;
177 }
178 }
179
180 return 0;
181
182 err_port_flood_set:
183 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, last_visited_vid)
184 __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid, vid, !set, true);
185 netdev_err(dev, "Failed to configure unicast flooding\n");
186 return err;
187 }
188
mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port * mlxsw_sp_port,struct switchdev_trans * trans,unsigned long brport_flags)189 static int mlxsw_sp_port_attr_br_flags_set(struct mlxsw_sp_port *mlxsw_sp_port,
190 struct switchdev_trans *trans,
191 unsigned long brport_flags)
192 {
193 unsigned long uc_flood = mlxsw_sp_port->uc_flood ? BR_FLOOD : 0;
194 bool set;
195 int err;
196
197 if (switchdev_trans_ph_prepare(trans))
198 return 0;
199
200 if ((uc_flood ^ brport_flags) & BR_FLOOD) {
201 set = mlxsw_sp_port->uc_flood ? false : true;
202 err = mlxsw_sp_port_uc_flood_set(mlxsw_sp_port, set);
203 if (err)
204 return err;
205 }
206
207 mlxsw_sp_port->uc_flood = brport_flags & BR_FLOOD ? 1 : 0;
208 mlxsw_sp_port->learning = brport_flags & BR_LEARNING ? 1 : 0;
209 mlxsw_sp_port->learning_sync = brport_flags & BR_LEARNING_SYNC ? 1 : 0;
210
211 return 0;
212 }
213
mlxsw_sp_ageing_set(struct mlxsw_sp * mlxsw_sp,u32 ageing_time)214 static int mlxsw_sp_ageing_set(struct mlxsw_sp *mlxsw_sp, u32 ageing_time)
215 {
216 char sfdat_pl[MLXSW_REG_SFDAT_LEN];
217 int err;
218
219 mlxsw_reg_sfdat_pack(sfdat_pl, ageing_time);
220 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfdat), sfdat_pl);
221 if (err)
222 return err;
223 mlxsw_sp->ageing_time = ageing_time;
224 return 0;
225 }
226
mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port * mlxsw_sp_port,struct switchdev_trans * trans,unsigned long ageing_clock_t)227 static int mlxsw_sp_port_attr_br_ageing_set(struct mlxsw_sp_port *mlxsw_sp_port,
228 struct switchdev_trans *trans,
229 unsigned long ageing_clock_t)
230 {
231 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
232 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
233 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
234
235 if (switchdev_trans_ph_prepare(trans)) {
236 if (ageing_time < MLXSW_SP_MIN_AGEING_TIME ||
237 ageing_time > MLXSW_SP_MAX_AGEING_TIME)
238 return -ERANGE;
239 else
240 return 0;
241 }
242
243 return mlxsw_sp_ageing_set(mlxsw_sp, ageing_time);
244 }
245
mlxsw_sp_port_attr_set(struct net_device * dev,const struct switchdev_attr * attr,struct switchdev_trans * trans)246 static int mlxsw_sp_port_attr_set(struct net_device *dev,
247 const struct switchdev_attr *attr,
248 struct switchdev_trans *trans)
249 {
250 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
251 int err = 0;
252
253 switch (attr->id) {
254 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
255 err = mlxsw_sp_port_attr_stp_state_set(mlxsw_sp_port, trans,
256 attr->u.stp_state);
257 break;
258 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
259 err = mlxsw_sp_port_attr_br_flags_set(mlxsw_sp_port, trans,
260 attr->u.brport_flags);
261 break;
262 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
263 err = mlxsw_sp_port_attr_br_ageing_set(mlxsw_sp_port, trans,
264 attr->u.ageing_time);
265 break;
266 default:
267 err = -EOPNOTSUPP;
268 break;
269 }
270
271 return err;
272 }
273
mlxsw_sp_port_pvid_set(struct mlxsw_sp_port * mlxsw_sp_port,u16 vid)274 static int mlxsw_sp_port_pvid_set(struct mlxsw_sp_port *mlxsw_sp_port, u16 vid)
275 {
276 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
277 char spvid_pl[MLXSW_REG_SPVID_LEN];
278
279 mlxsw_reg_spvid_pack(spvid_pl, mlxsw_sp_port->local_port, vid);
280 return mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(spvid), spvid_pl);
281 }
282
mlxsw_sp_fid_create(struct mlxsw_sp * mlxsw_sp,u16 fid)283 static int mlxsw_sp_fid_create(struct mlxsw_sp *mlxsw_sp, u16 fid)
284 {
285 char sfmr_pl[MLXSW_REG_SFMR_LEN];
286 int err;
287
288 mlxsw_reg_sfmr_pack(sfmr_pl, MLXSW_REG_SFMR_OP_CREATE_FID, fid, fid);
289 err = mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl);
290
291 if (err)
292 return err;
293
294 set_bit(fid, mlxsw_sp->active_fids);
295 return 0;
296 }
297
mlxsw_sp_fid_destroy(struct mlxsw_sp * mlxsw_sp,u16 fid)298 static void mlxsw_sp_fid_destroy(struct mlxsw_sp *mlxsw_sp, u16 fid)
299 {
300 char sfmr_pl[MLXSW_REG_SFMR_LEN];
301
302 clear_bit(fid, mlxsw_sp->active_fids);
303
304 mlxsw_reg_sfmr_pack(sfmr_pl, MLXSW_REG_SFMR_OP_DESTROY_FID,
305 fid, fid);
306 mlxsw_reg_write(mlxsw_sp->core, MLXSW_REG(sfmr), sfmr_pl);
307 }
308
mlxsw_sp_port_fid_map(struct mlxsw_sp_port * mlxsw_sp_port,u16 fid)309 static int mlxsw_sp_port_fid_map(struct mlxsw_sp_port *mlxsw_sp_port, u16 fid)
310 {
311 enum mlxsw_reg_svfa_mt mt;
312
313 if (mlxsw_sp_port->nr_vfids)
314 mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
315 else
316 mt = MLXSW_REG_SVFA_MT_VID_TO_FID;
317
318 return mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, true, fid, fid);
319 }
320
mlxsw_sp_port_fid_unmap(struct mlxsw_sp_port * mlxsw_sp_port,u16 fid)321 static int mlxsw_sp_port_fid_unmap(struct mlxsw_sp_port *mlxsw_sp_port, u16 fid)
322 {
323 enum mlxsw_reg_svfa_mt mt;
324
325 if (!mlxsw_sp_port->nr_vfids)
326 return 0;
327
328 mt = MLXSW_REG_SVFA_MT_PORT_VID_TO_FID;
329 return mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt, false, fid, fid);
330 }
331
mlxsw_sp_port_add_vids(struct net_device * dev,u16 vid_begin,u16 vid_end)332 static int mlxsw_sp_port_add_vids(struct net_device *dev, u16 vid_begin,
333 u16 vid_end)
334 {
335 u16 vid;
336 int err;
337
338 for (vid = vid_begin; vid <= vid_end; vid++) {
339 err = mlxsw_sp_port_add_vid(dev, 0, vid);
340 if (err)
341 goto err_port_add_vid;
342 }
343 return 0;
344
345 err_port_add_vid:
346 for (vid--; vid >= vid_begin; vid--)
347 mlxsw_sp_port_kill_vid(dev, 0, vid);
348 return err;
349 }
350
__mlxsw_sp_port_vlans_add(struct mlxsw_sp_port * mlxsw_sp_port,u16 vid_begin,u16 vid_end,bool flag_untagged,bool flag_pvid)351 static int __mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
352 u16 vid_begin, u16 vid_end,
353 bool flag_untagged, bool flag_pvid)
354 {
355 struct mlxsw_sp *mlxsw_sp = mlxsw_sp_port->mlxsw_sp;
356 struct net_device *dev = mlxsw_sp_port->dev;
357 enum mlxsw_reg_svfa_mt mt;
358 u16 vid, vid_e;
359 int err;
360
361 /* In case this is invoked with BRIDGE_FLAGS_SELF and port is
362 * not bridged, then packets ingressing through the port with
363 * the specified VIDs will be directed to CPU.
364 */
365 if (!mlxsw_sp_port->bridged)
366 return mlxsw_sp_port_add_vids(dev, vid_begin, vid_end);
367
368 for (vid = vid_begin; vid <= vid_end; vid++) {
369 if (!test_bit(vid, mlxsw_sp->active_fids)) {
370 err = mlxsw_sp_fid_create(mlxsw_sp, vid);
371 if (err) {
372 netdev_err(dev, "Failed to create FID=%d\n",
373 vid);
374 return err;
375 }
376
377 /* When creating a FID, we set a VID to FID mapping
378 * regardless of the port's mode.
379 */
380 mt = MLXSW_REG_SVFA_MT_VID_TO_FID;
381 err = mlxsw_sp_port_vid_to_fid_set(mlxsw_sp_port, mt,
382 true, vid, vid);
383 if (err) {
384 netdev_err(dev, "Failed to create FID=VID=%d mapping\n",
385 vid);
386 return err;
387 }
388 }
389
390 /* Set FID mapping according to port's mode */
391 err = mlxsw_sp_port_fid_map(mlxsw_sp_port, vid);
392 if (err) {
393 netdev_err(dev, "Failed to map FID=%d", vid);
394 return err;
395 }
396 }
397
398 err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid_begin, vid_end,
399 true, false);
400 if (err) {
401 netdev_err(dev, "Failed to configure flooding\n");
402 return err;
403 }
404
405 for (vid = vid_begin; vid <= vid_end;
406 vid += MLXSW_REG_SPVM_REC_MAX_COUNT) {
407 vid_e = min((u16) (vid + MLXSW_REG_SPVM_REC_MAX_COUNT - 1),
408 vid_end);
409
410 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid_e, true,
411 flag_untagged);
412 if (err) {
413 netdev_err(mlxsw_sp_port->dev, "Unable to add VIDs %d-%d\n",
414 vid, vid_e);
415 return err;
416 }
417 }
418
419 vid = vid_begin;
420 if (flag_pvid && mlxsw_sp_port->pvid != vid) {
421 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port, vid);
422 if (err) {
423 netdev_err(mlxsw_sp_port->dev, "Unable to add PVID %d\n",
424 vid);
425 return err;
426 }
427 mlxsw_sp_port->pvid = vid;
428 }
429
430 /* Changing activity bits only if HW operation succeded */
431 for (vid = vid_begin; vid <= vid_end; vid++)
432 set_bit(vid, mlxsw_sp_port->active_vlans);
433
434 return mlxsw_sp_port_stp_state_set(mlxsw_sp_port,
435 mlxsw_sp_port->stp_state);
436 }
437
mlxsw_sp_port_vlans_add(struct mlxsw_sp_port * mlxsw_sp_port,const struct switchdev_obj_port_vlan * vlan,struct switchdev_trans * trans)438 static int mlxsw_sp_port_vlans_add(struct mlxsw_sp_port *mlxsw_sp_port,
439 const struct switchdev_obj_port_vlan *vlan,
440 struct switchdev_trans *trans)
441 {
442 bool untagged_flag = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
443 bool pvid_flag = vlan->flags & BRIDGE_VLAN_INFO_PVID;
444
445 if (switchdev_trans_ph_prepare(trans))
446 return 0;
447
448 return __mlxsw_sp_port_vlans_add(mlxsw_sp_port,
449 vlan->vid_begin, vlan->vid_end,
450 untagged_flag, pvid_flag);
451 }
452
mlxsw_sp_port_fdb_op(struct mlxsw_sp_port * mlxsw_sp_port,const char * mac,u16 vid,bool adding,bool dynamic)453 static int mlxsw_sp_port_fdb_op(struct mlxsw_sp_port *mlxsw_sp_port,
454 const char *mac, u16 vid, bool adding,
455 bool dynamic)
456 {
457 enum mlxsw_reg_sfd_rec_policy policy;
458 enum mlxsw_reg_sfd_op op;
459 char *sfd_pl;
460 int err;
461
462 if (!vid)
463 vid = mlxsw_sp_port->pvid;
464
465 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
466 if (!sfd_pl)
467 return -ENOMEM;
468
469 policy = dynamic ? MLXSW_REG_SFD_REC_POLICY_DYNAMIC_ENTRY_INGRESS :
470 MLXSW_REG_SFD_REC_POLICY_STATIC_ENTRY;
471 op = adding ? MLXSW_REG_SFD_OP_WRITE_EDIT :
472 MLXSW_REG_SFD_OP_WRITE_REMOVE;
473 mlxsw_reg_sfd_pack(sfd_pl, op, 0);
474 mlxsw_reg_sfd_uc_pack(sfd_pl, 0, policy,
475 mac, vid, MLXSW_REG_SFD_REC_ACTION_NOP,
476 mlxsw_sp_port->local_port);
477 err = mlxsw_reg_write(mlxsw_sp_port->mlxsw_sp->core, MLXSW_REG(sfd),
478 sfd_pl);
479 kfree(sfd_pl);
480
481 return err;
482 }
483
484 static int
mlxsw_sp_port_fdb_static_add(struct mlxsw_sp_port * mlxsw_sp_port,const struct switchdev_obj_port_fdb * fdb,struct switchdev_trans * trans)485 mlxsw_sp_port_fdb_static_add(struct mlxsw_sp_port *mlxsw_sp_port,
486 const struct switchdev_obj_port_fdb *fdb,
487 struct switchdev_trans *trans)
488 {
489 if (switchdev_trans_ph_prepare(trans))
490 return 0;
491
492 return mlxsw_sp_port_fdb_op(mlxsw_sp_port, fdb->addr, fdb->vid,
493 true, false);
494 }
495
mlxsw_sp_port_obj_add(struct net_device * dev,const struct switchdev_obj * obj,struct switchdev_trans * trans)496 static int mlxsw_sp_port_obj_add(struct net_device *dev,
497 const struct switchdev_obj *obj,
498 struct switchdev_trans *trans)
499 {
500 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
501 int err = 0;
502
503 switch (obj->id) {
504 case SWITCHDEV_OBJ_ID_PORT_VLAN:
505 err = mlxsw_sp_port_vlans_add(mlxsw_sp_port,
506 SWITCHDEV_OBJ_PORT_VLAN(obj),
507 trans);
508 break;
509 case SWITCHDEV_OBJ_ID_PORT_FDB:
510 err = mlxsw_sp_port_fdb_static_add(mlxsw_sp_port,
511 SWITCHDEV_OBJ_PORT_FDB(obj),
512 trans);
513 break;
514 default:
515 err = -EOPNOTSUPP;
516 break;
517 }
518
519 return err;
520 }
521
mlxsw_sp_port_kill_vids(struct net_device * dev,u16 vid_begin,u16 vid_end)522 static int mlxsw_sp_port_kill_vids(struct net_device *dev, u16 vid_begin,
523 u16 vid_end)
524 {
525 u16 vid;
526 int err;
527
528 for (vid = vid_begin; vid <= vid_end; vid++) {
529 err = mlxsw_sp_port_kill_vid(dev, 0, vid);
530 if (err)
531 return err;
532 }
533
534 return 0;
535 }
536
__mlxsw_sp_port_vlans_del(struct mlxsw_sp_port * mlxsw_sp_port,u16 vid_begin,u16 vid_end,bool init)537 static int __mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
538 u16 vid_begin, u16 vid_end, bool init)
539 {
540 struct net_device *dev = mlxsw_sp_port->dev;
541 u16 vid, vid_e;
542 int err;
543
544 /* In case this is invoked with BRIDGE_FLAGS_SELF and port is
545 * not bridged, then prevent packets ingressing through the
546 * port with the specified VIDs from being trapped to CPU.
547 */
548 if (!init && !mlxsw_sp_port->bridged)
549 return mlxsw_sp_port_kill_vids(dev, vid_begin, vid_end);
550
551 for (vid = vid_begin; vid <= vid_end;
552 vid += MLXSW_REG_SPVM_REC_MAX_COUNT) {
553 vid_e = min((u16) (vid + MLXSW_REG_SPVM_REC_MAX_COUNT - 1),
554 vid_end);
555 err = mlxsw_sp_port_vlan_set(mlxsw_sp_port, vid, vid_e, false,
556 false);
557 if (err) {
558 netdev_err(mlxsw_sp_port->dev, "Unable to del VIDs %d-%d\n",
559 vid, vid_e);
560 return err;
561 }
562 }
563
564 if ((mlxsw_sp_port->pvid >= vid_begin) &&
565 (mlxsw_sp_port->pvid <= vid_end)) {
566 /* Default VLAN is always 1 */
567 mlxsw_sp_port->pvid = 1;
568 err = mlxsw_sp_port_pvid_set(mlxsw_sp_port,
569 mlxsw_sp_port->pvid);
570 if (err) {
571 netdev_err(mlxsw_sp_port->dev, "Unable to del PVID %d\n",
572 vid);
573 return err;
574 }
575 }
576
577 if (init)
578 goto out;
579
580 err = __mlxsw_sp_port_flood_set(mlxsw_sp_port, vid_begin, vid_end,
581 false, false);
582 if (err) {
583 netdev_err(dev, "Failed to clear flooding\n");
584 return err;
585 }
586
587 for (vid = vid_begin; vid <= vid_end; vid++) {
588 /* Remove FID mapping in case of Virtual mode */
589 err = mlxsw_sp_port_fid_unmap(mlxsw_sp_port, vid);
590 if (err) {
591 netdev_err(dev, "Failed to unmap FID=%d", vid);
592 return err;
593 }
594 }
595
596 out:
597 /* Changing activity bits only if HW operation succeded */
598 for (vid = vid_begin; vid <= vid_end; vid++)
599 clear_bit(vid, mlxsw_sp_port->active_vlans);
600
601 return 0;
602 }
603
mlxsw_sp_port_vlans_del(struct mlxsw_sp_port * mlxsw_sp_port,const struct switchdev_obj_port_vlan * vlan)604 static int mlxsw_sp_port_vlans_del(struct mlxsw_sp_port *mlxsw_sp_port,
605 const struct switchdev_obj_port_vlan *vlan)
606 {
607 return __mlxsw_sp_port_vlans_del(mlxsw_sp_port,
608 vlan->vid_begin, vlan->vid_end, false);
609 }
610
611 static int
mlxsw_sp_port_fdb_static_del(struct mlxsw_sp_port * mlxsw_sp_port,const struct switchdev_obj_port_fdb * fdb)612 mlxsw_sp_port_fdb_static_del(struct mlxsw_sp_port *mlxsw_sp_port,
613 const struct switchdev_obj_port_fdb *fdb)
614 {
615 return mlxsw_sp_port_fdb_op(mlxsw_sp_port, fdb->addr, fdb->vid,
616 false, false);
617 }
618
mlxsw_sp_port_obj_del(struct net_device * dev,const struct switchdev_obj * obj)619 static int mlxsw_sp_port_obj_del(struct net_device *dev,
620 const struct switchdev_obj *obj)
621 {
622 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
623 int err = 0;
624
625 switch (obj->id) {
626 case SWITCHDEV_OBJ_ID_PORT_VLAN:
627 err = mlxsw_sp_port_vlans_del(mlxsw_sp_port,
628 SWITCHDEV_OBJ_PORT_VLAN(obj));
629 break;
630 case SWITCHDEV_OBJ_ID_PORT_FDB:
631 err = mlxsw_sp_port_fdb_static_del(mlxsw_sp_port,
632 SWITCHDEV_OBJ_PORT_FDB(obj));
633 break;
634 default:
635 err = -EOPNOTSUPP;
636 break;
637 }
638
639 return err;
640 }
641
mlxsw_sp_port_fdb_dump(struct mlxsw_sp_port * mlxsw_sp_port,struct switchdev_obj_port_fdb * fdb,switchdev_obj_dump_cb_t * cb)642 static int mlxsw_sp_port_fdb_dump(struct mlxsw_sp_port *mlxsw_sp_port,
643 struct switchdev_obj_port_fdb *fdb,
644 switchdev_obj_dump_cb_t *cb)
645 {
646 char *sfd_pl;
647 char mac[ETH_ALEN];
648 u16 vid;
649 u8 local_port;
650 u8 num_rec;
651 int stored_err = 0;
652 int i;
653 int err;
654
655 sfd_pl = kmalloc(MLXSW_REG_SFD_LEN, GFP_KERNEL);
656 if (!sfd_pl)
657 return -ENOMEM;
658
659 mlxsw_reg_sfd_pack(sfd_pl, MLXSW_REG_SFD_OP_QUERY_DUMP, 0);
660 do {
661 mlxsw_reg_sfd_num_rec_set(sfd_pl, MLXSW_REG_SFD_REC_MAX_COUNT);
662 err = mlxsw_reg_query(mlxsw_sp_port->mlxsw_sp->core,
663 MLXSW_REG(sfd), sfd_pl);
664 if (err)
665 goto out;
666
667 num_rec = mlxsw_reg_sfd_num_rec_get(sfd_pl);
668
669 /* Even in case of error, we have to run the dump to the end
670 * so the session in firmware is finished.
671 */
672 if (stored_err)
673 continue;
674
675 for (i = 0; i < num_rec; i++) {
676 switch (mlxsw_reg_sfd_rec_type_get(sfd_pl, i)) {
677 case MLXSW_REG_SFD_REC_TYPE_UNICAST:
678 mlxsw_reg_sfd_uc_unpack(sfd_pl, i, mac, &vid,
679 &local_port);
680 if (local_port == mlxsw_sp_port->local_port) {
681 ether_addr_copy(fdb->addr, mac);
682 fdb->ndm_state = NUD_REACHABLE;
683 fdb->vid = vid;
684 err = cb(&fdb->obj);
685 if (err)
686 stored_err = err;
687 }
688 }
689 }
690 } while (num_rec == MLXSW_REG_SFD_REC_MAX_COUNT);
691
692 out:
693 kfree(sfd_pl);
694 return stored_err ? stored_err : err;
695 }
696
mlxsw_sp_port_vlan_dump(struct mlxsw_sp_port * mlxsw_sp_port,struct switchdev_obj_port_vlan * vlan,switchdev_obj_dump_cb_t * cb)697 static int mlxsw_sp_port_vlan_dump(struct mlxsw_sp_port *mlxsw_sp_port,
698 struct switchdev_obj_port_vlan *vlan,
699 switchdev_obj_dump_cb_t *cb)
700 {
701 u16 vid;
702 int err = 0;
703
704 for_each_set_bit(vid, mlxsw_sp_port->active_vlans, VLAN_N_VID) {
705 vlan->flags = 0;
706 if (vid == mlxsw_sp_port->pvid)
707 vlan->flags |= BRIDGE_VLAN_INFO_PVID;
708 vlan->vid_begin = vid;
709 vlan->vid_end = vid;
710 err = cb(&vlan->obj);
711 if (err)
712 break;
713 }
714 return err;
715 }
716
mlxsw_sp_port_obj_dump(struct net_device * dev,struct switchdev_obj * obj,switchdev_obj_dump_cb_t * cb)717 static int mlxsw_sp_port_obj_dump(struct net_device *dev,
718 struct switchdev_obj *obj,
719 switchdev_obj_dump_cb_t *cb)
720 {
721 struct mlxsw_sp_port *mlxsw_sp_port = netdev_priv(dev);
722 int err = 0;
723
724 switch (obj->id) {
725 case SWITCHDEV_OBJ_ID_PORT_VLAN:
726 err = mlxsw_sp_port_vlan_dump(mlxsw_sp_port,
727 SWITCHDEV_OBJ_PORT_VLAN(obj), cb);
728 break;
729 case SWITCHDEV_OBJ_ID_PORT_FDB:
730 err = mlxsw_sp_port_fdb_dump(mlxsw_sp_port,
731 SWITCHDEV_OBJ_PORT_FDB(obj), cb);
732 break;
733 default:
734 err = -EOPNOTSUPP;
735 break;
736 }
737
738 return err;
739 }
740
741 static const struct switchdev_ops mlxsw_sp_port_switchdev_ops = {
742 .switchdev_port_attr_get = mlxsw_sp_port_attr_get,
743 .switchdev_port_attr_set = mlxsw_sp_port_attr_set,
744 .switchdev_port_obj_add = mlxsw_sp_port_obj_add,
745 .switchdev_port_obj_del = mlxsw_sp_port_obj_del,
746 .switchdev_port_obj_dump = mlxsw_sp_port_obj_dump,
747 };
748
mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp * mlxsw_sp,char * sfn_pl,int rec_index,bool adding)749 static void mlxsw_sp_fdb_notify_mac_process(struct mlxsw_sp *mlxsw_sp,
750 char *sfn_pl, int rec_index,
751 bool adding)
752 {
753 struct mlxsw_sp_port *mlxsw_sp_port;
754 char mac[ETH_ALEN];
755 u8 local_port;
756 u16 vid;
757 int err;
758
759 mlxsw_reg_sfn_mac_unpack(sfn_pl, rec_index, mac, &vid, &local_port);
760 mlxsw_sp_port = mlxsw_sp->ports[local_port];
761 if (!mlxsw_sp_port) {
762 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Incorrect local port in FDB notification\n");
763 return;
764 }
765
766 err = mlxsw_sp_port_fdb_op(mlxsw_sp_port, mac, vid,
767 adding && mlxsw_sp_port->learning, true);
768 if (err) {
769 if (net_ratelimit())
770 netdev_err(mlxsw_sp_port->dev, "Failed to set FDB entry\n");
771 return;
772 }
773
774 if (mlxsw_sp_port->learning && mlxsw_sp_port->learning_sync) {
775 struct switchdev_notifier_fdb_info info;
776 unsigned long notifier_type;
777
778 info.addr = mac;
779 info.vid = vid;
780 notifier_type = adding ? SWITCHDEV_FDB_ADD : SWITCHDEV_FDB_DEL;
781 call_switchdev_notifiers(notifier_type, mlxsw_sp_port->dev,
782 &info.info);
783 }
784 }
785
mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp * mlxsw_sp,char * sfn_pl,int rec_index)786 static void mlxsw_sp_fdb_notify_rec_process(struct mlxsw_sp *mlxsw_sp,
787 char *sfn_pl, int rec_index)
788 {
789 switch (mlxsw_reg_sfn_rec_type_get(sfn_pl, rec_index)) {
790 case MLXSW_REG_SFN_REC_TYPE_LEARNED_MAC:
791 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
792 rec_index, true);
793 break;
794 case MLXSW_REG_SFN_REC_TYPE_AGED_OUT_MAC:
795 mlxsw_sp_fdb_notify_mac_process(mlxsw_sp, sfn_pl,
796 rec_index, false);
797 break;
798 }
799 }
800
mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp * mlxsw_sp)801 static void mlxsw_sp_fdb_notify_work_schedule(struct mlxsw_sp *mlxsw_sp)
802 {
803 schedule_delayed_work(&mlxsw_sp->fdb_notify.dw,
804 msecs_to_jiffies(mlxsw_sp->fdb_notify.interval));
805 }
806
mlxsw_sp_fdb_notify_work(struct work_struct * work)807 static void mlxsw_sp_fdb_notify_work(struct work_struct *work)
808 {
809 struct mlxsw_sp *mlxsw_sp;
810 char *sfn_pl;
811 u8 num_rec;
812 int i;
813 int err;
814
815 sfn_pl = kmalloc(MLXSW_REG_SFN_LEN, GFP_KERNEL);
816 if (!sfn_pl)
817 return;
818
819 mlxsw_sp = container_of(work, struct mlxsw_sp, fdb_notify.dw.work);
820
821 rtnl_lock();
822 do {
823 mlxsw_reg_sfn_pack(sfn_pl);
824 err = mlxsw_reg_query(mlxsw_sp->core, MLXSW_REG(sfn), sfn_pl);
825 if (err) {
826 dev_err_ratelimited(mlxsw_sp->bus_info->dev, "Failed to get FDB notifications\n");
827 break;
828 }
829 num_rec = mlxsw_reg_sfn_num_rec_get(sfn_pl);
830 for (i = 0; i < num_rec; i++)
831 mlxsw_sp_fdb_notify_rec_process(mlxsw_sp, sfn_pl, i);
832
833 } while (num_rec);
834 rtnl_unlock();
835
836 kfree(sfn_pl);
837 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
838 }
839
mlxsw_sp_fdb_init(struct mlxsw_sp * mlxsw_sp)840 static int mlxsw_sp_fdb_init(struct mlxsw_sp *mlxsw_sp)
841 {
842 int err;
843
844 err = mlxsw_sp_ageing_set(mlxsw_sp, MLXSW_SP_DEFAULT_AGEING_TIME);
845 if (err) {
846 dev_err(mlxsw_sp->bus_info->dev, "Failed to set default ageing time\n");
847 return err;
848 }
849 INIT_DELAYED_WORK(&mlxsw_sp->fdb_notify.dw, mlxsw_sp_fdb_notify_work);
850 mlxsw_sp->fdb_notify.interval = MLXSW_SP_DEFAULT_LEARNING_INTERVAL;
851 mlxsw_sp_fdb_notify_work_schedule(mlxsw_sp);
852 return 0;
853 }
854
mlxsw_sp_fdb_fini(struct mlxsw_sp * mlxsw_sp)855 static void mlxsw_sp_fdb_fini(struct mlxsw_sp *mlxsw_sp)
856 {
857 cancel_delayed_work_sync(&mlxsw_sp->fdb_notify.dw);
858 }
859
mlxsw_sp_fids_fini(struct mlxsw_sp * mlxsw_sp)860 static void mlxsw_sp_fids_fini(struct mlxsw_sp *mlxsw_sp)
861 {
862 u16 fid;
863
864 for_each_set_bit(fid, mlxsw_sp->active_fids, VLAN_N_VID)
865 mlxsw_sp_fid_destroy(mlxsw_sp, fid);
866 }
867
mlxsw_sp_switchdev_init(struct mlxsw_sp * mlxsw_sp)868 int mlxsw_sp_switchdev_init(struct mlxsw_sp *mlxsw_sp)
869 {
870 return mlxsw_sp_fdb_init(mlxsw_sp);
871 }
872
mlxsw_sp_switchdev_fini(struct mlxsw_sp * mlxsw_sp)873 void mlxsw_sp_switchdev_fini(struct mlxsw_sp *mlxsw_sp)
874 {
875 mlxsw_sp_fdb_fini(mlxsw_sp);
876 mlxsw_sp_fids_fini(mlxsw_sp);
877 }
878
mlxsw_sp_port_vlan_init(struct mlxsw_sp_port * mlxsw_sp_port)879 int mlxsw_sp_port_vlan_init(struct mlxsw_sp_port *mlxsw_sp_port)
880 {
881 struct net_device *dev = mlxsw_sp_port->dev;
882 int err;
883
884 /* Allow only untagged packets to ingress and tag them internally
885 * with VID 1.
886 */
887 mlxsw_sp_port->pvid = 1;
888 err = __mlxsw_sp_port_vlans_del(mlxsw_sp_port, 0, VLAN_N_VID, true);
889 if (err) {
890 netdev_err(dev, "Unable to init VLANs\n");
891 return err;
892 }
893
894 /* Add implicit VLAN interface in the device, so that untagged
895 * packets will be classified to the default vFID.
896 */
897 err = mlxsw_sp_port_add_vid(dev, 0, 1);
898 if (err)
899 netdev_err(dev, "Failed to configure default vFID\n");
900
901 return err;
902 }
903
mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port * mlxsw_sp_port)904 void mlxsw_sp_port_switchdev_init(struct mlxsw_sp_port *mlxsw_sp_port)
905 {
906 mlxsw_sp_port->dev->switchdev_ops = &mlxsw_sp_port_switchdev_ops;
907 }
908
mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port * mlxsw_sp_port)909 void mlxsw_sp_port_switchdev_fini(struct mlxsw_sp_port *mlxsw_sp_port)
910 {
911 }
912