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 <linux/mlx5/fs.h>
34 #include <net/vxlan.h>
39 struct mlx5e_rq_param {
40 u32 rqc[MLX5_ST_SZ_DW(rqc)];
41 struct mlx5_wq_param wq;
44 struct mlx5e_sq_param {
45 u32 sqc[MLX5_ST_SZ_DW(sqc)];
46 struct mlx5_wq_param wq;
50 struct mlx5e_cq_param {
51 u32 cqc[MLX5_ST_SZ_DW(cqc)];
52 struct mlx5_wq_param wq;
56 struct mlx5e_channel_param {
57 struct mlx5e_rq_param rq;
58 struct mlx5e_sq_param sq;
59 struct mlx5e_cq_param rx_cq;
60 struct mlx5e_cq_param tx_cq;
63 static void mlx5e_update_carrier(struct mlx5e_priv *priv)
65 struct mlx5_core_dev *mdev = priv->mdev;
68 port_state = mlx5_query_vport_state(mdev,
69 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT, 0);
71 if (port_state == VPORT_STATE_UP)
72 netif_carrier_on(priv->netdev);
74 netif_carrier_off(priv->netdev);
77 static void mlx5e_update_carrier_work(struct work_struct *work)
79 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
82 mutex_lock(&priv->state_lock);
83 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
84 mlx5e_update_carrier(priv);
85 mutex_unlock(&priv->state_lock);
88 static void mlx5e_update_pport_counters(struct mlx5e_priv *priv)
90 struct mlx5_core_dev *mdev = priv->mdev;
91 struct mlx5e_pport_stats *s = &priv->stats.pport;
94 int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
96 in = mlx5_vzalloc(sz);
97 out = mlx5_vzalloc(sz);
101 MLX5_SET(ppcnt_reg, in, local_port, 1);
103 MLX5_SET(ppcnt_reg, in, grp, MLX5_IEEE_802_3_COUNTERS_GROUP);
104 mlx5_core_access_reg(mdev, in, sz, out,
105 sz, MLX5_REG_PPCNT, 0, 0);
106 memcpy(s->IEEE_802_3_counters,
107 MLX5_ADDR_OF(ppcnt_reg, out, counter_set),
108 sizeof(s->IEEE_802_3_counters));
110 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2863_COUNTERS_GROUP);
111 mlx5_core_access_reg(mdev, in, sz, out,
112 sz, MLX5_REG_PPCNT, 0, 0);
113 memcpy(s->RFC_2863_counters,
114 MLX5_ADDR_OF(ppcnt_reg, out, counter_set),
115 sizeof(s->RFC_2863_counters));
117 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2819_COUNTERS_GROUP);
118 mlx5_core_access_reg(mdev, in, sz, out,
119 sz, MLX5_REG_PPCNT, 0, 0);
120 memcpy(s->RFC_2819_counters,
121 MLX5_ADDR_OF(ppcnt_reg, out, counter_set),
122 sizeof(s->RFC_2819_counters));
129 void mlx5e_update_stats(struct mlx5e_priv *priv)
131 struct mlx5_core_dev *mdev = priv->mdev;
132 struct mlx5e_vport_stats *s = &priv->stats.vport;
133 struct mlx5e_rq_stats *rq_stats;
134 struct mlx5e_sq_stats *sq_stats;
135 u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)];
137 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
141 out = mlx5_vzalloc(outlen);
145 /* Collect firts the SW counters and then HW for consistency */
152 s->tso_inner_packets = 0;
153 s->tso_inner_bytes = 0;
154 s->tx_queue_stopped = 0;
155 s->tx_queue_wake = 0;
156 s->tx_queue_dropped = 0;
157 s->tx_csum_inner = 0;
164 for (i = 0; i < priv->params.num_channels; i++) {
165 rq_stats = &priv->channel[i]->rq.stats;
167 s->rx_packets += rq_stats->packets;
168 s->rx_bytes += rq_stats->bytes;
169 s->lro_packets += rq_stats->lro_packets;
170 s->lro_bytes += rq_stats->lro_bytes;
171 s->rx_csum_none += rq_stats->csum_none;
172 s->rx_csum_sw += rq_stats->csum_sw;
173 s->rx_wqe_err += rq_stats->wqe_err;
175 for (j = 0; j < priv->params.num_tc; j++) {
176 sq_stats = &priv->channel[i]->sq[j].stats;
178 s->tx_packets += sq_stats->packets;
179 s->tx_bytes += sq_stats->bytes;
180 s->tso_packets += sq_stats->tso_packets;
181 s->tso_bytes += sq_stats->tso_bytes;
182 s->tso_inner_packets += sq_stats->tso_inner_packets;
183 s->tso_inner_bytes += sq_stats->tso_inner_bytes;
184 s->tx_queue_stopped += sq_stats->stopped;
185 s->tx_queue_wake += sq_stats->wake;
186 s->tx_queue_dropped += sq_stats->dropped;
187 s->tx_csum_inner += sq_stats->csum_offload_inner;
188 tx_offload_none += sq_stats->csum_offload_none;
193 memset(in, 0, sizeof(in));
195 MLX5_SET(query_vport_counter_in, in, opcode,
196 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
197 MLX5_SET(query_vport_counter_in, in, op_mod, 0);
198 MLX5_SET(query_vport_counter_in, in, other_vport, 0);
200 memset(out, 0, outlen);
202 if (mlx5_cmd_exec(mdev, in, sizeof(in), out, outlen))
205 #define MLX5_GET_CTR(p, x) \
206 MLX5_GET64(query_vport_counter_out, p, x)
208 s->rx_error_packets =
209 MLX5_GET_CTR(out, received_errors.packets);
211 MLX5_GET_CTR(out, received_errors.octets);
212 s->tx_error_packets =
213 MLX5_GET_CTR(out, transmit_errors.packets);
215 MLX5_GET_CTR(out, transmit_errors.octets);
217 s->rx_unicast_packets =
218 MLX5_GET_CTR(out, received_eth_unicast.packets);
219 s->rx_unicast_bytes =
220 MLX5_GET_CTR(out, received_eth_unicast.octets);
221 s->tx_unicast_packets =
222 MLX5_GET_CTR(out, transmitted_eth_unicast.packets);
223 s->tx_unicast_bytes =
224 MLX5_GET_CTR(out, transmitted_eth_unicast.octets);
226 s->rx_multicast_packets =
227 MLX5_GET_CTR(out, received_eth_multicast.packets);
228 s->rx_multicast_bytes =
229 MLX5_GET_CTR(out, received_eth_multicast.octets);
230 s->tx_multicast_packets =
231 MLX5_GET_CTR(out, transmitted_eth_multicast.packets);
232 s->tx_multicast_bytes =
233 MLX5_GET_CTR(out, transmitted_eth_multicast.octets);
235 s->rx_broadcast_packets =
236 MLX5_GET_CTR(out, received_eth_broadcast.packets);
237 s->rx_broadcast_bytes =
238 MLX5_GET_CTR(out, received_eth_broadcast.octets);
239 s->tx_broadcast_packets =
240 MLX5_GET_CTR(out, transmitted_eth_broadcast.packets);
241 s->tx_broadcast_bytes =
242 MLX5_GET_CTR(out, transmitted_eth_broadcast.octets);
244 /* Update calculated offload counters */
245 s->tx_csum_offload = s->tx_packets - tx_offload_none - s->tx_csum_inner;
246 s->rx_csum_good = s->rx_packets - s->rx_csum_none -
249 mlx5e_update_pport_counters(priv);
254 static void mlx5e_update_stats_work(struct work_struct *work)
256 struct delayed_work *dwork = to_delayed_work(work);
257 struct mlx5e_priv *priv = container_of(dwork, struct mlx5e_priv,
259 mutex_lock(&priv->state_lock);
260 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
261 mlx5e_update_stats(priv);
262 schedule_delayed_work(dwork,
264 MLX5E_UPDATE_STATS_INTERVAL));
266 mutex_unlock(&priv->state_lock);
269 static void mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
270 enum mlx5_dev_event event, unsigned long param)
272 struct mlx5e_priv *priv = vpriv;
274 if (!test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state))
278 case MLX5_DEV_EVENT_PORT_UP:
279 case MLX5_DEV_EVENT_PORT_DOWN:
280 schedule_work(&priv->update_carrier_work);
288 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
290 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state);
293 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
295 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state);
296 synchronize_irq(mlx5_get_msix_vec(priv->mdev, MLX5_EQ_VEC_ASYNC));
299 #define MLX5E_HW2SW_MTU(hwmtu) (hwmtu - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
300 #define MLX5E_SW2HW_MTU(swmtu) (swmtu + (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
302 static int mlx5e_create_rq(struct mlx5e_channel *c,
303 struct mlx5e_rq_param *param,
306 struct mlx5e_priv *priv = c->priv;
307 struct mlx5_core_dev *mdev = priv->mdev;
308 void *rqc = param->rqc;
309 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
314 param->wq.db_numa_node = cpu_to_node(c->cpu);
316 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
321 rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
323 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
324 rq->skb = kzalloc_node(wq_sz * sizeof(*rq->skb), GFP_KERNEL,
325 cpu_to_node(c->cpu));
328 goto err_rq_wq_destroy;
331 rq->wqe_sz = (priv->params.lro_en) ? priv->params.lro_wqe_sz :
332 MLX5E_SW2HW_MTU(priv->netdev->mtu);
333 rq->wqe_sz = SKB_DATA_ALIGN(rq->wqe_sz + MLX5E_NET_IP_ALIGN);
335 for (i = 0; i < wq_sz; i++) {
336 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
337 u32 byte_count = rq->wqe_sz - MLX5E_NET_IP_ALIGN;
339 wqe->data.lkey = c->mkey_be;
340 wqe->data.byte_count =
341 cpu_to_be32(byte_count | MLX5_HW_START_PADDING);
345 rq->netdev = c->netdev;
346 rq->tstamp = &priv->tstamp;
354 mlx5_wq_destroy(&rq->wq_ctrl);
359 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
362 mlx5_wq_destroy(&rq->wq_ctrl);
365 static int mlx5e_enable_rq(struct mlx5e_rq *rq, struct mlx5e_rq_param *param)
367 struct mlx5e_priv *priv = rq->priv;
368 struct mlx5_core_dev *mdev = priv->mdev;
376 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
377 sizeof(u64) * rq->wq_ctrl.buf.npages;
378 in = mlx5_vzalloc(inlen);
382 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
383 wq = MLX5_ADDR_OF(rqc, rqc, wq);
385 memcpy(rqc, param->rqc, sizeof(param->rqc));
387 MLX5_SET(rqc, rqc, cqn, rq->cq.mcq.cqn);
388 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
389 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
390 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
391 MLX5_ADAPTER_PAGE_SHIFT);
392 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
394 mlx5_fill_page_array(&rq->wq_ctrl.buf,
395 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
397 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
404 static int mlx5e_modify_rq(struct mlx5e_rq *rq, int curr_state, int next_state)
406 struct mlx5e_channel *c = rq->channel;
407 struct mlx5e_priv *priv = c->priv;
408 struct mlx5_core_dev *mdev = priv->mdev;
415 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
416 in = mlx5_vzalloc(inlen);
420 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
422 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
423 MLX5_SET(rqc, rqc, state, next_state);
425 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
432 static void mlx5e_disable_rq(struct mlx5e_rq *rq)
434 mlx5_core_destroy_rq(rq->priv->mdev, rq->rqn);
437 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq)
439 unsigned long exp_time = jiffies + msecs_to_jiffies(20000);
440 struct mlx5e_channel *c = rq->channel;
441 struct mlx5e_priv *priv = c->priv;
442 struct mlx5_wq_ll *wq = &rq->wq;
444 while (time_before(jiffies, exp_time)) {
445 if (wq->cur_sz >= priv->params.min_rx_wqes)
454 static int mlx5e_open_rq(struct mlx5e_channel *c,
455 struct mlx5e_rq_param *param,
460 err = mlx5e_create_rq(c, param, rq);
464 err = mlx5e_enable_rq(rq, param);
468 err = mlx5e_modify_rq(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
472 set_bit(MLX5E_RQ_STATE_POST_WQES_ENABLE, &rq->state);
473 mlx5e_send_nop(&c->sq[0], true); /* trigger mlx5e_post_rx_wqes() */
478 mlx5e_disable_rq(rq);
480 mlx5e_destroy_rq(rq);
485 static void mlx5e_close_rq(struct mlx5e_rq *rq)
487 clear_bit(MLX5E_RQ_STATE_POST_WQES_ENABLE, &rq->state);
488 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
490 mlx5e_modify_rq(rq, MLX5_RQC_STATE_RDY, MLX5_RQC_STATE_ERR);
491 while (!mlx5_wq_ll_is_empty(&rq->wq))
494 /* avoid destroying rq before mlx5e_poll_rx_cq() is done with it */
495 napi_synchronize(&rq->channel->napi);
497 mlx5e_disable_rq(rq);
498 mlx5e_destroy_rq(rq);
501 static void mlx5e_free_sq_db(struct mlx5e_sq *sq)
508 static int mlx5e_alloc_sq_db(struct mlx5e_sq *sq, int numa)
510 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
511 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
513 sq->skb = kzalloc_node(wq_sz * sizeof(*sq->skb), GFP_KERNEL, numa);
514 sq->dma_fifo = kzalloc_node(df_sz * sizeof(*sq->dma_fifo), GFP_KERNEL,
516 sq->wqe_info = kzalloc_node(wq_sz * sizeof(*sq->wqe_info), GFP_KERNEL,
519 if (!sq->skb || !sq->dma_fifo || !sq->wqe_info) {
520 mlx5e_free_sq_db(sq);
524 sq->dma_fifo_mask = df_sz - 1;
529 static int mlx5e_create_sq(struct mlx5e_channel *c,
531 struct mlx5e_sq_param *param,
534 struct mlx5e_priv *priv = c->priv;
535 struct mlx5_core_dev *mdev = priv->mdev;
537 void *sqc = param->sqc;
538 void *sqc_wq = MLX5_ADDR_OF(sqc, sqc, wq);
542 err = mlx5_alloc_map_uar(mdev, &sq->uar, true);
546 param->wq.db_numa_node = cpu_to_node(c->cpu);
548 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq,
551 goto err_unmap_free_uar;
553 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
554 if (sq->uar.bf_map) {
555 set_bit(MLX5E_SQ_STATE_BF_ENABLE, &sq->state);
556 sq->uar_map = sq->uar.bf_map;
558 sq->uar_map = sq->uar.map;
560 sq->bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
561 sq->max_inline = param->max_inline;
563 err = mlx5e_alloc_sq_db(sq, cpu_to_node(c->cpu));
565 goto err_sq_wq_destroy;
567 txq_ix = c->ix + tc * priv->params.num_channels;
568 sq->txq = netdev_get_tx_queue(priv->netdev, txq_ix);
571 sq->tstamp = &priv->tstamp;
572 sq->mkey_be = c->mkey_be;
575 sq->edge = (sq->wq.sz_m1 + 1) - MLX5_SEND_WQE_MAX_WQEBBS;
576 sq->bf_budget = MLX5E_SQ_BF_BUDGET;
577 priv->txq_to_sq_map[txq_ix] = sq;
582 mlx5_wq_destroy(&sq->wq_ctrl);
585 mlx5_unmap_free_uar(mdev, &sq->uar);
590 static void mlx5e_destroy_sq(struct mlx5e_sq *sq)
592 struct mlx5e_channel *c = sq->channel;
593 struct mlx5e_priv *priv = c->priv;
595 mlx5e_free_sq_db(sq);
596 mlx5_wq_destroy(&sq->wq_ctrl);
597 mlx5_unmap_free_uar(priv->mdev, &sq->uar);
600 static int mlx5e_enable_sq(struct mlx5e_sq *sq, struct mlx5e_sq_param *param)
602 struct mlx5e_channel *c = sq->channel;
603 struct mlx5e_priv *priv = c->priv;
604 struct mlx5_core_dev *mdev = priv->mdev;
612 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
613 sizeof(u64) * sq->wq_ctrl.buf.npages;
614 in = mlx5_vzalloc(inlen);
618 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
619 wq = MLX5_ADDR_OF(sqc, sqc, wq);
621 memcpy(sqc, param->sqc, sizeof(param->sqc));
623 MLX5_SET(sqc, sqc, tis_num_0, priv->tisn[sq->tc]);
624 MLX5_SET(sqc, sqc, cqn, c->sq[sq->tc].cq.mcq.cqn);
625 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
626 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
627 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
629 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
630 MLX5_SET(wq, wq, uar_page, sq->uar.index);
631 MLX5_SET(wq, wq, log_wq_pg_sz, sq->wq_ctrl.buf.page_shift -
632 MLX5_ADAPTER_PAGE_SHIFT);
633 MLX5_SET64(wq, wq, dbr_addr, sq->wq_ctrl.db.dma);
635 mlx5_fill_page_array(&sq->wq_ctrl.buf,
636 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
638 err = mlx5_core_create_sq(mdev, in, inlen, &sq->sqn);
645 static int mlx5e_modify_sq(struct mlx5e_sq *sq, int curr_state, int next_state)
647 struct mlx5e_channel *c = sq->channel;
648 struct mlx5e_priv *priv = c->priv;
649 struct mlx5_core_dev *mdev = priv->mdev;
656 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
657 in = mlx5_vzalloc(inlen);
661 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
663 MLX5_SET(modify_sq_in, in, sq_state, curr_state);
664 MLX5_SET(sqc, sqc, state, next_state);
666 err = mlx5_core_modify_sq(mdev, sq->sqn, in, inlen);
673 static void mlx5e_disable_sq(struct mlx5e_sq *sq)
675 struct mlx5e_channel *c = sq->channel;
676 struct mlx5e_priv *priv = c->priv;
677 struct mlx5_core_dev *mdev = priv->mdev;
679 mlx5_core_destroy_sq(mdev, sq->sqn);
682 static int mlx5e_open_sq(struct mlx5e_channel *c,
684 struct mlx5e_sq_param *param,
689 err = mlx5e_create_sq(c, tc, param, sq);
693 err = mlx5e_enable_sq(sq, param);
697 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST, MLX5_SQC_STATE_RDY);
701 set_bit(MLX5E_SQ_STATE_WAKE_TXQ_ENABLE, &sq->state);
702 netdev_tx_reset_queue(sq->txq);
703 netif_tx_start_queue(sq->txq);
708 mlx5e_disable_sq(sq);
710 mlx5e_destroy_sq(sq);
715 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
717 __netif_tx_lock_bh(txq);
718 netif_tx_stop_queue(txq);
719 __netif_tx_unlock_bh(txq);
722 static void mlx5e_close_sq(struct mlx5e_sq *sq)
724 clear_bit(MLX5E_SQ_STATE_WAKE_TXQ_ENABLE, &sq->state);
725 napi_synchronize(&sq->channel->napi); /* prevent netif_tx_wake_queue */
726 netif_tx_disable_queue(sq->txq);
728 /* ensure hw is notified of all pending wqes */
729 if (mlx5e_sq_has_room_for(sq, 1))
730 mlx5e_send_nop(sq, true);
732 mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY, MLX5_SQC_STATE_ERR);
733 while (sq->cc != sq->pc) /* wait till sq is empty */
736 /* avoid destroying sq before mlx5e_poll_tx_cq() is done with it */
737 napi_synchronize(&sq->channel->napi);
739 mlx5e_disable_sq(sq);
740 mlx5e_destroy_sq(sq);
743 static int mlx5e_create_cq(struct mlx5e_channel *c,
744 struct mlx5e_cq_param *param,
747 struct mlx5e_priv *priv = c->priv;
748 struct mlx5_core_dev *mdev = priv->mdev;
749 struct mlx5_core_cq *mcq = &cq->mcq;
755 param->wq.buf_numa_node = cpu_to_node(c->cpu);
756 param->wq.db_numa_node = cpu_to_node(c->cpu);
757 param->eq_ix = c->ix;
759 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
764 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
769 mcq->set_ci_db = cq->wq_ctrl.db.db;
770 mcq->arm_db = cq->wq_ctrl.db.db + 1;
773 mcq->vector = param->eq_ix;
774 mcq->comp = mlx5e_completion_event;
775 mcq->event = mlx5e_cq_error_event;
777 mcq->uar = &priv->cq_uar;
779 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
780 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
791 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
793 mlx5_wq_destroy(&cq->wq_ctrl);
796 static int mlx5e_enable_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
798 struct mlx5e_priv *priv = cq->priv;
799 struct mlx5_core_dev *mdev = priv->mdev;
800 struct mlx5_core_cq *mcq = &cq->mcq;
805 unsigned int irqn_not_used;
809 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
810 sizeof(u64) * cq->wq_ctrl.buf.npages;
811 in = mlx5_vzalloc(inlen);
815 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
817 memcpy(cqc, param->cqc, sizeof(param->cqc));
819 mlx5_fill_page_array(&cq->wq_ctrl.buf,
820 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
822 mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
824 MLX5_SET(cqc, cqc, c_eqn, eqn);
825 MLX5_SET(cqc, cqc, uar_page, mcq->uar->index);
826 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
827 MLX5_ADAPTER_PAGE_SHIFT);
828 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
830 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
842 static void mlx5e_disable_cq(struct mlx5e_cq *cq)
844 struct mlx5e_priv *priv = cq->priv;
845 struct mlx5_core_dev *mdev = priv->mdev;
847 mlx5_core_destroy_cq(mdev, &cq->mcq);
850 static int mlx5e_open_cq(struct mlx5e_channel *c,
851 struct mlx5e_cq_param *param,
853 u16 moderation_usecs,
854 u16 moderation_frames)
857 struct mlx5e_priv *priv = c->priv;
858 struct mlx5_core_dev *mdev = priv->mdev;
860 err = mlx5e_create_cq(c, param, cq);
864 err = mlx5e_enable_cq(cq, param);
868 if (MLX5_CAP_GEN(mdev, cq_moderation))
869 mlx5_core_modify_cq_moderation(mdev, &cq->mcq,
875 mlx5e_destroy_cq(cq);
880 static void mlx5e_close_cq(struct mlx5e_cq *cq)
882 mlx5e_disable_cq(cq);
883 mlx5e_destroy_cq(cq);
886 static int mlx5e_get_cpu(struct mlx5e_priv *priv, int ix)
888 return cpumask_first(priv->mdev->priv.irq_info[ix].mask);
891 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
892 struct mlx5e_channel_param *cparam)
894 struct mlx5e_priv *priv = c->priv;
898 for (tc = 0; tc < c->num_tc; tc++) {
899 err = mlx5e_open_cq(c, &cparam->tx_cq, &c->sq[tc].cq,
900 priv->params.tx_cq_moderation_usec,
901 priv->params.tx_cq_moderation_pkts);
903 goto err_close_tx_cqs;
909 for (tc--; tc >= 0; tc--)
910 mlx5e_close_cq(&c->sq[tc].cq);
915 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
919 for (tc = 0; tc < c->num_tc; tc++)
920 mlx5e_close_cq(&c->sq[tc].cq);
923 static int mlx5e_open_sqs(struct mlx5e_channel *c,
924 struct mlx5e_channel_param *cparam)
929 for (tc = 0; tc < c->num_tc; tc++) {
930 err = mlx5e_open_sq(c, tc, &cparam->sq, &c->sq[tc]);
938 for (tc--; tc >= 0; tc--)
939 mlx5e_close_sq(&c->sq[tc]);
944 static void mlx5e_close_sqs(struct mlx5e_channel *c)
948 for (tc = 0; tc < c->num_tc; tc++)
949 mlx5e_close_sq(&c->sq[tc]);
952 static void mlx5e_build_channeltc_to_txq_map(struct mlx5e_priv *priv, int ix)
956 for (i = 0; i < MLX5E_MAX_NUM_TC; i++)
957 priv->channeltc_to_txq_map[ix][i] =
958 ix + i * priv->params.num_channels;
961 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
962 struct mlx5e_channel_param *cparam,
963 struct mlx5e_channel **cp)
965 struct net_device *netdev = priv->netdev;
966 int cpu = mlx5e_get_cpu(priv, ix);
967 struct mlx5e_channel *c;
970 c = kzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
977 c->pdev = &priv->mdev->pdev->dev;
978 c->netdev = priv->netdev;
979 c->mkey_be = cpu_to_be32(priv->mr.key);
980 c->num_tc = priv->params.num_tc;
982 mlx5e_build_channeltc_to_txq_map(priv, ix);
984 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
986 err = mlx5e_open_tx_cqs(c, cparam);
990 err = mlx5e_open_cq(c, &cparam->rx_cq, &c->rq.cq,
991 priv->params.rx_cq_moderation_usec,
992 priv->params.rx_cq_moderation_pkts);
994 goto err_close_tx_cqs;
996 napi_enable(&c->napi);
998 err = mlx5e_open_sqs(c, cparam);
1000 goto err_disable_napi;
1002 err = mlx5e_open_rq(c, &cparam->rq, &c->rq);
1006 netif_set_xps_queue(netdev, get_cpu_mask(c->cpu), ix);
1015 napi_disable(&c->napi);
1016 mlx5e_close_cq(&c->rq.cq);
1019 mlx5e_close_tx_cqs(c);
1022 netif_napi_del(&c->napi);
1023 napi_hash_del(&c->napi);
1029 static void mlx5e_close_channel(struct mlx5e_channel *c)
1031 mlx5e_close_rq(&c->rq);
1033 napi_disable(&c->napi);
1034 mlx5e_close_cq(&c->rq.cq);
1035 mlx5e_close_tx_cqs(c);
1036 netif_napi_del(&c->napi);
1038 napi_hash_del(&c->napi);
1044 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
1045 struct mlx5e_rq_param *param)
1047 void *rqc = param->rqc;
1048 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1050 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1051 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1052 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1053 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_rq_size);
1054 MLX5_SET(wq, wq, pd, priv->pdn);
1056 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1057 param->wq.linear = 1;
1060 static void mlx5e_build_drop_rq_param(struct mlx5e_rq_param *param)
1062 void *rqc = param->rqc;
1063 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1065 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1066 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1069 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
1070 struct mlx5e_sq_param *param)
1072 void *sqc = param->sqc;
1073 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1075 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_sq_size);
1076 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1077 MLX5_SET(wq, wq, pd, priv->pdn);
1079 param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1080 param->max_inline = priv->params.tx_max_inline;
1083 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1084 struct mlx5e_cq_param *param)
1086 void *cqc = param->cqc;
1088 MLX5_SET(cqc, cqc, uar_page, priv->cq_uar.index);
1091 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
1092 struct mlx5e_cq_param *param)
1094 void *cqc = param->cqc;
1096 MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_rq_size);
1098 mlx5e_build_common_cq_param(priv, param);
1101 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
1102 struct mlx5e_cq_param *param)
1104 void *cqc = param->cqc;
1106 MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_sq_size);
1108 mlx5e_build_common_cq_param(priv, param);
1111 static void mlx5e_build_channel_param(struct mlx5e_priv *priv,
1112 struct mlx5e_channel_param *cparam)
1114 memset(cparam, 0, sizeof(*cparam));
1116 mlx5e_build_rq_param(priv, &cparam->rq);
1117 mlx5e_build_sq_param(priv, &cparam->sq);
1118 mlx5e_build_rx_cq_param(priv, &cparam->rx_cq);
1119 mlx5e_build_tx_cq_param(priv, &cparam->tx_cq);
1122 static int mlx5e_open_channels(struct mlx5e_priv *priv)
1124 struct mlx5e_channel_param cparam;
1125 int nch = priv->params.num_channels;
1130 priv->channel = kcalloc(nch, sizeof(struct mlx5e_channel *),
1133 priv->txq_to_sq_map = kcalloc(nch * priv->params.num_tc,
1134 sizeof(struct mlx5e_sq *), GFP_KERNEL);
1136 if (!priv->channel || !priv->txq_to_sq_map)
1137 goto err_free_txq_to_sq_map;
1139 mlx5e_build_channel_param(priv, &cparam);
1140 for (i = 0; i < nch; i++) {
1141 err = mlx5e_open_channel(priv, i, &cparam, &priv->channel[i]);
1143 goto err_close_channels;
1146 for (j = 0; j < nch; j++) {
1147 err = mlx5e_wait_for_min_rx_wqes(&priv->channel[j]->rq);
1149 goto err_close_channels;
1155 for (i--; i >= 0; i--)
1156 mlx5e_close_channel(priv->channel[i]);
1158 err_free_txq_to_sq_map:
1159 kfree(priv->txq_to_sq_map);
1160 kfree(priv->channel);
1165 static void mlx5e_close_channels(struct mlx5e_priv *priv)
1169 for (i = 0; i < priv->params.num_channels; i++)
1170 mlx5e_close_channel(priv->channel[i]);
1172 kfree(priv->txq_to_sq_map);
1173 kfree(priv->channel);
1176 static int mlx5e_rx_hash_fn(int hfunc)
1178 return (hfunc == ETH_RSS_HASH_TOP) ?
1179 MLX5_RX_HASH_FN_TOEPLITZ :
1180 MLX5_RX_HASH_FN_INVERTED_XOR8;
1183 static int mlx5e_bits_invert(unsigned long a, int size)
1188 for (i = 0; i < size; i++)
1189 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
1194 static void mlx5e_fill_indir_rqt_rqns(struct mlx5e_priv *priv, void *rqtc)
1198 for (i = 0; i < MLX5E_INDIR_RQT_SIZE; i++) {
1201 if (priv->params.rss_hfunc == ETH_RSS_HASH_XOR)
1202 ix = mlx5e_bits_invert(i, MLX5E_LOG_INDIR_RQT_SIZE);
1204 ix = priv->params.indirection_rqt[ix];
1205 MLX5_SET(rqtc, rqtc, rq_num[i],
1206 test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1207 priv->channel[ix]->rq.rqn :
1212 static void mlx5e_fill_rqt_rqns(struct mlx5e_priv *priv, void *rqtc,
1213 enum mlx5e_rqt_ix rqt_ix)
1217 case MLX5E_INDIRECTION_RQT:
1218 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1222 default: /* MLX5E_SINGLE_RQ_RQT */
1223 MLX5_SET(rqtc, rqtc, rq_num[0],
1224 test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1225 priv->channel[0]->rq.rqn :
1232 static int mlx5e_create_rqt(struct mlx5e_priv *priv, enum mlx5e_rqt_ix rqt_ix)
1234 struct mlx5_core_dev *mdev = priv->mdev;
1241 sz = (rqt_ix == MLX5E_SINGLE_RQ_RQT) ? 1 : MLX5E_INDIR_RQT_SIZE;
1243 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
1244 in = mlx5_vzalloc(inlen);
1248 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
1250 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1251 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
1253 mlx5e_fill_rqt_rqns(priv, rqtc, rqt_ix);
1255 err = mlx5_core_create_rqt(mdev, in, inlen, &priv->rqtn[rqt_ix]);
1262 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, enum mlx5e_rqt_ix rqt_ix)
1264 struct mlx5_core_dev *mdev = priv->mdev;
1271 sz = (rqt_ix == MLX5E_SINGLE_RQ_RQT) ? 1 : MLX5E_INDIR_RQT_SIZE;
1273 inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
1274 in = mlx5_vzalloc(inlen);
1278 rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
1280 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1282 mlx5e_fill_rqt_rqns(priv, rqtc, rqt_ix);
1284 MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
1286 err = mlx5_core_modify_rqt(mdev, priv->rqtn[rqt_ix], in, inlen);
1293 static void mlx5e_destroy_rqt(struct mlx5e_priv *priv, enum mlx5e_rqt_ix rqt_ix)
1295 mlx5_core_destroy_rqt(priv->mdev, priv->rqtn[rqt_ix]);
1298 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv)
1300 mlx5e_redirect_rqt(priv, MLX5E_INDIRECTION_RQT);
1301 mlx5e_redirect_rqt(priv, MLX5E_SINGLE_RQ_RQT);
1304 static void mlx5e_build_tir_ctx_lro(void *tirc, struct mlx5e_priv *priv)
1306 if (!priv->params.lro_en)
1309 #define ROUGH_MAX_L2_L3_HDR_SZ 256
1311 MLX5_SET(tirc, tirc, lro_enable_mask,
1312 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
1313 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
1314 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
1315 (priv->params.lro_wqe_sz -
1316 ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
1317 MLX5_SET(tirc, tirc, lro_timeout_period_usecs,
1318 MLX5_CAP_ETH(priv->mdev,
1319 lro_timer_supported_periods[2]));
1322 void mlx5e_build_tir_ctx_hash(void *tirc, struct mlx5e_priv *priv)
1324 MLX5_SET(tirc, tirc, rx_hash_fn,
1325 mlx5e_rx_hash_fn(priv->params.rss_hfunc));
1326 if (priv->params.rss_hfunc == ETH_RSS_HASH_TOP) {
1327 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
1328 rx_hash_toeplitz_key);
1329 size_t len = MLX5_FLD_SZ_BYTES(tirc,
1330 rx_hash_toeplitz_key);
1332 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1333 memcpy(rss_key, priv->params.toeplitz_hash_key, len);
1337 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
1339 struct mlx5_core_dev *mdev = priv->mdev;
1347 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
1348 in = mlx5_vzalloc(inlen);
1352 MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
1353 tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
1355 mlx5e_build_tir_ctx_lro(tirc, priv);
1357 for (tt = 0; tt < MLX5E_NUM_TT; tt++) {
1358 err = mlx5_core_modify_tir(mdev, priv->tirn[tt], in, inlen);
1368 static int mlx5e_refresh_tir_self_loopback_enable(struct mlx5_core_dev *mdev,
1375 inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
1376 in = mlx5_vzalloc(inlen);
1380 MLX5_SET(modify_tir_in, in, bitmask.self_lb_en, 1);
1382 err = mlx5_core_modify_tir(mdev, tirn, in, inlen);
1389 static int mlx5e_refresh_tirs_self_loopback_enable(struct mlx5e_priv *priv)
1394 for (i = 0; i < MLX5E_NUM_TT; i++) {
1395 err = mlx5e_refresh_tir_self_loopback_enable(priv->mdev,
1404 static int mlx5e_set_dev_port_mtu(struct net_device *netdev)
1406 struct mlx5e_priv *priv = netdev_priv(netdev);
1407 struct mlx5_core_dev *mdev = priv->mdev;
1411 err = mlx5_set_port_mtu(mdev, MLX5E_SW2HW_MTU(netdev->mtu), 1);
1415 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
1417 if (MLX5E_HW2SW_MTU(hw_mtu) != netdev->mtu)
1418 netdev_warn(netdev, "%s: Port MTU %d is different than netdev mtu %d\n",
1419 __func__, MLX5E_HW2SW_MTU(hw_mtu), netdev->mtu);
1421 netdev->mtu = MLX5E_HW2SW_MTU(hw_mtu);
1425 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
1427 struct mlx5e_priv *priv = netdev_priv(netdev);
1428 int nch = priv->params.num_channels;
1429 int ntc = priv->params.num_tc;
1432 netdev_reset_tc(netdev);
1437 netdev_set_num_tc(netdev, ntc);
1439 for (tc = 0; tc < ntc; tc++)
1440 netdev_set_tc_queue(netdev, tc, nch, tc * nch);
1443 int mlx5e_open_locked(struct net_device *netdev)
1445 struct mlx5e_priv *priv = netdev_priv(netdev);
1449 set_bit(MLX5E_STATE_OPENED, &priv->state);
1451 mlx5e_netdev_set_tcs(netdev);
1453 num_txqs = priv->params.num_channels * priv->params.num_tc;
1454 netif_set_real_num_tx_queues(netdev, num_txqs);
1455 netif_set_real_num_rx_queues(netdev, priv->params.num_channels);
1457 err = mlx5e_set_dev_port_mtu(netdev);
1459 goto err_clear_state_opened_flag;
1461 err = mlx5e_open_channels(priv);
1463 netdev_err(netdev, "%s: mlx5e_open_channels failed, %d\n",
1465 goto err_clear_state_opened_flag;
1468 err = mlx5e_refresh_tirs_self_loopback_enable(priv);
1470 netdev_err(netdev, "%s: mlx5e_refresh_tirs_self_loopback_enable failed, %d\n",
1472 goto err_close_channels;
1475 mlx5e_redirect_rqts(priv);
1476 mlx5e_update_carrier(priv);
1477 mlx5e_timestamp_init(priv);
1479 schedule_delayed_work(&priv->update_stats_work, 0);
1484 mlx5e_close_channels(priv);
1485 err_clear_state_opened_flag:
1486 clear_bit(MLX5E_STATE_OPENED, &priv->state);
1490 static int mlx5e_open(struct net_device *netdev)
1492 struct mlx5e_priv *priv = netdev_priv(netdev);
1495 mutex_lock(&priv->state_lock);
1496 err = mlx5e_open_locked(netdev);
1497 mutex_unlock(&priv->state_lock);
1502 int mlx5e_close_locked(struct net_device *netdev)
1504 struct mlx5e_priv *priv = netdev_priv(netdev);
1506 /* May already be CLOSED in case a previous configuration operation
1507 * (e.g RX/TX queue size change) that involves close&open failed.
1509 if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
1512 clear_bit(MLX5E_STATE_OPENED, &priv->state);
1514 mlx5e_timestamp_cleanup(priv);
1515 netif_carrier_off(priv->netdev);
1516 mlx5e_redirect_rqts(priv);
1517 mlx5e_close_channels(priv);
1522 static int mlx5e_close(struct net_device *netdev)
1524 struct mlx5e_priv *priv = netdev_priv(netdev);
1527 mutex_lock(&priv->state_lock);
1528 err = mlx5e_close_locked(netdev);
1529 mutex_unlock(&priv->state_lock);
1534 static int mlx5e_create_drop_rq(struct mlx5e_priv *priv,
1535 struct mlx5e_rq *rq,
1536 struct mlx5e_rq_param *param)
1538 struct mlx5_core_dev *mdev = priv->mdev;
1539 void *rqc = param->rqc;
1540 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
1543 param->wq.db_numa_node = param->wq.buf_numa_node;
1545 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
1555 static int mlx5e_create_drop_cq(struct mlx5e_priv *priv,
1556 struct mlx5e_cq *cq,
1557 struct mlx5e_cq_param *param)
1559 struct mlx5_core_dev *mdev = priv->mdev;
1560 struct mlx5_core_cq *mcq = &cq->mcq;
1565 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1570 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1573 mcq->set_ci_db = cq->wq_ctrl.db.db;
1574 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1575 *mcq->set_ci_db = 0;
1577 mcq->vector = param->eq_ix;
1578 mcq->comp = mlx5e_completion_event;
1579 mcq->event = mlx5e_cq_error_event;
1581 mcq->uar = &priv->cq_uar;
1588 static int mlx5e_open_drop_rq(struct mlx5e_priv *priv)
1590 struct mlx5e_cq_param cq_param;
1591 struct mlx5e_rq_param rq_param;
1592 struct mlx5e_rq *rq = &priv->drop_rq;
1593 struct mlx5e_cq *cq = &priv->drop_rq.cq;
1596 memset(&cq_param, 0, sizeof(cq_param));
1597 memset(&rq_param, 0, sizeof(rq_param));
1598 mlx5e_build_drop_rq_param(&rq_param);
1600 err = mlx5e_create_drop_cq(priv, cq, &cq_param);
1604 err = mlx5e_enable_cq(cq, &cq_param);
1606 goto err_destroy_cq;
1608 err = mlx5e_create_drop_rq(priv, rq, &rq_param);
1610 goto err_disable_cq;
1612 err = mlx5e_enable_rq(rq, &rq_param);
1614 goto err_destroy_rq;
1619 mlx5e_destroy_rq(&priv->drop_rq);
1622 mlx5e_disable_cq(&priv->drop_rq.cq);
1625 mlx5e_destroy_cq(&priv->drop_rq.cq);
1630 static void mlx5e_close_drop_rq(struct mlx5e_priv *priv)
1632 mlx5e_disable_rq(&priv->drop_rq);
1633 mlx5e_destroy_rq(&priv->drop_rq);
1634 mlx5e_disable_cq(&priv->drop_rq.cq);
1635 mlx5e_destroy_cq(&priv->drop_rq.cq);
1638 static int mlx5e_create_tis(struct mlx5e_priv *priv, int tc)
1640 struct mlx5_core_dev *mdev = priv->mdev;
1641 u32 in[MLX5_ST_SZ_DW(create_tis_in)];
1642 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1644 memset(in, 0, sizeof(in));
1646 MLX5_SET(tisc, tisc, prio, tc << 1);
1647 MLX5_SET(tisc, tisc, transport_domain, priv->tdn);
1649 return mlx5_core_create_tis(mdev, in, sizeof(in), &priv->tisn[tc]);
1652 static void mlx5e_destroy_tis(struct mlx5e_priv *priv, int tc)
1654 mlx5_core_destroy_tis(priv->mdev, priv->tisn[tc]);
1657 static int mlx5e_create_tises(struct mlx5e_priv *priv)
1662 for (tc = 0; tc < MLX5E_MAX_NUM_TC; tc++) {
1663 err = mlx5e_create_tis(priv, tc);
1665 goto err_close_tises;
1671 for (tc--; tc >= 0; tc--)
1672 mlx5e_destroy_tis(priv, tc);
1677 static void mlx5e_destroy_tises(struct mlx5e_priv *priv)
1681 for (tc = 0; tc < MLX5E_MAX_NUM_TC; tc++)
1682 mlx5e_destroy_tis(priv, tc);
1685 static void mlx5e_build_tir_ctx(struct mlx5e_priv *priv, u32 *tirc, int tt)
1687 void *hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1689 MLX5_SET(tirc, tirc, transport_domain, priv->tdn);
1691 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
1692 MLX5_HASH_FIELD_SEL_DST_IP)
1694 #define MLX5_HASH_IP_L4PORTS (MLX5_HASH_FIELD_SEL_SRC_IP |\
1695 MLX5_HASH_FIELD_SEL_DST_IP |\
1696 MLX5_HASH_FIELD_SEL_L4_SPORT |\
1697 MLX5_HASH_FIELD_SEL_L4_DPORT)
1699 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
1700 MLX5_HASH_FIELD_SEL_DST_IP |\
1701 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
1703 mlx5e_build_tir_ctx_lro(tirc, priv);
1705 MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
1709 MLX5_SET(tirc, tirc, indirect_table,
1710 priv->rqtn[MLX5E_SINGLE_RQ_RQT]);
1711 MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
1714 MLX5_SET(tirc, tirc, indirect_table,
1715 priv->rqtn[MLX5E_INDIRECTION_RQT]);
1716 mlx5e_build_tir_ctx_hash(tirc, priv);
1721 case MLX5E_TT_IPV4_TCP:
1722 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1723 MLX5_L3_PROT_TYPE_IPV4);
1724 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1725 MLX5_L4_PROT_TYPE_TCP);
1726 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1727 MLX5_HASH_IP_L4PORTS);
1730 case MLX5E_TT_IPV6_TCP:
1731 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1732 MLX5_L3_PROT_TYPE_IPV6);
1733 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1734 MLX5_L4_PROT_TYPE_TCP);
1735 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1736 MLX5_HASH_IP_L4PORTS);
1739 case MLX5E_TT_IPV4_UDP:
1740 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1741 MLX5_L3_PROT_TYPE_IPV4);
1742 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1743 MLX5_L4_PROT_TYPE_UDP);
1744 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1745 MLX5_HASH_IP_L4PORTS);
1748 case MLX5E_TT_IPV6_UDP:
1749 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1750 MLX5_L3_PROT_TYPE_IPV6);
1751 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1752 MLX5_L4_PROT_TYPE_UDP);
1753 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1754 MLX5_HASH_IP_L4PORTS);
1757 case MLX5E_TT_IPV4_IPSEC_AH:
1758 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1759 MLX5_L3_PROT_TYPE_IPV4);
1760 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1761 MLX5_HASH_IP_IPSEC_SPI);
1764 case MLX5E_TT_IPV6_IPSEC_AH:
1765 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1766 MLX5_L3_PROT_TYPE_IPV6);
1767 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1768 MLX5_HASH_IP_IPSEC_SPI);
1771 case MLX5E_TT_IPV4_IPSEC_ESP:
1772 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1773 MLX5_L3_PROT_TYPE_IPV4);
1774 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1775 MLX5_HASH_IP_IPSEC_SPI);
1778 case MLX5E_TT_IPV6_IPSEC_ESP:
1779 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1780 MLX5_L3_PROT_TYPE_IPV6);
1781 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1782 MLX5_HASH_IP_IPSEC_SPI);
1786 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1787 MLX5_L3_PROT_TYPE_IPV4);
1788 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1793 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1794 MLX5_L3_PROT_TYPE_IPV6);
1795 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1801 static int mlx5e_create_tir(struct mlx5e_priv *priv, int tt)
1803 struct mlx5_core_dev *mdev = priv->mdev;
1809 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
1810 in = mlx5_vzalloc(inlen);
1814 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1816 mlx5e_build_tir_ctx(priv, tirc, tt);
1818 err = mlx5_core_create_tir(mdev, in, inlen, &priv->tirn[tt]);
1825 static void mlx5e_destroy_tir(struct mlx5e_priv *priv, int tt)
1827 mlx5_core_destroy_tir(priv->mdev, priv->tirn[tt]);
1830 static int mlx5e_create_tirs(struct mlx5e_priv *priv)
1835 for (i = 0; i < MLX5E_NUM_TT; i++) {
1836 err = mlx5e_create_tir(priv, i);
1838 goto err_destroy_tirs;
1844 for (i--; i >= 0; i--)
1845 mlx5e_destroy_tir(priv, i);
1850 static void mlx5e_destroy_tirs(struct mlx5e_priv *priv)
1854 for (i = 0; i < MLX5E_NUM_TT; i++)
1855 mlx5e_destroy_tir(priv, i);
1858 static int mlx5e_setup_tc(struct net_device *netdev, u8 tc)
1860 struct mlx5e_priv *priv = netdev_priv(netdev);
1864 if (tc && tc != MLX5E_MAX_NUM_TC)
1867 mutex_lock(&priv->state_lock);
1869 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
1871 mlx5e_close_locked(priv->netdev);
1873 priv->params.num_tc = tc ? tc : 1;
1876 err = mlx5e_open_locked(priv->netdev);
1878 mutex_unlock(&priv->state_lock);
1883 static int mlx5e_ndo_setup_tc(struct net_device *dev, u32 handle,
1884 __be16 proto, struct tc_to_netdev *tc)
1886 if (handle != TC_H_ROOT || tc->type != TC_SETUP_MQPRIO)
1889 return mlx5e_setup_tc(dev, tc->tc);
1892 static struct rtnl_link_stats64 *
1893 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
1895 struct mlx5e_priv *priv = netdev_priv(dev);
1896 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
1898 stats->rx_packets = vstats->rx_packets;
1899 stats->rx_bytes = vstats->rx_bytes;
1900 stats->tx_packets = vstats->tx_packets;
1901 stats->tx_bytes = vstats->tx_bytes;
1902 stats->multicast = vstats->rx_multicast_packets +
1903 vstats->tx_multicast_packets;
1904 stats->tx_errors = vstats->tx_error_packets;
1905 stats->rx_errors = vstats->rx_error_packets;
1906 stats->tx_dropped = vstats->tx_queue_dropped;
1907 stats->rx_crc_errors = 0;
1908 stats->rx_length_errors = 0;
1913 static void mlx5e_set_rx_mode(struct net_device *dev)
1915 struct mlx5e_priv *priv = netdev_priv(dev);
1917 schedule_work(&priv->set_rx_mode_work);
1920 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
1922 struct mlx5e_priv *priv = netdev_priv(netdev);
1923 struct sockaddr *saddr = addr;
1925 if (!is_valid_ether_addr(saddr->sa_data))
1926 return -EADDRNOTAVAIL;
1928 netif_addr_lock_bh(netdev);
1929 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
1930 netif_addr_unlock_bh(netdev);
1932 schedule_work(&priv->set_rx_mode_work);
1937 static int mlx5e_set_features(struct net_device *netdev,
1938 netdev_features_t features)
1940 struct mlx5e_priv *priv = netdev_priv(netdev);
1942 netdev_features_t changes = features ^ netdev->features;
1944 mutex_lock(&priv->state_lock);
1946 if (changes & NETIF_F_LRO) {
1947 bool was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
1950 mlx5e_close_locked(priv->netdev);
1952 priv->params.lro_en = !!(features & NETIF_F_LRO);
1953 err = mlx5e_modify_tirs_lro(priv);
1955 mlx5_core_warn(priv->mdev, "lro modify failed, %d\n",
1959 err = mlx5e_open_locked(priv->netdev);
1962 mutex_unlock(&priv->state_lock);
1964 if (changes & NETIF_F_HW_VLAN_CTAG_FILTER) {
1965 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
1966 mlx5e_enable_vlan_filter(priv);
1968 mlx5e_disable_vlan_filter(priv);
1974 static int mlx5e_change_mtu(struct net_device *netdev, int new_mtu)
1976 struct mlx5e_priv *priv = netdev_priv(netdev);
1977 struct mlx5_core_dev *mdev = priv->mdev;
1982 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
1984 max_mtu = MLX5E_HW2SW_MTU(max_mtu);
1986 if (new_mtu > max_mtu) {
1988 "%s: Bad MTU (%d) > (%d) Max\n",
1989 __func__, new_mtu, max_mtu);
1993 mutex_lock(&priv->state_lock);
1995 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
1997 mlx5e_close_locked(netdev);
1999 netdev->mtu = new_mtu;
2002 err = mlx5e_open_locked(netdev);
2004 mutex_unlock(&priv->state_lock);
2009 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2013 return mlx5e_hwstamp_set(dev, ifr);
2015 return mlx5e_hwstamp_get(dev, ifr);
2021 static int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2023 struct mlx5e_priv *priv = netdev_priv(dev);
2024 struct mlx5_core_dev *mdev = priv->mdev;
2026 return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
2029 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos)
2031 struct mlx5e_priv *priv = netdev_priv(dev);
2032 struct mlx5_core_dev *mdev = priv->mdev;
2034 return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
2038 static int mlx5_vport_link2ifla(u8 esw_link)
2041 case MLX5_ESW_VPORT_ADMIN_STATE_DOWN:
2042 return IFLA_VF_LINK_STATE_DISABLE;
2043 case MLX5_ESW_VPORT_ADMIN_STATE_UP:
2044 return IFLA_VF_LINK_STATE_ENABLE;
2046 return IFLA_VF_LINK_STATE_AUTO;
2049 static int mlx5_ifla_link2vport(u8 ifla_link)
2051 switch (ifla_link) {
2052 case IFLA_VF_LINK_STATE_DISABLE:
2053 return MLX5_ESW_VPORT_ADMIN_STATE_DOWN;
2054 case IFLA_VF_LINK_STATE_ENABLE:
2055 return MLX5_ESW_VPORT_ADMIN_STATE_UP;
2057 return MLX5_ESW_VPORT_ADMIN_STATE_AUTO;
2060 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
2063 struct mlx5e_priv *priv = netdev_priv(dev);
2064 struct mlx5_core_dev *mdev = priv->mdev;
2066 return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
2067 mlx5_ifla_link2vport(link_state));
2070 static int mlx5e_get_vf_config(struct net_device *dev,
2071 int vf, struct ifla_vf_info *ivi)
2073 struct mlx5e_priv *priv = netdev_priv(dev);
2074 struct mlx5_core_dev *mdev = priv->mdev;
2077 err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
2080 ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
2084 static int mlx5e_get_vf_stats(struct net_device *dev,
2085 int vf, struct ifla_vf_stats *vf_stats)
2087 struct mlx5e_priv *priv = netdev_priv(dev);
2088 struct mlx5_core_dev *mdev = priv->mdev;
2090 return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
2094 static void mlx5e_add_vxlan_port(struct net_device *netdev,
2095 sa_family_t sa_family, __be16 port)
2097 struct mlx5e_priv *priv = netdev_priv(netdev);
2099 if (!mlx5e_vxlan_allowed(priv->mdev))
2102 mlx5e_vxlan_add_port(priv, be16_to_cpu(port));
2105 static void mlx5e_del_vxlan_port(struct net_device *netdev,
2106 sa_family_t sa_family, __be16 port)
2108 struct mlx5e_priv *priv = netdev_priv(netdev);
2110 if (!mlx5e_vxlan_allowed(priv->mdev))
2113 mlx5e_vxlan_del_port(priv, be16_to_cpu(port));
2116 static netdev_features_t mlx5e_vxlan_features_check(struct mlx5e_priv *priv,
2117 struct sk_buff *skb,
2118 netdev_features_t features)
2120 struct udphdr *udph;
2124 switch (vlan_get_protocol(skb)) {
2125 case htons(ETH_P_IP):
2126 proto = ip_hdr(skb)->protocol;
2128 case htons(ETH_P_IPV6):
2129 proto = ipv6_hdr(skb)->nexthdr;
2135 if (proto == IPPROTO_UDP) {
2136 udph = udp_hdr(skb);
2137 port = be16_to_cpu(udph->dest);
2140 /* Verify if UDP port is being offloaded by HW */
2141 if (port && mlx5e_vxlan_lookup_port(priv, port))
2145 /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
2146 return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2149 static netdev_features_t mlx5e_features_check(struct sk_buff *skb,
2150 struct net_device *netdev,
2151 netdev_features_t features)
2153 struct mlx5e_priv *priv = netdev_priv(netdev);
2155 features = vlan_features_check(skb, features);
2156 features = vxlan_features_check(skb, features);
2158 /* Validate if the tunneled packet is being offloaded by HW */
2159 if (skb->encapsulation &&
2160 (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
2161 return mlx5e_vxlan_features_check(priv, skb, features);
2166 static const struct net_device_ops mlx5e_netdev_ops_basic = {
2167 .ndo_open = mlx5e_open,
2168 .ndo_stop = mlx5e_close,
2169 .ndo_start_xmit = mlx5e_xmit,
2170 .ndo_setup_tc = mlx5e_ndo_setup_tc,
2171 .ndo_select_queue = mlx5e_select_queue,
2172 .ndo_get_stats64 = mlx5e_get_stats,
2173 .ndo_set_rx_mode = mlx5e_set_rx_mode,
2174 .ndo_set_mac_address = mlx5e_set_mac,
2175 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
2176 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
2177 .ndo_set_features = mlx5e_set_features,
2178 .ndo_change_mtu = mlx5e_change_mtu,
2179 .ndo_do_ioctl = mlx5e_ioctl,
2182 static const struct net_device_ops mlx5e_netdev_ops_sriov = {
2183 .ndo_open = mlx5e_open,
2184 .ndo_stop = mlx5e_close,
2185 .ndo_start_xmit = mlx5e_xmit,
2186 .ndo_setup_tc = mlx5e_ndo_setup_tc,
2187 .ndo_select_queue = mlx5e_select_queue,
2188 .ndo_get_stats64 = mlx5e_get_stats,
2189 .ndo_set_rx_mode = mlx5e_set_rx_mode,
2190 .ndo_set_mac_address = mlx5e_set_mac,
2191 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
2192 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
2193 .ndo_set_features = mlx5e_set_features,
2194 .ndo_change_mtu = mlx5e_change_mtu,
2195 .ndo_do_ioctl = mlx5e_ioctl,
2196 .ndo_add_vxlan_port = mlx5e_add_vxlan_port,
2197 .ndo_del_vxlan_port = mlx5e_del_vxlan_port,
2198 .ndo_features_check = mlx5e_features_check,
2199 .ndo_set_vf_mac = mlx5e_set_vf_mac,
2200 .ndo_set_vf_vlan = mlx5e_set_vf_vlan,
2201 .ndo_get_vf_config = mlx5e_get_vf_config,
2202 .ndo_set_vf_link_state = mlx5e_set_vf_link_state,
2203 .ndo_get_vf_stats = mlx5e_get_vf_stats,
2206 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
2208 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
2210 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
2211 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
2212 !MLX5_CAP_ETH(mdev, csum_cap) ||
2213 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
2214 !MLX5_CAP_ETH(mdev, vlan_cap) ||
2215 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
2216 MLX5_CAP_FLOWTABLE(mdev,
2217 flow_table_properties_nic_receive.max_ft_level)
2219 mlx5_core_warn(mdev,
2220 "Not creating net device, some required device capabilities are missing\n");
2223 if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
2224 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
2225 if (!MLX5_CAP_GEN(mdev, cq_moderation))
2226 mlx5_core_warn(mdev, "CQ modiration is not supported\n");
2231 u16 mlx5e_get_max_inline_cap(struct mlx5_core_dev *mdev)
2233 int bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
2235 return bf_buf_size -
2236 sizeof(struct mlx5e_tx_wqe) +
2237 2 /*sizeof(mlx5e_tx_wqe.inline_hdr_start)*/;
2240 #ifdef CONFIG_MLX5_CORE_EN_DCB
2241 static void mlx5e_ets_init(struct mlx5e_priv *priv)
2245 priv->params.ets.ets_cap = mlx5_max_tc(priv->mdev) + 1;
2246 for (i = 0; i < priv->params.ets.ets_cap; i++) {
2247 priv->params.ets.tc_tx_bw[i] = MLX5E_MAX_BW_ALLOC;
2248 priv->params.ets.tc_tsa[i] = IEEE_8021QAZ_TSA_VENDOR;
2249 priv->params.ets.prio_tc[i] = i;
2252 /* tclass[prio=0]=1, tclass[prio=1]=0, tclass[prio=i]=i (for i>1) */
2253 priv->params.ets.prio_tc[0] = 1;
2254 priv->params.ets.prio_tc[1] = 0;
2258 void mlx5e_build_default_indir_rqt(u32 *indirection_rqt, int len,
2263 for (i = 0; i < len; i++)
2264 indirection_rqt[i] = i % num_channels;
2267 static void mlx5e_build_netdev_priv(struct mlx5_core_dev *mdev,
2268 struct net_device *netdev,
2271 struct mlx5e_priv *priv = netdev_priv(netdev);
2273 priv->params.log_sq_size =
2274 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
2275 priv->params.log_rq_size =
2276 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
2277 priv->params.rx_cq_moderation_usec =
2278 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
2279 priv->params.rx_cq_moderation_pkts =
2280 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
2281 priv->params.tx_cq_moderation_usec =
2282 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
2283 priv->params.tx_cq_moderation_pkts =
2284 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
2285 priv->params.tx_max_inline = mlx5e_get_max_inline_cap(mdev);
2286 priv->params.min_rx_wqes =
2287 MLX5E_PARAMS_DEFAULT_MIN_RX_WQES;
2288 priv->params.num_tc = 1;
2289 priv->params.rss_hfunc = ETH_RSS_HASH_XOR;
2291 netdev_rss_key_fill(priv->params.toeplitz_hash_key,
2292 sizeof(priv->params.toeplitz_hash_key));
2294 mlx5e_build_default_indir_rqt(priv->params.indirection_rqt,
2295 MLX5E_INDIR_RQT_SIZE, num_channels);
2297 priv->params.lro_wqe_sz =
2298 MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
2301 priv->netdev = netdev;
2302 priv->params.num_channels = num_channels;
2304 #ifdef CONFIG_MLX5_CORE_EN_DCB
2305 mlx5e_ets_init(priv);
2308 mutex_init(&priv->state_lock);
2310 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
2311 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
2312 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
2315 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
2317 struct mlx5e_priv *priv = netdev_priv(netdev);
2319 mlx5_query_nic_vport_mac_address(priv->mdev, 0, netdev->dev_addr);
2320 if (is_zero_ether_addr(netdev->dev_addr) &&
2321 !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
2322 eth_hw_addr_random(netdev);
2323 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
2327 static void mlx5e_build_netdev(struct net_device *netdev)
2329 struct mlx5e_priv *priv = netdev_priv(netdev);
2330 struct mlx5_core_dev *mdev = priv->mdev;
2332 SET_NETDEV_DEV(netdev, &mdev->pdev->dev);
2334 if (MLX5_CAP_GEN(mdev, vport_group_manager)) {
2335 netdev->netdev_ops = &mlx5e_netdev_ops_sriov;
2336 #ifdef CONFIG_MLX5_CORE_EN_DCB
2337 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
2340 netdev->netdev_ops = &mlx5e_netdev_ops_basic;
2343 netdev->watchdog_timeo = 15 * HZ;
2345 netdev->ethtool_ops = &mlx5e_ethtool_ops;
2347 netdev->vlan_features |= NETIF_F_SG;
2348 netdev->vlan_features |= NETIF_F_IP_CSUM;
2349 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
2350 netdev->vlan_features |= NETIF_F_GRO;
2351 netdev->vlan_features |= NETIF_F_TSO;
2352 netdev->vlan_features |= NETIF_F_TSO6;
2353 netdev->vlan_features |= NETIF_F_RXCSUM;
2354 netdev->vlan_features |= NETIF_F_RXHASH;
2356 if (!!MLX5_CAP_ETH(mdev, lro_cap))
2357 netdev->vlan_features |= NETIF_F_LRO;
2359 netdev->hw_features = netdev->vlan_features;
2360 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_TX;
2361 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
2362 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
2364 if (mlx5e_vxlan_allowed(mdev)) {
2365 netdev->hw_features |= NETIF_F_GSO_UDP_TUNNEL;
2366 netdev->hw_enc_features |= NETIF_F_IP_CSUM;
2367 netdev->hw_enc_features |= NETIF_F_RXCSUM;
2368 netdev->hw_enc_features |= NETIF_F_TSO;
2369 netdev->hw_enc_features |= NETIF_F_TSO6;
2370 netdev->hw_enc_features |= NETIF_F_RXHASH;
2371 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL;
2374 netdev->features = netdev->hw_features;
2375 if (!priv->params.lro_en)
2376 netdev->features &= ~NETIF_F_LRO;
2378 netdev->features |= NETIF_F_HIGHDMA;
2380 netdev->priv_flags |= IFF_UNICAST_FLT;
2382 mlx5e_set_netdev_dev_addr(netdev);
2385 static int mlx5e_create_mkey(struct mlx5e_priv *priv, u32 pdn,
2386 struct mlx5_core_mr *mr)
2388 struct mlx5_core_dev *mdev = priv->mdev;
2389 struct mlx5_create_mkey_mbox_in *in;
2392 in = mlx5_vzalloc(sizeof(*in));
2396 in->seg.flags = MLX5_PERM_LOCAL_WRITE |
2397 MLX5_PERM_LOCAL_READ |
2398 MLX5_ACCESS_MODE_PA;
2399 in->seg.flags_pd = cpu_to_be32(pdn | MLX5_MKEY_LEN64);
2400 in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
2402 err = mlx5_core_create_mkey(mdev, mr, in, sizeof(*in), NULL, NULL,
2410 static void *mlx5e_create_netdev(struct mlx5_core_dev *mdev)
2412 struct net_device *netdev;
2413 struct mlx5e_priv *priv;
2414 int nch = mlx5e_get_max_num_channels(mdev);
2417 if (mlx5e_check_required_hca_cap(mdev))
2420 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
2421 nch * MLX5E_MAX_NUM_TC,
2424 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
2428 mlx5e_build_netdev_priv(mdev, netdev, nch);
2429 mlx5e_build_netdev(netdev);
2431 netif_carrier_off(netdev);
2433 priv = netdev_priv(netdev);
2435 err = mlx5_alloc_map_uar(mdev, &priv->cq_uar, false);
2437 mlx5_core_err(mdev, "alloc_map uar failed, %d\n", err);
2438 goto err_free_netdev;
2441 err = mlx5_core_alloc_pd(mdev, &priv->pdn);
2443 mlx5_core_err(mdev, "alloc pd failed, %d\n", err);
2444 goto err_unmap_free_uar;
2447 err = mlx5_core_alloc_transport_domain(mdev, &priv->tdn);
2449 mlx5_core_err(mdev, "alloc td failed, %d\n", err);
2450 goto err_dealloc_pd;
2453 err = mlx5e_create_mkey(priv, priv->pdn, &priv->mr);
2455 mlx5_core_err(mdev, "create mkey failed, %d\n", err);
2456 goto err_dealloc_transport_domain;
2459 err = mlx5e_create_tises(priv);
2461 mlx5_core_warn(mdev, "create tises failed, %d\n", err);
2462 goto err_destroy_mkey;
2465 err = mlx5e_open_drop_rq(priv);
2467 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
2468 goto err_destroy_tises;
2471 err = mlx5e_create_rqt(priv, MLX5E_INDIRECTION_RQT);
2473 mlx5_core_warn(mdev, "create rqt(INDIR) failed, %d\n", err);
2474 goto err_close_drop_rq;
2477 err = mlx5e_create_rqt(priv, MLX5E_SINGLE_RQ_RQT);
2479 mlx5_core_warn(mdev, "create rqt(SINGLE) failed, %d\n", err);
2480 goto err_destroy_rqt_indir;
2483 err = mlx5e_create_tirs(priv);
2485 mlx5_core_warn(mdev, "create tirs failed, %d\n", err);
2486 goto err_destroy_rqt_single;
2489 err = mlx5e_create_flow_tables(priv);
2491 mlx5_core_warn(mdev, "create flow tables failed, %d\n", err);
2492 goto err_destroy_tirs;
2495 mlx5e_init_eth_addr(priv);
2497 mlx5e_vxlan_init(priv);
2499 #ifdef CONFIG_MLX5_CORE_EN_DCB
2500 mlx5e_dcbnl_ieee_setets_core(priv, &priv->params.ets);
2503 err = register_netdev(netdev);
2505 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
2506 goto err_destroy_flow_tables;
2509 if (mlx5e_vxlan_allowed(mdev))
2510 vxlan_get_rx_port(netdev);
2512 mlx5e_enable_async_events(priv);
2513 schedule_work(&priv->set_rx_mode_work);
2517 err_destroy_flow_tables:
2518 mlx5e_destroy_flow_tables(priv);
2521 mlx5e_destroy_tirs(priv);
2523 err_destroy_rqt_single:
2524 mlx5e_destroy_rqt(priv, MLX5E_SINGLE_RQ_RQT);
2526 err_destroy_rqt_indir:
2527 mlx5e_destroy_rqt(priv, MLX5E_INDIRECTION_RQT);
2530 mlx5e_close_drop_rq(priv);
2533 mlx5e_destroy_tises(priv);
2536 mlx5_core_destroy_mkey(mdev, &priv->mr);
2538 err_dealloc_transport_domain:
2539 mlx5_core_dealloc_transport_domain(mdev, priv->tdn);
2542 mlx5_core_dealloc_pd(mdev, priv->pdn);
2545 mlx5_unmap_free_uar(mdev, &priv->cq_uar);
2548 free_netdev(netdev);
2553 static void mlx5e_destroy_netdev(struct mlx5_core_dev *mdev, void *vpriv)
2555 struct mlx5e_priv *priv = vpriv;
2556 struct net_device *netdev = priv->netdev;
2558 set_bit(MLX5E_STATE_DESTROYING, &priv->state);
2560 schedule_work(&priv->set_rx_mode_work);
2561 mlx5e_disable_async_events(priv);
2562 flush_scheduled_work();
2563 unregister_netdev(netdev);
2564 mlx5e_vxlan_cleanup(priv);
2565 mlx5e_destroy_flow_tables(priv);
2566 mlx5e_destroy_tirs(priv);
2567 mlx5e_destroy_rqt(priv, MLX5E_SINGLE_RQ_RQT);
2568 mlx5e_destroy_rqt(priv, MLX5E_INDIRECTION_RQT);
2569 mlx5e_close_drop_rq(priv);
2570 mlx5e_destroy_tises(priv);
2571 mlx5_core_destroy_mkey(priv->mdev, &priv->mr);
2572 mlx5_core_dealloc_transport_domain(priv->mdev, priv->tdn);
2573 mlx5_core_dealloc_pd(priv->mdev, priv->pdn);
2574 mlx5_unmap_free_uar(priv->mdev, &priv->cq_uar);
2575 free_netdev(netdev);
2578 static void *mlx5e_get_netdev(void *vpriv)
2580 struct mlx5e_priv *priv = vpriv;
2582 return priv->netdev;
2585 static struct mlx5_interface mlx5e_interface = {
2586 .add = mlx5e_create_netdev,
2587 .remove = mlx5e_destroy_netdev,
2588 .event = mlx5e_async_event,
2589 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
2590 .get_dev = mlx5e_get_netdev,
2593 void mlx5e_init(void)
2595 mlx5_register_interface(&mlx5e_interface);
2598 void mlx5e_cleanup(void)
2600 mlx5_unregister_interface(&mlx5e_interface);