blob: f9470f9567f4b045c6a8f0bcbf2289237186eb1c [file] [log] [blame]
Sol Kavy879eb8b2014-04-07 19:11:31 -07001/*
2 **************************************************************************
Stephen Wangaed46332016-12-12 17:29:03 -08003 * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
Sol Kavy879eb8b2014-04-07 19:11:31 -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/*
18 * nss_ipv6.c
19 * NSS IPv6 APIs
20 */
Sol Kavy879eb8b2014-04-07 19:11:31 -070021#include "nss_tx_rx_common.h"
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -070022#include "nss_dscp_map.h"
Yu Huang8c107082017-07-24 14:58:26 -070023#include "nss_ipv6_stats.h"
Sol Kavy879eb8b2014-04-07 19:11:31 -070024
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -070025#define NSS_IPV6_TX_MSG_TIMEOUT 1000 /* 1 sec timeout for IPv6 messages */
Stephen Wang0a2756a2016-08-04 15:52:47 -070026
27/*
28 * Private data structure for ipv6 configure messages
29 */
30struct nss_ipv6_cfg_pvt {
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -070031 struct semaphore sem; /* Semaphore structure */
32 struct completion complete; /* Completion structure */
33 int response; /* Response from FW */
34 void *cb; /* Original cb for sync msgs */
35 void *app_data; /* Original app_data for sync msgs */
36} nss_ipv6_pvt;
Stephen Wang0a2756a2016-08-04 15:52:47 -070037
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -070038/*
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -070039 * Private data structure for ipv6 connection information.
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -070040 */
41struct nss_ipv6_conn_table_info {
42 uint32_t ce_table_size; /* Size of connection entry table in NSS FW */
43 uint32_t cme_table_size; /* Size of connection match entry table in NSS FW */
44 unsigned long ce_mem; /* Start address for connection entry table */
45 unsigned long cme_mem; /* Start address for connection match entry table */
46} nss_ipv6_ct_info;
Stephen Wang0a2756a2016-08-04 15:52:47 -070047
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -070048int nss_ipv6_conn_cfg = NSS_DEFAULT_NUM_CONN;
Stephen Wangf6a9acb2017-08-23 10:35:39 -070049int nss_ipv6_accel_mode_cfg __read_mostly = 1;
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -070050
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -070051static struct nss_dscp_map_entry mapping[NSS_DSCP_MAP_ARRAY_SIZE];
Vijay Dewangan9db18752014-09-15 16:25:01 -070052
Sol Kavy879eb8b2014-04-07 19:11:31 -070053/*
Suruchi Agarwal611ecd02016-08-04 15:54:35 -070054 * Callback for conn_sync_many request message.
55 */
56nss_ipv6_msg_callback_t nss_ipv6_conn_sync_many_msg_cb = NULL;
57
58/*
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -070059 * nss_ipv6_dscp_map_usage()
60 * Help function shows the usage of the command.
Gareth Williams958aa822015-02-04 19:36:39 +000061 */
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -070062static inline void nss_ipv6_dscp_map_usage(void)
Gareth Williams958aa822015-02-04 19:36:39 +000063{
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -070064 nss_info_always("\nUsage:\n");
65 nss_info_always("echo <dscp> <action> <prio> > /proc/sys/dev/nss/ipv6cfg/ipv6_dscp_map\n\n");
66 nss_info_always("dscp[0-63] action[0-%u] prio[0-%u]:\n\n",
67 NSS_IPV6_DSCP_MAP_ACTION_MAX - 1,
68 NSS_DSCP_MAP_PRIORITY_MAX - 1);
Gareth Williams958aa822015-02-04 19:36:39 +000069}
Guojun Jin9a1cb282017-07-13 17:32:07 -070070
71/*
Sol Kavy879eb8b2014-04-07 19:11:31 -070072 * nss_ipv6_rx_msg_handler()
73 * Handle NSS -> HLOS messages for IPv6 bridge/route
74 */
75static void nss_ipv6_rx_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
76{
77 struct nss_ipv6_msg *nim = (struct nss_ipv6_msg *)ncm;
78 nss_ipv6_msg_callback_t cb;
79
80 BUG_ON(ncm->interface != NSS_IPV6_RX_INTERFACE);
81
82 /*
83 * Is this a valid request/response packet?
84 */
Murat Sezgin5c8c7362014-09-02 17:58:21 -070085 if (ncm->type >= NSS_IPV6_MAX_MSG_TYPES) {
Sol Kavy879eb8b2014-04-07 19:11:31 -070086 nss_warning("%p: received invalid message %d for IPv6 interface", nss_ctx, nim->cm.type);
87 return;
88 }
89
Suruchi Agarwalef8a8702016-01-08 12:40:08 -080090 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ipv6_msg)) {
91 nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Sol Kavy879eb8b2014-04-07 19:11:31 -070092 return;
93 }
94
95 /*
Suruchi Agarwale3940e72016-07-06 15:56:51 -070096 * Trace messages.
Sol Kavy879eb8b2014-04-07 19:11:31 -070097 */
Suruchi Agarwale3940e72016-07-06 15:56:51 -070098 nss_ipv6_log_rx_msg(nim);
Sol Kavy879eb8b2014-04-07 19:11:31 -070099
100 /*
Stephen Wang0a2756a2016-08-04 15:52:47 -0700101 * Handle deprecated messages. Eventually these messages should be removed.
Sol Kavy879eb8b2014-04-07 19:11:31 -0700102 */
103 switch (nim->cm.type) {
Murat Sezgin0c0561d2014-04-09 18:55:58 -0700104 case NSS_IPV6_RX_NODE_STATS_SYNC_MSG:
105 /*
106 * Update driver statistics on node sync.
107 */
Yu Huang8c107082017-07-24 14:58:26 -0700108 nss_ipv6_stats_node_sync(nss_ctx, &nim->msg.node_stats);
Murat Sezgin0c0561d2014-04-09 18:55:58 -0700109 break;
110
Sol Kavy879eb8b2014-04-07 19:11:31 -0700111 case NSS_IPV6_RX_CONN_STATS_SYNC_MSG:
112 /*
113 * Update driver statistics on connection sync.
114 */
Yu Huang8c107082017-07-24 14:58:26 -0700115 nss_ipv6_stats_conn_sync(nss_ctx, &nim->msg.conn_stats);
Sakthi Vignesh Radhakrishnan515f8c22014-06-21 15:04:19 -0700116 break;
Stephen Wang709f9b52015-07-07 14:48:35 -0700117
118 case NSS_IPV6_TX_CONN_STATS_SYNC_MANY_MSG:
119 /*
120 * Update driver statistics on connection sync many.
121 */
Yu Huang8c107082017-07-24 14:58:26 -0700122 nss_ipv6_stats_conn_sync_many(nss_ctx, &nim->msg.conn_stats_many);
Stephen Wangaed46332016-12-12 17:29:03 -0800123 ncm->cb = (nss_ptr_t)nss_ipv6_conn_sync_many_msg_cb;
Stephen Wang709f9b52015-07-07 14:48:35 -0700124 break;
Sol Kavy879eb8b2014-04-07 19:11:31 -0700125 }
Abhishek Rastogi55f39452014-05-08 19:23:29 +0530126
Sol Kavy879eb8b2014-04-07 19:11:31 -0700127 /*
128 * Update the callback and app_data for NOTIFY messages, IPv6 sends all notify messages
129 * to the same callback/app_data.
130 */
131 if (nim->cm.response == NSS_CMM_RESPONSE_NOTIFY) {
Stephen Wangaed46332016-12-12 17:29:03 -0800132 ncm->cb = (nss_ptr_t)nss_ctx->nss_top->ipv6_callback;
133 ncm->app_data = (nss_ptr_t)nss_ctx->nss_top->ipv6_ctx;
Sol Kavy879eb8b2014-04-07 19:11:31 -0700134 }
135
136 /*
137 * Do we have a callback?
138 */
139 if (!ncm->cb) {
140 return;
141 }
142
143 /*
144 * Callback
145 */
146 cb = (nss_ipv6_msg_callback_t)ncm->cb;
147 cb((void *)ncm->app_data, nim);
148}
149
150/*
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700151 * nss_ipv6_tx_sync_callback()
152 * Callback to handle the completion of synchronous tx messages.
153 */
154static void nss_ipv6_tx_sync_callback(void *app_data, struct nss_ipv6_msg *nim)
155{
156 nss_ipv6_msg_callback_t callback = (nss_ipv6_msg_callback_t)nss_ipv6_pvt.cb;
157 void *data = nss_ipv6_pvt.app_data;
158
159 nss_ipv6_pvt.cb = NULL;
160 nss_ipv6_pvt.app_data = NULL;
161
162 if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
163 nss_warning("ipv6 error response %d\n", nim->cm.response);
164 nss_ipv6_pvt.response = NSS_TX_FAILURE;
165 } else {
166 nss_ipv6_pvt.response = NSS_TX_SUCCESS;
167 }
168
169 if (callback) {
170 callback(data, nim);
171 }
172
173 complete(&nss_ipv6_pvt.complete);
174}
175
176/*
177 * nss_ipv6_dscp_action_get()
178 * Gets the action mapped to dscp.
179 */
180enum nss_ipv6_dscp_map_actions nss_ipv6_dscp_action_get(uint8_t dscp)
181{
Sakthi Vignesh Radhakrishnan1f7a66b2017-10-18 09:33:22 -0700182 if (dscp >= NSS_DSCP_MAP_ARRAY_SIZE) {
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700183 nss_warning("dscp:%u invalid\n", dscp);
184 return NSS_IPV6_DSCP_MAP_ACTION_MAX;
185 }
186
187 return mapping[dscp].action;
188}
189EXPORT_SYMBOL(nss_ipv6_dscp_action_get);
190
191/*
192 * nss_ipv6_max_conn_count()
193 * Return the maximum number of IPv6 connections that the NSS acceleration engine supports.
194 */
195int nss_ipv6_max_conn_count(void)
196{
197 return nss_ipv6_conn_cfg;
198}
199EXPORT_SYMBOL(nss_ipv6_max_conn_count);
200
201/*
202 * nss_ipv6_conn_inquiry()
203 * Inquiry if a connection has been established in NSS FW
204 */
205nss_tx_status_t nss_ipv6_conn_inquiry(struct nss_ipv6_5tuple *ipv6_5t_p,
206 nss_ipv6_msg_callback_t cb)
207{
208 nss_tx_status_t nss_tx_status;
209 struct nss_ipv6_msg nim;
210 struct nss_ctx_instance *nss_ctx = &nss_top_main.nss[0];
211
212 /*
213 * Initialize inquiry message structure.
214 * This is async message and the result will be returned
215 * to the caller by the msg_callback passed in.
216 */
217 memset(&nim, 0, sizeof(nim));
218 nss_ipv6_msg_init(&nim, NSS_IPV6_RX_INTERFACE,
219 NSS_IPV6_TX_CONN_CFG_INQUIRY_MSG,
220 sizeof(struct nss_ipv6_inquiry_msg),
221 cb, NULL);
222 nim.msg.inquiry.rr.tuple = *ipv6_5t_p;
223 nss_tx_status = nss_ipv6_tx(nss_ctx, &nim);
224 if (nss_tx_status != NSS_TX_SUCCESS) {
225 nss_warning("%p: Send inquiry message failed\n", ipv6_5t_p);
226 }
227
228 return nss_tx_status;
229}
230EXPORT_SYMBOL(nss_ipv6_conn_inquiry);
231
232/*
Stephen Wang709f9b52015-07-07 14:48:35 -0700233 * nss_ipv6_tx_with_size()
234 * Transmit an ipv6 message to the FW with a specified size.
Sol Kavy879eb8b2014-04-07 19:11:31 -0700235 */
Stephen Wang709f9b52015-07-07 14:48:35 -0700236nss_tx_status_t nss_ipv6_tx_with_size(struct nss_ctx_instance *nss_ctx, struct nss_ipv6_msg *nim, uint32_t size)
Sol Kavy879eb8b2014-04-07 19:11:31 -0700237{
238 struct nss_ipv6_msg *nim2;
239 struct nss_cmn_msg *ncm = &nim->cm;
240 struct sk_buff *nbuf;
241 int32_t status;
242
243 NSS_VERIFY_CTX_MAGIC(nss_ctx);
244 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
245 nss_warning("%p: ipv6 msg dropped as core not ready", nss_ctx);
246 return NSS_TX_FAILURE_NOT_READY;
247 }
248
249 /*
250 * Sanity check the message
251 */
252 if (ncm->interface != NSS_IPV6_RX_INTERFACE) {
253 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
254 return NSS_TX_FAILURE;
255 }
256
Murat Sezgin5c8c7362014-09-02 17:58:21 -0700257 if (ncm->type >= NSS_IPV6_MAX_MSG_TYPES) {
Sol Kavy879eb8b2014-04-07 19:11:31 -0700258 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
259 return NSS_TX_FAILURE;
260 }
261
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800262 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ipv6_msg)) {
263 nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Sol Kavy879eb8b2014-04-07 19:11:31 -0700264 return NSS_TX_FAILURE;
265 }
266
Stephen Wang709f9b52015-07-07 14:48:35 -0700267 if(size > PAGE_SIZE) {
268 nss_warning("%p: tx request size too large: %u", nss_ctx, size);
269 return NSS_TX_FAILURE;
270 }
271
272 nbuf = dev_alloc_skb(size);
Sol Kavy879eb8b2014-04-07 19:11:31 -0700273 if (unlikely(!nbuf)) {
Sundarajan Srinivasan62fee7e2015-01-22 11:13:10 -0800274 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]);
Sol Kavy879eb8b2014-04-07 19:11:31 -0700275 nss_warning("%p: msg dropped as command allocation failed", nss_ctx);
276 return NSS_TX_FAILURE;
277 }
278
279 /*
280 * Copy the message to our skb.
281 */
Suruchi Agarwalea2e5232016-09-14 10:32:08 -0700282 nim2 = (struct nss_ipv6_msg *)skb_put(nbuf, size);
Sol Kavy879eb8b2014-04-07 19:11:31 -0700283 memcpy(nim2, nim, sizeof(struct nss_ipv6_msg));
284
Suruchi Agarwale3940e72016-07-06 15:56:51 -0700285 /*
286 * Trace messages.
287 */
288 nss_ipv6_log_tx_msg(nim);
289
Sol Kavy879eb8b2014-04-07 19:11:31 -0700290 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
291 if (status != NSS_CORE_STATUS_SUCCESS) {
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530292 dev_kfree_skb_any(nbuf);
Sol Kavy879eb8b2014-04-07 19:11:31 -0700293 nss_warning("%p: Unable to enqueue 'Destroy IPv6' rule\n", nss_ctx);
294 return NSS_TX_FAILURE;
295 }
296
Stephen Wang90c67de2016-04-26 15:15:59 -0700297 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
Sol Kavy879eb8b2014-04-07 19:11:31 -0700298
299 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]);
300 return NSS_TX_SUCCESS;
301}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700302EXPORT_SYMBOL(nss_ipv6_tx_with_size);
Sol Kavy879eb8b2014-04-07 19:11:31 -0700303
304/*
Stephen Wang709f9b52015-07-07 14:48:35 -0700305 * nss_ipv6_tx()
Suruchi Agarwal611ecd02016-08-04 15:54:35 -0700306 * Transmit an ipv6 message to the FW.
Stephen Wang709f9b52015-07-07 14:48:35 -0700307 */
308nss_tx_status_t nss_ipv6_tx(struct nss_ctx_instance *nss_ctx, struct nss_ipv6_msg *nim)
309{
310 return nss_ipv6_tx_with_size(nss_ctx, nim, NSS_NBUF_PAYLOAD_SIZE);
311}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700312EXPORT_SYMBOL(nss_ipv6_tx);
Stephen Wang709f9b52015-07-07 14:48:35 -0700313
314/*
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700315 * nss_ipv6_tx_sync()
316 * Transmit a synchronous ipv6 message to the FW.
317 */
318nss_tx_status_t nss_ipv6_tx_sync(struct nss_ctx_instance *nss_ctx, struct nss_ipv6_msg *nim)
319{
320 nss_tx_status_t status;
321 int ret = 0;
322
323 down(&nss_ipv6_pvt.sem);
324 nss_ipv6_pvt.cb = (void *)nim->cm.cb;
325 nss_ipv6_pvt.app_data = (void *)nim->cm.app_data;
326
327 nim->cm.cb = (nss_ptr_t)nss_ipv6_tx_sync_callback;
328 nim->cm.app_data = (nss_ptr_t)NULL;
329
330 status = nss_ipv6_tx(nss_ctx, nim);
331 if (status != NSS_TX_SUCCESS) {
332 nss_warning("%p: nss ipv6 msg tx failed\n", nss_ctx);
333 up(&nss_ipv6_pvt.sem);
334 return status;
335 }
336
337 ret = wait_for_completion_timeout(&nss_ipv6_pvt.complete, msecs_to_jiffies(NSS_IPV6_TX_MSG_TIMEOUT));
338 if (!ret) {
339 nss_warning("%p: IPv6 tx sync failed due to timeout\n", nss_ctx);
340 nss_ipv6_pvt.response = NSS_TX_FAILURE;
341 }
342
343 status = nss_ipv6_pvt.response;
344 up(&nss_ipv6_pvt.sem);
345 return status;
346}
347EXPORT_SYMBOL(nss_ipv6_tx_sync);
348
349/*
Sol Kavy879eb8b2014-04-07 19:11:31 -0700350 **********************************
351 Register/Unregister/Miscellaneous APIs
352 **********************************
353 */
354
355/*
356 * nss_ipv6_notify_register()
357 * Register to received IPv6 events.
358 *
359 * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv6 on any core?
360 */
361struct nss_ctx_instance *nss_ipv6_notify_register(nss_ipv6_msg_callback_t cb, void *app_data)
362{
363 /*
364 * TODO: We need to have a new array in support of the new API
365 * TODO: If we use a per-context array, we would move the array into nss_ctx based.
366 */
367 nss_top_main.ipv6_callback = cb;
368 nss_top_main.ipv6_ctx = app_data;
369 return &nss_top_main.nss[nss_top_main.ipv6_handler_id];
370}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700371EXPORT_SYMBOL(nss_ipv6_notify_register);
Sol Kavy879eb8b2014-04-07 19:11:31 -0700372
373/*
374 * nss_ipv6_notify_unregister()
375 * Unregister to received IPv6 events.
376 *
377 * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv6 on any core?
378 */
379void nss_ipv6_notify_unregister(void)
380{
381 nss_top_main.ipv6_callback = NULL;
382}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700383EXPORT_SYMBOL(nss_ipv6_notify_unregister);
Sol Kavy879eb8b2014-04-07 19:11:31 -0700384
385/*
Suruchi Agarwal611ecd02016-08-04 15:54:35 -0700386 * nss_ipv6_conn_sync_many_notify_register()
387 * Register to receive IPv6 conn_sync_many message response.
388 */
389void nss_ipv6_conn_sync_many_notify_register(nss_ipv6_msg_callback_t cb)
390{
391 nss_ipv6_conn_sync_many_msg_cb = cb;
392}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700393EXPORT_SYMBOL(nss_ipv6_conn_sync_many_notify_register);
Suruchi Agarwal611ecd02016-08-04 15:54:35 -0700394
395/*
396 * nss_ipv6_conn_sync_many_notify_unregister()
397 * Unregister to receive IPv6 conn_sync_many message response.
398 */
399void nss_ipv6_conn_sync_many_notify_unregister(void)
400{
401 nss_ipv6_conn_sync_many_msg_cb = NULL;
402}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700403EXPORT_SYMBOL(nss_ipv6_conn_sync_many_notify_unregister);
Suruchi Agarwal611ecd02016-08-04 15:54:35 -0700404
405/*
Sol Kavy879eb8b2014-04-07 19:11:31 -0700406 * nss_ipv6_get_mgr()
407 *
408 * TODO: This only suppports a single ipv6, do we ever want to support more?
409 */
410struct nss_ctx_instance *nss_ipv6_get_mgr(void)
411{
412 return (void *)&nss_top_main.nss[nss_top_main.ipv6_handler_id];
413}
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700414EXPORT_SYMBOL(nss_ipv6_get_mgr);
Sol Kavy879eb8b2014-04-07 19:11:31 -0700415
416/*
417 * nss_ipv6_register_handler()
418 * Register our handler to receive messages for this interface
419 */
420void nss_ipv6_register_handler()
421{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700422 struct nss_ctx_instance *nss_ctx = nss_ipv6_get_mgr();
423
424 if (nss_core_register_handler(nss_ctx, NSS_IPV6_RX_INTERFACE, nss_ipv6_rx_msg_handler, NULL) != NSS_CORE_STATUS_SUCCESS) {
Sol Kavy879eb8b2014-04-07 19:11:31 -0700425 nss_warning("IPv6 handler failed to register");
426 }
Yu Huang8c107082017-07-24 14:58:26 -0700427
428 nss_ipv6_stats_dentry_create();
Sol Kavy879eb8b2014-04-07 19:11:31 -0700429}
430
Vijay Dewangan9db18752014-09-15 16:25:01 -0700431/*
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700432 * nss_ipv6_conn_cfg_process_callback()
433 * Call back function for the ipv6 connection configuration process.
434 */
435static void nss_ipv6_conn_cfg_process_callback(void *app_data, struct nss_ipv6_msg *nim)
436{
437 struct nss_ipv6_rule_conn_cfg_msg *nirccm = &nim->msg.rule_conn_cfg;
438 struct nss_ctx_instance *nss_ctx __maybe_unused = nss_ipv6_get_mgr();
439
440 if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
441 nss_warning("%p: IPv6 connection configuration failed with error: %d\n", nss_ctx, nim->cm.error);
Stephen Wang01433692017-10-30 14:45:11 -0700442 nss_core_update_max_ipv6_conn(NSS_FW_DEFAULT_NUM_CONN);
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700443 nss_ipv6_free_conn_tables();
444 return;
445 }
446
447 nss_ipv6_conn_cfg = ntohl(nirccm->num_conn);
448
449 nss_warning("%p: IPv6 connection configuration success: %d\n", nss_ctx, nim->cm.error);
450}
451
452/*
Tushar Mathura3c18932015-08-24 20:27:21 +0530453 * nss_ipv6_conn_cfg_process()
454 * Process request to configure number of ipv6 connections
455 */
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700456static int nss_ipv6_conn_cfg_process(struct nss_ctx_instance *nss_ctx, int conn)
Tushar Mathura3c18932015-08-24 20:27:21 +0530457{
458 struct nss_ipv6_msg nim;
459 struct nss_ipv6_rule_conn_cfg_msg *nirccm;
460 nss_tx_status_t nss_tx_status;
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700461
462 if ((!nss_ipv6_ct_info.ce_table_size) || (!nss_ipv6_ct_info.cme_table_size)) {
463 nss_warning("%p: connection entry or connection match entry table size not available\n",
464 nss_ctx);
465 return -EINVAL;
466 }
467
468 nss_info("%p: IPv6 supported connections: %d\n", nss_ctx, conn);
469
470 nss_ipv6_ct_info.ce_mem = __get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
471 get_order(nss_ipv6_ct_info.ce_table_size));
472 if (!nss_ipv6_ct_info.ce_mem) {
473 nss_warning("%p: Memory allocation failed for IPv6 Connections: %d\n",
474 nss_ctx,
475 conn);
476 goto fail;
477 }
478 nss_warning("%p: CE Memory allocated for IPv6 Connections: %d\n",
479 nss_ctx,
480 conn);
481
482 nss_ipv6_ct_info.cme_mem = __get_free_pages(GFP_KERNEL | __GFP_NOWARN | __GFP_ZERO,
483 get_order(nss_ipv6_ct_info.cme_table_size));
484 if (!nss_ipv6_ct_info.cme_mem) {
485 nss_warning("%p: Memory allocation failed for IPv6 Connections: %d\n",
486 nss_ctx,
487 conn);
488 goto fail;
489 }
490 nss_warning("%p: CME Memory allocated for IPv6 Connections: %d\n",
491 nss_ctx,
492 conn);
493
494 memset(&nim, 0, sizeof(struct nss_ipv6_msg));
495 nss_ipv6_msg_init(&nim, NSS_IPV6_RX_INTERFACE, NSS_IPV6_TX_CONN_CFG_RULE_MSG,
496 sizeof(struct nss_ipv6_rule_conn_cfg_msg), nss_ipv6_conn_cfg_process_callback, NULL);
497
498 nirccm = &nim.msg.rule_conn_cfg;
499 nirccm->num_conn = htonl(conn);
Suruchi Agarwal8c9e7162017-08-17 15:58:40 -0700500 nirccm->ce_mem = dma_map_single(nss_ctx->dev, (void *)nss_ipv6_ct_info.ce_mem, nss_ipv6_ct_info.ce_table_size, DMA_TO_DEVICE);
501 if (unlikely(dma_mapping_error(nss_ctx->dev, nirccm->ce_mem))) {
502 nss_warning("%p: DMA mapping failed for virtual address = %p", nss_ctx, (void *)nss_ipv6_ct_info.ce_mem);
503 goto fail;
504 }
505
506 nirccm->cme_mem = dma_map_single(nss_ctx->dev, (void *)nss_ipv6_ct_info.cme_mem, nss_ipv6_ct_info.cme_table_size, DMA_TO_DEVICE);
507 if (unlikely(dma_mapping_error(nss_ctx->dev, nirccm->cme_mem))) {
508 nss_warning("%p: DMA mapping failed for virtual address = %p", nss_ctx, (void *)nss_ipv6_ct_info.cme_mem);
509 goto fail;
510 }
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700511
512 nss_tx_status = nss_ipv6_tx(nss_ctx, &nim);
513 if (nss_tx_status != NSS_TX_SUCCESS) {
514 nss_warning("%p: nss_tx error setting IPv6 Connections: %d\n",
515 nss_ctx,
516 conn);
517 goto fail;
518 }
519
520 return 0;
521
522fail:
523 nss_ipv6_free_conn_tables();
524 return -EINVAL;
525}
526
527/*
528 * nss_ipv6_update_conn_count_callback()
529 * Call back function for the ipv6 get connection info message.
530 */
531static void nss_ipv6_update_conn_count_callback(void *app_data, struct nss_ipv6_msg *nim)
532{
533 struct nss_ipv6_rule_conn_get_table_size_msg *nircgts = &nim->msg.size;
534 struct nss_ctx_instance *nss_ctx = nss_ipv6_get_mgr();
535
536 if (nim->cm.response != NSS_CMN_RESPONSE_ACK) {
537 nss_warning("%p: IPv6 fetch connection info failed with error: %d\n", nss_ctx, nim->cm.error);
Stephen Wang01433692017-10-30 14:45:11 -0700538 nss_core_update_max_ipv6_conn(NSS_FW_DEFAULT_NUM_CONN);
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700539 return;
540 }
541
542 nss_info("IPv6 get connection info success\n");
543
544 nss_ipv6_ct_info.ce_table_size = ntohl(nircgts->ce_table_size);
545 nss_ipv6_ct_info.cme_table_size = ntohl(nircgts->cme_table_size);
546
547 if (nss_ipv6_conn_cfg_process(nss_ctx, ntohl(nircgts->num_conn)) != 0) {
548 nss_warning("%p: IPv6 connection entry or connection match entry table size\
549 not available\n", nss_ctx);
550 }
551
552 return;
553}
554
555/*
556 * nss_ipv6_update_conn_count()
557 * Sets the maximum number of IPv6 connections.
558 *
559 * It first gets the connection tables size information from NSS FW
560 * and then configures the connections in NSS FW.
561 */
562int nss_ipv6_update_conn_count(int ipv6_num_conn)
563{
564 struct nss_ctx_instance *nss_ctx = nss_ipv6_get_mgr();
565 struct nss_ipv6_msg nim;
566 struct nss_ipv6_rule_conn_get_table_size_msg *nircgts;
567 nss_tx_status_t nss_tx_status;
Tushar Mathura3c18932015-08-24 20:27:21 +0530568 uint32_t sum_of_conn;
569
570 /*
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700571 * By default, NSS FW is configured with default number of connections.
572 */
Stephen Wang01433692017-10-30 14:45:11 -0700573 if (ipv6_num_conn == NSS_FW_DEFAULT_NUM_CONN) {
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700574 nss_info("%p: Default number of connections (%d) already configured\n", nss_ctx, ipv6_num_conn);
575 return 0;
576 }
577
578 /*
Tushar Mathura3c18932015-08-24 20:27:21 +0530579 * Specifications for input
580 * 1) The input should be power of 2.
581 * 2) Input for ipv4 and ipv6 sum togther should not exceed 8k
582 * 3) Min. value should be at leat 256 connections. This is the
583 * minimum connections we will support for each of them.
584 */
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700585 sum_of_conn = nss_ipv4_conn_cfg + ipv6_num_conn;
586 if ((ipv6_num_conn & NSS_NUM_CONN_QUANTA_MASK) ||
Tushar Mathura3c18932015-08-24 20:27:21 +0530587 (sum_of_conn > NSS_MAX_TOTAL_NUM_CONN_IPV4_IPV6) ||
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700588 (ipv6_num_conn < NSS_MIN_NUM_CONN)) {
Tushar Mathura3c18932015-08-24 20:27:21 +0530589 nss_warning("%p: input supported connections (%d) does not adhere\
590 specifications\n1) not power of 2,\n2) is less than \
591 min val: %d, OR\n IPv4/6 total exceeds %d\n",
592 nss_ctx,
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700593 ipv6_num_conn,
Tushar Mathura3c18932015-08-24 20:27:21 +0530594 NSS_MIN_NUM_CONN,
595 NSS_MAX_TOTAL_NUM_CONN_IPV4_IPV6);
596 return -EINVAL;
597 }
598
Tushar Mathura3c18932015-08-24 20:27:21 +0530599 memset(&nim, 0, sizeof(struct nss_ipv6_msg));
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700600 nss_ipv6_msg_init(&nim, NSS_IPV6_RX_INTERFACE, NSS_IPV6_TX_CONN_TABLE_SIZE_MSG,
601 sizeof(struct nss_ipv6_rule_conn_get_table_size_msg), nss_ipv6_update_conn_count_callback, NULL);
Tushar Mathura3c18932015-08-24 20:27:21 +0530602
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700603 nircgts = &nim.msg.size;
604 nircgts->num_conn = htonl(ipv6_num_conn);
Tushar Mathura3c18932015-08-24 20:27:21 +0530605 nss_tx_status = nss_ipv6_tx(nss_ctx, &nim);
Tushar Mathura3c18932015-08-24 20:27:21 +0530606 if (nss_tx_status != NSS_TX_SUCCESS) {
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700607 nss_warning("%p: Send acceleration mode message failed\n", nss_ctx);
608 return -EINVAL;
Tushar Mathura3c18932015-08-24 20:27:21 +0530609 }
610
611 return 0;
612}
613
614/*
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700615 * nss_ipv6_free_conn_tables()
616 * Frees memory allocated for connection tables
Vijay Dewangan9db18752014-09-15 16:25:01 -0700617 */
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700618void nss_ipv6_free_conn_tables(void)
Vijay Dewangan9db18752014-09-15 16:25:01 -0700619{
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700620 if (nss_ipv6_ct_info.ce_mem) {
621 free_pages(nss_ipv6_ct_info.ce_mem, get_order(nss_ipv6_ct_info.ce_table_size));
Vijay Dewangan9db18752014-09-15 16:25:01 -0700622 }
623
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700624 if (nss_ipv6_ct_info.cme_mem) {
625 free_pages(nss_ipv6_ct_info.cme_mem, get_order(nss_ipv6_ct_info.cme_table_size));
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700626 }
Vijay Dewangan9db18752014-09-15 16:25:01 -0700627
Suruchi Agarwalcdcb5b42017-07-06 11:08:22 -0700628 memset(&nss_ipv6_ct_info, 0, sizeof(struct nss_ipv6_conn_table_info));
629 return;
Tushar Mathura3c18932015-08-24 20:27:21 +0530630}
631
Stephen Wang0a2756a2016-08-04 15:52:47 -0700632/*
Stephen Wang0a2756a2016-08-04 15:52:47 -0700633 * nss_ipv6_accel_mode_cfg_handler()
634 * Configure acceleration mode for IPv6
635 */
636static int nss_ipv6_accel_mode_cfg_handler(struct ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
637{
638 struct nss_top_instance *nss_top = &nss_top_main;
639 struct nss_ctx_instance *nss_ctx = &nss_top->nss[0];
640 struct nss_ipv6_msg nim;
641 struct nss_ipv6_accel_mode_cfg_msg *nipcm;
642 nss_tx_status_t nss_tx_status;
643 int ret = NSS_FAILURE;
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700644 int current_value;
Stephen Wang0a2756a2016-08-04 15:52:47 -0700645
646 /*
647 * Take snap shot of current value
648 */
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700649 current_value = nss_ipv6_accel_mode_cfg;
Stephen Wang0a2756a2016-08-04 15:52:47 -0700650
651 /*
652 * Write the variable with user input
653 */
654 ret = proc_dointvec(ctl, write, buffer, lenp, ppos);
655 if (ret || (!write)) {
Stephen Wang0a2756a2016-08-04 15:52:47 -0700656 return ret;
657 }
658
659 memset(&nim, 0, sizeof(struct nss_ipv6_msg));
660 nss_ipv6_msg_init(&nim, NSS_IPV6_RX_INTERFACE, NSS_IPV6_TX_ACCEL_MODE_CFG_MSG,
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700661 sizeof(struct nss_ipv6_accel_mode_cfg_msg), NULL, NULL);
Stephen Wang0a2756a2016-08-04 15:52:47 -0700662
663 nipcm = &nim.msg.accel_mode_cfg;
664 nipcm->mode = htonl(nss_ipv6_accel_mode_cfg);
Stephen Wang0a2756a2016-08-04 15:52:47 -0700665
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700666 nss_tx_status = nss_ipv6_tx_sync(nss_ctx, &nim);
Stephen Wang0a2756a2016-08-04 15:52:47 -0700667 if (nss_tx_status != NSS_TX_SUCCESS) {
668 nss_warning("%p: Send acceleration mode message failed\n", nss_ctx);
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700669 nss_ipv6_accel_mode_cfg = current_value;
670 return -EIO;
671 }
672
673 return 0;
674}
675
676/*
677 * nss_ipv6_dscp_map_cfg_handler()
678 * Sysctl handler for dscp/pri mappings.
679 */
680static int nss_ipv6_dscp_map_cfg_handler(struct ctl_table *ctl, int write, void __user *buffer, size_t *lenp, loff_t *ppos)
681{
682 struct nss_top_instance *nss_top = &nss_top_main;
683 struct nss_ctx_instance *nss_ctx = &nss_top->nss[0];
684 struct nss_dscp_map_parse out;
685 struct nss_ipv6_msg nim;
686 struct nss_ipv6_dscp2pri_cfg_msg *nipd2p;
687 nss_tx_status_t status;
688 int ret;
689
690 if (!write) {
691 return nss_dscp_map_print(ctl, buffer, lenp, ppos, mapping);
692 }
693
694 ret = nss_dscp_map_parse(ctl, buffer, lenp, ppos, &out);
695 if (ret) {
696 nss_warning("failed to parse dscp mapping:%d\n", ret);
697 return ret;
698 }
699
700 if (out.action >= NSS_IPV6_DSCP_MAP_ACTION_MAX) {
701 nss_warning("invalid action value: %d\n", out.action);
702 nss_ipv6_dscp_map_usage();
703 return -EINVAL;
704 }
705
706 memset(&nim, 0, sizeof(struct nss_ipv6_msg));
707 nss_ipv6_msg_init(&nim, NSS_IPV6_RX_INTERFACE, NSS_IPV6_TX_DSCP2PRI_CFG_MSG,
708 sizeof(struct nss_ipv6_dscp2pri_cfg_msg), NULL, NULL);
709
710 nipd2p = &nim.msg.dscp2pri_cfg;
711 nipd2p->dscp = out.dscp;
712 nipd2p->priority = out.priority;
713
714 status = nss_ipv6_tx_sync(nss_ctx, &nim);
715 if (status != NSS_TX_SUCCESS) {
716 nss_warning("%p: ipv6 dscp2pri config message failed\n", nss_ctx);
717 return -EFAULT;
Stephen Wang0a2756a2016-08-04 15:52:47 -0700718 }
719
720 /*
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700721 * NSS firmware acknowleged the configuration, so update the mapping
722 * table on HOST side as well.
Stephen Wang0a2756a2016-08-04 15:52:47 -0700723 */
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700724 mapping[out.dscp].action = out.action;
725 mapping[out.dscp].priority = out.priority;
Stephen Wang0a2756a2016-08-04 15:52:47 -0700726
Stephen Wang0a2756a2016-08-04 15:52:47 -0700727 return 0;
Stephen Wang0a2756a2016-08-04 15:52:47 -0700728}
729
Stephen Wang52e6d342016-03-29 15:02:33 -0700730static struct ctl_table nss_ipv6_table[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700731 {
Stephen Wang0a2756a2016-08-04 15:52:47 -0700732 .procname = "ipv6_accel_mode",
733 .data = &nss_ipv6_accel_mode_cfg,
734 .maxlen = sizeof(int),
735 .mode = 0644,
736 .proc_handler = &nss_ipv6_accel_mode_cfg_handler,
Vijay Dewangan9db18752014-09-15 16:25:01 -0700737 },
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700738 {
739 .procname = "ipv6_dscp_map",
740 .data = &mapping[NSS_DSCP_MAP_ARRAY_SIZE],
741 .maxlen = sizeof(struct nss_dscp_map_entry),
742 .mode = 0644,
743 .proc_handler = &nss_ipv6_dscp_map_cfg_handler,
744 },
Vijay Dewangan9db18752014-09-15 16:25:01 -0700745 { }
746};
747
Stephen Wang52e6d342016-03-29 15:02:33 -0700748static struct ctl_table nss_ipv6_dir[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700749 {
Vijay Dewangan4861f4e2014-10-14 16:56:35 -0700750 .procname = "ipv6cfg",
751 .mode = 0555,
752 .child = nss_ipv6_table,
Vijay Dewangan9db18752014-09-15 16:25:01 -0700753 },
754 { }
755};
756
Stephen Wang52e6d342016-03-29 15:02:33 -0700757static struct ctl_table nss_ipv6_root_dir[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700758 {
759 .procname = "nss",
760 .mode = 0555,
761 .child = nss_ipv6_dir,
762 },
763 { }
764};
765
Stephen Wang52e6d342016-03-29 15:02:33 -0700766static struct ctl_table nss_ipv6_root[] = {
Vijay Dewangan9db18752014-09-15 16:25:01 -0700767 {
768 .procname = "dev",
769 .mode = 0555,
770 .child = nss_ipv6_root_dir,
771 },
772 { }
773};
774
775static struct ctl_table_header *nss_ipv6_header;
776
777/*
778 * nss_ipv6_register_sysctl()
Kiran Kumar C.S.Kfadf92d2015-06-26 12:48:33 +0530779 * Register sysctl specific to ipv6
Vijay Dewangan9db18752014-09-15 16:25:01 -0700780 */
781void nss_ipv6_register_sysctl(void)
782{
Sakthi Vignesh Radhakrishnan8d02a2d2017-09-06 14:38:59 -0700783 sema_init(&nss_ipv6_pvt.sem, 1);
784 init_completion(&nss_ipv6_pvt.complete);
Stephen Wang49b474b2016-03-25 10:40:30 -0700785
Vijay Dewangan9db18752014-09-15 16:25:01 -0700786 /*
787 * Register sysctl table.
788 */
789 nss_ipv6_header = register_sysctl_table(nss_ipv6_root);
790}
791
792/*
793 * nss_ipv6_unregister_sysctl()
Kiran Kumar C.S.Kfadf92d2015-06-26 12:48:33 +0530794 * Unregister sysctl specific to ipv6
Vijay Dewangan9db18752014-09-15 16:25:01 -0700795 */
796void nss_ipv6_unregister_sysctl(void)
797{
798 /*
799 * Unregister sysctl table.
800 */
801 if (nss_ipv6_header) {
802 unregister_sysctl_table(nss_ipv6_header);
803 }
804}
805
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700806/*
807 * nss_ipv6_msg_init()
Stephen Wang0a2756a2016-08-04 15:52:47 -0700808 * Initialize IPv6 message.
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700809 */
810void nss_ipv6_msg_init(struct nss_ipv6_msg *nim, uint16_t if_num, uint32_t type, uint32_t len,
Sundarajan Srinivasan30a53d42015-01-30 10:52:08 -0800811 nss_ipv6_msg_callback_t cb, void *app_data)
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700812{
813 nss_cmn_msg_init(&nim->cm, if_num, type, len, (void *)cb, app_data);
814}
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700815EXPORT_SYMBOL(nss_ipv6_msg_init);