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