blob: 20ab41c1ebae3cb8575c8396e2f7908fad34c3c5 [file] [log] [blame]
Sol Kavy879eb8b2014-04-07 19:11:31 -07001/*
2 **************************************************************************
3 * Copyright (c) 2014, 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/*
18 * nss_ipv6.c
19 * NSS IPv6 APIs
20 */
21#include <linux/ppp_channel.h>
22#include "nss_tx_rx_common.h"
23#include "nss_ipv6.h"
24
25extern void nss_rx_metadata_ipv6_rule_establish(struct nss_ctx_instance *nss_ctx, struct nss_ipv6_rule_establish *nire);
26extern void nss_rx_ipv6_sync(struct nss_ctx_instance *nss_ctx, struct nss_ipv6_conn_sync *nirs);
27
28/*
29 * nss_ipv6_driver_conn_sync_update()
30 * Update driver specific information from the messsage.
31 */
32static void nss_ipv6_driver_conn_sync_update(struct nss_ctx_instance *nss_ctx, struct nss_ipv6_conn_sync *nics)
33{
34 struct nss_top_instance *nss_top = nss_ctx->nss_top;
35 struct net_device *pppoe_dev = NULL;
36
37 /*
38 * Update statistics maintained by NSS driver
39 */
40 spin_lock_bh(&nss_top->stats_lock);
41 nss_top->stats_ipv6[NSS_STATS_IPV6_ACCELERATED_RX_PKTS] += nics->flow_rx_packet_count + nics->return_rx_packet_count;
42 nss_top->stats_ipv6[NSS_STATS_IPV6_ACCELERATED_RX_BYTES] += nics->flow_rx_byte_count + nics->return_rx_byte_count;
43 nss_top->stats_ipv6[NSS_STATS_IPV6_ACCELERATED_TX_PKTS] += nics->flow_tx_packet_count + nics->return_tx_packet_count;
44 nss_top->stats_ipv6[NSS_STATS_IPV6_ACCELERATED_TX_BYTES] += nics->flow_tx_byte_count + nics->return_tx_byte_count;
45 spin_unlock(&nss_top->stats_lock);
46
47 /*
48 * Update the PPPoE interface stats, if there is any PPPoE session on the interfaces.
49 */
50 if (nics->flow_pppoe_session_id) {
51 pppoe_dev = ppp_session_to_netdev(nics->flow_pppoe_session_id, (uint8_t *)nics->flow_pppoe_remote_mac);
52 if (pppoe_dev) {
53 ppp_update_stats(pppoe_dev, nics->flow_rx_packet_count, nics->flow_rx_byte_count,
54 nics->flow_tx_packet_count, nics->flow_tx_byte_count);
55 dev_put(pppoe_dev);
56 }
57 }
58
59 if (nics->return_pppoe_session_id) {
60 pppoe_dev = ppp_session_to_netdev(nics->return_pppoe_session_id, (uint8_t *)nics->return_pppoe_remote_mac);
61 if (pppoe_dev) {
62 ppp_update_stats(pppoe_dev, nics->return_rx_packet_count, nics->return_rx_byte_count,
63 nics->return_tx_packet_count, nics->return_tx_byte_count);
64 dev_put(pppoe_dev);
65 }
66 }
67
68}
69
70/*
71 * nss_ipv6_rx_msg_handler()
72 * Handle NSS -> HLOS messages for IPv6 bridge/route
73 */
74static void nss_ipv6_rx_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
75{
76 struct nss_ipv6_msg *nim = (struct nss_ipv6_msg *)ncm;
77 nss_ipv6_msg_callback_t cb;
78
79 BUG_ON(ncm->interface != NSS_IPV6_RX_INTERFACE);
80
81 /*
82 * Is this a valid request/response packet?
83 */
84 if (nim->cm.type >= NSS_IPV6_MAX_MSG_TYPES) {
85 nss_warning("%p: received invalid message %d for IPv6 interface", nss_ctx, nim->cm.type);
86 return;
87 }
88
89 if (ncm->len > sizeof(struct nss_ipv6_msg)) {
90 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
91 return;
92 }
93
94 /*
95 * Log failures
96 */
97 nss_core_log_msg_failures(nss_ctx, ncm);
98
99 /*
100 * Handle deprecated messages. Eventually these messages should be removed.
101 */
102 switch (nim->cm.type) {
103 case NSS_IPV6_RX_ESTABLISH_RULE_MSG:
104 nss_rx_metadata_ipv6_rule_establish(nss_ctx, &nim->msg.rule_establish);
105 break;
106
107 case NSS_IPV6_RX_CONN_STATS_SYNC_MSG:
108 /*
109 * Update driver statistics on connection sync.
110 */
111 nss_ipv6_driver_conn_sync_update(nss_ctx, &nim->msg.conn_stats);
112 return nss_rx_ipv6_sync(nss_ctx, &nim->msg.conn_stats);
113 break;
114 }
115
116 /*
117 * Update the callback and app_data for NOTIFY messages, IPv6 sends all notify messages
118 * to the same callback/app_data.
119 */
120 if (nim->cm.response == NSS_CMM_RESPONSE_NOTIFY) {
121 ncm->cb = (uint32_t)nss_ctx->nss_top->ipv6_callback;
122 ncm->app_data = (uint32_t)nss_ctx->nss_top->ipv6_ctx;
123 }
124
125 /*
126 * Do we have a callback?
127 */
128 if (!ncm->cb) {
129 return;
130 }
131
132 /*
133 * Callback
134 */
135 cb = (nss_ipv6_msg_callback_t)ncm->cb;
136 cb((void *)ncm->app_data, nim);
137}
138
139/*
140 * nss_ipv6_tx()
141 * Transmit an ipv6 message to the FW.
142 */
143nss_tx_status_t nss_ipv6_tx(struct nss_ctx_instance *nss_ctx, struct nss_ipv6_msg *nim)
144{
145 struct nss_ipv6_msg *nim2;
146 struct nss_cmn_msg *ncm = &nim->cm;
147 struct sk_buff *nbuf;
148 int32_t status;
149
150 NSS_VERIFY_CTX_MAGIC(nss_ctx);
151 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
152 nss_warning("%p: ipv6 msg dropped as core not ready", nss_ctx);
153 return NSS_TX_FAILURE_NOT_READY;
154 }
155
156 /*
157 * Sanity check the message
158 */
159 if (ncm->interface != NSS_IPV6_RX_INTERFACE) {
160 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
161 return NSS_TX_FAILURE;
162 }
163
164 if (ncm->type > NSS_IPV6_MAX_MSG_TYPES) {
165 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
166 return NSS_TX_FAILURE;
167 }
168
169 if (ncm->len > sizeof(struct nss_ipv6_msg)) {
170 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
171 return NSS_TX_FAILURE;
172 }
173
174 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
175 if (unlikely(!nbuf)) {
176 spin_lock_bh(&nss_ctx->nss_top->stats_lock);
177 nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]++;
178 spin_unlock_bh(&nss_ctx->nss_top->stats_lock);
179 nss_warning("%p: msg dropped as command allocation failed", nss_ctx);
180 return NSS_TX_FAILURE;
181 }
182
183 /*
184 * Copy the message to our skb.
185 */
186 nim2 = (struct nss_ipv6_msg *)skb_put(nbuf, sizeof(struct nss_ipv6_msg));
187 memcpy(nim2, nim, sizeof(struct nss_ipv6_msg));
188
189 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
190 if (status != NSS_CORE_STATUS_SUCCESS) {
191 dev_kfree_skb_any(nbuf);
192 nss_warning("%p: Unable to enqueue 'Destroy IPv6' rule\n", nss_ctx);
193 return NSS_TX_FAILURE;
194 }
195
196 nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_CMD_QUEUE].desc_ring.int_bit,
197 NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE);
198
199 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]);
200 return NSS_TX_SUCCESS;
201}
202
203/*
204 **********************************
205 Register/Unregister/Miscellaneous APIs
206 **********************************
207 */
208
209/*
210 * nss_ipv6_notify_register()
211 * Register to received IPv6 events.
212 *
213 * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv6 on any core?
214 */
215struct nss_ctx_instance *nss_ipv6_notify_register(nss_ipv6_msg_callback_t cb, void *app_data)
216{
217 /*
218 * TODO: We need to have a new array in support of the new API
219 * TODO: If we use a per-context array, we would move the array into nss_ctx based.
220 */
221 nss_top_main.ipv6_callback = cb;
222 nss_top_main.ipv6_ctx = app_data;
223 return &nss_top_main.nss[nss_top_main.ipv6_handler_id];
224}
225
226/*
227 * nss_ipv6_notify_unregister()
228 * Unregister to received IPv6 events.
229 *
230 * NOTE: Do we want to pass an nss_ctx here so that we can register for ipv6 on any core?
231 */
232void nss_ipv6_notify_unregister(void)
233{
234 nss_top_main.ipv6_callback = NULL;
235}
236
237/*
238 * nss_ipv6_get_mgr()
239 *
240 * TODO: This only suppports a single ipv6, do we ever want to support more?
241 */
242struct nss_ctx_instance *nss_ipv6_get_mgr(void)
243{
244 return (void *)&nss_top_main.nss[nss_top_main.ipv6_handler_id];
245}
246
247/*
248 * nss_ipv6_register_handler()
249 * Register our handler to receive messages for this interface
250 */
251void nss_ipv6_register_handler()
252{
253 if (nss_core_register_handler(NSS_IPV6_RX_INTERFACE, nss_ipv6_rx_msg_handler, NULL) != NSS_CORE_STATUS_SUCCESS) {
254 nss_warning("IPv6 handler failed to register");
255 }
256}
257
258EXPORT_SYMBOL(nss_ipv6_tx);
259EXPORT_SYMBOL(nss_ipv6_notify_register);
260EXPORT_SYMBOL(nss_ipv6_notify_unregister);
261EXPORT_SYMBOL(nss_ipv6_get_mgr);
262EXPORT_SYMBOL(nss_ipv6_register_handler);