blob: e271b6225b726b9d556a65f659c0906b18270298 [file] [log] [blame]
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001// SPDX-License-Identifier: (GPL-2.0 OR MIT)
2/* Microsemi Ocelot Switch driver
3 *
Vladimir Olteanf59fd9c2021-01-15 04:11:20 +02004 * This contains glue logic between the switchdev driver operations and the
5 * mscc_ocelot_switch_lib.
6 *
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03007 * Copyright (c) 2017, 2019 Microsemi Corporation
Vladimir Oltean3c9cfb52021-09-17 14:17:35 +03008 * Copyright 2020-2021 NXP
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03009 */
10
Vladimir Olteandeab6b12021-10-12 14:40:40 +030011#include <linux/dsa/ocelot.h>
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030012#include <linux/if_bridge.h>
Vladimir Olteane6e12df2021-08-15 04:47:48 +030013#include <linux/of_net.h>
14#include <linux/phy/phy.h>
Vladimir Oltean0e9bb4e2021-01-29 03:00:00 +020015#include <net/pkt_cls.h>
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030016#include "ocelot.h"
Vladimir Oltean3c836542020-06-20 18:43:45 +030017#include "ocelot_vcap.h"
Clément Léger753a0262021-12-09 16:49:11 +010018#include "ocelot_fdma.h"
Vladimir Oltean9c90eea2020-06-20 18:43:44 +030019
Vladimir Olteane6e12df2021-08-15 04:47:48 +030020#define OCELOT_MAC_QUIRKS OCELOT_QUIRK_QSGMII_PORTS_MUST_BE_UP
21
Vladimir Olteanf59fd9c2021-01-15 04:11:20 +020022static struct ocelot *devlink_port_to_ocelot(struct devlink_port *dlp)
23{
24 return devlink_priv(dlp->devlink);
25}
26
27static int devlink_port_to_port(struct devlink_port *dlp)
28{
29 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
30
31 return dlp - ocelot->devlink_ports;
32}
33
34static int ocelot_devlink_sb_pool_get(struct devlink *dl,
35 unsigned int sb_index, u16 pool_index,
36 struct devlink_sb_pool_info *pool_info)
37{
38 struct ocelot *ocelot = devlink_priv(dl);
39
40 return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
41}
42
43static int ocelot_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
44 u16 pool_index, u32 size,
45 enum devlink_sb_threshold_type threshold_type,
46 struct netlink_ext_ack *extack)
47{
48 struct ocelot *ocelot = devlink_priv(dl);
49
50 return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
51 threshold_type, extack);
52}
53
54static int ocelot_devlink_sb_port_pool_get(struct devlink_port *dlp,
55 unsigned int sb_index, u16 pool_index,
56 u32 *p_threshold)
57{
58 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
59 int port = devlink_port_to_port(dlp);
60
61 return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
62 p_threshold);
63}
64
65static int ocelot_devlink_sb_port_pool_set(struct devlink_port *dlp,
66 unsigned int sb_index, u16 pool_index,
67 u32 threshold,
68 struct netlink_ext_ack *extack)
69{
70 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
71 int port = devlink_port_to_port(dlp);
72
73 return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
74 threshold, extack);
75}
76
77static int
78ocelot_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
79 unsigned int sb_index, u16 tc_index,
80 enum devlink_sb_pool_type pool_type,
81 u16 *p_pool_index, u32 *p_threshold)
82{
83 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
84 int port = devlink_port_to_port(dlp);
85
86 return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
87 pool_type, p_pool_index,
88 p_threshold);
89}
90
91static int
92ocelot_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
93 unsigned int sb_index, u16 tc_index,
94 enum devlink_sb_pool_type pool_type,
95 u16 pool_index, u32 threshold,
96 struct netlink_ext_ack *extack)
97{
98 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
99 int port = devlink_port_to_port(dlp);
100
101 return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
102 pool_type, pool_index, threshold,
103 extack);
104}
105
106static int ocelot_devlink_sb_occ_snapshot(struct devlink *dl,
107 unsigned int sb_index)
108{
109 struct ocelot *ocelot = devlink_priv(dl);
110
111 return ocelot_sb_occ_snapshot(ocelot, sb_index);
112}
113
114static int ocelot_devlink_sb_occ_max_clear(struct devlink *dl,
115 unsigned int sb_index)
116{
117 struct ocelot *ocelot = devlink_priv(dl);
118
119 return ocelot_sb_occ_max_clear(ocelot, sb_index);
120}
121
122static int ocelot_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
123 unsigned int sb_index,
124 u16 pool_index, u32 *p_cur,
125 u32 *p_max)
126{
127 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
128 int port = devlink_port_to_port(dlp);
129
130 return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
131 p_cur, p_max);
132}
133
134static int
135ocelot_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
136 unsigned int sb_index, u16 tc_index,
137 enum devlink_sb_pool_type pool_type,
138 u32 *p_cur, u32 *p_max)
139{
140 struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
141 int port = devlink_port_to_port(dlp);
142
143 return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index,
144 tc_index, pool_type,
145 p_cur, p_max);
146}
147
Vladimir Oltean6c30384e2021-01-15 04:11:18 +0200148const struct devlink_ops ocelot_devlink_ops = {
Vladimir Olteanf59fd9c2021-01-15 04:11:20 +0200149 .sb_pool_get = ocelot_devlink_sb_pool_get,
150 .sb_pool_set = ocelot_devlink_sb_pool_set,
151 .sb_port_pool_get = ocelot_devlink_sb_port_pool_get,
152 .sb_port_pool_set = ocelot_devlink_sb_port_pool_set,
153 .sb_tc_pool_bind_get = ocelot_devlink_sb_tc_pool_bind_get,
154 .sb_tc_pool_bind_set = ocelot_devlink_sb_tc_pool_bind_set,
155 .sb_occ_snapshot = ocelot_devlink_sb_occ_snapshot,
156 .sb_occ_max_clear = ocelot_devlink_sb_occ_max_clear,
157 .sb_occ_port_pool_get = ocelot_devlink_sb_occ_port_pool_get,
158 .sb_occ_tc_port_bind_get = ocelot_devlink_sb_occ_tc_port_bind_get,
Vladimir Oltean6c30384e2021-01-15 04:11:18 +0200159};
160
161int ocelot_port_devlink_init(struct ocelot *ocelot, int port,
162 enum devlink_port_flavour flavour)
163{
164 struct devlink_port *dlp = &ocelot->devlink_ports[port];
165 int id_len = sizeof(ocelot->base_mac);
166 struct devlink *dl = ocelot->devlink;
167 struct devlink_port_attrs attrs = {};
168
Horatiu Vulturb5e33a12021-08-19 19:49:57 +0300169 memset(dlp, 0, sizeof(*dlp));
Vladimir Oltean6c30384e2021-01-15 04:11:18 +0200170 memcpy(attrs.switch_id.id, &ocelot->base_mac, id_len);
171 attrs.switch_id.id_len = id_len;
172 attrs.phys.port_number = port;
173 attrs.flavour = flavour;
174
175 devlink_port_attrs_set(dlp, &attrs);
176
177 return devlink_port_register(dl, dlp, port);
178}
179
180void ocelot_port_devlink_teardown(struct ocelot *ocelot, int port)
181{
182 struct devlink_port *dlp = &ocelot->devlink_ports[port];
183
184 devlink_port_unregister(dlp);
185}
186
187static struct devlink_port *ocelot_get_devlink_port(struct net_device *dev)
188{
189 struct ocelot_port_private *priv = netdev_priv(dev);
190 struct ocelot *ocelot = priv->port.ocelot;
191 int port = priv->chip_port;
192
193 return &ocelot->devlink_ports[port];
194}
195
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300196int ocelot_setup_tc_cls_flower(struct ocelot_port_private *priv,
197 struct flow_cls_offload *f,
198 bool ingress)
199{
200 struct ocelot *ocelot = priv->port.ocelot;
201 int port = priv->chip_port;
202
203 if (!ingress)
204 return -EOPNOTSUPP;
205
206 switch (f->command) {
207 case FLOW_CLS_REPLACE:
208 return ocelot_cls_flower_replace(ocelot, port, f, ingress);
209 case FLOW_CLS_DESTROY:
210 return ocelot_cls_flower_destroy(ocelot, port, f, ingress);
211 case FLOW_CLS_STATS:
212 return ocelot_cls_flower_stats(ocelot, port, f, ingress);
213 default:
214 return -EOPNOTSUPP;
215 }
216}
217
218static int ocelot_setup_tc_cls_matchall(struct ocelot_port_private *priv,
219 struct tc_cls_matchall_offload *f,
220 bool ingress)
221{
222 struct netlink_ext_ack *extack = f->common.extack;
223 struct ocelot *ocelot = priv->port.ocelot;
224 struct ocelot_policer pol = { 0 };
225 struct flow_action_entry *action;
226 int port = priv->chip_port;
227 int err;
228
229 if (!ingress) {
230 NL_SET_ERR_MSG_MOD(extack, "Only ingress is supported");
231 return -EOPNOTSUPP;
232 }
233
234 switch (f->command) {
235 case TC_CLSMATCHALL_REPLACE:
236 if (!flow_offload_has_one_action(&f->rule->action)) {
237 NL_SET_ERR_MSG_MOD(extack,
238 "Only one action is supported");
239 return -EOPNOTSUPP;
240 }
241
242 if (priv->tc.block_shared) {
243 NL_SET_ERR_MSG_MOD(extack,
244 "Rate limit is not supported on shared blocks");
245 return -EOPNOTSUPP;
246 }
247
248 action = &f->rule->action.entries[0];
249
250 if (action->id != FLOW_ACTION_POLICE) {
251 NL_SET_ERR_MSG_MOD(extack, "Unsupported action");
252 return -EOPNOTSUPP;
253 }
254
255 if (priv->tc.police_id && priv->tc.police_id != f->cookie) {
256 NL_SET_ERR_MSG_MOD(extack,
257 "Only one policer per port is supported");
258 return -EEXIST;
259 }
260
Baowen Zheng6a56e192021-03-12 15:08:30 +0100261 if (action->police.rate_pkt_ps) {
262 NL_SET_ERR_MSG_MOD(extack,
263 "QoS offload not support packets per second");
264 return -EOPNOTSUPP;
265 }
266
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300267 pol.rate = (u32)div_u64(action->police.rate_bytes_ps, 1000) * 8;
Po Liu5f035af2020-06-29 14:54:16 +0800268 pol.burst = action->police.burst;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300269
270 err = ocelot_port_policer_add(ocelot, port, &pol);
271 if (err) {
272 NL_SET_ERR_MSG_MOD(extack, "Could not add policer");
273 return err;
274 }
275
276 priv->tc.police_id = f->cookie;
277 priv->tc.offload_cnt++;
278 return 0;
279 case TC_CLSMATCHALL_DESTROY:
280 if (priv->tc.police_id != f->cookie)
281 return -ENOENT;
282
283 err = ocelot_port_policer_del(ocelot, port);
284 if (err) {
285 NL_SET_ERR_MSG_MOD(extack,
286 "Could not delete policer");
287 return err;
288 }
289 priv->tc.police_id = 0;
290 priv->tc.offload_cnt--;
291 return 0;
292 case TC_CLSMATCHALL_STATS:
293 default:
294 return -EOPNOTSUPP;
295 }
296}
297
298static int ocelot_setup_tc_block_cb(enum tc_setup_type type,
299 void *type_data,
300 void *cb_priv, bool ingress)
301{
302 struct ocelot_port_private *priv = cb_priv;
303
304 if (!tc_cls_can_offload_and_chain0(priv->dev, type_data))
305 return -EOPNOTSUPP;
306
307 switch (type) {
308 case TC_SETUP_CLSMATCHALL:
309 return ocelot_setup_tc_cls_matchall(priv, type_data, ingress);
310 case TC_SETUP_CLSFLOWER:
311 return ocelot_setup_tc_cls_flower(priv, type_data, ingress);
312 default:
313 return -EOPNOTSUPP;
314 }
315}
316
317static int ocelot_setup_tc_block_cb_ig(enum tc_setup_type type,
318 void *type_data,
319 void *cb_priv)
320{
321 return ocelot_setup_tc_block_cb(type, type_data,
322 cb_priv, true);
323}
324
325static int ocelot_setup_tc_block_cb_eg(enum tc_setup_type type,
326 void *type_data,
327 void *cb_priv)
328{
329 return ocelot_setup_tc_block_cb(type, type_data,
330 cb_priv, false);
331}
332
333static LIST_HEAD(ocelot_block_cb_list);
334
335static int ocelot_setup_tc_block(struct ocelot_port_private *priv,
336 struct flow_block_offload *f)
337{
338 struct flow_block_cb *block_cb;
339 flow_setup_cb_t *cb;
340
341 if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) {
342 cb = ocelot_setup_tc_block_cb_ig;
343 priv->tc.block_shared = f->block_shared;
344 } else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
345 cb = ocelot_setup_tc_block_cb_eg;
346 } else {
347 return -EOPNOTSUPP;
348 }
349
350 f->driver_block_list = &ocelot_block_cb_list;
351
352 switch (f->command) {
353 case FLOW_BLOCK_BIND:
354 if (flow_block_cb_is_busy(cb, priv, &ocelot_block_cb_list))
355 return -EBUSY;
356
357 block_cb = flow_block_cb_alloc(cb, priv, priv, NULL);
358 if (IS_ERR(block_cb))
359 return PTR_ERR(block_cb);
360
361 flow_block_cb_add(block_cb, f);
362 list_add_tail(&block_cb->driver_list, f->driver_block_list);
363 return 0;
364 case FLOW_BLOCK_UNBIND:
365 block_cb = flow_block_cb_lookup(f->block, cb, priv);
366 if (!block_cb)
367 return -ENOENT;
368
369 flow_block_cb_remove(block_cb, f);
370 list_del(&block_cb->driver_list);
371 return 0;
372 default:
373 return -EOPNOTSUPP;
374 }
375}
376
377static int ocelot_setup_tc(struct net_device *dev, enum tc_setup_type type,
378 void *type_data)
379{
380 struct ocelot_port_private *priv = netdev_priv(dev);
381
382 switch (type) {
383 case TC_SETUP_BLOCK:
384 return ocelot_setup_tc_block(priv, type_data);
385 default:
386 return -EOPNOTSUPP;
387 }
388 return 0;
389}
390
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300391static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
392 bool untagged)
393{
394 struct ocelot_port_private *priv = netdev_priv(dev);
395 struct ocelot_port *ocelot_port = &priv->port;
396 struct ocelot *ocelot = ocelot_port->ocelot;
397 int port = priv->chip_port;
398 int ret;
399
400 ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged);
401 if (ret)
402 return ret;
403
404 /* Add the port MAC address to with the right VLAN information */
405 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
406 ENTRYTYPE_LOCKED);
407
408 return 0;
409}
410
411static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
412{
413 struct ocelot_port_private *priv = netdev_priv(dev);
414 struct ocelot *ocelot = priv->port.ocelot;
415 int port = priv->chip_port;
416 int ret;
417
418 /* 8021q removes VID 0 on module unload for all interfaces
419 * with VLAN filtering feature. We need to keep it to receive
420 * untagged traffic.
421 */
Vladimir Olteanbfbab312021-10-20 20:58:51 +0300422 if (vid == OCELOT_VLAN_UNAWARE_PVID)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300423 return 0;
424
425 ret = ocelot_vlan_del(ocelot, port, vid);
426 if (ret)
427 return ret;
428
429 /* Del the port MAC address to with the right VLAN information */
430 ocelot_mact_forget(ocelot, dev->dev_addr, vid);
431
432 return 0;
433}
434
435static int ocelot_port_open(struct net_device *dev)
436{
437 struct ocelot_port_private *priv = netdev_priv(dev);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300438
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300439 phylink_start(priv->phylink);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300440
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300441 return 0;
442}
443
444static int ocelot_port_stop(struct net_device *dev)
445{
446 struct ocelot_port_private *priv = netdev_priv(dev);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300447
Vladimir Olteane6e12df2021-08-15 04:47:48 +0300448 phylink_stop(priv->phylink);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300449
450 return 0;
451}
452
Vladimir Oltean137ffbc2021-02-14 00:37:54 +0200453static netdev_tx_t ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300454{
455 struct ocelot_port_private *priv = netdev_priv(dev);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300456 struct ocelot_port *ocelot_port = &priv->port;
457 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300458 int port = priv->chip_port;
Vladimir Oltean137ffbc2021-02-14 00:37:54 +0200459 u32 rew_op = 0;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300460
Clément Léger753a0262021-12-09 16:49:11 +0100461 if (!static_branch_unlikely(&ocelot_fdma_enabled) &&
462 !ocelot_can_inject(ocelot, 0))
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300463 return NETDEV_TX_BUSY;
464
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300465 /* Check if timestamping is needed */
Vladimir Oltean137ffbc2021-02-14 00:37:54 +0200466 if (ocelot->ptp && (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
Yangbo Lu682eaad2021-04-27 12:22:02 +0800467 struct sk_buff *clone = NULL;
468
469 if (ocelot_port_txtstamp_request(ocelot, port, skb, &clone)) {
470 kfree_skb(skb);
471 return NETDEV_TX_OK;
472 }
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300473
Yangbo Lu39e53082021-04-27 12:22:03 +0800474 if (clone)
475 OCELOT_SKB_CB(skb)->clone = clone;
476
477 rew_op = ocelot_ptp_rew_op(skb);
Vladimir Olteane2f9a8f2020-09-23 14:24:20 +0300478 }
Vladimir Oltean9dda66a2020-09-18 04:07:23 +0300479
Clément Léger753a0262021-12-09 16:49:11 +0100480 if (static_branch_unlikely(&ocelot_fdma_enabled)) {
481 ocelot_fdma_inject_frame(ocelot, port, rew_op, skb, dev);
482 } else {
483 ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300484
Clément Léger753a0262021-12-09 16:49:11 +0100485 consume_skb(skb);
486 }
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300487
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300488 return NETDEV_TX_OK;
489}
490
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200491enum ocelot_action_type {
492 OCELOT_MACT_LEARN,
493 OCELOT_MACT_FORGET,
494};
495
496struct ocelot_mact_work_ctx {
497 struct work_struct work;
498 struct ocelot *ocelot;
499 enum ocelot_action_type type;
500 union {
501 /* OCELOT_MACT_LEARN */
502 struct {
503 unsigned char addr[ETH_ALEN];
504 u16 vid;
505 enum macaccess_entry_type entry_type;
506 int pgid;
507 } learn;
508 /* OCELOT_MACT_FORGET */
509 struct {
510 unsigned char addr[ETH_ALEN];
511 u16 vid;
512 } forget;
513 };
514};
515
516#define ocelot_work_to_ctx(x) \
517 container_of((x), struct ocelot_mact_work_ctx, work)
518
519static void ocelot_mact_work(struct work_struct *work)
520{
521 struct ocelot_mact_work_ctx *w = ocelot_work_to_ctx(work);
522 struct ocelot *ocelot = w->ocelot;
523
524 switch (w->type) {
525 case OCELOT_MACT_LEARN:
526 ocelot_mact_learn(ocelot, w->learn.pgid, w->learn.addr,
527 w->learn.vid, w->learn.entry_type);
528 break;
529 case OCELOT_MACT_FORGET:
530 ocelot_mact_forget(ocelot, w->forget.addr, w->forget.vid);
531 break;
532 default:
533 break;
Xu Wang20efd2c2021-01-15 09:55:44 +0000534 }
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200535
536 kfree(w);
537}
538
539static int ocelot_enqueue_mact_action(struct ocelot *ocelot,
540 const struct ocelot_mact_work_ctx *ctx)
541{
542 struct ocelot_mact_work_ctx *w = kmemdup(ctx, sizeof(*w), GFP_ATOMIC);
543
544 if (!w)
545 return -ENOMEM;
546
547 w->ocelot = ocelot;
548 INIT_WORK(&w->work, ocelot_mact_work);
549 queue_work(ocelot->owq, &w->work);
550
551 return 0;
552}
553
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300554static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
555{
556 struct ocelot_port_private *priv = netdev_priv(dev);
557 struct ocelot_port *ocelot_port = &priv->port;
558 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200559 struct ocelot_mact_work_ctx w;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300560
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200561 ether_addr_copy(w.forget.addr, addr);
Vladimir Olteanbfbab312021-10-20 20:58:51 +0300562 w.forget.vid = OCELOT_VLAN_UNAWARE_PVID;
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200563 w.type = OCELOT_MACT_FORGET;
564
565 return ocelot_enqueue_mact_action(ocelot, &w);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300566}
567
568static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
569{
570 struct ocelot_port_private *priv = netdev_priv(dev);
571 struct ocelot_port *ocelot_port = &priv->port;
572 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200573 struct ocelot_mact_work_ctx w;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300574
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200575 ether_addr_copy(w.learn.addr, addr);
Vladimir Olteanbfbab312021-10-20 20:58:51 +0300576 w.learn.vid = OCELOT_VLAN_UNAWARE_PVID;
Vladimir Olteanca0b2722020-12-12 21:16:12 +0200577 w.learn.pgid = PGID_CPU;
578 w.learn.entry_type = ENTRYTYPE_LOCKED;
579 w.type = OCELOT_MACT_LEARN;
580
581 return ocelot_enqueue_mact_action(ocelot, &w);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300582}
583
584static void ocelot_set_rx_mode(struct net_device *dev)
585{
586 struct ocelot_port_private *priv = netdev_priv(dev);
587 struct ocelot *ocelot = priv->port.ocelot;
588 u32 val;
589 int i;
590
591 /* This doesn't handle promiscuous mode because the bridge core is
592 * setting IFF_PROMISC on all slave interfaces and all frames would be
593 * forwarded to the CPU port.
594 */
595 val = GENMASK(ocelot->num_phys_ports - 1, 0);
Vladimir Oltean96b029b2020-06-21 14:46:02 +0300596 for_each_nonreserved_multicast_dest_pgid(ocelot, i)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300597 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
598
599 __dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
600}
601
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300602static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
603{
604 struct ocelot_port_private *priv = netdev_priv(dev);
605 struct ocelot_port *ocelot_port = &priv->port;
606 struct ocelot *ocelot = ocelot_port->ocelot;
607 const struct sockaddr *addr = p;
608
609 /* Learn the new net device MAC address in the mac table. */
Vladimir Olteanc3e58a752020-10-31 12:29:12 +0200610 ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data,
Vladimir Olteanbfbab312021-10-20 20:58:51 +0300611 OCELOT_VLAN_UNAWARE_PVID, ENTRYTYPE_LOCKED);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300612 /* Then forget the previous one. */
Vladimir Olteanbfbab312021-10-20 20:58:51 +0300613 ocelot_mact_forget(ocelot, dev->dev_addr, OCELOT_VLAN_UNAWARE_PVID);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300614
Jakub Kicinskif3956eb2021-10-01 14:32:23 -0700615 eth_hw_addr_set(dev, addr->sa_data);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300616 return 0;
617}
618
619static void ocelot_get_stats64(struct net_device *dev,
620 struct rtnl_link_stats64 *stats)
621{
622 struct ocelot_port_private *priv = netdev_priv(dev);
623 struct ocelot *ocelot = priv->port.ocelot;
624 int port = priv->chip_port;
625
626 /* Configure the port to read the stats from */
627 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port),
628 SYS_STAT_CFG);
629
630 /* Get Rx stats */
631 stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
632 stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
633 ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
634 ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
635 ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
636 ocelot_read(ocelot, SYS_COUNT_RX_64) +
637 ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
638 ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
639 ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
640 ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
641 ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
642 stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
643 stats->rx_dropped = dev->stats.rx_dropped;
644
645 /* Get Tx stats */
646 stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
647 stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
648 ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
649 ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
650 ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
651 ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
652 ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
653 stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
654 ocelot_read(ocelot, SYS_COUNT_TX_AGING);
655 stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
656}
657
658static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
659 struct net_device *dev,
660 const unsigned char *addr,
661 u16 vid, u16 flags,
662 struct netlink_ext_ack *extack)
663{
664 struct ocelot_port_private *priv = netdev_priv(dev);
665 struct ocelot *ocelot = priv->port.ocelot;
666 int port = priv->chip_port;
667
668 return ocelot_fdb_add(ocelot, port, addr, vid);
669}
670
671static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
672 struct net_device *dev,
673 const unsigned char *addr, u16 vid)
674{
675 struct ocelot_port_private *priv = netdev_priv(dev);
676 struct ocelot *ocelot = priv->port.ocelot;
677 int port = priv->chip_port;
678
679 return ocelot_fdb_del(ocelot, port, addr, vid);
680}
681
682static int ocelot_port_fdb_dump(struct sk_buff *skb,
683 struct netlink_callback *cb,
684 struct net_device *dev,
685 struct net_device *filter_dev, int *idx)
686{
687 struct ocelot_port_private *priv = netdev_priv(dev);
688 struct ocelot *ocelot = priv->port.ocelot;
689 struct ocelot_dump_ctx dump = {
690 .dev = dev,
691 .skb = skb,
692 .cb = cb,
693 .idx = *idx,
694 };
695 int port = priv->chip_port;
696 int ret;
697
698 ret = ocelot_fdb_dump(ocelot, port, ocelot_port_fdb_do_dump, &dump);
699
700 *idx = dump.idx;
701
702 return ret;
703}
704
705static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
706 u16 vid)
707{
708 return ocelot_vlan_vid_add(dev, vid, false, false);
709}
710
711static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
712 u16 vid)
713{
714 return ocelot_vlan_vid_del(dev, vid);
715}
716
717static void ocelot_vlan_mode(struct ocelot *ocelot, int port,
718 netdev_features_t features)
719{
720 u32 val;
721
722 /* Filtering */
723 val = ocelot_read(ocelot, ANA_VLANMASK);
724 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
725 val |= BIT(port);
726 else
727 val &= ~BIT(port);
728 ocelot_write(ocelot, val, ANA_VLANMASK);
729}
730
731static int ocelot_set_features(struct net_device *dev,
732 netdev_features_t features)
733{
734 netdev_features_t changed = dev->features ^ features;
735 struct ocelot_port_private *priv = netdev_priv(dev);
736 struct ocelot *ocelot = priv->port.ocelot;
737 int port = priv->chip_port;
738
739 if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
740 priv->tc.offload_cnt) {
741 netdev_err(dev,
742 "Cannot disable HW TC offload while offloads active\n");
743 return -EBUSY;
744 }
745
746 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
747 ocelot_vlan_mode(ocelot, port, features);
748
749 return 0;
750}
751
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300752static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
753{
754 struct ocelot_port_private *priv = netdev_priv(dev);
755 struct ocelot *ocelot = priv->port.ocelot;
756 int port = priv->chip_port;
757
758 /* If the attached PHY device isn't capable of timestamping operations,
759 * use our own (when possible).
760 */
761 if (!phy_has_hwtstamp(dev->phydev) && ocelot->ptp) {
762 switch (cmd) {
763 case SIOCSHWTSTAMP:
764 return ocelot_hwstamp_set(ocelot, port, ifr);
765 case SIOCGHWTSTAMP:
766 return ocelot_hwstamp_get(ocelot, port, ifr);
767 }
768 }
769
770 return phy_mii_ioctl(dev->phydev, ifr, cmd);
771}
772
Clément Légerde5841e2021-12-09 16:49:10 +0100773static int ocelot_change_mtu(struct net_device *dev, int new_mtu)
774{
775 struct ocelot_port_private *priv = netdev_priv(dev);
776 struct ocelot_port *ocelot_port = &priv->port;
777 struct ocelot *ocelot = ocelot_port->ocelot;
778
779 ocelot_port_set_maxlen(ocelot, priv->chip_port, new_mtu);
780 WRITE_ONCE(dev->mtu, new_mtu);
781
782 return 0;
783}
784
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300785static const struct net_device_ops ocelot_port_netdev_ops = {
786 .ndo_open = ocelot_port_open,
787 .ndo_stop = ocelot_port_stop,
788 .ndo_start_xmit = ocelot_port_xmit,
Clément Légerde5841e2021-12-09 16:49:10 +0100789 .ndo_change_mtu = ocelot_change_mtu,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300790 .ndo_set_rx_mode = ocelot_set_rx_mode,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300791 .ndo_set_mac_address = ocelot_port_set_mac_address,
792 .ndo_get_stats64 = ocelot_get_stats64,
793 .ndo_fdb_add = ocelot_port_fdb_add,
794 .ndo_fdb_del = ocelot_port_fdb_del,
795 .ndo_fdb_dump = ocelot_port_fdb_dump,
796 .ndo_vlan_rx_add_vid = ocelot_vlan_rx_add_vid,
797 .ndo_vlan_rx_kill_vid = ocelot_vlan_rx_kill_vid,
798 .ndo_set_features = ocelot_set_features,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300799 .ndo_setup_tc = ocelot_setup_tc,
Arnd Bergmanna7605372021-07-27 15:45:13 +0200800 .ndo_eth_ioctl = ocelot_ioctl,
Vladimir Oltean6c30384e2021-01-15 04:11:18 +0200801 .ndo_get_devlink_port = ocelot_get_devlink_port,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300802};
803
Vladimir Oltean319e4dd2020-10-02 15:02:21 +0300804struct net_device *ocelot_port_to_netdev(struct ocelot *ocelot, int port)
805{
806 struct ocelot_port *ocelot_port = ocelot->ports[port];
807 struct ocelot_port_private *priv;
808
809 if (!ocelot_port)
810 return NULL;
811
812 priv = container_of(ocelot_port, struct ocelot_port_private, port);
813
814 return priv->dev;
815}
816
Vladimir Oltean7e38b032020-10-11 12:20:41 +0300817/* Checks if the net_device instance given to us originates from our driver */
818static bool ocelot_netdevice_dev_check(const struct net_device *dev)
Vladimir Oltean319e4dd2020-10-02 15:02:21 +0300819{
820 return dev->netdev_ops == &ocelot_port_netdev_ops;
821}
822
823int ocelot_netdev_to_port(struct net_device *dev)
824{
825 struct ocelot_port_private *priv;
826
Vladimir Oltean7e38b032020-10-11 12:20:41 +0300827 if (!dev || !ocelot_netdevice_dev_check(dev))
Vladimir Oltean319e4dd2020-10-02 15:02:21 +0300828 return -EINVAL;
829
830 priv = netdev_priv(dev);
831
832 return priv->chip_port;
833}
834
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300835static void ocelot_port_get_strings(struct net_device *netdev, u32 sset,
836 u8 *data)
837{
838 struct ocelot_port_private *priv = netdev_priv(netdev);
839 struct ocelot *ocelot = priv->port.ocelot;
840 int port = priv->chip_port;
841
842 ocelot_get_strings(ocelot, port, sset, data);
843}
844
845static void ocelot_port_get_ethtool_stats(struct net_device *dev,
846 struct ethtool_stats *stats,
847 u64 *data)
848{
849 struct ocelot_port_private *priv = netdev_priv(dev);
850 struct ocelot *ocelot = priv->port.ocelot;
851 int port = priv->chip_port;
852
853 ocelot_get_ethtool_stats(ocelot, port, data);
854}
855
856static int ocelot_port_get_sset_count(struct net_device *dev, int sset)
857{
858 struct ocelot_port_private *priv = netdev_priv(dev);
859 struct ocelot *ocelot = priv->port.ocelot;
860 int port = priv->chip_port;
861
862 return ocelot_get_sset_count(ocelot, port, sset);
863}
864
865static int ocelot_port_get_ts_info(struct net_device *dev,
866 struct ethtool_ts_info *info)
867{
868 struct ocelot_port_private *priv = netdev_priv(dev);
869 struct ocelot *ocelot = priv->port.ocelot;
870 int port = priv->chip_port;
871
872 if (!ocelot->ptp)
873 return ethtool_op_get_ts_info(dev, info);
874
875 return ocelot_get_ts_info(ocelot, port, info);
876}
877
878static const struct ethtool_ops ocelot_ethtool_ops = {
879 .get_strings = ocelot_port_get_strings,
880 .get_ethtool_stats = ocelot_port_get_ethtool_stats,
881 .get_sset_count = ocelot_port_get_sset_count,
882 .get_link_ksettings = phy_ethtool_get_link_ksettings,
883 .set_link_ksettings = phy_ethtool_set_link_ksettings,
884 .get_ts_info = ocelot_port_get_ts_info,
885};
886
887static void ocelot_port_attr_stp_state_set(struct ocelot *ocelot, int port,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300888 u8 state)
889{
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300890 ocelot_bridge_stp_state_set(ocelot, port, state);
891}
892
893static void ocelot_port_attr_ageing_set(struct ocelot *ocelot, int port,
894 unsigned long ageing_clock_t)
895{
896 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
897 u32 ageing_time = jiffies_to_msecs(ageing_jiffies);
898
899 ocelot_set_ageing_time(ocelot, ageing_time);
900}
901
902static void ocelot_port_attr_mc_set(struct ocelot *ocelot, int port, bool mc)
903{
904 u32 cpu_fwd_mcast = ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
905 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
906 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
907 u32 val = 0;
908
909 if (mc)
910 val = cpu_fwd_mcast;
911
912 ocelot_rmw_gix(ocelot, val, cpu_fwd_mcast,
913 ANA_PORT_CPU_FWD_CFG, port);
914}
915
Vladimir Oltean69bfac92021-06-27 14:54:24 +0300916static int ocelot_port_attr_set(struct net_device *dev, const void *ctx,
Vladimir Oltean4c08c582021-02-12 17:15:51 +0200917 const struct switchdev_attr *attr,
918 struct netlink_ext_ack *extack)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300919{
920 struct ocelot_port_private *priv = netdev_priv(dev);
921 struct ocelot *ocelot = priv->port.ocelot;
922 int port = priv->chip_port;
923 int err = 0;
924
Vladimir Oltean0d2cfbd2021-06-27 14:54:25 +0300925 if (ctx && ctx != priv)
926 return 0;
927
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300928 switch (attr->id) {
929 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
Vladimir Olteanbae33f22021-01-09 02:01:50 +0200930 ocelot_port_attr_stp_state_set(ocelot, port, attr->u.stp_state);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300931 break;
932 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
933 ocelot_port_attr_ageing_set(ocelot, port, attr->u.ageing_time);
934 break;
935 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
Vladimir Oltean3b95d1b2021-08-19 20:40:07 +0300936 ocelot_port_vlan_filtering(ocelot, port, attr->u.vlan_filtering,
937 extack);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300938 break;
939 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
940 ocelot_port_attr_mc_set(ocelot, port, !attr->u.mc_disabled);
941 break;
Vladimir Oltean421741e2021-02-12 17:15:59 +0200942 case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
943 err = ocelot_port_pre_bridge_flags(ocelot, port,
944 attr->u.brport_flags);
945 break;
946 case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
947 ocelot_port_bridge_flags(ocelot, port, attr->u.brport_flags);
948 break;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300949 default:
950 err = -EOPNOTSUPP;
951 break;
952 }
953
954 return err;
955}
956
Vladimir Oltean01af9402021-08-19 20:40:06 +0300957static int ocelot_vlan_vid_prepare(struct net_device *dev, u16 vid, bool pvid,
958 bool untagged, struct netlink_ext_ack *extack)
959{
960 struct ocelot_port_private *priv = netdev_priv(dev);
961 struct ocelot_port *ocelot_port = &priv->port;
962 struct ocelot *ocelot = ocelot_port->ocelot;
963 int port = priv->chip_port;
964
965 return ocelot_vlan_prepare(ocelot, port, vid, pvid, untagged, extack);
966}
967
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300968static int ocelot_port_obj_add_vlan(struct net_device *dev,
Vladimir Oltean01af9402021-08-19 20:40:06 +0300969 const struct switchdev_obj_port_vlan *vlan,
970 struct netlink_ext_ack *extack)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300971{
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +0200972 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
973 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300974 int ret;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300975
Vladimir Oltean01af9402021-08-19 20:40:06 +0300976 ret = ocelot_vlan_vid_prepare(dev, vlan->vid, pvid, untagged, extack);
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200977 if (ret)
978 return ret;
Vladimir Oltean2f0402f2020-10-31 12:29:15 +0200979
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200980 return ocelot_vlan_vid_add(dev, vlan->vid, pvid, untagged);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +0300981}
982
Vladimir Oltean209edf92020-06-21 14:46:01 +0300983static int ocelot_port_obj_add_mdb(struct net_device *dev,
Vladimir Olteanffb68fc2021-01-09 02:01:48 +0200984 const struct switchdev_obj_port_mdb *mdb)
Vladimir Oltean209edf92020-06-21 14:46:01 +0300985{
986 struct ocelot_port_private *priv = netdev_priv(dev);
987 struct ocelot_port *ocelot_port = &priv->port;
988 struct ocelot *ocelot = ocelot_port->ocelot;
989 int port = priv->chip_port;
990
Vladimir Oltean209edf92020-06-21 14:46:01 +0300991 return ocelot_port_mdb_add(ocelot, port, mdb);
992}
993
994static int ocelot_port_obj_del_mdb(struct net_device *dev,
995 const struct switchdev_obj_port_mdb *mdb)
996{
997 struct ocelot_port_private *priv = netdev_priv(dev);
998 struct ocelot_port *ocelot_port = &priv->port;
999 struct ocelot *ocelot = ocelot_port->ocelot;
1000 int port = priv->chip_port;
1001
1002 return ocelot_port_mdb_del(ocelot, port, mdb);
1003}
1004
Horatiu Vulturd8ea7ff2021-02-16 22:42:03 +01001005static int ocelot_port_obj_mrp_add(struct net_device *dev,
1006 const struct switchdev_obj_mrp *mrp)
1007{
1008 struct ocelot_port_private *priv = netdev_priv(dev);
1009 struct ocelot_port *ocelot_port = &priv->port;
1010 struct ocelot *ocelot = ocelot_port->ocelot;
1011 int port = priv->chip_port;
1012
1013 return ocelot_mrp_add(ocelot, port, mrp);
1014}
1015
1016static int ocelot_port_obj_mrp_del(struct net_device *dev,
1017 const struct switchdev_obj_mrp *mrp)
1018{
1019 struct ocelot_port_private *priv = netdev_priv(dev);
1020 struct ocelot_port *ocelot_port = &priv->port;
1021 struct ocelot *ocelot = ocelot_port->ocelot;
1022 int port = priv->chip_port;
1023
1024 return ocelot_mrp_del(ocelot, port, mrp);
1025}
1026
1027static int
1028ocelot_port_obj_mrp_add_ring_role(struct net_device *dev,
1029 const struct switchdev_obj_ring_role_mrp *mrp)
1030{
1031 struct ocelot_port_private *priv = netdev_priv(dev);
1032 struct ocelot_port *ocelot_port = &priv->port;
1033 struct ocelot *ocelot = ocelot_port->ocelot;
1034 int port = priv->chip_port;
1035
1036 return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1037}
1038
1039static int
1040ocelot_port_obj_mrp_del_ring_role(struct net_device *dev,
1041 const struct switchdev_obj_ring_role_mrp *mrp)
1042{
1043 struct ocelot_port_private *priv = netdev_priv(dev);
1044 struct ocelot_port *ocelot_port = &priv->port;
1045 struct ocelot *ocelot = ocelot_port->ocelot;
1046 int port = priv->chip_port;
1047
1048 return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1049}
1050
Vladimir Oltean69bfac92021-06-27 14:54:24 +03001051static int ocelot_port_obj_add(struct net_device *dev, const void *ctx,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001052 const struct switchdev_obj *obj,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001053 struct netlink_ext_ack *extack)
1054{
Vladimir Oltean0d2cfbd2021-06-27 14:54:25 +03001055 struct ocelot_port_private *priv = netdev_priv(dev);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001056 int ret = 0;
1057
Vladimir Oltean0d2cfbd2021-06-27 14:54:25 +03001058 if (ctx && ctx != priv)
1059 return 0;
1060
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001061 switch (obj->id) {
1062 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1063 ret = ocelot_port_obj_add_vlan(dev,
Vladimir Oltean01af9402021-08-19 20:40:06 +03001064 SWITCHDEV_OBJ_PORT_VLAN(obj),
1065 extack);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001066 break;
1067 case SWITCHDEV_OBJ_ID_PORT_MDB:
Vladimir Olteanffb68fc2021-01-09 02:01:48 +02001068 ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001069 break;
Horatiu Vulturd8ea7ff2021-02-16 22:42:03 +01001070 case SWITCHDEV_OBJ_ID_MRP:
1071 ret = ocelot_port_obj_mrp_add(dev, SWITCHDEV_OBJ_MRP(obj));
1072 break;
1073 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1074 ret = ocelot_port_obj_mrp_add_ring_role(dev,
1075 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1076 break;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001077 default:
1078 return -EOPNOTSUPP;
1079 }
1080
1081 return ret;
1082}
1083
Vladimir Oltean69bfac92021-06-27 14:54:24 +03001084static int ocelot_port_obj_del(struct net_device *dev, const void *ctx,
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001085 const struct switchdev_obj *obj)
1086{
Vladimir Oltean0d2cfbd2021-06-27 14:54:25 +03001087 struct ocelot_port_private *priv = netdev_priv(dev);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001088 int ret = 0;
1089
Vladimir Oltean0d2cfbd2021-06-27 14:54:25 +03001090 if (ctx && ctx != priv)
1091 return 0;
1092
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001093 switch (obj->id) {
1094 case SWITCHDEV_OBJ_ID_PORT_VLAN:
Vladimir Olteanb7a9e0d2021-01-09 02:01:46 +02001095 ret = ocelot_vlan_vid_del(dev,
1096 SWITCHDEV_OBJ_PORT_VLAN(obj)->vid);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001097 break;
1098 case SWITCHDEV_OBJ_ID_PORT_MDB:
1099 ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1100 break;
Horatiu Vulturd8ea7ff2021-02-16 22:42:03 +01001101 case SWITCHDEV_OBJ_ID_MRP:
1102 ret = ocelot_port_obj_mrp_del(dev, SWITCHDEV_OBJ_MRP(obj));
1103 break;
1104 case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1105 ret = ocelot_port_obj_mrp_del_ring_role(dev,
1106 SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1107 break;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001108 default:
1109 return -EOPNOTSUPP;
1110 }
1111
1112 return ret;
1113}
1114
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001115static void ocelot_inherit_brport_flags(struct ocelot *ocelot, int port,
1116 struct net_device *brport_dev)
1117{
1118 struct switchdev_brport_flags flags = {0};
1119 int flag;
1120
1121 flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1122
1123 for_each_set_bit(flag, &flags.mask, 32)
1124 if (br_port_flag_is_set(brport_dev, BIT(flag)))
1125 flags.val |= BIT(flag);
1126
1127 ocelot_port_bridge_flags(ocelot, port, flags);
1128}
1129
1130static void ocelot_clear_brport_flags(struct ocelot *ocelot, int port)
1131{
1132 struct switchdev_brport_flags flags;
1133
1134 flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1135 flags.val = flags.mask & ~BR_LEARNING;
1136
1137 ocelot_port_bridge_flags(ocelot, port, flags);
1138}
1139
1140static int ocelot_switchdev_sync(struct ocelot *ocelot, int port,
1141 struct net_device *brport_dev,
1142 struct net_device *bridge_dev,
1143 struct netlink_ext_ack *extack)
1144{
1145 clock_t ageing_time;
1146 u8 stp_state;
Vladimir Oltean0d2cfbd2021-06-27 14:54:25 +03001147
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001148 ocelot_inherit_brport_flags(ocelot, port, brport_dev);
1149
1150 stp_state = br_port_get_stp_state(brport_dev);
1151 ocelot_bridge_stp_state_set(ocelot, port, stp_state);
1152
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001153 ageing_time = br_get_ageing_time(bridge_dev);
1154 ocelot_port_attr_ageing_set(ocelot, port, ageing_time);
1155
Vladimir Oltean4e51bf42021-07-21 19:24:03 +03001156 return ocelot_port_vlan_filtering(ocelot, port,
Vladimir Oltean3b95d1b2021-08-19 20:40:07 +03001157 br_vlan_enabled(bridge_dev),
1158 extack);
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001159}
1160
1161static int ocelot_switchdev_unsync(struct ocelot *ocelot, int port)
1162{
1163 int err;
1164
Vladimir Oltean3b95d1b2021-08-19 20:40:07 +03001165 err = ocelot_port_vlan_filtering(ocelot, port, false, NULL);
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001166 if (err)
1167 return err;
1168
1169 ocelot_clear_brport_flags(ocelot, port);
1170
1171 ocelot_bridge_stp_state_set(ocelot, port, BR_STATE_FORWARDING);
1172
1173 return 0;
1174}
1175
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001176static int ocelot_netdevice_bridge_join(struct net_device *dev,
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001177 struct net_device *brport_dev,
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001178 struct net_device *bridge,
1179 struct netlink_ext_ack *extack)
Vladimir Oltean421741e2021-02-12 17:15:59 +02001180{
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001181 struct ocelot_port_private *priv = netdev_priv(dev);
1182 struct ocelot_port *ocelot_port = &priv->port;
1183 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001184 int port = priv->chip_port;
Vladimir Oltean421741e2021-02-12 17:15:59 +02001185 int err;
1186
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001187 ocelot_port_bridge_join(ocelot, port, bridge);
Vladimir Oltean421741e2021-02-12 17:15:59 +02001188
Vladimir Oltean4e51bf42021-07-21 19:24:03 +03001189 err = switchdev_bridge_port_offload(brport_dev, dev, priv,
Horatiu Vulturc0b7f7d2022-01-17 13:53:00 +01001190 &ocelot_switchdev_nb,
Vladimir Oltean4e51bf42021-07-21 19:24:03 +03001191 &ocelot_switchdev_blocking_nb,
Tobias Waldekranz47211192021-07-22 18:55:38 +03001192 false, extack);
Vladimir Oltean2f5dc002021-07-21 19:24:01 +03001193 if (err)
1194 goto err_switchdev_offload;
1195
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001196 err = ocelot_switchdev_sync(ocelot, port, brport_dev, bridge, extack);
Vladimir Oltean421741e2021-02-12 17:15:59 +02001197 if (err)
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001198 goto err_switchdev_sync;
Vladimir Oltean421741e2021-02-12 17:15:59 +02001199
1200 return 0;
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001201
1202err_switchdev_sync:
Vladimir Oltean4e51bf42021-07-21 19:24:03 +03001203 switchdev_bridge_port_unoffload(brport_dev, priv,
Horatiu Vulturc0b7f7d2022-01-17 13:53:00 +01001204 &ocelot_switchdev_nb,
Vladimir Oltean4e51bf42021-07-21 19:24:03 +03001205 &ocelot_switchdev_blocking_nb);
Vladimir Oltean2f5dc002021-07-21 19:24:01 +03001206err_switchdev_offload:
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001207 ocelot_port_bridge_leave(ocelot, port, bridge);
1208 return err;
Vladimir Oltean421741e2021-02-12 17:15:59 +02001209}
1210
Vladimir Oltean4e51bf42021-07-21 19:24:03 +03001211static void ocelot_netdevice_pre_bridge_leave(struct net_device *dev,
1212 struct net_device *brport_dev)
Vladimir Oltean2f5dc002021-07-21 19:24:01 +03001213{
Vladimir Oltean4e51bf42021-07-21 19:24:03 +03001214 struct ocelot_port_private *priv = netdev_priv(dev);
1215
1216 switchdev_bridge_port_unoffload(brport_dev, priv,
Horatiu Vulturc0b7f7d2022-01-17 13:53:00 +01001217 &ocelot_switchdev_nb,
Vladimir Oltean4e51bf42021-07-21 19:24:03 +03001218 &ocelot_switchdev_blocking_nb);
Vladimir Oltean2f5dc002021-07-21 19:24:01 +03001219}
1220
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001221static int ocelot_netdevice_bridge_leave(struct net_device *dev,
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001222 struct net_device *brport_dev,
Vladimir Oltean421741e2021-02-12 17:15:59 +02001223 struct net_device *bridge)
1224{
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001225 struct ocelot_port_private *priv = netdev_priv(dev);
1226 struct ocelot_port *ocelot_port = &priv->port;
1227 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001228 int port = priv->chip_port;
Vladimir Oltean421741e2021-02-12 17:15:59 +02001229 int err;
1230
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001231 err = ocelot_switchdev_unsync(ocelot, port);
1232 if (err)
1233 return err;
Vladimir Oltean421741e2021-02-12 17:15:59 +02001234
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001235 ocelot_port_bridge_leave(ocelot, port, bridge);
Vladimir Oltean421741e2021-02-12 17:15:59 +02001236
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001237 return 0;
Vladimir Oltean421741e2021-02-12 17:15:59 +02001238}
1239
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001240static int ocelot_netdevice_lag_join(struct net_device *dev,
1241 struct net_device *bond,
1242 struct netdev_lag_upper_info *info,
1243 struct netlink_ext_ack *extack)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001244{
1245 struct ocelot_port_private *priv = netdev_priv(dev);
1246 struct ocelot_port *ocelot_port = &priv->port;
1247 struct ocelot *ocelot = ocelot_port->ocelot;
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001248 struct net_device *bridge_dev;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001249 int port = priv->chip_port;
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001250 int err;
1251
1252 err = ocelot_port_lag_join(ocelot, port, bond, info);
1253 if (err == -EOPNOTSUPP) {
1254 NL_SET_ERR_MSG_MOD(extack, "Offloading not supported");
1255 return 0;
1256 }
1257
1258 bridge_dev = netdev_master_upper_dev_get(bond);
1259 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1260 return 0;
1261
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001262 err = ocelot_netdevice_bridge_join(dev, bond, bridge_dev, extack);
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001263 if (err)
1264 goto err_bridge_join;
1265
1266 return 0;
1267
1268err_bridge_join:
1269 ocelot_port_lag_leave(ocelot, port, bond);
1270 return err;
1271}
1272
Vladimir Oltean2f5dc002021-07-21 19:24:01 +03001273static void ocelot_netdevice_pre_lag_leave(struct net_device *dev,
1274 struct net_device *bond)
1275{
1276 struct net_device *bridge_dev;
1277
1278 bridge_dev = netdev_master_upper_dev_get(bond);
1279 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1280 return;
1281
Vladimir Oltean4e51bf42021-07-21 19:24:03 +03001282 ocelot_netdevice_pre_bridge_leave(dev, bond);
Vladimir Oltean2f5dc002021-07-21 19:24:01 +03001283}
1284
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001285static int ocelot_netdevice_lag_leave(struct net_device *dev,
1286 struct net_device *bond)
1287{
1288 struct ocelot_port_private *priv = netdev_priv(dev);
1289 struct ocelot_port *ocelot_port = &priv->port;
1290 struct ocelot *ocelot = ocelot_port->ocelot;
1291 struct net_device *bridge_dev;
1292 int port = priv->chip_port;
1293
1294 ocelot_port_lag_leave(ocelot, port, bond);
1295
1296 bridge_dev = netdev_master_upper_dev_get(bond);
1297 if (!bridge_dev || !netif_is_bridge_master(bridge_dev))
1298 return 0;
1299
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001300 return ocelot_netdevice_bridge_leave(dev, bond, bridge_dev);
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001301}
1302
1303static int ocelot_netdevice_changeupper(struct net_device *dev,
Vladimir Olteane56c6bb2021-07-13 12:33:50 +03001304 struct net_device *brport_dev,
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001305 struct netdev_notifier_changeupper_info *info)
1306{
1307 struct netlink_ext_ack *extack;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001308 int err = 0;
1309
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001310 extack = netdev_notifier_info_to_extack(&info->info);
1311
Vladimir Oltean662981b2021-02-06 00:02:10 +02001312 if (netif_is_bridge_master(info->upper_dev)) {
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001313 if (info->linking)
Vladimir Olteane56c6bb2021-07-13 12:33:50 +03001314 err = ocelot_netdevice_bridge_join(dev, brport_dev,
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001315 info->upper_dev,
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001316 extack);
1317 else
Vladimir Olteane56c6bb2021-07-13 12:33:50 +03001318 err = ocelot_netdevice_bridge_leave(dev, brport_dev,
Vladimir Olteane4bd44e2021-03-23 01:51:52 +02001319 info->upper_dev);
Vladimir Oltean662981b2021-02-06 00:02:10 +02001320 }
1321 if (netif_is_lag_master(info->upper_dev)) {
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001322 if (info->linking)
1323 err = ocelot_netdevice_lag_join(dev, info->upper_dev,
1324 info->upper_info, extack);
1325 else
1326 ocelot_netdevice_lag_leave(dev, info->upper_dev);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001327 }
1328
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001329 return notifier_from_errno(err);
1330}
1331
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001332/* Treat CHANGEUPPER events on an offloaded LAG as individual CHANGEUPPER
1333 * events for the lower physical ports of the LAG.
1334 * If the LAG upper isn't offloaded, ignore its CHANGEUPPER events.
1335 * In case the LAG joined a bridge, notify that we are offloading it and can do
1336 * forwarding in hardware towards it.
1337 */
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001338static int
1339ocelot_netdevice_lag_changeupper(struct net_device *dev,
1340 struct netdev_notifier_changeupper_info *info)
1341{
1342 struct net_device *lower;
1343 struct list_head *iter;
1344 int err = NOTIFY_DONE;
1345
1346 netdev_for_each_lower_dev(dev, lower, iter) {
Vladimir Oltean81ef35e2021-03-23 01:51:51 +02001347 struct ocelot_port_private *priv = netdev_priv(lower);
1348 struct ocelot_port *ocelot_port = &priv->port;
1349
1350 if (ocelot_port->bond != dev)
1351 return NOTIFY_OK;
1352
Vladimir Olteane56c6bb2021-07-13 12:33:50 +03001353 err = ocelot_netdevice_changeupper(lower, dev, info);
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001354 if (err)
1355 return notifier_from_errno(err);
1356 }
1357
1358 return NOTIFY_DONE;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001359}
1360
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02001361static int
Vladimir Oltean2f5dc002021-07-21 19:24:01 +03001362ocelot_netdevice_prechangeupper(struct net_device *dev,
1363 struct net_device *brport_dev,
1364 struct netdev_notifier_changeupper_info *info)
1365{
1366 if (netif_is_bridge_master(info->upper_dev) && !info->linking)
Vladimir Oltean4e51bf42021-07-21 19:24:03 +03001367 ocelot_netdevice_pre_bridge_leave(dev, brport_dev);
Vladimir Oltean2f5dc002021-07-21 19:24:01 +03001368
1369 if (netif_is_lag_master(info->upper_dev) && !info->linking)
1370 ocelot_netdevice_pre_lag_leave(dev, info->upper_dev);
1371
1372 return NOTIFY_DONE;
1373}
1374
1375static int
1376ocelot_netdevice_lag_prechangeupper(struct net_device *dev,
1377 struct netdev_notifier_changeupper_info *info)
1378{
1379 struct net_device *lower;
1380 struct list_head *iter;
1381 int err = NOTIFY_DONE;
1382
1383 netdev_for_each_lower_dev(dev, lower, iter) {
1384 struct ocelot_port_private *priv = netdev_priv(lower);
1385 struct ocelot_port *ocelot_port = &priv->port;
1386
1387 if (ocelot_port->bond != dev)
1388 return NOTIFY_OK;
1389
1390 err = ocelot_netdevice_prechangeupper(dev, lower, info);
1391 if (err)
1392 return err;
1393 }
1394
1395 return NOTIFY_DONE;
1396}
1397
1398static int
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02001399ocelot_netdevice_changelowerstate(struct net_device *dev,
1400 struct netdev_lag_lower_state_info *info)
1401{
1402 struct ocelot_port_private *priv = netdev_priv(dev);
1403 bool is_active = info->link_up && info->tx_enabled;
1404 struct ocelot_port *ocelot_port = &priv->port;
1405 struct ocelot *ocelot = ocelot_port->ocelot;
1406 int port = priv->chip_port;
1407
1408 if (!ocelot_port->bond)
1409 return NOTIFY_DONE;
1410
1411 if (ocelot_port->lag_tx_active == is_active)
1412 return NOTIFY_DONE;
1413
1414 ocelot_port_lag_change(ocelot, port, is_active);
1415
1416 return NOTIFY_OK;
1417}
1418
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001419static int ocelot_netdevice_event(struct notifier_block *unused,
1420 unsigned long event, void *ptr)
1421{
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001422 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001423
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001424 switch (event) {
Vladimir Oltean2f5dc002021-07-21 19:24:01 +03001425 case NETDEV_PRECHANGEUPPER: {
1426 struct netdev_notifier_changeupper_info *info = ptr;
1427
1428 if (ocelot_netdevice_dev_check(dev))
1429 return ocelot_netdevice_prechangeupper(dev, dev, info);
1430
1431 if (netif_is_lag_master(dev))
1432 return ocelot_netdevice_lag_prechangeupper(dev, info);
1433
1434 break;
1435 }
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001436 case NETDEV_CHANGEUPPER: {
1437 struct netdev_notifier_changeupper_info *info = ptr;
1438
1439 if (ocelot_netdevice_dev_check(dev))
Vladimir Olteane56c6bb2021-07-13 12:33:50 +03001440 return ocelot_netdevice_changeupper(dev, dev, info);
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001441
1442 if (netif_is_lag_master(dev))
1443 return ocelot_netdevice_lag_changeupper(dev, info);
1444
1445 break;
1446 }
Vladimir Oltean23ca3b72021-02-06 00:02:19 +02001447 case NETDEV_CHANGELOWERSTATE: {
1448 struct netdev_notifier_changelowerstate_info *info = ptr;
1449
1450 if (!ocelot_netdevice_dev_check(dev))
1451 break;
1452
1453 return ocelot_netdevice_changelowerstate(dev,
1454 info->lower_state_info);
1455 }
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001456 default:
1457 break;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001458 }
1459
Vladimir Oltean41e66fa2021-02-06 00:02:11 +02001460 return NOTIFY_DONE;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001461}
1462
1463struct notifier_block ocelot_netdevice_nb __read_mostly = {
1464 .notifier_call = ocelot_netdevice_event,
1465};
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001466
1467static int ocelot_switchdev_event(struct notifier_block *unused,
1468 unsigned long event, void *ptr)
1469{
1470 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1471 int err;
1472
1473 switch (event) {
1474 case SWITCHDEV_PORT_ATTR_SET:
1475 err = switchdev_handle_port_attr_set(dev, ptr,
1476 ocelot_netdevice_dev_check,
1477 ocelot_port_attr_set);
1478 return notifier_from_errno(err);
1479 }
1480
1481 return NOTIFY_DONE;
1482}
1483
1484struct notifier_block ocelot_switchdev_nb __read_mostly = {
1485 .notifier_call = ocelot_switchdev_event,
1486};
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001487
1488static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
1489 unsigned long event, void *ptr)
1490{
1491 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1492 int err;
1493
1494 switch (event) {
1495 /* Blocking events. */
1496 case SWITCHDEV_PORT_OBJ_ADD:
1497 err = switchdev_handle_port_obj_add(dev, ptr,
1498 ocelot_netdevice_dev_check,
1499 ocelot_port_obj_add);
1500 return notifier_from_errno(err);
1501 case SWITCHDEV_PORT_OBJ_DEL:
1502 err = switchdev_handle_port_obj_del(dev, ptr,
1503 ocelot_netdevice_dev_check,
1504 ocelot_port_obj_del);
1505 return notifier_from_errno(err);
1506 case SWITCHDEV_PORT_ATTR_SET:
1507 err = switchdev_handle_port_attr_set(dev, ptr,
1508 ocelot_netdevice_dev_check,
1509 ocelot_port_attr_set);
1510 return notifier_from_errno(err);
1511 }
1512
1513 return NOTIFY_DONE;
1514}
1515
1516struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
1517 .notifier_call = ocelot_switchdev_blocking_event,
1518};
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001519
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001520static void vsc7514_phylink_mac_config(struct phylink_config *config,
1521 unsigned int link_an_mode,
1522 const struct phylink_link_state *state)
1523{
1524 struct net_device *ndev = to_net_dev(config->dev);
1525 struct ocelot_port_private *priv = netdev_priv(ndev);
1526 struct ocelot_port *ocelot_port = &priv->port;
1527
1528 /* Disable HDX fast control */
1529 ocelot_port_writel(ocelot_port, DEV_PORT_MISC_HDX_FAST_DIS,
1530 DEV_PORT_MISC);
1531
1532 /* SGMII only for now */
1533 ocelot_port_writel(ocelot_port, PCS1G_MODE_CFG_SGMII_MODE_ENA,
1534 PCS1G_MODE_CFG);
1535 ocelot_port_writel(ocelot_port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
1536
1537 /* Enable PCS */
1538 ocelot_port_writel(ocelot_port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
1539
1540 /* No aneg on SGMII */
1541 ocelot_port_writel(ocelot_port, 0, PCS1G_ANEG_CFG);
1542
1543 /* No loopback */
1544 ocelot_port_writel(ocelot_port, 0, PCS1G_LB_CFG);
1545}
1546
1547static void vsc7514_phylink_mac_link_down(struct phylink_config *config,
1548 unsigned int link_an_mode,
1549 phy_interface_t interface)
1550{
1551 struct net_device *ndev = to_net_dev(config->dev);
1552 struct ocelot_port_private *priv = netdev_priv(ndev);
1553 struct ocelot *ocelot = priv->port.ocelot;
1554 int port = priv->chip_port;
1555
1556 ocelot_phylink_mac_link_down(ocelot, port, link_an_mode, interface,
1557 OCELOT_MAC_QUIRKS);
1558}
1559
1560static void vsc7514_phylink_mac_link_up(struct phylink_config *config,
1561 struct phy_device *phydev,
1562 unsigned int link_an_mode,
1563 phy_interface_t interface,
1564 int speed, int duplex,
1565 bool tx_pause, bool rx_pause)
1566{
1567 struct net_device *ndev = to_net_dev(config->dev);
1568 struct ocelot_port_private *priv = netdev_priv(ndev);
1569 struct ocelot *ocelot = priv->port.ocelot;
1570 int port = priv->chip_port;
1571
1572 ocelot_phylink_mac_link_up(ocelot, port, phydev, link_an_mode,
1573 interface, speed, duplex,
1574 tx_pause, rx_pause, OCELOT_MAC_QUIRKS);
1575}
1576
1577static const struct phylink_mac_ops ocelot_phylink_ops = {
Russell King (Oracle)7258aa52021-11-16 10:09:41 +00001578 .validate = phylink_generic_validate,
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001579 .mac_config = vsc7514_phylink_mac_config,
1580 .mac_link_down = vsc7514_phylink_mac_link_down,
1581 .mac_link_up = vsc7514_phylink_mac_link_up,
1582};
1583
1584static int ocelot_port_phylink_create(struct ocelot *ocelot, int port,
1585 struct device_node *portnp)
1586{
1587 struct ocelot_port *ocelot_port = ocelot->ports[port];
1588 struct ocelot_port_private *priv;
1589 struct device *dev = ocelot->dev;
1590 phy_interface_t phy_mode;
1591 struct phylink *phylink;
1592 int err;
1593
1594 of_get_phy_mode(portnp, &phy_mode);
1595 /* DT bindings of internal PHY ports are broken and don't
1596 * specify a phy-mode
1597 */
1598 if (phy_mode == PHY_INTERFACE_MODE_NA)
1599 phy_mode = PHY_INTERFACE_MODE_INTERNAL;
1600
1601 if (phy_mode != PHY_INTERFACE_MODE_SGMII &&
1602 phy_mode != PHY_INTERFACE_MODE_QSGMII &&
1603 phy_mode != PHY_INTERFACE_MODE_INTERNAL) {
1604 dev_err(dev, "unsupported phy mode %s for port %d\n",
1605 phy_modes(phy_mode), port);
1606 return -EINVAL;
1607 }
1608
1609 /* Ensure clock signals and speed are set on all QSGMII links */
1610 if (phy_mode == PHY_INTERFACE_MODE_QSGMII)
1611 ocelot_port_rmwl(ocelot_port, 0,
1612 DEV_CLOCK_CFG_MAC_TX_RST |
Wan Jiabing74a3bc42021-10-11 10:27:41 +08001613 DEV_CLOCK_CFG_MAC_RX_RST,
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001614 DEV_CLOCK_CFG);
1615
1616 ocelot_port->phy_mode = phy_mode;
1617
1618 if (phy_mode != PHY_INTERFACE_MODE_INTERNAL) {
1619 struct phy *serdes = of_phy_get(portnp, NULL);
1620
1621 if (IS_ERR(serdes)) {
1622 err = PTR_ERR(serdes);
1623 dev_err_probe(dev, err,
1624 "missing SerDes phys for port %d\n",
1625 port);
1626 return err;
1627 }
1628
1629 err = phy_set_mode_ext(serdes, PHY_MODE_ETHERNET, phy_mode);
1630 of_phy_put(serdes);
1631 if (err) {
1632 dev_err(dev, "Could not SerDes mode on port %d: %pe\n",
1633 port, ERR_PTR(err));
1634 return err;
1635 }
1636 }
1637
1638 priv = container_of(ocelot_port, struct ocelot_port_private, port);
1639
1640 priv->phylink_config.dev = &priv->dev->dev;
1641 priv->phylink_config.type = PHYLINK_NETDEV;
Russell King (Oracle)7258aa52021-11-16 10:09:41 +00001642 priv->phylink_config.mac_capabilities = MAC_ASYM_PAUSE | MAC_SYM_PAUSE |
1643 MAC_10 | MAC_100 | MAC_1000FD | MAC_2500FD;
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001644
Russell King (Oracle)8ea8c5b2021-11-16 10:09:31 +00001645 __set_bit(ocelot_port->phy_mode,
1646 priv->phylink_config.supported_interfaces);
1647
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001648 phylink = phylink_create(&priv->phylink_config,
1649 of_fwnode_handle(portnp),
1650 phy_mode, &ocelot_phylink_ops);
1651 if (IS_ERR(phylink)) {
1652 err = PTR_ERR(phylink);
1653 dev_err(dev, "Could not create phylink (%pe)\n", phylink);
1654 return err;
1655 }
1656
1657 priv->phylink = phylink;
1658
1659 err = phylink_of_phy_connect(phylink, portnp, 0);
1660 if (err) {
1661 dev_err(dev, "Could not connect to PHY: %pe\n", ERR_PTR(err));
1662 phylink_destroy(phylink);
1663 priv->phylink = NULL;
1664 return err;
1665 }
1666
1667 return 0;
1668}
1669
Vladimir Oltean91c724c2020-07-13 19:57:01 +03001670int ocelot_probe_port(struct ocelot *ocelot, int port, struct regmap *target,
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001671 struct device_node *portnp)
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001672{
1673 struct ocelot_port_private *priv;
1674 struct ocelot_port *ocelot_port;
1675 struct net_device *dev;
1676 int err;
1677
1678 dev = alloc_etherdev(sizeof(struct ocelot_port_private));
1679 if (!dev)
1680 return -ENOMEM;
1681 SET_NETDEV_DEV(dev, ocelot->dev);
1682 priv = netdev_priv(dev);
1683 priv->dev = dev;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001684 priv->chip_port = port;
1685 ocelot_port = &priv->port;
1686 ocelot_port->ocelot = ocelot;
Vladimir Oltean91c724c2020-07-13 19:57:01 +03001687 ocelot_port->target = target;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001688 ocelot->ports[port] = ocelot_port;
1689
1690 dev->netdev_ops = &ocelot_port_netdev_ops;
1691 dev->ethtool_ops = &ocelot_ethtool_ops;
Clément Légerde5841e2021-12-09 16:49:10 +01001692 dev->max_mtu = OCELOT_JUMBO_MTU;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001693
1694 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
1695 NETIF_F_HW_TC;
1696 dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
1697
Clément Léger84386992021-12-14 10:55:34 +01001698 err = of_get_ethdev_address(portnp, dev);
1699 if (err)
1700 eth_hw_addr_gen(dev, ocelot->base_mac, port);
1701
Vladimir Olteanc3e58a752020-10-31 12:29:12 +02001702 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr,
Vladimir Olteanbfbab312021-10-20 20:58:51 +03001703 OCELOT_VLAN_UNAWARE_PVID, ENTRYTYPE_LOCKED);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001704
1705 ocelot_init_port(ocelot, port);
1706
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001707 err = ocelot_port_phylink_create(ocelot, port, portnp);
1708 if (err)
1709 goto out;
1710
Clément Léger753a0262021-12-09 16:49:11 +01001711 if (ocelot->fdma)
1712 ocelot_fdma_netdev_init(ocelot, dev);
1713
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001714 err = register_netdev(dev);
1715 if (err) {
1716 dev_err(ocelot->dev, "register_netdev failed\n");
Clément Léger753a0262021-12-09 16:49:11 +01001717 goto out_fdma_deinit;
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001718 }
1719
Dan Carpentere0c16232021-02-02 12:12:38 +03001720 return 0;
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001721
Clément Léger753a0262021-12-09 16:49:11 +01001722out_fdma_deinit:
1723 if (ocelot->fdma)
1724 ocelot_fdma_netdev_deinit(ocelot, dev);
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001725out:
1726 ocelot->ports[port] = NULL;
1727 free_netdev(dev);
1728
1729 return err;
Dan Carpentere0c16232021-02-02 12:12:38 +03001730}
1731
1732void ocelot_release_port(struct ocelot_port *ocelot_port)
1733{
1734 struct ocelot_port_private *priv = container_of(ocelot_port,
1735 struct ocelot_port_private,
1736 port);
Clément Léger753a0262021-12-09 16:49:11 +01001737 struct ocelot *ocelot = ocelot_port->ocelot;
1738 struct ocelot_fdma *fdma = ocelot->fdma;
Dan Carpentere0c16232021-02-02 12:12:38 +03001739
1740 unregister_netdev(priv->dev);
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001741
Clément Léger753a0262021-12-09 16:49:11 +01001742 if (fdma)
1743 ocelot_fdma_netdev_deinit(ocelot, priv->dev);
1744
Vladimir Olteane6e12df2021-08-15 04:47:48 +03001745 if (priv->phylink) {
1746 rtnl_lock();
1747 phylink_disconnect_phy(priv->phylink);
1748 rtnl_unlock();
1749
1750 phylink_destroy(priv->phylink);
1751 }
1752
Dan Carpentere0c16232021-02-02 12:12:38 +03001753 free_netdev(priv->dev);
Vladimir Oltean9c90eea2020-06-20 18:43:44 +03001754}