blob: f2d26c75882ed0de09147963013c24508ac39b0a [file] [log] [blame]
Abhishek Rastogi99714332014-04-02 19:38:12 +05301/*
2 **************************************************************************
3 * Copyright (c) 2014, 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/*
18 * nss_cmn.c
19 * NSS generic APIs
20 */
21
22#include "nss_tx_rx_common.h"
23
24/*
25 * nss_cmn_msg_init()
26 * Initialize the common message structure.
27 */
28void nss_cmn_msg_init(struct nss_cmn_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data)
29{
30 ncm->interface = if_num;
31 ncm->version = NSS_HLOS_MESSAGE_VERSION;
32 ncm->type = type;
33 ncm->len = len;
34 ncm->cb = (uint32_t)cb;
35 ncm->app_data = (uint32_t)app_data;
36}
37
38/*
39 * nss_cmn_get_interface_number()
40 * Return the interface number of the NSS net_device.
41 *
42 * Returns -1 on failure or the interface number of dev is an NSS net_device.
43 */
44int32_t nss_cmn_get_interface_number(struct nss_ctx_instance *nss_ctx, struct net_device *dev)
45{
46 int i;
47
48 NSS_VERIFY_CTX_MAGIC(nss_ctx);
49 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
50 nss_warning("%p: Interface number could not be found as core not ready", nss_ctx);
51 return -1;
52 }
53
54 nss_assert(dev != 0);
55
56 /*
57 * Check physical interface table
58 */
59 for (i = 0; i < NSS_MAX_NET_INTERFACES; i++) {
60 if (dev == ((struct nss_ctx_instance *)nss_ctx)->nss_top->if_ctx[i]) {
61 return i;
62 }
63 }
64
65 nss_warning("%p: Interface number could not be found as interface has not registered yet", nss_ctx);
66 return -1;
67}
68
69/*
70 * nss_cmn_get_interface_dev()
71 * Return the net_device for NSS interface id.
72 *
73 * Returns NULL on failure or the net_device for NSS interface id.
74 */
75struct net_device *nss_cmn_get_interface_dev(struct nss_ctx_instance *ctx, uint32_t if_num)
76{
77 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)ctx;
78
79 NSS_VERIFY_CTX_MAGIC(nss_ctx);
80 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
81 nss_warning("%p: Interface device could not be found as core not ready", nss_ctx);
82 return NULL;
83 }
84
85 if (unlikely(if_num >= NSS_MAX_NET_INTERFACES)) {
86 return NULL;
87 }
88
89 return nss_ctx->nss_top->if_ctx[if_num];
90}
91
92/*
93 * nss_cmn_get_state()
94 * return the NSS initialization state
95 */
96nss_state_t nss_cmn_get_state(struct nss_ctx_instance *ctx)
97{
98 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)ctx;
99 nss_state_t state = NSS_STATE_UNINITIALIZED;
100
101 NSS_VERIFY_CTX_MAGIC(nss_ctx);
102 spin_lock_bh(&nss_top_main.lock);
103 if (nss_ctx->state == NSS_CORE_STATE_INITIALIZED) {
104 state = NSS_STATE_INITIALIZED;
105 }
106 spin_unlock_bh(&nss_top_main.lock);
107
108 return state;
109}
110
111/*
112 * nss_cmn_interface_is_virtual()
113 * Return true if the interface number is a virtual NSS interface
114 */
115bool nss_cmn_interface_is_virtual(void *nss_ctx, int32_t interface_num)
116{
117 return (NSS_IS_IF_TYPE(VIRTUAL, interface_num));
118}
119
120/*
121 * nss_cmn_register_queue_decongestion()
122 * Register for queue decongestion event
123 */
124nss_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)
125{
126 uint32_t i;
127
128 NSS_VERIFY_CTX_MAGIC(nss_ctx);
129 spin_lock_bh(&nss_ctx->decongest_cb_lock);
130
131 /*
132 * Find vacant location in callback table
133 */
134 for (i = 0; i< NSS_MAX_CLIENTS; i++) {
135 if (nss_ctx->queue_decongestion_callback[i] == NULL) {
136 nss_ctx->queue_decongestion_callback[i] = event_callback;
137 nss_ctx->queue_decongestion_ctx[i] = app_ctx;
138 spin_unlock_bh(&nss_ctx->decongest_cb_lock);
139 return NSS_CB_REGISTER_SUCCESS;
140 }
141 }
142
143 spin_unlock_bh(&nss_ctx->decongest_cb_lock);
144 return NSS_CB_REGISTER_FAILED;
145}
146
147/*
148 * nss_cmn_unregister_queue_decongestion()
149 * Unregister for queue decongestion event
150 */
151nss_cb_unregister_status_t nss_cmn_unregister_queue_decongestion(struct nss_ctx_instance *nss_ctx, nss_cmn_queue_decongestion_callback_t event_callback)
152{
153 uint32_t i;
154
155 NSS_VERIFY_CTX_MAGIC(nss_ctx);
156 spin_lock_bh(&nss_ctx->decongest_cb_lock);
157
158 /*
159 * Find actual location in callback table
160 */
161 for (i = 0; i< NSS_MAX_CLIENTS; i++) {
162 if (nss_ctx->queue_decongestion_callback[i] == event_callback) {
163 nss_ctx->queue_decongestion_callback[i] = NULL;
164 nss_ctx->queue_decongestion_ctx[i] = NULL;
165 spin_unlock_bh(&nss_ctx->decongest_cb_lock);
166 return NSS_CB_UNREGISTER_SUCCESS;
167 }
168 }
169
170 spin_unlock_bh(&nss_ctx->decongest_cb_lock);
171 return NSS_CB_UNREGISTER_FAILED;
172}
173
174EXPORT_SYMBOL(nss_cmn_get_interface_number);
175EXPORT_SYMBOL(nss_cmn_get_interface_dev);
176EXPORT_SYMBOL(nss_cmn_get_state);
177EXPORT_SYMBOL(nss_cmn_interface_is_virtual);
178EXPORT_SYMBOL(nss_cmn_msg_init);
179
180EXPORT_SYMBOL(nss_cmn_register_queue_decongestion);
181EXPORT_SYMBOL(nss_cmn_unregister_queue_decongestion);
182