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