Commit | Line | Data |
---|---|---|
d339b133 RC |
1 | /* |
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 | /* | |
7ebae817 RC |
30 | * The 82580 timesync updates the system timer every 8ns by 8ns, |
31 | * and this update value cannot be reprogrammed. | |
32 | * | |
d339b133 RC |
33 | * 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 | * | |
7ebae817 RC |
44 | * 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 | * | |
d339b133 RC |
52 | * |
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 | ||
a79f4f88 | 72 | #define IGB_SYSTIM_OVERFLOW_PERIOD (HZ * 60 * 9) |
428f1f71 | 73 | #define IGB_PTP_TX_TIMEOUT (HZ * 15) |
a79f4f88 MV |
74 | #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 | |
d339b133 RC |
78 | |
79 | /* | |
80 | * SYSTIM read access for the 82576 | |
81 | */ | |
82 | ||
a79f4f88 | 83 | static cycle_t igb_ptp_read_82576(const struct cyclecounter *cc) |
d339b133 | 84 | { |
d339b133 RC |
85 | struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc); |
86 | struct e1000_hw *hw = &igb->hw; | |
a79f4f88 MV |
87 | u64 val; |
88 | u32 lo, hi; | |
d339b133 RC |
89 | |
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 | ||
a79f4f88 | 103 | static cycle_t igb_ptp_read_82580(const struct cyclecounter *cc) |
d339b133 | 104 | { |
d339b133 RC |
105 | struct igb_adapter *igb = container_of(cc, struct igb_adapter, cc); |
106 | struct e1000_hw *hw = &igb->hw; | |
a79f4f88 MV |
107 | u64 val; |
108 | u32 lo, hi, jk; | |
d339b133 | 109 | |
7ebae817 RC |
110 | /* |
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 | */ | |
d339b133 RC |
115 | 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 | ||
e57b8bdb MV |
125 | /* |
126 | * SYSTIM read access for I210/I211 | |
127 | */ | |
128 | ||
129 | static 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 | ||
147 | static 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 | ||
a79f4f88 MV |
160 | /** |
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 | **/ | |
177 | static 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) { | |
e57b8bdb MV |
185 | 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; | |
a79f4f88 MV |
197 | case e1000_i210: |
198 | case e1000_i211: | |
e57b8bdb MV |
199 | 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); | |
a79f4f88 MV |
203 | break; |
204 | default: | |
e57b8bdb | 205 | break; |
a79f4f88 | 206 | } |
a79f4f88 MV |
207 | } |
208 | ||
d339b133 RC |
209 | /* |
210 | * PTP clock operations | |
211 | */ | |
212 | ||
a79f4f88 | 213 | static int igb_ptp_adjfreq_82576(struct ptp_clock_info *ptp, s32 ppb) |
d339b133 | 214 | { |
a79f4f88 MV |
215 | 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; | |
d339b133 RC |
219 | u64 rate; |
220 | u32 incvalue; | |
d339b133 RC |
221 | |
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 | ||
a79f4f88 | 242 | static int igb_ptp_adjfreq_82580(struct ptp_clock_info *ptp, s32 ppb) |
d339b133 | 243 | { |
a79f4f88 MV |
244 | 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; | |
d339b133 RC |
248 | u64 rate; |
249 | u32 inca; | |
d339b133 RC |
250 | |
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 | ||
e57b8bdb | 268 | static int igb_ptp_adjtime_82576(struct ptp_clock_info *ptp, s64 delta) |
d339b133 | 269 | { |
a79f4f88 MV |
270 | struct igb_adapter *igb = container_of(ptp, struct igb_adapter, |
271 | ptp_caps); | |
d339b133 | 272 | unsigned long flags; |
a79f4f88 | 273 | s64 now; |
d339b133 RC |
274 | |
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 | ||
e57b8bdb MV |
286 | static 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 | ||
304 | static int igb_ptp_gettime_82576(struct ptp_clock_info *ptp, | |
305 | struct timespec *ts) | |
d339b133 | 306 | { |
a79f4f88 MV |
307 | struct igb_adapter *igb = container_of(ptp, struct igb_adapter, |
308 | ptp_caps); | |
309 | unsigned long flags; | |
d339b133 RC |
310 | u64 ns; |
311 | u32 remainder; | |
d339b133 RC |
312 | |
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 | ||
e57b8bdb MV |
325 | static 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 | ||
341 | static int igb_ptp_settime_82576(struct ptp_clock_info *ptp, | |
342 | const struct timespec *ts) | |
d339b133 | 343 | { |
a79f4f88 MV |
344 | struct igb_adapter *igb = container_of(ptp, struct igb_adapter, |
345 | ptp_caps); | |
d339b133 | 346 | unsigned long flags; |
a79f4f88 | 347 | u64 ns; |
d339b133 RC |
348 | |
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 | ||
e57b8bdb MV |
361 | static 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 | ||
a79f4f88 MV |
377 | static int igb_ptp_enable(struct ptp_clock_info *ptp, |
378 | struct ptp_clock_request *rq, int on) | |
d339b133 RC |
379 | { |
380 | return -EOPNOTSUPP; | |
381 | } | |
382 | ||
1f6e8178 MV |
383 | /** |
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 | */ | |
390 | void 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 | ||
428f1f71 MV |
400 | 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 | ||
1f6e8178 MV |
409 | 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 | ||
a79f4f88 | 417 | static void igb_ptp_overflow_check(struct work_struct *work) |
d339b133 | 418 | { |
a79f4f88 MV |
419 | struct igb_adapter *igb = |
420 | container_of(work, struct igb_adapter, ptp_overflow_work.work); | |
421 | struct timespec ts; | |
422 | ||
e57b8bdb | 423 | igb->ptp_caps.gettime(&igb->ptp_caps, &ts); |
a79f4f88 MV |
424 | |
425 | pr_debug("igb overflow check at %ld.%09lu\n", ts.tv_sec, ts.tv_nsec); | |
426 | ||
427 | schedule_delayed_work(&igb->ptp_overflow_work, | |
428 | IGB_SYSTIM_OVERFLOW_PERIOD); | |
d339b133 RC |
429 | } |
430 | ||
fc580751 MV |
431 | /** |
432 | * igb_ptp_rx_hang - detect error case when Rx timestamp registers latched | |
433 | * @adapter: private network adapter structure | |
434 | * | |
435 | * This watchdog task is scheduled to detect error case where hardware has | |
436 | * dropped an Rx packet that was timestamped when the ring is full. The | |
437 | * particular error is rare but leaves the device in a state unable to timestamp | |
438 | * any future packets. | |
439 | */ | |
440 | void igb_ptp_rx_hang(struct igb_adapter *adapter) | |
441 | { | |
442 | struct e1000_hw *hw = &adapter->hw; | |
443 | struct igb_ring *rx_ring; | |
444 | u32 tsyncrxctl = rd32(E1000_TSYNCRXCTL); | |
445 | unsigned long rx_event; | |
446 | int n; | |
447 | ||
448 | if (hw->mac.type != e1000_82576) | |
449 | return; | |
450 | ||
451 | /* If we don't have a valid timestamp in the registers, just update the | |
452 | * timeout counter and exit | |
453 | */ | |
454 | if (!(tsyncrxctl & E1000_TSYNCRXCTL_VALID)) { | |
455 | adapter->last_rx_ptp_check = jiffies; | |
456 | return; | |
457 | } | |
458 | ||
459 | /* Determine the most recent watchdog or rx_timestamp event */ | |
460 | rx_event = adapter->last_rx_ptp_check; | |
461 | for (n = 0; n < adapter->num_rx_queues; n++) { | |
462 | rx_ring = adapter->rx_ring[n]; | |
463 | if (time_after(rx_ring->last_rx_timestamp, rx_event)) | |
464 | rx_event = rx_ring->last_rx_timestamp; | |
465 | } | |
466 | ||
467 | /* Only need to read the high RXSTMP register to clear the lock */ | |
468 | if (time_is_before_jiffies(rx_event + 5 * HZ)) { | |
469 | rd32(E1000_RXSTMPH); | |
470 | adapter->last_rx_ptp_check = jiffies; | |
471 | adapter->rx_hwtstamp_cleared++; | |
472 | dev_warn(&adapter->pdev->dev, "clearing Rx timestamp hang"); | |
473 | } | |
474 | } | |
475 | ||
a79f4f88 MV |
476 | /** |
477 | * igb_ptp_tx_hwtstamp - utility function which checks for TX time stamp | |
1f6e8178 | 478 | * @adapter: Board private structure. |
a79f4f88 MV |
479 | * |
480 | * If we were asked to do hardware stamping and such a time stamp is | |
481 | * available, then it must have been for this skb here because we only | |
482 | * allow only one such packet into the queue. | |
483 | */ | |
1f6e8178 | 484 | void igb_ptp_tx_hwtstamp(struct igb_adapter *adapter) |
d339b133 | 485 | { |
a79f4f88 MV |
486 | struct e1000_hw *hw = &adapter->hw; |
487 | struct skb_shared_hwtstamps shhwtstamps; | |
488 | u64 regval; | |
d339b133 | 489 | |
a79f4f88 MV |
490 | regval = rd32(E1000_TXSTMPL); |
491 | regval |= (u64)rd32(E1000_TXSTMPH) << 32; | |
d339b133 | 492 | |
a79f4f88 | 493 | igb_ptp_systim_to_hwtstamp(adapter, &shhwtstamps, regval); |
1f6e8178 MV |
494 | skb_tstamp_tx(adapter->ptp_tx_skb, &shhwtstamps); |
495 | dev_kfree_skb_any(adapter->ptp_tx_skb); | |
496 | adapter->ptp_tx_skb = NULL; | |
a79f4f88 MV |
497 | } |
498 | ||
b534550a AD |
499 | /** |
500 | * igb_ptp_rx_pktstamp - retrieve Rx per packet timestamp | |
501 | * @q_vector: Pointer to interrupt specific structure | |
502 | * @va: Pointer to address containing Rx buffer | |
503 | * @skb: Buffer containing timestamp and packet | |
504 | * | |
505 | * This function is meant to retrieve a timestamp from the first buffer of an | |
506 | * incoming frame. The value is stored in little endian format starting on | |
507 | * byte 8. | |
508 | */ | |
509 | void igb_ptp_rx_pktstamp(struct igb_q_vector *q_vector, | |
510 | unsigned char *va, | |
511 | struct sk_buff *skb) | |
512 | { | |
ac61d515 | 513 | __le64 *regval = (__le64 *)va; |
b534550a AD |
514 | |
515 | /* | |
516 | * The timestamp is recorded in little endian format. | |
517 | * DWORD: 0 1 2 3 | |
518 | * Field: Reserved Reserved SYSTIML SYSTIMH | |
519 | */ | |
520 | igb_ptp_systim_to_hwtstamp(q_vector->adapter, skb_hwtstamps(skb), | |
521 | le64_to_cpu(regval[1])); | |
522 | } | |
523 | ||
524 | /** | |
525 | * igb_ptp_rx_rgtstamp - retrieve Rx timestamp stored in register | |
526 | * @q_vector: Pointer to interrupt specific structure | |
527 | * @skb: Buffer containing timestamp and packet | |
528 | * | |
529 | * This function is meant to retrieve a timestamp from the internal registers | |
530 | * of the adapter and store it in the skb. | |
531 | */ | |
532 | void igb_ptp_rx_rgtstamp(struct igb_q_vector *q_vector, | |
a79f4f88 MV |
533 | struct sk_buff *skb) |
534 | { | |
535 | struct igb_adapter *adapter = q_vector->adapter; | |
536 | struct e1000_hw *hw = &adapter->hw; | |
537 | u64 regval; | |
538 | ||
a79f4f88 MV |
539 | /* |
540 | * If this bit is set, then the RX registers contain the time stamp. No | |
541 | * other packet will be time stamped until we read these registers, so | |
542 | * read the registers to make them available again. Because only one | |
543 | * packet can be time stamped at a time, we know that the register | |
544 | * values must belong to this one here and therefore we don't need to | |
545 | * compare any of the additional attributes stored for it. | |
546 | * | |
547 | * If nothing went wrong, then it should have a shared tx_flags that we | |
548 | * can turn into a skb_shared_hwtstamps. | |
549 | */ | |
b534550a AD |
550 | if (!(rd32(E1000_TSYNCRXCTL) & E1000_TSYNCRXCTL_VALID)) |
551 | return; | |
552 | ||
553 | regval = rd32(E1000_RXSTMPL); | |
554 | regval |= (u64)rd32(E1000_RXSTMPH) << 32; | |
a79f4f88 MV |
555 | |
556 | igb_ptp_systim_to_hwtstamp(adapter, skb_hwtstamps(skb), regval); | |
557 | } | |
558 | ||
559 | /** | |
560 | * igb_ptp_hwtstamp_ioctl - control hardware time stamping | |
561 | * @netdev: | |
562 | * @ifreq: | |
563 | * @cmd: | |
564 | * | |
565 | * Outgoing time stamping can be enabled and disabled. Play nice and | |
566 | * disable it when requested, although it shouldn't case any overhead | |
567 | * when no packet needs it. At most one packet in the queue may be | |
568 | * marked for time stamping, otherwise it would be impossible to tell | |
569 | * for sure to which packet the hardware time stamp belongs. | |
570 | * | |
571 | * Incoming time stamping has to be configured via the hardware | |
572 | * filters. Not all combinations are supported, in particular event | |
573 | * type has to be specified. Matching the kind of event packet is | |
574 | * not supported, with the exception of "all V2 events regardless of | |
575 | * level 2 or 4". | |
576 | * | |
577 | **/ | |
578 | int igb_ptp_hwtstamp_ioctl(struct net_device *netdev, | |
579 | struct ifreq *ifr, int cmd) | |
580 | { | |
581 | struct igb_adapter *adapter = netdev_priv(netdev); | |
582 | struct e1000_hw *hw = &adapter->hw; | |
583 | struct hwtstamp_config config; | |
584 | u32 tsync_tx_ctl = E1000_TSYNCTXCTL_ENABLED; | |
585 | u32 tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED; | |
586 | u32 tsync_rx_cfg = 0; | |
587 | bool is_l4 = false; | |
588 | bool is_l2 = false; | |
589 | u32 regval; | |
590 | ||
591 | if (copy_from_user(&config, ifr->ifr_data, sizeof(config))) | |
592 | return -EFAULT; | |
593 | ||
594 | /* reserved for future extensions */ | |
595 | if (config.flags) | |
596 | return -EINVAL; | |
597 | ||
598 | switch (config.tx_type) { | |
599 | case HWTSTAMP_TX_OFF: | |
600 | tsync_tx_ctl = 0; | |
601 | case HWTSTAMP_TX_ON: | |
602 | break; | |
603 | default: | |
604 | return -ERANGE; | |
605 | } | |
606 | ||
607 | switch (config.rx_filter) { | |
608 | case HWTSTAMP_FILTER_NONE: | |
609 | tsync_rx_ctl = 0; | |
610 | break; | |
a79f4f88 MV |
611 | case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: |
612 | tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1; | |
613 | tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_SYNC_MESSAGE; | |
614 | is_l4 = true; | |
615 | break; | |
616 | case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: | |
617 | tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_L4_V1; | |
618 | tsync_rx_cfg = E1000_TSYNCRXCFG_PTP_V1_DELAY_REQ_MESSAGE; | |
619 | is_l4 = true; | |
620 | break; | |
3e961a06 MV |
621 | case HWTSTAMP_FILTER_PTP_V2_EVENT: |
622 | case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: | |
623 | case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: | |
624 | case HWTSTAMP_FILTER_PTP_V2_SYNC: | |
a79f4f88 MV |
625 | case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: |
626 | case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: | |
3e961a06 | 627 | case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: |
a79f4f88 MV |
628 | case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: |
629 | case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: | |
a79f4f88 MV |
630 | tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_EVENT_V2; |
631 | config.rx_filter = HWTSTAMP_FILTER_PTP_V2_EVENT; | |
632 | is_l2 = true; | |
633 | is_l4 = true; | |
634 | break; | |
3e961a06 MV |
635 | case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: |
636 | case HWTSTAMP_FILTER_ALL: | |
637 | /* 82576 cannot timestamp all packets, which it needs to do to | |
638 | * support both V1 Sync and Delay_Req messages | |
639 | */ | |
640 | if (hw->mac.type != e1000_82576) { | |
641 | tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL; | |
642 | config.rx_filter = HWTSTAMP_FILTER_ALL; | |
643 | break; | |
644 | } | |
645 | /* fall through */ | |
a79f4f88 | 646 | default: |
3e961a06 | 647 | config.rx_filter = HWTSTAMP_FILTER_NONE; |
a79f4f88 MV |
648 | return -ERANGE; |
649 | } | |
650 | ||
651 | if (hw->mac.type == e1000_82575) { | |
652 | if (tsync_rx_ctl | tsync_tx_ctl) | |
653 | return -EINVAL; | |
654 | return 0; | |
655 | } | |
656 | ||
657 | /* | |
658 | * Per-packet timestamping only works if all packets are | |
659 | * timestamped, so enable timestamping in all packets as | |
660 | * long as one rx filter was configured. | |
661 | */ | |
662 | if ((hw->mac.type >= e1000_82580) && tsync_rx_ctl) { | |
663 | tsync_rx_ctl = E1000_TSYNCRXCTL_ENABLED; | |
664 | tsync_rx_ctl |= E1000_TSYNCRXCTL_TYPE_ALL; | |
3e961a06 MV |
665 | config.rx_filter = HWTSTAMP_FILTER_ALL; |
666 | is_l2 = true; | |
667 | is_l4 = true; | |
e57b8bdb MV |
668 | |
669 | if ((hw->mac.type == e1000_i210) || | |
670 | (hw->mac.type == e1000_i211)) { | |
671 | regval = rd32(E1000_RXPBS); | |
672 | regval |= E1000_RXPBS_CFG_TS_EN; | |
673 | wr32(E1000_RXPBS, regval); | |
674 | } | |
a79f4f88 MV |
675 | } |
676 | ||
677 | /* enable/disable TX */ | |
678 | regval = rd32(E1000_TSYNCTXCTL); | |
679 | regval &= ~E1000_TSYNCTXCTL_ENABLED; | |
680 | regval |= tsync_tx_ctl; | |
681 | wr32(E1000_TSYNCTXCTL, regval); | |
682 | ||
683 | /* enable/disable RX */ | |
684 | regval = rd32(E1000_TSYNCRXCTL); | |
685 | regval &= ~(E1000_TSYNCRXCTL_ENABLED | E1000_TSYNCRXCTL_TYPE_MASK); | |
686 | regval |= tsync_rx_ctl; | |
687 | wr32(E1000_TSYNCRXCTL, regval); | |
688 | ||
689 | /* define which PTP packets are time stamped */ | |
690 | wr32(E1000_TSYNCRXCFG, tsync_rx_cfg); | |
691 | ||
692 | /* define ethertype filter for timestamped packets */ | |
693 | if (is_l2) | |
694 | wr32(E1000_ETQF(3), | |
695 | (E1000_ETQF_FILTER_ENABLE | /* enable filter */ | |
696 | E1000_ETQF_1588 | /* enable timestamping */ | |
697 | ETH_P_1588)); /* 1588 eth protocol type */ | |
698 | else | |
699 | wr32(E1000_ETQF(3), 0); | |
700 | ||
701 | #define PTP_PORT 319 | |
702 | /* L4 Queue Filter[3]: filter by destination port and protocol */ | |
703 | if (is_l4) { | |
704 | u32 ftqf = (IPPROTO_UDP /* UDP */ | |
705 | | E1000_FTQF_VF_BP /* VF not compared */ | |
706 | | E1000_FTQF_1588_TIME_STAMP /* Enable Timestamping */ | |
707 | | E1000_FTQF_MASK); /* mask all inputs */ | |
708 | ftqf &= ~E1000_FTQF_MASK_PROTO_BP; /* enable protocol check */ | |
709 | ||
710 | wr32(E1000_IMIR(3), htons(PTP_PORT)); | |
711 | wr32(E1000_IMIREXT(3), | |
712 | (E1000_IMIREXT_SIZE_BP | E1000_IMIREXT_CTRL_BP)); | |
713 | if (hw->mac.type == e1000_82576) { | |
714 | /* enable source port check */ | |
715 | wr32(E1000_SPQF(3), htons(PTP_PORT)); | |
716 | ftqf &= ~E1000_FTQF_MASK_SOURCE_PORT_BP; | |
717 | } | |
718 | wr32(E1000_FTQF(3), ftqf); | |
719 | } else { | |
720 | wr32(E1000_FTQF(3), E1000_FTQF_MASK); | |
721 | } | |
722 | wrfl(); | |
723 | ||
724 | /* clear TX/RX time stamp registers, just to be sure */ | |
e57b8bdb | 725 | regval = rd32(E1000_TXSTMPL); |
a79f4f88 | 726 | regval = rd32(E1000_TXSTMPH); |
e57b8bdb | 727 | regval = rd32(E1000_RXSTMPL); |
a79f4f88 MV |
728 | regval = rd32(E1000_RXSTMPH); |
729 | ||
730 | return copy_to_user(ifr->ifr_data, &config, sizeof(config)) ? | |
731 | -EFAULT : 0; | |
d339b133 RC |
732 | } |
733 | ||
734 | void igb_ptp_init(struct igb_adapter *adapter) | |
735 | { | |
736 | struct e1000_hw *hw = &adapter->hw; | |
201987e3 | 737 | struct net_device *netdev = adapter->netdev; |
d339b133 RC |
738 | |
739 | switch (hw->mac.type) { | |
e57b8bdb MV |
740 | case e1000_82576: |
741 | snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr); | |
742 | adapter->ptp_caps.owner = THIS_MODULE; | |
743 | adapter->ptp_caps.max_adj = 1000000000; | |
744 | adapter->ptp_caps.n_ext_ts = 0; | |
745 | adapter->ptp_caps.pps = 0; | |
746 | adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82576; | |
747 | adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576; | |
748 | adapter->ptp_caps.gettime = igb_ptp_gettime_82576; | |
749 | adapter->ptp_caps.settime = igb_ptp_settime_82576; | |
750 | adapter->ptp_caps.enable = igb_ptp_enable; | |
751 | adapter->cc.read = igb_ptp_read_82576; | |
752 | adapter->cc.mask = CLOCKSOURCE_MASK(64); | |
753 | adapter->cc.mult = 1; | |
754 | adapter->cc.shift = IGB_82576_TSYNC_SHIFT; | |
755 | /* Dial the nominal frequency. */ | |
756 | wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576); | |
757 | break; | |
d339b133 | 758 | case e1000_82580: |
e57b8bdb | 759 | case e1000_i350: |
201987e3 | 760 | snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr); |
a79f4f88 | 761 | adapter->ptp_caps.owner = THIS_MODULE; |
a79f4f88 MV |
762 | adapter->ptp_caps.max_adj = 62499999; |
763 | adapter->ptp_caps.n_ext_ts = 0; | |
764 | adapter->ptp_caps.pps = 0; | |
765 | adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580; | |
e57b8bdb MV |
766 | adapter->ptp_caps.adjtime = igb_ptp_adjtime_82576; |
767 | adapter->ptp_caps.gettime = igb_ptp_gettime_82576; | |
768 | adapter->ptp_caps.settime = igb_ptp_settime_82576; | |
a79f4f88 MV |
769 | adapter->ptp_caps.enable = igb_ptp_enable; |
770 | adapter->cc.read = igb_ptp_read_82580; | |
771 | adapter->cc.mask = CLOCKSOURCE_MASK(IGB_NBITS_82580); | |
772 | adapter->cc.mult = 1; | |
773 | adapter->cc.shift = 0; | |
d339b133 RC |
774 | /* Enable the timer functions by clearing bit 31. */ |
775 | wr32(E1000_TSAUXC, 0x0); | |
776 | break; | |
e57b8bdb MV |
777 | case e1000_i210: |
778 | case e1000_i211: | |
201987e3 | 779 | snprintf(adapter->ptp_caps.name, 16, "%pm", netdev->dev_addr); |
a79f4f88 | 780 | adapter->ptp_caps.owner = THIS_MODULE; |
e57b8bdb | 781 | adapter->ptp_caps.max_adj = 62499999; |
a79f4f88 MV |
782 | adapter->ptp_caps.n_ext_ts = 0; |
783 | adapter->ptp_caps.pps = 0; | |
e57b8bdb MV |
784 | adapter->ptp_caps.adjfreq = igb_ptp_adjfreq_82580; |
785 | adapter->ptp_caps.adjtime = igb_ptp_adjtime_i210; | |
786 | adapter->ptp_caps.gettime = igb_ptp_gettime_i210; | |
787 | adapter->ptp_caps.settime = igb_ptp_settime_i210; | |
a79f4f88 | 788 | adapter->ptp_caps.enable = igb_ptp_enable; |
e57b8bdb MV |
789 | /* Enable the timer functions by clearing bit 31. */ |
790 | wr32(E1000_TSAUXC, 0x0); | |
d339b133 | 791 | break; |
d339b133 RC |
792 | default: |
793 | adapter->ptp_clock = NULL; | |
794 | return; | |
795 | } | |
796 | ||
797 | wrfl(); | |
798 | ||
e57b8bdb MV |
799 | spin_lock_init(&adapter->tmreg_lock); |
800 | INIT_WORK(&adapter->ptp_tx_work, igb_ptp_tx_work); | |
d339b133 | 801 | |
e57b8bdb MV |
802 | /* Initialize the clock and overflow work for devices that need it. */ |
803 | if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) { | |
804 | struct timespec ts = ktime_to_timespec(ktime_get_real()); | |
d339b133 | 805 | |
e57b8bdb MV |
806 | igb_ptp_settime_i210(&adapter->ptp_caps, &ts); |
807 | } else { | |
808 | timecounter_init(&adapter->tc, &adapter->cc, | |
809 | ktime_to_ns(ktime_get_real())); | |
d339b133 | 810 | |
e57b8bdb MV |
811 | INIT_DELAYED_WORK(&adapter->ptp_overflow_work, |
812 | igb_ptp_overflow_check); | |
1f6e8178 | 813 | |
e57b8bdb MV |
814 | schedule_delayed_work(&adapter->ptp_overflow_work, |
815 | IGB_SYSTIM_OVERFLOW_PERIOD); | |
816 | } | |
d339b133 | 817 | |
1f6e8178 MV |
818 | /* Initialize the time sync interrupts for devices that support it. */ |
819 | if (hw->mac.type >= e1000_82580) { | |
820 | wr32(E1000_TSIM, E1000_TSIM_TXTS); | |
821 | wr32(E1000_IMS, E1000_IMS_TS); | |
822 | } | |
823 | ||
1ef76158 RC |
824 | adapter->ptp_clock = ptp_clock_register(&adapter->ptp_caps, |
825 | &adapter->pdev->dev); | |
d339b133 RC |
826 | if (IS_ERR(adapter->ptp_clock)) { |
827 | adapter->ptp_clock = NULL; | |
828 | dev_err(&adapter->pdev->dev, "ptp_clock_register failed\n"); | |
1f6e8178 | 829 | } else { |
d339b133 RC |
830 | dev_info(&adapter->pdev->dev, "added PHC on %s\n", |
831 | adapter->netdev->name); | |
1f6e8178 MV |
832 | adapter->flags |= IGB_FLAG_PTP; |
833 | } | |
d339b133 RC |
834 | } |
835 | ||
a79f4f88 MV |
836 | /** |
837 | * igb_ptp_stop - Disable PTP device and stop the overflow check. | |
838 | * @adapter: Board private structure. | |
839 | * | |
840 | * This function stops the PTP support and cancels the delayed work. | |
841 | **/ | |
842 | void igb_ptp_stop(struct igb_adapter *adapter) | |
d339b133 | 843 | { |
d3eef8c8 | 844 | switch (adapter->hw.mac.type) { |
d3eef8c8 | 845 | case e1000_82576: |
1f6e8178 MV |
846 | case e1000_82580: |
847 | case e1000_i350: | |
a79f4f88 | 848 | cancel_delayed_work_sync(&adapter->ptp_overflow_work); |
d3eef8c8 | 849 | break; |
1f6e8178 MV |
850 | case e1000_i210: |
851 | case e1000_i211: | |
852 | /* No delayed work to cancel. */ | |
853 | break; | |
d3eef8c8 CW |
854 | default: |
855 | return; | |
856 | } | |
d339b133 | 857 | |
1f6e8178 | 858 | cancel_work_sync(&adapter->ptp_tx_work); |
badc26dd MV |
859 | if (adapter->ptp_tx_skb) { |
860 | dev_kfree_skb_any(adapter->ptp_tx_skb); | |
861 | adapter->ptp_tx_skb = NULL; | |
862 | } | |
1f6e8178 | 863 | |
d339b133 RC |
864 | if (adapter->ptp_clock) { |
865 | ptp_clock_unregister(adapter->ptp_clock); | |
866 | dev_info(&adapter->pdev->dev, "removed PHC on %s\n", | |
867 | adapter->netdev->name); | |
1f6e8178 | 868 | adapter->flags &= ~IGB_FLAG_PTP; |
d339b133 RC |
869 | } |
870 | } | |
1f6e8178 MV |
871 | |
872 | /** | |
873 | * igb_ptp_reset - Re-enable the adapter for PTP following a reset. | |
874 | * @adapter: Board private structure. | |
875 | * | |
876 | * This function handles the reset work required to re-enable the PTP device. | |
877 | **/ | |
878 | void igb_ptp_reset(struct igb_adapter *adapter) | |
879 | { | |
880 | struct e1000_hw *hw = &adapter->hw; | |
881 | ||
882 | if (!(adapter->flags & IGB_FLAG_PTP)) | |
883 | return; | |
884 | ||
885 | switch (adapter->hw.mac.type) { | |
886 | case e1000_82576: | |
887 | /* Dial the nominal frequency. */ | |
888 | wr32(E1000_TIMINCA, INCPERIOD_82576 | INCVALUE_82576); | |
889 | break; | |
890 | case e1000_82580: | |
891 | case e1000_i350: | |
892 | case e1000_i210: | |
893 | case e1000_i211: | |
894 | /* Enable the timer functions and interrupts. */ | |
895 | wr32(E1000_TSAUXC, 0x0); | |
896 | wr32(E1000_TSIM, E1000_TSIM_TXTS); | |
897 | wr32(E1000_IMS, E1000_IMS_TS); | |
898 | break; | |
899 | default: | |
900 | /* No work to do. */ | |
901 | return; | |
902 | } | |
903 | ||
e57b8bdb MV |
904 | /* Re-initialize the timer. */ |
905 | if ((hw->mac.type == e1000_i210) || (hw->mac.type == e1000_i211)) { | |
906 | struct timespec ts = ktime_to_timespec(ktime_get_real()); | |
907 | ||
908 | igb_ptp_settime_i210(&adapter->ptp_caps, &ts); | |
909 | } else { | |
910 | timecounter_init(&adapter->tc, &adapter->cc, | |
911 | ktime_to_ns(ktime_get_real())); | |
912 | } | |
1f6e8178 | 913 | } |