d13b6d69e54583ad06bb1ed7235fcaca6955b330
[dpdk.git] / drivers / net / sfc / sfc_ethdev.c
1 /*-
2  * Copyright (c) 2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * This software was jointly developed between OKTET Labs (under contract
6  * for Solarflare) and Solarflare Communications, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  *    this list of conditions and the following disclaimer in the documentation
15  *    and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <rte_dev.h>
31 #include <rte_ethdev.h>
32 #include <rte_pci.h>
33
34 #include "efx.h"
35
36 #include "sfc.h"
37 #include "sfc_debug.h"
38 #include "sfc_log.h"
39 #include "sfc_kvargs.h"
40 #include "sfc_ev.h"
41 #include "sfc_rx.h"
42 #include "sfc_tx.h"
43
44
45 static void
46 sfc_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
47 {
48         struct sfc_adapter *sa = dev->data->dev_private;
49         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
50
51         sfc_log_init(sa, "entry");
52
53         dev_info->pci_dev = RTE_DEV_TO_PCI(dev->device);
54         dev_info->max_rx_pktlen = EFX_MAC_PDU_MAX;
55
56         /* Autonegotiation may be disabled */
57         dev_info->speed_capa = ETH_LINK_SPEED_FIXED;
58         if (sa->port.phy_adv_cap_mask & EFX_PHY_CAP_1000FDX)
59                 dev_info->speed_capa |= ETH_LINK_SPEED_1G;
60         if (sa->port.phy_adv_cap_mask & EFX_PHY_CAP_10000FDX)
61                 dev_info->speed_capa |= ETH_LINK_SPEED_10G;
62         if (sa->port.phy_adv_cap_mask & EFX_PHY_CAP_40000FDX)
63                 dev_info->speed_capa |= ETH_LINK_SPEED_40G;
64
65         dev_info->max_rx_queues = sa->rxq_max;
66         dev_info->max_tx_queues = sa->txq_max;
67
68         /* By default packets are dropped if no descriptors are available */
69         dev_info->default_rxconf.rx_drop_en = 1;
70
71         dev_info->rx_offload_capa =
72                 DEV_RX_OFFLOAD_IPV4_CKSUM |
73                 DEV_RX_OFFLOAD_UDP_CKSUM |
74                 DEV_RX_OFFLOAD_TCP_CKSUM;
75
76         dev_info->tx_offload_capa =
77                 DEV_TX_OFFLOAD_IPV4_CKSUM |
78                 DEV_TX_OFFLOAD_UDP_CKSUM |
79                 DEV_TX_OFFLOAD_TCP_CKSUM;
80
81         dev_info->default_txconf.txq_flags = ETH_TXQ_FLAGS_NOXSUMSCTP;
82         if (!encp->enc_hw_tx_insert_vlan_enabled)
83                 dev_info->default_txconf.txq_flags |= ETH_TXQ_FLAGS_NOVLANOFFL;
84         else
85                 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_VLAN_INSERT;
86
87 #if EFSYS_OPT_RX_SCALE
88         if (sa->rss_support != EFX_RX_SCALE_UNAVAILABLE) {
89                 dev_info->reta_size = EFX_RSS_TBL_SIZE;
90                 dev_info->hash_key_size = SFC_RSS_KEY_SIZE;
91                 dev_info->flow_type_rss_offloads = SFC_RSS_OFFLOADS;
92         }
93 #endif
94
95         if (sa->tso)
96                 dev_info->tx_offload_capa |= DEV_TX_OFFLOAD_TCP_TSO;
97
98         dev_info->rx_desc_lim.nb_max = EFX_RXQ_MAXNDESCS;
99         dev_info->rx_desc_lim.nb_min = EFX_RXQ_MINNDESCS;
100         /* The RXQ hardware requires that the descriptor count is a power
101          * of 2, but rx_desc_lim cannot properly describe that constraint.
102          */
103         dev_info->rx_desc_lim.nb_align = EFX_RXQ_MINNDESCS;
104
105         dev_info->tx_desc_lim.nb_max = sa->txq_max_entries;
106         dev_info->tx_desc_lim.nb_min = EFX_TXQ_MINNDESCS;
107         /*
108          * The TXQ hardware requires that the descriptor count is a power
109          * of 2, but tx_desc_lim cannot properly describe that constraint
110          */
111         dev_info->tx_desc_lim.nb_align = EFX_TXQ_MINNDESCS;
112 }
113
114 static const uint32_t *
115 sfc_dev_supported_ptypes_get(struct rte_eth_dev *dev)
116 {
117         static const uint32_t ptypes[] = {
118                 RTE_PTYPE_L2_ETHER,
119                 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
120                 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
121                 RTE_PTYPE_L4_TCP,
122                 RTE_PTYPE_L4_UDP,
123                 RTE_PTYPE_UNKNOWN
124         };
125
126         if (dev->rx_pkt_burst == sfc_recv_pkts)
127                 return ptypes;
128
129         return NULL;
130 }
131
132 static int
133 sfc_dev_configure(struct rte_eth_dev *dev)
134 {
135         struct rte_eth_dev_data *dev_data = dev->data;
136         struct sfc_adapter *sa = dev_data->dev_private;
137         int rc;
138
139         sfc_log_init(sa, "entry n_rxq=%u n_txq=%u",
140                      dev_data->nb_rx_queues, dev_data->nb_tx_queues);
141
142         sfc_adapter_lock(sa);
143         switch (sa->state) {
144         case SFC_ADAPTER_CONFIGURED:
145                 sfc_close(sa);
146                 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
147                 /* FALLTHROUGH */
148         case SFC_ADAPTER_INITIALIZED:
149                 rc = sfc_configure(sa);
150                 break;
151         default:
152                 sfc_err(sa, "unexpected adapter state %u to configure",
153                         sa->state);
154                 rc = EINVAL;
155                 break;
156         }
157         sfc_adapter_unlock(sa);
158
159         sfc_log_init(sa, "done %d", rc);
160         SFC_ASSERT(rc >= 0);
161         return -rc;
162 }
163
164 static int
165 sfc_dev_start(struct rte_eth_dev *dev)
166 {
167         struct sfc_adapter *sa = dev->data->dev_private;
168         int rc;
169
170         sfc_log_init(sa, "entry");
171
172         sfc_adapter_lock(sa);
173         rc = sfc_start(sa);
174         sfc_adapter_unlock(sa);
175
176         sfc_log_init(sa, "done %d", rc);
177         SFC_ASSERT(rc >= 0);
178         return -rc;
179 }
180
181 static int
182 sfc_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
183 {
184         struct sfc_adapter *sa = dev->data->dev_private;
185         struct rte_eth_link *dev_link = &dev->data->dev_link;
186         struct rte_eth_link old_link;
187         struct rte_eth_link current_link;
188
189         sfc_log_init(sa, "entry");
190
191         if (sa->state != SFC_ADAPTER_STARTED)
192                 return 0;
193
194 retry:
195         EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
196         *(int64_t *)&old_link = rte_atomic64_read((rte_atomic64_t *)dev_link);
197
198         if (wait_to_complete) {
199                 efx_link_mode_t link_mode;
200
201                 if (efx_port_poll(sa->nic, &link_mode) != 0)
202                         link_mode = EFX_LINK_UNKNOWN;
203                 sfc_port_link_mode_to_info(link_mode, &current_link);
204
205                 if (!rte_atomic64_cmpset((volatile uint64_t *)dev_link,
206                                          *(uint64_t *)&old_link,
207                                          *(uint64_t *)&current_link))
208                         goto retry;
209         } else {
210                 sfc_ev_mgmt_qpoll(sa);
211                 *(int64_t *)&current_link =
212                         rte_atomic64_read((rte_atomic64_t *)dev_link);
213         }
214
215         if (old_link.link_status != current_link.link_status)
216                 sfc_info(sa, "Link status is %s",
217                          current_link.link_status ? "UP" : "DOWN");
218
219         return old_link.link_status == current_link.link_status ? 0 : -1;
220 }
221
222 static void
223 sfc_dev_stop(struct rte_eth_dev *dev)
224 {
225         struct sfc_adapter *sa = dev->data->dev_private;
226
227         sfc_log_init(sa, "entry");
228
229         sfc_adapter_lock(sa);
230         sfc_stop(sa);
231         sfc_adapter_unlock(sa);
232
233         sfc_log_init(sa, "done");
234 }
235
236 static int
237 sfc_dev_set_link_up(struct rte_eth_dev *dev)
238 {
239         struct sfc_adapter *sa = dev->data->dev_private;
240         int rc;
241
242         sfc_log_init(sa, "entry");
243
244         sfc_adapter_lock(sa);
245         rc = sfc_start(sa);
246         sfc_adapter_unlock(sa);
247
248         SFC_ASSERT(rc >= 0);
249         return -rc;
250 }
251
252 static int
253 sfc_dev_set_link_down(struct rte_eth_dev *dev)
254 {
255         struct sfc_adapter *sa = dev->data->dev_private;
256
257         sfc_log_init(sa, "entry");
258
259         sfc_adapter_lock(sa);
260         sfc_stop(sa);
261         sfc_adapter_unlock(sa);
262
263         return 0;
264 }
265
266 static void
267 sfc_dev_close(struct rte_eth_dev *dev)
268 {
269         struct sfc_adapter *sa = dev->data->dev_private;
270
271         sfc_log_init(sa, "entry");
272
273         sfc_adapter_lock(sa);
274         switch (sa->state) {
275         case SFC_ADAPTER_STARTED:
276                 sfc_stop(sa);
277                 SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
278                 /* FALLTHROUGH */
279         case SFC_ADAPTER_CONFIGURED:
280                 sfc_close(sa);
281                 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
282                 /* FALLTHROUGH */
283         case SFC_ADAPTER_INITIALIZED:
284                 break;
285         default:
286                 sfc_err(sa, "unexpected adapter state %u on close", sa->state);
287                 break;
288         }
289         sfc_adapter_unlock(sa);
290
291         sfc_log_init(sa, "done");
292 }
293
294 static void
295 sfc_dev_filter_set(struct rte_eth_dev *dev, enum sfc_dev_filter_mode mode,
296                    boolean_t enabled)
297 {
298         struct sfc_port *port;
299         boolean_t *toggle;
300         struct sfc_adapter *sa = dev->data->dev_private;
301         boolean_t allmulti = (mode == SFC_DEV_FILTER_MODE_ALLMULTI);
302         const char *desc = (allmulti) ? "all-multi" : "promiscuous";
303
304         sfc_adapter_lock(sa);
305
306         port = &sa->port;
307         toggle = (allmulti) ? (&port->allmulti) : (&port->promisc);
308
309         if (*toggle != enabled) {
310                 *toggle = enabled;
311
312                 if ((sa->state == SFC_ADAPTER_STARTED) &&
313                     (sfc_set_rx_mode(sa) != 0)) {
314                         *toggle = !(enabled);
315                         sfc_warn(sa, "Failed to %s %s mode",
316                                  ((enabled) ? "enable" : "disable"), desc);
317                 }
318         }
319
320         sfc_adapter_unlock(sa);
321 }
322
323 static void
324 sfc_dev_promisc_enable(struct rte_eth_dev *dev)
325 {
326         sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_PROMISC, B_TRUE);
327 }
328
329 static void
330 sfc_dev_promisc_disable(struct rte_eth_dev *dev)
331 {
332         sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_PROMISC, B_FALSE);
333 }
334
335 static void
336 sfc_dev_allmulti_enable(struct rte_eth_dev *dev)
337 {
338         sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_ALLMULTI, B_TRUE);
339 }
340
341 static void
342 sfc_dev_allmulti_disable(struct rte_eth_dev *dev)
343 {
344         sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_ALLMULTI, B_FALSE);
345 }
346
347 static int
348 sfc_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
349                    uint16_t nb_rx_desc, unsigned int socket_id,
350                    const struct rte_eth_rxconf *rx_conf,
351                    struct rte_mempool *mb_pool)
352 {
353         struct sfc_adapter *sa = dev->data->dev_private;
354         int rc;
355
356         sfc_log_init(sa, "RxQ=%u nb_rx_desc=%u socket_id=%u",
357                      rx_queue_id, nb_rx_desc, socket_id);
358
359         sfc_adapter_lock(sa);
360
361         rc = sfc_rx_qinit(sa, rx_queue_id, nb_rx_desc, socket_id,
362                           rx_conf, mb_pool);
363         if (rc != 0)
364                 goto fail_rx_qinit;
365
366         dev->data->rx_queues[rx_queue_id] = sa->rxq_info[rx_queue_id].rxq;
367
368         sfc_adapter_unlock(sa);
369
370         return 0;
371
372 fail_rx_qinit:
373         sfc_adapter_unlock(sa);
374         SFC_ASSERT(rc > 0);
375         return -rc;
376 }
377
378 static void
379 sfc_rx_queue_release(void *queue)
380 {
381         struct sfc_rxq *rxq = queue;
382         struct sfc_adapter *sa;
383         unsigned int sw_index;
384
385         if (rxq == NULL)
386                 return;
387
388         sa = rxq->evq->sa;
389         sfc_adapter_lock(sa);
390
391         sw_index = sfc_rxq_sw_index(rxq);
392
393         sfc_log_init(sa, "RxQ=%u", sw_index);
394
395         sa->eth_dev->data->rx_queues[sw_index] = NULL;
396
397         sfc_rx_qfini(sa, sw_index);
398
399         sfc_adapter_unlock(sa);
400 }
401
402 static int
403 sfc_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
404                    uint16_t nb_tx_desc, unsigned int socket_id,
405                    const struct rte_eth_txconf *tx_conf)
406 {
407         struct sfc_adapter *sa = dev->data->dev_private;
408         int rc;
409
410         sfc_log_init(sa, "TxQ = %u, nb_tx_desc = %u, socket_id = %u",
411                      tx_queue_id, nb_tx_desc, socket_id);
412
413         sfc_adapter_lock(sa);
414
415         rc = sfc_tx_qinit(sa, tx_queue_id, nb_tx_desc, socket_id, tx_conf);
416         if (rc != 0)
417                 goto fail_tx_qinit;
418
419         dev->data->tx_queues[tx_queue_id] = sa->txq_info[tx_queue_id].txq;
420
421         sfc_adapter_unlock(sa);
422         return 0;
423
424 fail_tx_qinit:
425         sfc_adapter_unlock(sa);
426         SFC_ASSERT(rc > 0);
427         return -rc;
428 }
429
430 static void
431 sfc_tx_queue_release(void *queue)
432 {
433         struct sfc_txq *txq = queue;
434         unsigned int sw_index;
435         struct sfc_adapter *sa;
436
437         if (txq == NULL)
438                 return;
439
440         sw_index = sfc_txq_sw_index(txq);
441
442         SFC_ASSERT(txq->evq != NULL);
443         sa = txq->evq->sa;
444
445         sfc_log_init(sa, "TxQ = %u", sw_index);
446
447         sfc_adapter_lock(sa);
448
449         SFC_ASSERT(sw_index < sa->eth_dev->data->nb_tx_queues);
450         sa->eth_dev->data->tx_queues[sw_index] = NULL;
451
452         sfc_tx_qfini(sa, sw_index);
453
454         sfc_adapter_unlock(sa);
455 }
456
457 static void
458 sfc_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
459 {
460         struct sfc_adapter *sa = dev->data->dev_private;
461         struct sfc_port *port = &sa->port;
462         uint64_t *mac_stats;
463
464         rte_spinlock_lock(&port->mac_stats_lock);
465
466         if (sfc_port_update_mac_stats(sa) != 0)
467                 goto unlock;
468
469         mac_stats = port->mac_stats_buf;
470
471         if (EFX_MAC_STAT_SUPPORTED(port->mac_stats_mask,
472                                    EFX_MAC_VADAPTER_RX_UNICAST_PACKETS)) {
473                 stats->ipackets =
474                         mac_stats[EFX_MAC_VADAPTER_RX_UNICAST_PACKETS] +
475                         mac_stats[EFX_MAC_VADAPTER_RX_MULTICAST_PACKETS] +
476                         mac_stats[EFX_MAC_VADAPTER_RX_BROADCAST_PACKETS];
477                 stats->opackets =
478                         mac_stats[EFX_MAC_VADAPTER_TX_UNICAST_PACKETS] +
479                         mac_stats[EFX_MAC_VADAPTER_TX_MULTICAST_PACKETS] +
480                         mac_stats[EFX_MAC_VADAPTER_TX_BROADCAST_PACKETS];
481                 stats->ibytes =
482                         mac_stats[EFX_MAC_VADAPTER_RX_UNICAST_BYTES] +
483                         mac_stats[EFX_MAC_VADAPTER_RX_MULTICAST_BYTES] +
484                         mac_stats[EFX_MAC_VADAPTER_RX_BROADCAST_BYTES];
485                 stats->obytes =
486                         mac_stats[EFX_MAC_VADAPTER_TX_UNICAST_BYTES] +
487                         mac_stats[EFX_MAC_VADAPTER_TX_MULTICAST_BYTES] +
488                         mac_stats[EFX_MAC_VADAPTER_TX_BROADCAST_BYTES];
489                 stats->imissed = mac_stats[EFX_MAC_VADAPTER_RX_OVERFLOW];
490                 stats->ierrors = mac_stats[EFX_MAC_VADAPTER_RX_BAD_PACKETS];
491                 stats->oerrors = mac_stats[EFX_MAC_VADAPTER_TX_BAD_PACKETS];
492         } else {
493                 stats->ipackets = mac_stats[EFX_MAC_RX_PKTS];
494                 stats->opackets = mac_stats[EFX_MAC_TX_PKTS];
495                 stats->ibytes = mac_stats[EFX_MAC_RX_OCTETS];
496                 stats->obytes = mac_stats[EFX_MAC_TX_OCTETS];
497                 /*
498                  * Take into account stats which are whenever supported
499                  * on EF10. If some stat is not supported by current
500                  * firmware variant or HW revision, it is guaranteed
501                  * to be zero in mac_stats.
502                  */
503                 stats->imissed =
504                         mac_stats[EFX_MAC_RX_NODESC_DROP_CNT] +
505                         mac_stats[EFX_MAC_PM_TRUNC_BB_OVERFLOW] +
506                         mac_stats[EFX_MAC_PM_DISCARD_BB_OVERFLOW] +
507                         mac_stats[EFX_MAC_PM_TRUNC_VFIFO_FULL] +
508                         mac_stats[EFX_MAC_PM_DISCARD_VFIFO_FULL] +
509                         mac_stats[EFX_MAC_PM_TRUNC_QBB] +
510                         mac_stats[EFX_MAC_PM_DISCARD_QBB] +
511                         mac_stats[EFX_MAC_PM_DISCARD_MAPPING] +
512                         mac_stats[EFX_MAC_RXDP_Q_DISABLED_PKTS] +
513                         mac_stats[EFX_MAC_RXDP_DI_DROPPED_PKTS];
514                 stats->ierrors =
515                         mac_stats[EFX_MAC_RX_FCS_ERRORS] +
516                         mac_stats[EFX_MAC_RX_ALIGN_ERRORS] +
517                         mac_stats[EFX_MAC_RX_JABBER_PKTS];
518                 /* no oerrors counters supported on EF10 */
519         }
520
521 unlock:
522         rte_spinlock_unlock(&port->mac_stats_lock);
523 }
524
525 static int
526 sfc_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
527                unsigned int xstats_count)
528 {
529         struct sfc_adapter *sa = dev->data->dev_private;
530         struct sfc_port *port = &sa->port;
531         uint64_t *mac_stats;
532         int rc;
533         unsigned int i;
534         int nstats = 0;
535
536         rte_spinlock_lock(&port->mac_stats_lock);
537
538         rc = sfc_port_update_mac_stats(sa);
539         if (rc != 0) {
540                 SFC_ASSERT(rc > 0);
541                 nstats = -rc;
542                 goto unlock;
543         }
544
545         mac_stats = port->mac_stats_buf;
546
547         for (i = 0; i < EFX_MAC_NSTATS; ++i) {
548                 if (EFX_MAC_STAT_SUPPORTED(port->mac_stats_mask, i)) {
549                         if (xstats != NULL && nstats < (int)xstats_count) {
550                                 xstats[nstats].id = nstats;
551                                 xstats[nstats].value = mac_stats[i];
552                         }
553                         nstats++;
554                 }
555         }
556
557 unlock:
558         rte_spinlock_unlock(&port->mac_stats_lock);
559
560         return nstats;
561 }
562
563 static int
564 sfc_xstats_get_names(struct rte_eth_dev *dev,
565                      struct rte_eth_xstat_name *xstats_names,
566                      unsigned int xstats_count)
567 {
568         struct sfc_adapter *sa = dev->data->dev_private;
569         struct sfc_port *port = &sa->port;
570         unsigned int i;
571         unsigned int nstats = 0;
572
573         for (i = 0; i < EFX_MAC_NSTATS; ++i) {
574                 if (EFX_MAC_STAT_SUPPORTED(port->mac_stats_mask, i)) {
575                         if (xstats_names != NULL && nstats < xstats_count)
576                                 strncpy(xstats_names[nstats].name,
577                                         efx_mac_stat_name(sa->nic, i),
578                                         sizeof(xstats_names[0].name));
579                         nstats++;
580                 }
581         }
582
583         return nstats;
584 }
585
586 static int
587 sfc_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
588 {
589         struct sfc_adapter *sa = dev->data->dev_private;
590         unsigned int wanted_fc, link_fc;
591
592         memset(fc_conf, 0, sizeof(*fc_conf));
593
594         sfc_adapter_lock(sa);
595
596         if (sa->state == SFC_ADAPTER_STARTED)
597                 efx_mac_fcntl_get(sa->nic, &wanted_fc, &link_fc);
598         else
599                 link_fc = sa->port.flow_ctrl;
600
601         switch (link_fc) {
602         case 0:
603                 fc_conf->mode = RTE_FC_NONE;
604                 break;
605         case EFX_FCNTL_RESPOND:
606                 fc_conf->mode = RTE_FC_RX_PAUSE;
607                 break;
608         case EFX_FCNTL_GENERATE:
609                 fc_conf->mode = RTE_FC_TX_PAUSE;
610                 break;
611         case (EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE):
612                 fc_conf->mode = RTE_FC_FULL;
613                 break;
614         default:
615                 sfc_err(sa, "%s: unexpected flow control value %#x",
616                         __func__, link_fc);
617         }
618
619         fc_conf->autoneg = sa->port.flow_ctrl_autoneg;
620
621         sfc_adapter_unlock(sa);
622
623         return 0;
624 }
625
626 static int
627 sfc_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
628 {
629         struct sfc_adapter *sa = dev->data->dev_private;
630         struct sfc_port *port = &sa->port;
631         unsigned int fcntl;
632         int rc;
633
634         if (fc_conf->high_water != 0 || fc_conf->low_water != 0 ||
635             fc_conf->pause_time != 0 || fc_conf->send_xon != 0 ||
636             fc_conf->mac_ctrl_frame_fwd != 0) {
637                 sfc_err(sa, "unsupported flow control settings specified");
638                 rc = EINVAL;
639                 goto fail_inval;
640         }
641
642         switch (fc_conf->mode) {
643         case RTE_FC_NONE:
644                 fcntl = 0;
645                 break;
646         case RTE_FC_RX_PAUSE:
647                 fcntl = EFX_FCNTL_RESPOND;
648                 break;
649         case RTE_FC_TX_PAUSE:
650                 fcntl = EFX_FCNTL_GENERATE;
651                 break;
652         case RTE_FC_FULL:
653                 fcntl = EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE;
654                 break;
655         default:
656                 rc = EINVAL;
657                 goto fail_inval;
658         }
659
660         sfc_adapter_lock(sa);
661
662         if (sa->state == SFC_ADAPTER_STARTED) {
663                 rc = efx_mac_fcntl_set(sa->nic, fcntl, fc_conf->autoneg);
664                 if (rc != 0)
665                         goto fail_mac_fcntl_set;
666         }
667
668         port->flow_ctrl = fcntl;
669         port->flow_ctrl_autoneg = fc_conf->autoneg;
670
671         sfc_adapter_unlock(sa);
672
673         return 0;
674
675 fail_mac_fcntl_set:
676         sfc_adapter_unlock(sa);
677 fail_inval:
678         SFC_ASSERT(rc > 0);
679         return -rc;
680 }
681
682 static int
683 sfc_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
684 {
685         struct sfc_adapter *sa = dev->data->dev_private;
686         size_t pdu = EFX_MAC_PDU(mtu);
687         size_t old_pdu;
688         int rc;
689
690         sfc_log_init(sa, "mtu=%u", mtu);
691
692         rc = EINVAL;
693         if (pdu < EFX_MAC_PDU_MIN) {
694                 sfc_err(sa, "too small MTU %u (PDU size %u less than min %u)",
695                         (unsigned int)mtu, (unsigned int)pdu,
696                         EFX_MAC_PDU_MIN);
697                 goto fail_inval;
698         }
699         if (pdu > EFX_MAC_PDU_MAX) {
700                 sfc_err(sa, "too big MTU %u (PDU size %u greater than max %u)",
701                         (unsigned int)mtu, (unsigned int)pdu,
702                         EFX_MAC_PDU_MAX);
703                 goto fail_inval;
704         }
705
706         sfc_adapter_lock(sa);
707
708         if (pdu != sa->port.pdu) {
709                 if (sa->state == SFC_ADAPTER_STARTED) {
710                         sfc_stop(sa);
711
712                         old_pdu = sa->port.pdu;
713                         sa->port.pdu = pdu;
714                         rc = sfc_start(sa);
715                         if (rc != 0)
716                                 goto fail_start;
717                 } else {
718                         sa->port.pdu = pdu;
719                 }
720         }
721
722         /*
723          * The driver does not use it, but other PMDs update jumbo_frame
724          * flag and max_rx_pkt_len when MTU is set.
725          */
726         dev->data->dev_conf.rxmode.jumbo_frame = (mtu > ETHER_MAX_LEN);
727         dev->data->dev_conf.rxmode.max_rx_pkt_len = sa->port.pdu;
728
729         sfc_adapter_unlock(sa);
730
731         sfc_log_init(sa, "done");
732         return 0;
733
734 fail_start:
735         sa->port.pdu = old_pdu;
736         if (sfc_start(sa) != 0)
737                 sfc_err(sa, "cannot start with neither new (%u) nor old (%u) "
738                         "PDU max size - port is stopped",
739                         (unsigned int)pdu, (unsigned int)old_pdu);
740         sfc_adapter_unlock(sa);
741
742 fail_inval:
743         sfc_log_init(sa, "failed %d", rc);
744         SFC_ASSERT(rc > 0);
745         return -rc;
746 }
747 static void
748 sfc_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
749 {
750         struct sfc_adapter *sa = dev->data->dev_private;
751         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
752         int rc;
753
754         sfc_adapter_lock(sa);
755
756         if (sa->state != SFC_ADAPTER_STARTED) {
757                 sfc_info(sa, "the port is not started");
758                 sfc_info(sa, "the new MAC address will be set on port start");
759
760                 goto unlock;
761         }
762
763         if (encp->enc_allow_set_mac_with_installed_filters) {
764                 rc = efx_mac_addr_set(sa->nic, mac_addr->addr_bytes);
765                 if (rc != 0) {
766                         sfc_err(sa, "cannot set MAC address (rc = %u)", rc);
767                         goto unlock;
768                 }
769
770                 /*
771                  * Changing the MAC address by means of MCDI request
772                  * has no effect on received traffic, therefore
773                  * we also need to update unicast filters
774                  */
775                 rc = sfc_set_rx_mode(sa);
776                 if (rc != 0)
777                         sfc_err(sa, "cannot set filter (rc = %u)", rc);
778         } else {
779                 sfc_warn(sa, "cannot set MAC address with filters installed");
780                 sfc_warn(sa, "adapter will be restarted to pick the new MAC");
781                 sfc_warn(sa, "(some traffic may be dropped)");
782
783                 /*
784                  * Since setting MAC address with filters installed is not
785                  * allowed on the adapter, one needs to simply restart adapter
786                  * so that the new MAC address will be taken from an outer
787                  * storage and set flawlessly by means of sfc_start() call
788                  */
789                 sfc_stop(sa);
790                 rc = sfc_start(sa);
791                 if (rc != 0)
792                         sfc_err(sa, "cannot restart adapter (rc = %u)", rc);
793         }
794
795 unlock:
796         sfc_adapter_unlock(sa);
797 }
798
799
800 static int
801 sfc_set_mc_addr_list(struct rte_eth_dev *dev, struct ether_addr *mc_addr_set,
802                      uint32_t nb_mc_addr)
803 {
804         struct sfc_adapter *sa = dev->data->dev_private;
805         uint8_t *mc_addrs_p;
806         uint8_t *mc_addrs;
807         int rc;
808         unsigned int i;
809
810         if (nb_mc_addr > EFX_MAC_MULTICAST_LIST_MAX) {
811                 sfc_err(sa, "too many multicast addresses: %u > %u",
812                          nb_mc_addr, EFX_MAC_MULTICAST_LIST_MAX);
813                 return -EINVAL;
814         }
815
816         mc_addrs_p = rte_calloc("mc-addrs", nb_mc_addr, EFX_MAC_ADDR_LEN, 0);
817         if (mc_addrs_p == NULL)
818                 return -ENOMEM;
819
820         mc_addrs = mc_addrs_p;
821
822         for (i = 0; i < nb_mc_addr; ++i) {
823                 (void)rte_memcpy(mc_addrs, mc_addr_set[i].addr_bytes,
824                                  EFX_MAC_ADDR_LEN);
825                 mc_addrs += EFX_MAC_ADDR_LEN;
826         }
827
828         rc = efx_mac_multicast_list_set(sa->nic, mc_addrs_p, nb_mc_addr);
829
830         rte_free(mc_addrs_p);
831
832         if (rc != 0)
833                 sfc_err(sa, "cannot set multicast address list (rc = %u)", rc);
834
835         SFC_ASSERT(rc > 0);
836         return -rc;
837 }
838
839 static void
840 sfc_rx_queue_info_get(struct rte_eth_dev *dev, uint16_t rx_queue_id,
841                       struct rte_eth_rxq_info *qinfo)
842 {
843         struct sfc_adapter *sa = dev->data->dev_private;
844         struct sfc_rxq_info *rxq_info;
845         struct sfc_rxq *rxq;
846
847         sfc_adapter_lock(sa);
848
849         SFC_ASSERT(rx_queue_id < sa->rxq_count);
850
851         rxq_info = &sa->rxq_info[rx_queue_id];
852         rxq = rxq_info->rxq;
853         SFC_ASSERT(rxq != NULL);
854
855         qinfo->mp = rxq->refill_mb_pool;
856         qinfo->conf.rx_free_thresh = rxq->refill_threshold;
857         qinfo->conf.rx_drop_en = 1;
858         qinfo->conf.rx_deferred_start = rxq_info->deferred_start;
859         qinfo->scattered_rx = (rxq_info->type == EFX_RXQ_TYPE_SCATTER);
860         qinfo->nb_desc = rxq_info->entries;
861
862         sfc_adapter_unlock(sa);
863 }
864
865 static void
866 sfc_tx_queue_info_get(struct rte_eth_dev *dev, uint16_t tx_queue_id,
867                       struct rte_eth_txq_info *qinfo)
868 {
869         struct sfc_adapter *sa = dev->data->dev_private;
870         struct sfc_txq_info *txq_info;
871
872         sfc_adapter_lock(sa);
873
874         SFC_ASSERT(tx_queue_id < sa->txq_count);
875
876         txq_info = &sa->txq_info[tx_queue_id];
877         SFC_ASSERT(txq_info->txq != NULL);
878
879         memset(qinfo, 0, sizeof(*qinfo));
880
881         qinfo->conf.txq_flags = txq_info->txq->flags;
882         qinfo->conf.tx_free_thresh = txq_info->txq->free_thresh;
883         qinfo->conf.tx_deferred_start = txq_info->deferred_start;
884         qinfo->nb_desc = txq_info->entries;
885
886         sfc_adapter_unlock(sa);
887 }
888
889 static uint32_t
890 sfc_rx_queue_count(struct rte_eth_dev *dev, uint16_t rx_queue_id)
891 {
892         struct sfc_adapter *sa = dev->data->dev_private;
893
894         sfc_log_init(sa, "RxQ=%u", rx_queue_id);
895
896         return sfc_rx_qdesc_npending(sa, rx_queue_id);
897 }
898
899 static int
900 sfc_rx_descriptor_done(void *queue, uint16_t offset)
901 {
902         struct sfc_rxq *rxq = queue;
903
904         return sfc_rx_qdesc_done(rxq, offset);
905 }
906
907 static int
908 sfc_rx_queue_start(struct rte_eth_dev *dev, uint16_t rx_queue_id)
909 {
910         struct sfc_adapter *sa = dev->data->dev_private;
911         int rc;
912
913         sfc_log_init(sa, "RxQ=%u", rx_queue_id);
914
915         sfc_adapter_lock(sa);
916
917         rc = EINVAL;
918         if (sa->state != SFC_ADAPTER_STARTED)
919                 goto fail_not_started;
920
921         rc = sfc_rx_qstart(sa, rx_queue_id);
922         if (rc != 0)
923                 goto fail_rx_qstart;
924
925         sa->rxq_info[rx_queue_id].deferred_started = B_TRUE;
926
927         sfc_adapter_unlock(sa);
928
929         return 0;
930
931 fail_rx_qstart:
932 fail_not_started:
933         sfc_adapter_unlock(sa);
934         SFC_ASSERT(rc > 0);
935         return -rc;
936 }
937
938 static int
939 sfc_rx_queue_stop(struct rte_eth_dev *dev, uint16_t rx_queue_id)
940 {
941         struct sfc_adapter *sa = dev->data->dev_private;
942
943         sfc_log_init(sa, "RxQ=%u", rx_queue_id);
944
945         sfc_adapter_lock(sa);
946         sfc_rx_qstop(sa, rx_queue_id);
947
948         sa->rxq_info[rx_queue_id].deferred_started = B_FALSE;
949
950         sfc_adapter_unlock(sa);
951
952         return 0;
953 }
954
955 static int
956 sfc_tx_queue_start(struct rte_eth_dev *dev, uint16_t tx_queue_id)
957 {
958         struct sfc_adapter *sa = dev->data->dev_private;
959         int rc;
960
961         sfc_log_init(sa, "TxQ = %u", tx_queue_id);
962
963         sfc_adapter_lock(sa);
964
965         rc = EINVAL;
966         if (sa->state != SFC_ADAPTER_STARTED)
967                 goto fail_not_started;
968
969         rc = sfc_tx_qstart(sa, tx_queue_id);
970         if (rc != 0)
971                 goto fail_tx_qstart;
972
973         sa->txq_info[tx_queue_id].deferred_started = B_TRUE;
974
975         sfc_adapter_unlock(sa);
976         return 0;
977
978 fail_tx_qstart:
979
980 fail_not_started:
981         sfc_adapter_unlock(sa);
982         SFC_ASSERT(rc > 0);
983         return -rc;
984 }
985
986 static int
987 sfc_tx_queue_stop(struct rte_eth_dev *dev, uint16_t tx_queue_id)
988 {
989         struct sfc_adapter *sa = dev->data->dev_private;
990
991         sfc_log_init(sa, "TxQ = %u", tx_queue_id);
992
993         sfc_adapter_lock(sa);
994
995         sfc_tx_qstop(sa, tx_queue_id);
996
997         sa->txq_info[tx_queue_id].deferred_started = B_FALSE;
998
999         sfc_adapter_unlock(sa);
1000         return 0;
1001 }
1002
1003 #if EFSYS_OPT_RX_SCALE
1004 static int
1005 sfc_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
1006                           struct rte_eth_rss_conf *rss_conf)
1007 {
1008         struct sfc_adapter *sa = dev->data->dev_private;
1009
1010         if ((sa->rss_channels == 1) ||
1011             (sa->rss_support != EFX_RX_SCALE_EXCLUSIVE))
1012                 return -ENOTSUP;
1013
1014         sfc_adapter_lock(sa);
1015
1016         /*
1017          * Mapping of hash configuration between RTE and EFX is not one-to-one,
1018          * hence, conversion is done here to derive a correct set of ETH_RSS
1019          * flags which corresponds to the active EFX configuration stored
1020          * locally in 'sfc_adapter' and kept up-to-date
1021          */
1022         rss_conf->rss_hf = sfc_efx_to_rte_hash_type(sa->rss_hash_types);
1023         rss_conf->rss_key_len = SFC_RSS_KEY_SIZE;
1024         if (rss_conf->rss_key != NULL)
1025                 rte_memcpy(rss_conf->rss_key, sa->rss_key, SFC_RSS_KEY_SIZE);
1026
1027         sfc_adapter_unlock(sa);
1028
1029         return 0;
1030 }
1031
1032 static int
1033 sfc_dev_rss_hash_update(struct rte_eth_dev *dev,
1034                         struct rte_eth_rss_conf *rss_conf)
1035 {
1036         struct sfc_adapter *sa = dev->data->dev_private;
1037         unsigned int efx_hash_types;
1038         int rc = 0;
1039
1040         if ((sa->rss_channels == 1) ||
1041             (sa->rss_support != EFX_RX_SCALE_EXCLUSIVE)) {
1042                 sfc_err(sa, "RSS is not available");
1043                 return -ENOTSUP;
1044         }
1045
1046         if ((rss_conf->rss_key != NULL) &&
1047             (rss_conf->rss_key_len != sizeof(sa->rss_key))) {
1048                 sfc_err(sa, "RSS key size is wrong (should be %lu)",
1049                         sizeof(sa->rss_key));
1050                 return -EINVAL;
1051         }
1052
1053         if ((rss_conf->rss_hf & ~SFC_RSS_OFFLOADS) != 0) {
1054                 sfc_err(sa, "unsupported hash functions requested");
1055                 return -EINVAL;
1056         }
1057
1058         sfc_adapter_lock(sa);
1059
1060         efx_hash_types = sfc_rte_to_efx_hash_type(rss_conf->rss_hf);
1061
1062         rc = efx_rx_scale_mode_set(sa->nic, EFX_RX_HASHALG_TOEPLITZ,
1063                                    efx_hash_types, B_TRUE);
1064         if (rc != 0)
1065                 goto fail_scale_mode_set;
1066
1067         if (rss_conf->rss_key != NULL) {
1068                 if (sa->state == SFC_ADAPTER_STARTED) {
1069                         rc = efx_rx_scale_key_set(sa->nic, rss_conf->rss_key,
1070                                                   sizeof(sa->rss_key));
1071                         if (rc != 0)
1072                                 goto fail_scale_key_set;
1073                 }
1074
1075                 rte_memcpy(sa->rss_key, rss_conf->rss_key, sizeof(sa->rss_key));
1076         }
1077
1078         sa->rss_hash_types = efx_hash_types;
1079
1080         sfc_adapter_unlock(sa);
1081
1082         return 0;
1083
1084 fail_scale_key_set:
1085         if (efx_rx_scale_mode_set(sa->nic, EFX_RX_HASHALG_TOEPLITZ,
1086                                   sa->rss_hash_types, B_TRUE) != 0)
1087                 sfc_err(sa, "failed to restore RSS mode");
1088
1089 fail_scale_mode_set:
1090         sfc_adapter_unlock(sa);
1091         return -rc;
1092 }
1093
1094 static int
1095 sfc_dev_rss_reta_query(struct rte_eth_dev *dev,
1096                        struct rte_eth_rss_reta_entry64 *reta_conf,
1097                        uint16_t reta_size)
1098 {
1099         struct sfc_adapter *sa = dev->data->dev_private;
1100         int entry;
1101
1102         if ((sa->rss_channels == 1) ||
1103             (sa->rss_support != EFX_RX_SCALE_EXCLUSIVE))
1104                 return -ENOTSUP;
1105
1106         if (reta_size != EFX_RSS_TBL_SIZE)
1107                 return -EINVAL;
1108
1109         sfc_adapter_lock(sa);
1110
1111         for (entry = 0; entry < reta_size; entry++) {
1112                 int grp = entry / RTE_RETA_GROUP_SIZE;
1113                 int grp_idx = entry % RTE_RETA_GROUP_SIZE;
1114
1115                 if ((reta_conf[grp].mask >> grp_idx) & 1)
1116                         reta_conf[grp].reta[grp_idx] = sa->rss_tbl[entry];
1117         }
1118
1119         sfc_adapter_unlock(sa);
1120
1121         return 0;
1122 }
1123
1124 static int
1125 sfc_dev_rss_reta_update(struct rte_eth_dev *dev,
1126                         struct rte_eth_rss_reta_entry64 *reta_conf,
1127                         uint16_t reta_size)
1128 {
1129         struct sfc_adapter *sa = dev->data->dev_private;
1130         unsigned int *rss_tbl_new;
1131         uint16_t entry;
1132         int rc;
1133
1134
1135         if ((sa->rss_channels == 1) ||
1136             (sa->rss_support != EFX_RX_SCALE_EXCLUSIVE)) {
1137                 sfc_err(sa, "RSS is not available");
1138                 return -ENOTSUP;
1139         }
1140
1141         if (reta_size != EFX_RSS_TBL_SIZE) {
1142                 sfc_err(sa, "RETA size is wrong (should be %u)",
1143                         EFX_RSS_TBL_SIZE);
1144                 return -EINVAL;
1145         }
1146
1147         rss_tbl_new = rte_zmalloc("rss_tbl_new", sizeof(sa->rss_tbl), 0);
1148         if (rss_tbl_new == NULL)
1149                 return -ENOMEM;
1150
1151         sfc_adapter_lock(sa);
1152
1153         rte_memcpy(rss_tbl_new, sa->rss_tbl, sizeof(sa->rss_tbl));
1154
1155         for (entry = 0; entry < reta_size; entry++) {
1156                 int grp_idx = entry % RTE_RETA_GROUP_SIZE;
1157                 struct rte_eth_rss_reta_entry64 *grp;
1158
1159                 grp = &reta_conf[entry / RTE_RETA_GROUP_SIZE];
1160
1161                 if (grp->mask & (1ull << grp_idx)) {
1162                         if (grp->reta[grp_idx] >= sa->rss_channels) {
1163                                 rc = EINVAL;
1164                                 goto bad_reta_entry;
1165                         }
1166                         rss_tbl_new[entry] = grp->reta[grp_idx];
1167                 }
1168         }
1169
1170         rc = efx_rx_scale_tbl_set(sa->nic, rss_tbl_new, EFX_RSS_TBL_SIZE);
1171         if (rc == 0)
1172                 rte_memcpy(sa->rss_tbl, rss_tbl_new, sizeof(sa->rss_tbl));
1173
1174 bad_reta_entry:
1175         sfc_adapter_unlock(sa);
1176
1177         rte_free(rss_tbl_new);
1178
1179         SFC_ASSERT(rc >= 0);
1180         return -rc;
1181 }
1182 #endif
1183
1184 static const struct eth_dev_ops sfc_eth_dev_ops = {
1185         .dev_configure                  = sfc_dev_configure,
1186         .dev_start                      = sfc_dev_start,
1187         .dev_stop                       = sfc_dev_stop,
1188         .dev_set_link_up                = sfc_dev_set_link_up,
1189         .dev_set_link_down              = sfc_dev_set_link_down,
1190         .dev_close                      = sfc_dev_close,
1191         .promiscuous_enable             = sfc_dev_promisc_enable,
1192         .promiscuous_disable            = sfc_dev_promisc_disable,
1193         .allmulticast_enable            = sfc_dev_allmulti_enable,
1194         .allmulticast_disable           = sfc_dev_allmulti_disable,
1195         .link_update                    = sfc_dev_link_update,
1196         .stats_get                      = sfc_stats_get,
1197         .xstats_get                     = sfc_xstats_get,
1198         .xstats_get_names               = sfc_xstats_get_names,
1199         .dev_infos_get                  = sfc_dev_infos_get,
1200         .dev_supported_ptypes_get       = sfc_dev_supported_ptypes_get,
1201         .mtu_set                        = sfc_dev_set_mtu,
1202         .rx_queue_start                 = sfc_rx_queue_start,
1203         .rx_queue_stop                  = sfc_rx_queue_stop,
1204         .tx_queue_start                 = sfc_tx_queue_start,
1205         .tx_queue_stop                  = sfc_tx_queue_stop,
1206         .rx_queue_setup                 = sfc_rx_queue_setup,
1207         .rx_queue_release               = sfc_rx_queue_release,
1208         .rx_queue_count                 = sfc_rx_queue_count,
1209         .rx_descriptor_done             = sfc_rx_descriptor_done,
1210         .tx_queue_setup                 = sfc_tx_queue_setup,
1211         .tx_queue_release               = sfc_tx_queue_release,
1212         .flow_ctrl_get                  = sfc_flow_ctrl_get,
1213         .flow_ctrl_set                  = sfc_flow_ctrl_set,
1214         .mac_addr_set                   = sfc_mac_addr_set,
1215 #if EFSYS_OPT_RX_SCALE
1216         .reta_update                    = sfc_dev_rss_reta_update,
1217         .reta_query                     = sfc_dev_rss_reta_query,
1218         .rss_hash_update                = sfc_dev_rss_hash_update,
1219         .rss_hash_conf_get              = sfc_dev_rss_hash_conf_get,
1220 #endif
1221         .set_mc_addr_list               = sfc_set_mc_addr_list,
1222         .rxq_info_get                   = sfc_rx_queue_info_get,
1223         .txq_info_get                   = sfc_tx_queue_info_get,
1224 };
1225
1226 static int
1227 sfc_eth_dev_init(struct rte_eth_dev *dev)
1228 {
1229         struct sfc_adapter *sa = dev->data->dev_private;
1230         struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(dev);
1231         int rc;
1232         const efx_nic_cfg_t *encp;
1233         const struct ether_addr *from;
1234
1235         /* Required for logging */
1236         sa->eth_dev = dev;
1237
1238         /* Copy PCI device info to the dev->data */
1239         rte_eth_copy_pci_info(dev, pci_dev);
1240
1241         rc = sfc_kvargs_parse(sa);
1242         if (rc != 0)
1243                 goto fail_kvargs_parse;
1244
1245         rc = sfc_kvargs_process(sa, SFC_KVARG_DEBUG_INIT,
1246                                 sfc_kvarg_bool_handler, &sa->debug_init);
1247         if (rc != 0)
1248                 goto fail_kvarg_debug_init;
1249
1250         sfc_log_init(sa, "entry");
1251
1252         dev->data->mac_addrs = rte_zmalloc("sfc", ETHER_ADDR_LEN, 0);
1253         if (dev->data->mac_addrs == NULL) {
1254                 rc = ENOMEM;
1255                 goto fail_mac_addrs;
1256         }
1257
1258         sfc_adapter_lock_init(sa);
1259         sfc_adapter_lock(sa);
1260
1261         sfc_log_init(sa, "attaching");
1262         rc = sfc_attach(sa);
1263         if (rc != 0)
1264                 goto fail_attach;
1265
1266         encp = efx_nic_cfg_get(sa->nic);
1267
1268         /*
1269          * The arguments are really reverse order in comparison to
1270          * Linux kernel. Copy from NIC config to Ethernet device data.
1271          */
1272         from = (const struct ether_addr *)(encp->enc_mac_addr);
1273         ether_addr_copy(from, &dev->data->mac_addrs[0]);
1274
1275         dev->dev_ops = &sfc_eth_dev_ops;
1276         dev->rx_pkt_burst = &sfc_recv_pkts;
1277         dev->tx_pkt_burst = &sfc_xmit_pkts;
1278
1279         sfc_adapter_unlock(sa);
1280
1281         sfc_log_init(sa, "done");
1282         return 0;
1283
1284 fail_attach:
1285         sfc_adapter_unlock(sa);
1286         sfc_adapter_lock_fini(sa);
1287         rte_free(dev->data->mac_addrs);
1288         dev->data->mac_addrs = NULL;
1289
1290 fail_mac_addrs:
1291 fail_kvarg_debug_init:
1292         sfc_kvargs_cleanup(sa);
1293
1294 fail_kvargs_parse:
1295         sfc_log_init(sa, "failed %d", rc);
1296         SFC_ASSERT(rc > 0);
1297         return -rc;
1298 }
1299
1300 static int
1301 sfc_eth_dev_uninit(struct rte_eth_dev *dev)
1302 {
1303         struct sfc_adapter *sa = dev->data->dev_private;
1304
1305         sfc_log_init(sa, "entry");
1306
1307         sfc_adapter_lock(sa);
1308
1309         sfc_detach(sa);
1310
1311         rte_free(dev->data->mac_addrs);
1312         dev->data->mac_addrs = NULL;
1313
1314         dev->dev_ops = NULL;
1315         dev->rx_pkt_burst = NULL;
1316         dev->tx_pkt_burst = NULL;
1317
1318         sfc_kvargs_cleanup(sa);
1319
1320         sfc_adapter_unlock(sa);
1321         sfc_adapter_lock_fini(sa);
1322
1323         sfc_log_init(sa, "done");
1324
1325         /* Required for logging, so cleanup last */
1326         sa->eth_dev = NULL;
1327         return 0;
1328 }
1329
1330 static const struct rte_pci_id pci_id_sfc_efx_map[] = {
1331         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_FARMINGDALE) },
1332         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_GREENPORT) },
1333         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_MEDFORD) },
1334         { .vendor_id = 0 /* sentinel */ }
1335 };
1336
1337 static struct eth_driver sfc_efx_pmd = {
1338         .pci_drv = {
1339                 .id_table = pci_id_sfc_efx_map,
1340                 .drv_flags =
1341                         RTE_PCI_DRV_INTR_LSC |
1342                         RTE_PCI_DRV_NEED_MAPPING,
1343                 .probe = rte_eth_dev_pci_probe,
1344                 .remove = rte_eth_dev_pci_remove,
1345         },
1346         .eth_dev_init = sfc_eth_dev_init,
1347         .eth_dev_uninit = sfc_eth_dev_uninit,
1348         .dev_private_size = sizeof(struct sfc_adapter),
1349 };
1350
1351 RTE_PMD_REGISTER_PCI(net_sfc_efx, sfc_efx_pmd.pci_drv);
1352 RTE_PMD_REGISTER_PCI_TABLE(net_sfc_efx, pci_id_sfc_efx_map);
1353 RTE_PMD_REGISTER_KMOD_DEP(net_sfc_efx, "* igb_uio | uio_pci_generic | vfio");
1354 RTE_PMD_REGISTER_PARAM_STRING(net_sfc_efx,
1355         SFC_KVARG_PERF_PROFILE "=" SFC_KVARG_VALUES_PERF_PROFILE " "
1356         SFC_KVARG_MCDI_LOGGING "=" SFC_KVARG_VALUES_BOOL " "
1357         SFC_KVARG_DEBUG_INIT "=" SFC_KVARG_VALUES_BOOL);