4 * Copyright 2015 6WIND S.A.
5 * Copyright 2015 Mellanox.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of 6WIND S.A. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 #include <rte_ether.h>
36 #include <rte_ethdev.h>
37 #include <rte_interrupts.h>
38 #include <rte_alarm.h>
41 #include "mlx5_rxtx.h"
42 #include "mlx5_utils.h"
45 * Stop traffic on Tx queues.
48 * Pointer to Ethernet device structure.
51 mlx5_txq_stop(struct rte_eth_dev *dev)
53 struct priv *priv = dev->data->dev_private;
56 for (i = 0; i != priv->txqs_n; ++i)
57 mlx5_txq_release(dev, i);
61 * Start traffic on Tx queues.
64 * Pointer to Ethernet device structure.
67 * 0 on success, a negative errno value otherwise and rte_errno is set.
70 mlx5_txq_start(struct rte_eth_dev *dev)
72 struct priv *priv = dev->data->dev_private;
76 for (i = 0; i != priv->txqs_n; ++i) {
79 struct mlx5_txq_ctrl *txq_ctrl = mlx5_txq_get(dev, i);
83 LIST_FOREACH(mr, &priv->mr, next) {
84 mlx5_txq_mp2mr_reg(&txq_ctrl->txq, mr->mp, idx++);
85 if (idx == MLX5_PMD_TX_MP_CACHE)
88 txq_alloc_elts(txq_ctrl);
89 txq_ctrl->ibv = mlx5_txq_ibv_new(dev, i);
95 ret = mlx5_tx_uar_remap(dev, priv->ctx->cmd_fd);
97 /* Adjust index for rollback. */
103 ret = rte_errno; /* Save rte_errno before cleanup. */
105 mlx5_txq_release(dev, i);
107 rte_errno = ret; /* Restore rte_errno. */
112 * Stop traffic on Rx queues.
115 * Pointer to Ethernet device structure.
118 mlx5_rxq_stop(struct rte_eth_dev *dev)
120 struct priv *priv = dev->data->dev_private;
123 for (i = 0; i != priv->rxqs_n; ++i)
124 mlx5_rxq_release(dev, i);
128 * Start traffic on Rx queues.
131 * Pointer to Ethernet device structure.
134 * 0 on success, a negative errno value otherwise and rte_errno is set.
137 mlx5_rxq_start(struct rte_eth_dev *dev)
139 struct priv *priv = dev->data->dev_private;
143 for (i = 0; i != priv->rxqs_n; ++i) {
144 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_rxq_get(dev, i);
148 ret = rxq_alloc_elts(rxq_ctrl);
151 rxq_ctrl->ibv = mlx5_rxq_ibv_new(dev, i);
157 ret = rte_errno; /* Save rte_errno before cleanup. */
159 mlx5_rxq_release(dev, i);
161 rte_errno = ret; /* Restore rte_errno. */
166 * DPDK callback to start the device.
168 * Simulate device start by attaching all configured flows.
171 * Pointer to Ethernet device structure.
174 * 0 on success, a negative errno value otherwise and rte_errno is set.
177 mlx5_dev_start(struct rte_eth_dev *dev)
179 struct priv *priv = dev->data->dev_private;
180 struct mlx5_mr *mr = NULL;
183 DRV_LOG(DEBUG, "port %u starting device", dev->data->port_id);
184 ret = mlx5_flow_create_drop_queue(dev);
186 DRV_LOG(ERR, "port %u drop queue allocation failed: %s",
187 dev->data->port_id, strerror(rte_errno));
190 rte_mempool_walk(mlx5_mp2mr_iter, priv);
191 ret = mlx5_txq_start(dev);
193 DRV_LOG(ERR, "port %u Tx queue allocation failed: %s",
194 dev->data->port_id, strerror(rte_errno));
197 ret = mlx5_rxq_start(dev);
199 DRV_LOG(ERR, "port %u Rx queue allocation failed: %s",
200 dev->data->port_id, strerror(rte_errno));
204 dev->data->dev_started = 1;
205 ret = mlx5_rx_intr_vec_enable(dev);
207 DRV_LOG(ERR, "port %u Rx interrupt vector creation failed",
211 mlx5_xstats_init(dev);
212 ret = mlx5_traffic_enable(dev);
214 DRV_LOG(DEBUG, "port %u failed to set defaults flows",
218 ret = mlx5_flow_start(dev, &priv->flows);
220 DRV_LOG(DEBUG, "port %u failed to set flows",
224 dev->tx_pkt_burst = mlx5_select_tx_function(dev);
225 dev->rx_pkt_burst = mlx5_select_rx_function(dev);
226 mlx5_dev_interrupt_handler_install(dev);
229 ret = rte_errno; /* Save rte_errno before cleanup. */
231 dev->data->dev_started = 0;
232 for (mr = LIST_FIRST(&priv->mr); mr; mr = LIST_FIRST(&priv->mr))
234 mlx5_flow_stop(dev, &priv->flows);
235 mlx5_traffic_disable(dev);
238 mlx5_flow_delete_drop_queue(dev);
239 rte_errno = ret; /* Restore rte_errno. */
244 * DPDK callback to stop the device.
246 * Simulate device stop by detaching all configured flows.
249 * Pointer to Ethernet device structure.
252 mlx5_dev_stop(struct rte_eth_dev *dev)
254 struct priv *priv = dev->data->dev_private;
257 dev->data->dev_started = 0;
258 /* Prevent crashes when queues are still in use. */
259 dev->rx_pkt_burst = removed_rx_burst;
260 dev->tx_pkt_burst = removed_tx_burst;
262 usleep(1000 * priv->rxqs_n);
263 DRV_LOG(DEBUG, "port %u stopping device", dev->data->port_id);
264 mlx5_flow_stop(dev, &priv->flows);
265 mlx5_traffic_disable(dev);
266 mlx5_rx_intr_vec_disable(dev);
267 mlx5_dev_interrupt_handler_uninstall(dev);
270 for (mr = LIST_FIRST(&priv->mr); mr; mr = LIST_FIRST(&priv->mr))
272 mlx5_flow_delete_drop_queue(dev);
276 * Enable traffic flows configured by control plane
279 * Pointer to Ethernet device private data.
281 * Pointer to Ethernet device structure.
284 * 0 on success, a negative errno value otherwise and rte_errno is set.
287 mlx5_traffic_enable(struct rte_eth_dev *dev)
289 struct priv *priv = dev->data->dev_private;
290 struct rte_flow_item_eth bcast = {
291 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
293 struct rte_flow_item_eth ipv6_multi_spec = {
294 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00",
296 struct rte_flow_item_eth ipv6_multi_mask = {
297 .dst.addr_bytes = "\xff\xff\x00\x00\x00\x00",
299 struct rte_flow_item_eth unicast = {
300 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
302 struct rte_flow_item_eth unicast_mask = {
303 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff",
305 const unsigned int vlan_filter_n = priv->vlan_filter_n;
306 const struct ether_addr cmp = {
307 .addr_bytes = "\x00\x00\x00\x00\x00\x00",
315 if (dev->data->promiscuous) {
316 struct rte_flow_item_eth promisc = {
317 .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00",
318 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
322 ret = mlx5_ctrl_flow(dev, &promisc, &promisc);
326 if (dev->data->all_multicast) {
327 struct rte_flow_item_eth multicast = {
328 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00",
329 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00",
333 ret = mlx5_ctrl_flow(dev, &multicast, &multicast);
337 /* Add broadcast/multicast flows. */
338 for (i = 0; i != vlan_filter_n; ++i) {
339 uint16_t vlan = priv->vlan_filter[i];
341 struct rte_flow_item_vlan vlan_spec = {
342 .tci = rte_cpu_to_be_16(vlan),
344 struct rte_flow_item_vlan vlan_mask =
345 rte_flow_item_vlan_mask;
347 ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast,
348 &vlan_spec, &vlan_mask);
351 ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec,
353 &vlan_spec, &vlan_mask);
357 if (!vlan_filter_n) {
358 ret = mlx5_ctrl_flow(dev, &bcast, &bcast);
361 ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec,
367 /* Add MAC address flows. */
368 for (i = 0; i != MLX5_MAX_MAC_ADDRESSES; ++i) {
369 struct ether_addr *mac = &dev->data->mac_addrs[i];
371 if (!memcmp(mac, &cmp, sizeof(*mac)))
373 memcpy(&unicast.dst.addr_bytes,
376 for (j = 0; j != vlan_filter_n; ++j) {
377 uint16_t vlan = priv->vlan_filter[j];
379 struct rte_flow_item_vlan vlan_spec = {
380 .tci = rte_cpu_to_be_16(vlan),
382 struct rte_flow_item_vlan vlan_mask =
383 rte_flow_item_vlan_mask;
385 ret = mlx5_ctrl_flow_vlan(dev, &unicast,
392 if (!vlan_filter_n) {
393 ret = mlx5_ctrl_flow(dev, &unicast, &unicast_mask);
400 ret = rte_errno; /* Save rte_errno before cleanup. */
401 mlx5_flow_list_flush(dev, &priv->ctrl_flows);
402 rte_errno = ret; /* Restore rte_errno. */
408 * Disable traffic flows configured by control plane
411 * Pointer to Ethernet device private data.
414 mlx5_traffic_disable(struct rte_eth_dev *dev)
416 struct priv *priv = dev->data->dev_private;
418 mlx5_flow_list_flush(dev, &priv->ctrl_flows);
422 * Restart traffic flows configured by control plane
425 * Pointer to Ethernet device private data.
428 * 0 on success, a negative errno value otherwise and rte_errno is set.
431 mlx5_traffic_restart(struct rte_eth_dev *dev)
433 if (dev->data->dev_started) {
434 mlx5_traffic_disable(dev);
435 return mlx5_traffic_enable(dev);