blob: 1871ebfdb793f5064cd0cbaf28953635908306ec [file] [log] [blame]
Sudarsana Reddy Kalluruc78c70f2017-02-15 10:24:10 +02001/* QLogic qed NIC Driver
2 * Copyright (c) 2015-2017 QLogic Corporation
3 *
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
9 *
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
12 * conditions are met:
13 *
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
16 * disclaimer.
17 *
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and /or other materials
21 * provided with the distribution.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30 * SOFTWARE.
31 */
32#include <linux/types.h>
33#include "qed.h"
34#include "qed_dev_api.h"
35#include "qed_hw.h"
36#include "qed_l2.h"
sudarsana.kalluru@cavium.comdb82f702017-04-26 09:00:50 -070037#include "qed_mcp.h"
Sudarsana Reddy Kalluruc78c70f2017-02-15 10:24:10 +020038#include "qed_reg_addr.h"
39
40/* 16 nano second time quantas to wait before making a Drift adjustment */
41#define QED_DRIFT_CNTR_TIME_QUANTA_SHIFT 0
42/* Nano seconds to add/subtract when making a Drift adjustment */
43#define QED_DRIFT_CNTR_ADJUSTMENT_SHIFT 28
44/* Add/subtract the Adjustment_Value when making a Drift adjustment */
45#define QED_DRIFT_CNTR_DIRECTION_SHIFT 31
46#define QED_TIMESTAMP_MASK BIT(16)
47
sudarsana.kalluru@cavium.comdb82f702017-04-26 09:00:50 -070048static enum qed_resc_lock qed_ptcdev_to_resc(struct qed_hwfn *p_hwfn)
49{
50 switch (qed_device_get_port_id(p_hwfn->cdev)) {
51 case 0:
52 return QED_RESC_LOCK_PTP_PORT0;
53 case 1:
54 return QED_RESC_LOCK_PTP_PORT1;
55 case 2:
56 return QED_RESC_LOCK_PTP_PORT2;
57 case 3:
58 return QED_RESC_LOCK_PTP_PORT3;
59 default:
60 return QED_RESC_LOCK_RESC_INVALID;
61 }
62}
63
64static int qed_ptp_res_lock(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
65{
66 struct qed_resc_lock_params params;
67 enum qed_resc_lock resource;
68 int rc;
69
70 resource = qed_ptcdev_to_resc(p_hwfn);
71 if (resource == QED_RESC_LOCK_RESC_INVALID)
72 return -EINVAL;
73
74 qed_mcp_resc_lock_default_init(&params, NULL, resource, true);
75
76 rc = qed_mcp_resc_lock(p_hwfn, p_ptt, &params);
77 if (rc && rc != -EINVAL) {
78 return rc;
79 } else if (rc == -EINVAL) {
80 /* MFW doesn't support resource locking, first PF on the port
81 * has lock ownership.
82 */
83 if (p_hwfn->abs_pf_id < p_hwfn->cdev->num_ports_in_engines)
84 return 0;
85
86 DP_INFO(p_hwfn, "PF doesn't have lock ownership\n");
87 return -EBUSY;
88 } else if (!rc && !params.b_granted) {
89 DP_INFO(p_hwfn, "Failed to acquire ptp resource lock\n");
90 return -EBUSY;
91 }
92
93 return rc;
94}
95
96static int qed_ptp_res_unlock(struct qed_hwfn *p_hwfn, struct qed_ptt *p_ptt)
97{
98 struct qed_resc_unlock_params params;
99 enum qed_resc_lock resource;
100 int rc;
101
102 resource = qed_ptcdev_to_resc(p_hwfn);
103 if (resource == QED_RESC_LOCK_RESC_INVALID)
104 return -EINVAL;
105
106 qed_mcp_resc_lock_default_init(NULL, &params, resource, true);
107
108 rc = qed_mcp_resc_unlock(p_hwfn, p_ptt, &params);
109 if (rc == -EINVAL) {
110 /* MFW doesn't support locking, first PF has lock ownership */
111 if (p_hwfn->abs_pf_id < p_hwfn->cdev->num_ports_in_engines) {
112 rc = 0;
113 } else {
114 DP_INFO(p_hwfn, "PF doesn't have lock ownership\n");
115 return -EINVAL;
116 }
117 } else if (rc) {
118 DP_INFO(p_hwfn, "Failed to release the ptp resource lock\n");
119 }
120
121 return rc;
122}
123
Sudarsana Reddy Kalluruc78c70f2017-02-15 10:24:10 +0200124/* Read Rx timestamp */
125static int qed_ptp_hw_read_rx_ts(struct qed_dev *cdev, u64 *timestamp)
126{
127 struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
128 struct qed_ptt *p_ptt = p_hwfn->p_ptp_ptt;
129 u32 val;
130
131 *timestamp = 0;
132 val = qed_rd(p_hwfn, p_ptt, NIG_REG_LLH_PTP_HOST_BUF_SEQID);
133 if (!(val & QED_TIMESTAMP_MASK)) {
134 DP_INFO(p_hwfn, "Invalid Rx timestamp, buf_seqid = %d\n", val);
135 return -EINVAL;
136 }
137
138 val = qed_rd(p_hwfn, p_ptt, NIG_REG_LLH_PTP_HOST_BUF_TS_LSB);
139 *timestamp = qed_rd(p_hwfn, p_ptt, NIG_REG_LLH_PTP_HOST_BUF_TS_MSB);
140 *timestamp <<= 32;
141 *timestamp |= val;
142
143 /* Reset timestamp register to allow new timestamp */
144 qed_wr(p_hwfn, p_ptt, NIG_REG_LLH_PTP_HOST_BUF_SEQID,
145 QED_TIMESTAMP_MASK);
146
147 return 0;
148}
149
150/* Read Tx timestamp */
151static int qed_ptp_hw_read_tx_ts(struct qed_dev *cdev, u64 *timestamp)
152{
153 struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
154 struct qed_ptt *p_ptt = p_hwfn->p_ptp_ptt;
155 u32 val;
156
157 *timestamp = 0;
158 val = qed_rd(p_hwfn, p_ptt, NIG_REG_TX_LLH_PTP_BUF_SEQID);
159 if (!(val & QED_TIMESTAMP_MASK)) {
160 DP_INFO(p_hwfn, "Invalid Tx timestamp, buf_seqid = %d\n", val);
161 return -EINVAL;
162 }
163
164 val = qed_rd(p_hwfn, p_ptt, NIG_REG_TX_LLH_PTP_BUF_TS_LSB);
165 *timestamp = qed_rd(p_hwfn, p_ptt, NIG_REG_TX_LLH_PTP_BUF_TS_MSB);
166 *timestamp <<= 32;
167 *timestamp |= val;
168
169 /* Reset timestamp register to allow new timestamp */
170 qed_wr(p_hwfn, p_ptt, NIG_REG_TX_LLH_PTP_BUF_SEQID, QED_TIMESTAMP_MASK);
171
172 return 0;
173}
174
175/* Read Phy Hardware Clock */
176static int qed_ptp_hw_read_cc(struct qed_dev *cdev, u64 *phc_cycles)
177{
178 struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
179 struct qed_ptt *p_ptt = p_hwfn->p_ptp_ptt;
180 u32 temp = 0;
181
182 temp = qed_rd(p_hwfn, p_ptt, NIG_REG_TSGEN_SYNC_TIME_LSB);
183 *phc_cycles = qed_rd(p_hwfn, p_ptt, NIG_REG_TSGEN_SYNC_TIME_MSB);
184 *phc_cycles <<= 32;
185 *phc_cycles |= temp;
186
187 return 0;
188}
189
190/* Filter PTP protocol packets that need to be timestamped */
191static int qed_ptp_hw_cfg_rx_filters(struct qed_dev *cdev,
192 enum qed_ptp_filter_type type)
193{
194 struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
195 struct qed_ptt *p_ptt = p_hwfn->p_ptp_ptt;
196 u32 rule_mask, parm_mask;
197
198 switch (type) {
199 case QED_PTP_FILTER_L2_IPV4_IPV6:
200 parm_mask = 0x6AA;
201 rule_mask = 0x3EEE;
202 break;
203 case QED_PTP_FILTER_L2:
204 parm_mask = 0x6BF;
205 rule_mask = 0x3EFF;
206 break;
207 case QED_PTP_FILTER_IPV4_IPV6:
208 parm_mask = 0x7EA;
209 rule_mask = 0x3FFE;
210 break;
211 case QED_PTP_FILTER_IPV4:
212 parm_mask = 0x7EE;
213 rule_mask = 0x3FFE;
214 break;
215 default:
216 DP_INFO(p_hwfn, "Invalid PTP filter type %d\n", type);
217 return -EINVAL;
218 }
219
220 qed_wr(p_hwfn, p_ptt, NIG_REG_LLH_PTP_PARAM_MASK, parm_mask);
221 qed_wr(p_hwfn, p_ptt, NIG_REG_LLH_PTP_RULE_MASK, rule_mask);
222
223 qed_wr(p_hwfn, p_ptt, NIG_REG_LLH_PTP_TO_HOST, 0x1);
224
225 /* Reset possibly old timestamps */
226 qed_wr(p_hwfn, p_ptt, NIG_REG_LLH_PTP_HOST_BUF_SEQID,
227 QED_TIMESTAMP_MASK);
228
229 return 0;
230}
231
232/* Adjust the HW clock by a rate given in parts-per-billion (ppb) units.
233 * FW/HW accepts the adjustment value in terms of 3 parameters:
234 * Drift period - adjustment happens once in certain number of nano seconds.
235 * Drift value - time is adjusted by a certain value, for example by 5 ns.
236 * Drift direction - add or subtract the adjustment value.
237 * The routine translates ppb into the adjustment triplet in an optimal manner.
238 */
239static int qed_ptp_hw_adjfreq(struct qed_dev *cdev, s32 ppb)
240{
241 s64 best_val = 0, val, best_period = 0, period, approx_dev, dif, dif2;
242 struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
243 struct qed_ptt *p_ptt = p_hwfn->p_ptp_ptt;
244 u32 drift_ctr_cfg = 0, drift_state;
245 int drift_dir = 1;
246
247 if (ppb < 0) {
248 ppb = -ppb;
249 drift_dir = 0;
250 }
251
252 if (ppb > 1) {
253 s64 best_dif = ppb, best_approx_dev = 1;
254
255 /* Adjustment value is up to +/-7ns, find an optimal value in
256 * this range.
257 */
258 for (val = 7; val > 0; val--) {
259 period = div_s64(val * 1000000000, ppb);
260 period -= 8;
261 period >>= 4;
262 if (period < 1)
263 period = 1;
264 if (period > 0xFFFFFFE)
265 period = 0xFFFFFFE;
266
267 /* Check both rounding ends for approximate error */
268 approx_dev = period * 16 + 8;
269 dif = ppb * approx_dev - val * 1000000000;
270 dif2 = dif + 16 * ppb;
271
272 if (dif < 0)
273 dif = -dif;
274 if (dif2 < 0)
275 dif2 = -dif2;
276
277 /* Determine which end gives better approximation */
278 if (dif * (approx_dev + 16) > dif2 * approx_dev) {
279 period++;
280 approx_dev += 16;
281 dif = dif2;
282 }
283
284 /* Track best approximation found so far */
285 if (best_dif * approx_dev > dif * best_approx_dev) {
286 best_dif = dif;
287 best_val = val;
288 best_period = period;
289 best_approx_dev = approx_dev;
290 }
291 }
292 } else if (ppb == 1) {
293 /* This is a special case as its the only value which wouldn't
294 * fit in a s64 variable. In order to prevent castings simple
295 * handle it seperately.
296 */
297 best_val = 4;
298 best_period = 0xee6b27f;
299 } else {
300 best_val = 0;
301 best_period = 0xFFFFFFF;
302 }
303
304 drift_ctr_cfg = (best_period << QED_DRIFT_CNTR_TIME_QUANTA_SHIFT) |
305 (((int)best_val) << QED_DRIFT_CNTR_ADJUSTMENT_SHIFT) |
306 (((int)drift_dir) << QED_DRIFT_CNTR_DIRECTION_SHIFT);
307
308 qed_wr(p_hwfn, p_ptt, NIG_REG_TSGEN_RST_DRIFT_CNTR, 0x1);
309
310 drift_state = qed_rd(p_hwfn, p_ptt, NIG_REG_TSGEN_RST_DRIFT_CNTR);
311 if (drift_state & 1) {
312 qed_wr(p_hwfn, p_ptt, NIG_REG_TSGEN_DRIFT_CNTR_CONF,
313 drift_ctr_cfg);
314 } else {
315 DP_INFO(p_hwfn, "Drift counter is not reset\n");
316 return -EINVAL;
317 }
318
319 qed_wr(p_hwfn, p_ptt, NIG_REG_TSGEN_RST_DRIFT_CNTR, 0x0);
320
321 return 0;
322}
323
324static int qed_ptp_hw_enable(struct qed_dev *cdev)
325{
326 struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
sudarsana.kalluru@cavium.comd179bd12017-04-26 09:00:53 -0700327 struct qed_ptt *p_ptt;
sudarsana.kalluru@cavium.comdb82f702017-04-26 09:00:50 -0700328 int rc;
329
sudarsana.kalluru@cavium.comd179bd12017-04-26 09:00:53 -0700330 p_ptt = qed_ptt_acquire(p_hwfn);
331 if (!p_ptt) {
332 DP_NOTICE(p_hwfn, "Failed to acquire PTT for PTP\n");
333 return -EBUSY;
334 }
335
336 p_hwfn->p_ptp_ptt = p_ptt;
337
sudarsana.kalluru@cavium.comdb82f702017-04-26 09:00:50 -0700338 rc = qed_ptp_res_lock(p_hwfn, p_ptt);
339 if (rc) {
340 DP_INFO(p_hwfn,
341 "Couldn't acquire the resource lock, skip ptp enable for this PF\n");
sudarsana.kalluru@cavium.comd179bd12017-04-26 09:00:53 -0700342 qed_ptt_release(p_hwfn, p_ptt);
343 p_hwfn->p_ptp_ptt = NULL;
sudarsana.kalluru@cavium.comdb82f702017-04-26 09:00:50 -0700344 return rc;
345 }
Sudarsana Reddy Kalluruc78c70f2017-02-15 10:24:10 +0200346
347 /* Reset PTP event detection rules - will be configured in the IOCTL */
348 qed_wr(p_hwfn, p_ptt, NIG_REG_LLH_PTP_PARAM_MASK, 0x7FF);
349 qed_wr(p_hwfn, p_ptt, NIG_REG_LLH_PTP_RULE_MASK, 0x3FFF);
350 qed_wr(p_hwfn, p_ptt, NIG_REG_TX_LLH_PTP_PARAM_MASK, 0x7FF);
351 qed_wr(p_hwfn, p_ptt, NIG_REG_TX_LLH_PTP_RULE_MASK, 0x3FFF);
352
353 qed_wr(p_hwfn, p_ptt, NIG_REG_TX_PTP_EN, 7);
354 qed_wr(p_hwfn, p_ptt, NIG_REG_RX_PTP_EN, 7);
355
356 qed_wr(p_hwfn, p_ptt, NIG_REG_TS_OUTPUT_ENABLE_PDA, 0x1);
357
358 /* Pause free running counter */
Mintz, Yuval9c79dda2017-03-14 16:23:54 +0200359 if (QED_IS_BB_B0(p_hwfn->cdev))
360 qed_wr(p_hwfn, p_ptt, NIG_REG_TIMESYNC_GEN_REG_BB, 2);
361 if (QED_IS_AH(p_hwfn->cdev))
362 qed_wr(p_hwfn, p_ptt, NIG_REG_TSGEN_FREECNT_UPDATE_K2, 2);
Sudarsana Reddy Kalluruc78c70f2017-02-15 10:24:10 +0200363
364 qed_wr(p_hwfn, p_ptt, NIG_REG_TSGEN_FREE_CNT_VALUE_LSB, 0);
365 qed_wr(p_hwfn, p_ptt, NIG_REG_TSGEN_FREE_CNT_VALUE_MSB, 0);
366 /* Resume free running counter */
Mintz, Yuval9c79dda2017-03-14 16:23:54 +0200367 if (QED_IS_BB_B0(p_hwfn->cdev))
368 qed_wr(p_hwfn, p_ptt, NIG_REG_TIMESYNC_GEN_REG_BB, 4);
369 if (QED_IS_AH(p_hwfn->cdev)) {
370 qed_wr(p_hwfn, p_ptt, NIG_REG_TSGEN_FREECNT_UPDATE_K2, 4);
371 qed_wr(p_hwfn, p_ptt, NIG_REG_PTP_LATCH_OSTS_PKT_TIME, 1);
372 }
Sudarsana Reddy Kalluruc78c70f2017-02-15 10:24:10 +0200373
374 /* Disable drift register */
375 qed_wr(p_hwfn, p_ptt, NIG_REG_TSGEN_DRIFT_CNTR_CONF, 0x0);
376 qed_wr(p_hwfn, p_ptt, NIG_REG_TSGEN_RST_DRIFT_CNTR, 0x0);
377
378 /* Reset possibly old timestamps */
379 qed_wr(p_hwfn, p_ptt, NIG_REG_LLH_PTP_HOST_BUF_SEQID,
380 QED_TIMESTAMP_MASK);
381 qed_wr(p_hwfn, p_ptt, NIG_REG_TX_LLH_PTP_BUF_SEQID, QED_TIMESTAMP_MASK);
382
383 return 0;
384}
385
386static int qed_ptp_hw_hwtstamp_tx_on(struct qed_dev *cdev)
387{
388 struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
389 struct qed_ptt *p_ptt = p_hwfn->p_ptp_ptt;
390
391 qed_wr(p_hwfn, p_ptt, NIG_REG_TX_LLH_PTP_PARAM_MASK, 0x6AA);
392 qed_wr(p_hwfn, p_ptt, NIG_REG_TX_LLH_PTP_RULE_MASK, 0x3EEE);
393
394 return 0;
395}
396
397static int qed_ptp_hw_disable(struct qed_dev *cdev)
398{
399 struct qed_hwfn *p_hwfn = QED_LEADING_HWFN(cdev);
400 struct qed_ptt *p_ptt = p_hwfn->p_ptp_ptt;
401
sudarsana.kalluru@cavium.comdb82f702017-04-26 09:00:50 -0700402 qed_ptp_res_unlock(p_hwfn, p_ptt);
403
Sudarsana Reddy Kalluruc78c70f2017-02-15 10:24:10 +0200404 /* Reset PTP event detection rules */
405 qed_wr(p_hwfn, p_ptt, NIG_REG_LLH_PTP_PARAM_MASK, 0x7FF);
406 qed_wr(p_hwfn, p_ptt, NIG_REG_LLH_PTP_RULE_MASK, 0x3FFF);
407
408 qed_wr(p_hwfn, p_ptt, NIG_REG_TX_LLH_PTP_PARAM_MASK, 0x7FF);
409 qed_wr(p_hwfn, p_ptt, NIG_REG_TX_LLH_PTP_RULE_MASK, 0x3FFF);
410
411 /* Disable the PTP feature */
412 qed_wr(p_hwfn, p_ptt, NIG_REG_RX_PTP_EN, 0x0);
413 qed_wr(p_hwfn, p_ptt, NIG_REG_TX_PTP_EN, 0x0);
414
sudarsana.kalluru@cavium.comd179bd12017-04-26 09:00:53 -0700415 qed_ptt_release(p_hwfn, p_ptt);
416 p_hwfn->p_ptp_ptt = NULL;
417
Sudarsana Reddy Kalluruc78c70f2017-02-15 10:24:10 +0200418 return 0;
419}
420
421const struct qed_eth_ptp_ops qed_ptp_ops_pass = {
422 .hwtstamp_tx_on = qed_ptp_hw_hwtstamp_tx_on,
423 .cfg_rx_filters = qed_ptp_hw_cfg_rx_filters,
424 .read_rx_ts = qed_ptp_hw_read_rx_ts,
425 .read_tx_ts = qed_ptp_hw_read_tx_ts,
426 .read_cc = qed_ptp_hw_read_cc,
427 .adjfreq = qed_ptp_hw_adjfreq,
428 .disable = qed_ptp_hw_disable,
429 .enable = qed_ptp_hw_enable,
430};