2 * Copyright (c) 2015-2018 Mellanox Technologies. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY AUTHOR AND CONTRIBUTORS `AS IS' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/sockio.h>
31 #include <machine/atomic.h>
33 #ifndef ETH_DRIVER_VERSION
34 #define ETH_DRIVER_VERSION "3.5.0"
36 #define DRIVER_RELDATE "November 2018"
38 static const char mlx5e_version[] = "mlx5en: Mellanox Ethernet driver "
39 ETH_DRIVER_VERSION " (" DRIVER_RELDATE ")\n";
41 static int mlx5e_get_wqe_sz(struct mlx5e_priv *priv, u32 *wqe_sz, u32 *nsegs);
43 struct mlx5e_channel_param {
44 struct mlx5e_rq_param rq;
45 struct mlx5e_sq_param sq;
46 struct mlx5e_cq_param rx_cq;
47 struct mlx5e_cq_param tx_cq;
53 } mlx5e_mode_table[MLX5E_LINK_MODES_NUMBER] = {
55 [MLX5E_1000BASE_CX_SGMII] = {
56 .subtype = IFM_1000_CX_SGMII,
57 .baudrate = IF_Mbps(1000ULL),
59 [MLX5E_1000BASE_KX] = {
60 .subtype = IFM_1000_KX,
61 .baudrate = IF_Mbps(1000ULL),
63 [MLX5E_10GBASE_CX4] = {
64 .subtype = IFM_10G_CX4,
65 .baudrate = IF_Gbps(10ULL),
67 [MLX5E_10GBASE_KX4] = {
68 .subtype = IFM_10G_KX4,
69 .baudrate = IF_Gbps(10ULL),
71 [MLX5E_10GBASE_KR] = {
72 .subtype = IFM_10G_KR,
73 .baudrate = IF_Gbps(10ULL),
75 [MLX5E_20GBASE_KR2] = {
76 .subtype = IFM_20G_KR2,
77 .baudrate = IF_Gbps(20ULL),
79 [MLX5E_40GBASE_CR4] = {
80 .subtype = IFM_40G_CR4,
81 .baudrate = IF_Gbps(40ULL),
83 [MLX5E_40GBASE_KR4] = {
84 .subtype = IFM_40G_KR4,
85 .baudrate = IF_Gbps(40ULL),
87 [MLX5E_56GBASE_R4] = {
88 .subtype = IFM_56G_R4,
89 .baudrate = IF_Gbps(56ULL),
91 [MLX5E_10GBASE_CR] = {
92 .subtype = IFM_10G_CR1,
93 .baudrate = IF_Gbps(10ULL),
95 [MLX5E_10GBASE_SR] = {
96 .subtype = IFM_10G_SR,
97 .baudrate = IF_Gbps(10ULL),
99 [MLX5E_10GBASE_ER] = {
100 .subtype = IFM_10G_ER,
101 .baudrate = IF_Gbps(10ULL),
103 [MLX5E_40GBASE_SR4] = {
104 .subtype = IFM_40G_SR4,
105 .baudrate = IF_Gbps(40ULL),
107 [MLX5E_40GBASE_LR4] = {
108 .subtype = IFM_40G_LR4,
109 .baudrate = IF_Gbps(40ULL),
111 [MLX5E_100GBASE_CR4] = {
112 .subtype = IFM_100G_CR4,
113 .baudrate = IF_Gbps(100ULL),
115 [MLX5E_100GBASE_SR4] = {
116 .subtype = IFM_100G_SR4,
117 .baudrate = IF_Gbps(100ULL),
119 [MLX5E_100GBASE_KR4] = {
120 .subtype = IFM_100G_KR4,
121 .baudrate = IF_Gbps(100ULL),
123 [MLX5E_100GBASE_LR4] = {
124 .subtype = IFM_100G_LR4,
125 .baudrate = IF_Gbps(100ULL),
127 [MLX5E_100BASE_TX] = {
128 .subtype = IFM_100_TX,
129 .baudrate = IF_Mbps(100ULL),
131 [MLX5E_1000BASE_T] = {
132 .subtype = IFM_1000_T,
133 .baudrate = IF_Mbps(1000ULL),
135 [MLX5E_10GBASE_T] = {
136 .subtype = IFM_10G_T,
137 .baudrate = IF_Gbps(10ULL),
139 [MLX5E_25GBASE_CR] = {
140 .subtype = IFM_25G_CR,
141 .baudrate = IF_Gbps(25ULL),
143 [MLX5E_25GBASE_KR] = {
144 .subtype = IFM_25G_KR,
145 .baudrate = IF_Gbps(25ULL),
147 [MLX5E_25GBASE_SR] = {
148 .subtype = IFM_25G_SR,
149 .baudrate = IF_Gbps(25ULL),
151 [MLX5E_50GBASE_CR2] = {
152 .subtype = IFM_50G_CR2,
153 .baudrate = IF_Gbps(50ULL),
155 [MLX5E_50GBASE_KR2] = {
156 .subtype = IFM_50G_KR2,
157 .baudrate = IF_Gbps(50ULL),
161 MALLOC_DEFINE(M_MLX5EN, "MLX5EN", "MLX5 Ethernet");
164 mlx5e_update_carrier(struct mlx5e_priv *priv)
166 struct mlx5_core_dev *mdev = priv->mdev;
167 u32 out[MLX5_ST_SZ_DW(ptys_reg)];
174 port_state = mlx5_query_vport_state(mdev,
175 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT, 0);
177 if (port_state == VPORT_STATE_UP) {
178 priv->media_status_last |= IFM_ACTIVE;
180 priv->media_status_last &= ~IFM_ACTIVE;
181 priv->media_active_last = IFM_ETHER;
182 if_link_state_change(priv->ifp, LINK_STATE_DOWN);
186 error = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN, 1);
188 priv->media_active_last = IFM_ETHER;
189 priv->ifp->if_baudrate = 1;
190 if_printf(priv->ifp, "%s: query port ptys failed: 0x%x\n",
194 eth_proto_oper = MLX5_GET(ptys_reg, out, eth_proto_oper);
196 for (i = 0; i != MLX5E_LINK_MODES_NUMBER; i++) {
197 if (mlx5e_mode_table[i].baudrate == 0)
199 if (MLX5E_PROT_MASK(i) & eth_proto_oper) {
200 u32 subtype = mlx5e_mode_table[i].subtype;
202 priv->ifp->if_baudrate =
203 mlx5e_mode_table[i].baudrate;
207 error = mlx5_query_pddr_range_info(mdev, 1, &is_er_type);
209 if_printf(priv->ifp, "%s: query port pddr failed: %d\n",
212 if (error != 0 || is_er_type == 0)
213 subtype = IFM_10G_LR;
216 error = mlx5_query_pddr_range_info(mdev, 1, &is_er_type);
218 if_printf(priv->ifp, "%s: query port pddr failed: %d\n",
221 if (error == 0 && is_er_type != 0)
222 subtype = IFM_40G_ER4;
225 priv->media_active_last = subtype | IFM_ETHER | IFM_FDX;
229 if_link_state_change(priv->ifp, LINK_STATE_UP);
233 mlx5e_media_status(struct ifnet *dev, struct ifmediareq *ifmr)
235 struct mlx5e_priv *priv = dev->if_softc;
237 ifmr->ifm_status = priv->media_status_last;
238 ifmr->ifm_active = priv->media_active_last |
239 (priv->params.rx_pauseframe_control ? IFM_ETH_RXPAUSE : 0) |
240 (priv->params.tx_pauseframe_control ? IFM_ETH_TXPAUSE : 0);
245 mlx5e_find_link_mode(u32 subtype)
252 subtype = IFM_10G_ER;
255 subtype = IFM_40G_LR4;
259 for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
260 if (mlx5e_mode_table[i].baudrate == 0)
262 if (mlx5e_mode_table[i].subtype == subtype)
263 link_mode |= MLX5E_PROT_MASK(i);
270 mlx5e_set_port_pause_and_pfc(struct mlx5e_priv *priv)
272 return (mlx5_set_port_pause_and_pfc(priv->mdev, 1,
273 priv->params.rx_pauseframe_control,
274 priv->params.tx_pauseframe_control,
275 priv->params.rx_priority_flow_control,
276 priv->params.tx_priority_flow_control));
280 mlx5e_set_port_pfc(struct mlx5e_priv *priv)
284 if (priv->params.rx_pauseframe_control ||
285 priv->params.tx_pauseframe_control) {
287 "Global pauseframes must be disabled before enabling PFC.\n");
290 error = mlx5e_set_port_pause_and_pfc(priv);
296 mlx5e_media_change(struct ifnet *dev)
298 struct mlx5e_priv *priv = dev->if_softc;
299 struct mlx5_core_dev *mdev = priv->mdev;
306 locked = PRIV_LOCKED(priv);
310 if (IFM_TYPE(priv->media.ifm_media) != IFM_ETHER) {
314 link_mode = mlx5e_find_link_mode(IFM_SUBTYPE(priv->media.ifm_media));
316 /* query supported capabilities */
317 error = mlx5_query_port_proto_cap(mdev, ð_proto_cap, MLX5_PTYS_EN);
319 if_printf(dev, "Query port media capability failed\n");
322 /* check for autoselect */
323 if (IFM_SUBTYPE(priv->media.ifm_media) == IFM_AUTO) {
324 link_mode = eth_proto_cap;
325 if (link_mode == 0) {
326 if_printf(dev, "Port media capability is zero\n");
331 link_mode = link_mode & eth_proto_cap;
332 if (link_mode == 0) {
333 if_printf(dev, "Not supported link mode requested\n");
338 if (priv->media.ifm_media & (IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE)) {
339 /* check if PFC is enabled */
340 if (priv->params.rx_priority_flow_control ||
341 priv->params.tx_priority_flow_control) {
342 if_printf(dev, "PFC must be disabled before enabling global pauseframes.\n");
347 /* update pauseframe control bits */
348 priv->params.rx_pauseframe_control =
349 (priv->media.ifm_media & IFM_ETH_RXPAUSE) ? 1 : 0;
350 priv->params.tx_pauseframe_control =
351 (priv->media.ifm_media & IFM_ETH_TXPAUSE) ? 1 : 0;
353 /* check if device is opened */
354 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
356 /* reconfigure the hardware */
357 mlx5_set_port_status(mdev, MLX5_PORT_DOWN);
358 mlx5_set_port_proto(mdev, link_mode, MLX5_PTYS_EN);
359 error = -mlx5e_set_port_pause_and_pfc(priv);
361 mlx5_set_port_status(mdev, MLX5_PORT_UP);
370 mlx5e_update_carrier_work(struct work_struct *work)
372 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
373 update_carrier_work);
376 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
377 mlx5e_update_carrier(priv);
382 * This function reads the physical port counters from the firmware
383 * using a pre-defined layout defined by various MLX5E_PPORT_XXX()
384 * macros. The output is converted from big-endian 64-bit values into
385 * host endian ones and stored in the "priv->stats.pport" structure.
388 mlx5e_update_pport_counters(struct mlx5e_priv *priv)
390 struct mlx5_core_dev *mdev = priv->mdev;
391 struct mlx5e_pport_stats *s = &priv->stats.pport;
392 struct mlx5e_port_stats_debug *s_debug = &priv->stats.port_stats_debug;
396 unsigned sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
401 /* allocate firmware request structures */
402 in = mlx5_vzalloc(sz);
403 out = mlx5_vzalloc(sz);
404 if (in == NULL || out == NULL)
408 * Get pointer to the 64-bit counter set which is located at a
409 * fixed offset in the output firmware request structure:
411 ptr = (const uint64_t *)MLX5_ADDR_OF(ppcnt_reg, out, counter_set);
413 MLX5_SET(ppcnt_reg, in, local_port, 1);
415 /* read IEEE802_3 counter group using predefined counter layout */
416 MLX5_SET(ppcnt_reg, in, grp, MLX5_IEEE_802_3_COUNTERS_GROUP);
417 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
418 for (x = 0, y = MLX5E_PPORT_PER_PRIO_STATS_NUM;
419 x != MLX5E_PPORT_IEEE802_3_STATS_NUM; x++, y++)
420 s->arg[y] = be64toh(ptr[x]);
422 /* read RFC2819 counter group using predefined counter layout */
423 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2819_COUNTERS_GROUP);
424 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
425 for (x = 0; x != MLX5E_PPORT_RFC2819_STATS_NUM; x++, y++)
426 s->arg[y] = be64toh(ptr[x]);
427 for (y = 0; x != MLX5E_PPORT_RFC2819_STATS_NUM +
428 MLX5E_PPORT_RFC2819_STATS_DEBUG_NUM; x++, y++)
429 s_debug->arg[y] = be64toh(ptr[x]);
431 /* read RFC2863 counter group using predefined counter layout */
432 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2863_COUNTERS_GROUP);
433 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
434 for (x = 0; x != MLX5E_PPORT_RFC2863_STATS_DEBUG_NUM; x++, y++)
435 s_debug->arg[y] = be64toh(ptr[x]);
437 /* read physical layer stats counter group using predefined counter layout */
438 MLX5_SET(ppcnt_reg, in, grp, MLX5_PHYSICAL_LAYER_COUNTERS_GROUP);
439 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
440 for (x = 0; x != MLX5E_PPORT_PHYSICAL_LAYER_STATS_DEBUG_NUM; x++, y++)
441 s_debug->arg[y] = be64toh(ptr[x]);
443 /* read Extended Ethernet counter group using predefined counter layout */
444 MLX5_SET(ppcnt_reg, in, grp, MLX5_ETHERNET_EXTENDED_COUNTERS_GROUP);
445 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
446 for (x = 0; x != MLX5E_PPORT_ETHERNET_EXTENDED_STATS_DEBUG_NUM; x++, y++)
447 s_debug->arg[y] = be64toh(ptr[x]);
449 /* read per-priority counters */
450 MLX5_SET(ppcnt_reg, in, grp, MLX5_PER_PRIORITY_COUNTERS_GROUP);
452 /* iterate all the priorities */
453 for (y = z = 0; z != MLX5E_PPORT_PER_PRIO_STATS_NUM_PRIO; z++) {
454 MLX5_SET(ppcnt_reg, in, prio_tc, z);
455 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
457 /* read per priority stats counter group using predefined counter layout */
458 for (x = 0; x != (MLX5E_PPORT_PER_PRIO_STATS_NUM /
459 MLX5E_PPORT_PER_PRIO_STATS_NUM_PRIO); x++, y++)
460 s->arg[y] = be64toh(ptr[x]);
464 /* free firmware request structures */
470 * This function is called regularly to collect all statistics
471 * counters from the firmware. The values can be viewed through the
472 * sysctl interface. Execution is serialized using the priv's global
473 * configuration lock.
476 mlx5e_update_stats_work(struct work_struct *work)
478 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
480 struct mlx5_core_dev *mdev = priv->mdev;
481 struct mlx5e_vport_stats *s = &priv->stats.vport;
482 struct mlx5e_sq_stats *sq_stats;
483 struct buf_ring *sq_br;
484 #if (__FreeBSD_version < 1100000)
485 struct ifnet *ifp = priv->ifp;
488 u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)];
490 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
493 u64 tx_queue_dropped = 0;
494 u64 tx_defragged = 0;
495 u64 tx_offload_none = 0;
498 u64 sw_lro_queued = 0;
499 u64 sw_lro_flushed = 0;
500 u64 rx_csum_none = 0;
502 u32 rx_out_of_buffer = 0;
507 out = mlx5_vzalloc(outlen);
510 if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
513 /* Collect firts the SW counters and then HW for consistency */
514 for (i = 0; i < priv->params.num_channels; i++) {
515 struct mlx5e_channel *pch = priv->channel + i;
516 struct mlx5e_rq *rq = &pch->rq;
517 struct mlx5e_rq_stats *rq_stats = &pch->rq.stats;
519 /* collect stats from LRO */
520 rq_stats->sw_lro_queued = rq->lro.lro_queued;
521 rq_stats->sw_lro_flushed = rq->lro.lro_flushed;
522 sw_lro_queued += rq_stats->sw_lro_queued;
523 sw_lro_flushed += rq_stats->sw_lro_flushed;
524 lro_packets += rq_stats->lro_packets;
525 lro_bytes += rq_stats->lro_bytes;
526 rx_csum_none += rq_stats->csum_none;
527 rx_wqe_err += rq_stats->wqe_err;
529 for (j = 0; j < priv->num_tc; j++) {
530 sq_stats = &pch->sq[j].stats;
531 sq_br = pch->sq[j].br;
533 tso_packets += sq_stats->tso_packets;
534 tso_bytes += sq_stats->tso_bytes;
535 tx_queue_dropped += sq_stats->dropped;
537 tx_queue_dropped += sq_br->br_drops;
538 tx_defragged += sq_stats->defragged;
539 tx_offload_none += sq_stats->csum_offload_none;
543 /* update counters */
544 s->tso_packets = tso_packets;
545 s->tso_bytes = tso_bytes;
546 s->tx_queue_dropped = tx_queue_dropped;
547 s->tx_defragged = tx_defragged;
548 s->lro_packets = lro_packets;
549 s->lro_bytes = lro_bytes;
550 s->sw_lro_queued = sw_lro_queued;
551 s->sw_lro_flushed = sw_lro_flushed;
552 s->rx_csum_none = rx_csum_none;
553 s->rx_wqe_err = rx_wqe_err;
556 memset(in, 0, sizeof(in));
558 MLX5_SET(query_vport_counter_in, in, opcode,
559 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
560 MLX5_SET(query_vport_counter_in, in, op_mod, 0);
561 MLX5_SET(query_vport_counter_in, in, other_vport, 0);
563 memset(out, 0, outlen);
565 /* get number of out-of-buffer drops first */
566 if (mlx5_vport_query_out_of_rx_buffer(mdev, priv->counter_set_id,
570 /* accumulate difference into a 64-bit counter */
571 s->rx_out_of_buffer += (u64)(u32)(rx_out_of_buffer - s->rx_out_of_buffer_prev);
572 s->rx_out_of_buffer_prev = rx_out_of_buffer;
574 /* get port statistics */
575 if (mlx5_cmd_exec(mdev, in, sizeof(in), out, outlen))
578 #define MLX5_GET_CTR(out, x) \
579 MLX5_GET64(query_vport_counter_out, out, x)
581 s->rx_error_packets =
582 MLX5_GET_CTR(out, received_errors.packets);
584 MLX5_GET_CTR(out, received_errors.octets);
585 s->tx_error_packets =
586 MLX5_GET_CTR(out, transmit_errors.packets);
588 MLX5_GET_CTR(out, transmit_errors.octets);
590 s->rx_unicast_packets =
591 MLX5_GET_CTR(out, received_eth_unicast.packets);
592 s->rx_unicast_bytes =
593 MLX5_GET_CTR(out, received_eth_unicast.octets);
594 s->tx_unicast_packets =
595 MLX5_GET_CTR(out, transmitted_eth_unicast.packets);
596 s->tx_unicast_bytes =
597 MLX5_GET_CTR(out, transmitted_eth_unicast.octets);
599 s->rx_multicast_packets =
600 MLX5_GET_CTR(out, received_eth_multicast.packets);
601 s->rx_multicast_bytes =
602 MLX5_GET_CTR(out, received_eth_multicast.octets);
603 s->tx_multicast_packets =
604 MLX5_GET_CTR(out, transmitted_eth_multicast.packets);
605 s->tx_multicast_bytes =
606 MLX5_GET_CTR(out, transmitted_eth_multicast.octets);
608 s->rx_broadcast_packets =
609 MLX5_GET_CTR(out, received_eth_broadcast.packets);
610 s->rx_broadcast_bytes =
611 MLX5_GET_CTR(out, received_eth_broadcast.octets);
612 s->tx_broadcast_packets =
613 MLX5_GET_CTR(out, transmitted_eth_broadcast.packets);
614 s->tx_broadcast_bytes =
615 MLX5_GET_CTR(out, transmitted_eth_broadcast.octets);
618 s->rx_unicast_packets +
619 s->rx_multicast_packets +
620 s->rx_broadcast_packets -
623 s->rx_unicast_bytes +
624 s->rx_multicast_bytes +
625 s->rx_broadcast_bytes;
627 s->tx_unicast_packets +
628 s->tx_multicast_packets +
629 s->tx_broadcast_packets;
631 s->tx_unicast_bytes +
632 s->tx_multicast_bytes +
633 s->tx_broadcast_bytes;
635 /* Update calculated offload counters */
636 s->tx_csum_offload = s->tx_packets - tx_offload_none;
637 s->rx_csum_good = s->rx_packets - s->rx_csum_none;
639 /* Get physical port counters */
640 mlx5e_update_pport_counters(priv);
642 s->tx_jumbo_packets =
643 priv->stats.port_stats_debug.tx_stat_p1519to2047octets +
644 priv->stats.port_stats_debug.tx_stat_p2048to4095octets +
645 priv->stats.port_stats_debug.tx_stat_p4096to8191octets +
646 priv->stats.port_stats_debug.tx_stat_p8192to10239octets;
648 #if (__FreeBSD_version < 1100000)
649 /* no get_counters interface in fbsd 10 */
650 ifp->if_ipackets = s->rx_packets;
651 ifp->if_ierrors = s->rx_error_packets +
652 priv->stats.pport.alignment_err +
653 priv->stats.pport.check_seq_err +
654 priv->stats.pport.crc_align_errors +
655 priv->stats.pport.in_range_len_errors +
656 priv->stats.pport.jabbers +
657 priv->stats.pport.out_of_range_len +
658 priv->stats.pport.oversize_pkts +
659 priv->stats.pport.symbol_err +
660 priv->stats.pport.too_long_errors +
661 priv->stats.pport.undersize_pkts +
662 priv->stats.pport.unsupported_op_rx;
663 ifp->if_iqdrops = s->rx_out_of_buffer +
664 priv->stats.pport.drop_events;
665 ifp->if_opackets = s->tx_packets;
666 ifp->if_oerrors = s->tx_error_packets;
667 ifp->if_snd.ifq_drops = s->tx_queue_dropped;
668 ifp->if_ibytes = s->rx_bytes;
669 ifp->if_obytes = s->tx_bytes;
671 priv->stats.pport.collisions;
677 /* Update diagnostics, if any */
678 if (priv->params_ethtool.diag_pci_enable ||
679 priv->params_ethtool.diag_general_enable) {
680 int error = mlx5_core_get_diagnostics_full(mdev,
681 priv->params_ethtool.diag_pci_enable ? &priv->params_pci : NULL,
682 priv->params_ethtool.diag_general_enable ? &priv->params_general : NULL);
684 if_printf(priv->ifp, "Failed reading diagnostics: %d\n", error);
690 mlx5e_update_stats(void *arg)
692 struct mlx5e_priv *priv = arg;
694 queue_work(priv->wq, &priv->update_stats_work);
696 callout_reset(&priv->watchdog, hz, &mlx5e_update_stats, priv);
700 mlx5e_async_event_sub(struct mlx5e_priv *priv,
701 enum mlx5_dev_event event)
704 case MLX5_DEV_EVENT_PORT_UP:
705 case MLX5_DEV_EVENT_PORT_DOWN:
706 queue_work(priv->wq, &priv->update_carrier_work);
715 mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
716 enum mlx5_dev_event event, unsigned long param)
718 struct mlx5e_priv *priv = vpriv;
720 mtx_lock(&priv->async_events_mtx);
721 if (test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state))
722 mlx5e_async_event_sub(priv, event);
723 mtx_unlock(&priv->async_events_mtx);
727 mlx5e_enable_async_events(struct mlx5e_priv *priv)
729 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state);
733 mlx5e_disable_async_events(struct mlx5e_priv *priv)
735 mtx_lock(&priv->async_events_mtx);
736 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state);
737 mtx_unlock(&priv->async_events_mtx);
740 static void mlx5e_calibration_callout(void *arg);
741 static int mlx5e_calibration_duration = 20;
742 static int mlx5e_fast_calibration = 1;
743 static int mlx5e_normal_calibration = 30;
745 static SYSCTL_NODE(_hw_mlx5, OID_AUTO, calibr, CTLFLAG_RW, 0,
746 "MLX5 timestamp calibration parameteres");
748 SYSCTL_INT(_hw_mlx5_calibr, OID_AUTO, duration, CTLFLAG_RWTUN,
749 &mlx5e_calibration_duration, 0,
750 "Duration of initial calibration");
751 SYSCTL_INT(_hw_mlx5_calibr, OID_AUTO, fast, CTLFLAG_RWTUN,
752 &mlx5e_fast_calibration, 0,
753 "Recalibration interval during initial calibration");
754 SYSCTL_INT(_hw_mlx5_calibr, OID_AUTO, normal, CTLFLAG_RWTUN,
755 &mlx5e_normal_calibration, 0,
756 "Recalibration interval during normal operations");
759 * Ignites the calibration process.
762 mlx5e_reset_calibration_callout(struct mlx5e_priv *priv)
765 if (priv->clbr_done == 0)
766 mlx5e_calibration_callout(priv);
768 callout_reset_curcpu(&priv->tstmp_clbr, (priv->clbr_done <
769 mlx5e_calibration_duration ? mlx5e_fast_calibration :
770 mlx5e_normal_calibration) * hz, mlx5e_calibration_callout,
775 mlx5e_timespec2usec(const struct timespec *ts)
778 return ((uint64_t)ts->tv_sec * 1000000000 + ts->tv_nsec);
782 mlx5e_hw_clock(struct mlx5e_priv *priv)
784 struct mlx5_init_seg *iseg;
785 uint32_t hw_h, hw_h1, hw_l;
787 iseg = priv->mdev->iseg;
789 hw_h = ioread32be(&iseg->internal_timer_h);
790 hw_l = ioread32be(&iseg->internal_timer_l);
791 hw_h1 = ioread32be(&iseg->internal_timer_h);
792 } while (hw_h1 != hw_h);
793 return (((uint64_t)hw_h << 32) | hw_l);
797 * The calibration callout, it runs either in the context of the
798 * thread which enables calibration, or in callout. It takes the
799 * snapshot of system and adapter clocks, then advances the pointers to
800 * the calibration point to allow rx path to read the consistent data
804 mlx5e_calibration_callout(void *arg)
806 struct mlx5e_priv *priv;
807 struct mlx5e_clbr_point *next, *curr;
812 curr = &priv->clbr_points[priv->clbr_curr];
813 clbr_curr_next = priv->clbr_curr + 1;
814 if (clbr_curr_next >= nitems(priv->clbr_points))
816 next = &priv->clbr_points[clbr_curr_next];
818 next->base_prev = curr->base_curr;
819 next->clbr_hw_prev = curr->clbr_hw_curr;
821 next->clbr_hw_curr = mlx5e_hw_clock(priv);
822 if (((next->clbr_hw_curr - curr->clbr_hw_prev) >> MLX5E_TSTMP_PREC) ==
824 if_printf(priv->ifp, "HW failed tstmp frozen %#jx %#jx,"
825 "disabling\n", next->clbr_hw_curr, curr->clbr_hw_prev);
831 next->base_curr = mlx5e_timespec2usec(&ts);
834 atomic_thread_fence_rel();
835 priv->clbr_curr = clbr_curr_next;
836 atomic_store_rel_int(&next->clbr_gen, ++(priv->clbr_gen));
838 if (priv->clbr_done < mlx5e_calibration_duration)
840 mlx5e_reset_calibration_callout(priv);
843 static const char *mlx5e_rq_stats_desc[] = {
844 MLX5E_RQ_STATS(MLX5E_STATS_DESC)
848 mlx5e_create_rq(struct mlx5e_channel *c,
849 struct mlx5e_rq_param *param,
852 struct mlx5e_priv *priv = c->priv;
853 struct mlx5_core_dev *mdev = priv->mdev;
855 void *rqc = param->rqc;
856 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
862 err = mlx5e_get_wqe_sz(priv, &wqe_sz, &nsegs);
866 /* Create DMA descriptor TAG */
867 if ((err = -bus_dma_tag_create(
868 bus_get_dma_tag(mdev->pdev->dev.bsddev),
869 1, /* any alignment */
871 BUS_SPACE_MAXADDR, /* lowaddr */
872 BUS_SPACE_MAXADDR, /* highaddr */
873 NULL, NULL, /* filter, filterarg */
874 nsegs * MLX5E_MAX_RX_BYTES, /* maxsize */
875 nsegs, /* nsegments */
876 nsegs * MLX5E_MAX_RX_BYTES, /* maxsegsize */
878 NULL, NULL, /* lockfunc, lockfuncarg */
882 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
885 goto err_free_dma_tag;
887 rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
889 err = mlx5e_get_wqe_sz(priv, &rq->wqe_sz, &rq->nsegs);
891 goto err_rq_wq_destroy;
893 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
895 err = -tcp_lro_init_args(&rq->lro, c->tag.m_snd_tag.ifp, TCP_LRO_ENTRIES, wq_sz);
897 goto err_rq_wq_destroy;
899 rq->mbuf = malloc(wq_sz * sizeof(rq->mbuf[0]), M_MLX5EN, M_WAITOK | M_ZERO);
900 for (i = 0; i != wq_sz; i++) {
901 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
902 #if (MLX5E_MAX_RX_SEGS == 1)
903 uint32_t byte_count = rq->wqe_sz - MLX5E_NET_IP_ALIGN;
908 err = -bus_dmamap_create(rq->dma_tag, 0, &rq->mbuf[i].dma_map);
911 bus_dmamap_destroy(rq->dma_tag, rq->mbuf[i].dma_map);
912 goto err_rq_mbuf_free;
915 /* set value for constant fields */
916 #if (MLX5E_MAX_RX_SEGS == 1)
917 wqe->data[0].lkey = c->mkey_be;
918 wqe->data[0].byte_count = cpu_to_be32(byte_count | MLX5_HW_START_PADDING);
920 for (j = 0; j < rq->nsegs; j++)
921 wqe->data[j].lkey = c->mkey_be;
925 INIT_WORK(&rq->dim.work, mlx5e_dim_work);
926 if (priv->params.rx_cq_moderation_mode < 2) {
927 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_DISABLED;
929 void *cqc = container_of(param,
930 struct mlx5e_channel_param, rq)->rx_cq.cqc;
932 switch (MLX5_GET(cqc, cqc, cq_period_mode)) {
933 case MLX5_CQ_PERIOD_MODE_START_FROM_EQE:
934 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
936 case MLX5_CQ_PERIOD_MODE_START_FROM_CQE:
937 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE;
940 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_DISABLED;
945 rq->ifp = c->tag.m_snd_tag.ifp;
949 snprintf(buffer, sizeof(buffer), "rxstat%d", c->ix);
950 mlx5e_create_stats(&rq->stats.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
951 buffer, mlx5e_rq_stats_desc, MLX5E_RQ_STATS_NUM,
956 free(rq->mbuf, M_MLX5EN);
957 tcp_lro_free(&rq->lro);
959 mlx5_wq_destroy(&rq->wq_ctrl);
961 bus_dma_tag_destroy(rq->dma_tag);
967 mlx5e_destroy_rq(struct mlx5e_rq *rq)
972 /* destroy all sysctl nodes */
973 sysctl_ctx_free(&rq->stats.ctx);
975 /* free leftover LRO packets, if any */
976 tcp_lro_free(&rq->lro);
978 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
979 for (i = 0; i != wq_sz; i++) {
980 if (rq->mbuf[i].mbuf != NULL) {
981 bus_dmamap_unload(rq->dma_tag, rq->mbuf[i].dma_map);
982 m_freem(rq->mbuf[i].mbuf);
984 bus_dmamap_destroy(rq->dma_tag, rq->mbuf[i].dma_map);
986 free(rq->mbuf, M_MLX5EN);
987 mlx5_wq_destroy(&rq->wq_ctrl);
991 mlx5e_enable_rq(struct mlx5e_rq *rq, struct mlx5e_rq_param *param)
993 struct mlx5e_channel *c = rq->channel;
994 struct mlx5e_priv *priv = c->priv;
995 struct mlx5_core_dev *mdev = priv->mdev;
1003 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
1004 sizeof(u64) * rq->wq_ctrl.buf.npages;
1005 in = mlx5_vzalloc(inlen);
1009 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
1010 wq = MLX5_ADDR_OF(rqc, rqc, wq);
1012 memcpy(rqc, param->rqc, sizeof(param->rqc));
1014 MLX5_SET(rqc, rqc, cqn, c->rq.cq.mcq.cqn);
1015 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
1016 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
1017 if (priv->counter_set_id >= 0)
1018 MLX5_SET(rqc, rqc, counter_set_id, priv->counter_set_id);
1019 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
1021 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
1023 mlx5_fill_page_array(&rq->wq_ctrl.buf,
1024 (__be64 *) MLX5_ADDR_OF(wq, wq, pas));
1026 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
1034 mlx5e_modify_rq(struct mlx5e_rq *rq, int curr_state, int next_state)
1036 struct mlx5e_channel *c = rq->channel;
1037 struct mlx5e_priv *priv = c->priv;
1038 struct mlx5_core_dev *mdev = priv->mdev;
1045 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
1046 in = mlx5_vzalloc(inlen);
1050 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
1052 MLX5_SET(modify_rq_in, in, rqn, rq->rqn);
1053 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
1054 MLX5_SET(rqc, rqc, state, next_state);
1056 err = mlx5_core_modify_rq(mdev, in, inlen);
1064 mlx5e_disable_rq(struct mlx5e_rq *rq)
1066 struct mlx5e_channel *c = rq->channel;
1067 struct mlx5e_priv *priv = c->priv;
1068 struct mlx5_core_dev *mdev = priv->mdev;
1070 mlx5_core_destroy_rq(mdev, rq->rqn);
1074 mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq)
1076 struct mlx5e_channel *c = rq->channel;
1077 struct mlx5e_priv *priv = c->priv;
1078 struct mlx5_wq_ll *wq = &rq->wq;
1081 for (i = 0; i < 1000; i++) {
1082 if (wq->cur_sz >= priv->params.min_rx_wqes)
1087 return (-ETIMEDOUT);
1091 mlx5e_open_rq(struct mlx5e_channel *c,
1092 struct mlx5e_rq_param *param,
1093 struct mlx5e_rq *rq)
1097 err = mlx5e_create_rq(c, param, rq);
1101 err = mlx5e_enable_rq(rq, param);
1103 goto err_destroy_rq;
1105 err = mlx5e_modify_rq(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
1107 goto err_disable_rq;
1114 mlx5e_disable_rq(rq);
1116 mlx5e_destroy_rq(rq);
1122 mlx5e_close_rq(struct mlx5e_rq *rq)
1126 callout_stop(&rq->watchdog);
1127 mtx_unlock(&rq->mtx);
1129 callout_drain(&rq->watchdog);
1131 mlx5e_modify_rq(rq, MLX5_RQC_STATE_RDY, MLX5_RQC_STATE_ERR);
1135 mlx5e_close_rq_wait(struct mlx5e_rq *rq)
1137 struct mlx5_core_dev *mdev = rq->channel->priv->mdev;
1139 /* wait till RQ is empty */
1140 while (!mlx5_wq_ll_is_empty(&rq->wq) &&
1141 (mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR)) {
1143 rq->cq.mcq.comp(&rq->cq.mcq);
1146 cancel_work_sync(&rq->dim.work);
1147 mlx5e_disable_rq(rq);
1148 mlx5e_destroy_rq(rq);
1152 mlx5e_free_sq_db(struct mlx5e_sq *sq)
1154 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1157 for (x = 0; x != wq_sz; x++)
1158 bus_dmamap_destroy(sq->dma_tag, sq->mbuf[x].dma_map);
1159 free(sq->mbuf, M_MLX5EN);
1163 mlx5e_alloc_sq_db(struct mlx5e_sq *sq)
1165 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
1169 sq->mbuf = malloc(wq_sz * sizeof(sq->mbuf[0]), M_MLX5EN, M_WAITOK | M_ZERO);
1171 /* Create DMA descriptor MAPs */
1172 for (x = 0; x != wq_sz; x++) {
1173 err = -bus_dmamap_create(sq->dma_tag, 0, &sq->mbuf[x].dma_map);
1176 bus_dmamap_destroy(sq->dma_tag, sq->mbuf[x].dma_map);
1177 free(sq->mbuf, M_MLX5EN);
1184 static const char *mlx5e_sq_stats_desc[] = {
1185 MLX5E_SQ_STATS(MLX5E_STATS_DESC)
1189 mlx5e_update_sq_inline(struct mlx5e_sq *sq)
1191 sq->max_inline = sq->priv->params.tx_max_inline;
1192 sq->min_inline_mode = sq->priv->params.tx_min_inline_mode;
1195 * Check if trust state is DSCP or if inline mode is NONE which
1196 * indicates CX-5 or newer hardware.
1198 if (sq->priv->params_ethtool.trust_state != MLX5_QPTS_TRUST_PCP ||
1199 sq->min_inline_mode == MLX5_INLINE_MODE_NONE) {
1200 if (MLX5_CAP_ETH(sq->priv->mdev, wqe_vlan_insert))
1201 sq->min_insert_caps = MLX5E_INSERT_VLAN | MLX5E_INSERT_NON_VLAN;
1203 sq->min_insert_caps = MLX5E_INSERT_NON_VLAN;
1205 sq->min_insert_caps = 0;
1210 mlx5e_refresh_sq_inline_sub(struct mlx5e_priv *priv, struct mlx5e_channel *c)
1214 for (i = 0; i != c->num_tc; i++) {
1215 mtx_lock(&c->sq[i].lock);
1216 mlx5e_update_sq_inline(&c->sq[i]);
1217 mtx_unlock(&c->sq[i].lock);
1222 mlx5e_refresh_sq_inline(struct mlx5e_priv *priv)
1226 /* check if channels are closed */
1227 if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
1230 for (i = 0; i < priv->params.num_channels; i++)
1231 mlx5e_refresh_sq_inline_sub(priv, &priv->channel[i]);
1235 mlx5e_create_sq(struct mlx5e_channel *c,
1237 struct mlx5e_sq_param *param,
1238 struct mlx5e_sq *sq)
1240 struct mlx5e_priv *priv = c->priv;
1241 struct mlx5_core_dev *mdev = priv->mdev;
1243 void *sqc = param->sqc;
1244 void *sqc_wq = MLX5_ADDR_OF(sqc, sqc, wq);
1247 /* Create DMA descriptor TAG */
1248 if ((err = -bus_dma_tag_create(
1249 bus_get_dma_tag(mdev->pdev->dev.bsddev),
1250 1, /* any alignment */
1251 0, /* no boundary */
1252 BUS_SPACE_MAXADDR, /* lowaddr */
1253 BUS_SPACE_MAXADDR, /* highaddr */
1254 NULL, NULL, /* filter, filterarg */
1255 MLX5E_MAX_TX_PAYLOAD_SIZE, /* maxsize */
1256 MLX5E_MAX_TX_MBUF_FRAGS, /* nsegments */
1257 MLX5E_MAX_TX_MBUF_SIZE, /* maxsegsize */
1259 NULL, NULL, /* lockfunc, lockfuncarg */
1263 err = mlx5_alloc_map_uar(mdev, &sq->uar);
1265 goto err_free_dma_tag;
1267 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq,
1270 goto err_unmap_free_uar;
1272 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
1273 sq->bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
1275 err = mlx5e_alloc_sq_db(sq);
1277 goto err_sq_wq_destroy;
1279 sq->mkey_be = c->mkey_be;
1280 sq->ifp = priv->ifp;
1284 mlx5e_update_sq_inline(sq);
1286 snprintf(buffer, sizeof(buffer), "txstat%dtc%d", c->ix, tc);
1287 mlx5e_create_stats(&sq->stats.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
1288 buffer, mlx5e_sq_stats_desc, MLX5E_SQ_STATS_NUM,
1294 mlx5_wq_destroy(&sq->wq_ctrl);
1297 mlx5_unmap_free_uar(mdev, &sq->uar);
1300 bus_dma_tag_destroy(sq->dma_tag);
1306 mlx5e_destroy_sq(struct mlx5e_sq *sq)
1308 /* destroy all sysctl nodes */
1309 sysctl_ctx_free(&sq->stats.ctx);
1311 mlx5e_free_sq_db(sq);
1312 mlx5_wq_destroy(&sq->wq_ctrl);
1313 mlx5_unmap_free_uar(sq->priv->mdev, &sq->uar);
1317 mlx5e_enable_sq(struct mlx5e_sq *sq, struct mlx5e_sq_param *param,
1326 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
1327 sizeof(u64) * sq->wq_ctrl.buf.npages;
1328 in = mlx5_vzalloc(inlen);
1332 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1333 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1335 memcpy(sqc, param->sqc, sizeof(param->sqc));
1337 MLX5_SET(sqc, sqc, tis_num_0, tis_num);
1338 MLX5_SET(sqc, sqc, cqn, sq->cq.mcq.cqn);
1339 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1340 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1341 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1343 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1344 MLX5_SET(wq, wq, uar_page, sq->uar.index);
1345 MLX5_SET(wq, wq, log_wq_pg_sz, sq->wq_ctrl.buf.page_shift -
1347 MLX5_SET64(wq, wq, dbr_addr, sq->wq_ctrl.db.dma);
1349 mlx5_fill_page_array(&sq->wq_ctrl.buf,
1350 (__be64 *) MLX5_ADDR_OF(wq, wq, pas));
1352 err = mlx5_core_create_sq(sq->priv->mdev, in, inlen, &sq->sqn);
1360 mlx5e_modify_sq(struct mlx5e_sq *sq, int curr_state, int next_state)
1367 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
1368 in = mlx5_vzalloc(inlen);
1372 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
1374 MLX5_SET(modify_sq_in, in, sqn, sq->sqn);
1375 MLX5_SET(modify_sq_in, in, sq_state, curr_state);
1376 MLX5_SET(sqc, sqc, state, next_state);
1378 err = mlx5_core_modify_sq(sq->priv->mdev, in, inlen);
1386 mlx5e_disable_sq(struct mlx5e_sq *sq)
1389 mlx5_core_destroy_sq(sq->priv->mdev, sq->sqn);
1393 mlx5e_open_sq(struct mlx5e_channel *c,
1395 struct mlx5e_sq_param *param,
1396 struct mlx5e_sq *sq)
1400 err = mlx5e_create_sq(c, tc, param, sq);
1404 err = mlx5e_enable_sq(sq, param, c->priv->tisn[tc]);
1406 goto err_destroy_sq;
1408 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST, MLX5_SQC_STATE_RDY);
1410 goto err_disable_sq;
1412 WRITE_ONCE(sq->running, 1);
1417 mlx5e_disable_sq(sq);
1419 mlx5e_destroy_sq(sq);
1425 mlx5e_sq_send_nops_locked(struct mlx5e_sq *sq, int can_sleep)
1427 /* fill up remainder with NOPs */
1428 while (sq->cev_counter != 0) {
1429 while (!mlx5e_sq_has_room_for(sq, 1)) {
1430 if (can_sleep != 0) {
1431 mtx_unlock(&sq->lock);
1433 mtx_lock(&sq->lock);
1438 /* send a single NOP */
1439 mlx5e_send_nop(sq, 1);
1440 atomic_thread_fence_rel();
1443 /* Check if we need to write the doorbell */
1444 if (likely(sq->doorbell.d64 != 0)) {
1445 mlx5e_tx_notify_hw(sq, sq->doorbell.d32, 0);
1446 sq->doorbell.d64 = 0;
1451 mlx5e_sq_cev_timeout(void *arg)
1453 struct mlx5e_sq *sq = arg;
1455 mtx_assert(&sq->lock, MA_OWNED);
1457 /* check next state */
1458 switch (sq->cev_next_state) {
1459 case MLX5E_CEV_STATE_SEND_NOPS:
1460 /* fill TX ring with NOPs, if any */
1461 mlx5e_sq_send_nops_locked(sq, 0);
1463 /* check if completed */
1464 if (sq->cev_counter == 0) {
1465 sq->cev_next_state = MLX5E_CEV_STATE_INITIAL;
1470 /* send NOPs on next timeout */
1471 sq->cev_next_state = MLX5E_CEV_STATE_SEND_NOPS;
1476 callout_reset_curcpu(&sq->cev_callout, hz, mlx5e_sq_cev_timeout, sq);
1480 mlx5e_drain_sq(struct mlx5e_sq *sq)
1483 struct mlx5_core_dev *mdev= sq->priv->mdev;
1486 * Check if already stopped.
1488 * NOTE: Serialization of this function is managed by the
1489 * caller ensuring the priv's state lock is locked or in case
1490 * of rate limit support, a single thread manages drain and
1491 * resume of SQs. The "running" variable can therefore safely
1492 * be read without any locks.
1494 if (READ_ONCE(sq->running) == 0)
1497 /* don't put more packets into the SQ */
1498 WRITE_ONCE(sq->running, 0);
1500 /* serialize access to DMA rings */
1501 mtx_lock(&sq->lock);
1503 /* teardown event factor timer, if any */
1504 sq->cev_next_state = MLX5E_CEV_STATE_HOLD_NOPS;
1505 callout_stop(&sq->cev_callout);
1507 /* send dummy NOPs in order to flush the transmit ring */
1508 mlx5e_sq_send_nops_locked(sq, 1);
1509 mtx_unlock(&sq->lock);
1511 /* make sure it is safe to free the callout */
1512 callout_drain(&sq->cev_callout);
1514 /* wait till SQ is empty or link is down */
1515 mtx_lock(&sq->lock);
1516 while (sq->cc != sq->pc &&
1517 (sq->priv->media_status_last & IFM_ACTIVE) != 0 &&
1518 mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
1519 mtx_unlock(&sq->lock);
1521 sq->cq.mcq.comp(&sq->cq.mcq);
1522 mtx_lock(&sq->lock);
1524 mtx_unlock(&sq->lock);
1526 /* error out remaining requests */
1527 error = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY, MLX5_SQC_STATE_ERR);
1530 "mlx5e_modify_sq() from RDY to ERR failed: %d\n", error);
1533 /* wait till SQ is empty */
1534 mtx_lock(&sq->lock);
1535 while (sq->cc != sq->pc &&
1536 mdev->state != MLX5_DEVICE_STATE_INTERNAL_ERROR) {
1537 mtx_unlock(&sq->lock);
1539 sq->cq.mcq.comp(&sq->cq.mcq);
1540 mtx_lock(&sq->lock);
1542 mtx_unlock(&sq->lock);
1546 mlx5e_close_sq_wait(struct mlx5e_sq *sq)
1550 mlx5e_disable_sq(sq);
1551 mlx5e_destroy_sq(sq);
1555 mlx5e_create_cq(struct mlx5e_priv *priv,
1556 struct mlx5e_cq_param *param,
1557 struct mlx5e_cq *cq,
1558 mlx5e_cq_comp_t *comp,
1561 struct mlx5_core_dev *mdev = priv->mdev;
1562 struct mlx5_core_cq *mcq = &cq->mcq;
1568 param->wq.buf_numa_node = 0;
1569 param->wq.db_numa_node = 0;
1571 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1576 mlx5_vector2eqn(mdev, eq_ix, &eqn_not_used, &irqn);
1579 mcq->set_ci_db = cq->wq_ctrl.db.db;
1580 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1581 *mcq->set_ci_db = 0;
1583 mcq->vector = eq_ix;
1585 mcq->event = mlx5e_cq_error_event;
1587 mcq->uar = &priv->cq_uar;
1589 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
1590 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1601 mlx5e_destroy_cq(struct mlx5e_cq *cq)
1603 mlx5_wq_destroy(&cq->wq_ctrl);
1607 mlx5e_enable_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param, int eq_ix)
1609 struct mlx5_core_cq *mcq = &cq->mcq;
1617 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1618 sizeof(u64) * cq->wq_ctrl.buf.npages;
1619 in = mlx5_vzalloc(inlen);
1623 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1625 memcpy(cqc, param->cqc, sizeof(param->cqc));
1627 mlx5_fill_page_array(&cq->wq_ctrl.buf,
1628 (__be64 *) MLX5_ADDR_OF(create_cq_in, in, pas));
1630 mlx5_vector2eqn(cq->priv->mdev, eq_ix, &eqn, &irqn_not_used);
1632 MLX5_SET(cqc, cqc, c_eqn, eqn);
1633 MLX5_SET(cqc, cqc, uar_page, mcq->uar->index);
1634 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
1636 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
1638 err = mlx5_core_create_cq(cq->priv->mdev, mcq, in, inlen);
1645 mlx5e_cq_arm(cq, MLX5_GET_DOORBELL_LOCK(&cq->priv->doorbell_lock));
1651 mlx5e_disable_cq(struct mlx5e_cq *cq)
1654 mlx5_core_destroy_cq(cq->priv->mdev, &cq->mcq);
1658 mlx5e_open_cq(struct mlx5e_priv *priv,
1659 struct mlx5e_cq_param *param,
1660 struct mlx5e_cq *cq,
1661 mlx5e_cq_comp_t *comp,
1666 err = mlx5e_create_cq(priv, param, cq, comp, eq_ix);
1670 err = mlx5e_enable_cq(cq, param, eq_ix);
1672 goto err_destroy_cq;
1677 mlx5e_destroy_cq(cq);
1683 mlx5e_close_cq(struct mlx5e_cq *cq)
1685 mlx5e_disable_cq(cq);
1686 mlx5e_destroy_cq(cq);
1690 mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1691 struct mlx5e_channel_param *cparam)
1696 for (tc = 0; tc < c->num_tc; tc++) {
1697 /* open completion queue */
1698 err = mlx5e_open_cq(c->priv, &cparam->tx_cq, &c->sq[tc].cq,
1699 &mlx5e_tx_cq_comp, c->ix);
1701 goto err_close_tx_cqs;
1706 for (tc--; tc >= 0; tc--)
1707 mlx5e_close_cq(&c->sq[tc].cq);
1713 mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1717 for (tc = 0; tc < c->num_tc; tc++)
1718 mlx5e_close_cq(&c->sq[tc].cq);
1722 mlx5e_open_sqs(struct mlx5e_channel *c,
1723 struct mlx5e_channel_param *cparam)
1728 for (tc = 0; tc < c->num_tc; tc++) {
1729 err = mlx5e_open_sq(c, tc, &cparam->sq, &c->sq[tc]);
1737 for (tc--; tc >= 0; tc--)
1738 mlx5e_close_sq_wait(&c->sq[tc]);
1744 mlx5e_close_sqs_wait(struct mlx5e_channel *c)
1748 for (tc = 0; tc < c->num_tc; tc++)
1749 mlx5e_close_sq_wait(&c->sq[tc]);
1753 mlx5e_chan_mtx_init(struct mlx5e_channel *c)
1757 mtx_init(&c->rq.mtx, "mlx5rx", MTX_NETWORK_LOCK, MTX_DEF);
1759 callout_init_mtx(&c->rq.watchdog, &c->rq.mtx, 0);
1761 for (tc = 0; tc < c->num_tc; tc++) {
1762 struct mlx5e_sq *sq = c->sq + tc;
1764 mtx_init(&sq->lock, "mlx5tx",
1765 MTX_NETWORK_LOCK " TX", MTX_DEF);
1766 mtx_init(&sq->comp_lock, "mlx5comp",
1767 MTX_NETWORK_LOCK " TX", MTX_DEF);
1769 callout_init_mtx(&sq->cev_callout, &sq->lock, 0);
1771 sq->cev_factor = c->priv->params_ethtool.tx_completion_fact;
1773 /* ensure the TX completion event factor is not zero */
1774 if (sq->cev_factor == 0)
1780 mlx5e_chan_mtx_destroy(struct mlx5e_channel *c)
1784 mtx_destroy(&c->rq.mtx);
1786 for (tc = 0; tc < c->num_tc; tc++) {
1787 mtx_destroy(&c->sq[tc].lock);
1788 mtx_destroy(&c->sq[tc].comp_lock);
1793 mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1794 struct mlx5e_channel_param *cparam,
1795 struct mlx5e_channel *c)
1799 memset(c, 0, sizeof(*c));
1803 /* setup send tag */
1804 c->tag.m_snd_tag.ifp = priv->ifp;
1805 c->tag.type = IF_SND_TAG_TYPE_UNLIMITED;
1806 c->mkey_be = cpu_to_be32(priv->mr.key);
1807 c->num_tc = priv->num_tc;
1810 mlx5e_chan_mtx_init(c);
1812 /* open transmit completion queue */
1813 err = mlx5e_open_tx_cqs(c, cparam);
1817 /* open receive completion queue */
1818 err = mlx5e_open_cq(c->priv, &cparam->rx_cq, &c->rq.cq,
1819 &mlx5e_rx_cq_comp, c->ix);
1821 goto err_close_tx_cqs;
1823 err = mlx5e_open_sqs(c, cparam);
1825 goto err_close_rx_cq;
1827 err = mlx5e_open_rq(c, &cparam->rq, &c->rq);
1831 /* poll receive queue initially */
1832 c->rq.cq.mcq.comp(&c->rq.cq.mcq);
1837 mlx5e_close_sqs_wait(c);
1840 mlx5e_close_cq(&c->rq.cq);
1843 mlx5e_close_tx_cqs(c);
1846 /* destroy mutexes */
1847 mlx5e_chan_mtx_destroy(c);
1852 mlx5e_close_channel(struct mlx5e_channel *c)
1854 mlx5e_close_rq(&c->rq);
1858 mlx5e_close_channel_wait(struct mlx5e_channel *c)
1860 mlx5e_close_rq_wait(&c->rq);
1861 mlx5e_close_sqs_wait(c);
1862 mlx5e_close_cq(&c->rq.cq);
1863 mlx5e_close_tx_cqs(c);
1864 /* destroy mutexes */
1865 mlx5e_chan_mtx_destroy(c);
1869 mlx5e_get_wqe_sz(struct mlx5e_priv *priv, u32 *wqe_sz, u32 *nsegs)
1873 r = priv->params.hw_lro_en ? priv->params.lro_wqe_sz :
1874 MLX5E_SW2MB_MTU(priv->ifp->if_mtu);
1875 if (r > MJUM16BYTES)
1880 else if (r > MJUMPAGESIZE)
1882 else if (r > MCLBYTES)
1888 * n + 1 must be a power of two, because stride size must be.
1889 * Stride size is 16 * (n + 1), as the first segment is
1892 for (n = howmany(r, MLX5E_MAX_RX_BYTES); !powerof2(n + 1); n++)
1901 mlx5e_build_rq_param(struct mlx5e_priv *priv,
1902 struct mlx5e_rq_param *param)
1904 void *rqc = param->rqc;
1905 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1908 mlx5e_get_wqe_sz(priv, &wqe_sz, &nsegs);
1909 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1910 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1911 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe) +
1912 nsegs * sizeof(struct mlx5_wqe_data_seg)));
1913 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_rq_size);
1914 MLX5_SET(wq, wq, pd, priv->pdn);
1916 param->wq.buf_numa_node = 0;
1917 param->wq.db_numa_node = 0;
1918 param->wq.linear = 1;
1922 mlx5e_build_sq_param(struct mlx5e_priv *priv,
1923 struct mlx5e_sq_param *param)
1925 void *sqc = param->sqc;
1926 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1928 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_sq_size);
1929 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1930 MLX5_SET(wq, wq, pd, priv->pdn);
1932 param->wq.buf_numa_node = 0;
1933 param->wq.db_numa_node = 0;
1934 param->wq.linear = 1;
1938 mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1939 struct mlx5e_cq_param *param)
1941 void *cqc = param->cqc;
1943 MLX5_SET(cqc, cqc, uar_page, priv->cq_uar.index);
1947 mlx5e_get_default_profile(struct mlx5e_priv *priv, int mode, struct net_dim_cq_moder *ptr)
1950 *ptr = net_dim_get_profile(mode, MLX5E_DIM_DEFAULT_PROFILE);
1952 /* apply LRO restrictions */
1953 if (priv->params.hw_lro_en &&
1954 ptr->pkts > MLX5E_DIM_MAX_RX_CQ_MODERATION_PKTS_WITH_LRO) {
1955 ptr->pkts = MLX5E_DIM_MAX_RX_CQ_MODERATION_PKTS_WITH_LRO;
1960 mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
1961 struct mlx5e_cq_param *param)
1963 struct net_dim_cq_moder curr;
1964 void *cqc = param->cqc;
1968 * TODO The sysctl to control on/off is a bool value for now, which means
1969 * we only support CSUM, once HASH is implemnted we'll need to address that.
1971 if (priv->params.cqe_zipping_en) {
1972 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
1973 MLX5_SET(cqc, cqc, cqe_compression_en, 1);
1976 MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_rq_size);
1978 switch (priv->params.rx_cq_moderation_mode) {
1980 MLX5_SET(cqc, cqc, cq_period, priv->params.rx_cq_moderation_usec);
1981 MLX5_SET(cqc, cqc, cq_max_count, priv->params.rx_cq_moderation_pkts);
1982 MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
1985 MLX5_SET(cqc, cqc, cq_period, priv->params.rx_cq_moderation_usec);
1986 MLX5_SET(cqc, cqc, cq_max_count, priv->params.rx_cq_moderation_pkts);
1987 if (MLX5_CAP_GEN(priv->mdev, cq_period_start_from_cqe))
1988 MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
1990 MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
1993 mlx5e_get_default_profile(priv, NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE, &curr);
1994 MLX5_SET(cqc, cqc, cq_period, curr.usec);
1995 MLX5_SET(cqc, cqc, cq_max_count, curr.pkts);
1996 MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
1999 mlx5e_get_default_profile(priv, NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE, &curr);
2000 MLX5_SET(cqc, cqc, cq_period, curr.usec);
2001 MLX5_SET(cqc, cqc, cq_max_count, curr.pkts);
2002 if (MLX5_CAP_GEN(priv->mdev, cq_period_start_from_cqe))
2003 MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
2005 MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
2011 mlx5e_dim_build_cq_param(priv, param);
2013 mlx5e_build_common_cq_param(priv, param);
2017 mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
2018 struct mlx5e_cq_param *param)
2020 void *cqc = param->cqc;
2022 MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_sq_size);
2023 MLX5_SET(cqc, cqc, cq_period, priv->params.tx_cq_moderation_usec);
2024 MLX5_SET(cqc, cqc, cq_max_count, priv->params.tx_cq_moderation_pkts);
2026 switch (priv->params.tx_cq_moderation_mode) {
2028 MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
2031 if (MLX5_CAP_GEN(priv->mdev, cq_period_start_from_cqe))
2032 MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
2034 MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
2038 mlx5e_build_common_cq_param(priv, param);
2042 mlx5e_build_channel_param(struct mlx5e_priv *priv,
2043 struct mlx5e_channel_param *cparam)
2045 memset(cparam, 0, sizeof(*cparam));
2047 mlx5e_build_rq_param(priv, &cparam->rq);
2048 mlx5e_build_sq_param(priv, &cparam->sq);
2049 mlx5e_build_rx_cq_param(priv, &cparam->rx_cq);
2050 mlx5e_build_tx_cq_param(priv, &cparam->tx_cq);
2054 mlx5e_open_channels(struct mlx5e_priv *priv)
2056 struct mlx5e_channel_param cparam;
2061 mlx5e_build_channel_param(priv, &cparam);
2062 for (i = 0; i < priv->params.num_channels; i++) {
2063 err = mlx5e_open_channel(priv, i, &cparam, &priv->channel[i]);
2065 goto err_close_channels;
2068 for (j = 0; j < priv->params.num_channels; j++) {
2069 err = mlx5e_wait_for_min_rx_wqes(&priv->channel[j].rq);
2071 goto err_close_channels;
2077 mlx5e_close_channel(&priv->channel[i]);
2078 mlx5e_close_channel_wait(&priv->channel[i]);
2084 mlx5e_close_channels(struct mlx5e_priv *priv)
2088 for (i = 0; i < priv->params.num_channels; i++)
2089 mlx5e_close_channel(&priv->channel[i]);
2090 for (i = 0; i < priv->params.num_channels; i++)
2091 mlx5e_close_channel_wait(&priv->channel[i]);
2095 mlx5e_refresh_sq_params(struct mlx5e_priv *priv, struct mlx5e_sq *sq)
2098 if (MLX5_CAP_GEN(priv->mdev, cq_period_mode_modify)) {
2101 switch (priv->params.tx_cq_moderation_mode) {
2104 cq_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
2107 cq_mode = MLX5_CQ_PERIOD_MODE_START_FROM_CQE;
2111 return (mlx5_core_modify_cq_moderation_mode(priv->mdev, &sq->cq.mcq,
2112 priv->params.tx_cq_moderation_usec,
2113 priv->params.tx_cq_moderation_pkts,
2117 return (mlx5_core_modify_cq_moderation(priv->mdev, &sq->cq.mcq,
2118 priv->params.tx_cq_moderation_usec,
2119 priv->params.tx_cq_moderation_pkts));
2123 mlx5e_refresh_rq_params(struct mlx5e_priv *priv, struct mlx5e_rq *rq)
2126 if (MLX5_CAP_GEN(priv->mdev, cq_period_mode_modify)) {
2131 switch (priv->params.rx_cq_moderation_mode) {
2134 cq_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
2135 dim_mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_EQE;
2138 cq_mode = MLX5_CQ_PERIOD_MODE_START_FROM_CQE;
2139 dim_mode = NET_DIM_CQ_PERIOD_MODE_START_FROM_CQE;
2143 /* tear down dynamic interrupt moderation */
2145 rq->dim.mode = NET_DIM_CQ_PERIOD_MODE_DISABLED;
2146 mtx_unlock(&rq->mtx);
2148 /* wait for dynamic interrupt moderation work task, if any */
2149 cancel_work_sync(&rq->dim.work);
2151 if (priv->params.rx_cq_moderation_mode >= 2) {
2152 struct net_dim_cq_moder curr;
2154 mlx5e_get_default_profile(priv, dim_mode, &curr);
2156 retval = mlx5_core_modify_cq_moderation_mode(priv->mdev, &rq->cq.mcq,
2157 curr.usec, curr.pkts, cq_mode);
2159 /* set dynamic interrupt moderation mode and zero defaults */
2161 rq->dim.mode = dim_mode;
2163 rq->dim.profile_ix = MLX5E_DIM_DEFAULT_PROFILE;
2164 mtx_unlock(&rq->mtx);
2166 retval = mlx5_core_modify_cq_moderation_mode(priv->mdev, &rq->cq.mcq,
2167 priv->params.rx_cq_moderation_usec,
2168 priv->params.rx_cq_moderation_pkts,
2174 return (mlx5_core_modify_cq_moderation(priv->mdev, &rq->cq.mcq,
2175 priv->params.rx_cq_moderation_usec,
2176 priv->params.rx_cq_moderation_pkts));
2180 mlx5e_refresh_channel_params_sub(struct mlx5e_priv *priv, struct mlx5e_channel *c)
2185 err = mlx5e_refresh_rq_params(priv, &c->rq);
2189 for (i = 0; i != c->num_tc; i++) {
2190 err = mlx5e_refresh_sq_params(priv, &c->sq[i]);
2199 mlx5e_refresh_channel_params(struct mlx5e_priv *priv)
2203 /* check if channels are closed */
2204 if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
2207 for (i = 0; i < priv->params.num_channels; i++) {
2210 err = mlx5e_refresh_channel_params_sub(priv, &priv->channel[i]);
2218 mlx5e_open_tis(struct mlx5e_priv *priv, int tc)
2220 struct mlx5_core_dev *mdev = priv->mdev;
2221 u32 in[MLX5_ST_SZ_DW(create_tis_in)];
2222 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
2224 memset(in, 0, sizeof(in));
2226 MLX5_SET(tisc, tisc, prio, tc);
2227 MLX5_SET(tisc, tisc, transport_domain, priv->tdn);
2229 return (mlx5_core_create_tis(mdev, in, sizeof(in), &priv->tisn[tc]));
2233 mlx5e_close_tis(struct mlx5e_priv *priv, int tc)
2235 mlx5_core_destroy_tis(priv->mdev, priv->tisn[tc]);
2239 mlx5e_open_tises(struct mlx5e_priv *priv)
2241 int num_tc = priv->num_tc;
2245 for (tc = 0; tc < num_tc; tc++) {
2246 err = mlx5e_open_tis(priv, tc);
2248 goto err_close_tises;
2254 for (tc--; tc >= 0; tc--)
2255 mlx5e_close_tis(priv, tc);
2261 mlx5e_close_tises(struct mlx5e_priv *priv)
2263 int num_tc = priv->num_tc;
2266 for (tc = 0; tc < num_tc; tc++)
2267 mlx5e_close_tis(priv, tc);
2271 mlx5e_open_rqt(struct mlx5e_priv *priv)
2273 struct mlx5_core_dev *mdev = priv->mdev;
2275 u32 out[MLX5_ST_SZ_DW(create_rqt_out)] = {0};
2282 sz = 1 << priv->params.rx_hash_log_tbl_sz;
2284 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
2285 in = mlx5_vzalloc(inlen);
2288 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
2290 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2291 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
2293 for (i = 0; i < sz; i++) {
2296 ix = rss_get_indirection_to_bucket(ix);
2298 /* ensure we don't overflow */
2299 ix %= priv->params.num_channels;
2301 /* apply receive side scaling stride, if any */
2302 ix -= ix % (int)priv->params.channels_rsss;
2304 MLX5_SET(rqtc, rqtc, rq_num[i], priv->channel[ix].rq.rqn);
2307 MLX5_SET(create_rqt_in, in, opcode, MLX5_CMD_OP_CREATE_RQT);
2309 err = mlx5_cmd_exec(mdev, in, inlen, out, sizeof(out));
2311 priv->rqtn = MLX5_GET(create_rqt_out, out, rqtn);
2319 mlx5e_close_rqt(struct mlx5e_priv *priv)
2321 u32 in[MLX5_ST_SZ_DW(destroy_rqt_in)] = {0};
2322 u32 out[MLX5_ST_SZ_DW(destroy_rqt_out)] = {0};
2324 MLX5_SET(destroy_rqt_in, in, opcode, MLX5_CMD_OP_DESTROY_RQT);
2325 MLX5_SET(destroy_rqt_in, in, rqtn, priv->rqtn);
2327 mlx5_cmd_exec(priv->mdev, in, sizeof(in), out, sizeof(out));
2331 mlx5e_build_tir_ctx(struct mlx5e_priv *priv, u32 * tirc, int tt)
2333 void *hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2336 MLX5_SET(tirc, tirc, transport_domain, priv->tdn);
2338 #define ROUGH_MAX_L2_L3_HDR_SZ 256
2340 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
2341 MLX5_HASH_FIELD_SEL_DST_IP)
2343 #define MLX5_HASH_ALL (MLX5_HASH_FIELD_SEL_SRC_IP |\
2344 MLX5_HASH_FIELD_SEL_DST_IP |\
2345 MLX5_HASH_FIELD_SEL_L4_SPORT |\
2346 MLX5_HASH_FIELD_SEL_L4_DPORT)
2348 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
2349 MLX5_HASH_FIELD_SEL_DST_IP |\
2350 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
2352 if (priv->params.hw_lro_en) {
2353 MLX5_SET(tirc, tirc, lro_enable_mask,
2354 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
2355 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
2356 MLX5_SET(tirc, tirc, lro_max_msg_sz,
2357 (priv->params.lro_wqe_sz -
2358 ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
2359 /* TODO: add the option to choose timer value dynamically */
2360 MLX5_SET(tirc, tirc, lro_timeout_period_usecs,
2361 MLX5_CAP_ETH(priv->mdev,
2362 lro_timer_supported_periods[2]));
2365 /* setup parameters for hashing TIR type, if any */
2368 MLX5_SET(tirc, tirc, disp_type,
2369 MLX5_TIRC_DISP_TYPE_DIRECT);
2370 MLX5_SET(tirc, tirc, inline_rqn,
2371 priv->channel[0].rq.rqn);
2374 MLX5_SET(tirc, tirc, disp_type,
2375 MLX5_TIRC_DISP_TYPE_INDIRECT);
2376 MLX5_SET(tirc, tirc, indirect_table,
2378 MLX5_SET(tirc, tirc, rx_hash_fn,
2379 MLX5_TIRC_RX_HASH_FN_HASH_TOEPLITZ);
2380 hkey = (__be32 *) MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
2383 * The FreeBSD RSS implementation does currently not
2384 * support symmetric Toeplitz hashes:
2386 MLX5_SET(tirc, tirc, rx_hash_symmetric, 0);
2387 rss_getkey((uint8_t *)hkey);
2389 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
2390 hkey[0] = cpu_to_be32(0xD181C62C);
2391 hkey[1] = cpu_to_be32(0xF7F4DB5B);
2392 hkey[2] = cpu_to_be32(0x1983A2FC);
2393 hkey[3] = cpu_to_be32(0x943E1ADB);
2394 hkey[4] = cpu_to_be32(0xD9389E6B);
2395 hkey[5] = cpu_to_be32(0xD1039C2C);
2396 hkey[6] = cpu_to_be32(0xA74499AD);
2397 hkey[7] = cpu_to_be32(0x593D56D9);
2398 hkey[8] = cpu_to_be32(0xF3253C06);
2399 hkey[9] = cpu_to_be32(0x2ADC1FFC);
2405 case MLX5E_TT_IPV4_TCP:
2406 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2407 MLX5_L3_PROT_TYPE_IPV4);
2408 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2409 MLX5_L4_PROT_TYPE_TCP);
2411 if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4)) {
2412 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2416 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2420 case MLX5E_TT_IPV6_TCP:
2421 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2422 MLX5_L3_PROT_TYPE_IPV6);
2423 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2424 MLX5_L4_PROT_TYPE_TCP);
2426 if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6)) {
2427 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2431 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2435 case MLX5E_TT_IPV4_UDP:
2436 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2437 MLX5_L3_PROT_TYPE_IPV4);
2438 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2439 MLX5_L4_PROT_TYPE_UDP);
2441 if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4)) {
2442 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2446 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2450 case MLX5E_TT_IPV6_UDP:
2451 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2452 MLX5_L3_PROT_TYPE_IPV6);
2453 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2454 MLX5_L4_PROT_TYPE_UDP);
2456 if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6)) {
2457 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2461 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2465 case MLX5E_TT_IPV4_IPSEC_AH:
2466 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2467 MLX5_L3_PROT_TYPE_IPV4);
2468 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2469 MLX5_HASH_IP_IPSEC_SPI);
2472 case MLX5E_TT_IPV6_IPSEC_AH:
2473 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2474 MLX5_L3_PROT_TYPE_IPV6);
2475 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2476 MLX5_HASH_IP_IPSEC_SPI);
2479 case MLX5E_TT_IPV4_IPSEC_ESP:
2480 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2481 MLX5_L3_PROT_TYPE_IPV4);
2482 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2483 MLX5_HASH_IP_IPSEC_SPI);
2486 case MLX5E_TT_IPV6_IPSEC_ESP:
2487 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2488 MLX5_L3_PROT_TYPE_IPV6);
2489 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2490 MLX5_HASH_IP_IPSEC_SPI);
2494 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2495 MLX5_L3_PROT_TYPE_IPV4);
2496 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2501 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2502 MLX5_L3_PROT_TYPE_IPV6);
2503 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2513 mlx5e_open_tir(struct mlx5e_priv *priv, int tt)
2515 struct mlx5_core_dev *mdev = priv->mdev;
2521 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2522 in = mlx5_vzalloc(inlen);
2525 tirc = MLX5_ADDR_OF(create_tir_in, in, tir_context);
2527 mlx5e_build_tir_ctx(priv, tirc, tt);
2529 err = mlx5_core_create_tir(mdev, in, inlen, &priv->tirn[tt]);
2537 mlx5e_close_tir(struct mlx5e_priv *priv, int tt)
2539 mlx5_core_destroy_tir(priv->mdev, priv->tirn[tt]);
2543 mlx5e_open_tirs(struct mlx5e_priv *priv)
2548 for (i = 0; i < MLX5E_NUM_TT; i++) {
2549 err = mlx5e_open_tir(priv, i);
2551 goto err_close_tirs;
2557 for (i--; i >= 0; i--)
2558 mlx5e_close_tir(priv, i);
2564 mlx5e_close_tirs(struct mlx5e_priv *priv)
2568 for (i = 0; i < MLX5E_NUM_TT; i++)
2569 mlx5e_close_tir(priv, i);
2573 * SW MTU does not include headers,
2574 * HW MTU includes all headers and checksums.
2577 mlx5e_set_dev_port_mtu(struct ifnet *ifp, int sw_mtu)
2579 struct mlx5e_priv *priv = ifp->if_softc;
2580 struct mlx5_core_dev *mdev = priv->mdev;
2584 hw_mtu = MLX5E_SW2HW_MTU(sw_mtu);
2586 err = mlx5_set_port_mtu(mdev, hw_mtu);
2588 if_printf(ifp, "%s: mlx5_set_port_mtu failed setting %d, err=%d\n",
2589 __func__, sw_mtu, err);
2593 /* Update vport context MTU */
2594 err = mlx5_set_vport_mtu(mdev, hw_mtu);
2596 if_printf(ifp, "%s: Failed updating vport context with MTU size, err=%d\n",
2600 ifp->if_mtu = sw_mtu;
2602 err = mlx5_query_vport_mtu(mdev, &hw_mtu);
2603 if (err || !hw_mtu) {
2604 /* fallback to port oper mtu */
2605 err = mlx5_query_port_oper_mtu(mdev, &hw_mtu);
2608 if_printf(ifp, "Query port MTU, after setting new "
2609 "MTU value, failed\n");
2611 } else if (MLX5E_HW2SW_MTU(hw_mtu) < sw_mtu) {
2613 if_printf(ifp, "Port MTU %d is smaller than "
2614 "ifp mtu %d\n", hw_mtu, sw_mtu);
2615 } else if (MLX5E_HW2SW_MTU(hw_mtu) > sw_mtu) {
2617 if_printf(ifp, "Port MTU %d is bigger than "
2618 "ifp mtu %d\n", hw_mtu, sw_mtu);
2620 priv->params_ethtool.hw_mtu = hw_mtu;
2626 mlx5e_open_locked(struct ifnet *ifp)
2628 struct mlx5e_priv *priv = ifp->if_softc;
2632 /* check if already opened */
2633 if (test_bit(MLX5E_STATE_OPENED, &priv->state) != 0)
2637 if (rss_getnumbuckets() > priv->params.num_channels) {
2638 if_printf(ifp, "NOTE: There are more RSS buckets(%u) than "
2639 "channels(%u) available\n", rss_getnumbuckets(),
2640 priv->params.num_channels);
2643 err = mlx5e_open_tises(priv);
2645 if_printf(ifp, "%s: mlx5e_open_tises failed, %d\n",
2649 err = mlx5_vport_alloc_q_counter(priv->mdev,
2650 MLX5_INTERFACE_PROTOCOL_ETH, &set_id);
2652 if_printf(priv->ifp,
2653 "%s: mlx5_vport_alloc_q_counter failed: %d\n",
2655 goto err_close_tises;
2657 /* store counter set ID */
2658 priv->counter_set_id = set_id;
2660 err = mlx5e_open_channels(priv);
2662 if_printf(ifp, "%s: mlx5e_open_channels failed, %d\n",
2664 goto err_dalloc_q_counter;
2666 err = mlx5e_open_rqt(priv);
2668 if_printf(ifp, "%s: mlx5e_open_rqt failed, %d\n",
2670 goto err_close_channels;
2672 err = mlx5e_open_tirs(priv);
2674 if_printf(ifp, "%s: mlx5e_open_tir failed, %d\n",
2676 goto err_close_rqls;
2678 err = mlx5e_open_flow_table(priv);
2680 if_printf(ifp, "%s: mlx5e_open_flow_table failed, %d\n",
2682 goto err_close_tirs;
2684 err = mlx5e_add_all_vlan_rules(priv);
2686 if_printf(ifp, "%s: mlx5e_add_all_vlan_rules failed, %d\n",
2688 goto err_close_flow_table;
2690 set_bit(MLX5E_STATE_OPENED, &priv->state);
2692 mlx5e_update_carrier(priv);
2693 mlx5e_set_rx_mode_core(priv);
2697 err_close_flow_table:
2698 mlx5e_close_flow_table(priv);
2701 mlx5e_close_tirs(priv);
2704 mlx5e_close_rqt(priv);
2707 mlx5e_close_channels(priv);
2709 err_dalloc_q_counter:
2710 mlx5_vport_dealloc_q_counter(priv->mdev,
2711 MLX5_INTERFACE_PROTOCOL_ETH, priv->counter_set_id);
2714 mlx5e_close_tises(priv);
2720 mlx5e_open(void *arg)
2722 struct mlx5e_priv *priv = arg;
2725 if (mlx5_set_port_status(priv->mdev, MLX5_PORT_UP))
2726 if_printf(priv->ifp,
2727 "%s: Setting port status to up failed\n",
2730 mlx5e_open_locked(priv->ifp);
2731 priv->ifp->if_drv_flags |= IFF_DRV_RUNNING;
2736 mlx5e_close_locked(struct ifnet *ifp)
2738 struct mlx5e_priv *priv = ifp->if_softc;
2740 /* check if already closed */
2741 if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
2744 clear_bit(MLX5E_STATE_OPENED, &priv->state);
2746 mlx5e_set_rx_mode_core(priv);
2747 mlx5e_del_all_vlan_rules(priv);
2748 if_link_state_change(priv->ifp, LINK_STATE_DOWN);
2749 mlx5e_close_flow_table(priv);
2750 mlx5e_close_tirs(priv);
2751 mlx5e_close_rqt(priv);
2752 mlx5e_close_channels(priv);
2753 mlx5_vport_dealloc_q_counter(priv->mdev,
2754 MLX5_INTERFACE_PROTOCOL_ETH, priv->counter_set_id);
2755 mlx5e_close_tises(priv);
2760 #if (__FreeBSD_version >= 1100000)
2762 mlx5e_get_counter(struct ifnet *ifp, ift_counter cnt)
2764 struct mlx5e_priv *priv = ifp->if_softc;
2767 /* PRIV_LOCK(priv); XXX not allowed */
2769 case IFCOUNTER_IPACKETS:
2770 retval = priv->stats.vport.rx_packets;
2772 case IFCOUNTER_IERRORS:
2773 retval = priv->stats.vport.rx_error_packets +
2774 priv->stats.pport.alignment_err +
2775 priv->stats.pport.check_seq_err +
2776 priv->stats.pport.crc_align_errors +
2777 priv->stats.pport.in_range_len_errors +
2778 priv->stats.pport.jabbers +
2779 priv->stats.pport.out_of_range_len +
2780 priv->stats.pport.oversize_pkts +
2781 priv->stats.pport.symbol_err +
2782 priv->stats.pport.too_long_errors +
2783 priv->stats.pport.undersize_pkts +
2784 priv->stats.pport.unsupported_op_rx;
2786 case IFCOUNTER_IQDROPS:
2787 retval = priv->stats.vport.rx_out_of_buffer +
2788 priv->stats.pport.drop_events;
2790 case IFCOUNTER_OPACKETS:
2791 retval = priv->stats.vport.tx_packets;
2793 case IFCOUNTER_OERRORS:
2794 retval = priv->stats.vport.tx_error_packets;
2796 case IFCOUNTER_IBYTES:
2797 retval = priv->stats.vport.rx_bytes;
2799 case IFCOUNTER_OBYTES:
2800 retval = priv->stats.vport.tx_bytes;
2802 case IFCOUNTER_IMCASTS:
2803 retval = priv->stats.vport.rx_multicast_packets;
2805 case IFCOUNTER_OMCASTS:
2806 retval = priv->stats.vport.tx_multicast_packets;
2808 case IFCOUNTER_OQDROPS:
2809 retval = priv->stats.vport.tx_queue_dropped;
2811 case IFCOUNTER_COLLISIONS:
2812 retval = priv->stats.pport.collisions;
2815 retval = if_get_counter_default(ifp, cnt);
2818 /* PRIV_UNLOCK(priv); XXX not allowed */
2824 mlx5e_set_rx_mode(struct ifnet *ifp)
2826 struct mlx5e_priv *priv = ifp->if_softc;
2828 queue_work(priv->wq, &priv->set_rx_mode_work);
2832 mlx5e_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2834 struct mlx5e_priv *priv;
2836 struct ifi2creq i2c;
2845 priv = ifp->if_softc;
2847 /* check if detaching */
2848 if (priv == NULL || priv->gone != 0)
2853 ifr = (struct ifreq *)data;
2856 mlx5_query_port_max_mtu(priv->mdev, &max_mtu);
2858 if (ifr->ifr_mtu >= MLX5E_MTU_MIN &&
2859 ifr->ifr_mtu <= MIN(MLX5E_MTU_MAX, max_mtu)) {
2862 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2864 mlx5e_close_locked(ifp);
2867 mlx5e_set_dev_port_mtu(ifp, ifr->ifr_mtu);
2870 mlx5e_open_locked(ifp);
2873 if_printf(ifp, "Invalid MTU value. Min val: %d, Max val: %d\n",
2874 MLX5E_MTU_MIN, MIN(MLX5E_MTU_MAX, max_mtu));
2879 if ((ifp->if_flags & IFF_UP) &&
2880 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2881 mlx5e_set_rx_mode(ifp);
2885 if (ifp->if_flags & IFF_UP) {
2886 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2887 if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
2888 mlx5e_open_locked(ifp);
2889 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2890 mlx5_set_port_status(priv->mdev, MLX5_PORT_UP);
2893 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2894 mlx5_set_port_status(priv->mdev,
2896 if (test_bit(MLX5E_STATE_OPENED, &priv->state) != 0)
2897 mlx5e_close_locked(ifp);
2898 mlx5e_update_carrier(priv);
2899 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2906 mlx5e_set_rx_mode(ifp);
2911 ifr = (struct ifreq *)data;
2912 error = ifmedia_ioctl(ifp, ifr, &priv->media, command);
2915 ifr = (struct ifreq *)data;
2917 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2919 if (mask & IFCAP_TXCSUM) {
2920 ifp->if_capenable ^= IFCAP_TXCSUM;
2921 ifp->if_hwassist ^= (CSUM_TCP | CSUM_UDP | CSUM_IP);
2923 if (IFCAP_TSO4 & ifp->if_capenable &&
2924 !(IFCAP_TXCSUM & ifp->if_capenable)) {
2925 ifp->if_capenable &= ~IFCAP_TSO4;
2926 ifp->if_hwassist &= ~CSUM_IP_TSO;
2928 "tso4 disabled due to -txcsum.\n");
2931 if (mask & IFCAP_TXCSUM_IPV6) {
2932 ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
2933 ifp->if_hwassist ^= (CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2935 if (IFCAP_TSO6 & ifp->if_capenable &&
2936 !(IFCAP_TXCSUM_IPV6 & ifp->if_capenable)) {
2937 ifp->if_capenable &= ~IFCAP_TSO6;
2938 ifp->if_hwassist &= ~CSUM_IP6_TSO;
2940 "tso6 disabled due to -txcsum6.\n");
2943 if (mask & IFCAP_RXCSUM)
2944 ifp->if_capenable ^= IFCAP_RXCSUM;
2945 if (mask & IFCAP_RXCSUM_IPV6)
2946 ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
2947 if (mask & IFCAP_TSO4) {
2948 if (!(IFCAP_TSO4 & ifp->if_capenable) &&
2949 !(IFCAP_TXCSUM & ifp->if_capenable)) {
2950 if_printf(ifp, "enable txcsum first.\n");
2954 ifp->if_capenable ^= IFCAP_TSO4;
2955 ifp->if_hwassist ^= CSUM_IP_TSO;
2957 if (mask & IFCAP_TSO6) {
2958 if (!(IFCAP_TSO6 & ifp->if_capenable) &&
2959 !(IFCAP_TXCSUM_IPV6 & ifp->if_capenable)) {
2960 if_printf(ifp, "enable txcsum6 first.\n");
2964 ifp->if_capenable ^= IFCAP_TSO6;
2965 ifp->if_hwassist ^= CSUM_IP6_TSO;
2967 if (mask & IFCAP_VLAN_HWFILTER) {
2968 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2969 mlx5e_disable_vlan_filter(priv);
2971 mlx5e_enable_vlan_filter(priv);
2973 ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
2975 if (mask & IFCAP_VLAN_HWTAGGING)
2976 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2977 if (mask & IFCAP_WOL_MAGIC)
2978 ifp->if_capenable ^= IFCAP_WOL_MAGIC;
2980 VLAN_CAPABILITIES(ifp);
2981 /* turn off LRO means also turn of HW LRO - if it's on */
2982 if (mask & IFCAP_LRO) {
2983 int was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2984 bool need_restart = false;
2986 ifp->if_capenable ^= IFCAP_LRO;
2988 /* figure out if updating HW LRO is needed */
2989 if (!(ifp->if_capenable & IFCAP_LRO)) {
2990 if (priv->params.hw_lro_en) {
2991 priv->params.hw_lro_en = false;
2992 need_restart = true;
2995 if (priv->params.hw_lro_en == false &&
2996 priv->params_ethtool.hw_lro != 0) {
2997 priv->params.hw_lro_en = true;
2998 need_restart = true;
3001 if (was_opened && need_restart) {
3002 mlx5e_close_locked(ifp);
3003 mlx5e_open_locked(ifp);
3006 if (mask & IFCAP_HWRXTSTMP) {
3007 ifp->if_capenable ^= IFCAP_HWRXTSTMP;
3008 if (ifp->if_capenable & IFCAP_HWRXTSTMP) {
3009 if (priv->clbr_done == 0)
3010 mlx5e_reset_calibration_callout(priv);
3012 callout_drain(&priv->tstmp_clbr);
3013 priv->clbr_done = 0;
3021 ifr = (struct ifreq *)data;
3024 * Copy from the user-space address ifr_data to the
3025 * kernel-space address i2c
3027 error = copyin(ifr_data_get_ptr(ifr), &i2c, sizeof(i2c));
3031 if (i2c.len > sizeof(i2c.data)) {
3037 /* Get module_num which is required for the query_eeprom */
3038 error = mlx5_query_module_num(priv->mdev, &module_num);
3040 if_printf(ifp, "Query module num failed, eeprom "
3041 "reading is not supported\n");
3045 /* Check if module is present before doing an access */
3046 module_status = mlx5_query_module_status(priv->mdev, module_num);
3047 if (module_status != MLX5_MODULE_STATUS_PLUGGED_ENABLED &&
3048 module_status != MLX5_MODULE_STATUS_PLUGGED_DISABLED) {
3053 * Currently 0XA0 and 0xA2 are the only addresses permitted.
3054 * The internal conversion is as follows:
3056 if (i2c.dev_addr == 0xA0)
3057 read_addr = MLX5E_I2C_ADDR_LOW;
3058 else if (i2c.dev_addr == 0xA2)
3059 read_addr = MLX5E_I2C_ADDR_HIGH;
3061 if_printf(ifp, "Query eeprom failed, "
3062 "Invalid Address: %X\n", i2c.dev_addr);
3066 error = mlx5_query_eeprom(priv->mdev,
3067 read_addr, MLX5E_EEPROM_LOW_PAGE,
3068 (uint32_t)i2c.offset, (uint32_t)i2c.len, module_num,
3069 (uint32_t *)i2c.data, &size_read);
3071 if_printf(ifp, "Query eeprom failed, eeprom "
3072 "reading is not supported\n");
3077 if (i2c.len > MLX5_EEPROM_MAX_BYTES) {
3078 error = mlx5_query_eeprom(priv->mdev,
3079 read_addr, MLX5E_EEPROM_LOW_PAGE,
3080 (uint32_t)(i2c.offset + size_read),
3081 (uint32_t)(i2c.len - size_read), module_num,
3082 (uint32_t *)(i2c.data + size_read), &size_read);
3085 if_printf(ifp, "Query eeprom failed, eeprom "
3086 "reading is not supported\n");
3091 error = copyout(&i2c, ifr_data_get_ptr(ifr), sizeof(i2c));
3097 error = ether_ioctl(ifp, command, data);
3104 mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
3107 * TODO: uncoment once FW really sets all these bits if
3108 * (!mdev->caps.eth.rss_ind_tbl_cap || !mdev->caps.eth.csum_cap ||
3109 * !mdev->caps.eth.max_lso_cap || !mdev->caps.eth.vlan_cap ||
3110 * !(mdev->caps.gen.flags & MLX5_DEV_CAP_FLAG_SCQE_BRK_MOD)) return
3114 /* TODO: add more must-to-have features */
3116 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
3123 mlx5e_get_max_inline_cap(struct mlx5_core_dev *mdev)
3125 uint32_t bf_buf_size = (1U << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2U;
3127 bf_buf_size -= sizeof(struct mlx5e_tx_wqe) - 2;
3129 /* verify against driver hardware limit */
3130 if (bf_buf_size > MLX5E_MAX_TX_INLINE)
3131 bf_buf_size = MLX5E_MAX_TX_INLINE;
3133 return (bf_buf_size);
3137 mlx5e_build_ifp_priv(struct mlx5_core_dev *mdev,
3138 struct mlx5e_priv *priv,
3139 int num_comp_vectors)
3144 * TODO: Consider link speed for setting "log_sq_size",
3145 * "log_rq_size" and "cq_moderation_xxx":
3147 priv->params.log_sq_size =
3148 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
3149 priv->params.log_rq_size =
3150 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
3151 priv->params.rx_cq_moderation_usec =
3152 MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
3153 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE :
3154 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
3155 priv->params.rx_cq_moderation_mode =
3156 MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ? 1 : 0;
3157 priv->params.rx_cq_moderation_pkts =
3158 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
3159 priv->params.tx_cq_moderation_usec =
3160 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
3161 priv->params.tx_cq_moderation_pkts =
3162 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
3163 priv->params.min_rx_wqes =
3164 MLX5E_PARAMS_DEFAULT_MIN_RX_WQES;
3165 priv->params.rx_hash_log_tbl_sz =
3166 (order_base_2(num_comp_vectors) >
3167 MLX5E_PARAMS_DEFAULT_RX_HASH_LOG_TBL_SZ) ?
3168 order_base_2(num_comp_vectors) :
3169 MLX5E_PARAMS_DEFAULT_RX_HASH_LOG_TBL_SZ;
3170 priv->params.num_tc = 1;
3171 priv->params.default_vlan_prio = 0;
3172 priv->counter_set_id = -1;
3173 priv->params.tx_max_inline = mlx5e_get_max_inline_cap(mdev);
3175 err = mlx5_query_min_inline(mdev, &priv->params.tx_min_inline_mode);
3180 * hw lro is currently defaulted to off. when it won't anymore we
3181 * will consider the HW capability: "!!MLX5_CAP_ETH(mdev, lro_cap)"
3183 priv->params.hw_lro_en = false;
3184 priv->params.lro_wqe_sz = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
3186 priv->params.cqe_zipping_en = !!MLX5_CAP_GEN(mdev, cqe_compression);
3189 priv->params.num_channels = num_comp_vectors;
3190 priv->params.channels_rsss = 1;
3191 priv->order_base_2_num_channels = order_base_2(num_comp_vectors);
3192 priv->queue_mapping_channel_mask =
3193 roundup_pow_of_two(num_comp_vectors) - 1;
3194 priv->num_tc = priv->params.num_tc;
3195 priv->default_vlan_prio = priv->params.default_vlan_prio;
3197 INIT_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
3198 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
3199 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
3205 mlx5e_create_mkey(struct mlx5e_priv *priv, u32 pdn,
3206 struct mlx5_core_mr *mkey)
3208 struct ifnet *ifp = priv->ifp;
3209 struct mlx5_core_dev *mdev = priv->mdev;
3210 int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
3215 in = mlx5_vzalloc(inlen);
3217 if_printf(ifp, "%s: failed to allocate inbox\n", __func__);
3221 mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
3222 MLX5_SET(mkc, mkc, access_mode, MLX5_ACCESS_MODE_PA);
3223 MLX5_SET(mkc, mkc, lw, 1);
3224 MLX5_SET(mkc, mkc, lr, 1);
3226 MLX5_SET(mkc, mkc, pd, pdn);
3227 MLX5_SET(mkc, mkc, length64, 1);
3228 MLX5_SET(mkc, mkc, qpn, 0xffffff);
3230 err = mlx5_core_create_mkey(mdev, mkey, in, inlen);
3232 if_printf(ifp, "%s: mlx5_core_create_mkey failed, %d\n",
3239 static const char *mlx5e_vport_stats_desc[] = {
3240 MLX5E_VPORT_STATS(MLX5E_STATS_DESC)
3243 static const char *mlx5e_pport_stats_desc[] = {
3244 MLX5E_PPORT_STATS(MLX5E_STATS_DESC)
3248 mlx5e_priv_mtx_init(struct mlx5e_priv *priv)
3250 mtx_init(&priv->async_events_mtx, "mlx5async", MTX_NETWORK_LOCK, MTX_DEF);
3251 sx_init(&priv->state_lock, "mlx5state");
3252 callout_init_mtx(&priv->watchdog, &priv->async_events_mtx, 0);
3253 MLX5_INIT_DOORBELL_LOCK(&priv->doorbell_lock);
3257 mlx5e_priv_mtx_destroy(struct mlx5e_priv *priv)
3259 mtx_destroy(&priv->async_events_mtx);
3260 sx_destroy(&priv->state_lock);
3264 sysctl_firmware(SYSCTL_HANDLER_ARGS)
3267 * %d.%d%.d the string format.
3268 * fw_rev_{maj,min,sub} return u16, 2^16 = 65536.
3269 * We need at most 5 chars to store that.
3270 * It also has: two "." and NULL at the end, which means we need 18
3271 * (5*3 + 3) chars at most.
3274 struct mlx5e_priv *priv = arg1;
3277 snprintf(fw, sizeof(fw), "%d.%d.%d", fw_rev_maj(priv->mdev), fw_rev_min(priv->mdev),
3278 fw_rev_sub(priv->mdev));
3279 error = sysctl_handle_string(oidp, fw, sizeof(fw), req);
3284 mlx5e_disable_tx_dma(struct mlx5e_channel *ch)
3288 for (i = 0; i < ch->num_tc; i++)
3289 mlx5e_drain_sq(&ch->sq[i]);
3293 mlx5e_reset_sq_doorbell_record(struct mlx5e_sq *sq)
3296 sq->doorbell.d32[0] = cpu_to_be32(MLX5_OPCODE_NOP);
3297 sq->doorbell.d32[1] = cpu_to_be32(sq->sqn << 8);
3298 mlx5e_tx_notify_hw(sq, sq->doorbell.d32, 0);
3299 sq->doorbell.d64 = 0;
3303 mlx5e_resume_sq(struct mlx5e_sq *sq)
3307 /* check if already enabled */
3308 if (READ_ONCE(sq->running) != 0)
3311 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_ERR,
3312 MLX5_SQC_STATE_RST);
3315 "mlx5e_modify_sq() from ERR to RST failed: %d\n", err);
3321 /* reset doorbell prior to moving from RST to RDY */
3322 mlx5e_reset_sq_doorbell_record(sq);
3324 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST,
3325 MLX5_SQC_STATE_RDY);
3328 "mlx5e_modify_sq() from RST to RDY failed: %d\n", err);
3331 sq->cev_next_state = MLX5E_CEV_STATE_INITIAL;
3332 WRITE_ONCE(sq->running, 1);
3336 mlx5e_enable_tx_dma(struct mlx5e_channel *ch)
3340 for (i = 0; i < ch->num_tc; i++)
3341 mlx5e_resume_sq(&ch->sq[i]);
3345 mlx5e_disable_rx_dma(struct mlx5e_channel *ch)
3347 struct mlx5e_rq *rq = &ch->rq;
3352 callout_stop(&rq->watchdog);
3353 mtx_unlock(&rq->mtx);
3355 callout_drain(&rq->watchdog);
3357 err = mlx5e_modify_rq(rq, MLX5_RQC_STATE_RDY, MLX5_RQC_STATE_ERR);
3360 "mlx5e_modify_rq() from RDY to RST failed: %d\n", err);
3363 while (!mlx5_wq_ll_is_empty(&rq->wq)) {
3365 rq->cq.mcq.comp(&rq->cq.mcq);
3369 * Transitioning into RST state will allow the FW to track less ERR state queues,
3370 * thus reducing the recv queue flushing time
3372 err = mlx5e_modify_rq(rq, MLX5_RQC_STATE_ERR, MLX5_RQC_STATE_RST);
3375 "mlx5e_modify_rq() from ERR to RST failed: %d\n", err);
3380 mlx5e_enable_rx_dma(struct mlx5e_channel *ch)
3382 struct mlx5e_rq *rq = &ch->rq;
3386 mlx5_wq_ll_update_db_record(&rq->wq);
3387 err = mlx5e_modify_rq(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
3390 "mlx5e_modify_rq() from RST to RDY failed: %d\n", err);
3395 rq->cq.mcq.comp(&rq->cq.mcq);
3399 mlx5e_modify_tx_dma(struct mlx5e_priv *priv, uint8_t value)
3403 if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
3406 for (i = 0; i < priv->params.num_channels; i++) {
3408 mlx5e_disable_tx_dma(&priv->channel[i]);
3410 mlx5e_enable_tx_dma(&priv->channel[i]);
3415 mlx5e_modify_rx_dma(struct mlx5e_priv *priv, uint8_t value)
3419 if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
3422 for (i = 0; i < priv->params.num_channels; i++) {
3424 mlx5e_disable_rx_dma(&priv->channel[i]);
3426 mlx5e_enable_rx_dma(&priv->channel[i]);
3431 mlx5e_add_hw_stats(struct mlx5e_priv *priv)
3433 SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_hw),
3434 OID_AUTO, "fw_version", CTLTYPE_STRING | CTLFLAG_RD, priv, 0,
3435 sysctl_firmware, "A", "HCA firmware version");
3437 SYSCTL_ADD_STRING(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_hw),
3438 OID_AUTO, "board_id", CTLFLAG_RD, priv->mdev->board_id, 0,
3443 mlx5e_sysctl_tx_priority_flow_control(SYSCTL_HANDLER_ARGS)
3445 struct mlx5e_priv *priv = arg1;
3452 tx_pfc = priv->params.tx_priority_flow_control;
3454 /* get current value */
3455 value = (tx_pfc >> arg2) & 1;
3457 error = sysctl_handle_32(oidp, &value, 0, req);
3459 /* range check value */
3461 priv->params.tx_priority_flow_control |= (1 << arg2);
3463 priv->params.tx_priority_flow_control &= ~(1 << arg2);
3465 /* check if update is required */
3466 if (error == 0 && priv->gone == 0 &&
3467 tx_pfc != priv->params.tx_priority_flow_control) {
3468 error = -mlx5e_set_port_pfc(priv);
3469 /* restore previous value */
3471 priv->params.tx_priority_flow_control= tx_pfc;
3479 mlx5e_sysctl_rx_priority_flow_control(SYSCTL_HANDLER_ARGS)
3481 struct mlx5e_priv *priv = arg1;
3488 rx_pfc = priv->params.rx_priority_flow_control;
3490 /* get current value */
3491 value = (rx_pfc >> arg2) & 1;
3493 error = sysctl_handle_32(oidp, &value, 0, req);
3495 /* range check value */
3497 priv->params.rx_priority_flow_control |= (1 << arg2);
3499 priv->params.rx_priority_flow_control &= ~(1 << arg2);
3501 /* check if update is required */
3502 if (error == 0 && priv->gone == 0 &&
3503 rx_pfc != priv->params.rx_priority_flow_control) {
3504 error = -mlx5e_set_port_pfc(priv);
3505 /* restore previous value */
3507 priv->params.rx_priority_flow_control= rx_pfc;
3515 mlx5e_setup_pauseframes(struct mlx5e_priv *priv)
3521 /* enable pauseframes by default */
3522 priv->params.tx_pauseframe_control = 1;
3523 priv->params.rx_pauseframe_control = 1;
3525 /* disable ports flow control, PFC, by default */
3526 priv->params.tx_priority_flow_control = 0;
3527 priv->params.rx_priority_flow_control = 0;
3529 #if (__FreeBSD_version < 1100000)
3530 /* compute path for sysctl */
3531 snprintf(path, sizeof(path), "dev.mce.%d.tx_pauseframe_control",
3532 device_get_unit(priv->mdev->pdev->dev.bsddev));
3534 /* try to fetch tunable, if any */
3535 TUNABLE_INT_FETCH(path, &priv->params.tx_pauseframe_control);
3537 /* compute path for sysctl */
3538 snprintf(path, sizeof(path), "dev.mce.%d.rx_pauseframe_control",
3539 device_get_unit(priv->mdev->pdev->dev.bsddev));
3541 /* try to fetch tunable, if any */
3542 TUNABLE_INT_FETCH(path, &priv->params.rx_pauseframe_control);
3544 for (x = 0; x != 8; x++) {
3546 /* compute path for sysctl */
3547 snprintf(path, sizeof(path), "dev.mce.%d.tx_priority_flow_control_%u",
3548 device_get_unit(priv->mdev->pdev->dev.bsddev), x);
3550 /* try to fetch tunable, if any */
3551 if (TUNABLE_INT_FETCH(path, &value) == 0 && value != 0)
3552 priv->params.tx_priority_flow_control |= 1 << x;
3554 /* compute path for sysctl */
3555 snprintf(path, sizeof(path), "dev.mce.%d.rx_priority_flow_control_%u",
3556 device_get_unit(priv->mdev->pdev->dev.bsddev), x);
3558 /* try to fetch tunable, if any */
3559 if (TUNABLE_INT_FETCH(path, &value) == 0 && value != 0)
3560 priv->params.rx_priority_flow_control |= 1 << x;
3564 /* register pauseframe SYSCTLs */
3565 SYSCTL_ADD_INT(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3566 OID_AUTO, "tx_pauseframe_control", CTLFLAG_RDTUN,
3567 &priv->params.tx_pauseframe_control, 0,
3568 "Set to enable TX pause frames. Clear to disable.");
3570 SYSCTL_ADD_INT(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3571 OID_AUTO, "rx_pauseframe_control", CTLFLAG_RDTUN,
3572 &priv->params.rx_pauseframe_control, 0,
3573 "Set to enable RX pause frames. Clear to disable.");
3575 /* register priority_flow control, PFC, SYSCTLs */
3576 for (x = 0; x != 8; x++) {
3577 snprintf(path, sizeof(path), "tx_priority_flow_control_%u", x);
3579 SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3580 OID_AUTO, path, CTLTYPE_UINT | CTLFLAG_RWTUN |
3581 CTLFLAG_MPSAFE, priv, x, &mlx5e_sysctl_tx_priority_flow_control, "IU",
3582 "Set to enable TX ports flow control frames for given priority. Clear to disable.");
3584 snprintf(path, sizeof(path), "rx_priority_flow_control_%u", x);
3586 SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3587 OID_AUTO, path, CTLTYPE_UINT | CTLFLAG_RWTUN |
3588 CTLFLAG_MPSAFE, priv, x, &mlx5e_sysctl_rx_priority_flow_control, "IU",
3589 "Set to enable RX ports flow control frames for given priority. Clear to disable.");
3595 priv->params.tx_pauseframe_control =
3596 priv->params.tx_pauseframe_control ? 1 : 0;
3597 priv->params.rx_pauseframe_control =
3598 priv->params.rx_pauseframe_control ? 1 : 0;
3600 /* update firmware */
3601 error = mlx5e_set_port_pause_and_pfc(priv);
3602 if (error == -EINVAL) {
3603 if_printf(priv->ifp,
3604 "Global pauseframes must be disabled before enabling PFC.\n");
3605 priv->params.rx_priority_flow_control = 0;
3606 priv->params.tx_priority_flow_control = 0;
3608 /* update firmware */
3609 (void) mlx5e_set_port_pause_and_pfc(priv);
3615 mlx5e_ul_snd_tag_alloc(struct ifnet *ifp,
3616 union if_snd_tag_alloc_params *params,
3617 struct m_snd_tag **ppmt)
3619 struct mlx5e_priv *priv;
3620 struct mlx5e_channel *pch;
3622 priv = ifp->if_softc;
3624 if (unlikely(priv->gone || params->hdr.flowtype == M_HASHTYPE_NONE)) {
3625 return (EOPNOTSUPP);
3627 /* keep this code synced with mlx5e_select_queue() */
3628 u32 ch = priv->params.num_channels;
3632 if (rss_hash2bucket(params->hdr.flowid,
3633 params->hdr.flowtype, &temp) == 0)
3637 ch = (params->hdr.flowid % 128) % ch;
3640 * NOTE: The channels array is only freed at detach
3641 * and it safe to return a pointer to the send tag
3642 * inside the channels structure as long as we
3643 * reference the priv.
3645 pch = priv->channel + ch;
3647 /* check if send queue is not running */
3648 if (unlikely(pch->sq[0].running == 0))
3650 mlx5e_ref_channel(priv);
3651 *ppmt = &pch->tag.m_snd_tag;
3657 mlx5e_ul_snd_tag_query(struct m_snd_tag *pmt, union if_snd_tag_query_params *params)
3659 struct mlx5e_channel *pch =
3660 container_of(pmt, struct mlx5e_channel, tag.m_snd_tag);
3662 params->unlimited.max_rate = -1ULL;
3663 params->unlimited.queue_level = mlx5e_sq_queue_level(&pch->sq[0]);
3668 mlx5e_ul_snd_tag_free(struct m_snd_tag *pmt)
3670 struct mlx5e_channel *pch =
3671 container_of(pmt, struct mlx5e_channel, tag.m_snd_tag);
3673 mlx5e_unref_channel(pch->priv);
3677 mlx5e_snd_tag_alloc(struct ifnet *ifp,
3678 union if_snd_tag_alloc_params *params,
3679 struct m_snd_tag **ppmt)
3682 switch (params->hdr.type) {
3684 case IF_SND_TAG_TYPE_RATE_LIMIT:
3685 return (mlx5e_rl_snd_tag_alloc(ifp, params, ppmt));
3687 case IF_SND_TAG_TYPE_UNLIMITED:
3688 return (mlx5e_ul_snd_tag_alloc(ifp, params, ppmt));
3690 return (EOPNOTSUPP);
3695 mlx5e_snd_tag_modify(struct m_snd_tag *pmt, union if_snd_tag_modify_params *params)
3697 struct mlx5e_snd_tag *tag =
3698 container_of(pmt, struct mlx5e_snd_tag, m_snd_tag);
3700 switch (tag->type) {
3702 case IF_SND_TAG_TYPE_RATE_LIMIT:
3703 return (mlx5e_rl_snd_tag_modify(pmt, params));
3705 case IF_SND_TAG_TYPE_UNLIMITED:
3707 return (EOPNOTSUPP);
3712 mlx5e_snd_tag_query(struct m_snd_tag *pmt, union if_snd_tag_query_params *params)
3714 struct mlx5e_snd_tag *tag =
3715 container_of(pmt, struct mlx5e_snd_tag, m_snd_tag);
3717 switch (tag->type) {
3719 case IF_SND_TAG_TYPE_RATE_LIMIT:
3720 return (mlx5e_rl_snd_tag_query(pmt, params));
3722 case IF_SND_TAG_TYPE_UNLIMITED:
3723 return (mlx5e_ul_snd_tag_query(pmt, params));
3725 return (EOPNOTSUPP);
3730 mlx5e_snd_tag_free(struct m_snd_tag *pmt)
3732 struct mlx5e_snd_tag *tag =
3733 container_of(pmt, struct mlx5e_snd_tag, m_snd_tag);
3735 switch (tag->type) {
3737 case IF_SND_TAG_TYPE_RATE_LIMIT:
3738 mlx5e_rl_snd_tag_free(pmt);
3741 case IF_SND_TAG_TYPE_UNLIMITED:
3742 mlx5e_ul_snd_tag_free(pmt);
3750 mlx5e_create_ifp(struct mlx5_core_dev *mdev)
3753 struct mlx5e_priv *priv;
3754 u8 dev_addr[ETHER_ADDR_LEN] __aligned(4);
3755 struct sysctl_oid_list *child;
3756 int ncv = mdev->priv.eq_table.num_comp_vectors;
3758 struct pfil_head_args pa;
3763 if (mlx5e_check_required_hca_cap(mdev)) {
3764 mlx5_core_dbg(mdev, "mlx5e_check_required_hca_cap() failed\n");
3768 * Try to allocate the priv and make room for worst-case
3769 * number of channel structures:
3771 priv = malloc(sizeof(*priv) +
3772 (sizeof(priv->channel[0]) * mdev->priv.eq_table.num_comp_vectors),
3773 M_MLX5EN, M_WAITOK | M_ZERO);
3774 mlx5e_priv_mtx_init(priv);
3776 ifp = priv->ifp = if_alloc_dev(IFT_ETHER, mdev->pdev->dev.bsddev);
3778 mlx5_core_err(mdev, "if_alloc() failed\n");
3781 ifp->if_softc = priv;
3782 if_initname(ifp, "mce", device_get_unit(mdev->pdev->dev.bsddev));
3783 ifp->if_mtu = ETHERMTU;
3784 ifp->if_init = mlx5e_open;
3785 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
3786 ifp->if_ioctl = mlx5e_ioctl;
3787 ifp->if_transmit = mlx5e_xmit;
3788 ifp->if_qflush = if_qflush;
3789 #if (__FreeBSD_version >= 1100000)
3790 ifp->if_get_counter = mlx5e_get_counter;
3792 ifp->if_snd.ifq_maxlen = ifqmaxlen;
3794 * Set driver features
3796 ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6;
3797 ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
3798 ifp->if_capabilities |= IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWFILTER;
3799 ifp->if_capabilities |= IFCAP_LINKSTATE | IFCAP_JUMBO_MTU;
3800 ifp->if_capabilities |= IFCAP_LRO;
3801 ifp->if_capabilities |= IFCAP_TSO | IFCAP_VLAN_HWTSO;
3802 ifp->if_capabilities |= IFCAP_HWSTATS | IFCAP_HWRXTSTMP;
3803 ifp->if_capabilities |= IFCAP_TXRTLMT;
3804 ifp->if_snd_tag_alloc = mlx5e_snd_tag_alloc;
3805 ifp->if_snd_tag_free = mlx5e_snd_tag_free;
3806 ifp->if_snd_tag_modify = mlx5e_snd_tag_modify;
3807 ifp->if_snd_tag_query = mlx5e_snd_tag_query;
3809 /* set TSO limits so that we don't have to drop TX packets */
3810 ifp->if_hw_tsomax = MLX5E_MAX_TX_PAYLOAD_SIZE - (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
3811 ifp->if_hw_tsomaxsegcount = MLX5E_MAX_TX_MBUF_FRAGS - 1 /* hdr */;
3812 ifp->if_hw_tsomaxsegsize = MLX5E_MAX_TX_MBUF_SIZE;
3814 ifp->if_capenable = ifp->if_capabilities;
3815 ifp->if_hwassist = 0;
3816 if (ifp->if_capenable & IFCAP_TSO)
3817 ifp->if_hwassist |= CSUM_TSO;
3818 if (ifp->if_capenable & IFCAP_TXCSUM)
3819 ifp->if_hwassist |= (CSUM_TCP | CSUM_UDP | CSUM_IP);
3820 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
3821 ifp->if_hwassist |= (CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
3823 /* ifnet sysctl tree */
3824 sysctl_ctx_init(&priv->sysctl_ctx);
3825 priv->sysctl_ifnet = SYSCTL_ADD_NODE(&priv->sysctl_ctx, SYSCTL_STATIC_CHILDREN(_dev),
3826 OID_AUTO, ifp->if_dname, CTLFLAG_RD, 0, "MLX5 ethernet - interface name");
3827 if (priv->sysctl_ifnet == NULL) {
3828 mlx5_core_err(mdev, "SYSCTL_ADD_NODE() failed\n");
3829 goto err_free_sysctl;
3831 snprintf(unit, sizeof(unit), "%d", ifp->if_dunit);
3832 priv->sysctl_ifnet = SYSCTL_ADD_NODE(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3833 OID_AUTO, unit, CTLFLAG_RD, 0, "MLX5 ethernet - interface unit");
3834 if (priv->sysctl_ifnet == NULL) {
3835 mlx5_core_err(mdev, "SYSCTL_ADD_NODE() failed\n");
3836 goto err_free_sysctl;
3839 /* HW sysctl tree */
3840 child = SYSCTL_CHILDREN(device_get_sysctl_tree(mdev->pdev->dev.bsddev));
3841 priv->sysctl_hw = SYSCTL_ADD_NODE(&priv->sysctl_ctx, child,
3842 OID_AUTO, "hw", CTLFLAG_RD, 0, "MLX5 ethernet dev hw");
3843 if (priv->sysctl_hw == NULL) {
3844 mlx5_core_err(mdev, "SYSCTL_ADD_NODE() failed\n");
3845 goto err_free_sysctl;
3848 err = mlx5e_build_ifp_priv(mdev, priv, ncv);
3850 mlx5_core_err(mdev, "mlx5e_build_ifp_priv() failed (%d)\n", err);
3851 goto err_free_sysctl;
3854 snprintf(unit, sizeof(unit), "mce%u_wq",
3855 device_get_unit(mdev->pdev->dev.bsddev));
3856 priv->wq = alloc_workqueue(unit, 0, 1);
3857 if (priv->wq == NULL) {
3858 if_printf(ifp, "%s: alloc_workqueue failed\n", __func__);
3859 goto err_free_sysctl;
3862 err = mlx5_alloc_map_uar(mdev, &priv->cq_uar);
3864 if_printf(ifp, "%s: mlx5_alloc_map_uar failed, %d\n",
3868 err = mlx5_core_alloc_pd(mdev, &priv->pdn);
3870 if_printf(ifp, "%s: mlx5_core_alloc_pd failed, %d\n",
3872 goto err_unmap_free_uar;
3874 err = mlx5_alloc_transport_domain(mdev, &priv->tdn);
3876 if_printf(ifp, "%s: mlx5_alloc_transport_domain failed, %d\n",
3878 goto err_dealloc_pd;
3880 err = mlx5e_create_mkey(priv, priv->pdn, &priv->mr);
3882 if_printf(ifp, "%s: mlx5e_create_mkey failed, %d\n",
3884 goto err_dealloc_transport_domain;
3886 mlx5_query_nic_vport_mac_address(priv->mdev, 0, dev_addr);
3888 /* check if we should generate a random MAC address */
3889 if (MLX5_CAP_GEN(priv->mdev, vport_group_manager) == 0 &&
3890 is_zero_ether_addr(dev_addr)) {
3891 random_ether_addr(dev_addr);
3892 if_printf(ifp, "Assigned random MAC address\n");
3895 err = mlx5e_rl_init(priv);
3897 if_printf(ifp, "%s: mlx5e_rl_init failed, %d\n",
3899 goto err_create_mkey;
3903 /* set default MTU */
3904 mlx5e_set_dev_port_mtu(ifp, ifp->if_mtu);
3906 /* Set default media status */
3907 priv->media_status_last = IFM_AVALID;
3908 priv->media_active_last = IFM_ETHER | IFM_AUTO |
3909 IFM_ETH_RXPAUSE | IFM_FDX;
3911 /* setup default pauseframes configuration */
3912 mlx5e_setup_pauseframes(priv);
3914 err = mlx5_query_port_proto_cap(mdev, ð_proto_cap, MLX5_PTYS_EN);
3917 if_printf(ifp, "%s: Query port media capability failed, %d\n",
3921 /* Setup supported medias */
3922 ifmedia_init(&priv->media, IFM_IMASK | IFM_ETH_FMASK,
3923 mlx5e_media_change, mlx5e_media_status);
3925 for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
3926 if (mlx5e_mode_table[i].baudrate == 0)
3928 if (MLX5E_PROT_MASK(i) & eth_proto_cap) {
3929 ifmedia_add(&priv->media,
3930 mlx5e_mode_table[i].subtype |
3931 IFM_ETHER, 0, NULL);
3932 ifmedia_add(&priv->media,
3933 mlx5e_mode_table[i].subtype |
3934 IFM_ETHER | IFM_FDX |
3935 IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE, 0, NULL);
3939 /* Additional supported medias */
3940 ifmedia_add(&priv->media, IFM_10G_LR | IFM_ETHER, 0, NULL);
3941 ifmedia_add(&priv->media, IFM_10G_LR |
3942 IFM_ETHER | IFM_FDX |
3943 IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE, 0, NULL);
3945 ifmedia_add(&priv->media, IFM_40G_ER4 | IFM_ETHER, 0, NULL);
3946 ifmedia_add(&priv->media, IFM_40G_ER4 |
3947 IFM_ETHER | IFM_FDX |
3948 IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE, 0, NULL);
3950 ifmedia_add(&priv->media, IFM_ETHER | IFM_AUTO, 0, NULL);
3951 ifmedia_add(&priv->media, IFM_ETHER | IFM_AUTO | IFM_FDX |
3952 IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE, 0, NULL);
3954 /* Set autoselect by default */
3955 ifmedia_set(&priv->media, IFM_ETHER | IFM_AUTO | IFM_FDX |
3956 IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE);
3957 ether_ifattach(ifp, dev_addr);
3959 /* Register for VLAN events */
3960 priv->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
3961 mlx5e_vlan_rx_add_vid, priv, EVENTHANDLER_PRI_FIRST);
3962 priv->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
3963 mlx5e_vlan_rx_kill_vid, priv, EVENTHANDLER_PRI_FIRST);
3965 /* Link is down by default */
3966 if_link_state_change(ifp, LINK_STATE_DOWN);
3968 mlx5e_enable_async_events(priv);
3970 mlx5e_add_hw_stats(priv);
3972 mlx5e_create_stats(&priv->stats.vport.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3973 "vstats", mlx5e_vport_stats_desc, MLX5E_VPORT_STATS_NUM,
3974 priv->stats.vport.arg);
3976 mlx5e_create_stats(&priv->stats.pport.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3977 "pstats", mlx5e_pport_stats_desc, MLX5E_PPORT_STATS_NUM,
3978 priv->stats.pport.arg);
3980 mlx5e_create_ethtool(priv);
3982 mtx_lock(&priv->async_events_mtx);
3983 mlx5e_update_stats(priv);
3984 mtx_unlock(&priv->async_events_mtx);
3986 SYSCTL_ADD_INT(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3987 OID_AUTO, "rx_clbr_done", CTLFLAG_RD,
3988 &priv->clbr_done, 0,
3989 "RX timestamps calibration state");
3990 callout_init(&priv->tstmp_clbr, CALLOUT_DIRECT);
3991 mlx5e_reset_calibration_callout(priv);
3993 pa.pa_version = PFIL_VERSION;
3994 pa.pa_flags = PFIL_IN;
3995 pa.pa_type = PFIL_TYPE_ETHERNET;
3996 pa.pa_headname = ifp->if_xname;
3997 priv->pfil = pfil_head_register(&pa);
4003 mlx5_core_destroy_mkey(priv->mdev, &priv->mr);
4005 err_dealloc_transport_domain:
4006 mlx5_dealloc_transport_domain(mdev, priv->tdn);
4009 mlx5_core_dealloc_pd(mdev, priv->pdn);
4012 mlx5_unmap_free_uar(mdev, &priv->cq_uar);
4015 destroy_workqueue(priv->wq);
4018 sysctl_ctx_free(&priv->sysctl_ctx);
4019 if (priv->sysctl_debug)
4020 sysctl_ctx_free(&priv->stats.port_stats_debug.ctx);
4024 mlx5e_priv_mtx_destroy(priv);
4025 free(priv, M_MLX5EN);
4030 mlx5e_destroy_ifp(struct mlx5_core_dev *mdev, void *vpriv)
4032 struct mlx5e_priv *priv = vpriv;
4033 struct ifnet *ifp = priv->ifp;
4035 /* don't allow more IOCTLs */
4038 /* XXX wait a bit to allow IOCTL handlers to complete */
4043 * The kernel can have reference(s) via the m_snd_tag's into
4044 * the ratelimit channels, and these must go away before
4047 while (READ_ONCE(priv->rl.stats.tx_active_connections) != 0) {
4048 if_printf(priv->ifp, "Waiting for all ratelimit connections "
4053 /* stop watchdog timer */
4054 callout_drain(&priv->watchdog);
4056 callout_drain(&priv->tstmp_clbr);
4058 if (priv->vlan_attach != NULL)
4059 EVENTHANDLER_DEREGISTER(vlan_config, priv->vlan_attach);
4060 if (priv->vlan_detach != NULL)
4061 EVENTHANDLER_DEREGISTER(vlan_unconfig, priv->vlan_detach);
4063 /* make sure device gets closed */
4065 mlx5e_close_locked(ifp);
4068 /* wait for all unlimited send tags to go away */
4069 while (priv->channel_refs != 0) {
4070 if_printf(priv->ifp, "Waiting for all unlimited connections "
4075 /* deregister pfil */
4076 if (priv->pfil != NULL) {
4077 pfil_head_unregister(priv->pfil);
4081 /* unregister device */
4082 ifmedia_removeall(&priv->media);
4083 ether_ifdetach(ifp);
4087 mlx5e_rl_cleanup(priv);
4089 /* destroy all remaining sysctl nodes */
4090 sysctl_ctx_free(&priv->stats.vport.ctx);
4091 sysctl_ctx_free(&priv->stats.pport.ctx);
4092 if (priv->sysctl_debug)
4093 sysctl_ctx_free(&priv->stats.port_stats_debug.ctx);
4094 sysctl_ctx_free(&priv->sysctl_ctx);
4096 mlx5_core_destroy_mkey(priv->mdev, &priv->mr);
4097 mlx5_dealloc_transport_domain(priv->mdev, priv->tdn);
4098 mlx5_core_dealloc_pd(priv->mdev, priv->pdn);
4099 mlx5_unmap_free_uar(priv->mdev, &priv->cq_uar);
4100 mlx5e_disable_async_events(priv);
4101 destroy_workqueue(priv->wq);
4102 mlx5e_priv_mtx_destroy(priv);
4103 free(priv, M_MLX5EN);
4107 mlx5e_get_ifp(void *vpriv)
4109 struct mlx5e_priv *priv = vpriv;
4114 static struct mlx5_interface mlx5e_interface = {
4115 .add = mlx5e_create_ifp,
4116 .remove = mlx5e_destroy_ifp,
4117 .event = mlx5e_async_event,
4118 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
4119 .get_dev = mlx5e_get_ifp,
4125 mlx5_register_interface(&mlx5e_interface);
4131 mlx5_unregister_interface(&mlx5e_interface);
4135 mlx5e_show_version(void __unused *arg)
4138 printf("%s", mlx5e_version);
4140 SYSINIT(mlx5e_show_version, SI_SUB_DRIVERS, SI_ORDER_ANY, mlx5e_show_version, NULL);
4142 module_init_order(mlx5e_init, SI_ORDER_THIRD);
4143 module_exit_order(mlx5e_cleanup, SI_ORDER_THIRD);
4145 #if (__FreeBSD_version >= 1100000)
4146 MODULE_DEPEND(mlx5en, linuxkpi, 1, 1, 1);
4148 MODULE_DEPEND(mlx5en, mlx5, 1, 1, 1);
4149 MODULE_VERSION(mlx5en, 1);