blob: 2ad87fbbbc62b5fb3d6a989539ffc92a4ef80263 [file] [log] [blame]
Thomas Wu68250352014-04-02 18:59:40 -07001/*
2 **************************************************************************
Cemil Coskun7d9d9b32019-06-28 15:49:39 -07003 * Copyright (c) 2013-2019, The Linux Foundation. All rights reserved.
Thomas Wu68250352014-04-02 18:59:40 -07004 * 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/*
Murat Sezgin5c8c7362014-09-02 17:58:21 -070018 * nss_ipv4.c
Thomas Wu68250352014-04-02 18:59:40 -070019 * NSS IPv4 APIs
20 */
Vijay Dewangan9db18752014-09-15 16:25:01 -070021#include <linux/sysctl.h>
Thomas Wu68250352014-04-02 18:59:40 -070022#include "nss_tx_rx_common.h"
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -070023#include "nss_dscp_map.h"
Yu Huang8c107082017-07-24 14:58:26 -070024#include "nss_ipv4_stats.h"
Thomas Wu68250352014-04-02 18:59:40 -070025
Stephen Wang0a2756a2016-08-04 15:52:47 -070026#define NSS_IPV4_TX_MSG_TIMEOUT 1000 /* 1 sec timeout for IPv4 messages */
27
28/*
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -070029 * Private data structure for ipv4 configuration
Stephen Wang0a2756a2016-08-04 15:52:47 -070030 */
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -070031struct nss_ipv4_pvt {
32 struct semaphore sem; /* Semaphore structure */
33 struct completion complete; /* completion structure */
34 int response; /* Response from FW */
35 void *cb; /* Original cb for sync msgs */
36 void *app_data; /* Original app_data for sync msgs */
37} nss_ipv4_pvt;
Stephen Wang0a2756a2016-08-04 15:52:47 -070038
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -070039/*
40 * Private data structure for ipv4 connection information.
41 */
42struct nss_ipv4_conn_table_info {
43 uint32_t ce_table_size; /* Size of connection table entry in NSS FW */
44 uint32_t cme_table_size; /* Size of connection match table entry in NSS FW */
45 unsigned long ce_mem; /* Start address for connection entry table */
46 unsigned long cme_mem; /* Start address for connection match entry table */
47} nss_ipv4_ct_info;
48
pavir099db842018-12-28 14:51:06 +053049int nss_ipv4_conn_cfg = NSS_DEFAULT_NUM_CONN;
Stephen Wang0a2756a2016-08-04 15:52:47 -070050int nss_ipv4_accel_mode_cfg __read_mostly = 1;
51
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -070052static struct nss_dscp_map_entry mapping[NSS_DSCP_MAP_ARRAY_SIZE];
Vijay Dewangan9db18752014-09-15 16:25:01 -070053
Thomas Wu68250352014-04-02 18:59:40 -070054/*
Suruchi Agarwal611ecd02016-08-04 15:54:35 -070055 * Callback for conn_sync_many request message.
56 */
57nss_ipv4_msg_callback_t nss_ipv4_conn_sync_many_msg_cb = NULL;
58
59/*
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -070060 * nss_ipv4_dscp_map_usage()
61 * Help function shows the usage of the command.
Gareth Williams958aa822015-02-04 19:36:39 +000062 */
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -070063static inline void nss_ipv4_dscp_map_usage(void)
Gareth Williams958aa822015-02-04 19:36:39 +000064{
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -070065 nss_info_always("\nUsage:\n");
66 nss_info_always("echo <dscp> <action> <prio> > /proc/sys/dev/nss/ipv4cfg/ipv4_dscp_map\n\n");
67 nss_info_always("dscp[0-63] action[0-%u] prio[0-%u]:\n\n",
68 NSS_IPV4_DSCP_MAP_ACTION_MAX - 1,
69 NSS_DSCP_MAP_PRIORITY_MAX - 1);
Gareth Williams958aa822015-02-04 19:36:39 +000070}
Guojun Jin9a1cb282017-07-13 17:32:07 -070071
72/*
Sol Kavy2783c072014-04-05 12:53:13 -070073 * nss_ipv4_rx_msg_handler()
Thomas Wu68250352014-04-02 18:59:40 -070074 * Handle NSS -> HLOS messages for IPv4 bridge/route
75 */
Sol Kavy2783c072014-04-05 12:53:13 -070076static void nss_ipv4_rx_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
Thomas Wu68250352014-04-02 18:59:40 -070077{
78 struct nss_ipv4_msg *nim = (struct nss_ipv4_msg *)ncm;
Sol Kavy57d24b42014-04-05 13:45:36 -070079 nss_ipv4_msg_callback_t cb;
Thomas Wu68250352014-04-02 18:59:40 -070080
81 BUG_ON(ncm->interface != NSS_IPV4_RX_INTERFACE);
82
83 /*
84 * Sanity check the message type
85 */
Murat Sezgin5c8c7362014-09-02 17:58:21 -070086 if (ncm->type >= NSS_IPV4_MAX_MSG_TYPES) {
Thomas Wu68250352014-04-02 18:59:40 -070087 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
88 return;
89 }
90
Suruchi Agarwalef8a8702016-01-08 12:40:08 -080091 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ipv4_msg)) {
92 nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Thomas Wu68250352014-04-02 18:59:40 -070093 return;
94 }
95
Thomas Wu68250352014-04-02 18:59:40 -070096 /*
Suruchi Agarwale3940e72016-07-06 15:56:51 -070097 * Trace messages.
Thomas Wu68250352014-04-02 18:59:40 -070098 */
Suruchi Agarwale3940e72016-07-06 15:56:51 -070099 nss_ipv4_log_rx_msg(nim);
Thomas Wu68250352014-04-02 18:59:40 -0700100
Thomas Wu68250352014-04-02 18:59:40 -0700101 switch (nim->cm.type) {
Murat Sezgin0c0561d2014-04-09 18:55:58 -0700102 case NSS_IPV4_RX_NODE_STATS_SYNC_MSG:
103 /*
104 * Update driver statistics on node sync.
105 */
Yu Huang8c107082017-07-24 14:58:26 -0700106 nss_ipv4_stats_node_sync(nss_ctx, &nim->msg.node_stats);
Murat Sezgin0c0561d2014-04-09 18:55:58 -0700107 break;
108
Thomas Wu68250352014-04-02 18:59:40 -0700109 case NSS_IPV4_RX_CONN_STATS_SYNC_MSG:
Sol Kavy57d24b42014-04-05 13:45:36 -0700110 /*
111 * Update driver statistics on connection sync.
112 */
Yu Huang8c107082017-07-24 14:58:26 -0700113 nss_ipv4_stats_conn_sync(nss_ctx, &nim->msg.conn_stats);
Sakthi Vignesh Radhakrishnan515f8c22014-06-21 15:04:19 -0700114 break;
Stephen Wang709f9b52015-07-07 14:48:35 -0700115
116 case NSS_IPV4_TX_CONN_STATS_SYNC_MANY_MSG:
117 /*
118 * Update driver statistics on connection sync many.
119 */
Yu Huang8c107082017-07-24 14:58:26 -0700120 nss_ipv4_stats_conn_sync_many(nss_ctx, &nim->msg.conn_stats_many);
Stephen Wangaed46332016-12-12 17:29:03 -0800121 ncm->cb = (nss_ptr_t)nss_ipv4_conn_sync_many_msg_cb;
Stephen Wang709f9b52015-07-07 14:48:35 -0700122 break;
Thomas Wu68250352014-04-02 18:59:40 -0700123 }
124
125 /*
Sol Kavy57d24b42014-04-05 13:45:36 -0700126 * Update the callback and app_data for NOTIFY messages, IPv4 sends all notify messages
127 * to the same callback/app_data.
Thomas Wu68250352014-04-02 18:59:40 -0700128 */
Suruchi Agarwale4ad24a2018-06-11 12:03:46 +0530129 if (nim->cm.response == NSS_CMN_RESPONSE_NOTIFY) {
Stephen Wangaed46332016-12-12 17:29:03 -0800130 ncm->cb = (nss_ptr_t)nss_ctx->nss_top->ipv4_callback;
131 ncm->app_data = (nss_ptr_t)nss_ctx->nss_top->ipv4_ctx;
Sol Kavy57d24b42014-04-05 13:45:36 -0700132 }
Sol Kavy2783c072014-04-05 12:53:13 -0700133
Thomas Wu68250352014-04-02 18:59:40 -0700134 /*
135 * Do we have a callback?
136 */
137 if (!ncm->cb) {
138 return;
139 }
140
141 /*
142 * Callback
143 */
Sol Kavy57d24b42014-04-05 13:45:36 -0700144 cb = (nss_ipv4_msg_callback_t)ncm->cb;
Thomas Wu68250352014-04-02 18:59:40 -0700145 cb((void *)ncm->app_data, nim);
Thomas Wu68250352014-04-02 18:59:40 -0700146}
147
148/*
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700149 * nss_ipv4_tx_sync_callback()
150 * Callback to handle the completion of synchronous tx messages.
151 */
152static void nss_ipv4_tx_sync_callback(void *app_data, struct nss_ipv4_msg *nim)
153{
154 nss_ipv4_msg_callback_t callback = (nss_ipv4_msg_callback_t)nss_ipv4_pvt.cb;
155 void *data = nss_ipv4_pvt.app_data;
156
157 nss_ipv4_pvt.cb = NULL;
158 nss_ipv4_pvt.app_data = NULL;
159
160 if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
161 nss_warning("ipv4 error response %d\n", nim->cm.response);
162 nss_ipv4_pvt.response = NSS_TX_FAILURE;
163 } else {
164 nss_ipv4_pvt.response = NSS_TX_SUCCESS;
165 }
166
167 if (callback) {
168 callback(data, nim);
169 }
170
171 complete(&nss_ipv4_pvt.complete);
172}
173
174/*
175 * nss_ipv4_dscp_action_get()
176 * Gets the action mapped to dscp.
177 */
178enum nss_ipv4_dscp_map_actions nss_ipv4_dscp_action_get(uint8_t dscp)
179{
Sakthi Vignesh Radhakrishnan1f7a66b2017-10-18 09:33:22 -0700180 if (dscp >= NSS_DSCP_MAP_ARRAY_SIZE) {
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700181 nss_warning("dscp:%u invalid\n", dscp);
182 return NSS_IPV4_DSCP_MAP_ACTION_MAX;
183 }
184
185 return mapping[dscp].action;
186}
187EXPORT_SYMBOL(nss_ipv4_dscp_action_get);
188
189/*
190 * nss_ipv4_max_conn_count()
191 * Return the maximum number of IPv4 connections that the NSS acceleration engine supports.
192 */
193int nss_ipv4_max_conn_count(void)
194{
195 return nss_ipv4_conn_cfg;
196}
197EXPORT_SYMBOL(nss_ipv4_max_conn_count);
198
199/*
200 * nss_ipv4_conn_inquiry()
201 * Inquiry if a connection has been established in NSS FW
202 */
203nss_tx_status_t nss_ipv4_conn_inquiry(struct nss_ipv4_5tuple *ipv4_5t_p,
204 nss_ipv4_msg_callback_t cb)
205{
206 nss_tx_status_t nss_tx_status;
207 struct nss_ipv4_msg nim;
208 struct nss_ctx_instance *nss_ctx = &nss_top_main.nss[0];
209
210 /*
211 * Initialize inquiry message structure.
212 * This is async message and the result will be returned
213 * to the caller by the msg_callback passed in.
214 */
215 memset(&nim, 0, sizeof(nim));
216 nss_ipv4_msg_init(&nim, NSS_IPV4_RX_INTERFACE,
217 NSS_IPV4_TX_CONN_CFG_INQUIRY_MSG,
218 sizeof(struct nss_ipv4_inquiry_msg),
219 cb, NULL);
220 nim.msg.inquiry.rr.tuple = *ipv4_5t_p;
221 nss_tx_status = nss_ipv4_tx(nss_ctx, &nim);
222 if (nss_tx_status != NSS_TX_SUCCESS) {
223 nss_warning("%p: Send inquiry message failed\n", ipv4_5t_p);
224 }
225
226 return nss_tx_status;
227}
228EXPORT_SYMBOL(nss_ipv4_conn_inquiry);
229
230/*
Stephen Wang709f9b52015-07-07 14:48:35 -0700231 * nss_ipv4_tx_with_size()
232 * Transmit an ipv4 message to the FW with a specified size.
Thomas Wu68250352014-04-02 18:59:40 -0700233 */
Stephen Wang709f9b52015-07-07 14:48:35 -0700234nss_tx_status_t nss_ipv4_tx_with_size(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_msg *nim, uint32_t size)
Thomas Wu68250352014-04-02 18:59:40 -0700235{
Thomas Wu68250352014-04-02 18:59:40 -0700236 struct nss_cmn_msg *ncm = &nim->cm;
Thomas Wu68250352014-04-02 18:59:40 -0700237
238 /*
239 * Sanity check the message
240 */
241 if (ncm->interface != NSS_IPV4_RX_INTERFACE) {
242 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
243 return NSS_TX_FAILURE;
244 }
245
Murat Sezgin5c8c7362014-09-02 17:58:21 -0700246 if (ncm->type >= NSS_IPV4_MAX_MSG_TYPES) {
Thomas Wu68250352014-04-02 18:59:40 -0700247 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
248 return NSS_TX_FAILURE;
249 }
250
Suruchi Agarwale3940e72016-07-06 15:56:51 -0700251 /*
252 * Trace messages.
253 */
254 nss_ipv4_log_tx_msg(nim);
255
Stephen Wang3e2dbd12018-03-14 17:28:17 -0700256 return nss_core_send_cmd(nss_ctx, nim, sizeof(*nim), size);
Thomas Wu68250352014-04-02 18:59:40 -0700257}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700258EXPORT_SYMBOL(nss_ipv4_tx_with_size);
Thomas Wu68250352014-04-02 18:59:40 -0700259
260/*
Stephen Wang709f9b52015-07-07 14:48:35 -0700261 * nss_ipv4_tx()
262 * Transmit an ipv4 message to the FW.
263 */
264nss_tx_status_t nss_ipv4_tx(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_msg *nim)
265{
266 return nss_ipv4_tx_with_size(nss_ctx, nim, NSS_NBUF_PAYLOAD_SIZE);
267}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700268EXPORT_SYMBOL(nss_ipv4_tx);
Stephen Wang709f9b52015-07-07 14:48:35 -0700269
270/*
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700271 * nss_ipv4_tx_sync()
272 * Transmit a synchronous ipv4 message to the FW.
273 */
274nss_tx_status_t nss_ipv4_tx_sync(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_msg *nim)
275{
276 nss_tx_status_t status;
277 int ret = 0;
278
279 down(&nss_ipv4_pvt.sem);
280 nss_ipv4_pvt.cb = (void *)nim->cm.cb;
281 nss_ipv4_pvt.app_data = (void *)nim->cm.app_data;
282
283 nim->cm.cb = (nss_ptr_t)nss_ipv4_tx_sync_callback;
284 nim->cm.app_data = (nss_ptr_t)NULL;
285
286 status = nss_ipv4_tx(nss_ctx, nim);
287 if (status != NSS_TX_SUCCESS) {
288 nss_warning("%p: nss ipv4 msg tx failed\n", nss_ctx);
289 up(&nss_ipv4_pvt.sem);
290 return status;
291 }
292
293 ret = wait_for_completion_timeout(&nss_ipv4_pvt.complete, msecs_to_jiffies(NSS_IPV4_TX_MSG_TIMEOUT));
294 if (!ret) {
295 nss_warning("%p: IPv4 tx sync failed due to timeout\n", nss_ctx);
296 nss_ipv4_pvt.response = NSS_TX_FAILURE;
297 }
298
299 status = nss_ipv4_pvt.response;
300 up(&nss_ipv4_pvt.sem);
301 return status;
302}
303EXPORT_SYMBOL(nss_ipv4_tx_sync);
304
305/*
Thomas Wu68250352014-04-02 18:59:40 -0700306 **********************************
307 Register/Unregister/Miscellaneous APIs
308 **********************************
309 */
310
311/*
312 * nss_ipv4_notify_register()
313 * Register to received IPv4 events.
314 *
315 * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv4 on any core?
316 */
317struct nss_ctx_instance *nss_ipv4_notify_register(nss_ipv4_msg_callback_t cb, void *app_data)
318{
319 /*
320 * TODO: We need to have a new array in support of the new API
321 * TODO: If we use a per-context array, we would move the array into nss_ctx based.
322 */
323 nss_top_main.ipv4_callback = cb;
Abhishek Rastogie11f47b2014-04-04 18:43:32 +0530324 nss_top_main.ipv4_ctx = app_data;
Thomas Wu68250352014-04-02 18:59:40 -0700325 return &nss_top_main.nss[nss_top_main.ipv4_handler_id];
326}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700327EXPORT_SYMBOL(nss_ipv4_notify_register);
Thomas Wu68250352014-04-02 18:59:40 -0700328
329/*
330 * nss_ipv4_notify_unregister()
331 * Unregister to received IPv4 events.
332 *
333 * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv4 on any core?
334 */
335void nss_ipv4_notify_unregister(void)
336{
337 nss_top_main.ipv4_callback = NULL;
338}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700339EXPORT_SYMBOL(nss_ipv4_notify_unregister);
Thomas Wu68250352014-04-02 18:59:40 -0700340
341/*
Suruchi Agarwal611ecd02016-08-04 15:54:35 -0700342 * nss_ipv4_conn_sync_many_notify_register()
343 * Register to receive IPv4 conn_sync_many message response.
344 */
345void nss_ipv4_conn_sync_many_notify_register(nss_ipv4_msg_callback_t cb)
346{
347 nss_ipv4_conn_sync_many_msg_cb = cb;
348}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700349EXPORT_SYMBOL(nss_ipv4_conn_sync_many_notify_register);
Suruchi Agarwal611ecd02016-08-04 15:54:35 -0700350
351/*
352 * nss_ipv4_conn_sync_many_notify_unregister()
353 * Unregister to receive IPv4 conn_sync_many message response.
354 */
355void nss_ipv4_conn_sync_many_notify_unregister(void)
356{
357 nss_ipv4_conn_sync_many_msg_cb = NULL;
358}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700359EXPORT_SYMBOL(nss_ipv4_conn_sync_many_notify_unregister);
Suruchi Agarwal611ecd02016-08-04 15:54:35 -0700360
361/*
Thomas Wu68250352014-04-02 18:59:40 -0700362 * nss_ipv4_get_mgr()
363 *
364 * TODO: This only suppports a single ipv4, do we ever want to support more?
365 */
366struct nss_ctx_instance *nss_ipv4_get_mgr(void)
367{
368 return (void *)&nss_top_main.nss[nss_top_main.ipv4_handler_id];
369}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700370EXPORT_SYMBOL(nss_ipv4_get_mgr);
Thomas Wu68250352014-04-02 18:59:40 -0700371
372/*
373 * nss_ipv4_register_handler()
374 * Register our handler to receive messages for this interface
375 */
Sol Kavy2783c072014-04-05 12:53:13 -0700376void nss_ipv4_register_handler(void)
Thomas Wu68250352014-04-02 18:59:40 -0700377{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700378 struct nss_ctx_instance *nss_ctx = nss_ipv4_get_mgr();
379
380 if (nss_core_register_handler(nss_ctx, NSS_IPV4_RX_INTERFACE, nss_ipv4_rx_msg_handler, NULL) != NSS_CORE_STATUS_SUCCESS) {
Thomas Wu68250352014-04-02 18:59:40 -0700381 nss_warning("IPv4 handler failed to register");
382 }
Yu Huang8c107082017-07-24 14:58:26 -0700383
384 nss_ipv4_stats_dentry_create();
Thomas Wu68250352014-04-02 18:59:40 -0700385}
386
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700387/*
388 * nss_ipv4_conn_cfg_process_callback()
389 * Call back function for the ipv4 connection configure process
390 */
391static void nss_ipv4_conn_cfg_process_callback(void *app_data, struct nss_ipv4_msg *nim)
392{
393 struct nss_ipv4_rule_conn_cfg_msg *nirccm = &nim->msg.rule_conn_cfg;
394 struct nss_ctx_instance *nss_ctx __maybe_unused = nss_ipv4_get_mgr();
395
396 if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
397 nss_warning("%p: IPv4 connection configuration failed with error: %d\n", nss_ctx, nim->cm.error);
Stephen Wang01433692017-10-30 14:45:11 -0700398 nss_core_update_max_ipv4_conn(NSS_FW_DEFAULT_NUM_CONN);
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700399 nss_ipv4_free_conn_tables();
400 return;
401 }
402
403 nss_ipv4_conn_cfg = ntohl(nirccm->num_conn);
Cemil Coskun7d9d9b32019-06-28 15:49:39 -0700404 nss_info("%p: IPv4 connection configuration success: %d\n", nss_ctx, nim->cm.error);
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700405}
406
Vijay Dewangan9db18752014-09-15 16:25:01 -0700407/*
Tushar Mathura3c18932015-08-24 20:27:21 +0530408 * nss_ipv4_conn_cfg_process()
409 * Process request to configure number of ipv4 connections
410 */
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700411static int nss_ipv4_conn_cfg_process(struct nss_ctx_instance *nss_ctx, int conn)
Tushar Mathura3c18932015-08-24 20:27:21 +0530412{
413 struct nss_ipv4_msg nim;
414 struct nss_ipv4_rule_conn_cfg_msg *nirccm;
415 nss_tx_status_t nss_tx_status;
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700416
417 if ((!nss_ipv4_ct_info.ce_table_size) || (!nss_ipv4_ct_info.cme_table_size)) {
418 nss_warning("%p: connection entry or connection match entry table size not available\n",
419 nss_ctx);
420 return -EINVAL;
421 }
422
423 nss_info("%p: IPv4 supported connections: %d\n", nss_ctx, conn);
424
425 nss_ipv4_ct_info.ce_mem = __get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
426 get_order(nss_ipv4_ct_info.ce_table_size));
427 if (!nss_ipv4_ct_info.ce_mem) {
428 nss_warning("%p: Memory allocation failed for IPv4 Connections: %d\n",
429 nss_ctx,
430 conn);
431 goto fail;
432 }
433
434 nss_ipv4_ct_info.cme_mem = __get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
435 get_order(nss_ipv4_ct_info.cme_table_size));
436 if (!nss_ipv4_ct_info.ce_mem) {
437 nss_warning("%p: Memory allocation failed for IPv4 Connections: %d\n",
438 nss_ctx,
439 conn);
440 goto fail;
441 }
442
443 memset(&nim, 0, sizeof(struct nss_ipv4_msg));
444 nss_ipv4_msg_init(&nim, NSS_IPV4_RX_INTERFACE, NSS_IPV4_TX_CONN_CFG_RULE_MSG,
445 sizeof(struct nss_ipv4_rule_conn_cfg_msg), nss_ipv4_conn_cfg_process_callback, NULL);
446
447 nirccm = &nim.msg.rule_conn_cfg;
448 nirccm->num_conn = htonl(conn);
Suruchi Agarwal8c9e7162017-08-17 15:58:40 -0700449 nirccm->ce_mem = dma_map_single(nss_ctx->dev, (void *)nss_ipv4_ct_info.ce_mem, nss_ipv4_ct_info.ce_table_size, DMA_TO_DEVICE);
450 if (unlikely(dma_mapping_error(nss_ctx->dev, nirccm->ce_mem))) {
451 nss_warning("%p: DMA mapping failed for virtual address = %p", nss_ctx, (void *)nss_ipv4_ct_info.ce_mem);
452 goto fail;
453 }
454
455 nirccm->cme_mem = dma_map_single(nss_ctx->dev, (void *)nss_ipv4_ct_info.cme_mem, nss_ipv4_ct_info.cme_table_size, DMA_TO_DEVICE);
456 if (unlikely(dma_mapping_error(nss_ctx->dev, nirccm->cme_mem))) {
457 nss_warning("%p: DMA mapping failed for virtual address = %p", nss_ctx, (void *)nss_ipv4_ct_info.cme_mem);
458 goto fail;
459 }
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700460
461 nss_tx_status = nss_ipv4_tx(nss_ctx, &nim);
462 if (nss_tx_status != NSS_TX_SUCCESS) {
463 nss_warning("%p: nss_tx error setting IPv4 Connections: %d\n",
464 nss_ctx,
465 conn);
466 goto fail;
467 }
468
469 return 0;
470
471fail:
472 nss_ipv4_free_conn_tables();
473 return -EINVAL;;
474}
475
476/*
477 * nss_ipv4_update_conn_count_callback()
478 * Callback function for the ipv4 get connection info message.
479 */
480static void nss_ipv4_update_conn_count_callback(void *app_data, struct nss_ipv4_msg *nim)
481{
482 struct nss_ipv4_rule_conn_get_table_size_msg *nircgts = &nim->msg.size;
483 struct nss_ctx_instance *nss_ctx = nss_ipv4_get_mgr();
484
485 if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
486 nss_warning("%p: IPv4 fetch connection info failed with error: %d\n", nss_ctx, nim->cm.error);
Stephen Wang01433692017-10-30 14:45:11 -0700487 nss_core_update_max_ipv4_conn(NSS_FW_DEFAULT_NUM_CONN);
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700488 return;
489 }
490
491 nss_info("IPv4 get connection info success\n");
492
493 nss_ipv4_ct_info.ce_table_size = ntohl(nircgts->ce_table_size);
494 nss_ipv4_ct_info.cme_table_size = ntohl(nircgts->cme_table_size);
495
496 if (nss_ipv4_conn_cfg_process(nss_ctx, ntohl(nircgts->num_conn)) != 0) {
497 nss_warning("%p: IPv4 connection entry or connection match entry table size\
498 not available\n", nss_ctx);
499 }
500
501 return;
502}
503
504/*
505 * nss_ipv4_update_conn_count()
506 * Sets the maximum number of IPv4 connections.
507 *
508 * It first gets the connection tables size information from NSS FW
509 * and then configures the connections in NSS FW.
510 */
511int nss_ipv4_update_conn_count(int ipv4_num_conn)
512{
513 struct nss_ctx_instance *nss_ctx = nss_ipv4_get_mgr();
514 struct nss_ipv4_msg nim;
515 struct nss_ipv4_rule_conn_get_table_size_msg *nircgts;
516 nss_tx_status_t nss_tx_status;
Tushar Mathura3c18932015-08-24 20:27:21 +0530517 uint32_t sum_of_conn;
518
519 /*
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700520 * By default, NSS FW is configured with default number of connections.
521 */
Stephen Wang01433692017-10-30 14:45:11 -0700522 if (ipv4_num_conn == NSS_FW_DEFAULT_NUM_CONN) {
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700523 nss_info("%p: Default number of connections (%d) already configured\n", nss_ctx, ipv4_num_conn);
524 return 0;
525 }
526
527 /*
Tushar Mathura3c18932015-08-24 20:27:21 +0530528 * The input should be multiple of 1024.
529 * Input for ipv4 and ipv6 sum together should not exceed 8k
530 * Min. value should be at least 256 connections. This is the
531 * minimum connections we will support for each of them.
532 */
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700533 sum_of_conn = ipv4_num_conn + nss_ipv6_conn_cfg;
534 if ((ipv4_num_conn & NSS_NUM_CONN_QUANTA_MASK) ||
Tushar Mathura3c18932015-08-24 20:27:21 +0530535 (sum_of_conn > NSS_MAX_TOTAL_NUM_CONN_IPV4_IPV6) ||
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700536 (ipv4_num_conn < NSS_MIN_NUM_CONN)) {
Tushar Mathura3c18932015-08-24 20:27:21 +0530537 nss_warning("%p: input supported connections (%d) does not adhere\
538 specifications\n1) not multiple of 1024,\n2) is less than \
Cemil Coskun7d9d9b32019-06-28 15:49:39 -0700539 min val: %d, OR\n IPv4/6 total exceeds %d\n",
Tushar Mathura3c18932015-08-24 20:27:21 +0530540 nss_ctx,
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700541 ipv4_num_conn,
Tushar Mathura3c18932015-08-24 20:27:21 +0530542 NSS_MIN_NUM_CONN,
543 NSS_MAX_TOTAL_NUM_CONN_IPV4_IPV6);
544 return -EINVAL;
545 }
546
Tushar Mathura3c18932015-08-24 20:27:21 +0530547 memset(&nim, 0, sizeof(struct nss_ipv4_msg));
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700548 nss_ipv4_msg_init(&nim, NSS_IPV4_RX_INTERFACE, NSS_IPV4_TX_CONN_TABLE_SIZE_MSG,
549 sizeof(struct nss_ipv4_rule_conn_get_table_size_msg), nss_ipv4_update_conn_count_callback, NULL);
Tushar Mathura3c18932015-08-24 20:27:21 +0530550
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700551 nircgts = &nim.msg.size;
552 nircgts->num_conn = htonl(ipv4_num_conn);
Tushar Mathura3c18932015-08-24 20:27:21 +0530553 nss_tx_status = nss_ipv4_tx(nss_ctx, &nim);
554
555 if (nss_tx_status != NSS_TX_SUCCESS) {
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700556 nss_warning("%p: Send fetch connection info message failed\n", nss_ctx);
557 return -EINVAL;
Tushar Mathura3c18932015-08-24 20:27:21 +0530558 }
559
560 return 0;
561}
562
Stephen Wang0a2756a2016-08-04 15:52:47 -0700563/*
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700564 * nss_ipv4_free_conn_tables()
565 * Frees memory allocated for connection tables
566 */
567void nss_ipv4_free_conn_tables(void)
568{
569 if (nss_ipv4_ct_info.ce_mem) {
570 free_pages(nss_ipv4_ct_info.ce_mem, get_order(nss_ipv4_ct_info.ce_table_size));
571 }
572
573 if (nss_ipv4_ct_info.cme_mem) {
574 free_pages(nss_ipv4_ct_info.cme_mem, get_order(nss_ipv4_ct_info.cme_table_size));
575 }
576
577 memset(&nss_ipv4_ct_info, 0, sizeof(struct nss_ipv4_conn_table_info));
578 return;
579}
580
581/*
Stephen Wang0a2756a2016-08-04 15:52:47 -0700582 * nss_ipv4_accel_mode_cfg_handler()
583 * Configure acceleration mode for IPv4
584 */
585static int nss_ipv4_accel_mode_cfg_handler(struct ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
586{
587 struct nss_top_instance *nss_top = &nss_top_main;
588 struct nss_ctx_instance *nss_ctx = &nss_top->nss[0];
589 struct nss_ipv4_msg nim;
590 struct nss_ipv4_accel_mode_cfg_msg *nipcm;
591 nss_tx_status_t nss_tx_status;
592 int ret = NSS_FAILURE;
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700593 int current_value;
Stephen Wang0a2756a2016-08-04 15:52:47 -0700594
595 /*
596 * Take snap shot of current value
597 */
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700598 current_value = nss_ipv4_accel_mode_cfg;
Stephen Wang0a2756a2016-08-04 15:52:47 -0700599
600 /*
601 * Write the variable with user input
602 */
603 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
604 if (ret || (!write)) {
Stephen Wang0a2756a2016-08-04 15:52:47 -0700605 return ret;
606 }
607
608 memset(&nim, 0, sizeof(struct nss_ipv4_msg));
609 nss_ipv4_msg_init(&nim, NSS_IPV4_RX_INTERFACE, NSS_IPV4_TX_ACCEL_MODE_CFG_MSG,
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700610 sizeof(struct nss_ipv4_accel_mode_cfg_msg), NULL, NULL);
Stephen Wang0a2756a2016-08-04 15:52:47 -0700611
612 nipcm = &nim.msg.accel_mode_cfg;
613 nipcm->mode = htonl(nss_ipv4_accel_mode_cfg);
Stephen Wang0a2756a2016-08-04 15:52:47 -0700614
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700615 nss_tx_status = nss_ipv4_tx_sync(nss_ctx, &nim);
Stephen Wang0a2756a2016-08-04 15:52:47 -0700616 if (nss_tx_status != NSS_TX_SUCCESS) {
617 nss_warning("%p: Send acceleration mode message failed\n", nss_ctx);
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700618 nss_ipv4_accel_mode_cfg = current_value;
619 return -EIO;
620 }
621
622 return 0;
623}
624
625/*
626 * nss_ipv4_dscp_map_cfg_handler()
627 * Sysctl handler for dscp/pri mappings.
628 */
629static int nss_ipv4_dscp_map_cfg_handler(struct ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
630{
631 struct nss_top_instance *nss_top = &nss_top_main;
632 struct nss_ctx_instance *nss_ctx = &nss_top->nss[0];
633 struct nss_dscp_map_parse out;
634 struct nss_ipv4_msg nim;
635 struct nss_ipv4_dscp2pri_cfg_msg *nipd2p;
636 nss_tx_status_t status;
637 int ret;
638
639 if (!write) {
640 return nss_dscp_map_print(ctl, buffer, lenp, ppos, mapping);
641 }
642
643 ret = nss_dscp_map_parse(ctl, buffer, lenp, ppos, &out);
644 if (ret) {
645 nss_warning("failed to parse dscp mapping:%d\n", ret);
646 nss_ipv4_dscp_map_usage();
647 return ret;
648 }
649
650 if (out.action >= NSS_IPV4_DSCP_MAP_ACTION_MAX) {
651 nss_warning("invalid action value: %d\n", out.action);
652 nss_ipv4_dscp_map_usage();
653 return -EINVAL;
654 }
655
656 memset(&nim, 0, sizeof(struct nss_ipv4_msg));
657 nss_ipv4_msg_init(&nim, NSS_IPV4_RX_INTERFACE, NSS_IPV4_TX_DSCP2PRI_CFG_MSG,
658 sizeof(struct nss_ipv4_dscp2pri_cfg_msg), NULL, NULL);
659
660 nipd2p = &nim.msg.dscp2pri_cfg;
661 nipd2p->dscp = out.dscp;
662 nipd2p->priority = out.priority;
663
664 status = nss_ipv4_tx_sync(nss_ctx, &nim);
665 if (status != NSS_TX_SUCCESS) {
666 nss_warning("%p: ipv4 dscp2pri config message failed\n", nss_ctx);
667 return -EFAULT;
Stephen Wang0a2756a2016-08-04 15:52:47 -0700668 }
669
670 /*
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700671 * NSS firmware acknowleged the configuration, so update the mapping
672 * table on HOST side as well.
Stephen Wang0a2756a2016-08-04 15:52:47 -0700673 */
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700674 mapping[out.dscp].action = out.action;
675 mapping[out.dscp].priority = out.priority;
Stephen Wang0a2756a2016-08-04 15:52:47 -0700676
Stephen Wang0a2756a2016-08-04 15:52:47 -0700677 return 0;
Stephen Wang0a2756a2016-08-04 15:52:47 -0700678}
679
Stephen Wang52e6d342016-03-29 15:02:33 -0700680static struct ctl_table nss_ipv4_table[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700681 {
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700682 .procname = "ipv4_accel_mode",
683 .data = &nss_ipv4_accel_mode_cfg,
684 .maxlen = sizeof(int),
685 .mode = 0644,
686 .proc_handler = &nss_ipv4_accel_mode_cfg_handler,
687 },
688 {
689 .procname = "ipv4_dscp_map",
690 .data = &mapping[NSS_DSCP_MAP_ARRAY_SIZE],
691 .maxlen = sizeof(struct nss_dscp_map_entry),
692 .mode = 0644,
693 .proc_handler = &nss_ipv4_dscp_map_cfg_handler,
Vijay Dewangan9db18752014-09-15 16:25:01 -0700694 },
695 { }
696};
697
Stephen Wang52e6d342016-03-29 15:02:33 -0700698static struct ctl_table nss_ipv4_dir[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700699 {
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700700 .procname = "ipv4cfg",
701 .mode = 0555,
702 .child = nss_ipv4_table,
Vijay Dewangan9db18752014-09-15 16:25:01 -0700703 },
704 { }
705};
706
Stephen Wang52e6d342016-03-29 15:02:33 -0700707static struct ctl_table nss_ipv4_root_dir[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700708 {
709 .procname = "nss",
710 .mode = 0555,
711 .child = nss_ipv4_dir,
712 },
713 { }
714};
715
Stephen Wang52e6d342016-03-29 15:02:33 -0700716static struct ctl_table nss_ipv4_root[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700717 {
718 .procname = "dev",
719 .mode = 0555,
720 .child = nss_ipv4_root_dir,
721 },
722 { }
723};
724
725static struct ctl_table_header *nss_ipv4_header;
726
727/*
728 * nss_ipv4_register_sysctl()
729 * Register sysctl specific to ipv4
730 */
731void nss_ipv4_register_sysctl(void)
732{
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700733 sema_init(&nss_ipv4_pvt.sem, 1);
734 init_completion(&nss_ipv4_pvt.complete);
Stephen Wang49b474b2016-03-25 10:40:30 -0700735
Vijay Dewangan9db18752014-09-15 16:25:01 -0700736 /*
737 * Register sysctl table.
738 */
739 nss_ipv4_header = register_sysctl_table(nss_ipv4_root);
740}
741
742/*
743 * nss_ipv4_unregister_sysctl()
744 * Unregister sysctl specific to ipv4
745 */
746void nss_ipv4_unregister_sysctl(void)
747{
748 /*
749 * Unregister sysctl table.
750 */
751 if (nss_ipv4_header) {
752 unregister_sysctl_table(nss_ipv4_header);
753 }
754}
755
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700756/*
757 * nss_ipv4_msg_init()
758 * Initialize IPv4 message.
759 */
760void nss_ipv4_msg_init(struct nss_ipv4_msg *nim, uint16_t if_num, uint32_t type, uint32_t len,
Sundarajan Srinivasan30a53d42015-01-30 10:52:08 -0800761 nss_ipv4_msg_callback_t cb, void *app_data)
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700762{
763 nss_cmn_msg_init(&nim->cm, if_num, type, len, (void *)cb, app_data);
764}
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700765EXPORT_SYMBOL(nss_ipv4_msg_init);