blob: e67b9591b5ef47e5e0fb712582815235f3e27bcf [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"
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053024
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053025#if defined(NSS_HAL_IPQ806X_SUPPORT)
26#define NSS_IPSEC_ENCAP_INTERFACE_NUM NSS_IPSEC_ENCAP_IF_NUMBER
27#define NSS_IPSEC_DECAP_INTERFACE_NUM NSS_IPSEC_DECAP_IF_NUMBER
28#define NSS_IPSEC_DATA_INTERFACE_NUM NSS_C2C_TX_INTERFACE
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053029
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053030#elif defined(NSS_HAL_FSM9010_SUPPORT)
31#define NSS_IPSEC_ENCAP_INTERFACE_NUM NSS_IPSEC_ENCAP_IF_NUMBER
32#define NSS_IPSEC_DECAP_INTERFACE_NUM NSS_IPSEC_DECAP_IF_NUMBER
33#define NSS_IPSEC_DATA_INTERFACE_NUM NSS_IPSEC_RULE_INTERFACE
34
35#elif defined(NSS_HAL_IPQ807x_SUPPORT)
36#define NSS_IPSEC_ENCAP_INTERFACE_NUM NSS_IPSEC_RULE_INTERFACE
37#define NSS_IPSEC_DECAP_INTERFACE_NUM NSS_IPSEC_RULE_INTERFACE
38#define NSS_IPSEC_DATA_INTERFACE_NUM NSS_IPSEC_RULE_INTERFACE
39
40#else
41#define NSS_IPSEC_ENCAP_INTERFACE_NUM -1
42#define NSS_IPSEC_DECAP_INTERFACE_NUM -1
43#define NSS_IPSEC_DATA_INTERFACE_NUM -1
44
45#endif
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053046
47/*
Vijay Vigneshanb809f422018-04-04 15:04:35 +053048 * Amount time the synchronous message should wait for response from
49 * NSS before the timeout happens. After the timeout the message
50 * response even if it arrives has to be discarded. Typically, the
51 * time needs to be selected based on the worst case time in case of
52 * peak throughput between host & NSS.
53 */
54#define NSS_IPSEC_TX_TIMEO_TICKS msecs_to_jiffies(3000) /* 3 Seconds */
55
56/*
57 * Private data structure to hold state for
58 * the ipsec specific NSS interaction
59 */
60struct nss_ipsec_pvt {
61 struct semaphore sem; /* used for synchronizing 'tx_msg_sync' */
62 struct completion complete; /* completion callback */
63 atomic_t resp; /* Response error type */
64} nss_ipsec;
65
66/*
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053067 * nss_ipsec_get_msg_ctx()
68 * return ipsec message context assoicated with the callback
69 *
70 * Note: certain SOC the decap interface specially programmed
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053071 */
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053072static 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 +053073{
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053074 struct nss_top_instance *nss_top = nss_ctx->nss_top;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053075
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053076 /*
77 * the encap is primary interface
78 */
Sourav Poddareb8cb652017-05-18 12:02:18 +053079 if (interface_num == NSS_IPSEC_ENCAP_INTERFACE_NUM)
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053080 return (nss_ptr_t)nss_top->ipsec_encap_ctx;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053081
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053082 return (nss_ptr_t)nss_top->ipsec_decap_ctx;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053083}
84
85/*
86 * nss_ipsec_get_msg_callback()
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053087 * this gets the message callback handler
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053088 */
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053089static 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 +053090{
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053091 struct nss_top_instance *nss_top = nss_ctx->nss_top;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053092
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053093 /*
94 * the encap is primary interface
95 */
Sourav Poddareb8cb652017-05-18 12:02:18 +053096 if (interface_num == NSS_IPSEC_ENCAP_INTERFACE_NUM)
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053097 return (nss_ptr_t)nss_top->ipsec_encap_callback;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +053098
Sourav Poddarbf8b50c2017-02-15 12:44:44 +053099 return (nss_ptr_t)nss_top->ipsec_decap_callback;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530100}
101
102/*
103 **********************************
104 Rx APIs
105 **********************************
106 */
107
108/*
109 * nss_ipsec_msg_handler()
110 * this handles all the IPsec events and responses
111 */
112static void nss_ipsec_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, void *app_data __attribute((unused)))
113{
114 struct nss_ipsec_msg *nim = (struct nss_ipsec_msg *)ncm;
115 nss_ipsec_msg_callback_t cb = NULL;
116 uint32_t if_num = ncm->interface;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530117
118 /*
119 * Sanity check the message type
120 */
121 if (ncm->type > NSS_IPSEC_MSG_TYPE_MAX) {
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530122 nss_warning("%p: rx message type out of range: %d", nss_ctx, ncm->type);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530123 return;
124 }
125
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800126 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ipsec_msg)) {
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530127 nss_warning("%p: rx message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530128 return;
129 }
130
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530131 BUG_ON((if_num != NSS_IPSEC_ENCAP_INTERFACE_NUM) && (if_num != NSS_IPSEC_DECAP_INTERFACE_NUM));
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530132
133 if (ncm->response == NSS_CMN_RESPONSE_LAST) {
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530134 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 +0530135 ncm->type, ncm->response);
136 return;
137 }
138
139 /*
140 * Is this a notification? if, yes then fill up the callback and app_data from
141 * locally stored state
142 */
143 if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) {
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530144 ncm->cb = nss_ipsec_get_msg_callback(nss_ctx, if_num);
145 ncm->app_data = nss_ipsec_get_msg_ctx(nss_ctx, if_num);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530146 }
147
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530148 nss_core_log_msg_failures(nss_ctx, ncm);
149
150 /*
151 * load, test & call
152 */
153 cb = (nss_ipsec_msg_callback_t)ncm->cb;
154 if (unlikely(!cb)) {
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530155 nss_trace("%p: rx handler has been unregistered for i/f: %d", nss_ctx, ncm->interface);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530156 return;
157 }
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530158
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530159 cb((void *)ncm->app_data, nim);
160}
161
162/*
163 **********************************
164 Tx APIs
165 **********************************
166 */
167
168/*
Vijay Vigneshanb809f422018-04-04 15:04:35 +0530169 * nss_ipsec_callback()
170 * Callback to handle the completion of NSS->HLOS messages.
171 */
172static void nss_ipsec_callback(void *app_data, struct nss_ipsec_msg *nim)
173{
174 struct nss_cmn_msg *ncm = &nim->cm;
175
176 /*
177 * This callback is for synchronous operation. The caller sends its
178 * response pointer which needs to be loaded with the response
179 * data arriving from the NSS
180 */
181 atomic_t *resp = (atomic_t *)app_data;
182
183 if (ncm->response == NSS_CMN_RESPONSE_ACK) {
184 atomic_set(resp, NSS_IPSEC_ERROR_TYPE_NONE);
185 complete(&nss_ipsec.complete);
186 return;
187 }
188
189 atomic_set(resp, ncm->error);
190 complete(&nss_ipsec.complete);
191}
192
193/*
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530194 * nss_ipsec_tx_msg
195 * Send ipsec rule to NSS.
196 */
197nss_tx_status_t nss_ipsec_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_ipsec_msg *msg)
198{
199 struct nss_cmn_msg *ncm = &msg->cm;
200 struct nss_ipsec_msg *nim;
201 struct sk_buff *nbuf;
202 int32_t status;
203
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530204 nss_info("%p: message %d for if %d\n", nss_ctx, ncm->type, ncm->interface);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530205
206 NSS_VERIFY_CTX_MAGIC(nss_ctx);
207
208 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530209 nss_warning("%p: tx message dropped as core not ready", nss_ctx);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530210 return NSS_TX_FAILURE_NOT_READY;
211 }
212
Tanmay V Jagdalef6b2bce2017-03-03 14:31:07 +0530213 BUILD_BUG_ON(NSS_NBUF_PAYLOAD_SIZE < sizeof(struct nss_ipsec_msg));
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530214
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530215 if ((ncm->interface != NSS_IPSEC_ENCAP_INTERFACE_NUM) && (ncm->interface != NSS_IPSEC_DECAP_INTERFACE_NUM)) {
216 nss_warning("%p: tx message request for another interface: %d", nss_ctx, ncm->interface);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530217 return NSS_TX_FAILURE;
218 }
219
220 if (ncm->type > NSS_IPSEC_MSG_TYPE_MAX) {
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530221 nss_warning("%p: tx message type out of range: %d", nss_ctx, ncm->type);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530222 return NSS_TX_FAILURE;
223 }
224
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800225 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ipsec_msg)) {
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530226 nss_warning("%p: tx message request len for if %d, is bad: %d", nss_ctx, ncm->interface, nss_cmn_get_msg_len(ncm));
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530227 return NSS_TX_FAILURE_BAD_PARAM;
228 }
229
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530230 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530231 if (unlikely(!nbuf)) {
Sundarajan Srinivasan62fee7e2015-01-22 11:13:10 -0800232 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]);
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530233 nss_warning("%p: tx rule dropped as command allocation failed", nss_ctx);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530234 return NSS_TX_FAILURE;
235 }
236
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530237 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 -0800238 ncm->version, ncm->interface, ncm->type, (void *)ncm->cb, (void *)ncm->app_data, ncm->len);
Samarjeet Banerjee7bce8c52014-05-02 15:32:13 +0530239
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530240 nim = (struct nss_ipsec_msg *)skb_put(nbuf, sizeof(struct nss_ipsec_msg));
241 memcpy(nim, msg, sizeof(struct nss_ipsec_msg));
242
243 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
244 if (status != NSS_CORE_STATUS_SUCCESS) {
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530245 dev_kfree_skb_any(nbuf);
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530246 nss_warning("%p: tx Unable to enqueue message \n", nss_ctx);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530247 return NSS_TX_FAILURE;
248 }
249
Stephen Wang90c67de2016-04-26 15:15:59 -0700250 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530251
252 return NSS_TX_SUCCESS;
253}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530254EXPORT_SYMBOL(nss_ipsec_tx_msg);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530255
256/*
Vijay Vigneshanb809f422018-04-04 15:04:35 +0530257 * nss_ipsec_tx_msg_sync()
258 * Transmit a ipsec message to NSS firmware synchronously.
259 */
260nss_tx_status_t nss_ipsec_tx_msg_sync(struct nss_ctx_instance *nss_ctx, uint32_t if_num,
261 enum nss_ipsec_msg_type type, uint16_t len,
262 struct nss_ipsec_msg *nim, enum nss_ipsec_error_type *resp)
263{
264 struct nss_ipsec_msg nim_local = { {0} };
265 nss_tx_status_t status;
266 int ret;
267
268 /*
269 * Length of the message should be the based on type
270 */
271 if (len > sizeof(nim_local.msg)) {
272 nss_warning("%p: (%u)Bad message length(%u) for type (%d)", nss_ctx, if_num, len, type);
273 return NSS_TX_FAILURE_TOO_LARGE;
274 }
275
276 /*
277 * Response buffer is a required for copying the response for message
278 */
279 if (!resp) {
280 nss_warning("%p: (%u)Response buffer is empty, type(%d)", nss_ctx, if_num, type);
281 return NSS_TX_FAILURE_BAD_PARAM;
282 }
283
284 /*
285 * TODO: this can be removed in future as we need to ensure that the response
286 * memory is only updated when the current outstanding request is waiting.
287 * This can be solved by introducing sequence no. in messages and only completing
288 * the message if the sequence no. matches. For now this is solved by passing
289 * a known memory nss_ipsec.resp
290 */
291 down(&nss_ipsec.sem);
292
293 /*
294 * Initializing it to a fail error type
295 */
296 atomic_set(&nss_ipsec.resp, NSS_IPSEC_ERROR_TYPE_UNHANDLED_MSG);
297
298 /*
299 * We need to copy the message content into the actual message
300 * to be sent to NSS
301 *
302 * Note: Here pass the nss_ipsec.resp as the pointer. Since, the caller
303 * provided pointer is not allocated by us and may go away when this function
304 * returns with failure. The callback is not aware of this and may try to
305 * access the pointer incorrectly potentially resulting in a crash.
306 */
307 nss_ipsec_msg_init(&nim_local, if_num, type, len, nss_ipsec_callback, &nss_ipsec.resp);
308 memcpy(&nim_local.msg, &nim->msg, len);
309
310 status = nss_ipsec_tx_msg(nss_ctx, &nim_local);
311 if (status != NSS_TX_SUCCESS) {
312 nss_warning("%p: ipsec_tx_msg failed", nss_ctx);
313 goto done;
314 }
315
316 ret = wait_for_completion_timeout(&nss_ipsec.complete, NSS_IPSEC_TX_TIMEO_TICKS);
317 if (!ret) {
318 nss_warning("%p: IPsec msg tx failed due to timeout", nss_ctx);
319 status = NSS_TX_FAILURE_NOT_ENABLED;
320 goto done;
321 }
322
323 /*
324 * Read memory barrier
325 */
326 smp_rmb();
327
328 /*
329 * Copy the response received
330 */
331 *resp = atomic_read(&nss_ipsec.resp);
332
333 /*
334 * Only in case of non-error response we will
335 * indicate success
336 */
337 if (*resp != NSS_IPSEC_ERROR_TYPE_NONE)
338 status = NSS_TX_FAILURE;
339
340done:
341 up(&nss_ipsec.sem);
342 return status;
343}
344EXPORT_SYMBOL(nss_ipsec_tx_msg_sync);
345
346/*
Radha krishna Simha Jigurud36b1e22014-09-12 15:14:52 +0530347 * nss_ipsec_tx_buf
348 * Send data packet for ipsec processing
349 */
Stephen Wang83e48752015-03-03 18:41:40 -0800350nss_tx_status_t nss_ipsec_tx_buf(struct sk_buff *skb, uint32_t if_num)
Radha krishna Simha Jigurud36b1e22014-09-12 15:14:52 +0530351{
Radha krishna Simha Jigurud36b1e22014-09-12 15:14:52 +0530352 struct nss_ctx_instance *nss_ctx = &nss_top_main.nss[nss_top_main.ipsec_handler_id];
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530353 int32_t status;
Radha krishna Simha Jigurud36b1e22014-09-12 15:14:52 +0530354
Stephen Wang83e48752015-03-03 18:41:40 -0800355 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 +0530356
357 NSS_VERIFY_CTX_MAGIC(nss_ctx);
358 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
359 nss_warning("%p: 'IPsec If Tx' packet dropped as core not ready", nss_ctx);
360 return NSS_TX_FAILURE_NOT_READY;
361 }
362
Stephen Wang83e48752015-03-03 18:41:40 -0800363 status = nss_core_send_buffer(nss_ctx, if_num, skb, NSS_IF_DATA_QUEUE_0, H2N_BUFFER_PACKET, 0);
Radha krishna Simha Jigurud36b1e22014-09-12 15:14:52 +0530364 if (unlikely(status != NSS_CORE_STATUS_SUCCESS)) {
365 nss_warning("%p: Unable to enqueue 'IPsec If Tx' packet\n", nss_ctx);
366 if (status == NSS_CORE_STATUS_FAILURE_QUEUE) {
367 return NSS_TX_FAILURE_QUEUE;
368 }
369
370 return NSS_TX_FAILURE;
371 }
372
373 /*
374 * Kick the NSS awake so it can process our new entry.
375 */
Stephen Wang90c67de2016-04-26 15:15:59 -0700376 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
Radha krishna Simha Jigurud36b1e22014-09-12 15:14:52 +0530377 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_PACKET]);
378 return NSS_TX_SUCCESS;
379}
380EXPORT_SYMBOL(nss_ipsec_tx_buf);
381
382/*
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530383 **********************************
384 Register APIs
385 **********************************
386 */
387
388/*
Ankit Dhanukaa0e4cae2014-05-26 16:33:10 +0530389 * nss_ipsec_notify_register()
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530390 * register message notifier for the given interface (if_num)
391 */
392struct nss_ctx_instance *nss_ipsec_notify_register(uint32_t if_num, nss_ipsec_msg_callback_t cb, void *app_data)
393{
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530394 struct nss_top_instance *nss_top = &nss_top_main;
395 uint8_t core_id = nss_top->ipsec_handler_id;
396 struct nss_ctx_instance *nss_ctx = &nss_top->nss[core_id];
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530397
398 if (if_num >= NSS_MAX_NET_INTERFACES) {
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530399 nss_warning("%p: notfiy register received for invalid interface %d", nss_ctx, if_num);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530400 return NULL;
401 }
402
Samarjeet Banerjee940e91b2016-03-29 19:23:17 +0530403 /*
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530404 * the encap is primary interface
Samarjeet Banerjee940e91b2016-03-29 19:23:17 +0530405 */
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530406 if (if_num == NSS_IPSEC_ENCAP_INTERFACE_NUM) {
407 nss_top->ipsec_encap_callback = cb;
408 nss_top->ipsec_encap_ctx = app_data;
Samarjeet Banerjee940e91b2016-03-29 19:23:17 +0530409 return nss_ctx;
410 }
411
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530412 nss_top->ipsec_decap_callback = cb;
413 nss_top->ipsec_decap_ctx = app_data;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530414 return nss_ctx;
415}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530416EXPORT_SYMBOL(nss_ipsec_notify_register);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530417
418/*
Ankit Dhanukaa0e4cae2014-05-26 16:33:10 +0530419 * nss_ipsec_notify_unregister()
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530420 * unregister the IPsec notifier for the given interface number (if_num)
421 */
422void nss_ipsec_notify_unregister(struct nss_ctx_instance *nss_ctx, uint32_t if_num)
423{
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530424 struct nss_top_instance *nss_top = nss_ctx->nss_top;
425
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530426 if (if_num >= NSS_MAX_NET_INTERFACES) {
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530427 nss_warning("%p: notify unregister received for invalid interface %d", nss_ctx, if_num);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530428 return;
429 }
430
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530431 /*
432 * the encap is primary interface
433 */
434 if (if_num == NSS_IPSEC_ENCAP_INTERFACE_NUM) {
435 nss_top->ipsec_encap_callback = NULL;
436 nss_top->ipsec_encap_ctx = NULL;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530437 return;
438 }
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530439
440 nss_top->ipsec_decap_callback = NULL;
441 nss_top->ipsec_decap_ctx = NULL;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530442}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530443EXPORT_SYMBOL(nss_ipsec_notify_unregister);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530444
445/*
Ankit Dhanukaa0e4cae2014-05-26 16:33:10 +0530446 * nss_ipsec_data_register()
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530447 * register a data callback routine
448 */
Sundarajan Srinivasan70374842014-11-19 15:22:52 -0800449struct 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 +0530450{
Arunkumar T657c4062016-09-21 18:06:48 +0530451 struct nss_ctx_instance *nss_ctx, *nss_ctx0;
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530452
453 nss_ctx = &nss_top_main.nss[nss_top_main.ipsec_handler_id];
454
455 if ((if_num >= NSS_MAX_NET_INTERFACES) && (if_num < NSS_MAX_PHYSICAL_INTERFACES)){
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530456 nss_warning("%p: data register received for invalid interface %d", nss_ctx, if_num);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530457 return NULL;
458 }
459
Samarjeet Banerjee940e91b2016-03-29 19:23:17 +0530460 /*
461 * avoid multiple registeration for multiple tunnels
462 */
Stephen Wang84e0e992016-09-07 12:31:40 -0700463 if (nss_ctx->subsys_dp_register[if_num].cb) {
Samarjeet Banerjee940e91b2016-03-29 19:23:17 +0530464 return nss_ctx;
465 }
466
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700467 nss_core_register_subsys_dp(nss_ctx, if_num, cb, NULL, NULL, netdev, features);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530468
Arunkumar T657c4062016-09-21 18:06:48 +0530469 if (nss_top_main.ipsec_handler_id == 1) {
470 nss_ctx0 = &nss_top_main.nss[0];
471
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700472 nss_core_register_subsys_dp(nss_ctx0, if_num, cb, NULL, NULL, netdev, features);
Arunkumar T657c4062016-09-21 18:06:48 +0530473 }
474
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530475 return nss_ctx;
476}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530477EXPORT_SYMBOL(nss_ipsec_data_register);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530478
479/*
Ankit Dhanukaa0e4cae2014-05-26 16:33:10 +0530480 * nss_ipsec_data_unregister()
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530481 * unregister a data callback routine
482 */
483void nss_ipsec_data_unregister(struct nss_ctx_instance *nss_ctx, uint32_t if_num)
484{
Arunkumar T657c4062016-09-21 18:06:48 +0530485 struct nss_ctx_instance *nss_ctx0;
486
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530487 if ((if_num >= NSS_MAX_NET_INTERFACES) && (if_num < NSS_MAX_PHYSICAL_INTERFACES)){
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530488 nss_warning("%p: data unregister received for invalid interface %d", nss_ctx, if_num);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530489 return;
490 }
491
Arunkumar T657c4062016-09-21 18:06:48 +0530492 if (nss_top_main.ipsec_handler_id == 1) {
493 nss_ctx0 = &nss_top_main.nss[0];
494
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700495 nss_core_unregister_subsys_dp(nss_ctx0, if_num);
Arunkumar T657c4062016-09-21 18:06:48 +0530496 }
497
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700498 nss_core_unregister_subsys_dp(nss_ctx, if_num);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530499}
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530500EXPORT_SYMBOL(nss_ipsec_data_unregister);
501
502/*
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530503 * nss_ipsec_get_encap_interface()
504 * Get the NSS interface number for encap message
Radha krishna Simha Jigurudb7ad242014-11-19 16:02:23 +0530505 */
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530506int32_t nss_ipsec_get_encap_interface(void)
Radha krishna Simha Jigurudb7ad242014-11-19 16:02:23 +0530507{
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530508 return NSS_IPSEC_ENCAP_INTERFACE_NUM;
Radha krishna Simha Jigurudb7ad242014-11-19 16:02:23 +0530509}
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530510EXPORT_SYMBOL(nss_ipsec_get_encap_interface);
511
512/*
513 * nss_ipsec_get_decap_interface()
514 * Get the NSS interface number for decap message
515 */
516int32_t nss_ipsec_get_decap_interface(void)
517{
518 return NSS_IPSEC_DECAP_INTERFACE_NUM;
519}
520EXPORT_SYMBOL(nss_ipsec_get_decap_interface);
521
522/*
523 * nss_ipsec_get_data_interface()
524 * Get the NSS interface number used for data path
525 */
526int32_t nss_ipsec_get_data_interface(void)
527{
528 return NSS_IPSEC_DATA_INTERFACE_NUM;
529}
530EXPORT_SYMBOL(nss_ipsec_get_data_interface);
Radha krishna Simha Jigurudb7ad242014-11-19 16:02:23 +0530531
532/*
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700533 * nss_ipsec_get_context()
Vijay Vigneshanb809f422018-04-04 15:04:35 +0530534 * Get NSS context instance for IPsec handle
Samarjeet Banerjee77332312014-08-07 14:48:22 +0530535 */
536struct nss_ctx_instance *nss_ipsec_get_context(void)
537{
538 return &nss_top_main.nss[nss_top_main.ipsec_handler_id];
539}
540EXPORT_SYMBOL(nss_ipsec_get_context);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530541
542/*
Vijay Vigneshanb809f422018-04-04 15:04:35 +0530543 * nss_ipsec_ppe_port_config()
544 * Configure PPE port for IPsec inline
545 */
546bool nss_ipsec_ppe_port_config(struct nss_ctx_instance *nss_ctx, struct net_device *dev,
547 uint32_t if_num, uint32_t vsi_num)
548{
549#ifdef NSS_PPE_SUPPORTED
550 if_num = NSS_INTERFACE_NUM_APPEND_COREID(nss_ctx, if_num);
551
552 if (nss_ppe_tx_ipsec_config_msg(if_num, vsi_num, dev->mtu) != NSS_TX_SUCCESS) {
553 nss_warning("%p: Failed to configure PPE IPsec port", nss_ctx);
554 return false;
555 }
556
557 return true;
558#else
559 return false;
560#endif
561}
562EXPORT_SYMBOL(nss_ipsec_ppe_port_config);
563
564/*
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530565 * nss_ipsec_register_handler()
566 */
567void nss_ipsec_register_handler()
568{
569 struct nss_ctx_instance *nss_ctx = &nss_top_main.nss[nss_top_main.ipsec_handler_id];
570
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530571 BUILD_BUG_ON(NSS_IPSEC_ENCAP_INTERFACE_NUM < 0);
572 BUILD_BUG_ON(NSS_IPSEC_DECAP_INTERFACE_NUM < 0);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530573
Vijay Vigneshanb809f422018-04-04 15:04:35 +0530574 sema_init(&nss_ipsec.sem, 1);
575 init_completion(&nss_ipsec.complete);
576 atomic_set(&nss_ipsec.resp, NSS_IPSEC_ERROR_TYPE_NONE);
577
Sourav Poddarbf8b50c2017-02-15 12:44:44 +0530578 nss_ctx->nss_top->ipsec_encap_callback = NULL;
579 nss_ctx->nss_top->ipsec_decap_callback = NULL;
580
581 nss_ctx->nss_top->ipsec_encap_ctx = NULL;
582 nss_ctx->nss_top->ipsec_decap_ctx = NULL;
583
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700584 nss_core_register_handler(nss_ctx, NSS_IPSEC_ENCAP_INTERFACE_NUM, nss_ipsec_msg_handler, NULL);
585 nss_core_register_handler(nss_ctx, NSS_IPSEC_DECAP_INTERFACE_NUM, nss_ipsec_msg_handler, NULL);
Samarjeet Banerjeed99d9d02014-04-08 18:51:00 +0530586}
587
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700588/*
589 * nss_ipsec_msg_init()
590 * Initialize ipsec message.
591 */
592void 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 -0800593 nss_ipsec_msg_callback_t cb, void *app_data)
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700594{
595 nss_cmn_msg_init(&nim->cm, if_num, type, len, (void *)cb, app_data);
596}
597EXPORT_SYMBOL(nss_ipsec_msg_init);