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