blob: c2db8a30e19fe4d96b87e56175ced9ecfed4f7a0 [file] [log] [blame]
Abhishek Rastogi99714332014-04-02 19:38:12 +05301/*
2 **************************************************************************
Sundarajan Srinivasancd1631b2015-06-18 01:23:30 -07003 * Copyright (c) 2014, 2015 The Linux Foundation. All rights reserved.
Abhishek Rastogi99714332014-04-02 19:38:12 +05304 * 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 Jiguru7098a962015-12-09 11:18:23 +053022#if (NSS_DT_SUPPORT == 1)
23#include <linux/of.h>
24#endif
25
Abhishek Rastogi99714332014-04-02 19:38:12 +053026#include "nss_tx_rx_common.h"
27
28/*
29 * nss_cmn_msg_init()
30 * Initialize the common message structure.
31 */
32void nss_cmn_msg_init(struct nss_cmn_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data)
33{
34 ncm->interface = if_num;
35 ncm->version = NSS_HLOS_MESSAGE_VERSION;
36 ncm->type = type;
37 ncm->len = len;
38 ncm->cb = (uint32_t)cb;
39 ncm->app_data = (uint32_t)app_data;
40}
41
42/*
43 * nss_cmn_get_interface_number()
44 * Return the interface number of the NSS net_device.
45 *
46 * Returns -1 on failure or the interface number of dev is an NSS net_device.
47 */
48int32_t nss_cmn_get_interface_number(struct nss_ctx_instance *nss_ctx, struct net_device *dev)
49{
50 int i;
51
52 NSS_VERIFY_CTX_MAGIC(nss_ctx);
53 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
54 nss_warning("%p: Interface number could not be found as core not ready", nss_ctx);
55 return -1;
56 }
57
58 nss_assert(dev != 0);
59
60 /*
61 * Check physical interface table
62 */
63 for (i = 0; i < NSS_MAX_NET_INTERFACES; i++) {
Sundarajan Srinivasan70374842014-11-19 15:22:52 -080064 if (dev == ((struct nss_ctx_instance *)nss_ctx)->nss_top->subsys_dp_register[i].ndev) {
Abhishek Rastogi99714332014-04-02 19:38:12 +053065 return i;
66 }
67 }
68
69 nss_warning("%p: Interface number could not be found as interface has not registered yet", nss_ctx);
70 return -1;
71}
72
73/*
74 * nss_cmn_get_interface_dev()
75 * Return the net_device for NSS interface id.
76 *
77 * Returns NULL on failure or the net_device for NSS interface id.
78 */
79struct net_device *nss_cmn_get_interface_dev(struct nss_ctx_instance *ctx, uint32_t if_num)
80{
81 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)ctx;
82
83 NSS_VERIFY_CTX_MAGIC(nss_ctx);
84 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
85 nss_warning("%p: Interface device could not be found as core not ready", nss_ctx);
86 return NULL;
87 }
88
89 if (unlikely(if_num >= NSS_MAX_NET_INTERFACES)) {
90 return NULL;
91 }
92
Sundarajan Srinivasan70374842014-11-19 15:22:52 -080093 return nss_ctx->nss_top->subsys_dp_register[if_num].ndev;
Abhishek Rastogi99714332014-04-02 19:38:12 +053094}
95
96/*
Gareth Williamsb52af512014-04-25 19:31:15 +010097 * nss_cmn_get_interface_number_by_dev()
98 * Return the NSS interface id for the net_device.
99 *
100 * Returns < 0 on failure or the NSS interface id for the given device.
101 */
102int32_t nss_cmn_get_interface_number_by_dev(struct net_device *dev)
103{
104 int i;
105
106 nss_assert(dev != 0);
107
108 /*
109 * Check physical interface table
110 */
111 for (i = 0; i < NSS_MAX_NET_INTERFACES; i++) {
Sundarajan Srinivasan70374842014-11-19 15:22:52 -0800112 if (dev == nss_top_main.subsys_dp_register[i].ndev) {
Gareth Williamsb52af512014-04-25 19:31:15 +0100113 return i;
114 }
115 }
116
117 nss_warning("Interface number could not be found for %p (%s) as interface has not registered yet", dev, dev->name);
118 return -1;
119}
120
121/*
Abhishek Rastogi99714332014-04-02 19:38:12 +0530122 * nss_cmn_get_state()
123 * return the NSS initialization state
124 */
125nss_state_t nss_cmn_get_state(struct nss_ctx_instance *ctx)
126{
127 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)ctx;
128 nss_state_t state = NSS_STATE_UNINITIALIZED;
129
130 NSS_VERIFY_CTX_MAGIC(nss_ctx);
131 spin_lock_bh(&nss_top_main.lock);
132 if (nss_ctx->state == NSS_CORE_STATE_INITIALIZED) {
133 state = NSS_STATE_INITIALIZED;
134 }
135 spin_unlock_bh(&nss_top_main.lock);
136
137 return state;
138}
139
140/*
141 * nss_cmn_interface_is_virtual()
142 * Return true if the interface number is a virtual NSS interface
143 */
144bool nss_cmn_interface_is_virtual(void *nss_ctx, int32_t interface_num)
145{
Sundarajan Srinivasancd1631b2015-06-18 01:23:30 -0700146 return ((nss_dynamic_interface_get_type(interface_num) == NSS_DYNAMIC_INTERFACE_TYPE_WIFI)
147 || (nss_dynamic_interface_get_type(interface_num) == NSS_DYNAMIC_INTERFACE_TYPE_802_3_REDIR)
148 || (nss_dynamic_interface_get_type(interface_num) == NSS_DYNAMIC_INTERFACE_TYPE_VIRTIF_DEPRECATED));
Abhishek Rastogi99714332014-04-02 19:38:12 +0530149}
150
151/*
152 * nss_cmn_register_queue_decongestion()
153 * Register for queue decongestion event
154 */
155nss_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)
156{
157 uint32_t i;
158
159 NSS_VERIFY_CTX_MAGIC(nss_ctx);
160 spin_lock_bh(&nss_ctx->decongest_cb_lock);
161
162 /*
163 * Find vacant location in callback table
164 */
165 for (i = 0; i< NSS_MAX_CLIENTS; i++) {
166 if (nss_ctx->queue_decongestion_callback[i] == NULL) {
167 nss_ctx->queue_decongestion_callback[i] = event_callback;
168 nss_ctx->queue_decongestion_ctx[i] = app_ctx;
169 spin_unlock_bh(&nss_ctx->decongest_cb_lock);
170 return NSS_CB_REGISTER_SUCCESS;
171 }
172 }
173
174 spin_unlock_bh(&nss_ctx->decongest_cb_lock);
175 return NSS_CB_REGISTER_FAILED;
176}
177
178/*
179 * nss_cmn_unregister_queue_decongestion()
180 * Unregister for queue decongestion event
181 */
182nss_cb_unregister_status_t nss_cmn_unregister_queue_decongestion(struct nss_ctx_instance *nss_ctx, nss_cmn_queue_decongestion_callback_t event_callback)
183{
184 uint32_t i;
185
186 NSS_VERIFY_CTX_MAGIC(nss_ctx);
187 spin_lock_bh(&nss_ctx->decongest_cb_lock);
188
189 /*
190 * Find actual location in callback table
191 */
192 for (i = 0; i< NSS_MAX_CLIENTS; i++) {
193 if (nss_ctx->queue_decongestion_callback[i] == event_callback) {
194 nss_ctx->queue_decongestion_callback[i] = NULL;
195 nss_ctx->queue_decongestion_ctx[i] = NULL;
196 spin_unlock_bh(&nss_ctx->decongest_cb_lock);
197 return NSS_CB_UNREGISTER_SUCCESS;
198 }
199 }
200
201 spin_unlock_bh(&nss_ctx->decongest_cb_lock);
202 return NSS_CB_UNREGISTER_FAILED;
203}
204
Radha krishna Simha Jiguru7098a962015-12-09 11:18:23 +0530205/*
206 * nss_cmn_get_nss_enabled()
207 * Check if NSS mode is supported on platform
208 *
209 * This API checks the device tree parameter to decide on whether
210 * NSS mode is enabled. On older kernels this will always return true
211 */
212bool nss_cmn_get_nss_enabled(void)
213{
214#if (NSS_DT_SUPPORT == 1)
215 struct device_node *cmn = NULL;
216
217 /*
218 * Get reference to NSS common device node
219 */
220 cmn = of_find_node_by_name(NULL, "nss-common");
221 if (!cmn) {
222 nss_info_always("nss is not enabled on this platform\n");
223 return false;
224 }
225#endif
226 return true;
227}
228
Abhishek Rastogi99714332014-04-02 19:38:12 +0530229EXPORT_SYMBOL(nss_cmn_get_interface_number);
230EXPORT_SYMBOL(nss_cmn_get_interface_dev);
231EXPORT_SYMBOL(nss_cmn_get_state);
232EXPORT_SYMBOL(nss_cmn_interface_is_virtual);
233EXPORT_SYMBOL(nss_cmn_msg_init);
Gareth Williamsb52af512014-04-25 19:31:15 +0100234EXPORT_SYMBOL(nss_cmn_get_interface_number_by_dev);
Abhishek Rastogi99714332014-04-02 19:38:12 +0530235
236EXPORT_SYMBOL(nss_cmn_register_queue_decongestion);
237EXPORT_SYMBOL(nss_cmn_unregister_queue_decongestion);
Radha krishna Simha Jiguru7098a962015-12-09 11:18:23 +0530238EXPORT_SYMBOL(nss_cmn_get_nss_enabled);
Abhishek Rastogi99714332014-04-02 19:38:12 +0530239