blob: 63a291b4be1c2ce6846cafcdedc15a4d0798a50d [file] [log] [blame]
Manikandan Mohandcc21ba2016-03-15 14:31:56 -07001/*
Dustin Brown4ea21db2018-01-05 14:13:17 -08002 * Copyright (c) 2016-2018 The Linux Foundation. All rights reserved.
Manikandan Mohandcc21ba2016-03-15 14:31:56 -07003 *
4 * Previously licensed under the ISC license by Qualcomm Atheros, Inc.
5 *
6 *
7 * Permission to use, copy, modify, and/or distribute this software for
8 * any purpose with or without fee is hereby granted, provided that the
9 * above copyright notice and this permission notice appear in all
10 * copies.
11 *
12 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
13 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
14 * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
15 * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
16 * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
17 * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
18 * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
19 * PERFORMANCE OF THIS SOFTWARE.
20 */
21
22/*
23 * This file was originally distributed by Qualcomm Atheros, Inc.
24 * under proprietary terms before Copyright ownership was assigned
25 * to the Linux Foundation.
26 */
27
28/**
29 * wlan_hdd_tsf.c - WLAN Host Device Driver tsf related implementation
30 */
31
32#include "wlan_hdd_main.h"
Jeff Johnsondde34492016-10-05 16:28:04 -070033#include "wlan_hdd_tsf.h"
Manikandan Mohandcc21ba2016-03-15 14:31:56 -070034#include "wma_api.h"
Sandeep Puligillafdd201e2017-02-02 18:43:46 -080035#include <qca_vendor.h>
Manikandan Mohandcc21ba2016-03-15 14:31:56 -070036
Manikandan Mohan5356c2b2016-04-03 15:51:35 -070037static struct completion tsf_sync_get_completion_evt;
38#define WLAN_TSF_SYNC_GET_TIMEOUT 2000
Yu Wangf5d5b5f2017-05-25 22:38:32 +080039
Manikandan Mohandcc21ba2016-03-15 14:31:56 -070040/**
Yu Wangf5d5b5f2017-05-25 22:38:32 +080041 * enum hdd_tsf_op_result - result of tsf operation
Manikandan Mohandcc21ba2016-03-15 14:31:56 -070042 *
Yu Wangf5d5b5f2017-05-25 22:38:32 +080043 * HDD_TSF_OP_SUCC: succeed
44 * HDD_TSF_OP_FAIL: fail
Manikandan Mohandcc21ba2016-03-15 14:31:56 -070045 */
Yu Wangf5d5b5f2017-05-25 22:38:32 +080046enum hdd_tsf_op_result {
47 HDD_TSF_OP_SUCC,
48 HDD_TSF_OP_FAIL
49};
50
Yu Wang000dc2f2017-05-26 17:38:48 +080051#ifdef WLAN_FEATURE_TSF_PLUS
Jeff Johnson8f389862017-08-29 14:19:23 -070052static inline void hdd_set_th_sync_status(struct hdd_adapter *adapter,
Yu Wang000dc2f2017-05-26 17:38:48 +080053 bool initialized)
54{
55 qdf_atomic_set(&adapter->tsf_sync_ready_flag,
56 (initialized ? 1 : 0));
57}
58
Jeff Johnson8f389862017-08-29 14:19:23 -070059static inline bool hdd_get_th_sync_status(struct hdd_adapter *adapter)
Yu Wang000dc2f2017-05-26 17:38:48 +080060{
61 return qdf_atomic_read(&adapter->tsf_sync_ready_flag) != 0;
62}
63
64#else
Jeff Johnson8f389862017-08-29 14:19:23 -070065static inline bool hdd_get_th_sync_status(struct hdd_adapter *adapter)
Yu Wang000dc2f2017-05-26 17:38:48 +080066{
67 return true;
68}
69#endif
70
Yu Wangf5d5b5f2017-05-25 22:38:32 +080071static
Jeff Johnson8f389862017-08-29 14:19:23 -070072enum hdd_tsf_get_state hdd_tsf_check_conn_state(struct hdd_adapter *adapter)
Manikandan Mohandcc21ba2016-03-15 14:31:56 -070073{
Yu Wangf5d5b5f2017-05-25 22:38:32 +080074 enum hdd_tsf_get_state ret = TSF_RETURN;
Jeff Johnson40dae4e2017-08-29 14:00:25 -070075 struct hdd_station_ctx *hdd_sta_ctx;
Manikandan Mohandcc21ba2016-03-15 14:31:56 -070076
Manikandan Mohandcc21ba2016-03-15 14:31:56 -070077 if (adapter->device_mode == QDF_STA_MODE ||
Yu Wangf5d5b5f2017-05-25 22:38:32 +080078 adapter->device_mode == QDF_P2P_CLIENT_MODE) {
Manikandan Mohandcc21ba2016-03-15 14:31:56 -070079 hdd_sta_ctx = WLAN_HDD_GET_STATION_CTX_PTR(adapter);
80 if (hdd_sta_ctx->conn_info.connState !=
Manikandan Mohan5356c2b2016-04-03 15:51:35 -070081 eConnectionState_Associated) {
82 hdd_err("failed to cap tsf, not connect with ap");
Yu Wangf5d5b5f2017-05-25 22:38:32 +080083 ret = TSF_STA_NOT_CONNECTED_NO_TSF;
Manikandan Mohandcc21ba2016-03-15 14:31:56 -070084 }
Yu Wangf5d5b5f2017-05-25 22:38:32 +080085 } else if ((adapter->device_mode == QDF_SAP_MODE ||
86 adapter->device_mode == QDF_P2P_GO_MODE) &&
87 !(test_bit(SOFTAP_BSS_STARTED,
88 &adapter->event_flags))) {
Manikandan Mohan5356c2b2016-04-03 15:51:35 -070089 hdd_err("Soft AP / P2p GO not beaconing");
Yu Wangf5d5b5f2017-05-25 22:38:32 +080090 ret = TSF_SAP_NOT_STARTED_NO_TSF;
Manikandan Mohandcc21ba2016-03-15 14:31:56 -070091 }
92 return ret;
93}
94
Jeff Johnson8f389862017-08-29 14:19:23 -070095static bool hdd_tsf_is_initialized(struct hdd_adapter *adapter)
Yu Wangf5d5b5f2017-05-25 22:38:32 +080096{
Jeff Johnson854cccd2017-08-28 11:39:24 -070097 struct hdd_context *hddctx;
Yu Wangf5d5b5f2017-05-25 22:38:32 +080098
99 if (!adapter) {
100 hdd_err("invalid adapter");
101 return false;
102 }
103
104 hddctx = WLAN_HDD_GET_CTX(adapter);
105 if (!hddctx) {
106 hdd_err("invalid hdd context");
107 return false;
108 }
109
Yu Wang000dc2f2017-05-26 17:38:48 +0800110 if (!qdf_atomic_read(&hddctx->tsf_ready_flag) ||
111 !hdd_get_th_sync_status(adapter)) {
Yu Wangf5d5b5f2017-05-25 22:38:32 +0800112 hdd_err("TSF is not initialized");
113 return false;
114 }
115
116 return true;
117}
118
Manikandan Mohandcc21ba2016-03-15 14:31:56 -0700119/**
Manikandan Mohan5356c2b2016-04-03 15:51:35 -0700120 * hdd_tsf_reset_gpio() - Reset TSF GPIO used for host timer sync
121 * @adapter: pointer to adapter
122 *
123 * This function send WMI command to reset GPIO configured in FW after
124 * TSF get operation.
125 *
126 * Return: TSF_RETURN on Success, TSF_RESET_GPIO_FAIL on failure
127 */
128#ifdef QCA_WIFI_3_0
Jeff Johnson85b5c112017-08-11 15:15:23 -0700129static int hdd_tsf_reset_gpio(struct hdd_adapter *adapter)
Manikandan Mohan5356c2b2016-04-03 15:51:35 -0700130{
131 /* No GPIO Host timer sync for integrated WIFI Device */
132 return TSF_RETURN;
133}
134#else
Jeff Johnson85b5c112017-08-11 15:15:23 -0700135static int hdd_tsf_reset_gpio(struct hdd_adapter *adapter)
Manikandan Mohan5356c2b2016-04-03 15:51:35 -0700136{
137 int ret;
Srinivas Girigowda388dea82017-03-25 11:49:56 -0700138
Jeff Johnson1b780e42017-10-31 14:11:45 -0700139 ret = wma_cli_set_command((int)adapter->session_id,
140 (int)GEN_PARAM_RESET_TSF_GPIO, adapter->session_id,
Manikandan Mohan5356c2b2016-04-03 15:51:35 -0700141 GEN_CMD);
142
143 if (ret != 0) {
144 hdd_err("tsf reset GPIO fail ");
145 ret = TSF_RESET_GPIO_FAIL;
146 } else {
147 ret = TSF_RETURN;
148 }
149 return ret;
150}
151#endif
152
Yu Wangf5d5b5f2017-05-25 22:38:32 +0800153static enum hdd_tsf_op_result hdd_capture_tsf_internal(
Jeff Johnson8f389862017-08-29 14:19:23 -0700154 struct hdd_adapter *adapter, uint32_t *buf, int len)
Manikandan Mohandcc21ba2016-03-15 14:31:56 -0700155{
Yu Wangf5d5b5f2017-05-25 22:38:32 +0800156 int ret;
Jeff Johnson854cccd2017-08-28 11:39:24 -0700157 struct hdd_context *hddctx;
Manikandan Mohandcc21ba2016-03-15 14:31:56 -0700158
159 if (adapter == NULL || buf == NULL) {
Manikandan Mohan5356c2b2016-04-03 15:51:35 -0700160 hdd_err("invalid pointer");
Yu Wangf5d5b5f2017-05-25 22:38:32 +0800161 return HDD_TSF_OP_FAIL;
162 }
163
164 if (len != 1)
165 return HDD_TSF_OP_FAIL;
166
167 hddctx = WLAN_HDD_GET_CTX(adapter);
168 if (!hddctx) {
169 hdd_err("invalid hdd context");
170 return HDD_TSF_OP_FAIL;
171 }
172
173 if (!hdd_tsf_is_initialized(adapter)) {
174 buf[0] = TSF_NOT_READY;
175 return HDD_TSF_OP_SUCC;
176 }
177
178 buf[0] = hdd_tsf_check_conn_state(adapter);
179 if (buf[0] != TSF_RETURN)
180 return HDD_TSF_OP_SUCC;
181
182 if (qdf_atomic_inc_return(&hddctx->cap_tsf_flag) > 1) {
183 hdd_err("current in capture state");
184 buf[0] = TSF_CURRENT_IN_CAP_STATE;
185 return HDD_TSF_OP_SUCC;
186 }
187
188 /* record adapter for cap_tsf_irq_handler */
189 hddctx->cap_tsf_context = adapter;
190
191 hdd_err("+ioctl issue cap tsf cmd");
192
193 /* Reset TSF value for new capture */
194 adapter->cur_target_time = 0;
195
196 buf[0] = TSF_RETURN;
197 init_completion(&tsf_sync_get_completion_evt);
Jeff Johnson1b780e42017-10-31 14:11:45 -0700198 ret = wma_cli_set_command((int)adapter->session_id,
Yu Wangf5d5b5f2017-05-25 22:38:32 +0800199 (int)GEN_PARAM_CAPTURE_TSF,
Jeff Johnson1b780e42017-10-31 14:11:45 -0700200 adapter->session_id, GEN_CMD);
Yu Wangf5d5b5f2017-05-25 22:38:32 +0800201 if (QDF_STATUS_SUCCESS != ret) {
202 hdd_err("cap tsf fail");
203 buf[0] = TSF_CAPTURE_FAIL;
204 hddctx->cap_tsf_context = NULL;
205 qdf_atomic_set(&hddctx->cap_tsf_flag, 0);
206 return HDD_TSF_OP_SUCC;
207 }
208 hdd_err("-ioctl return cap tsf cmd");
209 return HDD_TSF_OP_SUCC;
210}
211
212static enum hdd_tsf_op_result hdd_indicate_tsf_internal(
Jeff Johnson8f389862017-08-29 14:19:23 -0700213 struct hdd_adapter *adapter, uint32_t *buf, int len)
Yu Wangf5d5b5f2017-05-25 22:38:32 +0800214{
215 int ret;
Jeff Johnson854cccd2017-08-28 11:39:24 -0700216 struct hdd_context *hddctx;
Yu Wangf5d5b5f2017-05-25 22:38:32 +0800217
218 if (!adapter || !buf) {
219 hdd_err("invalid pointer");
220 return HDD_TSF_OP_FAIL;
Manikandan Mohandcc21ba2016-03-15 14:31:56 -0700221 }
222
223 if (len != 3)
Yu Wangf5d5b5f2017-05-25 22:38:32 +0800224 return HDD_TSF_OP_FAIL;
225
226 hddctx = WLAN_HDD_GET_CTX(adapter);
227 if (!hddctx) {
228 hdd_err("invalid hdd context");
229 return HDD_TSF_OP_FAIL;
230 }
Manikandan Mohandcc21ba2016-03-15 14:31:56 -0700231
232 buf[1] = 0;
233 buf[2] = 0;
Yu Wangf5d5b5f2017-05-25 22:38:32 +0800234
235 if (!hdd_tsf_is_initialized(adapter)) {
236 buf[0] = TSF_NOT_READY;
237 return HDD_TSF_OP_SUCC;
Manikandan Mohandcc21ba2016-03-15 14:31:56 -0700238 }
Yu Wangf5d5b5f2017-05-25 22:38:32 +0800239
240 buf[0] = hdd_tsf_check_conn_state(adapter);
241 if (buf[0] != TSF_RETURN)
242 return HDD_TSF_OP_SUCC;
243
244 if (adapter->cur_target_time == 0) {
Manikandan Mohan5356c2b2016-04-03 15:51:35 -0700245 hdd_info("TSF value not received");
Manikandan Mohandcc21ba2016-03-15 14:31:56 -0700246 buf[0] = TSF_NOT_RETURNED_BY_FW;
Yu Wangf5d5b5f2017-05-25 22:38:32 +0800247 return HDD_TSF_OP_SUCC;
Jeff Johnsona5fb2182017-10-06 20:27:38 -0700248 }
249
250 buf[0] = TSF_RETURN;
251 buf[1] = (uint32_t)(adapter->cur_target_time & 0xffffffff);
252 buf[2] = (uint32_t)((adapter->cur_target_time >> 32) &
Yu Wangf5d5b5f2017-05-25 22:38:32 +0800253 0xffffffff);
254
Jeff Johnsona5fb2182017-10-06 20:27:38 -0700255 if (!qdf_atomic_read(&hddctx->cap_tsf_flag)) {
256 hdd_info("old: status=%u, tsf_low=%u, tsf_high=%u",
257 buf[0], buf[1], buf[2]);
Yu Wangf5d5b5f2017-05-25 22:38:32 +0800258 return HDD_TSF_OP_SUCC;
Manikandan Mohandcc21ba2016-03-15 14:31:56 -0700259 }
Jeff Johnsona5fb2182017-10-06 20:27:38 -0700260
261 ret = hdd_tsf_reset_gpio(adapter);
262 if (0 != ret) {
263 hdd_err("reset tsf gpio fail");
264 buf[0] = TSF_RESET_GPIO_FAIL;
265 return HDD_TSF_OP_SUCC;
266 }
267 hddctx->cap_tsf_context = NULL;
268 qdf_atomic_set(&hddctx->cap_tsf_flag, 0);
269 hdd_info("get tsf cmd,status=%u, tsf_low=%u, tsf_high=%u",
270 buf[0], buf[1], buf[2]);
271
272 return HDD_TSF_OP_SUCC;
Yu Wangf5d5b5f2017-05-25 22:38:32 +0800273}
274
Yu Wang000dc2f2017-05-26 17:38:48 +0800275#ifdef WLAN_FEATURE_TSF_PLUS
276/* unit for target time: us; host time: ns */
277#define HOST_TO_TARGET_TIME_RATIO NSEC_PER_USEC
278#define MAX_ALLOWED_DEVIATION_NS (20 * NSEC_PER_MSEC)
279#define MAX_CONTINUOUS_ERROR_CNT 3
Yu Wang04ccd762017-05-31 19:29:43 +0800280
281/* to distinguish 32-bit overflow case, this inverval should:
282 * equal or less than (1/2 * OVERFLOW_INDICATOR32 us)
283 */
Yu Wang000dc2f2017-05-26 17:38:48 +0800284#define WLAN_HDD_CAPTURE_TSF_INTERVAL_SEC 500
285#define WLAN_HDD_CAPTURE_TSF_INIT_INTERVAL_MS 100
Yu Wang04ccd762017-05-31 19:29:43 +0800286#define NORMAL_INTERVAL_TARGET \
287 ((int64_t)((int64_t)WLAN_HDD_CAPTURE_TSF_INTERVAL_SEC * \
288 NSEC_PER_SEC / HOST_TO_TARGET_TIME_RATIO))
289#define OVERFLOW_INDICATOR32 (((int64_t)0x1) << 32)
Yu Wang000dc2f2017-05-26 17:38:48 +0800290
291/**
292 * TS_STATUS - timestamp status
293 *
294 * HDD_TS_STATUS_WAITING: one of the stamp-pair
295 * is not updated
296 * HDD_TS_STATUS_READY: valid tstamp-pair
297 * HDD_TS_STATUS_INVALID: invalid tstamp-pair
298 */
299enum hdd_ts_status {
300 HDD_TS_STATUS_WAITING,
301 HDD_TS_STATUS_READY,
302 HDD_TS_STATUS_INVALID
303};
304
305static
Jeff Johnson8f389862017-08-29 14:19:23 -0700306enum hdd_tsf_op_result __hdd_start_tsf_sync(struct hdd_adapter *adapter)
Yu Wang000dc2f2017-05-26 17:38:48 +0800307{
308 QDF_STATUS ret;
309
310 if (!hdd_get_th_sync_status(adapter)) {
311 hdd_err("Host Target sync has not initialized");
312 return HDD_TSF_OP_FAIL;
313 }
314
315 ret = qdf_mc_timer_start(&adapter->host_target_sync_timer,
316 WLAN_HDD_CAPTURE_TSF_INIT_INTERVAL_MS);
317 if (ret != QDF_STATUS_SUCCESS && ret != QDF_STATUS_E_ALREADY) {
318 hdd_err("Failed to start timer, ret: %d", ret);
319 return HDD_TSF_OP_FAIL;
320 }
321 return HDD_TSF_OP_SUCC;
322}
323
324static
Jeff Johnson8f389862017-08-29 14:19:23 -0700325enum hdd_tsf_op_result __hdd_stop_tsf_sync(struct hdd_adapter *adapter)
Yu Wang000dc2f2017-05-26 17:38:48 +0800326{
327 QDF_STATUS ret;
328
329 if (!hdd_get_th_sync_status(adapter)) {
330 hdd_err("Host Target sync has not initialized");
331 return HDD_TSF_OP_SUCC;
332 }
333
334 ret = qdf_mc_timer_stop(&adapter->host_target_sync_timer);
335 if (ret != QDF_STATUS_SUCCESS) {
336 hdd_err("Failed to stop timer, ret: %d", ret);
337 return HDD_TSF_OP_FAIL;
338 }
339 return HDD_TSF_OP_SUCC;
340}
341
Jeff Johnson8f389862017-08-29 14:19:23 -0700342static inline void hdd_reset_timestamps(struct hdd_adapter *adapter)
Yu Wang000dc2f2017-05-26 17:38:48 +0800343{
344 qdf_spin_lock_bh(&adapter->host_target_sync_lock);
345 adapter->cur_host_time = 0;
346 adapter->cur_target_time = 0;
347 adapter->last_host_time = 0;
348 adapter->last_target_time = 0;
349 qdf_spin_unlock_bh(&adapter->host_target_sync_lock);
350}
351
352/**
353 * hdd_check_timestamp_status() - return the tstamp status
354 *
355 * @last_target_time: the last saved target time
356 * @last_host_time: the last saved host time
357 * @cur_target_time : new target time
358 * @cur_host_time : new host time
359 *
360 * This function check the new timstamp-pair(cur_host_time/cur_target_time)
361 *
362 * Return:
363 * HDD_TS_STATUS_WAITING: cur_host_time or cur_host_time is 0
364 * HDD_TS_STATUS_READY: cur_target_time/cur_host_time is a valid pair,
365 * and can be saved
366 * HDD_TS_STATUS_INVALID: cur_target_time/cur_host_time is a invalid pair,
367 * should be discard
368 */
369static
370enum hdd_ts_status hdd_check_timestamp_status(
371 uint64_t last_target_time,
372 uint64_t last_host_time,
373 uint64_t cur_target_time,
374 uint64_t cur_host_time)
375{
376 uint64_t delta_ns, delta_target_time, delta_host_time;
377
378 /* one or more are not updated, need to wait */
379 if (cur_target_time == 0 || cur_host_time == 0)
380 return HDD_TS_STATUS_WAITING;
381
382 /* init value, it's the first time to update the pair */
383 if (last_target_time == 0 && last_host_time == 0)
384 return HDD_TS_STATUS_READY;
385
386 /* the new values should be greater than the saved values */
387 if ((cur_target_time <= last_target_time) ||
388 (cur_host_time <= last_host_time)) {
389 hdd_err("Invalid timestamps!last_target_time: %llu;"
390 "last_host_time: %llu; cur_target_time: %llu;"
391 "cur_host_time: %llu",
392 last_target_time, last_host_time,
393 cur_target_time, cur_host_time);
394 return HDD_TS_STATUS_INVALID;
395 }
396
397 delta_target_time = (cur_target_time - last_target_time) *
398 HOST_TO_TARGET_TIME_RATIO;
399 delta_host_time = cur_host_time - last_host_time;
400
401 /*
402 * DO NOT use abs64() , a big uint64 value might be turned to
403 * a small int64 value
404 */
405 delta_ns = ((delta_target_time > delta_host_time) ?
406 (delta_target_time - delta_host_time) :
407 (delta_host_time - delta_target_time));
408
409 /* the deviation should be smaller than a threshold */
410 if (delta_ns > MAX_ALLOWED_DEVIATION_NS) {
411 hdd_info("Invalid timestamps - delta: %llu ns", delta_ns);
412 return HDD_TS_STATUS_INVALID;
413 }
414 return HDD_TS_STATUS_READY;
415}
416
Jeff Johnson8f389862017-08-29 14:19:23 -0700417static void hdd_update_timestamp(struct hdd_adapter *adapter,
Yu Wang000dc2f2017-05-26 17:38:48 +0800418 uint64_t target_time, uint64_t host_time)
419{
420 int interval = 0;
421 enum hdd_ts_status sync_status;
422
423 if (!adapter)
424 return;
425
Yu Wang70757d52017-06-08 12:15:42 +0800426 /* host time is updated in IRQ context, it's always before target time,
427 * and so no need to try update last_host_time at present;
428 * since the interval of capturing TSF
429 * (WLAN_HDD_CAPTURE_TSF_INTERVAL_SEC) is long enough, host and target
430 * time are updated in pairs, and one by one, we can return here to
431 * avoid requiring spin lock, and to speed up the IRQ processing.
432 */
433 if (host_time > 0) {
434 adapter->cur_host_time = host_time;
435 return;
436 }
437
Yu Wang000dc2f2017-05-26 17:38:48 +0800438 qdf_spin_lock_bh(&adapter->host_target_sync_lock);
439 if (target_time > 0)
440 adapter->cur_target_time = target_time;
Yu Wang000dc2f2017-05-26 17:38:48 +0800441
442 sync_status = hdd_check_timestamp_status(adapter->last_target_time,
443 adapter->last_host_time,
444 adapter->cur_target_time,
445 adapter->cur_host_time);
446 switch (sync_status) {
447 case HDD_TS_STATUS_INVALID:
448 if (++adapter->continuous_error_count <
449 MAX_CONTINUOUS_ERROR_CNT) {
450 interval =
451 WLAN_HDD_CAPTURE_TSF_INIT_INTERVAL_MS;
452 adapter->cur_target_time = 0;
453 adapter->cur_host_time = 0;
454 break;
455 }
456 hdd_info("Reach the max continuous error count");
457 /*
458 * fall through:
459 * If reach MAX_CONTINUOUS_ERROR_CNT, treat it as a
460 * valid pair
461 */
462 case HDD_TS_STATUS_READY:
463 adapter->last_target_time = adapter->cur_target_time;
464 adapter->last_host_time = adapter->cur_host_time;
465 adapter->cur_target_time = 0;
466 adapter->cur_host_time = 0;
467 hdd_info("ts-pair updated: target: %llu; host: %llu",
468 adapter->last_target_time,
469 adapter->last_host_time);
470 interval = WLAN_HDD_CAPTURE_TSF_INTERVAL_SEC *
471 MSEC_PER_SEC;
472 adapter->continuous_error_count = 0;
473 break;
474 case HDD_TS_STATUS_WAITING:
475 interval = 0;
476 break;
477 }
478 qdf_spin_unlock_bh(&adapter->host_target_sync_lock);
479
480 if (interval > 0)
481 qdf_mc_timer_start(&adapter->host_target_sync_timer, interval);
482}
483
Jeff Johnson8f389862017-08-29 14:19:23 -0700484static inline bool hdd_tsf_is_in_cap(struct hdd_adapter *adapter)
Yu Wang04ccd762017-05-31 19:29:43 +0800485{
Jeff Johnson854cccd2017-08-28 11:39:24 -0700486 struct hdd_context *hddctx;
Yu Wang04ccd762017-05-31 19:29:43 +0800487
488 hddctx = WLAN_HDD_GET_CTX(adapter);
489 if (!hddctx)
490 return false;
491
492 return qdf_atomic_read(&hddctx->cap_tsf_flag) > 0;
493}
494
495/* define 64bit plus/minus to deal with overflow */
496static inline int hdd_64bit_plus(uint64_t x, int64_t y, uint64_t *ret)
497{
498 if ((y < 0 && (-y) > x) ||
499 (y > 0 && (y > U64_MAX - x))) {
500 *ret = 0;
501 return -EINVAL;
502 }
503
504 *ret = x + y;
505 return 0;
506}
507
Yu Wang0ea10172017-06-02 13:40:52 +0800508static inline int hdd_uint64_plus(uint64_t x, uint64_t y, uint64_t *ret)
509{
510 if (!ret)
511 return -EINVAL;
512
513 if (x > (U64_MAX - y)) {
514 *ret = 0;
515 return -EINVAL;
516 }
517
518 *ret = x + y;
519 return 0;
520}
521
522static inline int hdd_uint64_minus(uint64_t x, uint64_t y, uint64_t *ret)
523{
524 if (!ret)
525 return -EINVAL;
526
527 if (x < y) {
528 *ret = 0;
529 return -EINVAL;
530 }
531
532 *ret = x - y;
533 return 0;
534}
535
Yu Wang04ccd762017-05-31 19:29:43 +0800536static inline int32_t hdd_get_hosttime_from_targettime(
Jeff Johnson8f389862017-08-29 14:19:23 -0700537 struct hdd_adapter *adapter, uint64_t target_time,
Yu Wang04ccd762017-05-31 19:29:43 +0800538 uint64_t *host_time)
539{
540 int32_t ret = -EINVAL;
541 int64_t delta32_target;
542 bool in_cap_state;
543
544 in_cap_state = hdd_tsf_is_in_cap(adapter);
545
546 /*
547 * To avoid check the lock when it's not capturing tsf
548 * (the tstamp-pair won't be changed)
549 */
550 if (in_cap_state)
551 qdf_spin_lock_bh(&adapter->host_target_sync_lock);
552
553 /* at present, target_time is only 32bit in fact */
554 delta32_target = (int64_t)((target_time & U32_MAX) -
555 (adapter->last_target_time & U32_MAX));
556
557 if (delta32_target <
558 (NORMAL_INTERVAL_TARGET - OVERFLOW_INDICATOR32))
559 delta32_target += OVERFLOW_INDICATOR32;
560 else if (delta32_target >
561 (OVERFLOW_INDICATOR32 - NORMAL_INTERVAL_TARGET))
562 delta32_target -= OVERFLOW_INDICATOR32;
563
564 ret = hdd_64bit_plus(adapter->last_host_time,
565 HOST_TO_TARGET_TIME_RATIO * delta32_target,
566 host_time);
567
568 if (in_cap_state)
569 qdf_spin_unlock_bh(&adapter->host_target_sync_lock);
570
571 return ret;
572}
573
Yu Wang0ea10172017-06-02 13:40:52 +0800574static inline int32_t hdd_get_targettime_from_hosttime(
Jeff Johnson8f389862017-08-29 14:19:23 -0700575 struct hdd_adapter *adapter, uint64_t host_time,
Yu Wang0ea10172017-06-02 13:40:52 +0800576 uint64_t *target_time)
577{
578 int32_t ret = -EINVAL;
579 bool in_cap_state;
580
581 if (!adapter || host_time == 0)
582 return ret;
583
584 in_cap_state = hdd_tsf_is_in_cap(adapter);
585 if (in_cap_state)
586 qdf_spin_lock_bh(&adapter->host_target_sync_lock);
587
588 if (host_time < adapter->last_host_time)
589 ret = hdd_uint64_minus(adapter->last_target_time,
Yu Wangc9ef24f2017-09-15 18:37:07 +0800590 qdf_do_div(adapter->last_host_time -
591 host_time,
592 HOST_TO_TARGET_TIME_RATIO),
Yu Wang0ea10172017-06-02 13:40:52 +0800593 target_time);
594 else
595 ret = hdd_uint64_plus(adapter->last_target_time,
Yu Wangc9ef24f2017-09-15 18:37:07 +0800596 qdf_do_div(host_time -
597 adapter->last_host_time,
598 HOST_TO_TARGET_TIME_RATIO),
Yu Wang0ea10172017-06-02 13:40:52 +0800599 target_time);
600
601 if (in_cap_state)
602 qdf_spin_unlock_bh(&adapter->host_target_sync_lock);
603
604 return ret;
605}
606
Yu Wang66a250b2017-07-19 11:46:40 +0800607static inline
608uint64_t hdd_get_monotonic_host_time(struct hdd_context *hdd_ctx)
Yu Wang000dc2f2017-05-26 17:38:48 +0800609{
Yu Wang66a250b2017-07-19 11:46:40 +0800610 return HDD_TSF_IS_RAW_SET(hdd_ctx) ?
611 ktime_get_ns() : ktime_get_real_ns();
Yu Wang000dc2f2017-05-26 17:38:48 +0800612}
613
Yu Wang0ea10172017-06-02 13:40:52 +0800614static ssize_t __hdd_wlan_tsf_show(struct device *dev,
615 struct device_attribute *attr, char *buf)
616{
Jeff Johnson40dae4e2017-08-29 14:00:25 -0700617 struct hdd_station_ctx *hdd_sta_ctx;
Jeff Johnson8f389862017-08-29 14:19:23 -0700618 struct hdd_adapter *adapter;
Yu Wang66a250b2017-07-19 11:46:40 +0800619 struct hdd_context *hdd_ctx;
Yu Wang0ea10172017-06-02 13:40:52 +0800620 ssize_t size;
621 uint64_t host_time, target_time;
622
623 struct net_device *net_dev = container_of(dev, struct net_device, dev);
624
Jeff Johnson8f389862017-08-29 14:19:23 -0700625 adapter = (struct hdd_adapter *)(netdev_priv(net_dev));
Yu Wang0ea10172017-06-02 13:40:52 +0800626 if (adapter->magic != WLAN_HDD_ADAPTER_MAGIC)
627 return scnprintf(buf, PAGE_SIZE, "Invalid device\n");
628
629 if (!hdd_get_th_sync_status(adapter))
630 return scnprintf(buf, PAGE_SIZE,
631 "TSF sync is not initialized\n");
632
633 hdd_sta_ctx = WLAN_HDD_GET_STATION_CTX_PTR(adapter);
634 if (eConnectionState_Associated != hdd_sta_ctx->conn_info.connState)
635 return scnprintf(buf, PAGE_SIZE, "NOT connected\n");
636
Yu Wang66a250b2017-07-19 11:46:40 +0800637 hdd_ctx = WLAN_HDD_GET_CTX(adapter);
638 if (!hdd_ctx)
639 return scnprintf(buf, PAGE_SIZE, "Invalid HDD context\n");
640
641 host_time = hdd_get_monotonic_host_time(hdd_ctx);
Yu Wang0ea10172017-06-02 13:40:52 +0800642 if (hdd_get_targettime_from_hosttime(adapter, host_time,
643 &target_time))
644 size = scnprintf(buf, PAGE_SIZE, "Invalid timestamp\n");
645 else
646 size = scnprintf(buf, PAGE_SIZE, "%s%llu %llu %pM\n",
647 buf, target_time, host_time,
648 hdd_sta_ctx->conn_info.bssId.bytes);
649 return size;
650}
651
652static ssize_t hdd_wlan_tsf_show(struct device *dev,
653 struct device_attribute *attr, char *buf)
654{
655 ssize_t ret;
656
657 cds_ssr_protect(__func__);
658 ret = __hdd_wlan_tsf_show(dev, attr, buf);
659 cds_ssr_unprotect(__func__);
660
661 return ret;
662}
663
664static DEVICE_ATTR(tsf, 0400, hdd_wlan_tsf_show, NULL);
665
Yu Wang000dc2f2017-05-26 17:38:48 +0800666static void hdd_capture_tsf_timer_expired_handler(void *arg)
667{
668 uint32_t tsf_op_resp;
Jeff Johnson8f389862017-08-29 14:19:23 -0700669 struct hdd_adapter *adapter;
Yu Wang000dc2f2017-05-26 17:38:48 +0800670
671 if (!arg)
672 return;
673
Jeff Johnson8f389862017-08-29 14:19:23 -0700674 adapter = (struct hdd_adapter *)arg;
Yu Wang000dc2f2017-05-26 17:38:48 +0800675 hdd_capture_tsf_internal(adapter, &tsf_op_resp, 1);
676}
677
678static irqreturn_t hdd_tsf_captured_irq_handler(int irq, void *arg)
679{
Jeff Johnson8f389862017-08-29 14:19:23 -0700680 struct hdd_adapter *adapter;
Jeff Johnson854cccd2017-08-28 11:39:24 -0700681 struct hdd_context *hdd_ctx;
Yu Wang000dc2f2017-05-26 17:38:48 +0800682 uint64_t host_time;
683 char *name = NULL;
684
685 if (!arg)
686 return IRQ_NONE;
687
Jeff Johnson854cccd2017-08-28 11:39:24 -0700688 hdd_ctx = (struct hdd_context *)arg;
Yu Wang66a250b2017-07-19 11:46:40 +0800689 host_time = hdd_get_monotonic_host_time(hdd_ctx);
Yu Wang000dc2f2017-05-26 17:38:48 +0800690
691 adapter = hdd_ctx->cap_tsf_context;
692 if (!adapter)
693 return IRQ_HANDLED;
694
695 if (!hdd_tsf_is_initialized(adapter)) {
696 hdd_err("tsf is not init, ignore irq");
697 return IRQ_HANDLED;
698 }
699
700 hdd_update_timestamp(adapter, 0, host_time);
701 if (adapter->dev)
702 name = adapter->dev->name;
703
704 hdd_info("irq: %d - iface: %s - host_time: %llu",
705 irq, (!name ? "none" : name), host_time);
706
707 return IRQ_HANDLED;
708}
709
Jeff Johnson8f389862017-08-29 14:19:23 -0700710static enum hdd_tsf_op_result hdd_tsf_sync_init(struct hdd_adapter *adapter)
Yu Wang000dc2f2017-05-26 17:38:48 +0800711{
712 QDF_STATUS ret;
Jeff Johnson854cccd2017-08-28 11:39:24 -0700713 struct hdd_context *hddctx;
Yu Wang0ea10172017-06-02 13:40:52 +0800714 struct net_device *net_dev;
Yu Wang000dc2f2017-05-26 17:38:48 +0800715
716 if (!adapter)
717 return HDD_TSF_OP_FAIL;
718
719 hddctx = WLAN_HDD_GET_CTX(adapter);
720 if (!hddctx) {
721 hdd_err("invalid hdd context");
722 return HDD_TSF_OP_FAIL;
723 }
724
725 if (!qdf_atomic_read(&hddctx->tsf_ready_flag)) {
726 hdd_err("TSF feature has NOT been initialized");
727 return HDD_TSF_OP_FAIL;
728 }
729
730 if (hdd_get_th_sync_status(adapter)) {
731 hdd_err("Host Target sync has been initialized!!");
732 return HDD_TSF_OP_SUCC;
733 }
734
735 qdf_spinlock_create(&adapter->host_target_sync_lock);
736
737 hdd_reset_timestamps(adapter);
738
739 ret = qdf_mc_timer_init(&adapter->host_target_sync_timer,
740 QDF_TIMER_TYPE_SW,
741 hdd_capture_tsf_timer_expired_handler,
742 (void *)adapter);
743 if (ret != QDF_STATUS_SUCCESS) {
744 hdd_err("Failed to init timer, ret: %d", ret);
745 goto fail;
746 }
747
Yu Wang0ea10172017-06-02 13:40:52 +0800748 net_dev = adapter->dev;
Yu Wang66a250b2017-07-19 11:46:40 +0800749 if (net_dev && HDD_TSF_IS_DBG_FS_SET(hddctx))
750 device_create_file(&net_dev->dev, &dev_attr_tsf);
Yu Wang000dc2f2017-05-26 17:38:48 +0800751 hdd_set_th_sync_status(adapter, true);
752
753 return HDD_TSF_OP_SUCC;
754fail:
755 hdd_set_th_sync_status(adapter, false);
756 return HDD_TSF_OP_FAIL;
757}
758
Jeff Johnson8f389862017-08-29 14:19:23 -0700759static enum hdd_tsf_op_result hdd_tsf_sync_deinit(struct hdd_adapter *adapter)
Yu Wang000dc2f2017-05-26 17:38:48 +0800760{
761 QDF_STATUS ret;
Jeff Johnson854cccd2017-08-28 11:39:24 -0700762 struct hdd_context *hddctx;
Yu Wang0ea10172017-06-02 13:40:52 +0800763 struct net_device *net_dev;
Yu Wang000dc2f2017-05-26 17:38:48 +0800764
765 if (!adapter)
766 return HDD_TSF_OP_FAIL;
767
768 if (!hdd_get_th_sync_status(adapter)) {
769 hdd_err("Host Target sync has not been initialized!!");
770 return HDD_TSF_OP_SUCC;
771 }
772
773 hdd_set_th_sync_status(adapter, false);
774
775 ret = qdf_mc_timer_destroy(&adapter->host_target_sync_timer);
776 if (ret != QDF_STATUS_SUCCESS)
777 hdd_err("Failed to destroy timer, ret: %d", ret);
778
779 hddctx = WLAN_HDD_GET_CTX(adapter);
780
781 /* reset the cap_tsf flag and gpio if needed */
782 if (hddctx && qdf_atomic_read(&hddctx->cap_tsf_flag) &&
783 hddctx->cap_tsf_context == adapter) {
784 int reset_ret = hdd_tsf_reset_gpio(adapter);
785
786 if (reset_ret)
787 hdd_err("Failed to reset tsf gpio, ret:%d",
788 reset_ret);
789 hddctx->cap_tsf_context = NULL;
790 qdf_atomic_set(&hddctx->cap_tsf_flag, 0);
791 }
792
793 hdd_reset_timestamps(adapter);
Yu Wang66a250b2017-07-19 11:46:40 +0800794
795 net_dev = adapter->dev;
796 if (net_dev && HDD_TSF_IS_DBG_FS_SET(hddctx)) {
797 struct device *dev = &net_dev->dev;
798
799 device_remove_file(dev, &dev_attr_tsf);
800 }
Yu Wang000dc2f2017-05-26 17:38:48 +0800801 return HDD_TSF_OP_SUCC;
802}
803
Jeff Johnson8f389862017-08-29 14:19:23 -0700804static inline void hdd_update_tsf(struct hdd_adapter *adapter, uint64_t tsf)
Yu Wang000dc2f2017-05-26 17:38:48 +0800805{
806 uint32_t tsf_op_resp[3];
807
808 hdd_indicate_tsf_internal(adapter, tsf_op_resp, 3);
809 hdd_update_timestamp(adapter, tsf, 0);
810}
811
Yu Wang04ccd762017-05-31 19:29:43 +0800812static inline
813enum hdd_tsf_op_result hdd_netbuf_timestamp(qdf_nbuf_t netbuf,
814 uint64_t target_time)
815{
Jeff Johnson8f389862017-08-29 14:19:23 -0700816 struct hdd_adapter *adapter;
Yu Wang04ccd762017-05-31 19:29:43 +0800817 struct net_device *net_dev = netbuf->dev;
818
819 if (!net_dev)
820 return HDD_TSF_OP_FAIL;
821
Jeff Johnson8f389862017-08-29 14:19:23 -0700822 adapter = (struct hdd_adapter *)(netdev_priv(net_dev));
Yu Wang04ccd762017-05-31 19:29:43 +0800823 if (adapter && adapter->magic == WLAN_HDD_ADAPTER_MAGIC &&
824 hdd_get_th_sync_status(adapter)) {
825 uint64_t host_time;
826 int32_t ret = hdd_get_hosttime_from_targettime(adapter,
827 target_time, &host_time);
828 if (!ret) {
829 netbuf->tstamp = ns_to_ktime(host_time);
830 return HDD_TSF_OP_SUCC;
831 }
832 }
833
834 return HDD_TSF_OP_FAIL;
835}
836
Jeff Johnson8f389862017-08-29 14:19:23 -0700837int hdd_start_tsf_sync(struct hdd_adapter *adapter)
Yu Wang000dc2f2017-05-26 17:38:48 +0800838{
839 enum hdd_tsf_op_result ret;
840
841 if (!adapter)
842 return -EINVAL;
843
844 ret = hdd_tsf_sync_init(adapter);
845 if (ret != HDD_TSF_OP_SUCC) {
846 hdd_err("Failed to init tsf sync, ret: %d", ret);
847 return -EINVAL;
848 }
849
850 return (__hdd_start_tsf_sync(adapter) ==
851 HDD_TSF_OP_SUCC) ? 0 : -EINVAL;
852}
853
Jeff Johnson8f389862017-08-29 14:19:23 -0700854int hdd_stop_tsf_sync(struct hdd_adapter *adapter)
Yu Wang000dc2f2017-05-26 17:38:48 +0800855{
856 enum hdd_tsf_op_result ret;
857
858 if (!adapter)
859 return -EINVAL;
860
861 ret = __hdd_stop_tsf_sync(adapter);
862 if (ret != HDD_TSF_OP_SUCC)
863 return -EINVAL;
864
865 ret = hdd_tsf_sync_deinit(adapter);
866 if (ret != HDD_TSF_OP_SUCC) {
867 hdd_err("Failed to deinit tsf sync, ret: %d", ret);
868 return -EINVAL;
869 }
870 return 0;
871}
872
Yu Wang04ccd762017-05-31 19:29:43 +0800873int hdd_tx_timestamp(qdf_nbuf_t netbuf, uint64_t target_time)
874{
875 struct sock *sk = netbuf->sk;
876
877 if (!sk)
878 return -EINVAL;
879
880 if ((skb_shinfo(netbuf)->tx_flags & SKBTX_SW_TSTAMP) &&
881 !(skb_shinfo(netbuf)->tx_flags & SKBTX_IN_PROGRESS)) {
882 struct sock_exterr_skb *serr;
883 qdf_nbuf_t new_netbuf;
884 int err;
885
886 if (hdd_netbuf_timestamp(netbuf, target_time) !=
887 HDD_TSF_OP_SUCC)
888 return -EINVAL;
889
890 new_netbuf = qdf_nbuf_clone(netbuf);
891 if (!new_netbuf)
892 return -ENOMEM;
893
894 serr = SKB_EXT_ERR(new_netbuf);
895 memset(serr, 0, sizeof(*serr));
896 serr->ee.ee_errno = ENOMSG;
897 serr->ee.ee_origin = SO_EE_ORIGIN_TIMESTAMPING;
898
899 err = sock_queue_err_skb(sk, new_netbuf);
900 if (err) {
901 qdf_nbuf_free(new_netbuf);
902 return err;
903 }
904
905 return 0;
906 }
907 return -EINVAL;
908}
909
910int hdd_rx_timestamp(qdf_nbuf_t netbuf, uint64_t target_time)
911{
912 if (hdd_netbuf_timestamp(netbuf, target_time) ==
913 HDD_TSF_OP_SUCC)
914 return 0;
915
916 /* reset tstamp when failed */
917 netbuf->tstamp = ns_to_ktime(0);
918 return -EINVAL;
919}
920
Jeff Johnson8f389862017-08-29 14:19:23 -0700921static inline int __hdd_capture_tsf(struct hdd_adapter *adapter,
Yu Wang000dc2f2017-05-26 17:38:48 +0800922 uint32_t *buf, int len)
923{
924 if (!adapter || !buf) {
925 hdd_err("invalid pointer");
926 return -EINVAL;
927 }
928
929 if (len != 1)
930 return -EINVAL;
931
932 buf[0] = TSF_DISABLED_BY_TSFPLUS;
933
934 return 0;
935}
936
Jeff Johnson8f389862017-08-29 14:19:23 -0700937static inline int __hdd_indicate_tsf(struct hdd_adapter *adapter,
Yu Wang000dc2f2017-05-26 17:38:48 +0800938 uint32_t *buf, int len)
939{
940 if (!adapter || !buf) {
941 hdd_err("invalid pointer");
942 return -EINVAL;
943 }
944
945 if (len != 3)
946 return -EINVAL;
947
948 buf[0] = TSF_DISABLED_BY_TSFPLUS;
949 buf[1] = 0;
950 buf[2] = 0;
951
952 return 0;
953}
954
955static inline
Jeff Johnson854cccd2017-08-28 11:39:24 -0700956enum hdd_tsf_op_result wlan_hdd_tsf_plus_init(struct hdd_context *hdd_ctx)
Yu Wang000dc2f2017-05-26 17:38:48 +0800957{
958 int ret;
959
Yu Wang66a250b2017-07-19 11:46:40 +0800960 if (!HDD_TSF_IS_PTP_ENABLED(hdd_ctx)) {
961 hdd_info("To enable TSF_PLUS, set gtsf_ptp_options in ini");
962 return HDD_TSF_OP_FAIL;
963 }
964
Yu Wang000dc2f2017-05-26 17:38:48 +0800965 ret = cnss_common_register_tsf_captured_handler(
966 hdd_ctx->parent_dev,
967 hdd_tsf_captured_irq_handler,
968 (void *)hdd_ctx);
969 if (ret != 0) {
970 hdd_err("Failed to register irq handler: %d", ret);
971 return HDD_TSF_OP_FAIL;
972 }
Yu Wangceb357b2017-06-01 12:04:18 +0800973
Yu Wang66a250b2017-07-19 11:46:40 +0800974 if (HDD_TSF_IS_TX_SET(hdd_ctx))
975 ol_register_timestamp_callback(hdd_tx_timestamp);
Yu Wang000dc2f2017-05-26 17:38:48 +0800976 return HDD_TSF_OP_SUCC;
977}
978
979static inline
Jeff Johnson854cccd2017-08-28 11:39:24 -0700980enum hdd_tsf_op_result wlan_hdd_tsf_plus_deinit(struct hdd_context *hdd_ctx)
Yu Wang000dc2f2017-05-26 17:38:48 +0800981{
982 int ret;
983
Yu Wang66a250b2017-07-19 11:46:40 +0800984 if (!HDD_TSF_IS_PTP_ENABLED(hdd_ctx))
985 return HDD_TSF_OP_SUCC;
986
987 if (HDD_TSF_IS_TX_SET(hdd_ctx))
988 ol_deregister_timestamp_callback();
989
Yu Wang000dc2f2017-05-26 17:38:48 +0800990 ret = cnss_common_unregister_tsf_captured_handler(
991 hdd_ctx->parent_dev,
992 (void *)hdd_ctx);
993 if (ret != 0) {
994 hdd_err("Failed to unregister irq handler, ret:%d",
995 ret);
996 ret = HDD_TSF_OP_FAIL;
997 }
998
999 return HDD_TSF_OP_SUCC;
1000}
1001
Jeff Johnson8f389862017-08-29 14:19:23 -07001002void hdd_tsf_notify_wlan_state_change(struct hdd_adapter *adapter,
Yu Wang000dc2f2017-05-26 17:38:48 +08001003 eConnectionState old_state,
1004 eConnectionState new_state)
1005{
1006 if (!adapter)
1007 return;
1008
1009 if (old_state != eConnectionState_Associated &&
1010 new_state == eConnectionState_Associated)
1011 hdd_start_tsf_sync(adapter);
1012 else if (old_state == eConnectionState_Associated &&
1013 new_state != eConnectionState_Associated)
1014 hdd_stop_tsf_sync(adapter);
1015}
1016#else
Jeff Johnson8f389862017-08-29 14:19:23 -07001017static inline void hdd_update_tsf(struct hdd_adapter *adapter, uint64_t tsf)
Yu Wang000dc2f2017-05-26 17:38:48 +08001018{
1019}
1020
Jeff Johnson8f389862017-08-29 14:19:23 -07001021static inline int __hdd_indicate_tsf(struct hdd_adapter *adapter,
Yu Wang000dc2f2017-05-26 17:38:48 +08001022 uint32_t *buf, int len)
1023{
1024 return (hdd_indicate_tsf_internal(adapter, buf, len) ==
1025 HDD_TSF_OP_SUCC) ? 0 : -EINVAL;
1026}
1027
Jeff Johnson8f389862017-08-29 14:19:23 -07001028static inline int __hdd_capture_tsf(struct hdd_adapter *adapter,
Yu Wang000dc2f2017-05-26 17:38:48 +08001029 uint32_t *buf, int len)
Yu Wangf5d5b5f2017-05-25 22:38:32 +08001030{
1031 return (hdd_capture_tsf_internal(adapter, buf, len) ==
1032 HDD_TSF_OP_SUCC) ? 0 : -EINVAL;
1033}
1034
Yu Wang000dc2f2017-05-26 17:38:48 +08001035static inline
Jeff Johnson854cccd2017-08-28 11:39:24 -07001036enum hdd_tsf_op_result wlan_hdd_tsf_plus_init(struct hdd_context *hdd_ctx)
Yu Wang000dc2f2017-05-26 17:38:48 +08001037{
1038 return HDD_TSF_OP_SUCC;
1039}
1040
1041static inline
Jeff Johnson854cccd2017-08-28 11:39:24 -07001042enum hdd_tsf_op_result wlan_hdd_tsf_plus_deinit(struct hdd_context *hdd_ctx)
Yu Wang000dc2f2017-05-26 17:38:48 +08001043{
1044 return HDD_TSF_OP_SUCC;
1045}
1046#endif /* WLAN_FEATURE_TSF_PLUS */
1047
Jeff Johnson8f389862017-08-29 14:19:23 -07001048int hdd_capture_tsf(struct hdd_adapter *adapter, uint32_t *buf, int len)
Yu Wang000dc2f2017-05-26 17:38:48 +08001049{
1050 return __hdd_capture_tsf(adapter, buf, len);
1051}
1052
Jeff Johnson8f389862017-08-29 14:19:23 -07001053int hdd_indicate_tsf(struct hdd_adapter *adapter, uint32_t *buf, int len)
Yu Wangf5d5b5f2017-05-25 22:38:32 +08001054{
Yu Wang000dc2f2017-05-26 17:38:48 +08001055 return __hdd_indicate_tsf(adapter, buf, len);
Manikandan Mohandcc21ba2016-03-15 14:31:56 -07001056}
1057
1058/**
1059 * hdd_get_tsf_cb() - handle tsf callback
1060 * @pcb_cxt: pointer to the hdd_contex
1061 * @ptsf: pointer to struct stsf
1062 *
1063 * This function handle the event that reported by firmware at first.
1064 * The event contains the vdev_id, current tsf value of this vdev,
1065 * tsf value is 64bits, discripted in two varaible tsf_low and tsf_high.
1066 * These two values each is uint32.
1067 *
1068 * Return: 0 for success or non-zero negative failure code
1069 */
Arun Khandavalli4b55da72016-07-19 19:55:01 +05301070int hdd_get_tsf_cb(void *pcb_cxt, struct stsf *ptsf)
Manikandan Mohandcc21ba2016-03-15 14:31:56 -07001071{
Jeff Johnson82797b62017-08-11 15:31:27 -07001072 struct hdd_context *hddctx;
Jeff Johnson85b5c112017-08-11 15:15:23 -07001073 struct hdd_adapter *adapter;
Manikandan Mohandcc21ba2016-03-15 14:31:56 -07001074 int status;
1075
1076 if (pcb_cxt == NULL || ptsf == NULL) {
Manikandan Mohan5356c2b2016-04-03 15:51:35 -07001077 hdd_err("HDD context is not valid");
Manikandan Mohandcc21ba2016-03-15 14:31:56 -07001078 return -EINVAL;
1079 }
1080
Jeff Johnson82797b62017-08-11 15:31:27 -07001081 hddctx = (struct hdd_context *)pcb_cxt;
Manikandan Mohandcc21ba2016-03-15 14:31:56 -07001082 status = wlan_hdd_validate_context(hddctx);
Srinivas Girigowda388dea82017-03-25 11:49:56 -07001083 if (0 != status)
Manikandan Mohandcc21ba2016-03-15 14:31:56 -07001084 return -EINVAL;
Manikandan Mohandcc21ba2016-03-15 14:31:56 -07001085
1086 adapter = hdd_get_adapter_by_vdev(hddctx, ptsf->vdev_id);
1087
1088 if (NULL == adapter) {
Manikandan Mohan5356c2b2016-04-03 15:51:35 -07001089 hdd_err("failed to find adapter");
Manikandan Mohandcc21ba2016-03-15 14:31:56 -07001090 return -EINVAL;
1091 }
Yu Wangf5d5b5f2017-05-25 22:38:32 +08001092
1093 if (!hdd_tsf_is_initialized(adapter)) {
1094 hdd_err("tsf is not init, ignore tsf event");
1095 return -EINVAL;
1096 }
1097
Manikandan Mohan5356c2b2016-04-03 15:51:35 -07001098 hdd_info("tsf cb handle event, device_mode is %d",
Manikandan Mohandcc21ba2016-03-15 14:31:56 -07001099 adapter->device_mode);
1100
Yu Wangf5d5b5f2017-05-25 22:38:32 +08001101 adapter->cur_target_time = ((uint64_t)ptsf->tsf_high << 32 |
1102 ptsf->tsf_low);
Manikandan Mohan5356c2b2016-04-03 15:51:35 -07001103 adapter->tsf_sync_soc_timer = ((uint64_t) ptsf->soc_timer_high << 32 |
1104 ptsf->soc_timer_low);
Manikandan Mohandcc21ba2016-03-15 14:31:56 -07001105
Manikandan Mohan5356c2b2016-04-03 15:51:35 -07001106 complete(&tsf_sync_get_completion_evt);
Yu Wang000dc2f2017-05-26 17:38:48 +08001107 hdd_update_tsf(adapter, adapter->cur_target_time);
Manikandan Mohan5356c2b2016-04-03 15:51:35 -07001108 hdd_info("Vdev=%u, tsf_low=%u, tsf_high=%u soc_timer=%llu",
1109 ptsf->vdev_id, ptsf->tsf_low, ptsf->tsf_high,
1110 adapter->tsf_sync_soc_timer);
Manikandan Mohandcc21ba2016-03-15 14:31:56 -07001111 return 0;
1112}
1113
Jeff Johnson363764f2017-06-02 13:43:36 -07001114static const struct nla_policy tsf_policy[QCA_WLAN_VENDOR_ATTR_TSF_MAX + 1] = {
1115 [QCA_WLAN_VENDOR_ATTR_TSF_CMD] = {.type = NLA_U32},
1116};
1117
Manikandan Mohandcc21ba2016-03-15 14:31:56 -07001118/**
Manikandan Mohan5356c2b2016-04-03 15:51:35 -07001119 * __wlan_hdd_cfg80211_handle_tsf_cmd(): Setup TSF operations
1120 * @wiphy: Pointer to wireless phy
1121 * @wdev: Pointer to wireless device
1122 * @data: Pointer to data
1123 * @data_len: Data length
1124 *
1125 * Handle TSF SET / GET operation from userspace
1126 *
1127 * Return: 0 on success, negative errno on failure
1128 */
1129static int __wlan_hdd_cfg80211_handle_tsf_cmd(struct wiphy *wiphy,
1130 struct wireless_dev *wdev,
1131 const void *data,
1132 int data_len)
1133{
1134 struct net_device *dev = wdev->netdev;
Jeff Johnson8f389862017-08-29 14:19:23 -07001135 struct hdd_adapter *adapter = WLAN_HDD_GET_PRIV_PTR(dev);
Jeff Johnson854cccd2017-08-28 11:39:24 -07001136 struct hdd_context *hdd_ctx = wiphy_priv(wiphy);
Manikandan Mohan5356c2b2016-04-03 15:51:35 -07001137 struct nlattr *tb_vendor[QCA_WLAN_VENDOR_ATTR_TSF_MAX + 1];
1138 int status, ret;
1139 struct sk_buff *reply_skb;
1140 uint32_t tsf_op_resp[3], tsf_cmd;
1141
1142 ENTER_DEV(wdev->netdev);
1143
1144 status = wlan_hdd_validate_context(hdd_ctx);
1145 if (0 != status)
1146 return -EINVAL;
1147
Dustin Brown4ea21db2018-01-05 14:13:17 -08001148 if (wlan_cfg80211_nla_parse(tb_vendor, QCA_WLAN_VENDOR_ATTR_TSF_MAX,
1149 data, data_len, tsf_policy)) {
Manikandan Mohan5356c2b2016-04-03 15:51:35 -07001150 hdd_err("Invalid TSF cmd");
1151 return -EINVAL;
1152 }
1153
1154 if (!tb_vendor[QCA_WLAN_VENDOR_ATTR_TSF_CMD]) {
1155 hdd_err("Invalid TSF cmd");
1156 return -EINVAL;
1157 }
1158 tsf_cmd = nla_get_u32(tb_vendor[QCA_WLAN_VENDOR_ATTR_TSF_CMD]);
1159
1160 if (tsf_cmd == QCA_TSF_CAPTURE || tsf_cmd == QCA_TSF_SYNC_GET) {
1161 hdd_capture_tsf(adapter, tsf_op_resp, 1);
1162 switch (tsf_op_resp[0]) {
1163 case TSF_RETURN:
1164 status = 0;
1165 break;
1166 case TSF_CURRENT_IN_CAP_STATE:
1167 status = -EALREADY;
1168 break;
1169 case TSF_STA_NOT_CONNECTED_NO_TSF:
1170 case TSF_SAP_NOT_STARTED_NO_TSF:
1171 status = -EPERM;
1172 break;
1173 default:
1174 case TSF_CAPTURE_FAIL:
1175 status = -EINVAL;
1176 break;
1177 }
1178 }
1179 if (status < 0)
1180 goto end;
1181
1182 if (tsf_cmd == QCA_TSF_SYNC_GET) {
1183 ret = wait_for_completion_timeout(&tsf_sync_get_completion_evt,
1184 msecs_to_jiffies(WLAN_TSF_SYNC_GET_TIMEOUT));
1185 if (ret == 0) {
1186 status = -ETIMEDOUT;
1187 goto end;
1188 }
1189 }
1190
1191 if (tsf_cmd == QCA_TSF_GET || tsf_cmd == QCA_TSF_SYNC_GET) {
1192 hdd_indicate_tsf(adapter, tsf_op_resp, 3);
1193 switch (tsf_op_resp[0]) {
1194 case TSF_RETURN:
1195 status = 0;
1196 break;
1197 case TSF_NOT_RETURNED_BY_FW:
1198 status = -EINPROGRESS;
1199 break;
1200 case TSF_STA_NOT_CONNECTED_NO_TSF:
1201 case TSF_SAP_NOT_STARTED_NO_TSF:
1202 status = -EPERM;
1203 break;
1204 default:
1205 status = -EINVAL;
1206 break;
1207 }
1208 if (status != 0)
1209 goto end;
1210
1211 reply_skb = cfg80211_vendor_event_alloc(hdd_ctx->wiphy, NULL,
1212 sizeof(uint64_t) * 2 + NLMSG_HDRLEN,
1213 QCA_NL80211_VENDOR_SUBCMD_TSF_INDEX,
1214 GFP_KERNEL);
1215 if (!reply_skb) {
1216 hdd_err("cfg80211_vendor_cmd_alloc_reply_skb failed");
1217 status = -ENOMEM;
1218 goto end;
1219 }
Dustin Brownbb7e2f52016-10-17 12:16:35 -07001220 if (hdd_wlan_nla_put_u64(reply_skb,
1221 QCA_WLAN_VENDOR_ATTR_TSF_TIMER_VALUE,
Yu Wangf5d5b5f2017-05-25 22:38:32 +08001222 adapter->cur_target_time) ||
Dustin Brownbb7e2f52016-10-17 12:16:35 -07001223 hdd_wlan_nla_put_u64(reply_skb,
Manikandan Mohan5356c2b2016-04-03 15:51:35 -07001224 QCA_WLAN_VENDOR_ATTR_TSF_SOC_TIMER_VALUE,
Dustin Brownbb7e2f52016-10-17 12:16:35 -07001225 adapter->tsf_sync_soc_timer)) {
Manikandan Mohan5356c2b2016-04-03 15:51:35 -07001226 hdd_err("nla put fail");
1227 kfree_skb(reply_skb);
1228 status = -EINVAL;
1229 goto end;
1230 }
1231 status = cfg80211_vendor_cmd_reply(reply_skb);
1232 }
1233
1234end:
1235 hdd_info("TSF operation %d Status: %d", tsf_cmd, status);
1236 return status;
1237}
1238
Manikandan Mohan5356c2b2016-04-03 15:51:35 -07001239int wlan_hdd_cfg80211_handle_tsf_cmd(struct wiphy *wiphy,
1240 struct wireless_dev *wdev,
1241 const void *data,
1242 int data_len)
1243{
1244 int ret;
1245
1246 cds_ssr_protect(__func__);
1247 ret = __wlan_hdd_cfg80211_handle_tsf_cmd(wiphy, wdev, data, data_len);
1248 cds_ssr_unprotect(__func__);
1249
1250 return ret;
1251}
1252
1253/**
Manikandan Mohandcc21ba2016-03-15 14:31:56 -07001254 * wlan_hdd_tsf_init() - set callback to handle tsf value.
Jeff Johnson82797b62017-08-11 15:31:27 -07001255 * @hdd_ctx: pointer to the struct hdd_context
Manikandan Mohandcc21ba2016-03-15 14:31:56 -07001256 *
1257 * This function set the callback to sme module, the callback will be
1258 * called when a tsf event is reported by firmware
1259 *
1260 * Return: none
1261 */
Jeff Johnson82797b62017-08-11 15:31:27 -07001262void wlan_hdd_tsf_init(struct hdd_context *hdd_ctx)
Manikandan Mohandcc21ba2016-03-15 14:31:56 -07001263{
Yu Wangf5d5b5f2017-05-25 22:38:32 +08001264 QDF_STATUS hal_status;
1265
1266 if (!hdd_ctx)
1267 return;
1268
1269 if (qdf_atomic_inc_return(&hdd_ctx->tsf_ready_flag) > 1)
1270 return;
1271
1272 qdf_atomic_init(&hdd_ctx->cap_tsf_flag);
1273
1274 if (hdd_ctx->config->tsf_gpio_pin == TSF_GPIO_PIN_INVALID)
1275 goto fail;
1276
1277 hal_status = sme_set_tsf_gpio(hdd_ctx->hHal,
1278 hdd_ctx->config->tsf_gpio_pin);
1279 if (QDF_STATUS_SUCCESS != hal_status) {
1280 hdd_err("set tsf GPIO failed, status: %d", hal_status);
1281 goto fail;
1282 }
1283
Yu Wang000dc2f2017-05-26 17:38:48 +08001284 if (wlan_hdd_tsf_plus_init(hdd_ctx) != HDD_TSF_OP_SUCC)
1285 goto fail;
1286
Yu Wangf5d5b5f2017-05-25 22:38:32 +08001287 return;
1288
1289fail:
1290 qdf_atomic_set(&hdd_ctx->tsf_ready_flag, 0);
1291}
1292
Jeff Johnson854cccd2017-08-28 11:39:24 -07001293void wlan_hdd_tsf_deinit(struct hdd_context *hdd_ctx)
Yu Wangf5d5b5f2017-05-25 22:38:32 +08001294{
1295 if (!hdd_ctx)
1296 return;
1297
1298 if (!qdf_atomic_read(&hdd_ctx->tsf_ready_flag))
1299 return;
1300
Yu Wang000dc2f2017-05-26 17:38:48 +08001301 wlan_hdd_tsf_plus_deinit(hdd_ctx);
Yu Wangf5d5b5f2017-05-25 22:38:32 +08001302 qdf_atomic_set(&hdd_ctx->tsf_ready_flag, 0);
1303 qdf_atomic_set(&hdd_ctx->cap_tsf_flag, 0);
Manikandan Mohandcc21ba2016-03-15 14:31:56 -07001304}