blob: 553af629403e85b760976790a2ff705da0cfbddc [file] [log] [blame]
Ratheesh Kannoth65edb2d2020-01-23 09:08:46 +05301/*
2 **************************************************************************
3 * Copyright (c) 2020, The Linux Foundation. All rights reserved.
4 *
5 * Permission to use, copy, modify, and/or distribute this software for any
6 * purpose with or without fee is hereby granted, provided that the above
7 * copyright notice and this permission notice appear in all copies.
8 *
9 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE
16 **************************************************************************
17 */
18
19#include "nss_tx_rx_common.h"
20#include "nss_tls_log.h"
21
22#define NSS_TLS_INTERFACE_MAX_LONG BITS_TO_LONGS(NSS_MAX_NET_INTERFACES)
23#define NSS_TLS_STATS_MAX_LINES (NSS_STATS_NODE_MAX + 32)
24#define NSS_TLS_STATS_SIZE_PER_IF (NSS_STATS_MAX_STR_LENGTH * NSS_TLS_STATS_MAX_LINES)
25#define NSS_TLS_TX_TIMEOUT 3000 /* 3 Seconds */
26
27/*
28 * Private data structure for handling synchronous messaging.
29 */
30static struct nss_tls_pvt {
31 struct semaphore sem;
32 struct completion complete;
33 struct nss_tls_msg ntcm;
34 unsigned long if_map[NSS_TLS_INTERFACE_MAX_LONG];
35} tls_pvt;
36
37/*
38 * nss_tls_stats_sync()
39 * Update tls node statistics.
40 */
41static void nss_tls_stats_sync(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm)
42{
43 struct nss_tls_msg *ndcm = (struct nss_tls_msg *)ncm;
44 struct nss_top_instance *nss_top = nss_ctx->nss_top;
45 struct nss_tls_ctx_stats *msg_stats = &ndcm->msg.stats;
46 uint64_t *if_stats;
47 int i;
48
49 spin_lock_bh(&nss_top->stats_lock);
50
51 /*
52 * Update common node stats,
53 * Note: TLS only supports a single queue for RX
54 */
55 if_stats = nss_top->stats_node[ncm->interface];
56 if_stats[NSS_STATS_NODE_RX_PKTS] += msg_stats->pkt.rx_packets;
57 if_stats[NSS_STATS_NODE_RX_BYTES] += msg_stats->pkt.rx_bytes;
58
59 for (i = 0; i < NSS_MAX_NUM_PRI; i++)
60 if_stats[NSS_STATS_NODE_RX_QUEUE_0_DROPPED + i] += msg_stats->pkt.rx_dropped[i];
61
62 if_stats[NSS_STATS_NODE_TX_PKTS] += msg_stats->pkt.tx_packets;
63 if_stats[NSS_STATS_NODE_TX_BYTES] += msg_stats->pkt.tx_bytes;
64
65 spin_unlock_bh(&nss_top->stats_lock);
66}
67
68/*
69 * nss_tls_stats_read()
70 * Read tls node statiistics.
71 */
72static ssize_t nss_tls_stats_read(struct file *fp, char __user *ubuf, size_t sz, loff_t *ppos)
73{
74 struct nss_ctx_instance *nss_ctx = nss_tls_get_context();
75 enum nss_dynamic_interface_type type;
76 ssize_t bytes_read = 0;
77 size_t len = 0, size;
78 uint32_t if_num;
79 char *buf;
80
81 size = NSS_TLS_STATS_SIZE_PER_IF * bitmap_weight(tls_pvt.if_map, NSS_MAX_NET_INTERFACES);
82
83 buf = kzalloc(size, GFP_KERNEL);
84 if (!buf) {
85 nss_warning("Could not allocate memory for local statistics buffer");
86 return 0;
87 }
88
89 /*
90 * Common node stats for each TLS dynamic interface.
91 */
92 for_each_set_bit(if_num, tls_pvt.if_map, NSS_MAX_NET_INTERFACES) {
93 type = nss_dynamic_interface_get_type(nss_ctx, if_num);
94
95 switch (type) {
96 case NSS_DYNAMIC_INTERFACE_TYPE_TLS_INNER:
97 len += scnprintf(buf + len, size - len, "\nInner if_num:%03u", if_num);
98 break;
99
100 case NSS_DYNAMIC_INTERFACE_TYPE_TLS_OUTER:
101 len += scnprintf(buf + len, size - len, "\nOuter if_num:%03u", if_num);
102 break;
103
104 default:
105 len += scnprintf(buf + len, size - len, "\nUnknown(%d) if_num:%03u", type, if_num);
106 break;
107 }
108
109 len += scnprintf(buf + len, size - len, "\n-------------------\n");
110 len = nss_stats_fill_common_stats(if_num, NSS_STATS_SINGLE_INSTANCE, buf, len, size - len, "tls");
111 }
112
113 bytes_read = simple_read_from_buffer(ubuf, sz, ppos, buf, len);
114 kfree(buf);
115
116 return bytes_read;
117}
118
119/*
120 * nss_tls_stats_ops
121 */
122NSS_STATS_DECLARE_FILE_OPERATIONS(tls)
123
124/*
125 * nss_tls_verify_ifnum()
126 * Verify if the interface number is a TLS interface.
127 */
128static bool nss_tls_verify_ifnum(struct nss_ctx_instance *nss_ctx, uint32_t if_num)
129{
130 enum nss_dynamic_interface_type type = nss_dynamic_interface_get_type(nss_ctx, if_num);
131
132 if (type == NSS_DYNAMIC_INTERFACE_TYPE_TLS_INNER)
133 return true;
134
135 if (type == NSS_DYNAMIC_INTERFACE_TYPE_TLS_OUTER)
136 return true;
137
138 if (if_num == NSS_TLS_INTERFACE)
139 return true;
140
141 return false;
142}
143
144/*
145 * nss_tls_handler()
146 * Handle NSS -> HLOS messages for tls tunnel
147 */
148static void nss_tls_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, void *data)
149{
150 nss_tls_msg_callback_t cb;
151 void *app_data;
152
153 NSS_VERIFY_CTX_MAGIC(nss_ctx);
154
155 nss_trace("%p: handle event for interface num :%u", nss_ctx, ncm->interface);
156
157 /*
158 * Is this a valid request/response packet?
159 */
160 if (ncm->type >= NSS_TLS_MSG_MAX) {
161 nss_warning("%p:Bad message type(%d) for TLS interface %d", nss_ctx, ncm->type, ncm->interface);
162 return;
163 }
164
165 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_tls_msg)) {
166 nss_warning("%p:Bad message length(%d)", nss_ctx, ncm->len);
167 return;
168 }
169
170 if (ncm->type == NSS_TLS_MSG_TYPE_CTX_SYNC)
171 nss_tls_stats_sync(nss_ctx, ncm);
172
173 /*
174 * Update the callback and app_data for NOTIFY messages
175 */
176 if (ncm->response == NSS_CMN_RESPONSE_NOTIFY) {
177 ncm->cb = (nss_ptr_t)nss_top_main.if_rx_msg_callback[ncm->interface];
178 ncm->app_data = (nss_ptr_t)nss_ctx->nss_rx_interface_handlers[nss_ctx->id][ncm->interface].app_data;
179 }
180
181 /*
182 * Log failures
183 */
184 nss_core_log_msg_failures(nss_ctx, ncm);
185
186 /*
187 * Trace messages.
188 */
189 nss_tls_log_rx_msg((struct nss_tls_msg *)ncm);
190
191 /*
192 * Callback
193 */
194 cb = (nss_tls_msg_callback_t)ncm->cb;
195 app_data = (void *)ncm->app_data;
196
197 /*
198 * Call TLS session callback
199 */
200 if (!cb) {
201 nss_warning("%p: No callback for tls session interface %d", nss_ctx, ncm->interface);
202 return;
203 }
204
205 nss_trace("%p: calling tlsmgr event handler(%u)", nss_ctx, ncm->interface);
206 cb(app_data, ncm);
207}
208
209/*
210 * nss_tls_sync_resp()
211 * Callback to handle the completion of HLOS-->NSS messages.
212 */
213static void nss_tls_sync_resp(void *app_data, struct nss_cmn_msg *ncm)
214{
215 struct nss_tls_msg *pvt_msg = app_data;
216 struct nss_tls_msg *resp_msg = container_of(ncm, struct nss_tls_msg, cm);
217
218 /*
219 * Copy response message to pvt message
220 */
221 memcpy(pvt_msg, resp_msg, sizeof(*resp_msg));
222
223 /*
224 * Write memory barrier
225 */
226 smp_wmb();
227
228 complete(&tls_pvt.complete);
229}
230
231/*
232 * nss_tls_tx_buf()
233 * Transmit buffer over TLS interface
234 */
235nss_tx_status_t nss_tls_tx_buf(struct sk_buff *skb, uint32_t if_num, struct nss_ctx_instance *nss_ctx)
236{
237 int32_t status;
238
239 if (!nss_tls_verify_ifnum(nss_ctx, if_num))
240 return NSS_TX_FAILURE;
241
242 NSS_VERIFY_CTX_MAGIC(nss_ctx);
243
244 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
245 nss_warning("%p: tx_data packet dropped as core not ready", nss_ctx);
246 return NSS_TX_FAILURE_NOT_READY;
247 }
248
249 status = nss_core_send_buffer(nss_ctx, if_num, skb, NSS_IF_H2N_DATA_QUEUE, H2N_BUFFER_PACKET, 0);
250 switch (status) {
251 case NSS_CORE_STATUS_SUCCESS:
252 break;
253
254 case NSS_CORE_STATUS_FAILURE_QUEUE: /* queue full condition */
255 nss_warning("%p: H2N queue full for tx_buf", nss_ctx);
256 return NSS_TX_FAILURE_QUEUE;
257
258 default:
259 nss_warning("%p: general failure for tx_buf", nss_ctx);
260 return NSS_TX_FAILURE;
261 }
262
263 /*
264 * Kick the NSS awake so it can process our new entry.
265 */
266 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
267
268 return NSS_TX_SUCCESS;
269}
270EXPORT_SYMBOL(nss_tls_tx_buf);
271
272/*
273 * nss_tls_tx_msg()
274 * Transmit a TLS message to NSS firmware
275 */
276nss_tx_status_t nss_tls_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_tls_msg *msg)
277{
278 struct nss_cmn_msg *ncm = &msg->cm;
279
280 if (ncm->type >= NSS_TLS_MSG_MAX) {
281 nss_warning("%p: tls message type out of range: %d", nss_ctx, ncm->type);
282 return NSS_TX_FAILURE;
283 }
284
285 if (!nss_tls_verify_ifnum(nss_ctx, ncm->interface)) {
286 nss_warning("%p: tls message interface is bad: %u", nss_ctx, ncm->interface);
287 return NSS_TX_FAILURE;
288 }
289
290 /*
291 * Trace messages.
292 */
293 nss_tls_log_tx_msg(msg);
294
295 return nss_core_send_cmd(nss_ctx, msg, sizeof(*msg), NSS_NBUF_PAYLOAD_SIZE);
296}
297EXPORT_SYMBOL(nss_tls_tx_msg);
298
299/*
300 * nss_tls_tx_msg_sync()
301 * Transmit a TLS message to NSS firmware synchronously.
302 */
303nss_tx_status_t nss_tls_tx_msg_sync(struct nss_ctx_instance *nss_ctx, uint32_t if_num,
304 enum nss_tls_msg_type type, uint16_t len,
305 struct nss_tls_msg *ntcm)
306{
307 struct nss_tls_msg *local_ntcm = &tls_pvt.ntcm;
308 nss_tx_status_t status;
309 int ret = 0;
310
311 /*
312 * Length of the message should be the based on type
313 */
314 if (len > sizeof(struct nss_tls_msg)) {
315 nss_warning("%p: Invalid message length(%u), type (%d), I/F(%u)\n", nss_ctx, len, type, if_num);
316 return NSS_TX_FAILURE;
317 }
318
319 down(&tls_pvt.sem);
320
321 /*
322 * We need to copy the message content into the actual message
323 * to be sent to NSS
324 */
325 memset(local_ntcm, 0, sizeof(*local_ntcm));
326
327 nss_tls_msg_init(local_ntcm, if_num, type, len, nss_tls_sync_resp, local_ntcm);
328 memcpy(&local_ntcm->msg, &ntcm->msg, len);
329
330 status = nss_tls_tx_msg(nss_ctx, local_ntcm);
331 if (status != NSS_TX_SUCCESS) {
332 nss_warning("%p: Failed to send message\n", nss_ctx);
333 goto done;
334 }
335
336 ret = wait_for_completion_timeout(&tls_pvt.complete, msecs_to_jiffies(NSS_TLS_TX_TIMEOUT));
337 if (!ret) {
338 nss_warning("%p: Failed to receive response, timeout(%d)\n", nss_ctx, ret);
339 status = NSS_TX_FAILURE_NOT_READY;
340 goto done;
341 }
342
343 /*
344 * Read memory barrier
345 */
346 smp_rmb();
347
348 if (local_ntcm->cm.response != NSS_CMN_RESPONSE_ACK) {
349 status = NSS_TX_FAILURE;
350 ntcm->cm.response = local_ntcm->cm.response;
351 ntcm->cm.error = local_ntcm->cm.error;
352 goto done;
353 }
354
355 /*
356 * Copy the message received
357 */
358 memcpy(&ntcm->msg, &local_ntcm->msg, len);
359
360done:
361 up(&tls_pvt.sem);
362 return status;
363}
364EXPORT_SYMBOL(nss_tls_tx_msg_sync);
365
366
367/*
368 * nss_tls_notify_register()
369 * Register a handler for notification from NSS firmware.
370 */
371struct nss_ctx_instance *nss_tls_notify_register(uint32_t if_num, nss_tls_msg_callback_t ev_cb, void *app_data)
372{
373 struct nss_ctx_instance *nss_ctx = nss_tls_get_context();
374 uint32_t ret;
375
376 BUG_ON(!nss_ctx);
377
378 ret = nss_core_register_handler(nss_ctx, if_num, nss_tls_handler, app_data);
379 if (ret != NSS_CORE_STATUS_SUCCESS) {
380 nss_warning("%p: unable to register event handler for interface(%u)", nss_ctx, if_num);
381 return NULL;
382 }
383
384 nss_top_main.if_rx_msg_callback[if_num] = ev_cb;
385
386 return nss_ctx;
387}
388EXPORT_SYMBOL(nss_tls_notify_register);
389
390/*
391 * nss_tls_notify_unregister()
392 * Unregister notification callback handler.
393 */
394void nss_tls_notify_unregister(uint32_t if_num)
395{
396 struct nss_ctx_instance *nss_ctx = nss_tls_get_context();
397 uint32_t ret;
398
399 BUG_ON(!nss_ctx);
400
401 ret = nss_core_unregister_handler(nss_ctx, if_num);
402 if (ret != NSS_CORE_STATUS_SUCCESS) {
403 nss_warning("%p: unable to un register event handler for interface(%u)", nss_ctx, if_num);
404 return;
405 }
406
407 nss_top_main.if_rx_msg_callback[if_num] = NULL;
408
409 return;
410}
411EXPORT_SYMBOL(nss_tls_notify_unregister);
412
413/*
414 * nss_tls_register_if()
415 * Register data and event callback handlers for dynamic interface.
416 */
417struct nss_ctx_instance *nss_tls_register_if(uint32_t if_num,
418 nss_tls_data_callback_t data_cb,
419 nss_tls_msg_callback_t ev_cb,
420 struct net_device *netdev,
421 uint32_t features,
422 uint32_t type,
423 void *app_data)
424{
425 struct nss_ctx_instance *nss_ctx = nss_tls_get_context();
426 uint32_t ret;
427
428 if (!nss_tls_verify_ifnum(nss_ctx, if_num)) {
429 nss_warning("%p: TLS Interface is not dynamic:%u", nss_ctx, if_num);
430 return NULL;
431 }
432
433 if (nss_ctx->subsys_dp_register[if_num].ndev) {
434 nss_warning("%p: Cannot find free slot for TLS NSS I/F:%u", nss_ctx, if_num);
435 return NULL;
436 }
437
438 nss_core_register_subsys_dp(nss_ctx, if_num, data_cb, NULL, app_data, netdev, features);
439 nss_core_set_subsys_dp_type(nss_ctx, netdev, if_num, type);
440
441 ret = nss_core_register_handler(nss_ctx, if_num, nss_tls_handler, app_data);
442 if (ret != NSS_CORE_STATUS_SUCCESS) {
443 nss_warning("%p: unable to register event handler for interface(%u)", nss_ctx, if_num);
444 return NULL;
445 }
446
447 nss_top_main.if_rx_msg_callback[if_num] = ev_cb;
448
449 /*
450 * Atomically set the bitmap for the interface number
451 */
452 set_bit(if_num, tls_pvt.if_map);
453 return nss_ctx;
454}
455EXPORT_SYMBOL(nss_tls_register_if);
456
457/*
458 * nss_tls_unregister_if()
459 * Unregister data and event callback handlers for the interface.
460 */
461void nss_tls_unregister_if(uint32_t if_num)
462{
463 struct nss_ctx_instance *nss_ctx = nss_tls_get_context();
464
465 if (!nss_ctx->subsys_dp_register[if_num].ndev) {
466 nss_warning("%p: Cannot find registered netdev for TLS NSS I/F:%u", nss_ctx, if_num);
467 return;
468 }
469
470 /*
471 * Atomically clear the bitmap for the interface number
472 */
473 clear_bit(if_num, tls_pvt.if_map);
474
475 nss_core_unregister_handler(nss_ctx, if_num);
476 nss_top_main.if_rx_msg_callback[if_num] = NULL;
477
478 nss_core_unregister_subsys_dp(nss_ctx, if_num);
479}
480EXPORT_SYMBOL(nss_tls_unregister_if);
481
482/*
483 * nss_tls_get_context()
484 * Return TLS NSS context.
485 */
486struct nss_ctx_instance *nss_tls_get_context(void)
487{
488 return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.tls_handler_id];
489}
490EXPORT_SYMBOL(nss_tls_get_context);
491
492/*
493 * nss_tls_get_device()
494 * Gets the original device from probe.
495 */
496struct device *nss_tls_get_dev(struct nss_ctx_instance *nss_ctx)
497{
498 return nss_ctx->dev;
499}
500EXPORT_SYMBOL(nss_tls_get_dev);
501
502/*
503 * nss_tls_msg_init()
504 * Initialize nss_tls msg to be sent asynchronously.
505 */
506void nss_tls_msg_init(struct nss_tls_msg *ncm, uint32_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data)
507{
508 nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data);
509}
510EXPORT_SYMBOL(nss_tls_msg_init);
511
512/*
513 * nss_tls_msg_sync_init()
514 * Initialize nss_tls_msg to be sent synchronously.
515 */
516void nss_tls_msg_sync_init(struct nss_tls_msg *ncm, uint32_t if_num, uint32_t type, uint32_t len)
517{
518 nss_cmn_msg_sync_init(&ncm->cm, if_num, type, len);
519}
520EXPORT_SYMBOL(nss_tls_msg_sync_init);
521
522/*
523 * nss_tls_register_handler()
524 * TLS initialization.
525 */
526void nss_tls_register_handler(void)
527{
528 sema_init(&tls_pvt.sem, 1);
529 init_completion(&tls_pvt.complete);
530 nss_stats_create_dentry("tls", &nss_tls_stats_ops);
531}