blob: 139499d62d8aef8248a06ebb8e109369186086a0 [file] [log] [blame]
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +05301/*
2 **************************************************************************
Vijay Vigneshanb809f422018-04-04 15:04:35 +05303 * Copyright (c) 2013-2018, The Linux Foundation. All rights reserved.
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +05304 * Permission to use, copy, modify, and/or distribute this software for
5 * any purpose with or without fee is hereby granted, provided that the
6 * above copyright notice and this permission notice appear in all copies.
7 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
10 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
12 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
13 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
14 **************************************************************************
15 */
16
17/*
18 * nss_ipsec.c
19 * NSS IPsec APIs
20 */
21
22#include "nss_tx_rx_common.h"
Murat Sezginea1a4352014-04-15 19:09:51 -070023#include "nss_ipsec.h"
Amit Guptacbab9f42018-04-13 16:11:25 +053024#include "nss_ppe.h"
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053025
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053026#if defined(NSS_HAL_IPQ806X_SUPPORT)
27#define NSS_IPSEC_ENCAP_INTERFACE_NUM NSS_IPSEC_ENCAP_IF_NUMBER
28#define NSS_IPSEC_DECAP_INTERFACE_NUM NSS_IPSEC_DECAP_IF_NUMBER
29#define NSS_IPSEC_DATA_INTERFACE_NUM NSS_C2C_TX_INTERFACE
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053030
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053031#elif defined(NSS_HAL_FSM9010_SUPPORT)
32#define NSS_IPSEC_ENCAP_INTERFACE_NUM NSS_IPSEC_ENCAP_IF_NUMBER
33#define NSS_IPSEC_DECAP_INTERFACE_NUM NSS_IPSEC_DECAP_IF_NUMBER
34#define NSS_IPSEC_DATA_INTERFACE_NUM NSS_IPSEC_RULE_INTERFACE
35
36#elif defined(NSS_HAL_IPQ807x_SUPPORT)
37#define NSS_IPSEC_ENCAP_INTERFACE_NUM NSS_IPSEC_RULE_INTERFACE
38#define NSS_IPSEC_DECAP_INTERFACE_NUM NSS_IPSEC_RULE_INTERFACE
39#define NSS_IPSEC_DATA_INTERFACE_NUM NSS_IPSEC_RULE_INTERFACE
40
41#else
42#define NSS_IPSEC_ENCAP_INTERFACE_NUM -1
43#define NSS_IPSEC_DECAP_INTERFACE_NUM -1
44#define NSS_IPSEC_DATA_INTERFACE_NUM -1
45
46#endif
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053047
48/*
Vijay Vigneshanb809f422018-04-04 15:04:35 +053049 * Amount time the synchronous message should wait for response from
50 * NSS before the timeout happens. After the timeout the message
51 * response even if it arrives has to be discarded. Typically, the
52 * time needs to be selected based on the worst case time in case of
53 * peak throughput between host & NSS.
54 */
55#define NSS_IPSEC_TX_TIMEO_TICKS msecs_to_jiffies(3000) /* 3 Seconds */
56
57/*
58 * Private data structure to hold state for
59 * the ipsec specific NSS interaction
60 */
61struct nss_ipsec_pvt {
62 struct semaphore sem; /* used for synchronizing 'tx_msg_sync' */
63 struct completion complete; /* completion callback */
64 atomic_t resp; /* Response error type */
65} nss_ipsec;
66
67/*
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053068 * nss_ipsec_get_msg_ctx()
69 * return ipsec message context assoicated with the callback
70 *
71 * Note: certain SOC the decap interface specially programmed
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053072 */
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053073static inline nss_ptr_t nss_ipsec_get_msg_ctx(struct nss_ctx_instance *nss_ctx, uint32_t interface_num)
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053074{
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053075 struct nss_top_instance *nss_top = nss_ctx->nss_top;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053076
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053077 /*
78 * the encap is primary interface
79 */
Sourav Poddareb8cb652017-05-18 12:02:18 +053080 if (interface_num == NSS_IPSEC_ENCAP_INTERFACE_NUM)
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053081 return (nss_ptr_t)nss_top->ipsec_encap_ctx;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053082
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053083 return (nss_ptr_t)nss_top->ipsec_decap_ctx;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053084}
85
86/*
87 * nss_ipsec_get_msg_callback()
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053088 * this gets the message callback handler
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053089 */
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053090static inline nss_ptr_t nss_ipsec_get_msg_callback(struct nss_ctx_instance *nss_ctx, uint32_t interface_num)
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053091{
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053092 struct nss_top_instance *nss_top = nss_ctx->nss_top;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053093
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053094 /*
95 * the encap is primary interface
96 */
Sourav Poddareb8cb652017-05-18 12:02:18 +053097 if (interface_num == NSS_IPSEC_ENCAP_INTERFACE_NUM)
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053098 return (nss_ptr_t)nss_top->ipsec_encap_callback;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053099
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530100 return (nss_ptr_t)nss_top->ipsec_decap_callback;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530101}
102
103/*
104 **********************************
105 Rx APIs
106 **********************************
107 */
108
109/*
110 * nss_ipsec_msg_handler()
111 * this handles all the IPsec events and responses
112 */
113static void nss_ipsec_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, void *app_data __attribute((unused)))
114{
115 struct nss_ipsec_msg *nim = (struct nss_ipsec_msg *)ncm;
116 nss_ipsec_msg_callback_t cb = NULL;
117 uint32_t if_num = ncm->interface;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530118
119 /*
120 * Sanity check the message type
121 */
122 if (ncm->type > NSS_IPSEC_MSG_TYPE_MAX) {
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530123 nss_warning("%p: rx message type out of range: %d", nss_ctx, ncm->type);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530124 return;
125 }
126
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800127 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ipsec_msg)) {
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530128 nss_warning("%p: rx message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530129 return;
130 }
131
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530132 BUG_ON((if_num != NSS_IPSEC_ENCAP_INTERFACE_NUM) && (if_num != NSS_IPSEC_DECAP_INTERFACE_NUM));
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530133
134 if (ncm->response == NSS_CMN_RESPONSE_LAST) {
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530135 nss_warning("%p: rx message response for if %d, type %d, is invalid: %d", nss_ctx, ncm->interface,
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530136 ncm->type, ncm->response);
137 return;
138 }
139
140 /*
141 * Is this a notification? if, yes then fill up the callback and app_data from
142 * locally stored state
143 */
Suruchi Agarwale4ad24a2018-06-11 12:03:46 +0530144 if (ncm->response == NSS_CMN_RESPONSE_NOTIFY) {
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530145 ncm->cb = nss_ipsec_get_msg_callback(nss_ctx, if_num);
146 ncm->app_data = nss_ipsec_get_msg_ctx(nss_ctx, if_num);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530147 }
148
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530149 nss_core_log_msg_failures(nss_ctx, ncm);
150
151 /*
152 * load, test & call
153 */
154 cb = (nss_ipsec_msg_callback_t)ncm->cb;
155 if (unlikely(!cb)) {
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530156 nss_trace("%p: rx handler has been unregistered for i/f: %d", nss_ctx, ncm->interface);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530157 return;
158 }
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530159
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530160 cb((void *)ncm->app_data, nim);
161}
162
163/*
164 **********************************
165 Tx APIs
166 **********************************
167 */
168
169/*
Vijay Vigneshanb809f422018-04-04 15:04:35 +0530170 * nss_ipsec_callback()
171 * Callback to handle the completion of NSS->HLOS messages.
172 */
173static void nss_ipsec_callback(void *app_data, struct nss_ipsec_msg *nim)
174{
175 struct nss_cmn_msg *ncm = &nim->cm;
176
177 /*
178 * This callback is for synchronous operation. The caller sends its
179 * response pointer which needs to be loaded with the response
180 * data arriving from the NSS
181 */
182 atomic_t *resp = (atomic_t *)app_data;
183
184 if (ncm->response == NSS_CMN_RESPONSE_ACK) {
185 atomic_set(resp, NSS_IPSEC_ERROR_TYPE_NONE);
186 complete(&nss_ipsec.complete);
187 return;
188 }
189
190 atomic_set(resp, ncm->error);
191 complete(&nss_ipsec.complete);
192}
193
194/*
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530195 * nss_ipsec_tx_msg
196 * Send ipsec rule to NSS.
197 */
198nss_tx_status_t nss_ipsec_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_ipsec_msg *msg)
199{
200 struct nss_cmn_msg *ncm = &msg->cm;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530201
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530202 nss_info("%p: message %d for if %d\n", nss_ctx, ncm->type, ncm->interface);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530203
Tanmay V Jagdalef6b2bce2017-03-03 14:31:07 +0530204 BUILD_BUG_ON(NSS_NBUF_PAYLOAD_SIZE < sizeof(struct nss_ipsec_msg));
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530205
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530206 if ((ncm->interface != NSS_IPSEC_ENCAP_INTERFACE_NUM) && (ncm->interface != NSS_IPSEC_DECAP_INTERFACE_NUM)) {
207 nss_warning("%p: tx message request for another interface: %d", nss_ctx, ncm->interface);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530208 return NSS_TX_FAILURE;
209 }
210
211 if (ncm->type > NSS_IPSEC_MSG_TYPE_MAX) {
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530212 nss_warning("%p: tx message type out of range: %d", nss_ctx, ncm->type);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530213 return NSS_TX_FAILURE;
214 }
215
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530216 nss_info("msg params version:%d, interface:%d, type:%d, cb:%p, app_data:%p, len:%d\n",
Stephen Wangaed46332016-12-12 17:29:03 -0800217 ncm->version, ncm->interface, ncm->type, (void *)ncm->cb, (void *)ncm->app_data, ncm->len);
Samarjeet Banerjee7bce8c52014-05-02 15:32:13 +0530218
Stephen Wang3e2dbd12018-03-14 17:28:17 -0700219 return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530220}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530221EXPORT_SYMBOL(nss_ipsec_tx_msg);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530222
223/*
Vijay Vigneshanb809f422018-04-04 15:04:35 +0530224 * nss_ipsec_tx_msg_sync()
225 * Transmit a ipsec message to NSS firmware synchronously.
226 */
227nss_tx_status_t nss_ipsec_tx_msg_sync(struct nss_ctx_instance *nss_ctx, uint32_t if_num,
228 enum nss_ipsec_msg_type type, uint16_t len,
229 struct nss_ipsec_msg *nim, enum nss_ipsec_error_type *resp)
230{
231 struct nss_ipsec_msg nim_local = { {0} };
232 nss_tx_status_t status;
233 int ret;
234
235 /*
236 * Length of the message should be the based on type
237 */
238 if (len > sizeof(nim_local.msg)) {
239 nss_warning("%p: (%u)Bad message length(%u) for type (%d)", nss_ctx, if_num, len, type);
240 return NSS_TX_FAILURE_TOO_LARGE;
241 }
242
243 /*
244 * Response buffer is a required for copying the response for message
245 */
246 if (!resp) {
247 nss_warning("%p: (%u)Response buffer is empty, type(%d)", nss_ctx, if_num, type);
248 return NSS_TX_FAILURE_BAD_PARAM;
249 }
250
251 /*
252 * TODO: this can be removed in future as we need to ensure that the response
253 * memory is only updated when the current outstanding request is waiting.
254 * This can be solved by introducing sequence no. in messages and only completing
255 * the message if the sequence no. matches. For now this is solved by passing
256 * a known memory nss_ipsec.resp
257 */
258 down(&nss_ipsec.sem);
259
260 /*
261 * Initializing it to a fail error type
262 */
263 atomic_set(&nss_ipsec.resp, NSS_IPSEC_ERROR_TYPE_UNHANDLED_MSG);
264
265 /*
266 * We need to copy the message content into the actual message
267 * to be sent to NSS
268 *
269 * Note: Here pass the nss_ipsec.resp as the pointer. Since, the caller
270 * provided pointer is not allocated by us and may go away when this function
271 * returns with failure. The callback is not aware of this and may try to
272 * access the pointer incorrectly potentially resulting in a crash.
273 */
274 nss_ipsec_msg_init(&nim_local, if_num, type, len, nss_ipsec_callback, &nss_ipsec.resp);
275 memcpy(&nim_local.msg, &nim->msg, len);
276
277 status = nss_ipsec_tx_msg(nss_ctx, &nim_local);
278 if (status != NSS_TX_SUCCESS) {
279 nss_warning("%p: ipsec_tx_msg failed", nss_ctx);
280 goto done;
281 }
282
283 ret = wait_for_completion_timeout(&nss_ipsec.complete, NSS_IPSEC_TX_TIMEO_TICKS);
284 if (!ret) {
285 nss_warning("%p: IPsec msg tx failed due to timeout", nss_ctx);
286 status = NSS_TX_FAILURE_NOT_ENABLED;
287 goto done;
288 }
289
290 /*
291 * Read memory barrier
292 */
293 smp_rmb();
294
295 /*
296 * Copy the response received
297 */
298 *resp = atomic_read(&nss_ipsec.resp);
299
300 /*
301 * Only in case of non-error response we will
302 * indicate success
303 */
304 if (*resp != NSS_IPSEC_ERROR_TYPE_NONE)
305 status = NSS_TX_FAILURE;
306
307done:
308 up(&nss_ipsec.sem);
309 return status;
310}
311EXPORT_SYMBOL(nss_ipsec_tx_msg_sync);
312
313/*
Radha krishna Simha Jigurud36b1e22014-09-12 15:14:52 +0530314 * nss_ipsec_tx_buf
315 * Send data packet for ipsec processing
316 */
Stephen Wang83e48752015-03-03 18:41:40 -0800317nss_tx_status_t nss_ipsec_tx_buf(struct sk_buff *skb, uint32_t if_num)
Radha krishna Simha Jigurud36b1e22014-09-12 15:14:52 +0530318{
Radha krishna Simha Jigurud36b1e22014-09-12 15:14:52 +0530319 struct nss_ctx_instance *nss_ctx = &nss_top_main.nss[nss_top_main.ipsec_handler_id];
Radha krishna Simha Jigurud36b1e22014-09-12 15:14:52 +0530320
Stephen Wang83e48752015-03-03 18:41:40 -0800321 nss_trace("%p: IPsec If Tx packet, id:%d, data=%p", nss_ctx, if_num, skb->data);
Radha krishna Simha Jigurud36b1e22014-09-12 15:14:52 +0530322
Stephen Wang3e2dbd12018-03-14 17:28:17 -0700323 return nss_core_send_packet(nss_ctx, skb, if_num, 0);
Radha krishna Simha Jigurud36b1e22014-09-12 15:14:52 +0530324}
325EXPORT_SYMBOL(nss_ipsec_tx_buf);
326
327/*
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530328 **********************************
329 Register APIs
330 **********************************
331 */
332
333/*
Ankit Dhanukaa0e4cae2014-05-26 16:33:10 +0530334 * nss_ipsec_notify_register()
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530335 * register message notifier for the given interface (if_num)
336 */
337struct nss_ctx_instance *nss_ipsec_notify_register(uint32_t if_num, nss_ipsec_msg_callback_t cb, void *app_data)
338{
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530339 struct nss_top_instance *nss_top = &nss_top_main;
340 uint8_t core_id = nss_top->ipsec_handler_id;
341 struct nss_ctx_instance *nss_ctx = &nss_top->nss[core_id];
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530342
343 if (if_num >= NSS_MAX_NET_INTERFACES) {
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530344 nss_warning("%p: notfiy register received for invalid interface %d", nss_ctx, if_num);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530345 return NULL;
346 }
347
Samarjeet Banerjee940e91b2016-03-29 19:23:17 +0530348 /*
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530349 * the encap is primary interface
Samarjeet Banerjee940e91b2016-03-29 19:23:17 +0530350 */
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530351 if (if_num == NSS_IPSEC_ENCAP_INTERFACE_NUM) {
352 nss_top->ipsec_encap_callback = cb;
353 nss_top->ipsec_encap_ctx = app_data;
Samarjeet Banerjee940e91b2016-03-29 19:23:17 +0530354 return nss_ctx;
355 }
356
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530357 nss_top->ipsec_decap_callback = cb;
358 nss_top->ipsec_decap_ctx = app_data;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530359 return nss_ctx;
360}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530361EXPORT_SYMBOL(nss_ipsec_notify_register);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530362
363/*
Ankit Dhanukaa0e4cae2014-05-26 16:33:10 +0530364 * nss_ipsec_notify_unregister()
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530365 * unregister the IPsec notifier for the given interface number (if_num)
366 */
367void nss_ipsec_notify_unregister(struct nss_ctx_instance *nss_ctx, uint32_t if_num)
368{
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530369 struct nss_top_instance *nss_top = nss_ctx->nss_top;
370
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530371 if (if_num >= NSS_MAX_NET_INTERFACES) {
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530372 nss_warning("%p: notify unregister received for invalid interface %d", nss_ctx, if_num);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530373 return;
374 }
375
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530376 /*
377 * the encap is primary interface
378 */
379 if (if_num == NSS_IPSEC_ENCAP_INTERFACE_NUM) {
380 nss_top->ipsec_encap_callback = NULL;
381 nss_top->ipsec_encap_ctx = NULL;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530382 return;
383 }
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530384
385 nss_top->ipsec_decap_callback = NULL;
386 nss_top->ipsec_decap_ctx = NULL;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530387}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530388EXPORT_SYMBOL(nss_ipsec_notify_unregister);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530389
390/*
Ankit Dhanukaa0e4cae2014-05-26 16:33:10 +0530391 * nss_ipsec_data_register()
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530392 * register a data callback routine
393 */
Sundarajan Srinivasan70374842014-11-19 15:22:52 -0800394struct nss_ctx_instance *nss_ipsec_data_register(uint32_t if_num, nss_ipsec_buf_callback_t cb, struct net_device *netdev, uint32_t features)
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530395{
Arunkumar T657c4062016-09-21 18:06:48 +0530396 struct nss_ctx_instance *nss_ctx, *nss_ctx0;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530397
398 nss_ctx = &nss_top_main.nss[nss_top_main.ipsec_handler_id];
399
400 if ((if_num >= NSS_MAX_NET_INTERFACES) && (if_num < NSS_MAX_PHYSICAL_INTERFACES)){
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530401 nss_warning("%p: data register received for invalid interface %d", nss_ctx, if_num);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530402 return NULL;
403 }
404
Samarjeet Banerjee940e91b2016-03-29 19:23:17 +0530405 /*
406 * avoid multiple registeration for multiple tunnels
407 */
Stephen Wang84e0e992016-09-07 12:31:40 -0700408 if (nss_ctx->subsys_dp_register[if_num].cb) {
Samarjeet Banerjee940e91b2016-03-29 19:23:17 +0530409 return nss_ctx;
410 }
411
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700412 nss_core_register_subsys_dp(nss_ctx, if_num, cb, NULL, NULL, netdev, features);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530413
Arunkumar T657c4062016-09-21 18:06:48 +0530414 if (nss_top_main.ipsec_handler_id == 1) {
415 nss_ctx0 = &nss_top_main.nss[0];
416
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700417 nss_core_register_subsys_dp(nss_ctx0, if_num, cb, NULL, NULL, netdev, features);
Arunkumar T657c4062016-09-21 18:06:48 +0530418 }
419
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530420 return nss_ctx;
421}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530422EXPORT_SYMBOL(nss_ipsec_data_register);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530423
424/*
Ankit Dhanukaa0e4cae2014-05-26 16:33:10 +0530425 * nss_ipsec_data_unregister()
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530426 * unregister a data callback routine
427 */
428void nss_ipsec_data_unregister(struct nss_ctx_instance *nss_ctx, uint32_t if_num)
429{
Arunkumar T657c4062016-09-21 18:06:48 +0530430 struct nss_ctx_instance *nss_ctx0;
431
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530432 if ((if_num >= NSS_MAX_NET_INTERFACES) && (if_num < NSS_MAX_PHYSICAL_INTERFACES)){
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530433 nss_warning("%p: data unregister received for invalid interface %d", nss_ctx, if_num);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530434 return;
435 }
436
Arunkumar T657c4062016-09-21 18:06:48 +0530437 if (nss_top_main.ipsec_handler_id == 1) {
438 nss_ctx0 = &nss_top_main.nss[0];
439
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700440 nss_core_unregister_subsys_dp(nss_ctx0, if_num);
Arunkumar T657c4062016-09-21 18:06:48 +0530441 }
442
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700443 nss_core_unregister_subsys_dp(nss_ctx, if_num);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530444}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530445EXPORT_SYMBOL(nss_ipsec_data_unregister);
446
447/*
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530448 * nss_ipsec_get_encap_interface()
449 * Get the NSS interface number for encap message
Radha krishna Simha Jigurudb7ad242014-11-19 16:02:23 +0530450 */
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530451int32_t nss_ipsec_get_encap_interface(void)
Radha krishna Simha Jigurudb7ad242014-11-19 16:02:23 +0530452{
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530453 return NSS_IPSEC_ENCAP_INTERFACE_NUM;
Radha krishna Simha Jigurudb7ad242014-11-19 16:02:23 +0530454}
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530455EXPORT_SYMBOL(nss_ipsec_get_encap_interface);
456
457/*
458 * nss_ipsec_get_decap_interface()
459 * Get the NSS interface number for decap message
460 */
461int32_t nss_ipsec_get_decap_interface(void)
462{
463 return NSS_IPSEC_DECAP_INTERFACE_NUM;
464}
465EXPORT_SYMBOL(nss_ipsec_get_decap_interface);
466
467/*
468 * nss_ipsec_get_data_interface()
469 * Get the NSS interface number used for data path
470 */
471int32_t nss_ipsec_get_data_interface(void)
472{
473 return NSS_IPSEC_DATA_INTERFACE_NUM;
474}
475EXPORT_SYMBOL(nss_ipsec_get_data_interface);
Radha krishna Simha Jigurudb7ad242014-11-19 16:02:23 +0530476
477/*
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700478 * nss_ipsec_get_context()
Vijay Vigneshanb809f422018-04-04 15:04:35 +0530479 * Get NSS context instance for IPsec handle
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530480 */
481struct nss_ctx_instance *nss_ipsec_get_context(void)
482{
483 return &nss_top_main.nss[nss_top_main.ipsec_handler_id];
484}
485EXPORT_SYMBOL(nss_ipsec_get_context);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530486
487/*
Vijay Vigneshanb809f422018-04-04 15:04:35 +0530488 * nss_ipsec_ppe_port_config()
489 * Configure PPE port for IPsec inline
490 */
Vijay Vigneshan2475f412018-05-14 19:49:44 +0530491bool nss_ipsec_ppe_port_config(struct nss_ctx_instance *nss_ctx, struct net_device *netdev,
Vijay Vigneshanb809f422018-04-04 15:04:35 +0530492 uint32_t if_num, uint32_t vsi_num)
493{
494#ifdef NSS_PPE_SUPPORTED
495 if_num = NSS_INTERFACE_NUM_APPEND_COREID(nss_ctx, if_num);
496
Vijay Vigneshan2475f412018-05-14 19:49:44 +0530497 if (nss_ppe_tx_ipsec_config_msg(if_num, vsi_num, netdev->mtu, netdev->mtu) != NSS_TX_SUCCESS) {
Vijay Vigneshanb809f422018-04-04 15:04:35 +0530498 nss_warning("%p: Failed to configure PPE IPsec port", nss_ctx);
499 return false;
500 }
501
502 return true;
503#else
504 return false;
505#endif
506}
507EXPORT_SYMBOL(nss_ipsec_ppe_port_config);
508
509/*
Vijay Vigneshan2475f412018-05-14 19:49:44 +0530510 * nss_ipsec_ppe_mtu_update()
511 * Update PPE MTU for IPsec inline
512 */
513bool nss_ipsec_ppe_mtu_update(struct nss_ctx_instance *nss_ctx, uint32_t if_num, uint16_t mtu, uint16_t mru)
514{
515#ifdef NSS_PPE_SUPPORTED
516 if_num = NSS_INTERFACE_NUM_APPEND_COREID(nss_ctx, if_num);
517
518 if (nss_ppe_tx_ipsec_mtu_msg(if_num, mtu, mru) != NSS_TX_SUCCESS) {
519 nss_warning("%p: Failed to update PPE MTU for IPsec port", nss_ctx);
520 return false;
521 }
522
523 return true;
524#else
525 return false;
526#endif
527}
528EXPORT_SYMBOL(nss_ipsec_ppe_mtu_update);
529
530/*
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530531 * nss_ipsec_register_handler()
532 */
533void nss_ipsec_register_handler()
534{
535 struct nss_ctx_instance *nss_ctx = &nss_top_main.nss[nss_top_main.ipsec_handler_id];
536
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530537 BUILD_BUG_ON(NSS_IPSEC_ENCAP_INTERFACE_NUM < 0);
538 BUILD_BUG_ON(NSS_IPSEC_DECAP_INTERFACE_NUM < 0);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530539
Vijay Vigneshanb809f422018-04-04 15:04:35 +0530540 sema_init(&nss_ipsec.sem, 1);
541 init_completion(&nss_ipsec.complete);
542 atomic_set(&nss_ipsec.resp, NSS_IPSEC_ERROR_TYPE_NONE);
543
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530544 nss_ctx->nss_top->ipsec_encap_callback = NULL;
545 nss_ctx->nss_top->ipsec_decap_callback = NULL;
546
547 nss_ctx->nss_top->ipsec_encap_ctx = NULL;
548 nss_ctx->nss_top->ipsec_decap_ctx = NULL;
549
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700550 nss_core_register_handler(nss_ctx, NSS_IPSEC_ENCAP_INTERFACE_NUM, nss_ipsec_msg_handler, NULL);
551 nss_core_register_handler(nss_ctx, NSS_IPSEC_DECAP_INTERFACE_NUM, nss_ipsec_msg_handler, NULL);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530552}
553
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700554/*
555 * nss_ipsec_msg_init()
556 * Initialize ipsec message.
557 */
558void nss_ipsec_msg_init(struct nss_ipsec_msg *nim, uint16_t if_num, uint32_t type, uint32_t len,
Sundarajan Srinivasan30a53d42015-01-30 10:52:08 -0800559 nss_ipsec_msg_callback_t cb, void *app_data)
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700560{
561 nss_cmn_msg_init(&nim->cm, if_num, type, len, (void *)cb, app_data);
562}
563EXPORT_SYMBOL(nss_ipsec_msg_init);