Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
| 3 | * Copyright (c) 2017, The Linux Foundation. All rights reserved. |
| 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 | */ |
| 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 | */ |
| 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 | { |
| 144 | struct nss_wifili_msg *nm; |
| 145 | struct nss_cmn_msg *ncm = &msg->cm; |
| 146 | struct sk_buff *nbuf; |
| 147 | int32_t status; |
| 148 | |
| 149 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 150 | |
| 151 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
| 152 | nss_warning("%p: wifili message dropped as core not ready", nss_ctx); |
| 153 | return NSS_TX_FAILURE_NOT_READY; |
| 154 | } |
| 155 | |
| 156 | if (ncm->type >= NSS_WIFILI_MAX_MSG) { |
| 157 | nss_warning("%p: wifili message type out of range: %d", nss_ctx, ncm->type); |
| 158 | return NSS_TX_FAILURE; |
| 159 | } |
| 160 | |
| 161 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_wifili_msg)) { |
| 162 | nss_warning("%p: wifili message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm)); |
| 163 | return NSS_TX_FAILURE; |
| 164 | } |
| 165 | |
| 166 | /* |
| 167 | * The interface number shall be wifili soc interface or wifili radio interface |
| 168 | */ |
| 169 | if (ncm->interface != NSS_WIFILI_INTERFACE) { |
| 170 | nss_warning("%p: tx request for interface that is not a wifili: %d", nss_ctx, ncm->interface); |
| 171 | return NSS_TX_FAILURE; |
| 172 | } |
| 173 | |
| 174 | nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE); |
| 175 | if (unlikely(!nbuf)) { |
| 176 | spin_lock_bh(&nss_ctx->nss_top->stats_lock); |
| 177 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]); |
| 178 | spin_unlock_bh(&nss_ctx->nss_top->stats_lock); |
| 179 | nss_warning("%p: wifili message failed as command allocation failed", nss_ctx); |
| 180 | return NSS_TX_FAILURE; |
| 181 | } |
| 182 | |
| 183 | /* |
| 184 | * Copy the message to our skb |
| 185 | */ |
| 186 | nm = (struct nss_wifili_msg *)skb_put(nbuf, sizeof(struct nss_wifili_msg)); |
| 187 | memcpy(nm, msg, sizeof(struct nss_wifili_msg)); |
| 188 | |
| 189 | status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0); |
| 190 | if (status != NSS_CORE_STATUS_SUCCESS) { |
| 191 | dev_kfree_skb_any(nbuf); |
| 192 | nss_warning("%p: Unable to enqueue 'wifili message'", nss_ctx); |
| 193 | return NSS_TX_FAILURE; |
| 194 | } |
| 195 | |
| 196 | nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE); |
| 197 | |
| 198 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]); |
| 199 | |
| 200 | return NSS_TX_SUCCESS; |
| 201 | } |
| 202 | EXPORT_SYMBOL(nss_wifili_tx_msg); |
| 203 | |
| 204 | /* |
| 205 | * nss_wifili_tx_msg_sync() |
| 206 | * Transmit a wifili message to NSS firmware synchronously. |
| 207 | */ |
| 208 | nss_tx_status_t nss_wifili_tx_msg_sync(struct nss_ctx_instance *nss_ctx, struct nss_wifili_msg *nvm) |
| 209 | { |
| 210 | nss_tx_status_t status; |
| 211 | int ret = 0; |
| 212 | |
| 213 | down(&wifili_pvt.sem); |
| 214 | wifili_pvt.cb = (void *)nvm->cm.cb; |
| 215 | wifili_pvt.app_data = (void *)nvm->cm.app_data; |
| 216 | |
| 217 | nvm->cm.cb = (nss_ptr_t)nss_wifili_callback; |
| 218 | nvm->cm.app_data = (nss_ptr_t)NULL; |
| 219 | |
| 220 | status = nss_wifili_tx_msg(nss_ctx, nvm); |
| 221 | if (status != NSS_TX_SUCCESS) { |
| 222 | nss_warning("%p: wifili_tx_msg failed\n", nss_ctx); |
| 223 | up(&wifili_pvt.sem); |
| 224 | return status; |
| 225 | } |
| 226 | |
| 227 | ret = wait_for_completion_timeout(&wifili_pvt.complete, msecs_to_jiffies(NSS_WIFILI_TX_TIMEOUT)); |
| 228 | if (!ret) { |
| 229 | nss_warning("%p: wifili msg tx failed due to timeout\n", nss_ctx); |
| 230 | wifili_pvt.response = NSS_TX_FAILURE; |
| 231 | } |
| 232 | |
| 233 | status = wifili_pvt.response; |
| 234 | up(&wifili_pvt.sem); |
| 235 | return status; |
| 236 | } |
| 237 | EXPORT_SYMBOL(nss_wifili_tx_msg_sync); |
| 238 | |
| 239 | /* |
| 240 | * nss_wifili_get_context() |
| 241 | */ |
| 242 | struct nss_ctx_instance *nss_wifili_get_context(void) |
| 243 | { |
| 244 | return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 245 | } |
| 246 | EXPORT_SYMBOL(nss_wifili_get_context); |
| 247 | |
| 248 | /* |
| 249 | * nss_wifili_msg_init() |
| 250 | * Initialize nss_wifili_msg. |
| 251 | */ |
| 252 | 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) |
| 253 | { |
| 254 | nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data); |
| 255 | } |
| 256 | EXPORT_SYMBOL(nss_wifili_msg_init); |
| 257 | |
| 258 | /* |
| 259 | **************************************** |
| 260 | * Register/Unregister/Miscellaneous APIs |
| 261 | **************************************** |
| 262 | */ |
| 263 | |
| 264 | /* |
| 265 | * nss_register_wifili_if() |
| 266 | * Register wifili with nss driver |
| 267 | */ |
| 268 | struct nss_ctx_instance *nss_register_wifili_if(uint32_t if_num, nss_wifili_callback_t wifili_callback, |
| 269 | nss_wifili_callback_t wifili_ext_callback, |
| 270 | nss_wifili_msg_callback_t event_callback, struct net_device *netdev, uint32_t features) |
| 271 | { |
| 272 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 273 | |
| 274 | /* |
| 275 | * The interface number shall be wifili soc interface |
| 276 | */ |
| 277 | nss_assert(if_num == NSS_WIFILI_INTERFACE); |
| 278 | |
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 | nss_top_main.wifili_msg_callback = event_callback; |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 284 | |
| 285 | return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 286 | } |
| 287 | EXPORT_SYMBOL(nss_register_wifili_if); |
| 288 | |
| 289 | /* |
| 290 | * nss_unregister_wifili_if() |
| 291 | * Unregister wifili with nss driver |
| 292 | */ |
| 293 | void nss_unregister_wifili_if(uint32_t if_num) |
| 294 | { |
| 295 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 296 | |
| 297 | /* |
| 298 | * The interface number shall be wifili soc interface |
| 299 | */ |
| 300 | nss_assert(if_num == NSS_WIFILI_INTERFACE); |
| 301 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 302 | nss_core_unregister_subsys_dp(nss_ctx, if_num); |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 303 | } |
| 304 | EXPORT_SYMBOL(nss_unregister_wifili_if); |
| 305 | |
| 306 | /* |
| 307 | * nss_register_wifili_radio_if() |
| 308 | * Register wifili radio with nss driver |
| 309 | */ |
| 310 | struct nss_ctx_instance *nss_register_wifili_radio_if(uint32_t if_num, nss_wifili_callback_t wifili_callback, |
| 311 | nss_wifili_callback_t wifili_ext_callback, |
| 312 | nss_wifili_msg_callback_t event_callback, struct net_device *netdev, uint32_t features) |
| 313 | { |
| 314 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 315 | |
| 316 | /* |
| 317 | * The interface number shall be wifili radio dynamic interface |
| 318 | */ |
| 319 | nss_assert(nss_is_dynamic_interface(if_num)); |
Aniruddha Paul | 1b170c2 | 2017-05-29 12:30:39 +0530 | [diff] [blame] | 320 | 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] | 321 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 322 | 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] | 323 | |
| 324 | return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 325 | } |
| 326 | EXPORT_SYMBOL(nss_register_wifili_radio_if); |
| 327 | |
| 328 | /* |
| 329 | * nss_unregister_wifili_radio_if() |
| 330 | * Unregister wifili radio with nss driver |
| 331 | */ |
| 332 | void nss_unregister_wifili_radio_if(uint32_t if_num) |
| 333 | { |
| 334 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 335 | |
| 336 | /* |
| 337 | * The interface number shall be wifili radio dynamic interface |
| 338 | */ |
| 339 | nss_assert(nss_is_dynamic_interface(if_num)); |
| 340 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 341 | nss_core_unregister_subsys_dp(nss_ctx, if_num); |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 342 | } |
| 343 | EXPORT_SYMBOL(nss_unregister_wifili_radio_if); |
| 344 | |
| 345 | /* |
| 346 | * nss_wifili_register_handler() |
| 347 | * Register handle for notfication messages received on wifi interface |
| 348 | */ |
Aniruddha Paul | 1b170c2 | 2017-05-29 12:30:39 +0530 | [diff] [blame] | 349 | void nss_wifili_register_handler(void) |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 350 | { |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 351 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 352 | |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 353 | nss_info("nss_wifili_register_handler"); |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 354 | 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] | 355 | |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 356 | nss_wifili_stats_dentry_create(); |
| 357 | |
Varsha Mishra | e58d93b | 2017-05-20 20:54:41 +0530 | [diff] [blame] | 358 | sema_init(&wifili_pvt.sem, 1); |
| 359 | init_completion(&wifili_pvt.complete); |
| 360 | } |