flow_offload: reject configuration of packet-per-second policing in offload drivers
[linux-2.6-microblaze.git] / drivers / net / ethernet / mscc / ocelot_net.c
1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
2 /* Microsemi Ocelot Switch driver
3  *
4  * This contains glue logic between the switchdev driver operations and the
5  * mscc_ocelot_switch_lib.
6  *
7  * Copyright (c) 2017, 2019 Microsemi Corporation
8  * Copyright 2020-2021 NXP Semiconductors
9  */
10
11 #include <linux/if_bridge.h>
12 #include <net/pkt_cls.h>
13 #include "ocelot.h"
14 #include "ocelot_vcap.h"
15
16 static struct ocelot *devlink_port_to_ocelot(struct devlink_port *dlp)
17 {
18         return devlink_priv(dlp->devlink);
19 }
20
21 static int devlink_port_to_port(struct devlink_port *dlp)
22 {
23         struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
24
25         return dlp - ocelot->devlink_ports;
26 }
27
28 static int ocelot_devlink_sb_pool_get(struct devlink *dl,
29                                       unsigned int sb_index, u16 pool_index,
30                                       struct devlink_sb_pool_info *pool_info)
31 {
32         struct ocelot *ocelot = devlink_priv(dl);
33
34         return ocelot_sb_pool_get(ocelot, sb_index, pool_index, pool_info);
35 }
36
37 static int ocelot_devlink_sb_pool_set(struct devlink *dl, unsigned int sb_index,
38                                       u16 pool_index, u32 size,
39                                       enum devlink_sb_threshold_type threshold_type,
40                                       struct netlink_ext_ack *extack)
41 {
42         struct ocelot *ocelot = devlink_priv(dl);
43
44         return ocelot_sb_pool_set(ocelot, sb_index, pool_index, size,
45                                   threshold_type, extack);
46 }
47
48 static int ocelot_devlink_sb_port_pool_get(struct devlink_port *dlp,
49                                            unsigned int sb_index, u16 pool_index,
50                                            u32 *p_threshold)
51 {
52         struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
53         int port = devlink_port_to_port(dlp);
54
55         return ocelot_sb_port_pool_get(ocelot, port, sb_index, pool_index,
56                                        p_threshold);
57 }
58
59 static int ocelot_devlink_sb_port_pool_set(struct devlink_port *dlp,
60                                            unsigned int sb_index, u16 pool_index,
61                                            u32 threshold,
62                                            struct netlink_ext_ack *extack)
63 {
64         struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
65         int port = devlink_port_to_port(dlp);
66
67         return ocelot_sb_port_pool_set(ocelot, port, sb_index, pool_index,
68                                        threshold, extack);
69 }
70
71 static int
72 ocelot_devlink_sb_tc_pool_bind_get(struct devlink_port *dlp,
73                                    unsigned int sb_index, u16 tc_index,
74                                    enum devlink_sb_pool_type pool_type,
75                                    u16 *p_pool_index, u32 *p_threshold)
76 {
77         struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
78         int port = devlink_port_to_port(dlp);
79
80         return ocelot_sb_tc_pool_bind_get(ocelot, port, sb_index, tc_index,
81                                           pool_type, p_pool_index,
82                                           p_threshold);
83 }
84
85 static int
86 ocelot_devlink_sb_tc_pool_bind_set(struct devlink_port *dlp,
87                                    unsigned int sb_index, u16 tc_index,
88                                    enum devlink_sb_pool_type pool_type,
89                                    u16 pool_index, u32 threshold,
90                                    struct netlink_ext_ack *extack)
91 {
92         struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
93         int port = devlink_port_to_port(dlp);
94
95         return ocelot_sb_tc_pool_bind_set(ocelot, port, sb_index, tc_index,
96                                           pool_type, pool_index, threshold,
97                                           extack);
98 }
99
100 static int ocelot_devlink_sb_occ_snapshot(struct devlink *dl,
101                                           unsigned int sb_index)
102 {
103         struct ocelot *ocelot = devlink_priv(dl);
104
105         return ocelot_sb_occ_snapshot(ocelot, sb_index);
106 }
107
108 static int ocelot_devlink_sb_occ_max_clear(struct devlink *dl,
109                                            unsigned int sb_index)
110 {
111         struct ocelot *ocelot = devlink_priv(dl);
112
113         return ocelot_sb_occ_max_clear(ocelot, sb_index);
114 }
115
116 static int ocelot_devlink_sb_occ_port_pool_get(struct devlink_port *dlp,
117                                                unsigned int sb_index,
118                                                u16 pool_index, u32 *p_cur,
119                                                u32 *p_max)
120 {
121         struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
122         int port = devlink_port_to_port(dlp);
123
124         return ocelot_sb_occ_port_pool_get(ocelot, port, sb_index, pool_index,
125                                            p_cur, p_max);
126 }
127
128 static int
129 ocelot_devlink_sb_occ_tc_port_bind_get(struct devlink_port *dlp,
130                                        unsigned int sb_index, u16 tc_index,
131                                        enum devlink_sb_pool_type pool_type,
132                                        u32 *p_cur, u32 *p_max)
133 {
134         struct ocelot *ocelot = devlink_port_to_ocelot(dlp);
135         int port = devlink_port_to_port(dlp);
136
137         return ocelot_sb_occ_tc_port_bind_get(ocelot, port, sb_index,
138                                               tc_index, pool_type,
139                                               p_cur, p_max);
140 }
141
142 const struct devlink_ops ocelot_devlink_ops = {
143         .sb_pool_get                    = ocelot_devlink_sb_pool_get,
144         .sb_pool_set                    = ocelot_devlink_sb_pool_set,
145         .sb_port_pool_get               = ocelot_devlink_sb_port_pool_get,
146         .sb_port_pool_set               = ocelot_devlink_sb_port_pool_set,
147         .sb_tc_pool_bind_get            = ocelot_devlink_sb_tc_pool_bind_get,
148         .sb_tc_pool_bind_set            = ocelot_devlink_sb_tc_pool_bind_set,
149         .sb_occ_snapshot                = ocelot_devlink_sb_occ_snapshot,
150         .sb_occ_max_clear               = ocelot_devlink_sb_occ_max_clear,
151         .sb_occ_port_pool_get           = ocelot_devlink_sb_occ_port_pool_get,
152         .sb_occ_tc_port_bind_get        = ocelot_devlink_sb_occ_tc_port_bind_get,
153 };
154
155 int ocelot_port_devlink_init(struct ocelot *ocelot, int port,
156                              enum devlink_port_flavour flavour)
157 {
158         struct devlink_port *dlp = &ocelot->devlink_ports[port];
159         int id_len = sizeof(ocelot->base_mac);
160         struct devlink *dl = ocelot->devlink;
161         struct devlink_port_attrs attrs = {};
162
163         memcpy(attrs.switch_id.id, &ocelot->base_mac, id_len);
164         attrs.switch_id.id_len = id_len;
165         attrs.phys.port_number = port;
166         attrs.flavour = flavour;
167
168         devlink_port_attrs_set(dlp, &attrs);
169
170         return devlink_port_register(dl, dlp, port);
171 }
172
173 void ocelot_port_devlink_teardown(struct ocelot *ocelot, int port)
174 {
175         struct devlink_port *dlp = &ocelot->devlink_ports[port];
176
177         devlink_port_unregister(dlp);
178 }
179
180 static struct devlink_port *ocelot_get_devlink_port(struct net_device *dev)
181 {
182         struct ocelot_port_private *priv = netdev_priv(dev);
183         struct ocelot *ocelot = priv->port.ocelot;
184         int port = priv->chip_port;
185
186         return &ocelot->devlink_ports[port];
187 }
188
189 int ocelot_setup_tc_cls_flower(struct ocelot_port_private *priv,
190                                struct flow_cls_offload *f,
191                                bool ingress)
192 {
193         struct ocelot *ocelot = priv->port.ocelot;
194         int port = priv->chip_port;
195
196         if (!ingress)
197                 return -EOPNOTSUPP;
198
199         switch (f->command) {
200         case FLOW_CLS_REPLACE:
201                 return ocelot_cls_flower_replace(ocelot, port, f, ingress);
202         case FLOW_CLS_DESTROY:
203                 return ocelot_cls_flower_destroy(ocelot, port, f, ingress);
204         case FLOW_CLS_STATS:
205                 return ocelot_cls_flower_stats(ocelot, port, f, ingress);
206         default:
207                 return -EOPNOTSUPP;
208         }
209 }
210
211 static int ocelot_setup_tc_cls_matchall(struct ocelot_port_private *priv,
212                                         struct tc_cls_matchall_offload *f,
213                                         bool ingress)
214 {
215         struct netlink_ext_ack *extack = f->common.extack;
216         struct ocelot *ocelot = priv->port.ocelot;
217         struct ocelot_policer pol = { 0 };
218         struct flow_action_entry *action;
219         int port = priv->chip_port;
220         int err;
221
222         if (!ingress) {
223                 NL_SET_ERR_MSG_MOD(extack, "Only ingress is supported");
224                 return -EOPNOTSUPP;
225         }
226
227         switch (f->command) {
228         case TC_CLSMATCHALL_REPLACE:
229                 if (!flow_offload_has_one_action(&f->rule->action)) {
230                         NL_SET_ERR_MSG_MOD(extack,
231                                            "Only one action is supported");
232                         return -EOPNOTSUPP;
233                 }
234
235                 if (priv->tc.block_shared) {
236                         NL_SET_ERR_MSG_MOD(extack,
237                                            "Rate limit is not supported on shared blocks");
238                         return -EOPNOTSUPP;
239                 }
240
241                 action = &f->rule->action.entries[0];
242
243                 if (action->id != FLOW_ACTION_POLICE) {
244                         NL_SET_ERR_MSG_MOD(extack, "Unsupported action");
245                         return -EOPNOTSUPP;
246                 }
247
248                 if (priv->tc.police_id && priv->tc.police_id != f->cookie) {
249                         NL_SET_ERR_MSG_MOD(extack,
250                                            "Only one policer per port is supported");
251                         return -EEXIST;
252                 }
253
254                 if (action->police.rate_pkt_ps) {
255                         NL_SET_ERR_MSG_MOD(extack,
256                                            "QoS offload not support packets per second");
257                         return -EOPNOTSUPP;
258                 }
259
260                 pol.rate = (u32)div_u64(action->police.rate_bytes_ps, 1000) * 8;
261                 pol.burst = action->police.burst;
262
263                 err = ocelot_port_policer_add(ocelot, port, &pol);
264                 if (err) {
265                         NL_SET_ERR_MSG_MOD(extack, "Could not add policer");
266                         return err;
267                 }
268
269                 priv->tc.police_id = f->cookie;
270                 priv->tc.offload_cnt++;
271                 return 0;
272         case TC_CLSMATCHALL_DESTROY:
273                 if (priv->tc.police_id != f->cookie)
274                         return -ENOENT;
275
276                 err = ocelot_port_policer_del(ocelot, port);
277                 if (err) {
278                         NL_SET_ERR_MSG_MOD(extack,
279                                            "Could not delete policer");
280                         return err;
281                 }
282                 priv->tc.police_id = 0;
283                 priv->tc.offload_cnt--;
284                 return 0;
285         case TC_CLSMATCHALL_STATS:
286         default:
287                 return -EOPNOTSUPP;
288         }
289 }
290
291 static int ocelot_setup_tc_block_cb(enum tc_setup_type type,
292                                     void *type_data,
293                                     void *cb_priv, bool ingress)
294 {
295         struct ocelot_port_private *priv = cb_priv;
296
297         if (!tc_cls_can_offload_and_chain0(priv->dev, type_data))
298                 return -EOPNOTSUPP;
299
300         switch (type) {
301         case TC_SETUP_CLSMATCHALL:
302                 return ocelot_setup_tc_cls_matchall(priv, type_data, ingress);
303         case TC_SETUP_CLSFLOWER:
304                 return ocelot_setup_tc_cls_flower(priv, type_data, ingress);
305         default:
306                 return -EOPNOTSUPP;
307         }
308 }
309
310 static int ocelot_setup_tc_block_cb_ig(enum tc_setup_type type,
311                                        void *type_data,
312                                        void *cb_priv)
313 {
314         return ocelot_setup_tc_block_cb(type, type_data,
315                                         cb_priv, true);
316 }
317
318 static int ocelot_setup_tc_block_cb_eg(enum tc_setup_type type,
319                                        void *type_data,
320                                        void *cb_priv)
321 {
322         return ocelot_setup_tc_block_cb(type, type_data,
323                                         cb_priv, false);
324 }
325
326 static LIST_HEAD(ocelot_block_cb_list);
327
328 static int ocelot_setup_tc_block(struct ocelot_port_private *priv,
329                                  struct flow_block_offload *f)
330 {
331         struct flow_block_cb *block_cb;
332         flow_setup_cb_t *cb;
333
334         if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_INGRESS) {
335                 cb = ocelot_setup_tc_block_cb_ig;
336                 priv->tc.block_shared = f->block_shared;
337         } else if (f->binder_type == FLOW_BLOCK_BINDER_TYPE_CLSACT_EGRESS) {
338                 cb = ocelot_setup_tc_block_cb_eg;
339         } else {
340                 return -EOPNOTSUPP;
341         }
342
343         f->driver_block_list = &ocelot_block_cb_list;
344
345         switch (f->command) {
346         case FLOW_BLOCK_BIND:
347                 if (flow_block_cb_is_busy(cb, priv, &ocelot_block_cb_list))
348                         return -EBUSY;
349
350                 block_cb = flow_block_cb_alloc(cb, priv, priv, NULL);
351                 if (IS_ERR(block_cb))
352                         return PTR_ERR(block_cb);
353
354                 flow_block_cb_add(block_cb, f);
355                 list_add_tail(&block_cb->driver_list, f->driver_block_list);
356                 return 0;
357         case FLOW_BLOCK_UNBIND:
358                 block_cb = flow_block_cb_lookup(f->block, cb, priv);
359                 if (!block_cb)
360                         return -ENOENT;
361
362                 flow_block_cb_remove(block_cb, f);
363                 list_del(&block_cb->driver_list);
364                 return 0;
365         default:
366                 return -EOPNOTSUPP;
367         }
368 }
369
370 static int ocelot_setup_tc(struct net_device *dev, enum tc_setup_type type,
371                            void *type_data)
372 {
373         struct ocelot_port_private *priv = netdev_priv(dev);
374
375         switch (type) {
376         case TC_SETUP_BLOCK:
377                 return ocelot_setup_tc_block(priv, type_data);
378         default:
379                 return -EOPNOTSUPP;
380         }
381         return 0;
382 }
383
384 static void ocelot_port_adjust_link(struct net_device *dev)
385 {
386         struct ocelot_port_private *priv = netdev_priv(dev);
387         struct ocelot *ocelot = priv->port.ocelot;
388         int port = priv->chip_port;
389
390         ocelot_adjust_link(ocelot, port, dev->phydev);
391 }
392
393 static int ocelot_vlan_vid_prepare(struct net_device *dev, u16 vid, bool pvid,
394                                    bool untagged)
395 {
396         struct ocelot_port_private *priv = netdev_priv(dev);
397         struct ocelot_port *ocelot_port = &priv->port;
398         struct ocelot *ocelot = ocelot_port->ocelot;
399         int port = priv->chip_port;
400
401         return ocelot_vlan_prepare(ocelot, port, vid, pvid, untagged);
402 }
403
404 static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
405                                bool untagged)
406 {
407         struct ocelot_port_private *priv = netdev_priv(dev);
408         struct ocelot_port *ocelot_port = &priv->port;
409         struct ocelot *ocelot = ocelot_port->ocelot;
410         int port = priv->chip_port;
411         int ret;
412
413         ret = ocelot_vlan_add(ocelot, port, vid, pvid, untagged);
414         if (ret)
415                 return ret;
416
417         /* Add the port MAC address to with the right VLAN information */
418         ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
419                           ENTRYTYPE_LOCKED);
420
421         return 0;
422 }
423
424 static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
425 {
426         struct ocelot_port_private *priv = netdev_priv(dev);
427         struct ocelot *ocelot = priv->port.ocelot;
428         int port = priv->chip_port;
429         int ret;
430
431         /* 8021q removes VID 0 on module unload for all interfaces
432          * with VLAN filtering feature. We need to keep it to receive
433          * untagged traffic.
434          */
435         if (vid == 0)
436                 return 0;
437
438         ret = ocelot_vlan_del(ocelot, port, vid);
439         if (ret)
440                 return ret;
441
442         /* Del the port MAC address to with the right VLAN information */
443         ocelot_mact_forget(ocelot, dev->dev_addr, vid);
444
445         return 0;
446 }
447
448 static int ocelot_port_open(struct net_device *dev)
449 {
450         struct ocelot_port_private *priv = netdev_priv(dev);
451         struct ocelot_port *ocelot_port = &priv->port;
452         struct ocelot *ocelot = ocelot_port->ocelot;
453         int port = priv->chip_port;
454         int err;
455
456         if (priv->serdes) {
457                 err = phy_set_mode_ext(priv->serdes, PHY_MODE_ETHERNET,
458                                        ocelot_port->phy_mode);
459                 if (err) {
460                         netdev_err(dev, "Could not set mode of SerDes\n");
461                         return err;
462                 }
463         }
464
465         err = phy_connect_direct(dev, priv->phy, &ocelot_port_adjust_link,
466                                  ocelot_port->phy_mode);
467         if (err) {
468                 netdev_err(dev, "Could not attach to PHY\n");
469                 return err;
470         }
471
472         dev->phydev = priv->phy;
473
474         phy_attached_info(priv->phy);
475         phy_start(priv->phy);
476
477         ocelot_port_enable(ocelot, port, priv->phy);
478
479         return 0;
480 }
481
482 static int ocelot_port_stop(struct net_device *dev)
483 {
484         struct ocelot_port_private *priv = netdev_priv(dev);
485         struct ocelot *ocelot = priv->port.ocelot;
486         int port = priv->chip_port;
487
488         phy_disconnect(priv->phy);
489
490         dev->phydev = NULL;
491
492         ocelot_port_disable(ocelot, port);
493
494         return 0;
495 }
496
497 static netdev_tx_t ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
498 {
499         struct ocelot_port_private *priv = netdev_priv(dev);
500         struct ocelot_port *ocelot_port = &priv->port;
501         struct ocelot *ocelot = ocelot_port->ocelot;
502         int port = priv->chip_port;
503         u32 rew_op = 0;
504
505         if (!ocelot_can_inject(ocelot, 0))
506                 return NETDEV_TX_BUSY;
507
508         /* Check if timestamping is needed */
509         if (ocelot->ptp && (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
510                 rew_op = ocelot_port->ptp_cmd;
511
512                 if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
513                         struct sk_buff *clone;
514
515                         clone = skb_clone_sk(skb);
516                         if (!clone) {
517                                 kfree_skb(skb);
518                                 return NETDEV_TX_OK;
519                         }
520
521                         ocelot_port_add_txtstamp_skb(ocelot, port, clone);
522
523                         rew_op |= clone->cb[0] << 3;
524                 }
525         }
526
527         ocelot_port_inject_frame(ocelot, port, 0, rew_op, skb);
528
529         kfree_skb(skb);
530
531         return NETDEV_TX_OK;
532 }
533
534 enum ocelot_action_type {
535         OCELOT_MACT_LEARN,
536         OCELOT_MACT_FORGET,
537 };
538
539 struct ocelot_mact_work_ctx {
540         struct work_struct work;
541         struct ocelot *ocelot;
542         enum ocelot_action_type type;
543         union {
544                 /* OCELOT_MACT_LEARN */
545                 struct {
546                         unsigned char addr[ETH_ALEN];
547                         u16 vid;
548                         enum macaccess_entry_type entry_type;
549                         int pgid;
550                 } learn;
551                 /* OCELOT_MACT_FORGET */
552                 struct {
553                         unsigned char addr[ETH_ALEN];
554                         u16 vid;
555                 } forget;
556         };
557 };
558
559 #define ocelot_work_to_ctx(x) \
560         container_of((x), struct ocelot_mact_work_ctx, work)
561
562 static void ocelot_mact_work(struct work_struct *work)
563 {
564         struct ocelot_mact_work_ctx *w = ocelot_work_to_ctx(work);
565         struct ocelot *ocelot = w->ocelot;
566
567         switch (w->type) {
568         case OCELOT_MACT_LEARN:
569                 ocelot_mact_learn(ocelot, w->learn.pgid, w->learn.addr,
570                                   w->learn.vid, w->learn.entry_type);
571                 break;
572         case OCELOT_MACT_FORGET:
573                 ocelot_mact_forget(ocelot, w->forget.addr, w->forget.vid);
574                 break;
575         default:
576                 break;
577         }
578
579         kfree(w);
580 }
581
582 static int ocelot_enqueue_mact_action(struct ocelot *ocelot,
583                                       const struct ocelot_mact_work_ctx *ctx)
584 {
585         struct ocelot_mact_work_ctx *w = kmemdup(ctx, sizeof(*w), GFP_ATOMIC);
586
587         if (!w)
588                 return -ENOMEM;
589
590         w->ocelot = ocelot;
591         INIT_WORK(&w->work, ocelot_mact_work);
592         queue_work(ocelot->owq, &w->work);
593
594         return 0;
595 }
596
597 static int ocelot_mc_unsync(struct net_device *dev, const unsigned char *addr)
598 {
599         struct ocelot_port_private *priv = netdev_priv(dev);
600         struct ocelot_port *ocelot_port = &priv->port;
601         struct ocelot *ocelot = ocelot_port->ocelot;
602         struct ocelot_mact_work_ctx w;
603
604         ether_addr_copy(w.forget.addr, addr);
605         w.forget.vid = ocelot_port->pvid_vlan.vid;
606         w.type = OCELOT_MACT_FORGET;
607
608         return ocelot_enqueue_mact_action(ocelot, &w);
609 }
610
611 static int ocelot_mc_sync(struct net_device *dev, const unsigned char *addr)
612 {
613         struct ocelot_port_private *priv = netdev_priv(dev);
614         struct ocelot_port *ocelot_port = &priv->port;
615         struct ocelot *ocelot = ocelot_port->ocelot;
616         struct ocelot_mact_work_ctx w;
617
618         ether_addr_copy(w.learn.addr, addr);
619         w.learn.vid = ocelot_port->pvid_vlan.vid;
620         w.learn.pgid = PGID_CPU;
621         w.learn.entry_type = ENTRYTYPE_LOCKED;
622         w.type = OCELOT_MACT_LEARN;
623
624         return ocelot_enqueue_mact_action(ocelot, &w);
625 }
626
627 static void ocelot_set_rx_mode(struct net_device *dev)
628 {
629         struct ocelot_port_private *priv = netdev_priv(dev);
630         struct ocelot *ocelot = priv->port.ocelot;
631         u32 val;
632         int i;
633
634         /* This doesn't handle promiscuous mode because the bridge core is
635          * setting IFF_PROMISC on all slave interfaces and all frames would be
636          * forwarded to the CPU port.
637          */
638         val = GENMASK(ocelot->num_phys_ports - 1, 0);
639         for_each_nonreserved_multicast_dest_pgid(ocelot, i)
640                 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
641
642         __dev_mc_sync(dev, ocelot_mc_sync, ocelot_mc_unsync);
643 }
644
645 static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
646 {
647         struct ocelot_port_private *priv = netdev_priv(dev);
648         struct ocelot_port *ocelot_port = &priv->port;
649         struct ocelot *ocelot = ocelot_port->ocelot;
650         const struct sockaddr *addr = p;
651
652         /* Learn the new net device MAC address in the mac table. */
653         ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data,
654                           ocelot_port->pvid_vlan.vid, ENTRYTYPE_LOCKED);
655         /* Then forget the previous one. */
656         ocelot_mact_forget(ocelot, dev->dev_addr, ocelot_port->pvid_vlan.vid);
657
658         ether_addr_copy(dev->dev_addr, addr->sa_data);
659         return 0;
660 }
661
662 static void ocelot_get_stats64(struct net_device *dev,
663                                struct rtnl_link_stats64 *stats)
664 {
665         struct ocelot_port_private *priv = netdev_priv(dev);
666         struct ocelot *ocelot = priv->port.ocelot;
667         int port = priv->chip_port;
668
669         /* Configure the port to read the stats from */
670         ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port),
671                      SYS_STAT_CFG);
672
673         /* Get Rx stats */
674         stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
675         stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
676                             ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
677                             ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
678                             ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
679                             ocelot_read(ocelot, SYS_COUNT_RX_64) +
680                             ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
681                             ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
682                             ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
683                             ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
684                             ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
685         stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
686         stats->rx_dropped = dev->stats.rx_dropped;
687
688         /* Get Tx stats */
689         stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
690         stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
691                             ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
692                             ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
693                             ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
694                             ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
695                             ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
696         stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
697                             ocelot_read(ocelot, SYS_COUNT_TX_AGING);
698         stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
699 }
700
701 static int ocelot_port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
702                                struct net_device *dev,
703                                const unsigned char *addr,
704                                u16 vid, u16 flags,
705                                struct netlink_ext_ack *extack)
706 {
707         struct ocelot_port_private *priv = netdev_priv(dev);
708         struct ocelot *ocelot = priv->port.ocelot;
709         int port = priv->chip_port;
710
711         return ocelot_fdb_add(ocelot, port, addr, vid);
712 }
713
714 static int ocelot_port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
715                                struct net_device *dev,
716                                const unsigned char *addr, u16 vid)
717 {
718         struct ocelot_port_private *priv = netdev_priv(dev);
719         struct ocelot *ocelot = priv->port.ocelot;
720         int port = priv->chip_port;
721
722         return ocelot_fdb_del(ocelot, port, addr, vid);
723 }
724
725 static int ocelot_port_fdb_dump(struct sk_buff *skb,
726                                 struct netlink_callback *cb,
727                                 struct net_device *dev,
728                                 struct net_device *filter_dev, int *idx)
729 {
730         struct ocelot_port_private *priv = netdev_priv(dev);
731         struct ocelot *ocelot = priv->port.ocelot;
732         struct ocelot_dump_ctx dump = {
733                 .dev = dev,
734                 .skb = skb,
735                 .cb = cb,
736                 .idx = *idx,
737         };
738         int port = priv->chip_port;
739         int ret;
740
741         ret = ocelot_fdb_dump(ocelot, port, ocelot_port_fdb_do_dump, &dump);
742
743         *idx = dump.idx;
744
745         return ret;
746 }
747
748 static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
749                                   u16 vid)
750 {
751         return ocelot_vlan_vid_add(dev, vid, false, false);
752 }
753
754 static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
755                                    u16 vid)
756 {
757         return ocelot_vlan_vid_del(dev, vid);
758 }
759
760 static void ocelot_vlan_mode(struct ocelot *ocelot, int port,
761                              netdev_features_t features)
762 {
763         u32 val;
764
765         /* Filtering */
766         val = ocelot_read(ocelot, ANA_VLANMASK);
767         if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
768                 val |= BIT(port);
769         else
770                 val &= ~BIT(port);
771         ocelot_write(ocelot, val, ANA_VLANMASK);
772 }
773
774 static int ocelot_set_features(struct net_device *dev,
775                                netdev_features_t features)
776 {
777         netdev_features_t changed = dev->features ^ features;
778         struct ocelot_port_private *priv = netdev_priv(dev);
779         struct ocelot *ocelot = priv->port.ocelot;
780         int port = priv->chip_port;
781
782         if ((dev->features & NETIF_F_HW_TC) > (features & NETIF_F_HW_TC) &&
783             priv->tc.offload_cnt) {
784                 netdev_err(dev,
785                            "Cannot disable HW TC offload while offloads active\n");
786                 return -EBUSY;
787         }
788
789         if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
790                 ocelot_vlan_mode(ocelot, port, features);
791
792         return 0;
793 }
794
795 static int ocelot_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
796 {
797         struct ocelot_port_private *priv = netdev_priv(dev);
798         struct ocelot *ocelot = priv->port.ocelot;
799         int port = priv->chip_port;
800
801         /* If the attached PHY device isn't capable of timestamping operations,
802          * use our own (when possible).
803          */
804         if (!phy_has_hwtstamp(dev->phydev) && ocelot->ptp) {
805                 switch (cmd) {
806                 case SIOCSHWTSTAMP:
807                         return ocelot_hwstamp_set(ocelot, port, ifr);
808                 case SIOCGHWTSTAMP:
809                         return ocelot_hwstamp_get(ocelot, port, ifr);
810                 }
811         }
812
813         return phy_mii_ioctl(dev->phydev, ifr, cmd);
814 }
815
816 static const struct net_device_ops ocelot_port_netdev_ops = {
817         .ndo_open                       = ocelot_port_open,
818         .ndo_stop                       = ocelot_port_stop,
819         .ndo_start_xmit                 = ocelot_port_xmit,
820         .ndo_set_rx_mode                = ocelot_set_rx_mode,
821         .ndo_set_mac_address            = ocelot_port_set_mac_address,
822         .ndo_get_stats64                = ocelot_get_stats64,
823         .ndo_fdb_add                    = ocelot_port_fdb_add,
824         .ndo_fdb_del                    = ocelot_port_fdb_del,
825         .ndo_fdb_dump                   = ocelot_port_fdb_dump,
826         .ndo_vlan_rx_add_vid            = ocelot_vlan_rx_add_vid,
827         .ndo_vlan_rx_kill_vid           = ocelot_vlan_rx_kill_vid,
828         .ndo_set_features               = ocelot_set_features,
829         .ndo_setup_tc                   = ocelot_setup_tc,
830         .ndo_do_ioctl                   = ocelot_ioctl,
831         .ndo_get_devlink_port           = ocelot_get_devlink_port,
832 };
833
834 struct net_device *ocelot_port_to_netdev(struct ocelot *ocelot, int port)
835 {
836         struct ocelot_port *ocelot_port = ocelot->ports[port];
837         struct ocelot_port_private *priv;
838
839         if (!ocelot_port)
840                 return NULL;
841
842         priv = container_of(ocelot_port, struct ocelot_port_private, port);
843
844         return priv->dev;
845 }
846
847 /* Checks if the net_device instance given to us originates from our driver */
848 static bool ocelot_netdevice_dev_check(const struct net_device *dev)
849 {
850         return dev->netdev_ops == &ocelot_port_netdev_ops;
851 }
852
853 int ocelot_netdev_to_port(struct net_device *dev)
854 {
855         struct ocelot_port_private *priv;
856
857         if (!dev || !ocelot_netdevice_dev_check(dev))
858                 return -EINVAL;
859
860         priv = netdev_priv(dev);
861
862         return priv->chip_port;
863 }
864
865 static void ocelot_port_get_strings(struct net_device *netdev, u32 sset,
866                                     u8 *data)
867 {
868         struct ocelot_port_private *priv = netdev_priv(netdev);
869         struct ocelot *ocelot = priv->port.ocelot;
870         int port = priv->chip_port;
871
872         ocelot_get_strings(ocelot, port, sset, data);
873 }
874
875 static void ocelot_port_get_ethtool_stats(struct net_device *dev,
876                                           struct ethtool_stats *stats,
877                                           u64 *data)
878 {
879         struct ocelot_port_private *priv = netdev_priv(dev);
880         struct ocelot *ocelot = priv->port.ocelot;
881         int port = priv->chip_port;
882
883         ocelot_get_ethtool_stats(ocelot, port, data);
884 }
885
886 static int ocelot_port_get_sset_count(struct net_device *dev, int sset)
887 {
888         struct ocelot_port_private *priv = netdev_priv(dev);
889         struct ocelot *ocelot = priv->port.ocelot;
890         int port = priv->chip_port;
891
892         return ocelot_get_sset_count(ocelot, port, sset);
893 }
894
895 static int ocelot_port_get_ts_info(struct net_device *dev,
896                                    struct ethtool_ts_info *info)
897 {
898         struct ocelot_port_private *priv = netdev_priv(dev);
899         struct ocelot *ocelot = priv->port.ocelot;
900         int port = priv->chip_port;
901
902         if (!ocelot->ptp)
903                 return ethtool_op_get_ts_info(dev, info);
904
905         return ocelot_get_ts_info(ocelot, port, info);
906 }
907
908 static const struct ethtool_ops ocelot_ethtool_ops = {
909         .get_strings            = ocelot_port_get_strings,
910         .get_ethtool_stats      = ocelot_port_get_ethtool_stats,
911         .get_sset_count         = ocelot_port_get_sset_count,
912         .get_link_ksettings     = phy_ethtool_get_link_ksettings,
913         .set_link_ksettings     = phy_ethtool_set_link_ksettings,
914         .get_ts_info            = ocelot_port_get_ts_info,
915 };
916
917 static void ocelot_port_attr_stp_state_set(struct ocelot *ocelot, int port,
918                                            u8 state)
919 {
920         ocelot_bridge_stp_state_set(ocelot, port, state);
921 }
922
923 static void ocelot_port_attr_ageing_set(struct ocelot *ocelot, int port,
924                                         unsigned long ageing_clock_t)
925 {
926         unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
927         u32 ageing_time = jiffies_to_msecs(ageing_jiffies);
928
929         ocelot_set_ageing_time(ocelot, ageing_time);
930 }
931
932 static void ocelot_port_attr_mc_set(struct ocelot *ocelot, int port, bool mc)
933 {
934         u32 cpu_fwd_mcast = ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
935                             ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
936                             ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
937         u32 val = 0;
938
939         if (mc)
940                 val = cpu_fwd_mcast;
941
942         ocelot_rmw_gix(ocelot, val, cpu_fwd_mcast,
943                        ANA_PORT_CPU_FWD_CFG, port);
944 }
945
946 static int ocelot_port_attr_set(struct net_device *dev,
947                                 const struct switchdev_attr *attr,
948                                 struct netlink_ext_ack *extack)
949 {
950         struct ocelot_port_private *priv = netdev_priv(dev);
951         struct ocelot *ocelot = priv->port.ocelot;
952         int port = priv->chip_port;
953         int err = 0;
954
955         switch (attr->id) {
956         case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
957                 ocelot_port_attr_stp_state_set(ocelot, port, attr->u.stp_state);
958                 break;
959         case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
960                 ocelot_port_attr_ageing_set(ocelot, port, attr->u.ageing_time);
961                 break;
962         case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
963                 ocelot_port_vlan_filtering(ocelot, port, attr->u.vlan_filtering);
964                 break;
965         case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
966                 ocelot_port_attr_mc_set(ocelot, port, !attr->u.mc_disabled);
967                 break;
968         case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
969                 err = ocelot_port_pre_bridge_flags(ocelot, port,
970                                                    attr->u.brport_flags);
971                 break;
972         case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
973                 ocelot_port_bridge_flags(ocelot, port, attr->u.brport_flags);
974                 break;
975         default:
976                 err = -EOPNOTSUPP;
977                 break;
978         }
979
980         return err;
981 }
982
983 static int ocelot_port_obj_add_vlan(struct net_device *dev,
984                                     const struct switchdev_obj_port_vlan *vlan)
985 {
986         bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
987         bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
988         int ret;
989
990         ret = ocelot_vlan_vid_prepare(dev, vlan->vid, pvid, untagged);
991         if (ret)
992                 return ret;
993
994         return ocelot_vlan_vid_add(dev, vlan->vid, pvid, untagged);
995 }
996
997 static int ocelot_port_obj_add_mdb(struct net_device *dev,
998                                    const struct switchdev_obj_port_mdb *mdb)
999 {
1000         struct ocelot_port_private *priv = netdev_priv(dev);
1001         struct ocelot_port *ocelot_port = &priv->port;
1002         struct ocelot *ocelot = ocelot_port->ocelot;
1003         int port = priv->chip_port;
1004
1005         return ocelot_port_mdb_add(ocelot, port, mdb);
1006 }
1007
1008 static int ocelot_port_obj_del_mdb(struct net_device *dev,
1009                                    const struct switchdev_obj_port_mdb *mdb)
1010 {
1011         struct ocelot_port_private *priv = netdev_priv(dev);
1012         struct ocelot_port *ocelot_port = &priv->port;
1013         struct ocelot *ocelot = ocelot_port->ocelot;
1014         int port = priv->chip_port;
1015
1016         return ocelot_port_mdb_del(ocelot, port, mdb);
1017 }
1018
1019 static int ocelot_port_obj_mrp_add(struct net_device *dev,
1020                                    const struct switchdev_obj_mrp *mrp)
1021 {
1022         struct ocelot_port_private *priv = netdev_priv(dev);
1023         struct ocelot_port *ocelot_port = &priv->port;
1024         struct ocelot *ocelot = ocelot_port->ocelot;
1025         int port = priv->chip_port;
1026
1027         return ocelot_mrp_add(ocelot, port, mrp);
1028 }
1029
1030 static int ocelot_port_obj_mrp_del(struct net_device *dev,
1031                                    const struct switchdev_obj_mrp *mrp)
1032 {
1033         struct ocelot_port_private *priv = netdev_priv(dev);
1034         struct ocelot_port *ocelot_port = &priv->port;
1035         struct ocelot *ocelot = ocelot_port->ocelot;
1036         int port = priv->chip_port;
1037
1038         return ocelot_mrp_del(ocelot, port, mrp);
1039 }
1040
1041 static int
1042 ocelot_port_obj_mrp_add_ring_role(struct net_device *dev,
1043                                   const struct switchdev_obj_ring_role_mrp *mrp)
1044 {
1045         struct ocelot_port_private *priv = netdev_priv(dev);
1046         struct ocelot_port *ocelot_port = &priv->port;
1047         struct ocelot *ocelot = ocelot_port->ocelot;
1048         int port = priv->chip_port;
1049
1050         return ocelot_mrp_add_ring_role(ocelot, port, mrp);
1051 }
1052
1053 static int
1054 ocelot_port_obj_mrp_del_ring_role(struct net_device *dev,
1055                                   const struct switchdev_obj_ring_role_mrp *mrp)
1056 {
1057         struct ocelot_port_private *priv = netdev_priv(dev);
1058         struct ocelot_port *ocelot_port = &priv->port;
1059         struct ocelot *ocelot = ocelot_port->ocelot;
1060         int port = priv->chip_port;
1061
1062         return ocelot_mrp_del_ring_role(ocelot, port, mrp);
1063 }
1064
1065 static int ocelot_port_obj_add(struct net_device *dev,
1066                                const struct switchdev_obj *obj,
1067                                struct netlink_ext_ack *extack)
1068 {
1069         int ret = 0;
1070
1071         switch (obj->id) {
1072         case SWITCHDEV_OBJ_ID_PORT_VLAN:
1073                 ret = ocelot_port_obj_add_vlan(dev,
1074                                                SWITCHDEV_OBJ_PORT_VLAN(obj));
1075                 break;
1076         case SWITCHDEV_OBJ_ID_PORT_MDB:
1077                 ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1078                 break;
1079         case SWITCHDEV_OBJ_ID_MRP:
1080                 ret = ocelot_port_obj_mrp_add(dev, SWITCHDEV_OBJ_MRP(obj));
1081                 break;
1082         case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1083                 ret = ocelot_port_obj_mrp_add_ring_role(dev,
1084                                                         SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1085                 break;
1086         default:
1087                 return -EOPNOTSUPP;
1088         }
1089
1090         return ret;
1091 }
1092
1093 static int ocelot_port_obj_del(struct net_device *dev,
1094                                const struct switchdev_obj *obj)
1095 {
1096         int ret = 0;
1097
1098         switch (obj->id) {
1099         case SWITCHDEV_OBJ_ID_PORT_VLAN:
1100                 ret = ocelot_vlan_vid_del(dev,
1101                                           SWITCHDEV_OBJ_PORT_VLAN(obj)->vid);
1102                 break;
1103         case SWITCHDEV_OBJ_ID_PORT_MDB:
1104                 ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1105                 break;
1106         case SWITCHDEV_OBJ_ID_MRP:
1107                 ret = ocelot_port_obj_mrp_del(dev, SWITCHDEV_OBJ_MRP(obj));
1108                 break;
1109         case SWITCHDEV_OBJ_ID_RING_ROLE_MRP:
1110                 ret = ocelot_port_obj_mrp_del_ring_role(dev,
1111                                                         SWITCHDEV_OBJ_RING_ROLE_MRP(obj));
1112                 break;
1113         default:
1114                 return -EOPNOTSUPP;
1115         }
1116
1117         return ret;
1118 }
1119
1120 static int ocelot_netdevice_bridge_join(struct ocelot *ocelot, int port,
1121                                         struct net_device *bridge)
1122 {
1123         struct switchdev_brport_flags flags;
1124         int err;
1125
1126         flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1127         flags.val = flags.mask;
1128
1129         err = ocelot_port_bridge_join(ocelot, port, bridge);
1130         if (err)
1131                 return err;
1132
1133         ocelot_port_bridge_flags(ocelot, port, flags);
1134
1135         return 0;
1136 }
1137
1138 static int ocelot_netdevice_bridge_leave(struct ocelot *ocelot, int port,
1139                                          struct net_device *bridge)
1140 {
1141         struct switchdev_brport_flags flags;
1142         int err;
1143
1144         flags.mask = BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD;
1145         flags.val = flags.mask & ~BR_LEARNING;
1146
1147         err = ocelot_port_bridge_leave(ocelot, port, bridge);
1148
1149         ocelot_port_bridge_flags(ocelot, port, flags);
1150
1151         return err;
1152 }
1153
1154 static int ocelot_netdevice_changeupper(struct net_device *dev,
1155                                         struct netdev_notifier_changeupper_info *info)
1156 {
1157         struct ocelot_port_private *priv = netdev_priv(dev);
1158         struct ocelot_port *ocelot_port = &priv->port;
1159         struct ocelot *ocelot = ocelot_port->ocelot;
1160         int port = priv->chip_port;
1161         int err = 0;
1162
1163         if (netif_is_bridge_master(info->upper_dev)) {
1164                 if (info->linking) {
1165                         err = ocelot_netdevice_bridge_join(ocelot, port,
1166                                                            info->upper_dev);
1167                 } else {
1168                         err = ocelot_netdevice_bridge_leave(ocelot, port,
1169                                                             info->upper_dev);
1170                 }
1171         }
1172         if (netif_is_lag_master(info->upper_dev)) {
1173                 if (info->linking) {
1174                         err = ocelot_port_lag_join(ocelot, port,
1175                                                    info->upper_dev,
1176                                                    info->upper_info);
1177                         if (err == -EOPNOTSUPP) {
1178                                 NL_SET_ERR_MSG_MOD(info->info.extack,
1179                                                    "Offloading not supported");
1180                                 err = 0;
1181                         }
1182                 } else {
1183                         ocelot_port_lag_leave(ocelot, port,
1184                                               info->upper_dev);
1185                 }
1186         }
1187
1188         return notifier_from_errno(err);
1189 }
1190
1191 static int
1192 ocelot_netdevice_lag_changeupper(struct net_device *dev,
1193                                  struct netdev_notifier_changeupper_info *info)
1194 {
1195         struct net_device *lower;
1196         struct list_head *iter;
1197         int err = NOTIFY_DONE;
1198
1199         netdev_for_each_lower_dev(dev, lower, iter) {
1200                 err = ocelot_netdevice_changeupper(lower, info);
1201                 if (err)
1202                         return notifier_from_errno(err);
1203         }
1204
1205         return NOTIFY_DONE;
1206 }
1207
1208 static int
1209 ocelot_netdevice_changelowerstate(struct net_device *dev,
1210                                   struct netdev_lag_lower_state_info *info)
1211 {
1212         struct ocelot_port_private *priv = netdev_priv(dev);
1213         bool is_active = info->link_up && info->tx_enabled;
1214         struct ocelot_port *ocelot_port = &priv->port;
1215         struct ocelot *ocelot = ocelot_port->ocelot;
1216         int port = priv->chip_port;
1217
1218         if (!ocelot_port->bond)
1219                 return NOTIFY_DONE;
1220
1221         if (ocelot_port->lag_tx_active == is_active)
1222                 return NOTIFY_DONE;
1223
1224         ocelot_port_lag_change(ocelot, port, is_active);
1225
1226         return NOTIFY_OK;
1227 }
1228
1229 static int ocelot_netdevice_event(struct notifier_block *unused,
1230                                   unsigned long event, void *ptr)
1231 {
1232         struct net_device *dev = netdev_notifier_info_to_dev(ptr);
1233
1234         switch (event) {
1235         case NETDEV_CHANGEUPPER: {
1236                 struct netdev_notifier_changeupper_info *info = ptr;
1237
1238                 if (ocelot_netdevice_dev_check(dev))
1239                         return ocelot_netdevice_changeupper(dev, info);
1240
1241                 if (netif_is_lag_master(dev))
1242                         return ocelot_netdevice_lag_changeupper(dev, info);
1243
1244                 break;
1245         }
1246         case NETDEV_CHANGELOWERSTATE: {
1247                 struct netdev_notifier_changelowerstate_info *info = ptr;
1248
1249                 if (!ocelot_netdevice_dev_check(dev))
1250                         break;
1251
1252                 return ocelot_netdevice_changelowerstate(dev,
1253                                                          info->lower_state_info);
1254         }
1255         default:
1256                 break;
1257         }
1258
1259         return NOTIFY_DONE;
1260 }
1261
1262 struct notifier_block ocelot_netdevice_nb __read_mostly = {
1263         .notifier_call = ocelot_netdevice_event,
1264 };
1265
1266 static int ocelot_switchdev_event(struct notifier_block *unused,
1267                                   unsigned long event, void *ptr)
1268 {
1269         struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1270         int err;
1271
1272         switch (event) {
1273         case SWITCHDEV_PORT_ATTR_SET:
1274                 err = switchdev_handle_port_attr_set(dev, ptr,
1275                                                      ocelot_netdevice_dev_check,
1276                                                      ocelot_port_attr_set);
1277                 return notifier_from_errno(err);
1278         }
1279
1280         return NOTIFY_DONE;
1281 }
1282
1283 struct notifier_block ocelot_switchdev_nb __read_mostly = {
1284         .notifier_call = ocelot_switchdev_event,
1285 };
1286
1287 static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
1288                                            unsigned long event, void *ptr)
1289 {
1290         struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1291         int err;
1292
1293         switch (event) {
1294                 /* Blocking events. */
1295         case SWITCHDEV_PORT_OBJ_ADD:
1296                 err = switchdev_handle_port_obj_add(dev, ptr,
1297                                                     ocelot_netdevice_dev_check,
1298                                                     ocelot_port_obj_add);
1299                 return notifier_from_errno(err);
1300         case SWITCHDEV_PORT_OBJ_DEL:
1301                 err = switchdev_handle_port_obj_del(dev, ptr,
1302                                                     ocelot_netdevice_dev_check,
1303                                                     ocelot_port_obj_del);
1304                 return notifier_from_errno(err);
1305         case SWITCHDEV_PORT_ATTR_SET:
1306                 err = switchdev_handle_port_attr_set(dev, ptr,
1307                                                      ocelot_netdevice_dev_check,
1308                                                      ocelot_port_attr_set);
1309                 return notifier_from_errno(err);
1310         }
1311
1312         return NOTIFY_DONE;
1313 }
1314
1315 struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
1316         .notifier_call = ocelot_switchdev_blocking_event,
1317 };
1318
1319 int ocelot_probe_port(struct ocelot *ocelot, int port, struct regmap *target,
1320                       struct phy_device *phy)
1321 {
1322         struct ocelot_port_private *priv;
1323         struct ocelot_port *ocelot_port;
1324         struct net_device *dev;
1325         int err;
1326
1327         dev = alloc_etherdev(sizeof(struct ocelot_port_private));
1328         if (!dev)
1329                 return -ENOMEM;
1330         SET_NETDEV_DEV(dev, ocelot->dev);
1331         priv = netdev_priv(dev);
1332         priv->dev = dev;
1333         priv->phy = phy;
1334         priv->chip_port = port;
1335         ocelot_port = &priv->port;
1336         ocelot_port->ocelot = ocelot;
1337         ocelot_port->target = target;
1338         ocelot->ports[port] = ocelot_port;
1339
1340         dev->netdev_ops = &ocelot_port_netdev_ops;
1341         dev->ethtool_ops = &ocelot_ethtool_ops;
1342
1343         dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS |
1344                 NETIF_F_HW_TC;
1345         dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_HW_TC;
1346
1347         memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
1348         dev->dev_addr[ETH_ALEN - 1] += port;
1349         ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr,
1350                           ocelot_port->pvid_vlan.vid, ENTRYTYPE_LOCKED);
1351
1352         ocelot_init_port(ocelot, port);
1353
1354         err = register_netdev(dev);
1355         if (err) {
1356                 dev_err(ocelot->dev, "register_netdev failed\n");
1357                 free_netdev(dev);
1358                 ocelot->ports[port] = NULL;
1359                 return err;
1360         }
1361
1362         return 0;
1363 }
1364
1365 void ocelot_release_port(struct ocelot_port *ocelot_port)
1366 {
1367         struct ocelot_port_private *priv = container_of(ocelot_port,
1368                                                 struct ocelot_port_private,
1369                                                 port);
1370
1371         unregister_netdev(priv->dev);
1372         free_netdev(priv->dev);
1373 }