1 /* 2 * Copyright (c) 2015-2016, Mellanox Technologies. All rights reserved. 3 * 4 * This software is available to you under a choice of one of two 5 * licenses. You may choose to be licensed under the terms of the GNU 6 * General Public License (GPL) Version 2, available from the file 7 * COPYING in the main directory of this source tree, or the 8 * OpenIB.org BSD license below: 9 * 10 * Redistribution and use in source and binary forms, with or 11 * without modification, are permitted provided that the following 12 * conditions are met: 13 * 14 * - Redistributions of source code must retain the above 15 * copyright notice, this list of conditions and the following 16 * disclaimer. 17 * 18 * - Redistributions in binary form must reproduce the above 19 * copyright notice, this list of conditions and the following 20 * disclaimer in the documentation and/or other materials 21 * provided with the distribution. 22 * 23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS 27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN 28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN 29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 30 * SOFTWARE. 31 */ 32 33 #include <linux/dim.h> 34 #include <net/tc_act/tc_gact.h> 35 #include <linux/mlx5/fs.h> 36 #include <net/vxlan.h> 37 #include <net/geneve.h> 38 #include <linux/bpf.h> 39 #include <linux/debugfs.h> 40 #include <linux/if_bridge.h> 41 #include <linux/filter.h> 42 #include <net/netdev_lock.h> 43 #include <net/netdev_queues.h> 44 #include <net/netdev_rx_queue.h> 45 #include <net/page_pool/types.h> 46 #include <net/pkt_sched.h> 47 #include <net/xdp_sock_drv.h> 48 #include "eswitch.h" 49 #include "en.h" 50 #include "en/dim.h" 51 #include "en/txrx.h" 52 #include "en_tc.h" 53 #include "en_rep.h" 54 #include "en_accel/ipsec.h" 55 #include "en_accel/psp.h" 56 #include "en_accel/macsec.h" 57 #include "en_accel/en_accel.h" 58 #include "en_accel/ktls.h" 59 #include "lib/vxlan.h" 60 #include "lib/clock.h" 61 #include "en/port.h" 62 #include "en/xdp.h" 63 #include "lib/eq.h" 64 #include "en/monitor_stats.h" 65 #include "en/health.h" 66 #include "en/params.h" 67 #include "en/xsk/pool.h" 68 #include "en/xsk/setup.h" 69 #include "en/xsk/rx.h" 70 #include "en/xsk/tx.h" 71 #include "en/hv_vhca_stats.h" 72 #include "en/devlink.h" 73 #include "lib/mlx5.h" 74 #include "en/ptp.h" 75 #include "en/htb.h" 76 #include "qos.h" 77 #include "en/trap.h" 78 #include "lib/devcom.h" 79 #include "lib/sd.h" 80 #include "en/pcie_cong_event.h" 81 82 static bool mlx5e_hw_gro_supported(struct mlx5_core_dev *mdev) 83 { 84 if (!MLX5_CAP_GEN(mdev, shampo) || 85 !MLX5_CAP_SHAMPO(mdev, shampo_header_split_data_merge)) 86 return false; 87 88 /* Our HW-GRO implementation relies on "KSM Mkey" for 89 * SHAMPO headers buffer mapping 90 */ 91 if (!MLX5_CAP_GEN(mdev, fixed_buffer_size)) 92 return false; 93 94 if (!MLX5_CAP_GEN_2(mdev, min_mkey_log_entity_size_fixed_buffer_valid)) 95 return false; 96 97 if (MLX5_CAP_GEN_2(mdev, min_mkey_log_entity_size_fixed_buffer) > 98 MLX5E_SHAMPO_LOG_HEADER_ENTRY_SIZE) 99 return false; 100 101 return true; 102 } 103 104 bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev, u8 page_shift, 105 enum mlx5e_mpwrq_umr_mode umr_mode) 106 { 107 u16 umr_wqebbs, max_wqebbs; 108 bool striding_rq_umr; 109 110 striding_rq_umr = MLX5_CAP_GEN(mdev, striding_rq) && MLX5_CAP_GEN(mdev, umr_ptr_rlky) && 111 MLX5_CAP_ETH(mdev, reg_umr_sq); 112 if (!striding_rq_umr) 113 return false; 114 115 umr_wqebbs = mlx5e_mpwrq_umr_wqebbs(mdev, page_shift, umr_mode); 116 max_wqebbs = mlx5e_get_max_sq_aligned_wqebbs(mdev); 117 /* Sanity check; should never happen, because mlx5e_mpwrq_umr_wqebbs is 118 * calculated from mlx5e_get_max_sq_aligned_wqebbs. 119 */ 120 if (WARN_ON(umr_wqebbs > max_wqebbs)) 121 return false; 122 123 return true; 124 } 125 126 void mlx5e_update_carrier(struct mlx5e_priv *priv) 127 { 128 struct mlx5_core_dev *mdev = priv->mdev; 129 u8 port_state; 130 bool up; 131 132 port_state = mlx5_query_vport_state(mdev, 133 MLX5_VPORT_STATE_OP_MOD_VNIC_VPORT, 134 0); 135 136 up = port_state == VPORT_STATE_UP; 137 if (up == netif_carrier_ok(priv->netdev)) 138 netif_carrier_event(priv->netdev); 139 if (up) { 140 netdev_info(priv->netdev, "Link up\n"); 141 netif_carrier_on(priv->netdev); 142 } else { 143 netdev_info(priv->netdev, "Link down\n"); 144 netif_carrier_off(priv->netdev); 145 } 146 } 147 148 static void mlx5e_update_carrier_work(struct work_struct *work) 149 { 150 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv, 151 update_carrier_work); 152 153 mutex_lock(&priv->state_lock); 154 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) 155 if (priv->profile->update_carrier) 156 priv->profile->update_carrier(priv); 157 mutex_unlock(&priv->state_lock); 158 } 159 160 static void mlx5e_update_stats_work(struct work_struct *work) 161 { 162 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv, 163 update_stats_work); 164 165 mutex_lock(&priv->state_lock); 166 priv->profile->update_stats(priv); 167 mutex_unlock(&priv->state_lock); 168 } 169 170 void mlx5e_queue_update_stats(struct mlx5e_priv *priv) 171 { 172 if (!priv->profile->update_stats) 173 return; 174 175 if (unlikely(test_bit(MLX5E_STATE_DESTROYING, &priv->state))) 176 return; 177 178 queue_work(priv->wq, &priv->update_stats_work); 179 } 180 181 static int async_event(struct notifier_block *nb, unsigned long event, void *data) 182 { 183 struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, events_nb); 184 struct mlx5_eqe *eqe = data; 185 186 if (event != MLX5_EVENT_TYPE_PORT_CHANGE) 187 return NOTIFY_DONE; 188 189 switch (eqe->sub_type) { 190 case MLX5_PORT_CHANGE_SUBTYPE_DOWN: 191 case MLX5_PORT_CHANGE_SUBTYPE_ACTIVE: 192 queue_work(priv->wq, &priv->update_carrier_work); 193 break; 194 default: 195 return NOTIFY_DONE; 196 } 197 198 return NOTIFY_OK; 199 } 200 201 static void mlx5e_enable_async_events(struct mlx5e_priv *priv) 202 { 203 priv->events_nb.notifier_call = async_event; 204 mlx5_notifier_register(priv->mdev, &priv->events_nb); 205 } 206 207 static void mlx5e_disable_async_events(struct mlx5e_priv *priv) 208 { 209 mlx5_notifier_unregister(priv->mdev, &priv->events_nb); 210 } 211 212 static int mlx5e_devcom_event_mpv(int event, void *my_data, void *event_data) 213 { 214 struct mlx5e_priv *master_priv = event_data; 215 216 switch (event) { 217 case MPV_DEVCOM_MASTER_UP: 218 mlx5_devcom_comp_set_ready(master_priv->devcom, true); 219 break; 220 case MPV_DEVCOM_MASTER_DOWN: 221 /* no need for comp set ready false since we unregister after 222 * and it hurts cleanup flow. 223 */ 224 break; 225 case MPV_DEVCOM_IPSEC_MASTER_UP: 226 case MPV_DEVCOM_IPSEC_MASTER_DOWN: 227 mlx5e_ipsec_handle_mpv_event(event, my_data, event_data); 228 break; 229 } 230 231 return 0; 232 } 233 234 static int mlx5e_devcom_init_mpv(struct mlx5e_priv *priv, u64 *data) 235 { 236 struct mlx5_devcom_match_attr attr = { 237 .key.val = *data, 238 }; 239 240 priv->devcom = mlx5_devcom_register_component(priv->mdev->priv.devc, 241 MLX5_DEVCOM_MPV, 242 &attr, 243 mlx5e_devcom_event_mpv, 244 priv); 245 if (!priv->devcom) 246 return -EINVAL; 247 248 if (mlx5_core_is_mp_master(priv->mdev)) { 249 mlx5_devcom_send_event(priv->devcom, MPV_DEVCOM_MASTER_UP, 250 MPV_DEVCOM_MASTER_UP, priv); 251 mlx5e_ipsec_send_event(priv, MPV_DEVCOM_IPSEC_MASTER_UP); 252 } 253 254 return 0; 255 } 256 257 static void mlx5e_devcom_cleanup_mpv(struct mlx5e_priv *priv) 258 { 259 if (!priv->devcom) 260 return; 261 262 if (mlx5_core_is_mp_master(priv->mdev)) { 263 mlx5_devcom_send_event(priv->devcom, MPV_DEVCOM_MASTER_DOWN, 264 MPV_DEVCOM_MASTER_DOWN, priv); 265 mlx5e_ipsec_send_event(priv, MPV_DEVCOM_IPSEC_MASTER_DOWN); 266 } 267 268 mlx5_devcom_unregister_component(priv->devcom); 269 priv->devcom = NULL; 270 } 271 272 static int blocking_event(struct notifier_block *nb, unsigned long event, void *data) 273 { 274 struct mlx5e_priv *priv = container_of(nb, struct mlx5e_priv, blocking_events_nb); 275 struct mlx5_devlink_trap_event_ctx *trap_event_ctx = data; 276 int err; 277 278 switch (event) { 279 case MLX5_DRIVER_EVENT_TYPE_TRAP: 280 err = mlx5e_handle_trap_event(priv, trap_event_ctx->trap); 281 if (err) { 282 trap_event_ctx->err = err; 283 return NOTIFY_BAD; 284 } 285 break; 286 case MLX5_DRIVER_EVENT_AFFILIATION_DONE: 287 if (mlx5e_devcom_init_mpv(priv, data)) 288 return NOTIFY_BAD; 289 break; 290 case MLX5_DRIVER_EVENT_AFFILIATION_REMOVED: 291 mlx5e_devcom_cleanup_mpv(priv); 292 break; 293 default: 294 return NOTIFY_DONE; 295 } 296 return NOTIFY_OK; 297 } 298 299 static void mlx5e_enable_blocking_events(struct mlx5e_priv *priv) 300 { 301 priv->blocking_events_nb.notifier_call = blocking_event; 302 mlx5_blocking_notifier_register(priv->mdev, &priv->blocking_events_nb); 303 } 304 305 static void mlx5e_disable_blocking_events(struct mlx5e_priv *priv) 306 { 307 mlx5_blocking_notifier_unregister(priv->mdev, &priv->blocking_events_nb); 308 } 309 310 static u16 mlx5e_mpwrq_umr_octowords(u32 entries, enum mlx5e_mpwrq_umr_mode umr_mode) 311 { 312 u8 umr_entry_size = mlx5e_mpwrq_umr_entry_size(umr_mode); 313 u32 sz; 314 315 sz = ALIGN(entries * umr_entry_size, MLX5_UMR_FLEX_ALIGNMENT); 316 317 return sz / MLX5_OCTWORD; 318 } 319 320 static inline void mlx5e_build_umr_wqe(struct mlx5e_rq *rq, 321 struct mlx5e_icosq *sq, 322 struct mlx5e_umr_wqe *wqe) 323 { 324 struct mlx5_wqe_ctrl_seg *cseg = &wqe->hdr.ctrl; 325 struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->hdr.uctrl; 326 u16 octowords; 327 u8 ds_cnt; 328 329 ds_cnt = DIV_ROUND_UP(mlx5e_mpwrq_umr_wqe_sz(rq->mdev, rq->mpwqe.page_shift, 330 rq->mpwqe.umr_mode), 331 MLX5_SEND_WQE_DS); 332 333 cseg->qpn_ds = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) | 334 ds_cnt); 335 cseg->umr_mkey = rq->mpwqe.umr_mkey_be; 336 337 ucseg->flags = MLX5_UMR_TRANSLATION_OFFSET_EN | MLX5_UMR_INLINE; 338 octowords = mlx5e_mpwrq_umr_octowords(rq->mpwqe.pages_per_wqe, rq->mpwqe.umr_mode); 339 ucseg->xlt_octowords = cpu_to_be16(octowords); 340 ucseg->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE); 341 } 342 343 static int mlx5e_rq_alloc_mpwqe_info(struct mlx5e_rq *rq, int node) 344 { 345 int wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq); 346 size_t alloc_size; 347 348 alloc_size = array_size(wq_sz, struct_size(rq->mpwqe.info, 349 alloc_units.frag_pages, 350 rq->mpwqe.pages_per_wqe)); 351 352 rq->mpwqe.info = kvzalloc_node(alloc_size, GFP_KERNEL, node); 353 if (!rq->mpwqe.info) 354 return -ENOMEM; 355 356 /* For deferred page release (release right before alloc), make sure 357 * that on first round release is not called. 358 */ 359 for (int i = 0; i < wq_sz; i++) { 360 struct mlx5e_mpw_info *wi = mlx5e_get_mpw_info(rq, i); 361 362 bitmap_fill(wi->skip_release_bitmap, rq->mpwqe.pages_per_wqe); 363 } 364 365 mlx5e_build_umr_wqe(rq, rq->icosq, 366 container_of(&rq->mpwqe.umr_wqe, 367 struct mlx5e_umr_wqe, hdr)); 368 369 return 0; 370 } 371 372 static int mlx5e_rq_alloc_mpwqe_linear_info(struct mlx5e_rq *rq, int node, 373 struct mlx5e_params *params, 374 struct mlx5e_rq_opt_param *rqo) 375 { 376 struct mlx5_core_dev *mdev = rq->mdev; 377 struct mlx5e_mpw_linear_info *li; 378 u32 linear_frag_count; 379 380 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, params, rqo) || 381 !params->xdp_prog) 382 return 0; 383 384 li = kvzalloc_node(sizeof(*li), GFP_KERNEL, node); 385 if (!li) 386 return -ENOMEM; 387 388 linear_frag_count = 389 BIT(rq->mpwqe.page_shift - MLX5E_XDP_LOG_MAX_LINEAR_SZ); 390 if (linear_frag_count > U16_MAX) { 391 netdev_warn(rq->netdev, 392 "rq %d: linear_frag_count (%u) larger than expected (%u), page_shift: %u, log_max_linear_sz: %u\n", 393 rq->ix, linear_frag_count, U16_MAX, 394 rq->mpwqe.page_shift, MLX5E_XDP_LOG_MAX_LINEAR_SZ); 395 kvfree(li); 396 return -EINVAL; 397 } 398 399 li->max_frags = linear_frag_count; 400 rq->mpwqe.linear_info = li; 401 402 /* Set to max to force allocation on first run. */ 403 li->frag_page.frags = li->max_frags; 404 405 return 0; 406 } 407 408 static u8 mlx5e_mpwrq_access_mode(enum mlx5e_mpwrq_umr_mode umr_mode) 409 { 410 switch (umr_mode) { 411 case MLX5E_MPWRQ_UMR_MODE_ALIGNED: 412 return MLX5_MKC_ACCESS_MODE_MTT; 413 case MLX5E_MPWRQ_UMR_MODE_UNALIGNED: 414 return MLX5_MKC_ACCESS_MODE_KSM; 415 case MLX5E_MPWRQ_UMR_MODE_OVERSIZED: 416 return MLX5_MKC_ACCESS_MODE_KLMS; 417 case MLX5E_MPWRQ_UMR_MODE_TRIPLE: 418 return MLX5_MKC_ACCESS_MODE_KSM; 419 } 420 WARN_ONCE(1, "MPWRQ UMR mode %d is not known\n", umr_mode); 421 return 0; 422 } 423 424 static int mlx5e_create_umr_mkey(struct mlx5_core_dev *mdev, 425 u32 npages, u8 page_shift, u32 *umr_mkey, 426 dma_addr_t filler_addr, 427 enum mlx5e_mpwrq_umr_mode umr_mode, 428 u32 xsk_chunk_size) 429 { 430 struct mlx5_mtt *mtt; 431 struct mlx5_ksm *ksm; 432 struct mlx5_klm *klm; 433 u32 octwords; 434 int inlen; 435 void *mkc; 436 u32 *in; 437 int err; 438 int i; 439 440 if ((umr_mode == MLX5E_MPWRQ_UMR_MODE_UNALIGNED || 441 umr_mode == MLX5E_MPWRQ_UMR_MODE_TRIPLE) && 442 !MLX5_CAP_GEN(mdev, fixed_buffer_size)) { 443 mlx5_core_warn(mdev, "Unaligned AF_XDP requires fixed_buffer_size capability\n"); 444 return -EINVAL; 445 } 446 447 octwords = mlx5e_mpwrq_umr_octowords(npages, umr_mode); 448 449 inlen = MLX5_FLEXIBLE_INLEN(mdev, MLX5_ST_SZ_BYTES(create_mkey_in), 450 MLX5_OCTWORD, octwords); 451 if (inlen < 0) 452 return inlen; 453 454 in = kvzalloc(inlen, GFP_KERNEL); 455 if (!in) 456 return -ENOMEM; 457 458 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry); 459 460 MLX5_SET(mkc, mkc, free, 1); 461 MLX5_SET(mkc, mkc, umr_en, 1); 462 MLX5_SET(mkc, mkc, lw, 1); 463 MLX5_SET(mkc, mkc, lr, 1); 464 MLX5_SET(mkc, mkc, access_mode_1_0, mlx5e_mpwrq_access_mode(umr_mode)); 465 mlx5e_mkey_set_relaxed_ordering(mdev, mkc); 466 MLX5_SET(mkc, mkc, qpn, 0xffffff); 467 MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.hw_objs.pdn); 468 MLX5_SET64(mkc, mkc, len, npages << page_shift); 469 MLX5_SET(mkc, mkc, translations_octword_size, octwords); 470 if (umr_mode == MLX5E_MPWRQ_UMR_MODE_TRIPLE) 471 MLX5_SET(mkc, mkc, log_page_size, page_shift - 2); 472 else if (umr_mode != MLX5E_MPWRQ_UMR_MODE_OVERSIZED) 473 MLX5_SET(mkc, mkc, log_page_size, page_shift); 474 MLX5_SET(create_mkey_in, in, translations_octword_actual_size, octwords); 475 476 /* Initialize the mkey with all MTTs pointing to a default 477 * page (filler_addr). When the channels are activated, UMR 478 * WQEs will redirect the RX WQEs to the actual memory from 479 * the RQ's pool, while the gaps (wqe_overflow) remain mapped 480 * to the default page. 481 */ 482 switch (umr_mode) { 483 case MLX5E_MPWRQ_UMR_MODE_OVERSIZED: 484 klm = MLX5_ADDR_OF(create_mkey_in, in, klm_pas_mtt); 485 for (i = 0; i < npages; i++) { 486 klm[i << 1] = (struct mlx5_klm) { 487 .va = cpu_to_be64(filler_addr), 488 .bcount = cpu_to_be32(xsk_chunk_size), 489 .key = cpu_to_be32(mdev->mlx5e_res.hw_objs.mkey), 490 }; 491 klm[(i << 1) + 1] = (struct mlx5_klm) { 492 .va = cpu_to_be64(filler_addr), 493 .bcount = cpu_to_be32((1 << page_shift) - xsk_chunk_size), 494 .key = cpu_to_be32(mdev->mlx5e_res.hw_objs.mkey), 495 }; 496 } 497 break; 498 case MLX5E_MPWRQ_UMR_MODE_UNALIGNED: 499 ksm = MLX5_ADDR_OF(create_mkey_in, in, klm_pas_mtt); 500 for (i = 0; i < npages; i++) 501 ksm[i] = (struct mlx5_ksm) { 502 .key = cpu_to_be32(mdev->mlx5e_res.hw_objs.mkey), 503 .va = cpu_to_be64(filler_addr), 504 }; 505 break; 506 case MLX5E_MPWRQ_UMR_MODE_ALIGNED: 507 mtt = MLX5_ADDR_OF(create_mkey_in, in, klm_pas_mtt); 508 for (i = 0; i < npages; i++) 509 mtt[i] = (struct mlx5_mtt) { 510 .ptag = cpu_to_be64(filler_addr), 511 }; 512 break; 513 case MLX5E_MPWRQ_UMR_MODE_TRIPLE: 514 ksm = MLX5_ADDR_OF(create_mkey_in, in, klm_pas_mtt); 515 for (i = 0; i < npages * 4; i++) { 516 ksm[i] = (struct mlx5_ksm) { 517 .key = cpu_to_be32(mdev->mlx5e_res.hw_objs.mkey), 518 .va = cpu_to_be64(filler_addr), 519 }; 520 } 521 break; 522 } 523 524 err = mlx5_core_create_mkey(mdev, umr_mkey, in, inlen); 525 526 kvfree(in); 527 return err; 528 } 529 530 static int mlx5e_create_rq_umr_mkey(struct mlx5_core_dev *mdev, struct mlx5e_rq *rq) 531 { 532 u32 xsk_chunk_size = rq->xsk_pool ? rq->xsk_pool->chunk_size : 0; 533 u32 wq_size = mlx5_wq_ll_get_size(&rq->mpwqe.wq); 534 u32 num_entries, max_num_entries; 535 u32 umr_mkey; 536 int err; 537 538 max_num_entries = mlx5e_mpwrq_max_num_entries(mdev, rq->mpwqe.umr_mode); 539 540 /* Shouldn't overflow, the result is at most MLX5E_MAX_RQ_NUM_MTTS. */ 541 if (WARN_ON_ONCE(check_mul_overflow(wq_size, (u32)rq->mpwqe.mtts_per_wqe, 542 &num_entries) || 543 num_entries > max_num_entries)) 544 mlx5_core_err(mdev, "%s: multiplication overflow: %u * %u > %u\n", 545 __func__, wq_size, rq->mpwqe.mtts_per_wqe, 546 max_num_entries); 547 548 err = mlx5e_create_umr_mkey(mdev, num_entries, rq->mpwqe.page_shift, 549 &umr_mkey, rq->wqe_overflow.addr, 550 rq->mpwqe.umr_mode, xsk_chunk_size); 551 rq->mpwqe.umr_mkey_be = cpu_to_be32(umr_mkey); 552 return err; 553 } 554 555 static void mlx5e_init_frags_partition(struct mlx5e_rq *rq) 556 { 557 struct mlx5e_wqe_frag_info next_frag = {}; 558 struct mlx5e_wqe_frag_info *prev = NULL; 559 int i; 560 561 WARN_ON(rq->xsk_pool); 562 563 next_frag.frag_page = &rq->wqe.alloc_units->frag_pages[0]; 564 565 /* Skip first release due to deferred release. */ 566 next_frag.flags = BIT(MLX5E_WQE_FRAG_SKIP_RELEASE); 567 568 for (i = 0; i < mlx5_wq_cyc_get_size(&rq->wqe.wq); i++) { 569 struct mlx5e_rq_frag_info *frag_info = &rq->wqe.info.arr[0]; 570 struct mlx5e_wqe_frag_info *frag = 571 &rq->wqe.frags[i << rq->wqe.info.log_num_frags]; 572 int f; 573 574 for (f = 0; f < rq->wqe.info.num_frags; f++, frag++) { 575 if (next_frag.offset + frag_info[f].frag_stride > PAGE_SIZE) { 576 /* Pages are assigned at runtime. */ 577 next_frag.frag_page++; 578 next_frag.offset = 0; 579 if (prev) 580 prev->flags |= BIT(MLX5E_WQE_FRAG_LAST_IN_PAGE); 581 } 582 *frag = next_frag; 583 584 /* prepare next */ 585 next_frag.offset += frag_info[f].frag_stride; 586 prev = frag; 587 } 588 } 589 590 if (prev) 591 prev->flags |= BIT(MLX5E_WQE_FRAG_LAST_IN_PAGE); 592 } 593 594 static void mlx5e_init_xsk_buffs(struct mlx5e_rq *rq) 595 { 596 int i; 597 598 /* Assumptions used by XSK batched allocator. */ 599 WARN_ON(rq->wqe.info.num_frags != 1); 600 WARN_ON(rq->wqe.info.log_num_frags != 0); 601 WARN_ON(rq->wqe.info.arr[0].frag_stride != PAGE_SIZE); 602 603 /* Considering the above assumptions a fragment maps to a single 604 * xsk_buff. 605 */ 606 for (i = 0; i < mlx5_wq_cyc_get_size(&rq->wqe.wq); i++) { 607 rq->wqe.frags[i].xskp = &rq->wqe.alloc_units->xsk_buffs[i]; 608 609 /* Skip first release due to deferred release as WQES are 610 * not allocated yet. 611 */ 612 rq->wqe.frags[i].flags |= BIT(MLX5E_WQE_FRAG_SKIP_RELEASE); 613 } 614 } 615 616 static int mlx5e_init_wqe_alloc_info(struct mlx5e_rq *rq, int node) 617 { 618 int wq_sz = mlx5_wq_cyc_get_size(&rq->wqe.wq); 619 int len = wq_sz << rq->wqe.info.log_num_frags; 620 struct mlx5e_wqe_frag_info *frags; 621 union mlx5e_alloc_units *aus; 622 int aus_sz; 623 624 if (rq->xsk_pool) 625 aus_sz = sizeof(*aus->xsk_buffs); 626 else 627 aus_sz = sizeof(*aus->frag_pages); 628 629 aus = kvzalloc_node(array_size(len, aus_sz), GFP_KERNEL, node); 630 if (!aus) 631 return -ENOMEM; 632 633 frags = kvzalloc_node(array_size(len, sizeof(*frags)), GFP_KERNEL, node); 634 if (!frags) { 635 kvfree(aus); 636 return -ENOMEM; 637 } 638 639 rq->wqe.alloc_units = aus; 640 rq->wqe.frags = frags; 641 642 if (rq->xsk_pool) 643 mlx5e_init_xsk_buffs(rq); 644 else 645 mlx5e_init_frags_partition(rq); 646 647 return 0; 648 } 649 650 static void mlx5e_free_wqe_alloc_info(struct mlx5e_rq *rq) 651 { 652 kvfree(rq->wqe.frags); 653 kvfree(rq->wqe.alloc_units); 654 } 655 656 static void mlx5e_rq_err_cqe_work(struct work_struct *recover_work) 657 { 658 struct mlx5e_rq *rq = container_of(recover_work, struct mlx5e_rq, recover_work); 659 660 mlx5e_reporter_rq_cqe_err(rq); 661 } 662 663 static void mlx5e_rq_timeout_work(struct work_struct *timeout_work) 664 { 665 struct mlx5e_rq *rq = container_of(timeout_work, 666 struct mlx5e_rq, 667 rx_timeout_work); 668 669 mlx5e_reporter_rx_timeout(rq); 670 } 671 672 static int mlx5e_alloc_mpwqe_rq_drop_page(struct mlx5e_rq *rq) 673 { 674 /* xsk can have page_shift < PAGE_SHIFT */ 675 u16 page_order = max_t(s16, rq->mpwqe.page_shift - PAGE_SHIFT, 0); 676 u32 page_size = BIT(PAGE_SHIFT + page_order); 677 678 rq->wqe_overflow.page = alloc_pages(GFP_KERNEL, page_order); 679 if (!rq->wqe_overflow.page) 680 return -ENOMEM; 681 682 rq->wqe_overflow.addr = dma_map_page(rq->pdev, rq->wqe_overflow.page, 0, 683 page_size, rq->buff.map_dir); 684 if (dma_mapping_error(rq->pdev, rq->wqe_overflow.addr)) { 685 __free_pages(rq->wqe_overflow.page, page_order); 686 return -ENOMEM; 687 } 688 return 0; 689 } 690 691 static void mlx5e_free_mpwqe_rq_drop_page(struct mlx5e_rq *rq) 692 { 693 u16 page_order = max_t(s16, rq->mpwqe.page_shift - PAGE_SHIFT, 0); 694 u32 page_size = BIT(PAGE_SHIFT + page_order); 695 696 dma_unmap_page(rq->pdev, rq->wqe_overflow.addr, page_size, 697 rq->buff.map_dir); 698 __free_pages(rq->wqe_overflow.page, page_order); 699 } 700 701 static int mlx5e_init_rxq_rq(struct mlx5e_channel *c, struct mlx5e_params *params, 702 u32 xdp_frag_size, struct mlx5e_rq *rq) 703 { 704 struct mlx5_core_dev *mdev = c->mdev; 705 int err; 706 707 rq->wq_type = params->rq_wq_type; 708 rq->pdev = c->pdev; 709 rq->netdev = c->netdev; 710 rq->priv = c->priv; 711 rq->hwtstamp_config = &c->priv->hwtstamp_config; 712 rq->clock = mdev->clock; 713 rq->icosq = &c->icosq; 714 rq->ix = c->ix; 715 rq->channel = c; 716 rq->mdev = mdev; 717 rq->hw_mtu = 718 MLX5E_SW2HW_MTU(params, params->sw_mtu) - ETH_FCS_LEN * !params->scatter_fcs_en; 719 rq->xdpsq = &c->rq_xdpsq; 720 rq->stats = &c->priv->channel_stats[c->ix]->rq; 721 rq->ptp_cyc2time = mlx5_rq_ts_translator(mdev); 722 err = mlx5e_rq_set_handlers(rq, params, NULL); 723 if (err) 724 return err; 725 726 return __xdp_rxq_info_reg(&rq->xdp_rxq, rq->netdev, rq->ix, c->napi.napi_id, 727 xdp_frag_size); 728 } 729 730 static void mlx5e_release_rq_hd_pages(struct mlx5e_rq *rq, 731 struct mlx5e_shampo_hd *shampo) 732 733 { 734 for (int i = 0; i < shampo->nentries; i++) { 735 struct mlx5e_dma_info *info = &shampo->hd_buf_pages[i]; 736 737 if (!info->page) 738 continue; 739 740 dma_unmap_page(rq->pdev, info->addr, PAGE_SIZE, 741 rq->buff.map_dir); 742 __free_page(info->page); 743 } 744 } 745 746 static int mlx5e_alloc_rq_hd_pages(struct mlx5e_rq *rq, int node, 747 struct mlx5e_shampo_hd *shampo) 748 { 749 int err, i; 750 751 for (i = 0; i < shampo->nentries; i++) { 752 struct page *page = alloc_pages_node(node, GFP_KERNEL, 0); 753 dma_addr_t addr; 754 755 if (!page) { 756 err = -ENOMEM; 757 goto err_free_pages; 758 } 759 760 addr = dma_map_page(rq->pdev, page, 0, PAGE_SIZE, 761 rq->buff.map_dir); 762 err = dma_mapping_error(rq->pdev, addr); 763 if (err) { 764 __free_page(page); 765 goto err_free_pages; 766 } 767 768 shampo->hd_buf_pages[i].page = page; 769 shampo->hd_buf_pages[i].addr = addr; 770 } 771 772 return 0; 773 774 err_free_pages: 775 mlx5e_release_rq_hd_pages(rq, shampo); 776 777 return err; 778 } 779 780 static int mlx5e_create_rq_hd_mkey(struct mlx5_core_dev *mdev, 781 struct mlx5e_shampo_hd *shampo) 782 { 783 enum mlx5e_mpwrq_umr_mode umr_mode = MLX5E_MPWRQ_UMR_MODE_ALIGNED; 784 struct mlx5_mtt *mtt; 785 void *mkc, *in; 786 int inlen, err; 787 u32 octwords; 788 789 octwords = mlx5e_mpwrq_umr_octowords(shampo->nentries, umr_mode); 790 inlen = MLX5_FLEXIBLE_INLEN(mdev, MLX5_ST_SZ_BYTES(create_mkey_in), 791 MLX5_OCTWORD, octwords); 792 if (inlen < 0) 793 return inlen; 794 795 in = kvzalloc(inlen, GFP_KERNEL); 796 if (!in) 797 return -ENOMEM; 798 799 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry); 800 801 MLX5_SET(mkc, mkc, lw, 1); 802 MLX5_SET(mkc, mkc, lr, 1); 803 MLX5_SET(mkc, mkc, access_mode_1_0, MLX5_MKC_ACCESS_MODE_MTT); 804 mlx5e_mkey_set_relaxed_ordering(mdev, mkc); 805 MLX5_SET(mkc, mkc, qpn, 0xffffff); 806 MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.hw_objs.pdn); 807 MLX5_SET64(mkc, mkc, len, shampo->hd_buf_size); 808 MLX5_SET(mkc, mkc, log_page_size, PAGE_SHIFT); 809 MLX5_SET(mkc, mkc, translations_octword_size, octwords); 810 MLX5_SET(create_mkey_in, in, translations_octword_actual_size, 811 octwords); 812 813 mtt = MLX5_ADDR_OF(create_mkey_in, in, klm_pas_mtt); 814 for (int i = 0; i < shampo->nentries; i++) 815 mtt[i].ptag = cpu_to_be64(shampo->hd_buf_pages[i].addr); 816 817 err = mlx5_core_create_mkey(mdev, &shampo->mkey, in, inlen); 818 819 kvfree(in); 820 return err; 821 } 822 823 static int mlx5_rq_shampo_alloc(struct mlx5_core_dev *mdev, 824 struct mlx5e_params *params, 825 struct mlx5e_rq_param *rq_param, 826 struct mlx5e_rq *rq, 827 int node) 828 { 829 struct mlx5e_shampo_hd *shampo; 830 int nentries, err, shampo_sz; 831 u32 hd_per_wq, hd_buf_size; 832 833 if (!test_bit(MLX5E_RQ_STATE_SHAMPO, &rq->state)) 834 return 0; 835 836 hd_per_wq = mlx5e_shampo_hd_per_wq(mdev, params, rq_param); 837 hd_buf_size = hd_per_wq * BIT(MLX5E_SHAMPO_LOG_HEADER_ENTRY_SIZE); 838 nentries = hd_buf_size / PAGE_SIZE; 839 if (!nentries) { 840 mlx5_core_err(mdev, "SHAMPO header buffer size %u < %lu\n", 841 hd_buf_size, PAGE_SIZE); 842 return -EINVAL; 843 } 844 845 shampo_sz = struct_size(shampo, hd_buf_pages, nentries); 846 shampo = kvzalloc_node(shampo_sz, GFP_KERNEL, node); 847 if (!shampo) 848 return -ENOMEM; 849 850 shampo->hd_per_wq = hd_per_wq; 851 shampo->hd_buf_size = hd_buf_size; 852 shampo->nentries = nentries; 853 err = mlx5e_alloc_rq_hd_pages(rq, node, shampo); 854 if (err) 855 goto err_free; 856 857 err = mlx5e_create_rq_hd_mkey(mdev, shampo); 858 if (err) 859 goto err_release_pages; 860 861 /* gro only data structures */ 862 rq->hw_gro_data = kvzalloc_node(sizeof(*rq->hw_gro_data), GFP_KERNEL, node); 863 if (!rq->hw_gro_data) { 864 err = -ENOMEM; 865 goto err_destroy_mkey; 866 } 867 868 rq->mpwqe.shampo = shampo; 869 870 return 0; 871 872 err_destroy_mkey: 873 mlx5_core_destroy_mkey(mdev, shampo->mkey); 874 err_release_pages: 875 mlx5e_release_rq_hd_pages(rq, shampo); 876 err_free: 877 kvfree(shampo); 878 879 return err; 880 } 881 882 static void mlx5e_rq_free_shampo(struct mlx5e_rq *rq) 883 { 884 struct mlx5e_shampo_hd *shampo = rq->mpwqe.shampo; 885 886 if (!shampo) 887 return; 888 889 kvfree(rq->hw_gro_data); 890 mlx5_core_destroy_mkey(rq->mdev, shampo->mkey); 891 mlx5e_release_rq_hd_pages(rq, shampo); 892 kvfree(shampo); 893 } 894 895 static int mlx5e_alloc_rq(struct mlx5e_params *params, 896 struct mlx5e_rq_param *rq_param, 897 struct mlx5e_rq_opt_param *rqo, 898 int node, struct mlx5e_rq *rq) 899 { 900 void *rqc_wq = MLX5_ADDR_OF(rqc, rq_param->rqc, wq); 901 struct mlx5_core_dev *mdev = rq->mdev; 902 u32 pool_order = 0; 903 u32 pool_size; 904 int wq_sz; 905 int err; 906 int i; 907 908 rq_param->wq.db_numa_node = node; 909 INIT_WORK(&rq->recover_work, mlx5e_rq_err_cqe_work); 910 INIT_WORK(&rq->rx_timeout_work, mlx5e_rq_timeout_work); 911 912 if (params->xdp_prog) 913 bpf_prog_inc(params->xdp_prog); 914 RCU_INIT_POINTER(rq->xdp_prog, params->xdp_prog); 915 916 rq->buff.map_dir = params->xdp_prog ? DMA_BIDIRECTIONAL : DMA_FROM_DEVICE; 917 rq->buff.headroom = mlx5e_get_rq_headroom(mdev, params, rqo); 918 pool_size = 1 << params->log_rq_mtu_frames; 919 920 rq->mkey_be = cpu_to_be32(mdev->mlx5e_res.hw_objs.mkey); 921 922 switch (rq->wq_type) { 923 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ: 924 err = mlx5_wq_ll_create(mdev, &rq_param->wq, rqc_wq, 925 &rq->mpwqe.wq, &rq->wq_ctrl); 926 if (err) 927 goto err_rq_xdp_prog; 928 929 rq->mpwqe.wq.db = &rq->mpwqe.wq.db[MLX5_RCV_DBR]; 930 931 wq_sz = mlx5_wq_ll_get_size(&rq->mpwqe.wq); 932 933 rq->mpwqe.page_shift = mlx5e_mpwrq_page_shift(mdev, rqo); 934 err = mlx5e_alloc_mpwqe_rq_drop_page(rq); 935 if (err) 936 goto err_rq_wq_destroy; 937 938 rq->mpwqe.umr_mode = mlx5e_mpwrq_umr_mode(mdev, rqo); 939 rq->mpwqe.pages_per_wqe = 940 mlx5e_mpwrq_pages_per_wqe(mdev, rq->mpwqe.page_shift, 941 rq->mpwqe.umr_mode); 942 rq->mpwqe.umr_wqebbs = 943 mlx5e_mpwrq_umr_wqebbs(mdev, rq->mpwqe.page_shift, 944 rq->mpwqe.umr_mode); 945 rq->mpwqe.mtts_per_wqe = 946 mlx5e_mpwrq_mtts_per_wqe(mdev, rq->mpwqe.page_shift, 947 rq->mpwqe.umr_mode); 948 949 pool_size = rq->mpwqe.pages_per_wqe << 950 mlx5e_mpwqe_get_log_rq_size(mdev, params, rqo); 951 pool_order = rq->mpwqe.page_shift - PAGE_SHIFT; 952 953 rq->mpwqe.log_stride_sz = 954 mlx5e_mpwqe_get_log_stride_size(mdev, params, 955 rqo); 956 rq->mpwqe.num_strides = 957 BIT(mlx5e_mpwqe_get_log_num_strides(mdev, params, rqo)); 958 rq->mpwqe.min_wqe_bulk = mlx5e_mpwqe_get_min_wqe_bulk(wq_sz); 959 960 rq->buff.frame0_sz = (1 << rq->mpwqe.log_stride_sz); 961 962 err = mlx5e_create_rq_umr_mkey(mdev, rq); 963 if (err) 964 goto err_rq_drop_page; 965 966 err = mlx5e_rq_alloc_mpwqe_info(rq, node); 967 if (err) 968 goto err_rq_mkey; 969 970 err = mlx5e_rq_alloc_mpwqe_linear_info(rq, node, params, rqo); 971 if (err) 972 goto err_free_mpwqe_info; 973 974 err = mlx5_rq_shampo_alloc(mdev, params, rq_param, rq, node); 975 if (err) 976 goto err_free_mpwqe_linear_info; 977 978 break; 979 default: /* MLX5_WQ_TYPE_CYCLIC */ 980 err = mlx5_wq_cyc_create(mdev, &rq_param->wq, rqc_wq, 981 &rq->wqe.wq, &rq->wq_ctrl); 982 if (err) 983 goto err_rq_xdp_prog; 984 985 rq->wqe.wq.db = &rq->wqe.wq.db[MLX5_RCV_DBR]; 986 987 wq_sz = mlx5_wq_cyc_get_size(&rq->wqe.wq); 988 989 rq->wqe.info = rq_param->frags_info; 990 rq->buff.frame0_sz = rq->wqe.info.arr[0].frag_stride; 991 992 err = mlx5e_init_wqe_alloc_info(rq, node); 993 if (err) 994 goto err_rq_wq_destroy; 995 } 996 997 if (mlx5e_rqo_xsk_param(rqo)) { 998 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq, 999 MEM_TYPE_XSK_BUFF_POOL, NULL); 1000 if (err) 1001 goto err_free_by_rq_type; 1002 xsk_pool_set_rxq_info(rq->xsk_pool, &rq->xdp_rxq); 1003 } else { 1004 /* Create a page_pool and register it with rxq */ 1005 struct page_pool_params pp_params = { 0 }; 1006 1007 if (WARN_ON(BIT(PAGE_SHIFT + pool_order) / 64 > 1008 MLX5E_PAGECNT_BIAS_MAX)) { 1009 err = -E2BIG; 1010 goto err_free_by_rq_type; 1011 } 1012 1013 pp_params.order = pool_order; 1014 pp_params.flags = PP_FLAG_DMA_MAP | PP_FLAG_DMA_SYNC_DEV; 1015 pp_params.pool_size = pool_size; 1016 pp_params.nid = node; 1017 pp_params.dev = rq->pdev; 1018 pp_params.napi = rq->cq.napi; 1019 pp_params.netdev = rq->netdev; 1020 pp_params.dma_dir = rq->buff.map_dir; 1021 pp_params.max_len = BIT(PAGE_SHIFT + pool_order); 1022 pp_params.queue_idx = rq->ix; 1023 1024 /* Shampo header data split allow for unreadable netmem */ 1025 if (test_bit(MLX5E_RQ_STATE_SHAMPO, &rq->state)) 1026 pp_params.flags |= PP_FLAG_ALLOW_UNREADABLE_NETMEM; 1027 1028 /* page_pool can be used even when there is no rq->xdp_prog, 1029 * given page_pool does not handle DMA mapping there is no 1030 * required state to clear. And page_pool gracefully handle 1031 * elevated refcnt. 1032 */ 1033 rq->page_pool = page_pool_create(&pp_params); 1034 if (IS_ERR(rq->page_pool)) { 1035 err = PTR_ERR(rq->page_pool); 1036 rq->page_pool = NULL; 1037 goto err_free_by_rq_type; 1038 } 1039 if (!rq->hd_page_pool) 1040 rq->hd_page_pool = rq->page_pool; 1041 if (xdp_rxq_info_is_reg(&rq->xdp_rxq)) { 1042 err = xdp_rxq_info_reg_mem_model(&rq->xdp_rxq, 1043 MEM_TYPE_PAGE_POOL, rq->page_pool); 1044 if (err) 1045 goto err_destroy_page_pool; 1046 } 1047 } 1048 1049 for (i = 0; i < wq_sz; i++) { 1050 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) { 1051 struct mlx5e_rx_wqe_ll *wqe = 1052 mlx5_wq_ll_get_wqe(&rq->mpwqe.wq, i); 1053 u32 byte_count = 1054 rq->mpwqe.num_strides << rq->mpwqe.log_stride_sz; 1055 u64 dma_offset = mul_u32_u32(i, rq->mpwqe.mtts_per_wqe) << 1056 rq->mpwqe.page_shift; 1057 u16 headroom = test_bit(MLX5E_RQ_STATE_SHAMPO, &rq->state) ? 1058 0 : rq->buff.headroom; 1059 1060 wqe->data[0].addr = cpu_to_be64(dma_offset + headroom); 1061 wqe->data[0].byte_count = cpu_to_be32(byte_count); 1062 wqe->data[0].lkey = rq->mpwqe.umr_mkey_be; 1063 } else { 1064 struct mlx5e_rx_wqe_cyc *wqe = 1065 mlx5_wq_cyc_get_wqe(&rq->wqe.wq, i); 1066 int f; 1067 1068 for (f = 0; f < rq->wqe.info.num_frags; f++) { 1069 u32 frag_size = rq->wqe.info.arr[f].frag_size | 1070 MLX5_HW_START_PADDING; 1071 1072 wqe->data[f].byte_count = cpu_to_be32(frag_size); 1073 wqe->data[f].lkey = rq->mkey_be; 1074 } 1075 /* check if num_frags is not a pow of two */ 1076 if (rq->wqe.info.num_frags < (1 << rq->wqe.info.log_num_frags)) { 1077 wqe->data[f].byte_count = 0; 1078 wqe->data[f].lkey = params->terminate_lkey_be; 1079 wqe->data[f].addr = 0; 1080 } 1081 } 1082 } 1083 1084 return 0; 1085 1086 err_destroy_page_pool: 1087 page_pool_destroy(rq->page_pool); 1088 err_free_by_rq_type: 1089 switch (rq->wq_type) { 1090 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ: 1091 mlx5e_rq_free_shampo(rq); 1092 err_free_mpwqe_linear_info: 1093 kvfree(rq->mpwqe.linear_info); 1094 err_free_mpwqe_info: 1095 kvfree(rq->mpwqe.info); 1096 err_rq_mkey: 1097 mlx5_core_destroy_mkey(mdev, be32_to_cpu(rq->mpwqe.umr_mkey_be)); 1098 err_rq_drop_page: 1099 mlx5e_free_mpwqe_rq_drop_page(rq); 1100 break; 1101 default: /* MLX5_WQ_TYPE_CYCLIC */ 1102 mlx5e_free_wqe_alloc_info(rq); 1103 } 1104 err_rq_wq_destroy: 1105 mlx5_wq_destroy(&rq->wq_ctrl); 1106 err_rq_xdp_prog: 1107 if (params->xdp_prog) 1108 bpf_prog_put(params->xdp_prog); 1109 1110 return err; 1111 } 1112 1113 static void mlx5e_free_rq(struct mlx5e_rq *rq) 1114 { 1115 kvfree(rq->dim); 1116 page_pool_destroy(rq->page_pool); 1117 1118 switch (rq->wq_type) { 1119 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ: 1120 mlx5e_rq_free_shampo(rq); 1121 kvfree(rq->mpwqe.linear_info); 1122 kvfree(rq->mpwqe.info); 1123 mlx5_core_destroy_mkey(rq->mdev, be32_to_cpu(rq->mpwqe.umr_mkey_be)); 1124 mlx5e_free_mpwqe_rq_drop_page(rq); 1125 break; 1126 default: /* MLX5_WQ_TYPE_CYCLIC */ 1127 mlx5e_free_wqe_alloc_info(rq); 1128 } 1129 1130 mlx5_wq_destroy(&rq->wq_ctrl); 1131 1132 if (xdp_rxq_info_is_reg(&rq->xdp_rxq)) { 1133 struct bpf_prog *old_prog; 1134 1135 old_prog = rcu_dereference_protected(rq->xdp_prog, 1136 lockdep_is_held(&rq->priv->state_lock)); 1137 if (old_prog) 1138 bpf_prog_put(old_prog); 1139 } 1140 xdp_rxq_info_unreg(&rq->xdp_rxq); 1141 } 1142 1143 int mlx5e_create_rq(struct mlx5e_rq *rq, struct mlx5e_rq_param *rq_param, 1144 u16 q_counter) 1145 { 1146 struct mlx5_core_dev *mdev = rq->mdev; 1147 u8 ts_format; 1148 void *in; 1149 void *rqc; 1150 void *wq; 1151 int inlen; 1152 int err; 1153 1154 inlen = MLX5_ST_SZ_BYTES(create_rq_in) + 1155 sizeof(u64) * rq->wq_ctrl.buf.npages; 1156 in = kvzalloc(inlen, GFP_KERNEL); 1157 if (!in) 1158 return -ENOMEM; 1159 1160 ts_format = mlx5_is_real_time_rq(mdev) ? 1161 MLX5_TIMESTAMP_FORMAT_REAL_TIME : 1162 MLX5_TIMESTAMP_FORMAT_FREE_RUNNING; 1163 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx); 1164 wq = MLX5_ADDR_OF(rqc, rqc, wq); 1165 1166 memcpy(rqc, rq_param->rqc, sizeof(rq_param->rqc)); 1167 1168 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn); 1169 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST); 1170 MLX5_SET(rqc, rqc, ts_format, ts_format); 1171 MLX5_SET(rqc, rqc, counter_set_id, q_counter); 1172 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift - 1173 MLX5_ADAPTER_PAGE_SHIFT); 1174 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma); 1175 1176 if (test_bit(MLX5E_RQ_STATE_SHAMPO, &rq->state)) { 1177 MLX5_SET(wq, wq, log_headers_buffer_entry_num, 1178 order_base_2(rq->mpwqe.shampo->hd_per_wq)); 1179 MLX5_SET(wq, wq, headers_mkey, rq->mpwqe.shampo->mkey); 1180 } 1181 1182 mlx5_fill_page_frag_array(&rq->wq_ctrl.buf, 1183 (__be64 *)MLX5_ADDR_OF(wq, wq, pas)); 1184 1185 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn); 1186 1187 kvfree(in); 1188 1189 return err; 1190 } 1191 1192 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state, int next_state) 1193 { 1194 struct mlx5_core_dev *mdev = rq->mdev; 1195 1196 void *in; 1197 void *rqc; 1198 int inlen; 1199 int err; 1200 1201 inlen = MLX5_ST_SZ_BYTES(modify_rq_in); 1202 in = kvzalloc(inlen, GFP_KERNEL); 1203 if (!in) 1204 return -ENOMEM; 1205 1206 if (curr_state == MLX5_RQC_STATE_RST && next_state == MLX5_RQC_STATE_RDY) 1207 mlx5e_rqwq_reset(rq); 1208 1209 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx); 1210 1211 MLX5_SET(modify_rq_in, in, rq_state, curr_state); 1212 MLX5_SET(rqc, rqc, state, next_state); 1213 1214 err = mlx5_core_modify_rq(mdev, rq->rqn, in); 1215 1216 kvfree(in); 1217 1218 return err; 1219 } 1220 1221 static void mlx5e_flush_rq_cq(struct mlx5e_rq *rq) 1222 { 1223 struct mlx5_cqwq *cqwq = &rq->cq.wq; 1224 struct mlx5_cqe64 *cqe; 1225 1226 if (test_bit(MLX5E_RQ_STATE_MINI_CQE_ENHANCED, &rq->state)) { 1227 while ((cqe = mlx5_cqwq_get_cqe_enhanced_comp(cqwq))) 1228 mlx5_cqwq_pop(cqwq); 1229 } else { 1230 while ((cqe = mlx5_cqwq_get_cqe(cqwq))) 1231 mlx5_cqwq_pop(cqwq); 1232 } 1233 1234 mlx5_cqwq_update_db_record(cqwq); 1235 } 1236 1237 int mlx5e_flush_rq(struct mlx5e_rq *rq, int curr_state) 1238 { 1239 struct net_device *dev = rq->netdev; 1240 int err; 1241 1242 err = mlx5e_modify_rq_state(rq, curr_state, MLX5_RQC_STATE_RST); 1243 if (err) { 1244 netdev_err(dev, "Failed to move rq 0x%x to reset\n", rq->rqn); 1245 return err; 1246 } 1247 1248 mlx5e_free_rx_descs(rq); 1249 mlx5e_flush_rq_cq(rq); 1250 1251 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY); 1252 if (err) { 1253 netdev_err(dev, "Failed to move rq 0x%x to ready\n", rq->rqn); 1254 return err; 1255 } 1256 1257 return 0; 1258 } 1259 1260 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd) 1261 { 1262 struct mlx5_core_dev *mdev = rq->mdev; 1263 void *in; 1264 void *rqc; 1265 int inlen; 1266 int err; 1267 1268 inlen = MLX5_ST_SZ_BYTES(modify_rq_in); 1269 in = kvzalloc(inlen, GFP_KERNEL); 1270 if (!in) 1271 return -ENOMEM; 1272 1273 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx); 1274 1275 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY); 1276 MLX5_SET64(modify_rq_in, in, modify_bitmask, 1277 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD); 1278 MLX5_SET(rqc, rqc, vsd, vsd); 1279 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY); 1280 1281 err = mlx5_core_modify_rq(mdev, rq->rqn, in); 1282 1283 kvfree(in); 1284 1285 return err; 1286 } 1287 1288 void mlx5e_destroy_rq(struct mlx5e_rq *rq) 1289 { 1290 mlx5_core_destroy_rq(rq->mdev, rq->rqn); 1291 } 1292 1293 int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq, int wait_time) 1294 { 1295 unsigned long exp_time = jiffies + msecs_to_jiffies(wait_time); 1296 1297 u16 min_wqes = mlx5_min_rx_wqes(rq->wq_type, mlx5e_rqwq_get_size(rq)); 1298 1299 do { 1300 if (mlx5e_rqwq_get_cur_sz(rq) >= min_wqes) 1301 return 0; 1302 1303 msleep(20); 1304 } while (time_before(jiffies, exp_time)); 1305 1306 netdev_warn(rq->netdev, "Failed to get min RX wqes on Channel[%d] RQN[0x%x] wq cur_sz(%d) min_rx_wqes(%d)\n", 1307 rq->ix, rq->rqn, mlx5e_rqwq_get_cur_sz(rq), min_wqes); 1308 1309 queue_work(rq->priv->wq, &rq->rx_timeout_work); 1310 1311 return -ETIMEDOUT; 1312 } 1313 1314 void mlx5e_free_rx_missing_descs(struct mlx5e_rq *rq) 1315 { 1316 struct mlx5_wq_ll *wq; 1317 u16 head; 1318 int i; 1319 1320 if (rq->wq_type != MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) 1321 return; 1322 1323 wq = &rq->mpwqe.wq; 1324 head = wq->head; 1325 1326 /* Release WQEs that are in missing state: they have been 1327 * popped from the list after completion but were not freed 1328 * due to deferred release. 1329 * Also free the linked-list reserved entry, hence the "+ 1". 1330 */ 1331 for (i = 0; i < mlx5_wq_ll_missing(wq) + 1; i++) { 1332 rq->dealloc_wqe(rq, head); 1333 head = mlx5_wq_ll_get_wqe_next_ix(wq, head); 1334 } 1335 1336 rq->mpwqe.actual_wq_head = wq->head; 1337 rq->mpwqe.umr_in_progress = 0; 1338 rq->mpwqe.umr_completed = 0; 1339 } 1340 1341 void mlx5e_free_rx_descs(struct mlx5e_rq *rq) 1342 { 1343 __be16 wqe_ix_be; 1344 u16 wqe_ix; 1345 1346 if (rq->wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) { 1347 struct mlx5_wq_ll *wq = &rq->mpwqe.wq; 1348 1349 mlx5e_free_rx_missing_descs(rq); 1350 1351 while (!mlx5_wq_ll_is_empty(wq)) { 1352 struct mlx5e_rx_wqe_ll *wqe; 1353 1354 wqe_ix_be = *wq->tail_next; 1355 wqe_ix = be16_to_cpu(wqe_ix_be); 1356 wqe = mlx5_wq_ll_get_wqe(wq, wqe_ix); 1357 rq->dealloc_wqe(rq, wqe_ix); 1358 mlx5_wq_ll_pop(wq, wqe_ix_be, 1359 &wqe->next.next_wqe_index); 1360 } 1361 1362 mlx5e_mpwqe_dealloc_linear_page(rq); 1363 } else { 1364 struct mlx5_wq_cyc *wq = &rq->wqe.wq; 1365 u16 missing = mlx5_wq_cyc_missing(wq); 1366 u16 head = mlx5_wq_cyc_get_head(wq); 1367 1368 while (!mlx5_wq_cyc_is_empty(wq)) { 1369 wqe_ix = mlx5_wq_cyc_get_tail(wq); 1370 rq->dealloc_wqe(rq, wqe_ix); 1371 mlx5_wq_cyc_pop(wq); 1372 } 1373 /* Missing slots might also contain unreleased pages due to 1374 * deferred release. 1375 */ 1376 while (missing--) { 1377 wqe_ix = mlx5_wq_cyc_ctr2ix(wq, head++); 1378 rq->dealloc_wqe(rq, wqe_ix); 1379 } 1380 } 1381 1382 } 1383 1384 int mlx5e_open_rq(struct mlx5e_params *params, struct mlx5e_rq_param *rq_param, 1385 struct mlx5e_rq_opt_param *rqo, int node, u16 q_counter, 1386 struct mlx5e_rq *rq) 1387 { 1388 struct mlx5_core_dev *mdev = rq->mdev; 1389 int err; 1390 1391 if (params->packet_merge.type == MLX5E_PACKET_MERGE_SHAMPO) 1392 __set_bit(MLX5E_RQ_STATE_SHAMPO, &rq->state); 1393 1394 err = mlx5e_alloc_rq(params, rq_param, rqo, node, rq); 1395 if (err) 1396 return err; 1397 1398 err = mlx5e_create_rq(rq, rq_param, q_counter); 1399 if (err) 1400 goto err_free_rq; 1401 1402 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY); 1403 if (err) 1404 goto err_destroy_rq; 1405 1406 if (MLX5_CAP_ETH(mdev, cqe_checksum_full)) 1407 __set_bit(MLX5E_RQ_STATE_CSUM_FULL, &rq->state); 1408 1409 if (rq->channel && !params->rx_dim_enabled) { 1410 rq->channel->rx_cq_moder = params->rx_cq_moderation; 1411 } else if (rq->channel) { 1412 u8 cq_period_mode; 1413 1414 cq_period_mode = params->rx_moder_use_cqe_mode ? 1415 DIM_CQ_PERIOD_MODE_START_FROM_CQE : 1416 DIM_CQ_PERIOD_MODE_START_FROM_EQE; 1417 mlx5e_reset_rx_moderation(&rq->channel->rx_cq_moder, cq_period_mode, 1418 params->rx_dim_enabled); 1419 1420 err = mlx5e_dim_rx_change(rq, params->rx_dim_enabled); 1421 if (err) 1422 goto err_destroy_rq; 1423 } 1424 1425 /* We disable csum_complete when XDP is enabled since 1426 * XDP programs might manipulate packets which will render 1427 * skb->checksum incorrect. 1428 */ 1429 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_NO_CSUM_COMPLETE) || params->xdp_prog) 1430 __set_bit(MLX5E_RQ_STATE_NO_CSUM_COMPLETE, &rq->state); 1431 1432 /* For CQE compression on striding RQ, use stride index provided by 1433 * HW if capability is supported. 1434 */ 1435 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ) && 1436 MLX5_CAP_GEN(mdev, mini_cqe_resp_stride_index)) 1437 __set_bit(MLX5E_RQ_STATE_MINI_CQE_HW_STRIDX, &rq->state); 1438 1439 /* For enhanced CQE compression packet processing. decompress 1440 * session according to the enhanced layout. 1441 */ 1442 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS) && 1443 MLX5_CAP_GEN(mdev, enhanced_cqe_compression)) 1444 __set_bit(MLX5E_RQ_STATE_MINI_CQE_ENHANCED, &rq->state); 1445 1446 return 0; 1447 1448 err_destroy_rq: 1449 mlx5e_destroy_rq(rq); 1450 err_free_rq: 1451 mlx5e_free_rq(rq); 1452 1453 return err; 1454 } 1455 1456 void mlx5e_activate_rq(struct mlx5e_rq *rq) 1457 { 1458 set_bit(MLX5E_RQ_STATE_ENABLED, &rq->state); 1459 } 1460 1461 void mlx5e_deactivate_rq(struct mlx5e_rq *rq) 1462 { 1463 clear_bit(MLX5E_RQ_STATE_ENABLED, &rq->state); 1464 synchronize_net(); /* Sync with NAPI to prevent mlx5e_post_rx_wqes. */ 1465 } 1466 1467 void mlx5e_close_rq(struct mlx5e_rq *rq) 1468 { 1469 if (rq->dim) 1470 cancel_work_sync(&rq->dim->work); 1471 cancel_work_sync(&rq->recover_work); 1472 cancel_work_sync(&rq->rx_timeout_work); 1473 mlx5e_destroy_rq(rq); 1474 mlx5e_free_rx_descs(rq); 1475 mlx5e_free_rq(rq); 1476 } 1477 1478 u32 mlx5e_profile_get_tisn(struct mlx5_core_dev *mdev, 1479 struct mlx5e_priv *priv, 1480 const struct mlx5e_profile *profile, 1481 u8 lag_port, u8 tc) 1482 { 1483 if (profile->get_tisn) 1484 return profile->get_tisn(mdev, priv, lag_port, tc); 1485 1486 return mdev->mlx5e_res.hw_objs.tisn[lag_port][tc]; 1487 } 1488 1489 static void mlx5e_free_xdpsq_db(struct mlx5e_xdpsq *sq) 1490 { 1491 kvfree(sq->db.xdpi_fifo.xi); 1492 kvfree(sq->db.wqe_info); 1493 } 1494 1495 static int mlx5e_alloc_xdpsq_fifo(struct mlx5e_xdpsq *sq, int numa) 1496 { 1497 struct mlx5e_xdp_info_fifo *xdpi_fifo = &sq->db.xdpi_fifo; 1498 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq); 1499 int entries; 1500 size_t size; 1501 1502 /* upper bound for maximum num of entries of all xmit_modes. */ 1503 entries = roundup_pow_of_two(wq_sz * MLX5_SEND_WQEBB_NUM_DS * 1504 MLX5E_XDP_FIFO_ENTRIES2DS_MAX_RATIO); 1505 1506 size = array_size(sizeof(*xdpi_fifo->xi), entries); 1507 xdpi_fifo->xi = kvzalloc_node(size, GFP_KERNEL, numa); 1508 if (!xdpi_fifo->xi) 1509 return -ENOMEM; 1510 1511 xdpi_fifo->pc = &sq->xdpi_fifo_pc; 1512 xdpi_fifo->cc = &sq->xdpi_fifo_cc; 1513 xdpi_fifo->mask = entries - 1; 1514 1515 return 0; 1516 } 1517 1518 static int mlx5e_alloc_xdpsq_db(struct mlx5e_xdpsq *sq, int numa) 1519 { 1520 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq); 1521 size_t size; 1522 int err; 1523 1524 size = array_size(sizeof(*sq->db.wqe_info), wq_sz); 1525 sq->db.wqe_info = kvzalloc_node(size, GFP_KERNEL, numa); 1526 if (!sq->db.wqe_info) 1527 return -ENOMEM; 1528 1529 err = mlx5e_alloc_xdpsq_fifo(sq, numa); 1530 if (err) { 1531 mlx5e_free_xdpsq_db(sq); 1532 return err; 1533 } 1534 1535 return 0; 1536 } 1537 1538 static int mlx5e_alloc_xdpsq(struct mlx5e_channel *c, 1539 struct mlx5e_params *params, 1540 struct xsk_buff_pool *xsk_pool, 1541 struct mlx5e_sq_param *param, 1542 struct mlx5e_xdpsq *sq, 1543 bool is_redirect) 1544 { 1545 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq); 1546 struct mlx5_core_dev *mdev = c->mdev; 1547 struct mlx5_wq_cyc *wq = &sq->wq; 1548 int err; 1549 1550 sq->pdev = c->pdev; 1551 sq->mkey_be = c->mkey_be; 1552 sq->channel = c; 1553 sq->uar_map = c->bfreg->map; 1554 sq->min_inline_mode = params->tx_min_inline_mode; 1555 sq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu) - ETH_FCS_LEN; 1556 sq->xsk_pool = xsk_pool; 1557 1558 sq->stats = sq->xsk_pool ? 1559 &c->priv->channel_stats[c->ix]->xsksq : 1560 is_redirect ? 1561 &c->priv->channel_stats[c->ix]->xdpsq : 1562 &c->priv->channel_stats[c->ix]->rq_xdpsq; 1563 sq->stop_room = param->is_mpw ? mlx5e_stop_room_for_mpwqe(mdev) : 1564 mlx5e_stop_room_for_max_wqe(mdev); 1565 sq->max_sq_mpw_wqebbs = mlx5e_get_max_sq_aligned_wqebbs(mdev); 1566 1567 param->wq.db_numa_node = cpu_to_node(c->cpu); 1568 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl); 1569 if (err) 1570 return err; 1571 wq->db = &wq->db[MLX5_SND_DBR]; 1572 1573 err = mlx5e_alloc_xdpsq_db(sq, cpu_to_node(c->cpu)); 1574 if (err) 1575 goto err_sq_wq_destroy; 1576 1577 return 0; 1578 1579 err_sq_wq_destroy: 1580 mlx5_wq_destroy(&sq->wq_ctrl); 1581 1582 return err; 1583 } 1584 1585 static void mlx5e_free_xdpsq(struct mlx5e_xdpsq *sq) 1586 { 1587 mlx5e_free_xdpsq_db(sq); 1588 mlx5_wq_destroy(&sq->wq_ctrl); 1589 } 1590 1591 static void mlx5e_free_icosq_db(struct mlx5e_icosq *sq) 1592 { 1593 kvfree(sq->db.wqe_info); 1594 } 1595 1596 static int mlx5e_alloc_icosq_db(struct mlx5e_icosq *sq, int numa) 1597 { 1598 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq); 1599 size_t size; 1600 1601 size = array_size(wq_sz, sizeof(*sq->db.wqe_info)); 1602 sq->db.wqe_info = kvzalloc_node(size, GFP_KERNEL, numa); 1603 if (!sq->db.wqe_info) 1604 return -ENOMEM; 1605 1606 return 0; 1607 } 1608 1609 static void mlx5e_icosq_err_cqe_work(struct work_struct *recover_work) 1610 { 1611 struct mlx5e_icosq *sq = container_of(recover_work, struct mlx5e_icosq, 1612 recover_work); 1613 1614 mlx5e_reporter_icosq_cqe_err(sq); 1615 } 1616 1617 static void mlx5e_async_icosq_err_cqe_work(struct work_struct *recover_work) 1618 { 1619 struct mlx5e_icosq *sq = container_of(recover_work, struct mlx5e_icosq, 1620 recover_work); 1621 1622 /* Not implemented yet. */ 1623 1624 netdev_warn(sq->channel->netdev, "async_icosq recovery is not implemented\n"); 1625 } 1626 1627 static int mlx5e_alloc_icosq(struct mlx5e_channel *c, 1628 struct mlx5e_sq_param *param, 1629 struct mlx5e_icosq *sq, 1630 work_func_t recover_work_func) 1631 { 1632 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq); 1633 struct mlx5_core_dev *mdev = c->mdev; 1634 struct mlx5_wq_cyc *wq = &sq->wq; 1635 int err; 1636 1637 sq->channel = c; 1638 sq->uar_map = c->bfreg->map; 1639 sq->reserved_room = param->stop_room; 1640 1641 param->wq.db_numa_node = cpu_to_node(c->cpu); 1642 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl); 1643 if (err) 1644 return err; 1645 wq->db = &wq->db[MLX5_SND_DBR]; 1646 1647 err = mlx5e_alloc_icosq_db(sq, cpu_to_node(c->cpu)); 1648 if (err) 1649 goto err_sq_wq_destroy; 1650 1651 INIT_WORK(&sq->recover_work, recover_work_func); 1652 1653 return 0; 1654 1655 err_sq_wq_destroy: 1656 mlx5_wq_destroy(&sq->wq_ctrl); 1657 1658 return err; 1659 } 1660 1661 static void mlx5e_free_icosq(struct mlx5e_icosq *sq) 1662 { 1663 mlx5e_free_icosq_db(sq); 1664 mlx5_wq_destroy(&sq->wq_ctrl); 1665 } 1666 1667 void mlx5e_free_txqsq_db(struct mlx5e_txqsq *sq) 1668 { 1669 kvfree(sq->db.wqe_info); 1670 kvfree(sq->db.skb_fifo.fifo); 1671 kvfree(sq->db.dma_fifo); 1672 } 1673 1674 int mlx5e_alloc_txqsq_db(struct mlx5e_txqsq *sq, int numa) 1675 { 1676 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq); 1677 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS; 1678 1679 sq->db.dma_fifo = kvzalloc_node(array_size(df_sz, 1680 sizeof(*sq->db.dma_fifo)), 1681 GFP_KERNEL, numa); 1682 sq->db.skb_fifo.fifo = kvzalloc_node(array_size(df_sz, 1683 sizeof(*sq->db.skb_fifo.fifo)), 1684 GFP_KERNEL, numa); 1685 sq->db.wqe_info = kvzalloc_node(array_size(wq_sz, 1686 sizeof(*sq->db.wqe_info)), 1687 GFP_KERNEL, numa); 1688 if (!sq->db.dma_fifo || !sq->db.skb_fifo.fifo || !sq->db.wqe_info) { 1689 mlx5e_free_txqsq_db(sq); 1690 return -ENOMEM; 1691 } 1692 1693 sq->dma_fifo_mask = df_sz - 1; 1694 1695 sq->db.skb_fifo.pc = &sq->skb_fifo_pc; 1696 sq->db.skb_fifo.cc = &sq->skb_fifo_cc; 1697 sq->db.skb_fifo.mask = df_sz - 1; 1698 1699 return 0; 1700 } 1701 1702 static int mlx5e_alloc_txqsq(struct mlx5e_channel *c, 1703 int txq_ix, 1704 struct mlx5e_params *params, 1705 struct mlx5e_sq_param *param, 1706 struct mlx5e_txqsq *sq, 1707 int tc) 1708 { 1709 void *sqc_wq = MLX5_ADDR_OF(sqc, param->sqc, wq); 1710 struct mlx5_core_dev *mdev = c->mdev; 1711 struct mlx5_wq_cyc *wq = &sq->wq; 1712 int err; 1713 1714 sq->pdev = c->pdev; 1715 sq->clock = mdev->clock; 1716 sq->mkey_be = c->mkey_be; 1717 sq->netdev = c->netdev; 1718 sq->mdev = c->mdev; 1719 sq->channel = c; 1720 sq->priv = c->priv; 1721 sq->ch_ix = c->ix; 1722 sq->txq_ix = txq_ix; 1723 sq->uar_map = c->bfreg->map; 1724 sq->min_inline_mode = params->tx_min_inline_mode; 1725 sq->hw_mtu = MLX5E_SW2HW_MTU(params, params->sw_mtu); 1726 sq->max_sq_mpw_wqebbs = mlx5e_get_max_sq_aligned_wqebbs(mdev); 1727 INIT_WORK(&sq->recover_work, mlx5e_tx_err_cqe_work); 1728 if (mlx5_ipsec_device_caps(c->priv->mdev)) 1729 set_bit(MLX5E_SQ_STATE_IPSEC, &sq->state); 1730 if (param->is_mpw) 1731 set_bit(MLX5E_SQ_STATE_MPWQE, &sq->state); 1732 sq->stop_room = param->stop_room; 1733 sq->ptp_cyc2time = mlx5_sq_ts_translator(mdev); 1734 1735 param->wq.db_numa_node = cpu_to_node(c->cpu); 1736 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, wq, &sq->wq_ctrl); 1737 if (err) 1738 return err; 1739 wq->db = &wq->db[MLX5_SND_DBR]; 1740 1741 err = mlx5e_alloc_txqsq_db(sq, cpu_to_node(c->cpu)); 1742 if (err) 1743 goto err_sq_wq_destroy; 1744 1745 return 0; 1746 1747 err_sq_wq_destroy: 1748 mlx5_wq_destroy(&sq->wq_ctrl); 1749 1750 return err; 1751 } 1752 1753 void mlx5e_free_txqsq(struct mlx5e_txqsq *sq) 1754 { 1755 kvfree(sq->dim); 1756 mlx5e_free_txqsq_db(sq); 1757 mlx5_wq_destroy(&sq->wq_ctrl); 1758 } 1759 1760 static int mlx5e_create_sq(struct mlx5_core_dev *mdev, 1761 struct mlx5e_sq_param *param, 1762 struct mlx5e_create_sq_param *csp, 1763 u32 *sqn) 1764 { 1765 u8 ts_format; 1766 void *in; 1767 void *sqc; 1768 void *wq; 1769 int inlen; 1770 int err; 1771 1772 inlen = MLX5_ST_SZ_BYTES(create_sq_in) + 1773 sizeof(u64) * csp->wq_ctrl->buf.npages; 1774 in = kvzalloc(inlen, GFP_KERNEL); 1775 if (!in) 1776 return -ENOMEM; 1777 1778 ts_format = mlx5_is_real_time_sq(mdev) ? 1779 MLX5_TIMESTAMP_FORMAT_REAL_TIME : 1780 MLX5_TIMESTAMP_FORMAT_FREE_RUNNING; 1781 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx); 1782 wq = MLX5_ADDR_OF(sqc, sqc, wq); 1783 1784 memcpy(sqc, param->sqc, sizeof(param->sqc)); 1785 MLX5_SET(sqc, sqc, tis_lst_sz, csp->tis_lst_sz); 1786 MLX5_SET(sqc, sqc, tis_num_0, csp->tisn); 1787 MLX5_SET(sqc, sqc, cqn, csp->cqn); 1788 MLX5_SET(sqc, sqc, ts_cqe_to_dest_cqn, csp->ts_cqe_to_dest_cqn); 1789 MLX5_SET(sqc, sqc, ts_format, ts_format); 1790 1791 1792 if (MLX5_CAP_ETH(mdev, wqe_inline_mode) == MLX5_CAP_INLINE_MODE_VPORT_CONTEXT) 1793 MLX5_SET(sqc, sqc, min_wqe_inline_mode, csp->min_inline_mode); 1794 1795 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST); 1796 MLX5_SET(sqc, sqc, flush_in_error_en, 1); 1797 1798 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC); 1799 MLX5_SET(wq, wq, uar_page, csp->uar_page); 1800 MLX5_SET(wq, wq, log_wq_pg_sz, csp->wq_ctrl->buf.page_shift - 1801 MLX5_ADAPTER_PAGE_SHIFT); 1802 MLX5_SET64(wq, wq, dbr_addr, csp->wq_ctrl->db.dma); 1803 1804 mlx5_fill_page_frag_array(&csp->wq_ctrl->buf, 1805 (__be64 *)MLX5_ADDR_OF(wq, wq, pas)); 1806 1807 err = mlx5_core_create_sq(mdev, in, inlen, sqn); 1808 1809 kvfree(in); 1810 1811 return err; 1812 } 1813 1814 int mlx5e_modify_sq(struct mlx5_core_dev *mdev, u32 sqn, 1815 struct mlx5e_modify_sq_param *p) 1816 { 1817 u64 bitmask = 0; 1818 void *in; 1819 void *sqc; 1820 int inlen; 1821 int err; 1822 1823 inlen = MLX5_ST_SZ_BYTES(modify_sq_in); 1824 in = kvzalloc(inlen, GFP_KERNEL); 1825 if (!in) 1826 return -ENOMEM; 1827 1828 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx); 1829 1830 MLX5_SET(modify_sq_in, in, sq_state, p->curr_state); 1831 MLX5_SET(sqc, sqc, state, p->next_state); 1832 if (p->rl_update && p->next_state == MLX5_SQC_STATE_RDY) { 1833 bitmask |= 1; 1834 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, p->rl_index); 1835 } 1836 if (p->qos_update && p->next_state == MLX5_SQC_STATE_RDY) { 1837 bitmask |= 1 << 2; 1838 MLX5_SET(sqc, sqc, qos_queue_group_id, p->qos_queue_group_id); 1839 } 1840 MLX5_SET64(modify_sq_in, in, modify_bitmask, bitmask); 1841 1842 err = mlx5_core_modify_sq(mdev, sqn, in); 1843 1844 kvfree(in); 1845 1846 return err; 1847 } 1848 1849 static void mlx5e_destroy_sq(struct mlx5_core_dev *mdev, u32 sqn) 1850 { 1851 mlx5_core_destroy_sq(mdev, sqn); 1852 } 1853 1854 int mlx5e_create_sq_rdy(struct mlx5_core_dev *mdev, 1855 struct mlx5e_sq_param *param, 1856 struct mlx5e_create_sq_param *csp, 1857 u16 qos_queue_group_id, 1858 u32 *sqn) 1859 { 1860 struct mlx5e_modify_sq_param msp = {0}; 1861 int err; 1862 1863 err = mlx5e_create_sq(mdev, param, csp, sqn); 1864 if (err) 1865 return err; 1866 1867 msp.curr_state = MLX5_SQC_STATE_RST; 1868 msp.next_state = MLX5_SQC_STATE_RDY; 1869 if (qos_queue_group_id) { 1870 msp.qos_update = true; 1871 msp.qos_queue_group_id = qos_queue_group_id; 1872 } 1873 err = mlx5e_modify_sq(mdev, *sqn, &msp); 1874 if (err) 1875 mlx5e_destroy_sq(mdev, *sqn); 1876 1877 return err; 1878 } 1879 1880 static int mlx5e_set_sq_maxrate(struct net_device *dev, 1881 struct mlx5e_txqsq *sq, u32 rate); 1882 1883 int mlx5e_open_txqsq(struct mlx5e_channel *c, u32 tisn, int txq_ix, 1884 struct mlx5e_params *params, struct mlx5e_sq_param *param, 1885 struct mlx5e_txqsq *sq, int tc, u16 qos_queue_group_id, 1886 struct mlx5e_sq_stats *sq_stats) 1887 { 1888 struct mlx5e_create_sq_param csp = {}; 1889 u32 tx_rate; 1890 int err; 1891 1892 err = mlx5e_alloc_txqsq(c, txq_ix, params, param, sq, tc); 1893 if (err) 1894 return err; 1895 1896 sq->stats = sq_stats; 1897 1898 csp.tisn = tisn; 1899 csp.tis_lst_sz = 1; 1900 csp.cqn = sq->cq.mcq.cqn; 1901 csp.wq_ctrl = &sq->wq_ctrl; 1902 csp.min_inline_mode = sq->min_inline_mode; 1903 csp.uar_page = c->bfreg->index; 1904 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, qos_queue_group_id, &sq->sqn); 1905 if (err) 1906 goto err_free_txqsq; 1907 1908 tx_rate = c->priv->tx_rates[sq->txq_ix]; 1909 if (tx_rate) 1910 mlx5e_set_sq_maxrate(c->netdev, sq, tx_rate); 1911 1912 if (sq->channel && !params->tx_dim_enabled) { 1913 sq->channel->tx_cq_moder = params->tx_cq_moderation; 1914 } else if (sq->channel) { 1915 u8 cq_period_mode; 1916 1917 cq_period_mode = params->tx_moder_use_cqe_mode ? 1918 DIM_CQ_PERIOD_MODE_START_FROM_CQE : 1919 DIM_CQ_PERIOD_MODE_START_FROM_EQE; 1920 mlx5e_reset_tx_moderation(&sq->channel->tx_cq_moder, 1921 cq_period_mode, 1922 params->tx_dim_enabled); 1923 1924 err = mlx5e_dim_tx_change(sq, params->tx_dim_enabled); 1925 if (err) 1926 goto err_destroy_sq; 1927 } 1928 1929 return 0; 1930 1931 err_destroy_sq: 1932 mlx5e_destroy_sq(c->mdev, sq->sqn); 1933 err_free_txqsq: 1934 mlx5e_free_txqsq(sq); 1935 1936 return err; 1937 } 1938 1939 void mlx5e_activate_txqsq(struct mlx5e_txqsq *sq) 1940 { 1941 sq->txq = netdev_get_tx_queue(sq->netdev, sq->txq_ix); 1942 /* Reset BQL only when the SQ has no bytes in flight. */ 1943 if (sq->cc == sq->pc) 1944 netdev_tx_reset_queue(sq->txq); 1945 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state); 1946 netif_tx_start_queue(sq->txq); 1947 netif_queue_set_napi(sq->netdev, sq->txq_ix, NETDEV_QUEUE_TYPE_TX, sq->cq.napi); 1948 } 1949 1950 void mlx5e_tx_disable_queue(struct netdev_queue *txq) 1951 { 1952 __netif_tx_lock_bh(txq); 1953 netif_tx_stop_queue(txq); 1954 __netif_tx_unlock_bh(txq); 1955 } 1956 1957 void mlx5e_deactivate_txqsq(struct mlx5e_txqsq *sq) 1958 { 1959 struct mlx5_wq_cyc *wq = &sq->wq; 1960 1961 netif_queue_set_napi(sq->netdev, sq->txq_ix, NETDEV_QUEUE_TYPE_TX, NULL); 1962 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state); 1963 synchronize_net(); /* Sync with NAPI to prevent netif_tx_wake_queue. */ 1964 1965 mlx5e_tx_disable_queue(sq->txq); 1966 1967 /* last doorbell out, godspeed .. */ 1968 if (mlx5e_wqc_has_room_for(wq, sq->cc, sq->pc, 1)) { 1969 u16 pi = mlx5_wq_cyc_ctr2ix(wq, sq->pc); 1970 struct mlx5e_tx_wqe *nop; 1971 1972 sq->db.wqe_info[pi] = (struct mlx5e_tx_wqe_info) { 1973 .num_wqebbs = 1, 1974 }; 1975 1976 nop = mlx5e_post_nop(wq, sq->sqn, &sq->pc); 1977 mlx5e_notify_hw(wq, sq->pc, sq->uar_map, &nop->ctrl); 1978 } 1979 } 1980 1981 void mlx5e_close_txqsq(struct mlx5e_txqsq *sq) 1982 { 1983 struct mlx5_core_dev *mdev = sq->mdev; 1984 struct mlx5_rate_limit rl = {0}; 1985 1986 if (sq->dim) 1987 cancel_work_sync(&sq->dim->work); 1988 cancel_work_sync(&sq->recover_work); 1989 mlx5e_destroy_sq(mdev, sq->sqn); 1990 if (sq->rate_limit) { 1991 rl.rate = sq->rate_limit; 1992 mlx5_rl_remove_rate(mdev, &rl); 1993 } 1994 mlx5e_free_txqsq_descs(sq); 1995 mlx5e_free_txqsq(sq); 1996 } 1997 1998 void mlx5e_tx_err_cqe_work(struct work_struct *recover_work) 1999 { 2000 struct mlx5e_txqsq *sq = container_of(recover_work, struct mlx5e_txqsq, 2001 recover_work); 2002 2003 mlx5e_reporter_tx_err_cqe(sq); 2004 } 2005 2006 static struct dim_cq_moder mlx5e_get_def_tx_moderation(u8 cq_period_mode) 2007 { 2008 return (struct dim_cq_moder) { 2009 .cq_period_mode = cq_period_mode, 2010 .pkts = MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS, 2011 .usec = cq_period_mode == DIM_CQ_PERIOD_MODE_START_FROM_CQE ? 2012 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC_FROM_CQE : 2013 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC, 2014 }; 2015 } 2016 2017 bool mlx5e_reset_tx_moderation(struct dim_cq_moder *cq_moder, u8 cq_period_mode, 2018 bool dim_enabled) 2019 { 2020 bool reset_needed = cq_moder->cq_period_mode != cq_period_mode; 2021 2022 if (dim_enabled) 2023 *cq_moder = net_dim_get_def_tx_moderation(cq_period_mode); 2024 else 2025 *cq_moder = mlx5e_get_def_tx_moderation(cq_period_mode); 2026 2027 return reset_needed; 2028 } 2029 2030 bool mlx5e_reset_tx_channels_moderation(struct mlx5e_channels *chs, u8 cq_period_mode, 2031 bool dim_enabled, bool keep_dim_state) 2032 { 2033 bool reset = false; 2034 int i, tc; 2035 2036 for (i = 0; i < chs->num; i++) { 2037 for (tc = 0; tc < mlx5e_get_dcb_num_tc(&chs->params); tc++) { 2038 if (keep_dim_state) 2039 dim_enabled = !!chs->c[i]->sq[tc].dim; 2040 2041 reset |= mlx5e_reset_tx_moderation(&chs->c[i]->tx_cq_moder, 2042 cq_period_mode, dim_enabled); 2043 } 2044 } 2045 2046 return reset; 2047 } 2048 2049 static int mlx5e_open_icosq(struct mlx5e_channel *c, struct mlx5e_params *params, 2050 struct mlx5e_sq_param *param, struct mlx5e_icosq *sq, 2051 work_func_t recover_work_func) 2052 { 2053 struct mlx5e_create_sq_param csp = {}; 2054 int err; 2055 2056 err = mlx5e_alloc_icosq(c, param, sq, recover_work_func); 2057 if (err) 2058 return err; 2059 2060 csp.cqn = sq->cq.mcq.cqn; 2061 csp.wq_ctrl = &sq->wq_ctrl; 2062 csp.min_inline_mode = params->tx_min_inline_mode; 2063 csp.uar_page = c->bfreg->index; 2064 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, 0, &sq->sqn); 2065 if (err) 2066 goto err_free_icosq; 2067 2068 spin_lock_init(&sq->lock); 2069 2070 if (param->is_tls) { 2071 sq->ktls_resync = mlx5e_ktls_rx_resync_create_resp_list(); 2072 if (IS_ERR(sq->ktls_resync)) { 2073 err = PTR_ERR(sq->ktls_resync); 2074 goto err_destroy_icosq; 2075 } 2076 } 2077 return 0; 2078 2079 err_destroy_icosq: 2080 mlx5e_destroy_sq(c->mdev, sq->sqn); 2081 err_free_icosq: 2082 mlx5e_free_icosq(sq); 2083 2084 return err; 2085 } 2086 2087 void mlx5e_activate_icosq(struct mlx5e_icosq *icosq) 2088 { 2089 set_bit(MLX5E_SQ_STATE_ENABLED, &icosq->state); 2090 } 2091 2092 void mlx5e_deactivate_icosq(struct mlx5e_icosq *icosq) 2093 { 2094 clear_bit(MLX5E_SQ_STATE_ENABLED, &icosq->state); 2095 synchronize_net(); /* Sync with NAPI. */ 2096 } 2097 2098 static void mlx5e_close_icosq(struct mlx5e_icosq *sq) 2099 { 2100 struct mlx5e_channel *c = sq->channel; 2101 2102 if (sq->ktls_resync) 2103 mlx5e_ktls_rx_resync_destroy_resp_list(sq->ktls_resync); 2104 mlx5e_destroy_sq(c->mdev, sq->sqn); 2105 mlx5e_free_icosq_descs(sq); 2106 mlx5e_free_icosq(sq); 2107 } 2108 2109 int mlx5e_open_xdpsq(struct mlx5e_channel *c, struct mlx5e_params *params, 2110 struct mlx5e_sq_param *param, struct xsk_buff_pool *xsk_pool, 2111 struct mlx5e_xdpsq *sq, bool is_redirect) 2112 { 2113 struct mlx5e_create_sq_param csp = {}; 2114 int err; 2115 2116 err = mlx5e_alloc_xdpsq(c, params, xsk_pool, param, sq, is_redirect); 2117 if (err) 2118 return err; 2119 2120 csp.tis_lst_sz = 1; 2121 csp.tisn = mlx5e_profile_get_tisn(c->mdev, c->priv, c->priv->profile, 2122 c->lag_port, 0); /* tc = 0 */ 2123 csp.cqn = sq->cq.mcq.cqn; 2124 csp.wq_ctrl = &sq->wq_ctrl; 2125 csp.min_inline_mode = sq->min_inline_mode; 2126 csp.uar_page = c->bfreg->index; 2127 set_bit(MLX5E_SQ_STATE_ENABLED, &sq->state); 2128 2129 err = mlx5e_create_sq_rdy(c->mdev, param, &csp, 0, &sq->sqn); 2130 if (err) 2131 goto err_free_xdpsq; 2132 2133 mlx5e_set_xmit_fp(sq, param->is_mpw); 2134 2135 return 0; 2136 2137 err_free_xdpsq: 2138 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state); 2139 mlx5e_free_xdpsq(sq); 2140 2141 return err; 2142 } 2143 2144 void mlx5e_close_xdpsq(struct mlx5e_xdpsq *sq) 2145 { 2146 struct mlx5e_channel *c = sq->channel; 2147 2148 clear_bit(MLX5E_SQ_STATE_ENABLED, &sq->state); 2149 synchronize_net(); /* Sync with NAPI. */ 2150 2151 mlx5e_destroy_sq(c->mdev, sq->sqn); 2152 mlx5e_free_xdpsq_descs(sq); 2153 mlx5e_free_xdpsq(sq); 2154 } 2155 2156 static struct mlx5e_xdpsq *mlx5e_open_xdpredirect_sq(struct mlx5e_channel *c, 2157 struct mlx5e_params *params, 2158 struct mlx5e_channel_param *cparam, 2159 struct mlx5e_create_cq_param *ccp) 2160 { 2161 struct mlx5e_xdpsq *xdpsq; 2162 int err; 2163 2164 xdpsq = kvzalloc_node(sizeof(*xdpsq), GFP_KERNEL, cpu_to_node(c->cpu)); 2165 if (!xdpsq) 2166 return ERR_PTR(-ENOMEM); 2167 2168 err = mlx5e_open_cq(c->mdev, params->tx_cq_moderation, 2169 &cparam->xdp_sq.cqp, ccp, &xdpsq->cq); 2170 if (err) 2171 goto err_free_xdpsq; 2172 2173 err = mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, NULL, xdpsq, true); 2174 if (err) 2175 goto err_close_xdpsq_cq; 2176 2177 return xdpsq; 2178 2179 err_close_xdpsq_cq: 2180 mlx5e_close_cq(&xdpsq->cq); 2181 err_free_xdpsq: 2182 kvfree(xdpsq); 2183 2184 return ERR_PTR(err); 2185 } 2186 2187 static void mlx5e_close_xdpredirect_sq(struct mlx5e_xdpsq *xdpsq) 2188 { 2189 mlx5e_close_xdpsq(xdpsq); 2190 mlx5e_close_cq(&xdpsq->cq); 2191 kvfree(xdpsq); 2192 } 2193 2194 static int mlx5e_alloc_cq_common(struct mlx5_core_dev *mdev, 2195 struct net_device *netdev, 2196 struct workqueue_struct *workqueue, 2197 struct mlx5_uars_page *uar, 2198 struct mlx5e_cq_param *param, 2199 struct mlx5e_cq *cq) 2200 { 2201 struct mlx5_core_cq *mcq = &cq->mcq; 2202 int err; 2203 u32 i; 2204 2205 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq, 2206 &cq->wq_ctrl); 2207 if (err) 2208 return err; 2209 2210 mcq->cqe_sz = 64; 2211 mcq->set_ci_db = cq->wq_ctrl.db.db; 2212 mcq->arm_db = cq->wq_ctrl.db.db + 1; 2213 *mcq->set_ci_db = 0; 2214 mcq->vector = param->eq_ix; 2215 mcq->comp = mlx5e_completion_event; 2216 mcq->event = mlx5e_cq_error_event; 2217 2218 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) { 2219 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i); 2220 2221 cqe->op_own = 0xf1; 2222 cqe->validity_iteration_count = 0xff; 2223 } 2224 2225 cq->mdev = mdev; 2226 cq->netdev = netdev; 2227 cq->workqueue = workqueue; 2228 cq->uar = uar; 2229 2230 return 0; 2231 } 2232 2233 static int mlx5e_alloc_cq(struct mlx5_core_dev *mdev, 2234 struct mlx5e_cq_param *param, 2235 struct mlx5e_create_cq_param *ccp, 2236 struct mlx5e_cq *cq) 2237 { 2238 int err; 2239 2240 param->wq.buf_numa_node = ccp->node; 2241 param->wq.db_numa_node = ccp->node; 2242 param->eq_ix = ccp->ix; 2243 2244 err = mlx5e_alloc_cq_common(mdev, ccp->netdev, ccp->wq, 2245 ccp->uar, param, cq); 2246 2247 cq->napi = ccp->napi; 2248 cq->ch_stats = ccp->ch_stats; 2249 2250 return err; 2251 } 2252 2253 static void mlx5e_free_cq(struct mlx5e_cq *cq) 2254 { 2255 mlx5_wq_destroy(&cq->wq_ctrl); 2256 } 2257 2258 static int mlx5e_create_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param) 2259 { 2260 u32 out[MLX5_ST_SZ_DW(create_cq_out)]; 2261 struct mlx5_core_dev *mdev = cq->mdev; 2262 struct mlx5_core_cq *mcq = &cq->mcq; 2263 2264 void *in; 2265 void *cqc; 2266 int inlen; 2267 int eqn; 2268 int err; 2269 2270 err = mlx5_comp_eqn_get(mdev, param->eq_ix, &eqn); 2271 if (err) 2272 return err; 2273 2274 inlen = MLX5_ST_SZ_BYTES(create_cq_in) + 2275 sizeof(u64) * cq->wq_ctrl.buf.npages; 2276 in = kvzalloc(inlen, GFP_KERNEL); 2277 if (!in) 2278 return -ENOMEM; 2279 2280 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context); 2281 2282 memcpy(cqc, param->cqc, sizeof(param->cqc)); 2283 2284 mlx5_fill_page_frag_array(&cq->wq_ctrl.buf, 2285 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas)); 2286 2287 MLX5_SET(cqc, cqc, cq_period_mode, mlx5e_cq_period_mode(param->cq_period_mode)); 2288 2289 MLX5_SET(cqc, cqc, c_eqn_or_apu_element, eqn); 2290 MLX5_SET(cqc, cqc, uar_page, cq->uar->index); 2291 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift - 2292 MLX5_ADAPTER_PAGE_SHIFT); 2293 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma); 2294 2295 err = mlx5_core_create_cq(mdev, mcq, in, inlen, out, sizeof(out)); 2296 2297 kvfree(in); 2298 2299 if (err) 2300 return err; 2301 2302 mlx5e_cq_arm(cq); 2303 2304 return 0; 2305 } 2306 2307 static void mlx5e_destroy_cq(struct mlx5e_cq *cq) 2308 { 2309 mlx5_core_destroy_cq(cq->mdev, &cq->mcq); 2310 } 2311 2312 int mlx5e_open_cq(struct mlx5_core_dev *mdev, struct dim_cq_moder moder, 2313 struct mlx5e_cq_param *param, struct mlx5e_create_cq_param *ccp, 2314 struct mlx5e_cq *cq) 2315 { 2316 int err; 2317 2318 err = mlx5e_alloc_cq(mdev, param, ccp, cq); 2319 if (err) 2320 return err; 2321 2322 err = mlx5e_create_cq(cq, param); 2323 if (err) 2324 goto err_free_cq; 2325 2326 if (MLX5_CAP_GEN(mdev, cq_moderation) && 2327 MLX5_CAP_GEN(mdev, cq_period_mode_modify)) 2328 mlx5e_modify_cq_moderation(mdev, &cq->mcq, moder.usec, moder.pkts, 2329 mlx5e_cq_period_mode(moder.cq_period_mode)); 2330 return 0; 2331 2332 err_free_cq: 2333 mlx5e_free_cq(cq); 2334 2335 return err; 2336 } 2337 2338 void mlx5e_close_cq(struct mlx5e_cq *cq) 2339 { 2340 mlx5e_destroy_cq(cq); 2341 mlx5e_free_cq(cq); 2342 } 2343 2344 int mlx5e_modify_cq_period_mode(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq, 2345 u8 cq_period_mode) 2346 { 2347 u32 in[MLX5_ST_SZ_DW(modify_cq_in)] = {}; 2348 void *cqc; 2349 2350 MLX5_SET(modify_cq_in, in, cqn, cq->cqn); 2351 cqc = MLX5_ADDR_OF(modify_cq_in, in, cq_context); 2352 MLX5_SET(cqc, cqc, cq_period_mode, mlx5e_cq_period_mode(cq_period_mode)); 2353 MLX5_SET(modify_cq_in, in, 2354 modify_field_select_resize_field_select.modify_field_select.modify_field_select, 2355 MLX5_CQ_MODIFY_PERIOD_MODE); 2356 2357 return mlx5_core_modify_cq(dev, cq, in, sizeof(in)); 2358 } 2359 2360 int mlx5e_modify_cq_moderation(struct mlx5_core_dev *dev, struct mlx5_core_cq *cq, 2361 u16 cq_period, u16 cq_max_count, u8 cq_period_mode) 2362 { 2363 u32 in[MLX5_ST_SZ_DW(modify_cq_in)] = {}; 2364 void *cqc; 2365 2366 MLX5_SET(modify_cq_in, in, cqn, cq->cqn); 2367 cqc = MLX5_ADDR_OF(modify_cq_in, in, cq_context); 2368 MLX5_SET(cqc, cqc, cq_period, cq_period); 2369 MLX5_SET(cqc, cqc, cq_max_count, cq_max_count); 2370 MLX5_SET(cqc, cqc, cq_period_mode, cq_period_mode); 2371 MLX5_SET(modify_cq_in, in, 2372 modify_field_select_resize_field_select.modify_field_select.modify_field_select, 2373 MLX5_CQ_MODIFY_PERIOD | MLX5_CQ_MODIFY_COUNT | MLX5_CQ_MODIFY_PERIOD_MODE); 2374 2375 return mlx5_core_modify_cq(dev, cq, in, sizeof(in)); 2376 } 2377 2378 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c, 2379 struct mlx5e_params *params, 2380 struct mlx5e_create_cq_param *ccp, 2381 struct mlx5e_channel_param *cparam) 2382 { 2383 int err; 2384 int tc; 2385 2386 for (tc = 0; tc < c->num_tc; tc++) { 2387 err = mlx5e_open_cq(c->mdev, params->tx_cq_moderation, &cparam->txq_sq.cqp, 2388 ccp, &c->sq[tc].cq); 2389 if (err) 2390 goto err_close_tx_cqs; 2391 } 2392 2393 return 0; 2394 2395 err_close_tx_cqs: 2396 for (tc--; tc >= 0; tc--) 2397 mlx5e_close_cq(&c->sq[tc].cq); 2398 2399 return err; 2400 } 2401 2402 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c) 2403 { 2404 int tc; 2405 2406 for (tc = 0; tc < c->num_tc; tc++) 2407 mlx5e_close_cq(&c->sq[tc].cq); 2408 } 2409 2410 static int mlx5e_mqprio_txq_to_tc(struct netdev_tc_txq *tc_to_txq, unsigned int txq) 2411 { 2412 int tc; 2413 2414 for (tc = 0; tc < TC_MAX_QUEUE; tc++) 2415 if (txq - tc_to_txq[tc].offset < tc_to_txq[tc].count) 2416 return tc; 2417 2418 WARN(1, "Unexpected TCs configuration. No match found for txq %u", txq); 2419 return -ENOENT; 2420 } 2421 2422 static int mlx5e_txq_get_qos_node_hw_id(struct mlx5e_params *params, int txq_ix, 2423 u32 *hw_id) 2424 { 2425 int tc; 2426 2427 if (params->mqprio.mode != TC_MQPRIO_MODE_CHANNEL) { 2428 *hw_id = 0; 2429 return 0; 2430 } 2431 2432 tc = mlx5e_mqprio_txq_to_tc(params->mqprio.tc_to_txq, txq_ix); 2433 if (tc < 0) 2434 return tc; 2435 2436 if (tc >= params->mqprio.num_tc) { 2437 WARN(1, "Unexpected TCs configuration. tc %d is out of range of %u", 2438 tc, params->mqprio.num_tc); 2439 return -EINVAL; 2440 } 2441 2442 *hw_id = params->mqprio.channel.hw_id[tc]; 2443 return 0; 2444 } 2445 2446 static int mlx5e_open_sqs(struct mlx5e_channel *c, 2447 struct mlx5e_params *params, 2448 struct mlx5e_channel_param *cparam) 2449 { 2450 int err, tc; 2451 2452 for (tc = 0; tc < mlx5e_get_dcb_num_tc(params); tc++) { 2453 int txq_ix = c->ix + tc * params->num_channels; 2454 u32 qos_queue_group_id; 2455 u32 tisn; 2456 2457 tisn = mlx5e_profile_get_tisn(c->mdev, c->priv, c->priv->profile, 2458 c->lag_port, tc); 2459 err = mlx5e_txq_get_qos_node_hw_id(params, txq_ix, &qos_queue_group_id); 2460 if (err) 2461 goto err_close_sqs; 2462 2463 err = mlx5e_open_txqsq(c, tisn, txq_ix, 2464 params, &cparam->txq_sq, &c->sq[tc], tc, 2465 qos_queue_group_id, 2466 &c->priv->channel_stats[c->ix]->sq[tc]); 2467 if (err) 2468 goto err_close_sqs; 2469 } 2470 2471 return 0; 2472 2473 err_close_sqs: 2474 for (tc--; tc >= 0; tc--) 2475 mlx5e_close_txqsq(&c->sq[tc]); 2476 2477 return err; 2478 } 2479 2480 static void mlx5e_close_sqs(struct mlx5e_channel *c) 2481 { 2482 int tc; 2483 2484 for (tc = 0; tc < c->num_tc; tc++) 2485 mlx5e_close_txqsq(&c->sq[tc]); 2486 } 2487 2488 static int mlx5e_set_sq_maxrate(struct net_device *dev, 2489 struct mlx5e_txqsq *sq, u32 rate) 2490 { 2491 struct mlx5e_priv *priv = netdev_priv(dev); 2492 struct mlx5_core_dev *mdev = priv->mdev; 2493 struct mlx5e_modify_sq_param msp = {0}; 2494 struct mlx5_rate_limit rl = {0}; 2495 u16 rl_index = 0; 2496 int err; 2497 2498 if (rate == sq->rate_limit) 2499 /* nothing to do */ 2500 return 0; 2501 2502 if (sq->rate_limit) { 2503 rl.rate = sq->rate_limit; 2504 /* remove current rl index to free space to next ones */ 2505 mlx5_rl_remove_rate(mdev, &rl); 2506 } 2507 2508 sq->rate_limit = 0; 2509 2510 if (rate) { 2511 rl.rate = rate; 2512 err = mlx5_rl_add_rate(mdev, &rl_index, &rl); 2513 if (err) { 2514 netdev_err(dev, "Failed configuring rate %u: %d\n", 2515 rate, err); 2516 return err; 2517 } 2518 } 2519 2520 msp.curr_state = MLX5_SQC_STATE_RDY; 2521 msp.next_state = MLX5_SQC_STATE_RDY; 2522 msp.rl_index = rl_index; 2523 msp.rl_update = true; 2524 err = mlx5e_modify_sq(mdev, sq->sqn, &msp); 2525 if (err) { 2526 netdev_err(dev, "Failed configuring rate %u: %d\n", 2527 rate, err); 2528 /* remove the rate from the table */ 2529 if (rate) 2530 mlx5_rl_remove_rate(mdev, &rl); 2531 return err; 2532 } 2533 2534 sq->rate_limit = rate; 2535 return 0; 2536 } 2537 2538 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate) 2539 { 2540 struct mlx5e_priv *priv = netdev_priv(dev); 2541 struct mlx5_core_dev *mdev = priv->mdev; 2542 struct mlx5e_txqsq *sq = priv->txq2sq[index]; 2543 int err = 0; 2544 2545 if (!mlx5_rl_is_supported(mdev)) { 2546 netdev_err(dev, "Rate limiting is not supported on this device\n"); 2547 return -EINVAL; 2548 } 2549 2550 /* rate is given in Mb/sec, HW config is in Kb/sec */ 2551 rate = rate << 10; 2552 2553 /* Check whether rate in valid range, 0 is always valid */ 2554 if (rate && !mlx5_rl_is_in_range(mdev, rate)) { 2555 netdev_err(dev, "TX rate %u, is not in range\n", rate); 2556 return -ERANGE; 2557 } 2558 2559 mutex_lock(&priv->state_lock); 2560 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) 2561 err = mlx5e_set_sq_maxrate(dev, sq, rate); 2562 if (!err) 2563 priv->tx_rates[index] = rate; 2564 mutex_unlock(&priv->state_lock); 2565 2566 return err; 2567 } 2568 2569 static int mlx5e_open_rxq_rq(struct mlx5e_channel *c, 2570 struct mlx5e_params *params, 2571 struct mlx5e_rq_param *rq_param, 2572 struct mlx5e_rq_opt_param *rqo) 2573 { 2574 u16 q_counter = c->priv->q_counter[c->sd_ix]; 2575 int err; 2576 2577 err = mlx5e_init_rxq_rq(c, params, rq_param->xdp_frag_size, &c->rq); 2578 if (err) 2579 return err; 2580 2581 return mlx5e_open_rq(params, rq_param, rqo, cpu_to_node(c->cpu), 2582 q_counter, &c->rq); 2583 } 2584 2585 static struct mlx5e_icosq * 2586 mlx5e_open_async_icosq(struct mlx5e_channel *c, 2587 struct mlx5e_params *params, 2588 struct mlx5e_channel_param *cparam, 2589 struct mlx5e_create_cq_param *ccp) 2590 { 2591 struct dim_cq_moder icocq_moder = {0, 0}; 2592 struct mlx5e_icosq *async_icosq; 2593 int err; 2594 2595 async_icosq = kvzalloc_node(sizeof(*async_icosq), GFP_KERNEL, 2596 cpu_to_node(c->cpu)); 2597 if (!async_icosq) 2598 return ERR_PTR(-ENOMEM); 2599 2600 err = mlx5e_open_cq(c->mdev, icocq_moder, &cparam->async_icosq.cqp, ccp, 2601 &async_icosq->cq); 2602 if (err) 2603 goto err_free_async_icosq; 2604 2605 err = mlx5e_open_icosq(c, params, &cparam->async_icosq, async_icosq, 2606 mlx5e_async_icosq_err_cqe_work); 2607 if (err) 2608 goto err_close_async_icosq_cq; 2609 2610 return async_icosq; 2611 2612 err_close_async_icosq_cq: 2613 mlx5e_close_cq(&async_icosq->cq); 2614 err_free_async_icosq: 2615 kvfree(async_icosq); 2616 return ERR_PTR(err); 2617 } 2618 2619 static void mlx5e_close_async_icosq(struct mlx5e_icosq *async_icosq) 2620 { 2621 mlx5e_close_icosq(async_icosq); 2622 mlx5e_close_cq(&async_icosq->cq); 2623 kvfree(async_icosq); 2624 } 2625 2626 static int mlx5e_open_queues(struct mlx5e_channel *c, 2627 struct mlx5e_params *params, 2628 struct mlx5e_channel_param *cparam, 2629 bool async_icosq_needed) 2630 { 2631 const struct net_device_ops *netdev_ops = c->netdev->netdev_ops; 2632 struct dim_cq_moder icocq_moder = {0, 0}; 2633 struct mlx5e_create_cq_param ccp; 2634 int err; 2635 2636 mlx5e_build_create_cq_param(&ccp, c); 2637 2638 err = mlx5e_open_cq(c->mdev, icocq_moder, &cparam->icosq.cqp, &ccp, 2639 &c->icosq.cq); 2640 if (err) 2641 return err; 2642 2643 err = mlx5e_open_tx_cqs(c, params, &ccp, cparam); 2644 if (err) 2645 goto err_close_icosq_cq; 2646 2647 if (netdev_ops->ndo_xdp_xmit && c->xdp) { 2648 c->xdpsq = mlx5e_open_xdpredirect_sq(c, params, cparam, &ccp); 2649 if (IS_ERR(c->xdpsq)) { 2650 err = PTR_ERR(c->xdpsq); 2651 goto err_close_tx_cqs; 2652 } 2653 } 2654 2655 err = mlx5e_open_cq(c->mdev, params->rx_cq_moderation, &cparam->rq.cqp, &ccp, 2656 &c->rq.cq); 2657 if (err) 2658 goto err_close_xdpredirect_sq; 2659 2660 err = c->xdp ? mlx5e_open_cq(c->mdev, params->tx_cq_moderation, &cparam->xdp_sq.cqp, 2661 &ccp, &c->rq_xdpsq.cq) : 0; 2662 if (err) 2663 goto err_close_rx_cq; 2664 2665 if (async_icosq_needed) { 2666 c->async_icosq = mlx5e_open_async_icosq(c, params, cparam, 2667 &ccp); 2668 if (IS_ERR(c->async_icosq)) { 2669 err = PTR_ERR(c->async_icosq); 2670 goto err_close_rq_xdpsq_cq; 2671 } 2672 } 2673 2674 mutex_init(&c->icosq_recovery_lock); 2675 2676 err = mlx5e_open_icosq(c, params, &cparam->icosq, &c->icosq, 2677 mlx5e_icosq_err_cqe_work); 2678 if (err) 2679 goto err_close_async_icosq; 2680 2681 err = mlx5e_open_sqs(c, params, cparam); 2682 if (err) 2683 goto err_close_icosq; 2684 2685 err = mlx5e_open_rxq_rq(c, params, &cparam->rq, &cparam->rq_opt); 2686 if (err) 2687 goto err_close_sqs; 2688 2689 if (c->xdp) { 2690 err = mlx5e_open_xdpsq(c, params, &cparam->xdp_sq, NULL, 2691 &c->rq_xdpsq, false); 2692 if (err) 2693 goto err_close_rq; 2694 } 2695 2696 return 0; 2697 2698 err_close_rq: 2699 mlx5e_close_rq(&c->rq); 2700 2701 err_close_sqs: 2702 mlx5e_close_sqs(c); 2703 2704 err_close_icosq: 2705 mlx5e_close_icosq(&c->icosq); 2706 2707 err_close_async_icosq: 2708 if (c->async_icosq) 2709 mlx5e_close_async_icosq(c->async_icosq); 2710 2711 err_close_rq_xdpsq_cq: 2712 if (c->xdp) 2713 mlx5e_close_cq(&c->rq_xdpsq.cq); 2714 2715 err_close_rx_cq: 2716 mlx5e_close_cq(&c->rq.cq); 2717 2718 err_close_xdpredirect_sq: 2719 if (c->xdpsq) 2720 mlx5e_close_xdpredirect_sq(c->xdpsq); 2721 2722 err_close_tx_cqs: 2723 mlx5e_close_tx_cqs(c); 2724 2725 err_close_icosq_cq: 2726 mlx5e_close_cq(&c->icosq.cq); 2727 2728 return err; 2729 } 2730 2731 static void mlx5e_close_queues(struct mlx5e_channel *c) 2732 { 2733 if (c->xdp) 2734 mlx5e_close_xdpsq(&c->rq_xdpsq); 2735 /* The same ICOSQ is used for UMRs for both RQ and XSKRQ. */ 2736 cancel_work_sync(&c->icosq.recover_work); 2737 mlx5e_close_rq(&c->rq); 2738 mlx5e_close_sqs(c); 2739 mlx5e_close_icosq(&c->icosq); 2740 mutex_destroy(&c->icosq_recovery_lock); 2741 if (c->async_icosq) 2742 mlx5e_close_async_icosq(c->async_icosq); 2743 if (c->xdp) 2744 mlx5e_close_cq(&c->rq_xdpsq.cq); 2745 mlx5e_close_cq(&c->rq.cq); 2746 if (c->xdpsq) 2747 mlx5e_close_xdpredirect_sq(c->xdpsq); 2748 mlx5e_close_tx_cqs(c); 2749 mlx5e_close_cq(&c->icosq.cq); 2750 } 2751 2752 static u8 mlx5e_enumerate_lag_port(struct mlx5_core_dev *mdev, int ix) 2753 { 2754 u16 port_aff_bias = mlx5_core_is_pf(mdev) ? 0 : MLX5_CAP_GEN(mdev, vhca_id); 2755 2756 return (ix + port_aff_bias) % mlx5e_get_num_lag_ports(mdev); 2757 } 2758 2759 static int mlx5e_channel_stats_alloc(struct mlx5e_priv *priv, int ix, int cpu) 2760 { 2761 if (ix > priv->stats_nch) { 2762 netdev_warn(priv->netdev, "Unexpected channel stats index %d > %d\n", ix, 2763 priv->stats_nch); 2764 return -EINVAL; 2765 } 2766 2767 if (priv->channel_stats[ix]) 2768 return 0; 2769 2770 /* Asymmetric dynamic memory allocation. 2771 * Freed in mlx5e_priv_arrays_free, not on channel closure. 2772 */ 2773 netdev_dbg(priv->netdev, "Creating channel stats %d\n", ix); 2774 priv->channel_stats[ix] = kvzalloc_node(sizeof(**priv->channel_stats), 2775 GFP_KERNEL, cpu_to_node(cpu)); 2776 if (!priv->channel_stats[ix]) 2777 return -ENOMEM; 2778 mlx5e_stats_nch_write(priv, priv->stats_nch + 1); 2779 2780 return 0; 2781 } 2782 2783 void mlx5e_trigger_napi_icosq(struct mlx5e_channel *c) 2784 { 2785 struct mlx5e_icosq *sq = &c->icosq; 2786 bool locked; 2787 2788 set_bit(MLX5E_SQ_STATE_LOCK_NEEDED, &sq->state); 2789 synchronize_net(); 2790 2791 locked = mlx5e_icosq_sync_lock(sq); 2792 mlx5e_trigger_irq(sq); 2793 mlx5e_icosq_sync_unlock(sq, locked); 2794 2795 clear_bit(MLX5E_SQ_STATE_LOCK_NEEDED, &sq->state); 2796 } 2797 2798 void mlx5e_trigger_napi_async_icosq(struct mlx5e_channel *c) 2799 { 2800 struct mlx5e_icosq *sq = c->async_icosq; 2801 2802 spin_lock_bh(&sq->lock); 2803 mlx5e_trigger_irq(sq); 2804 spin_unlock_bh(&sq->lock); 2805 } 2806 2807 void mlx5e_trigger_napi_sched(struct napi_struct *napi) 2808 { 2809 local_bh_disable(); 2810 napi_schedule(napi); 2811 local_bh_enable(); 2812 } 2813 2814 static void mlx5e_channel_pick_doorbell(struct mlx5e_channel *c) 2815 { 2816 struct mlx5e_hw_objs *hw_objs = &c->mdev->mlx5e_res.hw_objs; 2817 2818 /* No dedicated Ethernet doorbells, use the global one. */ 2819 if (hw_objs->num_bfregs == 0) { 2820 c->bfreg = &c->mdev->priv.bfreg; 2821 return; 2822 } 2823 2824 /* Round-robin between doorbells. */ 2825 c->bfreg = hw_objs->bfregs + c->vec_ix % hw_objs->num_bfregs; 2826 } 2827 2828 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix, 2829 struct mlx5e_params *params, 2830 struct netdev_queue_config *qcfg, 2831 struct xsk_buff_pool *xsk_pool, 2832 struct mlx5e_channel **cp) 2833 { 2834 struct net_device *netdev = priv->netdev; 2835 struct mlx5e_channel_param *cparam; 2836 struct mlx5_core_dev *mdev; 2837 struct mlx5e_xsk_param xsk; 2838 bool async_icosq_needed; 2839 struct mlx5e_channel *c; 2840 unsigned int irq; 2841 int vec_ix; 2842 int cpu; 2843 int err; 2844 2845 mdev = mlx5_sd_ch_ix_get_dev(priv->mdev, ix); 2846 vec_ix = mlx5_sd_ch_ix_get_vec_ix(mdev, ix); 2847 cpu = mlx5_comp_vector_get_cpu(mdev, vec_ix); 2848 2849 err = mlx5_comp_irqn_get(mdev, vec_ix, &irq); 2850 if (err) 2851 return err; 2852 2853 err = mlx5e_channel_stats_alloc(priv, ix, cpu); 2854 if (err) 2855 return err; 2856 2857 c = kvzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu)); 2858 cparam = kvzalloc_obj(*cparam); 2859 if (!c || !cparam) { 2860 err = -ENOMEM; 2861 goto err_free; 2862 } 2863 2864 err = mlx5e_build_channel_param(mdev, params, qcfg, cparam); 2865 if (err) 2866 goto err_free; 2867 2868 c->priv = priv; 2869 c->mdev = mdev; 2870 c->ix = ix; 2871 c->vec_ix = vec_ix; 2872 c->sd_ix = mlx5_sd_ch_ix_get_dev_ix(mdev, ix); 2873 c->cpu = cpu; 2874 c->pdev = mlx5_core_dma_dev(mdev); 2875 c->netdev = priv->netdev; 2876 c->mkey_be = cpu_to_be32(mdev->mlx5e_res.hw_objs.mkey); 2877 c->num_tc = mlx5e_get_dcb_num_tc(params); 2878 c->xdp = !!params->xdp_prog; 2879 c->stats = &priv->channel_stats[ix]->ch; 2880 c->aff_mask = irq_get_effective_affinity_mask(irq); 2881 c->lag_port = mlx5e_enumerate_lag_port(mdev, ix); 2882 2883 mlx5e_channel_pick_doorbell(c); 2884 2885 netif_napi_add_config_locked(netdev, &c->napi, mlx5e_napi_poll, ix); 2886 netif_napi_set_irq_locked(&c->napi, irq); 2887 2888 async_icosq_needed = !!params->xdp_prog || priv->ktls_rx_was_enabled; 2889 err = mlx5e_open_queues(c, params, cparam, async_icosq_needed); 2890 if (unlikely(err)) 2891 goto err_napi_del; 2892 2893 if (xsk_pool) { 2894 mlx5e_build_xsk_param(xsk_pool, &xsk); 2895 mlx5e_build_xsk_channel_param(priv->mdev, params, &xsk, cparam); 2896 err = mlx5e_open_xsk(priv, params, cparam, xsk_pool, c); 2897 if (unlikely(err)) 2898 goto err_close_queues; 2899 } 2900 2901 *cp = c; 2902 2903 kvfree(cparam); 2904 return 0; 2905 2906 err_close_queues: 2907 mlx5e_close_queues(c); 2908 2909 err_napi_del: 2910 netif_napi_del_locked(&c->napi); 2911 2912 err_free: 2913 kvfree(cparam); 2914 kvfree(c); 2915 2916 return err; 2917 } 2918 2919 static void mlx5e_activate_channel(struct mlx5e_channel *c) 2920 { 2921 int tc; 2922 2923 napi_enable_locked(&c->napi); 2924 2925 for (tc = 0; tc < c->num_tc; tc++) 2926 mlx5e_activate_txqsq(&c->sq[tc]); 2927 mlx5e_activate_icosq(&c->icosq); 2928 if (c->async_icosq) 2929 mlx5e_activate_icosq(c->async_icosq); 2930 2931 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state)) 2932 mlx5e_activate_xsk(c); 2933 else 2934 mlx5e_activate_rq(&c->rq); 2935 2936 netif_queue_set_napi(c->netdev, c->ix, NETDEV_QUEUE_TYPE_RX, &c->napi); 2937 } 2938 2939 static void mlx5e_deactivate_channel(struct mlx5e_channel *c) 2940 { 2941 int tc; 2942 2943 netif_queue_set_napi(c->netdev, c->ix, NETDEV_QUEUE_TYPE_RX, NULL); 2944 2945 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state)) 2946 mlx5e_deactivate_xsk(c); 2947 else 2948 mlx5e_deactivate_rq(&c->rq); 2949 2950 if (c->async_icosq) 2951 mlx5e_deactivate_icosq(c->async_icosq); 2952 mlx5e_deactivate_icosq(&c->icosq); 2953 for (tc = 0; tc < c->num_tc; tc++) 2954 mlx5e_deactivate_txqsq(&c->sq[tc]); 2955 mlx5e_qos_deactivate_queues(c); 2956 2957 napi_disable_locked(&c->napi); 2958 } 2959 2960 static void mlx5e_close_channel(struct mlx5e_channel *c) 2961 { 2962 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state)) 2963 mlx5e_close_xsk(c); 2964 mlx5e_close_queues(c); 2965 mlx5e_qos_close_queues(c); 2966 netif_napi_del_locked(&c->napi); 2967 2968 kvfree(c); 2969 } 2970 2971 int mlx5e_open_channels(struct mlx5e_priv *priv, 2972 struct mlx5e_channels *chs) 2973 { 2974 int err = -ENOMEM; 2975 int i; 2976 2977 chs->num = chs->params.num_channels; 2978 2979 chs->c = kzalloc_objs(struct mlx5e_channel *, chs->num); 2980 if (!chs->c) 2981 goto err_out; 2982 2983 for (i = 0; i < chs->num; i++) { 2984 struct xsk_buff_pool *xsk_pool = NULL; 2985 2986 if (chs->params.xdp_prog) 2987 xsk_pool = mlx5e_xsk_get_pool(&chs->params, chs->params.xsk, i); 2988 2989 err = mlx5e_open_channel(priv, i, &chs->params, NULL, 2990 xsk_pool, &chs->c[i]); 2991 if (err) 2992 goto err_close_channels; 2993 } 2994 2995 if (MLX5E_GET_PFLAG(&chs->params, MLX5E_PFLAG_TX_PORT_TS) || chs->params.ptp_rx) { 2996 err = mlx5e_ptp_open(priv, &chs->params, chs->c[0]->lag_port, &chs->ptp); 2997 if (err) 2998 goto err_close_channels; 2999 } 3000 3001 if (priv->htb) { 3002 err = mlx5e_qos_open_queues(priv, chs); 3003 if (err) 3004 goto err_close_ptp; 3005 } 3006 3007 mlx5e_health_channels_update(priv); 3008 return 0; 3009 3010 err_close_ptp: 3011 if (chs->ptp) 3012 mlx5e_ptp_close(chs->ptp); 3013 3014 err_close_channels: 3015 for (i--; i >= 0; i--) 3016 mlx5e_close_channel(chs->c[i]); 3017 3018 kfree(chs->c); 3019 err_out: 3020 chs->num = 0; 3021 return err; 3022 } 3023 3024 static void mlx5e_activate_channels(struct mlx5e_priv *priv, struct mlx5e_channels *chs) 3025 { 3026 int i; 3027 3028 for (i = 0; i < chs->num; i++) 3029 mlx5e_activate_channel(chs->c[i]); 3030 3031 if (priv->htb) 3032 mlx5e_qos_activate_queues(priv); 3033 3034 for (i = 0; i < chs->num; i++) 3035 mlx5e_trigger_napi_icosq(chs->c[i]); 3036 3037 if (chs->ptp) 3038 mlx5e_ptp_activate_channel(chs->ptp); 3039 } 3040 3041 static int mlx5e_wait_channels_min_rx_wqes(struct mlx5e_channels *chs) 3042 { 3043 int err = 0; 3044 int i; 3045 3046 for (i = 0; i < chs->num; i++) { 3047 int timeout = err ? 0 : MLX5E_RQ_WQES_TIMEOUT; 3048 struct mlx5e_channel *c = chs->c[i]; 3049 3050 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state)) 3051 continue; 3052 3053 err |= mlx5e_wait_for_min_rx_wqes(&c->rq, timeout); 3054 3055 /* Don't wait on the XSK RQ, because the newer xdpsock sample 3056 * doesn't provide any Fill Ring entries at the setup stage. 3057 */ 3058 } 3059 3060 return err ? -ETIMEDOUT : 0; 3061 } 3062 3063 static void mlx5e_deactivate_channels(struct mlx5e_channels *chs) 3064 { 3065 int i; 3066 3067 if (chs->ptp) 3068 mlx5e_ptp_deactivate_channel(chs->ptp); 3069 3070 for (i = 0; i < chs->num; i++) 3071 mlx5e_deactivate_channel(chs->c[i]); 3072 } 3073 3074 void mlx5e_close_channels(struct mlx5e_channels *chs) 3075 { 3076 int i; 3077 3078 if (chs->ptp) { 3079 mlx5e_ptp_close(chs->ptp); 3080 chs->ptp = NULL; 3081 } 3082 for (i = 0; i < chs->num; i++) 3083 mlx5e_close_channel(chs->c[i]); 3084 3085 kfree(chs->c); 3086 chs->num = 0; 3087 } 3088 3089 static int mlx5e_modify_tirs_packet_merge(struct mlx5e_priv *priv) 3090 { 3091 struct mlx5e_rx_res *res = priv->rx_res; 3092 3093 return mlx5e_rx_res_packet_merge_set_param(res, &priv->channels.params.packet_merge); 3094 } 3095 3096 static MLX5E_DEFINE_PREACTIVATE_WRAPPER_CTX(mlx5e_modify_tirs_packet_merge); 3097 3098 static int mlx5e_set_mtu(struct mlx5_core_dev *mdev, 3099 struct mlx5e_params *params, u16 mtu) 3100 { 3101 u16 hw_mtu = MLX5E_SW2HW_MTU(params, mtu); 3102 int err; 3103 3104 err = mlx5_set_port_mtu(mdev, hw_mtu, 1); 3105 if (err) 3106 return err; 3107 3108 /* Update vport context MTU */ 3109 mlx5_modify_nic_vport_mtu(mdev, hw_mtu); 3110 return 0; 3111 } 3112 3113 static void mlx5e_query_mtu(struct mlx5_core_dev *mdev, 3114 struct mlx5e_params *params, u16 *mtu) 3115 { 3116 u16 hw_mtu = 0; 3117 int err; 3118 3119 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu); 3120 if (err || !hw_mtu) /* fallback to port oper mtu */ 3121 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1); 3122 3123 *mtu = MLX5E_HW2SW_MTU(params, hw_mtu); 3124 } 3125 3126 int mlx5e_set_dev_port_mtu(struct mlx5e_priv *priv) 3127 { 3128 struct mlx5e_params *params = &priv->channels.params; 3129 struct net_device *netdev = priv->netdev; 3130 struct mlx5_core_dev *mdev = priv->mdev; 3131 u16 mtu; 3132 int err; 3133 3134 err = mlx5e_set_mtu(mdev, params, params->sw_mtu); 3135 if (err) 3136 return err; 3137 3138 mlx5e_query_mtu(mdev, params, &mtu); 3139 if (mtu != params->sw_mtu) 3140 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n", 3141 __func__, mtu, params->sw_mtu); 3142 3143 params->sw_mtu = mtu; 3144 return 0; 3145 } 3146 3147 MLX5E_DEFINE_PREACTIVATE_WRAPPER_CTX(mlx5e_set_dev_port_mtu); 3148 3149 void mlx5e_set_netdev_mtu_boundaries(struct mlx5e_priv *priv) 3150 { 3151 struct mlx5e_params *params = &priv->channels.params; 3152 struct net_device *netdev = priv->netdev; 3153 struct mlx5_core_dev *mdev = priv->mdev; 3154 u16 max_mtu; 3155 3156 /* MTU range: 68 - hw-specific max */ 3157 netdev->min_mtu = ETH_MIN_MTU; 3158 3159 mlx5_query_port_max_mtu(mdev, &max_mtu, 1); 3160 netdev->max_mtu = min_t(unsigned int, MLX5E_HW2SW_MTU(params, max_mtu), 3161 ETH_MAX_MTU); 3162 } 3163 3164 static int mlx5e_netdev_set_tcs(struct net_device *netdev, u16 nch, u8 ntc, 3165 struct netdev_tc_txq *tc_to_txq) 3166 { 3167 int tc, err; 3168 3169 netdev_reset_tc(netdev); 3170 3171 if (ntc == 1) 3172 return 0; 3173 3174 err = netdev_set_num_tc(netdev, ntc); 3175 if (err) { 3176 netdev_WARN(netdev, "netdev_set_num_tc failed (%d), ntc = %d\n", err, ntc); 3177 return err; 3178 } 3179 3180 for (tc = 0; tc < ntc; tc++) { 3181 u16 count, offset; 3182 3183 count = tc_to_txq[tc].count; 3184 offset = tc_to_txq[tc].offset; 3185 netdev_set_tc_queue(netdev, tc, count, offset); 3186 } 3187 3188 return 0; 3189 } 3190 3191 int mlx5e_update_tx_netdev_queues(struct mlx5e_priv *priv) 3192 { 3193 int nch, ntc, num_txqs, err; 3194 int qos_queues = 0; 3195 3196 if (priv->htb) 3197 qos_queues = mlx5e_htb_cur_leaf_nodes(priv->htb); 3198 3199 nch = priv->channels.params.num_channels; 3200 ntc = mlx5e_get_dcb_num_tc(&priv->channels.params); 3201 num_txqs = nch * ntc + qos_queues; 3202 if (MLX5E_GET_PFLAG(&priv->channels.params, MLX5E_PFLAG_TX_PORT_TS)) 3203 num_txqs += ntc; 3204 3205 netdev_dbg(priv->netdev, "Setting num_txqs %d\n", num_txqs); 3206 err = netif_set_real_num_tx_queues(priv->netdev, num_txqs); 3207 if (err) 3208 netdev_warn(priv->netdev, "netif_set_real_num_tx_queues failed, %d\n", err); 3209 3210 return err; 3211 } 3212 3213 static void mlx5e_set_default_xps_cpumasks(struct mlx5e_priv *priv, 3214 struct mlx5e_params *params) 3215 { 3216 int ix; 3217 3218 for (ix = 0; ix < params->num_channels; ix++) { 3219 int num_comp_vectors, irq, vec_ix; 3220 struct mlx5_core_dev *mdev; 3221 3222 mdev = mlx5_sd_ch_ix_get_dev(priv->mdev, ix); 3223 num_comp_vectors = mlx5_comp_vectors_max(mdev); 3224 cpumask_clear(priv->scratchpad.cpumask); 3225 vec_ix = mlx5_sd_ch_ix_get_vec_ix(mdev, ix); 3226 3227 for (irq = vec_ix; irq < num_comp_vectors; irq += params->num_channels) { 3228 int cpu = mlx5_comp_vector_get_cpu(mdev, irq); 3229 3230 cpumask_set_cpu(cpu, priv->scratchpad.cpumask); 3231 } 3232 3233 netif_set_xps_queue(priv->netdev, priv->scratchpad.cpumask, ix); 3234 } 3235 } 3236 3237 static int mlx5e_update_tc_and_tx_queues(struct mlx5e_priv *priv) 3238 { 3239 struct netdev_tc_txq old_tc_to_txq[TC_MAX_QUEUE], *tc_to_txq; 3240 struct net_device *netdev = priv->netdev; 3241 int old_num_txqs, old_ntc; 3242 int nch, ntc; 3243 int err; 3244 int i; 3245 3246 old_num_txqs = netdev->real_num_tx_queues; 3247 old_ntc = netdev_get_num_tc(netdev) ? : 1; 3248 for (i = 0; i < ARRAY_SIZE(old_tc_to_txq); i++) 3249 old_tc_to_txq[i].combined = READ_ONCE(netdev->tc_to_txq[i].combined); 3250 3251 nch = priv->channels.params.num_channels; 3252 ntc = priv->channels.params.mqprio.num_tc; 3253 tc_to_txq = priv->channels.params.mqprio.tc_to_txq; 3254 3255 err = mlx5e_netdev_set_tcs(netdev, nch, ntc, tc_to_txq); 3256 if (err) 3257 goto err_out; 3258 err = mlx5e_update_tx_netdev_queues(priv); 3259 if (err) 3260 goto err_tcs; 3261 mlx5e_set_default_xps_cpumasks(priv, &priv->channels.params); 3262 3263 return 0; 3264 3265 err_tcs: 3266 WARN_ON_ONCE(mlx5e_netdev_set_tcs(netdev, old_num_txqs / old_ntc, old_ntc, 3267 old_tc_to_txq)); 3268 err_out: 3269 return err; 3270 } 3271 3272 MLX5E_DEFINE_PREACTIVATE_WRAPPER_CTX(mlx5e_update_tc_and_tx_queues); 3273 3274 static int mlx5e_num_channels_changed(struct mlx5e_priv *priv) 3275 { 3276 u16 count = priv->channels.params.num_channels; 3277 struct net_device *netdev = priv->netdev; 3278 int old_num_rxqs; 3279 int err; 3280 3281 old_num_rxqs = netdev->real_num_rx_queues; 3282 err = netif_set_real_num_rx_queues(netdev, count); 3283 if (err) { 3284 netdev_warn(netdev, "%s: netif_set_real_num_rx_queues failed, %d\n", 3285 __func__, err); 3286 return err; 3287 } 3288 err = mlx5e_update_tc_and_tx_queues(priv); 3289 if (err) { 3290 /* mlx5e_update_tc_and_tx_queues can fail if channels or TCs number increases. 3291 * Since channel number changed, it increased. That means, the call to 3292 * netif_set_real_num_rx_queues below should not fail, because it 3293 * decreases the number of RX queues. 3294 */ 3295 WARN_ON_ONCE(netif_set_real_num_rx_queues(netdev, old_num_rxqs)); 3296 return err; 3297 } 3298 3299 /* This function may be called on attach, before priv->rx_res is created. */ 3300 if (priv->rx_res) 3301 mlx5e_rx_res_rss_update_num_channels(priv->rx_res, count, 3302 netdev); 3303 3304 return 0; 3305 } 3306 3307 MLX5E_DEFINE_PREACTIVATE_WRAPPER_CTX(mlx5e_num_channels_changed); 3308 3309 static void mlx5e_build_txq_maps(struct mlx5e_priv *priv) 3310 { 3311 int i, ch, tc, num_tc; 3312 3313 ch = priv->channels.num; 3314 num_tc = mlx5e_get_dcb_num_tc(&priv->channels.params); 3315 3316 for (i = 0; i < ch; i++) { 3317 for (tc = 0; tc < num_tc; tc++) { 3318 struct mlx5e_channel *c = priv->channels.c[i]; 3319 struct mlx5e_txqsq *sq = &c->sq[tc]; 3320 3321 priv->txq2sq[sq->txq_ix] = sq; 3322 priv->txq2sq_stats[sq->txq_ix] = sq->stats; 3323 } 3324 } 3325 3326 if (!priv->channels.ptp) 3327 goto out; 3328 3329 if (!test_bit(MLX5E_PTP_STATE_TX, priv->channels.ptp->state)) 3330 goto out; 3331 3332 for (tc = 0; tc < num_tc; tc++) { 3333 struct mlx5e_ptp *c = priv->channels.ptp; 3334 struct mlx5e_txqsq *sq = &c->ptpsq[tc].txqsq; 3335 3336 priv->txq2sq[sq->txq_ix] = sq; 3337 priv->txq2sq_stats[sq->txq_ix] = sq->stats; 3338 } 3339 3340 out: 3341 /* Make the change to txq2sq visible before the queue is started. 3342 * As mlx5e_xmit runs under a spinlock, there is an implicit ACQUIRE, 3343 * which pairs with this barrier. 3344 */ 3345 smp_wmb(); 3346 } 3347 3348 void mlx5e_activate_priv_channels(struct mlx5e_priv *priv) 3349 { 3350 mlx5e_build_txq_maps(priv); 3351 mlx5e_activate_channels(priv, &priv->channels); 3352 mlx5e_xdp_tx_enable(priv); 3353 3354 /* dev_watchdog() wants all TX queues to be started when the carrier is 3355 * OK, including the ones in range real_num_tx_queues..num_tx_queues-1. 3356 * Make it happy to avoid TX timeout false alarms. 3357 */ 3358 netif_tx_start_all_queues(priv->netdev); 3359 3360 if (mlx5e_is_vport_rep(priv)) 3361 mlx5e_rep_activate_channels(priv); 3362 3363 set_bit(MLX5E_STATE_CHANNELS_ACTIVE, &priv->state); 3364 3365 mlx5e_wait_channels_min_rx_wqes(&priv->channels); 3366 3367 if (priv->rx_res) 3368 mlx5e_rx_res_channels_activate(priv->rx_res, &priv->channels); 3369 } 3370 3371 static void mlx5e_cancel_tx_timeout_work(struct mlx5e_priv *priv) 3372 { 3373 WARN_ON_ONCE(test_bit(MLX5E_STATE_CHANNELS_ACTIVE, &priv->state)); 3374 if (current_work() != &priv->tx_timeout_work) 3375 cancel_work_sync(&priv->tx_timeout_work); 3376 } 3377 3378 void mlx5e_deactivate_priv_channels(struct mlx5e_priv *priv) 3379 { 3380 if (priv->rx_res) 3381 mlx5e_rx_res_channels_deactivate(priv->rx_res); 3382 3383 clear_bit(MLX5E_STATE_CHANNELS_ACTIVE, &priv->state); 3384 mlx5e_cancel_tx_timeout_work(priv); 3385 3386 if (mlx5e_is_vport_rep(priv)) 3387 mlx5e_rep_deactivate_channels(priv); 3388 3389 /* The results of ndo_select_queue are unreliable, while netdev config 3390 * is being changed (real_num_tx_queues, num_tc). Stop all queues to 3391 * prevent ndo_start_xmit from being called, so that it can assume that 3392 * the selected queue is always valid. 3393 */ 3394 netif_tx_disable(priv->netdev); 3395 3396 mlx5e_xdp_tx_disable(priv); 3397 mlx5e_deactivate_channels(&priv->channels); 3398 } 3399 3400 static int mlx5e_switch_priv_params(struct mlx5e_priv *priv, 3401 struct mlx5e_params *new_params, 3402 mlx5e_fp_preactivate preactivate, 3403 void *context) 3404 { 3405 struct mlx5e_params old_params; 3406 3407 old_params = priv->channels.params; 3408 priv->channels.params = *new_params; 3409 3410 if (preactivate) { 3411 int err; 3412 3413 err = preactivate(priv, context); 3414 if (err) { 3415 priv->channels.params = old_params; 3416 return err; 3417 } 3418 } 3419 3420 mlx5e_set_xdp_feature(priv); 3421 return 0; 3422 } 3423 3424 /* The old and the new channel of the same index are bound to the same 3425 * XSK pool, which is lockless and expects a single user at a time. 3426 * Close the old XSK queues before the new ones are activated, even 3427 * when the closure of the rest of the old channels is deferred. 3428 */ 3429 static void mlx5e_close_channels_xsk(struct mlx5e_channels *chs) 3430 { 3431 int i; 3432 3433 for (i = 0; i < chs->num; i++) { 3434 struct mlx5e_channel *c = chs->c[i]; 3435 3436 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state)) 3437 mlx5e_close_xsk(c); 3438 } 3439 } 3440 3441 static int mlx5e_switch_priv_channels(struct mlx5e_priv *priv, 3442 struct mlx5e_channels *old_chs, 3443 struct mlx5e_channels *new_chs, 3444 mlx5e_fp_preactivate preactivate, 3445 void *context) 3446 { 3447 struct net_device *netdev = priv->netdev; 3448 int carrier_ok; 3449 int err = 0; 3450 3451 carrier_ok = netif_carrier_ok(netdev); 3452 netif_carrier_off(netdev); 3453 3454 mlx5e_deactivate_priv_channels(priv); 3455 3456 priv->channels = *new_chs; 3457 3458 /* New channels are ready to roll, call the preactivate hook if needed 3459 * to modify HW settings or update kernel parameters. 3460 */ 3461 if (preactivate) { 3462 err = preactivate(priv, context); 3463 if (err) { 3464 priv->channels = *old_chs; 3465 goto out; 3466 } 3467 } 3468 3469 mlx5e_set_xdp_feature(priv); 3470 if (!MLX5_CAP_GEN(priv->mdev, tis_tir_td_order)) 3471 mlx5e_close_channels(old_chs); 3472 else 3473 mlx5e_close_channels_xsk(old_chs); 3474 priv->profile->update_rx(priv); 3475 3476 mlx5e_selq_apply(&priv->selq); 3477 out: 3478 mlx5e_activate_priv_channels(priv); 3479 3480 /* return carrier back if needed */ 3481 if (carrier_ok) 3482 netif_carrier_on(netdev); 3483 3484 return err; 3485 } 3486 3487 int mlx5e_safe_switch_params(struct mlx5e_priv *priv, 3488 struct mlx5e_params *params, 3489 mlx5e_fp_preactivate preactivate, 3490 void *context, bool reset) 3491 { 3492 struct mlx5e_channels *old_chs, *new_chs; 3493 int err; 3494 3495 reset &= test_bit(MLX5E_STATE_OPENED, &priv->state); 3496 if (!reset) 3497 return mlx5e_switch_priv_params(priv, params, preactivate, context); 3498 3499 old_chs = kzalloc_obj(*old_chs); 3500 new_chs = kzalloc_obj(*new_chs); 3501 if (!old_chs || !new_chs) { 3502 err = -ENOMEM; 3503 goto err_free_chs; 3504 } 3505 3506 new_chs->params = *params; 3507 3508 mlx5e_selq_prepare_params(&priv->selq, &new_chs->params); 3509 3510 err = mlx5e_open_channels(priv, new_chs); 3511 if (err) 3512 goto err_cancel_selq; 3513 3514 *old_chs = priv->channels; 3515 3516 err = mlx5e_switch_priv_channels(priv, old_chs, new_chs, 3517 preactivate, context); 3518 if (err) 3519 goto err_close; 3520 3521 if (MLX5_CAP_GEN(priv->mdev, tis_tir_td_order)) 3522 mlx5e_close_channels(old_chs); 3523 3524 kfree(new_chs); 3525 kfree(old_chs); 3526 return 0; 3527 3528 err_close: 3529 mlx5e_close_channels(new_chs); 3530 3531 err_cancel_selq: 3532 mlx5e_selq_cancel(&priv->selq); 3533 err_free_chs: 3534 kfree(new_chs); 3535 kfree(old_chs); 3536 return err; 3537 } 3538 3539 int mlx5e_safe_reopen_channels(struct mlx5e_priv *priv) 3540 { 3541 return mlx5e_safe_switch_params(priv, &priv->channels.params, NULL, NULL, true); 3542 } 3543 3544 void mlx5e_timestamp_init(struct mlx5e_priv *priv) 3545 { 3546 priv->hwtstamp_config.tx_type = HWTSTAMP_TX_OFF; 3547 priv->hwtstamp_config.rx_filter = HWTSTAMP_FILTER_NONE; 3548 } 3549 3550 static void mlx5e_modify_admin_state(struct mlx5_core_dev *mdev, 3551 enum mlx5_port_status state) 3552 { 3553 struct mlx5_eswitch *esw = mdev->priv.eswitch; 3554 int vport_admin_state; 3555 3556 mlx5_set_port_admin_status(mdev, state); 3557 3558 if (mlx5_eswitch_mode(mdev) == MLX5_ESWITCH_OFFLOADS || 3559 !MLX5_CAP_GEN(mdev, uplink_follow)) 3560 return; 3561 3562 if (state == MLX5_PORT_UP) 3563 vport_admin_state = MLX5_VPORT_ADMIN_STATE_AUTO; 3564 else 3565 vport_admin_state = MLX5_VPORT_ADMIN_STATE_DOWN; 3566 3567 mlx5_eswitch_set_vport_state(esw, MLX5_VPORT_UPLINK, vport_admin_state); 3568 } 3569 3570 int mlx5e_open_locked(struct net_device *netdev) 3571 { 3572 struct mlx5e_priv *priv = netdev_priv(netdev); 3573 int err; 3574 3575 mlx5e_selq_prepare_params(&priv->selq, &priv->channels.params); 3576 3577 set_bit(MLX5E_STATE_OPENED, &priv->state); 3578 3579 err = mlx5e_open_channels(priv, &priv->channels); 3580 if (err) 3581 goto err_clear_state_opened_flag; 3582 3583 err = priv->profile->update_rx(priv); 3584 if (err) 3585 goto err_close_channels; 3586 3587 mlx5e_selq_apply(&priv->selq); 3588 mlx5e_activate_priv_channels(priv); 3589 mlx5e_apply_traps(priv, true); 3590 if (priv->profile->update_carrier) 3591 priv->profile->update_carrier(priv); 3592 3593 mlx5e_queue_update_stats(priv); 3594 return 0; 3595 3596 err_close_channels: 3597 mlx5e_close_channels(&priv->channels); 3598 err_clear_state_opened_flag: 3599 clear_bit(MLX5E_STATE_OPENED, &priv->state); 3600 mlx5e_selq_cancel(&priv->selq); 3601 return err; 3602 } 3603 3604 int mlx5e_open(struct net_device *netdev) 3605 { 3606 struct mlx5e_priv *priv = netdev_priv(netdev); 3607 int err; 3608 3609 mutex_lock(&priv->state_lock); 3610 err = mlx5e_open_locked(netdev); 3611 if (!err) 3612 mlx5e_modify_admin_state(priv->mdev, MLX5_PORT_UP); 3613 mutex_unlock(&priv->state_lock); 3614 3615 return err; 3616 } 3617 3618 int mlx5e_close_locked(struct net_device *netdev) 3619 { 3620 struct mlx5e_priv *priv = netdev_priv(netdev); 3621 3622 /* May already be CLOSED in case a previous configuration operation 3623 * (e.g RX/TX queue size change) that involves close&open failed. 3624 */ 3625 if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) 3626 return 0; 3627 3628 mlx5e_apply_traps(priv, false); 3629 clear_bit(MLX5E_STATE_OPENED, &priv->state); 3630 3631 netif_carrier_off(priv->netdev); 3632 mlx5e_deactivate_priv_channels(priv); 3633 mlx5e_close_channels(&priv->channels); 3634 3635 return 0; 3636 } 3637 3638 int mlx5e_close(struct net_device *netdev) 3639 { 3640 struct mlx5e_priv *priv = netdev_priv(netdev); 3641 int err; 3642 3643 if (!netif_device_present(netdev)) 3644 return -ENODEV; 3645 3646 mutex_lock(&priv->state_lock); 3647 mlx5e_modify_admin_state(priv->mdev, MLX5_PORT_DOWN); 3648 err = mlx5e_close_locked(netdev); 3649 mutex_unlock(&priv->state_lock); 3650 3651 return err; 3652 } 3653 3654 static void mlx5e_free_drop_rq(struct mlx5e_rq *rq) 3655 { 3656 mlx5_wq_destroy(&rq->wq_ctrl); 3657 } 3658 3659 static int mlx5e_alloc_drop_rq(struct mlx5_core_dev *mdev, 3660 struct mlx5e_rq *rq, 3661 struct mlx5e_rq_param *rq_param) 3662 { 3663 void *rqc_wq = MLX5_ADDR_OF(rqc, rq_param->rqc, wq); 3664 int err; 3665 3666 rq_param->wq.db_numa_node = rq_param->wq.buf_numa_node; 3667 3668 err = mlx5_wq_cyc_create(mdev, &rq_param->wq, rqc_wq, &rq->wqe.wq, 3669 &rq->wq_ctrl); 3670 if (err) 3671 return err; 3672 3673 /* Mark as unused given "Drop-RQ" packets never reach XDP */ 3674 xdp_rxq_info_unused(&rq->xdp_rxq); 3675 3676 rq->mdev = mdev; 3677 3678 return 0; 3679 } 3680 3681 static int mlx5e_alloc_drop_cq(struct mlx5e_priv *priv, 3682 struct mlx5e_cq *cq, 3683 struct mlx5e_cq_param *param) 3684 { 3685 struct mlx5_core_dev *mdev = priv->mdev; 3686 3687 param->wq.buf_numa_node = dev_to_node(mlx5_core_dma_dev(mdev)); 3688 param->wq.db_numa_node = dev_to_node(mlx5_core_dma_dev(mdev)); 3689 3690 return mlx5e_alloc_cq_common(priv->mdev, priv->netdev, priv->wq, 3691 mdev->priv.bfreg.up, param, cq); 3692 } 3693 3694 int mlx5e_open_drop_rq(struct mlx5e_priv *priv, 3695 struct mlx5e_rq *drop_rq) 3696 { 3697 struct mlx5_core_dev *mdev = priv->mdev; 3698 struct mlx5e_cq_param cq_param = {}; 3699 struct mlx5e_rq_param rq_param = {}; 3700 struct mlx5e_cq *cq = &drop_rq->cq; 3701 int err; 3702 3703 mlx5e_build_drop_rq_param(mdev, &rq_param); 3704 3705 err = mlx5e_alloc_drop_cq(priv, cq, &cq_param); 3706 if (err) 3707 return err; 3708 3709 err = mlx5e_create_cq(cq, &cq_param); 3710 if (err) 3711 goto err_free_cq; 3712 3713 err = mlx5e_alloc_drop_rq(mdev, drop_rq, &rq_param); 3714 if (err) 3715 goto err_destroy_cq; 3716 3717 err = mlx5e_create_rq(drop_rq, &rq_param, priv->drop_rq_q_counter); 3718 if (err) 3719 goto err_free_rq; 3720 3721 err = mlx5e_modify_rq_state(drop_rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY); 3722 if (err) 3723 mlx5_core_warn(priv->mdev, "modify_rq_state failed, rx_if_down_packets won't be counted %d\n", err); 3724 3725 return 0; 3726 3727 err_free_rq: 3728 mlx5e_free_drop_rq(drop_rq); 3729 3730 err_destroy_cq: 3731 mlx5e_destroy_cq(cq); 3732 3733 err_free_cq: 3734 mlx5e_free_cq(cq); 3735 3736 return err; 3737 } 3738 3739 void mlx5e_close_drop_rq(struct mlx5e_rq *drop_rq) 3740 { 3741 mlx5e_destroy_rq(drop_rq); 3742 mlx5e_free_drop_rq(drop_rq); 3743 mlx5e_destroy_cq(&drop_rq->cq); 3744 mlx5e_free_cq(&drop_rq->cq); 3745 } 3746 3747 static void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv) 3748 { 3749 if (priv->mqprio_rl) { 3750 mlx5e_mqprio_rl_cleanup(priv->mqprio_rl); 3751 mlx5e_mqprio_rl_free(priv->mqprio_rl); 3752 priv->mqprio_rl = NULL; 3753 } 3754 mlx5e_accel_cleanup_tx(priv); 3755 } 3756 3757 static int mlx5e_modify_channels_vsd(struct mlx5e_channels *chs, bool vsd) 3758 { 3759 int err; 3760 int i; 3761 3762 for (i = 0; i < chs->num; i++) { 3763 err = mlx5e_modify_rq_vsd(&chs->c[i]->rq, vsd); 3764 if (err) 3765 return err; 3766 } 3767 if (chs->ptp && test_bit(MLX5E_PTP_STATE_RX, chs->ptp->state)) 3768 return mlx5e_modify_rq_vsd(&chs->ptp->rq, vsd); 3769 3770 return 0; 3771 } 3772 3773 static void mlx5e_mqprio_build_default_tc_to_txq(struct netdev_tc_txq *tc_to_txq, 3774 int ntc, int nch) 3775 { 3776 int tc; 3777 3778 memset(tc_to_txq, 0, sizeof(*tc_to_txq) * TC_MAX_QUEUE); 3779 3780 /* Map netdev TCs to offset 0. 3781 * We have our own UP to TXQ mapping for DCB mode of QoS 3782 */ 3783 for (tc = 0; tc < ntc; tc++) { 3784 tc_to_txq[tc] = (struct netdev_tc_txq) { 3785 .count = nch, 3786 .offset = 0, 3787 }; 3788 } 3789 } 3790 3791 static void mlx5e_mqprio_build_tc_to_txq(struct netdev_tc_txq *tc_to_txq, 3792 struct tc_mqprio_qopt *qopt) 3793 { 3794 int tc; 3795 3796 for (tc = 0; tc < TC_MAX_QUEUE; tc++) { 3797 tc_to_txq[tc] = (struct netdev_tc_txq) { 3798 .count = qopt->count[tc], 3799 .offset = qopt->offset[tc], 3800 }; 3801 } 3802 } 3803 3804 static void mlx5e_params_mqprio_dcb_set(struct mlx5e_params *params, u8 num_tc) 3805 { 3806 params->mqprio.mode = TC_MQPRIO_MODE_DCB; 3807 params->mqprio.num_tc = num_tc; 3808 mlx5e_mqprio_build_default_tc_to_txq(params->mqprio.tc_to_txq, num_tc, 3809 params->num_channels); 3810 } 3811 3812 static void mlx5e_mqprio_rl_update_params(struct mlx5e_params *params, 3813 struct mlx5e_mqprio_rl *rl) 3814 { 3815 int tc; 3816 3817 for (tc = 0; tc < TC_MAX_QUEUE; tc++) { 3818 u32 hw_id = 0; 3819 3820 if (rl) 3821 mlx5e_mqprio_rl_get_node_hw_id(rl, tc, &hw_id); 3822 params->mqprio.channel.hw_id[tc] = hw_id; 3823 } 3824 } 3825 3826 static void mlx5e_params_mqprio_channel_set(struct mlx5e_params *params, 3827 struct tc_mqprio_qopt_offload *mqprio, 3828 struct mlx5e_mqprio_rl *rl) 3829 { 3830 int tc; 3831 3832 params->mqprio.mode = TC_MQPRIO_MODE_CHANNEL; 3833 params->mqprio.num_tc = mqprio->qopt.num_tc; 3834 3835 for (tc = 0; tc < TC_MAX_QUEUE; tc++) 3836 params->mqprio.channel.max_rate[tc] = mqprio->max_rate[tc]; 3837 3838 mlx5e_mqprio_rl_update_params(params, rl); 3839 mlx5e_mqprio_build_tc_to_txq(params->mqprio.tc_to_txq, &mqprio->qopt); 3840 } 3841 3842 static void mlx5e_params_mqprio_reset(struct mlx5e_params *params) 3843 { 3844 mlx5e_params_mqprio_dcb_set(params, 1); 3845 } 3846 3847 static int mlx5e_setup_tc_mqprio_dcb(struct mlx5e_priv *priv, 3848 struct tc_mqprio_qopt *mqprio) 3849 { 3850 struct mlx5e_params new_params; 3851 u8 tc = mqprio->num_tc; 3852 int err; 3853 3854 mqprio->hw = TC_MQPRIO_HW_OFFLOAD_TCS; 3855 3856 if (tc && tc != MLX5_MAX_NUM_TC) 3857 return -EINVAL; 3858 3859 new_params = priv->channels.params; 3860 mlx5e_params_mqprio_dcb_set(&new_params, tc ? tc : 1); 3861 3862 err = mlx5e_safe_switch_params(priv, &new_params, 3863 mlx5e_update_tc_and_tx_queues_ctx, NULL, true); 3864 3865 if (!err && priv->mqprio_rl) { 3866 mlx5e_mqprio_rl_cleanup(priv->mqprio_rl); 3867 mlx5e_mqprio_rl_free(priv->mqprio_rl); 3868 priv->mqprio_rl = NULL; 3869 } 3870 3871 priv->max_opened_tc = max_t(u8, priv->max_opened_tc, 3872 mlx5e_get_dcb_num_tc(&priv->channels.params)); 3873 return err; 3874 } 3875 3876 static int mlx5e_mqprio_channel_validate(struct mlx5e_priv *priv, 3877 struct tc_mqprio_qopt_offload *mqprio) 3878 { 3879 struct net_device *netdev = priv->netdev; 3880 struct mlx5e_ptp *ptp_channel; 3881 int agg_count = 0; 3882 int i; 3883 3884 ptp_channel = priv->channels.ptp; 3885 if (ptp_channel && test_bit(MLX5E_PTP_STATE_TX, ptp_channel->state)) { 3886 netdev_err(netdev, 3887 "Cannot activate MQPRIO mode channel since it conflicts with TX port TS\n"); 3888 return -EINVAL; 3889 } 3890 3891 if (mqprio->qopt.offset[0] != 0 || mqprio->qopt.num_tc < 1 || 3892 mqprio->qopt.num_tc > MLX5E_MAX_NUM_MQPRIO_CH_TC) 3893 return -EINVAL; 3894 3895 for (i = 0; i < mqprio->qopt.num_tc; i++) { 3896 if (!mqprio->qopt.count[i]) { 3897 netdev_err(netdev, "Zero size for queue-group (%d) is not supported\n", i); 3898 return -EINVAL; 3899 } 3900 if (mqprio->min_rate[i]) { 3901 netdev_err(netdev, "Min tx rate is not supported\n"); 3902 return -EINVAL; 3903 } 3904 3905 if (mqprio->max_rate[i]) { 3906 int err; 3907 3908 err = mlx5e_qos_bytes_rate_check(priv->mdev, mqprio->max_rate[i]); 3909 if (err) 3910 return err; 3911 } 3912 3913 if (mqprio->qopt.offset[i] != agg_count) { 3914 netdev_err(netdev, "Discontinuous queues config is not supported\n"); 3915 return -EINVAL; 3916 } 3917 agg_count += mqprio->qopt.count[i]; 3918 } 3919 3920 if (priv->channels.params.num_channels != agg_count) { 3921 netdev_err(netdev, "Num of queues (%d) does not match available (%d)\n", 3922 agg_count, priv->channels.params.num_channels); 3923 return -EINVAL; 3924 } 3925 3926 return 0; 3927 } 3928 3929 static bool mlx5e_mqprio_rate_limit(u8 num_tc, u64 max_rate[]) 3930 { 3931 int tc; 3932 3933 for (tc = 0; tc < num_tc; tc++) 3934 if (max_rate[tc]) 3935 return true; 3936 return false; 3937 } 3938 3939 static struct mlx5e_mqprio_rl *mlx5e_mqprio_rl_create(struct mlx5_core_dev *mdev, 3940 u8 num_tc, u64 max_rate[]) 3941 { 3942 struct mlx5e_mqprio_rl *rl; 3943 int err; 3944 3945 if (!mlx5e_mqprio_rate_limit(num_tc, max_rate)) 3946 return NULL; 3947 3948 rl = mlx5e_mqprio_rl_alloc(); 3949 if (!rl) 3950 return ERR_PTR(-ENOMEM); 3951 3952 err = mlx5e_mqprio_rl_init(rl, mdev, num_tc, max_rate); 3953 if (err) { 3954 mlx5e_mqprio_rl_free(rl); 3955 return ERR_PTR(err); 3956 } 3957 3958 return rl; 3959 } 3960 3961 static int mlx5e_setup_tc_mqprio_channel(struct mlx5e_priv *priv, 3962 struct tc_mqprio_qopt_offload *mqprio) 3963 { 3964 struct mlx5e_params new_params; 3965 struct mlx5e_mqprio_rl *rl; 3966 int err; 3967 3968 err = mlx5e_mqprio_channel_validate(priv, mqprio); 3969 if (err) 3970 return err; 3971 3972 rl = mlx5e_mqprio_rl_create(priv->mdev, mqprio->qopt.num_tc, mqprio->max_rate); 3973 if (IS_ERR(rl)) 3974 return PTR_ERR(rl); 3975 3976 new_params = priv->channels.params; 3977 mlx5e_params_mqprio_channel_set(&new_params, mqprio, rl); 3978 3979 err = mlx5e_safe_switch_params(priv, &new_params, 3980 mlx5e_update_tc_and_tx_queues_ctx, NULL, true); 3981 if (err) { 3982 if (rl) { 3983 mlx5e_mqprio_rl_cleanup(rl); 3984 mlx5e_mqprio_rl_free(rl); 3985 } 3986 return err; 3987 } 3988 3989 if (priv->mqprio_rl) { 3990 mlx5e_mqprio_rl_cleanup(priv->mqprio_rl); 3991 mlx5e_mqprio_rl_free(priv->mqprio_rl); 3992 } 3993 priv->mqprio_rl = rl; 3994 3995 return 0; 3996 } 3997 3998 static int mlx5e_setup_tc_mqprio(struct mlx5e_priv *priv, 3999 struct tc_mqprio_qopt_offload *mqprio) 4000 { 4001 /* MQPRIO is another toplevel qdisc that can't be attached 4002 * simultaneously with the offloaded HTB. 4003 */ 4004 if (mlx5e_selq_is_htb_enabled(&priv->selq)) { 4005 NL_SET_ERR_MSG_MOD(mqprio->extack, 4006 "MQPRIO cannot be configured when HTB offload is enabled."); 4007 return -EOPNOTSUPP; 4008 } 4009 4010 switch (mqprio->mode) { 4011 case TC_MQPRIO_MODE_DCB: 4012 return mlx5e_setup_tc_mqprio_dcb(priv, &mqprio->qopt); 4013 case TC_MQPRIO_MODE_CHANNEL: 4014 return mlx5e_setup_tc_mqprio_channel(priv, mqprio); 4015 default: 4016 return -EOPNOTSUPP; 4017 } 4018 } 4019 4020 static LIST_HEAD(mlx5e_block_cb_list); 4021 4022 static int mlx5e_setup_tc(struct net_device *dev, enum tc_setup_type type, 4023 void *type_data) 4024 { 4025 struct mlx5e_priv *priv = netdev_priv(dev); 4026 bool tc_unbind = false; 4027 int err; 4028 4029 if (type == TC_SETUP_BLOCK && 4030 ((struct flow_block_offload *)type_data)->command == FLOW_BLOCK_UNBIND) 4031 tc_unbind = true; 4032 4033 if (!netif_device_present(dev) && !tc_unbind) 4034 return -ENODEV; 4035 4036 switch (type) { 4037 case TC_SETUP_BLOCK: { 4038 struct flow_block_offload *f = type_data; 4039 4040 f->unlocked_driver_cb = true; 4041 return flow_block_cb_setup_simple(type_data, 4042 &mlx5e_block_cb_list, 4043 mlx5e_setup_tc_block_cb, 4044 priv, priv, true); 4045 } 4046 case TC_SETUP_QDISC_MQPRIO: 4047 mutex_lock(&priv->state_lock); 4048 err = mlx5e_setup_tc_mqprio(priv, type_data); 4049 mutex_unlock(&priv->state_lock); 4050 return err; 4051 case TC_SETUP_QDISC_HTB: 4052 mutex_lock(&priv->state_lock); 4053 err = mlx5e_htb_setup_tc(priv, type_data); 4054 mutex_unlock(&priv->state_lock); 4055 return err; 4056 default: 4057 return -EOPNOTSUPP; 4058 } 4059 } 4060 4061 void mlx5e_fold_sw_stats64(struct mlx5e_priv *priv, struct rtnl_link_stats64 *s) 4062 { 4063 u16 nch = mlx5e_stats_nch_read(priv); 4064 int i; 4065 4066 for (i = 0; i < nch; i++) { 4067 struct mlx5e_channel_stats *channel_stats = priv->channel_stats[i]; 4068 struct mlx5e_rq_stats *xskrq_stats = &channel_stats->xskrq; 4069 struct mlx5e_rq_stats *rq_stats = &channel_stats->rq; 4070 int j; 4071 4072 s->rx_packets += rq_stats->packets + xskrq_stats->packets; 4073 s->rx_bytes += rq_stats->bytes + xskrq_stats->bytes; 4074 s->multicast += rq_stats->mcast_packets + xskrq_stats->mcast_packets; 4075 4076 for (j = 0; j < priv->max_opened_tc; j++) { 4077 struct mlx5e_sq_stats *sq_stats = &channel_stats->sq[j]; 4078 4079 s->tx_packets += sq_stats->packets; 4080 s->tx_bytes += sq_stats->bytes; 4081 s->tx_dropped += sq_stats->dropped; 4082 } 4083 } 4084 if (priv->tx_ptp_opened) { 4085 for (i = 0; i < priv->max_opened_tc; i++) { 4086 struct mlx5e_sq_stats *sq_stats = &priv->ptp_stats.sq[i]; 4087 4088 s->tx_packets += sq_stats->packets; 4089 s->tx_bytes += sq_stats->bytes; 4090 s->tx_dropped += sq_stats->dropped; 4091 } 4092 } 4093 if (priv->rx_ptp_opened) { 4094 struct mlx5e_rq_stats *rq_stats = &priv->ptp_stats.rq; 4095 4096 s->rx_packets += rq_stats->packets; 4097 s->rx_bytes += rq_stats->bytes; 4098 s->multicast += rq_stats->mcast_packets; 4099 } 4100 4101 #ifdef CONFIG_MLX5_EN_PSP 4102 if (priv->psp) 4103 s->tx_dropped += atomic_read(&priv->psp->tx_drop); 4104 #endif 4105 } 4106 4107 void 4108 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats) 4109 { 4110 struct mlx5e_priv *priv = netdev_priv(dev); 4111 struct mlx5e_pport_stats *pstats = &priv->stats.pport; 4112 4113 if (!netif_device_present(dev)) 4114 return; 4115 4116 /* In switchdev mode, monitor counters doesn't monitor 4117 * rx/tx stats of 802_3. The update stats mechanism 4118 * should keep the 802_3 layout counters updated 4119 */ 4120 if (!mlx5e_monitor_counter_supported(priv) || 4121 mlx5e_is_uplink_rep(priv)) { 4122 /* update HW stats in background for next time */ 4123 mlx5e_queue_update_stats(priv); 4124 } 4125 4126 netdev_stats_to_stats64(stats, &dev->stats); 4127 4128 if (mlx5e_is_uplink_rep(priv)) { 4129 struct mlx5e_vport_stats *vstats = &priv->stats.vport; 4130 4131 stats->rx_packets = PPORT_802_3_GET(pstats, a_frames_received_ok); 4132 stats->rx_bytes = PPORT_802_3_GET(pstats, a_octets_received_ok); 4133 stats->tx_packets = PPORT_802_3_GET(pstats, a_frames_transmitted_ok); 4134 stats->tx_bytes = PPORT_802_3_GET(pstats, a_octets_transmitted_ok); 4135 4136 /* vport multicast also counts packets that are dropped due to steering 4137 * or rx out of buffer 4138 */ 4139 stats->multicast = VPORT_COUNTER_GET(vstats, received_eth_multicast.packets); 4140 } else { 4141 mlx5e_fold_sw_stats64(priv, stats); 4142 } 4143 4144 stats->rx_missed_errors += priv->stats.qcnt.rx_out_of_buffer; 4145 stats->rx_dropped += PPORT_2863_GET(pstats, if_in_discards); 4146 4147 stats->rx_length_errors += 4148 PPORT_802_3_GET(pstats, a_in_range_length_errors) + 4149 PPORT_802_3_GET(pstats, a_out_of_range_length_field) + 4150 PPORT_802_3_GET(pstats, a_frame_too_long_errors) + 4151 VNIC_ENV_GET(&priv->stats.vnic, eth_wqe_too_small); 4152 stats->rx_crc_errors += 4153 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors); 4154 stats->rx_frame_errors += PPORT_802_3_GET(pstats, a_alignment_errors); 4155 stats->tx_aborted_errors += PPORT_2863_GET(pstats, if_out_discards); 4156 stats->rx_errors += stats->rx_length_errors + stats->rx_crc_errors + 4157 stats->rx_frame_errors; 4158 stats->tx_errors += stats->tx_aborted_errors + stats->tx_carrier_errors; 4159 } 4160 4161 static void mlx5e_nic_set_rx_mode(struct mlx5e_priv *priv) 4162 { 4163 queue_work(priv->wq, &priv->set_rx_mode_work); 4164 } 4165 4166 static int mlx5e_set_rx_mode(struct net_device *dev, 4167 struct netdev_hw_addr_list *uc, 4168 struct netdev_hw_addr_list *mc) 4169 { 4170 struct mlx5e_priv *priv = netdev_priv(dev); 4171 4172 mlx5e_fs_set_rx_mode_work(priv->fs, dev, uc, mc); 4173 4174 return 0; 4175 } 4176 4177 static int mlx5e_set_mac(struct net_device *netdev, void *addr) 4178 { 4179 struct mlx5e_priv *priv = netdev_priv(netdev); 4180 struct sockaddr *saddr = addr; 4181 4182 if (!is_valid_ether_addr(saddr->sa_data)) 4183 return -EADDRNOTAVAIL; 4184 4185 netif_addr_lock_bh(netdev); 4186 eth_hw_addr_set(netdev, saddr->sa_data); 4187 netif_addr_unlock_bh(netdev); 4188 4189 mlx5e_nic_set_rx_mode(priv); 4190 4191 return 0; 4192 } 4193 4194 #define MLX5E_SET_FEATURE(features, feature, enable) \ 4195 do { \ 4196 if (enable) \ 4197 *features |= feature; \ 4198 else \ 4199 *features &= ~feature; \ 4200 } while (0) 4201 4202 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable); 4203 4204 static int set_feature_lro(struct net_device *netdev, bool enable) 4205 { 4206 struct mlx5e_priv *priv = netdev_priv(netdev); 4207 struct mlx5_core_dev *mdev = priv->mdev; 4208 struct mlx5e_params *cur_params; 4209 struct mlx5e_params new_params; 4210 bool reset = true; 4211 int err = 0; 4212 4213 mutex_lock(&priv->state_lock); 4214 4215 cur_params = &priv->channels.params; 4216 new_params = *cur_params; 4217 4218 if (enable) 4219 new_params.packet_merge.type = MLX5E_PACKET_MERGE_LRO; 4220 else if (new_params.packet_merge.type == MLX5E_PACKET_MERGE_LRO) 4221 new_params.packet_merge.type = MLX5E_PACKET_MERGE_NONE; 4222 else 4223 goto out; 4224 4225 if (!(cur_params->packet_merge.type == MLX5E_PACKET_MERGE_SHAMPO && 4226 new_params.packet_merge.type == MLX5E_PACKET_MERGE_LRO)) { 4227 if (cur_params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ) { 4228 if (mlx5e_rx_mpwqe_is_linear_skb(mdev, cur_params, NULL) == 4229 mlx5e_rx_mpwqe_is_linear_skb(mdev, &new_params, NULL)) 4230 reset = false; 4231 } 4232 } 4233 4234 err = mlx5e_safe_switch_params(priv, &new_params, 4235 mlx5e_modify_tirs_packet_merge_ctx, NULL, reset); 4236 out: 4237 mutex_unlock(&priv->state_lock); 4238 return err; 4239 } 4240 4241 static int set_feature_hw_gro(struct net_device *netdev, bool enable) 4242 { 4243 struct mlx5e_priv *priv = netdev_priv(netdev); 4244 struct mlx5e_params new_params; 4245 bool reset = true; 4246 int err = 0; 4247 4248 mutex_lock(&priv->state_lock); 4249 new_params = priv->channels.params; 4250 4251 if (enable) { 4252 new_params.packet_merge.type = MLX5E_PACKET_MERGE_SHAMPO; 4253 } else if (new_params.packet_merge.type == MLX5E_PACKET_MERGE_SHAMPO) { 4254 new_params.packet_merge.type = MLX5E_PACKET_MERGE_NONE; 4255 } else { 4256 goto out; 4257 } 4258 4259 err = mlx5e_safe_switch_params(priv, &new_params, NULL, NULL, reset); 4260 out: 4261 mutex_unlock(&priv->state_lock); 4262 return err; 4263 } 4264 4265 static int set_feature_cvlan_filter(struct net_device *netdev, bool enable) 4266 { 4267 struct mlx5e_priv *priv = netdev_priv(netdev); 4268 4269 if (enable) 4270 mlx5e_enable_cvlan_filter(priv->fs, 4271 !!(priv->netdev->flags & IFF_PROMISC)); 4272 else 4273 mlx5e_disable_cvlan_filter(priv->fs, 4274 !!(priv->netdev->flags & IFF_PROMISC)); 4275 4276 return 0; 4277 } 4278 4279 static int set_feature_hw_tc(struct net_device *netdev, bool enable) 4280 { 4281 struct mlx5e_priv *priv = netdev_priv(netdev); 4282 int err = 0; 4283 4284 #if IS_ENABLED(CONFIG_MLX5_CLS_ACT) 4285 int tc_flag = mlx5e_is_uplink_rep(priv) ? MLX5_TC_FLAG(ESW_OFFLOAD) : 4286 MLX5_TC_FLAG(NIC_OFFLOAD); 4287 if (!enable && mlx5e_tc_num_filters(priv, tc_flag)) { 4288 netdev_err(netdev, 4289 "Active offloaded tc filters, can't turn hw_tc_offload off\n"); 4290 return -EINVAL; 4291 } 4292 #endif 4293 4294 mutex_lock(&priv->state_lock); 4295 if (!enable && mlx5e_selq_is_htb_enabled(&priv->selq)) { 4296 netdev_err(netdev, "Active HTB offload, can't turn hw_tc_offload off\n"); 4297 err = -EINVAL; 4298 } 4299 mutex_unlock(&priv->state_lock); 4300 4301 return err; 4302 } 4303 4304 static int set_feature_rx_all(struct net_device *netdev, bool enable) 4305 { 4306 struct mlx5e_priv *priv = netdev_priv(netdev); 4307 struct mlx5_core_dev *mdev = priv->mdev; 4308 4309 return mlx5_set_port_fcs(mdev, !enable); 4310 } 4311 4312 static struct dim_cq_moder mlx5e_get_def_rx_moderation(u8 cq_period_mode) 4313 { 4314 return (struct dim_cq_moder) { 4315 .cq_period_mode = cq_period_mode, 4316 .pkts = MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS, 4317 .usec = cq_period_mode == DIM_CQ_PERIOD_MODE_START_FROM_CQE ? 4318 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE : 4319 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC, 4320 }; 4321 } 4322 4323 bool mlx5e_reset_rx_moderation(struct dim_cq_moder *cq_moder, u8 cq_period_mode, 4324 bool dim_enabled) 4325 { 4326 bool reset_needed = cq_moder->cq_period_mode != cq_period_mode; 4327 4328 if (dim_enabled) 4329 *cq_moder = net_dim_get_def_rx_moderation(cq_period_mode); 4330 else 4331 *cq_moder = mlx5e_get_def_rx_moderation(cq_period_mode); 4332 4333 return reset_needed; 4334 } 4335 4336 bool mlx5e_reset_rx_channels_moderation(struct mlx5e_channels *chs, u8 cq_period_mode, 4337 bool dim_enabled, bool keep_dim_state) 4338 { 4339 bool reset = false; 4340 int i; 4341 4342 for (i = 0; i < chs->num; i++) { 4343 if (keep_dim_state) 4344 dim_enabled = !!chs->c[i]->rq.dim; 4345 4346 reset |= mlx5e_reset_rx_moderation(&chs->c[i]->rx_cq_moder, 4347 cq_period_mode, dim_enabled); 4348 } 4349 4350 return reset; 4351 } 4352 4353 static int mlx5e_set_rx_port_ts(struct mlx5_core_dev *mdev, bool enable) 4354 { 4355 u32 in[MLX5_ST_SZ_DW(pcmr_reg)] = {}; 4356 bool supported, curr_state; 4357 int err; 4358 4359 if (!MLX5_CAP_GEN(mdev, ports_check)) 4360 return 0; 4361 4362 err = mlx5_query_ports_check(mdev, in, sizeof(in)); 4363 if (err) 4364 return err; 4365 4366 supported = MLX5_GET(pcmr_reg, in, rx_ts_over_crc_cap); 4367 curr_state = MLX5_GET(pcmr_reg, in, rx_ts_over_crc); 4368 4369 if (!supported || enable == curr_state) 4370 return 0; 4371 4372 MLX5_SET(pcmr_reg, in, local_port, 1); 4373 MLX5_SET(pcmr_reg, in, rx_ts_over_crc, enable); 4374 4375 return mlx5_set_ports_check(mdev, in, sizeof(in)); 4376 } 4377 4378 static int mlx5e_set_rx_port_ts_wrap(struct mlx5e_priv *priv, void *ctx) 4379 { 4380 struct mlx5_core_dev *mdev = priv->mdev; 4381 bool enable = *(bool *)ctx; 4382 4383 return mlx5e_set_rx_port_ts(mdev, enable); 4384 } 4385 4386 static int set_feature_rx_fcs(struct net_device *netdev, bool enable) 4387 { 4388 struct mlx5e_priv *priv = netdev_priv(netdev); 4389 struct mlx5e_channels *chs = &priv->channels; 4390 struct mlx5e_params new_params; 4391 int err; 4392 bool rx_ts_over_crc = !enable; 4393 4394 mutex_lock(&priv->state_lock); 4395 4396 new_params = chs->params; 4397 new_params.scatter_fcs_en = enable; 4398 err = mlx5e_safe_switch_params(priv, &new_params, mlx5e_set_rx_port_ts_wrap, 4399 &rx_ts_over_crc, true); 4400 mutex_unlock(&priv->state_lock); 4401 return err; 4402 } 4403 4404 static int set_feature_rx_vlan(struct net_device *netdev, bool enable) 4405 { 4406 struct mlx5e_priv *priv = netdev_priv(netdev); 4407 int err = 0; 4408 4409 mutex_lock(&priv->state_lock); 4410 4411 mlx5e_fs_set_vlan_strip_disable(priv->fs, !enable); 4412 priv->channels.params.vlan_strip_disable = !enable; 4413 4414 if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) 4415 goto unlock; 4416 4417 err = mlx5e_modify_channels_vsd(&priv->channels, !enable); 4418 if (err) { 4419 mlx5e_fs_set_vlan_strip_disable(priv->fs, enable); 4420 priv->channels.params.vlan_strip_disable = enable; 4421 } 4422 unlock: 4423 mutex_unlock(&priv->state_lock); 4424 4425 return err; 4426 } 4427 4428 int mlx5e_vlan_rx_add_vid(struct net_device *dev, __be16 proto, u16 vid) 4429 { 4430 struct mlx5e_priv *priv = netdev_priv(dev); 4431 struct mlx5e_flow_steering *fs = priv->fs; 4432 4433 if (mlx5e_is_uplink_rep(priv)) 4434 return 0; /* no vlan table for uplink rep */ 4435 4436 return mlx5e_fs_vlan_rx_add_vid(fs, dev, proto, vid); 4437 } 4438 4439 int mlx5e_vlan_rx_kill_vid(struct net_device *dev, __be16 proto, u16 vid) 4440 { 4441 struct mlx5e_priv *priv = netdev_priv(dev); 4442 struct mlx5e_flow_steering *fs = priv->fs; 4443 4444 if (mlx5e_is_uplink_rep(priv)) 4445 return 0; /* no vlan table for uplink rep */ 4446 4447 return mlx5e_fs_vlan_rx_kill_vid(fs, dev, proto, vid); 4448 } 4449 4450 #ifdef CONFIG_MLX5_EN_ARFS 4451 static int set_feature_arfs(struct net_device *netdev, bool enable) 4452 { 4453 struct mlx5e_priv *priv = netdev_priv(netdev); 4454 int err; 4455 4456 if (enable) 4457 err = mlx5e_arfs_enable(priv->fs); 4458 else 4459 err = mlx5e_arfs_disable(priv->fs); 4460 4461 return err; 4462 } 4463 #endif 4464 4465 static int mlx5e_handle_feature(struct net_device *netdev, 4466 netdev_features_t *features, 4467 netdev_features_t feature, 4468 mlx5e_feature_handler feature_handler) 4469 { 4470 netdev_features_t changes = *features ^ netdev->features; 4471 bool enable = !!(*features & feature); 4472 int err; 4473 4474 if (!(changes & feature)) 4475 return 0; 4476 4477 err = feature_handler(netdev, enable); 4478 if (err) { 4479 MLX5E_SET_FEATURE(features, feature, !enable); 4480 netdev_err(netdev, "%s feature %pNF failed, err %d\n", 4481 enable ? "Enable" : "Disable", &feature, err); 4482 return err; 4483 } 4484 4485 return 0; 4486 } 4487 4488 void mlx5e_set_xdp_feature(struct mlx5e_priv *priv) 4489 { 4490 struct mlx5e_params *params = &priv->channels.params; 4491 struct net_device *netdev = priv->netdev; 4492 xdp_features_t val = 0; 4493 4494 if (netdev->netdev_ops->ndo_bpf && 4495 params->packet_merge.type == MLX5E_PACKET_MERGE_NONE) 4496 val = NETDEV_XDP_ACT_BASIC | NETDEV_XDP_ACT_REDIRECT | 4497 NETDEV_XDP_ACT_XSK_ZEROCOPY | 4498 NETDEV_XDP_ACT_RX_SG; 4499 4500 if (netdev->netdev_ops->ndo_xdp_xmit && params->xdp_prog) 4501 val |= NETDEV_XDP_ACT_NDO_XMIT | 4502 NETDEV_XDP_ACT_NDO_XMIT_SG; 4503 4504 xdp_set_features_flag_locked(netdev, val); 4505 } 4506 4507 int mlx5e_set_features(struct net_device *netdev, netdev_features_t features) 4508 { 4509 netdev_features_t oper_features = features; 4510 int err = 0; 4511 4512 #define MLX5E_HANDLE_FEATURE(feature, handler) \ 4513 mlx5e_handle_feature(netdev, &oper_features, feature, handler) 4514 4515 if (features & (NETIF_F_GRO_HW | NETIF_F_LRO)) { 4516 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXFCS, set_feature_rx_fcs); 4517 err |= MLX5E_HANDLE_FEATURE(NETIF_F_LRO, set_feature_lro); 4518 err |= MLX5E_HANDLE_FEATURE(NETIF_F_GRO_HW, set_feature_hw_gro); 4519 } else { 4520 err |= MLX5E_HANDLE_FEATURE(NETIF_F_LRO, set_feature_lro); 4521 err |= MLX5E_HANDLE_FEATURE(NETIF_F_GRO_HW, set_feature_hw_gro); 4522 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXFCS, set_feature_rx_fcs); 4523 } 4524 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_FILTER, 4525 set_feature_cvlan_filter); 4526 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_TC, set_feature_hw_tc); 4527 err |= MLX5E_HANDLE_FEATURE(NETIF_F_RXALL, set_feature_rx_all); 4528 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_VLAN_CTAG_RX, set_feature_rx_vlan); 4529 #ifdef CONFIG_MLX5_EN_ARFS 4530 err |= MLX5E_HANDLE_FEATURE(NETIF_F_NTUPLE, set_feature_arfs); 4531 #endif 4532 err |= MLX5E_HANDLE_FEATURE(NETIF_F_HW_TLS_RX, mlx5e_ktls_set_feature_rx); 4533 4534 if (err) { 4535 netdev->features = oper_features; 4536 return -EINVAL; 4537 } 4538 4539 return 0; 4540 } 4541 4542 static netdev_features_t mlx5e_fix_uplink_rep_features(struct net_device *netdev, 4543 netdev_features_t features) 4544 { 4545 features &= ~NETIF_F_HW_TLS_RX; 4546 if (netdev->features & NETIF_F_HW_TLS_RX) 4547 netdev_warn(netdev, "Disabling hw_tls_rx, not supported in switchdev mode\n"); 4548 4549 features &= ~NETIF_F_HW_TLS_TX; 4550 if (netdev->features & NETIF_F_HW_TLS_TX) 4551 netdev_warn(netdev, "Disabling hw_tls_tx, not supported in switchdev mode\n"); 4552 4553 features &= ~NETIF_F_NTUPLE; 4554 if (netdev->features & NETIF_F_NTUPLE) 4555 netdev_warn(netdev, "Disabling ntuple, not supported in switchdev mode\n"); 4556 4557 features &= ~NETIF_F_GRO_HW; 4558 if (netdev->features & NETIF_F_GRO_HW) 4559 netdev_warn(netdev, "Disabling HW_GRO, not supported in switchdev mode\n"); 4560 4561 features &= ~NETIF_F_HW_VLAN_CTAG_FILTER; 4562 if (netdev->features & NETIF_F_HW_VLAN_CTAG_FILTER) 4563 netdev_warn(netdev, "Disabling HW_VLAN CTAG FILTERING, not supported in switchdev mode\n"); 4564 4565 features &= ~NETIF_F_HW_MACSEC; 4566 if (netdev->features & NETIF_F_HW_MACSEC) 4567 netdev_warn(netdev, "Disabling HW MACsec offload, not supported in switchdev mode\n"); 4568 4569 return features; 4570 } 4571 4572 static netdev_features_t mlx5e_fix_features(struct net_device *netdev, 4573 netdev_features_t features) 4574 { 4575 struct netdev_config *cfg = netdev->cfg_pending; 4576 struct mlx5e_priv *priv = netdev_priv(netdev); 4577 struct mlx5e_vlan_table *vlan; 4578 struct mlx5e_params *params; 4579 4580 if (!netif_device_present(netdev)) 4581 return features; 4582 4583 vlan = mlx5e_fs_get_vlan(priv->fs); 4584 mutex_lock(&priv->state_lock); 4585 params = &priv->channels.params; 4586 if (!vlan || 4587 !bitmap_empty(mlx5e_vlan_get_active_svlans(vlan), VLAN_N_VID)) { 4588 /* HW strips the outer C-tag header, this is a problem 4589 * for S-tag traffic. 4590 */ 4591 features &= ~NETIF_F_HW_VLAN_CTAG_RX; 4592 if (!params->vlan_strip_disable) 4593 netdev_warn(netdev, "Dropping C-tag vlan stripping offload due to S-tag vlan\n"); 4594 } 4595 4596 if (!MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_STRIDING_RQ)) { 4597 if (features & NETIF_F_LRO) { 4598 netdev_warn(netdev, "Disabling LRO, not supported in legacy RQ\n"); 4599 features &= ~NETIF_F_LRO; 4600 } 4601 if (features & NETIF_F_GRO_HW) { 4602 netdev_warn(netdev, "Disabling HW-GRO, not supported in legacy RQ\n"); 4603 features &= ~NETIF_F_GRO_HW; 4604 } 4605 } 4606 4607 if (params->xdp_prog) { 4608 if (features & NETIF_F_LRO) { 4609 netdev_warn(netdev, "LRO is incompatible with XDP\n"); 4610 features &= ~NETIF_F_LRO; 4611 } 4612 if (features & NETIF_F_GRO_HW) { 4613 netdev_warn(netdev, "HW GRO is incompatible with XDP\n"); 4614 features &= ~NETIF_F_GRO_HW; 4615 } 4616 } 4617 4618 if (priv->xsk.refcnt) { 4619 if (features & NETIF_F_LRO) { 4620 netdev_warn(netdev, "LRO is incompatible with AF_XDP (%u XSKs are active)\n", 4621 priv->xsk.refcnt); 4622 features &= ~NETIF_F_LRO; 4623 } 4624 if (features & NETIF_F_GRO_HW) { 4625 netdev_warn(netdev, "HW GRO is incompatible with AF_XDP (%u XSKs are active)\n", 4626 priv->xsk.refcnt); 4627 features &= ~NETIF_F_GRO_HW; 4628 } 4629 } 4630 4631 if (MLX5E_GET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS)) { 4632 features &= ~NETIF_F_RXHASH; 4633 if (netdev->features & NETIF_F_RXHASH) 4634 netdev_warn(netdev, "Disabling rxhash, not supported when CQE compress is active\n"); 4635 4636 if (features & NETIF_F_GRO_HW) { 4637 netdev_warn(netdev, "Disabling HW-GRO, not supported when CQE compress is active\n"); 4638 features &= ~NETIF_F_GRO_HW; 4639 } 4640 } 4641 4642 /* The header-data split ring param requires HW GRO to stay enabled. */ 4643 if (cfg && cfg->hds_config == ETHTOOL_TCP_DATA_SPLIT_ENABLED && 4644 !(features & NETIF_F_GRO_HW)) { 4645 netdev_warn(netdev, "Keeping HW-GRO enabled, TCP header-data split depends on it\n"); 4646 features |= NETIF_F_GRO_HW; 4647 } 4648 4649 if (mlx5e_is_uplink_rep(priv)) { 4650 features = mlx5e_fix_uplink_rep_features(netdev, features); 4651 netdev->netns_immutable = true; 4652 } else { 4653 netdev->netns_immutable = false; 4654 } 4655 4656 mutex_unlock(&priv->state_lock); 4657 4658 return features; 4659 } 4660 4661 static bool mlx5e_xsk_validate_mtu(struct net_device *netdev, 4662 struct mlx5e_channels *chs, 4663 struct mlx5e_params *new_params, 4664 struct mlx5_core_dev *mdev) 4665 { 4666 u16 ix; 4667 4668 for (ix = 0; ix < chs->params.num_channels; ix++) { 4669 struct xsk_buff_pool *xsk_pool = 4670 mlx5e_xsk_get_pool(&chs->params, chs->params.xsk, ix); 4671 struct mlx5e_rq_opt_param rqo = {0}; 4672 struct mlx5e_xsk_param xsk; 4673 int max_xdp_mtu; 4674 4675 if (!xsk_pool) 4676 continue; 4677 4678 mlx5e_build_xsk_param(xsk_pool, &xsk); 4679 rqo.xsk = &xsk; 4680 max_xdp_mtu = mlx5e_xdp_max_mtu(new_params, &rqo); 4681 4682 /* Validate XSK params and XDP MTU in advance */ 4683 if (!mlx5e_validate_xsk_param(new_params, &rqo, mdev) || 4684 new_params->sw_mtu > max_xdp_mtu) { 4685 u32 hr = mlx5e_get_linear_rq_headroom(new_params, &rqo); 4686 int max_mtu_frame, max_mtu_page, max_mtu; 4687 4688 /* Two criteria must be met: 4689 * 1. HW MTU + all headrooms <= XSK frame size. 4690 * 2. Size of SKBs allocated on XDP_PASS <= PAGE_SIZE. 4691 */ 4692 max_mtu_frame = MLX5E_HW2SW_MTU(new_params, xsk.chunk_size - hr); 4693 max_mtu_page = MLX5E_HW2SW_MTU(new_params, SKB_MAX_HEAD(0)); 4694 max_mtu = min3(max_mtu_frame, max_mtu_page, max_xdp_mtu); 4695 4696 netdev_err(netdev, "MTU %d is too big for an XSK running on channel %u or its redirection XDP program. Try MTU <= %d\n", 4697 new_params->sw_mtu, ix, max_mtu); 4698 return false; 4699 } 4700 } 4701 4702 return true; 4703 } 4704 4705 static bool mlx5e_params_validate_xdp(struct net_device *netdev, 4706 struct mlx5_core_dev *mdev, 4707 struct mlx5e_params *params) 4708 { 4709 bool is_linear; 4710 4711 /* No XSK params: AF_XDP can't be enabled yet at the point of setting 4712 * the XDP program. 4713 */ 4714 is_linear = params->rq_wq_type == MLX5_WQ_TYPE_CYCLIC ? 4715 mlx5e_rx_is_linear_skb(mdev, params, NULL) : 4716 mlx5e_rx_mpwqe_is_linear_skb(mdev, params, NULL); 4717 4718 if (!is_linear) { 4719 if (!params->xdp_prog->aux->xdp_has_frags) { 4720 netdev_warn(netdev, "MTU(%d) > %d, too big for an XDP program not aware of multi buffer\n", 4721 params->sw_mtu, 4722 mlx5e_xdp_max_mtu(params, NULL)); 4723 return false; 4724 } 4725 if (params->rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ && 4726 !mlx5e_verify_params_rx_mpwqe_strides(mdev, params, NULL)) { 4727 netdev_warn(netdev, "XDP is not allowed with striding RQ and MTU(%d) > %d\n", 4728 params->sw_mtu, 4729 mlx5e_xdp_max_mtu(params, NULL)); 4730 return false; 4731 } 4732 } 4733 4734 return true; 4735 } 4736 4737 int mlx5e_change_mtu(struct net_device *netdev, int new_mtu, 4738 mlx5e_fp_preactivate preactivate) 4739 { 4740 struct mlx5e_priv *priv = netdev_priv(netdev); 4741 struct mlx5e_params new_params; 4742 struct mlx5e_params *params; 4743 int err = 0; 4744 4745 mutex_lock(&priv->state_lock); 4746 4747 params = &priv->channels.params; 4748 4749 new_params = *params; 4750 new_params.sw_mtu = new_mtu; 4751 err = mlx5e_validate_params(priv->mdev, &new_params); 4752 if (err) 4753 goto out; 4754 4755 if (new_params.xdp_prog && !mlx5e_params_validate_xdp(netdev, priv->mdev, 4756 &new_params)) { 4757 err = -EINVAL; 4758 goto out; 4759 } 4760 4761 if (priv->xsk.refcnt && 4762 !mlx5e_xsk_validate_mtu(netdev, &priv->channels, 4763 &new_params, priv->mdev)) { 4764 err = -EINVAL; 4765 goto out; 4766 } 4767 4768 err = mlx5e_safe_switch_params(priv, &new_params, preactivate, NULL, 4769 true); 4770 4771 out: 4772 WRITE_ONCE(netdev->mtu, params->sw_mtu); 4773 mutex_unlock(&priv->state_lock); 4774 4775 if (!err) 4776 netdev_update_features(netdev); 4777 4778 return err; 4779 } 4780 4781 static int mlx5e_change_nic_mtu(struct net_device *netdev, int new_mtu) 4782 { 4783 return mlx5e_change_mtu(netdev, new_mtu, mlx5e_set_dev_port_mtu_ctx); 4784 } 4785 4786 int mlx5e_ptp_rx_manage_fs_ctx(struct mlx5e_priv *priv, void *ctx) 4787 { 4788 bool set = *(bool *)ctx; 4789 4790 return mlx5e_ptp_rx_manage_fs(priv, set); 4791 } 4792 4793 static int mlx5e_hwstamp_config_no_ptp_rx(struct mlx5e_priv *priv, bool rx_filter) 4794 { 4795 bool rx_cqe_compress_def = priv->channels.params.rx_cqe_compress_def; 4796 int err; 4797 4798 if (!rx_filter) 4799 /* Reset CQE compression to Admin default */ 4800 return mlx5e_modify_rx_cqe_compression_locked(priv, rx_cqe_compress_def, false); 4801 4802 if (!MLX5E_GET_PFLAG(&priv->channels.params, MLX5E_PFLAG_RX_CQE_COMPRESS)) 4803 return 0; 4804 4805 /* Disable CQE compression */ 4806 netdev_warn(priv->netdev, "Disabling RX cqe compression\n"); 4807 err = mlx5e_modify_rx_cqe_compression_locked(priv, false, true); 4808 if (err) 4809 netdev_err(priv->netdev, "Failed disabling cqe compression err=%d\n", err); 4810 4811 return err; 4812 } 4813 4814 static int mlx5e_hwstamp_config_ptp_rx(struct mlx5e_priv *priv, bool ptp_rx) 4815 { 4816 struct mlx5e_params new_params; 4817 4818 if (ptp_rx == priv->channels.params.ptp_rx) 4819 return 0; 4820 4821 new_params = priv->channels.params; 4822 new_params.ptp_rx = ptp_rx; 4823 return mlx5e_safe_switch_params(priv, &new_params, mlx5e_ptp_rx_manage_fs_ctx, 4824 &new_params.ptp_rx, true); 4825 } 4826 4827 int mlx5e_hwtstamp_set(struct mlx5e_priv *priv, 4828 struct kernel_hwtstamp_config *config, 4829 struct netlink_ext_ack *extack) 4830 { 4831 bool rx_cqe_compress_def; 4832 bool ptp_rx; 4833 int err; 4834 4835 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz) || 4836 (mlx5_clock_get_ptp_index(priv->mdev) == -1)) { 4837 NL_SET_ERR_MSG_MOD(extack, 4838 "Timestamps are not supported on this device"); 4839 return -EOPNOTSUPP; 4840 } 4841 4842 /* TX HW timestamp */ 4843 switch (config->tx_type) { 4844 case HWTSTAMP_TX_OFF: 4845 case HWTSTAMP_TX_ON: 4846 break; 4847 default: 4848 return -ERANGE; 4849 } 4850 4851 mutex_lock(&priv->state_lock); 4852 rx_cqe_compress_def = priv->channels.params.rx_cqe_compress_def; 4853 4854 /* RX HW timestamp */ 4855 switch (config->rx_filter) { 4856 case HWTSTAMP_FILTER_NONE: 4857 ptp_rx = false; 4858 break; 4859 case HWTSTAMP_FILTER_ALL: 4860 case HWTSTAMP_FILTER_SOME: 4861 case HWTSTAMP_FILTER_PTP_V1_L4_EVENT: 4862 case HWTSTAMP_FILTER_PTP_V1_L4_SYNC: 4863 case HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ: 4864 case HWTSTAMP_FILTER_PTP_V2_L4_EVENT: 4865 case HWTSTAMP_FILTER_PTP_V2_L4_SYNC: 4866 case HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ: 4867 case HWTSTAMP_FILTER_PTP_V2_L2_EVENT: 4868 case HWTSTAMP_FILTER_PTP_V2_L2_SYNC: 4869 case HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ: 4870 case HWTSTAMP_FILTER_PTP_V2_EVENT: 4871 case HWTSTAMP_FILTER_PTP_V2_SYNC: 4872 case HWTSTAMP_FILTER_PTP_V2_DELAY_REQ: 4873 case HWTSTAMP_FILTER_NTP_ALL: 4874 config->rx_filter = HWTSTAMP_FILTER_ALL; 4875 /* ptp_rx is set if both HW TS is set and CQE 4876 * compression is set 4877 */ 4878 ptp_rx = rx_cqe_compress_def; 4879 break; 4880 default: 4881 err = -ERANGE; 4882 goto err_unlock; 4883 } 4884 4885 if (!mlx5e_profile_feature_cap(priv->profile, PTP_RX)) 4886 err = mlx5e_hwstamp_config_no_ptp_rx(priv, 4887 config->rx_filter != HWTSTAMP_FILTER_NONE); 4888 else 4889 err = mlx5e_hwstamp_config_ptp_rx(priv, ptp_rx); 4890 if (err) 4891 goto err_unlock; 4892 4893 priv->hwtstamp_config = *config; 4894 mutex_unlock(&priv->state_lock); 4895 4896 /* might need to fix some features */ 4897 netdev_update_features(priv->netdev); 4898 4899 return 0; 4900 err_unlock: 4901 mutex_unlock(&priv->state_lock); 4902 return err; 4903 } 4904 4905 static int mlx5e_hwtstamp_set_ndo(struct net_device *netdev, 4906 struct kernel_hwtstamp_config *config, 4907 struct netlink_ext_ack *extack) 4908 { 4909 struct mlx5e_priv *priv = netdev_priv(netdev); 4910 4911 return mlx5e_hwtstamp_set(priv, config, extack); 4912 } 4913 4914 int mlx5e_hwtstamp_get(struct mlx5e_priv *priv, 4915 struct kernel_hwtstamp_config *config) 4916 { 4917 if (!MLX5_CAP_GEN(priv->mdev, device_frequency_khz)) 4918 return -EOPNOTSUPP; 4919 4920 *config = priv->hwtstamp_config; 4921 4922 return 0; 4923 } 4924 4925 static int mlx5e_hwtstamp_get_ndo(struct net_device *dev, 4926 struct kernel_hwtstamp_config *config) 4927 { 4928 struct mlx5e_priv *priv = netdev_priv(dev); 4929 4930 return mlx5e_hwtstamp_get(priv, config); 4931 } 4932 4933 #ifdef CONFIG_MLX5_ESWITCH 4934 int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac) 4935 { 4936 struct mlx5e_priv *priv = netdev_priv(dev); 4937 struct mlx5_core_dev *mdev = priv->mdev; 4938 4939 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac); 4940 } 4941 4942 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos, 4943 __be16 vlan_proto) 4944 { 4945 struct mlx5e_priv *priv = netdev_priv(dev); 4946 struct mlx5_core_dev *mdev = priv->mdev; 4947 4948 if (vlan_proto != htons(ETH_P_8021Q)) 4949 return -EPROTONOSUPPORT; 4950 4951 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1, 4952 vlan, qos); 4953 } 4954 4955 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting) 4956 { 4957 struct mlx5e_priv *priv = netdev_priv(dev); 4958 struct mlx5_core_dev *mdev = priv->mdev; 4959 4960 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting); 4961 } 4962 4963 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting) 4964 { 4965 struct mlx5e_priv *priv = netdev_priv(dev); 4966 struct mlx5_core_dev *mdev = priv->mdev; 4967 4968 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting); 4969 } 4970 4971 int mlx5e_set_vf_rate(struct net_device *dev, int vf, int min_tx_rate, 4972 int max_tx_rate) 4973 { 4974 struct mlx5e_priv *priv = netdev_priv(dev); 4975 struct mlx5_core_dev *mdev = priv->mdev; 4976 4977 return mlx5_eswitch_set_vport_rate(mdev->priv.eswitch, vf + 1, 4978 max_tx_rate, min_tx_rate); 4979 } 4980 4981 static int mlx5_vport_link2ifla(u8 esw_link) 4982 { 4983 switch (esw_link) { 4984 case MLX5_VPORT_ADMIN_STATE_DOWN: 4985 return IFLA_VF_LINK_STATE_DISABLE; 4986 case MLX5_VPORT_ADMIN_STATE_UP: 4987 return IFLA_VF_LINK_STATE_ENABLE; 4988 } 4989 return IFLA_VF_LINK_STATE_AUTO; 4990 } 4991 4992 static int mlx5_ifla_link2vport(u8 ifla_link) 4993 { 4994 switch (ifla_link) { 4995 case IFLA_VF_LINK_STATE_DISABLE: 4996 return MLX5_VPORT_ADMIN_STATE_DOWN; 4997 case IFLA_VF_LINK_STATE_ENABLE: 4998 return MLX5_VPORT_ADMIN_STATE_UP; 4999 } 5000 return MLX5_VPORT_ADMIN_STATE_AUTO; 5001 } 5002 5003 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf, 5004 int link_state) 5005 { 5006 struct mlx5e_priv *priv = netdev_priv(dev); 5007 struct mlx5_core_dev *mdev = priv->mdev; 5008 5009 if (mlx5e_is_uplink_rep(priv)) 5010 return -EOPNOTSUPP; 5011 5012 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1, 5013 mlx5_ifla_link2vport(link_state)); 5014 } 5015 5016 int mlx5e_get_vf_config(struct net_device *dev, 5017 int vf, struct ifla_vf_info *ivi) 5018 { 5019 struct mlx5e_priv *priv = netdev_priv(dev); 5020 struct mlx5_core_dev *mdev = priv->mdev; 5021 int err; 5022 5023 if (!netif_device_present(dev)) 5024 return -EOPNOTSUPP; 5025 5026 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi); 5027 if (err) 5028 return err; 5029 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate); 5030 return 0; 5031 } 5032 5033 int mlx5e_get_vf_stats(struct net_device *dev, 5034 int vf, struct ifla_vf_stats *vf_stats) 5035 { 5036 struct mlx5e_priv *priv = netdev_priv(dev); 5037 struct mlx5_core_dev *mdev = priv->mdev; 5038 5039 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1, 5040 vf_stats); 5041 } 5042 5043 static bool 5044 mlx5e_has_offload_stats(const struct net_device *dev, int attr_id) 5045 { 5046 struct mlx5e_priv *priv = netdev_priv(dev); 5047 5048 if (!netif_device_present(dev)) 5049 return false; 5050 5051 if (!mlx5e_is_uplink_rep(priv)) 5052 return false; 5053 5054 return mlx5e_rep_has_offload_stats(dev, attr_id); 5055 } 5056 5057 static int 5058 mlx5e_get_offload_stats(int attr_id, const struct net_device *dev, 5059 void *sp) 5060 { 5061 struct mlx5e_priv *priv = netdev_priv(dev); 5062 5063 if (!mlx5e_is_uplink_rep(priv)) 5064 return -EOPNOTSUPP; 5065 5066 return mlx5e_rep_get_offload_stats(attr_id, dev, sp); 5067 } 5068 #endif 5069 5070 static bool mlx5e_tunnel_proto_supported_tx(struct mlx5_core_dev *mdev, u8 proto_type) 5071 { 5072 switch (proto_type) { 5073 case IPPROTO_GRE: 5074 return MLX5_CAP_ETH(mdev, tunnel_stateless_gre); 5075 case IPPROTO_IPIP: 5076 case IPPROTO_IPV6: 5077 return (MLX5_CAP_ETH(mdev, tunnel_stateless_ip_over_ip) || 5078 MLX5_CAP_ETH(mdev, tunnel_stateless_ip_over_ip_tx)); 5079 default: 5080 return false; 5081 } 5082 } 5083 5084 static bool mlx5e_gre_tunnel_inner_proto_offload_supported(struct mlx5_core_dev *mdev, 5085 struct sk_buff *skb) 5086 { 5087 switch (skb->inner_protocol) { 5088 case htons(ETH_P_IP): 5089 case htons(ETH_P_IPV6): 5090 case htons(ETH_P_TEB): 5091 return true; 5092 case htons(ETH_P_MPLS_UC): 5093 case htons(ETH_P_MPLS_MC): 5094 return MLX5_CAP_ETH(mdev, tunnel_stateless_mpls_over_gre); 5095 } 5096 return false; 5097 } 5098 5099 static netdev_features_t mlx5e_tunnel_features_check(struct mlx5e_priv *priv, 5100 struct sk_buff *skb, 5101 netdev_features_t features) 5102 { 5103 unsigned int offset = 0; 5104 struct udphdr *udph; 5105 u8 proto; 5106 u16 port; 5107 5108 switch (vlan_get_protocol(skb)) { 5109 case htons(ETH_P_IP): 5110 proto = ip_hdr(skb)->protocol; 5111 break; 5112 case htons(ETH_P_IPV6): 5113 proto = ipv6_find_hdr(skb, &offset, -1, NULL, NULL); 5114 break; 5115 default: 5116 goto out; 5117 } 5118 5119 switch (proto) { 5120 case IPPROTO_GRE: 5121 if (mlx5e_gre_tunnel_inner_proto_offload_supported(priv->mdev, skb)) 5122 return features; 5123 break; 5124 case IPPROTO_IPIP: 5125 case IPPROTO_IPV6: 5126 if (mlx5e_tunnel_proto_supported_tx(priv->mdev, IPPROTO_IPIP)) 5127 return features; 5128 break; 5129 case IPPROTO_UDP: 5130 udph = udp_hdr(skb); 5131 port = be16_to_cpu(udph->dest); 5132 5133 /* Verify if UDP port is being offloaded by HW */ 5134 if (mlx5_vxlan_lookup_port(priv->mdev->vxlan, port)) 5135 return vxlan_features_check(skb, features); 5136 5137 #if IS_ENABLED(CONFIG_GENEVE) 5138 /* Support Geneve offload for default UDP port */ 5139 if (port == GENEVE_UDP_PORT && mlx5_geneve_tx_allowed(priv->mdev)) 5140 return features; 5141 #endif 5142 break; 5143 #ifdef CONFIG_MLX5_EN_IPSEC 5144 case IPPROTO_ESP: 5145 return mlx5e_ipsec_feature_check(skb, features); 5146 #endif 5147 } 5148 5149 out: 5150 /* Disable CSUM and GSO if skb cannot be offloaded by HW */ 5151 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK); 5152 } 5153 5154 netdev_features_t mlx5e_features_check(struct sk_buff *skb, 5155 struct net_device *netdev, 5156 netdev_features_t features) 5157 { 5158 struct mlx5e_priv *priv = netdev_priv(netdev); 5159 5160 features = vlan_features_check(skb, features); 5161 5162 /* Validate if the tunneled packet is being offloaded by HW */ 5163 if (skb->encapsulation && 5164 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK)) 5165 return mlx5e_tunnel_features_check(priv, skb, features); 5166 5167 return features; 5168 } 5169 5170 static void mlx5e_tx_timeout_work(struct work_struct *work) 5171 { 5172 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv, 5173 tx_timeout_work); 5174 struct net_device *netdev = priv->netdev; 5175 int i; 5176 5177 for (i = 0; i < netdev->real_num_tx_queues; i++) { 5178 struct netdev_queue *dev_queue = 5179 netdev_get_tx_queue(netdev, i); 5180 struct mlx5e_txqsq *sq = priv->txq2sq[i]; 5181 5182 if (!netif_xmit_timeout_ms(dev_queue)) 5183 continue; 5184 5185 if (mlx5e_reporter_tx_timeout(sq)) 5186 /* break if tried to reopened channels */ 5187 break; 5188 } 5189 } 5190 5191 static void mlx5e_tx_timeout(struct net_device *dev, unsigned int txqueue) 5192 { 5193 struct mlx5e_priv *priv = netdev_priv(dev); 5194 5195 netdev_err(dev, "TX timeout detected\n"); 5196 queue_work(priv->wq, &priv->tx_timeout_work); 5197 } 5198 5199 static int mlx5e_xdp_allowed(struct net_device *netdev, struct mlx5_core_dev *mdev, 5200 struct mlx5e_params *params) 5201 { 5202 if (params->packet_merge.type != MLX5E_PACKET_MERGE_NONE) { 5203 netdev_warn(netdev, "can't set XDP while HW-GRO/LRO is on, disable them first\n"); 5204 return -EINVAL; 5205 } 5206 5207 if (!mlx5e_params_validate_xdp(netdev, mdev, params)) 5208 return -EINVAL; 5209 5210 return 0; 5211 } 5212 5213 static void mlx5e_rq_replace_xdp_prog(struct mlx5e_rq *rq, struct bpf_prog *prog) 5214 { 5215 struct bpf_prog *old_prog; 5216 5217 old_prog = rcu_replace_pointer(rq->xdp_prog, prog, 5218 lockdep_is_held(&rq->priv->state_lock)); 5219 if (old_prog) 5220 bpf_prog_put(old_prog); 5221 } 5222 5223 static int mlx5e_xdp_set(struct net_device *netdev, struct bpf_prog *prog) 5224 { 5225 struct mlx5e_priv *priv = netdev_priv(netdev); 5226 struct mlx5e_params new_params; 5227 struct bpf_prog *old_prog; 5228 int err = 0; 5229 bool reset; 5230 int i; 5231 5232 mutex_lock(&priv->state_lock); 5233 5234 new_params = priv->channels.params; 5235 new_params.xdp_prog = prog; 5236 5237 if (prog) { 5238 err = mlx5e_xdp_allowed(netdev, priv->mdev, &new_params); 5239 if (err) 5240 goto unlock; 5241 } 5242 5243 /* no need for full reset when exchanging programs */ 5244 reset = (!priv->channels.params.xdp_prog || !prog); 5245 5246 old_prog = priv->channels.params.xdp_prog; 5247 5248 err = mlx5e_safe_switch_params(priv, &new_params, NULL, NULL, reset); 5249 if (err) 5250 goto unlock; 5251 5252 if (old_prog) 5253 bpf_prog_put(old_prog); 5254 5255 if (!test_bit(MLX5E_STATE_OPENED, &priv->state) || reset) 5256 goto unlock; 5257 5258 /* exchanging programs w/o reset, we update ref counts on behalf 5259 * of the channels RQs here. 5260 */ 5261 bpf_prog_add(prog, priv->channels.num); 5262 for (i = 0; i < priv->channels.num; i++) { 5263 struct mlx5e_channel *c = priv->channels.c[i]; 5264 5265 mlx5e_rq_replace_xdp_prog(&c->rq, prog); 5266 if (test_bit(MLX5E_CHANNEL_STATE_XSK, c->state)) { 5267 bpf_prog_inc(prog); 5268 mlx5e_rq_replace_xdp_prog(&c->xskrq, prog); 5269 } 5270 } 5271 5272 unlock: 5273 mutex_unlock(&priv->state_lock); 5274 5275 /* Need to fix some features. */ 5276 if (!err) 5277 netdev_update_features(netdev); 5278 5279 return err; 5280 } 5281 5282 static int mlx5e_xdp(struct net_device *dev, struct netdev_bpf *xdp) 5283 { 5284 switch (xdp->command) { 5285 case XDP_SETUP_PROG: 5286 return mlx5e_xdp_set(dev, xdp->prog); 5287 case XDP_SETUP_XSK_POOL: 5288 return mlx5e_xsk_setup_pool(dev, xdp->xsk.pool, 5289 xdp->xsk.queue_id); 5290 default: 5291 return -EINVAL; 5292 } 5293 } 5294 5295 #ifdef CONFIG_MLX5_ESWITCH 5296 static int mlx5e_bridge_getlink(struct sk_buff *skb, u32 pid, u32 seq, 5297 struct net_device *dev, u32 filter_mask, 5298 int nlflags) 5299 { 5300 struct mlx5e_priv *priv = netdev_priv(dev); 5301 struct mlx5_core_dev *mdev = priv->mdev; 5302 u8 mode, setting; 5303 5304 if (mlx5_eswitch_get_vepa(mdev->priv.eswitch, &setting)) 5305 return -EOPNOTSUPP; 5306 mode = setting ? BRIDGE_MODE_VEPA : BRIDGE_MODE_VEB; 5307 return ndo_dflt_bridge_getlink(skb, pid, seq, dev, 5308 mode, 5309 0, 0, nlflags, filter_mask, NULL); 5310 } 5311 5312 static int mlx5e_bridge_setlink(struct net_device *dev, struct nlmsghdr *nlh, 5313 u16 flags, struct netlink_ext_ack *extack) 5314 { 5315 struct mlx5e_priv *priv = netdev_priv(dev); 5316 struct mlx5_core_dev *mdev = priv->mdev; 5317 struct nlattr *attr, *br_spec; 5318 u16 mode = BRIDGE_MODE_UNDEF; 5319 u8 setting; 5320 int rem; 5321 5322 br_spec = nlmsg_find_attr(nlh, sizeof(struct ifinfomsg), IFLA_AF_SPEC); 5323 if (!br_spec) 5324 return -EINVAL; 5325 5326 nla_for_each_nested_type(attr, IFLA_BRIDGE_MODE, br_spec, rem) { 5327 mode = nla_get_u16(attr); 5328 if (mode > BRIDGE_MODE_VEPA) 5329 return -EINVAL; 5330 5331 break; 5332 } 5333 5334 if (mode == BRIDGE_MODE_UNDEF) 5335 return -EINVAL; 5336 5337 setting = (mode == BRIDGE_MODE_VEPA) ? 1 : 0; 5338 return mlx5_eswitch_set_vepa(mdev->priv.eswitch, setting); 5339 } 5340 #endif 5341 5342 const struct net_device_ops mlx5e_netdev_ops = { 5343 .ndo_open = mlx5e_open, 5344 .ndo_stop = mlx5e_close, 5345 .ndo_start_xmit = mlx5e_xmit, 5346 .ndo_setup_tc = mlx5e_setup_tc, 5347 .ndo_select_queue = mlx5e_select_queue, 5348 .ndo_get_stats64 = mlx5e_get_stats, 5349 .ndo_set_rx_mode_async = mlx5e_set_rx_mode, 5350 .ndo_set_mac_address = mlx5e_set_mac, 5351 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid, 5352 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid, 5353 .ndo_set_features = mlx5e_set_features, 5354 .ndo_fix_features = mlx5e_fix_features, 5355 .ndo_change_mtu = mlx5e_change_nic_mtu, 5356 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate, 5357 .ndo_features_check = mlx5e_features_check, 5358 .ndo_tx_timeout = mlx5e_tx_timeout, 5359 .ndo_bpf = mlx5e_xdp, 5360 .ndo_xdp_xmit = mlx5e_xdp_xmit, 5361 .ndo_xsk_wakeup = mlx5e_xsk_wakeup, 5362 .ndo_hwtstamp_get = mlx5e_hwtstamp_get_ndo, 5363 .ndo_hwtstamp_set = mlx5e_hwtstamp_set_ndo, 5364 #ifdef CONFIG_MLX5_EN_ARFS 5365 .ndo_rx_flow_steer = mlx5e_rx_flow_steer, 5366 #endif 5367 #ifdef CONFIG_MLX5_ESWITCH 5368 .ndo_bridge_setlink = mlx5e_bridge_setlink, 5369 .ndo_bridge_getlink = mlx5e_bridge_getlink, 5370 5371 /* SRIOV E-Switch NDOs */ 5372 .ndo_set_vf_mac = mlx5e_set_vf_mac, 5373 .ndo_set_vf_vlan = mlx5e_set_vf_vlan, 5374 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk, 5375 .ndo_set_vf_trust = mlx5e_set_vf_trust, 5376 .ndo_set_vf_rate = mlx5e_set_vf_rate, 5377 .ndo_get_vf_config = mlx5e_get_vf_config, 5378 .ndo_set_vf_link_state = mlx5e_set_vf_link_state, 5379 .ndo_get_vf_stats = mlx5e_get_vf_stats, 5380 .ndo_has_offload_stats = mlx5e_has_offload_stats, 5381 .ndo_get_offload_stats = mlx5e_get_offload_stats, 5382 #endif 5383 }; 5384 5385 void mlx5e_build_nic_params(struct mlx5e_priv *priv, struct mlx5e_xsk *xsk, u16 mtu) 5386 { 5387 struct mlx5e_params *params = &priv->channels.params; 5388 struct mlx5_core_dev *mdev = priv->mdev; 5389 5390 params->sw_mtu = mtu; 5391 params->hard_mtu = MLX5E_ETH_HARD_MTU; 5392 params->num_channels = min_t(unsigned int, MLX5E_MAX_NUM_CHANNELS / 2, 5393 priv->max_nch); 5394 mlx5e_params_mqprio_reset(params); 5395 5396 /* SQ */ 5397 params->log_sq_size = is_kdump_kernel() ? 5398 MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE : 5399 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE; 5400 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_SKB_TX_MPWQE, mlx5e_tx_mpwqe_supported(mdev)); 5401 5402 /* XDP SQ */ 5403 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_XDP_TX_MPWQE, mlx5e_tx_mpwqe_supported(mdev)); 5404 5405 /* set CQE compression */ 5406 params->rx_cqe_compress_def = false; 5407 if (MLX5_CAP_GEN(mdev, cqe_compression) && 5408 MLX5_CAP_GEN(mdev, vport_group_manager)) 5409 params->rx_cqe_compress_def = slow_pci_heuristic(mdev); 5410 5411 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_CQE_COMPRESS, params->rx_cqe_compress_def); 5412 MLX5E_SET_PFLAG(params, MLX5E_PFLAG_RX_NO_CSUM_COMPLETE, false); 5413 5414 /* RQ */ 5415 mlx5e_build_rq_params(mdev, params); 5416 5417 params->terminate_lkey_be = mlx5_core_get_terminate_scatter_list_mkey(mdev); 5418 5419 params->packet_merge.timeout = mlx5e_choose_lro_timeout(mdev, MLX5E_DEFAULT_LRO_TIMEOUT); 5420 5421 /* CQ moderation params */ 5422 params->rx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation) && 5423 MLX5_CAP_GEN(mdev, cq_period_mode_modify); 5424 params->tx_dim_enabled = MLX5_CAP_GEN(mdev, cq_moderation) && 5425 MLX5_CAP_GEN(mdev, cq_period_mode_modify); 5426 params->rx_moder_use_cqe_mode = !!MLX5_CAP_GEN(mdev, cq_period_start_from_cqe); 5427 params->tx_moder_use_cqe_mode = false; 5428 mlx5e_reset_rx_moderation(¶ms->rx_cq_moderation, params->rx_moder_use_cqe_mode, 5429 params->rx_dim_enabled); 5430 mlx5e_reset_tx_moderation(¶ms->tx_cq_moderation, params->tx_moder_use_cqe_mode, 5431 params->tx_dim_enabled); 5432 5433 /* TX inline */ 5434 mlx5_query_min_inline(mdev, ¶ms->tx_min_inline_mode); 5435 5436 /* AF_XDP */ 5437 params->xsk = xsk; 5438 5439 /* Do not update netdev->features directly in here 5440 * on mlx5e_attach_netdev() we will call mlx5e_update_features() 5441 * To update netdev->features please modify mlx5e_fix_features() 5442 */ 5443 } 5444 5445 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev) 5446 { 5447 struct mlx5e_priv *priv = netdev_priv(netdev); 5448 u8 addr[ETH_ALEN]; 5449 5450 mlx5_query_mac_address(priv->mdev, addr); 5451 if (is_zero_ether_addr(addr)) { 5452 eth_hw_addr_random(netdev); 5453 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr); 5454 return; 5455 } 5456 5457 eth_hw_addr_set(netdev, addr); 5458 } 5459 5460 static int mlx5e_vxlan_set_port(struct net_device *netdev, unsigned int table, 5461 unsigned int entry, struct udp_tunnel_info *ti) 5462 { 5463 struct mlx5e_priv *priv = netdev_priv(netdev); 5464 5465 return mlx5_vxlan_add_port(priv->mdev->vxlan, ntohs(ti->port)); 5466 } 5467 5468 static int mlx5e_vxlan_unset_port(struct net_device *netdev, unsigned int table, 5469 unsigned int entry, struct udp_tunnel_info *ti) 5470 { 5471 struct mlx5e_priv *priv = netdev_priv(netdev); 5472 5473 return mlx5_vxlan_del_port(priv->mdev->vxlan, ntohs(ti->port)); 5474 } 5475 5476 void mlx5e_vxlan_set_netdev_info(struct mlx5e_priv *priv) 5477 { 5478 if (!mlx5_vxlan_allowed(priv->mdev->vxlan)) 5479 return; 5480 5481 priv->nic_info.set_port = mlx5e_vxlan_set_port; 5482 priv->nic_info.unset_port = mlx5e_vxlan_unset_port; 5483 priv->nic_info.flags = UDP_TUNNEL_NIC_INFO_STATIC_IANA_VXLAN; 5484 priv->nic_info.tables[0].tunnel_types = UDP_TUNNEL_TYPE_VXLAN; 5485 /* Don't count the space hard-coded to the IANA port */ 5486 priv->nic_info.tables[0].n_entries = 5487 mlx5_vxlan_max_udp_ports(priv->mdev) - 1; 5488 5489 priv->netdev->udp_tunnel_nic_info = &priv->nic_info; 5490 } 5491 5492 static bool mlx5e_tunnel_any_tx_proto_supported(struct mlx5_core_dev *mdev) 5493 { 5494 int tt; 5495 5496 for (tt = 0; tt < MLX5_NUM_TUNNEL_TT; tt++) { 5497 if (mlx5e_tunnel_proto_supported_tx(mdev, mlx5_get_proto_by_tunnel_type(tt))) 5498 return true; 5499 } 5500 return (mlx5_vxlan_allowed(mdev->vxlan) || mlx5_geneve_tx_allowed(mdev)); 5501 } 5502 5503 static void mlx5e_get_queue_stats_rx(struct net_device *dev, int i, 5504 struct netdev_queue_stats_rx *stats) 5505 { 5506 struct mlx5e_priv *priv = netdev_priv(dev); 5507 struct mlx5e_channel_stats *channel_stats; 5508 struct mlx5e_rq_stats *xskrq_stats; 5509 struct mlx5e_rq_stats *rq_stats; 5510 5511 if (mlx5e_is_uplink_rep(priv) || i >= mlx5e_stats_nch_read(priv)) 5512 return; 5513 5514 channel_stats = priv->channel_stats[i]; 5515 xskrq_stats = &channel_stats->xskrq; 5516 rq_stats = &channel_stats->rq; 5517 5518 stats->packets = rq_stats->packets + xskrq_stats->packets; 5519 stats->bytes = rq_stats->bytes + xskrq_stats->bytes; 5520 stats->alloc_fail = rq_stats->buff_alloc_err + 5521 xskrq_stats->buff_alloc_err; 5522 5523 stats->hw_gro_packets = rq_stats->gro_skbs + xskrq_stats->gro_skbs; 5524 stats->hw_gro_wire_packets = 5525 rq_stats->gro_packets + xskrq_stats->gro_packets; 5526 stats->hw_gro_wire_bytes = rq_stats->gro_bytes + xskrq_stats->gro_bytes; 5527 } 5528 5529 static void mlx5e_get_queue_stats_tx(struct net_device *dev, int i, 5530 struct netdev_queue_stats_tx *stats) 5531 { 5532 struct mlx5e_priv *priv = netdev_priv(dev); 5533 struct mlx5e_sq_stats *sq_stats; 5534 5535 if (!mlx5e_stats_nch_read(priv)) 5536 return; 5537 5538 /* no special case needed for ptp htb etc since txq2sq_stats is kept up 5539 * to date for active sq_stats, otherwise get_base_stats takes care of 5540 * inactive sqs. 5541 */ 5542 sq_stats = priv->txq2sq_stats[i]; 5543 if (!sq_stats) 5544 return; 5545 5546 stats->packets = sq_stats->packets; 5547 stats->bytes = sq_stats->bytes; 5548 5549 stats->hw_gso_packets = 5550 sq_stats->tso_packets + sq_stats->tso_inner_packets; 5551 stats->hw_gso_bytes = sq_stats->tso_bytes + sq_stats->tso_inner_bytes; 5552 5553 stats->csum_none = sq_stats->csum_none; 5554 5555 stats->stop = sq_stats->stopped; 5556 stats->wake = sq_stats->wake; 5557 } 5558 5559 static void mlx5e_get_base_stats(struct net_device *dev, 5560 struct netdev_queue_stats_rx *rx, 5561 struct netdev_queue_stats_tx *tx) 5562 { 5563 struct mlx5e_priv *priv = netdev_priv(dev); 5564 u16 nch = mlx5e_stats_nch_read(priv); 5565 struct mlx5e_ptp *ptp_channel; 5566 int i, tc; 5567 5568 if (!mlx5e_is_uplink_rep(priv)) { 5569 rx->packets = 0; 5570 rx->bytes = 0; 5571 rx->alloc_fail = 0; 5572 rx->hw_gro_packets = 0; 5573 rx->hw_gro_wire_packets = 0; 5574 rx->hw_gro_wire_bytes = 0; 5575 5576 for (i = priv->channels.params.num_channels; i < nch; i++) { 5577 struct netdev_queue_stats_rx rx_i = {0}; 5578 5579 mlx5e_get_queue_stats_rx(dev, i, &rx_i); 5580 5581 rx->packets += rx_i.packets; 5582 rx->bytes += rx_i.bytes; 5583 rx->alloc_fail += rx_i.alloc_fail; 5584 rx->hw_gro_packets += rx_i.hw_gro_packets; 5585 rx->hw_gro_wire_packets += rx_i.hw_gro_wire_packets; 5586 rx->hw_gro_wire_bytes += rx_i.hw_gro_wire_bytes; 5587 } 5588 5589 /* always report PTP RX stats from base as there is no 5590 * corresponding channel to report them under in 5591 * mlx5e_get_queue_stats_rx. 5592 */ 5593 if (priv->rx_ptp_opened) { 5594 struct mlx5e_rq_stats *rq_stats = &priv->ptp_stats.rq; 5595 5596 rx->packets += rq_stats->packets; 5597 rx->bytes += rq_stats->bytes; 5598 rx->hw_gro_packets += rq_stats->gro_skbs; 5599 rx->hw_gro_wire_packets += rq_stats->gro_packets; 5600 rx->hw_gro_wire_bytes += rq_stats->gro_bytes; 5601 } 5602 } 5603 5604 tx->packets = 0; 5605 tx->bytes = 0; 5606 tx->hw_gso_packets = 0; 5607 tx->hw_gso_bytes = 0; 5608 tx->csum_none = 0; 5609 tx->stop = 0; 5610 tx->wake = 0; 5611 5612 for (i = 0; i < nch; i++) { 5613 struct mlx5e_channel_stats *channel_stats = priv->channel_stats[i]; 5614 5615 /* handle two cases: 5616 * 5617 * 1. channels which are active. In this case, 5618 * report only deactivated TCs on these channels. 5619 * 5620 * 2. channels which were deactivated 5621 * (i > priv->channels.params.num_channels) 5622 * must have all of their TCs [0 .. priv->max_opened_tc) 5623 * examined because deactivated channels will not be in the 5624 * range of [0..real_num_tx_queues) and will not have their 5625 * stats reported by mlx5e_get_queue_stats_tx. 5626 */ 5627 if (i < priv->channels.params.num_channels) 5628 tc = mlx5e_get_dcb_num_tc(&priv->channels.params); 5629 else 5630 tc = 0; 5631 5632 for (; tc < priv->max_opened_tc; tc++) { 5633 struct mlx5e_sq_stats *sq_stats = &channel_stats->sq[tc]; 5634 5635 tx->packets += sq_stats->packets; 5636 tx->bytes += sq_stats->bytes; 5637 tx->hw_gso_packets += sq_stats->tso_packets + 5638 sq_stats->tso_inner_packets; 5639 tx->hw_gso_bytes += sq_stats->tso_bytes + 5640 sq_stats->tso_inner_bytes; 5641 tx->csum_none += sq_stats->csum_none; 5642 tx->stop += sq_stats->stopped; 5643 tx->wake += sq_stats->wake; 5644 } 5645 } 5646 5647 /* if PTP TX was opened at some point and has since either: 5648 * - been shutdown and set to NULL, or 5649 * - simply disabled (bit unset) 5650 * 5651 * report stats directly from the ptp_stats structures as these queues 5652 * are now unavailable and there is no txq index to retrieve these 5653 * stats via calls to mlx5e_get_queue_stats_tx. 5654 */ 5655 ptp_channel = priv->channels.ptp; 5656 if (priv->tx_ptp_opened && (!ptp_channel || !test_bit(MLX5E_PTP_STATE_TX, ptp_channel->state))) { 5657 for (tc = 0; tc < priv->max_opened_tc; tc++) { 5658 struct mlx5e_sq_stats *sq_stats = &priv->ptp_stats.sq[tc]; 5659 5660 tx->packets += sq_stats->packets; 5661 tx->bytes += sq_stats->bytes; 5662 tx->hw_gso_packets += sq_stats->tso_packets + 5663 sq_stats->tso_inner_packets; 5664 tx->hw_gso_bytes += sq_stats->tso_bytes + 5665 sq_stats->tso_inner_bytes; 5666 tx->csum_none += sq_stats->csum_none; 5667 tx->stop += sq_stats->stopped; 5668 tx->wake += sq_stats->wake; 5669 } 5670 } 5671 } 5672 5673 static const struct netdev_stat_ops mlx5e_stat_ops = { 5674 .get_queue_stats_rx = mlx5e_get_queue_stats_rx, 5675 .get_queue_stats_tx = mlx5e_get_queue_stats_tx, 5676 .get_base_stats = mlx5e_get_base_stats, 5677 }; 5678 5679 struct mlx5_qmgmt_data { 5680 struct mlx5e_channel *c; 5681 }; 5682 5683 static void mlx5e_queue_default_qcfg(struct net_device *dev, 5684 struct netdev_queue_config *qcfg) 5685 { 5686 qcfg->rx_page_size = PAGE_SIZE; 5687 } 5688 5689 static int mlx5e_queue_validate_qcfg(struct net_device *dev, 5690 struct netdev_queue_config *qcfg, 5691 struct netlink_ext_ack *extack) 5692 { 5693 struct mlx5e_priv *priv = netdev_priv(dev); 5694 struct mlx5_core_dev *mdev = priv->mdev; 5695 u32 max; 5696 5697 if (!is_power_of_2(qcfg->rx_page_size)) { 5698 netdev_err(priv->netdev, "rx_page_size not power of 2: %u", 5699 qcfg->rx_page_size); 5700 return -EINVAL; 5701 } 5702 5703 max = mlx5e_mpwrq_max_page_size(mdev); 5704 if (qcfg->rx_page_size < PAGE_SIZE || qcfg->rx_page_size > max) { 5705 netdev_err(priv->netdev, 5706 "Selected rx_page_size %u not in supported range [%lu, %u]\n", 5707 qcfg->rx_page_size, PAGE_SIZE, max); 5708 return -ERANGE; 5709 } 5710 5711 return 0; 5712 } 5713 5714 static bool mlx5e_queue_validate_page_size(struct net_device *dev, 5715 struct netdev_queue_config *qcfg, 5716 int queue_index) 5717 { 5718 if (qcfg->rx_page_size == PAGE_SIZE) 5719 return true; 5720 5721 if (!netif_rxq_has_unreadable_mp(dev, queue_index)) 5722 return false; 5723 5724 return true; 5725 } 5726 5727 static int mlx5e_queue_mem_alloc(struct net_device *dev, 5728 struct netdev_queue_config *qcfg, 5729 void *newq, int queue_index) 5730 { 5731 struct mlx5_qmgmt_data *new = (struct mlx5_qmgmt_data *)newq; 5732 struct mlx5e_priv *priv = netdev_priv(dev); 5733 struct mlx5e_channels *chs = &priv->channels; 5734 struct mlx5e_params params = chs->params; 5735 int err; 5736 5737 mutex_lock(&priv->state_lock); 5738 if (!test_bit(MLX5E_STATE_OPENED, &priv->state)) { 5739 err = -ENODEV; 5740 goto unlock; 5741 } 5742 5743 if (queue_index >= chs->num) { 5744 err = -ERANGE; 5745 goto unlock; 5746 } 5747 5748 if (MLX5E_GET_PFLAG(&chs->params, MLX5E_PFLAG_TX_PORT_TS) || 5749 chs->params.ptp_rx || 5750 chs->params.xdp_prog || 5751 priv->htb) { 5752 netdev_err(priv->netdev, 5753 "Cloning channels with Port/rx PTP, XDP or HTB is not supported\n"); 5754 err = -EOPNOTSUPP; 5755 goto unlock; 5756 } 5757 5758 if (!mlx5e_queue_validate_page_size(dev, qcfg, queue_index)) { 5759 netdev_err(priv->netdev, "High order pages are supported only in Zero-Copy mode\n"); 5760 err = -EINVAL; 5761 goto unlock; 5762 } 5763 5764 err = mlx5e_open_channel(priv, queue_index, ¶ms, qcfg, NULL, 5765 &new->c); 5766 unlock: 5767 mutex_unlock(&priv->state_lock); 5768 return err; 5769 } 5770 5771 static void mlx5e_queue_mem_free(struct net_device *dev, void *mem) 5772 { 5773 struct mlx5_qmgmt_data *data = (struct mlx5_qmgmt_data *)mem; 5774 5775 /* not supposed to happen since mlx5e_queue_start never fails 5776 * but this is how this should be implemented just in case 5777 */ 5778 if (data->c) 5779 mlx5e_close_channel(data->c); 5780 } 5781 5782 static int mlx5e_queue_stop(struct net_device *dev, void *oldq, int queue_index) 5783 { 5784 /* In mlx5 a txq cannot be simply stopped in isolation, only restarted. 5785 * mlx5e_queue_start does not fail, we stop the old queue there. 5786 * TODO: Improve this. 5787 */ 5788 return 0; 5789 } 5790 5791 static int mlx5e_queue_start(struct net_device *dev, 5792 struct netdev_queue_config *qcfg, 5793 void *newq, int queue_index) 5794 { 5795 struct mlx5_qmgmt_data *new = (struct mlx5_qmgmt_data *)newq; 5796 struct mlx5e_priv *priv = netdev_priv(dev); 5797 struct mlx5e_channel *old; 5798 5799 mutex_lock(&priv->state_lock); 5800 5801 /* stop and close the old */ 5802 old = priv->channels.c[queue_index]; 5803 mlx5e_deactivate_priv_channels(priv); 5804 /* close old before activating new, to avoid napi conflict */ 5805 mlx5e_close_channel(old); 5806 5807 /* start the new */ 5808 priv->channels.c[queue_index] = new->c; 5809 mlx5e_activate_priv_channels(priv); 5810 mutex_unlock(&priv->state_lock); 5811 return 0; 5812 } 5813 5814 static struct device *mlx5e_queue_get_dma_dev(struct net_device *dev, 5815 int queue_index) 5816 { 5817 struct mlx5e_priv *priv = netdev_priv(dev); 5818 struct mlx5e_channels *channels; 5819 struct device *pdev = NULL; 5820 struct mlx5e_channel *ch; 5821 5822 channels = &priv->channels; 5823 5824 mutex_lock(&priv->state_lock); 5825 5826 if (queue_index >= channels->num) 5827 goto out; 5828 5829 ch = channels->c[queue_index]; 5830 pdev = ch->pdev; 5831 out: 5832 mutex_unlock(&priv->state_lock); 5833 5834 return pdev; 5835 } 5836 5837 static const struct netdev_queue_mgmt_ops mlx5e_queue_mgmt_ops = { 5838 .ndo_queue_mem_size = sizeof(struct mlx5_qmgmt_data), 5839 .ndo_queue_mem_alloc = mlx5e_queue_mem_alloc, 5840 .ndo_queue_mem_free = mlx5e_queue_mem_free, 5841 .ndo_queue_start = mlx5e_queue_start, 5842 .ndo_queue_stop = mlx5e_queue_stop, 5843 .ndo_queue_get_dma_dev = mlx5e_queue_get_dma_dev, 5844 .ndo_default_qcfg = mlx5e_queue_default_qcfg, 5845 .ndo_validate_qcfg = mlx5e_queue_validate_qcfg, 5846 .supported_params = QCFG_RX_PAGE_SIZE, 5847 }; 5848 5849 static void mlx5e_build_nic_netdev(struct net_device *netdev) 5850 { 5851 struct mlx5e_priv *priv = netdev_priv(netdev); 5852 struct mlx5_core_dev *mdev = priv->mdev; 5853 bool fcs_supported; 5854 bool fcs_enabled; 5855 5856 SET_NETDEV_DEV(netdev, mdev->device); 5857 5858 netdev->netdev_ops = &mlx5e_netdev_ops; 5859 netdev->queue_mgmt_ops = &mlx5e_queue_mgmt_ops; 5860 netdev->xdp_metadata_ops = &mlx5e_xdp_metadata_ops; 5861 netdev->xsk_tx_metadata_ops = &mlx5e_xsk_tx_metadata_ops; 5862 netdev->request_ops_lock = true; 5863 netdev_lockdep_set_classes(netdev); 5864 5865 mlx5e_dcbnl_build_netdev(netdev); 5866 5867 netdev->watchdog_timeo = 15 * HZ; 5868 5869 netdev->stat_ops = &mlx5e_stat_ops; 5870 netdev->ethtool_ops = &mlx5e_ethtool_ops; 5871 5872 netdev->vlan_features |= NETIF_F_SG; 5873 netdev->vlan_features |= NETIF_F_HW_CSUM; 5874 netdev->vlan_features |= NETIF_F_GRO; 5875 netdev->vlan_features |= NETIF_F_TSO; 5876 netdev->vlan_features |= NETIF_F_TSO6; 5877 netdev->vlan_features |= NETIF_F_RXCSUM; 5878 netdev->vlan_features |= NETIF_F_RXHASH; 5879 netdev->vlan_features |= NETIF_F_GSO_PARTIAL; 5880 5881 netdev->mpls_features |= NETIF_F_SG; 5882 netdev->mpls_features |= NETIF_F_HW_CSUM; 5883 netdev->mpls_features |= NETIF_F_TSO; 5884 netdev->mpls_features |= NETIF_F_TSO6; 5885 5886 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_TX; 5887 netdev->hw_enc_features |= NETIF_F_HW_VLAN_CTAG_RX; 5888 5889 /* Tunneled LRO is not supported in the driver, and the same RQs are 5890 * shared between inner and outer TIRs, so the driver can't disable LRO 5891 * for inner TIRs while having it enabled for outer TIRs. Due to this, 5892 * block LRO altogether if the firmware declares tunneled LRO support. 5893 */ 5894 if (!!MLX5_CAP_ETH(mdev, lro_cap) && 5895 !MLX5_CAP_ETH(mdev, tunnel_lro_vxlan) && 5896 !MLX5_CAP_ETH(mdev, tunnel_lro_gre) && 5897 mlx5e_check_fragmented_striding_rq_cap(mdev, PAGE_SHIFT, 5898 MLX5E_MPWRQ_UMR_MODE_ALIGNED)) 5899 netdev->vlan_features |= NETIF_F_LRO; 5900 5901 if (mlx5e_hw_gro_supported(mdev) && 5902 mlx5e_check_fragmented_striding_rq_cap(mdev, PAGE_SHIFT, 5903 MLX5E_MPWRQ_UMR_MODE_ALIGNED)) 5904 netdev->vlan_features |= NETIF_F_GRO_HW; 5905 5906 netdev->hw_features = netdev->vlan_features; 5907 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX; 5908 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX; 5909 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER; 5910 netdev->hw_features |= NETIF_F_HW_VLAN_STAG_TX; 5911 5912 if (mlx5e_tunnel_any_tx_proto_supported(mdev)) { 5913 netdev->hw_enc_features |= NETIF_F_HW_CSUM; 5914 netdev->hw_enc_features |= NETIF_F_TSO; 5915 netdev->hw_enc_features |= NETIF_F_TSO6; 5916 netdev->hw_enc_features |= NETIF_F_GSO_PARTIAL; 5917 } 5918 5919 if (mlx5_vxlan_allowed(mdev->vxlan) || mlx5_geneve_tx_allowed(mdev)) { 5920 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL | 5921 NETIF_F_GSO_UDP_TUNNEL_CSUM; 5922 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL | 5923 NETIF_F_GSO_UDP_TUNNEL_CSUM; 5924 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM; 5925 netdev->vlan_features |= NETIF_F_GSO_UDP_TUNNEL | 5926 NETIF_F_GSO_UDP_TUNNEL_CSUM; 5927 } 5928 5929 if (mlx5e_tunnel_proto_supported_tx(mdev, IPPROTO_GRE)) { 5930 netdev->hw_features |= NETIF_F_GSO_GRE | 5931 NETIF_F_GSO_GRE_CSUM; 5932 netdev->hw_enc_features |= NETIF_F_GSO_GRE | 5933 NETIF_F_GSO_GRE_CSUM; 5934 netdev->gso_partial_features |= NETIF_F_GSO_GRE_CSUM; 5935 netdev->vlan_features |= NETIF_F_GSO_GRE | NETIF_F_GSO_GRE_CSUM; 5936 } 5937 5938 if (mlx5e_tunnel_proto_supported_tx(mdev, IPPROTO_IPIP)) { 5939 netdev->hw_features |= NETIF_F_GSO_IPXIP4 | 5940 NETIF_F_GSO_IPXIP6; 5941 netdev->hw_enc_features |= NETIF_F_GSO_IPXIP4 | 5942 NETIF_F_GSO_IPXIP6; 5943 netdev->gso_partial_features |= NETIF_F_GSO_IPXIP4 | 5944 NETIF_F_GSO_IPXIP6; 5945 } 5946 5947 netdev->gso_partial_features |= NETIF_F_GSO_UDP_L4; 5948 netdev->hw_features |= NETIF_F_GSO_UDP_L4; 5949 netdev->hw_enc_features |= NETIF_F_GSO_UDP_L4; 5950 5951 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled); 5952 5953 if (fcs_supported) 5954 netdev->hw_features |= NETIF_F_RXALL; 5955 5956 if (MLX5_CAP_ETH(mdev, scatter_fcs)) 5957 netdev->hw_features |= NETIF_F_RXFCS; 5958 5959 if (mlx5_qos_is_supported(mdev)) 5960 netdev->hw_features |= NETIF_F_HW_TC; 5961 5962 netdev->features = netdev->hw_features; 5963 5964 /* Defaults */ 5965 if (fcs_enabled) 5966 netdev->features &= ~NETIF_F_RXALL; 5967 netdev->features &= ~NETIF_F_LRO; 5968 netdev->features &= ~NETIF_F_GRO_HW; 5969 netdev->features &= ~NETIF_F_RXFCS; 5970 5971 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f) 5972 if (FT_CAP(flow_modify_en) && 5973 FT_CAP(modify_root) && 5974 FT_CAP(identified_miss_table_mode) && 5975 FT_CAP(flow_table_modify)) { 5976 #if IS_ENABLED(CONFIG_MLX5_CLS_ACT) 5977 netdev->hw_features |= NETIF_F_HW_TC; 5978 #endif 5979 #if IS_ENABLED(CONFIG_MLX5_EN_ARFS) 5980 netdev->hw_features |= NETIF_F_NTUPLE; 5981 #elif IS_ENABLED(CONFIG_MLX5_EN_RXNFC) 5982 netdev->features |= NETIF_F_NTUPLE; 5983 #endif 5984 } 5985 5986 netdev->features |= NETIF_F_HIGHDMA; 5987 netdev->features |= NETIF_F_HW_VLAN_STAG_FILTER; 5988 5989 netdev->priv_flags |= IFF_UNICAST_FLT; 5990 5991 netdev->netmem_tx = NETMEM_TX_DMA; 5992 5993 netif_set_tso_max_size(netdev, GSO_MAX_SIZE); 5994 mlx5e_set_xdp_feature(priv); 5995 mlx5e_set_netdev_dev_addr(netdev); 5996 mlx5e_macsec_build_netdev(priv); 5997 mlx5e_ipsec_build_netdev(priv); 5998 mlx5e_ktls_build_netdev(priv); 5999 } 6000 6001 void mlx5e_create_q_counters(struct mlx5e_priv *priv) 6002 { 6003 u32 out[MLX5_ST_SZ_DW(alloc_q_counter_out)] = {}; 6004 u32 in[MLX5_ST_SZ_DW(alloc_q_counter_in)] = {}; 6005 struct mlx5_core_dev *mdev = priv->mdev; 6006 struct mlx5_core_dev *pos; 6007 int err, i; 6008 6009 MLX5_SET(alloc_q_counter_in, in, opcode, MLX5_CMD_OP_ALLOC_Q_COUNTER); 6010 6011 mlx5_sd_for_each_dev(i, mdev, pos) { 6012 err = mlx5_cmd_exec_inout(pos, alloc_q_counter, in, out); 6013 if (!err) 6014 priv->q_counter[i] = 6015 MLX5_GET(alloc_q_counter_out, out, counter_set_id); 6016 } 6017 6018 err = mlx5_cmd_exec_inout(mdev, alloc_q_counter, in, out); 6019 if (!err) 6020 priv->drop_rq_q_counter = 6021 MLX5_GET(alloc_q_counter_out, out, counter_set_id); 6022 } 6023 6024 void mlx5e_destroy_q_counters(struct mlx5e_priv *priv) 6025 { 6026 u32 in[MLX5_ST_SZ_DW(dealloc_q_counter_in)] = {}; 6027 struct mlx5_core_dev *pos; 6028 int i; 6029 6030 MLX5_SET(dealloc_q_counter_in, in, opcode, 6031 MLX5_CMD_OP_DEALLOC_Q_COUNTER); 6032 mlx5_sd_for_each_dev(i, priv->mdev, pos) { 6033 if (priv->q_counter[i]) { 6034 MLX5_SET(dealloc_q_counter_in, in, counter_set_id, 6035 priv->q_counter[i]); 6036 mlx5_cmd_exec_in(pos, dealloc_q_counter, in); 6037 } 6038 } 6039 6040 if (priv->drop_rq_q_counter) { 6041 MLX5_SET(dealloc_q_counter_in, in, counter_set_id, 6042 priv->drop_rq_q_counter); 6043 mlx5_cmd_exec_in(priv->mdev, dealloc_q_counter, in); 6044 } 6045 } 6046 6047 static int mlx5e_nic_init(struct mlx5_core_dev *mdev, 6048 struct net_device *netdev) 6049 { 6050 const bool take_rtnl = netdev->reg_state == NETREG_REGISTERED; 6051 struct mlx5e_priv *priv = netdev_priv(netdev); 6052 struct mlx5e_flow_steering *fs; 6053 int err; 6054 6055 mlx5e_build_nic_params(priv, &priv->xsk, netdev->mtu); 6056 mlx5e_vxlan_set_netdev_info(priv); 6057 6058 mlx5e_timestamp_init(priv); 6059 6060 priv->dfs_root = debugfs_create_dir("nic", 6061 mlx5_debugfs_get_dev_root(mdev)); 6062 6063 fs = mlx5e_fs_init(priv->profile, mdev, false, priv->dfs_root); 6064 if (!fs) { 6065 err = -ENOMEM; 6066 mlx5_core_err(mdev, "FS initialization failed, %d\n", err); 6067 debugfs_remove_recursive(priv->dfs_root); 6068 return err; 6069 } 6070 priv->fs = fs; 6071 6072 err = mlx5e_psp_init(priv); 6073 if (err) 6074 mlx5_core_err(mdev, "PSP initialization failed, %d\n", err); 6075 6076 err = mlx5e_ktls_init(priv); 6077 if (err) 6078 mlx5_core_err(mdev, "TLS initialization failed, %d\n", err); 6079 6080 mlx5e_health_create_reporters(priv); 6081 6082 /* If netdev is already registered (e.g. move from uplink to nic profile), 6083 * RTNL lock must be held before triggering netdev notifiers. 6084 */ 6085 if (take_rtnl) 6086 rtnl_lock(); 6087 6088 /* update XDP supported features */ 6089 mlx5e_set_xdp_feature(priv); 6090 6091 if (take_rtnl) 6092 rtnl_unlock(); 6093 6094 return 0; 6095 } 6096 6097 static void mlx5e_nic_cleanup(struct mlx5e_priv *priv) 6098 { 6099 mlx5e_health_destroy_reporters(priv); 6100 mlx5e_ktls_cleanup(priv); 6101 mlx5e_psp_cleanup(priv); 6102 mlx5e_fs_cleanup(priv->fs); 6103 debugfs_remove_recursive(priv->dfs_root); 6104 priv->fs = NULL; 6105 } 6106 6107 static int mlx5e_init_nic_rx(struct mlx5e_priv *priv) 6108 { 6109 struct mlx5_core_dev *mdev = priv->mdev; 6110 enum mlx5e_rx_res_features features; 6111 int err; 6112 6113 mlx5e_create_q_counters(priv); 6114 6115 err = mlx5e_open_drop_rq(priv, &priv->drop_rq); 6116 if (err) { 6117 mlx5_core_err(mdev, "open drop rq failed, %d\n", err); 6118 goto err_destroy_q_counters; 6119 } 6120 6121 features = MLX5E_RX_RES_FEATURE_PTP; 6122 if (mlx5_tunnel_inner_ft_supported(mdev)) 6123 features |= MLX5E_RX_RES_FEATURE_INNER_FT; 6124 if (mlx5_get_sd(priv->mdev)) 6125 features |= MLX5E_RX_RES_FEATURE_MULTI_VHCA; 6126 6127 priv->rx_res = mlx5e_rx_res_create(priv->mdev, features, priv->max_nch, priv->drop_rq.rqn, 6128 &priv->channels.params.packet_merge, 6129 priv->channels.params.num_channels); 6130 if (IS_ERR(priv->rx_res)) { 6131 err = PTR_ERR(priv->rx_res); 6132 priv->rx_res = NULL; 6133 mlx5_core_err(mdev, "create rx resources failed, %d\n", err); 6134 goto err_close_drop_rq; 6135 } 6136 6137 err = mlx5e_create_flow_steering(priv->fs, priv->rx_res, priv->profile, 6138 priv->netdev); 6139 if (err) { 6140 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err); 6141 goto err_destroy_rx_res; 6142 } 6143 6144 err = mlx5e_tc_nic_init(priv); 6145 if (err) 6146 goto err_destroy_flow_steering; 6147 6148 err = mlx5e_accel_init_rx(priv); 6149 if (err) 6150 goto err_tc_nic_cleanup; 6151 6152 #ifdef CONFIG_MLX5_EN_ARFS 6153 priv->netdev->rx_cpu_rmap = mlx5_eq_table_get_rmap(priv->mdev); 6154 #endif 6155 6156 return 0; 6157 6158 err_tc_nic_cleanup: 6159 mlx5e_tc_nic_cleanup(priv); 6160 err_destroy_flow_steering: 6161 mlx5e_destroy_flow_steering(priv->fs, mlx5e_fs_has_arfs(priv->netdev), 6162 priv->profile); 6163 err_destroy_rx_res: 6164 mlx5e_rx_res_destroy(priv->rx_res); 6165 priv->rx_res = NULL; 6166 err_close_drop_rq: 6167 mlx5e_close_drop_rq(&priv->drop_rq); 6168 err_destroy_q_counters: 6169 mlx5e_destroy_q_counters(priv); 6170 return err; 6171 } 6172 6173 static void mlx5e_cleanup_nic_rx(struct mlx5e_priv *priv) 6174 { 6175 mlx5e_accel_cleanup_rx(priv); 6176 mlx5e_tc_nic_cleanup(priv); 6177 mlx5e_destroy_flow_steering(priv->fs, mlx5e_fs_has_arfs(priv->netdev), 6178 priv->profile); 6179 mlx5e_rx_res_destroy(priv->rx_res); 6180 priv->rx_res = NULL; 6181 mlx5e_close_drop_rq(&priv->drop_rq); 6182 mlx5e_destroy_q_counters(priv); 6183 } 6184 6185 static void mlx5e_set_mqprio_rl(struct mlx5e_priv *priv) 6186 { 6187 struct mlx5e_params *params; 6188 struct mlx5e_mqprio_rl *rl; 6189 6190 params = &priv->channels.params; 6191 if (params->mqprio.mode != TC_MQPRIO_MODE_CHANNEL) 6192 return; 6193 6194 rl = mlx5e_mqprio_rl_create(priv->mdev, params->mqprio.num_tc, 6195 params->mqprio.channel.max_rate); 6196 if (IS_ERR(rl)) 6197 rl = NULL; 6198 priv->mqprio_rl = rl; 6199 mlx5e_mqprio_rl_update_params(params, rl); 6200 } 6201 6202 static int mlx5e_init_nic_tx(struct mlx5e_priv *priv) 6203 { 6204 int err; 6205 6206 err = mlx5e_accel_init_tx(priv); 6207 if (err) 6208 return err; 6209 6210 mlx5e_set_mqprio_rl(priv); 6211 mlx5e_dcbnl_initialize(priv); 6212 return 0; 6213 } 6214 6215 static int mlx5e_nic_enable(struct mlx5e_priv *priv) 6216 { 6217 struct net_device *netdev = priv->netdev; 6218 struct mlx5_core_dev *mdev = priv->mdev; 6219 int err; 6220 6221 mlx5e_fs_init_l2_addr(priv->fs, netdev); 6222 mlx5e_ipsec_init(priv); 6223 err = mlx5e_psp_register(priv); 6224 if (err) 6225 goto out_ipsec_cleanup; 6226 6227 err = mlx5e_macsec_init(priv); 6228 if (err) 6229 mlx5_core_err(mdev, "MACsec initialization failed, %d\n", err); 6230 6231 /* Marking the link as currently not needed by the Driver */ 6232 if (!netif_running(netdev)) 6233 mlx5e_modify_admin_state(mdev, MLX5_PORT_DOWN); 6234 6235 mlx5e_set_netdev_mtu_boundaries(priv); 6236 mlx5e_set_dev_port_mtu(priv); 6237 6238 mlx5_lag_add_netdev(mdev, netdev); 6239 6240 mlx5e_enable_async_events(priv); 6241 mlx5e_enable_blocking_events(priv); 6242 if (mlx5e_monitor_counter_supported(priv)) 6243 mlx5e_monitor_counter_init(priv); 6244 6245 mlx5e_pcie_cong_event_init(priv); 6246 mlx5e_hv_vhca_stats_create(priv); 6247 if (netdev->reg_state != NETREG_REGISTERED) 6248 return 0; 6249 mlx5e_dcbnl_init_app(priv); 6250 6251 mlx5e_nic_set_rx_mode(priv); 6252 6253 rtnl_lock(); 6254 netdev_lock(netdev); 6255 if (netif_running(netdev)) 6256 mlx5e_open(netdev); 6257 udp_tunnel_nic_reset_ntf(priv->netdev); 6258 netdev_unlock(netdev); 6259 netif_device_attach(netdev); 6260 rtnl_unlock(); 6261 6262 return 0; 6263 6264 out_ipsec_cleanup: 6265 mlx5e_ipsec_cleanup(priv); 6266 return err; 6267 } 6268 6269 static void mlx5e_nic_disable(struct mlx5e_priv *priv) 6270 { 6271 struct mlx5_core_dev *mdev = priv->mdev; 6272 6273 if (priv->netdev->reg_state == NETREG_REGISTERED) 6274 mlx5e_dcbnl_delete_app(priv); 6275 6276 rtnl_lock(); 6277 netdev_lock(priv->netdev); 6278 if (netif_running(priv->netdev)) 6279 mlx5e_close(priv->netdev); 6280 netif_device_detach(priv->netdev); 6281 if (priv->en_trap) { 6282 mlx5e_deactivate_trap(priv); 6283 mlx5e_close_trap(priv->en_trap); 6284 priv->en_trap = NULL; 6285 } 6286 netdev_unlock(priv->netdev); 6287 rtnl_unlock(); 6288 6289 mlx5e_nic_set_rx_mode(priv); 6290 6291 mlx5e_pcie_cong_event_cleanup(priv); 6292 mlx5e_hv_vhca_stats_destroy(priv); 6293 if (mlx5e_monitor_counter_supported(priv)) 6294 mlx5e_monitor_counter_cleanup(priv); 6295 6296 mlx5e_ipsec_disable_events(priv); 6297 mlx5e_disable_blocking_events(priv); 6298 mlx5e_disable_async_events(priv); 6299 mlx5_lag_remove_netdev(mdev, priv->netdev); 6300 mlx5_vxlan_reset_to_default(mdev->vxlan); 6301 mlx5e_macsec_cleanup(priv); 6302 mlx5e_psp_unregister(priv); 6303 mlx5e_ipsec_cleanup(priv); 6304 } 6305 6306 static int mlx5e_update_nic_rx(struct mlx5e_priv *priv) 6307 { 6308 return mlx5e_refresh_tirs(priv->mdev, false, false); 6309 } 6310 6311 static const struct mlx5e_profile mlx5e_nic_profile = { 6312 .init = mlx5e_nic_init, 6313 .cleanup = mlx5e_nic_cleanup, 6314 .init_rx = mlx5e_init_nic_rx, 6315 .cleanup_rx = mlx5e_cleanup_nic_rx, 6316 .init_tx = mlx5e_init_nic_tx, 6317 .cleanup_tx = mlx5e_cleanup_nic_tx, 6318 .enable = mlx5e_nic_enable, 6319 .disable = mlx5e_nic_disable, 6320 .update_rx = mlx5e_update_nic_rx, 6321 .update_stats = mlx5e_stats_update_ndo_stats, 6322 .update_carrier = mlx5e_update_carrier, 6323 .rx_handlers = &mlx5e_rx_handlers_nic, 6324 .max_tc = MLX5_MAX_NUM_TC, 6325 .stats_grps = mlx5e_nic_stats_grps, 6326 .stats_grps_num = mlx5e_nic_stats_grps_num, 6327 .features = BIT(MLX5E_PROFILE_FEATURE_PTP_RX) | 6328 BIT(MLX5E_PROFILE_FEATURE_PTP_TX) | 6329 BIT(MLX5E_PROFILE_FEATURE_QOS_HTB) | 6330 BIT(MLX5E_PROFILE_FEATURE_FS_VLAN) | 6331 BIT(MLX5E_PROFILE_FEATURE_FS_TC), 6332 }; 6333 6334 static int mlx5e_profile_max_num_channels(struct mlx5_core_dev *mdev, 6335 const struct mlx5e_profile *profile) 6336 { 6337 int nch; 6338 6339 nch = mlx5e_get_max_num_channels(mdev); 6340 6341 if (profile->max_nch_limit) 6342 nch = min_t(int, nch, profile->max_nch_limit(mdev)); 6343 return nch; 6344 } 6345 6346 static unsigned int 6347 mlx5e_calc_max_nch(struct mlx5_core_dev *mdev, struct net_device *netdev, 6348 const struct mlx5e_profile *profile) 6349 6350 { 6351 unsigned int max_nch, tmp; 6352 6353 /* core resources */ 6354 max_nch = mlx5e_profile_max_num_channels(mdev, profile); 6355 6356 /* netdev rx queues */ 6357 max_nch = min_t(unsigned int, max_nch, netdev->num_rx_queues); 6358 6359 /* netdev tx queues */ 6360 tmp = netdev->num_tx_queues; 6361 if (mlx5_qos_is_supported(mdev)) 6362 tmp -= mlx5e_qos_max_leaf_nodes(mdev); 6363 if (MLX5_CAP_GEN(mdev, ts_cqe_to_dest_cqn)) 6364 tmp -= profile->max_tc; 6365 tmp = tmp / profile->max_tc; 6366 max_nch = min_t(unsigned int, max_nch, tmp); 6367 6368 return max_nch; 6369 } 6370 6371 int mlx5e_get_pf_num_tirs(struct mlx5_core_dev *mdev) 6372 { 6373 /* Indirect TIRS: 2 sets of TTCs (inner + outer steering) 6374 * and 1 set of direct TIRS 6375 */ 6376 return 2 * MLX5E_NUM_INDIR_TIRS 6377 + mlx5e_profile_max_num_channels(mdev, &mlx5e_nic_profile); 6378 } 6379 6380 void mlx5e_set_rx_mode_work(struct work_struct *work) 6381 { 6382 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv, 6383 set_rx_mode_work); 6384 struct net_device *dev = priv->netdev; 6385 6386 netdev_lock_ops(dev); 6387 mlx5e_fs_set_rx_mode_work(priv->fs, dev, NULL, NULL); 6388 netdev_unlock_ops(dev); 6389 } 6390 6391 /* mlx5e generic netdev management API (move to en_common.c) */ 6392 int mlx5e_priv_init(struct mlx5e_priv *priv, 6393 const struct mlx5e_profile *profile, 6394 struct net_device *netdev, 6395 struct mlx5_core_dev *mdev) 6396 { 6397 int nch, num_txqs, node; 6398 int err; 6399 6400 num_txqs = netdev->num_tx_queues; 6401 nch = mlx5e_calc_max_nch(mdev, netdev, profile); 6402 node = dev_to_node(mlx5_core_dma_dev(mdev)); 6403 6404 /* priv init */ 6405 WRITE_ONCE(priv->mdev, mdev); 6406 priv->netdev = netdev; 6407 priv->max_nch = nch; 6408 priv->max_opened_tc = 1; 6409 6410 if (!alloc_cpumask_var(&priv->scratchpad.cpumask, GFP_KERNEL)) 6411 return -ENOMEM; 6412 6413 mutex_init(&priv->state_lock); 6414 6415 err = mlx5e_selq_init(&priv->selq, &priv->state_lock); 6416 if (err) 6417 goto err_free_cpumask; 6418 6419 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work); 6420 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work); 6421 INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work); 6422 INIT_WORK(&priv->update_stats_work, mlx5e_update_stats_work); 6423 6424 priv->wq = create_singlethread_workqueue("mlx5e"); 6425 if (!priv->wq) 6426 goto err_free_selq; 6427 6428 priv->txq2sq = kcalloc_node(num_txqs, sizeof(*priv->txq2sq), GFP_KERNEL, node); 6429 if (!priv->txq2sq) 6430 goto err_destroy_workqueue; 6431 6432 priv->txq2sq_stats = kcalloc_node(num_txqs, sizeof(*priv->txq2sq_stats), GFP_KERNEL, node); 6433 if (!priv->txq2sq_stats) 6434 goto err_free_txq2sq; 6435 6436 priv->tx_rates = kcalloc_node(num_txqs, sizeof(*priv->tx_rates), GFP_KERNEL, node); 6437 if (!priv->tx_rates) 6438 goto err_free_txq2sq_stats; 6439 6440 priv->channel_stats = 6441 kcalloc_node(nch, sizeof(*priv->channel_stats), GFP_KERNEL, node); 6442 if (!priv->channel_stats) 6443 goto err_free_tx_rates; 6444 6445 return 0; 6446 6447 err_free_tx_rates: 6448 kfree(priv->tx_rates); 6449 err_free_txq2sq_stats: 6450 kfree(priv->txq2sq_stats); 6451 err_free_txq2sq: 6452 kfree(priv->txq2sq); 6453 err_destroy_workqueue: 6454 destroy_workqueue(priv->wq); 6455 err_free_selq: 6456 mlx5e_selq_cleanup(&priv->selq); 6457 err_free_cpumask: 6458 free_cpumask_var(priv->scratchpad.cpumask); 6459 return -ENOMEM; 6460 } 6461 6462 void mlx5e_priv_cleanup(struct mlx5e_priv *priv) 6463 { 6464 bool destroying = test_bit(MLX5E_STATE_DESTROYING, &priv->state); 6465 int i; 6466 6467 /* bail if change profile failed and also rollback failed */ 6468 if (!priv->mdev) 6469 return; 6470 6471 for (i = 0; i < priv->stats_nch; i++) 6472 kvfree(priv->channel_stats[i]); 6473 kfree(priv->channel_stats); 6474 kfree(priv->tx_rates); 6475 kfree(priv->txq2sq_stats); 6476 kfree(priv->txq2sq); 6477 destroy_workqueue(priv->wq); 6478 mlx5e_selq_cleanup(&priv->selq); 6479 free_cpumask_var(priv->scratchpad.cpumask); 6480 6481 for (i = 0; i < priv->htb_max_qos_sqs; i++) 6482 kfree(priv->htb_qos_sq_stats[i]); 6483 kvfree(priv->htb_qos_sq_stats); 6484 6485 if (priv->mqprio_rl) { 6486 mlx5e_mqprio_rl_cleanup(priv->mqprio_rl); 6487 mlx5e_mqprio_rl_free(priv->mqprio_rl); 6488 } 6489 6490 /* Clear atomically before memset() for lockless XFRM readers. */ 6491 WRITE_ONCE(priv->mdev, NULL); 6492 memset(priv, 0, sizeof(*priv)); 6493 if (destroying) /* restore destroying bit, to allow unload */ 6494 set_bit(MLX5E_STATE_DESTROYING, &priv->state); 6495 } 6496 6497 static unsigned int mlx5e_get_max_num_txqs(struct mlx5_core_dev *mdev, 6498 const struct mlx5e_profile *profile) 6499 { 6500 unsigned int nch, ptp_txqs, qos_txqs; 6501 6502 nch = mlx5e_profile_max_num_channels(mdev, profile); 6503 6504 ptp_txqs = MLX5_CAP_GEN(mdev, ts_cqe_to_dest_cqn) && 6505 mlx5e_profile_feature_cap(profile, PTP_TX) ? 6506 profile->max_tc : 0; 6507 6508 qos_txqs = mlx5_qos_is_supported(mdev) && 6509 mlx5e_profile_feature_cap(profile, QOS_HTB) ? 6510 mlx5e_qos_max_leaf_nodes(mdev) : 0; 6511 6512 return nch * profile->max_tc + ptp_txqs + qos_txqs; 6513 } 6514 6515 static unsigned int mlx5e_get_max_num_rxqs(struct mlx5_core_dev *mdev, 6516 const struct mlx5e_profile *profile) 6517 { 6518 return mlx5e_profile_max_num_channels(mdev, profile); 6519 } 6520 6521 struct net_device * 6522 mlx5e_create_netdev(struct mlx5_core_dev *mdev, const struct mlx5e_profile *profile) 6523 { 6524 struct net_device *netdev; 6525 unsigned int txqs, rxqs; 6526 int err; 6527 6528 txqs = mlx5e_get_max_num_txqs(mdev, profile); 6529 rxqs = mlx5e_get_max_num_rxqs(mdev, profile); 6530 6531 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv), txqs, rxqs); 6532 if (!netdev) { 6533 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n"); 6534 return NULL; 6535 } 6536 6537 err = mlx5e_priv_init(netdev_priv(netdev), profile, netdev, mdev); 6538 if (err) { 6539 mlx5_core_err(mdev, "mlx5e_priv_init failed, err=%d\n", err); 6540 goto err_free_netdev; 6541 } 6542 6543 netif_carrier_off(netdev); 6544 netif_tx_disable(netdev); 6545 dev_net_set(netdev, mlx5_core_net(mdev)); 6546 6547 return netdev; 6548 6549 err_free_netdev: 6550 free_netdev(netdev); 6551 6552 return NULL; 6553 } 6554 6555 static void mlx5e_update_features(struct net_device *netdev) 6556 { 6557 if (netdev->reg_state != NETREG_REGISTERED) 6558 return; /* features will be updated on netdev registration */ 6559 6560 rtnl_lock(); 6561 netdev_lock(netdev); 6562 netdev_update_features(netdev); 6563 netdev_unlock(netdev); 6564 rtnl_unlock(); 6565 } 6566 6567 static void mlx5e_reset_channels(struct net_device *netdev) 6568 { 6569 netdev_reset_tc(netdev); 6570 } 6571 6572 int mlx5e_attach_netdev(struct mlx5e_priv *priv) 6573 { 6574 const bool need_lock = priv->netdev->reg_state == NETREG_REGISTERED; 6575 const struct mlx5e_profile *profile = priv->profile; 6576 int max_nch; 6577 int err; 6578 6579 clear_bit(MLX5E_STATE_DESTROYING, &priv->state); 6580 6581 /* Validate the max_wqe_size_sq capability. */ 6582 if (WARN_ON_ONCE(mlx5e_get_max_sq_wqebbs(priv->mdev) < MLX5E_MAX_TX_WQEBBS)) { 6583 mlx5_core_warn(priv->mdev, "MLX5E: Max SQ WQEBBs firmware capability: %u, needed %u\n", 6584 mlx5e_get_max_sq_wqebbs(priv->mdev), (unsigned int)MLX5E_MAX_TX_WQEBBS); 6585 return -EIO; 6586 } 6587 6588 /* max number of channels may have changed */ 6589 max_nch = mlx5e_calc_max_nch(priv->mdev, priv->netdev, profile); 6590 6591 /* Locking is required by ethtool_rxfh_indir_lost() (sends 6592 * ETHTOOL_MSG_RSS_NTF) and by netif_set_real_num_*_queues in case 6593 * the netdev has been registered by this point (if this function 6594 * was called in the reload or resume flow). 6595 */ 6596 if (need_lock) { 6597 rtnl_lock(); 6598 netdev_lock(priv->netdev); 6599 } 6600 6601 if (priv->channels.params.num_channels > max_nch) { 6602 mlx5_core_warn(priv->mdev, "MLX5E: Reducing number of channels to %d\n", max_nch); 6603 /* Reducing the number of channels - RXFH has to be reset, and 6604 * mlx5e_num_channels_changed below will build the RQT. 6605 */ 6606 ethtool_rxfh_indir_lost(priv->netdev); 6607 priv->channels.params.num_channels = max_nch; 6608 if (priv->channels.params.mqprio.mode == TC_MQPRIO_MODE_CHANNEL) { 6609 mlx5_core_warn(priv->mdev, "MLX5E: Disabling MQPRIO channel mode\n"); 6610 mlx5e_params_mqprio_reset(&priv->channels.params); 6611 } 6612 } 6613 if (max_nch != priv->max_nch) { 6614 mlx5_core_warn(priv->mdev, 6615 "MLX5E: Updating max number of channels from %u to %u\n", 6616 priv->max_nch, max_nch); 6617 priv->max_nch = max_nch; 6618 } 6619 6620 /* 1. Set the real number of queues in the kernel the first time. 6621 * 2. Set our default XPS cpumask. 6622 * 3. Build the RQT. 6623 */ 6624 err = mlx5e_num_channels_changed(priv); 6625 if (need_lock) { 6626 netdev_unlock(priv->netdev); 6627 rtnl_unlock(); 6628 } 6629 if (err) 6630 goto out; 6631 6632 err = profile->init_tx(priv); 6633 if (err) 6634 goto out; 6635 6636 err = profile->init_rx(priv); 6637 if (err) 6638 goto err_cleanup_tx; 6639 6640 if (priv->fs) 6641 mlx5e_fs_set_rx_mode_active(priv->fs, true); 6642 6643 if (profile->enable) { 6644 err = profile->enable(priv); 6645 if (err) 6646 goto err_cleanup_rx; 6647 } 6648 6649 mlx5e_update_features(priv->netdev); 6650 6651 return 0; 6652 6653 err_cleanup_rx: 6654 if (priv->fs) { 6655 mlx5e_fs_set_rx_mode_active(priv->fs, false); 6656 cancel_work_sync(&priv->set_rx_mode_work); 6657 } 6658 profile->cleanup_rx(priv); 6659 err_cleanup_tx: 6660 profile->cleanup_tx(priv); 6661 6662 out: 6663 mlx5e_reset_channels(priv->netdev); 6664 set_bit(MLX5E_STATE_DESTROYING, &priv->state); 6665 cancel_work_sync(&priv->update_stats_work); 6666 return err; 6667 } 6668 6669 void mlx5e_detach_netdev(struct mlx5e_priv *priv) 6670 { 6671 const struct mlx5e_profile *profile = priv->profile; 6672 6673 set_bit(MLX5E_STATE_DESTROYING, &priv->state); 6674 if (priv->fs) 6675 mlx5e_fs_set_rx_mode_active(priv->fs, false); 6676 6677 if (profile->disable) 6678 profile->disable(priv); 6679 flush_workqueue(priv->wq); 6680 6681 profile->cleanup_rx(priv); 6682 profile->cleanup_tx(priv); 6683 mlx5e_reset_channels(priv->netdev); 6684 cancel_work_sync(&priv->update_stats_work); 6685 } 6686 6687 static int 6688 mlx5e_netdev_init_profile(struct net_device *netdev, struct mlx5_core_dev *mdev, 6689 const struct mlx5e_profile *new_profile, void *new_ppriv) 6690 { 6691 struct mlx5e_priv *priv = netdev_priv(netdev); 6692 int err; 6693 6694 err = mlx5e_priv_init(priv, new_profile, netdev, mdev); 6695 if (err) { 6696 mlx5_core_err(mdev, "mlx5e_priv_init failed, err=%d\n", err); 6697 return err; 6698 } 6699 netif_carrier_off(netdev); 6700 priv->profile = new_profile; 6701 priv->ppriv = new_ppriv; 6702 err = new_profile->init(priv->mdev, priv->netdev); 6703 if (err) 6704 goto priv_cleanup; 6705 6706 return 0; 6707 6708 priv_cleanup: 6709 mlx5e_priv_cleanup(priv); 6710 return err; 6711 } 6712 6713 static int 6714 mlx5e_netdev_attach_profile(struct net_device *netdev, struct mlx5_core_dev *mdev, 6715 const struct mlx5e_profile *new_profile, void *new_ppriv) 6716 { 6717 struct mlx5e_priv *priv = netdev_priv(netdev); 6718 int err; 6719 6720 err = mlx5e_netdev_init_profile(netdev, mdev, new_profile, new_ppriv); 6721 if (err) 6722 return err; 6723 6724 err = mlx5e_attach_netdev(priv); 6725 if (err) 6726 goto profile_cleanup; 6727 return err; 6728 6729 profile_cleanup: 6730 new_profile->cleanup(priv); 6731 mlx5e_priv_cleanup(priv); 6732 return err; 6733 } 6734 6735 int mlx5e_netdev_change_profile(struct net_device *netdev, 6736 struct mlx5_core_dev *mdev, 6737 const struct mlx5e_profile *new_profile, 6738 void *new_ppriv) 6739 { 6740 struct mlx5e_priv *priv = netdev_priv(netdev); 6741 const struct mlx5e_profile *orig_profile; 6742 int err, rollback_err; 6743 void *orig_ppriv; 6744 6745 orig_profile = priv->profile; 6746 orig_ppriv = priv->ppriv; 6747 6748 /* NULL could happen if previous change_profile failed to rollback */ 6749 if (priv->profile) { 6750 WARN_ON_ONCE(priv->mdev != mdev); 6751 /* cleanup old profile */ 6752 mlx5e_detach_netdev(priv); 6753 priv->profile->cleanup(priv); 6754 mlx5e_priv_cleanup(priv); 6755 } 6756 /* priv members are not valid from this point ... */ 6757 6758 if (mdev->state == MLX5_DEVICE_STATE_INTERNAL_ERROR) { 6759 mlx5e_netdev_init_profile(netdev, mdev, new_profile, new_ppriv); 6760 set_bit(MLX5E_STATE_DESTROYING, &priv->state); 6761 return -EIO; 6762 } 6763 6764 err = mlx5e_netdev_attach_profile(netdev, mdev, new_profile, new_ppriv); 6765 if (err) { /* roll back to original profile */ 6766 netdev_warn(netdev, "%s: new profile init failed, %d\n", __func__, err); 6767 goto rollback; 6768 } 6769 6770 return 0; 6771 6772 rollback: 6773 if (!orig_profile) { 6774 netdev_warn(netdev, "no original profile to rollback to\n"); 6775 priv->profile = NULL; 6776 return err; 6777 } 6778 6779 rollback_err = mlx5e_netdev_attach_profile(netdev, mdev, orig_profile, orig_ppriv); 6780 if (rollback_err) { 6781 netdev_err(netdev, "failed to rollback to orig profile, %d\n", 6782 rollback_err); 6783 priv->profile = NULL; 6784 } 6785 return err; 6786 } 6787 6788 void mlx5e_netdev_attach_nic_profile(struct net_device *netdev, 6789 struct mlx5_core_dev *mdev) 6790 { 6791 mlx5e_netdev_change_profile(netdev, mdev, &mlx5e_nic_profile, NULL); 6792 } 6793 6794 void mlx5e_destroy_netdev(struct net_device *netdev) 6795 { 6796 struct mlx5e_priv *priv = netdev_priv(netdev); 6797 6798 if (priv->profile) 6799 mlx5e_priv_cleanup(priv); 6800 free_netdev(netdev); 6801 } 6802 6803 static int _mlx5e_resume(struct auxiliary_device *adev) 6804 { 6805 struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev); 6806 struct mlx5e_dev *mlx5e_dev = auxiliary_get_drvdata(adev); 6807 struct mlx5e_priv *priv = netdev_priv(mlx5e_dev->netdev); 6808 struct net_device *netdev = mlx5e_dev->netdev; 6809 struct mlx5_core_dev *mdev = edev->mdev; 6810 struct mlx5_core_dev *pos, *to; 6811 int err, i; 6812 6813 if (netif_device_present(netdev)) 6814 return 0; 6815 6816 mlx5_sd_for_each_dev(i, mdev, pos) { 6817 err = mlx5e_create_mdev_resources(pos, true); 6818 if (err) 6819 goto err_destroy_mdev_res; 6820 } 6821 6822 err = mlx5e_attach_netdev(priv); 6823 if (err) 6824 goto err_destroy_mdev_res; 6825 6826 return 0; 6827 6828 err_destroy_mdev_res: 6829 to = pos; 6830 mlx5_sd_for_each_dev_to(i, mdev, to, pos) 6831 mlx5e_destroy_mdev_resources(pos); 6832 return err; 6833 } 6834 6835 static int mlx5e_resume(struct auxiliary_device *adev) 6836 { 6837 struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev); 6838 struct mlx5_core_dev *mdev = edev->mdev; 6839 struct auxiliary_device *actual_adev; 6840 int err; 6841 6842 err = mlx5_sd_init(mdev); 6843 if (err) 6844 return err; 6845 6846 actual_adev = mlx5_sd_get_adev(mdev, adev, edev->idx); 6847 if (actual_adev) { 6848 err = _mlx5e_resume(actual_adev); 6849 mlx5_sd_put_adev(actual_adev, adev); 6850 } 6851 return err; 6852 } 6853 6854 static int _mlx5e_suspend(struct auxiliary_device *adev, bool pre_netdev_reg) 6855 { 6856 struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev); 6857 struct mlx5e_dev *mlx5e_dev = auxiliary_get_drvdata(adev); 6858 struct mlx5e_priv *priv = netdev_priv(mlx5e_dev->netdev); 6859 struct net_device *netdev = mlx5e_dev->netdev; 6860 struct mlx5_core_dev *mdev = edev->mdev; 6861 struct mlx5_core_dev *pos; 6862 int i; 6863 6864 if (!pre_netdev_reg && !netif_device_present(netdev)) { 6865 if (test_bit(MLX5E_STATE_DESTROYING, &priv->state)) 6866 mlx5_sd_for_each_dev(i, mdev, pos) 6867 mlx5e_destroy_mdev_resources(pos); 6868 return -ENODEV; 6869 } 6870 6871 mlx5e_detach_netdev(priv); 6872 mlx5_sd_for_each_dev(i, mdev, pos) 6873 mlx5e_destroy_mdev_resources(pos); 6874 6875 return 0; 6876 } 6877 6878 static int mlx5e_suspend(struct auxiliary_device *adev, pm_message_t state) 6879 { 6880 struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev); 6881 struct mlx5_core_dev *mdev = edev->mdev; 6882 struct auxiliary_device *actual_adev; 6883 int err = 0; 6884 6885 actual_adev = mlx5_sd_get_adev(mdev, adev, edev->idx); 6886 if (actual_adev) 6887 err = _mlx5e_suspend(actual_adev, false); 6888 6889 mlx5_sd_cleanup(mdev); 6890 if (actual_adev) 6891 mlx5_sd_put_adev(actual_adev, adev); 6892 return err; 6893 } 6894 6895 static int _mlx5e_probe(struct auxiliary_device *adev) 6896 { 6897 struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev); 6898 const struct mlx5e_profile *profile = &mlx5e_nic_profile; 6899 struct mlx5_core_dev *mdev = edev->mdev; 6900 struct mlx5e_dev *mlx5e_dev; 6901 struct net_device *netdev; 6902 struct mlx5e_priv *priv; 6903 int err; 6904 6905 mlx5e_dev = mlx5e_create_devlink(&adev->dev, mdev); 6906 if (IS_ERR(mlx5e_dev)) 6907 return PTR_ERR(mlx5e_dev); 6908 auxiliary_set_drvdata(adev, mlx5e_dev); 6909 6910 err = mlx5e_devlink_port_register(mlx5e_dev, mdev); 6911 if (err) { 6912 mlx5_core_err(mdev, "mlx5e_devlink_port_register failed, %d\n", err); 6913 goto err_devlink_unregister; 6914 } 6915 6916 netdev = mlx5e_create_netdev(mdev, profile); 6917 if (!netdev) { 6918 mlx5_core_err(mdev, "mlx5e_create_netdev failed\n"); 6919 err = -ENOMEM; 6920 goto err_devlink_port_unregister; 6921 } 6922 SET_NETDEV_DEVLINK_PORT(netdev, &mlx5e_dev->dl_port); 6923 mlx5e_dev->netdev = netdev; 6924 6925 mlx5e_build_nic_netdev(netdev); 6926 6927 priv = netdev_priv(netdev); 6928 6929 priv->profile = profile; 6930 priv->ppriv = NULL; 6931 6932 err = profile->init(mdev, netdev); 6933 if (err) { 6934 mlx5_core_err(mdev, "mlx5e_nic_profile init failed, %d\n", err); 6935 goto err_destroy_netdev; 6936 } 6937 6938 err = _mlx5e_resume(adev); 6939 if (err) { 6940 mlx5_core_err(mdev, "_mlx5e_resume failed, %d\n", err); 6941 goto err_profile_cleanup; 6942 } 6943 6944 err = register_netdev(netdev); 6945 if (err) { 6946 mlx5_core_err(mdev, "register_netdev failed, %d\n", err); 6947 goto err_resume; 6948 } 6949 6950 /* mlx5e_fix_features() returns early when the device is not present 6951 * to avoid dereferencing cleared priv during profile changes. 6952 * This also causes it to be a no-op during register_netdev(), where 6953 * the device is not yet present. 6954 * Trigger an additional features update that will actually work. 6955 */ 6956 mlx5e_update_features(netdev); 6957 6958 mlx5e_dcbnl_init_app(priv); 6959 mlx5_core_uplink_netdev_set(mdev, netdev); 6960 mlx5e_params_print_info(mdev, &priv->channels.params); 6961 return 0; 6962 6963 err_resume: 6964 _mlx5e_suspend(adev, true); 6965 err_profile_cleanup: 6966 profile->cleanup(priv); 6967 err_destroy_netdev: 6968 mlx5e_destroy_netdev(netdev); 6969 err_devlink_port_unregister: 6970 mlx5e_devlink_port_unregister(mlx5e_dev); 6971 err_devlink_unregister: 6972 mlx5e_destroy_devlink(mlx5e_dev); 6973 return err; 6974 } 6975 6976 static int mlx5e_probe(struct auxiliary_device *adev, 6977 const struct auxiliary_device_id *id) 6978 { 6979 struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev); 6980 struct mlx5_core_dev *mdev = edev->mdev; 6981 struct auxiliary_device *actual_adev; 6982 int err; 6983 6984 err = mlx5_sd_init(mdev); 6985 if (err) 6986 return err; 6987 6988 actual_adev = mlx5_sd_get_adev(mdev, adev, edev->idx); 6989 if (actual_adev) { 6990 err = _mlx5e_probe(actual_adev); 6991 if (err) 6992 goto sd_cleanup; 6993 mlx5_sd_put_adev(actual_adev, adev); 6994 } 6995 return 0; 6996 6997 sd_cleanup: 6998 mlx5_sd_cleanup(mdev); 6999 if (actual_adev) 7000 mlx5_sd_put_adev(actual_adev, adev); 7001 return err; 7002 } 7003 7004 static void _mlx5e_remove(struct auxiliary_device *adev) 7005 { 7006 struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev); 7007 struct mlx5e_dev *mlx5e_dev = auxiliary_get_drvdata(adev); 7008 struct net_device *netdev = mlx5e_dev->netdev; 7009 struct mlx5e_priv *priv = netdev_priv(netdev); 7010 struct mlx5_core_dev *mdev = edev->mdev; 7011 7012 mlx5_eswitch_safe_aux_devs_remove(mdev); 7013 mlx5_core_uplink_netdev_set(mdev, NULL); 7014 7015 if (priv->profile) 7016 mlx5e_dcbnl_delete_app(priv); 7017 /* When unload driver, the netdev is in registered state 7018 * if it's from legacy mode. If from switchdev mode, it 7019 * is already unregistered before changing to NIC profile. 7020 */ 7021 if (netdev->reg_state == NETREG_REGISTERED) { 7022 unregister_netdev(netdev); 7023 _mlx5e_suspend(adev, false); 7024 } else { 7025 struct mlx5_core_dev *pos; 7026 int i; 7027 7028 if (test_bit(MLX5E_STATE_DESTROYING, &priv->state)) 7029 mlx5_sd_for_each_dev(i, mdev, pos) 7030 mlx5e_destroy_mdev_resources(pos); 7031 else 7032 _mlx5e_suspend(adev, true); 7033 } 7034 /* Avoid cleanup if profile rollback failed. */ 7035 if (priv->profile) 7036 priv->profile->cleanup(priv); 7037 mlx5e_destroy_netdev(netdev); 7038 mlx5e_devlink_port_unregister(mlx5e_dev); 7039 mlx5e_destroy_devlink(mlx5e_dev); 7040 } 7041 7042 static void mlx5e_remove(struct auxiliary_device *adev) 7043 { 7044 struct mlx5_adev *edev = container_of(adev, struct mlx5_adev, adev); 7045 struct mlx5_core_dev *mdev = edev->mdev; 7046 struct auxiliary_device *actual_adev; 7047 7048 actual_adev = mlx5_sd_get_adev(mdev, adev, edev->idx); 7049 if (actual_adev) 7050 _mlx5e_remove(actual_adev); 7051 7052 mlx5_sd_cleanup(mdev); 7053 if (actual_adev) 7054 mlx5_sd_put_adev(actual_adev, adev); 7055 } 7056 7057 static const struct auxiliary_device_id mlx5e_id_table[] = { 7058 { .name = MLX5_ADEV_NAME ".eth", }, 7059 {}, 7060 }; 7061 7062 MODULE_DEVICE_TABLE(auxiliary, mlx5e_id_table); 7063 7064 static struct auxiliary_driver mlx5e_driver = { 7065 .name = "eth", 7066 .probe = mlx5e_probe, 7067 .remove = mlx5e_remove, 7068 .suspend = mlx5e_suspend, 7069 .resume = mlx5e_resume, 7070 .id_table = mlx5e_id_table, 7071 }; 7072 7073 int mlx5e_init(void) 7074 { 7075 int ret; 7076 7077 mlx5e_build_ptys2ethtool_map(); 7078 ret = auxiliary_driver_register(&mlx5e_driver); 7079 if (ret) 7080 return ret; 7081 7082 ret = mlx5e_rep_init(); 7083 if (ret) 7084 auxiliary_driver_unregister(&mlx5e_driver); 7085 return ret; 7086 } 7087 7088 void mlx5e_cleanup(void) 7089 { 7090 mlx5e_rep_cleanup(); 7091 auxiliary_driver_unregister(&mlx5e_driver); 7092 } 7093