Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 3 | * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved. |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +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_wifi_stats.h" |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 19 | |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 20 | /* |
| 21 | * nss_wifi_get_context() |
| 22 | * Get NSS context of Wifi. |
| 23 | */ |
| 24 | struct nss_ctx_instance *nss_wifi_get_context() |
| 25 | { |
| 26 | return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 27 | } |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 28 | |
| 29 | /* |
| 30 | * nss_wifi_handler() |
| 31 | * Handle NSS -> HLOS messages for wifi |
| 32 | */ |
| 33 | static void nss_wifi_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data) |
| 34 | { |
| 35 | struct nss_wifi_msg *ntm = (struct nss_wifi_msg *)ncm; |
| 36 | void *ctx; |
| 37 | nss_wifi_msg_callback_t cb; |
| 38 | |
| 39 | nss_info("%p: NSS ->HLOS message for wifi\n", nss_ctx); |
| 40 | |
| 41 | BUG_ON(((ncm->interface < NSS_WIFI_INTERFACE0) || (ncm->interface > NSS_WIFI_INTERFACE2))); |
| 42 | |
| 43 | /* |
| 44 | * Is this a valid request/response packet? |
| 45 | */ |
| 46 | if (ncm->type >= NSS_WIFI_MAX_MSG) { |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 47 | nss_warning("%p: received invalid message %d for wifi interface", nss_ctx, ncm->type); |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 48 | return; |
| 49 | } |
| 50 | |
Suruchi Agarwal | ef8a870 | 2016-01-08 12:40:08 -0800 | [diff] [blame] | 51 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_wifi_msg)) { |
| 52 | nss_warning("%p: Length of message is greater than required: %d", nss_ctx, nss_cmn_get_msg_len(ncm)); |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 53 | return; |
| 54 | } |
| 55 | |
| 56 | /* |
| 57 | * Snoop messages for local driver and handle |
| 58 | */ |
| 59 | switch (ntm->cm.type) { |
| 60 | case NSS_WIFI_STATS_MSG: |
| 61 | /* |
| 62 | * To create the old API gmac statistics, we use the new extended GMAC stats. |
| 63 | */ |
| 64 | nss_wifi_stats_sync(nss_ctx, &ntm->msg.statsmsg, ncm->interface); |
| 65 | break; |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Update the callback and app_data for NOTIFY messages, wifi sends all notify messages |
| 70 | * to the same callback/app_data. |
| 71 | */ |
| 72 | if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) { |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 73 | ncm->cb = (nss_ptr_t)nss_ctx->nss_top->wifi_msg_callback; |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 74 | } |
| 75 | |
| 76 | /* |
| 77 | * Log failures |
| 78 | */ |
| 79 | nss_core_log_msg_failures(nss_ctx, ncm); |
| 80 | |
| 81 | /* |
| 82 | * Do we have a call back |
| 83 | */ |
| 84 | if (!ncm->cb) { |
| 85 | nss_info("%p: cb null for wifi interface %d", nss_ctx, ncm->interface); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | /* |
| 90 | * Get callback & context |
| 91 | */ |
| 92 | cb = (nss_wifi_msg_callback_t)ncm->cb; |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 93 | ctx = nss_ctx->subsys_dp_register[ncm->interface].ndev; |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | * call wifi msg callback |
| 97 | */ |
| 98 | if (!ctx) { |
| 99 | nss_warning("%p: Event received for wifi interface %d before registration", nss_ctx, ncm->interface); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | cb(ctx, ntm); |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * nss_wifi_tx_msg |
| 108 | * Transmit a wifi message to NSS FW |
| 109 | */ |
| 110 | nss_tx_status_t nss_wifi_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_wifi_msg *msg) |
| 111 | { |
| 112 | struct nss_wifi_msg *nm; |
| 113 | struct nss_cmn_msg *ncm = &msg->cm; |
| 114 | struct sk_buff *nbuf; |
| 115 | int32_t status; |
| 116 | |
| 117 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 118 | |
| 119 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
| 120 | nss_warning("%p: wifi message dropped as core not ready", nss_ctx); |
| 121 | return NSS_TX_FAILURE_NOT_READY; |
| 122 | } |
| 123 | |
| 124 | if (ncm->type > NSS_WIFI_MAX_MSG) { |
| 125 | nss_warning("%p: wifi message type out of range: %d", nss_ctx, ncm->type); |
| 126 | return NSS_TX_FAILURE; |
| 127 | } |
| 128 | |
Suruchi Agarwal | ef8a870 | 2016-01-08 12:40:08 -0800 | [diff] [blame] | 129 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_wifi_msg)) { |
| 130 | nss_warning("%p: wifi message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm)); |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 131 | return NSS_TX_FAILURE; |
| 132 | } |
| 133 | |
| 134 | nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE); |
| 135 | if (unlikely(!nbuf)) { |
| 136 | spin_lock_bh(&nss_ctx->nss_top->stats_lock); |
| 137 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]); |
| 138 | spin_unlock_bh(&nss_ctx->nss_top->stats_lock); |
| 139 | nss_warning("%p: wifi message dropped as command allocation failed", nss_ctx); |
| 140 | return NSS_TX_FAILURE; |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * Copy the message to our skb |
| 145 | */ |
| 146 | nm = (struct nss_wifi_msg *)skb_put(nbuf, sizeof(struct nss_wifi_msg)); |
| 147 | memcpy(nm, msg, sizeof(struct nss_wifi_msg)); |
| 148 | |
| 149 | status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0); |
| 150 | if (status != NSS_CORE_STATUS_SUCCESS) { |
| 151 | dev_kfree_skb_any(nbuf); |
| 152 | nss_warning("%p: Unable to enqueue 'wifi message'", nss_ctx); |
| 153 | return NSS_TX_FAILURE; |
| 154 | } |
| 155 | |
Stephen Wang | 90c67de | 2016-04-26 15:15:59 -0700 | [diff] [blame] | 156 | nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE); |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 157 | |
| 158 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]); |
| 159 | |
| 160 | return NSS_TX_SUCCESS; |
| 161 | } |
| 162 | |
| 163 | /* |
| 164 | **************************************** |
| 165 | * Register/Unregister/Miscellaneous APIs |
| 166 | **************************************** |
| 167 | */ |
| 168 | |
| 169 | /* |
| 170 | * nss_register_wifi_if() |
| 171 | * Register Wifi with nss driver |
| 172 | */ |
| 173 | struct nss_ctx_instance *nss_register_wifi_if(uint32_t if_num, nss_wifi_callback_t wifi_callback, |
Radha krishna Simha Jiguru | 427d603 | 2015-07-07 19:06:07 +0530 | [diff] [blame] | 174 | nss_wifi_callback_t wifi_ext_callback, |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 175 | nss_wifi_msg_callback_t event_callback, struct net_device *netdev, uint32_t features) |
| 176 | { |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 177 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 178 | |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 179 | nss_assert(nss_ctx); |
| 180 | nss_assert((if_num >= NSS_MAX_VIRTUAL_INTERFACES) && (if_num < NSS_MAX_NET_INTERFACES)); |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 181 | |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 182 | nss_info("%p: nss_register_wifi_if if_num %d wifictx %p", nss_ctx, if_num, netdev); |
| 183 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 184 | nss_core_register_subsys_dp(nss_ctx, if_num, wifi_callback, wifi_ext_callback, NULL, netdev, features); |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 185 | |
| 186 | nss_top_main.wifi_msg_callback = event_callback; |
| 187 | |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 188 | return nss_ctx; |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* |
| 192 | * nss_unregister_wifi_if() |
| 193 | * Unregister wifi with nss driver |
| 194 | */ |
| 195 | void nss_unregister_wifi_if(uint32_t if_num) |
| 196 | { |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 197 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 198 | |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 199 | nss_assert(nss_ctx); |
| 200 | nss_assert((if_num >= NSS_MAX_VIRTUAL_INTERFACES) && (if_num < NSS_MAX_NET_INTERFACES)); |
| 201 | |
Radha krishna Simha Jiguru | f85b579 | 2017-11-28 14:52:40 +0530 | [diff] [blame^] | 202 | nss_ctx->nss_top->wifi_msg_callback = NULL; |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 203 | nss_core_unregister_subsys_dp(nss_ctx, if_num); |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | /* |
| 207 | * nss_wifi_register_handler() |
| 208 | * Register handle for notfication messages received on wifi interface |
| 209 | */ |
| 210 | void nss_wifi_register_handler(void ) |
| 211 | { |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 212 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id]; |
| 213 | |
| 214 | nss_assert(nss_ctx); |
| 215 | |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 216 | nss_info("nss_wifi_register_handler"); |
| 217 | |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 218 | nss_core_register_handler(nss_ctx, NSS_WIFI_INTERFACE0, nss_wifi_handler, NULL); |
| 219 | nss_core_register_handler(nss_ctx, NSS_WIFI_INTERFACE1, nss_wifi_handler, NULL); |
| 220 | nss_core_register_handler(nss_ctx, NSS_WIFI_INTERFACE2, nss_wifi_handler, NULL); |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 221 | |
| 222 | nss_wifi_stats_dentry_create(); |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 223 | } |
| 224 | |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 225 | EXPORT_SYMBOL(nss_wifi_get_context); |
Bharath M Kumar | cc666e9 | 2014-12-24 19:17:28 +0530 | [diff] [blame] | 226 | EXPORT_SYMBOL(nss_wifi_tx_msg); |
| 227 | EXPORT_SYMBOL(nss_register_wifi_if); |
| 228 | EXPORT_SYMBOL(nss_unregister_wifi_if); |