Skip to content

Commit 85a83a8

Browse files
committed
Merge branch 'PTP-driver-refactoring-for-SJA1105-DSA'
Vladimir Oltean says: ==================== PTP driver refactoring for SJA1105 DSA This series creates a better separation between the driver core and the PTP portion. Therefore, users who are not interested in PTP can get a simpler and smaller driver by compiling it out. This is in preparation for further patches: SPI transfer timestamping, synchronizing the hardware clock (as opposed to keeping it free-running), PPS input/output, etc. ==================== Acked-by: Richard Cochran <[email protected]> Signed-off-by: David S. Miller <[email protected]>
2 parents a98d62c + 6642777 commit 85a83a8

File tree

5 files changed

+386
-341
lines changed

5 files changed

+386
-341
lines changed

drivers/net/dsa/sja1105/sja1105.h

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
#define SJA1105_AGEING_TIME_MS(ms) ((ms) / 10)
2222

2323
#include "sja1105_tas.h"
24+
#include "sja1105_ptp.h"
2425

2526
/* Keeps the different addresses between E/T and P/Q/R/S */
2627
struct sja1105_regs {
@@ -71,7 +72,8 @@ struct sja1105_info {
7172
const struct sja1105_dynamic_table_ops *dyn_ops;
7273
const struct sja1105_table_ops *static_ops;
7374
const struct sja1105_regs *regs;
74-
int (*ptp_cmd)(const void *ctx, const void *data);
75+
int (*ptp_cmd)(const struct dsa_switch *ds,
76+
const struct sja1105_ptp_cmd *cmd);
7577
int (*reset_cmd)(const void *ctx, const void *data);
7678
int (*setup_rgmii_delay)(const void *ctx, int port);
7779
/* Prototypes from include/net/dsa.h */
@@ -91,26 +93,16 @@ struct sja1105_private {
9193
struct spi_device *spidev;
9294
struct dsa_switch *ds;
9395
struct sja1105_port ports[SJA1105_NUM_PORTS];
94-
struct ptp_clock_info ptp_caps;
95-
struct ptp_clock *clock;
96-
/* The cycle counter translates the PTP timestamps (based on
97-
* a free-running counter) into a software time domain.
98-
*/
99-
struct cyclecounter tstamp_cc;
100-
struct timecounter tstamp_tc;
101-
struct delayed_work refresh_work;
102-
/* Serializes all operations on the cycle counter */
103-
struct mutex ptp_lock;
10496
/* Serializes transmission of management frames so that
10597
* the switch doesn't confuse them with one another.
10698
*/
10799
struct mutex mgmt_lock;
108100
struct sja1105_tagger_data tagger_data;
101+
struct sja1105_ptp_data ptp_data;
109102
struct sja1105_tas_data tas_data;
110103
};
111104

112105
#include "sja1105_dynamic_config.h"
113-
#include "sja1105_ptp.h"
114106

115107
struct sja1105_spi_message {
116108
u64 access;

drivers/net/dsa/sja1105/sja1105_main.c

Lines changed: 4 additions & 230 deletions
Original file line numberDiff line numberDiff line change
@@ -506,39 +506,6 @@ static int sja1105_init_l2_policing(struct sja1105_private *priv)
506506
return 0;
507507
}
508508

509-
static int sja1105_init_avb_params(struct sja1105_private *priv,
510-
bool on)
511-
{
512-
struct sja1105_avb_params_entry *avb;
513-
struct sja1105_table *table;
514-
515-
table = &priv->static_config.tables[BLK_IDX_AVB_PARAMS];
516-
517-
/* Discard previous AVB Parameters Table */
518-
if (table->entry_count) {
519-
kfree(table->entries);
520-
table->entry_count = 0;
521-
}
522-
523-
/* Configure the reception of meta frames only if requested */
524-
if (!on)
525-
return 0;
526-
527-
table->entries = kcalloc(SJA1105_MAX_AVB_PARAMS_COUNT,
528-
table->ops->unpacked_entry_size, GFP_KERNEL);
529-
if (!table->entries)
530-
return -ENOMEM;
531-
532-
table->entry_count = SJA1105_MAX_AVB_PARAMS_COUNT;
533-
534-
avb = table->entries;
535-
536-
avb->destmeta = SJA1105_META_DMAC;
537-
avb->srcmeta = SJA1105_META_SMAC;
538-
539-
return 0;
540-
}
541-
542509
static int sja1105_static_config_load(struct sja1105_private *priv,
543510
struct sja1105_dt_port *ports)
544511
{
@@ -577,9 +544,6 @@ static int sja1105_static_config_load(struct sja1105_private *priv,
577544
if (rc < 0)
578545
return rc;
579546
rc = sja1105_init_general_params(priv);
580-
if (rc < 0)
581-
return rc;
582-
rc = sja1105_init_avb_params(priv, false);
583547
if (rc < 0)
584548
return rc;
585549

@@ -1686,7 +1650,7 @@ static int sja1105_setup(struct dsa_switch *ds)
16861650
return rc;
16871651
}
16881652

1689-
rc = sja1105_ptp_clock_register(priv);
1653+
rc = sja1105_ptp_clock_register(ds);
16901654
if (rc < 0) {
16911655
dev_err(ds->dev, "Failed to register PTP clock: %d\n", rc);
16921656
return rc;
@@ -1728,9 +1692,7 @@ static void sja1105_teardown(struct dsa_switch *ds)
17281692
struct sja1105_private *priv = ds->priv;
17291693

17301694
sja1105_tas_teardown(ds);
1731-
cancel_work_sync(&priv->tagger_data.rxtstamp_work);
1732-
skb_queue_purge(&priv->tagger_data.skb_rxtstamp_queue);
1733-
sja1105_ptp_clock_unregister(priv);
1695+
sja1105_ptp_clock_unregister(ds);
17341696
sja1105_static_config_free(&priv->static_config);
17351697
}
17361698

@@ -1816,11 +1778,8 @@ static netdev_tx_t sja1105_port_deferred_xmit(struct dsa_switch *ds, int port,
18161778
{
18171779
struct sja1105_private *priv = ds->priv;
18181780
struct sja1105_port *sp = &priv->ports[port];
1819-
struct skb_shared_hwtstamps shwt = {0};
18201781
int slot = sp->mgmt_slot;
18211782
struct sk_buff *clone;
1822-
u64 now, ts;
1823-
int rc;
18241783

18251784
/* The tragic fact about the switch having 4x2 slots for installing
18261785
* management routes is that all of them except one are actually
@@ -1846,27 +1805,8 @@ static netdev_tx_t sja1105_port_deferred_xmit(struct dsa_switch *ds, int port,
18461805
if (!clone)
18471806
goto out;
18481807

1849-
skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
1850-
1851-
mutex_lock(&priv->ptp_lock);
1852-
1853-
now = priv->tstamp_cc.read(&priv->tstamp_cc);
1854-
1855-
rc = sja1105_ptpegr_ts_poll(priv, slot, &ts);
1856-
if (rc < 0) {
1857-
dev_err(ds->dev, "xmit: timed out polling for tstamp\n");
1858-
kfree_skb(clone);
1859-
goto out_unlock_ptp;
1860-
}
1861-
1862-
ts = sja1105_tstamp_reconstruct(priv, now, ts);
1863-
ts = timecounter_cyc2time(&priv->tstamp_tc, ts);
1864-
1865-
shwt.hwtstamp = ns_to_ktime(ts);
1866-
skb_complete_tx_timestamp(clone, &shwt);
1808+
sja1105_ptp_txtstamp_skb(ds, slot, clone);
18671809

1868-
out_unlock_ptp:
1869-
mutex_unlock(&priv->ptp_lock);
18701810
out:
18711811
mutex_unlock(&priv->mgmt_lock);
18721812
return NETDEV_TX_OK;
@@ -1896,170 +1836,6 @@ static int sja1105_set_ageing_time(struct dsa_switch *ds,
18961836
return sja1105_static_config_reload(priv);
18971837
}
18981838

1899-
/* Must be called only with priv->tagger_data.state bit
1900-
* SJA1105_HWTS_RX_EN cleared
1901-
*/
1902-
static int sja1105_change_rxtstamping(struct sja1105_private *priv,
1903-
bool on)
1904-
{
1905-
struct sja1105_general_params_entry *general_params;
1906-
struct sja1105_table *table;
1907-
int rc;
1908-
1909-
table = &priv->static_config.tables[BLK_IDX_GENERAL_PARAMS];
1910-
general_params = table->entries;
1911-
general_params->send_meta1 = on;
1912-
general_params->send_meta0 = on;
1913-
1914-
rc = sja1105_init_avb_params(priv, on);
1915-
if (rc < 0)
1916-
return rc;
1917-
1918-
/* Initialize the meta state machine to a known state */
1919-
if (priv->tagger_data.stampable_skb) {
1920-
kfree_skb(priv->tagger_data.stampable_skb);
1921-
priv->tagger_data.stampable_skb = NULL;
1922-
}
1923-
1924-
return sja1105_static_config_reload(priv);
1925-
}
1926-
1927-
static int sja1105_hwtstamp_set(struct dsa_switch *ds, int port,
1928-
struct ifreq *ifr)
1929-
{
1930-
struct sja1105_private *priv = ds->priv;
1931-
struct hwtstamp_config config;
1932-
bool rx_on;
1933-
int rc;
1934-
1935-
if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
1936-
return -EFAULT;
1937-
1938-
switch (config.tx_type) {
1939-
case HWTSTAMP_TX_OFF:
1940-
priv->ports[port].hwts_tx_en = false;
1941-
break;
1942-
case HWTSTAMP_TX_ON:
1943-
priv->ports[port].hwts_tx_en = true;
1944-
break;
1945-
default:
1946-
return -ERANGE;
1947-
}
1948-
1949-
switch (config.rx_filter) {
1950-
case HWTSTAMP_FILTER_NONE:
1951-
rx_on = false;
1952-
break;
1953-
default:
1954-
rx_on = true;
1955-
break;
1956-
}
1957-
1958-
if (rx_on != test_bit(SJA1105_HWTS_RX_EN, &priv->tagger_data.state)) {
1959-
clear_bit(SJA1105_HWTS_RX_EN, &priv->tagger_data.state);
1960-
1961-
rc = sja1105_change_rxtstamping(priv, rx_on);
1962-
if (rc < 0) {
1963-
dev_err(ds->dev,
1964-
"Failed to change RX timestamping: %d\n", rc);
1965-
return rc;
1966-
}
1967-
if (rx_on)
1968-
set_bit(SJA1105_HWTS_RX_EN, &priv->tagger_data.state);
1969-
}
1970-
1971-
if (copy_to_user(ifr->ifr_data, &config, sizeof(config)))
1972-
return -EFAULT;
1973-
return 0;
1974-
}
1975-
1976-
static int sja1105_hwtstamp_get(struct dsa_switch *ds, int port,
1977-
struct ifreq *ifr)
1978-
{
1979-
struct sja1105_private *priv = ds->priv;
1980-
struct hwtstamp_config config;
1981-
1982-
config.flags = 0;
1983-
if (priv->ports[port].hwts_tx_en)
1984-
config.tx_type = HWTSTAMP_TX_ON;
1985-
else
1986-
config.tx_type = HWTSTAMP_TX_OFF;
1987-
if (test_bit(SJA1105_HWTS_RX_EN, &priv->tagger_data.state))
1988-
config.rx_filter = HWTSTAMP_FILTER_PTP_V2_L2_EVENT;
1989-
else
1990-
config.rx_filter = HWTSTAMP_FILTER_NONE;
1991-
1992-
return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
1993-
-EFAULT : 0;
1994-
}
1995-
1996-
#define to_tagger(d) \
1997-
container_of((d), struct sja1105_tagger_data, rxtstamp_work)
1998-
#define to_sja1105(d) \
1999-
container_of((d), struct sja1105_private, tagger_data)
2000-
2001-
static void sja1105_rxtstamp_work(struct work_struct *work)
2002-
{
2003-
struct sja1105_tagger_data *data = to_tagger(work);
2004-
struct sja1105_private *priv = to_sja1105(data);
2005-
struct sk_buff *skb;
2006-
u64 now;
2007-
2008-
mutex_lock(&priv->ptp_lock);
2009-
2010-
while ((skb = skb_dequeue(&data->skb_rxtstamp_queue)) != NULL) {
2011-
struct skb_shared_hwtstamps *shwt = skb_hwtstamps(skb);
2012-
u64 ts;
2013-
2014-
now = priv->tstamp_cc.read(&priv->tstamp_cc);
2015-
2016-
*shwt = (struct skb_shared_hwtstamps) {0};
2017-
2018-
ts = SJA1105_SKB_CB(skb)->meta_tstamp;
2019-
ts = sja1105_tstamp_reconstruct(priv, now, ts);
2020-
ts = timecounter_cyc2time(&priv->tstamp_tc, ts);
2021-
2022-
shwt->hwtstamp = ns_to_ktime(ts);
2023-
netif_rx_ni(skb);
2024-
}
2025-
2026-
mutex_unlock(&priv->ptp_lock);
2027-
}
2028-
2029-
/* Called from dsa_skb_defer_rx_timestamp */
2030-
static bool sja1105_port_rxtstamp(struct dsa_switch *ds, int port,
2031-
struct sk_buff *skb, unsigned int type)
2032-
{
2033-
struct sja1105_private *priv = ds->priv;
2034-
struct sja1105_tagger_data *data = &priv->tagger_data;
2035-
2036-
if (!test_bit(SJA1105_HWTS_RX_EN, &data->state))
2037-
return false;
2038-
2039-
/* We need to read the full PTP clock to reconstruct the Rx
2040-
* timestamp. For that we need a sleepable context.
2041-
*/
2042-
skb_queue_tail(&data->skb_rxtstamp_queue, skb);
2043-
schedule_work(&data->rxtstamp_work);
2044-
return true;
2045-
}
2046-
2047-
/* Called from dsa_skb_tx_timestamp. This callback is just to make DSA clone
2048-
* the skb and have it available in DSA_SKB_CB in the .port_deferred_xmit
2049-
* callback, where we will timestamp it synchronously.
2050-
*/
2051-
static bool sja1105_port_txtstamp(struct dsa_switch *ds, int port,
2052-
struct sk_buff *skb, unsigned int type)
2053-
{
2054-
struct sja1105_private *priv = ds->priv;
2055-
struct sja1105_port *sp = &priv->ports[port];
2056-
2057-
if (!sp->hwts_tx_en)
2058-
return false;
2059-
2060-
return true;
2061-
}
2062-
20631839
static int sja1105_port_setup_tc(struct dsa_switch *ds, int port,
20641840
enum tc_setup_type type,
20651841
void *type_data)
@@ -2280,9 +2056,6 @@ static int sja1105_probe(struct spi_device *spi)
22802056
priv->ds = ds;
22812057

22822058
tagger_data = &priv->tagger_data;
2283-
skb_queue_head_init(&tagger_data->skb_rxtstamp_queue);
2284-
INIT_WORK(&tagger_data->rxtstamp_work, sja1105_rxtstamp_work);
2285-
spin_lock_init(&tagger_data->meta_lock);
22862059

22872060
/* Connections between dsa_port and sja1105_port */
22882061
for (i = 0; i < SJA1105_NUM_PORTS; i++) {
@@ -2292,6 +2065,7 @@ static int sja1105_probe(struct spi_device *spi)
22922065
sp->dp = &ds->ports[i];
22932066
sp->data = tagger_data;
22942067
}
2068+
mutex_init(&priv->ptp_data.lock);
22952069
mutex_init(&priv->mgmt_lock);
22962070

22972071
sja1105_tas_setup(ds);

0 commit comments

Comments
 (0)