blob: 3b953d0482f0f8c4a0593f01b3615463cc32e5bd [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/*
85 * nss_bridge_verify_if_num()
86 * Verify if_num passed to us.
87 */
88static bool nss_bridge_verify_if_num(uint32_t if_num)
89{
90 if (nss_is_dynamic_interface(if_num) == false) {
91 return false;
92 }
93
94 if (nss_dynamic_interface_get_type(if_num) != NSS_DYNAMIC_INTERFACE_TYPE_BRIDGE) {
95 return false;
96 }
97
98 return true;
99}
100
101/*
102 * nss_bridge_callback()
103 * Callback to handle the completion of NSS->HLOS messages.
104 */
105static void nss_bridge_callback(void *app_data, struct nss_bridge_msg *nbm)
106{
107 nss_bridge_msg_callback_t callback = (nss_bridge_msg_callback_t)bridge_pvt.cb;
108 void *data = bridge_pvt.app_data;
109
110 bridge_pvt.response = NSS_TX_SUCCESS;
111 bridge_pvt.cb = NULL;
112 bridge_pvt.app_data = NULL;
113
114 if (nbm->cm.response != NSS_CMN_RESPONSE_ACK) {
115 nss_warning("bridge error response %d\n", nbm->cm.response);
116 bridge_pvt.response = nbm->cm.response;
117 }
118
119 if (callback) {
120 callback(data, nbm);
121 }
122 complete(&bridge_pvt.complete);
123}
124
125/*
126 * nss_bridge_tx_msg()
127 * Transmit a bridge message to NSSFW
128 */
129nss_tx_status_t nss_bridge_tx_msg(struct nss_ctx_instance *nss_ctx, struct nss_bridge_msg *msg)
130{
131 struct nss_bridge_msg *nm;
132 struct nss_cmn_msg *ncm = &msg->cm;
133 struct sk_buff *nbuf;
134 int32_t status;
135
136 NSS_VERIFY_CTX_MAGIC(nss_ctx);
137 if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) {
138 nss_warning("%p: bridge msg dropped as core not ready", nss_ctx);
139 return NSS_TX_FAILURE_NOT_READY;
140 }
141
142 /*
143 * Sanity check the message
144 */
145 if (!nss_is_dynamic_interface(ncm->interface)) {
146 nss_warning("%p: tx request for interface that is not a bridge: %d", nss_ctx, ncm->interface);
147 return NSS_TX_FAILURE;
148 }
149
150 if (ncm->type >= NSS_BRIDGE_MSG_TYPE_MAX) {
151 nss_warning("%p: message type out of range: %d", nss_ctx, ncm->type);
152 return NSS_TX_FAILURE;
153 }
154
155 if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_bridge_msg)) {
156 nss_warning("%p: message length is invalid: %d", nss_ctx, nss_cmn_get_msg_len(ncm));
157 return NSS_TX_FAILURE;
158 }
159
160 nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE);
161 if (unlikely(!nbuf)) {
162 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]);
163 nss_warning("%p: msg dropped as command allocation failed", nss_ctx);
164 return NSS_TX_FAILURE;
165 }
166
167 /*
168 * Copy the message to our skb
169 */
170 nm = (struct nss_bridge_msg *)skb_put(nbuf, sizeof(struct nss_bridge_msg));
171 memcpy(nm, msg, sizeof(struct nss_bridge_msg));
172
173 status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0);
174 if (status != NSS_CORE_STATUS_SUCCESS) {
175 dev_kfree_skb_any(nbuf);
176 nss_warning("%p: Unable to enqueue 'bridge message'", nss_ctx);
177 return NSS_TX_FAILURE;
178 }
179
180 nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE);
181
182 NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]);
183 return NSS_TX_SUCCESS;
184}
185EXPORT_SYMBOL(nss_bridge_tx_msg);
186
187/*
188 * nss_bridge_tx_msg_sync()
189 * Transmit a bridge message to NSS firmware synchronously.
190 */
191nss_tx_status_t nss_bridge_tx_msg_sync(struct nss_ctx_instance *nss_ctx, struct nss_bridge_msg *nbm)
192{
193 nss_tx_status_t status;
194 int ret = 0;
195
196 down(&bridge_pvt.sem);
197 bridge_pvt.cb = (void *)nbm->cm.cb;
198 bridge_pvt.app_data = (void *)nbm->cm.app_data;
199
Stephen Wangaed46332016-12-12 17:29:03 -0800200 nbm->cm.cb = (nss_ptr_t)nss_bridge_callback;
201 nbm->cm.app_data = (nss_ptr_t)NULL;
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700202
203 status = nss_bridge_tx_msg(nss_ctx, nbm);
204 if (status != NSS_TX_SUCCESS) {
205 nss_warning("%p: bridge_tx_msg failed\n", nss_ctx);
206 up(&bridge_pvt.sem);
207 return status;
208 }
209
210 ret = wait_for_completion_timeout(&bridge_pvt.complete, msecs_to_jiffies(NSS_BRIDGE_TX_TIMEOUT));
211
212 if (!ret) {
213 nss_warning("%p: bridge msg tx failed due to timeout\n", nss_ctx);
214 bridge_pvt.response = NSS_TX_FAILURE;
215 }
216
217 status = bridge_pvt.response;
218 up(&bridge_pvt.sem);
219 return status;
220}
221EXPORT_SYMBOL(nss_bridge_tx_msg_sync);
222
223/*
224 * nss_bridge_get_context()
225 */
226struct nss_ctx_instance *nss_bridge_get_context(void)
227{
228 return (struct nss_ctx_instance *)&nss_top_main.nss[nss_top_main.bridge_handler_id];
229}
230EXPORT_SYMBOL(nss_bridge_get_context);
231
232/*
233 * 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
443 nss_ctx->subsys_dp_register[if_num].ndev = NULL;
444 nss_ctx->subsys_dp_register[if_num].cb = NULL;
445 nss_ctx->subsys_dp_register[if_num].app_data = NULL;
446 nss_ctx->subsys_dp_register[if_num].features = 0;
447
448 nss_top_main.bridge_callback = NULL;
449
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700450 nss_core_unregister_handler(if_num);
451}
452EXPORT_SYMBOL(nss_bridge_unregister);
453
454/*
455 * nss_bridge_register()
456 */
Yu Huang12323ff2017-03-02 15:01:04 -0800457struct nss_ctx_instance *nss_bridge_register(uint32_t if_num, struct net_device *netdev,
458 nss_bridge_callback_t bridge_data_cb,
459 nss_bridge_msg_callback_t bridge_msg_cb,
460 uint32_t features,
461 void *app_data)
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700462{
Yu Huang12323ff2017-03-02 15:01:04 -0800463 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
464
465 nss_assert(nss_bridge_verify_if_num(if_num));
466
467 nss_ctx->subsys_dp_register[if_num].ndev = netdev;
468 nss_ctx->subsys_dp_register[if_num].cb = bridge_data_cb;
469 nss_ctx->subsys_dp_register[if_num].app_data = app_data;
470 nss_ctx->subsys_dp_register[if_num].features = features;
471
472 nss_top_main.bridge_callback = bridge_msg_cb;
473
474 nss_core_register_handler(if_num, nss_bridge_handler, app_data);
475 return nss_ctx;
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -0700476}
477EXPORT_SYMBOL(nss_bridge_register);
478
479/*
480 * nss_bridge_notify_register()
481 * Register to receive bridge notify messages.
482 */
483struct nss_ctx_instance *nss_bridge_notify_register(nss_bridge_msg_callback_t cb, void *app_data)
484{
485 nss_top_main.bridge_callback = cb;
486 nss_top_main.bridge_ctx = app_data;
487 return nss_bridge_get_context();
488}
489EXPORT_SYMBOL(nss_bridge_notify_register);
490
491/*
492 * nss_bridge_notify_unregister()
493 * Unregister to receive bridge notify messages.
494 */
495void nss_bridge_notify_unregister(void)
496{
497 nss_top_main.bridge_callback = NULL;
498}
499EXPORT_SYMBOL(nss_bridge_notify_unregister);