blob: 200deab2560ff6f15cd18cf7a930f515278f1a14 [file] [log] [blame]
Samarjeet Banerjeede86b802014-04-10 02:59:49 +05301/*
2 **************************************************************************
Sourav Poddar98838062017-01-08 16:52:21 +05303 * Copyright (c) 2013,2015-2017, 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"
Samarjeet Banerjeede86b802014-04-10 02:59:49 +053024
25/*
26 **********************************
27 General APIs
28 **********************************
29 */
30
Samarjeet Banerjeede86b802014-04-10 02:59:49 +053031/*
32 * nss_crypto_set_msg_callback()
33 * this sets the message callback handler and its associated context
34 */
35static inline void nss_crypto_set_msg_callback(struct nss_ctx_instance *nss_ctx, nss_crypto_msg_callback_t cb, void *crypto_ctx)
36{
37 struct nss_top_instance *nss_top = nss_ctx->nss_top;
38
39 nss_top->crypto_ctx = crypto_ctx;
40 nss_top->crypto_msg_callback = cb;
41}
42
43/*
44 * nss_crypto_get_msg_callback()
45 * this gets the message callback handler and its associated context
46 */
47static inline nss_crypto_msg_callback_t nss_crypto_get_msg_callback(struct nss_ctx_instance *nss_ctx, void **crypto_ctx)
48{
49 struct nss_top_instance *nss_top = nss_ctx->nss_top;
50
51 *crypto_ctx = nss_top->crypto_ctx;
52 return nss_top->crypto_msg_callback;
53}
54
55/*
Samarjeet Banerjeede86b802014-04-10 02:59:49 +053056 * nss_crypto_msg_handler()
57 * this handles all the IPsec events and responses
58 */
59static void nss_crypto_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, void *app_data __attribute((unused)))
60{
61 struct nss_crypto_msg *nim = (struct nss_crypto_msg *)ncm;
62 nss_crypto_msg_callback_t cb = NULL;
63 void *crypto_ctx = NULL;
64
65 /*
66 * Sanity check the message type
67 */
68 if (ncm->type > NSS_CRYPTO_MSG_TYPE_MAX) {
Sourav Poddar98838062017-01-08 16:52:21 +053069 nss_warning("%p: rx message type out of range: %d", nss_ctx, ncm->type);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +053070 return;
71 }
72
Suruchi Agarwalef8a8702016-01-08 12:40:08 -080073 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_crypto_msg)) {
Sourav Poddar98838062017-01-08 16:52:21 +053074 nss_warning("%p: rx message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Samarjeet Banerjeede86b802014-04-10 02:59:49 +053075 return;
76 }
77
78 if (ncm->interface != NSS_CRYPTO_INTERFACE) {
Sourav Poddar98838062017-01-08 16:52:21 +053079 nss_warning("%p: rx message request for another interface: %d", nss_ctx, ncm->interface);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +053080 return;
81 }
82
83 if (ncm->response == NSS_CMN_RESPONSE_LAST) {
Sourav Poddar98838062017-01-08 16:52:21 +053084 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 +053085 ncm->type, ncm->response);
86 return;
87 }
88
89 if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) {
Stephen Wangaed46332016-12-12 17:29:03 -080090 ncm->cb = (nss_ptr_t)nss_crypto_get_msg_callback(nss_ctx, &crypto_ctx);
91 ncm->app_data = (nss_ptr_t)crypto_ctx;
Samarjeet Banerjeede86b802014-04-10 02:59:49 +053092 }
93
94
95 nss_core_log_msg_failures(nss_ctx, ncm);
96
97 /*
98 * Load, Test & call
99 */
100 cb = (nss_crypto_msg_callback_t)ncm->cb;
101 if (unlikely(!cb)) {
Sourav Poddar98838062017-01-08 16:52:21 +0530102 nss_trace("%p: rx handler has been unregistered for i/f: %d", nss_ctx, ncm->interface);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530103 return;
104 }
105 cb((void *)ncm->app_data, nim);
106}
107/*
108 **********************************
109 Tx APIs
110 **********************************
111 */
112
113/*
114 * nss_crypto_tx_msg
115 * Send crypto config to NSS.
116 */
117nss_tx_status_t nss_crypto_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_crypto_msg *msg)
118{
119 struct nss_cmn_msg *ncm = &msg->cm;
120 struct nss_crypto_msg *nim;
121 struct sk_buff *nbuf;
122 int32_t status;
123
Sourav Poddar98838062017-01-08 16:52:21 +0530124 nss_info("%p: tx message %d for if %d\n", nss_ctx, ncm->type, ncm->interface);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530125
126 NSS_VERIFY_CTX_MAGIC(nss_ctx);
127 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
Sourav Poddar98838062017-01-08 16:52:21 +0530128 nss_warning("%p: tx message dropped as core not ready", nss_ctx);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530129 return NSS_TX_FAILURE_NOT_READY;
130 }
131
132
133 if (NSS_NBUF_PAYLOAD_SIZE < sizeof(struct nss_crypto_msg)) {
Sourav Poddar98838062017-01-08 16:52:21 +0530134 nss_warning("%p: tx message request is too large: %d (desired), %d (requested)", nss_ctx,
Stephen Wangaed46332016-12-12 17:29:03 -0800135 NSS_NBUF_PAYLOAD_SIZE, (int)sizeof(struct nss_crypto_msg));
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530136 return NSS_TX_FAILURE_TOO_LARGE;
137 }
138
139 if (ncm->interface != NSS_CRYPTO_INTERFACE) {
Sourav Poddar98838062017-01-08 16:52:21 +0530140 nss_warning("%p: tx message request for another interface: %d", nss_ctx, ncm->interface);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530141 }
142
143 if (ncm->type > NSS_CRYPTO_MSG_TYPE_MAX) {
Sourav Poddar98838062017-01-08 16:52:21 +0530144 nss_warning("%p: tx message type out of range: %d", nss_ctx, ncm->type);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530145 return NSS_TX_FAILURE;
146 }
147
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800148 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_crypto_msg)) {
Sourav Poddar98838062017-01-08 16:52:21 +0530149 nss_warning("%p: tx message request len for if %d, is bad: %d", nss_ctx, ncm->interface, nss_cmn_get_msg_len(ncm));
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530150 return NSS_TX_FAILURE_BAD_PARAM;
151 }
152
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530153 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530154 if (unlikely(!nbuf)) {
Sundarajan Srinivasan62fee7e2015-01-22 11:13:10 -0800155 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]);
Sourav Poddar98838062017-01-08 16:52:21 +0530156 nss_warning("%p: tx config dropped as command allocation failed", nss_ctx);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530157 return NSS_TX_FAILURE;
158 }
159
Stephen Wangaed46332016-12-12 17:29:03 -0800160 nss_info("msg params version:%d, interface:%d, type:%d, cb:%p, app_data:%p, len:%d\n",
161 ncm->version, ncm->interface, ncm->type, (void *)ncm->cb, (void *)ncm->app_data, ncm->len);
Samarjeet Banerjee7bce8c52014-05-02 15:32:13 +0530162
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530163 nim = (struct nss_crypto_msg *)skb_put(nbuf, sizeof(struct nss_crypto_msg));
164 memcpy(nim, msg, sizeof(struct nss_crypto_msg));
165
166 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
167 if (status != NSS_CORE_STATUS_SUCCESS) {
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530168 dev_kfree_skb_any(nbuf);
Sourav Poddar98838062017-01-08 16:52:21 +0530169 nss_warning("%p: Unable to enqueue message\n", nss_ctx);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530170 return NSS_TX_FAILURE;
171 }
172
Stephen Wang90c67de2016-04-26 15:15:59 -0700173 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530174
175 return NSS_TX_SUCCESS;
176}
177
178/*
179 * nss_crypto_tx_data()
180 * NSS crypto TX data API. Sends a crypto buffer to NSS.
181 */
Sourav Poddar98838062017-01-08 16:52:21 +0530182nss_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 +0530183{
184 int32_t status;
185
Sourav Poddar98838062017-01-08 16:52:21 +0530186 nss_trace("%p: tx_data buf=%p", nss_ctx, skb);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530187
188 NSS_VERIFY_CTX_MAGIC(nss_ctx);
189 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
Sourav Poddar98838062017-01-08 16:52:21 +0530190 nss_warning("%p: tx_data packet dropped as core not ready", nss_ctx);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530191 return NSS_TX_FAILURE_NOT_READY;
192 }
193
Sourav Poddar98838062017-01-08 16:52:21 +0530194 status = nss_core_send_buffer(nss_ctx, if_num, skb, NSS_IF_DATA_QUEUE_0, H2N_BUFFER_CRYPTO_REQ, 0);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530195 if (unlikely(status != NSS_CORE_STATUS_SUCCESS)) {
Sourav Poddar98838062017-01-08 16:52:21 +0530196 nss_warning("%p: tx_data Unable to enqueue packet", nss_ctx);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530197 if (status == NSS_CORE_STATUS_FAILURE_QUEUE) {
198 return NSS_TX_FAILURE_QUEUE;
199 }
200
201 return NSS_TX_FAILURE;
202 }
203
204 /*
205 * Kick the NSS awake so it can process our new entry.
206 */
Stephen Wang90c67de2016-04-26 15:15:59 -0700207 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530208
209 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CRYPTO_REQ]);
210
211 return NSS_TX_SUCCESS;
212}
213
214/*
215 **********************************
216 Register APIs
217 **********************************
218 */
219
220/*
221 * nss_crypto_notify_register()
222 * register message notifier for crypto interface
223 */
224struct nss_ctx_instance *nss_crypto_notify_register(nss_crypto_msg_callback_t cb, void *app_data)
225{
226 struct nss_ctx_instance *nss_ctx;
227
228 nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id];
229
230 nss_crypto_set_msg_callback(nss_ctx, cb, app_data);
231
232 return nss_ctx;
233}
234
235/*
236 * nss_crypto_notify_unregister()
237 * unregister message notifier for crypto interface
238 */
239void nss_crypto_notify_unregister(struct nss_ctx_instance *nss_ctx)
240{
241 nss_crypto_set_msg_callback(nss_ctx, NULL, NULL);
242}
243
244/*
245 * nss_crypto_data_register()
246 * register a data callback routine
247 */
Sourav Poddar98838062017-01-08 16:52:21 +0530248struct nss_ctx_instance *nss_crypto_data_register(uint32_t if_num, nss_crypto_buf_callback_t cb,
249 struct net_device *netdev, uint32_t features)
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530250{
251 struct nss_ctx_instance *nss_ctx;
252
253 nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id];
254
Sourav Poddar98838062017-01-08 16:52:21 +0530255 if ((if_num >= NSS_MAX_NET_INTERFACES) && (if_num < NSS_MAX_PHYSICAL_INTERFACES)) {
256 nss_warning("%p: data register received for invalid interface %d", nss_ctx, if_num);
257 return NULL;
258 }
259
260 /*
261 * Avoid multiple data callback registration with the
262 * sama interface number
263 */
Stephen Wang84e0e992016-09-07 12:31:40 -0700264 if (nss_ctx->subsys_dp_register[if_num].cb) {
Sourav Poddar98838062017-01-08 16:52:21 +0530265 return nss_ctx;
266 }
267
Stephen Wang84e0e992016-09-07 12:31:40 -0700268 nss_ctx->subsys_dp_register[if_num].cb = cb;
269 nss_ctx->subsys_dp_register[if_num].app_data = NULL;
270 nss_ctx->subsys_dp_register[if_num].ndev = netdev;
271 nss_ctx->subsys_dp_register[if_num].features = features;
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530272
273 return nss_ctx;
274}
275
276/*
277 * nss_crypto_data_unregister()
278 * unregister a data callback routine
279 */
Sourav Poddar98838062017-01-08 16:52:21 +0530280void nss_crypto_data_unregister(struct nss_ctx_instance *nss_ctx, uint32_t if_num)
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530281{
Sourav Poddar98838062017-01-08 16:52:21 +0530282 if ((if_num >= NSS_MAX_NET_INTERFACES) && (if_num < NSS_MAX_PHYSICAL_INTERFACES)) {
283 nss_warning("%p: data unregister received for invalid interface %d", nss_ctx, if_num);
284 return;
285 }
286
287 /*
288 * if already unregistered then return
289 */
Stephen Wang84e0e992016-09-07 12:31:40 -0700290 if (!nss_ctx->subsys_dp_register[if_num].cb) {
Sourav Poddar98838062017-01-08 16:52:21 +0530291 return;
292 }
293
Stephen Wang84e0e992016-09-07 12:31:40 -0700294 nss_ctx->subsys_dp_register[if_num].cb = NULL;
295 nss_ctx->subsys_dp_register[if_num].app_data = NULL;
296 nss_ctx->subsys_dp_register[if_num].ndev = NULL;
297 nss_ctx->subsys_dp_register[if_num].features = 0;
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530298}
299
300/*
Samarjeet Banerjeecdfc0bd2016-05-28 00:22:31 +0530301 * nss_crypto_pm_notify_register()
302 * register a PM notify callback routine
303 */
304void nss_crypto_pm_notify_register(nss_crypto_pm_event_callback_t cb, void *app_data)
305{
306 nss_top_main.crypto_pm_ctx = app_data;
307 nss_top_main.crypto_pm_callback = cb;
308}
309
310/*
Samarjeet Banerjee69731a22016-07-21 13:04:59 +0530311 * nss_crypto_pm_notify_unregister()
312 * unregister a PM notify callback routine
313 */
314void nss_crypto_pm_notify_unregister(void)
315{
316 nss_top_main.crypto_pm_ctx = NULL;
317 nss_top_main.crypto_pm_callback = NULL;
318}
319
320/*
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530321 * nss_crypto_register_handler()
322 */
323void nss_crypto_register_handler()
324{
325 nss_core_register_handler(NSS_CRYPTO_INTERFACE, nss_crypto_msg_handler, NULL);
326}
327
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700328/*
329 * nss_crypto_msg_init()
330 * Initialize crypto message
331 */
332void 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 -0800333 nss_crypto_msg_callback_t cb, void *app_data)
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700334{
335 nss_cmn_msg_init(&ncm->cm, if_num, type, len, (void *)cb, app_data);
336}
337
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530338EXPORT_SYMBOL(nss_crypto_notify_register);
339EXPORT_SYMBOL(nss_crypto_notify_unregister);
340EXPORT_SYMBOL(nss_crypto_data_register);
341EXPORT_SYMBOL(nss_crypto_data_unregister);
Samarjeet Banerjeecdfc0bd2016-05-28 00:22:31 +0530342EXPORT_SYMBOL(nss_crypto_pm_notify_register);
Samarjeet Banerjee69731a22016-07-21 13:04:59 +0530343EXPORT_SYMBOL(nss_crypto_pm_notify_unregister);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530344EXPORT_SYMBOL(nss_crypto_tx_msg);
345EXPORT_SYMBOL(nss_crypto_tx_buf);
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700346EXPORT_SYMBOL(nss_crypto_msg_init);