blob: 8b0e5cbf28d497b696a02e296d18fc53956191e9 [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"
Thomas Wu68250352014-04-02 18:59:40 -070023
Stephen Wang0a2756a2016-08-04 15:52:47 -070024#define NSS_IPV4_TX_MSG_TIMEOUT 1000 /* 1 sec timeout for IPv4 messages */
25
26/*
27 * Private data structure for ipv4 configure messages
28 */
29struct nss_ipv4_cfg_pvt {
30 struct semaphore sem; /* Semaphore structure */
31 struct completion complete; /* completion structure */
32 int current_value; /* valid entry */
33 int response; /* Response from FW */
34};
35
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -070036/*
37 * Private data structure for ipv4 connection information.
38 */
39struct nss_ipv4_conn_table_info {
40 uint32_t ce_table_size; /* Size of connection table entry in NSS FW */
41 uint32_t cme_table_size; /* Size of connection match table entry in NSS FW */
42 unsigned long ce_mem; /* Start address for connection entry table */
43 unsigned long cme_mem; /* Start address for connection match entry table */
44} nss_ipv4_ct_info;
45
Vijay Dewangan4861f4e2014-10-14 16:56:35 -070046int nss_ipv4_conn_cfg __read_mostly = NSS_DEFAULT_NUM_CONN;
Stephen Wang0a2756a2016-08-04 15:52:47 -070047int nss_ipv4_accel_mode_cfg __read_mostly = 1;
48
Stephen Wang0a2756a2016-08-04 15:52:47 -070049static struct nss_ipv4_cfg_pvt i4_accel_mode_cfgp;
Vijay Dewangan9db18752014-09-15 16:25:01 -070050
Thomas Wu68250352014-04-02 18:59:40 -070051/*
Suruchi Agarwal611ecd02016-08-04 15:54:35 -070052 * Callback for conn_sync_many request message.
53 */
54nss_ipv4_msg_callback_t nss_ipv4_conn_sync_many_msg_cb = NULL;
55
56/*
Gareth Williams958aa822015-02-04 19:36:39 +000057 * nss_ipv4_max_conn_count()
58 * Return the maximum number of IPv4 connections that the NSS acceleration engine supports.
59 */
60int nss_ipv4_max_conn_count(void)
61{
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -070062 return nss_ipv4_conn_cfg;
Gareth Williams958aa822015-02-04 19:36:39 +000063}
64EXPORT_SYMBOL(nss_ipv4_max_conn_count);
65
66/*
Guojun Jin9a1cb282017-07-13 17:32:07 -070067 * nss_ipv4_conn_inquiry()
68 * Inquiry if a connection has been established in NSS FW
69 */
70nss_tx_status_t nss_ipv4_conn_inquiry(struct nss_ipv4_5tuple *ipv4_5t_p,
71 nss_ipv4_msg_callback_t cb)
72{
73 nss_tx_status_t nss_tx_status;
74 struct nss_ipv4_msg nim;
75 struct nss_ctx_instance *nss_ctx = &nss_top_main.nss[0];
76
77 /*
78 * Initialize inquiry message structure.
79 * This is async message and the result will be returned
80 * to the caller by the msg_callback passed in.
81 */
82 memset(&nim, 0, sizeof(nim));
83 nss_ipv4_msg_init(&nim, NSS_IPV4_RX_INTERFACE,
84 NSS_IPV4_TX_CONN_CFG_INQUIRY_MSG,
85 sizeof(struct nss_ipv4_inquiry_msg),
86 cb, NULL);
87 nim.msg.inquiry.rr.tuple = *ipv4_5t_p;
88 nss_tx_status = nss_ipv4_tx(nss_ctx, &nim);
89 if (nss_tx_status != NSS_TX_SUCCESS) {
90 nss_warning("%p: Send inquiry message failed\n", ipv4_5t_p);
91 }
92
93 return nss_tx_status;
94}
95EXPORT_SYMBOL(nss_ipv4_conn_inquiry);
96
97/*
Sol Kavy57d24b42014-04-05 13:45:36 -070098 * nss_ipv4_driver_conn_sync_update()
Thomas Wu68250352014-04-02 18:59:40 -070099 * Update driver specific information from the messsage.
100 */
Murat Sezgin0c0561d2014-04-09 18:55:58 -0700101static void nss_ipv4_driver_conn_sync_update(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_conn_sync *nirs)
Thomas Wu68250352014-04-02 18:59:40 -0700102{
Sol Kavy57d24b42014-04-05 13:45:36 -0700103 struct nss_top_instance *nss_top = nss_ctx->nss_top;
Sol Kavy57d24b42014-04-05 13:45:36 -0700104
Thomas Wu68250352014-04-02 18:59:40 -0700105 /*
106 * Update statistics maintained by NSS driver
107 */
108 spin_lock_bh(&nss_top->stats_lock);
Thomas Wu68250352014-04-02 18:59:40 -0700109 nss_top->stats_ipv4[NSS_STATS_IPV4_ACCELERATED_RX_PKTS] += nirs->flow_rx_packet_count + nirs->return_rx_packet_count;
110 nss_top->stats_ipv4[NSS_STATS_IPV4_ACCELERATED_RX_BYTES] += nirs->flow_rx_byte_count + nirs->return_rx_byte_count;
111 nss_top->stats_ipv4[NSS_STATS_IPV4_ACCELERATED_TX_PKTS] += nirs->flow_tx_packet_count + nirs->return_tx_packet_count;
112 nss_top->stats_ipv4[NSS_STATS_IPV4_ACCELERATED_TX_BYTES] += nirs->flow_tx_byte_count + nirs->return_tx_byte_count;
Thomas Wu68250352014-04-02 18:59:40 -0700113 spin_unlock_bh(&nss_top->stats_lock);
114}
Thomas Wu68250352014-04-02 18:59:40 -0700115
116/*
Stephen Wang709f9b52015-07-07 14:48:35 -0700117 * nss_ipv4_driver_conn_sync_many_update()
118 * Update driver specific information from the conn_sync_many messsage.
119 */
120static void nss_ipv4_driver_conn_sync_many_update(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_conn_sync_many_msg *nicsm)
121{
122 int i;
123
124 /*
125 * Sanity check for the stats count
126 */
127 if (nicsm->count * sizeof(struct nss_ipv4_conn_sync) >= nicsm->size) {
128 nss_warning("%p: stats sync count %u exceeds the size of this msg %u", nss_ctx, nicsm->count, nicsm->size);
129 return;
130 }
131
132 for (i = 0; i < nicsm->count; i++) {
133 nss_ipv4_driver_conn_sync_update(nss_ctx, &nicsm->conn_sync[i]);
134 }
135}
136
137/*
Murat Sezgin0c0561d2014-04-09 18:55:58 -0700138 * nss_ipv4_driver_node_sync_update)
139 * Update driver specific information from the messsage.
140 */
141static void nss_ipv4_driver_node_sync_update(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_node_sync *nins)
142{
143 struct nss_top_instance *nss_top = nss_ctx->nss_top;
144 uint32_t i;
145
146 /*
147 * Update statistics maintained by NSS driver
148 */
149 spin_lock_bh(&nss_top->stats_lock);
150 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_RX_PKTS] += nins->node_stats.rx_packets;
151 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_RX_BYTES] += nins->node_stats.rx_bytes;
152 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_RX_DROPPED] += nins->node_stats.rx_dropped;
153 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_TX_PKTS] += nins->node_stats.tx_packets;
154 nss_top->stats_node[NSS_IPV4_RX_INTERFACE][NSS_STATS_NODE_TX_BYTES] += nins->node_stats.tx_bytes;
155
Murat Sezgin0c0561d2014-04-09 18:55:58 -0700156 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_CREATE_REQUESTS] += nins->ipv4_connection_create_requests;
157 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_CREATE_COLLISIONS] += nins->ipv4_connection_create_collisions;
158 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_CREATE_INVALID_INTERFACE] += nins->ipv4_connection_create_invalid_interface;
159 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_DESTROY_REQUESTS] += nins->ipv4_connection_destroy_requests;
160 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_DESTROY_MISSES] += nins->ipv4_connection_destroy_misses;
161 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_HASH_HITS] += nins->ipv4_connection_hash_hits;
162 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_HASH_REORDERS] += nins->ipv4_connection_hash_reorders;
163 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_FLUSHES] += nins->ipv4_connection_flushes;
164 nss_top->stats_ipv4[NSS_STATS_IPV4_CONNECTION_EVICTIONS] += nins->ipv4_connection_evictions;
Selin Dag60ea2b22014-11-05 09:36:22 -0800165 nss_top->stats_ipv4[NSS_STATS_IPV4_FRAGMENTATIONS] += nins->ipv4_fragmentations;
Kiran Kumar C.S.Kfadf92d2015-06-26 12:48:33 +0530166 nss_top->stats_ipv4[NSS_STATS_IPV4_MC_CONNECTION_CREATE_REQUESTS] += nins->ipv4_mc_connection_create_requests;
167 nss_top->stats_ipv4[NSS_STATS_IPV4_MC_CONNECTION_UPDATE_REQUESTS] += nins->ipv4_mc_connection_update_requests;
168 nss_top->stats_ipv4[NSS_STATS_IPV4_MC_CONNECTION_CREATE_INVALID_INTERFACE] += nins->ipv4_mc_connection_create_invalid_interface;
169 nss_top->stats_ipv4[NSS_STATS_IPV4_MC_CONNECTION_DESTROY_REQUESTS] += nins->ipv4_mc_connection_destroy_requests;
170 nss_top->stats_ipv4[NSS_STATS_IPV4_MC_CONNECTION_DESTROY_MISSES] += nins->ipv4_mc_connection_destroy_misses;
171 nss_top->stats_ipv4[NSS_STATS_IPV4_MC_CONNECTION_FLUSHES] += nins->ipv4_mc_connection_flushes;
Jackson Bockus67fc0a82017-08-09 10:30:56 -0700172 nss_top->stats_ipv4[NSS_STATS_IPV4_DROPPED_BY_RULE] += nins->ipv4_dropped_by_rule;
Murat Sezgin0c0561d2014-04-09 18:55:58 -0700173
174 for (i = 0; i < NSS_EXCEPTION_EVENT_IPV4_MAX; i++) {
175 nss_top->stats_if_exception_ipv4[i] += nins->exception_events[i];
176 }
177 spin_unlock_bh(&nss_top->stats_lock);
178}
179
180/*
Sol Kavy2783c072014-04-05 12:53:13 -0700181 * nss_ipv4_rx_msg_handler()
Thomas Wu68250352014-04-02 18:59:40 -0700182 * Handle NSS -> HLOS messages for IPv4 bridge/route
183 */
Sol Kavy2783c072014-04-05 12:53:13 -0700184static 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 -0700185{
186 struct nss_ipv4_msg *nim = (struct nss_ipv4_msg *)ncm;
Sol Kavy57d24b42014-04-05 13:45:36 -0700187 nss_ipv4_msg_callback_t cb;
Thomas Wu68250352014-04-02 18:59:40 -0700188
189 BUG_ON(ncm->interface != NSS_IPV4_RX_INTERFACE);
190
191 /*
192 * Sanity check the message type
193 */
Murat Sezgin5c8c7362014-09-02 17:58:21 -0700194 if (ncm->type >= NSS_IPV4_MAX_MSG_TYPES) {
Thomas Wu68250352014-04-02 18:59:40 -0700195 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
196 return;
197 }
198
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800199 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ipv4_msg)) {
200 nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Thomas Wu68250352014-04-02 18:59:40 -0700201 return;
202 }
203
Thomas Wu68250352014-04-02 18:59:40 -0700204 /*
Suruchi Agarwale3940e72016-07-06 15:56:51 -0700205 * Trace messages.
Thomas Wu68250352014-04-02 18:59:40 -0700206 */
Suruchi Agarwale3940e72016-07-06 15:56:51 -0700207 nss_ipv4_log_rx_msg(nim);
Thomas Wu68250352014-04-02 18:59:40 -0700208
Thomas Wu68250352014-04-02 18:59:40 -0700209 switch (nim->cm.type) {
Murat Sezgin0c0561d2014-04-09 18:55:58 -0700210 case NSS_IPV4_RX_NODE_STATS_SYNC_MSG:
211 /*
212 * Update driver statistics on node sync.
213 */
214 nss_ipv4_driver_node_sync_update(nss_ctx, &nim->msg.node_stats);
215 break;
216
Thomas Wu68250352014-04-02 18:59:40 -0700217 case NSS_IPV4_RX_CONN_STATS_SYNC_MSG:
Sol Kavy57d24b42014-04-05 13:45:36 -0700218 /*
219 * Update driver statistics on connection sync.
220 */
221 nss_ipv4_driver_conn_sync_update(nss_ctx, &nim->msg.conn_stats);
Sakthi Vignesh Radhakrishnan515f8c22014-06-21 15:04:19 -0700222 break;
Stephen Wang709f9b52015-07-07 14:48:35 -0700223
224 case NSS_IPV4_TX_CONN_STATS_SYNC_MANY_MSG:
225 /*
226 * Update driver statistics on connection sync many.
227 */
228 nss_ipv4_driver_conn_sync_many_update(nss_ctx, &nim->msg.conn_stats_many);
Stephen Wangaed46332016-12-12 17:29:03 -0800229 ncm->cb = (nss_ptr_t)nss_ipv4_conn_sync_many_msg_cb;
Stephen Wang709f9b52015-07-07 14:48:35 -0700230 break;
Thomas Wu68250352014-04-02 18:59:40 -0700231 }
232
233 /*
Sol Kavy57d24b42014-04-05 13:45:36 -0700234 * Update the callback and app_data for NOTIFY messages, IPv4 sends all notify messages
235 * to the same callback/app_data.
Thomas Wu68250352014-04-02 18:59:40 -0700236 */
Sol Kavy57d24b42014-04-05 13:45:36 -0700237 if (nim->cm.response == NSS_CMM_RESPONSE_NOTIFY) {
Stephen Wangaed46332016-12-12 17:29:03 -0800238 ncm->cb = (nss_ptr_t)nss_ctx->nss_top->ipv4_callback;
239 ncm->app_data = (nss_ptr_t)nss_ctx->nss_top->ipv4_ctx;
Sol Kavy57d24b42014-04-05 13:45:36 -0700240 }
Sol Kavy2783c072014-04-05 12:53:13 -0700241
Thomas Wu68250352014-04-02 18:59:40 -0700242 /*
243 * Do we have a callback?
244 */
245 if (!ncm->cb) {
246 return;
247 }
248
249 /*
250 * Callback
251 */
Sol Kavy57d24b42014-04-05 13:45:36 -0700252 cb = (nss_ipv4_msg_callback_t)ncm->cb;
Thomas Wu68250352014-04-02 18:59:40 -0700253 cb((void *)ncm->app_data, nim);
Thomas Wu68250352014-04-02 18:59:40 -0700254}
255
256/*
Stephen Wang709f9b52015-07-07 14:48:35 -0700257 * nss_ipv4_tx_with_size()
258 * Transmit an ipv4 message to the FW with a specified size.
Thomas Wu68250352014-04-02 18:59:40 -0700259 */
Stephen Wang709f9b52015-07-07 14:48:35 -0700260nss_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 -0700261{
262 struct nss_ipv4_msg *nim2;
263 struct nss_cmn_msg *ncm = &nim->cm;
264 struct sk_buff *nbuf;
265 int32_t status;
266
267 NSS_VERIFY_CTX_MAGIC(nss_ctx);
268 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
269 nss_warning("%p: ipv4 msg dropped as core not ready", nss_ctx);
270 return NSS_TX_FAILURE_NOT_READY;
271 }
272
273 /*
274 * Sanity check the message
275 */
276 if (ncm->interface != NSS_IPV4_RX_INTERFACE) {
277 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
278 return NSS_TX_FAILURE;
279 }
280
Murat Sezgin5c8c7362014-09-02 17:58:21 -0700281 if (ncm->type >= NSS_IPV4_MAX_MSG_TYPES) {
Thomas Wu68250352014-04-02 18:59:40 -0700282 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
283 return NSS_TX_FAILURE;
284 }
285
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800286 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ipv4_msg)) {
287 nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Thomas Wu68250352014-04-02 18:59:40 -0700288 return NSS_TX_FAILURE;
289 }
290
Stephen Wang709f9b52015-07-07 14:48:35 -0700291 if(size > PAGE_SIZE) {
292 nss_warning("%p: tx request size too large: %u", nss_ctx, size);
293 return NSS_TX_FAILURE;
294 }
295
296 nbuf = dev_alloc_skb(size);
Thomas Wu68250352014-04-02 18:59:40 -0700297 if (unlikely(!nbuf)) {
Sundarajan Srinivasan62fee7e2015-01-22 11:13:10 -0800298 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 -0700299 nss_warning("%p: msg dropped as command allocation failed", nss_ctx);
300 return NSS_TX_FAILURE;
301 }
302
303 /*
304 * Copy the message to our skb.
305 */
Suruchi Agarwalea2e5232016-09-14 10:32:08 -0700306 nim2 = (struct nss_ipv4_msg *)skb_put(nbuf, size);
Thomas Wu68250352014-04-02 18:59:40 -0700307 memcpy(nim2, nim, sizeof(struct nss_ipv4_msg));
308
Suruchi Agarwale3940e72016-07-06 15:56:51 -0700309 /*
310 * Trace messages.
311 */
312 nss_ipv4_log_tx_msg(nim);
313
Thomas Wu68250352014-04-02 18:59:40 -0700314 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
315 if (status != NSS_CORE_STATUS_SUCCESS) {
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530316 dev_kfree_skb_any(nbuf);
Sol Kavycd1bd5c2014-04-04 11:09:44 -0700317 nss_warning("%p: unable to enqueue IPv4 msg\n", nss_ctx);
Thomas Wu68250352014-04-02 18:59:40 -0700318 return NSS_TX_FAILURE;
319 }
320
Stephen Wang90c67de2016-04-26 15:15:59 -0700321 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
Thomas Wu68250352014-04-02 18:59:40 -0700322
323 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]);
324 return NSS_TX_SUCCESS;
325}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700326EXPORT_SYMBOL(nss_ipv4_tx_with_size);
Thomas Wu68250352014-04-02 18:59:40 -0700327
328/*
Stephen Wang709f9b52015-07-07 14:48:35 -0700329 * nss_ipv4_tx()
330 * Transmit an ipv4 message to the FW.
331 */
332nss_tx_status_t nss_ipv4_tx(struct nss_ctx_instance *nss_ctx, struct nss_ipv4_msg *nim)
333{
334 return nss_ipv4_tx_with_size(nss_ctx, nim, NSS_NBUF_PAYLOAD_SIZE);
335}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700336EXPORT_SYMBOL(nss_ipv4_tx);
Stephen Wang709f9b52015-07-07 14:48:35 -0700337
338/*
Thomas Wu68250352014-04-02 18:59:40 -0700339 **********************************
340 Register/Unregister/Miscellaneous APIs
341 **********************************
342 */
343
344/*
345 * nss_ipv4_notify_register()
346 * Register to received IPv4 events.
347 *
348 * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv4 on any core?
349 */
350struct nss_ctx_instance *nss_ipv4_notify_register(nss_ipv4_msg_callback_t cb, void *app_data)
351{
352 /*
353 * TODO: We need to have a new array in support of the new API
354 * TODO: If we use a per-context array, we would move the array into nss_ctx based.
355 */
356 nss_top_main.ipv4_callback = cb;
Abhishek Rastogie11f47b2014-04-04 18:43:32 +0530357 nss_top_main.ipv4_ctx = app_data;
Thomas Wu68250352014-04-02 18:59:40 -0700358 return &nss_top_main.nss[nss_top_main.ipv4_handler_id];
359}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700360EXPORT_SYMBOL(nss_ipv4_notify_register);
Thomas Wu68250352014-04-02 18:59:40 -0700361
362/*
363 * nss_ipv4_notify_unregister()
364 * Unregister to received IPv4 events.
365 *
366 * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv4 on any core?
367 */
368void nss_ipv4_notify_unregister(void)
369{
370 nss_top_main.ipv4_callback = NULL;
371}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700372EXPORT_SYMBOL(nss_ipv4_notify_unregister);
Thomas Wu68250352014-04-02 18:59:40 -0700373
374/*
Suruchi Agarwal611ecd02016-08-04 15:54:35 -0700375 * nss_ipv4_conn_sync_many_notify_register()
376 * Register to receive IPv4 conn_sync_many message response.
377 */
378void nss_ipv4_conn_sync_many_notify_register(nss_ipv4_msg_callback_t cb)
379{
380 nss_ipv4_conn_sync_many_msg_cb = cb;
381}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700382EXPORT_SYMBOL(nss_ipv4_conn_sync_many_notify_register);
Suruchi Agarwal611ecd02016-08-04 15:54:35 -0700383
384/*
385 * nss_ipv4_conn_sync_many_notify_unregister()
386 * Unregister to receive IPv4 conn_sync_many message response.
387 */
388void nss_ipv4_conn_sync_many_notify_unregister(void)
389{
390 nss_ipv4_conn_sync_many_msg_cb = NULL;
391}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700392EXPORT_SYMBOL(nss_ipv4_conn_sync_many_notify_unregister);
Suruchi Agarwal611ecd02016-08-04 15:54:35 -0700393
394/*
Thomas Wu68250352014-04-02 18:59:40 -0700395 * nss_ipv4_get_mgr()
396 *
397 * TODO: This only suppports a single ipv4, do we ever want to support more?
398 */
399struct nss_ctx_instance *nss_ipv4_get_mgr(void)
400{
401 return (void *)&nss_top_main.nss[nss_top_main.ipv4_handler_id];
402}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700403EXPORT_SYMBOL(nss_ipv4_get_mgr);
Thomas Wu68250352014-04-02 18:59:40 -0700404
405/*
406 * nss_ipv4_register_handler()
407 * Register our handler to receive messages for this interface
408 */
Sol Kavy2783c072014-04-05 12:53:13 -0700409void nss_ipv4_register_handler(void)
Thomas Wu68250352014-04-02 18:59:40 -0700410{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700411 struct nss_ctx_instance *nss_ctx = nss_ipv4_get_mgr();
412
413 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 -0700414 nss_warning("IPv4 handler failed to register");
415 }
416}
417
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700418/*
419 * nss_ipv4_conn_cfg_process_callback()
420 * Call back function for the ipv4 connection configure process
421 */
422static void nss_ipv4_conn_cfg_process_callback(void *app_data, struct nss_ipv4_msg *nim)
423{
424 struct nss_ipv4_rule_conn_cfg_msg *nirccm = &nim->msg.rule_conn_cfg;
425 struct nss_ctx_instance *nss_ctx __maybe_unused = nss_ipv4_get_mgr();
426
427 if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
428 nss_warning("%p: IPv4 connection configuration failed with error: %d\n", nss_ctx, nim->cm.error);
429 nss_core_update_max_ipv4_conn(NSS_DEFAULT_NUM_CONN);
430 nss_ipv4_free_conn_tables();
431 return;
432 }
433
434 nss_ipv4_conn_cfg = ntohl(nirccm->num_conn);
435 nss_warning("%p: IPv4 connection configuration success: %d\n", nss_ctx, nim->cm.error);
436}
437
Vijay Dewangan9db18752014-09-15 16:25:01 -0700438/*
Tushar Mathura3c18932015-08-24 20:27:21 +0530439 * nss_ipv4_conn_cfg_process()
440 * Process request to configure number of ipv4 connections
441 */
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700442static int nss_ipv4_conn_cfg_process(struct nss_ctx_instance *nss_ctx, int conn)
Tushar Mathura3c18932015-08-24 20:27:21 +0530443{
444 struct nss_ipv4_msg nim;
445 struct nss_ipv4_rule_conn_cfg_msg *nirccm;
446 nss_tx_status_t nss_tx_status;
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700447
448 if ((!nss_ipv4_ct_info.ce_table_size) || (!nss_ipv4_ct_info.cme_table_size)) {
449 nss_warning("%p: connection entry or connection match entry table size not available\n",
450 nss_ctx);
451 return -EINVAL;
452 }
453
454 nss_info("%p: IPv4 supported connections: %d\n", nss_ctx, conn);
455
456 nss_ipv4_ct_info.ce_mem = __get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
457 get_order(nss_ipv4_ct_info.ce_table_size));
458 if (!nss_ipv4_ct_info.ce_mem) {
459 nss_warning("%p: Memory allocation failed for IPv4 Connections: %d\n",
460 nss_ctx,
461 conn);
462 goto fail;
463 }
464
465 nss_ipv4_ct_info.cme_mem = __get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
466 get_order(nss_ipv4_ct_info.cme_table_size));
467 if (!nss_ipv4_ct_info.ce_mem) {
468 nss_warning("%p: Memory allocation failed for IPv4 Connections: %d\n",
469 nss_ctx,
470 conn);
471 goto fail;
472 }
473
474 memset(&nim, 0, sizeof(struct nss_ipv4_msg));
475 nss_ipv4_msg_init(&nim, NSS_IPV4_RX_INTERFACE, NSS_IPV4_TX_CONN_CFG_RULE_MSG,
476 sizeof(struct nss_ipv4_rule_conn_cfg_msg), nss_ipv4_conn_cfg_process_callback, NULL);
477
478 nirccm = &nim.msg.rule_conn_cfg;
479 nirccm->num_conn = htonl(conn);
Suruchi Agarwal8c9e7162017-08-17 15:58:40 -0700480 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);
481 if (unlikely(dma_mapping_error(nss_ctx->dev, nirccm->ce_mem))) {
482 nss_warning("%p: DMA mapping failed for virtual address = %p", nss_ctx, (void *)nss_ipv4_ct_info.ce_mem);
483 goto fail;
484 }
485
486 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);
487 if (unlikely(dma_mapping_error(nss_ctx->dev, nirccm->cme_mem))) {
488 nss_warning("%p: DMA mapping failed for virtual address = %p", nss_ctx, (void *)nss_ipv4_ct_info.cme_mem);
489 goto fail;
490 }
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700491
492 nss_tx_status = nss_ipv4_tx(nss_ctx, &nim);
493 if (nss_tx_status != NSS_TX_SUCCESS) {
494 nss_warning("%p: nss_tx error setting IPv4 Connections: %d\n",
495 nss_ctx,
496 conn);
497 goto fail;
498 }
499
500 return 0;
501
502fail:
503 nss_ipv4_free_conn_tables();
504 return -EINVAL;;
505}
506
507/*
508 * nss_ipv4_update_conn_count_callback()
509 * Callback function for the ipv4 get connection info message.
510 */
511static void nss_ipv4_update_conn_count_callback(void *app_data, struct nss_ipv4_msg *nim)
512{
513 struct nss_ipv4_rule_conn_get_table_size_msg *nircgts = &nim->msg.size;
514 struct nss_ctx_instance *nss_ctx = nss_ipv4_get_mgr();
515
516 if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
517 nss_warning("%p: IPv4 fetch connection info failed with error: %d\n", nss_ctx, nim->cm.error);
518 nss_core_update_max_ipv4_conn(NSS_DEFAULT_NUM_CONN);
519 return;
520 }
521
522 nss_info("IPv4 get connection info success\n");
523
524 nss_ipv4_ct_info.ce_table_size = ntohl(nircgts->ce_table_size);
525 nss_ipv4_ct_info.cme_table_size = ntohl(nircgts->cme_table_size);
526
527 if (nss_ipv4_conn_cfg_process(nss_ctx, ntohl(nircgts->num_conn)) != 0) {
528 nss_warning("%p: IPv4 connection entry or connection match entry table size\
529 not available\n", nss_ctx);
530 }
531
532 return;
533}
534
535/*
536 * nss_ipv4_update_conn_count()
537 * Sets the maximum number of IPv4 connections.
538 *
539 * It first gets the connection tables size information from NSS FW
540 * and then configures the connections in NSS FW.
541 */
542int nss_ipv4_update_conn_count(int ipv4_num_conn)
543{
544 struct nss_ctx_instance *nss_ctx = nss_ipv4_get_mgr();
545 struct nss_ipv4_msg nim;
546 struct nss_ipv4_rule_conn_get_table_size_msg *nircgts;
547 nss_tx_status_t nss_tx_status;
Tushar Mathura3c18932015-08-24 20:27:21 +0530548 uint32_t sum_of_conn;
549
550 /*
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700551 * By default, NSS FW is configured with default number of connections.
552 */
553 if (ipv4_num_conn == NSS_DEFAULT_NUM_CONN) {
554 nss_info("%p: Default number of connections (%d) already configured\n", nss_ctx, ipv4_num_conn);
555 return 0;
556 }
557
558 /*
Tushar Mathura3c18932015-08-24 20:27:21 +0530559 * The input should be multiple of 1024.
560 * Input for ipv4 and ipv6 sum together should not exceed 8k
561 * Min. value should be at least 256 connections. This is the
562 * minimum connections we will support for each of them.
563 */
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700564 sum_of_conn = ipv4_num_conn + nss_ipv6_conn_cfg;
565 if ((ipv4_num_conn & NSS_NUM_CONN_QUANTA_MASK) ||
Tushar Mathura3c18932015-08-24 20:27:21 +0530566 (sum_of_conn > NSS_MAX_TOTAL_NUM_CONN_IPV4_IPV6) ||
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700567 (ipv4_num_conn < NSS_MIN_NUM_CONN)) {
Tushar Mathura3c18932015-08-24 20:27:21 +0530568 nss_warning("%p: input supported connections (%d) does not adhere\
569 specifications\n1) not multiple of 1024,\n2) is less than \
570 min val: %d, OR\n IPv4/6 total exceeds %d\n",
571 nss_ctx,
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700572 ipv4_num_conn,
Tushar Mathura3c18932015-08-24 20:27:21 +0530573 NSS_MIN_NUM_CONN,
574 NSS_MAX_TOTAL_NUM_CONN_IPV4_IPV6);
575 return -EINVAL;
576 }
577
Tushar Mathura3c18932015-08-24 20:27:21 +0530578 memset(&nim, 0, sizeof(struct nss_ipv4_msg));
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700579 nss_ipv4_msg_init(&nim, NSS_IPV4_RX_INTERFACE, NSS_IPV4_TX_CONN_TABLE_SIZE_MSG,
580 sizeof(struct nss_ipv4_rule_conn_get_table_size_msg), nss_ipv4_update_conn_count_callback, NULL);
Tushar Mathura3c18932015-08-24 20:27:21 +0530581
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700582 nircgts = &nim.msg.size;
583 nircgts->num_conn = htonl(ipv4_num_conn);
Tushar Mathura3c18932015-08-24 20:27:21 +0530584 nss_tx_status = nss_ipv4_tx(nss_ctx, &nim);
585
586 if (nss_tx_status != NSS_TX_SUCCESS) {
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700587 nss_warning("%p: Send fetch connection info message failed\n", nss_ctx);
588 return -EINVAL;
Tushar Mathura3c18932015-08-24 20:27:21 +0530589 }
590
591 return 0;
592}
593
Stephen Wang0a2756a2016-08-04 15:52:47 -0700594/*
595 * nss_ipv4_accel_mode_cfg_callback()
596 * call back function for the ipv4 acceleration mode configurate handler
597 */
598static void nss_ipv4_accel_mode_cfg_callback(void *app_data, struct nss_ipv4_msg *nim)
599{
600 if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
601 nss_warning("IPv4 acceleration mode configuration failed with error: %d\n", nim->cm.error);
602 i4_accel_mode_cfgp.response = NSS_FAILURE;
603 complete(&i4_accel_mode_cfgp.complete);
604 return;
605 }
606
607 nss_info("IPv4 acceleration mode configuration success\n");
608 i4_accel_mode_cfgp.response = NSS_SUCCESS;
609 complete(&i4_accel_mode_cfgp.complete);
610}
611
612/*
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700613 * nss_ipv4_free_conn_tables()
614 * Frees memory allocated for connection tables
615 */
616void nss_ipv4_free_conn_tables(void)
617{
618 if (nss_ipv4_ct_info.ce_mem) {
619 free_pages(nss_ipv4_ct_info.ce_mem, get_order(nss_ipv4_ct_info.ce_table_size));
620 }
621
622 if (nss_ipv4_ct_info.cme_mem) {
623 free_pages(nss_ipv4_ct_info.cme_mem, get_order(nss_ipv4_ct_info.cme_table_size));
624 }
625
626 memset(&nss_ipv4_ct_info, 0, sizeof(struct nss_ipv4_conn_table_info));
627 return;
628}
629
630/*
Stephen Wang0a2756a2016-08-04 15:52:47 -0700631 * nss_ipv4_accel_mode_cfg_handler()
632 * Configure acceleration mode for IPv4
633 */
634static int nss_ipv4_accel_mode_cfg_handler(struct ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
635{
636 struct nss_top_instance *nss_top = &nss_top_main;
637 struct nss_ctx_instance *nss_ctx = &nss_top->nss[0];
638 struct nss_ipv4_msg nim;
639 struct nss_ipv4_accel_mode_cfg_msg *nipcm;
640 nss_tx_status_t nss_tx_status;
641 int ret = NSS_FAILURE;
642
643 /*
644 * Acquiring semaphore
645 */
646 down(&i4_accel_mode_cfgp.sem);
647
648 /*
649 * Take snap shot of current value
650 */
651 i4_accel_mode_cfgp.current_value = nss_ipv4_accel_mode_cfg;
652
653 /*
654 * Write the variable with user input
655 */
656 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
657 if (ret || (!write)) {
658 up(&i4_accel_mode_cfgp.sem);
659 return ret;
660 }
661
662 memset(&nim, 0, sizeof(struct nss_ipv4_msg));
663 nss_ipv4_msg_init(&nim, NSS_IPV4_RX_INTERFACE, NSS_IPV4_TX_ACCEL_MODE_CFG_MSG,
664 sizeof(struct nss_ipv4_accel_mode_cfg_msg), nss_ipv4_accel_mode_cfg_callback, NULL);
665
666 nipcm = &nim.msg.accel_mode_cfg;
667 nipcm->mode = htonl(nss_ipv4_accel_mode_cfg);
668 nss_tx_status = nss_ipv4_tx(nss_ctx, &nim);
669
670 if (nss_tx_status != NSS_TX_SUCCESS) {
671 nss_warning("%p: Send acceleration mode message failed\n", nss_ctx);
672 goto fail;
673 }
674
675 /*
676 * Blocking call, wait till we get ACK for this msg.
677 */
678 ret = wait_for_completion_timeout(&i4_accel_mode_cfgp.complete, msecs_to_jiffies(NSS_IPV4_TX_MSG_TIMEOUT));
679 if (ret == 0) {
680 nss_warning("%p: Waiting for ack timed out\n", nss_ctx);
681 goto fail;
682 }
683
684 if (NSS_FAILURE == i4_accel_mode_cfgp.response) {
685 nss_warning("%p: accel mode configure failed\n", nss_ctx);
686 goto fail;
687 }
688
689 up(&i4_accel_mode_cfgp.sem);
690 return 0;
691
692fail:
693 nss_ipv4_accel_mode_cfg = i4_accel_mode_cfgp.current_value;
694 up(&i4_accel_mode_cfgp.sem);
695 return -EIO;
696}
697
Stephen Wang52e6d342016-03-29 15:02:33 -0700698static struct ctl_table nss_ipv4_table[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700699 {
Stephen Wang0a2756a2016-08-04 15:52:47 -0700700 .procname = "ipv4_accel_mode",
701 .data = &nss_ipv4_accel_mode_cfg,
702 .maxlen = sizeof(int),
703 .mode = 0644,
704 .proc_handler = &nss_ipv4_accel_mode_cfg_handler,
Vijay Dewangan9db18752014-09-15 16:25:01 -0700705 },
706 { }
707};
708
Stephen Wang52e6d342016-03-29 15:02:33 -0700709static struct ctl_table nss_ipv4_dir[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700710 {
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700711 .procname = "ipv4cfg",
712 .mode = 0555,
713 .child = nss_ipv4_table,
Vijay Dewangan9db18752014-09-15 16:25:01 -0700714 },
715 { }
716};
717
718
Stephen Wang52e6d342016-03-29 15:02:33 -0700719static struct ctl_table nss_ipv4_root_dir[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700720 {
721 .procname = "nss",
722 .mode = 0555,
723 .child = nss_ipv4_dir,
724 },
725 { }
726};
727
Stephen Wang52e6d342016-03-29 15:02:33 -0700728static struct ctl_table nss_ipv4_root[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700729 {
730 .procname = "dev",
731 .mode = 0555,
732 .child = nss_ipv4_root_dir,
733 },
734 { }
735};
736
737static struct ctl_table_header *nss_ipv4_header;
738
739/*
740 * nss_ipv4_register_sysctl()
741 * Register sysctl specific to ipv4
742 */
743void nss_ipv4_register_sysctl(void)
744{
Stephen Wang0a2756a2016-08-04 15:52:47 -0700745 sema_init(&i4_accel_mode_cfgp.sem, 1);
746 init_completion(&i4_accel_mode_cfgp.complete);
Stephen Wang49b474b2016-03-25 10:40:30 -0700747
Vijay Dewangan9db18752014-09-15 16:25:01 -0700748 /*
749 * Register sysctl table.
750 */
751 nss_ipv4_header = register_sysctl_table(nss_ipv4_root);
752}
753
754/*
755 * nss_ipv4_unregister_sysctl()
756 * Unregister sysctl specific to ipv4
757 */
758void nss_ipv4_unregister_sysctl(void)
759{
760 /*
761 * Unregister sysctl table.
762 */
763 if (nss_ipv4_header) {
764 unregister_sysctl_table(nss_ipv4_header);
765 }
766}
767
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700768/*
769 * nss_ipv4_msg_init()
770 * Initialize IPv4 message.
771 */
772void 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 -0800773 nss_ipv4_msg_callback_t cb, void *app_data)
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700774{
775 nss_cmn_msg_init(&nim->cm, if_num, type, len, (void *)cb, app_data);
776}
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700777EXPORT_SYMBOL(nss_ipv4_msg_init);