blob: 086af4683544d70ac9f674c545da163e75f09f45 [file] [log] [blame]
Richard Cochrand339b132012-03-16 10:55:32 +00001/*
2 * PTP Hardware Clock (PHC) driver for the Intel 82576 and 82580
3 *
4 * Copyright (C) 2011 Richard Cochran <richardcochran@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20#include <linux/module.h>
21#include <linux/device.h>
22#include <linux/pci.h>
23
24#include "igb.h"
25
26#define INCVALUE_MASK 0x7fffffff
27#define ISGN 0x80000000
28
29/*
Richard Cochran7ebae812012-03-16 10:55:37 +000030 * The 82580 timesync updates the system timer every 8ns by 8ns,
31 * and this update value cannot be reprogrammed.
32 *
Richard Cochrand339b132012-03-16 10:55:32 +000033 * Neither the 82576 nor the 82580 offer registers wide enough to hold
34 * nanoseconds time values for very long. For the 82580, SYSTIM always
35 * counts nanoseconds, but the upper 24 bits are not availible. The
36 * frequency is adjusted by changing the 32 bit fractional nanoseconds
37 * register, TIMINCA.
38 *
39 * For the 82576, the SYSTIM register time unit is affect by the
40 * choice of the 24 bit TININCA:IV (incvalue) field. Five bits of this
41 * field are needed to provide the nominal 16 nanosecond period,
42 * leaving 19 bits for fractional nanoseconds.
43 *
Richard Cochran7ebae812012-03-16 10:55:37 +000044 * We scale the NIC clock cycle by a large factor so that relatively
45 * small clock corrections can be added or subtracted at each clock
46 * tick. The drawbacks of a large factor are a) that the clock
47 * register overflows more quickly (not such a big deal) and b) that
48 * the increment per tick has to fit into 24 bits. As a result we
49 * need to use a shift of 19 so we can fit a value of 16 into the
50 * TIMINCA register.
51 *
Richard Cochrand339b132012-03-16 10:55:32 +000052 *
53 * SYSTIMH SYSTIML
54 * +--------------+ +---+---+------+
55 * 82576 | 32 | | 8 | 5 | 19 |
56 * +--------------+ +---+---+------+
57 * \________ 45 bits _______/ fract
58 *
59 * +----------+---+ +--------------+
60 * 82580 | 24 | 8 | | 32 |
61 * +----------+---+ +--------------+
62 * reserved \______ 40 bits _____/
63 *
64 *
65 * The 45 bit 82576 SYSTIM overflows every
66 * 2^45 * 10^-9 / 3600 = 9.77 hours.
67 *
68 * The 40 bit 82580 SYSTIM overflows every
69 * 2^40 * 10^-9 / 60 = 18.3 minutes.
70 */
71
Matthew Vicka79f4f82012-08-10 05:40:44 +000072#define IGB_SYSTIM_OVERFLOW_PERIOD (HZ * 60 * 9)
Matthew Vick428f1f72012-12-13 07:20:34 +000073#define IGB_PTP_TX_TIMEOUT (HZ * 15)
Matthew Vicka79f4f82012-08-10 05:40:44 +000074#define INCPERIOD_82576 (1 << E1000_TIMINCA_16NS_SHIFT)
75#define INCVALUE_82576_MASK ((1 << E1000_TIMINCA_16NS_SHIFT) - 1)
76#define INCVALUE_82576 (16 << IGB_82576_TSYNC_SHIFT)
77#define IGB_NBITS_82580 40
Richard Cochrand339b132012-03-16 10:55:32 +000078
79/*
80 * SYSTIM read access for the 82576
81 */
82
Matthew Vicka79f4f82012-08-10 05:40:44 +000083static cycle_t igb_ptp_read_82576(const struct cyclecounter *cc)
Richard Cochrand339b132012-03-16 10:55:32 +000084{
Richard Cochrand339b132012-03-16 10:55:32 +000085 struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
86 struct e1000_hw *hw = &igb->hw;
Matthew Vicka79f4f82012-08-10 05:40:44 +000087 u64 val;
88 u32 lo, hi;
Richard Cochrand339b132012-03-16 10:55:32 +000089
90 lo = rd32(E1000_SYSTIML);
91 hi = rd32(E1000_SYSTIMH);
92
93 val = ((u64) hi) << 32;
94 val |= lo;
95
96 return val;
97}
98
99/*
100 * SYSTIM read access for the 82580
101 */
102
Matthew Vicka79f4f82012-08-10 05:40:44 +0000103static cycle_t igb_ptp_read_82580(const struct cyclecounter *cc)
Richard Cochrand339b132012-03-16 10:55:32 +0000104{
Richard Cochrand339b132012-03-16 10:55:32 +0000105 struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc);
106 struct e1000_hw *hw = &igb->hw;
Matthew Vicka79f4f82012-08-10 05:40:44 +0000107 u64 val;
108 u32 lo, hi, jk;
Richard Cochrand339b132012-03-16 10:55:32 +0000109
Richard Cochran7ebae812012-03-16 10:55:37 +0000110 /*
111 * The timestamp latches on lowest register read. For the 82580
112 * the lowest register is SYSTIMR instead of SYSTIML. However we only
113 * need to provide nanosecond resolution, so we just ignore it.
114 */
Richard Cochrand339b132012-03-16 10:55:32 +0000115 jk = rd32(E1000_SYSTIMR);
116 lo = rd32(E1000_SYSTIML);
117 hi = rd32(E1000_SYSTIMH);
118
119 val = ((u64) hi) << 32;
120 val |= lo;
121
122 return val;
123}
124
Matthew Vicke57b8bd2012-08-17 01:30:37 +0000125/*
126 * SYSTIM read access for I210/I211
127 */
128
129static void igb_ptp_read_i210(struct igb_adapter *adapter, struct timespec *ts)
130{
131 struct e1000_hw *hw = &adapter->hw;
132 u32 sec, nsec, jk;
133
134 /*
135 * The timestamp latches on lowest register read. For I210/I211, the
136 * lowest register is SYSTIMR. Since we only need to provide nanosecond
137 * resolution, we can ignore it.
138 */
139 jk = rd32(E1000_SYSTIMR);
140 nsec = rd32(E1000_SYSTIML);
141 sec = rd32(E1000_SYSTIMH);
142
143 ts->tv_sec = sec;
144 ts->tv_nsec = nsec;
145}
146
147static void igb_ptp_write_i210(struct igb_adapter *adapter,
148 const struct timespec *ts)
149{
150 struct e1000_hw *hw = &adapter->hw;
151
152 /*
153 * Writing the SYSTIMR register is not necessary as it only provides
154 * sub-nanosecond resolution.
155 */
156 wr32(E1000_SYSTIML, ts->tv_nsec);
157 wr32(E1000_SYSTIMH, ts->tv_sec);
158}
159
Matthew Vicka79f4f82012-08-10 05:40:44 +0000160/**
161 * igb_ptp_systim_to_hwtstamp - convert system time value to hw timestamp
162 * @adapter: board private structure
163 * @hwtstamps: timestamp structure to update
164 * @systim: unsigned 64bit system time value.
165 *
166 * We need to convert the system time value stored in the RX/TXSTMP registers
167 * into a hwtstamp which can be used by the upper level timestamping functions.
168 *
169 * The 'tmreg_lock' spinlock is used to protect the consistency of the
170 * system time value. This is needed because reading the 64 bit time
171 * value involves reading two (or three) 32 bit registers. The first
172 * read latches the value. Ditto for writing.
173 *
174 * In addition, here have extended the system time with an overflow
175 * counter in software.
176 **/
177static void igb_ptp_systim_to_hwtstamp(struct igb_adapter *adapter,
178 struct skb_shared_hwtstamps *hwtstamps,
179 u64 systim)
180{
181 unsigned long flags;
182 u64 ns;
183
184 switch (adapter->hw.mac.type) {
Matthew Vicke57b8bd2012-08-17 01:30:37 +0000185 case e1000_82576:
186 case e1000_82580:
187 case e1000_i350:
188 spin_lock_irqsave(&adapter->tmreg_lock, flags);
189
190 ns = timecounter_cyc2time(&adapter->tc, systim);
191
192 spin_unlock_irqrestore(&adapter->tmreg_lock, flags);
193
194 memset(hwtstamps, 0, sizeof(*hwtstamps));
195 hwtstamps->hwtstamp = ns_to_ktime(ns);
196 break;
Matthew Vicka79f4f82012-08-10 05:40:44 +0000197 case e1000_i210:
198 case e1000_i211:
Matthew Vicke57b8bd2012-08-17 01:30:37 +0000199 memset(hwtstamps, 0, sizeof(*hwtstamps));
200 /* Upper 32 bits contain s, lower 32 bits contain ns. */
201 hwtstamps->hwtstamp = ktime_set(systim >> 32,
202 systim & 0xFFFFFFFF);
Matthew Vicka79f4f82012-08-10 05:40:44 +0000203 break;
204 default:
Matthew Vicke57b8bd2012-08-17 01:30:37 +0000205 break;
Matthew Vicka79f4f82012-08-10 05:40:44 +0000206 }
Matthew Vicka79f4f82012-08-10 05:40:44 +0000207}
208
Richard Cochrand339b132012-03-16 10:55:32 +0000209/*
210 * PTP clock operations
211 */
212
Matthew Vicka79f4f82012-08-10 05:40:44 +0000213static int igb_ptp_adjfreq_82576(struct ptp_clock_info *ptp, s32 ppb)
Richard Cochrand339b132012-03-16 10:55:32 +0000214{
Matthew Vicka79f4f82012-08-10 05:40:44 +0000215 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
216 ptp_caps);
217 struct e1000_hw *hw = &igb->hw;
218 int neg_adj = 0;
Richard Cochrand339b132012-03-16 10:55:32 +0000219 u64 rate;
220 u32 incvalue;
Richard Cochrand339b132012-03-16 10:55:32 +0000221
222 if (ppb < 0) {
223 neg_adj = 1;
224 ppb = -ppb;
225 }
226 rate = ppb;
227 rate <<= 14;
228 rate = div_u64(rate, 1953125);
229
230 incvalue = 16 << IGB_82576_TSYNC_SHIFT;
231
232 if (neg_adj)
233 incvalue -= rate;
234 else
235 incvalue += rate;
236
237 wr32(E1000_TIMINCA, INCPERIOD_82576 | (incvalue & INCVALUE_82576_MASK));
238
239 return 0;
240}
241
Matthew Vicka79f4f82012-08-10 05:40:44 +0000242static int igb_ptp_adjfreq_82580(struct ptp_clock_info *ptp, s32 ppb)
Richard Cochrand339b132012-03-16 10:55:32 +0000243{
Matthew Vicka79f4f82012-08-10 05:40:44 +0000244 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
245 ptp_caps);
246 struct e1000_hw *hw = &igb->hw;
247 int neg_adj = 0;
Richard Cochrand339b132012-03-16 10:55:32 +0000248 u64 rate;
249 u32 inca;
Richard Cochrand339b132012-03-16 10:55:32 +0000250
251 if (ppb < 0) {
252 neg_adj = 1;
253 ppb = -ppb;
254 }
255 rate = ppb;
256 rate <<= 26;
257 rate = div_u64(rate, 1953125);
258
259 inca = rate & INCVALUE_MASK;
260 if (neg_adj)
261 inca |= ISGN;
262
263 wr32(E1000_TIMINCA, inca);
264
265 return 0;
266}
267
Matthew Vicke57b8bd2012-08-17 01:30:37 +0000268static int igb_ptp_adjtime_82576(struct ptp_clock_info *ptp, s64 delta)
Richard Cochrand339b132012-03-16 10:55:32 +0000269{
Matthew Vicka79f4f82012-08-10 05:40:44 +0000270 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
271 ptp_caps);
Richard Cochrand339b132012-03-16 10:55:32 +0000272 unsigned long flags;
Matthew Vicka79f4f82012-08-10 05:40:44 +0000273 s64 now;
Richard Cochrand339b132012-03-16 10:55:32 +0000274
275 spin_lock_irqsave(&igb->tmreg_lock, flags);
276
277 now = timecounter_read(&igb->tc);
278 now += delta;
279 timecounter_init(&igb->tc, &igb->cc, now);
280
281 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
282
283 return 0;
284}
285
Matthew Vicke57b8bd2012-08-17 01:30:37 +0000286static int igb_ptp_adjtime_i210(struct ptp_clock_info *ptp, s64 delta)
287{
288 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
289 ptp_caps);
290 unsigned long flags;
291 struct timespec now, then = ns_to_timespec(delta);
292
293 spin_lock_irqsave(&igb->tmreg_lock, flags);
294
295 igb_ptp_read_i210(igb, &now);
296 now = timespec_add(now, then);
297 igb_ptp_write_i210(igb, (const struct timespec *)&now);
298
299 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
300
301 return 0;
302}
303
304static int igb_ptp_gettime_82576(struct ptp_clock_info *ptp,
305 struct timespec *ts)
Richard Cochrand339b132012-03-16 10:55:32 +0000306{
Matthew Vicka79f4f82012-08-10 05:40:44 +0000307 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
308 ptp_caps);
309 unsigned long flags;
Richard Cochrand339b132012-03-16 10:55:32 +0000310 u64 ns;
311 u32 remainder;
Richard Cochrand339b132012-03-16 10:55:32 +0000312
313 spin_lock_irqsave(&igb->tmreg_lock, flags);
314
315 ns = timecounter_read(&igb->tc);
316
317 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
318
319 ts->tv_sec = div_u64_rem(ns, 1000000000, &remainder);
320 ts->tv_nsec = remainder;
321
322 return 0;
323}
324
Matthew Vicke57b8bd2012-08-17 01:30:37 +0000325static int igb_ptp_gettime_i210(struct ptp_clock_info *ptp,
326 struct timespec *ts)
327{
328 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
329 ptp_caps);
330 unsigned long flags;
331
332 spin_lock_irqsave(&igb->tmreg_lock, flags);
333
334 igb_ptp_read_i210(igb, ts);
335
336 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
337
338 return 0;
339}
340
341static int igb_ptp_settime_82576(struct ptp_clock_info *ptp,
342 const struct timespec *ts)
Richard Cochrand339b132012-03-16 10:55:32 +0000343{
Matthew Vicka79f4f82012-08-10 05:40:44 +0000344 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
345 ptp_caps);
Richard Cochrand339b132012-03-16 10:55:32 +0000346 unsigned long flags;
Matthew Vicka79f4f82012-08-10 05:40:44 +0000347 u64 ns;
Richard Cochrand339b132012-03-16 10:55:32 +0000348
349 ns = ts->tv_sec * 1000000000ULL;
350 ns += ts->tv_nsec;
351
352 spin_lock_irqsave(&igb->tmreg_lock, flags);
353
354 timecounter_init(&igb->tc, &igb->cc, ns);
355
356 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
357
358 return 0;
359}
360
Matthew Vicke57b8bd2012-08-17 01:30:37 +0000361static int igb_ptp_settime_i210(struct ptp_clock_info *ptp,
362 const struct timespec *ts)
363{
364 struct igb_adapter *igb = container_of(ptp, struct igb_adapter,
365 ptp_caps);
366 unsigned long flags;
367
368 spin_lock_irqsave(&igb->tmreg_lock, flags);
369
370 igb_ptp_write_i210(igb, ts);
371
372 spin_unlock_irqrestore(&igb->tmreg_lock, flags);
373
374 return 0;
375}
376
Matthew Vicka79f4f82012-08-10 05:40:44 +0000377static int igb_ptp_enable(struct ptp_clock_info *ptp,
378 struct ptp_clock_request *rq, int on)
Richard Cochrand339b132012-03-16 10:55:32 +0000379{
380 return -EOPNOTSUPP;
381}
382
Matthew Vick1f6e8172012-08-18 07:26:33 +0000383/**
384 * igb_ptp_tx_work
385 * @work: pointer to work struct
386 *
387 * This work function polls the TSYNCTXCTL valid bit to determine when a
388 * timestamp has been taken for the current stored skb.
389 */
390void igb_ptp_tx_work(struct work_struct *work)
391{
392 struct igb_adapter *adapter = container_of(work, struct igb_adapter,
393 ptp_tx_work);
394 struct e1000_hw *hw = &adapter->hw;
395 u32 tsynctxctl;
396
397 if (!adapter->ptp_tx_skb)
398 return;
399
Matthew Vick428f1f72012-12-13 07:20:34 +0000400 if (time_is_before_jiffies(adapter->ptp_tx_start +
401 IGB_PTP_TX_TIMEOUT)) {
402 dev_kfree_skb_any(adapter->ptp_tx_skb);
403 adapter->ptp_tx_skb = NULL;
404 adapter->tx_hwtstamp_timeouts++;
405 dev_warn(&adapter->pdev->dev, "clearing Tx timestamp hang");
406 return;
407 }
408
Matthew Vick1f6e8172012-08-18 07:26:33 +0000409 tsynctxctl = rd32(E1000_TSYNCTXCTL);
410 if (tsynctxctl & E1000_TSYNCTXCTL_VALID)
411 igb_ptp_tx_hwtstamp(adapter);
412 else
413 /* reschedule to check later */
414 schedule_work(&adapter->ptp_tx_work);
415}
416
Matthew Vicka79f4f82012-08-10 05:40:44 +0000417static void igb_ptp_overflow_check(struct work_struct *work)
Richard Cochrand339b132012-03-16 10:55:32 +0000418{
Richard Cochrand339b132012-03-16 10:55:32 +0000419 struct igb_adapter *igb =
Matthew Vicka79f4f82012-08-10 05:40:44 +0000420 container_of(work, struct igb_adapter, ptp_overflow_work.work);
421 struct timespec ts;
Richard Cochrand339b132012-03-16 10:55:32 +0000422
Matthew Vicke57b8bd2012-08-17 01:30:37 +0000423 igb->ptp_caps.gettime(&igb->ptp_caps, &ts);
Richard Cochrand339b132012-03-16 10:55:32 +0000424
425 pr_debug("igb overflow check at %ld.%09lu\n", ts.tv_sec, ts.tv_nsec);
426
Matthew Vicka79f4f82012-08-10 05:40:44 +0000427 schedule_delayed_work(&igb->ptp_overflow_work,
428 IGB_SYSTIM_OVERFLOW_PERIOD);
429}
430
431/**
432 * igb_ptp_tx_hwtstamp - utility function which checks for TX time stamp
Matthew Vick1f6e8172012-08-18 07:26:33 +0000433 * @adapter: Board private structure.
Matthew Vicka79f4f82012-08-10 05:40:44 +0000434 *
435 * If we were asked to do hardware stamping and such a time stamp is
436 * available, then it must have been for this skb here because we only
437 * allow only one such packet into the queue.
438 */
Matthew Vick1f6e8172012-08-18 07:26:33 +0000439void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter)
Matthew Vicka79f4f82012-08-10 05:40:44 +0000440{
Matthew Vicka79f4f82012-08-10 05:40:44 +0000441 struct e1000_hw *hw = &adapter->hw;
442 struct skb_shared_hwtstamps shhwtstamps;
443 u64 regval;
444
Matthew Vicka79f4f82012-08-10 05:40:44 +0000445 regval = rd32(E1000_TXSTMPL);
446 regval |= (u64)rd32(E1000_TXSTMPH) << 32;
447
448 igb_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval);
Matthew Vick1f6e8172012-08-18 07:26:33 +0000449 skb_tstamp_tx(adapter->ptp_tx_skb, &shhwtstamps);
450 dev_kfree_skb_any(adapter->ptp_tx_skb);
451 adapter->ptp_tx_skb = NULL;
Matthew Vicka79f4f82012-08-10 05:40:44 +0000452}
453
Alexander Duyckb5345502012-09-25 05:14:55 +0000454/**
455 * igb_ptp_rx_pktstamp - retrieve Rx per packet timestamp
456 * @q_vector: Pointer to interrupt specific structure
457 * @va: Pointer to address containing Rx buffer
458 * @skb: Buffer containing timestamp and packet
459 *
460 * This function is meant to retrieve a timestamp from the first buffer of an
461 * incoming frame. The value is stored in little endian format starting on
462 * byte 8.
463 */
464void igb_ptp_rx_pktstamp(struct igb_q_vector *q_vector,
465 unsigned char *va,
466 struct sk_buff *skb)
467{
Alexander Duyckac61d512012-10-23 00:01:04 +0000468 __le64 *regval = (__le64 *)va;
Alexander Duyckb5345502012-09-25 05:14:55 +0000469
470 /*
471 * The timestamp is recorded in little endian format.
472 * DWORD: 0 1 2 3
473 * Field: Reserved Reserved SYSTIML SYSTIMH
474 */
475 igb_ptp_systim_to_hwtstamp(q_vector->adapter, skb_hwtstamps(skb),
476 le64_to_cpu(regval[1]));
477}
478
479/**
480 * igb_ptp_rx_rgtstamp - retrieve Rx timestamp stored in register
481 * @q_vector: Pointer to interrupt specific structure
482 * @skb: Buffer containing timestamp and packet
483 *
484 * This function is meant to retrieve a timestamp from the internal registers
485 * of the adapter and store it in the skb.
486 */
487void igb_ptp_rx_rgtstamp(struct igb_q_vector *q_vector,
Matthew Vicka79f4f82012-08-10 05:40:44 +0000488 struct sk_buff *skb)
489{
490 struct igb_adapter *adapter = q_vector->adapter;
491 struct e1000_hw *hw = &adapter->hw;
492 u64 regval;
493
Matthew Vicka79f4f82012-08-10 05:40:44 +0000494 /*
495 * If this bit is set, then the RX registers contain the time stamp. No
496 * other packet will be time stamped until we read these registers, so
497 * read the registers to make them available again. Because only one
498 * packet can be time stamped at a time, we know that the register
499 * values must belong to this one here and therefore we don't need to
500 * compare any of the additional attributes stored for it.
501 *
502 * If nothing went wrong, then it should have a shared tx_flags that we
503 * can turn into a skb_shared_hwtstamps.
504 */
Alexander Duyckb5345502012-09-25 05:14:55 +0000505 if (!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID))
506 return;
Matthew Vicka79f4f82012-08-10 05:40:44 +0000507
Alexander Duyckb5345502012-09-25 05:14:55 +0000508 regval = rd32(E1000_RXSTMPL);
509 regval |= (u64)rd32(E1000_RXSTMPH) << 32;
Matthew Vicka79f4f82012-08-10 05:40:44 +0000510
511 igb_ptp_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), regval);
512}
513
514/**
515 * igb_ptp_hwtstamp_ioctl - control hardware time stamping
516 * @netdev:
517 * @ifreq:
518 * @cmd:
519 *
520 * Outgoing time stamping can be enabled and disabled. Play nice and
521 * disable it when requested, although it shouldn't case any overhead
522 * when no packet needs it. At most one packet in the queue may be
523 * marked for time stamping, otherwise it would be impossible to tell
524 * for sure to which packet the hardware time stamp belongs.
525 *
526 * Incoming time stamping has to be configured via the hardware
527 * filters. Not all combinations are supported, in particular event
528 * type has to be specified. Matching the kind of event packet is
529 * not supported, with the exception of "all V2 events regardless of
530 * level 2 or 4".
531 *
532 **/
533int igb_ptp_hwtstamp_ioctl(struct net_device *netdev,
534 struct ifreq *ifr, int cmd)
535{
536 struct igb_adapter *adapter = netdev_priv(netdev);
537 struct e1000_hw *hw = &adapter->hw;
538 struct hwtstamp_config config;
539 u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED;
540 u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
541 u32 tsync_rx_cfg = 0;
542 bool is_l4 = false;
543 bool is_l2 = false;
544 u32 regval;
545
546 if (copy_from_user(&config, ifr->ifr_data, sizeof(config)))
547 return -EFAULT;
548
549 /* reserved for future extensions */
550 if (config.flags)
551 return -EINVAL;
552
553 switch (config.tx_type) {
554 case HWTSTAMP_TX_OFF:
555 tsync_tx_ctl = 0;
556 case HWTSTAMP_TX_ON:
557 break;
558 default:
559 return -ERANGE;
560 }
561
562 switch (config.rx_filter) {
563 case HWTSTAMP_FILTER_NONE:
564 tsync_rx_ctl = 0;
565 break;
Matthew Vicka79f4f82012-08-10 05:40:44 +0000566 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC:
567 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
568 tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE;
569 is_l4 = true;
570 break;
571 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ:
572 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1;
573 tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE;
574 is_l4 = true;
575 break;
Matthew Vick3e961a02012-11-08 08:38:57 +0000576 case HWTSTAMP_FILTER_PTP_V2_EVENT:
577 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT:
578 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT:
579 case HWTSTAMP_FILTER_PTP_V2_SYNC:
Matthew Vicka79f4f82012-08-10 05:40:44 +0000580 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC:
581 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC:
Matthew Vick3e961a02012-11-08 08:38:57 +0000582 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ:
Matthew Vicka79f4f82012-08-10 05:40:44 +0000583 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ:
584 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ:
Matthew Vicka79f4f82012-08-10 05:40:44 +0000585 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2;
586 config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT;
587 is_l2 = true;
588 is_l4 = true;
589 break;
Matthew Vick3e961a02012-11-08 08:38:57 +0000590 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT:
591 case HWTSTAMP_FILTER_ALL:
592 /* 82576 cannot timestamp all packets, which it needs to do to
593 * support both V1 Sync and Delay_Req messages
594 */
595 if (hw->mac.type != e1000_82576) {
596 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
597 config.rx_filter = HWTSTAMP_FILTER_ALL;
598 break;
599 }
600 /* fall through */
Matthew Vicka79f4f82012-08-10 05:40:44 +0000601 default:
Matthew Vick3e961a02012-11-08 08:38:57 +0000602 config.rx_filter = HWTSTAMP_FILTER_NONE;
Matthew Vicka79f4f82012-08-10 05:40:44 +0000603 return -ERANGE;
604 }
605
606 if (hw->mac.type == e1000_82575) {
607 if (tsync_rx_ctl | tsync_tx_ctl)
608 return -EINVAL;
609 return 0;
610 }
611
612 /*
613 * Per-packet timestamping only works if all packets are
614 * timestamped, so enable timestamping in all packets as
615 * long as one rx filter was configured.
616 */
617 if ((hw->mac.type >= e1000_82580) && tsync_rx_ctl) {
618 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED;
619 tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL;
Matthew Vick3e961a02012-11-08 08:38:57 +0000620 config.rx_filter = HWTSTAMP_FILTER_ALL;
621 is_l2 = true;
622 is_l4 = true;
Matthew Vicke57b8bd2012-08-17 01:30:37 +0000623
624 if ((hw->mac.type == e1000_i210) ||
625 (hw->mac.type == e1000_i211)) {
626 regval = rd32(E1000_RXPBS);
627 regval |= E1000_RXPBS_CFG_TS_EN;
628 wr32(E1000_RXPBS, regval);
629 }
Matthew Vicka79f4f82012-08-10 05:40:44 +0000630 }
631
632 /* enable/disable TX */
633 regval = rd32(E1000_TSYNCTXCTL);
634 regval &= ~E1000_TSYNCTXCTL_ENABLED;
635 regval |= tsync_tx_ctl;
636 wr32(E1000_TSYNCTXCTL, regval);
637
638 /* enable/disable RX */
639 regval = rd32(E1000_TSYNCRXCTL);
640 regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK);
641 regval |= tsync_rx_ctl;
642 wr32(E1000_TSYNCRXCTL, regval);
643
644 /* define which PTP packets are time stamped */
645 wr32(E1000_TSYNCRXCFG, tsync_rx_cfg);
646
647 /* define ethertype filter for timestamped packets */
648 if (is_l2)
649 wr32(E1000_ETQF(3),
650 (E1000_ETQF_FILTER_ENABLE | /* enable filter */
651 E1000_ETQF_1588 | /* enable timestamping */
652 ETH_P_1588)); /* 1588 eth protocol type */
653 else
654 wr32(E1000_ETQF(3), 0);
655
656#define PTP_PORT 319
657 /* L4 Queue Filter[3]: filter by destination port and protocol */
658 if (is_l4) {
659 u32 ftqf = (IPPROTO_UDP /* UDP */
660 | E1000_FTQF_VF_BP /* VF not compared */
661 | E1000_FTQF_1588_TIME_STAMP /* Enable Timestamping */
662 | E1000_FTQF_MASK); /* mask all inputs */
663 ftqf &= ~E1000_FTQF_MASK_PROTO_BP; /* enable protocol check */
664
665 wr32(E1000_IMIR(3), htons(PTP_PORT));
666 wr32(E1000_IMIREXT(3),
667 (E1000_IMIREXT_SIZE_BP | E1000_IMIREXT_CTRL_BP));
668 if (hw->mac.type == e1000_82576) {
669 /* enable source port check */
670 wr32(E1000_SPQF(3), htons(PTP_PORT));
671 ftqf &= ~E1000_FTQF_MASK_SOURCE_PORT_BP;
672 }
673 wr32(E1000_FTQF(3), ftqf);
674 } else {
675 wr32(E1000_FTQF(3), E1000_FTQF_MASK);
676 }
677 wrfl();
678
679 /* clear TX/RX time stamp registers, just to be sure */
Matthew Vicke57b8bd2012-08-17 01:30:37 +0000680 regval = rd32(E1000_TXSTMPL);
Matthew Vicka79f4f82012-08-10 05:40:44 +0000681 regval = rd32(E1000_TXSTMPH);
Matthew Vicke57b8bd2012-08-17 01:30:37 +0000682 regval = rd32(E1000_RXSTMPL);
Matthew Vicka79f4f82012-08-10 05:40:44 +0000683 regval = rd32(E1000_RXSTMPH);
684
685 return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ?
686 -EFAULT : 0;
Richard Cochrand339b132012-03-16 10:55:32 +0000687}
688
689void igb_ptp_init(struct igb_adapter *adapter)
690{
691 struct e1000_hw *hw = &adapter->hw;
Matthew Vick201987e2012-08-10 05:40:46 +0000692 struct net_device *netdev = adapter->netdev;
Richard Cochrand339b132012-03-16 10:55:32 +0000693
694 switch (hw->mac.type) {
Richard Cochrand339b132012-03-16 10:55:32 +0000695 case e1000_82576:
Matthew Vick201987e2012-08-10 05:40:46 +0000696 snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
Matthew Vicka79f4f82012-08-10 05:40:44 +0000697 adapter->ptp_caps.owner = THIS_MODULE;
Matthew Vicka79f4f82012-08-10 05:40:44 +0000698 adapter->ptp_caps.max_adj = 1000000000;
699 adapter->ptp_caps.n_ext_ts = 0;
700 adapter->ptp_caps.pps = 0;
701 adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82576;
Matthew Vicke57b8bd2012-08-17 01:30:37 +0000702 adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
703 adapter->ptp_caps.gettime = igb_ptp_gettime_82576;
704 adapter->ptp_caps.settime = igb_ptp_settime_82576;
Matthew Vicka79f4f82012-08-10 05:40:44 +0000705 adapter->ptp_caps.enable = igb_ptp_enable;
706 adapter->cc.read = igb_ptp_read_82576;
707 adapter->cc.mask = CLOCKSOURCE_MASK(64);
708 adapter->cc.mult = 1;
709 adapter->cc.shift = IGB_82576_TSYNC_SHIFT;
Richard Cochrand339b132012-03-16 10:55:32 +0000710 /* Dial the nominal frequency. */
711 wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
712 break;
Matthew Vicke57b8bd2012-08-17 01:30:37 +0000713 case e1000_82580:
714 case e1000_i350:
715 snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
716 adapter->ptp_caps.owner = THIS_MODULE;
717 adapter->ptp_caps.max_adj = 62499999;
718 adapter->ptp_caps.n_ext_ts = 0;
719 adapter->ptp_caps.pps = 0;
720 adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
721 adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576;
722 adapter->ptp_caps.gettime = igb_ptp_gettime_82576;
723 adapter->ptp_caps.settime = igb_ptp_settime_82576;
724 adapter->ptp_caps.enable = igb_ptp_enable;
725 adapter->cc.read = igb_ptp_read_82580;
726 adapter->cc.mask = CLOCKSOURCE_MASK(IGB_NBITS_82580);
727 adapter->cc.mult = 1;
728 adapter->cc.shift = 0;
729 /* Enable the timer functions by clearing bit 31. */
730 wr32(E1000_TSAUXC, 0x0);
731 break;
732 case e1000_i210:
733 case e1000_i211:
734 snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr);
735 adapter->ptp_caps.owner = THIS_MODULE;
736 adapter->ptp_caps.max_adj = 62499999;
737 adapter->ptp_caps.n_ext_ts = 0;
738 adapter->ptp_caps.pps = 0;
739 adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580;
740 adapter->ptp_caps.adjtime = igb_ptp_adjtime_i210;
741 adapter->ptp_caps.gettime = igb_ptp_gettime_i210;
742 adapter->ptp_caps.settime = igb_ptp_settime_i210;
743 adapter->ptp_caps.enable = igb_ptp_enable;
744 /* Enable the timer functions by clearing bit 31. */
745 wr32(E1000_TSAUXC, 0x0);
746 break;
Richard Cochrand339b132012-03-16 10:55:32 +0000747 default:
748 adapter->ptp_clock = NULL;
749 return;
750 }
751
752 wrfl();
753
Richard Cochrand339b132012-03-16 10:55:32 +0000754 spin_lock_init(&adapter->tmreg_lock);
Matthew Vick1f6e8172012-08-18 07:26:33 +0000755 INIT_WORK(&adapter->ptp_tx_work, igb_ptp_tx_work);
756
Matthew Vicke57b8bd2012-08-17 01:30:37 +0000757 /* Initialize the clock and overflow work for devices that need it. */
758 if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
759 struct timespec ts = ktime_to_timespec(ktime_get_real());
760
761 igb_ptp_settime_i210(&adapter->ptp_caps, &ts);
762 } else {
763 timecounter_init(&adapter->tc, &adapter->cc,
764 ktime_to_ns(ktime_get_real()));
765
766 INIT_DELAYED_WORK(&adapter->ptp_overflow_work,
767 igb_ptp_overflow_check);
768
769 schedule_delayed_work(&adapter->ptp_overflow_work,
770 IGB_SYSTIM_OVERFLOW_PERIOD);
771 }
Richard Cochrand339b132012-03-16 10:55:32 +0000772
Matthew Vick1f6e8172012-08-18 07:26:33 +0000773 /* Initialize the time sync interrupts for devices that support it. */
774 if (hw->mac.type >= e1000_82580) {
775 wr32(E1000_TSIM, E1000_TSIM_TXTS);
776 wr32(E1000_IMS, E1000_IMS_TS);
777 }
778
Richard Cochran1ef76152012-09-22 07:02:03 +0000779 adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps,
780 &adapter->pdev->dev);
Richard Cochrand339b132012-03-16 10:55:32 +0000781 if (IS_ERR(adapter->ptp_clock)) {
782 adapter->ptp_clock = NULL;
783 dev_err(&adapter->pdev->dev, "ptp_clock_register failed\n");
Matthew Vick1f6e8172012-08-18 07:26:33 +0000784 } else {
Richard Cochrand339b132012-03-16 10:55:32 +0000785 dev_info(&adapter->pdev->dev, "added PHC on %s\n",
786 adapter->netdev->name);
Matthew Vick1f6e8172012-08-18 07:26:33 +0000787 adapter->flags |= IGB_FLAG_PTP;
788 }
Richard Cochrand339b132012-03-16 10:55:32 +0000789}
790
Matthew Vicka79f4f82012-08-10 05:40:44 +0000791/**
792 * igb_ptp_stop - Disable PTP device and stop the overflow check.
793 * @adapter: Board private structure.
794 *
795 * This function stops the PTP support and cancels the delayed work.
796 **/
797void igb_ptp_stop(struct igb_adapter *adapter)
Richard Cochrand339b132012-03-16 10:55:32 +0000798{
Carolyn Wybornyd3eef8c2012-05-16 01:46:00 +0000799 switch (adapter->hw.mac.type) {
Carolyn Wybornyd3eef8c2012-05-16 01:46:00 +0000800 case e1000_82576:
Matthew Vick1f6e8172012-08-18 07:26:33 +0000801 case e1000_82580:
802 case e1000_i350:
Matthew Vicka79f4f82012-08-10 05:40:44 +0000803 cancel_delayed_work_sync(&adapter->ptp_overflow_work);
Carolyn Wybornyd3eef8c2012-05-16 01:46:00 +0000804 break;
Matthew Vick1f6e8172012-08-18 07:26:33 +0000805 case e1000_i210:
806 case e1000_i211:
807 /* No delayed work to cancel. */
808 break;
Carolyn Wybornyd3eef8c2012-05-16 01:46:00 +0000809 default:
810 return;
811 }
Richard Cochrand339b132012-03-16 10:55:32 +0000812
Matthew Vick1f6e8172012-08-18 07:26:33 +0000813 cancel_work_sync(&adapter->ptp_tx_work);
814
Richard Cochrand339b132012-03-16 10:55:32 +0000815 if (adapter->ptp_clock) {
816 ptp_clock_unregister(adapter->ptp_clock);
817 dev_info(&adapter->pdev->dev, "removed PHC on %s\n",
818 adapter->netdev->name);
Matthew Vick1f6e8172012-08-18 07:26:33 +0000819 adapter->flags &= ~IGB_FLAG_PTP;
Richard Cochrand339b132012-03-16 10:55:32 +0000820 }
821}
Matthew Vick1f6e8172012-08-18 07:26:33 +0000822
823/**
824 * igb_ptp_reset - Re-enable the adapter for PTP following a reset.
825 * @adapter: Board private structure.
826 *
827 * This function handles the reset work required to re-enable the PTP device.
828 **/
829void igb_ptp_reset(struct igb_adapter *adapter)
830{
831 struct e1000_hw *hw = &adapter->hw;
832
833 if (!(adapter->flags & IGB_FLAG_PTP))
834 return;
835
836 switch (adapter->hw.mac.type) {
837 case e1000_82576:
838 /* Dial the nominal frequency. */
839 wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576);
840 break;
841 case e1000_82580:
842 case e1000_i350:
843 case e1000_i210:
844 case e1000_i211:
845 /* Enable the timer functions and interrupts. */
846 wr32(E1000_TSAUXC, 0x0);
847 wr32(E1000_TSIM, E1000_TSIM_TXTS);
848 wr32(E1000_IMS, E1000_IMS_TS);
849 break;
850 default:
851 /* No work to do. */
852 return;
853 }
854
Matthew Vicke57b8bd2012-08-17 01:30:37 +0000855 /* Re-initialize the timer. */
856 if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) {
857 struct timespec ts = ktime_to_timespec(ktime_get_real());
858
859 igb_ptp_settime_i210(&adapter->ptp_caps, &ts);
860 } else {
861 timecounter_init(&adapter->tc, &adapter->cc,
862 ktime_to_ns(ktime_get_real()));
863 }
Matthew Vick1f6e8172012-08-18 07:26:33 +0000864}