Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 3 | * Copyright (c) 2013,2015-2018, The Linux Foundation. All rights reserved. |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 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 | |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 31 | /* |
| 32 | * nss_crypto_set_msg_callback() |
| 33 | * this sets the message callback handler and its associated context |
| 34 | */ |
| 35 | static 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 | */ |
| 47 | static 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 Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 56 | * nss_crypto_msg_handler() |
| 57 | * this handles all the IPsec events and responses |
| 58 | */ |
| 59 | static 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 Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 69 | nss_warning("%p: rx message type out of range: %d", nss_ctx, ncm->type); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 70 | return; |
| 71 | } |
| 72 | |
Suruchi Agarwal | ef8a870 | 2016-01-08 12:40:08 -0800 | [diff] [blame] | 73 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_crypto_msg)) { |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 74 | nss_warning("%p: rx message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm)); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 75 | return; |
| 76 | } |
| 77 | |
| 78 | if (ncm->interface != NSS_CRYPTO_INTERFACE) { |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 79 | nss_warning("%p: rx message request for another interface: %d", nss_ctx, ncm->interface); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 80 | return; |
| 81 | } |
| 82 | |
| 83 | if (ncm->response == NSS_CMN_RESPONSE_LAST) { |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 84 | nss_warning("%p: rx message response for if %d, type %d, is invalid: %d", nss_ctx, ncm->interface, |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 85 | ncm->type, ncm->response); |
| 86 | return; |
| 87 | } |
| 88 | |
| 89 | if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) { |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 90 | ncm->cb = (nss_ptr_t)nss_crypto_get_msg_callback(nss_ctx, &crypto_ctx); |
| 91 | ncm->app_data = (nss_ptr_t)crypto_ctx; |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 92 | } |
| 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 Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 102 | nss_trace("%p: rx handler has been unregistered for i/f: %d", nss_ctx, ncm->interface); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 103 | 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 | */ |
| 117 | nss_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 Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 120 | |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 121 | nss_info("%p: tx message %d for if %d\n", nss_ctx, ncm->type, ncm->interface); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 122 | |
Tanmay V Jagdale | f6b2bce | 2017-03-03 14:31:07 +0530 | [diff] [blame] | 123 | BUILD_BUG_ON(NSS_NBUF_PAYLOAD_SIZE < sizeof(struct nss_crypto_msg)); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 124 | |
| 125 | if (ncm->interface != NSS_CRYPTO_INTERFACE) { |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 126 | nss_warning("%p: tx message request for another interface: %d", nss_ctx, ncm->interface); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 127 | } |
| 128 | |
| 129 | if (ncm->type > NSS_CRYPTO_MSG_TYPE_MAX) { |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 130 | nss_warning("%p: tx message type out of range: %d", nss_ctx, ncm->type); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 131 | return NSS_TX_FAILURE; |
| 132 | } |
| 133 | |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 134 | 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 Banerjee | 7bce8c5 | 2014-05-02 15:32:13 +0530 | [diff] [blame] | 136 | |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 137 | return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | /* |
| 141 | * nss_crypto_tx_data() |
| 142 | * NSS crypto TX data API. Sends a crypto buffer to NSS. |
| 143 | */ |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 144 | nss_tx_status_t nss_crypto_tx_buf(struct nss_ctx_instance *nss_ctx, uint32_t if_num, struct sk_buff *skb) |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 145 | { |
| 146 | int32_t status; |
| 147 | |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 148 | nss_trace("%p: tx_data buf=%p", nss_ctx, skb); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 149 | |
| 150 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 151 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 152 | nss_warning("%p: tx_data packet dropped as core not ready", nss_ctx); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 153 | return NSS_TX_FAILURE_NOT_READY; |
| 154 | } |
| 155 | |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 156 | status = nss_core_send_buffer(nss_ctx, if_num, skb, NSS_IF_H2N_DATA_QUEUE, H2N_BUFFER_PACKET, 0); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 157 | if (unlikely(status != NSS_CORE_STATUS_SUCCESS)) { |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 158 | nss_warning("%p: tx_data Unable to enqueue packet", nss_ctx); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 159 | 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 Wang | 90c67de | 2016-04-26 15:15:59 -0700 | [diff] [blame] | 169 | nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 170 | |
| 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 | */ |
| 186 | struct 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 | */ |
| 201 | void 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 Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 210 | struct 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 Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 212 | { |
| 213 | struct nss_ctx_instance *nss_ctx; |
| 214 | |
| 215 | nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id]; |
| 216 | |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 217 | 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 Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 223 | * Register subsystem, ensuring that no duplicate registrations occur. |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 224 | */ |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 225 | nss_core_register_subsys_dp(nss_ctx, if_num, cb, NULL, NULL, netdev, features); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 226 | |
| 227 | return nss_ctx; |
| 228 | } |
| 229 | |
| 230 | /* |
| 231 | * nss_crypto_data_unregister() |
| 232 | * unregister a data callback routine |
| 233 | */ |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 234 | void nss_crypto_data_unregister(struct nss_ctx_instance *nss_ctx, uint32_t if_num) |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 235 | { |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 236 | 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 Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 241 | nss_core_unregister_subsys_dp(nss_ctx, if_num); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | /* |
Samarjeet Banerjee | cdfc0bd | 2016-05-28 00:22:31 +0530 | [diff] [blame] | 245 | * nss_crypto_pm_notify_register() |
| 246 | * register a PM notify callback routine |
| 247 | */ |
| 248 | void 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 Banerjee | 69731a2 | 2016-07-21 13:04:59 +0530 | [diff] [blame] | 255 | * nss_crypto_pm_notify_unregister() |
| 256 | * unregister a PM notify callback routine |
| 257 | */ |
| 258 | void 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 Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 265 | * nss_crypto_register_handler() |
| 266 | */ |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 267 | void nss_crypto_register_handler(void) |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 268 | { |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 269 | 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 Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 272 | } |
| 273 | |
Sundarajan Srinivasan | 02e6c2b | 2014-10-06 11:51:12 -0700 | [diff] [blame] | 274 | /* |
| 275 | * nss_crypto_msg_init() |
| 276 | * Initialize crypto message |
| 277 | */ |
| 278 | void nss_crypto_msg_init(struct nss_crypto_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, |
Sundarajan Srinivasan | 30a53d4 | 2015-01-30 10:52:08 -0800 | [diff] [blame] | 279 | nss_crypto_msg_callback_t cb, void *app_data) |
Sundarajan Srinivasan | 02e6c2b | 2014-10-06 11:51:12 -0700 | [diff] [blame] | 280 | { |
| 281 | nss_cmn_msg_init(&ncm->cm, if_num, type, len, (void *)cb, app_data); |
| 282 | } |
| 283 | |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 284 | EXPORT_SYMBOL(nss_crypto_notify_register); |
| 285 | EXPORT_SYMBOL(nss_crypto_notify_unregister); |
| 286 | EXPORT_SYMBOL(nss_crypto_data_register); |
| 287 | EXPORT_SYMBOL(nss_crypto_data_unregister); |
Samarjeet Banerjee | cdfc0bd | 2016-05-28 00:22:31 +0530 | [diff] [blame] | 288 | EXPORT_SYMBOL(nss_crypto_pm_notify_register); |
Samarjeet Banerjee | 69731a2 | 2016-07-21 13:04:59 +0530 | [diff] [blame] | 289 | EXPORT_SYMBOL(nss_crypto_pm_notify_unregister); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 290 | EXPORT_SYMBOL(nss_crypto_tx_msg); |
| 291 | EXPORT_SYMBOL(nss_crypto_tx_buf); |
Sundarajan Srinivasan | 02e6c2b | 2014-10-06 11:51:12 -0700 | [diff] [blame] | 292 | EXPORT_SYMBOL(nss_crypto_msg_init); |