blob: ea3eec263875177adf5f40194000fe204cefc701 [file] [log] [blame]
Alexandre Bellonia556c762018-05-14 22:04:57 +02001// SPDX-License-Identifier: (GPL-2.0 OR MIT)
2/*
3 * Microsemi Ocelot Switch driver
4 *
5 * Copyright (c) 2017 Microsemi Corporation
6 */
7#include <linux/etherdevice.h>
8#include <linux/ethtool.h>
9#include <linux/if_bridge.h>
10#include <linux/if_ether.h>
11#include <linux/if_vlan.h>
12#include <linux/interrupt.h>
13#include <linux/kernel.h>
14#include <linux/module.h>
15#include <linux/netdevice.h>
16#include <linux/phy.h>
17#include <linux/skbuff.h>
18#include <net/arp.h>
19#include <net/netevent.h>
20#include <net/rtnetlink.h>
21#include <net/switchdev.h>
22
23#include "ocelot.h"
24
25/* MAC table entry types.
26 * ENTRYTYPE_NORMAL is subject to aging.
27 * ENTRYTYPE_LOCKED is not subject to aging.
28 * ENTRYTYPE_MACv4 is not subject to aging. For IPv4 multicast.
29 * ENTRYTYPE_MACv6 is not subject to aging. For IPv6 multicast.
30 */
31enum macaccess_entry_type {
32 ENTRYTYPE_NORMAL = 0,
33 ENTRYTYPE_LOCKED,
34 ENTRYTYPE_MACv4,
35 ENTRYTYPE_MACv6,
36};
37
38struct ocelot_mact_entry {
39 u8 mac[ETH_ALEN];
40 u16 vid;
41 enum macaccess_entry_type type;
42};
43
44static inline int ocelot_mact_wait_for_completion(struct ocelot *ocelot)
45{
46 unsigned int val, timeout = 10;
47
48 /* Wait for the issued mac table command to be completed, or timeout.
49 * When the command read from ANA_TABLES_MACACCESS is
50 * MACACCESS_CMD_IDLE, the issued command completed successfully.
51 */
52 do {
53 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
54 val &= ANA_TABLES_MACACCESS_MAC_TABLE_CMD_M;
55 } while (val != MACACCESS_CMD_IDLE && timeout--);
56
57 if (!timeout)
58 return -ETIMEDOUT;
59
60 return 0;
61}
62
63static void ocelot_mact_select(struct ocelot *ocelot,
64 const unsigned char mac[ETH_ALEN],
65 unsigned int vid)
66{
67 u32 macl = 0, mach = 0;
68
69 /* Set the MAC address to handle and the vlan associated in a format
70 * understood by the hardware.
71 */
72 mach |= vid << 16;
73 mach |= mac[0] << 8;
74 mach |= mac[1] << 0;
75 macl |= mac[2] << 24;
76 macl |= mac[3] << 16;
77 macl |= mac[4] << 8;
78 macl |= mac[5] << 0;
79
80 ocelot_write(ocelot, macl, ANA_TABLES_MACLDATA);
81 ocelot_write(ocelot, mach, ANA_TABLES_MACHDATA);
82
83}
84
85static int ocelot_mact_learn(struct ocelot *ocelot, int port,
86 const unsigned char mac[ETH_ALEN],
87 unsigned int vid,
88 enum macaccess_entry_type type)
89{
90 ocelot_mact_select(ocelot, mac, vid);
91
92 /* Issue a write command */
93 ocelot_write(ocelot, ANA_TABLES_MACACCESS_VALID |
94 ANA_TABLES_MACACCESS_DEST_IDX(port) |
95 ANA_TABLES_MACACCESS_ENTRYTYPE(type) |
96 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_LEARN),
97 ANA_TABLES_MACACCESS);
98
99 return ocelot_mact_wait_for_completion(ocelot);
100}
101
102static int ocelot_mact_forget(struct ocelot *ocelot,
103 const unsigned char mac[ETH_ALEN],
104 unsigned int vid)
105{
106 ocelot_mact_select(ocelot, mac, vid);
107
108 /* Issue a forget command */
109 ocelot_write(ocelot,
110 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_FORGET),
111 ANA_TABLES_MACACCESS);
112
113 return ocelot_mact_wait_for_completion(ocelot);
114}
115
116static void ocelot_mact_init(struct ocelot *ocelot)
117{
118 /* Configure the learning mode entries attributes:
119 * - Do not copy the frame to the CPU extraction queues.
120 * - Use the vlan and mac_cpoy for dmac lookup.
121 */
122 ocelot_rmw(ocelot, 0,
123 ANA_AGENCTRL_LEARN_CPU_COPY | ANA_AGENCTRL_IGNORE_DMAC_FLAGS
124 | ANA_AGENCTRL_LEARN_FWD_KILL
125 | ANA_AGENCTRL_LEARN_IGNORE_VLAN,
126 ANA_AGENCTRL);
127
128 /* Clear the MAC table */
129 ocelot_write(ocelot, MACACCESS_CMD_INIT, ANA_TABLES_MACACCESS);
130}
131
132static inline int ocelot_vlant_wait_for_completion(struct ocelot *ocelot)
133{
134 unsigned int val, timeout = 10;
135
Gregory CLEMENT06a36ec2018-10-17 17:26:35 +0200136 /* Wait for the issued vlan table command to be completed, or timeout.
137 * When the command read from ANA_TABLES_VLANACCESS is
138 * VLANACCESS_CMD_IDLE, the issued command completed successfully.
Alexandre Bellonia556c762018-05-14 22:04:57 +0200139 */
140 do {
141 val = ocelot_read(ocelot, ANA_TABLES_VLANACCESS);
142 val &= ANA_TABLES_VLANACCESS_VLAN_TBL_CMD_M;
143 } while (val != ANA_TABLES_VLANACCESS_CMD_IDLE && timeout--);
144
145 if (!timeout)
146 return -ETIMEDOUT;
147
148 return 0;
149}
150
Antoine Tenart71425292018-06-26 14:28:49 +0200151static int ocelot_vlant_set_mask(struct ocelot *ocelot, u16 vid, u32 mask)
152{
153 /* Select the VID to configure */
154 ocelot_write(ocelot, ANA_TABLES_VLANTIDX_V_INDEX(vid),
155 ANA_TABLES_VLANTIDX);
156 /* Set the vlan port members mask and issue a write command */
157 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_VLAN_PORT_MASK(mask) |
158 ANA_TABLES_VLANACCESS_CMD_WRITE,
159 ANA_TABLES_VLANACCESS);
160
161 return ocelot_vlant_wait_for_completion(ocelot);
162}
163
164static void ocelot_vlan_mode(struct ocelot_port *port,
165 netdev_features_t features)
166{
167 struct ocelot *ocelot = port->ocelot;
168 u8 p = port->chip_port;
169 u32 val;
170
171 /* Filtering */
172 val = ocelot_read(ocelot, ANA_VLANMASK);
173 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
174 val |= BIT(p);
175 else
176 val &= ~BIT(p);
177 ocelot_write(ocelot, val, ANA_VLANMASK);
178}
179
180static void ocelot_vlan_port_apply(struct ocelot *ocelot,
181 struct ocelot_port *port)
182{
183 u32 val;
184
185 /* Ingress clasification (ANA_PORT_VLAN_CFG) */
186 /* Default vlan to clasify for untagged frames (may be zero) */
187 val = ANA_PORT_VLAN_CFG_VLAN_VID(port->pvid);
188 if (port->vlan_aware)
189 val |= ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
190 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1);
191
192 ocelot_rmw_gix(ocelot, val,
193 ANA_PORT_VLAN_CFG_VLAN_VID_M |
194 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
195 ANA_PORT_VLAN_CFG_VLAN_POP_CNT_M,
196 ANA_PORT_VLAN_CFG, port->chip_port);
197
198 /* Drop frames with multicast source address */
199 val = ANA_PORT_DROP_CFG_DROP_MC_SMAC_ENA;
200 if (port->vlan_aware && !port->vid)
201 /* If port is vlan-aware and tagged, drop untagged and priority
202 * tagged frames.
203 */
204 val |= ANA_PORT_DROP_CFG_DROP_UNTAGGED_ENA |
205 ANA_PORT_DROP_CFG_DROP_PRIO_S_TAGGED_ENA |
206 ANA_PORT_DROP_CFG_DROP_PRIO_C_TAGGED_ENA;
207 ocelot_write_gix(ocelot, val, ANA_PORT_DROP_CFG, port->chip_port);
208
209 /* Egress configuration (REW_TAG_CFG): VLAN tag type to 8021Q. */
210 val = REW_TAG_CFG_TAG_TPID_CFG(0);
211
212 if (port->vlan_aware) {
213 if (port->vid)
214 /* Tag all frames except when VID == DEFAULT_VLAN */
215 val |= REW_TAG_CFG_TAG_CFG(1);
216 else
217 /* Tag all frames */
218 val |= REW_TAG_CFG_TAG_CFG(3);
219 }
220 ocelot_rmw_gix(ocelot, val,
221 REW_TAG_CFG_TAG_TPID_CFG_M |
222 REW_TAG_CFG_TAG_CFG_M,
223 REW_TAG_CFG, port->chip_port);
224
225 /* Set default VLAN and tag type to 8021Q. */
226 val = REW_PORT_VLAN_CFG_PORT_TPID(ETH_P_8021Q) |
227 REW_PORT_VLAN_CFG_PORT_VID(port->vid);
228 ocelot_rmw_gix(ocelot, val,
229 REW_PORT_VLAN_CFG_PORT_TPID_M |
230 REW_PORT_VLAN_CFG_PORT_VID_M,
231 REW_PORT_VLAN_CFG, port->chip_port);
232}
233
234static int ocelot_vlan_vid_add(struct net_device *dev, u16 vid, bool pvid,
235 bool untagged)
236{
237 struct ocelot_port *port = netdev_priv(dev);
238 struct ocelot *ocelot = port->ocelot;
239 int ret;
240
241 /* Add the port MAC address to with the right VLAN information */
242 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, vid,
243 ENTRYTYPE_LOCKED);
244
245 /* Make the port a member of the VLAN */
246 ocelot->vlan_mask[vid] |= BIT(port->chip_port);
247 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
248 if (ret)
249 return ret;
250
251 /* Default ingress vlan classification */
252 if (pvid)
253 port->pvid = vid;
254
255 /* Untagged egress vlan clasification */
256 if (untagged)
257 port->vid = vid;
258
259 ocelot_vlan_port_apply(ocelot, port);
260
261 return 0;
262}
263
264static int ocelot_vlan_vid_del(struct net_device *dev, u16 vid)
265{
266 struct ocelot_port *port = netdev_priv(dev);
267 struct ocelot *ocelot = port->ocelot;
268 int ret;
269
270 /* 8021q removes VID 0 on module unload for all interfaces
271 * with VLAN filtering feature. We need to keep it to receive
272 * untagged traffic.
273 */
274 if (vid == 0)
275 return 0;
276
277 /* Del the port MAC address to with the right VLAN information */
278 ocelot_mact_forget(ocelot, dev->dev_addr, vid);
279
280 /* Stop the port from being a member of the vlan */
281 ocelot->vlan_mask[vid] &= ~BIT(port->chip_port);
282 ret = ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
283 if (ret)
284 return ret;
285
286 /* Ingress */
287 if (port->pvid == vid)
288 port->pvid = 0;
289
290 /* Egress */
291 if (port->vid == vid)
292 port->vid = 0;
293
294 ocelot_vlan_port_apply(ocelot, port);
295
296 return 0;
297}
298
Alexandre Bellonia556c762018-05-14 22:04:57 +0200299static void ocelot_vlan_init(struct ocelot *ocelot)
300{
Antoine Tenart71425292018-06-26 14:28:49 +0200301 u16 port, vid;
302
Alexandre Bellonia556c762018-05-14 22:04:57 +0200303 /* Clear VLAN table, by default all ports are members of all VLANs */
304 ocelot_write(ocelot, ANA_TABLES_VLANACCESS_CMD_INIT,
305 ANA_TABLES_VLANACCESS);
306 ocelot_vlant_wait_for_completion(ocelot);
Antoine Tenart71425292018-06-26 14:28:49 +0200307
308 /* Configure the port VLAN memberships */
309 for (vid = 1; vid < VLAN_N_VID; vid++) {
310 ocelot->vlan_mask[vid] = 0;
311 ocelot_vlant_set_mask(ocelot, vid, ocelot->vlan_mask[vid]);
312 }
313
314 /* Because VLAN filtering is enabled, we need VID 0 to get untagged
315 * traffic. It is added automatically if 8021q module is loaded, but
316 * we can't rely on it since module may be not loaded.
317 */
318 ocelot->vlan_mask[0] = GENMASK(ocelot->num_phys_ports - 1, 0);
319 ocelot_vlant_set_mask(ocelot, 0, ocelot->vlan_mask[0]);
320
321 /* Configure the CPU port to be VLAN aware */
322 ocelot_write_gix(ocelot, ANA_PORT_VLAN_CFG_VLAN_VID(0) |
323 ANA_PORT_VLAN_CFG_VLAN_AWARE_ENA |
324 ANA_PORT_VLAN_CFG_VLAN_POP_CNT(1),
325 ANA_PORT_VLAN_CFG, ocelot->num_phys_ports);
326
327 /* Set vlan ingress filter mask to all ports but the CPU port by
328 * default.
329 */
330 ocelot_write(ocelot, GENMASK(9, 0), ANA_VLANMASK);
331
332 for (port = 0; port < ocelot->num_phys_ports; port++) {
333 ocelot_write_gix(ocelot, 0, REW_PORT_VLAN_CFG, port);
334 ocelot_write_gix(ocelot, 0, REW_TAG_CFG, port);
335 }
Alexandre Bellonia556c762018-05-14 22:04:57 +0200336}
337
338/* Watermark encode
339 * Bit 8: Unit; 0:1, 1:16
340 * Bit 7-0: Value to be multiplied with unit
341 */
342static u16 ocelot_wm_enc(u16 value)
343{
344 if (value >= BIT(8))
345 return BIT(8) | (value / 16);
346
347 return value;
348}
349
350static void ocelot_port_adjust_link(struct net_device *dev)
351{
352 struct ocelot_port *port = netdev_priv(dev);
353 struct ocelot *ocelot = port->ocelot;
354 u8 p = port->chip_port;
355 int speed, atop_wm, mode = 0;
356
357 switch (dev->phydev->speed) {
358 case SPEED_10:
359 speed = OCELOT_SPEED_10;
360 break;
361 case SPEED_100:
362 speed = OCELOT_SPEED_100;
363 break;
364 case SPEED_1000:
365 speed = OCELOT_SPEED_1000;
366 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
367 break;
368 case SPEED_2500:
369 speed = OCELOT_SPEED_2500;
370 mode = DEV_MAC_MODE_CFG_GIGA_MODE_ENA;
371 break;
372 default:
373 netdev_err(dev, "Unsupported PHY speed: %d\n",
374 dev->phydev->speed);
375 return;
376 }
377
378 phy_print_status(dev->phydev);
379
380 if (!dev->phydev->link)
381 return;
382
383 /* Only full duplex supported for now */
384 ocelot_port_writel(port, DEV_MAC_MODE_CFG_FDX_ENA |
385 mode, DEV_MAC_MODE_CFG);
386
387 /* Set MAC IFG Gaps
388 * FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 0
389 * !FDX: TX_IFG = 5, RX_IFG1 = RX_IFG2 = 5
390 */
391 ocelot_port_writel(port, DEV_MAC_IFG_CFG_TX_IFG(5), DEV_MAC_IFG_CFG);
392
393 /* Load seed (0) and set MAC HDX late collision */
394 ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67) |
395 DEV_MAC_HDX_CFG_SEED_LOAD,
396 DEV_MAC_HDX_CFG);
397 mdelay(1);
398 ocelot_port_writel(port, DEV_MAC_HDX_CFG_LATE_COL_POS(67),
399 DEV_MAC_HDX_CFG);
400
401 /* Disable HDX fast control */
402 ocelot_port_writel(port, DEV_PORT_MISC_HDX_FAST_DIS, DEV_PORT_MISC);
403
404 /* SGMII only for now */
405 ocelot_port_writel(port, PCS1G_MODE_CFG_SGMII_MODE_ENA, PCS1G_MODE_CFG);
406 ocelot_port_writel(port, PCS1G_SD_CFG_SD_SEL, PCS1G_SD_CFG);
407
408 /* Enable PCS */
409 ocelot_port_writel(port, PCS1G_CFG_PCS_ENA, PCS1G_CFG);
410
411 /* No aneg on SGMII */
412 ocelot_port_writel(port, 0, PCS1G_ANEG_CFG);
413
414 /* No loopback */
415 ocelot_port_writel(port, 0, PCS1G_LB_CFG);
416
417 /* Set Max Length and maximum tags allowed */
418 ocelot_port_writel(port, VLAN_ETH_FRAME_LEN, DEV_MAC_MAXLEN_CFG);
419 ocelot_port_writel(port, DEV_MAC_TAGS_CFG_TAG_ID(ETH_P_8021AD) |
420 DEV_MAC_TAGS_CFG_VLAN_AWR_ENA |
421 DEV_MAC_TAGS_CFG_VLAN_LEN_AWR_ENA,
422 DEV_MAC_TAGS_CFG);
423
424 /* Enable MAC module */
425 ocelot_port_writel(port, DEV_MAC_ENA_CFG_RX_ENA |
426 DEV_MAC_ENA_CFG_TX_ENA, DEV_MAC_ENA_CFG);
427
428 /* Take MAC, Port, Phy (intern) and PCS (SGMII/Serdes) clock out of
429 * reset */
430 ocelot_port_writel(port, DEV_CLOCK_CFG_LINK_SPEED(speed),
431 DEV_CLOCK_CFG);
432
433 /* Set SMAC of Pause frame (00:00:00:00:00:00) */
434 ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_HIGH_CFG);
435 ocelot_port_writel(port, 0, DEV_MAC_FC_MAC_LOW_CFG);
436
437 /* No PFC */
438 ocelot_write_gix(ocelot, ANA_PFC_PFC_CFG_FC_LINK_SPEED(speed),
439 ANA_PFC_PFC_CFG, p);
440
441 /* Set Pause WM hysteresis
442 * 152 = 6 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
443 * 101 = 4 * VLAN_ETH_FRAME_LEN / OCELOT_BUFFER_CELL_SZ
444 */
445 ocelot_write_rix(ocelot, SYS_PAUSE_CFG_PAUSE_ENA |
446 SYS_PAUSE_CFG_PAUSE_STOP(101) |
447 SYS_PAUSE_CFG_PAUSE_START(152), SYS_PAUSE_CFG, p);
448
449 /* Core: Enable port for frame transfer */
450 ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
451 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
452 QSYS_SWITCH_PORT_MODE_PORT_ENA,
453 QSYS_SWITCH_PORT_MODE, p);
454
455 /* Flow control */
456 ocelot_write_rix(ocelot, SYS_MAC_FC_CFG_PAUSE_VAL_CFG(0xffff) |
457 SYS_MAC_FC_CFG_RX_FC_ENA | SYS_MAC_FC_CFG_TX_FC_ENA |
458 SYS_MAC_FC_CFG_ZERO_PAUSE_ENA |
459 SYS_MAC_FC_CFG_FC_LATENCY_CFG(0x7) |
460 SYS_MAC_FC_CFG_FC_LINK_SPEED(speed),
461 SYS_MAC_FC_CFG, p);
462 ocelot_write_rix(ocelot, 0, ANA_POL_FLOWC, p);
463
464 /* Tail dropping watermark */
465 atop_wm = (ocelot->shared_queue_sz - 9 * VLAN_ETH_FRAME_LEN) / OCELOT_BUFFER_CELL_SZ;
466 ocelot_write_rix(ocelot, ocelot_wm_enc(9 * VLAN_ETH_FRAME_LEN),
467 SYS_ATOP, p);
468 ocelot_write(ocelot, ocelot_wm_enc(atop_wm), SYS_ATOP_TOT_CFG);
469}
470
471static int ocelot_port_open(struct net_device *dev)
472{
473 struct ocelot_port *port = netdev_priv(dev);
474 struct ocelot *ocelot = port->ocelot;
Quentin Schulz71e32a202018-10-04 14:22:08 +0200475 enum phy_mode phy_mode;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200476 int err;
477
478 /* Enable receiving frames on the port, and activate auto-learning of
479 * MAC addresses.
480 */
481 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_LEARNAUTO |
482 ANA_PORT_PORT_CFG_RECV_ENA |
483 ANA_PORT_PORT_CFG_PORTID_VAL(port->chip_port),
484 ANA_PORT_PORT_CFG, port->chip_port);
485
Quentin Schulz71e32a202018-10-04 14:22:08 +0200486 if (port->serdes) {
487 if (port->phy_mode == PHY_INTERFACE_MODE_SGMII)
488 phy_mode = PHY_MODE_SGMII;
489 else
490 phy_mode = PHY_MODE_QSGMII;
491
492 err = phy_set_mode(port->serdes, phy_mode);
493 if (err) {
494 netdev_err(dev, "Could not set mode of SerDes\n");
495 return err;
496 }
497 }
498
Alexandre Bellonia556c762018-05-14 22:04:57 +0200499 err = phy_connect_direct(dev, port->phy, &ocelot_port_adjust_link,
Quentin Schulz71e32a202018-10-04 14:22:08 +0200500 port->phy_mode);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200501 if (err) {
502 netdev_err(dev, "Could not attach to PHY\n");
503 return err;
504 }
505
506 dev->phydev = port->phy;
507
508 phy_attached_info(port->phy);
509 phy_start(port->phy);
510 return 0;
511}
512
513static int ocelot_port_stop(struct net_device *dev)
514{
515 struct ocelot_port *port = netdev_priv(dev);
516
517 phy_disconnect(port->phy);
518
519 dev->phydev = NULL;
520
521 ocelot_port_writel(port, 0, DEV_MAC_ENA_CFG);
522 ocelot_rmw_rix(port->ocelot, 0, QSYS_SWITCH_PORT_MODE_PORT_ENA,
523 QSYS_SWITCH_PORT_MODE, port->chip_port);
524 return 0;
525}
526
527/* Generate the IFH for frame injection
528 *
529 * The IFH is a 128bit-value
530 * bit 127: bypass the analyzer processing
531 * bit 56-67: destination mask
532 * bit 28-29: pop_cnt: 3 disables all rewriting of the frame
533 * bit 20-27: cpu extraction queue mask
534 * bit 16: tag type 0: C-tag, 1: S-tag
535 * bit 0-11: VID
536 */
537static int ocelot_gen_ifh(u32 *ifh, struct frame_info *info)
538{
539 ifh[0] = IFH_INJ_BYPASS;
Antoine Tenart08d02362018-06-20 10:50:46 +0200540 ifh[1] = (0xf00 & info->port) >> 8;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200541 ifh[2] = (0xff & info->port) << 24;
Antoine Tenart08d02362018-06-20 10:50:46 +0200542 ifh[3] = (info->tag_type << 16) | info->vid;
Alexandre Bellonia556c762018-05-14 22:04:57 +0200543
544 return 0;
545}
546
547static int ocelot_port_xmit(struct sk_buff *skb, struct net_device *dev)
548{
549 struct ocelot_port *port = netdev_priv(dev);
550 struct ocelot *ocelot = port->ocelot;
551 u32 val, ifh[IFH_LEN];
552 struct frame_info info = {};
553 u8 grp = 0; /* Send everything on CPU group 0 */
554 unsigned int i, count, last;
555
556 val = ocelot_read(ocelot, QS_INJ_STATUS);
557 if (!(val & QS_INJ_STATUS_FIFO_RDY(BIT(grp))) ||
558 (val & QS_INJ_STATUS_WMARK_REACHED(BIT(grp))))
559 return NETDEV_TX_BUSY;
560
561 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
562 QS_INJ_CTRL_SOF, QS_INJ_CTRL, grp);
563
564 info.port = BIT(port->chip_port);
Antoine Tenart08d02362018-06-20 10:50:46 +0200565 info.tag_type = IFH_TAG_TYPE_C;
566 info.vid = skb_vlan_tag_get(skb);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200567 ocelot_gen_ifh(ifh, &info);
568
569 for (i = 0; i < IFH_LEN; i++)
Antoine Tenartc2cd6502018-06-22 11:50:52 +0200570 ocelot_write_rix(ocelot, (__force u32)cpu_to_be32(ifh[i]),
571 QS_INJ_WR, grp);
Alexandre Bellonia556c762018-05-14 22:04:57 +0200572
573 count = (skb->len + 3) / 4;
574 last = skb->len % 4;
575 for (i = 0; i < count; i++) {
576 ocelot_write_rix(ocelot, ((u32 *)skb->data)[i], QS_INJ_WR, grp);
577 }
578
579 /* Add padding */
580 while (i < (OCELOT_BUFFER_CELL_SZ / 4)) {
581 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
582 i++;
583 }
584
585 /* Indicate EOF and valid bytes in last word */
586 ocelot_write_rix(ocelot, QS_INJ_CTRL_GAP_SIZE(1) |
587 QS_INJ_CTRL_VLD_BYTES(skb->len < OCELOT_BUFFER_CELL_SZ ? 0 : last) |
588 QS_INJ_CTRL_EOF,
589 QS_INJ_CTRL, grp);
590
591 /* Add dummy CRC */
592 ocelot_write_rix(ocelot, 0, QS_INJ_WR, grp);
593 skb_tx_timestamp(skb);
594
595 dev->stats.tx_packets++;
596 dev->stats.tx_bytes += skb->len;
597 dev_kfree_skb_any(skb);
598
599 return NETDEV_TX_OK;
600}
601
602static void ocelot_mact_mc_reset(struct ocelot_port *port)
603{
604 struct ocelot *ocelot = port->ocelot;
605 struct netdev_hw_addr *ha, *n;
606
607 /* Free and forget all the MAC addresses stored in the port private mc
608 * list. These are mc addresses that were previously added by calling
609 * ocelot_mact_mc_add().
610 */
611 list_for_each_entry_safe(ha, n, &port->mc, list) {
612 ocelot_mact_forget(ocelot, ha->addr, port->pvid);
613 list_del(&ha->list);
614 kfree(ha);
615 }
616}
617
618static int ocelot_mact_mc_add(struct ocelot_port *port,
619 struct netdev_hw_addr *hw_addr)
620{
621 struct ocelot *ocelot = port->ocelot;
622 struct netdev_hw_addr *ha = kzalloc(sizeof(*ha), GFP_KERNEL);
623
624 if (!ha)
625 return -ENOMEM;
626
627 memcpy(ha, hw_addr, sizeof(*ha));
628 list_add_tail(&ha->list, &port->mc);
629
630 ocelot_mact_learn(ocelot, PGID_CPU, ha->addr, port->pvid,
631 ENTRYTYPE_LOCKED);
632
633 return 0;
634}
635
636static void ocelot_set_rx_mode(struct net_device *dev)
637{
638 struct ocelot_port *port = netdev_priv(dev);
639 struct ocelot *ocelot = port->ocelot;
640 struct netdev_hw_addr *ha;
641 int i;
642 u32 val;
643
644 /* This doesn't handle promiscuous mode because the bridge core is
645 * setting IFF_PROMISC on all slave interfaces and all frames would be
646 * forwarded to the CPU port.
647 */
648 val = GENMASK(ocelot->num_phys_ports - 1, 0);
649 for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++)
650 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
651
652 /* Handle the device multicast addresses. First remove all the
653 * previously installed addresses and then add the latest ones to the
654 * mac table.
655 */
656 ocelot_mact_mc_reset(port);
657 netdev_for_each_mc_addr(ha, dev)
658 ocelot_mact_mc_add(port, ha);
659}
660
661static int ocelot_port_get_phys_port_name(struct net_device *dev,
662 char *buf, size_t len)
663{
664 struct ocelot_port *port = netdev_priv(dev);
665 int ret;
666
667 ret = snprintf(buf, len, "p%d", port->chip_port);
668 if (ret >= len)
669 return -EINVAL;
670
671 return 0;
672}
673
674static int ocelot_port_set_mac_address(struct net_device *dev, void *p)
675{
676 struct ocelot_port *port = netdev_priv(dev);
677 struct ocelot *ocelot = port->ocelot;
678 const struct sockaddr *addr = p;
679
680 /* Learn the new net device MAC address in the mac table. */
681 ocelot_mact_learn(ocelot, PGID_CPU, addr->sa_data, port->pvid,
682 ENTRYTYPE_LOCKED);
683 /* Then forget the previous one. */
684 ocelot_mact_forget(ocelot, dev->dev_addr, port->pvid);
685
686 ether_addr_copy(dev->dev_addr, addr->sa_data);
687 return 0;
688}
689
690static void ocelot_get_stats64(struct net_device *dev,
691 struct rtnl_link_stats64 *stats)
692{
693 struct ocelot_port *port = netdev_priv(dev);
694 struct ocelot *ocelot = port->ocelot;
695
696 /* Configure the port to read the stats from */
697 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port->chip_port),
698 SYS_STAT_CFG);
699
700 /* Get Rx stats */
701 stats->rx_bytes = ocelot_read(ocelot, SYS_COUNT_RX_OCTETS);
702 stats->rx_packets = ocelot_read(ocelot, SYS_COUNT_RX_SHORTS) +
703 ocelot_read(ocelot, SYS_COUNT_RX_FRAGMENTS) +
704 ocelot_read(ocelot, SYS_COUNT_RX_JABBERS) +
705 ocelot_read(ocelot, SYS_COUNT_RX_LONGS) +
706 ocelot_read(ocelot, SYS_COUNT_RX_64) +
707 ocelot_read(ocelot, SYS_COUNT_RX_65_127) +
708 ocelot_read(ocelot, SYS_COUNT_RX_128_255) +
709 ocelot_read(ocelot, SYS_COUNT_RX_256_1023) +
710 ocelot_read(ocelot, SYS_COUNT_RX_1024_1526) +
711 ocelot_read(ocelot, SYS_COUNT_RX_1527_MAX);
712 stats->multicast = ocelot_read(ocelot, SYS_COUNT_RX_MULTICAST);
713 stats->rx_dropped = dev->stats.rx_dropped;
714
715 /* Get Tx stats */
716 stats->tx_bytes = ocelot_read(ocelot, SYS_COUNT_TX_OCTETS);
717 stats->tx_packets = ocelot_read(ocelot, SYS_COUNT_TX_64) +
718 ocelot_read(ocelot, SYS_COUNT_TX_65_127) +
719 ocelot_read(ocelot, SYS_COUNT_TX_128_511) +
720 ocelot_read(ocelot, SYS_COUNT_TX_512_1023) +
721 ocelot_read(ocelot, SYS_COUNT_TX_1024_1526) +
722 ocelot_read(ocelot, SYS_COUNT_TX_1527_MAX);
723 stats->tx_dropped = ocelot_read(ocelot, SYS_COUNT_TX_DROPS) +
724 ocelot_read(ocelot, SYS_COUNT_TX_AGING);
725 stats->collisions = ocelot_read(ocelot, SYS_COUNT_TX_COLLISION);
726}
727
728static int ocelot_fdb_add(struct ndmsg *ndm, struct nlattr *tb[],
729 struct net_device *dev, const unsigned char *addr,
730 u16 vid, u16 flags)
731{
732 struct ocelot_port *port = netdev_priv(dev);
733 struct ocelot *ocelot = port->ocelot;
734
Antoine Tenart71425292018-06-26 14:28:49 +0200735 if (!vid) {
736 if (!port->vlan_aware)
737 /* If the bridge is not VLAN aware and no VID was
738 * provided, set it to pvid to ensure the MAC entry
739 * matches incoming untagged packets
740 */
741 vid = port->pvid;
742 else
743 /* If the bridge is VLAN aware a VID must be provided as
744 * otherwise the learnt entry wouldn't match any frame.
745 */
746 return -EINVAL;
747 }
748
Alexandre Bellonia556c762018-05-14 22:04:57 +0200749 return ocelot_mact_learn(ocelot, port->chip_port, addr, vid,
750 ENTRYTYPE_NORMAL);
751}
752
753static int ocelot_fdb_del(struct ndmsg *ndm, struct nlattr *tb[],
754 struct net_device *dev,
755 const unsigned char *addr, u16 vid)
756{
757 struct ocelot_port *port = netdev_priv(dev);
758 struct ocelot *ocelot = port->ocelot;
759
760 return ocelot_mact_forget(ocelot, addr, vid);
761}
762
763struct ocelot_dump_ctx {
764 struct net_device *dev;
765 struct sk_buff *skb;
766 struct netlink_callback *cb;
767 int idx;
768};
769
770static int ocelot_fdb_do_dump(struct ocelot_mact_entry *entry,
771 struct ocelot_dump_ctx *dump)
772{
773 u32 portid = NETLINK_CB(dump->cb->skb).portid;
774 u32 seq = dump->cb->nlh->nlmsg_seq;
775 struct nlmsghdr *nlh;
776 struct ndmsg *ndm;
777
778 if (dump->idx < dump->cb->args[2])
779 goto skip;
780
781 nlh = nlmsg_put(dump->skb, portid, seq, RTM_NEWNEIGH,
782 sizeof(*ndm), NLM_F_MULTI);
783 if (!nlh)
784 return -EMSGSIZE;
785
786 ndm = nlmsg_data(nlh);
787 ndm->ndm_family = AF_BRIDGE;
788 ndm->ndm_pad1 = 0;
789 ndm->ndm_pad2 = 0;
790 ndm->ndm_flags = NTF_SELF;
791 ndm->ndm_type = 0;
792 ndm->ndm_ifindex = dump->dev->ifindex;
793 ndm->ndm_state = NUD_REACHABLE;
794
795 if (nla_put(dump->skb, NDA_LLADDR, ETH_ALEN, entry->mac))
796 goto nla_put_failure;
797
798 if (entry->vid && nla_put_u16(dump->skb, NDA_VLAN, entry->vid))
799 goto nla_put_failure;
800
801 nlmsg_end(dump->skb, nlh);
802
803skip:
804 dump->idx++;
805 return 0;
806
807nla_put_failure:
808 nlmsg_cancel(dump->skb, nlh);
809 return -EMSGSIZE;
810}
811
812static inline int ocelot_mact_read(struct ocelot_port *port, int row, int col,
813 struct ocelot_mact_entry *entry)
814{
815 struct ocelot *ocelot = port->ocelot;
816 char mac[ETH_ALEN];
817 u32 val, dst, macl, mach;
818
819 /* Set row and column to read from */
820 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_M_INDEX, row);
821 ocelot_field_write(ocelot, ANA_TABLES_MACTINDX_BUCKET, col);
822
823 /* Issue a read command */
824 ocelot_write(ocelot,
825 ANA_TABLES_MACACCESS_MAC_TABLE_CMD(MACACCESS_CMD_READ),
826 ANA_TABLES_MACACCESS);
827
828 if (ocelot_mact_wait_for_completion(ocelot))
829 return -ETIMEDOUT;
830
831 /* Read the entry flags */
832 val = ocelot_read(ocelot, ANA_TABLES_MACACCESS);
833 if (!(val & ANA_TABLES_MACACCESS_VALID))
834 return -EINVAL;
835
836 /* If the entry read has another port configured as its destination,
837 * do not report it.
838 */
839 dst = (val & ANA_TABLES_MACACCESS_DEST_IDX_M) >> 3;
840 if (dst != port->chip_port)
841 return -EINVAL;
842
843 /* Get the entry's MAC address and VLAN id */
844 macl = ocelot_read(ocelot, ANA_TABLES_MACLDATA);
845 mach = ocelot_read(ocelot, ANA_TABLES_MACHDATA);
846
847 mac[0] = (mach >> 8) & 0xff;
848 mac[1] = (mach >> 0) & 0xff;
849 mac[2] = (macl >> 24) & 0xff;
850 mac[3] = (macl >> 16) & 0xff;
851 mac[4] = (macl >> 8) & 0xff;
852 mac[5] = (macl >> 0) & 0xff;
853
854 entry->vid = (mach >> 16) & 0xfff;
855 ether_addr_copy(entry->mac, mac);
856
857 return 0;
858}
859
860static int ocelot_fdb_dump(struct sk_buff *skb, struct netlink_callback *cb,
861 struct net_device *dev,
862 struct net_device *filter_dev, int *idx)
863{
864 struct ocelot_port *port = netdev_priv(dev);
865 int i, j, ret = 0;
866 struct ocelot_dump_ctx dump = {
867 .dev = dev,
868 .skb = skb,
869 .cb = cb,
870 .idx = *idx,
871 };
872
873 struct ocelot_mact_entry entry;
874
875 /* Loop through all the mac tables entries. There are 1024 rows of 4
876 * entries.
877 */
878 for (i = 0; i < 1024; i++) {
879 for (j = 0; j < 4; j++) {
880 ret = ocelot_mact_read(port, i, j, &entry);
881 /* If the entry is invalid (wrong port, invalid...),
882 * skip it.
883 */
884 if (ret == -EINVAL)
885 continue;
886 else if (ret)
887 goto end;
888
889 ret = ocelot_fdb_do_dump(&entry, &dump);
890 if (ret)
891 goto end;
892 }
893 }
894
895end:
896 *idx = dump.idx;
897 return ret;
898}
899
Antoine Tenart71425292018-06-26 14:28:49 +0200900static int ocelot_vlan_rx_add_vid(struct net_device *dev, __be16 proto,
901 u16 vid)
902{
903 return ocelot_vlan_vid_add(dev, vid, false, true);
904}
905
906static int ocelot_vlan_rx_kill_vid(struct net_device *dev, __be16 proto,
907 u16 vid)
908{
909 return ocelot_vlan_vid_del(dev, vid);
910}
911
912static int ocelot_set_features(struct net_device *dev,
913 netdev_features_t features)
914{
915 struct ocelot_port *port = netdev_priv(dev);
916 netdev_features_t changed = dev->features ^ features;
917
918 if (changed & NETIF_F_HW_VLAN_CTAG_FILTER)
919 ocelot_vlan_mode(port, features);
920
921 return 0;
922}
923
Alexandre Bellonia556c762018-05-14 22:04:57 +0200924static const struct net_device_ops ocelot_port_netdev_ops = {
925 .ndo_open = ocelot_port_open,
926 .ndo_stop = ocelot_port_stop,
927 .ndo_start_xmit = ocelot_port_xmit,
928 .ndo_set_rx_mode = ocelot_set_rx_mode,
929 .ndo_get_phys_port_name = ocelot_port_get_phys_port_name,
930 .ndo_set_mac_address = ocelot_port_set_mac_address,
931 .ndo_get_stats64 = ocelot_get_stats64,
932 .ndo_fdb_add = ocelot_fdb_add,
933 .ndo_fdb_del = ocelot_fdb_del,
934 .ndo_fdb_dump = ocelot_fdb_dump,
Antoine Tenart71425292018-06-26 14:28:49 +0200935 .ndo_vlan_rx_add_vid = ocelot_vlan_rx_add_vid,
936 .ndo_vlan_rx_kill_vid = ocelot_vlan_rx_kill_vid,
937 .ndo_set_features = ocelot_set_features,
Alexandre Bellonia556c762018-05-14 22:04:57 +0200938};
939
940static void ocelot_get_strings(struct net_device *netdev, u32 sset, u8 *data)
941{
942 struct ocelot_port *port = netdev_priv(netdev);
943 struct ocelot *ocelot = port->ocelot;
944 int i;
945
946 if (sset != ETH_SS_STATS)
947 return;
948
949 for (i = 0; i < ocelot->num_stats; i++)
950 memcpy(data + i * ETH_GSTRING_LEN, ocelot->stats_layout[i].name,
951 ETH_GSTRING_LEN);
952}
953
954static void ocelot_check_stats(struct work_struct *work)
955{
956 struct delayed_work *del_work = to_delayed_work(work);
957 struct ocelot *ocelot = container_of(del_work, struct ocelot, stats_work);
958 int i, j;
959
960 mutex_lock(&ocelot->stats_lock);
961
962 for (i = 0; i < ocelot->num_phys_ports; i++) {
963 /* Configure the port to read the stats from */
964 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(i), SYS_STAT_CFG);
965
966 for (j = 0; j < ocelot->num_stats; j++) {
967 u32 val;
968 unsigned int idx = i * ocelot->num_stats + j;
969
970 val = ocelot_read_rix(ocelot, SYS_COUNT_RX_OCTETS,
971 ocelot->stats_layout[j].offset);
972
973 if (val < (ocelot->stats[idx] & U32_MAX))
974 ocelot->stats[idx] += (u64)1 << 32;
975
976 ocelot->stats[idx] = (ocelot->stats[idx] &
977 ~(u64)U32_MAX) + val;
978 }
979 }
980
981 cancel_delayed_work(&ocelot->stats_work);
982 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
983 OCELOT_STATS_CHECK_DELAY);
984
985 mutex_unlock(&ocelot->stats_lock);
986}
987
988static void ocelot_get_ethtool_stats(struct net_device *dev,
989 struct ethtool_stats *stats, u64 *data)
990{
991 struct ocelot_port *port = netdev_priv(dev);
992 struct ocelot *ocelot = port->ocelot;
993 int i;
994
995 /* check and update now */
996 ocelot_check_stats(&ocelot->stats_work.work);
997
998 /* Copy all counters */
999 for (i = 0; i < ocelot->num_stats; i++)
1000 *data++ = ocelot->stats[port->chip_port * ocelot->num_stats + i];
1001}
1002
1003static int ocelot_get_sset_count(struct net_device *dev, int sset)
1004{
1005 struct ocelot_port *port = netdev_priv(dev);
1006 struct ocelot *ocelot = port->ocelot;
1007
1008 if (sset != ETH_SS_STATS)
1009 return -EOPNOTSUPP;
1010 return ocelot->num_stats;
1011}
1012
1013static const struct ethtool_ops ocelot_ethtool_ops = {
1014 .get_strings = ocelot_get_strings,
1015 .get_ethtool_stats = ocelot_get_ethtool_stats,
1016 .get_sset_count = ocelot_get_sset_count,
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001017 .get_link_ksettings = phy_ethtool_get_link_ksettings,
1018 .set_link_ksettings = phy_ethtool_set_link_ksettings,
Alexandre Bellonia556c762018-05-14 22:04:57 +02001019};
1020
1021static int ocelot_port_attr_get(struct net_device *dev,
1022 struct switchdev_attr *attr)
1023{
1024 struct ocelot_port *ocelot_port = netdev_priv(dev);
1025 struct ocelot *ocelot = ocelot_port->ocelot;
1026
1027 switch (attr->id) {
1028 case SWITCHDEV_ATTR_ID_PORT_PARENT_ID:
1029 attr->u.ppid.id_len = sizeof(ocelot->base_mac);
1030 memcpy(&attr->u.ppid.id, &ocelot->base_mac,
1031 attr->u.ppid.id_len);
1032 break;
1033 default:
1034 return -EOPNOTSUPP;
1035 }
1036
1037 return 0;
1038}
1039
1040static int ocelot_port_attr_stp_state_set(struct ocelot_port *ocelot_port,
1041 struct switchdev_trans *trans,
1042 u8 state)
1043{
1044 struct ocelot *ocelot = ocelot_port->ocelot;
1045 u32 port_cfg;
1046 int port, i;
1047
1048 if (switchdev_trans_ph_prepare(trans))
1049 return 0;
1050
1051 if (!(BIT(ocelot_port->chip_port) & ocelot->bridge_mask))
1052 return 0;
1053
1054 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG,
1055 ocelot_port->chip_port);
1056
1057 switch (state) {
1058 case BR_STATE_FORWARDING:
1059 ocelot->bridge_fwd_mask |= BIT(ocelot_port->chip_port);
1060 /* Fallthrough */
1061 case BR_STATE_LEARNING:
1062 port_cfg |= ANA_PORT_PORT_CFG_LEARN_ENA;
1063 break;
1064
1065 default:
1066 port_cfg &= ~ANA_PORT_PORT_CFG_LEARN_ENA;
1067 ocelot->bridge_fwd_mask &= ~BIT(ocelot_port->chip_port);
1068 break;
1069 }
1070
1071 ocelot_write_gix(ocelot, port_cfg, ANA_PORT_PORT_CFG,
1072 ocelot_port->chip_port);
1073
1074 /* Apply FWD mask. The loop is needed to add/remove the current port as
1075 * a source for the other ports.
1076 */
1077 for (port = 0; port < ocelot->num_phys_ports; port++) {
1078 if (ocelot->bridge_fwd_mask & BIT(port)) {
1079 unsigned long mask = ocelot->bridge_fwd_mask & ~BIT(port);
1080
1081 for (i = 0; i < ocelot->num_phys_ports; i++) {
1082 unsigned long bond_mask = ocelot->lags[i];
1083
1084 if (!bond_mask)
1085 continue;
1086
1087 if (bond_mask & BIT(port)) {
1088 mask &= ~bond_mask;
1089 break;
1090 }
1091 }
1092
1093 ocelot_write_rix(ocelot,
1094 BIT(ocelot->num_phys_ports) | mask,
1095 ANA_PGID_PGID, PGID_SRC + port);
1096 } else {
1097 /* Only the CPU port, this is compatible with link
1098 * aggregation.
1099 */
1100 ocelot_write_rix(ocelot,
1101 BIT(ocelot->num_phys_ports),
1102 ANA_PGID_PGID, PGID_SRC + port);
1103 }
1104 }
1105
1106 return 0;
1107}
1108
1109static void ocelot_port_attr_ageing_set(struct ocelot_port *ocelot_port,
1110 unsigned long ageing_clock_t)
1111{
1112 struct ocelot *ocelot = ocelot_port->ocelot;
1113 unsigned long ageing_jiffies = clock_t_to_jiffies(ageing_clock_t);
1114 u32 ageing_time = jiffies_to_msecs(ageing_jiffies) / 1000;
1115
1116 ocelot_write(ocelot, ANA_AUTOAGE_AGE_PERIOD(ageing_time / 2),
1117 ANA_AUTOAGE);
1118}
1119
1120static void ocelot_port_attr_mc_set(struct ocelot_port *port, bool mc)
1121{
1122 struct ocelot *ocelot = port->ocelot;
1123 u32 val = ocelot_read_gix(ocelot, ANA_PORT_CPU_FWD_CFG,
1124 port->chip_port);
1125
1126 if (mc)
1127 val |= ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1128 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1129 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA;
1130 else
1131 val &= ~(ANA_PORT_CPU_FWD_CFG_CPU_IGMP_REDIR_ENA |
1132 ANA_PORT_CPU_FWD_CFG_CPU_MLD_REDIR_ENA |
1133 ANA_PORT_CPU_FWD_CFG_CPU_IPMC_CTRL_COPY_ENA);
1134
1135 ocelot_write_gix(ocelot, val, ANA_PORT_CPU_FWD_CFG, port->chip_port);
1136}
1137
1138static int ocelot_port_attr_set(struct net_device *dev,
1139 const struct switchdev_attr *attr,
1140 struct switchdev_trans *trans)
1141{
1142 struct ocelot_port *ocelot_port = netdev_priv(dev);
1143 int err = 0;
1144
1145 switch (attr->id) {
1146 case SWITCHDEV_ATTR_ID_PORT_STP_STATE:
1147 ocelot_port_attr_stp_state_set(ocelot_port, trans,
1148 attr->u.stp_state);
1149 break;
1150 case SWITCHDEV_ATTR_ID_BRIDGE_AGEING_TIME:
1151 ocelot_port_attr_ageing_set(ocelot_port, attr->u.ageing_time);
1152 break;
Antoine Tenart71425292018-06-26 14:28:49 +02001153 case SWITCHDEV_ATTR_ID_BRIDGE_VLAN_FILTERING:
1154 ocelot_port->vlan_aware = attr->u.vlan_filtering;
1155 ocelot_vlan_port_apply(ocelot_port->ocelot, ocelot_port);
1156 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001157 case SWITCHDEV_ATTR_ID_BRIDGE_MC_DISABLED:
1158 ocelot_port_attr_mc_set(ocelot_port, !attr->u.mc_disabled);
1159 break;
1160 default:
1161 err = -EOPNOTSUPP;
1162 break;
1163 }
1164
1165 return err;
1166}
1167
Antoine Tenart71425292018-06-26 14:28:49 +02001168static int ocelot_port_obj_add_vlan(struct net_device *dev,
1169 const struct switchdev_obj_port_vlan *vlan,
1170 struct switchdev_trans *trans)
1171{
1172 int ret;
1173 u16 vid;
1174
1175 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1176 ret = ocelot_vlan_vid_add(dev, vid,
1177 vlan->flags & BRIDGE_VLAN_INFO_PVID,
1178 vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED);
1179 if (ret)
1180 return ret;
1181 }
1182
1183 return 0;
1184}
1185
1186static int ocelot_port_vlan_del_vlan(struct net_device *dev,
1187 const struct switchdev_obj_port_vlan *vlan)
1188{
1189 int ret;
1190 u16 vid;
1191
1192 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
1193 ret = ocelot_vlan_vid_del(dev, vid);
1194
1195 if (ret)
1196 return ret;
1197 }
1198
1199 return 0;
1200}
1201
Alexandre Bellonia556c762018-05-14 22:04:57 +02001202static struct ocelot_multicast *ocelot_multicast_get(struct ocelot *ocelot,
1203 const unsigned char *addr,
1204 u16 vid)
1205{
1206 struct ocelot_multicast *mc;
1207
1208 list_for_each_entry(mc, &ocelot->multicast, list) {
1209 if (ether_addr_equal(mc->addr, addr) && mc->vid == vid)
1210 return mc;
1211 }
1212
1213 return NULL;
1214}
1215
1216static int ocelot_port_obj_add_mdb(struct net_device *dev,
1217 const struct switchdev_obj_port_mdb *mdb,
1218 struct switchdev_trans *trans)
1219{
1220 struct ocelot_port *port = netdev_priv(dev);
1221 struct ocelot *ocelot = port->ocelot;
1222 struct ocelot_multicast *mc;
1223 unsigned char addr[ETH_ALEN];
1224 u16 vid = mdb->vid;
1225 bool new = false;
1226
1227 if (!vid)
Antoine Tenart71425292018-06-26 14:28:49 +02001228 vid = port->pvid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001229
1230 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1231 if (!mc) {
1232 mc = devm_kzalloc(ocelot->dev, sizeof(*mc), GFP_KERNEL);
1233 if (!mc)
1234 return -ENOMEM;
1235
1236 memcpy(mc->addr, mdb->addr, ETH_ALEN);
1237 mc->vid = vid;
1238
1239 list_add_tail(&mc->list, &ocelot->multicast);
1240 new = true;
1241 }
1242
1243 memcpy(addr, mc->addr, ETH_ALEN);
1244 addr[0] = 0;
1245
1246 if (!new) {
1247 addr[2] = mc->ports << 0;
1248 addr[1] = mc->ports << 8;
1249 ocelot_mact_forget(ocelot, addr, vid);
1250 }
1251
1252 mc->ports |= BIT(port->chip_port);
1253 addr[2] = mc->ports << 0;
1254 addr[1] = mc->ports << 8;
1255
1256 return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1257}
1258
1259static int ocelot_port_obj_del_mdb(struct net_device *dev,
1260 const struct switchdev_obj_port_mdb *mdb)
1261{
1262 struct ocelot_port *port = netdev_priv(dev);
1263 struct ocelot *ocelot = port->ocelot;
1264 struct ocelot_multicast *mc;
1265 unsigned char addr[ETH_ALEN];
1266 u16 vid = mdb->vid;
1267
1268 if (!vid)
Antoine Tenart71425292018-06-26 14:28:49 +02001269 vid = port->pvid;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001270
1271 mc = ocelot_multicast_get(ocelot, mdb->addr, vid);
1272 if (!mc)
1273 return -ENOENT;
1274
1275 memcpy(addr, mc->addr, ETH_ALEN);
1276 addr[2] = mc->ports << 0;
1277 addr[1] = mc->ports << 8;
1278 addr[0] = 0;
1279 ocelot_mact_forget(ocelot, addr, vid);
1280
1281 mc->ports &= ~BIT(port->chip_port);
1282 if (!mc->ports) {
1283 list_del(&mc->list);
1284 devm_kfree(ocelot->dev, mc);
1285 return 0;
1286 }
1287
1288 addr[2] = mc->ports << 0;
1289 addr[1] = mc->ports << 8;
1290
1291 return ocelot_mact_learn(ocelot, 0, addr, vid, ENTRYTYPE_MACv4);
1292}
1293
1294static int ocelot_port_obj_add(struct net_device *dev,
1295 const struct switchdev_obj *obj,
Petr Machata69213512018-12-12 17:02:56 +00001296 struct switchdev_trans *trans,
1297 struct netlink_ext_ack *extack)
Alexandre Bellonia556c762018-05-14 22:04:57 +02001298{
1299 int ret = 0;
1300
1301 switch (obj->id) {
Antoine Tenart71425292018-06-26 14:28:49 +02001302 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1303 ret = ocelot_port_obj_add_vlan(dev,
1304 SWITCHDEV_OBJ_PORT_VLAN(obj),
1305 trans);
1306 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001307 case SWITCHDEV_OBJ_ID_PORT_MDB:
1308 ret = ocelot_port_obj_add_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj),
1309 trans);
1310 break;
1311 default:
1312 return -EOPNOTSUPP;
1313 }
1314
1315 return ret;
1316}
1317
1318static int ocelot_port_obj_del(struct net_device *dev,
1319 const struct switchdev_obj *obj)
1320{
1321 int ret = 0;
1322
1323 switch (obj->id) {
Antoine Tenart71425292018-06-26 14:28:49 +02001324 case SWITCHDEV_OBJ_ID_PORT_VLAN:
1325 ret = ocelot_port_vlan_del_vlan(dev,
1326 SWITCHDEV_OBJ_PORT_VLAN(obj));
1327 break;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001328 case SWITCHDEV_OBJ_ID_PORT_MDB:
1329 ret = ocelot_port_obj_del_mdb(dev, SWITCHDEV_OBJ_PORT_MDB(obj));
1330 break;
1331 default:
1332 return -EOPNOTSUPP;
1333 }
1334
1335 return ret;
1336}
1337
1338static const struct switchdev_ops ocelot_port_switchdev_ops = {
1339 .switchdev_port_attr_get = ocelot_port_attr_get,
1340 .switchdev_port_attr_set = ocelot_port_attr_set,
Alexandre Bellonia556c762018-05-14 22:04:57 +02001341};
1342
1343static int ocelot_port_bridge_join(struct ocelot_port *ocelot_port,
1344 struct net_device *bridge)
1345{
1346 struct ocelot *ocelot = ocelot_port->ocelot;
1347
1348 if (!ocelot->bridge_mask) {
1349 ocelot->hw_bridge_dev = bridge;
1350 } else {
1351 if (ocelot->hw_bridge_dev != bridge)
1352 /* This is adding the port to a second bridge, this is
1353 * unsupported */
1354 return -ENODEV;
1355 }
1356
1357 ocelot->bridge_mask |= BIT(ocelot_port->chip_port);
1358
1359 return 0;
1360}
1361
1362static void ocelot_port_bridge_leave(struct ocelot_port *ocelot_port,
1363 struct net_device *bridge)
1364{
1365 struct ocelot *ocelot = ocelot_port->ocelot;
1366
1367 ocelot->bridge_mask &= ~BIT(ocelot_port->chip_port);
1368
1369 if (!ocelot->bridge_mask)
1370 ocelot->hw_bridge_dev = NULL;
Antoine Tenart71425292018-06-26 14:28:49 +02001371
1372 /* Clear bridge vlan settings before calling ocelot_vlan_port_apply */
1373 ocelot_port->vlan_aware = 0;
1374 ocelot_port->pvid = 0;
1375 ocelot_port->vid = 0;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001376}
1377
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001378static void ocelot_set_aggr_pgids(struct ocelot *ocelot)
1379{
1380 int i, port, lag;
1381
1382 /* Reset destination and aggregation PGIDS */
1383 for (port = 0; port < ocelot->num_phys_ports; port++)
1384 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1385
1386 for (i = PGID_AGGR; i < PGID_SRC; i++)
1387 ocelot_write_rix(ocelot, GENMASK(ocelot->num_phys_ports - 1, 0),
1388 ANA_PGID_PGID, i);
1389
1390 /* Now, set PGIDs for each LAG */
1391 for (lag = 0; lag < ocelot->num_phys_ports; lag++) {
1392 unsigned long bond_mask;
1393 int aggr_count = 0;
1394 u8 aggr_idx[16];
1395
1396 bond_mask = ocelot->lags[lag];
1397 if (!bond_mask)
1398 continue;
1399
1400 for_each_set_bit(port, &bond_mask, ocelot->num_phys_ports) {
1401 // Destination mask
1402 ocelot_write_rix(ocelot, bond_mask,
1403 ANA_PGID_PGID, port);
1404 aggr_idx[aggr_count] = port;
1405 aggr_count++;
1406 }
1407
1408 for (i = PGID_AGGR; i < PGID_SRC; i++) {
1409 u32 ac;
1410
1411 ac = ocelot_read_rix(ocelot, ANA_PGID_PGID, i);
1412 ac &= ~bond_mask;
1413 ac |= BIT(aggr_idx[i % aggr_count]);
1414 ocelot_write_rix(ocelot, ac, ANA_PGID_PGID, i);
1415 }
1416 }
1417}
1418
1419static void ocelot_setup_lag(struct ocelot *ocelot, int lag)
1420{
1421 unsigned long bond_mask = ocelot->lags[lag];
1422 unsigned int p;
1423
1424 for_each_set_bit(p, &bond_mask, ocelot->num_phys_ports) {
1425 u32 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1426
1427 port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1428
1429 /* Use lag port as logical port for port i */
1430 ocelot_write_gix(ocelot, port_cfg |
1431 ANA_PORT_PORT_CFG_PORTID_VAL(lag),
1432 ANA_PORT_PORT_CFG, p);
1433 }
1434}
1435
1436static int ocelot_port_lag_join(struct ocelot_port *ocelot_port,
1437 struct net_device *bond)
1438{
1439 struct ocelot *ocelot = ocelot_port->ocelot;
1440 int p = ocelot_port->chip_port;
1441 int lag, lp;
1442 struct net_device *ndev;
1443 u32 bond_mask = 0;
1444
1445 rcu_read_lock();
1446 for_each_netdev_in_bond_rcu(bond, ndev) {
1447 struct ocelot_port *port = netdev_priv(ndev);
1448
1449 bond_mask |= BIT(port->chip_port);
1450 }
1451 rcu_read_unlock();
1452
1453 lp = __ffs(bond_mask);
1454
1455 /* If the new port is the lowest one, use it as the logical port from
1456 * now on
1457 */
1458 if (p == lp) {
1459 lag = p;
1460 ocelot->lags[p] = bond_mask;
1461 bond_mask &= ~BIT(p);
1462 if (bond_mask) {
1463 lp = __ffs(bond_mask);
1464 ocelot->lags[lp] = 0;
1465 }
1466 } else {
1467 lag = lp;
1468 ocelot->lags[lp] |= BIT(p);
1469 }
1470
1471 ocelot_setup_lag(ocelot, lag);
1472 ocelot_set_aggr_pgids(ocelot);
1473
1474 return 0;
1475}
1476
1477static void ocelot_port_lag_leave(struct ocelot_port *ocelot_port,
1478 struct net_device *bond)
1479{
1480 struct ocelot *ocelot = ocelot_port->ocelot;
1481 int p = ocelot_port->chip_port;
1482 u32 port_cfg;
1483 int i;
1484
1485 /* Remove port from any lag */
1486 for (i = 0; i < ocelot->num_phys_ports; i++)
1487 ocelot->lags[i] &= ~BIT(ocelot_port->chip_port);
1488
1489 /* if it was the logical port of the lag, move the lag config to the
1490 * next port
1491 */
1492 if (ocelot->lags[p]) {
1493 int n = __ffs(ocelot->lags[p]);
1494
1495 ocelot->lags[n] = ocelot->lags[p];
1496 ocelot->lags[p] = 0;
1497
1498 ocelot_setup_lag(ocelot, n);
1499 }
1500
1501 port_cfg = ocelot_read_gix(ocelot, ANA_PORT_PORT_CFG, p);
1502 port_cfg &= ~ANA_PORT_PORT_CFG_PORTID_VAL_M;
1503 ocelot_write_gix(ocelot, port_cfg | ANA_PORT_PORT_CFG_PORTID_VAL(p),
1504 ANA_PORT_PORT_CFG, p);
1505
1506 ocelot_set_aggr_pgids(ocelot);
1507}
1508
Alexandre Bellonia556c762018-05-14 22:04:57 +02001509/* Checks if the net_device instance given to us originate from our driver. */
1510static bool ocelot_netdevice_dev_check(const struct net_device *dev)
1511{
1512 return dev->netdev_ops == &ocelot_port_netdev_ops;
1513}
1514
1515static int ocelot_netdevice_port_event(struct net_device *dev,
1516 unsigned long event,
1517 struct netdev_notifier_changeupper_info *info)
1518{
1519 struct ocelot_port *ocelot_port = netdev_priv(dev);
1520 int err = 0;
1521
1522 if (!ocelot_netdevice_dev_check(dev))
1523 return 0;
1524
1525 switch (event) {
1526 case NETDEV_CHANGEUPPER:
1527 if (netif_is_bridge_master(info->upper_dev)) {
1528 if (info->linking)
1529 err = ocelot_port_bridge_join(ocelot_port,
1530 info->upper_dev);
1531 else
1532 ocelot_port_bridge_leave(ocelot_port,
1533 info->upper_dev);
Antoine Tenart71425292018-06-26 14:28:49 +02001534
1535 ocelot_vlan_port_apply(ocelot_port->ocelot,
1536 ocelot_port);
Alexandre Bellonia556c762018-05-14 22:04:57 +02001537 }
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001538 if (netif_is_lag_master(info->upper_dev)) {
1539 if (info->linking)
1540 err = ocelot_port_lag_join(ocelot_port,
1541 info->upper_dev);
1542 else
1543 ocelot_port_lag_leave(ocelot_port,
1544 info->upper_dev);
1545 }
Alexandre Bellonia556c762018-05-14 22:04:57 +02001546 break;
1547 default:
1548 break;
1549 }
1550
1551 return err;
1552}
1553
1554static int ocelot_netdevice_event(struct notifier_block *unused,
1555 unsigned long event, void *ptr)
1556{
1557 struct netdev_notifier_changeupper_info *info = ptr;
1558 struct net_device *dev = netdev_notifier_info_to_dev(ptr);
Geert Uytterhoeven2ac0e152018-06-07 15:10:30 +02001559 int ret = 0;
Alexandre Bellonia556c762018-05-14 22:04:57 +02001560
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001561 if (event == NETDEV_PRECHANGEUPPER &&
1562 netif_is_lag_master(info->upper_dev)) {
1563 struct netdev_lag_upper_info *lag_upper_info = info->upper_info;
1564 struct netlink_ext_ack *extack;
1565
1566 if (lag_upper_info->tx_type != NETDEV_LAG_TX_TYPE_HASH) {
1567 extack = netdev_notifier_info_to_extack(&info->info);
1568 NL_SET_ERR_MSG_MOD(extack, "LAG device using unsupported Tx type");
1569
1570 ret = -EINVAL;
1571 goto notify;
1572 }
1573 }
1574
Alexandre Bellonia556c762018-05-14 22:04:57 +02001575 if (netif_is_lag_master(dev)) {
1576 struct net_device *slave;
1577 struct list_head *iter;
1578
1579 netdev_for_each_lower_dev(dev, slave, iter) {
1580 ret = ocelot_netdevice_port_event(slave, event, info);
1581 if (ret)
1582 goto notify;
1583 }
1584 } else {
1585 ret = ocelot_netdevice_port_event(dev, event, info);
1586 }
1587
1588notify:
1589 return notifier_from_errno(ret);
1590}
1591
1592struct notifier_block ocelot_netdevice_nb __read_mostly = {
1593 .notifier_call = ocelot_netdevice_event,
1594};
1595EXPORT_SYMBOL(ocelot_netdevice_nb);
1596
Petr Machata0e332c82018-11-22 23:30:11 +00001597static int ocelot_switchdev_blocking_event(struct notifier_block *unused,
1598 unsigned long event, void *ptr)
1599{
1600 struct net_device *dev = switchdev_notifier_info_to_dev(ptr);
1601 int err;
1602
1603 switch (event) {
1604 /* Blocking events. */
1605 case SWITCHDEV_PORT_OBJ_ADD:
1606 err = switchdev_handle_port_obj_add(dev, ptr,
1607 ocelot_netdevice_dev_check,
1608 ocelot_port_obj_add);
1609 return notifier_from_errno(err);
1610 case SWITCHDEV_PORT_OBJ_DEL:
1611 err = switchdev_handle_port_obj_del(dev, ptr,
1612 ocelot_netdevice_dev_check,
1613 ocelot_port_obj_del);
1614 return notifier_from_errno(err);
1615 }
1616
1617 return NOTIFY_DONE;
1618}
1619
1620struct notifier_block ocelot_switchdev_blocking_nb __read_mostly = {
1621 .notifier_call = ocelot_switchdev_blocking_event,
1622};
1623EXPORT_SYMBOL(ocelot_switchdev_blocking_nb);
1624
Alexandre Bellonia556c762018-05-14 22:04:57 +02001625int ocelot_probe_port(struct ocelot *ocelot, u8 port,
1626 void __iomem *regs,
1627 struct phy_device *phy)
1628{
1629 struct ocelot_port *ocelot_port;
1630 struct net_device *dev;
1631 int err;
1632
1633 dev = alloc_etherdev(sizeof(struct ocelot_port));
1634 if (!dev)
1635 return -ENOMEM;
1636 SET_NETDEV_DEV(dev, ocelot->dev);
1637 ocelot_port = netdev_priv(dev);
1638 ocelot_port->dev = dev;
1639 ocelot_port->ocelot = ocelot;
1640 ocelot_port->regs = regs;
1641 ocelot_port->chip_port = port;
1642 ocelot_port->phy = phy;
1643 INIT_LIST_HEAD(&ocelot_port->mc);
1644 ocelot->ports[port] = ocelot_port;
1645
1646 dev->netdev_ops = &ocelot_port_netdev_ops;
1647 dev->ethtool_ops = &ocelot_ethtool_ops;
1648 dev->switchdev_ops = &ocelot_port_switchdev_ops;
1649
Antoine Tenart60f8e672018-10-11 09:12:24 +02001650 dev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER | NETIF_F_RXFCS;
Antoine Tenart71425292018-06-26 14:28:49 +02001651 dev->features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1652
Alexandre Bellonia556c762018-05-14 22:04:57 +02001653 memcpy(dev->dev_addr, ocelot->base_mac, ETH_ALEN);
1654 dev->dev_addr[ETH_ALEN - 1] += port;
1655 ocelot_mact_learn(ocelot, PGID_CPU, dev->dev_addr, ocelot_port->pvid,
1656 ENTRYTYPE_LOCKED);
1657
1658 err = register_netdev(dev);
1659 if (err) {
1660 dev_err(ocelot->dev, "register_netdev failed\n");
1661 goto err_register_netdev;
1662 }
1663
Antoine Tenart71425292018-06-26 14:28:49 +02001664 /* Basic L2 initialization */
1665 ocelot_vlan_port_apply(ocelot, ocelot_port);
1666
Alexandre Bellonia556c762018-05-14 22:04:57 +02001667 return 0;
1668
1669err_register_netdev:
1670 free_netdev(dev);
1671 return err;
1672}
1673EXPORT_SYMBOL(ocelot_probe_port);
1674
1675int ocelot_init(struct ocelot *ocelot)
1676{
1677 u32 port;
1678 int i, cpu = ocelot->num_phys_ports;
1679 char queue_name[32];
1680
Alexandre Bellonidc96ee32018-06-26 14:28:48 +02001681 ocelot->lags = devm_kcalloc(ocelot->dev, ocelot->num_phys_ports,
1682 sizeof(u32), GFP_KERNEL);
1683 if (!ocelot->lags)
1684 return -ENOMEM;
1685
Alexandre Bellonia556c762018-05-14 22:04:57 +02001686 ocelot->stats = devm_kcalloc(ocelot->dev,
1687 ocelot->num_phys_ports * ocelot->num_stats,
1688 sizeof(u64), GFP_KERNEL);
1689 if (!ocelot->stats)
1690 return -ENOMEM;
1691
1692 mutex_init(&ocelot->stats_lock);
1693 snprintf(queue_name, sizeof(queue_name), "%s-stats",
1694 dev_name(ocelot->dev));
1695 ocelot->stats_queue = create_singlethread_workqueue(queue_name);
1696 if (!ocelot->stats_queue)
1697 return -ENOMEM;
1698
1699 ocelot_mact_init(ocelot);
1700 ocelot_vlan_init(ocelot);
1701
1702 for (port = 0; port < ocelot->num_phys_ports; port++) {
1703 /* Clear all counters (5 groups) */
1704 ocelot_write(ocelot, SYS_STAT_CFG_STAT_VIEW(port) |
1705 SYS_STAT_CFG_STAT_CLEAR_SHOT(0x7f),
1706 SYS_STAT_CFG);
1707 }
1708
1709 /* Only use S-Tag */
1710 ocelot_write(ocelot, ETH_P_8021AD, SYS_VLAN_ETYPE_CFG);
1711
1712 /* Aggregation mode */
1713 ocelot_write(ocelot, ANA_AGGR_CFG_AC_SMAC_ENA |
1714 ANA_AGGR_CFG_AC_DMAC_ENA |
1715 ANA_AGGR_CFG_AC_IP4_SIPDIP_ENA |
1716 ANA_AGGR_CFG_AC_IP4_TCPUDP_ENA, ANA_AGGR_CFG);
1717
1718 /* Set MAC age time to default value. The entry is aged after
1719 * 2*AGE_PERIOD
1720 */
1721 ocelot_write(ocelot,
1722 ANA_AUTOAGE_AGE_PERIOD(BR_DEFAULT_AGEING_TIME / 2 / HZ),
1723 ANA_AUTOAGE);
1724
1725 /* Disable learning for frames discarded by VLAN ingress filtering */
1726 regmap_field_write(ocelot->regfields[ANA_ADVLEARN_VLAN_CHK], 1);
1727
1728 /* Setup frame ageing - fixed value "2 sec" - in 6.5 us units */
1729 ocelot_write(ocelot, SYS_FRM_AGING_AGE_TX_ENA |
1730 SYS_FRM_AGING_MAX_AGE(307692), SYS_FRM_AGING);
1731
1732 /* Setup flooding PGIDs */
1733 ocelot_write_rix(ocelot, ANA_FLOODING_FLD_MULTICAST(PGID_MC) |
1734 ANA_FLOODING_FLD_BROADCAST(PGID_MC) |
1735 ANA_FLOODING_FLD_UNICAST(PGID_UC),
1736 ANA_FLOODING, 0);
1737 ocelot_write(ocelot, ANA_FLOODING_IPMC_FLD_MC6_DATA(PGID_MCIPV6) |
1738 ANA_FLOODING_IPMC_FLD_MC6_CTRL(PGID_MC) |
1739 ANA_FLOODING_IPMC_FLD_MC4_DATA(PGID_MCIPV4) |
1740 ANA_FLOODING_IPMC_FLD_MC4_CTRL(PGID_MC),
1741 ANA_FLOODING_IPMC);
1742
1743 for (port = 0; port < ocelot->num_phys_ports; port++) {
1744 /* Transmit the frame to the local port. */
1745 ocelot_write_rix(ocelot, BIT(port), ANA_PGID_PGID, port);
1746 /* Do not forward BPDU frames to the front ports. */
1747 ocelot_write_gix(ocelot,
1748 ANA_PORT_CPU_FWD_BPDU_CFG_BPDU_REDIR_ENA(0xffff),
1749 ANA_PORT_CPU_FWD_BPDU_CFG,
1750 port);
1751 /* Ensure bridging is disabled */
1752 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_SRC + port);
1753 }
1754
1755 /* Configure and enable the CPU port. */
1756 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, cpu);
1757 ocelot_write_rix(ocelot, BIT(cpu), ANA_PGID_PGID, PGID_CPU);
1758 ocelot_write_gix(ocelot, ANA_PORT_PORT_CFG_RECV_ENA |
1759 ANA_PORT_PORT_CFG_PORTID_VAL(cpu),
1760 ANA_PORT_PORT_CFG, cpu);
1761
1762 /* Allow broadcast MAC frames. */
1763 for (i = ocelot->num_phys_ports + 1; i < PGID_CPU; i++) {
1764 u32 val = ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports - 1, 0));
1765
1766 ocelot_write_rix(ocelot, val, ANA_PGID_PGID, i);
1767 }
1768 ocelot_write_rix(ocelot,
1769 ANA_PGID_PGID_PGID(GENMASK(ocelot->num_phys_ports, 0)),
1770 ANA_PGID_PGID, PGID_MC);
1771 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV4);
1772 ocelot_write_rix(ocelot, 0, ANA_PGID_PGID, PGID_MCIPV6);
1773
1774 /* CPU port Injection/Extraction configuration */
1775 ocelot_write_rix(ocelot, QSYS_SWITCH_PORT_MODE_INGRESS_DROP_MODE |
1776 QSYS_SWITCH_PORT_MODE_SCH_NEXT_CFG(1) |
1777 QSYS_SWITCH_PORT_MODE_PORT_ENA,
1778 QSYS_SWITCH_PORT_MODE, cpu);
1779 ocelot_write_rix(ocelot, SYS_PORT_MODE_INCL_XTR_HDR(1) |
1780 SYS_PORT_MODE_INCL_INJ_HDR(1), SYS_PORT_MODE, cpu);
1781 /* Allow manual injection via DEVCPU_QS registers, and byte swap these
1782 * registers endianness.
1783 */
1784 ocelot_write_rix(ocelot, QS_INJ_GRP_CFG_BYTE_SWAP |
1785 QS_INJ_GRP_CFG_MODE(1), QS_INJ_GRP_CFG, 0);
1786 ocelot_write_rix(ocelot, QS_XTR_GRP_CFG_BYTE_SWAP |
1787 QS_XTR_GRP_CFG_MODE(1), QS_XTR_GRP_CFG, 0);
1788 ocelot_write(ocelot, ANA_CPUQ_CFG_CPUQ_MIRROR(2) |
1789 ANA_CPUQ_CFG_CPUQ_LRN(2) |
1790 ANA_CPUQ_CFG_CPUQ_MAC_COPY(2) |
1791 ANA_CPUQ_CFG_CPUQ_SRC_COPY(2) |
1792 ANA_CPUQ_CFG_CPUQ_LOCKED_PORTMOVE(2) |
1793 ANA_CPUQ_CFG_CPUQ_ALLBRIDGE(6) |
1794 ANA_CPUQ_CFG_CPUQ_IPMC_CTRL(6) |
1795 ANA_CPUQ_CFG_CPUQ_IGMP(6) |
1796 ANA_CPUQ_CFG_CPUQ_MLD(6), ANA_CPUQ_CFG);
1797 for (i = 0; i < 16; i++)
1798 ocelot_write_rix(ocelot, ANA_CPUQ_8021_CFG_CPUQ_GARP_VAL(6) |
1799 ANA_CPUQ_8021_CFG_CPUQ_BPDU_VAL(6),
1800 ANA_CPUQ_8021_CFG, i);
1801
1802 INIT_DELAYED_WORK(&ocelot->stats_work, ocelot_check_stats);
1803 queue_delayed_work(ocelot->stats_queue, &ocelot->stats_work,
1804 OCELOT_STATS_CHECK_DELAY);
1805 return 0;
1806}
1807EXPORT_SYMBOL(ocelot_init);
1808
1809void ocelot_deinit(struct ocelot *ocelot)
1810{
1811 destroy_workqueue(ocelot->stats_queue);
1812 mutex_destroy(&ocelot->stats_lock);
1813}
1814EXPORT_SYMBOL(ocelot_deinit);
1815
1816MODULE_LICENSE("Dual MIT/GPL");