xref: /wlan-dirver/qca-wifi-host-cmn/dp/cmn_dp_api/dp_cal_client_api.c (revision 1397a33f48ea6455be40871470b286e535820eb8)
1 /*
2  * Copyright (c) 2017-2018 The Linux Foundation. All rights reserved.
3  *
4  * Permission to use, copy, modify, and/or distribute this software for
5  * any purpose with or without fee is hereby granted, provided that the
6  * above copyright notice and this permission notice appear in all
7  * copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL
10  * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED
11  * WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE
12  * AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
13  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
14  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
15  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
16  * PERFORMANCE OF THIS SOFTWARE.
17  */
18 
19 #include "dp_cal_client_api.h"
20 #include "qdf_module.h"
21 
22 /* dp_cal_client_attach - function to attach cal client timer
23  * @cal_client_ctx: cal client timer context
24  * @pdev: pdev handle
25  * @osdev: device pointer
26  * @dp_iterate_peer_list : function pointer to iterate and update peer stats
27  *
28  * return: void
29  */
30 void dp_cal_client_attach(void **cal_client_ctx, void *pdev,
31 			  qdf_device_t osdev,
32 			  void (*dp_iterate_peer_list)(void *))
33 {
34 	struct cal_client *cal_cl;
35 
36 	*cal_client_ctx = qdf_mem_malloc(sizeof(struct cal_client));
37 
38 	if (!(*cal_client_ctx))
39 		return;
40 
41 	cal_cl = (struct cal_client *)(*cal_client_ctx);
42 	cal_cl->iterate_update_peer_list = dp_iterate_peer_list;
43 	cal_cl->pdev_hdl = pdev;
44 
45 	qdf_timer_init(osdev, &cal_cl->cal_client_timer,
46 		       dp_cal_client_stats_timer_fn, *cal_client_ctx,
47 		       QDF_TIMER_TYPE_WAKE_APPS);
48 }
49 
50 qdf_export_symbol(dp_cal_client_attach);
51 
52 /* dp_cal_client_detach - detach cal client timer
53  * @cal_client_ctx: cal client timer context
54  *
55  * return: void
56  */
57 void dp_cal_client_detach(void **cal_client_ctx)
58 {
59 	struct cal_client *cal_cl;
60 
61 	if (*cal_client_ctx) {
62 		cal_cl = (struct cal_client *)*cal_client_ctx;
63 
64 		qdf_timer_stop(&cal_cl->cal_client_timer);
65 		qdf_timer_free(&cal_cl->cal_client_timer);
66 		qdf_mem_free(cal_cl);
67 		*cal_client_ctx = NULL;
68 	}
69 }
70 
71 qdf_export_symbol(dp_cal_client_detach);
72 
73 /* dp_cal_client_timer_start- api to start cal client timer
74  * @ctx: cal client timer ctx
75  *
76  * return: void
77  */
78 void dp_cal_client_timer_start(void *ctx)
79 {
80 	struct cal_client *cal_cl;
81 
82 	if (ctx) {
83 		cal_cl  = (struct cal_client *)ctx;
84 		qdf_timer_start(&cal_cl->cal_client_timer, DP_CAL_CLIENT_TIME);
85 	}
86 }
87 
88 qdf_export_symbol(dp_cal_client_timer_start);
89 
90 /* dp_cal_client_timer_stop- api to stop cal client timer
91  * @ctx: cal client timer ctx
92  *
93  * return: void
94  */
95 void dp_cal_client_timer_stop(void *ctx)
96 {
97 	struct cal_client *cal_cl;
98 
99 	if (ctx) {
100 		cal_cl = (struct cal_client *)ctx;
101 		qdf_timer_sync_cancel(&cal_cl->cal_client_timer);
102 		qdf_timer_stop(&cal_cl->cal_client_timer);
103 	}
104 }
105 
106 qdf_export_symbol(dp_cal_client_timer_stop);
107 
108 /* dp_cal_client_stats_timer_fn- function called on timer interval
109  * @ctx: cal client timer ctx
110  *
111  * return: void
112  */
113 void dp_cal_client_stats_timer_fn(void *ctx)
114 {
115 	struct cal_client *cal_cl = (struct cal_client *)ctx;
116 
117 	if (!cal_cl)
118 		return;
119 
120 	cal_cl->iterate_update_peer_list(cal_cl->pdev_hdl);
121 	qdf_timer_mod(&cal_cl->cal_client_timer, DP_CAL_CLIENT_TIME);
122 }
123 
124 qdf_export_symbol(dp_cal_client_stats_timer_fn);
125 
126 /*dp_cal_client_update_peer_stats - update peer stats in peer
127  * @peer_stats: cdp peer stats pointer
128  *
129  * return: void
130  */
131 void dp_cal_client_update_peer_stats(struct cdp_peer_stats *peer_stats)
132 {
133 	uint32_t temp_rx_bytes = peer_stats->rx.to_stack.bytes;
134 	uint32_t temp_rx_data = peer_stats->rx.to_stack.num;
135 	uint32_t temp_tx_bytes = peer_stats->tx.tx_success.bytes;
136 	uint32_t temp_tx_data = peer_stats->tx.tx_success.num;
137 	uint32_t temp_tx_ucast_pkts = peer_stats->tx.ucast.num;
138 
139 	peer_stats->rx.rx_byte_rate = temp_rx_bytes -
140 					peer_stats->rx.rx_bytes_success_last;
141 	peer_stats->rx.rx_data_rate  = temp_rx_data -
142 					peer_stats->rx.rx_data_success_last;
143 	peer_stats->tx.tx_byte_rate = temp_tx_bytes -
144 					peer_stats->tx.tx_bytes_success_last;
145 	peer_stats->tx.tx_data_rate  = temp_tx_data -
146 					peer_stats->tx.tx_data_success_last;
147 	peer_stats->tx.tx_data_ucast_rate = temp_tx_ucast_pkts -
148 					peer_stats->tx.tx_data_ucast_last;
149 
150 	peer_stats->rx.rx_bytes_success_last = temp_rx_bytes;
151 	peer_stats->rx.rx_data_success_last = temp_rx_data;
152 	peer_stats->tx.tx_bytes_success_last = temp_tx_bytes;
153 	peer_stats->tx.tx_data_success_last = temp_tx_data;
154 	peer_stats->tx.tx_data_ucast_last = temp_tx_ucast_pkts;
155 
156 	if (peer_stats->tx.tx_data_ucast_rate) {
157 		if (peer_stats->tx.tx_data_ucast_rate >
158 				peer_stats->tx.tx_data_rate)
159 			peer_stats->tx.last_per =
160 				((peer_stats->tx.tx_data_ucast_rate -
161 					peer_stats->tx.tx_data_rate) * 100) /
162 				peer_stats->tx.tx_data_ucast_rate;
163 		else
164 			peer_stats->tx.last_per = 0;
165 	}
166 
167 }
168 
169 qdf_export_symbol(dp_cal_client_update_peer_stats);
170 
171