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