net/sfc: support multicast addresses list controls
[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
50         sfc_log_init(sa, "entry");
51
52         dev_info->pci_dev = RTE_DEV_TO_PCI(dev->device);
53         dev_info->max_rx_pktlen = EFX_MAC_PDU_MAX;
54
55         /* Autonegotiation may be disabled */
56         dev_info->speed_capa = ETH_LINK_SPEED_FIXED;
57         if (sa->port.phy_adv_cap_mask & EFX_PHY_CAP_1000FDX)
58                 dev_info->speed_capa |= ETH_LINK_SPEED_1G;
59         if (sa->port.phy_adv_cap_mask & EFX_PHY_CAP_10000FDX)
60                 dev_info->speed_capa |= ETH_LINK_SPEED_10G;
61         if (sa->port.phy_adv_cap_mask & EFX_PHY_CAP_40000FDX)
62                 dev_info->speed_capa |= ETH_LINK_SPEED_40G;
63
64         dev_info->max_rx_queues = sa->rxq_max;
65         dev_info->max_tx_queues = sa->txq_max;
66
67         /* By default packets are dropped if no descriptors are available */
68         dev_info->default_rxconf.rx_drop_en = 1;
69
70         dev_info->tx_offload_capa =
71                 DEV_TX_OFFLOAD_IPV4_CKSUM |
72                 DEV_TX_OFFLOAD_UDP_CKSUM |
73                 DEV_TX_OFFLOAD_TCP_CKSUM;
74
75         dev_info->default_txconf.txq_flags = ETH_TXQ_FLAGS_NOVLANOFFL |
76                                              ETH_TXQ_FLAGS_NOXSUMSCTP;
77
78         dev_info->rx_desc_lim.nb_max = EFX_RXQ_MAXNDESCS;
79         dev_info->rx_desc_lim.nb_min = EFX_RXQ_MINNDESCS;
80         /* The RXQ hardware requires that the descriptor count is a power
81          * of 2, but rx_desc_lim cannot properly describe that constraint.
82          */
83         dev_info->rx_desc_lim.nb_align = EFX_RXQ_MINNDESCS;
84
85         dev_info->tx_desc_lim.nb_max = sa->txq_max_entries;
86         dev_info->tx_desc_lim.nb_min = EFX_TXQ_MINNDESCS;
87         /*
88          * The TXQ hardware requires that the descriptor count is a power
89          * of 2, but tx_desc_lim cannot properly describe that constraint
90          */
91         dev_info->tx_desc_lim.nb_align = EFX_TXQ_MINNDESCS;
92 }
93
94 static int
95 sfc_dev_configure(struct rte_eth_dev *dev)
96 {
97         struct rte_eth_dev_data *dev_data = dev->data;
98         struct sfc_adapter *sa = dev_data->dev_private;
99         int rc;
100
101         sfc_log_init(sa, "entry n_rxq=%u n_txq=%u",
102                      dev_data->nb_rx_queues, dev_data->nb_tx_queues);
103
104         sfc_adapter_lock(sa);
105         switch (sa->state) {
106         case SFC_ADAPTER_CONFIGURED:
107                 sfc_close(sa);
108                 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
109                 /* FALLTHROUGH */
110         case SFC_ADAPTER_INITIALIZED:
111                 rc = sfc_configure(sa);
112                 break;
113         default:
114                 sfc_err(sa, "unexpected adapter state %u to configure",
115                         sa->state);
116                 rc = EINVAL;
117                 break;
118         }
119         sfc_adapter_unlock(sa);
120
121         sfc_log_init(sa, "done %d", rc);
122         SFC_ASSERT(rc >= 0);
123         return -rc;
124 }
125
126 static int
127 sfc_dev_start(struct rte_eth_dev *dev)
128 {
129         struct sfc_adapter *sa = dev->data->dev_private;
130         int rc;
131
132         sfc_log_init(sa, "entry");
133
134         sfc_adapter_lock(sa);
135         rc = sfc_start(sa);
136         sfc_adapter_unlock(sa);
137
138         sfc_log_init(sa, "done %d", rc);
139         SFC_ASSERT(rc >= 0);
140         return -rc;
141 }
142
143 static int
144 sfc_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
145 {
146         struct sfc_adapter *sa = dev->data->dev_private;
147         struct rte_eth_link *dev_link = &dev->data->dev_link;
148         struct rte_eth_link old_link;
149         struct rte_eth_link current_link;
150
151         sfc_log_init(sa, "entry");
152
153         if (sa->state != SFC_ADAPTER_STARTED)
154                 return 0;
155
156 retry:
157         EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
158         *(int64_t *)&old_link = rte_atomic64_read((rte_atomic64_t *)dev_link);
159
160         if (wait_to_complete) {
161                 efx_link_mode_t link_mode;
162
163                 efx_port_poll(sa->nic, &link_mode);
164                 sfc_port_link_mode_to_info(link_mode, &current_link);
165
166                 if (!rte_atomic64_cmpset((volatile uint64_t *)dev_link,
167                                          *(uint64_t *)&old_link,
168                                          *(uint64_t *)&current_link))
169                         goto retry;
170         } else {
171                 sfc_ev_mgmt_qpoll(sa);
172                 *(int64_t *)&current_link =
173                         rte_atomic64_read((rte_atomic64_t *)dev_link);
174         }
175
176         if (old_link.link_status != current_link.link_status)
177                 sfc_info(sa, "Link status is %s",
178                          current_link.link_status ? "UP" : "DOWN");
179
180         return old_link.link_status == current_link.link_status ? 0 : -1;
181 }
182
183 static void
184 sfc_dev_stop(struct rte_eth_dev *dev)
185 {
186         struct sfc_adapter *sa = dev->data->dev_private;
187
188         sfc_log_init(sa, "entry");
189
190         sfc_adapter_lock(sa);
191         sfc_stop(sa);
192         sfc_adapter_unlock(sa);
193
194         sfc_log_init(sa, "done");
195 }
196
197 static int
198 sfc_dev_set_link_up(struct rte_eth_dev *dev)
199 {
200         struct sfc_adapter *sa = dev->data->dev_private;
201         int rc;
202
203         sfc_log_init(sa, "entry");
204
205         sfc_adapter_lock(sa);
206         rc = sfc_start(sa);
207         sfc_adapter_unlock(sa);
208
209         SFC_ASSERT(rc >= 0);
210         return -rc;
211 }
212
213 static int
214 sfc_dev_set_link_down(struct rte_eth_dev *dev)
215 {
216         struct sfc_adapter *sa = dev->data->dev_private;
217
218         sfc_log_init(sa, "entry");
219
220         sfc_adapter_lock(sa);
221         sfc_stop(sa);
222         sfc_adapter_unlock(sa);
223
224         return 0;
225 }
226
227 static void
228 sfc_dev_close(struct rte_eth_dev *dev)
229 {
230         struct sfc_adapter *sa = dev->data->dev_private;
231
232         sfc_log_init(sa, "entry");
233
234         sfc_adapter_lock(sa);
235         switch (sa->state) {
236         case SFC_ADAPTER_STARTED:
237                 sfc_stop(sa);
238                 SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
239                 /* FALLTHROUGH */
240         case SFC_ADAPTER_CONFIGURED:
241                 sfc_close(sa);
242                 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
243                 /* FALLTHROUGH */
244         case SFC_ADAPTER_INITIALIZED:
245                 break;
246         default:
247                 sfc_err(sa, "unexpected adapter state %u on close", sa->state);
248                 break;
249         }
250         sfc_adapter_unlock(sa);
251
252         sfc_log_init(sa, "done");
253 }
254
255 static void
256 sfc_dev_filter_set(struct rte_eth_dev *dev, enum sfc_dev_filter_mode mode,
257                    boolean_t enabled)
258 {
259         struct sfc_port *port;
260         boolean_t *toggle;
261         struct sfc_adapter *sa = dev->data->dev_private;
262         boolean_t allmulti = (mode == SFC_DEV_FILTER_MODE_ALLMULTI);
263         const char *desc = (allmulti) ? "all-multi" : "promiscuous";
264
265         sfc_adapter_lock(sa);
266
267         port = &sa->port;
268         toggle = (allmulti) ? (&port->allmulti) : (&port->promisc);
269
270         if (*toggle != enabled) {
271                 *toggle = enabled;
272
273                 if ((sa->state == SFC_ADAPTER_STARTED) &&
274                     (sfc_set_rx_mode(sa) != 0)) {
275                         *toggle = !(enabled);
276                         sfc_warn(sa, "Failed to %s %s mode",
277                                  ((enabled) ? "enable" : "disable"), desc);
278                 }
279         }
280
281         sfc_adapter_unlock(sa);
282 }
283
284 static void
285 sfc_dev_promisc_enable(struct rte_eth_dev *dev)
286 {
287         sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_PROMISC, B_TRUE);
288 }
289
290 static void
291 sfc_dev_promisc_disable(struct rte_eth_dev *dev)
292 {
293         sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_PROMISC, B_FALSE);
294 }
295
296 static void
297 sfc_dev_allmulti_enable(struct rte_eth_dev *dev)
298 {
299         sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_ALLMULTI, B_TRUE);
300 }
301
302 static void
303 sfc_dev_allmulti_disable(struct rte_eth_dev *dev)
304 {
305         sfc_dev_filter_set(dev, SFC_DEV_FILTER_MODE_ALLMULTI, B_FALSE);
306 }
307
308 static int
309 sfc_rx_queue_setup(struct rte_eth_dev *dev, uint16_t rx_queue_id,
310                    uint16_t nb_rx_desc, unsigned int socket_id,
311                    const struct rte_eth_rxconf *rx_conf,
312                    struct rte_mempool *mb_pool)
313 {
314         struct sfc_adapter *sa = dev->data->dev_private;
315         int rc;
316
317         sfc_log_init(sa, "RxQ=%u nb_rx_desc=%u socket_id=%u",
318                      rx_queue_id, nb_rx_desc, socket_id);
319
320         sfc_adapter_lock(sa);
321
322         rc = sfc_rx_qinit(sa, rx_queue_id, nb_rx_desc, socket_id,
323                           rx_conf, mb_pool);
324         if (rc != 0)
325                 goto fail_rx_qinit;
326
327         dev->data->rx_queues[rx_queue_id] = sa->rxq_info[rx_queue_id].rxq;
328
329         sfc_adapter_unlock(sa);
330
331         return 0;
332
333 fail_rx_qinit:
334         sfc_adapter_unlock(sa);
335         SFC_ASSERT(rc > 0);
336         return -rc;
337 }
338
339 static void
340 sfc_rx_queue_release(void *queue)
341 {
342         struct sfc_rxq *rxq = queue;
343         struct sfc_adapter *sa;
344         unsigned int sw_index;
345
346         if (rxq == NULL)
347                 return;
348
349         sa = rxq->evq->sa;
350         sfc_adapter_lock(sa);
351
352         sw_index = sfc_rxq_sw_index(rxq);
353
354         sfc_log_init(sa, "RxQ=%u", sw_index);
355
356         sa->eth_dev->data->rx_queues[sw_index] = NULL;
357
358         sfc_rx_qfini(sa, sw_index);
359
360         sfc_adapter_unlock(sa);
361 }
362
363 static int
364 sfc_tx_queue_setup(struct rte_eth_dev *dev, uint16_t tx_queue_id,
365                    uint16_t nb_tx_desc, unsigned int socket_id,
366                    const struct rte_eth_txconf *tx_conf)
367 {
368         struct sfc_adapter *sa = dev->data->dev_private;
369         int rc;
370
371         sfc_log_init(sa, "TxQ = %u, nb_tx_desc = %u, socket_id = %u",
372                      tx_queue_id, nb_tx_desc, socket_id);
373
374         sfc_adapter_lock(sa);
375
376         rc = sfc_tx_qinit(sa, tx_queue_id, nb_tx_desc, socket_id, tx_conf);
377         if (rc != 0)
378                 goto fail_tx_qinit;
379
380         dev->data->tx_queues[tx_queue_id] = sa->txq_info[tx_queue_id].txq;
381
382         sfc_adapter_unlock(sa);
383         return 0;
384
385 fail_tx_qinit:
386         sfc_adapter_unlock(sa);
387         SFC_ASSERT(rc > 0);
388         return -rc;
389 }
390
391 static void
392 sfc_tx_queue_release(void *queue)
393 {
394         struct sfc_txq *txq = queue;
395         unsigned int sw_index;
396         struct sfc_adapter *sa;
397
398         if (txq == NULL)
399                 return;
400
401         sw_index = sfc_txq_sw_index(txq);
402
403         SFC_ASSERT(txq->evq != NULL);
404         sa = txq->evq->sa;
405
406         sfc_log_init(sa, "TxQ = %u", sw_index);
407
408         sfc_adapter_lock(sa);
409
410         SFC_ASSERT(sw_index < sa->eth_dev->data->nb_tx_queues);
411         sa->eth_dev->data->tx_queues[sw_index] = NULL;
412
413         sfc_tx_qfini(sa, sw_index);
414
415         sfc_adapter_unlock(sa);
416 }
417
418 static void
419 sfc_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
420 {
421         struct sfc_adapter *sa = dev->data->dev_private;
422         struct sfc_port *port = &sa->port;
423         uint64_t *mac_stats;
424
425         rte_spinlock_lock(&port->mac_stats_lock);
426
427         if (sfc_port_update_mac_stats(sa) != 0)
428                 goto unlock;
429
430         mac_stats = port->mac_stats_buf;
431
432         if (EFX_MAC_STAT_SUPPORTED(port->mac_stats_mask,
433                                    EFX_MAC_VADAPTER_RX_UNICAST_PACKETS)) {
434                 stats->ipackets =
435                         mac_stats[EFX_MAC_VADAPTER_RX_UNICAST_PACKETS] +
436                         mac_stats[EFX_MAC_VADAPTER_RX_MULTICAST_PACKETS] +
437                         mac_stats[EFX_MAC_VADAPTER_RX_BROADCAST_PACKETS];
438                 stats->opackets =
439                         mac_stats[EFX_MAC_VADAPTER_TX_UNICAST_PACKETS] +
440                         mac_stats[EFX_MAC_VADAPTER_TX_MULTICAST_PACKETS] +
441                         mac_stats[EFX_MAC_VADAPTER_TX_BROADCAST_PACKETS];
442                 stats->ibytes =
443                         mac_stats[EFX_MAC_VADAPTER_RX_UNICAST_BYTES] +
444                         mac_stats[EFX_MAC_VADAPTER_RX_MULTICAST_BYTES] +
445                         mac_stats[EFX_MAC_VADAPTER_RX_BROADCAST_BYTES];
446                 stats->obytes =
447                         mac_stats[EFX_MAC_VADAPTER_TX_UNICAST_BYTES] +
448                         mac_stats[EFX_MAC_VADAPTER_TX_MULTICAST_BYTES] +
449                         mac_stats[EFX_MAC_VADAPTER_TX_BROADCAST_BYTES];
450                 stats->imissed = mac_stats[EFX_MAC_VADAPTER_RX_OVERFLOW];
451                 stats->ierrors = mac_stats[EFX_MAC_VADAPTER_RX_BAD_PACKETS];
452                 stats->oerrors = mac_stats[EFX_MAC_VADAPTER_TX_BAD_PACKETS];
453         } else {
454                 stats->ipackets = mac_stats[EFX_MAC_RX_PKTS];
455                 stats->opackets = mac_stats[EFX_MAC_TX_PKTS];
456                 stats->ibytes = mac_stats[EFX_MAC_RX_OCTETS];
457                 stats->obytes = mac_stats[EFX_MAC_TX_OCTETS];
458                 /*
459                  * Take into account stats which are whenever supported
460                  * on EF10. If some stat is not supported by current
461                  * firmware variant or HW revision, it is guaranteed
462                  * to be zero in mac_stats.
463                  */
464                 stats->imissed =
465                         mac_stats[EFX_MAC_RX_NODESC_DROP_CNT] +
466                         mac_stats[EFX_MAC_PM_TRUNC_BB_OVERFLOW] +
467                         mac_stats[EFX_MAC_PM_DISCARD_BB_OVERFLOW] +
468                         mac_stats[EFX_MAC_PM_TRUNC_VFIFO_FULL] +
469                         mac_stats[EFX_MAC_PM_DISCARD_VFIFO_FULL] +
470                         mac_stats[EFX_MAC_PM_TRUNC_QBB] +
471                         mac_stats[EFX_MAC_PM_DISCARD_QBB] +
472                         mac_stats[EFX_MAC_PM_DISCARD_MAPPING] +
473                         mac_stats[EFX_MAC_RXDP_Q_DISABLED_PKTS] +
474                         mac_stats[EFX_MAC_RXDP_DI_DROPPED_PKTS];
475                 stats->ierrors =
476                         mac_stats[EFX_MAC_RX_FCS_ERRORS] +
477                         mac_stats[EFX_MAC_RX_ALIGN_ERRORS] +
478                         mac_stats[EFX_MAC_RX_JABBER_PKTS];
479                 /* no oerrors counters supported on EF10 */
480         }
481
482 unlock:
483         rte_spinlock_unlock(&port->mac_stats_lock);
484 }
485
486 static int
487 sfc_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
488                unsigned int xstats_count)
489 {
490         struct sfc_adapter *sa = dev->data->dev_private;
491         struct sfc_port *port = &sa->port;
492         uint64_t *mac_stats;
493         int rc;
494         unsigned int i;
495         int nstats = 0;
496
497         rte_spinlock_lock(&port->mac_stats_lock);
498
499         rc = sfc_port_update_mac_stats(sa);
500         if (rc != 0) {
501                 SFC_ASSERT(rc > 0);
502                 nstats = -rc;
503                 goto unlock;
504         }
505
506         mac_stats = port->mac_stats_buf;
507
508         for (i = 0; i < EFX_MAC_NSTATS; ++i) {
509                 if (EFX_MAC_STAT_SUPPORTED(port->mac_stats_mask, i)) {
510                         if (xstats != NULL && nstats < (int)xstats_count) {
511                                 xstats[nstats].id = nstats;
512                                 xstats[nstats].value = mac_stats[i];
513                         }
514                         nstats++;
515                 }
516         }
517
518 unlock:
519         rte_spinlock_unlock(&port->mac_stats_lock);
520
521         return nstats;
522 }
523
524 static int
525 sfc_xstats_get_names(struct rte_eth_dev *dev,
526                      struct rte_eth_xstat_name *xstats_names,
527                      unsigned int xstats_count)
528 {
529         struct sfc_adapter *sa = dev->data->dev_private;
530         struct sfc_port *port = &sa->port;
531         unsigned int i;
532         unsigned int nstats = 0;
533
534         for (i = 0; i < EFX_MAC_NSTATS; ++i) {
535                 if (EFX_MAC_STAT_SUPPORTED(port->mac_stats_mask, i)) {
536                         if (xstats_names != NULL && nstats < xstats_count)
537                                 strncpy(xstats_names[nstats].name,
538                                         efx_mac_stat_name(sa->nic, i),
539                                         sizeof(xstats_names[0].name));
540                         nstats++;
541                 }
542         }
543
544         return nstats;
545 }
546
547 static int
548 sfc_flow_ctrl_get(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
549 {
550         struct sfc_adapter *sa = dev->data->dev_private;
551         unsigned int wanted_fc, link_fc;
552
553         memset(fc_conf, 0, sizeof(*fc_conf));
554
555         sfc_adapter_lock(sa);
556
557         if (sa->state == SFC_ADAPTER_STARTED)
558                 efx_mac_fcntl_get(sa->nic, &wanted_fc, &link_fc);
559         else
560                 link_fc = sa->port.flow_ctrl;
561
562         switch (link_fc) {
563         case 0:
564                 fc_conf->mode = RTE_FC_NONE;
565                 break;
566         case EFX_FCNTL_RESPOND:
567                 fc_conf->mode = RTE_FC_RX_PAUSE;
568                 break;
569         case EFX_FCNTL_GENERATE:
570                 fc_conf->mode = RTE_FC_TX_PAUSE;
571                 break;
572         case (EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE):
573                 fc_conf->mode = RTE_FC_FULL;
574                 break;
575         default:
576                 sfc_err(sa, "%s: unexpected flow control value %#x",
577                         __func__, link_fc);
578         }
579
580         fc_conf->autoneg = sa->port.flow_ctrl_autoneg;
581
582         sfc_adapter_unlock(sa);
583
584         return 0;
585 }
586
587 static int
588 sfc_flow_ctrl_set(struct rte_eth_dev *dev, struct rte_eth_fc_conf *fc_conf)
589 {
590         struct sfc_adapter *sa = dev->data->dev_private;
591         struct sfc_port *port = &sa->port;
592         unsigned int fcntl;
593         int rc;
594
595         if (fc_conf->high_water != 0 || fc_conf->low_water != 0 ||
596             fc_conf->pause_time != 0 || fc_conf->send_xon != 0 ||
597             fc_conf->mac_ctrl_frame_fwd != 0) {
598                 sfc_err(sa, "unsupported flow control settings specified");
599                 rc = EINVAL;
600                 goto fail_inval;
601         }
602
603         switch (fc_conf->mode) {
604         case RTE_FC_NONE:
605                 fcntl = 0;
606                 break;
607         case RTE_FC_RX_PAUSE:
608                 fcntl = EFX_FCNTL_RESPOND;
609                 break;
610         case RTE_FC_TX_PAUSE:
611                 fcntl = EFX_FCNTL_GENERATE;
612                 break;
613         case RTE_FC_FULL:
614                 fcntl = EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE;
615                 break;
616         default:
617                 rc = EINVAL;
618                 goto fail_inval;
619         }
620
621         sfc_adapter_lock(sa);
622
623         if (sa->state == SFC_ADAPTER_STARTED) {
624                 rc = efx_mac_fcntl_set(sa->nic, fcntl, fc_conf->autoneg);
625                 if (rc != 0)
626                         goto fail_mac_fcntl_set;
627         }
628
629         port->flow_ctrl = fcntl;
630         port->flow_ctrl_autoneg = fc_conf->autoneg;
631
632         sfc_adapter_unlock(sa);
633
634         return 0;
635
636 fail_mac_fcntl_set:
637         sfc_adapter_unlock(sa);
638 fail_inval:
639         SFC_ASSERT(rc > 0);
640         return -rc;
641 }
642
643 static int
644 sfc_dev_set_mtu(struct rte_eth_dev *dev, uint16_t mtu)
645 {
646         struct sfc_adapter *sa = dev->data->dev_private;
647         size_t pdu = EFX_MAC_PDU(mtu);
648         size_t old_pdu;
649         int rc;
650
651         sfc_log_init(sa, "mtu=%u", mtu);
652
653         rc = EINVAL;
654         if (pdu < EFX_MAC_PDU_MIN) {
655                 sfc_err(sa, "too small MTU %u (PDU size %u less than min %u)",
656                         (unsigned int)mtu, (unsigned int)pdu,
657                         EFX_MAC_PDU_MIN);
658                 goto fail_inval;
659         }
660         if (pdu > EFX_MAC_PDU_MAX) {
661                 sfc_err(sa, "too big MTU %u (PDU size %u greater than max %u)",
662                         (unsigned int)mtu, (unsigned int)pdu,
663                         EFX_MAC_PDU_MAX);
664                 goto fail_inval;
665         }
666
667         sfc_adapter_lock(sa);
668
669         if (pdu != sa->port.pdu) {
670                 if (sa->state == SFC_ADAPTER_STARTED) {
671                         sfc_stop(sa);
672
673                         old_pdu = sa->port.pdu;
674                         sa->port.pdu = pdu;
675                         rc = sfc_start(sa);
676                         if (rc != 0)
677                                 goto fail_start;
678                 } else {
679                         sa->port.pdu = pdu;
680                 }
681         }
682
683         /*
684          * The driver does not use it, but other PMDs update jumbo_frame
685          * flag and max_rx_pkt_len when MTU is set.
686          */
687         dev->data->dev_conf.rxmode.jumbo_frame = (mtu > ETHER_MAX_LEN);
688         dev->data->dev_conf.rxmode.max_rx_pkt_len = sa->port.pdu;
689
690         sfc_adapter_unlock(sa);
691
692         sfc_log_init(sa, "done");
693         return 0;
694
695 fail_start:
696         sa->port.pdu = old_pdu;
697         if (sfc_start(sa) != 0)
698                 sfc_err(sa, "cannot start with neither new (%u) nor old (%u) "
699                         "PDU max size - port is stopped",
700                         (unsigned int)pdu, (unsigned int)old_pdu);
701         sfc_adapter_unlock(sa);
702
703 fail_inval:
704         sfc_log_init(sa, "failed %d", rc);
705         SFC_ASSERT(rc > 0);
706         return -rc;
707 }
708 static void
709 sfc_mac_addr_set(struct rte_eth_dev *dev, struct ether_addr *mac_addr)
710 {
711         struct sfc_adapter *sa = dev->data->dev_private;
712         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
713         int rc;
714
715         sfc_adapter_lock(sa);
716
717         if (sa->state != SFC_ADAPTER_STARTED) {
718                 sfc_info(sa, "the port is not started");
719                 sfc_info(sa, "the new MAC address will be set on port start");
720
721                 goto unlock;
722         }
723
724         if (encp->enc_allow_set_mac_with_installed_filters) {
725                 rc = efx_mac_addr_set(sa->nic, mac_addr->addr_bytes);
726                 if (rc != 0) {
727                         sfc_err(sa, "cannot set MAC address (rc = %u)", rc);
728                         goto unlock;
729                 }
730
731                 /*
732                  * Changing the MAC address by means of MCDI request
733                  * has no effect on received traffic, therefore
734                  * we also need to update unicast filters
735                  */
736                 rc = sfc_set_rx_mode(sa);
737                 if (rc != 0)
738                         sfc_err(sa, "cannot set filter (rc = %u)", rc);
739         } else {
740                 sfc_warn(sa, "cannot set MAC address with filters installed");
741                 sfc_warn(sa, "adapter will be restarted to pick the new MAC");
742                 sfc_warn(sa, "(some traffic may be dropped)");
743
744                 /*
745                  * Since setting MAC address with filters installed is not
746                  * allowed on the adapter, one needs to simply restart adapter
747                  * so that the new MAC address will be taken from an outer
748                  * storage and set flawlessly by means of sfc_start() call
749                  */
750                 sfc_stop(sa);
751                 rc = sfc_start(sa);
752                 if (rc != 0)
753                         sfc_err(sa, "cannot restart adapter (rc = %u)", rc);
754         }
755
756 unlock:
757         sfc_adapter_unlock(sa);
758 }
759
760
761 static int
762 sfc_set_mc_addr_list(struct rte_eth_dev *dev, struct ether_addr *mc_addr_set,
763                      uint32_t nb_mc_addr)
764 {
765         struct sfc_adapter *sa = dev->data->dev_private;
766         uint8_t *mc_addrs_p;
767         uint8_t *mc_addrs;
768         int rc;
769         unsigned int i;
770
771         if (nb_mc_addr > EFX_MAC_MULTICAST_LIST_MAX) {
772                 sfc_err(sa, "too many multicast addresses: %u > %u",
773                          nb_mc_addr, EFX_MAC_MULTICAST_LIST_MAX);
774                 return -EINVAL;
775         }
776
777         mc_addrs_p = rte_calloc("mc-addrs", nb_mc_addr, EFX_MAC_ADDR_LEN, 0);
778         if (mc_addrs_p == NULL)
779                 return -ENOMEM;
780
781         mc_addrs = mc_addrs_p;
782
783         for (i = 0; i < nb_mc_addr; ++i) {
784                 (void)rte_memcpy(mc_addrs, mc_addr_set[i].addr_bytes,
785                                  EFX_MAC_ADDR_LEN);
786                 mc_addrs += EFX_MAC_ADDR_LEN;
787         }
788
789         rc = efx_mac_multicast_list_set(sa->nic, mc_addrs_p, nb_mc_addr);
790
791         rte_free(mc_addrs_p);
792
793         if (rc != 0)
794                 sfc_err(sa, "cannot set multicast address list (rc = %u)", rc);
795
796         SFC_ASSERT(rc > 0);
797         return -rc;
798 }
799
800 static const struct eth_dev_ops sfc_eth_dev_ops = {
801         .dev_configure                  = sfc_dev_configure,
802         .dev_start                      = sfc_dev_start,
803         .dev_stop                       = sfc_dev_stop,
804         .dev_set_link_up                = sfc_dev_set_link_up,
805         .dev_set_link_down              = sfc_dev_set_link_down,
806         .dev_close                      = sfc_dev_close,
807         .promiscuous_enable             = sfc_dev_promisc_enable,
808         .promiscuous_disable            = sfc_dev_promisc_disable,
809         .allmulticast_enable            = sfc_dev_allmulti_enable,
810         .allmulticast_disable           = sfc_dev_allmulti_disable,
811         .link_update                    = sfc_dev_link_update,
812         .stats_get                      = sfc_stats_get,
813         .xstats_get                     = sfc_xstats_get,
814         .xstats_get_names               = sfc_xstats_get_names,
815         .dev_infos_get                  = sfc_dev_infos_get,
816         .mtu_set                        = sfc_dev_set_mtu,
817         .rx_queue_setup                 = sfc_rx_queue_setup,
818         .rx_queue_release               = sfc_rx_queue_release,
819         .tx_queue_setup                 = sfc_tx_queue_setup,
820         .tx_queue_release               = sfc_tx_queue_release,
821         .flow_ctrl_get                  = sfc_flow_ctrl_get,
822         .flow_ctrl_set                  = sfc_flow_ctrl_set,
823         .mac_addr_set                   = sfc_mac_addr_set,
824         .set_mc_addr_list               = sfc_set_mc_addr_list,
825 };
826
827 static int
828 sfc_eth_dev_init(struct rte_eth_dev *dev)
829 {
830         struct sfc_adapter *sa = dev->data->dev_private;
831         struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(dev);
832         int rc;
833         const efx_nic_cfg_t *encp;
834         const struct ether_addr *from;
835
836         /* Required for logging */
837         sa->eth_dev = dev;
838
839         /* Copy PCI device info to the dev->data */
840         rte_eth_copy_pci_info(dev, pci_dev);
841
842         rc = sfc_kvargs_parse(sa);
843         if (rc != 0)
844                 goto fail_kvargs_parse;
845
846         rc = sfc_kvargs_process(sa, SFC_KVARG_DEBUG_INIT,
847                                 sfc_kvarg_bool_handler, &sa->debug_init);
848         if (rc != 0)
849                 goto fail_kvarg_debug_init;
850
851         sfc_log_init(sa, "entry");
852
853         dev->data->mac_addrs = rte_zmalloc("sfc", ETHER_ADDR_LEN, 0);
854         if (dev->data->mac_addrs == NULL) {
855                 rc = ENOMEM;
856                 goto fail_mac_addrs;
857         }
858
859         sfc_adapter_lock_init(sa);
860         sfc_adapter_lock(sa);
861
862         sfc_log_init(sa, "attaching");
863         rc = sfc_attach(sa);
864         if (rc != 0)
865                 goto fail_attach;
866
867         encp = efx_nic_cfg_get(sa->nic);
868
869         /*
870          * The arguments are really reverse order in comparison to
871          * Linux kernel. Copy from NIC config to Ethernet device data.
872          */
873         from = (const struct ether_addr *)(encp->enc_mac_addr);
874         ether_addr_copy(from, &dev->data->mac_addrs[0]);
875
876         dev->dev_ops = &sfc_eth_dev_ops;
877         dev->rx_pkt_burst = &sfc_recv_pkts;
878         dev->tx_pkt_burst = &sfc_xmit_pkts;
879
880         sfc_adapter_unlock(sa);
881
882         sfc_log_init(sa, "done");
883         return 0;
884
885 fail_attach:
886         sfc_adapter_unlock(sa);
887         sfc_adapter_lock_fini(sa);
888         rte_free(dev->data->mac_addrs);
889         dev->data->mac_addrs = NULL;
890
891 fail_mac_addrs:
892 fail_kvarg_debug_init:
893         sfc_kvargs_cleanup(sa);
894
895 fail_kvargs_parse:
896         sfc_log_init(sa, "failed %d", rc);
897         SFC_ASSERT(rc > 0);
898         return -rc;
899 }
900
901 static int
902 sfc_eth_dev_uninit(struct rte_eth_dev *dev)
903 {
904         struct sfc_adapter *sa = dev->data->dev_private;
905
906         sfc_log_init(sa, "entry");
907
908         sfc_adapter_lock(sa);
909
910         sfc_detach(sa);
911
912         rte_free(dev->data->mac_addrs);
913         dev->data->mac_addrs = NULL;
914
915         dev->dev_ops = NULL;
916         dev->rx_pkt_burst = NULL;
917         dev->tx_pkt_burst = NULL;
918
919         sfc_kvargs_cleanup(sa);
920
921         sfc_adapter_unlock(sa);
922         sfc_adapter_lock_fini(sa);
923
924         sfc_log_init(sa, "done");
925
926         /* Required for logging, so cleanup last */
927         sa->eth_dev = NULL;
928         return 0;
929 }
930
931 static const struct rte_pci_id pci_id_sfc_efx_map[] = {
932         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_FARMINGDALE) },
933         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_GREENPORT) },
934         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_MEDFORD) },
935         { .vendor_id = 0 /* sentinel */ }
936 };
937
938 static struct eth_driver sfc_efx_pmd = {
939         .pci_drv = {
940                 .id_table = pci_id_sfc_efx_map,
941                 .drv_flags =
942                         RTE_PCI_DRV_INTR_LSC |
943                         RTE_PCI_DRV_NEED_MAPPING,
944                 .probe = rte_eth_dev_pci_probe,
945                 .remove = rte_eth_dev_pci_remove,
946         },
947         .eth_dev_init = sfc_eth_dev_init,
948         .eth_dev_uninit = sfc_eth_dev_uninit,
949         .dev_private_size = sizeof(struct sfc_adapter),
950 };
951
952 RTE_PMD_REGISTER_PCI(net_sfc_efx, sfc_efx_pmd.pci_drv);
953 RTE_PMD_REGISTER_PCI_TABLE(net_sfc_efx, pci_id_sfc_efx_map);
954 RTE_PMD_REGISTER_PARAM_STRING(net_sfc_efx,
955         SFC_KVARG_PERF_PROFILE "=" SFC_KVARG_VALUES_PERF_PROFILE " "
956         SFC_KVARG_MCDI_LOGGING "=" SFC_KVARG_VALUES_BOOL " "
957         SFC_KVARG_DEBUG_INIT "=" SFC_KVARG_VALUES_BOOL);