ethdev: change promiscuous callbacks to return status
[dpdk.git] / drivers / net / bnx2x / bnx2x_ethdev.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  * Copyright (c) 2013-2015 Brocade Communications Systems, Inc.
3  * Copyright (c) 2015-2018 Cavium Inc.
4  * All rights reserved.
5  * www.cavium.com
6  */
7
8 #include "bnx2x.h"
9 #include "bnx2x_rxtx.h"
10
11 #include <rte_string_fns.h>
12 #include <rte_dev.h>
13 #include <rte_ethdev_pci.h>
14 #include <rte_alarm.h>
15
16 int bnx2x_logtype_init;
17 int bnx2x_logtype_driver;
18
19 /*
20  * The set of PCI devices this driver supports
21  */
22 #define BROADCOM_PCI_VENDOR_ID 0x14E4
23 static const struct rte_pci_id pci_id_bnx2x_map[] = {
24         { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57800) },
25         { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57711) },
26         { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57810) },
27         { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57811) },
28         { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57840_OBS) },
29         { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57840_4_10) },
30         { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57840_2_20) },
31 #ifdef RTE_LIBRTE_BNX2X_MF_SUPPORT
32         { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57810_MF) },
33         { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57811_MF) },
34         { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57840_MF) },
35 #endif
36         { .vendor_id = 0, }
37 };
38
39 static const struct rte_pci_id pci_id_bnx2xvf_map[] = {
40         { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57800_VF) },
41         { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57810_VF) },
42         { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57811_VF) },
43         { RTE_PCI_DEVICE(BROADCOM_PCI_VENDOR_ID, CHIP_NUM_57840_VF) },
44         { .vendor_id = 0, }
45 };
46
47 struct rte_bnx2x_xstats_name_off {
48         char name[RTE_ETH_XSTATS_NAME_SIZE];
49         uint32_t offset_hi;
50         uint32_t offset_lo;
51 };
52
53 static const struct rte_bnx2x_xstats_name_off bnx2x_xstats_strings[] = {
54         {"rx_buffer_drops",
55                 offsetof(struct bnx2x_eth_stats, brb_drop_hi),
56                 offsetof(struct bnx2x_eth_stats, brb_drop_lo)},
57         {"rx_buffer_truncates",
58                 offsetof(struct bnx2x_eth_stats, brb_truncate_hi),
59                 offsetof(struct bnx2x_eth_stats, brb_truncate_lo)},
60         {"rx_buffer_truncate_discard",
61                 offsetof(struct bnx2x_eth_stats, brb_truncate_discard),
62                 offsetof(struct bnx2x_eth_stats, brb_truncate_discard)},
63         {"mac_filter_discard",
64                 offsetof(struct bnx2x_eth_stats, mac_filter_discard),
65                 offsetof(struct bnx2x_eth_stats, mac_filter_discard)},
66         {"no_match_vlan_tag_discard",
67                 offsetof(struct bnx2x_eth_stats, mf_tag_discard),
68                 offsetof(struct bnx2x_eth_stats, mf_tag_discard)},
69         {"tx_pause",
70                 offsetof(struct bnx2x_eth_stats, pause_frames_sent_hi),
71                 offsetof(struct bnx2x_eth_stats, pause_frames_sent_lo)},
72         {"rx_pause",
73                 offsetof(struct bnx2x_eth_stats, pause_frames_received_hi),
74                 offsetof(struct bnx2x_eth_stats, pause_frames_received_lo)},
75         {"tx_priority_flow_control",
76                 offsetof(struct bnx2x_eth_stats, pfc_frames_sent_hi),
77                 offsetof(struct bnx2x_eth_stats, pfc_frames_sent_lo)},
78         {"rx_priority_flow_control",
79                 offsetof(struct bnx2x_eth_stats, pfc_frames_received_hi),
80                 offsetof(struct bnx2x_eth_stats, pfc_frames_received_lo)}
81 };
82
83 static int
84 bnx2x_link_update(struct rte_eth_dev *dev)
85 {
86         struct bnx2x_softc *sc = dev->data->dev_private;
87         struct rte_eth_link link;
88
89         PMD_INIT_FUNC_TRACE(sc);
90
91         memset(&link, 0, sizeof(link));
92         mb();
93         link.link_speed = sc->link_vars.line_speed;
94         switch (sc->link_vars.duplex) {
95                 case DUPLEX_FULL:
96                         link.link_duplex = ETH_LINK_FULL_DUPLEX;
97                         break;
98                 case DUPLEX_HALF:
99                         link.link_duplex = ETH_LINK_HALF_DUPLEX;
100                         break;
101         }
102         link.link_autoneg = !(dev->data->dev_conf.link_speeds &
103                         ETH_LINK_SPEED_FIXED);
104         link.link_status = sc->link_vars.link_up;
105
106         return rte_eth_linkstatus_set(dev, &link);
107 }
108
109 static void
110 bnx2x_interrupt_action(struct rte_eth_dev *dev, int intr_cxt)
111 {
112         struct bnx2x_softc *sc = dev->data->dev_private;
113         uint32_t link_status;
114
115         bnx2x_intr_legacy(sc);
116
117         if ((atomic_load_acq_long(&sc->periodic_flags) == PERIODIC_GO) &&
118             !intr_cxt)
119                 bnx2x_periodic_callout(sc);
120         link_status = REG_RD(sc, sc->link_params.shmem_base +
121                         offsetof(struct shmem_region,
122                                 port_mb[sc->link_params.port].link_status));
123         if ((link_status & LINK_STATUS_LINK_UP) != dev->data->dev_link.link_status)
124                 bnx2x_link_update(dev);
125 }
126
127 static void
128 bnx2x_interrupt_handler(void *param)
129 {
130         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
131         struct bnx2x_softc *sc = dev->data->dev_private;
132
133         PMD_DEBUG_PERIODIC_LOG(INFO, sc, "Interrupt handled");
134
135         bnx2x_interrupt_action(dev, 1);
136         rte_intr_ack(&sc->pci_dev->intr_handle);
137 }
138
139 static void bnx2x_periodic_start(void *param)
140 {
141         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
142         struct bnx2x_softc *sc = dev->data->dev_private;
143         int ret = 0;
144
145         atomic_store_rel_long(&sc->periodic_flags, PERIODIC_GO);
146         bnx2x_interrupt_action(dev, 0);
147         if (IS_PF(sc)) {
148                 ret = rte_eal_alarm_set(BNX2X_SP_TIMER_PERIOD,
149                                         bnx2x_periodic_start, (void *)dev);
150                 if (ret) {
151                         PMD_DRV_LOG(ERR, sc, "Unable to start periodic"
152                                              " timer rc %d", ret);
153                 }
154         }
155 }
156
157 void bnx2x_periodic_stop(void *param)
158 {
159         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
160         struct bnx2x_softc *sc = dev->data->dev_private;
161
162         atomic_store_rel_long(&sc->periodic_flags, PERIODIC_STOP);
163
164         rte_eal_alarm_cancel(bnx2x_periodic_start, (void *)dev);
165
166         PMD_DRV_LOG(DEBUG, sc, "Periodic poll stopped");
167 }
168
169 /*
170  * Devops - helper functions can be called from user application
171  */
172
173 static int
174 bnx2x_dev_configure(struct rte_eth_dev *dev)
175 {
176         struct bnx2x_softc *sc = dev->data->dev_private;
177         struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
178
179         int mp_ncpus = sysconf(_SC_NPROCESSORS_CONF);
180
181         PMD_INIT_FUNC_TRACE(sc);
182
183         if (rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
184                 sc->mtu = dev->data->dev_conf.rxmode.max_rx_pkt_len;
185                 dev->data->mtu = sc->mtu;
186         }
187
188         if (dev->data->nb_tx_queues > dev->data->nb_rx_queues) {
189                 PMD_DRV_LOG(ERR, sc, "The number of TX queues is greater than number of RX queues");
190                 return -EINVAL;
191         }
192
193         sc->num_queues = MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
194         if (sc->num_queues > mp_ncpus) {
195                 PMD_DRV_LOG(ERR, sc, "The number of queues is more than number of CPUs");
196                 return -EINVAL;
197         }
198
199         PMD_DRV_LOG(DEBUG, sc, "num_queues=%d, mtu=%d",
200                        sc->num_queues, sc->mtu);
201
202         /* allocate ilt */
203         if (bnx2x_alloc_ilt_mem(sc) != 0) {
204                 PMD_DRV_LOG(ERR, sc, "bnx2x_alloc_ilt_mem was failed");
205                 return -ENXIO;
206         }
207
208         bnx2x_dev_rxtx_init_dummy(dev);
209         return 0;
210 }
211
212 static int
213 bnx2x_dev_start(struct rte_eth_dev *dev)
214 {
215         struct bnx2x_softc *sc = dev->data->dev_private;
216         int ret = 0;
217
218         PMD_INIT_FUNC_TRACE(sc);
219
220         /* start the periodic callout */
221         if (IS_PF(sc)) {
222                 if (atomic_load_acq_long(&sc->periodic_flags) ==
223                     PERIODIC_STOP) {
224                         bnx2x_periodic_start(dev);
225                         PMD_DRV_LOG(DEBUG, sc, "Periodic poll re-started");
226                 }
227         }
228
229         ret = bnx2x_init(sc);
230         if (ret) {
231                 PMD_DRV_LOG(DEBUG, sc, "bnx2x_init failed (%d)", ret);
232                 return -1;
233         }
234
235         if (IS_PF(sc)) {
236                 rte_intr_callback_register(&sc->pci_dev->intr_handle,
237                                 bnx2x_interrupt_handler, (void *)dev);
238
239                 if (rte_intr_enable(&sc->pci_dev->intr_handle))
240                         PMD_DRV_LOG(ERR, sc, "rte_intr_enable failed");
241         }
242
243         bnx2x_dev_rxtx_init(dev);
244
245         bnx2x_print_device_info(sc);
246
247         return ret;
248 }
249
250 static void
251 bnx2x_dev_stop(struct rte_eth_dev *dev)
252 {
253         struct bnx2x_softc *sc = dev->data->dev_private;
254         int ret = 0;
255
256         PMD_INIT_FUNC_TRACE(sc);
257
258         bnx2x_dev_rxtx_init_dummy(dev);
259
260         if (IS_PF(sc)) {
261                 rte_intr_disable(&sc->pci_dev->intr_handle);
262                 rte_intr_callback_unregister(&sc->pci_dev->intr_handle,
263                                 bnx2x_interrupt_handler, (void *)dev);
264
265                 /* stop the periodic callout */
266                 bnx2x_periodic_stop(dev);
267         }
268
269         ret = bnx2x_nic_unload(sc, UNLOAD_NORMAL, FALSE);
270         if (ret) {
271                 PMD_DRV_LOG(DEBUG, sc, "bnx2x_nic_unload failed (%d)", ret);
272                 return;
273         }
274
275         return;
276 }
277
278 static void
279 bnx2x_dev_close(struct rte_eth_dev *dev)
280 {
281         struct bnx2x_softc *sc = dev->data->dev_private;
282
283         PMD_INIT_FUNC_TRACE(sc);
284
285         if (IS_VF(sc))
286                 bnx2x_vf_close(sc);
287
288         bnx2x_dev_clear_queues(dev);
289         memset(&(dev->data->dev_link), 0 , sizeof(struct rte_eth_link));
290
291         /* free ilt */
292         bnx2x_free_ilt_mem(sc);
293 }
294
295 static int
296 bnx2x_promisc_enable(struct rte_eth_dev *dev)
297 {
298         struct bnx2x_softc *sc = dev->data->dev_private;
299
300         PMD_INIT_FUNC_TRACE(sc);
301         sc->rx_mode = BNX2X_RX_MODE_PROMISC;
302         if (rte_eth_allmulticast_get(dev->data->port_id) == 1)
303                 sc->rx_mode = BNX2X_RX_MODE_ALLMULTI_PROMISC;
304         bnx2x_set_rx_mode(sc);
305
306         return 0;
307 }
308
309 static int
310 bnx2x_promisc_disable(struct rte_eth_dev *dev)
311 {
312         struct bnx2x_softc *sc = dev->data->dev_private;
313
314         PMD_INIT_FUNC_TRACE(sc);
315         sc->rx_mode = BNX2X_RX_MODE_NORMAL;
316         if (rte_eth_allmulticast_get(dev->data->port_id) == 1)
317                 sc->rx_mode = BNX2X_RX_MODE_ALLMULTI;
318         bnx2x_set_rx_mode(sc);
319
320         return 0;
321 }
322
323 static void
324 bnx2x_dev_allmulticast_enable(struct rte_eth_dev *dev)
325 {
326         struct bnx2x_softc *sc = dev->data->dev_private;
327
328         PMD_INIT_FUNC_TRACE(sc);
329         sc->rx_mode = BNX2X_RX_MODE_ALLMULTI;
330         if (rte_eth_promiscuous_get(dev->data->port_id) == 1)
331                 sc->rx_mode = BNX2X_RX_MODE_ALLMULTI_PROMISC;
332         bnx2x_set_rx_mode(sc);
333 }
334
335 static void
336 bnx2x_dev_allmulticast_disable(struct rte_eth_dev *dev)
337 {
338         struct bnx2x_softc *sc = dev->data->dev_private;
339
340         PMD_INIT_FUNC_TRACE(sc);
341         sc->rx_mode = BNX2X_RX_MODE_NORMAL;
342         if (rte_eth_promiscuous_get(dev->data->port_id) == 1)
343                 sc->rx_mode = BNX2X_RX_MODE_PROMISC;
344         bnx2x_set_rx_mode(sc);
345 }
346
347 static int
348 bnx2x_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
349 {
350         struct bnx2x_softc *sc = dev->data->dev_private;
351
352         PMD_INIT_FUNC_TRACE(sc);
353
354         return bnx2x_link_update(dev);
355 }
356
357 static int
358 bnx2xvf_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
359 {
360         struct bnx2x_softc *sc = dev->data->dev_private;
361         int ret = 0;
362
363         ret = bnx2x_link_update(dev);
364
365         bnx2x_check_bull(sc);
366         if (sc->old_bulletin.valid_bitmap & (1 << CHANNEL_DOWN)) {
367                 PMD_DRV_LOG(ERR, sc, "PF indicated channel is down."
368                                 "VF device is no longer operational");
369                 dev->data->dev_link.link_status = ETH_LINK_DOWN;
370         }
371
372         return ret;
373 }
374
375 static int
376 bnx2x_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
377 {
378         struct bnx2x_softc *sc = dev->data->dev_private;
379         uint32_t brb_truncate_discard;
380         uint64_t brb_drops;
381         uint64_t brb_truncates;
382
383         PMD_INIT_FUNC_TRACE(sc);
384
385         bnx2x_stats_handle(sc, STATS_EVENT_UPDATE);
386
387         memset(stats, 0, sizeof (struct rte_eth_stats));
388
389         stats->ipackets =
390                 HILO_U64(sc->eth_stats.total_unicast_packets_received_hi,
391                                 sc->eth_stats.total_unicast_packets_received_lo) +
392                 HILO_U64(sc->eth_stats.total_multicast_packets_received_hi,
393                                 sc->eth_stats.total_multicast_packets_received_lo) +
394                 HILO_U64(sc->eth_stats.total_broadcast_packets_received_hi,
395                                 sc->eth_stats.total_broadcast_packets_received_lo);
396
397         stats->opackets =
398                 HILO_U64(sc->eth_stats.total_unicast_packets_transmitted_hi,
399                                 sc->eth_stats.total_unicast_packets_transmitted_lo) +
400                 HILO_U64(sc->eth_stats.total_multicast_packets_transmitted_hi,
401                                 sc->eth_stats.total_multicast_packets_transmitted_lo) +
402                 HILO_U64(sc->eth_stats.total_broadcast_packets_transmitted_hi,
403                                 sc->eth_stats.total_broadcast_packets_transmitted_lo);
404
405         stats->ibytes =
406                 HILO_U64(sc->eth_stats.total_bytes_received_hi,
407                                 sc->eth_stats.total_bytes_received_lo);
408
409         stats->obytes =
410                 HILO_U64(sc->eth_stats.total_bytes_transmitted_hi,
411                                 sc->eth_stats.total_bytes_transmitted_lo);
412
413         stats->ierrors =
414                 HILO_U64(sc->eth_stats.error_bytes_received_hi,
415                                 sc->eth_stats.error_bytes_received_lo);
416
417         stats->oerrors = 0;
418
419         stats->rx_nombuf =
420                 HILO_U64(sc->eth_stats.no_buff_discard_hi,
421                                 sc->eth_stats.no_buff_discard_lo);
422
423         brb_drops =
424                 HILO_U64(sc->eth_stats.brb_drop_hi,
425                          sc->eth_stats.brb_drop_lo);
426
427         brb_truncates =
428                 HILO_U64(sc->eth_stats.brb_truncate_hi,
429                          sc->eth_stats.brb_truncate_lo);
430
431         brb_truncate_discard = sc->eth_stats.brb_truncate_discard;
432
433         stats->imissed = brb_drops + brb_truncates +
434                          brb_truncate_discard + stats->rx_nombuf;
435
436         return 0;
437 }
438
439 static int
440 bnx2x_get_xstats_names(__rte_unused struct rte_eth_dev *dev,
441                        struct rte_eth_xstat_name *xstats_names,
442                        __rte_unused unsigned limit)
443 {
444         unsigned int i, stat_cnt = RTE_DIM(bnx2x_xstats_strings);
445
446         if (xstats_names != NULL)
447                 for (i = 0; i < stat_cnt; i++)
448                         strlcpy(xstats_names[i].name,
449                                 bnx2x_xstats_strings[i].name,
450                                 sizeof(xstats_names[i].name));
451
452         return stat_cnt;
453 }
454
455 static int
456 bnx2x_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
457                      unsigned int n)
458 {
459         struct bnx2x_softc *sc = dev->data->dev_private;
460         unsigned int num = RTE_DIM(bnx2x_xstats_strings);
461
462         if (n < num)
463                 return num;
464
465         bnx2x_stats_handle(sc, STATS_EVENT_UPDATE);
466
467         for (num = 0; num < n; num++) {
468                 if (bnx2x_xstats_strings[num].offset_hi !=
469                     bnx2x_xstats_strings[num].offset_lo)
470                         xstats[num].value = HILO_U64(
471                                           *(uint32_t *)((char *)&sc->eth_stats +
472                                           bnx2x_xstats_strings[num].offset_hi),
473                                           *(uint32_t *)((char *)&sc->eth_stats +
474                                           bnx2x_xstats_strings[num].offset_lo));
475                 else
476                         xstats[num].value =
477                                           *(uint64_t *)((char *)&sc->eth_stats +
478                                           bnx2x_xstats_strings[num].offset_lo);
479                 xstats[num].id = num;
480         }
481
482         return num;
483 }
484
485 static int
486 bnx2x_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
487 {
488         struct bnx2x_softc *sc = dev->data->dev_private;
489
490         dev_info->max_rx_queues  = sc->max_rx_queues;
491         dev_info->max_tx_queues  = sc->max_tx_queues;
492         dev_info->min_rx_bufsize = BNX2X_MIN_RX_BUF_SIZE;
493         dev_info->max_rx_pktlen  = BNX2X_MAX_RX_PKT_LEN;
494         dev_info->max_mac_addrs  = BNX2X_MAX_MAC_ADDRS;
495         dev_info->speed_capa = ETH_LINK_SPEED_10G | ETH_LINK_SPEED_20G;
496         dev_info->rx_offload_capa = DEV_RX_OFFLOAD_JUMBO_FRAME;
497
498         dev_info->rx_desc_lim.nb_max = MAX_RX_AVAIL;
499         dev_info->rx_desc_lim.nb_min = MIN_RX_SIZE_NONTPA;
500         dev_info->tx_desc_lim.nb_max = MAX_TX_AVAIL;
501
502         return 0;
503 }
504
505 static int
506 bnx2x_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
507                 uint32_t index, uint32_t pool)
508 {
509         struct bnx2x_softc *sc = dev->data->dev_private;
510
511         if (sc->mac_ops.mac_addr_add) {
512                 sc->mac_ops.mac_addr_add(dev, mac_addr, index, pool);
513                 return 0;
514         }
515         return -ENOTSUP;
516 }
517
518 static void
519 bnx2x_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
520 {
521         struct bnx2x_softc *sc = dev->data->dev_private;
522
523         if (sc->mac_ops.mac_addr_remove)
524                 sc->mac_ops.mac_addr_remove(dev, index);
525 }
526
527 static const struct eth_dev_ops bnx2x_eth_dev_ops = {
528         .dev_configure                = bnx2x_dev_configure,
529         .dev_start                    = bnx2x_dev_start,
530         .dev_stop                     = bnx2x_dev_stop,
531         .dev_close                    = bnx2x_dev_close,
532         .promiscuous_enable           = bnx2x_promisc_enable,
533         .promiscuous_disable          = bnx2x_promisc_disable,
534         .allmulticast_enable          = bnx2x_dev_allmulticast_enable,
535         .allmulticast_disable         = bnx2x_dev_allmulticast_disable,
536         .link_update                  = bnx2x_dev_link_update,
537         .stats_get                    = bnx2x_dev_stats_get,
538         .xstats_get                   = bnx2x_dev_xstats_get,
539         .xstats_get_names             = bnx2x_get_xstats_names,
540         .dev_infos_get                = bnx2x_dev_infos_get,
541         .rx_queue_setup               = bnx2x_dev_rx_queue_setup,
542         .rx_queue_release             = bnx2x_dev_rx_queue_release,
543         .tx_queue_setup               = bnx2x_dev_tx_queue_setup,
544         .tx_queue_release             = bnx2x_dev_tx_queue_release,
545         .mac_addr_add                 = bnx2x_mac_addr_add,
546         .mac_addr_remove              = bnx2x_mac_addr_remove,
547 };
548
549 /*
550  * dev_ops for virtual function
551  */
552 static const struct eth_dev_ops bnx2xvf_eth_dev_ops = {
553         .dev_configure                = bnx2x_dev_configure,
554         .dev_start                    = bnx2x_dev_start,
555         .dev_stop                     = bnx2x_dev_stop,
556         .dev_close                    = bnx2x_dev_close,
557         .promiscuous_enable           = bnx2x_promisc_enable,
558         .promiscuous_disable          = bnx2x_promisc_disable,
559         .allmulticast_enable          = bnx2x_dev_allmulticast_enable,
560         .allmulticast_disable         = bnx2x_dev_allmulticast_disable,
561         .link_update                  = bnx2xvf_dev_link_update,
562         .stats_get                    = bnx2x_dev_stats_get,
563         .xstats_get                   = bnx2x_dev_xstats_get,
564         .xstats_get_names             = bnx2x_get_xstats_names,
565         .dev_infos_get                = bnx2x_dev_infos_get,
566         .rx_queue_setup               = bnx2x_dev_rx_queue_setup,
567         .rx_queue_release             = bnx2x_dev_rx_queue_release,
568         .tx_queue_setup               = bnx2x_dev_tx_queue_setup,
569         .tx_queue_release             = bnx2x_dev_tx_queue_release,
570         .mac_addr_add                 = bnx2x_mac_addr_add,
571         .mac_addr_remove              = bnx2x_mac_addr_remove,
572 };
573
574
575 static int
576 bnx2x_common_dev_init(struct rte_eth_dev *eth_dev, int is_vf)
577 {
578         int ret = 0;
579         struct rte_pci_device *pci_dev;
580         struct rte_pci_addr pci_addr;
581         struct bnx2x_softc *sc;
582         static bool adapter_info = true;
583
584         /* Extract key data structures */
585         sc = eth_dev->data->dev_private;
586         pci_dev = RTE_DEV_TO_PCI(eth_dev->device);
587         pci_addr = pci_dev->addr;
588
589         snprintf(sc->devinfo.name, NAME_SIZE, PCI_SHORT_PRI_FMT ":dpdk-port-%u",
590                  pci_addr.bus, pci_addr.devid, pci_addr.function,
591                  eth_dev->data->port_id);
592
593         PMD_INIT_FUNC_TRACE(sc);
594
595         eth_dev->dev_ops = is_vf ? &bnx2xvf_eth_dev_ops : &bnx2x_eth_dev_ops;
596
597         rte_eth_copy_pci_info(eth_dev, pci_dev);
598
599         sc->pcie_bus    = pci_dev->addr.bus;
600         sc->pcie_device = pci_dev->addr.devid;
601
602         sc->devinfo.vendor_id    = pci_dev->id.vendor_id;
603         sc->devinfo.device_id    = pci_dev->id.device_id;
604         sc->devinfo.subvendor_id = pci_dev->id.subsystem_vendor_id;
605         sc->devinfo.subdevice_id = pci_dev->id.subsystem_device_id;
606
607         if (is_vf)
608                 sc->flags = BNX2X_IS_VF_FLAG;
609
610         sc->pcie_func = pci_dev->addr.function;
611         sc->bar[BAR0].base_addr = (void *)pci_dev->mem_resource[0].addr;
612         if (is_vf)
613                 sc->bar[BAR1].base_addr = (void *)
614                         ((uintptr_t)pci_dev->mem_resource[0].addr + PXP_VF_ADDR_DB_START);
615         else
616                 sc->bar[BAR1].base_addr = pci_dev->mem_resource[2].addr;
617
618         assert(sc->bar[BAR0].base_addr);
619         assert(sc->bar[BAR1].base_addr);
620
621         bnx2x_load_firmware(sc);
622         assert(sc->firmware);
623
624         if (eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf & ETH_RSS_NONFRAG_IPV4_UDP)
625                 sc->udp_rss = 1;
626
627         sc->rx_budget = BNX2X_RX_BUDGET;
628         sc->hc_rx_ticks = BNX2X_RX_TICKS;
629         sc->hc_tx_ticks = BNX2X_TX_TICKS;
630
631         sc->interrupt_mode = INTR_MODE_SINGLE_MSIX;
632         sc->rx_mode = BNX2X_RX_MODE_NORMAL;
633
634         sc->pci_dev = pci_dev;
635         ret = bnx2x_attach(sc);
636         if (ret) {
637                 PMD_DRV_LOG(ERR, sc, "bnx2x_attach failed (%d)", ret);
638                 return ret;
639         }
640
641         /* Print important adapter info for the user. */
642         if (adapter_info) {
643                 bnx2x_print_adapter_info(sc);
644                 adapter_info = false;
645         }
646
647         /* schedule periodic poll for slowpath link events */
648         if (IS_PF(sc)) {
649                 PMD_DRV_LOG(DEBUG, sc, "Scheduling periodic poll for slowpath link events");
650                 ret = rte_eal_alarm_set(BNX2X_SP_TIMER_PERIOD,
651                                         bnx2x_periodic_start, (void *)eth_dev);
652                 if (ret) {
653                         PMD_DRV_LOG(ERR, sc, "Unable to start periodic"
654                                              " timer rc %d", ret);
655                         return -EINVAL;
656                 }
657         }
658
659         eth_dev->data->mac_addrs =
660                 (struct rte_ether_addr *)sc->link_params.mac_addr;
661
662         if (IS_VF(sc)) {
663                 rte_spinlock_init(&sc->vf2pf_lock);
664
665                 ret = bnx2x_dma_alloc(sc, sizeof(struct bnx2x_vf_mbx_msg),
666                                       &sc->vf2pf_mbox_mapping, "vf2pf_mbox",
667                                       RTE_CACHE_LINE_SIZE);
668                 if (ret)
669                         goto out;
670
671                 sc->vf2pf_mbox = (struct bnx2x_vf_mbx_msg *)
672                                          sc->vf2pf_mbox_mapping.vaddr;
673
674                 ret = bnx2x_dma_alloc(sc, sizeof(struct bnx2x_vf_bulletin),
675                                       &sc->pf2vf_bulletin_mapping, "vf2pf_bull",
676                                       RTE_CACHE_LINE_SIZE);
677                 if (ret)
678                         goto out;
679
680                 sc->pf2vf_bulletin = (struct bnx2x_vf_bulletin *)
681                                              sc->pf2vf_bulletin_mapping.vaddr;
682
683                 ret = bnx2x_vf_get_resources(sc, sc->max_tx_queues,
684                                              sc->max_rx_queues);
685                 if (ret)
686                         goto out;
687         }
688
689         return 0;
690
691 out:
692         if (IS_PF(sc))
693                 bnx2x_periodic_stop(eth_dev);
694
695         return ret;
696 }
697
698 static int
699 eth_bnx2x_dev_init(struct rte_eth_dev *eth_dev)
700 {
701         struct bnx2x_softc *sc = eth_dev->data->dev_private;
702         PMD_INIT_FUNC_TRACE(sc);
703         return bnx2x_common_dev_init(eth_dev, 0);
704 }
705
706 static int
707 eth_bnx2xvf_dev_init(struct rte_eth_dev *eth_dev)
708 {
709         struct bnx2x_softc *sc = eth_dev->data->dev_private;
710         PMD_INIT_FUNC_TRACE(sc);
711         return bnx2x_common_dev_init(eth_dev, 1);
712 }
713
714 static int eth_bnx2x_dev_uninit(struct rte_eth_dev *eth_dev)
715 {
716         /* mac_addrs must not be freed alone because part of dev_private */
717         eth_dev->data->mac_addrs = NULL;
718         return 0;
719 }
720
721 static struct rte_pci_driver rte_bnx2x_pmd;
722 static struct rte_pci_driver rte_bnx2xvf_pmd;
723
724 static int eth_bnx2x_pci_probe(struct rte_pci_driver *pci_drv,
725         struct rte_pci_device *pci_dev)
726 {
727         if (pci_drv == &rte_bnx2x_pmd)
728                 return rte_eth_dev_pci_generic_probe(pci_dev,
729                                 sizeof(struct bnx2x_softc), eth_bnx2x_dev_init);
730         else if (pci_drv == &rte_bnx2xvf_pmd)
731                 return rte_eth_dev_pci_generic_probe(pci_dev,
732                                 sizeof(struct bnx2x_softc), eth_bnx2xvf_dev_init);
733         else
734                 return -EINVAL;
735 }
736
737 static int eth_bnx2x_pci_remove(struct rte_pci_device *pci_dev)
738 {
739         return rte_eth_dev_pci_generic_remove(pci_dev, eth_bnx2x_dev_uninit);
740 }
741
742 static struct rte_pci_driver rte_bnx2x_pmd = {
743         .id_table = pci_id_bnx2x_map,
744         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
745         .probe = eth_bnx2x_pci_probe,
746         .remove = eth_bnx2x_pci_remove,
747 };
748
749 /*
750  * virtual function driver struct
751  */
752 static struct rte_pci_driver rte_bnx2xvf_pmd = {
753         .id_table = pci_id_bnx2xvf_map,
754         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
755         .probe = eth_bnx2x_pci_probe,
756         .remove = eth_bnx2x_pci_remove,
757 };
758
759 RTE_PMD_REGISTER_PCI(net_bnx2x, rte_bnx2x_pmd);
760 RTE_PMD_REGISTER_PCI_TABLE(net_bnx2x, pci_id_bnx2x_map);
761 RTE_PMD_REGISTER_KMOD_DEP(net_bnx2x, "* igb_uio | uio_pci_generic | vfio-pci");
762 RTE_PMD_REGISTER_PCI(net_bnx2xvf, rte_bnx2xvf_pmd);
763 RTE_PMD_REGISTER_PCI_TABLE(net_bnx2xvf, pci_id_bnx2xvf_map);
764 RTE_PMD_REGISTER_KMOD_DEP(net_bnx2xvf, "* igb_uio | vfio-pci");
765
766 RTE_INIT(bnx2x_init_log)
767 {
768         bnx2x_logtype_init = rte_log_register("pmd.net.bnx2x.init");
769         if (bnx2x_logtype_init >= 0)
770                 rte_log_set_level(bnx2x_logtype_init, RTE_LOG_NOTICE);
771         bnx2x_logtype_driver = rte_log_register("pmd.net.bnx2x.driver");
772         if (bnx2x_logtype_driver >= 0)
773                 rte_log_set_level(bnx2x_logtype_driver, RTE_LOG_NOTICE);
774 }