blob: e1ddff9144da8413a3d92be09ae9b0ffcb3c82f0 [file] [log] [blame]
Sakthi Vignesh Radhakrishnan4577f9b2016-08-23 11:29:40 -07001/*
2 **************************************************************************
3 * Copyright (c) 2016 The Linux Foundation. All rights reserved.
4 * 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) {
66 ncm->cb = (uint32_t)nss_ctx->nss_top->bridge_callback;
67 ncm->app_data = (uint32_t)nss_ctx->nss_top->bridge_ctx;
68 }
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
200 nbm->cm.cb = (uint32_t)nss_bridge_callback;
201 nbm->cm.app_data = (uint32_t)NULL;
202
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/*
243 * nss_bridge_tx_change_mtu_msg
244 * API to send change mtu message to NSS FW
245 */
246nss_tx_status_t nss_bridge_tx_set_mtu_msg(uint32_t bridge_if_num, uint32_t mtu)
247{
248 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
249 struct nss_bridge_msg nbm;
250 struct nss_if_mtu_change *nimc;
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(bridge_if_num) == false) {
258 nss_warning("%p: received invalid interface %d", nss_ctx, bridge_if_num);
259 return NSS_TX_FAILURE;
260 }
261
262 nss_bridge_msg_init(&nbm, bridge_if_num, NSS_IF_MTU_CHANGE,
263 sizeof(struct nss_if_mtu_change), NULL, NULL);
264
265 nimc = &nbm.msg.if_msg.mtu_change;
266 nimc->min_buf_size = (uint16_t)mtu;
267
268 return nss_bridge_tx_msg_sync(nss_ctx, &nbm);
269}
270EXPORT_SYMBOL(nss_bridge_tx_set_mtu_msg);
271
272/*
273 * nss_bridge_tx_set_mac_addr_msg
274 * API to send change mac addr message to NSS FW
275 */
276nss_tx_status_t nss_bridge_tx_set_mac_addr_msg(uint32_t bridge_if_num, uint8_t *addr)
277{
278 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
279 struct nss_bridge_msg nbm;
280 struct nss_if_mac_address_set *nmas;
281
282 if (!nss_ctx) {
283 nss_warning("Can't get nss context\n");
284 return NSS_TX_FAILURE;
285 }
286
287 if (nss_bridge_verify_if_num(bridge_if_num) == false) {
288 nss_warning("%p: received invalid interface %d", nss_ctx, bridge_if_num);
289 return NSS_TX_FAILURE;
290 }
291
292 nss_bridge_msg_init(&nbm, bridge_if_num, NSS_IF_MAC_ADDR_SET,
293 sizeof(struct nss_if_mac_address_set), NULL, NULL);
294
295 nmas = &nbm.msg.if_msg.mac_address_set;
296 memcpy(nmas->mac_addr, addr, ETH_ALEN);
297 return nss_bridge_tx_msg_sync(nss_ctx, &nbm);
298}
299EXPORT_SYMBOL(nss_bridge_tx_set_mac_addr_msg);
300
301/*
302 * nss_bridge_tx_join_msg
303 * API to send slave join message to NSS FW
304 */
305nss_tx_status_t nss_bridge_tx_join_msg(uint32_t bridge_if_num, struct net_device *netdev)
306{
307 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
308 struct nss_bridge_msg nbm;
309 uint32_t slave_if_num;
310
311 if (!nss_ctx) {
312 nss_warning("Can't get nss context\n");
313 return NSS_TX_FAILURE;
314 }
315
316 if (nss_bridge_verify_if_num(bridge_if_num) == false) {
317 nss_warning("%p: received invalid interface %d\n", nss_ctx, bridge_if_num);
318 return NSS_TX_FAILURE;
319 }
320
321 slave_if_num = nss_cmn_get_interface_number_by_dev(netdev);
322 if (slave_if_num < 0) {
323 nss_warning("%p: invalid slave device %p\n", nss_ctx, netdev);
324 return NSS_TX_FAILURE;
325 }
326
327 nbm.msg.br_join.if_num = slave_if_num;
328 nss_bridge_msg_init(&nbm, bridge_if_num, NSS_BRIDGE_MSG_JOIN,
329 sizeof(struct nss_bridge_join_msg), NULL, NULL);
330
331 return nss_bridge_tx_msg_sync(nss_ctx, &nbm);
332}
333EXPORT_SYMBOL(nss_bridge_tx_join_msg);
334
335/*
336 * nss_bridge_tx_leave_msg
337 * API to send slave leave message to NSS FW
338 */
339nss_tx_status_t nss_bridge_tx_leave_msg(uint32_t bridge_if_num, struct net_device *netdev)
340{
341 struct nss_ctx_instance *nss_ctx = nss_bridge_get_context();
342 struct nss_bridge_msg nbm;
343 uint32_t slave_if_num;
344
345 if (!nss_ctx) {
346 nss_warning("Can't get nss context\n");
347 return NSS_TX_FAILURE;
348 }
349
350 if (nss_bridge_verify_if_num(bridge_if_num) == false) {
351 nss_warning("%p: received invalid interface %d\n", nss_ctx, bridge_if_num);
352 return NSS_TX_FAILURE;
353 }
354
355 slave_if_num = nss_cmn_get_interface_number_by_dev(netdev);
356 if (slave_if_num < 0) {
357 nss_warning("%p: invalid slave device %p\n", nss_ctx, netdev);
358 return NSS_TX_FAILURE;
359 }
360
361 nbm.msg.br_leave.if_num = slave_if_num;
362 nss_bridge_msg_init(&nbm, bridge_if_num, NSS_BRIDGE_MSG_LEAVE,
363 sizeof(struct nss_bridge_leave_msg), NULL, NULL);
364
365 return nss_bridge_tx_msg_sync(nss_ctx, &nbm);
366}
367EXPORT_SYMBOL(nss_bridge_tx_leave_msg);
368
369/*
370 * nss_bridge_init()
371 */
372void nss_bridge_init(void)
373{
374 sema_init(&bridge_pvt.sem, 1);
375 init_completion(&bridge_pvt.complete);
376}
377
378/*
379 * nss_bridge_unregister()
380 */
381void nss_bridge_unregister(uint16_t if_num)
382{
383 nss_core_unregister_handler(if_num);
384}
385EXPORT_SYMBOL(nss_bridge_unregister);
386
387/*
388 * nss_bridge_register()
389 */
390struct nss_ctx_instance *nss_bridge_register(uint16_t if_num)
391{
392 nss_core_register_handler(if_num, nss_bridge_handler, NULL);
393 return nss_bridge_get_context();
394}
395EXPORT_SYMBOL(nss_bridge_register);
396
397/*
398 * nss_bridge_notify_register()
399 * Register to receive bridge notify messages.
400 */
401struct nss_ctx_instance *nss_bridge_notify_register(nss_bridge_msg_callback_t cb, void *app_data)
402{
403 nss_top_main.bridge_callback = cb;
404 nss_top_main.bridge_ctx = app_data;
405 return nss_bridge_get_context();
406}
407EXPORT_SYMBOL(nss_bridge_notify_register);
408
409/*
410 * nss_bridge_notify_unregister()
411 * Unregister to receive bridge notify messages.
412 */
413void nss_bridge_notify_unregister(void)
414{
415 nss_top_main.bridge_callback = NULL;
416}
417EXPORT_SYMBOL(nss_bridge_notify_unregister);