Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2017 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() |
| 43 | * Initialize the common message structure. |
| 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 | } |
| 54 | |
| 55 | /* |
| 56 | * nss_cmn_get_interface_number() |
| 57 | * Return the interface number of the NSS net_device. |
| 58 | * |
| 59 | * Returns -1 on failure or the interface number of dev is an NSS net_device. |
| 60 | */ |
| 61 | int32_t nss_cmn_get_interface_number(struct nss_ctx_instance *nss_ctx, struct net_device *dev) |
| 62 | { |
| 63 | int i; |
| 64 | |
| 65 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 66 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
| 67 | nss_warning("%p: Interface number could not be found as core not ready", nss_ctx); |
| 68 | return -1; |
| 69 | } |
| 70 | |
| 71 | nss_assert(dev != 0); |
| 72 | |
| 73 | /* |
| 74 | * Check physical interface table |
| 75 | */ |
| 76 | for (i = 0; i < NSS_MAX_NET_INTERFACES; i++) { |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 77 | if (dev == nss_ctx->subsys_dp_register[i].ndev) { |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 78 | return i; |
| 79 | } |
| 80 | } |
| 81 | |
| 82 | nss_warning("%p: Interface number could not be found as interface has not registered yet", nss_ctx); |
| 83 | return -1; |
| 84 | } |
| 85 | |
| 86 | /* |
| 87 | * nss_cmn_get_interface_dev() |
| 88 | * Return the net_device for NSS interface id. |
| 89 | * |
| 90 | * Returns NULL on failure or the net_device for NSS interface id. |
| 91 | */ |
| 92 | struct net_device *nss_cmn_get_interface_dev(struct nss_ctx_instance *ctx, uint32_t if_num) |
| 93 | { |
| 94 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)ctx; |
| 95 | |
| 96 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 97 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
| 98 | nss_warning("%p: Interface device could not be found as core not ready", nss_ctx); |
| 99 | return NULL; |
| 100 | } |
| 101 | |
| 102 | if (unlikely(if_num >= NSS_MAX_NET_INTERFACES)) { |
| 103 | return NULL; |
| 104 | } |
| 105 | |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 106 | return nss_ctx->subsys_dp_register[if_num].ndev; |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | /* |
Gareth Williams | b52af51 | 2014-04-25 19:31:15 +0100 | [diff] [blame] | 110 | * nss_cmn_get_interface_number_by_dev() |
| 111 | * Return the NSS interface id for the net_device. |
| 112 | * |
| 113 | * Returns < 0 on failure or the NSS interface id for the given device. |
| 114 | */ |
| 115 | int32_t nss_cmn_get_interface_number_by_dev(struct net_device *dev) |
| 116 | { |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 117 | int i, core; |
Gareth Williams | b52af51 | 2014-04-25 19:31:15 +0100 | [diff] [blame] | 118 | |
| 119 | nss_assert(dev != 0); |
| 120 | |
| 121 | /* |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 122 | * Check physical interface table on both cores |
Gareth Williams | b52af51 | 2014-04-25 19:31:15 +0100 | [diff] [blame] | 123 | */ |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 124 | for (core = 0; core < NSS_MAX_CORES; core++) { |
| 125 | for (i = 0; i < NSS_MAX_NET_INTERFACES; i++) { |
| 126 | if (dev == nss_top_main.nss[core].subsys_dp_register[i].ndev) { |
| 127 | return i; |
| 128 | } |
Gareth Williams | b52af51 | 2014-04-25 19:31:15 +0100 | [diff] [blame] | 129 | } |
| 130 | } |
| 131 | |
| 132 | nss_warning("Interface number could not be found for %p (%s) as interface has not registered yet", dev, dev->name); |
| 133 | return -1; |
| 134 | } |
| 135 | |
| 136 | /* |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 137 | * nss_cmn_get_state() |
| 138 | * return the NSS initialization state |
| 139 | */ |
| 140 | nss_state_t nss_cmn_get_state(struct nss_ctx_instance *ctx) |
| 141 | { |
| 142 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)ctx; |
| 143 | nss_state_t state = NSS_STATE_UNINITIALIZED; |
| 144 | |
| 145 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 146 | spin_lock_bh(&nss_top_main.lock); |
| 147 | if (nss_ctx->state == NSS_CORE_STATE_INITIALIZED) { |
| 148 | state = NSS_STATE_INITIALIZED; |
| 149 | } |
| 150 | spin_unlock_bh(&nss_top_main.lock); |
| 151 | |
| 152 | return state; |
| 153 | } |
| 154 | |
| 155 | /* |
Sakthi Vignesh Radhakrishnan | 2842f9c | 2017-07-27 13:56:07 -0700 | [diff] [blame] | 156 | * nss_cmn_interface_is_redirect() |
| 157 | * Return true if the interface is a redirect interface. |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 158 | */ |
Sakthi Vignesh Radhakrishnan | 2842f9c | 2017-07-27 13:56:07 -0700 | [diff] [blame] | 159 | 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] | 160 | { |
Stephen Wang | e8b8d0d | 2017-02-24 17:05:22 -0800 | [diff] [blame] | 161 | return (nss_dynamic_interface_get_type(nss_ctx, interface_num) == NSS_DYNAMIC_INTERFACE_TYPE_WIFI) |
| 162 | || (nss_dynamic_interface_get_type(nss_ctx, interface_num) == NSS_DYNAMIC_INTERFACE_TYPE_802_3_REDIR) |
| 163 | || (nss_dynamic_interface_get_type(nss_ctx, interface_num) == NSS_DYNAMIC_INTERFACE_TYPE_VIRTIF_DEPRECATED); |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | /* |
ratheesh kannoth | 93ba95c | 2017-07-13 15:52:52 +0530 | [diff] [blame] | 167 | * nss_cmn_rx_dropped_sum() |
| 168 | * Sum rx_dropped count. |
| 169 | */ |
| 170 | uint32_t nss_cmn_rx_dropped_sum(struct nss_cmn_node_stats *node_stats) |
| 171 | { |
| 172 | uint32_t sum = 0; |
| 173 | int i; |
| 174 | for (i = 0; i < NSS_MAX_NUM_PRI; i++) { |
| 175 | sum += node_stats->rx_dropped[i]; |
| 176 | } |
| 177 | return sum; |
| 178 | } |
| 179 | |
| 180 | /* |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 181 | * nss_cmn_register_queue_decongestion() |
| 182 | * Register for queue decongestion event |
| 183 | */ |
| 184 | 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) |
| 185 | { |
| 186 | uint32_t i; |
| 187 | |
| 188 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 189 | spin_lock_bh(&nss_ctx->decongest_cb_lock); |
| 190 | |
| 191 | /* |
| 192 | * Find vacant location in callback table |
| 193 | */ |
| 194 | for (i = 0; i< NSS_MAX_CLIENTS; i++) { |
| 195 | if (nss_ctx->queue_decongestion_callback[i] == NULL) { |
| 196 | nss_ctx->queue_decongestion_callback[i] = event_callback; |
| 197 | nss_ctx->queue_decongestion_ctx[i] = app_ctx; |
| 198 | spin_unlock_bh(&nss_ctx->decongest_cb_lock); |
| 199 | return NSS_CB_REGISTER_SUCCESS; |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | spin_unlock_bh(&nss_ctx->decongest_cb_lock); |
| 204 | return NSS_CB_REGISTER_FAILED; |
| 205 | } |
| 206 | |
| 207 | /* |
| 208 | * nss_cmn_unregister_queue_decongestion() |
| 209 | * Unregister for queue decongestion event |
| 210 | */ |
| 211 | nss_cb_unregister_status_t nss_cmn_unregister_queue_decongestion(struct nss_ctx_instance *nss_ctx, nss_cmn_queue_decongestion_callback_t event_callback) |
| 212 | { |
| 213 | uint32_t i; |
| 214 | |
| 215 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 216 | spin_lock_bh(&nss_ctx->decongest_cb_lock); |
| 217 | |
| 218 | /* |
| 219 | * Find actual location in callback table |
| 220 | */ |
| 221 | for (i = 0; i< NSS_MAX_CLIENTS; i++) { |
| 222 | if (nss_ctx->queue_decongestion_callback[i] == event_callback) { |
| 223 | nss_ctx->queue_decongestion_callback[i] = NULL; |
| 224 | nss_ctx->queue_decongestion_ctx[i] = NULL; |
| 225 | spin_unlock_bh(&nss_ctx->decongest_cb_lock); |
| 226 | return NSS_CB_UNREGISTER_SUCCESS; |
| 227 | } |
| 228 | } |
| 229 | |
| 230 | spin_unlock_bh(&nss_ctx->decongest_cb_lock); |
| 231 | return NSS_CB_UNREGISTER_FAILED; |
| 232 | } |
| 233 | |
Radha krishna Simha Jiguru | 7098a96 | 2015-12-09 11:18:23 +0530 | [diff] [blame] | 234 | /* |
| 235 | * nss_cmn_get_nss_enabled() |
| 236 | * Check if NSS mode is supported on platform |
| 237 | * |
| 238 | * This API checks the device tree parameter to decide on whether |
| 239 | * NSS mode is enabled. On older kernels this will always return true |
| 240 | */ |
| 241 | bool nss_cmn_get_nss_enabled(void) |
| 242 | { |
| 243 | #if (NSS_DT_SUPPORT == 1) |
| 244 | struct device_node *cmn = NULL; |
| 245 | |
| 246 | /* |
| 247 | * Get reference to NSS common device node |
| 248 | */ |
| 249 | cmn = of_find_node_by_name(NULL, "nss-common"); |
| 250 | if (!cmn) { |
| 251 | nss_info_always("nss is not enabled on this platform\n"); |
| 252 | return false; |
| 253 | } |
| 254 | #endif |
| 255 | return true; |
| 256 | } |
| 257 | |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 258 | EXPORT_SYMBOL(nss_cmn_get_interface_number); |
| 259 | EXPORT_SYMBOL(nss_cmn_get_interface_dev); |
| 260 | EXPORT_SYMBOL(nss_cmn_get_state); |
Sakthi Vignesh Radhakrishnan | 2842f9c | 2017-07-27 13:56:07 -0700 | [diff] [blame] | 261 | EXPORT_SYMBOL(nss_cmn_interface_is_redirect); |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 262 | EXPORT_SYMBOL(nss_cmn_msg_init); |
Gareth Williams | b52af51 | 2014-04-25 19:31:15 +0100 | [diff] [blame] | 263 | EXPORT_SYMBOL(nss_cmn_get_interface_number_by_dev); |
ratheesh kannoth | 93ba95c | 2017-07-13 15:52:52 +0530 | [diff] [blame] | 264 | EXPORT_SYMBOL(nss_cmn_rx_dropped_sum); |
Abhishek Rastogi | 9971433 | 2014-04-02 19:38:12 +0530 | [diff] [blame] | 265 | |
| 266 | EXPORT_SYMBOL(nss_cmn_register_queue_decongestion); |
| 267 | EXPORT_SYMBOL(nss_cmn_unregister_queue_decongestion); |
Radha krishna Simha Jiguru | 7098a96 | 2015-12-09 11:18:23 +0530 | [diff] [blame] | 268 | EXPORT_SYMBOL(nss_cmn_get_nss_enabled); |