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_rq_param {
38 u32 rqc [MLX5_ST_SZ_DW(rqc)];
39 struct mlx5_wq_param wq;
42 struct mlx5e_sq_param {
43 u32 sqc [MLX5_ST_SZ_DW(sqc)];
44 struct mlx5_wq_param wq;
47 struct mlx5e_cq_param {
48 u32 cqc [MLX5_ST_SZ_DW(cqc)];
49 struct mlx5_wq_param wq;
53 struct mlx5e_channel_param {
54 struct mlx5e_rq_param rq;
55 struct mlx5e_sq_param sq;
56 struct mlx5e_cq_param rx_cq;
57 struct mlx5e_cq_param tx_cq;
63 } mlx5e_mode_table[MLX5E_LINK_MODES_NUMBER] = {
65 [MLX5E_1000BASE_CX_SGMII] = {
66 .subtype = IFM_1000_CX_SGMII,
67 .baudrate = IF_Mbps(1000ULL),
69 [MLX5E_1000BASE_KX] = {
70 .subtype = IFM_1000_KX,
71 .baudrate = IF_Mbps(1000ULL),
73 [MLX5E_10GBASE_CX4] = {
74 .subtype = IFM_10G_CX4,
75 .baudrate = IF_Gbps(10ULL),
77 [MLX5E_10GBASE_KX4] = {
78 .subtype = IFM_10G_KX4,
79 .baudrate = IF_Gbps(10ULL),
81 [MLX5E_10GBASE_KR] = {
82 .subtype = IFM_10G_KR,
83 .baudrate = IF_Gbps(10ULL),
85 [MLX5E_20GBASE_KR2] = {
86 .subtype = IFM_20G_KR2,
87 .baudrate = IF_Gbps(20ULL),
89 [MLX5E_40GBASE_CR4] = {
90 .subtype = IFM_40G_CR4,
91 .baudrate = IF_Gbps(40ULL),
93 [MLX5E_40GBASE_KR4] = {
94 .subtype = IFM_40G_KR4,
95 .baudrate = IF_Gbps(40ULL),
97 [MLX5E_56GBASE_R4] = {
98 .subtype = IFM_56G_R4,
99 .baudrate = IF_Gbps(56ULL),
101 [MLX5E_10GBASE_CR] = {
102 .subtype = IFM_10G_CR1,
103 .baudrate = IF_Gbps(10ULL),
105 [MLX5E_10GBASE_SR] = {
106 .subtype = IFM_10G_SR,
107 .baudrate = IF_Gbps(10ULL),
109 [MLX5E_10GBASE_LR] = {
110 .subtype = IFM_10G_LR,
111 .baudrate = IF_Gbps(10ULL),
113 [MLX5E_40GBASE_SR4] = {
114 .subtype = IFM_40G_SR4,
115 .baudrate = IF_Gbps(40ULL),
117 [MLX5E_40GBASE_LR4] = {
118 .subtype = IFM_40G_LR4,
119 .baudrate = IF_Gbps(40ULL),
121 [MLX5E_100GBASE_CR4] = {
122 .subtype = IFM_100G_CR4,
123 .baudrate = IF_Gbps(100ULL),
125 [MLX5E_100GBASE_SR4] = {
126 .subtype = IFM_100G_SR4,
127 .baudrate = IF_Gbps(100ULL),
129 [MLX5E_100GBASE_KR4] = {
130 .subtype = IFM_100G_KR4,
131 .baudrate = IF_Gbps(100ULL),
133 [MLX5E_100GBASE_LR4] = {
134 .subtype = IFM_100G_LR4,
135 .baudrate = IF_Gbps(100ULL),
137 [MLX5E_100BASE_TX] = {
138 .subtype = IFM_100_TX,
139 .baudrate = IF_Mbps(100ULL),
141 [MLX5E_100BASE_T] = {
142 .subtype = IFM_100_T,
143 .baudrate = IF_Mbps(100ULL),
145 [MLX5E_10GBASE_T] = {
146 .subtype = IFM_10G_T,
147 .baudrate = IF_Gbps(10ULL),
149 [MLX5E_25GBASE_CR] = {
150 .subtype = IFM_25G_CR,
151 .baudrate = IF_Gbps(25ULL),
153 [MLX5E_25GBASE_KR] = {
154 .subtype = IFM_25G_KR,
155 .baudrate = IF_Gbps(25ULL),
157 [MLX5E_25GBASE_SR] = {
158 .subtype = IFM_25G_SR,
159 .baudrate = IF_Gbps(25ULL),
161 [MLX5E_50GBASE_CR2] = {
162 .subtype = IFM_50G_CR2,
163 .baudrate = IF_Gbps(50ULL),
165 [MLX5E_50GBASE_KR2] = {
166 .subtype = IFM_50G_KR2,
167 .baudrate = IF_Gbps(50ULL),
171 MALLOC_DEFINE(M_MLX5EN, "MLX5EN", "MLX5 Ethernet");
174 mlx5e_update_carrier(struct mlx5e_priv *priv)
176 struct mlx5_core_dev *mdev = priv->mdev;
177 u32 out[MLX5_ST_SZ_DW(ptys_reg)];
183 port_state = mlx5_query_vport_state(mdev,
184 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT);
186 if (port_state == VPORT_STATE_UP) {
187 priv->media_status_last |= IFM_ACTIVE;
189 priv->media_status_last &= ~IFM_ACTIVE;
190 priv->media_active_last = IFM_ETHER;
191 if_link_state_change(priv->ifp, LINK_STATE_DOWN);
195 error = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN);
197 priv->media_active_last = IFM_ETHER;
198 priv->ifp->if_baudrate = 1;
199 if_printf(priv->ifp, "%s: query port ptys failed: 0x%x\n",
203 eth_proto_oper = MLX5_GET(ptys_reg, out, eth_proto_oper);
205 for (i = 0; i != MLX5E_LINK_MODES_NUMBER; i++) {
206 if (mlx5e_mode_table[i].baudrate == 0)
208 if (MLX5E_PROT_MASK(i) & eth_proto_oper) {
209 priv->ifp->if_baudrate =
210 mlx5e_mode_table[i].baudrate;
211 priv->media_active_last =
212 mlx5e_mode_table[i].subtype | IFM_ETHER | IFM_FDX;
215 if_link_state_change(priv->ifp, LINK_STATE_UP);
219 mlx5e_media_status(struct ifnet *dev, struct ifmediareq *ifmr)
221 struct mlx5e_priv *priv = dev->if_softc;
223 ifmr->ifm_status = priv->media_status_last;
224 ifmr->ifm_active = priv->media_active_last |
225 (priv->params.rx_pauseframe_control ? IFM_ETH_RXPAUSE : 0) |
226 (priv->params.tx_pauseframe_control ? IFM_ETH_TXPAUSE : 0);
231 mlx5e_find_link_mode(u32 subtype)
236 for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
237 if (mlx5e_mode_table[i].baudrate == 0)
239 if (mlx5e_mode_table[i].subtype == subtype)
240 link_mode |= MLX5E_PROT_MASK(i);
247 mlx5e_media_change(struct ifnet *dev)
249 struct mlx5e_priv *priv = dev->if_softc;
250 struct mlx5_core_dev *mdev = priv->mdev;
257 locked = PRIV_LOCKED(priv);
261 if (IFM_TYPE(priv->media.ifm_media) != IFM_ETHER) {
265 link_mode = mlx5e_find_link_mode(IFM_SUBTYPE(priv->media.ifm_media));
267 /* query supported capabilities */
268 error = mlx5_query_port_proto_cap(mdev, ð_proto_cap, MLX5_PTYS_EN);
270 if_printf(dev, "Query port media capability failed\n");
273 /* check for autoselect */
274 if (IFM_SUBTYPE(priv->media.ifm_media) == IFM_AUTO) {
275 link_mode = eth_proto_cap;
276 if (link_mode == 0) {
277 if_printf(dev, "Port media capability is zero\n");
282 link_mode = link_mode & eth_proto_cap;
283 if (link_mode == 0) {
284 if_printf(dev, "Not supported link mode requested\n");
289 /* update pauseframe control bits */
290 priv->params.rx_pauseframe_control =
291 (priv->media.ifm_media & IFM_ETH_RXPAUSE) ? 1 : 0;
292 priv->params.tx_pauseframe_control =
293 (priv->media.ifm_media & IFM_ETH_TXPAUSE) ? 1 : 0;
295 /* check if device is opened */
296 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
298 /* reconfigure the hardware */
299 mlx5_set_port_status(mdev, MLX5_PORT_DOWN);
300 mlx5_set_port_proto(mdev, link_mode, MLX5_PTYS_EN);
301 mlx5_set_port_pause(mdev, 1,
302 priv->params.rx_pauseframe_control,
303 priv->params.tx_pauseframe_control);
305 mlx5_set_port_status(mdev, MLX5_PORT_UP);
314 mlx5e_update_carrier_work(struct work_struct *work)
316 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
317 update_carrier_work);
320 if (test_bit(MLX5E_STATE_OPENED, &priv->state))
321 mlx5e_update_carrier(priv);
326 mlx5e_update_pport_counters(struct mlx5e_priv *priv)
328 struct mlx5_core_dev *mdev = priv->mdev;
329 struct mlx5e_pport_stats *s = &priv->stats.pport;
330 struct mlx5e_port_stats_debug *s_debug = &priv->stats.port_stats_debug;
334 unsigned sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
338 in = mlx5_vzalloc(sz);
339 out = mlx5_vzalloc(sz);
340 if (in == NULL || out == NULL)
343 ptr = (uint64_t *)MLX5_ADDR_OF(ppcnt_reg, out, counter_set);
345 MLX5_SET(ppcnt_reg, in, local_port, 1);
347 MLX5_SET(ppcnt_reg, in, grp, MLX5_IEEE_802_3_COUNTERS_GROUP);
348 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
349 for (x = y = 0; x != MLX5E_PPORT_IEEE802_3_STATS_NUM; x++, y++)
350 s->arg[y] = be64toh(ptr[x]);
352 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2819_COUNTERS_GROUP);
353 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
354 for (x = 0; x != MLX5E_PPORT_RFC2819_STATS_NUM; x++, y++)
355 s->arg[y] = be64toh(ptr[x]);
356 for (y = 0; x != MLX5E_PPORT_RFC2819_STATS_NUM +
357 MLX5E_PPORT_RFC2819_STATS_DEBUG_NUM; x++, y++)
358 s_debug->arg[y] = be64toh(ptr[x]);
360 MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2863_COUNTERS_GROUP);
361 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
362 for (x = 0; x != MLX5E_PPORT_RFC2863_STATS_DEBUG_NUM; x++, y++)
363 s_debug->arg[y] = be64toh(ptr[x]);
365 MLX5_SET(ppcnt_reg, in, grp, MLX5_PHYSICAL_LAYER_COUNTERS_GROUP);
366 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
367 for (x = 0; x != MLX5E_PPORT_PHYSICAL_LAYER_STATS_DEBUG_NUM; x++, y++)
368 s_debug->arg[y] = be64toh(ptr[x]);
375 mlx5e_update_stats_work(struct work_struct *work)
377 struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
379 struct mlx5_core_dev *mdev = priv->mdev;
380 struct mlx5e_vport_stats *s = &priv->stats.vport;
381 struct mlx5e_rq_stats *rq_stats;
382 struct mlx5e_sq_stats *sq_stats;
383 struct buf_ring *sq_br;
384 #if (__FreeBSD_version < 1100000)
385 struct ifnet *ifp = priv->ifp;
388 u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)];
390 int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
393 u64 tx_queue_dropped = 0;
394 u64 tx_defragged = 0;
395 u64 tx_offload_none = 0;
398 u64 sw_lro_queued = 0;
399 u64 sw_lro_flushed = 0;
400 u64 rx_csum_none = 0;
402 u32 rx_out_of_buffer = 0;
407 out = mlx5_vzalloc(outlen);
410 if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
413 /* Collect firts the SW counters and then HW for consistency */
414 for (i = 0; i < priv->params.num_channels; i++) {
415 struct mlx5e_rq *rq = &priv->channel[i]->rq;
417 rq_stats = &priv->channel[i]->rq.stats;
419 /* collect stats from LRO */
420 rq_stats->sw_lro_queued = rq->lro.lro_queued;
421 rq_stats->sw_lro_flushed = rq->lro.lro_flushed;
422 sw_lro_queued += rq_stats->sw_lro_queued;
423 sw_lro_flushed += rq_stats->sw_lro_flushed;
424 lro_packets += rq_stats->lro_packets;
425 lro_bytes += rq_stats->lro_bytes;
426 rx_csum_none += rq_stats->csum_none;
427 rx_wqe_err += rq_stats->wqe_err;
429 for (j = 0; j < priv->num_tc; j++) {
430 sq_stats = &priv->channel[i]->sq[j].stats;
431 sq_br = priv->channel[i]->sq[j].br;
433 tso_packets += sq_stats->tso_packets;
434 tso_bytes += sq_stats->tso_bytes;
435 tx_queue_dropped += sq_stats->dropped;
436 tx_queue_dropped += sq_br->br_drops;
437 tx_defragged += sq_stats->defragged;
438 tx_offload_none += sq_stats->csum_offload_none;
442 /* update counters */
443 s->tso_packets = tso_packets;
444 s->tso_bytes = tso_bytes;
445 s->tx_queue_dropped = tx_queue_dropped;
446 s->tx_defragged = tx_defragged;
447 s->lro_packets = lro_packets;
448 s->lro_bytes = lro_bytes;
449 s->sw_lro_queued = sw_lro_queued;
450 s->sw_lro_flushed = sw_lro_flushed;
451 s->rx_csum_none = rx_csum_none;
452 s->rx_wqe_err = rx_wqe_err;
455 memset(in, 0, sizeof(in));
457 MLX5_SET(query_vport_counter_in, in, opcode,
458 MLX5_CMD_OP_QUERY_VPORT_COUNTER);
459 MLX5_SET(query_vport_counter_in, in, op_mod, 0);
460 MLX5_SET(query_vport_counter_in, in, other_vport, 0);
462 memset(out, 0, outlen);
464 /* get number of out-of-buffer drops first */
465 if (mlx5_vport_query_out_of_rx_buffer(mdev, priv->counter_set_id,
469 /* accumulate difference into a 64-bit counter */
470 s->rx_out_of_buffer += (u64)(u32)(rx_out_of_buffer - s->rx_out_of_buffer_prev);
471 s->rx_out_of_buffer_prev = rx_out_of_buffer;
473 /* get port statistics */
474 if (mlx5_cmd_exec(mdev, in, sizeof(in), out, outlen))
477 #define MLX5_GET_CTR(out, x) \
478 MLX5_GET64(query_vport_counter_out, out, x)
480 s->rx_error_packets =
481 MLX5_GET_CTR(out, received_errors.packets);
483 MLX5_GET_CTR(out, received_errors.octets);
484 s->tx_error_packets =
485 MLX5_GET_CTR(out, transmit_errors.packets);
487 MLX5_GET_CTR(out, transmit_errors.octets);
489 s->rx_unicast_packets =
490 MLX5_GET_CTR(out, received_eth_unicast.packets);
491 s->rx_unicast_bytes =
492 MLX5_GET_CTR(out, received_eth_unicast.octets);
493 s->tx_unicast_packets =
494 MLX5_GET_CTR(out, transmitted_eth_unicast.packets);
495 s->tx_unicast_bytes =
496 MLX5_GET_CTR(out, transmitted_eth_unicast.octets);
498 s->rx_multicast_packets =
499 MLX5_GET_CTR(out, received_eth_multicast.packets);
500 s->rx_multicast_bytes =
501 MLX5_GET_CTR(out, received_eth_multicast.octets);
502 s->tx_multicast_packets =
503 MLX5_GET_CTR(out, transmitted_eth_multicast.packets);
504 s->tx_multicast_bytes =
505 MLX5_GET_CTR(out, transmitted_eth_multicast.octets);
507 s->rx_broadcast_packets =
508 MLX5_GET_CTR(out, received_eth_broadcast.packets);
509 s->rx_broadcast_bytes =
510 MLX5_GET_CTR(out, received_eth_broadcast.octets);
511 s->tx_broadcast_packets =
512 MLX5_GET_CTR(out, transmitted_eth_broadcast.packets);
513 s->tx_broadcast_bytes =
514 MLX5_GET_CTR(out, transmitted_eth_broadcast.octets);
517 s->rx_unicast_packets +
518 s->rx_multicast_packets +
519 s->rx_broadcast_packets -
522 s->rx_unicast_bytes +
523 s->rx_multicast_bytes +
524 s->rx_broadcast_bytes;
526 s->tx_unicast_packets +
527 s->tx_multicast_packets +
528 s->tx_broadcast_packets;
530 s->tx_unicast_bytes +
531 s->tx_multicast_bytes +
532 s->tx_broadcast_bytes;
534 /* Update calculated offload counters */
535 s->tx_csum_offload = s->tx_packets - tx_offload_none;
536 s->rx_csum_good = s->rx_packets - s->rx_csum_none;
538 /* Update per port counters */
539 mlx5e_update_pport_counters(priv);
541 #if (__FreeBSD_version < 1100000)
542 /* no get_counters interface in fbsd 10 */
543 ifp->if_ipackets = s->rx_packets;
544 ifp->if_ierrors = s->rx_error_packets;
545 ifp->if_iqdrops = s->rx_out_of_buffer;
546 ifp->if_opackets = s->tx_packets;
547 ifp->if_oerrors = s->tx_error_packets;
548 ifp->if_snd.ifq_drops = s->tx_queue_dropped;
549 ifp->if_ibytes = s->rx_bytes;
550 ifp->if_obytes = s->tx_bytes;
559 mlx5e_update_stats(void *arg)
561 struct mlx5e_priv *priv = arg;
563 schedule_work(&priv->update_stats_work);
565 callout_reset(&priv->watchdog, hz, &mlx5e_update_stats, priv);
569 mlx5e_async_event_sub(struct mlx5e_priv *priv,
570 enum mlx5_dev_event event)
573 case MLX5_DEV_EVENT_PORT_UP:
574 case MLX5_DEV_EVENT_PORT_DOWN:
575 schedule_work(&priv->update_carrier_work);
584 mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
585 enum mlx5_dev_event event, unsigned long param)
587 struct mlx5e_priv *priv = vpriv;
589 mtx_lock(&priv->async_events_mtx);
590 if (test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state))
591 mlx5e_async_event_sub(priv, event);
592 mtx_unlock(&priv->async_events_mtx);
596 mlx5e_enable_async_events(struct mlx5e_priv *priv)
598 set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state);
602 mlx5e_disable_async_events(struct mlx5e_priv *priv)
604 mtx_lock(&priv->async_events_mtx);
605 clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state);
606 mtx_unlock(&priv->async_events_mtx);
609 static const char *mlx5e_rq_stats_desc[] = {
610 MLX5E_RQ_STATS(MLX5E_STATS_DESC)
614 mlx5e_create_rq(struct mlx5e_channel *c,
615 struct mlx5e_rq_param *param,
618 struct mlx5e_priv *priv = c->priv;
619 struct mlx5_core_dev *mdev = priv->mdev;
621 void *rqc = param->rqc;
622 void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
627 /* Create DMA descriptor TAG */
628 if ((err = -bus_dma_tag_create(
629 bus_get_dma_tag(mdev->pdev->dev.bsddev),
630 1, /* any alignment */
632 BUS_SPACE_MAXADDR, /* lowaddr */
633 BUS_SPACE_MAXADDR, /* highaddr */
634 NULL, NULL, /* filter, filterarg */
635 MJUM16BYTES, /* maxsize */
637 MJUM16BYTES, /* maxsegsize */
639 NULL, NULL, /* lockfunc, lockfuncarg */
643 err = mlx5_wq_ll_create(mdev, ¶m->wq, rqc_wq, &rq->wq,
646 goto err_free_dma_tag;
648 rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
650 if (priv->params.hw_lro_en) {
651 rq->wqe_sz = priv->params.lro_wqe_sz;
653 rq->wqe_sz = MLX5E_SW2MB_MTU(priv->ifp->if_mtu);
655 if (rq->wqe_sz > MJUM16BYTES) {
657 goto err_rq_wq_destroy;
658 } else if (rq->wqe_sz > MJUM9BYTES) {
659 rq->wqe_sz = MJUM16BYTES;
660 } else if (rq->wqe_sz > MJUMPAGESIZE) {
661 rq->wqe_sz = MJUM9BYTES;
662 } else if (rq->wqe_sz > MCLBYTES) {
663 rq->wqe_sz = MJUMPAGESIZE;
665 rq->wqe_sz = MCLBYTES;
668 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
669 rq->mbuf = malloc(wq_sz * sizeof(rq->mbuf[0]), M_MLX5EN, M_WAITOK | M_ZERO);
670 if (rq->mbuf == NULL) {
672 goto err_rq_wq_destroy;
674 for (i = 0; i != wq_sz; i++) {
675 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
676 uint32_t byte_count = rq->wqe_sz - MLX5E_NET_IP_ALIGN;
678 err = -bus_dmamap_create(rq->dma_tag, 0, &rq->mbuf[i].dma_map);
681 bus_dmamap_destroy(rq->dma_tag, rq->mbuf[i].dma_map);
682 goto err_rq_mbuf_free;
684 wqe->data.lkey = c->mkey_be;
685 wqe->data.byte_count = cpu_to_be32(byte_count | MLX5_HW_START_PADDING);
693 snprintf(buffer, sizeof(buffer), "rxstat%d", c->ix);
694 mlx5e_create_stats(&rq->stats.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
695 buffer, mlx5e_rq_stats_desc, MLX5E_RQ_STATS_NUM,
698 #ifdef HAVE_TURBO_LRO
699 if (tcp_tlro_init(&rq->lro, c->ifp, MLX5E_BUDGET_MAX) != 0)
702 if (tcp_lro_init(&rq->lro))
705 rq->lro.ifp = c->ifp;
710 free(rq->mbuf, M_MLX5EN);
712 mlx5_wq_destroy(&rq->wq_ctrl);
714 bus_dma_tag_destroy(rq->dma_tag);
720 mlx5e_destroy_rq(struct mlx5e_rq *rq)
725 /* destroy all sysctl nodes */
726 sysctl_ctx_free(&rq->stats.ctx);
728 /* free leftover LRO packets, if any */
729 #ifdef HAVE_TURBO_LRO
730 tcp_tlro_free(&rq->lro);
732 tcp_lro_free(&rq->lro);
734 wq_sz = mlx5_wq_ll_get_size(&rq->wq);
735 for (i = 0; i != wq_sz; i++) {
736 if (rq->mbuf[i].mbuf != NULL) {
737 bus_dmamap_unload(rq->dma_tag,
738 rq->mbuf[i].dma_map);
739 m_freem(rq->mbuf[i].mbuf);
741 bus_dmamap_destroy(rq->dma_tag, rq->mbuf[i].dma_map);
743 free(rq->mbuf, M_MLX5EN);
744 mlx5_wq_destroy(&rq->wq_ctrl);
748 mlx5e_enable_rq(struct mlx5e_rq *rq, struct mlx5e_rq_param *param)
750 struct mlx5e_channel *c = rq->channel;
751 struct mlx5e_priv *priv = c->priv;
752 struct mlx5_core_dev *mdev = priv->mdev;
760 inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
761 sizeof(u64) * rq->wq_ctrl.buf.npages;
762 in = mlx5_vzalloc(inlen);
766 rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
767 wq = MLX5_ADDR_OF(rqc, rqc, wq);
769 memcpy(rqc, param->rqc, sizeof(param->rqc));
771 MLX5_SET(rqc, rqc, cqn, c->rq.cq.mcq.cqn);
772 MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
773 MLX5_SET(rqc, rqc, flush_in_error_en, 1);
774 if (priv->counter_set_id >= 0)
775 MLX5_SET(rqc, rqc, counter_set_id, priv->counter_set_id);
776 MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
778 MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
780 mlx5_fill_page_array(&rq->wq_ctrl.buf,
781 (__be64 *) MLX5_ADDR_OF(wq, wq, pas));
783 err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
791 mlx5e_modify_rq(struct mlx5e_rq *rq, int curr_state, int next_state)
793 struct mlx5e_channel *c = rq->channel;
794 struct mlx5e_priv *priv = c->priv;
795 struct mlx5_core_dev *mdev = priv->mdev;
802 inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
803 in = mlx5_vzalloc(inlen);
807 rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
809 MLX5_SET(modify_rq_in, in, rqn, rq->rqn);
810 MLX5_SET(modify_rq_in, in, rq_state, curr_state);
811 MLX5_SET(rqc, rqc, state, next_state);
813 err = mlx5_core_modify_rq(mdev, in, inlen);
821 mlx5e_disable_rq(struct mlx5e_rq *rq)
823 struct mlx5e_channel *c = rq->channel;
824 struct mlx5e_priv *priv = c->priv;
825 struct mlx5_core_dev *mdev = priv->mdev;
827 mlx5_core_destroy_rq(mdev, rq->rqn);
831 mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq)
833 struct mlx5e_channel *c = rq->channel;
834 struct mlx5e_priv *priv = c->priv;
835 struct mlx5_wq_ll *wq = &rq->wq;
838 for (i = 0; i < 1000; i++) {
839 if (wq->cur_sz >= priv->params.min_rx_wqes)
848 mlx5e_open_rq(struct mlx5e_channel *c,
849 struct mlx5e_rq_param *param,
855 err = mlx5e_create_rq(c, param, rq);
859 err = mlx5e_enable_rq(rq, param);
863 err = mlx5e_modify_rq(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
870 * Test send queues, which will trigger
871 * "mlx5e_post_rx_wqes()":
873 for (i = 0; i != c->num_tc; i++)
874 mlx5e_send_nop(&c->sq[i], 1, true);
878 mlx5e_disable_rq(rq);
880 mlx5e_destroy_rq(rq);
886 mlx5e_close_rq(struct mlx5e_rq *rq)
889 mlx5e_modify_rq(rq, MLX5_RQC_STATE_RDY, MLX5_RQC_STATE_ERR);
893 mlx5e_close_rq_wait(struct mlx5e_rq *rq)
895 /* wait till RQ is empty */
896 while (!mlx5_wq_ll_is_empty(&rq->wq)) {
898 rq->cq.mcq.comp(&rq->cq.mcq);
901 mlx5e_disable_rq(rq);
902 mlx5e_destroy_rq(rq);
906 mlx5e_free_sq_db(struct mlx5e_sq *sq)
908 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
911 for (x = 0; x != wq_sz; x++)
912 bus_dmamap_destroy(sq->dma_tag, sq->mbuf[x].dma_map);
913 free(sq->mbuf, M_MLX5EN);
917 mlx5e_alloc_sq_db(struct mlx5e_sq *sq)
919 int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
923 sq->mbuf = malloc(wq_sz * sizeof(sq->mbuf[0]), M_MLX5EN, M_WAITOK | M_ZERO);
924 if (sq->mbuf == NULL)
927 /* Create DMA descriptor MAPs */
928 for (x = 0; x != wq_sz; x++) {
929 err = -bus_dmamap_create(sq->dma_tag, 0, &sq->mbuf[x].dma_map);
932 bus_dmamap_destroy(sq->dma_tag, sq->mbuf[x].dma_map);
933 free(sq->mbuf, M_MLX5EN);
940 static const char *mlx5e_sq_stats_desc[] = {
941 MLX5E_SQ_STATS(MLX5E_STATS_DESC)
945 mlx5e_create_sq(struct mlx5e_channel *c,
947 struct mlx5e_sq_param *param,
950 struct mlx5e_priv *priv = c->priv;
951 struct mlx5_core_dev *mdev = priv->mdev;
954 void *sqc = param->sqc;
955 void *sqc_wq = MLX5_ADDR_OF(sqc, sqc, wq);
962 /* Create DMA descriptor TAG */
963 if ((err = -bus_dma_tag_create(
964 bus_get_dma_tag(mdev->pdev->dev.bsddev),
965 1, /* any alignment */
967 BUS_SPACE_MAXADDR, /* lowaddr */
968 BUS_SPACE_MAXADDR, /* highaddr */
969 NULL, NULL, /* filter, filterarg */
970 MLX5E_MAX_TX_PAYLOAD_SIZE, /* maxsize */
971 MLX5E_MAX_TX_MBUF_FRAGS, /* nsegments */
972 MLX5E_MAX_TX_MBUF_SIZE, /* maxsegsize */
974 NULL, NULL, /* lockfunc, lockfuncarg */
978 err = mlx5_alloc_map_uar(mdev, &sq->uar);
980 goto err_free_dma_tag;
982 err = mlx5_wq_cyc_create(mdev, ¶m->wq, sqc_wq, &sq->wq,
985 goto err_unmap_free_uar;
987 sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
988 sq->uar_map = sq->uar.map;
989 sq->uar_bf_map = sq->uar.bf_map;
990 sq->bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
992 err = mlx5e_alloc_sq_db(sq);
994 goto err_sq_wq_destroy;
997 sq->mkey_be = c->mkey_be;
1001 sq->br = buf_ring_alloc(MLX5E_SQ_TX_QUEUE_SIZE, M_MLX5EN,
1002 M_WAITOK, &sq->lock);
1003 if (sq->br == NULL) {
1004 if_printf(c->ifp, "%s: Failed allocating sq drbr buffer\n",
1007 goto err_free_sq_db;
1010 sq->sq_tq = taskqueue_create_fast("mlx5e_que", M_WAITOK,
1011 taskqueue_thread_enqueue, &sq->sq_tq);
1012 if (sq->sq_tq == NULL) {
1013 if_printf(c->ifp, "%s: Failed allocating taskqueue\n",
1019 TASK_INIT(&sq->sq_task, 0, mlx5e_tx_que, sq);
1021 cpu_id = rss_getcpu(c->ix % rss_getnumbuckets());
1022 CPU_SETOF(cpu_id, &cpu_mask);
1023 taskqueue_start_threads_cpuset(&sq->sq_tq, 1, PI_NET, &cpu_mask,
1024 "%s TX SQ%d.%d CPU%d", c->ifp->if_xname, c->ix, tc, cpu_id);
1026 taskqueue_start_threads(&sq->sq_tq, 1, PI_NET,
1027 "%s TX SQ%d.%d", c->ifp->if_xname, c->ix, tc);
1029 snprintf(buffer, sizeof(buffer), "txstat%dtc%d", c->ix, tc);
1030 mlx5e_create_stats(&sq->stats.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
1031 buffer, mlx5e_sq_stats_desc, MLX5E_SQ_STATS_NUM,
1037 buf_ring_free(sq->br, M_MLX5EN);
1039 mlx5e_free_sq_db(sq);
1041 mlx5_wq_destroy(&sq->wq_ctrl);
1044 mlx5_unmap_free_uar(mdev, &sq->uar);
1047 bus_dma_tag_destroy(sq->dma_tag);
1053 mlx5e_destroy_sq(struct mlx5e_sq *sq)
1055 struct mlx5e_channel *c = sq->channel;
1056 struct mlx5e_priv *priv = c->priv;
1058 /* destroy all sysctl nodes */
1059 sysctl_ctx_free(&sq->stats.ctx);
1061 mlx5e_free_sq_db(sq);
1062 mlx5_wq_destroy(&sq->wq_ctrl);
1063 mlx5_unmap_free_uar(priv->mdev, &sq->uar);
1064 taskqueue_drain(sq->sq_tq, &sq->sq_task);
1065 taskqueue_free(sq->sq_tq);
1066 buf_ring_free(sq->br, M_MLX5EN);
1070 mlx5e_enable_sq(struct mlx5e_sq *sq, struct mlx5e_sq_param *param)
1072 struct mlx5e_channel *c = sq->channel;
1073 struct mlx5e_priv *priv = c->priv;
1074 struct mlx5_core_dev *mdev = priv->mdev;
1082 inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
1083 sizeof(u64) * sq->wq_ctrl.buf.npages;
1084 in = mlx5_vzalloc(inlen);
1088 sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1089 wq = MLX5_ADDR_OF(sqc, sqc, wq);
1091 memcpy(sqc, param->sqc, sizeof(param->sqc));
1093 MLX5_SET(sqc, sqc, tis_num_0, priv->tisn[sq->tc]);
1094 MLX5_SET(sqc, sqc, cqn, c->sq[sq->tc].cq.mcq.cqn);
1095 MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1096 MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1097 MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1099 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1100 MLX5_SET(wq, wq, uar_page, sq->uar.index);
1101 MLX5_SET(wq, wq, log_wq_pg_sz, sq->wq_ctrl.buf.page_shift -
1103 MLX5_SET64(wq, wq, dbr_addr, sq->wq_ctrl.db.dma);
1105 mlx5_fill_page_array(&sq->wq_ctrl.buf,
1106 (__be64 *) MLX5_ADDR_OF(wq, wq, pas));
1108 err = mlx5_core_create_sq(mdev, in, inlen, &sq->sqn);
1116 mlx5e_modify_sq(struct mlx5e_sq *sq, int curr_state, int next_state)
1118 struct mlx5e_channel *c = sq->channel;
1119 struct mlx5e_priv *priv = c->priv;
1120 struct mlx5_core_dev *mdev = priv->mdev;
1127 inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
1128 in = mlx5_vzalloc(inlen);
1132 sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
1134 MLX5_SET(modify_sq_in, in, sqn, sq->sqn);
1135 MLX5_SET(modify_sq_in, in, sq_state, curr_state);
1136 MLX5_SET(sqc, sqc, state, next_state);
1138 err = mlx5_core_modify_sq(mdev, in, inlen);
1146 mlx5e_disable_sq(struct mlx5e_sq *sq)
1148 struct mlx5e_channel *c = sq->channel;
1149 struct mlx5e_priv *priv = c->priv;
1150 struct mlx5_core_dev *mdev = priv->mdev;
1152 mlx5_core_destroy_sq(mdev, sq->sqn);
1156 mlx5e_open_sq(struct mlx5e_channel *c,
1158 struct mlx5e_sq_param *param,
1159 struct mlx5e_sq *sq)
1163 err = mlx5e_create_sq(c, tc, param, sq);
1167 err = mlx5e_enable_sq(sq, param);
1169 goto err_destroy_sq;
1171 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST, MLX5_SQC_STATE_RDY);
1173 goto err_disable_sq;
1175 atomic_store_rel_int(&sq->queue_state, MLX5E_SQ_READY);
1180 mlx5e_disable_sq(sq);
1182 mlx5e_destroy_sq(sq);
1188 mlx5e_close_sq(struct mlx5e_sq *sq)
1191 /* ensure hw is notified of all pending wqes */
1192 if (mlx5e_sq_has_room_for(sq, 1))
1193 mlx5e_send_nop(sq, 1, true);
1195 mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY, MLX5_SQC_STATE_ERR);
1199 mlx5e_close_sq_wait(struct mlx5e_sq *sq)
1201 /* wait till SQ is empty */
1202 while (sq->cc != sq->pc) {
1204 sq->cq.mcq.comp(&sq->cq.mcq);
1207 mlx5e_disable_sq(sq);
1208 mlx5e_destroy_sq(sq);
1212 mlx5e_create_cq(struct mlx5e_channel *c,
1213 struct mlx5e_cq_param *param,
1214 struct mlx5e_cq *cq,
1215 mlx5e_cq_comp_t *comp)
1217 struct mlx5e_priv *priv = c->priv;
1218 struct mlx5_core_dev *mdev = priv->mdev;
1219 struct mlx5_core_cq *mcq = &cq->mcq;
1225 param->wq.buf_numa_node = 0;
1226 param->wq.db_numa_node = 0;
1227 param->eq_ix = c->ix;
1229 err = mlx5_cqwq_create(mdev, ¶m->wq, param->cqc, &cq->wq,
1234 mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1237 mcq->set_ci_db = cq->wq_ctrl.db.db;
1238 mcq->arm_db = cq->wq_ctrl.db.db + 1;
1239 *mcq->set_ci_db = 0;
1241 mcq->vector = param->eq_ix;
1243 mcq->event = mlx5e_cq_error_event;
1245 mcq->uar = &priv->cq_uar;
1247 for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
1248 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1259 mlx5e_destroy_cq(struct mlx5e_cq *cq)
1261 mlx5_wq_destroy(&cq->wq_ctrl);
1265 mlx5e_enable_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param,
1268 struct mlx5e_channel *c = cq->channel;
1269 struct mlx5e_priv *priv = c->priv;
1270 struct mlx5_core_dev *mdev = priv->mdev;
1271 struct mlx5_core_cq *mcq = &cq->mcq;
1279 inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1280 sizeof(u64) * cq->wq_ctrl.buf.npages;
1281 in = mlx5_vzalloc(inlen);
1285 cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1287 memcpy(cqc, param->cqc, sizeof(param->cqc));
1289 mlx5_fill_page_array(&cq->wq_ctrl.buf,
1290 (__be64 *) MLX5_ADDR_OF(create_cq_in, in, pas));
1292 mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
1294 MLX5_SET(cqc, cqc, cq_period_mode, moderation_mode);
1295 MLX5_SET(cqc, cqc, c_eqn, eqn);
1296 MLX5_SET(cqc, cqc, uar_page, mcq->uar->index);
1297 MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
1299 MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
1301 err = mlx5_core_create_cq(mdev, mcq, in, inlen);
1314 mlx5e_disable_cq(struct mlx5e_cq *cq)
1316 struct mlx5e_channel *c = cq->channel;
1317 struct mlx5e_priv *priv = c->priv;
1318 struct mlx5_core_dev *mdev = priv->mdev;
1320 mlx5_core_destroy_cq(mdev, &cq->mcq);
1324 mlx5e_open_cq(struct mlx5e_channel *c,
1325 struct mlx5e_cq_param *param,
1326 struct mlx5e_cq *cq,
1327 mlx5e_cq_comp_t *comp,
1332 err = mlx5e_create_cq(c, param, cq, comp);
1336 err = mlx5e_enable_cq(cq, param, moderation_mode);
1338 goto err_destroy_cq;
1343 mlx5e_destroy_cq(cq);
1349 mlx5e_close_cq(struct mlx5e_cq *cq)
1351 mlx5e_disable_cq(cq);
1352 mlx5e_destroy_cq(cq);
1356 mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1357 struct mlx5e_channel_param *cparam)
1359 u8 tx_moderation_mode;
1363 switch (c->priv->params.tx_cq_moderation_mode) {
1365 tx_moderation_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1368 if (MLX5_CAP_GEN(c->priv->mdev, cq_period_start_from_cqe))
1369 tx_moderation_mode = MLX5_CQ_PERIOD_MODE_START_FROM_CQE;
1371 tx_moderation_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1374 for (tc = 0; tc < c->num_tc; tc++) {
1375 /* open completion queue */
1376 err = mlx5e_open_cq(c, &cparam->tx_cq, &c->sq[tc].cq,
1377 &mlx5e_tx_cq_comp, tx_moderation_mode);
1379 goto err_close_tx_cqs;
1384 for (tc--; tc >= 0; tc--)
1385 mlx5e_close_cq(&c->sq[tc].cq);
1391 mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1395 for (tc = 0; tc < c->num_tc; tc++)
1396 mlx5e_close_cq(&c->sq[tc].cq);
1400 mlx5e_open_sqs(struct mlx5e_channel *c,
1401 struct mlx5e_channel_param *cparam)
1406 for (tc = 0; tc < c->num_tc; tc++) {
1407 err = mlx5e_open_sq(c, tc, &cparam->sq, &c->sq[tc]);
1415 for (tc--; tc >= 0; tc--) {
1416 mlx5e_close_sq(&c->sq[tc]);
1417 mlx5e_close_sq_wait(&c->sq[tc]);
1424 mlx5e_close_sqs(struct mlx5e_channel *c)
1428 for (tc = 0; tc < c->num_tc; tc++)
1429 mlx5e_close_sq(&c->sq[tc]);
1433 mlx5e_close_sqs_wait(struct mlx5e_channel *c)
1437 for (tc = 0; tc < c->num_tc; tc++)
1438 mlx5e_close_sq_wait(&c->sq[tc]);
1442 mlx5e_chan_mtx_init(struct mlx5e_channel *c)
1446 mtx_init(&c->rq.mtx, "mlx5rx", MTX_NETWORK_LOCK, MTX_DEF);
1448 for (tc = 0; tc < c->num_tc; tc++) {
1449 mtx_init(&c->sq[tc].lock, "mlx5tx", MTX_NETWORK_LOCK, MTX_DEF);
1450 mtx_init(&c->sq[tc].comp_lock, "mlx5comp", MTX_NETWORK_LOCK,
1456 mlx5e_chan_mtx_destroy(struct mlx5e_channel *c)
1460 mtx_destroy(&c->rq.mtx);
1462 for (tc = 0; tc < c->num_tc; tc++) {
1463 mtx_destroy(&c->sq[tc].lock);
1464 mtx_destroy(&c->sq[tc].comp_lock);
1469 mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1470 struct mlx5e_channel_param *cparam,
1471 struct mlx5e_channel *volatile *cp)
1473 struct mlx5e_channel *c;
1474 u8 rx_moderation_mode;
1477 c = malloc(sizeof(*c), M_MLX5EN, M_WAITOK | M_ZERO);
1484 c->pdev = &priv->mdev->pdev->dev;
1486 c->mkey_be = cpu_to_be32(priv->mr.key);
1487 c->num_tc = priv->num_tc;
1490 mlx5e_chan_mtx_init(c);
1492 /* open transmit completion queue */
1493 err = mlx5e_open_tx_cqs(c, cparam);
1497 switch (priv->params.rx_cq_moderation_mode) {
1499 rx_moderation_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1502 if (MLX5_CAP_GEN(priv->mdev, cq_period_start_from_cqe))
1503 rx_moderation_mode = MLX5_CQ_PERIOD_MODE_START_FROM_CQE;
1505 rx_moderation_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1509 /* open receive completion queue */
1510 err = mlx5e_open_cq(c, &cparam->rx_cq, &c->rq.cq,
1511 &mlx5e_rx_cq_comp, rx_moderation_mode);
1513 goto err_close_tx_cqs;
1515 err = mlx5e_open_sqs(c, cparam);
1517 goto err_close_rx_cq;
1519 err = mlx5e_open_rq(c, &cparam->rq, &c->rq);
1523 /* store channel pointer */
1526 /* poll receive queue initially */
1527 c->rq.cq.mcq.comp(&c->rq.cq.mcq);
1533 mlx5e_close_sqs_wait(c);
1536 mlx5e_close_cq(&c->rq.cq);
1539 mlx5e_close_tx_cqs(c);
1542 /* destroy mutexes */
1543 mlx5e_chan_mtx_destroy(c);
1549 mlx5e_close_channel(struct mlx5e_channel *volatile *pp)
1551 struct mlx5e_channel *c = *pp;
1553 /* check if channel is already closed */
1556 mlx5e_close_rq(&c->rq);
1561 mlx5e_close_channel_wait(struct mlx5e_channel *volatile *pp)
1563 struct mlx5e_channel *c = *pp;
1565 /* check if channel is already closed */
1568 /* ensure channel pointer is no longer used */
1571 mlx5e_close_rq_wait(&c->rq);
1572 mlx5e_close_sqs_wait(c);
1573 mlx5e_close_cq(&c->rq.cq);
1574 mlx5e_close_tx_cqs(c);
1575 /* destroy mutexes */
1576 mlx5e_chan_mtx_destroy(c);
1581 mlx5e_build_rq_param(struct mlx5e_priv *priv,
1582 struct mlx5e_rq_param *param)
1584 void *rqc = param->rqc;
1585 void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1587 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1588 MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1589 MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1590 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_rq_size);
1591 MLX5_SET(wq, wq, pd, priv->pdn);
1593 param->wq.buf_numa_node = 0;
1594 param->wq.db_numa_node = 0;
1595 param->wq.linear = 1;
1599 mlx5e_build_sq_param(struct mlx5e_priv *priv,
1600 struct mlx5e_sq_param *param)
1602 void *sqc = param->sqc;
1603 void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1605 MLX5_SET(wq, wq, log_wq_sz, priv->params.log_sq_size);
1606 MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1607 MLX5_SET(wq, wq, pd, priv->pdn);
1609 param->wq.buf_numa_node = 0;
1610 param->wq.db_numa_node = 0;
1611 param->wq.linear = 1;
1615 mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1616 struct mlx5e_cq_param *param)
1618 void *cqc = param->cqc;
1620 MLX5_SET(cqc, cqc, uar_page, priv->cq_uar.index);
1624 mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
1625 struct mlx5e_cq_param *param)
1627 void *cqc = param->cqc;
1631 * TODO The sysctl to control on/off is a bool value for now, which means
1632 * we only support CSUM, once HASH is implemnted we'll need to address that.
1634 if (priv->params.cqe_zipping_en) {
1635 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
1636 MLX5_SET(cqc, cqc, cqe_compression_en, 1);
1639 MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_rq_size);
1640 MLX5_SET(cqc, cqc, cq_period, priv->params.rx_cq_moderation_usec);
1641 MLX5_SET(cqc, cqc, cq_max_count, priv->params.rx_cq_moderation_pkts);
1643 mlx5e_build_common_cq_param(priv, param);
1647 mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
1648 struct mlx5e_cq_param *param)
1650 void *cqc = param->cqc;
1652 MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_sq_size);
1653 MLX5_SET(cqc, cqc, cq_period, priv->params.tx_cq_moderation_usec);
1654 MLX5_SET(cqc, cqc, cq_max_count, priv->params.tx_cq_moderation_pkts);
1656 mlx5e_build_common_cq_param(priv, param);
1660 mlx5e_build_channel_param(struct mlx5e_priv *priv,
1661 struct mlx5e_channel_param *cparam)
1663 memset(cparam, 0, sizeof(*cparam));
1665 mlx5e_build_rq_param(priv, &cparam->rq);
1666 mlx5e_build_sq_param(priv, &cparam->sq);
1667 mlx5e_build_rx_cq_param(priv, &cparam->rx_cq);
1668 mlx5e_build_tx_cq_param(priv, &cparam->tx_cq);
1672 mlx5e_open_channels(struct mlx5e_priv *priv)
1674 struct mlx5e_channel_param cparam;
1680 priv->channel = malloc(priv->params.num_channels *
1681 sizeof(struct mlx5e_channel *), M_MLX5EN, M_WAITOK | M_ZERO);
1682 if (priv->channel == NULL)
1685 mlx5e_build_channel_param(priv, &cparam);
1686 for (i = 0; i < priv->params.num_channels; i++) {
1687 err = mlx5e_open_channel(priv, i, &cparam, &priv->channel[i]);
1689 goto err_close_channels;
1692 for (j = 0; j < priv->params.num_channels; j++) {
1693 err = mlx5e_wait_for_min_rx_wqes(&priv->channel[j]->rq);
1695 goto err_close_channels;
1701 for (i--; i >= 0; i--) {
1702 mlx5e_close_channel(&priv->channel[i]);
1703 mlx5e_close_channel_wait(&priv->channel[i]);
1706 /* remove "volatile" attribute from "channel" pointer */
1707 ptr = __DECONST(void *, priv->channel);
1708 priv->channel = NULL;
1710 free(ptr, M_MLX5EN);
1716 mlx5e_close_channels(struct mlx5e_priv *priv)
1721 if (priv->channel == NULL)
1724 for (i = 0; i < priv->params.num_channels; i++)
1725 mlx5e_close_channel(&priv->channel[i]);
1726 for (i = 0; i < priv->params.num_channels; i++)
1727 mlx5e_close_channel_wait(&priv->channel[i]);
1729 /* remove "volatile" attribute from "channel" pointer */
1730 ptr = __DECONST(void *, priv->channel);
1731 priv->channel = NULL;
1733 free(ptr, M_MLX5EN);
1737 mlx5e_refresh_sq_params(struct mlx5e_priv *priv, struct mlx5e_sq *sq)
1739 return (mlx5_core_modify_cq_moderation(priv->mdev, &sq->cq.mcq,
1740 priv->params.tx_cq_moderation_usec,
1741 priv->params.tx_cq_moderation_pkts));
1745 mlx5e_refresh_rq_params(struct mlx5e_priv *priv, struct mlx5e_rq *rq)
1747 return (mlx5_core_modify_cq_moderation(priv->mdev, &rq->cq.mcq,
1748 priv->params.rx_cq_moderation_usec,
1749 priv->params.rx_cq_moderation_pkts));
1753 mlx5e_refresh_channel_params_sub(struct mlx5e_priv *priv, struct mlx5e_channel *c)
1761 err = mlx5e_refresh_rq_params(priv, &c->rq);
1765 for (i = 0; i != c->num_tc; i++) {
1766 err = mlx5e_refresh_sq_params(priv, &c->sq[i]);
1775 mlx5e_refresh_channel_params(struct mlx5e_priv *priv)
1779 if (priv->channel == NULL)
1782 for (i = 0; i < priv->params.num_channels; i++) {
1785 err = mlx5e_refresh_channel_params_sub(priv, priv->channel[i]);
1793 mlx5e_open_tis(struct mlx5e_priv *priv, int tc)
1795 struct mlx5_core_dev *mdev = priv->mdev;
1796 u32 in[MLX5_ST_SZ_DW(create_tis_in)];
1797 void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1799 memset(in, 0, sizeof(in));
1801 MLX5_SET(tisc, tisc, prio, tc);
1802 MLX5_SET(tisc, tisc, transport_domain, priv->tdn);
1804 return (mlx5_core_create_tis(mdev, in, sizeof(in), &priv->tisn[tc]));
1808 mlx5e_close_tis(struct mlx5e_priv *priv, int tc)
1810 mlx5_core_destroy_tis(priv->mdev, priv->tisn[tc]);
1814 mlx5e_open_tises(struct mlx5e_priv *priv)
1816 int num_tc = priv->num_tc;
1820 for (tc = 0; tc < num_tc; tc++) {
1821 err = mlx5e_open_tis(priv, tc);
1823 goto err_close_tises;
1829 for (tc--; tc >= 0; tc--)
1830 mlx5e_close_tis(priv, tc);
1836 mlx5e_close_tises(struct mlx5e_priv *priv)
1838 int num_tc = priv->num_tc;
1841 for (tc = 0; tc < num_tc; tc++)
1842 mlx5e_close_tis(priv, tc);
1846 mlx5e_open_rqt(struct mlx5e_priv *priv)
1848 struct mlx5_core_dev *mdev = priv->mdev;
1850 u32 out[MLX5_ST_SZ_DW(create_rqt_out)];
1857 sz = 1 << priv->params.rx_hash_log_tbl_sz;
1859 inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
1860 in = mlx5_vzalloc(inlen);
1863 rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
1865 MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1866 MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
1868 for (i = 0; i < sz; i++) {
1871 ix = rss_get_indirection_to_bucket(i);
1875 /* ensure we don't overflow */
1876 ix %= priv->params.num_channels;
1877 MLX5_SET(rqtc, rqtc, rq_num[i], priv->channel[ix]->rq.rqn);
1880 MLX5_SET(create_rqt_in, in, opcode, MLX5_CMD_OP_CREATE_RQT);
1882 memset(out, 0, sizeof(out));
1883 err = mlx5_cmd_exec_check_status(mdev, in, inlen, out, sizeof(out));
1885 priv->rqtn = MLX5_GET(create_rqt_out, out, rqtn);
1893 mlx5e_close_rqt(struct mlx5e_priv *priv)
1895 u32 in[MLX5_ST_SZ_DW(destroy_rqt_in)];
1896 u32 out[MLX5_ST_SZ_DW(destroy_rqt_out)];
1898 memset(in, 0, sizeof(in));
1900 MLX5_SET(destroy_rqt_in, in, opcode, MLX5_CMD_OP_DESTROY_RQT);
1901 MLX5_SET(destroy_rqt_in, in, rqtn, priv->rqtn);
1903 mlx5_cmd_exec_check_status(priv->mdev, in, sizeof(in), out,
1908 mlx5e_build_tir_ctx(struct mlx5e_priv *priv, u32 * tirc, int tt)
1910 void *hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
1913 MLX5_SET(tirc, tirc, transport_domain, priv->tdn);
1915 #define ROUGH_MAX_L2_L3_HDR_SZ 256
1917 #define MLX5_HASH_IP (MLX5_HASH_FIELD_SEL_SRC_IP |\
1918 MLX5_HASH_FIELD_SEL_DST_IP)
1920 #define MLX5_HASH_ALL (MLX5_HASH_FIELD_SEL_SRC_IP |\
1921 MLX5_HASH_FIELD_SEL_DST_IP |\
1922 MLX5_HASH_FIELD_SEL_L4_SPORT |\
1923 MLX5_HASH_FIELD_SEL_L4_DPORT)
1925 #define MLX5_HASH_IP_IPSEC_SPI (MLX5_HASH_FIELD_SEL_SRC_IP |\
1926 MLX5_HASH_FIELD_SEL_DST_IP |\
1927 MLX5_HASH_FIELD_SEL_IPSEC_SPI)
1929 if (priv->params.hw_lro_en) {
1930 MLX5_SET(tirc, tirc, lro_enable_mask,
1931 MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
1932 MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
1933 MLX5_SET(tirc, tirc, lro_max_msg_sz,
1934 (priv->params.lro_wqe_sz -
1935 ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
1936 /* TODO: add the option to choose timer value dynamically */
1937 MLX5_SET(tirc, tirc, lro_timeout_period_usecs,
1938 MLX5_CAP_ETH(priv->mdev,
1939 lro_timer_supported_periods[2]));
1942 /* setup parameters for hashing TIR type, if any */
1945 MLX5_SET(tirc, tirc, disp_type,
1946 MLX5_TIRC_DISP_TYPE_DIRECT);
1947 MLX5_SET(tirc, tirc, inline_rqn,
1948 priv->channel[0]->rq.rqn);
1951 MLX5_SET(tirc, tirc, disp_type,
1952 MLX5_TIRC_DISP_TYPE_INDIRECT);
1953 MLX5_SET(tirc, tirc, indirect_table,
1955 MLX5_SET(tirc, tirc, rx_hash_fn,
1956 MLX5_TIRC_RX_HASH_FN_HASH_TOEPLITZ);
1957 hkey = (__be32 *) MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
1960 * The FreeBSD RSS implementation does currently not
1961 * support symmetric Toeplitz hashes:
1963 MLX5_SET(tirc, tirc, rx_hash_symmetric, 0);
1964 rss_getkey((uint8_t *)hkey);
1966 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1967 hkey[0] = cpu_to_be32(0xD181C62C);
1968 hkey[1] = cpu_to_be32(0xF7F4DB5B);
1969 hkey[2] = cpu_to_be32(0x1983A2FC);
1970 hkey[3] = cpu_to_be32(0x943E1ADB);
1971 hkey[4] = cpu_to_be32(0xD9389E6B);
1972 hkey[5] = cpu_to_be32(0xD1039C2C);
1973 hkey[6] = cpu_to_be32(0xA74499AD);
1974 hkey[7] = cpu_to_be32(0x593D56D9);
1975 hkey[8] = cpu_to_be32(0xF3253C06);
1976 hkey[9] = cpu_to_be32(0x2ADC1FFC);
1982 case MLX5E_TT_IPV4_TCP:
1983 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1984 MLX5_L3_PROT_TYPE_IPV4);
1985 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
1986 MLX5_L4_PROT_TYPE_TCP);
1988 if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4)) {
1989 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1993 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
1997 case MLX5E_TT_IPV6_TCP:
1998 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
1999 MLX5_L3_PROT_TYPE_IPV6);
2000 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2001 MLX5_L4_PROT_TYPE_TCP);
2003 if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6)) {
2004 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2008 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2012 case MLX5E_TT_IPV4_UDP:
2013 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2014 MLX5_L3_PROT_TYPE_IPV4);
2015 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2016 MLX5_L4_PROT_TYPE_UDP);
2018 if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4)) {
2019 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2023 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2027 case MLX5E_TT_IPV6_UDP:
2028 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2029 MLX5_L3_PROT_TYPE_IPV6);
2030 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2031 MLX5_L4_PROT_TYPE_UDP);
2033 if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6)) {
2034 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2038 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2042 case MLX5E_TT_IPV4_IPSEC_AH:
2043 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2044 MLX5_L3_PROT_TYPE_IPV4);
2045 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2046 MLX5_HASH_IP_IPSEC_SPI);
2049 case MLX5E_TT_IPV6_IPSEC_AH:
2050 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2051 MLX5_L3_PROT_TYPE_IPV6);
2052 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2053 MLX5_HASH_IP_IPSEC_SPI);
2056 case MLX5E_TT_IPV4_IPSEC_ESP:
2057 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2058 MLX5_L3_PROT_TYPE_IPV4);
2059 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2060 MLX5_HASH_IP_IPSEC_SPI);
2063 case MLX5E_TT_IPV6_IPSEC_ESP:
2064 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2065 MLX5_L3_PROT_TYPE_IPV6);
2066 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2067 MLX5_HASH_IP_IPSEC_SPI);
2071 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2072 MLX5_L3_PROT_TYPE_IPV4);
2073 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2078 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2079 MLX5_L3_PROT_TYPE_IPV6);
2080 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2090 mlx5e_open_tir(struct mlx5e_priv *priv, int tt)
2092 struct mlx5_core_dev *mdev = priv->mdev;
2098 inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2099 in = mlx5_vzalloc(inlen);
2102 tirc = MLX5_ADDR_OF(create_tir_in, in, tir_context);
2104 mlx5e_build_tir_ctx(priv, tirc, tt);
2106 err = mlx5_core_create_tir(mdev, in, inlen, &priv->tirn[tt]);
2114 mlx5e_close_tir(struct mlx5e_priv *priv, int tt)
2116 mlx5_core_destroy_tir(priv->mdev, priv->tirn[tt]);
2120 mlx5e_open_tirs(struct mlx5e_priv *priv)
2125 for (i = 0; i < MLX5E_NUM_TT; i++) {
2126 err = mlx5e_open_tir(priv, i);
2128 goto err_close_tirs;
2134 for (i--; i >= 0; i--)
2135 mlx5e_close_tir(priv, i);
2141 mlx5e_close_tirs(struct mlx5e_priv *priv)
2145 for (i = 0; i < MLX5E_NUM_TT; i++)
2146 mlx5e_close_tir(priv, i);
2150 * SW MTU does not include headers,
2151 * HW MTU includes all headers and checksums.
2154 mlx5e_set_dev_port_mtu(struct ifnet *ifp, int sw_mtu)
2156 struct mlx5e_priv *priv = ifp->if_softc;
2157 struct mlx5_core_dev *mdev = priv->mdev;
2162 err = mlx5_set_port_mtu(mdev, MLX5E_SW2HW_MTU(sw_mtu));
2164 if_printf(ifp, "%s: mlx5_set_port_mtu failed setting %d, err=%d\n",
2165 __func__, sw_mtu, err);
2168 err = mlx5_query_port_oper_mtu(mdev, &hw_mtu);
2170 ifp->if_mtu = MLX5E_HW2SW_MTU(hw_mtu);
2172 if (ifp->if_mtu != sw_mtu) {
2173 if_printf(ifp, "Port MTU %d is different than "
2174 "ifp mtu %d\n", sw_mtu, (int)ifp->if_mtu);
2177 if_printf(ifp, "Query port MTU, after setting new "
2178 "MTU value, failed\n");
2179 ifp->if_mtu = sw_mtu;
2185 mlx5e_open_locked(struct ifnet *ifp)
2187 struct mlx5e_priv *priv = ifp->if_softc;
2190 /* check if already opened */
2191 if (test_bit(MLX5E_STATE_OPENED, &priv->state) != 0)
2195 if (rss_getnumbuckets() > priv->params.num_channels) {
2196 if_printf(ifp, "NOTE: There are more RSS buckets(%u) than "
2197 "channels(%u) available\n", rss_getnumbuckets(),
2198 priv->params.num_channels);
2201 err = mlx5e_open_tises(priv);
2203 if_printf(ifp, "%s: mlx5e_open_tises failed, %d\n",
2207 err = mlx5_vport_alloc_q_counter(priv->mdev, &priv->counter_set_id);
2209 if_printf(priv->ifp,
2210 "%s: mlx5_vport_alloc_q_counter failed: %d\n",
2212 goto err_close_tises;
2214 err = mlx5e_open_channels(priv);
2216 if_printf(ifp, "%s: mlx5e_open_channels failed, %d\n",
2218 goto err_dalloc_q_counter;
2220 err = mlx5e_open_rqt(priv);
2222 if_printf(ifp, "%s: mlx5e_open_rqt failed, %d\n",
2224 goto err_close_channels;
2226 err = mlx5e_open_tirs(priv);
2228 if_printf(ifp, "%s: mlx5e_open_tir failed, %d\n",
2230 goto err_close_rqls;
2232 err = mlx5e_open_flow_table(priv);
2234 if_printf(ifp, "%s: mlx5e_open_flow_table failed, %d\n",
2236 goto err_close_tirs;
2238 err = mlx5e_add_all_vlan_rules(priv);
2240 if_printf(ifp, "%s: mlx5e_add_all_vlan_rules failed, %d\n",
2242 goto err_close_flow_table;
2244 set_bit(MLX5E_STATE_OPENED, &priv->state);
2246 mlx5e_update_carrier(priv);
2247 mlx5e_set_rx_mode_core(priv);
2251 err_close_flow_table:
2252 mlx5e_close_flow_table(priv);
2255 mlx5e_close_tirs(priv);
2258 mlx5e_close_rqt(priv);
2261 mlx5e_close_channels(priv);
2263 err_dalloc_q_counter:
2264 mlx5_vport_dealloc_q_counter(priv->mdev, priv->counter_set_id);
2267 mlx5e_close_tises(priv);
2273 mlx5e_open(void *arg)
2275 struct mlx5e_priv *priv = arg;
2278 if (mlx5_set_port_status(priv->mdev, MLX5_PORT_UP))
2279 if_printf(priv->ifp,
2280 "%s: Setting port status to up failed\n",
2283 mlx5e_open_locked(priv->ifp);
2284 priv->ifp->if_drv_flags |= IFF_DRV_RUNNING;
2289 mlx5e_close_locked(struct ifnet *ifp)
2291 struct mlx5e_priv *priv = ifp->if_softc;
2293 /* check if already closed */
2294 if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
2297 clear_bit(MLX5E_STATE_OPENED, &priv->state);
2299 mlx5e_set_rx_mode_core(priv);
2300 mlx5e_del_all_vlan_rules(priv);
2301 if_link_state_change(priv->ifp, LINK_STATE_DOWN);
2302 mlx5e_close_flow_table(priv);
2303 mlx5e_close_tirs(priv);
2304 mlx5e_close_rqt(priv);
2305 mlx5e_close_channels(priv);
2306 mlx5_vport_dealloc_q_counter(priv->mdev, priv->counter_set_id);
2307 mlx5e_close_tises(priv);
2312 #if (__FreeBSD_version >= 1100000)
2314 mlx5e_get_counter(struct ifnet *ifp, ift_counter cnt)
2316 struct mlx5e_priv *priv = ifp->if_softc;
2319 /* PRIV_LOCK(priv); XXX not allowed */
2321 case IFCOUNTER_IPACKETS:
2322 retval = priv->stats.vport.rx_packets;
2324 case IFCOUNTER_IERRORS:
2325 retval = priv->stats.vport.rx_error_packets;
2327 case IFCOUNTER_IQDROPS:
2328 retval = priv->stats.vport.rx_out_of_buffer;
2330 case IFCOUNTER_OPACKETS:
2331 retval = priv->stats.vport.tx_packets;
2333 case IFCOUNTER_OERRORS:
2334 retval = priv->stats.vport.tx_error_packets;
2336 case IFCOUNTER_IBYTES:
2337 retval = priv->stats.vport.rx_bytes;
2339 case IFCOUNTER_OBYTES:
2340 retval = priv->stats.vport.tx_bytes;
2342 case IFCOUNTER_IMCASTS:
2343 retval = priv->stats.vport.rx_multicast_packets;
2345 case IFCOUNTER_OMCASTS:
2346 retval = priv->stats.vport.tx_multicast_packets;
2348 case IFCOUNTER_OQDROPS:
2349 retval = priv->stats.vport.tx_queue_dropped;
2352 retval = if_get_counter_default(ifp, cnt);
2355 /* PRIV_UNLOCK(priv); XXX not allowed */
2361 mlx5e_set_rx_mode(struct ifnet *ifp)
2363 struct mlx5e_priv *priv = ifp->if_softc;
2365 schedule_work(&priv->set_rx_mode_work);
2369 mlx5e_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2371 struct mlx5e_priv *priv;
2373 struct ifi2creq i2c;
2381 priv = ifp->if_softc;
2383 /* check if detaching */
2384 if (priv == NULL || priv->gone != 0)
2389 ifr = (struct ifreq *)data;
2392 mlx5_query_port_max_mtu(priv->mdev, &max_mtu);
2394 if (ifr->ifr_mtu >= MLX5E_MTU_MIN &&
2395 ifr->ifr_mtu <= MIN(MLX5E_MTU_MAX, max_mtu)) {
2398 was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2400 mlx5e_close_locked(ifp);
2403 mlx5e_set_dev_port_mtu(ifp, ifr->ifr_mtu);
2406 mlx5e_open_locked(ifp);
2409 if_printf(ifp, "Invalid MTU value. Min val: %d, Max val: %d\n",
2410 MLX5E_MTU_MIN, MIN(MLX5E_MTU_MAX, max_mtu));
2415 if ((ifp->if_flags & IFF_UP) &&
2416 (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2417 mlx5e_set_rx_mode(ifp);
2421 if (ifp->if_flags & IFF_UP) {
2422 if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2423 if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
2424 mlx5e_open_locked(ifp);
2425 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2426 mlx5_set_port_status(priv->mdev, MLX5_PORT_UP);
2429 if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2430 mlx5_set_port_status(priv->mdev,
2432 if (test_bit(MLX5E_STATE_OPENED, &priv->state) != 0)
2433 mlx5e_close_locked(ifp);
2434 mlx5e_update_carrier(priv);
2435 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2442 mlx5e_set_rx_mode(ifp);
2447 ifr = (struct ifreq *)data;
2448 error = ifmedia_ioctl(ifp, ifr, &priv->media, command);
2451 ifr = (struct ifreq *)data;
2453 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2455 if (mask & IFCAP_TXCSUM) {
2456 ifp->if_capenable ^= IFCAP_TXCSUM;
2457 ifp->if_hwassist ^= (CSUM_TCP | CSUM_UDP | CSUM_IP);
2459 if (IFCAP_TSO4 & ifp->if_capenable &&
2460 !(IFCAP_TXCSUM & ifp->if_capenable)) {
2461 ifp->if_capenable &= ~IFCAP_TSO4;
2462 ifp->if_hwassist &= ~CSUM_IP_TSO;
2464 "tso4 disabled due to -txcsum.\n");
2467 if (mask & IFCAP_TXCSUM_IPV6) {
2468 ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
2469 ifp->if_hwassist ^= (CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2471 if (IFCAP_TSO6 & ifp->if_capenable &&
2472 !(IFCAP_TXCSUM_IPV6 & ifp->if_capenable)) {
2473 ifp->if_capenable &= ~IFCAP_TSO6;
2474 ifp->if_hwassist &= ~CSUM_IP6_TSO;
2476 "tso6 disabled due to -txcsum6.\n");
2479 if (mask & IFCAP_RXCSUM)
2480 ifp->if_capenable ^= IFCAP_RXCSUM;
2481 if (mask & IFCAP_RXCSUM_IPV6)
2482 ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
2483 if (mask & IFCAP_TSO4) {
2484 if (!(IFCAP_TSO4 & ifp->if_capenable) &&
2485 !(IFCAP_TXCSUM & ifp->if_capenable)) {
2486 if_printf(ifp, "enable txcsum first.\n");
2490 ifp->if_capenable ^= IFCAP_TSO4;
2491 ifp->if_hwassist ^= CSUM_IP_TSO;
2493 if (mask & IFCAP_TSO6) {
2494 if (!(IFCAP_TSO6 & ifp->if_capenable) &&
2495 !(IFCAP_TXCSUM_IPV6 & ifp->if_capenable)) {
2496 if_printf(ifp, "enable txcsum6 first.\n");
2500 ifp->if_capenable ^= IFCAP_TSO6;
2501 ifp->if_hwassist ^= CSUM_IP6_TSO;
2503 if (mask & IFCAP_VLAN_HWFILTER) {
2504 if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2505 mlx5e_disable_vlan_filter(priv);
2507 mlx5e_enable_vlan_filter(priv);
2509 ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
2511 if (mask & IFCAP_VLAN_HWTAGGING)
2512 ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2513 if (mask & IFCAP_WOL_MAGIC)
2514 ifp->if_capenable ^= IFCAP_WOL_MAGIC;
2516 VLAN_CAPABILITIES(ifp);
2517 /* turn off LRO means also turn of HW LRO - if it's on */
2518 if (mask & IFCAP_LRO) {
2519 int was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2520 bool need_restart = false;
2522 ifp->if_capenable ^= IFCAP_LRO;
2523 if (!(ifp->if_capenable & IFCAP_LRO)) {
2524 if (priv->params.hw_lro_en) {
2525 priv->params.hw_lro_en = false;
2526 need_restart = true;
2527 /* Not sure this is the correct way */
2528 priv->params_ethtool.hw_lro = priv->params.hw_lro_en;
2531 if (was_opened && need_restart) {
2532 mlx5e_close_locked(ifp);
2533 mlx5e_open_locked(ifp);
2541 ifr = (struct ifreq *)data;
2544 * Copy from the user-space address ifr_data to the
2545 * kernel-space address i2c
2547 error = copyin(ifr->ifr_data, &i2c, sizeof(i2c));
2551 if (i2c.len > sizeof(i2c.data)) {
2557 /* Get module_num which is required for the query_eeprom */
2558 error = mlx5_query_module_num(priv->mdev, &module_num);
2560 if_printf(ifp, "Query module num failed, eeprom "
2561 "reading is not supported\n");
2566 * Currently 0XA0 and 0xA2 are the only addresses permitted.
2567 * The internal conversion is as follows:
2569 if (i2c.dev_addr == 0xA0)
2570 read_addr = MLX5E_I2C_ADDR_LOW;
2571 else if (i2c.dev_addr == 0xA2)
2572 read_addr = MLX5E_I2C_ADDR_HIGH;
2574 if_printf(ifp, "Query eeprom failed, "
2575 "Invalid Address: %X\n", i2c.dev_addr);
2579 error = mlx5_query_eeprom(priv->mdev,
2580 read_addr, MLX5E_EEPROM_LOW_PAGE,
2581 (uint32_t)i2c.offset, (uint32_t)i2c.len, module_num,
2582 (uint32_t *)i2c.data, &size_read);
2584 if_printf(ifp, "Query eeprom failed, eeprom "
2585 "reading is not supported\n");
2589 if (i2c.len > MLX5_EEPROM_MAX_BYTES) {
2590 error = mlx5_query_eeprom(priv->mdev,
2591 read_addr, MLX5E_EEPROM_LOW_PAGE,
2592 (uint32_t)(i2c.offset + size_read),
2593 (uint32_t)(i2c.len - size_read), module_num,
2594 (uint32_t *)(i2c.data + size_read), &size_read);
2597 if_printf(ifp, "Query eeprom failed, eeprom "
2598 "reading is not supported\n");
2602 error = copyout(&i2c, ifr->ifr_data, sizeof(i2c));
2608 error = ether_ioctl(ifp, command, data);
2615 mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
2618 * TODO: uncoment once FW really sets all these bits if
2619 * (!mdev->caps.eth.rss_ind_tbl_cap || !mdev->caps.eth.csum_cap ||
2620 * !mdev->caps.eth.max_lso_cap || !mdev->caps.eth.vlan_cap ||
2621 * !(mdev->caps.gen.flags & MLX5_DEV_CAP_FLAG_SCQE_BRK_MOD)) return
2625 /* TODO: add more must-to-have features */
2631 mlx5e_build_ifp_priv(struct mlx5_core_dev *mdev,
2632 struct mlx5e_priv *priv,
2633 int num_comp_vectors)
2636 * TODO: Consider link speed for setting "log_sq_size",
2637 * "log_rq_size" and "cq_moderation_xxx":
2639 priv->params.log_sq_size =
2640 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
2641 priv->params.log_rq_size =
2642 MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
2643 priv->params.rx_cq_moderation_usec =
2644 MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
2645 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE :
2646 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
2647 priv->params.rx_cq_moderation_mode =
2648 MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ? 1 : 0;
2649 priv->params.rx_cq_moderation_pkts =
2650 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
2651 priv->params.tx_cq_moderation_usec =
2652 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
2653 priv->params.tx_cq_moderation_pkts =
2654 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
2655 priv->params.min_rx_wqes =
2656 MLX5E_PARAMS_DEFAULT_MIN_RX_WQES;
2657 priv->params.rx_hash_log_tbl_sz =
2658 (order_base_2(num_comp_vectors) >
2659 MLX5E_PARAMS_DEFAULT_RX_HASH_LOG_TBL_SZ) ?
2660 order_base_2(num_comp_vectors) :
2661 MLX5E_PARAMS_DEFAULT_RX_HASH_LOG_TBL_SZ;
2662 priv->params.num_tc = 1;
2663 priv->params.default_vlan_prio = 0;
2664 priv->counter_set_id = -1;
2667 * hw lro is currently defaulted to off. when it won't anymore we
2668 * will consider the HW capability: "!!MLX5_CAP_ETH(mdev, lro_cap)"
2670 priv->params.hw_lro_en = false;
2671 priv->params.lro_wqe_sz = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
2673 priv->params.cqe_zipping_en = !!MLX5_CAP_GEN(mdev, cqe_compression);
2676 priv->params.num_channels = num_comp_vectors;
2677 priv->order_base_2_num_channels = order_base_2(num_comp_vectors);
2678 priv->queue_mapping_channel_mask =
2679 roundup_pow_of_two(num_comp_vectors) - 1;
2680 priv->num_tc = priv->params.num_tc;
2681 priv->default_vlan_prio = priv->params.default_vlan_prio;
2683 INIT_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
2684 INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
2685 INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
2689 mlx5e_create_mkey(struct mlx5e_priv *priv, u32 pdn,
2690 struct mlx5_core_mr *mr)
2692 struct ifnet *ifp = priv->ifp;
2693 struct mlx5_core_dev *mdev = priv->mdev;
2694 struct mlx5_create_mkey_mbox_in *in;
2697 in = mlx5_vzalloc(sizeof(*in));
2699 if_printf(ifp, "%s: failed to allocate inbox\n", __func__);
2702 in->seg.flags = MLX5_PERM_LOCAL_WRITE |
2703 MLX5_PERM_LOCAL_READ |
2704 MLX5_ACCESS_MODE_PA;
2705 in->seg.flags_pd = cpu_to_be32(pdn | MLX5_MKEY_LEN64);
2706 in->seg.qpn_mkey7_0 = cpu_to_be32(0xffffff << 8);
2708 err = mlx5_core_create_mkey(mdev, mr, in, sizeof(*in), NULL, NULL,
2711 if_printf(ifp, "%s: mlx5_core_create_mkey failed, %d\n",
2719 static const char *mlx5e_vport_stats_desc[] = {
2720 MLX5E_VPORT_STATS(MLX5E_STATS_DESC)
2723 static const char *mlx5e_pport_stats_desc[] = {
2724 MLX5E_PPORT_STATS(MLX5E_STATS_DESC)
2728 mlx5e_priv_mtx_init(struct mlx5e_priv *priv)
2730 mtx_init(&priv->async_events_mtx, "mlx5async", MTX_NETWORK_LOCK, MTX_DEF);
2731 sx_init(&priv->state_lock, "mlx5state");
2732 callout_init_mtx(&priv->watchdog, &priv->async_events_mtx, 0);
2736 mlx5e_priv_mtx_destroy(struct mlx5e_priv *priv)
2738 mtx_destroy(&priv->async_events_mtx);
2739 sx_destroy(&priv->state_lock);
2743 sysctl_firmware(SYSCTL_HANDLER_ARGS)
2746 * %d.%d%.d the string format.
2747 * fw_rev_{maj,min,sub} return u16, 2^16 = 65536.
2748 * We need at most 5 chars to store that.
2749 * It also has: two "." and NULL at the end, which means we need 18
2750 * (5*3 + 3) chars at most.
2753 struct mlx5e_priv *priv = arg1;
2756 snprintf(fw, sizeof(fw), "%d.%d.%d", fw_rev_maj(priv->mdev), fw_rev_min(priv->mdev),
2757 fw_rev_sub(priv->mdev));
2758 error = sysctl_handle_string(oidp, fw, sizeof(fw), req);
2763 mlx5e_add_hw_stats(struct mlx5e_priv *priv)
2765 SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_hw),
2766 OID_AUTO, "fw_version", CTLTYPE_STRING | CTLFLAG_RD, priv, 0,
2767 sysctl_firmware, "A", "HCA firmware version");
2769 SYSCTL_ADD_STRING(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_hw),
2770 OID_AUTO, "board_id", CTLFLAG_RD, priv->mdev->board_id, 0,
2775 mlx5e_setup_pauseframes(struct mlx5e_priv *priv)
2777 #if (__FreeBSD_version < 1100000)
2781 /* Only receiving pauseframes is enabled by default */
2782 priv->params.tx_pauseframe_control = 0;
2783 priv->params.rx_pauseframe_control = 1;
2785 #if (__FreeBSD_version < 1100000)
2786 /* compute path for sysctl */
2787 snprintf(path, sizeof(path), "dev.mce.%d.tx_pauseframe_control",
2788 device_get_unit(priv->mdev->pdev->dev.bsddev));
2790 /* try to fetch tunable, if any */
2791 TUNABLE_INT_FETCH(path, &priv->params.tx_pauseframe_control);
2793 /* compute path for sysctl */
2794 snprintf(path, sizeof(path), "dev.mce.%d.rx_pauseframe_control",
2795 device_get_unit(priv->mdev->pdev->dev.bsddev));
2797 /* try to fetch tunable, if any */
2798 TUNABLE_INT_FETCH(path, &priv->params.rx_pauseframe_control);
2801 /* register pausframe SYSCTLs */
2802 SYSCTL_ADD_INT(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
2803 OID_AUTO, "tx_pauseframe_control", CTLFLAG_RDTUN,
2804 &priv->params.tx_pauseframe_control, 0,
2805 "Set to enable TX pause frames. Clear to disable.");
2807 SYSCTL_ADD_INT(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
2808 OID_AUTO, "rx_pauseframe_control", CTLFLAG_RDTUN,
2809 &priv->params.rx_pauseframe_control, 0,
2810 "Set to enable RX pause frames. Clear to disable.");
2813 priv->params.tx_pauseframe_control =
2814 priv->params.tx_pauseframe_control ? 1 : 0;
2815 priv->params.rx_pauseframe_control =
2816 priv->params.rx_pauseframe_control ? 1 : 0;
2818 /* update firmware */
2819 mlx5_set_port_pause(priv->mdev, 1,
2820 priv->params.rx_pauseframe_control,
2821 priv->params.tx_pauseframe_control);
2825 mlx5e_create_ifp(struct mlx5_core_dev *mdev)
2827 static volatile int mlx5_en_unit;
2829 struct mlx5e_priv *priv;
2830 u8 dev_addr[ETHER_ADDR_LEN] __aligned(4);
2831 struct sysctl_oid_list *child;
2832 int ncv = mdev->priv.eq_table.num_comp_vectors;
2838 if (mlx5e_check_required_hca_cap(mdev)) {
2839 mlx5_core_dbg(mdev, "mlx5e_check_required_hca_cap() failed\n");
2842 priv = malloc(sizeof(*priv), M_MLX5EN, M_WAITOK | M_ZERO);
2844 mlx5_core_err(mdev, "malloc() failed\n");
2847 mlx5e_priv_mtx_init(priv);
2849 ifp = priv->ifp = if_alloc(IFT_ETHER);
2851 mlx5_core_err(mdev, "if_alloc() failed\n");
2854 ifp->if_softc = priv;
2855 if_initname(ifp, "mce", atomic_fetchadd_int(&mlx5_en_unit, 1));
2856 ifp->if_mtu = ETHERMTU;
2857 ifp->if_init = mlx5e_open;
2858 ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
2859 ifp->if_ioctl = mlx5e_ioctl;
2860 ifp->if_transmit = mlx5e_xmit;
2861 ifp->if_qflush = if_qflush;
2862 #if (__FreeBSD_version >= 1100000)
2863 ifp->if_get_counter = mlx5e_get_counter;
2865 ifp->if_snd.ifq_maxlen = ifqmaxlen;
2867 * Set driver features
2869 ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6;
2870 ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
2871 ifp->if_capabilities |= IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWFILTER;
2872 ifp->if_capabilities |= IFCAP_LINKSTATE | IFCAP_JUMBO_MTU;
2873 ifp->if_capabilities |= IFCAP_LRO;
2874 ifp->if_capabilities |= IFCAP_TSO | IFCAP_VLAN_HWTSO;
2876 /* set TSO limits so that we don't have to drop TX packets */
2877 ifp->if_hw_tsomax = MLX5E_MAX_TX_PAYLOAD_SIZE - (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
2878 ifp->if_hw_tsomaxsegcount = MLX5E_MAX_TX_MBUF_FRAGS - 1 /* hdr */;
2879 ifp->if_hw_tsomaxsegsize = MLX5E_MAX_TX_MBUF_SIZE;
2881 ifp->if_capenable = ifp->if_capabilities;
2882 ifp->if_hwassist = 0;
2883 if (ifp->if_capenable & IFCAP_TSO)
2884 ifp->if_hwassist |= CSUM_TSO;
2885 if (ifp->if_capenable & IFCAP_TXCSUM)
2886 ifp->if_hwassist |= (CSUM_TCP | CSUM_UDP | CSUM_IP);
2887 if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
2888 ifp->if_hwassist |= (CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2890 /* ifnet sysctl tree */
2891 sysctl_ctx_init(&priv->sysctl_ctx);
2892 priv->sysctl_ifnet = SYSCTL_ADD_NODE(&priv->sysctl_ctx, SYSCTL_STATIC_CHILDREN(_dev),
2893 OID_AUTO, ifp->if_dname, CTLFLAG_RD, 0, "MLX5 ethernet - interface name");
2894 if (priv->sysctl_ifnet == NULL) {
2895 mlx5_core_err(mdev, "SYSCTL_ADD_NODE() failed\n");
2896 goto err_free_sysctl;
2898 snprintf(unit, sizeof(unit), "%d", ifp->if_dunit);
2899 priv->sysctl_ifnet = SYSCTL_ADD_NODE(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
2900 OID_AUTO, unit, CTLFLAG_RD, 0, "MLX5 ethernet - interface unit");
2901 if (priv->sysctl_ifnet == NULL) {
2902 mlx5_core_err(mdev, "SYSCTL_ADD_NODE() failed\n");
2903 goto err_free_sysctl;
2906 /* HW sysctl tree */
2907 child = SYSCTL_CHILDREN(device_get_sysctl_tree(mdev->pdev->dev.bsddev));
2908 priv->sysctl_hw = SYSCTL_ADD_NODE(&priv->sysctl_ctx, child,
2909 OID_AUTO, "hw", CTLFLAG_RD, 0, "MLX5 ethernet dev hw");
2910 if (priv->sysctl_hw == NULL) {
2911 mlx5_core_err(mdev, "SYSCTL_ADD_NODE() failed\n");
2912 goto err_free_sysctl;
2914 mlx5e_build_ifp_priv(mdev, priv, ncv);
2915 err = mlx5_alloc_map_uar(mdev, &priv->cq_uar);
2917 if_printf(ifp, "%s: mlx5_alloc_map_uar failed, %d\n",
2919 goto err_free_sysctl;
2921 err = mlx5_core_alloc_pd(mdev, &priv->pdn);
2923 if_printf(ifp, "%s: mlx5_core_alloc_pd failed, %d\n",
2925 goto err_unmap_free_uar;
2927 err = mlx5_alloc_transport_domain(mdev, &priv->tdn);
2929 if_printf(ifp, "%s: mlx5_alloc_transport_domain failed, %d\n",
2931 goto err_dealloc_pd;
2933 err = mlx5e_create_mkey(priv, priv->pdn, &priv->mr);
2935 if_printf(ifp, "%s: mlx5e_create_mkey failed, %d\n",
2937 goto err_dealloc_transport_domain;
2939 mlx5_query_nic_vport_mac_address(priv->mdev, 0, dev_addr);
2941 /* set default MTU */
2942 mlx5e_set_dev_port_mtu(ifp, ifp->if_mtu);
2945 device_set_desc(mdev->pdev->dev.bsddev, mlx5e_version);
2947 /* Set default media status */
2948 priv->media_status_last = IFM_AVALID;
2949 priv->media_active_last = IFM_ETHER | IFM_AUTO |
2950 IFM_ETH_RXPAUSE | IFM_FDX;
2952 /* setup default pauseframes configuration */
2953 mlx5e_setup_pauseframes(priv);
2955 err = mlx5_query_port_proto_cap(mdev, ð_proto_cap, MLX5_PTYS_EN);
2958 if_printf(ifp, "%s: Query port media capability failed, %d\n",
2962 /* Setup supported medias */
2963 ifmedia_init(&priv->media, IFM_IMASK | IFM_ETH_FMASK,
2964 mlx5e_media_change, mlx5e_media_status);
2966 for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
2967 if (mlx5e_mode_table[i].baudrate == 0)
2969 if (MLX5E_PROT_MASK(i) & eth_proto_cap) {
2970 ifmedia_add(&priv->media,
2971 mlx5e_mode_table[i].subtype |
2972 IFM_ETHER, 0, NULL);
2973 ifmedia_add(&priv->media,
2974 mlx5e_mode_table[i].subtype |
2975 IFM_ETHER | IFM_FDX |
2976 IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE, 0, NULL);
2980 ifmedia_add(&priv->media, IFM_ETHER | IFM_AUTO, 0, NULL);
2981 ifmedia_add(&priv->media, IFM_ETHER | IFM_AUTO | IFM_FDX |
2982 IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE, 0, NULL);
2984 /* Set autoselect by default */
2985 ifmedia_set(&priv->media, IFM_ETHER | IFM_AUTO | IFM_FDX |
2986 IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE);
2987 ether_ifattach(ifp, dev_addr);
2989 /* Register for VLAN events */
2990 priv->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
2991 mlx5e_vlan_rx_add_vid, priv, EVENTHANDLER_PRI_FIRST);
2992 priv->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
2993 mlx5e_vlan_rx_kill_vid, priv, EVENTHANDLER_PRI_FIRST);
2995 /* Link is down by default */
2996 if_link_state_change(ifp, LINK_STATE_DOWN);
2998 mlx5e_enable_async_events(priv);
3000 mlx5e_add_hw_stats(priv);
3002 mlx5e_create_stats(&priv->stats.vport.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3003 "vstats", mlx5e_vport_stats_desc, MLX5E_VPORT_STATS_NUM,
3004 priv->stats.vport.arg);
3006 mlx5e_create_stats(&priv->stats.pport.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3007 "pstats", mlx5e_pport_stats_desc, MLX5E_PPORT_STATS_NUM,
3008 priv->stats.pport.arg);
3010 mlx5e_create_ethtool(priv);
3012 mtx_lock(&priv->async_events_mtx);
3013 mlx5e_update_stats(priv);
3014 mtx_unlock(&priv->async_events_mtx);
3018 err_dealloc_transport_domain:
3019 mlx5_dealloc_transport_domain(mdev, priv->tdn);
3022 mlx5_core_dealloc_pd(mdev, priv->pdn);
3025 mlx5_unmap_free_uar(mdev, &priv->cq_uar);
3028 sysctl_ctx_free(&priv->sysctl_ctx);
3033 mlx5e_priv_mtx_destroy(priv);
3034 free(priv, M_MLX5EN);
3039 mlx5e_destroy_ifp(struct mlx5_core_dev *mdev, void *vpriv)
3041 struct mlx5e_priv *priv = vpriv;
3042 struct ifnet *ifp = priv->ifp;
3044 /* don't allow more IOCTLs */
3047 /* XXX wait a bit to allow IOCTL handlers to complete */
3050 /* stop watchdog timer */
3051 callout_drain(&priv->watchdog);
3053 if (priv->vlan_attach != NULL)
3054 EVENTHANDLER_DEREGISTER(vlan_config, priv->vlan_attach);
3055 if (priv->vlan_detach != NULL)
3056 EVENTHANDLER_DEREGISTER(vlan_unconfig, priv->vlan_detach);
3058 /* make sure device gets closed */
3060 mlx5e_close_locked(ifp);
3063 /* unregister device */
3064 ifmedia_removeall(&priv->media);
3065 ether_ifdetach(ifp);
3068 /* destroy all remaining sysctl nodes */
3069 if (priv->sysctl_debug)
3070 sysctl_ctx_free(&priv->stats.port_stats_debug.ctx);
3071 sysctl_ctx_free(&priv->stats.vport.ctx);
3072 sysctl_ctx_free(&priv->stats.pport.ctx);
3073 sysctl_ctx_free(&priv->sysctl_ctx);
3075 mlx5_core_destroy_mkey(priv->mdev, &priv->mr);
3076 mlx5_dealloc_transport_domain(priv->mdev, priv->tdn);
3077 mlx5_core_dealloc_pd(priv->mdev, priv->pdn);
3078 mlx5_unmap_free_uar(priv->mdev, &priv->cq_uar);
3079 mlx5e_disable_async_events(priv);
3080 flush_scheduled_work();
3081 mlx5e_priv_mtx_destroy(priv);
3082 free(priv, M_MLX5EN);
3086 mlx5e_get_ifp(void *vpriv)
3088 struct mlx5e_priv *priv = vpriv;
3093 static struct mlx5_interface mlx5e_interface = {
3094 .add = mlx5e_create_ifp,
3095 .remove = mlx5e_destroy_ifp,
3096 .event = mlx5e_async_event,
3097 .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
3098 .get_dev = mlx5e_get_ifp,
3104 mlx5_register_interface(&mlx5e_interface);
3110 mlx5_unregister_interface(&mlx5e_interface);
3113 module_init_order(mlx5e_init, SI_ORDER_THIRD);
3114 module_exit_order(mlx5e_cleanup, SI_ORDER_THIRD);
3116 #if (__FreeBSD_version >= 1100000)
3117 MODULE_DEPEND(mlx5en, linuxkpi, 1, 1, 1);
3119 MODULE_DEPEND(mlx5en, mlx5, 1, 1, 1);
3120 MODULE_VERSION(mlx5en, 1);