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" |
Sachin Shashidhar | 1bc9e68 | 2018-05-30 15:36:12 -0700 | [diff] [blame] | 24 | #include "nss_crypto_log.h" |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 25 | |
| 26 | /* |
| 27 | ********************************** |
| 28 | General APIs |
| 29 | ********************************** |
| 30 | */ |
| 31 | |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 32 | /* |
| 33 | * nss_crypto_set_msg_callback() |
| 34 | * this sets the message callback handler and its associated context |
| 35 | */ |
| 36 | static inline void nss_crypto_set_msg_callback(struct nss_ctx_instance *nss_ctx, nss_crypto_msg_callback_t cb, void *crypto_ctx) |
| 37 | { |
| 38 | struct nss_top_instance *nss_top = nss_ctx->nss_top; |
| 39 | |
| 40 | nss_top->crypto_ctx = crypto_ctx; |
| 41 | nss_top->crypto_msg_callback = cb; |
| 42 | } |
| 43 | |
| 44 | /* |
| 45 | * nss_crypto_get_msg_callback() |
| 46 | * this gets the message callback handler and its associated context |
| 47 | */ |
| 48 | static inline nss_crypto_msg_callback_t nss_crypto_get_msg_callback(struct nss_ctx_instance *nss_ctx, void **crypto_ctx) |
| 49 | { |
| 50 | struct nss_top_instance *nss_top = nss_ctx->nss_top; |
| 51 | |
| 52 | *crypto_ctx = nss_top->crypto_ctx; |
| 53 | return nss_top->crypto_msg_callback; |
| 54 | } |
| 55 | |
| 56 | /* |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 57 | * nss_crypto_msg_handler() |
| 58 | * this handles all the IPsec events and responses |
| 59 | */ |
| 60 | static void nss_crypto_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, void *app_data __attribute((unused))) |
| 61 | { |
| 62 | struct nss_crypto_msg *nim = (struct nss_crypto_msg *)ncm; |
| 63 | nss_crypto_msg_callback_t cb = NULL; |
| 64 | void *crypto_ctx = NULL; |
| 65 | |
| 66 | /* |
| 67 | * Sanity check the message type |
| 68 | */ |
| 69 | if (ncm->type > NSS_CRYPTO_MSG_TYPE_MAX) { |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 70 | 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] | 71 | return; |
| 72 | } |
| 73 | |
Suruchi Agarwal | ef8a870 | 2016-01-08 12:40:08 -0800 | [diff] [blame] | 74 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_crypto_msg)) { |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 75 | 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] | 76 | return; |
| 77 | } |
| 78 | |
| 79 | if (ncm->interface != NSS_CRYPTO_INTERFACE) { |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 80 | 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] | 81 | return; |
| 82 | } |
| 83 | |
| 84 | if (ncm->response == NSS_CMN_RESPONSE_LAST) { |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 85 | 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] | 86 | ncm->type, ncm->response); |
| 87 | return; |
| 88 | } |
| 89 | |
Suruchi Agarwal | e4ad24a | 2018-06-11 12:03:46 +0530 | [diff] [blame] | 90 | if (ncm->response == NSS_CMN_RESPONSE_NOTIFY) { |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 91 | ncm->cb = (nss_ptr_t)nss_crypto_get_msg_callback(nss_ctx, &crypto_ctx); |
| 92 | ncm->app_data = (nss_ptr_t)crypto_ctx; |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | |
| 96 | nss_core_log_msg_failures(nss_ctx, ncm); |
| 97 | |
| 98 | /* |
Sachin Shashidhar | 1bc9e68 | 2018-05-30 15:36:12 -0700 | [diff] [blame] | 99 | * Trace messages. |
| 100 | */ |
| 101 | nss_crypto_log_rx_msg(nim); |
| 102 | |
| 103 | /* |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 104 | * Load, Test & call |
| 105 | */ |
| 106 | cb = (nss_crypto_msg_callback_t)ncm->cb; |
| 107 | if (unlikely(!cb)) { |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 108 | 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] | 109 | return; |
| 110 | } |
| 111 | cb((void *)ncm->app_data, nim); |
| 112 | } |
| 113 | /* |
| 114 | ********************************** |
| 115 | Tx APIs |
| 116 | ********************************** |
| 117 | */ |
| 118 | |
| 119 | /* |
| 120 | * nss_crypto_tx_msg |
| 121 | * Send crypto config to NSS. |
| 122 | */ |
| 123 | nss_tx_status_t nss_crypto_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_crypto_msg *msg) |
| 124 | { |
| 125 | struct nss_cmn_msg *ncm = &msg->cm; |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 126 | |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 127 | 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] | 128 | |
Tanmay V Jagdale | f6b2bce | 2017-03-03 14:31:07 +0530 | [diff] [blame] | 129 | BUILD_BUG_ON(NSS_NBUF_PAYLOAD_SIZE < sizeof(struct nss_crypto_msg)); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 130 | |
| 131 | if (ncm->interface != NSS_CRYPTO_INTERFACE) { |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 132 | 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] | 133 | } |
| 134 | |
| 135 | if (ncm->type > NSS_CRYPTO_MSG_TYPE_MAX) { |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 136 | 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] | 137 | return NSS_TX_FAILURE; |
| 138 | } |
| 139 | |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 140 | nss_info("msg params version:%d, interface:%d, type:%d, cb:%p, app_data:%p, len:%d\n", |
| 141 | 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] | 142 | |
Sachin Shashidhar | 1bc9e68 | 2018-05-30 15:36:12 -0700 | [diff] [blame] | 143 | /* |
| 144 | * Trace messages. |
| 145 | */ |
| 146 | nss_crypto_log_tx_msg(msg); |
| 147 | |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 148 | 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] | 149 | } |
| 150 | |
| 151 | /* |
| 152 | * nss_crypto_tx_data() |
| 153 | * NSS crypto TX data API. Sends a crypto buffer to NSS. |
| 154 | */ |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 155 | 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] | 156 | { |
| 157 | int32_t status; |
| 158 | |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 159 | nss_trace("%p: tx_data buf=%p", nss_ctx, skb); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 160 | |
| 161 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 162 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 163 | 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] | 164 | return NSS_TX_FAILURE_NOT_READY; |
| 165 | } |
| 166 | |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame] | 167 | 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] | 168 | if (unlikely(status != NSS_CORE_STATUS_SUCCESS)) { |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 169 | nss_warning("%p: tx_data Unable to enqueue packet", nss_ctx); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 170 | if (status == NSS_CORE_STATUS_FAILURE_QUEUE) { |
| 171 | return NSS_TX_FAILURE_QUEUE; |
| 172 | } |
| 173 | |
| 174 | return NSS_TX_FAILURE; |
| 175 | } |
| 176 | |
| 177 | /* |
| 178 | * Kick the NSS awake so it can process our new entry. |
| 179 | */ |
Stephen Wang | 90c67de | 2016-04-26 15:15:59 -0700 | [diff] [blame] | 180 | nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 181 | |
| 182 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CRYPTO_REQ]); |
| 183 | |
| 184 | return NSS_TX_SUCCESS; |
| 185 | } |
| 186 | |
| 187 | /* |
| 188 | ********************************** |
| 189 | Register APIs |
| 190 | ********************************** |
| 191 | */ |
| 192 | |
| 193 | /* |
| 194 | * nss_crypto_notify_register() |
| 195 | * register message notifier for crypto interface |
| 196 | */ |
| 197 | struct nss_ctx_instance *nss_crypto_notify_register(nss_crypto_msg_callback_t cb, void *app_data) |
| 198 | { |
| 199 | struct nss_ctx_instance *nss_ctx; |
| 200 | |
| 201 | nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id]; |
| 202 | |
| 203 | nss_crypto_set_msg_callback(nss_ctx, cb, app_data); |
| 204 | |
| 205 | return nss_ctx; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * nss_crypto_notify_unregister() |
| 210 | * unregister message notifier for crypto interface |
| 211 | */ |
| 212 | void nss_crypto_notify_unregister(struct nss_ctx_instance *nss_ctx) |
| 213 | { |
| 214 | nss_crypto_set_msg_callback(nss_ctx, NULL, NULL); |
| 215 | } |
| 216 | |
| 217 | /* |
| 218 | * nss_crypto_data_register() |
| 219 | * register a data callback routine |
| 220 | */ |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 221 | struct nss_ctx_instance *nss_crypto_data_register(uint32_t if_num, nss_crypto_buf_callback_t cb, |
| 222 | struct net_device *netdev, uint32_t features) |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 223 | { |
| 224 | struct nss_ctx_instance *nss_ctx; |
| 225 | |
| 226 | nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id]; |
| 227 | |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 228 | if ((if_num >= NSS_MAX_NET_INTERFACES) && (if_num < NSS_MAX_PHYSICAL_INTERFACES)) { |
| 229 | nss_warning("%p: data register received for invalid interface %d", nss_ctx, if_num); |
| 230 | return NULL; |
| 231 | } |
| 232 | |
| 233 | /* |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 234 | * Register subsystem, ensuring that no duplicate registrations occur. |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 235 | */ |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 236 | 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] | 237 | |
| 238 | return nss_ctx; |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * nss_crypto_data_unregister() |
| 243 | * unregister a data callback routine |
| 244 | */ |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 245 | 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] | 246 | { |
Sourav Poddar | 9883806 | 2017-01-08 16:52:21 +0530 | [diff] [blame] | 247 | if ((if_num >= NSS_MAX_NET_INTERFACES) && (if_num < NSS_MAX_PHYSICAL_INTERFACES)) { |
| 248 | nss_warning("%p: data unregister received for invalid interface %d", nss_ctx, if_num); |
| 249 | return; |
| 250 | } |
| 251 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 252 | nss_core_unregister_subsys_dp(nss_ctx, if_num); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* |
Samarjeet Banerjee | cdfc0bd | 2016-05-28 00:22:31 +0530 | [diff] [blame] | 256 | * nss_crypto_pm_notify_register() |
| 257 | * register a PM notify callback routine |
| 258 | */ |
| 259 | void nss_crypto_pm_notify_register(nss_crypto_pm_event_callback_t cb, void *app_data) |
| 260 | { |
| 261 | nss_top_main.crypto_pm_ctx = app_data; |
| 262 | nss_top_main.crypto_pm_callback = cb; |
| 263 | } |
| 264 | |
| 265 | /* |
Samarjeet Banerjee | 69731a2 | 2016-07-21 13:04:59 +0530 | [diff] [blame] | 266 | * nss_crypto_pm_notify_unregister() |
| 267 | * unregister a PM notify callback routine |
| 268 | */ |
| 269 | void nss_crypto_pm_notify_unregister(void) |
| 270 | { |
| 271 | nss_top_main.crypto_pm_ctx = NULL; |
| 272 | nss_top_main.crypto_pm_callback = NULL; |
| 273 | } |
| 274 | |
| 275 | /* |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 276 | * nss_crypto_register_handler() |
| 277 | */ |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 278 | void nss_crypto_register_handler(void) |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 279 | { |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 280 | struct nss_ctx_instance *nss_ctx = &nss_top_main.nss[nss_top_main.crypto_handler_id]; |
| 281 | |
| 282 | 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] | 283 | } |
| 284 | |
Sundarajan Srinivasan | 02e6c2b | 2014-10-06 11:51:12 -0700 | [diff] [blame] | 285 | /* |
| 286 | * nss_crypto_msg_init() |
| 287 | * Initialize crypto message |
| 288 | */ |
| 289 | 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] | 290 | nss_crypto_msg_callback_t cb, void *app_data) |
Sundarajan Srinivasan | 02e6c2b | 2014-10-06 11:51:12 -0700 | [diff] [blame] | 291 | { |
| 292 | nss_cmn_msg_init(&ncm->cm, if_num, type, len, (void *)cb, app_data); |
| 293 | } |
| 294 | |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 295 | EXPORT_SYMBOL(nss_crypto_notify_register); |
| 296 | EXPORT_SYMBOL(nss_crypto_notify_unregister); |
| 297 | EXPORT_SYMBOL(nss_crypto_data_register); |
| 298 | EXPORT_SYMBOL(nss_crypto_data_unregister); |
Samarjeet Banerjee | cdfc0bd | 2016-05-28 00:22:31 +0530 | [diff] [blame] | 299 | EXPORT_SYMBOL(nss_crypto_pm_notify_register); |
Samarjeet Banerjee | 69731a2 | 2016-07-21 13:04:59 +0530 | [diff] [blame] | 300 | EXPORT_SYMBOL(nss_crypto_pm_notify_unregister); |
Samarjeet Banerjee | de86b80 | 2014-04-10 02:59:49 +0530 | [diff] [blame] | 301 | EXPORT_SYMBOL(nss_crypto_tx_msg); |
| 302 | EXPORT_SYMBOL(nss_crypto_tx_buf); |
Sundarajan Srinivasan | 02e6c2b | 2014-10-06 11:51:12 -0700 | [diff] [blame] | 303 | EXPORT_SYMBOL(nss_crypto_msg_init); |