blob: ab79f9b0c516f57ceefa23a26341629867c6d9ad [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"
Yu Huang8c107082017-07-24 14:58:26 -070018#include "nss_sjack_stats.h"
Ankit Dhanuka14999992014-11-12 15:35:11 +053019
20/*
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +053021 * nss_sjack_handler()
22 * Handle NSS -> HLOS messages for sjack
23 */
24static void nss_sjack_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm,
25 __attribute__((unused))void *app_data)
26{
27 void *ctx;
28 nss_sjack_msg_callback_t cb;
Ankit Dhanuka14999992014-11-12 15:35:11 +053029 struct nss_sjack_msg *nsm = (struct nss_sjack_msg *)ncm;
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +053030
31 BUG_ON(ncm->interface != NSS_SJACK_INTERFACE);
32
33 /*
34 * Is this a valid request/response packet?
35 */
36 if (ncm->type >= NSS_SJACK_MAX_MSG_TYPE) {
37 nss_warning("%p: received invalid message %d for sjack interface", nss_ctx, ncm->type);
38 return;
39 }
40
Suruchi Agarwalef8a8702016-01-08 12:40:08 -080041 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_sjack_msg)) {
42 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 +053043 return;
44 }
45
46 /*
47 * Update the callback and app_data for NOTIFY messages, sjack sends all notify messages
48 * to the same callback/app_data.
49 */
50 if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) {
Stephen Wangaed46332016-12-12 17:29:03 -080051 ncm->cb = (nss_ptr_t)nss_ctx->nss_top->if_rx_msg_callback[ncm->interface];
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +053052 }
53
54 /*
55 * Log failures
56 */
57 nss_core_log_msg_failures(nss_ctx, ncm);
58
Ankit Dhanuka14999992014-11-12 15:35:11 +053059 switch (ncm->type) {
60 case NSS_SJACK_STATS_SYNC_MSG:
61 /*
62 * Update sjack statistics on node sync.
63 */
Yu Huang8c107082017-07-24 14:58:26 -070064 nss_sjack_stats_node_sync(nss_ctx, &nsm->msg.stats_sync);
Ankit Dhanuka14999992014-11-12 15:35:11 +053065 break;
66 }
67
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +053068 /*
69 * Do we have a call back
70 */
71 if (!ncm->cb) {
72 return;
73 }
74
75 /*
76 * callback
77 */
78 cb = (nss_sjack_msg_callback_t)ncm->cb;
Stephen Wang84e0e992016-09-07 12:31:40 -070079 ctx = nss_ctx->subsys_dp_register[ncm->interface].ndev;
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +053080
81 cb(ctx, ncm);
82}
83
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +053084/*
85 * nss_sjack_tx_msg()
86 * Transmit a sjack message to NSSFW
87 */
88nss_tx_status_t nss_sjack_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_sjack_msg *msg)
89{
90 struct nss_sjack_msg *nm;
91 struct nss_cmn_msg *ncm = &msg->cm;
92 struct sk_buff *nbuf;
93 int32_t status;
94
95 NSS_VERIFY_CTX_MAGIC(nss_ctx);
96 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
97 nss_warning("%p: sjack msg dropped as core not ready", nss_ctx);
98 return NSS_TX_FAILURE_NOT_READY;
99 }
100
101 /*
102 * Sanity check the message
103 */
104 if (ncm->interface != NSS_SJACK_INTERFACE) {
105 nss_warning("%p: tx request for another interface: %d", nss_ctx, ncm->interface);
106 return NSS_TX_FAILURE;
107 }
108
109 if (ncm->type > NSS_SJACK_MAX_MSG_TYPE) {
110 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
111 return NSS_TX_FAILURE;
112 }
113
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800114 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_sjack_msg)) {
115 nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530116 return NSS_TX_FAILURE;
117 }
118
119 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
120 if (unlikely(!nbuf)) {
Sundarajan Srinivasan62fee7e2015-01-22 11:13:10 -0800121 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 +0530122 nss_warning("%p: msg dropped as command allocation failed", nss_ctx);
123 return NSS_TX_FAILURE;
124 }
125
126 /*
127 * Copy the message to our skb
128 */
129 nm = (struct nss_sjack_msg *)skb_put(nbuf, sizeof(struct nss_sjack_msg));
130 memcpy(nm, msg, sizeof(struct nss_sjack_msg));
131
132 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
133 if (status != NSS_CORE_STATUS_SUCCESS) {
134 dev_kfree_skb_any(nbuf);
135 nss_warning("%p: Unable to enqueue 'sjack message' \n", nss_ctx);
136 return NSS_TX_FAILURE;
137 }
138
Stephen Wang90c67de2016-04-26 15:15:59 -0700139 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530140
141 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]);
142 return NSS_TX_SUCCESS;
143}
144
145/*
146 * nss_sjack_register_if()
147 */
148struct nss_ctx_instance *nss_sjack_register_if(uint32_t if_num, struct net_device *netdev,
149 nss_sjack_msg_callback_t event_callback)
150{
Stephen Wang84e0e992016-09-07 12:31:40 -0700151 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.sjack_handler_id];
152
153 nss_assert(nss_ctx);
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530154 nss_assert(if_num == NSS_SJACK_INTERFACE);
155
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700156 nss_core_register_subsys_dp(nss_ctx, if_num, NULL, NULL, NULL, netdev, 0);
Sundarajan Srinivasan70374842014-11-19 15:22:52 -0800157
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530158 nss_top_main.if_rx_msg_callback[if_num] = event_callback;
159
Stephen Wang84e0e992016-09-07 12:31:40 -0700160 return nss_ctx;
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530161}
162
163/*
164 * nss_sjack_unregister_if()
165 */
166void nss_sjack_unregister_if(uint32_t if_num)
167{
Stephen Wang84e0e992016-09-07 12:31:40 -0700168 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.sjack_handler_id];
169
170 nss_assert(nss_ctx);
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530171 nss_assert(if_num == NSS_SJACK_INTERFACE);
172
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700173 nss_core_unregister_subsys_dp(nss_ctx, if_num);
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530174 nss_top_main.if_rx_msg_callback[if_num] = NULL;
175
176 return;
177}
178
179/*
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700180 * nss_sjack_get_context()
181 * get NSS context instance for sjack
182 */
183struct nss_ctx_instance *nss_sjack_get_context(void)
184{
185 return &nss_top_main.nss[nss_top_main.sjack_handler_id];
186}
187EXPORT_SYMBOL(nss_sjack_get_context);
188
189/*
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530190 * nss_sjack_register_handler()
191 * Registering handler for sending msg to sjack node on NSS.
192 */
193void nss_sjack_register_handler(void)
194{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700195 struct nss_ctx_instance *nss_ctx = nss_sjack_get_context();
196
197 nss_core_register_handler(nss_ctx, NSS_SJACK_INTERFACE, nss_sjack_handler, NULL);
Yu Huang8c107082017-07-24 14:58:26 -0700198
199 nss_sjack_stats_dentry_create();
Ankit Dhanuka2ae20a82014-08-25 18:04:00 +0530200}
201
202EXPORT_SYMBOL(nss_sjack_register_if);
203EXPORT_SYMBOL(nss_sjack_unregister_if);
204EXPORT_SYMBOL(nss_sjack_tx_msg);