blob: b06136323c5fcdb32abd9f4f9abff7823b1da39f [file] [log] [blame]
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -08001/*
2 **************************************************************************
Kyle Swensondd7b2962021-03-16 13:46:32 -06003 * Copyright (c) 2019-2020, The Linux Foundation. All rights reserved.
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -08004 * 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_pvxlan.c
19 * NSS PVXLAN driver interface APIs
20 */
21#include "nss_core.h"
22#include "nss_pvxlan.h"
23#include "nss_cmn.h"
24#include "nss_tx_rx_common.h"
25#include "nss_pvxlan_stats.h"
26#include "nss_pvxlan_log.h"
27
28#define NSS_PVXLAN_TX_TIMEOUT 3000
29
30/*
31 * Spinlock for protecting tunnel operations colliding with a tunnel destroy
32 */
33DEFINE_SPINLOCK(nss_pvxlan_spinlock);
34
35/*
36 * Private data structure
37 */
38static struct nss_pvxlan_pvt {
39 struct semaphore sem; /* Semaphore structure. */
40 struct completion complete; /* Completion structure. */
41 int response; /* Response from FW. */
42 void *cb; /* Original cb for msgs. */
43 void *app_data; /* Original app_data for msgs. */
44} pvxlan_pvt;
45
46/*
47 * Per PVxLAN tunnel/interface number instance.
48 */
49struct nss_pvxlan_handle {
50 atomic_t refcnt; /* Reference count on the tunnel */
51 uint32_t if_num; /* Interface number */
52 uint32_t tunnel_status; /* 0=disable, 1=enabled */
53 nss_pvxlan_msg_callback_t msg_callback; /* Msg callback */
54 void *app_data; /* App data (argument) */
55};
56
57/*
58 * Array of pointer for NSS PvLAN handles. Each handle has per-tunnel
59 * stats based on the if_num which is an index.
60 */
61static struct nss_pvxlan_handle *nss_pvxlan_hdl[NSS_MAX_DYNAMIC_INTERFACES];
62
63/*
64 * nss_pvxlan_verify_if_num()
65 * Verify if_num passed to us.
66 */
67static bool nss_pvxlan_verify_if_num(uint32_t if_num)
68{
69 uint32_t type = nss_dynamic_interface_get_type(nss_pvxlan_get_ctx(), if_num);
70
71 return ((type == NSS_DYNAMIC_INTERFACE_TYPE_PVXLAN_HOST_INNER) ||
72 (type == NSS_DYNAMIC_INTERFACE_TYPE_PVXLAN_OUTER));
73}
74
75/*
76 * nss_pvxlan_hdl_instance_free()
77 * Free PVxLAN tunnel handle instance.
78 */
79static bool nss_pvxlan_hdl_instance_free(struct nss_ctx_instance *nss_ctx, uint32_t if_num)
80{
81 struct nss_pvxlan_handle *h;
82
83 spin_lock(&nss_pvxlan_spinlock);
84 h = nss_pvxlan_hdl[if_num - NSS_DYNAMIC_IF_START];
85 if (!h) {
86 spin_unlock(&nss_pvxlan_spinlock);
Kyle Swensondd7b2962021-03-16 13:46:32 -060087 nss_warning("%px: Instance does not exist: %d", nss_ctx, if_num);
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -080088 return false;
89 }
90
91 if (h->if_num != if_num) {
92 spin_unlock(&nss_pvxlan_spinlock);
Kyle Swensondd7b2962021-03-16 13:46:32 -060093 nss_warning("%px: Not correct if_num: %d", nss_ctx, if_num);
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -080094 return false;
95 }
96
97 nss_pvxlan_hdl[if_num - NSS_DYNAMIC_IF_START] = NULL;
98 spin_unlock(&nss_pvxlan_spinlock);
99 kfree(h);
100 return true;
101}
102
103/*
104 * nss_pvxlan_hdl_instance_alloc()
105 * Allocate PVxLAN tunnel instance.
106 */
107static bool nss_pvxlan_hdl_instance_alloc(struct nss_ctx_instance *nss_ctx, uint32_t if_num,
108 nss_pvxlan_msg_callback_t notify_cb, void *app_data)
109{
110 struct nss_pvxlan_handle *h;
111
112 /*
113 * Allocate a handle
114 */
115 h = kzalloc(sizeof(struct nss_pvxlan_handle), GFP_ATOMIC);
116 if (!h) {
Kyle Swensondd7b2962021-03-16 13:46:32 -0600117 nss_warning("%px: no memory for allocating PVxLAN handle instance for interface : %d", nss_ctx, if_num);
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -0800118 return false;
119 }
120 h->if_num = if_num;
121
122 spin_lock(&nss_pvxlan_spinlock);
123 if (nss_pvxlan_hdl[if_num - NSS_DYNAMIC_IF_START] != NULL) {
124 spin_unlock(&nss_pvxlan_spinlock);
125 kfree(h);
Kyle Swensondd7b2962021-03-16 13:46:32 -0600126 nss_warning("%px: The handle has been taken by another thread :%d", nss_ctx, if_num);
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -0800127 return false;
128 }
129
130 h->msg_callback = notify_cb;
131 h->app_data = app_data;
132 nss_pvxlan_hdl[if_num - NSS_DYNAMIC_IF_START] = h;
133 spin_unlock(&nss_pvxlan_spinlock);
134
135 return true;
136}
137
138/*
139 * nss_pvxlan_callback()
140 * Callback to handle the completion of NSS->HLOS messages.
141 */
142static void nss_pvxlan_callback(void *app_data, struct nss_pvxlan_msg *nvxm)
143{
144 nss_pvxlan_msg_callback_t callback = (nss_pvxlan_msg_callback_t)pvxlan_pvt.cb;
145 void *data = pvxlan_pvt.app_data;
146
147 pvxlan_pvt.response = NSS_TX_SUCCESS;
148 pvxlan_pvt.cb = NULL;
149 pvxlan_pvt.app_data = NULL;
150
151 if (nvxm->cm.response != NSS_CMN_RESPONSE_ACK) {
152 nss_warning("Pvxlan Error response %d\n", nvxm->cm.response);
153 pvxlan_pvt.response = nvxm->cm.response;
154 }
155
156 if (callback) {
157 callback(data, nvxm);
158 }
159 complete(&pvxlan_pvt.complete);
160}
161
162/*
163 * nss_pvxlan_handler()
164 * Handle NSS -> HLOS messages for PVxLAN.
165 */
166static void nss_pvxlan_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
167{
168 struct nss_pvxlan_msg *nvxm = (struct nss_pvxlan_msg *)ncm;
169 nss_pvxlan_msg_callback_t cb;
170 struct nss_pvxlan_handle * h;
171
172 BUG_ON(!nss_pvxlan_verify_if_num(ncm->interface));
173
174 /*
175 * Is this a valid request/response packet?
176 */
177 if (ncm->type >= NSS_PVXLAN_MSG_TYPE_MAX) {
Kyle Swensondd7b2962021-03-16 13:46:32 -0600178 nss_warning("%px: received invalid message %d for PVXLAN interface", nss_ctx, ncm->type);
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -0800179 return;
180 }
181
182 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_pvxlan_msg)) {
Kyle Swensondd7b2962021-03-16 13:46:32 -0600183 nss_warning("%px: Length of message is greater than required: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -0800184 return;
185 }
186
187 /*
188 * Trace messages.
189 */
190 nss_core_log_msg_failures(nss_ctx, ncm);
191 nss_pvxlan_log_rx_msg(nvxm);
192
193 switch (nvxm->cm.type) {
194 case NSS_PVXLAN_MSG_TYPE_SYNC_STATS:
195 nss_pvxlan_stats_sync(nss_ctx, &nvxm->msg.stats, ncm->interface);
196 break;
197 }
198
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -0800199 /*
200 * Update the callback and app_data for NOTIFY messages.
201 */
202 if (ncm->response == NSS_CMN_RESPONSE_NOTIFY) {
203 uint32_t if_num = ncm->interface - NSS_DYNAMIC_IF_START;
204 spin_lock(&nss_pvxlan_spinlock);
205 h = nss_pvxlan_hdl[if_num];
206 if (h) {
207 ncm->cb = (nss_ptr_t)h->msg_callback;
208 ncm->app_data = (nss_ptr_t)h->app_data;
209 }
210 spin_unlock(&nss_pvxlan_spinlock);
211
212 }
213
214 cb = (nss_pvxlan_msg_callback_t)ncm->cb;
215
216 /*
217 * Do we have a callback
218 */
219 if (!cb) {
Kyle Swensondd7b2962021-03-16 13:46:32 -0600220 nss_trace("%px: cb is null for interface %d", nss_ctx, ncm->interface);
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -0800221 return;
222 }
223
224 cb((void *)ncm->app_data, nvxm);
225}
226
227/*
228 * nss_pvxlan_tx_msg()
229 * Transmit a PVXLAN message to NSS FW. Don't call this from softirq/interrupts.
230 */
231nss_tx_status_t nss_pvxlan_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_pvxlan_msg *msg)
232{
233 struct nss_cmn_msg *ncm = &msg->cm;
234
235 if (!nss_pvxlan_verify_if_num(msg->cm.interface)) {
236 return NSS_TX_FAILURE_BAD_PARAM;
237 }
238
239 if (ncm->type >= NSS_PVXLAN_MSG_TYPE_MAX) {
240 return NSS_TX_FAILURE_BAD_PARAM;
241 }
242
243 /*
244 * Trace messages.
245 */
246 nss_pvxlan_log_tx_msg(msg);
247
248 return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE);
249}
250EXPORT_SYMBOL(nss_pvxlan_tx_msg);
251
252/*
253 * nss_pvxlan_tx_msg_sync()
254 * Transmit a pvxlan message to NSS firmware synchronously.
255 */
256nss_tx_status_t nss_pvxlan_tx_msg_sync(struct nss_ctx_instance *nss_ctx, struct nss_pvxlan_msg *nvxm)
257{
258 nss_tx_status_t status;
259 int ret;
260
261 down(&pvxlan_pvt.sem);
262 nvxm->cm.cb = (nss_ptr_t)nss_pvxlan_callback;
263 nvxm->cm.app_data = (nss_ptr_t)NULL;
264
265 status = nss_pvxlan_tx_msg(nss_ctx, nvxm);
266 if (status != NSS_TX_SUCCESS) {
Kyle Swensondd7b2962021-03-16 13:46:32 -0600267 nss_warning("%px: pvxlan_tx_msg failed\n", nss_ctx);
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -0800268 up(&pvxlan_pvt.sem);
269 return status;
270 }
271
272 ret = wait_for_completion_timeout(&pvxlan_pvt.complete, msecs_to_jiffies(NSS_PVXLAN_TX_TIMEOUT));
273 if (!ret) {
Kyle Swensondd7b2962021-03-16 13:46:32 -0600274 nss_warning("%px: pvxlan tx sync failed due to timeout\n", nss_ctx);
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -0800275 pvxlan_pvt.response = NSS_TX_FAILURE;
276 }
277
278 status = pvxlan_pvt.response;
279 up(&pvxlan_pvt.sem);
280 return status;
281}
282EXPORT_SYMBOL(nss_pvxlan_tx_msg_sync);
283
284/*
285 * nss_pvxlan_tx_buf()
286 * Transmit data buffer (skb) to a NSS interface number
287 */
288nss_tx_status_t nss_pvxlan_tx_buf(struct nss_ctx_instance *nss_ctx, struct sk_buff *buf, uint32_t if_num)
289{
290 BUG_ON(!nss_pvxlan_verify_if_num(if_num));
291
Aniruddha Paul38d72f42019-08-20 16:51:31 +0530292 return nss_core_send_packet(nss_ctx, buf, if_num, H2N_BIT_FLAG_VIRTUAL_BUFFER | H2N_BIT_FLAG_BUFFER_REUSABLE);
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -0800293}
294EXPORT_SYMBOL(nss_pvxlan_tx_buf);
295
296/*
297 ***********************************
298 * Register/Unregister/Miscellaneous APIs
299 ***********************************
300 */
301
302/*
303 * nss_pvxlan_unregister()
304 * Unregister a data packet notifier with NSS FW.
305 */
306bool nss_pvxlan_unregister(uint32_t if_num)
307{
308 struct nss_ctx_instance *nss_ctx;
309 int32_t i;
310
311 nss_ctx = nss_pvxlan_get_ctx();
312 if (!nss_pvxlan_verify_if_num(if_num)) {
Kyle Swensondd7b2962021-03-16 13:46:32 -0600313 nss_warning("%px: data unregister received for invalid interface %d", nss_ctx, if_num);
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -0800314 return false;
315 }
316
317 spin_lock_bh(&nss_pvxlan_tunnel_stats_debug_lock);
318 for (i = 0; i < NSS_PVXLAN_MAX_INTERFACES; i++) {
319 if (nss_pvxlan_tunnel_debug_stats[i].if_num != if_num) {
320 continue;
321 }
322
323 memset(&nss_pvxlan_tunnel_debug_stats[i], 0,
324 sizeof(struct nss_pvxlan_tunnel_stats_debug));
325 break;
326 }
327 spin_unlock_bh(&nss_pvxlan_tunnel_stats_debug_lock);
328
329 nss_core_unregister_handler(nss_ctx, if_num);
330 nss_core_unregister_subsys_dp(nss_ctx, if_num);
331 nss_pvxlan_hdl_instance_free(nss_ctx, if_num);
332 return true;
333}
334EXPORT_SYMBOL(nss_pvxlan_unregister);
335
336/*
337 * nss_pvxlan_register()
338 * Registers a data packet notifier with NSS FW.
339 */
340struct nss_ctx_instance *nss_pvxlan_register(uint32_t if_num,
341 nss_pvxlan_buf_callback_t data_cb,
342 nss_pvxlan_msg_callback_t notify_cb,
343 struct net_device *netdev,
344 uint32_t features)
345{
346 struct nss_ctx_instance *nss_ctx;
347 int core_status;
348 int32_t i;
349
350 nss_ctx = nss_pvxlan_get_ctx();
351 if (!nss_pvxlan_verify_if_num(if_num)) {
Kyle Swensondd7b2962021-03-16 13:46:32 -0600352 nss_warning("%px: data register received for invalid interface %d", nss_ctx, if_num);
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -0800353 return NULL;
354 }
355
356 core_status = nss_core_register_handler(nss_ctx, if_num, nss_pvxlan_msg_handler, NULL);
357 if (core_status != NSS_CORE_STATUS_SUCCESS) {
Kyle Swensondd7b2962021-03-16 13:46:32 -0600358 nss_warning("%px: nss core register handler failed for if_num:%d with error :%d", nss_ctx, if_num, core_status);
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -0800359 return NULL;
360 }
361
362 if (!nss_pvxlan_hdl_instance_alloc(nss_ctx, if_num, notify_cb, (void *)netdev)) {
363 nss_core_unregister_handler(nss_ctx, if_num);
Kyle Swensondd7b2962021-03-16 13:46:32 -0600364 nss_warning("%px: couldn't allocate handle instance for if_num:%d", nss_ctx, if_num);
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -0800365 return NULL;
366 }
367
368 spin_lock_bh(&nss_pvxlan_tunnel_stats_debug_lock);
369 for (i = 0; i < NSS_PVXLAN_MAX_INTERFACES; i++) {
370 if (nss_pvxlan_tunnel_debug_stats[i].valid) {
371 continue;
372 }
373
374 nss_pvxlan_tunnel_debug_stats[i].valid = true;
375 nss_pvxlan_tunnel_debug_stats[i].if_num = if_num;
376 nss_pvxlan_tunnel_debug_stats[i].if_index = netdev->ifindex;
377 break;
378 }
379 spin_unlock_bh(&nss_pvxlan_tunnel_stats_debug_lock);
380
381 if (i == NSS_PVXLAN_MAX_INTERFACES) {
Kyle Swensondd7b2962021-03-16 13:46:32 -0600382 nss_warning("%px: No available debug stats instance :%d", nss_ctx, if_num);
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -0800383 nss_pvxlan_hdl_instance_free(nss_ctx, if_num);
384 nss_core_unregister_handler(nss_ctx, if_num);
385 return NULL;
386 }
387
388 nss_core_register_subsys_dp(nss_ctx, if_num, data_cb, NULL, NULL, netdev, features);
389 return nss_ctx;
390}
391EXPORT_SYMBOL(nss_pvxlan_register);
392
393/*
394 * nss_pvxlan_ifnum_with_core_id()
395 * Append core id to pvxlan interface num.
396 */
397int nss_pvxlan_ifnum_with_core_id(int if_num)
398{
399 struct nss_ctx_instance *nss_ctx = nss_pvxlan_get_ctx();
400
401 NSS_VERIFY_CTX_MAGIC(nss_ctx);
402
403 if (!nss_is_dynamic_interface(if_num)) {
Kyle Swensondd7b2962021-03-16 13:46:32 -0600404 nss_warning("%px: Invalid if_num: %d, must be a dynamic interface\n", nss_ctx, if_num);
Cemil Coskuncdcd1dd2019-01-28 13:35:31 -0800405 return 0;
406 }
407 return NSS_INTERFACE_NUM_APPEND_COREID(nss_ctx, if_num);
408}
409EXPORT_SYMBOL(nss_pvxlan_ifnum_with_core_id);
410
411/*
412 * nss_pvxlan_msg_init()
413 * Initialize pvxlan message.
414 */
415void nss_pvxlan_msg_init(struct nss_pvxlan_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len,
416 nss_pvxlan_msg_callback_t cb, void *app_data)
417{
418 nss_cmn_msg_init(&ncm->cm, if_num, type, len, (void*)cb, app_data);
419}
420EXPORT_SYMBOL(nss_pvxlan_msg_init);
421
422/*
423 * nss_pvxlan_get_ctx()
424 * Return a Pvxlan NSS context.
425 */
426struct nss_ctx_instance *nss_pvxlan_get_ctx()
427{
428 struct nss_ctx_instance *nss_ctx;
429
430 nss_ctx = &nss_top_main.nss[nss_top_main.pvxlan_handler_id];
431 return nss_ctx;
432}
433EXPORT_SYMBOL(nss_pvxlan_get_ctx);
434
435/*
436 * nss_pvxlan_init()
437 * Initializes Pvxlan. Gets called from nss_init.c.
438 */
439void nss_pvxlan_init()
440{
441 nss_pvxlan_stats_dentry_create();
442 sema_init(&pvxlan_pvt.sem, 1);
443 init_completion(&pvxlan_pvt.complete);
444
445 memset(&nss_pvxlan_hdl, 0, sizeof(nss_pvxlan_hdl));
446}