blob: 90f0c5d449cf709f64d644c41011067a596b29bd [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{
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 Gupta24a1ccb2018-02-26 18:32:35 +0530199 * nss_ppe_tx_ipsec_config_msg
200 * API to send inline IPsec port configure message to NSS FW
201 */
202nss_tx_status_t nss_ppe_tx_ipsec_config_msg(uint32_t nss_ifnum, uint32_t vsi_num, uint16_t mtu)
203{
204 struct nss_ctx_instance *nss_ctx = nss_ppe_get_context();
205 struct nss_ppe_msg npm = {0};
206
207 if (!nss_ctx) {
208 nss_warning("Can't get nss context\n");
209 return NSS_TX_FAILURE;
210 }
211
212 if (vsi_num >= NSS_PPE_VSI_NUM_MAX) {
213 nss_warning("Invalid vsi number:%u\n", vsi_num);
214 return NSS_TX_FAILURE;
215 }
216
217 nss_ppe_msg_init(&npm, NSS_PPE_INTERFACE, NSS_PPE_MSG_IPSEC_PORT_CONFIG,
218 sizeof(struct nss_ppe_ipsec_port_config_msg), NULL, NULL);
219
220 npm.msg.ipsec_config.nss_ifnum = nss_ifnum;
221 npm.msg.ipsec_config.vsi_num = vsi_num;
222 npm.msg.ipsec_config.mtu = mtu;
223
224 return nss_ppe_tx_msg_sync(nss_ctx, &npm);
225}
226EXPORT_SYMBOL(nss_ppe_tx_ipsec_config_msg);
227
228/*
229 * nss_ppe_tx_ipsec_mtu_msg
230 * API to send IPsec port MTU change message to NSS FW
231 */
232nss_tx_status_t nss_ppe_tx_ipsec_mtu_msg(uint32_t nss_ifnum, uint16_t mtu)
233{
234 struct nss_ctx_instance *nss_ctx = nss_ppe_get_context();
235 struct nss_ppe_msg npm = {0};
236
237 if (!nss_ctx) {
238 nss_warning("Can't get nss context\n");
239 return NSS_TX_FAILURE;
240 }
241
242 nss_ppe_msg_init(&npm, NSS_PPE_INTERFACE, NSS_PPE_MSG_IPSEC_PORT_MTU_CHANGE,
243 sizeof(struct nss_ppe_ipsec_port_mtu_msg), NULL, NULL);
244
245 npm.msg.ipsec_mtu.nss_ifnum = nss_ifnum;
246 npm.msg.ipsec_mtu.mtu = mtu;
247
248 return nss_ppe_tx_msg_sync(nss_ctx, &npm);
249}
250EXPORT_SYMBOL(nss_ppe_tx_ipsec_mtu_msg);
251
252/*
253 * nss_ppe_tx_ipsec_add_intf_msg
254 * API to attach NSS interface to IPsec port
255 */
256nss_tx_status_t nss_ppe_tx_ipsec_add_intf_msg(uint32_t nss_ifnum)
257{
258 struct nss_ctx_instance *nss_ctx = nss_ppe_get_context();
259 struct nss_ppe_msg npm = {0};
260
261 if (!nss_ctx) {
262 nss_warning("Can't get nss context\n");
263 return NSS_TX_FAILURE;
264 }
265
266 nss_ppe_msg_init(&npm, NSS_PPE_INTERFACE, NSS_PPE_MSG_IPSEC_ADD_INTF,
267 sizeof(struct nss_ppe_ipsec_add_intf_msg), NULL, NULL);
268
269 npm.msg.ipsec_addif.nss_ifnum = nss_ifnum;
270
271 return nss_ppe_tx_msg_sync(nss_ctx, &npm);
272}
273EXPORT_SYMBOL(nss_ppe_tx_ipsec_add_intf_msg);
274
275/*
276 * nss_ppe_tx_ipsec_del_intf_msg
277 * API to detach NSS interface to IPsec port
278 */
279nss_tx_status_t nss_ppe_tx_ipsec_del_intf_msg(uint32_t nss_ifnum)
280{
281 struct nss_ctx_instance *nss_ctx = nss_ppe_get_context();
282 struct nss_ppe_msg npm = {0};
283
284 if (!nss_ctx) {
285 nss_warning("Can't get nss context\n");
286 return NSS_TX_FAILURE;
287 }
288
289 nss_ppe_msg_init(&npm, NSS_PPE_INTERFACE, NSS_PPE_MSG_IPSEC_DEL_INTF,
290 sizeof(struct nss_ppe_ipsec_del_intf_msg), NULL, NULL);
291
292 npm.msg.ipsec_delif.nss_ifnum = nss_ifnum;
293
294 return nss_ppe_tx_msg_sync(nss_ctx, &npm);
295}
296EXPORT_SYMBOL(nss_ppe_tx_ipsec_del_intf_msg);
297
298/*
Amit Gupta316729b2016-08-12 12:21:15 +0530299 * nss_ppe_handler()
Amit Gupta1263b082017-07-06 14:22:57 +0530300 * Handle NSS -> HLOS messages for ppe
Amit Gupta316729b2016-08-12 12:21:15 +0530301 */
302static void nss_ppe_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, __attribute__((unused))void *app_data)
303{
304 struct nss_ppe_msg *msg = (struct nss_ppe_msg *)ncm;
Amit Gupta1263b082017-07-06 14:22:57 +0530305 void *ctx;
Amit Gupta316729b2016-08-12 12:21:15 +0530306
Amit Gupta1263b082017-07-06 14:22:57 +0530307 nss_ppe_msg_callback_t cb;
308
309 nss_trace("nss_ctx: %p ppe msg: %p\n", nss_ctx, msg);
Amit Gupta316729b2016-08-12 12:21:15 +0530310 BUG_ON(!nss_ppe_verify_ifnum(ncm->interface));
311
312 /*
313 * Is this a valid request/response packet?
314 */
315 if (ncm->type >= NSS_PPE_MSG_MAX) {
Amit Gupta1263b082017-07-06 14:22:57 +0530316 nss_warning("%p: received invalid message %d for PPE interface\n", nss_ctx, ncm->type);
Amit Gupta316729b2016-08-12 12:21:15 +0530317 return;
318 }
319
320 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_ppe_msg)) {
Amit Gupta1263b082017-07-06 14:22:57 +0530321 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 +0530322 return;
323 }
324
325 switch (msg->cm.type) {
326 case NSS_PPE_MSG_SYNC_STATS:
327 /*
328 * session debug stats embeded in session stats msg
329 */
330 nss_ppe_stats_sync(nss_ctx, &msg->msg.stats, ncm->interface);
Amit Gupta1263b082017-07-06 14:22:57 +0530331 return;
Amit Gupta316729b2016-08-12 12:21:15 +0530332 }
Amit Gupta316729b2016-08-12 12:21:15 +0530333
Amit Gupta1263b082017-07-06 14:22:57 +0530334 /*
335 * Log failures
336 */
337 nss_core_log_msg_failures(nss_ctx, ncm);
338
339 /*
340 * Do we have a call back
341 */
342 if (!ncm->cb) {
343 return;
344 }
345
346 /*
347 * callback
348 */
349 cb = (nss_ppe_msg_callback_t)ncm->cb;
350 ctx = (void *)ncm->app_data;
351
352 cb(ctx, msg);
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700353}
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700354
355/*
Amit Gupta316729b2016-08-12 12:21:15 +0530356 * nss_ppe_register_handler()
357 * debugfs stats msg handler received on static ppe interface
358 *
359 * TODO: Export API so that others can also read PPE stats.
360 */
361void nss_ppe_register_handler(void)
362{
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700363 struct nss_ctx_instance *nss_ctx = nss_ppe_get_context();
364
365 nss_core_register_handler(nss_ctx, NSS_PPE_INTERFACE, nss_ppe_handler, NULL);
Yu Huang8c107082017-07-24 14:58:26 -0700366
367 nss_ppe_stats_dentry_create();
Amit Gupta316729b2016-08-12 12:21:15 +0530368}
369
370/*
371 * nss_ppe_free()
372 * Uninitialize PPE base
373 */
374void nss_ppe_free(void)
375{
376 /*
377 * Check if PPE base is already uninitialized.
378 */
379 if (!ppe_pvt.ppe_base) {
380 return;
381 }
382
383 /*
384 * Unmap PPE base address
385 */
386 iounmap(ppe_pvt.ppe_base);
387 ppe_pvt.ppe_base = NULL;
388
389 spin_lock_bh(&nss_ppe_stats_lock);
390 nss_ppe_debug_stats.valid = false;
391 nss_ppe_debug_stats.if_num = 0;
392 nss_ppe_debug_stats.if_index = 0;
393 spin_unlock_bh(&nss_ppe_stats_lock);
394}
395
396/*
397 * nss_ppe_init()
398 * Initialize PPE base
399 */
400void nss_ppe_init(void)
401{
402 /*
403 * Check if PPE base is already initialized.
404 */
405 if (ppe_pvt.ppe_base) {
406 return;
407 }
408
409 /*
410 * Get the PPE base address
411 */
412 ppe_pvt.ppe_base = ioremap_nocache(PPE_BASE_ADDR, PPE_REG_SIZE);
413 if (!ppe_pvt.ppe_base) {
414 nss_warning("DRV can't get PPE base address\n");
415 return;
416 }
417
418 spin_lock_bh(&nss_ppe_stats_lock);
419 nss_ppe_debug_stats.valid = true;
420 nss_ppe_debug_stats.if_num = 0;
421 nss_ppe_debug_stats.if_index = 0;
422 spin_unlock_bh(&nss_ppe_stats_lock);
Amit Gupta1263b082017-07-06 14:22:57 +0530423
424 sema_init(&ppe_pvt.sem, 1);
425 init_completion(&ppe_pvt.complete);
Amit Gupta316729b2016-08-12 12:21:15 +0530426}