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