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;
47 struct mlx5e_sq_param {
48 u32 sqc[MLX5_ST_SZ_DW(sqc)];
49 struct mlx5_wq_param wq;
54 struct mlx5e_cq_param {
55 u32 cqc[MLX5_ST_SZ_DW(cqc)];
56 struct mlx5_wq_param wq;
61 struct mlx5e_channel_param {
62 struct mlx5e_rq_param rq;
63 struct mlx5e_sq_param sq;
64 struct mlx5e_sq_param icosq;
65 struct mlx5e_cq_param rx_cq;
66 struct mlx5e_cq_param tx_cq;
67 struct mlx5e_cq_param icosq_cq;
70 static void mlx5e_update_carrier(struct mlx5e_priv *priv)
72 struct mlx5_core_dev *mdev = priv->mdev;
75 port_state = mlx5_query_vport_state(mdev,
76 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT, 0);
78 if (port_state == VPORT_STATE_UP)
79 netif_carrier_on(priv->netdev);
81 netif_carrier_off(priv->netdev);
84 static void mlx5e_update_carrier_work(struct work_struct *work)
86 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
89 mutex_lock(&priv->state_lock);
90 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
91 mlx5e_update_carrier(priv);
92 mutex_unlock(&priv->state_lock);
95 static void mlx5e_update_sw_counters(struct mlx5e_priv *priv)
97 struct mlx5e_sw_stats *s = &priv->stats.sw;
98 struct mlx5e_rq_stats *rq_stats;
99 struct mlx5e_sq_stats *sq_stats;
100 u64 tx_offload_none = 0;
103 memset(s, 0, sizeof(*s));
104 for (i = 0; i < priv->params.num_channels; i++) {
105 rq_stats = &priv->channel[i]->rq.stats;
107 s->rx_packets += rq_stats->packets;
108 s->rx_bytes += rq_stats->bytes;
109 s->lro_packets += rq_stats->lro_packets;
110 s->lro_bytes += rq_stats->lro_bytes;
111 s->rx_csum_none += rq_stats->csum_none;
112 s->rx_csum_sw += rq_stats->csum_sw;
113 s->rx_csum_inner += rq_stats->csum_inner;
114 s->rx_wqe_err += rq_stats->wqe_err;
115 s->rx_mpwqe_filler += rq_stats->mpwqe_filler;
116 s->rx_mpwqe_frag += rq_stats->mpwqe_frag;
117 s->rx_buff_alloc_err += rq_stats->buff_alloc_err;
118 s->rx_cqe_compress_blks += rq_stats->cqe_compress_blks;
119 s->rx_cqe_compress_pkts += rq_stats->cqe_compress_pkts;
121 for (j = 0; j < priv->params.num_tc; j++) {
122 sq_stats = &priv->channel[i]->sq[j].stats;
124 s->tx_packets += sq_stats->packets;
125 s->tx_bytes += sq_stats->bytes;
126 s->tso_packets += sq_stats->tso_packets;
127 s->tso_bytes += sq_stats->tso_bytes;
128 s->tso_inner_packets += sq_stats->tso_inner_packets;
129 s->tso_inner_bytes += sq_stats->tso_inner_bytes;
130 s->tx_queue_stopped += sq_stats->stopped;
131 s->tx_queue_wake += sq_stats->wake;
132 s->tx_queue_dropped += sq_stats->dropped;
133 s->tx_csum_inner += sq_stats->csum_offload_inner;
134 tx_offload_none += sq_stats->csum_offload_none;
138 /* Update calculated offload counters */
139 s->tx_csum_offload = s->tx_packets - tx_offload_none - s->tx_csum_inner;
140 s->rx_csum_good = s->rx_packets - s->rx_csum_none -
143 s->link_down_events = MLX5_GET(ppcnt_reg,
144 priv->stats.pport.phy_counters,
145 counter_set.phys_layer_cntrs.link_down_events);
148 static void mlx5e_update_vport_counters(struct mlx5e_priv *priv)
150 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
151 u32 *out = (u32 *)priv->stats.vport.query_vport_out;
152 u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)];
153 struct mlx5_core_dev *mdev = priv->mdev;
155 memset(in, 0, sizeof(in));
157 MLX5_SET(query_vport_counter_in, in, opcode,
158 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
159 MLX5_SET(query_vport_counter_in, in, op_mod, 0);
160 MLX5_SET(query_vport_counter_in, in, other_vport, 0);
162 memset(out, 0, outlen);
164 mlx5_cmd_exec(mdev, in, sizeof(in), out, outlen);
167 static void mlx5e_update_pport_counters(struct mlx5e_priv *priv)
169 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
170 struct mlx5_core_dev *mdev = priv->mdev;
171 int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
176 in = mlx5_vzalloc(sz);
180 MLX5_SET(ppcnt_reg, in, local_port, 1);
182 out = pstats->IEEE_802_3_counters;
183 MLX5_SET(ppcnt_reg, in, grp, MLX5_IEEE_802_3_COUNTERS_GROUP);
184 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
186 out = pstats->RFC_2863_counters;
187 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2863_COUNTERS_GROUP);
188 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
190 out = pstats->RFC_2819_counters;
191 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2819_COUNTERS_GROUP);
192 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
194 out = pstats->phy_counters;
195 MLX5_SET(ppcnt_reg, in, grp, MLX5_PHYSICAL_LAYER_COUNTERS_GROUP);
196 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
198 MLX5_SET(ppcnt_reg, in, grp, MLX5_PER_PRIORITY_COUNTERS_GROUP);
199 for (prio = 0; prio < NUM_PPORT_PRIO; prio++) {
200 out = pstats->per_prio_counters[prio];
201 MLX5_SET(ppcnt_reg, in, prio_tc, prio);
202 mlx5_core_access_reg(mdev, in, sz, out, sz,
203 MLX5_REG_PPCNT, 0, 0);
210 static void mlx5e_update_q_counter(struct mlx5e_priv *priv)
212 struct mlx5e_qcounter_stats *qcnt = &priv->stats.qcnt;
214 if (!priv->q_counter)
217 mlx5_core_query_out_of_buffer(priv->mdev, priv->q_counter,
218 &qcnt->rx_out_of_buffer);
221 void mlx5e_update_stats(struct mlx5e_priv *priv)
223 mlx5e_update_q_counter(priv);
224 mlx5e_update_vport_counters(priv);
225 mlx5e_update_pport_counters(priv);
226 mlx5e_update_sw_counters(priv);
229 static void mlx5e_update_stats_work(struct work_struct *work)
231 struct delayed_work *dwork = to_delayed_work(work);
232 struct mlx5e_priv *priv = container_of(dwork, struct mlx5e_priv,
234 mutex_lock(&priv->state_lock);
235 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
236 mlx5e_update_stats(priv);
237 queue_delayed_work(priv->wq, dwork,
238 msecs_to_jiffies(MLX5E_UPDATE_STATS_INTERVAL));
240 mutex_unlock(&priv->state_lock);
243 static void mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
244 enum mlx5_dev_event event, unsigned long param)
246 struct mlx5e_priv *priv = vpriv;
248 if (!test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state))
252 case MLX5_DEV_EVENT_PORT_UP:
253 case MLX5_DEV_EVENT_PORT_DOWN:
254 queue_work(priv->wq, &priv->update_carrier_work);
262 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
264 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state);
267 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
269 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state);
270 synchronize_irq(mlx5_get_msix_vec(priv->mdev, MLX5_EQ_VEC_ASYNC));
273 #define MLX5E_HW2SW_MTU(hwmtu) (hwmtu - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
274 #define MLX5E_SW2HW_MTU(swmtu) (swmtu + (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
276 static int mlx5e_create_rq(struct mlx5e_channel *c,
277 struct mlx5e_rq_param *param,
280 struct mlx5e_priv *priv = c->priv;
281 struct mlx5_core_dev *mdev = priv->mdev;
282 void *rqc = param->rqc;
283 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
289 param->wq.db_numa_node = cpu_to_node(c->cpu);
291 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
296 rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
298 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
300 switch (priv->params.rq_wq_type) {
301 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
302 rq->wqe_info = kzalloc_node(wq_sz * sizeof(*rq->wqe_info),
303 GFP_KERNEL, cpu_to_node(c->cpu));
306 goto err_rq_wq_destroy;
308 rq->handle_rx_cqe = mlx5e_handle_rx_cqe_mpwrq;
309 rq->alloc_wqe = mlx5e_alloc_rx_mpwqe;
311 rq->mpwqe_stride_sz = BIT(priv->params.mpwqe_log_stride_sz);
312 rq->mpwqe_num_strides = BIT(priv->params.mpwqe_log_num_strides);
313 rq->wqe_sz = rq->mpwqe_stride_sz * rq->mpwqe_num_strides;
314 byte_count = rq->wqe_sz;
316 default: /* MLX5_WQ_TYPE_LINKED_LIST */
317 rq->skb = kzalloc_node(wq_sz * sizeof(*rq->skb), GFP_KERNEL,
318 cpu_to_node(c->cpu));
321 goto err_rq_wq_destroy;
323 rq->handle_rx_cqe = mlx5e_handle_rx_cqe;
324 rq->alloc_wqe = mlx5e_alloc_rx_wqe;
326 rq->wqe_sz = (priv->params.lro_en) ?
327 priv->params.lro_wqe_sz :
328 MLX5E_SW2HW_MTU(priv->netdev->mtu);
329 rq->wqe_sz = SKB_DATA_ALIGN(rq->wqe_sz);
330 byte_count = rq->wqe_sz;
331 byte_count |= MLX5_HW_START_PADDING;
334 for (i = 0; i < wq_sz; i++) {
335 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
337 wqe->data.byte_count = cpu_to_be32(byte_count);
340 rq->wq_type = priv->params.rq_wq_type;
342 rq->netdev = c->netdev;
343 rq->tstamp = &priv->tstamp;
347 rq->mkey_be = c->mkey_be;
348 rq->umr_mkey_be = cpu_to_be32(c->priv->umr_mkey.key);
353 mlx5_wq_destroy(&rq->wq_ctrl);
358 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
360 switch (rq->wq_type) {
361 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
364 default: /* MLX5_WQ_TYPE_LINKED_LIST */
368 mlx5_wq_destroy(&rq->wq_ctrl);
371 static int mlx5e_enable_rq(struct mlx5e_rq *rq, struct mlx5e_rq_param *param)
373 struct mlx5e_priv *priv = rq->priv;
374 struct mlx5_core_dev *mdev = priv->mdev;
382 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
383 sizeof(u64) * rq->wq_ctrl.buf.npages;
384 in = mlx5_vzalloc(inlen);
388 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
389 wq = MLX5_ADDR_OF(rqc, rqc, wq);
391 memcpy(rqc, param->rqc, sizeof(param->rqc));
393 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
394 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
395 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
396 MLX5_SET(rqc, rqc, vsd, priv->params.vlan_strip_disable);
397 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
398 MLX5_ADAPTER_PAGE_SHIFT);
399 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
401 mlx5_fill_page_array(&rq->wq_ctrl.buf,
402 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
404 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
411 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
414 struct mlx5e_channel *c = rq->channel;
415 struct mlx5e_priv *priv = c->priv;
416 struct mlx5_core_dev *mdev = priv->mdev;
423 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
424 in = mlx5_vzalloc(inlen);
428 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
430 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
431 MLX5_SET(rqc, rqc, state, next_state);
433 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
440 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
442 struct mlx5e_channel *c = rq->channel;
443 struct mlx5e_priv *priv = c->priv;
444 struct mlx5_core_dev *mdev = priv->mdev;
451 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
452 in = mlx5_vzalloc(inlen);
456 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
458 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
459 MLX5_SET64(modify_rq_in, in, modify_bitmask, MLX5_RQ_BITMASK_VSD);
460 MLX5_SET(rqc, rqc, vsd, vsd);
461 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
463 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
470 static void mlx5e_disable_rq(struct mlx5e_rq *rq)
472 mlx5_core_destroy_rq(rq->priv->mdev, rq->rqn);
475 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq)
477 unsigned long exp_time = jiffies + msecs_to_jiffies(20000);
478 struct mlx5e_channel *c = rq->channel;
479 struct mlx5e_priv *priv = c->priv;
480 struct mlx5_wq_ll *wq = &rq->wq;
482 while (time_before(jiffies, exp_time)) {
483 if (wq->cur_sz >= priv->params.min_rx_wqes)
492 static int mlx5e_open_rq(struct mlx5e_channel *c,
493 struct mlx5e_rq_param *param,
496 struct mlx5e_sq *sq = &c->icosq;
497 u16 pi = sq->pc & sq->wq.sz_m1;
500 err = mlx5e_create_rq(c, param, rq);
504 err = mlx5e_enable_rq(rq, param);
508 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
512 set_bit(MLX5E_RQ_STATE_POST_WQES_ENABLE, &rq->state);
514 sq->ico_wqe_info[pi].opcode = MLX5_OPCODE_NOP;
515 sq->ico_wqe_info[pi].num_wqebbs = 1;
516 mlx5e_send_nop(sq, true); /* trigger mlx5e_post_rx_wqes() */
521 mlx5e_disable_rq(rq);
523 mlx5e_destroy_rq(rq);
528 static void mlx5e_close_rq(struct mlx5e_rq *rq)
530 clear_bit(MLX5E_RQ_STATE_POST_WQES_ENABLE, &rq->state);
531 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
533 mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RDY, MLX5_RQC_STATE_ERR);
534 while (!mlx5_wq_ll_is_empty(&rq->wq))
537 /* avoid destroying rq before mlx5e_poll_rx_cq() is done with it */
538 napi_synchronize(&rq->channel->napi);
540 mlx5e_disable_rq(rq);
541 mlx5e_destroy_rq(rq);
544 static void mlx5e_free_sq_db(struct mlx5e_sq *sq)
551 static int mlx5e_alloc_sq_db(struct mlx5e_sq *sq, int numa)
553 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
554 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
556 sq->skb = kzalloc_node(wq_sz * sizeof(*sq->skb), GFP_KERNEL, numa);
557 sq->dma_fifo = kzalloc_node(df_sz * sizeof(*sq->dma_fifo), GFP_KERNEL,
559 sq->wqe_info = kzalloc_node(wq_sz * sizeof(*sq->wqe_info), GFP_KERNEL,
562 if (!sq->skb || !sq->dma_fifo || !sq->wqe_info) {
563 mlx5e_free_sq_db(sq);
567 sq->dma_fifo_mask = df_sz - 1;
572 static int mlx5e_create_sq(struct mlx5e_channel *c,
574 struct mlx5e_sq_param *param,
577 struct mlx5e_priv *priv = c->priv;
578 struct mlx5_core_dev *mdev = priv->mdev;
580 void *sqc = param->sqc;
581 void *sqc_wq = MLX5_ADDR_OF(sqc, sqc, wq);
584 err = mlx5_alloc_map_uar(mdev, &sq->uar, true);
588 param->wq.db_numa_node = cpu_to_node(c->cpu);
590 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq,
593 goto err_unmap_free_uar;
595 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
596 if (sq->uar.bf_map) {
597 set_bit(MLX5E_SQ_STATE_BF_ENABLE, &sq->state);
598 sq->uar_map = sq->uar.bf_map;
600 sq->uar_map = sq->uar.map;
602 sq->bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
603 sq->max_inline = param->max_inline;
605 err = mlx5e_alloc_sq_db(sq, cpu_to_node(c->cpu));
607 goto err_sq_wq_destroy;
610 u8 wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
612 sq->ico_wqe_info = kzalloc_node(sizeof(*sq->ico_wqe_info) *
615 cpu_to_node(c->cpu));
616 if (!sq->ico_wqe_info) {
623 txq_ix = c->ix + tc * priv->params.num_channels;
624 sq->txq = netdev_get_tx_queue(priv->netdev, txq_ix);
625 priv->txq_to_sq_map[txq_ix] = sq;
629 sq->tstamp = &priv->tstamp;
630 sq->mkey_be = c->mkey_be;
633 sq->edge = (sq->wq.sz_m1 + 1) - MLX5_SEND_WQE_MAX_WQEBBS;
634 sq->bf_budget = MLX5E_SQ_BF_BUDGET;
639 mlx5e_free_sq_db(sq);
642 mlx5_wq_destroy(&sq->wq_ctrl);
645 mlx5_unmap_free_uar(mdev, &sq->uar);
650 static void mlx5e_destroy_sq(struct mlx5e_sq *sq)
652 struct mlx5e_channel *c = sq->channel;
653 struct mlx5e_priv *priv = c->priv;
655 kfree(sq->ico_wqe_info);
656 mlx5e_free_sq_db(sq);
657 mlx5_wq_destroy(&sq->wq_ctrl);
658 mlx5_unmap_free_uar(priv->mdev, &sq->uar);
661 static int mlx5e_enable_sq(struct mlx5e_sq *sq, struct mlx5e_sq_param *param)
663 struct mlx5e_channel *c = sq->channel;
664 struct mlx5e_priv *priv = c->priv;
665 struct mlx5_core_dev *mdev = priv->mdev;
673 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
674 sizeof(u64) * sq->wq_ctrl.buf.npages;
675 in = mlx5_vzalloc(inlen);
679 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
680 wq = MLX5_ADDR_OF(sqc, sqc, wq);
682 memcpy(sqc, param->sqc, sizeof(param->sqc));
684 MLX5_SET(sqc, sqc, tis_num_0, param->icosq ? 0 : priv->tisn[sq->tc]);
685 MLX5_SET(sqc, sqc, cqn, sq->cq.mcq.cqn);
686 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
687 MLX5_SET(sqc, sqc, tis_lst_sz, param->icosq ? 0 : 1);
688 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
690 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
691 MLX5_SET(wq, wq, uar_page, sq->uar.index);
692 MLX5_SET(wq, wq, log_wq_pg_sz, sq->wq_ctrl.buf.page_shift -
693 MLX5_ADAPTER_PAGE_SHIFT);
694 MLX5_SET64(wq, wq, dbr_addr, sq->wq_ctrl.db.dma);
696 mlx5_fill_page_array(&sq->wq_ctrl.buf,
697 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
699 err = mlx5_core_create_sq(mdev, in, inlen, &sq->sqn);
706 static int mlx5e_modify_sq(struct mlx5e_sq *sq, int curr_state,
707 int next_state, bool update_rl, int rl_index)
709 struct mlx5e_channel *c = sq->channel;
710 struct mlx5e_priv *priv = c->priv;
711 struct mlx5_core_dev *mdev = priv->mdev;
718 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
719 in = mlx5_vzalloc(inlen);
723 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
725 MLX5_SET(modify_sq_in, in, sq_state, curr_state);
726 MLX5_SET(sqc, sqc, state, next_state);
727 if (update_rl && next_state == MLX5_SQC_STATE_RDY) {
728 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
729 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
732 err = mlx5_core_modify_sq(mdev, sq->sqn, in, inlen);
739 static void mlx5e_disable_sq(struct mlx5e_sq *sq)
741 struct mlx5e_channel *c = sq->channel;
742 struct mlx5e_priv *priv = c->priv;
743 struct mlx5_core_dev *mdev = priv->mdev;
745 mlx5_core_destroy_sq(mdev, sq->sqn);
747 mlx5_rl_remove_rate(mdev, sq->rate_limit);
750 static int mlx5e_open_sq(struct mlx5e_channel *c,
752 struct mlx5e_sq_param *param,
757 err = mlx5e_create_sq(c, tc, param, sq);
761 err = mlx5e_enable_sq(sq, param);
765 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST, MLX5_SQC_STATE_RDY,
771 set_bit(MLX5E_SQ_STATE_WAKE_TXQ_ENABLE, &sq->state);
772 netdev_tx_reset_queue(sq->txq);
773 netif_tx_start_queue(sq->txq);
779 mlx5e_disable_sq(sq);
781 mlx5e_destroy_sq(sq);
786 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
788 __netif_tx_lock_bh(txq);
789 netif_tx_stop_queue(txq);
790 __netif_tx_unlock_bh(txq);
793 static void mlx5e_close_sq(struct mlx5e_sq *sq)
796 clear_bit(MLX5E_SQ_STATE_WAKE_TXQ_ENABLE, &sq->state);
797 /* prevent netif_tx_wake_queue */
798 napi_synchronize(&sq->channel->napi);
799 netif_tx_disable_queue(sq->txq);
801 /* ensure hw is notified of all pending wqes */
802 if (mlx5e_sq_has_room_for(sq, 1))
803 mlx5e_send_nop(sq, true);
805 mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY, MLX5_SQC_STATE_ERR,
809 while (sq->cc != sq->pc) /* wait till sq is empty */
812 /* avoid destroying sq before mlx5e_poll_tx_cq() is done with it */
813 napi_synchronize(&sq->channel->napi);
815 mlx5e_disable_sq(sq);
816 mlx5e_destroy_sq(sq);
819 static int mlx5e_create_cq(struct mlx5e_channel *c,
820 struct mlx5e_cq_param *param,
823 struct mlx5e_priv *priv = c->priv;
824 struct mlx5_core_dev *mdev = priv->mdev;
825 struct mlx5_core_cq *mcq = &cq->mcq;
831 param->wq.buf_numa_node = cpu_to_node(c->cpu);
832 param->wq.db_numa_node = cpu_to_node(c->cpu);
833 param->eq_ix = c->ix;
835 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
840 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
845 mcq->set_ci_db = cq->wq_ctrl.db.db;
846 mcq->arm_db = cq->wq_ctrl.db.db + 1;
849 mcq->vector = param->eq_ix;
850 mcq->comp = mlx5e_completion_event;
851 mcq->event = mlx5e_cq_error_event;
853 mcq->uar = &priv->cq_uar;
855 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
856 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
867 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
869 mlx5_wq_destroy(&cq->wq_ctrl);
872 static int mlx5e_enable_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
874 struct mlx5e_priv *priv = cq->priv;
875 struct mlx5_core_dev *mdev = priv->mdev;
876 struct mlx5_core_cq *mcq = &cq->mcq;
881 unsigned int irqn_not_used;
885 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
886 sizeof(u64) * cq->wq_ctrl.buf.npages;
887 in = mlx5_vzalloc(inlen);
891 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
893 memcpy(cqc, param->cqc, sizeof(param->cqc));
895 mlx5_fill_page_array(&cq->wq_ctrl.buf,
896 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
898 mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
900 MLX5_SET(cqc, cqc, cq_period_mode, param->cq_period_mode);
901 MLX5_SET(cqc, cqc, c_eqn, eqn);
902 MLX5_SET(cqc, cqc, uar_page, mcq->uar->index);
903 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
904 MLX5_ADAPTER_PAGE_SHIFT);
905 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
907 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
919 static void mlx5e_disable_cq(struct mlx5e_cq *cq)
921 struct mlx5e_priv *priv = cq->priv;
922 struct mlx5_core_dev *mdev = priv->mdev;
924 mlx5_core_destroy_cq(mdev, &cq->mcq);
927 static int mlx5e_open_cq(struct mlx5e_channel *c,
928 struct mlx5e_cq_param *param,
930 struct mlx5e_cq_moder moderation)
933 struct mlx5e_priv *priv = c->priv;
934 struct mlx5_core_dev *mdev = priv->mdev;
936 err = mlx5e_create_cq(c, param, cq);
940 err = mlx5e_enable_cq(cq, param);
944 if (MLX5_CAP_GEN(mdev, cq_moderation))
945 mlx5_core_modify_cq_moderation(mdev, &cq->mcq,
951 mlx5e_destroy_cq(cq);
956 static void mlx5e_close_cq(struct mlx5e_cq *cq)
958 mlx5e_disable_cq(cq);
959 mlx5e_destroy_cq(cq);
962 static int mlx5e_get_cpu(struct mlx5e_priv *priv, int ix)
964 return cpumask_first(priv->mdev->priv.irq_info[ix].mask);
967 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
968 struct mlx5e_channel_param *cparam)
970 struct mlx5e_priv *priv = c->priv;
974 for (tc = 0; tc < c->num_tc; tc++) {
975 err = mlx5e_open_cq(c, &cparam->tx_cq, &c->sq[tc].cq,
976 priv->params.tx_cq_moderation);
978 goto err_close_tx_cqs;
984 for (tc--; tc >= 0; tc--)
985 mlx5e_close_cq(&c->sq[tc].cq);
990 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
994 for (tc = 0; tc < c->num_tc; tc++)
995 mlx5e_close_cq(&c->sq[tc].cq);
998 static int mlx5e_open_sqs(struct mlx5e_channel *c,
999 struct mlx5e_channel_param *cparam)
1004 for (tc = 0; tc < c->num_tc; tc++) {
1005 err = mlx5e_open_sq(c, tc, &cparam->sq, &c->sq[tc]);
1013 for (tc--; tc >= 0; tc--)
1014 mlx5e_close_sq(&c->sq[tc]);
1019 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1023 for (tc = 0; tc < c->num_tc; tc++)
1024 mlx5e_close_sq(&c->sq[tc]);
1027 static void mlx5e_build_channeltc_to_txq_map(struct mlx5e_priv *priv, int ix)
1031 for (i = 0; i < MLX5E_MAX_NUM_TC; i++)
1032 priv->channeltc_to_txq_map[ix][i] =
1033 ix + i * priv->params.num_channels;
1036 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1037 struct mlx5e_sq *sq, u32 rate)
1039 struct mlx5e_priv *priv = netdev_priv(dev);
1040 struct mlx5_core_dev *mdev = priv->mdev;
1044 if (rate == sq->rate_limit)
1049 /* remove current rl index to free space to next ones */
1050 mlx5_rl_remove_rate(mdev, sq->rate_limit);
1055 err = mlx5_rl_add_rate(mdev, rate, &rl_index);
1057 netdev_err(dev, "Failed configuring rate %u: %d\n",
1063 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY,
1064 MLX5_SQC_STATE_RDY, true, rl_index);
1066 netdev_err(dev, "Failed configuring rate %u: %d\n",
1068 /* remove the rate from the table */
1070 mlx5_rl_remove_rate(mdev, rate);
1074 sq->rate_limit = rate;
1078 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1080 struct mlx5e_priv *priv = netdev_priv(dev);
1081 struct mlx5_core_dev *mdev = priv->mdev;
1082 struct mlx5e_sq *sq = priv->txq_to_sq_map[index];
1085 if (!mlx5_rl_is_supported(mdev)) {
1086 netdev_err(dev, "Rate limiting is not supported on this device\n");
1090 /* rate is given in Mb/sec, HW config is in Kb/sec */
1093 /* Check whether rate in valid range, 0 is always valid */
1094 if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1095 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1099 mutex_lock(&priv->state_lock);
1100 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1101 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1103 priv->tx_rates[index] = rate;
1104 mutex_unlock(&priv->state_lock);
1109 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1110 struct mlx5e_channel_param *cparam,
1111 struct mlx5e_channel **cp)
1113 struct mlx5e_cq_moder icosq_cq_moder = {0, 0};
1114 struct net_device *netdev = priv->netdev;
1115 int cpu = mlx5e_get_cpu(priv, ix);
1116 struct mlx5e_channel *c;
1117 struct mlx5e_sq *sq;
1121 c = kzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1128 c->pdev = &priv->mdev->pdev->dev;
1129 c->netdev = priv->netdev;
1130 c->mkey_be = cpu_to_be32(priv->mkey.key);
1131 c->num_tc = priv->params.num_tc;
1133 mlx5e_build_channeltc_to_txq_map(priv, ix);
1135 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1137 err = mlx5e_open_cq(c, &cparam->icosq_cq, &c->icosq.cq, icosq_cq_moder);
1141 err = mlx5e_open_tx_cqs(c, cparam);
1143 goto err_close_icosq_cq;
1145 err = mlx5e_open_cq(c, &cparam->rx_cq, &c->rq.cq,
1146 priv->params.rx_cq_moderation);
1148 goto err_close_tx_cqs;
1150 napi_enable(&c->napi);
1152 err = mlx5e_open_sq(c, 0, &cparam->icosq, &c->icosq);
1154 goto err_disable_napi;
1156 err = mlx5e_open_sqs(c, cparam);
1158 goto err_close_icosq;
1160 for (i = 0; i < priv->params.num_tc; i++) {
1161 u32 txq_ix = priv->channeltc_to_txq_map[ix][i];
1163 if (priv->tx_rates[txq_ix]) {
1164 sq = priv->txq_to_sq_map[txq_ix];
1165 mlx5e_set_sq_maxrate(priv->netdev, sq,
1166 priv->tx_rates[txq_ix]);
1170 err = mlx5e_open_rq(c, &cparam->rq, &c->rq);
1174 netif_set_xps_queue(netdev, get_cpu_mask(c->cpu), ix);
1183 mlx5e_close_sq(&c->icosq);
1186 napi_disable(&c->napi);
1187 mlx5e_close_cq(&c->rq.cq);
1190 mlx5e_close_tx_cqs(c);
1193 mlx5e_close_cq(&c->icosq.cq);
1196 netif_napi_del(&c->napi);
1197 napi_hash_del(&c->napi);
1203 static void mlx5e_close_channel(struct mlx5e_channel *c)
1205 mlx5e_close_rq(&c->rq);
1207 mlx5e_close_sq(&c->icosq);
1208 napi_disable(&c->napi);
1209 mlx5e_close_cq(&c->rq.cq);
1210 mlx5e_close_tx_cqs(c);
1211 mlx5e_close_cq(&c->icosq.cq);
1212 netif_napi_del(&c->napi);
1214 napi_hash_del(&c->napi);
1220 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
1221 struct mlx5e_rq_param *param)
1223 void *rqc = param->rqc;
1224 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1226 switch (priv->params.rq_wq_type) {
1227 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1228 MLX5_SET(wq, wq, log_wqe_num_of_strides,
1229 priv->params.mpwqe_log_num_strides - 9);
1230 MLX5_SET(wq, wq, log_wqe_stride_size,
1231 priv->params.mpwqe_log_stride_sz - 6);
1232 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ);
1234 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1235 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1238 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1239 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1240 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_rq_size);
1241 MLX5_SET(wq, wq, pd, priv->pdn);
1242 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
1244 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1245 param->wq.linear = 1;
1248 static void mlx5e_build_drop_rq_param(struct mlx5e_rq_param *param)
1250 void *rqc = param->rqc;
1251 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1253 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1254 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1257 static void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
1258 struct mlx5e_sq_param *param)
1260 void *sqc = param->sqc;
1261 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1263 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1264 MLX5_SET(wq, wq, pd, priv->pdn);
1266 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1269 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
1270 struct mlx5e_sq_param *param)
1272 void *sqc = param->sqc;
1273 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1275 mlx5e_build_sq_param_common(priv, param);
1276 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_sq_size);
1278 param->max_inline = priv->params.tx_max_inline;
1281 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1282 struct mlx5e_cq_param *param)
1284 void *cqc = param->cqc;
1286 MLX5_SET(cqc, cqc, uar_page, priv->cq_uar.index);
1289 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
1290 struct mlx5e_cq_param *param)
1292 void *cqc = param->cqc;
1295 switch (priv->params.rq_wq_type) {
1296 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1297 log_cq_size = priv->params.log_rq_size +
1298 priv->params.mpwqe_log_num_strides;
1300 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1301 log_cq_size = priv->params.log_rq_size;
1304 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
1305 if (priv->params.rx_cqe_compress) {
1306 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
1307 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
1310 mlx5e_build_common_cq_param(priv, param);
1312 param->cq_period_mode = priv->params.rx_cq_period_mode;
1315 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
1316 struct mlx5e_cq_param *param)
1318 void *cqc = param->cqc;
1320 MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_sq_size);
1322 mlx5e_build_common_cq_param(priv, param);
1324 param->cq_period_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1327 static void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
1328 struct mlx5e_cq_param *param,
1331 void *cqc = param->cqc;
1333 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
1335 mlx5e_build_common_cq_param(priv, param);
1337 param->cq_period_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1340 static void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
1341 struct mlx5e_sq_param *param,
1344 void *sqc = param->sqc;
1345 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1347 mlx5e_build_sq_param_common(priv, param);
1349 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
1350 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
1352 param->icosq = true;
1355 static void mlx5e_build_channel_param(struct mlx5e_priv *priv, struct mlx5e_channel_param *cparam)
1357 u8 icosq_log_wq_sz = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
1359 mlx5e_build_rq_param(priv, &cparam->rq);
1360 mlx5e_build_sq_param(priv, &cparam->sq);
1361 mlx5e_build_icosq_param(priv, &cparam->icosq, icosq_log_wq_sz);
1362 mlx5e_build_rx_cq_param(priv, &cparam->rx_cq);
1363 mlx5e_build_tx_cq_param(priv, &cparam->tx_cq);
1364 mlx5e_build_ico_cq_param(priv, &cparam->icosq_cq, icosq_log_wq_sz);
1367 static int mlx5e_open_channels(struct mlx5e_priv *priv)
1369 struct mlx5e_channel_param *cparam;
1370 int nch = priv->params.num_channels;
1375 priv->channel = kcalloc(nch, sizeof(struct mlx5e_channel *),
1378 priv->txq_to_sq_map = kcalloc(nch * priv->params.num_tc,
1379 sizeof(struct mlx5e_sq *), GFP_KERNEL);
1381 cparam = kzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
1383 if (!priv->channel || !priv->txq_to_sq_map || !cparam)
1384 goto err_free_txq_to_sq_map;
1386 mlx5e_build_channel_param(priv, cparam);
1388 for (i = 0; i < nch; i++) {
1389 err = mlx5e_open_channel(priv, i, cparam, &priv->channel[i]);
1391 goto err_close_channels;
1394 for (j = 0; j < nch; j++) {
1395 err = mlx5e_wait_for_min_rx_wqes(&priv->channel[j]->rq);
1397 goto err_close_channels;
1404 for (i--; i >= 0; i--)
1405 mlx5e_close_channel(priv->channel[i]);
1407 err_free_txq_to_sq_map:
1408 kfree(priv->txq_to_sq_map);
1409 kfree(priv->channel);
1415 static void mlx5e_close_channels(struct mlx5e_priv *priv)
1419 for (i = 0; i < priv->params.num_channels; i++)
1420 mlx5e_close_channel(priv->channel[i]);
1422 kfree(priv->txq_to_sq_map);
1423 kfree(priv->channel);
1426 static int mlx5e_rx_hash_fn(int hfunc)
1428 return (hfunc == ETH_RSS_HASH_TOP) ?
1429 MLX5_RX_HASH_FN_TOEPLITZ :
1430 MLX5_RX_HASH_FN_INVERTED_XOR8;
1433 static int mlx5e_bits_invert(unsigned long a, int size)
1438 for (i = 0; i < size; i++)
1439 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
1444 static void mlx5e_fill_indir_rqt_rqns(struct mlx5e_priv *priv, void *rqtc)
1448 for (i = 0; i < MLX5E_INDIR_RQT_SIZE; i++) {
1452 if (priv->params.rss_hfunc == ETH_RSS_HASH_XOR)
1453 ix = mlx5e_bits_invert(i, MLX5E_LOG_INDIR_RQT_SIZE);
1455 ix = priv->params.indirection_rqt[ix];
1456 rqn = test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1457 priv->channel[ix]->rq.rqn :
1459 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
1463 static void mlx5e_fill_direct_rqt_rqn(struct mlx5e_priv *priv, void *rqtc,
1466 u32 rqn = test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1467 priv->channel[ix]->rq.rqn :
1470 MLX5_SET(rqtc, rqtc, rq_num[0], rqn);
1473 static int mlx5e_create_rqt(struct mlx5e_priv *priv, int sz, int ix, u32 *rqtn)
1475 struct mlx5_core_dev *mdev = priv->mdev;
1481 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
1482 in = mlx5_vzalloc(inlen);
1486 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
1488 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1489 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
1491 if (sz > 1) /* RSS */
1492 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1494 mlx5e_fill_direct_rqt_rqn(priv, rqtc, ix);
1496 err = mlx5_core_create_rqt(mdev, in, inlen, rqtn);
1502 static void mlx5e_destroy_rqt(struct mlx5e_priv *priv, u32 rqtn)
1504 mlx5_core_destroy_rqt(priv->mdev, rqtn);
1507 static int mlx5e_create_rqts(struct mlx5e_priv *priv)
1509 int nch = mlx5e_get_max_num_channels(priv->mdev);
1515 rqtn = &priv->indir_rqtn;
1516 err = mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, 0, rqtn);
1521 for (ix = 0; ix < nch; ix++) {
1522 rqtn = &priv->direct_tir[ix].rqtn;
1523 err = mlx5e_create_rqt(priv, 1 /*size */, ix, rqtn);
1525 goto err_destroy_rqts;
1531 for (ix--; ix >= 0; ix--)
1532 mlx5e_destroy_rqt(priv, priv->direct_tir[ix].rqtn);
1534 mlx5e_destroy_rqt(priv, priv->indir_rqtn);
1539 static void mlx5e_destroy_rqts(struct mlx5e_priv *priv)
1541 int nch = mlx5e_get_max_num_channels(priv->mdev);
1544 for (i = 0; i < nch; i++)
1545 mlx5e_destroy_rqt(priv, priv->direct_tir[i].rqtn);
1547 mlx5e_destroy_rqt(priv, priv->indir_rqtn);
1550 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz, int ix)
1552 struct mlx5_core_dev *mdev = priv->mdev;
1558 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
1559 in = mlx5_vzalloc(inlen);
1563 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
1565 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1566 if (sz > 1) /* RSS */
1567 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1569 mlx5e_fill_direct_rqt_rqn(priv, rqtc, ix);
1571 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
1573 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
1580 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv)
1585 rqtn = priv->indir_rqtn;
1586 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, 0);
1587 for (ix = 0; ix < priv->params.num_channels; ix++) {
1588 rqtn = priv->direct_tir[ix].rqtn;
1589 mlx5e_redirect_rqt(priv, rqtn, 1, ix);
1593 static void mlx5e_build_tir_ctx_lro(void *tirc, struct mlx5e_priv *priv)
1595 if (!priv->params.lro_en)
1598 #define ROUGH_MAX_L2_L3_HDR_SZ 256
1600 MLX5_SET(tirc, tirc, lro_enable_mask,
1601 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
1602 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
1603 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
1604 (priv->params.lro_wqe_sz -
1605 ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
1606 MLX5_SET(tirc, tirc, lro_timeout_period_usecs,
1607 MLX5_CAP_ETH(priv->mdev,
1608 lro_timer_supported_periods[2]));
1611 void mlx5e_build_tir_ctx_hash(void *tirc, struct mlx5e_priv *priv)
1613 MLX5_SET(tirc, tirc, rx_hash_fn,
1614 mlx5e_rx_hash_fn(priv->params.rss_hfunc));
1615 if (priv->params.rss_hfunc == ETH_RSS_HASH_TOP) {
1616 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
1617 rx_hash_toeplitz_key);
1618 size_t len = MLX5_FLD_SZ_BYTES(tirc,
1619 rx_hash_toeplitz_key);
1621 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1622 memcpy(rss_key, priv->params.toeplitz_hash_key, len);
1626 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
1628 struct mlx5_core_dev *mdev = priv->mdev;
1637 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
1638 in = mlx5_vzalloc(inlen);
1642 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
1643 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
1645 mlx5e_build_tir_ctx_lro(tirc, priv);
1647 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
1648 err = mlx5_core_modify_tir(mdev, priv->indir_tirn[tt], in,
1654 for (ix = 0; ix < mlx5e_get_max_num_channels(mdev); ix++) {
1655 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
1667 static int mlx5e_refresh_tirs_self_loopback_enable(struct mlx5e_priv *priv)
1674 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
1675 in = mlx5_vzalloc(inlen);
1679 MLX5_SET(modify_tir_in, in, bitmask.self_lb_en, 1);
1681 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++) {
1682 err = mlx5_core_modify_tir(priv->mdev, priv->indir_tirn[i], in,
1688 for (i = 0; i < priv->params.num_channels; i++) {
1689 err = mlx5_core_modify_tir(priv->mdev,
1690 priv->direct_tir[i].tirn, in,
1701 static int mlx5e_set_mtu(struct mlx5e_priv *priv, u16 mtu)
1703 struct mlx5_core_dev *mdev = priv->mdev;
1704 u16 hw_mtu = MLX5E_SW2HW_MTU(mtu);
1707 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
1711 /* Update vport context MTU */
1712 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
1716 static void mlx5e_query_mtu(struct mlx5e_priv *priv, u16 *mtu)
1718 struct mlx5_core_dev *mdev = priv->mdev;
1722 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
1723 if (err || !hw_mtu) /* fallback to port oper mtu */
1724 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
1726 *mtu = MLX5E_HW2SW_MTU(hw_mtu);
1729 static int mlx5e_set_dev_port_mtu(struct net_device *netdev)
1731 struct mlx5e_priv *priv = netdev_priv(netdev);
1735 err = mlx5e_set_mtu(priv, netdev->mtu);
1739 mlx5e_query_mtu(priv, &mtu);
1740 if (mtu != netdev->mtu)
1741 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
1742 __func__, mtu, netdev->mtu);
1748 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
1750 struct mlx5e_priv *priv = netdev_priv(netdev);
1751 int nch = priv->params.num_channels;
1752 int ntc = priv->params.num_tc;
1755 netdev_reset_tc(netdev);
1760 netdev_set_num_tc(netdev, ntc);
1762 for (tc = 0; tc < ntc; tc++)
1763 netdev_set_tc_queue(netdev, tc, nch, tc * nch);
1766 int mlx5e_open_locked(struct net_device *netdev)
1768 struct mlx5e_priv *priv = netdev_priv(netdev);
1772 set_bit(MLX5E_STATE_OPENED, &priv->state);
1774 mlx5e_netdev_set_tcs(netdev);
1776 num_txqs = priv->params.num_channels * priv->params.num_tc;
1777 netif_set_real_num_tx_queues(netdev, num_txqs);
1778 netif_set_real_num_rx_queues(netdev, priv->params.num_channels);
1780 err = mlx5e_set_dev_port_mtu(netdev);
1782 goto err_clear_state_opened_flag;
1784 err = mlx5e_open_channels(priv);
1786 netdev_err(netdev, "%s: mlx5e_open_channels failed, %d\n",
1788 goto err_clear_state_opened_flag;
1791 err = mlx5e_refresh_tirs_self_loopback_enable(priv);
1793 netdev_err(netdev, "%s: mlx5e_refresh_tirs_self_loopback_enable failed, %d\n",
1795 goto err_close_channels;
1798 mlx5e_redirect_rqts(priv);
1799 mlx5e_update_carrier(priv);
1800 mlx5e_timestamp_init(priv);
1801 #ifdef CONFIG_RFS_ACCEL
1802 priv->netdev->rx_cpu_rmap = priv->mdev->rmap;
1805 queue_delayed_work(priv->wq, &priv->update_stats_work, 0);
1810 mlx5e_close_channels(priv);
1811 err_clear_state_opened_flag:
1812 clear_bit(MLX5E_STATE_OPENED, &priv->state);
1816 static int mlx5e_open(struct net_device *netdev)
1818 struct mlx5e_priv *priv = netdev_priv(netdev);
1821 mutex_lock(&priv->state_lock);
1822 err = mlx5e_open_locked(netdev);
1823 mutex_unlock(&priv->state_lock);
1828 int mlx5e_close_locked(struct net_device *netdev)
1830 struct mlx5e_priv *priv = netdev_priv(netdev);
1832 /* May already be CLOSED in case a previous configuration operation
1833 * (e.g RX/TX queue size change) that involves close&open failed.
1835 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
1838 clear_bit(MLX5E_STATE_OPENED, &priv->state);
1840 mlx5e_timestamp_cleanup(priv);
1841 netif_carrier_off(priv->netdev);
1842 mlx5e_redirect_rqts(priv);
1843 mlx5e_close_channels(priv);
1848 static int mlx5e_close(struct net_device *netdev)
1850 struct mlx5e_priv *priv = netdev_priv(netdev);
1853 mutex_lock(&priv->state_lock);
1854 err = mlx5e_close_locked(netdev);
1855 mutex_unlock(&priv->state_lock);
1860 static int mlx5e_create_drop_rq(struct mlx5e_priv *priv,
1861 struct mlx5e_rq *rq,
1862 struct mlx5e_rq_param *param)
1864 struct mlx5_core_dev *mdev = priv->mdev;
1865 void *rqc = param->rqc;
1866 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
1869 param->wq.db_numa_node = param->wq.buf_numa_node;
1871 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
1881 static int mlx5e_create_drop_cq(struct mlx5e_priv *priv,
1882 struct mlx5e_cq *cq,
1883 struct mlx5e_cq_param *param)
1885 struct mlx5_core_dev *mdev = priv->mdev;
1886 struct mlx5_core_cq *mcq = &cq->mcq;
1891 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1896 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1899 mcq->set_ci_db = cq->wq_ctrl.db.db;
1900 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1901 *mcq->set_ci_db = 0;
1903 mcq->vector = param->eq_ix;
1904 mcq->comp = mlx5e_completion_event;
1905 mcq->event = mlx5e_cq_error_event;
1907 mcq->uar = &priv->cq_uar;
1914 static int mlx5e_open_drop_rq(struct mlx5e_priv *priv)
1916 struct mlx5e_cq_param cq_param;
1917 struct mlx5e_rq_param rq_param;
1918 struct mlx5e_rq *rq = &priv->drop_rq;
1919 struct mlx5e_cq *cq = &priv->drop_rq.cq;
1922 memset(&cq_param, 0, sizeof(cq_param));
1923 memset(&rq_param, 0, sizeof(rq_param));
1924 mlx5e_build_drop_rq_param(&rq_param);
1926 err = mlx5e_create_drop_cq(priv, cq, &cq_param);
1930 err = mlx5e_enable_cq(cq, &cq_param);
1932 goto err_destroy_cq;
1934 err = mlx5e_create_drop_rq(priv, rq, &rq_param);
1936 goto err_disable_cq;
1938 err = mlx5e_enable_rq(rq, &rq_param);
1940 goto err_destroy_rq;
1945 mlx5e_destroy_rq(&priv->drop_rq);
1948 mlx5e_disable_cq(&priv->drop_rq.cq);
1951 mlx5e_destroy_cq(&priv->drop_rq.cq);
1956 static void mlx5e_close_drop_rq(struct mlx5e_priv *priv)
1958 mlx5e_disable_rq(&priv->drop_rq);
1959 mlx5e_destroy_rq(&priv->drop_rq);
1960 mlx5e_disable_cq(&priv->drop_rq.cq);
1961 mlx5e_destroy_cq(&priv->drop_rq.cq);
1964 static int mlx5e_create_tis(struct mlx5e_priv *priv, int tc)
1966 struct mlx5_core_dev *mdev = priv->mdev;
1967 u32 in[MLX5_ST_SZ_DW(create_tis_in)];
1968 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1970 memset(in, 0, sizeof(in));
1972 MLX5_SET(tisc, tisc, prio, tc << 1);
1973 MLX5_SET(tisc, tisc, transport_domain, priv->tdn);
1975 return mlx5_core_create_tis(mdev, in, sizeof(in), &priv->tisn[tc]);
1978 static void mlx5e_destroy_tis(struct mlx5e_priv *priv, int tc)
1980 mlx5_core_destroy_tis(priv->mdev, priv->tisn[tc]);
1983 static int mlx5e_create_tises(struct mlx5e_priv *priv)
1988 for (tc = 0; tc < MLX5E_MAX_NUM_TC; tc++) {
1989 err = mlx5e_create_tis(priv, tc);
1991 goto err_close_tises;
1997 for (tc--; tc >= 0; tc--)
1998 mlx5e_destroy_tis(priv, tc);
2003 static void mlx5e_destroy_tises(struct mlx5e_priv *priv)
2007 for (tc = 0; tc < MLX5E_MAX_NUM_TC; tc++)
2008 mlx5e_destroy_tis(priv, tc);
2011 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv, u32 *tirc,
2012 enum mlx5e_traffic_types tt)
2014 void *hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2016 MLX5_SET(tirc, tirc, transport_domain, priv->tdn);
2018 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
2019 MLX5_HASH_FIELD_SEL_DST_IP)
2021 #define MLX5_HASH_IP_L4PORTS (MLX5_HASH_FIELD_SEL_SRC_IP |\
2022 MLX5_HASH_FIELD_SEL_DST_IP |\
2023 MLX5_HASH_FIELD_SEL_L4_SPORT |\
2024 MLX5_HASH_FIELD_SEL_L4_DPORT)
2026 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
2027 MLX5_HASH_FIELD_SEL_DST_IP |\
2028 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
2030 mlx5e_build_tir_ctx_lro(tirc, priv);
2032 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2033 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqtn);
2034 mlx5e_build_tir_ctx_hash(tirc, priv);
2037 case MLX5E_TT_IPV4_TCP:
2038 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2039 MLX5_L3_PROT_TYPE_IPV4);
2040 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2041 MLX5_L4_PROT_TYPE_TCP);
2042 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2043 MLX5_HASH_IP_L4PORTS);
2046 case MLX5E_TT_IPV6_TCP:
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, l4_prot_type,
2050 MLX5_L4_PROT_TYPE_TCP);
2051 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2052 MLX5_HASH_IP_L4PORTS);
2055 case MLX5E_TT_IPV4_UDP:
2056 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2057 MLX5_L3_PROT_TYPE_IPV4);
2058 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2059 MLX5_L4_PROT_TYPE_UDP);
2060 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2061 MLX5_HASH_IP_L4PORTS);
2064 case MLX5E_TT_IPV6_UDP:
2065 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2066 MLX5_L3_PROT_TYPE_IPV6);
2067 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2068 MLX5_L4_PROT_TYPE_UDP);
2069 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2070 MLX5_HASH_IP_L4PORTS);
2073 case MLX5E_TT_IPV4_IPSEC_AH:
2074 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2075 MLX5_L3_PROT_TYPE_IPV4);
2076 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2077 MLX5_HASH_IP_IPSEC_SPI);
2080 case MLX5E_TT_IPV6_IPSEC_AH:
2081 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2082 MLX5_L3_PROT_TYPE_IPV6);
2083 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2084 MLX5_HASH_IP_IPSEC_SPI);
2087 case MLX5E_TT_IPV4_IPSEC_ESP:
2088 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2089 MLX5_L3_PROT_TYPE_IPV4);
2090 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2091 MLX5_HASH_IP_IPSEC_SPI);
2094 case MLX5E_TT_IPV6_IPSEC_ESP:
2095 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2096 MLX5_L3_PROT_TYPE_IPV6);
2097 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2098 MLX5_HASH_IP_IPSEC_SPI);
2102 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2103 MLX5_L3_PROT_TYPE_IPV4);
2104 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2109 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2110 MLX5_L3_PROT_TYPE_IPV6);
2111 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2116 "mlx5e_build_indir_tir_ctx: bad traffic type!\n");
2120 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 *tirc,
2123 MLX5_SET(tirc, tirc, transport_domain, priv->tdn);
2125 mlx5e_build_tir_ctx_lro(tirc, priv);
2127 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2128 MLX5_SET(tirc, tirc, indirect_table, rqtn);
2129 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
2132 static int mlx5e_create_tirs(struct mlx5e_priv *priv)
2134 int nch = mlx5e_get_max_num_channels(priv->mdev);
2143 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2144 in = mlx5_vzalloc(inlen);
2149 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2150 memset(in, 0, inlen);
2151 tirn = &priv->indir_tirn[tt];
2152 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2153 mlx5e_build_indir_tir_ctx(priv, tirc, tt);
2154 err = mlx5_core_create_tir(priv->mdev, in, inlen, tirn);
2156 goto err_destroy_tirs;
2160 for (ix = 0; ix < nch; ix++) {
2161 memset(in, 0, inlen);
2162 tirn = &priv->direct_tir[ix].tirn;
2163 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2164 mlx5e_build_direct_tir_ctx(priv, tirc,
2165 priv->direct_tir[ix].rqtn);
2166 err = mlx5_core_create_tir(priv->mdev, in, inlen, tirn);
2168 goto err_destroy_ch_tirs;
2175 err_destroy_ch_tirs:
2176 for (ix--; ix >= 0; ix--)
2177 mlx5_core_destroy_tir(priv->mdev, priv->direct_tir[ix].tirn);
2180 for (tt--; tt >= 0; tt--)
2181 mlx5_core_destroy_tir(priv->mdev, priv->indir_tirn[tt]);
2188 static void mlx5e_destroy_tirs(struct mlx5e_priv *priv)
2190 int nch = mlx5e_get_max_num_channels(priv->mdev);
2193 for (i = 0; i < nch; i++)
2194 mlx5_core_destroy_tir(priv->mdev, priv->direct_tir[i].tirn);
2196 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
2197 mlx5_core_destroy_tir(priv->mdev, priv->indir_tirn[i]);
2200 int mlx5e_modify_rqs_vsd(struct mlx5e_priv *priv, bool vsd)
2205 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
2208 for (i = 0; i < priv->params.num_channels; i++) {
2209 err = mlx5e_modify_rq_vsd(&priv->channel[i]->rq, vsd);
2217 static int mlx5e_setup_tc(struct net_device *netdev, u8 tc)
2219 struct mlx5e_priv *priv = netdev_priv(netdev);
2223 if (tc && tc != MLX5E_MAX_NUM_TC)
2226 mutex_lock(&priv->state_lock);
2228 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2230 mlx5e_close_locked(priv->netdev);
2232 priv->params.num_tc = tc ? tc : 1;
2235 err = mlx5e_open_locked(priv->netdev);
2237 mutex_unlock(&priv->state_lock);
2242 static int mlx5e_ndo_setup_tc(struct net_device *dev, u32 handle,
2243 __be16 proto, struct tc_to_netdev *tc)
2245 struct mlx5e_priv *priv = netdev_priv(dev);
2247 if (TC_H_MAJ(handle) != TC_H_MAJ(TC_H_INGRESS))
2251 case TC_SETUP_CLSFLOWER:
2252 switch (tc->cls_flower->command) {
2253 case TC_CLSFLOWER_REPLACE:
2254 return mlx5e_configure_flower(priv, proto, tc->cls_flower);
2255 case TC_CLSFLOWER_DESTROY:
2256 return mlx5e_delete_flower(priv, tc->cls_flower);
2257 case TC_CLSFLOWER_STATS:
2258 return mlx5e_stats_flower(priv, tc->cls_flower);
2265 if (tc->type != TC_SETUP_MQPRIO)
2268 return mlx5e_setup_tc(dev, tc->tc);
2271 static struct rtnl_link_stats64 *
2272 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
2274 struct mlx5e_priv *priv = netdev_priv(dev);
2275 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
2276 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
2277 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
2279 stats->rx_packets = sstats->rx_packets;
2280 stats->rx_bytes = sstats->rx_bytes;
2281 stats->tx_packets = sstats->tx_packets;
2282 stats->tx_bytes = sstats->tx_bytes;
2284 stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
2285 stats->tx_dropped = sstats->tx_queue_dropped;
2287 stats->rx_length_errors =
2288 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
2289 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
2290 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
2291 stats->rx_crc_errors =
2292 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
2293 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
2294 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
2295 stats->tx_carrier_errors =
2296 PPORT_802_3_GET(pstats, a_symbol_error_during_carrier);
2297 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
2298 stats->rx_frame_errors;
2299 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
2301 /* vport multicast also counts packets that are dropped due to steering
2302 * or rx out of buffer
2305 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
2310 static void mlx5e_set_rx_mode(struct net_device *dev)
2312 struct mlx5e_priv *priv = netdev_priv(dev);
2314 queue_work(priv->wq, &priv->set_rx_mode_work);
2317 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
2319 struct mlx5e_priv *priv = netdev_priv(netdev);
2320 struct sockaddr *saddr = addr;
2322 if (!is_valid_ether_addr(saddr->sa_data))
2323 return -EADDRNOTAVAIL;
2325 netif_addr_lock_bh(netdev);
2326 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
2327 netif_addr_unlock_bh(netdev);
2329 queue_work(priv->wq, &priv->set_rx_mode_work);
2334 #define MLX5E_SET_FEATURE(netdev, feature, enable) \
2337 netdev->features |= feature; \
2339 netdev->features &= ~feature; \
2342 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
2344 static int set_feature_lro(struct net_device *netdev, bool enable)
2346 struct mlx5e_priv *priv = netdev_priv(netdev);
2347 bool was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2350 mutex_lock(&priv->state_lock);
2352 if (was_opened && (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST))
2353 mlx5e_close_locked(priv->netdev);
2355 priv->params.lro_en = enable;
2356 err = mlx5e_modify_tirs_lro(priv);
2358 netdev_err(netdev, "lro modify failed, %d\n", err);
2359 priv->params.lro_en = !enable;
2362 if (was_opened && (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST))
2363 mlx5e_open_locked(priv->netdev);
2365 mutex_unlock(&priv->state_lock);
2370 static int set_feature_vlan_filter(struct net_device *netdev, bool enable)
2372 struct mlx5e_priv *priv = netdev_priv(netdev);
2375 mlx5e_enable_vlan_filter(priv);
2377 mlx5e_disable_vlan_filter(priv);
2382 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
2384 struct mlx5e_priv *priv = netdev_priv(netdev);
2386 if (!enable && mlx5e_tc_num_filters(priv)) {
2388 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
2395 static int set_feature_rx_all(struct net_device *netdev, bool enable)
2397 struct mlx5e_priv *priv = netdev_priv(netdev);
2398 struct mlx5_core_dev *mdev = priv->mdev;
2400 return mlx5_set_port_fcs(mdev, !enable);
2403 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
2405 struct mlx5e_priv *priv = netdev_priv(netdev);
2408 mutex_lock(&priv->state_lock);
2410 priv->params.vlan_strip_disable = !enable;
2411 err = mlx5e_modify_rqs_vsd(priv, !enable);
2413 priv->params.vlan_strip_disable = enable;
2415 mutex_unlock(&priv->state_lock);
2420 #ifdef CONFIG_RFS_ACCEL
2421 static int set_feature_arfs(struct net_device *netdev, bool enable)
2423 struct mlx5e_priv *priv = netdev_priv(netdev);
2427 err = mlx5e_arfs_enable(priv);
2429 err = mlx5e_arfs_disable(priv);
2435 static int mlx5e_handle_feature(struct net_device *netdev,
2436 netdev_features_t wanted_features,
2437 netdev_features_t feature,
2438 mlx5e_feature_handler feature_handler)
2440 netdev_features_t changes = wanted_features ^ netdev->features;
2441 bool enable = !!(wanted_features & feature);
2444 if (!(changes & feature))
2447 err = feature_handler(netdev, enable);
2449 netdev_err(netdev, "%s feature 0x%llx failed err %d\n",
2450 enable ? "Enable" : "Disable", feature, err);
2454 MLX5E_SET_FEATURE(netdev, feature, enable);
2458 static int mlx5e_set_features(struct net_device *netdev,
2459 netdev_features_t features)
2463 err = mlx5e_handle_feature(netdev, features, NETIF_F_LRO,
2465 err |= mlx5e_handle_feature(netdev, features,
2466 NETIF_F_HW_VLAN_CTAG_FILTER,
2467 set_feature_vlan_filter);
2468 err |= mlx5e_handle_feature(netdev, features, NETIF_F_HW_TC,
2469 set_feature_tc_num_filters);
2470 err |= mlx5e_handle_feature(netdev, features, NETIF_F_RXALL,
2471 set_feature_rx_all);
2472 err |= mlx5e_handle_feature(netdev, features, NETIF_F_HW_VLAN_CTAG_RX,
2473 set_feature_rx_vlan);
2474 #ifdef CONFIG_RFS_ACCEL
2475 err |= mlx5e_handle_feature(netdev, features, NETIF_F_NTUPLE,
2479 return err ? -EINVAL : 0;
2482 #define MXL5_HW_MIN_MTU 64
2483 #define MXL5E_MIN_MTU (MXL5_HW_MIN_MTU + ETH_FCS_LEN)
2485 static int mlx5e_change_mtu(struct net_device *netdev, int new_mtu)
2487 struct mlx5e_priv *priv = netdev_priv(netdev);
2488 struct mlx5_core_dev *mdev = priv->mdev;
2494 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
2496 max_mtu = MLX5E_HW2SW_MTU(max_mtu);
2497 min_mtu = MLX5E_HW2SW_MTU(MXL5E_MIN_MTU);
2499 if (new_mtu > max_mtu || new_mtu < min_mtu) {
2501 "%s: Bad MTU (%d), valid range is: [%d..%d]\n",
2502 __func__, new_mtu, min_mtu, max_mtu);
2506 mutex_lock(&priv->state_lock);
2508 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2510 mlx5e_close_locked(netdev);
2512 netdev->mtu = new_mtu;
2515 err = mlx5e_open_locked(netdev);
2517 mutex_unlock(&priv->state_lock);
2522 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2526 return mlx5e_hwstamp_set(dev, ifr);
2528 return mlx5e_hwstamp_get(dev, ifr);
2534 static int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2536 struct mlx5e_priv *priv = netdev_priv(dev);
2537 struct mlx5_core_dev *mdev = priv->mdev;
2539 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
2542 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos)
2544 struct mlx5e_priv *priv = netdev_priv(dev);
2545 struct mlx5_core_dev *mdev = priv->mdev;
2547 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
2551 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2553 struct mlx5e_priv *priv = netdev_priv(dev);
2554 struct mlx5_core_dev *mdev = priv->mdev;
2556 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
2559 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
2561 struct mlx5e_priv *priv = netdev_priv(dev);
2562 struct mlx5_core_dev *mdev = priv->mdev;
2564 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
2566 static int mlx5_vport_link2ifla(u8 esw_link)
2569 case MLX5_ESW_VPORT_ADMIN_STATE_DOWN:
2570 return IFLA_VF_LINK_STATE_DISABLE;
2571 case MLX5_ESW_VPORT_ADMIN_STATE_UP:
2572 return IFLA_VF_LINK_STATE_ENABLE;
2574 return IFLA_VF_LINK_STATE_AUTO;
2577 static int mlx5_ifla_link2vport(u8 ifla_link)
2579 switch (ifla_link) {
2580 case IFLA_VF_LINK_STATE_DISABLE:
2581 return MLX5_ESW_VPORT_ADMIN_STATE_DOWN;
2582 case IFLA_VF_LINK_STATE_ENABLE:
2583 return MLX5_ESW_VPORT_ADMIN_STATE_UP;
2585 return MLX5_ESW_VPORT_ADMIN_STATE_AUTO;
2588 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
2591 struct mlx5e_priv *priv = netdev_priv(dev);
2592 struct mlx5_core_dev *mdev = priv->mdev;
2594 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
2595 mlx5_ifla_link2vport(link_state));
2598 static int mlx5e_get_vf_config(struct net_device *dev,
2599 int vf, struct ifla_vf_info *ivi)
2601 struct mlx5e_priv *priv = netdev_priv(dev);
2602 struct mlx5_core_dev *mdev = priv->mdev;
2605 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
2608 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
2612 static int mlx5e_get_vf_stats(struct net_device *dev,
2613 int vf, struct ifla_vf_stats *vf_stats)
2615 struct mlx5e_priv *priv = netdev_priv(dev);
2616 struct mlx5_core_dev *mdev = priv->mdev;
2618 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
2622 static void mlx5e_add_vxlan_port(struct net_device *netdev,
2623 struct udp_tunnel_info *ti)
2625 struct mlx5e_priv *priv = netdev_priv(netdev);
2627 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2630 if (!mlx5e_vxlan_allowed(priv->mdev))
2633 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 1);
2636 static void mlx5e_del_vxlan_port(struct net_device *netdev,
2637 struct udp_tunnel_info *ti)
2639 struct mlx5e_priv *priv = netdev_priv(netdev);
2641 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2644 if (!mlx5e_vxlan_allowed(priv->mdev))
2647 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 0);
2650 static netdev_features_t mlx5e_vxlan_features_check(struct mlx5e_priv *priv,
2651 struct sk_buff *skb,
2652 netdev_features_t features)
2654 struct udphdr *udph;
2658 switch (vlan_get_protocol(skb)) {
2659 case htons(ETH_P_IP):
2660 proto = ip_hdr(skb)->protocol;
2662 case htons(ETH_P_IPV6):
2663 proto = ipv6_hdr(skb)->nexthdr;
2669 if (proto == IPPROTO_UDP) {
2670 udph = udp_hdr(skb);
2671 port = be16_to_cpu(udph->dest);
2674 /* Verify if UDP port is being offloaded by HW */
2675 if (port && mlx5e_vxlan_lookup_port(priv, port))
2679 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
2680 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2683 static netdev_features_t mlx5e_features_check(struct sk_buff *skb,
2684 struct net_device *netdev,
2685 netdev_features_t features)
2687 struct mlx5e_priv *priv = netdev_priv(netdev);
2689 features = vlan_features_check(skb, features);
2690 features = vxlan_features_check(skb, features);
2692 /* Validate if the tunneled packet is being offloaded by HW */
2693 if (skb->encapsulation &&
2694 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
2695 return mlx5e_vxlan_features_check(priv, skb, features);
2700 static const struct net_device_ops mlx5e_netdev_ops_basic = {
2701 .ndo_open = mlx5e_open,
2702 .ndo_stop = mlx5e_close,
2703 .ndo_start_xmit = mlx5e_xmit,
2704 .ndo_setup_tc = mlx5e_ndo_setup_tc,
2705 .ndo_select_queue = mlx5e_select_queue,
2706 .ndo_get_stats64 = mlx5e_get_stats,
2707 .ndo_set_rx_mode = mlx5e_set_rx_mode,
2708 .ndo_set_mac_address = mlx5e_set_mac,
2709 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
2710 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
2711 .ndo_set_features = mlx5e_set_features,
2712 .ndo_change_mtu = mlx5e_change_mtu,
2713 .ndo_do_ioctl = mlx5e_ioctl,
2714 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
2715 #ifdef CONFIG_RFS_ACCEL
2716 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
2720 static const struct net_device_ops mlx5e_netdev_ops_sriov = {
2721 .ndo_open = mlx5e_open,
2722 .ndo_stop = mlx5e_close,
2723 .ndo_start_xmit = mlx5e_xmit,
2724 .ndo_setup_tc = mlx5e_ndo_setup_tc,
2725 .ndo_select_queue = mlx5e_select_queue,
2726 .ndo_get_stats64 = mlx5e_get_stats,
2727 .ndo_set_rx_mode = mlx5e_set_rx_mode,
2728 .ndo_set_mac_address = mlx5e_set_mac,
2729 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
2730 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
2731 .ndo_set_features = mlx5e_set_features,
2732 .ndo_change_mtu = mlx5e_change_mtu,
2733 .ndo_do_ioctl = mlx5e_ioctl,
2734 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
2735 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
2736 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
2737 .ndo_features_check = mlx5e_features_check,
2738 #ifdef CONFIG_RFS_ACCEL
2739 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
2741 .ndo_set_vf_mac = mlx5e_set_vf_mac,
2742 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
2743 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
2744 .ndo_set_vf_trust = mlx5e_set_vf_trust,
2745 .ndo_get_vf_config = mlx5e_get_vf_config,
2746 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
2747 .ndo_get_vf_stats = mlx5e_get_vf_stats,
2750 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
2752 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
2754 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
2755 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
2756 !MLX5_CAP_ETH(mdev, csum_cap) ||
2757 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
2758 !MLX5_CAP_ETH(mdev, vlan_cap) ||
2759 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
2760 MLX5_CAP_FLOWTABLE(mdev,
2761 flow_table_properties_nic_receive.max_ft_level)
2763 mlx5_core_warn(mdev,
2764 "Not creating net device, some required device capabilities are missing\n");
2767 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
2768 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
2769 if (!MLX5_CAP_GEN(mdev, cq_moderation))
2770 mlx5_core_warn(mdev, "CQ modiration is not supported\n");
2775 u16 mlx5e_get_max_inline_cap(struct mlx5_core_dev *mdev)
2777 int bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
2779 return bf_buf_size -
2780 sizeof(struct mlx5e_tx_wqe) +
2781 2 /*sizeof(mlx5e_tx_wqe.inline_hdr_start)*/;
2784 #ifdef CONFIG_MLX5_CORE_EN_DCB
2785 static void mlx5e_ets_init(struct mlx5e_priv *priv)
2789 priv->params.ets.ets_cap = mlx5_max_tc(priv->mdev) + 1;
2790 for (i = 0; i < priv->params.ets.ets_cap; i++) {
2791 priv->params.ets.tc_tx_bw[i] = MLX5E_MAX_BW_ALLOC;
2792 priv->params.ets.tc_tsa[i] = IEEE_8021QAZ_TSA_VENDOR;
2793 priv->params.ets.prio_tc[i] = i;
2796 /* tclass[prio=0]=1, tclass[prio=1]=0, tclass[prio=i]=i (for i>1) */
2797 priv->params.ets.prio_tc[0] = 1;
2798 priv->params.ets.prio_tc[1] = 0;
2802 void mlx5e_build_default_indir_rqt(struct mlx5_core_dev *mdev,
2803 u32 *indirection_rqt, int len,
2806 int node = mdev->priv.numa_node;
2807 int node_num_of_cores;
2811 node = first_online_node;
2813 node_num_of_cores = cpumask_weight(cpumask_of_node(node));
2815 if (node_num_of_cores)
2816 num_channels = min_t(int, num_channels, node_num_of_cores);
2818 for (i = 0; i < len; i++)
2819 indirection_rqt[i] = i % num_channels;
2822 static bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
2824 return MLX5_CAP_GEN(mdev, striding_rq) &&
2825 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
2826 MLX5_CAP_ETH(mdev, reg_umr_sq);
2829 static int mlx5e_get_pci_bw(struct mlx5_core_dev *mdev, u32 *pci_bw)
2831 enum pcie_link_width width;
2832 enum pci_bus_speed speed;
2835 err = pcie_get_minimum_link(mdev->pdev, &speed, &width);
2839 if (speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
2843 case PCIE_SPEED_2_5GT:
2844 *pci_bw = 2500 * width;
2846 case PCIE_SPEED_5_0GT:
2847 *pci_bw = 5000 * width;
2849 case PCIE_SPEED_8_0GT:
2850 *pci_bw = 8000 * width;
2859 static bool cqe_compress_heuristic(u32 link_speed, u32 pci_bw)
2861 return (link_speed && pci_bw &&
2862 (pci_bw < 40000) && (pci_bw < link_speed));
2865 void mlx5e_set_rx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
2867 params->rx_cq_period_mode = cq_period_mode;
2869 params->rx_cq_moderation.pkts =
2870 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
2871 params->rx_cq_moderation.usec =
2872 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
2874 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
2875 params->rx_cq_moderation.usec =
2876 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE;
2879 static void mlx5e_build_netdev_priv(struct mlx5_core_dev *mdev,
2880 struct net_device *netdev,
2883 struct mlx5e_priv *priv = netdev_priv(netdev);
2887 priv->params.log_sq_size =
2888 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
2889 priv->params.rq_wq_type = mlx5e_check_fragmented_striding_rq_cap(mdev) ?
2890 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
2891 MLX5_WQ_TYPE_LINKED_LIST;
2893 /* set CQE compression */
2894 priv->params.rx_cqe_compress_admin = false;
2895 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
2896 MLX5_CAP_GEN(mdev, vport_group_manager)) {
2897 mlx5e_get_max_linkspeed(mdev, &link_speed);
2898 mlx5e_get_pci_bw(mdev, &pci_bw);
2899 mlx5_core_dbg(mdev, "Max link speed = %d, PCI BW = %d\n",
2900 link_speed, pci_bw);
2901 priv->params.rx_cqe_compress_admin =
2902 cqe_compress_heuristic(link_speed, pci_bw);
2905 priv->params.rx_cqe_compress = priv->params.rx_cqe_compress_admin;
2907 switch (priv->params.rq_wq_type) {
2908 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
2909 priv->params.log_rq_size = MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE_MPW;
2910 priv->params.mpwqe_log_stride_sz =
2911 priv->params.rx_cqe_compress ?
2912 MLX5_MPWRQ_LOG_STRIDE_SIZE_CQE_COMPRESS :
2913 MLX5_MPWRQ_LOG_STRIDE_SIZE;
2914 priv->params.mpwqe_log_num_strides = MLX5_MPWRQ_LOG_WQE_SZ -
2915 priv->params.mpwqe_log_stride_sz;
2916 priv->params.lro_en = true;
2918 default: /* MLX5_WQ_TYPE_LINKED_LIST */
2919 priv->params.log_rq_size = MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
2922 mlx5_core_info(mdev,
2923 "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
2924 priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
2925 BIT(priv->params.log_rq_size),
2926 BIT(priv->params.mpwqe_log_stride_sz),
2927 priv->params.rx_cqe_compress_admin);
2929 priv->params.min_rx_wqes = mlx5_min_rx_wqes(priv->params.rq_wq_type,
2930 BIT(priv->params.log_rq_size));
2932 mlx5e_set_rx_cq_mode_params(&priv->params,
2933 MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
2935 priv->params.tx_cq_moderation.usec =
2936 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
2937 priv->params.tx_cq_moderation.pkts =
2938 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
2939 priv->params.tx_max_inline = mlx5e_get_max_inline_cap(mdev);
2940 priv->params.num_tc = 1;
2941 priv->params.rss_hfunc = ETH_RSS_HASH_XOR;
2943 netdev_rss_key_fill(priv->params.toeplitz_hash_key,
2944 sizeof(priv->params.toeplitz_hash_key));
2946 mlx5e_build_default_indir_rqt(mdev, priv->params.indirection_rqt,
2947 MLX5E_INDIR_RQT_SIZE, num_channels);
2949 priv->params.lro_wqe_sz =
2950 MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
2952 /* Initialize pflags */
2953 MLX5E_SET_PRIV_FLAG(priv, MLX5E_PFLAG_RX_CQE_BASED_MODER,
2954 priv->params.rx_cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
2957 priv->netdev = netdev;
2958 priv->params.num_channels = num_channels;
2960 #ifdef CONFIG_MLX5_CORE_EN_DCB
2961 mlx5e_ets_init(priv);
2964 mutex_init(&priv->state_lock);
2966 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
2967 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
2968 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
2971 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
2973 struct mlx5e_priv *priv = netdev_priv(netdev);
2975 mlx5_query_nic_vport_mac_address(priv->mdev, 0, netdev->dev_addr);
2976 if (is_zero_ether_addr(netdev->dev_addr) &&
2977 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
2978 eth_hw_addr_random(netdev);
2979 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
2983 static void mlx5e_build_netdev(struct net_device *netdev)
2985 struct mlx5e_priv *priv = netdev_priv(netdev);
2986 struct mlx5_core_dev *mdev = priv->mdev;
2990 SET_NETDEV_DEV(netdev, &mdev->pdev->dev);
2992 if (MLX5_CAP_GEN(mdev, vport_group_manager)) {
2993 netdev->netdev_ops = &mlx5e_netdev_ops_sriov;
2994 #ifdef CONFIG_MLX5_CORE_EN_DCB
2995 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
2998 netdev->netdev_ops = &mlx5e_netdev_ops_basic;
3001 netdev->watchdog_timeo = 15 * HZ;
3003 netdev->ethtool_ops = &mlx5e_ethtool_ops;
3005 netdev->vlan_features |= NETIF_F_SG;
3006 netdev->vlan_features |= NETIF_F_IP_CSUM;
3007 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
3008 netdev->vlan_features |= NETIF_F_GRO;
3009 netdev->vlan_features |= NETIF_F_TSO;
3010 netdev->vlan_features |= NETIF_F_TSO6;
3011 netdev->vlan_features |= NETIF_F_RXCSUM;
3012 netdev->vlan_features |= NETIF_F_RXHASH;
3014 if (!!MLX5_CAP_ETH(mdev, lro_cap))
3015 netdev->vlan_features |= NETIF_F_LRO;
3017 netdev->hw_features = netdev->vlan_features;
3018 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
3019 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
3020 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3022 if (mlx5e_vxlan_allowed(mdev)) {
3023 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
3024 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3025 NETIF_F_GSO_PARTIAL;
3026 netdev->hw_enc_features |= NETIF_F_IP_CSUM;
3027 netdev->hw_enc_features |= NETIF_F_IPV6_CSUM;
3028 netdev->hw_enc_features |= NETIF_F_TSO;
3029 netdev->hw_enc_features |= NETIF_F_TSO6;
3030 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL;
3031 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
3032 NETIF_F_GSO_PARTIAL;
3033 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
3036 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
3039 netdev->hw_features |= NETIF_F_RXALL;
3041 netdev->features = netdev->hw_features;
3042 if (!priv->params.lro_en)
3043 netdev->features &= ~NETIF_F_LRO;
3046 netdev->features &= ~NETIF_F_RXALL;
3048 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
3049 if (FT_CAP(flow_modify_en) &&
3050 FT_CAP(modify_root) &&
3051 FT_CAP(identified_miss_table_mode) &&
3052 FT_CAP(flow_table_modify)) {
3053 netdev->hw_features |= NETIF_F_HW_TC;
3054 #ifdef CONFIG_RFS_ACCEL
3055 netdev->hw_features |= NETIF_F_NTUPLE;
3059 netdev->features |= NETIF_F_HIGHDMA;
3061 netdev->priv_flags |= IFF_UNICAST_FLT;
3063 mlx5e_set_netdev_dev_addr(netdev);
3066 static int mlx5e_create_mkey(struct mlx5e_priv *priv, u32 pdn,
3067 struct mlx5_core_mkey *mkey)
3069 struct mlx5_core_dev *mdev = priv->mdev;
3070 struct mlx5_create_mkey_mbox_in *in;
3073 in = mlx5_vzalloc(sizeof(*in));
3077 in->seg.flags = MLX5_PERM_LOCAL_WRITE |
3078 MLX5_PERM_LOCAL_READ |
3079 MLX5_ACCESS_MODE_PA;
3080 in->seg.flags_pd = cpu_to_be32(pdn | MLX5_MKEY_LEN64);
3081 in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
3083 err = mlx5_core_create_mkey(mdev, mkey, in, sizeof(*in), NULL, NULL,
3091 static void mlx5e_create_q_counter(struct mlx5e_priv *priv)
3093 struct mlx5_core_dev *mdev = priv->mdev;
3096 err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
3098 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
3099 priv->q_counter = 0;
3103 static void mlx5e_destroy_q_counter(struct mlx5e_priv *priv)
3105 if (!priv->q_counter)
3108 mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
3111 static int mlx5e_create_umr_mkey(struct mlx5e_priv *priv)
3113 struct mlx5_core_dev *mdev = priv->mdev;
3114 struct mlx5_create_mkey_mbox_in *in;
3115 struct mlx5_mkey_seg *mkc;
3116 int inlen = sizeof(*in);
3118 mlx5e_get_max_num_channels(mdev) * MLX5_CHANNEL_MAX_NUM_MTTS;
3121 in = mlx5_vzalloc(inlen);
3126 mkc->status = MLX5_MKEY_STATUS_FREE;
3127 mkc->flags = MLX5_PERM_UMR_EN |
3128 MLX5_PERM_LOCAL_READ |
3129 MLX5_PERM_LOCAL_WRITE |
3130 MLX5_ACCESS_MODE_MTT;
3132 mkc->qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
3133 mkc->flags_pd = cpu_to_be32(priv->pdn);
3134 mkc->len = cpu_to_be64(npages << PAGE_SHIFT);
3135 mkc->xlt_oct_size = cpu_to_be32(mlx5e_get_mtt_octw(npages));
3136 mkc->log2_page_size = PAGE_SHIFT;
3138 err = mlx5_core_create_mkey(mdev, &priv->umr_mkey, in, inlen, NULL,
3146 static void *mlx5e_create_netdev(struct mlx5_core_dev *mdev)
3148 struct net_device *netdev;
3149 struct mlx5e_priv *priv;
3150 int nch = mlx5e_get_max_num_channels(mdev);
3153 if (mlx5e_check_required_hca_cap(mdev))
3156 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
3157 nch * MLX5E_MAX_NUM_TC,
3160 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
3164 mlx5e_build_netdev_priv(mdev, netdev, nch);
3165 mlx5e_build_netdev(netdev);
3167 netif_carrier_off(netdev);
3169 priv = netdev_priv(netdev);
3171 priv->wq = create_singlethread_workqueue("mlx5e");
3173 goto err_free_netdev;
3175 err = mlx5_alloc_map_uar(mdev, &priv->cq_uar, false);
3177 mlx5_core_err(mdev, "alloc_map uar failed, %d\n", err);
3178 goto err_destroy_wq;
3181 err = mlx5_core_alloc_pd(mdev, &priv->pdn);
3183 mlx5_core_err(mdev, "alloc pd failed, %d\n", err);
3184 goto err_unmap_free_uar;
3187 err = mlx5_core_alloc_transport_domain(mdev, &priv->tdn);
3189 mlx5_core_err(mdev, "alloc td failed, %d\n", err);
3190 goto err_dealloc_pd;
3193 err = mlx5e_create_mkey(priv, priv->pdn, &priv->mkey);
3195 mlx5_core_err(mdev, "create mkey failed, %d\n", err);
3196 goto err_dealloc_transport_domain;
3199 err = mlx5e_create_umr_mkey(priv);
3201 mlx5_core_err(mdev, "create umr mkey failed, %d\n", err);
3202 goto err_destroy_mkey;
3205 err = mlx5e_create_tises(priv);
3207 mlx5_core_warn(mdev, "create tises failed, %d\n", err);
3208 goto err_destroy_umr_mkey;
3211 err = mlx5e_open_drop_rq(priv);
3213 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
3214 goto err_destroy_tises;
3217 err = mlx5e_create_rqts(priv);
3219 mlx5_core_warn(mdev, "create rqts failed, %d\n", err);
3220 goto err_close_drop_rq;
3223 err = mlx5e_create_tirs(priv);
3225 mlx5_core_warn(mdev, "create tirs failed, %d\n", err);
3226 goto err_destroy_rqts;
3229 err = mlx5e_create_flow_steering(priv);
3231 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
3232 goto err_destroy_tirs;
3235 mlx5e_create_q_counter(priv);
3237 mlx5e_init_l2_addr(priv);
3239 mlx5e_vxlan_init(priv);
3241 err = mlx5e_tc_init(priv);
3243 goto err_dealloc_q_counters;
3245 #ifdef CONFIG_MLX5_CORE_EN_DCB
3246 mlx5e_dcbnl_ieee_setets_core(priv, &priv->params.ets);
3249 err = register_netdev(netdev);
3251 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
3252 goto err_tc_cleanup;
3255 if (mlx5e_vxlan_allowed(mdev)) {
3257 udp_tunnel_get_rx_info(netdev);
3261 mlx5e_enable_async_events(priv);
3262 queue_work(priv->wq, &priv->set_rx_mode_work);
3267 mlx5e_tc_cleanup(priv);
3269 err_dealloc_q_counters:
3270 mlx5e_destroy_q_counter(priv);
3271 mlx5e_destroy_flow_steering(priv);
3274 mlx5e_destroy_tirs(priv);
3277 mlx5e_destroy_rqts(priv);
3280 mlx5e_close_drop_rq(priv);
3283 mlx5e_destroy_tises(priv);
3285 err_destroy_umr_mkey:
3286 mlx5_core_destroy_mkey(mdev, &priv->umr_mkey);
3289 mlx5_core_destroy_mkey(mdev, &priv->mkey);
3291 err_dealloc_transport_domain:
3292 mlx5_core_dealloc_transport_domain(mdev, priv->tdn);
3295 mlx5_core_dealloc_pd(mdev, priv->pdn);
3298 mlx5_unmap_free_uar(mdev, &priv->cq_uar);
3301 destroy_workqueue(priv->wq);
3304 free_netdev(netdev);
3309 static void mlx5e_destroy_netdev(struct mlx5_core_dev *mdev, void *vpriv)
3311 struct mlx5e_priv *priv = vpriv;
3312 struct net_device *netdev = priv->netdev;
3314 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
3316 queue_work(priv->wq, &priv->set_rx_mode_work);
3317 mlx5e_disable_async_events(priv);
3318 flush_workqueue(priv->wq);
3319 if (test_bit(MLX5_INTERFACE_STATE_SHUTDOWN, &mdev->intf_state)) {
3320 netif_device_detach(netdev);
3321 mlx5e_close(netdev);
3323 unregister_netdev(netdev);
3326 mlx5e_tc_cleanup(priv);
3327 mlx5e_vxlan_cleanup(priv);
3328 mlx5e_destroy_q_counter(priv);
3329 mlx5e_destroy_flow_steering(priv);
3330 mlx5e_destroy_tirs(priv);
3331 mlx5e_destroy_rqts(priv);
3332 mlx5e_close_drop_rq(priv);
3333 mlx5e_destroy_tises(priv);
3334 mlx5_core_destroy_mkey(priv->mdev, &priv->umr_mkey);
3335 mlx5_core_destroy_mkey(priv->mdev, &priv->mkey);
3336 mlx5_core_dealloc_transport_domain(priv->mdev, priv->tdn);
3337 mlx5_core_dealloc_pd(priv->mdev, priv->pdn);
3338 mlx5_unmap_free_uar(priv->mdev, &priv->cq_uar);
3339 cancel_delayed_work_sync(&priv->update_stats_work);
3340 destroy_workqueue(priv->wq);
3342 if (!test_bit(MLX5_INTERFACE_STATE_SHUTDOWN, &mdev->intf_state))
3343 free_netdev(netdev);
3346 static void *mlx5e_get_netdev(void *vpriv)
3348 struct mlx5e_priv *priv = vpriv;
3350 return priv->netdev;
3353 static struct mlx5_interface mlx5e_interface = {
3354 .add = mlx5e_create_netdev,
3355 .remove = mlx5e_destroy_netdev,
3356 .event = mlx5e_async_event,
3357 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
3358 .get_dev = mlx5e_get_netdev,
3361 void mlx5e_init(void)
3363 mlx5_register_interface(&mlx5e_interface);
3366 void mlx5e_cleanup(void)
3368 mlx5_unregister_interface(&mlx5e_interface);