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 rxq_ctrl->wqn = rxq_ctrl->ibv->wq->wq_num; 130 } 131 return 0; 132 error: 133 ret = rte_errno; /* Save rte_errno before cleanup. */ 134 do { 135 mlx5_rxq_release(dev, i); 136 } while (i-- != 0); 137 rte_errno = ret; /* Restore rte_errno. */ 138 return -rte_errno; 139 } 140 141 /** 142 * DPDK callback to start the device. 143 * 144 * Simulate device start by attaching all configured flows. 145 * 146 * @param dev 147 * Pointer to Ethernet device structure. 148 * 149 * @return 150 * 0 on success, a negative errno value otherwise and rte_errno is set. 151 */ 152 int 153 mlx5_dev_start(struct rte_eth_dev *dev) 154 { 155 struct mlx5_priv *priv = dev->data->dev_private; 156 int ret; 157 158 DRV_LOG(DEBUG, "port %u starting device", dev->data->port_id); 159 ret = mlx5_txq_start(dev); 160 if (ret) { 161 DRV_LOG(ERR, "port %u Tx queue allocation failed: %s", 162 dev->data->port_id, strerror(rte_errno)); 163 return -rte_errno; 164 } 165 ret = mlx5_rxq_start(dev); 166 if (ret) { 167 DRV_LOG(ERR, "port %u Rx queue allocation failed: %s", 168 dev->data->port_id, strerror(rte_errno)); 169 mlx5_txq_stop(dev); 170 return -rte_errno; 171 } 172 dev->data->dev_started = 1; 173 ret = mlx5_rx_intr_vec_enable(dev); 174 if (ret) { 175 DRV_LOG(ERR, "port %u Rx interrupt vector creation failed", 176 dev->data->port_id); 177 goto error; 178 } 179 mlx5_stats_init(dev); 180 ret = mlx5_traffic_enable(dev); 181 if (ret) { 182 DRV_LOG(DEBUG, "port %u failed to set defaults flows", 183 dev->data->port_id); 184 goto error; 185 } 186 ret = mlx5_flow_start(dev, &priv->flows); 187 if (ret) { 188 DRV_LOG(DEBUG, "port %u failed to set flows", 189 dev->data->port_id); 190 goto error; 191 } 192 rte_wmb(); 193 dev->tx_pkt_burst = mlx5_select_tx_function(dev); 194 dev->rx_pkt_burst = mlx5_select_rx_function(dev); 195 /* Enable datapath on secondary process. */ 196 mlx5_mp_req_start_rxtx(dev); 197 mlx5_dev_interrupt_handler_install(dev); 198 return 0; 199 error: 200 ret = rte_errno; /* Save rte_errno before cleanup. */ 201 /* Rollback. */ 202 dev->data->dev_started = 0; 203 mlx5_flow_stop(dev, &priv->flows); 204 mlx5_traffic_disable(dev); 205 mlx5_txq_stop(dev); 206 mlx5_rxq_stop(dev); 207 rte_errno = ret; /* Restore rte_errno. */ 208 return -rte_errno; 209 } 210 211 /** 212 * DPDK callback to stop the device. 213 * 214 * Simulate device stop by detaching all configured flows. 215 * 216 * @param dev 217 * Pointer to Ethernet device structure. 218 */ 219 void 220 mlx5_dev_stop(struct rte_eth_dev *dev) 221 { 222 struct mlx5_priv *priv = dev->data->dev_private; 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 /* Disable datapath on secondary process. */ 230 mlx5_mp_req_stop_rxtx(dev); 231 usleep(1000 * priv->rxqs_n); 232 DRV_LOG(DEBUG, "port %u stopping device", dev->data->port_id); 233 mlx5_flow_stop(dev, &priv->flows); 234 mlx5_traffic_disable(dev); 235 mlx5_rx_intr_vec_disable(dev); 236 mlx5_dev_interrupt_handler_uninstall(dev); 237 mlx5_txq_stop(dev); 238 mlx5_rxq_stop(dev); 239 } 240 241 /** 242 * Enable traffic flows configured by control plane 243 * 244 * @param dev 245 * Pointer to Ethernet device private data. 246 * @param dev 247 * Pointer to Ethernet device structure. 248 * 249 * @return 250 * 0 on success, a negative errno value otherwise and rte_errno is set. 251 */ 252 int 253 mlx5_traffic_enable(struct rte_eth_dev *dev) 254 { 255 struct mlx5_priv *priv = dev->data->dev_private; 256 struct rte_flow_item_eth bcast = { 257 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff", 258 }; 259 struct rte_flow_item_eth ipv6_multi_spec = { 260 .dst.addr_bytes = "\x33\x33\x00\x00\x00\x00", 261 }; 262 struct rte_flow_item_eth ipv6_multi_mask = { 263 .dst.addr_bytes = "\xff\xff\x00\x00\x00\x00", 264 }; 265 struct rte_flow_item_eth unicast = { 266 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00", 267 }; 268 struct rte_flow_item_eth unicast_mask = { 269 .dst.addr_bytes = "\xff\xff\xff\xff\xff\xff", 270 }; 271 const unsigned int vlan_filter_n = priv->vlan_filter_n; 272 const struct rte_ether_addr cmp = { 273 .addr_bytes = "\x00\x00\x00\x00\x00\x00", 274 }; 275 unsigned int i; 276 unsigned int j; 277 int ret; 278 279 if (priv->isolated) 280 return 0; 281 if (dev->data->promiscuous) { 282 struct rte_flow_item_eth promisc = { 283 .dst.addr_bytes = "\x00\x00\x00\x00\x00\x00", 284 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00", 285 .type = 0, 286 }; 287 288 ret = mlx5_ctrl_flow(dev, &promisc, &promisc); 289 if (ret) 290 goto error; 291 } 292 if (dev->data->all_multicast) { 293 struct rte_flow_item_eth multicast = { 294 .dst.addr_bytes = "\x01\x00\x00\x00\x00\x00", 295 .src.addr_bytes = "\x00\x00\x00\x00\x00\x00", 296 .type = 0, 297 }; 298 299 ret = mlx5_ctrl_flow(dev, &multicast, &multicast); 300 if (ret) 301 goto error; 302 } else { 303 /* Add broadcast/multicast flows. */ 304 for (i = 0; i != vlan_filter_n; ++i) { 305 uint16_t vlan = priv->vlan_filter[i]; 306 307 struct rte_flow_item_vlan vlan_spec = { 308 .tci = rte_cpu_to_be_16(vlan), 309 }; 310 struct rte_flow_item_vlan vlan_mask = 311 rte_flow_item_vlan_mask; 312 313 ret = mlx5_ctrl_flow_vlan(dev, &bcast, &bcast, 314 &vlan_spec, &vlan_mask); 315 if (ret) 316 goto error; 317 ret = mlx5_ctrl_flow_vlan(dev, &ipv6_multi_spec, 318 &ipv6_multi_mask, 319 &vlan_spec, &vlan_mask); 320 if (ret) 321 goto error; 322 } 323 if (!vlan_filter_n) { 324 ret = mlx5_ctrl_flow(dev, &bcast, &bcast); 325 if (ret) 326 goto error; 327 ret = mlx5_ctrl_flow(dev, &ipv6_multi_spec, 328 &ipv6_multi_mask); 329 if (ret) 330 goto error; 331 } 332 } 333 /* Add MAC address flows. */ 334 for (i = 0; i != MLX5_MAX_MAC_ADDRESSES; ++i) { 335 struct rte_ether_addr *mac = &dev->data->mac_addrs[i]; 336 337 if (!memcmp(mac, &cmp, sizeof(*mac))) 338 continue; 339 memcpy(&unicast.dst.addr_bytes, 340 mac->addr_bytes, 341 RTE_ETHER_ADDR_LEN); 342 for (j = 0; j != vlan_filter_n; ++j) { 343 uint16_t vlan = priv->vlan_filter[j]; 344 345 struct rte_flow_item_vlan vlan_spec = { 346 .tci = rte_cpu_to_be_16(vlan), 347 }; 348 struct rte_flow_item_vlan vlan_mask = 349 rte_flow_item_vlan_mask; 350 351 ret = mlx5_ctrl_flow_vlan(dev, &unicast, 352 &unicast_mask, 353 &vlan_spec, 354 &vlan_mask); 355 if (ret) 356 goto error; 357 } 358 if (!vlan_filter_n) { 359 ret = mlx5_ctrl_flow(dev, &unicast, &unicast_mask); 360 if (ret) 361 goto error; 362 } 363 } 364 return 0; 365 error: 366 ret = rte_errno; /* Save rte_errno before cleanup. */ 367 mlx5_flow_list_flush(dev, &priv->ctrl_flows); 368 rte_errno = ret; /* Restore rte_errno. */ 369 return -rte_errno; 370 } 371 372 373 /** 374 * Disable traffic flows configured by control plane 375 * 376 * @param dev 377 * Pointer to Ethernet device private data. 378 */ 379 void 380 mlx5_traffic_disable(struct rte_eth_dev *dev) 381 { 382 struct mlx5_priv *priv = dev->data->dev_private; 383 384 mlx5_flow_list_flush(dev, &priv->ctrl_flows); 385 } 386 387 /** 388 * Restart traffic flows configured by control plane 389 * 390 * @param dev 391 * Pointer to Ethernet device private data. 392 * 393 * @return 394 * 0 on success, a negative errno value otherwise and rte_errno is set. 395 */ 396 int 397 mlx5_traffic_restart(struct rte_eth_dev *dev) 398 { 399 if (dev->data->dev_started) { 400 mlx5_traffic_disable(dev); 401 return mlx5_traffic_enable(dev); 402 } 403 return 0; 404 } 405