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" |
| 18 | |
| 19 | #define NSS_WIFILI_TX_TIMEOUT 1000 /* Millisecond to jiffies*/ |
| 20 | |
| 21 | /* |
| 22 | * nss_wifili_pvt |
| 23 | * Private data structure |
| 24 | */ |
| 25 | static struct nss_wifili_pvt { |
| 26 | struct semaphore sem; |
| 27 | struct completion complete; |
| 28 | int response; |
| 29 | void *cb; |
| 30 | void *app_data; |
| 31 | } wifili_pvt; |
| 32 | |
| 33 | /* |
| 34 | * nss_wifili_stats_sync() |
| 35 | * Handle the syncing of WIFI stats. |
| 36 | */ |
| 37 | static void nss_wifili_stats_sync(struct nss_ctx_instance *nss_ctx, |
| 38 | struct nss_wifili_stats_sync_msg *stats, uint16_t interface) |
| 39 | { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | /* |
| 44 | * nss_wifili_handler() |
| 45 | * Handle NSS -> HLOS messages for wifi |
| 46 | */ |
| 47 | static void nss_wifili_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data) |
| 48 | { |
| 49 | struct nss_wifili_msg *ntm = (struct nss_wifili_msg *)ncm; |
| 50 | void *ctx; |
| 51 | nss_wifili_msg_callback_t cb; |
| 52 | |
| 53 | nss_info("%p: NSS->HLOS message for wifili\n", nss_ctx); |
| 54 | |
| 55 | /* |
| 56 | * The interface number shall be wifili soc interface or wifili radio interface |
| 57 | */ |
| 58 | BUG_ON((nss_is_dynamic_interface(ncm->interface)) || ncm->interface != NSS_WIFILI_INTERFACE); |
| 59 | |
| 60 | /* |
| 61 | * Is this a valid request/response packet? |
| 62 | */ |
| 63 | if (ncm->type >= NSS_WIFILI_MAX_MSG) { |
| 64 | nss_warning("%p: Received invalid message %d for wifili interface", nss_ctx, ncm->type); |
| 65 | return; |
| 66 | } |
| 67 | |
| 68 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_wifili_msg)) { |
| 69 | nss_warning("%p: Length of message is greater than required: %d", nss_ctx, nss_cmn_get_msg_len(ncm)); |
| 70 | return; |
| 71 | } |
| 72 | |
| 73 | /* |
| 74 | * Snoop messages for local driver and handle |
| 75 | */ |
| 76 | switch (ntm->cm.type) { |
| 77 | case NSS_WIFILI_PDEV_STATS_SYNC_MSG: |
| 78 | nss_wifili_stats_sync(nss_ctx, &ntm->msg.wlsoc_stats, ncm->interface); |
| 79 | break; |
| 80 | } |
| 81 | |
| 82 | /* |
| 83 | * Update the callback and app_data for notify messages, wifili sends all notify messages |
| 84 | * to the same callback/app_data. |
| 85 | */ |
| 86 | if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) { |
| 87 | ncm->cb = (nss_ptr_t)nss_ctx->nss_top->wifili_msg_callback; |
| 88 | } |
| 89 | |
| 90 | /* |
| 91 | * Log failures |
| 92 | */ |
| 93 | nss_core_log_msg_failures(nss_ctx, ncm); |
| 94 | |
| 95 | /* |
| 96 | * Do we have a call back |
| 97 | */ |
| 98 | if (!ncm->cb) { |
| 99 | nss_info("%p: cb null for wifili interface %d", nss_ctx, ncm->interface); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | /* |
| 104 | * Get callback & context |
| 105 | */ |
| 106 | cb = (nss_wifili_msg_callback_t)ncm->cb; |
| 107 | ctx = nss_ctx->subsys_dp_register[ncm->interface].ndev; |
| 108 | |
| 109 | /* |
| 110 | * call wifili msg callback |
| 111 | */ |
| 112 | if (!ctx) { |
| 113 | nss_warning("%p: Event received for wifili interface %d before registration", nss_ctx, ncm->interface); |
| 114 | return; |
| 115 | } |
| 116 | |
| 117 | cb(ctx, ntm); |
| 118 | } |
| 119 | |
| 120 | /* |
| 121 | * nss_wifili_callback() |
| 122 | * Callback to handle the completion of NSS->HLOS messages. |
| 123 | */ |
| 124 | static void nss_wifili_callback(void *app_data, struct nss_wifili_msg *nvm) |
| 125 | { |
| 126 | nss_wifili_msg_callback_t callback = (nss_wifili_msg_callback_t)wifili_pvt.cb; |
| 127 | void *data = wifili_pvt.app_data; |
| 128 | |
| 129 | wifili_pvt.response = NSS_TX_SUCCESS; |
| 130 | wifili_pvt.cb = NULL; |
| 131 | wifili_pvt.app_data = NULL; |
| 132 | |
| 133 | if (nvm->cm.response != NSS_CMN_RESPONSE_ACK) { |
| 134 | nss_warning("wifili error response %d\n", nvm->cm.response); |
| 135 | wifili_pvt.response = nvm->cm.response; |
| 136 | } |
| 137 | |
| 138 | if (callback) { |
| 139 | callback(data, nvm); |
| 140 | } |
| 141 | complete(&wifili_pvt.complete); |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * nss_wifili_tx_msg |
| 146 | * Transmit a wifili message to NSS FW |
| 147 | * |
| 148 | * NOTE: The caller is expected to handle synchronous wait for message |
| 149 | * response if needed. |
| 150 | */ |
| 151 | nss_tx_status_t nss_wifili_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_wifili_msg *msg) |
| 152 | { |
| 153 | struct nss_wifili_msg *nm; |
| 154 | struct nss_cmn_msg *ncm = &msg->cm; |
| 155 | struct sk_buff *nbuf; |
| 156 | int32_t status; |
| 157 | |
| 158 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 159 | |
| 160 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
| 161 | nss_warning("%p: wifili message dropped as core not ready", nss_ctx); |
| 162 | return NSS_TX_FAILURE_NOT_READY; |
| 163 | } |
| 164 | |
| 165 | if (ncm->type >= NSS_WIFILI_MAX_MSG) { |
| 166 | nss_warning("%p: wifili message type out of range: %d", nss_ctx, ncm->type); |
| 167 | return NSS_TX_FAILURE; |
| 168 | } |
| 169 | |
| 170 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_wifili_msg)) { |
| 171 | nss_warning("%p: wifili message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm)); |
| 172 | return NSS_TX_FAILURE; |
| 173 | } |
| 174 | |
| 175 | /* |
| 176 | * The interface number shall be wifili soc interface or wifili radio interface |
| 177 | */ |
| 178 | if (ncm->interface != NSS_WIFILI_INTERFACE) { |
| 179 | nss_warning("%p: tx request for interface that is not a wifili: %d", nss_ctx, ncm->interface); |
| 180 | return NSS_TX_FAILURE; |
| 181 | } |
| 182 | |
| 183 | nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE); |
| 184 | if (unlikely(!nbuf)) { |
| 185 | spin_lock_bh(&nss_ctx->nss_top->stats_lock); |
| 186 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]); |
| 187 | spin_unlock_bh(&nss_ctx->nss_top->stats_lock); |
| 188 | nss_warning("%p: wifili message failed as command allocation failed", nss_ctx); |
| 189 | return NSS_TX_FAILURE; |
| 190 | } |
| 191 | |
| 192 | /* |
| 193 | * Copy the message to our skb |
| 194 | */ |
| 195 | nm = (struct nss_wifili_msg *)skb_put(nbuf, sizeof(struct nss_wifili_msg)); |
| 196 | memcpy(nm, msg, sizeof(struct nss_wifili_msg)); |
| 197 | |
| 198 | status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0); |
| 199 | if (status != NSS_CORE_STATUS_SUCCESS) { |
| 200 | dev_kfree_skb_any(nbuf); |
| 201 | nss_warning("%p: Unable to enqueue 'wifili message'", nss_ctx); |
| 202 | return NSS_TX_FAILURE; |
| 203 | } |
| 204 | |
| 205 | nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE); |
| 206 | |
| 207 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]); |
| 208 | |
| 209 | return NSS_TX_SUCCESS; |
| 210 | } |
| 211 | EXPORT_SYMBOL(nss_wifili_tx_msg); |
| 212 | |
| 213 | /* |
| 214 | * nss_wifili_tx_msg_sync() |
| 215 | * Transmit a wifili message to NSS firmware synchronously. |
| 216 | */ |
| 217 | nss_tx_status_t nss_wifili_tx_msg_sync(struct nss_ctx_instance *nss_ctx, struct nss_wifili_msg *nvm) |
| 218 | { |
| 219 | nss_tx_status_t status; |
| 220 | int ret = 0; |
| 221 | |
| 222 | down(&wifili_pvt.sem); |
| 223 | wifili_pvt.cb = (void *)nvm->cm.cb; |
| 224 | wifili_pvt.app_data = (void *)nvm->cm.app_data; |
| 225 | |
| 226 | nvm->cm.cb = (nss_ptr_t)nss_wifili_callback; |
| 227 | nvm->cm.app_data = (nss_ptr_t)NULL; |
| 228 | |
| 229 | status = nss_wifili_tx_msg(nss_ctx, nvm); |
| 230 | if (status != NSS_TX_SUCCESS) { |
| 231 | nss_warning("%p: wifili_tx_msg failed\n", nss_ctx); |
| 232 | up(&wifili_pvt.sem); |
| 233 | return status; |
| 234 | } |
| 235 | |
| 236 | ret = wait_for_completion_timeout(&wifili_pvt.complete, msecs_to_jiffies(NSS_WIFILI_TX_TIMEOUT)); |
| 237 | if (!ret) { |
| 238 | nss_warning("%p: wifili msg tx failed due to timeout\n", nss_ctx); |
| 239 | wifili_pvt.response = NSS_TX_FAILURE; |
| 240 | } |
| 241 | |
| 242 | status = wifili_pvt.response; |
| 243 | up(&wifili_pvt.sem); |
| 244 | return status; |
| 245 | } |
| 246 | EXPORT_SYMBOL(nss_wifili_tx_msg_sync); |
| 247 | |
| 248 | /* |
| 249 | * nss_wifili_get_context() |
| 250 | */ |
| 251 | struct nss_ctx_instance *nss_wifili_get_context(void) |
| 252 | { |
| 253 | return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 254 | } |
| 255 | EXPORT_SYMBOL(nss_wifili_get_context); |
| 256 | |
| 257 | /* |
| 258 | * nss_wifili_msg_init() |
| 259 | * Initialize nss_wifili_msg. |
| 260 | */ |
| 261 | 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) |
| 262 | { |
| 263 | nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data); |
| 264 | } |
| 265 | EXPORT_SYMBOL(nss_wifili_msg_init); |
| 266 | |
| 267 | /* |
| 268 | **************************************** |
| 269 | * Register/Unregister/Miscellaneous APIs |
| 270 | **************************************** |
| 271 | */ |
| 272 | |
| 273 | /* |
| 274 | * nss_register_wifili_if() |
| 275 | * Register wifili with nss driver |
| 276 | */ |
| 277 | struct nss_ctx_instance *nss_register_wifili_if(uint32_t if_num, nss_wifili_callback_t wifili_callback, |
| 278 | nss_wifili_callback_t wifili_ext_callback, |
| 279 | nss_wifili_msg_callback_t event_callback, struct net_device *netdev, uint32_t features) |
| 280 | { |
| 281 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 282 | |
| 283 | /* |
| 284 | * The interface number shall be wifili soc interface |
| 285 | */ |
| 286 | nss_assert(if_num == NSS_WIFILI_INTERFACE); |
| 287 | |
| 288 | nss_info("nss_register_wifili_if if_num %d wifictx %p", if_num, netdev); |
| 289 | |
| 290 | nss_ctx->subsys_dp_register[if_num].ndev = netdev; |
| 291 | nss_ctx->subsys_dp_register[if_num].cb = wifili_callback; |
| 292 | nss_ctx->subsys_dp_register[if_num].ext_cb = wifili_ext_callback; |
| 293 | nss_ctx->subsys_dp_register[if_num].app_data = NULL; |
| 294 | nss_ctx->subsys_dp_register[if_num].features = features; |
| 295 | |
| 296 | nss_top_main.wifili_msg_callback = event_callback; |
| 297 | nss_core_register_handler(if_num, nss_wifili_handler, NULL); |
| 298 | |
| 299 | return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 300 | } |
| 301 | EXPORT_SYMBOL(nss_register_wifili_if); |
| 302 | |
| 303 | /* |
| 304 | * nss_unregister_wifili_if() |
| 305 | * Unregister wifili with nss driver |
| 306 | */ |
| 307 | void nss_unregister_wifili_if(uint32_t if_num) |
| 308 | { |
| 309 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 310 | |
| 311 | /* |
| 312 | * The interface number shall be wifili soc interface |
| 313 | */ |
| 314 | nss_assert(if_num == NSS_WIFILI_INTERFACE); |
| 315 | |
| 316 | nss_ctx->subsys_dp_register[if_num].ndev = NULL; |
| 317 | nss_ctx->subsys_dp_register[if_num].cb = NULL; |
| 318 | nss_ctx->subsys_dp_register[if_num].ext_cb = NULL; |
| 319 | nss_ctx->subsys_dp_register[if_num].app_data = NULL; |
| 320 | nss_ctx->subsys_dp_register[if_num].features = 0; |
| 321 | } |
| 322 | EXPORT_SYMBOL(nss_unregister_wifili_if); |
| 323 | |
| 324 | /* |
| 325 | * nss_register_wifili_radio_if() |
| 326 | * Register wifili radio with nss driver |
| 327 | */ |
| 328 | struct nss_ctx_instance *nss_register_wifili_radio_if(uint32_t if_num, nss_wifili_callback_t wifili_callback, |
| 329 | nss_wifili_callback_t wifili_ext_callback, |
| 330 | nss_wifili_msg_callback_t event_callback, struct net_device *netdev, uint32_t features) |
| 331 | { |
| 332 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 333 | |
| 334 | /* |
| 335 | * The interface number shall be wifili radio dynamic interface |
| 336 | */ |
| 337 | nss_assert(nss_is_dynamic_interface(if_num)); |
| 338 | nss_info("nss_register_wifili_if if_num %d wifictx %p", if_num, netdev); |
| 339 | |
| 340 | nss_ctx->subsys_dp_register[if_num].ndev = netdev; |
| 341 | nss_ctx->subsys_dp_register[if_num].cb = wifili_callback; |
| 342 | nss_ctx->subsys_dp_register[if_num].ext_cb = wifili_ext_callback; |
| 343 | nss_ctx->subsys_dp_register[if_num].app_data = NULL; |
| 344 | nss_ctx->subsys_dp_register[if_num].features = features; |
| 345 | |
| 346 | return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 347 | } |
| 348 | EXPORT_SYMBOL(nss_register_wifili_radio_if); |
| 349 | |
| 350 | /* |
| 351 | * nss_unregister_wifili_radio_if() |
| 352 | * Unregister wifili radio with nss driver |
| 353 | */ |
| 354 | void nss_unregister_wifili_radio_if(uint32_t if_num) |
| 355 | { |
| 356 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 357 | |
| 358 | /* |
| 359 | * The interface number shall be wifili radio dynamic interface |
| 360 | */ |
| 361 | nss_assert(nss_is_dynamic_interface(if_num)); |
| 362 | |
| 363 | nss_ctx->subsys_dp_register[if_num].ndev = NULL; |
| 364 | nss_ctx->subsys_dp_register[if_num].cb = NULL; |
| 365 | nss_ctx->subsys_dp_register[if_num].ext_cb = NULL; |
| 366 | nss_ctx->subsys_dp_register[if_num].app_data = NULL; |
| 367 | nss_ctx->subsys_dp_register[if_num].features = 0; |
| 368 | } |
| 369 | EXPORT_SYMBOL(nss_unregister_wifili_radio_if); |
| 370 | |
| 371 | /* |
| 372 | * nss_wifili_register_handler() |
| 373 | * Register handle for notfication messages received on wifi interface |
| 374 | */ |
| 375 | void nss_wifili_register_handler(void ) |
| 376 | { |
| 377 | nss_info("nss_wifili_register_handler"); |
| 378 | nss_core_register_handler(NSS_WIFILI_INTERFACE, nss_wifili_handler, NULL); |
| 379 | |
| 380 | sema_init(&wifili_pvt.sem, 1); |
| 381 | init_completion(&wifili_pvt.complete); |
| 382 | } |