blob: 493874fb4b3a8676acef5e6b48de58935c2547ce [file] [log] [blame]
Varsha Mishrae58d93b2017-05-20 20:54:41 +05301/*
2 **************************************************************************
Stephen Wang3e2dbd12018-03-14 17:28:17 -07003 * Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
Varsha Mishrae58d93b2017-05-20 20:54:41 +05304 * 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 copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
13 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 **************************************************************************
15 */
16
17#include "nss_tx_rx_common.h"
Yu Huang8c107082017-07-24 14:58:26 -070018#include "nss_wifili_stats.h"
Varsha Mishrae58d93b2017-05-20 20:54:41 +053019
20#define NSS_WIFILI_TX_TIMEOUT 1000 /* Millisecond to jiffies*/
21
22/*
23 * nss_wifili_pvt
24 * Private data structure
25 */
26static struct nss_wifili_pvt {
27 struct semaphore sem;
28 struct completion complete;
29 int response;
30 void *cb;
31 void *app_data;
32} wifili_pvt;
33
34/*
Varsha Mishrae58d93b2017-05-20 20:54:41 +053035 * nss_wifili_handler()
36 * Handle NSS -> HLOS messages for wifi
37 */
38static void nss_wifili_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
39{
40 struct nss_wifili_msg *ntm = (struct nss_wifili_msg *)ncm;
41 void *ctx;
42 nss_wifili_msg_callback_t cb;
43
44 nss_info("%p: NSS->HLOS message for wifili\n", nss_ctx);
45
46 /*
47 * The interface number shall be wifili soc interface or wifili radio interface
48 */
49 BUG_ON((nss_is_dynamic_interface(ncm->interface)) || ncm->interface != NSS_WIFILI_INTERFACE);
50
51 /*
52 * Is this a valid request/response packet?
53 */
54 if (ncm->type >= NSS_WIFILI_MAX_MSG) {
55 nss_warning("%p: Received invalid message %d for wifili interface", nss_ctx, ncm->type);
56 return;
57 }
58
59 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_wifili_msg)) {
60 nss_warning("%p: Length of message is greater than required: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
61 return;
62 }
63
64 /*
65 * Snoop messages for local driver and handle
66 */
67 switch (ntm->cm.type) {
Aniruddha Paul1b170c22017-05-29 12:30:39 +053068 case NSS_WIFILI_STATS_MSG:
Varsha Mishrae58d93b2017-05-20 20:54:41 +053069 nss_wifili_stats_sync(nss_ctx, &ntm->msg.wlsoc_stats, ncm->interface);
70 break;
71 }
72
73 /*
74 * Update the callback and app_data for notify messages, wifili sends all notify messages
75 * to the same callback/app_data.
76 */
77 if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) {
78 ncm->cb = (nss_ptr_t)nss_ctx->nss_top->wifili_msg_callback;
79 }
80
81 /*
82 * Log failures
83 */
84 nss_core_log_msg_failures(nss_ctx, ncm);
85
86 /*
87 * Do we have a call back
88 */
89 if (!ncm->cb) {
90 nss_info("%p: cb null for wifili interface %d", nss_ctx, ncm->interface);
91 return;
92 }
93
94 /*
95 * Get callback & context
96 */
97 cb = (nss_wifili_msg_callback_t)ncm->cb;
98 ctx = nss_ctx->subsys_dp_register[ncm->interface].ndev;
99
100 /*
101 * call wifili msg callback
102 */
103 if (!ctx) {
104 nss_warning("%p: Event received for wifili interface %d before registration", nss_ctx, ncm->interface);
105 return;
106 }
107
108 cb(ctx, ntm);
109}
110
111/*
112 * nss_wifili_callback()
113 * Callback to handle the completion of NSS->HLOS messages.
114 */
115static void nss_wifili_callback(void *app_data, struct nss_wifili_msg *nvm)
116{
117 nss_wifili_msg_callback_t callback = (nss_wifili_msg_callback_t)wifili_pvt.cb;
118 void *data = wifili_pvt.app_data;
119
120 wifili_pvt.response = NSS_TX_SUCCESS;
121 wifili_pvt.cb = NULL;
122 wifili_pvt.app_data = NULL;
123
124 if (nvm->cm.response != NSS_CMN_RESPONSE_ACK) {
125 nss_warning("wifili error response %d\n", nvm->cm.response);
126 wifili_pvt.response = nvm->cm.response;
127 }
128
129 if (callback) {
130 callback(data, nvm);
131 }
132 complete(&wifili_pvt.complete);
133}
134
135/*
136 * nss_wifili_tx_msg
137 * Transmit a wifili message to NSS FW
138 *
139 * NOTE: The caller is expected to handle synchronous wait for message
140 * response if needed.
141 */
142nss_tx_status_t nss_wifili_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_wifili_msg *msg)
143{
Varsha Mishrae58d93b2017-05-20 20:54:41 +0530144 struct nss_cmn_msg *ncm = &msg->cm;
Varsha Mishrae58d93b2017-05-20 20:54:41 +0530145
146 if (ncm->type >= NSS_WIFILI_MAX_MSG) {
147 nss_warning("%p: wifili message type out of range: %d", nss_ctx, ncm->type);
148 return NSS_TX_FAILURE;
149 }
150
Varsha Mishrae58d93b2017-05-20 20:54:41 +0530151 /*
152 * The interface number shall be wifili soc interface or wifili radio interface
153 */
154 if (ncm->interface != NSS_WIFILI_INTERFACE) {
155 nss_warning("%p: tx request for interface that is not a wifili: %d", nss_ctx, ncm->interface);
156 return NSS_TX_FAILURE;
157 }
158
Stephen Wang3e2dbd12018-03-14 17:28:17 -0700159 return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE);
Varsha Mishrae58d93b2017-05-20 20:54:41 +0530160}
161EXPORT_SYMBOL(nss_wifili_tx_msg);
162
163/*
164 * nss_wifili_tx_msg_sync()
165 * Transmit a wifili message to NSS firmware synchronously.
166 */
167nss_tx_status_t nss_wifili_tx_msg_sync(struct nss_ctx_instance *nss_ctx, struct nss_wifili_msg *nvm)
168{
169 nss_tx_status_t status;
170 int ret = 0;
171
172 down(&wifili_pvt.sem);
173 wifili_pvt.cb = (void *)nvm->cm.cb;
174 wifili_pvt.app_data = (void *)nvm->cm.app_data;
175
176 nvm->cm.cb = (nss_ptr_t)nss_wifili_callback;
177 nvm->cm.app_data = (nss_ptr_t)NULL;
178
179 status = nss_wifili_tx_msg(nss_ctx, nvm);
180 if (status != NSS_TX_SUCCESS) {
181 nss_warning("%p: wifili_tx_msg failed\n", nss_ctx);
182 up(&wifili_pvt.sem);
183 return status;
184 }
185
186 ret = wait_for_completion_timeout(&wifili_pvt.complete, msecs_to_jiffies(NSS_WIFILI_TX_TIMEOUT));
187 if (!ret) {
188 nss_warning("%p: wifili msg tx failed due to timeout\n", nss_ctx);
189 wifili_pvt.response = NSS_TX_FAILURE;
190 }
191
192 status = wifili_pvt.response;
193 up(&wifili_pvt.sem);
194 return status;
195}
196EXPORT_SYMBOL(nss_wifili_tx_msg_sync);
197
198/*
199 * nss_wifili_get_context()
200 */
201struct nss_ctx_instance *nss_wifili_get_context(void)
202{
203 return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id];
204}
205EXPORT_SYMBOL(nss_wifili_get_context);
206
207/*
208 * nss_wifili_msg_init()
209 * Initialize nss_wifili_msg.
210 */
211void nss_wifili_msg_init(struct nss_wifili_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data)
212{
213 nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data);
214}
215EXPORT_SYMBOL(nss_wifili_msg_init);
216
217/*
218 ****************************************
219 * Register/Unregister/Miscellaneous APIs
220 ****************************************
221 */
222
223/*
224 * nss_register_wifili_if()
225 * Register wifili with nss driver
226 */
227struct nss_ctx_instance *nss_register_wifili_if(uint32_t if_num, nss_wifili_callback_t wifili_callback,
228 nss_wifili_callback_t wifili_ext_callback,
229 nss_wifili_msg_callback_t event_callback, struct net_device *netdev, uint32_t features)
230{
231 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id];
232
233 /*
234 * The interface number shall be wifili soc interface
235 */
236 nss_assert(if_num == NSS_WIFILI_INTERFACE);
237
Aniruddha Paul1b170c22017-05-29 12:30:39 +0530238 nss_info("nss_register_wifili_if if_num %d wifictx %p", if_num, netdev);
Varsha Mishrae58d93b2017-05-20 20:54:41 +0530239
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700240 nss_core_register_subsys_dp(nss_ctx, if_num, wifili_callback, wifili_ext_callback, NULL, netdev, features);
Varsha Mishrae58d93b2017-05-20 20:54:41 +0530241
242 nss_top_main.wifili_msg_callback = event_callback;
Varsha Mishrae58d93b2017-05-20 20:54:41 +0530243
244 return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id];
245}
246EXPORT_SYMBOL(nss_register_wifili_if);
247
248/*
249 * nss_unregister_wifili_if()
250 * Unregister wifili with nss driver
251 */
252void nss_unregister_wifili_if(uint32_t if_num)
253{
254 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id];
255
256 /*
257 * The interface number shall be wifili soc interface
258 */
259 nss_assert(if_num == NSS_WIFILI_INTERFACE);
260
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700261 nss_core_unregister_subsys_dp(nss_ctx, if_num);
Varsha Mishrae58d93b2017-05-20 20:54:41 +0530262}
263EXPORT_SYMBOL(nss_unregister_wifili_if);
264
265/*
266 * nss_register_wifili_radio_if()
267 * Register wifili radio with nss driver
268 */
269struct nss_ctx_instance *nss_register_wifili_radio_if(uint32_t if_num, nss_wifili_callback_t wifili_callback,
270 nss_wifili_callback_t wifili_ext_callback,
271 nss_wifili_msg_callback_t event_callback, struct net_device *netdev, uint32_t features)
272{
273 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id];
274
275 /*
276 * The interface number shall be wifili radio dynamic interface
277 */
278 nss_assert(nss_is_dynamic_interface(if_num));
Aniruddha Paul1b170c22017-05-29 12:30:39 +0530279 nss_info("nss_register_wifili_if if_num %d wifictx %p", if_num, netdev);
Varsha Mishrae58d93b2017-05-20 20:54:41 +0530280
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700281 nss_core_register_subsys_dp(nss_ctx, if_num, wifili_callback, wifili_ext_callback, NULL, netdev, features);
Varsha Mishrae58d93b2017-05-20 20:54:41 +0530282
283 return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id];
284}
285EXPORT_SYMBOL(nss_register_wifili_radio_if);
286
287/*
288 * nss_unregister_wifili_radio_if()
289 * Unregister wifili radio with nss driver
290 */
291void nss_unregister_wifili_radio_if(uint32_t if_num)
292{
293 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id];
294
295 /*
296 * The interface number shall be wifili radio dynamic interface
297 */
298 nss_assert(nss_is_dynamic_interface(if_num));
299
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700300 nss_core_unregister_subsys_dp(nss_ctx, if_num);
Varsha Mishrae58d93b2017-05-20 20:54:41 +0530301}
302EXPORT_SYMBOL(nss_unregister_wifili_radio_if);
303
304/*
305 * nss_wifili_register_handler()
306 * Register handle for notfication messages received on wifi interface
307 */
Aniruddha Paul1b170c22017-05-29 12:30:39 +0530308void nss_wifili_register_handler(void)
Varsha Mishrae58d93b2017-05-20 20:54:41 +0530309{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700310 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id];
311
Varsha Mishrae58d93b2017-05-20 20:54:41 +0530312 nss_info("nss_wifili_register_handler");
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700313 nss_core_register_handler(nss_ctx, NSS_WIFILI_INTERFACE, nss_wifili_handler, NULL);
Varsha Mishrae58d93b2017-05-20 20:54:41 +0530314
Yu Huang8c107082017-07-24 14:58:26 -0700315 nss_wifili_stats_dentry_create();
316
Varsha Mishrae58d93b2017-05-20 20:54:41 +0530317 sema_init(&wifili_pvt.sem, 1);
318 init_completion(&wifili_pvt.complete);
319}