Merge tag 'rxrpc-rewrite-20160824-2' of git://git.kernel.org/pub/scm/linux/kernel...
[cascardo/linux.git] / drivers / net / ethernet / mellanox / mlx5 / core / en_main.c
1 /*
2  * Copyright (c) 2015-2016, Mellanox Technologies. All rights reserved.
3  *
4  * This software is available to you under a choice of one of two
5  * licenses.  You may choose to be licensed under the terms of the GNU
6  * General Public License (GPL) Version 2, available from the file
7  * COPYING in the main directory of this source tree, or the
8  * OpenIB.org BSD license below:
9  *
10  *     Redistribution and use in source and binary forms, with or
11  *     without modification, are permitted provided that the following
12  *     conditions are met:
13  *
14  *      - Redistributions of source code must retain the above
15  *        copyright notice, this list of conditions and the following
16  *        disclaimer.
17  *
18  *      - Redistributions in binary form must reproduce the above
19  *        copyright notice, this list of conditions and the following
20  *        disclaimer in the documentation and/or other materials
21  *        provided with the distribution.
22  *
23  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
30  * SOFTWARE.
31  */
32
33 #include <net/tc_act/tc_gact.h>
34 #include <net/pkt_cls.h>
35 #include <linux/mlx5/fs.h>
36 #include <net/vxlan.h>
37 #include "en.h"
38 #include "en_tc.h"
39 #include "eswitch.h"
40 #include "vxlan.h"
41
42 enum {
43         MLX5_EN_QP_FLUSH_TIMEOUT_MS     = 5000,
44         MLX5_EN_QP_FLUSH_MSLEEP_QUANT   = 20,
45         MLX5_EN_QP_FLUSH_MAX_ITER       = MLX5_EN_QP_FLUSH_TIMEOUT_MS /
46                                           MLX5_EN_QP_FLUSH_MSLEEP_QUANT,
47 };
48
49 struct mlx5e_rq_param {
50         u32                     rqc[MLX5_ST_SZ_DW(rqc)];
51         struct mlx5_wq_param    wq;
52         bool                    am_enabled;
53 };
54
55 struct mlx5e_sq_param {
56         u32                        sqc[MLX5_ST_SZ_DW(sqc)];
57         struct mlx5_wq_param       wq;
58         u16                        max_inline;
59         u8                         min_inline_mode;
60         bool                       icosq;
61 };
62
63 struct mlx5e_cq_param {
64         u32                        cqc[MLX5_ST_SZ_DW(cqc)];
65         struct mlx5_wq_param       wq;
66         u16                        eq_ix;
67         u8                         cq_period_mode;
68 };
69
70 struct mlx5e_channel_param {
71         struct mlx5e_rq_param      rq;
72         struct mlx5e_sq_param      sq;
73         struct mlx5e_sq_param      icosq;
74         struct mlx5e_cq_param      rx_cq;
75         struct mlx5e_cq_param      tx_cq;
76         struct mlx5e_cq_param      icosq_cq;
77 };
78
79 static void mlx5e_update_carrier(struct mlx5e_priv *priv)
80 {
81         struct mlx5_core_dev *mdev = priv->mdev;
82         u8 port_state;
83
84         port_state = mlx5_query_vport_state(mdev,
85                 MLX5_QUERY_VPORT_STATE_IN_OP_MOD_VNIC_VPORT, 0);
86
87         if (port_state == VPORT_STATE_UP) {
88                 netdev_info(priv->netdev, "Link up\n");
89                 netif_carrier_on(priv->netdev);
90         } else {
91                 netdev_info(priv->netdev, "Link down\n");
92                 netif_carrier_off(priv->netdev);
93         }
94 }
95
96 static void mlx5e_update_carrier_work(struct work_struct *work)
97 {
98         struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
99                                                update_carrier_work);
100
101         mutex_lock(&priv->state_lock);
102         if (test_bit(MLX5E_STATE_OPENED, &priv->state))
103                 mlx5e_update_carrier(priv);
104         mutex_unlock(&priv->state_lock);
105 }
106
107 static void mlx5e_tx_timeout_work(struct work_struct *work)
108 {
109         struct mlx5e_priv *priv = container_of(work, struct mlx5e_priv,
110                                                tx_timeout_work);
111         int err;
112
113         rtnl_lock();
114         mutex_lock(&priv->state_lock);
115         if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
116                 goto unlock;
117         mlx5e_close_locked(priv->netdev);
118         err = mlx5e_open_locked(priv->netdev);
119         if (err)
120                 netdev_err(priv->netdev, "mlx5e_open_locked failed recovering from a tx_timeout, err(%d).\n",
121                            err);
122 unlock:
123         mutex_unlock(&priv->state_lock);
124         rtnl_unlock();
125 }
126
127 static void mlx5e_update_sw_counters(struct mlx5e_priv *priv)
128 {
129         struct mlx5e_sw_stats *s = &priv->stats.sw;
130         struct mlx5e_rq_stats *rq_stats;
131         struct mlx5e_sq_stats *sq_stats;
132         u64 tx_offload_none = 0;
133         int i, j;
134
135         memset(s, 0, sizeof(*s));
136         for (i = 0; i < priv->params.num_channels; i++) {
137                 rq_stats = &priv->channel[i]->rq.stats;
138
139                 s->rx_packets   += rq_stats->packets;
140                 s->rx_bytes     += rq_stats->bytes;
141                 s->rx_lro_packets += rq_stats->lro_packets;
142                 s->rx_lro_bytes += rq_stats->lro_bytes;
143                 s->rx_csum_none += rq_stats->csum_none;
144                 s->rx_csum_complete += rq_stats->csum_complete;
145                 s->rx_csum_unnecessary_inner += rq_stats->csum_unnecessary_inner;
146                 s->rx_wqe_err   += rq_stats->wqe_err;
147                 s->rx_mpwqe_filler += rq_stats->mpwqe_filler;
148                 s->rx_mpwqe_frag   += rq_stats->mpwqe_frag;
149                 s->rx_buff_alloc_err += rq_stats->buff_alloc_err;
150                 s->rx_cqe_compress_blks += rq_stats->cqe_compress_blks;
151                 s->rx_cqe_compress_pkts += rq_stats->cqe_compress_pkts;
152
153                 for (j = 0; j < priv->params.num_tc; j++) {
154                         sq_stats = &priv->channel[i]->sq[j].stats;
155
156                         s->tx_packets           += sq_stats->packets;
157                         s->tx_bytes             += sq_stats->bytes;
158                         s->tx_tso_packets       += sq_stats->tso_packets;
159                         s->tx_tso_bytes         += sq_stats->tso_bytes;
160                         s->tx_tso_inner_packets += sq_stats->tso_inner_packets;
161                         s->tx_tso_inner_bytes   += sq_stats->tso_inner_bytes;
162                         s->tx_queue_stopped     += sq_stats->stopped;
163                         s->tx_queue_wake        += sq_stats->wake;
164                         s->tx_queue_dropped     += sq_stats->dropped;
165                         s->tx_csum_partial_inner += sq_stats->csum_partial_inner;
166                         tx_offload_none         += sq_stats->csum_none;
167                 }
168         }
169
170         /* Update calculated offload counters */
171         s->tx_csum_partial = s->tx_packets - tx_offload_none - s->tx_csum_partial_inner;
172         s->rx_csum_unnecessary = s->rx_packets - s->rx_csum_none - s->rx_csum_complete;
173
174         s->link_down_events_phy = MLX5_GET(ppcnt_reg,
175                                 priv->stats.pport.phy_counters,
176                                 counter_set.phys_layer_cntrs.link_down_events);
177 }
178
179 static void mlx5e_update_vport_counters(struct mlx5e_priv *priv)
180 {
181         int outlen = MLX5_ST_SZ_BYTES(query_vport_counter_out);
182         u32 *out = (u32 *)priv->stats.vport.query_vport_out;
183         u32 in[MLX5_ST_SZ_DW(query_vport_counter_in)] = {0};
184         struct mlx5_core_dev *mdev = priv->mdev;
185
186         MLX5_SET(query_vport_counter_in, in, opcode,
187                  MLX5_CMD_OP_QUERY_VPORT_COUNTER);
188         MLX5_SET(query_vport_counter_in, in, op_mod, 0);
189         MLX5_SET(query_vport_counter_in, in, other_vport, 0);
190
191         memset(out, 0, outlen);
192         mlx5_cmd_exec(mdev, in, sizeof(in), out, outlen);
193 }
194
195 static void mlx5e_update_pport_counters(struct mlx5e_priv *priv)
196 {
197         struct mlx5e_pport_stats *pstats = &priv->stats.pport;
198         struct mlx5_core_dev *mdev = priv->mdev;
199         int sz = MLX5_ST_SZ_BYTES(ppcnt_reg);
200         int prio;
201         void *out;
202         u32 *in;
203
204         in = mlx5_vzalloc(sz);
205         if (!in)
206                 goto free_out;
207
208         MLX5_SET(ppcnt_reg, in, local_port, 1);
209
210         out = pstats->IEEE_802_3_counters;
211         MLX5_SET(ppcnt_reg, in, grp, MLX5_IEEE_802_3_COUNTERS_GROUP);
212         mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
213
214         out = pstats->RFC_2863_counters;
215         MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2863_COUNTERS_GROUP);
216         mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
217
218         out = pstats->RFC_2819_counters;
219         MLX5_SET(ppcnt_reg, in, grp, MLX5_RFC_2819_COUNTERS_GROUP);
220         mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
221
222         out = pstats->phy_counters;
223         MLX5_SET(ppcnt_reg, in, grp, MLX5_PHYSICAL_LAYER_COUNTERS_GROUP);
224         mlx5_core_access_reg(mdev, in, sz, out, sz, MLX5_REG_PPCNT, 0, 0);
225
226         MLX5_SET(ppcnt_reg, in, grp, MLX5_PER_PRIORITY_COUNTERS_GROUP);
227         for (prio = 0; prio < NUM_PPORT_PRIO; prio++) {
228                 out = pstats->per_prio_counters[prio];
229                 MLX5_SET(ppcnt_reg, in, prio_tc, prio);
230                 mlx5_core_access_reg(mdev, in, sz, out, sz,
231                                      MLX5_REG_PPCNT, 0, 0);
232         }
233
234 free_out:
235         kvfree(in);
236 }
237
238 static void mlx5e_update_q_counter(struct mlx5e_priv *priv)
239 {
240         struct mlx5e_qcounter_stats *qcnt = &priv->stats.qcnt;
241
242         if (!priv->q_counter)
243                 return;
244
245         mlx5_core_query_out_of_buffer(priv->mdev, priv->q_counter,
246                                       &qcnt->rx_out_of_buffer);
247 }
248
249 void mlx5e_update_stats(struct mlx5e_priv *priv)
250 {
251         mlx5e_update_q_counter(priv);
252         mlx5e_update_vport_counters(priv);
253         mlx5e_update_pport_counters(priv);
254         mlx5e_update_sw_counters(priv);
255 }
256
257 void mlx5e_update_stats_work(struct work_struct *work)
258 {
259         struct delayed_work *dwork = to_delayed_work(work);
260         struct mlx5e_priv *priv = container_of(dwork, struct mlx5e_priv,
261                                                update_stats_work);
262         mutex_lock(&priv->state_lock);
263         if (test_bit(MLX5E_STATE_OPENED, &priv->state)) {
264                 priv->profile->update_stats(priv);
265                 queue_delayed_work(priv->wq, dwork,
266                                    msecs_to_jiffies(MLX5E_UPDATE_STATS_INTERVAL));
267         }
268         mutex_unlock(&priv->state_lock);
269 }
270
271 static void mlx5e_async_event(struct mlx5_core_dev *mdev, void *vpriv,
272                               enum mlx5_dev_event event, unsigned long param)
273 {
274         struct mlx5e_priv *priv = vpriv;
275
276         if (!test_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state))
277                 return;
278
279         switch (event) {
280         case MLX5_DEV_EVENT_PORT_UP:
281         case MLX5_DEV_EVENT_PORT_DOWN:
282                 queue_work(priv->wq, &priv->update_carrier_work);
283                 break;
284
285         default:
286                 break;
287         }
288 }
289
290 static void mlx5e_enable_async_events(struct mlx5e_priv *priv)
291 {
292         set_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
293 }
294
295 static void mlx5e_disable_async_events(struct mlx5e_priv *priv)
296 {
297         clear_bit(MLX5E_STATE_ASYNC_EVENTS_ENABLED, &priv->state);
298         synchronize_irq(mlx5_get_msix_vec(priv->mdev, MLX5_EQ_VEC_ASYNC));
299 }
300
301 #define MLX5E_HW2SW_MTU(hwmtu) (hwmtu - (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
302 #define MLX5E_SW2HW_MTU(swmtu) (swmtu + (ETH_HLEN + VLAN_HLEN + ETH_FCS_LEN))
303
304 static int mlx5e_create_rq(struct mlx5e_channel *c,
305                            struct mlx5e_rq_param *param,
306                            struct mlx5e_rq *rq)
307 {
308         struct mlx5e_priv *priv = c->priv;
309         struct mlx5_core_dev *mdev = priv->mdev;
310         void *rqc = param->rqc;
311         void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
312         u32 byte_count;
313         int wq_sz;
314         int err;
315         int i;
316
317         param->wq.db_numa_node = cpu_to_node(c->cpu);
318
319         err = mlx5_wq_ll_create(mdev, &param->wq, rqc_wq, &rq->wq,
320                                 &rq->wq_ctrl);
321         if (err)
322                 return err;
323
324         rq->wq.db = &rq->wq.db[MLX5_RCV_DBR];
325
326         wq_sz = mlx5_wq_ll_get_size(&rq->wq);
327
328         switch (priv->params.rq_wq_type) {
329         case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
330                 rq->wqe_info = kzalloc_node(wq_sz * sizeof(*rq->wqe_info),
331                                             GFP_KERNEL, cpu_to_node(c->cpu));
332                 if (!rq->wqe_info) {
333                         err = -ENOMEM;
334                         goto err_rq_wq_destroy;
335                 }
336                 rq->handle_rx_cqe = mlx5e_handle_rx_cqe_mpwrq;
337                 rq->alloc_wqe = mlx5e_alloc_rx_mpwqe;
338                 rq->dealloc_wqe = mlx5e_dealloc_rx_mpwqe;
339
340                 rq->mpwqe_stride_sz = BIT(priv->params.mpwqe_log_stride_sz);
341                 rq->mpwqe_num_strides = BIT(priv->params.mpwqe_log_num_strides);
342                 rq->wqe_sz = rq->mpwqe_stride_sz * rq->mpwqe_num_strides;
343                 byte_count = rq->wqe_sz;
344                 break;
345         default: /* MLX5_WQ_TYPE_LINKED_LIST */
346                 rq->skb = kzalloc_node(wq_sz * sizeof(*rq->skb), GFP_KERNEL,
347                                        cpu_to_node(c->cpu));
348                 if (!rq->skb) {
349                         err = -ENOMEM;
350                         goto err_rq_wq_destroy;
351                 }
352                 rq->handle_rx_cqe = mlx5e_handle_rx_cqe;
353                 rq->alloc_wqe = mlx5e_alloc_rx_wqe;
354                 rq->dealloc_wqe = mlx5e_dealloc_rx_wqe;
355
356                 rq->wqe_sz = (priv->params.lro_en) ?
357                                 priv->params.lro_wqe_sz :
358                                 MLX5E_SW2HW_MTU(priv->netdev->mtu);
359                 rq->wqe_sz = SKB_DATA_ALIGN(rq->wqe_sz);
360                 byte_count = rq->wqe_sz;
361                 byte_count |= MLX5_HW_START_PADDING;
362         }
363
364         for (i = 0; i < wq_sz; i++) {
365                 struct mlx5e_rx_wqe *wqe = mlx5_wq_ll_get_wqe(&rq->wq, i);
366
367                 wqe->data.byte_count = cpu_to_be32(byte_count);
368         }
369
370         INIT_WORK(&rq->am.work, mlx5e_rx_am_work);
371         rq->am.mode = priv->params.rx_cq_period_mode;
372
373         rq->wq_type = priv->params.rq_wq_type;
374         rq->pdev    = c->pdev;
375         rq->netdev  = c->netdev;
376         rq->tstamp  = &priv->tstamp;
377         rq->channel = c;
378         rq->ix      = c->ix;
379         rq->priv    = c->priv;
380         rq->mkey_be = c->mkey_be;
381         rq->umr_mkey_be = cpu_to_be32(c->priv->umr_mkey.key);
382
383         return 0;
384
385 err_rq_wq_destroy:
386         mlx5_wq_destroy(&rq->wq_ctrl);
387
388         return err;
389 }
390
391 static void mlx5e_destroy_rq(struct mlx5e_rq *rq)
392 {
393         switch (rq->wq_type) {
394         case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
395                 kfree(rq->wqe_info);
396                 break;
397         default: /* MLX5_WQ_TYPE_LINKED_LIST */
398                 kfree(rq->skb);
399         }
400
401         mlx5_wq_destroy(&rq->wq_ctrl);
402 }
403
404 static int mlx5e_enable_rq(struct mlx5e_rq *rq, struct mlx5e_rq_param *param)
405 {
406         struct mlx5e_priv *priv = rq->priv;
407         struct mlx5_core_dev *mdev = priv->mdev;
408
409         void *in;
410         void *rqc;
411         void *wq;
412         int inlen;
413         int err;
414
415         inlen = MLX5_ST_SZ_BYTES(create_rq_in) +
416                 sizeof(u64) * rq->wq_ctrl.buf.npages;
417         in = mlx5_vzalloc(inlen);
418         if (!in)
419                 return -ENOMEM;
420
421         rqc = MLX5_ADDR_OF(create_rq_in, in, ctx);
422         wq  = MLX5_ADDR_OF(rqc, rqc, wq);
423
424         memcpy(rqc, param->rqc, sizeof(param->rqc));
425
426         MLX5_SET(rqc,  rqc, cqn,                rq->cq.mcq.cqn);
427         MLX5_SET(rqc,  rqc, state,              MLX5_RQC_STATE_RST);
428         MLX5_SET(rqc,  rqc, flush_in_error_en,  1);
429         MLX5_SET(rqc,  rqc, vsd, priv->params.vlan_strip_disable);
430         MLX5_SET(wq,   wq,  log_wq_pg_sz,       rq->wq_ctrl.buf.page_shift -
431                                                 MLX5_ADAPTER_PAGE_SHIFT);
432         MLX5_SET64(wq, wq,  dbr_addr,           rq->wq_ctrl.db.dma);
433
434         mlx5_fill_page_array(&rq->wq_ctrl.buf,
435                              (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
436
437         err = mlx5_core_create_rq(mdev, in, inlen, &rq->rqn);
438
439         kvfree(in);
440
441         return err;
442 }
443
444 static int mlx5e_modify_rq_state(struct mlx5e_rq *rq, int curr_state,
445                                  int next_state)
446 {
447         struct mlx5e_channel *c = rq->channel;
448         struct mlx5e_priv *priv = c->priv;
449         struct mlx5_core_dev *mdev = priv->mdev;
450
451         void *in;
452         void *rqc;
453         int inlen;
454         int err;
455
456         inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
457         in = mlx5_vzalloc(inlen);
458         if (!in)
459                 return -ENOMEM;
460
461         rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
462
463         MLX5_SET(modify_rq_in, in, rq_state, curr_state);
464         MLX5_SET(rqc, rqc, state, next_state);
465
466         err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
467
468         kvfree(in);
469
470         return err;
471 }
472
473 static int mlx5e_modify_rq_vsd(struct mlx5e_rq *rq, bool vsd)
474 {
475         struct mlx5e_channel *c = rq->channel;
476         struct mlx5e_priv *priv = c->priv;
477         struct mlx5_core_dev *mdev = priv->mdev;
478
479         void *in;
480         void *rqc;
481         int inlen;
482         int err;
483
484         inlen = MLX5_ST_SZ_BYTES(modify_rq_in);
485         in = mlx5_vzalloc(inlen);
486         if (!in)
487                 return -ENOMEM;
488
489         rqc = MLX5_ADDR_OF(modify_rq_in, in, ctx);
490
491         MLX5_SET(modify_rq_in, in, rq_state, MLX5_RQC_STATE_RDY);
492         MLX5_SET64(modify_rq_in, in, modify_bitmask,
493                    MLX5_MODIFY_RQ_IN_MODIFY_BITMASK_VSD);
494         MLX5_SET(rqc, rqc, vsd, vsd);
495         MLX5_SET(rqc, rqc, state, MLX5_RQC_STATE_RDY);
496
497         err = mlx5_core_modify_rq(mdev, rq->rqn, in, inlen);
498
499         kvfree(in);
500
501         return err;
502 }
503
504 static void mlx5e_disable_rq(struct mlx5e_rq *rq)
505 {
506         mlx5_core_destroy_rq(rq->priv->mdev, rq->rqn);
507 }
508
509 static int mlx5e_wait_for_min_rx_wqes(struct mlx5e_rq *rq)
510 {
511         unsigned long exp_time = jiffies + msecs_to_jiffies(20000);
512         struct mlx5e_channel *c = rq->channel;
513         struct mlx5e_priv *priv = c->priv;
514         struct mlx5_wq_ll *wq = &rq->wq;
515
516         while (time_before(jiffies, exp_time)) {
517                 if (wq->cur_sz >= priv->params.min_rx_wqes)
518                         return 0;
519
520                 msleep(20);
521         }
522
523         return -ETIMEDOUT;
524 }
525
526 static int mlx5e_open_rq(struct mlx5e_channel *c,
527                          struct mlx5e_rq_param *param,
528                          struct mlx5e_rq *rq)
529 {
530         struct mlx5e_sq *sq = &c->icosq;
531         u16 pi = sq->pc & sq->wq.sz_m1;
532         int err;
533
534         err = mlx5e_create_rq(c, param, rq);
535         if (err)
536                 return err;
537
538         err = mlx5e_enable_rq(rq, param);
539         if (err)
540                 goto err_destroy_rq;
541
542         err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RST, MLX5_RQC_STATE_RDY);
543         if (err)
544                 goto err_disable_rq;
545
546         if (param->am_enabled)
547                 set_bit(MLX5E_RQ_STATE_AM, &c->rq.state);
548
549         set_bit(MLX5E_RQ_STATE_POST_WQES_ENABLE, &rq->state);
550
551         sq->ico_wqe_info[pi].opcode     = MLX5_OPCODE_NOP;
552         sq->ico_wqe_info[pi].num_wqebbs = 1;
553         mlx5e_send_nop(sq, true); /* trigger mlx5e_post_rx_wqes() */
554
555         return 0;
556
557 err_disable_rq:
558         mlx5e_disable_rq(rq);
559 err_destroy_rq:
560         mlx5e_destroy_rq(rq);
561
562         return err;
563 }
564
565 static void mlx5e_close_rq(struct mlx5e_rq *rq)
566 {
567         int tout = 0;
568         int err;
569
570         clear_bit(MLX5E_RQ_STATE_POST_WQES_ENABLE, &rq->state);
571         napi_synchronize(&rq->channel->napi); /* prevent mlx5e_post_rx_wqes */
572
573         err = mlx5e_modify_rq_state(rq, MLX5_RQC_STATE_RDY, MLX5_RQC_STATE_ERR);
574         while (!mlx5_wq_ll_is_empty(&rq->wq) && !err &&
575                tout++ < MLX5_EN_QP_FLUSH_MAX_ITER)
576                 msleep(MLX5_EN_QP_FLUSH_MSLEEP_QUANT);
577
578         if (err || tout == MLX5_EN_QP_FLUSH_MAX_ITER)
579                 set_bit(MLX5E_RQ_STATE_FLUSH_TIMEOUT, &rq->state);
580
581         /* avoid destroying rq before mlx5e_poll_rx_cq() is done with it */
582         napi_synchronize(&rq->channel->napi);
583
584         cancel_work_sync(&rq->am.work);
585
586         mlx5e_disable_rq(rq);
587         mlx5e_free_rx_descs(rq);
588         mlx5e_destroy_rq(rq);
589 }
590
591 static void mlx5e_free_sq_db(struct mlx5e_sq *sq)
592 {
593         kfree(sq->wqe_info);
594         kfree(sq->dma_fifo);
595         kfree(sq->skb);
596 }
597
598 static int mlx5e_alloc_sq_db(struct mlx5e_sq *sq, int numa)
599 {
600         int wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
601         int df_sz = wq_sz * MLX5_SEND_WQEBB_NUM_DS;
602
603         sq->skb = kzalloc_node(wq_sz * sizeof(*sq->skb), GFP_KERNEL, numa);
604         sq->dma_fifo = kzalloc_node(df_sz * sizeof(*sq->dma_fifo), GFP_KERNEL,
605                                     numa);
606         sq->wqe_info = kzalloc_node(wq_sz * sizeof(*sq->wqe_info), GFP_KERNEL,
607                                     numa);
608
609         if (!sq->skb || !sq->dma_fifo || !sq->wqe_info) {
610                 mlx5e_free_sq_db(sq);
611                 return -ENOMEM;
612         }
613
614         sq->dma_fifo_mask = df_sz - 1;
615
616         return 0;
617 }
618
619 static int mlx5e_create_sq(struct mlx5e_channel *c,
620                            int tc,
621                            struct mlx5e_sq_param *param,
622                            struct mlx5e_sq *sq)
623 {
624         struct mlx5e_priv *priv = c->priv;
625         struct mlx5_core_dev *mdev = priv->mdev;
626
627         void *sqc = param->sqc;
628         void *sqc_wq = MLX5_ADDR_OF(sqc, sqc, wq);
629         int err;
630
631         err = mlx5_alloc_map_uar(mdev, &sq->uar, !!MLX5_CAP_GEN(mdev, bf));
632         if (err)
633                 return err;
634
635         param->wq.db_numa_node = cpu_to_node(c->cpu);
636
637         err = mlx5_wq_cyc_create(mdev, &param->wq, sqc_wq, &sq->wq,
638                                  &sq->wq_ctrl);
639         if (err)
640                 goto err_unmap_free_uar;
641
642         sq->wq.db       = &sq->wq.db[MLX5_SND_DBR];
643         if (sq->uar.bf_map) {
644                 set_bit(MLX5E_SQ_STATE_BF_ENABLE, &sq->state);
645                 sq->uar_map = sq->uar.bf_map;
646         } else {
647                 sq->uar_map = sq->uar.map;
648         }
649         sq->bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
650         sq->max_inline  = param->max_inline;
651         sq->min_inline_mode =
652                 MLX5_CAP_ETH(mdev, wqe_inline_mode) == MLX5E_INLINE_MODE_VPORT_CONTEXT ?
653                 param->min_inline_mode : 0;
654
655         err = mlx5e_alloc_sq_db(sq, cpu_to_node(c->cpu));
656         if (err)
657                 goto err_sq_wq_destroy;
658
659         if (param->icosq) {
660                 u8 wq_sz = mlx5_wq_cyc_get_size(&sq->wq);
661
662                 sq->ico_wqe_info = kzalloc_node(sizeof(*sq->ico_wqe_info) *
663                                                 wq_sz,
664                                                 GFP_KERNEL,
665                                                 cpu_to_node(c->cpu));
666                 if (!sq->ico_wqe_info) {
667                         err = -ENOMEM;
668                         goto err_free_sq_db;
669                 }
670         } else {
671                 int txq_ix;
672
673                 txq_ix = c->ix + tc * priv->params.num_channels;
674                 sq->txq = netdev_get_tx_queue(priv->netdev, txq_ix);
675                 priv->txq_to_sq_map[txq_ix] = sq;
676         }
677
678         sq->pdev      = c->pdev;
679         sq->tstamp    = &priv->tstamp;
680         sq->mkey_be   = c->mkey_be;
681         sq->channel   = c;
682         sq->tc        = tc;
683         sq->edge      = (sq->wq.sz_m1 + 1) - MLX5_SEND_WQE_MAX_WQEBBS;
684         sq->bf_budget = MLX5E_SQ_BF_BUDGET;
685
686         return 0;
687
688 err_free_sq_db:
689         mlx5e_free_sq_db(sq);
690
691 err_sq_wq_destroy:
692         mlx5_wq_destroy(&sq->wq_ctrl);
693
694 err_unmap_free_uar:
695         mlx5_unmap_free_uar(mdev, &sq->uar);
696
697         return err;
698 }
699
700 static void mlx5e_destroy_sq(struct mlx5e_sq *sq)
701 {
702         struct mlx5e_channel *c = sq->channel;
703         struct mlx5e_priv *priv = c->priv;
704
705         kfree(sq->ico_wqe_info);
706         mlx5e_free_sq_db(sq);
707         mlx5_wq_destroy(&sq->wq_ctrl);
708         mlx5_unmap_free_uar(priv->mdev, &sq->uar);
709 }
710
711 static int mlx5e_enable_sq(struct mlx5e_sq *sq, struct mlx5e_sq_param *param)
712 {
713         struct mlx5e_channel *c = sq->channel;
714         struct mlx5e_priv *priv = c->priv;
715         struct mlx5_core_dev *mdev = priv->mdev;
716
717         void *in;
718         void *sqc;
719         void *wq;
720         int inlen;
721         int err;
722
723         inlen = MLX5_ST_SZ_BYTES(create_sq_in) +
724                 sizeof(u64) * sq->wq_ctrl.buf.npages;
725         in = mlx5_vzalloc(inlen);
726         if (!in)
727                 return -ENOMEM;
728
729         sqc = MLX5_ADDR_OF(create_sq_in, in, ctx);
730         wq = MLX5_ADDR_OF(sqc, sqc, wq);
731
732         memcpy(sqc, param->sqc, sizeof(param->sqc));
733
734         MLX5_SET(sqc,  sqc, tis_num_0, param->icosq ? 0 : priv->tisn[sq->tc]);
735         MLX5_SET(sqc,  sqc, cqn,                sq->cq.mcq.cqn);
736         MLX5_SET(sqc,  sqc, min_wqe_inline_mode, sq->min_inline_mode);
737         MLX5_SET(sqc,  sqc, state,              MLX5_SQC_STATE_RST);
738         MLX5_SET(sqc,  sqc, tis_lst_sz,         param->icosq ? 0 : 1);
739         MLX5_SET(sqc,  sqc, flush_in_error_en,  1);
740
741         MLX5_SET(wq,   wq, wq_type,       MLX5_WQ_TYPE_CYCLIC);
742         MLX5_SET(wq,   wq, uar_page,      sq->uar.index);
743         MLX5_SET(wq,   wq, log_wq_pg_sz,  sq->wq_ctrl.buf.page_shift -
744                                           MLX5_ADAPTER_PAGE_SHIFT);
745         MLX5_SET64(wq, wq, dbr_addr,      sq->wq_ctrl.db.dma);
746
747         mlx5_fill_page_array(&sq->wq_ctrl.buf,
748                              (__be64 *)MLX5_ADDR_OF(wq, wq, pas));
749
750         err = mlx5_core_create_sq(mdev, in, inlen, &sq->sqn);
751
752         kvfree(in);
753
754         return err;
755 }
756
757 static int mlx5e_modify_sq(struct mlx5e_sq *sq, int curr_state,
758                            int next_state, bool update_rl, int rl_index)
759 {
760         struct mlx5e_channel *c = sq->channel;
761         struct mlx5e_priv *priv = c->priv;
762         struct mlx5_core_dev *mdev = priv->mdev;
763
764         void *in;
765         void *sqc;
766         int inlen;
767         int err;
768
769         inlen = MLX5_ST_SZ_BYTES(modify_sq_in);
770         in = mlx5_vzalloc(inlen);
771         if (!in)
772                 return -ENOMEM;
773
774         sqc = MLX5_ADDR_OF(modify_sq_in, in, ctx);
775
776         MLX5_SET(modify_sq_in, in, sq_state, curr_state);
777         MLX5_SET(sqc, sqc, state, next_state);
778         if (update_rl && next_state == MLX5_SQC_STATE_RDY) {
779                 MLX5_SET64(modify_sq_in, in, modify_bitmask, 1);
780                 MLX5_SET(sqc,  sqc, packet_pacing_rate_limit_index, rl_index);
781         }
782
783         err = mlx5_core_modify_sq(mdev, sq->sqn, in, inlen);
784
785         kvfree(in);
786
787         return err;
788 }
789
790 static void mlx5e_disable_sq(struct mlx5e_sq *sq)
791 {
792         struct mlx5e_channel *c = sq->channel;
793         struct mlx5e_priv *priv = c->priv;
794         struct mlx5_core_dev *mdev = priv->mdev;
795
796         mlx5_core_destroy_sq(mdev, sq->sqn);
797         if (sq->rate_limit)
798                 mlx5_rl_remove_rate(mdev, sq->rate_limit);
799 }
800
801 static int mlx5e_open_sq(struct mlx5e_channel *c,
802                          int tc,
803                          struct mlx5e_sq_param *param,
804                          struct mlx5e_sq *sq)
805 {
806         int err;
807
808         err = mlx5e_create_sq(c, tc, param, sq);
809         if (err)
810                 return err;
811
812         err = mlx5e_enable_sq(sq, param);
813         if (err)
814                 goto err_destroy_sq;
815
816         err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RST, MLX5_SQC_STATE_RDY,
817                               false, 0);
818         if (err)
819                 goto err_disable_sq;
820
821         if (sq->txq) {
822                 set_bit(MLX5E_SQ_STATE_WAKE_TXQ_ENABLE, &sq->state);
823                 netdev_tx_reset_queue(sq->txq);
824                 netif_tx_start_queue(sq->txq);
825         }
826
827         return 0;
828
829 err_disable_sq:
830         mlx5e_disable_sq(sq);
831 err_destroy_sq:
832         mlx5e_destroy_sq(sq);
833
834         return err;
835 }
836
837 static inline void netif_tx_disable_queue(struct netdev_queue *txq)
838 {
839         __netif_tx_lock_bh(txq);
840         netif_tx_stop_queue(txq);
841         __netif_tx_unlock_bh(txq);
842 }
843
844 static void mlx5e_close_sq(struct mlx5e_sq *sq)
845 {
846         int tout = 0;
847         int err;
848
849         if (sq->txq) {
850                 clear_bit(MLX5E_SQ_STATE_WAKE_TXQ_ENABLE, &sq->state);
851                 /* prevent netif_tx_wake_queue */
852                 napi_synchronize(&sq->channel->napi);
853                 netif_tx_disable_queue(sq->txq);
854
855                 /* ensure hw is notified of all pending wqes */
856                 if (mlx5e_sq_has_room_for(sq, 1))
857                         mlx5e_send_nop(sq, true);
858
859                 err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY,
860                                       MLX5_SQC_STATE_ERR, false, 0);
861                 if (err)
862                         set_bit(MLX5E_SQ_STATE_TX_TIMEOUT, &sq->state);
863         }
864
865         /* wait till sq is empty, unless a TX timeout occurred on this SQ */
866         while (sq->cc != sq->pc &&
867                !test_bit(MLX5E_SQ_STATE_TX_TIMEOUT, &sq->state)) {
868                 msleep(MLX5_EN_QP_FLUSH_MSLEEP_QUANT);
869                 if (tout++ > MLX5_EN_QP_FLUSH_MAX_ITER)
870                         set_bit(MLX5E_SQ_STATE_TX_TIMEOUT, &sq->state);
871         }
872
873         /* avoid destroying sq before mlx5e_poll_tx_cq() is done with it */
874         napi_synchronize(&sq->channel->napi);
875
876         mlx5e_free_tx_descs(sq);
877         mlx5e_disable_sq(sq);
878         mlx5e_destroy_sq(sq);
879 }
880
881 static int mlx5e_create_cq(struct mlx5e_channel *c,
882                            struct mlx5e_cq_param *param,
883                            struct mlx5e_cq *cq)
884 {
885         struct mlx5e_priv *priv = c->priv;
886         struct mlx5_core_dev *mdev = priv->mdev;
887         struct mlx5_core_cq *mcq = &cq->mcq;
888         int eqn_not_used;
889         unsigned int irqn;
890         int err;
891         u32 i;
892
893         param->wq.buf_numa_node = cpu_to_node(c->cpu);
894         param->wq.db_numa_node  = cpu_to_node(c->cpu);
895         param->eq_ix   = c->ix;
896
897         err = mlx5_cqwq_create(mdev, &param->wq, param->cqc, &cq->wq,
898                                &cq->wq_ctrl);
899         if (err)
900                 return err;
901
902         mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
903
904         cq->napi        = &c->napi;
905
906         mcq->cqe_sz     = 64;
907         mcq->set_ci_db  = cq->wq_ctrl.db.db;
908         mcq->arm_db     = cq->wq_ctrl.db.db + 1;
909         *mcq->set_ci_db = 0;
910         *mcq->arm_db    = 0;
911         mcq->vector     = param->eq_ix;
912         mcq->comp       = mlx5e_completion_event;
913         mcq->event      = mlx5e_cq_error_event;
914         mcq->irqn       = irqn;
915         mcq->uar        = &mdev->mlx5e_res.cq_uar;
916
917         for (i = 0; i < mlx5_cqwq_get_size(&cq->wq); i++) {
918                 struct mlx5_cqe64 *cqe = mlx5_cqwq_get_wqe(&cq->wq, i);
919
920                 cqe->op_own = 0xf1;
921         }
922
923         cq->channel = c;
924         cq->priv = priv;
925
926         return 0;
927 }
928
929 static void mlx5e_destroy_cq(struct mlx5e_cq *cq)
930 {
931         mlx5_wq_destroy(&cq->wq_ctrl);
932 }
933
934 static int mlx5e_enable_cq(struct mlx5e_cq *cq, struct mlx5e_cq_param *param)
935 {
936         struct mlx5e_priv *priv = cq->priv;
937         struct mlx5_core_dev *mdev = priv->mdev;
938         struct mlx5_core_cq *mcq = &cq->mcq;
939
940         void *in;
941         void *cqc;
942         int inlen;
943         unsigned int irqn_not_used;
944         int eqn;
945         int err;
946
947         inlen = MLX5_ST_SZ_BYTES(create_cq_in) +
948                 sizeof(u64) * cq->wq_ctrl.buf.npages;
949         in = mlx5_vzalloc(inlen);
950         if (!in)
951                 return -ENOMEM;
952
953         cqc = MLX5_ADDR_OF(create_cq_in, in, cq_context);
954
955         memcpy(cqc, param->cqc, sizeof(param->cqc));
956
957         mlx5_fill_page_array(&cq->wq_ctrl.buf,
958                              (__be64 *)MLX5_ADDR_OF(create_cq_in, in, pas));
959
960         mlx5_vector2eqn(mdev, param->eq_ix, &eqn, &irqn_not_used);
961
962         MLX5_SET(cqc,   cqc, cq_period_mode, param->cq_period_mode);
963         MLX5_SET(cqc,   cqc, c_eqn,         eqn);
964         MLX5_SET(cqc,   cqc, uar_page,      mcq->uar->index);
965         MLX5_SET(cqc,   cqc, log_page_size, cq->wq_ctrl.buf.page_shift -
966                                             MLX5_ADAPTER_PAGE_SHIFT);
967         MLX5_SET64(cqc, cqc, dbr_addr,      cq->wq_ctrl.db.dma);
968
969         err = mlx5_core_create_cq(mdev, mcq, in, inlen);
970
971         kvfree(in);
972
973         if (err)
974                 return err;
975
976         mlx5e_cq_arm(cq);
977
978         return 0;
979 }
980
981 static void mlx5e_disable_cq(struct mlx5e_cq *cq)
982 {
983         struct mlx5e_priv *priv = cq->priv;
984         struct mlx5_core_dev *mdev = priv->mdev;
985
986         mlx5_core_destroy_cq(mdev, &cq->mcq);
987 }
988
989 static int mlx5e_open_cq(struct mlx5e_channel *c,
990                          struct mlx5e_cq_param *param,
991                          struct mlx5e_cq *cq,
992                          struct mlx5e_cq_moder moderation)
993 {
994         int err;
995         struct mlx5e_priv *priv = c->priv;
996         struct mlx5_core_dev *mdev = priv->mdev;
997
998         err = mlx5e_create_cq(c, param, cq);
999         if (err)
1000                 return err;
1001
1002         err = mlx5e_enable_cq(cq, param);
1003         if (err)
1004                 goto err_destroy_cq;
1005
1006         if (MLX5_CAP_GEN(mdev, cq_moderation))
1007                 mlx5_core_modify_cq_moderation(mdev, &cq->mcq,
1008                                                moderation.usec,
1009                                                moderation.pkts);
1010         return 0;
1011
1012 err_destroy_cq:
1013         mlx5e_destroy_cq(cq);
1014
1015         return err;
1016 }
1017
1018 static void mlx5e_close_cq(struct mlx5e_cq *cq)
1019 {
1020         mlx5e_disable_cq(cq);
1021         mlx5e_destroy_cq(cq);
1022 }
1023
1024 static int mlx5e_get_cpu(struct mlx5e_priv *priv, int ix)
1025 {
1026         return cpumask_first(priv->mdev->priv.irq_info[ix].mask);
1027 }
1028
1029 static int mlx5e_open_tx_cqs(struct mlx5e_channel *c,
1030                              struct mlx5e_channel_param *cparam)
1031 {
1032         struct mlx5e_priv *priv = c->priv;
1033         int err;
1034         int tc;
1035
1036         for (tc = 0; tc < c->num_tc; tc++) {
1037                 err = mlx5e_open_cq(c, &cparam->tx_cq, &c->sq[tc].cq,
1038                                     priv->params.tx_cq_moderation);
1039                 if (err)
1040                         goto err_close_tx_cqs;
1041         }
1042
1043         return 0;
1044
1045 err_close_tx_cqs:
1046         for (tc--; tc >= 0; tc--)
1047                 mlx5e_close_cq(&c->sq[tc].cq);
1048
1049         return err;
1050 }
1051
1052 static void mlx5e_close_tx_cqs(struct mlx5e_channel *c)
1053 {
1054         int tc;
1055
1056         for (tc = 0; tc < c->num_tc; tc++)
1057                 mlx5e_close_cq(&c->sq[tc].cq);
1058 }
1059
1060 static int mlx5e_open_sqs(struct mlx5e_channel *c,
1061                           struct mlx5e_channel_param *cparam)
1062 {
1063         int err;
1064         int tc;
1065
1066         for (tc = 0; tc < c->num_tc; tc++) {
1067                 err = mlx5e_open_sq(c, tc, &cparam->sq, &c->sq[tc]);
1068                 if (err)
1069                         goto err_close_sqs;
1070         }
1071
1072         return 0;
1073
1074 err_close_sqs:
1075         for (tc--; tc >= 0; tc--)
1076                 mlx5e_close_sq(&c->sq[tc]);
1077
1078         return err;
1079 }
1080
1081 static void mlx5e_close_sqs(struct mlx5e_channel *c)
1082 {
1083         int tc;
1084
1085         for (tc = 0; tc < c->num_tc; tc++)
1086                 mlx5e_close_sq(&c->sq[tc]);
1087 }
1088
1089 static void mlx5e_build_channeltc_to_txq_map(struct mlx5e_priv *priv, int ix)
1090 {
1091         int i;
1092
1093         for (i = 0; i < priv->profile->max_tc; i++)
1094                 priv->channeltc_to_txq_map[ix][i] =
1095                         ix + i * priv->params.num_channels;
1096 }
1097
1098 static int mlx5e_set_sq_maxrate(struct net_device *dev,
1099                                 struct mlx5e_sq *sq, u32 rate)
1100 {
1101         struct mlx5e_priv *priv = netdev_priv(dev);
1102         struct mlx5_core_dev *mdev = priv->mdev;
1103         u16 rl_index = 0;
1104         int err;
1105
1106         if (rate == sq->rate_limit)
1107                 /* nothing to do */
1108                 return 0;
1109
1110         if (sq->rate_limit)
1111                 /* remove current rl index to free space to next ones */
1112                 mlx5_rl_remove_rate(mdev, sq->rate_limit);
1113
1114         sq->rate_limit = 0;
1115
1116         if (rate) {
1117                 err = mlx5_rl_add_rate(mdev, rate, &rl_index);
1118                 if (err) {
1119                         netdev_err(dev, "Failed configuring rate %u: %d\n",
1120                                    rate, err);
1121                         return err;
1122                 }
1123         }
1124
1125         err = mlx5e_modify_sq(sq, MLX5_SQC_STATE_RDY,
1126                               MLX5_SQC_STATE_RDY, true, rl_index);
1127         if (err) {
1128                 netdev_err(dev, "Failed configuring rate %u: %d\n",
1129                            rate, err);
1130                 /* remove the rate from the table */
1131                 if (rate)
1132                         mlx5_rl_remove_rate(mdev, rate);
1133                 return err;
1134         }
1135
1136         sq->rate_limit = rate;
1137         return 0;
1138 }
1139
1140 static int mlx5e_set_tx_maxrate(struct net_device *dev, int index, u32 rate)
1141 {
1142         struct mlx5e_priv *priv = netdev_priv(dev);
1143         struct mlx5_core_dev *mdev = priv->mdev;
1144         struct mlx5e_sq *sq = priv->txq_to_sq_map[index];
1145         int err = 0;
1146
1147         if (!mlx5_rl_is_supported(mdev)) {
1148                 netdev_err(dev, "Rate limiting is not supported on this device\n");
1149                 return -EINVAL;
1150         }
1151
1152         /* rate is given in Mb/sec, HW config is in Kb/sec */
1153         rate = rate << 10;
1154
1155         /* Check whether rate in valid range, 0 is always valid */
1156         if (rate && !mlx5_rl_is_in_range(mdev, rate)) {
1157                 netdev_err(dev, "TX rate %u, is not in range\n", rate);
1158                 return -ERANGE;
1159         }
1160
1161         mutex_lock(&priv->state_lock);
1162         if (test_bit(MLX5E_STATE_OPENED, &priv->state))
1163                 err = mlx5e_set_sq_maxrate(dev, sq, rate);
1164         if (!err)
1165                 priv->tx_rates[index] = rate;
1166         mutex_unlock(&priv->state_lock);
1167
1168         return err;
1169 }
1170
1171 static int mlx5e_open_channel(struct mlx5e_priv *priv, int ix,
1172                               struct mlx5e_channel_param *cparam,
1173                               struct mlx5e_channel **cp)
1174 {
1175         struct mlx5e_cq_moder icosq_cq_moder = {0, 0};
1176         struct net_device *netdev = priv->netdev;
1177         struct mlx5e_cq_moder rx_cq_profile;
1178         int cpu = mlx5e_get_cpu(priv, ix);
1179         struct mlx5e_channel *c;
1180         struct mlx5e_sq *sq;
1181         int err;
1182         int i;
1183
1184         c = kzalloc_node(sizeof(*c), GFP_KERNEL, cpu_to_node(cpu));
1185         if (!c)
1186                 return -ENOMEM;
1187
1188         c->priv     = priv;
1189         c->ix       = ix;
1190         c->cpu      = cpu;
1191         c->pdev     = &priv->mdev->pdev->dev;
1192         c->netdev   = priv->netdev;
1193         c->mkey_be  = cpu_to_be32(priv->mdev->mlx5e_res.mkey.key);
1194         c->num_tc   = priv->params.num_tc;
1195
1196         if (priv->params.rx_am_enabled)
1197                 rx_cq_profile = mlx5e_am_get_def_profile(priv->params.rx_cq_period_mode);
1198         else
1199                 rx_cq_profile = priv->params.rx_cq_moderation;
1200
1201         mlx5e_build_channeltc_to_txq_map(priv, ix);
1202
1203         netif_napi_add(netdev, &c->napi, mlx5e_napi_poll, 64);
1204
1205         err = mlx5e_open_cq(c, &cparam->icosq_cq, &c->icosq.cq, icosq_cq_moder);
1206         if (err)
1207                 goto err_napi_del;
1208
1209         err = mlx5e_open_tx_cqs(c, cparam);
1210         if (err)
1211                 goto err_close_icosq_cq;
1212
1213         err = mlx5e_open_cq(c, &cparam->rx_cq, &c->rq.cq,
1214                             rx_cq_profile);
1215         if (err)
1216                 goto err_close_tx_cqs;
1217
1218         napi_enable(&c->napi);
1219
1220         err = mlx5e_open_sq(c, 0, &cparam->icosq, &c->icosq);
1221         if (err)
1222                 goto err_disable_napi;
1223
1224         err = mlx5e_open_sqs(c, cparam);
1225         if (err)
1226                 goto err_close_icosq;
1227
1228         for (i = 0; i < priv->params.num_tc; i++) {
1229                 u32 txq_ix = priv->channeltc_to_txq_map[ix][i];
1230
1231                 if (priv->tx_rates[txq_ix]) {
1232                         sq = priv->txq_to_sq_map[txq_ix];
1233                         mlx5e_set_sq_maxrate(priv->netdev, sq,
1234                                              priv->tx_rates[txq_ix]);
1235                 }
1236         }
1237
1238         err = mlx5e_open_rq(c, &cparam->rq, &c->rq);
1239         if (err)
1240                 goto err_close_sqs;
1241
1242         netif_set_xps_queue(netdev, get_cpu_mask(c->cpu), ix);
1243         *cp = c;
1244
1245         return 0;
1246
1247 err_close_sqs:
1248         mlx5e_close_sqs(c);
1249
1250 err_close_icosq:
1251         mlx5e_close_sq(&c->icosq);
1252
1253 err_disable_napi:
1254         napi_disable(&c->napi);
1255         mlx5e_close_cq(&c->rq.cq);
1256
1257 err_close_tx_cqs:
1258         mlx5e_close_tx_cqs(c);
1259
1260 err_close_icosq_cq:
1261         mlx5e_close_cq(&c->icosq.cq);
1262
1263 err_napi_del:
1264         netif_napi_del(&c->napi);
1265         napi_hash_del(&c->napi);
1266         kfree(c);
1267
1268         return err;
1269 }
1270
1271 static void mlx5e_close_channel(struct mlx5e_channel *c)
1272 {
1273         mlx5e_close_rq(&c->rq);
1274         mlx5e_close_sqs(c);
1275         mlx5e_close_sq(&c->icosq);
1276         napi_disable(&c->napi);
1277         mlx5e_close_cq(&c->rq.cq);
1278         mlx5e_close_tx_cqs(c);
1279         mlx5e_close_cq(&c->icosq.cq);
1280         netif_napi_del(&c->napi);
1281
1282         napi_hash_del(&c->napi);
1283         synchronize_rcu();
1284
1285         kfree(c);
1286 }
1287
1288 static void mlx5e_build_rq_param(struct mlx5e_priv *priv,
1289                                  struct mlx5e_rq_param *param)
1290 {
1291         void *rqc = param->rqc;
1292         void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1293
1294         switch (priv->params.rq_wq_type) {
1295         case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1296                 MLX5_SET(wq, wq, log_wqe_num_of_strides,
1297                          priv->params.mpwqe_log_num_strides - 9);
1298                 MLX5_SET(wq, wq, log_wqe_stride_size,
1299                          priv->params.mpwqe_log_stride_sz - 6);
1300                 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ);
1301                 break;
1302         default: /* MLX5_WQ_TYPE_LINKED_LIST */
1303                 MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1304         }
1305
1306         MLX5_SET(wq, wq, end_padding_mode, MLX5_WQ_END_PAD_MODE_ALIGN);
1307         MLX5_SET(wq, wq, log_wq_stride,    ilog2(sizeof(struct mlx5e_rx_wqe)));
1308         MLX5_SET(wq, wq, log_wq_sz,        priv->params.log_rq_size);
1309         MLX5_SET(wq, wq, pd,               priv->mdev->mlx5e_res.pdn);
1310         MLX5_SET(rqc, rqc, counter_set_id, priv->q_counter);
1311
1312         param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1313         param->wq.linear = 1;
1314
1315         param->am_enabled = priv->params.rx_am_enabled;
1316 }
1317
1318 static void mlx5e_build_drop_rq_param(struct mlx5e_rq_param *param)
1319 {
1320         void *rqc = param->rqc;
1321         void *wq = MLX5_ADDR_OF(rqc, rqc, wq);
1322
1323         MLX5_SET(wq, wq, wq_type, MLX5_WQ_TYPE_LINKED_LIST);
1324         MLX5_SET(wq, wq, log_wq_stride,    ilog2(sizeof(struct mlx5e_rx_wqe)));
1325 }
1326
1327 static void mlx5e_build_sq_param_common(struct mlx5e_priv *priv,
1328                                         struct mlx5e_sq_param *param)
1329 {
1330         void *sqc = param->sqc;
1331         void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1332
1333         MLX5_SET(wq, wq, log_wq_stride, ilog2(MLX5_SEND_WQE_BB));
1334         MLX5_SET(wq, wq, pd,            priv->mdev->mlx5e_res.pdn);
1335
1336         param->wq.buf_numa_node = dev_to_node(&priv->mdev->pdev->dev);
1337 }
1338
1339 static void mlx5e_build_sq_param(struct mlx5e_priv *priv,
1340                                  struct mlx5e_sq_param *param)
1341 {
1342         void *sqc = param->sqc;
1343         void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1344
1345         mlx5e_build_sq_param_common(priv, param);
1346         MLX5_SET(wq, wq, log_wq_sz,     priv->params.log_sq_size);
1347
1348         param->max_inline = priv->params.tx_max_inline;
1349         param->min_inline_mode = priv->params.tx_min_inline_mode;
1350 }
1351
1352 static void mlx5e_build_common_cq_param(struct mlx5e_priv *priv,
1353                                         struct mlx5e_cq_param *param)
1354 {
1355         void *cqc = param->cqc;
1356
1357         MLX5_SET(cqc, cqc, uar_page, priv->mdev->mlx5e_res.cq_uar.index);
1358 }
1359
1360 static void mlx5e_build_rx_cq_param(struct mlx5e_priv *priv,
1361                                     struct mlx5e_cq_param *param)
1362 {
1363         void *cqc = param->cqc;
1364         u8 log_cq_size;
1365
1366         switch (priv->params.rq_wq_type) {
1367         case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
1368                 log_cq_size = priv->params.log_rq_size +
1369                         priv->params.mpwqe_log_num_strides;
1370                 break;
1371         default: /* MLX5_WQ_TYPE_LINKED_LIST */
1372                 log_cq_size = priv->params.log_rq_size;
1373         }
1374
1375         MLX5_SET(cqc, cqc, log_cq_size, log_cq_size);
1376         if (priv->params.rx_cqe_compress) {
1377                 MLX5_SET(cqc, cqc, mini_cqe_res_format, MLX5_CQE_FORMAT_CSUM);
1378                 MLX5_SET(cqc, cqc, cqe_comp_en, 1);
1379         }
1380
1381         mlx5e_build_common_cq_param(priv, param);
1382
1383         param->cq_period_mode = priv->params.rx_cq_period_mode;
1384 }
1385
1386 static void mlx5e_build_tx_cq_param(struct mlx5e_priv *priv,
1387                                     struct mlx5e_cq_param *param)
1388 {
1389         void *cqc = param->cqc;
1390
1391         MLX5_SET(cqc, cqc, log_cq_size, priv->params.log_sq_size);
1392
1393         mlx5e_build_common_cq_param(priv, param);
1394
1395         param->cq_period_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1396 }
1397
1398 static void mlx5e_build_ico_cq_param(struct mlx5e_priv *priv,
1399                                      struct mlx5e_cq_param *param,
1400                                      u8 log_wq_size)
1401 {
1402         void *cqc = param->cqc;
1403
1404         MLX5_SET(cqc, cqc, log_cq_size, log_wq_size);
1405
1406         mlx5e_build_common_cq_param(priv, param);
1407
1408         param->cq_period_mode = MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
1409 }
1410
1411 static void mlx5e_build_icosq_param(struct mlx5e_priv *priv,
1412                                     struct mlx5e_sq_param *param,
1413                                     u8 log_wq_size)
1414 {
1415         void *sqc = param->sqc;
1416         void *wq = MLX5_ADDR_OF(sqc, sqc, wq);
1417
1418         mlx5e_build_sq_param_common(priv, param);
1419
1420         MLX5_SET(wq, wq, log_wq_sz, log_wq_size);
1421         MLX5_SET(sqc, sqc, reg_umr, MLX5_CAP_ETH(priv->mdev, reg_umr_sq));
1422
1423         param->icosq = true;
1424 }
1425
1426 static void mlx5e_build_channel_param(struct mlx5e_priv *priv, struct mlx5e_channel_param *cparam)
1427 {
1428         u8 icosq_log_wq_sz = MLX5E_PARAMS_MINIMUM_LOG_SQ_SIZE;
1429
1430         mlx5e_build_rq_param(priv, &cparam->rq);
1431         mlx5e_build_sq_param(priv, &cparam->sq);
1432         mlx5e_build_icosq_param(priv, &cparam->icosq, icosq_log_wq_sz);
1433         mlx5e_build_rx_cq_param(priv, &cparam->rx_cq);
1434         mlx5e_build_tx_cq_param(priv, &cparam->tx_cq);
1435         mlx5e_build_ico_cq_param(priv, &cparam->icosq_cq, icosq_log_wq_sz);
1436 }
1437
1438 static int mlx5e_open_channels(struct mlx5e_priv *priv)
1439 {
1440         struct mlx5e_channel_param *cparam;
1441         int nch = priv->params.num_channels;
1442         int err = -ENOMEM;
1443         int i;
1444         int j;
1445
1446         priv->channel = kcalloc(nch, sizeof(struct mlx5e_channel *),
1447                                 GFP_KERNEL);
1448
1449         priv->txq_to_sq_map = kcalloc(nch * priv->params.num_tc,
1450                                       sizeof(struct mlx5e_sq *), GFP_KERNEL);
1451
1452         cparam = kzalloc(sizeof(struct mlx5e_channel_param), GFP_KERNEL);
1453
1454         if (!priv->channel || !priv->txq_to_sq_map || !cparam)
1455                 goto err_free_txq_to_sq_map;
1456
1457         mlx5e_build_channel_param(priv, cparam);
1458
1459         for (i = 0; i < nch; i++) {
1460                 err = mlx5e_open_channel(priv, i, cparam, &priv->channel[i]);
1461                 if (err)
1462                         goto err_close_channels;
1463         }
1464
1465         for (j = 0; j < nch; j++) {
1466                 err = mlx5e_wait_for_min_rx_wqes(&priv->channel[j]->rq);
1467                 if (err)
1468                         goto err_close_channels;
1469         }
1470
1471         /* FIXME: This is a W/A for tx timeout watch dog false alarm when
1472          * polling for inactive tx queues.
1473          */
1474         netif_tx_start_all_queues(priv->netdev);
1475
1476         kfree(cparam);
1477         return 0;
1478
1479 err_close_channels:
1480         for (i--; i >= 0; i--)
1481                 mlx5e_close_channel(priv->channel[i]);
1482
1483 err_free_txq_to_sq_map:
1484         kfree(priv->txq_to_sq_map);
1485         kfree(priv->channel);
1486         kfree(cparam);
1487
1488         return err;
1489 }
1490
1491 static void mlx5e_close_channels(struct mlx5e_priv *priv)
1492 {
1493         int i;
1494
1495         /* FIXME: This is a W/A only for tx timeout watch dog false alarm when
1496          * polling for inactive tx queues.
1497          */
1498         netif_tx_stop_all_queues(priv->netdev);
1499         netif_tx_disable(priv->netdev);
1500
1501         for (i = 0; i < priv->params.num_channels; i++)
1502                 mlx5e_close_channel(priv->channel[i]);
1503
1504         kfree(priv->txq_to_sq_map);
1505         kfree(priv->channel);
1506 }
1507
1508 static int mlx5e_rx_hash_fn(int hfunc)
1509 {
1510         return (hfunc == ETH_RSS_HASH_TOP) ?
1511                MLX5_RX_HASH_FN_TOEPLITZ :
1512                MLX5_RX_HASH_FN_INVERTED_XOR8;
1513 }
1514
1515 static int mlx5e_bits_invert(unsigned long a, int size)
1516 {
1517         int inv = 0;
1518         int i;
1519
1520         for (i = 0; i < size; i++)
1521                 inv |= (test_bit(size - i - 1, &a) ? 1 : 0) << i;
1522
1523         return inv;
1524 }
1525
1526 static void mlx5e_fill_indir_rqt_rqns(struct mlx5e_priv *priv, void *rqtc)
1527 {
1528         int i;
1529
1530         for (i = 0; i < MLX5E_INDIR_RQT_SIZE; i++) {
1531                 int ix = i;
1532                 u32 rqn;
1533
1534                 if (priv->params.rss_hfunc == ETH_RSS_HASH_XOR)
1535                         ix = mlx5e_bits_invert(i, MLX5E_LOG_INDIR_RQT_SIZE);
1536
1537                 ix = priv->params.indirection_rqt[ix];
1538                 rqn = test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1539                                 priv->channel[ix]->rq.rqn :
1540                                 priv->drop_rq.rqn;
1541                 MLX5_SET(rqtc, rqtc, rq_num[i], rqn);
1542         }
1543 }
1544
1545 static void mlx5e_fill_direct_rqt_rqn(struct mlx5e_priv *priv, void *rqtc,
1546                                       int ix)
1547 {
1548         u32 rqn = test_bit(MLX5E_STATE_OPENED, &priv->state) ?
1549                         priv->channel[ix]->rq.rqn :
1550                         priv->drop_rq.rqn;
1551
1552         MLX5_SET(rqtc, rqtc, rq_num[0], rqn);
1553 }
1554
1555 static int mlx5e_create_rqt(struct mlx5e_priv *priv, int sz,
1556                             int ix, struct mlx5e_rqt *rqt)
1557 {
1558         struct mlx5_core_dev *mdev = priv->mdev;
1559         void *rqtc;
1560         int inlen;
1561         int err;
1562         u32 *in;
1563
1564         inlen = MLX5_ST_SZ_BYTES(create_rqt_in) + sizeof(u32) * sz;
1565         in = mlx5_vzalloc(inlen);
1566         if (!in)
1567                 return -ENOMEM;
1568
1569         rqtc = MLX5_ADDR_OF(create_rqt_in, in, rqt_context);
1570
1571         MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1572         MLX5_SET(rqtc, rqtc, rqt_max_size, sz);
1573
1574         if (sz > 1) /* RSS */
1575                 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1576         else
1577                 mlx5e_fill_direct_rqt_rqn(priv, rqtc, ix);
1578
1579         err = mlx5_core_create_rqt(mdev, in, inlen, &rqt->rqtn);
1580         if (!err)
1581                 rqt->enabled = true;
1582
1583         kvfree(in);
1584         return err;
1585 }
1586
1587 void mlx5e_destroy_rqt(struct mlx5e_priv *priv, struct mlx5e_rqt *rqt)
1588 {
1589         rqt->enabled = false;
1590         mlx5_core_destroy_rqt(priv->mdev, rqt->rqtn);
1591 }
1592
1593 static int mlx5e_create_indirect_rqts(struct mlx5e_priv *priv)
1594 {
1595         struct mlx5e_rqt *rqt = &priv->indir_rqt;
1596
1597         return mlx5e_create_rqt(priv, MLX5E_INDIR_RQT_SIZE, 0, rqt);
1598 }
1599
1600 int mlx5e_create_direct_rqts(struct mlx5e_priv *priv)
1601 {
1602         struct mlx5e_rqt *rqt;
1603         int err;
1604         int ix;
1605
1606         for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
1607                 rqt = &priv->direct_tir[ix].rqt;
1608                 err = mlx5e_create_rqt(priv, 1 /*size */, ix, rqt);
1609                 if (err)
1610                         goto err_destroy_rqts;
1611         }
1612
1613         return 0;
1614
1615 err_destroy_rqts:
1616         for (ix--; ix >= 0; ix--)
1617                 mlx5e_destroy_rqt(priv, &priv->direct_tir[ix].rqt);
1618
1619         return err;
1620 }
1621
1622 int mlx5e_redirect_rqt(struct mlx5e_priv *priv, u32 rqtn, int sz, int ix)
1623 {
1624         struct mlx5_core_dev *mdev = priv->mdev;
1625         void *rqtc;
1626         int inlen;
1627         u32 *in;
1628         int err;
1629
1630         inlen = MLX5_ST_SZ_BYTES(modify_rqt_in) + sizeof(u32) * sz;
1631         in = mlx5_vzalloc(inlen);
1632         if (!in)
1633                 return -ENOMEM;
1634
1635         rqtc = MLX5_ADDR_OF(modify_rqt_in, in, ctx);
1636
1637         MLX5_SET(rqtc, rqtc, rqt_actual_size, sz);
1638         if (sz > 1) /* RSS */
1639                 mlx5e_fill_indir_rqt_rqns(priv, rqtc);
1640         else
1641                 mlx5e_fill_direct_rqt_rqn(priv, rqtc, ix);
1642
1643         MLX5_SET(modify_rqt_in, in, bitmask.rqn_list, 1);
1644
1645         err = mlx5_core_modify_rqt(mdev, rqtn, in, inlen);
1646
1647         kvfree(in);
1648
1649         return err;
1650 }
1651
1652 static void mlx5e_redirect_rqts(struct mlx5e_priv *priv)
1653 {
1654         u32 rqtn;
1655         int ix;
1656
1657         if (priv->indir_rqt.enabled) {
1658                 rqtn = priv->indir_rqt.rqtn;
1659                 mlx5e_redirect_rqt(priv, rqtn, MLX5E_INDIR_RQT_SIZE, 0);
1660         }
1661
1662         for (ix = 0; ix < priv->params.num_channels; ix++) {
1663                 if (!priv->direct_tir[ix].rqt.enabled)
1664                         continue;
1665                 rqtn = priv->direct_tir[ix].rqt.rqtn;
1666                 mlx5e_redirect_rqt(priv, rqtn, 1, ix);
1667         }
1668 }
1669
1670 static void mlx5e_build_tir_ctx_lro(void *tirc, struct mlx5e_priv *priv)
1671 {
1672         if (!priv->params.lro_en)
1673                 return;
1674
1675 #define ROUGH_MAX_L2_L3_HDR_SZ 256
1676
1677         MLX5_SET(tirc, tirc, lro_enable_mask,
1678                  MLX5_TIRC_LRO_ENABLE_MASK_IPV4_LRO |
1679                  MLX5_TIRC_LRO_ENABLE_MASK_IPV6_LRO);
1680         MLX5_SET(tirc, tirc, lro_max_ip_payload_size,
1681                  (priv->params.lro_wqe_sz -
1682                   ROUGH_MAX_L2_L3_HDR_SZ) >> 8);
1683         MLX5_SET(tirc, tirc, lro_timeout_period_usecs,
1684                  MLX5_CAP_ETH(priv->mdev,
1685                               lro_timer_supported_periods[2]));
1686 }
1687
1688 void mlx5e_build_tir_ctx_hash(void *tirc, struct mlx5e_priv *priv)
1689 {
1690         MLX5_SET(tirc, tirc, rx_hash_fn,
1691                  mlx5e_rx_hash_fn(priv->params.rss_hfunc));
1692         if (priv->params.rss_hfunc == ETH_RSS_HASH_TOP) {
1693                 void *rss_key = MLX5_ADDR_OF(tirc, tirc,
1694                                              rx_hash_toeplitz_key);
1695                 size_t len = MLX5_FLD_SZ_BYTES(tirc,
1696                                                rx_hash_toeplitz_key);
1697
1698                 MLX5_SET(tirc, tirc, rx_hash_symmetric, 1);
1699                 memcpy(rss_key, priv->params.toeplitz_hash_key, len);
1700         }
1701 }
1702
1703 static int mlx5e_modify_tirs_lro(struct mlx5e_priv *priv)
1704 {
1705         struct mlx5_core_dev *mdev = priv->mdev;
1706
1707         void *in;
1708         void *tirc;
1709         int inlen;
1710         int err;
1711         int tt;
1712         int ix;
1713
1714         inlen = MLX5_ST_SZ_BYTES(modify_tir_in);
1715         in = mlx5_vzalloc(inlen);
1716         if (!in)
1717                 return -ENOMEM;
1718
1719         MLX5_SET(modify_tir_in, in, bitmask.lro, 1);
1720         tirc = MLX5_ADDR_OF(modify_tir_in, in, ctx);
1721
1722         mlx5e_build_tir_ctx_lro(tirc, priv);
1723
1724         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
1725                 err = mlx5_core_modify_tir(mdev, priv->indir_tir[tt].tirn, in,
1726                                            inlen);
1727                 if (err)
1728                         goto free_in;
1729         }
1730
1731         for (ix = 0; ix < priv->profile->max_nch(priv->mdev); ix++) {
1732                 err = mlx5_core_modify_tir(mdev, priv->direct_tir[ix].tirn,
1733                                            in, inlen);
1734                 if (err)
1735                         goto free_in;
1736         }
1737
1738 free_in:
1739         kvfree(in);
1740
1741         return err;
1742 }
1743
1744 static int mlx5e_set_mtu(struct mlx5e_priv *priv, u16 mtu)
1745 {
1746         struct mlx5_core_dev *mdev = priv->mdev;
1747         u16 hw_mtu = MLX5E_SW2HW_MTU(mtu);
1748         int err;
1749
1750         err = mlx5_set_port_mtu(mdev, hw_mtu, 1);
1751         if (err)
1752                 return err;
1753
1754         /* Update vport context MTU */
1755         mlx5_modify_nic_vport_mtu(mdev, hw_mtu);
1756         return 0;
1757 }
1758
1759 static void mlx5e_query_mtu(struct mlx5e_priv *priv, u16 *mtu)
1760 {
1761         struct mlx5_core_dev *mdev = priv->mdev;
1762         u16 hw_mtu = 0;
1763         int err;
1764
1765         err = mlx5_query_nic_vport_mtu(mdev, &hw_mtu);
1766         if (err || !hw_mtu) /* fallback to port oper mtu */
1767                 mlx5_query_port_oper_mtu(mdev, &hw_mtu, 1);
1768
1769         *mtu = MLX5E_HW2SW_MTU(hw_mtu);
1770 }
1771
1772 static int mlx5e_set_dev_port_mtu(struct net_device *netdev)
1773 {
1774         struct mlx5e_priv *priv = netdev_priv(netdev);
1775         u16 mtu;
1776         int err;
1777
1778         err = mlx5e_set_mtu(priv, netdev->mtu);
1779         if (err)
1780                 return err;
1781
1782         mlx5e_query_mtu(priv, &mtu);
1783         if (mtu != netdev->mtu)
1784                 netdev_warn(netdev, "%s: VPort MTU %d is different than netdev mtu %d\n",
1785                             __func__, mtu, netdev->mtu);
1786
1787         netdev->mtu = mtu;
1788         return 0;
1789 }
1790
1791 static void mlx5e_netdev_set_tcs(struct net_device *netdev)
1792 {
1793         struct mlx5e_priv *priv = netdev_priv(netdev);
1794         int nch = priv->params.num_channels;
1795         int ntc = priv->params.num_tc;
1796         int tc;
1797
1798         netdev_reset_tc(netdev);
1799
1800         if (ntc == 1)
1801                 return;
1802
1803         netdev_set_num_tc(netdev, ntc);
1804
1805         /* Map netdev TCs to offset 0
1806          * We have our own UP to TXQ mapping for QoS
1807          */
1808         for (tc = 0; tc < ntc; tc++)
1809                 netdev_set_tc_queue(netdev, tc, nch, 0);
1810 }
1811
1812 int mlx5e_open_locked(struct net_device *netdev)
1813 {
1814         struct mlx5e_priv *priv = netdev_priv(netdev);
1815         struct mlx5_core_dev *mdev = priv->mdev;
1816         int num_txqs;
1817         int err;
1818
1819         set_bit(MLX5E_STATE_OPENED, &priv->state);
1820
1821         mlx5e_netdev_set_tcs(netdev);
1822
1823         num_txqs = priv->params.num_channels * priv->params.num_tc;
1824         netif_set_real_num_tx_queues(netdev, num_txqs);
1825         netif_set_real_num_rx_queues(netdev, priv->params.num_channels);
1826
1827         err = mlx5e_set_dev_port_mtu(netdev);
1828         if (err)
1829                 goto err_clear_state_opened_flag;
1830
1831         err = mlx5e_open_channels(priv);
1832         if (err) {
1833                 netdev_err(netdev, "%s: mlx5e_open_channels failed, %d\n",
1834                            __func__, err);
1835                 goto err_clear_state_opened_flag;
1836         }
1837
1838         err = mlx5e_refresh_tirs_self_loopback_enable(priv->mdev);
1839         if (err) {
1840                 netdev_err(netdev, "%s: mlx5e_refresh_tirs_self_loopback_enable failed, %d\n",
1841                            __func__, err);
1842                 goto err_close_channels;
1843         }
1844
1845         mlx5e_redirect_rqts(priv);
1846         mlx5e_update_carrier(priv);
1847         mlx5e_timestamp_init(priv);
1848 #ifdef CONFIG_RFS_ACCEL
1849         priv->netdev->rx_cpu_rmap = priv->mdev->rmap;
1850 #endif
1851         if (priv->profile->update_stats)
1852                 queue_delayed_work(priv->wq, &priv->update_stats_work, 0);
1853
1854         if (MLX5_CAP_GEN(mdev, vport_group_manager)) {
1855                 err = mlx5e_add_sqs_fwd_rules(priv);
1856                 if (err)
1857                         goto err_close_channels;
1858         }
1859         return 0;
1860
1861 err_close_channels:
1862         mlx5e_close_channels(priv);
1863 err_clear_state_opened_flag:
1864         clear_bit(MLX5E_STATE_OPENED, &priv->state);
1865         return err;
1866 }
1867
1868 int mlx5e_open(struct net_device *netdev)
1869 {
1870         struct mlx5e_priv *priv = netdev_priv(netdev);
1871         int err;
1872
1873         mutex_lock(&priv->state_lock);
1874         err = mlx5e_open_locked(netdev);
1875         mutex_unlock(&priv->state_lock);
1876
1877         return err;
1878 }
1879
1880 int mlx5e_close_locked(struct net_device *netdev)
1881 {
1882         struct mlx5e_priv *priv = netdev_priv(netdev);
1883         struct mlx5_core_dev *mdev = priv->mdev;
1884
1885         /* May already be CLOSED in case a previous configuration operation
1886          * (e.g RX/TX queue size change) that involves close&open failed.
1887          */
1888         if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
1889                 return 0;
1890
1891         clear_bit(MLX5E_STATE_OPENED, &priv->state);
1892
1893         if (MLX5_CAP_GEN(mdev, vport_group_manager))
1894                 mlx5e_remove_sqs_fwd_rules(priv);
1895
1896         mlx5e_timestamp_cleanup(priv);
1897         netif_carrier_off(priv->netdev);
1898         mlx5e_redirect_rqts(priv);
1899         mlx5e_close_channels(priv);
1900
1901         return 0;
1902 }
1903
1904 int mlx5e_close(struct net_device *netdev)
1905 {
1906         struct mlx5e_priv *priv = netdev_priv(netdev);
1907         int err;
1908
1909         mutex_lock(&priv->state_lock);
1910         err = mlx5e_close_locked(netdev);
1911         mutex_unlock(&priv->state_lock);
1912
1913         return err;
1914 }
1915
1916 static int mlx5e_create_drop_rq(struct mlx5e_priv *priv,
1917                                 struct mlx5e_rq *rq,
1918                                 struct mlx5e_rq_param *param)
1919 {
1920         struct mlx5_core_dev *mdev = priv->mdev;
1921         void *rqc = param->rqc;
1922         void *rqc_wq = MLX5_ADDR_OF(rqc, rqc, wq);
1923         int err;
1924
1925         param->wq.db_numa_node = param->wq.buf_numa_node;
1926
1927         err = mlx5_wq_ll_create(mdev, &param->wq, rqc_wq, &rq->wq,
1928                                 &rq->wq_ctrl);
1929         if (err)
1930                 return err;
1931
1932         rq->priv = priv;
1933
1934         return 0;
1935 }
1936
1937 static int mlx5e_create_drop_cq(struct mlx5e_priv *priv,
1938                                 struct mlx5e_cq *cq,
1939                                 struct mlx5e_cq_param *param)
1940 {
1941         struct mlx5_core_dev *mdev = priv->mdev;
1942         struct mlx5_core_cq *mcq = &cq->mcq;
1943         int eqn_not_used;
1944         unsigned int irqn;
1945         int err;
1946
1947         err = mlx5_cqwq_create(mdev, &param->wq, param->cqc, &cq->wq,
1948                                &cq->wq_ctrl);
1949         if (err)
1950                 return err;
1951
1952         mlx5_vector2eqn(mdev, param->eq_ix, &eqn_not_used, &irqn);
1953
1954         mcq->cqe_sz     = 64;
1955         mcq->set_ci_db  = cq->wq_ctrl.db.db;
1956         mcq->arm_db     = cq->wq_ctrl.db.db + 1;
1957         *mcq->set_ci_db = 0;
1958         *mcq->arm_db    = 0;
1959         mcq->vector     = param->eq_ix;
1960         mcq->comp       = mlx5e_completion_event;
1961         mcq->event      = mlx5e_cq_error_event;
1962         mcq->irqn       = irqn;
1963         mcq->uar        = &mdev->mlx5e_res.cq_uar;
1964
1965         cq->priv = priv;
1966
1967         return 0;
1968 }
1969
1970 static int mlx5e_open_drop_rq(struct mlx5e_priv *priv)
1971 {
1972         struct mlx5e_cq_param cq_param;
1973         struct mlx5e_rq_param rq_param;
1974         struct mlx5e_rq *rq = &priv->drop_rq;
1975         struct mlx5e_cq *cq = &priv->drop_rq.cq;
1976         int err;
1977
1978         memset(&cq_param, 0, sizeof(cq_param));
1979         memset(&rq_param, 0, sizeof(rq_param));
1980         mlx5e_build_drop_rq_param(&rq_param);
1981
1982         err = mlx5e_create_drop_cq(priv, cq, &cq_param);
1983         if (err)
1984                 return err;
1985
1986         err = mlx5e_enable_cq(cq, &cq_param);
1987         if (err)
1988                 goto err_destroy_cq;
1989
1990         err = mlx5e_create_drop_rq(priv, rq, &rq_param);
1991         if (err)
1992                 goto err_disable_cq;
1993
1994         err = mlx5e_enable_rq(rq, &rq_param);
1995         if (err)
1996                 goto err_destroy_rq;
1997
1998         return 0;
1999
2000 err_destroy_rq:
2001         mlx5e_destroy_rq(&priv->drop_rq);
2002
2003 err_disable_cq:
2004         mlx5e_disable_cq(&priv->drop_rq.cq);
2005
2006 err_destroy_cq:
2007         mlx5e_destroy_cq(&priv->drop_rq.cq);
2008
2009         return err;
2010 }
2011
2012 static void mlx5e_close_drop_rq(struct mlx5e_priv *priv)
2013 {
2014         mlx5e_disable_rq(&priv->drop_rq);
2015         mlx5e_destroy_rq(&priv->drop_rq);
2016         mlx5e_disable_cq(&priv->drop_rq.cq);
2017         mlx5e_destroy_cq(&priv->drop_rq.cq);
2018 }
2019
2020 static int mlx5e_create_tis(struct mlx5e_priv *priv, int tc)
2021 {
2022         struct mlx5_core_dev *mdev = priv->mdev;
2023         u32 in[MLX5_ST_SZ_DW(create_tis_in)] = {0};
2024         void *tisc = MLX5_ADDR_OF(create_tis_in, in, ctx);
2025
2026         MLX5_SET(tisc, tisc, prio, tc << 1);
2027         MLX5_SET(tisc, tisc, transport_domain, mdev->mlx5e_res.td.tdn);
2028
2029         if (mlx5_lag_is_lacp_owner(mdev))
2030                 MLX5_SET(tisc, tisc, strict_lag_tx_port_affinity, 1);
2031
2032         return mlx5_core_create_tis(mdev, in, sizeof(in), &priv->tisn[tc]);
2033 }
2034
2035 static void mlx5e_destroy_tis(struct mlx5e_priv *priv, int tc)
2036 {
2037         mlx5_core_destroy_tis(priv->mdev, priv->tisn[tc]);
2038 }
2039
2040 int mlx5e_create_tises(struct mlx5e_priv *priv)
2041 {
2042         int err;
2043         int tc;
2044
2045         for (tc = 0; tc < priv->profile->max_tc; tc++) {
2046                 err = mlx5e_create_tis(priv, tc);
2047                 if (err)
2048                         goto err_close_tises;
2049         }
2050
2051         return 0;
2052
2053 err_close_tises:
2054         for (tc--; tc >= 0; tc--)
2055                 mlx5e_destroy_tis(priv, tc);
2056
2057         return err;
2058 }
2059
2060 void mlx5e_cleanup_nic_tx(struct mlx5e_priv *priv)
2061 {
2062         int tc;
2063
2064         for (tc = 0; tc < priv->profile->max_tc; tc++)
2065                 mlx5e_destroy_tis(priv, tc);
2066 }
2067
2068 static void mlx5e_build_indir_tir_ctx(struct mlx5e_priv *priv, u32 *tirc,
2069                                       enum mlx5e_traffic_types tt)
2070 {
2071         void *hfso = MLX5_ADDR_OF(tirc, tirc, rx_hash_field_selector_outer);
2072
2073         MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2074
2075 #define MLX5_HASH_IP            (MLX5_HASH_FIELD_SEL_SRC_IP   |\
2076                                  MLX5_HASH_FIELD_SEL_DST_IP)
2077
2078 #define MLX5_HASH_IP_L4PORTS    (MLX5_HASH_FIELD_SEL_SRC_IP   |\
2079                                  MLX5_HASH_FIELD_SEL_DST_IP   |\
2080                                  MLX5_HASH_FIELD_SEL_L4_SPORT |\
2081                                  MLX5_HASH_FIELD_SEL_L4_DPORT)
2082
2083 #define MLX5_HASH_IP_IPSEC_SPI  (MLX5_HASH_FIELD_SEL_SRC_IP   |\
2084                                  MLX5_HASH_FIELD_SEL_DST_IP   |\
2085                                  MLX5_HASH_FIELD_SEL_IPSEC_SPI)
2086
2087         mlx5e_build_tir_ctx_lro(tirc, priv);
2088
2089         MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2090         MLX5_SET(tirc, tirc, indirect_table, priv->indir_rqt.rqtn);
2091         mlx5e_build_tir_ctx_hash(tirc, priv);
2092
2093         switch (tt) {
2094         case MLX5E_TT_IPV4_TCP:
2095                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2096                          MLX5_L3_PROT_TYPE_IPV4);
2097                 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2098                          MLX5_L4_PROT_TYPE_TCP);
2099                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2100                          MLX5_HASH_IP_L4PORTS);
2101                 break;
2102
2103         case MLX5E_TT_IPV6_TCP:
2104                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2105                          MLX5_L3_PROT_TYPE_IPV6);
2106                 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2107                          MLX5_L4_PROT_TYPE_TCP);
2108                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2109                          MLX5_HASH_IP_L4PORTS);
2110                 break;
2111
2112         case MLX5E_TT_IPV4_UDP:
2113                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2114                          MLX5_L3_PROT_TYPE_IPV4);
2115                 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2116                          MLX5_L4_PROT_TYPE_UDP);
2117                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2118                          MLX5_HASH_IP_L4PORTS);
2119                 break;
2120
2121         case MLX5E_TT_IPV6_UDP:
2122                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2123                          MLX5_L3_PROT_TYPE_IPV6);
2124                 MLX5_SET(rx_hash_field_select, hfso, l4_prot_type,
2125                          MLX5_L4_PROT_TYPE_UDP);
2126                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2127                          MLX5_HASH_IP_L4PORTS);
2128                 break;
2129
2130         case MLX5E_TT_IPV4_IPSEC_AH:
2131                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2132                          MLX5_L3_PROT_TYPE_IPV4);
2133                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2134                          MLX5_HASH_IP_IPSEC_SPI);
2135                 break;
2136
2137         case MLX5E_TT_IPV6_IPSEC_AH:
2138                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2139                          MLX5_L3_PROT_TYPE_IPV6);
2140                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2141                          MLX5_HASH_IP_IPSEC_SPI);
2142                 break;
2143
2144         case MLX5E_TT_IPV4_IPSEC_ESP:
2145                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2146                          MLX5_L3_PROT_TYPE_IPV4);
2147                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2148                          MLX5_HASH_IP_IPSEC_SPI);
2149                 break;
2150
2151         case MLX5E_TT_IPV6_IPSEC_ESP:
2152                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2153                          MLX5_L3_PROT_TYPE_IPV6);
2154                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2155                          MLX5_HASH_IP_IPSEC_SPI);
2156                 break;
2157
2158         case MLX5E_TT_IPV4:
2159                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2160                          MLX5_L3_PROT_TYPE_IPV4);
2161                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2162                          MLX5_HASH_IP);
2163                 break;
2164
2165         case MLX5E_TT_IPV6:
2166                 MLX5_SET(rx_hash_field_select, hfso, l3_prot_type,
2167                          MLX5_L3_PROT_TYPE_IPV6);
2168                 MLX5_SET(rx_hash_field_select, hfso, selected_fields,
2169                          MLX5_HASH_IP);
2170                 break;
2171         default:
2172                 WARN_ONCE(true,
2173                           "mlx5e_build_indir_tir_ctx: bad traffic type!\n");
2174         }
2175 }
2176
2177 static void mlx5e_build_direct_tir_ctx(struct mlx5e_priv *priv, u32 *tirc,
2178                                        u32 rqtn)
2179 {
2180         MLX5_SET(tirc, tirc, transport_domain, priv->mdev->mlx5e_res.td.tdn);
2181
2182         mlx5e_build_tir_ctx_lro(tirc, priv);
2183
2184         MLX5_SET(tirc, tirc, disp_type, MLX5_TIRC_DISP_TYPE_INDIRECT);
2185         MLX5_SET(tirc, tirc, indirect_table, rqtn);
2186         MLX5_SET(tirc, tirc, rx_hash_fn, MLX5_RX_HASH_FN_INVERTED_XOR8);
2187 }
2188
2189 static int mlx5e_create_indirect_tirs(struct mlx5e_priv *priv)
2190 {
2191         struct mlx5e_tir *tir;
2192         void *tirc;
2193         int inlen;
2194         int err;
2195         u32 *in;
2196         int tt;
2197
2198         inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2199         in = mlx5_vzalloc(inlen);
2200         if (!in)
2201                 return -ENOMEM;
2202
2203         for (tt = 0; tt < MLX5E_NUM_INDIR_TIRS; tt++) {
2204                 memset(in, 0, inlen);
2205                 tir = &priv->indir_tir[tt];
2206                 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2207                 mlx5e_build_indir_tir_ctx(priv, tirc, tt);
2208                 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
2209                 if (err)
2210                         goto err_destroy_tirs;
2211         }
2212
2213         kvfree(in);
2214
2215         return 0;
2216
2217 err_destroy_tirs:
2218         for (tt--; tt >= 0; tt--)
2219                 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[tt]);
2220
2221         kvfree(in);
2222
2223         return err;
2224 }
2225
2226 int mlx5e_create_direct_tirs(struct mlx5e_priv *priv)
2227 {
2228         int nch = priv->profile->max_nch(priv->mdev);
2229         struct mlx5e_tir *tir;
2230         void *tirc;
2231         int inlen;
2232         int err;
2233         u32 *in;
2234         int ix;
2235
2236         inlen = MLX5_ST_SZ_BYTES(create_tir_in);
2237         in = mlx5_vzalloc(inlen);
2238         if (!in)
2239                 return -ENOMEM;
2240
2241         for (ix = 0; ix < nch; ix++) {
2242                 memset(in, 0, inlen);
2243                 tir = &priv->direct_tir[ix];
2244                 tirc = MLX5_ADDR_OF(create_tir_in, in, ctx);
2245                 mlx5e_build_direct_tir_ctx(priv, tirc,
2246                                            priv->direct_tir[ix].rqt.rqtn);
2247                 err = mlx5e_create_tir(priv->mdev, tir, in, inlen);
2248                 if (err)
2249                         goto err_destroy_ch_tirs;
2250         }
2251
2252         kvfree(in);
2253
2254         return 0;
2255
2256 err_destroy_ch_tirs:
2257         for (ix--; ix >= 0; ix--)
2258                 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[ix]);
2259
2260         kvfree(in);
2261
2262         return err;
2263 }
2264
2265 static void mlx5e_destroy_indirect_tirs(struct mlx5e_priv *priv)
2266 {
2267         int i;
2268
2269         for (i = 0; i < MLX5E_NUM_INDIR_TIRS; i++)
2270                 mlx5e_destroy_tir(priv->mdev, &priv->indir_tir[i]);
2271 }
2272
2273 void mlx5e_destroy_direct_tirs(struct mlx5e_priv *priv)
2274 {
2275         int nch = priv->profile->max_nch(priv->mdev);
2276         int i;
2277
2278         for (i = 0; i < nch; i++)
2279                 mlx5e_destroy_tir(priv->mdev, &priv->direct_tir[i]);
2280 }
2281
2282 int mlx5e_modify_rqs_vsd(struct mlx5e_priv *priv, bool vsd)
2283 {
2284         int err = 0;
2285         int i;
2286
2287         if (!test_bit(MLX5E_STATE_OPENED, &priv->state))
2288                 return 0;
2289
2290         for (i = 0; i < priv->params.num_channels; i++) {
2291                 err = mlx5e_modify_rq_vsd(&priv->channel[i]->rq, vsd);
2292                 if (err)
2293                         return err;
2294         }
2295
2296         return 0;
2297 }
2298
2299 static int mlx5e_setup_tc(struct net_device *netdev, u8 tc)
2300 {
2301         struct mlx5e_priv *priv = netdev_priv(netdev);
2302         bool was_opened;
2303         int err = 0;
2304
2305         if (tc && tc != MLX5E_MAX_NUM_TC)
2306                 return -EINVAL;
2307
2308         mutex_lock(&priv->state_lock);
2309
2310         was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2311         if (was_opened)
2312                 mlx5e_close_locked(priv->netdev);
2313
2314         priv->params.num_tc = tc ? tc : 1;
2315
2316         if (was_opened)
2317                 err = mlx5e_open_locked(priv->netdev);
2318
2319         mutex_unlock(&priv->state_lock);
2320
2321         return err;
2322 }
2323
2324 static int mlx5e_ndo_setup_tc(struct net_device *dev, u32 handle,
2325                               __be16 proto, struct tc_to_netdev *tc)
2326 {
2327         struct mlx5e_priv *priv = netdev_priv(dev);
2328
2329         if (TC_H_MAJ(handle) != TC_H_MAJ(TC_H_INGRESS))
2330                 goto mqprio;
2331
2332         switch (tc->type) {
2333         case TC_SETUP_CLSFLOWER:
2334                 switch (tc->cls_flower->command) {
2335                 case TC_CLSFLOWER_REPLACE:
2336                         return mlx5e_configure_flower(priv, proto, tc->cls_flower);
2337                 case TC_CLSFLOWER_DESTROY:
2338                         return mlx5e_delete_flower(priv, tc->cls_flower);
2339                 case TC_CLSFLOWER_STATS:
2340                         return mlx5e_stats_flower(priv, tc->cls_flower);
2341                 }
2342         default:
2343                 return -EOPNOTSUPP;
2344         }
2345
2346 mqprio:
2347         if (tc->type != TC_SETUP_MQPRIO)
2348                 return -EINVAL;
2349
2350         return mlx5e_setup_tc(dev, tc->tc);
2351 }
2352
2353 struct rtnl_link_stats64 *
2354 mlx5e_get_stats(struct net_device *dev, struct rtnl_link_stats64 *stats)
2355 {
2356         struct mlx5e_priv *priv = netdev_priv(dev);
2357         struct mlx5e_sw_stats *sstats = &priv->stats.sw;
2358         struct mlx5e_vport_stats *vstats = &priv->stats.vport;
2359         struct mlx5e_pport_stats *pstats = &priv->stats.pport;
2360
2361         stats->rx_packets = sstats->rx_packets;
2362         stats->rx_bytes   = sstats->rx_bytes;
2363         stats->tx_packets = sstats->tx_packets;
2364         stats->tx_bytes   = sstats->tx_bytes;
2365
2366         stats->rx_dropped = priv->stats.qcnt.rx_out_of_buffer;
2367         stats->tx_dropped = sstats->tx_queue_dropped;
2368
2369         stats->rx_length_errors =
2370                 PPORT_802_3_GET(pstats, a_in_range_length_errors) +
2371                 PPORT_802_3_GET(pstats, a_out_of_range_length_field) +
2372                 PPORT_802_3_GET(pstats, a_frame_too_long_errors);
2373         stats->rx_crc_errors =
2374                 PPORT_802_3_GET(pstats, a_frame_check_sequence_errors);
2375         stats->rx_frame_errors = PPORT_802_3_GET(pstats, a_alignment_errors);
2376         stats->tx_aborted_errors = PPORT_2863_GET(pstats, if_out_discards);
2377         stats->tx_carrier_errors =
2378                 PPORT_802_3_GET(pstats, a_symbol_error_during_carrier);
2379         stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors +
2380                            stats->rx_frame_errors;
2381         stats->tx_errors = stats->tx_aborted_errors + stats->tx_carrier_errors;
2382
2383         /* vport multicast also counts packets that are dropped due to steering
2384          * or rx out of buffer
2385          */
2386         stats->multicast =
2387                 VPORT_COUNTER_GET(vstats, received_eth_multicast.packets);
2388
2389         return stats;
2390 }
2391
2392 static void mlx5e_set_rx_mode(struct net_device *dev)
2393 {
2394         struct mlx5e_priv *priv = netdev_priv(dev);
2395
2396         queue_work(priv->wq, &priv->set_rx_mode_work);
2397 }
2398
2399 static int mlx5e_set_mac(struct net_device *netdev, void *addr)
2400 {
2401         struct mlx5e_priv *priv = netdev_priv(netdev);
2402         struct sockaddr *saddr = addr;
2403
2404         if (!is_valid_ether_addr(saddr->sa_data))
2405                 return -EADDRNOTAVAIL;
2406
2407         netif_addr_lock_bh(netdev);
2408         ether_addr_copy(netdev->dev_addr, saddr->sa_data);
2409         netif_addr_unlock_bh(netdev);
2410
2411         queue_work(priv->wq, &priv->set_rx_mode_work);
2412
2413         return 0;
2414 }
2415
2416 #define MLX5E_SET_FEATURE(netdev, feature, enable)      \
2417         do {                                            \
2418                 if (enable)                             \
2419                         netdev->features |= feature;    \
2420                 else                                    \
2421                         netdev->features &= ~feature;   \
2422         } while (0)
2423
2424 typedef int (*mlx5e_feature_handler)(struct net_device *netdev, bool enable);
2425
2426 static int set_feature_lro(struct net_device *netdev, bool enable)
2427 {
2428         struct mlx5e_priv *priv = netdev_priv(netdev);
2429         bool was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2430         int err;
2431
2432         mutex_lock(&priv->state_lock);
2433
2434         if (was_opened && (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST))
2435                 mlx5e_close_locked(priv->netdev);
2436
2437         priv->params.lro_en = enable;
2438         err = mlx5e_modify_tirs_lro(priv);
2439         if (err) {
2440                 netdev_err(netdev, "lro modify failed, %d\n", err);
2441                 priv->params.lro_en = !enable;
2442         }
2443
2444         if (was_opened && (priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST))
2445                 mlx5e_open_locked(priv->netdev);
2446
2447         mutex_unlock(&priv->state_lock);
2448
2449         return err;
2450 }
2451
2452 static int set_feature_vlan_filter(struct net_device *netdev, bool enable)
2453 {
2454         struct mlx5e_priv *priv = netdev_priv(netdev);
2455
2456         if (enable)
2457                 mlx5e_enable_vlan_filter(priv);
2458         else
2459                 mlx5e_disable_vlan_filter(priv);
2460
2461         return 0;
2462 }
2463
2464 static int set_feature_tc_num_filters(struct net_device *netdev, bool enable)
2465 {
2466         struct mlx5e_priv *priv = netdev_priv(netdev);
2467
2468         if (!enable && mlx5e_tc_num_filters(priv)) {
2469                 netdev_err(netdev,
2470                            "Active offloaded tc filters, can't turn hw_tc_offload off\n");
2471                 return -EINVAL;
2472         }
2473
2474         return 0;
2475 }
2476
2477 static int set_feature_rx_all(struct net_device *netdev, bool enable)
2478 {
2479         struct mlx5e_priv *priv = netdev_priv(netdev);
2480         struct mlx5_core_dev *mdev = priv->mdev;
2481
2482         return mlx5_set_port_fcs(mdev, !enable);
2483 }
2484
2485 static int set_feature_rx_vlan(struct net_device *netdev, bool enable)
2486 {
2487         struct mlx5e_priv *priv = netdev_priv(netdev);
2488         int err;
2489
2490         mutex_lock(&priv->state_lock);
2491
2492         priv->params.vlan_strip_disable = !enable;
2493         err = mlx5e_modify_rqs_vsd(priv, !enable);
2494         if (err)
2495                 priv->params.vlan_strip_disable = enable;
2496
2497         mutex_unlock(&priv->state_lock);
2498
2499         return err;
2500 }
2501
2502 #ifdef CONFIG_RFS_ACCEL
2503 static int set_feature_arfs(struct net_device *netdev, bool enable)
2504 {
2505         struct mlx5e_priv *priv = netdev_priv(netdev);
2506         int err;
2507
2508         if (enable)
2509                 err = mlx5e_arfs_enable(priv);
2510         else
2511                 err = mlx5e_arfs_disable(priv);
2512
2513         return err;
2514 }
2515 #endif
2516
2517 static int mlx5e_handle_feature(struct net_device *netdev,
2518                                 netdev_features_t wanted_features,
2519                                 netdev_features_t feature,
2520                                 mlx5e_feature_handler feature_handler)
2521 {
2522         netdev_features_t changes = wanted_features ^ netdev->features;
2523         bool enable = !!(wanted_features & feature);
2524         int err;
2525
2526         if (!(changes & feature))
2527                 return 0;
2528
2529         err = feature_handler(netdev, enable);
2530         if (err) {
2531                 netdev_err(netdev, "%s feature 0x%llx failed err %d\n",
2532                            enable ? "Enable" : "Disable", feature, err);
2533                 return err;
2534         }
2535
2536         MLX5E_SET_FEATURE(netdev, feature, enable);
2537         return 0;
2538 }
2539
2540 static int mlx5e_set_features(struct net_device *netdev,
2541                               netdev_features_t features)
2542 {
2543         int err;
2544
2545         err  = mlx5e_handle_feature(netdev, features, NETIF_F_LRO,
2546                                     set_feature_lro);
2547         err |= mlx5e_handle_feature(netdev, features,
2548                                     NETIF_F_HW_VLAN_CTAG_FILTER,
2549                                     set_feature_vlan_filter);
2550         err |= mlx5e_handle_feature(netdev, features, NETIF_F_HW_TC,
2551                                     set_feature_tc_num_filters);
2552         err |= mlx5e_handle_feature(netdev, features, NETIF_F_RXALL,
2553                                     set_feature_rx_all);
2554         err |= mlx5e_handle_feature(netdev, features, NETIF_F_HW_VLAN_CTAG_RX,
2555                                     set_feature_rx_vlan);
2556 #ifdef CONFIG_RFS_ACCEL
2557         err |= mlx5e_handle_feature(netdev, features, NETIF_F_NTUPLE,
2558                                     set_feature_arfs);
2559 #endif
2560
2561         return err ? -EINVAL : 0;
2562 }
2563
2564 #define MXL5_HW_MIN_MTU 64
2565 #define MXL5E_MIN_MTU (MXL5_HW_MIN_MTU + ETH_FCS_LEN)
2566
2567 static int mlx5e_change_mtu(struct net_device *netdev, int new_mtu)
2568 {
2569         struct mlx5e_priv *priv = netdev_priv(netdev);
2570         struct mlx5_core_dev *mdev = priv->mdev;
2571         bool was_opened;
2572         u16 max_mtu;
2573         u16 min_mtu;
2574         int err = 0;
2575
2576         mlx5_query_port_max_mtu(mdev, &max_mtu, 1);
2577
2578         max_mtu = MLX5E_HW2SW_MTU(max_mtu);
2579         min_mtu = MLX5E_HW2SW_MTU(MXL5E_MIN_MTU);
2580
2581         if (new_mtu > max_mtu || new_mtu < min_mtu) {
2582                 netdev_err(netdev,
2583                            "%s: Bad MTU (%d), valid range is: [%d..%d]\n",
2584                            __func__, new_mtu, min_mtu, max_mtu);
2585                 return -EINVAL;
2586         }
2587
2588         mutex_lock(&priv->state_lock);
2589
2590         was_opened = test_bit(MLX5E_STATE_OPENED, &priv->state);
2591         if (was_opened)
2592                 mlx5e_close_locked(netdev);
2593
2594         netdev->mtu = new_mtu;
2595
2596         if (was_opened)
2597                 err = mlx5e_open_locked(netdev);
2598
2599         mutex_unlock(&priv->state_lock);
2600
2601         return err;
2602 }
2603
2604 static int mlx5e_ioctl(struct net_device *dev, struct ifreq *ifr, int cmd)
2605 {
2606         switch (cmd) {
2607         case SIOCSHWTSTAMP:
2608                 return mlx5e_hwstamp_set(dev, ifr);
2609         case SIOCGHWTSTAMP:
2610                 return mlx5e_hwstamp_get(dev, ifr);
2611         default:
2612                 return -EOPNOTSUPP;
2613         }
2614 }
2615
2616 static int mlx5e_set_vf_mac(struct net_device *dev, int vf, u8 *mac)
2617 {
2618         struct mlx5e_priv *priv = netdev_priv(dev);
2619         struct mlx5_core_dev *mdev = priv->mdev;
2620
2621         return mlx5_eswitch_set_vport_mac(mdev->priv.eswitch, vf + 1, mac);
2622 }
2623
2624 static int mlx5e_set_vf_vlan(struct net_device *dev, int vf, u16 vlan, u8 qos)
2625 {
2626         struct mlx5e_priv *priv = netdev_priv(dev);
2627         struct mlx5_core_dev *mdev = priv->mdev;
2628
2629         return mlx5_eswitch_set_vport_vlan(mdev->priv.eswitch, vf + 1,
2630                                            vlan, qos);
2631 }
2632
2633 static int mlx5e_set_vf_spoofchk(struct net_device *dev, int vf, bool setting)
2634 {
2635         struct mlx5e_priv *priv = netdev_priv(dev);
2636         struct mlx5_core_dev *mdev = priv->mdev;
2637
2638         return mlx5_eswitch_set_vport_spoofchk(mdev->priv.eswitch, vf + 1, setting);
2639 }
2640
2641 static int mlx5e_set_vf_trust(struct net_device *dev, int vf, bool setting)
2642 {
2643         struct mlx5e_priv *priv = netdev_priv(dev);
2644         struct mlx5_core_dev *mdev = priv->mdev;
2645
2646         return mlx5_eswitch_set_vport_trust(mdev->priv.eswitch, vf + 1, setting);
2647 }
2648 static int mlx5_vport_link2ifla(u8 esw_link)
2649 {
2650         switch (esw_link) {
2651         case MLX5_ESW_VPORT_ADMIN_STATE_DOWN:
2652                 return IFLA_VF_LINK_STATE_DISABLE;
2653         case MLX5_ESW_VPORT_ADMIN_STATE_UP:
2654                 return IFLA_VF_LINK_STATE_ENABLE;
2655         }
2656         return IFLA_VF_LINK_STATE_AUTO;
2657 }
2658
2659 static int mlx5_ifla_link2vport(u8 ifla_link)
2660 {
2661         switch (ifla_link) {
2662         case IFLA_VF_LINK_STATE_DISABLE:
2663                 return MLX5_ESW_VPORT_ADMIN_STATE_DOWN;
2664         case IFLA_VF_LINK_STATE_ENABLE:
2665                 return MLX5_ESW_VPORT_ADMIN_STATE_UP;
2666         }
2667         return MLX5_ESW_VPORT_ADMIN_STATE_AUTO;
2668 }
2669
2670 static int mlx5e_set_vf_link_state(struct net_device *dev, int vf,
2671                                    int link_state)
2672 {
2673         struct mlx5e_priv *priv = netdev_priv(dev);
2674         struct mlx5_core_dev *mdev = priv->mdev;
2675
2676         return mlx5_eswitch_set_vport_state(mdev->priv.eswitch, vf + 1,
2677                                             mlx5_ifla_link2vport(link_state));
2678 }
2679
2680 static int mlx5e_get_vf_config(struct net_device *dev,
2681                                int vf, struct ifla_vf_info *ivi)
2682 {
2683         struct mlx5e_priv *priv = netdev_priv(dev);
2684         struct mlx5_core_dev *mdev = priv->mdev;
2685         int err;
2686
2687         err = mlx5_eswitch_get_vport_config(mdev->priv.eswitch, vf + 1, ivi);
2688         if (err)
2689                 return err;
2690         ivi->linkstate = mlx5_vport_link2ifla(ivi->linkstate);
2691         return 0;
2692 }
2693
2694 static int mlx5e_get_vf_stats(struct net_device *dev,
2695                               int vf, struct ifla_vf_stats *vf_stats)
2696 {
2697         struct mlx5e_priv *priv = netdev_priv(dev);
2698         struct mlx5_core_dev *mdev = priv->mdev;
2699
2700         return mlx5_eswitch_get_vport_stats(mdev->priv.eswitch, vf + 1,
2701                                             vf_stats);
2702 }
2703
2704 static void mlx5e_add_vxlan_port(struct net_device *netdev,
2705                                  struct udp_tunnel_info *ti)
2706 {
2707         struct mlx5e_priv *priv = netdev_priv(netdev);
2708
2709         if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2710                 return;
2711
2712         if (!mlx5e_vxlan_allowed(priv->mdev))
2713                 return;
2714
2715         mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 1);
2716 }
2717
2718 static void mlx5e_del_vxlan_port(struct net_device *netdev,
2719                                  struct udp_tunnel_info *ti)
2720 {
2721         struct mlx5e_priv *priv = netdev_priv(netdev);
2722
2723         if (ti->type != UDP_TUNNEL_TYPE_VXLAN)
2724                 return;
2725
2726         if (!mlx5e_vxlan_allowed(priv->mdev))
2727                 return;
2728
2729         mlx5e_vxlan_queue_work(priv, ti->sa_family, be16_to_cpu(ti->port), 0);
2730 }
2731
2732 static netdev_features_t mlx5e_vxlan_features_check(struct mlx5e_priv *priv,
2733                                                     struct sk_buff *skb,
2734                                                     netdev_features_t features)
2735 {
2736         struct udphdr *udph;
2737         u16 proto;
2738         u16 port = 0;
2739
2740         switch (vlan_get_protocol(skb)) {
2741         case htons(ETH_P_IP):
2742                 proto = ip_hdr(skb)->protocol;
2743                 break;
2744         case htons(ETH_P_IPV6):
2745                 proto = ipv6_hdr(skb)->nexthdr;
2746                 break;
2747         default:
2748                 goto out;
2749         }
2750
2751         if (proto == IPPROTO_UDP) {
2752                 udph = udp_hdr(skb);
2753                 port = be16_to_cpu(udph->dest);
2754         }
2755
2756         /* Verify if UDP port is being offloaded by HW */
2757         if (port && mlx5e_vxlan_lookup_port(priv, port))
2758                 return features;
2759
2760 out:
2761         /* Disable CSUM and GSO if the udp dport is not offloaded by HW */
2762         return features & ~(NETIF_F_CSUM_MASK | NETIF_F_GSO_MASK);
2763 }
2764
2765 static netdev_features_t mlx5e_features_check(struct sk_buff *skb,
2766                                               struct net_device *netdev,
2767                                               netdev_features_t features)
2768 {
2769         struct mlx5e_priv *priv = netdev_priv(netdev);
2770
2771         features = vlan_features_check(skb, features);
2772         features = vxlan_features_check(skb, features);
2773
2774         /* Validate if the tunneled packet is being offloaded by HW */
2775         if (skb->encapsulation &&
2776             (features & NETIF_F_CSUM_MASK || features & NETIF_F_GSO_MASK))
2777                 return mlx5e_vxlan_features_check(priv, skb, features);
2778
2779         return features;
2780 }
2781
2782 static void mlx5e_tx_timeout(struct net_device *dev)
2783 {
2784         struct mlx5e_priv *priv = netdev_priv(dev);
2785         bool sched_work = false;
2786         int i;
2787
2788         netdev_err(dev, "TX timeout detected\n");
2789
2790         for (i = 0; i < priv->params.num_channels * priv->params.num_tc; i++) {
2791                 struct mlx5e_sq *sq = priv->txq_to_sq_map[i];
2792
2793                 if (!netif_xmit_stopped(netdev_get_tx_queue(dev, i)))
2794                         continue;
2795                 sched_work = true;
2796                 set_bit(MLX5E_SQ_STATE_TX_TIMEOUT, &sq->state);
2797                 netdev_err(dev, "TX timeout on queue: %d, SQ: 0x%x, CQ: 0x%x, SQ Cons: 0x%x SQ Prod: 0x%x\n",
2798                            i, sq->sqn, sq->cq.mcq.cqn, sq->cc, sq->pc);
2799         }
2800
2801         if (sched_work && test_bit(MLX5E_STATE_OPENED, &priv->state))
2802                 schedule_work(&priv->tx_timeout_work);
2803 }
2804
2805 static const struct net_device_ops mlx5e_netdev_ops_basic = {
2806         .ndo_open                = mlx5e_open,
2807         .ndo_stop                = mlx5e_close,
2808         .ndo_start_xmit          = mlx5e_xmit,
2809         .ndo_setup_tc            = mlx5e_ndo_setup_tc,
2810         .ndo_select_queue        = mlx5e_select_queue,
2811         .ndo_get_stats64         = mlx5e_get_stats,
2812         .ndo_set_rx_mode         = mlx5e_set_rx_mode,
2813         .ndo_set_mac_address     = mlx5e_set_mac,
2814         .ndo_vlan_rx_add_vid     = mlx5e_vlan_rx_add_vid,
2815         .ndo_vlan_rx_kill_vid    = mlx5e_vlan_rx_kill_vid,
2816         .ndo_set_features        = mlx5e_set_features,
2817         .ndo_change_mtu          = mlx5e_change_mtu,
2818         .ndo_do_ioctl            = mlx5e_ioctl,
2819         .ndo_set_tx_maxrate      = mlx5e_set_tx_maxrate,
2820 #ifdef CONFIG_RFS_ACCEL
2821         .ndo_rx_flow_steer       = mlx5e_rx_flow_steer,
2822 #endif
2823         .ndo_tx_timeout          = mlx5e_tx_timeout,
2824 };
2825
2826 static const struct net_device_ops mlx5e_netdev_ops_sriov = {
2827         .ndo_open                = mlx5e_open,
2828         .ndo_stop                = mlx5e_close,
2829         .ndo_start_xmit          = mlx5e_xmit,
2830         .ndo_setup_tc            = mlx5e_ndo_setup_tc,
2831         .ndo_select_queue        = mlx5e_select_queue,
2832         .ndo_get_stats64         = mlx5e_get_stats,
2833         .ndo_set_rx_mode         = mlx5e_set_rx_mode,
2834         .ndo_set_mac_address     = mlx5e_set_mac,
2835         .ndo_vlan_rx_add_vid     = mlx5e_vlan_rx_add_vid,
2836         .ndo_vlan_rx_kill_vid    = mlx5e_vlan_rx_kill_vid,
2837         .ndo_set_features        = mlx5e_set_features,
2838         .ndo_change_mtu          = mlx5e_change_mtu,
2839         .ndo_do_ioctl            = mlx5e_ioctl,
2840         .ndo_udp_tunnel_add      = mlx5e_add_vxlan_port,
2841         .ndo_udp_tunnel_del      = mlx5e_del_vxlan_port,
2842         .ndo_set_tx_maxrate      = mlx5e_set_tx_maxrate,
2843         .ndo_features_check      = mlx5e_features_check,
2844 #ifdef CONFIG_RFS_ACCEL
2845         .ndo_rx_flow_steer       = mlx5e_rx_flow_steer,
2846 #endif
2847         .ndo_set_vf_mac          = mlx5e_set_vf_mac,
2848         .ndo_set_vf_vlan         = mlx5e_set_vf_vlan,
2849         .ndo_set_vf_spoofchk     = mlx5e_set_vf_spoofchk,
2850         .ndo_set_vf_trust        = mlx5e_set_vf_trust,
2851         .ndo_get_vf_config       = mlx5e_get_vf_config,
2852         .ndo_set_vf_link_state   = mlx5e_set_vf_link_state,
2853         .ndo_get_vf_stats        = mlx5e_get_vf_stats,
2854         .ndo_tx_timeout          = mlx5e_tx_timeout,
2855 };
2856
2857 static int mlx5e_check_required_hca_cap(struct mlx5_core_dev *mdev)
2858 {
2859         if (MLX5_CAP_GEN(mdev, port_type) != MLX5_CAP_PORT_TYPE_ETH)
2860                 return -ENOTSUPP;
2861         if (!MLX5_CAP_GEN(mdev, eth_net_offloads) ||
2862             !MLX5_CAP_GEN(mdev, nic_flow_table) ||
2863             !MLX5_CAP_ETH(mdev, csum_cap) ||
2864             !MLX5_CAP_ETH(mdev, max_lso_cap) ||
2865             !MLX5_CAP_ETH(mdev, vlan_cap) ||
2866             !MLX5_CAP_ETH(mdev, rss_ind_tbl_cap) ||
2867             MLX5_CAP_FLOWTABLE(mdev,
2868                                flow_table_properties_nic_receive.max_ft_level)
2869                                < 3) {
2870                 mlx5_core_warn(mdev,
2871                                "Not creating net device, some required device capabilities are missing\n");
2872                 return -ENOTSUPP;
2873         }
2874         if (!MLX5_CAP_ETH(mdev, self_lb_en_modifiable))
2875                 mlx5_core_warn(mdev, "Self loop back prevention is not supported\n");
2876         if (!MLX5_CAP_GEN(mdev, cq_moderation))
2877                 mlx5_core_warn(mdev, "CQ modiration is not supported\n");
2878
2879         return 0;
2880 }
2881
2882 u16 mlx5e_get_max_inline_cap(struct mlx5_core_dev *mdev)
2883 {
2884         int bf_buf_size = (1 << MLX5_CAP_GEN(mdev, log_bf_reg_size)) / 2;
2885
2886         return bf_buf_size -
2887                sizeof(struct mlx5e_tx_wqe) +
2888                2 /*sizeof(mlx5e_tx_wqe.inline_hdr_start)*/;
2889 }
2890
2891 #ifdef CONFIG_MLX5_CORE_EN_DCB
2892 static void mlx5e_ets_init(struct mlx5e_priv *priv)
2893 {
2894         int i;
2895
2896         priv->params.ets.ets_cap = mlx5_max_tc(priv->mdev) + 1;
2897         for (i = 0; i < priv->params.ets.ets_cap; i++) {
2898                 priv->params.ets.tc_tx_bw[i] = MLX5E_MAX_BW_ALLOC;
2899                 priv->params.ets.tc_tsa[i] = IEEE_8021QAZ_TSA_VENDOR;
2900                 priv->params.ets.prio_tc[i] = i;
2901         }
2902
2903         /* tclass[prio=0]=1, tclass[prio=1]=0, tclass[prio=i]=i (for i>1) */
2904         priv->params.ets.prio_tc[0] = 1;
2905         priv->params.ets.prio_tc[1] = 0;
2906 }
2907 #endif
2908
2909 void mlx5e_build_default_indir_rqt(struct mlx5_core_dev *mdev,
2910                                    u32 *indirection_rqt, int len,
2911                                    int num_channels)
2912 {
2913         int node = mdev->priv.numa_node;
2914         int node_num_of_cores;
2915         int i;
2916
2917         if (node == -1)
2918                 node = first_online_node;
2919
2920         node_num_of_cores = cpumask_weight(cpumask_of_node(node));
2921
2922         if (node_num_of_cores)
2923                 num_channels = min_t(int, num_channels, node_num_of_cores);
2924
2925         for (i = 0; i < len; i++)
2926                 indirection_rqt[i] = i % num_channels;
2927 }
2928
2929 static bool mlx5e_check_fragmented_striding_rq_cap(struct mlx5_core_dev *mdev)
2930 {
2931         return MLX5_CAP_GEN(mdev, striding_rq) &&
2932                 MLX5_CAP_GEN(mdev, umr_ptr_rlky) &&
2933                 MLX5_CAP_ETH(mdev, reg_umr_sq);
2934 }
2935
2936 static int mlx5e_get_pci_bw(struct mlx5_core_dev *mdev, u32 *pci_bw)
2937 {
2938         enum pcie_link_width width;
2939         enum pci_bus_speed speed;
2940         int err = 0;
2941
2942         err = pcie_get_minimum_link(mdev->pdev, &speed, &width);
2943         if (err)
2944                 return err;
2945
2946         if (speed == PCI_SPEED_UNKNOWN || width == PCIE_LNK_WIDTH_UNKNOWN)
2947                 return -EINVAL;
2948
2949         switch (speed) {
2950         case PCIE_SPEED_2_5GT:
2951                 *pci_bw = 2500 * width;
2952                 break;
2953         case PCIE_SPEED_5_0GT:
2954                 *pci_bw = 5000 * width;
2955                 break;
2956         case PCIE_SPEED_8_0GT:
2957                 *pci_bw = 8000 * width;
2958                 break;
2959         default:
2960                 return -EINVAL;
2961         }
2962
2963         return 0;
2964 }
2965
2966 static bool cqe_compress_heuristic(u32 link_speed, u32 pci_bw)
2967 {
2968         return (link_speed && pci_bw &&
2969                 (pci_bw < 40000) && (pci_bw < link_speed));
2970 }
2971
2972 void mlx5e_set_rx_cq_mode_params(struct mlx5e_params *params, u8 cq_period_mode)
2973 {
2974         params->rx_cq_period_mode = cq_period_mode;
2975
2976         params->rx_cq_moderation.pkts =
2977                 MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_PKTS;
2978         params->rx_cq_moderation.usec =
2979                         MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC;
2980
2981         if (cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE)
2982                 params->rx_cq_moderation.usec =
2983                         MLX5E_PARAMS_DEFAULT_RX_CQ_MODERATION_USEC_FROM_CQE;
2984 }
2985
2986 static void mlx5e_query_min_inline(struct mlx5_core_dev *mdev,
2987                                    u8 *min_inline_mode)
2988 {
2989         switch (MLX5_CAP_ETH(mdev, wqe_inline_mode)) {
2990         case MLX5E_INLINE_MODE_L2:
2991                 *min_inline_mode = MLX5_INLINE_MODE_L2;
2992                 break;
2993         case MLX5E_INLINE_MODE_VPORT_CONTEXT:
2994                 mlx5_query_nic_vport_min_inline(mdev,
2995                                                 min_inline_mode);
2996                 break;
2997         case MLX5_INLINE_MODE_NOT_REQUIRED:
2998                 *min_inline_mode = MLX5_INLINE_MODE_NONE;
2999                 break;
3000         }
3001 }
3002
3003 static void mlx5e_build_nic_netdev_priv(struct mlx5_core_dev *mdev,
3004                                         struct net_device *netdev,
3005                                         const struct mlx5e_profile *profile,
3006                                         void *ppriv)
3007 {
3008         struct mlx5e_priv *priv = netdev_priv(netdev);
3009         u32 link_speed = 0;
3010         u32 pci_bw = 0;
3011         u8 cq_period_mode = MLX5_CAP_GEN(mdev, cq_period_start_from_cqe) ?
3012                                          MLX5_CQ_PERIOD_MODE_START_FROM_CQE :
3013                                          MLX5_CQ_PERIOD_MODE_START_FROM_EQE;
3014
3015         priv->params.log_sq_size           =
3016                 MLX5E_PARAMS_DEFAULT_LOG_SQ_SIZE;
3017         priv->params.rq_wq_type = mlx5e_check_fragmented_striding_rq_cap(mdev) ?
3018                 MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ :
3019                 MLX5_WQ_TYPE_LINKED_LIST;
3020
3021         /* set CQE compression */
3022         priv->params.rx_cqe_compress_admin = false;
3023         if (MLX5_CAP_GEN(mdev, cqe_compression) &&
3024             MLX5_CAP_GEN(mdev, vport_group_manager)) {
3025                 mlx5e_get_max_linkspeed(mdev, &link_speed);
3026                 mlx5e_get_pci_bw(mdev, &pci_bw);
3027                 mlx5_core_dbg(mdev, "Max link speed = %d, PCI BW = %d\n",
3028                               link_speed, pci_bw);
3029                 priv->params.rx_cqe_compress_admin =
3030                         cqe_compress_heuristic(link_speed, pci_bw);
3031         }
3032
3033         priv->params.rx_cqe_compress = priv->params.rx_cqe_compress_admin;
3034
3035         switch (priv->params.rq_wq_type) {
3036         case MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ:
3037                 priv->params.log_rq_size = MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE_MPW;
3038                 priv->params.mpwqe_log_stride_sz =
3039                         priv->params.rx_cqe_compress ?
3040                         MLX5_MPWRQ_LOG_STRIDE_SIZE_CQE_COMPRESS :
3041                         MLX5_MPWRQ_LOG_STRIDE_SIZE;
3042                 priv->params.mpwqe_log_num_strides = MLX5_MPWRQ_LOG_WQE_SZ -
3043                         priv->params.mpwqe_log_stride_sz;
3044                 priv->params.lro_en = true;
3045                 break;
3046         default: /* MLX5_WQ_TYPE_LINKED_LIST */
3047                 priv->params.log_rq_size = MLX5E_PARAMS_DEFAULT_LOG_RQ_SIZE;
3048         }
3049
3050         mlx5_core_info(mdev,
3051                        "MLX5E: StrdRq(%d) RqSz(%ld) StrdSz(%ld) RxCqeCmprss(%d)\n",
3052                        priv->params.rq_wq_type == MLX5_WQ_TYPE_LINKED_LIST_STRIDING_RQ,
3053                        BIT(priv->params.log_rq_size),
3054                        BIT(priv->params.mpwqe_log_stride_sz),
3055                        priv->params.rx_cqe_compress_admin);
3056
3057         priv->params.min_rx_wqes = mlx5_min_rx_wqes(priv->params.rq_wq_type,
3058                                             BIT(priv->params.log_rq_size));
3059
3060         priv->params.rx_am_enabled = MLX5_CAP_GEN(mdev, cq_moderation);
3061         mlx5e_set_rx_cq_mode_params(&priv->params, cq_period_mode);
3062
3063         priv->params.tx_cq_moderation.usec =
3064                 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_USEC;
3065         priv->params.tx_cq_moderation.pkts =
3066                 MLX5E_PARAMS_DEFAULT_TX_CQ_MODERATION_PKTS;
3067         priv->params.tx_max_inline         = mlx5e_get_max_inline_cap(mdev);
3068         mlx5e_query_min_inline(mdev, &priv->params.tx_min_inline_mode);
3069         priv->params.num_tc                = 1;
3070         priv->params.rss_hfunc             = ETH_RSS_HASH_XOR;
3071
3072         netdev_rss_key_fill(priv->params.toeplitz_hash_key,
3073                             sizeof(priv->params.toeplitz_hash_key));
3074
3075         mlx5e_build_default_indir_rqt(mdev, priv->params.indirection_rqt,
3076                                       MLX5E_INDIR_RQT_SIZE, profile->max_nch(mdev));
3077
3078         priv->params.lro_wqe_sz            =
3079                 MLX5E_PARAMS_DEFAULT_LRO_WQE_SZ;
3080
3081         /* Initialize pflags */
3082         MLX5E_SET_PRIV_FLAG(priv, MLX5E_PFLAG_RX_CQE_BASED_MODER,
3083                             priv->params.rx_cq_period_mode == MLX5_CQ_PERIOD_MODE_START_FROM_CQE);
3084
3085         priv->mdev                         = mdev;
3086         priv->netdev                       = netdev;
3087         priv->params.num_channels          = profile->max_nch(mdev);
3088         priv->profile                      = profile;
3089         priv->ppriv                        = ppriv;
3090
3091 #ifdef CONFIG_MLX5_CORE_EN_DCB
3092         mlx5e_ets_init(priv);
3093 #endif
3094
3095         mutex_init(&priv->state_lock);
3096
3097         INIT_WORK(&priv->update_carrier_work, mlx5e_update_carrier_work);
3098         INIT_WORK(&priv->set_rx_mode_work, mlx5e_set_rx_mode_work);
3099         INIT_WORK(&priv->tx_timeout_work, mlx5e_tx_timeout_work);
3100         INIT_DELAYED_WORK(&priv->update_stats_work, mlx5e_update_stats_work);
3101 }
3102
3103 static void mlx5e_set_netdev_dev_addr(struct net_device *netdev)
3104 {
3105         struct mlx5e_priv *priv = netdev_priv(netdev);
3106
3107         mlx5_query_nic_vport_mac_address(priv->mdev, 0, netdev->dev_addr);
3108         if (is_zero_ether_addr(netdev->dev_addr) &&
3109             !MLX5_CAP_GEN(priv->mdev, vport_group_manager)) {
3110                 eth_hw_addr_random(netdev);
3111                 mlx5_core_info(priv->mdev, "Assigned random MAC address %pM\n", netdev->dev_addr);
3112         }
3113 }
3114
3115 static const struct switchdev_ops mlx5e_switchdev_ops = {
3116         .switchdev_port_attr_get        = mlx5e_attr_get,
3117 };
3118
3119 static void mlx5e_build_nic_netdev(struct net_device *netdev)
3120 {
3121         struct mlx5e_priv *priv = netdev_priv(netdev);
3122         struct mlx5_core_dev *mdev = priv->mdev;
3123         bool fcs_supported;
3124         bool fcs_enabled;
3125
3126         SET_NETDEV_DEV(netdev, &mdev->pdev->dev);
3127
3128         if (MLX5_CAP_GEN(mdev, vport_group_manager)) {
3129                 netdev->netdev_ops = &mlx5e_netdev_ops_sriov;
3130 #ifdef CONFIG_MLX5_CORE_EN_DCB
3131                 netdev->dcbnl_ops = &mlx5e_dcbnl_ops;
3132 #endif
3133         } else {
3134                 netdev->netdev_ops = &mlx5e_netdev_ops_basic;
3135         }
3136
3137         netdev->watchdog_timeo    = 15 * HZ;
3138
3139         netdev->ethtool_ops       = &mlx5e_ethtool_ops;
3140
3141         netdev->vlan_features    |= NETIF_F_SG;
3142         netdev->vlan_features    |= NETIF_F_IP_CSUM;
3143         netdev->vlan_features    |= NETIF_F_IPV6_CSUM;
3144         netdev->vlan_features    |= NETIF_F_GRO;
3145         netdev->vlan_features    |= NETIF_F_TSO;
3146         netdev->vlan_features    |= NETIF_F_TSO6;
3147         netdev->vlan_features    |= NETIF_F_RXCSUM;
3148         netdev->vlan_features    |= NETIF_F_RXHASH;
3149
3150         if (!!MLX5_CAP_ETH(mdev, lro_cap))
3151                 netdev->vlan_features    |= NETIF_F_LRO;
3152
3153         netdev->hw_features       = netdev->vlan_features;
3154         netdev->hw_features      |= NETIF_F_HW_VLAN_CTAG_TX;
3155         netdev->hw_features      |= NETIF_F_HW_VLAN_CTAG_RX;
3156         netdev->hw_features      |= NETIF_F_HW_VLAN_CTAG_FILTER;
3157
3158         if (mlx5e_vxlan_allowed(mdev)) {
3159                 netdev->hw_features     |= NETIF_F_GSO_UDP_TUNNEL |
3160                                            NETIF_F_GSO_UDP_TUNNEL_CSUM |
3161                                            NETIF_F_GSO_PARTIAL;
3162                 netdev->hw_enc_features |= NETIF_F_IP_CSUM;
3163                 netdev->hw_enc_features |= NETIF_F_IPV6_CSUM;
3164                 netdev->hw_enc_features |= NETIF_F_TSO;
3165                 netdev->hw_enc_features |= NETIF_F_TSO6;
3166                 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL;
3167                 netdev->hw_enc_features |= NETIF_F_GSO_UDP_TUNNEL_CSUM |
3168                                            NETIF_F_GSO_PARTIAL;
3169                 netdev->gso_partial_features = NETIF_F_GSO_UDP_TUNNEL_CSUM;
3170         }
3171
3172         mlx5_query_port_fcs(mdev, &fcs_supported, &fcs_enabled);
3173
3174         if (fcs_supported)
3175                 netdev->hw_features |= NETIF_F_RXALL;
3176
3177         netdev->features          = netdev->hw_features;
3178         if (!priv->params.lro_en)
3179                 netdev->features  &= ~NETIF_F_LRO;
3180
3181         if (fcs_enabled)
3182                 netdev->features  &= ~NETIF_F_RXALL;
3183
3184 #define FT_CAP(f) MLX5_CAP_FLOWTABLE(mdev, flow_table_properties_nic_receive.f)
3185         if (FT_CAP(flow_modify_en) &&
3186             FT_CAP(modify_root) &&
3187             FT_CAP(identified_miss_table_mode) &&
3188             FT_CAP(flow_table_modify)) {
3189                 netdev->hw_features      |= NETIF_F_HW_TC;
3190 #ifdef CONFIG_RFS_ACCEL
3191                 netdev->hw_features      |= NETIF_F_NTUPLE;
3192 #endif
3193         }
3194
3195         netdev->features         |= NETIF_F_HIGHDMA;
3196
3197         netdev->priv_flags       |= IFF_UNICAST_FLT;
3198
3199         mlx5e_set_netdev_dev_addr(netdev);
3200
3201 #ifdef CONFIG_NET_SWITCHDEV
3202         if (MLX5_CAP_GEN(mdev, vport_group_manager))
3203                 netdev->switchdev_ops = &mlx5e_switchdev_ops;
3204 #endif
3205 }
3206
3207 static void mlx5e_create_q_counter(struct mlx5e_priv *priv)
3208 {
3209         struct mlx5_core_dev *mdev = priv->mdev;
3210         int err;
3211
3212         err = mlx5_core_alloc_q_counter(mdev, &priv->q_counter);
3213         if (err) {
3214                 mlx5_core_warn(mdev, "alloc queue counter failed, %d\n", err);
3215                 priv->q_counter = 0;
3216         }
3217 }
3218
3219 static void mlx5e_destroy_q_counter(struct mlx5e_priv *priv)
3220 {
3221         if (!priv->q_counter)
3222                 return;
3223
3224         mlx5_core_dealloc_q_counter(priv->mdev, priv->q_counter);
3225 }
3226
3227 static int mlx5e_create_umr_mkey(struct mlx5e_priv *priv)
3228 {
3229         struct mlx5_core_dev *mdev = priv->mdev;
3230         u64 npages = priv->profile->max_nch(mdev) * MLX5_CHANNEL_MAX_NUM_MTTS;
3231         int inlen = MLX5_ST_SZ_BYTES(create_mkey_in);
3232         void *mkc;
3233         u32 *in;
3234         int err;
3235
3236         in = mlx5_vzalloc(inlen);
3237         if (!in)
3238                 return -ENOMEM;
3239
3240         mkc = MLX5_ADDR_OF(create_mkey_in, in, memory_key_mkey_entry);
3241
3242         MLX5_SET(mkc, mkc, free, 1);
3243         MLX5_SET(mkc, mkc, umr_en, 1);
3244         MLX5_SET(mkc, mkc, lw, 1);
3245         MLX5_SET(mkc, mkc, lr, 1);
3246         MLX5_SET(mkc, mkc, access_mode, MLX5_MKC_ACCESS_MODE_MTT);
3247
3248         MLX5_SET(mkc, mkc, qpn, 0xffffff);
3249         MLX5_SET(mkc, mkc, pd, mdev->mlx5e_res.pdn);
3250         MLX5_SET64(mkc, mkc, len, npages << PAGE_SHIFT);
3251         MLX5_SET(mkc, mkc, translations_octword_size,
3252                  mlx5e_get_mtt_octw(npages));
3253         MLX5_SET(mkc, mkc, log_page_size, PAGE_SHIFT);
3254
3255         err = mlx5_core_create_mkey(mdev, &priv->umr_mkey, in, inlen);
3256
3257         kvfree(in);
3258         return err;
3259 }
3260
3261 static void mlx5e_nic_init(struct mlx5_core_dev *mdev,
3262                            struct net_device *netdev,
3263                            const struct mlx5e_profile *profile,
3264                            void *ppriv)
3265 {
3266         struct mlx5e_priv *priv = netdev_priv(netdev);
3267
3268         mlx5e_build_nic_netdev_priv(mdev, netdev, profile, ppriv);
3269         mlx5e_build_nic_netdev(netdev);
3270         mlx5e_vxlan_init(priv);
3271 }
3272
3273 static void mlx5e_nic_cleanup(struct mlx5e_priv *priv)
3274 {
3275         struct mlx5_core_dev *mdev = priv->mdev;
3276         struct mlx5_eswitch *esw = mdev->priv.eswitch;
3277
3278         mlx5e_vxlan_cleanup(priv);
3279
3280         if (MLX5_CAP_GEN(mdev, vport_group_manager))
3281                 mlx5_eswitch_unregister_vport_rep(esw, 0);
3282 }
3283
3284 static int mlx5e_init_nic_rx(struct mlx5e_priv *priv)
3285 {
3286         struct mlx5_core_dev *mdev = priv->mdev;
3287         int err;
3288         int i;
3289
3290         err = mlx5e_create_indirect_rqts(priv);
3291         if (err) {
3292                 mlx5_core_warn(mdev, "create indirect rqts failed, %d\n", err);
3293                 return err;
3294         }
3295
3296         err = mlx5e_create_direct_rqts(priv);
3297         if (err) {
3298                 mlx5_core_warn(mdev, "create direct rqts failed, %d\n", err);
3299                 goto err_destroy_indirect_rqts;
3300         }
3301
3302         err = mlx5e_create_indirect_tirs(priv);
3303         if (err) {
3304                 mlx5_core_warn(mdev, "create indirect tirs failed, %d\n", err);
3305                 goto err_destroy_direct_rqts;
3306         }
3307
3308         err = mlx5e_create_direct_tirs(priv);
3309         if (err) {
3310                 mlx5_core_warn(mdev, "create direct tirs failed, %d\n", err);
3311                 goto err_destroy_indirect_tirs;
3312         }
3313
3314         err = mlx5e_create_flow_steering(priv);
3315         if (err) {
3316                 mlx5_core_warn(mdev, "create flow steering failed, %d\n", err);
3317                 goto err_destroy_direct_tirs;
3318         }
3319
3320         err = mlx5e_tc_init(priv);
3321         if (err)
3322                 goto err_destroy_flow_steering;
3323
3324         return 0;
3325
3326 err_destroy_flow_steering:
3327         mlx5e_destroy_flow_steering(priv);
3328 err_destroy_direct_tirs:
3329         mlx5e_destroy_direct_tirs(priv);
3330 err_destroy_indirect_tirs:
3331         mlx5e_destroy_indirect_tirs(priv);
3332 err_destroy_direct_rqts:
3333         for (i = 0; i < priv->profile->max_nch(mdev); i++)
3334                 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
3335 err_destroy_indirect_rqts:
3336         mlx5e_destroy_rqt(priv, &priv->indir_rqt);
3337         return err;
3338 }
3339
3340 static void mlx5e_cleanup_nic_rx(struct mlx5e_priv *priv)
3341 {
3342         int i;
3343
3344         mlx5e_tc_cleanup(priv);
3345         mlx5e_destroy_flow_steering(priv);
3346         mlx5e_destroy_direct_tirs(priv);
3347         mlx5e_destroy_indirect_tirs(priv);
3348         for (i = 0; i < priv->profile->max_nch(priv->mdev); i++)
3349                 mlx5e_destroy_rqt(priv, &priv->direct_tir[i].rqt);
3350         mlx5e_destroy_rqt(priv, &priv->indir_rqt);
3351 }
3352
3353 static int mlx5e_init_nic_tx(struct mlx5e_priv *priv)
3354 {
3355         int err;
3356
3357         err = mlx5e_create_tises(priv);
3358         if (err) {
3359                 mlx5_core_warn(priv->mdev, "create tises failed, %d\n", err);
3360                 return err;
3361         }
3362
3363 #ifdef CONFIG_MLX5_CORE_EN_DCB
3364         mlx5e_dcbnl_ieee_setets_core(priv, &priv->params.ets);
3365 #endif
3366         return 0;
3367 }
3368
3369 static void mlx5e_nic_enable(struct mlx5e_priv *priv)
3370 {
3371         struct net_device *netdev = priv->netdev;
3372         struct mlx5_core_dev *mdev = priv->mdev;
3373         struct mlx5_eswitch *esw = mdev->priv.eswitch;
3374         struct mlx5_eswitch_rep rep;
3375
3376         mlx5_lag_add(mdev, netdev);
3377
3378         if (mlx5e_vxlan_allowed(mdev)) {
3379                 rtnl_lock();
3380                 udp_tunnel_get_rx_info(netdev);
3381                 rtnl_unlock();
3382         }
3383
3384         mlx5e_enable_async_events(priv);
3385         queue_work(priv->wq, &priv->set_rx_mode_work);
3386
3387         if (MLX5_CAP_GEN(mdev, vport_group_manager)) {
3388                 rep.load = mlx5e_nic_rep_load;
3389                 rep.unload = mlx5e_nic_rep_unload;
3390                 rep.vport = 0;
3391                 rep.priv_data = priv;
3392                 mlx5_eswitch_register_vport_rep(esw, &rep);
3393         }
3394 }
3395
3396 static void mlx5e_nic_disable(struct mlx5e_priv *priv)
3397 {
3398         queue_work(priv->wq, &priv->set_rx_mode_work);
3399         mlx5e_disable_async_events(priv);
3400         mlx5_lag_remove(priv->mdev);
3401 }
3402
3403 static const struct mlx5e_profile mlx5e_nic_profile = {
3404         .init              = mlx5e_nic_init,
3405         .cleanup           = mlx5e_nic_cleanup,
3406         .init_rx           = mlx5e_init_nic_rx,
3407         .cleanup_rx        = mlx5e_cleanup_nic_rx,
3408         .init_tx           = mlx5e_init_nic_tx,
3409         .cleanup_tx        = mlx5e_cleanup_nic_tx,
3410         .enable            = mlx5e_nic_enable,
3411         .disable           = mlx5e_nic_disable,
3412         .update_stats      = mlx5e_update_stats,
3413         .max_nch           = mlx5e_get_max_num_channels,
3414         .max_tc            = MLX5E_MAX_NUM_TC,
3415 };
3416
3417 void *mlx5e_create_netdev(struct mlx5_core_dev *mdev,
3418                           const struct mlx5e_profile *profile, void *ppriv)
3419 {
3420         struct net_device *netdev;
3421         struct mlx5e_priv *priv;
3422         int nch = profile->max_nch(mdev);
3423         int err;
3424
3425         netdev = alloc_etherdev_mqs(sizeof(struct mlx5e_priv),
3426                                     nch * profile->max_tc,
3427                                     nch);
3428         if (!netdev) {
3429                 mlx5_core_err(mdev, "alloc_etherdev_mqs() failed\n");
3430                 return NULL;
3431         }
3432
3433         profile->init(mdev, netdev, profile, ppriv);
3434
3435         netif_carrier_off(netdev);
3436
3437         priv = netdev_priv(netdev);
3438
3439         priv->wq = create_singlethread_workqueue("mlx5e");
3440         if (!priv->wq)
3441                 goto err_free_netdev;
3442
3443         err = mlx5e_create_umr_mkey(priv);
3444         if (err) {
3445                 mlx5_core_err(mdev, "create umr mkey failed, %d\n", err);
3446                 goto err_destroy_wq;
3447         }
3448
3449         err = profile->init_tx(priv);
3450         if (err)
3451                 goto err_destroy_umr_mkey;
3452
3453         err = mlx5e_open_drop_rq(priv);
3454         if (err) {
3455                 mlx5_core_err(mdev, "open drop rq failed, %d\n", err);
3456                 goto err_cleanup_tx;
3457         }
3458
3459         err = profile->init_rx(priv);
3460         if (err)
3461                 goto err_close_drop_rq;
3462
3463         mlx5e_create_q_counter(priv);
3464
3465         mlx5e_init_l2_addr(priv);
3466
3467         err = register_netdev(netdev);
3468         if (err) {
3469                 mlx5_core_err(mdev, "register_netdev failed, %d\n", err);
3470                 goto err_dealloc_q_counters;
3471         }
3472
3473         if (profile->enable)
3474                 profile->enable(priv);
3475
3476         return priv;
3477
3478 err_dealloc_q_counters:
3479         mlx5e_destroy_q_counter(priv);
3480         profile->cleanup_rx(priv);
3481
3482 err_close_drop_rq:
3483         mlx5e_close_drop_rq(priv);
3484
3485 err_cleanup_tx:
3486         profile->cleanup_tx(priv);
3487
3488 err_destroy_umr_mkey:
3489         mlx5_core_destroy_mkey(mdev, &priv->umr_mkey);
3490
3491 err_destroy_wq:
3492         destroy_workqueue(priv->wq);
3493
3494 err_free_netdev:
3495         free_netdev(netdev);
3496
3497         return NULL;
3498 }
3499
3500 static void mlx5e_register_vport_rep(struct mlx5_core_dev *mdev)
3501 {
3502         struct mlx5_eswitch *esw = mdev->priv.eswitch;
3503         int total_vfs = MLX5_TOTAL_VPORTS(mdev);
3504         int vport;
3505
3506         if (!MLX5_CAP_GEN(mdev, vport_group_manager))
3507                 return;
3508
3509         for (vport = 1; vport < total_vfs; vport++) {
3510                 struct mlx5_eswitch_rep rep;
3511
3512                 rep.load = mlx5e_vport_rep_load;
3513                 rep.unload = mlx5e_vport_rep_unload;
3514                 rep.vport = vport;
3515                 mlx5_eswitch_register_vport_rep(esw, &rep);
3516         }
3517 }
3518
3519 static void *mlx5e_add(struct mlx5_core_dev *mdev)
3520 {
3521         struct mlx5_eswitch *esw = mdev->priv.eswitch;
3522         void *ppriv = NULL;
3523         void *ret;
3524
3525         if (mlx5e_check_required_hca_cap(mdev))
3526                 return NULL;
3527
3528         if (mlx5e_create_mdev_resources(mdev))
3529                 return NULL;
3530
3531         mlx5e_register_vport_rep(mdev);
3532
3533         if (MLX5_CAP_GEN(mdev, vport_group_manager))
3534                 ppriv = &esw->offloads.vport_reps[0];
3535
3536         ret = mlx5e_create_netdev(mdev, &mlx5e_nic_profile, ppriv);
3537         if (!ret) {
3538                 mlx5e_destroy_mdev_resources(mdev);
3539                 return NULL;
3540         }
3541         return ret;
3542 }
3543
3544 void mlx5e_destroy_netdev(struct mlx5_core_dev *mdev, struct mlx5e_priv *priv)
3545 {
3546         const struct mlx5e_profile *profile = priv->profile;
3547         struct net_device *netdev = priv->netdev;
3548
3549         set_bit(MLX5E_STATE_DESTROYING, &priv->state);
3550         if (profile->disable)
3551                 profile->disable(priv);
3552
3553         flush_workqueue(priv->wq);
3554         if (test_bit(MLX5_INTERFACE_STATE_SHUTDOWN, &mdev->intf_state)) {
3555                 netif_device_detach(netdev);
3556                 mlx5e_close(netdev);
3557         } else {
3558                 unregister_netdev(netdev);
3559         }
3560
3561         mlx5e_destroy_q_counter(priv);
3562         profile->cleanup_rx(priv);
3563         mlx5e_close_drop_rq(priv);
3564         profile->cleanup_tx(priv);
3565         mlx5_core_destroy_mkey(priv->mdev, &priv->umr_mkey);
3566         cancel_delayed_work_sync(&priv->update_stats_work);
3567         destroy_workqueue(priv->wq);
3568         if (profile->cleanup)
3569                 profile->cleanup(priv);
3570
3571         if (!test_bit(MLX5_INTERFACE_STATE_SHUTDOWN, &mdev->intf_state))
3572                 free_netdev(netdev);
3573 }
3574
3575 static void mlx5e_remove(struct mlx5_core_dev *mdev, void *vpriv)
3576 {
3577         struct mlx5_eswitch *esw = mdev->priv.eswitch;
3578         int total_vfs = MLX5_TOTAL_VPORTS(mdev);
3579         struct mlx5e_priv *priv = vpriv;
3580         int vport;
3581
3582         mlx5e_destroy_netdev(mdev, priv);
3583
3584         for (vport = 1; vport < total_vfs; vport++)
3585                 mlx5_eswitch_unregister_vport_rep(esw, vport);
3586
3587         mlx5e_destroy_mdev_resources(mdev);
3588 }
3589
3590 static void *mlx5e_get_netdev(void *vpriv)
3591 {
3592         struct mlx5e_priv *priv = vpriv;
3593
3594         return priv->netdev;
3595 }
3596
3597 static struct mlx5_interface mlx5e_interface = {
3598         .add       = mlx5e_add,
3599         .remove    = mlx5e_remove,
3600         .event     = mlx5e_async_event,
3601         .protocol  = MLX5_INTERFACE_PROTOCOL_ETH,
3602         .get_dev   = mlx5e_get_netdev,
3603 };
3604
3605 void mlx5e_init(void)
3606 {
3607         mlx5e_build_ptys2ethtool_map();
3608         mlx5_register_interface(&mlx5e_interface);
3609 }
3610
3611 void mlx5e_cleanup(void)
3612 {
3613         mlx5_unregister_interface(&mlx5e_interface);
3614 }