2 * Copyright (c) 2015-2016, Mellanox Technologies. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
33 #include <net/tc_act/tc_gact.h>
34 #include <net/pkt_cls.h>
35 #include <linux/mlx5/fs.h>
36 #include <net/vxlan.h>
42 struct mlx5e_rq_param {
43 u32 rqc[MLX5_ST_SZ_DW(rqc)];
44 struct mlx5_wq_param wq;
48 struct mlx5e_sq_param {
49 u32 sqc[MLX5_ST_SZ_DW(sqc)];
50 struct mlx5_wq_param wq;
56 struct mlx5e_cq_param {
57 u32 cqc[MLX5_ST_SZ_DW(cqc)];
58 struct mlx5_wq_param wq;
63 struct mlx5e_channel_param {
64 struct mlx5e_rq_param rq;
65 struct mlx5e_sq_param sq;
66 struct mlx5e_sq_param icosq;
67 struct mlx5e_cq_param rx_cq;
68 struct mlx5e_cq_param tx_cq;
69 struct mlx5e_cq_param icosq_cq;
72 static void mlx5e_update_carrier(struct mlx5e_priv *priv)
74 struct mlx5_core_dev *mdev = priv->mdev;
77 port_state = mlx5_query_vport_state(mdev,
78 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT, 0);
80 if (port_state == VPORT_STATE_UP) {
81 netdev_info(priv->netdev, "Link up\n");
82 netif_carrier_on(priv->netdev);
84 netdev_info(priv->netdev, "Link down\n");
85 netif_carrier_off(priv->netdev);
89 static void mlx5e_update_carrier_work(struct work_struct *work)
91 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
94 mutex_lock(&priv->state_lock);
95 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
96 mlx5e_update_carrier(priv);
97 mutex_unlock(&priv->state_lock);
100 static void mlx5e_tx_timeout_work(struct work_struct *work)
102 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
107 mutex_lock(&priv->state_lock);
108 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
110 mlx5e_close_locked(priv->netdev);
111 err = mlx5e_open_locked(priv->netdev);
113 netdev_err(priv->netdev, "mlx5e_open_locked failed recovering from a tx_timeout, err(%d).\n",
116 mutex_unlock(&priv->state_lock);
120 static void mlx5e_update_sw_counters(struct mlx5e_priv *priv)
122 struct mlx5e_sw_stats *s = &priv->stats.sw;
123 struct mlx5e_rq_stats *rq_stats;
124 struct mlx5e_sq_stats *sq_stats;
125 u64 tx_offload_none = 0;
128 memset(s, 0, sizeof(*s));
129 for (i = 0; i < priv->params.num_channels; i++) {
130 rq_stats = &priv->channel[i]->rq.stats;
132 s->rx_packets += rq_stats->packets;
133 s->rx_bytes += rq_stats->bytes;
134 s->rx_lro_packets += rq_stats->lro_packets;
135 s->rx_lro_bytes += rq_stats->lro_bytes;
136 s->rx_csum_none += rq_stats->csum_none;
137 s->rx_csum_complete += rq_stats->csum_complete;
138 s->rx_csum_unnecessary_inner += rq_stats->csum_unnecessary_inner;
139 s->rx_wqe_err += rq_stats->wqe_err;
140 s->rx_mpwqe_filler += rq_stats->mpwqe_filler;
141 s->rx_buff_alloc_err += rq_stats->buff_alloc_err;
142 s->rx_cqe_compress_blks += rq_stats->cqe_compress_blks;
143 s->rx_cqe_compress_pkts += rq_stats->cqe_compress_pkts;
145 for (j = 0; j < priv->params.num_tc; j++) {
146 sq_stats = &priv->channel[i]->sq[j].stats;
148 s->tx_packets += sq_stats->packets;
149 s->tx_bytes += sq_stats->bytes;
150 s->tx_tso_packets += sq_stats->tso_packets;
151 s->tx_tso_bytes += sq_stats->tso_bytes;
152 s->tx_tso_inner_packets += sq_stats->tso_inner_packets;
153 s->tx_tso_inner_bytes += sq_stats->tso_inner_bytes;
154 s->tx_queue_stopped += sq_stats->stopped;
155 s->tx_queue_wake += sq_stats->wake;
156 s->tx_queue_dropped += sq_stats->dropped;
157 s->tx_xmit_more += sq_stats->xmit_more;
158 s->tx_csum_partial_inner += sq_stats->csum_partial_inner;
159 tx_offload_none += sq_stats->csum_none;
163 /* Update calculated offload counters */
164 s->tx_csum_partial = s->tx_packets - tx_offload_none - s->tx_csum_partial_inner;
165 s->rx_csum_unnecessary = s->rx_packets - s->rx_csum_none - s->rx_csum_complete;
167 s->link_down_events_phy = MLX5_GET(ppcnt_reg,
168 priv->stats.pport.phy_counters,
169 counter_set.phys_layer_cntrs.link_down_events);
172 static void mlx5e_update_vport_counters(struct mlx5e_priv *priv)
174 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
175 u32 *out = (u32 *)priv->stats.vport.query_vport_out;
176 u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)] = {0};
177 struct mlx5_core_dev *mdev = priv->mdev;
179 MLX5_SET(query_vport_counter_in, in, opcode,
180 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
181 MLX5_SET(query_vport_counter_in, in, op_mod, 0);
182 MLX5_SET(query_vport_counter_in, in, other_vport, 0);
184 memset(out, 0, outlen);
185 mlx5_cmd_exec(mdev, in, sizeof(in), out, outlen);
188 static void mlx5e_update_pport_counters(struct mlx5e_priv *priv)
190 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
191 struct mlx5_core_dev *mdev = priv->mdev;
192 int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
197 in = mlx5_vzalloc(sz);
201 MLX5_SET(ppcnt_reg, in, local_port, 1);
203 out = pstats->IEEE_802_3_counters;
204 MLX5_SET(ppcnt_reg, in, grp, MLX5_IEEE_802_3_COUNTERS_GROUP);
205 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
207 out = pstats->RFC_2863_counters;
208 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2863_COUNTERS_GROUP);
209 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
211 out = pstats->RFC_2819_counters;
212 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2819_COUNTERS_GROUP);
213 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
215 out = pstats->phy_counters;
216 MLX5_SET(ppcnt_reg, in, grp, MLX5_PHYSICAL_LAYER_COUNTERS_GROUP);
217 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
219 MLX5_SET(ppcnt_reg, in, grp, MLX5_PER_PRIORITY_COUNTERS_GROUP);
220 for (prio = 0; prio < NUM_PPORT_PRIO; prio++) {
221 out = pstats->per_prio_counters[prio];
222 MLX5_SET(ppcnt_reg, in, prio_tc, prio);
223 mlx5_core_access_reg(mdev, in, sz, out, sz,
224 MLX5_REG_PPCNT, 0, 0);
231 static void mlx5e_update_q_counter(struct mlx5e_priv *priv)
233 struct mlx5e_qcounter_stats *qcnt = &priv->stats.qcnt;
235 if (!priv->q_counter)
238 mlx5_core_query_out_of_buffer(priv->mdev, priv->q_counter,
239 &qcnt->rx_out_of_buffer);
242 void mlx5e_update_stats(struct mlx5e_priv *priv)
244 mlx5e_update_q_counter(priv);
245 mlx5e_update_vport_counters(priv);
246 mlx5e_update_pport_counters(priv);
247 mlx5e_update_sw_counters(priv);
250 void mlx5e_update_stats_work(struct work_struct *work)
252 struct delayed_work *dwork = to_delayed_work(work);
253 struct mlx5e_priv *priv = container_of(dwork, struct mlx5e_priv,
255 mutex_lock(&priv->state_lock);
256 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
257 priv->profile->update_stats(priv);
258 queue_delayed_work(priv->wq, dwork,
259 msecs_to_jiffies(MLX5E_UPDATE_STATS_INTERVAL));
261 mutex_unlock(&priv->state_lock);
264 static void mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
265 enum mlx5_dev_event event, unsigned long param)
267 struct mlx5e_priv *priv = vpriv;
269 if (!test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state))
273 case MLX5_DEV_EVENT_PORT_UP:
274 case MLX5_DEV_EVENT_PORT_DOWN:
275 queue_work(priv->wq, &priv->update_carrier_work);
283 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
285 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
288 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
290 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
291 synchronize_irq(mlx5_get_msix_vec(priv->mdev, MLX5_EQ_VEC_ASYNC));
294 #define MLX5E_HW2SW_MTU(hwmtu) (hwmtu - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
295 #define MLX5E_SW2HW_MTU(swmtu) (swmtu + (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
297 static inline int mlx5e_get_wqe_mtt_sz(void)
299 /* UMR copies MTTs in units of MLX5_UMR_MTT_ALIGNMENT bytes.
300 * To avoid copying garbage after the mtt array, we allocate
303 return ALIGN(MLX5_MPWRQ_PAGES_PER_WQE * sizeof(__be64),
304 MLX5_UMR_MTT_ALIGNMENT);
307 static inline void mlx5e_build_umr_wqe(struct mlx5e_rq *rq, struct mlx5e_sq *sq,
308 struct mlx5e_umr_wqe *wqe, u16 ix)
310 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
311 struct mlx5_wqe_umr_ctrl_seg *ucseg = &wqe->uctrl;
312 struct mlx5_wqe_data_seg *dseg = &wqe->data;
313 struct mlx5e_mpw_info *wi = &rq->wqe_info[ix];
314 u8 ds_cnt = DIV_ROUND_UP(sizeof(*wqe), MLX5_SEND_WQE_DS);
315 u32 umr_wqe_mtt_offset = mlx5e_get_wqe_mtt_offset(rq, ix);
317 cseg->qpn_ds = cpu_to_be32((sq->sqn << MLX5_WQE_CTRL_QPN_SHIFT) |
319 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
320 cseg->imm = rq->mkey_be;
322 ucseg->flags = MLX5_UMR_TRANSLATION_OFFSET_EN;
323 ucseg->klm_octowords =
324 cpu_to_be16(MLX5_MTT_OCTW(MLX5_MPWRQ_PAGES_PER_WQE));
325 ucseg->bsf_octowords =
326 cpu_to_be16(MLX5_MTT_OCTW(umr_wqe_mtt_offset));
327 ucseg->mkey_mask = cpu_to_be64(MLX5_MKEY_MASK_FREE);
329 dseg->lkey = sq->mkey_be;
330 dseg->addr = cpu_to_be64(wi->umr.mtt_addr);
333 static int mlx5e_rq_alloc_mpwqe_info(struct mlx5e_rq *rq,
334 struct mlx5e_channel *c)
336 int wq_sz = mlx5_wq_ll_get_size(&rq->wq);
337 int mtt_sz = mlx5e_get_wqe_mtt_sz();
338 int mtt_alloc = mtt_sz + MLX5_UMR_ALIGN - 1;
341 rq->wqe_info = kzalloc_node(wq_sz * sizeof(*rq->wqe_info),
342 GFP_KERNEL, cpu_to_node(c->cpu));
346 /* We allocate more than mtt_sz as we will align the pointer */
347 rq->mtt_no_align = kzalloc_node(mtt_alloc * wq_sz, GFP_KERNEL,
348 cpu_to_node(c->cpu));
349 if (unlikely(!rq->mtt_no_align))
350 goto err_free_wqe_info;
352 for (i = 0; i < wq_sz; i++) {
353 struct mlx5e_mpw_info *wi = &rq->wqe_info[i];
355 wi->umr.mtt = PTR_ALIGN(rq->mtt_no_align + i * mtt_alloc,
357 wi->umr.mtt_addr = dma_map_single(c->pdev, wi->umr.mtt, mtt_sz,
359 if (unlikely(dma_mapping_error(c->pdev, wi->umr.mtt_addr)))
362 mlx5e_build_umr_wqe(rq, &c->icosq, &wi->umr.wqe, i);
369 struct mlx5e_mpw_info *wi = &rq->wqe_info[i];
371 dma_unmap_single(c->pdev, wi->umr.mtt_addr, mtt_sz,
374 kfree(rq->mtt_no_align);
382 static void mlx5e_rq_free_mpwqe_info(struct mlx5e_rq *rq)
384 int wq_sz = mlx5_wq_ll_get_size(&rq->wq);
385 int mtt_sz = mlx5e_get_wqe_mtt_sz();
388 for (i = 0; i < wq_sz; i++) {
389 struct mlx5e_mpw_info *wi = &rq->wqe_info[i];
391 dma_unmap_single(rq->pdev, wi->umr.mtt_addr, mtt_sz,
394 kfree(rq->mtt_no_align);
398 static int mlx5e_create_rq(struct mlx5e_channel *c,
399 struct mlx5e_rq_param *param,
402 struct mlx5e_priv *priv = c->priv;
403 struct mlx5_core_dev *mdev = priv->mdev;
404 void *rqc = param->rqc;
405 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
411 param->wq.db_numa_node = cpu_to_node(c->cpu);
413 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
418 rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
420 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
422 rq->wq_type = priv->params.rq_wq_type;
424 rq->netdev = c->netdev;
425 rq->tstamp = &priv->tstamp;
430 switch (priv->params.rq_wq_type) {
431 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
432 rq->handle_rx_cqe = mlx5e_handle_rx_cqe_mpwrq;
433 rq->alloc_wqe = mlx5e_alloc_rx_mpwqe;
434 rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
436 rq->mpwqe_mtt_offset = c->ix *
437 MLX5E_REQUIRED_MTTS(1, BIT(priv->params.log_rq_size));
439 rq->mpwqe_stride_sz = BIT(priv->params.mpwqe_log_stride_sz);
440 rq->mpwqe_num_strides = BIT(priv->params.mpwqe_log_num_strides);
441 rq->wqe_sz = rq->mpwqe_stride_sz * rq->mpwqe_num_strides;
442 byte_count = rq->wqe_sz;
443 rq->mkey_be = cpu_to_be32(c->priv->umr_mkey.key);
444 err = mlx5e_rq_alloc_mpwqe_info(rq, c);
446 goto err_rq_wq_destroy;
448 default: /* MLX5_WQ_TYPE_LINKED_LIST */
449 rq->skb = kzalloc_node(wq_sz * sizeof(*rq->skb), GFP_KERNEL,
450 cpu_to_node(c->cpu));
453 goto err_rq_wq_destroy;
455 rq->handle_rx_cqe = mlx5e_handle_rx_cqe;
456 rq->alloc_wqe = mlx5e_alloc_rx_wqe;
457 rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
459 rq->wqe_sz = (priv->params.lro_en) ?
460 priv->params.lro_wqe_sz :
461 MLX5E_SW2HW_MTU(priv->netdev->mtu);
462 rq->wqe_sz = SKB_DATA_ALIGN(rq->wqe_sz);
463 byte_count = rq->wqe_sz;
464 byte_count |= MLX5_HW_START_PADDING;
465 rq->mkey_be = c->mkey_be;
468 for (i = 0; i < wq_sz; i++) {
469 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
471 wqe->data.byte_count = cpu_to_be32(byte_count);
472 wqe->data.lkey = rq->mkey_be;
475 INIT_WORK(&rq->am.work, mlx5e_rx_am_work);
476 rq->am.mode = priv->params.rx_cq_period_mode;
481 mlx5_wq_destroy(&rq->wq_ctrl);
486 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
488 switch (rq->wq_type) {
489 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
490 mlx5e_rq_free_mpwqe_info(rq);
492 default: /* MLX5_WQ_TYPE_LINKED_LIST */
496 mlx5_wq_destroy(&rq->wq_ctrl);
499 static int mlx5e_enable_rq(struct mlx5e_rq *rq, struct mlx5e_rq_param *param)
501 struct mlx5e_priv *priv = rq->priv;
502 struct mlx5_core_dev *mdev = priv->mdev;
510 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
511 sizeof(u64) * rq->wq_ctrl.buf.npages;
512 in = mlx5_vzalloc(inlen);
516 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
517 wq = MLX5_ADDR_OF(rqc, rqc, wq);
519 memcpy(rqc, param->rqc, sizeof(param->rqc));
521 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
522 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
523 MLX5_SET(rqc, rqc, vsd, priv->params.vlan_strip_disable);
524 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
525 MLX5_ADAPTER_PAGE_SHIFT);
526 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
528 mlx5_fill_page_array(&rq->wq_ctrl.buf,
529 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
531 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
538 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
541 struct mlx5e_channel *c = rq->channel;
542 struct mlx5e_priv *priv = c->priv;
543 struct mlx5_core_dev *mdev = priv->mdev;
550 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
551 in = mlx5_vzalloc(inlen);
555 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
557 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
558 MLX5_SET(rqc, rqc, state, next_state);
560 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
567 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
569 struct mlx5e_channel *c = rq->channel;
570 struct mlx5e_priv *priv = c->priv;
571 struct mlx5_core_dev *mdev = priv->mdev;
578 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
579 in = mlx5_vzalloc(inlen);
583 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
585 MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
586 MLX5_SET64(modify_rq_in, in, modify_bitmask,
587 MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
588 MLX5_SET(rqc, rqc, vsd, vsd);
589 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
591 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
598 static void mlx5e_disable_rq(struct mlx5e_rq *rq)
600 mlx5_core_destroy_rq(rq->priv->mdev, rq->rqn);
603 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq)
605 unsigned long exp_time = jiffies + msecs_to_jiffies(20000);
606 struct mlx5e_channel *c = rq->channel;
607 struct mlx5e_priv *priv = c->priv;
608 struct mlx5_wq_ll *wq = &rq->wq;
610 while (time_before(jiffies, exp_time)) {
611 if (wq->cur_sz >= priv->params.min_rx_wqes)
620 static void mlx5e_free_rx_descs(struct mlx5e_rq *rq)
622 struct mlx5_wq_ll *wq = &rq->wq;
623 struct mlx5e_rx_wqe *wqe;
627 /* UMR WQE (if in progress) is always at wq->head */
628 if (test_bit(MLX5E_RQ_STATE_UMR_WQE_IN_PROGRESS, &rq->state))
629 mlx5e_free_rx_mpwqe(rq, &rq->wqe_info[wq->head]);
631 while (!mlx5_wq_ll_is_empty(wq)) {
632 wqe_ix_be = *wq->tail_next;
633 wqe_ix = be16_to_cpu(wqe_ix_be);
634 wqe = mlx5_wq_ll_get_wqe(&rq->wq, wqe_ix);
635 rq->dealloc_wqe(rq, wqe_ix);
636 mlx5_wq_ll_pop(&rq->wq, wqe_ix_be,
637 &wqe->next.next_wqe_index);
641 static int mlx5e_open_rq(struct mlx5e_channel *c,
642 struct mlx5e_rq_param *param,
645 struct mlx5e_sq *sq = &c->icosq;
646 u16 pi = sq->pc & sq->wq.sz_m1;
649 err = mlx5e_create_rq(c, param, rq);
653 err = mlx5e_enable_rq(rq, param);
657 err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
661 if (param->am_enabled)
662 set_bit(MLX5E_RQ_STATE_AM, &c->rq.state);
664 sq->ico_wqe_info[pi].opcode = MLX5_OPCODE_NOP;
665 sq->ico_wqe_info[pi].num_wqebbs = 1;
666 mlx5e_send_nop(sq, true); /* trigger mlx5e_post_rx_wqes() */
671 mlx5e_disable_rq(rq);
673 mlx5e_destroy_rq(rq);
678 static void mlx5e_close_rq(struct mlx5e_rq *rq)
680 set_bit(MLX5E_RQ_STATE_FLUSH, &rq->state);
681 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
682 cancel_work_sync(&rq->am.work);
684 mlx5e_disable_rq(rq);
685 mlx5e_free_rx_descs(rq);
686 mlx5e_destroy_rq(rq);
689 static void mlx5e_free_sq_db(struct mlx5e_sq *sq)
696 static int mlx5e_alloc_sq_db(struct mlx5e_sq *sq, int numa)
698 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
699 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
701 sq->skb = kzalloc_node(wq_sz * sizeof(*sq->skb), GFP_KERNEL, numa);
702 sq->dma_fifo = kzalloc_node(df_sz * sizeof(*sq->dma_fifo), GFP_KERNEL,
704 sq->wqe_info = kzalloc_node(wq_sz * sizeof(*sq->wqe_info), GFP_KERNEL,
707 if (!sq->skb || !sq->dma_fifo || !sq->wqe_info) {
708 mlx5e_free_sq_db(sq);
712 sq->dma_fifo_mask = df_sz - 1;
717 static int mlx5e_create_sq(struct mlx5e_channel *c,
719 struct mlx5e_sq_param *param,
722 struct mlx5e_priv *priv = c->priv;
723 struct mlx5_core_dev *mdev = priv->mdev;
725 void *sqc = param->sqc;
726 void *sqc_wq = MLX5_ADDR_OF(sqc, sqc, wq);
729 err = mlx5_alloc_map_uar(mdev, &sq->uar, !!MLX5_CAP_GEN(mdev, bf));
733 param->wq.db_numa_node = cpu_to_node(c->cpu);
735 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq,
738 goto err_unmap_free_uar;
740 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
741 if (sq->uar.bf_map) {
742 set_bit(MLX5E_SQ_STATE_BF_ENABLE, &sq->state);
743 sq->uar_map = sq->uar.bf_map;
745 sq->uar_map = sq->uar.map;
747 sq->bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
748 sq->max_inline = param->max_inline;
749 sq->min_inline_mode =
750 MLX5_CAP_ETH(mdev, wqe_inline_mode) == MLX5E_INLINE_MODE_VPORT_CONTEXT ?
751 param->min_inline_mode : 0;
753 err = mlx5e_alloc_sq_db(sq, cpu_to_node(c->cpu));
755 goto err_sq_wq_destroy;
758 u8 wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
760 sq->ico_wqe_info = kzalloc_node(sizeof(*sq->ico_wqe_info) *
763 cpu_to_node(c->cpu));
764 if (!sq->ico_wqe_info) {
771 txq_ix = c->ix + tc * priv->params.num_channels;
772 sq->txq = netdev_get_tx_queue(priv->netdev, txq_ix);
773 priv->txq_to_sq_map[txq_ix] = sq;
777 sq->tstamp = &priv->tstamp;
778 sq->mkey_be = c->mkey_be;
781 sq->edge = (sq->wq.sz_m1 + 1) - MLX5_SEND_WQE_MAX_WQEBBS;
782 sq->bf_budget = MLX5E_SQ_BF_BUDGET;
787 mlx5e_free_sq_db(sq);
790 mlx5_wq_destroy(&sq->wq_ctrl);
793 mlx5_unmap_free_uar(mdev, &sq->uar);
798 static void mlx5e_destroy_sq(struct mlx5e_sq *sq)
800 struct mlx5e_channel *c = sq->channel;
801 struct mlx5e_priv *priv = c->priv;
803 kfree(sq->ico_wqe_info);
804 mlx5e_free_sq_db(sq);
805 mlx5_wq_destroy(&sq->wq_ctrl);
806 mlx5_unmap_free_uar(priv->mdev, &sq->uar);
809 static int mlx5e_enable_sq(struct mlx5e_sq *sq, struct mlx5e_sq_param *param)
811 struct mlx5e_channel *c = sq->channel;
812 struct mlx5e_priv *priv = c->priv;
813 struct mlx5_core_dev *mdev = priv->mdev;
821 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
822 sizeof(u64) * sq->wq_ctrl.buf.npages;
823 in = mlx5_vzalloc(inlen);
827 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
828 wq = MLX5_ADDR_OF(sqc, sqc, wq);
830 memcpy(sqc, param->sqc, sizeof(param->sqc));
832 MLX5_SET(sqc, sqc, tis_num_0, param->icosq ? 0 : priv->tisn[sq->tc]);
833 MLX5_SET(sqc, sqc, cqn, sq->cq.mcq.cqn);
834 MLX5_SET(sqc, sqc, min_wqe_inline_mode, sq->min_inline_mode);
835 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
836 MLX5_SET(sqc, sqc, tis_lst_sz, param->icosq ? 0 : 1);
837 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
839 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
840 MLX5_SET(wq, wq, uar_page, sq->uar.index);
841 MLX5_SET(wq, wq, log_wq_pg_sz, sq->wq_ctrl.buf.page_shift -
842 MLX5_ADAPTER_PAGE_SHIFT);
843 MLX5_SET64(wq, wq, dbr_addr, sq->wq_ctrl.db.dma);
845 mlx5_fill_page_array(&sq->wq_ctrl.buf,
846 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
848 err = mlx5_core_create_sq(mdev, in, inlen, &sq->sqn);
855 static int mlx5e_modify_sq(struct mlx5e_sq *sq, int curr_state,
856 int next_state, bool update_rl, int rl_index)
858 struct mlx5e_channel *c = sq->channel;
859 struct mlx5e_priv *priv = c->priv;
860 struct mlx5_core_dev *mdev = priv->mdev;
867 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
868 in = mlx5_vzalloc(inlen);
872 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
874 MLX5_SET(modify_sq_in, in, sq_state, curr_state);
875 MLX5_SET(sqc, sqc, state, next_state);
876 if (update_rl && next_state == MLX5_SQC_STATE_RDY) {
877 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
878 MLX5_SET(sqc, sqc, packet_pacing_rate_limit_index, rl_index);
881 err = mlx5_core_modify_sq(mdev, sq->sqn, in, inlen);
888 static void mlx5e_disable_sq(struct mlx5e_sq *sq)
890 struct mlx5e_channel *c = sq->channel;
891 struct mlx5e_priv *priv = c->priv;
892 struct mlx5_core_dev *mdev = priv->mdev;
894 mlx5_core_destroy_sq(mdev, sq->sqn);
896 mlx5_rl_remove_rate(mdev, sq->rate_limit);
899 static int mlx5e_open_sq(struct mlx5e_channel *c,
901 struct mlx5e_sq_param *param,
906 err = mlx5e_create_sq(c, tc, param, sq);
910 err = mlx5e_enable_sq(sq, param);
914 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST, MLX5_SQC_STATE_RDY,
920 netdev_tx_reset_queue(sq->txq);
921 netif_tx_start_queue(sq->txq);
927 mlx5e_disable_sq(sq);
929 mlx5e_destroy_sq(sq);
934 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
936 __netif_tx_lock_bh(txq);
937 netif_tx_stop_queue(txq);
938 __netif_tx_unlock_bh(txq);
941 static void mlx5e_close_sq(struct mlx5e_sq *sq)
943 set_bit(MLX5E_SQ_STATE_FLUSH, &sq->state);
944 /* prevent netif_tx_wake_queue */
945 napi_synchronize(&sq->channel->napi);
948 netif_tx_disable_queue(sq->txq);
950 /* last doorbell out, godspeed .. */
951 if (mlx5e_sq_has_room_for(sq, 1))
952 mlx5e_send_nop(sq, true);
955 mlx5e_disable_sq(sq);
956 mlx5e_free_tx_descs(sq);
957 mlx5e_destroy_sq(sq);
960 static int mlx5e_create_cq(struct mlx5e_channel *c,
961 struct mlx5e_cq_param *param,
964 struct mlx5e_priv *priv = c->priv;
965 struct mlx5_core_dev *mdev = priv->mdev;
966 struct mlx5_core_cq *mcq = &cq->mcq;
972 param->wq.buf_numa_node = cpu_to_node(c->cpu);
973 param->wq.db_numa_node = cpu_to_node(c->cpu);
974 param->eq_ix = c->ix;
976 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
981 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
986 mcq->set_ci_db = cq->wq_ctrl.db.db;
987 mcq->arm_db = cq->wq_ctrl.db.db + 1;
990 mcq->vector = param->eq_ix;
991 mcq->comp = mlx5e_completion_event;
992 mcq->event = mlx5e_cq_error_event;
994 mcq->uar = &mdev->mlx5e_res.cq_uar;
996 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
997 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1008 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
1010 mlx5_wq_destroy(&cq->wq_ctrl);
1013 static int mlx5e_enable_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
1015 struct mlx5e_priv *priv = cq->priv;
1016 struct mlx5_core_dev *mdev = priv->mdev;
1017 struct mlx5_core_cq *mcq = &cq->mcq;
1022 unsigned int irqn_not_used;
1026 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1027 sizeof(u64) * cq->wq_ctrl.buf.npages;
1028 in = mlx5_vzalloc(inlen);
1032 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1034 memcpy(cqc, param->cqc, sizeof(param->cqc));
1036 mlx5_fill_page_array(&cq->wq_ctrl.buf,
1037 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
1039 mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
1041 MLX5_SET(cqc, cqc, cq_period_mode, param->cq_period_mode);
1042 MLX5_SET(cqc, cqc, c_eqn, eqn);
1043 MLX5_SET(cqc, cqc, uar_page, mcq->uar->index);
1044 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
1045 MLX5_ADAPTER_PAGE_SHIFT);
1046 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
1048 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
1060 static void mlx5e_disable_cq(struct mlx5e_cq *cq)
1062 struct mlx5e_priv *priv = cq->priv;
1063 struct mlx5_core_dev *mdev = priv->mdev;
1065 mlx5_core_destroy_cq(mdev, &cq->mcq);
1068 static int mlx5e_open_cq(struct mlx5e_channel *c,
1069 struct mlx5e_cq_param *param,
1070 struct mlx5e_cq *cq,
1071 struct mlx5e_cq_moder moderation)
1074 struct mlx5e_priv *priv = c->priv;
1075 struct mlx5_core_dev *mdev = priv->mdev;
1077 err = mlx5e_create_cq(c, param, cq);
1081 err = mlx5e_enable_cq(cq, param);
1083 goto err_destroy_cq;
1085 if (MLX5_CAP_GEN(mdev, cq_moderation))
1086 mlx5_core_modify_cq_moderation(mdev, &cq->mcq,
1092 mlx5e_destroy_cq(cq);
1097 static void mlx5e_close_cq(struct mlx5e_cq *cq)
1099 mlx5e_disable_cq(cq);
1100 mlx5e_destroy_cq(cq);
1103 static int mlx5e_get_cpu(struct mlx5e_priv *priv, int ix)
1105 return cpumask_first(priv->mdev->priv.irq_info[ix].mask);
1108 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1109 struct mlx5e_channel_param *cparam)
1111 struct mlx5e_priv *priv = c->priv;
1115 for (tc = 0; tc < c->num_tc; tc++) {
1116 err = mlx5e_open_cq(c, &cparam->tx_cq, &c->sq[tc].cq,
1117 priv->params.tx_cq_moderation);
1119 goto err_close_tx_cqs;
1125 for (tc--; tc >= 0; tc--)
1126 mlx5e_close_cq(&c->sq[tc].cq);
1131 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1135 for (tc = 0; tc < c->num_tc; tc++)
1136 mlx5e_close_cq(&c->sq[tc].cq);
1139 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1140 struct mlx5e_channel_param *cparam)
1145 for (tc = 0; tc < c->num_tc; tc++) {
1146 err = mlx5e_open_sq(c, tc, &cparam->sq, &c->sq[tc]);
1154 for (tc--; tc >= 0; tc--)
1155 mlx5e_close_sq(&c->sq[tc]);
1160 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1164 for (tc = 0; tc < c->num_tc; tc++)
1165 mlx5e_close_sq(&c->sq[tc]);
1168 static void mlx5e_build_channeltc_to_txq_map(struct mlx5e_priv *priv, int ix)
1172 for (i = 0; i < priv->profile->max_tc; i++)
1173 priv->channeltc_to_txq_map[ix][i] =
1174 ix + i * priv->params.num_channels;
1177 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1178 struct mlx5e_sq *sq, u32 rate)
1180 struct mlx5e_priv *priv = netdev_priv(dev);
1181 struct mlx5_core_dev *mdev = priv->mdev;
1185 if (rate == sq->rate_limit)
1190 /* remove current rl index to free space to next ones */
1191 mlx5_rl_remove_rate(mdev, sq->rate_limit);
1196 err = mlx5_rl_add_rate(mdev, rate, &rl_index);
1198 netdev_err(dev, "Failed configuring rate %u: %d\n",
1204 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY,
1205 MLX5_SQC_STATE_RDY, true, rl_index);
1207 netdev_err(dev, "Failed configuring rate %u: %d\n",
1209 /* remove the rate from the table */
1211 mlx5_rl_remove_rate(mdev, rate);
1215 sq->rate_limit = rate;
1219 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1221 struct mlx5e_priv *priv = netdev_priv(dev);
1222 struct mlx5_core_dev *mdev = priv->mdev;
1223 struct mlx5e_sq *sq = priv->txq_to_sq_map[index];
1226 if (!mlx5_rl_is_supported(mdev)) {
1227 netdev_err(dev, "Rate limiting is not supported on this device\n");
1231 /* rate is given in Mb/sec, HW config is in Kb/sec */
1234 /* Check whether rate in valid range, 0 is always valid */
1235 if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1236 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1240 mutex_lock(&priv->state_lock);
1241 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1242 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1244 priv->tx_rates[index] = rate;
1245 mutex_unlock(&priv->state_lock);
1250 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1251 struct mlx5e_channel_param *cparam,
1252 struct mlx5e_channel **cp)
1254 struct mlx5e_cq_moder icosq_cq_moder = {0, 0};
1255 struct net_device *netdev = priv->netdev;
1256 struct mlx5e_cq_moder rx_cq_profile;
1257 int cpu = mlx5e_get_cpu(priv, ix);
1258 struct mlx5e_channel *c;
1259 struct mlx5e_sq *sq;
1263 c = kzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1270 c->pdev = &priv->mdev->pdev->dev;
1271 c->netdev = priv->netdev;
1272 c->mkey_be = cpu_to_be32(priv->mdev->mlx5e_res.mkey.key);
1273 c->num_tc = priv->params.num_tc;
1275 if (priv->params.rx_am_enabled)
1276 rx_cq_profile = mlx5e_am_get_def_profile(priv->params.rx_cq_period_mode);
1278 rx_cq_profile = priv->params.rx_cq_moderation;
1280 mlx5e_build_channeltc_to_txq_map(priv, ix);
1282 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1284 err = mlx5e_open_cq(c, &cparam->icosq_cq, &c->icosq.cq, icosq_cq_moder);
1288 err = mlx5e_open_tx_cqs(c, cparam);
1290 goto err_close_icosq_cq;
1292 err = mlx5e_open_cq(c, &cparam->rx_cq, &c->rq.cq,
1295 goto err_close_tx_cqs;
1297 napi_enable(&c->napi);
1299 err = mlx5e_open_sq(c, 0, &cparam->icosq, &c->icosq);
1301 goto err_disable_napi;
1303 err = mlx5e_open_sqs(c, cparam);
1305 goto err_close_icosq;
1307 for (i = 0; i < priv->params.num_tc; i++) {
1308 u32 txq_ix = priv->channeltc_to_txq_map[ix][i];
1310 if (priv->tx_rates[txq_ix]) {
1311 sq = priv->txq_to_sq_map[txq_ix];
1312 mlx5e_set_sq_maxrate(priv->netdev, sq,
1313 priv->tx_rates[txq_ix]);
1317 err = mlx5e_open_rq(c, &cparam->rq, &c->rq);
1321 netif_set_xps_queue(netdev, get_cpu_mask(c->cpu), ix);
1330 mlx5e_close_sq(&c->icosq);
1333 napi_disable(&c->napi);
1334 mlx5e_close_cq(&c->rq.cq);
1337 mlx5e_close_tx_cqs(c);
1340 mlx5e_close_cq(&c->icosq.cq);
1343 netif_napi_del(&c->napi);
1344 napi_hash_del(&c->napi);
1350 static void mlx5e_close_channel(struct mlx5e_channel *c)
1352 mlx5e_close_rq(&c->rq);
1354 mlx5e_close_sq(&c->icosq);
1355 napi_disable(&c->napi);
1356 mlx5e_close_cq(&c->rq.cq);
1357 mlx5e_close_tx_cqs(c);
1358 mlx5e_close_cq(&c->icosq.cq);
1359 netif_napi_del(&c->napi);
1361 napi_hash_del(&c->napi);
1367 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
1368 struct mlx5e_rq_param *param)
1370 void *rqc = param->rqc;
1371 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1373 switch (priv->params.rq_wq_type) {
1374 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1375 MLX5_SET(wq, wq, log_wqe_num_of_strides,
1376 priv->params.mpwqe_log_num_strides - 9);
1377 MLX5_SET(wq, wq, log_wqe_stride_size,
1378 priv->params.mpwqe_log_stride_sz - 6);
1379 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ);
1381 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1382 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1385 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1386 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1387 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_rq_size);
1388 MLX5_SET(wq, wq, pd, priv->mdev->mlx5e_res.pdn);
1389 MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
1391 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1392 param->wq.linear = 1;
1394 param->am_enabled = priv->params.rx_am_enabled;
1397 static void mlx5e_build_drop_rq_param(struct mlx5e_rq_param *param)
1399 void *rqc = param->rqc;
1400 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1402 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1403 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1406 static void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
1407 struct mlx5e_sq_param *param)
1409 void *sqc = param->sqc;
1410 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1412 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1413 MLX5_SET(wq, wq, pd, priv->mdev->mlx5e_res.pdn);
1415 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1418 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
1419 struct mlx5e_sq_param *param)
1421 void *sqc = param->sqc;
1422 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1424 mlx5e_build_sq_param_common(priv, param);
1425 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_sq_size);
1427 param->max_inline = priv->params.tx_max_inline;
1428 param->min_inline_mode = priv->params.tx_min_inline_mode;
1431 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1432 struct mlx5e_cq_param *param)
1434 void *cqc = param->cqc;
1436 MLX5_SET(cqc, cqc, uar_page, priv->mdev->mlx5e_res.cq_uar.index);
1439 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
1440 struct mlx5e_cq_param *param)
1442 void *cqc = param->cqc;
1445 switch (priv->params.rq_wq_type) {
1446 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1447 log_cq_size = priv->params.log_rq_size +
1448 priv->params.mpwqe_log_num_strides;
1450 default: /* MLX5_WQ_TYPE_LINKED_LIST */
1451 log_cq_size = priv->params.log_rq_size;
1454 MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
1455 if (priv->params.rx_cqe_compress) {
1456 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
1457 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
1460 mlx5e_build_common_cq_param(priv, param);
1462 param->cq_period_mode = priv->params.rx_cq_period_mode;
1465 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
1466 struct mlx5e_cq_param *param)
1468 void *cqc = param->cqc;
1470 MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_sq_size);
1472 mlx5e_build_common_cq_param(priv, param);
1474 param->cq_period_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1477 static void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
1478 struct mlx5e_cq_param *param,
1481 void *cqc = param->cqc;
1483 MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
1485 mlx5e_build_common_cq_param(priv, param);
1487 param->cq_period_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1490 static void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
1491 struct mlx5e_sq_param *param,
1494 void *sqc = param->sqc;
1495 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1497 mlx5e_build_sq_param_common(priv, param);
1499 MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
1500 MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
1502 param->icosq = true;
1505 static void mlx5e_build_channel_param(struct mlx5e_priv *priv, struct mlx5e_channel_param *cparam)
1507 u8 icosq_log_wq_sz = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
1509 mlx5e_build_rq_param(priv, &cparam->rq);
1510 mlx5e_build_sq_param(priv, &cparam->sq);
1511 mlx5e_build_icosq_param(priv, &cparam->icosq, icosq_log_wq_sz);
1512 mlx5e_build_rx_cq_param(priv, &cparam->rx_cq);
1513 mlx5e_build_tx_cq_param(priv, &cparam->tx_cq);
1514 mlx5e_build_ico_cq_param(priv, &cparam->icosq_cq, icosq_log_wq_sz);
1517 static int mlx5e_open_channels(struct mlx5e_priv *priv)
1519 struct mlx5e_channel_param *cparam;
1520 int nch = priv->params.num_channels;
1525 priv->channel = kcalloc(nch, sizeof(struct mlx5e_channel *),
1528 priv->txq_to_sq_map = kcalloc(nch * priv->params.num_tc,
1529 sizeof(struct mlx5e_sq *), GFP_KERNEL);
1531 cparam = kzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
1533 if (!priv->channel || !priv->txq_to_sq_map || !cparam)
1534 goto err_free_txq_to_sq_map;
1536 mlx5e_build_channel_param(priv, cparam);
1538 for (i = 0; i < nch; i++) {
1539 err = mlx5e_open_channel(priv, i, cparam, &priv->channel[i]);
1541 goto err_close_channels;
1544 for (j = 0; j < nch; j++) {
1545 err = mlx5e_wait_for_min_rx_wqes(&priv->channel[j]->rq);
1547 goto err_close_channels;
1550 /* FIXME: This is a W/A for tx timeout watch dog false alarm when
1551 * polling for inactive tx queues.
1553 netif_tx_start_all_queues(priv->netdev);
1559 for (i--; i >= 0; i--)
1560 mlx5e_close_channel(priv->channel[i]);
1562 err_free_txq_to_sq_map:
1563 kfree(priv->txq_to_sq_map);
1564 kfree(priv->channel);
1570 static void mlx5e_close_channels(struct mlx5e_priv *priv)
1574 /* FIXME: This is a W/A only for tx timeout watch dog false alarm when
1575 * polling for inactive tx queues.
1577 netif_tx_stop_all_queues(priv->netdev);
1578 netif_tx_disable(priv->netdev);
1580 for (i = 0; i < priv->params.num_channels; i++)
1581 mlx5e_close_channel(priv->channel[i]);
1583 kfree(priv->txq_to_sq_map);
1584 kfree(priv->channel);
1587 static int mlx5e_rx_hash_fn(int hfunc)
1589 return (hfunc == ETH_RSS_HASH_TOP) ?
1590 MLX5_RX_HASH_FN_TOEPLITZ :
1591 MLX5_RX_HASH_FN_INVERTED_XOR8;
1594 static int mlx5e_bits_invert(unsigned long a, int size)
1599 for (i = 0; i < size; i++)
1600 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
1605 static void mlx5e_fill_indir_rqt_rqns(struct mlx5e_priv *priv, void *rqtc)
1609 for (i = 0; i < MLX5E_INDIR_RQT_SIZE; i++) {
1613 if (priv->params.rss_hfunc == ETH_RSS_HASH_XOR)
1614 ix = mlx5e_bits_invert(i, MLX5E_LOG_INDIR_RQT_SIZE);
1616 ix = priv->params.indirection_rqt[ix];
1617 rqn = test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1618 priv->channel[ix]->rq.rqn :
1620 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
1624 static void mlx5e_fill_direct_rqt_rqn(struct mlx5e_priv *priv, void *rqtc,
1627 u32 rqn = test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1628 priv->channel[ix]->rq.rqn :
1631 MLX5_SET(rqtc, rqtc, rq_num[0], rqn);
1634 static int mlx5e_create_rqt(struct mlx5e_priv *priv, int sz,
1635 int ix, struct mlx5e_rqt *rqt)
1637 struct mlx5_core_dev *mdev = priv->mdev;
1643 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
1644 in = mlx5_vzalloc(inlen);
1648 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
1650 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1651 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
1653 if (sz > 1) /* RSS */
1654 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1656 mlx5e_fill_direct_rqt_rqn(priv, rqtc, ix);
1658 err = mlx5_core_create_rqt(mdev, in, inlen, &rqt->rqtn);
1660 rqt->enabled = true;
1666 void mlx5e_destroy_rqt(struct mlx5e_priv *priv, struct mlx5e_rqt *rqt)
1668 rqt->enabled = false;
1669 mlx5_core_destroy_rqt(priv->mdev, rqt->rqtn);
1672 static int mlx5e_create_indirect_rqts(struct mlx5e_priv *priv)
1674 struct mlx5e_rqt *rqt = &priv->indir_rqt;
1676 return mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, 0, rqt);
1679 int mlx5e_create_direct_rqts(struct mlx5e_priv *priv)
1681 struct mlx5e_rqt *rqt;
1685 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
1686 rqt = &priv->direct_tir[ix].rqt;
1687 err = mlx5e_create_rqt(priv, 1 /*size */, ix, rqt);
1689 goto err_destroy_rqts;
1695 for (ix--; ix >= 0; ix--)
1696 mlx5e_destroy_rqt(priv, &priv->direct_tir[ix].rqt);
1701 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz, int ix)
1703 struct mlx5_core_dev *mdev = priv->mdev;
1709 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
1710 in = mlx5_vzalloc(inlen);
1714 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
1716 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1717 if (sz > 1) /* RSS */
1718 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1720 mlx5e_fill_direct_rqt_rqn(priv, rqtc, ix);
1722 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
1724 err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
1731 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv)
1736 if (priv->indir_rqt.enabled) {
1737 rqtn = priv->indir_rqt.rqtn;
1738 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, 0);
1741 for (ix = 0; ix < priv->params.num_channels; ix++) {
1742 if (!priv->direct_tir[ix].rqt.enabled)
1744 rqtn = priv->direct_tir[ix].rqt.rqtn;
1745 mlx5e_redirect_rqt(priv, rqtn, 1, ix);
1749 static void mlx5e_build_tir_ctx_lro(void *tirc, struct mlx5e_priv *priv)
1751 if (!priv->params.lro_en)
1754 #define ROUGH_MAX_L2_L3_HDR_SZ 256
1756 MLX5_SET(tirc, tirc, lro_enable_mask,
1757 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
1758 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
1759 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
1760 (priv->params.lro_wqe_sz -
1761 ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
1762 MLX5_SET(tirc, tirc, lro_timeout_period_usecs,
1763 MLX5_CAP_ETH(priv->mdev,
1764 lro_timer_supported_periods[2]));
1767 void mlx5e_build_tir_ctx_hash(void *tirc, struct mlx5e_priv *priv)
1769 MLX5_SET(tirc, tirc, rx_hash_fn,
1770 mlx5e_rx_hash_fn(priv->params.rss_hfunc));
1771 if (priv->params.rss_hfunc == ETH_RSS_HASH_TOP) {
1772 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
1773 rx_hash_toeplitz_key);
1774 size_t len = MLX5_FLD_SZ_BYTES(tirc,
1775 rx_hash_toeplitz_key);
1777 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1778 memcpy(rss_key, priv->params.toeplitz_hash_key, len);
1782 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
1784 struct mlx5_core_dev *mdev = priv->mdev;
1793 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
1794 in = mlx5_vzalloc(inlen);
1798 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
1799 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
1801 mlx5e_build_tir_ctx_lro(tirc, priv);
1803 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
1804 err = mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in,
1810 for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
1811 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
1823 static int mlx5e_set_mtu(struct mlx5e_priv *priv, u16 mtu)
1825 struct mlx5_core_dev *mdev = priv->mdev;
1826 u16 hw_mtu = MLX5E_SW2HW_MTU(mtu);
1829 err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
1833 /* Update vport context MTU */
1834 mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
1838 static void mlx5e_query_mtu(struct mlx5e_priv *priv, u16 *mtu)
1840 struct mlx5_core_dev *mdev = priv->mdev;
1844 err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
1845 if (err || !hw_mtu) /* fallback to port oper mtu */
1846 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
1848 *mtu = MLX5E_HW2SW_MTU(hw_mtu);
1851 static int mlx5e_set_dev_port_mtu(struct net_device *netdev)
1853 struct mlx5e_priv *priv = netdev_priv(netdev);
1857 err = mlx5e_set_mtu(priv, netdev->mtu);
1861 mlx5e_query_mtu(priv, &mtu);
1862 if (mtu != netdev->mtu)
1863 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
1864 __func__, mtu, netdev->mtu);
1870 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
1872 struct mlx5e_priv *priv = netdev_priv(netdev);
1873 int nch = priv->params.num_channels;
1874 int ntc = priv->params.num_tc;
1877 netdev_reset_tc(netdev);
1882 netdev_set_num_tc(netdev, ntc);
1884 /* Map netdev TCs to offset 0
1885 * We have our own UP to TXQ mapping for QoS
1887 for (tc = 0; tc < ntc; tc++)
1888 netdev_set_tc_queue(netdev, tc, nch, 0);
1891 int mlx5e_open_locked(struct net_device *netdev)
1893 struct mlx5e_priv *priv = netdev_priv(netdev);
1894 struct mlx5_core_dev *mdev = priv->mdev;
1898 set_bit(MLX5E_STATE_OPENED, &priv->state);
1900 mlx5e_netdev_set_tcs(netdev);
1902 num_txqs = priv->params.num_channels * priv->params.num_tc;
1903 netif_set_real_num_tx_queues(netdev, num_txqs);
1904 netif_set_real_num_rx_queues(netdev, priv->params.num_channels);
1906 err = mlx5e_open_channels(priv);
1908 netdev_err(netdev, "%s: mlx5e_open_channels failed, %d\n",
1910 goto err_clear_state_opened_flag;
1913 err = mlx5e_refresh_tirs_self_loopback_enable(priv->mdev);
1915 netdev_err(netdev, "%s: mlx5e_refresh_tirs_self_loopback_enable failed, %d\n",
1917 goto err_close_channels;
1920 mlx5e_redirect_rqts(priv);
1921 mlx5e_update_carrier(priv);
1922 mlx5e_timestamp_init(priv);
1923 #ifdef CONFIG_RFS_ACCEL
1924 priv->netdev->rx_cpu_rmap = priv->mdev->rmap;
1926 if (priv->profile->update_stats)
1927 queue_delayed_work(priv->wq, &priv->update_stats_work, 0);
1929 if (MLX5_CAP_GEN(mdev, vport_group_manager)) {
1930 err = mlx5e_add_sqs_fwd_rules(priv);
1932 goto err_close_channels;
1937 mlx5e_close_channels(priv);
1938 err_clear_state_opened_flag:
1939 clear_bit(MLX5E_STATE_OPENED, &priv->state);
1943 int mlx5e_open(struct net_device *netdev)
1945 struct mlx5e_priv *priv = netdev_priv(netdev);
1948 mutex_lock(&priv->state_lock);
1949 err = mlx5e_open_locked(netdev);
1950 mutex_unlock(&priv->state_lock);
1955 int mlx5e_close_locked(struct net_device *netdev)
1957 struct mlx5e_priv *priv = netdev_priv(netdev);
1958 struct mlx5_core_dev *mdev = priv->mdev;
1960 /* May already be CLOSED in case a previous configuration operation
1961 * (e.g RX/TX queue size change) that involves close&open failed.
1963 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
1966 clear_bit(MLX5E_STATE_OPENED, &priv->state);
1968 if (MLX5_CAP_GEN(mdev, vport_group_manager))
1969 mlx5e_remove_sqs_fwd_rules(priv);
1971 mlx5e_timestamp_cleanup(priv);
1972 netif_carrier_off(priv->netdev);
1973 mlx5e_redirect_rqts(priv);
1974 mlx5e_close_channels(priv);
1979 int mlx5e_close(struct net_device *netdev)
1981 struct mlx5e_priv *priv = netdev_priv(netdev);
1984 if (!netif_device_present(netdev))
1987 mutex_lock(&priv->state_lock);
1988 err = mlx5e_close_locked(netdev);
1989 mutex_unlock(&priv->state_lock);
1994 static int mlx5e_create_drop_rq(struct mlx5e_priv *priv,
1995 struct mlx5e_rq *rq,
1996 struct mlx5e_rq_param *param)
1998 struct mlx5_core_dev *mdev = priv->mdev;
1999 void *rqc = param->rqc;
2000 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
2003 param->wq.db_numa_node = param->wq.buf_numa_node;
2005 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
2015 static int mlx5e_create_drop_cq(struct mlx5e_priv *priv,
2016 struct mlx5e_cq *cq,
2017 struct mlx5e_cq_param *param)
2019 struct mlx5_core_dev *mdev = priv->mdev;
2020 struct mlx5_core_cq *mcq = &cq->mcq;
2025 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
2030 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
2033 mcq->set_ci_db = cq->wq_ctrl.db.db;
2034 mcq->arm_db = cq->wq_ctrl.db.db + 1;
2035 *mcq->set_ci_db = 0;
2037 mcq->vector = param->eq_ix;
2038 mcq->comp = mlx5e_completion_event;
2039 mcq->event = mlx5e_cq_error_event;
2041 mcq->uar = &mdev->mlx5e_res.cq_uar;
2048 static int mlx5e_open_drop_rq(struct mlx5e_priv *priv)
2050 struct mlx5e_cq_param cq_param;
2051 struct mlx5e_rq_param rq_param;
2052 struct mlx5e_rq *rq = &priv->drop_rq;
2053 struct mlx5e_cq *cq = &priv->drop_rq.cq;
2056 memset(&cq_param, 0, sizeof(cq_param));
2057 memset(&rq_param, 0, sizeof(rq_param));
2058 mlx5e_build_drop_rq_param(&rq_param);
2060 err = mlx5e_create_drop_cq(priv, cq, &cq_param);
2064 err = mlx5e_enable_cq(cq, &cq_param);
2066 goto err_destroy_cq;
2068 err = mlx5e_create_drop_rq(priv, rq, &rq_param);
2070 goto err_disable_cq;
2072 err = mlx5e_enable_rq(rq, &rq_param);
2074 goto err_destroy_rq;
2079 mlx5e_destroy_rq(&priv->drop_rq);
2082 mlx5e_disable_cq(&priv->drop_rq.cq);
2085 mlx5e_destroy_cq(&priv->drop_rq.cq);
2090 static void mlx5e_close_drop_rq(struct mlx5e_priv *priv)
2092 mlx5e_disable_rq(&priv->drop_rq);
2093 mlx5e_destroy_rq(&priv->drop_rq);
2094 mlx5e_disable_cq(&priv->drop_rq.cq);
2095 mlx5e_destroy_cq(&priv->drop_rq.cq);
2098 static int mlx5e_create_tis(struct mlx5e_priv *priv, int tc)
2100 struct mlx5_core_dev *mdev = priv->mdev;
2101 u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
2102 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
2104 MLX5_SET(tisc, tisc, prio, tc << 1);
2105 MLX5_SET(tisc, tisc, transport_domain, mdev->mlx5e_res.td.tdn);
2107 if (mlx5_lag_is_lacp_owner(mdev))
2108 MLX5_SET(tisc, tisc, strict_lag_tx_port_affinity, 1);
2110 return mlx5_core_create_tis(mdev, in, sizeof(in), &priv->tisn[tc]);
2113 static void mlx5e_destroy_tis(struct mlx5e_priv *priv, int tc)
2115 mlx5_core_destroy_tis(priv->mdev, priv->tisn[tc]);
2118 int mlx5e_create_tises(struct mlx5e_priv *priv)
2123 for (tc = 0; tc < priv->profile->max_tc; tc++) {
2124 err = mlx5e_create_tis(priv, tc);
2126 goto err_close_tises;
2132 for (tc--; tc >= 0; tc--)
2133 mlx5e_destroy_tis(priv, tc);
2138 void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv)
2142 for (tc = 0; tc < priv->profile->max_tc; tc++)
2143 mlx5e_destroy_tis(priv, tc);
2146 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv, u32 *tirc,
2147 enum mlx5e_traffic_types tt)
2149 void *hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2151 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2153 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
2154 MLX5_HASH_FIELD_SEL_DST_IP)
2156 #define MLX5_HASH_IP_L4PORTS (MLX5_HASH_FIELD_SEL_SRC_IP |\
2157 MLX5_HASH_FIELD_SEL_DST_IP |\
2158 MLX5_HASH_FIELD_SEL_L4_SPORT |\
2159 MLX5_HASH_FIELD_SEL_L4_DPORT)
2161 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
2162 MLX5_HASH_FIELD_SEL_DST_IP |\
2163 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
2165 mlx5e_build_tir_ctx_lro(tirc, priv);
2167 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2168 MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
2169 mlx5e_build_tir_ctx_hash(tirc, priv);
2172 case MLX5E_TT_IPV4_TCP:
2173 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2174 MLX5_L3_PROT_TYPE_IPV4);
2175 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2176 MLX5_L4_PROT_TYPE_TCP);
2177 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2178 MLX5_HASH_IP_L4PORTS);
2181 case MLX5E_TT_IPV6_TCP:
2182 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2183 MLX5_L3_PROT_TYPE_IPV6);
2184 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2185 MLX5_L4_PROT_TYPE_TCP);
2186 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2187 MLX5_HASH_IP_L4PORTS);
2190 case MLX5E_TT_IPV4_UDP:
2191 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2192 MLX5_L3_PROT_TYPE_IPV4);
2193 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2194 MLX5_L4_PROT_TYPE_UDP);
2195 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2196 MLX5_HASH_IP_L4PORTS);
2199 case MLX5E_TT_IPV6_UDP:
2200 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2201 MLX5_L3_PROT_TYPE_IPV6);
2202 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2203 MLX5_L4_PROT_TYPE_UDP);
2204 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2205 MLX5_HASH_IP_L4PORTS);
2208 case MLX5E_TT_IPV4_IPSEC_AH:
2209 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2210 MLX5_L3_PROT_TYPE_IPV4);
2211 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2212 MLX5_HASH_IP_IPSEC_SPI);
2215 case MLX5E_TT_IPV6_IPSEC_AH:
2216 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2217 MLX5_L3_PROT_TYPE_IPV6);
2218 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2219 MLX5_HASH_IP_IPSEC_SPI);
2222 case MLX5E_TT_IPV4_IPSEC_ESP:
2223 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2224 MLX5_L3_PROT_TYPE_IPV4);
2225 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2226 MLX5_HASH_IP_IPSEC_SPI);
2229 case MLX5E_TT_IPV6_IPSEC_ESP:
2230 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2231 MLX5_L3_PROT_TYPE_IPV6);
2232 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2233 MLX5_HASH_IP_IPSEC_SPI);
2237 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2238 MLX5_L3_PROT_TYPE_IPV4);
2239 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2244 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2245 MLX5_L3_PROT_TYPE_IPV6);
2246 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2251 "mlx5e_build_indir_tir_ctx: bad traffic type!\n");
2255 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 *tirc,
2258 MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2260 mlx5e_build_tir_ctx_lro(tirc, priv);
2262 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2263 MLX5_SET(tirc, tirc, indirect_table, rqtn);
2264 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
2267 static int mlx5e_create_indirect_tirs(struct mlx5e_priv *priv)
2269 struct mlx5e_tir *tir;
2276 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2277 in = mlx5_vzalloc(inlen);
2281 for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2282 memset(in, 0, inlen);
2283 tir = &priv->indir_tir[tt];
2284 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2285 mlx5e_build_indir_tir_ctx(priv, tirc, tt);
2286 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
2288 goto err_destroy_tirs;
2296 for (tt--; tt >= 0; tt--)
2297 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[tt]);
2304 int mlx5e_create_direct_tirs(struct mlx5e_priv *priv)
2306 int nch = priv->profile->max_nch(priv->mdev);
2307 struct mlx5e_tir *tir;
2314 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2315 in = mlx5_vzalloc(inlen);
2319 for (ix = 0; ix < nch; ix++) {
2320 memset(in, 0, inlen);
2321 tir = &priv->direct_tir[ix];
2322 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2323 mlx5e_build_direct_tir_ctx(priv, tirc,
2324 priv->direct_tir[ix].rqt.rqtn);
2325 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
2327 goto err_destroy_ch_tirs;
2334 err_destroy_ch_tirs:
2335 for (ix--; ix >= 0; ix--)
2336 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[ix]);
2343 static void mlx5e_destroy_indirect_tirs(struct mlx5e_priv *priv)
2347 for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
2348 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[i]);
2351 void mlx5e_destroy_direct_tirs(struct mlx5e_priv *priv)
2353 int nch = priv->profile->max_nch(priv->mdev);
2356 for (i = 0; i < nch; i++)
2357 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[i]);
2360 int mlx5e_modify_rqs_vsd(struct mlx5e_priv *priv, bool vsd)
2365 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
2368 for (i = 0; i < priv->params.num_channels; i++) {
2369 err = mlx5e_modify_rq_vsd(&priv->channel[i]->rq, vsd);
2377 static int mlx5e_setup_tc(struct net_device *netdev, u8 tc)
2379 struct mlx5e_priv *priv = netdev_priv(netdev);
2383 if (tc && tc != MLX5E_MAX_NUM_TC)
2386 mutex_lock(&priv->state_lock);
2388 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2390 mlx5e_close_locked(priv->netdev);
2392 priv->params.num_tc = tc ? tc : 1;
2395 err = mlx5e_open_locked(priv->netdev);
2397 mutex_unlock(&priv->state_lock);
2402 static int mlx5e_ndo_setup_tc(struct net_device *dev, u32 handle,
2403 __be16 proto, struct tc_to_netdev *tc)
2405 struct mlx5e_priv *priv = netdev_priv(dev);
2407 if (TC_H_MAJ(handle) != TC_H_MAJ(TC_H_INGRESS))
2411 case TC_SETUP_CLSFLOWER:
2412 switch (tc->cls_flower->command) {
2413 case TC_CLSFLOWER_REPLACE:
2414 return mlx5e_configure_flower(priv, proto, tc->cls_flower);
2415 case TC_CLSFLOWER_DESTROY:
2416 return mlx5e_delete_flower(priv, tc->cls_flower);
2417 case TC_CLSFLOWER_STATS:
2418 return mlx5e_stats_flower(priv, tc->cls_flower);
2425 if (tc->type != TC_SETUP_MQPRIO)
2428 return mlx5e_setup_tc(dev, tc->tc);
2431 struct rtnl_link_stats64 *
2432 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
2434 struct mlx5e_priv *priv = netdev_priv(dev);
2435 struct mlx5e_sw_stats *sstats = &priv->stats.sw;
2436 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
2437 struct mlx5e_pport_stats *pstats = &priv->stats.pport;
2439 stats->rx_packets = sstats->rx_packets;
2440 stats->rx_bytes = sstats->rx_bytes;
2441 stats->tx_packets = sstats->tx_packets;
2442 stats->tx_bytes = sstats->tx_bytes;
2444 stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
2445 stats->tx_dropped = sstats->tx_queue_dropped;
2447 stats->rx_length_errors =
2448 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
2449 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
2450 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
2451 stats->rx_crc_errors =
2452 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
2453 stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
2454 stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
2455 stats->tx_carrier_errors =
2456 PPORT_802_3_GET(pstats, a_symbol_error_during_carrier);
2457 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
2458 stats->rx_frame_errors;
2459 stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
2461 /* vport multicast also counts packets that are dropped due to steering
2462 * or rx out of buffer
2465 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
2470 static void mlx5e_set_rx_mode(struct net_device *dev)
2472 struct mlx5e_priv *priv = netdev_priv(dev);
2474 queue_work(priv->wq, &priv->set_rx_mode_work);
2477 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
2479 struct mlx5e_priv *priv = netdev_priv(netdev);
2480 struct sockaddr *saddr = addr;
2482 if (!is_valid_ether_addr(saddr->sa_data))
2483 return -EADDRNOTAVAIL;
2485 netif_addr_lock_bh(netdev);
2486 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
2487 netif_addr_unlock_bh(netdev);
2489 queue_work(priv->wq, &priv->set_rx_mode_work);
2494 #define MLX5E_SET_FEATURE(netdev, feature, enable) \
2497 netdev->features |= feature; \
2499 netdev->features &= ~feature; \
2502 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
2504 static int set_feature_lro(struct net_device *netdev, bool enable)
2506 struct mlx5e_priv *priv = netdev_priv(netdev);
2507 bool was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2510 mutex_lock(&priv->state_lock);
2512 if (was_opened && (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST))
2513 mlx5e_close_locked(priv->netdev);
2515 priv->params.lro_en = enable;
2516 err = mlx5e_modify_tirs_lro(priv);
2518 netdev_err(netdev, "lro modify failed, %d\n", err);
2519 priv->params.lro_en = !enable;
2522 if (was_opened && (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST))
2523 mlx5e_open_locked(priv->netdev);
2525 mutex_unlock(&priv->state_lock);
2530 static int set_feature_vlan_filter(struct net_device *netdev, bool enable)
2532 struct mlx5e_priv *priv = netdev_priv(netdev);
2535 mlx5e_enable_vlan_filter(priv);
2537 mlx5e_disable_vlan_filter(priv);
2542 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
2544 struct mlx5e_priv *priv = netdev_priv(netdev);
2546 if (!enable && mlx5e_tc_num_filters(priv)) {
2548 "Active offloaded tc filters, can't turn hw_tc_offload off\n");
2555 static int set_feature_rx_all(struct net_device *netdev, bool enable)
2557 struct mlx5e_priv *priv = netdev_priv(netdev);
2558 struct mlx5_core_dev *mdev = priv->mdev;
2560 return mlx5_set_port_fcs(mdev, !enable);
2563 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
2565 struct mlx5e_priv *priv = netdev_priv(netdev);
2568 mutex_lock(&priv->state_lock);
2570 priv->params.vlan_strip_disable = !enable;
2571 err = mlx5e_modify_rqs_vsd(priv, !enable);
2573 priv->params.vlan_strip_disable = enable;
2575 mutex_unlock(&priv->state_lock);
2580 #ifdef CONFIG_RFS_ACCEL
2581 static int set_feature_arfs(struct net_device *netdev, bool enable)
2583 struct mlx5e_priv *priv = netdev_priv(netdev);
2587 err = mlx5e_arfs_enable(priv);
2589 err = mlx5e_arfs_disable(priv);
2595 static int mlx5e_handle_feature(struct net_device *netdev,
2596 netdev_features_t wanted_features,
2597 netdev_features_t feature,
2598 mlx5e_feature_handler feature_handler)
2600 netdev_features_t changes = wanted_features ^ netdev->features;
2601 bool enable = !!(wanted_features & feature);
2604 if (!(changes & feature))
2607 err = feature_handler(netdev, enable);
2609 netdev_err(netdev, "%s feature 0x%llx failed err %d\n",
2610 enable ? "Enable" : "Disable", feature, err);
2614 MLX5E_SET_FEATURE(netdev, feature, enable);
2618 static int mlx5e_set_features(struct net_device *netdev,
2619 netdev_features_t features)
2623 err = mlx5e_handle_feature(netdev, features, NETIF_F_LRO,
2625 err |= mlx5e_handle_feature(netdev, features,
2626 NETIF_F_HW_VLAN_CTAG_FILTER,
2627 set_feature_vlan_filter);
2628 err |= mlx5e_handle_feature(netdev, features, NETIF_F_HW_TC,
2629 set_feature_tc_num_filters);
2630 err |= mlx5e_handle_feature(netdev, features, NETIF_F_RXALL,
2631 set_feature_rx_all);
2632 err |= mlx5e_handle_feature(netdev, features, NETIF_F_HW_VLAN_CTAG_RX,
2633 set_feature_rx_vlan);
2634 #ifdef CONFIG_RFS_ACCEL
2635 err |= mlx5e_handle_feature(netdev, features, NETIF_F_NTUPLE,
2639 return err ? -EINVAL : 0;
2642 #define MXL5_HW_MIN_MTU 64
2643 #define MXL5E_MIN_MTU (MXL5_HW_MIN_MTU + ETH_FCS_LEN)
2645 static int mlx5e_change_mtu(struct net_device *netdev, int new_mtu)
2647 struct mlx5e_priv *priv = netdev_priv(netdev);
2648 struct mlx5_core_dev *mdev = priv->mdev;
2655 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
2657 max_mtu = MLX5E_HW2SW_MTU(max_mtu);
2658 min_mtu = MLX5E_HW2SW_MTU(MXL5E_MIN_MTU);
2660 if (new_mtu > max_mtu || new_mtu < min_mtu) {
2662 "%s: Bad MTU (%d), valid range is: [%d..%d]\n",
2663 __func__, new_mtu, min_mtu, max_mtu);
2667 mutex_lock(&priv->state_lock);
2669 reset = !priv->params.lro_en &&
2670 (priv->params.rq_wq_type !=
2671 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ);
2673 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2674 if (was_opened && reset)
2675 mlx5e_close_locked(netdev);
2677 netdev->mtu = new_mtu;
2678 mlx5e_set_dev_port_mtu(netdev);
2680 if (was_opened && reset)
2681 err = mlx5e_open_locked(netdev);
2683 mutex_unlock(&priv->state_lock);
2688 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2692 return mlx5e_hwstamp_set(dev, ifr);
2694 return mlx5e_hwstamp_get(dev, ifr);
2700 static int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2702 struct mlx5e_priv *priv = netdev_priv(dev);
2703 struct mlx5_core_dev *mdev = priv->mdev;
2705 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
2708 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos)
2710 struct mlx5e_priv *priv = netdev_priv(dev);
2711 struct mlx5_core_dev *mdev = priv->mdev;
2713 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
2717 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2719 struct mlx5e_priv *priv = netdev_priv(dev);
2720 struct mlx5_core_dev *mdev = priv->mdev;
2722 return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
2725 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
2727 struct mlx5e_priv *priv = netdev_priv(dev);
2728 struct mlx5_core_dev *mdev = priv->mdev;
2730 return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
2732 static int mlx5_vport_link2ifla(u8 esw_link)
2735 case MLX5_ESW_VPORT_ADMIN_STATE_DOWN:
2736 return IFLA_VF_LINK_STATE_DISABLE;
2737 case MLX5_ESW_VPORT_ADMIN_STATE_UP:
2738 return IFLA_VF_LINK_STATE_ENABLE;
2740 return IFLA_VF_LINK_STATE_AUTO;
2743 static int mlx5_ifla_link2vport(u8 ifla_link)
2745 switch (ifla_link) {
2746 case IFLA_VF_LINK_STATE_DISABLE:
2747 return MLX5_ESW_VPORT_ADMIN_STATE_DOWN;
2748 case IFLA_VF_LINK_STATE_ENABLE:
2749 return MLX5_ESW_VPORT_ADMIN_STATE_UP;
2751 return MLX5_ESW_VPORT_ADMIN_STATE_AUTO;
2754 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
2757 struct mlx5e_priv *priv = netdev_priv(dev);
2758 struct mlx5_core_dev *mdev = priv->mdev;
2760 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
2761 mlx5_ifla_link2vport(link_state));
2764 static int mlx5e_get_vf_config(struct net_device *dev,
2765 int vf, struct ifla_vf_info *ivi)
2767 struct mlx5e_priv *priv = netdev_priv(dev);
2768 struct mlx5_core_dev *mdev = priv->mdev;
2771 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
2774 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
2778 static int mlx5e_get_vf_stats(struct net_device *dev,
2779 int vf, struct ifla_vf_stats *vf_stats)
2781 struct mlx5e_priv *priv = netdev_priv(dev);
2782 struct mlx5_core_dev *mdev = priv->mdev;
2784 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
2788 static void mlx5e_add_vxlan_port(struct net_device *netdev,
2789 struct udp_tunnel_info *ti)
2791 struct mlx5e_priv *priv = netdev_priv(netdev);
2793 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2796 if (!mlx5e_vxlan_allowed(priv->mdev))
2799 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 1);
2802 static void mlx5e_del_vxlan_port(struct net_device *netdev,
2803 struct udp_tunnel_info *ti)
2805 struct mlx5e_priv *priv = netdev_priv(netdev);
2807 if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2810 if (!mlx5e_vxlan_allowed(priv->mdev))
2813 mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 0);
2816 static netdev_features_t mlx5e_vxlan_features_check(struct mlx5e_priv *priv,
2817 struct sk_buff *skb,
2818 netdev_features_t features)
2820 struct udphdr *udph;
2824 switch (vlan_get_protocol(skb)) {
2825 case htons(ETH_P_IP):
2826 proto = ip_hdr(skb)->protocol;
2828 case htons(ETH_P_IPV6):
2829 proto = ipv6_hdr(skb)->nexthdr;
2835 if (proto == IPPROTO_UDP) {
2836 udph = udp_hdr(skb);
2837 port = be16_to_cpu(udph->dest);
2840 /* Verify if UDP port is being offloaded by HW */
2841 if (port && mlx5e_vxlan_lookup_port(priv, port))
2845 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
2846 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2849 static netdev_features_t mlx5e_features_check(struct sk_buff *skb,
2850 struct net_device *netdev,
2851 netdev_features_t features)
2853 struct mlx5e_priv *priv = netdev_priv(netdev);
2855 features = vlan_features_check(skb, features);
2856 features = vxlan_features_check(skb, features);
2858 /* Validate if the tunneled packet is being offloaded by HW */
2859 if (skb->encapsulation &&
2860 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
2861 return mlx5e_vxlan_features_check(priv, skb, features);
2866 static void mlx5e_tx_timeout(struct net_device *dev)
2868 struct mlx5e_priv *priv = netdev_priv(dev);
2869 bool sched_work = false;
2872 netdev_err(dev, "TX timeout detected\n");
2874 for (i = 0; i < priv->params.num_channels * priv->params.num_tc; i++) {
2875 struct mlx5e_sq *sq = priv->txq_to_sq_map[i];
2877 if (!netif_xmit_stopped(netdev_get_tx_queue(dev, i)))
2880 set_bit(MLX5E_SQ_STATE_FLUSH, &sq->state);
2881 netdev_err(dev, "TX timeout on queue: %d, SQ: 0x%x, CQ: 0x%x, SQ Cons: 0x%x SQ Prod: 0x%x\n",
2882 i, sq->sqn, sq->cq.mcq.cqn, sq->cc, sq->pc);
2885 if (sched_work && test_bit(MLX5E_STATE_OPENED, &priv->state))
2886 schedule_work(&priv->tx_timeout_work);
2889 static const struct net_device_ops mlx5e_netdev_ops_basic = {
2890 .ndo_open = mlx5e_open,
2891 .ndo_stop = mlx5e_close,
2892 .ndo_start_xmit = mlx5e_xmit,
2893 .ndo_setup_tc = mlx5e_ndo_setup_tc,
2894 .ndo_select_queue = mlx5e_select_queue,
2895 .ndo_get_stats64 = mlx5e_get_stats,
2896 .ndo_set_rx_mode = mlx5e_set_rx_mode,
2897 .ndo_set_mac_address = mlx5e_set_mac,
2898 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
2899 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
2900 .ndo_set_features = mlx5e_set_features,
2901 .ndo_change_mtu = mlx5e_change_mtu,
2902 .ndo_do_ioctl = mlx5e_ioctl,
2903 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
2904 #ifdef CONFIG_RFS_ACCEL
2905 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
2907 .ndo_tx_timeout = mlx5e_tx_timeout,
2910 static const struct net_device_ops mlx5e_netdev_ops_sriov = {
2911 .ndo_open = mlx5e_open,
2912 .ndo_stop = mlx5e_close,
2913 .ndo_start_xmit = mlx5e_xmit,
2914 .ndo_setup_tc = mlx5e_ndo_setup_tc,
2915 .ndo_select_queue = mlx5e_select_queue,
2916 .ndo_get_stats64 = mlx5e_get_stats,
2917 .ndo_set_rx_mode = mlx5e_set_rx_mode,
2918 .ndo_set_mac_address = mlx5e_set_mac,
2919 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
2920 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
2921 .ndo_set_features = mlx5e_set_features,
2922 .ndo_change_mtu = mlx5e_change_mtu,
2923 .ndo_do_ioctl = mlx5e_ioctl,
2924 .ndo_udp_tunnel_add = mlx5e_add_vxlan_port,
2925 .ndo_udp_tunnel_del = mlx5e_del_vxlan_port,
2926 .ndo_set_tx_maxrate = mlx5e_set_tx_maxrate,
2927 .ndo_features_check = mlx5e_features_check,
2928 #ifdef CONFIG_RFS_ACCEL
2929 .ndo_rx_flow_steer = mlx5e_rx_flow_steer,
2931 .ndo_set_vf_mac = mlx5e_set_vf_mac,
2932 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
2933 .ndo_set_vf_spoofchk = mlx5e_set_vf_spoofchk,
2934 .ndo_set_vf_trust = mlx5e_set_vf_trust,
2935 .ndo_get_vf_config = mlx5e_get_vf_config,
2936 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
2937 .ndo_get_vf_stats = mlx5e_get_vf_stats,
2938 .ndo_tx_timeout = mlx5e_tx_timeout,
2941 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
2943 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
2945 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
2946 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
2947 !MLX5_CAP_ETH(mdev, csum_cap) ||
2948 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
2949 !MLX5_CAP_ETH(mdev, vlan_cap) ||
2950 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
2951 MLX5_CAP_FLOWTABLE(mdev,
2952 flow_table_properties_nic_receive.max_ft_level)
2954 mlx5_core_warn(mdev,
2955 "Not creating net device, some required device capabilities are missing\n");
2958 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
2959 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
2960 if (!MLX5_CAP_GEN(mdev, cq_moderation))
2961 mlx5_core_warn(mdev, "CQ modiration is not supported\n");
2966 u16 mlx5e_get_max_inline_cap(struct mlx5_core_dev *mdev)
2968 int bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
2970 return bf_buf_size -
2971 sizeof(struct mlx5e_tx_wqe) +
2972 2 /*sizeof(mlx5e_tx_wqe.inline_hdr_start)*/;
2975 #ifdef CONFIG_MLX5_CORE_EN_DCB
2976 static void mlx5e_ets_init(struct mlx5e_priv *priv)
2980 priv->params.ets.ets_cap = mlx5_max_tc(priv->mdev) + 1;
2981 for (i = 0; i < priv->params.ets.ets_cap; i++) {
2982 priv->params.ets.tc_tx_bw[i] = MLX5E_MAX_BW_ALLOC;
2983 priv->params.ets.tc_tsa[i] = IEEE_8021QAZ_TSA_VENDOR;
2984 priv->params.ets.prio_tc[i] = i;
2987 /* tclass[prio=0]=1, tclass[prio=1]=0, tclass[prio=i]=i (for i>1) */
2988 priv->params.ets.prio_tc[0] = 1;
2989 priv->params.ets.prio_tc[1] = 0;
2993 void mlx5e_build_default_indir_rqt(struct mlx5_core_dev *mdev,
2994 u32 *indirection_rqt, int len,
2997 int node = mdev->priv.numa_node;
2998 int node_num_of_cores;
3002 node = first_online_node;
3004 node_num_of_cores = cpumask_weight(cpumask_of_node(node));
3006 if (node_num_of_cores)
3007 num_channels = min_t(int, num_channels, node_num_of_cores);
3009 for (i = 0; i < len; i++)
3010 indirection_rqt[i] = i % num_channels;
3013 static bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
3015 return MLX5_CAP_GEN(mdev, striding_rq) &&
3016 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
3017 MLX5_CAP_ETH(mdev, reg_umr_sq);
3020 static int mlx5e_get_pci_bw(struct mlx5_core_dev *mdev, u32 *pci_bw)
3022 enum pcie_link_width width;
3023 enum pci_bus_speed speed;
3026 err = pcie_get_minimum_link(mdev->pdev, &speed, &width);
3030 if (speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
3034 case PCIE_SPEED_2_5GT:
3035 *pci_bw = 2500 * width;
3037 case PCIE_SPEED_5_0GT:
3038 *pci_bw = 5000 * width;
3040 case PCIE_SPEED_8_0GT:
3041 *pci_bw = 8000 * width;
3050 static bool cqe_compress_heuristic(u32 link_speed, u32 pci_bw)
3052 return (link_speed && pci_bw &&
3053 (pci_bw < 40000) && (pci_bw < link_speed));
3056 void mlx5e_set_rx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
3058 params->rx_cq_period_mode = cq_period_mode;
3060 params->rx_cq_moderation.pkts =
3061 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
3062 params->rx_cq_moderation.usec =
3063 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
3065 if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
3066 params->rx_cq_moderation.usec =
3067 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE;
3070 static void mlx5e_query_min_inline(struct mlx5_core_dev *mdev,
3071 u8 *min_inline_mode)
3073 switch (MLX5_CAP_ETH(mdev, wqe_inline_mode)) {
3074 case MLX5E_INLINE_MODE_L2:
3075 *min_inline_mode = MLX5_INLINE_MODE_L2;
3077 case MLX5E_INLINE_MODE_VPORT_CONTEXT:
3078 mlx5_query_nic_vport_min_inline(mdev,
3081 case MLX5_INLINE_MODE_NOT_REQUIRED:
3082 *min_inline_mode = MLX5_INLINE_MODE_NONE;
3087 static void mlx5e_build_nic_netdev_priv(struct mlx5_core_dev *mdev,
3088 struct net_device *netdev,
3089 const struct mlx5e_profile *profile,
3092 struct mlx5e_priv *priv = netdev_priv(netdev);
3095 u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
3096 MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
3097 MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
3099 priv->params.log_sq_size =
3100 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
3101 priv->params.rq_wq_type = mlx5e_check_fragmented_striding_rq_cap(mdev) ?
3102 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
3103 MLX5_WQ_TYPE_LINKED_LIST;
3105 /* set CQE compression */
3106 priv->params.rx_cqe_compress_admin = false;
3107 if (MLX5_CAP_GEN(mdev, cqe_compression) &&
3108 MLX5_CAP_GEN(mdev, vport_group_manager)) {
3109 mlx5e_get_max_linkspeed(mdev, &link_speed);
3110 mlx5e_get_pci_bw(mdev, &pci_bw);
3111 mlx5_core_dbg(mdev, "Max link speed = %d, PCI BW = %d\n",
3112 link_speed, pci_bw);
3113 priv->params.rx_cqe_compress_admin =
3114 cqe_compress_heuristic(link_speed, pci_bw);
3117 priv->params.rx_cqe_compress = priv->params.rx_cqe_compress_admin;
3119 switch (priv->params.rq_wq_type) {
3120 case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
3121 priv->params.log_rq_size = MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE_MPW;
3122 priv->params.mpwqe_log_stride_sz =
3123 priv->params.rx_cqe_compress ?
3124 MLX5_MPWRQ_LOG_STRIDE_SIZE_CQE_COMPRESS :
3125 MLX5_MPWRQ_LOG_STRIDE_SIZE;
3126 priv->params.mpwqe_log_num_strides = MLX5_MPWRQ_LOG_WQE_SZ -
3127 priv->params.mpwqe_log_stride_sz;
3128 priv->params.lro_en = true;
3130 default: /* MLX5_WQ_TYPE_LINKED_LIST */
3131 priv->params.log_rq_size = MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
3134 mlx5_core_info(mdev,
3135 "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
3136 priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
3137 BIT(priv->params.log_rq_size),
3138 BIT(priv->params.mpwqe_log_stride_sz),
3139 priv->params.rx_cqe_compress_admin);
3141 priv->params.min_rx_wqes = mlx5_min_rx_wqes(priv->params.rq_wq_type,
3142 BIT(priv->params.log_rq_size));
3144 priv->params.rx_am_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
3145 mlx5e_set_rx_cq_mode_params(&priv->params, cq_period_mode);
3147 priv->params.tx_cq_moderation.usec =
3148 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
3149 priv->params.tx_cq_moderation.pkts =
3150 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
3151 priv->params.tx_max_inline = mlx5e_get_max_inline_cap(mdev);
3152 mlx5e_query_min_inline(mdev, &priv->params.tx_min_inline_mode);
3153 priv->params.num_tc = 1;
3154 priv->params.rss_hfunc = ETH_RSS_HASH_XOR;
3156 netdev_rss_key_fill(priv->params.toeplitz_hash_key,
3157 sizeof(priv->params.toeplitz_hash_key));
3159 mlx5e_build_default_indir_rqt(mdev, priv->params.indirection_rqt,
3160 MLX5E_INDIR_RQT_SIZE, profile->max_nch(mdev));
3162 priv->params.lro_wqe_sz =
3163 MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
3165 /* Initialize pflags */
3166 MLX5E_SET_PRIV_FLAG(priv, MLX5E_PFLAG_RX_CQE_BASED_MODER,
3167 priv->params.rx_cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
3170 priv->netdev = netdev;
3171 priv->params.num_channels = profile->max_nch(mdev);
3172 priv->profile = profile;
3173 priv->ppriv = ppriv;
3175 #ifdef CONFIG_MLX5_CORE_EN_DCB
3176 mlx5e_ets_init(priv);
3179 mutex_init(&priv->state_lock);
3181 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
3182 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
3183 INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work);
3184 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
3187 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
3189 struct mlx5e_priv *priv = netdev_priv(netdev);
3191 mlx5_query_nic_vport_mac_address(priv->mdev, 0, netdev->dev_addr);
3192 if (is_zero_ether_addr(netdev->dev_addr) &&
3193 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
3194 eth_hw_addr_random(netdev);
3195 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
3199 static const struct switchdev_ops mlx5e_switchdev_ops = {
3200 .switchdev_port_attr_get = mlx5e_attr_get,
3203 static void mlx5e_build_nic_netdev(struct net_device *netdev)
3205 struct mlx5e_priv *priv = netdev_priv(netdev);
3206 struct mlx5_core_dev *mdev = priv->mdev;
3210 SET_NETDEV_DEV(netdev, &mdev->pdev->dev);
3212 if (MLX5_CAP_GEN(mdev, vport_group_manager)) {
3213 netdev->netdev_ops = &mlx5e_netdev_ops_sriov;
3214 #ifdef CONFIG_MLX5_CORE_EN_DCB
3215 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
3218 netdev->netdev_ops = &mlx5e_netdev_ops_basic;
3221 netdev->watchdog_timeo = 15 * HZ;
3223 netdev->ethtool_ops = &mlx5e_ethtool_ops;
3225 netdev->vlan_features |= NETIF_F_SG;
3226 netdev->vlan_features |= NETIF_F_IP_CSUM;
3227 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
3228 netdev->vlan_features |= NETIF_F_GRO;
3229 netdev->vlan_features |= NETIF_F_TSO;
3230 netdev->vlan_features |= NETIF_F_TSO6;
3231 netdev->vlan_features |= NETIF_F_RXCSUM;
3232 netdev->vlan_features |= NETIF_F_RXHASH;
3234 if (!!MLX5_CAP_ETH(mdev, lro_cap))
3235 netdev->vlan_features |= NETIF_F_LRO;
3237 netdev->hw_features = netdev->vlan_features;
3238 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
3239 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
3240 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
3242 if (mlx5e_vxlan_allowed(mdev)) {
3243 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL |
3244 NETIF_F_GSO_UDP_TUNNEL_CSUM |
3245 NETIF_F_GSO_PARTIAL;
3246 netdev->hw_enc_features |= NETIF_F_IP_CSUM;
3247 netdev->hw_enc_features |= NETIF_F_IPV6_CSUM;
3248 netdev->hw_enc_features |= NETIF_F_TSO;
3249 netdev->hw_enc_features |= NETIF_F_TSO6;
3250 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL;
3251 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
3252 NETIF_F_GSO_PARTIAL;
3253 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
3256 mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
3259 netdev->hw_features |= NETIF_F_RXALL;
3261 netdev->features = netdev->hw_features;
3262 if (!priv->params.lro_en)
3263 netdev->features &= ~NETIF_F_LRO;
3266 netdev->features &= ~NETIF_F_RXALL;
3268 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
3269 if (FT_CAP(flow_modify_en) &&
3270 FT_CAP(modify_root) &&
3271 FT_CAP(identified_miss_table_mode) &&
3272 FT_CAP(flow_table_modify)) {
3273 netdev->hw_features |= NETIF_F_HW_TC;
3274 #ifdef CONFIG_RFS_ACCEL
3275 netdev->hw_features |= NETIF_F_NTUPLE;
3279 netdev->features |= NETIF_F_HIGHDMA;
3281 netdev->priv_flags |= IFF_UNICAST_FLT;
3283 mlx5e_set_netdev_dev_addr(netdev);
3285 #ifdef CONFIG_NET_SWITCHDEV
3286 if (MLX5_CAP_GEN(mdev, vport_group_manager))
3287 netdev->switchdev_ops = &mlx5e_switchdev_ops;
3291 static void mlx5e_create_q_counter(struct mlx5e_priv *priv)
3293 struct mlx5_core_dev *mdev = priv->mdev;
3296 err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
3298 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
3299 priv->q_counter = 0;
3303 static void mlx5e_destroy_q_counter(struct mlx5e_priv *priv)
3305 if (!priv->q_counter)
3308 mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
3311 static int mlx5e_create_umr_mkey(struct mlx5e_priv *priv)
3313 struct mlx5_core_dev *mdev = priv->mdev;
3314 u64 npages = MLX5E_REQUIRED_MTTS(priv->profile->max_nch(mdev),
3315 BIT(MLX5E_PARAMS_MAXIMUM_LOG_RQ_SIZE_MPW));
3316 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
3321 in = mlx5_vzalloc(inlen);
3325 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
3327 npages = min_t(u32, ALIGN(U16_MAX, 4) * 2, npages);
3329 MLX5_SET(mkc, mkc, free, 1);
3330 MLX5_SET(mkc, mkc, umr_en, 1);
3331 MLX5_SET(mkc, mkc, lw, 1);
3332 MLX5_SET(mkc, mkc, lr, 1);
3333 MLX5_SET(mkc, mkc, access_mode, MLX5_MKC_ACCESS_MODE_MTT);
3335 MLX5_SET(mkc, mkc, qpn, 0xffffff);
3336 MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.pdn);
3337 MLX5_SET64(mkc, mkc, len, npages << PAGE_SHIFT);
3338 MLX5_SET(mkc, mkc, translations_octword_size,
3339 MLX5_MTT_OCTW(npages));
3340 MLX5_SET(mkc, mkc, log_page_size, PAGE_SHIFT);
3342 err = mlx5_core_create_mkey(mdev, &priv->umr_mkey, in, inlen);
3348 static void mlx5e_nic_init(struct mlx5_core_dev *mdev,
3349 struct net_device *netdev,
3350 const struct mlx5e_profile *profile,
3353 struct mlx5e_priv *priv = netdev_priv(netdev);
3355 mlx5e_build_nic_netdev_priv(mdev, netdev, profile, ppriv);
3356 mlx5e_build_nic_netdev(netdev);
3357 mlx5e_vxlan_init(priv);
3360 static void mlx5e_nic_cleanup(struct mlx5e_priv *priv)
3362 struct mlx5_core_dev *mdev = priv->mdev;
3363 struct mlx5_eswitch *esw = mdev->priv.eswitch;
3365 mlx5e_vxlan_cleanup(priv);
3367 if (MLX5_CAP_GEN(mdev, vport_group_manager))
3368 mlx5_eswitch_unregister_vport_rep(esw, 0);
3371 static int mlx5e_init_nic_rx(struct mlx5e_priv *priv)
3373 struct mlx5_core_dev *mdev = priv->mdev;
3377 err = mlx5e_create_indirect_rqts(priv);
3379 mlx5_core_warn(mdev, "create indirect rqts failed, %d\n", err);
3383 err = mlx5e_create_direct_rqts(priv);
3385 mlx5_core_warn(mdev, "create direct rqts failed, %d\n", err);
3386 goto err_destroy_indirect_rqts;
3389 err = mlx5e_create_indirect_tirs(priv);
3391 mlx5_core_warn(mdev, "create indirect tirs failed, %d\n", err);
3392 goto err_destroy_direct_rqts;
3395 err = mlx5e_create_direct_tirs(priv);
3397 mlx5_core_warn(mdev, "create direct tirs failed, %d\n", err);
3398 goto err_destroy_indirect_tirs;
3401 err = mlx5e_create_flow_steering(priv);
3403 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
3404 goto err_destroy_direct_tirs;
3407 err = mlx5e_tc_init(priv);
3409 goto err_destroy_flow_steering;
3413 err_destroy_flow_steering:
3414 mlx5e_destroy_flow_steering(priv);
3415 err_destroy_direct_tirs:
3416 mlx5e_destroy_direct_tirs(priv);
3417 err_destroy_indirect_tirs:
3418 mlx5e_destroy_indirect_tirs(priv);
3419 err_destroy_direct_rqts:
3420 for (i = 0; i < priv->profile->max_nch(mdev); i++)
3421 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
3422 err_destroy_indirect_rqts:
3423 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
3427 static void mlx5e_cleanup_nic_rx(struct mlx5e_priv *priv)
3431 mlx5e_tc_cleanup(priv);
3432 mlx5e_destroy_flow_steering(priv);
3433 mlx5e_destroy_direct_tirs(priv);
3434 mlx5e_destroy_indirect_tirs(priv);
3435 for (i = 0; i < priv->profile->max_nch(priv->mdev); i++)
3436 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
3437 mlx5e_destroy_rqt(priv, &priv->indir_rqt);
3440 static int mlx5e_init_nic_tx(struct mlx5e_priv *priv)
3444 err = mlx5e_create_tises(priv);
3446 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
3450 #ifdef CONFIG_MLX5_CORE_EN_DCB
3451 mlx5e_dcbnl_ieee_setets_core(priv, &priv->params.ets);
3456 static void mlx5e_nic_enable(struct mlx5e_priv *priv)
3458 struct net_device *netdev = priv->netdev;
3459 struct mlx5_core_dev *mdev = priv->mdev;
3460 struct mlx5_eswitch *esw = mdev->priv.eswitch;
3461 struct mlx5_eswitch_rep rep;
3463 mlx5_lag_add(mdev, netdev);
3465 if (mlx5e_vxlan_allowed(mdev)) {
3467 udp_tunnel_get_rx_info(netdev);
3471 mlx5e_enable_async_events(priv);
3472 queue_work(priv->wq, &priv->set_rx_mode_work);
3474 if (MLX5_CAP_GEN(mdev, vport_group_manager)) {
3475 mlx5_query_nic_vport_mac_address(mdev, 0, rep.hw_id);
3476 rep.load = mlx5e_nic_rep_load;
3477 rep.unload = mlx5e_nic_rep_unload;
3479 rep.priv_data = priv;
3480 mlx5_eswitch_register_vport_rep(esw, &rep);
3484 static void mlx5e_nic_disable(struct mlx5e_priv *priv)
3486 queue_work(priv->wq, &priv->set_rx_mode_work);
3487 mlx5e_disable_async_events(priv);
3488 mlx5_lag_remove(priv->mdev);
3491 static const struct mlx5e_profile mlx5e_nic_profile = {
3492 .init = mlx5e_nic_init,
3493 .cleanup = mlx5e_nic_cleanup,
3494 .init_rx = mlx5e_init_nic_rx,
3495 .cleanup_rx = mlx5e_cleanup_nic_rx,
3496 .init_tx = mlx5e_init_nic_tx,
3497 .cleanup_tx = mlx5e_cleanup_nic_tx,
3498 .enable = mlx5e_nic_enable,
3499 .disable = mlx5e_nic_disable,
3500 .update_stats = mlx5e_update_stats,
3501 .max_nch = mlx5e_get_max_num_channels,
3502 .max_tc = MLX5E_MAX_NUM_TC,
3505 struct net_device *mlx5e_create_netdev(struct mlx5_core_dev *mdev,
3506 const struct mlx5e_profile *profile,
3509 int nch = profile->max_nch(mdev);
3510 struct net_device *netdev;
3511 struct mlx5e_priv *priv;
3513 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
3514 nch * profile->max_tc,
3517 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
3521 profile->init(mdev, netdev, profile, ppriv);
3523 netif_carrier_off(netdev);
3525 priv = netdev_priv(netdev);
3527 priv->wq = create_singlethread_workqueue("mlx5e");
3529 goto err_cleanup_nic;
3534 profile->cleanup(priv);
3535 free_netdev(netdev);
3540 int mlx5e_attach_netdev(struct mlx5_core_dev *mdev, struct net_device *netdev)
3542 const struct mlx5e_profile *profile;
3543 struct mlx5e_priv *priv;
3546 priv = netdev_priv(netdev);
3547 profile = priv->profile;
3548 clear_bit(MLX5E_STATE_DESTROYING, &priv->state);
3550 err = mlx5e_create_umr_mkey(priv);
3552 mlx5_core_err(mdev, "create umr mkey failed, %d\n", err);
3556 err = profile->init_tx(priv);
3558 goto err_destroy_umr_mkey;
3560 err = mlx5e_open_drop_rq(priv);
3562 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
3563 goto err_cleanup_tx;
3566 err = profile->init_rx(priv);
3568 goto err_close_drop_rq;
3570 mlx5e_create_q_counter(priv);
3572 mlx5e_init_l2_addr(priv);
3574 mlx5e_set_dev_port_mtu(netdev);
3576 if (profile->enable)
3577 profile->enable(priv);
3580 if (netif_running(netdev))
3582 netif_device_attach(netdev);
3588 mlx5e_close_drop_rq(priv);
3591 profile->cleanup_tx(priv);
3593 err_destroy_umr_mkey:
3594 mlx5_core_destroy_mkey(mdev, &priv->umr_mkey);
3600 static void mlx5e_register_vport_rep(struct mlx5_core_dev *mdev)
3602 struct mlx5_eswitch *esw = mdev->priv.eswitch;
3603 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
3607 if (!MLX5_CAP_GEN(mdev, vport_group_manager))
3610 mlx5_query_nic_vport_mac_address(mdev, 0, mac);
3612 for (vport = 1; vport < total_vfs; vport++) {
3613 struct mlx5_eswitch_rep rep;
3615 rep.load = mlx5e_vport_rep_load;
3616 rep.unload = mlx5e_vport_rep_unload;
3618 ether_addr_copy(rep.hw_id, mac);
3619 mlx5_eswitch_register_vport_rep(esw, &rep);
3623 void mlx5e_detach_netdev(struct mlx5_core_dev *mdev, struct net_device *netdev)
3625 struct mlx5e_priv *priv = netdev_priv(netdev);
3626 const struct mlx5e_profile *profile = priv->profile;
3628 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
3629 if (profile->disable)
3630 profile->disable(priv);
3632 flush_workqueue(priv->wq);
3635 if (netif_running(netdev))
3636 mlx5e_close(netdev);
3637 netif_device_detach(netdev);
3640 mlx5e_destroy_q_counter(priv);
3641 profile->cleanup_rx(priv);
3642 mlx5e_close_drop_rq(priv);
3643 profile->cleanup_tx(priv);
3644 mlx5_core_destroy_mkey(priv->mdev, &priv->umr_mkey);
3645 cancel_delayed_work_sync(&priv->update_stats_work);
3648 /* mlx5e_attach and mlx5e_detach scope should be only creating/destroying
3649 * hardware contexts and to connect it to the current netdev.
3651 static int mlx5e_attach(struct mlx5_core_dev *mdev, void *vpriv)
3653 struct mlx5e_priv *priv = vpriv;
3654 struct net_device *netdev = priv->netdev;
3657 if (netif_device_present(netdev))
3660 err = mlx5e_create_mdev_resources(mdev);
3664 err = mlx5e_attach_netdev(mdev, netdev);
3666 mlx5e_destroy_mdev_resources(mdev);
3673 static void mlx5e_detach(struct mlx5_core_dev *mdev, void *vpriv)
3675 struct mlx5e_priv *priv = vpriv;
3676 struct net_device *netdev = priv->netdev;
3678 if (!netif_device_present(netdev))
3681 mlx5e_detach_netdev(mdev, netdev);
3682 mlx5e_destroy_mdev_resources(mdev);
3685 static void *mlx5e_add(struct mlx5_core_dev *mdev)
3687 struct mlx5_eswitch *esw = mdev->priv.eswitch;
3688 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
3693 struct net_device *netdev;
3695 err = mlx5e_check_required_hca_cap(mdev);
3699 mlx5e_register_vport_rep(mdev);
3701 if (MLX5_CAP_GEN(mdev, vport_group_manager))
3702 ppriv = &esw->offloads.vport_reps[0];
3704 netdev = mlx5e_create_netdev(mdev, &mlx5e_nic_profile, ppriv);
3706 mlx5_core_err(mdev, "mlx5e_create_netdev failed\n");
3707 goto err_unregister_reps;
3710 priv = netdev_priv(netdev);
3712 err = mlx5e_attach(mdev, priv);
3714 mlx5_core_err(mdev, "mlx5e_attach failed, %d\n", err);
3715 goto err_destroy_netdev;
3718 err = register_netdev(netdev);
3720 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
3727 mlx5e_detach(mdev, priv);
3730 mlx5e_destroy_netdev(mdev, priv);
3732 err_unregister_reps:
3733 for (vport = 1; vport < total_vfs; vport++)
3734 mlx5_eswitch_unregister_vport_rep(esw, vport);
3739 void mlx5e_destroy_netdev(struct mlx5_core_dev *mdev, struct mlx5e_priv *priv)
3741 const struct mlx5e_profile *profile = priv->profile;
3742 struct net_device *netdev = priv->netdev;
3744 unregister_netdev(netdev);
3745 destroy_workqueue(priv->wq);
3746 if (profile->cleanup)
3747 profile->cleanup(priv);
3748 free_netdev(netdev);
3751 static void mlx5e_remove(struct mlx5_core_dev *mdev, void *vpriv)
3753 struct mlx5_eswitch *esw = mdev->priv.eswitch;
3754 int total_vfs = MLX5_TOTAL_VPORTS(mdev);
3755 struct mlx5e_priv *priv = vpriv;
3758 for (vport = 1; vport < total_vfs; vport++)
3759 mlx5_eswitch_unregister_vport_rep(esw, vport);
3761 mlx5e_detach(mdev, vpriv);
3762 mlx5e_destroy_netdev(mdev, priv);
3765 static void *mlx5e_get_netdev(void *vpriv)
3767 struct mlx5e_priv *priv = vpriv;
3769 return priv->netdev;
3772 static struct mlx5_interface mlx5e_interface = {
3774 .remove = mlx5e_remove,
3775 .attach = mlx5e_attach,
3776 .detach = mlx5e_detach,
3777 .event = mlx5e_async_event,
3778 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
3779 .get_dev = mlx5e_get_netdev,
3782 void mlx5e_init(void)
3784 mlx5e_build_ptys2ethtool_map();
3785 mlx5_register_interface(&mlx5e_interface);
3788 void mlx5e_cleanup(void)
3790 mlx5_unregister_interface(&mlx5e_interface);