net/enic: add Rx/Tx queue configuration getters
[dpdk.git] / drivers / net / enic / enic_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright 2008-2017 Cisco Systems, Inc.  All rights reserved.
3  * Copyright 2007 Nuova Systems, Inc.  All rights reserved.
4  */
5
6 #include <stdio.h>
7 #include <stdint.h>
8
9 #include <rte_dev.h>
10 #include <rte_pci.h>
11 #include <rte_bus_pci.h>
12 #include <rte_ethdev_driver.h>
13 #include <rte_ethdev_pci.h>
14 #include <rte_string_fns.h>
15
16 #include "vnic_intr.h"
17 #include "vnic_cq.h"
18 #include "vnic_wq.h"
19 #include "vnic_rq.h"
20 #include "vnic_enet.h"
21 #include "enic.h"
22
23 int enicpmd_logtype_init;
24 int enicpmd_logtype_flow;
25
26 #define PMD_INIT_LOG(level, fmt, args...) \
27         rte_log(RTE_LOG_ ## level, enicpmd_logtype_init, \
28                 "%s" fmt "\n", __func__, ##args)
29
30 #define ENICPMD_FUNC_TRACE() PMD_INIT_LOG(DEBUG, " >>")
31
32 /*
33  * The set of PCI devices this driver supports
34  */
35 #define CISCO_PCI_VENDOR_ID 0x1137
36 static const struct rte_pci_id pci_id_enic_map[] = {
37         { RTE_PCI_DEVICE(CISCO_PCI_VENDOR_ID, PCI_DEVICE_ID_CISCO_VIC_ENET) },
38         { RTE_PCI_DEVICE(CISCO_PCI_VENDOR_ID, PCI_DEVICE_ID_CISCO_VIC_ENET_VF) },
39         {.vendor_id = 0, /* sentinel */},
40 };
41
42 #define ENIC_TX_OFFLOAD_CAPA (                  \
43                 DEV_TX_OFFLOAD_VLAN_INSERT |    \
44                 DEV_TX_OFFLOAD_IPV4_CKSUM  |    \
45                 DEV_TX_OFFLOAD_UDP_CKSUM   |    \
46                 DEV_TX_OFFLOAD_TCP_CKSUM   |    \
47                 DEV_TX_OFFLOAD_TCP_TSO)
48
49 #define ENIC_RX_OFFLOAD_CAPA (                  \
50                 DEV_RX_OFFLOAD_VLAN_STRIP |     \
51                 DEV_RX_OFFLOAD_IPV4_CKSUM |     \
52                 DEV_RX_OFFLOAD_UDP_CKSUM  |     \
53                 DEV_RX_OFFLOAD_TCP_CKSUM)
54
55 RTE_INIT(enicpmd_init_log);
56 static void
57 enicpmd_init_log(void)
58 {
59         enicpmd_logtype_init = rte_log_register("pmd.net.enic.init");
60         if (enicpmd_logtype_init >= 0)
61                 rte_log_set_level(enicpmd_logtype_init, RTE_LOG_NOTICE);
62         enicpmd_logtype_flow = rte_log_register("pmd.net.enic.flow");
63         if (enicpmd_logtype_flow >= 0)
64                 rte_log_set_level(enicpmd_logtype_flow, RTE_LOG_NOTICE);
65 }
66
67 static int
68 enicpmd_fdir_ctrl_func(struct rte_eth_dev *eth_dev,
69                         enum rte_filter_op filter_op, void *arg)
70 {
71         struct enic *enic = pmd_priv(eth_dev);
72         int ret = 0;
73
74         ENICPMD_FUNC_TRACE();
75         if (filter_op == RTE_ETH_FILTER_NOP)
76                 return 0;
77
78         if (arg == NULL && filter_op != RTE_ETH_FILTER_FLUSH)
79                 return -EINVAL;
80
81         switch (filter_op) {
82         case RTE_ETH_FILTER_ADD:
83         case RTE_ETH_FILTER_UPDATE:
84                 ret = enic_fdir_add_fltr(enic,
85                         (struct rte_eth_fdir_filter *)arg);
86                 break;
87
88         case RTE_ETH_FILTER_DELETE:
89                 ret = enic_fdir_del_fltr(enic,
90                         (struct rte_eth_fdir_filter *)arg);
91                 break;
92
93         case RTE_ETH_FILTER_STATS:
94                 enic_fdir_stats_get(enic, (struct rte_eth_fdir_stats *)arg);
95                 break;
96
97         case RTE_ETH_FILTER_FLUSH:
98                 dev_warning(enic, "unsupported operation %u", filter_op);
99                 ret = -ENOTSUP;
100                 break;
101         case RTE_ETH_FILTER_INFO:
102                 enic_fdir_info_get(enic, (struct rte_eth_fdir_info *)arg);
103                 break;
104         default:
105                 dev_err(enic, "unknown operation %u", filter_op);
106                 ret = -EINVAL;
107                 break;
108         }
109         return ret;
110 }
111
112 static int
113 enicpmd_dev_filter_ctrl(struct rte_eth_dev *dev,
114                      enum rte_filter_type filter_type,
115                      enum rte_filter_op filter_op,
116                      void *arg)
117 {
118         int ret = 0;
119
120         ENICPMD_FUNC_TRACE();
121
122         switch (filter_type) {
123         case RTE_ETH_FILTER_GENERIC:
124                 if (filter_op != RTE_ETH_FILTER_GET)
125                         return -EINVAL;
126                 *(const void **)arg = &enic_flow_ops;
127                 break;
128         case RTE_ETH_FILTER_FDIR:
129                 ret = enicpmd_fdir_ctrl_func(dev, filter_op, arg);
130                 break;
131         default:
132                 dev_warning(enic, "Filter type (%d) not supported",
133                         filter_type);
134                 ret = -EINVAL;
135                 break;
136         }
137
138         return ret;
139 }
140
141 static void enicpmd_dev_tx_queue_release(void *txq)
142 {
143         ENICPMD_FUNC_TRACE();
144
145         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
146                 return;
147
148         enic_free_wq(txq);
149 }
150
151 static int enicpmd_dev_setup_intr(struct enic *enic)
152 {
153         int ret;
154         unsigned int index;
155
156         ENICPMD_FUNC_TRACE();
157
158         /* Are we done with the init of all the queues? */
159         for (index = 0; index < enic->cq_count; index++) {
160                 if (!enic->cq[index].ctrl)
161                         break;
162         }
163         if (enic->cq_count != index)
164                 return 0;
165         for (index = 0; index < enic->wq_count; index++) {
166                 if (!enic->wq[index].ctrl)
167                         break;
168         }
169         if (enic->wq_count != index)
170                 return 0;
171         /* check start of packet (SOP) RQs only in case scatter is disabled. */
172         for (index = 0; index < enic->rq_count; index++) {
173                 if (!enic->rq[enic_rte_rq_idx_to_sop_idx(index)].ctrl)
174                         break;
175         }
176         if (enic->rq_count != index)
177                 return 0;
178
179         ret = enic_alloc_intr_resources(enic);
180         if (ret) {
181                 dev_err(enic, "alloc intr failed\n");
182                 return ret;
183         }
184         enic_init_vnic_resources(enic);
185
186         ret = enic_setup_finish(enic);
187         if (ret)
188                 dev_err(enic, "setup could not be finished\n");
189
190         return ret;
191 }
192
193 static int enicpmd_dev_tx_queue_setup(struct rte_eth_dev *eth_dev,
194         uint16_t queue_idx,
195         uint16_t nb_desc,
196         unsigned int socket_id,
197         __rte_unused const struct rte_eth_txconf *tx_conf)
198 {
199         int ret;
200         struct enic *enic = pmd_priv(eth_dev);
201
202         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
203                 return -E_RTE_SECONDARY;
204
205         ENICPMD_FUNC_TRACE();
206         RTE_ASSERT(queue_idx < enic->conf_wq_count);
207         eth_dev->data->tx_queues[queue_idx] = (void *)&enic->wq[queue_idx];
208
209         ret = enic_alloc_wq(enic, queue_idx, socket_id, nb_desc);
210         if (ret) {
211                 dev_err(enic, "error in allocating wq\n");
212                 return ret;
213         }
214
215         return enicpmd_dev_setup_intr(enic);
216 }
217
218 static int enicpmd_dev_tx_queue_start(struct rte_eth_dev *eth_dev,
219         uint16_t queue_idx)
220 {
221         struct enic *enic = pmd_priv(eth_dev);
222
223         ENICPMD_FUNC_TRACE();
224
225         enic_start_wq(enic, queue_idx);
226
227         return 0;
228 }
229
230 static int enicpmd_dev_tx_queue_stop(struct rte_eth_dev *eth_dev,
231         uint16_t queue_idx)
232 {
233         int ret;
234         struct enic *enic = pmd_priv(eth_dev);
235
236         ENICPMD_FUNC_TRACE();
237
238         ret = enic_stop_wq(enic, queue_idx);
239         if (ret)
240                 dev_err(enic, "error in stopping wq %d\n", queue_idx);
241
242         return ret;
243 }
244
245 static int enicpmd_dev_rx_queue_start(struct rte_eth_dev *eth_dev,
246         uint16_t queue_idx)
247 {
248         struct enic *enic = pmd_priv(eth_dev);
249
250         ENICPMD_FUNC_TRACE();
251
252         enic_start_rq(enic, queue_idx);
253
254         return 0;
255 }
256
257 static int enicpmd_dev_rx_queue_stop(struct rte_eth_dev *eth_dev,
258         uint16_t queue_idx)
259 {
260         int ret;
261         struct enic *enic = pmd_priv(eth_dev);
262
263         ENICPMD_FUNC_TRACE();
264
265         ret = enic_stop_rq(enic, queue_idx);
266         if (ret)
267                 dev_err(enic, "error in stopping rq %d\n", queue_idx);
268
269         return ret;
270 }
271
272 static void enicpmd_dev_rx_queue_release(void *rxq)
273 {
274         ENICPMD_FUNC_TRACE();
275
276         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
277                 return;
278
279         enic_free_rq(rxq);
280 }
281
282 static uint32_t enicpmd_dev_rx_queue_count(struct rte_eth_dev *dev,
283                                            uint16_t rx_queue_id)
284 {
285         struct enic *enic = pmd_priv(dev);
286         uint32_t queue_count = 0;
287         struct vnic_cq *cq;
288         uint32_t cq_tail;
289         uint16_t cq_idx;
290         int rq_num;
291
292         rq_num = enic_rte_rq_idx_to_sop_idx(rx_queue_id);
293         cq = &enic->cq[enic_cq_rq(enic, rq_num)];
294         cq_idx = cq->to_clean;
295
296         cq_tail = ioread32(&cq->ctrl->cq_tail);
297
298         if (cq_tail < cq_idx)
299                 cq_tail += cq->ring.desc_count;
300
301         queue_count = cq_tail - cq_idx;
302
303         return queue_count;
304 }
305
306 static int enicpmd_dev_rx_queue_setup(struct rte_eth_dev *eth_dev,
307         uint16_t queue_idx,
308         uint16_t nb_desc,
309         unsigned int socket_id,
310         const struct rte_eth_rxconf *rx_conf,
311         struct rte_mempool *mp)
312 {
313         int ret;
314         struct enic *enic = pmd_priv(eth_dev);
315
316         ENICPMD_FUNC_TRACE();
317
318         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
319                 return -E_RTE_SECONDARY;
320         RTE_ASSERT(enic_rte_rq_idx_to_sop_idx(queue_idx) < enic->conf_rq_count);
321         eth_dev->data->rx_queues[queue_idx] =
322                 (void *)&enic->rq[enic_rte_rq_idx_to_sop_idx(queue_idx)];
323
324         ret = enic_alloc_rq(enic, queue_idx, socket_id, mp, nb_desc,
325                             rx_conf->rx_free_thresh);
326         if (ret) {
327                 dev_err(enic, "error in allocating rq\n");
328                 return ret;
329         }
330
331         return enicpmd_dev_setup_intr(enic);
332 }
333
334 static int enicpmd_vlan_offload_set(struct rte_eth_dev *eth_dev, int mask)
335 {
336         struct enic *enic = pmd_priv(eth_dev);
337         uint64_t offloads;
338
339         ENICPMD_FUNC_TRACE();
340
341         offloads = eth_dev->data->dev_conf.rxmode.offloads;
342         if (mask & ETH_VLAN_STRIP_MASK) {
343                 if (offloads & DEV_RX_OFFLOAD_VLAN_STRIP)
344                         enic->ig_vlan_strip_en = 1;
345                 else
346                         enic->ig_vlan_strip_en = 0;
347         }
348
349         if ((mask & ETH_VLAN_FILTER_MASK) &&
350             (offloads & DEV_RX_OFFLOAD_VLAN_FILTER)) {
351                 dev_warning(enic,
352                         "Configuration of VLAN filter is not supported\n");
353         }
354
355         if ((mask & ETH_VLAN_EXTEND_MASK) &&
356             (offloads & DEV_RX_OFFLOAD_VLAN_EXTEND)) {
357                 dev_warning(enic,
358                         "Configuration of extended VLAN is not supported\n");
359         }
360
361         return enic_set_vlan_strip(enic);
362 }
363
364 static int enicpmd_dev_configure(struct rte_eth_dev *eth_dev)
365 {
366         int ret;
367         int mask;
368         struct enic *enic = pmd_priv(eth_dev);
369
370         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
371                 return -E_RTE_SECONDARY;
372
373         ENICPMD_FUNC_TRACE();
374         ret = enic_set_vnic_res(enic);
375         if (ret) {
376                 dev_err(enic, "Set vNIC resource num  failed, aborting\n");
377                 return ret;
378         }
379
380         enic->hw_ip_checksum = !!(eth_dev->data->dev_conf.rxmode.offloads &
381                                   DEV_RX_OFFLOAD_CHECKSUM);
382         /* All vlan offload masks to apply the current settings */
383         mask = ETH_VLAN_STRIP_MASK |
384                 ETH_VLAN_FILTER_MASK |
385                 ETH_VLAN_EXTEND_MASK;
386         ret = enicpmd_vlan_offload_set(eth_dev, mask);
387         if (ret) {
388                 dev_err(enic, "Failed to configure VLAN offloads\n");
389                 return ret;
390         }
391         /*
392          * Initialize RSS with the default reta and key. If the user key is
393          * given (rx_adv_conf.rss_conf.rss_key), will use that instead of the
394          * default key.
395          */
396         return enic_init_rss_nic_cfg(enic);
397 }
398
399 /* Start the device.
400  * It returns 0 on success.
401  */
402 static int enicpmd_dev_start(struct rte_eth_dev *eth_dev)
403 {
404         struct enic *enic = pmd_priv(eth_dev);
405
406         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
407                 return -E_RTE_SECONDARY;
408
409         ENICPMD_FUNC_TRACE();
410         return enic_enable(enic);
411 }
412
413 /*
414  * Stop device: disable rx and tx functions to allow for reconfiguring.
415  */
416 static void enicpmd_dev_stop(struct rte_eth_dev *eth_dev)
417 {
418         struct rte_eth_link link;
419         struct enic *enic = pmd_priv(eth_dev);
420
421         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
422                 return;
423
424         ENICPMD_FUNC_TRACE();
425         enic_disable(enic);
426         memset(&link, 0, sizeof(link));
427         rte_atomic64_cmpset((uint64_t *)&eth_dev->data->dev_link,
428                 *(uint64_t *)&eth_dev->data->dev_link,
429                 *(uint64_t *)&link);
430 }
431
432 /*
433  * Stop device.
434  */
435 static void enicpmd_dev_close(struct rte_eth_dev *eth_dev)
436 {
437         struct enic *enic = pmd_priv(eth_dev);
438
439         ENICPMD_FUNC_TRACE();
440         enic_remove(enic);
441 }
442
443 static int enicpmd_dev_link_update(struct rte_eth_dev *eth_dev,
444         __rte_unused int wait_to_complete)
445 {
446         struct enic *enic = pmd_priv(eth_dev);
447
448         ENICPMD_FUNC_TRACE();
449         return enic_link_update(enic);
450 }
451
452 static int enicpmd_dev_stats_get(struct rte_eth_dev *eth_dev,
453         struct rte_eth_stats *stats)
454 {
455         struct enic *enic = pmd_priv(eth_dev);
456
457         ENICPMD_FUNC_TRACE();
458         return enic_dev_stats_get(enic, stats);
459 }
460
461 static void enicpmd_dev_stats_reset(struct rte_eth_dev *eth_dev)
462 {
463         struct enic *enic = pmd_priv(eth_dev);
464
465         ENICPMD_FUNC_TRACE();
466         enic_dev_stats_clear(enic);
467 }
468
469 static void enicpmd_dev_info_get(struct rte_eth_dev *eth_dev,
470         struct rte_eth_dev_info *device_info)
471 {
472         struct enic *enic = pmd_priv(eth_dev);
473
474         ENICPMD_FUNC_TRACE();
475         device_info->pci_dev = RTE_ETH_DEV_TO_PCI(eth_dev);
476         /* Scattered Rx uses two receive queues per rx queue exposed to dpdk */
477         device_info->max_rx_queues = enic->conf_rq_count / 2;
478         device_info->max_tx_queues = enic->conf_wq_count;
479         device_info->min_rx_bufsize = ENIC_MIN_MTU;
480         /* "Max" mtu is not a typo. HW receives packet sizes up to the
481          * max mtu regardless of the current mtu (vNIC's mtu). vNIC mtu is
482          * a hint to the driver to size receive buffers accordingly so that
483          * larger-than-vnic-mtu packets get truncated.. For DPDK, we let
484          * the user decide the buffer size via rxmode.max_rx_pkt_len, basically
485          * ignoring vNIC mtu.
486          */
487         device_info->max_rx_pktlen = enic_mtu_to_max_rx_pktlen(enic->max_mtu);
488         device_info->max_mac_addrs = ENIC_MAX_MAC_ADDR;
489         device_info->rx_offload_capa = ENIC_RX_OFFLOAD_CAPA;
490         device_info->tx_offload_capa = ENIC_TX_OFFLOAD_CAPA;
491         device_info->default_rxconf = (struct rte_eth_rxconf) {
492                 .rx_free_thresh = ENIC_DEFAULT_RX_FREE_THRESH
493         };
494         device_info->reta_size = enic->reta_size;
495         device_info->hash_key_size = enic->hash_key_size;
496         device_info->flow_type_rss_offloads = enic->flow_type_rss_offloads;
497 }
498
499 static const uint32_t *enicpmd_dev_supported_ptypes_get(struct rte_eth_dev *dev)
500 {
501         static const uint32_t ptypes[] = {
502                 RTE_PTYPE_L2_ETHER,
503                 RTE_PTYPE_L2_ETHER_VLAN,
504                 RTE_PTYPE_L3_IPV4_EXT_UNKNOWN,
505                 RTE_PTYPE_L3_IPV6_EXT_UNKNOWN,
506                 RTE_PTYPE_L4_TCP,
507                 RTE_PTYPE_L4_UDP,
508                 RTE_PTYPE_L4_FRAG,
509                 RTE_PTYPE_L4_NONFRAG,
510                 RTE_PTYPE_UNKNOWN
511         };
512
513         if (dev->rx_pkt_burst == enic_recv_pkts)
514                 return ptypes;
515         return NULL;
516 }
517
518 static void enicpmd_dev_promiscuous_enable(struct rte_eth_dev *eth_dev)
519 {
520         struct enic *enic = pmd_priv(eth_dev);
521
522         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
523                 return;
524
525         ENICPMD_FUNC_TRACE();
526
527         enic->promisc = 1;
528         enic_add_packet_filter(enic);
529 }
530
531 static void enicpmd_dev_promiscuous_disable(struct rte_eth_dev *eth_dev)
532 {
533         struct enic *enic = pmd_priv(eth_dev);
534
535         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
536                 return;
537
538         ENICPMD_FUNC_TRACE();
539         enic->promisc = 0;
540         enic_add_packet_filter(enic);
541 }
542
543 static void enicpmd_dev_allmulticast_enable(struct rte_eth_dev *eth_dev)
544 {
545         struct enic *enic = pmd_priv(eth_dev);
546
547         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
548                 return;
549
550         ENICPMD_FUNC_TRACE();
551         enic->allmulti = 1;
552         enic_add_packet_filter(enic);
553 }
554
555 static void enicpmd_dev_allmulticast_disable(struct rte_eth_dev *eth_dev)
556 {
557         struct enic *enic = pmd_priv(eth_dev);
558
559         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
560                 return;
561
562         ENICPMD_FUNC_TRACE();
563         enic->allmulti = 0;
564         enic_add_packet_filter(enic);
565 }
566
567 static int enicpmd_add_mac_addr(struct rte_eth_dev *eth_dev,
568         struct ether_addr *mac_addr,
569         __rte_unused uint32_t index, __rte_unused uint32_t pool)
570 {
571         struct enic *enic = pmd_priv(eth_dev);
572
573         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
574                 return -E_RTE_SECONDARY;
575
576         ENICPMD_FUNC_TRACE();
577         return enic_set_mac_address(enic, mac_addr->addr_bytes);
578 }
579
580 static void enicpmd_remove_mac_addr(struct rte_eth_dev *eth_dev, uint32_t index)
581 {
582         struct enic *enic = pmd_priv(eth_dev);
583
584         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
585                 return;
586
587         ENICPMD_FUNC_TRACE();
588         enic_del_mac_address(enic, index);
589 }
590
591 static int enicpmd_mtu_set(struct rte_eth_dev *eth_dev, uint16_t mtu)
592 {
593         struct enic *enic = pmd_priv(eth_dev);
594
595         ENICPMD_FUNC_TRACE();
596         return enic_set_mtu(enic, mtu);
597 }
598
599 static int enicpmd_dev_rss_reta_query(struct rte_eth_dev *dev,
600                                       struct rte_eth_rss_reta_entry64
601                                       *reta_conf,
602                                       uint16_t reta_size)
603 {
604         struct enic *enic = pmd_priv(dev);
605         uint16_t i, idx, shift;
606
607         ENICPMD_FUNC_TRACE();
608         if (reta_size != ENIC_RSS_RETA_SIZE) {
609                 dev_err(enic, "reta_query: wrong reta_size. given=%u expected=%u\n",
610                         reta_size, ENIC_RSS_RETA_SIZE);
611                 return -EINVAL;
612         }
613
614         for (i = 0; i < reta_size; i++) {
615                 idx = i / RTE_RETA_GROUP_SIZE;
616                 shift = i % RTE_RETA_GROUP_SIZE;
617                 if (reta_conf[idx].mask & (1ULL << shift))
618                         reta_conf[idx].reta[shift] = enic_sop_rq_idx_to_rte_idx(
619                                 enic->rss_cpu.cpu[i / 4].b[i % 4]);
620         }
621
622         return 0;
623 }
624
625 static int enicpmd_dev_rss_reta_update(struct rte_eth_dev *dev,
626                                        struct rte_eth_rss_reta_entry64
627                                        *reta_conf,
628                                        uint16_t reta_size)
629 {
630         struct enic *enic = pmd_priv(dev);
631         union vnic_rss_cpu rss_cpu;
632         uint16_t i, idx, shift;
633
634         ENICPMD_FUNC_TRACE();
635         if (reta_size != ENIC_RSS_RETA_SIZE) {
636                 dev_err(enic, "reta_update: wrong reta_size. given=%u"
637                         " expected=%u\n",
638                         reta_size, ENIC_RSS_RETA_SIZE);
639                 return -EINVAL;
640         }
641         /*
642          * Start with the current reta and modify it per reta_conf, as we
643          * need to push the entire reta even if we only modify one entry.
644          */
645         rss_cpu = enic->rss_cpu;
646         for (i = 0; i < reta_size; i++) {
647                 idx = i / RTE_RETA_GROUP_SIZE;
648                 shift = i % RTE_RETA_GROUP_SIZE;
649                 if (reta_conf[idx].mask & (1ULL << shift))
650                         rss_cpu.cpu[i / 4].b[i % 4] =
651                                 enic_rte_rq_idx_to_sop_idx(
652                                         reta_conf[idx].reta[shift]);
653         }
654         return enic_set_rss_reta(enic, &rss_cpu);
655 }
656
657 static int enicpmd_dev_rss_hash_update(struct rte_eth_dev *dev,
658                                        struct rte_eth_rss_conf *rss_conf)
659 {
660         struct enic *enic = pmd_priv(dev);
661
662         ENICPMD_FUNC_TRACE();
663         return enic_set_rss_conf(enic, rss_conf);
664 }
665
666 static int enicpmd_dev_rss_hash_conf_get(struct rte_eth_dev *dev,
667                                          struct rte_eth_rss_conf *rss_conf)
668 {
669         struct enic *enic = pmd_priv(dev);
670
671         ENICPMD_FUNC_TRACE();
672         if (rss_conf == NULL)
673                 return -EINVAL;
674         if (rss_conf->rss_key != NULL &&
675             rss_conf->rss_key_len < ENIC_RSS_HASH_KEY_SIZE) {
676                 dev_err(enic, "rss_hash_conf_get: wrong rss_key_len. given=%u"
677                         " expected=%u+\n",
678                         rss_conf->rss_key_len, ENIC_RSS_HASH_KEY_SIZE);
679                 return -EINVAL;
680         }
681         rss_conf->rss_hf = enic->rss_hf;
682         if (rss_conf->rss_key != NULL) {
683                 int i;
684                 for (i = 0; i < ENIC_RSS_HASH_KEY_SIZE; i++) {
685                         rss_conf->rss_key[i] =
686                                 enic->rss_key.key[i / 10].b[i % 10];
687                 }
688                 rss_conf->rss_key_len = ENIC_RSS_HASH_KEY_SIZE;
689         }
690         return 0;
691 }
692
693 static void enicpmd_dev_rxq_info_get(struct rte_eth_dev *dev,
694                                      uint16_t rx_queue_id,
695                                      struct rte_eth_rxq_info *qinfo)
696 {
697         struct enic *enic = pmd_priv(dev);
698         struct vnic_rq *rq_sop;
699         struct vnic_rq *rq_data;
700         struct rte_eth_rxconf *conf;
701         uint16_t sop_queue_idx;
702         uint16_t data_queue_idx;
703
704         ENICPMD_FUNC_TRACE();
705         sop_queue_idx = enic_rte_rq_idx_to_sop_idx(rx_queue_id);
706         data_queue_idx = enic_rte_rq_idx_to_data_idx(rx_queue_id);
707         rq_sop = &enic->rq[sop_queue_idx];
708         rq_data = &enic->rq[data_queue_idx]; /* valid if data_queue_enable */
709         qinfo->mp = rq_sop->mp;
710         qinfo->scattered_rx = rq_sop->data_queue_enable;
711         qinfo->nb_desc = rq_sop->ring.desc_count;
712         if (qinfo->scattered_rx)
713                 qinfo->nb_desc += rq_data->ring.desc_count;
714         conf = &qinfo->conf;
715         memset(conf, 0, sizeof(*conf));
716         conf->rx_free_thresh = rq_sop->rx_free_thresh;
717         conf->rx_drop_en = 1;
718         /*
719          * Except VLAN stripping (port setting), all the checksum offloads
720          * are always enabled.
721          */
722         conf->offloads = ENIC_RX_OFFLOAD_CAPA;
723         if (!enic->ig_vlan_strip_en)
724                 conf->offloads &= ~DEV_RX_OFFLOAD_VLAN_STRIP;
725         /* rx_thresh and other fields are not applicable for enic */
726 }
727
728 static void enicpmd_dev_txq_info_get(struct rte_eth_dev *dev,
729                                      __rte_unused uint16_t tx_queue_id,
730                                      struct rte_eth_txq_info *qinfo)
731 {
732         struct enic *enic = pmd_priv(dev);
733
734         ENICPMD_FUNC_TRACE();
735         qinfo->nb_desc = enic->config.wq_desc_count;
736         memset(&qinfo->conf, 0, sizeof(qinfo->conf));
737         qinfo->conf.offloads = ENIC_TX_OFFLOAD_CAPA; /* not configurable */
738         /* tx_thresh, and all the other fields are not applicable for enic */
739 }
740
741 static const struct eth_dev_ops enicpmd_eth_dev_ops = {
742         .dev_configure        = enicpmd_dev_configure,
743         .dev_start            = enicpmd_dev_start,
744         .dev_stop             = enicpmd_dev_stop,
745         .dev_set_link_up      = NULL,
746         .dev_set_link_down    = NULL,
747         .dev_close            = enicpmd_dev_close,
748         .promiscuous_enable   = enicpmd_dev_promiscuous_enable,
749         .promiscuous_disable  = enicpmd_dev_promiscuous_disable,
750         .allmulticast_enable  = enicpmd_dev_allmulticast_enable,
751         .allmulticast_disable = enicpmd_dev_allmulticast_disable,
752         .link_update          = enicpmd_dev_link_update,
753         .stats_get            = enicpmd_dev_stats_get,
754         .stats_reset          = enicpmd_dev_stats_reset,
755         .queue_stats_mapping_set = NULL,
756         .dev_infos_get        = enicpmd_dev_info_get,
757         .dev_supported_ptypes_get = enicpmd_dev_supported_ptypes_get,
758         .mtu_set              = enicpmd_mtu_set,
759         .vlan_filter_set      = NULL,
760         .vlan_tpid_set        = NULL,
761         .vlan_offload_set     = enicpmd_vlan_offload_set,
762         .vlan_strip_queue_set = NULL,
763         .rx_queue_start       = enicpmd_dev_rx_queue_start,
764         .rx_queue_stop        = enicpmd_dev_rx_queue_stop,
765         .tx_queue_start       = enicpmd_dev_tx_queue_start,
766         .tx_queue_stop        = enicpmd_dev_tx_queue_stop,
767         .rx_queue_setup       = enicpmd_dev_rx_queue_setup,
768         .rx_queue_release     = enicpmd_dev_rx_queue_release,
769         .rx_queue_count       = enicpmd_dev_rx_queue_count,
770         .rx_descriptor_done   = NULL,
771         .tx_queue_setup       = enicpmd_dev_tx_queue_setup,
772         .tx_queue_release     = enicpmd_dev_tx_queue_release,
773         .rxq_info_get         = enicpmd_dev_rxq_info_get,
774         .txq_info_get         = enicpmd_dev_txq_info_get,
775         .dev_led_on           = NULL,
776         .dev_led_off          = NULL,
777         .flow_ctrl_get        = NULL,
778         .flow_ctrl_set        = NULL,
779         .priority_flow_ctrl_set = NULL,
780         .mac_addr_add         = enicpmd_add_mac_addr,
781         .mac_addr_remove      = enicpmd_remove_mac_addr,
782         .filter_ctrl          = enicpmd_dev_filter_ctrl,
783         .reta_query           = enicpmd_dev_rss_reta_query,
784         .reta_update          = enicpmd_dev_rss_reta_update,
785         .rss_hash_conf_get    = enicpmd_dev_rss_hash_conf_get,
786         .rss_hash_update      = enicpmd_dev_rss_hash_update,
787 };
788
789 struct enic *enicpmd_list_head = NULL;
790 /* Initialize the driver
791  * It returns 0 on success.
792  */
793 static int eth_enicpmd_dev_init(struct rte_eth_dev *eth_dev)
794 {
795         struct rte_pci_device *pdev;
796         struct rte_pci_addr *addr;
797         struct enic *enic = pmd_priv(eth_dev);
798
799         ENICPMD_FUNC_TRACE();
800
801         enic->port_id = eth_dev->data->port_id;
802         enic->rte_dev = eth_dev;
803         eth_dev->dev_ops = &enicpmd_eth_dev_ops;
804         eth_dev->rx_pkt_burst = &enic_recv_pkts;
805         eth_dev->tx_pkt_burst = &enic_xmit_pkts;
806         eth_dev->tx_pkt_prepare = &enic_prep_pkts;
807
808         pdev = RTE_ETH_DEV_TO_PCI(eth_dev);
809         rte_eth_copy_pci_info(eth_dev, pdev);
810         enic->pdev = pdev;
811         addr = &pdev->addr;
812
813         snprintf(enic->bdf_name, ENICPMD_BDF_LENGTH, "%04x:%02x:%02x.%x",
814                 addr->domain, addr->bus, addr->devid, addr->function);
815
816         return enic_probe(enic);
817 }
818
819 static int eth_enic_pci_probe(struct rte_pci_driver *pci_drv __rte_unused,
820         struct rte_pci_device *pci_dev)
821 {
822         return rte_eth_dev_pci_generic_probe(pci_dev, sizeof(struct enic),
823                 eth_enicpmd_dev_init);
824 }
825
826 static int eth_enic_pci_remove(struct rte_pci_device *pci_dev)
827 {
828         return rte_eth_dev_pci_generic_remove(pci_dev, NULL);
829 }
830
831 static struct rte_pci_driver rte_enic_pmd = {
832         .id_table = pci_id_enic_map,
833         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
834         .probe = eth_enic_pci_probe,
835         .remove = eth_enic_pci_remove,
836 };
837
838 RTE_PMD_REGISTER_PCI(net_enic, rte_enic_pmd);
839 RTE_PMD_REGISTER_PCI_TABLE(net_enic, pci_id_enic_map);
840 RTE_PMD_REGISTER_KMOD_DEP(net_enic, "* igb_uio | uio_pci_generic | vfio-pci");