2 * Copyright (c) 2015, 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/flow_table.h>
36 struct mlx5e_rq_param {
37 u32 rqc[MLX5_ST_SZ_DW(rqc)];
38 struct mlx5_wq_param wq;
41 struct mlx5e_sq_param {
42 u32 sqc[MLX5_ST_SZ_DW(sqc)];
43 struct mlx5_wq_param wq;
46 struct mlx5e_cq_param {
47 u32 cqc[MLX5_ST_SZ_DW(cqc)];
48 struct mlx5_wq_param wq;
52 struct mlx5e_channel_param {
53 struct mlx5e_rq_param rq;
54 struct mlx5e_sq_param sq;
55 struct mlx5e_cq_param rx_cq;
56 struct mlx5e_cq_param tx_cq;
59 static void mlx5e_update_carrier(struct mlx5e_priv *priv)
61 struct mlx5_core_dev *mdev = priv->mdev;
64 port_state = mlx5_query_vport_state(mdev,
65 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT);
67 if (port_state == VPORT_STATE_UP)
68 netif_carrier_on(priv->netdev);
70 netif_carrier_off(priv->netdev);
73 static void mlx5e_update_carrier_work(struct work_struct *work)
75 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
78 mutex_lock(&priv->state_lock);
79 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
80 mlx5e_update_carrier(priv);
81 mutex_unlock(&priv->state_lock);
84 void mlx5e_update_stats(struct mlx5e_priv *priv)
86 struct mlx5_core_dev *mdev = priv->mdev;
87 struct mlx5e_vport_stats *s = &priv->stats.vport;
88 struct mlx5e_rq_stats *rq_stats;
89 struct mlx5e_sq_stats *sq_stats;
90 u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)];
92 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
96 out = mlx5_vzalloc(outlen);
100 /* Collect firts the SW counters and then HW for consistency */
103 s->tx_queue_stopped = 0;
104 s->tx_queue_wake = 0;
105 s->tx_queue_dropped = 0;
111 for (i = 0; i < priv->params.num_channels; i++) {
112 rq_stats = &priv->channel[i]->rq.stats;
114 s->lro_packets += rq_stats->lro_packets;
115 s->lro_bytes += rq_stats->lro_bytes;
116 s->rx_csum_none += rq_stats->csum_none;
117 s->rx_wqe_err += rq_stats->wqe_err;
119 for (j = 0; j < priv->num_tc; j++) {
120 sq_stats = &priv->channel[i]->sq[j].stats;
122 s->tso_packets += sq_stats->tso_packets;
123 s->tso_bytes += sq_stats->tso_bytes;
124 s->tx_queue_stopped += sq_stats->stopped;
125 s->tx_queue_wake += sq_stats->wake;
126 s->tx_queue_dropped += sq_stats->dropped;
127 tx_offload_none += sq_stats->csum_offload_none;
132 memset(in, 0, sizeof(in));
134 MLX5_SET(query_vport_counter_in, in, opcode,
135 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
136 MLX5_SET(query_vport_counter_in, in, op_mod, 0);
137 MLX5_SET(query_vport_counter_in, in, other_vport, 0);
139 memset(out, 0, outlen);
141 if (mlx5_cmd_exec(mdev, in, sizeof(in), out, outlen))
144 #define MLX5_GET_CTR(p, x) \
145 MLX5_GET64(query_vport_counter_out, p, x)
147 s->rx_error_packets =
148 MLX5_GET_CTR(out, received_errors.packets);
150 MLX5_GET_CTR(out, received_errors.octets);
151 s->tx_error_packets =
152 MLX5_GET_CTR(out, transmit_errors.packets);
154 MLX5_GET_CTR(out, transmit_errors.octets);
156 s->rx_unicast_packets =
157 MLX5_GET_CTR(out, received_eth_unicast.packets);
158 s->rx_unicast_bytes =
159 MLX5_GET_CTR(out, received_eth_unicast.octets);
160 s->tx_unicast_packets =
161 MLX5_GET_CTR(out, transmitted_eth_unicast.packets);
162 s->tx_unicast_bytes =
163 MLX5_GET_CTR(out, transmitted_eth_unicast.octets);
165 s->rx_multicast_packets =
166 MLX5_GET_CTR(out, received_eth_multicast.packets);
167 s->rx_multicast_bytes =
168 MLX5_GET_CTR(out, received_eth_multicast.octets);
169 s->tx_multicast_packets =
170 MLX5_GET_CTR(out, transmitted_eth_multicast.packets);
171 s->tx_multicast_bytes =
172 MLX5_GET_CTR(out, transmitted_eth_multicast.octets);
174 s->rx_broadcast_packets =
175 MLX5_GET_CTR(out, received_eth_broadcast.packets);
176 s->rx_broadcast_bytes =
177 MLX5_GET_CTR(out, received_eth_broadcast.octets);
178 s->tx_broadcast_packets =
179 MLX5_GET_CTR(out, transmitted_eth_broadcast.packets);
180 s->tx_broadcast_bytes =
181 MLX5_GET_CTR(out, transmitted_eth_broadcast.octets);
184 s->rx_unicast_packets +
185 s->rx_multicast_packets +
186 s->rx_broadcast_packets;
188 s->rx_unicast_bytes +
189 s->rx_multicast_bytes +
190 s->rx_broadcast_bytes;
192 s->tx_unicast_packets +
193 s->tx_multicast_packets +
194 s->tx_broadcast_packets;
196 s->tx_unicast_bytes +
197 s->tx_multicast_bytes +
198 s->tx_broadcast_bytes;
200 /* Update calculated offload counters */
201 s->tx_csum_offload = s->tx_packets - tx_offload_none;
202 s->rx_csum_good = s->rx_packets - s->rx_csum_none;
208 static void mlx5e_update_stats_work(struct work_struct *work)
210 struct delayed_work *dwork = to_delayed_work(work);
211 struct mlx5e_priv *priv = container_of(dwork, struct mlx5e_priv,
213 mutex_lock(&priv->state_lock);
214 if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
215 mlx5e_update_stats(priv);
216 schedule_delayed_work(dwork,
218 MLX5E_UPDATE_STATS_INTERVAL));
220 mutex_unlock(&priv->state_lock);
223 static void __mlx5e_async_event(struct mlx5e_priv *priv,
224 enum mlx5_dev_event event)
227 case MLX5_DEV_EVENT_PORT_UP:
228 case MLX5_DEV_EVENT_PORT_DOWN:
229 schedule_work(&priv->update_carrier_work);
237 static void mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
238 enum mlx5_dev_event event, unsigned long param)
240 struct mlx5e_priv *priv = vpriv;
242 spin_lock(&priv->async_events_spinlock);
243 if (test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state))
244 __mlx5e_async_event(priv, event);
245 spin_unlock(&priv->async_events_spinlock);
248 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
250 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state);
253 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
255 spin_lock_irq(&priv->async_events_spinlock);
256 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state);
257 spin_unlock_irq(&priv->async_events_spinlock);
260 static void mlx5e_send_nop(struct mlx5e_sq *sq)
262 struct mlx5_wq_cyc *wq = &sq->wq;
264 u16 pi = sq->pc & wq->sz_m1;
265 struct mlx5e_tx_wqe *wqe = mlx5_wq_cyc_get_wqe(wq, pi);
267 struct mlx5_wqe_ctrl_seg *cseg = &wqe->ctrl;
269 memset(cseg, 0, sizeof(*cseg));
271 cseg->opmod_idx_opcode = cpu_to_be32((sq->pc << 8) | MLX5_OPCODE_NOP);
272 cseg->qpn_ds = cpu_to_be32((sq->sqn << 8) | 0x01);
273 cseg->fm_ce_se = MLX5_WQE_CTRL_CQ_UPDATE;
277 mlx5e_tx_notify_hw(sq, wqe);
280 #define MLX5E_HW2SW_MTU(hwmtu) (hwmtu - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
281 #define MLX5E_SW2HW_MTU(swmtu) (swmtu + (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
283 static int mlx5e_create_rq(struct mlx5e_channel *c,
284 struct mlx5e_rq_param *param,
287 struct mlx5e_priv *priv = c->priv;
288 struct mlx5_core_dev *mdev = priv->mdev;
289 void *rqc = param->rqc;
290 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
295 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
300 rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
302 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
303 rq->skb = kzalloc_node(wq_sz * sizeof(*rq->skb), GFP_KERNEL,
304 cpu_to_node(c->cpu));
307 goto err_rq_wq_destroy;
310 rq->wqe_sz = (priv->params.lro_en) ? priv->params.lro_wqe_sz :
311 MLX5E_SW2HW_MTU(priv->netdev->mtu);
312 rq->wqe_sz = SKB_DATA_ALIGN(rq->wqe_sz + MLX5E_NET_IP_ALIGN);
314 for (i = 0; i < wq_sz; i++) {
315 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
316 u32 byte_count = rq->wqe_sz - MLX5E_NET_IP_ALIGN;
318 wqe->data.lkey = c->mkey_be;
319 wqe->data.byte_count =
320 cpu_to_be32(byte_count | MLX5_HW_START_PADDING);
324 rq->netdev = c->netdev;
331 mlx5_wq_destroy(&rq->wq_ctrl);
336 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
339 mlx5_wq_destroy(&rq->wq_ctrl);
342 static int mlx5e_enable_rq(struct mlx5e_rq *rq, struct mlx5e_rq_param *param)
344 struct mlx5e_channel *c = rq->channel;
345 struct mlx5e_priv *priv = c->priv;
346 struct mlx5_core_dev *mdev = priv->mdev;
354 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
355 sizeof(u64) * rq->wq_ctrl.buf.npages;
356 in = mlx5_vzalloc(inlen);
360 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
361 wq = MLX5_ADDR_OF(rqc, rqc, wq);
363 memcpy(rqc, param->rqc, sizeof(param->rqc));
365 MLX5_SET(rqc, rqc, cqn, c->rq.cq.mcq.cqn);
366 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
367 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
368 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
369 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
371 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
373 mlx5_fill_page_array(&rq->wq_ctrl.buf,
374 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
376 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
383 static int mlx5e_modify_rq(struct mlx5e_rq *rq, int curr_state, int next_state)
385 struct mlx5e_channel *c = rq->channel;
386 struct mlx5e_priv *priv = c->priv;
387 struct mlx5_core_dev *mdev = priv->mdev;
394 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
395 in = mlx5_vzalloc(inlen);
399 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
401 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
402 MLX5_SET(rqc, rqc, state, next_state);
404 err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
411 static void mlx5e_disable_rq(struct mlx5e_rq *rq)
413 struct mlx5e_channel *c = rq->channel;
414 struct mlx5e_priv *priv = c->priv;
415 struct mlx5_core_dev *mdev = priv->mdev;
417 mlx5_core_destroy_rq(mdev, rq->rqn);
420 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq)
422 struct mlx5e_channel *c = rq->channel;
423 struct mlx5e_priv *priv = c->priv;
424 struct mlx5_wq_ll *wq = &rq->wq;
427 for (i = 0; i < 1000; i++) {
428 if (wq->cur_sz >= priv->params.min_rx_wqes)
437 static int mlx5e_open_rq(struct mlx5e_channel *c,
438 struct mlx5e_rq_param *param,
443 err = mlx5e_create_rq(c, param, rq);
447 err = mlx5e_enable_rq(rq, param);
451 err = mlx5e_modify_rq(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
455 set_bit(MLX5E_RQ_STATE_POST_WQES_ENABLE, &rq->state);
456 mlx5e_send_nop(&c->sq[0]); /* trigger mlx5e_post_rx_wqes() */
461 mlx5e_disable_rq(rq);
463 mlx5e_destroy_rq(rq);
468 static void mlx5e_close_rq(struct mlx5e_rq *rq)
470 clear_bit(MLX5E_RQ_STATE_POST_WQES_ENABLE, &rq->state);
471 napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
473 mlx5e_modify_rq(rq, MLX5_RQC_STATE_RDY, MLX5_RQC_STATE_ERR);
474 while (!mlx5_wq_ll_is_empty(&rq->wq))
477 /* avoid destroying rq before mlx5e_poll_rx_cq() is done with it */
478 napi_synchronize(&rq->channel->napi);
480 mlx5e_disable_rq(rq);
481 mlx5e_destroy_rq(rq);
484 static void mlx5e_free_sq_db(struct mlx5e_sq *sq)
490 static int mlx5e_alloc_sq_db(struct mlx5e_sq *sq, int numa)
492 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
493 int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
495 sq->skb = kzalloc_node(wq_sz * sizeof(*sq->skb), GFP_KERNEL, numa);
496 sq->dma_fifo = kzalloc_node(df_sz * sizeof(*sq->dma_fifo), GFP_KERNEL,
499 if (!sq->skb || !sq->dma_fifo) {
500 mlx5e_free_sq_db(sq);
504 sq->dma_fifo_mask = df_sz - 1;
509 static int mlx5e_create_sq(struct mlx5e_channel *c,
511 struct mlx5e_sq_param *param,
514 struct mlx5e_priv *priv = c->priv;
515 struct mlx5_core_dev *mdev = priv->mdev;
517 void *sqc = param->sqc;
518 void *sqc_wq = MLX5_ADDR_OF(sqc, sqc, wq);
521 err = mlx5_alloc_map_uar(mdev, &sq->uar);
525 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq,
528 goto err_unmap_free_uar;
530 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
531 sq->uar_map = sq->uar.map;
532 sq->bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
534 err = mlx5e_alloc_sq_db(sq, cpu_to_node(c->cpu));
536 goto err_sq_wq_destroy;
538 sq->txq = netdev_get_tx_queue(priv->netdev,
539 c->ix + tc * priv->params.num_channels);
542 sq->mkey_be = c->mkey_be;
549 mlx5_wq_destroy(&sq->wq_ctrl);
552 mlx5_unmap_free_uar(mdev, &sq->uar);
557 static void mlx5e_destroy_sq(struct mlx5e_sq *sq)
559 struct mlx5e_channel *c = sq->channel;
560 struct mlx5e_priv *priv = c->priv;
562 mlx5e_free_sq_db(sq);
563 mlx5_wq_destroy(&sq->wq_ctrl);
564 mlx5_unmap_free_uar(priv->mdev, &sq->uar);
567 static int mlx5e_enable_sq(struct mlx5e_sq *sq, struct mlx5e_sq_param *param)
569 struct mlx5e_channel *c = sq->channel;
570 struct mlx5e_priv *priv = c->priv;
571 struct mlx5_core_dev *mdev = priv->mdev;
579 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
580 sizeof(u64) * sq->wq_ctrl.buf.npages;
581 in = mlx5_vzalloc(inlen);
585 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
586 wq = MLX5_ADDR_OF(sqc, sqc, wq);
588 memcpy(sqc, param->sqc, sizeof(param->sqc));
590 MLX5_SET(sqc, sqc, user_index, sq->tc);
591 MLX5_SET(sqc, sqc, tis_num_0, priv->tisn[sq->tc]);
592 MLX5_SET(sqc, sqc, cqn, c->sq[sq->tc].cq.mcq.cqn);
593 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
594 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
595 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
597 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
598 MLX5_SET(wq, wq, uar_page, sq->uar.index);
599 MLX5_SET(wq, wq, log_wq_pg_sz, sq->wq_ctrl.buf.page_shift -
601 MLX5_SET64(wq, wq, dbr_addr, sq->wq_ctrl.db.dma);
603 mlx5_fill_page_array(&sq->wq_ctrl.buf,
604 (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
606 err = mlx5_core_create_sq(mdev, in, inlen, &sq->sqn);
613 static int mlx5e_modify_sq(struct mlx5e_sq *sq, int curr_state, int next_state)
615 struct mlx5e_channel *c = sq->channel;
616 struct mlx5e_priv *priv = c->priv;
617 struct mlx5_core_dev *mdev = priv->mdev;
624 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
625 in = mlx5_vzalloc(inlen);
629 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
631 MLX5_SET(modify_sq_in, in, sq_state, curr_state);
632 MLX5_SET(sqc, sqc, state, next_state);
634 err = mlx5_core_modify_sq(mdev, sq->sqn, in, inlen);
641 static void mlx5e_disable_sq(struct mlx5e_sq *sq)
643 struct mlx5e_channel *c = sq->channel;
644 struct mlx5e_priv *priv = c->priv;
645 struct mlx5_core_dev *mdev = priv->mdev;
647 mlx5_core_destroy_sq(mdev, sq->sqn);
650 static int mlx5e_open_sq(struct mlx5e_channel *c,
652 struct mlx5e_sq_param *param,
657 err = mlx5e_create_sq(c, tc, param, sq);
661 err = mlx5e_enable_sq(sq, param);
665 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST, MLX5_SQC_STATE_RDY);
669 set_bit(MLX5E_SQ_STATE_WAKE_TXQ_ENABLE, &sq->state);
670 netdev_tx_reset_queue(sq->txq);
671 netif_tx_start_queue(sq->txq);
676 mlx5e_disable_sq(sq);
678 mlx5e_destroy_sq(sq);
683 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
685 __netif_tx_lock_bh(txq);
686 netif_tx_stop_queue(txq);
687 __netif_tx_unlock_bh(txq);
690 static void mlx5e_close_sq(struct mlx5e_sq *sq)
692 clear_bit(MLX5E_SQ_STATE_WAKE_TXQ_ENABLE, &sq->state);
693 napi_synchronize(&sq->channel->napi); /* prevent netif_tx_wake_queue */
694 netif_tx_disable_queue(sq->txq);
696 /* ensure hw is notified of all pending wqes */
697 if (mlx5e_sq_has_room_for(sq, 1))
700 mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY, MLX5_SQC_STATE_ERR);
701 while (sq->cc != sq->pc) /* wait till sq is empty */
704 /* avoid destroying sq before mlx5e_poll_tx_cq() is done with it */
705 napi_synchronize(&sq->channel->napi);
707 mlx5e_disable_sq(sq);
708 mlx5e_destroy_sq(sq);
711 static int mlx5e_create_cq(struct mlx5e_channel *c,
712 struct mlx5e_cq_param *param,
715 struct mlx5e_priv *priv = c->priv;
716 struct mlx5_core_dev *mdev = priv->mdev;
717 struct mlx5_core_cq *mcq = &cq->mcq;
723 param->wq.numa = cpu_to_node(c->cpu);
724 param->eq_ix = c->ix;
726 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
731 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
736 mcq->set_ci_db = cq->wq_ctrl.db.db;
737 mcq->arm_db = cq->wq_ctrl.db.db + 1;
740 mcq->vector = param->eq_ix;
741 mcq->comp = mlx5e_completion_event;
742 mcq->event = mlx5e_cq_error_event;
744 mcq->uar = &priv->cq_uar;
746 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
747 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
757 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
759 mlx5_wq_destroy(&cq->wq_ctrl);
762 static int mlx5e_enable_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
764 struct mlx5e_channel *c = cq->channel;
765 struct mlx5e_priv *priv = c->priv;
766 struct mlx5_core_dev *mdev = priv->mdev;
767 struct mlx5_core_cq *mcq = &cq->mcq;
776 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
777 sizeof(u64) * cq->wq_ctrl.buf.npages;
778 in = mlx5_vzalloc(inlen);
782 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
784 memcpy(cqc, param->cqc, sizeof(param->cqc));
786 mlx5_fill_page_array(&cq->wq_ctrl.buf,
787 (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
789 mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
791 MLX5_SET(cqc, cqc, c_eqn, eqn);
792 MLX5_SET(cqc, cqc, uar_page, mcq->uar->index);
793 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
795 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
797 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
809 static void mlx5e_disable_cq(struct mlx5e_cq *cq)
811 struct mlx5e_channel *c = cq->channel;
812 struct mlx5e_priv *priv = c->priv;
813 struct mlx5_core_dev *mdev = priv->mdev;
815 mlx5_core_destroy_cq(mdev, &cq->mcq);
818 static int mlx5e_open_cq(struct mlx5e_channel *c,
819 struct mlx5e_cq_param *param,
821 u16 moderation_usecs,
822 u16 moderation_frames)
825 struct mlx5e_priv *priv = c->priv;
826 struct mlx5_core_dev *mdev = priv->mdev;
828 err = mlx5e_create_cq(c, param, cq);
832 err = mlx5e_enable_cq(cq, param);
836 err = mlx5_core_modify_cq_moderation(mdev, &cq->mcq,
845 mlx5e_destroy_cq(cq);
850 static void mlx5e_close_cq(struct mlx5e_cq *cq)
852 mlx5e_disable_cq(cq);
853 mlx5e_destroy_cq(cq);
856 static int mlx5e_get_cpu(struct mlx5e_priv *priv, int ix)
858 return cpumask_first(priv->mdev->priv.irq_info[ix].mask);
861 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
862 struct mlx5e_channel_param *cparam)
864 struct mlx5e_priv *priv = c->priv;
868 for (tc = 0; tc < c->num_tc; tc++) {
869 err = mlx5e_open_cq(c, &cparam->tx_cq, &c->sq[tc].cq,
870 priv->params.tx_cq_moderation_usec,
871 priv->params.tx_cq_moderation_pkts);
873 goto err_close_tx_cqs;
875 c->sq[tc].cq.sqrq = &c->sq[tc];
881 for (tc--; tc >= 0; tc--)
882 mlx5e_close_cq(&c->sq[tc].cq);
887 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
891 for (tc = 0; tc < c->num_tc; tc++)
892 mlx5e_close_cq(&c->sq[tc].cq);
895 static int mlx5e_open_sqs(struct mlx5e_channel *c,
896 struct mlx5e_channel_param *cparam)
901 for (tc = 0; tc < c->num_tc; tc++) {
902 err = mlx5e_open_sq(c, tc, &cparam->sq, &c->sq[tc]);
910 for (tc--; tc >= 0; tc--)
911 mlx5e_close_sq(&c->sq[tc]);
916 static void mlx5e_close_sqs(struct mlx5e_channel *c)
920 for (tc = 0; tc < c->num_tc; tc++)
921 mlx5e_close_sq(&c->sq[tc]);
924 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
925 struct mlx5e_channel_param *cparam,
926 struct mlx5e_channel **cp)
928 struct net_device *netdev = priv->netdev;
929 int cpu = mlx5e_get_cpu(priv, ix);
930 struct mlx5e_channel *c;
933 c = kzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
940 c->pdev = &priv->mdev->pdev->dev;
941 c->netdev = priv->netdev;
942 c->mkey_be = cpu_to_be32(priv->mr.key);
943 c->num_tc = priv->num_tc;
945 netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
947 err = mlx5e_open_tx_cqs(c, cparam);
951 err = mlx5e_open_cq(c, &cparam->rx_cq, &c->rq.cq,
952 priv->params.rx_cq_moderation_usec,
953 priv->params.rx_cq_moderation_pkts);
955 goto err_close_tx_cqs;
956 c->rq.cq.sqrq = &c->rq;
958 napi_enable(&c->napi);
960 err = mlx5e_open_sqs(c, cparam);
962 goto err_disable_napi;
964 err = mlx5e_open_rq(c, &cparam->rq, &c->rq);
968 netif_set_xps_queue(netdev, get_cpu_mask(c->cpu), ix);
977 napi_disable(&c->napi);
978 mlx5e_close_cq(&c->rq.cq);
981 mlx5e_close_tx_cqs(c);
984 netif_napi_del(&c->napi);
990 static void mlx5e_close_channel(struct mlx5e_channel *c)
992 mlx5e_close_rq(&c->rq);
994 napi_disable(&c->napi);
995 mlx5e_close_cq(&c->rq.cq);
996 mlx5e_close_tx_cqs(c);
997 netif_napi_del(&c->napi);
1001 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
1002 struct mlx5e_rq_param *param)
1004 void *rqc = param->rqc;
1005 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1007 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1008 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1009 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1010 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_rq_size);
1011 MLX5_SET(wq, wq, pd, priv->pdn);
1013 param->wq.numa = dev_to_node(&priv->mdev->pdev->dev);
1014 param->wq.linear = 1;
1017 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
1018 struct mlx5e_sq_param *param)
1020 void *sqc = param->sqc;
1021 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1023 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_sq_size);
1024 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1025 MLX5_SET(wq, wq, pd, priv->pdn);
1027 param->wq.numa = dev_to_node(&priv->mdev->pdev->dev);
1030 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1031 struct mlx5e_cq_param *param)
1033 void *cqc = param->cqc;
1035 MLX5_SET(cqc, cqc, uar_page, priv->cq_uar.index);
1038 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
1039 struct mlx5e_cq_param *param)
1041 void *cqc = param->cqc;
1043 MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_rq_size);
1045 mlx5e_build_common_cq_param(priv, param);
1048 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
1049 struct mlx5e_cq_param *param)
1051 void *cqc = param->cqc;
1053 MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_sq_size);
1055 mlx5e_build_common_cq_param(priv, param);
1058 static void mlx5e_build_channel_param(struct mlx5e_priv *priv,
1059 struct mlx5e_channel_param *cparam)
1061 memset(cparam, 0, sizeof(*cparam));
1063 mlx5e_build_rq_param(priv, &cparam->rq);
1064 mlx5e_build_sq_param(priv, &cparam->sq);
1065 mlx5e_build_rx_cq_param(priv, &cparam->rx_cq);
1066 mlx5e_build_tx_cq_param(priv, &cparam->tx_cq);
1069 static int mlx5e_open_channels(struct mlx5e_priv *priv)
1071 struct mlx5e_channel_param cparam;
1076 priv->channel = kcalloc(priv->params.num_channels,
1077 sizeof(struct mlx5e_channel *), GFP_KERNEL);
1081 mlx5e_build_channel_param(priv, &cparam);
1082 for (i = 0; i < priv->params.num_channels; i++) {
1083 err = mlx5e_open_channel(priv, i, &cparam, &priv->channel[i]);
1085 goto err_close_channels;
1088 for (j = 0; j < priv->params.num_channels; j++) {
1089 err = mlx5e_wait_for_min_rx_wqes(&priv->channel[j]->rq);
1091 goto err_close_channels;
1097 for (i--; i >= 0; i--)
1098 mlx5e_close_channel(priv->channel[i]);
1100 kfree(priv->channel);
1105 static void mlx5e_close_channels(struct mlx5e_priv *priv)
1109 for (i = 0; i < priv->params.num_channels; i++)
1110 mlx5e_close_channel(priv->channel[i]);
1112 kfree(priv->channel);
1115 static int mlx5e_open_tis(struct mlx5e_priv *priv, int tc)
1117 struct mlx5_core_dev *mdev = priv->mdev;
1118 u32 in[MLX5_ST_SZ_DW(create_tis_in)];
1119 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1121 memset(in, 0, sizeof(in));
1123 MLX5_SET(tisc, tisc, prio, tc);
1125 return mlx5_core_create_tis(mdev, in, sizeof(in), &priv->tisn[tc]);
1128 static void mlx5e_close_tis(struct mlx5e_priv *priv, int tc)
1130 mlx5_core_destroy_tis(priv->mdev, priv->tisn[tc]);
1133 static int mlx5e_open_tises(struct mlx5e_priv *priv)
1135 int num_tc = priv->num_tc;
1139 for (tc = 0; tc < num_tc; tc++) {
1140 err = mlx5e_open_tis(priv, tc);
1142 goto err_close_tises;
1148 for (tc--; tc >= 0; tc--)
1149 mlx5e_close_tis(priv, tc);
1154 static void mlx5e_close_tises(struct mlx5e_priv *priv)
1156 int num_tc = priv->num_tc;
1159 for (tc = 0; tc < num_tc; tc++)
1160 mlx5e_close_tis(priv, tc);
1163 static int mlx5e_open_rqt(struct mlx5e_priv *priv)
1165 struct mlx5_core_dev *mdev = priv->mdev;
1167 u32 out[MLX5_ST_SZ_DW(create_rqt_out)];
1174 sz = 1 << priv->params.rx_hash_log_tbl_sz;
1176 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
1177 in = mlx5_vzalloc(inlen);
1181 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
1183 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1184 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
1186 for (i = 0; i < sz; i++) {
1187 int ix = i % priv->params.num_channels;
1189 MLX5_SET(rqtc, rqtc, rq_num[i], priv->channel[ix]->rq.rqn);
1192 MLX5_SET(create_rqt_in, in, opcode, MLX5_CMD_OP_CREATE_RQT);
1194 memset(out, 0, sizeof(out));
1195 err = mlx5_cmd_exec_check_status(mdev, in, inlen, out, sizeof(out));
1197 priv->rqtn = MLX5_GET(create_rqt_out, out, rqtn);
1204 static void mlx5e_close_rqt(struct mlx5e_priv *priv)
1206 u32 in[MLX5_ST_SZ_DW(destroy_rqt_in)];
1207 u32 out[MLX5_ST_SZ_DW(destroy_rqt_out)];
1209 memset(in, 0, sizeof(in));
1211 MLX5_SET(destroy_rqt_in, in, opcode, MLX5_CMD_OP_DESTROY_RQT);
1212 MLX5_SET(destroy_rqt_in, in, rqtn, priv->rqtn);
1214 mlx5_cmd_exec_check_status(priv->mdev, in, sizeof(in), out,
1218 static void mlx5e_build_tir_ctx(struct mlx5e_priv *priv, u32 *tirc, int tt)
1220 void *hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1222 #define ROUGH_MAX_L2_L3_HDR_SZ 256
1224 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
1225 MLX5_HASH_FIELD_SEL_DST_IP)
1227 #define MLX5_HASH_ALL (MLX5_HASH_FIELD_SEL_SRC_IP |\
1228 MLX5_HASH_FIELD_SEL_DST_IP |\
1229 MLX5_HASH_FIELD_SEL_L4_SPORT |\
1230 MLX5_HASH_FIELD_SEL_L4_DPORT)
1232 if (priv->params.lro_en) {
1233 MLX5_SET(tirc, tirc, lro_enable_mask,
1234 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
1235 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
1236 MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
1237 (priv->params.lro_wqe_sz -
1238 ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
1239 MLX5_SET(tirc, tirc, lro_timeout_period_usecs,
1240 MLX5_CAP_ETH(priv->mdev,
1241 lro_timer_supported_periods[3]));
1246 MLX5_SET(tirc, tirc, disp_type,
1247 MLX5_TIRC_DISP_TYPE_DIRECT);
1248 MLX5_SET(tirc, tirc, inline_rqn,
1249 priv->channel[0]->rq.rqn);
1252 MLX5_SET(tirc, tirc, disp_type,
1253 MLX5_TIRC_DISP_TYPE_INDIRECT);
1254 MLX5_SET(tirc, tirc, indirect_table,
1256 MLX5_SET(tirc, tirc, rx_hash_fn,
1257 MLX5_TIRC_RX_HASH_FN_HASH_TOEPLITZ);
1258 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1259 netdev_rss_key_fill(MLX5_ADDR_OF(tirc, tirc,
1260 rx_hash_toeplitz_key),
1261 MLX5_FLD_SZ_BYTES(tirc,
1262 rx_hash_toeplitz_key));
1267 case MLX5E_TT_IPV4_TCP:
1268 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1269 MLX5_L3_PROT_TYPE_IPV4);
1270 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1271 MLX5_L4_PROT_TYPE_TCP);
1272 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1276 case MLX5E_TT_IPV6_TCP:
1277 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1278 MLX5_L3_PROT_TYPE_IPV6);
1279 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1280 MLX5_L4_PROT_TYPE_TCP);
1281 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1285 case MLX5E_TT_IPV4_UDP:
1286 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1287 MLX5_L3_PROT_TYPE_IPV4);
1288 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1289 MLX5_L4_PROT_TYPE_UDP);
1290 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1294 case MLX5E_TT_IPV6_UDP:
1295 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1296 MLX5_L3_PROT_TYPE_IPV6);
1297 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1298 MLX5_L4_PROT_TYPE_UDP);
1299 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1304 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1305 MLX5_L3_PROT_TYPE_IPV4);
1306 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1311 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1312 MLX5_L3_PROT_TYPE_IPV6);
1313 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1319 static int mlx5e_open_tir(struct mlx5e_priv *priv, int tt)
1321 struct mlx5_core_dev *mdev = priv->mdev;
1327 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
1328 in = mlx5_vzalloc(inlen);
1332 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
1334 mlx5e_build_tir_ctx(priv, tirc, tt);
1336 err = mlx5_core_create_tir(mdev, in, inlen, &priv->tirn[tt]);
1343 static void mlx5e_close_tir(struct mlx5e_priv *priv, int tt)
1345 mlx5_core_destroy_tir(priv->mdev, priv->tirn[tt]);
1348 static int mlx5e_open_tirs(struct mlx5e_priv *priv)
1353 for (i = 0; i < MLX5E_NUM_TT; i++) {
1354 err = mlx5e_open_tir(priv, i);
1356 goto err_close_tirs;
1362 for (i--; i >= 0; i--)
1363 mlx5e_close_tir(priv, i);
1368 static void mlx5e_close_tirs(struct mlx5e_priv *priv)
1372 for (i = 0; i < MLX5E_NUM_TT; i++)
1373 mlx5e_close_tir(priv, i);
1376 static int mlx5e_set_dev_port_mtu(struct net_device *netdev)
1378 struct mlx5e_priv *priv = netdev_priv(netdev);
1379 struct mlx5_core_dev *mdev = priv->mdev;
1383 err = mlx5_set_port_mtu(mdev, MLX5E_SW2HW_MTU(netdev->mtu), 1);
1387 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
1389 if (MLX5E_HW2SW_MTU(hw_mtu) != netdev->mtu)
1390 netdev_warn(netdev, "%s: Port MTU %d is different than netdev mtu %d\n",
1391 __func__, MLX5E_HW2SW_MTU(hw_mtu), netdev->mtu);
1393 netdev->mtu = MLX5E_HW2SW_MTU(hw_mtu);
1397 int mlx5e_open_locked(struct net_device *netdev)
1399 struct mlx5e_priv *priv = netdev_priv(netdev);
1403 num_txqs = roundup_pow_of_two(priv->params.num_channels) *
1404 priv->params.num_tc;
1405 netif_set_real_num_tx_queues(netdev, num_txqs);
1406 netif_set_real_num_rx_queues(netdev, priv->params.num_channels);
1408 err = mlx5e_set_dev_port_mtu(netdev);
1412 err = mlx5e_open_tises(priv);
1414 netdev_err(netdev, "%s: mlx5e_open_tises failed, %d\n",
1419 err = mlx5e_open_channels(priv);
1421 netdev_err(netdev, "%s: mlx5e_open_channels failed, %d\n",
1423 goto err_close_tises;
1426 err = mlx5e_open_rqt(priv);
1428 netdev_err(netdev, "%s: mlx5e_open_rqt failed, %d\n",
1430 goto err_close_channels;
1433 err = mlx5e_open_tirs(priv);
1435 netdev_err(netdev, "%s: mlx5e_open_tir failed, %d\n",
1437 goto err_close_rqls;
1440 err = mlx5e_open_flow_table(priv);
1442 netdev_err(netdev, "%s: mlx5e_open_flow_table failed, %d\n",
1444 goto err_close_tirs;
1447 err = mlx5e_add_all_vlan_rules(priv);
1449 netdev_err(netdev, "%s: mlx5e_add_all_vlan_rules failed, %d\n",
1451 goto err_close_flow_table;
1454 mlx5e_init_eth_addr(priv);
1456 set_bit(MLX5E_STATE_OPENED, &priv->state);
1458 mlx5e_update_carrier(priv);
1459 mlx5e_set_rx_mode_core(priv);
1461 schedule_delayed_work(&priv->update_stats_work, 0);
1464 err_close_flow_table:
1465 mlx5e_close_flow_table(priv);
1468 mlx5e_close_tirs(priv);
1471 mlx5e_close_rqt(priv);
1474 mlx5e_close_channels(priv);
1477 mlx5e_close_tises(priv);
1482 static int mlx5e_open(struct net_device *netdev)
1484 struct mlx5e_priv *priv = netdev_priv(netdev);
1487 mutex_lock(&priv->state_lock);
1488 err = mlx5e_open_locked(netdev);
1489 mutex_unlock(&priv->state_lock);
1494 int mlx5e_close_locked(struct net_device *netdev)
1496 struct mlx5e_priv *priv = netdev_priv(netdev);
1498 clear_bit(MLX5E_STATE_OPENED, &priv->state);
1500 mlx5e_set_rx_mode_core(priv);
1501 mlx5e_del_all_vlan_rules(priv);
1502 netif_carrier_off(priv->netdev);
1503 mlx5e_close_flow_table(priv);
1504 mlx5e_close_tirs(priv);
1505 mlx5e_close_rqt(priv);
1506 mlx5e_close_channels(priv);
1507 mlx5e_close_tises(priv);
1512 static int mlx5e_close(struct net_device *netdev)
1514 struct mlx5e_priv *priv = netdev_priv(netdev);
1517 mutex_lock(&priv->state_lock);
1518 err = mlx5e_close_locked(netdev);
1519 mutex_unlock(&priv->state_lock);
1524 int mlx5e_update_priv_params(struct mlx5e_priv *priv,
1525 struct mlx5e_params *new_params)
1530 WARN_ON(!mutex_is_locked(&priv->state_lock));
1532 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
1534 mlx5e_close_locked(priv->netdev);
1536 priv->params = *new_params;
1539 err = mlx5e_open_locked(priv->netdev);
1544 static struct rtnl_link_stats64 *
1545 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
1547 struct mlx5e_priv *priv = netdev_priv(dev);
1548 struct mlx5e_vport_stats *vstats = &priv->stats.vport;
1550 stats->rx_packets = vstats->rx_packets;
1551 stats->rx_bytes = vstats->rx_bytes;
1552 stats->tx_packets = vstats->tx_packets;
1553 stats->tx_bytes = vstats->tx_bytes;
1554 stats->multicast = vstats->rx_multicast_packets +
1555 vstats->tx_multicast_packets;
1556 stats->tx_errors = vstats->tx_error_packets;
1557 stats->rx_errors = vstats->rx_error_packets;
1558 stats->tx_dropped = vstats->tx_queue_dropped;
1559 stats->rx_crc_errors = 0;
1560 stats->rx_length_errors = 0;
1565 static void mlx5e_set_rx_mode(struct net_device *dev)
1567 struct mlx5e_priv *priv = netdev_priv(dev);
1569 schedule_work(&priv->set_rx_mode_work);
1572 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
1574 struct mlx5e_priv *priv = netdev_priv(netdev);
1575 struct sockaddr *saddr = addr;
1577 if (!is_valid_ether_addr(saddr->sa_data))
1578 return -EADDRNOTAVAIL;
1580 netif_addr_lock_bh(netdev);
1581 ether_addr_copy(netdev->dev_addr, saddr->sa_data);
1582 netif_addr_unlock_bh(netdev);
1584 schedule_work(&priv->set_rx_mode_work);
1589 static int mlx5e_set_features(struct net_device *netdev,
1590 netdev_features_t features)
1592 struct mlx5e_priv *priv = netdev_priv(netdev);
1593 netdev_features_t changes = features ^ netdev->features;
1594 struct mlx5e_params new_params;
1595 bool update_params = false;
1597 mutex_lock(&priv->state_lock);
1598 new_params = priv->params;
1600 if (changes & NETIF_F_LRO) {
1601 new_params.lro_en = !!(features & NETIF_F_LRO);
1602 update_params = true;
1606 mlx5e_update_priv_params(priv, &new_params);
1608 if (changes & NETIF_F_HW_VLAN_CTAG_FILTER) {
1609 if (features & NETIF_F_HW_VLAN_CTAG_FILTER)
1610 mlx5e_enable_vlan_filter(priv);
1612 mlx5e_disable_vlan_filter(priv);
1615 mutex_unlock(&priv->state_lock);
1620 static int mlx5e_change_mtu(struct net_device *netdev, int new_mtu)
1622 struct mlx5e_priv *priv = netdev_priv(netdev);
1623 struct mlx5_core_dev *mdev = priv->mdev;
1627 mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
1629 if (new_mtu > max_mtu) {
1631 "%s: Bad MTU (%d) > (%d) Max\n",
1632 __func__, new_mtu, max_mtu);
1636 mutex_lock(&priv->state_lock);
1637 netdev->mtu = new_mtu;
1638 err = mlx5e_update_priv_params(priv, &priv->params);
1639 mutex_unlock(&priv->state_lock);
1644 static struct net_device_ops mlx5e_netdev_ops = {
1645 .ndo_open = mlx5e_open,
1646 .ndo_stop = mlx5e_close,
1647 .ndo_start_xmit = mlx5e_xmit,
1648 .ndo_get_stats64 = mlx5e_get_stats,
1649 .ndo_set_rx_mode = mlx5e_set_rx_mode,
1650 .ndo_set_mac_address = mlx5e_set_mac,
1651 .ndo_vlan_rx_add_vid = mlx5e_vlan_rx_add_vid,
1652 .ndo_vlan_rx_kill_vid = mlx5e_vlan_rx_kill_vid,
1653 .ndo_set_features = mlx5e_set_features,
1654 .ndo_change_mtu = mlx5e_change_mtu,
1657 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
1659 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
1661 if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
1662 !MLX5_CAP_GEN(mdev, nic_flow_table) ||
1663 !MLX5_CAP_ETH(mdev, csum_cap) ||
1664 !MLX5_CAP_ETH(mdev, max_lso_cap) ||
1665 !MLX5_CAP_ETH(mdev, vlan_cap) ||
1666 !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
1667 MLX5_CAP_FLOWTABLE(mdev,
1668 flow_table_properties_nic_receive.max_ft_level)
1670 mlx5_core_warn(mdev,
1671 "Not creating net device, some required device capabilities are missing\n");
1677 static void mlx5e_build_netdev_priv(struct mlx5_core_dev *mdev,
1678 struct net_device *netdev,
1679 int num_comp_vectors)
1681 struct mlx5e_priv *priv = netdev_priv(netdev);
1683 priv->params.log_sq_size =
1684 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
1685 priv->params.log_rq_size =
1686 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
1687 priv->params.rx_cq_moderation_usec =
1688 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
1689 priv->params.rx_cq_moderation_pkts =
1690 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
1691 priv->params.tx_cq_moderation_usec =
1692 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
1693 priv->params.tx_cq_moderation_pkts =
1694 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
1695 priv->params.min_rx_wqes =
1696 MLX5E_PARAMS_DEFAULT_MIN_RX_WQES;
1697 priv->params.rx_hash_log_tbl_sz =
1698 (order_base_2(num_comp_vectors) >
1699 MLX5E_PARAMS_DEFAULT_RX_HASH_LOG_TBL_SZ) ?
1700 order_base_2(num_comp_vectors) :
1701 MLX5E_PARAMS_DEFAULT_RX_HASH_LOG_TBL_SZ;
1702 priv->params.num_tc = 1;
1703 priv->params.default_vlan_prio = 0;
1705 priv->params.lro_en = false && !!MLX5_CAP_ETH(priv->mdev, lro_cap);
1706 priv->params.lro_wqe_sz =
1707 MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
1710 priv->netdev = netdev;
1711 priv->params.num_channels = num_comp_vectors;
1712 priv->order_base_2_num_channels = order_base_2(num_comp_vectors);
1713 priv->queue_mapping_channel_mask =
1714 roundup_pow_of_two(num_comp_vectors) - 1;
1715 priv->num_tc = priv->params.num_tc;
1716 priv->default_vlan_prio = priv->params.default_vlan_prio;
1718 spin_lock_init(&priv->async_events_spinlock);
1719 mutex_init(&priv->state_lock);
1721 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
1722 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
1723 INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
1726 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
1728 struct mlx5e_priv *priv = netdev_priv(netdev);
1730 mlx5_query_nic_vport_mac_address(priv->mdev, netdev->dev_addr);
1733 static void mlx5e_build_netdev(struct net_device *netdev)
1735 struct mlx5e_priv *priv = netdev_priv(netdev);
1736 struct mlx5_core_dev *mdev = priv->mdev;
1738 SET_NETDEV_DEV(netdev, &mdev->pdev->dev);
1740 if (priv->num_tc > 1) {
1741 mlx5e_netdev_ops.ndo_select_queue = mlx5e_select_queue;
1742 mlx5e_netdev_ops.ndo_start_xmit = mlx5e_xmit_multi_tc;
1745 netdev->netdev_ops = &mlx5e_netdev_ops;
1746 netdev->watchdog_timeo = 15 * HZ;
1748 netdev->ethtool_ops = &mlx5e_ethtool_ops;
1750 netdev->vlan_features |= NETIF_F_IP_CSUM;
1751 netdev->vlan_features |= NETIF_F_IPV6_CSUM;
1752 netdev->vlan_features |= NETIF_F_GRO;
1753 netdev->vlan_features |= NETIF_F_TSO;
1754 netdev->vlan_features |= NETIF_F_TSO6;
1755 netdev->vlan_features |= NETIF_F_RXCSUM;
1756 netdev->vlan_features |= NETIF_F_RXHASH;
1758 if (!!MLX5_CAP_ETH(mdev, lro_cap))
1759 netdev->vlan_features |= NETIF_F_LRO;
1761 netdev->hw_features = netdev->vlan_features;
1762 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_RX;
1763 netdev->hw_features |= NETIF_F_HW_VLAN_CTAG_FILTER;
1765 netdev->features = netdev->hw_features;
1766 if (!priv->params.lro_en)
1767 netdev->features &= ~NETIF_F_LRO;
1769 netdev->features |= NETIF_F_HIGHDMA;
1771 netdev->priv_flags |= IFF_UNICAST_FLT;
1773 mlx5e_set_netdev_dev_addr(netdev);
1776 static int mlx5e_create_mkey(struct mlx5e_priv *priv, u32 pdn,
1777 struct mlx5_core_mr *mr)
1779 struct mlx5_core_dev *mdev = priv->mdev;
1780 struct mlx5_create_mkey_mbox_in *in;
1783 in = mlx5_vzalloc(sizeof(*in));
1787 in->seg.flags = MLX5_PERM_LOCAL_WRITE |
1788 MLX5_PERM_LOCAL_READ |
1789 MLX5_ACCESS_MODE_PA;
1790 in->seg.flags_pd = cpu_to_be32(pdn | MLX5_MKEY_LEN64);
1791 in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
1793 err = mlx5_core_create_mkey(mdev, mr, in, sizeof(*in), NULL, NULL,
1801 static void *mlx5e_create_netdev(struct mlx5_core_dev *mdev)
1803 struct net_device *netdev;
1804 struct mlx5e_priv *priv;
1805 int ncv = mdev->priv.eq_table.num_comp_vectors;
1808 if (mlx5e_check_required_hca_cap(mdev))
1811 netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
1812 roundup_pow_of_two(ncv) * MLX5E_MAX_NUM_TC,
1815 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
1819 mlx5e_build_netdev_priv(mdev, netdev, ncv);
1820 mlx5e_build_netdev(netdev);
1822 netif_carrier_off(netdev);
1824 priv = netdev_priv(netdev);
1826 err = mlx5_alloc_map_uar(mdev, &priv->cq_uar);
1828 netdev_err(netdev, "%s: mlx5_alloc_map_uar failed, %d\n",
1830 goto err_free_netdev;
1833 err = mlx5_core_alloc_pd(mdev, &priv->pdn);
1835 netdev_err(netdev, "%s: mlx5_core_alloc_pd failed, %d\n",
1837 goto err_unmap_free_uar;
1840 err = mlx5e_create_mkey(priv, priv->pdn, &priv->mr);
1842 netdev_err(netdev, "%s: mlx5e_create_mkey failed, %d\n",
1844 goto err_dealloc_pd;
1847 err = register_netdev(netdev);
1849 netdev_err(netdev, "%s: register_netdev failed, %d\n",
1851 goto err_destroy_mkey;
1854 mlx5e_enable_async_events(priv);
1859 mlx5_core_destroy_mkey(mdev, &priv->mr);
1862 mlx5_core_dealloc_pd(mdev, priv->pdn);
1865 mlx5_unmap_free_uar(mdev, &priv->cq_uar);
1868 free_netdev(netdev);
1873 static void mlx5e_destroy_netdev(struct mlx5_core_dev *mdev, void *vpriv)
1875 struct mlx5e_priv *priv = vpriv;
1876 struct net_device *netdev = priv->netdev;
1878 unregister_netdev(netdev);
1879 mlx5_core_destroy_mkey(priv->mdev, &priv->mr);
1880 mlx5_core_dealloc_pd(priv->mdev, priv->pdn);
1881 mlx5_unmap_free_uar(priv->mdev, &priv->cq_uar);
1882 mlx5e_disable_async_events(priv);
1883 flush_scheduled_work();
1884 free_netdev(netdev);
1887 static void *mlx5e_get_netdev(void *vpriv)
1889 struct mlx5e_priv *priv = vpriv;
1891 return priv->netdev;
1894 static struct mlx5_interface mlx5e_interface = {
1895 .add = mlx5e_create_netdev,
1896 .remove = mlx5e_destroy_netdev,
1897 .event = mlx5e_async_event,
1898 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
1899 .get_dev = mlx5e_get_netdev,
1902 void mlx5e_init(void)
1904 mlx5_register_interface(&mlx5e_interface);
1907 void mlx5e_cleanup(void)
1909 mlx5_unregister_interface(&mlx5e_interface);