blob: fb8ad5a77b72d4bd5af17cc04fbaf50a3438ae59 [file] [log] [blame]
Bharath M Kumar614bbf82013-08-31 20:18:44 +05301/*
Murat Sezgin84ca6512014-04-14 13:57:24 -07002 **************************************************************************
Stephen Wangaed46332016-12-12 17:29:03 -08003 * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
Bharath M Kumar614bbf82013-08-31 20:18:44 +05304 * Permission to use, copy, modify, and/or distribute this software for
5 * any purpose with or without fee is hereby granted, provided that the
Murat Sezgin84ca6512014-04-14 13:57:24 -07006 * above copyright notice and this permission notice appear in all copies.
Bharath M Kumar614bbf82013-08-31 20:18:44 +05307 * 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.
Murat Sezgin84ca6512014-04-14 13:57:24 -070014 **************************************************************************
Bharath M Kumar614bbf82013-08-31 20:18:44 +053015 */
16
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053017#include "nss_tx_rx_common.h"
Bharath M Kumar614bbf82013-08-31 20:18:44 +053018
Bharath M Kumar614bbf82013-08-31 20:18:44 +053019/*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053020 * nss_tunipip6_handler()
Sol Kavy359ef152014-04-04 11:55:24 -070021 * Handle NSS -> HLOS messages for 6rd tunnel
Bharath M Kumar614bbf82013-08-31 20:18:44 +053022 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053023static void nss_tunipip6_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
Bharath M Kumar614bbf82013-08-31 20:18:44 +053024{
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053025 struct nss_tunipip6_msg *ntm = (struct nss_tunipip6_msg *)ncm;
26 void *ctx;
27 nss_tunipip6_msg_callback_t cb;
Bharath M Kumar614bbf82013-08-31 20:18:44 +053028
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053029 BUG_ON(ncm->interface != NSS_TUNIPIP6_INTERFACE);
Bharath M Kumar614bbf82013-08-31 20:18:44 +053030 /*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053031 * Is this a valid request/response packet?
Bharath M Kumar614bbf82013-08-31 20:18:44 +053032 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053033 if (ncm->type >= NSS_TUNIPIP6_MAX) {
34 nss_warning("%p: received invalid message %d for DS-Lite interface", nss_ctx, ncm->type);
Bharath M Kumar614bbf82013-08-31 20:18:44 +053035 return;
36 }
37
Suruchi Agarwalef8a8702016-01-08 12:40:08 -080038 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_tunipip6_msg)) {
39 nss_warning("%p: Length of message is greater than required: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Bharath M Kumar614bbf82013-08-31 20:18:44 +053040 return;
41 }
42
43 /*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053044 * Update the callback and app_data for NOTIFY messages, tun6rd sends all notify messages
45 * to the same callback/app_data.
Bharath M Kumar614bbf82013-08-31 20:18:44 +053046 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053047 if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) {
Stephen Wangaed46332016-12-12 17:29:03 -080048 ncm->cb = (nss_ptr_t)nss_ctx->nss_top->tunipip6_msg_callback;
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053049 }
50
51 /*
52 * Log failures
53 */
54 nss_core_log_msg_failures(nss_ctx, ncm);
55
56 /*
57 * Do we have a call back
58 */
59 if (!ncm->cb) {
Bharath M Kumar614bbf82013-08-31 20:18:44 +053060 return;
61 }
62
63 /*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053064 * callback
Bharath M Kumar614bbf82013-08-31 20:18:44 +053065 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053066 cb = (nss_tunipip6_msg_callback_t)ncm->cb;
Stephen Wang84e0e992016-09-07 12:31:40 -070067 ctx = nss_ctx->subsys_dp_register[ncm->interface].ndev;
Bharath M Kumar614bbf82013-08-31 20:18:44 +053068
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053069 /*
70 * call ipip6 tunnel callback
71 */
72 if (!ctx) {
73 nss_warning("%p: Event received for DS-Lite tunnel interface %d before registration", nss_ctx, ncm->interface);
Bharath M Kumar614bbf82013-08-31 20:18:44 +053074 return;
75 }
76
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053077 cb(ctx, ntm);
Bharath M Kumar614bbf82013-08-31 20:18:44 +053078}
79
80/*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053081 * nss_tunipip6_tx()
82 * Transmit a tun6rd message to NSSFW
Bharath M Kumar614bbf82013-08-31 20:18:44 +053083 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053084nss_tx_status_t nss_tunipip6_tx(struct nss_ctx_instance *nss_ctx, struct nss_tunipip6_msg *msg)
Bharath M Kumar614bbf82013-08-31 20:18:44 +053085{
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053086 struct nss_tunipip6_msg *nm;
87 struct nss_cmn_msg *ncm = &msg->cm;
88 struct sk_buff *nbuf;
89 int32_t status;
Bharath M Kumar614bbf82013-08-31 20:18:44 +053090
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053091 NSS_VERIFY_CTX_MAGIC(nss_ctx);
92 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
93 nss_warning("%p: tun6rd msg dropped as core not ready", nss_ctx);
94 return NSS_TX_FAILURE_NOT_READY;
Bharath M Kumar614bbf82013-08-31 20:18:44 +053095 }
96
Bharath M Kumar614bbf82013-08-31 20:18:44 +053097 /*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +053098 * Sanity check the message
Bharath M Kumar614bbf82013-08-31 20:18:44 +053099 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530100 if (ncm->interface != NSS_TUNIPIP6_INTERFACE) {
101 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
102 return NSS_TX_FAILURE;
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530103 }
104
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530105 if (ncm->type > NSS_TUNIPIP6_MAX) {
106 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
107 return NSS_TX_FAILURE;
Radhakrishna Jiguru5ad493d2013-11-25 20:17:54 +0530108 }
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530109
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800110 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_tunipip6_msg)) {
111 nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530112 return NSS_TX_FAILURE;
113 }
114
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530115 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530116 if (unlikely(!nbuf)) {
Sundarajan Srinivasan62fee7e2015-01-22 11:13:10 -0800117 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]);
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530118 nss_warning("%p: msg dropped as command allocation failed", nss_ctx);
119 return NSS_TX_FAILURE;
120 }
121
122 /*
123 * Copy the message to our skb
124 */
125 nm = (struct nss_tunipip6_msg *)skb_put(nbuf, sizeof(struct nss_tunipip6_msg));
126 memcpy(nm, msg, sizeof(struct nss_tunipip6_msg));
127
128 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
129 if (status != NSS_CORE_STATUS_SUCCESS) {
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +0530130 dev_kfree_skb_any(nbuf);
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530131 nss_warning("%p: Unable to enqueue 'tun6rd message' \n", nss_ctx);
132 return NSS_TX_FAILURE;
133 }
134
Stephen Wang90c67de2016-04-26 15:15:59 -0700135 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530136
137 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]);
138 return NSS_TX_SUCCESS;
Radhakrishna Jiguru5ad493d2013-11-25 20:17:54 +0530139}
140
141/*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530142 * **********************************
143 * Register/Unregister/Miscellaneous APIs
144 * **********************************
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530145 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530146
147/*
148 * nss_register_tunipip6_if()
149 */
150struct nss_ctx_instance *nss_register_tunipip6_if(uint32_t if_num,
151 nss_tunipip6_callback_t tunipip6_callback,
Sundarajan Srinivasan70374842014-11-19 15:22:52 -0800152 nss_tunipip6_msg_callback_t event_callback,
153 struct net_device *netdev,
154 uint32_t features)
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530155{
Stephen Wang84e0e992016-09-07 12:31:40 -0700156 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.tunipip6_handler_id];
157
158 nss_assert(nss_ctx);
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530159 nss_assert((if_num >= NSS_MAX_VIRTUAL_INTERFACES) && (if_num < NSS_MAX_NET_INTERFACES));
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530160
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530161 nss_top_main.tunipip6_msg_callback = event_callback;
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530162
Stephen Wang84e0e992016-09-07 12:31:40 -0700163 nss_ctx->subsys_dp_register[if_num].ndev = netdev;
164 nss_ctx->subsys_dp_register[if_num].cb = tunipip6_callback;
165 nss_ctx->subsys_dp_register[if_num].app_data = NULL;
166 nss_ctx->subsys_dp_register[if_num].features = features;
Sundarajan Srinivasan70374842014-11-19 15:22:52 -0800167
Stephen Wang84e0e992016-09-07 12:31:40 -0700168 return nss_ctx;
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530169}
170
171/*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530172 * nss_unregister_tunipip6_if()
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530173 */
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530174void nss_unregister_tunipip6_if(uint32_t if_num)
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530175{
Stephen Wang84e0e992016-09-07 12:31:40 -0700176 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.tunipip6_handler_id];
177
178 nss_assert(nss_ctx);
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530179 nss_assert((if_num >= NSS_MAX_VIRTUAL_INTERFACES) && (if_num < NSS_MAX_NET_INTERFACES));
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530180
Stephen Wang84e0e992016-09-07 12:31:40 -0700181 nss_ctx->subsys_dp_register[if_num].cb = NULL;
182 nss_ctx->subsys_dp_register[if_num].ndev = NULL;
183 nss_ctx->subsys_dp_register[if_num].features = 0;
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530184 nss_top_main.tunipip6_msg_callback = NULL;
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530185}
186
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530187/*
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700188 * nss_tunipip6_get_context()
189 */
190struct nss_ctx_instance *nss_tunipip6_get_context(void)
191{
192 return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.tunipip6_handler_id];
193}
194
195/*
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530196 * nss_tunipip6_register_handler()
197 */
198void nss_tunipip6_register_handler()
199{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700200 struct nss_ctx_instance *nss_ctx = nss_tunipip6_get_context();
201
202 nss_core_register_handler(nss_ctx, NSS_TUNIPIP6_INTERFACE, nss_tunipip6_handler, NULL);
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530203}
Bharath M Kumar614bbf82013-08-31 20:18:44 +0530204
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700205/*
206 * nss_tunipip6_msg_init()
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700207 * Initialize nss_tunipip6 msg.
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700208 */
209void nss_tunipip6_msg_init(struct nss_tunipip6_msg *ntm, uint16_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data)
210{
211 nss_cmn_msg_init(&ntm->cm, if_num, type, len, cb, app_data);
212}
213
Radha krishna Simha Jiguru62a29e62014-04-04 17:28:53 +0530214EXPORT_SYMBOL(nss_tunipip6_tx);
215EXPORT_SYMBOL(nss_register_tunipip6_if);
216EXPORT_SYMBOL(nss_unregister_tunipip6_if);
Sundarajan Srinivasan02e6c2b2014-10-06 11:51:12 -0700217EXPORT_SYMBOL(nss_tunipip6_msg_init);