New upstream version 17.11.5
[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 #include <rte_alarm.h>
17 #include <rte_atomic.h>
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 /**
84  * Atomically reads the link status information from global
85  * structure rte_eth_dev.
86  *
87  * @param dev
88  *   - Pointer to the structure rte_eth_dev to read from.
89  *   - Pointer to the buffer to be saved with the link status.
90  *
91  * @return
92  *   - On success, zero.
93  *   - On failure, negative value.
94  */
95 static inline int
96 bnx2x_dev_atomic_read_link_status(struct rte_eth_dev *dev,
97                                   struct rte_eth_link *link)
98 {
99         struct rte_eth_link *dst = link;
100         struct rte_eth_link *src = &dev->data->dev_link;
101
102         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
103                                         *(uint64_t *)src) == 0)
104                 return -1;
105
106         return 0;
107 }
108
109 /**
110  * Atomically writes the link status information into global
111  * structure rte_eth_dev.
112  *
113  * @param dev
114  *   - Pointer to the structure rte_eth_dev to read from.
115  *   - Pointer to the buffer to be saved with the link status.
116  *
117  * @return
118  *   - On success, zero.
119  *   - On failure, negative value.
120  */
121 static inline int
122 bnx2x_dev_atomic_write_link_status(struct rte_eth_dev *dev,
123                                    struct rte_eth_link *link)
124 {
125         struct rte_eth_link *dst = &dev->data->dev_link;
126         struct rte_eth_link *src = link;
127
128         if (rte_atomic64_cmpset((uint64_t *)dst, *(uint64_t *)dst,
129                                         *(uint64_t *)src) == 0)
130                 return -1;
131
132         return 0;
133 }
134
135 static int
136 bnx2x_link_update(struct rte_eth_dev *dev)
137 {
138         struct bnx2x_softc *sc = dev->data->dev_private;
139         struct rte_eth_link orig;
140         struct rte_eth_link link;
141
142         PMD_INIT_FUNC_TRACE(sc);
143
144         bnx2x_link_status_update(sc);
145         memset(&orig, 0, sizeof(orig));
146         memset(&link, 0, sizeof(link));
147         bnx2x_dev_atomic_read_link_status(dev, &orig);
148         mb();
149         link.link_speed = sc->link_vars.line_speed;
150         switch (sc->link_vars.duplex) {
151                 case DUPLEX_FULL:
152                         link.link_duplex = ETH_LINK_FULL_DUPLEX;
153                         break;
154                 case DUPLEX_HALF:
155                         link.link_duplex = ETH_LINK_HALF_DUPLEX;
156                         break;
157         }
158         link.link_autoneg = !(dev->data->dev_conf.link_speeds &
159                         ETH_LINK_SPEED_FIXED);
160         link.link_status = sc->link_vars.link_up;
161         bnx2x_dev_atomic_write_link_status(dev, &link);
162
163         return (link.link_status == orig.link_status) ? -1 : 0;
164 }
165
166 static void
167 bnx2x_interrupt_action(struct rte_eth_dev *dev)
168 {
169         struct bnx2x_softc *sc = dev->data->dev_private;
170         uint32_t link_status;
171
172         bnx2x_intr_legacy(sc, 0);
173
174         if (sc->periodic_flags & PERIODIC_GO)
175                 bnx2x_periodic_callout(sc);
176         link_status = REG_RD(sc, sc->link_params.shmem_base +
177                         offsetof(struct shmem_region,
178                                 port_mb[sc->link_params.port].link_status));
179         if ((link_status & LINK_STATUS_LINK_UP) != dev->data->dev_link.link_status)
180                 bnx2x_link_update(dev);
181 }
182
183 static void
184 bnx2x_interrupt_handler(void *param)
185 {
186         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
187         struct bnx2x_softc *sc = dev->data->dev_private;
188
189         PMD_DEBUG_PERIODIC_LOG(INFO, sc, "Interrupt handled");
190
191         atomic_store_rel_long(&sc->periodic_flags, PERIODIC_STOP);
192         bnx2x_interrupt_action(dev);
193         atomic_store_rel_long(&sc->periodic_flags, PERIODIC_GO);
194         rte_intr_enable(&sc->pci_dev->intr_handle);
195 }
196
197 static void bnx2x_periodic_start(void *param)
198 {
199         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
200         struct bnx2x_softc *sc = dev->data->dev_private;
201         int ret = 0;
202
203         atomic_store_rel_long(&sc->periodic_flags, PERIODIC_GO);
204         bnx2x_interrupt_action(dev);
205         if (IS_PF(sc)) {
206                 ret = rte_eal_alarm_set(BNX2X_SP_TIMER_PERIOD,
207                                         bnx2x_periodic_start, (void *)dev);
208                 if (ret) {
209                         PMD_DRV_LOG(ERR, sc, "Unable to start periodic"
210                                              " timer rc %d", ret);
211                         assert(false && "Unable to start periodic timer");
212                 }
213         }
214 }
215
216 void bnx2x_periodic_stop(void *param)
217 {
218         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
219         struct bnx2x_softc *sc = dev->data->dev_private;
220
221         atomic_store_rel_long(&sc->periodic_flags, PERIODIC_STOP);
222
223         rte_eal_alarm_cancel(bnx2x_periodic_start, (void *)dev);
224 }
225
226 /*
227  * Devops - helper functions can be called from user application
228  */
229
230 static int
231 bnx2x_dev_link_update(struct rte_eth_dev *dev,
232                       __rte_unused int wait_to_complete)
233 {
234 #ifdef RTE_LIBRTE_BNX2X_DEBUG_INIT
235         struct bnx2x_softc *sc = dev->data->dev_private;
236 #endif
237
238         PMD_INIT_FUNC_TRACE(sc);
239
240         return bnx2x_link_update(dev);
241 }
242
243 static int
244 bnx2xvf_dev_link_update(struct rte_eth_dev *dev,
245                         __rte_unused int wait_to_complete)
246 {
247         struct bnx2x_softc *sc = dev->data->dev_private;
248         int ret = 0;
249
250         ret = bnx2x_link_update(dev);
251
252         bnx2x_check_bull(sc);
253         if (sc->old_bulletin.valid_bitmap & (1 << CHANNEL_DOWN)) {
254                 PMD_DRV_LOG(ERR, sc, "PF indicated channel is down."
255                                 "VF device is no longer operational");
256                 dev->data->dev_link.link_status = ETH_LINK_DOWN;
257         }
258
259         return ret;
260 }
261
262 static int
263 bnx2x_dev_configure(struct rte_eth_dev *dev)
264 {
265         struct bnx2x_softc *sc = dev->data->dev_private;
266         int mp_ncpus = sysconf(_SC_NPROCESSORS_CONF);
267
268         PMD_INIT_FUNC_TRACE(sc);
269
270         if (dev->data->dev_conf.rxmode.jumbo_frame)
271                 sc->mtu = dev->data->dev_conf.rxmode.max_rx_pkt_len;
272
273         if (dev->data->nb_tx_queues > dev->data->nb_rx_queues) {
274                 PMD_DRV_LOG(ERR, sc, "The number of TX queues is greater than number of RX queues");
275                 return -EINVAL;
276         }
277
278         sc->num_queues = MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
279         if (sc->num_queues > mp_ncpus) {
280                 PMD_DRV_LOG(ERR, sc, "The number of queues is more than number of CPUs");
281                 return -EINVAL;
282         }
283
284         PMD_DRV_LOG(DEBUG, sc, "num_queues=%d, mtu=%d",
285                        sc->num_queues, sc->mtu);
286
287         /* allocate ilt */
288         if (bnx2x_alloc_ilt_mem(sc) != 0) {
289                 PMD_DRV_LOG(ERR, sc, "bnx2x_alloc_ilt_mem was failed");
290                 return -ENXIO;
291         }
292
293         /* allocate the host hardware/software hsi structures */
294         if (bnx2x_alloc_hsi_mem(sc) != 0) {
295                 PMD_DRV_LOG(ERR, sc, "bnx2x_alloc_hsi_mem was failed");
296                 bnx2x_free_ilt_mem(sc);
297                 return -ENXIO;
298         }
299
300         return 0;
301 }
302
303 static int
304 bnx2x_dev_start(struct rte_eth_dev *dev)
305 {
306         struct bnx2x_softc *sc = dev->data->dev_private;
307         int ret = 0;
308
309         PMD_INIT_FUNC_TRACE(sc);
310
311         /* start the periodic callout */
312         if (sc->periodic_flags & PERIODIC_STOP)
313                 bnx2x_periodic_start(dev);
314
315         ret = bnx2x_init(sc);
316         if (ret) {
317                 PMD_DRV_LOG(DEBUG, sc, "bnx2x_init failed (%d)", ret);
318                 return -1;
319         }
320
321         if (IS_PF(sc)) {
322                 rte_intr_callback_register(&sc->pci_dev->intr_handle,
323                                 bnx2x_interrupt_handler, (void *)dev);
324
325                 if (rte_intr_enable(&sc->pci_dev->intr_handle))
326                         PMD_DRV_LOG(ERR, sc, "rte_intr_enable failed");
327         }
328
329         ret = bnx2x_dev_rx_init(dev);
330         if (ret != 0) {
331                 PMD_DRV_LOG(DEBUG, sc, "bnx2x_dev_rx_init returned error code");
332                 return -3;
333         }
334
335         /* Print important adapter info for the user. */
336         bnx2x_print_adapter_info(sc);
337
338         return ret;
339 }
340
341 static void
342 bnx2x_dev_stop(struct rte_eth_dev *dev)
343 {
344         struct bnx2x_softc *sc = dev->data->dev_private;
345         int ret = 0;
346
347         PMD_INIT_FUNC_TRACE(sc);
348
349         if (IS_PF(sc)) {
350                 rte_intr_disable(&sc->pci_dev->intr_handle);
351                 rte_intr_callback_unregister(&sc->pci_dev->intr_handle,
352                                 bnx2x_interrupt_handler, (void *)dev);
353         }
354
355         /* stop the periodic callout */
356         bnx2x_periodic_stop(dev);
357
358         ret = bnx2x_nic_unload(sc, UNLOAD_NORMAL, FALSE);
359         if (ret) {
360                 PMD_DRV_LOG(DEBUG, sc, "bnx2x_nic_unload failed (%d)", ret);
361                 return;
362         }
363
364         /* Update device link status */
365         if (IS_PF(sc))
366                 bnx2x_dev_link_update(dev, 0);
367         else
368                 bnx2xvf_dev_link_update(dev, 0);
369
370         return;
371 }
372
373 static void
374 bnx2x_dev_close(struct rte_eth_dev *dev)
375 {
376         struct bnx2x_softc *sc = dev->data->dev_private;
377
378         PMD_INIT_FUNC_TRACE(sc);
379
380         if (IS_VF(sc))
381                 bnx2x_vf_close(sc);
382
383         bnx2x_dev_clear_queues(dev);
384         memset(&(dev->data->dev_link), 0 , sizeof(struct rte_eth_link));
385
386         /* free the host hardware/software hsi structures */
387         bnx2x_free_hsi_mem(sc);
388
389         /* free ilt */
390         bnx2x_free_ilt_mem(sc);
391 }
392
393 static void
394 bnx2x_promisc_enable(struct rte_eth_dev *dev)
395 {
396         struct bnx2x_softc *sc = dev->data->dev_private;
397
398         PMD_INIT_FUNC_TRACE(sc);
399         sc->rx_mode = BNX2X_RX_MODE_PROMISC;
400         if (rte_eth_allmulticast_get(dev->data->port_id) == 1)
401                 sc->rx_mode = BNX2X_RX_MODE_ALLMULTI_PROMISC;
402         bnx2x_set_rx_mode(sc);
403 }
404
405 static void
406 bnx2x_promisc_disable(struct rte_eth_dev *dev)
407 {
408         struct bnx2x_softc *sc = dev->data->dev_private;
409
410         PMD_INIT_FUNC_TRACE(sc);
411         sc->rx_mode = BNX2X_RX_MODE_NORMAL;
412         if (rte_eth_allmulticast_get(dev->data->port_id) == 1)
413                 sc->rx_mode = BNX2X_RX_MODE_ALLMULTI;
414         bnx2x_set_rx_mode(sc);
415 }
416
417 static void
418 bnx2x_dev_allmulticast_enable(struct rte_eth_dev *dev)
419 {
420         struct bnx2x_softc *sc = dev->data->dev_private;
421
422         PMD_INIT_FUNC_TRACE(sc);
423         sc->rx_mode = BNX2X_RX_MODE_ALLMULTI;
424         if (rte_eth_promiscuous_get(dev->data->port_id) == 1)
425                 sc->rx_mode = BNX2X_RX_MODE_ALLMULTI_PROMISC;
426         bnx2x_set_rx_mode(sc);
427 }
428
429 static void
430 bnx2x_dev_allmulticast_disable(struct rte_eth_dev *dev)
431 {
432         struct bnx2x_softc *sc = dev->data->dev_private;
433
434         PMD_INIT_FUNC_TRACE(sc);
435         sc->rx_mode = BNX2X_RX_MODE_NORMAL;
436         if (rte_eth_promiscuous_get(dev->data->port_id) == 1)
437                 sc->rx_mode = BNX2X_RX_MODE_PROMISC;
438         bnx2x_set_rx_mode(sc);
439 }
440
441 static int
442 bnx2x_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
443 {
444         struct bnx2x_softc *sc = dev->data->dev_private;
445         uint32_t brb_truncate_discard;
446         uint64_t brb_drops;
447         uint64_t brb_truncates;
448
449         PMD_INIT_FUNC_TRACE(sc);
450
451         bnx2x_stats_handle(sc, STATS_EVENT_UPDATE);
452
453         memset(stats, 0, sizeof (struct rte_eth_stats));
454
455         stats->ipackets =
456                 HILO_U64(sc->eth_stats.total_unicast_packets_received_hi,
457                                 sc->eth_stats.total_unicast_packets_received_lo) +
458                 HILO_U64(sc->eth_stats.total_multicast_packets_received_hi,
459                                 sc->eth_stats.total_multicast_packets_received_lo) +
460                 HILO_U64(sc->eth_stats.total_broadcast_packets_received_hi,
461                                 sc->eth_stats.total_broadcast_packets_received_lo);
462
463         stats->opackets =
464                 HILO_U64(sc->eth_stats.total_unicast_packets_transmitted_hi,
465                                 sc->eth_stats.total_unicast_packets_transmitted_lo) +
466                 HILO_U64(sc->eth_stats.total_multicast_packets_transmitted_hi,
467                                 sc->eth_stats.total_multicast_packets_transmitted_lo) +
468                 HILO_U64(sc->eth_stats.total_broadcast_packets_transmitted_hi,
469                                 sc->eth_stats.total_broadcast_packets_transmitted_lo);
470
471         stats->ibytes =
472                 HILO_U64(sc->eth_stats.total_bytes_received_hi,
473                                 sc->eth_stats.total_bytes_received_lo);
474
475         stats->obytes =
476                 HILO_U64(sc->eth_stats.total_bytes_transmitted_hi,
477                                 sc->eth_stats.total_bytes_transmitted_lo);
478
479         stats->ierrors =
480                 HILO_U64(sc->eth_stats.error_bytes_received_hi,
481                                 sc->eth_stats.error_bytes_received_lo);
482
483         stats->oerrors = 0;
484
485         stats->rx_nombuf =
486                 HILO_U64(sc->eth_stats.no_buff_discard_hi,
487                                 sc->eth_stats.no_buff_discard_lo);
488
489         brb_drops =
490                 HILO_U64(sc->eth_stats.brb_drop_hi,
491                          sc->eth_stats.brb_drop_lo);
492
493         brb_truncates =
494                 HILO_U64(sc->eth_stats.brb_truncate_hi,
495                          sc->eth_stats.brb_truncate_lo);
496
497         brb_truncate_discard = sc->eth_stats.brb_truncate_discard;
498
499         stats->imissed = brb_drops + brb_truncates +
500                          brb_truncate_discard + stats->rx_nombuf;
501
502         return 0;
503 }
504
505 static int
506 bnx2x_get_xstats_names(__rte_unused struct rte_eth_dev *dev,
507                        struct rte_eth_xstat_name *xstats_names,
508                        __rte_unused unsigned limit)
509 {
510         unsigned int i, stat_cnt = RTE_DIM(bnx2x_xstats_strings);
511
512         if (xstats_names != NULL)
513                 for (i = 0; i < stat_cnt; i++)
514                         snprintf(xstats_names[i].name,
515                                 sizeof(xstats_names[i].name),
516                                 "%s",
517                                 bnx2x_xstats_strings[i].name);
518
519         return stat_cnt;
520 }
521
522 static int
523 bnx2x_dev_xstats_get(struct rte_eth_dev *dev, struct rte_eth_xstat *xstats,
524                      unsigned int n)
525 {
526         struct bnx2x_softc *sc = dev->data->dev_private;
527         unsigned int num = RTE_DIM(bnx2x_xstats_strings);
528
529         if (n < num)
530                 return num;
531
532         bnx2x_stats_handle(sc, STATS_EVENT_UPDATE);
533
534         for (num = 0; num < n; num++) {
535                 if (bnx2x_xstats_strings[num].offset_hi !=
536                     bnx2x_xstats_strings[num].offset_lo)
537                         xstats[num].value = HILO_U64(
538                                           *(uint32_t *)((char *)&sc->eth_stats +
539                                           bnx2x_xstats_strings[num].offset_hi),
540                                           *(uint32_t *)((char *)&sc->eth_stats +
541                                           bnx2x_xstats_strings[num].offset_lo));
542                 else
543                         xstats[num].value =
544                                           *(uint64_t *)((char *)&sc->eth_stats +
545                                           bnx2x_xstats_strings[num].offset_lo);
546                 xstats[num].id = num;
547         }
548
549         return num;
550 }
551
552 static void
553 bnx2x_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
554 {
555         struct bnx2x_softc *sc = dev->data->dev_private;
556         dev_info->pci_dev = RTE_ETH_DEV_TO_PCI(dev);
557         dev_info->max_rx_queues  = sc->max_rx_queues;
558         dev_info->max_tx_queues  = sc->max_tx_queues;
559         dev_info->min_rx_bufsize = BNX2X_MIN_RX_BUF_SIZE;
560         dev_info->max_rx_pktlen  = BNX2X_MAX_RX_PKT_LEN;
561         dev_info->max_mac_addrs  = BNX2X_MAX_MAC_ADDRS;
562         dev_info->speed_capa = ETH_LINK_SPEED_10G | ETH_LINK_SPEED_20G;
563 }
564
565 static int
566 bnx2x_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
567                 uint32_t index, uint32_t pool)
568 {
569         struct bnx2x_softc *sc = dev->data->dev_private;
570
571         if (sc->mac_ops.mac_addr_add) {
572                 sc->mac_ops.mac_addr_add(dev, mac_addr, index, pool);
573                 return 0;
574         }
575         return -ENOTSUP;
576 }
577
578 static void
579 bnx2x_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
580 {
581         struct bnx2x_softc *sc = dev->data->dev_private;
582
583         if (sc->mac_ops.mac_addr_remove)
584                 sc->mac_ops.mac_addr_remove(dev, index);
585 }
586
587 static const struct eth_dev_ops bnx2x_eth_dev_ops = {
588         .dev_configure                = bnx2x_dev_configure,
589         .dev_start                    = bnx2x_dev_start,
590         .dev_stop                     = bnx2x_dev_stop,
591         .dev_close                    = bnx2x_dev_close,
592         .promiscuous_enable           = bnx2x_promisc_enable,
593         .promiscuous_disable          = bnx2x_promisc_disable,
594         .allmulticast_enable          = bnx2x_dev_allmulticast_enable,
595         .allmulticast_disable         = bnx2x_dev_allmulticast_disable,
596         .link_update                  = bnx2x_dev_link_update,
597         .stats_get                    = bnx2x_dev_stats_get,
598         .xstats_get                   = bnx2x_dev_xstats_get,
599         .xstats_get_names             = bnx2x_get_xstats_names,
600         .dev_infos_get                = bnx2x_dev_infos_get,
601         .rx_queue_setup               = bnx2x_dev_rx_queue_setup,
602         .rx_queue_release             = bnx2x_dev_rx_queue_release,
603         .tx_queue_setup               = bnx2x_dev_tx_queue_setup,
604         .tx_queue_release             = bnx2x_dev_tx_queue_release,
605         .mac_addr_add                 = bnx2x_mac_addr_add,
606         .mac_addr_remove              = bnx2x_mac_addr_remove,
607 };
608
609 /*
610  * dev_ops for virtual function
611  */
612 static const struct eth_dev_ops bnx2xvf_eth_dev_ops = {
613         .dev_configure                = bnx2x_dev_configure,
614         .dev_start                    = bnx2x_dev_start,
615         .dev_stop                     = bnx2x_dev_stop,
616         .dev_close                    = bnx2x_dev_close,
617         .promiscuous_enable           = bnx2x_promisc_enable,
618         .promiscuous_disable          = bnx2x_promisc_disable,
619         .allmulticast_enable          = bnx2x_dev_allmulticast_enable,
620         .allmulticast_disable         = bnx2x_dev_allmulticast_disable,
621         .link_update                  = bnx2xvf_dev_link_update,
622         .stats_get                    = bnx2x_dev_stats_get,
623         .xstats_get                   = bnx2x_dev_xstats_get,
624         .xstats_get_names             = bnx2x_get_xstats_names,
625         .dev_infos_get                = bnx2x_dev_infos_get,
626         .rx_queue_setup               = bnx2x_dev_rx_queue_setup,
627         .rx_queue_release             = bnx2x_dev_rx_queue_release,
628         .tx_queue_setup               = bnx2x_dev_tx_queue_setup,
629         .tx_queue_release             = bnx2x_dev_tx_queue_release,
630         .mac_addr_add                 = bnx2x_mac_addr_add,
631         .mac_addr_remove              = bnx2x_mac_addr_remove,
632 };
633
634
635 static int
636 bnx2x_common_dev_init(struct rte_eth_dev *eth_dev, int is_vf)
637 {
638         int ret = 0;
639         struct rte_pci_device *pci_dev;
640         struct rte_pci_addr pci_addr;
641         struct bnx2x_softc *sc;
642
643         /* Extract key data structures */
644         sc = eth_dev->data->dev_private;
645         pci_dev = RTE_DEV_TO_PCI(eth_dev->device);
646         pci_addr = pci_dev->addr;
647
648         snprintf(sc->devinfo.name, NAME_SIZE, PCI_SHORT_PRI_FMT ":dpdk-port-%u",
649                  pci_addr.bus, pci_addr.devid, pci_addr.function,
650                  eth_dev->data->port_id);
651
652         PMD_INIT_FUNC_TRACE(sc);
653
654         eth_dev->dev_ops = is_vf ? &bnx2xvf_eth_dev_ops : &bnx2x_eth_dev_ops;
655
656         rte_eth_copy_pci_info(eth_dev, pci_dev);
657
658         sc->pcie_bus    = pci_dev->addr.bus;
659         sc->pcie_device = pci_dev->addr.devid;
660
661         sc->devinfo.vendor_id    = pci_dev->id.vendor_id;
662         sc->devinfo.device_id    = pci_dev->id.device_id;
663         sc->devinfo.subvendor_id = pci_dev->id.subsystem_vendor_id;
664         sc->devinfo.subdevice_id = pci_dev->id.subsystem_device_id;
665
666         if (is_vf)
667                 sc->flags = BNX2X_IS_VF_FLAG;
668
669         sc->pcie_func = pci_dev->addr.function;
670         sc->bar[BAR0].base_addr = (void *)pci_dev->mem_resource[0].addr;
671         if (is_vf)
672                 sc->bar[BAR1].base_addr = (void *)
673                         ((uintptr_t)pci_dev->mem_resource[0].addr + PXP_VF_ADDR_DB_START);
674         else
675                 sc->bar[BAR1].base_addr = pci_dev->mem_resource[2].addr;
676
677         assert(sc->bar[BAR0].base_addr);
678         assert(sc->bar[BAR1].base_addr);
679
680         bnx2x_load_firmware(sc);
681         assert(sc->firmware);
682
683         if (eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf & ETH_RSS_NONFRAG_IPV4_UDP)
684                 sc->udp_rss = 1;
685
686         sc->rx_budget = BNX2X_RX_BUDGET;
687         sc->hc_rx_ticks = BNX2X_RX_TICKS;
688         sc->hc_tx_ticks = BNX2X_TX_TICKS;
689
690         sc->interrupt_mode = INTR_MODE_SINGLE_MSIX;
691         sc->rx_mode = BNX2X_RX_MODE_NORMAL;
692
693         sc->pci_dev = pci_dev;
694         ret = bnx2x_attach(sc);
695         if (ret) {
696                 PMD_DRV_LOG(ERR, sc, "bnx2x_attach failed (%d)", ret);
697                 return ret;
698         }
699
700         /* schedule periodic poll for slowpath link events */
701         if (IS_PF(sc)) {
702                 ret = rte_eal_alarm_set(BNX2X_SP_TIMER_PERIOD,
703                                         bnx2x_periodic_start, (void *)eth_dev);
704                 if (ret) {
705                         PMD_DRV_LOG(ERR, sc, "Unable to start periodic"
706                                              " timer rc %d", ret);
707                         return -EINVAL;
708                 }
709         }
710
711         eth_dev->data->mac_addrs = (struct ether_addr *)sc->link_params.mac_addr;
712
713         PMD_DRV_LOG(INFO, sc, "pcie_bus=%d, pcie_device=%d",
714                         sc->pcie_bus, sc->pcie_device);
715         PMD_DRV_LOG(INFO, sc, "bar0.addr=%p, bar1.addr=%p",
716                         sc->bar[BAR0].base_addr, sc->bar[BAR1].base_addr);
717         PMD_DRV_LOG(INFO, sc, "port=%d, path=%d, vnic=%d, func=%d",
718                         PORT_ID(sc), PATH_ID(sc), VNIC_ID(sc), FUNC_ID(sc));
719         PMD_DRV_LOG(INFO, sc, "portID=%d vendorID=0x%x deviceID=0x%x",
720                         eth_dev->data->port_id, pci_dev->id.vendor_id, pci_dev->id.device_id);
721
722         if (IS_VF(sc)) {
723                 rte_spinlock_init(&sc->vf2pf_lock);
724
725                 ret = bnx2x_dma_alloc(sc, sizeof(struct bnx2x_vf_mbx_msg),
726                                       &sc->vf2pf_mbox_mapping, "vf2pf_mbox",
727                                       RTE_CACHE_LINE_SIZE);
728                 if (ret)
729                         goto out;
730
731                 sc->vf2pf_mbox = (struct bnx2x_vf_mbx_msg *)
732                                          sc->vf2pf_mbox_mapping.vaddr;
733
734                 ret = bnx2x_dma_alloc(sc, sizeof(struct bnx2x_vf_bulletin),
735                                       &sc->pf2vf_bulletin_mapping, "vf2pf_bull",
736                                       RTE_CACHE_LINE_SIZE);
737                 if (ret)
738                         goto out;
739
740                 sc->pf2vf_bulletin = (struct bnx2x_vf_bulletin *)
741                                              sc->pf2vf_bulletin_mapping.vaddr;
742
743                 ret = bnx2x_vf_get_resources(sc, sc->max_tx_queues,
744                                              sc->max_rx_queues);
745                 if (ret)
746                         goto out;
747         }
748
749         return 0;
750
751 out:
752         bnx2x_periodic_stop(eth_dev);
753         return ret;
754 }
755
756 static int
757 eth_bnx2x_dev_init(struct rte_eth_dev *eth_dev)
758 {
759 #ifdef RTE_LIBRTE_BNX2X_DEBUG_INIT
760         struct bnx2x_softc *sc = eth_dev->data->dev_private;
761 #endif
762         PMD_INIT_FUNC_TRACE(sc);
763         return bnx2x_common_dev_init(eth_dev, 0);
764 }
765
766 static int
767 eth_bnx2xvf_dev_init(struct rte_eth_dev *eth_dev)
768 {
769 #ifdef RTE_LIBRTE_BNX2X_DEBUG_INIT
770         struct bnx2x_softc *sc = eth_dev->data->dev_private;
771 #endif
772         PMD_INIT_FUNC_TRACE(sc);
773         return bnx2x_common_dev_init(eth_dev, 1);
774 }
775
776 static struct rte_pci_driver rte_bnx2x_pmd;
777 static struct rte_pci_driver rte_bnx2xvf_pmd;
778
779 static int eth_bnx2x_pci_probe(struct rte_pci_driver *pci_drv,
780         struct rte_pci_device *pci_dev)
781 {
782         struct rte_eth_dev *eth_dev;
783         int ret;
784
785         eth_dev = rte_eth_dev_pci_allocate(pci_dev, sizeof(struct bnx2x_softc));
786         if (!eth_dev)
787                 return -ENOMEM;
788
789         if (pci_drv == &rte_bnx2x_pmd)
790                 ret = eth_bnx2x_dev_init(eth_dev);
791         else if (pci_drv == &rte_bnx2xvf_pmd)
792                 ret = eth_bnx2xvf_dev_init(eth_dev);
793         else
794                 ret = -EINVAL;
795
796         if (ret)
797                 rte_eth_dev_pci_release(eth_dev);
798
799         return ret;
800 }
801
802 static int eth_bnx2x_pci_remove(struct rte_pci_device *pci_dev)
803 {
804         return rte_eth_dev_pci_generic_remove(pci_dev, NULL);
805 }
806
807 static struct rte_pci_driver rte_bnx2x_pmd = {
808         .id_table = pci_id_bnx2x_map,
809         .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
810         .probe = eth_bnx2x_pci_probe,
811         .remove = eth_bnx2x_pci_remove,
812 };
813
814 /*
815  * virtual function driver struct
816  */
817 static struct rte_pci_driver rte_bnx2xvf_pmd = {
818         .id_table = pci_id_bnx2xvf_map,
819         .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
820         .probe = eth_bnx2x_pci_probe,
821         .remove = eth_bnx2x_pci_remove,
822 };
823
824 RTE_PMD_REGISTER_PCI(net_bnx2x, rte_bnx2x_pmd);
825 RTE_PMD_REGISTER_PCI_TABLE(net_bnx2x, pci_id_bnx2x_map);
826 RTE_PMD_REGISTER_KMOD_DEP(net_bnx2x, "* igb_uio | uio_pci_generic | vfio-pci");
827 RTE_PMD_REGISTER_PCI(net_bnx2xvf, rte_bnx2xvf_pmd);
828 RTE_PMD_REGISTER_PCI_TABLE(net_bnx2xvf, pci_id_bnx2xvf_map);
829 RTE_PMD_REGISTER_KMOD_DEP(net_bnx2xvf, "* igb_uio | vfio-pci");