2 * Copyright (c) 2015 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 #define ETH_DRIVER_VERSION "3.1.0-dev"
34 char mlx5e_version[] = "Mellanox Ethernet driver"
35 " (" ETH_DRIVER_VERSION ")";
37 struct mlx5e_channel_param {
38 struct mlx5e_rq_param rq;
39 struct mlx5e_sq_param sq;
40 struct mlx5e_cq_param rx_cq;
41 struct mlx5e_cq_param tx_cq;
47 } mlx5e_mode_table[MLX5E_LINK_MODES_NUMBER] = {
49 [MLX5E_1000BASE_CX_SGMII] = {
50 .subtype = IFM_1000_CX_SGMII,
51 .baudrate = IF_Mbps(1000ULL),
53 [MLX5E_1000BASE_KX] = {
54 .subtype = IFM_1000_KX,
55 .baudrate = IF_Mbps(1000ULL),
57 [MLX5E_10GBASE_CX4] = {
58 .subtype = IFM_10G_CX4,
59 .baudrate = IF_Gbps(10ULL),
61 [MLX5E_10GBASE_KX4] = {
62 .subtype = IFM_10G_KX4,
63 .baudrate = IF_Gbps(10ULL),
65 [MLX5E_10GBASE_KR] = {
66 .subtype = IFM_10G_KR,
67 .baudrate = IF_Gbps(10ULL),
69 [MLX5E_20GBASE_KR2] = {
70 .subtype = IFM_20G_KR2,
71 .baudrate = IF_Gbps(20ULL),
73 [MLX5E_40GBASE_CR4] = {
74 .subtype = IFM_40G_CR4,
75 .baudrate = IF_Gbps(40ULL),
77 [MLX5E_40GBASE_KR4] = {
78 .subtype = IFM_40G_KR4,
79 .baudrate = IF_Gbps(40ULL),
81 [MLX5E_56GBASE_R4] = {
82 .subtype = IFM_56G_R4,
83 .baudrate = IF_Gbps(56ULL),
85 [MLX5E_10GBASE_CR] = {
86 .subtype = IFM_10G_CR1,
87 .baudrate = IF_Gbps(10ULL),
89 [MLX5E_10GBASE_SR] = {
90 .subtype = IFM_10G_SR,
91 .baudrate = IF_Gbps(10ULL),
93 [MLX5E_10GBASE_LR] = {
94 .subtype = IFM_10G_LR,
95 .baudrate = IF_Gbps(10ULL),
97 [MLX5E_40GBASE_SR4] = {
98 .subtype = IFM_40G_SR4,
99 .baudrate = IF_Gbps(40ULL),
101 [MLX5E_40GBASE_LR4] = {
102 .subtype = IFM_40G_LR4,
103 .baudrate = IF_Gbps(40ULL),
105 [MLX5E_100GBASE_CR4] = {
106 .subtype = IFM_100G_CR4,
107 .baudrate = IF_Gbps(100ULL),
109 [MLX5E_100GBASE_SR4] = {
110 .subtype = IFM_100G_SR4,
111 .baudrate = IF_Gbps(100ULL),
113 [MLX5E_100GBASE_KR4] = {
114 .subtype = IFM_100G_KR4,
115 .baudrate = IF_Gbps(100ULL),
117 [MLX5E_100GBASE_LR4] = {
118 .subtype = IFM_100G_LR4,
119 .baudrate = IF_Gbps(100ULL),
121 [MLX5E_100BASE_TX] = {
122 .subtype = IFM_100_TX,
123 .baudrate = IF_Mbps(100ULL),
125 [MLX5E_100BASE_T] = {
126 .subtype = IFM_100_T,
127 .baudrate = IF_Mbps(100ULL),
129 [MLX5E_10GBASE_T] = {
130 .subtype = IFM_10G_T,
131 .baudrate = IF_Gbps(10ULL),
133 [MLX5E_25GBASE_CR] = {
134 .subtype = IFM_25G_CR,
135 .baudrate = IF_Gbps(25ULL),
137 [MLX5E_25GBASE_KR] = {
138 .subtype = IFM_25G_KR,
139 .baudrate = IF_Gbps(25ULL),
141 [MLX5E_25GBASE_SR] = {
142 .subtype = IFM_25G_SR,
143 .baudrate = IF_Gbps(25ULL),
145 [MLX5E_50GBASE_CR2] = {
146 .subtype = IFM_50G_CR2,
147 .baudrate = IF_Gbps(50ULL),
149 [MLX5E_50GBASE_KR2] = {
150 .subtype = IFM_50G_KR2,
151 .baudrate = IF_Gbps(50ULL),
155 MALLOC_DEFINE(M_MLX5EN, "MLX5EN", "MLX5 Ethernet");
158 mlx5e_update_carrier(struct mlx5e_priv *priv)
160 struct mlx5_core_dev *mdev = priv->mdev;
161 u32 out[MLX5_ST_SZ_DW(ptys_reg)];
167 port_state = mlx5_query_vport_state(mdev,
168 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT, 0);
170 if (port_state == VPORT_STATE_UP) {
171 priv->media_status_last |= IFM_ACTIVE;
173 priv->media_status_last &= ~IFM_ACTIVE;
174 priv->media_active_last = IFM_ETHER;
175 if_link_state_change(priv->ifp, LINK_STATE_DOWN);
179 error = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN);
181 priv->media_active_last = IFM_ETHER;
182 priv->ifp->if_baudrate = 1;
183 if_printf(priv->ifp, "%s: query port ptys failed: 0x%x\n",
187 eth_proto_oper = MLX5_GET(ptys_reg, out, eth_proto_oper);
189 for (i = 0; i != MLX5E_LINK_MODES_NUMBER; i++) {
190 if (mlx5e_mode_table[i].baudrate == 0)
192 if (MLX5E_PROT_MASK(i) & eth_proto_oper) {
193 priv->ifp->if_baudrate =
194 mlx5e_mode_table[i].baudrate;
195 priv->media_active_last =
196 mlx5e_mode_table[i].subtype | IFM_ETHER | IFM_FDX;
199 if_link_state_change(priv->ifp, LINK_STATE_UP);
203 mlx5e_media_status(struct ifnet *dev, struct ifmediareq *ifmr)
205 struct mlx5e_priv *priv = dev->if_softc;
207 ifmr->ifm_status = priv->media_status_last;
208 ifmr->ifm_active = priv->media_active_last |
209 (priv->params.rx_pauseframe_control ? IFM_ETH_RXPAUSE : 0) |
210 (priv->params.tx_pauseframe_control ? IFM_ETH_TXPAUSE : 0);
215 mlx5e_find_link_mode(u32 subtype)
220 for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
221 if (mlx5e_mode_table[i].baudrate == 0)
223 if (mlx5e_mode_table[i].subtype == subtype)
224 link_mode |= MLX5E_PROT_MASK(i);
231 mlx5e_media_change(struct ifnet *dev)
233 struct mlx5e_priv *priv = dev->if_softc;
234 struct mlx5_core_dev *mdev = priv->mdev;
241 locked = PRIV_LOCKED(priv);
245 if (IFM_TYPE(priv->media.ifm_media) != IFM_ETHER) {
249 link_mode = mlx5e_find_link_mode(IFM_SUBTYPE(priv->media.ifm_media));
251 /* query supported capabilities */
252 error = mlx5_query_port_proto_cap(mdev, ð_proto_cap, MLX5_PTYS_EN);
254 if_printf(dev, "Query port media capability failed\n");
257 /* check for autoselect */
258 if (IFM_SUBTYPE(priv->media.ifm_media) == IFM_AUTO) {
259 link_mode = eth_proto_cap;
260 if (link_mode == 0) {
261 if_printf(dev, "Port media capability is zero\n");
266 link_mode = link_mode & eth_proto_cap;
267 if (link_mode == 0) {
268 if_printf(dev, "Not supported link mode requested\n");
273 /* update pauseframe control bits */
274 priv->params.rx_pauseframe_control =
275 (priv->media.ifm_media & IFM_ETH_RXPAUSE) ? 1 : 0;
276 priv->params.tx_pauseframe_control =
277 (priv->media.ifm_media & IFM_ETH_TXPAUSE) ? 1 : 0;
279 /* check if device is opened */
280 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
282 /* reconfigure the hardware */
283 mlx5_set_port_status(mdev, MLX5_PORT_DOWN);
284 mlx5_set_port_proto(mdev, link_mode, MLX5_PTYS_EN);
285 mlx5_set_port_pause(mdev, 1,
286 priv->params.rx_pauseframe_control,
287 priv->params.tx_pauseframe_control);
289 mlx5_set_port_status(mdev, MLX5_PORT_UP);
298 mlx5e_update_carrier_work(struct work_struct *work)
300 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
301 update_carrier_work);
304 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
305 mlx5e_update_carrier(priv);
310 * This function reads the physical port counters from the firmware
311 * using a pre-defined layout defined by various MLX5E_PPORT_XXX()
312 * macros. The output is converted from big-endian 64-bit values into
313 * host endian ones and stored in the "priv->stats.pport" structure.
316 mlx5e_update_pport_counters(struct mlx5e_priv *priv)
318 struct mlx5_core_dev *mdev = priv->mdev;
319 struct mlx5e_pport_stats *s = &priv->stats.pport;
320 struct mlx5e_port_stats_debug *s_debug = &priv->stats.port_stats_debug;
324 unsigned sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
328 /* allocate firmware request structures */
329 in = mlx5_vzalloc(sz);
330 out = mlx5_vzalloc(sz);
331 if (in == NULL || out == NULL)
335 * Get pointer to the 64-bit counter set which is located at a
336 * fixed offset in the output firmware request structure:
338 ptr = (const uint64_t *)MLX5_ADDR_OF(ppcnt_reg, out, counter_set);
340 MLX5_SET(ppcnt_reg, in, local_port, 1);
342 /* read IEEE802_3 counter group using predefined counter layout */
343 MLX5_SET(ppcnt_reg, in, grp, MLX5_IEEE_802_3_COUNTERS_GROUP);
344 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
345 for (x = y = 0; x != MLX5E_PPORT_IEEE802_3_STATS_NUM; x++, y++)
346 s->arg[y] = be64toh(ptr[x]);
348 /* read RFC2819 counter group using predefined counter layout */
349 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2819_COUNTERS_GROUP);
350 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
351 for (x = 0; x != MLX5E_PPORT_RFC2819_STATS_NUM; x++, y++)
352 s->arg[y] = be64toh(ptr[x]);
353 for (y = 0; x != MLX5E_PPORT_RFC2819_STATS_NUM +
354 MLX5E_PPORT_RFC2819_STATS_DEBUG_NUM; x++, y++)
355 s_debug->arg[y] = be64toh(ptr[x]);
357 /* read RFC2863 counter group using predefined counter layout */
358 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2863_COUNTERS_GROUP);
359 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
360 for (x = 0; x != MLX5E_PPORT_RFC2863_STATS_DEBUG_NUM; x++, y++)
361 s_debug->arg[y] = be64toh(ptr[x]);
363 /* read physical layer stats counter group using predefined counter layout */
364 MLX5_SET(ppcnt_reg, in, grp, MLX5_PHYSICAL_LAYER_COUNTERS_GROUP);
365 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
366 for (x = 0; x != MLX5E_PPORT_PHYSICAL_LAYER_STATS_DEBUG_NUM; x++, y++)
367 s_debug->arg[y] = be64toh(ptr[x]);
369 /* free firmware request structures */
375 * This function is called regularly to collect all statistics
376 * counters from the firmware. The values can be viewed through the
377 * sysctl interface. Execution is serialized using the priv's global
378 * configuration lock.
381 mlx5e_update_stats_work(struct work_struct *work)
383 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
385 struct mlx5_core_dev *mdev = priv->mdev;
386 struct mlx5e_vport_stats *s = &priv->stats.vport;
387 struct mlx5e_rq_stats *rq_stats;
388 struct mlx5e_sq_stats *sq_stats;
389 struct buf_ring *sq_br;
390 #if (__FreeBSD_version < 1100000)
391 struct ifnet *ifp = priv->ifp;
394 u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)];
396 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
399 u64 tx_queue_dropped = 0;
400 u64 tx_defragged = 0;
401 u64 tx_offload_none = 0;
404 u64 sw_lro_queued = 0;
405 u64 sw_lro_flushed = 0;
406 u64 rx_csum_none = 0;
408 u32 rx_out_of_buffer = 0;
413 out = mlx5_vzalloc(outlen);
416 if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
419 /* Collect firts the SW counters and then HW for consistency */
420 for (i = 0; i < priv->params.num_channels; i++) {
421 struct mlx5e_rq *rq = &priv->channel[i]->rq;
423 rq_stats = &priv->channel[i]->rq.stats;
425 /* collect stats from LRO */
426 rq_stats->sw_lro_queued = rq->lro.lro_queued;
427 rq_stats->sw_lro_flushed = rq->lro.lro_flushed;
428 sw_lro_queued += rq_stats->sw_lro_queued;
429 sw_lro_flushed += rq_stats->sw_lro_flushed;
430 lro_packets += rq_stats->lro_packets;
431 lro_bytes += rq_stats->lro_bytes;
432 rx_csum_none += rq_stats->csum_none;
433 rx_wqe_err += rq_stats->wqe_err;
435 for (j = 0; j < priv->num_tc; j++) {
436 sq_stats = &priv->channel[i]->sq[j].stats;
437 sq_br = priv->channel[i]->sq[j].br;
439 tso_packets += sq_stats->tso_packets;
440 tso_bytes += sq_stats->tso_bytes;
441 tx_queue_dropped += sq_stats->dropped;
443 tx_queue_dropped += sq_br->br_drops;
444 tx_defragged += sq_stats->defragged;
445 tx_offload_none += sq_stats->csum_offload_none;
449 /* update counters */
450 s->tso_packets = tso_packets;
451 s->tso_bytes = tso_bytes;
452 s->tx_queue_dropped = tx_queue_dropped;
453 s->tx_defragged = tx_defragged;
454 s->lro_packets = lro_packets;
455 s->lro_bytes = lro_bytes;
456 s->sw_lro_queued = sw_lro_queued;
457 s->sw_lro_flushed = sw_lro_flushed;
458 s->rx_csum_none = rx_csum_none;
459 s->rx_wqe_err = rx_wqe_err;
462 memset(in, 0, sizeof(in));
464 MLX5_SET(query_vport_counter_in, in, opcode,
465 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
466 MLX5_SET(query_vport_counter_in, in, op_mod, 0);
467 MLX5_SET(query_vport_counter_in, in, other_vport, 0);
469 memset(out, 0, outlen);
471 /* get number of out-of-buffer drops first */
472 if (mlx5_vport_query_out_of_rx_buffer(mdev, priv->counter_set_id,
476 /* accumulate difference into a 64-bit counter */
477 s->rx_out_of_buffer += (u64)(u32)(rx_out_of_buffer - s->rx_out_of_buffer_prev);
478 s->rx_out_of_buffer_prev = rx_out_of_buffer;
480 /* get port statistics */
481 if (mlx5_cmd_exec(mdev, in, sizeof(in), out, outlen))
484 #define MLX5_GET_CTR(out, x) \
485 MLX5_GET64(query_vport_counter_out, out, x)
487 s->rx_error_packets =
488 MLX5_GET_CTR(out, received_errors.packets);
490 MLX5_GET_CTR(out, received_errors.octets);
491 s->tx_error_packets =
492 MLX5_GET_CTR(out, transmit_errors.packets);
494 MLX5_GET_CTR(out, transmit_errors.octets);
496 s->rx_unicast_packets =
497 MLX5_GET_CTR(out, received_eth_unicast.packets);
498 s->rx_unicast_bytes =
499 MLX5_GET_CTR(out, received_eth_unicast.octets);
500 s->tx_unicast_packets =
501 MLX5_GET_CTR(out, transmitted_eth_unicast.packets);
502 s->tx_unicast_bytes =
503 MLX5_GET_CTR(out, transmitted_eth_unicast.octets);
505 s->rx_multicast_packets =
506 MLX5_GET_CTR(out, received_eth_multicast.packets);
507 s->rx_multicast_bytes =
508 MLX5_GET_CTR(out, received_eth_multicast.octets);
509 s->tx_multicast_packets =
510 MLX5_GET_CTR(out, transmitted_eth_multicast.packets);
511 s->tx_multicast_bytes =
512 MLX5_GET_CTR(out, transmitted_eth_multicast.octets);
514 s->rx_broadcast_packets =
515 MLX5_GET_CTR(out, received_eth_broadcast.packets);
516 s->rx_broadcast_bytes =
517 MLX5_GET_CTR(out, received_eth_broadcast.octets);
518 s->tx_broadcast_packets =
519 MLX5_GET_CTR(out, transmitted_eth_broadcast.packets);
520 s->tx_broadcast_bytes =
521 MLX5_GET_CTR(out, transmitted_eth_broadcast.octets);
524 s->rx_unicast_packets +
525 s->rx_multicast_packets +
526 s->rx_broadcast_packets -
529 s->rx_unicast_bytes +
530 s->rx_multicast_bytes +
531 s->rx_broadcast_bytes;
533 s->tx_unicast_packets +
534 s->tx_multicast_packets +
535 s->tx_broadcast_packets;
537 s->tx_unicast_bytes +
538 s->tx_multicast_bytes +
539 s->tx_broadcast_bytes;
541 /* Update calculated offload counters */
542 s->tx_csum_offload = s->tx_packets - tx_offload_none;
543 s->rx_csum_good = s->rx_packets - s->rx_csum_none;
545 /* Get physical port counters */
546 mlx5e_update_pport_counters(priv);
548 #if (__FreeBSD_version < 1100000)
549 /* no get_counters interface in fbsd 10 */
550 ifp->if_ipackets = s->rx_packets;
551 ifp->if_ierrors = s->rx_error_packets +
552 priv->stats.pport.alignment_err +
553 priv->stats.pport.check_seq_err +
554 priv->stats.pport.crc_align_errors +
555 priv->stats.pport.drop_events +
556 priv->stats.pport.in_range_len_errors +
557 priv->stats.pport.jabbers +
558 priv->stats.pport.out_of_range_len +
559 priv->stats.pport.oversize_pkts +
560 priv->stats.pport.symbol_err +
561 priv->stats.pport.too_long_errors +
562 priv->stats.pport.undersize_pkts +
563 priv->stats.pport.unsupported_op_rx;
564 ifp->if_iqdrops = s->rx_out_of_buffer;
565 ifp->if_opackets = s->tx_packets;
566 ifp->if_oerrors = s->tx_error_packets;
567 ifp->if_snd.ifq_drops = s->tx_queue_dropped;
568 ifp->if_ibytes = s->rx_bytes;
569 ifp->if_obytes = s->tx_bytes;
571 priv->stats.pport.collisions;
577 /* Update diagnostics, if any */
578 if (priv->params_ethtool.diag_pci_enable ||
579 priv->params_ethtool.diag_general_enable) {
580 int error = mlx5_core_get_diagnostics_full(mdev,
581 priv->params_ethtool.diag_pci_enable ? &priv->params_pci : NULL,
582 priv->params_ethtool.diag_general_enable ? &priv->params_general : NULL);
584 if_printf(priv->ifp, "Failed reading diagnostics: %d\n", error);
590 mlx5e_update_stats(void *arg)
592 struct mlx5e_priv *priv = arg;
594 schedule_work(&priv->update_stats_work);
596 callout_reset(&priv->watchdog, hz, &mlx5e_update_stats, priv);
600 mlx5e_async_event_sub(struct mlx5e_priv *priv,
601 enum mlx5_dev_event event)
604 case MLX5_DEV_EVENT_PORT_UP:
605 case MLX5_DEV_EVENT_PORT_DOWN:
606 schedule_work(&priv->update_carrier_work);
615 mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
616 enum mlx5_dev_event event, unsigned long param)
618 struct mlx5e_priv *priv = vpriv;
620 mtx_lock(&priv->async_events_mtx);
621 if (test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state))
622 mlx5e_async_event_sub(priv, event);
623 mtx_unlock(&priv->async_events_mtx);
627 mlx5e_enable_async_events(struct mlx5e_priv *priv)
629 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state);
633 mlx5e_disable_async_events(struct mlx5e_priv *priv)
635 mtx_lock(&priv->async_events_mtx);
636 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state);
637 mtx_unlock(&priv->async_events_mtx);
640 static const char *mlx5e_rq_stats_desc[] = {
641 MLX5E_RQ_STATS(MLX5E_STATS_DESC)
645 mlx5e_create_rq(struct mlx5e_channel *c,
646 struct mlx5e_rq_param *param,
649 struct mlx5e_priv *priv = c->priv;
650 struct mlx5_core_dev *mdev = priv->mdev;
652 void *rqc = param->rqc;
653 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
658 /* Create DMA descriptor TAG */
659 if ((err = -bus_dma_tag_create(
660 bus_get_dma_tag(mdev->pdev->dev.bsddev),
661 1, /* any alignment */
663 BUS_SPACE_MAXADDR, /* lowaddr */
664 BUS_SPACE_MAXADDR, /* highaddr */
665 NULL, NULL, /* filter, filterarg */
666 MJUM16BYTES, /* maxsize */
668 MJUM16BYTES, /* maxsegsize */
670 NULL, NULL, /* lockfunc, lockfuncarg */
674 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
677 goto err_free_dma_tag;
679 rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
681 if (priv->params.hw_lro_en) {
682 rq->wqe_sz = priv->params.lro_wqe_sz;
684 rq->wqe_sz = MLX5E_SW2MB_MTU(priv->ifp->if_mtu);
686 if (rq->wqe_sz > MJUM16BYTES) {
688 goto err_rq_wq_destroy;
689 } else if (rq->wqe_sz > MJUM9BYTES) {
690 rq->wqe_sz = MJUM16BYTES;
691 } else if (rq->wqe_sz > MJUMPAGESIZE) {
692 rq->wqe_sz = MJUM9BYTES;
693 } else if (rq->wqe_sz > MCLBYTES) {
694 rq->wqe_sz = MJUMPAGESIZE;
696 rq->wqe_sz = MCLBYTES;
699 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
701 err = -tcp_lro_init_args(&rq->lro, c->ifp, TCP_LRO_ENTRIES, wq_sz);
703 goto err_rq_wq_destroy;
705 rq->mbuf = malloc(wq_sz * sizeof(rq->mbuf[0]), M_MLX5EN, M_WAITOK | M_ZERO);
706 for (i = 0; i != wq_sz; i++) {
707 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
708 uint32_t byte_count = rq->wqe_sz - MLX5E_NET_IP_ALIGN;
710 err = -bus_dmamap_create(rq->dma_tag, 0, &rq->mbuf[i].dma_map);
713 bus_dmamap_destroy(rq->dma_tag, rq->mbuf[i].dma_map);
714 goto err_rq_mbuf_free;
716 wqe->data.lkey = c->mkey_be;
717 wqe->data.byte_count = cpu_to_be32(byte_count | MLX5_HW_START_PADDING);
724 snprintf(buffer, sizeof(buffer), "rxstat%d", c->ix);
725 mlx5e_create_stats(&rq->stats.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
726 buffer, mlx5e_rq_stats_desc, MLX5E_RQ_STATS_NUM,
731 free(rq->mbuf, M_MLX5EN);
732 tcp_lro_free(&rq->lro);
734 mlx5_wq_destroy(&rq->wq_ctrl);
736 bus_dma_tag_destroy(rq->dma_tag);
742 mlx5e_destroy_rq(struct mlx5e_rq *rq)
747 /* destroy all sysctl nodes */
748 sysctl_ctx_free(&rq->stats.ctx);
750 /* free leftover LRO packets, if any */
751 tcp_lro_free(&rq->lro);
753 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
754 for (i = 0; i != wq_sz; i++) {
755 if (rq->mbuf[i].mbuf != NULL) {
756 bus_dmamap_unload(rq->dma_tag,
757 rq->mbuf[i].dma_map);
758 m_freem(rq->mbuf[i].mbuf);
760 bus_dmamap_destroy(rq->dma_tag, rq->mbuf[i].dma_map);
762 free(rq->mbuf, M_MLX5EN);
763 mlx5_wq_destroy(&rq->wq_ctrl);
767 mlx5e_enable_rq(struct mlx5e_rq *rq, struct mlx5e_rq_param *param)
769 struct mlx5e_channel *c = rq->channel;
770 struct mlx5e_priv *priv = c->priv;
771 struct mlx5_core_dev *mdev = priv->mdev;
779 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
780 sizeof(u64) * rq->wq_ctrl.buf.npages;
781 in = mlx5_vzalloc(inlen);
785 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
786 wq = MLX5_ADDR_OF(rqc, rqc, wq);
788 memcpy(rqc, param->rqc, sizeof(param->rqc));
790 MLX5_SET(rqc, rqc, cqn, c->rq.cq.mcq.cqn);
791 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
792 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
793 if (priv->counter_set_id >= 0)
794 MLX5_SET(rqc, rqc, counter_set_id, priv->counter_set_id);
795 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
797 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
799 mlx5_fill_page_array(&rq->wq_ctrl.buf,
800 (__be64 *) MLX5_ADDR_OF(wq, wq, pas));
802 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
810 mlx5e_modify_rq(struct mlx5e_rq *rq, int curr_state, int next_state)
812 struct mlx5e_channel *c = rq->channel;
813 struct mlx5e_priv *priv = c->priv;
814 struct mlx5_core_dev *mdev = priv->mdev;
821 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
822 in = mlx5_vzalloc(inlen);
826 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
828 MLX5_SET(modify_rq_in, in, rqn, rq->rqn);
829 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
830 MLX5_SET(rqc, rqc, state, next_state);
832 err = mlx5_core_modify_rq(mdev, in, inlen);
840 mlx5e_disable_rq(struct mlx5e_rq *rq)
842 struct mlx5e_channel *c = rq->channel;
843 struct mlx5e_priv *priv = c->priv;
844 struct mlx5_core_dev *mdev = priv->mdev;
846 mlx5_core_destroy_rq(mdev, rq->rqn);
850 mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq)
852 struct mlx5e_channel *c = rq->channel;
853 struct mlx5e_priv *priv = c->priv;
854 struct mlx5_wq_ll *wq = &rq->wq;
857 for (i = 0; i < 1000; i++) {
858 if (wq->cur_sz >= priv->params.min_rx_wqes)
867 mlx5e_open_rq(struct mlx5e_channel *c,
868 struct mlx5e_rq_param *param,
873 err = mlx5e_create_rq(c, param, rq);
877 err = mlx5e_enable_rq(rq, param);
881 err = mlx5e_modify_rq(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
890 mlx5e_disable_rq(rq);
892 mlx5e_destroy_rq(rq);
898 mlx5e_close_rq(struct mlx5e_rq *rq)
902 callout_stop(&rq->watchdog);
903 mtx_unlock(&rq->mtx);
905 callout_drain(&rq->watchdog);
907 mlx5e_modify_rq(rq, MLX5_RQC_STATE_RDY, MLX5_RQC_STATE_ERR);
911 mlx5e_close_rq_wait(struct mlx5e_rq *rq)
913 /* wait till RQ is empty */
914 while (!mlx5_wq_ll_is_empty(&rq->wq)) {
916 rq->cq.mcq.comp(&rq->cq.mcq);
919 mlx5e_disable_rq(rq);
920 mlx5e_destroy_rq(rq);
924 mlx5e_free_sq_db(struct mlx5e_sq *sq)
926 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
929 for (x = 0; x != wq_sz; x++)
930 bus_dmamap_destroy(sq->dma_tag, sq->mbuf[x].dma_map);
931 free(sq->mbuf, M_MLX5EN);
935 mlx5e_alloc_sq_db(struct mlx5e_sq *sq)
937 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
941 sq->mbuf = malloc(wq_sz * sizeof(sq->mbuf[0]), M_MLX5EN, M_WAITOK | M_ZERO);
943 /* Create DMA descriptor MAPs */
944 for (x = 0; x != wq_sz; x++) {
945 err = -bus_dmamap_create(sq->dma_tag, 0, &sq->mbuf[x].dma_map);
948 bus_dmamap_destroy(sq->dma_tag, sq->mbuf[x].dma_map);
949 free(sq->mbuf, M_MLX5EN);
956 static const char *mlx5e_sq_stats_desc[] = {
957 MLX5E_SQ_STATS(MLX5E_STATS_DESC)
961 mlx5e_create_sq(struct mlx5e_channel *c,
963 struct mlx5e_sq_param *param,
966 struct mlx5e_priv *priv = c->priv;
967 struct mlx5_core_dev *mdev = priv->mdev;
970 void *sqc = param->sqc;
971 void *sqc_wq = MLX5_ADDR_OF(sqc, sqc, wq);
978 /* Create DMA descriptor TAG */
979 if ((err = -bus_dma_tag_create(
980 bus_get_dma_tag(mdev->pdev->dev.bsddev),
981 1, /* any alignment */
983 BUS_SPACE_MAXADDR, /* lowaddr */
984 BUS_SPACE_MAXADDR, /* highaddr */
985 NULL, NULL, /* filter, filterarg */
986 MLX5E_MAX_TX_PAYLOAD_SIZE, /* maxsize */
987 MLX5E_MAX_TX_MBUF_FRAGS, /* nsegments */
988 MLX5E_MAX_TX_MBUF_SIZE, /* maxsegsize */
990 NULL, NULL, /* lockfunc, lockfuncarg */
994 err = mlx5_alloc_map_uar(mdev, &sq->uar);
996 goto err_free_dma_tag;
998 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq,
1001 goto err_unmap_free_uar;
1003 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
1004 sq->bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
1006 err = mlx5e_alloc_sq_db(sq);
1008 goto err_sq_wq_destroy;
1010 sq->mkey_be = c->mkey_be;
1011 sq->ifp = priv->ifp;
1015 /* check if we should allocate a second packet buffer */
1016 if (priv->params_ethtool.tx_bufring_disable == 0) {
1017 sq->br = buf_ring_alloc(MLX5E_SQ_TX_QUEUE_SIZE, M_MLX5EN,
1018 M_WAITOK, &sq->lock);
1019 if (sq->br == NULL) {
1020 if_printf(c->ifp, "%s: Failed allocating sq drbr buffer\n",
1023 goto err_free_sq_db;
1026 sq->sq_tq = taskqueue_create_fast("mlx5e_que", M_WAITOK,
1027 taskqueue_thread_enqueue, &sq->sq_tq);
1028 if (sq->sq_tq == NULL) {
1029 if_printf(c->ifp, "%s: Failed allocating taskqueue\n",
1035 TASK_INIT(&sq->sq_task, 0, mlx5e_tx_que, sq);
1037 cpu_id = rss_getcpu(c->ix % rss_getnumbuckets());
1038 CPU_SETOF(cpu_id, &cpu_mask);
1039 taskqueue_start_threads_cpuset(&sq->sq_tq, 1, PI_NET, &cpu_mask,
1040 "%s TX SQ%d.%d CPU%d", c->ifp->if_xname, c->ix, tc, cpu_id);
1042 taskqueue_start_threads(&sq->sq_tq, 1, PI_NET,
1043 "%s TX SQ%d.%d", c->ifp->if_xname, c->ix, tc);
1046 snprintf(buffer, sizeof(buffer), "txstat%dtc%d", c->ix, tc);
1047 mlx5e_create_stats(&sq->stats.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
1048 buffer, mlx5e_sq_stats_desc, MLX5E_SQ_STATS_NUM,
1054 buf_ring_free(sq->br, M_MLX5EN);
1056 mlx5e_free_sq_db(sq);
1058 mlx5_wq_destroy(&sq->wq_ctrl);
1061 mlx5_unmap_free_uar(mdev, &sq->uar);
1064 bus_dma_tag_destroy(sq->dma_tag);
1070 mlx5e_destroy_sq(struct mlx5e_sq *sq)
1072 /* destroy all sysctl nodes */
1073 sysctl_ctx_free(&sq->stats.ctx);
1075 mlx5e_free_sq_db(sq);
1076 mlx5_wq_destroy(&sq->wq_ctrl);
1077 mlx5_unmap_free_uar(sq->priv->mdev, &sq->uar);
1078 if (sq->sq_tq != NULL) {
1079 taskqueue_drain(sq->sq_tq, &sq->sq_task);
1080 taskqueue_free(sq->sq_tq);
1083 buf_ring_free(sq->br, M_MLX5EN);
1087 mlx5e_enable_sq(struct mlx5e_sq *sq, struct mlx5e_sq_param *param,
1096 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
1097 sizeof(u64) * sq->wq_ctrl.buf.npages;
1098 in = mlx5_vzalloc(inlen);
1102 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1103 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1105 memcpy(sqc, param->sqc, sizeof(param->sqc));
1107 MLX5_SET(sqc, sqc, tis_num_0, tis_num);
1108 MLX5_SET(sqc, sqc, cqn, sq->cq.mcq.cqn);
1109 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1110 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1111 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1113 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1114 MLX5_SET(wq, wq, uar_page, sq->uar.index);
1115 MLX5_SET(wq, wq, log_wq_pg_sz, sq->wq_ctrl.buf.page_shift -
1117 MLX5_SET64(wq, wq, dbr_addr, sq->wq_ctrl.db.dma);
1119 mlx5_fill_page_array(&sq->wq_ctrl.buf,
1120 (__be64 *) MLX5_ADDR_OF(wq, wq, pas));
1122 err = mlx5_core_create_sq(sq->priv->mdev, in, inlen, &sq->sqn);
1130 mlx5e_modify_sq(struct mlx5e_sq *sq, int curr_state, int next_state)
1137 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
1138 in = mlx5_vzalloc(inlen);
1142 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
1144 MLX5_SET(modify_sq_in, in, sqn, sq->sqn);
1145 MLX5_SET(modify_sq_in, in, sq_state, curr_state);
1146 MLX5_SET(sqc, sqc, state, next_state);
1148 err = mlx5_core_modify_sq(sq->priv->mdev, in, inlen);
1156 mlx5e_disable_sq(struct mlx5e_sq *sq)
1159 mlx5_core_destroy_sq(sq->priv->mdev, sq->sqn);
1163 mlx5e_open_sq(struct mlx5e_channel *c,
1165 struct mlx5e_sq_param *param,
1166 struct mlx5e_sq *sq)
1170 err = mlx5e_create_sq(c, tc, param, sq);
1174 err = mlx5e_enable_sq(sq, param, c->priv->tisn[tc]);
1176 goto err_destroy_sq;
1178 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST, MLX5_SQC_STATE_RDY);
1180 goto err_disable_sq;
1182 atomic_store_rel_int(&sq->queue_state, MLX5E_SQ_READY);
1187 mlx5e_disable_sq(sq);
1189 mlx5e_destroy_sq(sq);
1195 mlx5e_sq_send_nops_locked(struct mlx5e_sq *sq, int can_sleep)
1197 /* fill up remainder with NOPs */
1198 while (sq->cev_counter != 0) {
1199 while (!mlx5e_sq_has_room_for(sq, 1)) {
1200 if (can_sleep != 0) {
1201 mtx_unlock(&sq->lock);
1203 mtx_lock(&sq->lock);
1208 /* send a single NOP */
1209 mlx5e_send_nop(sq, 1);
1213 /* Check if we need to write the doorbell */
1214 if (likely(sq->doorbell.d64 != 0)) {
1215 mlx5e_tx_notify_hw(sq, sq->doorbell.d32, 0);
1216 sq->doorbell.d64 = 0;
1221 mlx5e_sq_cev_timeout(void *arg)
1223 struct mlx5e_sq *sq = arg;
1225 mtx_assert(&sq->lock, MA_OWNED);
1227 /* check next state */
1228 switch (sq->cev_next_state) {
1229 case MLX5E_CEV_STATE_SEND_NOPS:
1230 /* fill TX ring with NOPs, if any */
1231 mlx5e_sq_send_nops_locked(sq, 0);
1233 /* check if completed */
1234 if (sq->cev_counter == 0) {
1235 sq->cev_next_state = MLX5E_CEV_STATE_INITIAL;
1240 /* send NOPs on next timeout */
1241 sq->cev_next_state = MLX5E_CEV_STATE_SEND_NOPS;
1246 callout_reset_curcpu(&sq->cev_callout, hz, mlx5e_sq_cev_timeout, sq);
1250 mlx5e_drain_sq(struct mlx5e_sq *sq)
1255 * Check if already stopped.
1257 * NOTE: The "stopped" variable is only written when both the
1258 * priv's configuration lock and the SQ's lock is locked. It
1259 * can therefore safely be read when only one of the two locks
1260 * is locked. This function is always called when the priv's
1261 * configuration lock is locked.
1263 if (sq->stopped != 0)
1266 mtx_lock(&sq->lock);
1268 /* don't put more packets into the SQ */
1271 /* teardown event factor timer, if any */
1272 sq->cev_next_state = MLX5E_CEV_STATE_HOLD_NOPS;
1273 callout_stop(&sq->cev_callout);
1275 /* send dummy NOPs in order to flush the transmit ring */
1276 mlx5e_sq_send_nops_locked(sq, 1);
1277 mtx_unlock(&sq->lock);
1279 /* make sure it is safe to free the callout */
1280 callout_drain(&sq->cev_callout);
1282 /* wait till SQ is empty or link is down */
1283 mtx_lock(&sq->lock);
1284 while (sq->cc != sq->pc &&
1285 (sq->priv->media_status_last & IFM_ACTIVE) != 0) {
1286 mtx_unlock(&sq->lock);
1288 sq->cq.mcq.comp(&sq->cq.mcq);
1289 mtx_lock(&sq->lock);
1291 mtx_unlock(&sq->lock);
1293 /* error out remaining requests */
1294 error = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY, MLX5_SQC_STATE_ERR);
1297 "mlx5e_modify_sq() from RDY to ERR failed: %d\n", error);
1300 /* wait till SQ is empty */
1301 mtx_lock(&sq->lock);
1302 while (sq->cc != sq->pc) {
1303 mtx_unlock(&sq->lock);
1305 sq->cq.mcq.comp(&sq->cq.mcq);
1306 mtx_lock(&sq->lock);
1308 mtx_unlock(&sq->lock);
1312 mlx5e_close_sq_wait(struct mlx5e_sq *sq)
1316 mlx5e_disable_sq(sq);
1317 mlx5e_destroy_sq(sq);
1321 mlx5e_create_cq(struct mlx5e_priv *priv,
1322 struct mlx5e_cq_param *param,
1323 struct mlx5e_cq *cq,
1324 mlx5e_cq_comp_t *comp,
1327 struct mlx5_core_dev *mdev = priv->mdev;
1328 struct mlx5_core_cq *mcq = &cq->mcq;
1334 param->wq.buf_numa_node = 0;
1335 param->wq.db_numa_node = 0;
1337 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1342 mlx5_vector2eqn(mdev, eq_ix, &eqn_not_used, &irqn);
1345 mcq->set_ci_db = cq->wq_ctrl.db.db;
1346 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1347 *mcq->set_ci_db = 0;
1349 mcq->vector = eq_ix;
1351 mcq->event = mlx5e_cq_error_event;
1353 mcq->uar = &priv->cq_uar;
1355 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
1356 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1367 mlx5e_destroy_cq(struct mlx5e_cq *cq)
1369 mlx5_wq_destroy(&cq->wq_ctrl);
1373 mlx5e_enable_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param, int eq_ix)
1375 struct mlx5_core_cq *mcq = &cq->mcq;
1383 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1384 sizeof(u64) * cq->wq_ctrl.buf.npages;
1385 in = mlx5_vzalloc(inlen);
1389 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1391 memcpy(cqc, param->cqc, sizeof(param->cqc));
1393 mlx5_fill_page_array(&cq->wq_ctrl.buf,
1394 (__be64 *) MLX5_ADDR_OF(create_cq_in, in, pas));
1396 mlx5_vector2eqn(cq->priv->mdev, eq_ix, &eqn, &irqn_not_used);
1398 MLX5_SET(cqc, cqc, c_eqn, eqn);
1399 MLX5_SET(cqc, cqc, uar_page, mcq->uar->index);
1400 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
1402 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
1404 err = mlx5_core_create_cq(cq->priv->mdev, mcq, in, inlen);
1417 mlx5e_disable_cq(struct mlx5e_cq *cq)
1420 mlx5_core_destroy_cq(cq->priv->mdev, &cq->mcq);
1424 mlx5e_open_cq(struct mlx5e_priv *priv,
1425 struct mlx5e_cq_param *param,
1426 struct mlx5e_cq *cq,
1427 mlx5e_cq_comp_t *comp,
1432 err = mlx5e_create_cq(priv, param, cq, comp, eq_ix);
1436 err = mlx5e_enable_cq(cq, param, eq_ix);
1438 goto err_destroy_cq;
1443 mlx5e_destroy_cq(cq);
1449 mlx5e_close_cq(struct mlx5e_cq *cq)
1451 mlx5e_disable_cq(cq);
1452 mlx5e_destroy_cq(cq);
1456 mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1457 struct mlx5e_channel_param *cparam)
1462 for (tc = 0; tc < c->num_tc; tc++) {
1463 /* open completion queue */
1464 err = mlx5e_open_cq(c->priv, &cparam->tx_cq, &c->sq[tc].cq,
1465 &mlx5e_tx_cq_comp, c->ix);
1467 goto err_close_tx_cqs;
1472 for (tc--; tc >= 0; tc--)
1473 mlx5e_close_cq(&c->sq[tc].cq);
1479 mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1483 for (tc = 0; tc < c->num_tc; tc++)
1484 mlx5e_close_cq(&c->sq[tc].cq);
1488 mlx5e_open_sqs(struct mlx5e_channel *c,
1489 struct mlx5e_channel_param *cparam)
1494 for (tc = 0; tc < c->num_tc; tc++) {
1495 err = mlx5e_open_sq(c, tc, &cparam->sq, &c->sq[tc]);
1503 for (tc--; tc >= 0; tc--)
1504 mlx5e_close_sq_wait(&c->sq[tc]);
1510 mlx5e_close_sqs_wait(struct mlx5e_channel *c)
1514 for (tc = 0; tc < c->num_tc; tc++)
1515 mlx5e_close_sq_wait(&c->sq[tc]);
1519 mlx5e_chan_mtx_init(struct mlx5e_channel *c)
1523 mtx_init(&c->rq.mtx, "mlx5rx", MTX_NETWORK_LOCK, MTX_DEF);
1525 callout_init_mtx(&c->rq.watchdog, &c->rq.mtx, 0);
1527 for (tc = 0; tc < c->num_tc; tc++) {
1528 struct mlx5e_sq *sq = c->sq + tc;
1530 mtx_init(&sq->lock, "mlx5tx",
1531 MTX_NETWORK_LOCK " TX", MTX_DEF);
1532 mtx_init(&sq->comp_lock, "mlx5comp",
1533 MTX_NETWORK_LOCK " TX", MTX_DEF);
1535 callout_init_mtx(&sq->cev_callout, &sq->lock, 0);
1537 sq->cev_factor = c->priv->params_ethtool.tx_completion_fact;
1539 /* ensure the TX completion event factor is not zero */
1540 if (sq->cev_factor == 0)
1546 mlx5e_chan_mtx_destroy(struct mlx5e_channel *c)
1550 mtx_destroy(&c->rq.mtx);
1552 for (tc = 0; tc < c->num_tc; tc++) {
1553 mtx_destroy(&c->sq[tc].lock);
1554 mtx_destroy(&c->sq[tc].comp_lock);
1559 mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1560 struct mlx5e_channel_param *cparam,
1561 struct mlx5e_channel *volatile *cp)
1563 struct mlx5e_channel *c;
1566 c = malloc(sizeof(*c), M_MLX5EN, M_WAITOK | M_ZERO);
1571 c->mkey_be = cpu_to_be32(priv->mr.key);
1572 c->num_tc = priv->num_tc;
1575 mlx5e_chan_mtx_init(c);
1577 /* open transmit completion queue */
1578 err = mlx5e_open_tx_cqs(c, cparam);
1582 /* open receive completion queue */
1583 err = mlx5e_open_cq(c->priv, &cparam->rx_cq, &c->rq.cq,
1584 &mlx5e_rx_cq_comp, c->ix);
1586 goto err_close_tx_cqs;
1588 err = mlx5e_open_sqs(c, cparam);
1590 goto err_close_rx_cq;
1592 err = mlx5e_open_rq(c, &cparam->rq, &c->rq);
1596 /* store channel pointer */
1599 /* poll receive queue initially */
1600 c->rq.cq.mcq.comp(&c->rq.cq.mcq);
1605 mlx5e_close_sqs_wait(c);
1608 mlx5e_close_cq(&c->rq.cq);
1611 mlx5e_close_tx_cqs(c);
1614 /* destroy mutexes */
1615 mlx5e_chan_mtx_destroy(c);
1621 mlx5e_close_channel(struct mlx5e_channel *volatile *pp)
1623 struct mlx5e_channel *c = *pp;
1625 /* check if channel is already closed */
1628 mlx5e_close_rq(&c->rq);
1632 mlx5e_close_channel_wait(struct mlx5e_channel *volatile *pp)
1634 struct mlx5e_channel *c = *pp;
1636 /* check if channel is already closed */
1639 /* ensure channel pointer is no longer used */
1642 mlx5e_close_rq_wait(&c->rq);
1643 mlx5e_close_sqs_wait(c);
1644 mlx5e_close_cq(&c->rq.cq);
1645 mlx5e_close_tx_cqs(c);
1646 /* destroy mutexes */
1647 mlx5e_chan_mtx_destroy(c);
1652 mlx5e_build_rq_param(struct mlx5e_priv *priv,
1653 struct mlx5e_rq_param *param)
1655 void *rqc = param->rqc;
1656 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1658 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1659 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1660 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1661 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_rq_size);
1662 MLX5_SET(wq, wq, pd, priv->pdn);
1664 param->wq.buf_numa_node = 0;
1665 param->wq.db_numa_node = 0;
1666 param->wq.linear = 1;
1670 mlx5e_build_sq_param(struct mlx5e_priv *priv,
1671 struct mlx5e_sq_param *param)
1673 void *sqc = param->sqc;
1674 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1676 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_sq_size);
1677 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1678 MLX5_SET(wq, wq, pd, priv->pdn);
1680 param->wq.buf_numa_node = 0;
1681 param->wq.db_numa_node = 0;
1682 param->wq.linear = 1;
1686 mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1687 struct mlx5e_cq_param *param)
1689 void *cqc = param->cqc;
1691 MLX5_SET(cqc, cqc, uar_page, priv->cq_uar.index);
1695 mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
1696 struct mlx5e_cq_param *param)
1698 void *cqc = param->cqc;
1702 * TODO The sysctl to control on/off is a bool value for now, which means
1703 * we only support CSUM, once HASH is implemnted we'll need to address that.
1705 if (priv->params.cqe_zipping_en) {
1706 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
1707 MLX5_SET(cqc, cqc, cqe_compression_en, 1);
1710 MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_rq_size);
1711 MLX5_SET(cqc, cqc, cq_period, priv->params.rx_cq_moderation_usec);
1712 MLX5_SET(cqc, cqc, cq_max_count, priv->params.rx_cq_moderation_pkts);
1714 switch (priv->params.rx_cq_moderation_mode) {
1716 MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
1719 if (MLX5_CAP_GEN(priv->mdev, cq_period_start_from_cqe))
1720 MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
1722 MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
1726 mlx5e_build_common_cq_param(priv, param);
1730 mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
1731 struct mlx5e_cq_param *param)
1733 void *cqc = param->cqc;
1735 MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_sq_size);
1736 MLX5_SET(cqc, cqc, cq_period, priv->params.tx_cq_moderation_usec);
1737 MLX5_SET(cqc, cqc, cq_max_count, priv->params.tx_cq_moderation_pkts);
1739 switch (priv->params.tx_cq_moderation_mode) {
1741 MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
1744 if (MLX5_CAP_GEN(priv->mdev, cq_period_start_from_cqe))
1745 MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
1747 MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
1751 mlx5e_build_common_cq_param(priv, param);
1755 mlx5e_build_channel_param(struct mlx5e_priv *priv,
1756 struct mlx5e_channel_param *cparam)
1758 memset(cparam, 0, sizeof(*cparam));
1760 mlx5e_build_rq_param(priv, &cparam->rq);
1761 mlx5e_build_sq_param(priv, &cparam->sq);
1762 mlx5e_build_rx_cq_param(priv, &cparam->rx_cq);
1763 mlx5e_build_tx_cq_param(priv, &cparam->tx_cq);
1767 mlx5e_open_channels(struct mlx5e_priv *priv)
1769 struct mlx5e_channel_param cparam;
1775 priv->channel = malloc(priv->params.num_channels *
1776 sizeof(struct mlx5e_channel *), M_MLX5EN, M_WAITOK | M_ZERO);
1778 mlx5e_build_channel_param(priv, &cparam);
1779 for (i = 0; i < priv->params.num_channels; i++) {
1780 err = mlx5e_open_channel(priv, i, &cparam, &priv->channel[i]);
1782 goto err_close_channels;
1785 for (j = 0; j < priv->params.num_channels; j++) {
1786 err = mlx5e_wait_for_min_rx_wqes(&priv->channel[j]->rq);
1788 goto err_close_channels;
1794 for (i--; i >= 0; i--) {
1795 mlx5e_close_channel(&priv->channel[i]);
1796 mlx5e_close_channel_wait(&priv->channel[i]);
1799 /* remove "volatile" attribute from "channel" pointer */
1800 ptr = __DECONST(void *, priv->channel);
1801 priv->channel = NULL;
1803 free(ptr, M_MLX5EN);
1809 mlx5e_close_channels(struct mlx5e_priv *priv)
1814 if (priv->channel == NULL)
1817 for (i = 0; i < priv->params.num_channels; i++)
1818 mlx5e_close_channel(&priv->channel[i]);
1819 for (i = 0; i < priv->params.num_channels; i++)
1820 mlx5e_close_channel_wait(&priv->channel[i]);
1822 /* remove "volatile" attribute from "channel" pointer */
1823 ptr = __DECONST(void *, priv->channel);
1824 priv->channel = NULL;
1826 free(ptr, M_MLX5EN);
1830 mlx5e_refresh_sq_params(struct mlx5e_priv *priv, struct mlx5e_sq *sq)
1833 if (MLX5_CAP_GEN(priv->mdev, cq_period_mode_modify)) {
1836 switch (priv->params.tx_cq_moderation_mode) {
1838 cq_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1841 cq_mode = MLX5_CQ_PERIOD_MODE_START_FROM_CQE;
1845 return (mlx5_core_modify_cq_moderation_mode(priv->mdev, &sq->cq.mcq,
1846 priv->params.tx_cq_moderation_usec,
1847 priv->params.tx_cq_moderation_pkts,
1851 return (mlx5_core_modify_cq_moderation(priv->mdev, &sq->cq.mcq,
1852 priv->params.tx_cq_moderation_usec,
1853 priv->params.tx_cq_moderation_pkts));
1857 mlx5e_refresh_rq_params(struct mlx5e_priv *priv, struct mlx5e_rq *rq)
1860 if (MLX5_CAP_GEN(priv->mdev, cq_period_mode_modify)) {
1864 switch (priv->params.rx_cq_moderation_mode) {
1866 cq_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1869 cq_mode = MLX5_CQ_PERIOD_MODE_START_FROM_CQE;
1873 retval = mlx5_core_modify_cq_moderation_mode(priv->mdev, &rq->cq.mcq,
1874 priv->params.rx_cq_moderation_usec,
1875 priv->params.rx_cq_moderation_pkts,
1881 return (mlx5_core_modify_cq_moderation(priv->mdev, &rq->cq.mcq,
1882 priv->params.rx_cq_moderation_usec,
1883 priv->params.rx_cq_moderation_pkts));
1887 mlx5e_refresh_channel_params_sub(struct mlx5e_priv *priv, struct mlx5e_channel *c)
1895 err = mlx5e_refresh_rq_params(priv, &c->rq);
1899 for (i = 0; i != c->num_tc; i++) {
1900 err = mlx5e_refresh_sq_params(priv, &c->sq[i]);
1909 mlx5e_refresh_channel_params(struct mlx5e_priv *priv)
1913 if (priv->channel == NULL)
1916 for (i = 0; i < priv->params.num_channels; i++) {
1919 err = mlx5e_refresh_channel_params_sub(priv, priv->channel[i]);
1927 mlx5e_open_tis(struct mlx5e_priv *priv, int tc)
1929 struct mlx5_core_dev *mdev = priv->mdev;
1930 u32 in[MLX5_ST_SZ_DW(create_tis_in)];
1931 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1933 memset(in, 0, sizeof(in));
1935 MLX5_SET(tisc, tisc, prio, tc);
1936 MLX5_SET(tisc, tisc, transport_domain, priv->tdn);
1938 return (mlx5_core_create_tis(mdev, in, sizeof(in), &priv->tisn[tc]));
1942 mlx5e_close_tis(struct mlx5e_priv *priv, int tc)
1944 mlx5_core_destroy_tis(priv->mdev, priv->tisn[tc]);
1948 mlx5e_open_tises(struct mlx5e_priv *priv)
1950 int num_tc = priv->num_tc;
1954 for (tc = 0; tc < num_tc; tc++) {
1955 err = mlx5e_open_tis(priv, tc);
1957 goto err_close_tises;
1963 for (tc--; tc >= 0; tc--)
1964 mlx5e_close_tis(priv, tc);
1970 mlx5e_close_tises(struct mlx5e_priv *priv)
1972 int num_tc = priv->num_tc;
1975 for (tc = 0; tc < num_tc; tc++)
1976 mlx5e_close_tis(priv, tc);
1980 mlx5e_open_rqt(struct mlx5e_priv *priv)
1982 struct mlx5_core_dev *mdev = priv->mdev;
1984 u32 out[MLX5_ST_SZ_DW(create_rqt_out)];
1991 sz = 1 << priv->params.rx_hash_log_tbl_sz;
1993 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
1994 in = mlx5_vzalloc(inlen);
1997 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
1999 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2000 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
2002 for (i = 0; i < sz; i++) {
2005 ix = rss_get_indirection_to_bucket(i);
2009 /* ensure we don't overflow */
2010 ix %= priv->params.num_channels;
2011 MLX5_SET(rqtc, rqtc, rq_num[i], priv->channel[ix]->rq.rqn);
2014 MLX5_SET(create_rqt_in, in, opcode, MLX5_CMD_OP_CREATE_RQT);
2016 memset(out, 0, sizeof(out));
2017 err = mlx5_cmd_exec_check_status(mdev, in, inlen, out, sizeof(out));
2019 priv->rqtn = MLX5_GET(create_rqt_out, out, rqtn);
2027 mlx5e_close_rqt(struct mlx5e_priv *priv)
2029 u32 in[MLX5_ST_SZ_DW(destroy_rqt_in)];
2030 u32 out[MLX5_ST_SZ_DW(destroy_rqt_out)];
2032 memset(in, 0, sizeof(in));
2034 MLX5_SET(destroy_rqt_in, in, opcode, MLX5_CMD_OP_DESTROY_RQT);
2035 MLX5_SET(destroy_rqt_in, in, rqtn, priv->rqtn);
2037 mlx5_cmd_exec_check_status(priv->mdev, in, sizeof(in), out,
2042 mlx5e_build_tir_ctx(struct mlx5e_priv *priv, u32 * tirc, int tt)
2044 void *hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2047 MLX5_SET(tirc, tirc, transport_domain, priv->tdn);
2049 #define ROUGH_MAX_L2_L3_HDR_SZ 256
2051 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
2052 MLX5_HASH_FIELD_SEL_DST_IP)
2054 #define MLX5_HASH_ALL (MLX5_HASH_FIELD_SEL_SRC_IP |\
2055 MLX5_HASH_FIELD_SEL_DST_IP |\
2056 MLX5_HASH_FIELD_SEL_L4_SPORT |\
2057 MLX5_HASH_FIELD_SEL_L4_DPORT)
2059 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
2060 MLX5_HASH_FIELD_SEL_DST_IP |\
2061 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
2063 if (priv->params.hw_lro_en) {
2064 MLX5_SET(tirc, tirc, lro_enable_mask,
2065 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
2066 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
2067 MLX5_SET(tirc, tirc, lro_max_msg_sz,
2068 (priv->params.lro_wqe_sz -
2069 ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
2070 /* TODO: add the option to choose timer value dynamically */
2071 MLX5_SET(tirc, tirc, lro_timeout_period_usecs,
2072 MLX5_CAP_ETH(priv->mdev,
2073 lro_timer_supported_periods[2]));
2076 /* setup parameters for hashing TIR type, if any */
2079 MLX5_SET(tirc, tirc, disp_type,
2080 MLX5_TIRC_DISP_TYPE_DIRECT);
2081 MLX5_SET(tirc, tirc, inline_rqn,
2082 priv->channel[0]->rq.rqn);
2085 MLX5_SET(tirc, tirc, disp_type,
2086 MLX5_TIRC_DISP_TYPE_INDIRECT);
2087 MLX5_SET(tirc, tirc, indirect_table,
2089 MLX5_SET(tirc, tirc, rx_hash_fn,
2090 MLX5_TIRC_RX_HASH_FN_HASH_TOEPLITZ);
2091 hkey = (__be32 *) MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
2094 * The FreeBSD RSS implementation does currently not
2095 * support symmetric Toeplitz hashes:
2097 MLX5_SET(tirc, tirc, rx_hash_symmetric, 0);
2098 rss_getkey((uint8_t *)hkey);
2100 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
2101 hkey[0] = cpu_to_be32(0xD181C62C);
2102 hkey[1] = cpu_to_be32(0xF7F4DB5B);
2103 hkey[2] = cpu_to_be32(0x1983A2FC);
2104 hkey[3] = cpu_to_be32(0x943E1ADB);
2105 hkey[4] = cpu_to_be32(0xD9389E6B);
2106 hkey[5] = cpu_to_be32(0xD1039C2C);
2107 hkey[6] = cpu_to_be32(0xA74499AD);
2108 hkey[7] = cpu_to_be32(0x593D56D9);
2109 hkey[8] = cpu_to_be32(0xF3253C06);
2110 hkey[9] = cpu_to_be32(0x2ADC1FFC);
2116 case MLX5E_TT_IPV4_TCP:
2117 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2118 MLX5_L3_PROT_TYPE_IPV4);
2119 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2120 MLX5_L4_PROT_TYPE_TCP);
2122 if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4)) {
2123 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2127 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2131 case MLX5E_TT_IPV6_TCP:
2132 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2133 MLX5_L3_PROT_TYPE_IPV6);
2134 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2135 MLX5_L4_PROT_TYPE_TCP);
2137 if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6)) {
2138 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2142 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2146 case MLX5E_TT_IPV4_UDP:
2147 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2148 MLX5_L3_PROT_TYPE_IPV4);
2149 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2150 MLX5_L4_PROT_TYPE_UDP);
2152 if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4)) {
2153 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2157 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2161 case MLX5E_TT_IPV6_UDP:
2162 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2163 MLX5_L3_PROT_TYPE_IPV6);
2164 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2165 MLX5_L4_PROT_TYPE_UDP);
2167 if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6)) {
2168 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2172 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2176 case MLX5E_TT_IPV4_IPSEC_AH:
2177 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2178 MLX5_L3_PROT_TYPE_IPV4);
2179 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2180 MLX5_HASH_IP_IPSEC_SPI);
2183 case MLX5E_TT_IPV6_IPSEC_AH:
2184 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2185 MLX5_L3_PROT_TYPE_IPV6);
2186 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2187 MLX5_HASH_IP_IPSEC_SPI);
2190 case MLX5E_TT_IPV4_IPSEC_ESP:
2191 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2192 MLX5_L3_PROT_TYPE_IPV4);
2193 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2194 MLX5_HASH_IP_IPSEC_SPI);
2197 case MLX5E_TT_IPV6_IPSEC_ESP:
2198 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2199 MLX5_L3_PROT_TYPE_IPV6);
2200 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2201 MLX5_HASH_IP_IPSEC_SPI);
2205 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2206 MLX5_L3_PROT_TYPE_IPV4);
2207 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2212 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2213 MLX5_L3_PROT_TYPE_IPV6);
2214 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2224 mlx5e_open_tir(struct mlx5e_priv *priv, int tt)
2226 struct mlx5_core_dev *mdev = priv->mdev;
2232 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2233 in = mlx5_vzalloc(inlen);
2236 tirc = MLX5_ADDR_OF(create_tir_in, in, tir_context);
2238 mlx5e_build_tir_ctx(priv, tirc, tt);
2240 err = mlx5_core_create_tir(mdev, in, inlen, &priv->tirn[tt]);
2248 mlx5e_close_tir(struct mlx5e_priv *priv, int tt)
2250 mlx5_core_destroy_tir(priv->mdev, priv->tirn[tt]);
2254 mlx5e_open_tirs(struct mlx5e_priv *priv)
2259 for (i = 0; i < MLX5E_NUM_TT; i++) {
2260 err = mlx5e_open_tir(priv, i);
2262 goto err_close_tirs;
2268 for (i--; i >= 0; i--)
2269 mlx5e_close_tir(priv, i);
2275 mlx5e_close_tirs(struct mlx5e_priv *priv)
2279 for (i = 0; i < MLX5E_NUM_TT; i++)
2280 mlx5e_close_tir(priv, i);
2284 * SW MTU does not include headers,
2285 * HW MTU includes all headers and checksums.
2288 mlx5e_set_dev_port_mtu(struct ifnet *ifp, int sw_mtu)
2290 struct mlx5e_priv *priv = ifp->if_softc;
2291 struct mlx5_core_dev *mdev = priv->mdev;
2295 err = mlx5_set_port_mtu(mdev, MLX5E_SW2HW_MTU(sw_mtu));
2297 if_printf(ifp, "%s: mlx5_set_port_mtu failed setting %d, err=%d\n",
2298 __func__, sw_mtu, err);
2301 err = mlx5_query_port_oper_mtu(mdev, &hw_mtu);
2303 if_printf(ifp, "Query port MTU, after setting new "
2304 "MTU value, failed\n");
2305 } else if (MLX5E_HW2SW_MTU(hw_mtu) < sw_mtu) {
2307 if_printf(ifp, "Port MTU %d is smaller than "
2308 "ifp mtu %d\n", hw_mtu, sw_mtu);
2309 } else if (MLX5E_HW2SW_MTU(hw_mtu) > sw_mtu) {
2311 if_printf(ifp, "Port MTU %d is bigger than "
2312 "ifp mtu %d\n", hw_mtu, sw_mtu);
2314 ifp->if_mtu = sw_mtu;
2319 mlx5e_open_locked(struct ifnet *ifp)
2321 struct mlx5e_priv *priv = ifp->if_softc;
2325 /* check if already opened */
2326 if (test_bit(MLX5E_STATE_OPENED, &priv->state) != 0)
2330 if (rss_getnumbuckets() > priv->params.num_channels) {
2331 if_printf(ifp, "NOTE: There are more RSS buckets(%u) than "
2332 "channels(%u) available\n", rss_getnumbuckets(),
2333 priv->params.num_channels);
2336 err = mlx5e_open_tises(priv);
2338 if_printf(ifp, "%s: mlx5e_open_tises failed, %d\n",
2342 err = mlx5_vport_alloc_q_counter(priv->mdev,
2343 MLX5_INTERFACE_PROTOCOL_ETH, &set_id);
2345 if_printf(priv->ifp,
2346 "%s: mlx5_vport_alloc_q_counter failed: %d\n",
2348 goto err_close_tises;
2350 /* store counter set ID */
2351 priv->counter_set_id = set_id;
2353 err = mlx5e_open_channels(priv);
2355 if_printf(ifp, "%s: mlx5e_open_channels failed, %d\n",
2357 goto err_dalloc_q_counter;
2359 err = mlx5e_open_rqt(priv);
2361 if_printf(ifp, "%s: mlx5e_open_rqt failed, %d\n",
2363 goto err_close_channels;
2365 err = mlx5e_open_tirs(priv);
2367 if_printf(ifp, "%s: mlx5e_open_tir failed, %d\n",
2369 goto err_close_rqls;
2371 err = mlx5e_open_flow_table(priv);
2373 if_printf(ifp, "%s: mlx5e_open_flow_table failed, %d\n",
2375 goto err_close_tirs;
2377 err = mlx5e_add_all_vlan_rules(priv);
2379 if_printf(ifp, "%s: mlx5e_add_all_vlan_rules failed, %d\n",
2381 goto err_close_flow_table;
2383 set_bit(MLX5E_STATE_OPENED, &priv->state);
2385 mlx5e_update_carrier(priv);
2386 mlx5e_set_rx_mode_core(priv);
2390 err_close_flow_table:
2391 mlx5e_close_flow_table(priv);
2394 mlx5e_close_tirs(priv);
2397 mlx5e_close_rqt(priv);
2400 mlx5e_close_channels(priv);
2402 err_dalloc_q_counter:
2403 mlx5_vport_dealloc_q_counter(priv->mdev,
2404 MLX5_INTERFACE_PROTOCOL_ETH, priv->counter_set_id);
2407 mlx5e_close_tises(priv);
2413 mlx5e_open(void *arg)
2415 struct mlx5e_priv *priv = arg;
2418 if (mlx5_set_port_status(priv->mdev, MLX5_PORT_UP))
2419 if_printf(priv->ifp,
2420 "%s: Setting port status to up failed\n",
2423 mlx5e_open_locked(priv->ifp);
2424 priv->ifp->if_drv_flags |= IFF_DRV_RUNNING;
2429 mlx5e_close_locked(struct ifnet *ifp)
2431 struct mlx5e_priv *priv = ifp->if_softc;
2433 /* check if already closed */
2434 if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
2437 clear_bit(MLX5E_STATE_OPENED, &priv->state);
2439 mlx5e_set_rx_mode_core(priv);
2440 mlx5e_del_all_vlan_rules(priv);
2441 if_link_state_change(priv->ifp, LINK_STATE_DOWN);
2442 mlx5e_close_flow_table(priv);
2443 mlx5e_close_tirs(priv);
2444 mlx5e_close_rqt(priv);
2445 mlx5e_close_channels(priv);
2446 mlx5_vport_dealloc_q_counter(priv->mdev,
2447 MLX5_INTERFACE_PROTOCOL_ETH, priv->counter_set_id);
2448 mlx5e_close_tises(priv);
2453 #if (__FreeBSD_version >= 1100000)
2455 mlx5e_get_counter(struct ifnet *ifp, ift_counter cnt)
2457 struct mlx5e_priv *priv = ifp->if_softc;
2460 /* PRIV_LOCK(priv); XXX not allowed */
2462 case IFCOUNTER_IPACKETS:
2463 retval = priv->stats.vport.rx_packets;
2465 case IFCOUNTER_IERRORS:
2466 retval = priv->stats.vport.rx_error_packets +
2467 priv->stats.pport.alignment_err +
2468 priv->stats.pport.check_seq_err +
2469 priv->stats.pport.crc_align_errors +
2470 priv->stats.pport.drop_events +
2471 priv->stats.pport.in_range_len_errors +
2472 priv->stats.pport.jabbers +
2473 priv->stats.pport.out_of_range_len +
2474 priv->stats.pport.oversize_pkts +
2475 priv->stats.pport.symbol_err +
2476 priv->stats.pport.too_long_errors +
2477 priv->stats.pport.undersize_pkts +
2478 priv->stats.pport.unsupported_op_rx;
2480 case IFCOUNTER_IQDROPS:
2481 retval = priv->stats.vport.rx_out_of_buffer;
2483 case IFCOUNTER_OPACKETS:
2484 retval = priv->stats.vport.tx_packets;
2486 case IFCOUNTER_OERRORS:
2487 retval = priv->stats.vport.tx_error_packets;
2489 case IFCOUNTER_IBYTES:
2490 retval = priv->stats.vport.rx_bytes;
2492 case IFCOUNTER_OBYTES:
2493 retval = priv->stats.vport.tx_bytes;
2495 case IFCOUNTER_IMCASTS:
2496 retval = priv->stats.vport.rx_multicast_packets;
2498 case IFCOUNTER_OMCASTS:
2499 retval = priv->stats.vport.tx_multicast_packets;
2501 case IFCOUNTER_OQDROPS:
2502 retval = priv->stats.vport.tx_queue_dropped;
2504 case IFCOUNTER_COLLISIONS:
2505 retval = priv->stats.pport.collisions;
2508 retval = if_get_counter_default(ifp, cnt);
2511 /* PRIV_UNLOCK(priv); XXX not allowed */
2517 mlx5e_set_rx_mode(struct ifnet *ifp)
2519 struct mlx5e_priv *priv = ifp->if_softc;
2521 schedule_work(&priv->set_rx_mode_work);
2525 mlx5e_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2527 struct mlx5e_priv *priv;
2529 struct ifi2creq i2c;
2537 priv = ifp->if_softc;
2539 /* check if detaching */
2540 if (priv == NULL || priv->gone != 0)
2545 ifr = (struct ifreq *)data;
2548 mlx5_query_port_max_mtu(priv->mdev, &max_mtu);
2550 if (ifr->ifr_mtu >= MLX5E_MTU_MIN &&
2551 ifr->ifr_mtu <= MIN(MLX5E_MTU_MAX, max_mtu)) {
2554 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2556 mlx5e_close_locked(ifp);
2559 mlx5e_set_dev_port_mtu(ifp, ifr->ifr_mtu);
2562 mlx5e_open_locked(ifp);
2565 if_printf(ifp, "Invalid MTU value. Min val: %d, Max val: %d\n",
2566 MLX5E_MTU_MIN, MIN(MLX5E_MTU_MAX, max_mtu));
2571 if ((ifp->if_flags & IFF_UP) &&
2572 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2573 mlx5e_set_rx_mode(ifp);
2577 if (ifp->if_flags & IFF_UP) {
2578 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2579 if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
2580 mlx5e_open_locked(ifp);
2581 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2582 mlx5_set_port_status(priv->mdev, MLX5_PORT_UP);
2585 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2586 mlx5_set_port_status(priv->mdev,
2588 if (test_bit(MLX5E_STATE_OPENED, &priv->state) != 0)
2589 mlx5e_close_locked(ifp);
2590 mlx5e_update_carrier(priv);
2591 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2598 mlx5e_set_rx_mode(ifp);
2603 ifr = (struct ifreq *)data;
2604 error = ifmedia_ioctl(ifp, ifr, &priv->media, command);
2607 ifr = (struct ifreq *)data;
2609 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2611 if (mask & IFCAP_TXCSUM) {
2612 ifp->if_capenable ^= IFCAP_TXCSUM;
2613 ifp->if_hwassist ^= (CSUM_TCP | CSUM_UDP | CSUM_IP);
2615 if (IFCAP_TSO4 & ifp->if_capenable &&
2616 !(IFCAP_TXCSUM & ifp->if_capenable)) {
2617 ifp->if_capenable &= ~IFCAP_TSO4;
2618 ifp->if_hwassist &= ~CSUM_IP_TSO;
2620 "tso4 disabled due to -txcsum.\n");
2623 if (mask & IFCAP_TXCSUM_IPV6) {
2624 ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
2625 ifp->if_hwassist ^= (CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2627 if (IFCAP_TSO6 & ifp->if_capenable &&
2628 !(IFCAP_TXCSUM_IPV6 & ifp->if_capenable)) {
2629 ifp->if_capenable &= ~IFCAP_TSO6;
2630 ifp->if_hwassist &= ~CSUM_IP6_TSO;
2632 "tso6 disabled due to -txcsum6.\n");
2635 if (mask & IFCAP_RXCSUM)
2636 ifp->if_capenable ^= IFCAP_RXCSUM;
2637 if (mask & IFCAP_RXCSUM_IPV6)
2638 ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
2639 if (mask & IFCAP_TSO4) {
2640 if (!(IFCAP_TSO4 & ifp->if_capenable) &&
2641 !(IFCAP_TXCSUM & ifp->if_capenable)) {
2642 if_printf(ifp, "enable txcsum first.\n");
2646 ifp->if_capenable ^= IFCAP_TSO4;
2647 ifp->if_hwassist ^= CSUM_IP_TSO;
2649 if (mask & IFCAP_TSO6) {
2650 if (!(IFCAP_TSO6 & ifp->if_capenable) &&
2651 !(IFCAP_TXCSUM_IPV6 & ifp->if_capenable)) {
2652 if_printf(ifp, "enable txcsum6 first.\n");
2656 ifp->if_capenable ^= IFCAP_TSO6;
2657 ifp->if_hwassist ^= CSUM_IP6_TSO;
2659 if (mask & IFCAP_VLAN_HWFILTER) {
2660 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2661 mlx5e_disable_vlan_filter(priv);
2663 mlx5e_enable_vlan_filter(priv);
2665 ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
2667 if (mask & IFCAP_VLAN_HWTAGGING)
2668 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2669 if (mask & IFCAP_WOL_MAGIC)
2670 ifp->if_capenable ^= IFCAP_WOL_MAGIC;
2672 VLAN_CAPABILITIES(ifp);
2673 /* turn off LRO means also turn of HW LRO - if it's on */
2674 if (mask & IFCAP_LRO) {
2675 int was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2676 bool need_restart = false;
2678 ifp->if_capenable ^= IFCAP_LRO;
2679 if (!(ifp->if_capenable & IFCAP_LRO)) {
2680 if (priv->params.hw_lro_en) {
2681 priv->params.hw_lro_en = false;
2682 need_restart = true;
2683 /* Not sure this is the correct way */
2684 priv->params_ethtool.hw_lro = priv->params.hw_lro_en;
2687 if (was_opened && need_restart) {
2688 mlx5e_close_locked(ifp);
2689 mlx5e_open_locked(ifp);
2697 ifr = (struct ifreq *)data;
2700 * Copy from the user-space address ifr_data to the
2701 * kernel-space address i2c
2703 error = copyin(ifr->ifr_data, &i2c, sizeof(i2c));
2707 if (i2c.len > sizeof(i2c.data)) {
2713 /* Get module_num which is required for the query_eeprom */
2714 error = mlx5_query_module_num(priv->mdev, &module_num);
2716 if_printf(ifp, "Query module num failed, eeprom "
2717 "reading is not supported\n");
2721 /* Check if module is present before doing an access */
2722 if (mlx5_query_module_status(priv->mdev, module_num) !=
2723 MLX5_MODULE_STATUS_PLUGGED) {
2728 * Currently 0XA0 and 0xA2 are the only addresses permitted.
2729 * The internal conversion is as follows:
2731 if (i2c.dev_addr == 0xA0)
2732 read_addr = MLX5E_I2C_ADDR_LOW;
2733 else if (i2c.dev_addr == 0xA2)
2734 read_addr = MLX5E_I2C_ADDR_HIGH;
2736 if_printf(ifp, "Query eeprom failed, "
2737 "Invalid Address: %X\n", i2c.dev_addr);
2741 error = mlx5_query_eeprom(priv->mdev,
2742 read_addr, MLX5E_EEPROM_LOW_PAGE,
2743 (uint32_t)i2c.offset, (uint32_t)i2c.len, module_num,
2744 (uint32_t *)i2c.data, &size_read);
2746 if_printf(ifp, "Query eeprom failed, eeprom "
2747 "reading is not supported\n");
2752 if (i2c.len > MLX5_EEPROM_MAX_BYTES) {
2753 error = mlx5_query_eeprom(priv->mdev,
2754 read_addr, MLX5E_EEPROM_LOW_PAGE,
2755 (uint32_t)(i2c.offset + size_read),
2756 (uint32_t)(i2c.len - size_read), module_num,
2757 (uint32_t *)(i2c.data + size_read), &size_read);
2760 if_printf(ifp, "Query eeprom failed, eeprom "
2761 "reading is not supported\n");
2766 error = copyout(&i2c, ifr->ifr_data, sizeof(i2c));
2772 error = ether_ioctl(ifp, command, data);
2779 mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
2782 * TODO: uncoment once FW really sets all these bits if
2783 * (!mdev->caps.eth.rss_ind_tbl_cap || !mdev->caps.eth.csum_cap ||
2784 * !mdev->caps.eth.max_lso_cap || !mdev->caps.eth.vlan_cap ||
2785 * !(mdev->caps.gen.flags & MLX5_DEV_CAP_FLAG_SCQE_BRK_MOD)) return
2789 /* TODO: add more must-to-have features */
2791 if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
2798 mlx5e_build_ifp_priv(struct mlx5_core_dev *mdev,
2799 struct mlx5e_priv *priv,
2800 int num_comp_vectors)
2803 * TODO: Consider link speed for setting "log_sq_size",
2804 * "log_rq_size" and "cq_moderation_xxx":
2806 priv->params.log_sq_size =
2807 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
2808 priv->params.log_rq_size =
2809 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
2810 priv->params.rx_cq_moderation_usec =
2811 MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
2812 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE :
2813 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
2814 priv->params.rx_cq_moderation_mode =
2815 MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ? 1 : 0;
2816 priv->params.rx_cq_moderation_pkts =
2817 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
2818 priv->params.tx_cq_moderation_usec =
2819 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
2820 priv->params.tx_cq_moderation_pkts =
2821 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
2822 priv->params.min_rx_wqes =
2823 MLX5E_PARAMS_DEFAULT_MIN_RX_WQES;
2824 priv->params.rx_hash_log_tbl_sz =
2825 (order_base_2(num_comp_vectors) >
2826 MLX5E_PARAMS_DEFAULT_RX_HASH_LOG_TBL_SZ) ?
2827 order_base_2(num_comp_vectors) :
2828 MLX5E_PARAMS_DEFAULT_RX_HASH_LOG_TBL_SZ;
2829 priv->params.num_tc = 1;
2830 priv->params.default_vlan_prio = 0;
2831 priv->counter_set_id = -1;
2834 * hw lro is currently defaulted to off. when it won't anymore we
2835 * will consider the HW capability: "!!MLX5_CAP_ETH(mdev, lro_cap)"
2837 priv->params.hw_lro_en = false;
2838 priv->params.lro_wqe_sz = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
2840 priv->params.cqe_zipping_en = !!MLX5_CAP_GEN(mdev, cqe_compression);
2843 priv->params.num_channels = num_comp_vectors;
2844 priv->order_base_2_num_channels = order_base_2(num_comp_vectors);
2845 priv->queue_mapping_channel_mask =
2846 roundup_pow_of_two(num_comp_vectors) - 1;
2847 priv->num_tc = priv->params.num_tc;
2848 priv->default_vlan_prio = priv->params.default_vlan_prio;
2850 INIT_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
2851 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
2852 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
2856 mlx5e_create_mkey(struct mlx5e_priv *priv, u32 pdn,
2857 struct mlx5_core_mr *mr)
2859 struct ifnet *ifp = priv->ifp;
2860 struct mlx5_core_dev *mdev = priv->mdev;
2861 struct mlx5_create_mkey_mbox_in *in;
2864 in = mlx5_vzalloc(sizeof(*in));
2866 if_printf(ifp, "%s: failed to allocate inbox\n", __func__);
2869 in->seg.flags = MLX5_PERM_LOCAL_WRITE |
2870 MLX5_PERM_LOCAL_READ |
2871 MLX5_ACCESS_MODE_PA;
2872 in->seg.flags_pd = cpu_to_be32(pdn | MLX5_MKEY_LEN64);
2873 in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
2875 err = mlx5_core_create_mkey(mdev, mr, in, sizeof(*in), NULL, NULL,
2878 if_printf(ifp, "%s: mlx5_core_create_mkey failed, %d\n",
2886 static const char *mlx5e_vport_stats_desc[] = {
2887 MLX5E_VPORT_STATS(MLX5E_STATS_DESC)
2890 static const char *mlx5e_pport_stats_desc[] = {
2891 MLX5E_PPORT_STATS(MLX5E_STATS_DESC)
2895 mlx5e_priv_mtx_init(struct mlx5e_priv *priv)
2897 mtx_init(&priv->async_events_mtx, "mlx5async", MTX_NETWORK_LOCK, MTX_DEF);
2898 sx_init(&priv->state_lock, "mlx5state");
2899 callout_init_mtx(&priv->watchdog, &priv->async_events_mtx, 0);
2900 MLX5_INIT_DOORBELL_LOCK(&priv->doorbell_lock);
2904 mlx5e_priv_mtx_destroy(struct mlx5e_priv *priv)
2906 mtx_destroy(&priv->async_events_mtx);
2907 sx_destroy(&priv->state_lock);
2911 sysctl_firmware(SYSCTL_HANDLER_ARGS)
2914 * %d.%d%.d the string format.
2915 * fw_rev_{maj,min,sub} return u16, 2^16 = 65536.
2916 * We need at most 5 chars to store that.
2917 * It also has: two "." and NULL at the end, which means we need 18
2918 * (5*3 + 3) chars at most.
2921 struct mlx5e_priv *priv = arg1;
2924 snprintf(fw, sizeof(fw), "%d.%d.%d", fw_rev_maj(priv->mdev), fw_rev_min(priv->mdev),
2925 fw_rev_sub(priv->mdev));
2926 error = sysctl_handle_string(oidp, fw, sizeof(fw), req);
2931 mlx5e_add_hw_stats(struct mlx5e_priv *priv)
2933 SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_hw),
2934 OID_AUTO, "fw_version", CTLTYPE_STRING | CTLFLAG_RD, priv, 0,
2935 sysctl_firmware, "A", "HCA firmware version");
2937 SYSCTL_ADD_STRING(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_hw),
2938 OID_AUTO, "board_id", CTLFLAG_RD, priv->mdev->board_id, 0,
2943 mlx5e_setup_pauseframes(struct mlx5e_priv *priv)
2945 #if (__FreeBSD_version < 1100000)
2949 /* Only receiving pauseframes is enabled by default */
2950 priv->params.tx_pauseframe_control = 0;
2951 priv->params.rx_pauseframe_control = 1;
2953 #if (__FreeBSD_version < 1100000)
2954 /* compute path for sysctl */
2955 snprintf(path, sizeof(path), "dev.mce.%d.tx_pauseframe_control",
2956 device_get_unit(priv->mdev->pdev->dev.bsddev));
2958 /* try to fetch tunable, if any */
2959 TUNABLE_INT_FETCH(path, &priv->params.tx_pauseframe_control);
2961 /* compute path for sysctl */
2962 snprintf(path, sizeof(path), "dev.mce.%d.rx_pauseframe_control",
2963 device_get_unit(priv->mdev->pdev->dev.bsddev));
2965 /* try to fetch tunable, if any */
2966 TUNABLE_INT_FETCH(path, &priv->params.rx_pauseframe_control);
2969 /* register pausframe SYSCTLs */
2970 SYSCTL_ADD_INT(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
2971 OID_AUTO, "tx_pauseframe_control", CTLFLAG_RDTUN,
2972 &priv->params.tx_pauseframe_control, 0,
2973 "Set to enable TX pause frames. Clear to disable.");
2975 SYSCTL_ADD_INT(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
2976 OID_AUTO, "rx_pauseframe_control", CTLFLAG_RDTUN,
2977 &priv->params.rx_pauseframe_control, 0,
2978 "Set to enable RX pause frames. Clear to disable.");
2981 priv->params.tx_pauseframe_control =
2982 priv->params.tx_pauseframe_control ? 1 : 0;
2983 priv->params.rx_pauseframe_control =
2984 priv->params.rx_pauseframe_control ? 1 : 0;
2986 /* update firmware */
2987 mlx5_set_port_pause(priv->mdev, 1,
2988 priv->params.rx_pauseframe_control,
2989 priv->params.tx_pauseframe_control);
2993 mlx5e_create_ifp(struct mlx5_core_dev *mdev)
2995 static volatile int mlx5_en_unit;
2997 struct mlx5e_priv *priv;
2998 u8 dev_addr[ETHER_ADDR_LEN] __aligned(4);
2999 struct sysctl_oid_list *child;
3000 int ncv = mdev->priv.eq_table.num_comp_vectors;
3006 if (mlx5e_check_required_hca_cap(mdev)) {
3007 mlx5_core_dbg(mdev, "mlx5e_check_required_hca_cap() failed\n");
3010 priv = malloc(sizeof(*priv), M_MLX5EN, M_WAITOK | M_ZERO);
3011 mlx5e_priv_mtx_init(priv);
3013 ifp = priv->ifp = if_alloc(IFT_ETHER);
3015 mlx5_core_err(mdev, "if_alloc() failed\n");
3018 ifp->if_softc = priv;
3019 if_initname(ifp, "mce", atomic_fetchadd_int(&mlx5_en_unit, 1));
3020 ifp->if_mtu = ETHERMTU;
3021 ifp->if_init = mlx5e_open;
3022 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
3023 ifp->if_ioctl = mlx5e_ioctl;
3024 ifp->if_transmit = mlx5e_xmit;
3025 ifp->if_qflush = if_qflush;
3026 #if (__FreeBSD_version >= 1100000)
3027 ifp->if_get_counter = mlx5e_get_counter;
3029 ifp->if_snd.ifq_maxlen = ifqmaxlen;
3031 * Set driver features
3033 ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6;
3034 ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
3035 ifp->if_capabilities |= IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWFILTER;
3036 ifp->if_capabilities |= IFCAP_LINKSTATE | IFCAP_JUMBO_MTU;
3037 ifp->if_capabilities |= IFCAP_LRO;
3038 ifp->if_capabilities |= IFCAP_TSO | IFCAP_VLAN_HWTSO;
3039 ifp->if_capabilities |= IFCAP_HWSTATS;
3041 /* set TSO limits so that we don't have to drop TX packets */
3042 ifp->if_hw_tsomax = MLX5E_MAX_TX_PAYLOAD_SIZE - (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
3043 ifp->if_hw_tsomaxsegcount = MLX5E_MAX_TX_MBUF_FRAGS - 1 /* hdr */;
3044 ifp->if_hw_tsomaxsegsize = MLX5E_MAX_TX_MBUF_SIZE;
3046 ifp->if_capenable = ifp->if_capabilities;
3047 ifp->if_hwassist = 0;
3048 if (ifp->if_capenable & IFCAP_TSO)
3049 ifp->if_hwassist |= CSUM_TSO;
3050 if (ifp->if_capenable & IFCAP_TXCSUM)
3051 ifp->if_hwassist |= (CSUM_TCP | CSUM_UDP | CSUM_IP);
3052 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
3053 ifp->if_hwassist |= (CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
3055 /* ifnet sysctl tree */
3056 sysctl_ctx_init(&priv->sysctl_ctx);
3057 priv->sysctl_ifnet = SYSCTL_ADD_NODE(&priv->sysctl_ctx, SYSCTL_STATIC_CHILDREN(_dev),
3058 OID_AUTO, ifp->if_dname, CTLFLAG_RD, 0, "MLX5 ethernet - interface name");
3059 if (priv->sysctl_ifnet == NULL) {
3060 mlx5_core_err(mdev, "SYSCTL_ADD_NODE() failed\n");
3061 goto err_free_sysctl;
3063 snprintf(unit, sizeof(unit), "%d", ifp->if_dunit);
3064 priv->sysctl_ifnet = SYSCTL_ADD_NODE(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3065 OID_AUTO, unit, CTLFLAG_RD, 0, "MLX5 ethernet - interface unit");
3066 if (priv->sysctl_ifnet == NULL) {
3067 mlx5_core_err(mdev, "SYSCTL_ADD_NODE() failed\n");
3068 goto err_free_sysctl;
3071 /* HW sysctl tree */
3072 child = SYSCTL_CHILDREN(device_get_sysctl_tree(mdev->pdev->dev.bsddev));
3073 priv->sysctl_hw = SYSCTL_ADD_NODE(&priv->sysctl_ctx, child,
3074 OID_AUTO, "hw", CTLFLAG_RD, 0, "MLX5 ethernet dev hw");
3075 if (priv->sysctl_hw == NULL) {
3076 mlx5_core_err(mdev, "SYSCTL_ADD_NODE() failed\n");
3077 goto err_free_sysctl;
3079 mlx5e_build_ifp_priv(mdev, priv, ncv);
3080 err = mlx5_alloc_map_uar(mdev, &priv->cq_uar);
3082 if_printf(ifp, "%s: mlx5_alloc_map_uar failed, %d\n",
3084 goto err_free_sysctl;
3086 err = mlx5_core_alloc_pd(mdev, &priv->pdn);
3088 if_printf(ifp, "%s: mlx5_core_alloc_pd failed, %d\n",
3090 goto err_unmap_free_uar;
3092 err = mlx5_alloc_transport_domain(mdev, &priv->tdn);
3094 if_printf(ifp, "%s: mlx5_alloc_transport_domain failed, %d\n",
3096 goto err_dealloc_pd;
3098 err = mlx5e_create_mkey(priv, priv->pdn, &priv->mr);
3100 if_printf(ifp, "%s: mlx5e_create_mkey failed, %d\n",
3102 goto err_dealloc_transport_domain;
3104 mlx5_query_nic_vport_mac_address(priv->mdev, 0, dev_addr);
3106 /* check if we should generate a random MAC address */
3107 if (MLX5_CAP_GEN(priv->mdev, vport_group_manager) == 0 &&
3108 is_zero_ether_addr(dev_addr)) {
3109 random_ether_addr(dev_addr);
3110 if_printf(ifp, "Assigned random MAC address\n");
3113 /* set default MTU */
3114 mlx5e_set_dev_port_mtu(ifp, ifp->if_mtu);
3117 device_set_desc(mdev->pdev->dev.bsddev, mlx5e_version);
3119 /* Set default media status */
3120 priv->media_status_last = IFM_AVALID;
3121 priv->media_active_last = IFM_ETHER | IFM_AUTO |
3122 IFM_ETH_RXPAUSE | IFM_FDX;
3124 /* setup default pauseframes configuration */
3125 mlx5e_setup_pauseframes(priv);
3127 err = mlx5_query_port_proto_cap(mdev, ð_proto_cap, MLX5_PTYS_EN);
3130 if_printf(ifp, "%s: Query port media capability failed, %d\n",
3134 /* Setup supported medias */
3135 ifmedia_init(&priv->media, IFM_IMASK | IFM_ETH_FMASK,
3136 mlx5e_media_change, mlx5e_media_status);
3138 for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
3139 if (mlx5e_mode_table[i].baudrate == 0)
3141 if (MLX5E_PROT_MASK(i) & eth_proto_cap) {
3142 ifmedia_add(&priv->media,
3143 mlx5e_mode_table[i].subtype |
3144 IFM_ETHER, 0, NULL);
3145 ifmedia_add(&priv->media,
3146 mlx5e_mode_table[i].subtype |
3147 IFM_ETHER | IFM_FDX |
3148 IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE, 0, NULL);
3152 ifmedia_add(&priv->media, IFM_ETHER | IFM_AUTO, 0, NULL);
3153 ifmedia_add(&priv->media, IFM_ETHER | IFM_AUTO | IFM_FDX |
3154 IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE, 0, NULL);
3156 /* Set autoselect by default */
3157 ifmedia_set(&priv->media, IFM_ETHER | IFM_AUTO | IFM_FDX |
3158 IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE);
3159 ether_ifattach(ifp, dev_addr);
3161 /* Register for VLAN events */
3162 priv->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
3163 mlx5e_vlan_rx_add_vid, priv, EVENTHANDLER_PRI_FIRST);
3164 priv->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
3165 mlx5e_vlan_rx_kill_vid, priv, EVENTHANDLER_PRI_FIRST);
3167 /* Link is down by default */
3168 if_link_state_change(ifp, LINK_STATE_DOWN);
3170 mlx5e_enable_async_events(priv);
3172 mlx5e_add_hw_stats(priv);
3174 mlx5e_create_stats(&priv->stats.vport.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3175 "vstats", mlx5e_vport_stats_desc, MLX5E_VPORT_STATS_NUM,
3176 priv->stats.vport.arg);
3178 mlx5e_create_stats(&priv->stats.pport.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3179 "pstats", mlx5e_pport_stats_desc, MLX5E_PPORT_STATS_NUM,
3180 priv->stats.pport.arg);
3182 mlx5e_create_ethtool(priv);
3184 mtx_lock(&priv->async_events_mtx);
3185 mlx5e_update_stats(priv);
3186 mtx_unlock(&priv->async_events_mtx);
3190 err_dealloc_transport_domain:
3191 mlx5_dealloc_transport_domain(mdev, priv->tdn);
3194 mlx5_core_dealloc_pd(mdev, priv->pdn);
3197 mlx5_unmap_free_uar(mdev, &priv->cq_uar);
3200 sysctl_ctx_free(&priv->sysctl_ctx);
3205 mlx5e_priv_mtx_destroy(priv);
3206 free(priv, M_MLX5EN);
3211 mlx5e_destroy_ifp(struct mlx5_core_dev *mdev, void *vpriv)
3213 struct mlx5e_priv *priv = vpriv;
3214 struct ifnet *ifp = priv->ifp;
3216 /* don't allow more IOCTLs */
3220 * Clear the device description to avoid use after free,
3221 * because the bsddev is not destroyed when this module is
3224 device_set_desc(mdev->pdev->dev.bsddev, NULL);
3226 /* XXX wait a bit to allow IOCTL handlers to complete */
3229 /* stop watchdog timer */
3230 callout_drain(&priv->watchdog);
3232 if (priv->vlan_attach != NULL)
3233 EVENTHANDLER_DEREGISTER(vlan_config, priv->vlan_attach);
3234 if (priv->vlan_detach != NULL)
3235 EVENTHANDLER_DEREGISTER(vlan_unconfig, priv->vlan_detach);
3237 /* make sure device gets closed */
3239 mlx5e_close_locked(ifp);
3242 /* unregister device */
3243 ifmedia_removeall(&priv->media);
3244 ether_ifdetach(ifp);
3247 /* destroy all remaining sysctl nodes */
3248 if (priv->sysctl_debug)
3249 sysctl_ctx_free(&priv->stats.port_stats_debug.ctx);
3250 sysctl_ctx_free(&priv->stats.vport.ctx);
3251 sysctl_ctx_free(&priv->stats.pport.ctx);
3252 sysctl_ctx_free(&priv->sysctl_ctx);
3254 mlx5_core_destroy_mkey(priv->mdev, &priv->mr);
3255 mlx5_dealloc_transport_domain(priv->mdev, priv->tdn);
3256 mlx5_core_dealloc_pd(priv->mdev, priv->pdn);
3257 mlx5_unmap_free_uar(priv->mdev, &priv->cq_uar);
3258 mlx5e_disable_async_events(priv);
3259 flush_scheduled_work();
3260 mlx5e_priv_mtx_destroy(priv);
3261 free(priv, M_MLX5EN);
3265 mlx5e_get_ifp(void *vpriv)
3267 struct mlx5e_priv *priv = vpriv;
3272 static struct mlx5_interface mlx5e_interface = {
3273 .add = mlx5e_create_ifp,
3274 .remove = mlx5e_destroy_ifp,
3275 .event = mlx5e_async_event,
3276 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
3277 .get_dev = mlx5e_get_ifp,
3283 mlx5_register_interface(&mlx5e_interface);
3289 mlx5_unregister_interface(&mlx5e_interface);
3292 module_init_order(mlx5e_init, SI_ORDER_THIRD);
3293 module_exit_order(mlx5e_cleanup, SI_ORDER_THIRD);
3295 #if (__FreeBSD_version >= 1100000)
3296 MODULE_DEPEND(mlx5en, linuxkpi, 1, 1, 1);
3298 MODULE_DEPEND(mlx5en, mlx5, 1, 1, 1);
3299 MODULE_VERSION(mlx5en, 1);