]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/mlx5/mlx5_en/mlx5_en_main.c
MFC r330647:
[FreeBSD/FreeBSD.git] / sys / dev / mlx5 / mlx5_en / mlx5_en_main.c
1 /*-
2  * Copyright (c) 2015 Mellanox Technologies. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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
23  * SUCH DAMAGE.
24  *
25  * $FreeBSD$
26  */
27
28 #include "en.h"
29
30 #include <sys/sockio.h>
31 #include <machine/atomic.h>
32
33 #define ETH_DRIVER_VERSION      "3.1.0-dev"
34 char mlx5e_version[] = "Mellanox Ethernet driver"
35     " (" ETH_DRIVER_VERSION ")";
36
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;
42 };
43
44 static const struct {
45         u32     subtype;
46         u64     baudrate;
47 }       mlx5e_mode_table[MLX5E_LINK_MODES_NUMBER] = {
48
49         [MLX5E_1000BASE_CX_SGMII] = {
50                 .subtype = IFM_1000_CX_SGMII,
51                 .baudrate = IF_Mbps(1000ULL),
52         },
53         [MLX5E_1000BASE_KX] = {
54                 .subtype = IFM_1000_KX,
55                 .baudrate = IF_Mbps(1000ULL),
56         },
57         [MLX5E_10GBASE_CX4] = {
58                 .subtype = IFM_10G_CX4,
59                 .baudrate = IF_Gbps(10ULL),
60         },
61         [MLX5E_10GBASE_KX4] = {
62                 .subtype = IFM_10G_KX4,
63                 .baudrate = IF_Gbps(10ULL),
64         },
65         [MLX5E_10GBASE_KR] = {
66                 .subtype = IFM_10G_KR,
67                 .baudrate = IF_Gbps(10ULL),
68         },
69         [MLX5E_20GBASE_KR2] = {
70                 .subtype = IFM_20G_KR2,
71                 .baudrate = IF_Gbps(20ULL),
72         },
73         [MLX5E_40GBASE_CR4] = {
74                 .subtype = IFM_40G_CR4,
75                 .baudrate = IF_Gbps(40ULL),
76         },
77         [MLX5E_40GBASE_KR4] = {
78                 .subtype = IFM_40G_KR4,
79                 .baudrate = IF_Gbps(40ULL),
80         },
81         [MLX5E_56GBASE_R4] = {
82                 .subtype = IFM_56G_R4,
83                 .baudrate = IF_Gbps(56ULL),
84         },
85         [MLX5E_10GBASE_CR] = {
86                 .subtype = IFM_10G_CR1,
87                 .baudrate = IF_Gbps(10ULL),
88         },
89         [MLX5E_10GBASE_SR] = {
90                 .subtype = IFM_10G_SR,
91                 .baudrate = IF_Gbps(10ULL),
92         },
93         [MLX5E_10GBASE_ER] = {
94                 .subtype = IFM_10G_ER,
95                 .baudrate = IF_Gbps(10ULL),
96         },
97         [MLX5E_40GBASE_SR4] = {
98                 .subtype = IFM_40G_SR4,
99                 .baudrate = IF_Gbps(40ULL),
100         },
101         [MLX5E_40GBASE_LR4] = {
102                 .subtype = IFM_40G_LR4,
103                 .baudrate = IF_Gbps(40ULL),
104         },
105         [MLX5E_100GBASE_CR4] = {
106                 .subtype = IFM_100G_CR4,
107                 .baudrate = IF_Gbps(100ULL),
108         },
109         [MLX5E_100GBASE_SR4] = {
110                 .subtype = IFM_100G_SR4,
111                 .baudrate = IF_Gbps(100ULL),
112         },
113         [MLX5E_100GBASE_KR4] = {
114                 .subtype = IFM_100G_KR4,
115                 .baudrate = IF_Gbps(100ULL),
116         },
117         [MLX5E_100GBASE_LR4] = {
118                 .subtype = IFM_100G_LR4,
119                 .baudrate = IF_Gbps(100ULL),
120         },
121         [MLX5E_100BASE_TX] = {
122                 .subtype = IFM_100_TX,
123                 .baudrate = IF_Mbps(100ULL),
124         },
125         [MLX5E_1000BASE_T] = {
126                 .subtype = IFM_1000_T,
127                 .baudrate = IF_Mbps(1000ULL),
128         },
129         [MLX5E_10GBASE_T] = {
130                 .subtype = IFM_10G_T,
131                 .baudrate = IF_Gbps(10ULL),
132         },
133         [MLX5E_25GBASE_CR] = {
134                 .subtype = IFM_25G_CR,
135                 .baudrate = IF_Gbps(25ULL),
136         },
137         [MLX5E_25GBASE_KR] = {
138                 .subtype = IFM_25G_KR,
139                 .baudrate = IF_Gbps(25ULL),
140         },
141         [MLX5E_25GBASE_SR] = {
142                 .subtype = IFM_25G_SR,
143                 .baudrate = IF_Gbps(25ULL),
144         },
145         [MLX5E_50GBASE_CR2] = {
146                 .subtype = IFM_50G_CR2,
147                 .baudrate = IF_Gbps(50ULL),
148         },
149         [MLX5E_50GBASE_KR2] = {
150                 .subtype = IFM_50G_KR2,
151                 .baudrate = IF_Gbps(50ULL),
152         },
153 };
154
155 MALLOC_DEFINE(M_MLX5EN, "MLX5EN", "MLX5 Ethernet");
156
157 static void
158 mlx5e_update_carrier(struct mlx5e_priv *priv)
159 {
160         struct mlx5_core_dev *mdev = priv->mdev;
161         u32 out[MLX5_ST_SZ_DW(ptys_reg)];
162         u32 eth_proto_oper;
163         int error;
164         u8 port_state;
165         u8 i;
166
167         port_state = mlx5_query_vport_state(mdev,
168             MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT, 0);
169
170         if (port_state == VPORT_STATE_UP) {
171                 priv->media_status_last |= IFM_ACTIVE;
172         } else {
173                 priv->media_status_last &= ~IFM_ACTIVE;
174                 priv->media_active_last = IFM_ETHER;
175                 if_link_state_change(priv->ifp, LINK_STATE_DOWN);
176                 return;
177         }
178
179         error = mlx5_query_port_ptys(mdev, out, sizeof(out), MLX5_PTYS_EN, 1);
180         if (error) {
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",
184                     __func__, error);
185                 return;
186         }
187         eth_proto_oper = MLX5_GET(ptys_reg, out, eth_proto_oper);
188
189         for (i = 0; i != MLX5E_LINK_MODES_NUMBER; i++) {
190                 if (mlx5e_mode_table[i].baudrate == 0)
191                         continue;
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;
197                 }
198         }
199         if_link_state_change(priv->ifp, LINK_STATE_UP);
200 }
201
202 static void
203 mlx5e_media_status(struct ifnet *dev, struct ifmediareq *ifmr)
204 {
205         struct mlx5e_priv *priv = dev->if_softc;
206
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);
211
212 }
213
214 static u32
215 mlx5e_find_link_mode(u32 subtype)
216 {
217         u32 i;
218         u32 link_mode = 0;
219
220         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
221                 if (mlx5e_mode_table[i].baudrate == 0)
222                         continue;
223                 if (mlx5e_mode_table[i].subtype == subtype)
224                         link_mode |= MLX5E_PROT_MASK(i);
225         }
226
227         return (link_mode);
228 }
229
230 static int
231 mlx5e_set_port_pause_and_pfc(struct mlx5e_priv *priv)
232 {
233         return (mlx5_set_port_pause_and_pfc(priv->mdev, 1,
234             priv->params.rx_pauseframe_control,
235             priv->params.tx_pauseframe_control,
236             priv->params.rx_priority_flow_control,
237             priv->params.tx_priority_flow_control));
238 }
239
240 static int
241 mlx5e_set_port_pfc(struct mlx5e_priv *priv)
242 {
243         int error;
244
245         if (priv->params.rx_pauseframe_control ||
246             priv->params.tx_pauseframe_control) {
247                 if_printf(priv->ifp,
248                     "Global pauseframes must be disabled before enabling PFC.\n");
249                 error = -EINVAL;
250         } else {
251                 error = mlx5e_set_port_pause_and_pfc(priv);
252         }
253         return (error);
254 }
255
256 static int
257 mlx5e_media_change(struct ifnet *dev)
258 {
259         struct mlx5e_priv *priv = dev->if_softc;
260         struct mlx5_core_dev *mdev = priv->mdev;
261         u32 eth_proto_cap;
262         u32 link_mode;
263         int was_opened;
264         int locked;
265         int error;
266
267         locked = PRIV_LOCKED(priv);
268         if (!locked)
269                 PRIV_LOCK(priv);
270
271         if (IFM_TYPE(priv->media.ifm_media) != IFM_ETHER) {
272                 error = EINVAL;
273                 goto done;
274         }
275         link_mode = mlx5e_find_link_mode(IFM_SUBTYPE(priv->media.ifm_media));
276
277         /* query supported capabilities */
278         error = mlx5_query_port_proto_cap(mdev, &eth_proto_cap, MLX5_PTYS_EN);
279         if (error != 0) {
280                 if_printf(dev, "Query port media capability failed\n");
281                 goto done;
282         }
283         /* check for autoselect */
284         if (IFM_SUBTYPE(priv->media.ifm_media) == IFM_AUTO) {
285                 link_mode = eth_proto_cap;
286                 if (link_mode == 0) {
287                         if_printf(dev, "Port media capability is zero\n");
288                         error = EINVAL;
289                         goto done;
290                 }
291         } else {
292                 link_mode = link_mode & eth_proto_cap;
293                 if (link_mode == 0) {
294                         if_printf(dev, "Not supported link mode requested\n");
295                         error = EINVAL;
296                         goto done;
297                 }
298         }
299         if (priv->media.ifm_media & (IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE)) {
300                 /* check if PFC is enabled */
301                 if (priv->params.rx_priority_flow_control ||
302                     priv->params.tx_priority_flow_control) {
303                         if_printf(dev, "PFC must be disabled before enabling global pauseframes.\n");
304                         error = EINVAL;
305                         goto done;
306                 }
307         }
308         /* update pauseframe control bits */
309         priv->params.rx_pauseframe_control =
310             (priv->media.ifm_media & IFM_ETH_RXPAUSE) ? 1 : 0;
311         priv->params.tx_pauseframe_control =
312             (priv->media.ifm_media & IFM_ETH_TXPAUSE) ? 1 : 0;
313
314         /* check if device is opened */
315         was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
316
317         /* reconfigure the hardware */
318         mlx5_set_port_status(mdev, MLX5_PORT_DOWN);
319         mlx5_set_port_proto(mdev, link_mode, MLX5_PTYS_EN);
320         error = -mlx5e_set_port_pause_and_pfc(priv);
321         if (was_opened)
322                 mlx5_set_port_status(mdev, MLX5_PORT_UP);
323
324 done:
325         if (!locked)
326                 PRIV_UNLOCK(priv);
327         return (error);
328 }
329
330 static void
331 mlx5e_update_carrier_work(struct work_struct *work)
332 {
333         struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
334             update_carrier_work);
335
336         PRIV_LOCK(priv);
337         if (test_bit(MLX5E_STATE_OPENED, &priv->state))
338                 mlx5e_update_carrier(priv);
339         PRIV_UNLOCK(priv);
340 }
341
342 /*
343  * This function reads the physical port counters from the firmware
344  * using a pre-defined layout defined by various MLX5E_PPORT_XXX()
345  * macros. The output is converted from big-endian 64-bit values into
346  * host endian ones and stored in the "priv->stats.pport" structure.
347  */
348 static void
349 mlx5e_update_pport_counters(struct mlx5e_priv *priv)
350 {
351         struct mlx5_core_dev *mdev = priv->mdev;
352         struct mlx5e_pport_stats *s = &priv->stats.pport;
353         struct mlx5e_port_stats_debug *s_debug = &priv->stats.port_stats_debug;
354         u32 *in;
355         u32 *out;
356         const u64 *ptr;
357         unsigned sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
358         unsigned x;
359         unsigned y;
360         unsigned z;
361
362         /* allocate firmware request structures */
363         in = mlx5_vzalloc(sz);
364         out = mlx5_vzalloc(sz);
365         if (in == NULL || out == NULL)
366                 goto free_out;
367
368         /*
369          * Get pointer to the 64-bit counter set which is located at a
370          * fixed offset in the output firmware request structure:
371          */
372         ptr = (const uint64_t *)MLX5_ADDR_OF(ppcnt_reg, out, counter_set);
373
374         MLX5_SET(ppcnt_reg, in, local_port, 1);
375
376         /* read IEEE802_3 counter group using predefined counter layout */
377         MLX5_SET(ppcnt_reg, in, grp, MLX5_IEEE_802_3_COUNTERS_GROUP);
378         mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
379         for (x = 0, y = MLX5E_PPORT_PER_PRIO_STATS_NUM;
380              x != MLX5E_PPORT_IEEE802_3_STATS_NUM; x++, y++)
381                 s->arg[y] = be64toh(ptr[x]);
382
383         /* read RFC2819 counter group using predefined counter layout */
384         MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2819_COUNTERS_GROUP);
385         mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
386         for (x = 0; x != MLX5E_PPORT_RFC2819_STATS_NUM; x++, y++)
387                 s->arg[y] = be64toh(ptr[x]);
388         for (y = 0; x != MLX5E_PPORT_RFC2819_STATS_NUM +
389             MLX5E_PPORT_RFC2819_STATS_DEBUG_NUM; x++, y++)
390                 s_debug->arg[y] = be64toh(ptr[x]);
391
392         /* read RFC2863 counter group using predefined counter layout */
393         MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2863_COUNTERS_GROUP);
394         mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
395         for (x = 0; x != MLX5E_PPORT_RFC2863_STATS_DEBUG_NUM; x++, y++)
396                 s_debug->arg[y] = be64toh(ptr[x]);
397
398         /* read physical layer stats counter group using predefined counter layout */
399         MLX5_SET(ppcnt_reg, in, grp, MLX5_PHYSICAL_LAYER_COUNTERS_GROUP);
400         mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
401         for (x = 0; x != MLX5E_PPORT_PHYSICAL_LAYER_STATS_DEBUG_NUM; x++, y++)
402                 s_debug->arg[y] = be64toh(ptr[x]);
403
404         /* read per-priority counters */
405         MLX5_SET(ppcnt_reg, in, grp, MLX5_PER_PRIORITY_COUNTERS_GROUP);
406
407         /* iterate all the priorities */
408         for (y = z = 0; z != MLX5E_PPORT_PER_PRIO_STATS_NUM_PRIO; z++) {
409                 MLX5_SET(ppcnt_reg, in, prio_tc, z);
410                 mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
411
412                 /* read per priority stats counter group using predefined counter layout */
413                 for (x = 0; x != (MLX5E_PPORT_PER_PRIO_STATS_NUM /
414                     MLX5E_PPORT_PER_PRIO_STATS_NUM_PRIO); x++, y++)
415                         s->arg[y] = be64toh(ptr[x]);
416         }
417 free_out:
418         /* free firmware request structures */
419         kvfree(in);
420         kvfree(out);
421 }
422
423 /*
424  * This function is called regularly to collect all statistics
425  * counters from the firmware. The values can be viewed through the
426  * sysctl interface. Execution is serialized using the priv's global
427  * configuration lock.
428  */
429 static void
430 mlx5e_update_stats_work(struct work_struct *work)
431 {
432         struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
433             update_stats_work);
434         struct mlx5_core_dev *mdev = priv->mdev;
435         struct mlx5e_vport_stats *s = &priv->stats.vport;
436         struct mlx5e_rq_stats *rq_stats;
437         struct mlx5e_sq_stats *sq_stats;
438         struct buf_ring *sq_br;
439 #if (__FreeBSD_version < 1100000)
440         struct ifnet *ifp = priv->ifp;
441 #endif
442
443         u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)];
444         u32 *out;
445         int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
446         u64 tso_packets = 0;
447         u64 tso_bytes = 0;
448         u64 tx_queue_dropped = 0;
449         u64 tx_defragged = 0;
450         u64 tx_offload_none = 0;
451         u64 lro_packets = 0;
452         u64 lro_bytes = 0;
453         u64 sw_lro_queued = 0;
454         u64 sw_lro_flushed = 0;
455         u64 rx_csum_none = 0;
456         u64 rx_wqe_err = 0;
457         u32 rx_out_of_buffer = 0;
458         int i;
459         int j;
460
461         PRIV_LOCK(priv);
462         out = mlx5_vzalloc(outlen);
463         if (out == NULL)
464                 goto free_out;
465         if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
466                 goto free_out;
467
468         /* Collect firts the SW counters and then HW for consistency */
469         for (i = 0; i < priv->params.num_channels; i++) {
470                 struct mlx5e_rq *rq = &priv->channel[i]->rq;
471
472                 rq_stats = &priv->channel[i]->rq.stats;
473
474                 /* collect stats from LRO */
475                 rq_stats->sw_lro_queued = rq->lro.lro_queued;
476                 rq_stats->sw_lro_flushed = rq->lro.lro_flushed;
477                 sw_lro_queued += rq_stats->sw_lro_queued;
478                 sw_lro_flushed += rq_stats->sw_lro_flushed;
479                 lro_packets += rq_stats->lro_packets;
480                 lro_bytes += rq_stats->lro_bytes;
481                 rx_csum_none += rq_stats->csum_none;
482                 rx_wqe_err += rq_stats->wqe_err;
483
484                 for (j = 0; j < priv->num_tc; j++) {
485                         sq_stats = &priv->channel[i]->sq[j].stats;
486                         sq_br = priv->channel[i]->sq[j].br;
487
488                         tso_packets += sq_stats->tso_packets;
489                         tso_bytes += sq_stats->tso_bytes;
490                         tx_queue_dropped += sq_stats->dropped;
491                         if (sq_br != NULL)
492                                 tx_queue_dropped += sq_br->br_drops;
493                         tx_defragged += sq_stats->defragged;
494                         tx_offload_none += sq_stats->csum_offload_none;
495                 }
496         }
497
498         /* update counters */
499         s->tso_packets = tso_packets;
500         s->tso_bytes = tso_bytes;
501         s->tx_queue_dropped = tx_queue_dropped;
502         s->tx_defragged = tx_defragged;
503         s->lro_packets = lro_packets;
504         s->lro_bytes = lro_bytes;
505         s->sw_lro_queued = sw_lro_queued;
506         s->sw_lro_flushed = sw_lro_flushed;
507         s->rx_csum_none = rx_csum_none;
508         s->rx_wqe_err = rx_wqe_err;
509
510         /* HW counters */
511         memset(in, 0, sizeof(in));
512
513         MLX5_SET(query_vport_counter_in, in, opcode,
514             MLX5_CMD_OP_QUERY_VPORT_COUNTER);
515         MLX5_SET(query_vport_counter_in, in, op_mod, 0);
516         MLX5_SET(query_vport_counter_in, in, other_vport, 0);
517
518         memset(out, 0, outlen);
519
520         /* get number of out-of-buffer drops first */
521         if (mlx5_vport_query_out_of_rx_buffer(mdev, priv->counter_set_id,
522             &rx_out_of_buffer))
523                 goto free_out;
524
525         /* accumulate difference into a 64-bit counter */
526         s->rx_out_of_buffer += (u64)(u32)(rx_out_of_buffer - s->rx_out_of_buffer_prev);
527         s->rx_out_of_buffer_prev = rx_out_of_buffer;
528
529         /* get port statistics */
530         if (mlx5_cmd_exec(mdev, in, sizeof(in), out, outlen))
531                 goto free_out;
532
533 #define MLX5_GET_CTR(out, x) \
534         MLX5_GET64(query_vport_counter_out, out, x)
535
536         s->rx_error_packets =
537             MLX5_GET_CTR(out, received_errors.packets);
538         s->rx_error_bytes =
539             MLX5_GET_CTR(out, received_errors.octets);
540         s->tx_error_packets =
541             MLX5_GET_CTR(out, transmit_errors.packets);
542         s->tx_error_bytes =
543             MLX5_GET_CTR(out, transmit_errors.octets);
544
545         s->rx_unicast_packets =
546             MLX5_GET_CTR(out, received_eth_unicast.packets);
547         s->rx_unicast_bytes =
548             MLX5_GET_CTR(out, received_eth_unicast.octets);
549         s->tx_unicast_packets =
550             MLX5_GET_CTR(out, transmitted_eth_unicast.packets);
551         s->tx_unicast_bytes =
552             MLX5_GET_CTR(out, transmitted_eth_unicast.octets);
553
554         s->rx_multicast_packets =
555             MLX5_GET_CTR(out, received_eth_multicast.packets);
556         s->rx_multicast_bytes =
557             MLX5_GET_CTR(out, received_eth_multicast.octets);
558         s->tx_multicast_packets =
559             MLX5_GET_CTR(out, transmitted_eth_multicast.packets);
560         s->tx_multicast_bytes =
561             MLX5_GET_CTR(out, transmitted_eth_multicast.octets);
562
563         s->rx_broadcast_packets =
564             MLX5_GET_CTR(out, received_eth_broadcast.packets);
565         s->rx_broadcast_bytes =
566             MLX5_GET_CTR(out, received_eth_broadcast.octets);
567         s->tx_broadcast_packets =
568             MLX5_GET_CTR(out, transmitted_eth_broadcast.packets);
569         s->tx_broadcast_bytes =
570             MLX5_GET_CTR(out, transmitted_eth_broadcast.octets);
571
572         s->rx_packets =
573             s->rx_unicast_packets +
574             s->rx_multicast_packets +
575             s->rx_broadcast_packets -
576             s->rx_out_of_buffer;
577         s->rx_bytes =
578             s->rx_unicast_bytes +
579             s->rx_multicast_bytes +
580             s->rx_broadcast_bytes;
581         s->tx_packets =
582             s->tx_unicast_packets +
583             s->tx_multicast_packets +
584             s->tx_broadcast_packets;
585         s->tx_bytes =
586             s->tx_unicast_bytes +
587             s->tx_multicast_bytes +
588             s->tx_broadcast_bytes;
589
590         /* Update calculated offload counters */
591         s->tx_csum_offload = s->tx_packets - tx_offload_none;
592         s->rx_csum_good = s->rx_packets - s->rx_csum_none;
593
594         /* Get physical port counters */
595         mlx5e_update_pport_counters(priv);
596
597 #if (__FreeBSD_version < 1100000)
598         /* no get_counters interface in fbsd 10 */
599         ifp->if_ipackets = s->rx_packets;
600         ifp->if_ierrors = s->rx_error_packets +
601             priv->stats.pport.alignment_err +
602             priv->stats.pport.check_seq_err +
603             priv->stats.pport.crc_align_errors +
604             priv->stats.pport.in_range_len_errors +
605             priv->stats.pport.jabbers +
606             priv->stats.pport.out_of_range_len +
607             priv->stats.pport.oversize_pkts +
608             priv->stats.pport.symbol_err +
609             priv->stats.pport.too_long_errors +
610             priv->stats.pport.undersize_pkts +
611             priv->stats.pport.unsupported_op_rx;
612         ifp->if_iqdrops = s->rx_out_of_buffer +
613             priv->stats.pport.drop_events;
614         ifp->if_opackets = s->tx_packets;
615         ifp->if_oerrors = s->tx_error_packets;
616         ifp->if_snd.ifq_drops = s->tx_queue_dropped;
617         ifp->if_ibytes = s->rx_bytes;
618         ifp->if_obytes = s->tx_bytes;
619         ifp->if_collisions =
620             priv->stats.pport.collisions;
621 #endif
622
623 free_out:
624         kvfree(out);
625
626         /* Update diagnostics, if any */
627         if (priv->params_ethtool.diag_pci_enable ||
628             priv->params_ethtool.diag_general_enable) {
629                 int error = mlx5_core_get_diagnostics_full(mdev,
630                     priv->params_ethtool.diag_pci_enable ? &priv->params_pci : NULL,
631                     priv->params_ethtool.diag_general_enable ? &priv->params_general : NULL);
632                 if (error != 0)
633                         if_printf(priv->ifp, "Failed reading diagnostics: %d\n", error);
634         }
635         PRIV_UNLOCK(priv);
636 }
637
638 static void
639 mlx5e_update_stats(void *arg)
640 {
641         struct mlx5e_priv *priv = arg;
642
643         queue_work(priv->wq, &priv->update_stats_work);
644
645         callout_reset(&priv->watchdog, hz, &mlx5e_update_stats, priv);
646 }
647
648 static void
649 mlx5e_async_event_sub(struct mlx5e_priv *priv,
650     enum mlx5_dev_event event)
651 {
652         switch (event) {
653         case MLX5_DEV_EVENT_PORT_UP:
654         case MLX5_DEV_EVENT_PORT_DOWN:
655                 queue_work(priv->wq, &priv->update_carrier_work);
656                 break;
657
658         default:
659                 break;
660         }
661 }
662
663 static void
664 mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
665     enum mlx5_dev_event event, unsigned long param)
666 {
667         struct mlx5e_priv *priv = vpriv;
668
669         mtx_lock(&priv->async_events_mtx);
670         if (test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state))
671                 mlx5e_async_event_sub(priv, event);
672         mtx_unlock(&priv->async_events_mtx);
673 }
674
675 static void
676 mlx5e_enable_async_events(struct mlx5e_priv *priv)
677 {
678         set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state);
679 }
680
681 static void
682 mlx5e_disable_async_events(struct mlx5e_priv *priv)
683 {
684         mtx_lock(&priv->async_events_mtx);
685         clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLE, &priv->state);
686         mtx_unlock(&priv->async_events_mtx);
687 }
688
689 static const char *mlx5e_rq_stats_desc[] = {
690         MLX5E_RQ_STATS(MLX5E_STATS_DESC)
691 };
692
693 static int
694 mlx5e_create_rq(struct mlx5e_channel *c,
695     struct mlx5e_rq_param *param,
696     struct mlx5e_rq *rq)
697 {
698         struct mlx5e_priv *priv = c->priv;
699         struct mlx5_core_dev *mdev = priv->mdev;
700         char buffer[16];
701         void *rqc = param->rqc;
702         void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
703         int wq_sz;
704         int err;
705         int i;
706
707         /* Create DMA descriptor TAG */
708         if ((err = -bus_dma_tag_create(
709             bus_get_dma_tag(mdev->pdev->dev.bsddev),
710             1,                          /* any alignment */
711             0,                          /* no boundary */
712             BUS_SPACE_MAXADDR,          /* lowaddr */
713             BUS_SPACE_MAXADDR,          /* highaddr */
714             NULL, NULL,                 /* filter, filterarg */
715             MJUM16BYTES,                /* maxsize */
716             1,                          /* nsegments */
717             MJUM16BYTES,                /* maxsegsize */
718             0,                          /* flags */
719             NULL, NULL,                 /* lockfunc, lockfuncarg */
720             &rq->dma_tag)))
721                 goto done;
722
723         err = mlx5_wq_ll_create(mdev, &param->wq, rqc_wq, &rq->wq,
724             &rq->wq_ctrl);
725         if (err)
726                 goto err_free_dma_tag;
727
728         rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
729
730         if (priv->params.hw_lro_en) {
731                 rq->wqe_sz = priv->params.lro_wqe_sz;
732         } else {
733                 rq->wqe_sz = MLX5E_SW2MB_MTU(priv->ifp->if_mtu);
734         }
735         if (rq->wqe_sz > MJUM16BYTES) {
736                 err = -ENOMEM;
737                 goto err_rq_wq_destroy;
738         } else if (rq->wqe_sz > MJUM9BYTES) {
739                 rq->wqe_sz = MJUM16BYTES;
740         } else if (rq->wqe_sz > MJUMPAGESIZE) {
741                 rq->wqe_sz = MJUM9BYTES;
742         } else if (rq->wqe_sz > MCLBYTES) {
743                 rq->wqe_sz = MJUMPAGESIZE;
744         } else {
745                 rq->wqe_sz = MCLBYTES;
746         }
747
748         wq_sz = mlx5_wq_ll_get_size(&rq->wq);
749
750         err = -tcp_lro_init_args(&rq->lro, c->ifp, TCP_LRO_ENTRIES, wq_sz);
751         if (err)
752                 goto err_rq_wq_destroy;
753
754         rq->mbuf = malloc(wq_sz * sizeof(rq->mbuf[0]), M_MLX5EN, M_WAITOK | M_ZERO);
755         for (i = 0; i != wq_sz; i++) {
756                 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
757                 uint32_t byte_count = rq->wqe_sz - MLX5E_NET_IP_ALIGN;
758
759                 err = -bus_dmamap_create(rq->dma_tag, 0, &rq->mbuf[i].dma_map);
760                 if (err != 0) {
761                         while (i--)
762                                 bus_dmamap_destroy(rq->dma_tag, rq->mbuf[i].dma_map);
763                         goto err_rq_mbuf_free;
764                 }
765                 wqe->data.lkey = c->mkey_be;
766                 wqe->data.byte_count = cpu_to_be32(byte_count | MLX5_HW_START_PADDING);
767         }
768
769         rq->ifp = c->ifp;
770         rq->channel = c;
771         rq->ix = c->ix;
772
773         snprintf(buffer, sizeof(buffer), "rxstat%d", c->ix);
774         mlx5e_create_stats(&rq->stats.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
775             buffer, mlx5e_rq_stats_desc, MLX5E_RQ_STATS_NUM,
776             rq->stats.arg);
777         return (0);
778
779 err_rq_mbuf_free:
780         free(rq->mbuf, M_MLX5EN);
781         tcp_lro_free(&rq->lro);
782 err_rq_wq_destroy:
783         mlx5_wq_destroy(&rq->wq_ctrl);
784 err_free_dma_tag:
785         bus_dma_tag_destroy(rq->dma_tag);
786 done:
787         return (err);
788 }
789
790 static void
791 mlx5e_destroy_rq(struct mlx5e_rq *rq)
792 {
793         int wq_sz;
794         int i;
795
796         /* destroy all sysctl nodes */
797         sysctl_ctx_free(&rq->stats.ctx);
798
799         /* free leftover LRO packets, if any */
800         tcp_lro_free(&rq->lro);
801
802         wq_sz = mlx5_wq_ll_get_size(&rq->wq);
803         for (i = 0; i != wq_sz; i++) {
804                 if (rq->mbuf[i].mbuf != NULL) {
805                         bus_dmamap_unload(rq->dma_tag,
806                             rq->mbuf[i].dma_map);
807                         m_freem(rq->mbuf[i].mbuf);
808                 }
809                 bus_dmamap_destroy(rq->dma_tag, rq->mbuf[i].dma_map);
810         }
811         free(rq->mbuf, M_MLX5EN);
812         mlx5_wq_destroy(&rq->wq_ctrl);
813 }
814
815 static int
816 mlx5e_enable_rq(struct mlx5e_rq *rq, struct mlx5e_rq_param *param)
817 {
818         struct mlx5e_channel *c = rq->channel;
819         struct mlx5e_priv *priv = c->priv;
820         struct mlx5_core_dev *mdev = priv->mdev;
821
822         void *in;
823         void *rqc;
824         void *wq;
825         int inlen;
826         int err;
827
828         inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
829             sizeof(u64) * rq->wq_ctrl.buf.npages;
830         in = mlx5_vzalloc(inlen);
831         if (in == NULL)
832                 return (-ENOMEM);
833
834         rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
835         wq = MLX5_ADDR_OF(rqc, rqc, wq);
836
837         memcpy(rqc, param->rqc, sizeof(param->rqc));
838
839         MLX5_SET(rqc, rqc, cqn, c->rq.cq.mcq.cqn);
840         MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RST);
841         MLX5_SET(rqc, rqc, flush_in_error_en, 1);
842         if (priv->counter_set_id >= 0)
843                 MLX5_SET(rqc, rqc, counter_set_id, priv->counter_set_id);
844         MLX5_SET(wq, wq, log_wq_pg_sz, rq->wq_ctrl.buf.page_shift -
845             PAGE_SHIFT);
846         MLX5_SET64(wq, wq, dbr_addr, rq->wq_ctrl.db.dma);
847
848         mlx5_fill_page_array(&rq->wq_ctrl.buf,
849             (__be64 *) MLX5_ADDR_OF(wq, wq, pas));
850
851         err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
852
853         kvfree(in);
854
855         return (err);
856 }
857
858 static int
859 mlx5e_modify_rq(struct mlx5e_rq *rq, int curr_state, int next_state)
860 {
861         struct mlx5e_channel *c = rq->channel;
862         struct mlx5e_priv *priv = c->priv;
863         struct mlx5_core_dev *mdev = priv->mdev;
864
865         void *in;
866         void *rqc;
867         int inlen;
868         int err;
869
870         inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
871         in = mlx5_vzalloc(inlen);
872         if (in == NULL)
873                 return (-ENOMEM);
874
875         rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
876
877         MLX5_SET(modify_rq_in, in, rqn, rq->rqn);
878         MLX5_SET(modify_rq_in, in, rq_state, curr_state);
879         MLX5_SET(rqc, rqc, state, next_state);
880
881         err = mlx5_core_modify_rq(mdev, in, inlen);
882
883         kvfree(in);
884
885         return (err);
886 }
887
888 static void
889 mlx5e_disable_rq(struct mlx5e_rq *rq)
890 {
891         struct mlx5e_channel *c = rq->channel;
892         struct mlx5e_priv *priv = c->priv;
893         struct mlx5_core_dev *mdev = priv->mdev;
894
895         mlx5_core_destroy_rq(mdev, rq->rqn);
896 }
897
898 static int
899 mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq)
900 {
901         struct mlx5e_channel *c = rq->channel;
902         struct mlx5e_priv *priv = c->priv;
903         struct mlx5_wq_ll *wq = &rq->wq;
904         int i;
905
906         for (i = 0; i < 1000; i++) {
907                 if (wq->cur_sz >= priv->params.min_rx_wqes)
908                         return (0);
909
910                 msleep(4);
911         }
912         return (-ETIMEDOUT);
913 }
914
915 static int
916 mlx5e_open_rq(struct mlx5e_channel *c,
917     struct mlx5e_rq_param *param,
918     struct mlx5e_rq *rq)
919 {
920         int err;
921
922         err = mlx5e_create_rq(c, param, rq);
923         if (err)
924                 return (err);
925
926         err = mlx5e_enable_rq(rq, param);
927         if (err)
928                 goto err_destroy_rq;
929
930         err = mlx5e_modify_rq(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
931         if (err)
932                 goto err_disable_rq;
933
934         c->rq.enabled = 1;
935
936         return (0);
937
938 err_disable_rq:
939         mlx5e_disable_rq(rq);
940 err_destroy_rq:
941         mlx5e_destroy_rq(rq);
942
943         return (err);
944 }
945
946 static void
947 mlx5e_close_rq(struct mlx5e_rq *rq)
948 {
949         mtx_lock(&rq->mtx);
950         rq->enabled = 0;
951         callout_stop(&rq->watchdog);
952         mtx_unlock(&rq->mtx);
953
954         callout_drain(&rq->watchdog);
955
956         mlx5e_modify_rq(rq, MLX5_RQC_STATE_RDY, MLX5_RQC_STATE_ERR);
957 }
958
959 static void
960 mlx5e_close_rq_wait(struct mlx5e_rq *rq)
961 {
962         /* wait till RQ is empty */
963         while (!mlx5_wq_ll_is_empty(&rq->wq)) {
964                 msleep(4);
965                 rq->cq.mcq.comp(&rq->cq.mcq);
966         }
967
968         mlx5e_disable_rq(rq);
969         mlx5e_destroy_rq(rq);
970 }
971
972 void
973 mlx5e_free_sq_db(struct mlx5e_sq *sq)
974 {
975         int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
976         int x;
977
978         for (x = 0; x != wq_sz; x++)
979                 bus_dmamap_destroy(sq->dma_tag, sq->mbuf[x].dma_map);
980         free(sq->mbuf, M_MLX5EN);
981 }
982
983 int
984 mlx5e_alloc_sq_db(struct mlx5e_sq *sq)
985 {
986         int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
987         int err;
988         int x;
989
990         sq->mbuf = malloc(wq_sz * sizeof(sq->mbuf[0]), M_MLX5EN, M_WAITOK | M_ZERO);
991
992         /* Create DMA descriptor MAPs */
993         for (x = 0; x != wq_sz; x++) {
994                 err = -bus_dmamap_create(sq->dma_tag, 0, &sq->mbuf[x].dma_map);
995                 if (err != 0) {
996                         while (x--)
997                                 bus_dmamap_destroy(sq->dma_tag, sq->mbuf[x].dma_map);
998                         free(sq->mbuf, M_MLX5EN);
999                         return (err);
1000                 }
1001         }
1002         return (0);
1003 }
1004
1005 static const char *mlx5e_sq_stats_desc[] = {
1006         MLX5E_SQ_STATS(MLX5E_STATS_DESC)
1007 };
1008
1009 static int
1010 mlx5e_create_sq(struct mlx5e_channel *c,
1011     int tc,
1012     struct mlx5e_sq_param *param,
1013     struct mlx5e_sq *sq)
1014 {
1015         struct mlx5e_priv *priv = c->priv;
1016         struct mlx5_core_dev *mdev = priv->mdev;
1017         char buffer[16];
1018
1019         void *sqc = param->sqc;
1020         void *sqc_wq = MLX5_ADDR_OF(sqc, sqc, wq);
1021 #ifdef RSS
1022         cpuset_t cpu_mask;
1023         int cpu_id;
1024 #endif
1025         int err;
1026
1027         /* Create DMA descriptor TAG */
1028         if ((err = -bus_dma_tag_create(
1029             bus_get_dma_tag(mdev->pdev->dev.bsddev),
1030             1,                          /* any alignment */
1031             0,                          /* no boundary */
1032             BUS_SPACE_MAXADDR,          /* lowaddr */
1033             BUS_SPACE_MAXADDR,          /* highaddr */
1034             NULL, NULL,                 /* filter, filterarg */
1035             MLX5E_MAX_TX_PAYLOAD_SIZE,  /* maxsize */
1036             MLX5E_MAX_TX_MBUF_FRAGS,    /* nsegments */
1037             MLX5E_MAX_TX_MBUF_SIZE,     /* maxsegsize */
1038             0,                          /* flags */
1039             NULL, NULL,                 /* lockfunc, lockfuncarg */
1040             &sq->dma_tag)))
1041                 goto done;
1042
1043         err = mlx5_alloc_map_uar(mdev, &sq->uar);
1044         if (err)
1045                 goto err_free_dma_tag;
1046
1047         err = mlx5_wq_cyc_create(mdev, &param->wq, sqc_wq, &sq->wq,
1048             &sq->wq_ctrl);
1049         if (err)
1050                 goto err_unmap_free_uar;
1051
1052         sq->wq.db = &sq->wq.db[MLX5_SND_DBR];
1053         sq->bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
1054
1055         err = mlx5e_alloc_sq_db(sq);
1056         if (err)
1057                 goto err_sq_wq_destroy;
1058
1059         sq->mkey_be = c->mkey_be;
1060         sq->ifp = priv->ifp;
1061         sq->priv = priv;
1062         sq->tc = tc;
1063
1064         /* check if we should allocate a second packet buffer */
1065         if (priv->params_ethtool.tx_bufring_disable == 0) {
1066                 sq->br = buf_ring_alloc(MLX5E_SQ_TX_QUEUE_SIZE, M_MLX5EN,
1067                     M_WAITOK, &sq->lock);
1068                 if (sq->br == NULL) {
1069                         if_printf(c->ifp, "%s: Failed allocating sq drbr buffer\n",
1070                             __func__);
1071                         err = -ENOMEM;
1072                         goto err_free_sq_db;
1073                 }
1074
1075                 sq->sq_tq = taskqueue_create_fast("mlx5e_que", M_WAITOK,
1076                     taskqueue_thread_enqueue, &sq->sq_tq);
1077                 if (sq->sq_tq == NULL) {
1078                         if_printf(c->ifp, "%s: Failed allocating taskqueue\n",
1079                             __func__);
1080                         err = -ENOMEM;
1081                         goto err_free_drbr;
1082                 }
1083
1084                 TASK_INIT(&sq->sq_task, 0, mlx5e_tx_que, sq);
1085 #ifdef RSS
1086                 cpu_id = rss_getcpu(c->ix % rss_getnumbuckets());
1087                 CPU_SETOF(cpu_id, &cpu_mask);
1088                 taskqueue_start_threads_cpuset(&sq->sq_tq, 1, PI_NET, &cpu_mask,
1089                     "%s TX SQ%d.%d CPU%d", c->ifp->if_xname, c->ix, tc, cpu_id);
1090 #else
1091                 taskqueue_start_threads(&sq->sq_tq, 1, PI_NET,
1092                     "%s TX SQ%d.%d", c->ifp->if_xname, c->ix, tc);
1093 #endif
1094         }
1095         snprintf(buffer, sizeof(buffer), "txstat%dtc%d", c->ix, tc);
1096         mlx5e_create_stats(&sq->stats.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
1097             buffer, mlx5e_sq_stats_desc, MLX5E_SQ_STATS_NUM,
1098             sq->stats.arg);
1099
1100         return (0);
1101
1102 err_free_drbr:
1103         buf_ring_free(sq->br, M_MLX5EN);
1104 err_free_sq_db:
1105         mlx5e_free_sq_db(sq);
1106 err_sq_wq_destroy:
1107         mlx5_wq_destroy(&sq->wq_ctrl);
1108
1109 err_unmap_free_uar:
1110         mlx5_unmap_free_uar(mdev, &sq->uar);
1111
1112 err_free_dma_tag:
1113         bus_dma_tag_destroy(sq->dma_tag);
1114 done:
1115         return (err);
1116 }
1117
1118 static void
1119 mlx5e_destroy_sq(struct mlx5e_sq *sq)
1120 {
1121         /* destroy all sysctl nodes */
1122         sysctl_ctx_free(&sq->stats.ctx);
1123
1124         mlx5e_free_sq_db(sq);
1125         mlx5_wq_destroy(&sq->wq_ctrl);
1126         mlx5_unmap_free_uar(sq->priv->mdev, &sq->uar);
1127         if (sq->sq_tq != NULL) {
1128                 taskqueue_drain(sq->sq_tq, &sq->sq_task);
1129                 taskqueue_free(sq->sq_tq);
1130         }
1131         if (sq->br != NULL)
1132                 buf_ring_free(sq->br, M_MLX5EN);
1133 }
1134
1135 int
1136 mlx5e_enable_sq(struct mlx5e_sq *sq, struct mlx5e_sq_param *param,
1137     int tis_num)
1138 {
1139         void *in;
1140         void *sqc;
1141         void *wq;
1142         int inlen;
1143         int err;
1144
1145         inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
1146             sizeof(u64) * sq->wq_ctrl.buf.npages;
1147         in = mlx5_vzalloc(inlen);
1148         if (in == NULL)
1149                 return (-ENOMEM);
1150
1151         sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
1152         wq = MLX5_ADDR_OF(sqc, sqc, wq);
1153
1154         memcpy(sqc, param->sqc, sizeof(param->sqc));
1155
1156         MLX5_SET(sqc, sqc, tis_num_0, tis_num);
1157         MLX5_SET(sqc, sqc, cqn, sq->cq.mcq.cqn);
1158         MLX5_SET(sqc, sqc, state, MLX5_SQC_STATE_RST);
1159         MLX5_SET(sqc, sqc, tis_lst_sz, 1);
1160         MLX5_SET(sqc, sqc, flush_in_error_en, 1);
1161
1162         MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_CYCLIC);
1163         MLX5_SET(wq, wq, uar_page, sq->uar.index);
1164         MLX5_SET(wq, wq, log_wq_pg_sz, sq->wq_ctrl.buf.page_shift -
1165             PAGE_SHIFT);
1166         MLX5_SET64(wq, wq, dbr_addr, sq->wq_ctrl.db.dma);
1167
1168         mlx5_fill_page_array(&sq->wq_ctrl.buf,
1169             (__be64 *) MLX5_ADDR_OF(wq, wq, pas));
1170
1171         err = mlx5_core_create_sq(sq->priv->mdev, in, inlen, &sq->sqn);
1172
1173         kvfree(in);
1174
1175         return (err);
1176 }
1177
1178 int
1179 mlx5e_modify_sq(struct mlx5e_sq *sq, int curr_state, int next_state)
1180 {
1181         void *in;
1182         void *sqc;
1183         int inlen;
1184         int err;
1185
1186         inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
1187         in = mlx5_vzalloc(inlen);
1188         if (in == NULL)
1189                 return (-ENOMEM);
1190
1191         sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
1192
1193         MLX5_SET(modify_sq_in, in, sqn, sq->sqn);
1194         MLX5_SET(modify_sq_in, in, sq_state, curr_state);
1195         MLX5_SET(sqc, sqc, state, next_state);
1196
1197         err = mlx5_core_modify_sq(sq->priv->mdev, in, inlen);
1198
1199         kvfree(in);
1200
1201         return (err);
1202 }
1203
1204 void
1205 mlx5e_disable_sq(struct mlx5e_sq *sq)
1206 {
1207
1208         mlx5_core_destroy_sq(sq->priv->mdev, sq->sqn);
1209 }
1210
1211 static int
1212 mlx5e_open_sq(struct mlx5e_channel *c,
1213     int tc,
1214     struct mlx5e_sq_param *param,
1215     struct mlx5e_sq *sq)
1216 {
1217         int err;
1218
1219         err = mlx5e_create_sq(c, tc, param, sq);
1220         if (err)
1221                 return (err);
1222
1223         err = mlx5e_enable_sq(sq, param, c->priv->tisn[tc]);
1224         if (err)
1225                 goto err_destroy_sq;
1226
1227         err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST, MLX5_SQC_STATE_RDY);
1228         if (err)
1229                 goto err_disable_sq;
1230
1231         atomic_store_rel_int(&sq->queue_state, MLX5E_SQ_READY);
1232
1233         return (0);
1234
1235 err_disable_sq:
1236         mlx5e_disable_sq(sq);
1237 err_destroy_sq:
1238         mlx5e_destroy_sq(sq);
1239
1240         return (err);
1241 }
1242
1243 static void
1244 mlx5e_sq_send_nops_locked(struct mlx5e_sq *sq, int can_sleep)
1245 {
1246         /* fill up remainder with NOPs */
1247         while (sq->cev_counter != 0) {
1248                 while (!mlx5e_sq_has_room_for(sq, 1)) {
1249                         if (can_sleep != 0) {
1250                                 mtx_unlock(&sq->lock);
1251                                 msleep(4);
1252                                 mtx_lock(&sq->lock);
1253                         } else {
1254                                 goto done;
1255                         }
1256                 }
1257                 /* send a single NOP */
1258                 mlx5e_send_nop(sq, 1);
1259                 atomic_thread_fence_rel();
1260         }
1261 done:
1262         /* Check if we need to write the doorbell */
1263         if (likely(sq->doorbell.d64 != 0)) {
1264                 mlx5e_tx_notify_hw(sq, sq->doorbell.d32, 0);
1265                 sq->doorbell.d64 = 0;
1266         }
1267 }
1268
1269 void
1270 mlx5e_sq_cev_timeout(void *arg)
1271 {
1272         struct mlx5e_sq *sq = arg;
1273
1274         mtx_assert(&sq->lock, MA_OWNED);
1275
1276         /* check next state */
1277         switch (sq->cev_next_state) {
1278         case MLX5E_CEV_STATE_SEND_NOPS:
1279                 /* fill TX ring with NOPs, if any */
1280                 mlx5e_sq_send_nops_locked(sq, 0);
1281
1282                 /* check if completed */
1283                 if (sq->cev_counter == 0) {
1284                         sq->cev_next_state = MLX5E_CEV_STATE_INITIAL;
1285                         return;
1286                 }
1287                 break;
1288         default:
1289                 /* send NOPs on next timeout */
1290                 sq->cev_next_state = MLX5E_CEV_STATE_SEND_NOPS;
1291                 break;
1292         }
1293
1294         /* restart timer */
1295         callout_reset_curcpu(&sq->cev_callout, hz, mlx5e_sq_cev_timeout, sq);
1296 }
1297
1298 void
1299 mlx5e_drain_sq(struct mlx5e_sq *sq)
1300 {
1301         int error;
1302
1303         /*
1304          * Check if already stopped.
1305          *
1306          * NOTE: The "stopped" variable is only written when both the
1307          * priv's configuration lock and the SQ's lock is locked. It
1308          * can therefore safely be read when only one of the two locks
1309          * is locked. This function is always called when the priv's
1310          * configuration lock is locked.
1311          */
1312         if (sq->stopped != 0)
1313                 return;
1314
1315         mtx_lock(&sq->lock);
1316
1317         /* don't put more packets into the SQ */
1318         sq->stopped = 1;
1319
1320         /* teardown event factor timer, if any */
1321         sq->cev_next_state = MLX5E_CEV_STATE_HOLD_NOPS;
1322         callout_stop(&sq->cev_callout);
1323
1324         /* send dummy NOPs in order to flush the transmit ring */
1325         mlx5e_sq_send_nops_locked(sq, 1);
1326         mtx_unlock(&sq->lock);
1327
1328         /* make sure it is safe to free the callout */
1329         callout_drain(&sq->cev_callout);
1330
1331         /* wait till SQ is empty or link is down */
1332         mtx_lock(&sq->lock);
1333         while (sq->cc != sq->pc &&
1334             (sq->priv->media_status_last & IFM_ACTIVE) != 0) {
1335                 mtx_unlock(&sq->lock);
1336                 msleep(1);
1337                 sq->cq.mcq.comp(&sq->cq.mcq);
1338                 mtx_lock(&sq->lock);
1339         }
1340         mtx_unlock(&sq->lock);
1341
1342         /* error out remaining requests */
1343         error = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY, MLX5_SQC_STATE_ERR);
1344         if (error != 0) {
1345                 if_printf(sq->ifp,
1346                     "mlx5e_modify_sq() from RDY to ERR failed: %d\n", error);
1347         }
1348
1349         /* wait till SQ is empty */
1350         mtx_lock(&sq->lock);
1351         while (sq->cc != sq->pc) {
1352                 mtx_unlock(&sq->lock);
1353                 msleep(1);
1354                 sq->cq.mcq.comp(&sq->cq.mcq);
1355                 mtx_lock(&sq->lock);
1356         }
1357         mtx_unlock(&sq->lock);
1358 }
1359
1360 static void
1361 mlx5e_close_sq_wait(struct mlx5e_sq *sq)
1362 {
1363
1364         mlx5e_drain_sq(sq);
1365         mlx5e_disable_sq(sq);
1366         mlx5e_destroy_sq(sq);
1367 }
1368
1369 static int
1370 mlx5e_create_cq(struct mlx5e_priv *priv,
1371     struct mlx5e_cq_param *param,
1372     struct mlx5e_cq *cq,
1373     mlx5e_cq_comp_t *comp,
1374     int eq_ix)
1375 {
1376         struct mlx5_core_dev *mdev = priv->mdev;
1377         struct mlx5_core_cq *mcq = &cq->mcq;
1378         int eqn_not_used;
1379         int irqn;
1380         int err;
1381         u32 i;
1382
1383         param->wq.buf_numa_node = 0;
1384         param->wq.db_numa_node = 0;
1385
1386         err = mlx5_cqwq_create(mdev, &param->wq, param->cqc, &cq->wq,
1387             &cq->wq_ctrl);
1388         if (err)
1389                 return (err);
1390
1391         mlx5_vector2eqn(mdev, eq_ix, &eqn_not_used, &irqn);
1392
1393         mcq->cqe_sz = 64;
1394         mcq->set_ci_db = cq->wq_ctrl.db.db;
1395         mcq->arm_db = cq->wq_ctrl.db.db + 1;
1396         *mcq->set_ci_db = 0;
1397         *mcq->arm_db = 0;
1398         mcq->vector = eq_ix;
1399         mcq->comp = comp;
1400         mcq->event = mlx5e_cq_error_event;
1401         mcq->irqn = irqn;
1402         mcq->uar = &priv->cq_uar;
1403
1404         for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
1405                 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
1406
1407                 cqe->op_own = 0xf1;
1408         }
1409
1410         cq->priv = priv;
1411
1412         return (0);
1413 }
1414
1415 static void
1416 mlx5e_destroy_cq(struct mlx5e_cq *cq)
1417 {
1418         mlx5_wq_destroy(&cq->wq_ctrl);
1419 }
1420
1421 static int
1422 mlx5e_enable_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param, int eq_ix)
1423 {
1424         struct mlx5_core_cq *mcq = &cq->mcq;
1425         void *in;
1426         void *cqc;
1427         int inlen;
1428         int irqn_not_used;
1429         int eqn;
1430         int err;
1431
1432         inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
1433             sizeof(u64) * cq->wq_ctrl.buf.npages;
1434         in = mlx5_vzalloc(inlen);
1435         if (in == NULL)
1436                 return (-ENOMEM);
1437
1438         cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
1439
1440         memcpy(cqc, param->cqc, sizeof(param->cqc));
1441
1442         mlx5_fill_page_array(&cq->wq_ctrl.buf,
1443             (__be64 *) MLX5_ADDR_OF(create_cq_in, in, pas));
1444
1445         mlx5_vector2eqn(cq->priv->mdev, eq_ix, &eqn, &irqn_not_used);
1446
1447         MLX5_SET(cqc, cqc, c_eqn, eqn);
1448         MLX5_SET(cqc, cqc, uar_page, mcq->uar->index);
1449         MLX5_SET(cqc, cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
1450             PAGE_SHIFT);
1451         MLX5_SET64(cqc, cqc, dbr_addr, cq->wq_ctrl.db.dma);
1452
1453         err = mlx5_core_create_cq(cq->priv->mdev, mcq, in, inlen);
1454
1455         kvfree(in);
1456
1457         if (err)
1458                 return (err);
1459
1460         mlx5e_cq_arm(cq, MLX5_GET_DOORBELL_LOCK(&cq->priv->doorbell_lock));
1461
1462         return (0);
1463 }
1464
1465 static void
1466 mlx5e_disable_cq(struct mlx5e_cq *cq)
1467 {
1468
1469         mlx5_core_destroy_cq(cq->priv->mdev, &cq->mcq);
1470 }
1471
1472 int
1473 mlx5e_open_cq(struct mlx5e_priv *priv,
1474     struct mlx5e_cq_param *param,
1475     struct mlx5e_cq *cq,
1476     mlx5e_cq_comp_t *comp,
1477     int eq_ix)
1478 {
1479         int err;
1480
1481         err = mlx5e_create_cq(priv, param, cq, comp, eq_ix);
1482         if (err)
1483                 return (err);
1484
1485         err = mlx5e_enable_cq(cq, param, eq_ix);
1486         if (err)
1487                 goto err_destroy_cq;
1488
1489         return (0);
1490
1491 err_destroy_cq:
1492         mlx5e_destroy_cq(cq);
1493
1494         return (err);
1495 }
1496
1497 void
1498 mlx5e_close_cq(struct mlx5e_cq *cq)
1499 {
1500         mlx5e_disable_cq(cq);
1501         mlx5e_destroy_cq(cq);
1502 }
1503
1504 static int
1505 mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1506     struct mlx5e_channel_param *cparam)
1507 {
1508         int err;
1509         int tc;
1510
1511         for (tc = 0; tc < c->num_tc; tc++) {
1512                 /* open completion queue */
1513                 err = mlx5e_open_cq(c->priv, &cparam->tx_cq, &c->sq[tc].cq,
1514                     &mlx5e_tx_cq_comp, c->ix);
1515                 if (err)
1516                         goto err_close_tx_cqs;
1517         }
1518         return (0);
1519
1520 err_close_tx_cqs:
1521         for (tc--; tc >= 0; tc--)
1522                 mlx5e_close_cq(&c->sq[tc].cq);
1523
1524         return (err);
1525 }
1526
1527 static void
1528 mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1529 {
1530         int tc;
1531
1532         for (tc = 0; tc < c->num_tc; tc++)
1533                 mlx5e_close_cq(&c->sq[tc].cq);
1534 }
1535
1536 static int
1537 mlx5e_open_sqs(struct mlx5e_channel *c,
1538     struct mlx5e_channel_param *cparam)
1539 {
1540         int err;
1541         int tc;
1542
1543         for (tc = 0; tc < c->num_tc; tc++) {
1544                 err = mlx5e_open_sq(c, tc, &cparam->sq, &c->sq[tc]);
1545                 if (err)
1546                         goto err_close_sqs;
1547         }
1548
1549         return (0);
1550
1551 err_close_sqs:
1552         for (tc--; tc >= 0; tc--)
1553                 mlx5e_close_sq_wait(&c->sq[tc]);
1554
1555         return (err);
1556 }
1557
1558 static void
1559 mlx5e_close_sqs_wait(struct mlx5e_channel *c)
1560 {
1561         int tc;
1562
1563         for (tc = 0; tc < c->num_tc; tc++)
1564                 mlx5e_close_sq_wait(&c->sq[tc]);
1565 }
1566
1567 static void
1568 mlx5e_chan_mtx_init(struct mlx5e_channel *c)
1569 {
1570         int tc;
1571
1572         mtx_init(&c->rq.mtx, "mlx5rx", MTX_NETWORK_LOCK, MTX_DEF);
1573
1574         callout_init_mtx(&c->rq.watchdog, &c->rq.mtx, 0);
1575
1576         for (tc = 0; tc < c->num_tc; tc++) {
1577                 struct mlx5e_sq *sq = c->sq + tc;
1578
1579                 mtx_init(&sq->lock, "mlx5tx",
1580                     MTX_NETWORK_LOCK " TX", MTX_DEF);
1581                 mtx_init(&sq->comp_lock, "mlx5comp",
1582                     MTX_NETWORK_LOCK " TX", MTX_DEF);
1583
1584                 callout_init_mtx(&sq->cev_callout, &sq->lock, 0);
1585
1586                 sq->cev_factor = c->priv->params_ethtool.tx_completion_fact;
1587
1588                 /* ensure the TX completion event factor is not zero */
1589                 if (sq->cev_factor == 0)
1590                         sq->cev_factor = 1;
1591         }
1592 }
1593
1594 static void
1595 mlx5e_chan_mtx_destroy(struct mlx5e_channel *c)
1596 {
1597         int tc;
1598
1599         mtx_destroy(&c->rq.mtx);
1600
1601         for (tc = 0; tc < c->num_tc; tc++) {
1602                 mtx_destroy(&c->sq[tc].lock);
1603                 mtx_destroy(&c->sq[tc].comp_lock);
1604         }
1605 }
1606
1607 static int
1608 mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1609     struct mlx5e_channel_param *cparam,
1610     struct mlx5e_channel *volatile *cp)
1611 {
1612         struct mlx5e_channel *c;
1613         int err;
1614
1615         c = malloc(sizeof(*c), M_MLX5EN, M_WAITOK | M_ZERO);
1616         c->priv = priv;
1617         c->ix = ix;
1618         c->cpu = 0;
1619         c->ifp = priv->ifp;
1620         c->mkey_be = cpu_to_be32(priv->mr.key);
1621         c->num_tc = priv->num_tc;
1622
1623         /* init mutexes */
1624         mlx5e_chan_mtx_init(c);
1625
1626         /* open transmit completion queue */
1627         err = mlx5e_open_tx_cqs(c, cparam);
1628         if (err)
1629                 goto err_free;
1630
1631         /* open receive completion queue */
1632         err = mlx5e_open_cq(c->priv, &cparam->rx_cq, &c->rq.cq,
1633             &mlx5e_rx_cq_comp, c->ix);
1634         if (err)
1635                 goto err_close_tx_cqs;
1636
1637         err = mlx5e_open_sqs(c, cparam);
1638         if (err)
1639                 goto err_close_rx_cq;
1640
1641         err = mlx5e_open_rq(c, &cparam->rq, &c->rq);
1642         if (err)
1643                 goto err_close_sqs;
1644
1645         /* store channel pointer */
1646         *cp = c;
1647
1648         /* poll receive queue initially */
1649         c->rq.cq.mcq.comp(&c->rq.cq.mcq);
1650
1651         return (0);
1652
1653 err_close_sqs:
1654         mlx5e_close_sqs_wait(c);
1655
1656 err_close_rx_cq:
1657         mlx5e_close_cq(&c->rq.cq);
1658
1659 err_close_tx_cqs:
1660         mlx5e_close_tx_cqs(c);
1661
1662 err_free:
1663         /* destroy mutexes */
1664         mlx5e_chan_mtx_destroy(c);
1665         free(c, M_MLX5EN);
1666         return (err);
1667 }
1668
1669 static void
1670 mlx5e_close_channel(struct mlx5e_channel *volatile *pp)
1671 {
1672         struct mlx5e_channel *c = *pp;
1673
1674         /* check if channel is already closed */
1675         if (c == NULL)
1676                 return;
1677         mlx5e_close_rq(&c->rq);
1678 }
1679
1680 static void
1681 mlx5e_close_channel_wait(struct mlx5e_channel *volatile *pp)
1682 {
1683         struct mlx5e_channel *c = *pp;
1684
1685         /* check if channel is already closed */
1686         if (c == NULL)
1687                 return;
1688         /* ensure channel pointer is no longer used */
1689         *pp = NULL;
1690
1691         mlx5e_close_rq_wait(&c->rq);
1692         mlx5e_close_sqs_wait(c);
1693         mlx5e_close_cq(&c->rq.cq);
1694         mlx5e_close_tx_cqs(c);
1695         /* destroy mutexes */
1696         mlx5e_chan_mtx_destroy(c);
1697         free(c, M_MLX5EN);
1698 }
1699
1700 static void
1701 mlx5e_build_rq_param(struct mlx5e_priv *priv,
1702     struct mlx5e_rq_param *param)
1703 {
1704         void *rqc = param->rqc;
1705         void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1706
1707         MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1708         MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1709         MLX5_SET(wq, wq, log_wq_stride, ilog2(sizeof(struct mlx5e_rx_wqe)));
1710         MLX5_SET(wq, wq, log_wq_sz, priv->params.log_rq_size);
1711         MLX5_SET(wq, wq, pd, priv->pdn);
1712
1713         param->wq.buf_numa_node = 0;
1714         param->wq.db_numa_node = 0;
1715         param->wq.linear = 1;
1716 }
1717
1718 static void
1719 mlx5e_build_sq_param(struct mlx5e_priv *priv,
1720     struct mlx5e_sq_param *param)
1721 {
1722         void *sqc = param->sqc;
1723         void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1724
1725         MLX5_SET(wq, wq, log_wq_sz, priv->params.log_sq_size);
1726         MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1727         MLX5_SET(wq, wq, pd, priv->pdn);
1728
1729         param->wq.buf_numa_node = 0;
1730         param->wq.db_numa_node = 0;
1731         param->wq.linear = 1;
1732 }
1733
1734 static void
1735 mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1736     struct mlx5e_cq_param *param)
1737 {
1738         void *cqc = param->cqc;
1739
1740         MLX5_SET(cqc, cqc, uar_page, priv->cq_uar.index);
1741 }
1742
1743 static void
1744 mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
1745     struct mlx5e_cq_param *param)
1746 {
1747         void *cqc = param->cqc;
1748
1749
1750         /*
1751          * TODO The sysctl to control on/off is a bool value for now, which means
1752          * we only support CSUM, once HASH is implemnted we'll need to address that.
1753          */
1754         if (priv->params.cqe_zipping_en) {
1755                 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
1756                 MLX5_SET(cqc, cqc, cqe_compression_en, 1);
1757         }
1758
1759         MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_rq_size);
1760         MLX5_SET(cqc, cqc, cq_period, priv->params.rx_cq_moderation_usec);
1761         MLX5_SET(cqc, cqc, cq_max_count, priv->params.rx_cq_moderation_pkts);
1762
1763         switch (priv->params.rx_cq_moderation_mode) {
1764         case 0:
1765                 MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
1766                 break;
1767         default:
1768                 if (MLX5_CAP_GEN(priv->mdev, cq_period_start_from_cqe))
1769                         MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
1770                 else
1771                         MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
1772                 break;
1773         }
1774
1775         mlx5e_build_common_cq_param(priv, param);
1776 }
1777
1778 static void
1779 mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
1780     struct mlx5e_cq_param *param)
1781 {
1782         void *cqc = param->cqc;
1783
1784         MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_sq_size);
1785         MLX5_SET(cqc, cqc, cq_period, priv->params.tx_cq_moderation_usec);
1786         MLX5_SET(cqc, cqc, cq_max_count, priv->params.tx_cq_moderation_pkts);
1787
1788         switch (priv->params.tx_cq_moderation_mode) {
1789         case 0:
1790                 MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
1791                 break;
1792         default:
1793                 if (MLX5_CAP_GEN(priv->mdev, cq_period_start_from_cqe))
1794                         MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
1795                 else
1796                         MLX5_SET(cqc, cqc, cq_period_mode, MLX5_CQ_PERIOD_MODE_START_FROM_EQE);
1797                 break;
1798         }
1799
1800         mlx5e_build_common_cq_param(priv, param);
1801 }
1802
1803 static void
1804 mlx5e_build_channel_param(struct mlx5e_priv *priv,
1805     struct mlx5e_channel_param *cparam)
1806 {
1807         memset(cparam, 0, sizeof(*cparam));
1808
1809         mlx5e_build_rq_param(priv, &cparam->rq);
1810         mlx5e_build_sq_param(priv, &cparam->sq);
1811         mlx5e_build_rx_cq_param(priv, &cparam->rx_cq);
1812         mlx5e_build_tx_cq_param(priv, &cparam->tx_cq);
1813 }
1814
1815 static int
1816 mlx5e_open_channels(struct mlx5e_priv *priv)
1817 {
1818         struct mlx5e_channel_param cparam;
1819         void *ptr;
1820         int err;
1821         int i;
1822         int j;
1823
1824         priv->channel = malloc(priv->params.num_channels *
1825             sizeof(struct mlx5e_channel *), M_MLX5EN, M_WAITOK | M_ZERO);
1826
1827         mlx5e_build_channel_param(priv, &cparam);
1828         for (i = 0; i < priv->params.num_channels; i++) {
1829                 err = mlx5e_open_channel(priv, i, &cparam, &priv->channel[i]);
1830                 if (err)
1831                         goto err_close_channels;
1832         }
1833
1834         for (j = 0; j < priv->params.num_channels; j++) {
1835                 err = mlx5e_wait_for_min_rx_wqes(&priv->channel[j]->rq);
1836                 if (err)
1837                         goto err_close_channels;
1838         }
1839
1840         return (0);
1841
1842 err_close_channels:
1843         for (i--; i >= 0; i--) {
1844                 mlx5e_close_channel(&priv->channel[i]);
1845                 mlx5e_close_channel_wait(&priv->channel[i]);
1846         }
1847
1848         /* remove "volatile" attribute from "channel" pointer */
1849         ptr = __DECONST(void *, priv->channel);
1850         priv->channel = NULL;
1851
1852         free(ptr, M_MLX5EN);
1853
1854         return (err);
1855 }
1856
1857 static void
1858 mlx5e_close_channels(struct mlx5e_priv *priv)
1859 {
1860         void *ptr;
1861         int i;
1862
1863         if (priv->channel == NULL)
1864                 return;
1865
1866         for (i = 0; i < priv->params.num_channels; i++)
1867                 mlx5e_close_channel(&priv->channel[i]);
1868         for (i = 0; i < priv->params.num_channels; i++)
1869                 mlx5e_close_channel_wait(&priv->channel[i]);
1870
1871         /* remove "volatile" attribute from "channel" pointer */
1872         ptr = __DECONST(void *, priv->channel);
1873         priv->channel = NULL;
1874
1875         free(ptr, M_MLX5EN);
1876 }
1877
1878 static int
1879 mlx5e_refresh_sq_params(struct mlx5e_priv *priv, struct mlx5e_sq *sq)
1880 {
1881
1882         if (MLX5_CAP_GEN(priv->mdev, cq_period_mode_modify)) {
1883                 uint8_t cq_mode;
1884
1885                 switch (priv->params.tx_cq_moderation_mode) {
1886                 case 0:
1887                         cq_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1888                         break;
1889                 default:
1890                         cq_mode = MLX5_CQ_PERIOD_MODE_START_FROM_CQE;
1891                         break;
1892                 }
1893
1894                 return (mlx5_core_modify_cq_moderation_mode(priv->mdev, &sq->cq.mcq,
1895                     priv->params.tx_cq_moderation_usec,
1896                     priv->params.tx_cq_moderation_pkts,
1897                     cq_mode));
1898         }
1899
1900         return (mlx5_core_modify_cq_moderation(priv->mdev, &sq->cq.mcq,
1901             priv->params.tx_cq_moderation_usec,
1902             priv->params.tx_cq_moderation_pkts));
1903 }
1904
1905 static int
1906 mlx5e_refresh_rq_params(struct mlx5e_priv *priv, struct mlx5e_rq *rq)
1907 {
1908
1909         if (MLX5_CAP_GEN(priv->mdev, cq_period_mode_modify)) {
1910                 uint8_t cq_mode;
1911                 int retval;
1912
1913                 switch (priv->params.rx_cq_moderation_mode) {
1914                 case 0:
1915                         cq_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1916                         break;
1917                 default:
1918                         cq_mode = MLX5_CQ_PERIOD_MODE_START_FROM_CQE;
1919                         break;
1920                 }
1921
1922                 retval = mlx5_core_modify_cq_moderation_mode(priv->mdev, &rq->cq.mcq,
1923                     priv->params.rx_cq_moderation_usec,
1924                     priv->params.rx_cq_moderation_pkts,
1925                     cq_mode);
1926
1927                 return (retval);
1928         }
1929
1930         return (mlx5_core_modify_cq_moderation(priv->mdev, &rq->cq.mcq,
1931             priv->params.rx_cq_moderation_usec,
1932             priv->params.rx_cq_moderation_pkts));
1933 }
1934
1935 static int
1936 mlx5e_refresh_channel_params_sub(struct mlx5e_priv *priv, struct mlx5e_channel *c)
1937 {
1938         int err;
1939         int i;
1940
1941         if (c == NULL)
1942                 return (EINVAL);
1943
1944         err = mlx5e_refresh_rq_params(priv, &c->rq);
1945         if (err)
1946                 goto done;
1947
1948         for (i = 0; i != c->num_tc; i++) {
1949                 err = mlx5e_refresh_sq_params(priv, &c->sq[i]);
1950                 if (err)
1951                         goto done;
1952         }
1953 done:
1954         return (err);
1955 }
1956
1957 int
1958 mlx5e_refresh_channel_params(struct mlx5e_priv *priv)
1959 {
1960         int i;
1961
1962         if (priv->channel == NULL)
1963                 return (EINVAL);
1964
1965         for (i = 0; i < priv->params.num_channels; i++) {
1966                 int err;
1967
1968                 err = mlx5e_refresh_channel_params_sub(priv, priv->channel[i]);
1969                 if (err)
1970                         return (err);
1971         }
1972         return (0);
1973 }
1974
1975 static int
1976 mlx5e_open_tis(struct mlx5e_priv *priv, int tc)
1977 {
1978         struct mlx5_core_dev *mdev = priv->mdev;
1979         u32 in[MLX5_ST_SZ_DW(create_tis_in)];
1980         void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
1981
1982         memset(in, 0, sizeof(in));
1983
1984         MLX5_SET(tisc, tisc, prio, tc);
1985         MLX5_SET(tisc, tisc, transport_domain, priv->tdn);
1986
1987         return (mlx5_core_create_tis(mdev, in, sizeof(in), &priv->tisn[tc]));
1988 }
1989
1990 static void
1991 mlx5e_close_tis(struct mlx5e_priv *priv, int tc)
1992 {
1993         mlx5_core_destroy_tis(priv->mdev, priv->tisn[tc]);
1994 }
1995
1996 static int
1997 mlx5e_open_tises(struct mlx5e_priv *priv)
1998 {
1999         int num_tc = priv->num_tc;
2000         int err;
2001         int tc;
2002
2003         for (tc = 0; tc < num_tc; tc++) {
2004                 err = mlx5e_open_tis(priv, tc);
2005                 if (err)
2006                         goto err_close_tises;
2007         }
2008
2009         return (0);
2010
2011 err_close_tises:
2012         for (tc--; tc >= 0; tc--)
2013                 mlx5e_close_tis(priv, tc);
2014
2015         return (err);
2016 }
2017
2018 static void
2019 mlx5e_close_tises(struct mlx5e_priv *priv)
2020 {
2021         int num_tc = priv->num_tc;
2022         int tc;
2023
2024         for (tc = 0; tc < num_tc; tc++)
2025                 mlx5e_close_tis(priv, tc);
2026 }
2027
2028 static int
2029 mlx5e_open_rqt(struct mlx5e_priv *priv)
2030 {
2031         struct mlx5_core_dev *mdev = priv->mdev;
2032         u32 *in;
2033         u32 out[MLX5_ST_SZ_DW(create_rqt_out)] = {0};
2034         void *rqtc;
2035         int inlen;
2036         int err;
2037         int sz;
2038         int i;
2039
2040         sz = 1 << priv->params.rx_hash_log_tbl_sz;
2041
2042         inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
2043         in = mlx5_vzalloc(inlen);
2044         if (in == NULL)
2045                 return (-ENOMEM);
2046         rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
2047
2048         MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
2049         MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
2050
2051         for (i = 0; i < sz; i++) {
2052                 int ix;
2053 #ifdef RSS
2054                 ix = rss_get_indirection_to_bucket(i);
2055 #else
2056                 ix = i;
2057 #endif
2058                 /* ensure we don't overflow */
2059                 ix %= priv->params.num_channels;
2060                 MLX5_SET(rqtc, rqtc, rq_num[i], priv->channel[ix]->rq.rqn);
2061         }
2062
2063         MLX5_SET(create_rqt_in, in, opcode, MLX5_CMD_OP_CREATE_RQT);
2064
2065         err = mlx5_cmd_exec(mdev, in, inlen, out, sizeof(out));
2066         if (!err)
2067                 priv->rqtn = MLX5_GET(create_rqt_out, out, rqtn);
2068
2069         kvfree(in);
2070
2071         return (err);
2072 }
2073
2074 static void
2075 mlx5e_close_rqt(struct mlx5e_priv *priv)
2076 {
2077         u32 in[MLX5_ST_SZ_DW(destroy_rqt_in)] = {0};
2078         u32 out[MLX5_ST_SZ_DW(destroy_rqt_out)] = {0};
2079
2080         MLX5_SET(destroy_rqt_in, in, opcode, MLX5_CMD_OP_DESTROY_RQT);
2081         MLX5_SET(destroy_rqt_in, in, rqtn, priv->rqtn);
2082
2083         mlx5_cmd_exec(priv->mdev, in, sizeof(in), out, sizeof(out));
2084 }
2085
2086 static void
2087 mlx5e_build_tir_ctx(struct mlx5e_priv *priv, u32 * tirc, int tt)
2088 {
2089         void *hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2090         __be32 *hkey;
2091
2092         MLX5_SET(tirc, tirc, transport_domain, priv->tdn);
2093
2094 #define ROUGH_MAX_L2_L3_HDR_SZ 256
2095
2096 #define MLX5_HASH_IP     (MLX5_HASH_FIELD_SEL_SRC_IP   |\
2097                           MLX5_HASH_FIELD_SEL_DST_IP)
2098
2099 #define MLX5_HASH_ALL    (MLX5_HASH_FIELD_SEL_SRC_IP   |\
2100                           MLX5_HASH_FIELD_SEL_DST_IP   |\
2101                           MLX5_HASH_FIELD_SEL_L4_SPORT |\
2102                           MLX5_HASH_FIELD_SEL_L4_DPORT)
2103
2104 #define MLX5_HASH_IP_IPSEC_SPI  (MLX5_HASH_FIELD_SEL_SRC_IP   |\
2105                                  MLX5_HASH_FIELD_SEL_DST_IP   |\
2106                                  MLX5_HASH_FIELD_SEL_IPSEC_SPI)
2107
2108         if (priv->params.hw_lro_en) {
2109                 MLX5_SET(tirc, tirc, lro_enable_mask,
2110                     MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
2111                     MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
2112                 MLX5_SET(tirc, tirc, lro_max_msg_sz,
2113                     (priv->params.lro_wqe_sz -
2114                     ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
2115                 /* TODO: add the option to choose timer value dynamically */
2116                 MLX5_SET(tirc, tirc, lro_timeout_period_usecs,
2117                     MLX5_CAP_ETH(priv->mdev,
2118                     lro_timer_supported_periods[2]));
2119         }
2120
2121         /* setup parameters for hashing TIR type, if any */
2122         switch (tt) {
2123         case MLX5E_TT_ANY:
2124                 MLX5_SET(tirc, tirc, disp_type,
2125                     MLX5_TIRC_DISP_TYPE_DIRECT);
2126                 MLX5_SET(tirc, tirc, inline_rqn,
2127                     priv->channel[0]->rq.rqn);
2128                 break;
2129         default:
2130                 MLX5_SET(tirc, tirc, disp_type,
2131                     MLX5_TIRC_DISP_TYPE_INDIRECT);
2132                 MLX5_SET(tirc, tirc, indirect_table,
2133                     priv->rqtn);
2134                 MLX5_SET(tirc, tirc, rx_hash_fn,
2135                     MLX5_TIRC_RX_HASH_FN_HASH_TOEPLITZ);
2136                 hkey = (__be32 *) MLX5_ADDR_OF(tirc, tirc, rx_hash_toeplitz_key);
2137 #ifdef RSS
2138                 /*
2139                  * The FreeBSD RSS implementation does currently not
2140                  * support symmetric Toeplitz hashes:
2141                  */
2142                 MLX5_SET(tirc, tirc, rx_hash_symmetric, 0);
2143                 rss_getkey((uint8_t *)hkey);
2144 #else
2145                 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
2146                 hkey[0] = cpu_to_be32(0xD181C62C);
2147                 hkey[1] = cpu_to_be32(0xF7F4DB5B);
2148                 hkey[2] = cpu_to_be32(0x1983A2FC);
2149                 hkey[3] = cpu_to_be32(0x943E1ADB);
2150                 hkey[4] = cpu_to_be32(0xD9389E6B);
2151                 hkey[5] = cpu_to_be32(0xD1039C2C);
2152                 hkey[6] = cpu_to_be32(0xA74499AD);
2153                 hkey[7] = cpu_to_be32(0x593D56D9);
2154                 hkey[8] = cpu_to_be32(0xF3253C06);
2155                 hkey[9] = cpu_to_be32(0x2ADC1FFC);
2156 #endif
2157                 break;
2158         }
2159
2160         switch (tt) {
2161         case MLX5E_TT_IPV4_TCP:
2162                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2163                     MLX5_L3_PROT_TYPE_IPV4);
2164                 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2165                     MLX5_L4_PROT_TYPE_TCP);
2166 #ifdef RSS
2167                 if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV4)) {
2168                         MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2169                             MLX5_HASH_IP);
2170                 } else
2171 #endif
2172                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2173                     MLX5_HASH_ALL);
2174                 break;
2175
2176         case MLX5E_TT_IPV6_TCP:
2177                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2178                     MLX5_L3_PROT_TYPE_IPV6);
2179                 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2180                     MLX5_L4_PROT_TYPE_TCP);
2181 #ifdef RSS
2182                 if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_TCP_IPV6)) {
2183                         MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2184                             MLX5_HASH_IP);
2185                 } else
2186 #endif
2187                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2188                     MLX5_HASH_ALL);
2189                 break;
2190
2191         case MLX5E_TT_IPV4_UDP:
2192                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2193                     MLX5_L3_PROT_TYPE_IPV4);
2194                 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2195                     MLX5_L4_PROT_TYPE_UDP);
2196 #ifdef RSS
2197                 if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV4)) {
2198                         MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2199                             MLX5_HASH_IP);
2200                 } else
2201 #endif
2202                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2203                     MLX5_HASH_ALL);
2204                 break;
2205
2206         case MLX5E_TT_IPV6_UDP:
2207                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2208                     MLX5_L3_PROT_TYPE_IPV6);
2209                 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2210                     MLX5_L4_PROT_TYPE_UDP);
2211 #ifdef RSS
2212                 if (!(rss_gethashconfig() & RSS_HASHTYPE_RSS_UDP_IPV6)) {
2213                         MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2214                             MLX5_HASH_IP);
2215                 } else
2216 #endif
2217                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2218                     MLX5_HASH_ALL);
2219                 break;
2220
2221         case MLX5E_TT_IPV4_IPSEC_AH:
2222                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2223                     MLX5_L3_PROT_TYPE_IPV4);
2224                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2225                     MLX5_HASH_IP_IPSEC_SPI);
2226                 break;
2227
2228         case MLX5E_TT_IPV6_IPSEC_AH:
2229                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2230                     MLX5_L3_PROT_TYPE_IPV6);
2231                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2232                     MLX5_HASH_IP_IPSEC_SPI);
2233                 break;
2234
2235         case MLX5E_TT_IPV4_IPSEC_ESP:
2236                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2237                     MLX5_L3_PROT_TYPE_IPV4);
2238                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2239                     MLX5_HASH_IP_IPSEC_SPI);
2240                 break;
2241
2242         case MLX5E_TT_IPV6_IPSEC_ESP:
2243                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2244                     MLX5_L3_PROT_TYPE_IPV6);
2245                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2246                     MLX5_HASH_IP_IPSEC_SPI);
2247                 break;
2248
2249         case MLX5E_TT_IPV4:
2250                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2251                     MLX5_L3_PROT_TYPE_IPV4);
2252                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2253                     MLX5_HASH_IP);
2254                 break;
2255
2256         case MLX5E_TT_IPV6:
2257                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2258                     MLX5_L3_PROT_TYPE_IPV6);
2259                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2260                     MLX5_HASH_IP);
2261                 break;
2262
2263         default:
2264                 break;
2265         }
2266 }
2267
2268 static int
2269 mlx5e_open_tir(struct mlx5e_priv *priv, int tt)
2270 {
2271         struct mlx5_core_dev *mdev = priv->mdev;
2272         u32 *in;
2273         void *tirc;
2274         int inlen;
2275         int err;
2276
2277         inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2278         in = mlx5_vzalloc(inlen);
2279         if (in == NULL)
2280                 return (-ENOMEM);
2281         tirc = MLX5_ADDR_OF(create_tir_in, in, tir_context);
2282
2283         mlx5e_build_tir_ctx(priv, tirc, tt);
2284
2285         err = mlx5_core_create_tir(mdev, in, inlen, &priv->tirn[tt]);
2286
2287         kvfree(in);
2288
2289         return (err);
2290 }
2291
2292 static void
2293 mlx5e_close_tir(struct mlx5e_priv *priv, int tt)
2294 {
2295         mlx5_core_destroy_tir(priv->mdev, priv->tirn[tt]);
2296 }
2297
2298 static int
2299 mlx5e_open_tirs(struct mlx5e_priv *priv)
2300 {
2301         int err;
2302         int i;
2303
2304         for (i = 0; i < MLX5E_NUM_TT; i++) {
2305                 err = mlx5e_open_tir(priv, i);
2306                 if (err)
2307                         goto err_close_tirs;
2308         }
2309
2310         return (0);
2311
2312 err_close_tirs:
2313         for (i--; i >= 0; i--)
2314                 mlx5e_close_tir(priv, i);
2315
2316         return (err);
2317 }
2318
2319 static void
2320 mlx5e_close_tirs(struct mlx5e_priv *priv)
2321 {
2322         int i;
2323
2324         for (i = 0; i < MLX5E_NUM_TT; i++)
2325                 mlx5e_close_tir(priv, i);
2326 }
2327
2328 /*
2329  * SW MTU does not include headers,
2330  * HW MTU includes all headers and checksums.
2331  */
2332 static int
2333 mlx5e_set_dev_port_mtu(struct ifnet *ifp, int sw_mtu)
2334 {
2335         struct mlx5e_priv *priv = ifp->if_softc;
2336         struct mlx5_core_dev *mdev = priv->mdev;
2337         int hw_mtu;
2338         int err;
2339
2340         hw_mtu = MLX5E_SW2HW_MTU(sw_mtu);
2341
2342         err = mlx5_set_port_mtu(mdev, hw_mtu);
2343         if (err) {
2344                 if_printf(ifp, "%s: mlx5_set_port_mtu failed setting %d, err=%d\n",
2345                     __func__, sw_mtu, err);
2346                 return (err);
2347         }
2348
2349         /* Update vport context MTU */
2350         err = mlx5_set_vport_mtu(mdev, hw_mtu);
2351         if (err) {
2352                 if_printf(ifp, "%s: Failed updating vport context with MTU size, err=%d\n",
2353                     __func__, err);
2354         }
2355
2356         ifp->if_mtu = sw_mtu;
2357
2358         err = mlx5_query_vport_mtu(mdev, &hw_mtu);
2359         if (err || !hw_mtu) {
2360                 /* fallback to port oper mtu */
2361                 err = mlx5_query_port_oper_mtu(mdev, &hw_mtu);
2362         }
2363         if (err) {
2364                 if_printf(ifp, "Query port MTU, after setting new "
2365                     "MTU value, failed\n");
2366                 return (err);
2367         } else if (MLX5E_HW2SW_MTU(hw_mtu) < sw_mtu) {
2368                 err = -E2BIG,
2369                 if_printf(ifp, "Port MTU %d is smaller than "
2370                     "ifp mtu %d\n", hw_mtu, sw_mtu);
2371         } else if (MLX5E_HW2SW_MTU(hw_mtu) > sw_mtu) {
2372                 err = -EINVAL;
2373                 if_printf(ifp, "Port MTU %d is bigger than "
2374                     "ifp mtu %d\n", hw_mtu, sw_mtu);
2375         }
2376         priv->params_ethtool.hw_mtu = hw_mtu;
2377
2378         return (err);
2379 }
2380
2381 int
2382 mlx5e_open_locked(struct ifnet *ifp)
2383 {
2384         struct mlx5e_priv *priv = ifp->if_softc;
2385         int err;
2386         u16 set_id;
2387
2388         /* check if already opened */
2389         if (test_bit(MLX5E_STATE_OPENED, &priv->state) != 0)
2390                 return (0);
2391
2392 #ifdef RSS
2393         if (rss_getnumbuckets() > priv->params.num_channels) {
2394                 if_printf(ifp, "NOTE: There are more RSS buckets(%u) than "
2395                     "channels(%u) available\n", rss_getnumbuckets(),
2396                     priv->params.num_channels);
2397         }
2398 #endif
2399         err = mlx5e_open_tises(priv);
2400         if (err) {
2401                 if_printf(ifp, "%s: mlx5e_open_tises failed, %d\n",
2402                     __func__, err);
2403                 return (err);
2404         }
2405         err = mlx5_vport_alloc_q_counter(priv->mdev,
2406             MLX5_INTERFACE_PROTOCOL_ETH, &set_id);
2407         if (err) {
2408                 if_printf(priv->ifp,
2409                     "%s: mlx5_vport_alloc_q_counter failed: %d\n",
2410                     __func__, err);
2411                 goto err_close_tises;
2412         }
2413         /* store counter set ID */
2414         priv->counter_set_id = set_id;
2415
2416         err = mlx5e_open_channels(priv);
2417         if (err) {
2418                 if_printf(ifp, "%s: mlx5e_open_channels failed, %d\n",
2419                     __func__, err);
2420                 goto err_dalloc_q_counter;
2421         }
2422         err = mlx5e_open_rqt(priv);
2423         if (err) {
2424                 if_printf(ifp, "%s: mlx5e_open_rqt failed, %d\n",
2425                     __func__, err);
2426                 goto err_close_channels;
2427         }
2428         err = mlx5e_open_tirs(priv);
2429         if (err) {
2430                 if_printf(ifp, "%s: mlx5e_open_tir failed, %d\n",
2431                     __func__, err);
2432                 goto err_close_rqls;
2433         }
2434         err = mlx5e_open_flow_table(priv);
2435         if (err) {
2436                 if_printf(ifp, "%s: mlx5e_open_flow_table failed, %d\n",
2437                     __func__, err);
2438                 goto err_close_tirs;
2439         }
2440         err = mlx5e_add_all_vlan_rules(priv);
2441         if (err) {
2442                 if_printf(ifp, "%s: mlx5e_add_all_vlan_rules failed, %d\n",
2443                     __func__, err);
2444                 goto err_close_flow_table;
2445         }
2446         set_bit(MLX5E_STATE_OPENED, &priv->state);
2447
2448         mlx5e_update_carrier(priv);
2449         mlx5e_set_rx_mode_core(priv);
2450
2451         return (0);
2452
2453 err_close_flow_table:
2454         mlx5e_close_flow_table(priv);
2455
2456 err_close_tirs:
2457         mlx5e_close_tirs(priv);
2458
2459 err_close_rqls:
2460         mlx5e_close_rqt(priv);
2461
2462 err_close_channels:
2463         mlx5e_close_channels(priv);
2464
2465 err_dalloc_q_counter:
2466         mlx5_vport_dealloc_q_counter(priv->mdev,
2467             MLX5_INTERFACE_PROTOCOL_ETH, priv->counter_set_id);
2468
2469 err_close_tises:
2470         mlx5e_close_tises(priv);
2471
2472         return (err);
2473 }
2474
2475 static void
2476 mlx5e_open(void *arg)
2477 {
2478         struct mlx5e_priv *priv = arg;
2479
2480         PRIV_LOCK(priv);
2481         if (mlx5_set_port_status(priv->mdev, MLX5_PORT_UP))
2482                 if_printf(priv->ifp,
2483                     "%s: Setting port status to up failed\n",
2484                     __func__);
2485
2486         mlx5e_open_locked(priv->ifp);
2487         priv->ifp->if_drv_flags |= IFF_DRV_RUNNING;
2488         PRIV_UNLOCK(priv);
2489 }
2490
2491 int
2492 mlx5e_close_locked(struct ifnet *ifp)
2493 {
2494         struct mlx5e_priv *priv = ifp->if_softc;
2495
2496         /* check if already closed */
2497         if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
2498                 return (0);
2499
2500         clear_bit(MLX5E_STATE_OPENED, &priv->state);
2501
2502         mlx5e_set_rx_mode_core(priv);
2503         mlx5e_del_all_vlan_rules(priv);
2504         if_link_state_change(priv->ifp, LINK_STATE_DOWN);
2505         mlx5e_close_flow_table(priv);
2506         mlx5e_close_tirs(priv);
2507         mlx5e_close_rqt(priv);
2508         mlx5e_close_channels(priv);
2509         mlx5_vport_dealloc_q_counter(priv->mdev,
2510             MLX5_INTERFACE_PROTOCOL_ETH, priv->counter_set_id);
2511         mlx5e_close_tises(priv);
2512
2513         return (0);
2514 }
2515
2516 #if (__FreeBSD_version >= 1100000)
2517 static uint64_t
2518 mlx5e_get_counter(struct ifnet *ifp, ift_counter cnt)
2519 {
2520         struct mlx5e_priv *priv = ifp->if_softc;
2521         u64 retval;
2522
2523         /* PRIV_LOCK(priv); XXX not allowed */
2524         switch (cnt) {
2525         case IFCOUNTER_IPACKETS:
2526                 retval = priv->stats.vport.rx_packets;
2527                 break;
2528         case IFCOUNTER_IERRORS:
2529                 retval = priv->stats.vport.rx_error_packets +
2530                     priv->stats.pport.alignment_err +
2531                     priv->stats.pport.check_seq_err +
2532                     priv->stats.pport.crc_align_errors +
2533                     priv->stats.pport.in_range_len_errors +
2534                     priv->stats.pport.jabbers +
2535                     priv->stats.pport.out_of_range_len +
2536                     priv->stats.pport.oversize_pkts +
2537                     priv->stats.pport.symbol_err +
2538                     priv->stats.pport.too_long_errors +
2539                     priv->stats.pport.undersize_pkts +
2540                     priv->stats.pport.unsupported_op_rx;
2541                 break;
2542         case IFCOUNTER_IQDROPS:
2543                 retval = priv->stats.vport.rx_out_of_buffer +
2544                     priv->stats.pport.drop_events;
2545                 break;
2546         case IFCOUNTER_OPACKETS:
2547                 retval = priv->stats.vport.tx_packets;
2548                 break;
2549         case IFCOUNTER_OERRORS:
2550                 retval = priv->stats.vport.tx_error_packets;
2551                 break;
2552         case IFCOUNTER_IBYTES:
2553                 retval = priv->stats.vport.rx_bytes;
2554                 break;
2555         case IFCOUNTER_OBYTES:
2556                 retval = priv->stats.vport.tx_bytes;
2557                 break;
2558         case IFCOUNTER_IMCASTS:
2559                 retval = priv->stats.vport.rx_multicast_packets;
2560                 break;
2561         case IFCOUNTER_OMCASTS:
2562                 retval = priv->stats.vport.tx_multicast_packets;
2563                 break;
2564         case IFCOUNTER_OQDROPS:
2565                 retval = priv->stats.vport.tx_queue_dropped;
2566                 break;
2567         case IFCOUNTER_COLLISIONS:
2568                 retval = priv->stats.pport.collisions;
2569                 break;
2570         default:
2571                 retval = if_get_counter_default(ifp, cnt);
2572                 break;
2573         }
2574         /* PRIV_UNLOCK(priv); XXX not allowed */
2575         return (retval);
2576 }
2577 #endif
2578
2579 static void
2580 mlx5e_set_rx_mode(struct ifnet *ifp)
2581 {
2582         struct mlx5e_priv *priv = ifp->if_softc;
2583
2584         queue_work(priv->wq, &priv->set_rx_mode_work);
2585 }
2586
2587 static int
2588 mlx5e_ioctl(struct ifnet *ifp, u_long command, caddr_t data)
2589 {
2590         struct mlx5e_priv *priv;
2591         struct ifreq *ifr;
2592         struct ifi2creq i2c;
2593         int error = 0;
2594         int mask = 0;
2595         int size_read = 0;
2596         int module_status;
2597         int module_num;
2598         int max_mtu;
2599         uint8_t read_addr;
2600
2601         priv = ifp->if_softc;
2602
2603         /* check if detaching */
2604         if (priv == NULL || priv->gone != 0)
2605                 return (ENXIO);
2606
2607         switch (command) {
2608         case SIOCSIFMTU:
2609                 ifr = (struct ifreq *)data;
2610
2611                 PRIV_LOCK(priv);
2612                 mlx5_query_port_max_mtu(priv->mdev, &max_mtu);
2613
2614                 if (ifr->ifr_mtu >= MLX5E_MTU_MIN &&
2615                     ifr->ifr_mtu <= MIN(MLX5E_MTU_MAX, max_mtu)) {
2616                         int was_opened;
2617
2618                         was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2619                         if (was_opened)
2620                                 mlx5e_close_locked(ifp);
2621
2622                         /* set new MTU */
2623                         mlx5e_set_dev_port_mtu(ifp, ifr->ifr_mtu);
2624
2625                         if (was_opened)
2626                                 mlx5e_open_locked(ifp);
2627                 } else {
2628                         error = EINVAL;
2629                         if_printf(ifp, "Invalid MTU value. Min val: %d, Max val: %d\n",
2630                             MLX5E_MTU_MIN, MIN(MLX5E_MTU_MAX, max_mtu));
2631                 }
2632                 PRIV_UNLOCK(priv);
2633                 break;
2634         case SIOCSIFFLAGS:
2635                 if ((ifp->if_flags & IFF_UP) &&
2636                     (ifp->if_drv_flags & IFF_DRV_RUNNING)) {
2637                         mlx5e_set_rx_mode(ifp);
2638                         break;
2639                 }
2640                 PRIV_LOCK(priv);
2641                 if (ifp->if_flags & IFF_UP) {
2642                         if ((ifp->if_drv_flags & IFF_DRV_RUNNING) == 0) {
2643                                 if (test_bit(MLX5E_STATE_OPENED, &priv->state) == 0)
2644                                         mlx5e_open_locked(ifp);
2645                                 ifp->if_drv_flags |= IFF_DRV_RUNNING;
2646                                 mlx5_set_port_status(priv->mdev, MLX5_PORT_UP);
2647                         }
2648                 } else {
2649                         if (ifp->if_drv_flags & IFF_DRV_RUNNING) {
2650                                 mlx5_set_port_status(priv->mdev,
2651                                     MLX5_PORT_DOWN);
2652                                 if (test_bit(MLX5E_STATE_OPENED, &priv->state) != 0)
2653                                         mlx5e_close_locked(ifp);
2654                                 mlx5e_update_carrier(priv);
2655                                 ifp->if_drv_flags &= ~IFF_DRV_RUNNING;
2656                         }
2657                 }
2658                 PRIV_UNLOCK(priv);
2659                 break;
2660         case SIOCADDMULTI:
2661         case SIOCDELMULTI:
2662                 mlx5e_set_rx_mode(ifp);
2663                 break;
2664         case SIOCSIFMEDIA:
2665         case SIOCGIFMEDIA:
2666         case SIOCGIFXMEDIA:
2667                 ifr = (struct ifreq *)data;
2668                 error = ifmedia_ioctl(ifp, ifr, &priv->media, command);
2669                 break;
2670         case SIOCSIFCAP:
2671                 ifr = (struct ifreq *)data;
2672                 PRIV_LOCK(priv);
2673                 mask = ifr->ifr_reqcap ^ ifp->if_capenable;
2674
2675                 if (mask & IFCAP_TXCSUM) {
2676                         ifp->if_capenable ^= IFCAP_TXCSUM;
2677                         ifp->if_hwassist ^= (CSUM_TCP | CSUM_UDP | CSUM_IP);
2678
2679                         if (IFCAP_TSO4 & ifp->if_capenable &&
2680                             !(IFCAP_TXCSUM & ifp->if_capenable)) {
2681                                 ifp->if_capenable &= ~IFCAP_TSO4;
2682                                 ifp->if_hwassist &= ~CSUM_IP_TSO;
2683                                 if_printf(ifp,
2684                                     "tso4 disabled due to -txcsum.\n");
2685                         }
2686                 }
2687                 if (mask & IFCAP_TXCSUM_IPV6) {
2688                         ifp->if_capenable ^= IFCAP_TXCSUM_IPV6;
2689                         ifp->if_hwassist ^= (CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
2690
2691                         if (IFCAP_TSO6 & ifp->if_capenable &&
2692                             !(IFCAP_TXCSUM_IPV6 & ifp->if_capenable)) {
2693                                 ifp->if_capenable &= ~IFCAP_TSO6;
2694                                 ifp->if_hwassist &= ~CSUM_IP6_TSO;
2695                                 if_printf(ifp,
2696                                     "tso6 disabled due to -txcsum6.\n");
2697                         }
2698                 }
2699                 if (mask & IFCAP_RXCSUM)
2700                         ifp->if_capenable ^= IFCAP_RXCSUM;
2701                 if (mask & IFCAP_RXCSUM_IPV6)
2702                         ifp->if_capenable ^= IFCAP_RXCSUM_IPV6;
2703                 if (mask & IFCAP_TSO4) {
2704                         if (!(IFCAP_TSO4 & ifp->if_capenable) &&
2705                             !(IFCAP_TXCSUM & ifp->if_capenable)) {
2706                                 if_printf(ifp, "enable txcsum first.\n");
2707                                 error = EAGAIN;
2708                                 goto out;
2709                         }
2710                         ifp->if_capenable ^= IFCAP_TSO4;
2711                         ifp->if_hwassist ^= CSUM_IP_TSO;
2712                 }
2713                 if (mask & IFCAP_TSO6) {
2714                         if (!(IFCAP_TSO6 & ifp->if_capenable) &&
2715                             !(IFCAP_TXCSUM_IPV6 & ifp->if_capenable)) {
2716                                 if_printf(ifp, "enable txcsum6 first.\n");
2717                                 error = EAGAIN;
2718                                 goto out;
2719                         }
2720                         ifp->if_capenable ^= IFCAP_TSO6;
2721                         ifp->if_hwassist ^= CSUM_IP6_TSO;
2722                 }
2723                 if (mask & IFCAP_VLAN_HWFILTER) {
2724                         if (ifp->if_capenable & IFCAP_VLAN_HWFILTER)
2725                                 mlx5e_disable_vlan_filter(priv);
2726                         else
2727                                 mlx5e_enable_vlan_filter(priv);
2728
2729                         ifp->if_capenable ^= IFCAP_VLAN_HWFILTER;
2730                 }
2731                 if (mask & IFCAP_VLAN_HWTAGGING)
2732                         ifp->if_capenable ^= IFCAP_VLAN_HWTAGGING;
2733                 if (mask & IFCAP_WOL_MAGIC)
2734                         ifp->if_capenable ^= IFCAP_WOL_MAGIC;
2735
2736                 VLAN_CAPABILITIES(ifp);
2737                 /* turn off LRO means also turn of HW LRO - if it's on */
2738                 if (mask & IFCAP_LRO) {
2739                         int was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2740                         bool need_restart = false;
2741
2742                         ifp->if_capenable ^= IFCAP_LRO;
2743                         if (!(ifp->if_capenable & IFCAP_LRO)) {
2744                                 if (priv->params.hw_lro_en) {
2745                                         priv->params.hw_lro_en = false;
2746                                         need_restart = true;
2747                                         /* Not sure this is the correct way */
2748                                         priv->params_ethtool.hw_lro = priv->params.hw_lro_en;
2749                                 }
2750                         }
2751                         if (was_opened && need_restart) {
2752                                 mlx5e_close_locked(ifp);
2753                                 mlx5e_open_locked(ifp);
2754                         }
2755                 }
2756 out:
2757                 PRIV_UNLOCK(priv);
2758                 break;
2759
2760         case SIOCGI2C:
2761                 ifr = (struct ifreq *)data;
2762
2763                 /*
2764                  * Copy from the user-space address ifr_data to the
2765                  * kernel-space address i2c
2766                  */
2767                 error = copyin(ifr->ifr_data, &i2c, sizeof(i2c));
2768                 if (error)
2769                         break;
2770
2771                 if (i2c.len > sizeof(i2c.data)) {
2772                         error = EINVAL;
2773                         break;
2774                 }
2775
2776                 PRIV_LOCK(priv);
2777                 /* Get module_num which is required for the query_eeprom */
2778                 error = mlx5_query_module_num(priv->mdev, &module_num);
2779                 if (error) {
2780                         if_printf(ifp, "Query module num failed, eeprom "
2781                             "reading is not supported\n");
2782                         error = EINVAL;
2783                         goto err_i2c;
2784                 }
2785                 /* Check if module is present before doing an access */
2786                 module_status = mlx5_query_module_status(priv->mdev, module_num);
2787                 if (module_status != MLX5_MODULE_STATUS_PLUGGED_ENABLED &&
2788                     module_status != MLX5_MODULE_STATUS_PLUGGED_DISABLED) {
2789                         error = EINVAL;
2790                         goto err_i2c;
2791                 }
2792                 /*
2793                  * Currently 0XA0 and 0xA2 are the only addresses permitted.
2794                  * The internal conversion is as follows:
2795                  */
2796                 if (i2c.dev_addr == 0xA0)
2797                         read_addr = MLX5E_I2C_ADDR_LOW;
2798                 else if (i2c.dev_addr == 0xA2)
2799                         read_addr = MLX5E_I2C_ADDR_HIGH;
2800                 else {
2801                         if_printf(ifp, "Query eeprom failed, "
2802                             "Invalid Address: %X\n", i2c.dev_addr);
2803                         error = EINVAL;
2804                         goto err_i2c;
2805                 }
2806                 error = mlx5_query_eeprom(priv->mdev,
2807                     read_addr, MLX5E_EEPROM_LOW_PAGE,
2808                     (uint32_t)i2c.offset, (uint32_t)i2c.len, module_num,
2809                     (uint32_t *)i2c.data, &size_read);
2810                 if (error) {
2811                         if_printf(ifp, "Query eeprom failed, eeprom "
2812                             "reading is not supported\n");
2813                         error = EINVAL;
2814                         goto err_i2c;
2815                 }
2816
2817                 if (i2c.len > MLX5_EEPROM_MAX_BYTES) {
2818                         error = mlx5_query_eeprom(priv->mdev,
2819                             read_addr, MLX5E_EEPROM_LOW_PAGE,
2820                             (uint32_t)(i2c.offset + size_read),
2821                             (uint32_t)(i2c.len - size_read), module_num,
2822                             (uint32_t *)(i2c.data + size_read), &size_read);
2823                 }
2824                 if (error) {
2825                         if_printf(ifp, "Query eeprom failed, eeprom "
2826                             "reading is not supported\n");
2827                         error = EINVAL;
2828                         goto err_i2c;
2829                 }
2830
2831                 error = copyout(&i2c, ifr->ifr_data, sizeof(i2c));
2832 err_i2c:
2833                 PRIV_UNLOCK(priv);
2834                 break;
2835
2836         default:
2837                 error = ether_ioctl(ifp, command, data);
2838                 break;
2839         }
2840         return (error);
2841 }
2842
2843 static int
2844 mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
2845 {
2846         /*
2847          * TODO: uncoment once FW really sets all these bits if
2848          * (!mdev->caps.eth.rss_ind_tbl_cap || !mdev->caps.eth.csum_cap ||
2849          * !mdev->caps.eth.max_lso_cap || !mdev->caps.eth.vlan_cap ||
2850          * !(mdev->caps.gen.flags & MLX5_DEV_CAP_FLAG_SCQE_BRK_MOD)) return
2851          * -ENOTSUPP;
2852          */
2853
2854         /* TODO: add more must-to-have features */
2855
2856         if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
2857                 return (-ENODEV);
2858
2859         return (0);
2860 }
2861
2862 static void
2863 mlx5e_build_ifp_priv(struct mlx5_core_dev *mdev,
2864     struct mlx5e_priv *priv,
2865     int num_comp_vectors)
2866 {
2867         /*
2868          * TODO: Consider link speed for setting "log_sq_size",
2869          * "log_rq_size" and "cq_moderation_xxx":
2870          */
2871         priv->params.log_sq_size =
2872             MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
2873         priv->params.log_rq_size =
2874             MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
2875         priv->params.rx_cq_moderation_usec =
2876             MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
2877             MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE :
2878             MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
2879         priv->params.rx_cq_moderation_mode =
2880             MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ? 1 : 0;
2881         priv->params.rx_cq_moderation_pkts =
2882             MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
2883         priv->params.tx_cq_moderation_usec =
2884             MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
2885         priv->params.tx_cq_moderation_pkts =
2886             MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
2887         priv->params.min_rx_wqes =
2888             MLX5E_PARAMS_DEFAULT_MIN_RX_WQES;
2889         priv->params.rx_hash_log_tbl_sz =
2890             (order_base_2(num_comp_vectors) >
2891             MLX5E_PARAMS_DEFAULT_RX_HASH_LOG_TBL_SZ) ?
2892             order_base_2(num_comp_vectors) :
2893             MLX5E_PARAMS_DEFAULT_RX_HASH_LOG_TBL_SZ;
2894         priv->params.num_tc = 1;
2895         priv->params.default_vlan_prio = 0;
2896         priv->counter_set_id = -1;
2897
2898         /*
2899          * hw lro is currently defaulted to off. when it won't anymore we
2900          * will consider the HW capability: "!!MLX5_CAP_ETH(mdev, lro_cap)"
2901          */
2902         priv->params.hw_lro_en = false;
2903         priv->params.lro_wqe_sz = MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
2904
2905         priv->params.cqe_zipping_en = !!MLX5_CAP_GEN(mdev, cqe_compression);
2906
2907         priv->mdev = mdev;
2908         priv->params.num_channels = num_comp_vectors;
2909         priv->order_base_2_num_channels = order_base_2(num_comp_vectors);
2910         priv->queue_mapping_channel_mask =
2911             roundup_pow_of_two(num_comp_vectors) - 1;
2912         priv->num_tc = priv->params.num_tc;
2913         priv->default_vlan_prio = priv->params.default_vlan_prio;
2914
2915         INIT_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
2916         INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
2917         INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
2918 }
2919
2920 static int
2921 mlx5e_create_mkey(struct mlx5e_priv *priv, u32 pdn,
2922                   struct mlx5_core_mr *mkey)
2923 {
2924         struct ifnet *ifp = priv->ifp;
2925         struct mlx5_core_dev *mdev = priv->mdev;
2926         int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
2927         void *mkc;
2928         u32 *in;
2929         int err;
2930
2931         in = mlx5_vzalloc(inlen);
2932         if (in == NULL) {
2933                 if_printf(ifp, "%s: failed to allocate inbox\n", __func__);
2934                 return (-ENOMEM);
2935         }
2936
2937         mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
2938         MLX5_SET(mkc, mkc, access_mode, MLX5_ACCESS_MODE_PA);
2939         MLX5_SET(mkc, mkc, lw, 1);
2940         MLX5_SET(mkc, mkc, lr, 1);
2941
2942         MLX5_SET(mkc, mkc, pd, pdn);
2943         MLX5_SET(mkc, mkc, length64, 1);
2944         MLX5_SET(mkc, mkc, qpn, 0xffffff);
2945
2946         err = mlx5_core_create_mkey(mdev, mkey, in, inlen);
2947         if (err)
2948                 if_printf(ifp, "%s: mlx5_core_create_mkey failed, %d\n",
2949                     __func__, err);
2950
2951         kvfree(in);
2952         return (err);
2953 }
2954
2955 static const char *mlx5e_vport_stats_desc[] = {
2956         MLX5E_VPORT_STATS(MLX5E_STATS_DESC)
2957 };
2958
2959 static const char *mlx5e_pport_stats_desc[] = {
2960         MLX5E_PPORT_STATS(MLX5E_STATS_DESC)
2961 };
2962
2963 static void
2964 mlx5e_priv_mtx_init(struct mlx5e_priv *priv)
2965 {
2966         mtx_init(&priv->async_events_mtx, "mlx5async", MTX_NETWORK_LOCK, MTX_DEF);
2967         sx_init(&priv->state_lock, "mlx5state");
2968         callout_init_mtx(&priv->watchdog, &priv->async_events_mtx, 0);
2969         MLX5_INIT_DOORBELL_LOCK(&priv->doorbell_lock);
2970 }
2971
2972 static void
2973 mlx5e_priv_mtx_destroy(struct mlx5e_priv *priv)
2974 {
2975         mtx_destroy(&priv->async_events_mtx);
2976         sx_destroy(&priv->state_lock);
2977 }
2978
2979 static int
2980 sysctl_firmware(SYSCTL_HANDLER_ARGS)
2981 {
2982         /*
2983          * %d.%d%.d the string format.
2984          * fw_rev_{maj,min,sub} return u16, 2^16 = 65536.
2985          * We need at most 5 chars to store that.
2986          * It also has: two "." and NULL at the end, which means we need 18
2987          * (5*3 + 3) chars at most.
2988          */
2989         char fw[18];
2990         struct mlx5e_priv *priv = arg1;
2991         int error;
2992
2993         snprintf(fw, sizeof(fw), "%d.%d.%d", fw_rev_maj(priv->mdev), fw_rev_min(priv->mdev),
2994             fw_rev_sub(priv->mdev));
2995         error = sysctl_handle_string(oidp, fw, sizeof(fw), req);
2996         return (error);
2997 }
2998
2999 static void
3000 mlx5e_disable_tx_dma(struct mlx5e_channel *ch)
3001 {
3002         int i;
3003
3004         for (i = 0; i < ch->num_tc; i++)
3005                 mlx5e_drain_sq(&ch->sq[i]);
3006 }
3007
3008 static void
3009 mlx5e_reset_sq_doorbell_record(struct mlx5e_sq *sq)
3010 {
3011
3012         sq->doorbell.d32[0] = cpu_to_be32(MLX5_OPCODE_NOP);
3013         sq->doorbell.d32[1] = cpu_to_be32(sq->sqn << 8);
3014         mlx5e_tx_notify_hw(sq, sq->doorbell.d32, 0);
3015         sq->doorbell.d64 = 0;
3016 }
3017
3018 void
3019 mlx5e_resume_sq(struct mlx5e_sq *sq)
3020 {
3021         int err;
3022
3023         /* check if already enabled */
3024         if (sq->stopped == 0)
3025                 return;
3026
3027         err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_ERR,
3028             MLX5_SQC_STATE_RST);
3029         if (err != 0) {
3030                 if_printf(sq->ifp,
3031                     "mlx5e_modify_sq() from ERR to RST failed: %d\n", err);
3032         }
3033
3034         sq->cc = 0;
3035         sq->pc = 0;
3036
3037         /* reset doorbell prior to moving from RST to RDY */
3038         mlx5e_reset_sq_doorbell_record(sq);
3039
3040         err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST,
3041             MLX5_SQC_STATE_RDY);
3042         if (err != 0) {
3043                 if_printf(sq->ifp,
3044                     "mlx5e_modify_sq() from RST to RDY failed: %d\n", err);
3045         }
3046
3047         mtx_lock(&sq->lock);
3048         sq->cev_next_state = MLX5E_CEV_STATE_INITIAL;
3049         sq->stopped = 0;
3050         mtx_unlock(&sq->lock);
3051
3052 }
3053
3054 static void
3055 mlx5e_enable_tx_dma(struct mlx5e_channel *ch)
3056 {
3057         int i;
3058
3059         for (i = 0; i < ch->num_tc; i++)
3060                 mlx5e_resume_sq(&ch->sq[i]);
3061 }
3062
3063 static void
3064 mlx5e_disable_rx_dma(struct mlx5e_channel *ch)
3065 {
3066         struct mlx5e_rq *rq = &ch->rq;
3067         int err;
3068
3069         mtx_lock(&rq->mtx);
3070         rq->enabled = 0;
3071         callout_stop(&rq->watchdog);
3072         mtx_unlock(&rq->mtx);
3073
3074         callout_drain(&rq->watchdog);
3075
3076         err = mlx5e_modify_rq(rq, MLX5_RQC_STATE_RDY, MLX5_RQC_STATE_ERR);
3077         if (err != 0) {
3078                 if_printf(rq->ifp,
3079                     "mlx5e_modify_rq() from RDY to RST failed: %d\n", err);
3080         }
3081
3082         while (!mlx5_wq_ll_is_empty(&rq->wq)) {
3083                 msleep(1);
3084                 rq->cq.mcq.comp(&rq->cq.mcq);
3085         }
3086
3087         /*
3088          * Transitioning into RST state will allow the FW to track less ERR state queues,
3089          * thus reducing the recv queue flushing time
3090          */
3091         err = mlx5e_modify_rq(rq, MLX5_RQC_STATE_ERR, MLX5_RQC_STATE_RST);
3092         if (err != 0) {
3093                 if_printf(rq->ifp,
3094                     "mlx5e_modify_rq() from ERR to RST failed: %d\n", err);
3095         }
3096 }
3097
3098 static void
3099 mlx5e_enable_rx_dma(struct mlx5e_channel *ch)
3100 {
3101         struct mlx5e_rq *rq = &ch->rq;
3102         int err;
3103
3104         rq->wq.wqe_ctr = 0;
3105         mlx5_wq_ll_update_db_record(&rq->wq);
3106         err = mlx5e_modify_rq(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
3107         if (err != 0) {
3108                 if_printf(rq->ifp,
3109                     "mlx5e_modify_rq() from RST to RDY failed: %d\n", err);
3110         }
3111
3112         rq->enabled = 1;
3113
3114         rq->cq.mcq.comp(&rq->cq.mcq);
3115 }
3116
3117 void
3118 mlx5e_modify_tx_dma(struct mlx5e_priv *priv, uint8_t value)
3119 {
3120         int i;
3121
3122         if (priv->channel == NULL)
3123                 return;
3124
3125         for (i = 0; i < priv->params.num_channels; i++) {
3126
3127                 if (!priv->channel[i])
3128                         continue;
3129
3130                 if (value)
3131                         mlx5e_disable_tx_dma(priv->channel[i]);
3132                 else
3133                         mlx5e_enable_tx_dma(priv->channel[i]);
3134         }
3135 }
3136
3137 void
3138 mlx5e_modify_rx_dma(struct mlx5e_priv *priv, uint8_t value)
3139 {
3140         int i;
3141
3142         if (priv->channel == NULL)
3143                 return;
3144
3145         for (i = 0; i < priv->params.num_channels; i++) {
3146
3147                 if (!priv->channel[i])
3148                         continue;
3149
3150                 if (value)
3151                         mlx5e_disable_rx_dma(priv->channel[i]);
3152                 else
3153                         mlx5e_enable_rx_dma(priv->channel[i]);
3154         }
3155 }
3156
3157 static void
3158 mlx5e_add_hw_stats(struct mlx5e_priv *priv)
3159 {
3160         SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_hw),
3161             OID_AUTO, "fw_version", CTLTYPE_STRING | CTLFLAG_RD, priv, 0,
3162             sysctl_firmware, "A", "HCA firmware version");
3163
3164         SYSCTL_ADD_STRING(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_hw),
3165             OID_AUTO, "board_id", CTLFLAG_RD, priv->mdev->board_id, 0,
3166             "Board ID");
3167 }
3168
3169 static int
3170 mlx5e_sysctl_tx_priority_flow_control(SYSCTL_HANDLER_ARGS)
3171 {
3172         struct mlx5e_priv *priv = arg1;
3173         uint32_t tx_pfc;
3174         uint32_t value;
3175         int error;
3176
3177         PRIV_LOCK(priv);
3178
3179         tx_pfc = priv->params.tx_priority_flow_control;
3180
3181         /* get current value */
3182         value = (tx_pfc >> arg2) & 1;
3183
3184         error = sysctl_handle_32(oidp, &value, 0, req);
3185
3186         /* range check value */
3187         if (value != 0)
3188                 priv->params.tx_priority_flow_control |= (1 << arg2);
3189         else
3190                 priv->params.tx_priority_flow_control &= ~(1 << arg2);
3191
3192         /* check if update is required */
3193         if (error == 0 && priv->gone == 0 &&
3194             tx_pfc != priv->params.tx_priority_flow_control) {
3195                 error = -mlx5e_set_port_pfc(priv);
3196                 /* restore previous value */
3197                 if (error != 0)
3198                         priv->params.tx_priority_flow_control= tx_pfc;
3199         }
3200         PRIV_UNLOCK(priv);
3201
3202         return (error);
3203 }
3204
3205 static int
3206 mlx5e_sysctl_rx_priority_flow_control(SYSCTL_HANDLER_ARGS)
3207 {
3208         struct mlx5e_priv *priv = arg1;
3209         uint32_t rx_pfc;
3210         uint32_t value;
3211         int error;
3212
3213         PRIV_LOCK(priv);
3214
3215         rx_pfc = priv->params.rx_priority_flow_control;
3216
3217         /* get current value */
3218         value = (rx_pfc >> arg2) & 1;
3219
3220         error = sysctl_handle_32(oidp, &value, 0, req);
3221
3222         /* range check value */
3223         if (value != 0)
3224                 priv->params.rx_priority_flow_control |= (1 << arg2);
3225         else
3226                 priv->params.rx_priority_flow_control &= ~(1 << arg2);
3227
3228         /* check if update is required */
3229         if (error == 0 && priv->gone == 0 &&
3230             rx_pfc != priv->params.rx_priority_flow_control) {
3231                 error = -mlx5e_set_port_pfc(priv);
3232                 /* restore previous value */
3233                 if (error != 0)
3234                         priv->params.rx_priority_flow_control= rx_pfc;
3235         }
3236         PRIV_UNLOCK(priv);
3237
3238         return (error);
3239 }
3240
3241 static void
3242 mlx5e_setup_pauseframes(struct mlx5e_priv *priv)
3243 {
3244         unsigned int x;
3245         char path[96];
3246         int error;
3247
3248         /* Only receiving pauseframes is enabled by default */
3249         priv->params.tx_pauseframe_control = 0;
3250         priv->params.rx_pauseframe_control = 1;
3251
3252         /* disable ports flow control, PFC, by default */
3253         priv->params.tx_priority_flow_control = 0;
3254         priv->params.rx_priority_flow_control = 0;
3255
3256 #if (__FreeBSD_version < 1100000)
3257         /* compute path for sysctl */
3258         snprintf(path, sizeof(path), "dev.mce.%d.tx_pauseframe_control",
3259             device_get_unit(priv->mdev->pdev->dev.bsddev));
3260
3261         /* try to fetch tunable, if any */
3262         TUNABLE_INT_FETCH(path, &priv->params.tx_pauseframe_control);
3263
3264         /* compute path for sysctl */
3265         snprintf(path, sizeof(path), "dev.mce.%d.rx_pauseframe_control",
3266             device_get_unit(priv->mdev->pdev->dev.bsddev));
3267
3268         /* try to fetch tunable, if any */
3269         TUNABLE_INT_FETCH(path, &priv->params.rx_pauseframe_control);
3270
3271         for (x = 0; x != 8; x++) {
3272
3273                 /* compute path for sysctl */
3274                 snprintf(path, sizeof(path), "dev.mce.%d.tx_priority_flow_control_%u",
3275                     device_get_unit(priv->mdev->pdev->dev.bsddev), x);
3276
3277                 /* try to fetch tunable, if any */
3278                 if (TUNABLE_INT_FETCH(path, &value) == 0 && value != 0)
3279                         priv->params.tx_priority_flow_control |= 1 << x;
3280
3281                 /* compute path for sysctl */
3282                 snprintf(path, sizeof(path), "dev.mce.%d.rx_priority_flow_control_%u",
3283                     device_get_unit(priv->mdev->pdev->dev.bsddev), x);
3284
3285                 /* try to fetch tunable, if any */
3286                 if (TUNABLE_INT_FETCH(path, &value) == 0 && value != 0)
3287                         priv->params.rx_priority_flow_control |= 1 << x;
3288         }
3289 #endif
3290
3291         /* register pauseframe SYSCTLs */
3292         SYSCTL_ADD_INT(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3293             OID_AUTO, "tx_pauseframe_control", CTLFLAG_RDTUN,
3294             &priv->params.tx_pauseframe_control, 0,
3295             "Set to enable TX pause frames. Clear to disable.");
3296
3297         SYSCTL_ADD_INT(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3298             OID_AUTO, "rx_pauseframe_control", CTLFLAG_RDTUN,
3299             &priv->params.rx_pauseframe_control, 0,
3300             "Set to enable RX pause frames. Clear to disable.");
3301
3302         /* register priority_flow control, PFC, SYSCTLs */
3303         for (x = 0; x != 8; x++) {
3304                 snprintf(path, sizeof(path), "tx_priority_flow_control_%u", x);
3305
3306                 SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3307                     OID_AUTO, path, CTLTYPE_UINT | CTLFLAG_RWTUN |
3308                     CTLFLAG_MPSAFE, priv, x, &mlx5e_sysctl_tx_priority_flow_control, "IU",
3309                     "Set to enable TX ports flow control frames for given priority. Clear to disable.");
3310
3311                 snprintf(path, sizeof(path), "rx_priority_flow_control_%u", x);
3312
3313                 SYSCTL_ADD_PROC(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3314                     OID_AUTO, path, CTLTYPE_UINT | CTLFLAG_RWTUN |
3315                     CTLFLAG_MPSAFE, priv, x, &mlx5e_sysctl_rx_priority_flow_control, "IU",
3316                     "Set to enable RX ports flow control frames for given priority. Clear to disable.");
3317         }
3318
3319         PRIV_LOCK(priv);
3320
3321         /* range check */
3322         priv->params.tx_pauseframe_control =
3323             priv->params.tx_pauseframe_control ? 1 : 0;
3324         priv->params.rx_pauseframe_control =
3325             priv->params.rx_pauseframe_control ? 1 : 0;
3326
3327         /* update firmware */
3328         error = mlx5e_set_port_pause_and_pfc(priv);
3329         if (error == -EINVAL) {
3330                 if_printf(priv->ifp,
3331                     "Global pauseframes must be disabled before enabling PFC.\n");
3332                 priv->params.rx_priority_flow_control = 0;
3333                 priv->params.tx_priority_flow_control = 0;
3334
3335                 /* update firmware */
3336                 (void) mlx5e_set_port_pause_and_pfc(priv);
3337         }
3338         PRIV_UNLOCK(priv);
3339 }
3340
3341 static void *
3342 mlx5e_create_ifp(struct mlx5_core_dev *mdev)
3343 {
3344         struct ifnet *ifp;
3345         struct mlx5e_priv *priv;
3346         u8 dev_addr[ETHER_ADDR_LEN] __aligned(4);
3347         struct sysctl_oid_list *child;
3348         int ncv = mdev->priv.eq_table.num_comp_vectors;
3349         char unit[16];
3350         int err;
3351         int i;
3352         u32 eth_proto_cap;
3353
3354         if (mlx5e_check_required_hca_cap(mdev)) {
3355                 mlx5_core_dbg(mdev, "mlx5e_check_required_hca_cap() failed\n");
3356                 return (NULL);
3357         }
3358         priv = malloc(sizeof(*priv), M_MLX5EN, M_WAITOK | M_ZERO);
3359         mlx5e_priv_mtx_init(priv);
3360
3361         ifp = priv->ifp = if_alloc(IFT_ETHER);
3362         if (ifp == NULL) {
3363                 mlx5_core_err(mdev, "if_alloc() failed\n");
3364                 goto err_free_priv;
3365         }
3366         ifp->if_softc = priv;
3367         if_initname(ifp, "mce", device_get_unit(mdev->pdev->dev.bsddev));
3368         ifp->if_mtu = ETHERMTU;
3369         ifp->if_init = mlx5e_open;
3370         ifp->if_flags = IFF_BROADCAST | IFF_SIMPLEX | IFF_MULTICAST;
3371         ifp->if_ioctl = mlx5e_ioctl;
3372         ifp->if_transmit = mlx5e_xmit;
3373         ifp->if_qflush = if_qflush;
3374 #if (__FreeBSD_version >= 1100000)
3375         ifp->if_get_counter = mlx5e_get_counter;
3376 #endif
3377         ifp->if_snd.ifq_maxlen = ifqmaxlen;
3378         /*
3379          * Set driver features
3380          */
3381         ifp->if_capabilities |= IFCAP_HWCSUM | IFCAP_HWCSUM_IPV6;
3382         ifp->if_capabilities |= IFCAP_VLAN_MTU | IFCAP_VLAN_HWTAGGING;
3383         ifp->if_capabilities |= IFCAP_VLAN_HWCSUM | IFCAP_VLAN_HWFILTER;
3384         ifp->if_capabilities |= IFCAP_LINKSTATE | IFCAP_JUMBO_MTU;
3385         ifp->if_capabilities |= IFCAP_LRO;
3386         ifp->if_capabilities |= IFCAP_TSO | IFCAP_VLAN_HWTSO;
3387         ifp->if_capabilities |= IFCAP_HWSTATS;
3388
3389         /* set TSO limits so that we don't have to drop TX packets */
3390         ifp->if_hw_tsomax = MLX5E_MAX_TX_PAYLOAD_SIZE - (ETHER_HDR_LEN + ETHER_VLAN_ENCAP_LEN);
3391         ifp->if_hw_tsomaxsegcount = MLX5E_MAX_TX_MBUF_FRAGS - 1 /* hdr */;
3392         ifp->if_hw_tsomaxsegsize = MLX5E_MAX_TX_MBUF_SIZE;
3393
3394         ifp->if_capenable = ifp->if_capabilities;
3395         ifp->if_hwassist = 0;
3396         if (ifp->if_capenable & IFCAP_TSO)
3397                 ifp->if_hwassist |= CSUM_TSO;
3398         if (ifp->if_capenable & IFCAP_TXCSUM)
3399                 ifp->if_hwassist |= (CSUM_TCP | CSUM_UDP | CSUM_IP);
3400         if (ifp->if_capenable & IFCAP_TXCSUM_IPV6)
3401                 ifp->if_hwassist |= (CSUM_UDP_IPV6 | CSUM_TCP_IPV6);
3402
3403         /* ifnet sysctl tree */
3404         sysctl_ctx_init(&priv->sysctl_ctx);
3405         priv->sysctl_ifnet = SYSCTL_ADD_NODE(&priv->sysctl_ctx, SYSCTL_STATIC_CHILDREN(_dev),
3406             OID_AUTO, ifp->if_dname, CTLFLAG_RD, 0, "MLX5 ethernet - interface name");
3407         if (priv->sysctl_ifnet == NULL) {
3408                 mlx5_core_err(mdev, "SYSCTL_ADD_NODE() failed\n");
3409                 goto err_free_sysctl;
3410         }
3411         snprintf(unit, sizeof(unit), "%d", ifp->if_dunit);
3412         priv->sysctl_ifnet = SYSCTL_ADD_NODE(&priv->sysctl_ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3413             OID_AUTO, unit, CTLFLAG_RD, 0, "MLX5 ethernet - interface unit");
3414         if (priv->sysctl_ifnet == NULL) {
3415                 mlx5_core_err(mdev, "SYSCTL_ADD_NODE() failed\n");
3416                 goto err_free_sysctl;
3417         }
3418
3419         /* HW sysctl tree */
3420         child = SYSCTL_CHILDREN(device_get_sysctl_tree(mdev->pdev->dev.bsddev));
3421         priv->sysctl_hw = SYSCTL_ADD_NODE(&priv->sysctl_ctx, child,
3422             OID_AUTO, "hw", CTLFLAG_RD, 0, "MLX5 ethernet dev hw");
3423         if (priv->sysctl_hw == NULL) {
3424                 mlx5_core_err(mdev, "SYSCTL_ADD_NODE() failed\n");
3425                 goto err_free_sysctl;
3426         }
3427         mlx5e_build_ifp_priv(mdev, priv, ncv);
3428
3429         snprintf(unit, sizeof(unit), "mce%u_wq",
3430             device_get_unit(mdev->pdev->dev.bsddev));
3431         priv->wq = alloc_workqueue(unit, 0, 1);
3432         if (priv->wq == NULL) {
3433                 if_printf(ifp, "%s: alloc_workqueue failed\n", __func__);
3434                 goto err_free_sysctl;
3435         }
3436
3437         err = mlx5_alloc_map_uar(mdev, &priv->cq_uar);
3438         if (err) {
3439                 if_printf(ifp, "%s: mlx5_alloc_map_uar failed, %d\n",
3440                     __func__, err);
3441                 goto err_free_wq;
3442         }
3443         err = mlx5_core_alloc_pd(mdev, &priv->pdn);
3444         if (err) {
3445                 if_printf(ifp, "%s: mlx5_core_alloc_pd failed, %d\n",
3446                     __func__, err);
3447                 goto err_unmap_free_uar;
3448         }
3449         err = mlx5_alloc_transport_domain(mdev, &priv->tdn);
3450         if (err) {
3451                 if_printf(ifp, "%s: mlx5_alloc_transport_domain failed, %d\n",
3452                     __func__, err);
3453                 goto err_dealloc_pd;
3454         }
3455         err = mlx5e_create_mkey(priv, priv->pdn, &priv->mr);
3456         if (err) {
3457                 if_printf(ifp, "%s: mlx5e_create_mkey failed, %d\n",
3458                     __func__, err);
3459                 goto err_dealloc_transport_domain;
3460         }
3461         mlx5_query_nic_vport_mac_address(priv->mdev, 0, dev_addr);
3462
3463         /* check if we should generate a random MAC address */
3464         if (MLX5_CAP_GEN(priv->mdev, vport_group_manager) == 0 &&
3465             is_zero_ether_addr(dev_addr)) {
3466                 random_ether_addr(dev_addr);
3467                 if_printf(ifp, "Assigned random MAC address\n");
3468         }
3469
3470         /* set default MTU */
3471         mlx5e_set_dev_port_mtu(ifp, ifp->if_mtu);
3472
3473         /* Set desc */
3474         device_set_desc(mdev->pdev->dev.bsddev, mlx5e_version);
3475
3476         /* Set default media status */
3477         priv->media_status_last = IFM_AVALID;
3478         priv->media_active_last = IFM_ETHER | IFM_AUTO |
3479             IFM_ETH_RXPAUSE | IFM_FDX;
3480
3481         /* setup default pauseframes configuration */
3482         mlx5e_setup_pauseframes(priv);
3483
3484         err = mlx5_query_port_proto_cap(mdev, &eth_proto_cap, MLX5_PTYS_EN);
3485         if (err) {
3486                 eth_proto_cap = 0;
3487                 if_printf(ifp, "%s: Query port media capability failed, %d\n",
3488                     __func__, err);
3489         }
3490
3491         /* Setup supported medias */
3492         ifmedia_init(&priv->media, IFM_IMASK | IFM_ETH_FMASK,
3493             mlx5e_media_change, mlx5e_media_status);
3494
3495         for (i = 0; i < MLX5E_LINK_MODES_NUMBER; ++i) {
3496                 if (mlx5e_mode_table[i].baudrate == 0)
3497                         continue;
3498                 if (MLX5E_PROT_MASK(i) & eth_proto_cap) {
3499                         ifmedia_add(&priv->media,
3500                             mlx5e_mode_table[i].subtype |
3501                             IFM_ETHER, 0, NULL);
3502                         ifmedia_add(&priv->media,
3503                             mlx5e_mode_table[i].subtype |
3504                             IFM_ETHER | IFM_FDX |
3505                             IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE, 0, NULL);
3506                 }
3507         }
3508
3509         ifmedia_add(&priv->media, IFM_ETHER | IFM_AUTO, 0, NULL);
3510         ifmedia_add(&priv->media, IFM_ETHER | IFM_AUTO | IFM_FDX |
3511             IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE, 0, NULL);
3512
3513         /* Set autoselect by default */
3514         ifmedia_set(&priv->media, IFM_ETHER | IFM_AUTO | IFM_FDX |
3515             IFM_ETH_RXPAUSE | IFM_ETH_TXPAUSE);
3516         ether_ifattach(ifp, dev_addr);
3517
3518         /* Register for VLAN events */
3519         priv->vlan_attach = EVENTHANDLER_REGISTER(vlan_config,
3520             mlx5e_vlan_rx_add_vid, priv, EVENTHANDLER_PRI_FIRST);
3521         priv->vlan_detach = EVENTHANDLER_REGISTER(vlan_unconfig,
3522             mlx5e_vlan_rx_kill_vid, priv, EVENTHANDLER_PRI_FIRST);
3523
3524         /* Link is down by default */
3525         if_link_state_change(ifp, LINK_STATE_DOWN);
3526
3527         mlx5e_enable_async_events(priv);
3528
3529         mlx5e_add_hw_stats(priv);
3530
3531         mlx5e_create_stats(&priv->stats.vport.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3532             "vstats", mlx5e_vport_stats_desc, MLX5E_VPORT_STATS_NUM,
3533             priv->stats.vport.arg);
3534
3535         mlx5e_create_stats(&priv->stats.pport.ctx, SYSCTL_CHILDREN(priv->sysctl_ifnet),
3536             "pstats", mlx5e_pport_stats_desc, MLX5E_PPORT_STATS_NUM,
3537             priv->stats.pport.arg);
3538
3539         mlx5e_create_ethtool(priv);
3540
3541         mtx_lock(&priv->async_events_mtx);
3542         mlx5e_update_stats(priv);
3543         mtx_unlock(&priv->async_events_mtx);
3544
3545         return (priv);
3546
3547 err_dealloc_transport_domain:
3548         mlx5_dealloc_transport_domain(mdev, priv->tdn);
3549
3550 err_dealloc_pd:
3551         mlx5_core_dealloc_pd(mdev, priv->pdn);
3552
3553 err_unmap_free_uar:
3554         mlx5_unmap_free_uar(mdev, &priv->cq_uar);
3555
3556 err_free_wq:
3557         destroy_workqueue(priv->wq);
3558
3559 err_free_sysctl:
3560         sysctl_ctx_free(&priv->sysctl_ctx);
3561
3562         if_free(ifp);
3563
3564 err_free_priv:
3565         mlx5e_priv_mtx_destroy(priv);
3566         free(priv, M_MLX5EN);
3567         return (NULL);
3568 }
3569
3570 static void
3571 mlx5e_destroy_ifp(struct mlx5_core_dev *mdev, void *vpriv)
3572 {
3573         struct mlx5e_priv *priv = vpriv;
3574         struct ifnet *ifp = priv->ifp;
3575
3576         /* don't allow more IOCTLs */
3577         priv->gone = 1;
3578
3579         /*
3580          * Clear the device description to avoid use after free,
3581          * because the bsddev is not destroyed when this module is
3582          * unloaded:
3583          */
3584         device_set_desc(mdev->pdev->dev.bsddev, NULL);
3585
3586         /* XXX wait a bit to allow IOCTL handlers to complete */
3587         pause("W", hz);
3588
3589         /* stop watchdog timer */
3590         callout_drain(&priv->watchdog);
3591
3592         if (priv->vlan_attach != NULL)
3593                 EVENTHANDLER_DEREGISTER(vlan_config, priv->vlan_attach);
3594         if (priv->vlan_detach != NULL)
3595                 EVENTHANDLER_DEREGISTER(vlan_unconfig, priv->vlan_detach);
3596
3597         /* make sure device gets closed */
3598         PRIV_LOCK(priv);
3599         mlx5e_close_locked(ifp);
3600         PRIV_UNLOCK(priv);
3601
3602         /* unregister device */
3603         ifmedia_removeall(&priv->media);
3604         ether_ifdetach(ifp);
3605         if_free(ifp);
3606
3607         /* destroy all remaining sysctl nodes */
3608         if (priv->sysctl_debug)
3609                 sysctl_ctx_free(&priv->stats.port_stats_debug.ctx);
3610         sysctl_ctx_free(&priv->stats.vport.ctx);
3611         sysctl_ctx_free(&priv->stats.pport.ctx);
3612         sysctl_ctx_free(&priv->sysctl_ctx);
3613
3614         mlx5_core_destroy_mkey(priv->mdev, &priv->mr);
3615         mlx5_dealloc_transport_domain(priv->mdev, priv->tdn);
3616         mlx5_core_dealloc_pd(priv->mdev, priv->pdn);
3617         mlx5_unmap_free_uar(priv->mdev, &priv->cq_uar);
3618         mlx5e_disable_async_events(priv);
3619         destroy_workqueue(priv->wq);
3620         mlx5e_priv_mtx_destroy(priv);
3621         free(priv, M_MLX5EN);
3622 }
3623
3624 static void *
3625 mlx5e_get_ifp(void *vpriv)
3626 {
3627         struct mlx5e_priv *priv = vpriv;
3628
3629         return (priv->ifp);
3630 }
3631
3632 static struct mlx5_interface mlx5e_interface = {
3633         .add = mlx5e_create_ifp,
3634         .remove = mlx5e_destroy_ifp,
3635         .event = mlx5e_async_event,
3636         .protocol = MLX5_INTERFACE_PROTOCOL_ETH,
3637         .get_dev = mlx5e_get_ifp,
3638 };
3639
3640 void
3641 mlx5e_init(void)
3642 {
3643         mlx5_register_interface(&mlx5e_interface);
3644 }
3645
3646 void
3647 mlx5e_cleanup(void)
3648 {
3649         mlx5_unregister_interface(&mlx5e_interface);
3650 }
3651
3652 module_init_order(mlx5e_init, SI_ORDER_THIRD);
3653 module_exit_order(mlx5e_cleanup, SI_ORDER_THIRD);
3654
3655 #if (__FreeBSD_version >= 1100000)
3656 MODULE_DEPEND(mlx5en, linuxkpi, 1, 1, 1);
3657 #endif
3658 MODULE_DEPEND(mlx5en, mlx5, 1, 1, 1);
3659 MODULE_VERSION(mlx5en, 1);