Tushar Mathur | 6650654 | 2014-04-03 22:01:40 +0530 | [diff] [blame] | 1 | /* |
| 2 | ************************************************************************** |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, The Linux Foundation. All rights reserved. |
Tushar Mathur | 6650654 | 2014-04-03 22:01:40 +0530 | [diff] [blame] | 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 | /* |
| 18 | * nss_tx_rx_lag.c |
| 19 | * NSS LAG Tx APIs |
| 20 | */ |
| 21 | |
| 22 | #include <linux/if_bonding.h> |
| 23 | |
| 24 | #include "nss_tx_rx_common.h" |
| 25 | |
Bhaskar Valaboju | 76988ef | 2017-02-20 19:51:49 +0530 | [diff] [blame] | 26 | #define NSS_LAG_RESP_TIMEOUT 60000 /* 60 Sec */ |
| 27 | |
| 28 | /* |
| 29 | * Private data structure of dynamic interface |
| 30 | */ |
| 31 | struct nss_lag_pvt { |
| 32 | struct completion complete; /* completion structure */ |
| 33 | enum nss_cmn_response response; /* Message response */ |
| 34 | }; |
| 35 | |
| 36 | /* |
| 37 | * nss_lag_state_callback() |
| 38 | * Call back function for nss LAG State |
| 39 | */ |
| 40 | void nss_lag_state_callback(void *arg, struct nss_lag_msg *nm) |
| 41 | { |
| 42 | struct nss_lag_pvt *lag_msg_state = arg; |
| 43 | |
| 44 | /* |
| 45 | * Unblock the sleeping function. |
| 46 | */ |
| 47 | lag_msg_state->response = nm->cm.response; |
| 48 | complete(&lag_msg_state->complete); |
| 49 | } |
| 50 | |
| 51 | /* |
| 52 | * nss_lag_verify_ifnum() |
| 53 | * |
| 54 | */ |
| 55 | static void nss_lag_verify_ifnum(uint32_t if_num) |
| 56 | { |
| 57 | nss_assert((if_num == NSS_LAG0_INTERFACE_NUM) || |
| 58 | (if_num == NSS_LAG1_INTERFACE_NUM) || |
| 59 | (if_num == NSS_LAG2_INTERFACE_NUM) || |
| 60 | (if_num == NSS_LAG3_INTERFACE_NUM)); |
| 61 | } |
| 62 | |
| 63 | /* |
| 64 | * nss_lag_get_context() |
| 65 | */ |
| 66 | static struct nss_ctx_instance *nss_lag_get_context(void) |
| 67 | { |
| 68 | uint8_t ipv4_handler_id = nss_top_main.ipv4_handler_id; |
| 69 | |
| 70 | return (struct nss_ctx_instance *)&nss_top_main.nss[ipv4_handler_id]; |
| 71 | } |
| 72 | |
Tushar Mathur | 6650654 | 2014-04-03 22:01:40 +0530 | [diff] [blame] | 73 | /* |
| 74 | * nss_lag_tx() |
Sol Kavy | cd1bd5c | 2014-04-04 11:09:44 -0700 | [diff] [blame] | 75 | * Transmit a LAG msg to the firmware. |
Tushar Mathur | 6650654 | 2014-04-03 22:01:40 +0530 | [diff] [blame] | 76 | */ |
| 77 | nss_tx_status_t nss_lag_tx(struct nss_ctx_instance *nss_ctx, struct nss_lag_msg *msg) |
| 78 | { |
| 79 | struct sk_buff *nbuf; |
| 80 | int32_t status; |
| 81 | struct nss_lag_msg *nm; |
| 82 | |
| 83 | nss_info("%p: NSS LAG Tx\n", nss_ctx); |
| 84 | |
| 85 | NSS_VERIFY_CTX_MAGIC(nss_ctx); |
| 86 | if (unlikely(nss_ctx->state != NSS_CORE_STATE_INITIALIZED)) { |
Sol Kavy | cd1bd5c | 2014-04-04 11:09:44 -0700 | [diff] [blame] | 87 | nss_warning("%p: LAG msg dropped as core not ready", nss_ctx); |
Tushar Mathur | 6650654 | 2014-04-03 22:01:40 +0530 | [diff] [blame] | 88 | return NSS_TX_FAILURE_NOT_READY; |
| 89 | } |
| 90 | |
Pamidipati, Vijay | b6e3884 | 2014-09-16 10:26:05 +0530 | [diff] [blame] | 91 | nbuf = dev_alloc_skb(NSS_NBUF_PAYLOAD_SIZE); |
Tushar Mathur | 6650654 | 2014-04-03 22:01:40 +0530 | [diff] [blame] | 92 | if (unlikely(!nbuf)) { |
Sundarajan Srinivasan | 62fee7e | 2015-01-22 11:13:10 -0800 | [diff] [blame] | 93 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_NBUF_ALLOC_FAILS]); |
Sol Kavy | cd1bd5c | 2014-04-04 11:09:44 -0700 | [diff] [blame] | 94 | nss_warning("%p: LAG msg dropped as command allocation failed", nss_ctx); |
Tushar Mathur | 6650654 | 2014-04-03 22:01:40 +0530 | [diff] [blame] | 95 | return NSS_TX_FAILURE; |
| 96 | } |
| 97 | |
| 98 | nm = (struct nss_lag_msg *)skb_put(nbuf, sizeof(struct nss_lag_msg)); |
| 99 | memcpy(nm, msg, sizeof(struct nss_lag_msg)); |
| 100 | |
| 101 | status = nss_core_send_buffer(nss_ctx, 0, nbuf, NSS_IF_CMD_QUEUE, H2N_BUFFER_CTRL, 0); |
| 102 | if (status != NSS_CORE_STATUS_SUCCESS) { |
Pamidipati, Vijay | b6e3884 | 2014-09-16 10:26:05 +0530 | [diff] [blame] | 103 | dev_kfree_skb_any(nbuf); |
Sol Kavy | cd1bd5c | 2014-04-04 11:09:44 -0700 | [diff] [blame] | 104 | nss_warning("%p: Unable to enqueue LAG msg\n", nss_ctx); |
| 105 | return NSS_TX_FAILURE; |
Tushar Mathur | 6650654 | 2014-04-03 22:01:40 +0530 | [diff] [blame] | 106 | } |
Stephen Wang | 90c67de | 2016-04-26 15:15:59 -0700 | [diff] [blame] | 107 | nss_hal_send_interrupt(nss_ctx, NSS_H2N_INTR_DATA_COMMAND_QUEUE); |
Tushar Mathur | 6650654 | 2014-04-03 22:01:40 +0530 | [diff] [blame] | 108 | |
| 109 | NSS_PKT_STATS_INCREMENT(nss_ctx, &nss_ctx->nss_top->stats_drv[NSS_STATS_DRV_TX_CMD_REQ]); |
| 110 | return NSS_TX_SUCCESS; |
| 111 | } |
| 112 | EXPORT_SYMBOL(nss_lag_tx); |
Tushar Mathur | a3e0305 | 2014-04-07 20:17:28 +0530 | [diff] [blame] | 113 | |
| 114 | /** |
| 115 | * nss_register_lag_if() |
| 116 | */ |
Gareth Williams | b52af51 | 2014-04-25 19:31:15 +0100 | [diff] [blame] | 117 | void *nss_register_lag_if(uint32_t if_num, |
Tushar Mathur | a3e0305 | 2014-04-07 20:17:28 +0530 | [diff] [blame] | 118 | nss_lag_callback_t lag_cb, |
| 119 | nss_lag_event_callback_t lag_ev_cb, |
| 120 | struct net_device *netdev) |
| 121 | { |
Bhaskar Valaboju | 76988ef | 2017-02-20 19:51:49 +0530 | [diff] [blame] | 122 | struct nss_ctx_instance *nss_ctx = nss_lag_get_context(); |
Sundarajan Srinivasan | 7037484 | 2014-11-19 15:22:52 -0800 | [diff] [blame] | 123 | uint32_t features = 0; |
| 124 | |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 125 | nss_assert(nss_ctx); |
Bhaskar Valaboju | 76988ef | 2017-02-20 19:51:49 +0530 | [diff] [blame] | 126 | nss_lag_verify_ifnum(if_num); |
Tushar Mathur | a3e0305 | 2014-04-07 20:17:28 +0530 | [diff] [blame] | 127 | |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 128 | nss_ctx->subsys_dp_register[if_num].ndev = netdev; |
| 129 | nss_ctx->subsys_dp_register[if_num].cb = lag_cb; |
| 130 | nss_ctx->subsys_dp_register[if_num].app_data = NULL; |
| 131 | nss_ctx->subsys_dp_register[if_num].features = features; |
Sundarajan Srinivasan | 7037484 | 2014-11-19 15:22:52 -0800 | [diff] [blame] | 132 | |
Tushar Mathur | a3e0305 | 2014-04-07 20:17:28 +0530 | [diff] [blame] | 133 | nss_top_main.lag_event_callback = lag_ev_cb; |
Gareth Williams | b52af51 | 2014-04-25 19:31:15 +0100 | [diff] [blame] | 134 | |
| 135 | /* |
| 136 | * Return the NSS driver context for LAG (same as for ipv4 functions) |
| 137 | */ |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 138 | return (void *)nss_ctx; |
Tushar Mathur | a3e0305 | 2014-04-07 20:17:28 +0530 | [diff] [blame] | 139 | } |
| 140 | EXPORT_SYMBOL(nss_register_lag_if); |
| 141 | |
| 142 | |
| 143 | /** |
| 144 | * nss_unregister_lag_if() |
| 145 | */ |
| 146 | void nss_unregister_lag_if(uint32_t if_num) |
| 147 | { |
Bhaskar Valaboju | 76988ef | 2017-02-20 19:51:49 +0530 | [diff] [blame] | 148 | struct nss_ctx_instance *nss_ctx = nss_lag_get_context(); |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 149 | |
| 150 | nss_assert(nss_ctx); |
Bhaskar Valaboju | 76988ef | 2017-02-20 19:51:49 +0530 | [diff] [blame] | 151 | nss_lag_verify_ifnum(if_num); |
Tushar Mathur | a3e0305 | 2014-04-07 20:17:28 +0530 | [diff] [blame] | 152 | |
Stephen Wang | 84e0e99 | 2016-09-07 12:31:40 -0700 | [diff] [blame] | 153 | nss_ctx->subsys_dp_register[if_num].cb = NULL; |
| 154 | nss_ctx->subsys_dp_register[if_num].ndev = NULL; |
| 155 | nss_ctx->subsys_dp_register[if_num].app_data = NULL; |
| 156 | nss_ctx->subsys_dp_register[if_num].features = 0; |
Sundarajan Srinivasan | 7037484 | 2014-11-19 15:22:52 -0800 | [diff] [blame] | 157 | |
Tushar Mathur | a3e0305 | 2014-04-07 20:17:28 +0530 | [diff] [blame] | 158 | nss_top_main.lag_event_callback = NULL; |
| 159 | } |
| 160 | EXPORT_SYMBOL(nss_unregister_lag_if); |
| 161 | |
| 162 | |
| 163 | /** |
| 164 | * nss_lag_handler() |
| 165 | */ |
| 166 | void nss_lag_handler(struct nss_ctx_instance *nss_ctx, |
| 167 | struct nss_cmn_msg *ncm, |
| 168 | void *app_data) |
| 169 | { |
| 170 | struct nss_lag_msg *lm = (struct nss_lag_msg *)ncm; |
| 171 | void *ctx = NULL; |
| 172 | nss_lag_event_callback_t cb; |
| 173 | |
| 174 | BUG_ON(ncm->interface != NSS_LAG0_INTERFACE_NUM |
Suman Ghosh | cf8ed1c | 2015-09-07 18:57:04 +0530 | [diff] [blame] | 175 | && ncm->interface != NSS_LAG1_INTERFACE_NUM |
| 176 | && ncm->interface != NSS_LAG2_INTERFACE_NUM |
| 177 | && ncm->interface != NSS_LAG3_INTERFACE_NUM); |
Tushar Mathur | a3e0305 | 2014-04-07 20:17:28 +0530 | [diff] [blame] | 178 | |
| 179 | if (ncm->type >= NSS_TX_METADATA_LAG_MAX) { |
| 180 | nss_warning("%p: received invalid message %d for LAG interface", nss_ctx, ncm->type); |
| 181 | return; |
| 182 | } |
| 183 | |
Suruchi Agarwal | ef8a870 | 2016-01-08 12:40:08 -0800 | [diff] [blame] | 184 | if (nss_cmn_get_msg_len(ncm) > sizeof(struct nss_lag_msg)) { |
| 185 | nss_warning("%p: invalid length for LAG message: %d", nss_ctx, nss_cmn_get_msg_len(ncm)); |
Tushar Mathur | a3e0305 | 2014-04-07 20:17:28 +0530 | [diff] [blame] | 186 | return; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Update the callback and app_data for NOTIFY messages. |
| 191 | * LAG sends all notify messages to the same callback. |
| 192 | */ |
| 193 | if (ncm->response == NSS_CMM_RESPONSE_NOTIFY) { |
Stephen Wang | aed4633 | 2016-12-12 17:29:03 -0800 | [diff] [blame] | 194 | ncm->cb = (nss_ptr_t)nss_ctx->nss_top->lag_event_callback; |
Tushar Mathur | a3e0305 | 2014-04-07 20:17:28 +0530 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /** |
| 198 | * Log failures |
| 199 | */ |
| 200 | nss_core_log_msg_failures(nss_ctx, ncm); |
| 201 | |
| 202 | /** |
| 203 | * Do we have a call back |
| 204 | */ |
| 205 | if (!ncm->cb) { |
| 206 | return; |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * callback |
| 211 | */ |
| 212 | cb = (nss_lag_event_callback_t)ncm->cb; |
Bhaskar Valaboju | 76988ef | 2017-02-20 19:51:49 +0530 | [diff] [blame] | 213 | ctx = (void *)ncm->app_data; |
Tushar Mathur | a3e0305 | 2014-04-07 20:17:28 +0530 | [diff] [blame] | 214 | |
| 215 | cb(ctx, lm); |
| 216 | } |
| 217 | |
| 218 | |
| 219 | /** |
| 220 | * nss_lag_register_handler() |
| 221 | */ |
| 222 | void nss_lag_register_handler(void) |
| 223 | { |
Thomas Wu | 91f4bdf | 2017-06-09 12:03:02 -0700 | [diff] [blame] | 224 | struct nss_ctx_instance *nss_ctx = nss_lag_get_context(); |
| 225 | |
| 226 | nss_core_register_handler(nss_ctx, NSS_LAG0_INTERFACE_NUM, nss_lag_handler, NULL); |
| 227 | nss_core_register_handler(nss_ctx, NSS_LAG1_INTERFACE_NUM, nss_lag_handler, NULL); |
| 228 | nss_core_register_handler(nss_ctx, NSS_LAG2_INTERFACE_NUM, nss_lag_handler, NULL); |
| 229 | nss_core_register_handler(nss_ctx, NSS_LAG3_INTERFACE_NUM, nss_lag_handler, NULL); |
Tushar Mathur | a3e0305 | 2014-04-07 20:17:28 +0530 | [diff] [blame] | 230 | } |
| 231 | |
Sundarajan Srinivasan | 02e6c2b | 2014-10-06 11:51:12 -0700 | [diff] [blame] | 232 | /** |
| 233 | * nss_lag_msg_init() |
| 234 | * Initialize lag message |
| 235 | */ |
| 236 | void nss_lag_msg_init(struct nss_lag_msg *nlm, uint16_t lag_num, uint32_t type, uint32_t len, |
Bhaskar Valaboju | 76988ef | 2017-02-20 19:51:49 +0530 | [diff] [blame] | 237 | nss_lag_msg_callback_t cb, void *app_data) |
Sundarajan Srinivasan | 02e6c2b | 2014-10-06 11:51:12 -0700 | [diff] [blame] | 238 | { |
| 239 | nss_cmn_msg_init(&nlm->cm, lag_num, type, len, (void *)cb, app_data); |
| 240 | } |
| 241 | EXPORT_SYMBOL(nss_lag_msg_init); |
Bhaskar Valaboju | 76988ef | 2017-02-20 19:51:49 +0530 | [diff] [blame] | 242 | |
| 243 | /** |
| 244 | * nss_lag_tx_slave_state() |
| 245 | */ |
| 246 | nss_tx_status_t nss_lag_tx_slave_state(uint16_t lagid, int32_t slave_ifnum, |
| 247 | enum nss_lag_state_change_ev slave_state) |
| 248 | { |
| 249 | struct nss_lag_msg nm; |
| 250 | struct nss_lag_state_change *nlsc = NULL; |
| 251 | nss_tx_status_t status; |
| 252 | int ret; |
| 253 | struct nss_ctx_instance *nss_ctx = nss_lag_get_context(); |
| 254 | struct nss_lag_pvt lag_msg_state; |
| 255 | |
| 256 | init_completion(&lag_msg_state.complete); |
| 257 | lag_msg_state.response = false; |
| 258 | |
| 259 | /* |
| 260 | * Construct a message to the NSS to update it |
| 261 | */ |
| 262 | nss_lag_msg_init(&nm, lagid, |
| 263 | NSS_TX_METADATA_LAG_STATE_CHANGE, |
| 264 | sizeof(struct nss_lag_state_change), |
| 265 | nss_lag_state_callback, &lag_msg_state); |
| 266 | |
| 267 | nlsc = &nm.msg.state; |
| 268 | nlsc->event = slave_state; |
| 269 | nlsc->interface = slave_ifnum; |
| 270 | |
| 271 | status = nss_lag_tx(nss_ctx, &nm); |
| 272 | if (status != NSS_TX_SUCCESS) { |
| 273 | nss_warning("%p: Send LAG update failed, status: %d\n", nss_ctx, |
| 274 | status); |
| 275 | return NSS_TX_FAILURE; |
| 276 | } |
| 277 | |
| 278 | /* |
| 279 | * Blocking call, wait till we get ACK for this msg. |
| 280 | */ |
| 281 | ret = wait_for_completion_timeout(&lag_msg_state.complete, |
| 282 | msecs_to_jiffies(NSS_LAG_RESP_TIMEOUT)); |
| 283 | if (!ret) { |
| 284 | nss_warning("%p: Waiting for ack timed out\n", nss_ctx); |
| 285 | return NSS_TX_FAILURE; |
| 286 | } |
| 287 | |
| 288 | return lag_msg_state.response; |
| 289 | } |
| 290 | EXPORT_SYMBOL(nss_lag_tx_slave_state); |
| 291 | |