ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
| 3 | * Copyright (c) 2017, 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 | #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 | { |
| 184 | struct nss_gre_msg *nm; |
| 185 | struct nss_cmn_msg *ncm = &msg->cm; |
| 186 | struct sk_buff *nbuf; |
| 187 | int32_t status; |
| 188 | |
| 189 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 190 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
| 191 | nss_warning("%p: gre msg dropped as core not ready", nss_ctx); |
| 192 | return NSS_TX_FAILURE_NOT_READY; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | * Sanity check the message |
| 197 | */ |
| 198 | if (!nss_is_dynamic_interface(ncm->interface)) { |
| 199 | nss_warning("%p: tx request for non dynamic interface: %d", nss_ctx, ncm->interface); |
| 200 | return NSS_TX_FAILURE; |
| 201 | } |
| 202 | |
| 203 | if (ncm->type > NSS_GRE_MSG_MAX) { |
| 204 | nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type); |
| 205 | return NSS_TX_FAILURE; |
| 206 | } |
| 207 | |
| 208 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_gre_msg)) { |
| 209 | nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm)); |
| 210 | return NSS_TX_FAILURE; |
| 211 | } |
| 212 | |
| 213 | nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE); |
| 214 | if (unlikely(!nbuf)) { |
| 215 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]); |
| 216 | nss_warning("%p: msg dropped as command allocation failed", nss_ctx); |
| 217 | return NSS_TX_FAILURE; |
| 218 | } |
| 219 | |
| 220 | /* |
| 221 | * Copy the message to our skb |
| 222 | */ |
| 223 | nm = (struct nss_gre_msg *)skb_put(nbuf, sizeof(struct nss_gre_msg)); |
| 224 | memcpy(nm, msg, sizeof(struct nss_gre_msg)); |
| 225 | |
| 226 | status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0); |
| 227 | if (status != NSS_CORE_STATUS_SUCCESS) { |
| 228 | dev_kfree_skb_any(nbuf); |
| 229 | nss_warning("%p: Unable to enqueue 'gre message'\n", nss_ctx); |
| 230 | if (status == NSS_CORE_STATUS_FAILURE_QUEUE) { |
| 231 | return NSS_TX_FAILURE_QUEUE; |
| 232 | } |
| 233 | return NSS_TX_FAILURE; |
| 234 | } |
| 235 | |
| 236 | nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE); |
| 237 | |
| 238 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]); |
| 239 | return NSS_TX_SUCCESS; |
| 240 | } |
| 241 | EXPORT_SYMBOL(nss_gre_tx_msg); |
| 242 | |
| 243 | /* |
| 244 | * nss_gre_tx_msg_sync() |
| 245 | * Transmit a GRE message to NSS firmware synchronously. |
| 246 | */ |
| 247 | nss_tx_status_t nss_gre_tx_msg_sync(struct nss_ctx_instance *nss_ctx, struct nss_gre_msg *msg) |
| 248 | { |
| 249 | nss_tx_status_t status; |
| 250 | int ret = 0; |
| 251 | |
| 252 | down(&nss_gre_pvt.sem); |
| 253 | nss_gre_pvt.cb = (void *)msg->cm.cb; |
| 254 | nss_gre_pvt.app_data = (void *)msg->cm.app_data; |
| 255 | |
| 256 | msg->cm.cb = (nss_ptr_t)nss_gre_callback; |
| 257 | msg->cm.app_data = (nss_ptr_t)NULL; |
| 258 | |
| 259 | status = nss_gre_tx_msg(nss_ctx, msg); |
| 260 | if (status != NSS_TX_SUCCESS) { |
| 261 | nss_warning("%p: gre_tx_msg failed\n", nss_ctx); |
| 262 | up(&nss_gre_pvt.sem); |
| 263 | return status; |
| 264 | } |
| 265 | ret = wait_for_completion_timeout(&nss_gre_pvt.complete, msecs_to_jiffies(NSS_GRE_TX_TIMEOUT)); |
| 266 | |
| 267 | if (!ret) { |
| 268 | nss_warning("%p: GRE STD tx sync failed due to timeout\n", nss_ctx); |
| 269 | nss_gre_pvt.response = NSS_TX_FAILURE; |
| 270 | } |
| 271 | |
| 272 | status = nss_gre_pvt.response; |
| 273 | up(&nss_gre_pvt.sem); |
| 274 | return status; |
| 275 | } |
| 276 | EXPORT_SYMBOL(nss_gre_tx_msg_sync); |
| 277 | |
| 278 | /* |
| 279 | * nss_gre_tx_buf() |
| 280 | * Send packet to GRE interface owned by NSS |
| 281 | */ |
| 282 | nss_tx_status_t nss_gre_tx_buf(struct nss_ctx_instance *nss_ctx, uint32_t if_num, struct sk_buff *skb) |
| 283 | { |
| 284 | int32_t status; |
| 285 | |
| 286 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 287 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
| 288 | nss_warning("%p: GRE std packet dropped as core not ready", nss_ctx); |
| 289 | return NSS_TX_FAILURE_NOT_READY; |
| 290 | } |
| 291 | |
| 292 | status = nss_core_send_buffer(nss_ctx, if_num, skb, NSS_IF_DATA_QUEUE_0, H2N_BUFFER_PACKET, H2N_BIT_FLAG_VIRTUAL_BUFFER); |
| 293 | if (unlikely(status != NSS_CORE_STATUS_SUCCESS)) { |
| 294 | nss_warning("%p: Unable to enqueue GRE std packet\n", nss_ctx); |
| 295 | return NSS_TX_FAILURE_QUEUE; |
| 296 | } |
| 297 | |
| 298 | /* |
| 299 | * Kick the NSS awake so it can process our new entry. |
| 300 | */ |
| 301 | nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE); |
| 302 | |
| 303 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_PACKET]); |
| 304 | return NSS_TX_SUCCESS; |
| 305 | |
| 306 | } |
| 307 | EXPORT_SYMBOL(nss_gre_tx_buf); |
| 308 | |
| 309 | /* |
| 310 | *********************************** |
| 311 | * Register/Unregister/Miscellaneous APIs |
| 312 | *********************************** |
| 313 | */ |
| 314 | |
| 315 | /* |
| 316 | * nss_gre_register_if() |
| 317 | * Register data and message handlers for GRE. |
| 318 | */ |
ratheesh kannoth | 7746db2 | 2017-05-16 14:16:43 +0530 | [diff] [blame] | 319 | 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] | 320 | nss_gre_msg_callback_t event_callback, struct net_device *netdev, uint32_t features) |
| 321 | { |
| 322 | 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] | 323 | |
| 324 | nss_assert(nss_ctx); |
| 325 | nss_assert(nss_is_dynamic_interface(if_num)); |
| 326 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 327 | 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] | 328 | |
| 329 | nss_top_main.gre_msg_callback = event_callback; |
ratheesh kannoth | 7746db2 | 2017-05-16 14:16:43 +0530 | [diff] [blame] | 330 | nss_top_main.gre_data_callback = data_callback; |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 331 | |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 332 | 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] | 333 | |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 334 | nss_gre_stats_session_register(if_num, netdev); |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 335 | |
| 336 | return nss_ctx; |
| 337 | } |
| 338 | EXPORT_SYMBOL(nss_gre_register_if); |
| 339 | |
| 340 | /* |
| 341 | * nss_gre_unregister_if() |
| 342 | * Unregister data and message handler. |
| 343 | */ |
| 344 | void nss_gre_unregister_if(uint32_t if_num) |
| 345 | { |
| 346 | 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] | 347 | |
| 348 | nss_assert(nss_ctx); |
| 349 | nss_assert(nss_is_dynamic_interface(if_num)); |
| 350 | |
Jackson Bockus | 7ca70ec | 2017-07-17 13:47:29 -0700 | [diff] [blame] | 351 | nss_core_unregister_subsys_dp(nss_ctx, if_num); |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 352 | |
| 353 | nss_top_main.gre_msg_callback = NULL; |
| 354 | |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 355 | nss_core_unregister_handler(nss_ctx, if_num); |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 356 | |
Yu Huang | 8c10708 | 2017-07-24 14:58:26 -0700 | [diff] [blame] | 357 | nss_gre_stats_session_unregister(if_num); |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 358 | } |
| 359 | EXPORT_SYMBOL(nss_gre_unregister_if); |
| 360 | |
| 361 | /* |
| 362 | * nss_get_gre_context() |
| 363 | */ |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 364 | struct nss_ctx_instance *nss_gre_get_context(void) |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 365 | { |
| 366 | return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.gre_handler_id]; |
| 367 | } |
| 368 | EXPORT_SYMBOL(nss_gre_get_context); |
| 369 | |
| 370 | /* |
| 371 | * nss_gre_msg_init() |
| 372 | * Initialize nss_gre msg. |
| 373 | */ |
| 374 | 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) |
| 375 | { |
| 376 | nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data); |
| 377 | } |
| 378 | EXPORT_SYMBOL(nss_gre_msg_init); |
| 379 | |
| 380 | /* |
| 381 | * nss_gre_register_handler() |
| 382 | * debugfs stats msg handler received on static gre interface |
| 383 | */ |
| 384 | void nss_gre_register_handler(void) |
| 385 | { |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 386 | struct nss_ctx_instance *nss_ctx = nss_gre_get_context(); |
| 387 | |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 388 | nss_info("nss_gre_register_handler"); |
| 389 | sema_init(&nss_gre_pvt.sem, 1); |
| 390 | init_completion(&nss_gre_pvt.complete); |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 391 | 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] | 392 | nss_gre_stats_dentry_create(); |
ratheesh kannoth | eb2a0a8 | 2017-05-04 09:20:17 +0530 | [diff] [blame] | 393 | } |