net/bnx2x: fix MTU for jumbo frame
[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)
112 {
113         struct bnx2x_softc *sc = dev->data->dev_private;
114         uint32_t link_status;
115
116         bnx2x_intr_legacy(sc, 0);
117
118         if (sc->periodic_flags & PERIODIC_GO)
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         atomic_store_rel_long(&sc->periodic_flags, PERIODIC_STOP);
136         bnx2x_interrupt_action(dev);
137         atomic_store_rel_long(&sc->periodic_flags, PERIODIC_GO);
138         rte_intr_enable(&sc->pci_dev->intr_handle);
139 }
140
141 static void bnx2x_periodic_start(void *param)
142 {
143         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
144         struct bnx2x_softc *sc = dev->data->dev_private;
145         int ret = 0;
146
147         atomic_store_rel_long(&sc->periodic_flags, PERIODIC_GO);
148         bnx2x_interrupt_action(dev);
149         if (IS_PF(sc)) {
150                 ret = rte_eal_alarm_set(BNX2X_SP_TIMER_PERIOD,
151                                         bnx2x_periodic_start, (void *)dev);
152                 if (ret) {
153                         PMD_DRV_LOG(ERR, sc, "Unable to start periodic"
154                                              " timer rc %d", ret);
155                         assert(false && "Unable to start periodic timer");
156                 }
157         }
158 }
159
160 void bnx2x_periodic_stop(void *param)
161 {
162         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
163         struct bnx2x_softc *sc = dev->data->dev_private;
164
165         atomic_store_rel_long(&sc->periodic_flags, PERIODIC_STOP);
166
167         rte_eal_alarm_cancel(bnx2x_periodic_start, (void *)dev);
168 }
169
170 /*
171  * Devops - helper functions can be called from user application
172  */
173
174 static int
175 bnx2x_dev_configure(struct rte_eth_dev *dev)
176 {
177         struct bnx2x_softc *sc = dev->data->dev_private;
178         struct rte_eth_rxmode *rxmode = &dev->data->dev_conf.rxmode;
179
180         int mp_ncpus = sysconf(_SC_NPROCESSORS_CONF);
181
182         PMD_INIT_FUNC_TRACE(sc);
183
184         if (rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME) {
185                 sc->mtu = dev->data->dev_conf.rxmode.max_rx_pkt_len;
186                 dev->data->mtu = sc->mtu;
187         }
188
189         if (dev->data->nb_tx_queues > dev->data->nb_rx_queues) {
190                 PMD_DRV_LOG(ERR, sc, "The number of TX queues is greater than number of RX queues");
191                 return -EINVAL;
192         }
193
194         sc->num_queues = MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
195         if (sc->num_queues > mp_ncpus) {
196                 PMD_DRV_LOG(ERR, sc, "The number of queues is more than number of CPUs");
197                 return -EINVAL;
198         }
199
200         PMD_DRV_LOG(DEBUG, sc, "num_queues=%d, mtu=%d",
201                        sc->num_queues, sc->mtu);
202
203         /* allocate ilt */
204         if (bnx2x_alloc_ilt_mem(sc) != 0) {
205                 PMD_DRV_LOG(ERR, sc, "bnx2x_alloc_ilt_mem was failed");
206                 return -ENXIO;
207         }
208
209         /* allocate the host hardware/software hsi structures */
210         if (bnx2x_alloc_hsi_mem(sc) != 0) {
211                 PMD_DRV_LOG(ERR, sc, "bnx2x_alloc_hsi_mem was failed");
212                 bnx2x_free_ilt_mem(sc);
213                 return -ENXIO;
214         }
215
216         return 0;
217 }
218
219 static int
220 bnx2x_dev_start(struct rte_eth_dev *dev)
221 {
222         struct bnx2x_softc *sc = dev->data->dev_private;
223         int ret = 0;
224
225         PMD_INIT_FUNC_TRACE(sc);
226
227         /* start the periodic callout */
228         if (sc->periodic_flags & PERIODIC_STOP)
229                 bnx2x_periodic_start(dev);
230
231         ret = bnx2x_init(sc);
232         if (ret) {
233                 PMD_DRV_LOG(DEBUG, sc, "bnx2x_init failed (%d)", ret);
234                 return -1;
235         }
236
237         if (IS_PF(sc)) {
238                 rte_intr_callback_register(&sc->pci_dev->intr_handle,
239                                 bnx2x_interrupt_handler, (void *)dev);
240
241                 if (rte_intr_enable(&sc->pci_dev->intr_handle))
242                         PMD_DRV_LOG(ERR, sc, "rte_intr_enable failed");
243         }
244
245         ret = bnx2x_dev_rx_init(dev);
246         if (ret != 0) {
247                 PMD_DRV_LOG(DEBUG, sc, "bnx2x_dev_rx_init returned error code");
248                 return -3;
249         }
250
251         bnx2x_print_device_info(sc);
252
253         return ret;
254 }
255
256 static void
257 bnx2x_dev_stop(struct rte_eth_dev *dev)
258 {
259         struct bnx2x_softc *sc = dev->data->dev_private;
260         int ret = 0;
261
262         PMD_INIT_FUNC_TRACE(sc);
263
264         if (IS_PF(sc)) {
265                 rte_intr_disable(&sc->pci_dev->intr_handle);
266                 rte_intr_callback_unregister(&sc->pci_dev->intr_handle,
267                                 bnx2x_interrupt_handler, (void *)dev);
268         }
269
270         /* stop the periodic callout */
271         bnx2x_periodic_stop(dev);
272
273         ret = bnx2x_nic_unload(sc, UNLOAD_NORMAL, FALSE);
274         if (ret) {
275                 PMD_DRV_LOG(DEBUG, sc, "bnx2x_nic_unload failed (%d)", ret);
276                 return;
277         }
278
279         return;
280 }
281
282 static void
283 bnx2x_dev_close(struct rte_eth_dev *dev)
284 {
285         struct bnx2x_softc *sc = dev->data->dev_private;
286
287         PMD_INIT_FUNC_TRACE(sc);
288
289         if (IS_VF(sc))
290                 bnx2x_vf_close(sc);
291
292         bnx2x_dev_clear_queues(dev);
293         memset(&(dev->data->dev_link), 0 , sizeof(struct rte_eth_link));
294
295         /* free the host hardware/software hsi structures */
296         bnx2x_free_hsi_mem(sc);
297
298         /* free ilt */
299         bnx2x_free_ilt_mem(sc);
300 }
301
302 static void
303 bnx2x_promisc_enable(struct rte_eth_dev *dev)
304 {
305         struct bnx2x_softc *sc = dev->data->dev_private;
306
307         PMD_INIT_FUNC_TRACE(sc);
308         sc->rx_mode = BNX2X_RX_MODE_PROMISC;
309         if (rte_eth_allmulticast_get(dev->data->port_id) == 1)
310                 sc->rx_mode = BNX2X_RX_MODE_ALLMULTI_PROMISC;
311         bnx2x_set_rx_mode(sc);
312 }
313
314 static void
315 bnx2x_promisc_disable(struct rte_eth_dev *dev)
316 {
317         struct bnx2x_softc *sc = dev->data->dev_private;
318
319         PMD_INIT_FUNC_TRACE(sc);
320         sc->rx_mode = BNX2X_RX_MODE_NORMAL;
321         if (rte_eth_allmulticast_get(dev->data->port_id) == 1)
322                 sc->rx_mode = BNX2X_RX_MODE_ALLMULTI;
323         bnx2x_set_rx_mode(sc);
324 }
325
326 static void
327 bnx2x_dev_allmulticast_enable(struct rte_eth_dev *dev)
328 {
329         struct bnx2x_softc *sc = dev->data->dev_private;
330
331         PMD_INIT_FUNC_TRACE(sc);
332         sc->rx_mode = BNX2X_RX_MODE_ALLMULTI;
333         if (rte_eth_promiscuous_get(dev->data->port_id) == 1)
334                 sc->rx_mode = BNX2X_RX_MODE_ALLMULTI_PROMISC;
335         bnx2x_set_rx_mode(sc);
336 }
337
338 static void
339 bnx2x_dev_allmulticast_disable(struct rte_eth_dev *dev)
340 {
341         struct bnx2x_softc *sc = dev->data->dev_private;
342
343         PMD_INIT_FUNC_TRACE(sc);
344         sc->rx_mode = BNX2X_RX_MODE_NORMAL;
345         if (rte_eth_promiscuous_get(dev->data->port_id) == 1)
346                 sc->rx_mode = BNX2X_RX_MODE_PROMISC;
347         bnx2x_set_rx_mode(sc);
348 }
349
350 static int
351 bnx2x_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
352 {
353         struct bnx2x_softc *sc = dev->data->dev_private;
354
355         PMD_INIT_FUNC_TRACE(sc);
356
357         return bnx2x_link_update(dev);
358 }
359
360 static int
361 bnx2xvf_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
362 {
363         struct bnx2x_softc *sc = dev->data->dev_private;
364         int ret = 0;
365
366         ret = bnx2x_link_update(dev);
367
368         bnx2x_check_bull(sc);
369         if (sc->old_bulletin.valid_bitmap & (1 << CHANNEL_DOWN)) {
370                 PMD_DRV_LOG(ERR, sc, "PF indicated channel is down."
371                                 "VF device is no longer operational");
372                 dev->data->dev_link.link_status = ETH_LINK_DOWN;
373         }
374
375         return ret;
376 }
377
378 static int
379 bnx2x_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
380 {
381         struct bnx2x_softc *sc = dev->data->dev_private;
382         uint32_t brb_truncate_discard;
383         uint64_t brb_drops;
384         uint64_t brb_truncates;
385
386         PMD_INIT_FUNC_TRACE(sc);
387
388         bnx2x_stats_handle(sc, STATS_EVENT_UPDATE);
389
390         memset(stats, 0, sizeof (struct rte_eth_stats));
391
392         stats->ipackets =
393                 HILO_U64(sc->eth_stats.total_unicast_packets_received_hi,
394                                 sc->eth_stats.total_unicast_packets_received_lo) +
395                 HILO_U64(sc->eth_stats.total_multicast_packets_received_hi,
396                                 sc->eth_stats.total_multicast_packets_received_lo) +
397                 HILO_U64(sc->eth_stats.total_broadcast_packets_received_hi,
398                                 sc->eth_stats.total_broadcast_packets_received_lo);
399
400         stats->opackets =
401                 HILO_U64(sc->eth_stats.total_unicast_packets_transmitted_hi,
402                                 sc->eth_stats.total_unicast_packets_transmitted_lo) +
403                 HILO_U64(sc->eth_stats.total_multicast_packets_transmitted_hi,
404                                 sc->eth_stats.total_multicast_packets_transmitted_lo) +
405                 HILO_U64(sc->eth_stats.total_broadcast_packets_transmitted_hi,
406                                 sc->eth_stats.total_broadcast_packets_transmitted_lo);
407
408         stats->ibytes =
409                 HILO_U64(sc->eth_stats.total_bytes_received_hi,
410                                 sc->eth_stats.total_bytes_received_lo);
411
412         stats->obytes =
413                 HILO_U64(sc->eth_stats.total_bytes_transmitted_hi,
414                                 sc->eth_stats.total_bytes_transmitted_lo);
415
416         stats->ierrors =
417                 HILO_U64(sc->eth_stats.error_bytes_received_hi,
418                                 sc->eth_stats.error_bytes_received_lo);
419
420         stats->oerrors = 0;
421
422         stats->rx_nombuf =
423                 HILO_U64(sc->eth_stats.no_buff_discard_hi,
424                                 sc->eth_stats.no_buff_discard_lo);
425
426         brb_drops =
427                 HILO_U64(sc->eth_stats.brb_drop_hi,
428                          sc->eth_stats.brb_drop_lo);
429
430         brb_truncates =
431                 HILO_U64(sc->eth_stats.brb_truncate_hi,
432                          sc->eth_stats.brb_truncate_lo);
433
434         brb_truncate_discard = sc->eth_stats.brb_truncate_discard;
435
436         stats->imissed = brb_drops + brb_truncates +
437                          brb_truncate_discard + stats->rx_nombuf;
438
439         return 0;
440 }
441
442 static int
443 bnx2x_get_xstats_names(__rte_unused struct rte_eth_dev *dev,
444                        struct rte_eth_xstat_name *xstats_names,
445                        __rte_unused unsigned limit)
446 {
447         unsigned int i, stat_cnt = RTE_DIM(bnx2x_xstats_strings);
448
449         if (xstats_names != NULL)
450                 for (i = 0; i < stat_cnt; i++)
451                         strlcpy(xstats_names[i].name,
452                                 bnx2x_xstats_strings[i].name,
453                                 sizeof(xstats_names[i].name));
454
455         return stat_cnt;
456 }
457
458 static int
459 bnx2x_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
460                      unsigned int n)
461 {
462         struct bnx2x_softc *sc = dev->data->dev_private;
463         unsigned int num = RTE_DIM(bnx2x_xstats_strings);
464
465         if (n < num)
466                 return num;
467
468         bnx2x_stats_handle(sc, STATS_EVENT_UPDATE);
469
470         for (num = 0; num < n; num++) {
471                 if (bnx2x_xstats_strings[num].offset_hi !=
472                     bnx2x_xstats_strings[num].offset_lo)
473                         xstats[num].value = HILO_U64(
474                                           *(uint32_t *)((char *)&sc->eth_stats +
475                                           bnx2x_xstats_strings[num].offset_hi),
476                                           *(uint32_t *)((char *)&sc->eth_stats +
477                                           bnx2x_xstats_strings[num].offset_lo));
478                 else
479                         xstats[num].value =
480                                           *(uint64_t *)((char *)&sc->eth_stats +
481                                           bnx2x_xstats_strings[num].offset_lo);
482                 xstats[num].id = num;
483         }
484
485         return num;
486 }
487
488 static void
489 bnx2x_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
490 {
491         struct bnx2x_softc *sc = dev->data->dev_private;
492         dev_info->max_rx_queues  = sc->max_rx_queues;
493         dev_info->max_tx_queues  = sc->max_tx_queues;
494         dev_info->min_rx_bufsize = BNX2X_MIN_RX_BUF_SIZE;
495         dev_info->max_rx_pktlen  = BNX2X_MAX_RX_PKT_LEN;
496         dev_info->max_mac_addrs  = BNX2X_MAX_MAC_ADDRS;
497         dev_info->speed_capa = ETH_LINK_SPEED_10G | ETH_LINK_SPEED_20G;
498         dev_info->rx_offload_capa = DEV_RX_OFFLOAD_JUMBO_FRAME;
499 }
500
501 static int
502 bnx2x_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
503                 uint32_t index, uint32_t pool)
504 {
505         struct bnx2x_softc *sc = dev->data->dev_private;
506
507         if (sc->mac_ops.mac_addr_add) {
508                 sc->mac_ops.mac_addr_add(dev, mac_addr, index, pool);
509                 return 0;
510         }
511         return -ENOTSUP;
512 }
513
514 static void
515 bnx2x_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
516 {
517         struct bnx2x_softc *sc = dev->data->dev_private;
518
519         if (sc->mac_ops.mac_addr_remove)
520                 sc->mac_ops.mac_addr_remove(dev, index);
521 }
522
523 static const struct eth_dev_ops bnx2x_eth_dev_ops = {
524         .dev_configure                = bnx2x_dev_configure,
525         .dev_start                    = bnx2x_dev_start,
526         .dev_stop                     = bnx2x_dev_stop,
527         .dev_close                    = bnx2x_dev_close,
528         .promiscuous_enable           = bnx2x_promisc_enable,
529         .promiscuous_disable          = bnx2x_promisc_disable,
530         .allmulticast_enable          = bnx2x_dev_allmulticast_enable,
531         .allmulticast_disable         = bnx2x_dev_allmulticast_disable,
532         .link_update                  = bnx2x_dev_link_update,
533         .stats_get                    = bnx2x_dev_stats_get,
534         .xstats_get                   = bnx2x_dev_xstats_get,
535         .xstats_get_names             = bnx2x_get_xstats_names,
536         .dev_infos_get                = bnx2x_dev_infos_get,
537         .rx_queue_setup               = bnx2x_dev_rx_queue_setup,
538         .rx_queue_release             = bnx2x_dev_rx_queue_release,
539         .tx_queue_setup               = bnx2x_dev_tx_queue_setup,
540         .tx_queue_release             = bnx2x_dev_tx_queue_release,
541         .mac_addr_add                 = bnx2x_mac_addr_add,
542         .mac_addr_remove              = bnx2x_mac_addr_remove,
543 };
544
545 /*
546  * dev_ops for virtual function
547  */
548 static const struct eth_dev_ops bnx2xvf_eth_dev_ops = {
549         .dev_configure                = bnx2x_dev_configure,
550         .dev_start                    = bnx2x_dev_start,
551         .dev_stop                     = bnx2x_dev_stop,
552         .dev_close                    = bnx2x_dev_close,
553         .promiscuous_enable           = bnx2x_promisc_enable,
554         .promiscuous_disable          = bnx2x_promisc_disable,
555         .allmulticast_enable          = bnx2x_dev_allmulticast_enable,
556         .allmulticast_disable         = bnx2x_dev_allmulticast_disable,
557         .link_update                  = bnx2xvf_dev_link_update,
558         .stats_get                    = bnx2x_dev_stats_get,
559         .xstats_get                   = bnx2x_dev_xstats_get,
560         .xstats_get_names             = bnx2x_get_xstats_names,
561         .dev_infos_get                = bnx2x_dev_infos_get,
562         .rx_queue_setup               = bnx2x_dev_rx_queue_setup,
563         .rx_queue_release             = bnx2x_dev_rx_queue_release,
564         .tx_queue_setup               = bnx2x_dev_tx_queue_setup,
565         .tx_queue_release             = bnx2x_dev_tx_queue_release,
566         .mac_addr_add                 = bnx2x_mac_addr_add,
567         .mac_addr_remove              = bnx2x_mac_addr_remove,
568 };
569
570
571 static int
572 bnx2x_common_dev_init(struct rte_eth_dev *eth_dev, int is_vf)
573 {
574         int ret = 0;
575         struct rte_pci_device *pci_dev;
576         struct rte_pci_addr pci_addr;
577         struct bnx2x_softc *sc;
578         static bool adapter_info = true;
579
580         /* Extract key data structures */
581         sc = eth_dev->data->dev_private;
582         pci_dev = RTE_DEV_TO_PCI(eth_dev->device);
583         pci_addr = pci_dev->addr;
584
585         snprintf(sc->devinfo.name, NAME_SIZE, PCI_SHORT_PRI_FMT ":dpdk-port-%u",
586                  pci_addr.bus, pci_addr.devid, pci_addr.function,
587                  eth_dev->data->port_id);
588
589         PMD_INIT_FUNC_TRACE(sc);
590
591         eth_dev->dev_ops = is_vf ? &bnx2xvf_eth_dev_ops : &bnx2x_eth_dev_ops;
592
593         rte_eth_copy_pci_info(eth_dev, pci_dev);
594
595         sc->pcie_bus    = pci_dev->addr.bus;
596         sc->pcie_device = pci_dev->addr.devid;
597
598         sc->devinfo.vendor_id    = pci_dev->id.vendor_id;
599         sc->devinfo.device_id    = pci_dev->id.device_id;
600         sc->devinfo.subvendor_id = pci_dev->id.subsystem_vendor_id;
601         sc->devinfo.subdevice_id = pci_dev->id.subsystem_device_id;
602
603         if (is_vf)
604                 sc->flags = BNX2X_IS_VF_FLAG;
605
606         sc->pcie_func = pci_dev->addr.function;
607         sc->bar[BAR0].base_addr = (void *)pci_dev->mem_resource[0].addr;
608         if (is_vf)
609                 sc->bar[BAR1].base_addr = (void *)
610                         ((uintptr_t)pci_dev->mem_resource[0].addr + PXP_VF_ADDR_DB_START);
611         else
612                 sc->bar[BAR1].base_addr = pci_dev->mem_resource[2].addr;
613
614         assert(sc->bar[BAR0].base_addr);
615         assert(sc->bar[BAR1].base_addr);
616
617         bnx2x_load_firmware(sc);
618         assert(sc->firmware);
619
620         if (eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf & ETH_RSS_NONFRAG_IPV4_UDP)
621                 sc->udp_rss = 1;
622
623         sc->rx_budget = BNX2X_RX_BUDGET;
624         sc->hc_rx_ticks = BNX2X_RX_TICKS;
625         sc->hc_tx_ticks = BNX2X_TX_TICKS;
626
627         sc->interrupt_mode = INTR_MODE_SINGLE_MSIX;
628         sc->rx_mode = BNX2X_RX_MODE_NORMAL;
629
630         sc->pci_dev = pci_dev;
631         ret = bnx2x_attach(sc);
632         if (ret) {
633                 PMD_DRV_LOG(ERR, sc, "bnx2x_attach failed (%d)", ret);
634                 return ret;
635         }
636
637         /* Print important adapter info for the user. */
638         if (adapter_info) {
639                 bnx2x_print_adapter_info(sc);
640                 adapter_info = false;
641         }
642
643         /* schedule periodic poll for slowpath link events */
644         if (IS_PF(sc)) {
645                 PMD_DRV_LOG(DEBUG, sc, "Scheduling periodic poll for slowpath link events");
646                 ret = rte_eal_alarm_set(BNX2X_SP_TIMER_PERIOD,
647                                         bnx2x_periodic_start, (void *)eth_dev);
648                 if (ret) {
649                         PMD_DRV_LOG(ERR, sc, "Unable to start periodic"
650                                              " timer rc %d", ret);
651                         return -EINVAL;
652                 }
653         }
654
655         eth_dev->data->mac_addrs = (struct ether_addr *)sc->link_params.mac_addr;
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 }