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 mlx5_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 mlx5_priv *priv = dev->data->dev_private; 46 unsigned int i; 47 int ret; 48 49 for (i = 0; i != priv->txqs_n; ++i) { 50 struct mlx5_txq_ctrl *txq_ctrl = mlx5_txq_get(dev, i); 51 52 if (!txq_ctrl) 53 continue; 54 txq_alloc_elts(txq_ctrl); 55 txq_ctrl->ibv = mlx5_txq_ibv_new(dev, i); 56 if (!txq_ctrl->ibv) { 57 rte_errno = ENOMEM; 58 goto error; 59 } 60 } 61 return 0; 62 error: 63 ret = rte_errno; /* Save rte_errno before cleanup. */ 64 do { 65 mlx5_txq_release(dev, i); 66 } while (i-- != 0); 67 rte_errno = ret; /* Restore rte_errno. */ 68 return -rte_errno; 69 } 70 71 /** 72 * Stop traffic on Rx queues. 73 * 74 * @param dev 75 * Pointer to Ethernet device structure. 76 */ 77 static void 78 mlx5_rxq_stop(struct rte_eth_dev *dev) 79 { 80 struct mlx5_priv *priv = dev->data->dev_private; 81 unsigned int i; 82 83 for (i = 0; i != priv->rxqs_n; ++i) 84 mlx5_rxq_release(dev, i); 85 } 86 87 /** 88 * Start traffic on Rx queues. 89 * 90 * @param dev 91 * Pointer to Ethernet device structure. 92 * 93 * @return 94 * 0 on success, a negative errno value otherwise and rte_errno is set. 95 */ 96 static int 97 mlx5_rxq_start(struct rte_eth_dev *dev) 98 { 99 struct mlx5_priv *priv = dev->data->dev_private; 100 unsigned int i; 101 int ret = 0; 102 103 /* Allocate/reuse/resize mempool for Multi-Packet RQ. */ 104 if (mlx5_mprq_alloc_mp(dev)) { 105 /* Should not release Rx queues but return immediately. */ 106 return -rte_errno; 107 } 108 for (i = 0; i != priv->rxqs_n; ++i) { 109 struct mlx5_rxq_ctrl *rxq_ctrl = mlx5_rxq_get(dev, i); 110 struct rte_mempool *mp; 111 112 if (!rxq_ctrl) 113 continue; 114 /* Pre-register Rx mempool. */ 115 mp = mlx5_rxq_mprq_enabled(&rxq_ctrl->rxq) ? 116 rxq_ctrl->rxq.mprq_mp : rxq_ctrl->rxq.mp; 117 DRV_LOG(DEBUG, 118 "port %u Rx queue %u registering" 119 " mp %s having %u chunks", 120 dev->data->port_id, rxq_ctrl->rxq.idx, 121 mp->name, mp->nb_mem_chunks); 122 mlx5_mr_update_mp(dev, &rxq_ctrl->rxq.mr_ctrl, mp); 123 ret = rxq_alloc_elts(rxq_ctrl); 124 if (ret) 125 goto error; 126 rxq_ctrl->ibv = mlx5_rxq_ibv_new(dev, i); 127 if (!rxq_ctrl->ibv) 128 goto error; 129 } 130 return 0; 131 error: 132 ret = rte_errno; /* Save rte_errno before cleanup. */ 133 do { 134 mlx5_rxq_release(dev, i); 135 } while (i-- != 0); 136 rte_errno = ret; /* Restore rte_errno. */ 137 return -rte_errno; 138 } 139 140 /** 141 * DPDK callback to start the device. 142 * 143 * Simulate device start by attaching all configured flows. 144 * 145 * @param dev 146 * Pointer to Ethernet device structure. 147 * 148 * @return 149 * 0 on success, a negative errno value otherwise and rte_errno is set. 150 */ 151 int 152 mlx5_dev_start(struct rte_eth_dev *dev) 153 { 154 struct mlx5_priv *priv = dev->data->dev_private; 155 int ret; 156 157 DRV_LOG(DEBUG, "port %u starting device", dev->data->port_id); 158 ret = mlx5_txq_start(dev); 159 if (ret) { 160 DRV_LOG(ERR, "port %u Tx queue allocation failed: %s", 161 dev->data->port_id, strerror(rte_errno)); 162 return -rte_errno; 163 } 164 ret = mlx5_rxq_start(dev); 165 if (ret) { 166 DRV_LOG(ERR, "port %u Rx queue allocation failed: %s", 167 dev->data->port_id, strerror(rte_errno)); 168 mlx5_txq_stop(dev); 169 return -rte_errno; 170 } 171 dev->data->dev_started = 1; 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_stats_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 rte_wmb(); 192 dev->tx_pkt_burst = mlx5_select_tx_function(dev); 193 dev->rx_pkt_burst = mlx5_select_rx_function(dev); 194 /* Enable datapath on secondary process. */ 195 mlx5_mp_req_start_rxtx(dev); 196 mlx5_dev_interrupt_handler_install(dev); 197 return 0; 198 error: 199 ret = rte_errno; /* Save rte_errno before cleanup. */ 200 /* Rollback. */ 201 dev->data->dev_started = 0; 202 mlx5_flow_stop(dev, &priv->flows); 203 mlx5_traffic_disable(dev); 204 mlx5_txq_stop(dev); 205 mlx5_rxq_stop(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 mlx5_priv *priv = dev->data->dev_private; 222 223 dev->data->dev_started = 0; 224 /* Prevent crashes when queues are still in use. */ 225 dev->rx_pkt_burst = removed_rx_burst; 226 dev->tx_pkt_burst = removed_tx_burst; 227 rte_wmb(); 228 /* Disable datapath on secondary process. */ 229 mlx5_mp_req_stop_rxtx(dev); 230 usleep(1000 * priv->rxqs_n); 231 DRV_LOG(DEBUG, "port %u stopping device", 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 } 239 240 /** 241 * Enable traffic flows configured by control plane 242 * 243 * @param dev 244 * Pointer to Ethernet device private data. 245 * @param dev 246 * Pointer to Ethernet device structure. 247 * 248 * @return 249 * 0 on success, a negative errno value otherwise and rte_errno is set. 250 */ 251 int 252 mlx5_traffic_enable(struct rte_eth_dev *dev) 253 { 254 struct mlx5_priv *priv = dev->data->dev_private; 255 struct rte_flow_item_eth bcast = { 256 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff", 257 }; 258 struct rte_flow_item_eth ipv6_multi_spec = { 259 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00", 260 }; 261 struct rte_flow_item_eth ipv6_multi_mask = { 262 .dst.addr_bytes = "\xff\xff\x00\x00\x00\x00", 263 }; 264 struct rte_flow_item_eth unicast = { 265 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00", 266 }; 267 struct rte_flow_item_eth unicast_mask = { 268 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff", 269 }; 270 const unsigned int vlan_filter_n = priv->vlan_filter_n; 271 const struct ether_addr cmp = { 272 .addr_bytes = "\x00\x00\x00\x00\x00\x00", 273 }; 274 unsigned int i; 275 unsigned int j; 276 int ret; 277 278 if (priv->isolated) 279 return 0; 280 if (dev->data->promiscuous) { 281 struct rte_flow_item_eth promisc = { 282 .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00", 283 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00", 284 .type = 0, 285 }; 286 287 ret = mlx5_ctrl_flow(dev, &promisc, &promisc); 288 if (ret) 289 goto error; 290 } 291 if (dev->data->all_multicast) { 292 struct rte_flow_item_eth multicast = { 293 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00", 294 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00", 295 .type = 0, 296 }; 297 298 ret = mlx5_ctrl_flow(dev, &multicast, &multicast); 299 if (ret) 300 goto error; 301 } else { 302 /* Add broadcast/multicast flows. */ 303 for (i = 0; i != vlan_filter_n; ++i) { 304 uint16_t vlan = priv->vlan_filter[i]; 305 306 struct rte_flow_item_vlan vlan_spec = { 307 .tci = rte_cpu_to_be_16(vlan), 308 }; 309 struct rte_flow_item_vlan vlan_mask = 310 rte_flow_item_vlan_mask; 311 312 ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast, 313 &vlan_spec, &vlan_mask); 314 if (ret) 315 goto error; 316 ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec, 317 &ipv6_multi_mask, 318 &vlan_spec, &vlan_mask); 319 if (ret) 320 goto error; 321 } 322 if (!vlan_filter_n) { 323 ret = mlx5_ctrl_flow(dev, &bcast, &bcast); 324 if (ret) 325 goto error; 326 ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec, 327 &ipv6_multi_mask); 328 if (ret) 329 goto error; 330 } 331 } 332 /* Add MAC address flows. */ 333 for (i = 0; i != MLX5_MAX_MAC_ADDRESSES; ++i) { 334 struct ether_addr *mac = &dev->data->mac_addrs[i]; 335 336 if (!memcmp(mac, &cmp, sizeof(*mac))) 337 continue; 338 memcpy(&unicast.dst.addr_bytes, 339 mac->addr_bytes, 340 ETHER_ADDR_LEN); 341 for (j = 0; j != vlan_filter_n; ++j) { 342 uint16_t vlan = priv->vlan_filter[j]; 343 344 struct rte_flow_item_vlan vlan_spec = { 345 .tci = rte_cpu_to_be_16(vlan), 346 }; 347 struct rte_flow_item_vlan vlan_mask = 348 rte_flow_item_vlan_mask; 349 350 ret = mlx5_ctrl_flow_vlan(dev, &unicast, 351 &unicast_mask, 352 &vlan_spec, 353 &vlan_mask); 354 if (ret) 355 goto error; 356 } 357 if (!vlan_filter_n) { 358 ret = mlx5_ctrl_flow(dev, &unicast, &unicast_mask); 359 if (ret) 360 goto error; 361 } 362 } 363 return 0; 364 error: 365 ret = rte_errno; /* Save rte_errno before cleanup. */ 366 mlx5_flow_list_flush(dev, &priv->ctrl_flows); 367 rte_errno = ret; /* Restore rte_errno. */ 368 return -rte_errno; 369 } 370 371 372 /** 373 * Disable traffic flows configured by control plane 374 * 375 * @param dev 376 * Pointer to Ethernet device private data. 377 */ 378 void 379 mlx5_traffic_disable(struct rte_eth_dev *dev) 380 { 381 struct mlx5_priv *priv = dev->data->dev_private; 382 383 mlx5_flow_list_flush(dev, &priv->ctrl_flows); 384 } 385 386 /** 387 * Restart traffic flows configured by control plane 388 * 389 * @param dev 390 * Pointer to Ethernet device private data. 391 * 392 * @return 393 * 0 on success, a negative errno value otherwise and rte_errno is set. 394 */ 395 int 396 mlx5_traffic_restart(struct rte_eth_dev *dev) 397 { 398 if (dev->data->dev_started) { 399 mlx5_traffic_disable(dev); 400 return mlx5_traffic_enable(dev); 401 } 402 return 0; 403 } 404