Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Murat Sezgin | 8fb2f36 | 2019-01-22 10:44:53 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2019, The Linux Foundation. All rights reserved. |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +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 | /* |
| 18 | * nss_cmn.c |
| 19 | * NSS generic APIs |
| 20 | */ |
| 21 | |
Radha krishna Simha Jiguru | 7098a96 | 2015-12-09 11:18:23 +0530 | [diff] [blame] | 22 | #if (NSS_DT_SUPPORT == 1) |
| 23 | #include <linux/of.h> |
| 24 | #endif |
| 25 | |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 26 | #include "nss_tx_rx_common.h" |
| 27 | |
| 28 | /* |
Suruchi Agarwal | e3940e7 | 2016-07-06 15:56:51 -0700 | [diff] [blame] | 29 | * nss_cmn_response_str |
| 30 | * Common response structure string |
| 31 | */ |
| 32 | int8_t *nss_cmn_response_str[NSS_CMN_RESPONSE_LAST] = { |
| 33 | "Message Acknowledge without errors", |
| 34 | "Common message version not supported", |
| 35 | "Unknown Interface", |
| 36 | "Length Error", |
| 37 | "Message Error", |
| 38 | "FW Notification Message", |
| 39 | }; |
| 40 | |
| 41 | /* |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 42 | * nss_cmn_msg_init() |
Casey Chen | 3589c45 | 2018-09-20 15:27:41 -0700 | [diff] [blame] | 43 | * Initialize the common message of an ASYNC message. |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 44 | */ |
Shashank Balashankar | 497b015 | 2017-09-07 11:26:10 -0700 | [diff] [blame] | 45 | void nss_cmn_msg_init(struct nss_cmn_msg *ncm, uint32_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data) |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 46 | { |
| 47 | ncm->interface = if_num; |
| 48 | ncm->version = NSS_HLOS_MESSAGE_VERSION; |
| 49 | ncm->type = type; |
| 50 | ncm->len = len; |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 51 | ncm->cb = (nss_ptr_t)cb; |
| 52 | ncm->app_data = (nss_ptr_t)app_data; |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 53 | } |
Stephen Wang | a73a789 | 2017-07-05 15:23:14 -0700 | [diff] [blame] | 54 | EXPORT_SYMBOL(nss_cmn_msg_init); |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 55 | |
| 56 | /* |
Casey Chen | 3589c45 | 2018-09-20 15:27:41 -0700 | [diff] [blame] | 57 | * nss_cmn_msg_sync_init() |
| 58 | * Initialize the common message of a SYNC message. |
| 59 | */ |
| 60 | void nss_cmn_msg_sync_init(struct nss_cmn_msg *ncm, uint32_t if_num, uint32_t type, uint32_t len) |
| 61 | { |
| 62 | nss_cmn_msg_init(ncm, if_num, type, len, NULL, NULL); |
| 63 | } |
| 64 | EXPORT_SYMBOL(nss_cmn_msg_sync_init); |
| 65 | |
| 66 | /* |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 67 | * nss_cmn_get_interface_number() |
| 68 | * Return the interface number of the NSS net_device. |
| 69 | * |
| 70 | * Returns -1 on failure or the interface number of dev is an NSS net_device. |
| 71 | */ |
| 72 | int32_t nss_cmn_get_interface_number(struct nss_ctx_instance *nss_ctx, struct net_device *dev) |
| 73 | { |
| 74 | int i; |
| 75 | |
| 76 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 77 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
Selin Dag | b4a99d8 | 2018-08-30 09:19:54 -0700 | [diff] [blame] | 78 | nss_warning("%p: Interface number could not be found as core not ready\n", nss_ctx); |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 79 | return -1; |
| 80 | } |
| 81 | |
| 82 | nss_assert(dev != 0); |
| 83 | |
| 84 | /* |
| 85 | * Check physical interface table |
| 86 | */ |
| 87 | for (i = 0; i < NSS_MAX_NET_INTERFACES; i++) { |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 88 | if (dev == nss_ctx->subsys_dp_register[i].ndev) { |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 89 | return i; |
| 90 | } |
| 91 | } |
| 92 | |
Selin Dag | b4a99d8 | 2018-08-30 09:19:54 -0700 | [diff] [blame] | 93 | nss_warning("%p: Interface number could not be found as interface has not registered yet\n", nss_ctx); |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 94 | return -1; |
| 95 | } |
Stephen Wang | a73a789 | 2017-07-05 15:23:14 -0700 | [diff] [blame] | 96 | EXPORT_SYMBOL(nss_cmn_get_interface_number); |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 97 | |
| 98 | /* |
| 99 | * nss_cmn_get_interface_dev() |
| 100 | * Return the net_device for NSS interface id. |
| 101 | * |
| 102 | * Returns NULL on failure or the net_device for NSS interface id. |
| 103 | */ |
| 104 | struct net_device *nss_cmn_get_interface_dev(struct nss_ctx_instance *ctx, uint32_t if_num) |
| 105 | { |
| 106 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)ctx; |
| 107 | |
| 108 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 109 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
Selin Dag | b4a99d8 | 2018-08-30 09:19:54 -0700 | [diff] [blame] | 110 | nss_warning("%p: Interface device could not be found as core not ready\n", nss_ctx); |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 111 | return NULL; |
| 112 | } |
| 113 | |
| 114 | if (unlikely(if_num >= NSS_MAX_NET_INTERFACES)) { |
| 115 | return NULL; |
| 116 | } |
| 117 | |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 118 | return nss_ctx->subsys_dp_register[if_num].ndev; |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 119 | } |
Stephen Wang | a73a789 | 2017-07-05 15:23:14 -0700 | [diff] [blame] | 120 | EXPORT_SYMBOL(nss_cmn_get_interface_dev); |
| 121 | |
| 122 | /* |
| 123 | * nss_cmn_get_interface_number_by_dev_and_type() |
| 124 | * Return the NSS interface id for the net_device. |
| 125 | * |
| 126 | * Returns < 0 on failure or the NSS interface id for the given device and type. |
| 127 | */ |
| 128 | int32_t nss_cmn_get_interface_number_by_dev_and_type(struct net_device *dev, uint32_t type) |
| 129 | { |
| 130 | int i, core; |
| 131 | struct nss_subsystem_dataplane_register *nsdr; |
| 132 | |
| 133 | nss_assert(dev != 0); |
Radha krishna Simha Jiguru | 68d0cbc | 2019-02-15 00:19:21 +0530 | [diff] [blame^] | 134 | for (core = 0; core < nss_top_main.num_nss; core++) { |
Stephen Wang | a73a789 | 2017-07-05 15:23:14 -0700 | [diff] [blame] | 135 | for (i = 0; i < NSS_MAX_NET_INTERFACES; i++) { |
| 136 | nsdr = &nss_top_main.nss[core].subsys_dp_register[i]; |
| 137 | if (dev == nsdr->ndev && type == nsdr->type) { |
| 138 | return i; |
| 139 | } |
| 140 | } |
| 141 | } |
| 142 | |
Selin Dag | b4a99d8 | 2018-08-30 09:19:54 -0700 | [diff] [blame] | 143 | nss_warning("Interface number could not be found for %p (%s) as interface has not registered yet\n", dev, dev->name); |
Stephen Wang | a73a789 | 2017-07-05 15:23:14 -0700 | [diff] [blame] | 144 | return -1; |
| 145 | } |
| 146 | EXPORT_SYMBOL(nss_cmn_get_interface_number_by_dev_and_type); |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 147 | |
| 148 | /* |
Gareth Williams | b52af51 | 2014-04-25 19:31:15 +0100 | [diff] [blame] | 149 | * nss_cmn_get_interface_number_by_dev() |
| 150 | * Return the NSS interface id for the net_device. |
| 151 | * |
| 152 | * Returns < 0 on failure or the NSS interface id for the given device. |
| 153 | */ |
| 154 | int32_t nss_cmn_get_interface_number_by_dev(struct net_device *dev) |
| 155 | { |
Stephen Wang | a73a789 | 2017-07-05 15:23:14 -0700 | [diff] [blame] | 156 | return nss_cmn_get_interface_number_by_dev_and_type(dev, 0); |
Gareth Williams | b52af51 | 2014-04-25 19:31:15 +0100 | [diff] [blame] | 157 | } |
Stephen Wang | a73a789 | 2017-07-05 15:23:14 -0700 | [diff] [blame] | 158 | EXPORT_SYMBOL(nss_cmn_get_interface_number_by_dev); |
Gareth Williams | b52af51 | 2014-04-25 19:31:15 +0100 | [diff] [blame] | 159 | |
| 160 | /* |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 161 | * nss_cmn_get_state() |
| 162 | * return the NSS initialization state |
| 163 | */ |
| 164 | nss_state_t nss_cmn_get_state(struct nss_ctx_instance *ctx) |
| 165 | { |
| 166 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)ctx; |
| 167 | nss_state_t state = NSS_STATE_UNINITIALIZED; |
| 168 | |
| 169 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 170 | spin_lock_bh(&nss_top_main.lock); |
| 171 | if (nss_ctx->state == NSS_CORE_STATE_INITIALIZED) { |
| 172 | state = NSS_STATE_INITIALIZED; |
| 173 | } |
| 174 | spin_unlock_bh(&nss_top_main.lock); |
| 175 | |
| 176 | return state; |
| 177 | } |
Stephen Wang | a73a789 | 2017-07-05 15:23:14 -0700 | [diff] [blame] | 178 | EXPORT_SYMBOL(nss_cmn_get_state); |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 179 | |
| 180 | /* |
Sakthi Vignesh Radhakrishnan | 2842f9c | 2017-07-27 13:56:07 -0700 | [diff] [blame] | 181 | * nss_cmn_interface_is_redirect() |
| 182 | * Return true if the interface is a redirect interface. |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 183 | */ |
Sakthi Vignesh Radhakrishnan | 2842f9c | 2017-07-27 13:56:07 -0700 | [diff] [blame] | 184 | bool nss_cmn_interface_is_redirect(struct nss_ctx_instance *nss_ctx, int32_t interface_num) |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 185 | { |
Casey Chen | d90d0e6 | 2017-11-06 13:21:26 -0800 | [diff] [blame] | 186 | enum nss_dynamic_interface_type type = nss_dynamic_interface_get_type(nss_ctx, interface_num); |
| 187 | |
| 188 | return type == NSS_DYNAMIC_INTERFACE_TYPE_WIFI |
Murat Sezgin | 8fb2f36 | 2019-01-22 10:44:53 -0800 | [diff] [blame] | 189 | || type == NSS_DYNAMIC_INTERFACE_TYPE_GENERIC_REDIR_N2H |
| 190 | || type == NSS_DYNAMIC_INTERFACE_TYPE_GENERIC_REDIR_H2N |
Casey Chen | d90d0e6 | 2017-11-06 13:21:26 -0800 | [diff] [blame] | 191 | || type == NSS_DYNAMIC_INTERFACE_TYPE_VIRTIF_DEPRECATED; |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 192 | } |
Stephen Wang | a73a789 | 2017-07-05 15:23:14 -0700 | [diff] [blame] | 193 | EXPORT_SYMBOL(nss_cmn_interface_is_redirect); |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 194 | |
| 195 | /* |
ratheesh kannoth | 93ba95c | 2017-07-13 15:52:52 +0530 | [diff] [blame] | 196 | * nss_cmn_rx_dropped_sum() |
| 197 | * Sum rx_dropped count. |
| 198 | */ |
| 199 | uint32_t nss_cmn_rx_dropped_sum(struct nss_cmn_node_stats *node_stats) |
| 200 | { |
| 201 | uint32_t sum = 0; |
| 202 | int i; |
| 203 | for (i = 0; i < NSS_MAX_NUM_PRI; i++) { |
| 204 | sum += node_stats->rx_dropped[i]; |
| 205 | } |
| 206 | return sum; |
| 207 | } |
Stephen Wang | a73a789 | 2017-07-05 15:23:14 -0700 | [diff] [blame] | 208 | EXPORT_SYMBOL(nss_cmn_rx_dropped_sum); |
ratheesh kannoth | 93ba95c | 2017-07-13 15:52:52 +0530 | [diff] [blame] | 209 | |
| 210 | /* |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 211 | * nss_cmn_register_queue_decongestion() |
| 212 | * Register for queue decongestion event |
| 213 | */ |
| 214 | nss_cb_register_status_t nss_cmn_register_queue_decongestion(struct nss_ctx_instance *nss_ctx, nss_cmn_queue_decongestion_callback_t event_callback, void *app_ctx) |
| 215 | { |
| 216 | uint32_t i; |
| 217 | |
| 218 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 219 | spin_lock_bh(&nss_ctx->decongest_cb_lock); |
| 220 | |
| 221 | /* |
| 222 | * Find vacant location in callback table |
| 223 | */ |
| 224 | for (i = 0; i< NSS_MAX_CLIENTS; i++) { |
| 225 | if (nss_ctx->queue_decongestion_callback[i] == NULL) { |
| 226 | nss_ctx->queue_decongestion_callback[i] = event_callback; |
| 227 | nss_ctx->queue_decongestion_ctx[i] = app_ctx; |
| 228 | spin_unlock_bh(&nss_ctx->decongest_cb_lock); |
| 229 | return NSS_CB_REGISTER_SUCCESS; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | spin_unlock_bh(&nss_ctx->decongest_cb_lock); |
| 234 | return NSS_CB_REGISTER_FAILED; |
| 235 | } |
Stephen Wang | a73a789 | 2017-07-05 15:23:14 -0700 | [diff] [blame] | 236 | EXPORT_SYMBOL(nss_cmn_register_queue_decongestion); |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 237 | |
| 238 | /* |
| 239 | * nss_cmn_unregister_queue_decongestion() |
| 240 | * Unregister for queue decongestion event |
| 241 | */ |
| 242 | nss_cb_unregister_status_t nss_cmn_unregister_queue_decongestion(struct nss_ctx_instance *nss_ctx, nss_cmn_queue_decongestion_callback_t event_callback) |
| 243 | { |
| 244 | uint32_t i; |
| 245 | |
| 246 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 247 | spin_lock_bh(&nss_ctx->decongest_cb_lock); |
| 248 | |
| 249 | /* |
| 250 | * Find actual location in callback table |
| 251 | */ |
| 252 | for (i = 0; i< NSS_MAX_CLIENTS; i++) { |
| 253 | if (nss_ctx->queue_decongestion_callback[i] == event_callback) { |
| 254 | nss_ctx->queue_decongestion_callback[i] = NULL; |
| 255 | nss_ctx->queue_decongestion_ctx[i] = NULL; |
| 256 | spin_unlock_bh(&nss_ctx->decongest_cb_lock); |
| 257 | return NSS_CB_UNREGISTER_SUCCESS; |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | spin_unlock_bh(&nss_ctx->decongest_cb_lock); |
| 262 | return NSS_CB_UNREGISTER_FAILED; |
| 263 | } |
Stephen Wang | a73a789 | 2017-07-05 15:23:14 -0700 | [diff] [blame] | 264 | EXPORT_SYMBOL(nss_cmn_unregister_queue_decongestion); |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 265 | |
Radha krishna Simha Jiguru | 7098a96 | 2015-12-09 11:18:23 +0530 | [diff] [blame] | 266 | /* |
Selin Dag | b4a99d8 | 2018-08-30 09:19:54 -0700 | [diff] [blame] | 267 | * nss_cmn_register_service_code() |
| 268 | * Register for service code event |
| 269 | */ |
| 270 | nss_cb_register_status_t nss_cmn_register_service_code(struct nss_ctx_instance *nss_ctx, nss_cmn_service_code_callback_t cb, uint8_t service_code, void *app_data) |
| 271 | { |
| 272 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 273 | |
| 274 | if (nss_ctx->service_code_callback[service_code]) { |
| 275 | /* |
| 276 | * We already have a callback registered for this service code. |
| 277 | */ |
| 278 | nss_warning("%p: a callback is registered already for this service code %d\n", nss_ctx, service_code); |
| 279 | |
| 280 | return NSS_CB_REGISTER_FAILED; |
| 281 | } |
| 282 | |
| 283 | nss_ctx->service_code_callback[service_code] = cb; |
| 284 | nss_ctx->service_code_ctx[service_code] = app_data; |
| 285 | return NSS_CB_REGISTER_SUCCESS; |
| 286 | } |
| 287 | EXPORT_SYMBOL(nss_cmn_register_service_code); |
| 288 | |
| 289 | /* |
| 290 | * nss_cmn_unregister_service_code() |
| 291 | * Unregister for service code event |
| 292 | */ |
| 293 | nss_cb_unregister_status_t nss_cmn_unregister_service_code(struct nss_ctx_instance *nss_ctx, nss_cmn_service_code_callback_t cb, uint8_t service_code) |
| 294 | { |
| 295 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 296 | |
| 297 | if (!nss_ctx->service_code_callback[service_code]) { |
| 298 | /* |
| 299 | * No callback was registered for this service code. |
| 300 | */ |
| 301 | nss_warning("%p: no callback is registered for this service code %d\n", nss_ctx, service_code); |
| 302 | return NSS_CB_UNREGISTER_FAILED; |
| 303 | } |
| 304 | |
| 305 | nss_ctx->service_code_callback[service_code] = NULL; |
| 306 | nss_ctx->service_code_ctx[service_code] = NULL; |
| 307 | return NSS_CB_UNREGISTER_SUCCESS; |
| 308 | } |
| 309 | EXPORT_SYMBOL(nss_cmn_unregister_service_code); |
| 310 | |
| 311 | /* |
Radha krishna Simha Jiguru | 7098a96 | 2015-12-09 11:18:23 +0530 | [diff] [blame] | 312 | * nss_cmn_get_nss_enabled() |
| 313 | * Check if NSS mode is supported on platform |
| 314 | * |
| 315 | * This API checks the device tree parameter to decide on whether |
| 316 | * NSS mode is enabled. On older kernels this will always return true |
| 317 | */ |
| 318 | bool nss_cmn_get_nss_enabled(void) |
| 319 | { |
| 320 | #if (NSS_DT_SUPPORT == 1) |
| 321 | struct device_node *cmn = NULL; |
| 322 | |
| 323 | /* |
| 324 | * Get reference to NSS common device node |
| 325 | */ |
| 326 | cmn = of_find_node_by_name(NULL, "nss-common"); |
| 327 | if (!cmn) { |
| 328 | nss_info_always("nss is not enabled on this platform\n"); |
| 329 | return false; |
| 330 | } |
| 331 | #endif |
| 332 | return true; |
| 333 | } |
Radha krishna Simha Jiguru | 7098a96 | 2015-12-09 11:18:23 +0530 | [diff] [blame] | 334 | EXPORT_SYMBOL(nss_cmn_get_nss_enabled); |