blob: 17fedbd3b76505c35cff0025d910fd4759b6d967 [file] [log] [blame]
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +05301/*
2 **************************************************************************
Stephen Wangaed46332016-12-12 17:29:03 -08003 * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved.
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +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
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/*
Ankit Dhanuka14999992014-11-12 15:35:11 +053020 * nss_sjack_node_sync_update()
21 * Update sjack node stats.
22 */
23static void nss_sjack_node_sync_update(struct nss_ctx_instance *nss_ctx, struct nss_sjack_stats_sync_msg *nins)
24{
25 struct nss_top_instance *nss_top = nss_ctx->nss_top;
26
27 /*
28 * Update SJACK node stats.
29 */
30 spin_lock_bh(&nss_top->stats_lock);
31 nss_top->stats_node[NSS_SJACK_INTERFACE][NSS_STATS_NODE_RX_PKTS] += nins->node_stats.rx_packets;
32 nss_top->stats_node[NSS_SJACK_INTERFACE][NSS_STATS_NODE_RX_BYTES] += nins->node_stats.rx_bytes;
33 nss_top->stats_node[NSS_SJACK_INTERFACE][NSS_STATS_NODE_RX_DROPPED] += nins->node_stats.rx_dropped;
34 nss_top->stats_node[NSS_SJACK_INTERFACE][NSS_STATS_NODE_TX_PKTS] += nins->node_stats.tx_packets;
35 nss_top->stats_node[NSS_SJACK_INTERFACE][NSS_STATS_NODE_TX_BYTES] += nins->node_stats.tx_bytes;
36 spin_unlock_bh(&nss_top->stats_lock);
37}
38
39/*
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +053040 * nss_sjack_handler()
41 * Handle NSS -> HLOS messages for sjack
42 */
43static void nss_sjack_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm,
44 __attribute__((unused))void *app_data)
45{
46 void *ctx;
47 nss_sjack_msg_callback_t cb;
Ankit Dhanuka14999992014-11-12 15:35:11 +053048 struct nss_sjack_msg *nsm = (struct nss_sjack_msg *)ncm;
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +053049
50 BUG_ON(ncm->interface != NSS_SJACK_INTERFACE);
51
52 /*
53 * Is this a valid request/response packet?
54 */
55 if (ncm->type >= NSS_SJACK_MAX_MSG_TYPE) {
56 nss_warning("%p: received invalid message %d for sjack interface", nss_ctx, ncm->type);
57 return;
58 }
59
Suruchi Agarwalef8a8702016-01-08 12:40:08 -080060 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_sjack_msg)) {
61 nss_warning("%p: Length of message is greater than required: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +053062 return;
63 }
64
65 /*
66 * Update the callback and app_data for NOTIFY messages, sjack sends all notify messages
67 * to the same callback/app_data.
68 */
69 if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) {
Stephen Wangaed46332016-12-12 17:29:03 -080070 ncm->cb = (nss_ptr_t)nss_ctx->nss_top->if_rx_msg_callback[ncm->interface];
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +053071 }
72
73 /*
74 * Log failures
75 */
76 nss_core_log_msg_failures(nss_ctx, ncm);
77
Ankit Dhanuka14999992014-11-12 15:35:11 +053078 switch (ncm->type) {
79 case NSS_SJACK_STATS_SYNC_MSG:
80 /*
81 * Update sjack statistics on node sync.
82 */
83 nss_sjack_node_sync_update(nss_ctx, &nsm->msg.stats_sync);
84 break;
85 }
86
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +053087 /*
88 * Do we have a call back
89 */
90 if (!ncm->cb) {
91 return;
92 }
93
94 /*
95 * callback
96 */
97 cb = (nss_sjack_msg_callback_t)ncm->cb;
Stephen Wang84e0e992016-09-07 12:31:40 -070098 ctx = nss_ctx->subsys_dp_register[ncm->interface].ndev;
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +053099
100 cb(ctx, ncm);
101}
102
103
104/*
105 * nss_sjack_tx_msg()
106 * Transmit a sjack message to NSSFW
107 */
108nss_tx_status_t nss_sjack_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_sjack_msg *msg)
109{
110 struct nss_sjack_msg *nm;
111 struct nss_cmn_msg *ncm = &msg->cm;
112 struct sk_buff *nbuf;
113 int32_t status;
114
115 NSS_VERIFY_CTX_MAGIC(nss_ctx);
116 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
117 nss_warning("%p: sjack msg dropped as core not ready", nss_ctx);
118 return NSS_TX_FAILURE_NOT_READY;
119 }
120
121 /*
122 * Sanity check the message
123 */
124 if (ncm->interface != NSS_SJACK_INTERFACE) {
125 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
126 return NSS_TX_FAILURE;
127 }
128
129 if (ncm->type > NSS_SJACK_MAX_MSG_TYPE) {
130 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
131 return NSS_TX_FAILURE;
132 }
133
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800134 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_sjack_msg)) {
135 nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530136 return NSS_TX_FAILURE;
137 }
138
139 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
140 if (unlikely(!nbuf)) {
Sundarajan Srinivasan62fee7e2015-01-22 11:13:10 -0800141 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]);
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530142 nss_warning("%p: msg dropped as command allocation failed", nss_ctx);
143 return NSS_TX_FAILURE;
144 }
145
146 /*
147 * Copy the message to our skb
148 */
149 nm = (struct nss_sjack_msg *)skb_put(nbuf, sizeof(struct nss_sjack_msg));
150 memcpy(nm, msg, sizeof(struct nss_sjack_msg));
151
152 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
153 if (status != NSS_CORE_STATUS_SUCCESS) {
154 dev_kfree_skb_any(nbuf);
155 nss_warning("%p: Unable to enqueue 'sjack message' \n", nss_ctx);
156 return NSS_TX_FAILURE;
157 }
158
Stephen Wang90c67de2016-04-26 15:15:59 -0700159 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530160
161 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]);
162 return NSS_TX_SUCCESS;
163}
164
165/*
166 * nss_sjack_register_if()
167 */
168struct nss_ctx_instance *nss_sjack_register_if(uint32_t if_num, struct net_device *netdev,
169 nss_sjack_msg_callback_t event_callback)
170{
Stephen Wang84e0e992016-09-07 12:31:40 -0700171 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.sjack_handler_id];
172
173 nss_assert(nss_ctx);
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530174 nss_assert(if_num == NSS_SJACK_INTERFACE);
175
Stephen Wang84e0e992016-09-07 12:31:40 -0700176 nss_ctx->subsys_dp_register[if_num].ndev = netdev;
Sundarajan Srinivasan70374842014-11-19 15:22:52 -0800177
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530178 nss_top_main.if_rx_msg_callback[if_num] = event_callback;
179
Stephen Wang84e0e992016-09-07 12:31:40 -0700180 return nss_ctx;
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530181}
182
183/*
184 * nss_sjack_unregister_if()
185 */
186void nss_sjack_unregister_if(uint32_t if_num)
187{
Stephen Wang84e0e992016-09-07 12:31:40 -0700188 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.sjack_handler_id];
189
190 nss_assert(nss_ctx);
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530191 nss_assert(if_num == NSS_SJACK_INTERFACE);
192
Stephen Wang84e0e992016-09-07 12:31:40 -0700193 nss_ctx->subsys_dp_register[if_num].ndev = NULL;
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530194 nss_top_main.if_rx_msg_callback[if_num] = NULL;
195
196 return;
197}
198
199/*
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700200 * nss_sjack_get_context()
201 * get NSS context instance for sjack
202 */
203struct nss_ctx_instance *nss_sjack_get_context(void)
204{
205 return &nss_top_main.nss[nss_top_main.sjack_handler_id];
206}
207EXPORT_SYMBOL(nss_sjack_get_context);
208
209/*
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530210 * nss_sjack_register_handler()
211 * Registering handler for sending msg to sjack node on NSS.
212 */
213void nss_sjack_register_handler(void)
214{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700215 struct nss_ctx_instance *nss_ctx = nss_sjack_get_context();
216
217 nss_core_register_handler(nss_ctx, NSS_SJACK_INTERFACE, nss_sjack_handler, NULL);
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530218}
219
220EXPORT_SYMBOL(nss_sjack_register_if);
221EXPORT_SYMBOL(nss_sjack_unregister_if);
222EXPORT_SYMBOL(nss_sjack_tx_msg);