Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 1 | /* |
| 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 Sezgin | ea1a435 | 2014-04-15 19:09:51 -0700 | [diff] [blame] | 23 | #include "nss_crypto.h" |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 24 | |
| 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 | */ |
| 39 | static 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 | */ |
| 51 | static 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 | */ |
| 69 | void 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)) { |
| 76 | nss_crypto_trace("%p: rx data handler has been unregistered for i/f: %d", nss_ctx, ncm->interface); |
| 77 | 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 | */ |
| 86 | static 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 | */ |
| 144 | nss_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 | |
| 189 | nim = (struct nss_crypto_msg *)skb_put(nbuf, sizeof(struct nss_crypto_msg)); |
| 190 | memcpy(nim, msg, sizeof(struct nss_crypto_msg)); |
| 191 | |
| 192 | status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0); |
| 193 | if (status != NSS_CORE_STATUS_SUCCESS) { |
| 194 | dev_kfree_skb_any(nbuf); |
| 195 | nss_crypto_warning("%p: Unable to enqueue message\n", nss_ctx); |
| 196 | return NSS_TX_FAILURE; |
| 197 | } |
| 198 | |
| 199 | nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_CMD_QUEUE].desc_ring.int_bit, |
| 200 | NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE); |
| 201 | |
| 202 | return NSS_TX_SUCCESS; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * nss_crypto_tx_data() |
| 207 | * NSS crypto TX data API. Sends a crypto buffer to NSS. |
| 208 | */ |
| 209 | nss_tx_status_t nss_crypto_tx_buf(struct nss_ctx_instance *nss_ctx, void *buf, uint32_t buf_paddr, uint16_t len) |
| 210 | { |
| 211 | int32_t status; |
| 212 | |
| 213 | nss_crypto_trace("%p: tx_data buf=%p", nss_ctx, buf); |
| 214 | |
| 215 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 216 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
| 217 | nss_crypto_warning("%p: tx_data packet dropped as core not ready", nss_ctx); |
| 218 | return NSS_TX_FAILURE_NOT_READY; |
| 219 | } |
| 220 | |
| 221 | status = nss_core_send_crypto(nss_ctx, buf, buf_paddr, len); |
| 222 | if (unlikely(status != NSS_CORE_STATUS_SUCCESS)) { |
| 223 | nss_crypto_warning("%p: tx_data Unable to enqueue packet", nss_ctx); |
| 224 | if (status == NSS_CORE_STATUS_FAILURE_QUEUE) { |
| 225 | return NSS_TX_FAILURE_QUEUE; |
| 226 | } |
| 227 | |
| 228 | return NSS_TX_FAILURE; |
| 229 | } |
| 230 | |
| 231 | /* |
| 232 | * Kick the NSS awake so it can process our new entry. |
| 233 | */ |
| 234 | nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_DATA_QUEUE].desc_ring.int_bit, |
| 235 | NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE); |
| 236 | |
| 237 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CRYPTO_REQ]); |
| 238 | |
| 239 | return NSS_TX_SUCCESS; |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | ********************************** |
| 244 | Register APIs |
| 245 | ********************************** |
| 246 | */ |
| 247 | |
| 248 | /* |
| 249 | * nss_crypto_notify_register() |
| 250 | * register message notifier for crypto interface |
| 251 | */ |
| 252 | struct nss_ctx_instance *nss_crypto_notify_register(nss_crypto_msg_callback_t cb, void *app_data) |
| 253 | { |
| 254 | struct nss_ctx_instance *nss_ctx; |
| 255 | |
| 256 | nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id]; |
| 257 | |
| 258 | nss_crypto_set_msg_callback(nss_ctx, cb, app_data); |
| 259 | |
| 260 | return nss_ctx; |
| 261 | } |
| 262 | |
| 263 | /* |
| 264 | * nss_crypto_notify_unregister() |
| 265 | * unregister message notifier for crypto interface |
| 266 | */ |
| 267 | void nss_crypto_notify_unregister(struct nss_ctx_instance *nss_ctx) |
| 268 | { |
| 269 | nss_crypto_set_msg_callback(nss_ctx, NULL, NULL); |
| 270 | } |
| 271 | |
| 272 | /* |
| 273 | * nss_crypto_data_register() |
| 274 | * register a data callback routine |
| 275 | */ |
| 276 | struct nss_ctx_instance *nss_crypto_data_register(nss_crypto_buf_callback_t cb, void *app_data) |
| 277 | { |
| 278 | struct nss_ctx_instance *nss_ctx; |
| 279 | |
| 280 | nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id]; |
| 281 | |
| 282 | nss_ctx->nss_top->crypto_ctx = app_data; |
| 283 | nss_ctx->nss_top->crypto_buf_callback = cb; |
| 284 | |
| 285 | return nss_ctx; |
| 286 | } |
| 287 | |
| 288 | /* |
| 289 | * nss_crypto_data_unregister() |
| 290 | * unregister a data callback routine |
| 291 | */ |
| 292 | void nss_crypto_data_unregister(struct nss_ctx_instance *nss_ctx) |
| 293 | { |
| 294 | nss_ctx->nss_top->crypto_ctx = NULL; |
| 295 | nss_ctx->nss_top->crypto_buf_callback = NULL; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * nss_crypto_register_handler() |
| 300 | */ |
| 301 | void nss_crypto_register_handler() |
| 302 | { |
| 303 | nss_core_register_handler(NSS_CRYPTO_INTERFACE, nss_crypto_msg_handler, NULL); |
| 304 | } |
| 305 | |
| 306 | EXPORT_SYMBOL(nss_crypto_notify_register); |
| 307 | EXPORT_SYMBOL(nss_crypto_notify_unregister); |
| 308 | EXPORT_SYMBOL(nss_crypto_data_register); |
| 309 | EXPORT_SYMBOL(nss_crypto_data_unregister); |
| 310 | EXPORT_SYMBOL(nss_crypto_tx_msg); |
| 311 | EXPORT_SYMBOL(nss_crypto_tx_buf); |