blob: 1e8fda42f75ccac950c526354f1b384f63a42a37 [file] [log] [blame]
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -07001/*
2 **************************************************************************
Yu Huang417e4ed2016-10-05 15:02:03 -07003 * Copyright (c) 2016-2017, The Linux Foundation. All rights reserved.
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -07004 * 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"
18
19#define NSS_BRIDGE_TX_TIMEOUT 1000 /* 1 Second */
20
21/*
22 * Private data structure
23 */
24static struct nss_bridge_pvt {
25 struct semaphore sem;
26 struct completion complete;
27 int response;
28 void *cb;
29 void *app_data;
30} bridge_pvt;
31
32/*
33 * nss_bridge_handler()
34 * Handle NSS -> HLOS messages for bridge
35 */
36static void nss_bridge_handler(struct nss_ctx_instance *nss_ctx, struct nss_cmn_msg *ncm, void *app_data)
37{
38 struct nss_bridge_msg *nbm = (struct nss_bridge_msg *)ncm;
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -070039 nss_bridge_msg_callback_t cb;
40
41 BUG_ON(!nss_is_dynamic_interface(ncm->interface));
42
43 /*
44 * Is this a valid request/response packet?
45 */
46 if (ncm->type >= NSS_BRIDGE_MSG_TYPE_MAX) {
47 nss_warning("%p: received invalid message %d for bridge interface", nss_ctx, ncm->type);
48 return;
49 }
50
51 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_bridge_msg)) {
52 nss_warning("%p: length of message is greater than required: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
53 return;
54 }
55
56 /*
57 * Log failures
58 */
59 nss_core_log_msg_failures(nss_ctx, ncm);
60
61 /*
62 * Update the callback and app_data for NOTIFY messages, IPv4 sends all notify messages
63 * to the same callback/app_data.
64 */
65 if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) {
Stephen Wangaed46332016-12-12 17:29:03 -080066 ncm->cb = (nss_ptr_t)nss_ctx->nss_top->bridge_callback;
67 ncm->app_data = (nss_ptr_t)nss_ctx->nss_top->bridge_ctx;
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -070068 }
69
70 /*
71 * Do we have a call back
72 */
73 if (!ncm->cb) {
74 return;
75 }
76
77 /*
78 * callback
79 */
80 cb = (nss_bridge_msg_callback_t)ncm->cb;
81 cb((void *)ncm->app_data, nbm);
82}
83
84/*
Stephen Wange8b8d0d2017-02-24 17:05:22 -080085 * nss_bridge_get_context()
86 */
87struct nss_ctx_instance *nss_bridge_get_context(void)
88{
89 return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.bridge_handler_id];
90}
91EXPORT_SYMBOL(nss_bridge_get_context);
92
93/*
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -070094 * nss_bridge_callback()
95 * Callback to handle the completion of NSS->HLOS messages.
96 */
97static void nss_bridge_callback(void *app_data, struct nss_bridge_msg *nbm)
98{
99 nss_bridge_msg_callback_t callback = (nss_bridge_msg_callback_t)bridge_pvt.cb;
100 void *data = bridge_pvt.app_data;
101
102 bridge_pvt.response = NSS_TX_SUCCESS;
103 bridge_pvt.cb = NULL;
104 bridge_pvt.app_data = NULL;
105
106 if (nbm->cm.response != NSS_CMN_RESPONSE_ACK) {
107 nss_warning("bridge error response %d\n", nbm->cm.response);
108 bridge_pvt.response = nbm->cm.response;
109 }
110
111 if (callback) {
112 callback(data, nbm);
113 }
114 complete(&bridge_pvt.complete);
115}
116
117/*
Sakthi Vignesh Radhakrishnan2842f9c2017-07-27 13:56:07 -0700118 * nss_bridge_verify_if_num()
119 * Verify if_num passed to us.
120 */
121bool nss_bridge_verify_if_num(uint32_t if_num)
122{
123 if (nss_is_dynamic_interface(if_num) == false) {
124 return false;
125 }
126
127 if (nss_dynamic_interface_get_type(nss_bridge_get_context(), if_num) != NSS_DYNAMIC_INTERFACE_TYPE_BRIDGE) {
128 return false;
129 }
130
131 return true;
132}
133EXPORT_SYMBOL(nss_bridge_verify_if_num);
134
135/*
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700136 * nss_bridge_tx_msg()
137 * Transmit a bridge message to NSSFW
138 */
139nss_tx_status_t nss_bridge_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_bridge_msg *msg)
140{
141 struct nss_bridge_msg *nm;
142 struct nss_cmn_msg *ncm = &msg->cm;
143 struct sk_buff *nbuf;
144 int32_t status;
145
146 NSS_VERIFY_CTX_MAGIC(nss_ctx);
147 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
148 nss_warning("%p: bridge msg dropped as core not ready", nss_ctx);
149 return NSS_TX_FAILURE_NOT_READY;
150 }
151
152 /*
153 * Sanity check the message
154 */
155 if (!nss_is_dynamic_interface(ncm->interface)) {
156 nss_warning("%p: tx request for interface that is not a bridge: %d", nss_ctx, ncm->interface);
157 return NSS_TX_FAILURE;
158 }
159
160 if (ncm->type >= NSS_BRIDGE_MSG_TYPE_MAX) {
161 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
162 return NSS_TX_FAILURE;
163 }
164
165 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_bridge_msg)) {
166 nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
167 return NSS_TX_FAILURE;
168 }
169
170 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
171 if (unlikely(!nbuf)) {
172 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]);
173 nss_warning("%p: msg dropped as command allocation failed", nss_ctx);
174 return NSS_TX_FAILURE;
175 }
176
177 /*
178 * Copy the message to our skb
179 */
180 nm = (struct nss_bridge_msg *)skb_put(nbuf, sizeof(struct nss_bridge_msg));
181 memcpy(nm, msg, sizeof(struct nss_bridge_msg));
182
183 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
184 if (status != NSS_CORE_STATUS_SUCCESS) {
185 dev_kfree_skb_any(nbuf);
186 nss_warning("%p: Unable to enqueue 'bridge message'", nss_ctx);
187 return NSS_TX_FAILURE;
188 }
189
190 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
191
192 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]);
193 return NSS_TX_SUCCESS;
194}
195EXPORT_SYMBOL(nss_bridge_tx_msg);
196
197/*
198 * nss_bridge_tx_msg_sync()
199 * Transmit a bridge message to NSS firmware synchronously.
200 */
201nss_tx_status_t nss_bridge_tx_msg_sync(struct nss_ctx_instance *nss_ctx, struct nss_bridge_msg *nbm)
202{
203 nss_tx_status_t status;
204 int ret = 0;
205
206 down(&bridge_pvt.sem);
207 bridge_pvt.cb = (void *)nbm->cm.cb;
208 bridge_pvt.app_data = (void *)nbm->cm.app_data;
209
Stephen Wangaed46332016-12-12 17:29:03 -0800210 nbm->cm.cb = (nss_ptr_t)nss_bridge_callback;
211 nbm->cm.app_data = (nss_ptr_t)NULL;
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700212
213 status = nss_bridge_tx_msg(nss_ctx, nbm);
214 if (status != NSS_TX_SUCCESS) {
215 nss_warning("%p: bridge_tx_msg failed\n", nss_ctx);
216 up(&bridge_pvt.sem);
217 return status;
218 }
219
220 ret = wait_for_completion_timeout(&bridge_pvt.complete, msecs_to_jiffies(NSS_BRIDGE_TX_TIMEOUT));
221
222 if (!ret) {
223 nss_warning("%p: bridge msg tx failed due to timeout\n", nss_ctx);
224 bridge_pvt.response = NSS_TX_FAILURE;
225 }
226
227 status = bridge_pvt.response;
228 up(&bridge_pvt.sem);
229 return status;
230}
231EXPORT_SYMBOL(nss_bridge_tx_msg_sync);
232
233/*
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700234 * nss_bridge_msg_init()
235 * Initialize nss_bridge_msg.
236 */
237void nss_bridge_msg_init(struct nss_bridge_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data)
238{
239 nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data);
240}
241EXPORT_SYMBOL(nss_bridge_msg_init);
242
243/*
Yu Huang417e4ed2016-10-05 15:02:03 -0700244 * nss_bridge_tx_vsi_assign_msg
245 * API to send vsi assign message to NSS FW
246 */
247nss_tx_status_t nss_bridge_tx_vsi_assign_msg(uint32_t if_num, uint32_t vsi)
248{
249 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
250 struct nss_bridge_msg nbm;
251
252 if (!nss_ctx) {
253 nss_warning("Can't get nss context\n");
254 return NSS_TX_FAILURE;
255 }
256
257 if (nss_bridge_verify_if_num(if_num) == false) {
258 nss_warning("%p: invalid interface %d", nss_ctx, if_num);
259 return NSS_TX_FAILURE;
260 }
261
262 nss_bridge_msg_init(&nbm, if_num, NSS_IF_VSI_ASSIGN,
263 sizeof(struct nss_if_vsi_assign), NULL, NULL);
264
265 nbm.msg.if_msg.vsi_assign.vsi = vsi;
266
267 return nss_bridge_tx_msg_sync(nss_ctx, &nbm);
268}
269EXPORT_SYMBOL(nss_bridge_tx_vsi_assign_msg);
270
271/*
272 * nss_bridge_tx_vsi_unassign_msg
273 * API to send vsi unassign message to NSS FW
274 */
275nss_tx_status_t nss_bridge_tx_vsi_unassign_msg(uint32_t if_num, uint32_t vsi)
276{
277 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
278 struct nss_bridge_msg nbm;
279
280 if (!nss_ctx) {
281 nss_warning("Can't get nss context\n");
282 return NSS_TX_FAILURE;
283 }
284
285 if (nss_bridge_verify_if_num(if_num) == false) {
286 nss_warning("%p: invalid interface %d", nss_ctx, if_num);
287 return NSS_TX_FAILURE;
288 }
289
290 nss_bridge_msg_init(&nbm, if_num, NSS_IF_VSI_UNASSIGN,
291 sizeof(struct nss_if_vsi_unassign), NULL, NULL);
292
293 nbm.msg.if_msg.vsi_unassign.vsi = vsi;
294
295 return nss_bridge_tx_msg_sync(nss_ctx, &nbm);
296}
297EXPORT_SYMBOL(nss_bridge_tx_vsi_unassign_msg);
298
299/*
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700300 * nss_bridge_tx_change_mtu_msg
301 * API to send change mtu message to NSS FW
302 */
303nss_tx_status_t nss_bridge_tx_set_mtu_msg(uint32_t bridge_if_num, uint32_t mtu)
304{
305 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
306 struct nss_bridge_msg nbm;
307 struct nss_if_mtu_change *nimc;
308
309 if (!nss_ctx) {
310 nss_warning("Can't get nss context\n");
311 return NSS_TX_FAILURE;
312 }
313
314 if (nss_bridge_verify_if_num(bridge_if_num) == false) {
315 nss_warning("%p: received invalid interface %d", nss_ctx, bridge_if_num);
316 return NSS_TX_FAILURE;
317 }
318
319 nss_bridge_msg_init(&nbm, bridge_if_num, NSS_IF_MTU_CHANGE,
320 sizeof(struct nss_if_mtu_change), NULL, NULL);
321
322 nimc = &nbm.msg.if_msg.mtu_change;
323 nimc->min_buf_size = (uint16_t)mtu;
324
325 return nss_bridge_tx_msg_sync(nss_ctx, &nbm);
326}
327EXPORT_SYMBOL(nss_bridge_tx_set_mtu_msg);
328
329/*
330 * nss_bridge_tx_set_mac_addr_msg
331 * API to send change mac addr message to NSS FW
332 */
333nss_tx_status_t nss_bridge_tx_set_mac_addr_msg(uint32_t bridge_if_num, uint8_t *addr)
334{
335 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
336 struct nss_bridge_msg nbm;
337 struct nss_if_mac_address_set *nmas;
338
339 if (!nss_ctx) {
340 nss_warning("Can't get nss context\n");
341 return NSS_TX_FAILURE;
342 }
343
344 if (nss_bridge_verify_if_num(bridge_if_num) == false) {
345 nss_warning("%p: received invalid interface %d", nss_ctx, bridge_if_num);
346 return NSS_TX_FAILURE;
347 }
348
349 nss_bridge_msg_init(&nbm, bridge_if_num, NSS_IF_MAC_ADDR_SET,
350 sizeof(struct nss_if_mac_address_set), NULL, NULL);
351
352 nmas = &nbm.msg.if_msg.mac_address_set;
353 memcpy(nmas->mac_addr, addr, ETH_ALEN);
354 return nss_bridge_tx_msg_sync(nss_ctx, &nbm);
355}
356EXPORT_SYMBOL(nss_bridge_tx_set_mac_addr_msg);
357
358/*
359 * nss_bridge_tx_join_msg
360 * API to send slave join message to NSS FW
361 */
362nss_tx_status_t nss_bridge_tx_join_msg(uint32_t bridge_if_num, struct net_device *netdev)
363{
364 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
365 struct nss_bridge_msg nbm;
366 uint32_t slave_if_num;
367
368 if (!nss_ctx) {
369 nss_warning("Can't get nss context\n");
370 return NSS_TX_FAILURE;
371 }
372
373 if (nss_bridge_verify_if_num(bridge_if_num) == false) {
374 nss_warning("%p: received invalid interface %d\n", nss_ctx, bridge_if_num);
375 return NSS_TX_FAILURE;
376 }
377
378 slave_if_num = nss_cmn_get_interface_number_by_dev(netdev);
379 if (slave_if_num < 0) {
380 nss_warning("%p: invalid slave device %p\n", nss_ctx, netdev);
381 return NSS_TX_FAILURE;
382 }
383
384 nbm.msg.br_join.if_num = slave_if_num;
385 nss_bridge_msg_init(&nbm, bridge_if_num, NSS_BRIDGE_MSG_JOIN,
386 sizeof(struct nss_bridge_join_msg), NULL, NULL);
387
388 return nss_bridge_tx_msg_sync(nss_ctx, &nbm);
389}
390EXPORT_SYMBOL(nss_bridge_tx_join_msg);
391
392/*
393 * nss_bridge_tx_leave_msg
394 * API to send slave leave message to NSS FW
395 */
396nss_tx_status_t nss_bridge_tx_leave_msg(uint32_t bridge_if_num, struct net_device *netdev)
397{
398 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
399 struct nss_bridge_msg nbm;
400 uint32_t slave_if_num;
401
402 if (!nss_ctx) {
403 nss_warning("Can't get nss context\n");
404 return NSS_TX_FAILURE;
405 }
406
407 if (nss_bridge_verify_if_num(bridge_if_num) == false) {
408 nss_warning("%p: received invalid interface %d\n", nss_ctx, bridge_if_num);
409 return NSS_TX_FAILURE;
410 }
411
412 slave_if_num = nss_cmn_get_interface_number_by_dev(netdev);
413 if (slave_if_num < 0) {
414 nss_warning("%p: invalid slave device %p\n", nss_ctx, netdev);
415 return NSS_TX_FAILURE;
416 }
417
418 nbm.msg.br_leave.if_num = slave_if_num;
419 nss_bridge_msg_init(&nbm, bridge_if_num, NSS_BRIDGE_MSG_LEAVE,
420 sizeof(struct nss_bridge_leave_msg), NULL, NULL);
421
422 return nss_bridge_tx_msg_sync(nss_ctx, &nbm);
423}
424EXPORT_SYMBOL(nss_bridge_tx_leave_msg);
425
426/*
427 * nss_bridge_init()
428 */
429void nss_bridge_init(void)
430{
431 sema_init(&bridge_pvt.sem, 1);
432 init_completion(&bridge_pvt.complete);
433}
434
435/*
436 * nss_bridge_unregister()
437 */
Yu Huang12323ff2017-03-02 15:01:04 -0800438void nss_bridge_unregister(uint32_t if_num)
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700439{
Yu Huang12323ff2017-03-02 15:01:04 -0800440 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
441
442 nss_assert(nss_bridge_verify_if_num(if_num));
443
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700444 nss_core_unregister_subsys_dp(nss_ctx, if_num);
Yu Huang12323ff2017-03-02 15:01:04 -0800445
446 nss_top_main.bridge_callback = NULL;
447
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700448 nss_core_unregister_handler(nss_ctx, if_num);
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700449}
450EXPORT_SYMBOL(nss_bridge_unregister);
451
452/*
453 * nss_bridge_register()
454 */
Yu Huang12323ff2017-03-02 15:01:04 -0800455struct nss_ctx_instance *nss_bridge_register(uint32_t if_num, struct net_device *netdev,
456 nss_bridge_callback_t bridge_data_cb,
457 nss_bridge_msg_callback_t bridge_msg_cb,
458 uint32_t features,
459 void *app_data)
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700460{
Yu Huang12323ff2017-03-02 15:01:04 -0800461 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
462
463 nss_assert(nss_bridge_verify_if_num(if_num));
464
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700465 nss_core_register_subsys_dp(nss_ctx, if_num, bridge_data_cb, NULL, app_data, netdev, features);
Yu Huang12323ff2017-03-02 15:01:04 -0800466
467 nss_top_main.bridge_callback = bridge_msg_cb;
468
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700469 nss_core_register_handler(nss_ctx, if_num, nss_bridge_handler, app_data);
Yu Huang12323ff2017-03-02 15:01:04 -0800470 return nss_ctx;
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700471}
472EXPORT_SYMBOL(nss_bridge_register);
473
474/*
475 * nss_bridge_notify_register()
476 * Register to receive bridge notify messages.
477 */
478struct nss_ctx_instance *nss_bridge_notify_register(nss_bridge_msg_callback_t cb, void *app_data)
479{
480 nss_top_main.bridge_callback = cb;
481 nss_top_main.bridge_ctx = app_data;
482 return nss_bridge_get_context();
483}
484EXPORT_SYMBOL(nss_bridge_notify_register);
485
486/*
487 * nss_bridge_notify_unregister()
488 * Unregister to receive bridge notify messages.
489 */
490void nss_bridge_notify_unregister(void)
491{
492 nss_top_main.bridge_callback = NULL;
493}
494EXPORT_SYMBOL(nss_bridge_notify_unregister);