Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 3 | * Copyright (c) 2017-2018, The Linux Foundation. All rights reserved. |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 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 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 Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 18 | #include "nss_wifili_stats.h" |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 19 | |
| 20 | #define NSS_WIFILI_TX_TIMEOUT 1000 /* Millisecond to jiffies*/ |
| 21 | |
| 22 | /* |
| 23 | * nss_wifili_pvt |
| 24 | * Private data structure |
| 25 | */ |
| 26 | static 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 Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 35 | * nss_wifili_handler() |
| 36 | * Handle NSS -> HLOS messages for wifi |
| 37 | */ |
| 38 | static 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 Paul | 1b170c2 | 2017-05-29 12:30:39 +0530 | [diff] [blame] | 68 | case NSS_WIFILI_STATS_MSG: |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 69 | 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 | */ |
Suruchi Agarwal | e4ad24a | 2018-06-11 12:03:46 +0530 | [diff] [blame^] | 77 | if (ncm->response == NSS_CMN_RESPONSE_NOTIFY) { |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 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 | */ |
| 115 | static 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 | */ |
| 142 | nss_tx_status_t nss_wifili_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_wifili_msg *msg) |
| 143 | { |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 144 | struct nss_cmn_msg *ncm = &msg->cm; |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 145 | |
| 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 Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 151 | /* |
| 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 Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 159 | return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE); |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 160 | } |
| 161 | EXPORT_SYMBOL(nss_wifili_tx_msg); |
| 162 | |
| 163 | /* |
| 164 | * nss_wifili_tx_msg_sync() |
| 165 | * Transmit a wifili message to NSS firmware synchronously. |
| 166 | */ |
| 167 | nss_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 | } |
| 196 | EXPORT_SYMBOL(nss_wifili_tx_msg_sync); |
| 197 | |
| 198 | /* |
| 199 | * nss_wifili_get_context() |
| 200 | */ |
| 201 | struct 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 | } |
| 205 | EXPORT_SYMBOL(nss_wifili_get_context); |
| 206 | |
| 207 | /* |
| 208 | * nss_wifili_msg_init() |
| 209 | * Initialize nss_wifili_msg. |
| 210 | */ |
| 211 | void 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 | } |
| 215 | EXPORT_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 | */ |
| 227 | struct 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 Paul | 1b170c2 | 2017-05-29 12:30:39 +0530 | [diff] [blame] | 238 | nss_info("nss_register_wifili_if if_num %d wifictx %p", if_num, netdev); |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 239 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 240 | nss_core_register_subsys_dp(nss_ctx, if_num, wifili_callback, wifili_ext_callback, NULL, netdev, features); |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 241 | |
| 242 | nss_top_main.wifili_msg_callback = event_callback; |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 243 | |
| 244 | return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 245 | } |
| 246 | EXPORT_SYMBOL(nss_register_wifili_if); |
| 247 | |
| 248 | /* |
| 249 | * nss_unregister_wifili_if() |
| 250 | * Unregister wifili with nss driver |
| 251 | */ |
| 252 | void 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 Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 261 | nss_core_unregister_subsys_dp(nss_ctx, if_num); |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 262 | } |
| 263 | EXPORT_SYMBOL(nss_unregister_wifili_if); |
| 264 | |
| 265 | /* |
| 266 | * nss_register_wifili_radio_if() |
| 267 | * Register wifili radio with nss driver |
| 268 | */ |
| 269 | struct 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 Paul | 1b170c2 | 2017-05-29 12:30:39 +0530 | [diff] [blame] | 279 | nss_info("nss_register_wifili_if if_num %d wifictx %p", if_num, netdev); |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 280 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 281 | nss_core_register_subsys_dp(nss_ctx, if_num, wifili_callback, wifili_ext_callback, NULL, netdev, features); |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 282 | |
| 283 | return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 284 | } |
| 285 | EXPORT_SYMBOL(nss_register_wifili_radio_if); |
| 286 | |
| 287 | /* |
| 288 | * nss_unregister_wifili_radio_if() |
| 289 | * Unregister wifili radio with nss driver |
| 290 | */ |
| 291 | void 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 Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 300 | nss_core_unregister_subsys_dp(nss_ctx, if_num); |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 301 | } |
| 302 | EXPORT_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 Paul | 1b170c2 | 2017-05-29 12:30:39 +0530 | [diff] [blame] | 308 | void nss_wifili_register_handler(void) |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 309 | { |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 310 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 311 | |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 312 | nss_info("nss_wifili_register_handler"); |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 313 | nss_core_register_handler(nss_ctx, NSS_WIFILI_INTERFACE, nss_wifili_handler, NULL); |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 314 | |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 315 | nss_wifili_stats_dentry_create(); |
| 316 | |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 317 | sema_init(&wifili_pvt.sem, 1); |
| 318 | init_completion(&wifili_pvt.complete); |
| 319 | } |