blob: 7c9fa9275eb4c25a8fbda5a1084acc017f6ff9b7 [file] [log] [blame]
Samarjeet Banerjeede86b802014-04-10 02:59:49 +05301/*
2 **************************************************************************
Stephen Wang3e2dbd12018-03-14 17:28:17 -07003 * Copyright (c) 2013,2015-2018, The Linux Foundation. All rights reserved.
Samarjeet Banerjeede86b802014-04-10 02:59:49 +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_crypto.c
19 * NSS Crypto APIs
20 */
21
22#include "nss_tx_rx_common.h"
Murat Sezginea1a4352014-04-15 19:09:51 -070023#include "nss_crypto.h"
Sachin Shashidhar1bc9e682018-05-30 15:36:12 -070024#include "nss_crypto_log.h"
Samarjeet Banerjeede86b802014-04-10 02:59:49 +053025
26/*
27 **********************************
28 General APIs
29 **********************************
30 */
31
Samarjeet Banerjeede86b802014-04-10 02:59:49 +053032/*
33 * nss_crypto_set_msg_callback()
34 * this sets the message callback handler and its associated context
35 */
36static inline void nss_crypto_set_msg_callback(struct nss_ctx_instance *nss_ctx, nss_crypto_msg_callback_t cb, void *crypto_ctx)
37{
38 struct nss_top_instance *nss_top = nss_ctx->nss_top;
39
40 nss_top->crypto_ctx = crypto_ctx;
41 nss_top->crypto_msg_callback = cb;
42}
43
44/*
45 * nss_crypto_get_msg_callback()
46 * this gets the message callback handler and its associated context
47 */
48static inline nss_crypto_msg_callback_t nss_crypto_get_msg_callback(struct nss_ctx_instance *nss_ctx, void **crypto_ctx)
49{
50 struct nss_top_instance *nss_top = nss_ctx->nss_top;
51
52 *crypto_ctx = nss_top->crypto_ctx;
53 return nss_top->crypto_msg_callback;
54}
55
56/*
Samarjeet Banerjeede86b802014-04-10 02:59:49 +053057 * nss_crypto_msg_handler()
58 * this handles all the IPsec events and responses
59 */
60static void nss_crypto_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, void *app_data __attribute((unused)))
61{
62 struct nss_crypto_msg *nim = (struct nss_crypto_msg *)ncm;
63 nss_crypto_msg_callback_t cb = NULL;
64 void *crypto_ctx = NULL;
65
66 /*
67 * Sanity check the message type
68 */
69 if (ncm->type > NSS_CRYPTO_MSG_TYPE_MAX) {
Sourav Poddar98838062017-01-08 16:52:21 +053070 nss_warning("%p: rx message type out of range: %d", nss_ctx, ncm->type);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +053071 return;
72 }
73
Suruchi Agarwalef8a8702016-01-08 12:40:08 -080074 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_crypto_msg)) {
Sourav Poddar98838062017-01-08 16:52:21 +053075 nss_warning("%p: rx message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Samarjeet Banerjeede86b802014-04-10 02:59:49 +053076 return;
77 }
78
79 if (ncm->interface != NSS_CRYPTO_INTERFACE) {
Sourav Poddar98838062017-01-08 16:52:21 +053080 nss_warning("%p: rx message request for another interface: %d", nss_ctx, ncm->interface);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +053081 return;
82 }
83
84 if (ncm->response == NSS_CMN_RESPONSE_LAST) {
Sourav Poddar98838062017-01-08 16:52:21 +053085 nss_warning("%p: rx message response for if %d, type %d, is invalid: %d", nss_ctx, ncm->interface,
Samarjeet Banerjeede86b802014-04-10 02:59:49 +053086 ncm->type, ncm->response);
87 return;
88 }
89
Suruchi Agarwale4ad24a2018-06-11 12:03:46 +053090 if (ncm->response == NSS_CMN_RESPONSE_NOTIFY) {
Stephen Wangaed46332016-12-12 17:29:03 -080091 ncm->cb = (nss_ptr_t)nss_crypto_get_msg_callback(nss_ctx, &crypto_ctx);
92 ncm->app_data = (nss_ptr_t)crypto_ctx;
Samarjeet Banerjeede86b802014-04-10 02:59:49 +053093 }
94
95
96 nss_core_log_msg_failures(nss_ctx, ncm);
97
98 /*
Sachin Shashidhar1bc9e682018-05-30 15:36:12 -070099 * Trace messages.
100 */
101 nss_crypto_log_rx_msg(nim);
102
103 /*
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530104 * Load, Test & call
105 */
106 cb = (nss_crypto_msg_callback_t)ncm->cb;
107 if (unlikely(!cb)) {
Sourav Poddar98838062017-01-08 16:52:21 +0530108 nss_trace("%p: rx handler has been unregistered for i/f: %d", nss_ctx, ncm->interface);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530109 return;
110 }
111 cb((void *)ncm->app_data, nim);
112}
113/*
114 **********************************
115 Tx APIs
116 **********************************
117 */
118
119/*
120 * nss_crypto_tx_msg
121 * Send crypto config to NSS.
122 */
123nss_tx_status_t nss_crypto_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_crypto_msg *msg)
124{
125 struct nss_cmn_msg *ncm = &msg->cm;
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530126
Sourav Poddar98838062017-01-08 16:52:21 +0530127 nss_info("%p: tx message %d for if %d\n", nss_ctx, ncm->type, ncm->interface);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530128
Tanmay V Jagdalef6b2bce2017-03-03 14:31:07 +0530129 BUILD_BUG_ON(NSS_NBUF_PAYLOAD_SIZE < sizeof(struct nss_crypto_msg));
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530130
131 if (ncm->interface != NSS_CRYPTO_INTERFACE) {
Sourav Poddar98838062017-01-08 16:52:21 +0530132 nss_warning("%p: tx message request for another interface: %d", nss_ctx, ncm->interface);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530133 }
134
135 if (ncm->type > NSS_CRYPTO_MSG_TYPE_MAX) {
Sourav Poddar98838062017-01-08 16:52:21 +0530136 nss_warning("%p: tx message type out of range: %d", nss_ctx, ncm->type);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530137 return NSS_TX_FAILURE;
138 }
139
Stephen Wangaed46332016-12-12 17:29:03 -0800140 nss_info("msg params version:%d, interface:%d, type:%d, cb:%p, app_data:%p, len:%d\n",
141 ncm->version, ncm->interface, ncm->type, (void *)ncm->cb, (void *)ncm->app_data, ncm->len);
Samarjeet Banerjee7bce8c52014-05-02 15:32:13 +0530142
Sachin Shashidhar1bc9e682018-05-30 15:36:12 -0700143 /*
144 * Trace messages.
145 */
146 nss_crypto_log_tx_msg(msg);
147
Stephen Wang3e2dbd12018-03-14 17:28:17 -0700148 return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530149}
150
151/*
152 * nss_crypto_tx_data()
153 * NSS crypto TX data API. Sends a crypto buffer to NSS.
154 */
Sourav Poddar98838062017-01-08 16:52:21 +0530155nss_tx_status_t nss_crypto_tx_buf(struct nss_ctx_instance *nss_ctx, uint32_t if_num, struct sk_buff *skb)
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530156{
157 int32_t status;
158
Sourav Poddar98838062017-01-08 16:52:21 +0530159 nss_trace("%p: tx_data buf=%p", nss_ctx, skb);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530160
161 NSS_VERIFY_CTX_MAGIC(nss_ctx);
162 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
Sourav Poddar98838062017-01-08 16:52:21 +0530163 nss_warning("%p: tx_data packet dropped as core not ready", nss_ctx);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530164 return NSS_TX_FAILURE_NOT_READY;
165 }
166
Stephen Wang3e2dbd12018-03-14 17:28:17 -0700167 status = nss_core_send_buffer(nss_ctx, if_num, skb, NSS_IF_H2N_DATA_QUEUE, H2N_BUFFER_PACKET, 0);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530168 if (unlikely(status != NSS_CORE_STATUS_SUCCESS)) {
Sourav Poddar98838062017-01-08 16:52:21 +0530169 nss_warning("%p: tx_data Unable to enqueue packet", nss_ctx);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530170 if (status == NSS_CORE_STATUS_FAILURE_QUEUE) {
171 return NSS_TX_FAILURE_QUEUE;
172 }
173
174 return NSS_TX_FAILURE;
175 }
176
177 /*
178 * Kick the NSS awake so it can process our new entry.
179 */
Stephen Wang90c67de2016-04-26 15:15:59 -0700180 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530181
182 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CRYPTO_REQ]);
183
184 return NSS_TX_SUCCESS;
185}
186
187/*
188 **********************************
189 Register APIs
190 **********************************
191 */
192
193/*
194 * nss_crypto_notify_register()
195 * register message notifier for crypto interface
196 */
197struct nss_ctx_instance *nss_crypto_notify_register(nss_crypto_msg_callback_t cb, void *app_data)
198{
199 struct nss_ctx_instance *nss_ctx;
200
201 nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id];
202
203 nss_crypto_set_msg_callback(nss_ctx, cb, app_data);
204
205 return nss_ctx;
206}
207
208/*
209 * nss_crypto_notify_unregister()
210 * unregister message notifier for crypto interface
211 */
212void nss_crypto_notify_unregister(struct nss_ctx_instance *nss_ctx)
213{
214 nss_crypto_set_msg_callback(nss_ctx, NULL, NULL);
215}
216
217/*
218 * nss_crypto_data_register()
219 * register a data callback routine
220 */
Sourav Poddar98838062017-01-08 16:52:21 +0530221struct nss_ctx_instance *nss_crypto_data_register(uint32_t if_num, nss_crypto_buf_callback_t cb,
222 struct net_device *netdev, uint32_t features)
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530223{
224 struct nss_ctx_instance *nss_ctx;
225
226 nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id];
227
Sourav Poddar98838062017-01-08 16:52:21 +0530228 if ((if_num >= NSS_MAX_NET_INTERFACES) && (if_num < NSS_MAX_PHYSICAL_INTERFACES)) {
229 nss_warning("%p: data register received for invalid interface %d", nss_ctx, if_num);
230 return NULL;
231 }
232
233 /*
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700234 * Register subsystem, ensuring that no duplicate registrations occur.
Sourav Poddar98838062017-01-08 16:52:21 +0530235 */
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700236 nss_core_register_subsys_dp(nss_ctx, if_num, cb, NULL, NULL, netdev, features);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530237
238 return nss_ctx;
239}
240
241/*
242 * nss_crypto_data_unregister()
243 * unregister a data callback routine
244 */
Sourav Poddar98838062017-01-08 16:52:21 +0530245void nss_crypto_data_unregister(struct nss_ctx_instance *nss_ctx, uint32_t if_num)
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530246{
Sourav Poddar98838062017-01-08 16:52:21 +0530247 if ((if_num >= NSS_MAX_NET_INTERFACES) && (if_num < NSS_MAX_PHYSICAL_INTERFACES)) {
248 nss_warning("%p: data unregister received for invalid interface %d", nss_ctx, if_num);
249 return;
250 }
251
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700252 nss_core_unregister_subsys_dp(nss_ctx, if_num);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530253}
254
255/*
Samarjeet Banerjeecdfc0bd2016-05-28 00:22:31 +0530256 * nss_crypto_pm_notify_register()
257 * register a PM notify callback routine
258 */
259void nss_crypto_pm_notify_register(nss_crypto_pm_event_callback_t cb, void *app_data)
260{
261 nss_top_main.crypto_pm_ctx = app_data;
262 nss_top_main.crypto_pm_callback = cb;
263}
264
265/*
Samarjeet Banerjee69731a22016-07-21 13:04:59 +0530266 * nss_crypto_pm_notify_unregister()
267 * unregister a PM notify callback routine
268 */
269void nss_crypto_pm_notify_unregister(void)
270{
271 nss_top_main.crypto_pm_ctx = NULL;
272 nss_top_main.crypto_pm_callback = NULL;
273}
274
275/*
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530276 * nss_crypto_register_handler()
277 */
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700278void nss_crypto_register_handler(void)
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530279{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700280 struct nss_ctx_instance *nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id];
281
282 nss_core_register_handler(nss_ctx, NSS_CRYPTO_INTERFACE, nss_crypto_msg_handler, NULL);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530283}
284
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700285/*
286 * nss_crypto_msg_init()
287 * Initialize crypto message
288 */
289void nss_crypto_msg_init(struct nss_crypto_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len,
Sundarajan Srinivasan30a53d42015-01-30 10:52:08 -0800290 nss_crypto_msg_callback_t cb, void *app_data)
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700291{
292 nss_cmn_msg_init(&ncm->cm, if_num, type, len, (void *)cb, app_data);
293}
294
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530295EXPORT_SYMBOL(nss_crypto_notify_register);
296EXPORT_SYMBOL(nss_crypto_notify_unregister);
297EXPORT_SYMBOL(nss_crypto_data_register);
298EXPORT_SYMBOL(nss_crypto_data_unregister);
Samarjeet Banerjeecdfc0bd2016-05-28 00:22:31 +0530299EXPORT_SYMBOL(nss_crypto_pm_notify_register);
Samarjeet Banerjee69731a22016-07-21 13:04:59 +0530300EXPORT_SYMBOL(nss_crypto_pm_notify_unregister);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530301EXPORT_SYMBOL(nss_crypto_tx_msg);
302EXPORT_SYMBOL(nss_crypto_tx_buf);
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700303EXPORT_SYMBOL(nss_crypto_msg_init);