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