1 // SPDX-License-Identifier: (GPL-2.0 OR MIT)
3 * Microsemi Ocelot Switch driver
5 * Copyright (c) 2017 Microsemi Corporation
7 #include <linux/interrupt.h>
8 #include <linux/module.h>
9 #include <linux/of_net.h>
10 #include <linux/netdevice.h>
11 #include <linux/of_mdio.h>
12 #include <linux/of_platform.h>
13 #include <linux/mfd/syscon.h>
14 #include <linux/skbuff.h>
15 #include <net/switchdev.h>
17 #include <soc/mscc/ocelot_vcap.h>
20 #define IFH_EXTRACT_BITFIELD64(x, o, w) (((x) >> (o)) & GENMASK_ULL((w) - 1, 0))
21 #define VSC7514_VCAP_IS2_CNT 64
22 #define VSC7514_VCAP_IS2_ENTRY_WIDTH 376
23 #define VSC7514_VCAP_IS2_ACTION_WIDTH 99
24 #define VSC7514_VCAP_PORT_CNT 11
26 static int ocelot_parse_ifh(u32 *_ifh, struct frame_info *info)
31 ifh[0] = be64_to_cpu(((__force __be64 *)_ifh)[0]);
32 ifh[1] = be64_to_cpu(((__force __be64 *)_ifh)[1]);
34 wlen = IFH_EXTRACT_BITFIELD64(ifh[0], 7, 8);
35 llen = IFH_EXTRACT_BITFIELD64(ifh[0], 15, 6);
37 info->len = OCELOT_BUFFER_CELL_SZ * wlen + llen - 80;
39 info->timestamp = IFH_EXTRACT_BITFIELD64(ifh[0], 21, 32);
41 info->port = IFH_EXTRACT_BITFIELD64(ifh[1], 43, 4);
43 info->tag_type = IFH_EXTRACT_BITFIELD64(ifh[1], 16, 1);
44 info->vid = IFH_EXTRACT_BITFIELD64(ifh[1], 0, 12);
49 static int ocelot_rx_frame_word(struct ocelot *ocelot, u8 grp, bool ifh,
55 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
56 if (val == XTR_NOT_READY) {
61 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
62 } while (val == XTR_NOT_READY);
73 bytes_valid = XTR_VALID_BYTES(val);
74 val = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
75 if (val == XTR_ESCAPE)
76 *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
82 *rval = ocelot_read_rix(ocelot, QS_XTR_RD, grp);
92 static irqreturn_t ocelot_xtr_irq_handler(int irq, void *arg)
94 struct ocelot *ocelot = arg;
98 if (!(ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp)))
102 struct skb_shared_hwtstamps *shhwtstamps;
103 struct ocelot_port_private *priv;
104 struct ocelot_port *ocelot_port;
105 u64 tod_in_ns, full_ts_in_ns;
106 struct frame_info info = {};
107 struct net_device *dev;
108 u32 ifh[4], val, *buf;
109 struct timespec64 ts;
110 int sz, len, buf_len;
113 for (i = 0; i < OCELOT_TAG_LEN / 4; i++) {
114 err = ocelot_rx_frame_word(ocelot, grp, true, &ifh[i]);
122 /* At this point the IFH was read correctly, so it is safe to
123 * presume that there is no error. The err needs to be reset
124 * otherwise a frame could come in CPU queue between the while
125 * condition and the check for error later on. And in that case
126 * the new frame is just removed and not processed.
130 ocelot_parse_ifh(ifh, &info);
132 ocelot_port = ocelot->ports[info.port];
133 priv = container_of(ocelot_port, struct ocelot_port_private,
137 skb = netdev_alloc_skb(dev, info.len);
139 if (unlikely(!skb)) {
140 netdev_err(dev, "Unable to allocate sk_buff\n");
144 buf_len = info.len - ETH_FCS_LEN;
145 buf = (u32 *)skb_put(skb, buf_len);
149 sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
152 } while (len < buf_len);
155 sz = ocelot_rx_frame_word(ocelot, grp, false, &val);
156 /* Update the statistics if part of the FCS was read before */
157 len -= ETH_FCS_LEN - sz;
159 if (unlikely(dev->features & NETIF_F_RXFCS)) {
160 buf = (u32 *)skb_put(skb, ETH_FCS_LEN);
170 ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
172 tod_in_ns = ktime_set(ts.tv_sec, ts.tv_nsec);
173 if ((tod_in_ns & 0xffffffff) < info.timestamp)
174 full_ts_in_ns = (((tod_in_ns >> 32) - 1) << 32) |
177 full_ts_in_ns = (tod_in_ns & GENMASK_ULL(63, 32)) |
180 shhwtstamps = skb_hwtstamps(skb);
181 memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
182 shhwtstamps->hwtstamp = full_ts_in_ns;
185 /* Everything we see on an interface that is in the HW bridge
186 * has already been forwarded.
188 if (ocelot->bridge_mask & BIT(info.port))
189 skb->offload_fwd_mark = 1;
191 skb->protocol = eth_type_trans(skb, dev);
192 if (!skb_defer_rx_timestamp(skb))
194 dev->stats.rx_bytes += len;
195 dev->stats.rx_packets++;
196 } while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp));
199 while (ocelot_read(ocelot, QS_XTR_DATA_PRESENT) & BIT(grp))
200 ocelot_read_rix(ocelot, QS_XTR_RD, grp);
205 static irqreturn_t ocelot_ptp_rdy_irq_handler(int irq, void *arg)
207 struct ocelot *ocelot = arg;
209 ocelot_get_txtstamp(ocelot);
214 static const struct of_device_id mscc_ocelot_match[] = {
215 { .compatible = "mscc,vsc7514-switch" },
218 MODULE_DEVICE_TABLE(of, mscc_ocelot_match);
220 static int ocelot_reset(struct ocelot *ocelot)
225 regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_INIT], 1);
226 regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_ENA], 1);
230 regmap_field_read(ocelot->regfields[SYS_RESET_CFG_MEM_INIT],
232 } while (val && --retries);
237 regmap_field_write(ocelot->regfields[SYS_RESET_CFG_MEM_ENA], 1);
238 regmap_field_write(ocelot->regfields[SYS_RESET_CFG_CORE_ENA], 1);
243 static const struct ocelot_ops ocelot_ops = {
244 .reset = ocelot_reset,
247 static const struct vcap_field vsc7514_vcap_is2_keys[] = {
248 /* Common: 46 bits */
249 [VCAP_IS2_TYPE] = { 0, 4},
250 [VCAP_IS2_HK_FIRST] = { 4, 1},
251 [VCAP_IS2_HK_PAG] = { 5, 8},
252 [VCAP_IS2_HK_IGR_PORT_MASK] = { 13, 12},
253 [VCAP_IS2_HK_RSV2] = { 25, 1},
254 [VCAP_IS2_HK_HOST_MATCH] = { 26, 1},
255 [VCAP_IS2_HK_L2_MC] = { 27, 1},
256 [VCAP_IS2_HK_L2_BC] = { 28, 1},
257 [VCAP_IS2_HK_VLAN_TAGGED] = { 29, 1},
258 [VCAP_IS2_HK_VID] = { 30, 12},
259 [VCAP_IS2_HK_DEI] = { 42, 1},
260 [VCAP_IS2_HK_PCP] = { 43, 3},
261 /* MAC_ETYPE / MAC_LLC / MAC_SNAP / OAM common */
262 [VCAP_IS2_HK_L2_DMAC] = { 46, 48},
263 [VCAP_IS2_HK_L2_SMAC] = { 94, 48},
264 /* MAC_ETYPE (TYPE=000) */
265 [VCAP_IS2_HK_MAC_ETYPE_ETYPE] = {142, 16},
266 [VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD0] = {158, 16},
267 [VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD1] = {174, 8},
268 [VCAP_IS2_HK_MAC_ETYPE_L2_PAYLOAD2] = {182, 3},
269 /* MAC_LLC (TYPE=001) */
270 [VCAP_IS2_HK_MAC_LLC_L2_LLC] = {142, 40},
271 /* MAC_SNAP (TYPE=010) */
272 [VCAP_IS2_HK_MAC_SNAP_L2_SNAP] = {142, 40},
273 /* MAC_ARP (TYPE=011) */
274 [VCAP_IS2_HK_MAC_ARP_SMAC] = { 46, 48},
275 [VCAP_IS2_HK_MAC_ARP_ADDR_SPACE_OK] = { 94, 1},
276 [VCAP_IS2_HK_MAC_ARP_PROTO_SPACE_OK] = { 95, 1},
277 [VCAP_IS2_HK_MAC_ARP_LEN_OK] = { 96, 1},
278 [VCAP_IS2_HK_MAC_ARP_TARGET_MATCH] = { 97, 1},
279 [VCAP_IS2_HK_MAC_ARP_SENDER_MATCH] = { 98, 1},
280 [VCAP_IS2_HK_MAC_ARP_OPCODE_UNKNOWN] = { 99, 1},
281 [VCAP_IS2_HK_MAC_ARP_OPCODE] = {100, 2},
282 [VCAP_IS2_HK_MAC_ARP_L3_IP4_DIP] = {102, 32},
283 [VCAP_IS2_HK_MAC_ARP_L3_IP4_SIP] = {134, 32},
284 [VCAP_IS2_HK_MAC_ARP_DIP_EQ_SIP] = {166, 1},
285 /* IP4_TCP_UDP / IP4_OTHER common */
286 [VCAP_IS2_HK_IP4] = { 46, 1},
287 [VCAP_IS2_HK_L3_FRAGMENT] = { 47, 1},
288 [VCAP_IS2_HK_L3_FRAG_OFS_GT0] = { 48, 1},
289 [VCAP_IS2_HK_L3_OPTIONS] = { 49, 1},
290 [VCAP_IS2_HK_IP4_L3_TTL_GT0] = { 50, 1},
291 [VCAP_IS2_HK_L3_TOS] = { 51, 8},
292 [VCAP_IS2_HK_L3_IP4_DIP] = { 59, 32},
293 [VCAP_IS2_HK_L3_IP4_SIP] = { 91, 32},
294 [VCAP_IS2_HK_DIP_EQ_SIP] = {123, 1},
295 /* IP4_TCP_UDP (TYPE=100) */
296 [VCAP_IS2_HK_TCP] = {124, 1},
297 [VCAP_IS2_HK_L4_SPORT] = {125, 16},
298 [VCAP_IS2_HK_L4_DPORT] = {141, 16},
299 [VCAP_IS2_HK_L4_RNG] = {157, 8},
300 [VCAP_IS2_HK_L4_SPORT_EQ_DPORT] = {165, 1},
301 [VCAP_IS2_HK_L4_SEQUENCE_EQ0] = {166, 1},
302 [VCAP_IS2_HK_L4_URG] = {167, 1},
303 [VCAP_IS2_HK_L4_ACK] = {168, 1},
304 [VCAP_IS2_HK_L4_PSH] = {169, 1},
305 [VCAP_IS2_HK_L4_RST] = {170, 1},
306 [VCAP_IS2_HK_L4_SYN] = {171, 1},
307 [VCAP_IS2_HK_L4_FIN] = {172, 1},
308 [VCAP_IS2_HK_L4_1588_DOM] = {173, 8},
309 [VCAP_IS2_HK_L4_1588_VER] = {181, 4},
310 /* IP4_OTHER (TYPE=101) */
311 [VCAP_IS2_HK_IP4_L3_PROTO] = {124, 8},
312 [VCAP_IS2_HK_L3_PAYLOAD] = {132, 56},
313 /* IP6_STD (TYPE=110) */
314 [VCAP_IS2_HK_IP6_L3_TTL_GT0] = { 46, 1},
315 [VCAP_IS2_HK_L3_IP6_SIP] = { 47, 128},
316 [VCAP_IS2_HK_IP6_L3_PROTO] = {175, 8},
318 [VCAP_IS2_HK_OAM_MEL_FLAGS] = {142, 7},
319 [VCAP_IS2_HK_OAM_VER] = {149, 5},
320 [VCAP_IS2_HK_OAM_OPCODE] = {154, 8},
321 [VCAP_IS2_HK_OAM_FLAGS] = {162, 8},
322 [VCAP_IS2_HK_OAM_MEPID] = {170, 16},
323 [VCAP_IS2_HK_OAM_CCM_CNTS_EQ0] = {186, 1},
324 [VCAP_IS2_HK_OAM_IS_Y1731] = {187, 1},
327 static const struct vcap_field vsc7514_vcap_is2_actions[] = {
328 [VCAP_IS2_ACT_HIT_ME_ONCE] = { 0, 1},
329 [VCAP_IS2_ACT_CPU_COPY_ENA] = { 1, 1},
330 [VCAP_IS2_ACT_CPU_QU_NUM] = { 2, 3},
331 [VCAP_IS2_ACT_MASK_MODE] = { 5, 2},
332 [VCAP_IS2_ACT_MIRROR_ENA] = { 7, 1},
333 [VCAP_IS2_ACT_LRN_DIS] = { 8, 1},
334 [VCAP_IS2_ACT_POLICE_ENA] = { 9, 1},
335 [VCAP_IS2_ACT_POLICE_IDX] = { 10, 9},
336 [VCAP_IS2_ACT_POLICE_VCAP_ONLY] = { 19, 1},
337 [VCAP_IS2_ACT_PORT_MASK] = { 20, 11},
338 [VCAP_IS2_ACT_REW_OP] = { 31, 9},
339 [VCAP_IS2_ACT_SMAC_REPLACE_ENA] = { 40, 1},
340 [VCAP_IS2_ACT_RSV] = { 41, 2},
341 [VCAP_IS2_ACT_ACL_ID] = { 43, 6},
342 [VCAP_IS2_ACT_HIT_CNT] = { 49, 32},
345 static const struct vcap_props vsc7514_vcap_props[] = {
349 .entry_count = VSC7514_VCAP_IS2_CNT,
350 .entry_width = VSC7514_VCAP_IS2_ENTRY_WIDTH,
351 .action_count = VSC7514_VCAP_IS2_CNT +
352 VSC7514_VCAP_PORT_CNT + 2,
354 .action_type_width = 1,
356 [IS2_ACTION_TYPE_NORMAL] = {
360 [IS2_ACTION_TYPE_SMAC_SIP] = {
370 static struct ptp_clock_info ocelot_ptp_clock_info = {
371 .owner = THIS_MODULE,
372 .name = "ocelot ptp",
373 .max_adj = 0x7fffffff,
376 .n_per_out = OCELOT_PTP_PINS_NUM,
377 .n_pins = OCELOT_PTP_PINS_NUM,
379 .gettime64 = ocelot_ptp_gettime64,
380 .settime64 = ocelot_ptp_settime64,
381 .adjtime = ocelot_ptp_adjtime,
382 .adjfine = ocelot_ptp_adjfine,
383 .verify = ocelot_ptp_verify,
384 .enable = ocelot_ptp_enable,
387 static int mscc_ocelot_probe(struct platform_device *pdev)
389 struct device_node *np = pdev->dev.of_node;
390 struct device_node *ports, *portnp;
391 int err, irq_xtr, irq_ptp_rdy;
392 struct ocelot *ocelot;
397 enum ocelot_target id;
410 if (!np && !pdev->dev.platform_data)
413 ocelot = devm_kzalloc(&pdev->dev, sizeof(*ocelot), GFP_KERNEL);
417 platform_set_drvdata(pdev, ocelot);
418 ocelot->dev = &pdev->dev;
420 for (i = 0; i < ARRAY_SIZE(io_target); i++) {
421 struct regmap *target;
422 struct resource *res;
424 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
427 target = ocelot_regmap_init(ocelot, res);
428 if (IS_ERR(target)) {
429 if (io_target[i].optional) {
430 ocelot->targets[io_target[i].id] = NULL;
433 return PTR_ERR(target);
436 ocelot->targets[io_target[i].id] = target;
439 hsio = syscon_regmap_lookup_by_compatible("mscc,ocelot-hsio");
441 dev_err(&pdev->dev, "missing hsio syscon\n");
442 return PTR_ERR(hsio);
445 ocelot->targets[HSIO] = hsio;
447 err = ocelot_chip_init(ocelot, &ocelot_ops);
451 irq_xtr = platform_get_irq_byname(pdev, "xtr");
455 err = devm_request_threaded_irq(&pdev->dev, irq_xtr, NULL,
456 ocelot_xtr_irq_handler, IRQF_ONESHOT,
457 "frame extraction", ocelot);
461 irq_ptp_rdy = platform_get_irq_byname(pdev, "ptp_rdy");
462 if (irq_ptp_rdy > 0 && ocelot->targets[PTP]) {
463 err = devm_request_threaded_irq(&pdev->dev, irq_ptp_rdy, NULL,
464 ocelot_ptp_rdy_irq_handler,
465 IRQF_ONESHOT, "ptp ready",
470 /* Both the PTP interrupt and the PTP bank are available */
474 ports = of_get_child_by_name(np, "ethernet-ports");
476 dev_err(&pdev->dev, "no ethernet-ports child node found\n");
480 ocelot->num_phys_ports = of_get_child_count(ports);
482 ocelot->ports = devm_kcalloc(&pdev->dev, ocelot->num_phys_ports,
483 sizeof(struct ocelot_port *), GFP_KERNEL);
485 ocelot->vcap_is2_keys = vsc7514_vcap_is2_keys;
486 ocelot->vcap_is2_actions = vsc7514_vcap_is2_actions;
487 ocelot->vcap = vsc7514_vcap_props;
491 err = ocelot_init_timestamp(ocelot, &ocelot_ptp_clock_info);
494 "Timestamp initialization failed\n");
500 ocelot_configure_cpu(ocelot, -1, OCELOT_TAG_PREFIX_NONE,
501 OCELOT_TAG_PREFIX_NONE);
503 for_each_available_child_of_node(ports, portnp) {
504 struct ocelot_port_private *priv;
505 struct ocelot_port *ocelot_port;
506 struct device_node *phy_node;
507 phy_interface_t phy_mode;
508 struct phy_device *phy;
509 struct resource *res;
515 if (of_property_read_u32(portnp, "reg", &port))
518 snprintf(res_name, sizeof(res_name), "port%d", port);
520 res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
522 regs = devm_ioremap_resource(&pdev->dev, res);
526 phy_node = of_parse_phandle(portnp, "phy-handle", 0);
530 phy = of_phy_find_device(phy_node);
531 of_node_put(phy_node);
535 err = ocelot_probe_port(ocelot, port, regs, phy);
541 ocelot_port = ocelot->ports[port];
542 priv = container_of(ocelot_port, struct ocelot_port_private,
545 of_get_phy_mode(portnp, &phy_mode);
547 ocelot_port->phy_mode = phy_mode;
549 switch (ocelot_port->phy_mode) {
550 case PHY_INTERFACE_MODE_NA:
552 case PHY_INTERFACE_MODE_SGMII:
554 case PHY_INTERFACE_MODE_QSGMII:
555 /* Ensure clock signals and speed is set on all
558 ocelot_port_writel(ocelot_port,
559 DEV_CLOCK_CFG_LINK_SPEED
565 "invalid phy mode for port%d, (Q)SGMII only\n",
572 serdes = devm_of_phy_get(ocelot->dev, portnp, NULL);
573 if (IS_ERR(serdes)) {
574 err = PTR_ERR(serdes);
575 if (err == -EPROBE_DEFER)
576 dev_dbg(ocelot->dev, "deferring probe\n");
579 "missing SerDes phys for port%d\n",
586 priv->serdes = serdes;
589 register_netdevice_notifier(&ocelot_netdevice_nb);
590 register_switchdev_notifier(&ocelot_switchdev_nb);
591 register_switchdev_blocking_notifier(&ocelot_switchdev_blocking_nb);
593 dev_info(&pdev->dev, "Ocelot switch probed\n");
600 static int mscc_ocelot_remove(struct platform_device *pdev)
602 struct ocelot *ocelot = platform_get_drvdata(pdev);
604 ocelot_deinit_timestamp(ocelot);
605 ocelot_deinit(ocelot);
606 unregister_switchdev_blocking_notifier(&ocelot_switchdev_blocking_nb);
607 unregister_switchdev_notifier(&ocelot_switchdev_nb);
608 unregister_netdevice_notifier(&ocelot_netdevice_nb);
613 static struct platform_driver mscc_ocelot_driver = {
614 .probe = mscc_ocelot_probe,
615 .remove = mscc_ocelot_remove,
617 .name = "ocelot-switch",
618 .of_match_table = mscc_ocelot_match,
622 module_platform_driver(mscc_ocelot_driver);
624 MODULE_DESCRIPTION("Microsemi Ocelot switch driver");
625 MODULE_AUTHOR("Alexandre Belloni <alexandre.belloni@bootlin.com>");
626 MODULE_LICENSE("Dual MIT/GPL");