blob: 6126dcbf940e391b77391a48482578bdf3e3bdfe [file] [log] [blame]
Thomas Wu68250352014-04-02 18:59:40 -07001/*
2 **************************************************************************
Stephen Wangaed46332016-12-12 17:29:03 -08003 * Copyright (c) 2013-2017, 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
Vijay Dewangan4861f4e2014-10-14 16:56:35 -070049int nss_ipv4_conn_cfg __read_mostly = 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 */
Sol Kavy57d24b42014-04-05 13:45:36 -0700129 if (nim->cm.response == NSS_CMM_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{
180 if (dscp > NSS_DSCP_MAP_ARRAY_SIZE) {
181 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{
236 struct nss_ipv4_msg *nim2;
237 struct nss_cmn_msg *ncm = &nim->cm;
238 struct sk_buff *nbuf;
239 int32_t status;
240
241 NSS_VERIFY_CTX_MAGIC(nss_ctx);
242 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
243 nss_warning("%p: ipv4 msg dropped as core not ready", nss_ctx);
244 return NSS_TX_FAILURE_NOT_READY;
245 }
246
247 /*
248 * Sanity check the message
249 */
250 if (ncm->interface != NSS_IPV4_RX_INTERFACE) {
251 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
252 return NSS_TX_FAILURE;
253 }
254
Murat Sezgin5c8c7362014-09-02 17:58:21 -0700255 if (ncm->type >= NSS_IPV4_MAX_MSG_TYPES) {
Thomas Wu68250352014-04-02 18:59:40 -0700256 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
257 return NSS_TX_FAILURE;
258 }
259
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800260 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ipv4_msg)) {
261 nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Thomas Wu68250352014-04-02 18:59:40 -0700262 return NSS_TX_FAILURE;
263 }
264
Stephen Wang709f9b52015-07-07 14:48:35 -0700265 if(size > PAGE_SIZE) {
266 nss_warning("%p: tx request size too large: %u", nss_ctx, size);
267 return NSS_TX_FAILURE;
268 }
269
270 nbuf = dev_alloc_skb(size);
Thomas Wu68250352014-04-02 18:59:40 -0700271 if (unlikely(!nbuf)) {
Sundarajan Srinivasan62fee7e2015-01-22 11:13:10 -0800272 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]);
Thomas Wu68250352014-04-02 18:59:40 -0700273 nss_warning("%p: msg dropped as command allocation failed", nss_ctx);
274 return NSS_TX_FAILURE;
275 }
276
277 /*
278 * Copy the message to our skb.
279 */
Suruchi Agarwalea2e5232016-09-14 10:32:08 -0700280 nim2 = (struct nss_ipv4_msg *)skb_put(nbuf, size);
Thomas Wu68250352014-04-02 18:59:40 -0700281 memcpy(nim2, nim, sizeof(struct nss_ipv4_msg));
282
Suruchi Agarwale3940e72016-07-06 15:56:51 -0700283 /*
284 * Trace messages.
285 */
286 nss_ipv4_log_tx_msg(nim);
287
Thomas Wu68250352014-04-02 18:59:40 -0700288 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
289 if (status != NSS_CORE_STATUS_SUCCESS) {
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530290 dev_kfree_skb_any(nbuf);
Sol Kavycd1bd5c2014-04-04 11:09:44 -0700291 nss_warning("%p: unable to enqueue IPv4 msg\n", nss_ctx);
Thomas Wu68250352014-04-02 18:59:40 -0700292 return NSS_TX_FAILURE;
293 }
294
Stephen Wang90c67de2016-04-26 15:15:59 -0700295 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
Thomas Wu68250352014-04-02 18:59:40 -0700296
297 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]);
298 return NSS_TX_SUCCESS;
299}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700300EXPORT_SYMBOL(nss_ipv4_tx_with_size);
Thomas Wu68250352014-04-02 18:59:40 -0700301
302/*
Stephen Wang709f9b52015-07-07 14:48:35 -0700303 * nss_ipv4_tx()
304 * Transmit an ipv4 message to the FW.
305 */
306nss_tx_status_t nss_ipv4_tx(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_msg *nim)
307{
308 return nss_ipv4_tx_with_size(nss_ctx, nim, NSS_NBUF_PAYLOAD_SIZE);
309}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700310EXPORT_SYMBOL(nss_ipv4_tx);
Stephen Wang709f9b52015-07-07 14:48:35 -0700311
312/*
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700313 * nss_ipv4_tx_sync()
314 * Transmit a synchronous ipv4 message to the FW.
315 */
316nss_tx_status_t nss_ipv4_tx_sync(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_msg *nim)
317{
318 nss_tx_status_t status;
319 int ret = 0;
320
321 down(&nss_ipv4_pvt.sem);
322 nss_ipv4_pvt.cb = (void *)nim->cm.cb;
323 nss_ipv4_pvt.app_data = (void *)nim->cm.app_data;
324
325 nim->cm.cb = (nss_ptr_t)nss_ipv4_tx_sync_callback;
326 nim->cm.app_data = (nss_ptr_t)NULL;
327
328 status = nss_ipv4_tx(nss_ctx, nim);
329 if (status != NSS_TX_SUCCESS) {
330 nss_warning("%p: nss ipv4 msg tx failed\n", nss_ctx);
331 up(&nss_ipv4_pvt.sem);
332 return status;
333 }
334
335 ret = wait_for_completion_timeout(&nss_ipv4_pvt.complete, msecs_to_jiffies(NSS_IPV4_TX_MSG_TIMEOUT));
336 if (!ret) {
337 nss_warning("%p: IPv4 tx sync failed due to timeout\n", nss_ctx);
338 nss_ipv4_pvt.response = NSS_TX_FAILURE;
339 }
340
341 status = nss_ipv4_pvt.response;
342 up(&nss_ipv4_pvt.sem);
343 return status;
344}
345EXPORT_SYMBOL(nss_ipv4_tx_sync);
346
347/*
Thomas Wu68250352014-04-02 18:59:40 -0700348 **********************************
349 Register/Unregister/Miscellaneous APIs
350 **********************************
351 */
352
353/*
354 * nss_ipv4_notify_register()
355 * Register to received IPv4 events.
356 *
357 * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv4 on any core?
358 */
359struct nss_ctx_instance *nss_ipv4_notify_register(nss_ipv4_msg_callback_t cb, void *app_data)
360{
361 /*
362 * TODO: We need to have a new array in support of the new API
363 * TODO: If we use a per-context array, we would move the array into nss_ctx based.
364 */
365 nss_top_main.ipv4_callback = cb;
Abhishek Rastogie11f47b2014-04-04 18:43:32 +0530366 nss_top_main.ipv4_ctx = app_data;
Thomas Wu68250352014-04-02 18:59:40 -0700367 return &nss_top_main.nss[nss_top_main.ipv4_handler_id];
368}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700369EXPORT_SYMBOL(nss_ipv4_notify_register);
Thomas Wu68250352014-04-02 18:59:40 -0700370
371/*
372 * nss_ipv4_notify_unregister()
373 * Unregister to received IPv4 events.
374 *
375 * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv4 on any core?
376 */
377void nss_ipv4_notify_unregister(void)
378{
379 nss_top_main.ipv4_callback = NULL;
380}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700381EXPORT_SYMBOL(nss_ipv4_notify_unregister);
Thomas Wu68250352014-04-02 18:59:40 -0700382
383/*
Suruchi Agarwal611ecd02016-08-04 15:54:35 -0700384 * nss_ipv4_conn_sync_many_notify_register()
385 * Register to receive IPv4 conn_sync_many message response.
386 */
387void nss_ipv4_conn_sync_many_notify_register(nss_ipv4_msg_callback_t cb)
388{
389 nss_ipv4_conn_sync_many_msg_cb = cb;
390}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700391EXPORT_SYMBOL(nss_ipv4_conn_sync_many_notify_register);
Suruchi Agarwal611ecd02016-08-04 15:54:35 -0700392
393/*
394 * nss_ipv4_conn_sync_many_notify_unregister()
395 * Unregister to receive IPv4 conn_sync_many message response.
396 */
397void nss_ipv4_conn_sync_many_notify_unregister(void)
398{
399 nss_ipv4_conn_sync_many_msg_cb = NULL;
400}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700401EXPORT_SYMBOL(nss_ipv4_conn_sync_many_notify_unregister);
Suruchi Agarwal611ecd02016-08-04 15:54:35 -0700402
403/*
Thomas Wu68250352014-04-02 18:59:40 -0700404 * nss_ipv4_get_mgr()
405 *
406 * TODO: This only suppports a single ipv4, do we ever want to support more?
407 */
408struct nss_ctx_instance *nss_ipv4_get_mgr(void)
409{
410 return (void *)&nss_top_main.nss[nss_top_main.ipv4_handler_id];
411}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700412EXPORT_SYMBOL(nss_ipv4_get_mgr);
Thomas Wu68250352014-04-02 18:59:40 -0700413
414/*
415 * nss_ipv4_register_handler()
416 * Register our handler to receive messages for this interface
417 */
Sol Kavy2783c072014-04-05 12:53:13 -0700418void nss_ipv4_register_handler(void)
Thomas Wu68250352014-04-02 18:59:40 -0700419{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700420 struct nss_ctx_instance *nss_ctx = nss_ipv4_get_mgr();
421
422 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 -0700423 nss_warning("IPv4 handler failed to register");
424 }
Yu Huang8c107082017-07-24 14:58:26 -0700425
426 nss_ipv4_stats_dentry_create();
Thomas Wu68250352014-04-02 18:59:40 -0700427}
428
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700429/*
430 * nss_ipv4_conn_cfg_process_callback()
431 * Call back function for the ipv4 connection configure process
432 */
433static void nss_ipv4_conn_cfg_process_callback(void *app_data, struct nss_ipv4_msg *nim)
434{
435 struct nss_ipv4_rule_conn_cfg_msg *nirccm = &nim->msg.rule_conn_cfg;
436 struct nss_ctx_instance *nss_ctx __maybe_unused = nss_ipv4_get_mgr();
437
438 if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
439 nss_warning("%p: IPv4 connection configuration failed with error: %d\n", nss_ctx, nim->cm.error);
440 nss_core_update_max_ipv4_conn(NSS_DEFAULT_NUM_CONN);
441 nss_ipv4_free_conn_tables();
442 return;
443 }
444
445 nss_ipv4_conn_cfg = ntohl(nirccm->num_conn);
446 nss_warning("%p: IPv4 connection configuration success: %d\n", nss_ctx, nim->cm.error);
447}
448
Vijay Dewangan9db18752014-09-15 16:25:01 -0700449/*
Tushar Mathura3c18932015-08-24 20:27:21 +0530450 * nss_ipv4_conn_cfg_process()
451 * Process request to configure number of ipv4 connections
452 */
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700453static int nss_ipv4_conn_cfg_process(struct nss_ctx_instance *nss_ctx, int conn)
Tushar Mathura3c18932015-08-24 20:27:21 +0530454{
455 struct nss_ipv4_msg nim;
456 struct nss_ipv4_rule_conn_cfg_msg *nirccm;
457 nss_tx_status_t nss_tx_status;
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700458
459 if ((!nss_ipv4_ct_info.ce_table_size) || (!nss_ipv4_ct_info.cme_table_size)) {
460 nss_warning("%p: connection entry or connection match entry table size not available\n",
461 nss_ctx);
462 return -EINVAL;
463 }
464
465 nss_info("%p: IPv4 supported connections: %d\n", nss_ctx, conn);
466
467 nss_ipv4_ct_info.ce_mem = __get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
468 get_order(nss_ipv4_ct_info.ce_table_size));
469 if (!nss_ipv4_ct_info.ce_mem) {
470 nss_warning("%p: Memory allocation failed for IPv4 Connections: %d\n",
471 nss_ctx,
472 conn);
473 goto fail;
474 }
475
476 nss_ipv4_ct_info.cme_mem = __get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
477 get_order(nss_ipv4_ct_info.cme_table_size));
478 if (!nss_ipv4_ct_info.ce_mem) {
479 nss_warning("%p: Memory allocation failed for IPv4 Connections: %d\n",
480 nss_ctx,
481 conn);
482 goto fail;
483 }
484
485 memset(&nim, 0, sizeof(struct nss_ipv4_msg));
486 nss_ipv4_msg_init(&nim, NSS_IPV4_RX_INTERFACE, NSS_IPV4_TX_CONN_CFG_RULE_MSG,
487 sizeof(struct nss_ipv4_rule_conn_cfg_msg), nss_ipv4_conn_cfg_process_callback, NULL);
488
489 nirccm = &nim.msg.rule_conn_cfg;
490 nirccm->num_conn = htonl(conn);
Suruchi Agarwal8c9e7162017-08-17 15:58:40 -0700491 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);
492 if (unlikely(dma_mapping_error(nss_ctx->dev, nirccm->ce_mem))) {
493 nss_warning("%p: DMA mapping failed for virtual address = %p", nss_ctx, (void *)nss_ipv4_ct_info.ce_mem);
494 goto fail;
495 }
496
497 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);
498 if (unlikely(dma_mapping_error(nss_ctx->dev, nirccm->cme_mem))) {
499 nss_warning("%p: DMA mapping failed for virtual address = %p", nss_ctx, (void *)nss_ipv4_ct_info.cme_mem);
500 goto fail;
501 }
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700502
503 nss_tx_status = nss_ipv4_tx(nss_ctx, &nim);
504 if (nss_tx_status != NSS_TX_SUCCESS) {
505 nss_warning("%p: nss_tx error setting IPv4 Connections: %d\n",
506 nss_ctx,
507 conn);
508 goto fail;
509 }
510
511 return 0;
512
513fail:
514 nss_ipv4_free_conn_tables();
515 return -EINVAL;;
516}
517
518/*
519 * nss_ipv4_update_conn_count_callback()
520 * Callback function for the ipv4 get connection info message.
521 */
522static void nss_ipv4_update_conn_count_callback(void *app_data, struct nss_ipv4_msg *nim)
523{
524 struct nss_ipv4_rule_conn_get_table_size_msg *nircgts = &nim->msg.size;
525 struct nss_ctx_instance *nss_ctx = nss_ipv4_get_mgr();
526
527 if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
528 nss_warning("%p: IPv4 fetch connection info failed with error: %d\n", nss_ctx, nim->cm.error);
529 nss_core_update_max_ipv4_conn(NSS_DEFAULT_NUM_CONN);
530 return;
531 }
532
533 nss_info("IPv4 get connection info success\n");
534
535 nss_ipv4_ct_info.ce_table_size = ntohl(nircgts->ce_table_size);
536 nss_ipv4_ct_info.cme_table_size = ntohl(nircgts->cme_table_size);
537
538 if (nss_ipv4_conn_cfg_process(nss_ctx, ntohl(nircgts->num_conn)) != 0) {
539 nss_warning("%p: IPv4 connection entry or connection match entry table size\
540 not available\n", nss_ctx);
541 }
542
543 return;
544}
545
546/*
547 * nss_ipv4_update_conn_count()
548 * Sets the maximum number of IPv4 connections.
549 *
550 * It first gets the connection tables size information from NSS FW
551 * and then configures the connections in NSS FW.
552 */
553int nss_ipv4_update_conn_count(int ipv4_num_conn)
554{
555 struct nss_ctx_instance *nss_ctx = nss_ipv4_get_mgr();
556 struct nss_ipv4_msg nim;
557 struct nss_ipv4_rule_conn_get_table_size_msg *nircgts;
558 nss_tx_status_t nss_tx_status;
Tushar Mathura3c18932015-08-24 20:27:21 +0530559 uint32_t sum_of_conn;
560
561 /*
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700562 * By default, NSS FW is configured with default number of connections.
563 */
564 if (ipv4_num_conn == NSS_DEFAULT_NUM_CONN) {
565 nss_info("%p: Default number of connections (%d) already configured\n", nss_ctx, ipv4_num_conn);
566 return 0;
567 }
568
569 /*
Tushar Mathura3c18932015-08-24 20:27:21 +0530570 * The input should be multiple of 1024.
571 * Input for ipv4 and ipv6 sum together should not exceed 8k
572 * Min. value should be at least 256 connections. This is the
573 * minimum connections we will support for each of them.
574 */
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700575 sum_of_conn = ipv4_num_conn + nss_ipv6_conn_cfg;
576 if ((ipv4_num_conn & NSS_NUM_CONN_QUANTA_MASK) ||
Tushar Mathura3c18932015-08-24 20:27:21 +0530577 (sum_of_conn > NSS_MAX_TOTAL_NUM_CONN_IPV4_IPV6) ||
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700578 (ipv4_num_conn < NSS_MIN_NUM_CONN)) {
Tushar Mathura3c18932015-08-24 20:27:21 +0530579 nss_warning("%p: input supported connections (%d) does not adhere\
580 specifications\n1) not multiple of 1024,\n2) is less than \
581 min val: %d, OR\n IPv4/6 total exceeds %d\n",
582 nss_ctx,
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700583 ipv4_num_conn,
Tushar Mathura3c18932015-08-24 20:27:21 +0530584 NSS_MIN_NUM_CONN,
585 NSS_MAX_TOTAL_NUM_CONN_IPV4_IPV6);
586 return -EINVAL;
587 }
588
Tushar Mathura3c18932015-08-24 20:27:21 +0530589 memset(&nim, 0, sizeof(struct nss_ipv4_msg));
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700590 nss_ipv4_msg_init(&nim, NSS_IPV4_RX_INTERFACE, NSS_IPV4_TX_CONN_TABLE_SIZE_MSG,
591 sizeof(struct nss_ipv4_rule_conn_get_table_size_msg), nss_ipv4_update_conn_count_callback, NULL);
Tushar Mathura3c18932015-08-24 20:27:21 +0530592
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700593 nircgts = &nim.msg.size;
594 nircgts->num_conn = htonl(ipv4_num_conn);
Tushar Mathura3c18932015-08-24 20:27:21 +0530595 nss_tx_status = nss_ipv4_tx(nss_ctx, &nim);
596
597 if (nss_tx_status != NSS_TX_SUCCESS) {
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700598 nss_warning("%p: Send fetch connection info message failed\n", nss_ctx);
599 return -EINVAL;
Tushar Mathura3c18932015-08-24 20:27:21 +0530600 }
601
602 return 0;
603}
604
Stephen Wang0a2756a2016-08-04 15:52:47 -0700605/*
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700606 * nss_ipv4_free_conn_tables()
607 * Frees memory allocated for connection tables
608 */
609void nss_ipv4_free_conn_tables(void)
610{
611 if (nss_ipv4_ct_info.ce_mem) {
612 free_pages(nss_ipv4_ct_info.ce_mem, get_order(nss_ipv4_ct_info.ce_table_size));
613 }
614
615 if (nss_ipv4_ct_info.cme_mem) {
616 free_pages(nss_ipv4_ct_info.cme_mem, get_order(nss_ipv4_ct_info.cme_table_size));
617 }
618
619 memset(&nss_ipv4_ct_info, 0, sizeof(struct nss_ipv4_conn_table_info));
620 return;
621}
622
623/*
Stephen Wang0a2756a2016-08-04 15:52:47 -0700624 * nss_ipv4_accel_mode_cfg_handler()
625 * Configure acceleration mode for IPv4
626 */
627static int nss_ipv4_accel_mode_cfg_handler(struct ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
628{
629 struct nss_top_instance *nss_top = &nss_top_main;
630 struct nss_ctx_instance *nss_ctx = &nss_top->nss[0];
631 struct nss_ipv4_msg nim;
632 struct nss_ipv4_accel_mode_cfg_msg *nipcm;
633 nss_tx_status_t nss_tx_status;
634 int ret = NSS_FAILURE;
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700635 int current_value;
Stephen Wang0a2756a2016-08-04 15:52:47 -0700636
637 /*
638 * Take snap shot of current value
639 */
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700640 current_value = nss_ipv4_accel_mode_cfg;
Stephen Wang0a2756a2016-08-04 15:52:47 -0700641
642 /*
643 * Write the variable with user input
644 */
645 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
646 if (ret || (!write)) {
Stephen Wang0a2756a2016-08-04 15:52:47 -0700647 return ret;
648 }
649
650 memset(&nim, 0, sizeof(struct nss_ipv4_msg));
651 nss_ipv4_msg_init(&nim, NSS_IPV4_RX_INTERFACE, NSS_IPV4_TX_ACCEL_MODE_CFG_MSG,
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700652 sizeof(struct nss_ipv4_accel_mode_cfg_msg), NULL, NULL);
Stephen Wang0a2756a2016-08-04 15:52:47 -0700653
654 nipcm = &nim.msg.accel_mode_cfg;
655 nipcm->mode = htonl(nss_ipv4_accel_mode_cfg);
Stephen Wang0a2756a2016-08-04 15:52:47 -0700656
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700657 nss_tx_status = nss_ipv4_tx_sync(nss_ctx, &nim);
Stephen Wang0a2756a2016-08-04 15:52:47 -0700658 if (nss_tx_status != NSS_TX_SUCCESS) {
659 nss_warning("%p: Send acceleration mode message failed\n", nss_ctx);
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700660 nss_ipv4_accel_mode_cfg = current_value;
661 return -EIO;
662 }
663
664 return 0;
665}
666
667/*
668 * nss_ipv4_dscp_map_cfg_handler()
669 * Sysctl handler for dscp/pri mappings.
670 */
671static int nss_ipv4_dscp_map_cfg_handler(struct ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
672{
673 struct nss_top_instance *nss_top = &nss_top_main;
674 struct nss_ctx_instance *nss_ctx = &nss_top->nss[0];
675 struct nss_dscp_map_parse out;
676 struct nss_ipv4_msg nim;
677 struct nss_ipv4_dscp2pri_cfg_msg *nipd2p;
678 nss_tx_status_t status;
679 int ret;
680
681 if (!write) {
682 return nss_dscp_map_print(ctl, buffer, lenp, ppos, mapping);
683 }
684
685 ret = nss_dscp_map_parse(ctl, buffer, lenp, ppos, &out);
686 if (ret) {
687 nss_warning("failed to parse dscp mapping:%d\n", ret);
688 nss_ipv4_dscp_map_usage();
689 return ret;
690 }
691
692 if (out.action >= NSS_IPV4_DSCP_MAP_ACTION_MAX) {
693 nss_warning("invalid action value: %d\n", out.action);
694 nss_ipv4_dscp_map_usage();
695 return -EINVAL;
696 }
697
698 memset(&nim, 0, sizeof(struct nss_ipv4_msg));
699 nss_ipv4_msg_init(&nim, NSS_IPV4_RX_INTERFACE, NSS_IPV4_TX_DSCP2PRI_CFG_MSG,
700 sizeof(struct nss_ipv4_dscp2pri_cfg_msg), NULL, NULL);
701
702 nipd2p = &nim.msg.dscp2pri_cfg;
703 nipd2p->dscp = out.dscp;
704 nipd2p->priority = out.priority;
705
706 status = nss_ipv4_tx_sync(nss_ctx, &nim);
707 if (status != NSS_TX_SUCCESS) {
708 nss_warning("%p: ipv4 dscp2pri config message failed\n", nss_ctx);
709 return -EFAULT;
Stephen Wang0a2756a2016-08-04 15:52:47 -0700710 }
711
712 /*
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700713 * NSS firmware acknowleged the configuration, so update the mapping
714 * table on HOST side as well.
Stephen Wang0a2756a2016-08-04 15:52:47 -0700715 */
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700716 mapping[out.dscp].action = out.action;
717 mapping[out.dscp].priority = out.priority;
Stephen Wang0a2756a2016-08-04 15:52:47 -0700718
Stephen Wang0a2756a2016-08-04 15:52:47 -0700719 return 0;
Stephen Wang0a2756a2016-08-04 15:52:47 -0700720}
721
Stephen Wang52e6d342016-03-29 15:02:33 -0700722static struct ctl_table nss_ipv4_table[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700723 {
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700724 .procname = "ipv4_accel_mode",
725 .data = &nss_ipv4_accel_mode_cfg,
726 .maxlen = sizeof(int),
727 .mode = 0644,
728 .proc_handler = &nss_ipv4_accel_mode_cfg_handler,
729 },
730 {
731 .procname = "ipv4_dscp_map",
732 .data = &mapping[NSS_DSCP_MAP_ARRAY_SIZE],
733 .maxlen = sizeof(struct nss_dscp_map_entry),
734 .mode = 0644,
735 .proc_handler = &nss_ipv4_dscp_map_cfg_handler,
Vijay Dewangan9db18752014-09-15 16:25:01 -0700736 },
737 { }
738};
739
Stephen Wang52e6d342016-03-29 15:02:33 -0700740static struct ctl_table nss_ipv4_dir[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700741 {
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700742 .procname = "ipv4cfg",
743 .mode = 0555,
744 .child = nss_ipv4_table,
Vijay Dewangan9db18752014-09-15 16:25:01 -0700745 },
746 { }
747};
748
Stephen Wang52e6d342016-03-29 15:02:33 -0700749static struct ctl_table nss_ipv4_root_dir[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700750 {
751 .procname = "nss",
752 .mode = 0555,
753 .child = nss_ipv4_dir,
754 },
755 { }
756};
757
Stephen Wang52e6d342016-03-29 15:02:33 -0700758static struct ctl_table nss_ipv4_root[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700759 {
760 .procname = "dev",
761 .mode = 0555,
762 .child = nss_ipv4_root_dir,
763 },
764 { }
765};
766
767static struct ctl_table_header *nss_ipv4_header;
768
769/*
770 * nss_ipv4_register_sysctl()
771 * Register sysctl specific to ipv4
772 */
773void nss_ipv4_register_sysctl(void)
774{
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700775 sema_init(&nss_ipv4_pvt.sem, 1);
776 init_completion(&nss_ipv4_pvt.complete);
Stephen Wang49b474b2016-03-25 10:40:30 -0700777
Vijay Dewangan9db18752014-09-15 16:25:01 -0700778 /*
779 * Register sysctl table.
780 */
781 nss_ipv4_header = register_sysctl_table(nss_ipv4_root);
782}
783
784/*
785 * nss_ipv4_unregister_sysctl()
786 * Unregister sysctl specific to ipv4
787 */
788void nss_ipv4_unregister_sysctl(void)
789{
790 /*
791 * Unregister sysctl table.
792 */
793 if (nss_ipv4_header) {
794 unregister_sysctl_table(nss_ipv4_header);
795 }
796}
797
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700798/*
799 * nss_ipv4_msg_init()
800 * Initialize IPv4 message.
801 */
802void 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 -0800803 nss_ipv4_msg_callback_t cb, void *app_data)
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700804{
805 nss_cmn_msg_init(&nim->cm, if_num, type, len, (void *)cb, app_data);
806}
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700807EXPORT_SYMBOL(nss_ipv4_msg_init);