blob: 664c5d64085bc3faeb49034f3214c08dd01351f6 [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"
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;
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530120
Sourav Poddar98838062017-01-08 16:52:21 +0530121 nss_info("%p: tx message %d for if %d\n", nss_ctx, ncm->type, ncm->interface);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530122
Tanmay V Jagdalef6b2bce2017-03-03 14:31:07 +0530123 BUILD_BUG_ON(NSS_NBUF_PAYLOAD_SIZE < sizeof(struct nss_crypto_msg));
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530124
125 if (ncm->interface != NSS_CRYPTO_INTERFACE) {
Sourav Poddar98838062017-01-08 16:52:21 +0530126 nss_warning("%p: tx message request for another interface: %d", nss_ctx, ncm->interface);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530127 }
128
129 if (ncm->type > NSS_CRYPTO_MSG_TYPE_MAX) {
Sourav Poddar98838062017-01-08 16:52:21 +0530130 nss_warning("%p: tx message type out of range: %d", nss_ctx, ncm->type);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530131 return NSS_TX_FAILURE;
132 }
133
Stephen Wangaed46332016-12-12 17:29:03 -0800134 nss_info("msg params version:%d, interface:%d, type:%d, cb:%p, app_data:%p, len:%d\n",
135 ncm->version, ncm->interface, ncm->type, (void *)ncm->cb, (void *)ncm->app_data, ncm->len);
Samarjeet Banerjee7bce8c52014-05-02 15:32:13 +0530136
Stephen Wang3e2dbd12018-03-14 17:28:17 -0700137 return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530138}
139
140/*
141 * nss_crypto_tx_data()
142 * NSS crypto TX data API. Sends a crypto buffer to NSS.
143 */
Sourav Poddar98838062017-01-08 16:52:21 +0530144nss_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 +0530145{
146 int32_t status;
147
Sourav Poddar98838062017-01-08 16:52:21 +0530148 nss_trace("%p: tx_data buf=%p", nss_ctx, skb);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530149
150 NSS_VERIFY_CTX_MAGIC(nss_ctx);
151 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
Sourav Poddar98838062017-01-08 16:52:21 +0530152 nss_warning("%p: tx_data packet dropped as core not ready", nss_ctx);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530153 return NSS_TX_FAILURE_NOT_READY;
154 }
155
Stephen Wang3e2dbd12018-03-14 17:28:17 -0700156 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 +0530157 if (unlikely(status != NSS_CORE_STATUS_SUCCESS)) {
Sourav Poddar98838062017-01-08 16:52:21 +0530158 nss_warning("%p: tx_data Unable to enqueue packet", nss_ctx);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530159 if (status == NSS_CORE_STATUS_FAILURE_QUEUE) {
160 return NSS_TX_FAILURE_QUEUE;
161 }
162
163 return NSS_TX_FAILURE;
164 }
165
166 /*
167 * Kick the NSS awake so it can process our new entry.
168 */
Stephen Wang90c67de2016-04-26 15:15:59 -0700169 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530170
171 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CRYPTO_REQ]);
172
173 return NSS_TX_SUCCESS;
174}
175
176/*
177 **********************************
178 Register APIs
179 **********************************
180 */
181
182/*
183 * nss_crypto_notify_register()
184 * register message notifier for crypto interface
185 */
186struct nss_ctx_instance *nss_crypto_notify_register(nss_crypto_msg_callback_t cb, void *app_data)
187{
188 struct nss_ctx_instance *nss_ctx;
189
190 nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id];
191
192 nss_crypto_set_msg_callback(nss_ctx, cb, app_data);
193
194 return nss_ctx;
195}
196
197/*
198 * nss_crypto_notify_unregister()
199 * unregister message notifier for crypto interface
200 */
201void nss_crypto_notify_unregister(struct nss_ctx_instance *nss_ctx)
202{
203 nss_crypto_set_msg_callback(nss_ctx, NULL, NULL);
204}
205
206/*
207 * nss_crypto_data_register()
208 * register a data callback routine
209 */
Sourav Poddar98838062017-01-08 16:52:21 +0530210struct nss_ctx_instance *nss_crypto_data_register(uint32_t if_num, nss_crypto_buf_callback_t cb,
211 struct net_device *netdev, uint32_t features)
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530212{
213 struct nss_ctx_instance *nss_ctx;
214
215 nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id];
216
Sourav Poddar98838062017-01-08 16:52:21 +0530217 if ((if_num >= NSS_MAX_NET_INTERFACES) && (if_num < NSS_MAX_PHYSICAL_INTERFACES)) {
218 nss_warning("%p: data register received for invalid interface %d", nss_ctx, if_num);
219 return NULL;
220 }
221
222 /*
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700223 * Register subsystem, ensuring that no duplicate registrations occur.
Sourav Poddar98838062017-01-08 16:52:21 +0530224 */
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700225 nss_core_register_subsys_dp(nss_ctx, if_num, cb, NULL, NULL, netdev, features);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530226
227 return nss_ctx;
228}
229
230/*
231 * nss_crypto_data_unregister()
232 * unregister a data callback routine
233 */
Sourav Poddar98838062017-01-08 16:52:21 +0530234void nss_crypto_data_unregister(struct nss_ctx_instance *nss_ctx, uint32_t if_num)
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530235{
Sourav Poddar98838062017-01-08 16:52:21 +0530236 if ((if_num >= NSS_MAX_NET_INTERFACES) && (if_num < NSS_MAX_PHYSICAL_INTERFACES)) {
237 nss_warning("%p: data unregister received for invalid interface %d", nss_ctx, if_num);
238 return;
239 }
240
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700241 nss_core_unregister_subsys_dp(nss_ctx, if_num);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530242}
243
244/*
Samarjeet Banerjeecdfc0bd2016-05-28 00:22:31 +0530245 * nss_crypto_pm_notify_register()
246 * register a PM notify callback routine
247 */
248void nss_crypto_pm_notify_register(nss_crypto_pm_event_callback_t cb, void *app_data)
249{
250 nss_top_main.crypto_pm_ctx = app_data;
251 nss_top_main.crypto_pm_callback = cb;
252}
253
254/*
Samarjeet Banerjee69731a22016-07-21 13:04:59 +0530255 * nss_crypto_pm_notify_unregister()
256 * unregister a PM notify callback routine
257 */
258void nss_crypto_pm_notify_unregister(void)
259{
260 nss_top_main.crypto_pm_ctx = NULL;
261 nss_top_main.crypto_pm_callback = NULL;
262}
263
264/*
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530265 * nss_crypto_register_handler()
266 */
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700267void nss_crypto_register_handler(void)
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530268{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700269 struct nss_ctx_instance *nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id];
270
271 nss_core_register_handler(nss_ctx, NSS_CRYPTO_INTERFACE, nss_crypto_msg_handler, NULL);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530272}
273
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700274/*
275 * nss_crypto_msg_init()
276 * Initialize crypto message
277 */
278void 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 -0800279 nss_crypto_msg_callback_t cb, void *app_data)
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700280{
281 nss_cmn_msg_init(&ncm->cm, if_num, type, len, (void *)cb, app_data);
282}
283
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530284EXPORT_SYMBOL(nss_crypto_notify_register);
285EXPORT_SYMBOL(nss_crypto_notify_unregister);
286EXPORT_SYMBOL(nss_crypto_data_register);
287EXPORT_SYMBOL(nss_crypto_data_unregister);
Samarjeet Banerjeecdfc0bd2016-05-28 00:22:31 +0530288EXPORT_SYMBOL(nss_crypto_pm_notify_register);
Samarjeet Banerjee69731a22016-07-21 13:04:59 +0530289EXPORT_SYMBOL(nss_crypto_pm_notify_unregister);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530290EXPORT_SYMBOL(nss_crypto_tx_msg);
291EXPORT_SYMBOL(nss_crypto_tx_buf);
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700292EXPORT_SYMBOL(nss_crypto_msg_init);