Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Merge branch 'PTP-support-for-the-SJA1105-DSA-driver'

Vladimir Oltean says:

====================
PTP support for the SJA1105 DSA driver

This patchset adds the following:

- A timecounter/cyclecounter based PHC for the free-running
timestamping clock of this switch.

- A state machine implemented in the DSA tagger for SJA1105, which
keeps track of metadata follow-up Ethernet frames (the switch's way
of transmitting RX timestamps).

Clock manipulations on the actual hardware PTP clock will have to be
implemented anyway, for the TTEthernet block and the time-based ingress
policer.

v3 patchset can be found at:
https://lkml.org/lkml/2019/6/4/954

Changes from v3:

- Made it compile with the SJA1105 DSA driver and PTP driver as modules.

- Reworked/simplified/fixed some issues in 03/17
(dsa_8021q_remove_header) and added an ASCII image that
illustrates the transformation that is taking place.

- Removed a useless check for sja1105_is_link_local from 16/17 (RX
timestamping) which also made previous 08/17 patch ("Move
sja1105_is_link_local to include/linux") useless and therefore dropped.

v2 patchset can be found at:
https://lkml.org/lkml/2019/6/2/146

Changes from v2:

- Broke previous 09/10 patch (timestamping) into multiple smaller
patches.

- Every patch in the series compiles.

v1 patchset can be found at:
https://lkml.org/lkml/2019/5/28/1093

Changes from v1:

- Removed the addition of the DSA .can_timestamp callback.

- Waiting for meta frames is done completely inside the tagger, and all
frames emitted on RX are already partially timestamped.

- Added a global data structure for the tagger common to all ports.

- Made PTP work with ports in standalone mode, by limiting use of the
DMAC-mangling "incl_srcpt" mode only when ports are bridged, aka when
the DSA master is already promiscuous and can receive anything.
Also changed meta frames to be sent at the 01-80-C2-00-00-0E DMAC.

- Made some progress w.r.t. observed negative path delay. Apparently it
only appears when the delay mechanism is the delay request-response
(end-to-end) one. If peer delay is used (-P), the path delay is
positive and appears reasonable for an 1000Base-T link (485 ns in
steady state).

SJA1105 as PTP slave (OC) with E2E path delay:

ptp4l[55.600]: master offset 8 s2 freq +83677 path delay -2390
ptp4l[56.600]: master offset 17 s2 freq +83688 path delay -2391
ptp4l[57.601]: master offset 6 s2 freq +83682 path delay -2391
ptp4l[58.601]: master offset -1 s2 freq +83677 path delay -2391

SJA1105 as PTP slave (OC) with P2P path delay:

ptp4l[48.343]: master offset 5 s2 freq +83715 path delay 484
ptp4l[48.468]: master offset -3 s2 freq +83705 path delay 485
ptp4l[48.593]: master offset 0 s2 freq +83708 path delay 485
ptp4l[48.718]: master offset 1 s2 freq +83710 path delay 485
ptp4l[48.844]: master offset 1 s2 freq +83710 path delay 485
ptp4l[48.969]: master offset -5 s2 freq +83702 path delay 485
ptp4l[49.094]: master offset 3 s2 freq +83712 path delay 485
ptp4l[49.219]: master offset 4 s2 freq +83714 path delay 485
ptp4l[49.344]: master offset -5 s2 freq +83702 path delay 485
ptp4l[49.469]: master offset 3 s2 freq +83713 path delay 487
====================

Signed-off-by: David S. Miller <davem@davemloft.net>

+1184 -73
+7
drivers/net/dsa/sja1105/Kconfig
··· 16 16 - SJA1105Q (Gen. 2, No SGMII, TT-Ethernet) 17 17 - SJA1105R (Gen. 2, SGMII, No TT-Ethernet) 18 18 - SJA1105S (Gen. 2, SGMII, TT-Ethernet) 19 + 20 + config NET_DSA_SJA1105_PTP 21 + tristate "Support for the PTP clock on the NXP SJA1105 Ethernet switch" 22 + depends on NET_DSA_SJA1105 23 + help 24 + This enables support for timestamping and PTP clock manipulations in 25 + the SJA1105 DSA driver.
+1
drivers/net/dsa/sja1105/Makefile
··· 1 1 # SPDX-License-Identifier: GPL-2.0-only 2 2 obj-$(CONFIG_NET_DSA_SJA1105) += sja1105.o 3 + obj-$(CONFIG_NET_DSA_SJA1105_PTP) += sja1105_ptp.o 3 4 4 5 sja1105-objs := \ 5 6 sja1105_spi.o \
+29
drivers/net/dsa/sja1105/sja1105.h
··· 5 5 #ifndef _SJA1105_H 6 6 #define _SJA1105_H 7 7 8 + #include <linux/ptp_clock_kernel.h> 9 + #include <linux/timecounter.h> 8 10 #include <linux/dsa/sja1105.h> 9 11 #include <net/dsa.h> 10 12 #include <linux/mutex.h> ··· 29 27 u64 rgu; 30 28 u64 config; 31 29 u64 rmii_pll1; 30 + u64 ptp_control; 31 + u64 ptpclk; 32 + u64 ptpclkrate; 33 + u64 ptptsclk; 34 + u64 ptpegr_ts[SJA1105_NUM_PORTS]; 32 35 u64 pad_mii_tx[SJA1105_NUM_PORTS]; 33 36 u64 cgu_idiv[SJA1105_NUM_PORTS]; 34 37 u64 rgmii_pad_mii_tx[SJA1105_NUM_PORTS]; ··· 57 50 * switch core and device_id) 58 51 */ 59 52 u64 part_no; 53 + /* E/T and P/Q/R/S have partial timestamps of different sizes. 54 + * They must be reconstructed on both families anyway to get the full 55 + * 64-bit values back. 56 + */ 57 + int ptp_ts_bits; 58 + /* Also SPI commands are of different sizes to retrieve 59 + * the egress timestamps. 60 + */ 61 + int ptpegr_ts_bytes; 60 62 const struct sja1105_dynamic_table_ops *dyn_ops; 61 63 const struct sja1105_table_ops *static_ops; 62 64 const struct sja1105_regs *regs; 65 + int (*ptp_cmd)(const void *ctx, const void *data); 63 66 int (*reset_cmd)(const void *ctx, const void *data); 64 67 int (*setup_rgmii_delay)(const void *ctx, int port); 65 68 /* Prototypes from include/net/dsa.h */ ··· 89 72 struct spi_device *spidev; 90 73 struct dsa_switch *ds; 91 74 struct sja1105_port ports[SJA1105_NUM_PORTS]; 75 + struct ptp_clock_info ptp_caps; 76 + struct ptp_clock *clock; 77 + /* The cycle counter translates the PTP timestamps (based on 78 + * a free-running counter) into a software time domain. 79 + */ 80 + struct cyclecounter tstamp_cc; 81 + struct timecounter tstamp_tc; 82 + struct delayed_work refresh_work; 83 + /* Serializes all operations on the cycle counter */ 84 + struct mutex ptp_lock; 92 85 /* Serializes transmission of management frames so that 93 86 * the switch doesn't confuse them with one another. 94 87 */ 95 88 struct mutex mgmt_lock; 89 + struct sja1105_tagger_data tagger_data; 96 90 }; 97 91 98 92 #include "sja1105_dynamic_config.h" 93 + #include "sja1105_ptp.h" 99 94 100 95 struct sja1105_spi_message { 101 96 u64 access;
+2
drivers/net/dsa/sja1105/sja1105_dynamic_config.c
··· 378 378 .addr = 0x38, 379 379 }, 380 380 [BLK_IDX_L2_FORWARDING_PARAMS] = {0}, 381 + [BLK_IDX_AVB_PARAMS] = {0}, 381 382 [BLK_IDX_GENERAL_PARAMS] = { 382 383 .entry_packing = sja1105et_general_params_entry_packing, 383 384 .cmd_packing = sja1105et_general_params_cmd_packing, ··· 442 441 .addr = 0x38, 443 442 }, 444 443 [BLK_IDX_L2_FORWARDING_PARAMS] = {0}, 444 + [BLK_IDX_AVB_PARAMS] = {0}, 445 445 [BLK_IDX_GENERAL_PARAMS] = { 446 446 .entry_packing = sja1105et_general_params_entry_packing, 447 447 .cmd_packing = sja1105et_general_params_cmd_packing,
+290 -26
drivers/net/dsa/sja1105/sja1105_main.c
··· 389 389 .mirr_ptacu = 0, 390 390 .switchid = priv->ds->index, 391 391 /* Priority queue for link-local frames trapped to CPU */ 392 - .hostprio = 0, 392 + .hostprio = 7, 393 393 .mac_fltres1 = SJA1105_LINKLOCAL_FILTER_A, 394 394 .mac_flt1 = SJA1105_LINKLOCAL_FILTER_A_MASK, 395 - .incl_srcpt1 = true, 395 + .incl_srcpt1 = false, 396 396 .send_meta1 = false, 397 397 .mac_fltres0 = SJA1105_LINKLOCAL_FILTER_B, 398 398 .mac_flt0 = SJA1105_LINKLOCAL_FILTER_B_MASK, 399 - .incl_srcpt0 = true, 399 + .incl_srcpt0 = false, 400 400 .send_meta0 = false, 401 401 /* The destination for traffic matching mac_fltres1 and 402 402 * mac_fltres0 on all ports except host_port. Such traffic ··· 508 508 return 0; 509 509 } 510 510 511 + static int sja1105_init_avb_params(struct sja1105_private *priv, 512 + bool on) 513 + { 514 + struct sja1105_avb_params_entry *avb; 515 + struct sja1105_table *table; 516 + 517 + table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS]; 518 + 519 + /* Discard previous AVB Parameters Table */ 520 + if (table->entry_count) { 521 + kfree(table->entries); 522 + table->entry_count = 0; 523 + } 524 + 525 + /* Configure the reception of meta frames only if requested */ 526 + if (!on) 527 + return 0; 528 + 529 + table->entries = kcalloc(SJA1105_MAX_AVB_PARAMS_COUNT, 530 + table->ops->unpacked_entry_size, GFP_KERNEL); 531 + if (!table->entries) 532 + return -ENOMEM; 533 + 534 + table->entry_count = SJA1105_MAX_AVB_PARAMS_COUNT; 535 + 536 + avb = table->entries; 537 + 538 + avb->destmeta = SJA1105_META_DMAC; 539 + avb->srcmeta = SJA1105_META_SMAC; 540 + 541 + return 0; 542 + } 543 + 511 544 static int sja1105_static_config_load(struct sja1105_private *priv, 512 545 struct sja1105_dt_port *ports) 513 546 { ··· 579 546 if (rc < 0) 580 547 return rc; 581 548 rc = sja1105_init_general_params(priv); 549 + if (rc < 0) 550 + return rc; 551 + rc = sja1105_init_avb_params(priv, false); 582 552 if (rc < 0) 583 553 return rc; 584 554 ··· 1325 1289 return rc; 1326 1290 } 1327 1291 1328 - /* The TPID setting belongs to the General Parameters table, 1329 - * which can only be partially reconfigured at runtime (and not the TPID). 1330 - * So a switch reset is required. 1331 - */ 1332 - static int sja1105_change_tpid(struct sja1105_private *priv, 1333 - u16 tpid, u16 tpid2) 1334 - { 1335 - struct sja1105_general_params_entry *general_params; 1336 - struct sja1105_table *table; 1337 - 1338 - table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 1339 - general_params = table->entries; 1340 - general_params->tpid = tpid; 1341 - general_params->tpid2 = tpid2; 1342 - return sja1105_static_config_reload(priv); 1343 - } 1344 - 1345 1292 static int sja1105_pvid_apply(struct sja1105_private *priv, int port, u16 pvid) 1346 1293 { 1347 1294 struct sja1105_mac_config_entry *mac; ··· 1443 1424 return 0; 1444 1425 } 1445 1426 1427 + /* The TPID setting belongs to the General Parameters table, 1428 + * which can only be partially reconfigured at runtime (and not the TPID). 1429 + * So a switch reset is required. 1430 + */ 1446 1431 static int sja1105_vlan_filtering(struct dsa_switch *ds, int port, bool enabled) 1447 1432 { 1433 + struct sja1105_general_params_entry *general_params; 1448 1434 struct sja1105_private *priv = ds->priv; 1435 + struct sja1105_table *table; 1436 + u16 tpid, tpid2; 1449 1437 int rc; 1450 1438 1451 - if (enabled) 1439 + if (enabled) { 1452 1440 /* Enable VLAN filtering. */ 1453 - rc = sja1105_change_tpid(priv, ETH_P_8021Q, ETH_P_8021AD); 1454 - else 1441 + tpid = ETH_P_8021AD; 1442 + tpid2 = ETH_P_8021Q; 1443 + } else { 1455 1444 /* Disable VLAN filtering. */ 1456 - rc = sja1105_change_tpid(priv, ETH_P_SJA1105, ETH_P_SJA1105); 1445 + tpid = ETH_P_SJA1105; 1446 + tpid2 = ETH_P_SJA1105; 1447 + } 1448 + 1449 + table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 1450 + general_params = table->entries; 1451 + /* EtherType used to identify outer tagged (S-tag) VLAN traffic */ 1452 + general_params->tpid = tpid; 1453 + /* EtherType used to identify inner tagged (C-tag) VLAN traffic */ 1454 + general_params->tpid2 = tpid2; 1455 + /* When VLAN filtering is on, we need to at least be able to 1456 + * decode management traffic through the "backup plan". 1457 + */ 1458 + general_params->incl_srcpt1 = enabled; 1459 + general_params->incl_srcpt0 = enabled; 1460 + 1461 + rc = sja1105_static_config_reload(priv); 1457 1462 if (rc) 1458 1463 dev_err(ds->dev, "Failed to change VLAN Ethertype\n"); 1459 1464 ··· 1566 1523 return rc; 1567 1524 } 1568 1525 1526 + rc = sja1105_ptp_clock_register(priv); 1527 + if (rc < 0) { 1528 + dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc); 1529 + return rc; 1530 + } 1569 1531 /* Create and send configuration down to device */ 1570 1532 rc = sja1105_static_config_load(priv, ports); 1571 1533 if (rc < 0) { ··· 1600 1552 return sja1105_setup_8021q_tagging(ds, true); 1601 1553 } 1602 1554 1555 + static void sja1105_teardown(struct dsa_switch *ds) 1556 + { 1557 + struct sja1105_private *priv = ds->priv; 1558 + 1559 + cancel_work_sync(&priv->tagger_data.rxtstamp_work); 1560 + skb_queue_purge(&priv->tagger_data.skb_rxtstamp_queue); 1561 + } 1562 + 1603 1563 static int sja1105_mgmt_xmit(struct dsa_switch *ds, int port, int slot, 1604 - struct sk_buff *skb) 1564 + struct sk_buff *skb, bool takets) 1605 1565 { 1606 1566 struct sja1105_mgmt_entry mgmt_route = {0}; 1607 1567 struct sja1105_private *priv = ds->priv; ··· 1622 1566 mgmt_route.macaddr = ether_addr_to_u64(hdr->h_dest); 1623 1567 mgmt_route.destports = BIT(port); 1624 1568 mgmt_route.enfport = 1; 1569 + mgmt_route.tsreg = 0; 1570 + mgmt_route.takets = takets; 1625 1571 1626 1572 rc = sja1105_dynamic_config_write(priv, BLK_IDX_MGMT_ROUTE, 1627 1573 slot, &mgmt_route, true); ··· 1675 1617 { 1676 1618 struct sja1105_private *priv = ds->priv; 1677 1619 struct sja1105_port *sp = &priv->ports[port]; 1620 + struct skb_shared_hwtstamps shwt = {0}; 1678 1621 int slot = sp->mgmt_slot; 1622 + struct sk_buff *clone; 1623 + u64 now, ts; 1624 + int rc; 1679 1625 1680 1626 /* The tragic fact about the switch having 4x2 slots for installing 1681 1627 * management routes is that all of them except one are actually ··· 1697 1635 */ 1698 1636 mutex_lock(&priv->mgmt_lock); 1699 1637 1700 - sja1105_mgmt_xmit(ds, port, slot, skb); 1638 + /* The clone, if there, was made by dsa_skb_tx_timestamp */ 1639 + clone = DSA_SKB_CB(skb)->clone; 1701 1640 1641 + sja1105_mgmt_xmit(ds, port, slot, skb, !!clone); 1642 + 1643 + if (!clone) 1644 + goto out; 1645 + 1646 + skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS; 1647 + 1648 + mutex_lock(&priv->ptp_lock); 1649 + 1650 + now = priv->tstamp_cc.read(&priv->tstamp_cc); 1651 + 1652 + rc = sja1105_ptpegr_ts_poll(priv, slot, &ts); 1653 + if (rc < 0) { 1654 + dev_err(ds->dev, "xmit: timed out polling for tstamp\n"); 1655 + kfree_skb(clone); 1656 + goto out_unlock_ptp; 1657 + } 1658 + 1659 + ts = sja1105_tstamp_reconstruct(priv, now, ts); 1660 + ts = timecounter_cyc2time(&priv->tstamp_tc, ts); 1661 + 1662 + shwt.hwtstamp = ns_to_ktime(ts); 1663 + skb_complete_tx_timestamp(clone, &shwt); 1664 + 1665 + out_unlock_ptp: 1666 + mutex_unlock(&priv->ptp_lock); 1667 + out: 1702 1668 mutex_unlock(&priv->mgmt_lock); 1703 1669 return NETDEV_TX_OK; 1704 1670 } ··· 1755 1665 return sja1105_static_config_reload(priv); 1756 1666 } 1757 1667 1668 + /* Caller must hold priv->tagger_data.meta_lock */ 1669 + static int sja1105_change_rxtstamping(struct sja1105_private *priv, 1670 + bool on) 1671 + { 1672 + struct sja1105_general_params_entry *general_params; 1673 + struct sja1105_table *table; 1674 + int rc; 1675 + 1676 + table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS]; 1677 + general_params = table->entries; 1678 + general_params->send_meta1 = on; 1679 + general_params->send_meta0 = on; 1680 + 1681 + rc = sja1105_init_avb_params(priv, on); 1682 + if (rc < 0) 1683 + return rc; 1684 + 1685 + /* Initialize the meta state machine to a known state */ 1686 + if (priv->tagger_data.stampable_skb) { 1687 + kfree_skb(priv->tagger_data.stampable_skb); 1688 + priv->tagger_data.stampable_skb = NULL; 1689 + } 1690 + 1691 + return sja1105_static_config_reload(priv); 1692 + } 1693 + 1694 + static int sja1105_hwtstamp_set(struct dsa_switch *ds, int port, 1695 + struct ifreq *ifr) 1696 + { 1697 + struct sja1105_private *priv = ds->priv; 1698 + struct hwtstamp_config config; 1699 + bool rx_on; 1700 + int rc; 1701 + 1702 + if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) 1703 + return -EFAULT; 1704 + 1705 + switch (config.tx_type) { 1706 + case HWTSTAMP_TX_OFF: 1707 + priv->ports[port].hwts_tx_en = false; 1708 + break; 1709 + case HWTSTAMP_TX_ON: 1710 + priv->ports[port].hwts_tx_en = true; 1711 + break; 1712 + default: 1713 + return -ERANGE; 1714 + } 1715 + 1716 + switch (config.rx_filter) { 1717 + case HWTSTAMP_FILTER_NONE: 1718 + rx_on = false; 1719 + break; 1720 + default: 1721 + rx_on = true; 1722 + break; 1723 + } 1724 + 1725 + if (rx_on != priv->tagger_data.hwts_rx_en) { 1726 + spin_lock(&priv->tagger_data.meta_lock); 1727 + rc = sja1105_change_rxtstamping(priv, rx_on); 1728 + spin_unlock(&priv->tagger_data.meta_lock); 1729 + if (rc < 0) { 1730 + dev_err(ds->dev, 1731 + "Failed to change RX timestamping: %d\n", rc); 1732 + return -EFAULT; 1733 + } 1734 + priv->tagger_data.hwts_rx_en = rx_on; 1735 + } 1736 + 1737 + if (copy_to_user(ifr->ifr_data, &config, sizeof(config))) 1738 + return -EFAULT; 1739 + return 0; 1740 + } 1741 + 1742 + static int sja1105_hwtstamp_get(struct dsa_switch *ds, int port, 1743 + struct ifreq *ifr) 1744 + { 1745 + struct sja1105_private *priv = ds->priv; 1746 + struct hwtstamp_config config; 1747 + 1748 + config.flags = 0; 1749 + if (priv->ports[port].hwts_tx_en) 1750 + config.tx_type = HWTSTAMP_TX_ON; 1751 + else 1752 + config.tx_type = HWTSTAMP_TX_OFF; 1753 + if (priv->tagger_data.hwts_rx_en) 1754 + config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT; 1755 + else 1756 + config.rx_filter = HWTSTAMP_FILTER_NONE; 1757 + 1758 + return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? 1759 + -EFAULT : 0; 1760 + } 1761 + 1762 + #define to_tagger(d) \ 1763 + container_of((d), struct sja1105_tagger_data, rxtstamp_work) 1764 + #define to_sja1105(d) \ 1765 + container_of((d), struct sja1105_private, tagger_data) 1766 + 1767 + static void sja1105_rxtstamp_work(struct work_struct *work) 1768 + { 1769 + struct sja1105_tagger_data *data = to_tagger(work); 1770 + struct sja1105_private *priv = to_sja1105(data); 1771 + struct sk_buff *skb; 1772 + u64 now; 1773 + 1774 + mutex_lock(&priv->ptp_lock); 1775 + 1776 + now = priv->tstamp_cc.read(&priv->tstamp_cc); 1777 + 1778 + while ((skb = skb_dequeue(&data->skb_rxtstamp_queue)) != NULL) { 1779 + struct skb_shared_hwtstamps *shwt = skb_hwtstamps(skb); 1780 + u64 ts; 1781 + 1782 + *shwt = (struct skb_shared_hwtstamps) {0}; 1783 + 1784 + ts = SJA1105_SKB_CB(skb)->meta_tstamp; 1785 + ts = sja1105_tstamp_reconstruct(priv, now, ts); 1786 + ts = timecounter_cyc2time(&priv->tstamp_tc, ts); 1787 + 1788 + shwt->hwtstamp = ns_to_ktime(ts); 1789 + netif_rx_ni(skb); 1790 + } 1791 + 1792 + mutex_unlock(&priv->ptp_lock); 1793 + } 1794 + 1795 + /* Called from dsa_skb_defer_rx_timestamp */ 1796 + bool sja1105_port_rxtstamp(struct dsa_switch *ds, int port, 1797 + struct sk_buff *skb, unsigned int type) 1798 + { 1799 + struct sja1105_private *priv = ds->priv; 1800 + struct sja1105_tagger_data *data = &priv->tagger_data; 1801 + 1802 + if (!data->hwts_rx_en) 1803 + return false; 1804 + 1805 + /* We need to read the full PTP clock to reconstruct the Rx 1806 + * timestamp. For that we need a sleepable context. 1807 + */ 1808 + skb_queue_tail(&data->skb_rxtstamp_queue, skb); 1809 + schedule_work(&data->rxtstamp_work); 1810 + return true; 1811 + } 1812 + 1813 + /* Called from dsa_skb_tx_timestamp. This callback is just to make DSA clone 1814 + * the skb and have it available in DSA_SKB_CB in the .port_deferred_xmit 1815 + * callback, where we will timestamp it synchronously. 1816 + */ 1817 + bool sja1105_port_txtstamp(struct dsa_switch *ds, int port, 1818 + struct sk_buff *skb, unsigned int type) 1819 + { 1820 + struct sja1105_private *priv = ds->priv; 1821 + struct sja1105_port *sp = &priv->ports[port]; 1822 + 1823 + if (!sp->hwts_tx_en) 1824 + return false; 1825 + 1826 + return true; 1827 + } 1828 + 1758 1829 static const struct dsa_switch_ops sja1105_switch_ops = { 1759 1830 .get_tag_protocol = sja1105_get_tag_protocol, 1760 1831 .setup = sja1105_setup, 1832 + .teardown = sja1105_teardown, 1761 1833 .set_ageing_time = sja1105_set_ageing_time, 1762 1834 .phylink_validate = sja1105_phylink_validate, 1763 1835 .phylink_mac_config = sja1105_mac_config, 1764 1836 .get_strings = sja1105_get_strings, 1765 1837 .get_ethtool_stats = sja1105_get_ethtool_stats, 1766 1838 .get_sset_count = sja1105_get_sset_count, 1839 + .get_ts_info = sja1105_get_ts_info, 1767 1840 .port_fdb_dump = sja1105_fdb_dump, 1768 1841 .port_fdb_add = sja1105_fdb_add, 1769 1842 .port_fdb_del = sja1105_fdb_del, ··· 1941 1688 .port_mdb_add = sja1105_mdb_add, 1942 1689 .port_mdb_del = sja1105_mdb_del, 1943 1690 .port_deferred_xmit = sja1105_port_deferred_xmit, 1691 + .port_hwtstamp_get = sja1105_hwtstamp_get, 1692 + .port_hwtstamp_set = sja1105_hwtstamp_set, 1693 + .port_rxtstamp = sja1105_port_rxtstamp, 1694 + .port_txtstamp = sja1105_port_txtstamp, 1944 1695 }; 1945 1696 1946 1697 static int sja1105_check_device_id(struct sja1105_private *priv) ··· 1985 1728 1986 1729 static int sja1105_probe(struct spi_device *spi) 1987 1730 { 1731 + struct sja1105_tagger_data *tagger_data; 1988 1732 struct device *dev = &spi->dev; 1989 1733 struct sja1105_private *priv; 1990 1734 struct dsa_switch *ds; ··· 2040 1782 ds->priv = priv; 2041 1783 priv->ds = ds; 2042 1784 1785 + tagger_data = &priv->tagger_data; 1786 + skb_queue_head_init(&tagger_data->skb_rxtstamp_queue); 1787 + INIT_WORK(&tagger_data->rxtstamp_work, sja1105_rxtstamp_work); 1788 + 2043 1789 /* Connections between dsa_port and sja1105_port */ 2044 1790 for (i = 0; i < SJA1105_NUM_PORTS; i++) { 2045 1791 struct sja1105_port *sp = &priv->ports[i]; 2046 1792 2047 1793 ds->ports[i].priv = sp; 2048 1794 sp->dp = &ds->ports[i]; 1795 + sp->data = tagger_data; 2049 1796 } 2050 1797 mutex_init(&priv->mgmt_lock); 2051 1798 ··· 2061 1798 { 2062 1799 struct sja1105_private *priv = spi_get_drvdata(spi); 2063 1800 1801 + sja1105_ptp_clock_unregister(priv); 2064 1802 dsa_unregister_switch(priv->ds); 2065 1803 sja1105_static_config_free(&priv->static_config); 2066 1804 return 0;
+404
drivers/net/dsa/sja1105/sja1105_ptp.c
··· 1 + // SPDX-License-Identifier: GPL-2.0 2 + /* Copyright (c) 2019, Vladimir Oltean <olteanv@gmail.com> 3 + */ 4 + #include "sja1105.h" 5 + 6 + /* The adjfine API clamps ppb between [-32,768,000, 32,768,000], and 7 + * therefore scaled_ppm between [-2,147,483,648, 2,147,483,647]. 8 + * Set the maximum supported ppb to a round value smaller than the maximum. 9 + * 10 + * Percentually speaking, this is a +/- 0.032x adjustment of the 11 + * free-running counter (0.968x to 1.032x). 12 + */ 13 + #define SJA1105_MAX_ADJ_PPB 32000000 14 + #define SJA1105_SIZE_PTP_CMD 4 15 + 16 + /* Timestamps are in units of 8 ns clock ticks (equivalent to a fixed 17 + * 125 MHz clock) so the scale factor (MULT / SHIFT) needs to be 8. 18 + * Furthermore, wisely pick SHIFT as 28 bits, which translates 19 + * MULT into 2^31 (0x80000000). This is the same value around which 20 + * the hardware PTPCLKRATE is centered, so the same ppb conversion 21 + * arithmetic can be reused. 22 + */ 23 + #define SJA1105_CC_SHIFT 28 24 + #define SJA1105_CC_MULT (8 << SJA1105_CC_SHIFT) 25 + 26 + /* Having 33 bits of cycle counter left until a 64-bit overflow during delta 27 + * conversion, we multiply this by the 8 ns counter resolution and arrive at 28 + * a comfortable 68.71 second refresh interval until the delta would cause 29 + * an integer overflow, in absence of any other readout. 30 + * Approximate to 1 minute. 31 + */ 32 + #define SJA1105_REFRESH_INTERVAL (HZ * 60) 33 + 34 + /* This range is actually +/- SJA1105_MAX_ADJ_PPB 35 + * divided by 1000 (ppb -> ppm) and with a 16-bit 36 + * "fractional" part (actually fixed point). 37 + * | 38 + * v 39 + * Convert scaled_ppm from the +/- ((10^6) << 16) range 40 + * into the +/- (1 << 31) range. 41 + * 42 + * This forgoes a "ppb" numeric representation (up to NSEC_PER_SEC) 43 + * and defines the scaling factor between scaled_ppm and the actual 44 + * frequency adjustments (both cycle counter and hardware). 45 + * 46 + * ptpclkrate = scaled_ppm * 2^31 / (10^6 * 2^16) 47 + * simplifies to 48 + * ptpclkrate = scaled_ppm * 2^9 / 5^6 49 + */ 50 + #define SJA1105_CC_MULT_NUM (1 << 9) 51 + #define SJA1105_CC_MULT_DEM 15625 52 + 53 + #define ptp_to_sja1105(d) container_of((d), struct sja1105_private, ptp_caps) 54 + #define cc_to_sja1105(d) container_of((d), struct sja1105_private, tstamp_cc) 55 + #define dw_to_sja1105(d) container_of((d), struct sja1105_private, refresh_work) 56 + 57 + struct sja1105_ptp_cmd { 58 + u64 resptp; /* reset */ 59 + }; 60 + 61 + int sja1105_get_ts_info(struct dsa_switch *ds, int port, 62 + struct ethtool_ts_info *info) 63 + { 64 + struct sja1105_private *priv = ds->priv; 65 + 66 + /* Called during cleanup */ 67 + if (!priv->clock) 68 + return -ENODEV; 69 + 70 + info->so_timestamping = SOF_TIMESTAMPING_TX_HARDWARE | 71 + SOF_TIMESTAMPING_RX_HARDWARE | 72 + SOF_TIMESTAMPING_RAW_HARDWARE; 73 + info->tx_types = (1 << HWTSTAMP_TX_OFF) | 74 + (1 << HWTSTAMP_TX_ON); 75 + info->rx_filters = (1 << HWTSTAMP_FILTER_NONE) | 76 + (1 << HWTSTAMP_FILTER_PTP_V2_L2_EVENT); 77 + info->phc_index = ptp_clock_index(priv->clock); 78 + return 0; 79 + } 80 + EXPORT_SYMBOL_GPL(sja1105_get_ts_info); 81 + 82 + int sja1105et_ptp_cmd(const void *ctx, const void *data) 83 + { 84 + const struct sja1105_ptp_cmd *cmd = data; 85 + const struct sja1105_private *priv = ctx; 86 + const struct sja1105_regs *regs = priv->info->regs; 87 + const int size = SJA1105_SIZE_PTP_CMD; 88 + u8 buf[SJA1105_SIZE_PTP_CMD] = {0}; 89 + /* No need to keep this as part of the structure */ 90 + u64 valid = 1; 91 + 92 + sja1105_pack(buf, &valid, 31, 31, size); 93 + sja1105_pack(buf, &cmd->resptp, 2, 2, size); 94 + 95 + return sja1105_spi_send_packed_buf(priv, SPI_WRITE, regs->ptp_control, 96 + buf, SJA1105_SIZE_PTP_CMD); 97 + } 98 + EXPORT_SYMBOL_GPL(sja1105et_ptp_cmd); 99 + 100 + int sja1105pqrs_ptp_cmd(const void *ctx, const void *data) 101 + { 102 + const struct sja1105_ptp_cmd *cmd = data; 103 + const struct sja1105_private *priv = ctx; 104 + const struct sja1105_regs *regs = priv->info->regs; 105 + const int size = SJA1105_SIZE_PTP_CMD; 106 + u8 buf[SJA1105_SIZE_PTP_CMD] = {0}; 107 + /* No need to keep this as part of the structure */ 108 + u64 valid = 1; 109 + 110 + sja1105_pack(buf, &valid, 31, 31, size); 111 + sja1105_pack(buf, &cmd->resptp, 3, 3, size); 112 + 113 + return sja1105_spi_send_packed_buf(priv, SPI_WRITE, regs->ptp_control, 114 + buf, SJA1105_SIZE_PTP_CMD); 115 + } 116 + EXPORT_SYMBOL_GPL(sja1105pqrs_ptp_cmd); 117 + 118 + /* The switch returns partial timestamps (24 bits for SJA1105 E/T, which wrap 119 + * around in 0.135 seconds, and 32 bits for P/Q/R/S, wrapping around in 34.35 120 + * seconds). 121 + * 122 + * This receives the RX or TX MAC timestamps, provided by hardware as 123 + * the lower bits of the cycle counter, sampled at the time the timestamp was 124 + * collected. 125 + * 126 + * To reconstruct into a full 64-bit-wide timestamp, the cycle counter is 127 + * read and the high-order bits are filled in. 128 + * 129 + * Must be called within one wraparound period of the partial timestamp since 130 + * it was generated by the MAC. 131 + */ 132 + u64 sja1105_tstamp_reconstruct(struct sja1105_private *priv, u64 now, 133 + u64 ts_partial) 134 + { 135 + u64 partial_tstamp_mask = CYCLECOUNTER_MASK(priv->info->ptp_ts_bits); 136 + u64 ts_reconstructed; 137 + 138 + ts_reconstructed = (now & ~partial_tstamp_mask) | ts_partial; 139 + 140 + /* Check lower bits of current cycle counter against the timestamp. 141 + * If the current cycle counter is lower than the partial timestamp, 142 + * then wraparound surely occurred and must be accounted for. 143 + */ 144 + if ((now & partial_tstamp_mask) <= ts_partial) 145 + ts_reconstructed -= (partial_tstamp_mask + 1); 146 + 147 + return ts_reconstructed; 148 + } 149 + EXPORT_SYMBOL_GPL(sja1105_tstamp_reconstruct); 150 + 151 + /* Reads the SPI interface for an egress timestamp generated by the switch 152 + * for frames sent using management routes. 153 + * 154 + * SJA1105 E/T layout of the 4-byte SPI payload: 155 + * 156 + * 31 23 15 7 0 157 + * | | | | | 158 + * +-----+-----+-----+ ^ 159 + * ^ | 160 + * | | 161 + * 24-bit timestamp Update bit 162 + * 163 + * 164 + * SJA1105 P/Q/R/S layout of the 8-byte SPI payload: 165 + * 166 + * 31 23 15 7 0 63 55 47 39 32 167 + * | | | | | | | | | | 168 + * ^ +-----+-----+-----+-----+ 169 + * | ^ 170 + * | | 171 + * Update bit 32-bit timestamp 172 + * 173 + * Notice that the update bit is in the same place. 174 + * To have common code for E/T and P/Q/R/S for reading the timestamp, 175 + * we need to juggle with the offset and the bit indices. 176 + */ 177 + int sja1105_ptpegr_ts_poll(struct sja1105_private *priv, int port, u64 *ts) 178 + { 179 + const struct sja1105_regs *regs = priv->info->regs; 180 + int tstamp_bit_start, tstamp_bit_end; 181 + int timeout = 10; 182 + u8 packed_buf[8]; 183 + u64 update; 184 + int rc; 185 + 186 + do { 187 + rc = sja1105_spi_send_packed_buf(priv, SPI_READ, 188 + regs->ptpegr_ts[port], 189 + packed_buf, 190 + priv->info->ptpegr_ts_bytes); 191 + if (rc < 0) 192 + return rc; 193 + 194 + sja1105_unpack(packed_buf, &update, 0, 0, 195 + priv->info->ptpegr_ts_bytes); 196 + if (update) 197 + break; 198 + 199 + usleep_range(10, 50); 200 + } while (--timeout); 201 + 202 + if (!timeout) 203 + return -ETIMEDOUT; 204 + 205 + /* Point the end bit to the second 32-bit word on P/Q/R/S, 206 + * no-op on E/T. 207 + */ 208 + tstamp_bit_end = (priv->info->ptpegr_ts_bytes - 4) * 8; 209 + /* Shift the 24-bit timestamp on E/T to be collected from 31:8. 210 + * No-op on P/Q/R/S. 211 + */ 212 + tstamp_bit_end += 32 - priv->info->ptp_ts_bits; 213 + tstamp_bit_start = tstamp_bit_end + priv->info->ptp_ts_bits - 1; 214 + 215 + *ts = 0; 216 + 217 + sja1105_unpack(packed_buf, ts, tstamp_bit_start, tstamp_bit_end, 218 + priv->info->ptpegr_ts_bytes); 219 + 220 + return 0; 221 + } 222 + EXPORT_SYMBOL_GPL(sja1105_ptpegr_ts_poll); 223 + 224 + int sja1105_ptp_reset(struct sja1105_private *priv) 225 + { 226 + struct dsa_switch *ds = priv->ds; 227 + struct sja1105_ptp_cmd cmd = {0}; 228 + int rc; 229 + 230 + mutex_lock(&priv->ptp_lock); 231 + 232 + cmd.resptp = 1; 233 + dev_dbg(ds->dev, "Resetting PTP clock\n"); 234 + rc = priv->info->ptp_cmd(priv, &cmd); 235 + 236 + timecounter_init(&priv->tstamp_tc, &priv->tstamp_cc, 237 + ktime_to_ns(ktime_get_real())); 238 + 239 + mutex_unlock(&priv->ptp_lock); 240 + 241 + return rc; 242 + } 243 + EXPORT_SYMBOL_GPL(sja1105_ptp_reset); 244 + 245 + static int sja1105_ptp_gettime(struct ptp_clock_info *ptp, 246 + struct timespec64 *ts) 247 + { 248 + struct sja1105_private *priv = ptp_to_sja1105(ptp); 249 + u64 ns; 250 + 251 + mutex_lock(&priv->ptp_lock); 252 + ns = timecounter_read(&priv->tstamp_tc); 253 + mutex_unlock(&priv->ptp_lock); 254 + 255 + *ts = ns_to_timespec64(ns); 256 + 257 + return 0; 258 + } 259 + 260 + static int sja1105_ptp_settime(struct ptp_clock_info *ptp, 261 + const struct timespec64 *ts) 262 + { 263 + struct sja1105_private *priv = ptp_to_sja1105(ptp); 264 + u64 ns = timespec64_to_ns(ts); 265 + 266 + mutex_lock(&priv->ptp_lock); 267 + timecounter_init(&priv->tstamp_tc, &priv->tstamp_cc, ns); 268 + mutex_unlock(&priv->ptp_lock); 269 + 270 + return 0; 271 + } 272 + 273 + static int sja1105_ptp_adjfine(struct ptp_clock_info *ptp, long scaled_ppm) 274 + { 275 + struct sja1105_private *priv = ptp_to_sja1105(ptp); 276 + s64 clkrate; 277 + 278 + clkrate = (s64)scaled_ppm * SJA1105_CC_MULT_NUM; 279 + clkrate = div_s64(clkrate, SJA1105_CC_MULT_DEM); 280 + 281 + mutex_lock(&priv->ptp_lock); 282 + 283 + /* Force a readout to update the timer *before* changing its frequency. 284 + * 285 + * This way, its corrected time curve can at all times be modeled 286 + * as a linear "A * x + B" function, where: 287 + * 288 + * - B are past frequency adjustments and offset shifts, all 289 + * accumulated into the cycle_last variable. 290 + * 291 + * - A is the new frequency adjustments we're just about to set. 292 + * 293 + * Reading now makes B accumulate the correct amount of time, 294 + * corrected at the old rate, before changing it. 295 + * 296 + * Hardware timestamps then become simple points on the curve and 297 + * are approximated using the above function. This is still better 298 + * than letting the switch take the timestamps using the hardware 299 + * rate-corrected clock (PTPCLKVAL) - the comparison in this case would 300 + * be that we're shifting the ruler at the same time as we're taking 301 + * measurements with it. 302 + * 303 + * The disadvantage is that it's possible to receive timestamps when 304 + * a frequency adjustment took place in the near past. 305 + * In this case they will be approximated using the new ppb value 306 + * instead of a compound function made of two segments (one at the old 307 + * and the other at the new rate) - introducing some inaccuracy. 308 + */ 309 + timecounter_read(&priv->tstamp_tc); 310 + 311 + priv->tstamp_cc.mult = SJA1105_CC_MULT + clkrate; 312 + 313 + mutex_unlock(&priv->ptp_lock); 314 + 315 + return 0; 316 + } 317 + 318 + static int sja1105_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta) 319 + { 320 + struct sja1105_private *priv = ptp_to_sja1105(ptp); 321 + 322 + mutex_lock(&priv->ptp_lock); 323 + timecounter_adjtime(&priv->tstamp_tc, delta); 324 + mutex_unlock(&priv->ptp_lock); 325 + 326 + return 0; 327 + } 328 + 329 + static u64 sja1105_ptptsclk_read(const struct cyclecounter *cc) 330 + { 331 + struct sja1105_private *priv = cc_to_sja1105(cc); 332 + const struct sja1105_regs *regs = priv->info->regs; 333 + u64 ptptsclk = 0; 334 + int rc; 335 + 336 + rc = sja1105_spi_send_int(priv, SPI_READ, regs->ptptsclk, 337 + &ptptsclk, 8); 338 + if (rc < 0) 339 + dev_err_ratelimited(priv->ds->dev, 340 + "failed to read ptp cycle counter: %d\n", 341 + rc); 342 + return ptptsclk; 343 + } 344 + 345 + static void sja1105_ptp_overflow_check(struct work_struct *work) 346 + { 347 + struct delayed_work *dw = to_delayed_work(work); 348 + struct sja1105_private *priv = dw_to_sja1105(dw); 349 + struct timespec64 ts; 350 + 351 + sja1105_ptp_gettime(&priv->ptp_caps, &ts); 352 + 353 + schedule_delayed_work(&priv->refresh_work, SJA1105_REFRESH_INTERVAL); 354 + } 355 + 356 + static const struct ptp_clock_info sja1105_ptp_caps = { 357 + .owner = THIS_MODULE, 358 + .name = "SJA1105 PHC", 359 + .adjfine = sja1105_ptp_adjfine, 360 + .adjtime = sja1105_ptp_adjtime, 361 + .gettime64 = sja1105_ptp_gettime, 362 + .settime64 = sja1105_ptp_settime, 363 + .max_adj = SJA1105_MAX_ADJ_PPB, 364 + }; 365 + 366 + int sja1105_ptp_clock_register(struct sja1105_private *priv) 367 + { 368 + struct dsa_switch *ds = priv->ds; 369 + 370 + /* Set up the cycle counter */ 371 + priv->tstamp_cc = (struct cyclecounter) { 372 + .read = sja1105_ptptsclk_read, 373 + .mask = CYCLECOUNTER_MASK(64), 374 + .shift = SJA1105_CC_SHIFT, 375 + .mult = SJA1105_CC_MULT, 376 + }; 377 + mutex_init(&priv->ptp_lock); 378 + INIT_DELAYED_WORK(&priv->refresh_work, sja1105_ptp_overflow_check); 379 + 380 + schedule_delayed_work(&priv->refresh_work, SJA1105_REFRESH_INTERVAL); 381 + 382 + priv->ptp_caps = sja1105_ptp_caps; 383 + 384 + priv->clock = ptp_clock_register(&priv->ptp_caps, ds->dev); 385 + if (IS_ERR_OR_NULL(priv->clock)) 386 + return PTR_ERR(priv->clock); 387 + 388 + return sja1105_ptp_reset(priv); 389 + } 390 + EXPORT_SYMBOL_GPL(sja1105_ptp_clock_register); 391 + 392 + void sja1105_ptp_clock_unregister(struct sja1105_private *priv) 393 + { 394 + if (IS_ERR_OR_NULL(priv->clock)) 395 + return; 396 + 397 + ptp_clock_unregister(priv->clock); 398 + priv->clock = NULL; 399 + } 400 + EXPORT_SYMBOL_GPL(sja1105_ptp_clock_unregister); 401 + 402 + MODULE_AUTHOR("Vladimir Oltean <olteanv@gmail.com>"); 403 + MODULE_DESCRIPTION("SJA1105 PHC Driver"); 404 + MODULE_LICENSE("GPL v2");
+64
drivers/net/dsa/sja1105/sja1105_ptp.h
··· 1 + /* SPDX-License-Identifier: GPL-2.0 2 + * Copyright (c) 2019, Vladimir Oltean <olteanv@gmail.com> 3 + */ 4 + #ifndef _SJA1105_PTP_H 5 + #define _SJA1105_PTP_H 6 + 7 + #if IS_ENABLED(CONFIG_NET_DSA_SJA1105_PTP) 8 + 9 + int sja1105_ptp_clock_register(struct sja1105_private *priv); 10 + 11 + void sja1105_ptp_clock_unregister(struct sja1105_private *priv); 12 + 13 + int sja1105_ptpegr_ts_poll(struct sja1105_private *priv, int port, u64 *ts); 14 + 15 + int sja1105et_ptp_cmd(const void *ctx, const void *data); 16 + 17 + int sja1105pqrs_ptp_cmd(const void *ctx, const void *data); 18 + 19 + int sja1105_get_ts_info(struct dsa_switch *ds, int port, 20 + struct ethtool_ts_info *ts); 21 + 22 + u64 sja1105_tstamp_reconstruct(struct sja1105_private *priv, u64 now, 23 + u64 ts_partial); 24 + 25 + int sja1105_ptp_reset(struct sja1105_private *priv); 26 + 27 + #else 28 + 29 + static inline int sja1105_ptp_clock_register(struct sja1105_private *priv) 30 + { 31 + return 0; 32 + } 33 + 34 + static inline void sja1105_ptp_clock_unregister(struct sja1105_private *priv) 35 + { 36 + return; 37 + } 38 + 39 + static inline int 40 + sja1105_ptpegr_ts_poll(struct sja1105_private *priv, int port, u64 *ts) 41 + { 42 + return 0; 43 + } 44 + 45 + static inline u64 sja1105_tstamp_reconstruct(struct sja1105_private *priv, 46 + u64 now, u64 ts_partial) 47 + { 48 + return 0; 49 + } 50 + 51 + static inline int sja1105_ptp_reset(struct sja1105_private *priv) 52 + { 53 + return 0; 54 + } 55 + 56 + #define sja1105et_ptp_cmd NULL 57 + 58 + #define sja1105pqrs_ptp_cmd NULL 59 + 60 + #define sja1105_get_ts_info NULL 61 + 62 + #endif /* IS_ENABLED(CONFIG_NET_DSA_SJA1105_PTP) */ 63 + 64 + #endif /* _SJA1105_PTP_H */
+35
drivers/net/dsa/sja1105/sja1105_spi.c
··· 100 100 101 101 return 0; 102 102 } 103 + EXPORT_SYMBOL_GPL(sja1105_spi_send_packed_buf); 103 104 104 105 /* If @rw is: 105 106 * - SPI_WRITE: creates and sends an SPI write message at absolute ··· 136 135 137 136 return rc; 138 137 } 138 + EXPORT_SYMBOL_GPL(sja1105_spi_send_int); 139 139 140 140 /* Should be used if a @packed_buf larger than SJA1105_SIZE_SPI_MSG_MAXLEN 141 141 * must be sent/received. Splitting the buffer into chunks and assembling ··· 480 478 dev_info(dev, "Succeeded after %d tried\n", RETRIES - retries); 481 479 } 482 480 481 + rc = sja1105_ptp_reset(priv); 482 + if (rc < 0) 483 + dev_err(dev, "Failed to reset PTP clock: %d\n", rc); 484 + 483 485 dev_info(dev, "Reset switch and programmed static config\n"); 486 + 484 487 out: 485 488 kfree(config_buf); 486 489 return rc; ··· 514 507 .rgmii_tx_clk = {0x100016, 0x10001D, 0x100024, 0x10002B, 0x100032}, 515 508 .rmii_ref_clk = {0x100015, 0x10001C, 0x100023, 0x10002A, 0x100031}, 516 509 .rmii_ext_tx_clk = {0x100018, 0x10001F, 0x100026, 0x10002D, 0x100034}, 510 + .ptpegr_ts = {0xC0, 0xC2, 0xC4, 0xC6, 0xC8}, 511 + .ptp_control = 0x17, 512 + .ptpclk = 0x18, /* Spans 0x18 to 0x19 */ 513 + .ptpclkrate = 0x1A, 514 + .ptptsclk = 0x1B, /* Spans 0x1B to 0x1C */ 517 515 }; 518 516 519 517 static struct sja1105_regs sja1105pqrs_regs = { ··· 545 533 .rmii_ref_clk = {0x100015, 0x10001B, 0x100021, 0x100027, 0x10002D}, 546 534 .rmii_ext_tx_clk = {0x100017, 0x10001D, 0x100023, 0x100029, 0x10002F}, 547 535 .qlevel = {0x604, 0x614, 0x624, 0x634, 0x644}, 536 + .ptpegr_ts = {0xC0, 0xC4, 0xC8, 0xCC, 0xD0}, 537 + .ptp_control = 0x18, 538 + .ptpclk = 0x19, 539 + .ptpclkrate = 0x1B, 540 + .ptptsclk = 0x1C, 548 541 }; 549 542 550 543 struct sja1105_info sja1105e_info = { ··· 557 540 .part_no = SJA1105ET_PART_NO, 558 541 .static_ops = sja1105e_table_ops, 559 542 .dyn_ops = sja1105et_dyn_ops, 543 + .ptp_ts_bits = 24, 544 + .ptpegr_ts_bytes = 4, 560 545 .reset_cmd = sja1105et_reset_cmd, 561 546 .fdb_add_cmd = sja1105et_fdb_add, 562 547 .fdb_del_cmd = sja1105et_fdb_del, 548 + .ptp_cmd = sja1105et_ptp_cmd, 563 549 .regs = &sja1105et_regs, 564 550 .name = "SJA1105E", 565 551 }; ··· 571 551 .part_no = SJA1105ET_PART_NO, 572 552 .static_ops = sja1105t_table_ops, 573 553 .dyn_ops = sja1105et_dyn_ops, 554 + .ptp_ts_bits = 24, 555 + .ptpegr_ts_bytes = 4, 574 556 .reset_cmd = sja1105et_reset_cmd, 575 557 .fdb_add_cmd = sja1105et_fdb_add, 576 558 .fdb_del_cmd = sja1105et_fdb_del, 559 + .ptp_cmd = sja1105et_ptp_cmd, 577 560 .regs = &sja1105et_regs, 578 561 .name = "SJA1105T", 579 562 }; ··· 585 562 .part_no = SJA1105P_PART_NO, 586 563 .static_ops = sja1105p_table_ops, 587 564 .dyn_ops = sja1105pqrs_dyn_ops, 565 + .ptp_ts_bits = 32, 566 + .ptpegr_ts_bytes = 8, 588 567 .reset_cmd = sja1105pqrs_reset_cmd, 589 568 .fdb_add_cmd = sja1105pqrs_fdb_add, 590 569 .fdb_del_cmd = sja1105pqrs_fdb_del, 570 + .ptp_cmd = sja1105pqrs_ptp_cmd, 591 571 .regs = &sja1105pqrs_regs, 592 572 .name = "SJA1105P", 593 573 }; ··· 599 573 .part_no = SJA1105Q_PART_NO, 600 574 .static_ops = sja1105q_table_ops, 601 575 .dyn_ops = sja1105pqrs_dyn_ops, 576 + .ptp_ts_bits = 32, 577 + .ptpegr_ts_bytes = 8, 602 578 .reset_cmd = sja1105pqrs_reset_cmd, 603 579 .fdb_add_cmd = sja1105pqrs_fdb_add, 604 580 .fdb_del_cmd = sja1105pqrs_fdb_del, 581 + .ptp_cmd = sja1105pqrs_ptp_cmd, 605 582 .regs = &sja1105pqrs_regs, 606 583 .name = "SJA1105Q", 607 584 }; ··· 613 584 .part_no = SJA1105R_PART_NO, 614 585 .static_ops = sja1105r_table_ops, 615 586 .dyn_ops = sja1105pqrs_dyn_ops, 587 + .ptp_ts_bits = 32, 588 + .ptpegr_ts_bytes = 8, 616 589 .reset_cmd = sja1105pqrs_reset_cmd, 617 590 .fdb_add_cmd = sja1105pqrs_fdb_add, 618 591 .fdb_del_cmd = sja1105pqrs_fdb_del, 592 + .ptp_cmd = sja1105pqrs_ptp_cmd, 619 593 .regs = &sja1105pqrs_regs, 620 594 .name = "SJA1105R", 621 595 }; ··· 628 596 .static_ops = sja1105s_table_ops, 629 597 .dyn_ops = sja1105pqrs_dyn_ops, 630 598 .regs = &sja1105pqrs_regs, 599 + .ptp_ts_bits = 32, 600 + .ptpegr_ts_bytes = 8, 631 601 .reset_cmd = sja1105pqrs_reset_cmd, 632 602 .fdb_add_cmd = sja1105pqrs_fdb_add, 633 603 .fdb_del_cmd = sja1105pqrs_fdb_del, 604 + .ptp_cmd = sja1105pqrs_ptp_cmd, 634 605 .name = "SJA1105S", 635 606 };
+62
drivers/net/dsa/sja1105/sja1105_static_config.c
··· 35 35 } 36 36 dump_stack(); 37 37 } 38 + EXPORT_SYMBOL_GPL(sja1105_pack); 38 39 39 40 void sja1105_unpack(const void *buf, u64 *val, int start, int end, size_t len) 40 41 { ··· 53 52 start, end); 54 53 dump_stack(); 55 54 } 55 + EXPORT_SYMBOL_GPL(sja1105_unpack); 56 56 57 57 void sja1105_packing(void *buf, u64 *val, int start, int end, 58 58 size_t len, enum packing_op op) ··· 76 74 } 77 75 dump_stack(); 78 76 } 77 + EXPORT_SYMBOL_GPL(sja1105_packing); 79 78 80 79 /* Little-endian Ethernet CRC32 of data packed as big-endian u32 words */ 81 80 u32 sja1105_crc32(const void *buf, size_t len) ··· 92 89 crc = crc32_le(crc, (u8 *)&word, 4); 93 90 } 94 91 return ~crc; 92 + } 93 + 94 + static size_t sja1105et_avb_params_entry_packing(void *buf, void *entry_ptr, 95 + enum packing_op op) 96 + { 97 + const size_t size = SJA1105ET_SIZE_AVB_PARAMS_ENTRY; 98 + struct sja1105_avb_params_entry *entry = entry_ptr; 99 + 100 + sja1105_packing(buf, &entry->destmeta, 95, 48, size, op); 101 + sja1105_packing(buf, &entry->srcmeta, 47, 0, size, op); 102 + return size; 103 + } 104 + 105 + static size_t sja1105pqrs_avb_params_entry_packing(void *buf, void *entry_ptr, 106 + enum packing_op op) 107 + { 108 + const size_t size = SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY; 109 + struct sja1105_avb_params_entry *entry = entry_ptr; 110 + 111 + sja1105_packing(buf, &entry->destmeta, 125, 78, size, op); 112 + sja1105_packing(buf, &entry->srcmeta, 77, 30, size, op); 113 + return size; 95 114 } 96 115 97 116 static size_t sja1105et_general_params_entry_packing(void *buf, void *entry_ptr, ··· 448 423 [BLK_IDX_MAC_CONFIG] = BLKID_MAC_CONFIG, 449 424 [BLK_IDX_L2_LOOKUP_PARAMS] = BLKID_L2_LOOKUP_PARAMS, 450 425 [BLK_IDX_L2_FORWARDING_PARAMS] = BLKID_L2_FORWARDING_PARAMS, 426 + [BLK_IDX_AVB_PARAMS] = BLKID_AVB_PARAMS, 451 427 [BLK_IDX_GENERAL_PARAMS] = BLKID_GENERAL_PARAMS, 452 428 [BLK_IDX_XMII_PARAMS] = BLKID_XMII_PARAMS, 453 429 }; ··· 650 624 .packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY, 651 625 .max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT, 652 626 }, 627 + [BLK_IDX_AVB_PARAMS] = { 628 + .packing = sja1105et_avb_params_entry_packing, 629 + .unpacked_entry_size = sizeof(struct sja1105_avb_params_entry), 630 + .packed_entry_size = SJA1105ET_SIZE_AVB_PARAMS_ENTRY, 631 + .max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT, 632 + }, 653 633 [BLK_IDX_GENERAL_PARAMS] = { 654 634 .packing = sja1105et_general_params_entry_packing, 655 635 .unpacked_entry_size = sizeof(struct sja1105_general_params_entry), ··· 713 681 .unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_params_entry), 714 682 .packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY, 715 683 .max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT, 684 + }, 685 + [BLK_IDX_AVB_PARAMS] = { 686 + .packing = sja1105et_avb_params_entry_packing, 687 + .unpacked_entry_size = sizeof(struct sja1105_avb_params_entry), 688 + .packed_entry_size = SJA1105ET_SIZE_AVB_PARAMS_ENTRY, 689 + .max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT, 716 690 }, 717 691 [BLK_IDX_GENERAL_PARAMS] = { 718 692 .packing = sja1105et_general_params_entry_packing, ··· 778 740 .packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY, 779 741 .max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT, 780 742 }, 743 + [BLK_IDX_AVB_PARAMS] = { 744 + .packing = sja1105pqrs_avb_params_entry_packing, 745 + .unpacked_entry_size = sizeof(struct sja1105_avb_params_entry), 746 + .packed_entry_size = SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY, 747 + .max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT, 748 + }, 781 749 [BLK_IDX_GENERAL_PARAMS] = { 782 750 .packing = sja1105pqrs_general_params_entry_packing, 783 751 .unpacked_entry_size = sizeof(struct sja1105_general_params_entry), ··· 841 797 .unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_params_entry), 842 798 .packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY, 843 799 .max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT, 800 + }, 801 + [BLK_IDX_AVB_PARAMS] = { 802 + .packing = sja1105pqrs_avb_params_entry_packing, 803 + .unpacked_entry_size = sizeof(struct sja1105_avb_params_entry), 804 + .packed_entry_size = SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY, 805 + .max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT, 844 806 }, 845 807 [BLK_IDX_GENERAL_PARAMS] = { 846 808 .packing = sja1105pqrs_general_params_entry_packing, ··· 906 856 .packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY, 907 857 .max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT, 908 858 }, 859 + [BLK_IDX_AVB_PARAMS] = { 860 + .packing = sja1105pqrs_avb_params_entry_packing, 861 + .unpacked_entry_size = sizeof(struct sja1105_avb_params_entry), 862 + .packed_entry_size = SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY, 863 + .max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT, 864 + }, 909 865 [BLK_IDX_GENERAL_PARAMS] = { 910 866 .packing = sja1105pqrs_general_params_entry_packing, 911 867 .unpacked_entry_size = sizeof(struct sja1105_general_params_entry), ··· 969 913 .unpacked_entry_size = sizeof(struct sja1105_l2_forwarding_params_entry), 970 914 .packed_entry_size = SJA1105_SIZE_L2_FORWARDING_PARAMS_ENTRY, 971 915 .max_entry_count = SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT, 916 + }, 917 + [BLK_IDX_AVB_PARAMS] = { 918 + .packing = sja1105pqrs_avb_params_entry_packing, 919 + .unpacked_entry_size = sizeof(struct sja1105_avb_params_entry), 920 + .packed_entry_size = SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY, 921 + .max_entry_count = SJA1105_MAX_AVB_PARAMS_COUNT, 972 922 }, 973 923 [BLK_IDX_GENERAL_PARAMS] = { 974 924 .packing = sja1105pqrs_general_params_entry_packing,
+10
drivers/net/dsa/sja1105/sja1105_static_config.h
··· 20 20 #define SJA1105ET_SIZE_MAC_CONFIG_ENTRY 28 21 21 #define SJA1105ET_SIZE_L2_LOOKUP_PARAMS_ENTRY 4 22 22 #define SJA1105ET_SIZE_GENERAL_PARAMS_ENTRY 40 23 + #define SJA1105ET_SIZE_AVB_PARAMS_ENTRY 12 23 24 #define SJA1105PQRS_SIZE_L2_LOOKUP_ENTRY 20 24 25 #define SJA1105PQRS_SIZE_MAC_CONFIG_ENTRY 32 25 26 #define SJA1105PQRS_SIZE_L2_LOOKUP_PARAMS_ENTRY 16 26 27 #define SJA1105PQRS_SIZE_GENERAL_PARAMS_ENTRY 44 28 + #define SJA1105PQRS_SIZE_AVB_PARAMS_ENTRY 16 27 29 28 30 /* UM10944.pdf Page 11, Table 2. Configuration Blocks */ 29 31 enum { ··· 36 34 BLKID_MAC_CONFIG = 0x09, 37 35 BLKID_L2_LOOKUP_PARAMS = 0x0D, 38 36 BLKID_L2_FORWARDING_PARAMS = 0x0E, 37 + BLKID_AVB_PARAMS = 0x10, 39 38 BLKID_GENERAL_PARAMS = 0x11, 40 39 BLKID_XMII_PARAMS = 0x4E, 41 40 }; ··· 49 46 BLK_IDX_MAC_CONFIG, 50 47 BLK_IDX_L2_LOOKUP_PARAMS, 51 48 BLK_IDX_L2_FORWARDING_PARAMS, 49 + BLK_IDX_AVB_PARAMS, 52 50 BLK_IDX_GENERAL_PARAMS, 53 51 BLK_IDX_XMII_PARAMS, 54 52 BLK_IDX_MAX, ··· 68 64 #define SJA1105_MAX_L2_FORWARDING_PARAMS_COUNT 1 69 65 #define SJA1105_MAX_GENERAL_PARAMS_COUNT 1 70 66 #define SJA1105_MAX_XMII_PARAMS_COUNT 1 67 + #define SJA1105_MAX_AVB_PARAMS_COUNT 1 71 68 72 69 #define SJA1105_MAX_FRAME_MEMORY 929 73 70 ··· 182 177 u64 rate; 183 178 u64 maxlen; 184 179 u64 partition; 180 + }; 181 + 182 + struct sja1105_avb_params_entry { 183 + u64 destmeta; 184 + u64 srcmeta; 185 185 }; 186 186 187 187 struct sja1105_mac_config_entry {
+7 -9
include/linux/dsa/8021q.h
··· 20 20 struct sk_buff *dsa_8021q_xmit(struct sk_buff *skb, struct net_device *netdev, 21 21 u16 tpid, u16 tci); 22 22 23 - struct sk_buff *dsa_8021q_rcv(struct sk_buff *skb, struct net_device *netdev, 24 - struct packet_type *pt, u16 *tpid, u16 *tci); 25 - 26 23 u16 dsa_8021q_tx_vid(struct dsa_switch *ds, int port); 27 24 28 25 u16 dsa_8021q_rx_vid(struct dsa_switch *ds, int port); ··· 27 30 int dsa_8021q_rx_switch_id(u16 vid); 28 31 29 32 int dsa_8021q_rx_source_port(u16 vid); 33 + 34 + struct sk_buff *dsa_8021q_remove_header(struct sk_buff *skb); 30 35 31 36 #else 32 37 ··· 40 41 41 42 struct sk_buff *dsa_8021q_xmit(struct sk_buff *skb, struct net_device *netdev, 42 43 u16 tpid, u16 tci) 43 - { 44 - return NULL; 45 - } 46 - 47 - struct sk_buff *dsa_8021q_rcv(struct sk_buff *skb, struct net_device *netdev, 48 - struct packet_type *pt, u16 *tpid, u16 *tci) 49 44 { 50 45 return NULL; 51 46 } ··· 62 69 int dsa_8021q_rx_source_port(u16 vid) 63 70 { 64 71 return 0; 72 + } 73 + 74 + struct sk_buff *dsa_8021q_remove_header(struct sk_buff *skb) 75 + { 76 + return NULL; 65 77 } 66 78 67 79 #endif /* IS_ENABLED(CONFIG_NET_DSA_TAG_8021Q) */
+34
include/linux/dsa/sja1105.h
··· 12 12 #include <net/dsa.h> 13 13 14 14 #define ETH_P_SJA1105 ETH_P_DSA_8021Q 15 + #define ETH_P_SJA1105_META 0x0008 15 16 16 17 /* IEEE 802.3 Annex 57A: Slow Protocols PDUs (01:80:C2:xx:xx:xx) */ 17 18 #define SJA1105_LINKLOCAL_FILTER_A 0x0180C2000000ull ··· 21 20 #define SJA1105_LINKLOCAL_FILTER_B 0x011B19000000ull 22 21 #define SJA1105_LINKLOCAL_FILTER_B_MASK 0xFFFFFF000000ull 23 22 23 + /* Source and Destination MAC of follow-up meta frames. 24 + * Whereas the choice of SMAC only affects the unique identification of the 25 + * switch as sender of meta frames, the DMAC must be an address that is present 26 + * in the DSA master port's multicast MAC filter. 27 + * 01-80-C2-00-00-0E is a good choice for this, as all profiles of IEEE 1588 28 + * over L2 use this address for some purpose already. 29 + */ 30 + #define SJA1105_META_SMAC 0x222222222222ull 31 + #define SJA1105_META_DMAC 0x0180C200000Eull 32 + 33 + /* Global tagger data: each struct sja1105_port has a reference to 34 + * the structure defined in struct sja1105_private. 35 + */ 36 + struct sja1105_tagger_data { 37 + struct sk_buff_head skb_rxtstamp_queue; 38 + struct work_struct rxtstamp_work; 39 + struct sk_buff *stampable_skb; 40 + /* Protects concurrent access to the meta state machine 41 + * from taggers running on multiple ports on SMP systems 42 + */ 43 + spinlock_t meta_lock; 44 + bool hwts_rx_en; 45 + }; 46 + 47 + struct sja1105_skb_cb { 48 + u32 meta_tstamp; 49 + }; 50 + 51 + #define SJA1105_SKB_CB(skb) \ 52 + ((struct sja1105_skb_cb *)DSA_SKB_CB_PRIV(skb)) 53 + 24 54 struct sja1105_port { 55 + struct sja1105_tagger_data *data; 25 56 struct dsa_port *dp; 57 + bool hwts_tx_en; 26 58 int mgmt_slot; 27 59 }; 28 60
+1
include/net/dsa.h
··· 357 357 int port); 358 358 359 359 int (*setup)(struct dsa_switch *ds); 360 + void (*teardown)(struct dsa_switch *ds); 360 361 u32 (*get_phy_flags)(struct dsa_switch *ds, int port); 361 362 362 363 /*
+3
net/dsa/dsa2.c
··· 408 408 409 409 dsa_switch_unregister_notifier(ds); 410 410 411 + if (ds->ops->teardown) 412 + ds->ops->teardown(ds); 413 + 411 414 if (ds->devlink) { 412 415 devlink_unregister(ds->devlink); 413 416 devlink_free(ds->devlink);
+3
net/dsa/slave.c
··· 423 423 if (!clone) 424 424 return; 425 425 426 + DSA_SKB_CB(skb)->clone = clone; 427 + 426 428 if (ds->ops->port_txtstamp(ds, p->dp->index, clone, type)) 427 429 return; 428 430 ··· 462 460 u64_stats_update_end(&s->syncp); 463 461 464 462 DSA_SKB_CB(skb)->deferred_xmit = false; 463 + DSA_SKB_CB(skb)->clone = NULL; 465 464 466 465 /* Identify PTP protocol packets, clone them, and pass them to the 467 466 * switch driver
+37 -20
net/dsa/tag_8021q.c
··· 235 235 } 236 236 EXPORT_SYMBOL_GPL(dsa_8021q_xmit); 237 237 238 - struct sk_buff *dsa_8021q_rcv(struct sk_buff *skb, struct net_device *netdev, 239 - struct packet_type *pt, u16 *tpid, u16 *tci) 238 + /* In the DSA packet_type handler, skb->data points in the middle of the VLAN 239 + * tag, after tpid and before tci. This is because so far, ETH_HLEN 240 + * (DMAC, SMAC, EtherType) bytes were pulled. 241 + * There are 2 bytes of VLAN tag left in skb->data, and upper 242 + * layers expect the 'real' EtherType to be consumed as well. 243 + * Coincidentally, a VLAN header is also of the same size as 244 + * the number of bytes that need to be pulled. 245 + * 246 + * skb_mac_header skb->data 247 + * | | 248 + * v v 249 + * | | | | | | | | | | | | | | | | | | | 250 + * +-----------------------+-----------------------+-------+-------+-------+ 251 + * | Destination MAC | Source MAC | TPID | TCI | EType | 252 + * +-----------------------+-----------------------+-------+-------+-------+ 253 + * ^ | | 254 + * |<--VLAN_HLEN-->to <---VLAN_HLEN---> 255 + * from | 256 + * >>>>>>> v 257 + * >>>>>>> | | | | | | | | | | | | | | | 258 + * >>>>>>> +-----------------------+-----------------------+-------+ 259 + * >>>>>>> | Destination MAC | Source MAC | EType | 260 + * +-----------------------+-----------------------+-------+ 261 + * ^ ^ 262 + * (now part of | | 263 + * skb->head) skb_mac_header skb->data 264 + */ 265 + struct sk_buff *dsa_8021q_remove_header(struct sk_buff *skb) 240 266 { 241 - struct vlan_ethhdr *tag; 267 + u8 *from = skb_mac_header(skb); 268 + u8 *dest = from + VLAN_HLEN; 242 269 243 - if (unlikely(!pskb_may_pull(skb, VLAN_HLEN))) 244 - return NULL; 245 - 246 - tag = vlan_eth_hdr(skb); 247 - *tpid = ntohs(tag->h_vlan_proto); 248 - *tci = ntohs(tag->h_vlan_TCI); 249 - 250 - /* skb->data points in the middle of the VLAN tag, 251 - * after tpid and before tci. This is because so far, 252 - * ETH_HLEN (DMAC, SMAC, EtherType) bytes were pulled. 253 - * There are 2 bytes of VLAN tag left in skb->data, and upper 254 - * layers expect the 'real' EtherType to be consumed as well. 255 - * Coincidentally, a VLAN header is also of the same size as 256 - * the number of bytes that need to be pulled. 257 - */ 258 - skb_pull_rcsum(skb, VLAN_HLEN); 270 + memmove(dest, from, ETH_HLEN - VLAN_HLEN); 271 + skb_pull(skb, VLAN_HLEN); 272 + skb_push(skb, ETH_HLEN); 273 + skb_reset_mac_header(skb); 274 + skb_reset_mac_len(skb); 275 + skb_pull_rcsum(skb, ETH_HLEN); 259 276 260 277 return skb; 261 278 } 262 - EXPORT_SYMBOL_GPL(dsa_8021q_rcv); 279 + EXPORT_SYMBOL_GPL(dsa_8021q_remove_header); 263 280 264 281 static const struct dsa_device_ops dsa_8021q_netdev_ops = { 265 282 .name = "8021q",
+195 -18
net/dsa/tag_sja1105.c
··· 13 13 const struct ethhdr *hdr = eth_hdr(skb); 14 14 u64 dmac = ether_addr_to_u64(hdr->h_dest); 15 15 16 + if (ntohs(hdr->h_proto) == ETH_P_SJA1105_META) 17 + return false; 16 18 if ((dmac & SJA1105_LINKLOCAL_FILTER_A_MASK) == 17 19 SJA1105_LINKLOCAL_FILTER_A) 18 20 return true; ··· 24 22 return false; 25 23 } 26 24 25 + struct sja1105_meta { 26 + u64 tstamp; 27 + u64 dmac_byte_4; 28 + u64 dmac_byte_3; 29 + u64 source_port; 30 + u64 switch_id; 31 + }; 32 + 33 + static void sja1105_meta_unpack(const struct sk_buff *skb, 34 + struct sja1105_meta *meta) 35 + { 36 + u8 *buf = skb_mac_header(skb) + ETH_HLEN; 37 + 38 + /* UM10944.pdf section 4.2.17 AVB Parameters: 39 + * Structure of the meta-data follow-up frame. 40 + * It is in network byte order, so there are no quirks 41 + * while unpacking the meta frame. 42 + * 43 + * Also SJA1105 E/T only populates bits 23:0 of the timestamp 44 + * whereas P/Q/R/S does 32 bits. Since the structure is the 45 + * same and the E/T puts zeroes in the high-order byte, use 46 + * a unified unpacking command for both device series. 47 + */ 48 + packing(buf, &meta->tstamp, 31, 0, 4, UNPACK, 0); 49 + packing(buf + 4, &meta->dmac_byte_4, 7, 0, 1, UNPACK, 0); 50 + packing(buf + 5, &meta->dmac_byte_3, 7, 0, 1, UNPACK, 0); 51 + packing(buf + 6, &meta->source_port, 7, 0, 1, UNPACK, 0); 52 + packing(buf + 7, &meta->switch_id, 7, 0, 1, UNPACK, 0); 53 + } 54 + 55 + static inline bool sja1105_is_meta_frame(const struct sk_buff *skb) 56 + { 57 + const struct ethhdr *hdr = eth_hdr(skb); 58 + u64 smac = ether_addr_to_u64(hdr->h_source); 59 + u64 dmac = ether_addr_to_u64(hdr->h_dest); 60 + 61 + if (smac != SJA1105_META_SMAC) 62 + return false; 63 + if (dmac != SJA1105_META_DMAC) 64 + return false; 65 + if (ntohs(hdr->h_proto) != ETH_P_SJA1105_META) 66 + return false; 67 + return true; 68 + } 69 + 27 70 /* This is the first time the tagger sees the frame on RX. 28 - * Figure out if we can decode it, and if we can, annotate skb->cb with how we 29 - * plan to do that, so we don't need to check again in the rcv function. 71 + * Figure out if we can decode it. 30 72 */ 31 73 static bool sja1105_filter(const struct sk_buff *skb, struct net_device *dev) 32 74 { 75 + if (!dsa_port_is_vlan_filtering(dev->dsa_ptr)) 76 + return true; 33 77 if (sja1105_is_link_local(skb)) 34 78 return true; 35 - if (!dsa_port_is_vlan_filtering(dev->dsa_ptr)) 79 + if (sja1105_is_meta_frame(skb)) 36 80 return true; 37 81 return false; 38 82 } ··· 110 62 ((pcp << VLAN_PRIO_SHIFT) | tx_vid)); 111 63 } 112 64 65 + static void sja1105_transfer_meta(struct sk_buff *skb, 66 + const struct sja1105_meta *meta) 67 + { 68 + struct ethhdr *hdr = eth_hdr(skb); 69 + 70 + hdr->h_dest[3] = meta->dmac_byte_3; 71 + hdr->h_dest[4] = meta->dmac_byte_4; 72 + SJA1105_SKB_CB(skb)->meta_tstamp = meta->tstamp; 73 + } 74 + 75 + /* This is a simple state machine which follows the hardware mechanism of 76 + * generating RX timestamps: 77 + * 78 + * After each timestampable skb (all traffic for which send_meta1 and 79 + * send_meta0 is true, aka all MAC-filtered link-local traffic) a meta frame 80 + * containing a partial timestamp is immediately generated by the switch and 81 + * sent as a follow-up to the link-local frame on the CPU port. 82 + * 83 + * The meta frames have no unique identifier (such as sequence number) by which 84 + * one may pair them to the correct timestampable frame. 85 + * Instead, the switch has internal logic that ensures no frames are sent on 86 + * the CPU port between a link-local timestampable frame and its corresponding 87 + * meta follow-up. It also ensures strict ordering between ports (lower ports 88 + * have higher priority towards the CPU port). For this reason, a per-port 89 + * data structure is not needed/desirable. 90 + * 91 + * This function pairs the link-local frame with its partial timestamp from the 92 + * meta follow-up frame. The full timestamp will be reconstructed later in a 93 + * work queue. 94 + */ 95 + static struct sk_buff 96 + *sja1105_rcv_meta_state_machine(struct sk_buff *skb, 97 + struct sja1105_meta *meta, 98 + bool is_link_local, 99 + bool is_meta) 100 + { 101 + struct sja1105_port *sp; 102 + struct dsa_port *dp; 103 + 104 + dp = dsa_slave_to_port(skb->dev); 105 + sp = dp->priv; 106 + 107 + /* Step 1: A timestampable frame was received. 108 + * Buffer it until we get its meta frame. 109 + */ 110 + if (is_link_local && sp->data->hwts_rx_en) { 111 + spin_lock(&sp->data->meta_lock); 112 + /* Was this a link-local frame instead of the meta 113 + * that we were expecting? 114 + */ 115 + if (sp->data->stampable_skb) { 116 + dev_err_ratelimited(dp->ds->dev, 117 + "Expected meta frame, is %12llx " 118 + "in the DSA master multicast filter?\n", 119 + SJA1105_META_DMAC); 120 + } 121 + 122 + /* Hold a reference to avoid dsa_switch_rcv 123 + * from freeing the skb. 124 + */ 125 + sp->data->stampable_skb = skb_get(skb); 126 + spin_unlock(&sp->data->meta_lock); 127 + 128 + /* Tell DSA we got nothing */ 129 + return NULL; 130 + 131 + /* Step 2: The meta frame arrived. 132 + * Time to take the stampable skb out of the closet, annotate it 133 + * with the partial timestamp, and pretend that we received it 134 + * just now (basically masquerade the buffered frame as the meta 135 + * frame, which serves no further purpose). 136 + */ 137 + } else if (is_meta) { 138 + struct sk_buff *stampable_skb; 139 + 140 + spin_lock(&sp->data->meta_lock); 141 + 142 + stampable_skb = sp->data->stampable_skb; 143 + sp->data->stampable_skb = NULL; 144 + 145 + /* Was this a meta frame instead of the link-local 146 + * that we were expecting? 147 + */ 148 + if (!stampable_skb) { 149 + dev_err_ratelimited(dp->ds->dev, 150 + "Unexpected meta frame\n"); 151 + spin_unlock(&sp->data->meta_lock); 152 + return NULL; 153 + } 154 + 155 + if (stampable_skb->dev != skb->dev) { 156 + dev_err_ratelimited(dp->ds->dev, 157 + "Meta frame on wrong port\n"); 158 + spin_unlock(&sp->data->meta_lock); 159 + return NULL; 160 + } 161 + 162 + /* Free the meta frame and give DSA the buffered stampable_skb 163 + * for further processing up the network stack. 164 + */ 165 + kfree_skb(skb); 166 + 167 + skb = skb_copy(stampable_skb, GFP_ATOMIC); 168 + if (!skb) { 169 + dev_err_ratelimited(dp->ds->dev, 170 + "Failed to copy stampable skb\n"); 171 + return NULL; 172 + } 173 + sja1105_transfer_meta(skb, meta); 174 + /* The cached copy will be freed now */ 175 + skb_unref(stampable_skb); 176 + 177 + spin_unlock(&sp->data->meta_lock); 178 + } 179 + 180 + return skb; 181 + } 182 + 113 183 static struct sk_buff *sja1105_rcv(struct sk_buff *skb, 114 184 struct net_device *netdev, 115 185 struct packet_type *pt) 116 186 { 117 - struct ethhdr *hdr = eth_hdr(skb); 118 - u64 source_port, switch_id; 119 - struct sk_buff *nskb; 187 + struct sja1105_meta meta = {0}; 188 + int source_port, switch_id; 189 + struct vlan_ethhdr *hdr; 120 190 u16 tpid, vid, tci; 191 + bool is_link_local; 121 192 bool is_tagged; 193 + bool is_meta; 122 194 123 - nskb = dsa_8021q_rcv(skb, netdev, pt, &tpid, &tci); 124 - is_tagged = (nskb && tpid == ETH_P_SJA1105); 125 - 126 - skb->priority = (tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT; 127 - vid = tci & VLAN_VID_MASK; 195 + hdr = vlan_eth_hdr(skb); 196 + tpid = ntohs(hdr->h_vlan_proto); 197 + is_tagged = (tpid == ETH_P_SJA1105); 198 + is_link_local = sja1105_is_link_local(skb); 199 + is_meta = sja1105_is_meta_frame(skb); 128 200 129 201 skb->offload_fwd_mark = 1; 130 202 131 - if (sja1105_is_link_local(skb)) { 203 + if (is_tagged) { 204 + /* Normal traffic path. */ 205 + tci = ntohs(hdr->h_vlan_TCI); 206 + vid = tci & VLAN_VID_MASK; 207 + source_port = dsa_8021q_rx_source_port(vid); 208 + switch_id = dsa_8021q_rx_switch_id(vid); 209 + skb->priority = (tci & VLAN_PRIO_MASK) >> VLAN_PRIO_SHIFT; 210 + } else if (is_link_local) { 132 211 /* Management traffic path. Switch embeds the switch ID and 133 212 * port ID into bytes of the destination MAC, courtesy of 134 213 * the incl_srcpt options. ··· 265 90 /* Clear the DMAC bytes that were mangled by the switch */ 266 91 hdr->h_dest[3] = 0; 267 92 hdr->h_dest[4] = 0; 93 + } else if (is_meta) { 94 + sja1105_meta_unpack(skb, &meta); 95 + source_port = meta.source_port; 96 + switch_id = meta.switch_id; 268 97 } else { 269 - /* Normal traffic path. */ 270 - source_port = dsa_8021q_rx_source_port(vid); 271 - switch_id = dsa_8021q_rx_switch_id(vid); 98 + return NULL; 272 99 } 273 100 274 101 skb->dev = dsa_master_find_slave(netdev, switch_id, source_port); ··· 283 106 * it there, see dsa_switch_rcv: skb_push(skb, ETH_HLEN). 284 107 */ 285 108 if (is_tagged) 286 - memmove(skb->data - ETH_HLEN, skb->data - ETH_HLEN - VLAN_HLEN, 287 - ETH_HLEN - VLAN_HLEN); 109 + skb = dsa_8021q_remove_header(skb); 288 110 289 - return skb; 111 + return sja1105_rcv_meta_state_machine(skb, &meta, is_link_local, 112 + is_meta); 290 113 } 291 114 292 115 static struct dsa_device_ops sja1105_netdev_ops = {