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;
55 struct mlx5e_cq_param {
56 u32 cqc[MLX5_ST_SZ_DW(cqc)];
57 struct mlx5_wq_param wq;
62 struct mlx5e_channel_param {
63 struct mlx5e_rq_param rq;
64 struct mlx5e_sq_param sq;
65 struct mlx5e_sq_param icosq;
66 struct mlx5e_cq_param rx_cq;
67 struct mlx5e_cq_param tx_cq;
68 struct mlx5e_cq_param icosq_cq;
71 static void mlx5e_update_carrier(struct mlx5e_priv *priv)
73 struct mlx5_core_dev *mdev = priv->mdev;
76 port_state = mlx5_query_vport_state(mdev,
77 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT, 0);
79 if (port_state == VPORT_STATE_UP)
80 netif_carrier_on(priv->netdev);
82 netif_carrier_off(priv->netdev);
85 static void mlx5e_update_carrier_work(struct work_struct *work)
87 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
90 mutex_lock(&priv->state_lock);
91 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
92 mlx5e_update_carrier(priv);
93 mutex_unlock(&priv->state_lock);
96 static void mlx5e_update_sw_counters(struct mlx5e_priv *priv)
98 struct mlx5e_sw_stats *s = &priv->stats.sw;
99 struct mlx5e_rq_stats *rq_stats;
100 struct mlx5e_sq_stats *sq_stats;
101 u64 tx_offload_none = 0;
104 memset(s, 0, sizeof(*s));
105 for (i = 0; i < priv->params.num_channels; i++) {
106 rq_stats = &priv->channel[i]->rq.stats;
108 s->rx_packets += rq_stats->packets;
109 s->rx_bytes += rq_stats->bytes;
110 s->rx_lro_packets += rq_stats->lro_packets;
111 s->rx_lro_bytes += rq_stats->lro_bytes;
112 s->rx_csum_none += rq_stats->csum_none;
113 s->rx_csum_complete += rq_stats->csum_complete;
114 s->rx_csum_unnecessary_inner += rq_stats->csum_unnecessary_inner;
115 s->rx_wqe_err += rq_stats->wqe_err;
116 s->rx_mpwqe_filler += rq_stats->mpwqe_filler;
117 s->rx_mpwqe_frag += rq_stats->mpwqe_frag;
118 s->rx_buff_alloc_err += rq_stats->buff_alloc_err;
119 s->rx_cqe_compress_blks += rq_stats->cqe_compress_blks;
120 s->rx_cqe_compress_pkts += rq_stats->cqe_compress_pkts;
122 for (j = 0; j < priv->params.num_tc; j++) {
123 sq_stats = &priv->channel[i]->sq[j].stats;
125 s->tx_packets += sq_stats->packets;
126 s->tx_bytes += sq_stats->bytes;
127 s->tx_tso_packets += sq_stats->tso_packets;
128 s->tx_tso_bytes += sq_stats->tso_bytes;
129 s->tx_tso_inner_packets += sq_stats->tso_inner_packets;
130 s->tx_tso_inner_bytes += sq_stats->tso_inner_bytes;
131 s->tx_queue_stopped += sq_stats->stopped;
132 s->tx_queue_wake += sq_stats->wake;
133 s->tx_queue_dropped += sq_stats->dropped;
134 s->tx_csum_partial_inner += sq_stats->csum_partial_inner;
135 tx_offload_none += sq_stats->csum_none;
139 /* Update calculated offload counters */
140 s->tx_csum_partial = s->tx_packets - tx_offload_none - s->tx_csum_partial_inner;
141 s->rx_csum_unnecessary = s->rx_packets - s->rx_csum_none - s->rx_csum_complete;
143 s->link_down_events_phy = MLX5_GET(ppcnt_reg,
144 priv->stats.pport.phy_counters,
145 counter_set.phys_layer_cntrs.link_down_events);
148 static void mlx5e_update_vport_counters(struct mlx5e_priv *priv)
150 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
151 u32 *out = (u32 *)priv->stats.vport.query_vport_out;
152 u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)];
153 struct mlx5_core_dev *mdev = priv->mdev;
155 memset(in, 0, sizeof(in));
157 MLX5_SET(query_vport_counter_in, in, opcode,
158 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
159 MLX5_SET(query_vport_counter_in, in, op_mod, 0);
160 MLX5_SET(query_vport_counter_in, in, other_vport, 0);
162 memset(out, 0, outlen);
164 mlx5_cmd_exec(mdev, in, sizeof(in), out, outlen);
167 static void mlx5e_update_pport_counters(struct mlx5e_priv *priv)
169 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
170 struct mlx5_core_dev *mdev = priv->mdev;
171 int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
176 in = mlx5_vzalloc(sz);
180 MLX5_SET(ppcnt_reg, in, local_port, 1);
182 out = pstats->IEEE_802_3_counters;
183 MLX5_SET(ppcnt_reg, in, grp, MLX5_IEEE_802_3_COUNTERS_GROUP);
184 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
186 out = pstats->RFC_2863_counters;
187 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2863_COUNTERS_GROUP);
188 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
190 out = pstats->RFC_2819_counters;
191 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2819_COUNTERS_GROUP);
192 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
194 out = pstats->phy_counters;
195 MLX5_SET(ppcnt_reg, in, grp, MLX5_PHYSICAL_LAYER_COUNTERS_GROUP);
196 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
198 MLX5_SET(ppcnt_reg, in, grp, MLX5_PER_PRIORITY_COUNTERS_GROUP);
199 for (prio = 0; prio < NUM_PPORT_PRIO; prio++) {
200 out = pstats->per_prio_counters[prio];
201 MLX5_SET(ppcnt_reg, in, prio_tc, prio);
202 mlx5_core_access_reg(mdev, in, sz, out, sz,
203 MLX5_REG_PPCNT, 0, 0);
210 static void mlx5e_update_q_counter(struct mlx5e_priv *priv)
212 struct mlx5e_qcounter_stats *qcnt = &priv->stats.qcnt;
214 if (!priv->q_counter)
217 mlx5_core_query_out_of_buffer(priv->mdev, priv->q_counter,
218 &qcnt->rx_out_of_buffer);
221 void mlx5e_update_stats(struct mlx5e_priv *priv)
223 mlx5e_update_q_counter(priv);
224 mlx5e_update_vport_counters(priv);
225 mlx5e_update_pport_counters(priv);
226 mlx5e_update_sw_counters(priv);
229 static void mlx5e_update_stats_work(struct work_struct *work)
231 struct delayed_work *dwork = to_delayed_work(work);
232 struct mlx5e_priv *priv = container_of(dwork, struct mlx5e_priv,
234 mutex_lock(&priv->state_lock);
235 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
236 mlx5e_update_stats(priv);
237 queue_delayed_work(priv->wq, dwork,
238 msecs_to_jiffies(MLX5E_UPDATE_STATS_INTERVAL));
240 mutex_unlock(&priv->state_lock);
243 static void mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
244 enum mlx5_dev_event event, unsigned long param)
246 struct mlx5e_priv *priv = vpriv;
248 if (!test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state))
252 case MLX5_DEV_EVENT_PORT_UP:
253 case MLX5_DEV_EVENT_PORT_DOWN:
254 queue_work(priv->wq, &priv->update_carrier_work);
262 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
264 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
267 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
269 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
270 synchronize_irq(mlx5_get_msix_vec(priv->mdev, MLX5_EQ_VEC_ASYNC));
273 #define MLX5E_HW2SW_MTU(hwmtu) (hwmtu - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
274 #define MLX5E_SW2HW_MTU(swmtu) (swmtu + (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
276 static int mlx5e_create_rq(struct mlx5e_channel *c,
277 struct mlx5e_rq_param *param,
280 struct mlx5e_priv *priv = c->priv;
281 struct mlx5_core_dev *mdev = priv->mdev;
282 void *rqc = param->rqc;
283 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
289 param->wq.db_numa_node = cpu_to_node(c->cpu);
291 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
296 rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
298 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
300 switch (priv->params.rq_wq_type) {
301 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
302 rq->wqe_info = kzalloc_node(wq_sz * sizeof(*rq->wqe_info),
303 GFP_KERNEL, cpu_to_node(c->cpu));
306 goto err_rq_wq_destroy;
308 rq->handle_rx_cqe = mlx5e_handle_rx_cqe_mpwrq;
309 rq->alloc_wqe = mlx5e_alloc_rx_mpwqe;
311 rq->mpwqe_stride_sz = BIT(priv->params.mpwqe_log_stride_sz);
312 rq->mpwqe_num_strides = BIT(priv->params.mpwqe_log_num_strides);
313 rq->wqe_sz = rq->mpwqe_stride_sz * rq->mpwqe_num_strides;
314 byte_count = rq->wqe_sz;
316 default: /* MLX5_WQ_TYPE_LINKED_LIST */
317 rq->skb = kzalloc_node(wq_sz * sizeof(*rq->skb), GFP_KERNEL,
318 cpu_to_node(c->cpu));
321 goto err_rq_wq_destroy;
323 rq->handle_rx_cqe = mlx5e_handle_rx_cqe;
324 rq->alloc_wqe = mlx5e_alloc_rx_wqe;
326 rq->wqe_sz = (priv->params.lro_en) ?
327 priv->params.lro_wqe_sz :
328 MLX5E_SW2HW_MTU(priv->netdev->mtu);
329 rq->wqe_sz = SKB_DATA_ALIGN(rq->wqe_sz);
330 byte_count = rq->wqe_sz;
331 byte_count |= MLX5_HW_START_PADDING;
334 for (i = 0; i < wq_sz; i++) {
335 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
337 wqe->data.byte_count = cpu_to_be32(byte_count);
340 INIT_WORK(&rq->am.work, mlx5e_rx_am_work);
341 rq->am.mode = priv->params.rx_cq_period_mode;
343 rq->wq_type = priv->params.rq_wq_type;
345 rq->netdev = c->netdev;
346 rq->tstamp = &priv->tstamp;
350 rq->mkey_be = c->mkey_be;
351 rq->umr_mkey_be = cpu_to_be32(c->priv->umr_mkey.key);
356 mlx5_wq_destroy(&rq->wq_ctrl);
361 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
363 switch (rq->wq_type) {
364 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
367 default: /* MLX5_WQ_TYPE_LINKED_LIST */
371 mlx5_wq_destroy(&rq->wq_ctrl);
374 static int mlx5e_enable_rq(struct mlx5e_rq *rq, struct mlx5e_rq_param *param)
376 struct mlx5e_priv *priv = rq->priv;
377 struct mlx5_core_dev *mdev = priv->mdev;
385 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
386 sizeof(u64) * rq->wq_ctrl.buf.npages;
387 in = mlx5_vzalloc(inlen);
391 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
392 wq = MLX5_ADDR_OF(rqc, rqc, wq);
394 memcpy(rqc, param->rqc, sizeof(param->rqc));
396 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
397 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
398 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
399 MLX5_SET(rqc, rqc, vsd, priv->params.vlan_strip_disable);
400 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
401 MLX5_ADAPTER_PAGE_SHIFT);
402 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
404 mlx5_fill_page_array(&rq->wq_ctrl.buf,
405 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
407 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
414 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
417 struct mlx5e_channel *c = rq->channel;
418 struct mlx5e_priv *priv = c->priv;
419 struct mlx5_core_dev *mdev = priv->mdev;
426 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
427 in = mlx5_vzalloc(inlen);
431 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
433 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
434 MLX5_SET(rqc, rqc, state, next_state);
436 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
443 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
445 struct mlx5e_channel *c = rq->channel;
446 struct mlx5e_priv *priv = c->priv;
447 struct mlx5_core_dev *mdev = priv->mdev;
454 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
455 in = mlx5_vzalloc(inlen);
459 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
461 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
462 MLX5_SET64(modify_rq_in, in, modify_bitmask, MLX5_RQ_BITMASK_VSD);
463 MLX5_SET(rqc, rqc, vsd, vsd);
464 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
466 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
473 static void mlx5e_disable_rq(struct mlx5e_rq *rq)
475 mlx5_core_destroy_rq(rq->priv->mdev, rq->rqn);
478 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq)
480 unsigned long exp_time = jiffies + msecs_to_jiffies(20000);
481 struct mlx5e_channel *c = rq->channel;
482 struct mlx5e_priv *priv = c->priv;
483 struct mlx5_wq_ll *wq = &rq->wq;
485 while (time_before(jiffies, exp_time)) {
486 if (wq->cur_sz >= priv->params.min_rx_wqes)
495 static int mlx5e_open_rq(struct mlx5e_channel *c,
496 struct mlx5e_rq_param *param,
499 struct mlx5e_sq *sq = &c->icosq;
500 u16 pi = sq->pc & sq->wq.sz_m1;
503 err = mlx5e_create_rq(c, param, rq);
507 err = mlx5e_enable_rq(rq, param);
511 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
515 if (param->am_enabled)
516 set_bit(MLX5E_RQ_STATE_AM, &c->rq.state);
518 set_bit(MLX5E_RQ_STATE_POST_WQES_ENABLE, &rq->state);
520 sq->ico_wqe_info[pi].opcode = MLX5_OPCODE_NOP;
521 sq->ico_wqe_info[pi].num_wqebbs = 1;
522 mlx5e_send_nop(sq, true); /* trigger mlx5e_post_rx_wqes() */
527 mlx5e_disable_rq(rq);
529 mlx5e_destroy_rq(rq);
534 static void mlx5e_close_rq(struct mlx5e_rq *rq)
536 clear_bit(MLX5E_RQ_STATE_POST_WQES_ENABLE, &rq->state);
537 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
539 mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RDY, MLX5_RQC_STATE_ERR);
540 while (!mlx5_wq_ll_is_empty(&rq->wq))
543 /* avoid destroying rq before mlx5e_poll_rx_cq() is done with it */
544 napi_synchronize(&rq->channel->napi);
546 cancel_work_sync(&rq->am.work);
548 mlx5e_disable_rq(rq);
549 mlx5e_destroy_rq(rq);
552 static void mlx5e_free_sq_db(struct mlx5e_sq *sq)
559 static int mlx5e_alloc_sq_db(struct mlx5e_sq *sq, int numa)
561 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
562 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
564 sq->skb = kzalloc_node(wq_sz * sizeof(*sq->skb), GFP_KERNEL, numa);
565 sq->dma_fifo = kzalloc_node(df_sz * sizeof(*sq->dma_fifo), GFP_KERNEL,
567 sq->wqe_info = kzalloc_node(wq_sz * sizeof(*sq->wqe_info), GFP_KERNEL,
570 if (!sq->skb || !sq->dma_fifo || !sq->wqe_info) {
571 mlx5e_free_sq_db(sq);
575 sq->dma_fifo_mask = df_sz - 1;
580 static int mlx5e_create_sq(struct mlx5e_channel *c,
582 struct mlx5e_sq_param *param,
585 struct mlx5e_priv *priv = c->priv;
586 struct mlx5_core_dev *mdev = priv->mdev;
588 void *sqc = param->sqc;
589 void *sqc_wq = MLX5_ADDR_OF(sqc, sqc, wq);
592 err = mlx5_alloc_map_uar(mdev, &sq->uar, !!MLX5_CAP_GEN(mdev, bf));
596 param->wq.db_numa_node = cpu_to_node(c->cpu);
598 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq,
601 goto err_unmap_free_uar;
603 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
604 if (sq->uar.bf_map) {
605 set_bit(MLX5E_SQ_STATE_BF_ENABLE, &sq->state);
606 sq->uar_map = sq->uar.bf_map;
608 sq->uar_map = sq->uar.map;
610 sq->bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
611 sq->max_inline = param->max_inline;
613 err = mlx5e_alloc_sq_db(sq, cpu_to_node(c->cpu));
615 goto err_sq_wq_destroy;
618 u8 wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
620 sq->ico_wqe_info = kzalloc_node(sizeof(*sq->ico_wqe_info) *
623 cpu_to_node(c->cpu));
624 if (!sq->ico_wqe_info) {
631 txq_ix = c->ix + tc * priv->params.num_channels;
632 sq->txq = netdev_get_tx_queue(priv->netdev, txq_ix);
633 priv->txq_to_sq_map[txq_ix] = sq;
637 sq->tstamp = &priv->tstamp;
638 sq->mkey_be = c->mkey_be;
641 sq->edge = (sq->wq.sz_m1 + 1) - MLX5_SEND_WQE_MAX_WQEBBS;
642 sq->bf_budget = MLX5E_SQ_BF_BUDGET;
647 mlx5e_free_sq_db(sq);
650 mlx5_wq_destroy(&sq->wq_ctrl);
653 mlx5_unmap_free_uar(mdev, &sq->uar);
658 static void mlx5e_destroy_sq(struct mlx5e_sq *sq)
660 struct mlx5e_channel *c = sq->channel;
661 struct mlx5e_priv *priv = c->priv;
663 kfree(sq->ico_wqe_info);
664 mlx5e_free_sq_db(sq);
665 mlx5_wq_destroy(&sq->wq_ctrl);
666 mlx5_unmap_free_uar(priv->mdev, &sq->uar);
669 static int mlx5e_enable_sq(struct mlx5e_sq *sq, struct mlx5e_sq_param *param)
671 struct mlx5e_channel *c = sq->channel;
672 struct mlx5e_priv *priv = c->priv;
673 struct mlx5_core_dev *mdev = priv->mdev;
681 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
682 sizeof(u64) * sq->wq_ctrl.buf.npages;
683 in = mlx5_vzalloc(inlen);
687 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
688 wq = MLX5_ADDR_OF(sqc, sqc, wq);
690 memcpy(sqc, param->sqc, sizeof(param->sqc));
692 MLX5_SET(sqc, sqc, tis_num_0, param->icosq ? 0 : priv->tisn[sq->tc]);
693 MLX5_SET(sqc, sqc, cqn, sq->cq.mcq.cqn);
694 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
695 MLX5_SET(sqc, sqc, tis_lst_sz, param->icosq ? 0 : 1);
696 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
698 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
699 MLX5_SET(wq, wq, uar_page, sq->uar.index);
700 MLX5_SET(wq, wq, log_wq_pg_sz, sq->wq_ctrl.buf.page_shift -
701 MLX5_ADAPTER_PAGE_SHIFT);
702 MLX5_SET64(wq, wq, dbr_addr, sq->wq_ctrl.db.dma);
704 mlx5_fill_page_array(&sq->wq_ctrl.buf,
705 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
707 err = mlx5_core_create_sq(mdev, in, inlen, &sq->sqn);
714 static int mlx5e_modify_sq(struct mlx5e_sq *sq, int curr_state,
715 int next_state, bool update_rl, int rl_index)
717 struct mlx5e_channel *c = sq->channel;
718 struct mlx5e_priv *priv = c->priv;
719 struct mlx5_core_dev *mdev = priv->mdev;
726 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
727 in = mlx5_vzalloc(inlen);
731 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
733 MLX5_SET(modify_sq_in, in, sq_state, curr_state);
734 MLX5_SET(sqc, sqc, state, next_state);
735 if (update_rl && next_state == MLX5_SQC_STATE_RDY) {
736 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
737 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
740 err = mlx5_core_modify_sq(mdev, sq->sqn, in, inlen);
747 static void mlx5e_disable_sq(struct mlx5e_sq *sq)
749 struct mlx5e_channel *c = sq->channel;
750 struct mlx5e_priv *priv = c->priv;
751 struct mlx5_core_dev *mdev = priv->mdev;
753 mlx5_core_destroy_sq(mdev, sq->sqn);
755 mlx5_rl_remove_rate(mdev, sq->rate_limit);
758 static int mlx5e_open_sq(struct mlx5e_channel *c,
760 struct mlx5e_sq_param *param,
765 err = mlx5e_create_sq(c, tc, param, sq);
769 err = mlx5e_enable_sq(sq, param);
773 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST, MLX5_SQC_STATE_RDY,
779 set_bit(MLX5E_SQ_STATE_WAKE_TXQ_ENABLE, &sq->state);
780 netdev_tx_reset_queue(sq->txq);
781 netif_tx_start_queue(sq->txq);
787 mlx5e_disable_sq(sq);
789 mlx5e_destroy_sq(sq);
794 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
796 __netif_tx_lock_bh(txq);
797 netif_tx_stop_queue(txq);
798 __netif_tx_unlock_bh(txq);
801 static void mlx5e_close_sq(struct mlx5e_sq *sq)
804 clear_bit(MLX5E_SQ_STATE_WAKE_TXQ_ENABLE, &sq->state);
805 /* prevent netif_tx_wake_queue */
806 napi_synchronize(&sq->channel->napi);
807 netif_tx_disable_queue(sq->txq);
809 /* ensure hw is notified of all pending wqes */
810 if (mlx5e_sq_has_room_for(sq, 1))
811 mlx5e_send_nop(sq, true);
813 mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY, MLX5_SQC_STATE_ERR,
817 while (sq->cc != sq->pc) /* wait till sq is empty */
820 /* avoid destroying sq before mlx5e_poll_tx_cq() is done with it */
821 napi_synchronize(&sq->channel->napi);
823 mlx5e_disable_sq(sq);
824 mlx5e_destroy_sq(sq);
827 static int mlx5e_create_cq(struct mlx5e_channel *c,
828 struct mlx5e_cq_param *param,
831 struct mlx5e_priv *priv = c->priv;
832 struct mlx5_core_dev *mdev = priv->mdev;
833 struct mlx5_core_cq *mcq = &cq->mcq;
839 param->wq.buf_numa_node = cpu_to_node(c->cpu);
840 param->wq.db_numa_node = cpu_to_node(c->cpu);
841 param->eq_ix = c->ix;
843 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
848 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
853 mcq->set_ci_db = cq->wq_ctrl.db.db;
854 mcq->arm_db = cq->wq_ctrl.db.db + 1;
857 mcq->vector = param->eq_ix;
858 mcq->comp = mlx5e_completion_event;
859 mcq->event = mlx5e_cq_error_event;
861 mcq->uar = &priv->cq_uar;
863 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
864 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
875 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
877 mlx5_wq_destroy(&cq->wq_ctrl);
880 static int mlx5e_enable_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
882 struct mlx5e_priv *priv = cq->priv;
883 struct mlx5_core_dev *mdev = priv->mdev;
884 struct mlx5_core_cq *mcq = &cq->mcq;
889 unsigned int irqn_not_used;
893 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
894 sizeof(u64) * cq->wq_ctrl.buf.npages;
895 in = mlx5_vzalloc(inlen);
899 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
901 memcpy(cqc, param->cqc, sizeof(param->cqc));
903 mlx5_fill_page_array(&cq->wq_ctrl.buf,
904 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
906 mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
908 MLX5_SET(cqc, cqc, cq_period_mode, param->cq_period_mode);
909 MLX5_SET(cqc, cqc, c_eqn, eqn);
910 MLX5_SET(cqc, cqc, uar_page, mcq->uar->index);
911 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
912 MLX5_ADAPTER_PAGE_SHIFT);
913 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
915 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
927 static void mlx5e_disable_cq(struct mlx5e_cq *cq)
929 struct mlx5e_priv *priv = cq->priv;
930 struct mlx5_core_dev *mdev = priv->mdev;
932 mlx5_core_destroy_cq(mdev, &cq->mcq);
935 static int mlx5e_open_cq(struct mlx5e_channel *c,
936 struct mlx5e_cq_param *param,
938 struct mlx5e_cq_moder moderation)
941 struct mlx5e_priv *priv = c->priv;
942 struct mlx5_core_dev *mdev = priv->mdev;
944 err = mlx5e_create_cq(c, param, cq);
948 err = mlx5e_enable_cq(cq, param);
952 if (MLX5_CAP_GEN(mdev, cq_moderation))
953 mlx5_core_modify_cq_moderation(mdev, &cq->mcq,
959 mlx5e_destroy_cq(cq);
964 static void mlx5e_close_cq(struct mlx5e_cq *cq)
966 mlx5e_disable_cq(cq);
967 mlx5e_destroy_cq(cq);
970 static int mlx5e_get_cpu(struct mlx5e_priv *priv, int ix)
972 return cpumask_first(priv->mdev->priv.irq_info[ix].mask);
975 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
976 struct mlx5e_channel_param *cparam)
978 struct mlx5e_priv *priv = c->priv;
982 for (tc = 0; tc < c->num_tc; tc++) {
983 err = mlx5e_open_cq(c, &cparam->tx_cq, &c->sq[tc].cq,
984 priv->params.tx_cq_moderation);
986 goto err_close_tx_cqs;
992 for (tc--; tc >= 0; tc--)
993 mlx5e_close_cq(&c->sq[tc].cq);
998 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1002 for (tc = 0; tc < c->num_tc; tc++)
1003 mlx5e_close_cq(&c->sq[tc].cq);
1006 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1007 struct mlx5e_channel_param *cparam)
1012 for (tc = 0; tc < c->num_tc; tc++) {
1013 err = mlx5e_open_sq(c, tc, &cparam->sq, &c->sq[tc]);
1021 for (tc--; tc >= 0; tc--)
1022 mlx5e_close_sq(&c->sq[tc]);
1027 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1031 for (tc = 0; tc < c->num_tc; tc++)
1032 mlx5e_close_sq(&c->sq[tc]);
1035 static void mlx5e_build_channeltc_to_txq_map(struct mlx5e_priv *priv, int ix)
1039 for (i = 0; i < MLX5E_MAX_NUM_TC; i++)
1040 priv->channeltc_to_txq_map[ix][i] =
1041 ix + i * priv->params.num_channels;
1044 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1045 struct mlx5e_sq *sq, u32 rate)
1047 struct mlx5e_priv *priv = netdev_priv(dev);
1048 struct mlx5_core_dev *mdev = priv->mdev;
1052 if (rate == sq->rate_limit)
1057 /* remove current rl index to free space to next ones */
1058 mlx5_rl_remove_rate(mdev, sq->rate_limit);
1063 err = mlx5_rl_add_rate(mdev, rate, &rl_index);
1065 netdev_err(dev, "Failed configuring rate %u: %d\n",
1071 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY,
1072 MLX5_SQC_STATE_RDY, true, rl_index);
1074 netdev_err(dev, "Failed configuring rate %u: %d\n",
1076 /* remove the rate from the table */
1078 mlx5_rl_remove_rate(mdev, rate);
1082 sq->rate_limit = rate;
1086 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1088 struct mlx5e_priv *priv = netdev_priv(dev);
1089 struct mlx5_core_dev *mdev = priv->mdev;
1090 struct mlx5e_sq *sq = priv->txq_to_sq_map[index];
1093 if (!mlx5_rl_is_supported(mdev)) {
1094 netdev_err(dev, "Rate limiting is not supported on this device\n");
1098 /* rate is given in Mb/sec, HW config is in Kb/sec */
1101 /* Check whether rate in valid range, 0 is always valid */
1102 if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1103 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1107 mutex_lock(&priv->state_lock);
1108 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1109 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1111 priv->tx_rates[index] = rate;
1112 mutex_unlock(&priv->state_lock);
1117 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1118 struct mlx5e_channel_param *cparam,
1119 struct mlx5e_channel **cp)
1121 struct mlx5e_cq_moder icosq_cq_moder = {0, 0};
1122 struct net_device *netdev = priv->netdev;
1123 struct mlx5e_cq_moder rx_cq_profile;
1124 int cpu = mlx5e_get_cpu(priv, ix);
1125 struct mlx5e_channel *c;
1126 struct mlx5e_sq *sq;
1130 c = kzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1137 c->pdev = &priv->mdev->pdev->dev;
1138 c->netdev = priv->netdev;
1139 c->mkey_be = cpu_to_be32(priv->mkey.key);
1140 c->num_tc = priv->params.num_tc;
1142 if (priv->params.rx_am_enabled)
1143 rx_cq_profile = mlx5e_am_get_def_profile(priv->params.rx_cq_period_mode);
1145 rx_cq_profile = priv->params.rx_cq_moderation;
1147 mlx5e_build_channeltc_to_txq_map(priv, ix);
1149 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1151 err = mlx5e_open_cq(c, &cparam->icosq_cq, &c->icosq.cq, icosq_cq_moder);
1155 err = mlx5e_open_tx_cqs(c, cparam);
1157 goto err_close_icosq_cq;
1159 err = mlx5e_open_cq(c, &cparam->rx_cq, &c->rq.cq,
1162 goto err_close_tx_cqs;
1164 napi_enable(&c->napi);
1166 err = mlx5e_open_sq(c, 0, &cparam->icosq, &c->icosq);
1168 goto err_disable_napi;
1170 err = mlx5e_open_sqs(c, cparam);
1172 goto err_close_icosq;
1174 for (i = 0; i < priv->params.num_tc; i++) {
1175 u32 txq_ix = priv->channeltc_to_txq_map[ix][i];
1177 if (priv->tx_rates[txq_ix]) {
1178 sq = priv->txq_to_sq_map[txq_ix];
1179 mlx5e_set_sq_maxrate(priv->netdev, sq,
1180 priv->tx_rates[txq_ix]);
1184 err = mlx5e_open_rq(c, &cparam->rq, &c->rq);
1188 netif_set_xps_queue(netdev, get_cpu_mask(c->cpu), ix);
1197 mlx5e_close_sq(&c->icosq);
1200 napi_disable(&c->napi);
1201 mlx5e_close_cq(&c->rq.cq);
1204 mlx5e_close_tx_cqs(c);
1207 mlx5e_close_cq(&c->icosq.cq);
1210 netif_napi_del(&c->napi);
1211 napi_hash_del(&c->napi);
1217 static void mlx5e_close_channel(struct mlx5e_channel *c)
1219 mlx5e_close_rq(&c->rq);
1221 mlx5e_close_sq(&c->icosq);
1222 napi_disable(&c->napi);
1223 mlx5e_close_cq(&c->rq.cq);
1224 mlx5e_close_tx_cqs(c);
1225 mlx5e_close_cq(&c->icosq.cq);
1226 netif_napi_del(&c->napi);
1228 napi_hash_del(&c->napi);
1234 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
1235 struct mlx5e_rq_param *param)
1237 void *rqc = param->rqc;
1238 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1240 switch (priv->params.rq_wq_type) {
1241 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1242 MLX5_SET(wq, wq, log_wqe_num_of_strides,
1243 priv->params.mpwqe_log_num_strides - 9);
1244 MLX5_SET(wq, wq, log_wqe_stride_size,
1245 priv->params.mpwqe_log_stride_sz - 6);
1246 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ);
1248 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1249 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1252 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1253 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1254 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_rq_size);
1255 MLX5_SET(wq, wq, pd, priv->pdn);
1256 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
1258 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1259 param->wq.linear = 1;
1261 param->am_enabled = priv->params.rx_am_enabled;
1264 static void mlx5e_build_drop_rq_param(struct mlx5e_rq_param *param)
1266 void *rqc = param->rqc;
1267 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1269 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1270 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1273 static void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
1274 struct mlx5e_sq_param *param)
1276 void *sqc = param->sqc;
1277 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1279 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1280 MLX5_SET(wq, wq, pd, priv->pdn);
1282 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1285 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
1286 struct mlx5e_sq_param *param)
1288 void *sqc = param->sqc;
1289 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1291 mlx5e_build_sq_param_common(priv, param);
1292 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_sq_size);
1294 param->max_inline = priv->params.tx_max_inline;
1297 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1298 struct mlx5e_cq_param *param)
1300 void *cqc = param->cqc;
1302 MLX5_SET(cqc, cqc, uar_page, priv->cq_uar.index);
1305 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
1306 struct mlx5e_cq_param *param)
1308 void *cqc = param->cqc;
1311 switch (priv->params.rq_wq_type) {
1312 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1313 log_cq_size = priv->params.log_rq_size +
1314 priv->params.mpwqe_log_num_strides;
1316 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1317 log_cq_size = priv->params.log_rq_size;
1320 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
1321 if (priv->params.rx_cqe_compress) {
1322 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
1323 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
1326 mlx5e_build_common_cq_param(priv, param);
1328 param->cq_period_mode = priv->params.rx_cq_period_mode;
1331 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
1332 struct mlx5e_cq_param *param)
1334 void *cqc = param->cqc;
1336 MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_sq_size);
1338 mlx5e_build_common_cq_param(priv, param);
1340 param->cq_period_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1343 static void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
1344 struct mlx5e_cq_param *param,
1347 void *cqc = param->cqc;
1349 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
1351 mlx5e_build_common_cq_param(priv, param);
1353 param->cq_period_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1356 static void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
1357 struct mlx5e_sq_param *param,
1360 void *sqc = param->sqc;
1361 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1363 mlx5e_build_sq_param_common(priv, param);
1365 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
1366 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
1368 param->icosq = true;
1371 static void mlx5e_build_channel_param(struct mlx5e_priv *priv, struct mlx5e_channel_param *cparam)
1373 u8 icosq_log_wq_sz = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
1375 mlx5e_build_rq_param(priv, &cparam->rq);
1376 mlx5e_build_sq_param(priv, &cparam->sq);
1377 mlx5e_build_icosq_param(priv, &cparam->icosq, icosq_log_wq_sz);
1378 mlx5e_build_rx_cq_param(priv, &cparam->rx_cq);
1379 mlx5e_build_tx_cq_param(priv, &cparam->tx_cq);
1380 mlx5e_build_ico_cq_param(priv, &cparam->icosq_cq, icosq_log_wq_sz);
1383 static int mlx5e_open_channels(struct mlx5e_priv *priv)
1385 struct mlx5e_channel_param *cparam;
1386 int nch = priv->params.num_channels;
1391 priv->channel = kcalloc(nch, sizeof(struct mlx5e_channel *),
1394 priv->txq_to_sq_map = kcalloc(nch * priv->params.num_tc,
1395 sizeof(struct mlx5e_sq *), GFP_KERNEL);
1397 cparam = kzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
1399 if (!priv->channel || !priv->txq_to_sq_map || !cparam)
1400 goto err_free_txq_to_sq_map;
1402 mlx5e_build_channel_param(priv, cparam);
1404 for (i = 0; i < nch; i++) {
1405 err = mlx5e_open_channel(priv, i, cparam, &priv->channel[i]);
1407 goto err_close_channels;
1410 for (j = 0; j < nch; j++) {
1411 err = mlx5e_wait_for_min_rx_wqes(&priv->channel[j]->rq);
1413 goto err_close_channels;
1420 for (i--; i >= 0; i--)
1421 mlx5e_close_channel(priv->channel[i]);
1423 err_free_txq_to_sq_map:
1424 kfree(priv->txq_to_sq_map);
1425 kfree(priv->channel);
1431 static void mlx5e_close_channels(struct mlx5e_priv *priv)
1435 for (i = 0; i < priv->params.num_channels; i++)
1436 mlx5e_close_channel(priv->channel[i]);
1438 kfree(priv->txq_to_sq_map);
1439 kfree(priv->channel);
1442 static int mlx5e_rx_hash_fn(int hfunc)
1444 return (hfunc == ETH_RSS_HASH_TOP) ?
1445 MLX5_RX_HASH_FN_TOEPLITZ :
1446 MLX5_RX_HASH_FN_INVERTED_XOR8;
1449 static int mlx5e_bits_invert(unsigned long a, int size)
1454 for (i = 0; i < size; i++)
1455 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
1460 static void mlx5e_fill_indir_rqt_rqns(struct mlx5e_priv *priv, void *rqtc)
1464 for (i = 0; i < MLX5E_INDIR_RQT_SIZE; i++) {
1468 if (priv->params.rss_hfunc == ETH_RSS_HASH_XOR)
1469 ix = mlx5e_bits_invert(i, MLX5E_LOG_INDIR_RQT_SIZE);
1471 ix = priv->params.indirection_rqt[ix];
1472 rqn = test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1473 priv->channel[ix]->rq.rqn :
1475 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
1479 static void mlx5e_fill_direct_rqt_rqn(struct mlx5e_priv *priv, void *rqtc,
1482 u32 rqn = test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1483 priv->channel[ix]->rq.rqn :
1486 MLX5_SET(rqtc, rqtc, rq_num[0], rqn);
1489 static int mlx5e_create_rqt(struct mlx5e_priv *priv, int sz, int ix, u32 *rqtn)
1491 struct mlx5_core_dev *mdev = priv->mdev;
1497 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
1498 in = mlx5_vzalloc(inlen);
1502 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
1504 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1505 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
1507 if (sz > 1) /* RSS */
1508 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1510 mlx5e_fill_direct_rqt_rqn(priv, rqtc, ix);
1512 err = mlx5_core_create_rqt(mdev, in, inlen, rqtn);
1518 static void mlx5e_destroy_rqt(struct mlx5e_priv *priv, u32 rqtn)
1520 mlx5_core_destroy_rqt(priv->mdev, rqtn);
1523 static int mlx5e_create_rqts(struct mlx5e_priv *priv)
1525 int nch = mlx5e_get_max_num_channels(priv->mdev);
1531 rqtn = &priv->indir_rqtn;
1532 err = mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, 0, rqtn);
1537 for (ix = 0; ix < nch; ix++) {
1538 rqtn = &priv->direct_tir[ix].rqtn;
1539 err = mlx5e_create_rqt(priv, 1 /*size */, ix, rqtn);
1541 goto err_destroy_rqts;
1547 for (ix--; ix >= 0; ix--)
1548 mlx5e_destroy_rqt(priv, priv->direct_tir[ix].rqtn);
1550 mlx5e_destroy_rqt(priv, priv->indir_rqtn);
1555 static void mlx5e_destroy_rqts(struct mlx5e_priv *priv)
1557 int nch = mlx5e_get_max_num_channels(priv->mdev);
1560 for (i = 0; i < nch; i++)
1561 mlx5e_destroy_rqt(priv, priv->direct_tir[i].rqtn);
1563 mlx5e_destroy_rqt(priv, priv->indir_rqtn);
1566 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz, int ix)
1568 struct mlx5_core_dev *mdev = priv->mdev;
1574 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
1575 in = mlx5_vzalloc(inlen);
1579 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
1581 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1582 if (sz > 1) /* RSS */
1583 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1585 mlx5e_fill_direct_rqt_rqn(priv, rqtc, ix);
1587 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
1589 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
1596 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv)
1601 rqtn = priv->indir_rqtn;
1602 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, 0);
1603 for (ix = 0; ix < priv->params.num_channels; ix++) {
1604 rqtn = priv->direct_tir[ix].rqtn;
1605 mlx5e_redirect_rqt(priv, rqtn, 1, ix);
1609 static void mlx5e_build_tir_ctx_lro(void *tirc, struct mlx5e_priv *priv)
1611 if (!priv->params.lro_en)
1614 #define ROUGH_MAX_L2_L3_HDR_SZ 256
1616 MLX5_SET(tirc, tirc, lro_enable_mask,
1617 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
1618 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
1619 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
1620 (priv->params.lro_wqe_sz -
1621 ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
1622 MLX5_SET(tirc, tirc, lro_timeout_period_usecs,
1623 MLX5_CAP_ETH(priv->mdev,
1624 lro_timer_supported_periods[2]));
1627 void mlx5e_build_tir_ctx_hash(void *tirc, struct mlx5e_priv *priv)
1629 MLX5_SET(tirc, tirc, rx_hash_fn,
1630 mlx5e_rx_hash_fn(priv->params.rss_hfunc));
1631 if (priv->params.rss_hfunc == ETH_RSS_HASH_TOP) {
1632 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
1633 rx_hash_toeplitz_key);
1634 size_t len = MLX5_FLD_SZ_BYTES(tirc,
1635 rx_hash_toeplitz_key);
1637 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1638 memcpy(rss_key, priv->params.toeplitz_hash_key, len);
1642 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
1644 struct mlx5_core_dev *mdev = priv->mdev;
1653 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
1654 in = mlx5_vzalloc(inlen);
1658 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
1659 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
1661 mlx5e_build_tir_ctx_lro(tirc, priv);
1663 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
1664 err = mlx5_core_modify_tir(mdev, priv->indir_tirn[tt], in,
1670 for (ix = 0; ix < mlx5e_get_max_num_channels(mdev); ix++) {
1671 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
1683 static int mlx5e_refresh_tirs_self_loopback_enable(struct mlx5e_priv *priv)
1690 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
1691 in = mlx5_vzalloc(inlen);
1695 MLX5_SET(modify_tir_in, in, bitmask.self_lb_en, 1);
1697 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++) {
1698 err = mlx5_core_modify_tir(priv->mdev, priv->indir_tirn[i], in,
1704 for (i = 0; i < priv->params.num_channels; i++) {
1705 err = mlx5_core_modify_tir(priv->mdev,
1706 priv->direct_tir[i].tirn, in,
1717 static int mlx5e_set_mtu(struct mlx5e_priv *priv, u16 mtu)
1719 struct mlx5_core_dev *mdev = priv->mdev;
1720 u16 hw_mtu = MLX5E_SW2HW_MTU(mtu);
1723 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
1727 /* Update vport context MTU */
1728 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
1732 static void mlx5e_query_mtu(struct mlx5e_priv *priv, u16 *mtu)
1734 struct mlx5_core_dev *mdev = priv->mdev;
1738 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
1739 if (err || !hw_mtu) /* fallback to port oper mtu */
1740 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
1742 *mtu = MLX5E_HW2SW_MTU(hw_mtu);
1745 static int mlx5e_set_dev_port_mtu(struct net_device *netdev)
1747 struct mlx5e_priv *priv = netdev_priv(netdev);
1751 err = mlx5e_set_mtu(priv, netdev->mtu);
1755 mlx5e_query_mtu(priv, &mtu);
1756 if (mtu != netdev->mtu)
1757 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
1758 __func__, mtu, netdev->mtu);
1764 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
1766 struct mlx5e_priv *priv = netdev_priv(netdev);
1767 int nch = priv->params.num_channels;
1768 int ntc = priv->params.num_tc;
1771 netdev_reset_tc(netdev);
1776 netdev_set_num_tc(netdev, ntc);
1778 for (tc = 0; tc < ntc; tc++)
1779 netdev_set_tc_queue(netdev, tc, nch, tc * nch);
1782 int mlx5e_open_locked(struct net_device *netdev)
1784 struct mlx5e_priv *priv = netdev_priv(netdev);
1788 set_bit(MLX5E_STATE_OPENED, &priv->state);
1790 mlx5e_netdev_set_tcs(netdev);
1792 num_txqs = priv->params.num_channels * priv->params.num_tc;
1793 netif_set_real_num_tx_queues(netdev, num_txqs);
1794 netif_set_real_num_rx_queues(netdev, priv->params.num_channels);
1796 err = mlx5e_set_dev_port_mtu(netdev);
1798 goto err_clear_state_opened_flag;
1800 err = mlx5e_open_channels(priv);
1802 netdev_err(netdev, "%s: mlx5e_open_channels failed, %d\n",
1804 goto err_clear_state_opened_flag;
1807 err = mlx5e_refresh_tirs_self_loopback_enable(priv);
1809 netdev_err(netdev, "%s: mlx5e_refresh_tirs_self_loopback_enable failed, %d\n",
1811 goto err_close_channels;
1814 mlx5e_redirect_rqts(priv);
1815 mlx5e_update_carrier(priv);
1816 mlx5e_timestamp_init(priv);
1817 #ifdef CONFIG_RFS_ACCEL
1818 priv->netdev->rx_cpu_rmap = priv->mdev->rmap;
1821 queue_delayed_work(priv->wq, &priv->update_stats_work, 0);
1826 mlx5e_close_channels(priv);
1827 err_clear_state_opened_flag:
1828 clear_bit(MLX5E_STATE_OPENED, &priv->state);
1832 static int mlx5e_open(struct net_device *netdev)
1834 struct mlx5e_priv *priv = netdev_priv(netdev);
1837 mutex_lock(&priv->state_lock);
1838 err = mlx5e_open_locked(netdev);
1839 mutex_unlock(&priv->state_lock);
1844 int mlx5e_close_locked(struct net_device *netdev)
1846 struct mlx5e_priv *priv = netdev_priv(netdev);
1848 /* May already be CLOSED in case a previous configuration operation
1849 * (e.g RX/TX queue size change) that involves close&open failed.
1851 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
1854 clear_bit(MLX5E_STATE_OPENED, &priv->state);
1856 mlx5e_timestamp_cleanup(priv);
1857 netif_carrier_off(priv->netdev);
1858 mlx5e_redirect_rqts(priv);
1859 mlx5e_close_channels(priv);
1864 static int mlx5e_close(struct net_device *netdev)
1866 struct mlx5e_priv *priv = netdev_priv(netdev);
1869 mutex_lock(&priv->state_lock);
1870 err = mlx5e_close_locked(netdev);
1871 mutex_unlock(&priv->state_lock);
1876 static int mlx5e_create_drop_rq(struct mlx5e_priv *priv,
1877 struct mlx5e_rq *rq,
1878 struct mlx5e_rq_param *param)
1880 struct mlx5_core_dev *mdev = priv->mdev;
1881 void *rqc = param->rqc;
1882 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
1885 param->wq.db_numa_node = param->wq.buf_numa_node;
1887 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
1897 static int mlx5e_create_drop_cq(struct mlx5e_priv *priv,
1898 struct mlx5e_cq *cq,
1899 struct mlx5e_cq_param *param)
1901 struct mlx5_core_dev *mdev = priv->mdev;
1902 struct mlx5_core_cq *mcq = &cq->mcq;
1907 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1912 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1915 mcq->set_ci_db = cq->wq_ctrl.db.db;
1916 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1917 *mcq->set_ci_db = 0;
1919 mcq->vector = param->eq_ix;
1920 mcq->comp = mlx5e_completion_event;
1921 mcq->event = mlx5e_cq_error_event;
1923 mcq->uar = &priv->cq_uar;
1930 static int mlx5e_open_drop_rq(struct mlx5e_priv *priv)
1932 struct mlx5e_cq_param cq_param;
1933 struct mlx5e_rq_param rq_param;
1934 struct mlx5e_rq *rq = &priv->drop_rq;
1935 struct mlx5e_cq *cq = &priv->drop_rq.cq;
1938 memset(&cq_param, 0, sizeof(cq_param));
1939 memset(&rq_param, 0, sizeof(rq_param));
1940 mlx5e_build_drop_rq_param(&rq_param);
1942 err = mlx5e_create_drop_cq(priv, cq, &cq_param);
1946 err = mlx5e_enable_cq(cq, &cq_param);
1948 goto err_destroy_cq;
1950 err = mlx5e_create_drop_rq(priv, rq, &rq_param);
1952 goto err_disable_cq;
1954 err = mlx5e_enable_rq(rq, &rq_param);
1956 goto err_destroy_rq;
1961 mlx5e_destroy_rq(&priv->drop_rq);
1964 mlx5e_disable_cq(&priv->drop_rq.cq);
1967 mlx5e_destroy_cq(&priv->drop_rq.cq);
1972 static void mlx5e_close_drop_rq(struct mlx5e_priv *priv)
1974 mlx5e_disable_rq(&priv->drop_rq);
1975 mlx5e_destroy_rq(&priv->drop_rq);
1976 mlx5e_disable_cq(&priv->drop_rq.cq);
1977 mlx5e_destroy_cq(&priv->drop_rq.cq);
1980 static int mlx5e_create_tis(struct mlx5e_priv *priv, int tc)
1982 struct mlx5_core_dev *mdev = priv->mdev;
1983 u32 in[MLX5_ST_SZ_DW(create_tis_in)];
1984 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1986 memset(in, 0, sizeof(in));
1988 MLX5_SET(tisc, tisc, prio, tc << 1);
1989 MLX5_SET(tisc, tisc, transport_domain, priv->tdn);
1991 return mlx5_core_create_tis(mdev, in, sizeof(in), &priv->tisn[tc]);
1994 static void mlx5e_destroy_tis(struct mlx5e_priv *priv, int tc)
1996 mlx5_core_destroy_tis(priv->mdev, priv->tisn[tc]);
1999 static int mlx5e_create_tises(struct mlx5e_priv *priv)
2004 for (tc = 0; tc < MLX5E_MAX_NUM_TC; tc++) {
2005 err = mlx5e_create_tis(priv, tc);
2007 goto err_close_tises;
2013 for (tc--; tc >= 0; tc--)
2014 mlx5e_destroy_tis(priv, tc);
2019 static void mlx5e_destroy_tises(struct mlx5e_priv *priv)
2023 for (tc = 0; tc < MLX5E_MAX_NUM_TC; tc++)
2024 mlx5e_destroy_tis(priv, tc);
2027 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv, u32 *tirc,
2028 enum mlx5e_traffic_types tt)
2030 void *hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2032 MLX5_SET(tirc, tirc, transport_domain, priv->tdn);
2034 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
2035 MLX5_HASH_FIELD_SEL_DST_IP)
2037 #define MLX5_HASH_IP_L4PORTS (MLX5_HASH_FIELD_SEL_SRC_IP |\
2038 MLX5_HASH_FIELD_SEL_DST_IP |\
2039 MLX5_HASH_FIELD_SEL_L4_SPORT |\
2040 MLX5_HASH_FIELD_SEL_L4_DPORT)
2042 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
2043 MLX5_HASH_FIELD_SEL_DST_IP |\
2044 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
2046 mlx5e_build_tir_ctx_lro(tirc, priv);
2048 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2049 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqtn);
2050 mlx5e_build_tir_ctx_hash(tirc, priv);
2053 case MLX5E_TT_IPV4_TCP:
2054 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2055 MLX5_L3_PROT_TYPE_IPV4);
2056 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2057 MLX5_L4_PROT_TYPE_TCP);
2058 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2059 MLX5_HASH_IP_L4PORTS);
2062 case MLX5E_TT_IPV6_TCP:
2063 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2064 MLX5_L3_PROT_TYPE_IPV6);
2065 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2066 MLX5_L4_PROT_TYPE_TCP);
2067 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2068 MLX5_HASH_IP_L4PORTS);
2071 case MLX5E_TT_IPV4_UDP:
2072 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2073 MLX5_L3_PROT_TYPE_IPV4);
2074 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2075 MLX5_L4_PROT_TYPE_UDP);
2076 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2077 MLX5_HASH_IP_L4PORTS);
2080 case MLX5E_TT_IPV6_UDP:
2081 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2082 MLX5_L3_PROT_TYPE_IPV6);
2083 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2084 MLX5_L4_PROT_TYPE_UDP);
2085 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2086 MLX5_HASH_IP_L4PORTS);
2089 case MLX5E_TT_IPV4_IPSEC_AH:
2090 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2091 MLX5_L3_PROT_TYPE_IPV4);
2092 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2093 MLX5_HASH_IP_IPSEC_SPI);
2096 case MLX5E_TT_IPV6_IPSEC_AH:
2097 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2098 MLX5_L3_PROT_TYPE_IPV6);
2099 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2100 MLX5_HASH_IP_IPSEC_SPI);
2103 case MLX5E_TT_IPV4_IPSEC_ESP:
2104 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2105 MLX5_L3_PROT_TYPE_IPV4);
2106 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2107 MLX5_HASH_IP_IPSEC_SPI);
2110 case MLX5E_TT_IPV6_IPSEC_ESP:
2111 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2112 MLX5_L3_PROT_TYPE_IPV6);
2113 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2114 MLX5_HASH_IP_IPSEC_SPI);
2118 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2119 MLX5_L3_PROT_TYPE_IPV4);
2120 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2125 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2126 MLX5_L3_PROT_TYPE_IPV6);
2127 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2132 "mlx5e_build_indir_tir_ctx: bad traffic type!\n");
2136 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 *tirc,
2139 MLX5_SET(tirc, tirc, transport_domain, priv->tdn);
2141 mlx5e_build_tir_ctx_lro(tirc, priv);
2143 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2144 MLX5_SET(tirc, tirc, indirect_table, rqtn);
2145 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
2148 static int mlx5e_create_tirs(struct mlx5e_priv *priv)
2150 int nch = mlx5e_get_max_num_channels(priv->mdev);
2159 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2160 in = mlx5_vzalloc(inlen);
2165 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2166 memset(in, 0, inlen);
2167 tirn = &priv->indir_tirn[tt];
2168 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2169 mlx5e_build_indir_tir_ctx(priv, tirc, tt);
2170 err = mlx5_core_create_tir(priv->mdev, in, inlen, tirn);
2172 goto err_destroy_tirs;
2176 for (ix = 0; ix < nch; ix++) {
2177 memset(in, 0, inlen);
2178 tirn = &priv->direct_tir[ix].tirn;
2179 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2180 mlx5e_build_direct_tir_ctx(priv, tirc,
2181 priv->direct_tir[ix].rqtn);
2182 err = mlx5_core_create_tir(priv->mdev, in, inlen, tirn);
2184 goto err_destroy_ch_tirs;
2191 err_destroy_ch_tirs:
2192 for (ix--; ix >= 0; ix--)
2193 mlx5_core_destroy_tir(priv->mdev, priv->direct_tir[ix].tirn);
2196 for (tt--; tt >= 0; tt--)
2197 mlx5_core_destroy_tir(priv->mdev, priv->indir_tirn[tt]);
2204 static void mlx5e_destroy_tirs(struct mlx5e_priv *priv)
2206 int nch = mlx5e_get_max_num_channels(priv->mdev);
2209 for (i = 0; i < nch; i++)
2210 mlx5_core_destroy_tir(priv->mdev, priv->direct_tir[i].tirn);
2212 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
2213 mlx5_core_destroy_tir(priv->mdev, priv->indir_tirn[i]);
2216 int mlx5e_modify_rqs_vsd(struct mlx5e_priv *priv, bool vsd)
2221 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
2224 for (i = 0; i < priv->params.num_channels; i++) {
2225 err = mlx5e_modify_rq_vsd(&priv->channel[i]->rq, vsd);
2233 static int mlx5e_setup_tc(struct net_device *netdev, u8 tc)
2235 struct mlx5e_priv *priv = netdev_priv(netdev);
2239 if (tc && tc != MLX5E_MAX_NUM_TC)
2242 mutex_lock(&priv->state_lock);
2244 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2246 mlx5e_close_locked(priv->netdev);
2248 priv->params.num_tc = tc ? tc : 1;
2251 err = mlx5e_open_locked(priv->netdev);
2253 mutex_unlock(&priv->state_lock);
2258 static int mlx5e_ndo_setup_tc(struct net_device *dev, u32 handle,
2259 __be16 proto, struct tc_to_netdev *tc)
2261 struct mlx5e_priv *priv = netdev_priv(dev);
2263 if (TC_H_MAJ(handle) != TC_H_MAJ(TC_H_INGRESS))
2267 case TC_SETUP_CLSFLOWER:
2268 switch (tc->cls_flower->command) {
2269 case TC_CLSFLOWER_REPLACE:
2270 return mlx5e_configure_flower(priv, proto, tc->cls_flower);
2271 case TC_CLSFLOWER_DESTROY:
2272 return mlx5e_delete_flower(priv, tc->cls_flower);
2273 case TC_CLSFLOWER_STATS:
2274 return mlx5e_stats_flower(priv, tc->cls_flower);
2281 if (tc->type != TC_SETUP_MQPRIO)
2284 return mlx5e_setup_tc(dev, tc->tc);
2287 static struct rtnl_link_stats64 *
2288 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
2290 struct mlx5e_priv *priv = netdev_priv(dev);
2291 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
2292 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
2293 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
2295 stats->rx_packets = sstats->rx_packets;
2296 stats->rx_bytes = sstats->rx_bytes;
2297 stats->tx_packets = sstats->tx_packets;
2298 stats->tx_bytes = sstats->tx_bytes;
2300 stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
2301 stats->tx_dropped = sstats->tx_queue_dropped;
2303 stats->rx_length_errors =
2304 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
2305 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
2306 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
2307 stats->rx_crc_errors =
2308 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
2309 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
2310 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
2311 stats->tx_carrier_errors =
2312 PPORT_802_3_GET(pstats, a_symbol_error_during_carrier);
2313 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
2314 stats->rx_frame_errors;
2315 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
2317 /* vport multicast also counts packets that are dropped due to steering
2318 * or rx out of buffer
2321 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
2326 static void mlx5e_set_rx_mode(struct net_device *dev)
2328 struct mlx5e_priv *priv = netdev_priv(dev);
2330 queue_work(priv->wq, &priv->set_rx_mode_work);
2333 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
2335 struct mlx5e_priv *priv = netdev_priv(netdev);
2336 struct sockaddr *saddr = addr;
2338 if (!is_valid_ether_addr(saddr->sa_data))
2339 return -EADDRNOTAVAIL;
2341 netif_addr_lock_bh(netdev);
2342 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
2343 netif_addr_unlock_bh(netdev);
2345 queue_work(priv->wq, &priv->set_rx_mode_work);
2350 #define MLX5E_SET_FEATURE(netdev, feature, enable) \
2353 netdev->features |= feature; \
2355 netdev->features &= ~feature; \
2358 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
2360 static int set_feature_lro(struct net_device *netdev, bool enable)
2362 struct mlx5e_priv *priv = netdev_priv(netdev);
2363 bool was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2366 mutex_lock(&priv->state_lock);
2368 if (was_opened && (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST))
2369 mlx5e_close_locked(priv->netdev);
2371 priv->params.lro_en = enable;
2372 err = mlx5e_modify_tirs_lro(priv);
2374 netdev_err(netdev, "lro modify failed, %d\n", err);
2375 priv->params.lro_en = !enable;
2378 if (was_opened && (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST))
2379 mlx5e_open_locked(priv->netdev);
2381 mutex_unlock(&priv->state_lock);
2386 static int set_feature_vlan_filter(struct net_device *netdev, bool enable)
2388 struct mlx5e_priv *priv = netdev_priv(netdev);
2391 mlx5e_enable_vlan_filter(priv);
2393 mlx5e_disable_vlan_filter(priv);
2398 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
2400 struct mlx5e_priv *priv = netdev_priv(netdev);
2402 if (!enable && mlx5e_tc_num_filters(priv)) {
2404 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
2411 static int set_feature_rx_all(struct net_device *netdev, bool enable)
2413 struct mlx5e_priv *priv = netdev_priv(netdev);
2414 struct mlx5_core_dev *mdev = priv->mdev;
2416 return mlx5_set_port_fcs(mdev, !enable);
2419 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
2421 struct mlx5e_priv *priv = netdev_priv(netdev);
2424 mutex_lock(&priv->state_lock);
2426 priv->params.vlan_strip_disable = !enable;
2427 err = mlx5e_modify_rqs_vsd(priv, !enable);
2429 priv->params.vlan_strip_disable = enable;
2431 mutex_unlock(&priv->state_lock);
2436 #ifdef CONFIG_RFS_ACCEL
2437 static int set_feature_arfs(struct net_device *netdev, bool enable)
2439 struct mlx5e_priv *priv = netdev_priv(netdev);
2443 err = mlx5e_arfs_enable(priv);
2445 err = mlx5e_arfs_disable(priv);
2451 static int mlx5e_handle_feature(struct net_device *netdev,
2452 netdev_features_t wanted_features,
2453 netdev_features_t feature,
2454 mlx5e_feature_handler feature_handler)
2456 netdev_features_t changes = wanted_features ^ netdev->features;
2457 bool enable = !!(wanted_features & feature);
2460 if (!(changes & feature))
2463 err = feature_handler(netdev, enable);
2465 netdev_err(netdev, "%s feature 0x%llx failed err %d\n",
2466 enable ? "Enable" : "Disable", feature, err);
2470 MLX5E_SET_FEATURE(netdev, feature, enable);
2474 static int mlx5e_set_features(struct net_device *netdev,
2475 netdev_features_t features)
2479 err = mlx5e_handle_feature(netdev, features, NETIF_F_LRO,
2481 err |= mlx5e_handle_feature(netdev, features,
2482 NETIF_F_HW_VLAN_CTAG_FILTER,
2483 set_feature_vlan_filter);
2484 err |= mlx5e_handle_feature(netdev, features, NETIF_F_HW_TC,
2485 set_feature_tc_num_filters);
2486 err |= mlx5e_handle_feature(netdev, features, NETIF_F_RXALL,
2487 set_feature_rx_all);
2488 err |= mlx5e_handle_feature(netdev, features, NETIF_F_HW_VLAN_CTAG_RX,
2489 set_feature_rx_vlan);
2490 #ifdef CONFIG_RFS_ACCEL
2491 err |= mlx5e_handle_feature(netdev, features, NETIF_F_NTUPLE,
2495 return err ? -EINVAL : 0;
2498 #define MXL5_HW_MIN_MTU 64
2499 #define MXL5E_MIN_MTU (MXL5_HW_MIN_MTU + ETH_FCS_LEN)
2501 static int mlx5e_change_mtu(struct net_device *netdev, int new_mtu)
2503 struct mlx5e_priv *priv = netdev_priv(netdev);
2504 struct mlx5_core_dev *mdev = priv->mdev;
2510 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
2512 max_mtu = MLX5E_HW2SW_MTU(max_mtu);
2513 min_mtu = MLX5E_HW2SW_MTU(MXL5E_MIN_MTU);
2515 if (new_mtu > max_mtu || new_mtu < min_mtu) {
2517 "%s: Bad MTU (%d), valid range is: [%d..%d]\n",
2518 __func__, new_mtu, min_mtu, max_mtu);
2522 mutex_lock(&priv->state_lock);
2524 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2526 mlx5e_close_locked(netdev);
2528 netdev->mtu = new_mtu;
2531 err = mlx5e_open_locked(netdev);
2533 mutex_unlock(&priv->state_lock);
2538 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2542 return mlx5e_hwstamp_set(dev, ifr);
2544 return mlx5e_hwstamp_get(dev, ifr);
2550 static int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2552 struct mlx5e_priv *priv = netdev_priv(dev);
2553 struct mlx5_core_dev *mdev = priv->mdev;
2555 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
2558 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos)
2560 struct mlx5e_priv *priv = netdev_priv(dev);
2561 struct mlx5_core_dev *mdev = priv->mdev;
2563 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
2567 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2569 struct mlx5e_priv *priv = netdev_priv(dev);
2570 struct mlx5_core_dev *mdev = priv->mdev;
2572 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
2575 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
2577 struct mlx5e_priv *priv = netdev_priv(dev);
2578 struct mlx5_core_dev *mdev = priv->mdev;
2580 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
2582 static int mlx5_vport_link2ifla(u8 esw_link)
2585 case MLX5_ESW_VPORT_ADMIN_STATE_DOWN:
2586 return IFLA_VF_LINK_STATE_DISABLE;
2587 case MLX5_ESW_VPORT_ADMIN_STATE_UP:
2588 return IFLA_VF_LINK_STATE_ENABLE;
2590 return IFLA_VF_LINK_STATE_AUTO;
2593 static int mlx5_ifla_link2vport(u8 ifla_link)
2595 switch (ifla_link) {
2596 case IFLA_VF_LINK_STATE_DISABLE:
2597 return MLX5_ESW_VPORT_ADMIN_STATE_DOWN;
2598 case IFLA_VF_LINK_STATE_ENABLE:
2599 return MLX5_ESW_VPORT_ADMIN_STATE_UP;
2601 return MLX5_ESW_VPORT_ADMIN_STATE_AUTO;
2604 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
2607 struct mlx5e_priv *priv = netdev_priv(dev);
2608 struct mlx5_core_dev *mdev = priv->mdev;
2610 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
2611 mlx5_ifla_link2vport(link_state));
2614 static int mlx5e_get_vf_config(struct net_device *dev,
2615 int vf, struct ifla_vf_info *ivi)
2617 struct mlx5e_priv *priv = netdev_priv(dev);
2618 struct mlx5_core_dev *mdev = priv->mdev;
2621 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
2624 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
2628 static int mlx5e_get_vf_stats(struct net_device *dev,
2629 int vf, struct ifla_vf_stats *vf_stats)
2631 struct mlx5e_priv *priv = netdev_priv(dev);
2632 struct mlx5_core_dev *mdev = priv->mdev;
2634 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
2638 static void mlx5e_add_vxlan_port(struct net_device *netdev,
2639 struct udp_tunnel_info *ti)
2641 struct mlx5e_priv *priv = netdev_priv(netdev);
2643 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2646 if (!mlx5e_vxlan_allowed(priv->mdev))
2649 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 1);
2652 static void mlx5e_del_vxlan_port(struct net_device *netdev,
2653 struct udp_tunnel_info *ti)
2655 struct mlx5e_priv *priv = netdev_priv(netdev);
2657 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2660 if (!mlx5e_vxlan_allowed(priv->mdev))
2663 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 0);
2666 static netdev_features_t mlx5e_vxlan_features_check(struct mlx5e_priv *priv,
2667 struct sk_buff *skb,
2668 netdev_features_t features)
2670 struct udphdr *udph;
2674 switch (vlan_get_protocol(skb)) {
2675 case htons(ETH_P_IP):
2676 proto = ip_hdr(skb)->protocol;
2678 case htons(ETH_P_IPV6):
2679 proto = ipv6_hdr(skb)->nexthdr;
2685 if (proto == IPPROTO_UDP) {
2686 udph = udp_hdr(skb);
2687 port = be16_to_cpu(udph->dest);
2690 /* Verify if UDP port is being offloaded by HW */
2691 if (port && mlx5e_vxlan_lookup_port(priv, port))
2695 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
2696 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2699 static netdev_features_t mlx5e_features_check(struct sk_buff *skb,
2700 struct net_device *netdev,
2701 netdev_features_t features)
2703 struct mlx5e_priv *priv = netdev_priv(netdev);
2705 features = vlan_features_check(skb, features);
2706 features = vxlan_features_check(skb, features);
2708 /* Validate if the tunneled packet is being offloaded by HW */
2709 if (skb->encapsulation &&
2710 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
2711 return mlx5e_vxlan_features_check(priv, skb, features);
2716 static const struct net_device_ops mlx5e_netdev_ops_basic = {
2717 .ndo_open = mlx5e_open,
2718 .ndo_stop = mlx5e_close,
2719 .ndo_start_xmit = mlx5e_xmit,
2720 .ndo_setup_tc = mlx5e_ndo_setup_tc,
2721 .ndo_select_queue = mlx5e_select_queue,
2722 .ndo_get_stats64 = mlx5e_get_stats,
2723 .ndo_set_rx_mode = mlx5e_set_rx_mode,
2724 .ndo_set_mac_address = mlx5e_set_mac,
2725 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
2726 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
2727 .ndo_set_features = mlx5e_set_features,
2728 .ndo_change_mtu = mlx5e_change_mtu,
2729 .ndo_do_ioctl = mlx5e_ioctl,
2730 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
2731 #ifdef CONFIG_RFS_ACCEL
2732 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
2736 static const struct net_device_ops mlx5e_netdev_ops_sriov = {
2737 .ndo_open = mlx5e_open,
2738 .ndo_stop = mlx5e_close,
2739 .ndo_start_xmit = mlx5e_xmit,
2740 .ndo_setup_tc = mlx5e_ndo_setup_tc,
2741 .ndo_select_queue = mlx5e_select_queue,
2742 .ndo_get_stats64 = mlx5e_get_stats,
2743 .ndo_set_rx_mode = mlx5e_set_rx_mode,
2744 .ndo_set_mac_address = mlx5e_set_mac,
2745 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
2746 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
2747 .ndo_set_features = mlx5e_set_features,
2748 .ndo_change_mtu = mlx5e_change_mtu,
2749 .ndo_do_ioctl = mlx5e_ioctl,
2750 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
2751 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
2752 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
2753 .ndo_features_check = mlx5e_features_check,
2754 #ifdef CONFIG_RFS_ACCEL
2755 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
2757 .ndo_set_vf_mac = mlx5e_set_vf_mac,
2758 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
2759 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
2760 .ndo_set_vf_trust = mlx5e_set_vf_trust,
2761 .ndo_get_vf_config = mlx5e_get_vf_config,
2762 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
2763 .ndo_get_vf_stats = mlx5e_get_vf_stats,
2766 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
2768 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
2770 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
2771 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
2772 !MLX5_CAP_ETH(mdev, csum_cap) ||
2773 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
2774 !MLX5_CAP_ETH(mdev, vlan_cap) ||
2775 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
2776 MLX5_CAP_FLOWTABLE(mdev,
2777 flow_table_properties_nic_receive.max_ft_level)
2779 mlx5_core_warn(mdev,
2780 "Not creating net device, some required device capabilities are missing\n");
2783 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
2784 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
2785 if (!MLX5_CAP_GEN(mdev, cq_moderation))
2786 mlx5_core_warn(mdev, "CQ modiration is not supported\n");
2791 u16 mlx5e_get_max_inline_cap(struct mlx5_core_dev *mdev)
2793 int bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
2795 return bf_buf_size -
2796 sizeof(struct mlx5e_tx_wqe) +
2797 2 /*sizeof(mlx5e_tx_wqe.inline_hdr_start)*/;
2800 #ifdef CONFIG_MLX5_CORE_EN_DCB
2801 static void mlx5e_ets_init(struct mlx5e_priv *priv)
2805 priv->params.ets.ets_cap = mlx5_max_tc(priv->mdev) + 1;
2806 for (i = 0; i < priv->params.ets.ets_cap; i++) {
2807 priv->params.ets.tc_tx_bw[i] = MLX5E_MAX_BW_ALLOC;
2808 priv->params.ets.tc_tsa[i] = IEEE_8021QAZ_TSA_VENDOR;
2809 priv->params.ets.prio_tc[i] = i;
2812 /* tclass[prio=0]=1, tclass[prio=1]=0, tclass[prio=i]=i (for i>1) */
2813 priv->params.ets.prio_tc[0] = 1;
2814 priv->params.ets.prio_tc[1] = 0;
2818 void mlx5e_build_default_indir_rqt(struct mlx5_core_dev *mdev,
2819 u32 *indirection_rqt, int len,
2822 int node = mdev->priv.numa_node;
2823 int node_num_of_cores;
2827 node = first_online_node;
2829 node_num_of_cores = cpumask_weight(cpumask_of_node(node));
2831 if (node_num_of_cores)
2832 num_channels = min_t(int, num_channels, node_num_of_cores);
2834 for (i = 0; i < len; i++)
2835 indirection_rqt[i] = i % num_channels;
2838 static bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
2840 return MLX5_CAP_GEN(mdev, striding_rq) &&
2841 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
2842 MLX5_CAP_ETH(mdev, reg_umr_sq);
2845 static int mlx5e_get_pci_bw(struct mlx5_core_dev *mdev, u32 *pci_bw)
2847 enum pcie_link_width width;
2848 enum pci_bus_speed speed;
2851 err = pcie_get_minimum_link(mdev->pdev, &speed, &width);
2855 if (speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
2859 case PCIE_SPEED_2_5GT:
2860 *pci_bw = 2500 * width;
2862 case PCIE_SPEED_5_0GT:
2863 *pci_bw = 5000 * width;
2865 case PCIE_SPEED_8_0GT:
2866 *pci_bw = 8000 * width;
2875 static bool cqe_compress_heuristic(u32 link_speed, u32 pci_bw)
2877 return (link_speed && pci_bw &&
2878 (pci_bw < 40000) && (pci_bw < link_speed));
2881 void mlx5e_set_rx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
2883 params->rx_cq_period_mode = cq_period_mode;
2885 params->rx_cq_moderation.pkts =
2886 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
2887 params->rx_cq_moderation.usec =
2888 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
2890 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
2891 params->rx_cq_moderation.usec =
2892 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE;
2895 static void mlx5e_build_netdev_priv(struct mlx5_core_dev *mdev,
2896 struct net_device *netdev,
2899 struct mlx5e_priv *priv = netdev_priv(netdev);
2902 u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
2903 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
2904 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
2906 priv->params.log_sq_size =
2907 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
2908 priv->params.rq_wq_type = mlx5e_check_fragmented_striding_rq_cap(mdev) ?
2909 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
2910 MLX5_WQ_TYPE_LINKED_LIST;
2912 /* set CQE compression */
2913 priv->params.rx_cqe_compress_admin = false;
2914 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
2915 MLX5_CAP_GEN(mdev, vport_group_manager)) {
2916 mlx5e_get_max_linkspeed(mdev, &link_speed);
2917 mlx5e_get_pci_bw(mdev, &pci_bw);
2918 mlx5_core_dbg(mdev, "Max link speed = %d, PCI BW = %d\n",
2919 link_speed, pci_bw);
2920 priv->params.rx_cqe_compress_admin =
2921 cqe_compress_heuristic(link_speed, pci_bw);
2924 priv->params.rx_cqe_compress = priv->params.rx_cqe_compress_admin;
2926 switch (priv->params.rq_wq_type) {
2927 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2928 priv->params.log_rq_size = MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE_MPW;
2929 priv->params.mpwqe_log_stride_sz =
2930 priv->params.rx_cqe_compress ?
2931 MLX5_MPWRQ_LOG_STRIDE_SIZE_CQE_COMPRESS :
2932 MLX5_MPWRQ_LOG_STRIDE_SIZE;
2933 priv->params.mpwqe_log_num_strides = MLX5_MPWRQ_LOG_WQE_SZ -
2934 priv->params.mpwqe_log_stride_sz;
2935 priv->params.lro_en = true;
2937 default: /* MLX5_WQ_TYPE_LINKED_LIST */
2938 priv->params.log_rq_size = MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
2941 mlx5_core_info(mdev,
2942 "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
2943 priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
2944 BIT(priv->params.log_rq_size),
2945 BIT(priv->params.mpwqe_log_stride_sz),
2946 priv->params.rx_cqe_compress_admin);
2948 priv->params.min_rx_wqes = mlx5_min_rx_wqes(priv->params.rq_wq_type,
2949 BIT(priv->params.log_rq_size));
2951 priv->params.rx_am_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
2952 mlx5e_set_rx_cq_mode_params(&priv->params, cq_period_mode);
2954 priv->params.tx_cq_moderation.usec =
2955 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
2956 priv->params.tx_cq_moderation.pkts =
2957 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
2958 priv->params.tx_max_inline = mlx5e_get_max_inline_cap(mdev);
2959 priv->params.num_tc = 1;
2960 priv->params.rss_hfunc = ETH_RSS_HASH_XOR;
2962 netdev_rss_key_fill(priv->params.toeplitz_hash_key,
2963 sizeof(priv->params.toeplitz_hash_key));
2965 mlx5e_build_default_indir_rqt(mdev, priv->params.indirection_rqt,
2966 MLX5E_INDIR_RQT_SIZE, num_channels);
2968 priv->params.lro_wqe_sz =
2969 MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
2971 /* Initialize pflags */
2972 MLX5E_SET_PRIV_FLAG(priv, MLX5E_PFLAG_RX_CQE_BASED_MODER,
2973 priv->params.rx_cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
2976 priv->netdev = netdev;
2977 priv->params.num_channels = num_channels;
2979 #ifdef CONFIG_MLX5_CORE_EN_DCB
2980 mlx5e_ets_init(priv);
2983 mutex_init(&priv->state_lock);
2985 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
2986 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
2987 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
2990 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
2992 struct mlx5e_priv *priv = netdev_priv(netdev);
2994 mlx5_query_nic_vport_mac_address(priv->mdev, 0, netdev->dev_addr);
2995 if (is_zero_ether_addr(netdev->dev_addr) &&
2996 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
2997 eth_hw_addr_random(netdev);
2998 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
3002 static void mlx5e_build_netdev(struct net_device *netdev)
3004 struct mlx5e_priv *priv = netdev_priv(netdev);
3005 struct mlx5_core_dev *mdev = priv->mdev;
3009 SET_NETDEV_DEV(netdev, &mdev->pdev->dev);
3011 if (MLX5_CAP_GEN(mdev, vport_group_manager)) {
3012 netdev->netdev_ops = &mlx5e_netdev_ops_sriov;
3013 #ifdef CONFIG_MLX5_CORE_EN_DCB
3014 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
3017 netdev->netdev_ops = &mlx5e_netdev_ops_basic;
3020 netdev->watchdog_timeo = 15 * HZ;
3022 netdev->ethtool_ops = &mlx5e_ethtool_ops;
3024 netdev->vlan_features |= NETIF_F_SG;
3025 netdev->vlan_features |= NETIF_F_IP_CSUM;
3026 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
3027 netdev->vlan_features |= NETIF_F_GRO;
3028 netdev->vlan_features |= NETIF_F_TSO;
3029 netdev->vlan_features |= NETIF_F_TSO6;
3030 netdev->vlan_features |= NETIF_F_RXCSUM;
3031 netdev->vlan_features |= NETIF_F_RXHASH;
3033 if (!!MLX5_CAP_ETH(mdev, lro_cap))
3034 netdev->vlan_features |= NETIF_F_LRO;
3036 netdev->hw_features = netdev->vlan_features;
3037 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
3038 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
3039 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3041 if (mlx5e_vxlan_allowed(mdev)) {
3042 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
3043 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3044 NETIF_F_GSO_PARTIAL;
3045 netdev->hw_enc_features |= NETIF_F_IP_CSUM;
3046 netdev->hw_enc_features |= NETIF_F_IPV6_CSUM;
3047 netdev->hw_enc_features |= NETIF_F_TSO;
3048 netdev->hw_enc_features |= NETIF_F_TSO6;
3049 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL;
3050 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
3051 NETIF_F_GSO_PARTIAL;
3052 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
3055 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
3058 netdev->hw_features |= NETIF_F_RXALL;
3060 netdev->features = netdev->hw_features;
3061 if (!priv->params.lro_en)
3062 netdev->features &= ~NETIF_F_LRO;
3065 netdev->features &= ~NETIF_F_RXALL;
3067 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
3068 if (FT_CAP(flow_modify_en) &&
3069 FT_CAP(modify_root) &&
3070 FT_CAP(identified_miss_table_mode) &&
3071 FT_CAP(flow_table_modify)) {
3072 netdev->hw_features |= NETIF_F_HW_TC;
3073 #ifdef CONFIG_RFS_ACCEL
3074 netdev->hw_features |= NETIF_F_NTUPLE;
3078 netdev->features |= NETIF_F_HIGHDMA;
3080 netdev->priv_flags |= IFF_UNICAST_FLT;
3082 mlx5e_set_netdev_dev_addr(netdev);
3085 static int mlx5e_create_mkey(struct mlx5e_priv *priv, u32 pdn,
3086 struct mlx5_core_mkey *mkey)
3088 struct mlx5_core_dev *mdev = priv->mdev;
3089 struct mlx5_create_mkey_mbox_in *in;
3092 in = mlx5_vzalloc(sizeof(*in));
3096 in->seg.flags = MLX5_PERM_LOCAL_WRITE |
3097 MLX5_PERM_LOCAL_READ |
3098 MLX5_ACCESS_MODE_PA;
3099 in->seg.flags_pd = cpu_to_be32(pdn | MLX5_MKEY_LEN64);
3100 in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
3102 err = mlx5_core_create_mkey(mdev, mkey, in, sizeof(*in), NULL, NULL,
3110 static void mlx5e_create_q_counter(struct mlx5e_priv *priv)
3112 struct mlx5_core_dev *mdev = priv->mdev;
3115 err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
3117 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
3118 priv->q_counter = 0;
3122 static void mlx5e_destroy_q_counter(struct mlx5e_priv *priv)
3124 if (!priv->q_counter)
3127 mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
3130 static int mlx5e_create_umr_mkey(struct mlx5e_priv *priv)
3132 struct mlx5_core_dev *mdev = priv->mdev;
3133 struct mlx5_create_mkey_mbox_in *in;
3134 struct mlx5_mkey_seg *mkc;
3135 int inlen = sizeof(*in);
3137 mlx5e_get_max_num_channels(mdev) * MLX5_CHANNEL_MAX_NUM_MTTS;
3140 in = mlx5_vzalloc(inlen);
3145 mkc->status = MLX5_MKEY_STATUS_FREE;
3146 mkc->flags = MLX5_PERM_UMR_EN |
3147 MLX5_PERM_LOCAL_READ |
3148 MLX5_PERM_LOCAL_WRITE |
3149 MLX5_ACCESS_MODE_MTT;
3151 mkc->qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
3152 mkc->flags_pd = cpu_to_be32(priv->pdn);
3153 mkc->len = cpu_to_be64(npages << PAGE_SHIFT);
3154 mkc->xlt_oct_size = cpu_to_be32(mlx5e_get_mtt_octw(npages));
3155 mkc->log2_page_size = PAGE_SHIFT;
3157 err = mlx5_core_create_mkey(mdev, &priv->umr_mkey, in, inlen, NULL,
3165 static void *mlx5e_create_netdev(struct mlx5_core_dev *mdev)
3167 struct net_device *netdev;
3168 struct mlx5e_priv *priv;
3169 int nch = mlx5e_get_max_num_channels(mdev);
3172 if (mlx5e_check_required_hca_cap(mdev))
3175 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
3176 nch * MLX5E_MAX_NUM_TC,
3179 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
3183 mlx5e_build_netdev_priv(mdev, netdev, nch);
3184 mlx5e_build_netdev(netdev);
3186 netif_carrier_off(netdev);
3188 priv = netdev_priv(netdev);
3190 priv->wq = create_singlethread_workqueue("mlx5e");
3192 goto err_free_netdev;
3194 err = mlx5_alloc_map_uar(mdev, &priv->cq_uar, false);
3196 mlx5_core_err(mdev, "alloc_map uar failed, %d\n", err);
3197 goto err_destroy_wq;
3200 err = mlx5_core_alloc_pd(mdev, &priv->pdn);
3202 mlx5_core_err(mdev, "alloc pd failed, %d\n", err);
3203 goto err_unmap_free_uar;
3206 err = mlx5_core_alloc_transport_domain(mdev, &priv->tdn);
3208 mlx5_core_err(mdev, "alloc td failed, %d\n", err);
3209 goto err_dealloc_pd;
3212 err = mlx5e_create_mkey(priv, priv->pdn, &priv->mkey);
3214 mlx5_core_err(mdev, "create mkey failed, %d\n", err);
3215 goto err_dealloc_transport_domain;
3218 err = mlx5e_create_umr_mkey(priv);
3220 mlx5_core_err(mdev, "create umr mkey failed, %d\n", err);
3221 goto err_destroy_mkey;
3224 err = mlx5e_create_tises(priv);
3226 mlx5_core_warn(mdev, "create tises failed, %d\n", err);
3227 goto err_destroy_umr_mkey;
3230 err = mlx5e_open_drop_rq(priv);
3232 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
3233 goto err_destroy_tises;
3236 err = mlx5e_create_rqts(priv);
3238 mlx5_core_warn(mdev, "create rqts failed, %d\n", err);
3239 goto err_close_drop_rq;
3242 err = mlx5e_create_tirs(priv);
3244 mlx5_core_warn(mdev, "create tirs failed, %d\n", err);
3245 goto err_destroy_rqts;
3248 err = mlx5e_create_flow_steering(priv);
3250 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
3251 goto err_destroy_tirs;
3254 mlx5e_create_q_counter(priv);
3256 mlx5e_init_l2_addr(priv);
3258 mlx5e_vxlan_init(priv);
3260 err = mlx5e_tc_init(priv);
3262 goto err_dealloc_q_counters;
3264 #ifdef CONFIG_MLX5_CORE_EN_DCB
3265 mlx5e_dcbnl_ieee_setets_core(priv, &priv->params.ets);
3268 err = register_netdev(netdev);
3270 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
3271 goto err_tc_cleanup;
3274 if (mlx5e_vxlan_allowed(mdev)) {
3276 udp_tunnel_get_rx_info(netdev);
3280 mlx5e_enable_async_events(priv);
3281 queue_work(priv->wq, &priv->set_rx_mode_work);
3286 mlx5e_tc_cleanup(priv);
3288 err_dealloc_q_counters:
3289 mlx5e_destroy_q_counter(priv);
3290 mlx5e_destroy_flow_steering(priv);
3293 mlx5e_destroy_tirs(priv);
3296 mlx5e_destroy_rqts(priv);
3299 mlx5e_close_drop_rq(priv);
3302 mlx5e_destroy_tises(priv);
3304 err_destroy_umr_mkey:
3305 mlx5_core_destroy_mkey(mdev, &priv->umr_mkey);
3308 mlx5_core_destroy_mkey(mdev, &priv->mkey);
3310 err_dealloc_transport_domain:
3311 mlx5_core_dealloc_transport_domain(mdev, priv->tdn);
3314 mlx5_core_dealloc_pd(mdev, priv->pdn);
3317 mlx5_unmap_free_uar(mdev, &priv->cq_uar);
3320 destroy_workqueue(priv->wq);
3323 free_netdev(netdev);
3328 static void mlx5e_destroy_netdev(struct mlx5_core_dev *mdev, void *vpriv)
3330 struct mlx5e_priv *priv = vpriv;
3331 struct net_device *netdev = priv->netdev;
3333 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
3335 queue_work(priv->wq, &priv->set_rx_mode_work);
3336 mlx5e_disable_async_events(priv);
3337 flush_workqueue(priv->wq);
3338 if (test_bit(MLX5_INTERFACE_STATE_SHUTDOWN, &mdev->intf_state)) {
3339 netif_device_detach(netdev);
3340 mlx5e_close(netdev);
3342 unregister_netdev(netdev);
3345 mlx5e_tc_cleanup(priv);
3346 mlx5e_vxlan_cleanup(priv);
3347 mlx5e_destroy_q_counter(priv);
3348 mlx5e_destroy_flow_steering(priv);
3349 mlx5e_destroy_tirs(priv);
3350 mlx5e_destroy_rqts(priv);
3351 mlx5e_close_drop_rq(priv);
3352 mlx5e_destroy_tises(priv);
3353 mlx5_core_destroy_mkey(priv->mdev, &priv->umr_mkey);
3354 mlx5_core_destroy_mkey(priv->mdev, &priv->mkey);
3355 mlx5_core_dealloc_transport_domain(priv->mdev, priv->tdn);
3356 mlx5_core_dealloc_pd(priv->mdev, priv->pdn);
3357 mlx5_unmap_free_uar(priv->mdev, &priv->cq_uar);
3358 cancel_delayed_work_sync(&priv->update_stats_work);
3359 destroy_workqueue(priv->wq);
3361 if (!test_bit(MLX5_INTERFACE_STATE_SHUTDOWN, &mdev->intf_state))
3362 free_netdev(netdev);
3365 static void *mlx5e_get_netdev(void *vpriv)
3367 struct mlx5e_priv *priv = vpriv;
3369 return priv->netdev;
3372 static struct mlx5_interface mlx5e_interface = {
3373 .add = mlx5e_create_netdev,
3374 .remove = mlx5e_destroy_netdev,
3375 .event = mlx5e_async_event,
3376 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
3377 .get_dev = mlx5e_get_netdev,
3380 void mlx5e_init(void)
3382 mlx5e_build_ptys2ethtool_map();
3383 mlx5_register_interface(&mlx5e_interface);
3386 void mlx5e_cleanup(void)
3388 mlx5_unregister_interface(&mlx5e_interface);