blob: a914880bf41bd05f39c5a3d1610874ddb4e1c0a2 [file] [log] [blame]
Amit Gupta316729b2016-08-12 12:21:15 +05301/*
2 **************************************************************************
Thomas Wufc4d9fd2017-03-22 10:15:30 -07003 * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
Amit Gupta316729b2016-08-12 12:21:15 +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_ppe.h"
Yu Huang8c107082017-07-24 14:58:26 -070018#include "nss_ppe_stats.h"
Amit Gupta79c1c202017-06-30 15:28:13 +053019
Amit Gupta6c0f6412017-11-20 15:42:43 +053020DEFINE_SPINLOCK(nss_ppe_stats_lock);
21
Amit Guptacb1696b2017-11-13 13:09:06 +053022struct nss_ppe_stats_debug nss_ppe_debug_stats;
Amit Gupta6c0f6412017-11-20 15:42:43 +053023struct nss_ppe_pvt ppe_pvt;
Amit Guptacb1696b2017-11-13 13:09:06 +053024
Amit Gupta316729b2016-08-12 12:21:15 +053025/*
26 * nss_ppe_verify_ifnum()
27 * Verify PPE interface number.
28 */
29static inline bool nss_ppe_verify_ifnum(int if_num)
30{
31 return nss_is_dynamic_interface(if_num) || (if_num == NSS_PPE_INTERFACE);
32}
33
34/*
Amit Gupta1263b082017-07-06 14:22:57 +053035 * nss_ppe_callback()
36 * Callback to handle the completion of NSS->HLOS messages.
37 */
38static void nss_ppe_callback(void *app_data, struct nss_ppe_msg *npm)
39{
40 nss_ppe_msg_callback_t callback = (nss_ppe_msg_callback_t)ppe_pvt.cb;
41 void *data = ppe_pvt.app_data;
42
43 ppe_pvt.response = NSS_TX_SUCCESS;
44 ppe_pvt.cb = NULL;
45 ppe_pvt.app_data = NULL;
46
47 if (npm->cm.response != NSS_CMN_RESPONSE_ACK) {
48 nss_warning("ppe error response %d\n", npm->cm.response);
49 ppe_pvt.response = npm->cm.response;
50 }
51
52 if (callback) {
53 callback(data, npm);
54 }
55 complete(&ppe_pvt.complete);
56}
57
58/*
59 * nss_ppe_tx_msg()
60 * Transmit a ppe message to NSSFW
61 */
62nss_tx_status_t nss_ppe_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_ppe_msg *msg)
63{
64 struct nss_ppe_msg *nm;
65 struct nss_cmn_msg *ncm = &msg->cm;
66 struct sk_buff *nbuf;
67 int32_t status;
68
69 NSS_VERIFY_CTX_MAGIC(nss_ctx);
70 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
71 nss_warning("%p: ppe msg dropped as core not ready\n", nss_ctx);
72 return NSS_TX_FAILURE_NOT_READY;
73 }
74
75 /*
76 * Sanity check the message
77 */
78 if (ncm->type >= NSS_PPE_MSG_MAX) {
79 nss_warning("%p: message type out of range: %d\n", nss_ctx, ncm->type);
80 return NSS_TX_FAILURE;
81 }
82
83 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ppe_msg)) {
84 nss_warning("%p: message length is invalid: %d\n", nss_ctx, nss_cmn_get_msg_len(ncm));
85 return NSS_TX_FAILURE;
86 }
87
88 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
89 if (unlikely(!nbuf)) {
90 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]);
91 nss_warning("%p: msg dropped as command allocation failed\n", nss_ctx);
92 return NSS_TX_FAILURE;
93 }
94
95 /*
96 * Copy the message to our skb
97 */
98 nm = (struct nss_ppe_msg *)skb_put(nbuf, sizeof(struct nss_ppe_msg));
99 memcpy(nm, msg, sizeof(struct nss_ppe_msg));
100
101 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
102 if (status != NSS_CORE_STATUS_SUCCESS) {
103 dev_kfree_skb_any(nbuf);
104 nss_warning("%p: Unable to enqueue 'ppe message'\n", nss_ctx);
105 return NSS_TX_FAILURE;
106 }
107
108 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
109
110 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]);
111 return NSS_TX_SUCCESS;
112}
113EXPORT_SYMBOL(nss_ppe_tx_msg);
114
115/*
116 * nss_ppe_tx_msg_sync()
117 * Transmit a ppe message to NSS firmware synchronously.
118 */
119nss_tx_status_t nss_ppe_tx_msg_sync(struct nss_ctx_instance *nss_ctx, struct nss_ppe_msg *npm)
120{
121 nss_tx_status_t status;
122 int ret = 0;
123
124 down(&ppe_pvt.sem);
125 ppe_pvt.cb = (void *)npm->cm.cb;
126 ppe_pvt.app_data = (void *)npm->cm.app_data;
127
128 npm->cm.cb = (nss_ptr_t)nss_ppe_callback;
129 npm->cm.app_data = (nss_ptr_t)NULL;
130
131 status = nss_ppe_tx_msg(nss_ctx, npm);
132 if (status != NSS_TX_SUCCESS) {
133 nss_warning("%p: ppe_tx_msg failed\n", nss_ctx);
134 up(&ppe_pvt.sem);
135 return status;
136 }
137
138 ret = wait_for_completion_timeout(&ppe_pvt.complete, msecs_to_jiffies(NSS_PPE_TX_TIMEOUT));
139 if (!ret) {
140 nss_warning("%p: ppe msg tx failed due to timeout\n", nss_ctx);
141 ppe_pvt.response = NSS_TX_FAILURE;
142 }
143
144 status = ppe_pvt.response;
145 up(&ppe_pvt.sem);
146 return status;
147}
148EXPORT_SYMBOL(nss_ppe_tx_msg_sync);
149
150/*
151 * nss_ppe_get_context()
152 * Get NSS context instance for ppe
153 */
154struct nss_ctx_instance *nss_ppe_get_context(void)
155{
156 return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.ppe_handler_id];
157}
158EXPORT_SYMBOL(nss_ppe_get_context);
159
160/*
161 * nss_ppe_msg_init()
162 * Initialize nss_ppe_msg.
163 */
164void nss_ppe_msg_init(struct nss_ppe_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data)
165{
166 nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data);
167}
168EXPORT_SYMBOL(nss_ppe_msg_init);
169
170/*
171 * nss_ppe_tx_l2_exception_msg
172 * API to send vsi assign message to NSS FW
173 */
174nss_tx_status_t nss_ppe_tx_l2_exception_msg(uint32_t if_num, bool exception_enable)
175{
176 struct nss_ctx_instance *nss_ctx = nss_ppe_get_context();
177 struct nss_ppe_msg npm;
178
179 if (!nss_ctx) {
180 nss_warning("Can't get nss context\n");
181 return NSS_TX_FAILURE;
182 }
183
184 if (!nss_ppe_verify_ifnum(if_num)) {
185 nss_warning("%p: invalid interface %d\n", nss_ctx, if_num);
186 return NSS_TX_FAILURE;
187 }
188
189 nss_ppe_msg_init(&npm, if_num, NSS_PPE_MSG_L2_EXCEPTION,
190 sizeof(struct nss_ppe_l2_exception_msg), NULL, NULL);
191
192 npm.msg.l2_exception.l2_exception_enable = exception_enable;
193
194 return nss_ppe_tx_msg_sync(nss_ctx, &npm);
195}
196EXPORT_SYMBOL(nss_ppe_tx_l2_exception_msg);
197
198/*
Amit Gupta316729b2016-08-12 12:21:15 +0530199 * nss_ppe_handler()
Amit Gupta1263b082017-07-06 14:22:57 +0530200 * Handle NSS -> HLOS messages for ppe
Amit Gupta316729b2016-08-12 12:21:15 +0530201 */
202static void nss_ppe_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
203{
204 struct nss_ppe_msg *msg = (struct nss_ppe_msg *)ncm;
Amit Gupta1263b082017-07-06 14:22:57 +0530205 void *ctx;
Amit Gupta316729b2016-08-12 12:21:15 +0530206
Amit Gupta1263b082017-07-06 14:22:57 +0530207 nss_ppe_msg_callback_t cb;
208
209 nss_trace("nss_ctx: %p ppe msg: %p\n", nss_ctx, msg);
Amit Gupta316729b2016-08-12 12:21:15 +0530210 BUG_ON(!nss_ppe_verify_ifnum(ncm->interface));
211
212 /*
213 * Is this a valid request/response packet?
214 */
215 if (ncm->type >= NSS_PPE_MSG_MAX) {
Amit Gupta1263b082017-07-06 14:22:57 +0530216 nss_warning("%p: received invalid message %d for PPE interface\n", nss_ctx, ncm->type);
Amit Gupta316729b2016-08-12 12:21:15 +0530217 return;
218 }
219
220 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ppe_msg)) {
Amit Gupta1263b082017-07-06 14:22:57 +0530221 nss_warning("%p: Length of message is greater than required: %d\n", nss_ctx, nss_cmn_get_msg_len(ncm));
Amit Gupta316729b2016-08-12 12:21:15 +0530222 return;
223 }
224
225 switch (msg->cm.type) {
226 case NSS_PPE_MSG_SYNC_STATS:
227 /*
228 * session debug stats embeded in session stats msg
229 */
230 nss_ppe_stats_sync(nss_ctx, &msg->msg.stats, ncm->interface);
Amit Gupta1263b082017-07-06 14:22:57 +0530231 return;
Amit Gupta316729b2016-08-12 12:21:15 +0530232 }
Amit Gupta316729b2016-08-12 12:21:15 +0530233
Amit Gupta1263b082017-07-06 14:22:57 +0530234 /*
235 * Log failures
236 */
237 nss_core_log_msg_failures(nss_ctx, ncm);
238
239 /*
240 * Do we have a call back
241 */
242 if (!ncm->cb) {
243 return;
244 }
245
246 /*
247 * callback
248 */
249 cb = (nss_ppe_msg_callback_t)ncm->cb;
250 ctx = (void *)ncm->app_data;
251
252 cb(ctx, msg);
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700253}
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700254
255/*
Amit Gupta316729b2016-08-12 12:21:15 +0530256 * nss_ppe_register_handler()
257 * debugfs stats msg handler received on static ppe interface
258 *
259 * TODO: Export API so that others can also read PPE stats.
260 */
261void nss_ppe_register_handler(void)
262{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700263 struct nss_ctx_instance *nss_ctx = nss_ppe_get_context();
264
265 nss_core_register_handler(nss_ctx, NSS_PPE_INTERFACE, nss_ppe_handler, NULL);
Yu Huang8c107082017-07-24 14:58:26 -0700266
267 nss_ppe_stats_dentry_create();
Amit Gupta316729b2016-08-12 12:21:15 +0530268}
269
270/*
271 * nss_ppe_free()
272 * Uninitialize PPE base
273 */
274void nss_ppe_free(void)
275{
276 /*
277 * Check if PPE base is already uninitialized.
278 */
279 if (!ppe_pvt.ppe_base) {
280 return;
281 }
282
283 /*
284 * Unmap PPE base address
285 */
286 iounmap(ppe_pvt.ppe_base);
287 ppe_pvt.ppe_base = NULL;
288
289 spin_lock_bh(&nss_ppe_stats_lock);
290 nss_ppe_debug_stats.valid = false;
291 nss_ppe_debug_stats.if_num = 0;
292 nss_ppe_debug_stats.if_index = 0;
293 spin_unlock_bh(&nss_ppe_stats_lock);
294}
295
296/*
297 * nss_ppe_init()
298 * Initialize PPE base
299 */
300void nss_ppe_init(void)
301{
302 /*
303 * Check if PPE base is already initialized.
304 */
305 if (ppe_pvt.ppe_base) {
306 return;
307 }
308
309 /*
310 * Get the PPE base address
311 */
312 ppe_pvt.ppe_base = ioremap_nocache(PPE_BASE_ADDR, PPE_REG_SIZE);
313 if (!ppe_pvt.ppe_base) {
314 nss_warning("DRV can't get PPE base address\n");
315 return;
316 }
317
318 spin_lock_bh(&nss_ppe_stats_lock);
319 nss_ppe_debug_stats.valid = true;
320 nss_ppe_debug_stats.if_num = 0;
321 nss_ppe_debug_stats.if_index = 0;
322 spin_unlock_bh(&nss_ppe_stats_lock);
Amit Gupta1263b082017-07-06 14:22:57 +0530323
324 sema_init(&ppe_pvt.sem, 1);
325 init_completion(&ppe_pvt.complete);
Amit Gupta316729b2016-08-12 12:21:15 +0530326}