blob: 33a3d19dcc87c52c49b7fb305134323a338298d6 [file] [log] [blame]
Amit Gupta316729b2016-08-12 12:21:15 +05301/*
2 **************************************************************************
Amit Gupta24a1ccb2018-02-26 18:32:35 +05303 * Copyright (c) 2016-2018, 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{
Amit Gupta1263b082017-07-06 14:22:57 +053064 struct nss_cmn_msg *ncm = &msg->cm;
Amit Gupta1263b082017-07-06 14:22:57 +053065
66 /*
67 * Sanity check the message
68 */
69 if (ncm->type >= NSS_PPE_MSG_MAX) {
70 nss_warning("%p: message type out of range: %d\n", nss_ctx, ncm->type);
71 return NSS_TX_FAILURE;
72 }
73
Stephen Wang3e2dbd12018-03-14 17:28:17 -070074 if (!nss_ppe_verify_ifnum(ncm->interface)) {
75 nss_warning("%p: invalid interface %d\n", nss_ctx, ncm->interface);
Amit Gupta1263b082017-07-06 14:22:57 +053076 return NSS_TX_FAILURE;
77 }
78
Stephen Wang3e2dbd12018-03-14 17:28:17 -070079 return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE);
Amit Gupta1263b082017-07-06 14:22:57 +053080}
81EXPORT_SYMBOL(nss_ppe_tx_msg);
82
83/*
84 * nss_ppe_tx_msg_sync()
85 * Transmit a ppe message to NSS firmware synchronously.
86 */
87nss_tx_status_t nss_ppe_tx_msg_sync(struct nss_ctx_instance *nss_ctx, struct nss_ppe_msg *npm)
88{
89 nss_tx_status_t status;
90 int ret = 0;
91
92 down(&ppe_pvt.sem);
93 ppe_pvt.cb = (void *)npm->cm.cb;
94 ppe_pvt.app_data = (void *)npm->cm.app_data;
95
96 npm->cm.cb = (nss_ptr_t)nss_ppe_callback;
97 npm->cm.app_data = (nss_ptr_t)NULL;
98
99 status = nss_ppe_tx_msg(nss_ctx, npm);
100 if (status != NSS_TX_SUCCESS) {
101 nss_warning("%p: ppe_tx_msg failed\n", nss_ctx);
102 up(&ppe_pvt.sem);
103 return status;
104 }
105
106 ret = wait_for_completion_timeout(&ppe_pvt.complete, msecs_to_jiffies(NSS_PPE_TX_TIMEOUT));
107 if (!ret) {
108 nss_warning("%p: ppe msg tx failed due to timeout\n", nss_ctx);
109 ppe_pvt.response = NSS_TX_FAILURE;
110 }
111
112 status = ppe_pvt.response;
113 up(&ppe_pvt.sem);
114 return status;
115}
116EXPORT_SYMBOL(nss_ppe_tx_msg_sync);
117
118/*
119 * nss_ppe_get_context()
120 * Get NSS context instance for ppe
121 */
122struct nss_ctx_instance *nss_ppe_get_context(void)
123{
124 return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.ppe_handler_id];
125}
126EXPORT_SYMBOL(nss_ppe_get_context);
127
128/*
129 * nss_ppe_msg_init()
130 * Initialize nss_ppe_msg.
131 */
132void nss_ppe_msg_init(struct nss_ppe_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data)
133{
134 nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data);
135}
136EXPORT_SYMBOL(nss_ppe_msg_init);
137
138/*
139 * nss_ppe_tx_l2_exception_msg
140 * API to send vsi assign message to NSS FW
141 */
142nss_tx_status_t nss_ppe_tx_l2_exception_msg(uint32_t if_num, bool exception_enable)
143{
144 struct nss_ctx_instance *nss_ctx = nss_ppe_get_context();
145 struct nss_ppe_msg npm;
146
147 if (!nss_ctx) {
148 nss_warning("Can't get nss context\n");
149 return NSS_TX_FAILURE;
150 }
151
Amit Gupta1263b082017-07-06 14:22:57 +0530152 nss_ppe_msg_init(&npm, if_num, NSS_PPE_MSG_L2_EXCEPTION,
153 sizeof(struct nss_ppe_l2_exception_msg), NULL, NULL);
154
155 npm.msg.l2_exception.l2_exception_enable = exception_enable;
156
157 return nss_ppe_tx_msg_sync(nss_ctx, &npm);
158}
159EXPORT_SYMBOL(nss_ppe_tx_l2_exception_msg);
160
161/*
Amit Gupta24a1ccb2018-02-26 18:32:35 +0530162 * nss_ppe_tx_ipsec_config_msg
163 * API to send inline IPsec port configure message to NSS FW
164 */
165nss_tx_status_t nss_ppe_tx_ipsec_config_msg(uint32_t nss_ifnum, uint32_t vsi_num, uint16_t mtu)
166{
167 struct nss_ctx_instance *nss_ctx = nss_ppe_get_context();
168 struct nss_ppe_msg npm = {0};
169
170 if (!nss_ctx) {
171 nss_warning("Can't get nss context\n");
172 return NSS_TX_FAILURE;
173 }
174
175 if (vsi_num >= NSS_PPE_VSI_NUM_MAX) {
176 nss_warning("Invalid vsi number:%u\n", vsi_num);
177 return NSS_TX_FAILURE;
178 }
179
180 nss_ppe_msg_init(&npm, NSS_PPE_INTERFACE, NSS_PPE_MSG_IPSEC_PORT_CONFIG,
181 sizeof(struct nss_ppe_ipsec_port_config_msg), NULL, NULL);
182
183 npm.msg.ipsec_config.nss_ifnum = nss_ifnum;
184 npm.msg.ipsec_config.vsi_num = vsi_num;
185 npm.msg.ipsec_config.mtu = mtu;
186
187 return nss_ppe_tx_msg_sync(nss_ctx, &npm);
188}
189EXPORT_SYMBOL(nss_ppe_tx_ipsec_config_msg);
190
191/*
192 * nss_ppe_tx_ipsec_mtu_msg
193 * API to send IPsec port MTU change message to NSS FW
194 */
195nss_tx_status_t nss_ppe_tx_ipsec_mtu_msg(uint32_t nss_ifnum, uint16_t mtu)
196{
197 struct nss_ctx_instance *nss_ctx = nss_ppe_get_context();
198 struct nss_ppe_msg npm = {0};
199
200 if (!nss_ctx) {
201 nss_warning("Can't get nss context\n");
202 return NSS_TX_FAILURE;
203 }
204
205 nss_ppe_msg_init(&npm, NSS_PPE_INTERFACE, NSS_PPE_MSG_IPSEC_PORT_MTU_CHANGE,
206 sizeof(struct nss_ppe_ipsec_port_mtu_msg), NULL, NULL);
207
208 npm.msg.ipsec_mtu.nss_ifnum = nss_ifnum;
209 npm.msg.ipsec_mtu.mtu = mtu;
210
211 return nss_ppe_tx_msg_sync(nss_ctx, &npm);
212}
213EXPORT_SYMBOL(nss_ppe_tx_ipsec_mtu_msg);
214
215/*
216 * nss_ppe_tx_ipsec_add_intf_msg
217 * API to attach NSS interface to IPsec port
218 */
219nss_tx_status_t nss_ppe_tx_ipsec_add_intf_msg(uint32_t nss_ifnum)
220{
221 struct nss_ctx_instance *nss_ctx = nss_ppe_get_context();
222 struct nss_ppe_msg npm = {0};
223
224 if (!nss_ctx) {
225 nss_warning("Can't get nss context\n");
226 return NSS_TX_FAILURE;
227 }
228
229 nss_ppe_msg_init(&npm, NSS_PPE_INTERFACE, NSS_PPE_MSG_IPSEC_ADD_INTF,
230 sizeof(struct nss_ppe_ipsec_add_intf_msg), NULL, NULL);
231
232 npm.msg.ipsec_addif.nss_ifnum = nss_ifnum;
233
234 return nss_ppe_tx_msg_sync(nss_ctx, &npm);
235}
236EXPORT_SYMBOL(nss_ppe_tx_ipsec_add_intf_msg);
237
238/*
239 * nss_ppe_tx_ipsec_del_intf_msg
240 * API to detach NSS interface to IPsec port
241 */
242nss_tx_status_t nss_ppe_tx_ipsec_del_intf_msg(uint32_t nss_ifnum)
243{
244 struct nss_ctx_instance *nss_ctx = nss_ppe_get_context();
245 struct nss_ppe_msg npm = {0};
246
247 if (!nss_ctx) {
248 nss_warning("Can't get nss context\n");
249 return NSS_TX_FAILURE;
250 }
251
252 nss_ppe_msg_init(&npm, NSS_PPE_INTERFACE, NSS_PPE_MSG_IPSEC_DEL_INTF,
253 sizeof(struct nss_ppe_ipsec_del_intf_msg), NULL, NULL);
254
255 npm.msg.ipsec_delif.nss_ifnum = nss_ifnum;
256
257 return nss_ppe_tx_msg_sync(nss_ctx, &npm);
258}
259EXPORT_SYMBOL(nss_ppe_tx_ipsec_del_intf_msg);
260
261/*
Amit Gupta316729b2016-08-12 12:21:15 +0530262 * nss_ppe_handler()
Amit Gupta1263b082017-07-06 14:22:57 +0530263 * Handle NSS -> HLOS messages for ppe
Amit Gupta316729b2016-08-12 12:21:15 +0530264 */
265static void nss_ppe_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
266{
267 struct nss_ppe_msg *msg = (struct nss_ppe_msg *)ncm;
Amit Gupta1263b082017-07-06 14:22:57 +0530268 void *ctx;
Amit Gupta316729b2016-08-12 12:21:15 +0530269
Amit Gupta1263b082017-07-06 14:22:57 +0530270 nss_ppe_msg_callback_t cb;
271
272 nss_trace("nss_ctx: %p ppe msg: %p\n", nss_ctx, msg);
Amit Gupta316729b2016-08-12 12:21:15 +0530273 BUG_ON(!nss_ppe_verify_ifnum(ncm->interface));
274
275 /*
276 * Is this a valid request/response packet?
277 */
278 if (ncm->type >= NSS_PPE_MSG_MAX) {
Amit Gupta1263b082017-07-06 14:22:57 +0530279 nss_warning("%p: received invalid message %d for PPE interface\n", nss_ctx, ncm->type);
Amit Gupta316729b2016-08-12 12:21:15 +0530280 return;
281 }
282
283 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ppe_msg)) {
Amit Gupta1263b082017-07-06 14:22:57 +0530284 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 +0530285 return;
286 }
287
288 switch (msg->cm.type) {
289 case NSS_PPE_MSG_SYNC_STATS:
290 /*
291 * session debug stats embeded in session stats msg
292 */
293 nss_ppe_stats_sync(nss_ctx, &msg->msg.stats, ncm->interface);
Amit Gupta1263b082017-07-06 14:22:57 +0530294 return;
Amit Gupta316729b2016-08-12 12:21:15 +0530295 }
Amit Gupta316729b2016-08-12 12:21:15 +0530296
Amit Gupta1263b082017-07-06 14:22:57 +0530297 /*
298 * Log failures
299 */
300 nss_core_log_msg_failures(nss_ctx, ncm);
301
302 /*
303 * Do we have a call back
304 */
305 if (!ncm->cb) {
306 return;
307 }
308
309 /*
310 * callback
311 */
312 cb = (nss_ppe_msg_callback_t)ncm->cb;
313 ctx = (void *)ncm->app_data;
314
315 cb(ctx, msg);
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700316}
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700317
318/*
Amit Gupta316729b2016-08-12 12:21:15 +0530319 * nss_ppe_register_handler()
320 * debugfs stats msg handler received on static ppe interface
321 *
322 * TODO: Export API so that others can also read PPE stats.
323 */
324void nss_ppe_register_handler(void)
325{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700326 struct nss_ctx_instance *nss_ctx = nss_ppe_get_context();
327
328 nss_core_register_handler(nss_ctx, NSS_PPE_INTERFACE, nss_ppe_handler, NULL);
Yu Huang8c107082017-07-24 14:58:26 -0700329
330 nss_ppe_stats_dentry_create();
Amit Gupta316729b2016-08-12 12:21:15 +0530331}
332
333/*
334 * nss_ppe_free()
335 * Uninitialize PPE base
336 */
337void nss_ppe_free(void)
338{
339 /*
340 * Check if PPE base is already uninitialized.
341 */
342 if (!ppe_pvt.ppe_base) {
343 return;
344 }
345
346 /*
347 * Unmap PPE base address
348 */
349 iounmap(ppe_pvt.ppe_base);
350 ppe_pvt.ppe_base = NULL;
351
352 spin_lock_bh(&nss_ppe_stats_lock);
353 nss_ppe_debug_stats.valid = false;
354 nss_ppe_debug_stats.if_num = 0;
355 nss_ppe_debug_stats.if_index = 0;
356 spin_unlock_bh(&nss_ppe_stats_lock);
357}
358
359/*
360 * nss_ppe_init()
361 * Initialize PPE base
362 */
363void nss_ppe_init(void)
364{
365 /*
366 * Check if PPE base is already initialized.
367 */
368 if (ppe_pvt.ppe_base) {
369 return;
370 }
371
372 /*
373 * Get the PPE base address
374 */
375 ppe_pvt.ppe_base = ioremap_nocache(PPE_BASE_ADDR, PPE_REG_SIZE);
376 if (!ppe_pvt.ppe_base) {
377 nss_warning("DRV can't get PPE base address\n");
378 return;
379 }
380
381 spin_lock_bh(&nss_ppe_stats_lock);
382 nss_ppe_debug_stats.valid = true;
383 nss_ppe_debug_stats.if_num = 0;
384 nss_ppe_debug_stats.if_index = 0;
385 spin_unlock_bh(&nss_ppe_stats_lock);
Amit Gupta1263b082017-07-06 14:22:57 +0530386
387 sema_init(&ppe_pvt.sem, 1);
388 init_completion(&ppe_pvt.complete);
Amit Gupta316729b2016-08-12 12:21:15 +0530389}