blob: fe44ba6fd959cc6ce3b8a9c346f38061af1de6cb [file] [log] [blame]
Samarjeet Banerjeede86b802014-04-10 02:59:49 +05301/*
2 **************************************************************************
3 * Copyright (c) 2013, The Linux Foundation. All rights reserved.
4 * 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
180 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
181 if (unlikely(!nbuf)) {
182 spin_lock_bh(&nss_ctx->nss_top->stats_lock);
183 nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]++;
184 spin_unlock_bh(&nss_ctx->nss_top->stats_lock);
185 nss_crypto_warning("%p: tx config dropped as command allocation failed", nss_ctx);
186 return NSS_TX_FAILURE;
187 }
188
Samarjeet Banerjee7bce8c52014-05-02 15:32:13 +0530189 nss_crypto_info("msg params version:%d, interface:%d, type:%d, cb:%d, app_data:%d, len:%d\n",
190 ncm->version, ncm->interface, ncm->type, ncm->cb, ncm->app_data, ncm->len);
191
Samarjeet Banerjeede86b802014-04-10 02:59:49 +0530192 nim = (struct nss_crypto_msg *)skb_put(nbuf, sizeof(struct nss_crypto_msg));
193 memcpy(nim, msg, sizeof(struct nss_crypto_msg));
194
195 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
196 if (status != NSS_CORE_STATUS_SUCCESS) {
197 dev_kfree_skb_any(nbuf);
198 nss_crypto_warning("%p: Unable to enqueue message\n", nss_ctx);
199 return NSS_TX_FAILURE;
200 }
201
202 nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_CMD_QUEUE].desc_ring.int_bit,
203 NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE);
204
205 return NSS_TX_SUCCESS;
206}
207
208/*
209 * nss_crypto_tx_data()
210 * NSS crypto TX data API. Sends a crypto buffer to NSS.
211 */
212nss_tx_status_t nss_crypto_tx_buf(struct nss_ctx_instance *nss_ctx, void *buf, uint32_t buf_paddr, uint16_t len)
213{
214 int32_t status;
215
216 nss_crypto_trace("%p: tx_data buf=%p", nss_ctx, buf);
217
218 NSS_VERIFY_CTX_MAGIC(nss_ctx);
219 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
220 nss_crypto_warning("%p: tx_data packet dropped as core not ready", nss_ctx);
221 return NSS_TX_FAILURE_NOT_READY;
222 }
223
224 status = nss_core_send_crypto(nss_ctx, buf, buf_paddr, len);
225 if (unlikely(status != NSS_CORE_STATUS_SUCCESS)) {
226 nss_crypto_warning("%p: tx_data Unable to enqueue packet", nss_ctx);
227 if (status == NSS_CORE_STATUS_FAILURE_QUEUE) {
228 return NSS_TX_FAILURE_QUEUE;
229 }
230
231 return NSS_TX_FAILURE;
232 }
233
234 /*
235 * Kick the NSS awake so it can process our new entry.
236 */
237 nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_DATA_QUEUE].desc_ring.int_bit,
238 NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE);
239
240 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CRYPTO_REQ]);
241
242 return NSS_TX_SUCCESS;
243}
244
245/*
246 **********************************
247 Register APIs
248 **********************************
249 */
250
251/*
252 * nss_crypto_notify_register()
253 * register message notifier for crypto interface
254 */
255struct nss_ctx_instance *nss_crypto_notify_register(nss_crypto_msg_callback_t cb, void *app_data)
256{
257 struct nss_ctx_instance *nss_ctx;
258
259 nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id];
260
261 nss_crypto_set_msg_callback(nss_ctx, cb, app_data);
262
263 return nss_ctx;
264}
265
266/*
267 * nss_crypto_notify_unregister()
268 * unregister message notifier for crypto interface
269 */
270void nss_crypto_notify_unregister(struct nss_ctx_instance *nss_ctx)
271{
272 nss_crypto_set_msg_callback(nss_ctx, NULL, NULL);
273}
274
275/*
276 * nss_crypto_data_register()
277 * register a data callback routine
278 */
279struct nss_ctx_instance *nss_crypto_data_register(nss_crypto_buf_callback_t cb, void *app_data)
280{
281 struct nss_ctx_instance *nss_ctx;
282
283 nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id];
284
285 nss_ctx->nss_top->crypto_ctx = app_data;
286 nss_ctx->nss_top->crypto_buf_callback = cb;
287
288 return nss_ctx;
289}
290
291/*
292 * nss_crypto_data_unregister()
293 * unregister a data callback routine
294 */
295void nss_crypto_data_unregister(struct nss_ctx_instance *nss_ctx)
296{
297 nss_ctx->nss_top->crypto_ctx = NULL;
298 nss_ctx->nss_top->crypto_buf_callback = NULL;
299}
300
301/*
302 * nss_crypto_register_handler()
303 */
304void nss_crypto_register_handler()
305{
306 nss_core_register_handler(NSS_CRYPTO_INTERFACE, nss_crypto_msg_handler, NULL);
307}
308
309EXPORT_SYMBOL(nss_crypto_notify_register);
310EXPORT_SYMBOL(nss_crypto_notify_unregister);
311EXPORT_SYMBOL(nss_crypto_data_register);
312EXPORT_SYMBOL(nss_crypto_data_unregister);
313EXPORT_SYMBOL(nss_crypto_tx_msg);
314EXPORT_SYMBOL(nss_crypto_tx_buf);