blob: 015a0dea7d392a1a6a383a6fb9359cc35f82117b [file] [log] [blame]
Samarjeet Banerjeede86b802014-04-10 02:59:49 +05301/*
2 **************************************************************************
Sundarajan Srinivasan30a53d42015-01-30 10:52:08 -08003 * Copyright (c) 2013,2015, 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
31#define nss_crypto_warning(fmt, arg...) nss_warning("Crypto:"fmt, ##arg)
32#define nss_crypto_info(fmt, arg...) nss_info("Crypto:"fmt, ##arg)
33#define nss_crypto_trace(fmt, arg...) nss_trace("Crypto:"fmt, ##arg)
34
35/*
36 * nss_crypto_set_msg_callback()
37 * this sets the message callback handler and its associated context
38 */
39static inline void nss_crypto_set_msg_callback(struct nss_ctx_instance *nss_ctx, nss_crypto_msg_callback_t cb, void *crypto_ctx)
40{
41 struct nss_top_instance *nss_top = nss_ctx->nss_top;
42
43 nss_top->crypto_ctx = crypto_ctx;
44 nss_top->crypto_msg_callback = cb;
45}
46
47/*
48 * nss_crypto_get_msg_callback()
49 * this gets the message callback handler and its associated context
50 */
51static inline nss_crypto_msg_callback_t nss_crypto_get_msg_callback(struct nss_ctx_instance *nss_ctx, void **crypto_ctx)
52{
53 struct nss_top_instance *nss_top = nss_ctx->nss_top;
54
55 *crypto_ctx = nss_top->crypto_ctx;
56 return nss_top->crypto_msg_callback;
57}
58
59/*
60 **********************************
61 Rx APIs
62 **********************************
63 */
64
65/*
66 * nss_crypto_buf_handler()
67 * RX packet handler for crypto buf, note the crypto buf is special
68 */
69void nss_crypto_buf_handler(struct nss_ctx_instance *nss_ctx, void *buf, uint32_t paddr, uint16_t len)
70{
71 struct nss_top_instance *nss_top = nss_ctx->nss_top;
72 void *app_data = nss_top->crypto_ctx;
73 nss_crypto_buf_callback_t cb = nss_top->crypto_buf_callback;
74
75 if (unlikely(!cb)) {
Samarjeet Banerjee7bce8c52014-05-02 15:32:13 +053076 nss_crypto_trace("%p: rx data handler has been unregistered for i/f", nss_ctx);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +053077 return;
78 }
79
80 cb(app_data, buf, paddr, len);
81}
82/*
83 * nss_crypto_msg_handler()
84 * this handles all the IPsec events and responses
85 */
86static void nss_crypto_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, void *app_data __attribute((unused)))
87{
88 struct nss_crypto_msg *nim = (struct nss_crypto_msg *)ncm;
89 nss_crypto_msg_callback_t cb = NULL;
90 void *crypto_ctx = NULL;
91
92 /*
93 * Sanity check the message type
94 */
95 if (ncm->type > NSS_CRYPTO_MSG_TYPE_MAX) {
96 nss_crypto_warning("%p: rx message type out of range: %d", nss_ctx, ncm->type);
97 return;
98 }
99
100 if (ncm->len > sizeof(struct nss_crypto_msg)) {
101 nss_crypto_warning("%p: rx request for another interface: %d", nss_ctx, ncm->interface);
102 return;
103 }
104
105 if (ncm->interface != NSS_CRYPTO_INTERFACE) {
106 nss_crypto_warning("%p: rx message request for another interface: %d", nss_ctx, ncm->interface);
107 return;
108 }
109
110 if (ncm->response == NSS_CMN_RESPONSE_LAST) {
111 nss_crypto_warning("%p: rx message response for if %d, type %d, is invalid: %d", nss_ctx, ncm->interface,
112 ncm->type, ncm->response);
113 return;
114 }
115
116 if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) {
117 ncm->cb = (uint32_t)nss_crypto_get_msg_callback(nss_ctx, &crypto_ctx);
118 ncm->app_data = (uint32_t)crypto_ctx;
119 }
120
121
122 nss_core_log_msg_failures(nss_ctx, ncm);
123
124 /*
125 * Load, Test & call
126 */
127 cb = (nss_crypto_msg_callback_t)ncm->cb;
128 if (unlikely(!cb)) {
129 nss_crypto_trace("%p: rx handler has been unregistered for i/f: %d", nss_ctx, ncm->interface);
130 return;
131 }
132 cb((void *)ncm->app_data, nim);
133}
134/*
135 **********************************
136 Tx APIs
137 **********************************
138 */
139
140/*
141 * nss_crypto_tx_msg
142 * Send crypto config to NSS.
143 */
144nss_tx_status_t nss_crypto_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_crypto_msg *msg)
145{
146 struct nss_cmn_msg *ncm = &msg->cm;
147 struct nss_crypto_msg *nim;
148 struct sk_buff *nbuf;
149 int32_t status;
150
151 nss_crypto_info("%p: tx message %d for if %d\n", nss_ctx, ncm->type, ncm->interface);
152
153 NSS_VERIFY_CTX_MAGIC(nss_ctx);
154 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
155 nss_crypto_warning("%p: tx message dropped as core not ready", nss_ctx);
156 return NSS_TX_FAILURE_NOT_READY;
157 }
158
159
160 if (NSS_NBUF_PAYLOAD_SIZE < sizeof(struct nss_crypto_msg)) {
161 nss_crypto_warning("%p: tx message request is too large: %d (desired), %d (requested)", nss_ctx,
162 NSS_NBUF_PAYLOAD_SIZE, sizeof(struct nss_crypto_msg));
163 return NSS_TX_FAILURE_TOO_LARGE;
164 }
165
166 if (ncm->interface != NSS_CRYPTO_INTERFACE) {
167 nss_crypto_warning("%p: tx message request for another interface: %d", nss_ctx, ncm->interface);
168 }
169
170 if (ncm->type > NSS_CRYPTO_MSG_TYPE_MAX) {
171 nss_crypto_warning("%p: tx message type out of range: %d", nss_ctx, ncm->type);
172 return NSS_TX_FAILURE;
173 }
174
175 if (ncm->len > sizeof(struct nss_crypto_msg)) {
176 nss_crypto_warning("%p: tx message request len for if %d, is bad: %d", nss_ctx, ncm->interface, ncm->len);
177 return NSS_TX_FAILURE_BAD_PARAM;
178 }
179
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530180 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530181 if (unlikely(!nbuf)) {
Sundarajan Srinivasan62fee7e2015-01-22 11:13:10 -0800182 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530183 nss_crypto_warning("%p: tx config dropped as command allocation failed", nss_ctx);
184 return NSS_TX_FAILURE;
185 }
186
Samarjeet Banerjee7bce8c52014-05-02 15:32:13 +0530187 nss_crypto_info("msg params version:%d, interface:%d, type:%d, cb:%d, app_data:%d, len:%d\n",
188 ncm->version, ncm->interface, ncm->type, ncm->cb, ncm->app_data, ncm->len);
189
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530190 nim = (struct nss_crypto_msg *)skb_put(nbuf, sizeof(struct nss_crypto_msg));
191 memcpy(nim, msg, sizeof(struct nss_crypto_msg));
192
193 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
194 if (status != NSS_CORE_STATUS_SUCCESS) {
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530195 dev_kfree_skb_any(nbuf);
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530196 nss_crypto_warning("%p: Unable to enqueue message\n", nss_ctx);
197 return NSS_TX_FAILURE;
198 }
199
200 nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_CMD_QUEUE].desc_ring.int_bit,
201 NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE);
202
203 return NSS_TX_SUCCESS;
204}
205
206/*
207 * nss_crypto_tx_data()
208 * NSS crypto TX data API. Sends a crypto buffer to NSS.
209 */
210nss_tx_status_t nss_crypto_tx_buf(struct nss_ctx_instance *nss_ctx, void *buf, uint32_t buf_paddr, uint16_t len)
211{
212 int32_t status;
213
214 nss_crypto_trace("%p: tx_data buf=%p", nss_ctx, buf);
215
216 NSS_VERIFY_CTX_MAGIC(nss_ctx);
217 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
218 nss_crypto_warning("%p: tx_data packet dropped as core not ready", nss_ctx);
219 return NSS_TX_FAILURE_NOT_READY;
220 }
221
222 status = nss_core_send_crypto(nss_ctx, buf, buf_paddr, len);
223 if (unlikely(status != NSS_CORE_STATUS_SUCCESS)) {
224 nss_crypto_warning("%p: tx_data Unable to enqueue packet", nss_ctx);
225 if (status == NSS_CORE_STATUS_FAILURE_QUEUE) {
226 return NSS_TX_FAILURE_QUEUE;
227 }
228
229 return NSS_TX_FAILURE;
230 }
231
232 /*
233 * Kick the NSS awake so it can process our new entry.
234 */
Pamidipati, Vijayefcc4692014-05-09 14:47:38 +0530235 nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_DATA_QUEUE_0].desc_ring.int_bit,
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530236 NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE);
237
238 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CRYPTO_REQ]);
239
240 return NSS_TX_SUCCESS;
241}
242
243/*
244 **********************************
245 Register APIs
246 **********************************
247 */
248
249/*
250 * nss_crypto_notify_register()
251 * register message notifier for crypto interface
252 */
253struct nss_ctx_instance *nss_crypto_notify_register(nss_crypto_msg_callback_t cb, void *app_data)
254{
255 struct nss_ctx_instance *nss_ctx;
256
257 nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id];
258
259 nss_crypto_set_msg_callback(nss_ctx, cb, app_data);
260
261 return nss_ctx;
262}
263
264/*
265 * nss_crypto_notify_unregister()
266 * unregister message notifier for crypto interface
267 */
268void nss_crypto_notify_unregister(struct nss_ctx_instance *nss_ctx)
269{
270 nss_crypto_set_msg_callback(nss_ctx, NULL, NULL);
271}
272
273/*
274 * nss_crypto_data_register()
275 * register a data callback routine
276 */
277struct nss_ctx_instance *nss_crypto_data_register(nss_crypto_buf_callback_t cb, void *app_data)
278{
279 struct nss_ctx_instance *nss_ctx;
280
281 nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id];
282
283 nss_ctx->nss_top->crypto_ctx = app_data;
284 nss_ctx->nss_top->crypto_buf_callback = cb;
285
286 return nss_ctx;
287}
288
289/*
290 * nss_crypto_data_unregister()
291 * unregister a data callback routine
292 */
293void nss_crypto_data_unregister(struct nss_ctx_instance *nss_ctx)
294{
295 nss_ctx->nss_top->crypto_ctx = NULL;
296 nss_ctx->nss_top->crypto_buf_callback = NULL;
297}
298
299/*
300 * nss_crypto_register_handler()
301 */
302void nss_crypto_register_handler()
303{
304 nss_core_register_handler(NSS_CRYPTO_INTERFACE, nss_crypto_msg_handler, NULL);
305}
306
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700307/*
308 * nss_crypto_msg_init()
309 * Initialize crypto message
310 */
311void 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 -0800312 nss_crypto_msg_callback_t cb, void *app_data)
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700313{
314 nss_cmn_msg_init(&ncm->cm, if_num, type, len, (void *)cb, app_data);
315}
316
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530317EXPORT_SYMBOL(nss_crypto_notify_register);
318EXPORT_SYMBOL(nss_crypto_notify_unregister);
319EXPORT_SYMBOL(nss_crypto_data_register);
320EXPORT_SYMBOL(nss_crypto_data_unregister);
321EXPORT_SYMBOL(nss_crypto_tx_msg);
322EXPORT_SYMBOL(nss_crypto_tx_buf);
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700323EXPORT_SYMBOL(nss_crypto_msg_init);