blob: 2edb4663ff218deec2cadafc0291fb1d2088555b [file] [log] [blame]
ratheesh kannotheb2a0a82017-05-04 09:20:17 +05301/*
2 **************************************************************************
Stephen Wang3e2dbd12018-03-14 17:28:17 -07003 * Copyright (c) 2017-2018, The Linux Foundation. All rights reserved.
ratheesh kannotheb2a0a82017-05-04 09:20:17 +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#include "nss_tx_rx_common.h"
Yu Huang8c107082017-07-24 14:58:26 -070018#include "nss_gre_stats.h"
ratheesh kannotheb2a0a82017-05-04 09:20:17 +053019
20#define NSS_GRE_TX_TIMEOUT 3000 /* 3 Seconds */
21
22/*
23 * Private data structure
24 */
25static struct {
26 struct semaphore sem;
27 struct completion complete;
28 int response;
29 void *cb;
30 void *app_data;
31} nss_gre_pvt;
32
ratheesh kannoth7746db22017-05-16 14:16:43 +053033static atomic64_t pkt_cb_addr = ATOMIC64_INIT(0);
34
35/*
36 * nss_gre_rx_handler()
37 * GRE rx handler.
38 */
39static void nss_gre_rx_handler(struct net_device *dev, struct sk_buff *skb,
40 __attribute__((unused)) struct napi_struct *napi)
41{
42 nss_gre_data_callback_t cb;
43
44 nss_gre_pkt_callback_t scb = (nss_gre_pkt_callback_t)(unsigned long)atomic64_read(&pkt_cb_addr);
45 if (unlikely(scb)) {
46 struct nss_gre_info *info = (struct nss_gre_info *)netdev_priv(dev);
47 if (likely(info->next_dev)) {
48 scb(info->next_dev, skb);
49 }
50 }
51
52 cb = nss_top_main.gre_data_callback;
53 cb(dev, skb, 0);
54}
55
ratheesh kannotheb2a0a82017-05-04 09:20:17 +053056/*
ratheesh kannotheb2a0a82017-05-04 09:20:17 +053057 * nss_gre_msg_handler()
58 * Handle NSS -> HLOS messages for GRE
59 */
60static void nss_gre_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
61{
62 struct nss_gre_msg *ntm = (struct nss_gre_msg *)ncm;
63 void *ctx;
64
65 nss_gre_msg_callback_t cb;
66
67 NSS_VERIFY_CTX_MAGIC(nss_ctx);
68 BUG_ON(!(nss_is_dynamic_interface(ncm->interface) || ncm->interface == NSS_GRE_INTERFACE));
69
70 /*
71 * Is this a valid request/response packet?
72 */
73 if (ncm->type >= NSS_GRE_MSG_MAX) {
74 nss_warning("%p: received invalid message %d for GRE STD interface", nss_ctx, ncm->type);
75 return;
76 }
77
78 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_gre_msg)) {
79 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
80 return;
81 }
82
83 switch (ntm->cm.type) {
84 case NSS_GRE_MSG_SESSION_STATS:
85 /*
86 * debug stats embedded in stats msg
87 */
Yu Huang8c107082017-07-24 14:58:26 -070088 nss_gre_stats_session_debug_sync(nss_ctx, &ntm->msg.sstats, ncm->interface);
ratheesh kannotheb2a0a82017-05-04 09:20:17 +053089 break;
90
91 case NSS_GRE_MSG_BASE_STATS:
Yu Huang8c107082017-07-24 14:58:26 -070092 nss_gre_stats_base_debug_sync(nss_ctx, &ntm->msg.bstats);
ratheesh kannotheb2a0a82017-05-04 09:20:17 +053093 break;
94
95 default:
96 break;
97
98 }
99
100 /*
101 * Update the callback and app_data for NOTIFY messages, gre sends all notify messages
102 * to the same callback/app_data.
103 */
104 if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) {
105 ncm->cb = (nss_ptr_t)nss_ctx->nss_top->gre_msg_callback;
106 ncm->app_data = (nss_ptr_t)nss_ctx->subsys_dp_register[ncm->interface].app_data;
107 }
108
109 /*
110 * Log failures
111 */
112 nss_core_log_msg_failures(nss_ctx, ncm);
113
114 /*
115 * callback
116 */
117 cb = (nss_gre_msg_callback_t)ncm->cb;
118 ctx = (void *)ncm->app_data;
119
120 /*
121 * call gre-std callback
122 */
123 if (!cb) {
124 nss_warning("%p: No callback for gre-std interface %d",
125 nss_ctx, ncm->interface);
126 return;
127 }
128
129 cb(ctx, ntm);
130}
131
132/*
133 * nss_gre_callback()
134 * Callback to handle the completion of HLOS-->NSS messages.
135 */
136static void nss_gre_callback(void *app_data, struct nss_gre_msg *nim)
137{
138 nss_gre_msg_callback_t callback = (nss_gre_msg_callback_t)nss_gre_pvt.cb;
139 void *data = nss_gre_pvt.app_data;
140
141 nss_gre_pvt.cb = NULL;
142 nss_gre_pvt.app_data = NULL;
143
144 if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
145 nss_warning("gre Error response %d\n", nim->cm.response);
146 nss_gre_pvt.response = NSS_TX_FAILURE;
147 } else {
148 nss_gre_pvt.response = NSS_TX_SUCCESS;
149 }
150
151 if (callback) {
152 callback(data, nim);
153 }
154
155 complete(&nss_gre_pvt.complete);
156}
157
158/*
ratheesh kannoth7746db22017-05-16 14:16:43 +0530159 * nss_gre_register_pkt_callback()
160 * Register for data callback.
161 */
162void nss_gre_register_pkt_callback(nss_gre_pkt_callback_t cb)
163{
164 atomic64_set(&pkt_cb_addr, (unsigned long)cb);
165}
166EXPORT_SYMBOL(nss_gre_register_pkt_callback);
167
168/*
169 * nss_gre_unregister_pkt_callback()
170 * Unregister for data callback.
171 */
172void nss_gre_unregister_pkt_callback()
173{
174 atomic64_set(&pkt_cb_addr, 0);
175}
176EXPORT_SYMBOL(nss_gre_unregister_pkt_callback);
177
178/*
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530179 * nss_gre_tx_msg()
180 * Transmit a GRE message to NSS firmware
181 */
182nss_tx_status_t nss_gre_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_gre_msg *msg)
183{
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530184 struct nss_cmn_msg *ncm = &msg->cm;
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530185
186 /*
187 * Sanity check the message
188 */
189 if (!nss_is_dynamic_interface(ncm->interface)) {
190 nss_warning("%p: tx request for non dynamic interface: %d", nss_ctx, ncm->interface);
191 return NSS_TX_FAILURE;
192 }
193
194 if (ncm->type > NSS_GRE_MSG_MAX) {
195 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
196 return NSS_TX_FAILURE;
197 }
198
Stephen Wang3e2dbd12018-03-14 17:28:17 -0700199 return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE);
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530200}
201EXPORT_SYMBOL(nss_gre_tx_msg);
202
203/*
204 * nss_gre_tx_msg_sync()
205 * Transmit a GRE message to NSS firmware synchronously.
206 */
207nss_tx_status_t nss_gre_tx_msg_sync(struct nss_ctx_instance *nss_ctx, struct nss_gre_msg *msg)
208{
209 nss_tx_status_t status;
210 int ret = 0;
211
212 down(&nss_gre_pvt.sem);
213 nss_gre_pvt.cb = (void *)msg->cm.cb;
214 nss_gre_pvt.app_data = (void *)msg->cm.app_data;
215
216 msg->cm.cb = (nss_ptr_t)nss_gre_callback;
217 msg->cm.app_data = (nss_ptr_t)NULL;
218
219 status = nss_gre_tx_msg(nss_ctx, msg);
220 if (status != NSS_TX_SUCCESS) {
221 nss_warning("%p: gre_tx_msg failed\n", nss_ctx);
222 up(&nss_gre_pvt.sem);
223 return status;
224 }
225 ret = wait_for_completion_timeout(&nss_gre_pvt.complete, msecs_to_jiffies(NSS_GRE_TX_TIMEOUT));
226
227 if (!ret) {
228 nss_warning("%p: GRE STD tx sync failed due to timeout\n", nss_ctx);
229 nss_gre_pvt.response = NSS_TX_FAILURE;
230 }
231
232 status = nss_gre_pvt.response;
233 up(&nss_gre_pvt.sem);
234 return status;
235}
236EXPORT_SYMBOL(nss_gre_tx_msg_sync);
237
238/*
239 * nss_gre_tx_buf()
240 * Send packet to GRE interface owned by NSS
241 */
242nss_tx_status_t nss_gre_tx_buf(struct nss_ctx_instance *nss_ctx, uint32_t if_num, struct sk_buff *skb)
243{
Stephen Wang3e2dbd12018-03-14 17:28:17 -0700244 return nss_core_send_packet(nss_ctx, skb, if_num, H2N_BIT_FLAG_VIRTUAL_BUFFER);
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530245}
246EXPORT_SYMBOL(nss_gre_tx_buf);
247
248/*
249 ***********************************
250 * Register/Unregister/Miscellaneous APIs
251 ***********************************
252 */
253
254/*
255 * nss_gre_register_if()
256 * Register data and message handlers for GRE.
257 */
ratheesh kannoth7746db22017-05-16 14:16:43 +0530258struct nss_ctx_instance *nss_gre_register_if(uint32_t if_num, nss_gre_data_callback_t data_callback,
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530259 nss_gre_msg_callback_t event_callback, struct net_device *netdev, uint32_t features)
260{
261 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.gre_handler_id];
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530262
263 nss_assert(nss_ctx);
264 nss_assert(nss_is_dynamic_interface(if_num));
265
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700266 nss_core_register_subsys_dp(nss_ctx, if_num, nss_gre_rx_handler, NULL, netdev, netdev, features);
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530267
268 nss_top_main.gre_msg_callback = event_callback;
ratheesh kannoth7746db22017-05-16 14:16:43 +0530269 nss_top_main.gre_data_callback = data_callback;
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530270
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700271 nss_core_register_handler(nss_ctx, if_num, nss_gre_msg_handler, NULL);
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530272
Yu Huang8c107082017-07-24 14:58:26 -0700273 nss_gre_stats_session_register(if_num, netdev);
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530274
275 return nss_ctx;
276}
277EXPORT_SYMBOL(nss_gre_register_if);
278
279/*
280 * nss_gre_unregister_if()
281 * Unregister data and message handler.
282 */
283void nss_gre_unregister_if(uint32_t if_num)
284{
285 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.gre_handler_id];
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530286
287 nss_assert(nss_ctx);
288 nss_assert(nss_is_dynamic_interface(if_num));
289
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700290 nss_core_unregister_subsys_dp(nss_ctx, if_num);
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530291
292 nss_top_main.gre_msg_callback = NULL;
293
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700294 nss_core_unregister_handler(nss_ctx, if_num);
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530295
Yu Huang8c107082017-07-24 14:58:26 -0700296 nss_gre_stats_session_unregister(if_num);
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530297}
298EXPORT_SYMBOL(nss_gre_unregister_if);
299
300/*
301 * nss_get_gre_context()
302 */
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700303struct nss_ctx_instance *nss_gre_get_context(void)
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530304{
305 return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.gre_handler_id];
306}
307EXPORT_SYMBOL(nss_gre_get_context);
308
309/*
310 * nss_gre_msg_init()
311 * Initialize nss_gre msg.
312 */
313void nss_gre_msg_init(struct nss_gre_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data)
314{
315 nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data);
316}
317EXPORT_SYMBOL(nss_gre_msg_init);
318
319/*
320 * nss_gre_register_handler()
321 * debugfs stats msg handler received on static gre interface
322 */
323void nss_gre_register_handler(void)
324{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700325 struct nss_ctx_instance *nss_ctx = nss_gre_get_context();
326
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530327 nss_info("nss_gre_register_handler");
328 sema_init(&nss_gre_pvt.sem, 1);
329 init_completion(&nss_gre_pvt.complete);
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700330 nss_core_register_handler(nss_ctx, NSS_GRE_INTERFACE, nss_gre_msg_handler, NULL);
Yu Huang8c107082017-07-24 14:58:26 -0700331 nss_gre_stats_dentry_create();
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530332}