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->lro_packets += rq_stats->lro_packets;
111 s->lro_bytes += rq_stats->lro_bytes;
112 s->rx_csum_none += rq_stats->csum_none;
113 s->rx_csum_sw += rq_stats->csum_sw;
114 s->rx_csum_inner += rq_stats->csum_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->tso_packets += sq_stats->tso_packets;
128 s->tso_bytes += sq_stats->tso_bytes;
129 s->tso_inner_packets += sq_stats->tso_inner_packets;
130 s->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_inner += sq_stats->csum_offload_inner;
135 tx_offload_none += sq_stats->csum_offload_none;
139 /* Update calculated offload counters */
140 s->tx_csum_offload = s->tx_packets - tx_offload_none - s->tx_csum_inner;
141 s->rx_csum_good = s->rx_packets - s->rx_csum_none -
144 s->link_down_events = MLX5_GET(ppcnt_reg,
145 priv->stats.pport.phy_counters,
146 counter_set.phys_layer_cntrs.link_down_events);
149 static void mlx5e_update_vport_counters(struct mlx5e_priv *priv)
151 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
152 u32 *out = (u32 *)priv->stats.vport.query_vport_out;
153 u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)];
154 struct mlx5_core_dev *mdev = priv->mdev;
156 memset(in, 0, sizeof(in));
158 MLX5_SET(query_vport_counter_in, in, opcode,
159 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
160 MLX5_SET(query_vport_counter_in, in, op_mod, 0);
161 MLX5_SET(query_vport_counter_in, in, other_vport, 0);
163 memset(out, 0, outlen);
165 mlx5_cmd_exec(mdev, in, sizeof(in), out, outlen);
168 static void mlx5e_update_pport_counters(struct mlx5e_priv *priv)
170 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
171 struct mlx5_core_dev *mdev = priv->mdev;
172 int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
177 in = mlx5_vzalloc(sz);
181 MLX5_SET(ppcnt_reg, in, local_port, 1);
183 out = pstats->IEEE_802_3_counters;
184 MLX5_SET(ppcnt_reg, in, grp, MLX5_IEEE_802_3_COUNTERS_GROUP);
185 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
187 out = pstats->RFC_2863_counters;
188 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2863_COUNTERS_GROUP);
189 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
191 out = pstats->RFC_2819_counters;
192 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2819_COUNTERS_GROUP);
193 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
195 out = pstats->phy_counters;
196 MLX5_SET(ppcnt_reg, in, grp, MLX5_PHYSICAL_LAYER_COUNTERS_GROUP);
197 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
199 MLX5_SET(ppcnt_reg, in, grp, MLX5_PER_PRIORITY_COUNTERS_GROUP);
200 for (prio = 0; prio < NUM_PPORT_PRIO; prio++) {
201 out = pstats->per_prio_counters[prio];
202 MLX5_SET(ppcnt_reg, in, prio_tc, prio);
203 mlx5_core_access_reg(mdev, in, sz, out, sz,
204 MLX5_REG_PPCNT, 0, 0);
211 static void mlx5e_update_q_counter(struct mlx5e_priv *priv)
213 struct mlx5e_qcounter_stats *qcnt = &priv->stats.qcnt;
215 if (!priv->q_counter)
218 mlx5_core_query_out_of_buffer(priv->mdev, priv->q_counter,
219 &qcnt->rx_out_of_buffer);
222 void mlx5e_update_stats(struct mlx5e_priv *priv)
224 mlx5e_update_q_counter(priv);
225 mlx5e_update_vport_counters(priv);
226 mlx5e_update_pport_counters(priv);
227 mlx5e_update_sw_counters(priv);
230 static void mlx5e_update_stats_work(struct work_struct *work)
232 struct delayed_work *dwork = to_delayed_work(work);
233 struct mlx5e_priv *priv = container_of(dwork, struct mlx5e_priv,
235 mutex_lock(&priv->state_lock);
236 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
237 mlx5e_update_stats(priv);
238 queue_delayed_work(priv->wq, dwork,
239 msecs_to_jiffies(MLX5E_UPDATE_STATS_INTERVAL));
241 mutex_unlock(&priv->state_lock);
244 static void mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
245 enum mlx5_dev_event event, unsigned long param)
247 struct mlx5e_priv *priv = vpriv;
249 if (!test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state))
253 case MLX5_DEV_EVENT_PORT_UP:
254 case MLX5_DEV_EVENT_PORT_DOWN:
255 queue_work(priv->wq, &priv->update_carrier_work);
263 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
265 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state);
268 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
270 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state);
271 synchronize_irq(mlx5_get_msix_vec(priv->mdev, MLX5_EQ_VEC_ASYNC));
274 #define MLX5E_HW2SW_MTU(hwmtu) (hwmtu - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
275 #define MLX5E_SW2HW_MTU(swmtu) (swmtu + (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
277 static int mlx5e_create_rq(struct mlx5e_channel *c,
278 struct mlx5e_rq_param *param,
281 struct mlx5e_priv *priv = c->priv;
282 struct mlx5_core_dev *mdev = priv->mdev;
283 void *rqc = param->rqc;
284 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
290 param->wq.db_numa_node = cpu_to_node(c->cpu);
292 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
297 rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
299 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
301 switch (priv->params.rq_wq_type) {
302 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
303 rq->wqe_info = kzalloc_node(wq_sz * sizeof(*rq->wqe_info),
304 GFP_KERNEL, cpu_to_node(c->cpu));
307 goto err_rq_wq_destroy;
309 rq->handle_rx_cqe = mlx5e_handle_rx_cqe_mpwrq;
310 rq->alloc_wqe = mlx5e_alloc_rx_mpwqe;
312 rq->mpwqe_stride_sz = BIT(priv->params.mpwqe_log_stride_sz);
313 rq->mpwqe_num_strides = BIT(priv->params.mpwqe_log_num_strides);
314 rq->wqe_sz = rq->mpwqe_stride_sz * rq->mpwqe_num_strides;
315 byte_count = rq->wqe_sz;
317 default: /* MLX5_WQ_TYPE_LINKED_LIST */
318 rq->skb = kzalloc_node(wq_sz * sizeof(*rq->skb), GFP_KERNEL,
319 cpu_to_node(c->cpu));
322 goto err_rq_wq_destroy;
324 rq->handle_rx_cqe = mlx5e_handle_rx_cqe;
325 rq->alloc_wqe = mlx5e_alloc_rx_wqe;
327 rq->wqe_sz = (priv->params.lro_en) ?
328 priv->params.lro_wqe_sz :
329 MLX5E_SW2HW_MTU(priv->netdev->mtu);
330 rq->wqe_sz = SKB_DATA_ALIGN(rq->wqe_sz);
331 byte_count = rq->wqe_sz;
332 byte_count |= MLX5_HW_START_PADDING;
335 for (i = 0; i < wq_sz; i++) {
336 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
338 wqe->data.byte_count = cpu_to_be32(byte_count);
341 INIT_WORK(&rq->am.work, mlx5e_rx_am_work);
342 rq->am.mode = priv->params.rx_cq_period_mode;
344 rq->wq_type = priv->params.rq_wq_type;
346 rq->netdev = c->netdev;
347 rq->tstamp = &priv->tstamp;
351 rq->mkey_be = c->mkey_be;
352 rq->umr_mkey_be = cpu_to_be32(c->priv->umr_mkey.key);
357 mlx5_wq_destroy(&rq->wq_ctrl);
362 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
364 switch (rq->wq_type) {
365 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
368 default: /* MLX5_WQ_TYPE_LINKED_LIST */
372 mlx5_wq_destroy(&rq->wq_ctrl);
375 static int mlx5e_enable_rq(struct mlx5e_rq *rq, struct mlx5e_rq_param *param)
377 struct mlx5e_priv *priv = rq->priv;
378 struct mlx5_core_dev *mdev = priv->mdev;
386 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
387 sizeof(u64) * rq->wq_ctrl.buf.npages;
388 in = mlx5_vzalloc(inlen);
392 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
393 wq = MLX5_ADDR_OF(rqc, rqc, wq);
395 memcpy(rqc, param->rqc, sizeof(param->rqc));
397 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
398 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
399 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
400 MLX5_SET(rqc, rqc, vsd, priv->params.vlan_strip_disable);
401 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
402 MLX5_ADAPTER_PAGE_SHIFT);
403 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
405 mlx5_fill_page_array(&rq->wq_ctrl.buf,
406 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
408 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
415 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
418 struct mlx5e_channel *c = rq->channel;
419 struct mlx5e_priv *priv = c->priv;
420 struct mlx5_core_dev *mdev = priv->mdev;
427 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
428 in = mlx5_vzalloc(inlen);
432 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
434 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
435 MLX5_SET(rqc, rqc, state, next_state);
437 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
444 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
446 struct mlx5e_channel *c = rq->channel;
447 struct mlx5e_priv *priv = c->priv;
448 struct mlx5_core_dev *mdev = priv->mdev;
455 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
456 in = mlx5_vzalloc(inlen);
460 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
462 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
463 MLX5_SET64(modify_rq_in, in, modify_bitmask, MLX5_RQ_BITMASK_VSD);
464 MLX5_SET(rqc, rqc, vsd, vsd);
465 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
467 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
474 static void mlx5e_disable_rq(struct mlx5e_rq *rq)
476 mlx5_core_destroy_rq(rq->priv->mdev, rq->rqn);
479 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq)
481 unsigned long exp_time = jiffies + msecs_to_jiffies(20000);
482 struct mlx5e_channel *c = rq->channel;
483 struct mlx5e_priv *priv = c->priv;
484 struct mlx5_wq_ll *wq = &rq->wq;
486 while (time_before(jiffies, exp_time)) {
487 if (wq->cur_sz >= priv->params.min_rx_wqes)
496 static int mlx5e_open_rq(struct mlx5e_channel *c,
497 struct mlx5e_rq_param *param,
500 struct mlx5e_sq *sq = &c->icosq;
501 u16 pi = sq->pc & sq->wq.sz_m1;
504 err = mlx5e_create_rq(c, param, rq);
508 err = mlx5e_enable_rq(rq, param);
512 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
516 if (param->am_enabled)
517 set_bit(MLX5E_RQ_STATE_AM, &c->rq.state);
519 set_bit(MLX5E_RQ_STATE_POST_WQES_ENABLE, &rq->state);
521 sq->ico_wqe_info[pi].opcode = MLX5_OPCODE_NOP;
522 sq->ico_wqe_info[pi].num_wqebbs = 1;
523 mlx5e_send_nop(sq, true); /* trigger mlx5e_post_rx_wqes() */
528 mlx5e_disable_rq(rq);
530 mlx5e_destroy_rq(rq);
535 static void mlx5e_close_rq(struct mlx5e_rq *rq)
537 clear_bit(MLX5E_RQ_STATE_POST_WQES_ENABLE, &rq->state);
538 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
540 mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RDY, MLX5_RQC_STATE_ERR);
541 while (!mlx5_wq_ll_is_empty(&rq->wq))
544 /* avoid destroying rq before mlx5e_poll_rx_cq() is done with it */
545 napi_synchronize(&rq->channel->napi);
547 cancel_work_sync(&rq->am.work);
549 mlx5e_disable_rq(rq);
550 mlx5e_destroy_rq(rq);
553 static void mlx5e_free_sq_db(struct mlx5e_sq *sq)
560 static int mlx5e_alloc_sq_db(struct mlx5e_sq *sq, int numa)
562 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
563 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
565 sq->skb = kzalloc_node(wq_sz * sizeof(*sq->skb), GFP_KERNEL, numa);
566 sq->dma_fifo = kzalloc_node(df_sz * sizeof(*sq->dma_fifo), GFP_KERNEL,
568 sq->wqe_info = kzalloc_node(wq_sz * sizeof(*sq->wqe_info), GFP_KERNEL,
571 if (!sq->skb || !sq->dma_fifo || !sq->wqe_info) {
572 mlx5e_free_sq_db(sq);
576 sq->dma_fifo_mask = df_sz - 1;
581 static int mlx5e_create_sq(struct mlx5e_channel *c,
583 struct mlx5e_sq_param *param,
586 struct mlx5e_priv *priv = c->priv;
587 struct mlx5_core_dev *mdev = priv->mdev;
589 void *sqc = param->sqc;
590 void *sqc_wq = MLX5_ADDR_OF(sqc, sqc, wq);
593 err = mlx5_alloc_map_uar(mdev, &sq->uar, true);
597 param->wq.db_numa_node = cpu_to_node(c->cpu);
599 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq,
602 goto err_unmap_free_uar;
604 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
605 if (sq->uar.bf_map) {
606 set_bit(MLX5E_SQ_STATE_BF_ENABLE, &sq->state);
607 sq->uar_map = sq->uar.bf_map;
609 sq->uar_map = sq->uar.map;
611 sq->bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
612 sq->max_inline = param->max_inline;
614 err = mlx5e_alloc_sq_db(sq, cpu_to_node(c->cpu));
616 goto err_sq_wq_destroy;
619 u8 wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
621 sq->ico_wqe_info = kzalloc_node(sizeof(*sq->ico_wqe_info) *
624 cpu_to_node(c->cpu));
625 if (!sq->ico_wqe_info) {
632 txq_ix = c->ix + tc * priv->params.num_channels;
633 sq->txq = netdev_get_tx_queue(priv->netdev, txq_ix);
634 priv->txq_to_sq_map[txq_ix] = sq;
638 sq->tstamp = &priv->tstamp;
639 sq->mkey_be = c->mkey_be;
642 sq->edge = (sq->wq.sz_m1 + 1) - MLX5_SEND_WQE_MAX_WQEBBS;
643 sq->bf_budget = MLX5E_SQ_BF_BUDGET;
648 mlx5e_free_sq_db(sq);
651 mlx5_wq_destroy(&sq->wq_ctrl);
654 mlx5_unmap_free_uar(mdev, &sq->uar);
659 static void mlx5e_destroy_sq(struct mlx5e_sq *sq)
661 struct mlx5e_channel *c = sq->channel;
662 struct mlx5e_priv *priv = c->priv;
664 kfree(sq->ico_wqe_info);
665 mlx5e_free_sq_db(sq);
666 mlx5_wq_destroy(&sq->wq_ctrl);
667 mlx5_unmap_free_uar(priv->mdev, &sq->uar);
670 static int mlx5e_enable_sq(struct mlx5e_sq *sq, struct mlx5e_sq_param *param)
672 struct mlx5e_channel *c = sq->channel;
673 struct mlx5e_priv *priv = c->priv;
674 struct mlx5_core_dev *mdev = priv->mdev;
682 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
683 sizeof(u64) * sq->wq_ctrl.buf.npages;
684 in = mlx5_vzalloc(inlen);
688 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
689 wq = MLX5_ADDR_OF(sqc, sqc, wq);
691 memcpy(sqc, param->sqc, sizeof(param->sqc));
693 MLX5_SET(sqc, sqc, tis_num_0, param->icosq ? 0 : priv->tisn[sq->tc]);
694 MLX5_SET(sqc, sqc, cqn, sq->cq.mcq.cqn);
695 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
696 MLX5_SET(sqc, sqc, tis_lst_sz, param->icosq ? 0 : 1);
697 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
699 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
700 MLX5_SET(wq, wq, uar_page, sq->uar.index);
701 MLX5_SET(wq, wq, log_wq_pg_sz, sq->wq_ctrl.buf.page_shift -
702 MLX5_ADAPTER_PAGE_SHIFT);
703 MLX5_SET64(wq, wq, dbr_addr, sq->wq_ctrl.db.dma);
705 mlx5_fill_page_array(&sq->wq_ctrl.buf,
706 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
708 err = mlx5_core_create_sq(mdev, in, inlen, &sq->sqn);
715 static int mlx5e_modify_sq(struct mlx5e_sq *sq, int curr_state,
716 int next_state, bool update_rl, int rl_index)
718 struct mlx5e_channel *c = sq->channel;
719 struct mlx5e_priv *priv = c->priv;
720 struct mlx5_core_dev *mdev = priv->mdev;
727 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
728 in = mlx5_vzalloc(inlen);
732 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
734 MLX5_SET(modify_sq_in, in, sq_state, curr_state);
735 MLX5_SET(sqc, sqc, state, next_state);
736 if (update_rl && next_state == MLX5_SQC_STATE_RDY) {
737 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
738 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
741 err = mlx5_core_modify_sq(mdev, sq->sqn, in, inlen);
748 static void mlx5e_disable_sq(struct mlx5e_sq *sq)
750 struct mlx5e_channel *c = sq->channel;
751 struct mlx5e_priv *priv = c->priv;
752 struct mlx5_core_dev *mdev = priv->mdev;
754 mlx5_core_destroy_sq(mdev, sq->sqn);
756 mlx5_rl_remove_rate(mdev, sq->rate_limit);
759 static int mlx5e_open_sq(struct mlx5e_channel *c,
761 struct mlx5e_sq_param *param,
766 err = mlx5e_create_sq(c, tc, param, sq);
770 err = mlx5e_enable_sq(sq, param);
774 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST, MLX5_SQC_STATE_RDY,
780 set_bit(MLX5E_SQ_STATE_WAKE_TXQ_ENABLE, &sq->state);
781 netdev_tx_reset_queue(sq->txq);
782 netif_tx_start_queue(sq->txq);
788 mlx5e_disable_sq(sq);
790 mlx5e_destroy_sq(sq);
795 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
797 __netif_tx_lock_bh(txq);
798 netif_tx_stop_queue(txq);
799 __netif_tx_unlock_bh(txq);
802 static void mlx5e_close_sq(struct mlx5e_sq *sq)
805 clear_bit(MLX5E_SQ_STATE_WAKE_TXQ_ENABLE, &sq->state);
806 /* prevent netif_tx_wake_queue */
807 napi_synchronize(&sq->channel->napi);
808 netif_tx_disable_queue(sq->txq);
810 /* ensure hw is notified of all pending wqes */
811 if (mlx5e_sq_has_room_for(sq, 1))
812 mlx5e_send_nop(sq, true);
814 mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY, MLX5_SQC_STATE_ERR,
818 while (sq->cc != sq->pc) /* wait till sq is empty */
821 /* avoid destroying sq before mlx5e_poll_tx_cq() is done with it */
822 napi_synchronize(&sq->channel->napi);
824 mlx5e_disable_sq(sq);
825 mlx5e_destroy_sq(sq);
828 static int mlx5e_create_cq(struct mlx5e_channel *c,
829 struct mlx5e_cq_param *param,
832 struct mlx5e_priv *priv = c->priv;
833 struct mlx5_core_dev *mdev = priv->mdev;
834 struct mlx5_core_cq *mcq = &cq->mcq;
840 param->wq.buf_numa_node = cpu_to_node(c->cpu);
841 param->wq.db_numa_node = cpu_to_node(c->cpu);
842 param->eq_ix = c->ix;
844 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
849 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
854 mcq->set_ci_db = cq->wq_ctrl.db.db;
855 mcq->arm_db = cq->wq_ctrl.db.db + 1;
858 mcq->vector = param->eq_ix;
859 mcq->comp = mlx5e_completion_event;
860 mcq->event = mlx5e_cq_error_event;
862 mcq->uar = &priv->cq_uar;
864 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
865 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
876 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
878 mlx5_wq_destroy(&cq->wq_ctrl);
881 static int mlx5e_enable_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
883 struct mlx5e_priv *priv = cq->priv;
884 struct mlx5_core_dev *mdev = priv->mdev;
885 struct mlx5_core_cq *mcq = &cq->mcq;
890 unsigned int irqn_not_used;
894 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
895 sizeof(u64) * cq->wq_ctrl.buf.npages;
896 in = mlx5_vzalloc(inlen);
900 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
902 memcpy(cqc, param->cqc, sizeof(param->cqc));
904 mlx5_fill_page_array(&cq->wq_ctrl.buf,
905 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
907 mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
909 MLX5_SET(cqc, cqc, cq_period_mode, param->cq_period_mode);
910 MLX5_SET(cqc, cqc, c_eqn, eqn);
911 MLX5_SET(cqc, cqc, uar_page, mcq->uar->index);
912 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
913 MLX5_ADAPTER_PAGE_SHIFT);
914 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
916 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
928 static void mlx5e_disable_cq(struct mlx5e_cq *cq)
930 struct mlx5e_priv *priv = cq->priv;
931 struct mlx5_core_dev *mdev = priv->mdev;
933 mlx5_core_destroy_cq(mdev, &cq->mcq);
936 static int mlx5e_open_cq(struct mlx5e_channel *c,
937 struct mlx5e_cq_param *param,
939 struct mlx5e_cq_moder moderation)
942 struct mlx5e_priv *priv = c->priv;
943 struct mlx5_core_dev *mdev = priv->mdev;
945 err = mlx5e_create_cq(c, param, cq);
949 err = mlx5e_enable_cq(cq, param);
953 if (MLX5_CAP_GEN(mdev, cq_moderation))
954 mlx5_core_modify_cq_moderation(mdev, &cq->mcq,
960 mlx5e_destroy_cq(cq);
965 static void mlx5e_close_cq(struct mlx5e_cq *cq)
967 mlx5e_disable_cq(cq);
968 mlx5e_destroy_cq(cq);
971 static int mlx5e_get_cpu(struct mlx5e_priv *priv, int ix)
973 return cpumask_first(priv->mdev->priv.irq_info[ix].mask);
976 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
977 struct mlx5e_channel_param *cparam)
979 struct mlx5e_priv *priv = c->priv;
983 for (tc = 0; tc < c->num_tc; tc++) {
984 err = mlx5e_open_cq(c, &cparam->tx_cq, &c->sq[tc].cq,
985 priv->params.tx_cq_moderation);
987 goto err_close_tx_cqs;
993 for (tc--; tc >= 0; tc--)
994 mlx5e_close_cq(&c->sq[tc].cq);
999 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1003 for (tc = 0; tc < c->num_tc; tc++)
1004 mlx5e_close_cq(&c->sq[tc].cq);
1007 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1008 struct mlx5e_channel_param *cparam)
1013 for (tc = 0; tc < c->num_tc; tc++) {
1014 err = mlx5e_open_sq(c, tc, &cparam->sq, &c->sq[tc]);
1022 for (tc--; tc >= 0; tc--)
1023 mlx5e_close_sq(&c->sq[tc]);
1028 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1032 for (tc = 0; tc < c->num_tc; tc++)
1033 mlx5e_close_sq(&c->sq[tc]);
1036 static void mlx5e_build_channeltc_to_txq_map(struct mlx5e_priv *priv, int ix)
1040 for (i = 0; i < MLX5E_MAX_NUM_TC; i++)
1041 priv->channeltc_to_txq_map[ix][i] =
1042 ix + i * priv->params.num_channels;
1045 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1046 struct mlx5e_sq *sq, u32 rate)
1048 struct mlx5e_priv *priv = netdev_priv(dev);
1049 struct mlx5_core_dev *mdev = priv->mdev;
1053 if (rate == sq->rate_limit)
1058 /* remove current rl index to free space to next ones */
1059 mlx5_rl_remove_rate(mdev, sq->rate_limit);
1064 err = mlx5_rl_add_rate(mdev, rate, &rl_index);
1066 netdev_err(dev, "Failed configuring rate %u: %d\n",
1072 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY,
1073 MLX5_SQC_STATE_RDY, true, rl_index);
1075 netdev_err(dev, "Failed configuring rate %u: %d\n",
1077 /* remove the rate from the table */
1079 mlx5_rl_remove_rate(mdev, rate);
1083 sq->rate_limit = rate;
1087 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1089 struct mlx5e_priv *priv = netdev_priv(dev);
1090 struct mlx5_core_dev *mdev = priv->mdev;
1091 struct mlx5e_sq *sq = priv->txq_to_sq_map[index];
1094 if (!mlx5_rl_is_supported(mdev)) {
1095 netdev_err(dev, "Rate limiting is not supported on this device\n");
1099 /* rate is given in Mb/sec, HW config is in Kb/sec */
1102 /* Check whether rate in valid range, 0 is always valid */
1103 if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1104 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1108 mutex_lock(&priv->state_lock);
1109 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1110 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1112 priv->tx_rates[index] = rate;
1113 mutex_unlock(&priv->state_lock);
1118 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1119 struct mlx5e_channel_param *cparam,
1120 struct mlx5e_channel **cp)
1122 struct mlx5e_cq_moder icosq_cq_moder = {0, 0};
1123 struct net_device *netdev = priv->netdev;
1124 struct mlx5e_cq_moder rx_cq_profile;
1125 int cpu = mlx5e_get_cpu(priv, ix);
1126 struct mlx5e_channel *c;
1127 struct mlx5e_sq *sq;
1131 c = kzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1138 c->pdev = &priv->mdev->pdev->dev;
1139 c->netdev = priv->netdev;
1140 c->mkey_be = cpu_to_be32(priv->mkey.key);
1141 c->num_tc = priv->params.num_tc;
1143 if (priv->params.rx_am_enabled)
1144 rx_cq_profile = mlx5e_am_get_def_profile(priv->params.rx_cq_period_mode);
1146 rx_cq_profile = priv->params.rx_cq_moderation;
1148 mlx5e_build_channeltc_to_txq_map(priv, ix);
1150 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1152 err = mlx5e_open_cq(c, &cparam->icosq_cq, &c->icosq.cq, icosq_cq_moder);
1156 err = mlx5e_open_tx_cqs(c, cparam);
1158 goto err_close_icosq_cq;
1160 err = mlx5e_open_cq(c, &cparam->rx_cq, &c->rq.cq,
1163 goto err_close_tx_cqs;
1165 napi_enable(&c->napi);
1167 err = mlx5e_open_sq(c, 0, &cparam->icosq, &c->icosq);
1169 goto err_disable_napi;
1171 err = mlx5e_open_sqs(c, cparam);
1173 goto err_close_icosq;
1175 for (i = 0; i < priv->params.num_tc; i++) {
1176 u32 txq_ix = priv->channeltc_to_txq_map[ix][i];
1178 if (priv->tx_rates[txq_ix]) {
1179 sq = priv->txq_to_sq_map[txq_ix];
1180 mlx5e_set_sq_maxrate(priv->netdev, sq,
1181 priv->tx_rates[txq_ix]);
1185 err = mlx5e_open_rq(c, &cparam->rq, &c->rq);
1189 netif_set_xps_queue(netdev, get_cpu_mask(c->cpu), ix);
1198 mlx5e_close_sq(&c->icosq);
1201 napi_disable(&c->napi);
1202 mlx5e_close_cq(&c->rq.cq);
1205 mlx5e_close_tx_cqs(c);
1208 mlx5e_close_cq(&c->icosq.cq);
1211 netif_napi_del(&c->napi);
1212 napi_hash_del(&c->napi);
1218 static void mlx5e_close_channel(struct mlx5e_channel *c)
1220 mlx5e_close_rq(&c->rq);
1222 mlx5e_close_sq(&c->icosq);
1223 napi_disable(&c->napi);
1224 mlx5e_close_cq(&c->rq.cq);
1225 mlx5e_close_tx_cqs(c);
1226 mlx5e_close_cq(&c->icosq.cq);
1227 netif_napi_del(&c->napi);
1229 napi_hash_del(&c->napi);
1235 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
1236 struct mlx5e_rq_param *param)
1238 void *rqc = param->rqc;
1239 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1241 switch (priv->params.rq_wq_type) {
1242 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1243 MLX5_SET(wq, wq, log_wqe_num_of_strides,
1244 priv->params.mpwqe_log_num_strides - 9);
1245 MLX5_SET(wq, wq, log_wqe_stride_size,
1246 priv->params.mpwqe_log_stride_sz - 6);
1247 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ);
1249 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1250 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1253 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1254 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1255 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_rq_size);
1256 MLX5_SET(wq, wq, pd, priv->pdn);
1257 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
1259 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1260 param->wq.linear = 1;
1262 param->am_enabled = priv->params.rx_am_enabled;
1265 static void mlx5e_build_drop_rq_param(struct mlx5e_rq_param *param)
1267 void *rqc = param->rqc;
1268 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1270 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1271 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1274 static void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
1275 struct mlx5e_sq_param *param)
1277 void *sqc = param->sqc;
1278 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1280 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1281 MLX5_SET(wq, wq, pd, priv->pdn);
1283 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1286 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
1287 struct mlx5e_sq_param *param)
1289 void *sqc = param->sqc;
1290 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1292 mlx5e_build_sq_param_common(priv, param);
1293 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_sq_size);
1295 param->max_inline = priv->params.tx_max_inline;
1298 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1299 struct mlx5e_cq_param *param)
1301 void *cqc = param->cqc;
1303 MLX5_SET(cqc, cqc, uar_page, priv->cq_uar.index);
1306 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
1307 struct mlx5e_cq_param *param)
1309 void *cqc = param->cqc;
1312 switch (priv->params.rq_wq_type) {
1313 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1314 log_cq_size = priv->params.log_rq_size +
1315 priv->params.mpwqe_log_num_strides;
1317 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1318 log_cq_size = priv->params.log_rq_size;
1321 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
1322 if (priv->params.rx_cqe_compress) {
1323 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
1324 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
1327 mlx5e_build_common_cq_param(priv, param);
1329 param->cq_period_mode = priv->params.rx_cq_period_mode;
1332 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
1333 struct mlx5e_cq_param *param)
1335 void *cqc = param->cqc;
1337 MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_sq_size);
1339 mlx5e_build_common_cq_param(priv, param);
1341 param->cq_period_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1344 static void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
1345 struct mlx5e_cq_param *param,
1348 void *cqc = param->cqc;
1350 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
1352 mlx5e_build_common_cq_param(priv, param);
1354 param->cq_period_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1357 static void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
1358 struct mlx5e_sq_param *param,
1361 void *sqc = param->sqc;
1362 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1364 mlx5e_build_sq_param_common(priv, param);
1366 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
1367 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
1369 param->icosq = true;
1372 static void mlx5e_build_channel_param(struct mlx5e_priv *priv, struct mlx5e_channel_param *cparam)
1374 u8 icosq_log_wq_sz = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
1376 mlx5e_build_rq_param(priv, &cparam->rq);
1377 mlx5e_build_sq_param(priv, &cparam->sq);
1378 mlx5e_build_icosq_param(priv, &cparam->icosq, icosq_log_wq_sz);
1379 mlx5e_build_rx_cq_param(priv, &cparam->rx_cq);
1380 mlx5e_build_tx_cq_param(priv, &cparam->tx_cq);
1381 mlx5e_build_ico_cq_param(priv, &cparam->icosq_cq, icosq_log_wq_sz);
1384 static int mlx5e_open_channels(struct mlx5e_priv *priv)
1386 struct mlx5e_channel_param *cparam;
1387 int nch = priv->params.num_channels;
1392 priv->channel = kcalloc(nch, sizeof(struct mlx5e_channel *),
1395 priv->txq_to_sq_map = kcalloc(nch * priv->params.num_tc,
1396 sizeof(struct mlx5e_sq *), GFP_KERNEL);
1398 cparam = kzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
1400 if (!priv->channel || !priv->txq_to_sq_map || !cparam)
1401 goto err_free_txq_to_sq_map;
1403 mlx5e_build_channel_param(priv, cparam);
1405 for (i = 0; i < nch; i++) {
1406 err = mlx5e_open_channel(priv, i, cparam, &priv->channel[i]);
1408 goto err_close_channels;
1411 for (j = 0; j < nch; j++) {
1412 err = mlx5e_wait_for_min_rx_wqes(&priv->channel[j]->rq);
1414 goto err_close_channels;
1421 for (i--; i >= 0; i--)
1422 mlx5e_close_channel(priv->channel[i]);
1424 err_free_txq_to_sq_map:
1425 kfree(priv->txq_to_sq_map);
1426 kfree(priv->channel);
1432 static void mlx5e_close_channels(struct mlx5e_priv *priv)
1436 for (i = 0; i < priv->params.num_channels; i++)
1437 mlx5e_close_channel(priv->channel[i]);
1439 kfree(priv->txq_to_sq_map);
1440 kfree(priv->channel);
1443 static int mlx5e_rx_hash_fn(int hfunc)
1445 return (hfunc == ETH_RSS_HASH_TOP) ?
1446 MLX5_RX_HASH_FN_TOEPLITZ :
1447 MLX5_RX_HASH_FN_INVERTED_XOR8;
1450 static int mlx5e_bits_invert(unsigned long a, int size)
1455 for (i = 0; i < size; i++)
1456 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
1461 static void mlx5e_fill_indir_rqt_rqns(struct mlx5e_priv *priv, void *rqtc)
1465 for (i = 0; i < MLX5E_INDIR_RQT_SIZE; i++) {
1469 if (priv->params.rss_hfunc == ETH_RSS_HASH_XOR)
1470 ix = mlx5e_bits_invert(i, MLX5E_LOG_INDIR_RQT_SIZE);
1472 ix = priv->params.indirection_rqt[ix];
1473 rqn = test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1474 priv->channel[ix]->rq.rqn :
1476 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
1480 static void mlx5e_fill_direct_rqt_rqn(struct mlx5e_priv *priv, void *rqtc,
1483 u32 rqn = test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1484 priv->channel[ix]->rq.rqn :
1487 MLX5_SET(rqtc, rqtc, rq_num[0], rqn);
1490 static int mlx5e_create_rqt(struct mlx5e_priv *priv, int sz, int ix, u32 *rqtn)
1492 struct mlx5_core_dev *mdev = priv->mdev;
1498 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
1499 in = mlx5_vzalloc(inlen);
1503 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
1505 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1506 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
1508 if (sz > 1) /* RSS */
1509 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1511 mlx5e_fill_direct_rqt_rqn(priv, rqtc, ix);
1513 err = mlx5_core_create_rqt(mdev, in, inlen, rqtn);
1519 static void mlx5e_destroy_rqt(struct mlx5e_priv *priv, u32 rqtn)
1521 mlx5_core_destroy_rqt(priv->mdev, rqtn);
1524 static int mlx5e_create_rqts(struct mlx5e_priv *priv)
1526 int nch = mlx5e_get_max_num_channels(priv->mdev);
1532 rqtn = &priv->indir_rqtn;
1533 err = mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, 0, rqtn);
1538 for (ix = 0; ix < nch; ix++) {
1539 rqtn = &priv->direct_tir[ix].rqtn;
1540 err = mlx5e_create_rqt(priv, 1 /*size */, ix, rqtn);
1542 goto err_destroy_rqts;
1548 for (ix--; ix >= 0; ix--)
1549 mlx5e_destroy_rqt(priv, priv->direct_tir[ix].rqtn);
1551 mlx5e_destroy_rqt(priv, priv->indir_rqtn);
1556 static void mlx5e_destroy_rqts(struct mlx5e_priv *priv)
1558 int nch = mlx5e_get_max_num_channels(priv->mdev);
1561 for (i = 0; i < nch; i++)
1562 mlx5e_destroy_rqt(priv, priv->direct_tir[i].rqtn);
1564 mlx5e_destroy_rqt(priv, priv->indir_rqtn);
1567 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz, int ix)
1569 struct mlx5_core_dev *mdev = priv->mdev;
1575 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
1576 in = mlx5_vzalloc(inlen);
1580 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
1582 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1583 if (sz > 1) /* RSS */
1584 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1586 mlx5e_fill_direct_rqt_rqn(priv, rqtc, ix);
1588 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
1590 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
1597 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv)
1602 rqtn = priv->indir_rqtn;
1603 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, 0);
1604 for (ix = 0; ix < priv->params.num_channels; ix++) {
1605 rqtn = priv->direct_tir[ix].rqtn;
1606 mlx5e_redirect_rqt(priv, rqtn, 1, ix);
1610 static void mlx5e_build_tir_ctx_lro(void *tirc, struct mlx5e_priv *priv)
1612 if (!priv->params.lro_en)
1615 #define ROUGH_MAX_L2_L3_HDR_SZ 256
1617 MLX5_SET(tirc, tirc, lro_enable_mask,
1618 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
1619 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
1620 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
1621 (priv->params.lro_wqe_sz -
1622 ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
1623 MLX5_SET(tirc, tirc, lro_timeout_period_usecs,
1624 MLX5_CAP_ETH(priv->mdev,
1625 lro_timer_supported_periods[2]));
1628 void mlx5e_build_tir_ctx_hash(void *tirc, struct mlx5e_priv *priv)
1630 MLX5_SET(tirc, tirc, rx_hash_fn,
1631 mlx5e_rx_hash_fn(priv->params.rss_hfunc));
1632 if (priv->params.rss_hfunc == ETH_RSS_HASH_TOP) {
1633 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
1634 rx_hash_toeplitz_key);
1635 size_t len = MLX5_FLD_SZ_BYTES(tirc,
1636 rx_hash_toeplitz_key);
1638 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1639 memcpy(rss_key, priv->params.toeplitz_hash_key, len);
1643 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
1645 struct mlx5_core_dev *mdev = priv->mdev;
1654 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
1655 in = mlx5_vzalloc(inlen);
1659 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
1660 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
1662 mlx5e_build_tir_ctx_lro(tirc, priv);
1664 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
1665 err = mlx5_core_modify_tir(mdev, priv->indir_tirn[tt], in,
1671 for (ix = 0; ix < mlx5e_get_max_num_channels(mdev); ix++) {
1672 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
1684 static int mlx5e_refresh_tirs_self_loopback_enable(struct mlx5e_priv *priv)
1691 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
1692 in = mlx5_vzalloc(inlen);
1696 MLX5_SET(modify_tir_in, in, bitmask.self_lb_en, 1);
1698 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++) {
1699 err = mlx5_core_modify_tir(priv->mdev, priv->indir_tirn[i], in,
1705 for (i = 0; i < priv->params.num_channels; i++) {
1706 err = mlx5_core_modify_tir(priv->mdev,
1707 priv->direct_tir[i].tirn, in,
1718 static int mlx5e_set_mtu(struct mlx5e_priv *priv, u16 mtu)
1720 struct mlx5_core_dev *mdev = priv->mdev;
1721 u16 hw_mtu = MLX5E_SW2HW_MTU(mtu);
1724 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
1728 /* Update vport context MTU */
1729 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
1733 static void mlx5e_query_mtu(struct mlx5e_priv *priv, u16 *mtu)
1735 struct mlx5_core_dev *mdev = priv->mdev;
1739 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
1740 if (err || !hw_mtu) /* fallback to port oper mtu */
1741 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
1743 *mtu = MLX5E_HW2SW_MTU(hw_mtu);
1746 static int mlx5e_set_dev_port_mtu(struct net_device *netdev)
1748 struct mlx5e_priv *priv = netdev_priv(netdev);
1752 err = mlx5e_set_mtu(priv, netdev->mtu);
1756 mlx5e_query_mtu(priv, &mtu);
1757 if (mtu != netdev->mtu)
1758 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
1759 __func__, mtu, netdev->mtu);
1765 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
1767 struct mlx5e_priv *priv = netdev_priv(netdev);
1768 int nch = priv->params.num_channels;
1769 int ntc = priv->params.num_tc;
1772 netdev_reset_tc(netdev);
1777 netdev_set_num_tc(netdev, ntc);
1779 for (tc = 0; tc < ntc; tc++)
1780 netdev_set_tc_queue(netdev, tc, nch, tc * nch);
1783 int mlx5e_open_locked(struct net_device *netdev)
1785 struct mlx5e_priv *priv = netdev_priv(netdev);
1789 set_bit(MLX5E_STATE_OPENED, &priv->state);
1791 mlx5e_netdev_set_tcs(netdev);
1793 num_txqs = priv->params.num_channels * priv->params.num_tc;
1794 netif_set_real_num_tx_queues(netdev, num_txqs);
1795 netif_set_real_num_rx_queues(netdev, priv->params.num_channels);
1797 err = mlx5e_set_dev_port_mtu(netdev);
1799 goto err_clear_state_opened_flag;
1801 err = mlx5e_open_channels(priv);
1803 netdev_err(netdev, "%s: mlx5e_open_channels failed, %d\n",
1805 goto err_clear_state_opened_flag;
1808 err = mlx5e_refresh_tirs_self_loopback_enable(priv);
1810 netdev_err(netdev, "%s: mlx5e_refresh_tirs_self_loopback_enable failed, %d\n",
1812 goto err_close_channels;
1815 mlx5e_redirect_rqts(priv);
1816 mlx5e_update_carrier(priv);
1817 mlx5e_timestamp_init(priv);
1818 #ifdef CONFIG_RFS_ACCEL
1819 priv->netdev->rx_cpu_rmap = priv->mdev->rmap;
1822 queue_delayed_work(priv->wq, &priv->update_stats_work, 0);
1827 mlx5e_close_channels(priv);
1828 err_clear_state_opened_flag:
1829 clear_bit(MLX5E_STATE_OPENED, &priv->state);
1833 static int mlx5e_open(struct net_device *netdev)
1835 struct mlx5e_priv *priv = netdev_priv(netdev);
1838 mutex_lock(&priv->state_lock);
1839 err = mlx5e_open_locked(netdev);
1840 mutex_unlock(&priv->state_lock);
1845 int mlx5e_close_locked(struct net_device *netdev)
1847 struct mlx5e_priv *priv = netdev_priv(netdev);
1849 /* May already be CLOSED in case a previous configuration operation
1850 * (e.g RX/TX queue size change) that involves close&open failed.
1852 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
1855 clear_bit(MLX5E_STATE_OPENED, &priv->state);
1857 mlx5e_timestamp_cleanup(priv);
1858 netif_carrier_off(priv->netdev);
1859 mlx5e_redirect_rqts(priv);
1860 mlx5e_close_channels(priv);
1865 static int mlx5e_close(struct net_device *netdev)
1867 struct mlx5e_priv *priv = netdev_priv(netdev);
1870 mutex_lock(&priv->state_lock);
1871 err = mlx5e_close_locked(netdev);
1872 mutex_unlock(&priv->state_lock);
1877 static int mlx5e_create_drop_rq(struct mlx5e_priv *priv,
1878 struct mlx5e_rq *rq,
1879 struct mlx5e_rq_param *param)
1881 struct mlx5_core_dev *mdev = priv->mdev;
1882 void *rqc = param->rqc;
1883 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
1886 param->wq.db_numa_node = param->wq.buf_numa_node;
1888 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
1898 static int mlx5e_create_drop_cq(struct mlx5e_priv *priv,
1899 struct mlx5e_cq *cq,
1900 struct mlx5e_cq_param *param)
1902 struct mlx5_core_dev *mdev = priv->mdev;
1903 struct mlx5_core_cq *mcq = &cq->mcq;
1908 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1913 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1916 mcq->set_ci_db = cq->wq_ctrl.db.db;
1917 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1918 *mcq->set_ci_db = 0;
1920 mcq->vector = param->eq_ix;
1921 mcq->comp = mlx5e_completion_event;
1922 mcq->event = mlx5e_cq_error_event;
1924 mcq->uar = &priv->cq_uar;
1931 static int mlx5e_open_drop_rq(struct mlx5e_priv *priv)
1933 struct mlx5e_cq_param cq_param;
1934 struct mlx5e_rq_param rq_param;
1935 struct mlx5e_rq *rq = &priv->drop_rq;
1936 struct mlx5e_cq *cq = &priv->drop_rq.cq;
1939 memset(&cq_param, 0, sizeof(cq_param));
1940 memset(&rq_param, 0, sizeof(rq_param));
1941 mlx5e_build_drop_rq_param(&rq_param);
1943 err = mlx5e_create_drop_cq(priv, cq, &cq_param);
1947 err = mlx5e_enable_cq(cq, &cq_param);
1949 goto err_destroy_cq;
1951 err = mlx5e_create_drop_rq(priv, rq, &rq_param);
1953 goto err_disable_cq;
1955 err = mlx5e_enable_rq(rq, &rq_param);
1957 goto err_destroy_rq;
1962 mlx5e_destroy_rq(&priv->drop_rq);
1965 mlx5e_disable_cq(&priv->drop_rq.cq);
1968 mlx5e_destroy_cq(&priv->drop_rq.cq);
1973 static void mlx5e_close_drop_rq(struct mlx5e_priv *priv)
1975 mlx5e_disable_rq(&priv->drop_rq);
1976 mlx5e_destroy_rq(&priv->drop_rq);
1977 mlx5e_disable_cq(&priv->drop_rq.cq);
1978 mlx5e_destroy_cq(&priv->drop_rq.cq);
1981 static int mlx5e_create_tis(struct mlx5e_priv *priv, int tc)
1983 struct mlx5_core_dev *mdev = priv->mdev;
1984 u32 in[MLX5_ST_SZ_DW(create_tis_in)];
1985 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1987 memset(in, 0, sizeof(in));
1989 MLX5_SET(tisc, tisc, prio, tc << 1);
1990 MLX5_SET(tisc, tisc, transport_domain, priv->tdn);
1992 return mlx5_core_create_tis(mdev, in, sizeof(in), &priv->tisn[tc]);
1995 static void mlx5e_destroy_tis(struct mlx5e_priv *priv, int tc)
1997 mlx5_core_destroy_tis(priv->mdev, priv->tisn[tc]);
2000 static int mlx5e_create_tises(struct mlx5e_priv *priv)
2005 for (tc = 0; tc < MLX5E_MAX_NUM_TC; tc++) {
2006 err = mlx5e_create_tis(priv, tc);
2008 goto err_close_tises;
2014 for (tc--; tc >= 0; tc--)
2015 mlx5e_destroy_tis(priv, tc);
2020 static void mlx5e_destroy_tises(struct mlx5e_priv *priv)
2024 for (tc = 0; tc < MLX5E_MAX_NUM_TC; tc++)
2025 mlx5e_destroy_tis(priv, tc);
2028 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv, u32 *tirc,
2029 enum mlx5e_traffic_types tt)
2031 void *hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2033 MLX5_SET(tirc, tirc, transport_domain, priv->tdn);
2035 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
2036 MLX5_HASH_FIELD_SEL_DST_IP)
2038 #define MLX5_HASH_IP_L4PORTS (MLX5_HASH_FIELD_SEL_SRC_IP |\
2039 MLX5_HASH_FIELD_SEL_DST_IP |\
2040 MLX5_HASH_FIELD_SEL_L4_SPORT |\
2041 MLX5_HASH_FIELD_SEL_L4_DPORT)
2043 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
2044 MLX5_HASH_FIELD_SEL_DST_IP |\
2045 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
2047 mlx5e_build_tir_ctx_lro(tirc, priv);
2049 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2050 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqtn);
2051 mlx5e_build_tir_ctx_hash(tirc, priv);
2054 case MLX5E_TT_IPV4_TCP:
2055 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2056 MLX5_L3_PROT_TYPE_IPV4);
2057 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2058 MLX5_L4_PROT_TYPE_TCP);
2059 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2060 MLX5_HASH_IP_L4PORTS);
2063 case MLX5E_TT_IPV6_TCP:
2064 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2065 MLX5_L3_PROT_TYPE_IPV6);
2066 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2067 MLX5_L4_PROT_TYPE_TCP);
2068 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2069 MLX5_HASH_IP_L4PORTS);
2072 case MLX5E_TT_IPV4_UDP:
2073 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2074 MLX5_L3_PROT_TYPE_IPV4);
2075 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2076 MLX5_L4_PROT_TYPE_UDP);
2077 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2078 MLX5_HASH_IP_L4PORTS);
2081 case MLX5E_TT_IPV6_UDP:
2082 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2083 MLX5_L3_PROT_TYPE_IPV6);
2084 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2085 MLX5_L4_PROT_TYPE_UDP);
2086 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2087 MLX5_HASH_IP_L4PORTS);
2090 case MLX5E_TT_IPV4_IPSEC_AH:
2091 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2092 MLX5_L3_PROT_TYPE_IPV4);
2093 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2094 MLX5_HASH_IP_IPSEC_SPI);
2097 case MLX5E_TT_IPV6_IPSEC_AH:
2098 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2099 MLX5_L3_PROT_TYPE_IPV6);
2100 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2101 MLX5_HASH_IP_IPSEC_SPI);
2104 case MLX5E_TT_IPV4_IPSEC_ESP:
2105 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2106 MLX5_L3_PROT_TYPE_IPV4);
2107 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2108 MLX5_HASH_IP_IPSEC_SPI);
2111 case MLX5E_TT_IPV6_IPSEC_ESP:
2112 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2113 MLX5_L3_PROT_TYPE_IPV6);
2114 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2115 MLX5_HASH_IP_IPSEC_SPI);
2119 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2120 MLX5_L3_PROT_TYPE_IPV4);
2121 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2126 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2127 MLX5_L3_PROT_TYPE_IPV6);
2128 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2133 "mlx5e_build_indir_tir_ctx: bad traffic type!\n");
2137 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 *tirc,
2140 MLX5_SET(tirc, tirc, transport_domain, priv->tdn);
2142 mlx5e_build_tir_ctx_lro(tirc, priv);
2144 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2145 MLX5_SET(tirc, tirc, indirect_table, rqtn);
2146 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
2149 static int mlx5e_create_tirs(struct mlx5e_priv *priv)
2151 int nch = mlx5e_get_max_num_channels(priv->mdev);
2160 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2161 in = mlx5_vzalloc(inlen);
2166 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2167 memset(in, 0, inlen);
2168 tirn = &priv->indir_tirn[tt];
2169 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2170 mlx5e_build_indir_tir_ctx(priv, tirc, tt);
2171 err = mlx5_core_create_tir(priv->mdev, in, inlen, tirn);
2173 goto err_destroy_tirs;
2177 for (ix = 0; ix < nch; ix++) {
2178 memset(in, 0, inlen);
2179 tirn = &priv->direct_tir[ix].tirn;
2180 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2181 mlx5e_build_direct_tir_ctx(priv, tirc,
2182 priv->direct_tir[ix].rqtn);
2183 err = mlx5_core_create_tir(priv->mdev, in, inlen, tirn);
2185 goto err_destroy_ch_tirs;
2192 err_destroy_ch_tirs:
2193 for (ix--; ix >= 0; ix--)
2194 mlx5_core_destroy_tir(priv->mdev, priv->direct_tir[ix].tirn);
2197 for (tt--; tt >= 0; tt--)
2198 mlx5_core_destroy_tir(priv->mdev, priv->indir_tirn[tt]);
2205 static void mlx5e_destroy_tirs(struct mlx5e_priv *priv)
2207 int nch = mlx5e_get_max_num_channels(priv->mdev);
2210 for (i = 0; i < nch; i++)
2211 mlx5_core_destroy_tir(priv->mdev, priv->direct_tir[i].tirn);
2213 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
2214 mlx5_core_destroy_tir(priv->mdev, priv->indir_tirn[i]);
2217 int mlx5e_modify_rqs_vsd(struct mlx5e_priv *priv, bool vsd)
2222 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
2225 for (i = 0; i < priv->params.num_channels; i++) {
2226 err = mlx5e_modify_rq_vsd(&priv->channel[i]->rq, vsd);
2234 static int mlx5e_setup_tc(struct net_device *netdev, u8 tc)
2236 struct mlx5e_priv *priv = netdev_priv(netdev);
2240 if (tc && tc != MLX5E_MAX_NUM_TC)
2243 mutex_lock(&priv->state_lock);
2245 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2247 mlx5e_close_locked(priv->netdev);
2249 priv->params.num_tc = tc ? tc : 1;
2252 err = mlx5e_open_locked(priv->netdev);
2254 mutex_unlock(&priv->state_lock);
2259 static int mlx5e_ndo_setup_tc(struct net_device *dev, u32 handle,
2260 __be16 proto, struct tc_to_netdev *tc)
2262 struct mlx5e_priv *priv = netdev_priv(dev);
2264 if (TC_H_MAJ(handle) != TC_H_MAJ(TC_H_INGRESS))
2268 case TC_SETUP_CLSFLOWER:
2269 switch (tc->cls_flower->command) {
2270 case TC_CLSFLOWER_REPLACE:
2271 return mlx5e_configure_flower(priv, proto, tc->cls_flower);
2272 case TC_CLSFLOWER_DESTROY:
2273 return mlx5e_delete_flower(priv, tc->cls_flower);
2274 case TC_CLSFLOWER_STATS:
2275 return mlx5e_stats_flower(priv, tc->cls_flower);
2282 if (tc->type != TC_SETUP_MQPRIO)
2285 return mlx5e_setup_tc(dev, tc->tc);
2288 static struct rtnl_link_stats64 *
2289 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
2291 struct mlx5e_priv *priv = netdev_priv(dev);
2292 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
2293 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
2294 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
2296 stats->rx_packets = sstats->rx_packets;
2297 stats->rx_bytes = sstats->rx_bytes;
2298 stats->tx_packets = sstats->tx_packets;
2299 stats->tx_bytes = sstats->tx_bytes;
2301 stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
2302 stats->tx_dropped = sstats->tx_queue_dropped;
2304 stats->rx_length_errors =
2305 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
2306 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
2307 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
2308 stats->rx_crc_errors =
2309 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
2310 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
2311 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
2312 stats->tx_carrier_errors =
2313 PPORT_802_3_GET(pstats, a_symbol_error_during_carrier);
2314 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
2315 stats->rx_frame_errors;
2316 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
2318 /* vport multicast also counts packets that are dropped due to steering
2319 * or rx out of buffer
2322 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
2327 static void mlx5e_set_rx_mode(struct net_device *dev)
2329 struct mlx5e_priv *priv = netdev_priv(dev);
2331 queue_work(priv->wq, &priv->set_rx_mode_work);
2334 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
2336 struct mlx5e_priv *priv = netdev_priv(netdev);
2337 struct sockaddr *saddr = addr;
2339 if (!is_valid_ether_addr(saddr->sa_data))
2340 return -EADDRNOTAVAIL;
2342 netif_addr_lock_bh(netdev);
2343 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
2344 netif_addr_unlock_bh(netdev);
2346 queue_work(priv->wq, &priv->set_rx_mode_work);
2351 #define MLX5E_SET_FEATURE(netdev, feature, enable) \
2354 netdev->features |= feature; \
2356 netdev->features &= ~feature; \
2359 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
2361 static int set_feature_lro(struct net_device *netdev, bool enable)
2363 struct mlx5e_priv *priv = netdev_priv(netdev);
2364 bool was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2367 mutex_lock(&priv->state_lock);
2369 if (was_opened && (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST))
2370 mlx5e_close_locked(priv->netdev);
2372 priv->params.lro_en = enable;
2373 err = mlx5e_modify_tirs_lro(priv);
2375 netdev_err(netdev, "lro modify failed, %d\n", err);
2376 priv->params.lro_en = !enable;
2379 if (was_opened && (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST))
2380 mlx5e_open_locked(priv->netdev);
2382 mutex_unlock(&priv->state_lock);
2387 static int set_feature_vlan_filter(struct net_device *netdev, bool enable)
2389 struct mlx5e_priv *priv = netdev_priv(netdev);
2392 mlx5e_enable_vlan_filter(priv);
2394 mlx5e_disable_vlan_filter(priv);
2399 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
2401 struct mlx5e_priv *priv = netdev_priv(netdev);
2403 if (!enable && mlx5e_tc_num_filters(priv)) {
2405 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
2412 static int set_feature_rx_all(struct net_device *netdev, bool enable)
2414 struct mlx5e_priv *priv = netdev_priv(netdev);
2415 struct mlx5_core_dev *mdev = priv->mdev;
2417 return mlx5_set_port_fcs(mdev, !enable);
2420 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
2422 struct mlx5e_priv *priv = netdev_priv(netdev);
2425 mutex_lock(&priv->state_lock);
2427 priv->params.vlan_strip_disable = !enable;
2428 err = mlx5e_modify_rqs_vsd(priv, !enable);
2430 priv->params.vlan_strip_disable = enable;
2432 mutex_unlock(&priv->state_lock);
2437 #ifdef CONFIG_RFS_ACCEL
2438 static int set_feature_arfs(struct net_device *netdev, bool enable)
2440 struct mlx5e_priv *priv = netdev_priv(netdev);
2444 err = mlx5e_arfs_enable(priv);
2446 err = mlx5e_arfs_disable(priv);
2452 static int mlx5e_handle_feature(struct net_device *netdev,
2453 netdev_features_t wanted_features,
2454 netdev_features_t feature,
2455 mlx5e_feature_handler feature_handler)
2457 netdev_features_t changes = wanted_features ^ netdev->features;
2458 bool enable = !!(wanted_features & feature);
2461 if (!(changes & feature))
2464 err = feature_handler(netdev, enable);
2466 netdev_err(netdev, "%s feature 0x%llx failed err %d\n",
2467 enable ? "Enable" : "Disable", feature, err);
2471 MLX5E_SET_FEATURE(netdev, feature, enable);
2475 static int mlx5e_set_features(struct net_device *netdev,
2476 netdev_features_t features)
2480 err = mlx5e_handle_feature(netdev, features, NETIF_F_LRO,
2482 err |= mlx5e_handle_feature(netdev, features,
2483 NETIF_F_HW_VLAN_CTAG_FILTER,
2484 set_feature_vlan_filter);
2485 err |= mlx5e_handle_feature(netdev, features, NETIF_F_HW_TC,
2486 set_feature_tc_num_filters);
2487 err |= mlx5e_handle_feature(netdev, features, NETIF_F_RXALL,
2488 set_feature_rx_all);
2489 err |= mlx5e_handle_feature(netdev, features, NETIF_F_HW_VLAN_CTAG_RX,
2490 set_feature_rx_vlan);
2491 #ifdef CONFIG_RFS_ACCEL
2492 err |= mlx5e_handle_feature(netdev, features, NETIF_F_NTUPLE,
2496 return err ? -EINVAL : 0;
2499 #define MXL5_HW_MIN_MTU 64
2500 #define MXL5E_MIN_MTU (MXL5_HW_MIN_MTU + ETH_FCS_LEN)
2502 static int mlx5e_change_mtu(struct net_device *netdev, int new_mtu)
2504 struct mlx5e_priv *priv = netdev_priv(netdev);
2505 struct mlx5_core_dev *mdev = priv->mdev;
2511 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
2513 max_mtu = MLX5E_HW2SW_MTU(max_mtu);
2514 min_mtu = MLX5E_HW2SW_MTU(MXL5E_MIN_MTU);
2516 if (new_mtu > max_mtu || new_mtu < min_mtu) {
2518 "%s: Bad MTU (%d), valid range is: [%d..%d]\n",
2519 __func__, new_mtu, min_mtu, max_mtu);
2523 mutex_lock(&priv->state_lock);
2525 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2527 mlx5e_close_locked(netdev);
2529 netdev->mtu = new_mtu;
2532 err = mlx5e_open_locked(netdev);
2534 mutex_unlock(&priv->state_lock);
2539 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2543 return mlx5e_hwstamp_set(dev, ifr);
2545 return mlx5e_hwstamp_get(dev, ifr);
2551 static int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2553 struct mlx5e_priv *priv = netdev_priv(dev);
2554 struct mlx5_core_dev *mdev = priv->mdev;
2556 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
2559 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos)
2561 struct mlx5e_priv *priv = netdev_priv(dev);
2562 struct mlx5_core_dev *mdev = priv->mdev;
2564 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
2568 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2570 struct mlx5e_priv *priv = netdev_priv(dev);
2571 struct mlx5_core_dev *mdev = priv->mdev;
2573 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
2576 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
2578 struct mlx5e_priv *priv = netdev_priv(dev);
2579 struct mlx5_core_dev *mdev = priv->mdev;
2581 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
2583 static int mlx5_vport_link2ifla(u8 esw_link)
2586 case MLX5_ESW_VPORT_ADMIN_STATE_DOWN:
2587 return IFLA_VF_LINK_STATE_DISABLE;
2588 case MLX5_ESW_VPORT_ADMIN_STATE_UP:
2589 return IFLA_VF_LINK_STATE_ENABLE;
2591 return IFLA_VF_LINK_STATE_AUTO;
2594 static int mlx5_ifla_link2vport(u8 ifla_link)
2596 switch (ifla_link) {
2597 case IFLA_VF_LINK_STATE_DISABLE:
2598 return MLX5_ESW_VPORT_ADMIN_STATE_DOWN;
2599 case IFLA_VF_LINK_STATE_ENABLE:
2600 return MLX5_ESW_VPORT_ADMIN_STATE_UP;
2602 return MLX5_ESW_VPORT_ADMIN_STATE_AUTO;
2605 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
2608 struct mlx5e_priv *priv = netdev_priv(dev);
2609 struct mlx5_core_dev *mdev = priv->mdev;
2611 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
2612 mlx5_ifla_link2vport(link_state));
2615 static int mlx5e_get_vf_config(struct net_device *dev,
2616 int vf, struct ifla_vf_info *ivi)
2618 struct mlx5e_priv *priv = netdev_priv(dev);
2619 struct mlx5_core_dev *mdev = priv->mdev;
2622 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
2625 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
2629 static int mlx5e_get_vf_stats(struct net_device *dev,
2630 int vf, struct ifla_vf_stats *vf_stats)
2632 struct mlx5e_priv *priv = netdev_priv(dev);
2633 struct mlx5_core_dev *mdev = priv->mdev;
2635 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
2639 static void mlx5e_add_vxlan_port(struct net_device *netdev,
2640 struct udp_tunnel_info *ti)
2642 struct mlx5e_priv *priv = netdev_priv(netdev);
2644 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2647 if (!mlx5e_vxlan_allowed(priv->mdev))
2650 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 1);
2653 static void mlx5e_del_vxlan_port(struct net_device *netdev,
2654 struct udp_tunnel_info *ti)
2656 struct mlx5e_priv *priv = netdev_priv(netdev);
2658 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2661 if (!mlx5e_vxlan_allowed(priv->mdev))
2664 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 0);
2667 static netdev_features_t mlx5e_vxlan_features_check(struct mlx5e_priv *priv,
2668 struct sk_buff *skb,
2669 netdev_features_t features)
2671 struct udphdr *udph;
2675 switch (vlan_get_protocol(skb)) {
2676 case htons(ETH_P_IP):
2677 proto = ip_hdr(skb)->protocol;
2679 case htons(ETH_P_IPV6):
2680 proto = ipv6_hdr(skb)->nexthdr;
2686 if (proto == IPPROTO_UDP) {
2687 udph = udp_hdr(skb);
2688 port = be16_to_cpu(udph->dest);
2691 /* Verify if UDP port is being offloaded by HW */
2692 if (port && mlx5e_vxlan_lookup_port(priv, port))
2696 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
2697 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2700 static netdev_features_t mlx5e_features_check(struct sk_buff *skb,
2701 struct net_device *netdev,
2702 netdev_features_t features)
2704 struct mlx5e_priv *priv = netdev_priv(netdev);
2706 features = vlan_features_check(skb, features);
2707 features = vxlan_features_check(skb, features);
2709 /* Validate if the tunneled packet is being offloaded by HW */
2710 if (skb->encapsulation &&
2711 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
2712 return mlx5e_vxlan_features_check(priv, skb, features);
2717 static const struct net_device_ops mlx5e_netdev_ops_basic = {
2718 .ndo_open = mlx5e_open,
2719 .ndo_stop = mlx5e_close,
2720 .ndo_start_xmit = mlx5e_xmit,
2721 .ndo_setup_tc = mlx5e_ndo_setup_tc,
2722 .ndo_select_queue = mlx5e_select_queue,
2723 .ndo_get_stats64 = mlx5e_get_stats,
2724 .ndo_set_rx_mode = mlx5e_set_rx_mode,
2725 .ndo_set_mac_address = mlx5e_set_mac,
2726 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
2727 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
2728 .ndo_set_features = mlx5e_set_features,
2729 .ndo_change_mtu = mlx5e_change_mtu,
2730 .ndo_do_ioctl = mlx5e_ioctl,
2731 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
2732 #ifdef CONFIG_RFS_ACCEL
2733 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
2737 static const struct net_device_ops mlx5e_netdev_ops_sriov = {
2738 .ndo_open = mlx5e_open,
2739 .ndo_stop = mlx5e_close,
2740 .ndo_start_xmit = mlx5e_xmit,
2741 .ndo_setup_tc = mlx5e_ndo_setup_tc,
2742 .ndo_select_queue = mlx5e_select_queue,
2743 .ndo_get_stats64 = mlx5e_get_stats,
2744 .ndo_set_rx_mode = mlx5e_set_rx_mode,
2745 .ndo_set_mac_address = mlx5e_set_mac,
2746 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
2747 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
2748 .ndo_set_features = mlx5e_set_features,
2749 .ndo_change_mtu = mlx5e_change_mtu,
2750 .ndo_do_ioctl = mlx5e_ioctl,
2751 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
2752 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
2753 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
2754 .ndo_features_check = mlx5e_features_check,
2755 #ifdef CONFIG_RFS_ACCEL
2756 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
2758 .ndo_set_vf_mac = mlx5e_set_vf_mac,
2759 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
2760 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
2761 .ndo_set_vf_trust = mlx5e_set_vf_trust,
2762 .ndo_get_vf_config = mlx5e_get_vf_config,
2763 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
2764 .ndo_get_vf_stats = mlx5e_get_vf_stats,
2767 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
2769 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
2771 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
2772 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
2773 !MLX5_CAP_ETH(mdev, csum_cap) ||
2774 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
2775 !MLX5_CAP_ETH(mdev, vlan_cap) ||
2776 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
2777 MLX5_CAP_FLOWTABLE(mdev,
2778 flow_table_properties_nic_receive.max_ft_level)
2780 mlx5_core_warn(mdev,
2781 "Not creating net device, some required device capabilities are missing\n");
2784 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
2785 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
2786 if (!MLX5_CAP_GEN(mdev, cq_moderation))
2787 mlx5_core_warn(mdev, "CQ modiration is not supported\n");
2792 u16 mlx5e_get_max_inline_cap(struct mlx5_core_dev *mdev)
2794 int bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
2796 return bf_buf_size -
2797 sizeof(struct mlx5e_tx_wqe) +
2798 2 /*sizeof(mlx5e_tx_wqe.inline_hdr_start)*/;
2801 #ifdef CONFIG_MLX5_CORE_EN_DCB
2802 static void mlx5e_ets_init(struct mlx5e_priv *priv)
2806 priv->params.ets.ets_cap = mlx5_max_tc(priv->mdev) + 1;
2807 for (i = 0; i < priv->params.ets.ets_cap; i++) {
2808 priv->params.ets.tc_tx_bw[i] = MLX5E_MAX_BW_ALLOC;
2809 priv->params.ets.tc_tsa[i] = IEEE_8021QAZ_TSA_VENDOR;
2810 priv->params.ets.prio_tc[i] = i;
2813 /* tclass[prio=0]=1, tclass[prio=1]=0, tclass[prio=i]=i (for i>1) */
2814 priv->params.ets.prio_tc[0] = 1;
2815 priv->params.ets.prio_tc[1] = 0;
2819 void mlx5e_build_default_indir_rqt(struct mlx5_core_dev *mdev,
2820 u32 *indirection_rqt, int len,
2823 int node = mdev->priv.numa_node;
2824 int node_num_of_cores;
2828 node = first_online_node;
2830 node_num_of_cores = cpumask_weight(cpumask_of_node(node));
2832 if (node_num_of_cores)
2833 num_channels = min_t(int, num_channels, node_num_of_cores);
2835 for (i = 0; i < len; i++)
2836 indirection_rqt[i] = i % num_channels;
2839 static bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
2841 return MLX5_CAP_GEN(mdev, striding_rq) &&
2842 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
2843 MLX5_CAP_ETH(mdev, reg_umr_sq);
2846 static int mlx5e_get_pci_bw(struct mlx5_core_dev *mdev, u32 *pci_bw)
2848 enum pcie_link_width width;
2849 enum pci_bus_speed speed;
2852 err = pcie_get_minimum_link(mdev->pdev, &speed, &width);
2856 if (speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
2860 case PCIE_SPEED_2_5GT:
2861 *pci_bw = 2500 * width;
2863 case PCIE_SPEED_5_0GT:
2864 *pci_bw = 5000 * width;
2866 case PCIE_SPEED_8_0GT:
2867 *pci_bw = 8000 * width;
2876 static bool cqe_compress_heuristic(u32 link_speed, u32 pci_bw)
2878 return (link_speed && pci_bw &&
2879 (pci_bw < 40000) && (pci_bw < link_speed));
2882 void mlx5e_set_rx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
2884 params->rx_cq_period_mode = cq_period_mode;
2886 params->rx_cq_moderation.pkts =
2887 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
2888 params->rx_cq_moderation.usec =
2889 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
2891 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
2892 params->rx_cq_moderation.usec =
2893 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE;
2896 static void mlx5e_build_netdev_priv(struct mlx5_core_dev *mdev,
2897 struct net_device *netdev,
2900 struct mlx5e_priv *priv = netdev_priv(netdev);
2903 u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
2904 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
2905 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
2907 priv->params.log_sq_size =
2908 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
2909 priv->params.rq_wq_type = mlx5e_check_fragmented_striding_rq_cap(mdev) ?
2910 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
2911 MLX5_WQ_TYPE_LINKED_LIST;
2913 /* set CQE compression */
2914 priv->params.rx_cqe_compress_admin = false;
2915 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
2916 MLX5_CAP_GEN(mdev, vport_group_manager)) {
2917 mlx5e_get_max_linkspeed(mdev, &link_speed);
2918 mlx5e_get_pci_bw(mdev, &pci_bw);
2919 mlx5_core_dbg(mdev, "Max link speed = %d, PCI BW = %d\n",
2920 link_speed, pci_bw);
2921 priv->params.rx_cqe_compress_admin =
2922 cqe_compress_heuristic(link_speed, pci_bw);
2925 priv->params.rx_cqe_compress = priv->params.rx_cqe_compress_admin;
2927 switch (priv->params.rq_wq_type) {
2928 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2929 priv->params.log_rq_size = MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE_MPW;
2930 priv->params.mpwqe_log_stride_sz =
2931 priv->params.rx_cqe_compress ?
2932 MLX5_MPWRQ_LOG_STRIDE_SIZE_CQE_COMPRESS :
2933 MLX5_MPWRQ_LOG_STRIDE_SIZE;
2934 priv->params.mpwqe_log_num_strides = MLX5_MPWRQ_LOG_WQE_SZ -
2935 priv->params.mpwqe_log_stride_sz;
2936 priv->params.lro_en = true;
2938 default: /* MLX5_WQ_TYPE_LINKED_LIST */
2939 priv->params.log_rq_size = MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
2942 mlx5_core_info(mdev,
2943 "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
2944 priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
2945 BIT(priv->params.log_rq_size),
2946 BIT(priv->params.mpwqe_log_stride_sz),
2947 priv->params.rx_cqe_compress_admin);
2949 priv->params.min_rx_wqes = mlx5_min_rx_wqes(priv->params.rq_wq_type,
2950 BIT(priv->params.log_rq_size));
2952 priv->params.rx_am_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
2953 mlx5e_set_rx_cq_mode_params(&priv->params, cq_period_mode);
2955 priv->params.tx_cq_moderation.usec =
2956 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
2957 priv->params.tx_cq_moderation.pkts =
2958 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
2959 priv->params.tx_max_inline = mlx5e_get_max_inline_cap(mdev);
2960 priv->params.num_tc = 1;
2961 priv->params.rss_hfunc = ETH_RSS_HASH_XOR;
2963 netdev_rss_key_fill(priv->params.toeplitz_hash_key,
2964 sizeof(priv->params.toeplitz_hash_key));
2966 mlx5e_build_default_indir_rqt(mdev, priv->params.indirection_rqt,
2967 MLX5E_INDIR_RQT_SIZE, num_channels);
2969 priv->params.lro_wqe_sz =
2970 MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
2972 /* Initialize pflags */
2973 MLX5E_SET_PRIV_FLAG(priv, MLX5E_PFLAG_RX_CQE_BASED_MODER,
2974 priv->params.rx_cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
2977 priv->netdev = netdev;
2978 priv->params.num_channels = num_channels;
2980 #ifdef CONFIG_MLX5_CORE_EN_DCB
2981 mlx5e_ets_init(priv);
2984 mutex_init(&priv->state_lock);
2986 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
2987 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
2988 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
2991 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
2993 struct mlx5e_priv *priv = netdev_priv(netdev);
2995 mlx5_query_nic_vport_mac_address(priv->mdev, 0, netdev->dev_addr);
2996 if (is_zero_ether_addr(netdev->dev_addr) &&
2997 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
2998 eth_hw_addr_random(netdev);
2999 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
3003 static void mlx5e_build_netdev(struct net_device *netdev)
3005 struct mlx5e_priv *priv = netdev_priv(netdev);
3006 struct mlx5_core_dev *mdev = priv->mdev;
3010 SET_NETDEV_DEV(netdev, &mdev->pdev->dev);
3012 if (MLX5_CAP_GEN(mdev, vport_group_manager)) {
3013 netdev->netdev_ops = &mlx5e_netdev_ops_sriov;
3014 #ifdef CONFIG_MLX5_CORE_EN_DCB
3015 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
3018 netdev->netdev_ops = &mlx5e_netdev_ops_basic;
3021 netdev->watchdog_timeo = 15 * HZ;
3023 netdev->ethtool_ops = &mlx5e_ethtool_ops;
3025 netdev->vlan_features |= NETIF_F_SG;
3026 netdev->vlan_features |= NETIF_F_IP_CSUM;
3027 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
3028 netdev->vlan_features |= NETIF_F_GRO;
3029 netdev->vlan_features |= NETIF_F_TSO;
3030 netdev->vlan_features |= NETIF_F_TSO6;
3031 netdev->vlan_features |= NETIF_F_RXCSUM;
3032 netdev->vlan_features |= NETIF_F_RXHASH;
3034 if (!!MLX5_CAP_ETH(mdev, lro_cap))
3035 netdev->vlan_features |= NETIF_F_LRO;
3037 netdev->hw_features = netdev->vlan_features;
3038 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
3039 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
3040 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3042 if (mlx5e_vxlan_allowed(mdev)) {
3043 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
3044 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3045 NETIF_F_GSO_PARTIAL;
3046 netdev->hw_enc_features |= NETIF_F_IP_CSUM;
3047 netdev->hw_enc_features |= NETIF_F_IPV6_CSUM;
3048 netdev->hw_enc_features |= NETIF_F_TSO;
3049 netdev->hw_enc_features |= NETIF_F_TSO6;
3050 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL;
3051 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
3052 NETIF_F_GSO_PARTIAL;
3053 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
3056 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
3059 netdev->hw_features |= NETIF_F_RXALL;
3061 netdev->features = netdev->hw_features;
3062 if (!priv->params.lro_en)
3063 netdev->features &= ~NETIF_F_LRO;
3066 netdev->features &= ~NETIF_F_RXALL;
3068 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
3069 if (FT_CAP(flow_modify_en) &&
3070 FT_CAP(modify_root) &&
3071 FT_CAP(identified_miss_table_mode) &&
3072 FT_CAP(flow_table_modify)) {
3073 netdev->hw_features |= NETIF_F_HW_TC;
3074 #ifdef CONFIG_RFS_ACCEL
3075 netdev->hw_features |= NETIF_F_NTUPLE;
3079 netdev->features |= NETIF_F_HIGHDMA;
3081 netdev->priv_flags |= IFF_UNICAST_FLT;
3083 mlx5e_set_netdev_dev_addr(netdev);
3086 static int mlx5e_create_mkey(struct mlx5e_priv *priv, u32 pdn,
3087 struct mlx5_core_mkey *mkey)
3089 struct mlx5_core_dev *mdev = priv->mdev;
3090 struct mlx5_create_mkey_mbox_in *in;
3093 in = mlx5_vzalloc(sizeof(*in));
3097 in->seg.flags = MLX5_PERM_LOCAL_WRITE |
3098 MLX5_PERM_LOCAL_READ |
3099 MLX5_ACCESS_MODE_PA;
3100 in->seg.flags_pd = cpu_to_be32(pdn | MLX5_MKEY_LEN64);
3101 in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
3103 err = mlx5_core_create_mkey(mdev, mkey, in, sizeof(*in), NULL, NULL,
3111 static void mlx5e_create_q_counter(struct mlx5e_priv *priv)
3113 struct mlx5_core_dev *mdev = priv->mdev;
3116 err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
3118 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
3119 priv->q_counter = 0;
3123 static void mlx5e_destroy_q_counter(struct mlx5e_priv *priv)
3125 if (!priv->q_counter)
3128 mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
3131 static int mlx5e_create_umr_mkey(struct mlx5e_priv *priv)
3133 struct mlx5_core_dev *mdev = priv->mdev;
3134 struct mlx5_create_mkey_mbox_in *in;
3135 struct mlx5_mkey_seg *mkc;
3136 int inlen = sizeof(*in);
3138 mlx5e_get_max_num_channels(mdev) * MLX5_CHANNEL_MAX_NUM_MTTS;
3141 in = mlx5_vzalloc(inlen);
3146 mkc->status = MLX5_MKEY_STATUS_FREE;
3147 mkc->flags = MLX5_PERM_UMR_EN |
3148 MLX5_PERM_LOCAL_READ |
3149 MLX5_PERM_LOCAL_WRITE |
3150 MLX5_ACCESS_MODE_MTT;
3152 mkc->qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
3153 mkc->flags_pd = cpu_to_be32(priv->pdn);
3154 mkc->len = cpu_to_be64(npages << PAGE_SHIFT);
3155 mkc->xlt_oct_size = cpu_to_be32(mlx5e_get_mtt_octw(npages));
3156 mkc->log2_page_size = PAGE_SHIFT;
3158 err = mlx5_core_create_mkey(mdev, &priv->umr_mkey, in, inlen, NULL,
3166 static void *mlx5e_create_netdev(struct mlx5_core_dev *mdev)
3168 struct net_device *netdev;
3169 struct mlx5e_priv *priv;
3170 int nch = mlx5e_get_max_num_channels(mdev);
3173 if (mlx5e_check_required_hca_cap(mdev))
3176 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
3177 nch * MLX5E_MAX_NUM_TC,
3180 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
3184 mlx5e_build_netdev_priv(mdev, netdev, nch);
3185 mlx5e_build_netdev(netdev);
3187 netif_carrier_off(netdev);
3189 priv = netdev_priv(netdev);
3191 priv->wq = create_singlethread_workqueue("mlx5e");
3193 goto err_free_netdev;
3195 err = mlx5_alloc_map_uar(mdev, &priv->cq_uar, false);
3197 mlx5_core_err(mdev, "alloc_map uar failed, %d\n", err);
3198 goto err_destroy_wq;
3201 err = mlx5_core_alloc_pd(mdev, &priv->pdn);
3203 mlx5_core_err(mdev, "alloc pd failed, %d\n", err);
3204 goto err_unmap_free_uar;
3207 err = mlx5_core_alloc_transport_domain(mdev, &priv->tdn);
3209 mlx5_core_err(mdev, "alloc td failed, %d\n", err);
3210 goto err_dealloc_pd;
3213 err = mlx5e_create_mkey(priv, priv->pdn, &priv->mkey);
3215 mlx5_core_err(mdev, "create mkey failed, %d\n", err);
3216 goto err_dealloc_transport_domain;
3219 err = mlx5e_create_umr_mkey(priv);
3221 mlx5_core_err(mdev, "create umr mkey failed, %d\n", err);
3222 goto err_destroy_mkey;
3225 err = mlx5e_create_tises(priv);
3227 mlx5_core_warn(mdev, "create tises failed, %d\n", err);
3228 goto err_destroy_umr_mkey;
3231 err = mlx5e_open_drop_rq(priv);
3233 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
3234 goto err_destroy_tises;
3237 err = mlx5e_create_rqts(priv);
3239 mlx5_core_warn(mdev, "create rqts failed, %d\n", err);
3240 goto err_close_drop_rq;
3243 err = mlx5e_create_tirs(priv);
3245 mlx5_core_warn(mdev, "create tirs failed, %d\n", err);
3246 goto err_destroy_rqts;
3249 err = mlx5e_create_flow_steering(priv);
3251 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
3252 goto err_destroy_tirs;
3255 mlx5e_create_q_counter(priv);
3257 mlx5e_init_l2_addr(priv);
3259 mlx5e_vxlan_init(priv);
3261 err = mlx5e_tc_init(priv);
3263 goto err_dealloc_q_counters;
3265 #ifdef CONFIG_MLX5_CORE_EN_DCB
3266 mlx5e_dcbnl_ieee_setets_core(priv, &priv->params.ets);
3269 err = register_netdev(netdev);
3271 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
3272 goto err_tc_cleanup;
3275 if (mlx5e_vxlan_allowed(mdev)) {
3277 udp_tunnel_get_rx_info(netdev);
3281 mlx5e_enable_async_events(priv);
3282 queue_work(priv->wq, &priv->set_rx_mode_work);
3287 mlx5e_tc_cleanup(priv);
3289 err_dealloc_q_counters:
3290 mlx5e_destroy_q_counter(priv);
3291 mlx5e_destroy_flow_steering(priv);
3294 mlx5e_destroy_tirs(priv);
3297 mlx5e_destroy_rqts(priv);
3300 mlx5e_close_drop_rq(priv);
3303 mlx5e_destroy_tises(priv);
3305 err_destroy_umr_mkey:
3306 mlx5_core_destroy_mkey(mdev, &priv->umr_mkey);
3309 mlx5_core_destroy_mkey(mdev, &priv->mkey);
3311 err_dealloc_transport_domain:
3312 mlx5_core_dealloc_transport_domain(mdev, priv->tdn);
3315 mlx5_core_dealloc_pd(mdev, priv->pdn);
3318 mlx5_unmap_free_uar(mdev, &priv->cq_uar);
3321 destroy_workqueue(priv->wq);
3324 free_netdev(netdev);
3329 static void mlx5e_destroy_netdev(struct mlx5_core_dev *mdev, void *vpriv)
3331 struct mlx5e_priv *priv = vpriv;
3332 struct net_device *netdev = priv->netdev;
3334 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
3336 queue_work(priv->wq, &priv->set_rx_mode_work);
3337 mlx5e_disable_async_events(priv);
3338 flush_workqueue(priv->wq);
3339 if (test_bit(MLX5_INTERFACE_STATE_SHUTDOWN, &mdev->intf_state)) {
3340 netif_device_detach(netdev);
3341 mlx5e_close(netdev);
3343 unregister_netdev(netdev);
3346 mlx5e_tc_cleanup(priv);
3347 mlx5e_vxlan_cleanup(priv);
3348 mlx5e_destroy_q_counter(priv);
3349 mlx5e_destroy_flow_steering(priv);
3350 mlx5e_destroy_tirs(priv);
3351 mlx5e_destroy_rqts(priv);
3352 mlx5e_close_drop_rq(priv);
3353 mlx5e_destroy_tises(priv);
3354 mlx5_core_destroy_mkey(priv->mdev, &priv->umr_mkey);
3355 mlx5_core_destroy_mkey(priv->mdev, &priv->mkey);
3356 mlx5_core_dealloc_transport_domain(priv->mdev, priv->tdn);
3357 mlx5_core_dealloc_pd(priv->mdev, priv->pdn);
3358 mlx5_unmap_free_uar(priv->mdev, &priv->cq_uar);
3359 cancel_delayed_work_sync(&priv->update_stats_work);
3360 destroy_workqueue(priv->wq);
3362 if (!test_bit(MLX5_INTERFACE_STATE_SHUTDOWN, &mdev->intf_state))
3363 free_netdev(netdev);
3366 static void *mlx5e_get_netdev(void *vpriv)
3368 struct mlx5e_priv *priv = vpriv;
3370 return priv->netdev;
3373 static struct mlx5_interface mlx5e_interface = {
3374 .add = mlx5e_create_netdev,
3375 .remove = mlx5e_destroy_netdev,
3376 .event = mlx5e_async_event,
3377 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
3378 .get_dev = mlx5e_get_netdev,
3381 void mlx5e_init(void)
3383 mlx5e_build_ptys2ethtool_map();
3384 mlx5_register_interface(&mlx5e_interface);
3387 void mlx5e_cleanup(void)
3389 mlx5_unregister_interface(&mlx5e_interface);