blob: 315b438708ffa36f6b842504e3d55154a768886a [file] [log] [blame]
Bharath M Kumarcc666e92014-12-24 19:17:28 +05301/*
2 **************************************************************************
Stephen Wangaed46332016-12-12 17:29:03 -08003 * Copyright (c) 2015-2017, The Linux Foundation. All rights reserved.
Bharath M Kumarcc666e92014-12-24 19:17:28 +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_wifi_stats.h"
Bharath M Kumarcc666e92014-12-24 19:17:28 +053019
ratheesh kannotheb2a0a82017-05-04 09:20:17 +053020/*
21 * nss_wifi_get_context()
22 * Get NSS context of Wifi.
23 */
24struct nss_ctx_instance *nss_wifi_get_context()
25{
26 return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id];
27}
Bharath M Kumarcc666e92014-12-24 19:17:28 +053028
29/*
30 * nss_wifi_handler()
31 * Handle NSS -> HLOS messages for wifi
32 */
33static void nss_wifi_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
34{
35 struct nss_wifi_msg *ntm = (struct nss_wifi_msg *)ncm;
36 void *ctx;
37 nss_wifi_msg_callback_t cb;
38
39 nss_info("%p: NSS ->HLOS message for wifi\n", nss_ctx);
40
41 BUG_ON(((ncm->interface < NSS_WIFI_INTERFACE0) || (ncm->interface > NSS_WIFI_INTERFACE2)));
42
43 /*
44 * Is this a valid request/response packet?
45 */
46 if (ncm->type >= NSS_WIFI_MAX_MSG) {
Yu Huang8c107082017-07-24 14:58:26 -070047 nss_warning("%p: received invalid message %d for wifi interface", nss_ctx, ncm->type);
Bharath M Kumarcc666e92014-12-24 19:17:28 +053048 return;
49 }
50
Suruchi Agarwalef8a8702016-01-08 12:40:08 -080051 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_wifi_msg)) {
52 nss_warning("%p: Length of message is greater than required: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Bharath M Kumarcc666e92014-12-24 19:17:28 +053053 return;
54 }
55
56 /*
57 * Snoop messages for local driver and handle
58 */
59 switch (ntm->cm.type) {
60 case NSS_WIFI_STATS_MSG:
61 /*
62 * To create the old API gmac statistics, we use the new extended GMAC stats.
63 */
64 nss_wifi_stats_sync(nss_ctx, &ntm->msg.statsmsg, ncm->interface);
65 break;
66 }
67
68 /*
69 * Update the callback and app_data for NOTIFY messages, wifi sends all notify messages
70 * to the same callback/app_data.
71 */
72 if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) {
Stephen Wangaed46332016-12-12 17:29:03 -080073 ncm->cb = (nss_ptr_t)nss_ctx->nss_top->wifi_msg_callback;
Bharath M Kumarcc666e92014-12-24 19:17:28 +053074 }
75
76 /*
77 * Log failures
78 */
79 nss_core_log_msg_failures(nss_ctx, ncm);
80
81 /*
82 * Do we have a call back
83 */
84 if (!ncm->cb) {
85 nss_info("%p: cb null for wifi interface %d", nss_ctx, ncm->interface);
86 return;
87 }
88
89 /*
90 * Get callback & context
91 */
92 cb = (nss_wifi_msg_callback_t)ncm->cb;
Stephen Wang84e0e992016-09-07 12:31:40 -070093 ctx = nss_ctx->subsys_dp_register[ncm->interface].ndev;
Bharath M Kumarcc666e92014-12-24 19:17:28 +053094
95 /*
96 * call wifi msg callback
97 */
98 if (!ctx) {
99 nss_warning("%p: Event received for wifi interface %d before registration", nss_ctx, ncm->interface);
100 return;
101 }
102
103 cb(ctx, ntm);
104}
105
106/*
107 * nss_wifi_tx_msg
108 * Transmit a wifi message to NSS FW
109 */
110nss_tx_status_t nss_wifi_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_wifi_msg *msg)
111{
112 struct nss_wifi_msg *nm;
113 struct nss_cmn_msg *ncm = &msg->cm;
114 struct sk_buff *nbuf;
115 int32_t status;
116
117 NSS_VERIFY_CTX_MAGIC(nss_ctx);
118
119 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
120 nss_warning("%p: wifi message dropped as core not ready", nss_ctx);
121 return NSS_TX_FAILURE_NOT_READY;
122 }
123
124 if (ncm->type > NSS_WIFI_MAX_MSG) {
125 nss_warning("%p: wifi message type out of range: %d", nss_ctx, ncm->type);
126 return NSS_TX_FAILURE;
127 }
128
Suruchi Agarwalef8a8702016-01-08 12:40:08 -0800129 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_wifi_msg)) {
130 nss_warning("%p: wifi message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
Bharath M Kumarcc666e92014-12-24 19:17:28 +0530131 return NSS_TX_FAILURE;
132 }
133
134 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
135 if (unlikely(!nbuf)) {
136 spin_lock_bh(&nss_ctx->nss_top->stats_lock);
137 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]);
138 spin_unlock_bh(&nss_ctx->nss_top->stats_lock);
139 nss_warning("%p: wifi message dropped as command allocation failed", nss_ctx);
140 return NSS_TX_FAILURE;
141 }
142
143 /*
144 * Copy the message to our skb
145 */
146 nm = (struct nss_wifi_msg *)skb_put(nbuf, sizeof(struct nss_wifi_msg));
147 memcpy(nm, msg, sizeof(struct nss_wifi_msg));
148
149 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
150 if (status != NSS_CORE_STATUS_SUCCESS) {
151 dev_kfree_skb_any(nbuf);
152 nss_warning("%p: Unable to enqueue 'wifi message'", nss_ctx);
153 return NSS_TX_FAILURE;
154 }
155
Stephen Wang90c67de2016-04-26 15:15:59 -0700156 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
Bharath M Kumarcc666e92014-12-24 19:17:28 +0530157
158 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]);
159
160 return NSS_TX_SUCCESS;
161}
162
163/*
164 ****************************************
165 * Register/Unregister/Miscellaneous APIs
166 ****************************************
167 */
168
169/*
170 * nss_register_wifi_if()
171 * Register Wifi with nss driver
172 */
173struct nss_ctx_instance *nss_register_wifi_if(uint32_t if_num, nss_wifi_callback_t wifi_callback,
Radha krishna Simha Jiguru427d6032015-07-07 19:06:07 +0530174 nss_wifi_callback_t wifi_ext_callback,
Bharath M Kumarcc666e92014-12-24 19:17:28 +0530175 nss_wifi_msg_callback_t event_callback, struct net_device *netdev, uint32_t features)
176{
Stephen Wang84e0e992016-09-07 12:31:40 -0700177 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id];
Bharath M Kumarcc666e92014-12-24 19:17:28 +0530178
Stephen Wang84e0e992016-09-07 12:31:40 -0700179 nss_assert(nss_ctx);
180 nss_assert((if_num >= NSS_MAX_VIRTUAL_INTERFACES) && (if_num < NSS_MAX_NET_INTERFACES));
Bharath M Kumarcc666e92014-12-24 19:17:28 +0530181
Stephen Wang84e0e992016-09-07 12:31:40 -0700182 nss_info("%p: nss_register_wifi_if if_num %d wifictx %p", nss_ctx, if_num, netdev);
183
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700184 nss_core_register_subsys_dp(nss_ctx, if_num, wifi_callback, wifi_ext_callback, NULL, netdev, features);
Bharath M Kumarcc666e92014-12-24 19:17:28 +0530185
186 nss_top_main.wifi_msg_callback = event_callback;
187
Stephen Wang84e0e992016-09-07 12:31:40 -0700188 return nss_ctx;
Bharath M Kumarcc666e92014-12-24 19:17:28 +0530189}
190
191/*
192 * nss_unregister_wifi_if()
193 * Unregister wifi with nss driver
194 */
195void nss_unregister_wifi_if(uint32_t if_num)
196{
Stephen Wang84e0e992016-09-07 12:31:40 -0700197 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id];
Bharath M Kumarcc666e92014-12-24 19:17:28 +0530198
Stephen Wang84e0e992016-09-07 12:31:40 -0700199 nss_assert(nss_ctx);
200 nss_assert((if_num >= NSS_MAX_VIRTUAL_INTERFACES) && (if_num < NSS_MAX_NET_INTERFACES));
201
Radha krishna Simha Jiguruf85b5792017-11-28 14:52:40 +0530202 nss_ctx->nss_top->wifi_msg_callback = NULL;
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700203 nss_core_unregister_subsys_dp(nss_ctx, if_num);
Bharath M Kumarcc666e92014-12-24 19:17:28 +0530204}
205
206/*
207 * nss_wifi_register_handler()
208 * Register handle for notfication messages received on wifi interface
209 */
210void nss_wifi_register_handler(void )
211{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700212 struct nss_ctx_instance *nss_ctx = (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.wifi_handler_id];
213
214 nss_assert(nss_ctx);
215
Bharath M Kumarcc666e92014-12-24 19:17:28 +0530216 nss_info("nss_wifi_register_handler");
217
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700218 nss_core_register_handler(nss_ctx, NSS_WIFI_INTERFACE0, nss_wifi_handler, NULL);
219 nss_core_register_handler(nss_ctx, NSS_WIFI_INTERFACE1, nss_wifi_handler, NULL);
220 nss_core_register_handler(nss_ctx, NSS_WIFI_INTERFACE2, nss_wifi_handler, NULL);
Yu Huang8c107082017-07-24 14:58:26 -0700221
222 nss_wifi_stats_dentry_create();
Bharath M Kumarcc666e92014-12-24 19:17:28 +0530223}
224
ratheesh kannotheb2a0a82017-05-04 09:20:17 +0530225EXPORT_SYMBOL(nss_wifi_get_context);
Bharath M Kumarcc666e92014-12-24 19:17:28 +0530226EXPORT_SYMBOL(nss_wifi_tx_msg);
227EXPORT_SYMBOL(nss_register_wifi_if);
228EXPORT_SYMBOL(nss_unregister_wifi_if);