1 From 34bfb24b8ff5af09b014ea8530e1e8d89bb2a155 Mon Sep 17 00:00:00 2001
2 From: Yangbo Lu <yangbo.lu@nxp.com>
3 Date: Wed, 20 Nov 2019 16:23:18 +0800
4 Subject: [PATCH] net: dsa: ocelot: add hardware timestamping support for Felix
6 This patch is to reuse ocelot functions as possible to enable PTP
7 clock and to support hardware timestamping on Felix.
8 On TX path, timestamping works on packet which requires timestamp.
9 The injection header will be configured accordingly, and skb clone
10 requires timestamp will be added into a list. The TX timestamp
11 is final handled in threaded interrupt handler when PTP timestamp
13 On RX path, timestamping is always working. The RX timestamp could
14 be got from extraction header.
16 Signed-off-by: Yangbo Lu <yangbo.lu@nxp.com>
17 Signed-off-by: David S. Miller <davem@davemloft.net>
19 drivers/net/dsa/ocelot/felix.c | 89 ++++++++++++++++++++++++++++++++++++++++++
20 net/dsa/tag_ocelot.c | 14 ++++++-
21 2 files changed, 102 insertions(+), 1 deletion(-)
23 --- a/drivers/net/dsa/ocelot/felix.c
24 +++ b/drivers/net/dsa/ocelot/felix.c
27 #include <uapi/linux/if_bridge.h>
28 #include <soc/mscc/ocelot.h>
29 +#include <linux/packing.h>
30 #include <linux/module.h>
31 #include <linux/pci.h>
33 @@ -303,6 +304,62 @@ static void felix_teardown(struct dsa_sw
34 ocelot_deinit(ocelot);
37 +static int felix_hwtstamp_get(struct dsa_switch *ds, int port,
40 + struct ocelot *ocelot = ds->priv;
42 + return ocelot_hwstamp_get(ocelot, port, ifr);
45 +static int felix_hwtstamp_set(struct dsa_switch *ds, int port,
48 + struct ocelot *ocelot = ds->priv;
50 + return ocelot_hwstamp_set(ocelot, port, ifr);
53 +static bool felix_rxtstamp(struct dsa_switch *ds, int port,
54 + struct sk_buff *skb, unsigned int type)
56 + struct skb_shared_hwtstamps *shhwtstamps;
57 + struct ocelot *ocelot = ds->priv;
58 + u8 *extraction = skb->data - ETH_HLEN - OCELOT_TAG_LEN;
59 + u32 tstamp_lo, tstamp_hi;
60 + struct timespec64 ts;
63 + ocelot_ptp_gettime64(&ocelot->ptp_info, &ts);
64 + tstamp = ktime_set(ts.tv_sec, ts.tv_nsec);
66 + packing(extraction, &val, 116, 85, OCELOT_TAG_LEN, UNPACK, 0);
67 + tstamp_lo = (u32)val;
69 + tstamp_hi = tstamp >> 32;
70 + if ((tstamp & 0xffffffff) < tstamp_lo)
73 + tstamp = ((u64)tstamp_hi << 32) | tstamp_lo;
75 + shhwtstamps = skb_hwtstamps(skb);
76 + memset(shhwtstamps, 0, sizeof(struct skb_shared_hwtstamps));
77 + shhwtstamps->hwtstamp = tstamp;
81 +bool felix_txtstamp(struct dsa_switch *ds, int port,
82 + struct sk_buff *clone, unsigned int type)
84 + struct ocelot *ocelot = ds->priv;
85 + struct ocelot_port *ocelot_port = ocelot->ports[port];
87 + if (!ocelot_port_add_txtstamp_skb(ocelot_port, clone))
93 static const struct dsa_switch_ops felix_switch_ops = {
94 .get_tag_protocol = felix_get_tag_protocol,
96 @@ -325,12 +382,33 @@ static const struct dsa_switch_ops felix
97 .port_vlan_filtering = felix_vlan_filtering,
98 .port_vlan_add = felix_vlan_add,
99 .port_vlan_del = felix_vlan_del,
100 + .port_hwtstamp_get = felix_hwtstamp_get,
101 + .port_hwtstamp_set = felix_hwtstamp_set,
102 + .port_rxtstamp = felix_rxtstamp,
103 + .port_txtstamp = felix_txtstamp,
106 static struct felix_info *felix_instance_tbl[] = {
107 [FELIX_INSTANCE_VSC9959] = &felix_info_vsc9959,
110 +static irqreturn_t felix_irq_handler(int irq, void *data)
112 + struct ocelot *ocelot = (struct ocelot *)data;
114 + /* The INTB interrupt is used for both PTP TX timestamp interrupt
115 + * and preemption status change interrupt on each port.
117 + * - Get txtstamp if have
118 + * - TODO: handle preemption. Without handling it, driver may get
122 + ocelot_get_txtstamp(ocelot);
124 + return IRQ_HANDLED;
127 static int felix_pci_probe(struct pci_dev *pdev,
128 const struct pci_device_id *id)
130 @@ -372,6 +450,16 @@ static int felix_pci_probe(struct pci_de
132 pci_set_master(pdev);
134 + err = devm_request_threaded_irq(&pdev->dev, pdev->irq, NULL,
135 + &felix_irq_handler, IRQF_ONESHOT,
136 + "felix-intb", ocelot);
138 + dev_err(&pdev->dev, "Failed to request irq\n");
139 + goto err_alloc_irq;
144 ds = kzalloc(sizeof(struct dsa_switch), GFP_KERNEL);
147 @@ -396,6 +484,7 @@ static int felix_pci_probe(struct pci_de
155 --- a/net/dsa/tag_ocelot.c
156 +++ b/net/dsa/tag_ocelot.c
157 @@ -137,9 +137,11 @@ static struct sk_buff *ocelot_xmit(struc
158 struct net_device *netdev)
160 struct dsa_port *dp = dsa_slave_to_port(netdev);
161 - u64 bypass, dest, src, qos_class;
162 + u64 bypass, dest, src, qos_class, rew_op;
163 struct dsa_switch *ds = dp->ds;
164 int port = dp->index;
165 + struct ocelot *ocelot = ds->priv;
166 + struct ocelot_port *ocelot_port = ocelot->ports[port];
169 if (unlikely(skb_cow_head(skb, OCELOT_TAG_LEN) < 0)) {
170 @@ -161,6 +163,16 @@ static struct sk_buff *ocelot_xmit(struc
171 packing(injection, &src, 46, 43, OCELOT_TAG_LEN, PACK, 0);
172 packing(injection, &qos_class, 19, 17, OCELOT_TAG_LEN, PACK, 0);
174 + if (ocelot->ptp && (skb_shinfo(skb)->tx_flags & SKBTX_HW_TSTAMP)) {
175 + rew_op = ocelot_port->ptp_cmd;
176 + if (ocelot_port->ptp_cmd == IFH_REW_OP_TWO_STEP_PTP) {
177 + rew_op |= (ocelot_port->ts_id % 4) << 3;
178 + ocelot_port->ts_id++;
181 + packing(injection, &rew_op, 125, 117, OCELOT_TAG_LEN, PACK, 0);