net/bnx2x: add multicast MAC address filtering
[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         /* Configure the previously stored Multicast address list */
244         if (IS_VF(sc))
245                 bnx2x_vfpf_set_mcast(sc, sc->mc_addrs, sc->mc_addrs_num);
246         bnx2x_dev_rxtx_init(dev);
247
248         bnx2x_print_device_info(sc);
249
250         return ret;
251 }
252
253 static void
254 bnx2x_dev_stop(struct rte_eth_dev *dev)
255 {
256         struct bnx2x_softc *sc = dev->data->dev_private;
257         int ret = 0;
258
259         PMD_INIT_FUNC_TRACE(sc);
260
261         bnx2x_dev_rxtx_init_dummy(dev);
262
263         if (IS_PF(sc)) {
264                 rte_intr_disable(&sc->pci_dev->intr_handle);
265                 rte_intr_callback_unregister(&sc->pci_dev->intr_handle,
266                                 bnx2x_interrupt_handler, (void *)dev);
267
268                 /* stop the periodic callout */
269                 bnx2x_periodic_stop(dev);
270         }
271         /* Remove the configured Multicast list
272          * Sending NULL for the list of address and the
273          * Number is set to 0 denoting DEL_CMD
274          */
275         if (IS_VF(sc))
276                 bnx2x_vfpf_set_mcast(sc, NULL, 0);
277         ret = bnx2x_nic_unload(sc, UNLOAD_NORMAL, FALSE);
278         if (ret) {
279                 PMD_DRV_LOG(DEBUG, sc, "bnx2x_nic_unload failed (%d)", ret);
280                 return;
281         }
282
283         return;
284 }
285
286 static void
287 bnx2x_dev_close(struct rte_eth_dev *dev)
288 {
289         struct bnx2x_softc *sc = dev->data->dev_private;
290
291         PMD_INIT_FUNC_TRACE(sc);
292
293         if (IS_VF(sc))
294                 bnx2x_vf_close(sc);
295
296         bnx2x_dev_clear_queues(dev);
297         memset(&(dev->data->dev_link), 0 , sizeof(struct rte_eth_link));
298
299         /* free ilt */
300         bnx2x_free_ilt_mem(sc);
301 }
302
303 static int
304 bnx2x_promisc_enable(struct rte_eth_dev *dev)
305 {
306         struct bnx2x_softc *sc = dev->data->dev_private;
307
308         PMD_INIT_FUNC_TRACE(sc);
309         sc->rx_mode = BNX2X_RX_MODE_PROMISC;
310         if (rte_eth_allmulticast_get(dev->data->port_id) == 1)
311                 sc->rx_mode = BNX2X_RX_MODE_ALLMULTI_PROMISC;
312         bnx2x_set_rx_mode(sc);
313
314         return 0;
315 }
316
317 static int
318 bnx2x_promisc_disable(struct rte_eth_dev *dev)
319 {
320         struct bnx2x_softc *sc = dev->data->dev_private;
321
322         PMD_INIT_FUNC_TRACE(sc);
323         sc->rx_mode = BNX2X_RX_MODE_NORMAL;
324         if (rte_eth_allmulticast_get(dev->data->port_id) == 1)
325                 sc->rx_mode = BNX2X_RX_MODE_ALLMULTI;
326         bnx2x_set_rx_mode(sc);
327
328         return 0;
329 }
330
331 static int
332 bnx2x_dev_allmulticast_enable(struct rte_eth_dev *dev)
333 {
334         struct bnx2x_softc *sc = dev->data->dev_private;
335
336         PMD_INIT_FUNC_TRACE(sc);
337         sc->rx_mode = BNX2X_RX_MODE_ALLMULTI;
338         if (rte_eth_promiscuous_get(dev->data->port_id) == 1)
339                 sc->rx_mode = BNX2X_RX_MODE_ALLMULTI_PROMISC;
340         bnx2x_set_rx_mode(sc);
341
342         return 0;
343 }
344
345 static int
346 bnx2x_dev_allmulticast_disable(struct rte_eth_dev *dev)
347 {
348         struct bnx2x_softc *sc = dev->data->dev_private;
349
350         PMD_INIT_FUNC_TRACE(sc);
351         sc->rx_mode = BNX2X_RX_MODE_NORMAL;
352         if (rte_eth_promiscuous_get(dev->data->port_id) == 1)
353                 sc->rx_mode = BNX2X_RX_MODE_PROMISC;
354         bnx2x_set_rx_mode(sc);
355
356         return 0;
357 }
358
359 static int
360 bnx2x_dev_set_mc_addr_list(struct rte_eth_dev *dev,
361                 struct rte_ether_addr *mc_addrs, uint32_t mc_addrs_num)
362 {
363         struct bnx2x_softc *sc = dev->data->dev_private;
364         int err;
365         PMD_INIT_FUNC_TRACE(sc);
366         /* flush previous addresses */
367         err = bnx2x_vfpf_set_mcast(sc, NULL, 0);
368         if (err)
369                 return err;
370         sc->mc_addrs_num = 0;
371
372         /* Add new ones */
373         err = bnx2x_vfpf_set_mcast(sc, mc_addrs, mc_addrs_num);
374         if (err)
375                 return err;
376
377         sc->mc_addrs_num = mc_addrs_num;
378         memcpy(sc->mc_addrs, mc_addrs, mc_addrs_num * sizeof(*mc_addrs));
379
380         return 0;
381 }
382
383 static int
384 bnx2x_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
385 {
386         struct bnx2x_softc *sc = dev->data->dev_private;
387
388         PMD_INIT_FUNC_TRACE(sc);
389
390         return bnx2x_link_update(dev);
391 }
392
393 static int
394 bnx2xvf_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
395 {
396         struct bnx2x_softc *sc = dev->data->dev_private;
397         int ret = 0;
398
399         ret = bnx2x_link_update(dev);
400
401         bnx2x_check_bull(sc);
402         if (sc->old_bulletin.valid_bitmap & (1 << CHANNEL_DOWN)) {
403                 PMD_DRV_LOG(ERR, sc, "PF indicated channel is down."
404                                 "VF device is no longer operational");
405                 dev->data->dev_link.link_status = ETH_LINK_DOWN;
406         }
407
408         return ret;
409 }
410
411 static int
412 bnx2x_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
413 {
414         struct bnx2x_softc *sc = dev->data->dev_private;
415         uint32_t brb_truncate_discard;
416         uint64_t brb_drops;
417         uint64_t brb_truncates;
418
419         PMD_INIT_FUNC_TRACE(sc);
420
421         bnx2x_stats_handle(sc, STATS_EVENT_UPDATE);
422
423         memset(stats, 0, sizeof (struct rte_eth_stats));
424
425         stats->ipackets =
426                 HILO_U64(sc->eth_stats.total_unicast_packets_received_hi,
427                                 sc->eth_stats.total_unicast_packets_received_lo) +
428                 HILO_U64(sc->eth_stats.total_multicast_packets_received_hi,
429                                 sc->eth_stats.total_multicast_packets_received_lo) +
430                 HILO_U64(sc->eth_stats.total_broadcast_packets_received_hi,
431                                 sc->eth_stats.total_broadcast_packets_received_lo);
432
433         stats->opackets =
434                 HILO_U64(sc->eth_stats.total_unicast_packets_transmitted_hi,
435                                 sc->eth_stats.total_unicast_packets_transmitted_lo) +
436                 HILO_U64(sc->eth_stats.total_multicast_packets_transmitted_hi,
437                                 sc->eth_stats.total_multicast_packets_transmitted_lo) +
438                 HILO_U64(sc->eth_stats.total_broadcast_packets_transmitted_hi,
439                                 sc->eth_stats.total_broadcast_packets_transmitted_lo);
440
441         stats->ibytes =
442                 HILO_U64(sc->eth_stats.total_bytes_received_hi,
443                                 sc->eth_stats.total_bytes_received_lo);
444
445         stats->obytes =
446                 HILO_U64(sc->eth_stats.total_bytes_transmitted_hi,
447                                 sc->eth_stats.total_bytes_transmitted_lo);
448
449         stats->ierrors =
450                 HILO_U64(sc->eth_stats.error_bytes_received_hi,
451                                 sc->eth_stats.error_bytes_received_lo);
452
453         stats->oerrors = 0;
454
455         stats->rx_nombuf =
456                 HILO_U64(sc->eth_stats.no_buff_discard_hi,
457                                 sc->eth_stats.no_buff_discard_lo);
458
459         brb_drops =
460                 HILO_U64(sc->eth_stats.brb_drop_hi,
461                          sc->eth_stats.brb_drop_lo);
462
463         brb_truncates =
464                 HILO_U64(sc->eth_stats.brb_truncate_hi,
465                          sc->eth_stats.brb_truncate_lo);
466
467         brb_truncate_discard = sc->eth_stats.brb_truncate_discard;
468
469         stats->imissed = brb_drops + brb_truncates +
470                          brb_truncate_discard + stats->rx_nombuf;
471
472         return 0;
473 }
474
475 static int
476 bnx2x_get_xstats_names(__rte_unused struct rte_eth_dev *dev,
477                        struct rte_eth_xstat_name *xstats_names,
478                        __rte_unused unsigned limit)
479 {
480         unsigned int i, stat_cnt = RTE_DIM(bnx2x_xstats_strings);
481
482         if (xstats_names != NULL)
483                 for (i = 0; i < stat_cnt; i++)
484                         strlcpy(xstats_names[i].name,
485                                 bnx2x_xstats_strings[i].name,
486                                 sizeof(xstats_names[i].name));
487
488         return stat_cnt;
489 }
490
491 static int
492 bnx2x_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
493                      unsigned int n)
494 {
495         struct bnx2x_softc *sc = dev->data->dev_private;
496         unsigned int num = RTE_DIM(bnx2x_xstats_strings);
497
498         if (n < num)
499                 return num;
500
501         bnx2x_stats_handle(sc, STATS_EVENT_UPDATE);
502
503         for (num = 0; num < n; num++) {
504                 if (bnx2x_xstats_strings[num].offset_hi !=
505                     bnx2x_xstats_strings[num].offset_lo)
506                         xstats[num].value = HILO_U64(
507                                           *(uint32_t *)((char *)&sc->eth_stats +
508                                           bnx2x_xstats_strings[num].offset_hi),
509                                           *(uint32_t *)((char *)&sc->eth_stats +
510                                           bnx2x_xstats_strings[num].offset_lo));
511                 else
512                         xstats[num].value =
513                                           *(uint64_t *)((char *)&sc->eth_stats +
514                                           bnx2x_xstats_strings[num].offset_lo);
515                 xstats[num].id = num;
516         }
517
518         return num;
519 }
520
521 static int
522 bnx2x_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
523 {
524         struct bnx2x_softc *sc = dev->data->dev_private;
525
526         dev_info->max_rx_queues  = sc->max_rx_queues;
527         dev_info->max_tx_queues  = sc->max_tx_queues;
528         dev_info->min_rx_bufsize = BNX2X_MIN_RX_BUF_SIZE;
529         dev_info->max_rx_pktlen  = BNX2X_MAX_RX_PKT_LEN;
530         dev_info->max_mac_addrs  = BNX2X_MAX_MAC_ADDRS;
531         dev_info->speed_capa = ETH_LINK_SPEED_10G | ETH_LINK_SPEED_20G;
532         dev_info->rx_offload_capa = DEV_RX_OFFLOAD_JUMBO_FRAME;
533
534         dev_info->rx_desc_lim.nb_max = MAX_RX_AVAIL;
535         dev_info->rx_desc_lim.nb_min = MIN_RX_SIZE_NONTPA;
536         dev_info->tx_desc_lim.nb_max = MAX_TX_AVAIL;
537
538         return 0;
539 }
540
541 static int
542 bnx2x_mac_addr_add(struct rte_eth_dev *dev, struct rte_ether_addr *mac_addr,
543                 uint32_t index, uint32_t pool)
544 {
545         struct bnx2x_softc *sc = dev->data->dev_private;
546
547         if (sc->mac_ops.mac_addr_add) {
548                 sc->mac_ops.mac_addr_add(dev, mac_addr, index, pool);
549                 return 0;
550         }
551         return -ENOTSUP;
552 }
553
554 static void
555 bnx2x_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
556 {
557         struct bnx2x_softc *sc = dev->data->dev_private;
558
559         if (sc->mac_ops.mac_addr_remove)
560                 sc->mac_ops.mac_addr_remove(dev, index);
561 }
562
563 static const struct eth_dev_ops bnx2x_eth_dev_ops = {
564         .dev_configure                = bnx2x_dev_configure,
565         .dev_start                    = bnx2x_dev_start,
566         .dev_stop                     = bnx2x_dev_stop,
567         .dev_close                    = bnx2x_dev_close,
568         .promiscuous_enable           = bnx2x_promisc_enable,
569         .promiscuous_disable          = bnx2x_promisc_disable,
570         .allmulticast_enable          = bnx2x_dev_allmulticast_enable,
571         .allmulticast_disable         = bnx2x_dev_allmulticast_disable,
572         .link_update                  = bnx2x_dev_link_update,
573         .stats_get                    = bnx2x_dev_stats_get,
574         .xstats_get                   = bnx2x_dev_xstats_get,
575         .xstats_get_names             = bnx2x_get_xstats_names,
576         .dev_infos_get                = bnx2x_dev_infos_get,
577         .rx_queue_setup               = bnx2x_dev_rx_queue_setup,
578         .rx_queue_release             = bnx2x_dev_rx_queue_release,
579         .tx_queue_setup               = bnx2x_dev_tx_queue_setup,
580         .tx_queue_release             = bnx2x_dev_tx_queue_release,
581         .mac_addr_add                 = bnx2x_mac_addr_add,
582         .mac_addr_remove              = bnx2x_mac_addr_remove,
583 };
584
585 /*
586  * dev_ops for virtual function
587  */
588 static const struct eth_dev_ops bnx2xvf_eth_dev_ops = {
589         .dev_configure                = bnx2x_dev_configure,
590         .dev_start                    = bnx2x_dev_start,
591         .dev_stop                     = bnx2x_dev_stop,
592         .dev_close                    = bnx2x_dev_close,
593         .promiscuous_enable           = bnx2x_promisc_enable,
594         .promiscuous_disable          = bnx2x_promisc_disable,
595         .allmulticast_enable          = bnx2x_dev_allmulticast_enable,
596         .allmulticast_disable         = bnx2x_dev_allmulticast_disable,
597         .set_mc_addr_list             = bnx2x_dev_set_mc_addr_list,
598         .link_update                  = bnx2xvf_dev_link_update,
599         .stats_get                    = bnx2x_dev_stats_get,
600         .xstats_get                   = bnx2x_dev_xstats_get,
601         .xstats_get_names             = bnx2x_get_xstats_names,
602         .dev_infos_get                = bnx2x_dev_infos_get,
603         .rx_queue_setup               = bnx2x_dev_rx_queue_setup,
604         .rx_queue_release             = bnx2x_dev_rx_queue_release,
605         .tx_queue_setup               = bnx2x_dev_tx_queue_setup,
606         .tx_queue_release             = bnx2x_dev_tx_queue_release,
607         .mac_addr_add                 = bnx2x_mac_addr_add,
608         .mac_addr_remove              = bnx2x_mac_addr_remove,
609 };
610
611
612 static int
613 bnx2x_common_dev_init(struct rte_eth_dev *eth_dev, int is_vf)
614 {
615         int ret = 0;
616         struct rte_pci_device *pci_dev;
617         struct rte_pci_addr pci_addr;
618         struct bnx2x_softc *sc;
619         static bool adapter_info = true;
620
621         /* Extract key data structures */
622         sc = eth_dev->data->dev_private;
623         pci_dev = RTE_DEV_TO_PCI(eth_dev->device);
624         pci_addr = pci_dev->addr;
625
626         snprintf(sc->devinfo.name, NAME_SIZE, PCI_SHORT_PRI_FMT ":dpdk-port-%u",
627                  pci_addr.bus, pci_addr.devid, pci_addr.function,
628                  eth_dev->data->port_id);
629
630         PMD_INIT_FUNC_TRACE(sc);
631
632         eth_dev->dev_ops = is_vf ? &bnx2xvf_eth_dev_ops : &bnx2x_eth_dev_ops;
633
634         if (rte_eal_process_type() != RTE_PROC_PRIMARY) {
635                 PMD_DRV_LOG(ERR, sc, "Skipping device init from secondary process");
636                 return 0;
637         }
638
639         rte_eth_copy_pci_info(eth_dev, pci_dev);
640
641         sc->pcie_bus    = pci_dev->addr.bus;
642         sc->pcie_device = pci_dev->addr.devid;
643
644         sc->devinfo.vendor_id    = pci_dev->id.vendor_id;
645         sc->devinfo.device_id    = pci_dev->id.device_id;
646         sc->devinfo.subvendor_id = pci_dev->id.subsystem_vendor_id;
647         sc->devinfo.subdevice_id = pci_dev->id.subsystem_device_id;
648
649         if (is_vf)
650                 sc->flags = BNX2X_IS_VF_FLAG;
651
652         sc->pcie_func = pci_dev->addr.function;
653         sc->bar[BAR0].base_addr = (void *)pci_dev->mem_resource[0].addr;
654         if (is_vf)
655                 sc->bar[BAR1].base_addr = (void *)
656                         ((uintptr_t)pci_dev->mem_resource[0].addr + PXP_VF_ADDR_DB_START);
657         else
658                 sc->bar[BAR1].base_addr = pci_dev->mem_resource[2].addr;
659
660         assert(sc->bar[BAR0].base_addr);
661         assert(sc->bar[BAR1].base_addr);
662
663         bnx2x_load_firmware(sc);
664         assert(sc->firmware);
665
666         if (eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf & ETH_RSS_NONFRAG_IPV4_UDP)
667                 sc->udp_rss = 1;
668
669         sc->rx_budget = BNX2X_RX_BUDGET;
670         sc->hc_rx_ticks = BNX2X_RX_TICKS;
671         sc->hc_tx_ticks = BNX2X_TX_TICKS;
672
673         sc->interrupt_mode = INTR_MODE_SINGLE_MSIX;
674         sc->rx_mode = BNX2X_RX_MODE_NORMAL;
675
676         sc->pci_dev = pci_dev;
677         ret = bnx2x_attach(sc);
678         if (ret) {
679                 PMD_DRV_LOG(ERR, sc, "bnx2x_attach failed (%d)", ret);
680                 return ret;
681         }
682
683         /* Print important adapter info for the user. */
684         if (adapter_info) {
685                 bnx2x_print_adapter_info(sc);
686                 adapter_info = false;
687         }
688
689         /* schedule periodic poll for slowpath link events */
690         if (IS_PF(sc)) {
691                 PMD_DRV_LOG(DEBUG, sc, "Scheduling periodic poll for slowpath link events");
692                 ret = rte_eal_alarm_set(BNX2X_SP_TIMER_PERIOD,
693                                         bnx2x_periodic_start, (void *)eth_dev);
694                 if (ret) {
695                         PMD_DRV_LOG(ERR, sc, "Unable to start periodic"
696                                              " timer rc %d", ret);
697                         return -EINVAL;
698                 }
699         }
700
701         eth_dev->data->mac_addrs =
702                 (struct rte_ether_addr *)sc->link_params.mac_addr;
703
704         if (IS_VF(sc)) {
705                 rte_spinlock_init(&sc->vf2pf_lock);
706
707                 ret = bnx2x_dma_alloc(sc, sizeof(struct bnx2x_vf_mbx_msg),
708                                       &sc->vf2pf_mbox_mapping, "vf2pf_mbox",
709                                       RTE_CACHE_LINE_SIZE);
710                 if (ret)
711                         goto out;
712
713                 sc->vf2pf_mbox = (struct bnx2x_vf_mbx_msg *)
714                                          sc->vf2pf_mbox_mapping.vaddr;
715
716                 ret = bnx2x_dma_alloc(sc, sizeof(struct bnx2x_vf_bulletin),
717                                       &sc->pf2vf_bulletin_mapping, "vf2pf_bull",
718                                       RTE_CACHE_LINE_SIZE);
719                 if (ret)
720                         goto out;
721
722                 sc->pf2vf_bulletin = (struct bnx2x_vf_bulletin *)
723                                              sc->pf2vf_bulletin_mapping.vaddr;
724
725                 ret = bnx2x_vf_get_resources(sc, sc->max_tx_queues,
726                                              sc->max_rx_queues);
727                 if (ret)
728                         goto out;
729         }
730
731         return 0;
732
733 out:
734         if (IS_PF(sc))
735                 bnx2x_periodic_stop(eth_dev);
736
737         return ret;
738 }
739
740 static int
741 eth_bnx2x_dev_init(struct rte_eth_dev *eth_dev)
742 {
743         struct bnx2x_softc *sc = eth_dev->data->dev_private;
744         PMD_INIT_FUNC_TRACE(sc);
745         return bnx2x_common_dev_init(eth_dev, 0);
746 }
747
748 static int
749 eth_bnx2xvf_dev_init(struct rte_eth_dev *eth_dev)
750 {
751         struct bnx2x_softc *sc = eth_dev->data->dev_private;
752         PMD_INIT_FUNC_TRACE(sc);
753         return bnx2x_common_dev_init(eth_dev, 1);
754 }
755
756 static int eth_bnx2x_dev_uninit(struct rte_eth_dev *eth_dev)
757 {
758         /* mac_addrs must not be freed alone because part of dev_private */
759         eth_dev->data->mac_addrs = NULL;
760         return 0;
761 }
762
763 static struct rte_pci_driver rte_bnx2x_pmd;
764 static struct rte_pci_driver rte_bnx2xvf_pmd;
765
766 static int eth_bnx2x_pci_probe(struct rte_pci_driver *pci_drv,
767         struct rte_pci_device *pci_dev)
768 {
769         if (pci_drv == &rte_bnx2x_pmd)
770                 return rte_eth_dev_pci_generic_probe(pci_dev,
771                                 sizeof(struct bnx2x_softc), eth_bnx2x_dev_init);
772         else if (pci_drv == &rte_bnx2xvf_pmd)
773                 return rte_eth_dev_pci_generic_probe(pci_dev,
774                                 sizeof(struct bnx2x_softc), eth_bnx2xvf_dev_init);
775         else
776                 return -EINVAL;
777 }
778
779 static int eth_bnx2x_pci_remove(struct rte_pci_device *pci_dev)
780 {
781         return rte_eth_dev_pci_generic_remove(pci_dev, eth_bnx2x_dev_uninit);
782 }
783
784 static struct rte_pci_driver rte_bnx2x_pmd = {
785         .id_table = pci_id_bnx2x_map,
786         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
787         .probe = eth_bnx2x_pci_probe,
788         .remove = eth_bnx2x_pci_remove,
789 };
790
791 /*
792  * virtual function driver struct
793  */
794 static struct rte_pci_driver rte_bnx2xvf_pmd = {
795         .id_table = pci_id_bnx2xvf_map,
796         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
797         .probe = eth_bnx2x_pci_probe,
798         .remove = eth_bnx2x_pci_remove,
799 };
800
801 RTE_PMD_REGISTER_PCI(net_bnx2x, rte_bnx2x_pmd);
802 RTE_PMD_REGISTER_PCI_TABLE(net_bnx2x, pci_id_bnx2x_map);
803 RTE_PMD_REGISTER_KMOD_DEP(net_bnx2x, "* igb_uio | uio_pci_generic | vfio-pci");
804 RTE_PMD_REGISTER_PCI(net_bnx2xvf, rte_bnx2xvf_pmd);
805 RTE_PMD_REGISTER_PCI_TABLE(net_bnx2xvf, pci_id_bnx2xvf_map);
806 RTE_PMD_REGISTER_KMOD_DEP(net_bnx2xvf, "* igb_uio | vfio-pci");
807
808 RTE_INIT(bnx2x_init_log)
809 {
810         bnx2x_logtype_init = rte_log_register("pmd.net.bnx2x.init");
811         if (bnx2x_logtype_init >= 0)
812                 rte_log_set_level(bnx2x_logtype_init, RTE_LOG_NOTICE);
813         bnx2x_logtype_driver = rte_log_register("pmd.net.bnx2x.driver");
814         if (bnx2x_logtype_driver >= 0)
815                 rte_log_set_level(bnx2x_logtype_driver, RTE_LOG_NOTICE);
816 }