1 /* SPDX-License-Identifier: BSD-3-Clause 2 * Copyright 2015 6WIND S.A. 3 * Copyright 2015 Mellanox Technologies, Ltd 4 */ 5 6 #include <unistd.h> 7 8 #include <rte_ether.h> 9 #include <rte_ethdev_driver.h> 10 #include <rte_interrupts.h> 11 #include <rte_alarm.h> 12 13 #include "mlx5.h" 14 #include "mlx5_rxtx.h" 15 #include "mlx5_utils.h" 16 17 /** 18 * Stop traffic on Tx queues. 19 * 20 * @param dev 21 * Pointer to Ethernet device structure. 22 */ 23 static void 24 mlx5_txq_stop(struct rte_eth_dev *dev) 25 { 26 struct priv *priv = dev->data->dev_private; 27 unsigned int i; 28 29 for (i = 0; i != priv->txqs_n; ++i) 30 mlx5_txq_release(dev, i); 31 } 32 33 /** 34 * Start traffic on Tx queues. 35 * 36 * @param dev 37 * Pointer to Ethernet device structure. 38 * 39 * @return 40 * 0 on success, a negative errno value otherwise and rte_errno is set. 41 */ 42 static int 43 mlx5_txq_start(struct rte_eth_dev *dev) 44 { 45 struct priv *priv = dev->data->dev_private; 46 unsigned int i; 47 int ret; 48 49 /* Add memory regions to Tx queues. */ 50 for (i = 0; i != priv->txqs_n; ++i) { 51 unsigned int idx = 0; 52 struct mlx5_mr *mr; 53 struct mlx5_txq_ctrl *txq_ctrl = mlx5_txq_get(dev, i); 54 55 if (!txq_ctrl) 56 continue; 57 LIST_FOREACH(mr, &priv->mr, next) { 58 mlx5_txq_mp2mr_reg(&txq_ctrl->txq, mr->mp, idx++); 59 if (idx == MLX5_PMD_TX_MP_CACHE) 60 break; 61 } 62 txq_alloc_elts(txq_ctrl); 63 txq_ctrl->ibv = mlx5_txq_ibv_new(dev, i); 64 if (!txq_ctrl->ibv) { 65 rte_errno = ENOMEM; 66 goto error; 67 } 68 } 69 ret = mlx5_tx_uar_remap(dev, priv->ctx->cmd_fd); 70 if (ret) 71 goto error; 72 return 0; 73 error: 74 ret = rte_errno; /* Save rte_errno before cleanup. */ 75 mlx5_txq_stop(dev); 76 rte_errno = ret; /* Restore rte_errno. */ 77 return -rte_errno; 78 } 79 80 /** 81 * Stop traffic on Rx queues. 82 * 83 * @param dev 84 * Pointer to Ethernet device structure. 85 */ 86 static void 87 mlx5_rxq_stop(struct rte_eth_dev *dev) 88 { 89 struct priv *priv = dev->data->dev_private; 90 unsigned int i; 91 92 for (i = 0; i != priv->rxqs_n; ++i) 93 mlx5_rxq_release(dev, i); 94 } 95 96 /** 97 * Start traffic on Rx queues. 98 * 99 * @param dev 100 * Pointer to Ethernet device structure. 101 * 102 * @return 103 * 0 on success, a negative errno value otherwise and rte_errno is set. 104 */ 105 static int 106 mlx5_rxq_start(struct rte_eth_dev *dev) 107 { 108 struct priv *priv = dev->data->dev_private; 109 unsigned int i; 110 int ret = 0; 111 112 for (i = 0; i != priv->rxqs_n; ++i) { 113 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_rxq_get(dev, i); 114 115 if (!rxq_ctrl) 116 continue; 117 ret = rxq_alloc_elts(rxq_ctrl); 118 if (ret) 119 goto error; 120 rxq_ctrl->ibv = mlx5_rxq_ibv_new(dev, i); 121 if (!rxq_ctrl->ibv) 122 goto error; 123 } 124 return 0; 125 error: 126 ret = rte_errno; /* Save rte_errno before cleanup. */ 127 mlx5_rxq_stop(dev); 128 rte_errno = ret; /* Restore rte_errno. */ 129 return -rte_errno; 130 } 131 132 /** 133 * DPDK callback to start the device. 134 * 135 * Simulate device start by attaching all configured flows. 136 * 137 * @param dev 138 * Pointer to Ethernet device structure. 139 * 140 * @return 141 * 0 on success, a negative errno value otherwise and rte_errno is set. 142 */ 143 int 144 mlx5_dev_start(struct rte_eth_dev *dev) 145 { 146 struct priv *priv = dev->data->dev_private; 147 struct mlx5_mr *mr = NULL; 148 int ret; 149 150 dev->data->dev_started = 1; 151 ret = mlx5_flow_create_drop_queue(dev); 152 if (ret) { 153 DRV_LOG(ERR, "port %u drop queue allocation failed: %s", 154 dev->data->port_id, strerror(rte_errno)); 155 goto error; 156 } 157 DRV_LOG(DEBUG, "port %u allocating and configuring hash Rx queues", 158 dev->data->port_id); 159 rte_mempool_walk(mlx5_mp2mr_iter, priv); 160 ret = mlx5_txq_start(dev); 161 if (ret) { 162 DRV_LOG(ERR, "port %u Tx queue allocation failed: %s", 163 dev->data->port_id, strerror(rte_errno)); 164 goto error; 165 } 166 ret = mlx5_rxq_start(dev); 167 if (ret) { 168 DRV_LOG(ERR, "port %u Rx queue allocation failed: %s", 169 dev->data->port_id, strerror(rte_errno)); 170 goto error; 171 } 172 ret = mlx5_rx_intr_vec_enable(dev); 173 if (ret) { 174 DRV_LOG(ERR, "port %u Rx interrupt vector creation failed", 175 dev->data->port_id); 176 goto error; 177 } 178 mlx5_xstats_init(dev); 179 ret = mlx5_traffic_enable(dev); 180 if (ret) { 181 DRV_LOG(DEBUG, "port %u failed to set defaults flows", 182 dev->data->port_id); 183 goto error; 184 } 185 ret = mlx5_flow_start(dev, &priv->flows); 186 if (ret) { 187 DRV_LOG(DEBUG, "port %u failed to set flows", 188 dev->data->port_id); 189 goto error; 190 } 191 dev->tx_pkt_burst = mlx5_select_tx_function(dev); 192 dev->rx_pkt_burst = mlx5_select_rx_function(dev); 193 mlx5_dev_interrupt_handler_install(dev); 194 return 0; 195 error: 196 ret = rte_errno; /* Save rte_errno before cleanup. */ 197 /* Rollback. */ 198 dev->data->dev_started = 0; 199 for (mr = LIST_FIRST(&priv->mr); mr; mr = LIST_FIRST(&priv->mr)) 200 mlx5_mr_release(mr); 201 mlx5_flow_stop(dev, &priv->flows); 202 mlx5_traffic_disable(dev); 203 mlx5_txq_stop(dev); 204 mlx5_rxq_stop(dev); 205 mlx5_flow_delete_drop_queue(dev); 206 rte_errno = ret; /* Restore rte_errno. */ 207 return -rte_errno; 208 } 209 210 /** 211 * DPDK callback to stop the device. 212 * 213 * Simulate device stop by detaching all configured flows. 214 * 215 * @param dev 216 * Pointer to Ethernet device structure. 217 */ 218 void 219 mlx5_dev_stop(struct rte_eth_dev *dev) 220 { 221 struct priv *priv = dev->data->dev_private; 222 struct mlx5_mr *mr; 223 224 dev->data->dev_started = 0; 225 /* Prevent crashes when queues are still in use. */ 226 dev->rx_pkt_burst = removed_rx_burst; 227 dev->tx_pkt_burst = removed_tx_burst; 228 rte_wmb(); 229 usleep(1000 * priv->rxqs_n); 230 DRV_LOG(DEBUG, "port %u cleaning up and destroying hash Rx queues", 231 dev->data->port_id); 232 mlx5_flow_stop(dev, &priv->flows); 233 mlx5_traffic_disable(dev); 234 mlx5_rx_intr_vec_disable(dev); 235 mlx5_dev_interrupt_handler_uninstall(dev); 236 mlx5_txq_stop(dev); 237 mlx5_rxq_stop(dev); 238 for (mr = LIST_FIRST(&priv->mr); mr; mr = LIST_FIRST(&priv->mr)) 239 mlx5_mr_release(mr); 240 mlx5_flow_delete_drop_queue(dev); 241 } 242 243 /** 244 * Enable traffic flows configured by control plane 245 * 246 * @param dev 247 * Pointer to Ethernet device private data. 248 * @param dev 249 * Pointer to Ethernet device structure. 250 * 251 * @return 252 * 0 on success, a negative errno value otherwise and rte_errno is set. 253 */ 254 int 255 mlx5_traffic_enable(struct rte_eth_dev *dev) 256 { 257 struct priv *priv = dev->data->dev_private; 258 struct rte_flow_item_eth bcast = { 259 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff", 260 }; 261 struct rte_flow_item_eth ipv6_multi_spec = { 262 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00", 263 }; 264 struct rte_flow_item_eth ipv6_multi_mask = { 265 .dst.addr_bytes = "\xff\xff\x00\x00\x00\x00", 266 }; 267 struct rte_flow_item_eth unicast = { 268 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00", 269 }; 270 struct rte_flow_item_eth unicast_mask = { 271 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff", 272 }; 273 const unsigned int vlan_filter_n = priv->vlan_filter_n; 274 const struct ether_addr cmp = { 275 .addr_bytes = "\x00\x00\x00\x00\x00\x00", 276 }; 277 unsigned int i; 278 unsigned int j; 279 int ret; 280 281 if (priv->isolated) 282 return 0; 283 if (dev->data->promiscuous) { 284 struct rte_flow_item_eth promisc = { 285 .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00", 286 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00", 287 .type = 0, 288 }; 289 290 ret = mlx5_ctrl_flow(dev, &promisc, &promisc); 291 if (ret) 292 goto error; 293 } 294 if (dev->data->all_multicast) { 295 struct rte_flow_item_eth multicast = { 296 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00", 297 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00", 298 .type = 0, 299 }; 300 301 ret = mlx5_ctrl_flow(dev, &multicast, &multicast); 302 if (ret) 303 goto error; 304 } else { 305 /* Add broadcast/multicast flows. */ 306 for (i = 0; i != vlan_filter_n; ++i) { 307 uint16_t vlan = priv->vlan_filter[i]; 308 309 struct rte_flow_item_vlan vlan_spec = { 310 .tci = rte_cpu_to_be_16(vlan), 311 }; 312 struct rte_flow_item_vlan vlan_mask = { 313 .tci = 0xffff, 314 }; 315 316 ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast, 317 &vlan_spec, &vlan_mask); 318 if (ret) 319 goto error; 320 ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec, 321 &ipv6_multi_mask, 322 &vlan_spec, &vlan_mask); 323 if (ret) 324 goto error; 325 } 326 if (!vlan_filter_n) { 327 ret = mlx5_ctrl_flow(dev, &bcast, &bcast); 328 if (ret) 329 goto error; 330 ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec, 331 &ipv6_multi_mask); 332 if (ret) 333 goto error; 334 } 335 } 336 /* Add MAC address flows. */ 337 for (i = 0; i != MLX5_MAX_MAC_ADDRESSES; ++i) { 338 struct ether_addr *mac = &dev->data->mac_addrs[i]; 339 340 if (!memcmp(mac, &cmp, sizeof(*mac))) 341 continue; 342 memcpy(&unicast.dst.addr_bytes, 343 mac->addr_bytes, 344 ETHER_ADDR_LEN); 345 for (j = 0; j != vlan_filter_n; ++j) { 346 uint16_t vlan = priv->vlan_filter[j]; 347 348 struct rte_flow_item_vlan vlan_spec = { 349 .tci = rte_cpu_to_be_16(vlan), 350 }; 351 struct rte_flow_item_vlan vlan_mask = { 352 .tci = 0xffff, 353 }; 354 355 ret = mlx5_ctrl_flow_vlan(dev, &unicast, 356 &unicast_mask, 357 &vlan_spec, 358 &vlan_mask); 359 if (ret) 360 goto error; 361 } 362 if (!vlan_filter_n) { 363 ret = mlx5_ctrl_flow(dev, &unicast, &unicast_mask); 364 if (ret) 365 goto error; 366 } 367 } 368 return 0; 369 error: 370 ret = rte_errno; /* Save rte_errno before cleanup. */ 371 mlx5_flow_list_flush(dev, &priv->ctrl_flows); 372 rte_errno = ret; /* Restore rte_errno. */ 373 return -rte_errno; 374 } 375 376 377 /** 378 * Disable traffic flows configured by control plane 379 * 380 * @param dev 381 * Pointer to Ethernet device private data. 382 */ 383 void 384 mlx5_traffic_disable(struct rte_eth_dev *dev) 385 { 386 struct priv *priv = dev->data->dev_private; 387 388 mlx5_flow_list_flush(dev, &priv->ctrl_flows); 389 } 390 391 /** 392 * Restart traffic flows configured by control plane 393 * 394 * @param dev 395 * Pointer to Ethernet device private data. 396 * 397 * @return 398 * 0 on success, a negative errno value otherwise and rte_errno is set. 399 */ 400 int 401 mlx5_traffic_restart(struct rte_eth_dev *dev) 402 { 403 if (dev->data->dev_started) { 404 mlx5_traffic_disable(dev); 405 return mlx5_traffic_enable(dev); 406 } 407 return 0; 408 } 409