blob: cc1808a57c1c3e2c27c1070e1b02e7e2606ce930 [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
Tanmay V Jagdalef6b2bce2017-03-03 14:31:07 +0530132 BUILD_BUG_ON(NSS_NBUF_PAYLOAD_SIZE < sizeof(struct nss_crypto_msg));
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530133
134 if (ncm->interface != NSS_CRYPTO_INTERFACE) {
Sourav Poddar98838062017-01-08 16:52:21 +0530135 nss_warning("%p: tx message request for another interface: %d", nss_ctx, ncm->interface);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530136 }
137
138 if (ncm->type > NSS_CRYPTO_MSG_TYPE_MAX) {
Sourav Poddar98838062017-01-08 16:52:21 +0530139 nss_warning("%p: tx message type out of range: %d", nss_ctx, ncm->type);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530140 return NSS_TX_FAILURE;
141 }
142
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800143 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_crypto_msg)) {
Sourav Poddar98838062017-01-08 16:52:21 +0530144 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 +0530145 return NSS_TX_FAILURE_BAD_PARAM;
146 }
147
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530148 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530149 if (unlikely(!nbuf)) {
Sundarajan Srinivasan62fee7e2015-01-22 11:13:10 -0800150 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 +0530151 nss_warning("%p: tx config dropped as command allocation failed", nss_ctx);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530152 return NSS_TX_FAILURE;
153 }
154
Stephen Wangaed46332016-12-12 17:29:03 -0800155 nss_info("msg params version:%d, interface:%d, type:%d, cb:%p, app_data:%p, len:%d\n",
156 ncm->version, ncm->interface, ncm->type, (void *)ncm->cb, (void *)ncm->app_data, ncm->len);
Samarjeet Banerjee7bce8c52014-05-02 15:32:13 +0530157
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530158 nim = (struct nss_crypto_msg *)skb_put(nbuf, sizeof(struct nss_crypto_msg));
159 memcpy(nim, msg, sizeof(struct nss_crypto_msg));
160
161 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
162 if (status != NSS_CORE_STATUS_SUCCESS) {
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530163 dev_kfree_skb_any(nbuf);
Sourav Poddar98838062017-01-08 16:52:21 +0530164 nss_warning("%p: Unable to enqueue message\n", nss_ctx);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530165 return NSS_TX_FAILURE;
166 }
167
Stephen Wang90c67de2016-04-26 15:15:59 -0700168 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530169
170 return NSS_TX_SUCCESS;
171}
172
173/*
174 * nss_crypto_tx_data()
175 * NSS crypto TX data API. Sends a crypto buffer to NSS.
176 */
Sourav Poddar98838062017-01-08 16:52:21 +0530177nss_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 +0530178{
179 int32_t status;
180
Sourav Poddar98838062017-01-08 16:52:21 +0530181 nss_trace("%p: tx_data buf=%p", nss_ctx, skb);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530182
183 NSS_VERIFY_CTX_MAGIC(nss_ctx);
184 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
Sourav Poddar98838062017-01-08 16:52:21 +0530185 nss_warning("%p: tx_data packet dropped as core not ready", nss_ctx);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530186 return NSS_TX_FAILURE_NOT_READY;
187 }
188
Sourav Poddar98838062017-01-08 16:52:21 +0530189 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 +0530190 if (unlikely(status != NSS_CORE_STATUS_SUCCESS)) {
Sourav Poddar98838062017-01-08 16:52:21 +0530191 nss_warning("%p: tx_data Unable to enqueue packet", nss_ctx);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530192 if (status == NSS_CORE_STATUS_FAILURE_QUEUE) {
193 return NSS_TX_FAILURE_QUEUE;
194 }
195
196 return NSS_TX_FAILURE;
197 }
198
199 /*
200 * Kick the NSS awake so it can process our new entry.
201 */
Stephen Wang90c67de2016-04-26 15:15:59 -0700202 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530203
204 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CRYPTO_REQ]);
205
206 return NSS_TX_SUCCESS;
207}
208
209/*
210 **********************************
211 Register APIs
212 **********************************
213 */
214
215/*
216 * nss_crypto_notify_register()
217 * register message notifier for crypto interface
218 */
219struct nss_ctx_instance *nss_crypto_notify_register(nss_crypto_msg_callback_t cb, void *app_data)
220{
221 struct nss_ctx_instance *nss_ctx;
222
223 nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id];
224
225 nss_crypto_set_msg_callback(nss_ctx, cb, app_data);
226
227 return nss_ctx;
228}
229
230/*
231 * nss_crypto_notify_unregister()
232 * unregister message notifier for crypto interface
233 */
234void nss_crypto_notify_unregister(struct nss_ctx_instance *nss_ctx)
235{
236 nss_crypto_set_msg_callback(nss_ctx, NULL, NULL);
237}
238
239/*
240 * nss_crypto_data_register()
241 * register a data callback routine
242 */
Sourav Poddar98838062017-01-08 16:52:21 +0530243struct nss_ctx_instance *nss_crypto_data_register(uint32_t if_num, nss_crypto_buf_callback_t cb,
244 struct net_device *netdev, uint32_t features)
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530245{
246 struct nss_ctx_instance *nss_ctx;
247
248 nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id];
249
Sourav Poddar98838062017-01-08 16:52:21 +0530250 if ((if_num >= NSS_MAX_NET_INTERFACES) && (if_num < NSS_MAX_PHYSICAL_INTERFACES)) {
251 nss_warning("%p: data register received for invalid interface %d", nss_ctx, if_num);
252 return NULL;
253 }
254
255 /*
256 * Avoid multiple data callback registration with the
257 * sama interface number
258 */
Stephen Wang84e0e992016-09-07 12:31:40 -0700259 if (nss_ctx->subsys_dp_register[if_num].cb) {
Sourav Poddar98838062017-01-08 16:52:21 +0530260 return nss_ctx;
261 }
262
Stephen Wang84e0e992016-09-07 12:31:40 -0700263 nss_ctx->subsys_dp_register[if_num].cb = cb;
264 nss_ctx->subsys_dp_register[if_num].app_data = NULL;
265 nss_ctx->subsys_dp_register[if_num].ndev = netdev;
266 nss_ctx->subsys_dp_register[if_num].features = features;
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530267
268 return nss_ctx;
269}
270
271/*
272 * nss_crypto_data_unregister()
273 * unregister a data callback routine
274 */
Sourav Poddar98838062017-01-08 16:52:21 +0530275void nss_crypto_data_unregister(struct nss_ctx_instance *nss_ctx, uint32_t if_num)
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530276{
Sourav Poddar98838062017-01-08 16:52:21 +0530277 if ((if_num >= NSS_MAX_NET_INTERFACES) && (if_num < NSS_MAX_PHYSICAL_INTERFACES)) {
278 nss_warning("%p: data unregister received for invalid interface %d", nss_ctx, if_num);
279 return;
280 }
281
282 /*
283 * if already unregistered then return
284 */
Stephen Wang84e0e992016-09-07 12:31:40 -0700285 if (!nss_ctx->subsys_dp_register[if_num].cb) {
Sourav Poddar98838062017-01-08 16:52:21 +0530286 return;
287 }
288
Stephen Wang84e0e992016-09-07 12:31:40 -0700289 nss_ctx->subsys_dp_register[if_num].cb = NULL;
290 nss_ctx->subsys_dp_register[if_num].app_data = NULL;
291 nss_ctx->subsys_dp_register[if_num].ndev = NULL;
292 nss_ctx->subsys_dp_register[if_num].features = 0;
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530293}
294
295/*
Samarjeet Banerjeecdfc0bd2016-05-28 00:22:31 +0530296 * nss_crypto_pm_notify_register()
297 * register a PM notify callback routine
298 */
299void nss_crypto_pm_notify_register(nss_crypto_pm_event_callback_t cb, void *app_data)
300{
301 nss_top_main.crypto_pm_ctx = app_data;
302 nss_top_main.crypto_pm_callback = cb;
303}
304
305/*
Samarjeet Banerjee69731a22016-07-21 13:04:59 +0530306 * nss_crypto_pm_notify_unregister()
307 * unregister a PM notify callback routine
308 */
309void nss_crypto_pm_notify_unregister(void)
310{
311 nss_top_main.crypto_pm_ctx = NULL;
312 nss_top_main.crypto_pm_callback = NULL;
313}
314
315/*
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530316 * nss_crypto_register_handler()
317 */
318void nss_crypto_register_handler()
319{
320 nss_core_register_handler(NSS_CRYPTO_INTERFACE, nss_crypto_msg_handler, NULL);
321}
322
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700323/*
324 * nss_crypto_msg_init()
325 * Initialize crypto message
326 */
327void 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 -0800328 nss_crypto_msg_callback_t cb, void *app_data)
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700329{
330 nss_cmn_msg_init(&ncm->cm, if_num, type, len, (void *)cb, app_data);
331}
332
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530333EXPORT_SYMBOL(nss_crypto_notify_register);
334EXPORT_SYMBOL(nss_crypto_notify_unregister);
335EXPORT_SYMBOL(nss_crypto_data_register);
336EXPORT_SYMBOL(nss_crypto_data_unregister);
Samarjeet Banerjeecdfc0bd2016-05-28 00:22:31 +0530337EXPORT_SYMBOL(nss_crypto_pm_notify_register);
Samarjeet Banerjee69731a22016-07-21 13:04:59 +0530338EXPORT_SYMBOL(nss_crypto_pm_notify_unregister);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530339EXPORT_SYMBOL(nss_crypto_tx_msg);
340EXPORT_SYMBOL(nss_crypto_tx_buf);
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700341EXPORT_SYMBOL(nss_crypto_msg_init);