2 * Copyright (c) 2015-2016, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <net/tc_act/tc_gact.h>
34 #include <net/pkt_cls.h>
35 #include <linux/mlx5/fs.h>
36 #include <net/vxlan.h>
43 MLX5_EN_QP_FLUSH_TIMEOUT_MS = 5000,
44 MLX5_EN_QP_FLUSH_MSLEEP_QUANT = 20,
45 MLX5_EN_QP_FLUSH_MAX_ITER = MLX5_EN_QP_FLUSH_TIMEOUT_MS /
46 MLX5_EN_QP_FLUSH_MSLEEP_QUANT,
49 struct mlx5e_rq_param {
50 u32 rqc[MLX5_ST_SZ_DW(rqc)];
51 struct mlx5_wq_param wq;
54 struct mlx5e_sq_param {
55 u32 sqc[MLX5_ST_SZ_DW(sqc)];
56 struct mlx5_wq_param wq;
61 struct mlx5e_cq_param {
62 u32 cqc[MLX5_ST_SZ_DW(cqc)];
63 struct mlx5_wq_param wq;
67 struct mlx5e_channel_param {
68 struct mlx5e_rq_param rq;
69 struct mlx5e_sq_param sq;
70 struct mlx5e_sq_param icosq;
71 struct mlx5e_cq_param rx_cq;
72 struct mlx5e_cq_param tx_cq;
73 struct mlx5e_cq_param icosq_cq;
76 static void mlx5e_update_carrier(struct mlx5e_priv *priv)
78 struct mlx5_core_dev *mdev = priv->mdev;
81 port_state = mlx5_query_vport_state(mdev,
82 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT, 0);
84 if (port_state == VPORT_STATE_UP)
85 netif_carrier_on(priv->netdev);
87 netif_carrier_off(priv->netdev);
90 static void mlx5e_update_carrier_work(struct work_struct *work)
92 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
95 mutex_lock(&priv->state_lock);
96 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
97 mlx5e_update_carrier(priv);
98 mutex_unlock(&priv->state_lock);
101 static void mlx5e_tx_timeout_work(struct work_struct *work)
103 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
108 mutex_lock(&priv->state_lock);
109 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
111 mlx5e_close_locked(priv->netdev);
112 err = mlx5e_open_locked(priv->netdev);
114 netdev_err(priv->netdev, "mlx5e_open_locked failed recovering from a tx_timeout, err(%d).\n",
117 mutex_unlock(&priv->state_lock);
121 static void mlx5e_update_sw_counters(struct mlx5e_priv *priv)
123 struct mlx5e_sw_stats *s = &priv->stats.sw;
124 struct mlx5e_rq_stats *rq_stats;
125 struct mlx5e_sq_stats *sq_stats;
126 u64 tx_offload_none = 0;
129 memset(s, 0, sizeof(*s));
130 for (i = 0; i < priv->params.num_channels; i++) {
131 rq_stats = &priv->channel[i]->rq.stats;
133 s->rx_packets += rq_stats->packets;
134 s->rx_bytes += rq_stats->bytes;
135 s->rx_lro_packets += rq_stats->lro_packets;
136 s->rx_lro_bytes += rq_stats->lro_bytes;
137 s->rx_csum_none += rq_stats->csum_none;
138 s->rx_csum_complete += rq_stats->csum_complete;
139 s->rx_csum_unnecessary_inner += rq_stats->csum_unnecessary_inner;
140 s->rx_wqe_err += rq_stats->wqe_err;
141 s->rx_mpwqe_filler += rq_stats->mpwqe_filler;
142 s->rx_mpwqe_frag += rq_stats->mpwqe_frag;
143 s->rx_buff_alloc_err += rq_stats->buff_alloc_err;
144 s->rx_cqe_compress_blks += rq_stats->cqe_compress_blks;
145 s->rx_cqe_compress_pkts += rq_stats->cqe_compress_pkts;
147 for (j = 0; j < priv->params.num_tc; j++) {
148 sq_stats = &priv->channel[i]->sq[j].stats;
150 s->tx_packets += sq_stats->packets;
151 s->tx_bytes += sq_stats->bytes;
152 s->tx_tso_packets += sq_stats->tso_packets;
153 s->tx_tso_bytes += sq_stats->tso_bytes;
154 s->tx_tso_inner_packets += sq_stats->tso_inner_packets;
155 s->tx_tso_inner_bytes += sq_stats->tso_inner_bytes;
156 s->tx_queue_stopped += sq_stats->stopped;
157 s->tx_queue_wake += sq_stats->wake;
158 s->tx_queue_dropped += sq_stats->dropped;
159 s->tx_csum_partial_inner += sq_stats->csum_partial_inner;
160 tx_offload_none += sq_stats->csum_none;
164 /* Update calculated offload counters */
165 s->tx_csum_partial = s->tx_packets - tx_offload_none - s->tx_csum_partial_inner;
166 s->rx_csum_unnecessary = s->rx_packets - s->rx_csum_none - s->rx_csum_complete;
168 s->link_down_events_phy = MLX5_GET(ppcnt_reg,
169 priv->stats.pport.phy_counters,
170 counter_set.phys_layer_cntrs.link_down_events);
173 static void mlx5e_update_vport_counters(struct mlx5e_priv *priv)
175 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
176 u32 *out = (u32 *)priv->stats.vport.query_vport_out;
177 u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)];
178 struct mlx5_core_dev *mdev = priv->mdev;
180 memset(in, 0, sizeof(in));
182 MLX5_SET(query_vport_counter_in, in, opcode,
183 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
184 MLX5_SET(query_vport_counter_in, in, op_mod, 0);
185 MLX5_SET(query_vport_counter_in, in, other_vport, 0);
187 memset(out, 0, outlen);
189 mlx5_cmd_exec(mdev, in, sizeof(in), out, outlen);
192 static void mlx5e_update_pport_counters(struct mlx5e_priv *priv)
194 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
195 struct mlx5_core_dev *mdev = priv->mdev;
196 int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
201 in = mlx5_vzalloc(sz);
205 MLX5_SET(ppcnt_reg, in, local_port, 1);
207 out = pstats->IEEE_802_3_counters;
208 MLX5_SET(ppcnt_reg, in, grp, MLX5_IEEE_802_3_COUNTERS_GROUP);
209 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
211 out = pstats->RFC_2863_counters;
212 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2863_COUNTERS_GROUP);
213 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
215 out = pstats->RFC_2819_counters;
216 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2819_COUNTERS_GROUP);
217 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
219 out = pstats->phy_counters;
220 MLX5_SET(ppcnt_reg, in, grp, MLX5_PHYSICAL_LAYER_COUNTERS_GROUP);
221 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
223 MLX5_SET(ppcnt_reg, in, grp, MLX5_PER_PRIORITY_COUNTERS_GROUP);
224 for (prio = 0; prio < NUM_PPORT_PRIO; prio++) {
225 out = pstats->per_prio_counters[prio];
226 MLX5_SET(ppcnt_reg, in, prio_tc, prio);
227 mlx5_core_access_reg(mdev, in, sz, out, sz,
228 MLX5_REG_PPCNT, 0, 0);
235 static void mlx5e_update_q_counter(struct mlx5e_priv *priv)
237 struct mlx5e_qcounter_stats *qcnt = &priv->stats.qcnt;
239 if (!priv->q_counter)
242 mlx5_core_query_out_of_buffer(priv->mdev, priv->q_counter,
243 &qcnt->rx_out_of_buffer);
246 void mlx5e_update_stats(struct mlx5e_priv *priv)
248 mlx5e_update_q_counter(priv);
249 mlx5e_update_vport_counters(priv);
250 mlx5e_update_pport_counters(priv);
251 mlx5e_update_sw_counters(priv);
254 static void mlx5e_update_stats_work(struct work_struct *work)
256 struct delayed_work *dwork = to_delayed_work(work);
257 struct mlx5e_priv *priv = container_of(dwork, struct mlx5e_priv,
259 mutex_lock(&priv->state_lock);
260 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
261 mlx5e_update_stats(priv);
262 queue_delayed_work(priv->wq, dwork,
263 msecs_to_jiffies(MLX5E_UPDATE_STATS_INTERVAL));
265 mutex_unlock(&priv->state_lock);
268 static void mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
269 enum mlx5_dev_event event, unsigned long param)
271 struct mlx5e_priv *priv = vpriv;
273 if (!test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state))
277 case MLX5_DEV_EVENT_PORT_UP:
278 case MLX5_DEV_EVENT_PORT_DOWN:
279 queue_work(priv->wq, &priv->update_carrier_work);
287 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
289 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
292 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
294 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
295 synchronize_irq(mlx5_get_msix_vec(priv->mdev, MLX5_EQ_VEC_ASYNC));
298 #define MLX5E_HW2SW_MTU(hwmtu) (hwmtu - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
299 #define MLX5E_SW2HW_MTU(swmtu) (swmtu + (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
301 static int mlx5e_create_rq(struct mlx5e_channel *c,
302 struct mlx5e_rq_param *param,
305 struct mlx5e_priv *priv = c->priv;
306 struct mlx5_core_dev *mdev = priv->mdev;
307 void *rqc = param->rqc;
308 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
314 param->wq.db_numa_node = cpu_to_node(c->cpu);
316 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
321 rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
323 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
325 switch (priv->params.rq_wq_type) {
326 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
327 rq->wqe_info = kzalloc_node(wq_sz * sizeof(*rq->wqe_info),
328 GFP_KERNEL, cpu_to_node(c->cpu));
331 goto err_rq_wq_destroy;
333 rq->handle_rx_cqe = mlx5e_handle_rx_cqe_mpwrq;
334 rq->alloc_wqe = mlx5e_alloc_rx_mpwqe;
336 rq->mpwqe_stride_sz = BIT(priv->params.mpwqe_log_stride_sz);
337 rq->mpwqe_num_strides = BIT(priv->params.mpwqe_log_num_strides);
338 rq->wqe_sz = rq->mpwqe_stride_sz * rq->mpwqe_num_strides;
339 byte_count = rq->wqe_sz;
341 default: /* MLX5_WQ_TYPE_LINKED_LIST */
342 rq->skb = kzalloc_node(wq_sz * sizeof(*rq->skb), GFP_KERNEL,
343 cpu_to_node(c->cpu));
346 goto err_rq_wq_destroy;
348 rq->handle_rx_cqe = mlx5e_handle_rx_cqe;
349 rq->alloc_wqe = mlx5e_alloc_rx_wqe;
351 rq->wqe_sz = (priv->params.lro_en) ?
352 priv->params.lro_wqe_sz :
353 MLX5E_SW2HW_MTU(priv->netdev->mtu);
354 rq->wqe_sz = SKB_DATA_ALIGN(rq->wqe_sz);
355 byte_count = rq->wqe_sz;
356 byte_count |= MLX5_HW_START_PADDING;
359 for (i = 0; i < wq_sz; i++) {
360 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
362 wqe->data.byte_count = cpu_to_be32(byte_count);
365 rq->wq_type = priv->params.rq_wq_type;
367 rq->netdev = c->netdev;
368 rq->tstamp = &priv->tstamp;
372 rq->mkey_be = c->mkey_be;
373 rq->umr_mkey_be = cpu_to_be32(c->priv->umr_mkey.key);
378 mlx5_wq_destroy(&rq->wq_ctrl);
383 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
385 switch (rq->wq_type) {
386 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
389 default: /* MLX5_WQ_TYPE_LINKED_LIST */
393 mlx5_wq_destroy(&rq->wq_ctrl);
396 static int mlx5e_enable_rq(struct mlx5e_rq *rq, struct mlx5e_rq_param *param)
398 struct mlx5e_priv *priv = rq->priv;
399 struct mlx5_core_dev *mdev = priv->mdev;
407 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
408 sizeof(u64) * rq->wq_ctrl.buf.npages;
409 in = mlx5_vzalloc(inlen);
413 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
414 wq = MLX5_ADDR_OF(rqc, rqc, wq);
416 memcpy(rqc, param->rqc, sizeof(param->rqc));
418 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
419 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
420 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
421 MLX5_SET(rqc, rqc, vsd, priv->params.vlan_strip_disable);
422 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
423 MLX5_ADAPTER_PAGE_SHIFT);
424 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
426 mlx5_fill_page_array(&rq->wq_ctrl.buf,
427 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
429 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
436 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
439 struct mlx5e_channel *c = rq->channel;
440 struct mlx5e_priv *priv = c->priv;
441 struct mlx5_core_dev *mdev = priv->mdev;
448 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
449 in = mlx5_vzalloc(inlen);
453 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
455 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
456 MLX5_SET(rqc, rqc, state, next_state);
458 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
465 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
467 struct mlx5e_channel *c = rq->channel;
468 struct mlx5e_priv *priv = c->priv;
469 struct mlx5_core_dev *mdev = priv->mdev;
476 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
477 in = mlx5_vzalloc(inlen);
481 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
483 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
484 MLX5_SET64(modify_rq_in, in, modify_bitmask, MLX5_RQ_BITMASK_VSD);
485 MLX5_SET(rqc, rqc, vsd, vsd);
486 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
488 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
495 static void mlx5e_disable_rq(struct mlx5e_rq *rq)
497 mlx5_core_destroy_rq(rq->priv->mdev, rq->rqn);
500 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq)
502 unsigned long exp_time = jiffies + msecs_to_jiffies(20000);
503 struct mlx5e_channel *c = rq->channel;
504 struct mlx5e_priv *priv = c->priv;
505 struct mlx5_wq_ll *wq = &rq->wq;
507 while (time_before(jiffies, exp_time)) {
508 if (wq->cur_sz >= priv->params.min_rx_wqes)
517 static int mlx5e_open_rq(struct mlx5e_channel *c,
518 struct mlx5e_rq_param *param,
521 struct mlx5e_sq *sq = &c->icosq;
522 u16 pi = sq->pc & sq->wq.sz_m1;
525 err = mlx5e_create_rq(c, param, rq);
529 err = mlx5e_enable_rq(rq, param);
533 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
537 set_bit(MLX5E_RQ_STATE_POST_WQES_ENABLE, &rq->state);
539 sq->ico_wqe_info[pi].opcode = MLX5_OPCODE_NOP;
540 sq->ico_wqe_info[pi].num_wqebbs = 1;
541 mlx5e_send_nop(sq, true); /* trigger mlx5e_post_rx_wqes() */
546 mlx5e_disable_rq(rq);
548 mlx5e_destroy_rq(rq);
553 static void mlx5e_close_rq(struct mlx5e_rq *rq)
555 clear_bit(MLX5E_RQ_STATE_POST_WQES_ENABLE, &rq->state);
556 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
558 mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RDY, MLX5_RQC_STATE_ERR);
559 while (!mlx5_wq_ll_is_empty(&rq->wq))
562 /* avoid destroying rq before mlx5e_poll_rx_cq() is done with it */
563 napi_synchronize(&rq->channel->napi);
565 mlx5e_disable_rq(rq);
566 mlx5e_destroy_rq(rq);
569 static void mlx5e_free_sq_db(struct mlx5e_sq *sq)
576 static int mlx5e_alloc_sq_db(struct mlx5e_sq *sq, int numa)
578 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
579 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
581 sq->skb = kzalloc_node(wq_sz * sizeof(*sq->skb), GFP_KERNEL, numa);
582 sq->dma_fifo = kzalloc_node(df_sz * sizeof(*sq->dma_fifo), GFP_KERNEL,
584 sq->wqe_info = kzalloc_node(wq_sz * sizeof(*sq->wqe_info), GFP_KERNEL,
587 if (!sq->skb || !sq->dma_fifo || !sq->wqe_info) {
588 mlx5e_free_sq_db(sq);
592 sq->dma_fifo_mask = df_sz - 1;
597 static int mlx5e_create_sq(struct mlx5e_channel *c,
599 struct mlx5e_sq_param *param,
602 struct mlx5e_priv *priv = c->priv;
603 struct mlx5_core_dev *mdev = priv->mdev;
605 void *sqc = param->sqc;
606 void *sqc_wq = MLX5_ADDR_OF(sqc, sqc, wq);
609 err = mlx5_alloc_map_uar(mdev, &sq->uar, !!MLX5_CAP_GEN(mdev, bf));
613 param->wq.db_numa_node = cpu_to_node(c->cpu);
615 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq,
618 goto err_unmap_free_uar;
620 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
621 if (sq->uar.bf_map) {
622 set_bit(MLX5E_SQ_STATE_BF_ENABLE, &sq->state);
623 sq->uar_map = sq->uar.bf_map;
625 sq->uar_map = sq->uar.map;
627 sq->bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
628 sq->max_inline = param->max_inline;
630 err = mlx5e_alloc_sq_db(sq, cpu_to_node(c->cpu));
632 goto err_sq_wq_destroy;
635 u8 wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
637 sq->ico_wqe_info = kzalloc_node(sizeof(*sq->ico_wqe_info) *
640 cpu_to_node(c->cpu));
641 if (!sq->ico_wqe_info) {
648 txq_ix = c->ix + tc * priv->params.num_channels;
649 sq->txq = netdev_get_tx_queue(priv->netdev, txq_ix);
650 priv->txq_to_sq_map[txq_ix] = sq;
654 sq->tstamp = &priv->tstamp;
655 sq->mkey_be = c->mkey_be;
658 sq->edge = (sq->wq.sz_m1 + 1) - MLX5_SEND_WQE_MAX_WQEBBS;
659 sq->bf_budget = MLX5E_SQ_BF_BUDGET;
664 mlx5e_free_sq_db(sq);
667 mlx5_wq_destroy(&sq->wq_ctrl);
670 mlx5_unmap_free_uar(mdev, &sq->uar);
675 static void mlx5e_destroy_sq(struct mlx5e_sq *sq)
677 struct mlx5e_channel *c = sq->channel;
678 struct mlx5e_priv *priv = c->priv;
680 kfree(sq->ico_wqe_info);
681 mlx5e_free_sq_db(sq);
682 mlx5_wq_destroy(&sq->wq_ctrl);
683 mlx5_unmap_free_uar(priv->mdev, &sq->uar);
686 static int mlx5e_enable_sq(struct mlx5e_sq *sq, struct mlx5e_sq_param *param)
688 struct mlx5e_channel *c = sq->channel;
689 struct mlx5e_priv *priv = c->priv;
690 struct mlx5_core_dev *mdev = priv->mdev;
698 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
699 sizeof(u64) * sq->wq_ctrl.buf.npages;
700 in = mlx5_vzalloc(inlen);
704 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
705 wq = MLX5_ADDR_OF(sqc, sqc, wq);
707 memcpy(sqc, param->sqc, sizeof(param->sqc));
709 MLX5_SET(sqc, sqc, tis_num_0, param->icosq ? 0 : priv->tisn[sq->tc]);
710 MLX5_SET(sqc, sqc, cqn, sq->cq.mcq.cqn);
711 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
712 MLX5_SET(sqc, sqc, tis_lst_sz, param->icosq ? 0 : 1);
713 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
715 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
716 MLX5_SET(wq, wq, uar_page, sq->uar.index);
717 MLX5_SET(wq, wq, log_wq_pg_sz, sq->wq_ctrl.buf.page_shift -
718 MLX5_ADAPTER_PAGE_SHIFT);
719 MLX5_SET64(wq, wq, dbr_addr, sq->wq_ctrl.db.dma);
721 mlx5_fill_page_array(&sq->wq_ctrl.buf,
722 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
724 err = mlx5_core_create_sq(mdev, in, inlen, &sq->sqn);
731 static int mlx5e_modify_sq(struct mlx5e_sq *sq, int curr_state, int next_state)
733 struct mlx5e_channel *c = sq->channel;
734 struct mlx5e_priv *priv = c->priv;
735 struct mlx5_core_dev *mdev = priv->mdev;
742 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
743 in = mlx5_vzalloc(inlen);
747 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
749 MLX5_SET(modify_sq_in, in, sq_state, curr_state);
750 MLX5_SET(sqc, sqc, state, next_state);
752 err = mlx5_core_modify_sq(mdev, sq->sqn, in, inlen);
759 static void mlx5e_disable_sq(struct mlx5e_sq *sq)
761 struct mlx5e_channel *c = sq->channel;
762 struct mlx5e_priv *priv = c->priv;
763 struct mlx5_core_dev *mdev = priv->mdev;
765 mlx5_core_destroy_sq(mdev, sq->sqn);
768 static int mlx5e_open_sq(struct mlx5e_channel *c,
770 struct mlx5e_sq_param *param,
775 err = mlx5e_create_sq(c, tc, param, sq);
779 err = mlx5e_enable_sq(sq, param);
783 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST, MLX5_SQC_STATE_RDY);
788 set_bit(MLX5E_SQ_STATE_WAKE_TXQ_ENABLE, &sq->state);
789 netdev_tx_reset_queue(sq->txq);
790 netif_tx_start_queue(sq->txq);
796 mlx5e_disable_sq(sq);
798 mlx5e_destroy_sq(sq);
803 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
805 __netif_tx_lock_bh(txq);
806 netif_tx_stop_queue(txq);
807 __netif_tx_unlock_bh(txq);
810 static void mlx5e_close_sq(struct mlx5e_sq *sq)
816 clear_bit(MLX5E_SQ_STATE_WAKE_TXQ_ENABLE, &sq->state);
817 /* prevent netif_tx_wake_queue */
818 napi_synchronize(&sq->channel->napi);
819 netif_tx_disable_queue(sq->txq);
821 /* ensure hw is notified of all pending wqes */
822 if (mlx5e_sq_has_room_for(sq, 1))
823 mlx5e_send_nop(sq, true);
825 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY,
828 set_bit(MLX5E_SQ_STATE_TX_TIMEOUT, &sq->state);
831 /* wait till sq is empty, unless a TX timeout occurred on this SQ */
832 while (sq->cc != sq->pc &&
833 !test_bit(MLX5E_SQ_STATE_TX_TIMEOUT, &sq->state)) {
834 msleep(MLX5_EN_QP_FLUSH_MSLEEP_QUANT);
835 if (tout++ > MLX5_EN_QP_FLUSH_MAX_ITER)
836 set_bit(MLX5E_SQ_STATE_TX_TIMEOUT, &sq->state);
839 /* avoid destroying sq before mlx5e_poll_tx_cq() is done with it */
840 napi_synchronize(&sq->channel->napi);
842 mlx5e_free_tx_descs(sq);
843 mlx5e_disable_sq(sq);
844 mlx5e_destroy_sq(sq);
847 static int mlx5e_create_cq(struct mlx5e_channel *c,
848 struct mlx5e_cq_param *param,
851 struct mlx5e_priv *priv = c->priv;
852 struct mlx5_core_dev *mdev = priv->mdev;
853 struct mlx5_core_cq *mcq = &cq->mcq;
859 param->wq.buf_numa_node = cpu_to_node(c->cpu);
860 param->wq.db_numa_node = cpu_to_node(c->cpu);
861 param->eq_ix = c->ix;
863 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
868 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
873 mcq->set_ci_db = cq->wq_ctrl.db.db;
874 mcq->arm_db = cq->wq_ctrl.db.db + 1;
877 mcq->vector = param->eq_ix;
878 mcq->comp = mlx5e_completion_event;
879 mcq->event = mlx5e_cq_error_event;
881 mcq->uar = &priv->cq_uar;
883 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
884 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
895 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
897 mlx5_wq_destroy(&cq->wq_ctrl);
900 static int mlx5e_enable_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
902 struct mlx5e_priv *priv = cq->priv;
903 struct mlx5_core_dev *mdev = priv->mdev;
904 struct mlx5_core_cq *mcq = &cq->mcq;
909 unsigned int irqn_not_used;
913 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
914 sizeof(u64) * cq->wq_ctrl.buf.npages;
915 in = mlx5_vzalloc(inlen);
919 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
921 memcpy(cqc, param->cqc, sizeof(param->cqc));
923 mlx5_fill_page_array(&cq->wq_ctrl.buf,
924 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
926 mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
928 MLX5_SET(cqc, cqc, c_eqn, eqn);
929 MLX5_SET(cqc, cqc, uar_page, mcq->uar->index);
930 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
931 MLX5_ADAPTER_PAGE_SHIFT);
932 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
934 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
946 static void mlx5e_disable_cq(struct mlx5e_cq *cq)
948 struct mlx5e_priv *priv = cq->priv;
949 struct mlx5_core_dev *mdev = priv->mdev;
951 mlx5_core_destroy_cq(mdev, &cq->mcq);
954 static int mlx5e_open_cq(struct mlx5e_channel *c,
955 struct mlx5e_cq_param *param,
957 u16 moderation_usecs,
958 u16 moderation_frames)
961 struct mlx5e_priv *priv = c->priv;
962 struct mlx5_core_dev *mdev = priv->mdev;
964 err = mlx5e_create_cq(c, param, cq);
968 err = mlx5e_enable_cq(cq, param);
972 if (MLX5_CAP_GEN(mdev, cq_moderation))
973 mlx5_core_modify_cq_moderation(mdev, &cq->mcq,
979 mlx5e_destroy_cq(cq);
984 static void mlx5e_close_cq(struct mlx5e_cq *cq)
986 mlx5e_disable_cq(cq);
987 mlx5e_destroy_cq(cq);
990 static int mlx5e_get_cpu(struct mlx5e_priv *priv, int ix)
992 return cpumask_first(priv->mdev->priv.irq_info[ix].mask);
995 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
996 struct mlx5e_channel_param *cparam)
998 struct mlx5e_priv *priv = c->priv;
1002 for (tc = 0; tc < c->num_tc; tc++) {
1003 err = mlx5e_open_cq(c, &cparam->tx_cq, &c->sq[tc].cq,
1004 priv->params.tx_cq_moderation_usec,
1005 priv->params.tx_cq_moderation_pkts);
1007 goto err_close_tx_cqs;
1013 for (tc--; tc >= 0; tc--)
1014 mlx5e_close_cq(&c->sq[tc].cq);
1019 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1023 for (tc = 0; tc < c->num_tc; tc++)
1024 mlx5e_close_cq(&c->sq[tc].cq);
1027 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1028 struct mlx5e_channel_param *cparam)
1033 for (tc = 0; tc < c->num_tc; tc++) {
1034 err = mlx5e_open_sq(c, tc, &cparam->sq, &c->sq[tc]);
1042 for (tc--; tc >= 0; tc--)
1043 mlx5e_close_sq(&c->sq[tc]);
1048 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1052 for (tc = 0; tc < c->num_tc; tc++)
1053 mlx5e_close_sq(&c->sq[tc]);
1056 static void mlx5e_build_channeltc_to_txq_map(struct mlx5e_priv *priv, int ix)
1060 for (i = 0; i < MLX5E_MAX_NUM_TC; i++)
1061 priv->channeltc_to_txq_map[ix][i] =
1062 ix + i * priv->params.num_channels;
1065 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1066 struct mlx5e_channel_param *cparam,
1067 struct mlx5e_channel **cp)
1069 struct net_device *netdev = priv->netdev;
1070 int cpu = mlx5e_get_cpu(priv, ix);
1071 struct mlx5e_channel *c;
1074 c = kzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1081 c->pdev = &priv->mdev->pdev->dev;
1082 c->netdev = priv->netdev;
1083 c->mkey_be = cpu_to_be32(priv->mkey.key);
1084 c->num_tc = priv->params.num_tc;
1086 mlx5e_build_channeltc_to_txq_map(priv, ix);
1088 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1090 err = mlx5e_open_cq(c, &cparam->icosq_cq, &c->icosq.cq, 0, 0);
1094 err = mlx5e_open_tx_cqs(c, cparam);
1096 goto err_close_icosq_cq;
1098 err = mlx5e_open_cq(c, &cparam->rx_cq, &c->rq.cq,
1099 priv->params.rx_cq_moderation_usec,
1100 priv->params.rx_cq_moderation_pkts);
1102 goto err_close_tx_cqs;
1104 napi_enable(&c->napi);
1106 err = mlx5e_open_sq(c, 0, &cparam->icosq, &c->icosq);
1108 goto err_disable_napi;
1110 err = mlx5e_open_sqs(c, cparam);
1112 goto err_close_icosq;
1114 err = mlx5e_open_rq(c, &cparam->rq, &c->rq);
1118 netif_set_xps_queue(netdev, get_cpu_mask(c->cpu), ix);
1127 mlx5e_close_sq(&c->icosq);
1130 napi_disable(&c->napi);
1131 mlx5e_close_cq(&c->rq.cq);
1134 mlx5e_close_tx_cqs(c);
1137 mlx5e_close_cq(&c->icosq.cq);
1140 netif_napi_del(&c->napi);
1141 napi_hash_del(&c->napi);
1147 static void mlx5e_close_channel(struct mlx5e_channel *c)
1149 mlx5e_close_rq(&c->rq);
1151 mlx5e_close_sq(&c->icosq);
1152 napi_disable(&c->napi);
1153 mlx5e_close_cq(&c->rq.cq);
1154 mlx5e_close_tx_cqs(c);
1155 mlx5e_close_cq(&c->icosq.cq);
1156 netif_napi_del(&c->napi);
1158 napi_hash_del(&c->napi);
1164 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
1165 struct mlx5e_rq_param *param)
1167 void *rqc = param->rqc;
1168 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1170 switch (priv->params.rq_wq_type) {
1171 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1172 MLX5_SET(wq, wq, log_wqe_num_of_strides,
1173 priv->params.mpwqe_log_num_strides - 9);
1174 MLX5_SET(wq, wq, log_wqe_stride_size,
1175 priv->params.mpwqe_log_stride_sz - 6);
1176 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ);
1178 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1179 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1182 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1183 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1184 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_rq_size);
1185 MLX5_SET(wq, wq, pd, priv->pdn);
1186 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
1188 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1189 param->wq.linear = 1;
1192 static void mlx5e_build_drop_rq_param(struct mlx5e_rq_param *param)
1194 void *rqc = param->rqc;
1195 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1197 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1198 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1201 static void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
1202 struct mlx5e_sq_param *param)
1204 void *sqc = param->sqc;
1205 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1207 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1208 MLX5_SET(wq, wq, pd, priv->pdn);
1210 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1213 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
1214 struct mlx5e_sq_param *param)
1216 void *sqc = param->sqc;
1217 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1219 mlx5e_build_sq_param_common(priv, param);
1220 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_sq_size);
1222 param->max_inline = priv->params.tx_max_inline;
1225 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1226 struct mlx5e_cq_param *param)
1228 void *cqc = param->cqc;
1230 MLX5_SET(cqc, cqc, uar_page, priv->cq_uar.index);
1233 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
1234 struct mlx5e_cq_param *param)
1236 void *cqc = param->cqc;
1239 switch (priv->params.rq_wq_type) {
1240 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1241 log_cq_size = priv->params.log_rq_size +
1242 priv->params.mpwqe_log_num_strides;
1244 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1245 log_cq_size = priv->params.log_rq_size;
1248 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
1249 if (priv->params.rx_cqe_compress) {
1250 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
1251 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
1254 mlx5e_build_common_cq_param(priv, param);
1257 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
1258 struct mlx5e_cq_param *param)
1260 void *cqc = param->cqc;
1262 MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_sq_size);
1264 mlx5e_build_common_cq_param(priv, param);
1267 static void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
1268 struct mlx5e_cq_param *param,
1271 void *cqc = param->cqc;
1273 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
1275 mlx5e_build_common_cq_param(priv, param);
1278 static void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
1279 struct mlx5e_sq_param *param,
1282 void *sqc = param->sqc;
1283 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1285 mlx5e_build_sq_param_common(priv, param);
1287 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
1288 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
1290 param->icosq = true;
1293 static void mlx5e_build_channel_param(struct mlx5e_priv *priv, struct mlx5e_channel_param *cparam)
1295 u8 icosq_log_wq_sz = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
1297 mlx5e_build_rq_param(priv, &cparam->rq);
1298 mlx5e_build_sq_param(priv, &cparam->sq);
1299 mlx5e_build_icosq_param(priv, &cparam->icosq, icosq_log_wq_sz);
1300 mlx5e_build_rx_cq_param(priv, &cparam->rx_cq);
1301 mlx5e_build_tx_cq_param(priv, &cparam->tx_cq);
1302 mlx5e_build_ico_cq_param(priv, &cparam->icosq_cq, icosq_log_wq_sz);
1305 static int mlx5e_open_channels(struct mlx5e_priv *priv)
1307 struct mlx5e_channel_param *cparam;
1308 int nch = priv->params.num_channels;
1313 priv->channel = kcalloc(nch, sizeof(struct mlx5e_channel *),
1316 priv->txq_to_sq_map = kcalloc(nch * priv->params.num_tc,
1317 sizeof(struct mlx5e_sq *), GFP_KERNEL);
1319 cparam = kzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
1321 if (!priv->channel || !priv->txq_to_sq_map || !cparam)
1322 goto err_free_txq_to_sq_map;
1324 mlx5e_build_channel_param(priv, cparam);
1326 for (i = 0; i < nch; i++) {
1327 err = mlx5e_open_channel(priv, i, cparam, &priv->channel[i]);
1329 goto err_close_channels;
1332 for (j = 0; j < nch; j++) {
1333 err = mlx5e_wait_for_min_rx_wqes(&priv->channel[j]->rq);
1335 goto err_close_channels;
1342 for (i--; i >= 0; i--)
1343 mlx5e_close_channel(priv->channel[i]);
1345 err_free_txq_to_sq_map:
1346 kfree(priv->txq_to_sq_map);
1347 kfree(priv->channel);
1353 static void mlx5e_close_channels(struct mlx5e_priv *priv)
1357 for (i = 0; i < priv->params.num_channels; i++)
1358 mlx5e_close_channel(priv->channel[i]);
1360 kfree(priv->txq_to_sq_map);
1361 kfree(priv->channel);
1364 static int mlx5e_rx_hash_fn(int hfunc)
1366 return (hfunc == ETH_RSS_HASH_TOP) ?
1367 MLX5_RX_HASH_FN_TOEPLITZ :
1368 MLX5_RX_HASH_FN_INVERTED_XOR8;
1371 static int mlx5e_bits_invert(unsigned long a, int size)
1376 for (i = 0; i < size; i++)
1377 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
1382 static void mlx5e_fill_indir_rqt_rqns(struct mlx5e_priv *priv, void *rqtc)
1386 for (i = 0; i < MLX5E_INDIR_RQT_SIZE; i++) {
1390 if (priv->params.rss_hfunc == ETH_RSS_HASH_XOR)
1391 ix = mlx5e_bits_invert(i, MLX5E_LOG_INDIR_RQT_SIZE);
1393 ix = priv->params.indirection_rqt[ix];
1394 rqn = test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1395 priv->channel[ix]->rq.rqn :
1397 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
1401 static void mlx5e_fill_direct_rqt_rqn(struct mlx5e_priv *priv, void *rqtc,
1404 u32 rqn = test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1405 priv->channel[ix]->rq.rqn :
1408 MLX5_SET(rqtc, rqtc, rq_num[0], rqn);
1411 static int mlx5e_create_rqt(struct mlx5e_priv *priv, int sz, int ix, u32 *rqtn)
1413 struct mlx5_core_dev *mdev = priv->mdev;
1419 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
1420 in = mlx5_vzalloc(inlen);
1424 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
1426 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1427 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
1429 if (sz > 1) /* RSS */
1430 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1432 mlx5e_fill_direct_rqt_rqn(priv, rqtc, ix);
1434 err = mlx5_core_create_rqt(mdev, in, inlen, rqtn);
1440 static void mlx5e_destroy_rqt(struct mlx5e_priv *priv, u32 rqtn)
1442 mlx5_core_destroy_rqt(priv->mdev, rqtn);
1445 static int mlx5e_create_rqts(struct mlx5e_priv *priv)
1447 int nch = mlx5e_get_max_num_channels(priv->mdev);
1453 rqtn = &priv->indir_rqtn;
1454 err = mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, 0, rqtn);
1459 for (ix = 0; ix < nch; ix++) {
1460 rqtn = &priv->direct_tir[ix].rqtn;
1461 err = mlx5e_create_rqt(priv, 1 /*size */, ix, rqtn);
1463 goto err_destroy_rqts;
1469 for (ix--; ix >= 0; ix--)
1470 mlx5e_destroy_rqt(priv, priv->direct_tir[ix].rqtn);
1472 mlx5e_destroy_rqt(priv, priv->indir_rqtn);
1477 static void mlx5e_destroy_rqts(struct mlx5e_priv *priv)
1479 int nch = mlx5e_get_max_num_channels(priv->mdev);
1482 for (i = 0; i < nch; i++)
1483 mlx5e_destroy_rqt(priv, priv->direct_tir[i].rqtn);
1485 mlx5e_destroy_rqt(priv, priv->indir_rqtn);
1488 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz, int ix)
1490 struct mlx5_core_dev *mdev = priv->mdev;
1496 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
1497 in = mlx5_vzalloc(inlen);
1501 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
1503 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1504 if (sz > 1) /* RSS */
1505 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1507 mlx5e_fill_direct_rqt_rqn(priv, rqtc, ix);
1509 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
1511 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
1518 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv)
1523 rqtn = priv->indir_rqtn;
1524 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, 0);
1525 for (ix = 0; ix < priv->params.num_channels; ix++) {
1526 rqtn = priv->direct_tir[ix].rqtn;
1527 mlx5e_redirect_rqt(priv, rqtn, 1, ix);
1531 static void mlx5e_build_tir_ctx_lro(void *tirc, struct mlx5e_priv *priv)
1533 if (!priv->params.lro_en)
1536 #define ROUGH_MAX_L2_L3_HDR_SZ 256
1538 MLX5_SET(tirc, tirc, lro_enable_mask,
1539 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
1540 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
1541 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
1542 (priv->params.lro_wqe_sz -
1543 ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
1544 MLX5_SET(tirc, tirc, lro_timeout_period_usecs,
1545 MLX5_CAP_ETH(priv->mdev,
1546 lro_timer_supported_periods[2]));
1549 void mlx5e_build_tir_ctx_hash(void *tirc, struct mlx5e_priv *priv)
1551 MLX5_SET(tirc, tirc, rx_hash_fn,
1552 mlx5e_rx_hash_fn(priv->params.rss_hfunc));
1553 if (priv->params.rss_hfunc == ETH_RSS_HASH_TOP) {
1554 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
1555 rx_hash_toeplitz_key);
1556 size_t len = MLX5_FLD_SZ_BYTES(tirc,
1557 rx_hash_toeplitz_key);
1559 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1560 memcpy(rss_key, priv->params.toeplitz_hash_key, len);
1564 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
1566 struct mlx5_core_dev *mdev = priv->mdev;
1575 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
1576 in = mlx5_vzalloc(inlen);
1580 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
1581 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
1583 mlx5e_build_tir_ctx_lro(tirc, priv);
1585 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
1586 err = mlx5_core_modify_tir(mdev, priv->indir_tirn[tt], in,
1592 for (ix = 0; ix < mlx5e_get_max_num_channels(mdev); ix++) {
1593 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
1605 static int mlx5e_refresh_tirs_self_loopback_enable(struct mlx5e_priv *priv)
1612 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
1613 in = mlx5_vzalloc(inlen);
1617 MLX5_SET(modify_tir_in, in, bitmask.self_lb_en, 1);
1619 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++) {
1620 err = mlx5_core_modify_tir(priv->mdev, priv->indir_tirn[i], in,
1626 for (i = 0; i < priv->params.num_channels; i++) {
1627 err = mlx5_core_modify_tir(priv->mdev,
1628 priv->direct_tir[i].tirn, in,
1639 static int mlx5e_set_mtu(struct mlx5e_priv *priv, u16 mtu)
1641 struct mlx5_core_dev *mdev = priv->mdev;
1642 u16 hw_mtu = MLX5E_SW2HW_MTU(mtu);
1645 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
1649 /* Update vport context MTU */
1650 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
1654 static void mlx5e_query_mtu(struct mlx5e_priv *priv, u16 *mtu)
1656 struct mlx5_core_dev *mdev = priv->mdev;
1660 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
1661 if (err || !hw_mtu) /* fallback to port oper mtu */
1662 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
1664 *mtu = MLX5E_HW2SW_MTU(hw_mtu);
1667 static int mlx5e_set_dev_port_mtu(struct net_device *netdev)
1669 struct mlx5e_priv *priv = netdev_priv(netdev);
1673 err = mlx5e_set_mtu(priv, netdev->mtu);
1677 mlx5e_query_mtu(priv, &mtu);
1678 if (mtu != netdev->mtu)
1679 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
1680 __func__, mtu, netdev->mtu);
1686 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
1688 struct mlx5e_priv *priv = netdev_priv(netdev);
1689 int nch = priv->params.num_channels;
1690 int ntc = priv->params.num_tc;
1693 netdev_reset_tc(netdev);
1698 netdev_set_num_tc(netdev, ntc);
1700 for (tc = 0; tc < ntc; tc++)
1701 netdev_set_tc_queue(netdev, tc, nch, tc * nch);
1704 int mlx5e_open_locked(struct net_device *netdev)
1706 struct mlx5e_priv *priv = netdev_priv(netdev);
1710 set_bit(MLX5E_STATE_OPENED, &priv->state);
1712 mlx5e_netdev_set_tcs(netdev);
1714 num_txqs = priv->params.num_channels * priv->params.num_tc;
1715 netif_set_real_num_tx_queues(netdev, num_txqs);
1716 netif_set_real_num_rx_queues(netdev, priv->params.num_channels);
1718 err = mlx5e_set_dev_port_mtu(netdev);
1720 goto err_clear_state_opened_flag;
1722 err = mlx5e_open_channels(priv);
1724 netdev_err(netdev, "%s: mlx5e_open_channels failed, %d\n",
1726 goto err_clear_state_opened_flag;
1729 err = mlx5e_refresh_tirs_self_loopback_enable(priv);
1731 netdev_err(netdev, "%s: mlx5e_refresh_tirs_self_loopback_enable failed, %d\n",
1733 goto err_close_channels;
1736 mlx5e_redirect_rqts(priv);
1737 mlx5e_update_carrier(priv);
1738 mlx5e_timestamp_init(priv);
1739 #ifdef CONFIG_RFS_ACCEL
1740 priv->netdev->rx_cpu_rmap = priv->mdev->rmap;
1743 queue_delayed_work(priv->wq, &priv->update_stats_work, 0);
1748 mlx5e_close_channels(priv);
1749 err_clear_state_opened_flag:
1750 clear_bit(MLX5E_STATE_OPENED, &priv->state);
1754 static int mlx5e_open(struct net_device *netdev)
1756 struct mlx5e_priv *priv = netdev_priv(netdev);
1759 mutex_lock(&priv->state_lock);
1760 err = mlx5e_open_locked(netdev);
1761 mutex_unlock(&priv->state_lock);
1766 int mlx5e_close_locked(struct net_device *netdev)
1768 struct mlx5e_priv *priv = netdev_priv(netdev);
1770 /* May already be CLOSED in case a previous configuration operation
1771 * (e.g RX/TX queue size change) that involves close&open failed.
1773 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
1776 clear_bit(MLX5E_STATE_OPENED, &priv->state);
1778 mlx5e_timestamp_cleanup(priv);
1779 netif_carrier_off(priv->netdev);
1780 mlx5e_redirect_rqts(priv);
1781 mlx5e_close_channels(priv);
1786 static int mlx5e_close(struct net_device *netdev)
1788 struct mlx5e_priv *priv = netdev_priv(netdev);
1791 mutex_lock(&priv->state_lock);
1792 err = mlx5e_close_locked(netdev);
1793 mutex_unlock(&priv->state_lock);
1798 static int mlx5e_create_drop_rq(struct mlx5e_priv *priv,
1799 struct mlx5e_rq *rq,
1800 struct mlx5e_rq_param *param)
1802 struct mlx5_core_dev *mdev = priv->mdev;
1803 void *rqc = param->rqc;
1804 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
1807 param->wq.db_numa_node = param->wq.buf_numa_node;
1809 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
1819 static int mlx5e_create_drop_cq(struct mlx5e_priv *priv,
1820 struct mlx5e_cq *cq,
1821 struct mlx5e_cq_param *param)
1823 struct mlx5_core_dev *mdev = priv->mdev;
1824 struct mlx5_core_cq *mcq = &cq->mcq;
1829 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1834 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1837 mcq->set_ci_db = cq->wq_ctrl.db.db;
1838 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1839 *mcq->set_ci_db = 0;
1841 mcq->vector = param->eq_ix;
1842 mcq->comp = mlx5e_completion_event;
1843 mcq->event = mlx5e_cq_error_event;
1845 mcq->uar = &priv->cq_uar;
1852 static int mlx5e_open_drop_rq(struct mlx5e_priv *priv)
1854 struct mlx5e_cq_param cq_param;
1855 struct mlx5e_rq_param rq_param;
1856 struct mlx5e_rq *rq = &priv->drop_rq;
1857 struct mlx5e_cq *cq = &priv->drop_rq.cq;
1860 memset(&cq_param, 0, sizeof(cq_param));
1861 memset(&rq_param, 0, sizeof(rq_param));
1862 mlx5e_build_drop_rq_param(&rq_param);
1864 err = mlx5e_create_drop_cq(priv, cq, &cq_param);
1868 err = mlx5e_enable_cq(cq, &cq_param);
1870 goto err_destroy_cq;
1872 err = mlx5e_create_drop_rq(priv, rq, &rq_param);
1874 goto err_disable_cq;
1876 err = mlx5e_enable_rq(rq, &rq_param);
1878 goto err_destroy_rq;
1883 mlx5e_destroy_rq(&priv->drop_rq);
1886 mlx5e_disable_cq(&priv->drop_rq.cq);
1889 mlx5e_destroy_cq(&priv->drop_rq.cq);
1894 static void mlx5e_close_drop_rq(struct mlx5e_priv *priv)
1896 mlx5e_disable_rq(&priv->drop_rq);
1897 mlx5e_destroy_rq(&priv->drop_rq);
1898 mlx5e_disable_cq(&priv->drop_rq.cq);
1899 mlx5e_destroy_cq(&priv->drop_rq.cq);
1902 static int mlx5e_create_tis(struct mlx5e_priv *priv, int tc)
1904 struct mlx5_core_dev *mdev = priv->mdev;
1905 u32 in[MLX5_ST_SZ_DW(create_tis_in)];
1906 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1908 memset(in, 0, sizeof(in));
1910 MLX5_SET(tisc, tisc, prio, tc << 1);
1911 MLX5_SET(tisc, tisc, transport_domain, priv->tdn);
1913 return mlx5_core_create_tis(mdev, in, sizeof(in), &priv->tisn[tc]);
1916 static void mlx5e_destroy_tis(struct mlx5e_priv *priv, int tc)
1918 mlx5_core_destroy_tis(priv->mdev, priv->tisn[tc]);
1921 static int mlx5e_create_tises(struct mlx5e_priv *priv)
1926 for (tc = 0; tc < MLX5E_MAX_NUM_TC; tc++) {
1927 err = mlx5e_create_tis(priv, tc);
1929 goto err_close_tises;
1935 for (tc--; tc >= 0; tc--)
1936 mlx5e_destroy_tis(priv, tc);
1941 static void mlx5e_destroy_tises(struct mlx5e_priv *priv)
1945 for (tc = 0; tc < MLX5E_MAX_NUM_TC; tc++)
1946 mlx5e_destroy_tis(priv, tc);
1949 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv, u32 *tirc,
1950 enum mlx5e_traffic_types tt)
1952 void *hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1954 MLX5_SET(tirc, tirc, transport_domain, priv->tdn);
1956 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
1957 MLX5_HASH_FIELD_SEL_DST_IP)
1959 #define MLX5_HASH_IP_L4PORTS (MLX5_HASH_FIELD_SEL_SRC_IP |\
1960 MLX5_HASH_FIELD_SEL_DST_IP |\
1961 MLX5_HASH_FIELD_SEL_L4_SPORT |\
1962 MLX5_HASH_FIELD_SEL_L4_DPORT)
1964 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
1965 MLX5_HASH_FIELD_SEL_DST_IP |\
1966 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
1968 mlx5e_build_tir_ctx_lro(tirc, priv);
1970 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
1971 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqtn);
1972 mlx5e_build_tir_ctx_hash(tirc, priv);
1975 case MLX5E_TT_IPV4_TCP:
1976 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1977 MLX5_L3_PROT_TYPE_IPV4);
1978 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1979 MLX5_L4_PROT_TYPE_TCP);
1980 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1981 MLX5_HASH_IP_L4PORTS);
1984 case MLX5E_TT_IPV6_TCP:
1985 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1986 MLX5_L3_PROT_TYPE_IPV6);
1987 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1988 MLX5_L4_PROT_TYPE_TCP);
1989 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1990 MLX5_HASH_IP_L4PORTS);
1993 case MLX5E_TT_IPV4_UDP:
1994 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1995 MLX5_L3_PROT_TYPE_IPV4);
1996 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1997 MLX5_L4_PROT_TYPE_UDP);
1998 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1999 MLX5_HASH_IP_L4PORTS);
2002 case MLX5E_TT_IPV6_UDP:
2003 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2004 MLX5_L3_PROT_TYPE_IPV6);
2005 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2006 MLX5_L4_PROT_TYPE_UDP);
2007 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2008 MLX5_HASH_IP_L4PORTS);
2011 case MLX5E_TT_IPV4_IPSEC_AH:
2012 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2013 MLX5_L3_PROT_TYPE_IPV4);
2014 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2015 MLX5_HASH_IP_IPSEC_SPI);
2018 case MLX5E_TT_IPV6_IPSEC_AH:
2019 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2020 MLX5_L3_PROT_TYPE_IPV6);
2021 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2022 MLX5_HASH_IP_IPSEC_SPI);
2025 case MLX5E_TT_IPV4_IPSEC_ESP:
2026 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2027 MLX5_L3_PROT_TYPE_IPV4);
2028 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2029 MLX5_HASH_IP_IPSEC_SPI);
2032 case MLX5E_TT_IPV6_IPSEC_ESP:
2033 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2034 MLX5_L3_PROT_TYPE_IPV6);
2035 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2036 MLX5_HASH_IP_IPSEC_SPI);
2040 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2041 MLX5_L3_PROT_TYPE_IPV4);
2042 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2047 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2048 MLX5_L3_PROT_TYPE_IPV6);
2049 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2054 "mlx5e_build_indir_tir_ctx: bad traffic type!\n");
2058 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 *tirc,
2061 MLX5_SET(tirc, tirc, transport_domain, priv->tdn);
2063 mlx5e_build_tir_ctx_lro(tirc, priv);
2065 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2066 MLX5_SET(tirc, tirc, indirect_table, rqtn);
2067 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
2070 static int mlx5e_create_tirs(struct mlx5e_priv *priv)
2072 int nch = mlx5e_get_max_num_channels(priv->mdev);
2081 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2082 in = mlx5_vzalloc(inlen);
2087 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2088 memset(in, 0, inlen);
2089 tirn = &priv->indir_tirn[tt];
2090 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2091 mlx5e_build_indir_tir_ctx(priv, tirc, tt);
2092 err = mlx5_core_create_tir(priv->mdev, in, inlen, tirn);
2094 goto err_destroy_tirs;
2098 for (ix = 0; ix < nch; ix++) {
2099 memset(in, 0, inlen);
2100 tirn = &priv->direct_tir[ix].tirn;
2101 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2102 mlx5e_build_direct_tir_ctx(priv, tirc,
2103 priv->direct_tir[ix].rqtn);
2104 err = mlx5_core_create_tir(priv->mdev, in, inlen, tirn);
2106 goto err_destroy_ch_tirs;
2113 err_destroy_ch_tirs:
2114 for (ix--; ix >= 0; ix--)
2115 mlx5_core_destroy_tir(priv->mdev, priv->direct_tir[ix].tirn);
2118 for (tt--; tt >= 0; tt--)
2119 mlx5_core_destroy_tir(priv->mdev, priv->indir_tirn[tt]);
2126 static void mlx5e_destroy_tirs(struct mlx5e_priv *priv)
2128 int nch = mlx5e_get_max_num_channels(priv->mdev);
2131 for (i = 0; i < nch; i++)
2132 mlx5_core_destroy_tir(priv->mdev, priv->direct_tir[i].tirn);
2134 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
2135 mlx5_core_destroy_tir(priv->mdev, priv->indir_tirn[i]);
2138 int mlx5e_modify_rqs_vsd(struct mlx5e_priv *priv, bool vsd)
2143 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
2146 for (i = 0; i < priv->params.num_channels; i++) {
2147 err = mlx5e_modify_rq_vsd(&priv->channel[i]->rq, vsd);
2155 static int mlx5e_setup_tc(struct net_device *netdev, u8 tc)
2157 struct mlx5e_priv *priv = netdev_priv(netdev);
2161 if (tc && tc != MLX5E_MAX_NUM_TC)
2164 mutex_lock(&priv->state_lock);
2166 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2168 mlx5e_close_locked(priv->netdev);
2170 priv->params.num_tc = tc ? tc : 1;
2173 err = mlx5e_open_locked(priv->netdev);
2175 mutex_unlock(&priv->state_lock);
2180 static int mlx5e_ndo_setup_tc(struct net_device *dev, u32 handle,
2181 __be16 proto, struct tc_to_netdev *tc)
2183 struct mlx5e_priv *priv = netdev_priv(dev);
2185 if (TC_H_MAJ(handle) != TC_H_MAJ(TC_H_INGRESS))
2189 case TC_SETUP_CLSFLOWER:
2190 switch (tc->cls_flower->command) {
2191 case TC_CLSFLOWER_REPLACE:
2192 return mlx5e_configure_flower(priv, proto, tc->cls_flower);
2193 case TC_CLSFLOWER_DESTROY:
2194 return mlx5e_delete_flower(priv, tc->cls_flower);
2195 case TC_CLSFLOWER_STATS:
2196 return mlx5e_stats_flower(priv, tc->cls_flower);
2203 if (tc->type != TC_SETUP_MQPRIO)
2206 return mlx5e_setup_tc(dev, tc->tc);
2209 static struct rtnl_link_stats64 *
2210 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
2212 struct mlx5e_priv *priv = netdev_priv(dev);
2213 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
2214 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
2215 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
2217 stats->rx_packets = sstats->rx_packets;
2218 stats->rx_bytes = sstats->rx_bytes;
2219 stats->tx_packets = sstats->tx_packets;
2220 stats->tx_bytes = sstats->tx_bytes;
2222 stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
2223 stats->tx_dropped = sstats->tx_queue_dropped;
2225 stats->rx_length_errors =
2226 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
2227 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
2228 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
2229 stats->rx_crc_errors =
2230 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
2231 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
2232 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
2233 stats->tx_carrier_errors =
2234 PPORT_802_3_GET(pstats, a_symbol_error_during_carrier);
2235 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
2236 stats->rx_frame_errors;
2237 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
2239 /* vport multicast also counts packets that are dropped due to steering
2240 * or rx out of buffer
2243 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
2248 static void mlx5e_set_rx_mode(struct net_device *dev)
2250 struct mlx5e_priv *priv = netdev_priv(dev);
2252 queue_work(priv->wq, &priv->set_rx_mode_work);
2255 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
2257 struct mlx5e_priv *priv = netdev_priv(netdev);
2258 struct sockaddr *saddr = addr;
2260 if (!is_valid_ether_addr(saddr->sa_data))
2261 return -EADDRNOTAVAIL;
2263 netif_addr_lock_bh(netdev);
2264 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
2265 netif_addr_unlock_bh(netdev);
2267 queue_work(priv->wq, &priv->set_rx_mode_work);
2272 #define MLX5E_SET_FEATURE(netdev, feature, enable) \
2275 netdev->features |= feature; \
2277 netdev->features &= ~feature; \
2280 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
2282 static int set_feature_lro(struct net_device *netdev, bool enable)
2284 struct mlx5e_priv *priv = netdev_priv(netdev);
2285 bool was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2288 mutex_lock(&priv->state_lock);
2290 if (was_opened && (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST))
2291 mlx5e_close_locked(priv->netdev);
2293 priv->params.lro_en = enable;
2294 err = mlx5e_modify_tirs_lro(priv);
2296 netdev_err(netdev, "lro modify failed, %d\n", err);
2297 priv->params.lro_en = !enable;
2300 if (was_opened && (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST))
2301 mlx5e_open_locked(priv->netdev);
2303 mutex_unlock(&priv->state_lock);
2308 static int set_feature_vlan_filter(struct net_device *netdev, bool enable)
2310 struct mlx5e_priv *priv = netdev_priv(netdev);
2313 mlx5e_enable_vlan_filter(priv);
2315 mlx5e_disable_vlan_filter(priv);
2320 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
2322 struct mlx5e_priv *priv = netdev_priv(netdev);
2324 if (!enable && mlx5e_tc_num_filters(priv)) {
2326 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
2333 static int set_feature_rx_all(struct net_device *netdev, bool enable)
2335 struct mlx5e_priv *priv = netdev_priv(netdev);
2336 struct mlx5_core_dev *mdev = priv->mdev;
2338 return mlx5_set_port_fcs(mdev, !enable);
2341 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
2343 struct mlx5e_priv *priv = netdev_priv(netdev);
2346 mutex_lock(&priv->state_lock);
2348 priv->params.vlan_strip_disable = !enable;
2349 err = mlx5e_modify_rqs_vsd(priv, !enable);
2351 priv->params.vlan_strip_disable = enable;
2353 mutex_unlock(&priv->state_lock);
2358 #ifdef CONFIG_RFS_ACCEL
2359 static int set_feature_arfs(struct net_device *netdev, bool enable)
2361 struct mlx5e_priv *priv = netdev_priv(netdev);
2365 err = mlx5e_arfs_enable(priv);
2367 err = mlx5e_arfs_disable(priv);
2373 static int mlx5e_handle_feature(struct net_device *netdev,
2374 netdev_features_t wanted_features,
2375 netdev_features_t feature,
2376 mlx5e_feature_handler feature_handler)
2378 netdev_features_t changes = wanted_features ^ netdev->features;
2379 bool enable = !!(wanted_features & feature);
2382 if (!(changes & feature))
2385 err = feature_handler(netdev, enable);
2387 netdev_err(netdev, "%s feature 0x%llx failed err %d\n",
2388 enable ? "Enable" : "Disable", feature, err);
2392 MLX5E_SET_FEATURE(netdev, feature, enable);
2396 static int mlx5e_set_features(struct net_device *netdev,
2397 netdev_features_t features)
2401 err = mlx5e_handle_feature(netdev, features, NETIF_F_LRO,
2403 err |= mlx5e_handle_feature(netdev, features,
2404 NETIF_F_HW_VLAN_CTAG_FILTER,
2405 set_feature_vlan_filter);
2406 err |= mlx5e_handle_feature(netdev, features, NETIF_F_HW_TC,
2407 set_feature_tc_num_filters);
2408 err |= mlx5e_handle_feature(netdev, features, NETIF_F_RXALL,
2409 set_feature_rx_all);
2410 err |= mlx5e_handle_feature(netdev, features, NETIF_F_HW_VLAN_CTAG_RX,
2411 set_feature_rx_vlan);
2412 #ifdef CONFIG_RFS_ACCEL
2413 err |= mlx5e_handle_feature(netdev, features, NETIF_F_NTUPLE,
2417 return err ? -EINVAL : 0;
2420 #define MXL5_HW_MIN_MTU 64
2421 #define MXL5E_MIN_MTU (MXL5_HW_MIN_MTU + ETH_FCS_LEN)
2423 static int mlx5e_change_mtu(struct net_device *netdev, int new_mtu)
2425 struct mlx5e_priv *priv = netdev_priv(netdev);
2426 struct mlx5_core_dev *mdev = priv->mdev;
2432 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
2434 max_mtu = MLX5E_HW2SW_MTU(max_mtu);
2435 min_mtu = MLX5E_HW2SW_MTU(MXL5E_MIN_MTU);
2437 if (new_mtu > max_mtu || new_mtu < min_mtu) {
2439 "%s: Bad MTU (%d), valid range is: [%d..%d]\n",
2440 __func__, new_mtu, min_mtu, max_mtu);
2444 mutex_lock(&priv->state_lock);
2446 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2448 mlx5e_close_locked(netdev);
2450 netdev->mtu = new_mtu;
2453 err = mlx5e_open_locked(netdev);
2455 mutex_unlock(&priv->state_lock);
2460 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2464 return mlx5e_hwstamp_set(dev, ifr);
2466 return mlx5e_hwstamp_get(dev, ifr);
2472 static int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2474 struct mlx5e_priv *priv = netdev_priv(dev);
2475 struct mlx5_core_dev *mdev = priv->mdev;
2477 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
2480 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos)
2482 struct mlx5e_priv *priv = netdev_priv(dev);
2483 struct mlx5_core_dev *mdev = priv->mdev;
2485 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
2489 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2491 struct mlx5e_priv *priv = netdev_priv(dev);
2492 struct mlx5_core_dev *mdev = priv->mdev;
2494 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
2497 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
2499 struct mlx5e_priv *priv = netdev_priv(dev);
2500 struct mlx5_core_dev *mdev = priv->mdev;
2502 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
2504 static int mlx5_vport_link2ifla(u8 esw_link)
2507 case MLX5_ESW_VPORT_ADMIN_STATE_DOWN:
2508 return IFLA_VF_LINK_STATE_DISABLE;
2509 case MLX5_ESW_VPORT_ADMIN_STATE_UP:
2510 return IFLA_VF_LINK_STATE_ENABLE;
2512 return IFLA_VF_LINK_STATE_AUTO;
2515 static int mlx5_ifla_link2vport(u8 ifla_link)
2517 switch (ifla_link) {
2518 case IFLA_VF_LINK_STATE_DISABLE:
2519 return MLX5_ESW_VPORT_ADMIN_STATE_DOWN;
2520 case IFLA_VF_LINK_STATE_ENABLE:
2521 return MLX5_ESW_VPORT_ADMIN_STATE_UP;
2523 return MLX5_ESW_VPORT_ADMIN_STATE_AUTO;
2526 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
2529 struct mlx5e_priv *priv = netdev_priv(dev);
2530 struct mlx5_core_dev *mdev = priv->mdev;
2532 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
2533 mlx5_ifla_link2vport(link_state));
2536 static int mlx5e_get_vf_config(struct net_device *dev,
2537 int vf, struct ifla_vf_info *ivi)
2539 struct mlx5e_priv *priv = netdev_priv(dev);
2540 struct mlx5_core_dev *mdev = priv->mdev;
2543 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
2546 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
2550 static int mlx5e_get_vf_stats(struct net_device *dev,
2551 int vf, struct ifla_vf_stats *vf_stats)
2553 struct mlx5e_priv *priv = netdev_priv(dev);
2554 struct mlx5_core_dev *mdev = priv->mdev;
2556 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
2560 static void mlx5e_add_vxlan_port(struct net_device *netdev,
2561 sa_family_t sa_family, __be16 port)
2563 struct mlx5e_priv *priv = netdev_priv(netdev);
2565 if (!mlx5e_vxlan_allowed(priv->mdev))
2568 mlx5e_vxlan_queue_work(priv, sa_family, be16_to_cpu(port), 1);
2571 static void mlx5e_del_vxlan_port(struct net_device *netdev,
2572 sa_family_t sa_family, __be16 port)
2574 struct mlx5e_priv *priv = netdev_priv(netdev);
2576 if (!mlx5e_vxlan_allowed(priv->mdev))
2579 mlx5e_vxlan_queue_work(priv, sa_family, be16_to_cpu(port), 0);
2582 static netdev_features_t mlx5e_vxlan_features_check(struct mlx5e_priv *priv,
2583 struct sk_buff *skb,
2584 netdev_features_t features)
2586 struct udphdr *udph;
2590 switch (vlan_get_protocol(skb)) {
2591 case htons(ETH_P_IP):
2592 proto = ip_hdr(skb)->protocol;
2594 case htons(ETH_P_IPV6):
2595 proto = ipv6_hdr(skb)->nexthdr;
2601 if (proto == IPPROTO_UDP) {
2602 udph = udp_hdr(skb);
2603 port = be16_to_cpu(udph->dest);
2606 /* Verify if UDP port is being offloaded by HW */
2607 if (port && mlx5e_vxlan_lookup_port(priv, port))
2611 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
2612 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2615 static netdev_features_t mlx5e_features_check(struct sk_buff *skb,
2616 struct net_device *netdev,
2617 netdev_features_t features)
2619 struct mlx5e_priv *priv = netdev_priv(netdev);
2621 features = vlan_features_check(skb, features);
2622 features = vxlan_features_check(skb, features);
2624 /* Validate if the tunneled packet is being offloaded by HW */
2625 if (skb->encapsulation &&
2626 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
2627 return mlx5e_vxlan_features_check(priv, skb, features);
2632 static void mlx5e_tx_timeout(struct net_device *dev)
2634 struct mlx5e_priv *priv = netdev_priv(dev);
2635 bool sched_work = false;
2638 netdev_err(dev, "TX timeout detected\n");
2640 for (i = 0; i < priv->params.num_channels * priv->params.num_tc; i++) {
2641 struct mlx5e_sq *sq = priv->txq_to_sq_map[i];
2643 if (!netif_tx_queue_stopped(netdev_get_tx_queue(dev, i)))
2646 set_bit(MLX5E_SQ_STATE_TX_TIMEOUT, &sq->state);
2647 netdev_err(dev, "TX timeout on queue: %d, SQ: 0x%x, CQ: 0x%x, SQ Cons: 0x%x SQ Prod: 0x%x\n",
2648 i, sq->sqn, sq->cq.mcq.cqn, sq->cc, sq->pc);
2651 if (sched_work && test_bit(MLX5E_STATE_OPENED, &priv->state))
2652 schedule_work(&priv->tx_timeout_work);
2655 static const struct net_device_ops mlx5e_netdev_ops_basic = {
2656 .ndo_open = mlx5e_open,
2657 .ndo_stop = mlx5e_close,
2658 .ndo_start_xmit = mlx5e_xmit,
2659 .ndo_setup_tc = mlx5e_ndo_setup_tc,
2660 .ndo_select_queue = mlx5e_select_queue,
2661 .ndo_get_stats64 = mlx5e_get_stats,
2662 .ndo_set_rx_mode = mlx5e_set_rx_mode,
2663 .ndo_set_mac_address = mlx5e_set_mac,
2664 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
2665 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
2666 .ndo_set_features = mlx5e_set_features,
2667 .ndo_change_mtu = mlx5e_change_mtu,
2668 .ndo_do_ioctl = mlx5e_ioctl,
2669 #ifdef CONFIG_RFS_ACCEL
2670 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
2672 .ndo_tx_timeout = mlx5e_tx_timeout,
2675 static const struct net_device_ops mlx5e_netdev_ops_sriov = {
2676 .ndo_open = mlx5e_open,
2677 .ndo_stop = mlx5e_close,
2678 .ndo_start_xmit = mlx5e_xmit,
2679 .ndo_setup_tc = mlx5e_ndo_setup_tc,
2680 .ndo_select_queue = mlx5e_select_queue,
2681 .ndo_get_stats64 = mlx5e_get_stats,
2682 .ndo_set_rx_mode = mlx5e_set_rx_mode,
2683 .ndo_set_mac_address = mlx5e_set_mac,
2684 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
2685 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
2686 .ndo_set_features = mlx5e_set_features,
2687 .ndo_change_mtu = mlx5e_change_mtu,
2688 .ndo_do_ioctl = mlx5e_ioctl,
2689 .ndo_add_vxlan_port = mlx5e_add_vxlan_port,
2690 .ndo_del_vxlan_port = mlx5e_del_vxlan_port,
2691 .ndo_features_check = mlx5e_features_check,
2692 #ifdef CONFIG_RFS_ACCEL
2693 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
2695 .ndo_set_vf_mac = mlx5e_set_vf_mac,
2696 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
2697 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
2698 .ndo_set_vf_trust = mlx5e_set_vf_trust,
2699 .ndo_get_vf_config = mlx5e_get_vf_config,
2700 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
2701 .ndo_get_vf_stats = mlx5e_get_vf_stats,
2702 .ndo_tx_timeout = mlx5e_tx_timeout,
2705 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
2707 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
2709 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
2710 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
2711 !MLX5_CAP_ETH(mdev, csum_cap) ||
2712 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
2713 !MLX5_CAP_ETH(mdev, vlan_cap) ||
2714 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
2715 MLX5_CAP_FLOWTABLE(mdev,
2716 flow_table_properties_nic_receive.max_ft_level)
2718 mlx5_core_warn(mdev,
2719 "Not creating net device, some required device capabilities are missing\n");
2722 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
2723 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
2724 if (!MLX5_CAP_GEN(mdev, cq_moderation))
2725 mlx5_core_warn(mdev, "CQ modiration is not supported\n");
2730 u16 mlx5e_get_max_inline_cap(struct mlx5_core_dev *mdev)
2732 int bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
2734 return bf_buf_size -
2735 sizeof(struct mlx5e_tx_wqe) +
2736 2 /*sizeof(mlx5e_tx_wqe.inline_hdr_start)*/;
2739 #ifdef CONFIG_MLX5_CORE_EN_DCB
2740 static void mlx5e_ets_init(struct mlx5e_priv *priv)
2744 priv->params.ets.ets_cap = mlx5_max_tc(priv->mdev) + 1;
2745 for (i = 0; i < priv->params.ets.ets_cap; i++) {
2746 priv->params.ets.tc_tx_bw[i] = MLX5E_MAX_BW_ALLOC;
2747 priv->params.ets.tc_tsa[i] = IEEE_8021QAZ_TSA_VENDOR;
2748 priv->params.ets.prio_tc[i] = i;
2751 /* tclass[prio=0]=1, tclass[prio=1]=0, tclass[prio=i]=i (for i>1) */
2752 priv->params.ets.prio_tc[0] = 1;
2753 priv->params.ets.prio_tc[1] = 0;
2757 void mlx5e_build_default_indir_rqt(struct mlx5_core_dev *mdev,
2758 u32 *indirection_rqt, int len,
2761 int node = mdev->priv.numa_node;
2762 int node_num_of_cores;
2766 node = first_online_node;
2768 node_num_of_cores = cpumask_weight(cpumask_of_node(node));
2770 if (node_num_of_cores)
2771 num_channels = min_t(int, num_channels, node_num_of_cores);
2773 for (i = 0; i < len; i++)
2774 indirection_rqt[i] = i % num_channels;
2777 static bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
2779 return MLX5_CAP_GEN(mdev, striding_rq) &&
2780 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
2781 MLX5_CAP_ETH(mdev, reg_umr_sq);
2784 static int mlx5e_get_pci_bw(struct mlx5_core_dev *mdev, u32 *pci_bw)
2786 enum pcie_link_width width;
2787 enum pci_bus_speed speed;
2790 err = pcie_get_minimum_link(mdev->pdev, &speed, &width);
2794 if (speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
2798 case PCIE_SPEED_2_5GT:
2799 *pci_bw = 2500 * width;
2801 case PCIE_SPEED_5_0GT:
2802 *pci_bw = 5000 * width;
2804 case PCIE_SPEED_8_0GT:
2805 *pci_bw = 8000 * width;
2814 static bool cqe_compress_heuristic(u32 link_speed, u32 pci_bw)
2816 return (link_speed && pci_bw &&
2817 (pci_bw < 40000) && (pci_bw < link_speed));
2820 static void mlx5e_build_netdev_priv(struct mlx5_core_dev *mdev,
2821 struct net_device *netdev,
2824 struct mlx5e_priv *priv = netdev_priv(netdev);
2828 priv->params.log_sq_size =
2829 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
2830 priv->params.rq_wq_type = mlx5e_check_fragmented_striding_rq_cap(mdev) ?
2831 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
2832 MLX5_WQ_TYPE_LINKED_LIST;
2834 /* set CQE compression */
2835 priv->params.rx_cqe_compress_admin = false;
2836 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
2837 MLX5_CAP_GEN(mdev, vport_group_manager)) {
2838 mlx5e_get_max_linkspeed(mdev, &link_speed);
2839 mlx5e_get_pci_bw(mdev, &pci_bw);
2840 mlx5_core_dbg(mdev, "Max link speed = %d, PCI BW = %d\n",
2841 link_speed, pci_bw);
2842 priv->params.rx_cqe_compress_admin =
2843 cqe_compress_heuristic(link_speed, pci_bw);
2846 priv->params.rx_cqe_compress = priv->params.rx_cqe_compress_admin;
2848 switch (priv->params.rq_wq_type) {
2849 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2850 priv->params.log_rq_size = MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE_MPW;
2851 priv->params.mpwqe_log_stride_sz =
2852 priv->params.rx_cqe_compress ?
2853 MLX5_MPWRQ_LOG_STRIDE_SIZE_CQE_COMPRESS :
2854 MLX5_MPWRQ_LOG_STRIDE_SIZE;
2855 priv->params.mpwqe_log_num_strides = MLX5_MPWRQ_LOG_WQE_SZ -
2856 priv->params.mpwqe_log_stride_sz;
2857 priv->params.lro_en = true;
2859 default: /* MLX5_WQ_TYPE_LINKED_LIST */
2860 priv->params.log_rq_size = MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
2863 mlx5_core_info(mdev,
2864 "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
2865 priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
2866 BIT(priv->params.log_rq_size),
2867 BIT(priv->params.mpwqe_log_stride_sz),
2868 priv->params.rx_cqe_compress_admin);
2870 priv->params.min_rx_wqes = mlx5_min_rx_wqes(priv->params.rq_wq_type,
2871 BIT(priv->params.log_rq_size));
2872 priv->params.rx_cq_moderation_usec =
2873 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
2874 priv->params.rx_cq_moderation_pkts =
2875 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
2876 priv->params.tx_cq_moderation_usec =
2877 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
2878 priv->params.tx_cq_moderation_pkts =
2879 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
2880 priv->params.tx_max_inline = mlx5e_get_max_inline_cap(mdev);
2881 priv->params.num_tc = 1;
2882 priv->params.rss_hfunc = ETH_RSS_HASH_XOR;
2884 netdev_rss_key_fill(priv->params.toeplitz_hash_key,
2885 sizeof(priv->params.toeplitz_hash_key));
2887 mlx5e_build_default_indir_rqt(mdev, priv->params.indirection_rqt,
2888 MLX5E_INDIR_RQT_SIZE, num_channels);
2890 priv->params.lro_wqe_sz =
2891 MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
2894 priv->netdev = netdev;
2895 priv->params.num_channels = num_channels;
2897 #ifdef CONFIG_MLX5_CORE_EN_DCB
2898 mlx5e_ets_init(priv);
2901 mutex_init(&priv->state_lock);
2903 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
2904 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
2905 INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work);
2906 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
2909 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
2911 struct mlx5e_priv *priv = netdev_priv(netdev);
2913 mlx5_query_nic_vport_mac_address(priv->mdev, 0, netdev->dev_addr);
2914 if (is_zero_ether_addr(netdev->dev_addr) &&
2915 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
2916 eth_hw_addr_random(netdev);
2917 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
2921 static void mlx5e_build_netdev(struct net_device *netdev)
2923 struct mlx5e_priv *priv = netdev_priv(netdev);
2924 struct mlx5_core_dev *mdev = priv->mdev;
2928 SET_NETDEV_DEV(netdev, &mdev->pdev->dev);
2930 if (MLX5_CAP_GEN(mdev, vport_group_manager)) {
2931 netdev->netdev_ops = &mlx5e_netdev_ops_sriov;
2932 #ifdef CONFIG_MLX5_CORE_EN_DCB
2933 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
2936 netdev->netdev_ops = &mlx5e_netdev_ops_basic;
2939 netdev->watchdog_timeo = 15 * HZ;
2941 netdev->ethtool_ops = &mlx5e_ethtool_ops;
2943 netdev->vlan_features |= NETIF_F_SG;
2944 netdev->vlan_features |= NETIF_F_IP_CSUM;
2945 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
2946 netdev->vlan_features |= NETIF_F_GRO;
2947 netdev->vlan_features |= NETIF_F_TSO;
2948 netdev->vlan_features |= NETIF_F_TSO6;
2949 netdev->vlan_features |= NETIF_F_RXCSUM;
2950 netdev->vlan_features |= NETIF_F_RXHASH;
2952 if (!!MLX5_CAP_ETH(mdev, lro_cap))
2953 netdev->vlan_features |= NETIF_F_LRO;
2955 netdev->hw_features = netdev->vlan_features;
2956 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
2957 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
2958 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2960 if (mlx5e_vxlan_allowed(mdev)) {
2961 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
2962 NETIF_F_GSO_UDP_TUNNEL_CSUM |
2963 NETIF_F_GSO_PARTIAL;
2964 netdev->hw_enc_features |= NETIF_F_IP_CSUM;
2965 netdev->hw_enc_features |= NETIF_F_IPV6_CSUM;
2966 netdev->hw_enc_features |= NETIF_F_TSO;
2967 netdev->hw_enc_features |= NETIF_F_TSO6;
2968 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL;
2969 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
2970 NETIF_F_GSO_PARTIAL;
2971 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
2974 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
2977 netdev->hw_features |= NETIF_F_RXALL;
2979 netdev->features = netdev->hw_features;
2980 if (!priv->params.lro_en)
2981 netdev->features &= ~NETIF_F_LRO;
2984 netdev->features &= ~NETIF_F_RXALL;
2986 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
2987 if (FT_CAP(flow_modify_en) &&
2988 FT_CAP(modify_root) &&
2989 FT_CAP(identified_miss_table_mode) &&
2990 FT_CAP(flow_table_modify)) {
2991 netdev->hw_features |= NETIF_F_HW_TC;
2992 #ifdef CONFIG_RFS_ACCEL
2993 netdev->hw_features |= NETIF_F_NTUPLE;
2997 netdev->features |= NETIF_F_HIGHDMA;
2999 netdev->priv_flags |= IFF_UNICAST_FLT;
3001 mlx5e_set_netdev_dev_addr(netdev);
3004 static int mlx5e_create_mkey(struct mlx5e_priv *priv, u32 pdn,
3005 struct mlx5_core_mkey *mkey)
3007 struct mlx5_core_dev *mdev = priv->mdev;
3008 struct mlx5_create_mkey_mbox_in *in;
3011 in = mlx5_vzalloc(sizeof(*in));
3015 in->seg.flags = MLX5_PERM_LOCAL_WRITE |
3016 MLX5_PERM_LOCAL_READ |
3017 MLX5_ACCESS_MODE_PA;
3018 in->seg.flags_pd = cpu_to_be32(pdn | MLX5_MKEY_LEN64);
3019 in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
3021 err = mlx5_core_create_mkey(mdev, mkey, in, sizeof(*in), NULL, NULL,
3029 static void mlx5e_create_q_counter(struct mlx5e_priv *priv)
3031 struct mlx5_core_dev *mdev = priv->mdev;
3034 err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
3036 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
3037 priv->q_counter = 0;
3041 static void mlx5e_destroy_q_counter(struct mlx5e_priv *priv)
3043 if (!priv->q_counter)
3046 mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
3049 static int mlx5e_create_umr_mkey(struct mlx5e_priv *priv)
3051 struct mlx5_core_dev *mdev = priv->mdev;
3052 struct mlx5_create_mkey_mbox_in *in;
3053 struct mlx5_mkey_seg *mkc;
3054 int inlen = sizeof(*in);
3056 mlx5e_get_max_num_channels(mdev) * MLX5_CHANNEL_MAX_NUM_MTTS;
3059 in = mlx5_vzalloc(inlen);
3064 mkc->status = MLX5_MKEY_STATUS_FREE;
3065 mkc->flags = MLX5_PERM_UMR_EN |
3066 MLX5_PERM_LOCAL_READ |
3067 MLX5_PERM_LOCAL_WRITE |
3068 MLX5_ACCESS_MODE_MTT;
3070 mkc->qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
3071 mkc->flags_pd = cpu_to_be32(priv->pdn);
3072 mkc->len = cpu_to_be64(npages << PAGE_SHIFT);
3073 mkc->xlt_oct_size = cpu_to_be32(mlx5e_get_mtt_octw(npages));
3074 mkc->log2_page_size = PAGE_SHIFT;
3076 err = mlx5_core_create_mkey(mdev, &priv->umr_mkey, in, inlen, NULL,
3084 static void *mlx5e_create_netdev(struct mlx5_core_dev *mdev)
3086 struct net_device *netdev;
3087 struct mlx5e_priv *priv;
3088 int nch = mlx5e_get_max_num_channels(mdev);
3091 if (mlx5e_check_required_hca_cap(mdev))
3094 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
3095 nch * MLX5E_MAX_NUM_TC,
3098 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
3102 mlx5e_build_netdev_priv(mdev, netdev, nch);
3103 mlx5e_build_netdev(netdev);
3105 netif_carrier_off(netdev);
3107 priv = netdev_priv(netdev);
3109 priv->wq = create_singlethread_workqueue("mlx5e");
3111 goto err_free_netdev;
3113 err = mlx5_alloc_map_uar(mdev, &priv->cq_uar, false);
3115 mlx5_core_err(mdev, "alloc_map uar failed, %d\n", err);
3116 goto err_destroy_wq;
3119 err = mlx5_core_alloc_pd(mdev, &priv->pdn);
3121 mlx5_core_err(mdev, "alloc pd failed, %d\n", err);
3122 goto err_unmap_free_uar;
3125 err = mlx5_core_alloc_transport_domain(mdev, &priv->tdn);
3127 mlx5_core_err(mdev, "alloc td failed, %d\n", err);
3128 goto err_dealloc_pd;
3131 err = mlx5e_create_mkey(priv, priv->pdn, &priv->mkey);
3133 mlx5_core_err(mdev, "create mkey failed, %d\n", err);
3134 goto err_dealloc_transport_domain;
3137 err = mlx5e_create_umr_mkey(priv);
3139 mlx5_core_err(mdev, "create umr mkey failed, %d\n", err);
3140 goto err_destroy_mkey;
3143 err = mlx5e_create_tises(priv);
3145 mlx5_core_warn(mdev, "create tises failed, %d\n", err);
3146 goto err_destroy_umr_mkey;
3149 err = mlx5e_open_drop_rq(priv);
3151 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
3152 goto err_destroy_tises;
3155 err = mlx5e_create_rqts(priv);
3157 mlx5_core_warn(mdev, "create rqts failed, %d\n", err);
3158 goto err_close_drop_rq;
3161 err = mlx5e_create_tirs(priv);
3163 mlx5_core_warn(mdev, "create tirs failed, %d\n", err);
3164 goto err_destroy_rqts;
3167 err = mlx5e_create_flow_steering(priv);
3169 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
3170 goto err_destroy_tirs;
3173 mlx5e_create_q_counter(priv);
3175 mlx5e_init_l2_addr(priv);
3177 mlx5e_vxlan_init(priv);
3179 err = mlx5e_tc_init(priv);
3181 goto err_dealloc_q_counters;
3183 #ifdef CONFIG_MLX5_CORE_EN_DCB
3184 mlx5e_dcbnl_ieee_setets_core(priv, &priv->params.ets);
3187 err = register_netdev(netdev);
3189 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
3190 goto err_tc_cleanup;
3193 if (mlx5e_vxlan_allowed(mdev)) {
3195 vxlan_get_rx_port(netdev);
3199 mlx5e_enable_async_events(priv);
3200 queue_work(priv->wq, &priv->set_rx_mode_work);
3205 mlx5e_tc_cleanup(priv);
3207 err_dealloc_q_counters:
3208 mlx5e_destroy_q_counter(priv);
3209 mlx5e_destroy_flow_steering(priv);
3212 mlx5e_destroy_tirs(priv);
3215 mlx5e_destroy_rqts(priv);
3218 mlx5e_close_drop_rq(priv);
3221 mlx5e_destroy_tises(priv);
3223 err_destroy_umr_mkey:
3224 mlx5_core_destroy_mkey(mdev, &priv->umr_mkey);
3227 mlx5_core_destroy_mkey(mdev, &priv->mkey);
3229 err_dealloc_transport_domain:
3230 mlx5_core_dealloc_transport_domain(mdev, priv->tdn);
3233 mlx5_core_dealloc_pd(mdev, priv->pdn);
3236 mlx5_unmap_free_uar(mdev, &priv->cq_uar);
3239 destroy_workqueue(priv->wq);
3242 free_netdev(netdev);
3247 static void mlx5e_destroy_netdev(struct mlx5_core_dev *mdev, void *vpriv)
3249 struct mlx5e_priv *priv = vpriv;
3250 struct net_device *netdev = priv->netdev;
3252 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
3254 queue_work(priv->wq, &priv->set_rx_mode_work);
3255 mlx5e_disable_async_events(priv);
3256 flush_workqueue(priv->wq);
3257 if (test_bit(MLX5_INTERFACE_STATE_SHUTDOWN, &mdev->intf_state)) {
3258 netif_device_detach(netdev);
3259 mlx5e_close(netdev);
3261 unregister_netdev(netdev);
3264 mlx5e_tc_cleanup(priv);
3265 mlx5e_vxlan_cleanup(priv);
3266 mlx5e_destroy_q_counter(priv);
3267 mlx5e_destroy_flow_steering(priv);
3268 mlx5e_destroy_tirs(priv);
3269 mlx5e_destroy_rqts(priv);
3270 mlx5e_close_drop_rq(priv);
3271 mlx5e_destroy_tises(priv);
3272 mlx5_core_destroy_mkey(priv->mdev, &priv->umr_mkey);
3273 mlx5_core_destroy_mkey(priv->mdev, &priv->mkey);
3274 mlx5_core_dealloc_transport_domain(priv->mdev, priv->tdn);
3275 mlx5_core_dealloc_pd(priv->mdev, priv->pdn);
3276 mlx5_unmap_free_uar(priv->mdev, &priv->cq_uar);
3277 cancel_delayed_work_sync(&priv->update_stats_work);
3278 destroy_workqueue(priv->wq);
3280 if (!test_bit(MLX5_INTERFACE_STATE_SHUTDOWN, &mdev->intf_state))
3281 free_netdev(netdev);
3284 static void *mlx5e_get_netdev(void *vpriv)
3286 struct mlx5e_priv *priv = vpriv;
3288 return priv->netdev;
3291 static struct mlx5_interface mlx5e_interface = {
3292 .add = mlx5e_create_netdev,
3293 .remove = mlx5e_destroy_netdev,
3294 .event = mlx5e_async_event,
3295 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
3296 .get_dev = mlx5e_get_netdev,
3299 void mlx5e_init(void)
3301 mlx5_register_interface(&mlx5e_interface);
3304 void mlx5e_cleanup(void)
3306 mlx5_unregister_interface(&mlx5e_interface);