1 /* Intel Ethernet Switch Host Interface Driver
2 * Copyright(c) 2013 - 2015 Intel Corporation.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms and conditions of the GNU General Public License,
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope it will be useful, but WITHOUT
9 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * The full GNU General Public License is included in this distribution in
14 * the file called "COPYING".
16 * Contact Information:
17 * e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
18 * Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
21 #include <linux/ptp_classify.h>
22 #include <linux/ptp_clock_kernel.h>
26 #define FM10K_TS_TX_TIMEOUT (HZ * 15)
28 void fm10k_systime_to_hwtstamp(struct fm10k_intfc *interface,
29 struct skb_shared_hwtstamps *hwtstamp,
34 read_lock_irqsave(&interface->systime_lock, flags);
35 systime += interface->ptp_adjust;
36 read_unlock_irqrestore(&interface->systime_lock, flags);
38 hwtstamp->hwtstamp = ns_to_ktime(systime);
41 static struct sk_buff *fm10k_ts_tx_skb(struct fm10k_intfc *interface,
44 struct sk_buff_head *list = &interface->ts_tx_skb_queue;
47 skb_queue_walk(list, skb) {
48 if (FM10K_CB(skb)->fi.w.dglort == dglort)
55 void fm10k_ts_tx_enqueue(struct fm10k_intfc *interface, struct sk_buff *skb)
57 struct sk_buff_head *list = &interface->ts_tx_skb_queue;
58 struct sk_buff *clone;
61 /* create clone for us to return on the Tx path */
62 clone = skb_clone_sk(skb);
66 FM10K_CB(clone)->ts_tx_timeout = jiffies + FM10K_TS_TX_TIMEOUT;
67 spin_lock_irqsave(&list->lock, flags);
69 /* attempt to locate any buffers with the same dglort,
70 * if none are present then insert skb in tail of list
72 skb = fm10k_ts_tx_skb(interface, FM10K_CB(clone)->fi.w.dglort);
74 __skb_queue_tail(list, clone);
76 spin_unlock_irqrestore(&list->lock, flags);
78 /* if list is already has one then we just free the clone */
82 skb_shinfo(clone)->tx_flags |= SKBTX_IN_PROGRESS;
85 void fm10k_ts_tx_hwtstamp(struct fm10k_intfc *interface, __le16 dglort,
88 struct skb_shared_hwtstamps shhwtstamps;
89 struct sk_buff_head *list = &interface->ts_tx_skb_queue;
93 spin_lock_irqsave(&list->lock, flags);
95 /* attempt to locate and pull the sk_buff out of the list */
96 skb = fm10k_ts_tx_skb(interface, dglort);
98 __skb_unlink(skb, list);
100 spin_unlock_irqrestore(&list->lock, flags);
102 /* if not found do nothing */
106 /* timestamp the sk_buff and return it to the socket */
107 fm10k_systime_to_hwtstamp(interface, &shhwtstamps, systime);
108 skb_complete_tx_timestamp(skb, &shhwtstamps);
111 void fm10k_ts_tx_subtask(struct fm10k_intfc *interface)
113 struct sk_buff_head *list = &interface->ts_tx_skb_queue;
114 struct sk_buff *skb, *tmp;
117 /* If we're down or resetting, just bail */
118 if (test_bit(__FM10K_DOWN, &interface->state) ||
119 test_bit(__FM10K_RESETTING, &interface->state))
122 spin_lock_irqsave(&list->lock, flags);
124 /* walk though the list and flush any expired timestamp packets */
125 skb_queue_walk_safe(list, skb, tmp) {
126 if (!time_is_after_jiffies(FM10K_CB(skb)->ts_tx_timeout))
128 __skb_unlink(skb, list);
130 interface->tx_hwtstamp_timeouts++;
133 spin_unlock_irqrestore(&list->lock, flags);
136 static u64 fm10k_systime_read(struct fm10k_intfc *interface)
138 struct fm10k_hw *hw = &interface->hw;
140 return hw->mac.ops.read_systime(hw);
143 void fm10k_ts_reset(struct fm10k_intfc *interface)
145 s64 ns = ktime_to_ns(ktime_get_real());
148 /* reinitialize the clock */
149 write_lock_irqsave(&interface->systime_lock, flags);
150 interface->ptp_adjust = fm10k_systime_read(interface) - ns;
151 write_unlock_irqrestore(&interface->systime_lock, flags);
154 void fm10k_ts_init(struct fm10k_intfc *interface)
156 /* Initialize lock protecting systime access */
157 rwlock_init(&interface->systime_lock);
159 /* Initialize skb queue for pending timestamp requests */
160 skb_queue_head_init(&interface->ts_tx_skb_queue);
162 /* reset the clock to current kernel time */
163 fm10k_ts_reset(interface);
167 * fm10k_get_ts_config - get current hardware timestamping configuration
168 * @netdev: network interface device structure
171 * This function returns the current timestamping settings. Rather than
172 * attempt to deconstruct registers to fill in the values, simply keep a copy
173 * of the old settings around, and return a copy when requested.
175 int fm10k_get_ts_config(struct net_device *netdev, struct ifreq *ifr)
177 struct fm10k_intfc *interface = netdev_priv(netdev);
178 struct hwtstamp_config *config = &interface->ts_config;
180 return copy_to_user(ifr->ifr_data, config, sizeof(*config)) ?
185 * fm10k_set_ts_config - control hardware time stamping
186 * @netdev: network interface device structure
189 * Outgoing time stamping can be enabled and disabled. Play nice and
190 * disable it when requested, although it shouldn't cause any overhead
191 * when no packet needs it. At most one packet in the queue may be
192 * marked for time stamping, otherwise it would be impossible to tell
193 * for sure to which packet the hardware time stamp belongs.
195 * Incoming time stamping has to be configured via the hardware
196 * filters. Not all combinations are supported, in particular event
197 * type has to be specified. Matching the kind of event packet is
198 * not supported, with the exception of "all V2 events regardless of
201 * Since hardware always timestamps Path delay packets when timestamping V2
202 * packets, regardless of the type specified in the register, only use V2
203 * Event mode. This more accurately tells the user what the hardware is going
206 int fm10k_set_ts_config(struct net_device *netdev, struct ifreq *ifr)
208 struct fm10k_intfc *interface = netdev_priv(netdev);
209 struct hwtstamp_config ts_config;
211 if (copy_from_user(&ts_config, ifr->ifr_data, sizeof(ts_config)))
214 /* reserved for future extensions */
218 switch (ts_config.tx_type) {
219 case HWTSTAMP_TX_OFF:
222 /* we likely need some check here to see if this is supported */
228 switch (ts_config.rx_filter) {
229 case HWTSTAMP_FILTER_NONE:
230 interface->flags &= ~FM10K_FLAG_RX_TS_ENABLED;
232 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
233 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
234 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
235 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
236 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
237 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
238 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
239 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
240 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
241 case HWTSTAMP_FILTER_PTP_V2_EVENT:
242 case HWTSTAMP_FILTER_PTP_V2_SYNC:
243 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
244 case HWTSTAMP_FILTER_ALL:
245 interface->flags |= FM10K_FLAG_RX_TS_ENABLED;
246 ts_config.rx_filter = HWTSTAMP_FILTER_ALL;
252 /* save these settings for future reference */
253 interface->ts_config = ts_config;
255 return copy_to_user(ifr->ifr_data, &ts_config, sizeof(ts_config)) ?
259 static int fm10k_ptp_adjfreq(struct ptp_clock_info *ptp, s32 ppb)
261 struct fm10k_intfc *interface;
265 interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
268 err = hw->mac.ops.adjust_systime(hw, ppb);
270 /* the only error we should see is if the value is out of range */
271 return (err == FM10K_ERR_PARAM) ? -ERANGE : err;
274 static int fm10k_ptp_adjtime(struct ptp_clock_info *ptp, s64 delta)
276 struct fm10k_intfc *interface;
279 interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
281 write_lock_irqsave(&interface->systime_lock, flags);
282 interface->ptp_adjust += delta;
283 write_unlock_irqrestore(&interface->systime_lock, flags);
288 static int fm10k_ptp_gettime(struct ptp_clock_info *ptp, struct timespec64 *ts)
290 struct fm10k_intfc *interface;
294 interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
296 read_lock_irqsave(&interface->systime_lock, flags);
297 now = fm10k_systime_read(interface) + interface->ptp_adjust;
298 read_unlock_irqrestore(&interface->systime_lock, flags);
300 *ts = ns_to_timespec64(now);
305 static int fm10k_ptp_settime(struct ptp_clock_info *ptp,
306 const struct timespec64 *ts)
308 struct fm10k_intfc *interface;
310 u64 ns = timespec64_to_ns(ts);
312 interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
314 write_lock_irqsave(&interface->systime_lock, flags);
315 interface->ptp_adjust = fm10k_systime_read(interface) - ns;
316 write_unlock_irqrestore(&interface->systime_lock, flags);
321 static int fm10k_ptp_enable(struct ptp_clock_info *ptp,
322 struct ptp_clock_request *rq,
323 int __always_unused on)
325 struct ptp_clock_time *t = &rq->perout.period;
326 struct fm10k_intfc *interface;
331 /* we can only support periodic output */
332 if (rq->type != PTP_CLK_REQ_PEROUT)
335 /* verify the requested channel is there */
336 if (rq->perout.index >= ptp->n_per_out)
339 /* we cannot enforce start time as there is no
340 * mechanism for that in the hardware, we can only control
344 /* we cannot support periods greater than 4 seconds due to reg limit */
345 if (t->sec > 4 || t->sec < 0)
348 interface = container_of(ptp, struct fm10k_intfc, ptp_caps);
351 /* we simply cannot support the operation if we don't have BAR4 */
355 /* convert to unsigned 64b ns, verify we can put it in a 32b register */
356 period = t->sec * 1000000000LL + t->nsec;
358 /* determine the minimum size for period */
359 step = 2 * (fm10k_read_reg(hw, FM10K_SYSTIME_CFG) &
360 FM10K_SYSTIME_CFG_STEP_MASK);
362 /* verify the value is in range supported by hardware */
363 if ((period && (period < step)) || (period > U32_MAX))
366 /* notify hardware of request to being sending pulses */
367 fm10k_write_sw_reg(hw, FM10K_SW_SYSTIME_PULSE(rq->perout.index),
373 static struct ptp_pin_desc fm10k_ptp_pd[2] = {
375 .name = "IEEE1588_PULSE0",
377 .func = PTP_PF_PEROUT,
381 .name = "IEEE1588_PULSE1",
383 .func = PTP_PF_PEROUT,
388 static int fm10k_ptp_verify(struct ptp_clock_info *ptp, unsigned int pin,
389 enum ptp_pin_function func, unsigned int chan)
391 /* verify the requested pin is there */
392 if (pin >= ptp->n_pins || !ptp->pin_config)
395 /* enforce locked channels, no changing them */
396 if (chan != ptp->pin_config[pin].chan)
399 /* we want to keep the functions locked as well */
400 if (func != ptp->pin_config[pin].func)
406 void fm10k_ptp_register(struct fm10k_intfc *interface)
408 struct ptp_clock_info *ptp_caps = &interface->ptp_caps;
409 struct device *dev = &interface->pdev->dev;
410 struct ptp_clock *ptp_clock;
412 snprintf(ptp_caps->name, sizeof(ptp_caps->name),
413 "%s", interface->netdev->name);
414 ptp_caps->owner = THIS_MODULE;
415 /* This math is simply the inverse of the math in
416 * fm10k_adjust_systime_pf applied to an adjustment value
417 * of 2^30 - 1 which is the maximum value of the register:
418 * max_ppb == ((2^30 - 1) * 5^9) / 2^31
420 ptp_caps->max_adj = 976562;
421 ptp_caps->adjfreq = fm10k_ptp_adjfreq;
422 ptp_caps->adjtime = fm10k_ptp_adjtime;
423 ptp_caps->gettime64 = fm10k_ptp_gettime;
424 ptp_caps->settime64 = fm10k_ptp_settime;
426 /* provide pins if BAR4 is accessible */
427 if (interface->sw_addr) {
428 /* enable periodic outputs */
429 ptp_caps->n_per_out = 2;
430 ptp_caps->enable = fm10k_ptp_enable;
432 /* enable clock pins */
433 ptp_caps->verify = fm10k_ptp_verify;
434 ptp_caps->n_pins = 2;
435 ptp_caps->pin_config = fm10k_ptp_pd;
438 ptp_clock = ptp_clock_register(ptp_caps, dev);
439 if (IS_ERR(ptp_clock)) {
441 dev_err(dev, "ptp_clock_register failed\n");
443 dev_info(dev, "registered PHC device %s\n", ptp_caps->name);
446 interface->ptp_clock = ptp_clock;
449 void fm10k_ptp_unregister(struct fm10k_intfc *interface)
451 struct ptp_clock *ptp_clock = interface->ptp_clock;
452 struct device *dev = &interface->pdev->dev;
457 interface->ptp_clock = NULL;
459 ptp_clock_unregister(ptp_clock);
460 dev_info(dev, "removed PHC %s\n", interface->ptp_caps.name);