Shyam Sunder | 66e889d | 2015-11-02 15:31:20 +0530 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Suruchi Agarwal | ef8a870 | 2016-01-08 12:40:08 -0800 | [diff] [blame] | 3 | * Copyright (c) 2015-2016, The Linux Foundation. All rights reserved. |
Shyam Sunder | 66e889d | 2015-11-02 15:31:20 +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 <net/sock.h> |
| 18 | #include "nss_tx_rx_common.h" |
| 19 | |
| 20 | /* |
| 21 | * Data structures to store pptp nss debug stats |
| 22 | */ |
| 23 | static DEFINE_SPINLOCK(nss_pptp_session_debug_stats_lock); |
| 24 | static struct nss_stats_pptp_session_debug nss_pptp_session_debug_stats[NSS_MAX_PPTP_DYNAMIC_INTERFACES]; |
| 25 | |
| 26 | /* |
| 27 | * nss_pptp_session_debug_stats_sync |
| 28 | * Per session debug stats for pptp |
| 29 | */ |
| 30 | void nss_pptp_session_debug_stats_sync(struct nss_ctx_instance *nss_ctx, struct nss_pptp_sync_session_stats_msg *stats_msg, uint16_t if_num) |
| 31 | { |
| 32 | int i; |
| 33 | spin_lock_bh(&nss_pptp_session_debug_stats_lock); |
| 34 | for (i = 0; i < NSS_MAX_PPTP_DYNAMIC_INTERFACES; i++) { |
| 35 | if (nss_pptp_session_debug_stats[i].if_num == if_num) { |
| 36 | nss_pptp_session_debug_stats[i].stats[NSS_STATS_PPTP_SESSION_RX_DROPPED] += stats_msg->rx_dropped; |
| 37 | nss_pptp_session_debug_stats[i].stats[NSS_STATS_PPTP_SESSION_TX_DROPPED] += stats_msg->tx_dropped; |
| 38 | nss_pptp_session_debug_stats[i].stats[NSS_STATS_PPTP_SESSION_RX_PPP_LCP_PKTS] += stats_msg->rx_ppp_lcp_pkts; |
| 39 | nss_pptp_session_debug_stats[i].stats[NSS_STATS_PPTP_SESSION_RX_EXP_DATA_PKTS] += stats_msg->rx_exception_data_pkts; |
| 40 | break; |
| 41 | } |
| 42 | } |
| 43 | spin_unlock_bh(&nss_pptp_session_debug_stats_lock); |
| 44 | } |
| 45 | |
| 46 | /* |
| 47 | * nss_pptp_global_session_stats_get() |
| 48 | * Get session pptp statitics. |
| 49 | */ |
| 50 | void nss_pptp_session_debug_stats_get(void *stats_mem) |
| 51 | { |
| 52 | struct nss_stats_pptp_session_debug *stats = (struct nss_stats_pptp_session_debug *)stats_mem; |
| 53 | int i; |
| 54 | |
| 55 | if (!stats) { |
| 56 | nss_warning("No memory to copy pptp session stats"); |
| 57 | return; |
| 58 | } |
| 59 | |
| 60 | spin_lock_bh(&nss_pptp_session_debug_stats_lock); |
| 61 | for (i = 0; i < NSS_MAX_PPTP_DYNAMIC_INTERFACES; i++) { |
| 62 | if (nss_pptp_session_debug_stats[i].valid) { |
| 63 | memcpy(stats, &nss_pptp_session_debug_stats[i], sizeof(struct nss_stats_pptp_session_debug)); |
| 64 | stats++; |
| 65 | } |
| 66 | } |
| 67 | spin_unlock_bh(&nss_pptp_session_debug_stats_lock); |
| 68 | } |
| 69 | |
| 70 | /* |
| 71 | * nss_pptp_handler() |
| 72 | * Handle NSS -> HLOS messages for pptp tunnel |
| 73 | */ |
| 74 | static void nss_pptp_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data) |
| 75 | { |
| 76 | struct nss_pptp_msg *ntm = (struct nss_pptp_msg *)ncm; |
| 77 | void *ctx; |
| 78 | |
| 79 | nss_pptp_msg_callback_t cb; |
| 80 | |
| 81 | BUG_ON(!(nss_is_dynamic_interface(ncm->interface) || ncm->interface == NSS_PPTP_INTERFACE)); |
| 82 | |
| 83 | /* |
| 84 | * Is this a valid request/response packet? |
| 85 | */ |
| 86 | if (ncm->type >= NSS_PPTP_MSG_MAX) { |
| 87 | nss_warning("%p: received invalid message %d for PPTP interface", nss_ctx, ncm->type); |
| 88 | return; |
| 89 | } |
| 90 | |
Suruchi Agarwal | ef8a870 | 2016-01-08 12:40:08 -0800 | [diff] [blame] | 91 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_pptp_msg)) { |
| 92 | nss_warning("%p: Length of message is greater than required: %d", nss_ctx, nss_cmn_get_msg_len(ncm)); |
Shyam Sunder | 66e889d | 2015-11-02 15:31:20 +0530 | [diff] [blame] | 93 | return; |
| 94 | } |
| 95 | |
| 96 | switch (ntm->cm.type) { |
| 97 | |
| 98 | case NSS_PPTP_MSG_SYNC_STATS: |
| 99 | /* |
| 100 | * session debug stats embeded in session stats msg |
| 101 | */ |
| 102 | nss_pptp_session_debug_stats_sync(nss_ctx, &ntm->msg.stats, ncm->interface); |
| 103 | break; |
| 104 | } |
| 105 | |
| 106 | /* |
| 107 | * Update the callback and app_data for NOTIFY messages, pptp sends all notify messages |
| 108 | * to the same callback/app_data. |
| 109 | */ |
| 110 | if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) { |
| 111 | ncm->cb = (uint32_t)nss_ctx->nss_top->pptp_msg_callback; |
| 112 | } |
| 113 | |
| 114 | /* |
| 115 | * Log failures |
| 116 | */ |
| 117 | nss_core_log_msg_failures(nss_ctx, ncm); |
| 118 | |
| 119 | /* |
| 120 | * Do we have a call back |
| 121 | */ |
| 122 | if (!ncm->cb) { |
| 123 | return; |
| 124 | } |
| 125 | |
| 126 | /* |
| 127 | * callback |
| 128 | */ |
| 129 | cb = (nss_pptp_msg_callback_t)ncm->cb; |
| 130 | ctx = nss_ctx->nss_top->subsys_dp_register[ncm->interface].ndev; |
| 131 | |
| 132 | /* |
| 133 | * call pptp tunnel callback |
| 134 | */ |
| 135 | if (!ctx) { |
| 136 | nss_warning("%p: Event received for pptp tunnel interface %d before registration", nss_ctx, ncm->interface); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | cb(ctx, ntm); |
| 141 | } |
| 142 | |
| 143 | /* |
| 144 | * nss_pptp_tx() |
| 145 | * Transmit a pptp message to NSS firmware |
| 146 | */ |
| 147 | nss_tx_status_t nss_pptp_tx(struct nss_ctx_instance *nss_ctx, struct nss_pptp_msg *msg) |
| 148 | { |
| 149 | struct nss_pptp_msg *nm; |
| 150 | struct nss_cmn_msg *ncm = &msg->cm; |
| 151 | struct sk_buff *nbuf; |
| 152 | int32_t status; |
| 153 | |
| 154 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 155 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
| 156 | nss_warning("%p: pptp msg dropped as core not ready", nss_ctx); |
| 157 | return NSS_TX_FAILURE_NOT_READY; |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | * Sanity check the message |
| 162 | */ |
| 163 | if (!nss_is_dynamic_interface(ncm->interface)) { |
| 164 | nss_warning("%p: tx request for non dynamic interface: %d", nss_ctx, ncm->interface); |
| 165 | return NSS_TX_FAILURE; |
| 166 | } |
| 167 | |
| 168 | if (ncm->type > NSS_PPTP_MSG_MAX) { |
| 169 | nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type); |
| 170 | return NSS_TX_FAILURE; |
| 171 | } |
| 172 | |
Suruchi Agarwal | ef8a870 | 2016-01-08 12:40:08 -0800 | [diff] [blame] | 173 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_pptp_msg)) { |
| 174 | nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm)); |
Shyam Sunder | 66e889d | 2015-11-02 15:31:20 +0530 | [diff] [blame] | 175 | return NSS_TX_FAILURE; |
| 176 | } |
| 177 | |
| 178 | nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE); |
| 179 | if (unlikely(!nbuf)) { |
| 180 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]); |
| 181 | nss_warning("%p: msg dropped as command allocation failed", nss_ctx); |
| 182 | return NSS_TX_FAILURE; |
| 183 | } |
| 184 | |
| 185 | /* |
| 186 | * Copy the message to our skb |
| 187 | */ |
| 188 | nm = (struct nss_pptp_msg *)skb_put(nbuf, sizeof(struct nss_pptp_msg)); |
| 189 | memcpy(nm, msg, sizeof(struct nss_pptp_msg)); |
| 190 | |
| 191 | status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0); |
| 192 | if (status != NSS_CORE_STATUS_SUCCESS) { |
| 193 | dev_kfree_skb_any(nbuf); |
| 194 | nss_warning("%p: Unable to enqueue 'pptp message'\n", nss_ctx); |
| 195 | if (status == NSS_CORE_STATUS_FAILURE_QUEUE) { |
| 196 | return NSS_TX_FAILURE_QUEUE; |
| 197 | } |
| 198 | return NSS_TX_FAILURE; |
| 199 | } |
| 200 | |
| 201 | nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_CMD_QUEUE].desc_ring.int_bit, |
| 202 | NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE); |
| 203 | |
| 204 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]); |
| 205 | return NSS_TX_SUCCESS; |
| 206 | } |
| 207 | |
| 208 | /* |
| 209 | * nss_register_pptp_if() |
| 210 | */ |
| 211 | struct nss_ctx_instance *nss_register_pptp_if(uint32_t if_num, nss_pptp_callback_t pptp_callback, |
| 212 | nss_pptp_msg_callback_t event_callback, struct net_device *netdev, uint32_t features) |
| 213 | { |
| 214 | |
| 215 | nss_assert(nss_is_dynamic_interface(if_num)); |
| 216 | |
| 217 | nss_top_main.subsys_dp_register[if_num].ndev = netdev; |
| 218 | nss_top_main.subsys_dp_register[if_num].cb = pptp_callback; |
| 219 | nss_top_main.subsys_dp_register[if_num].app_data = NULL; |
| 220 | nss_top_main.subsys_dp_register[if_num].features = features; |
| 221 | |
| 222 | nss_top_main.pptp_msg_callback = event_callback; |
| 223 | |
| 224 | nss_core_register_handler(if_num, nss_pptp_handler, NULL); |
| 225 | |
| 226 | return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.pptp_handler_id]; |
| 227 | } |
| 228 | |
| 229 | /* |
| 230 | * nss_unregister_pptp_if() |
| 231 | */ |
| 232 | void nss_unregister_pptp_if(uint32_t if_num) |
| 233 | { |
| 234 | nss_assert(nss_is_dynamic_interface(if_num)); |
| 235 | |
| 236 | nss_top_main.subsys_dp_register[if_num].ndev = NULL; |
| 237 | nss_top_main.subsys_dp_register[if_num].cb = NULL; |
| 238 | nss_top_main.subsys_dp_register[if_num].app_data = NULL; |
| 239 | nss_top_main.subsys_dp_register[if_num].features = 0; |
| 240 | |
| 241 | nss_top_main.pptp_msg_callback = NULL; |
| 242 | |
| 243 | nss_core_unregister_handler(if_num); |
| 244 | } |
| 245 | |
| 246 | /* |
| 247 | * nss_get_pptp_context() |
| 248 | */ |
| 249 | struct nss_ctx_instance *nss_pptp_get_context() |
| 250 | { |
| 251 | return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.pptp_handler_id]; |
| 252 | } |
| 253 | |
| 254 | /* |
| 255 | * nss_pptp_msg_init() |
| 256 | * Initialize nss_pptp msg. |
| 257 | */ |
| 258 | void nss_pptp_msg_init(struct nss_pptp_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data) |
| 259 | { |
| 260 | nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data); |
| 261 | } |
| 262 | |
| 263 | /* nss_pptp_register_handler() |
| 264 | * debugfs stats msg handler received on static pptp interface |
| 265 | */ |
| 266 | void nss_pptp_register_handler(void) |
| 267 | { |
| 268 | nss_info("nss_pptp_register_handler"); |
| 269 | nss_core_register_handler(NSS_PPTP_INTERFACE, nss_pptp_handler, NULL); |
| 270 | } |
| 271 | |
| 272 | EXPORT_SYMBOL(nss_pptp_get_context); |
| 273 | EXPORT_SYMBOL(nss_pptp_tx); |
| 274 | EXPORT_SYMBOL(nss_unregister_pptp_if); |
| 275 | EXPORT_SYMBOL(nss_pptp_msg_init); |
| 276 | EXPORT_SYMBOL(nss_register_pptp_if); |