blob: 12bbffa8cc4f5b9ec68c844416681a7d7afd3e70 [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_verify_if_num()
95 * Verify if_num passed to us.
96 */
97static bool nss_bridge_verify_if_num(uint32_t if_num)
98{
99 if (nss_is_dynamic_interface(if_num) == false) {
100 return false;
101 }
102
Stephen Wange8b8d0d2017-02-24 17:05:22 -0800103 if (nss_dynamic_interface_get_type(nss_bridge_get_context(), if_num) != NSS_DYNAMIC_INTERFACE_TYPE_BRIDGE) {
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700104 return false;
105 }
106
107 return true;
108}
109
110/*
111 * nss_bridge_callback()
112 * Callback to handle the completion of NSS->HLOS messages.
113 */
114static void nss_bridge_callback(void *app_data, struct nss_bridge_msg *nbm)
115{
116 nss_bridge_msg_callback_t callback = (nss_bridge_msg_callback_t)bridge_pvt.cb;
117 void *data = bridge_pvt.app_data;
118
119 bridge_pvt.response = NSS_TX_SUCCESS;
120 bridge_pvt.cb = NULL;
121 bridge_pvt.app_data = NULL;
122
123 if (nbm->cm.response != NSS_CMN_RESPONSE_ACK) {
124 nss_warning("bridge error response %d\n", nbm->cm.response);
125 bridge_pvt.response = nbm->cm.response;
126 }
127
128 if (callback) {
129 callback(data, nbm);
130 }
131 complete(&bridge_pvt.complete);
132}
133
134/*
135 * nss_bridge_tx_msg()
136 * Transmit a bridge message to NSSFW
137 */
138nss_tx_status_t nss_bridge_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_bridge_msg *msg)
139{
140 struct nss_bridge_msg *nm;
141 struct nss_cmn_msg *ncm = &msg->cm;
142 struct sk_buff *nbuf;
143 int32_t status;
144
145 NSS_VERIFY_CTX_MAGIC(nss_ctx);
146 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
147 nss_warning("%p: bridge msg dropped as core not ready", nss_ctx);
148 return NSS_TX_FAILURE_NOT_READY;
149 }
150
151 /*
152 * Sanity check the message
153 */
154 if (!nss_is_dynamic_interface(ncm->interface)) {
155 nss_warning("%p: tx request for interface that is not a bridge: %d", nss_ctx, ncm->interface);
156 return NSS_TX_FAILURE;
157 }
158
159 if (ncm->type >= NSS_BRIDGE_MSG_TYPE_MAX) {
160 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
161 return NSS_TX_FAILURE;
162 }
163
164 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_bridge_msg)) {
165 nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
166 return NSS_TX_FAILURE;
167 }
168
169 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
170 if (unlikely(!nbuf)) {
171 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]);
172 nss_warning("%p: msg dropped as command allocation failed", nss_ctx);
173 return NSS_TX_FAILURE;
174 }
175
176 /*
177 * Copy the message to our skb
178 */
179 nm = (struct nss_bridge_msg *)skb_put(nbuf, sizeof(struct nss_bridge_msg));
180 memcpy(nm, msg, sizeof(struct nss_bridge_msg));
181
182 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
183 if (status != NSS_CORE_STATUS_SUCCESS) {
184 dev_kfree_skb_any(nbuf);
185 nss_warning("%p: Unable to enqueue 'bridge message'", nss_ctx);
186 return NSS_TX_FAILURE;
187 }
188
189 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
190
191 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]);
192 return NSS_TX_SUCCESS;
193}
194EXPORT_SYMBOL(nss_bridge_tx_msg);
195
196/*
197 * nss_bridge_tx_msg_sync()
198 * Transmit a bridge message to NSS firmware synchronously.
199 */
200nss_tx_status_t nss_bridge_tx_msg_sync(struct nss_ctx_instance *nss_ctx, struct nss_bridge_msg *nbm)
201{
202 nss_tx_status_t status;
203 int ret = 0;
204
205 down(&bridge_pvt.sem);
206 bridge_pvt.cb = (void *)nbm->cm.cb;
207 bridge_pvt.app_data = (void *)nbm->cm.app_data;
208
Stephen Wangaed46332016-12-12 17:29:03 -0800209 nbm->cm.cb = (nss_ptr_t)nss_bridge_callback;
210 nbm->cm.app_data = (nss_ptr_t)NULL;
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700211
212 status = nss_bridge_tx_msg(nss_ctx, nbm);
213 if (status != NSS_TX_SUCCESS) {
214 nss_warning("%p: bridge_tx_msg failed\n", nss_ctx);
215 up(&bridge_pvt.sem);
216 return status;
217 }
218
219 ret = wait_for_completion_timeout(&bridge_pvt.complete, msecs_to_jiffies(NSS_BRIDGE_TX_TIMEOUT));
220
221 if (!ret) {
222 nss_warning("%p: bridge msg tx failed due to timeout\n", nss_ctx);
223 bridge_pvt.response = NSS_TX_FAILURE;
224 }
225
226 status = bridge_pvt.response;
227 up(&bridge_pvt.sem);
228 return status;
229}
230EXPORT_SYMBOL(nss_bridge_tx_msg_sync);
231
232/*
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700233 * nss_bridge_msg_init()
234 * Initialize nss_bridge_msg.
235 */
236void nss_bridge_msg_init(struct nss_bridge_msg *ncm, uint16_t if_num, uint32_t type, uint32_t len, void *cb, void *app_data)
237{
238 nss_cmn_msg_init(&ncm->cm, if_num, type, len, cb, app_data);
239}
240EXPORT_SYMBOL(nss_bridge_msg_init);
241
242/*
Yu Huang417e4ed2016-10-05 15:02:03 -0700243 * nss_bridge_tx_vsi_assign_msg
244 * API to send vsi assign message to NSS FW
245 */
246nss_tx_status_t nss_bridge_tx_vsi_assign_msg(uint32_t if_num, uint32_t vsi)
247{
248 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
249 struct nss_bridge_msg nbm;
250
251 if (!nss_ctx) {
252 nss_warning("Can't get nss context\n");
253 return NSS_TX_FAILURE;
254 }
255
256 if (nss_bridge_verify_if_num(if_num) == false) {
257 nss_warning("%p: invalid interface %d", nss_ctx, if_num);
258 return NSS_TX_FAILURE;
259 }
260
261 nss_bridge_msg_init(&nbm, if_num, NSS_IF_VSI_ASSIGN,
262 sizeof(struct nss_if_vsi_assign), NULL, NULL);
263
264 nbm.msg.if_msg.vsi_assign.vsi = vsi;
265
266 return nss_bridge_tx_msg_sync(nss_ctx, &nbm);
267}
268EXPORT_SYMBOL(nss_bridge_tx_vsi_assign_msg);
269
270/*
271 * nss_bridge_tx_vsi_unassign_msg
272 * API to send vsi unassign message to NSS FW
273 */
274nss_tx_status_t nss_bridge_tx_vsi_unassign_msg(uint32_t if_num, uint32_t vsi)
275{
276 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
277 struct nss_bridge_msg nbm;
278
279 if (!nss_ctx) {
280 nss_warning("Can't get nss context\n");
281 return NSS_TX_FAILURE;
282 }
283
284 if (nss_bridge_verify_if_num(if_num) == false) {
285 nss_warning("%p: invalid interface %d", nss_ctx, if_num);
286 return NSS_TX_FAILURE;
287 }
288
289 nss_bridge_msg_init(&nbm, if_num, NSS_IF_VSI_UNASSIGN,
290 sizeof(struct nss_if_vsi_unassign), NULL, NULL);
291
292 nbm.msg.if_msg.vsi_unassign.vsi = vsi;
293
294 return nss_bridge_tx_msg_sync(nss_ctx, &nbm);
295}
296EXPORT_SYMBOL(nss_bridge_tx_vsi_unassign_msg);
297
298/*
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700299 * nss_bridge_tx_change_mtu_msg
300 * API to send change mtu message to NSS FW
301 */
302nss_tx_status_t nss_bridge_tx_set_mtu_msg(uint32_t bridge_if_num, uint32_t mtu)
303{
304 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
305 struct nss_bridge_msg nbm;
306 struct nss_if_mtu_change *nimc;
307
308 if (!nss_ctx) {
309 nss_warning("Can't get nss context\n");
310 return NSS_TX_FAILURE;
311 }
312
313 if (nss_bridge_verify_if_num(bridge_if_num) == false) {
314 nss_warning("%p: received invalid interface %d", nss_ctx, bridge_if_num);
315 return NSS_TX_FAILURE;
316 }
317
318 nss_bridge_msg_init(&nbm, bridge_if_num, NSS_IF_MTU_CHANGE,
319 sizeof(struct nss_if_mtu_change), NULL, NULL);
320
321 nimc = &nbm.msg.if_msg.mtu_change;
322 nimc->min_buf_size = (uint16_t)mtu;
323
324 return nss_bridge_tx_msg_sync(nss_ctx, &nbm);
325}
326EXPORT_SYMBOL(nss_bridge_tx_set_mtu_msg);
327
328/*
329 * nss_bridge_tx_set_mac_addr_msg
330 * API to send change mac addr message to NSS FW
331 */
332nss_tx_status_t nss_bridge_tx_set_mac_addr_msg(uint32_t bridge_if_num, uint8_t *addr)
333{
334 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
335 struct nss_bridge_msg nbm;
336 struct nss_if_mac_address_set *nmas;
337
338 if (!nss_ctx) {
339 nss_warning("Can't get nss context\n");
340 return NSS_TX_FAILURE;
341 }
342
343 if (nss_bridge_verify_if_num(bridge_if_num) == false) {
344 nss_warning("%p: received invalid interface %d", nss_ctx, bridge_if_num);
345 return NSS_TX_FAILURE;
346 }
347
348 nss_bridge_msg_init(&nbm, bridge_if_num, NSS_IF_MAC_ADDR_SET,
349 sizeof(struct nss_if_mac_address_set), NULL, NULL);
350
351 nmas = &nbm.msg.if_msg.mac_address_set;
352 memcpy(nmas->mac_addr, addr, ETH_ALEN);
353 return nss_bridge_tx_msg_sync(nss_ctx, &nbm);
354}
355EXPORT_SYMBOL(nss_bridge_tx_set_mac_addr_msg);
356
357/*
358 * nss_bridge_tx_join_msg
359 * API to send slave join message to NSS FW
360 */
361nss_tx_status_t nss_bridge_tx_join_msg(uint32_t bridge_if_num, struct net_device *netdev)
362{
363 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
364 struct nss_bridge_msg nbm;
365 uint32_t slave_if_num;
366
367 if (!nss_ctx) {
368 nss_warning("Can't get nss context\n");
369 return NSS_TX_FAILURE;
370 }
371
372 if (nss_bridge_verify_if_num(bridge_if_num) == false) {
373 nss_warning("%p: received invalid interface %d\n", nss_ctx, bridge_if_num);
374 return NSS_TX_FAILURE;
375 }
376
377 slave_if_num = nss_cmn_get_interface_number_by_dev(netdev);
378 if (slave_if_num < 0) {
379 nss_warning("%p: invalid slave device %p\n", nss_ctx, netdev);
380 return NSS_TX_FAILURE;
381 }
382
383 nbm.msg.br_join.if_num = slave_if_num;
384 nss_bridge_msg_init(&nbm, bridge_if_num, NSS_BRIDGE_MSG_JOIN,
385 sizeof(struct nss_bridge_join_msg), NULL, NULL);
386
387 return nss_bridge_tx_msg_sync(nss_ctx, &nbm);
388}
389EXPORT_SYMBOL(nss_bridge_tx_join_msg);
390
391/*
392 * nss_bridge_tx_leave_msg
393 * API to send slave leave message to NSS FW
394 */
395nss_tx_status_t nss_bridge_tx_leave_msg(uint32_t bridge_if_num, struct net_device *netdev)
396{
397 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
398 struct nss_bridge_msg nbm;
399 uint32_t slave_if_num;
400
401 if (!nss_ctx) {
402 nss_warning("Can't get nss context\n");
403 return NSS_TX_FAILURE;
404 }
405
406 if (nss_bridge_verify_if_num(bridge_if_num) == false) {
407 nss_warning("%p: received invalid interface %d\n", nss_ctx, bridge_if_num);
408 return NSS_TX_FAILURE;
409 }
410
411 slave_if_num = nss_cmn_get_interface_number_by_dev(netdev);
412 if (slave_if_num < 0) {
413 nss_warning("%p: invalid slave device %p\n", nss_ctx, netdev);
414 return NSS_TX_FAILURE;
415 }
416
417 nbm.msg.br_leave.if_num = slave_if_num;
418 nss_bridge_msg_init(&nbm, bridge_if_num, NSS_BRIDGE_MSG_LEAVE,
419 sizeof(struct nss_bridge_leave_msg), NULL, NULL);
420
421 return nss_bridge_tx_msg_sync(nss_ctx, &nbm);
422}
423EXPORT_SYMBOL(nss_bridge_tx_leave_msg);
424
425/*
426 * nss_bridge_init()
427 */
428void nss_bridge_init(void)
429{
430 sema_init(&bridge_pvt.sem, 1);
431 init_completion(&bridge_pvt.complete);
432}
433
434/*
435 * nss_bridge_unregister()
436 */
Yu Huang12323ff2017-03-02 15:01:04 -0800437void nss_bridge_unregister(uint32_t if_num)
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700438{
Yu Huang12323ff2017-03-02 15:01:04 -0800439 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
440
441 nss_assert(nss_bridge_verify_if_num(if_num));
442
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700443 nss_core_unregister_subsys_dp(nss_ctx, if_num);
Yu Huang12323ff2017-03-02 15:01:04 -0800444
445 nss_top_main.bridge_callback = NULL;
446
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700447 nss_core_unregister_handler(nss_ctx, if_num);
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700448}
449EXPORT_SYMBOL(nss_bridge_unregister);
450
451/*
452 * nss_bridge_register()
453 */
Yu Huang12323ff2017-03-02 15:01:04 -0800454struct nss_ctx_instance *nss_bridge_register(uint32_t if_num, struct net_device *netdev,
455 nss_bridge_callback_t bridge_data_cb,
456 nss_bridge_msg_callback_t bridge_msg_cb,
457 uint32_t features,
458 void *app_data)
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700459{
Yu Huang12323ff2017-03-02 15:01:04 -0800460 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
461
462 nss_assert(nss_bridge_verify_if_num(if_num));
463
Jackson Bockus7ca70ec2017-07-17 13:47:29 -0700464 nss_core_register_subsys_dp(nss_ctx, if_num, bridge_data_cb, NULL, app_data, netdev, features);
Yu Huang12323ff2017-03-02 15:01:04 -0800465
466 nss_top_main.bridge_callback = bridge_msg_cb;
467
Thomas Wu91f4bdf2017-06-09 12:03:02 -0700468 nss_core_register_handler(nss_ctx, if_num, nss_bridge_handler, app_data);
Yu Huang12323ff2017-03-02 15:01:04 -0800469 return nss_ctx;
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700470}
471EXPORT_SYMBOL(nss_bridge_register);
472
473/*
474 * nss_bridge_notify_register()
475 * Register to receive bridge notify messages.
476 */
477struct nss_ctx_instance *nss_bridge_notify_register(nss_bridge_msg_callback_t cb, void *app_data)
478{
479 nss_top_main.bridge_callback = cb;
480 nss_top_main.bridge_ctx = app_data;
481 return nss_bridge_get_context();
482}
483EXPORT_SYMBOL(nss_bridge_notify_register);
484
485/*
486 * nss_bridge_notify_unregister()
487 * Unregister to receive bridge notify messages.
488 */
489void nss_bridge_notify_unregister(void)
490{
491 nss_top_main.bridge_callback = NULL;
492}
493EXPORT_SYMBOL(nss_bridge_notify_unregister);