ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame^] | 3 | * Copyright (c) 2017-2018, The Linux Foundation. All rights reserved. |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +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 | #include "nss_tx_rx_common.h" |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 18 | #include "nss_gre_stats.h" |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 19 | |
| 20 | #define NSS_GRE_TX_TIMEOUT 3000 /* 3 Seconds */ |
| 21 | |
| 22 | /* |
| 23 | * Private data structure |
| 24 | */ |
| 25 | static struct { |
| 26 | struct semaphore sem; |
| 27 | struct completion complete; |
| 28 | int response; |
| 29 | void *cb; |
| 30 | void *app_data; |
| 31 | } nss_gre_pvt; |
| 32 | |
ratheesh kannoth | 7746db2 | 2017-05-16 14:16:43 +0530 | [diff] [blame] | 33 | static atomic64_t pkt_cb_addr = ATOMIC64_INIT(0); |
| 34 | |
| 35 | /* |
| 36 | * nss_gre_rx_handler() |
| 37 | * GRE rx handler. |
| 38 | */ |
| 39 | static void nss_gre_rx_handler(struct net_device *dev, struct sk_buff *skb, |
| 40 | __attribute__((unused)) struct napi_struct *napi) |
| 41 | { |
| 42 | nss_gre_data_callback_t cb; |
| 43 | |
| 44 | nss_gre_pkt_callback_t scb = (nss_gre_pkt_callback_t)(unsigned long)atomic64_read(&pkt_cb_addr); |
| 45 | if (unlikely(scb)) { |
| 46 | struct nss_gre_info *info = (struct nss_gre_info *)netdev_priv(dev); |
| 47 | if (likely(info->next_dev)) { |
| 48 | scb(info->next_dev, skb); |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | cb = nss_top_main.gre_data_callback; |
| 53 | cb(dev, skb, 0); |
| 54 | } |
| 55 | |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 56 | /* |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 57 | * nss_gre_msg_handler() |
| 58 | * Handle NSS -> HLOS messages for GRE |
| 59 | */ |
| 60 | static void nss_gre_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data) |
| 61 | { |
| 62 | struct nss_gre_msg *ntm = (struct nss_gre_msg *)ncm; |
| 63 | void *ctx; |
| 64 | |
| 65 | nss_gre_msg_callback_t cb; |
| 66 | |
| 67 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 68 | BUG_ON(!(nss_is_dynamic_interface(ncm->interface) || ncm->interface == NSS_GRE_INTERFACE)); |
| 69 | |
| 70 | /* |
| 71 | * Is this a valid request/response packet? |
| 72 | */ |
| 73 | if (ncm->type >= NSS_GRE_MSG_MAX) { |
| 74 | nss_warning("%p: received invalid message %d for GRE STD interface", nss_ctx, ncm->type); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_gre_msg)) { |
| 79 | nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface); |
| 80 | return; |
| 81 | } |
| 82 | |
| 83 | switch (ntm->cm.type) { |
| 84 | case NSS_GRE_MSG_SESSION_STATS: |
| 85 | /* |
| 86 | * debug stats embedded in stats msg |
| 87 | */ |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 88 | nss_gre_stats_session_debug_sync(nss_ctx, &ntm->msg.sstats, ncm->interface); |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 89 | break; |
| 90 | |
| 91 | case NSS_GRE_MSG_BASE_STATS: |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 92 | nss_gre_stats_base_debug_sync(nss_ctx, &ntm->msg.bstats); |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 93 | break; |
| 94 | |
| 95 | default: |
| 96 | break; |
| 97 | |
| 98 | } |
| 99 | |
| 100 | /* |
| 101 | * Update the callback and app_data for NOTIFY messages, gre sends all notify messages |
| 102 | * to the same callback/app_data. |
| 103 | */ |
| 104 | if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) { |
| 105 | ncm->cb = (nss_ptr_t)nss_ctx->nss_top->gre_msg_callback; |
| 106 | ncm->app_data = (nss_ptr_t)nss_ctx->subsys_dp_register[ncm->interface].app_data; |
| 107 | } |
| 108 | |
| 109 | /* |
| 110 | * Log failures |
| 111 | */ |
| 112 | nss_core_log_msg_failures(nss_ctx, ncm); |
| 113 | |
| 114 | /* |
| 115 | * callback |
| 116 | */ |
| 117 | cb = (nss_gre_msg_callback_t)ncm->cb; |
| 118 | ctx = (void *)ncm->app_data; |
| 119 | |
| 120 | /* |
| 121 | * call gre-std callback |
| 122 | */ |
| 123 | if (!cb) { |
| 124 | nss_warning("%p: No callback for gre-std interface %d", |
| 125 | nss_ctx, ncm->interface); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | cb(ctx, ntm); |
| 130 | } |
| 131 | |
| 132 | /* |
| 133 | * nss_gre_callback() |
| 134 | * Callback to handle the completion of HLOS-->NSS messages. |
| 135 | */ |
| 136 | static void nss_gre_callback(void *app_data, struct nss_gre_msg *nim) |
| 137 | { |
| 138 | nss_gre_msg_callback_t callback = (nss_gre_msg_callback_t)nss_gre_pvt.cb; |
| 139 | void *data = nss_gre_pvt.app_data; |
| 140 | |
| 141 | nss_gre_pvt.cb = NULL; |
| 142 | nss_gre_pvt.app_data = NULL; |
| 143 | |
| 144 | if (nim->cm.response != NSS_CMN_RESPONSE_ACK) { |
| 145 | nss_warning("gre Error response %d\n", nim->cm.response); |
| 146 | nss_gre_pvt.response = NSS_TX_FAILURE; |
| 147 | } else { |
| 148 | nss_gre_pvt.response = NSS_TX_SUCCESS; |
| 149 | } |
| 150 | |
| 151 | if (callback) { |
| 152 | callback(data, nim); |
| 153 | } |
| 154 | |
| 155 | complete(&nss_gre_pvt.complete); |
| 156 | } |
| 157 | |
| 158 | /* |
ratheesh kannoth | 7746db2 | 2017-05-16 14:16:43 +0530 | [diff] [blame] | 159 | * nss_gre_register_pkt_callback() |
| 160 | * Register for data callback. |
| 161 | */ |
| 162 | void nss_gre_register_pkt_callback(nss_gre_pkt_callback_t cb) |
| 163 | { |
| 164 | atomic64_set(&pkt_cb_addr, (unsigned long)cb); |
| 165 | } |
| 166 | EXPORT_SYMBOL(nss_gre_register_pkt_callback); |
| 167 | |
| 168 | /* |
| 169 | * nss_gre_unregister_pkt_callback() |
| 170 | * Unregister for data callback. |
| 171 | */ |
| 172 | void nss_gre_unregister_pkt_callback() |
| 173 | { |
| 174 | atomic64_set(&pkt_cb_addr, 0); |
| 175 | } |
| 176 | EXPORT_SYMBOL(nss_gre_unregister_pkt_callback); |
| 177 | |
| 178 | /* |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 179 | * nss_gre_tx_msg() |
| 180 | * Transmit a GRE message to NSS firmware |
| 181 | */ |
| 182 | nss_tx_status_t nss_gre_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_gre_msg *msg) |
| 183 | { |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 184 | struct nss_cmn_msg *ncm = &msg->cm; |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 185 | |
| 186 | /* |
| 187 | * Sanity check the message |
| 188 | */ |
| 189 | if (!nss_is_dynamic_interface(ncm->interface)) { |
| 190 | nss_warning("%p: tx request for non dynamic interface: %d", nss_ctx, ncm->interface); |
| 191 | return NSS_TX_FAILURE; |
| 192 | } |
| 193 | |
| 194 | if (ncm->type > NSS_GRE_MSG_MAX) { |
| 195 | nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type); |
| 196 | return NSS_TX_FAILURE; |
| 197 | } |
| 198 | |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame^] | 199 | return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE); |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 200 | } |
| 201 | EXPORT_SYMBOL(nss_gre_tx_msg); |
| 202 | |
| 203 | /* |
| 204 | * nss_gre_tx_msg_sync() |
| 205 | * Transmit a GRE message to NSS firmware synchronously. |
| 206 | */ |
| 207 | nss_tx_status_t nss_gre_tx_msg_sync(struct nss_ctx_instance *nss_ctx, struct nss_gre_msg *msg) |
| 208 | { |
| 209 | nss_tx_status_t status; |
| 210 | int ret = 0; |
| 211 | |
| 212 | down(&nss_gre_pvt.sem); |
| 213 | nss_gre_pvt.cb = (void *)msg->cm.cb; |
| 214 | nss_gre_pvt.app_data = (void *)msg->cm.app_data; |
| 215 | |
| 216 | msg->cm.cb = (nss_ptr_t)nss_gre_callback; |
| 217 | msg->cm.app_data = (nss_ptr_t)NULL; |
| 218 | |
| 219 | status = nss_gre_tx_msg(nss_ctx, msg); |
| 220 | if (status != NSS_TX_SUCCESS) { |
| 221 | nss_warning("%p: gre_tx_msg failed\n", nss_ctx); |
| 222 | up(&nss_gre_pvt.sem); |
| 223 | return status; |
| 224 | } |
| 225 | ret = wait_for_completion_timeout(&nss_gre_pvt.complete, msecs_to_jiffies(NSS_GRE_TX_TIMEOUT)); |
| 226 | |
| 227 | if (!ret) { |
| 228 | nss_warning("%p: GRE STD tx sync failed due to timeout\n", nss_ctx); |
| 229 | nss_gre_pvt.response = NSS_TX_FAILURE; |
| 230 | } |
| 231 | |
| 232 | status = nss_gre_pvt.response; |
| 233 | up(&nss_gre_pvt.sem); |
| 234 | return status; |
| 235 | } |
| 236 | EXPORT_SYMBOL(nss_gre_tx_msg_sync); |
| 237 | |
| 238 | /* |
| 239 | * nss_gre_tx_buf() |
| 240 | * Send packet to GRE interface owned by NSS |
| 241 | */ |
| 242 | nss_tx_status_t nss_gre_tx_buf(struct nss_ctx_instance *nss_ctx, uint32_t if_num, struct sk_buff *skb) |
| 243 | { |
Stephen Wang | 3e2dbd1 | 2018-03-14 17:28:17 -0700 | [diff] [blame^] | 244 | return nss_core_send_packet(nss_ctx, skb, if_num, H2N_BIT_FLAG_VIRTUAL_BUFFER); |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 245 | } |
| 246 | EXPORT_SYMBOL(nss_gre_tx_buf); |
| 247 | |
| 248 | /* |
| 249 | *********************************** |
| 250 | * Register/Unregister/Miscellaneous APIs |
| 251 | *********************************** |
| 252 | */ |
| 253 | |
| 254 | /* |
| 255 | * nss_gre_register_if() |
| 256 | * Register data and message handlers for GRE. |
| 257 | */ |
ratheesh kannoth | 7746db2 | 2017-05-16 14:16:43 +0530 | [diff] [blame] | 258 | struct nss_ctx_instance *nss_gre_register_if(uint32_t if_num, nss_gre_data_callback_t data_callback, |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 259 | nss_gre_msg_callback_t event_callback, struct net_device *netdev, uint32_t features) |
| 260 | { |
| 261 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.gre_handler_id]; |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 262 | |
| 263 | nss_assert(nss_ctx); |
| 264 | nss_assert(nss_is_dynamic_interface(if_num)); |
| 265 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 266 | nss_core_register_subsys_dp(nss_ctx, if_num, nss_gre_rx_handler, NULL, netdev, netdev, features); |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 267 | |
| 268 | nss_top_main.gre_msg_callback = event_callback; |
ratheesh kannoth | 7746db2 | 2017-05-16 14:16:43 +0530 | [diff] [blame] | 269 | nss_top_main.gre_data_callback = data_callback; |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 270 | |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 271 | nss_core_register_handler(nss_ctx, if_num, nss_gre_msg_handler, NULL); |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 272 | |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 273 | nss_gre_stats_session_register(if_num, netdev); |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 274 | |
| 275 | return nss_ctx; |
| 276 | } |
| 277 | EXPORT_SYMBOL(nss_gre_register_if); |
| 278 | |
| 279 | /* |
| 280 | * nss_gre_unregister_if() |
| 281 | * Unregister data and message handler. |
| 282 | */ |
| 283 | void nss_gre_unregister_if(uint32_t if_num) |
| 284 | { |
| 285 | struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.gre_handler_id]; |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 286 | |
| 287 | nss_assert(nss_ctx); |
| 288 | nss_assert(nss_is_dynamic_interface(if_num)); |
| 289 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 290 | nss_core_unregister_subsys_dp(nss_ctx, if_num); |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 291 | |
| 292 | nss_top_main.gre_msg_callback = NULL; |
| 293 | |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 294 | nss_core_unregister_handler(nss_ctx, if_num); |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 295 | |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 296 | nss_gre_stats_session_unregister(if_num); |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 297 | } |
| 298 | EXPORT_SYMBOL(nss_gre_unregister_if); |
| 299 | |
| 300 | /* |
| 301 | * nss_get_gre_context() |
| 302 | */ |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 303 | struct nss_ctx_instance *nss_gre_get_context(void) |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 304 | { |
| 305 | return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.gre_handler_id]; |
| 306 | } |
| 307 | EXPORT_SYMBOL(nss_gre_get_context); |
| 308 | |
| 309 | /* |
| 310 | * nss_gre_msg_init() |
| 311 | * Initialize nss_gre msg. |
| 312 | */ |
| 313 | void nss_gre_msg_init(struct nss_gre_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data) |
| 314 | { |
| 315 | nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data); |
| 316 | } |
| 317 | EXPORT_SYMBOL(nss_gre_msg_init); |
| 318 | |
| 319 | /* |
| 320 | * nss_gre_register_handler() |
| 321 | * debugfs stats msg handler received on static gre interface |
| 322 | */ |
| 323 | void nss_gre_register_handler(void) |
| 324 | { |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 325 | struct nss_ctx_instance *nss_ctx = nss_gre_get_context(); |
| 326 | |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 327 | nss_info("nss_gre_register_handler"); |
| 328 | sema_init(&nss_gre_pvt.sem, 1); |
| 329 | init_completion(&nss_gre_pvt.complete); |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 330 | nss_core_register_handler(nss_ctx, NSS_GRE_INTERFACE, nss_gre_msg_handler, NULL); |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 331 | nss_gre_stats_dentry_create(); |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 332 | } |