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>
43 MLX5_EN_QP_FLUSH_TIMEOUT_MS = 5000,
44 MLX5_EN_QP_FLUSH_MSLEEP_QUANT = 20,
45 MLX5_EN_QP_FLUSH_MAX_ITER = MLX5_EN_QP_FLUSH_TIMEOUT_MS /
46 MLX5_EN_QP_FLUSH_MSLEEP_QUANT,
49 struct mlx5e_rq_param {
50 u32 rqc[MLX5_ST_SZ_DW(rqc)];
51 struct mlx5_wq_param wq;
54 struct mlx5e_sq_param {
55 u32 sqc[MLX5_ST_SZ_DW(sqc)];
56 struct mlx5_wq_param wq;
61 struct mlx5e_cq_param {
62 u32 cqc[MLX5_ST_SZ_DW(cqc)];
63 struct mlx5_wq_param wq;
67 struct mlx5e_channel_param {
68 struct mlx5e_rq_param rq;
69 struct mlx5e_sq_param sq;
70 struct mlx5e_sq_param icosq;
71 struct mlx5e_cq_param rx_cq;
72 struct mlx5e_cq_param tx_cq;
73 struct mlx5e_cq_param icosq_cq;
76 static void mlx5e_update_carrier(struct mlx5e_priv *priv)
78 struct mlx5_core_dev *mdev = priv->mdev;
81 port_state = mlx5_query_vport_state(mdev,
82 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT, 0);
84 if (port_state == VPORT_STATE_UP)
85 netif_carrier_on(priv->netdev);
87 netif_carrier_off(priv->netdev);
90 static void mlx5e_update_carrier_work(struct work_struct *work)
92 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
95 mutex_lock(&priv->state_lock);
96 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
97 mlx5e_update_carrier(priv);
98 mutex_unlock(&priv->state_lock);
101 static void mlx5e_tx_timeout_work(struct work_struct *work)
103 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
108 mutex_lock(&priv->state_lock);
109 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
111 mlx5e_close_locked(priv->netdev);
112 err = mlx5e_open_locked(priv->netdev);
114 netdev_err(priv->netdev, "mlx5e_open_locked failed recovering from a tx_timeout, err(%d).\n",
117 mutex_unlock(&priv->state_lock);
121 static void mlx5e_update_sw_counters(struct mlx5e_priv *priv)
123 struct mlx5e_sw_stats *s = &priv->stats.sw;
124 struct mlx5e_rq_stats *rq_stats;
125 struct mlx5e_sq_stats *sq_stats;
126 u64 tx_offload_none = 0;
129 memset(s, 0, sizeof(*s));
130 for (i = 0; i < priv->params.num_channels; i++) {
131 rq_stats = &priv->channel[i]->rq.stats;
133 s->rx_packets += rq_stats->packets;
134 s->rx_bytes += rq_stats->bytes;
135 s->rx_lro_packets += rq_stats->lro_packets;
136 s->rx_lro_bytes += rq_stats->lro_bytes;
137 s->rx_csum_none += rq_stats->csum_none;
138 s->rx_csum_complete += rq_stats->csum_complete;
139 s->rx_csum_unnecessary_inner += rq_stats->csum_unnecessary_inner;
140 s->rx_wqe_err += rq_stats->wqe_err;
141 s->rx_mpwqe_filler += rq_stats->mpwqe_filler;
142 s->rx_mpwqe_frag += rq_stats->mpwqe_frag;
143 s->rx_buff_alloc_err += rq_stats->buff_alloc_err;
144 s->rx_cqe_compress_blks += rq_stats->cqe_compress_blks;
145 s->rx_cqe_compress_pkts += rq_stats->cqe_compress_pkts;
147 for (j = 0; j < priv->params.num_tc; j++) {
148 sq_stats = &priv->channel[i]->sq[j].stats;
150 s->tx_packets += sq_stats->packets;
151 s->tx_bytes += sq_stats->bytes;
152 s->tx_tso_packets += sq_stats->tso_packets;
153 s->tx_tso_bytes += sq_stats->tso_bytes;
154 s->tx_tso_inner_packets += sq_stats->tso_inner_packets;
155 s->tx_tso_inner_bytes += sq_stats->tso_inner_bytes;
156 s->tx_queue_stopped += sq_stats->stopped;
157 s->tx_queue_wake += sq_stats->wake;
158 s->tx_queue_dropped += sq_stats->dropped;
159 s->tx_csum_partial_inner += sq_stats->csum_partial_inner;
160 tx_offload_none += sq_stats->csum_none;
164 /* Update calculated offload counters */
165 s->tx_csum_partial = s->tx_packets - tx_offload_none - s->tx_csum_partial_inner;
166 s->rx_csum_unnecessary = s->rx_packets - s->rx_csum_none - s->rx_csum_complete;
168 s->link_down_events_phy = MLX5_GET(ppcnt_reg,
169 priv->stats.pport.phy_counters,
170 counter_set.phys_layer_cntrs.link_down_events);
173 static void mlx5e_update_vport_counters(struct mlx5e_priv *priv)
175 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
176 u32 *out = (u32 *)priv->stats.vport.query_vport_out;
177 u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)];
178 struct mlx5_core_dev *mdev = priv->mdev;
180 memset(in, 0, sizeof(in));
182 MLX5_SET(query_vport_counter_in, in, opcode,
183 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
184 MLX5_SET(query_vport_counter_in, in, op_mod, 0);
185 MLX5_SET(query_vport_counter_in, in, other_vport, 0);
187 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 static 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 mlx5e_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 int mlx5e_create_rq(struct mlx5e_channel *c,
302 struct mlx5e_rq_param *param,
305 struct mlx5e_priv *priv = c->priv;
306 struct mlx5_core_dev *mdev = priv->mdev;
307 void *rqc = param->rqc;
308 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
314 param->wq.db_numa_node = cpu_to_node(c->cpu);
316 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
321 rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
323 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
325 switch (priv->params.rq_wq_type) {
326 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
327 rq->wqe_info = kzalloc_node(wq_sz * sizeof(*rq->wqe_info),
328 GFP_KERNEL, cpu_to_node(c->cpu));
331 goto err_rq_wq_destroy;
333 rq->handle_rx_cqe = mlx5e_handle_rx_cqe_mpwrq;
334 rq->alloc_wqe = mlx5e_alloc_rx_mpwqe;
335 rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
337 rq->mpwqe_stride_sz = BIT(priv->params.mpwqe_log_stride_sz);
338 rq->mpwqe_num_strides = BIT(priv->params.mpwqe_log_num_strides);
339 rq->wqe_sz = rq->mpwqe_stride_sz * rq->mpwqe_num_strides;
340 byte_count = rq->wqe_sz;
342 default: /* MLX5_WQ_TYPE_LINKED_LIST */
343 rq->skb = kzalloc_node(wq_sz * sizeof(*rq->skb), GFP_KERNEL,
344 cpu_to_node(c->cpu));
347 goto err_rq_wq_destroy;
349 rq->handle_rx_cqe = mlx5e_handle_rx_cqe;
350 rq->alloc_wqe = mlx5e_alloc_rx_wqe;
351 rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
353 rq->wqe_sz = (priv->params.lro_en) ?
354 priv->params.lro_wqe_sz :
355 MLX5E_SW2HW_MTU(priv->netdev->mtu);
356 rq->wqe_sz = SKB_DATA_ALIGN(rq->wqe_sz);
357 byte_count = rq->wqe_sz;
358 byte_count |= MLX5_HW_START_PADDING;
361 for (i = 0; i < wq_sz; i++) {
362 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
364 wqe->data.byte_count = cpu_to_be32(byte_count);
367 rq->wq_type = priv->params.rq_wq_type;
369 rq->netdev = c->netdev;
370 rq->tstamp = &priv->tstamp;
374 rq->mkey_be = c->mkey_be;
375 rq->umr_mkey_be = cpu_to_be32(c->priv->umr_mkey.key);
380 mlx5_wq_destroy(&rq->wq_ctrl);
385 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
387 switch (rq->wq_type) {
388 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
391 default: /* MLX5_WQ_TYPE_LINKED_LIST */
395 mlx5_wq_destroy(&rq->wq_ctrl);
398 static int mlx5e_enable_rq(struct mlx5e_rq *rq, struct mlx5e_rq_param *param)
400 struct mlx5e_priv *priv = rq->priv;
401 struct mlx5_core_dev *mdev = priv->mdev;
409 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
410 sizeof(u64) * rq->wq_ctrl.buf.npages;
411 in = mlx5_vzalloc(inlen);
415 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
416 wq = MLX5_ADDR_OF(rqc, rqc, wq);
418 memcpy(rqc, param->rqc, sizeof(param->rqc));
420 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
421 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
422 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
423 MLX5_SET(rqc, rqc, vsd, priv->params.vlan_strip_disable);
424 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
425 MLX5_ADAPTER_PAGE_SHIFT);
426 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
428 mlx5_fill_page_array(&rq->wq_ctrl.buf,
429 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
431 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
438 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
441 struct mlx5e_channel *c = rq->channel;
442 struct mlx5e_priv *priv = c->priv;
443 struct mlx5_core_dev *mdev = priv->mdev;
450 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
451 in = mlx5_vzalloc(inlen);
455 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
457 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
458 MLX5_SET(rqc, rqc, state, next_state);
460 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
467 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
469 struct mlx5e_channel *c = rq->channel;
470 struct mlx5e_priv *priv = c->priv;
471 struct mlx5_core_dev *mdev = priv->mdev;
478 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
479 in = mlx5_vzalloc(inlen);
483 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
485 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
486 MLX5_SET64(modify_rq_in, in, modify_bitmask, MLX5_RQ_BITMASK_VSD);
487 MLX5_SET(rqc, rqc, vsd, vsd);
488 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
490 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
497 static void mlx5e_disable_rq(struct mlx5e_rq *rq)
499 mlx5_core_destroy_rq(rq->priv->mdev, rq->rqn);
502 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq)
504 unsigned long exp_time = jiffies + msecs_to_jiffies(20000);
505 struct mlx5e_channel *c = rq->channel;
506 struct mlx5e_priv *priv = c->priv;
507 struct mlx5_wq_ll *wq = &rq->wq;
509 while (time_before(jiffies, exp_time)) {
510 if (wq->cur_sz >= priv->params.min_rx_wqes)
519 static int mlx5e_open_rq(struct mlx5e_channel *c,
520 struct mlx5e_rq_param *param,
523 struct mlx5e_sq *sq = &c->icosq;
524 u16 pi = sq->pc & sq->wq.sz_m1;
527 err = mlx5e_create_rq(c, param, rq);
531 err = mlx5e_enable_rq(rq, param);
535 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
539 set_bit(MLX5E_RQ_STATE_POST_WQES_ENABLE, &rq->state);
541 sq->ico_wqe_info[pi].opcode = MLX5_OPCODE_NOP;
542 sq->ico_wqe_info[pi].num_wqebbs = 1;
543 mlx5e_send_nop(sq, true); /* trigger mlx5e_post_rx_wqes() */
548 mlx5e_disable_rq(rq);
550 mlx5e_destroy_rq(rq);
555 static void mlx5e_close_rq(struct mlx5e_rq *rq)
560 clear_bit(MLX5E_RQ_STATE_POST_WQES_ENABLE, &rq->state);
561 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
563 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RDY, MLX5_RQC_STATE_ERR);
564 while (!mlx5_wq_ll_is_empty(&rq->wq) && !err &&
565 tout++ < MLX5_EN_QP_FLUSH_MAX_ITER)
566 msleep(MLX5_EN_QP_FLUSH_MSLEEP_QUANT);
568 if (err || tout == MLX5_EN_QP_FLUSH_MAX_ITER)
569 set_bit(MLX5E_RQ_STATE_FLUSH_TIMEOUT, &rq->state);
571 /* avoid destroying rq before mlx5e_poll_rx_cq() is done with it */
572 napi_synchronize(&rq->channel->napi);
574 mlx5e_disable_rq(rq);
575 mlx5e_free_rx_descs(rq);
576 mlx5e_destroy_rq(rq);
579 static void mlx5e_free_sq_db(struct mlx5e_sq *sq)
586 static int mlx5e_alloc_sq_db(struct mlx5e_sq *sq, int numa)
588 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
589 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
591 sq->skb = kzalloc_node(wq_sz * sizeof(*sq->skb), GFP_KERNEL, numa);
592 sq->dma_fifo = kzalloc_node(df_sz * sizeof(*sq->dma_fifo), GFP_KERNEL,
594 sq->wqe_info = kzalloc_node(wq_sz * sizeof(*sq->wqe_info), GFP_KERNEL,
597 if (!sq->skb || !sq->dma_fifo || !sq->wqe_info) {
598 mlx5e_free_sq_db(sq);
602 sq->dma_fifo_mask = df_sz - 1;
607 static int mlx5e_create_sq(struct mlx5e_channel *c,
609 struct mlx5e_sq_param *param,
612 struct mlx5e_priv *priv = c->priv;
613 struct mlx5_core_dev *mdev = priv->mdev;
615 void *sqc = param->sqc;
616 void *sqc_wq = MLX5_ADDR_OF(sqc, sqc, wq);
619 err = mlx5_alloc_map_uar(mdev, &sq->uar, !!MLX5_CAP_GEN(mdev, bf));
623 param->wq.db_numa_node = cpu_to_node(c->cpu);
625 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq,
628 goto err_unmap_free_uar;
630 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
631 if (sq->uar.bf_map) {
632 set_bit(MLX5E_SQ_STATE_BF_ENABLE, &sq->state);
633 sq->uar_map = sq->uar.bf_map;
635 sq->uar_map = sq->uar.map;
637 sq->bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
638 sq->max_inline = param->max_inline;
640 err = mlx5e_alloc_sq_db(sq, cpu_to_node(c->cpu));
642 goto err_sq_wq_destroy;
645 u8 wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
647 sq->ico_wqe_info = kzalloc_node(sizeof(*sq->ico_wqe_info) *
650 cpu_to_node(c->cpu));
651 if (!sq->ico_wqe_info) {
658 txq_ix = c->ix + tc * priv->params.num_channels;
659 sq->txq = netdev_get_tx_queue(priv->netdev, txq_ix);
660 priv->txq_to_sq_map[txq_ix] = sq;
664 sq->tstamp = &priv->tstamp;
665 sq->mkey_be = c->mkey_be;
668 sq->edge = (sq->wq.sz_m1 + 1) - MLX5_SEND_WQE_MAX_WQEBBS;
669 sq->bf_budget = MLX5E_SQ_BF_BUDGET;
674 mlx5e_free_sq_db(sq);
677 mlx5_wq_destroy(&sq->wq_ctrl);
680 mlx5_unmap_free_uar(mdev, &sq->uar);
685 static void mlx5e_destroy_sq(struct mlx5e_sq *sq)
687 struct mlx5e_channel *c = sq->channel;
688 struct mlx5e_priv *priv = c->priv;
690 kfree(sq->ico_wqe_info);
691 mlx5e_free_sq_db(sq);
692 mlx5_wq_destroy(&sq->wq_ctrl);
693 mlx5_unmap_free_uar(priv->mdev, &sq->uar);
696 static int mlx5e_enable_sq(struct mlx5e_sq *sq, struct mlx5e_sq_param *param)
698 struct mlx5e_channel *c = sq->channel;
699 struct mlx5e_priv *priv = c->priv;
700 struct mlx5_core_dev *mdev = priv->mdev;
708 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
709 sizeof(u64) * sq->wq_ctrl.buf.npages;
710 in = mlx5_vzalloc(inlen);
714 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
715 wq = MLX5_ADDR_OF(sqc, sqc, wq);
717 memcpy(sqc, param->sqc, sizeof(param->sqc));
719 MLX5_SET(sqc, sqc, tis_num_0, param->icosq ? 0 : priv->tisn[sq->tc]);
720 MLX5_SET(sqc, sqc, cqn, sq->cq.mcq.cqn);
721 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
722 MLX5_SET(sqc, sqc, tis_lst_sz, param->icosq ? 0 : 1);
723 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
725 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
726 MLX5_SET(wq, wq, uar_page, sq->uar.index);
727 MLX5_SET(wq, wq, log_wq_pg_sz, sq->wq_ctrl.buf.page_shift -
728 MLX5_ADAPTER_PAGE_SHIFT);
729 MLX5_SET64(wq, wq, dbr_addr, sq->wq_ctrl.db.dma);
731 mlx5_fill_page_array(&sq->wq_ctrl.buf,
732 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
734 err = mlx5_core_create_sq(mdev, in, inlen, &sq->sqn);
741 static int mlx5e_modify_sq(struct mlx5e_sq *sq, int curr_state, int next_state)
743 struct mlx5e_channel *c = sq->channel;
744 struct mlx5e_priv *priv = c->priv;
745 struct mlx5_core_dev *mdev = priv->mdev;
752 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
753 in = mlx5_vzalloc(inlen);
757 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
759 MLX5_SET(modify_sq_in, in, sq_state, curr_state);
760 MLX5_SET(sqc, sqc, state, next_state);
762 err = mlx5_core_modify_sq(mdev, sq->sqn, in, inlen);
769 static void mlx5e_disable_sq(struct mlx5e_sq *sq)
771 struct mlx5e_channel *c = sq->channel;
772 struct mlx5e_priv *priv = c->priv;
773 struct mlx5_core_dev *mdev = priv->mdev;
775 mlx5_core_destroy_sq(mdev, sq->sqn);
778 static int mlx5e_open_sq(struct mlx5e_channel *c,
780 struct mlx5e_sq_param *param,
785 err = mlx5e_create_sq(c, tc, param, sq);
789 err = mlx5e_enable_sq(sq, param);
793 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST, MLX5_SQC_STATE_RDY);
798 set_bit(MLX5E_SQ_STATE_WAKE_TXQ_ENABLE, &sq->state);
799 netdev_tx_reset_queue(sq->txq);
800 netif_tx_start_queue(sq->txq);
806 mlx5e_disable_sq(sq);
808 mlx5e_destroy_sq(sq);
813 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
815 __netif_tx_lock_bh(txq);
816 netif_tx_stop_queue(txq);
817 __netif_tx_unlock_bh(txq);
820 static void mlx5e_close_sq(struct mlx5e_sq *sq)
826 clear_bit(MLX5E_SQ_STATE_WAKE_TXQ_ENABLE, &sq->state);
827 /* prevent netif_tx_wake_queue */
828 napi_synchronize(&sq->channel->napi);
829 netif_tx_disable_queue(sq->txq);
831 /* ensure hw is notified of all pending wqes */
832 if (mlx5e_sq_has_room_for(sq, 1))
833 mlx5e_send_nop(sq, true);
835 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY,
838 set_bit(MLX5E_SQ_STATE_TX_TIMEOUT, &sq->state);
841 /* wait till sq is empty, unless a TX timeout occurred on this SQ */
842 while (sq->cc != sq->pc &&
843 !test_bit(MLX5E_SQ_STATE_TX_TIMEOUT, &sq->state)) {
844 msleep(MLX5_EN_QP_FLUSH_MSLEEP_QUANT);
845 if (tout++ > MLX5_EN_QP_FLUSH_MAX_ITER)
846 set_bit(MLX5E_SQ_STATE_TX_TIMEOUT, &sq->state);
849 /* avoid destroying sq before mlx5e_poll_tx_cq() is done with it */
850 napi_synchronize(&sq->channel->napi);
852 mlx5e_free_tx_descs(sq);
853 mlx5e_disable_sq(sq);
854 mlx5e_destroy_sq(sq);
857 static int mlx5e_create_cq(struct mlx5e_channel *c,
858 struct mlx5e_cq_param *param,
861 struct mlx5e_priv *priv = c->priv;
862 struct mlx5_core_dev *mdev = priv->mdev;
863 struct mlx5_core_cq *mcq = &cq->mcq;
869 param->wq.buf_numa_node = cpu_to_node(c->cpu);
870 param->wq.db_numa_node = cpu_to_node(c->cpu);
871 param->eq_ix = c->ix;
873 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
878 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
883 mcq->set_ci_db = cq->wq_ctrl.db.db;
884 mcq->arm_db = cq->wq_ctrl.db.db + 1;
887 mcq->vector = param->eq_ix;
888 mcq->comp = mlx5e_completion_event;
889 mcq->event = mlx5e_cq_error_event;
891 mcq->uar = &priv->cq_uar;
893 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
894 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
905 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
907 mlx5_wq_destroy(&cq->wq_ctrl);
910 static int mlx5e_enable_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
912 struct mlx5e_priv *priv = cq->priv;
913 struct mlx5_core_dev *mdev = priv->mdev;
914 struct mlx5_core_cq *mcq = &cq->mcq;
919 unsigned int irqn_not_used;
923 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
924 sizeof(u64) * cq->wq_ctrl.buf.npages;
925 in = mlx5_vzalloc(inlen);
929 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
931 memcpy(cqc, param->cqc, sizeof(param->cqc));
933 mlx5_fill_page_array(&cq->wq_ctrl.buf,
934 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
936 mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
938 MLX5_SET(cqc, cqc, c_eqn, eqn);
939 MLX5_SET(cqc, cqc, uar_page, mcq->uar->index);
940 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
941 MLX5_ADAPTER_PAGE_SHIFT);
942 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
944 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
956 static void mlx5e_disable_cq(struct mlx5e_cq *cq)
958 struct mlx5e_priv *priv = cq->priv;
959 struct mlx5_core_dev *mdev = priv->mdev;
961 mlx5_core_destroy_cq(mdev, &cq->mcq);
964 static int mlx5e_open_cq(struct mlx5e_channel *c,
965 struct mlx5e_cq_param *param,
967 u16 moderation_usecs,
968 u16 moderation_frames)
971 struct mlx5e_priv *priv = c->priv;
972 struct mlx5_core_dev *mdev = priv->mdev;
974 err = mlx5e_create_cq(c, param, cq);
978 err = mlx5e_enable_cq(cq, param);
982 if (MLX5_CAP_GEN(mdev, cq_moderation))
983 mlx5_core_modify_cq_moderation(mdev, &cq->mcq,
989 mlx5e_destroy_cq(cq);
994 static void mlx5e_close_cq(struct mlx5e_cq *cq)
996 mlx5e_disable_cq(cq);
997 mlx5e_destroy_cq(cq);
1000 static int mlx5e_get_cpu(struct mlx5e_priv *priv, int ix)
1002 return cpumask_first(priv->mdev->priv.irq_info[ix].mask);
1005 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1006 struct mlx5e_channel_param *cparam)
1008 struct mlx5e_priv *priv = c->priv;
1012 for (tc = 0; tc < c->num_tc; tc++) {
1013 err = mlx5e_open_cq(c, &cparam->tx_cq, &c->sq[tc].cq,
1014 priv->params.tx_cq_moderation_usec,
1015 priv->params.tx_cq_moderation_pkts);
1017 goto err_close_tx_cqs;
1023 for (tc--; tc >= 0; tc--)
1024 mlx5e_close_cq(&c->sq[tc].cq);
1029 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1033 for (tc = 0; tc < c->num_tc; tc++)
1034 mlx5e_close_cq(&c->sq[tc].cq);
1037 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1038 struct mlx5e_channel_param *cparam)
1043 for (tc = 0; tc < c->num_tc; tc++) {
1044 err = mlx5e_open_sq(c, tc, &cparam->sq, &c->sq[tc]);
1052 for (tc--; tc >= 0; tc--)
1053 mlx5e_close_sq(&c->sq[tc]);
1058 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1062 for (tc = 0; tc < c->num_tc; tc++)
1063 mlx5e_close_sq(&c->sq[tc]);
1066 static void mlx5e_build_channeltc_to_txq_map(struct mlx5e_priv *priv, int ix)
1070 for (i = 0; i < MLX5E_MAX_NUM_TC; i++)
1071 priv->channeltc_to_txq_map[ix][i] =
1072 ix + i * priv->params.num_channels;
1075 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1076 struct mlx5e_channel_param *cparam,
1077 struct mlx5e_channel **cp)
1079 struct net_device *netdev = priv->netdev;
1080 int cpu = mlx5e_get_cpu(priv, ix);
1081 struct mlx5e_channel *c;
1084 c = kzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1091 c->pdev = &priv->mdev->pdev->dev;
1092 c->netdev = priv->netdev;
1093 c->mkey_be = cpu_to_be32(priv->mkey.key);
1094 c->num_tc = priv->params.num_tc;
1096 mlx5e_build_channeltc_to_txq_map(priv, ix);
1098 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1100 err = mlx5e_open_cq(c, &cparam->icosq_cq, &c->icosq.cq, 0, 0);
1104 err = mlx5e_open_tx_cqs(c, cparam);
1106 goto err_close_icosq_cq;
1108 err = mlx5e_open_cq(c, &cparam->rx_cq, &c->rq.cq,
1109 priv->params.rx_cq_moderation_usec,
1110 priv->params.rx_cq_moderation_pkts);
1112 goto err_close_tx_cqs;
1114 napi_enable(&c->napi);
1116 err = mlx5e_open_sq(c, 0, &cparam->icosq, &c->icosq);
1118 goto err_disable_napi;
1120 err = mlx5e_open_sqs(c, cparam);
1122 goto err_close_icosq;
1124 err = mlx5e_open_rq(c, &cparam->rq, &c->rq);
1128 netif_set_xps_queue(netdev, get_cpu_mask(c->cpu), ix);
1137 mlx5e_close_sq(&c->icosq);
1140 napi_disable(&c->napi);
1141 mlx5e_close_cq(&c->rq.cq);
1144 mlx5e_close_tx_cqs(c);
1147 mlx5e_close_cq(&c->icosq.cq);
1150 netif_napi_del(&c->napi);
1151 napi_hash_del(&c->napi);
1157 static void mlx5e_close_channel(struct mlx5e_channel *c)
1159 mlx5e_close_rq(&c->rq);
1161 mlx5e_close_sq(&c->icosq);
1162 napi_disable(&c->napi);
1163 mlx5e_close_cq(&c->rq.cq);
1164 mlx5e_close_tx_cqs(c);
1165 mlx5e_close_cq(&c->icosq.cq);
1166 netif_napi_del(&c->napi);
1168 napi_hash_del(&c->napi);
1174 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
1175 struct mlx5e_rq_param *param)
1177 void *rqc = param->rqc;
1178 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1180 switch (priv->params.rq_wq_type) {
1181 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1182 MLX5_SET(wq, wq, log_wqe_num_of_strides,
1183 priv->params.mpwqe_log_num_strides - 9);
1184 MLX5_SET(wq, wq, log_wqe_stride_size,
1185 priv->params.mpwqe_log_stride_sz - 6);
1186 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ);
1188 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1189 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1192 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1193 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1194 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_rq_size);
1195 MLX5_SET(wq, wq, pd, priv->pdn);
1196 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
1198 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1199 param->wq.linear = 1;
1202 static void mlx5e_build_drop_rq_param(struct mlx5e_rq_param *param)
1204 void *rqc = param->rqc;
1205 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1207 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1208 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1211 static void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
1212 struct mlx5e_sq_param *param)
1214 void *sqc = param->sqc;
1215 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1217 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1218 MLX5_SET(wq, wq, pd, priv->pdn);
1220 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1223 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
1224 struct mlx5e_sq_param *param)
1226 void *sqc = param->sqc;
1227 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1229 mlx5e_build_sq_param_common(priv, param);
1230 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_sq_size);
1232 param->max_inline = priv->params.tx_max_inline;
1235 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1236 struct mlx5e_cq_param *param)
1238 void *cqc = param->cqc;
1240 MLX5_SET(cqc, cqc, uar_page, priv->cq_uar.index);
1243 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
1244 struct mlx5e_cq_param *param)
1246 void *cqc = param->cqc;
1249 switch (priv->params.rq_wq_type) {
1250 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1251 log_cq_size = priv->params.log_rq_size +
1252 priv->params.mpwqe_log_num_strides;
1254 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1255 log_cq_size = priv->params.log_rq_size;
1258 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
1259 if (priv->params.rx_cqe_compress) {
1260 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
1261 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
1264 mlx5e_build_common_cq_param(priv, param);
1267 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
1268 struct mlx5e_cq_param *param)
1270 void *cqc = param->cqc;
1272 MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_sq_size);
1274 mlx5e_build_common_cq_param(priv, param);
1277 static void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
1278 struct mlx5e_cq_param *param,
1281 void *cqc = param->cqc;
1283 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
1285 mlx5e_build_common_cq_param(priv, param);
1288 static void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
1289 struct mlx5e_sq_param *param,
1292 void *sqc = param->sqc;
1293 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1295 mlx5e_build_sq_param_common(priv, param);
1297 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
1298 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
1300 param->icosq = true;
1303 static void mlx5e_build_channel_param(struct mlx5e_priv *priv, struct mlx5e_channel_param *cparam)
1305 u8 icosq_log_wq_sz = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
1307 mlx5e_build_rq_param(priv, &cparam->rq);
1308 mlx5e_build_sq_param(priv, &cparam->sq);
1309 mlx5e_build_icosq_param(priv, &cparam->icosq, icosq_log_wq_sz);
1310 mlx5e_build_rx_cq_param(priv, &cparam->rx_cq);
1311 mlx5e_build_tx_cq_param(priv, &cparam->tx_cq);
1312 mlx5e_build_ico_cq_param(priv, &cparam->icosq_cq, icosq_log_wq_sz);
1315 static int mlx5e_open_channels(struct mlx5e_priv *priv)
1317 struct mlx5e_channel_param *cparam;
1318 int nch = priv->params.num_channels;
1323 priv->channel = kcalloc(nch, sizeof(struct mlx5e_channel *),
1326 priv->txq_to_sq_map = kcalloc(nch * priv->params.num_tc,
1327 sizeof(struct mlx5e_sq *), GFP_KERNEL);
1329 cparam = kzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
1331 if (!priv->channel || !priv->txq_to_sq_map || !cparam)
1332 goto err_free_txq_to_sq_map;
1334 mlx5e_build_channel_param(priv, cparam);
1336 for (i = 0; i < nch; i++) {
1337 err = mlx5e_open_channel(priv, i, cparam, &priv->channel[i]);
1339 goto err_close_channels;
1342 for (j = 0; j < nch; j++) {
1343 err = mlx5e_wait_for_min_rx_wqes(&priv->channel[j]->rq);
1345 goto err_close_channels;
1352 for (i--; i >= 0; i--)
1353 mlx5e_close_channel(priv->channel[i]);
1355 err_free_txq_to_sq_map:
1356 kfree(priv->txq_to_sq_map);
1357 kfree(priv->channel);
1363 static void mlx5e_close_channels(struct mlx5e_priv *priv)
1367 for (i = 0; i < priv->params.num_channels; i++)
1368 mlx5e_close_channel(priv->channel[i]);
1370 kfree(priv->txq_to_sq_map);
1371 kfree(priv->channel);
1374 static int mlx5e_rx_hash_fn(int hfunc)
1376 return (hfunc == ETH_RSS_HASH_TOP) ?
1377 MLX5_RX_HASH_FN_TOEPLITZ :
1378 MLX5_RX_HASH_FN_INVERTED_XOR8;
1381 static int mlx5e_bits_invert(unsigned long a, int size)
1386 for (i = 0; i < size; i++)
1387 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
1392 static void mlx5e_fill_indir_rqt_rqns(struct mlx5e_priv *priv, void *rqtc)
1396 for (i = 0; i < MLX5E_INDIR_RQT_SIZE; i++) {
1400 if (priv->params.rss_hfunc == ETH_RSS_HASH_XOR)
1401 ix = mlx5e_bits_invert(i, MLX5E_LOG_INDIR_RQT_SIZE);
1403 ix = priv->params.indirection_rqt[ix];
1404 rqn = test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1405 priv->channel[ix]->rq.rqn :
1407 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
1411 static void mlx5e_fill_direct_rqt_rqn(struct mlx5e_priv *priv, void *rqtc,
1414 u32 rqn = test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1415 priv->channel[ix]->rq.rqn :
1418 MLX5_SET(rqtc, rqtc, rq_num[0], rqn);
1421 static int mlx5e_create_rqt(struct mlx5e_priv *priv, int sz, int ix, u32 *rqtn)
1423 struct mlx5_core_dev *mdev = priv->mdev;
1429 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
1430 in = mlx5_vzalloc(inlen);
1434 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
1436 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1437 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
1439 if (sz > 1) /* RSS */
1440 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1442 mlx5e_fill_direct_rqt_rqn(priv, rqtc, ix);
1444 err = mlx5_core_create_rqt(mdev, in, inlen, rqtn);
1450 static void mlx5e_destroy_rqt(struct mlx5e_priv *priv, u32 rqtn)
1452 mlx5_core_destroy_rqt(priv->mdev, rqtn);
1455 static int mlx5e_create_rqts(struct mlx5e_priv *priv)
1457 int nch = mlx5e_get_max_num_channels(priv->mdev);
1463 rqtn = &priv->indir_rqtn;
1464 err = mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, 0, rqtn);
1469 for (ix = 0; ix < nch; ix++) {
1470 rqtn = &priv->direct_tir[ix].rqtn;
1471 err = mlx5e_create_rqt(priv, 1 /*size */, ix, rqtn);
1473 goto err_destroy_rqts;
1479 for (ix--; ix >= 0; ix--)
1480 mlx5e_destroy_rqt(priv, priv->direct_tir[ix].rqtn);
1482 mlx5e_destroy_rqt(priv, priv->indir_rqtn);
1487 static void mlx5e_destroy_rqts(struct mlx5e_priv *priv)
1489 int nch = mlx5e_get_max_num_channels(priv->mdev);
1492 for (i = 0; i < nch; i++)
1493 mlx5e_destroy_rqt(priv, priv->direct_tir[i].rqtn);
1495 mlx5e_destroy_rqt(priv, priv->indir_rqtn);
1498 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz, int ix)
1500 struct mlx5_core_dev *mdev = priv->mdev;
1506 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
1507 in = mlx5_vzalloc(inlen);
1511 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
1513 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1514 if (sz > 1) /* RSS */
1515 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1517 mlx5e_fill_direct_rqt_rqn(priv, rqtc, ix);
1519 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
1521 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
1528 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv)
1533 rqtn = priv->indir_rqtn;
1534 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, 0);
1535 for (ix = 0; ix < priv->params.num_channels; ix++) {
1536 rqtn = priv->direct_tir[ix].rqtn;
1537 mlx5e_redirect_rqt(priv, rqtn, 1, ix);
1541 static void mlx5e_build_tir_ctx_lro(void *tirc, struct mlx5e_priv *priv)
1543 if (!priv->params.lro_en)
1546 #define ROUGH_MAX_L2_L3_HDR_SZ 256
1548 MLX5_SET(tirc, tirc, lro_enable_mask,
1549 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
1550 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
1551 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
1552 (priv->params.lro_wqe_sz -
1553 ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
1554 MLX5_SET(tirc, tirc, lro_timeout_period_usecs,
1555 MLX5_CAP_ETH(priv->mdev,
1556 lro_timer_supported_periods[2]));
1559 void mlx5e_build_tir_ctx_hash(void *tirc, struct mlx5e_priv *priv)
1561 MLX5_SET(tirc, tirc, rx_hash_fn,
1562 mlx5e_rx_hash_fn(priv->params.rss_hfunc));
1563 if (priv->params.rss_hfunc == ETH_RSS_HASH_TOP) {
1564 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
1565 rx_hash_toeplitz_key);
1566 size_t len = MLX5_FLD_SZ_BYTES(tirc,
1567 rx_hash_toeplitz_key);
1569 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1570 memcpy(rss_key, priv->params.toeplitz_hash_key, len);
1574 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
1576 struct mlx5_core_dev *mdev = priv->mdev;
1585 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
1586 in = mlx5_vzalloc(inlen);
1590 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
1591 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
1593 mlx5e_build_tir_ctx_lro(tirc, priv);
1595 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
1596 err = mlx5_core_modify_tir(mdev, priv->indir_tirn[tt], in,
1602 for (ix = 0; ix < mlx5e_get_max_num_channels(mdev); ix++) {
1603 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
1615 static int mlx5e_refresh_tirs_self_loopback_enable(struct mlx5e_priv *priv)
1622 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
1623 in = mlx5_vzalloc(inlen);
1627 MLX5_SET(modify_tir_in, in, bitmask.self_lb_en, 1);
1629 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++) {
1630 err = mlx5_core_modify_tir(priv->mdev, priv->indir_tirn[i], in,
1636 for (i = 0; i < priv->params.num_channels; i++) {
1637 err = mlx5_core_modify_tir(priv->mdev,
1638 priv->direct_tir[i].tirn, in,
1649 static int mlx5e_set_mtu(struct mlx5e_priv *priv, u16 mtu)
1651 struct mlx5_core_dev *mdev = priv->mdev;
1652 u16 hw_mtu = MLX5E_SW2HW_MTU(mtu);
1655 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
1659 /* Update vport context MTU */
1660 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
1664 static void mlx5e_query_mtu(struct mlx5e_priv *priv, u16 *mtu)
1666 struct mlx5_core_dev *mdev = priv->mdev;
1670 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
1671 if (err || !hw_mtu) /* fallback to port oper mtu */
1672 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
1674 *mtu = MLX5E_HW2SW_MTU(hw_mtu);
1677 static int mlx5e_set_dev_port_mtu(struct net_device *netdev)
1679 struct mlx5e_priv *priv = netdev_priv(netdev);
1683 err = mlx5e_set_mtu(priv, netdev->mtu);
1687 mlx5e_query_mtu(priv, &mtu);
1688 if (mtu != netdev->mtu)
1689 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
1690 __func__, mtu, netdev->mtu);
1696 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
1698 struct mlx5e_priv *priv = netdev_priv(netdev);
1699 int nch = priv->params.num_channels;
1700 int ntc = priv->params.num_tc;
1703 netdev_reset_tc(netdev);
1708 netdev_set_num_tc(netdev, ntc);
1710 for (tc = 0; tc < ntc; tc++)
1711 netdev_set_tc_queue(netdev, tc, nch, tc * nch);
1714 int mlx5e_open_locked(struct net_device *netdev)
1716 struct mlx5e_priv *priv = netdev_priv(netdev);
1720 set_bit(MLX5E_STATE_OPENED, &priv->state);
1722 mlx5e_netdev_set_tcs(netdev);
1724 num_txqs = priv->params.num_channels * priv->params.num_tc;
1725 netif_set_real_num_tx_queues(netdev, num_txqs);
1726 netif_set_real_num_rx_queues(netdev, priv->params.num_channels);
1728 err = mlx5e_set_dev_port_mtu(netdev);
1730 goto err_clear_state_opened_flag;
1732 err = mlx5e_open_channels(priv);
1734 netdev_err(netdev, "%s: mlx5e_open_channels failed, %d\n",
1736 goto err_clear_state_opened_flag;
1739 err = mlx5e_refresh_tirs_self_loopback_enable(priv);
1741 netdev_err(netdev, "%s: mlx5e_refresh_tirs_self_loopback_enable failed, %d\n",
1743 goto err_close_channels;
1746 mlx5e_redirect_rqts(priv);
1747 mlx5e_update_carrier(priv);
1748 mlx5e_timestamp_init(priv);
1749 #ifdef CONFIG_RFS_ACCEL
1750 priv->netdev->rx_cpu_rmap = priv->mdev->rmap;
1753 queue_delayed_work(priv->wq, &priv->update_stats_work, 0);
1758 mlx5e_close_channels(priv);
1759 err_clear_state_opened_flag:
1760 clear_bit(MLX5E_STATE_OPENED, &priv->state);
1764 static int mlx5e_open(struct net_device *netdev)
1766 struct mlx5e_priv *priv = netdev_priv(netdev);
1769 mutex_lock(&priv->state_lock);
1770 err = mlx5e_open_locked(netdev);
1771 mutex_unlock(&priv->state_lock);
1776 int mlx5e_close_locked(struct net_device *netdev)
1778 struct mlx5e_priv *priv = netdev_priv(netdev);
1780 /* May already be CLOSED in case a previous configuration operation
1781 * (e.g RX/TX queue size change) that involves close&open failed.
1783 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
1786 clear_bit(MLX5E_STATE_OPENED, &priv->state);
1788 mlx5e_timestamp_cleanup(priv);
1789 netif_carrier_off(priv->netdev);
1790 mlx5e_redirect_rqts(priv);
1791 mlx5e_close_channels(priv);
1796 static int mlx5e_close(struct net_device *netdev)
1798 struct mlx5e_priv *priv = netdev_priv(netdev);
1801 mutex_lock(&priv->state_lock);
1802 err = mlx5e_close_locked(netdev);
1803 mutex_unlock(&priv->state_lock);
1808 static int mlx5e_create_drop_rq(struct mlx5e_priv *priv,
1809 struct mlx5e_rq *rq,
1810 struct mlx5e_rq_param *param)
1812 struct mlx5_core_dev *mdev = priv->mdev;
1813 void *rqc = param->rqc;
1814 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
1817 param->wq.db_numa_node = param->wq.buf_numa_node;
1819 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
1829 static int mlx5e_create_drop_cq(struct mlx5e_priv *priv,
1830 struct mlx5e_cq *cq,
1831 struct mlx5e_cq_param *param)
1833 struct mlx5_core_dev *mdev = priv->mdev;
1834 struct mlx5_core_cq *mcq = &cq->mcq;
1839 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1844 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1847 mcq->set_ci_db = cq->wq_ctrl.db.db;
1848 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1849 *mcq->set_ci_db = 0;
1851 mcq->vector = param->eq_ix;
1852 mcq->comp = mlx5e_completion_event;
1853 mcq->event = mlx5e_cq_error_event;
1855 mcq->uar = &priv->cq_uar;
1862 static int mlx5e_open_drop_rq(struct mlx5e_priv *priv)
1864 struct mlx5e_cq_param cq_param;
1865 struct mlx5e_rq_param rq_param;
1866 struct mlx5e_rq *rq = &priv->drop_rq;
1867 struct mlx5e_cq *cq = &priv->drop_rq.cq;
1870 memset(&cq_param, 0, sizeof(cq_param));
1871 memset(&rq_param, 0, sizeof(rq_param));
1872 mlx5e_build_drop_rq_param(&rq_param);
1874 err = mlx5e_create_drop_cq(priv, cq, &cq_param);
1878 err = mlx5e_enable_cq(cq, &cq_param);
1880 goto err_destroy_cq;
1882 err = mlx5e_create_drop_rq(priv, rq, &rq_param);
1884 goto err_disable_cq;
1886 err = mlx5e_enable_rq(rq, &rq_param);
1888 goto err_destroy_rq;
1893 mlx5e_destroy_rq(&priv->drop_rq);
1896 mlx5e_disable_cq(&priv->drop_rq.cq);
1899 mlx5e_destroy_cq(&priv->drop_rq.cq);
1904 static void mlx5e_close_drop_rq(struct mlx5e_priv *priv)
1906 mlx5e_disable_rq(&priv->drop_rq);
1907 mlx5e_destroy_rq(&priv->drop_rq);
1908 mlx5e_disable_cq(&priv->drop_rq.cq);
1909 mlx5e_destroy_cq(&priv->drop_rq.cq);
1912 static int mlx5e_create_tis(struct mlx5e_priv *priv, int tc)
1914 struct mlx5_core_dev *mdev = priv->mdev;
1915 u32 in[MLX5_ST_SZ_DW(create_tis_in)];
1916 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1918 memset(in, 0, sizeof(in));
1920 MLX5_SET(tisc, tisc, prio, tc << 1);
1921 MLX5_SET(tisc, tisc, transport_domain, priv->tdn);
1923 return mlx5_core_create_tis(mdev, in, sizeof(in), &priv->tisn[tc]);
1926 static void mlx5e_destroy_tis(struct mlx5e_priv *priv, int tc)
1928 mlx5_core_destroy_tis(priv->mdev, priv->tisn[tc]);
1931 static int mlx5e_create_tises(struct mlx5e_priv *priv)
1936 for (tc = 0; tc < MLX5E_MAX_NUM_TC; tc++) {
1937 err = mlx5e_create_tis(priv, tc);
1939 goto err_close_tises;
1945 for (tc--; tc >= 0; tc--)
1946 mlx5e_destroy_tis(priv, tc);
1951 static void mlx5e_destroy_tises(struct mlx5e_priv *priv)
1955 for (tc = 0; tc < MLX5E_MAX_NUM_TC; tc++)
1956 mlx5e_destroy_tis(priv, tc);
1959 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv, u32 *tirc,
1960 enum mlx5e_traffic_types tt)
1962 void *hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1964 MLX5_SET(tirc, tirc, transport_domain, priv->tdn);
1966 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
1967 MLX5_HASH_FIELD_SEL_DST_IP)
1969 #define MLX5_HASH_IP_L4PORTS (MLX5_HASH_FIELD_SEL_SRC_IP |\
1970 MLX5_HASH_FIELD_SEL_DST_IP |\
1971 MLX5_HASH_FIELD_SEL_L4_SPORT |\
1972 MLX5_HASH_FIELD_SEL_L4_DPORT)
1974 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
1975 MLX5_HASH_FIELD_SEL_DST_IP |\
1976 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
1978 mlx5e_build_tir_ctx_lro(tirc, priv);
1980 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
1981 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqtn);
1982 mlx5e_build_tir_ctx_hash(tirc, priv);
1985 case MLX5E_TT_IPV4_TCP:
1986 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1987 MLX5_L3_PROT_TYPE_IPV4);
1988 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1989 MLX5_L4_PROT_TYPE_TCP);
1990 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1991 MLX5_HASH_IP_L4PORTS);
1994 case MLX5E_TT_IPV6_TCP:
1995 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1996 MLX5_L3_PROT_TYPE_IPV6);
1997 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1998 MLX5_L4_PROT_TYPE_TCP);
1999 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2000 MLX5_HASH_IP_L4PORTS);
2003 case MLX5E_TT_IPV4_UDP:
2004 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2005 MLX5_L3_PROT_TYPE_IPV4);
2006 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2007 MLX5_L4_PROT_TYPE_UDP);
2008 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2009 MLX5_HASH_IP_L4PORTS);
2012 case MLX5E_TT_IPV6_UDP:
2013 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2014 MLX5_L3_PROT_TYPE_IPV6);
2015 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2016 MLX5_L4_PROT_TYPE_UDP);
2017 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2018 MLX5_HASH_IP_L4PORTS);
2021 case MLX5E_TT_IPV4_IPSEC_AH:
2022 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2023 MLX5_L3_PROT_TYPE_IPV4);
2024 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2025 MLX5_HASH_IP_IPSEC_SPI);
2028 case MLX5E_TT_IPV6_IPSEC_AH:
2029 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2030 MLX5_L3_PROT_TYPE_IPV6);
2031 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2032 MLX5_HASH_IP_IPSEC_SPI);
2035 case MLX5E_TT_IPV4_IPSEC_ESP:
2036 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2037 MLX5_L3_PROT_TYPE_IPV4);
2038 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2039 MLX5_HASH_IP_IPSEC_SPI);
2042 case MLX5E_TT_IPV6_IPSEC_ESP:
2043 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2044 MLX5_L3_PROT_TYPE_IPV6);
2045 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2046 MLX5_HASH_IP_IPSEC_SPI);
2050 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2051 MLX5_L3_PROT_TYPE_IPV4);
2052 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2057 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2058 MLX5_L3_PROT_TYPE_IPV6);
2059 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2064 "mlx5e_build_indir_tir_ctx: bad traffic type!\n");
2068 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 *tirc,
2071 MLX5_SET(tirc, tirc, transport_domain, priv->tdn);
2073 mlx5e_build_tir_ctx_lro(tirc, priv);
2075 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2076 MLX5_SET(tirc, tirc, indirect_table, rqtn);
2077 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
2080 static int mlx5e_create_tirs(struct mlx5e_priv *priv)
2082 int nch = mlx5e_get_max_num_channels(priv->mdev);
2091 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2092 in = mlx5_vzalloc(inlen);
2097 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2098 memset(in, 0, inlen);
2099 tirn = &priv->indir_tirn[tt];
2100 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2101 mlx5e_build_indir_tir_ctx(priv, tirc, tt);
2102 err = mlx5_core_create_tir(priv->mdev, in, inlen, tirn);
2104 goto err_destroy_tirs;
2108 for (ix = 0; ix < nch; ix++) {
2109 memset(in, 0, inlen);
2110 tirn = &priv->direct_tir[ix].tirn;
2111 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2112 mlx5e_build_direct_tir_ctx(priv, tirc,
2113 priv->direct_tir[ix].rqtn);
2114 err = mlx5_core_create_tir(priv->mdev, in, inlen, tirn);
2116 goto err_destroy_ch_tirs;
2123 err_destroy_ch_tirs:
2124 for (ix--; ix >= 0; ix--)
2125 mlx5_core_destroy_tir(priv->mdev, priv->direct_tir[ix].tirn);
2128 for (tt--; tt >= 0; tt--)
2129 mlx5_core_destroy_tir(priv->mdev, priv->indir_tirn[tt]);
2136 static void mlx5e_destroy_tirs(struct mlx5e_priv *priv)
2138 int nch = mlx5e_get_max_num_channels(priv->mdev);
2141 for (i = 0; i < nch; i++)
2142 mlx5_core_destroy_tir(priv->mdev, priv->direct_tir[i].tirn);
2144 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
2145 mlx5_core_destroy_tir(priv->mdev, priv->indir_tirn[i]);
2148 int mlx5e_modify_rqs_vsd(struct mlx5e_priv *priv, bool vsd)
2153 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
2156 for (i = 0; i < priv->params.num_channels; i++) {
2157 err = mlx5e_modify_rq_vsd(&priv->channel[i]->rq, vsd);
2165 static int mlx5e_setup_tc(struct net_device *netdev, u8 tc)
2167 struct mlx5e_priv *priv = netdev_priv(netdev);
2171 if (tc && tc != MLX5E_MAX_NUM_TC)
2174 mutex_lock(&priv->state_lock);
2176 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2178 mlx5e_close_locked(priv->netdev);
2180 priv->params.num_tc = tc ? tc : 1;
2183 err = mlx5e_open_locked(priv->netdev);
2185 mutex_unlock(&priv->state_lock);
2190 static int mlx5e_ndo_setup_tc(struct net_device *dev, u32 handle,
2191 __be16 proto, struct tc_to_netdev *tc)
2193 struct mlx5e_priv *priv = netdev_priv(dev);
2195 if (TC_H_MAJ(handle) != TC_H_MAJ(TC_H_INGRESS))
2199 case TC_SETUP_CLSFLOWER:
2200 switch (tc->cls_flower->command) {
2201 case TC_CLSFLOWER_REPLACE:
2202 return mlx5e_configure_flower(priv, proto, tc->cls_flower);
2203 case TC_CLSFLOWER_DESTROY:
2204 return mlx5e_delete_flower(priv, tc->cls_flower);
2205 case TC_CLSFLOWER_STATS:
2206 return mlx5e_stats_flower(priv, tc->cls_flower);
2213 if (tc->type != TC_SETUP_MQPRIO)
2216 return mlx5e_setup_tc(dev, tc->tc);
2219 static struct rtnl_link_stats64 *
2220 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
2222 struct mlx5e_priv *priv = netdev_priv(dev);
2223 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
2224 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
2225 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
2227 stats->rx_packets = sstats->rx_packets;
2228 stats->rx_bytes = sstats->rx_bytes;
2229 stats->tx_packets = sstats->tx_packets;
2230 stats->tx_bytes = sstats->tx_bytes;
2232 stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
2233 stats->tx_dropped = sstats->tx_queue_dropped;
2235 stats->rx_length_errors =
2236 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
2237 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
2238 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
2239 stats->rx_crc_errors =
2240 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
2241 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
2242 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
2243 stats->tx_carrier_errors =
2244 PPORT_802_3_GET(pstats, a_symbol_error_during_carrier);
2245 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
2246 stats->rx_frame_errors;
2247 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
2249 /* vport multicast also counts packets that are dropped due to steering
2250 * or rx out of buffer
2253 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
2258 static void mlx5e_set_rx_mode(struct net_device *dev)
2260 struct mlx5e_priv *priv = netdev_priv(dev);
2262 queue_work(priv->wq, &priv->set_rx_mode_work);
2265 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
2267 struct mlx5e_priv *priv = netdev_priv(netdev);
2268 struct sockaddr *saddr = addr;
2270 if (!is_valid_ether_addr(saddr->sa_data))
2271 return -EADDRNOTAVAIL;
2273 netif_addr_lock_bh(netdev);
2274 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
2275 netif_addr_unlock_bh(netdev);
2277 queue_work(priv->wq, &priv->set_rx_mode_work);
2282 #define MLX5E_SET_FEATURE(netdev, feature, enable) \
2285 netdev->features |= feature; \
2287 netdev->features &= ~feature; \
2290 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
2292 static int set_feature_lro(struct net_device *netdev, bool enable)
2294 struct mlx5e_priv *priv = netdev_priv(netdev);
2295 bool was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2298 mutex_lock(&priv->state_lock);
2300 if (was_opened && (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST))
2301 mlx5e_close_locked(priv->netdev);
2303 priv->params.lro_en = enable;
2304 err = mlx5e_modify_tirs_lro(priv);
2306 netdev_err(netdev, "lro modify failed, %d\n", err);
2307 priv->params.lro_en = !enable;
2310 if (was_opened && (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST))
2311 mlx5e_open_locked(priv->netdev);
2313 mutex_unlock(&priv->state_lock);
2318 static int set_feature_vlan_filter(struct net_device *netdev, bool enable)
2320 struct mlx5e_priv *priv = netdev_priv(netdev);
2323 mlx5e_enable_vlan_filter(priv);
2325 mlx5e_disable_vlan_filter(priv);
2330 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
2332 struct mlx5e_priv *priv = netdev_priv(netdev);
2334 if (!enable && mlx5e_tc_num_filters(priv)) {
2336 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
2343 static int set_feature_rx_all(struct net_device *netdev, bool enable)
2345 struct mlx5e_priv *priv = netdev_priv(netdev);
2346 struct mlx5_core_dev *mdev = priv->mdev;
2348 return mlx5_set_port_fcs(mdev, !enable);
2351 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
2353 struct mlx5e_priv *priv = netdev_priv(netdev);
2356 mutex_lock(&priv->state_lock);
2358 priv->params.vlan_strip_disable = !enable;
2359 err = mlx5e_modify_rqs_vsd(priv, !enable);
2361 priv->params.vlan_strip_disable = enable;
2363 mutex_unlock(&priv->state_lock);
2368 #ifdef CONFIG_RFS_ACCEL
2369 static int set_feature_arfs(struct net_device *netdev, bool enable)
2371 struct mlx5e_priv *priv = netdev_priv(netdev);
2375 err = mlx5e_arfs_enable(priv);
2377 err = mlx5e_arfs_disable(priv);
2383 static int mlx5e_handle_feature(struct net_device *netdev,
2384 netdev_features_t wanted_features,
2385 netdev_features_t feature,
2386 mlx5e_feature_handler feature_handler)
2388 netdev_features_t changes = wanted_features ^ netdev->features;
2389 bool enable = !!(wanted_features & feature);
2392 if (!(changes & feature))
2395 err = feature_handler(netdev, enable);
2397 netdev_err(netdev, "%s feature 0x%llx failed err %d\n",
2398 enable ? "Enable" : "Disable", feature, err);
2402 MLX5E_SET_FEATURE(netdev, feature, enable);
2406 static int mlx5e_set_features(struct net_device *netdev,
2407 netdev_features_t features)
2411 err = mlx5e_handle_feature(netdev, features, NETIF_F_LRO,
2413 err |= mlx5e_handle_feature(netdev, features,
2414 NETIF_F_HW_VLAN_CTAG_FILTER,
2415 set_feature_vlan_filter);
2416 err |= mlx5e_handle_feature(netdev, features, NETIF_F_HW_TC,
2417 set_feature_tc_num_filters);
2418 err |= mlx5e_handle_feature(netdev, features, NETIF_F_RXALL,
2419 set_feature_rx_all);
2420 err |= mlx5e_handle_feature(netdev, features, NETIF_F_HW_VLAN_CTAG_RX,
2421 set_feature_rx_vlan);
2422 #ifdef CONFIG_RFS_ACCEL
2423 err |= mlx5e_handle_feature(netdev, features, NETIF_F_NTUPLE,
2427 return err ? -EINVAL : 0;
2430 #define MXL5_HW_MIN_MTU 64
2431 #define MXL5E_MIN_MTU (MXL5_HW_MIN_MTU + ETH_FCS_LEN)
2433 static int mlx5e_change_mtu(struct net_device *netdev, int new_mtu)
2435 struct mlx5e_priv *priv = netdev_priv(netdev);
2436 struct mlx5_core_dev *mdev = priv->mdev;
2442 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
2444 max_mtu = MLX5E_HW2SW_MTU(max_mtu);
2445 min_mtu = MLX5E_HW2SW_MTU(MXL5E_MIN_MTU);
2447 if (new_mtu > max_mtu || new_mtu < min_mtu) {
2449 "%s: Bad MTU (%d), valid range is: [%d..%d]\n",
2450 __func__, new_mtu, min_mtu, max_mtu);
2454 mutex_lock(&priv->state_lock);
2456 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2458 mlx5e_close_locked(netdev);
2460 netdev->mtu = new_mtu;
2463 err = mlx5e_open_locked(netdev);
2465 mutex_unlock(&priv->state_lock);
2470 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2474 return mlx5e_hwstamp_set(dev, ifr);
2476 return mlx5e_hwstamp_get(dev, ifr);
2482 static int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2484 struct mlx5e_priv *priv = netdev_priv(dev);
2485 struct mlx5_core_dev *mdev = priv->mdev;
2487 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
2490 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos)
2492 struct mlx5e_priv *priv = netdev_priv(dev);
2493 struct mlx5_core_dev *mdev = priv->mdev;
2495 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
2499 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2501 struct mlx5e_priv *priv = netdev_priv(dev);
2502 struct mlx5_core_dev *mdev = priv->mdev;
2504 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
2507 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
2509 struct mlx5e_priv *priv = netdev_priv(dev);
2510 struct mlx5_core_dev *mdev = priv->mdev;
2512 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
2514 static int mlx5_vport_link2ifla(u8 esw_link)
2517 case MLX5_ESW_VPORT_ADMIN_STATE_DOWN:
2518 return IFLA_VF_LINK_STATE_DISABLE;
2519 case MLX5_ESW_VPORT_ADMIN_STATE_UP:
2520 return IFLA_VF_LINK_STATE_ENABLE;
2522 return IFLA_VF_LINK_STATE_AUTO;
2525 static int mlx5_ifla_link2vport(u8 ifla_link)
2527 switch (ifla_link) {
2528 case IFLA_VF_LINK_STATE_DISABLE:
2529 return MLX5_ESW_VPORT_ADMIN_STATE_DOWN;
2530 case IFLA_VF_LINK_STATE_ENABLE:
2531 return MLX5_ESW_VPORT_ADMIN_STATE_UP;
2533 return MLX5_ESW_VPORT_ADMIN_STATE_AUTO;
2536 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
2539 struct mlx5e_priv *priv = netdev_priv(dev);
2540 struct mlx5_core_dev *mdev = priv->mdev;
2542 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
2543 mlx5_ifla_link2vport(link_state));
2546 static int mlx5e_get_vf_config(struct net_device *dev,
2547 int vf, struct ifla_vf_info *ivi)
2549 struct mlx5e_priv *priv = netdev_priv(dev);
2550 struct mlx5_core_dev *mdev = priv->mdev;
2553 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
2556 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
2560 static int mlx5e_get_vf_stats(struct net_device *dev,
2561 int vf, struct ifla_vf_stats *vf_stats)
2563 struct mlx5e_priv *priv = netdev_priv(dev);
2564 struct mlx5_core_dev *mdev = priv->mdev;
2566 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
2570 static void mlx5e_add_vxlan_port(struct net_device *netdev,
2571 sa_family_t sa_family, __be16 port)
2573 struct mlx5e_priv *priv = netdev_priv(netdev);
2575 if (!mlx5e_vxlan_allowed(priv->mdev))
2578 mlx5e_vxlan_queue_work(priv, sa_family, be16_to_cpu(port), 1);
2581 static void mlx5e_del_vxlan_port(struct net_device *netdev,
2582 sa_family_t sa_family, __be16 port)
2584 struct mlx5e_priv *priv = netdev_priv(netdev);
2586 if (!mlx5e_vxlan_allowed(priv->mdev))
2589 mlx5e_vxlan_queue_work(priv, sa_family, be16_to_cpu(port), 0);
2592 static netdev_features_t mlx5e_vxlan_features_check(struct mlx5e_priv *priv,
2593 struct sk_buff *skb,
2594 netdev_features_t features)
2596 struct udphdr *udph;
2600 switch (vlan_get_protocol(skb)) {
2601 case htons(ETH_P_IP):
2602 proto = ip_hdr(skb)->protocol;
2604 case htons(ETH_P_IPV6):
2605 proto = ipv6_hdr(skb)->nexthdr;
2611 if (proto == IPPROTO_UDP) {
2612 udph = udp_hdr(skb);
2613 port = be16_to_cpu(udph->dest);
2616 /* Verify if UDP port is being offloaded by HW */
2617 if (port && mlx5e_vxlan_lookup_port(priv, port))
2621 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
2622 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2625 static netdev_features_t mlx5e_features_check(struct sk_buff *skb,
2626 struct net_device *netdev,
2627 netdev_features_t features)
2629 struct mlx5e_priv *priv = netdev_priv(netdev);
2631 features = vlan_features_check(skb, features);
2632 features = vxlan_features_check(skb, features);
2634 /* Validate if the tunneled packet is being offloaded by HW */
2635 if (skb->encapsulation &&
2636 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
2637 return mlx5e_vxlan_features_check(priv, skb, features);
2642 static void mlx5e_tx_timeout(struct net_device *dev)
2644 struct mlx5e_priv *priv = netdev_priv(dev);
2645 bool sched_work = false;
2648 netdev_err(dev, "TX timeout detected\n");
2650 for (i = 0; i < priv->params.num_channels * priv->params.num_tc; i++) {
2651 struct mlx5e_sq *sq = priv->txq_to_sq_map[i];
2653 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, i)))
2656 set_bit(MLX5E_SQ_STATE_TX_TIMEOUT, &sq->state);
2657 netdev_err(dev, "TX timeout on queue: %d, SQ: 0x%x, CQ: 0x%x, SQ Cons: 0x%x SQ Prod: 0x%x\n",
2658 i, sq->sqn, sq->cq.mcq.cqn, sq->cc, sq->pc);
2661 if (sched_work && test_bit(MLX5E_STATE_OPENED, &priv->state))
2662 schedule_work(&priv->tx_timeout_work);
2665 static const struct net_device_ops mlx5e_netdev_ops_basic = {
2666 .ndo_open = mlx5e_open,
2667 .ndo_stop = mlx5e_close,
2668 .ndo_start_xmit = mlx5e_xmit,
2669 .ndo_setup_tc = mlx5e_ndo_setup_tc,
2670 .ndo_select_queue = mlx5e_select_queue,
2671 .ndo_get_stats64 = mlx5e_get_stats,
2672 .ndo_set_rx_mode = mlx5e_set_rx_mode,
2673 .ndo_set_mac_address = mlx5e_set_mac,
2674 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
2675 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
2676 .ndo_set_features = mlx5e_set_features,
2677 .ndo_change_mtu = mlx5e_change_mtu,
2678 .ndo_do_ioctl = mlx5e_ioctl,
2679 #ifdef CONFIG_RFS_ACCEL
2680 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
2682 .ndo_tx_timeout = mlx5e_tx_timeout,
2685 static const struct net_device_ops mlx5e_netdev_ops_sriov = {
2686 .ndo_open = mlx5e_open,
2687 .ndo_stop = mlx5e_close,
2688 .ndo_start_xmit = mlx5e_xmit,
2689 .ndo_setup_tc = mlx5e_ndo_setup_tc,
2690 .ndo_select_queue = mlx5e_select_queue,
2691 .ndo_get_stats64 = mlx5e_get_stats,
2692 .ndo_set_rx_mode = mlx5e_set_rx_mode,
2693 .ndo_set_mac_address = mlx5e_set_mac,
2694 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
2695 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
2696 .ndo_set_features = mlx5e_set_features,
2697 .ndo_change_mtu = mlx5e_change_mtu,
2698 .ndo_do_ioctl = mlx5e_ioctl,
2699 .ndo_add_vxlan_port = mlx5e_add_vxlan_port,
2700 .ndo_del_vxlan_port = mlx5e_del_vxlan_port,
2701 .ndo_features_check = mlx5e_features_check,
2702 #ifdef CONFIG_RFS_ACCEL
2703 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
2705 .ndo_set_vf_mac = mlx5e_set_vf_mac,
2706 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
2707 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
2708 .ndo_set_vf_trust = mlx5e_set_vf_trust,
2709 .ndo_get_vf_config = mlx5e_get_vf_config,
2710 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
2711 .ndo_get_vf_stats = mlx5e_get_vf_stats,
2712 .ndo_tx_timeout = mlx5e_tx_timeout,
2715 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
2717 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
2719 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
2720 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
2721 !MLX5_CAP_ETH(mdev, csum_cap) ||
2722 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
2723 !MLX5_CAP_ETH(mdev, vlan_cap) ||
2724 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
2725 MLX5_CAP_FLOWTABLE(mdev,
2726 flow_table_properties_nic_receive.max_ft_level)
2728 mlx5_core_warn(mdev,
2729 "Not creating net device, some required device capabilities are missing\n");
2732 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
2733 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
2734 if (!MLX5_CAP_GEN(mdev, cq_moderation))
2735 mlx5_core_warn(mdev, "CQ modiration is not supported\n");
2740 u16 mlx5e_get_max_inline_cap(struct mlx5_core_dev *mdev)
2742 int bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
2744 return bf_buf_size -
2745 sizeof(struct mlx5e_tx_wqe) +
2746 2 /*sizeof(mlx5e_tx_wqe.inline_hdr_start)*/;
2749 #ifdef CONFIG_MLX5_CORE_EN_DCB
2750 static void mlx5e_ets_init(struct mlx5e_priv *priv)
2754 priv->params.ets.ets_cap = mlx5_max_tc(priv->mdev) + 1;
2755 for (i = 0; i < priv->params.ets.ets_cap; i++) {
2756 priv->params.ets.tc_tx_bw[i] = MLX5E_MAX_BW_ALLOC;
2757 priv->params.ets.tc_tsa[i] = IEEE_8021QAZ_TSA_VENDOR;
2758 priv->params.ets.prio_tc[i] = i;
2761 /* tclass[prio=0]=1, tclass[prio=1]=0, tclass[prio=i]=i (for i>1) */
2762 priv->params.ets.prio_tc[0] = 1;
2763 priv->params.ets.prio_tc[1] = 0;
2767 void mlx5e_build_default_indir_rqt(struct mlx5_core_dev *mdev,
2768 u32 *indirection_rqt, int len,
2771 int node = mdev->priv.numa_node;
2772 int node_num_of_cores;
2776 node = first_online_node;
2778 node_num_of_cores = cpumask_weight(cpumask_of_node(node));
2780 if (node_num_of_cores)
2781 num_channels = min_t(int, num_channels, node_num_of_cores);
2783 for (i = 0; i < len; i++)
2784 indirection_rqt[i] = i % num_channels;
2787 static bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
2789 return MLX5_CAP_GEN(mdev, striding_rq) &&
2790 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
2791 MLX5_CAP_ETH(mdev, reg_umr_sq);
2794 static int mlx5e_get_pci_bw(struct mlx5_core_dev *mdev, u32 *pci_bw)
2796 enum pcie_link_width width;
2797 enum pci_bus_speed speed;
2800 err = pcie_get_minimum_link(mdev->pdev, &speed, &width);
2804 if (speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
2808 case PCIE_SPEED_2_5GT:
2809 *pci_bw = 2500 * width;
2811 case PCIE_SPEED_5_0GT:
2812 *pci_bw = 5000 * width;
2814 case PCIE_SPEED_8_0GT:
2815 *pci_bw = 8000 * width;
2824 static bool cqe_compress_heuristic(u32 link_speed, u32 pci_bw)
2826 return (link_speed && pci_bw &&
2827 (pci_bw < 40000) && (pci_bw < link_speed));
2830 static void mlx5e_build_netdev_priv(struct mlx5_core_dev *mdev,
2831 struct net_device *netdev,
2834 struct mlx5e_priv *priv = netdev_priv(netdev);
2838 priv->params.log_sq_size =
2839 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
2840 priv->params.rq_wq_type = mlx5e_check_fragmented_striding_rq_cap(mdev) ?
2841 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
2842 MLX5_WQ_TYPE_LINKED_LIST;
2844 /* set CQE compression */
2845 priv->params.rx_cqe_compress_admin = false;
2846 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
2847 MLX5_CAP_GEN(mdev, vport_group_manager)) {
2848 mlx5e_get_max_linkspeed(mdev, &link_speed);
2849 mlx5e_get_pci_bw(mdev, &pci_bw);
2850 mlx5_core_dbg(mdev, "Max link speed = %d, PCI BW = %d\n",
2851 link_speed, pci_bw);
2852 priv->params.rx_cqe_compress_admin =
2853 cqe_compress_heuristic(link_speed, pci_bw);
2856 priv->params.rx_cqe_compress = priv->params.rx_cqe_compress_admin;
2858 switch (priv->params.rq_wq_type) {
2859 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2860 priv->params.log_rq_size = MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE_MPW;
2861 priv->params.mpwqe_log_stride_sz =
2862 priv->params.rx_cqe_compress ?
2863 MLX5_MPWRQ_LOG_STRIDE_SIZE_CQE_COMPRESS :
2864 MLX5_MPWRQ_LOG_STRIDE_SIZE;
2865 priv->params.mpwqe_log_num_strides = MLX5_MPWRQ_LOG_WQE_SZ -
2866 priv->params.mpwqe_log_stride_sz;
2867 priv->params.lro_en = true;
2869 default: /* MLX5_WQ_TYPE_LINKED_LIST */
2870 priv->params.log_rq_size = MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
2873 mlx5_core_info(mdev,
2874 "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
2875 priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
2876 BIT(priv->params.log_rq_size),
2877 BIT(priv->params.mpwqe_log_stride_sz),
2878 priv->params.rx_cqe_compress_admin);
2880 priv->params.min_rx_wqes = mlx5_min_rx_wqes(priv->params.rq_wq_type,
2881 BIT(priv->params.log_rq_size));
2882 priv->params.rx_cq_moderation_usec =
2883 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
2884 priv->params.rx_cq_moderation_pkts =
2885 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
2886 priv->params.tx_cq_moderation_usec =
2887 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
2888 priv->params.tx_cq_moderation_pkts =
2889 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
2890 priv->params.tx_max_inline = mlx5e_get_max_inline_cap(mdev);
2891 priv->params.num_tc = 1;
2892 priv->params.rss_hfunc = ETH_RSS_HASH_XOR;
2894 netdev_rss_key_fill(priv->params.toeplitz_hash_key,
2895 sizeof(priv->params.toeplitz_hash_key));
2897 mlx5e_build_default_indir_rqt(mdev, priv->params.indirection_rqt,
2898 MLX5E_INDIR_RQT_SIZE, num_channels);
2900 priv->params.lro_wqe_sz =
2901 MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
2904 priv->netdev = netdev;
2905 priv->params.num_channels = num_channels;
2907 #ifdef CONFIG_MLX5_CORE_EN_DCB
2908 mlx5e_ets_init(priv);
2911 mutex_init(&priv->state_lock);
2913 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
2914 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
2915 INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work);
2916 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
2919 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
2921 struct mlx5e_priv *priv = netdev_priv(netdev);
2923 mlx5_query_nic_vport_mac_address(priv->mdev, 0, netdev->dev_addr);
2924 if (is_zero_ether_addr(netdev->dev_addr) &&
2925 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
2926 eth_hw_addr_random(netdev);
2927 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
2931 static void mlx5e_build_netdev(struct net_device *netdev)
2933 struct mlx5e_priv *priv = netdev_priv(netdev);
2934 struct mlx5_core_dev *mdev = priv->mdev;
2938 SET_NETDEV_DEV(netdev, &mdev->pdev->dev);
2940 if (MLX5_CAP_GEN(mdev, vport_group_manager)) {
2941 netdev->netdev_ops = &mlx5e_netdev_ops_sriov;
2942 #ifdef CONFIG_MLX5_CORE_EN_DCB
2943 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
2946 netdev->netdev_ops = &mlx5e_netdev_ops_basic;
2949 netdev->watchdog_timeo = 15 * HZ;
2951 netdev->ethtool_ops = &mlx5e_ethtool_ops;
2953 netdev->vlan_features |= NETIF_F_SG;
2954 netdev->vlan_features |= NETIF_F_IP_CSUM;
2955 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
2956 netdev->vlan_features |= NETIF_F_GRO;
2957 netdev->vlan_features |= NETIF_F_TSO;
2958 netdev->vlan_features |= NETIF_F_TSO6;
2959 netdev->vlan_features |= NETIF_F_RXCSUM;
2960 netdev->vlan_features |= NETIF_F_RXHASH;
2962 if (!!MLX5_CAP_ETH(mdev, lro_cap))
2963 netdev->vlan_features |= NETIF_F_LRO;
2965 netdev->hw_features = netdev->vlan_features;
2966 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
2967 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
2968 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2970 if (mlx5e_vxlan_allowed(mdev)) {
2971 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
2972 NETIF_F_GSO_UDP_TUNNEL_CSUM |
2973 NETIF_F_GSO_PARTIAL;
2974 netdev->hw_enc_features |= NETIF_F_IP_CSUM;
2975 netdev->hw_enc_features |= NETIF_F_IPV6_CSUM;
2976 netdev->hw_enc_features |= NETIF_F_TSO;
2977 netdev->hw_enc_features |= NETIF_F_TSO6;
2978 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL;
2979 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
2980 NETIF_F_GSO_PARTIAL;
2981 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
2984 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
2987 netdev->hw_features |= NETIF_F_RXALL;
2989 netdev->features = netdev->hw_features;
2990 if (!priv->params.lro_en)
2991 netdev->features &= ~NETIF_F_LRO;
2994 netdev->features &= ~NETIF_F_RXALL;
2996 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
2997 if (FT_CAP(flow_modify_en) &&
2998 FT_CAP(modify_root) &&
2999 FT_CAP(identified_miss_table_mode) &&
3000 FT_CAP(flow_table_modify)) {
3001 netdev->hw_features |= NETIF_F_HW_TC;
3002 #ifdef CONFIG_RFS_ACCEL
3003 netdev->hw_features |= NETIF_F_NTUPLE;
3007 netdev->features |= NETIF_F_HIGHDMA;
3009 netdev->priv_flags |= IFF_UNICAST_FLT;
3011 mlx5e_set_netdev_dev_addr(netdev);
3014 static int mlx5e_create_mkey(struct mlx5e_priv *priv, u32 pdn,
3015 struct mlx5_core_mkey *mkey)
3017 struct mlx5_core_dev *mdev = priv->mdev;
3018 struct mlx5_create_mkey_mbox_in *in;
3021 in = mlx5_vzalloc(sizeof(*in));
3025 in->seg.flags = MLX5_PERM_LOCAL_WRITE |
3026 MLX5_PERM_LOCAL_READ |
3027 MLX5_ACCESS_MODE_PA;
3028 in->seg.flags_pd = cpu_to_be32(pdn | MLX5_MKEY_LEN64);
3029 in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
3031 err = mlx5_core_create_mkey(mdev, mkey, in, sizeof(*in), NULL, NULL,
3039 static void mlx5e_create_q_counter(struct mlx5e_priv *priv)
3041 struct mlx5_core_dev *mdev = priv->mdev;
3044 err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
3046 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
3047 priv->q_counter = 0;
3051 static void mlx5e_destroy_q_counter(struct mlx5e_priv *priv)
3053 if (!priv->q_counter)
3056 mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
3059 static int mlx5e_create_umr_mkey(struct mlx5e_priv *priv)
3061 struct mlx5_core_dev *mdev = priv->mdev;
3062 struct mlx5_create_mkey_mbox_in *in;
3063 struct mlx5_mkey_seg *mkc;
3064 int inlen = sizeof(*in);
3066 mlx5e_get_max_num_channels(mdev) * MLX5_CHANNEL_MAX_NUM_MTTS;
3069 in = mlx5_vzalloc(inlen);
3074 mkc->status = MLX5_MKEY_STATUS_FREE;
3075 mkc->flags = MLX5_PERM_UMR_EN |
3076 MLX5_PERM_LOCAL_READ |
3077 MLX5_PERM_LOCAL_WRITE |
3078 MLX5_ACCESS_MODE_MTT;
3080 mkc->qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
3081 mkc->flags_pd = cpu_to_be32(priv->pdn);
3082 mkc->len = cpu_to_be64(npages << PAGE_SHIFT);
3083 mkc->xlt_oct_size = cpu_to_be32(mlx5e_get_mtt_octw(npages));
3084 mkc->log2_page_size = PAGE_SHIFT;
3086 err = mlx5_core_create_mkey(mdev, &priv->umr_mkey, in, inlen, NULL,
3094 static void *mlx5e_create_netdev(struct mlx5_core_dev *mdev)
3096 struct net_device *netdev;
3097 struct mlx5e_priv *priv;
3098 int nch = mlx5e_get_max_num_channels(mdev);
3101 if (mlx5e_check_required_hca_cap(mdev))
3104 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
3105 nch * MLX5E_MAX_NUM_TC,
3108 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
3112 mlx5e_build_netdev_priv(mdev, netdev, nch);
3113 mlx5e_build_netdev(netdev);
3115 netif_carrier_off(netdev);
3117 priv = netdev_priv(netdev);
3119 priv->wq = create_singlethread_workqueue("mlx5e");
3121 goto err_free_netdev;
3123 err = mlx5_alloc_map_uar(mdev, &priv->cq_uar, false);
3125 mlx5_core_err(mdev, "alloc_map uar failed, %d\n", err);
3126 goto err_destroy_wq;
3129 err = mlx5_core_alloc_pd(mdev, &priv->pdn);
3131 mlx5_core_err(mdev, "alloc pd failed, %d\n", err);
3132 goto err_unmap_free_uar;
3135 err = mlx5_core_alloc_transport_domain(mdev, &priv->tdn);
3137 mlx5_core_err(mdev, "alloc td failed, %d\n", err);
3138 goto err_dealloc_pd;
3141 err = mlx5e_create_mkey(priv, priv->pdn, &priv->mkey);
3143 mlx5_core_err(mdev, "create mkey failed, %d\n", err);
3144 goto err_dealloc_transport_domain;
3147 err = mlx5e_create_umr_mkey(priv);
3149 mlx5_core_err(mdev, "create umr mkey failed, %d\n", err);
3150 goto err_destroy_mkey;
3153 err = mlx5e_create_tises(priv);
3155 mlx5_core_warn(mdev, "create tises failed, %d\n", err);
3156 goto err_destroy_umr_mkey;
3159 err = mlx5e_open_drop_rq(priv);
3161 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
3162 goto err_destroy_tises;
3165 err = mlx5e_create_rqts(priv);
3167 mlx5_core_warn(mdev, "create rqts failed, %d\n", err);
3168 goto err_close_drop_rq;
3171 err = mlx5e_create_tirs(priv);
3173 mlx5_core_warn(mdev, "create tirs failed, %d\n", err);
3174 goto err_destroy_rqts;
3177 err = mlx5e_create_flow_steering(priv);
3179 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
3180 goto err_destroy_tirs;
3183 mlx5e_create_q_counter(priv);
3185 mlx5e_init_l2_addr(priv);
3187 mlx5e_vxlan_init(priv);
3189 err = mlx5e_tc_init(priv);
3191 goto err_dealloc_q_counters;
3193 #ifdef CONFIG_MLX5_CORE_EN_DCB
3194 mlx5e_dcbnl_ieee_setets_core(priv, &priv->params.ets);
3197 err = register_netdev(netdev);
3199 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
3200 goto err_tc_cleanup;
3203 if (mlx5e_vxlan_allowed(mdev)) {
3205 vxlan_get_rx_port(netdev);
3209 mlx5e_enable_async_events(priv);
3210 queue_work(priv->wq, &priv->set_rx_mode_work);
3215 mlx5e_tc_cleanup(priv);
3217 err_dealloc_q_counters:
3218 mlx5e_destroy_q_counter(priv);
3219 mlx5e_destroy_flow_steering(priv);
3222 mlx5e_destroy_tirs(priv);
3225 mlx5e_destroy_rqts(priv);
3228 mlx5e_close_drop_rq(priv);
3231 mlx5e_destroy_tises(priv);
3233 err_destroy_umr_mkey:
3234 mlx5_core_destroy_mkey(mdev, &priv->umr_mkey);
3237 mlx5_core_destroy_mkey(mdev, &priv->mkey);
3239 err_dealloc_transport_domain:
3240 mlx5_core_dealloc_transport_domain(mdev, priv->tdn);
3243 mlx5_core_dealloc_pd(mdev, priv->pdn);
3246 mlx5_unmap_free_uar(mdev, &priv->cq_uar);
3249 destroy_workqueue(priv->wq);
3252 free_netdev(netdev);
3257 static void mlx5e_destroy_netdev(struct mlx5_core_dev *mdev, void *vpriv)
3259 struct mlx5e_priv *priv = vpriv;
3260 struct net_device *netdev = priv->netdev;
3262 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
3264 queue_work(priv->wq, &priv->set_rx_mode_work);
3265 mlx5e_disable_async_events(priv);
3266 flush_workqueue(priv->wq);
3267 if (test_bit(MLX5_INTERFACE_STATE_SHUTDOWN, &mdev->intf_state)) {
3268 netif_device_detach(netdev);
3269 mlx5e_close(netdev);
3271 unregister_netdev(netdev);
3274 mlx5e_tc_cleanup(priv);
3275 mlx5e_vxlan_cleanup(priv);
3276 mlx5e_destroy_q_counter(priv);
3277 mlx5e_destroy_flow_steering(priv);
3278 mlx5e_destroy_tirs(priv);
3279 mlx5e_destroy_rqts(priv);
3280 mlx5e_close_drop_rq(priv);
3281 mlx5e_destroy_tises(priv);
3282 mlx5_core_destroy_mkey(priv->mdev, &priv->umr_mkey);
3283 mlx5_core_destroy_mkey(priv->mdev, &priv->mkey);
3284 mlx5_core_dealloc_transport_domain(priv->mdev, priv->tdn);
3285 mlx5_core_dealloc_pd(priv->mdev, priv->pdn);
3286 mlx5_unmap_free_uar(priv->mdev, &priv->cq_uar);
3287 cancel_delayed_work_sync(&priv->update_stats_work);
3288 destroy_workqueue(priv->wq);
3290 if (!test_bit(MLX5_INTERFACE_STATE_SHUTDOWN, &mdev->intf_state))
3291 free_netdev(netdev);
3294 static void *mlx5e_get_netdev(void *vpriv)
3296 struct mlx5e_priv *priv = vpriv;
3298 return priv->netdev;
3301 static struct mlx5_interface mlx5e_interface = {
3302 .add = mlx5e_create_netdev,
3303 .remove = mlx5e_destroy_netdev,
3304 .event = mlx5e_async_event,
3305 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
3306 .get_dev = mlx5e_get_netdev,
3309 void mlx5e_init(void)
3311 mlx5_register_interface(&mlx5e_interface);
3314 void mlx5e_cleanup(void)
3316 mlx5_unregister_interface(&mlx5e_interface);