blob: 6cbee89d17f11b948e639e7a41aa73726197591d [file] [log] [blame]
Ankit Dhanukaa1569ce2014-05-13 19:58:06 +05301/*
2 **************************************************************************
3 * Copyright (c) 2013, 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_pppoe.c
19 * NSS PPPoE APIs
20 */
21
22#include "nss_tx_rx_common.h"
23#include <linux/ppp_channel.h>
24
25extern void nss_tx_destroy_pppoe_connection_rule(void *ctx, uint16_t pppoe_session_id, uint8_t *pppoe_remote_mac);
26
27/*
28 **********************************
29 Tx APIs
30 **********************************
31 */
32
33/*
34 * nss_pppoe_tx()
35 * Transmit an PPPoe message to the FW.
36 */
37nss_tx_status_t nss_pppoe_tx(struct nss_ctx_instance *nss_ctx, struct nss_pppoe_msg *nim)
38{
39 struct nss_pppoe_msg *nim2;
40 struct nss_cmn_msg *ncm = &nim->cm;
41 struct sk_buff *nbuf;
42 int32_t status;
43
44 NSS_VERIFY_CTX_MAGIC(nss_ctx);
45
46 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
47 nss_warning("%p: pppoe msg dropped as core not ready", nss_ctx);
48 return NSS_TX_FAILURE_NOT_READY;
49 }
50
51 /*
52 * Sanity check the message
53 */
54 if (ncm->interface != NSS_PPPOE_RX_INTERFACE) {
55 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
56 return NSS_TX_FAILURE;
57 }
58
59 if (ncm->type > NSS_PPPOE_MAX) {
60 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
61 return NSS_TX_FAILURE;
62 }
63
64 if (ncm->len > sizeof(struct nss_pppoe_msg)) {
65 nss_warning("%p: message length is invalid: %d", nss_ctx, ncm->len);
66 return NSS_TX_FAILURE;
67 }
68
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +053069 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
Ankit Dhanukaa1569ce2014-05-13 19:58:06 +053070 if (unlikely(!nbuf)) {
71 spin_lock_bh(&nss_ctx->nss_top->stats_lock);
72 nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]++;
73 spin_unlock_bh(&nss_ctx->nss_top->stats_lock);
74 nss_warning("%p: msg dropped as command allocation failed", nss_ctx);
75 return NSS_TX_FAILURE;
76 }
77
78 /*
79 * Copy the message to our skb.
80 */
81 nim2 = (struct nss_pppoe_msg *)skb_put(nbuf, sizeof(struct nss_pppoe_msg));
82 memcpy(nim2, nim, sizeof(struct nss_pppoe_msg));
83
84 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
85 if (status != NSS_CORE_STATUS_SUCCESS) {
Pamidipati, Vijayb6e38842014-09-16 10:26:05 +053086 dev_kfree_skb_any(nbuf);
Ankit Dhanukaa1569ce2014-05-13 19:58:06 +053087 nss_warning("%p: unable to enqueue PPPoE msg\n", nss_ctx);
88 return NSS_TX_FAILURE;
89 }
90
91 nss_hal_send_interrupt(nss_ctx->nmap, nss_ctx->h2n_desc_rings[NSS_IF_CMD_QUEUE].desc_ring.int_bit,
92 NSS_REGS_H2N_INTR_STATUS_DATA_COMMAND_QUEUE);
93
94 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]);
95
96 return NSS_TX_SUCCESS;
97}
98
99/*
100 **********************************
101 Rx APIs
102 **********************************
103 */
104
105/*
106 * nss_pppoe_reset_stats()
107 * Reset PPPoE stats when session is destroyed.
108 */
109static void nss_pppoe_reset_stats(struct nss_ctx_instance *nss_ctx)
110{
111 uint32_t i, j, k;
112
113 /*
114 * Reset the PPPoE statistics.
115 */
116 spin_lock_bh(&nss_ctx->nss_top->stats_lock);
117
118 /*
119 * TODO: Don't reset all the statistics. Reset only the destroyed session's stats.
120 */
121 for (i = 0; i < NSS_MAX_PHYSICAL_INTERFACES; i++) {
122 for (j = 0; j < NSS_PPPOE_NUM_SESSION_PER_INTERFACE; j++) {
123 for (k = 0; k < NSS_PPPOE_EXCEPTION_EVENT_MAX; k++) {
124 nss_ctx->nss_top->stats_if_exception_pppoe[i][j][k] = 0;
125 }
126 }
127 }
128
129 nss_ctx->nss_top->stats_pppoe[NSS_STATS_PPPOE_SESSION_CREATE_REQUESTS] = 0;
130 nss_ctx->nss_top->stats_pppoe[NSS_STATS_PPPOE_SESSION_CREATE_FAILURES] = 0;
131 nss_ctx->nss_top->stats_pppoe[NSS_STATS_PPPOE_SESSION_DESTROY_REQUESTS] = 0;
132 nss_ctx->nss_top->stats_pppoe[NSS_STATS_PPPOE_SESSION_DESTROY_MISSES] = 0;
133
134 /*
135 * TODO: Do we need to unregister the destroy method? The ppp_dev has already gone.
136 */
137 spin_unlock_bh(&nss_ctx->nss_top->stats_lock);
138}
139
140/*
141 * nss_pppoe_exception_stats_sync()
142 * Handle the syncing of PPPoE exception statistics.
143 */
144static void nss_pppoe_exception_stats_sync(struct nss_ctx_instance *nss_ctx, struct nss_pppoe_conn_stats_sync_msg *npess)
145{
146 struct nss_top_instance *nss_top = nss_ctx->nss_top;
147 uint32_t index = npess->index;
148 uint32_t interface_num = npess->interface_num;
149 uint32_t i;
150
151 spin_lock_bh(&nss_top->stats_lock);
152
153 if (interface_num >= NSS_MAX_PHYSICAL_INTERFACES) {
154 nss_warning("%p: Incorrect interface number %d for PPPoE exception stats", nss_ctx, interface_num);
155 return;
156 }
157
158 /*
159 * pppoe exception stats
160 */
161 for (i = 0; i < NSS_PPPOE_EXCEPTION_EVENT_MAX; i++) {
162 nss_top->stats_if_exception_pppoe[interface_num][index][i] += npess->exception_events_pppoe[i];
163 }
164
165 spin_unlock_bh(&nss_top->stats_lock);
166}
167
168/*
169 * nss_pppoe_node_stats_sync()
170 * Handle the syncing of PPPoE node statistics.
171 */
172static void nss_pppoe_node_stats_sync(struct nss_ctx_instance *nss_ctx, struct nss_pppoe_node_stats_sync_msg *npess)
173{
174 struct nss_top_instance *nss_top = nss_ctx->nss_top;
175
176 spin_lock_bh(&nss_top->stats_lock);
177
178 nss_top->stats_node[NSS_PPPOE_RX_INTERFACE][NSS_STATS_NODE_RX_PKTS] += npess->node_stats.rx_packets;
179 nss_top->stats_node[NSS_PPPOE_RX_INTERFACE][NSS_STATS_NODE_RX_BYTES] += npess->node_stats.rx_bytes;
180 nss_top->stats_node[NSS_PPPOE_RX_INTERFACE][NSS_STATS_NODE_RX_DROPPED] += npess->node_stats.rx_dropped;
181 nss_top->stats_node[NSS_PPPOE_RX_INTERFACE][NSS_STATS_NODE_TX_PKTS] += npess->node_stats.tx_packets;
182 nss_top->stats_node[NSS_PPPOE_RX_INTERFACE][NSS_STATS_NODE_TX_BYTES] += npess->node_stats.tx_bytes;
183
184 nss_top->stats_pppoe[NSS_STATS_PPPOE_SESSION_CREATE_REQUESTS] += npess->pppoe_session_create_requests;
185 nss_top->stats_pppoe[NSS_STATS_PPPOE_SESSION_CREATE_FAILURES] += npess->pppoe_session_create_failures;
186 nss_top->stats_pppoe[NSS_STATS_PPPOE_SESSION_DESTROY_REQUESTS] += npess->pppoe_session_destroy_requests;
187 nss_top->stats_pppoe[NSS_STATS_PPPOE_SESSION_DESTROY_MISSES] += npess->pppoe_session_destroy_misses;
188
189 spin_unlock_bh(&nss_top->stats_lock);
190}
191
192/*
193 * nss_pppoe_rule_create_success()
194 * Handle the PPPoE rule create success message.
195 */
196static void nss_pppoe_rule_create_success(struct nss_ctx_instance *nss_ctx, struct nss_pppoe_rule_create_success_msg *pcs)
197{
198 struct net_device *ppp_dev = ppp_session_to_netdev(pcs->pppoe_session_id, pcs->pppoe_remote_mac);
199
200 if (!ppp_dev) {
201 nss_warning("%p: There is not any PPP devices with SID: %x remote MAC: %x:%x:%x:%x:%x:%x", nss_ctx, pcs->pppoe_session_id,
202 pcs->pppoe_remote_mac[0], pcs->pppoe_remote_mac[1], pcs->pppoe_remote_mac[2],
203 pcs->pppoe_remote_mac[3], pcs->pppoe_remote_mac[4], pcs->pppoe_remote_mac[5]);
204
205 return;
206 }
207
208 /*
209 * TODO Remove this registration once kernel directly calls nss_pppoe_tx().
210 */
211 if (!ppp_register_destroy_method(ppp_dev, nss_tx_destroy_pppoe_connection_rule, (void *)nss_ctx)) {
212 nss_warning("%p: Failed to register destroy method", nss_ctx);
213 }
214
215 dev_put(ppp_dev);
216}
217
218/*
219 * nss_pppoe_rx_msg_handler()
220 * Handle NSS -> HLOS messages for PPPoE
221 */
222static void nss_pppoe_rx_msg_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
223{
224 struct nss_pppoe_msg *nim = (struct nss_pppoe_msg *)ncm;
225
226 BUG_ON(ncm->interface != NSS_PPPOE_RX_INTERFACE);
227
228 /*
229 * Sanity check the message type
230 */
231 if (ncm->type > NSS_PPPOE_MAX) {
232 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
233 return;
234 }
235
236 if (ncm->len > sizeof(struct nss_pppoe_msg)) {
237 nss_warning("%p: message length is invalid: %d", nss_ctx, ncm->len);
238 return;
239 }
240
241 /*
242 * Log failures
243 */
244 nss_core_log_msg_failures(nss_ctx, ncm);
245
246 /*
247 * Handling PPPoE messages coming from NSS fw.
248 */
249 switch (nim->cm.type) {
250 case NSS_PPPOE_RX_CONN_RULE_SUCCESS:
251 nss_pppoe_rule_create_success(nss_ctx, &nim->msg.pppoe_rule_create_success);
252 break;
253
254 case NSS_PPPOE_RX_NODE_STATS_SYNC:
255 nss_pppoe_node_stats_sync(nss_ctx, &nim->msg.pppoe_node_stats_sync);
256 break;
257
258 case NSS_PPPOE_RX_CONN_STATS_SYNC:
259 nss_pppoe_exception_stats_sync(nss_ctx, &nim->msg.pppoe_conn_stats_sync);
260 break;
261
262 case NSS_PPPOE_TX_CONN_RULE_DESTROY:
263 if (ncm->response == NSS_CMN_RESPONSE_ACK) {
264 nss_pppoe_reset_stats(nss_ctx);
265 }
266 break;
267
268 default:
269 nss_warning("%p: Received response %d for type %d, interface %d",
270 nss_ctx, ncm->response, ncm->type, ncm->interface);
271 }
272}
273
274/*
275 * nss_pppoe_register_handler()
276 */
277void nss_pppoe_register_handler()
278{
279 nss_core_register_handler(NSS_PPPOE_RX_INTERFACE, nss_pppoe_rx_msg_handler, NULL);
280}
281