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 void mlx5e_update_carrier(struct mlx5e_priv *priv)
74 struct mlx5_core_dev *mdev = priv->mdev;
77 port_state = mlx5_query_vport_state(mdev,
78 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT, 0);
80 if (port_state == VPORT_STATE_UP) {
81 netdev_info(priv->netdev, "Link up\n");
82 netif_carrier_on(priv->netdev);
84 netdev_info(priv->netdev, "Link down\n");
85 netif_carrier_off(priv->netdev);
89 static void mlx5e_update_carrier_work(struct work_struct *work)
91 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
94 mutex_lock(&priv->state_lock);
95 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
96 mlx5e_update_carrier(priv);
97 mutex_unlock(&priv->state_lock);
100 static void mlx5e_tx_timeout_work(struct work_struct *work)
102 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
107 mutex_lock(&priv->state_lock);
108 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
110 mlx5e_close_locked(priv->netdev);
111 err = mlx5e_open_locked(priv->netdev);
113 netdev_err(priv->netdev, "mlx5e_open_locked failed recovering from a tx_timeout, err(%d).\n",
116 mutex_unlock(&priv->state_lock);
120 static void mlx5e_update_sw_counters(struct mlx5e_priv *priv)
122 struct mlx5e_sw_stats *s = &priv->stats.sw;
123 struct mlx5e_rq_stats *rq_stats;
124 struct mlx5e_sq_stats *sq_stats;
125 u64 tx_offload_none = 0;
128 memset(s, 0, sizeof(*s));
129 for (i = 0; i < priv->params.num_channels; i++) {
130 rq_stats = &priv->channel[i]->rq.stats;
132 s->rx_packets += rq_stats->packets;
133 s->rx_bytes += rq_stats->bytes;
134 s->rx_lro_packets += rq_stats->lro_packets;
135 s->rx_lro_bytes += rq_stats->lro_bytes;
136 s->rx_csum_none += rq_stats->csum_none;
137 s->rx_csum_complete += rq_stats->csum_complete;
138 s->rx_csum_unnecessary_inner += rq_stats->csum_unnecessary_inner;
139 s->rx_wqe_err += rq_stats->wqe_err;
140 s->rx_mpwqe_filler += rq_stats->mpwqe_filler;
141 s->rx_buff_alloc_err += rq_stats->buff_alloc_err;
142 s->rx_cqe_compress_blks += rq_stats->cqe_compress_blks;
143 s->rx_cqe_compress_pkts += rq_stats->cqe_compress_pkts;
144 s->rx_cache_reuse += rq_stats->cache_reuse;
145 s->rx_cache_full += rq_stats->cache_full;
146 s->rx_cache_empty += rq_stats->cache_empty;
147 s->rx_cache_busy += rq_stats->cache_busy;
149 for (j = 0; j < priv->params.num_tc; j++) {
150 sq_stats = &priv->channel[i]->sq[j].stats;
152 s->tx_packets += sq_stats->packets;
153 s->tx_bytes += sq_stats->bytes;
154 s->tx_tso_packets += sq_stats->tso_packets;
155 s->tx_tso_bytes += sq_stats->tso_bytes;
156 s->tx_tso_inner_packets += sq_stats->tso_inner_packets;
157 s->tx_tso_inner_bytes += sq_stats->tso_inner_bytes;
158 s->tx_queue_stopped += sq_stats->stopped;
159 s->tx_queue_wake += sq_stats->wake;
160 s->tx_queue_dropped += sq_stats->dropped;
161 s->tx_xmit_more += sq_stats->xmit_more;
162 s->tx_csum_partial_inner += sq_stats->csum_partial_inner;
163 tx_offload_none += sq_stats->csum_none;
167 /* Update calculated offload counters */
168 s->tx_csum_partial = s->tx_packets - tx_offload_none - s->tx_csum_partial_inner;
169 s->rx_csum_unnecessary = s->rx_packets - s->rx_csum_none - s->rx_csum_complete;
171 s->link_down_events_phy = MLX5_GET(ppcnt_reg,
172 priv->stats.pport.phy_counters,
173 counter_set.phys_layer_cntrs.link_down_events);
176 static void mlx5e_update_vport_counters(struct mlx5e_priv *priv)
178 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
179 u32 *out = (u32 *)priv->stats.vport.query_vport_out;
180 u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)] = {0};
181 struct mlx5_core_dev *mdev = priv->mdev;
183 MLX5_SET(query_vport_counter_in, in, opcode,
184 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
185 MLX5_SET(query_vport_counter_in, in, op_mod, 0);
186 MLX5_SET(query_vport_counter_in, in, other_vport, 0);
188 memset(out, 0, outlen);
189 mlx5_cmd_exec(mdev, in, sizeof(in), out, outlen);
192 static void mlx5e_update_pport_counters(struct mlx5e_priv *priv)
194 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
195 struct mlx5_core_dev *mdev = priv->mdev;
196 int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
201 in = mlx5_vzalloc(sz);
205 MLX5_SET(ppcnt_reg, in, local_port, 1);
207 out = pstats->IEEE_802_3_counters;
208 MLX5_SET(ppcnt_reg, in, grp, MLX5_IEEE_802_3_COUNTERS_GROUP);
209 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
211 out = pstats->RFC_2863_counters;
212 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2863_COUNTERS_GROUP);
213 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
215 out = pstats->RFC_2819_counters;
216 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2819_COUNTERS_GROUP);
217 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
219 out = pstats->phy_counters;
220 MLX5_SET(ppcnt_reg, in, grp, MLX5_PHYSICAL_LAYER_COUNTERS_GROUP);
221 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
223 MLX5_SET(ppcnt_reg, in, grp, MLX5_PER_PRIORITY_COUNTERS_GROUP);
224 for (prio = 0; prio < NUM_PPORT_PRIO; prio++) {
225 out = pstats->per_prio_counters[prio];
226 MLX5_SET(ppcnt_reg, in, prio_tc, prio);
227 mlx5_core_access_reg(mdev, in, sz, out, sz,
228 MLX5_REG_PPCNT, 0, 0);
235 static void mlx5e_update_q_counter(struct mlx5e_priv *priv)
237 struct mlx5e_qcounter_stats *qcnt = &priv->stats.qcnt;
239 if (!priv->q_counter)
242 mlx5_core_query_out_of_buffer(priv->mdev, priv->q_counter,
243 &qcnt->rx_out_of_buffer);
246 void mlx5e_update_stats(struct mlx5e_priv *priv)
248 mlx5e_update_q_counter(priv);
249 mlx5e_update_vport_counters(priv);
250 mlx5e_update_pport_counters(priv);
251 mlx5e_update_sw_counters(priv);
254 void mlx5e_update_stats_work(struct work_struct *work)
256 struct delayed_work *dwork = to_delayed_work(work);
257 struct mlx5e_priv *priv = container_of(dwork, struct mlx5e_priv,
259 mutex_lock(&priv->state_lock);
260 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
261 priv->profile->update_stats(priv);
262 queue_delayed_work(priv->wq, dwork,
263 msecs_to_jiffies(MLX5E_UPDATE_STATS_INTERVAL));
265 mutex_unlock(&priv->state_lock);
268 static void mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
269 enum mlx5_dev_event event, unsigned long param)
271 struct mlx5e_priv *priv = vpriv;
273 if (!test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state))
277 case MLX5_DEV_EVENT_PORT_UP:
278 case MLX5_DEV_EVENT_PORT_DOWN:
279 queue_work(priv->wq, &priv->update_carrier_work);
287 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
289 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
292 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
294 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
295 synchronize_irq(mlx5_get_msix_vec(priv->mdev, MLX5_EQ_VEC_ASYNC));
298 #define MLX5E_HW2SW_MTU(hwmtu) (hwmtu - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
299 #define MLX5E_SW2HW_MTU(swmtu) (swmtu + (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
301 static inline int mlx5e_get_wqe_mtt_sz(void)
303 /* UMR copies MTTs in units of MLX5_UMR_MTT_ALIGNMENT bytes.
304 * To avoid copying garbage after the mtt array, we allocate
307 return ALIGN(MLX5_MPWRQ_PAGES_PER_WQE * sizeof(__be64),
308 MLX5_UMR_MTT_ALIGNMENT);
311 static inline void mlx5e_build_umr_wqe(struct mlx5e_rq *rq, struct mlx5e_sq *sq,
312 struct mlx5e_umr_wqe *wqe, u16 ix)
314 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
315 struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->uctrl;
316 struct mlx5_wqe_data_seg *dseg = &wqe->data;
317 struct mlx5e_mpw_info *wi = &rq->mpwqe.info[ix];
318 u8 ds_cnt = DIV_ROUND_UP(sizeof(*wqe), MLX5_SEND_WQE_DS);
319 u32 umr_wqe_mtt_offset = mlx5e_get_wqe_mtt_offset(rq, ix);
321 cseg->qpn_ds = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
323 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
324 cseg->imm = rq->mkey_be;
326 ucseg->flags = MLX5_UMR_TRANSLATION_OFFSET_EN;
327 ucseg->klm_octowords =
328 cpu_to_be16(MLX5_MTT_OCTW(MLX5_MPWRQ_PAGES_PER_WQE));
329 ucseg->bsf_octowords =
330 cpu_to_be16(MLX5_MTT_OCTW(umr_wqe_mtt_offset));
331 ucseg->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
333 dseg->lkey = sq->mkey_be;
334 dseg->addr = cpu_to_be64(wi->umr.mtt_addr);
337 static int mlx5e_rq_alloc_mpwqe_info(struct mlx5e_rq *rq,
338 struct mlx5e_channel *c)
340 int wq_sz = mlx5_wq_ll_get_size(&rq->wq);
341 int mtt_sz = mlx5e_get_wqe_mtt_sz();
342 int mtt_alloc = mtt_sz + MLX5_UMR_ALIGN - 1;
345 rq->mpwqe.info = kzalloc_node(wq_sz * sizeof(*rq->mpwqe.info),
346 GFP_KERNEL, cpu_to_node(c->cpu));
350 /* We allocate more than mtt_sz as we will align the pointer */
351 rq->mpwqe.mtt_no_align = kzalloc_node(mtt_alloc * wq_sz, GFP_KERNEL,
352 cpu_to_node(c->cpu));
353 if (unlikely(!rq->mpwqe.mtt_no_align))
354 goto err_free_wqe_info;
356 for (i = 0; i < wq_sz; i++) {
357 struct mlx5e_mpw_info *wi = &rq->mpwqe.info[i];
359 wi->umr.mtt = PTR_ALIGN(rq->mpwqe.mtt_no_align + i * mtt_alloc,
361 wi->umr.mtt_addr = dma_map_single(c->pdev, wi->umr.mtt, mtt_sz,
363 if (unlikely(dma_mapping_error(c->pdev, wi->umr.mtt_addr)))
366 mlx5e_build_umr_wqe(rq, &c->icosq, &wi->umr.wqe, i);
373 struct mlx5e_mpw_info *wi = &rq->mpwqe.info[i];
375 dma_unmap_single(c->pdev, wi->umr.mtt_addr, mtt_sz,
378 kfree(rq->mpwqe.mtt_no_align);
380 kfree(rq->mpwqe.info);
386 static void mlx5e_rq_free_mpwqe_info(struct mlx5e_rq *rq)
388 int wq_sz = mlx5_wq_ll_get_size(&rq->wq);
389 int mtt_sz = mlx5e_get_wqe_mtt_sz();
392 for (i = 0; i < wq_sz; i++) {
393 struct mlx5e_mpw_info *wi = &rq->mpwqe.info[i];
395 dma_unmap_single(rq->pdev, wi->umr.mtt_addr, mtt_sz,
398 kfree(rq->mpwqe.mtt_no_align);
399 kfree(rq->mpwqe.info);
402 static int mlx5e_create_rq(struct mlx5e_channel *c,
403 struct mlx5e_rq_param *param,
406 struct mlx5e_priv *priv = c->priv;
407 struct mlx5_core_dev *mdev = priv->mdev;
408 void *rqc = param->rqc;
409 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
417 param->wq.db_numa_node = cpu_to_node(c->cpu);
419 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
424 rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
426 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
428 rq->wq_type = priv->params.rq_wq_type;
430 rq->netdev = c->netdev;
431 rq->tstamp = &priv->tstamp;
436 switch (priv->params.rq_wq_type) {
437 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
438 rq->handle_rx_cqe = mlx5e_handle_rx_cqe_mpwrq;
439 rq->alloc_wqe = mlx5e_alloc_rx_mpwqe;
440 rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
442 rq->mpwqe.mtt_offset = c->ix *
443 MLX5E_REQUIRED_MTTS(1, BIT(priv->params.log_rq_size));
445 rq->mpwqe_stride_sz = BIT(priv->params.mpwqe_log_stride_sz);
446 rq->mpwqe_num_strides = BIT(priv->params.mpwqe_log_num_strides);
448 rq->buff.wqe_sz = rq->mpwqe_stride_sz * rq->mpwqe_num_strides;
449 byte_count = rq->buff.wqe_sz;
450 rq->mkey_be = cpu_to_be32(c->priv->umr_mkey.key);
451 err = mlx5e_rq_alloc_mpwqe_info(rq, c);
453 goto err_rq_wq_destroy;
455 default: /* MLX5_WQ_TYPE_LINKED_LIST */
456 rq->dma_info = kzalloc_node(wq_sz * sizeof(*rq->dma_info),
457 GFP_KERNEL, cpu_to_node(c->cpu));
460 goto err_rq_wq_destroy;
463 rq->handle_rx_cqe = mlx5e_handle_rx_cqe;
464 rq->alloc_wqe = mlx5e_alloc_rx_wqe;
465 rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
467 rq->buff.wqe_sz = (priv->params.lro_en) ?
468 priv->params.lro_wqe_sz :
469 MLX5E_SW2HW_MTU(priv->netdev->mtu);
470 byte_count = rq->buff.wqe_sz;
472 /* calc the required page order */
473 frag_sz = MLX5_RX_HEADROOM +
474 byte_count /* packet data */ +
475 SKB_DATA_ALIGN(sizeof(struct skb_shared_info));
476 frag_sz = SKB_DATA_ALIGN(frag_sz);
478 npages = DIV_ROUND_UP(frag_sz, PAGE_SIZE);
479 rq->buff.page_order = order_base_2(npages);
481 byte_count |= MLX5_HW_START_PADDING;
482 rq->mkey_be = c->mkey_be;
485 for (i = 0; i < wq_sz; i++) {
486 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
488 wqe->data.byte_count = cpu_to_be32(byte_count);
489 wqe->data.lkey = rq->mkey_be;
492 INIT_WORK(&rq->am.work, mlx5e_rx_am_work);
493 rq->am.mode = priv->params.rx_cq_period_mode;
495 rq->page_cache.head = 0;
496 rq->page_cache.tail = 0;
501 mlx5_wq_destroy(&rq->wq_ctrl);
506 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
510 switch (rq->wq_type) {
511 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
512 mlx5e_rq_free_mpwqe_info(rq);
514 default: /* MLX5_WQ_TYPE_LINKED_LIST */
518 for (i = rq->page_cache.head; i != rq->page_cache.tail;
519 i = (i + 1) & (MLX5E_CACHE_SIZE - 1)) {
520 struct mlx5e_dma_info *dma_info = &rq->page_cache.page_cache[i];
522 mlx5e_page_release(rq, dma_info, false);
524 mlx5_wq_destroy(&rq->wq_ctrl);
527 static int mlx5e_enable_rq(struct mlx5e_rq *rq, struct mlx5e_rq_param *param)
529 struct mlx5e_priv *priv = rq->priv;
530 struct mlx5_core_dev *mdev = priv->mdev;
538 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
539 sizeof(u64) * rq->wq_ctrl.buf.npages;
540 in = mlx5_vzalloc(inlen);
544 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
545 wq = MLX5_ADDR_OF(rqc, rqc, wq);
547 memcpy(rqc, param->rqc, sizeof(param->rqc));
549 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
550 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
551 MLX5_SET(rqc, rqc, vsd, priv->params.vlan_strip_disable);
552 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
553 MLX5_ADAPTER_PAGE_SHIFT);
554 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
556 mlx5_fill_page_array(&rq->wq_ctrl.buf,
557 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
559 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
566 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
569 struct mlx5e_channel *c = rq->channel;
570 struct mlx5e_priv *priv = c->priv;
571 struct mlx5_core_dev *mdev = priv->mdev;
578 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
579 in = mlx5_vzalloc(inlen);
583 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
585 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
586 MLX5_SET(rqc, rqc, state, next_state);
588 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
595 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
597 struct mlx5e_channel *c = rq->channel;
598 struct mlx5e_priv *priv = c->priv;
599 struct mlx5_core_dev *mdev = priv->mdev;
606 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
607 in = mlx5_vzalloc(inlen);
611 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
613 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
614 MLX5_SET64(modify_rq_in, in, modify_bitmask,
615 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
616 MLX5_SET(rqc, rqc, vsd, vsd);
617 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
619 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
626 static void mlx5e_disable_rq(struct mlx5e_rq *rq)
628 mlx5_core_destroy_rq(rq->priv->mdev, rq->rqn);
631 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq)
633 unsigned long exp_time = jiffies + msecs_to_jiffies(20000);
634 struct mlx5e_channel *c = rq->channel;
635 struct mlx5e_priv *priv = c->priv;
636 struct mlx5_wq_ll *wq = &rq->wq;
638 while (time_before(jiffies, exp_time)) {
639 if (wq->cur_sz >= priv->params.min_rx_wqes)
648 static void mlx5e_free_rx_descs(struct mlx5e_rq *rq)
650 struct mlx5_wq_ll *wq = &rq->wq;
651 struct mlx5e_rx_wqe *wqe;
655 /* UMR WQE (if in progress) is always at wq->head */
656 if (test_bit(MLX5E_RQ_STATE_UMR_WQE_IN_PROGRESS, &rq->state))
657 mlx5e_free_rx_mpwqe(rq, &rq->mpwqe.info[wq->head]);
659 while (!mlx5_wq_ll_is_empty(wq)) {
660 wqe_ix_be = *wq->tail_next;
661 wqe_ix = be16_to_cpu(wqe_ix_be);
662 wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_ix);
663 rq->dealloc_wqe(rq, wqe_ix);
664 mlx5_wq_ll_pop(&rq->wq, wqe_ix_be,
665 &wqe->next.next_wqe_index);
669 static int mlx5e_open_rq(struct mlx5e_channel *c,
670 struct mlx5e_rq_param *param,
673 struct mlx5e_sq *sq = &c->icosq;
674 u16 pi = sq->pc & sq->wq.sz_m1;
677 err = mlx5e_create_rq(c, param, rq);
681 err = mlx5e_enable_rq(rq, param);
685 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
689 if (param->am_enabled)
690 set_bit(MLX5E_RQ_STATE_AM, &c->rq.state);
692 sq->ico_wqe_info[pi].opcode = MLX5_OPCODE_NOP;
693 sq->ico_wqe_info[pi].num_wqebbs = 1;
694 mlx5e_send_nop(sq, true); /* trigger mlx5e_post_rx_wqes() */
699 mlx5e_disable_rq(rq);
701 mlx5e_destroy_rq(rq);
706 static void mlx5e_close_rq(struct mlx5e_rq *rq)
708 set_bit(MLX5E_RQ_STATE_FLUSH, &rq->state);
709 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
710 cancel_work_sync(&rq->am.work);
712 mlx5e_disable_rq(rq);
713 mlx5e_free_rx_descs(rq);
714 mlx5e_destroy_rq(rq);
717 static void mlx5e_free_sq_db(struct mlx5e_sq *sq)
724 static int mlx5e_alloc_sq_db(struct mlx5e_sq *sq, int numa)
726 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
727 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
729 sq->skb = kzalloc_node(wq_sz * sizeof(*sq->skb), GFP_KERNEL, numa);
730 sq->dma_fifo = kzalloc_node(df_sz * sizeof(*sq->dma_fifo), GFP_KERNEL,
732 sq->wqe_info = kzalloc_node(wq_sz * sizeof(*sq->wqe_info), GFP_KERNEL,
735 if (!sq->skb || !sq->dma_fifo || !sq->wqe_info) {
736 mlx5e_free_sq_db(sq);
740 sq->dma_fifo_mask = df_sz - 1;
745 static int mlx5e_create_sq(struct mlx5e_channel *c,
747 struct mlx5e_sq_param *param,
750 struct mlx5e_priv *priv = c->priv;
751 struct mlx5_core_dev *mdev = priv->mdev;
753 void *sqc = param->sqc;
754 void *sqc_wq = MLX5_ADDR_OF(sqc, sqc, wq);
757 err = mlx5_alloc_map_uar(mdev, &sq->uar, !!MLX5_CAP_GEN(mdev, bf));
761 param->wq.db_numa_node = cpu_to_node(c->cpu);
763 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq,
766 goto err_unmap_free_uar;
768 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
769 if (sq->uar.bf_map) {
770 set_bit(MLX5E_SQ_STATE_BF_ENABLE, &sq->state);
771 sq->uar_map = sq->uar.bf_map;
773 sq->uar_map = sq->uar.map;
775 sq->bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
776 sq->max_inline = param->max_inline;
777 sq->min_inline_mode =
778 MLX5_CAP_ETH(mdev, wqe_inline_mode) == MLX5E_INLINE_MODE_VPORT_CONTEXT ?
779 param->min_inline_mode : 0;
781 err = mlx5e_alloc_sq_db(sq, cpu_to_node(c->cpu));
783 goto err_sq_wq_destroy;
786 u8 wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
788 sq->ico_wqe_info = kzalloc_node(sizeof(*sq->ico_wqe_info) *
791 cpu_to_node(c->cpu));
792 if (!sq->ico_wqe_info) {
799 txq_ix = c->ix + tc * priv->params.num_channels;
800 sq->txq = netdev_get_tx_queue(priv->netdev, txq_ix);
801 priv->txq_to_sq_map[txq_ix] = sq;
805 sq->tstamp = &priv->tstamp;
806 sq->mkey_be = c->mkey_be;
809 sq->edge = (sq->wq.sz_m1 + 1) - MLX5_SEND_WQE_MAX_WQEBBS;
810 sq->bf_budget = MLX5E_SQ_BF_BUDGET;
815 mlx5e_free_sq_db(sq);
818 mlx5_wq_destroy(&sq->wq_ctrl);
821 mlx5_unmap_free_uar(mdev, &sq->uar);
826 static void mlx5e_destroy_sq(struct mlx5e_sq *sq)
828 struct mlx5e_channel *c = sq->channel;
829 struct mlx5e_priv *priv = c->priv;
831 kfree(sq->ico_wqe_info);
832 mlx5e_free_sq_db(sq);
833 mlx5_wq_destroy(&sq->wq_ctrl);
834 mlx5_unmap_free_uar(priv->mdev, &sq->uar);
837 static int mlx5e_enable_sq(struct mlx5e_sq *sq, struct mlx5e_sq_param *param)
839 struct mlx5e_channel *c = sq->channel;
840 struct mlx5e_priv *priv = c->priv;
841 struct mlx5_core_dev *mdev = priv->mdev;
849 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
850 sizeof(u64) * sq->wq_ctrl.buf.npages;
851 in = mlx5_vzalloc(inlen);
855 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
856 wq = MLX5_ADDR_OF(sqc, sqc, wq);
858 memcpy(sqc, param->sqc, sizeof(param->sqc));
860 MLX5_SET(sqc, sqc, tis_num_0, param->icosq ? 0 : priv->tisn[sq->tc]);
861 MLX5_SET(sqc, sqc, cqn, sq->cq.mcq.cqn);
862 MLX5_SET(sqc, sqc, min_wqe_inline_mode, sq->min_inline_mode);
863 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
864 MLX5_SET(sqc, sqc, tis_lst_sz, param->icosq ? 0 : 1);
865 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
867 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
868 MLX5_SET(wq, wq, uar_page, sq->uar.index);
869 MLX5_SET(wq, wq, log_wq_pg_sz, sq->wq_ctrl.buf.page_shift -
870 MLX5_ADAPTER_PAGE_SHIFT);
871 MLX5_SET64(wq, wq, dbr_addr, sq->wq_ctrl.db.dma);
873 mlx5_fill_page_array(&sq->wq_ctrl.buf,
874 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
876 err = mlx5_core_create_sq(mdev, in, inlen, &sq->sqn);
883 static int mlx5e_modify_sq(struct mlx5e_sq *sq, int curr_state,
884 int next_state, bool update_rl, int rl_index)
886 struct mlx5e_channel *c = sq->channel;
887 struct mlx5e_priv *priv = c->priv;
888 struct mlx5_core_dev *mdev = priv->mdev;
895 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
896 in = mlx5_vzalloc(inlen);
900 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
902 MLX5_SET(modify_sq_in, in, sq_state, curr_state);
903 MLX5_SET(sqc, sqc, state, next_state);
904 if (update_rl && next_state == MLX5_SQC_STATE_RDY) {
905 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
906 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
909 err = mlx5_core_modify_sq(mdev, sq->sqn, in, inlen);
916 static void mlx5e_disable_sq(struct mlx5e_sq *sq)
918 struct mlx5e_channel *c = sq->channel;
919 struct mlx5e_priv *priv = c->priv;
920 struct mlx5_core_dev *mdev = priv->mdev;
922 mlx5_core_destroy_sq(mdev, sq->sqn);
924 mlx5_rl_remove_rate(mdev, sq->rate_limit);
927 static int mlx5e_open_sq(struct mlx5e_channel *c,
929 struct mlx5e_sq_param *param,
934 err = mlx5e_create_sq(c, tc, param, sq);
938 err = mlx5e_enable_sq(sq, param);
942 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST, MLX5_SQC_STATE_RDY,
948 netdev_tx_reset_queue(sq->txq);
949 netif_tx_start_queue(sq->txq);
955 mlx5e_disable_sq(sq);
957 mlx5e_destroy_sq(sq);
962 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
964 __netif_tx_lock_bh(txq);
965 netif_tx_stop_queue(txq);
966 __netif_tx_unlock_bh(txq);
969 static void mlx5e_close_sq(struct mlx5e_sq *sq)
971 set_bit(MLX5E_SQ_STATE_FLUSH, &sq->state);
972 /* prevent netif_tx_wake_queue */
973 napi_synchronize(&sq->channel->napi);
976 netif_tx_disable_queue(sq->txq);
978 /* last doorbell out, godspeed .. */
979 if (mlx5e_sq_has_room_for(sq, 1))
980 mlx5e_send_nop(sq, true);
983 mlx5e_disable_sq(sq);
984 mlx5e_free_tx_descs(sq);
985 mlx5e_destroy_sq(sq);
988 static int mlx5e_create_cq(struct mlx5e_channel *c,
989 struct mlx5e_cq_param *param,
992 struct mlx5e_priv *priv = c->priv;
993 struct mlx5_core_dev *mdev = priv->mdev;
994 struct mlx5_core_cq *mcq = &cq->mcq;
1000 param->wq.buf_numa_node = cpu_to_node(c->cpu);
1001 param->wq.db_numa_node = cpu_to_node(c->cpu);
1002 param->eq_ix = c->ix;
1004 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1009 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1011 cq->napi = &c->napi;
1014 mcq->set_ci_db = cq->wq_ctrl.db.db;
1015 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1016 *mcq->set_ci_db = 0;
1018 mcq->vector = param->eq_ix;
1019 mcq->comp = mlx5e_completion_event;
1020 mcq->event = mlx5e_cq_error_event;
1022 mcq->uar = &mdev->mlx5e_res.cq_uar;
1024 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
1025 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1036 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
1038 mlx5_wq_destroy(&cq->wq_ctrl);
1041 static int mlx5e_enable_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
1043 struct mlx5e_priv *priv = cq->priv;
1044 struct mlx5_core_dev *mdev = priv->mdev;
1045 struct mlx5_core_cq *mcq = &cq->mcq;
1050 unsigned int irqn_not_used;
1054 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1055 sizeof(u64) * cq->wq_ctrl.buf.npages;
1056 in = mlx5_vzalloc(inlen);
1060 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1062 memcpy(cqc, param->cqc, sizeof(param->cqc));
1064 mlx5_fill_page_array(&cq->wq_ctrl.buf,
1065 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
1067 mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
1069 MLX5_SET(cqc, cqc, cq_period_mode, param->cq_period_mode);
1070 MLX5_SET(cqc, cqc, c_eqn, eqn);
1071 MLX5_SET(cqc, cqc, uar_page, mcq->uar->index);
1072 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
1073 MLX5_ADAPTER_PAGE_SHIFT);
1074 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
1076 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
1088 static void mlx5e_disable_cq(struct mlx5e_cq *cq)
1090 struct mlx5e_priv *priv = cq->priv;
1091 struct mlx5_core_dev *mdev = priv->mdev;
1093 mlx5_core_destroy_cq(mdev, &cq->mcq);
1096 static int mlx5e_open_cq(struct mlx5e_channel *c,
1097 struct mlx5e_cq_param *param,
1098 struct mlx5e_cq *cq,
1099 struct mlx5e_cq_moder moderation)
1102 struct mlx5e_priv *priv = c->priv;
1103 struct mlx5_core_dev *mdev = priv->mdev;
1105 err = mlx5e_create_cq(c, param, cq);
1109 err = mlx5e_enable_cq(cq, param);
1111 goto err_destroy_cq;
1113 if (MLX5_CAP_GEN(mdev, cq_moderation))
1114 mlx5_core_modify_cq_moderation(mdev, &cq->mcq,
1120 mlx5e_destroy_cq(cq);
1125 static void mlx5e_close_cq(struct mlx5e_cq *cq)
1127 mlx5e_disable_cq(cq);
1128 mlx5e_destroy_cq(cq);
1131 static int mlx5e_get_cpu(struct mlx5e_priv *priv, int ix)
1133 return cpumask_first(priv->mdev->priv.irq_info[ix].mask);
1136 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1137 struct mlx5e_channel_param *cparam)
1139 struct mlx5e_priv *priv = c->priv;
1143 for (tc = 0; tc < c->num_tc; tc++) {
1144 err = mlx5e_open_cq(c, &cparam->tx_cq, &c->sq[tc].cq,
1145 priv->params.tx_cq_moderation);
1147 goto err_close_tx_cqs;
1153 for (tc--; tc >= 0; tc--)
1154 mlx5e_close_cq(&c->sq[tc].cq);
1159 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1163 for (tc = 0; tc < c->num_tc; tc++)
1164 mlx5e_close_cq(&c->sq[tc].cq);
1167 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1168 struct mlx5e_channel_param *cparam)
1173 for (tc = 0; tc < c->num_tc; tc++) {
1174 err = mlx5e_open_sq(c, tc, &cparam->sq, &c->sq[tc]);
1182 for (tc--; tc >= 0; tc--)
1183 mlx5e_close_sq(&c->sq[tc]);
1188 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1192 for (tc = 0; tc < c->num_tc; tc++)
1193 mlx5e_close_sq(&c->sq[tc]);
1196 static void mlx5e_build_channeltc_to_txq_map(struct mlx5e_priv *priv, int ix)
1200 for (i = 0; i < priv->profile->max_tc; i++)
1201 priv->channeltc_to_txq_map[ix][i] =
1202 ix + i * priv->params.num_channels;
1205 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1206 struct mlx5e_sq *sq, u32 rate)
1208 struct mlx5e_priv *priv = netdev_priv(dev);
1209 struct mlx5_core_dev *mdev = priv->mdev;
1213 if (rate == sq->rate_limit)
1218 /* remove current rl index to free space to next ones */
1219 mlx5_rl_remove_rate(mdev, sq->rate_limit);
1224 err = mlx5_rl_add_rate(mdev, rate, &rl_index);
1226 netdev_err(dev, "Failed configuring rate %u: %d\n",
1232 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY,
1233 MLX5_SQC_STATE_RDY, true, rl_index);
1235 netdev_err(dev, "Failed configuring rate %u: %d\n",
1237 /* remove the rate from the table */
1239 mlx5_rl_remove_rate(mdev, rate);
1243 sq->rate_limit = rate;
1247 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1249 struct mlx5e_priv *priv = netdev_priv(dev);
1250 struct mlx5_core_dev *mdev = priv->mdev;
1251 struct mlx5e_sq *sq = priv->txq_to_sq_map[index];
1254 if (!mlx5_rl_is_supported(mdev)) {
1255 netdev_err(dev, "Rate limiting is not supported on this device\n");
1259 /* rate is given in Mb/sec, HW config is in Kb/sec */
1262 /* Check whether rate in valid range, 0 is always valid */
1263 if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1264 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1268 mutex_lock(&priv->state_lock);
1269 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1270 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1272 priv->tx_rates[index] = rate;
1273 mutex_unlock(&priv->state_lock);
1278 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1279 struct mlx5e_channel_param *cparam,
1280 struct mlx5e_channel **cp)
1282 struct mlx5e_cq_moder icosq_cq_moder = {0, 0};
1283 struct net_device *netdev = priv->netdev;
1284 struct mlx5e_cq_moder rx_cq_profile;
1285 int cpu = mlx5e_get_cpu(priv, ix);
1286 struct mlx5e_channel *c;
1287 struct mlx5e_sq *sq;
1291 c = kzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1298 c->pdev = &priv->mdev->pdev->dev;
1299 c->netdev = priv->netdev;
1300 c->mkey_be = cpu_to_be32(priv->mdev->mlx5e_res.mkey.key);
1301 c->num_tc = priv->params.num_tc;
1303 if (priv->params.rx_am_enabled)
1304 rx_cq_profile = mlx5e_am_get_def_profile(priv->params.rx_cq_period_mode);
1306 rx_cq_profile = priv->params.rx_cq_moderation;
1308 mlx5e_build_channeltc_to_txq_map(priv, ix);
1310 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1312 err = mlx5e_open_cq(c, &cparam->icosq_cq, &c->icosq.cq, icosq_cq_moder);
1316 err = mlx5e_open_tx_cqs(c, cparam);
1318 goto err_close_icosq_cq;
1320 err = mlx5e_open_cq(c, &cparam->rx_cq, &c->rq.cq,
1323 goto err_close_tx_cqs;
1325 napi_enable(&c->napi);
1327 err = mlx5e_open_sq(c, 0, &cparam->icosq, &c->icosq);
1329 goto err_disable_napi;
1331 err = mlx5e_open_sqs(c, cparam);
1333 goto err_close_icosq;
1335 for (i = 0; i < priv->params.num_tc; i++) {
1336 u32 txq_ix = priv->channeltc_to_txq_map[ix][i];
1338 if (priv->tx_rates[txq_ix]) {
1339 sq = priv->txq_to_sq_map[txq_ix];
1340 mlx5e_set_sq_maxrate(priv->netdev, sq,
1341 priv->tx_rates[txq_ix]);
1345 err = mlx5e_open_rq(c, &cparam->rq, &c->rq);
1349 netif_set_xps_queue(netdev, get_cpu_mask(c->cpu), ix);
1358 mlx5e_close_sq(&c->icosq);
1361 napi_disable(&c->napi);
1362 mlx5e_close_cq(&c->rq.cq);
1365 mlx5e_close_tx_cqs(c);
1368 mlx5e_close_cq(&c->icosq.cq);
1371 netif_napi_del(&c->napi);
1372 napi_hash_del(&c->napi);
1378 static void mlx5e_close_channel(struct mlx5e_channel *c)
1380 mlx5e_close_rq(&c->rq);
1382 mlx5e_close_sq(&c->icosq);
1383 napi_disable(&c->napi);
1384 mlx5e_close_cq(&c->rq.cq);
1385 mlx5e_close_tx_cqs(c);
1386 mlx5e_close_cq(&c->icosq.cq);
1387 netif_napi_del(&c->napi);
1389 napi_hash_del(&c->napi);
1395 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
1396 struct mlx5e_rq_param *param)
1398 void *rqc = param->rqc;
1399 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1401 switch (priv->params.rq_wq_type) {
1402 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1403 MLX5_SET(wq, wq, log_wqe_num_of_strides,
1404 priv->params.mpwqe_log_num_strides - 9);
1405 MLX5_SET(wq, wq, log_wqe_stride_size,
1406 priv->params.mpwqe_log_stride_sz - 6);
1407 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ);
1409 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1410 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1413 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1414 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1415 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_rq_size);
1416 MLX5_SET(wq, wq, pd, priv->mdev->mlx5e_res.pdn);
1417 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
1419 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1420 param->wq.linear = 1;
1422 param->am_enabled = priv->params.rx_am_enabled;
1425 static void mlx5e_build_drop_rq_param(struct mlx5e_rq_param *param)
1427 void *rqc = param->rqc;
1428 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1430 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1431 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1434 static void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
1435 struct mlx5e_sq_param *param)
1437 void *sqc = param->sqc;
1438 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1440 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1441 MLX5_SET(wq, wq, pd, priv->mdev->mlx5e_res.pdn);
1443 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1446 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
1447 struct mlx5e_sq_param *param)
1449 void *sqc = param->sqc;
1450 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1452 mlx5e_build_sq_param_common(priv, param);
1453 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_sq_size);
1455 param->max_inline = priv->params.tx_max_inline;
1456 param->min_inline_mode = priv->params.tx_min_inline_mode;
1459 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1460 struct mlx5e_cq_param *param)
1462 void *cqc = param->cqc;
1464 MLX5_SET(cqc, cqc, uar_page, priv->mdev->mlx5e_res.cq_uar.index);
1467 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
1468 struct mlx5e_cq_param *param)
1470 void *cqc = param->cqc;
1473 switch (priv->params.rq_wq_type) {
1474 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1475 log_cq_size = priv->params.log_rq_size +
1476 priv->params.mpwqe_log_num_strides;
1478 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1479 log_cq_size = priv->params.log_rq_size;
1482 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
1483 if (priv->params.rx_cqe_compress) {
1484 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
1485 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
1488 mlx5e_build_common_cq_param(priv, param);
1490 param->cq_period_mode = priv->params.rx_cq_period_mode;
1493 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
1494 struct mlx5e_cq_param *param)
1496 void *cqc = param->cqc;
1498 MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_sq_size);
1500 mlx5e_build_common_cq_param(priv, param);
1502 param->cq_period_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1505 static void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
1506 struct mlx5e_cq_param *param,
1509 void *cqc = param->cqc;
1511 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
1513 mlx5e_build_common_cq_param(priv, param);
1515 param->cq_period_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1518 static void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
1519 struct mlx5e_sq_param *param,
1522 void *sqc = param->sqc;
1523 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1525 mlx5e_build_sq_param_common(priv, param);
1527 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
1528 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
1530 param->icosq = true;
1533 static void mlx5e_build_channel_param(struct mlx5e_priv *priv, struct mlx5e_channel_param *cparam)
1535 u8 icosq_log_wq_sz = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
1537 mlx5e_build_rq_param(priv, &cparam->rq);
1538 mlx5e_build_sq_param(priv, &cparam->sq);
1539 mlx5e_build_icosq_param(priv, &cparam->icosq, icosq_log_wq_sz);
1540 mlx5e_build_rx_cq_param(priv, &cparam->rx_cq);
1541 mlx5e_build_tx_cq_param(priv, &cparam->tx_cq);
1542 mlx5e_build_ico_cq_param(priv, &cparam->icosq_cq, icosq_log_wq_sz);
1545 static int mlx5e_open_channels(struct mlx5e_priv *priv)
1547 struct mlx5e_channel_param *cparam;
1548 int nch = priv->params.num_channels;
1553 priv->channel = kcalloc(nch, sizeof(struct mlx5e_channel *),
1556 priv->txq_to_sq_map = kcalloc(nch * priv->params.num_tc,
1557 sizeof(struct mlx5e_sq *), GFP_KERNEL);
1559 cparam = kzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
1561 if (!priv->channel || !priv->txq_to_sq_map || !cparam)
1562 goto err_free_txq_to_sq_map;
1564 mlx5e_build_channel_param(priv, cparam);
1566 for (i = 0; i < nch; i++) {
1567 err = mlx5e_open_channel(priv, i, cparam, &priv->channel[i]);
1569 goto err_close_channels;
1572 for (j = 0; j < nch; j++) {
1573 err = mlx5e_wait_for_min_rx_wqes(&priv->channel[j]->rq);
1575 goto err_close_channels;
1578 /* FIXME: This is a W/A for tx timeout watch dog false alarm when
1579 * polling for inactive tx queues.
1581 netif_tx_start_all_queues(priv->netdev);
1587 for (i--; i >= 0; i--)
1588 mlx5e_close_channel(priv->channel[i]);
1590 err_free_txq_to_sq_map:
1591 kfree(priv->txq_to_sq_map);
1592 kfree(priv->channel);
1598 static void mlx5e_close_channels(struct mlx5e_priv *priv)
1602 /* FIXME: This is a W/A only for tx timeout watch dog false alarm when
1603 * polling for inactive tx queues.
1605 netif_tx_stop_all_queues(priv->netdev);
1606 netif_tx_disable(priv->netdev);
1608 for (i = 0; i < priv->params.num_channels; i++)
1609 mlx5e_close_channel(priv->channel[i]);
1611 kfree(priv->txq_to_sq_map);
1612 kfree(priv->channel);
1615 static int mlx5e_rx_hash_fn(int hfunc)
1617 return (hfunc == ETH_RSS_HASH_TOP) ?
1618 MLX5_RX_HASH_FN_TOEPLITZ :
1619 MLX5_RX_HASH_FN_INVERTED_XOR8;
1622 static int mlx5e_bits_invert(unsigned long a, int size)
1627 for (i = 0; i < size; i++)
1628 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
1633 static void mlx5e_fill_indir_rqt_rqns(struct mlx5e_priv *priv, void *rqtc)
1637 for (i = 0; i < MLX5E_INDIR_RQT_SIZE; i++) {
1641 if (priv->params.rss_hfunc == ETH_RSS_HASH_XOR)
1642 ix = mlx5e_bits_invert(i, MLX5E_LOG_INDIR_RQT_SIZE);
1644 ix = priv->params.indirection_rqt[ix];
1645 rqn = test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1646 priv->channel[ix]->rq.rqn :
1648 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
1652 static void mlx5e_fill_direct_rqt_rqn(struct mlx5e_priv *priv, void *rqtc,
1655 u32 rqn = test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1656 priv->channel[ix]->rq.rqn :
1659 MLX5_SET(rqtc, rqtc, rq_num[0], rqn);
1662 static int mlx5e_create_rqt(struct mlx5e_priv *priv, int sz,
1663 int ix, struct mlx5e_rqt *rqt)
1665 struct mlx5_core_dev *mdev = priv->mdev;
1671 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
1672 in = mlx5_vzalloc(inlen);
1676 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
1678 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1679 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
1681 if (sz > 1) /* RSS */
1682 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1684 mlx5e_fill_direct_rqt_rqn(priv, rqtc, ix);
1686 err = mlx5_core_create_rqt(mdev, in, inlen, &rqt->rqtn);
1688 rqt->enabled = true;
1694 void mlx5e_destroy_rqt(struct mlx5e_priv *priv, struct mlx5e_rqt *rqt)
1696 rqt->enabled = false;
1697 mlx5_core_destroy_rqt(priv->mdev, rqt->rqtn);
1700 static int mlx5e_create_indirect_rqts(struct mlx5e_priv *priv)
1702 struct mlx5e_rqt *rqt = &priv->indir_rqt;
1704 return mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, 0, rqt);
1707 int mlx5e_create_direct_rqts(struct mlx5e_priv *priv)
1709 struct mlx5e_rqt *rqt;
1713 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
1714 rqt = &priv->direct_tir[ix].rqt;
1715 err = mlx5e_create_rqt(priv, 1 /*size */, ix, rqt);
1717 goto err_destroy_rqts;
1723 for (ix--; ix >= 0; ix--)
1724 mlx5e_destroy_rqt(priv, &priv->direct_tir[ix].rqt);
1729 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz, int ix)
1731 struct mlx5_core_dev *mdev = priv->mdev;
1737 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
1738 in = mlx5_vzalloc(inlen);
1742 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
1744 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1745 if (sz > 1) /* RSS */
1746 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1748 mlx5e_fill_direct_rqt_rqn(priv, rqtc, ix);
1750 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
1752 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
1759 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv)
1764 if (priv->indir_rqt.enabled) {
1765 rqtn = priv->indir_rqt.rqtn;
1766 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, 0);
1769 for (ix = 0; ix < priv->params.num_channels; ix++) {
1770 if (!priv->direct_tir[ix].rqt.enabled)
1772 rqtn = priv->direct_tir[ix].rqt.rqtn;
1773 mlx5e_redirect_rqt(priv, rqtn, 1, ix);
1777 static void mlx5e_build_tir_ctx_lro(void *tirc, struct mlx5e_priv *priv)
1779 if (!priv->params.lro_en)
1782 #define ROUGH_MAX_L2_L3_HDR_SZ 256
1784 MLX5_SET(tirc, tirc, lro_enable_mask,
1785 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
1786 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
1787 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
1788 (priv->params.lro_wqe_sz -
1789 ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
1790 MLX5_SET(tirc, tirc, lro_timeout_period_usecs,
1791 MLX5_CAP_ETH(priv->mdev,
1792 lro_timer_supported_periods[2]));
1795 void mlx5e_build_tir_ctx_hash(void *tirc, struct mlx5e_priv *priv)
1797 MLX5_SET(tirc, tirc, rx_hash_fn,
1798 mlx5e_rx_hash_fn(priv->params.rss_hfunc));
1799 if (priv->params.rss_hfunc == ETH_RSS_HASH_TOP) {
1800 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
1801 rx_hash_toeplitz_key);
1802 size_t len = MLX5_FLD_SZ_BYTES(tirc,
1803 rx_hash_toeplitz_key);
1805 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1806 memcpy(rss_key, priv->params.toeplitz_hash_key, len);
1810 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
1812 struct mlx5_core_dev *mdev = priv->mdev;
1821 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
1822 in = mlx5_vzalloc(inlen);
1826 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
1827 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
1829 mlx5e_build_tir_ctx_lro(tirc, priv);
1831 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
1832 err = mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in,
1838 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
1839 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
1851 static int mlx5e_set_mtu(struct mlx5e_priv *priv, u16 mtu)
1853 struct mlx5_core_dev *mdev = priv->mdev;
1854 u16 hw_mtu = MLX5E_SW2HW_MTU(mtu);
1857 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
1861 /* Update vport context MTU */
1862 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
1866 static void mlx5e_query_mtu(struct mlx5e_priv *priv, u16 *mtu)
1868 struct mlx5_core_dev *mdev = priv->mdev;
1872 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
1873 if (err || !hw_mtu) /* fallback to port oper mtu */
1874 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
1876 *mtu = MLX5E_HW2SW_MTU(hw_mtu);
1879 static int mlx5e_set_dev_port_mtu(struct net_device *netdev)
1881 struct mlx5e_priv *priv = netdev_priv(netdev);
1885 err = mlx5e_set_mtu(priv, netdev->mtu);
1889 mlx5e_query_mtu(priv, &mtu);
1890 if (mtu != netdev->mtu)
1891 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
1892 __func__, mtu, netdev->mtu);
1898 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
1900 struct mlx5e_priv *priv = netdev_priv(netdev);
1901 int nch = priv->params.num_channels;
1902 int ntc = priv->params.num_tc;
1905 netdev_reset_tc(netdev);
1910 netdev_set_num_tc(netdev, ntc);
1912 /* Map netdev TCs to offset 0
1913 * We have our own UP to TXQ mapping for QoS
1915 for (tc = 0; tc < ntc; tc++)
1916 netdev_set_tc_queue(netdev, tc, nch, 0);
1919 int mlx5e_open_locked(struct net_device *netdev)
1921 struct mlx5e_priv *priv = netdev_priv(netdev);
1922 struct mlx5_core_dev *mdev = priv->mdev;
1926 set_bit(MLX5E_STATE_OPENED, &priv->state);
1928 mlx5e_netdev_set_tcs(netdev);
1930 num_txqs = priv->params.num_channels * priv->params.num_tc;
1931 netif_set_real_num_tx_queues(netdev, num_txqs);
1932 netif_set_real_num_rx_queues(netdev, priv->params.num_channels);
1934 err = mlx5e_open_channels(priv);
1936 netdev_err(netdev, "%s: mlx5e_open_channels failed, %d\n",
1938 goto err_clear_state_opened_flag;
1941 err = mlx5e_refresh_tirs_self_loopback_enable(priv->mdev);
1943 netdev_err(netdev, "%s: mlx5e_refresh_tirs_self_loopback_enable failed, %d\n",
1945 goto err_close_channels;
1948 mlx5e_redirect_rqts(priv);
1949 mlx5e_update_carrier(priv);
1950 mlx5e_timestamp_init(priv);
1951 #ifdef CONFIG_RFS_ACCEL
1952 priv->netdev->rx_cpu_rmap = priv->mdev->rmap;
1954 if (priv->profile->update_stats)
1955 queue_delayed_work(priv->wq, &priv->update_stats_work, 0);
1957 if (MLX5_CAP_GEN(mdev, vport_group_manager)) {
1958 err = mlx5e_add_sqs_fwd_rules(priv);
1960 goto err_close_channels;
1965 mlx5e_close_channels(priv);
1966 err_clear_state_opened_flag:
1967 clear_bit(MLX5E_STATE_OPENED, &priv->state);
1971 int mlx5e_open(struct net_device *netdev)
1973 struct mlx5e_priv *priv = netdev_priv(netdev);
1976 mutex_lock(&priv->state_lock);
1977 err = mlx5e_open_locked(netdev);
1978 mutex_unlock(&priv->state_lock);
1983 int mlx5e_close_locked(struct net_device *netdev)
1985 struct mlx5e_priv *priv = netdev_priv(netdev);
1986 struct mlx5_core_dev *mdev = priv->mdev;
1988 /* May already be CLOSED in case a previous configuration operation
1989 * (e.g RX/TX queue size change) that involves close&open failed.
1991 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
1994 clear_bit(MLX5E_STATE_OPENED, &priv->state);
1996 if (MLX5_CAP_GEN(mdev, vport_group_manager))
1997 mlx5e_remove_sqs_fwd_rules(priv);
1999 mlx5e_timestamp_cleanup(priv);
2000 netif_carrier_off(priv->netdev);
2001 mlx5e_redirect_rqts(priv);
2002 mlx5e_close_channels(priv);
2007 int mlx5e_close(struct net_device *netdev)
2009 struct mlx5e_priv *priv = netdev_priv(netdev);
2012 if (!netif_device_present(netdev))
2015 mutex_lock(&priv->state_lock);
2016 err = mlx5e_close_locked(netdev);
2017 mutex_unlock(&priv->state_lock);
2022 static int mlx5e_create_drop_rq(struct mlx5e_priv *priv,
2023 struct mlx5e_rq *rq,
2024 struct mlx5e_rq_param *param)
2026 struct mlx5_core_dev *mdev = priv->mdev;
2027 void *rqc = param->rqc;
2028 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
2031 param->wq.db_numa_node = param->wq.buf_numa_node;
2033 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
2043 static int mlx5e_create_drop_cq(struct mlx5e_priv *priv,
2044 struct mlx5e_cq *cq,
2045 struct mlx5e_cq_param *param)
2047 struct mlx5_core_dev *mdev = priv->mdev;
2048 struct mlx5_core_cq *mcq = &cq->mcq;
2053 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
2058 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
2061 mcq->set_ci_db = cq->wq_ctrl.db.db;
2062 mcq->arm_db = cq->wq_ctrl.db.db + 1;
2063 *mcq->set_ci_db = 0;
2065 mcq->vector = param->eq_ix;
2066 mcq->comp = mlx5e_completion_event;
2067 mcq->event = mlx5e_cq_error_event;
2069 mcq->uar = &mdev->mlx5e_res.cq_uar;
2076 static int mlx5e_open_drop_rq(struct mlx5e_priv *priv)
2078 struct mlx5e_cq_param cq_param;
2079 struct mlx5e_rq_param rq_param;
2080 struct mlx5e_rq *rq = &priv->drop_rq;
2081 struct mlx5e_cq *cq = &priv->drop_rq.cq;
2084 memset(&cq_param, 0, sizeof(cq_param));
2085 memset(&rq_param, 0, sizeof(rq_param));
2086 mlx5e_build_drop_rq_param(&rq_param);
2088 err = mlx5e_create_drop_cq(priv, cq, &cq_param);
2092 err = mlx5e_enable_cq(cq, &cq_param);
2094 goto err_destroy_cq;
2096 err = mlx5e_create_drop_rq(priv, rq, &rq_param);
2098 goto err_disable_cq;
2100 err = mlx5e_enable_rq(rq, &rq_param);
2102 goto err_destroy_rq;
2107 mlx5e_destroy_rq(&priv->drop_rq);
2110 mlx5e_disable_cq(&priv->drop_rq.cq);
2113 mlx5e_destroy_cq(&priv->drop_rq.cq);
2118 static void mlx5e_close_drop_rq(struct mlx5e_priv *priv)
2120 mlx5e_disable_rq(&priv->drop_rq);
2121 mlx5e_destroy_rq(&priv->drop_rq);
2122 mlx5e_disable_cq(&priv->drop_rq.cq);
2123 mlx5e_destroy_cq(&priv->drop_rq.cq);
2126 static int mlx5e_create_tis(struct mlx5e_priv *priv, int tc)
2128 struct mlx5_core_dev *mdev = priv->mdev;
2129 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
2130 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
2132 MLX5_SET(tisc, tisc, prio, tc << 1);
2133 MLX5_SET(tisc, tisc, transport_domain, mdev->mlx5e_res.td.tdn);
2135 if (mlx5_lag_is_lacp_owner(mdev))
2136 MLX5_SET(tisc, tisc, strict_lag_tx_port_affinity, 1);
2138 return mlx5_core_create_tis(mdev, in, sizeof(in), &priv->tisn[tc]);
2141 static void mlx5e_destroy_tis(struct mlx5e_priv *priv, int tc)
2143 mlx5_core_destroy_tis(priv->mdev, priv->tisn[tc]);
2146 int mlx5e_create_tises(struct mlx5e_priv *priv)
2151 for (tc = 0; tc < priv->profile->max_tc; tc++) {
2152 err = mlx5e_create_tis(priv, tc);
2154 goto err_close_tises;
2160 for (tc--; tc >= 0; tc--)
2161 mlx5e_destroy_tis(priv, tc);
2166 void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv)
2170 for (tc = 0; tc < priv->profile->max_tc; tc++)
2171 mlx5e_destroy_tis(priv, tc);
2174 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv, u32 *tirc,
2175 enum mlx5e_traffic_types tt)
2177 void *hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2179 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2181 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
2182 MLX5_HASH_FIELD_SEL_DST_IP)
2184 #define MLX5_HASH_IP_L4PORTS (MLX5_HASH_FIELD_SEL_SRC_IP |\
2185 MLX5_HASH_FIELD_SEL_DST_IP |\
2186 MLX5_HASH_FIELD_SEL_L4_SPORT |\
2187 MLX5_HASH_FIELD_SEL_L4_DPORT)
2189 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
2190 MLX5_HASH_FIELD_SEL_DST_IP |\
2191 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
2193 mlx5e_build_tir_ctx_lro(tirc, priv);
2195 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2196 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
2197 mlx5e_build_tir_ctx_hash(tirc, priv);
2200 case MLX5E_TT_IPV4_TCP:
2201 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2202 MLX5_L3_PROT_TYPE_IPV4);
2203 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2204 MLX5_L4_PROT_TYPE_TCP);
2205 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2206 MLX5_HASH_IP_L4PORTS);
2209 case MLX5E_TT_IPV6_TCP:
2210 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2211 MLX5_L3_PROT_TYPE_IPV6);
2212 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2213 MLX5_L4_PROT_TYPE_TCP);
2214 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2215 MLX5_HASH_IP_L4PORTS);
2218 case MLX5E_TT_IPV4_UDP:
2219 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2220 MLX5_L3_PROT_TYPE_IPV4);
2221 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2222 MLX5_L4_PROT_TYPE_UDP);
2223 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2224 MLX5_HASH_IP_L4PORTS);
2227 case MLX5E_TT_IPV6_UDP:
2228 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2229 MLX5_L3_PROT_TYPE_IPV6);
2230 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2231 MLX5_L4_PROT_TYPE_UDP);
2232 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2233 MLX5_HASH_IP_L4PORTS);
2236 case MLX5E_TT_IPV4_IPSEC_AH:
2237 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2238 MLX5_L3_PROT_TYPE_IPV4);
2239 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2240 MLX5_HASH_IP_IPSEC_SPI);
2243 case MLX5E_TT_IPV6_IPSEC_AH:
2244 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2245 MLX5_L3_PROT_TYPE_IPV6);
2246 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2247 MLX5_HASH_IP_IPSEC_SPI);
2250 case MLX5E_TT_IPV4_IPSEC_ESP:
2251 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2252 MLX5_L3_PROT_TYPE_IPV4);
2253 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2254 MLX5_HASH_IP_IPSEC_SPI);
2257 case MLX5E_TT_IPV6_IPSEC_ESP:
2258 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2259 MLX5_L3_PROT_TYPE_IPV6);
2260 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2261 MLX5_HASH_IP_IPSEC_SPI);
2265 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2266 MLX5_L3_PROT_TYPE_IPV4);
2267 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2272 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2273 MLX5_L3_PROT_TYPE_IPV6);
2274 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2279 "mlx5e_build_indir_tir_ctx: bad traffic type!\n");
2283 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 *tirc,
2286 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2288 mlx5e_build_tir_ctx_lro(tirc, priv);
2290 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2291 MLX5_SET(tirc, tirc, indirect_table, rqtn);
2292 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
2295 static int mlx5e_create_indirect_tirs(struct mlx5e_priv *priv)
2297 struct mlx5e_tir *tir;
2304 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2305 in = mlx5_vzalloc(inlen);
2309 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2310 memset(in, 0, inlen);
2311 tir = &priv->indir_tir[tt];
2312 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2313 mlx5e_build_indir_tir_ctx(priv, tirc, tt);
2314 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
2316 goto err_destroy_tirs;
2324 for (tt--; tt >= 0; tt--)
2325 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[tt]);
2332 int mlx5e_create_direct_tirs(struct mlx5e_priv *priv)
2334 int nch = priv->profile->max_nch(priv->mdev);
2335 struct mlx5e_tir *tir;
2342 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2343 in = mlx5_vzalloc(inlen);
2347 for (ix = 0; ix < nch; ix++) {
2348 memset(in, 0, inlen);
2349 tir = &priv->direct_tir[ix];
2350 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2351 mlx5e_build_direct_tir_ctx(priv, tirc,
2352 priv->direct_tir[ix].rqt.rqtn);
2353 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
2355 goto err_destroy_ch_tirs;
2362 err_destroy_ch_tirs:
2363 for (ix--; ix >= 0; ix--)
2364 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[ix]);
2371 static void mlx5e_destroy_indirect_tirs(struct mlx5e_priv *priv)
2375 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
2376 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[i]);
2379 void mlx5e_destroy_direct_tirs(struct mlx5e_priv *priv)
2381 int nch = priv->profile->max_nch(priv->mdev);
2384 for (i = 0; i < nch; i++)
2385 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[i]);
2388 int mlx5e_modify_rqs_vsd(struct mlx5e_priv *priv, bool vsd)
2393 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
2396 for (i = 0; i < priv->params.num_channels; i++) {
2397 err = mlx5e_modify_rq_vsd(&priv->channel[i]->rq, vsd);
2405 static int mlx5e_setup_tc(struct net_device *netdev, u8 tc)
2407 struct mlx5e_priv *priv = netdev_priv(netdev);
2411 if (tc && tc != MLX5E_MAX_NUM_TC)
2414 mutex_lock(&priv->state_lock);
2416 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2418 mlx5e_close_locked(priv->netdev);
2420 priv->params.num_tc = tc ? tc : 1;
2423 err = mlx5e_open_locked(priv->netdev);
2425 mutex_unlock(&priv->state_lock);
2430 static int mlx5e_ndo_setup_tc(struct net_device *dev, u32 handle,
2431 __be16 proto, struct tc_to_netdev *tc)
2433 struct mlx5e_priv *priv = netdev_priv(dev);
2435 if (TC_H_MAJ(handle) != TC_H_MAJ(TC_H_INGRESS))
2439 case TC_SETUP_CLSFLOWER:
2440 switch (tc->cls_flower->command) {
2441 case TC_CLSFLOWER_REPLACE:
2442 return mlx5e_configure_flower(priv, proto, tc->cls_flower);
2443 case TC_CLSFLOWER_DESTROY:
2444 return mlx5e_delete_flower(priv, tc->cls_flower);
2445 case TC_CLSFLOWER_STATS:
2446 return mlx5e_stats_flower(priv, tc->cls_flower);
2453 if (tc->type != TC_SETUP_MQPRIO)
2456 return mlx5e_setup_tc(dev, tc->tc);
2459 struct rtnl_link_stats64 *
2460 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
2462 struct mlx5e_priv *priv = netdev_priv(dev);
2463 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
2464 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
2465 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
2467 stats->rx_packets = sstats->rx_packets;
2468 stats->rx_bytes = sstats->rx_bytes;
2469 stats->tx_packets = sstats->tx_packets;
2470 stats->tx_bytes = sstats->tx_bytes;
2472 stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
2473 stats->tx_dropped = sstats->tx_queue_dropped;
2475 stats->rx_length_errors =
2476 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
2477 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
2478 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
2479 stats->rx_crc_errors =
2480 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
2481 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
2482 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
2483 stats->tx_carrier_errors =
2484 PPORT_802_3_GET(pstats, a_symbol_error_during_carrier);
2485 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
2486 stats->rx_frame_errors;
2487 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
2489 /* vport multicast also counts packets that are dropped due to steering
2490 * or rx out of buffer
2493 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
2498 static void mlx5e_set_rx_mode(struct net_device *dev)
2500 struct mlx5e_priv *priv = netdev_priv(dev);
2502 queue_work(priv->wq, &priv->set_rx_mode_work);
2505 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
2507 struct mlx5e_priv *priv = netdev_priv(netdev);
2508 struct sockaddr *saddr = addr;
2510 if (!is_valid_ether_addr(saddr->sa_data))
2511 return -EADDRNOTAVAIL;
2513 netif_addr_lock_bh(netdev);
2514 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
2515 netif_addr_unlock_bh(netdev);
2517 queue_work(priv->wq, &priv->set_rx_mode_work);
2522 #define MLX5E_SET_FEATURE(netdev, feature, enable) \
2525 netdev->features |= feature; \
2527 netdev->features &= ~feature; \
2530 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
2532 static int set_feature_lro(struct net_device *netdev, bool enable)
2534 struct mlx5e_priv *priv = netdev_priv(netdev);
2535 bool was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2538 mutex_lock(&priv->state_lock);
2540 if (was_opened && (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST))
2541 mlx5e_close_locked(priv->netdev);
2543 priv->params.lro_en = enable;
2544 err = mlx5e_modify_tirs_lro(priv);
2546 netdev_err(netdev, "lro modify failed, %d\n", err);
2547 priv->params.lro_en = !enable;
2550 if (was_opened && (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST))
2551 mlx5e_open_locked(priv->netdev);
2553 mutex_unlock(&priv->state_lock);
2558 static int set_feature_vlan_filter(struct net_device *netdev, bool enable)
2560 struct mlx5e_priv *priv = netdev_priv(netdev);
2563 mlx5e_enable_vlan_filter(priv);
2565 mlx5e_disable_vlan_filter(priv);
2570 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
2572 struct mlx5e_priv *priv = netdev_priv(netdev);
2574 if (!enable && mlx5e_tc_num_filters(priv)) {
2576 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
2583 static int set_feature_rx_all(struct net_device *netdev, bool enable)
2585 struct mlx5e_priv *priv = netdev_priv(netdev);
2586 struct mlx5_core_dev *mdev = priv->mdev;
2588 return mlx5_set_port_fcs(mdev, !enable);
2591 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
2593 struct mlx5e_priv *priv = netdev_priv(netdev);
2596 mutex_lock(&priv->state_lock);
2598 priv->params.vlan_strip_disable = !enable;
2599 err = mlx5e_modify_rqs_vsd(priv, !enable);
2601 priv->params.vlan_strip_disable = enable;
2603 mutex_unlock(&priv->state_lock);
2608 #ifdef CONFIG_RFS_ACCEL
2609 static int set_feature_arfs(struct net_device *netdev, bool enable)
2611 struct mlx5e_priv *priv = netdev_priv(netdev);
2615 err = mlx5e_arfs_enable(priv);
2617 err = mlx5e_arfs_disable(priv);
2623 static int mlx5e_handle_feature(struct net_device *netdev,
2624 netdev_features_t wanted_features,
2625 netdev_features_t feature,
2626 mlx5e_feature_handler feature_handler)
2628 netdev_features_t changes = wanted_features ^ netdev->features;
2629 bool enable = !!(wanted_features & feature);
2632 if (!(changes & feature))
2635 err = feature_handler(netdev, enable);
2637 netdev_err(netdev, "%s feature 0x%llx failed err %d\n",
2638 enable ? "Enable" : "Disable", feature, err);
2642 MLX5E_SET_FEATURE(netdev, feature, enable);
2646 static int mlx5e_set_features(struct net_device *netdev,
2647 netdev_features_t features)
2651 err = mlx5e_handle_feature(netdev, features, NETIF_F_LRO,
2653 err |= mlx5e_handle_feature(netdev, features,
2654 NETIF_F_HW_VLAN_CTAG_FILTER,
2655 set_feature_vlan_filter);
2656 err |= mlx5e_handle_feature(netdev, features, NETIF_F_HW_TC,
2657 set_feature_tc_num_filters);
2658 err |= mlx5e_handle_feature(netdev, features, NETIF_F_RXALL,
2659 set_feature_rx_all);
2660 err |= mlx5e_handle_feature(netdev, features, NETIF_F_HW_VLAN_CTAG_RX,
2661 set_feature_rx_vlan);
2662 #ifdef CONFIG_RFS_ACCEL
2663 err |= mlx5e_handle_feature(netdev, features, NETIF_F_NTUPLE,
2667 return err ? -EINVAL : 0;
2670 #define MXL5_HW_MIN_MTU 64
2671 #define MXL5E_MIN_MTU (MXL5_HW_MIN_MTU + ETH_FCS_LEN)
2673 static int mlx5e_change_mtu(struct net_device *netdev, int new_mtu)
2675 struct mlx5e_priv *priv = netdev_priv(netdev);
2676 struct mlx5_core_dev *mdev = priv->mdev;
2683 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
2685 max_mtu = MLX5E_HW2SW_MTU(max_mtu);
2686 min_mtu = MLX5E_HW2SW_MTU(MXL5E_MIN_MTU);
2688 if (new_mtu > max_mtu || new_mtu < min_mtu) {
2690 "%s: Bad MTU (%d), valid range is: [%d..%d]\n",
2691 __func__, new_mtu, min_mtu, max_mtu);
2695 mutex_lock(&priv->state_lock);
2697 reset = !priv->params.lro_en &&
2698 (priv->params.rq_wq_type !=
2699 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ);
2701 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2702 if (was_opened && reset)
2703 mlx5e_close_locked(netdev);
2705 netdev->mtu = new_mtu;
2706 mlx5e_set_dev_port_mtu(netdev);
2708 if (was_opened && reset)
2709 err = mlx5e_open_locked(netdev);
2711 mutex_unlock(&priv->state_lock);
2716 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2720 return mlx5e_hwstamp_set(dev, ifr);
2722 return mlx5e_hwstamp_get(dev, ifr);
2728 static int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2730 struct mlx5e_priv *priv = netdev_priv(dev);
2731 struct mlx5_core_dev *mdev = priv->mdev;
2733 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
2736 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos)
2738 struct mlx5e_priv *priv = netdev_priv(dev);
2739 struct mlx5_core_dev *mdev = priv->mdev;
2741 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
2745 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2747 struct mlx5e_priv *priv = netdev_priv(dev);
2748 struct mlx5_core_dev *mdev = priv->mdev;
2750 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
2753 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
2755 struct mlx5e_priv *priv = netdev_priv(dev);
2756 struct mlx5_core_dev *mdev = priv->mdev;
2758 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
2760 static int mlx5_vport_link2ifla(u8 esw_link)
2763 case MLX5_ESW_VPORT_ADMIN_STATE_DOWN:
2764 return IFLA_VF_LINK_STATE_DISABLE;
2765 case MLX5_ESW_VPORT_ADMIN_STATE_UP:
2766 return IFLA_VF_LINK_STATE_ENABLE;
2768 return IFLA_VF_LINK_STATE_AUTO;
2771 static int mlx5_ifla_link2vport(u8 ifla_link)
2773 switch (ifla_link) {
2774 case IFLA_VF_LINK_STATE_DISABLE:
2775 return MLX5_ESW_VPORT_ADMIN_STATE_DOWN;
2776 case IFLA_VF_LINK_STATE_ENABLE:
2777 return MLX5_ESW_VPORT_ADMIN_STATE_UP;
2779 return MLX5_ESW_VPORT_ADMIN_STATE_AUTO;
2782 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
2785 struct mlx5e_priv *priv = netdev_priv(dev);
2786 struct mlx5_core_dev *mdev = priv->mdev;
2788 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
2789 mlx5_ifla_link2vport(link_state));
2792 static int mlx5e_get_vf_config(struct net_device *dev,
2793 int vf, struct ifla_vf_info *ivi)
2795 struct mlx5e_priv *priv = netdev_priv(dev);
2796 struct mlx5_core_dev *mdev = priv->mdev;
2799 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
2802 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
2806 static int mlx5e_get_vf_stats(struct net_device *dev,
2807 int vf, struct ifla_vf_stats *vf_stats)
2809 struct mlx5e_priv *priv = netdev_priv(dev);
2810 struct mlx5_core_dev *mdev = priv->mdev;
2812 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
2816 static void mlx5e_add_vxlan_port(struct net_device *netdev,
2817 struct udp_tunnel_info *ti)
2819 struct mlx5e_priv *priv = netdev_priv(netdev);
2821 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2824 if (!mlx5e_vxlan_allowed(priv->mdev))
2827 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 1);
2830 static void mlx5e_del_vxlan_port(struct net_device *netdev,
2831 struct udp_tunnel_info *ti)
2833 struct mlx5e_priv *priv = netdev_priv(netdev);
2835 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2838 if (!mlx5e_vxlan_allowed(priv->mdev))
2841 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 0);
2844 static netdev_features_t mlx5e_vxlan_features_check(struct mlx5e_priv *priv,
2845 struct sk_buff *skb,
2846 netdev_features_t features)
2848 struct udphdr *udph;
2852 switch (vlan_get_protocol(skb)) {
2853 case htons(ETH_P_IP):
2854 proto = ip_hdr(skb)->protocol;
2856 case htons(ETH_P_IPV6):
2857 proto = ipv6_hdr(skb)->nexthdr;
2863 if (proto == IPPROTO_UDP) {
2864 udph = udp_hdr(skb);
2865 port = be16_to_cpu(udph->dest);
2868 /* Verify if UDP port is being offloaded by HW */
2869 if (port && mlx5e_vxlan_lookup_port(priv, port))
2873 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
2874 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2877 static netdev_features_t mlx5e_features_check(struct sk_buff *skb,
2878 struct net_device *netdev,
2879 netdev_features_t features)
2881 struct mlx5e_priv *priv = netdev_priv(netdev);
2883 features = vlan_features_check(skb, features);
2884 features = vxlan_features_check(skb, features);
2886 /* Validate if the tunneled packet is being offloaded by HW */
2887 if (skb->encapsulation &&
2888 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
2889 return mlx5e_vxlan_features_check(priv, skb, features);
2894 static void mlx5e_tx_timeout(struct net_device *dev)
2896 struct mlx5e_priv *priv = netdev_priv(dev);
2897 bool sched_work = false;
2900 netdev_err(dev, "TX timeout detected\n");
2902 for (i = 0; i < priv->params.num_channels * priv->params.num_tc; i++) {
2903 struct mlx5e_sq *sq = priv->txq_to_sq_map[i];
2905 if (!netif_xmit_stopped(netdev_get_tx_queue(dev, i)))
2908 set_bit(MLX5E_SQ_STATE_FLUSH, &sq->state);
2909 netdev_err(dev, "TX timeout on queue: %d, SQ: 0x%x, CQ: 0x%x, SQ Cons: 0x%x SQ Prod: 0x%x\n",
2910 i, sq->sqn, sq->cq.mcq.cqn, sq->cc, sq->pc);
2913 if (sched_work && test_bit(MLX5E_STATE_OPENED, &priv->state))
2914 schedule_work(&priv->tx_timeout_work);
2917 static const struct net_device_ops mlx5e_netdev_ops_basic = {
2918 .ndo_open = mlx5e_open,
2919 .ndo_stop = mlx5e_close,
2920 .ndo_start_xmit = mlx5e_xmit,
2921 .ndo_setup_tc = mlx5e_ndo_setup_tc,
2922 .ndo_select_queue = mlx5e_select_queue,
2923 .ndo_get_stats64 = mlx5e_get_stats,
2924 .ndo_set_rx_mode = mlx5e_set_rx_mode,
2925 .ndo_set_mac_address = mlx5e_set_mac,
2926 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
2927 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
2928 .ndo_set_features = mlx5e_set_features,
2929 .ndo_change_mtu = mlx5e_change_mtu,
2930 .ndo_do_ioctl = mlx5e_ioctl,
2931 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
2932 #ifdef CONFIG_RFS_ACCEL
2933 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
2935 .ndo_tx_timeout = mlx5e_tx_timeout,
2938 static const struct net_device_ops mlx5e_netdev_ops_sriov = {
2939 .ndo_open = mlx5e_open,
2940 .ndo_stop = mlx5e_close,
2941 .ndo_start_xmit = mlx5e_xmit,
2942 .ndo_setup_tc = mlx5e_ndo_setup_tc,
2943 .ndo_select_queue = mlx5e_select_queue,
2944 .ndo_get_stats64 = mlx5e_get_stats,
2945 .ndo_set_rx_mode = mlx5e_set_rx_mode,
2946 .ndo_set_mac_address = mlx5e_set_mac,
2947 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
2948 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
2949 .ndo_set_features = mlx5e_set_features,
2950 .ndo_change_mtu = mlx5e_change_mtu,
2951 .ndo_do_ioctl = mlx5e_ioctl,
2952 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
2953 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
2954 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
2955 .ndo_features_check = mlx5e_features_check,
2956 #ifdef CONFIG_RFS_ACCEL
2957 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
2959 .ndo_set_vf_mac = mlx5e_set_vf_mac,
2960 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
2961 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
2962 .ndo_set_vf_trust = mlx5e_set_vf_trust,
2963 .ndo_get_vf_config = mlx5e_get_vf_config,
2964 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
2965 .ndo_get_vf_stats = mlx5e_get_vf_stats,
2966 .ndo_tx_timeout = mlx5e_tx_timeout,
2969 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
2971 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
2973 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
2974 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
2975 !MLX5_CAP_ETH(mdev, csum_cap) ||
2976 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
2977 !MLX5_CAP_ETH(mdev, vlan_cap) ||
2978 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
2979 MLX5_CAP_FLOWTABLE(mdev,
2980 flow_table_properties_nic_receive.max_ft_level)
2982 mlx5_core_warn(mdev,
2983 "Not creating net device, some required device capabilities are missing\n");
2986 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
2987 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
2988 if (!MLX5_CAP_GEN(mdev, cq_moderation))
2989 mlx5_core_warn(mdev, "CQ modiration is not supported\n");
2994 u16 mlx5e_get_max_inline_cap(struct mlx5_core_dev *mdev)
2996 int bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
2998 return bf_buf_size -
2999 sizeof(struct mlx5e_tx_wqe) +
3000 2 /*sizeof(mlx5e_tx_wqe.inline_hdr_start)*/;
3003 #ifdef CONFIG_MLX5_CORE_EN_DCB
3004 static void mlx5e_ets_init(struct mlx5e_priv *priv)
3008 priv->params.ets.ets_cap = mlx5_max_tc(priv->mdev) + 1;
3009 for (i = 0; i < priv->params.ets.ets_cap; i++) {
3010 priv->params.ets.tc_tx_bw[i] = MLX5E_MAX_BW_ALLOC;
3011 priv->params.ets.tc_tsa[i] = IEEE_8021QAZ_TSA_VENDOR;
3012 priv->params.ets.prio_tc[i] = i;
3015 /* tclass[prio=0]=1, tclass[prio=1]=0, tclass[prio=i]=i (for i>1) */
3016 priv->params.ets.prio_tc[0] = 1;
3017 priv->params.ets.prio_tc[1] = 0;
3021 void mlx5e_build_default_indir_rqt(struct mlx5_core_dev *mdev,
3022 u32 *indirection_rqt, int len,
3025 int node = mdev->priv.numa_node;
3026 int node_num_of_cores;
3030 node = first_online_node;
3032 node_num_of_cores = cpumask_weight(cpumask_of_node(node));
3034 if (node_num_of_cores)
3035 num_channels = min_t(int, num_channels, node_num_of_cores);
3037 for (i = 0; i < len; i++)
3038 indirection_rqt[i] = i % num_channels;
3041 static bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
3043 return MLX5_CAP_GEN(mdev, striding_rq) &&
3044 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
3045 MLX5_CAP_ETH(mdev, reg_umr_sq);
3048 static int mlx5e_get_pci_bw(struct mlx5_core_dev *mdev, u32 *pci_bw)
3050 enum pcie_link_width width;
3051 enum pci_bus_speed speed;
3054 err = pcie_get_minimum_link(mdev->pdev, &speed, &width);
3058 if (speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
3062 case PCIE_SPEED_2_5GT:
3063 *pci_bw = 2500 * width;
3065 case PCIE_SPEED_5_0GT:
3066 *pci_bw = 5000 * width;
3068 case PCIE_SPEED_8_0GT:
3069 *pci_bw = 8000 * width;
3078 static bool cqe_compress_heuristic(u32 link_speed, u32 pci_bw)
3080 return (link_speed && pci_bw &&
3081 (pci_bw < 40000) && (pci_bw < link_speed));
3084 void mlx5e_set_rx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
3086 params->rx_cq_period_mode = cq_period_mode;
3088 params->rx_cq_moderation.pkts =
3089 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
3090 params->rx_cq_moderation.usec =
3091 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
3093 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
3094 params->rx_cq_moderation.usec =
3095 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE;
3098 static void mlx5e_query_min_inline(struct mlx5_core_dev *mdev,
3099 u8 *min_inline_mode)
3101 switch (MLX5_CAP_ETH(mdev, wqe_inline_mode)) {
3102 case MLX5E_INLINE_MODE_L2:
3103 *min_inline_mode = MLX5_INLINE_MODE_L2;
3105 case MLX5E_INLINE_MODE_VPORT_CONTEXT:
3106 mlx5_query_nic_vport_min_inline(mdev,
3109 case MLX5_INLINE_MODE_NOT_REQUIRED:
3110 *min_inline_mode = MLX5_INLINE_MODE_NONE;
3115 static void mlx5e_build_nic_netdev_priv(struct mlx5_core_dev *mdev,
3116 struct net_device *netdev,
3117 const struct mlx5e_profile *profile,
3120 struct mlx5e_priv *priv = netdev_priv(netdev);
3123 u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
3124 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
3125 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
3127 priv->params.log_sq_size =
3128 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
3129 priv->params.rq_wq_type = mlx5e_check_fragmented_striding_rq_cap(mdev) ?
3130 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
3131 MLX5_WQ_TYPE_LINKED_LIST;
3133 /* set CQE compression */
3134 priv->params.rx_cqe_compress_admin = false;
3135 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
3136 MLX5_CAP_GEN(mdev, vport_group_manager)) {
3137 mlx5e_get_max_linkspeed(mdev, &link_speed);
3138 mlx5e_get_pci_bw(mdev, &pci_bw);
3139 mlx5_core_dbg(mdev, "Max link speed = %d, PCI BW = %d\n",
3140 link_speed, pci_bw);
3141 priv->params.rx_cqe_compress_admin =
3142 cqe_compress_heuristic(link_speed, pci_bw);
3145 priv->params.rx_cqe_compress = priv->params.rx_cqe_compress_admin;
3147 switch (priv->params.rq_wq_type) {
3148 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
3149 priv->params.log_rq_size = MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE_MPW;
3150 priv->params.mpwqe_log_stride_sz =
3151 priv->params.rx_cqe_compress ?
3152 MLX5_MPWRQ_LOG_STRIDE_SIZE_CQE_COMPRESS :
3153 MLX5_MPWRQ_LOG_STRIDE_SIZE;
3154 priv->params.mpwqe_log_num_strides = MLX5_MPWRQ_LOG_WQE_SZ -
3155 priv->params.mpwqe_log_stride_sz;
3156 priv->params.lro_en = true;
3158 default: /* MLX5_WQ_TYPE_LINKED_LIST */
3159 priv->params.log_rq_size = MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
3162 mlx5_core_info(mdev,
3163 "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
3164 priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
3165 BIT(priv->params.log_rq_size),
3166 BIT(priv->params.mpwqe_log_stride_sz),
3167 priv->params.rx_cqe_compress_admin);
3169 priv->params.min_rx_wqes = mlx5_min_rx_wqes(priv->params.rq_wq_type,
3170 BIT(priv->params.log_rq_size));
3172 priv->params.rx_am_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
3173 mlx5e_set_rx_cq_mode_params(&priv->params, cq_period_mode);
3175 priv->params.tx_cq_moderation.usec =
3176 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
3177 priv->params.tx_cq_moderation.pkts =
3178 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
3179 priv->params.tx_max_inline = mlx5e_get_max_inline_cap(mdev);
3180 mlx5e_query_min_inline(mdev, &priv->params.tx_min_inline_mode);
3181 priv->params.num_tc = 1;
3182 priv->params.rss_hfunc = ETH_RSS_HASH_XOR;
3184 netdev_rss_key_fill(priv->params.toeplitz_hash_key,
3185 sizeof(priv->params.toeplitz_hash_key));
3187 mlx5e_build_default_indir_rqt(mdev, priv->params.indirection_rqt,
3188 MLX5E_INDIR_RQT_SIZE, profile->max_nch(mdev));
3190 priv->params.lro_wqe_sz =
3191 MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
3193 /* Initialize pflags */
3194 MLX5E_SET_PRIV_FLAG(priv, MLX5E_PFLAG_RX_CQE_BASED_MODER,
3195 priv->params.rx_cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
3198 priv->netdev = netdev;
3199 priv->params.num_channels = profile->max_nch(mdev);
3200 priv->profile = profile;
3201 priv->ppriv = ppriv;
3203 #ifdef CONFIG_MLX5_CORE_EN_DCB
3204 mlx5e_ets_init(priv);
3207 mutex_init(&priv->state_lock);
3209 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
3210 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
3211 INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work);
3212 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
3215 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
3217 struct mlx5e_priv *priv = netdev_priv(netdev);
3219 mlx5_query_nic_vport_mac_address(priv->mdev, 0, netdev->dev_addr);
3220 if (is_zero_ether_addr(netdev->dev_addr) &&
3221 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
3222 eth_hw_addr_random(netdev);
3223 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
3227 static const struct switchdev_ops mlx5e_switchdev_ops = {
3228 .switchdev_port_attr_get = mlx5e_attr_get,
3231 static void mlx5e_build_nic_netdev(struct net_device *netdev)
3233 struct mlx5e_priv *priv = netdev_priv(netdev);
3234 struct mlx5_core_dev *mdev = priv->mdev;
3238 SET_NETDEV_DEV(netdev, &mdev->pdev->dev);
3240 if (MLX5_CAP_GEN(mdev, vport_group_manager)) {
3241 netdev->netdev_ops = &mlx5e_netdev_ops_sriov;
3242 #ifdef CONFIG_MLX5_CORE_EN_DCB
3243 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
3246 netdev->netdev_ops = &mlx5e_netdev_ops_basic;
3249 netdev->watchdog_timeo = 15 * HZ;
3251 netdev->ethtool_ops = &mlx5e_ethtool_ops;
3253 netdev->vlan_features |= NETIF_F_SG;
3254 netdev->vlan_features |= NETIF_F_IP_CSUM;
3255 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
3256 netdev->vlan_features |= NETIF_F_GRO;
3257 netdev->vlan_features |= NETIF_F_TSO;
3258 netdev->vlan_features |= NETIF_F_TSO6;
3259 netdev->vlan_features |= NETIF_F_RXCSUM;
3260 netdev->vlan_features |= NETIF_F_RXHASH;
3262 if (!!MLX5_CAP_ETH(mdev, lro_cap))
3263 netdev->vlan_features |= NETIF_F_LRO;
3265 netdev->hw_features = netdev->vlan_features;
3266 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
3267 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
3268 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3270 if (mlx5e_vxlan_allowed(mdev)) {
3271 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
3272 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3273 NETIF_F_GSO_PARTIAL;
3274 netdev->hw_enc_features |= NETIF_F_IP_CSUM;
3275 netdev->hw_enc_features |= NETIF_F_IPV6_CSUM;
3276 netdev->hw_enc_features |= NETIF_F_TSO;
3277 netdev->hw_enc_features |= NETIF_F_TSO6;
3278 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL;
3279 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
3280 NETIF_F_GSO_PARTIAL;
3281 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
3284 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
3287 netdev->hw_features |= NETIF_F_RXALL;
3289 netdev->features = netdev->hw_features;
3290 if (!priv->params.lro_en)
3291 netdev->features &= ~NETIF_F_LRO;
3294 netdev->features &= ~NETIF_F_RXALL;
3296 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
3297 if (FT_CAP(flow_modify_en) &&
3298 FT_CAP(modify_root) &&
3299 FT_CAP(identified_miss_table_mode) &&
3300 FT_CAP(flow_table_modify)) {
3301 netdev->hw_features |= NETIF_F_HW_TC;
3302 #ifdef CONFIG_RFS_ACCEL
3303 netdev->hw_features |= NETIF_F_NTUPLE;
3307 netdev->features |= NETIF_F_HIGHDMA;
3309 netdev->priv_flags |= IFF_UNICAST_FLT;
3311 mlx5e_set_netdev_dev_addr(netdev);
3313 #ifdef CONFIG_NET_SWITCHDEV
3314 if (MLX5_CAP_GEN(mdev, vport_group_manager))
3315 netdev->switchdev_ops = &mlx5e_switchdev_ops;
3319 static void mlx5e_create_q_counter(struct mlx5e_priv *priv)
3321 struct mlx5_core_dev *mdev = priv->mdev;
3324 err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
3326 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
3327 priv->q_counter = 0;
3331 static void mlx5e_destroy_q_counter(struct mlx5e_priv *priv)
3333 if (!priv->q_counter)
3336 mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
3339 static int mlx5e_create_umr_mkey(struct mlx5e_priv *priv)
3341 struct mlx5_core_dev *mdev = priv->mdev;
3342 u64 npages = MLX5E_REQUIRED_MTTS(priv->profile->max_nch(mdev),
3343 BIT(MLX5E_PARAMS_MAXIMUM_LOG_RQ_SIZE_MPW));
3344 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
3349 in = mlx5_vzalloc(inlen);
3353 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
3355 npages = min_t(u32, ALIGN(U16_MAX, 4) * 2, npages);
3357 MLX5_SET(mkc, mkc, free, 1);
3358 MLX5_SET(mkc, mkc, umr_en, 1);
3359 MLX5_SET(mkc, mkc, lw, 1);
3360 MLX5_SET(mkc, mkc, lr, 1);
3361 MLX5_SET(mkc, mkc, access_mode, MLX5_MKC_ACCESS_MODE_MTT);
3363 MLX5_SET(mkc, mkc, qpn, 0xffffff);
3364 MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.pdn);
3365 MLX5_SET64(mkc, mkc, len, npages << PAGE_SHIFT);
3366 MLX5_SET(mkc, mkc, translations_octword_size,
3367 MLX5_MTT_OCTW(npages));
3368 MLX5_SET(mkc, mkc, log_page_size, PAGE_SHIFT);
3370 err = mlx5_core_create_mkey(mdev, &priv->umr_mkey, in, inlen);
3376 static void mlx5e_nic_init(struct mlx5_core_dev *mdev,
3377 struct net_device *netdev,
3378 const struct mlx5e_profile *profile,
3381 struct mlx5e_priv *priv = netdev_priv(netdev);
3383 mlx5e_build_nic_netdev_priv(mdev, netdev, profile, ppriv);
3384 mlx5e_build_nic_netdev(netdev);
3385 mlx5e_vxlan_init(priv);
3388 static void mlx5e_nic_cleanup(struct mlx5e_priv *priv)
3390 struct mlx5_core_dev *mdev = priv->mdev;
3391 struct mlx5_eswitch *esw = mdev->priv.eswitch;
3393 mlx5e_vxlan_cleanup(priv);
3395 if (MLX5_CAP_GEN(mdev, vport_group_manager))
3396 mlx5_eswitch_unregister_vport_rep(esw, 0);
3399 static int mlx5e_init_nic_rx(struct mlx5e_priv *priv)
3401 struct mlx5_core_dev *mdev = priv->mdev;
3405 err = mlx5e_create_indirect_rqts(priv);
3407 mlx5_core_warn(mdev, "create indirect rqts failed, %d\n", err);
3411 err = mlx5e_create_direct_rqts(priv);
3413 mlx5_core_warn(mdev, "create direct rqts failed, %d\n", err);
3414 goto err_destroy_indirect_rqts;
3417 err = mlx5e_create_indirect_tirs(priv);
3419 mlx5_core_warn(mdev, "create indirect tirs failed, %d\n", err);
3420 goto err_destroy_direct_rqts;
3423 err = mlx5e_create_direct_tirs(priv);
3425 mlx5_core_warn(mdev, "create direct tirs failed, %d\n", err);
3426 goto err_destroy_indirect_tirs;
3429 err = mlx5e_create_flow_steering(priv);
3431 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
3432 goto err_destroy_direct_tirs;
3435 err = mlx5e_tc_init(priv);
3437 goto err_destroy_flow_steering;
3441 err_destroy_flow_steering:
3442 mlx5e_destroy_flow_steering(priv);
3443 err_destroy_direct_tirs:
3444 mlx5e_destroy_direct_tirs(priv);
3445 err_destroy_indirect_tirs:
3446 mlx5e_destroy_indirect_tirs(priv);
3447 err_destroy_direct_rqts:
3448 for (i = 0; i < priv->profile->max_nch(mdev); i++)
3449 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
3450 err_destroy_indirect_rqts:
3451 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
3455 static void mlx5e_cleanup_nic_rx(struct mlx5e_priv *priv)
3459 mlx5e_tc_cleanup(priv);
3460 mlx5e_destroy_flow_steering(priv);
3461 mlx5e_destroy_direct_tirs(priv);
3462 mlx5e_destroy_indirect_tirs(priv);
3463 for (i = 0; i < priv->profile->max_nch(priv->mdev); i++)
3464 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
3465 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
3468 static int mlx5e_init_nic_tx(struct mlx5e_priv *priv)
3472 err = mlx5e_create_tises(priv);
3474 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
3478 #ifdef CONFIG_MLX5_CORE_EN_DCB
3479 mlx5e_dcbnl_ieee_setets_core(priv, &priv->params.ets);
3484 static void mlx5e_nic_enable(struct mlx5e_priv *priv)
3486 struct net_device *netdev = priv->netdev;
3487 struct mlx5_core_dev *mdev = priv->mdev;
3488 struct mlx5_eswitch *esw = mdev->priv.eswitch;
3489 struct mlx5_eswitch_rep rep;
3491 mlx5_lag_add(mdev, netdev);
3493 if (mlx5e_vxlan_allowed(mdev)) {
3495 udp_tunnel_get_rx_info(netdev);
3499 mlx5e_enable_async_events(priv);
3500 queue_work(priv->wq, &priv->set_rx_mode_work);
3502 if (MLX5_CAP_GEN(mdev, vport_group_manager)) {
3503 mlx5_query_nic_vport_mac_address(mdev, 0, rep.hw_id);
3504 rep.load = mlx5e_nic_rep_load;
3505 rep.unload = mlx5e_nic_rep_unload;
3507 rep.priv_data = priv;
3508 mlx5_eswitch_register_vport_rep(esw, &rep);
3512 static void mlx5e_nic_disable(struct mlx5e_priv *priv)
3514 queue_work(priv->wq, &priv->set_rx_mode_work);
3515 mlx5e_disable_async_events(priv);
3516 mlx5_lag_remove(priv->mdev);
3519 static const struct mlx5e_profile mlx5e_nic_profile = {
3520 .init = mlx5e_nic_init,
3521 .cleanup = mlx5e_nic_cleanup,
3522 .init_rx = mlx5e_init_nic_rx,
3523 .cleanup_rx = mlx5e_cleanup_nic_rx,
3524 .init_tx = mlx5e_init_nic_tx,
3525 .cleanup_tx = mlx5e_cleanup_nic_tx,
3526 .enable = mlx5e_nic_enable,
3527 .disable = mlx5e_nic_disable,
3528 .update_stats = mlx5e_update_stats,
3529 .max_nch = mlx5e_get_max_num_channels,
3530 .max_tc = MLX5E_MAX_NUM_TC,
3533 struct net_device *mlx5e_create_netdev(struct mlx5_core_dev *mdev,
3534 const struct mlx5e_profile *profile,
3537 int nch = profile->max_nch(mdev);
3538 struct net_device *netdev;
3539 struct mlx5e_priv *priv;
3541 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
3542 nch * profile->max_tc,
3545 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
3549 profile->init(mdev, netdev, profile, ppriv);
3551 netif_carrier_off(netdev);
3553 priv = netdev_priv(netdev);
3555 priv->wq = create_singlethread_workqueue("mlx5e");
3557 goto err_cleanup_nic;
3562 profile->cleanup(priv);
3563 free_netdev(netdev);
3568 int mlx5e_attach_netdev(struct mlx5_core_dev *mdev, struct net_device *netdev)
3570 const struct mlx5e_profile *profile;
3571 struct mlx5e_priv *priv;
3574 priv = netdev_priv(netdev);
3575 profile = priv->profile;
3576 clear_bit(MLX5E_STATE_DESTROYING, &priv->state);
3578 err = mlx5e_create_umr_mkey(priv);
3580 mlx5_core_err(mdev, "create umr mkey failed, %d\n", err);
3584 err = profile->init_tx(priv);
3586 goto err_destroy_umr_mkey;
3588 err = mlx5e_open_drop_rq(priv);
3590 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
3591 goto err_cleanup_tx;
3594 err = profile->init_rx(priv);
3596 goto err_close_drop_rq;
3598 mlx5e_create_q_counter(priv);
3600 mlx5e_init_l2_addr(priv);
3602 mlx5e_set_dev_port_mtu(netdev);
3604 if (profile->enable)
3605 profile->enable(priv);
3608 if (netif_running(netdev))
3610 netif_device_attach(netdev);
3616 mlx5e_close_drop_rq(priv);
3619 profile->cleanup_tx(priv);
3621 err_destroy_umr_mkey:
3622 mlx5_core_destroy_mkey(mdev, &priv->umr_mkey);
3628 static void mlx5e_register_vport_rep(struct mlx5_core_dev *mdev)
3630 struct mlx5_eswitch *esw = mdev->priv.eswitch;
3631 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
3635 if (!MLX5_CAP_GEN(mdev, vport_group_manager))
3638 mlx5_query_nic_vport_mac_address(mdev, 0, mac);
3640 for (vport = 1; vport < total_vfs; vport++) {
3641 struct mlx5_eswitch_rep rep;
3643 rep.load = mlx5e_vport_rep_load;
3644 rep.unload = mlx5e_vport_rep_unload;
3646 ether_addr_copy(rep.hw_id, mac);
3647 mlx5_eswitch_register_vport_rep(esw, &rep);
3651 void mlx5e_detach_netdev(struct mlx5_core_dev *mdev, struct net_device *netdev)
3653 struct mlx5e_priv *priv = netdev_priv(netdev);
3654 const struct mlx5e_profile *profile = priv->profile;
3656 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
3657 if (profile->disable)
3658 profile->disable(priv);
3660 flush_workqueue(priv->wq);
3663 if (netif_running(netdev))
3664 mlx5e_close(netdev);
3665 netif_device_detach(netdev);
3668 mlx5e_destroy_q_counter(priv);
3669 profile->cleanup_rx(priv);
3670 mlx5e_close_drop_rq(priv);
3671 profile->cleanup_tx(priv);
3672 mlx5_core_destroy_mkey(priv->mdev, &priv->umr_mkey);
3673 cancel_delayed_work_sync(&priv->update_stats_work);
3676 /* mlx5e_attach and mlx5e_detach scope should be only creating/destroying
3677 * hardware contexts and to connect it to the current netdev.
3679 static int mlx5e_attach(struct mlx5_core_dev *mdev, void *vpriv)
3681 struct mlx5e_priv *priv = vpriv;
3682 struct net_device *netdev = priv->netdev;
3685 if (netif_device_present(netdev))
3688 err = mlx5e_create_mdev_resources(mdev);
3692 err = mlx5e_attach_netdev(mdev, netdev);
3694 mlx5e_destroy_mdev_resources(mdev);
3701 static void mlx5e_detach(struct mlx5_core_dev *mdev, void *vpriv)
3703 struct mlx5e_priv *priv = vpriv;
3704 struct net_device *netdev = priv->netdev;
3706 if (!netif_device_present(netdev))
3709 mlx5e_detach_netdev(mdev, netdev);
3710 mlx5e_destroy_mdev_resources(mdev);
3713 static void *mlx5e_add(struct mlx5_core_dev *mdev)
3715 struct mlx5_eswitch *esw = mdev->priv.eswitch;
3716 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
3721 struct net_device *netdev;
3723 err = mlx5e_check_required_hca_cap(mdev);
3727 mlx5e_register_vport_rep(mdev);
3729 if (MLX5_CAP_GEN(mdev, vport_group_manager))
3730 ppriv = &esw->offloads.vport_reps[0];
3732 netdev = mlx5e_create_netdev(mdev, &mlx5e_nic_profile, ppriv);
3734 mlx5_core_err(mdev, "mlx5e_create_netdev failed\n");
3735 goto err_unregister_reps;
3738 priv = netdev_priv(netdev);
3740 err = mlx5e_attach(mdev, priv);
3742 mlx5_core_err(mdev, "mlx5e_attach failed, %d\n", err);
3743 goto err_destroy_netdev;
3746 err = register_netdev(netdev);
3748 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
3755 mlx5e_detach(mdev, priv);
3758 mlx5e_destroy_netdev(mdev, priv);
3760 err_unregister_reps:
3761 for (vport = 1; vport < total_vfs; vport++)
3762 mlx5_eswitch_unregister_vport_rep(esw, vport);
3767 void mlx5e_destroy_netdev(struct mlx5_core_dev *mdev, struct mlx5e_priv *priv)
3769 const struct mlx5e_profile *profile = priv->profile;
3770 struct net_device *netdev = priv->netdev;
3772 unregister_netdev(netdev);
3773 destroy_workqueue(priv->wq);
3774 if (profile->cleanup)
3775 profile->cleanup(priv);
3776 free_netdev(netdev);
3779 static void mlx5e_remove(struct mlx5_core_dev *mdev, void *vpriv)
3781 struct mlx5_eswitch *esw = mdev->priv.eswitch;
3782 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
3783 struct mlx5e_priv *priv = vpriv;
3786 for (vport = 1; vport < total_vfs; vport++)
3787 mlx5_eswitch_unregister_vport_rep(esw, vport);
3789 mlx5e_detach(mdev, vpriv);
3790 mlx5e_destroy_netdev(mdev, priv);
3793 static void *mlx5e_get_netdev(void *vpriv)
3795 struct mlx5e_priv *priv = vpriv;
3797 return priv->netdev;
3800 static struct mlx5_interface mlx5e_interface = {
3802 .remove = mlx5e_remove,
3803 .attach = mlx5e_attach,
3804 .detach = mlx5e_detach,
3805 .event = mlx5e_async_event,
3806 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
3807 .get_dev = mlx5e_get_netdev,
3810 void mlx5e_init(void)
3812 mlx5e_build_ptys2ethtool_map();
3813 mlx5_register_interface(&mlx5e_interface);
3816 void mlx5e_cleanup(void)
3818 mlx5_unregister_interface(&mlx5e_interface);