1/*
2 **************************************************************************
3 * Copyright (c) 2014-2015, 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"
18
19/*
20 * nss_tun6rd_handler()
21 * 	Handle NSS -> HLOS messages for 6rd tunnel
22 */
23static void nss_tun6rd_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
24{
25	struct nss_tun6rd_msg *ntm = (struct nss_tun6rd_msg *)ncm;
26	void *ctx;
27
28	nss_tun6rd_msg_callback_t cb;
29
30	BUG_ON(!nss_is_dynamic_interface(ncm->interface));
31
32	/*
33	 * Is this a valid request/response packet?
34	 */
35	if (ncm->type >= NSS_TUN6RD_MAX) {
36		nss_warning("%p: received invalid message %d for Tun6RD interface", nss_ctx, ncm->type);
37		return;
38	}
39
40	if (ncm->len > sizeof(struct nss_tun6rd_msg)) {
41		nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
42		return;
43	}
44
45	/*
46 	 * Update the callback and app_data for NOTIFY messages, tun6rd sends all notify messages
47 	 * to the same callback/app_data.
48 	 */
49	if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) {
50		ncm->cb = (uint32_t)nss_ctx->nss_top->tun6rd_msg_callback;
51	}
52
53	/*
54	 * Log failures
55	 */
56	nss_core_log_msg_failures(nss_ctx, ncm);
57
58	/*
59	 * Do we have a call back
60	 */
61	if (!ncm->cb) {
62		return;
63	}
64
65	/*
66	 * callback
67	 */
68	cb = (nss_tun6rd_msg_callback_t)ncm->cb;
69	ctx =  nss_ctx->nss_top->subsys_dp_register[ncm->interface].ndev;
70
71	/*
72	 * call 6rd tunnel callback
73	 */
74	if (!ctx) {
75		nss_warning("%p: Event received for 6rd tunnel interface %d before registration", nss_ctx, ncm->interface);
76		return;
77	}
78
79	cb(ctx, ntm);
80}
81
82/*
83 * nss_tun6rd_tx()
84 * 	Transmit a tun6rd message to NSSFW
85 */
86nss_tx_status_t nss_tun6rd_tx(struct nss_ctx_instance *nss_ctx, struct nss_tun6rd_msg *msg)
87{
88	struct nss_tun6rd_msg *nm;
89	struct nss_cmn_msg *ncm = &msg->cm;
90	struct sk_buff *nbuf;
91	int32_t status;
92
93	NSS_VERIFY_CTX_MAGIC(nss_ctx);
94	if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
95		nss_warning("%p: tun6rd msg dropped as core not ready", nss_ctx);
96		return NSS_TX_FAILURE_NOT_READY;
97	}
98
99	/*
100	 * Sanity check the message
101	 */
102	if (!nss_is_dynamic_interface(ncm->interface)) {
103		nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
104		return NSS_TX_FAILURE;
105	}
106
107	if (ncm->type > NSS_TUN6RD_MAX) {
108		nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
109		return NSS_TX_FAILURE;
110	}
111
112	if (ncm->len > sizeof(struct nss_tun6rd_msg)) {
113		nss_warning("%p: message length is invalid: %d", nss_ctx, ncm->len);
114		return NSS_TX_FAILURE;
115	}
116
117	nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
118	if (unlikely(!nbuf)) {
119		NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]);
120		nss_warning("%p: msg dropped as command allocation failed", nss_ctx);
121		return NSS_TX_FAILURE;
122	}
123
124	/*
125	 * Copy the message to our skb
126	 */
127	nm = (struct nss_tun6rd_msg *)skb_put(nbuf, sizeof(struct nss_tun6rd_msg));
128	memcpy(nm, msg, sizeof(struct nss_tun6rd_msg));
129
130	status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
131	if (status != NSS_CORE_STATUS_SUCCESS) {
132		dev_kfree_skb_any(nbuf);
133		nss_warning("%p: Unable to enqueue 'tun6rd message' \n", nss_ctx);
134		return NSS_TX_FAILURE;
135	}
136
137	nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_CMD_QUEUE].desc_ring.int_bit,
138				NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE);
139
140	NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]);
141	return NSS_TX_SUCCESS;
142}
143
144/*
145 ***********************************
146 * Register/Unregister/Miscellaneous APIs
147 ***********************************
148 */
149
150/*
151 * nss_register_tun6rd_if()
152 */
153struct nss_ctx_instance *nss_register_tun6rd_if(uint32_t if_num, nss_tun6rd_callback_t tun6rd_callback,
154			nss_tun6rd_msg_callback_t event_callback, struct net_device *netdev, uint32_t features)
155{
156	nss_assert((if_num >=  NSS_DYNAMIC_IF_START) && (if_num < NSS_SPECIAL_IF_START));
157
158	nss_top_main.subsys_dp_register[if_num].ndev = netdev;
159	nss_top_main.subsys_dp_register[if_num].cb = tun6rd_callback;
160	nss_top_main.subsys_dp_register[if_num].app_data = NULL;
161	nss_top_main.subsys_dp_register[if_num].features = features;
162
163	nss_top_main.tun6rd_msg_callback = event_callback;
164
165	nss_core_register_handler(if_num, nss_tun6rd_handler, NULL);
166
167	return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.tun6rd_handler_id];
168}
169
170/*
171 * nss_get_tun6rd_context()
172 */
173struct nss_ctx_instance * nss_tun6rd_get_context()
174{
175	return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.tun6rd_handler_id];
176}
177
178/*
179 * nss_unregister_tun6rd_if()
180 */
181void nss_unregister_tun6rd_if(uint32_t if_num)
182{
183	nss_assert(nss_is_dynamic_interface(if_num));
184
185	nss_top_main.subsys_dp_register[if_num].ndev = NULL;
186	nss_top_main.subsys_dp_register[if_num].cb = NULL;
187	nss_top_main.subsys_dp_register[if_num].app_data = NULL;
188	nss_top_main.subsys_dp_register[if_num].features = 0;
189
190	nss_top_main.tun6rd_msg_callback = NULL;
191
192	nss_core_unregister_handler(if_num);
193}
194
195/*
196 * nss_tun6rd_msg_init()
197 *      Initialize nss_tun6rd msg.
198 */
199void nss_tun6rd_msg_init(struct nss_tun6rd_msg *ncm, uint16_t if_num, uint32_t type,  uint32_t len, void *cb, void *app_data)
200{
201        nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data);
202}
203
204EXPORT_SYMBOL(nss_tun6rd_get_context);
205EXPORT_SYMBOL(nss_tun6rd_tx);
206EXPORT_SYMBOL(nss_register_tun6rd_if);
207EXPORT_SYMBOL(nss_unregister_tun6rd_if);
208EXPORT_SYMBOL(nss_tun6rd_msg_init);
209