46aa37093e86e9231b0264509376680651b157af
[linux-2.6-microblaze.git] / drivers / staging / fsl-dpaa2 / ethsw / ethsw.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * DPAA2 Ethernet Switch driver
4  *
5  * Copyright 2014-2016 Freescale Semiconductor Inc.
6  * Copyright 2017-2018 NXP
7  *
8  */
9
10 #include <linux/module.h>
11
12 #include <linux/interrupt.h>
13 #include <linux/msi.h>
14 #include <linux/kthread.h>
15 #include <linux/workqueue.h>
16
17 #include <linux/fsl/mc.h>
18
19 #include "ethsw.h"
20
21 /* Minimal supported DPSW version */
22 #define DPSW_MIN_VER_MAJOR              8
23 #define DPSW_MIN_VER_MINOR              1
24
25 #define DEFAULT_VLAN_ID                 1
26
27 static int ethsw_add_vlan(struct ethsw_core *ethsw, u16 vid)
28 {
29         int err;
30
31         struct dpsw_vlan_cfg    vcfg = {
32                 .fdb_id = 0,
33         };
34
35         err = dpsw_vlan_add(ethsw->mc_io, 0,
36                             ethsw->dpsw_handle, vid, &vcfg);
37         if (err) {
38                 dev_err(ethsw->dev, "dpsw_vlan_add err %d\n", err);
39                 return err;
40         }
41         ethsw->vlans[vid] = ETHSW_VLAN_MEMBER;
42
43         return 0;
44 }
45
46 static int ethsw_port_set_pvid(struct ethsw_port_priv *port_priv, u16 pvid)
47 {
48         struct ethsw_core *ethsw = port_priv->ethsw_data;
49         struct net_device *netdev = port_priv->netdev;
50         struct dpsw_tci_cfg tci_cfg = { 0 };
51         bool is_oper;
52         int err, ret;
53
54         err = dpsw_if_get_tci(ethsw->mc_io, 0, ethsw->dpsw_handle,
55                               port_priv->idx, &tci_cfg);
56         if (err) {
57                 netdev_err(netdev, "dpsw_if_get_tci err %d\n", err);
58                 return err;
59         }
60
61         tci_cfg.vlan_id = pvid;
62
63         /* Interface needs to be down to change PVID */
64         is_oper = netif_oper_up(netdev);
65         if (is_oper) {
66                 err = dpsw_if_disable(ethsw->mc_io, 0,
67                                       ethsw->dpsw_handle,
68                                       port_priv->idx);
69                 if (err) {
70                         netdev_err(netdev, "dpsw_if_disable err %d\n", err);
71                         return err;
72                 }
73         }
74
75         err = dpsw_if_set_tci(ethsw->mc_io, 0, ethsw->dpsw_handle,
76                               port_priv->idx, &tci_cfg);
77         if (err) {
78                 netdev_err(netdev, "dpsw_if_set_tci err %d\n", err);
79                 goto set_tci_error;
80         }
81
82         /* Delete previous PVID info and mark the new one */
83         port_priv->vlans[port_priv->pvid] &= ~ETHSW_VLAN_PVID;
84         port_priv->vlans[pvid] |= ETHSW_VLAN_PVID;
85         port_priv->pvid = pvid;
86
87 set_tci_error:
88         if (is_oper) {
89                 ret = dpsw_if_enable(ethsw->mc_io, 0,
90                                      ethsw->dpsw_handle,
91                                      port_priv->idx);
92                 if (ret) {
93                         netdev_err(netdev, "dpsw_if_enable err %d\n", ret);
94                         return ret;
95                 }
96         }
97
98         return err;
99 }
100
101 static int ethsw_port_add_vlan(struct ethsw_port_priv *port_priv,
102                                u16 vid, u16 flags)
103 {
104         struct ethsw_core *ethsw = port_priv->ethsw_data;
105         struct net_device *netdev = port_priv->netdev;
106         struct dpsw_vlan_if_cfg vcfg;
107         int err;
108
109         if (port_priv->vlans[vid]) {
110                 netdev_warn(netdev, "VLAN %d already configured\n", vid);
111                 return -EEXIST;
112         }
113
114         vcfg.num_ifs = 1;
115         vcfg.if_id[0] = port_priv->idx;
116         err = dpsw_vlan_add_if(ethsw->mc_io, 0, ethsw->dpsw_handle, vid, &vcfg);
117         if (err) {
118                 netdev_err(netdev, "dpsw_vlan_add_if err %d\n", err);
119                 return err;
120         }
121
122         port_priv->vlans[vid] = ETHSW_VLAN_MEMBER;
123
124         if (flags & BRIDGE_VLAN_INFO_UNTAGGED) {
125                 err = dpsw_vlan_add_if_untagged(ethsw->mc_io, 0,
126                                                 ethsw->dpsw_handle,
127                                                 vid, &vcfg);
128                 if (err) {
129                         netdev_err(netdev,
130                                    "dpsw_vlan_add_if_untagged err %d\n", err);
131                         return err;
132                 }
133                 port_priv->vlans[vid] |= ETHSW_VLAN_UNTAGGED;
134         }
135
136         if (flags & BRIDGE_VLAN_INFO_PVID) {
137                 err = ethsw_port_set_pvid(port_priv, vid);
138                 if (err)
139                         return err;
140         }
141
142         return 0;
143 }
144
145 static int ethsw_set_learning(struct ethsw_core *ethsw, bool enable)
146 {
147         enum dpsw_fdb_learning_mode learn_mode;
148         int err;
149
150         if (enable)
151                 learn_mode = DPSW_FDB_LEARNING_MODE_HW;
152         else
153                 learn_mode = DPSW_FDB_LEARNING_MODE_DIS;
154
155         err = dpsw_fdb_set_learning_mode(ethsw->mc_io, 0, ethsw->dpsw_handle, 0,
156                                          learn_mode);
157         if (err) {
158                 dev_err(ethsw->dev, "dpsw_fdb_set_learning_mode err %d\n", err);
159                 return err;
160         }
161         ethsw->learning = enable;
162
163         return 0;
164 }
165
166 static int ethsw_port_set_flood(struct ethsw_port_priv *port_priv, bool enable)
167 {
168         int err;
169
170         err = dpsw_if_set_flooding(port_priv->ethsw_data->mc_io, 0,
171                                    port_priv->ethsw_data->dpsw_handle,
172                                    port_priv->idx, enable);
173         if (err) {
174                 netdev_err(port_priv->netdev,
175                            "dpsw_if_set_flooding err %d\n", err);
176                 return err;
177         }
178         port_priv->flood = enable;
179
180         return 0;
181 }
182
183 static int ethsw_port_set_stp_state(struct ethsw_port_priv *port_priv, u8 state)
184 {
185         struct dpsw_stp_cfg stp_cfg = {
186                 .vlan_id = DEFAULT_VLAN_ID,
187                 .state = state,
188         };
189         int err;
190
191         if (!netif_oper_up(port_priv->netdev) || state == port_priv->stp_state)
192                 return 0;       /* Nothing to do */
193
194         err = dpsw_if_set_stp(port_priv->ethsw_data->mc_io, 0,
195                               port_priv->ethsw_data->dpsw_handle,
196                               port_priv->idx, &stp_cfg);
197         if (err) {
198                 netdev_err(port_priv->netdev,
199                            "dpsw_if_set_stp err %d\n", err);
200                 return err;
201         }
202
203         port_priv->stp_state = state;
204
205         return 0;
206 }
207
208 static int ethsw_dellink_switch(struct ethsw_core *ethsw, u16 vid)
209 {
210         struct ethsw_port_priv *ppriv_local = NULL;
211         int i, err;
212
213         if (!ethsw->vlans[vid])
214                 return -ENOENT;
215
216         err = dpsw_vlan_remove(ethsw->mc_io, 0, ethsw->dpsw_handle, vid);
217         if (err) {
218                 dev_err(ethsw->dev, "dpsw_vlan_remove err %d\n", err);
219                 return err;
220         }
221         ethsw->vlans[vid] = 0;
222
223         for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
224                 ppriv_local = ethsw->ports[i];
225                 ppriv_local->vlans[vid] = 0;
226         }
227
228         return 0;
229 }
230
231 static int ethsw_port_fdb_add_uc(struct ethsw_port_priv *port_priv,
232                                  const unsigned char *addr)
233 {
234         struct dpsw_fdb_unicast_cfg entry = {0};
235         int err;
236
237         entry.if_egress = port_priv->idx;
238         entry.type = DPSW_FDB_ENTRY_STATIC;
239         ether_addr_copy(entry.mac_addr, addr);
240
241         err = dpsw_fdb_add_unicast(port_priv->ethsw_data->mc_io, 0,
242                                    port_priv->ethsw_data->dpsw_handle,
243                                    0, &entry);
244         if (err)
245                 netdev_err(port_priv->netdev,
246                            "dpsw_fdb_add_unicast err %d\n", err);
247         return err;
248 }
249
250 static int ethsw_port_fdb_del_uc(struct ethsw_port_priv *port_priv,
251                                  const unsigned char *addr)
252 {
253         struct dpsw_fdb_unicast_cfg entry = {0};
254         int err;
255
256         entry.if_egress = port_priv->idx;
257         entry.type = DPSW_FDB_ENTRY_STATIC;
258         ether_addr_copy(entry.mac_addr, addr);
259
260         err = dpsw_fdb_remove_unicast(port_priv->ethsw_data->mc_io, 0,
261                                       port_priv->ethsw_data->dpsw_handle,
262                                       0, &entry);
263         /* Silently discard error for calling multiple times the del command */
264         if (err && err != -ENXIO)
265                 netdev_err(port_priv->netdev,
266                            "dpsw_fdb_remove_unicast err %d\n", err);
267         return err;
268 }
269
270 static int ethsw_port_fdb_add_mc(struct ethsw_port_priv *port_priv,
271                                  const unsigned char *addr)
272 {
273         struct dpsw_fdb_multicast_cfg entry = {0};
274         int err;
275
276         ether_addr_copy(entry.mac_addr, addr);
277         entry.type = DPSW_FDB_ENTRY_STATIC;
278         entry.num_ifs = 1;
279         entry.if_id[0] = port_priv->idx;
280
281         err = dpsw_fdb_add_multicast(port_priv->ethsw_data->mc_io, 0,
282                                      port_priv->ethsw_data->dpsw_handle,
283                                      0, &entry);
284         /* Silently discard error for calling multiple times the add command */
285         if (err && err != -ENXIO)
286                 netdev_err(port_priv->netdev, "dpsw_fdb_add_multicast err %d\n",
287                            err);
288         return err;
289 }
290
291 static int ethsw_port_fdb_del_mc(struct ethsw_port_priv *port_priv,
292                                  const unsigned char *addr)
293 {
294         struct dpsw_fdb_multicast_cfg entry = {0};
295         int err;
296
297         ether_addr_copy(entry.mac_addr, addr);
298         entry.type = DPSW_FDB_ENTRY_STATIC;
299         entry.num_ifs = 1;
300         entry.if_id[0] = port_priv->idx;
301
302         err = dpsw_fdb_remove_multicast(port_priv->ethsw_data->mc_io, 0,
303                                         port_priv->ethsw_data->dpsw_handle,
304                                         0, &entry);
305         /* Silently discard error for calling multiple times the del command */
306         if (err && err != -ENAVAIL)
307                 netdev_err(port_priv->netdev,
308                            "dpsw_fdb_remove_multicast err %d\n", err);
309         return err;
310 }
311
312 static int port_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
313                         struct net_device *dev, const unsigned char *addr,
314                         u16 vid, u16 flags,
315                         struct netlink_ext_ack *extack)
316 {
317         if (is_unicast_ether_addr(addr))
318                 return ethsw_port_fdb_add_uc(netdev_priv(dev),
319                                              addr);
320         else
321                 return ethsw_port_fdb_add_mc(netdev_priv(dev),
322                                              addr);
323 }
324
325 static int port_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
326                         struct net_device *dev,
327                         const unsigned char *addr, u16 vid)
328 {
329         if (is_unicast_ether_addr(addr))
330                 return ethsw_port_fdb_del_uc(netdev_priv(dev),
331                                              addr);
332         else
333                 return ethsw_port_fdb_del_mc(netdev_priv(dev),
334                                              addr);
335 }
336
337 static void port_get_stats(struct net_device *netdev,
338                            struct rtnl_link_stats64 *stats)
339 {
340         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
341         u64 tmp;
342         int err;
343
344         err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
345                                   port_priv->ethsw_data->dpsw_handle,
346                                   port_priv->idx,
347                                   DPSW_CNT_ING_FRAME, &stats->rx_packets);
348         if (err)
349                 goto error;
350
351         err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
352                                   port_priv->ethsw_data->dpsw_handle,
353                                   port_priv->idx,
354                                   DPSW_CNT_EGR_FRAME, &stats->tx_packets);
355         if (err)
356                 goto error;
357
358         err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
359                                   port_priv->ethsw_data->dpsw_handle,
360                                   port_priv->idx,
361                                   DPSW_CNT_ING_BYTE, &stats->rx_bytes);
362         if (err)
363                 goto error;
364
365         err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
366                                   port_priv->ethsw_data->dpsw_handle,
367                                   port_priv->idx,
368                                   DPSW_CNT_EGR_BYTE, &stats->tx_bytes);
369         if (err)
370                 goto error;
371
372         err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
373                                   port_priv->ethsw_data->dpsw_handle,
374                                   port_priv->idx,
375                                   DPSW_CNT_ING_FRAME_DISCARD,
376                                   &stats->rx_dropped);
377         if (err)
378                 goto error;
379
380         err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
381                                   port_priv->ethsw_data->dpsw_handle,
382                                   port_priv->idx,
383                                   DPSW_CNT_ING_FLTR_FRAME,
384                                   &tmp);
385         if (err)
386                 goto error;
387         stats->rx_dropped += tmp;
388
389         err = dpsw_if_get_counter(port_priv->ethsw_data->mc_io, 0,
390                                   port_priv->ethsw_data->dpsw_handle,
391                                   port_priv->idx,
392                                   DPSW_CNT_EGR_FRAME_DISCARD,
393                                   &stats->tx_dropped);
394         if (err)
395                 goto error;
396
397         return;
398
399 error:
400         netdev_err(netdev, "dpsw_if_get_counter err %d\n", err);
401 }
402
403 static bool port_has_offload_stats(const struct net_device *netdev,
404                                    int attr_id)
405 {
406         return (attr_id == IFLA_OFFLOAD_XSTATS_CPU_HIT);
407 }
408
409 static int port_get_offload_stats(int attr_id,
410                                   const struct net_device *netdev,
411                                   void *sp)
412 {
413         switch (attr_id) {
414         case IFLA_OFFLOAD_XSTATS_CPU_HIT:
415                 port_get_stats((struct net_device *)netdev, sp);
416                 return 0;
417         }
418
419         return -EINVAL;
420 }
421
422 static int port_change_mtu(struct net_device *netdev, int mtu)
423 {
424         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
425         int err;
426
427         err = dpsw_if_set_max_frame_length(port_priv->ethsw_data->mc_io,
428                                            0,
429                                            port_priv->ethsw_data->dpsw_handle,
430                                            port_priv->idx,
431                                            (u16)ETHSW_L2_MAX_FRM(mtu));
432         if (err) {
433                 netdev_err(netdev,
434                            "dpsw_if_set_max_frame_length() err %d\n", err);
435                 return err;
436         }
437
438         netdev->mtu = mtu;
439         return 0;
440 }
441
442 static int port_carrier_state_sync(struct net_device *netdev)
443 {
444         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
445         struct dpsw_link_state state;
446         int err;
447
448         err = dpsw_if_get_link_state(port_priv->ethsw_data->mc_io, 0,
449                                      port_priv->ethsw_data->dpsw_handle,
450                                      port_priv->idx, &state);
451         if (err) {
452                 netdev_err(netdev, "dpsw_if_get_link_state() err %d\n", err);
453                 return err;
454         }
455
456         WARN_ONCE(state.up > 1, "Garbage read into link_state");
457
458         if (state.up != port_priv->link_state) {
459                 if (state.up)
460                         netif_carrier_on(netdev);
461                 else
462                         netif_carrier_off(netdev);
463                 port_priv->link_state = state.up;
464         }
465         return 0;
466 }
467
468 static int port_open(struct net_device *netdev)
469 {
470         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
471         int err;
472
473         /* No need to allow Tx as control interface is disabled */
474         netif_tx_stop_all_queues(netdev);
475
476         /* Explicitly set carrier off, otherwise
477          * netif_carrier_ok() will return true and cause 'ip link show'
478          * to report the LOWER_UP flag, even though the link
479          * notification wasn't even received.
480          */
481         netif_carrier_off(netdev);
482
483         err = dpsw_if_enable(port_priv->ethsw_data->mc_io, 0,
484                              port_priv->ethsw_data->dpsw_handle,
485                              port_priv->idx);
486         if (err) {
487                 netdev_err(netdev, "dpsw_if_enable err %d\n", err);
488                 return err;
489         }
490
491         /* sync carrier state */
492         err = port_carrier_state_sync(netdev);
493         if (err) {
494                 netdev_err(netdev,
495                            "port_carrier_state_sync err %d\n", err);
496                 goto err_carrier_sync;
497         }
498
499         return 0;
500
501 err_carrier_sync:
502         dpsw_if_disable(port_priv->ethsw_data->mc_io, 0,
503                         port_priv->ethsw_data->dpsw_handle,
504                         port_priv->idx);
505         return err;
506 }
507
508 static int port_stop(struct net_device *netdev)
509 {
510         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
511         int err;
512
513         err = dpsw_if_disable(port_priv->ethsw_data->mc_io, 0,
514                               port_priv->ethsw_data->dpsw_handle,
515                               port_priv->idx);
516         if (err) {
517                 netdev_err(netdev, "dpsw_if_disable err %d\n", err);
518                 return err;
519         }
520
521         return 0;
522 }
523
524 static netdev_tx_t port_dropframe(struct sk_buff *skb,
525                                   struct net_device *netdev)
526 {
527         /* we don't support I/O for now, drop the frame */
528         dev_kfree_skb_any(skb);
529
530         return NETDEV_TX_OK;
531 }
532
533 static int swdev_get_port_parent_id(struct net_device *dev,
534                                     struct netdev_phys_item_id *ppid)
535 {
536         struct ethsw_port_priv *port_priv = netdev_priv(dev);
537
538         ppid->id_len = 1;
539         ppid->id[0] = port_priv->ethsw_data->dev_id;
540
541         return 0;
542 }
543
544 static int port_get_phys_name(struct net_device *netdev, char *name,
545                               size_t len)
546 {
547         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
548         int err;
549
550         err = snprintf(name, len, "p%d", port_priv->idx);
551         if (err >= len)
552                 return -EINVAL;
553
554         return 0;
555 }
556
557 struct ethsw_dump_ctx {
558         struct net_device *dev;
559         struct sk_buff *skb;
560         struct netlink_callback *cb;
561         int idx;
562 };
563
564 static int ethsw_fdb_do_dump(struct fdb_dump_entry *entry,
565                              struct ethsw_dump_ctx *dump)
566 {
567         int is_dynamic = entry->type & DPSW_FDB_ENTRY_DINAMIC;
568         u32 portid = NETLINK_CB(dump->cb->skb).portid;
569         u32 seq = dump->cb->nlh->nlmsg_seq;
570         struct nlmsghdr *nlh;
571         struct ndmsg *ndm;
572
573         if (dump->idx < dump->cb->args[2])
574                 goto skip;
575
576         nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
577                         sizeof(*ndm), NLM_F_MULTI);
578         if (!nlh)
579                 return -EMSGSIZE;
580
581         ndm = nlmsg_data(nlh);
582         ndm->ndm_family  = AF_BRIDGE;
583         ndm->ndm_pad1    = 0;
584         ndm->ndm_pad2    = 0;
585         ndm->ndm_flags   = NTF_SELF;
586         ndm->ndm_type    = 0;
587         ndm->ndm_ifindex = dump->dev->ifindex;
588         ndm->ndm_state   = is_dynamic ? NUD_REACHABLE : NUD_NOARP;
589
590         if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac_addr))
591                 goto nla_put_failure;
592
593         nlmsg_end(dump->skb, nlh);
594
595 skip:
596         dump->idx++;
597         return 0;
598
599 nla_put_failure:
600         nlmsg_cancel(dump->skb, nlh);
601         return -EMSGSIZE;
602 }
603
604 static int port_fdb_valid_entry(struct fdb_dump_entry *entry,
605                                 struct ethsw_port_priv *port_priv)
606 {
607         int idx = port_priv->idx;
608         int valid;
609
610         if (entry->type & DPSW_FDB_ENTRY_TYPE_UNICAST)
611                 valid = entry->if_info == port_priv->idx;
612         else
613                 valid = entry->if_mask[idx / 8] & BIT(idx % 8);
614
615         return valid;
616 }
617
618 static int port_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
619                          struct net_device *net_dev,
620                          struct net_device *filter_dev, int *idx)
621 {
622         struct ethsw_port_priv *port_priv = netdev_priv(net_dev);
623         struct ethsw_core *ethsw = port_priv->ethsw_data;
624         struct device *dev = net_dev->dev.parent;
625         struct fdb_dump_entry *fdb_entries;
626         struct fdb_dump_entry fdb_entry;
627         struct ethsw_dump_ctx dump = {
628                 .dev = net_dev,
629                 .skb = skb,
630                 .cb = cb,
631                 .idx = *idx,
632         };
633         dma_addr_t fdb_dump_iova;
634         u16 num_fdb_entries;
635         u32 fdb_dump_size;
636         int err = 0, i;
637         u8 *dma_mem;
638
639         fdb_dump_size = ethsw->sw_attr.max_fdb_entries * sizeof(fdb_entry);
640         dma_mem = kzalloc(fdb_dump_size, GFP_KERNEL);
641         if (!dma_mem)
642                 return -ENOMEM;
643
644         fdb_dump_iova = dma_map_single(dev, dma_mem, fdb_dump_size,
645                                        DMA_FROM_DEVICE);
646         if (dma_mapping_error(dev, fdb_dump_iova)) {
647                 netdev_err(net_dev, "dma_map_single() failed\n");
648                 err = -ENOMEM;
649                 goto err_map;
650         }
651
652         err = dpsw_fdb_dump(ethsw->mc_io, 0, ethsw->dpsw_handle, 0,
653                             fdb_dump_iova, fdb_dump_size, &num_fdb_entries);
654         if (err) {
655                 netdev_err(net_dev, "dpsw_fdb_dump() = %d\n", err);
656                 goto err_dump;
657         }
658
659         dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_FROM_DEVICE);
660
661         fdb_entries = (struct fdb_dump_entry *)dma_mem;
662         for (i = 0; i < num_fdb_entries; i++) {
663                 fdb_entry = fdb_entries[i];
664
665                 if (!port_fdb_valid_entry(&fdb_entry, port_priv))
666                         continue;
667
668                 err = ethsw_fdb_do_dump(&fdb_entry, &dump);
669                 if (err)
670                         goto end;
671         }
672
673 end:
674         *idx = dump.idx;
675
676         kfree(dma_mem);
677
678         return 0;
679
680 err_dump:
681         dma_unmap_single(dev, fdb_dump_iova, fdb_dump_size, DMA_TO_DEVICE);
682 err_map:
683         kfree(dma_mem);
684         return err;
685 }
686
687 static const struct net_device_ops ethsw_port_ops = {
688         .ndo_open               = port_open,
689         .ndo_stop               = port_stop,
690
691         .ndo_set_mac_address    = eth_mac_addr,
692         .ndo_get_stats64        = port_get_stats,
693         .ndo_change_mtu         = port_change_mtu,
694         .ndo_has_offload_stats  = port_has_offload_stats,
695         .ndo_get_offload_stats  = port_get_offload_stats,
696         .ndo_fdb_add            = port_fdb_add,
697         .ndo_fdb_del            = port_fdb_del,
698         .ndo_fdb_dump           = port_fdb_dump,
699
700         .ndo_start_xmit         = port_dropframe,
701         .ndo_get_port_parent_id = swdev_get_port_parent_id,
702         .ndo_get_phys_port_name = port_get_phys_name,
703 };
704
705 static void ethsw_links_state_update(struct ethsw_core *ethsw)
706 {
707         int i;
708
709         for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
710                 port_carrier_state_sync(ethsw->ports[i]->netdev);
711 }
712
713 static irqreturn_t ethsw_irq0_handler_thread(int irq_num, void *arg)
714 {
715         struct device *dev = (struct device *)arg;
716         struct ethsw_core *ethsw = dev_get_drvdata(dev);
717
718         /* Mask the events and the if_id reserved bits to be cleared on read */
719         u32 status = DPSW_IRQ_EVENT_LINK_CHANGED | 0xFFFF0000;
720         int err;
721
722         err = dpsw_get_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
723                                   DPSW_IRQ_INDEX_IF, &status);
724         if (err) {
725                 dev_err(dev, "Can't get irq status (err %d)\n", err);
726
727                 err = dpsw_clear_irq_status(ethsw->mc_io, 0, ethsw->dpsw_handle,
728                                             DPSW_IRQ_INDEX_IF, 0xFFFFFFFF);
729                 if (err)
730                         dev_err(dev, "Can't clear irq status (err %d)\n", err);
731                 goto out;
732         }
733
734         if (status & DPSW_IRQ_EVENT_LINK_CHANGED)
735                 ethsw_links_state_update(ethsw);
736
737 out:
738         return IRQ_HANDLED;
739 }
740
741 static int ethsw_setup_irqs(struct fsl_mc_device *sw_dev)
742 {
743         struct device *dev = &sw_dev->dev;
744         struct ethsw_core *ethsw = dev_get_drvdata(dev);
745         u32 mask = DPSW_IRQ_EVENT_LINK_CHANGED;
746         struct fsl_mc_device_irq *irq;
747         int err;
748
749         err = fsl_mc_allocate_irqs(sw_dev);
750         if (err) {
751                 dev_err(dev, "MC irqs allocation failed\n");
752                 return err;
753         }
754
755         if (WARN_ON(sw_dev->obj_desc.irq_count != DPSW_IRQ_NUM)) {
756                 err = -EINVAL;
757                 goto free_irq;
758         }
759
760         err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
761                                   DPSW_IRQ_INDEX_IF, 0);
762         if (err) {
763                 dev_err(dev, "dpsw_set_irq_enable err %d\n", err);
764                 goto free_irq;
765         }
766
767         irq = sw_dev->irqs[DPSW_IRQ_INDEX_IF];
768
769         err = devm_request_threaded_irq(dev, irq->msi_desc->irq,
770                                         NULL,
771                                         ethsw_irq0_handler_thread,
772                                         IRQF_NO_SUSPEND | IRQF_ONESHOT,
773                                         dev_name(dev), dev);
774         if (err) {
775                 dev_err(dev, "devm_request_threaded_irq(): %d\n", err);
776                 goto free_irq;
777         }
778
779         err = dpsw_set_irq_mask(ethsw->mc_io, 0, ethsw->dpsw_handle,
780                                 DPSW_IRQ_INDEX_IF, mask);
781         if (err) {
782                 dev_err(dev, "dpsw_set_irq_mask(): %d\n", err);
783                 goto free_devm_irq;
784         }
785
786         err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
787                                   DPSW_IRQ_INDEX_IF, 1);
788         if (err) {
789                 dev_err(dev, "dpsw_set_irq_enable(): %d\n", err);
790                 goto free_devm_irq;
791         }
792
793         return 0;
794
795 free_devm_irq:
796         devm_free_irq(dev, irq->msi_desc->irq, dev);
797 free_irq:
798         fsl_mc_free_irqs(sw_dev);
799         return err;
800 }
801
802 static void ethsw_teardown_irqs(struct fsl_mc_device *sw_dev)
803 {
804         struct device *dev = &sw_dev->dev;
805         struct ethsw_core *ethsw = dev_get_drvdata(dev);
806         int err;
807
808         err = dpsw_set_irq_enable(ethsw->mc_io, 0, ethsw->dpsw_handle,
809                                   DPSW_IRQ_INDEX_IF, 0);
810         if (err)
811                 dev_err(dev, "dpsw_set_irq_enable err %d\n", err);
812
813         fsl_mc_free_irqs(sw_dev);
814 }
815
816 static int port_attr_stp_state_set(struct net_device *netdev,
817                                    struct switchdev_trans *trans,
818                                    u8 state)
819 {
820         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
821
822         if (switchdev_trans_ph_prepare(trans))
823                 return 0;
824
825         return ethsw_port_set_stp_state(port_priv, state);
826 }
827
828 static int port_attr_br_flags_pre_set(struct net_device *netdev,
829                                       struct switchdev_trans *trans,
830                                       unsigned long flags)
831 {
832         if (flags & ~(BR_LEARNING | BR_FLOOD))
833                 return -EINVAL;
834
835         return 0;
836 }
837
838 static int port_attr_br_flags_set(struct net_device *netdev,
839                                   struct switchdev_trans *trans,
840                                   unsigned long flags)
841 {
842         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
843         int err = 0;
844
845         if (switchdev_trans_ph_prepare(trans))
846                 return 0;
847
848         /* Learning is enabled per switch */
849         err = ethsw_set_learning(port_priv->ethsw_data,
850                                  !!(flags & BR_LEARNING));
851         if (err)
852                 goto exit;
853
854         err = ethsw_port_set_flood(port_priv, !!(flags & BR_FLOOD));
855
856 exit:
857         return err;
858 }
859
860 static int swdev_port_attr_set(struct net_device *netdev,
861                                const struct switchdev_attr *attr,
862                                struct switchdev_trans *trans)
863 {
864         int err = 0;
865
866         switch (attr->id) {
867         case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
868                 err = port_attr_stp_state_set(netdev, trans,
869                                               attr->u.stp_state);
870                 break;
871         case SWITCHDEV_ATTR_ID_PORT_PRE_BRIDGE_FLAGS:
872                 err = port_attr_br_flags_pre_set(netdev, trans,
873                                                  attr->u.brport_flags);
874                 break;
875         case SWITCHDEV_ATTR_ID_PORT_BRIDGE_FLAGS:
876                 err = port_attr_br_flags_set(netdev, trans,
877                                              attr->u.brport_flags);
878                 break;
879         case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
880                 /* VLANs are supported by default  */
881                 break;
882         default:
883                 err = -EOPNOTSUPP;
884                 break;
885         }
886
887         return err;
888 }
889
890 static int port_vlans_add(struct net_device *netdev,
891                           const struct switchdev_obj_port_vlan *vlan,
892                           struct switchdev_trans *trans)
893 {
894         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
895         int vid, err = 0;
896
897         if (switchdev_trans_ph_prepare(trans))
898                 return 0;
899
900         for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
901                 if (!port_priv->ethsw_data->vlans[vid]) {
902                         /* this is a new VLAN */
903                         err = ethsw_add_vlan(port_priv->ethsw_data, vid);
904                         if (err)
905                                 return err;
906
907                         port_priv->ethsw_data->vlans[vid] |= ETHSW_VLAN_GLOBAL;
908                 }
909                 err = ethsw_port_add_vlan(port_priv, vid, vlan->flags);
910                 if (err)
911                         break;
912         }
913
914         return err;
915 }
916
917 static int port_lookup_address(struct net_device *netdev, int is_uc,
918                                const unsigned char *addr)
919 {
920         struct netdev_hw_addr_list *list = (is_uc) ? &netdev->uc : &netdev->mc;
921         struct netdev_hw_addr *ha;
922
923         netif_addr_lock_bh(netdev);
924         list_for_each_entry(ha, &list->list, list) {
925                 if (ether_addr_equal(ha->addr, addr)) {
926                         netif_addr_unlock_bh(netdev);
927                         return 1;
928                 }
929         }
930         netif_addr_unlock_bh(netdev);
931         return 0;
932 }
933
934 static int port_mdb_add(struct net_device *netdev,
935                         const struct switchdev_obj_port_mdb *mdb,
936                         struct switchdev_trans *trans)
937 {
938         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
939         int err;
940
941         if (switchdev_trans_ph_prepare(trans))
942                 return 0;
943
944         /* Check if address is already set on this port */
945         if (port_lookup_address(netdev, 0, mdb->addr))
946                 return -EEXIST;
947
948         err = ethsw_port_fdb_add_mc(port_priv, mdb->addr);
949         if (err)
950                 return err;
951
952         err = dev_mc_add(netdev, mdb->addr);
953         if (err) {
954                 netdev_err(netdev, "dev_mc_add err %d\n", err);
955                 ethsw_port_fdb_del_mc(port_priv, mdb->addr);
956         }
957
958         return err;
959 }
960
961 static int swdev_port_obj_add(struct net_device *netdev,
962                               const struct switchdev_obj *obj,
963                               struct switchdev_trans *trans)
964 {
965         int err;
966
967         switch (obj->id) {
968         case SWITCHDEV_OBJ_ID_PORT_VLAN:
969                 err = port_vlans_add(netdev,
970                                      SWITCHDEV_OBJ_PORT_VLAN(obj),
971                                      trans);
972                 break;
973         case SWITCHDEV_OBJ_ID_PORT_MDB:
974                 err = port_mdb_add(netdev,
975                                    SWITCHDEV_OBJ_PORT_MDB(obj),
976                                    trans);
977                 break;
978         default:
979                 err = -EOPNOTSUPP;
980                 break;
981         }
982
983         return err;
984 }
985
986 static int ethsw_port_del_vlan(struct ethsw_port_priv *port_priv, u16 vid)
987 {
988         struct ethsw_core *ethsw = port_priv->ethsw_data;
989         struct net_device *netdev = port_priv->netdev;
990         struct dpsw_vlan_if_cfg vcfg;
991         int i, err;
992
993         if (!port_priv->vlans[vid])
994                 return -ENOENT;
995
996         if (port_priv->vlans[vid] & ETHSW_VLAN_PVID) {
997                 err = ethsw_port_set_pvid(port_priv, 0);
998                 if (err)
999                         return err;
1000         }
1001
1002         vcfg.num_ifs = 1;
1003         vcfg.if_id[0] = port_priv->idx;
1004         if (port_priv->vlans[vid] & ETHSW_VLAN_UNTAGGED) {
1005                 err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0,
1006                                                    ethsw->dpsw_handle,
1007                                                    vid, &vcfg);
1008                 if (err) {
1009                         netdev_err(netdev,
1010                                    "dpsw_vlan_remove_if_untagged err %d\n",
1011                                    err);
1012                 }
1013                 port_priv->vlans[vid] &= ~ETHSW_VLAN_UNTAGGED;
1014         }
1015
1016         if (port_priv->vlans[vid] & ETHSW_VLAN_MEMBER) {
1017                 err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
1018                                           vid, &vcfg);
1019                 if (err) {
1020                         netdev_err(netdev,
1021                                    "dpsw_vlan_remove_if err %d\n", err);
1022                         return err;
1023                 }
1024                 port_priv->vlans[vid] &= ~ETHSW_VLAN_MEMBER;
1025
1026                 /* Delete VLAN from switch if it is no longer configured on
1027                  * any port
1028                  */
1029                 for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
1030                         if (ethsw->ports[i]->vlans[vid] & ETHSW_VLAN_MEMBER)
1031                                 return 0; /* Found a port member in VID */
1032
1033                 ethsw->vlans[vid] &= ~ETHSW_VLAN_GLOBAL;
1034
1035                 err = ethsw_dellink_switch(ethsw, vid);
1036                 if (err)
1037                         return err;
1038         }
1039
1040         return 0;
1041 }
1042
1043 static int port_vlans_del(struct net_device *netdev,
1044                           const struct switchdev_obj_port_vlan *vlan)
1045 {
1046         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1047         int vid, err = 0;
1048
1049         if (netif_is_bridge_master(vlan->obj.orig_dev))
1050                 return -EOPNOTSUPP;
1051
1052         for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1053                 err = ethsw_port_del_vlan(port_priv, vid);
1054                 if (err)
1055                         break;
1056         }
1057
1058         return err;
1059 }
1060
1061 static int port_mdb_del(struct net_device *netdev,
1062                         const struct switchdev_obj_port_mdb *mdb)
1063 {
1064         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1065         int err;
1066
1067         if (!port_lookup_address(netdev, 0, mdb->addr))
1068                 return -ENOENT;
1069
1070         err = ethsw_port_fdb_del_mc(port_priv, mdb->addr);
1071         if (err)
1072                 return err;
1073
1074         err = dev_mc_del(netdev, mdb->addr);
1075         if (err) {
1076                 netdev_err(netdev, "dev_mc_del err %d\n", err);
1077                 return err;
1078         }
1079
1080         return err;
1081 }
1082
1083 static int swdev_port_obj_del(struct net_device *netdev,
1084                               const struct switchdev_obj *obj)
1085 {
1086         int err;
1087
1088         switch (obj->id) {
1089         case SWITCHDEV_OBJ_ID_PORT_VLAN:
1090                 err = port_vlans_del(netdev, SWITCHDEV_OBJ_PORT_VLAN(obj));
1091                 break;
1092         case SWITCHDEV_OBJ_ID_PORT_MDB:
1093                 err = port_mdb_del(netdev, SWITCHDEV_OBJ_PORT_MDB(obj));
1094                 break;
1095         default:
1096                 err = -EOPNOTSUPP;
1097                 break;
1098         }
1099         return err;
1100 }
1101
1102 static int
1103 ethsw_switchdev_port_attr_set_event(struct net_device *netdev,
1104                                     struct switchdev_notifier_port_attr_info
1105                                     *port_attr_info)
1106 {
1107         int err;
1108
1109         err = swdev_port_attr_set(netdev, port_attr_info->attr,
1110                                   port_attr_info->trans);
1111
1112         port_attr_info->handled = true;
1113         return notifier_from_errno(err);
1114 }
1115
1116 /* For the moment, only flood setting needs to be updated */
1117 static int port_bridge_join(struct net_device *netdev,
1118                             struct net_device *upper_dev)
1119 {
1120         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1121         struct ethsw_core *ethsw = port_priv->ethsw_data;
1122         int i, err;
1123
1124         for (i = 0; i < ethsw->sw_attr.num_ifs; i++)
1125                 if (ethsw->ports[i]->bridge_dev &&
1126                     (ethsw->ports[i]->bridge_dev != upper_dev)) {
1127                         netdev_err(netdev,
1128                                    "Only one bridge supported per DPSW object!\n");
1129                         return -EINVAL;
1130                 }
1131
1132         /* Enable flooding */
1133         err = ethsw_port_set_flood(port_priv, 1);
1134         if (!err)
1135                 port_priv->bridge_dev = upper_dev;
1136
1137         return err;
1138 }
1139
1140 static int port_bridge_leave(struct net_device *netdev)
1141 {
1142         struct ethsw_port_priv *port_priv = netdev_priv(netdev);
1143         int err;
1144
1145         /* Disable flooding */
1146         err = ethsw_port_set_flood(port_priv, 0);
1147         if (!err)
1148                 port_priv->bridge_dev = NULL;
1149
1150         return err;
1151 }
1152
1153 static bool ethsw_port_dev_check(const struct net_device *netdev)
1154 {
1155         return netdev->netdev_ops == &ethsw_port_ops;
1156 }
1157
1158 static int port_netdevice_event(struct notifier_block *unused,
1159                                 unsigned long event, void *ptr)
1160 {
1161         struct net_device *netdev = netdev_notifier_info_to_dev(ptr);
1162         struct netdev_notifier_changeupper_info *info = ptr;
1163         struct net_device *upper_dev;
1164         int err = 0;
1165
1166         if (!ethsw_port_dev_check(netdev))
1167                 return NOTIFY_DONE;
1168
1169         /* Handle just upper dev link/unlink for the moment */
1170         if (event == NETDEV_CHANGEUPPER) {
1171                 upper_dev = info->upper_dev;
1172                 if (netif_is_bridge_master(upper_dev)) {
1173                         if (info->linking)
1174                                 err = port_bridge_join(netdev, upper_dev);
1175                         else
1176                                 err = port_bridge_leave(netdev);
1177                 }
1178         }
1179
1180         return notifier_from_errno(err);
1181 }
1182
1183 struct ethsw_switchdev_event_work {
1184         struct work_struct work;
1185         struct switchdev_notifier_fdb_info fdb_info;
1186         struct net_device *dev;
1187         unsigned long event;
1188 };
1189
1190 static void ethsw_switchdev_event_work(struct work_struct *work)
1191 {
1192         struct ethsw_switchdev_event_work *switchdev_work =
1193                 container_of(work, struct ethsw_switchdev_event_work, work);
1194         struct net_device *dev = switchdev_work->dev;
1195         struct switchdev_notifier_fdb_info *fdb_info;
1196         int err;
1197
1198         rtnl_lock();
1199         fdb_info = &switchdev_work->fdb_info;
1200
1201         switch (switchdev_work->event) {
1202         case SWITCHDEV_FDB_ADD_TO_DEVICE:
1203                 if (!fdb_info->added_by_user)
1204                         break;
1205                 if (is_unicast_ether_addr(fdb_info->addr))
1206                         err = ethsw_port_fdb_add_uc(netdev_priv(dev),
1207                                                     fdb_info->addr);
1208                 else
1209                         err = ethsw_port_fdb_add_mc(netdev_priv(dev),
1210                                                     fdb_info->addr);
1211                 if (err)
1212                         break;
1213                 fdb_info->offloaded = true;
1214                 call_switchdev_notifiers(SWITCHDEV_FDB_OFFLOADED, dev,
1215                                          &fdb_info->info, NULL);
1216                 break;
1217         case SWITCHDEV_FDB_DEL_TO_DEVICE:
1218                 if (!fdb_info->added_by_user)
1219                         break;
1220                 if (is_unicast_ether_addr(fdb_info->addr))
1221                         ethsw_port_fdb_del_uc(netdev_priv(dev), fdb_info->addr);
1222                 else
1223                         ethsw_port_fdb_del_mc(netdev_priv(dev), fdb_info->addr);
1224                 break;
1225         }
1226
1227         rtnl_unlock();
1228         kfree(switchdev_work->fdb_info.addr);
1229         kfree(switchdev_work);
1230         dev_put(dev);
1231 }
1232
1233 /* Called under rcu_read_lock() */
1234 static int port_switchdev_event(struct notifier_block *unused,
1235                                 unsigned long event, void *ptr)
1236 {
1237         struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1238         struct ethsw_port_priv *port_priv = netdev_priv(dev);
1239         struct ethsw_switchdev_event_work *switchdev_work;
1240         struct switchdev_notifier_fdb_info *fdb_info = ptr;
1241         struct ethsw_core *ethsw = port_priv->ethsw_data;
1242
1243         if (!ethsw_port_dev_check(dev))
1244                 return NOTIFY_DONE;
1245
1246         if (event == SWITCHDEV_PORT_ATTR_SET)
1247                 return ethsw_switchdev_port_attr_set_event(dev, ptr);
1248
1249         switchdev_work = kzalloc(sizeof(*switchdev_work), GFP_ATOMIC);
1250         if (!switchdev_work)
1251                 return NOTIFY_BAD;
1252
1253         INIT_WORK(&switchdev_work->work, ethsw_switchdev_event_work);
1254         switchdev_work->dev = dev;
1255         switchdev_work->event = event;
1256
1257         switch (event) {
1258         case SWITCHDEV_FDB_ADD_TO_DEVICE:
1259         case SWITCHDEV_FDB_DEL_TO_DEVICE:
1260                 memcpy(&switchdev_work->fdb_info, ptr,
1261                        sizeof(switchdev_work->fdb_info));
1262                 switchdev_work->fdb_info.addr = kzalloc(ETH_ALEN, GFP_ATOMIC);
1263                 if (!switchdev_work->fdb_info.addr)
1264                         goto err_addr_alloc;
1265
1266                 ether_addr_copy((u8 *)switchdev_work->fdb_info.addr,
1267                                 fdb_info->addr);
1268
1269                 /* Take a reference on the device to avoid being freed. */
1270                 dev_hold(dev);
1271                 break;
1272         default:
1273                 kfree(switchdev_work);
1274                 return NOTIFY_DONE;
1275         }
1276
1277         queue_work(ethsw->workqueue, &switchdev_work->work);
1278
1279         return NOTIFY_DONE;
1280
1281 err_addr_alloc:
1282         kfree(switchdev_work);
1283         return NOTIFY_BAD;
1284 }
1285
1286 static int
1287 ethsw_switchdev_port_obj_event(unsigned long event, struct net_device *netdev,
1288                                struct switchdev_notifier_port_obj_info
1289                                *port_obj_info)
1290 {
1291         int err = -EOPNOTSUPP;
1292
1293         switch (event) {
1294         case SWITCHDEV_PORT_OBJ_ADD:
1295                 err = swdev_port_obj_add(netdev, port_obj_info->obj,
1296                                          port_obj_info->trans);
1297                 break;
1298         case SWITCHDEV_PORT_OBJ_DEL:
1299                 err = swdev_port_obj_del(netdev, port_obj_info->obj);
1300                 break;
1301         }
1302
1303         port_obj_info->handled = true;
1304         return notifier_from_errno(err);
1305 }
1306
1307 static int port_switchdev_blocking_event(struct notifier_block *unused,
1308                                          unsigned long event, void *ptr)
1309 {
1310         struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1311
1312         if (!ethsw_port_dev_check(dev))
1313                 return NOTIFY_DONE;
1314
1315         switch (event) {
1316         case SWITCHDEV_PORT_OBJ_ADD: /* fall through */
1317         case SWITCHDEV_PORT_OBJ_DEL:
1318                 return ethsw_switchdev_port_obj_event(event, dev, ptr);
1319         case SWITCHDEV_PORT_ATTR_SET:
1320                 return ethsw_switchdev_port_attr_set_event(dev, ptr);
1321         }
1322
1323         return NOTIFY_DONE;
1324 }
1325
1326 static int ethsw_register_notifier(struct device *dev)
1327 {
1328         struct ethsw_core *ethsw = dev_get_drvdata(dev);
1329         int err;
1330
1331         ethsw->port_nb.notifier_call = port_netdevice_event;
1332         err = register_netdevice_notifier(&ethsw->port_nb);
1333         if (err) {
1334                 dev_err(dev, "Failed to register netdev notifier\n");
1335                 return err;
1336         }
1337
1338         ethsw->port_switchdev_nb.notifier_call = port_switchdev_event;
1339         err = register_switchdev_notifier(&ethsw->port_switchdev_nb);
1340         if (err) {
1341                 dev_err(dev, "Failed to register switchdev notifier\n");
1342                 goto err_switchdev_nb;
1343         }
1344
1345         ethsw->port_switchdevb_nb.notifier_call = port_switchdev_blocking_event;
1346         err = register_switchdev_blocking_notifier(&ethsw->port_switchdevb_nb);
1347         if (err) {
1348                 dev_err(dev, "Failed to register switchdev blocking notifier\n");
1349                 goto err_switchdev_blocking_nb;
1350         }
1351
1352         return 0;
1353
1354 err_switchdev_blocking_nb:
1355         unregister_switchdev_notifier(&ethsw->port_switchdev_nb);
1356 err_switchdev_nb:
1357         unregister_netdevice_notifier(&ethsw->port_nb);
1358         return err;
1359 }
1360
1361 static int ethsw_init(struct fsl_mc_device *sw_dev)
1362 {
1363         struct device *dev = &sw_dev->dev;
1364         struct ethsw_core *ethsw = dev_get_drvdata(dev);
1365         u16 version_major, version_minor, i;
1366         struct dpsw_stp_cfg stp_cfg;
1367         int err;
1368
1369         ethsw->dev_id = sw_dev->obj_desc.id;
1370
1371         err = dpsw_open(ethsw->mc_io, 0, ethsw->dev_id, &ethsw->dpsw_handle);
1372         if (err) {
1373                 dev_err(dev, "dpsw_open err %d\n", err);
1374                 return err;
1375         }
1376
1377         err = dpsw_get_attributes(ethsw->mc_io, 0, ethsw->dpsw_handle,
1378                                   &ethsw->sw_attr);
1379         if (err) {
1380                 dev_err(dev, "dpsw_get_attributes err %d\n", err);
1381                 goto err_close;
1382         }
1383
1384         err = dpsw_get_api_version(ethsw->mc_io, 0,
1385                                    &version_major,
1386                                    &version_minor);
1387         if (err) {
1388                 dev_err(dev, "dpsw_get_api_version err %d\n", err);
1389                 goto err_close;
1390         }
1391
1392         /* Minimum supported DPSW version check */
1393         if (version_major < DPSW_MIN_VER_MAJOR ||
1394             (version_major == DPSW_MIN_VER_MAJOR &&
1395              version_minor < DPSW_MIN_VER_MINOR)) {
1396                 dev_err(dev, "DPSW version %d:%d not supported. Use %d.%d or greater.\n",
1397                         version_major,
1398                         version_minor,
1399                         DPSW_MIN_VER_MAJOR, DPSW_MIN_VER_MINOR);
1400                 err = -ENOTSUPP;
1401                 goto err_close;
1402         }
1403
1404         err = dpsw_reset(ethsw->mc_io, 0, ethsw->dpsw_handle);
1405         if (err) {
1406                 dev_err(dev, "dpsw_reset err %d\n", err);
1407                 goto err_close;
1408         }
1409
1410         err = dpsw_fdb_set_learning_mode(ethsw->mc_io, 0, ethsw->dpsw_handle, 0,
1411                                          DPSW_FDB_LEARNING_MODE_HW);
1412         if (err) {
1413                 dev_err(dev, "dpsw_fdb_set_learning_mode err %d\n", err);
1414                 goto err_close;
1415         }
1416
1417         stp_cfg.vlan_id = DEFAULT_VLAN_ID;
1418         stp_cfg.state = DPSW_STP_STATE_FORWARDING;
1419
1420         for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1421                 err = dpsw_if_set_stp(ethsw->mc_io, 0, ethsw->dpsw_handle, i,
1422                                       &stp_cfg);
1423                 if (err) {
1424                         dev_err(dev, "dpsw_if_set_stp err %d for port %d\n",
1425                                 err, i);
1426                         goto err_close;
1427                 }
1428
1429                 err = dpsw_if_set_broadcast(ethsw->mc_io, 0,
1430                                             ethsw->dpsw_handle, i, 1);
1431                 if (err) {
1432                         dev_err(dev,
1433                                 "dpsw_if_set_broadcast err %d for port %d\n",
1434                                 err, i);
1435                         goto err_close;
1436                 }
1437         }
1438
1439         ethsw->workqueue = alloc_ordered_workqueue("%s_%d_ordered",
1440                                                    WQ_MEM_RECLAIM, "ethsw",
1441                                                    ethsw->sw_attr.id);
1442         if (!ethsw->workqueue) {
1443                 err = -ENOMEM;
1444                 goto err_close;
1445         }
1446
1447         err = ethsw_register_notifier(dev);
1448         if (err)
1449                 goto err_destroy_ordered_workqueue;
1450
1451         return 0;
1452
1453 err_destroy_ordered_workqueue:
1454         destroy_workqueue(ethsw->workqueue);
1455
1456 err_close:
1457         dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle);
1458         return err;
1459 }
1460
1461 static int ethsw_port_init(struct ethsw_port_priv *port_priv, u16 port)
1462 {
1463         struct net_device *netdev = port_priv->netdev;
1464         struct ethsw_core *ethsw = port_priv->ethsw_data;
1465         struct dpsw_vlan_if_cfg vcfg;
1466         int err;
1467
1468         /* Switch starts with all ports configured to VLAN 1. Need to
1469          * remove this setting to allow configuration at bridge join
1470          */
1471         vcfg.num_ifs = 1;
1472         vcfg.if_id[0] = port_priv->idx;
1473
1474         err = dpsw_vlan_remove_if_untagged(ethsw->mc_io, 0, ethsw->dpsw_handle,
1475                                            DEFAULT_VLAN_ID, &vcfg);
1476         if (err) {
1477                 netdev_err(netdev, "dpsw_vlan_remove_if_untagged err %d\n",
1478                            err);
1479                 return err;
1480         }
1481
1482         err = ethsw_port_set_pvid(port_priv, 0);
1483         if (err)
1484                 return err;
1485
1486         err = dpsw_vlan_remove_if(ethsw->mc_io, 0, ethsw->dpsw_handle,
1487                                   DEFAULT_VLAN_ID, &vcfg);
1488         if (err)
1489                 netdev_err(netdev, "dpsw_vlan_remove_if err %d\n", err);
1490
1491         return err;
1492 }
1493
1494 static void ethsw_unregister_notifier(struct device *dev)
1495 {
1496         struct ethsw_core *ethsw = dev_get_drvdata(dev);
1497         struct notifier_block *nb;
1498         int err;
1499
1500         nb = &ethsw->port_switchdevb_nb;
1501         err = unregister_switchdev_blocking_notifier(nb);
1502         if (err)
1503                 dev_err(dev,
1504                         "Failed to unregister switchdev blocking notifier (%d)\n",
1505                         err);
1506
1507         err = unregister_switchdev_notifier(&ethsw->port_switchdev_nb);
1508         if (err)
1509                 dev_err(dev,
1510                         "Failed to unregister switchdev notifier (%d)\n", err);
1511
1512         err = unregister_netdevice_notifier(&ethsw->port_nb);
1513         if (err)
1514                 dev_err(dev,
1515                         "Failed to unregister netdev notifier (%d)\n", err);
1516 }
1517
1518 static void ethsw_takedown(struct fsl_mc_device *sw_dev)
1519 {
1520         struct device *dev = &sw_dev->dev;
1521         struct ethsw_core *ethsw = dev_get_drvdata(dev);
1522         int err;
1523
1524         ethsw_unregister_notifier(dev);
1525
1526         err = dpsw_close(ethsw->mc_io, 0, ethsw->dpsw_handle);
1527         if (err)
1528                 dev_warn(dev, "dpsw_close err %d\n", err);
1529 }
1530
1531 static int ethsw_remove(struct fsl_mc_device *sw_dev)
1532 {
1533         struct ethsw_port_priv *port_priv;
1534         struct ethsw_core *ethsw;
1535         struct device *dev;
1536         int i;
1537
1538         dev = &sw_dev->dev;
1539         ethsw = dev_get_drvdata(dev);
1540
1541         ethsw_teardown_irqs(sw_dev);
1542
1543         destroy_workqueue(ethsw->workqueue);
1544
1545         dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
1546
1547         for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1548                 port_priv = ethsw->ports[i];
1549                 unregister_netdev(port_priv->netdev);
1550                 free_netdev(port_priv->netdev);
1551         }
1552         kfree(ethsw->ports);
1553
1554         ethsw_takedown(sw_dev);
1555         fsl_mc_portal_free(ethsw->mc_io);
1556
1557         kfree(ethsw);
1558
1559         dev_set_drvdata(dev, NULL);
1560
1561         return 0;
1562 }
1563
1564 static int ethsw_probe_port(struct ethsw_core *ethsw, u16 port_idx)
1565 {
1566         struct ethsw_port_priv *port_priv;
1567         struct device *dev = ethsw->dev;
1568         struct net_device *port_netdev;
1569         int err;
1570
1571         port_netdev = alloc_etherdev(sizeof(struct ethsw_port_priv));
1572         if (!port_netdev) {
1573                 dev_err(dev, "alloc_etherdev error\n");
1574                 return -ENOMEM;
1575         }
1576
1577         port_priv = netdev_priv(port_netdev);
1578         port_priv->netdev = port_netdev;
1579         port_priv->ethsw_data = ethsw;
1580
1581         port_priv->idx = port_idx;
1582         port_priv->stp_state = BR_STATE_FORWARDING;
1583
1584         /* Flooding is implicitly enabled */
1585         port_priv->flood = true;
1586
1587         SET_NETDEV_DEV(port_netdev, dev);
1588         port_netdev->netdev_ops = &ethsw_port_ops;
1589         port_netdev->ethtool_ops = &ethsw_port_ethtool_ops;
1590
1591         /* Set MTU limits */
1592         port_netdev->min_mtu = ETH_MIN_MTU;
1593         port_netdev->max_mtu = ETHSW_MAX_FRAME_LENGTH;
1594
1595         err = ethsw_port_init(port_priv, port_idx);
1596         if (err)
1597                 goto err_port_probe;
1598
1599         err = register_netdev(port_netdev);
1600         if (err < 0) {
1601                 dev_err(dev, "register_netdev error %d\n", err);
1602                 goto err_port_probe;
1603         }
1604
1605         ethsw->ports[port_idx] = port_priv;
1606
1607         return 0;
1608
1609 err_port_probe:
1610         free_netdev(port_netdev);
1611
1612         return err;
1613 }
1614
1615 static int ethsw_probe(struct fsl_mc_device *sw_dev)
1616 {
1617         struct device *dev = &sw_dev->dev;
1618         struct ethsw_core *ethsw;
1619         int i, err;
1620
1621         /* Allocate switch core*/
1622         ethsw = kzalloc(sizeof(*ethsw), GFP_KERNEL);
1623
1624         if (!ethsw)
1625                 return -ENOMEM;
1626
1627         ethsw->dev = dev;
1628         dev_set_drvdata(dev, ethsw);
1629
1630         err = fsl_mc_portal_allocate(sw_dev, FSL_MC_IO_ATOMIC_CONTEXT_PORTAL,
1631                                      &ethsw->mc_io);
1632         if (err) {
1633                 if (err == -ENXIO)
1634                         err = -EPROBE_DEFER;
1635                 else
1636                         dev_err(dev, "fsl_mc_portal_allocate err %d\n", err);
1637                 goto err_free_drvdata;
1638         }
1639
1640         err = ethsw_init(sw_dev);
1641         if (err)
1642                 goto err_free_cmdport;
1643
1644         /* DEFAULT_VLAN_ID is implicitly configured on the switch */
1645         ethsw->vlans[DEFAULT_VLAN_ID] = ETHSW_VLAN_MEMBER;
1646
1647         /* Learning is implicitly enabled */
1648         ethsw->learning = true;
1649
1650         ethsw->ports = kcalloc(ethsw->sw_attr.num_ifs, sizeof(*ethsw->ports),
1651                                GFP_KERNEL);
1652         if (!(ethsw->ports)) {
1653                 err = -ENOMEM;
1654                 goto err_takedown;
1655         }
1656
1657         for (i = 0; i < ethsw->sw_attr.num_ifs; i++) {
1658                 err = ethsw_probe_port(ethsw, i);
1659                 if (err)
1660                         goto err_free_ports;
1661         }
1662
1663         err = dpsw_enable(ethsw->mc_io, 0, ethsw->dpsw_handle);
1664         if (err) {
1665                 dev_err(ethsw->dev, "dpsw_enable err %d\n", err);
1666                 goto err_free_ports;
1667         }
1668
1669         /* Setup IRQs */
1670         err = ethsw_setup_irqs(sw_dev);
1671         if (err)
1672                 goto err_stop;
1673
1674         dev_info(dev, "probed %d port switch\n", ethsw->sw_attr.num_ifs);
1675         return 0;
1676
1677 err_stop:
1678         dpsw_disable(ethsw->mc_io, 0, ethsw->dpsw_handle);
1679
1680 err_free_ports:
1681         /* Cleanup registered ports only */
1682         for (i--; i >= 0; i--) {
1683                 unregister_netdev(ethsw->ports[i]->netdev);
1684                 free_netdev(ethsw->ports[i]->netdev);
1685         }
1686         kfree(ethsw->ports);
1687
1688 err_takedown:
1689         ethsw_takedown(sw_dev);
1690
1691 err_free_cmdport:
1692         fsl_mc_portal_free(ethsw->mc_io);
1693
1694 err_free_drvdata:
1695         kfree(ethsw);
1696         dev_set_drvdata(dev, NULL);
1697
1698         return err;
1699 }
1700
1701 static const struct fsl_mc_device_id ethsw_match_id_table[] = {
1702         {
1703                 .vendor = FSL_MC_VENDOR_FREESCALE,
1704                 .obj_type = "dpsw",
1705         },
1706         { .vendor = 0x0 }
1707 };
1708 MODULE_DEVICE_TABLE(fslmc, ethsw_match_id_table);
1709
1710 static struct fsl_mc_driver eth_sw_drv = {
1711         .driver = {
1712                 .name = KBUILD_MODNAME,
1713                 .owner = THIS_MODULE,
1714         },
1715         .probe = ethsw_probe,
1716         .remove = ethsw_remove,
1717         .match_id_table = ethsw_match_id_table
1718 };
1719
1720 module_fsl_mc_driver(eth_sw_drv);
1721
1722 MODULE_LICENSE("GPL v2");
1723 MODULE_DESCRIPTION("DPAA2 Ethernet Switch Driver");