2 * Copyright (c) 2015-2016, Mellanox Technologies. All rights reserved.
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:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
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.
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
33 #include <net/tc_act/tc_gact.h>
34 #include <net/pkt_cls.h>
35 #include <linux/mlx5/fs.h>
36 #include <net/vxlan.h>
42 struct mlx5e_rq_param {
43 u32 rqc[MLX5_ST_SZ_DW(rqc)];
44 struct mlx5_wq_param wq;
48 struct mlx5e_sq_param {
49 u32 sqc[MLX5_ST_SZ_DW(sqc)];
50 struct mlx5_wq_param wq;
56 struct mlx5e_cq_param {
57 u32 cqc[MLX5_ST_SZ_DW(cqc)];
58 struct mlx5_wq_param wq;
63 struct mlx5e_channel_param {
64 struct mlx5e_rq_param rq;
65 struct mlx5e_sq_param sq;
66 struct mlx5e_sq_param icosq;
67 struct mlx5e_cq_param rx_cq;
68 struct mlx5e_cq_param tx_cq;
69 struct mlx5e_cq_param icosq_cq;
72 static bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
74 return MLX5_CAP_GEN(mdev, striding_rq) &&
75 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
76 MLX5_CAP_ETH(mdev, reg_umr_sq);
79 static void mlx5e_set_rq_type_params(struct mlx5e_priv *priv, u8 rq_type)
81 priv->params.rq_wq_type = rq_type;
82 switch (priv->params.rq_wq_type) {
83 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
84 priv->params.log_rq_size = MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE_MPW;
85 priv->params.mpwqe_log_stride_sz = priv->params.rx_cqe_compress ?
86 MLX5_MPWRQ_LOG_STRIDE_SIZE_CQE_COMPRESS :
87 MLX5_MPWRQ_LOG_STRIDE_SIZE;
88 priv->params.mpwqe_log_num_strides = MLX5_MPWRQ_LOG_WQE_SZ -
89 priv->params.mpwqe_log_stride_sz;
91 default: /* MLX5_WQ_TYPE_LINKED_LIST */
92 priv->params.log_rq_size = MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
94 priv->params.min_rx_wqes = mlx5_min_rx_wqes(priv->params.rq_wq_type,
95 BIT(priv->params.log_rq_size));
97 mlx5_core_info(priv->mdev,
98 "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
99 priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
100 BIT(priv->params.log_rq_size),
101 BIT(priv->params.mpwqe_log_stride_sz),
102 priv->params.rx_cqe_compress_admin);
105 static void mlx5e_set_rq_priv_params(struct mlx5e_priv *priv)
107 u8 rq_type = mlx5e_check_fragmented_striding_rq_cap(priv->mdev) ?
108 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
109 MLX5_WQ_TYPE_LINKED_LIST;
110 mlx5e_set_rq_type_params(priv, rq_type);
113 static void mlx5e_update_carrier(struct mlx5e_priv *priv)
115 struct mlx5_core_dev *mdev = priv->mdev;
118 port_state = mlx5_query_vport_state(mdev,
119 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT, 0);
121 if (port_state == VPORT_STATE_UP) {
122 netdev_info(priv->netdev, "Link up\n");
123 netif_carrier_on(priv->netdev);
125 netdev_info(priv->netdev, "Link down\n");
126 netif_carrier_off(priv->netdev);
130 static void mlx5e_update_carrier_work(struct work_struct *work)
132 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
133 update_carrier_work);
135 mutex_lock(&priv->state_lock);
136 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
137 mlx5e_update_carrier(priv);
138 mutex_unlock(&priv->state_lock);
141 static void mlx5e_tx_timeout_work(struct work_struct *work)
143 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
148 mutex_lock(&priv->state_lock);
149 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
151 mlx5e_close_locked(priv->netdev);
152 err = mlx5e_open_locked(priv->netdev);
154 netdev_err(priv->netdev, "mlx5e_open_locked failed recovering from a tx_timeout, err(%d).\n",
157 mutex_unlock(&priv->state_lock);
161 static void mlx5e_update_sw_counters(struct mlx5e_priv *priv)
163 struct mlx5e_sw_stats *s = &priv->stats.sw;
164 struct mlx5e_rq_stats *rq_stats;
165 struct mlx5e_sq_stats *sq_stats;
166 u64 tx_offload_none = 0;
169 memset(s, 0, sizeof(*s));
170 for (i = 0; i < priv->params.num_channels; i++) {
171 rq_stats = &priv->channel[i]->rq.stats;
173 s->rx_packets += rq_stats->packets;
174 s->rx_bytes += rq_stats->bytes;
175 s->rx_lro_packets += rq_stats->lro_packets;
176 s->rx_lro_bytes += rq_stats->lro_bytes;
177 s->rx_csum_none += rq_stats->csum_none;
178 s->rx_csum_complete += rq_stats->csum_complete;
179 s->rx_csum_unnecessary_inner += rq_stats->csum_unnecessary_inner;
180 s->rx_wqe_err += rq_stats->wqe_err;
181 s->rx_mpwqe_filler += rq_stats->mpwqe_filler;
182 s->rx_buff_alloc_err += rq_stats->buff_alloc_err;
183 s->rx_cqe_compress_blks += rq_stats->cqe_compress_blks;
184 s->rx_cqe_compress_pkts += rq_stats->cqe_compress_pkts;
185 s->rx_cache_reuse += rq_stats->cache_reuse;
186 s->rx_cache_full += rq_stats->cache_full;
187 s->rx_cache_empty += rq_stats->cache_empty;
188 s->rx_cache_busy += rq_stats->cache_busy;
190 for (j = 0; j < priv->params.num_tc; j++) {
191 sq_stats = &priv->channel[i]->sq[j].stats;
193 s->tx_packets += sq_stats->packets;
194 s->tx_bytes += sq_stats->bytes;
195 s->tx_tso_packets += sq_stats->tso_packets;
196 s->tx_tso_bytes += sq_stats->tso_bytes;
197 s->tx_tso_inner_packets += sq_stats->tso_inner_packets;
198 s->tx_tso_inner_bytes += sq_stats->tso_inner_bytes;
199 s->tx_queue_stopped += sq_stats->stopped;
200 s->tx_queue_wake += sq_stats->wake;
201 s->tx_queue_dropped += sq_stats->dropped;
202 s->tx_xmit_more += sq_stats->xmit_more;
203 s->tx_csum_partial_inner += sq_stats->csum_partial_inner;
204 tx_offload_none += sq_stats->csum_none;
208 /* Update calculated offload counters */
209 s->tx_csum_partial = s->tx_packets - tx_offload_none - s->tx_csum_partial_inner;
210 s->rx_csum_unnecessary = s->rx_packets - s->rx_csum_none - s->rx_csum_complete;
212 s->link_down_events_phy = MLX5_GET(ppcnt_reg,
213 priv->stats.pport.phy_counters,
214 counter_set.phys_layer_cntrs.link_down_events);
217 static void mlx5e_update_vport_counters(struct mlx5e_priv *priv)
219 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
220 u32 *out = (u32 *)priv->stats.vport.query_vport_out;
221 u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)] = {0};
222 struct mlx5_core_dev *mdev = priv->mdev;
224 MLX5_SET(query_vport_counter_in, in, opcode,
225 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
226 MLX5_SET(query_vport_counter_in, in, op_mod, 0);
227 MLX5_SET(query_vport_counter_in, in, other_vport, 0);
229 memset(out, 0, outlen);
230 mlx5_cmd_exec(mdev, in, sizeof(in), out, outlen);
233 static void mlx5e_update_pport_counters(struct mlx5e_priv *priv)
235 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
236 struct mlx5_core_dev *mdev = priv->mdev;
237 int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
242 in = mlx5_vzalloc(sz);
246 MLX5_SET(ppcnt_reg, in, local_port, 1);
248 out = pstats->IEEE_802_3_counters;
249 MLX5_SET(ppcnt_reg, in, grp, MLX5_IEEE_802_3_COUNTERS_GROUP);
250 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
252 out = pstats->RFC_2863_counters;
253 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2863_COUNTERS_GROUP);
254 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
256 out = pstats->RFC_2819_counters;
257 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2819_COUNTERS_GROUP);
258 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
260 out = pstats->phy_counters;
261 MLX5_SET(ppcnt_reg, in, grp, MLX5_PHYSICAL_LAYER_COUNTERS_GROUP);
262 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
264 MLX5_SET(ppcnt_reg, in, grp, MLX5_PER_PRIORITY_COUNTERS_GROUP);
265 for (prio = 0; prio < NUM_PPORT_PRIO; prio++) {
266 out = pstats->per_prio_counters[prio];
267 MLX5_SET(ppcnt_reg, in, prio_tc, prio);
268 mlx5_core_access_reg(mdev, in, sz, out, sz,
269 MLX5_REG_PPCNT, 0, 0);
276 static void mlx5e_update_q_counter(struct mlx5e_priv *priv)
278 struct mlx5e_qcounter_stats *qcnt = &priv->stats.qcnt;
280 if (!priv->q_counter)
283 mlx5_core_query_out_of_buffer(priv->mdev, priv->q_counter,
284 &qcnt->rx_out_of_buffer);
287 void mlx5e_update_stats(struct mlx5e_priv *priv)
289 mlx5e_update_q_counter(priv);
290 mlx5e_update_vport_counters(priv);
291 mlx5e_update_pport_counters(priv);
292 mlx5e_update_sw_counters(priv);
295 void mlx5e_update_stats_work(struct work_struct *work)
297 struct delayed_work *dwork = to_delayed_work(work);
298 struct mlx5e_priv *priv = container_of(dwork, struct mlx5e_priv,
300 mutex_lock(&priv->state_lock);
301 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
302 priv->profile->update_stats(priv);
303 queue_delayed_work(priv->wq, dwork,
304 msecs_to_jiffies(MLX5E_UPDATE_STATS_INTERVAL));
306 mutex_unlock(&priv->state_lock);
309 static void mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
310 enum mlx5_dev_event event, unsigned long param)
312 struct mlx5e_priv *priv = vpriv;
314 if (!test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state))
318 case MLX5_DEV_EVENT_PORT_UP:
319 case MLX5_DEV_EVENT_PORT_DOWN:
320 queue_work(priv->wq, &priv->update_carrier_work);
328 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
330 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
333 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
335 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
336 synchronize_irq(mlx5_get_msix_vec(priv->mdev, MLX5_EQ_VEC_ASYNC));
339 #define MLX5E_HW2SW_MTU(hwmtu) (hwmtu - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
340 #define MLX5E_SW2HW_MTU(swmtu) (swmtu + (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
342 static inline int mlx5e_get_wqe_mtt_sz(void)
344 /* UMR copies MTTs in units of MLX5_UMR_MTT_ALIGNMENT bytes.
345 * To avoid copying garbage after the mtt array, we allocate
348 return ALIGN(MLX5_MPWRQ_PAGES_PER_WQE * sizeof(__be64),
349 MLX5_UMR_MTT_ALIGNMENT);
352 static inline void mlx5e_build_umr_wqe(struct mlx5e_rq *rq, struct mlx5e_sq *sq,
353 struct mlx5e_umr_wqe *wqe, u16 ix)
355 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
356 struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->uctrl;
357 struct mlx5_wqe_data_seg *dseg = &wqe->data;
358 struct mlx5e_mpw_info *wi = &rq->mpwqe.info[ix];
359 u8 ds_cnt = DIV_ROUND_UP(sizeof(*wqe), MLX5_SEND_WQE_DS);
360 u32 umr_wqe_mtt_offset = mlx5e_get_wqe_mtt_offset(rq, ix);
362 cseg->qpn_ds = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
364 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
365 cseg->imm = rq->mkey_be;
367 ucseg->flags = MLX5_UMR_TRANSLATION_OFFSET_EN;
368 ucseg->klm_octowords =
369 cpu_to_be16(MLX5_MTT_OCTW(MLX5_MPWRQ_PAGES_PER_WQE));
370 ucseg->bsf_octowords =
371 cpu_to_be16(MLX5_MTT_OCTW(umr_wqe_mtt_offset));
372 ucseg->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
374 dseg->lkey = sq->mkey_be;
375 dseg->addr = cpu_to_be64(wi->umr.mtt_addr);
378 static int mlx5e_rq_alloc_mpwqe_info(struct mlx5e_rq *rq,
379 struct mlx5e_channel *c)
381 int wq_sz = mlx5_wq_ll_get_size(&rq->wq);
382 int mtt_sz = mlx5e_get_wqe_mtt_sz();
383 int mtt_alloc = mtt_sz + MLX5_UMR_ALIGN - 1;
386 rq->mpwqe.info = kzalloc_node(wq_sz * sizeof(*rq->mpwqe.info),
387 GFP_KERNEL, cpu_to_node(c->cpu));
391 /* We allocate more than mtt_sz as we will align the pointer */
392 rq->mpwqe.mtt_no_align = kzalloc_node(mtt_alloc * wq_sz, GFP_KERNEL,
393 cpu_to_node(c->cpu));
394 if (unlikely(!rq->mpwqe.mtt_no_align))
395 goto err_free_wqe_info;
397 for (i = 0; i < wq_sz; i++) {
398 struct mlx5e_mpw_info *wi = &rq->mpwqe.info[i];
400 wi->umr.mtt = PTR_ALIGN(rq->mpwqe.mtt_no_align + i * mtt_alloc,
402 wi->umr.mtt_addr = dma_map_single(c->pdev, wi->umr.mtt, mtt_sz,
404 if (unlikely(dma_mapping_error(c->pdev, wi->umr.mtt_addr)))
407 mlx5e_build_umr_wqe(rq, &c->icosq, &wi->umr.wqe, i);
414 struct mlx5e_mpw_info *wi = &rq->mpwqe.info[i];
416 dma_unmap_single(c->pdev, wi->umr.mtt_addr, mtt_sz,
419 kfree(rq->mpwqe.mtt_no_align);
421 kfree(rq->mpwqe.info);
427 static void mlx5e_rq_free_mpwqe_info(struct mlx5e_rq *rq)
429 int wq_sz = mlx5_wq_ll_get_size(&rq->wq);
430 int mtt_sz = mlx5e_get_wqe_mtt_sz();
433 for (i = 0; i < wq_sz; i++) {
434 struct mlx5e_mpw_info *wi = &rq->mpwqe.info[i];
436 dma_unmap_single(rq->pdev, wi->umr.mtt_addr, mtt_sz,
439 kfree(rq->mpwqe.mtt_no_align);
440 kfree(rq->mpwqe.info);
443 static int mlx5e_create_rq(struct mlx5e_channel *c,
444 struct mlx5e_rq_param *param,
447 struct mlx5e_priv *priv = c->priv;
448 struct mlx5_core_dev *mdev = priv->mdev;
449 void *rqc = param->rqc;
450 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
458 param->wq.db_numa_node = cpu_to_node(c->cpu);
460 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
465 rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
467 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
469 rq->wq_type = priv->params.rq_wq_type;
471 rq->netdev = c->netdev;
472 rq->tstamp = &priv->tstamp;
477 switch (priv->params.rq_wq_type) {
478 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
479 rq->handle_rx_cqe = mlx5e_handle_rx_cqe_mpwrq;
480 rq->alloc_wqe = mlx5e_alloc_rx_mpwqe;
481 rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
483 rq->mpwqe.mtt_offset = c->ix *
484 MLX5E_REQUIRED_MTTS(1, BIT(priv->params.log_rq_size));
486 rq->mpwqe_stride_sz = BIT(priv->params.mpwqe_log_stride_sz);
487 rq->mpwqe_num_strides = BIT(priv->params.mpwqe_log_num_strides);
489 rq->buff.wqe_sz = rq->mpwqe_stride_sz * rq->mpwqe_num_strides;
490 byte_count = rq->buff.wqe_sz;
491 rq->mkey_be = cpu_to_be32(c->priv->umr_mkey.key);
492 err = mlx5e_rq_alloc_mpwqe_info(rq, c);
494 goto err_rq_wq_destroy;
496 default: /* MLX5_WQ_TYPE_LINKED_LIST */
497 rq->dma_info = kzalloc_node(wq_sz * sizeof(*rq->dma_info),
498 GFP_KERNEL, cpu_to_node(c->cpu));
501 goto err_rq_wq_destroy;
504 rq->handle_rx_cqe = mlx5e_handle_rx_cqe;
505 rq->alloc_wqe = mlx5e_alloc_rx_wqe;
506 rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
508 rq->buff.wqe_sz = (priv->params.lro_en) ?
509 priv->params.lro_wqe_sz :
510 MLX5E_SW2HW_MTU(priv->netdev->mtu);
511 byte_count = rq->buff.wqe_sz;
513 /* calc the required page order */
514 frag_sz = MLX5_RX_HEADROOM +
515 byte_count /* packet data */ +
516 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
517 frag_sz = SKB_DATA_ALIGN(frag_sz);
519 npages = DIV_ROUND_UP(frag_sz, PAGE_SIZE);
520 rq->buff.page_order = order_base_2(npages);
522 byte_count |= MLX5_HW_START_PADDING;
523 rq->mkey_be = c->mkey_be;
526 for (i = 0; i < wq_sz; i++) {
527 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
529 wqe->data.byte_count = cpu_to_be32(byte_count);
530 wqe->data.lkey = rq->mkey_be;
533 INIT_WORK(&rq->am.work, mlx5e_rx_am_work);
534 rq->am.mode = priv->params.rx_cq_period_mode;
536 rq->page_cache.head = 0;
537 rq->page_cache.tail = 0;
542 mlx5_wq_destroy(&rq->wq_ctrl);
547 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
551 switch (rq->wq_type) {
552 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
553 mlx5e_rq_free_mpwqe_info(rq);
555 default: /* MLX5_WQ_TYPE_LINKED_LIST */
559 for (i = rq->page_cache.head; i != rq->page_cache.tail;
560 i = (i + 1) & (MLX5E_CACHE_SIZE - 1)) {
561 struct mlx5e_dma_info *dma_info = &rq->page_cache.page_cache[i];
563 mlx5e_page_release(rq, dma_info, false);
565 mlx5_wq_destroy(&rq->wq_ctrl);
568 static int mlx5e_enable_rq(struct mlx5e_rq *rq, struct mlx5e_rq_param *param)
570 struct mlx5e_priv *priv = rq->priv;
571 struct mlx5_core_dev *mdev = priv->mdev;
579 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
580 sizeof(u64) * rq->wq_ctrl.buf.npages;
581 in = mlx5_vzalloc(inlen);
585 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
586 wq = MLX5_ADDR_OF(rqc, rqc, wq);
588 memcpy(rqc, param->rqc, sizeof(param->rqc));
590 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
591 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
592 MLX5_SET(rqc, rqc, vsd, priv->params.vlan_strip_disable);
593 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
594 MLX5_ADAPTER_PAGE_SHIFT);
595 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
597 mlx5_fill_page_array(&rq->wq_ctrl.buf,
598 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
600 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
607 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
610 struct mlx5e_channel *c = rq->channel;
611 struct mlx5e_priv *priv = c->priv;
612 struct mlx5_core_dev *mdev = priv->mdev;
619 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
620 in = mlx5_vzalloc(inlen);
624 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
626 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
627 MLX5_SET(rqc, rqc, state, next_state);
629 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
636 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
638 struct mlx5e_channel *c = rq->channel;
639 struct mlx5e_priv *priv = c->priv;
640 struct mlx5_core_dev *mdev = priv->mdev;
647 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
648 in = mlx5_vzalloc(inlen);
652 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
654 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
655 MLX5_SET64(modify_rq_in, in, modify_bitmask,
656 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
657 MLX5_SET(rqc, rqc, vsd, vsd);
658 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
660 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
667 static void mlx5e_disable_rq(struct mlx5e_rq *rq)
669 mlx5_core_destroy_rq(rq->priv->mdev, rq->rqn);
672 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq)
674 unsigned long exp_time = jiffies + msecs_to_jiffies(20000);
675 struct mlx5e_channel *c = rq->channel;
676 struct mlx5e_priv *priv = c->priv;
677 struct mlx5_wq_ll *wq = &rq->wq;
679 while (time_before(jiffies, exp_time)) {
680 if (wq->cur_sz >= priv->params.min_rx_wqes)
689 static void mlx5e_free_rx_descs(struct mlx5e_rq *rq)
691 struct mlx5_wq_ll *wq = &rq->wq;
692 struct mlx5e_rx_wqe *wqe;
696 /* UMR WQE (if in progress) is always at wq->head */
697 if (test_bit(MLX5E_RQ_STATE_UMR_WQE_IN_PROGRESS, &rq->state))
698 mlx5e_free_rx_mpwqe(rq, &rq->mpwqe.info[wq->head]);
700 while (!mlx5_wq_ll_is_empty(wq)) {
701 wqe_ix_be = *wq->tail_next;
702 wqe_ix = be16_to_cpu(wqe_ix_be);
703 wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_ix);
704 rq->dealloc_wqe(rq, wqe_ix);
705 mlx5_wq_ll_pop(&rq->wq, wqe_ix_be,
706 &wqe->next.next_wqe_index);
710 static int mlx5e_open_rq(struct mlx5e_channel *c,
711 struct mlx5e_rq_param *param,
714 struct mlx5e_sq *sq = &c->icosq;
715 u16 pi = sq->pc & sq->wq.sz_m1;
718 err = mlx5e_create_rq(c, param, rq);
722 err = mlx5e_enable_rq(rq, param);
726 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
730 if (param->am_enabled)
731 set_bit(MLX5E_RQ_STATE_AM, &c->rq.state);
733 sq->ico_wqe_info[pi].opcode = MLX5_OPCODE_NOP;
734 sq->ico_wqe_info[pi].num_wqebbs = 1;
735 mlx5e_send_nop(sq, true); /* trigger mlx5e_post_rx_wqes() */
740 mlx5e_disable_rq(rq);
742 mlx5e_destroy_rq(rq);
747 static void mlx5e_close_rq(struct mlx5e_rq *rq)
749 set_bit(MLX5E_RQ_STATE_FLUSH, &rq->state);
750 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
751 cancel_work_sync(&rq->am.work);
753 mlx5e_disable_rq(rq);
754 mlx5e_free_rx_descs(rq);
755 mlx5e_destroy_rq(rq);
758 static void mlx5e_free_sq_db(struct mlx5e_sq *sq)
765 static int mlx5e_alloc_sq_db(struct mlx5e_sq *sq, int numa)
767 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
768 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
770 sq->skb = kzalloc_node(wq_sz * sizeof(*sq->skb), GFP_KERNEL, numa);
771 sq->dma_fifo = kzalloc_node(df_sz * sizeof(*sq->dma_fifo), GFP_KERNEL,
773 sq->wqe_info = kzalloc_node(wq_sz * sizeof(*sq->wqe_info), GFP_KERNEL,
776 if (!sq->skb || !sq->dma_fifo || !sq->wqe_info) {
777 mlx5e_free_sq_db(sq);
781 sq->dma_fifo_mask = df_sz - 1;
786 static int mlx5e_create_sq(struct mlx5e_channel *c,
788 struct mlx5e_sq_param *param,
791 struct mlx5e_priv *priv = c->priv;
792 struct mlx5_core_dev *mdev = priv->mdev;
794 void *sqc = param->sqc;
795 void *sqc_wq = MLX5_ADDR_OF(sqc, sqc, wq);
798 err = mlx5_alloc_map_uar(mdev, &sq->uar, !!MLX5_CAP_GEN(mdev, bf));
802 param->wq.db_numa_node = cpu_to_node(c->cpu);
804 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq,
807 goto err_unmap_free_uar;
809 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
810 if (sq->uar.bf_map) {
811 set_bit(MLX5E_SQ_STATE_BF_ENABLE, &sq->state);
812 sq->uar_map = sq->uar.bf_map;
814 sq->uar_map = sq->uar.map;
816 sq->bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
817 sq->max_inline = param->max_inline;
818 sq->min_inline_mode =
819 MLX5_CAP_ETH(mdev, wqe_inline_mode) == MLX5E_INLINE_MODE_VPORT_CONTEXT ?
820 param->min_inline_mode : 0;
822 err = mlx5e_alloc_sq_db(sq, cpu_to_node(c->cpu));
824 goto err_sq_wq_destroy;
827 u8 wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
829 sq->ico_wqe_info = kzalloc_node(sizeof(*sq->ico_wqe_info) *
832 cpu_to_node(c->cpu));
833 if (!sq->ico_wqe_info) {
840 txq_ix = c->ix + tc * priv->params.num_channels;
841 sq->txq = netdev_get_tx_queue(priv->netdev, txq_ix);
842 priv->txq_to_sq_map[txq_ix] = sq;
846 sq->tstamp = &priv->tstamp;
847 sq->mkey_be = c->mkey_be;
850 sq->edge = (sq->wq.sz_m1 + 1) - MLX5_SEND_WQE_MAX_WQEBBS;
851 sq->bf_budget = MLX5E_SQ_BF_BUDGET;
856 mlx5e_free_sq_db(sq);
859 mlx5_wq_destroy(&sq->wq_ctrl);
862 mlx5_unmap_free_uar(mdev, &sq->uar);
867 static void mlx5e_destroy_sq(struct mlx5e_sq *sq)
869 struct mlx5e_channel *c = sq->channel;
870 struct mlx5e_priv *priv = c->priv;
872 kfree(sq->ico_wqe_info);
873 mlx5e_free_sq_db(sq);
874 mlx5_wq_destroy(&sq->wq_ctrl);
875 mlx5_unmap_free_uar(priv->mdev, &sq->uar);
878 static int mlx5e_enable_sq(struct mlx5e_sq *sq, struct mlx5e_sq_param *param)
880 struct mlx5e_channel *c = sq->channel;
881 struct mlx5e_priv *priv = c->priv;
882 struct mlx5_core_dev *mdev = priv->mdev;
890 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
891 sizeof(u64) * sq->wq_ctrl.buf.npages;
892 in = mlx5_vzalloc(inlen);
896 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
897 wq = MLX5_ADDR_OF(sqc, sqc, wq);
899 memcpy(sqc, param->sqc, sizeof(param->sqc));
901 MLX5_SET(sqc, sqc, tis_num_0, param->icosq ? 0 : priv->tisn[sq->tc]);
902 MLX5_SET(sqc, sqc, cqn, sq->cq.mcq.cqn);
903 MLX5_SET(sqc, sqc, min_wqe_inline_mode, sq->min_inline_mode);
904 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
905 MLX5_SET(sqc, sqc, tis_lst_sz, param->icosq ? 0 : 1);
906 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
908 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
909 MLX5_SET(wq, wq, uar_page, sq->uar.index);
910 MLX5_SET(wq, wq, log_wq_pg_sz, sq->wq_ctrl.buf.page_shift -
911 MLX5_ADAPTER_PAGE_SHIFT);
912 MLX5_SET64(wq, wq, dbr_addr, sq->wq_ctrl.db.dma);
914 mlx5_fill_page_array(&sq->wq_ctrl.buf,
915 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
917 err = mlx5_core_create_sq(mdev, in, inlen, &sq->sqn);
924 static int mlx5e_modify_sq(struct mlx5e_sq *sq, int curr_state,
925 int next_state, bool update_rl, int rl_index)
927 struct mlx5e_channel *c = sq->channel;
928 struct mlx5e_priv *priv = c->priv;
929 struct mlx5_core_dev *mdev = priv->mdev;
936 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
937 in = mlx5_vzalloc(inlen);
941 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
943 MLX5_SET(modify_sq_in, in, sq_state, curr_state);
944 MLX5_SET(sqc, sqc, state, next_state);
945 if (update_rl && next_state == MLX5_SQC_STATE_RDY) {
946 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
947 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
950 err = mlx5_core_modify_sq(mdev, sq->sqn, in, inlen);
957 static void mlx5e_disable_sq(struct mlx5e_sq *sq)
959 struct mlx5e_channel *c = sq->channel;
960 struct mlx5e_priv *priv = c->priv;
961 struct mlx5_core_dev *mdev = priv->mdev;
963 mlx5_core_destroy_sq(mdev, sq->sqn);
965 mlx5_rl_remove_rate(mdev, sq->rate_limit);
968 static int mlx5e_open_sq(struct mlx5e_channel *c,
970 struct mlx5e_sq_param *param,
975 err = mlx5e_create_sq(c, tc, param, sq);
979 err = mlx5e_enable_sq(sq, param);
983 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST, MLX5_SQC_STATE_RDY,
989 netdev_tx_reset_queue(sq->txq);
990 netif_tx_start_queue(sq->txq);
996 mlx5e_disable_sq(sq);
998 mlx5e_destroy_sq(sq);
1003 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
1005 __netif_tx_lock_bh(txq);
1006 netif_tx_stop_queue(txq);
1007 __netif_tx_unlock_bh(txq);
1010 static void mlx5e_close_sq(struct mlx5e_sq *sq)
1012 set_bit(MLX5E_SQ_STATE_FLUSH, &sq->state);
1013 /* prevent netif_tx_wake_queue */
1014 napi_synchronize(&sq->channel->napi);
1017 netif_tx_disable_queue(sq->txq);
1019 /* last doorbell out, godspeed .. */
1020 if (mlx5e_sq_has_room_for(sq, 1))
1021 mlx5e_send_nop(sq, true);
1024 mlx5e_disable_sq(sq);
1025 mlx5e_free_tx_descs(sq);
1026 mlx5e_destroy_sq(sq);
1029 static int mlx5e_create_cq(struct mlx5e_channel *c,
1030 struct mlx5e_cq_param *param,
1031 struct mlx5e_cq *cq)
1033 struct mlx5e_priv *priv = c->priv;
1034 struct mlx5_core_dev *mdev = priv->mdev;
1035 struct mlx5_core_cq *mcq = &cq->mcq;
1041 param->wq.buf_numa_node = cpu_to_node(c->cpu);
1042 param->wq.db_numa_node = cpu_to_node(c->cpu);
1043 param->eq_ix = c->ix;
1045 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1050 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1052 cq->napi = &c->napi;
1055 mcq->set_ci_db = cq->wq_ctrl.db.db;
1056 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1057 *mcq->set_ci_db = 0;
1059 mcq->vector = param->eq_ix;
1060 mcq->comp = mlx5e_completion_event;
1061 mcq->event = mlx5e_cq_error_event;
1063 mcq->uar = &mdev->mlx5e_res.cq_uar;
1065 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
1066 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1077 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
1079 mlx5_wq_destroy(&cq->wq_ctrl);
1082 static int mlx5e_enable_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
1084 struct mlx5e_priv *priv = cq->priv;
1085 struct mlx5_core_dev *mdev = priv->mdev;
1086 struct mlx5_core_cq *mcq = &cq->mcq;
1091 unsigned int irqn_not_used;
1095 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1096 sizeof(u64) * cq->wq_ctrl.buf.npages;
1097 in = mlx5_vzalloc(inlen);
1101 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1103 memcpy(cqc, param->cqc, sizeof(param->cqc));
1105 mlx5_fill_page_array(&cq->wq_ctrl.buf,
1106 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
1108 mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
1110 MLX5_SET(cqc, cqc, cq_period_mode, param->cq_period_mode);
1111 MLX5_SET(cqc, cqc, c_eqn, eqn);
1112 MLX5_SET(cqc, cqc, uar_page, mcq->uar->index);
1113 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
1114 MLX5_ADAPTER_PAGE_SHIFT);
1115 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
1117 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
1129 static void mlx5e_disable_cq(struct mlx5e_cq *cq)
1131 struct mlx5e_priv *priv = cq->priv;
1132 struct mlx5_core_dev *mdev = priv->mdev;
1134 mlx5_core_destroy_cq(mdev, &cq->mcq);
1137 static int mlx5e_open_cq(struct mlx5e_channel *c,
1138 struct mlx5e_cq_param *param,
1139 struct mlx5e_cq *cq,
1140 struct mlx5e_cq_moder moderation)
1143 struct mlx5e_priv *priv = c->priv;
1144 struct mlx5_core_dev *mdev = priv->mdev;
1146 err = mlx5e_create_cq(c, param, cq);
1150 err = mlx5e_enable_cq(cq, param);
1152 goto err_destroy_cq;
1154 if (MLX5_CAP_GEN(mdev, cq_moderation))
1155 mlx5_core_modify_cq_moderation(mdev, &cq->mcq,
1161 mlx5e_destroy_cq(cq);
1166 static void mlx5e_close_cq(struct mlx5e_cq *cq)
1168 mlx5e_disable_cq(cq);
1169 mlx5e_destroy_cq(cq);
1172 static int mlx5e_get_cpu(struct mlx5e_priv *priv, int ix)
1174 return cpumask_first(priv->mdev->priv.irq_info[ix].mask);
1177 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1178 struct mlx5e_channel_param *cparam)
1180 struct mlx5e_priv *priv = c->priv;
1184 for (tc = 0; tc < c->num_tc; tc++) {
1185 err = mlx5e_open_cq(c, &cparam->tx_cq, &c->sq[tc].cq,
1186 priv->params.tx_cq_moderation);
1188 goto err_close_tx_cqs;
1194 for (tc--; tc >= 0; tc--)
1195 mlx5e_close_cq(&c->sq[tc].cq);
1200 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1204 for (tc = 0; tc < c->num_tc; tc++)
1205 mlx5e_close_cq(&c->sq[tc].cq);
1208 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1209 struct mlx5e_channel_param *cparam)
1214 for (tc = 0; tc < c->num_tc; tc++) {
1215 err = mlx5e_open_sq(c, tc, &cparam->sq, &c->sq[tc]);
1223 for (tc--; tc >= 0; tc--)
1224 mlx5e_close_sq(&c->sq[tc]);
1229 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1233 for (tc = 0; tc < c->num_tc; tc++)
1234 mlx5e_close_sq(&c->sq[tc]);
1237 static void mlx5e_build_channeltc_to_txq_map(struct mlx5e_priv *priv, int ix)
1241 for (i = 0; i < priv->profile->max_tc; i++)
1242 priv->channeltc_to_txq_map[ix][i] =
1243 ix + i * priv->params.num_channels;
1246 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1247 struct mlx5e_sq *sq, u32 rate)
1249 struct mlx5e_priv *priv = netdev_priv(dev);
1250 struct mlx5_core_dev *mdev = priv->mdev;
1254 if (rate == sq->rate_limit)
1259 /* remove current rl index to free space to next ones */
1260 mlx5_rl_remove_rate(mdev, sq->rate_limit);
1265 err = mlx5_rl_add_rate(mdev, rate, &rl_index);
1267 netdev_err(dev, "Failed configuring rate %u: %d\n",
1273 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY,
1274 MLX5_SQC_STATE_RDY, true, rl_index);
1276 netdev_err(dev, "Failed configuring rate %u: %d\n",
1278 /* remove the rate from the table */
1280 mlx5_rl_remove_rate(mdev, rate);
1284 sq->rate_limit = rate;
1288 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1290 struct mlx5e_priv *priv = netdev_priv(dev);
1291 struct mlx5_core_dev *mdev = priv->mdev;
1292 struct mlx5e_sq *sq = priv->txq_to_sq_map[index];
1295 if (!mlx5_rl_is_supported(mdev)) {
1296 netdev_err(dev, "Rate limiting is not supported on this device\n");
1300 /* rate is given in Mb/sec, HW config is in Kb/sec */
1303 /* Check whether rate in valid range, 0 is always valid */
1304 if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1305 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1309 mutex_lock(&priv->state_lock);
1310 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1311 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1313 priv->tx_rates[index] = rate;
1314 mutex_unlock(&priv->state_lock);
1319 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1320 struct mlx5e_channel_param *cparam,
1321 struct mlx5e_channel **cp)
1323 struct mlx5e_cq_moder icosq_cq_moder = {0, 0};
1324 struct net_device *netdev = priv->netdev;
1325 struct mlx5e_cq_moder rx_cq_profile;
1326 int cpu = mlx5e_get_cpu(priv, ix);
1327 struct mlx5e_channel *c;
1328 struct mlx5e_sq *sq;
1332 c = kzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1339 c->pdev = &priv->mdev->pdev->dev;
1340 c->netdev = priv->netdev;
1341 c->mkey_be = cpu_to_be32(priv->mdev->mlx5e_res.mkey.key);
1342 c->num_tc = priv->params.num_tc;
1344 if (priv->params.rx_am_enabled)
1345 rx_cq_profile = mlx5e_am_get_def_profile(priv->params.rx_cq_period_mode);
1347 rx_cq_profile = priv->params.rx_cq_moderation;
1349 mlx5e_build_channeltc_to_txq_map(priv, ix);
1351 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1353 err = mlx5e_open_cq(c, &cparam->icosq_cq, &c->icosq.cq, icosq_cq_moder);
1357 err = mlx5e_open_tx_cqs(c, cparam);
1359 goto err_close_icosq_cq;
1361 err = mlx5e_open_cq(c, &cparam->rx_cq, &c->rq.cq,
1364 goto err_close_tx_cqs;
1366 napi_enable(&c->napi);
1368 err = mlx5e_open_sq(c, 0, &cparam->icosq, &c->icosq);
1370 goto err_disable_napi;
1372 err = mlx5e_open_sqs(c, cparam);
1374 goto err_close_icosq;
1376 for (i = 0; i < priv->params.num_tc; i++) {
1377 u32 txq_ix = priv->channeltc_to_txq_map[ix][i];
1379 if (priv->tx_rates[txq_ix]) {
1380 sq = priv->txq_to_sq_map[txq_ix];
1381 mlx5e_set_sq_maxrate(priv->netdev, sq,
1382 priv->tx_rates[txq_ix]);
1386 err = mlx5e_open_rq(c, &cparam->rq, &c->rq);
1390 netif_set_xps_queue(netdev, get_cpu_mask(c->cpu), ix);
1399 mlx5e_close_sq(&c->icosq);
1402 napi_disable(&c->napi);
1403 mlx5e_close_cq(&c->rq.cq);
1406 mlx5e_close_tx_cqs(c);
1409 mlx5e_close_cq(&c->icosq.cq);
1412 netif_napi_del(&c->napi);
1413 napi_hash_del(&c->napi);
1419 static void mlx5e_close_channel(struct mlx5e_channel *c)
1421 mlx5e_close_rq(&c->rq);
1423 mlx5e_close_sq(&c->icosq);
1424 napi_disable(&c->napi);
1425 mlx5e_close_cq(&c->rq.cq);
1426 mlx5e_close_tx_cqs(c);
1427 mlx5e_close_cq(&c->icosq.cq);
1428 netif_napi_del(&c->napi);
1430 napi_hash_del(&c->napi);
1436 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
1437 struct mlx5e_rq_param *param)
1439 void *rqc = param->rqc;
1440 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1442 switch (priv->params.rq_wq_type) {
1443 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1444 MLX5_SET(wq, wq, log_wqe_num_of_strides,
1445 priv->params.mpwqe_log_num_strides - 9);
1446 MLX5_SET(wq, wq, log_wqe_stride_size,
1447 priv->params.mpwqe_log_stride_sz - 6);
1448 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ);
1450 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1451 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1454 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1455 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1456 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_rq_size);
1457 MLX5_SET(wq, wq, pd, priv->mdev->mlx5e_res.pdn);
1458 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
1460 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1461 param->wq.linear = 1;
1463 param->am_enabled = priv->params.rx_am_enabled;
1466 static void mlx5e_build_drop_rq_param(struct mlx5e_rq_param *param)
1468 void *rqc = param->rqc;
1469 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1471 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1472 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1475 static void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
1476 struct mlx5e_sq_param *param)
1478 void *sqc = param->sqc;
1479 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1481 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1482 MLX5_SET(wq, wq, pd, priv->mdev->mlx5e_res.pdn);
1484 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1487 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
1488 struct mlx5e_sq_param *param)
1490 void *sqc = param->sqc;
1491 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1493 mlx5e_build_sq_param_common(priv, param);
1494 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_sq_size);
1496 param->max_inline = priv->params.tx_max_inline;
1497 param->min_inline_mode = priv->params.tx_min_inline_mode;
1500 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1501 struct mlx5e_cq_param *param)
1503 void *cqc = param->cqc;
1505 MLX5_SET(cqc, cqc, uar_page, priv->mdev->mlx5e_res.cq_uar.index);
1508 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
1509 struct mlx5e_cq_param *param)
1511 void *cqc = param->cqc;
1514 switch (priv->params.rq_wq_type) {
1515 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1516 log_cq_size = priv->params.log_rq_size +
1517 priv->params.mpwqe_log_num_strides;
1519 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1520 log_cq_size = priv->params.log_rq_size;
1523 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
1524 if (priv->params.rx_cqe_compress) {
1525 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
1526 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
1529 mlx5e_build_common_cq_param(priv, param);
1531 param->cq_period_mode = priv->params.rx_cq_period_mode;
1534 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
1535 struct mlx5e_cq_param *param)
1537 void *cqc = param->cqc;
1539 MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_sq_size);
1541 mlx5e_build_common_cq_param(priv, param);
1543 param->cq_period_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1546 static void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
1547 struct mlx5e_cq_param *param,
1550 void *cqc = param->cqc;
1552 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
1554 mlx5e_build_common_cq_param(priv, param);
1556 param->cq_period_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1559 static void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
1560 struct mlx5e_sq_param *param,
1563 void *sqc = param->sqc;
1564 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1566 mlx5e_build_sq_param_common(priv, param);
1568 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
1569 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
1571 param->icosq = true;
1574 static void mlx5e_build_channel_param(struct mlx5e_priv *priv, struct mlx5e_channel_param *cparam)
1576 u8 icosq_log_wq_sz = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
1578 mlx5e_build_rq_param(priv, &cparam->rq);
1579 mlx5e_build_sq_param(priv, &cparam->sq);
1580 mlx5e_build_icosq_param(priv, &cparam->icosq, icosq_log_wq_sz);
1581 mlx5e_build_rx_cq_param(priv, &cparam->rx_cq);
1582 mlx5e_build_tx_cq_param(priv, &cparam->tx_cq);
1583 mlx5e_build_ico_cq_param(priv, &cparam->icosq_cq, icosq_log_wq_sz);
1586 static int mlx5e_open_channels(struct mlx5e_priv *priv)
1588 struct mlx5e_channel_param *cparam;
1589 int nch = priv->params.num_channels;
1594 priv->channel = kcalloc(nch, sizeof(struct mlx5e_channel *),
1597 priv->txq_to_sq_map = kcalloc(nch * priv->params.num_tc,
1598 sizeof(struct mlx5e_sq *), GFP_KERNEL);
1600 cparam = kzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
1602 if (!priv->channel || !priv->txq_to_sq_map || !cparam)
1603 goto err_free_txq_to_sq_map;
1605 mlx5e_build_channel_param(priv, cparam);
1607 for (i = 0; i < nch; i++) {
1608 err = mlx5e_open_channel(priv, i, cparam, &priv->channel[i]);
1610 goto err_close_channels;
1613 for (j = 0; j < nch; j++) {
1614 err = mlx5e_wait_for_min_rx_wqes(&priv->channel[j]->rq);
1616 goto err_close_channels;
1619 /* FIXME: This is a W/A for tx timeout watch dog false alarm when
1620 * polling for inactive tx queues.
1622 netif_tx_start_all_queues(priv->netdev);
1628 for (i--; i >= 0; i--)
1629 mlx5e_close_channel(priv->channel[i]);
1631 err_free_txq_to_sq_map:
1632 kfree(priv->txq_to_sq_map);
1633 kfree(priv->channel);
1639 static void mlx5e_close_channels(struct mlx5e_priv *priv)
1643 /* FIXME: This is a W/A only for tx timeout watch dog false alarm when
1644 * polling for inactive tx queues.
1646 netif_tx_stop_all_queues(priv->netdev);
1647 netif_tx_disable(priv->netdev);
1649 for (i = 0; i < priv->params.num_channels; i++)
1650 mlx5e_close_channel(priv->channel[i]);
1652 kfree(priv->txq_to_sq_map);
1653 kfree(priv->channel);
1656 static int mlx5e_rx_hash_fn(int hfunc)
1658 return (hfunc == ETH_RSS_HASH_TOP) ?
1659 MLX5_RX_HASH_FN_TOEPLITZ :
1660 MLX5_RX_HASH_FN_INVERTED_XOR8;
1663 static int mlx5e_bits_invert(unsigned long a, int size)
1668 for (i = 0; i < size; i++)
1669 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
1674 static void mlx5e_fill_indir_rqt_rqns(struct mlx5e_priv *priv, void *rqtc)
1678 for (i = 0; i < MLX5E_INDIR_RQT_SIZE; i++) {
1682 if (priv->params.rss_hfunc == ETH_RSS_HASH_XOR)
1683 ix = mlx5e_bits_invert(i, MLX5E_LOG_INDIR_RQT_SIZE);
1685 ix = priv->params.indirection_rqt[ix];
1686 rqn = test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1687 priv->channel[ix]->rq.rqn :
1689 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
1693 static void mlx5e_fill_direct_rqt_rqn(struct mlx5e_priv *priv, void *rqtc,
1696 u32 rqn = test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1697 priv->channel[ix]->rq.rqn :
1700 MLX5_SET(rqtc, rqtc, rq_num[0], rqn);
1703 static int mlx5e_create_rqt(struct mlx5e_priv *priv, int sz,
1704 int ix, struct mlx5e_rqt *rqt)
1706 struct mlx5_core_dev *mdev = priv->mdev;
1712 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
1713 in = mlx5_vzalloc(inlen);
1717 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
1719 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1720 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
1722 if (sz > 1) /* RSS */
1723 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1725 mlx5e_fill_direct_rqt_rqn(priv, rqtc, ix);
1727 err = mlx5_core_create_rqt(mdev, in, inlen, &rqt->rqtn);
1729 rqt->enabled = true;
1735 void mlx5e_destroy_rqt(struct mlx5e_priv *priv, struct mlx5e_rqt *rqt)
1737 rqt->enabled = false;
1738 mlx5_core_destroy_rqt(priv->mdev, rqt->rqtn);
1741 static int mlx5e_create_indirect_rqts(struct mlx5e_priv *priv)
1743 struct mlx5e_rqt *rqt = &priv->indir_rqt;
1745 return mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, 0, rqt);
1748 int mlx5e_create_direct_rqts(struct mlx5e_priv *priv)
1750 struct mlx5e_rqt *rqt;
1754 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
1755 rqt = &priv->direct_tir[ix].rqt;
1756 err = mlx5e_create_rqt(priv, 1 /*size */, ix, rqt);
1758 goto err_destroy_rqts;
1764 for (ix--; ix >= 0; ix--)
1765 mlx5e_destroy_rqt(priv, &priv->direct_tir[ix].rqt);
1770 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz, int ix)
1772 struct mlx5_core_dev *mdev = priv->mdev;
1778 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
1779 in = mlx5_vzalloc(inlen);
1783 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
1785 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1786 if (sz > 1) /* RSS */
1787 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1789 mlx5e_fill_direct_rqt_rqn(priv, rqtc, ix);
1791 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
1793 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
1800 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv)
1805 if (priv->indir_rqt.enabled) {
1806 rqtn = priv->indir_rqt.rqtn;
1807 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, 0);
1810 for (ix = 0; ix < priv->params.num_channels; ix++) {
1811 if (!priv->direct_tir[ix].rqt.enabled)
1813 rqtn = priv->direct_tir[ix].rqt.rqtn;
1814 mlx5e_redirect_rqt(priv, rqtn, 1, ix);
1818 static void mlx5e_build_tir_ctx_lro(void *tirc, struct mlx5e_priv *priv)
1820 if (!priv->params.lro_en)
1823 #define ROUGH_MAX_L2_L3_HDR_SZ 256
1825 MLX5_SET(tirc, tirc, lro_enable_mask,
1826 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
1827 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
1828 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
1829 (priv->params.lro_wqe_sz -
1830 ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
1831 MLX5_SET(tirc, tirc, lro_timeout_period_usecs,
1832 MLX5_CAP_ETH(priv->mdev,
1833 lro_timer_supported_periods[2]));
1836 void mlx5e_build_tir_ctx_hash(void *tirc, struct mlx5e_priv *priv)
1838 MLX5_SET(tirc, tirc, rx_hash_fn,
1839 mlx5e_rx_hash_fn(priv->params.rss_hfunc));
1840 if (priv->params.rss_hfunc == ETH_RSS_HASH_TOP) {
1841 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
1842 rx_hash_toeplitz_key);
1843 size_t len = MLX5_FLD_SZ_BYTES(tirc,
1844 rx_hash_toeplitz_key);
1846 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1847 memcpy(rss_key, priv->params.toeplitz_hash_key, len);
1851 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
1853 struct mlx5_core_dev *mdev = priv->mdev;
1862 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
1863 in = mlx5_vzalloc(inlen);
1867 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
1868 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
1870 mlx5e_build_tir_ctx_lro(tirc, priv);
1872 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
1873 err = mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in,
1879 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
1880 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
1892 static int mlx5e_set_mtu(struct mlx5e_priv *priv, u16 mtu)
1894 struct mlx5_core_dev *mdev = priv->mdev;
1895 u16 hw_mtu = MLX5E_SW2HW_MTU(mtu);
1898 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
1902 /* Update vport context MTU */
1903 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
1907 static void mlx5e_query_mtu(struct mlx5e_priv *priv, u16 *mtu)
1909 struct mlx5_core_dev *mdev = priv->mdev;
1913 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
1914 if (err || !hw_mtu) /* fallback to port oper mtu */
1915 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
1917 *mtu = MLX5E_HW2SW_MTU(hw_mtu);
1920 static int mlx5e_set_dev_port_mtu(struct net_device *netdev)
1922 struct mlx5e_priv *priv = netdev_priv(netdev);
1926 err = mlx5e_set_mtu(priv, netdev->mtu);
1930 mlx5e_query_mtu(priv, &mtu);
1931 if (mtu != netdev->mtu)
1932 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
1933 __func__, mtu, netdev->mtu);
1939 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
1941 struct mlx5e_priv *priv = netdev_priv(netdev);
1942 int nch = priv->params.num_channels;
1943 int ntc = priv->params.num_tc;
1946 netdev_reset_tc(netdev);
1951 netdev_set_num_tc(netdev, ntc);
1953 /* Map netdev TCs to offset 0
1954 * We have our own UP to TXQ mapping for QoS
1956 for (tc = 0; tc < ntc; tc++)
1957 netdev_set_tc_queue(netdev, tc, nch, 0);
1960 int mlx5e_open_locked(struct net_device *netdev)
1962 struct mlx5e_priv *priv = netdev_priv(netdev);
1963 struct mlx5_core_dev *mdev = priv->mdev;
1967 set_bit(MLX5E_STATE_OPENED, &priv->state);
1969 mlx5e_netdev_set_tcs(netdev);
1971 num_txqs = priv->params.num_channels * priv->params.num_tc;
1972 netif_set_real_num_tx_queues(netdev, num_txqs);
1973 netif_set_real_num_rx_queues(netdev, priv->params.num_channels);
1975 err = mlx5e_open_channels(priv);
1977 netdev_err(netdev, "%s: mlx5e_open_channels failed, %d\n",
1979 goto err_clear_state_opened_flag;
1982 err = mlx5e_refresh_tirs_self_loopback_enable(priv->mdev);
1984 netdev_err(netdev, "%s: mlx5e_refresh_tirs_self_loopback_enable failed, %d\n",
1986 goto err_close_channels;
1989 mlx5e_redirect_rqts(priv);
1990 mlx5e_update_carrier(priv);
1991 mlx5e_timestamp_init(priv);
1992 #ifdef CONFIG_RFS_ACCEL
1993 priv->netdev->rx_cpu_rmap = priv->mdev->rmap;
1995 if (priv->profile->update_stats)
1996 queue_delayed_work(priv->wq, &priv->update_stats_work, 0);
1998 if (MLX5_CAP_GEN(mdev, vport_group_manager)) {
1999 err = mlx5e_add_sqs_fwd_rules(priv);
2001 goto err_close_channels;
2006 mlx5e_close_channels(priv);
2007 err_clear_state_opened_flag:
2008 clear_bit(MLX5E_STATE_OPENED, &priv->state);
2012 int mlx5e_open(struct net_device *netdev)
2014 struct mlx5e_priv *priv = netdev_priv(netdev);
2017 mutex_lock(&priv->state_lock);
2018 err = mlx5e_open_locked(netdev);
2019 mutex_unlock(&priv->state_lock);
2024 int mlx5e_close_locked(struct net_device *netdev)
2026 struct mlx5e_priv *priv = netdev_priv(netdev);
2027 struct mlx5_core_dev *mdev = priv->mdev;
2029 /* May already be CLOSED in case a previous configuration operation
2030 * (e.g RX/TX queue size change) that involves close&open failed.
2032 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
2035 clear_bit(MLX5E_STATE_OPENED, &priv->state);
2037 if (MLX5_CAP_GEN(mdev, vport_group_manager))
2038 mlx5e_remove_sqs_fwd_rules(priv);
2040 mlx5e_timestamp_cleanup(priv);
2041 netif_carrier_off(priv->netdev);
2042 mlx5e_redirect_rqts(priv);
2043 mlx5e_close_channels(priv);
2048 int mlx5e_close(struct net_device *netdev)
2050 struct mlx5e_priv *priv = netdev_priv(netdev);
2053 if (!netif_device_present(netdev))
2056 mutex_lock(&priv->state_lock);
2057 err = mlx5e_close_locked(netdev);
2058 mutex_unlock(&priv->state_lock);
2063 static int mlx5e_create_drop_rq(struct mlx5e_priv *priv,
2064 struct mlx5e_rq *rq,
2065 struct mlx5e_rq_param *param)
2067 struct mlx5_core_dev *mdev = priv->mdev;
2068 void *rqc = param->rqc;
2069 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
2072 param->wq.db_numa_node = param->wq.buf_numa_node;
2074 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
2084 static int mlx5e_create_drop_cq(struct mlx5e_priv *priv,
2085 struct mlx5e_cq *cq,
2086 struct mlx5e_cq_param *param)
2088 struct mlx5_core_dev *mdev = priv->mdev;
2089 struct mlx5_core_cq *mcq = &cq->mcq;
2094 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
2099 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
2102 mcq->set_ci_db = cq->wq_ctrl.db.db;
2103 mcq->arm_db = cq->wq_ctrl.db.db + 1;
2104 *mcq->set_ci_db = 0;
2106 mcq->vector = param->eq_ix;
2107 mcq->comp = mlx5e_completion_event;
2108 mcq->event = mlx5e_cq_error_event;
2110 mcq->uar = &mdev->mlx5e_res.cq_uar;
2117 static int mlx5e_open_drop_rq(struct mlx5e_priv *priv)
2119 struct mlx5e_cq_param cq_param;
2120 struct mlx5e_rq_param rq_param;
2121 struct mlx5e_rq *rq = &priv->drop_rq;
2122 struct mlx5e_cq *cq = &priv->drop_rq.cq;
2125 memset(&cq_param, 0, sizeof(cq_param));
2126 memset(&rq_param, 0, sizeof(rq_param));
2127 mlx5e_build_drop_rq_param(&rq_param);
2129 err = mlx5e_create_drop_cq(priv, cq, &cq_param);
2133 err = mlx5e_enable_cq(cq, &cq_param);
2135 goto err_destroy_cq;
2137 err = mlx5e_create_drop_rq(priv, rq, &rq_param);
2139 goto err_disable_cq;
2141 err = mlx5e_enable_rq(rq, &rq_param);
2143 goto err_destroy_rq;
2148 mlx5e_destroy_rq(&priv->drop_rq);
2151 mlx5e_disable_cq(&priv->drop_rq.cq);
2154 mlx5e_destroy_cq(&priv->drop_rq.cq);
2159 static void mlx5e_close_drop_rq(struct mlx5e_priv *priv)
2161 mlx5e_disable_rq(&priv->drop_rq);
2162 mlx5e_destroy_rq(&priv->drop_rq);
2163 mlx5e_disable_cq(&priv->drop_rq.cq);
2164 mlx5e_destroy_cq(&priv->drop_rq.cq);
2167 static int mlx5e_create_tis(struct mlx5e_priv *priv, int tc)
2169 struct mlx5_core_dev *mdev = priv->mdev;
2170 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
2171 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
2173 MLX5_SET(tisc, tisc, prio, tc << 1);
2174 MLX5_SET(tisc, tisc, transport_domain, mdev->mlx5e_res.td.tdn);
2176 if (mlx5_lag_is_lacp_owner(mdev))
2177 MLX5_SET(tisc, tisc, strict_lag_tx_port_affinity, 1);
2179 return mlx5_core_create_tis(mdev, in, sizeof(in), &priv->tisn[tc]);
2182 static void mlx5e_destroy_tis(struct mlx5e_priv *priv, int tc)
2184 mlx5_core_destroy_tis(priv->mdev, priv->tisn[tc]);
2187 int mlx5e_create_tises(struct mlx5e_priv *priv)
2192 for (tc = 0; tc < priv->profile->max_tc; tc++) {
2193 err = mlx5e_create_tis(priv, tc);
2195 goto err_close_tises;
2201 for (tc--; tc >= 0; tc--)
2202 mlx5e_destroy_tis(priv, tc);
2207 void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv)
2211 for (tc = 0; tc < priv->profile->max_tc; tc++)
2212 mlx5e_destroy_tis(priv, tc);
2215 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv, u32 *tirc,
2216 enum mlx5e_traffic_types tt)
2218 void *hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2220 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2222 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
2223 MLX5_HASH_FIELD_SEL_DST_IP)
2225 #define MLX5_HASH_IP_L4PORTS (MLX5_HASH_FIELD_SEL_SRC_IP |\
2226 MLX5_HASH_FIELD_SEL_DST_IP |\
2227 MLX5_HASH_FIELD_SEL_L4_SPORT |\
2228 MLX5_HASH_FIELD_SEL_L4_DPORT)
2230 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
2231 MLX5_HASH_FIELD_SEL_DST_IP |\
2232 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
2234 mlx5e_build_tir_ctx_lro(tirc, priv);
2236 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2237 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
2238 mlx5e_build_tir_ctx_hash(tirc, priv);
2241 case MLX5E_TT_IPV4_TCP:
2242 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2243 MLX5_L3_PROT_TYPE_IPV4);
2244 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2245 MLX5_L4_PROT_TYPE_TCP);
2246 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2247 MLX5_HASH_IP_L4PORTS);
2250 case MLX5E_TT_IPV6_TCP:
2251 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2252 MLX5_L3_PROT_TYPE_IPV6);
2253 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2254 MLX5_L4_PROT_TYPE_TCP);
2255 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2256 MLX5_HASH_IP_L4PORTS);
2259 case MLX5E_TT_IPV4_UDP:
2260 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2261 MLX5_L3_PROT_TYPE_IPV4);
2262 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2263 MLX5_L4_PROT_TYPE_UDP);
2264 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2265 MLX5_HASH_IP_L4PORTS);
2268 case MLX5E_TT_IPV6_UDP:
2269 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2270 MLX5_L3_PROT_TYPE_IPV6);
2271 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2272 MLX5_L4_PROT_TYPE_UDP);
2273 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2274 MLX5_HASH_IP_L4PORTS);
2277 case MLX5E_TT_IPV4_IPSEC_AH:
2278 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2279 MLX5_L3_PROT_TYPE_IPV4);
2280 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2281 MLX5_HASH_IP_IPSEC_SPI);
2284 case MLX5E_TT_IPV6_IPSEC_AH:
2285 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2286 MLX5_L3_PROT_TYPE_IPV6);
2287 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2288 MLX5_HASH_IP_IPSEC_SPI);
2291 case MLX5E_TT_IPV4_IPSEC_ESP:
2292 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2293 MLX5_L3_PROT_TYPE_IPV4);
2294 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2295 MLX5_HASH_IP_IPSEC_SPI);
2298 case MLX5E_TT_IPV6_IPSEC_ESP:
2299 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2300 MLX5_L3_PROT_TYPE_IPV6);
2301 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2302 MLX5_HASH_IP_IPSEC_SPI);
2306 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2307 MLX5_L3_PROT_TYPE_IPV4);
2308 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2313 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2314 MLX5_L3_PROT_TYPE_IPV6);
2315 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2320 "mlx5e_build_indir_tir_ctx: bad traffic type!\n");
2324 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 *tirc,
2327 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2329 mlx5e_build_tir_ctx_lro(tirc, priv);
2331 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2332 MLX5_SET(tirc, tirc, indirect_table, rqtn);
2333 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
2336 static int mlx5e_create_indirect_tirs(struct mlx5e_priv *priv)
2338 struct mlx5e_tir *tir;
2345 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2346 in = mlx5_vzalloc(inlen);
2350 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2351 memset(in, 0, inlen);
2352 tir = &priv->indir_tir[tt];
2353 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2354 mlx5e_build_indir_tir_ctx(priv, tirc, tt);
2355 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
2357 goto err_destroy_tirs;
2365 for (tt--; tt >= 0; tt--)
2366 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[tt]);
2373 int mlx5e_create_direct_tirs(struct mlx5e_priv *priv)
2375 int nch = priv->profile->max_nch(priv->mdev);
2376 struct mlx5e_tir *tir;
2383 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2384 in = mlx5_vzalloc(inlen);
2388 for (ix = 0; ix < nch; ix++) {
2389 memset(in, 0, inlen);
2390 tir = &priv->direct_tir[ix];
2391 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2392 mlx5e_build_direct_tir_ctx(priv, tirc,
2393 priv->direct_tir[ix].rqt.rqtn);
2394 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
2396 goto err_destroy_ch_tirs;
2403 err_destroy_ch_tirs:
2404 for (ix--; ix >= 0; ix--)
2405 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[ix]);
2412 static void mlx5e_destroy_indirect_tirs(struct mlx5e_priv *priv)
2416 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
2417 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[i]);
2420 void mlx5e_destroy_direct_tirs(struct mlx5e_priv *priv)
2422 int nch = priv->profile->max_nch(priv->mdev);
2425 for (i = 0; i < nch; i++)
2426 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[i]);
2429 int mlx5e_modify_rqs_vsd(struct mlx5e_priv *priv, bool vsd)
2434 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
2437 for (i = 0; i < priv->params.num_channels; i++) {
2438 err = mlx5e_modify_rq_vsd(&priv->channel[i]->rq, vsd);
2446 static int mlx5e_setup_tc(struct net_device *netdev, u8 tc)
2448 struct mlx5e_priv *priv = netdev_priv(netdev);
2452 if (tc && tc != MLX5E_MAX_NUM_TC)
2455 mutex_lock(&priv->state_lock);
2457 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2459 mlx5e_close_locked(priv->netdev);
2461 priv->params.num_tc = tc ? tc : 1;
2464 err = mlx5e_open_locked(priv->netdev);
2466 mutex_unlock(&priv->state_lock);
2471 static int mlx5e_ndo_setup_tc(struct net_device *dev, u32 handle,
2472 __be16 proto, struct tc_to_netdev *tc)
2474 struct mlx5e_priv *priv = netdev_priv(dev);
2476 if (TC_H_MAJ(handle) != TC_H_MAJ(TC_H_INGRESS))
2480 case TC_SETUP_CLSFLOWER:
2481 switch (tc->cls_flower->command) {
2482 case TC_CLSFLOWER_REPLACE:
2483 return mlx5e_configure_flower(priv, proto, tc->cls_flower);
2484 case TC_CLSFLOWER_DESTROY:
2485 return mlx5e_delete_flower(priv, tc->cls_flower);
2486 case TC_CLSFLOWER_STATS:
2487 return mlx5e_stats_flower(priv, tc->cls_flower);
2494 if (tc->type != TC_SETUP_MQPRIO)
2497 return mlx5e_setup_tc(dev, tc->tc);
2500 struct rtnl_link_stats64 *
2501 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
2503 struct mlx5e_priv *priv = netdev_priv(dev);
2504 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
2505 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
2506 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
2508 stats->rx_packets = sstats->rx_packets;
2509 stats->rx_bytes = sstats->rx_bytes;
2510 stats->tx_packets = sstats->tx_packets;
2511 stats->tx_bytes = sstats->tx_bytes;
2513 stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
2514 stats->tx_dropped = sstats->tx_queue_dropped;
2516 stats->rx_length_errors =
2517 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
2518 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
2519 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
2520 stats->rx_crc_errors =
2521 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
2522 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
2523 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
2524 stats->tx_carrier_errors =
2525 PPORT_802_3_GET(pstats, a_symbol_error_during_carrier);
2526 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
2527 stats->rx_frame_errors;
2528 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
2530 /* vport multicast also counts packets that are dropped due to steering
2531 * or rx out of buffer
2534 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
2539 static void mlx5e_set_rx_mode(struct net_device *dev)
2541 struct mlx5e_priv *priv = netdev_priv(dev);
2543 queue_work(priv->wq, &priv->set_rx_mode_work);
2546 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
2548 struct mlx5e_priv *priv = netdev_priv(netdev);
2549 struct sockaddr *saddr = addr;
2551 if (!is_valid_ether_addr(saddr->sa_data))
2552 return -EADDRNOTAVAIL;
2554 netif_addr_lock_bh(netdev);
2555 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
2556 netif_addr_unlock_bh(netdev);
2558 queue_work(priv->wq, &priv->set_rx_mode_work);
2563 #define MLX5E_SET_FEATURE(netdev, feature, enable) \
2566 netdev->features |= feature; \
2568 netdev->features &= ~feature; \
2571 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
2573 static int set_feature_lro(struct net_device *netdev, bool enable)
2575 struct mlx5e_priv *priv = netdev_priv(netdev);
2576 bool was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2579 mutex_lock(&priv->state_lock);
2581 if (was_opened && (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST))
2582 mlx5e_close_locked(priv->netdev);
2584 priv->params.lro_en = enable;
2585 err = mlx5e_modify_tirs_lro(priv);
2587 netdev_err(netdev, "lro modify failed, %d\n", err);
2588 priv->params.lro_en = !enable;
2591 if (was_opened && (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST))
2592 mlx5e_open_locked(priv->netdev);
2594 mutex_unlock(&priv->state_lock);
2599 static int set_feature_vlan_filter(struct net_device *netdev, bool enable)
2601 struct mlx5e_priv *priv = netdev_priv(netdev);
2604 mlx5e_enable_vlan_filter(priv);
2606 mlx5e_disable_vlan_filter(priv);
2611 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
2613 struct mlx5e_priv *priv = netdev_priv(netdev);
2615 if (!enable && mlx5e_tc_num_filters(priv)) {
2617 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
2624 static int set_feature_rx_all(struct net_device *netdev, bool enable)
2626 struct mlx5e_priv *priv = netdev_priv(netdev);
2627 struct mlx5_core_dev *mdev = priv->mdev;
2629 return mlx5_set_port_fcs(mdev, !enable);
2632 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
2634 struct mlx5e_priv *priv = netdev_priv(netdev);
2637 mutex_lock(&priv->state_lock);
2639 priv->params.vlan_strip_disable = !enable;
2640 err = mlx5e_modify_rqs_vsd(priv, !enable);
2642 priv->params.vlan_strip_disable = enable;
2644 mutex_unlock(&priv->state_lock);
2649 #ifdef CONFIG_RFS_ACCEL
2650 static int set_feature_arfs(struct net_device *netdev, bool enable)
2652 struct mlx5e_priv *priv = netdev_priv(netdev);
2656 err = mlx5e_arfs_enable(priv);
2658 err = mlx5e_arfs_disable(priv);
2664 static int mlx5e_handle_feature(struct net_device *netdev,
2665 netdev_features_t wanted_features,
2666 netdev_features_t feature,
2667 mlx5e_feature_handler feature_handler)
2669 netdev_features_t changes = wanted_features ^ netdev->features;
2670 bool enable = !!(wanted_features & feature);
2673 if (!(changes & feature))
2676 err = feature_handler(netdev, enable);
2678 netdev_err(netdev, "%s feature 0x%llx failed err %d\n",
2679 enable ? "Enable" : "Disable", feature, err);
2683 MLX5E_SET_FEATURE(netdev, feature, enable);
2687 static int mlx5e_set_features(struct net_device *netdev,
2688 netdev_features_t features)
2692 err = mlx5e_handle_feature(netdev, features, NETIF_F_LRO,
2694 err |= mlx5e_handle_feature(netdev, features,
2695 NETIF_F_HW_VLAN_CTAG_FILTER,
2696 set_feature_vlan_filter);
2697 err |= mlx5e_handle_feature(netdev, features, NETIF_F_HW_TC,
2698 set_feature_tc_num_filters);
2699 err |= mlx5e_handle_feature(netdev, features, NETIF_F_RXALL,
2700 set_feature_rx_all);
2701 err |= mlx5e_handle_feature(netdev, features, NETIF_F_HW_VLAN_CTAG_RX,
2702 set_feature_rx_vlan);
2703 #ifdef CONFIG_RFS_ACCEL
2704 err |= mlx5e_handle_feature(netdev, features, NETIF_F_NTUPLE,
2708 return err ? -EINVAL : 0;
2711 #define MXL5_HW_MIN_MTU 64
2712 #define MXL5E_MIN_MTU (MXL5_HW_MIN_MTU + ETH_FCS_LEN)
2714 static int mlx5e_change_mtu(struct net_device *netdev, int new_mtu)
2716 struct mlx5e_priv *priv = netdev_priv(netdev);
2717 struct mlx5_core_dev *mdev = priv->mdev;
2724 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
2726 max_mtu = MLX5E_HW2SW_MTU(max_mtu);
2727 min_mtu = MLX5E_HW2SW_MTU(MXL5E_MIN_MTU);
2729 if (new_mtu > max_mtu || new_mtu < min_mtu) {
2731 "%s: Bad MTU (%d), valid range is: [%d..%d]\n",
2732 __func__, new_mtu, min_mtu, max_mtu);
2736 mutex_lock(&priv->state_lock);
2738 reset = !priv->params.lro_en &&
2739 (priv->params.rq_wq_type !=
2740 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ);
2742 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2743 if (was_opened && reset)
2744 mlx5e_close_locked(netdev);
2746 netdev->mtu = new_mtu;
2747 mlx5e_set_dev_port_mtu(netdev);
2749 if (was_opened && reset)
2750 err = mlx5e_open_locked(netdev);
2752 mutex_unlock(&priv->state_lock);
2757 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2761 return mlx5e_hwstamp_set(dev, ifr);
2763 return mlx5e_hwstamp_get(dev, ifr);
2769 static int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2771 struct mlx5e_priv *priv = netdev_priv(dev);
2772 struct mlx5_core_dev *mdev = priv->mdev;
2774 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
2777 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos)
2779 struct mlx5e_priv *priv = netdev_priv(dev);
2780 struct mlx5_core_dev *mdev = priv->mdev;
2782 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
2786 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2788 struct mlx5e_priv *priv = netdev_priv(dev);
2789 struct mlx5_core_dev *mdev = priv->mdev;
2791 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
2794 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
2796 struct mlx5e_priv *priv = netdev_priv(dev);
2797 struct mlx5_core_dev *mdev = priv->mdev;
2799 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
2801 static int mlx5_vport_link2ifla(u8 esw_link)
2804 case MLX5_ESW_VPORT_ADMIN_STATE_DOWN:
2805 return IFLA_VF_LINK_STATE_DISABLE;
2806 case MLX5_ESW_VPORT_ADMIN_STATE_UP:
2807 return IFLA_VF_LINK_STATE_ENABLE;
2809 return IFLA_VF_LINK_STATE_AUTO;
2812 static int mlx5_ifla_link2vport(u8 ifla_link)
2814 switch (ifla_link) {
2815 case IFLA_VF_LINK_STATE_DISABLE:
2816 return MLX5_ESW_VPORT_ADMIN_STATE_DOWN;
2817 case IFLA_VF_LINK_STATE_ENABLE:
2818 return MLX5_ESW_VPORT_ADMIN_STATE_UP;
2820 return MLX5_ESW_VPORT_ADMIN_STATE_AUTO;
2823 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
2826 struct mlx5e_priv *priv = netdev_priv(dev);
2827 struct mlx5_core_dev *mdev = priv->mdev;
2829 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
2830 mlx5_ifla_link2vport(link_state));
2833 static int mlx5e_get_vf_config(struct net_device *dev,
2834 int vf, struct ifla_vf_info *ivi)
2836 struct mlx5e_priv *priv = netdev_priv(dev);
2837 struct mlx5_core_dev *mdev = priv->mdev;
2840 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
2843 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
2847 static int mlx5e_get_vf_stats(struct net_device *dev,
2848 int vf, struct ifla_vf_stats *vf_stats)
2850 struct mlx5e_priv *priv = netdev_priv(dev);
2851 struct mlx5_core_dev *mdev = priv->mdev;
2853 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
2857 static void mlx5e_add_vxlan_port(struct net_device *netdev,
2858 struct udp_tunnel_info *ti)
2860 struct mlx5e_priv *priv = netdev_priv(netdev);
2862 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2865 if (!mlx5e_vxlan_allowed(priv->mdev))
2868 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 1);
2871 static void mlx5e_del_vxlan_port(struct net_device *netdev,
2872 struct udp_tunnel_info *ti)
2874 struct mlx5e_priv *priv = netdev_priv(netdev);
2876 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2879 if (!mlx5e_vxlan_allowed(priv->mdev))
2882 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 0);
2885 static netdev_features_t mlx5e_vxlan_features_check(struct mlx5e_priv *priv,
2886 struct sk_buff *skb,
2887 netdev_features_t features)
2889 struct udphdr *udph;
2893 switch (vlan_get_protocol(skb)) {
2894 case htons(ETH_P_IP):
2895 proto = ip_hdr(skb)->protocol;
2897 case htons(ETH_P_IPV6):
2898 proto = ipv6_hdr(skb)->nexthdr;
2904 if (proto == IPPROTO_UDP) {
2905 udph = udp_hdr(skb);
2906 port = be16_to_cpu(udph->dest);
2909 /* Verify if UDP port is being offloaded by HW */
2910 if (port && mlx5e_vxlan_lookup_port(priv, port))
2914 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
2915 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2918 static netdev_features_t mlx5e_features_check(struct sk_buff *skb,
2919 struct net_device *netdev,
2920 netdev_features_t features)
2922 struct mlx5e_priv *priv = netdev_priv(netdev);
2924 features = vlan_features_check(skb, features);
2925 features = vxlan_features_check(skb, features);
2927 /* Validate if the tunneled packet is being offloaded by HW */
2928 if (skb->encapsulation &&
2929 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
2930 return mlx5e_vxlan_features_check(priv, skb, features);
2935 static void mlx5e_tx_timeout(struct net_device *dev)
2937 struct mlx5e_priv *priv = netdev_priv(dev);
2938 bool sched_work = false;
2941 netdev_err(dev, "TX timeout detected\n");
2943 for (i = 0; i < priv->params.num_channels * priv->params.num_tc; i++) {
2944 struct mlx5e_sq *sq = priv->txq_to_sq_map[i];
2946 if (!netif_xmit_stopped(netdev_get_tx_queue(dev, i)))
2949 set_bit(MLX5E_SQ_STATE_FLUSH, &sq->state);
2950 netdev_err(dev, "TX timeout on queue: %d, SQ: 0x%x, CQ: 0x%x, SQ Cons: 0x%x SQ Prod: 0x%x\n",
2951 i, sq->sqn, sq->cq.mcq.cqn, sq->cc, sq->pc);
2954 if (sched_work && test_bit(MLX5E_STATE_OPENED, &priv->state))
2955 schedule_work(&priv->tx_timeout_work);
2958 static const struct net_device_ops mlx5e_netdev_ops_basic = {
2959 .ndo_open = mlx5e_open,
2960 .ndo_stop = mlx5e_close,
2961 .ndo_start_xmit = mlx5e_xmit,
2962 .ndo_setup_tc = mlx5e_ndo_setup_tc,
2963 .ndo_select_queue = mlx5e_select_queue,
2964 .ndo_get_stats64 = mlx5e_get_stats,
2965 .ndo_set_rx_mode = mlx5e_set_rx_mode,
2966 .ndo_set_mac_address = mlx5e_set_mac,
2967 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
2968 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
2969 .ndo_set_features = mlx5e_set_features,
2970 .ndo_change_mtu = mlx5e_change_mtu,
2971 .ndo_do_ioctl = mlx5e_ioctl,
2972 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
2973 #ifdef CONFIG_RFS_ACCEL
2974 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
2976 .ndo_tx_timeout = mlx5e_tx_timeout,
2979 static const struct net_device_ops mlx5e_netdev_ops_sriov = {
2980 .ndo_open = mlx5e_open,
2981 .ndo_stop = mlx5e_close,
2982 .ndo_start_xmit = mlx5e_xmit,
2983 .ndo_setup_tc = mlx5e_ndo_setup_tc,
2984 .ndo_select_queue = mlx5e_select_queue,
2985 .ndo_get_stats64 = mlx5e_get_stats,
2986 .ndo_set_rx_mode = mlx5e_set_rx_mode,
2987 .ndo_set_mac_address = mlx5e_set_mac,
2988 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
2989 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
2990 .ndo_set_features = mlx5e_set_features,
2991 .ndo_change_mtu = mlx5e_change_mtu,
2992 .ndo_do_ioctl = mlx5e_ioctl,
2993 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
2994 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
2995 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
2996 .ndo_features_check = mlx5e_features_check,
2997 #ifdef CONFIG_RFS_ACCEL
2998 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
3000 .ndo_set_vf_mac = mlx5e_set_vf_mac,
3001 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
3002 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
3003 .ndo_set_vf_trust = mlx5e_set_vf_trust,
3004 .ndo_get_vf_config = mlx5e_get_vf_config,
3005 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
3006 .ndo_get_vf_stats = mlx5e_get_vf_stats,
3007 .ndo_tx_timeout = mlx5e_tx_timeout,
3010 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
3012 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
3014 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
3015 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
3016 !MLX5_CAP_ETH(mdev, csum_cap) ||
3017 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
3018 !MLX5_CAP_ETH(mdev, vlan_cap) ||
3019 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
3020 MLX5_CAP_FLOWTABLE(mdev,
3021 flow_table_properties_nic_receive.max_ft_level)
3023 mlx5_core_warn(mdev,
3024 "Not creating net device, some required device capabilities are missing\n");
3027 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
3028 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
3029 if (!MLX5_CAP_GEN(mdev, cq_moderation))
3030 mlx5_core_warn(mdev, "CQ modiration is not supported\n");
3035 u16 mlx5e_get_max_inline_cap(struct mlx5_core_dev *mdev)
3037 int bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
3039 return bf_buf_size -
3040 sizeof(struct mlx5e_tx_wqe) +
3041 2 /*sizeof(mlx5e_tx_wqe.inline_hdr_start)*/;
3044 #ifdef CONFIG_MLX5_CORE_EN_DCB
3045 static void mlx5e_ets_init(struct mlx5e_priv *priv)
3049 priv->params.ets.ets_cap = mlx5_max_tc(priv->mdev) + 1;
3050 for (i = 0; i < priv->params.ets.ets_cap; i++) {
3051 priv->params.ets.tc_tx_bw[i] = MLX5E_MAX_BW_ALLOC;
3052 priv->params.ets.tc_tsa[i] = IEEE_8021QAZ_TSA_VENDOR;
3053 priv->params.ets.prio_tc[i] = i;
3056 /* tclass[prio=0]=1, tclass[prio=1]=0, tclass[prio=i]=i (for i>1) */
3057 priv->params.ets.prio_tc[0] = 1;
3058 priv->params.ets.prio_tc[1] = 0;
3062 void mlx5e_build_default_indir_rqt(struct mlx5_core_dev *mdev,
3063 u32 *indirection_rqt, int len,
3066 int node = mdev->priv.numa_node;
3067 int node_num_of_cores;
3071 node = first_online_node;
3073 node_num_of_cores = cpumask_weight(cpumask_of_node(node));
3075 if (node_num_of_cores)
3076 num_channels = min_t(int, num_channels, node_num_of_cores);
3078 for (i = 0; i < len; i++)
3079 indirection_rqt[i] = i % num_channels;
3082 static int mlx5e_get_pci_bw(struct mlx5_core_dev *mdev, u32 *pci_bw)
3084 enum pcie_link_width width;
3085 enum pci_bus_speed speed;
3088 err = pcie_get_minimum_link(mdev->pdev, &speed, &width);
3092 if (speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
3096 case PCIE_SPEED_2_5GT:
3097 *pci_bw = 2500 * width;
3099 case PCIE_SPEED_5_0GT:
3100 *pci_bw = 5000 * width;
3102 case PCIE_SPEED_8_0GT:
3103 *pci_bw = 8000 * width;
3112 static bool cqe_compress_heuristic(u32 link_speed, u32 pci_bw)
3114 return (link_speed && pci_bw &&
3115 (pci_bw < 40000) && (pci_bw < link_speed));
3118 void mlx5e_set_rx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
3120 params->rx_cq_period_mode = cq_period_mode;
3122 params->rx_cq_moderation.pkts =
3123 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
3124 params->rx_cq_moderation.usec =
3125 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
3127 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
3128 params->rx_cq_moderation.usec =
3129 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE;
3132 static void mlx5e_query_min_inline(struct mlx5_core_dev *mdev,
3133 u8 *min_inline_mode)
3135 switch (MLX5_CAP_ETH(mdev, wqe_inline_mode)) {
3136 case MLX5E_INLINE_MODE_L2:
3137 *min_inline_mode = MLX5_INLINE_MODE_L2;
3139 case MLX5E_INLINE_MODE_VPORT_CONTEXT:
3140 mlx5_query_nic_vport_min_inline(mdev,
3143 case MLX5_INLINE_MODE_NOT_REQUIRED:
3144 *min_inline_mode = MLX5_INLINE_MODE_NONE;
3149 static void mlx5e_build_nic_netdev_priv(struct mlx5_core_dev *mdev,
3150 struct net_device *netdev,
3151 const struct mlx5e_profile *profile,
3154 struct mlx5e_priv *priv = netdev_priv(netdev);
3157 u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
3158 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
3159 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
3162 priv->netdev = netdev;
3163 priv->params.num_channels = profile->max_nch(mdev);
3164 priv->profile = profile;
3165 priv->ppriv = ppriv;
3167 priv->params.log_sq_size = MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
3169 /* set CQE compression */
3170 priv->params.rx_cqe_compress_admin = false;
3171 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
3172 MLX5_CAP_GEN(mdev, vport_group_manager)) {
3173 mlx5e_get_max_linkspeed(mdev, &link_speed);
3174 mlx5e_get_pci_bw(mdev, &pci_bw);
3175 mlx5_core_dbg(mdev, "Max link speed = %d, PCI BW = %d\n",
3176 link_speed, pci_bw);
3177 priv->params.rx_cqe_compress_admin =
3178 cqe_compress_heuristic(link_speed, pci_bw);
3180 priv->params.rx_cqe_compress = priv->params.rx_cqe_compress_admin;
3182 mlx5e_set_rq_priv_params(priv);
3183 if (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ)
3184 priv->params.lro_en = true;
3186 priv->params.rx_am_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
3187 mlx5e_set_rx_cq_mode_params(&priv->params, cq_period_mode);
3189 priv->params.tx_cq_moderation.usec =
3190 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
3191 priv->params.tx_cq_moderation.pkts =
3192 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
3193 priv->params.tx_max_inline = mlx5e_get_max_inline_cap(mdev);
3194 mlx5e_query_min_inline(mdev, &priv->params.tx_min_inline_mode);
3195 priv->params.num_tc = 1;
3196 priv->params.rss_hfunc = ETH_RSS_HASH_XOR;
3198 netdev_rss_key_fill(priv->params.toeplitz_hash_key,
3199 sizeof(priv->params.toeplitz_hash_key));
3201 mlx5e_build_default_indir_rqt(mdev, priv->params.indirection_rqt,
3202 MLX5E_INDIR_RQT_SIZE, profile->max_nch(mdev));
3204 priv->params.lro_wqe_sz =
3205 MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ -
3206 /* Extra room needed for build_skb */
3208 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
3210 /* Initialize pflags */
3211 MLX5E_SET_PRIV_FLAG(priv, MLX5E_PFLAG_RX_CQE_BASED_MODER,
3212 priv->params.rx_cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
3214 #ifdef CONFIG_MLX5_CORE_EN_DCB
3215 mlx5e_ets_init(priv);
3218 mutex_init(&priv->state_lock);
3220 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
3221 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
3222 INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work);
3223 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
3226 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
3228 struct mlx5e_priv *priv = netdev_priv(netdev);
3230 mlx5_query_nic_vport_mac_address(priv->mdev, 0, netdev->dev_addr);
3231 if (is_zero_ether_addr(netdev->dev_addr) &&
3232 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
3233 eth_hw_addr_random(netdev);
3234 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
3238 static const struct switchdev_ops mlx5e_switchdev_ops = {
3239 .switchdev_port_attr_get = mlx5e_attr_get,
3242 static void mlx5e_build_nic_netdev(struct net_device *netdev)
3244 struct mlx5e_priv *priv = netdev_priv(netdev);
3245 struct mlx5_core_dev *mdev = priv->mdev;
3249 SET_NETDEV_DEV(netdev, &mdev->pdev->dev);
3251 if (MLX5_CAP_GEN(mdev, vport_group_manager)) {
3252 netdev->netdev_ops = &mlx5e_netdev_ops_sriov;
3253 #ifdef CONFIG_MLX5_CORE_EN_DCB
3254 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
3257 netdev->netdev_ops = &mlx5e_netdev_ops_basic;
3260 netdev->watchdog_timeo = 15 * HZ;
3262 netdev->ethtool_ops = &mlx5e_ethtool_ops;
3264 netdev->vlan_features |= NETIF_F_SG;
3265 netdev->vlan_features |= NETIF_F_IP_CSUM;
3266 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
3267 netdev->vlan_features |= NETIF_F_GRO;
3268 netdev->vlan_features |= NETIF_F_TSO;
3269 netdev->vlan_features |= NETIF_F_TSO6;
3270 netdev->vlan_features |= NETIF_F_RXCSUM;
3271 netdev->vlan_features |= NETIF_F_RXHASH;
3273 if (!!MLX5_CAP_ETH(mdev, lro_cap))
3274 netdev->vlan_features |= NETIF_F_LRO;
3276 netdev->hw_features = netdev->vlan_features;
3277 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
3278 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
3279 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3281 if (mlx5e_vxlan_allowed(mdev)) {
3282 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
3283 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3284 NETIF_F_GSO_PARTIAL;
3285 netdev->hw_enc_features |= NETIF_F_IP_CSUM;
3286 netdev->hw_enc_features |= NETIF_F_IPV6_CSUM;
3287 netdev->hw_enc_features |= NETIF_F_TSO;
3288 netdev->hw_enc_features |= NETIF_F_TSO6;
3289 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL;
3290 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
3291 NETIF_F_GSO_PARTIAL;
3292 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
3295 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
3298 netdev->hw_features |= NETIF_F_RXALL;
3300 netdev->features = netdev->hw_features;
3301 if (!priv->params.lro_en)
3302 netdev->features &= ~NETIF_F_LRO;
3305 netdev->features &= ~NETIF_F_RXALL;
3307 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
3308 if (FT_CAP(flow_modify_en) &&
3309 FT_CAP(modify_root) &&
3310 FT_CAP(identified_miss_table_mode) &&
3311 FT_CAP(flow_table_modify)) {
3312 netdev->hw_features |= NETIF_F_HW_TC;
3313 #ifdef CONFIG_RFS_ACCEL
3314 netdev->hw_features |= NETIF_F_NTUPLE;
3318 netdev->features |= NETIF_F_HIGHDMA;
3320 netdev->priv_flags |= IFF_UNICAST_FLT;
3322 mlx5e_set_netdev_dev_addr(netdev);
3324 #ifdef CONFIG_NET_SWITCHDEV
3325 if (MLX5_CAP_GEN(mdev, vport_group_manager))
3326 netdev->switchdev_ops = &mlx5e_switchdev_ops;
3330 static void mlx5e_create_q_counter(struct mlx5e_priv *priv)
3332 struct mlx5_core_dev *mdev = priv->mdev;
3335 err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
3337 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
3338 priv->q_counter = 0;
3342 static void mlx5e_destroy_q_counter(struct mlx5e_priv *priv)
3344 if (!priv->q_counter)
3347 mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
3350 static int mlx5e_create_umr_mkey(struct mlx5e_priv *priv)
3352 struct mlx5_core_dev *mdev = priv->mdev;
3353 u64 npages = MLX5E_REQUIRED_MTTS(priv->profile->max_nch(mdev),
3354 BIT(MLX5E_PARAMS_MAXIMUM_LOG_RQ_SIZE_MPW));
3355 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
3360 in = mlx5_vzalloc(inlen);
3364 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
3366 npages = min_t(u32, ALIGN(U16_MAX, 4) * 2, npages);
3368 MLX5_SET(mkc, mkc, free, 1);
3369 MLX5_SET(mkc, mkc, umr_en, 1);
3370 MLX5_SET(mkc, mkc, lw, 1);
3371 MLX5_SET(mkc, mkc, lr, 1);
3372 MLX5_SET(mkc, mkc, access_mode, MLX5_MKC_ACCESS_MODE_MTT);
3374 MLX5_SET(mkc, mkc, qpn, 0xffffff);
3375 MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.pdn);
3376 MLX5_SET64(mkc, mkc, len, npages << PAGE_SHIFT);
3377 MLX5_SET(mkc, mkc, translations_octword_size,
3378 MLX5_MTT_OCTW(npages));
3379 MLX5_SET(mkc, mkc, log_page_size, PAGE_SHIFT);
3381 err = mlx5_core_create_mkey(mdev, &priv->umr_mkey, in, inlen);
3387 static void mlx5e_nic_init(struct mlx5_core_dev *mdev,
3388 struct net_device *netdev,
3389 const struct mlx5e_profile *profile,
3392 struct mlx5e_priv *priv = netdev_priv(netdev);
3394 mlx5e_build_nic_netdev_priv(mdev, netdev, profile, ppriv);
3395 mlx5e_build_nic_netdev(netdev);
3396 mlx5e_vxlan_init(priv);
3399 static void mlx5e_nic_cleanup(struct mlx5e_priv *priv)
3401 struct mlx5_core_dev *mdev = priv->mdev;
3402 struct mlx5_eswitch *esw = mdev->priv.eswitch;
3404 mlx5e_vxlan_cleanup(priv);
3406 if (MLX5_CAP_GEN(mdev, vport_group_manager))
3407 mlx5_eswitch_unregister_vport_rep(esw, 0);
3410 static int mlx5e_init_nic_rx(struct mlx5e_priv *priv)
3412 struct mlx5_core_dev *mdev = priv->mdev;
3416 err = mlx5e_create_indirect_rqts(priv);
3418 mlx5_core_warn(mdev, "create indirect rqts failed, %d\n", err);
3422 err = mlx5e_create_direct_rqts(priv);
3424 mlx5_core_warn(mdev, "create direct rqts failed, %d\n", err);
3425 goto err_destroy_indirect_rqts;
3428 err = mlx5e_create_indirect_tirs(priv);
3430 mlx5_core_warn(mdev, "create indirect tirs failed, %d\n", err);
3431 goto err_destroy_direct_rqts;
3434 err = mlx5e_create_direct_tirs(priv);
3436 mlx5_core_warn(mdev, "create direct tirs failed, %d\n", err);
3437 goto err_destroy_indirect_tirs;
3440 err = mlx5e_create_flow_steering(priv);
3442 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
3443 goto err_destroy_direct_tirs;
3446 err = mlx5e_tc_init(priv);
3448 goto err_destroy_flow_steering;
3452 err_destroy_flow_steering:
3453 mlx5e_destroy_flow_steering(priv);
3454 err_destroy_direct_tirs:
3455 mlx5e_destroy_direct_tirs(priv);
3456 err_destroy_indirect_tirs:
3457 mlx5e_destroy_indirect_tirs(priv);
3458 err_destroy_direct_rqts:
3459 for (i = 0; i < priv->profile->max_nch(mdev); i++)
3460 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
3461 err_destroy_indirect_rqts:
3462 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
3466 static void mlx5e_cleanup_nic_rx(struct mlx5e_priv *priv)
3470 mlx5e_tc_cleanup(priv);
3471 mlx5e_destroy_flow_steering(priv);
3472 mlx5e_destroy_direct_tirs(priv);
3473 mlx5e_destroy_indirect_tirs(priv);
3474 for (i = 0; i < priv->profile->max_nch(priv->mdev); i++)
3475 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
3476 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
3479 static int mlx5e_init_nic_tx(struct mlx5e_priv *priv)
3483 err = mlx5e_create_tises(priv);
3485 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
3489 #ifdef CONFIG_MLX5_CORE_EN_DCB
3490 mlx5e_dcbnl_ieee_setets_core(priv, &priv->params.ets);
3495 static void mlx5e_nic_enable(struct mlx5e_priv *priv)
3497 struct net_device *netdev = priv->netdev;
3498 struct mlx5_core_dev *mdev = priv->mdev;
3499 struct mlx5_eswitch *esw = mdev->priv.eswitch;
3500 struct mlx5_eswitch_rep rep;
3502 mlx5_lag_add(mdev, netdev);
3504 if (mlx5e_vxlan_allowed(mdev)) {
3506 udp_tunnel_get_rx_info(netdev);
3510 mlx5e_enable_async_events(priv);
3511 queue_work(priv->wq, &priv->set_rx_mode_work);
3513 if (MLX5_CAP_GEN(mdev, vport_group_manager)) {
3514 mlx5_query_nic_vport_mac_address(mdev, 0, rep.hw_id);
3515 rep.load = mlx5e_nic_rep_load;
3516 rep.unload = mlx5e_nic_rep_unload;
3518 rep.priv_data = priv;
3519 mlx5_eswitch_register_vport_rep(esw, &rep);
3523 static void mlx5e_nic_disable(struct mlx5e_priv *priv)
3525 queue_work(priv->wq, &priv->set_rx_mode_work);
3526 mlx5e_disable_async_events(priv);
3527 mlx5_lag_remove(priv->mdev);
3530 static const struct mlx5e_profile mlx5e_nic_profile = {
3531 .init = mlx5e_nic_init,
3532 .cleanup = mlx5e_nic_cleanup,
3533 .init_rx = mlx5e_init_nic_rx,
3534 .cleanup_rx = mlx5e_cleanup_nic_rx,
3535 .init_tx = mlx5e_init_nic_tx,
3536 .cleanup_tx = mlx5e_cleanup_nic_tx,
3537 .enable = mlx5e_nic_enable,
3538 .disable = mlx5e_nic_disable,
3539 .update_stats = mlx5e_update_stats,
3540 .max_nch = mlx5e_get_max_num_channels,
3541 .max_tc = MLX5E_MAX_NUM_TC,
3544 struct net_device *mlx5e_create_netdev(struct mlx5_core_dev *mdev,
3545 const struct mlx5e_profile *profile,
3548 int nch = profile->max_nch(mdev);
3549 struct net_device *netdev;
3550 struct mlx5e_priv *priv;
3552 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
3553 nch * profile->max_tc,
3556 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
3560 profile->init(mdev, netdev, profile, ppriv);
3562 netif_carrier_off(netdev);
3564 priv = netdev_priv(netdev);
3566 priv->wq = create_singlethread_workqueue("mlx5e");
3568 goto err_cleanup_nic;
3573 profile->cleanup(priv);
3574 free_netdev(netdev);
3579 int mlx5e_attach_netdev(struct mlx5_core_dev *mdev, struct net_device *netdev)
3581 const struct mlx5e_profile *profile;
3582 struct mlx5e_priv *priv;
3585 priv = netdev_priv(netdev);
3586 profile = priv->profile;
3587 clear_bit(MLX5E_STATE_DESTROYING, &priv->state);
3589 err = mlx5e_create_umr_mkey(priv);
3591 mlx5_core_err(mdev, "create umr mkey failed, %d\n", err);
3595 err = profile->init_tx(priv);
3597 goto err_destroy_umr_mkey;
3599 err = mlx5e_open_drop_rq(priv);
3601 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
3602 goto err_cleanup_tx;
3605 err = profile->init_rx(priv);
3607 goto err_close_drop_rq;
3609 mlx5e_create_q_counter(priv);
3611 mlx5e_init_l2_addr(priv);
3613 mlx5e_set_dev_port_mtu(netdev);
3615 if (profile->enable)
3616 profile->enable(priv);
3619 if (netif_running(netdev))
3621 netif_device_attach(netdev);
3627 mlx5e_close_drop_rq(priv);
3630 profile->cleanup_tx(priv);
3632 err_destroy_umr_mkey:
3633 mlx5_core_destroy_mkey(mdev, &priv->umr_mkey);
3639 static void mlx5e_register_vport_rep(struct mlx5_core_dev *mdev)
3641 struct mlx5_eswitch *esw = mdev->priv.eswitch;
3642 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
3646 if (!MLX5_CAP_GEN(mdev, vport_group_manager))
3649 mlx5_query_nic_vport_mac_address(mdev, 0, mac);
3651 for (vport = 1; vport < total_vfs; vport++) {
3652 struct mlx5_eswitch_rep rep;
3654 rep.load = mlx5e_vport_rep_load;
3655 rep.unload = mlx5e_vport_rep_unload;
3657 ether_addr_copy(rep.hw_id, mac);
3658 mlx5_eswitch_register_vport_rep(esw, &rep);
3662 void mlx5e_detach_netdev(struct mlx5_core_dev *mdev, struct net_device *netdev)
3664 struct mlx5e_priv *priv = netdev_priv(netdev);
3665 const struct mlx5e_profile *profile = priv->profile;
3667 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
3668 if (profile->disable)
3669 profile->disable(priv);
3671 flush_workqueue(priv->wq);
3674 if (netif_running(netdev))
3675 mlx5e_close(netdev);
3676 netif_device_detach(netdev);
3679 mlx5e_destroy_q_counter(priv);
3680 profile->cleanup_rx(priv);
3681 mlx5e_close_drop_rq(priv);
3682 profile->cleanup_tx(priv);
3683 mlx5_core_destroy_mkey(priv->mdev, &priv->umr_mkey);
3684 cancel_delayed_work_sync(&priv->update_stats_work);
3687 /* mlx5e_attach and mlx5e_detach scope should be only creating/destroying
3688 * hardware contexts and to connect it to the current netdev.
3690 static int mlx5e_attach(struct mlx5_core_dev *mdev, void *vpriv)
3692 struct mlx5e_priv *priv = vpriv;
3693 struct net_device *netdev = priv->netdev;
3696 if (netif_device_present(netdev))
3699 err = mlx5e_create_mdev_resources(mdev);
3703 err = mlx5e_attach_netdev(mdev, netdev);
3705 mlx5e_destroy_mdev_resources(mdev);
3712 static void mlx5e_detach(struct mlx5_core_dev *mdev, void *vpriv)
3714 struct mlx5e_priv *priv = vpriv;
3715 struct net_device *netdev = priv->netdev;
3717 if (!netif_device_present(netdev))
3720 mlx5e_detach_netdev(mdev, netdev);
3721 mlx5e_destroy_mdev_resources(mdev);
3724 static void *mlx5e_add(struct mlx5_core_dev *mdev)
3726 struct mlx5_eswitch *esw = mdev->priv.eswitch;
3727 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
3732 struct net_device *netdev;
3734 err = mlx5e_check_required_hca_cap(mdev);
3738 mlx5e_register_vport_rep(mdev);
3740 if (MLX5_CAP_GEN(mdev, vport_group_manager))
3741 ppriv = &esw->offloads.vport_reps[0];
3743 netdev = mlx5e_create_netdev(mdev, &mlx5e_nic_profile, ppriv);
3745 mlx5_core_err(mdev, "mlx5e_create_netdev failed\n");
3746 goto err_unregister_reps;
3749 priv = netdev_priv(netdev);
3751 err = mlx5e_attach(mdev, priv);
3753 mlx5_core_err(mdev, "mlx5e_attach failed, %d\n", err);
3754 goto err_destroy_netdev;
3757 err = register_netdev(netdev);
3759 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
3766 mlx5e_detach(mdev, priv);
3769 mlx5e_destroy_netdev(mdev, priv);
3771 err_unregister_reps:
3772 for (vport = 1; vport < total_vfs; vport++)
3773 mlx5_eswitch_unregister_vport_rep(esw, vport);
3778 void mlx5e_destroy_netdev(struct mlx5_core_dev *mdev, struct mlx5e_priv *priv)
3780 const struct mlx5e_profile *profile = priv->profile;
3781 struct net_device *netdev = priv->netdev;
3783 unregister_netdev(netdev);
3784 destroy_workqueue(priv->wq);
3785 if (profile->cleanup)
3786 profile->cleanup(priv);
3787 free_netdev(netdev);
3790 static void mlx5e_remove(struct mlx5_core_dev *mdev, void *vpriv)
3792 struct mlx5_eswitch *esw = mdev->priv.eswitch;
3793 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
3794 struct mlx5e_priv *priv = vpriv;
3797 for (vport = 1; vport < total_vfs; vport++)
3798 mlx5_eswitch_unregister_vport_rep(esw, vport);
3800 mlx5e_detach(mdev, vpriv);
3801 mlx5e_destroy_netdev(mdev, priv);
3804 static void *mlx5e_get_netdev(void *vpriv)
3806 struct mlx5e_priv *priv = vpriv;
3808 return priv->netdev;
3811 static struct mlx5_interface mlx5e_interface = {
3813 .remove = mlx5e_remove,
3814 .attach = mlx5e_attach,
3815 .detach = mlx5e_detach,
3816 .event = mlx5e_async_event,
3817 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
3818 .get_dev = mlx5e_get_netdev,
3821 void mlx5e_init(void)
3823 mlx5e_build_ptys2ethtool_map();
3824 mlx5_register_interface(&mlx5e_interface);
3827 void mlx5e_cleanup(void)
3829 mlx5_unregister_interface(&mlx5e_interface);