mpipe: fix link initialization ordering
[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
16 /*
17  * The set of PCI devices this driver supports
18  */
19 static struct rte_pci_id pci_id_bnx2x_map[] = {
20 #define RTE_PCI_DEV_ID_DECL_BNX2X(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
21 #include "rte_pci_dev_ids.h"
22         { .vendor_id = 0, }
23 };
24
25 static struct rte_pci_id pci_id_bnx2xvf_map[] = {
26 #define RTE_PCI_DEV_ID_DECL_BNX2XVF(vend, dev) {RTE_PCI_DEVICE(vend, dev)},
27 #include "rte_pci_dev_ids.h"
28         { .vendor_id = 0, }
29 };
30
31 static void
32 bnx2x_link_update(struct rte_eth_dev *dev)
33 {
34         struct bnx2x_softc *sc = dev->data->dev_private;
35
36         PMD_INIT_FUNC_TRACE();
37         bnx2x_link_status_update(sc);
38         mb();
39         dev->data->dev_link.link_speed = sc->link_vars.line_speed;
40         switch (sc->link_vars.duplex) {
41                 case DUPLEX_FULL:
42                         dev->data->dev_link.link_duplex = ETH_LINK_FULL_DUPLEX;
43                         break;
44                 case DUPLEX_HALF:
45                         dev->data->dev_link.link_duplex = ETH_LINK_HALF_DUPLEX;
46                         break;
47                 default:
48                         dev->data->dev_link.link_duplex = ETH_LINK_AUTONEG_DUPLEX;
49         }
50         dev->data->dev_link.link_status = sc->link_vars.link_up;
51 }
52
53 static void
54 bnx2x_interrupt_action(struct rte_eth_dev *dev)
55 {
56         struct bnx2x_softc *sc = dev->data->dev_private;
57         uint32_t link_status;
58
59         PMD_DEBUG_PERIODIC_LOG(INFO, "Interrupt handled");
60
61         if (bnx2x_intr_legacy(sc, 0))
62                 DELAY_MS(250);
63         if (sc->periodic_flags & PERIODIC_GO)
64                 bnx2x_periodic_callout(sc);
65         link_status = REG_RD(sc, sc->link_params.shmem_base +
66                         offsetof(struct shmem_region,
67                                 port_mb[sc->link_params.port].link_status));
68         if ((link_status & LINK_STATUS_LINK_UP) != dev->data->dev_link.link_status)
69                 bnx2x_link_update(dev);
70 }
71
72 static __rte_unused void
73 bnx2x_interrupt_handler(__rte_unused struct rte_intr_handle *handle, void *param)
74 {
75         struct rte_eth_dev *dev = (struct rte_eth_dev *)param;
76
77         bnx2x_interrupt_action(dev);
78         rte_intr_enable(&(dev->pci_dev->intr_handle));
79 }
80
81 /*
82  * Devops - helper functions can be called from user application
83  */
84
85 static int
86 bnx2x_dev_configure(struct rte_eth_dev *dev)
87 {
88         struct bnx2x_softc *sc = dev->data->dev_private;
89         int mp_ncpus = sysconf(_SC_NPROCESSORS_CONF);
90         int ret;
91
92         PMD_INIT_FUNC_TRACE();
93
94         if (dev->data->dev_conf.rxmode.jumbo_frame)
95                 sc->mtu = dev->data->dev_conf.rxmode.max_rx_pkt_len;
96
97         if (dev->data->nb_tx_queues > dev->data->nb_rx_queues) {
98                 PMD_DRV_LOG(ERR, "The number of TX queues is greater than number of RX queues");
99                 return -EINVAL;
100         }
101
102         sc->num_queues = MAX(dev->data->nb_rx_queues, dev->data->nb_tx_queues);
103         if (sc->num_queues > mp_ncpus) {
104                 PMD_DRV_LOG(ERR, "The number of queues is more than number of CPUs");
105                 return -EINVAL;
106         }
107
108         PMD_DRV_LOG(DEBUG, "num_queues=%d, mtu=%d",
109                        sc->num_queues, sc->mtu);
110
111         /* allocate ilt */
112         if (bnx2x_alloc_ilt_mem(sc) != 0) {
113                 PMD_DRV_LOG(ERR, "bnx2x_alloc_ilt_mem was failed");
114                 return -ENXIO;
115         }
116
117         /* allocate the host hardware/software hsi structures */
118         if (bnx2x_alloc_hsi_mem(sc) != 0) {
119                 PMD_DRV_LOG(ERR, "bnx2x_alloc_hsi_mem was failed");
120                 bnx2x_free_ilt_mem(sc);
121                 return -ENXIO;
122         }
123
124         if (IS_VF(sc)) {
125                 if (bnx2x_dma_alloc(sc, sizeof(struct bnx2x_vf_mbx_msg),
126                                   &sc->vf2pf_mbox_mapping, "vf2pf_mbox",
127                                   RTE_CACHE_LINE_SIZE) != 0)
128                         return -ENOMEM;
129
130                 sc->vf2pf_mbox = (struct bnx2x_vf_mbx_msg *)sc->vf2pf_mbox_mapping.vaddr;
131                 if (bnx2x_dma_alloc(sc, sizeof(struct bnx2x_vf_bulletin),
132                                   &sc->pf2vf_bulletin_mapping, "vf2pf_bull",
133                                   RTE_CACHE_LINE_SIZE) != 0)
134                         return -ENOMEM;
135
136                 sc->pf2vf_bulletin = (struct bnx2x_vf_bulletin *)sc->pf2vf_bulletin_mapping.vaddr;
137
138                 ret = bnx2x_vf_get_resources(sc, sc->num_queues, sc->num_queues);
139                 if (ret)
140                         return ret;
141         }
142
143         return 0;
144 }
145
146 static int
147 bnx2x_dev_start(struct rte_eth_dev *dev)
148 {
149         struct bnx2x_softc *sc = dev->data->dev_private;
150         int ret = 0;
151
152         PMD_INIT_FUNC_TRACE();
153
154         ret = bnx2x_init(sc);
155         if (ret) {
156                 PMD_DRV_LOG(DEBUG, "bnx2x_init failed (%d)", ret);
157                 return -1;
158         }
159
160         if (IS_PF(sc)) {
161                 rte_intr_callback_register(&(dev->pci_dev->intr_handle),
162                                 bnx2x_interrupt_handler, (void *)dev);
163
164                 if(rte_intr_enable(&(dev->pci_dev->intr_handle)))
165                         PMD_DRV_LOG(ERR, "rte_intr_enable failed");
166         }
167
168         ret = bnx2x_dev_rx_init(dev);
169         if (ret != 0) {
170                 PMD_DRV_LOG(DEBUG, "bnx2x_dev_rx_init returned error code");
171                 return -3;
172         }
173
174         /* Print important adapter info for the user. */
175         bnx2x_print_adapter_info(sc);
176
177         DELAY_MS(2500);
178
179         return ret;
180 }
181
182 static void
183 bnx2x_dev_stop(struct rte_eth_dev *dev)
184 {
185         struct bnx2x_softc *sc = dev->data->dev_private;
186         int ret = 0;
187
188         PMD_INIT_FUNC_TRACE();
189
190         if (IS_PF(sc)) {
191                 rte_intr_disable(&(dev->pci_dev->intr_handle));
192                 rte_intr_callback_unregister(&(dev->pci_dev->intr_handle),
193                                 bnx2x_interrupt_handler, (void *)dev);
194         }
195
196         ret = bnx2x_nic_unload(sc, UNLOAD_NORMAL, FALSE);
197         if (ret) {
198                 PMD_DRV_LOG(DEBUG, "bnx2x_nic_unload failed (%d)", ret);
199                 return;
200         }
201
202         return;
203 }
204
205 static void
206 bnx2x_dev_close(struct rte_eth_dev *dev)
207 {
208         struct bnx2x_softc *sc = dev->data->dev_private;
209
210         PMD_INIT_FUNC_TRACE();
211
212         if (IS_VF(sc))
213                 bnx2x_vf_close(sc);
214
215         bnx2x_dev_clear_queues(dev);
216         memset(&(dev->data->dev_link), 0 , sizeof(struct rte_eth_link));
217
218         /* free the host hardware/software hsi structures */
219         bnx2x_free_hsi_mem(sc);
220
221         /* free ilt */
222         bnx2x_free_ilt_mem(sc);
223 }
224
225 static void
226 bnx2x_promisc_enable(struct rte_eth_dev *dev)
227 {
228         struct bnx2x_softc *sc = dev->data->dev_private;
229
230         PMD_INIT_FUNC_TRACE();
231         sc->rx_mode = BNX2X_RX_MODE_PROMISC;
232         bnx2x_set_rx_mode(sc);
233 }
234
235 static void
236 bnx2x_promisc_disable(struct rte_eth_dev *dev)
237 {
238         struct bnx2x_softc *sc = dev->data->dev_private;
239
240         PMD_INIT_FUNC_TRACE();
241         sc->rx_mode = BNX2X_RX_MODE_NORMAL;
242         bnx2x_set_rx_mode(sc);
243 }
244
245 static void
246 bnx2x_dev_allmulticast_enable(struct rte_eth_dev *dev)
247 {
248         struct bnx2x_softc *sc = dev->data->dev_private;
249
250         PMD_INIT_FUNC_TRACE();
251         sc->rx_mode = BNX2X_RX_MODE_ALLMULTI;
252         bnx2x_set_rx_mode(sc);
253 }
254
255 static void
256 bnx2x_dev_allmulticast_disable(struct rte_eth_dev *dev)
257 {
258         struct bnx2x_softc *sc = dev->data->dev_private;
259
260         PMD_INIT_FUNC_TRACE();
261         sc->rx_mode = BNX2X_RX_MODE_NORMAL;
262         bnx2x_set_rx_mode(sc);
263 }
264
265 static int
266 bnx2x_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
267 {
268         PMD_INIT_FUNC_TRACE();
269
270         int old_link_status = dev->data->dev_link.link_status;
271
272         bnx2x_link_update(dev);
273
274         return old_link_status == dev->data->dev_link.link_status ? -1 : 0;
275 }
276
277 static int
278 bnx2xvf_dev_link_update(struct rte_eth_dev *dev, __rte_unused int wait_to_complete)
279 {
280         int old_link_status = dev->data->dev_link.link_status;
281         struct bnx2x_softc *sc = dev->data->dev_private;
282
283         bnx2x_link_update(dev);
284
285         bnx2x_check_bull(sc);
286         if (sc->old_bulletin.valid_bitmap & (1 << CHANNEL_DOWN)) {
287                 PMD_DRV_LOG(ERR, "PF indicated channel is down."
288                                 "VF device is no longer operational");
289                 dev->data->dev_link.link_status = 0;
290         }
291
292         return old_link_status == dev->data->dev_link.link_status ? -1 : 0;
293 }
294
295 static void
296 bnx2x_dev_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
297 {
298         struct bnx2x_softc *sc = dev->data->dev_private;
299
300         PMD_INIT_FUNC_TRACE();
301
302         bnx2x_stats_handle(sc, STATS_EVENT_UPDATE);
303
304         memset(stats, 0, sizeof (struct rte_eth_stats));
305
306         stats->ipackets =
307                 HILO_U64(sc->eth_stats.total_unicast_packets_received_hi,
308                                 sc->eth_stats.total_unicast_packets_received_lo) +
309                 HILO_U64(sc->eth_stats.total_multicast_packets_received_hi,
310                                 sc->eth_stats.total_multicast_packets_received_lo) +
311                 HILO_U64(sc->eth_stats.total_broadcast_packets_received_hi,
312                                 sc->eth_stats.total_broadcast_packets_received_lo);
313
314         stats->opackets =
315                 HILO_U64(sc->eth_stats.total_unicast_packets_transmitted_hi,
316                                 sc->eth_stats.total_unicast_packets_transmitted_lo) +
317                 HILO_U64(sc->eth_stats.total_multicast_packets_transmitted_hi,
318                                 sc->eth_stats.total_multicast_packets_transmitted_lo) +
319                 HILO_U64(sc->eth_stats.total_broadcast_packets_transmitted_hi,
320                                 sc->eth_stats.total_broadcast_packets_transmitted_lo);
321
322         stats->ibytes =
323                 HILO_U64(sc->eth_stats.total_bytes_received_hi,
324                                 sc->eth_stats.total_bytes_received_lo);
325
326         stats->obytes =
327                 HILO_U64(sc->eth_stats.total_bytes_transmitted_hi,
328                                 sc->eth_stats.total_bytes_transmitted_lo);
329
330         stats->ierrors =
331                 HILO_U64(sc->eth_stats.error_bytes_received_hi,
332                                 sc->eth_stats.error_bytes_received_lo);
333
334         stats->oerrors = 0;
335
336         stats->rx_nombuf =
337                 HILO_U64(sc->eth_stats.no_buff_discard_hi,
338                                 sc->eth_stats.no_buff_discard_lo);
339 }
340
341 static void
342 bnx2x_dev_infos_get(struct rte_eth_dev *dev, __rte_unused struct rte_eth_dev_info *dev_info)
343 {
344         struct bnx2x_softc *sc = dev->data->dev_private;
345         dev_info->max_rx_queues  = sc->max_rx_queues;
346         dev_info->max_tx_queues  = sc->max_tx_queues;
347         dev_info->min_rx_bufsize = BNX2X_MIN_RX_BUF_SIZE;
348         dev_info->max_rx_pktlen  = BNX2X_MAX_RX_PKT_LEN;
349         dev_info->max_mac_addrs  = BNX2X_MAX_MAC_ADDRS;
350 }
351
352 static void
353 bnx2x_mac_addr_add(struct rte_eth_dev *dev, struct ether_addr *mac_addr,
354                 uint32_t index, uint32_t pool)
355 {
356         struct bnx2x_softc *sc = dev->data->dev_private;
357
358         if (sc->mac_ops.mac_addr_add)
359                 sc->mac_ops.mac_addr_add(dev, mac_addr, index, pool);
360 }
361
362 static void
363 bnx2x_mac_addr_remove(struct rte_eth_dev *dev, uint32_t index)
364 {
365         struct bnx2x_softc *sc = dev->data->dev_private;
366
367         if (sc->mac_ops.mac_addr_remove)
368                 sc->mac_ops.mac_addr_remove(dev, index);
369 }
370
371 static struct eth_dev_ops bnx2x_eth_dev_ops = {
372         .dev_configure                = bnx2x_dev_configure,
373         .dev_start                    = bnx2x_dev_start,
374         .dev_stop                     = bnx2x_dev_stop,
375         .dev_close                    = bnx2x_dev_close,
376         .promiscuous_enable           = bnx2x_promisc_enable,
377         .promiscuous_disable          = bnx2x_promisc_disable,
378         .allmulticast_enable          = bnx2x_dev_allmulticast_enable,
379         .allmulticast_disable         = bnx2x_dev_allmulticast_disable,
380         .link_update                  = bnx2x_dev_link_update,
381         .stats_get                    = bnx2x_dev_stats_get,
382         .dev_infos_get                = bnx2x_dev_infos_get,
383         .rx_queue_setup               = bnx2x_dev_rx_queue_setup,
384         .rx_queue_release             = bnx2x_dev_rx_queue_release,
385         .tx_queue_setup               = bnx2x_dev_tx_queue_setup,
386         .tx_queue_release             = bnx2x_dev_tx_queue_release,
387         .mac_addr_add                 = bnx2x_mac_addr_add,
388         .mac_addr_remove              = bnx2x_mac_addr_remove,
389 };
390
391 /*
392  * dev_ops for virtual function
393  */
394 static struct eth_dev_ops bnx2xvf_eth_dev_ops = {
395         .dev_configure                = bnx2x_dev_configure,
396         .dev_start                    = bnx2x_dev_start,
397         .dev_stop                     = bnx2x_dev_stop,
398         .dev_close                    = bnx2x_dev_close,
399         .promiscuous_enable           = bnx2x_promisc_enable,
400         .promiscuous_disable          = bnx2x_promisc_disable,
401         .allmulticast_enable          = bnx2x_dev_allmulticast_enable,
402         .allmulticast_disable         = bnx2x_dev_allmulticast_disable,
403         .link_update                  = bnx2xvf_dev_link_update,
404         .stats_get                    = bnx2x_dev_stats_get,
405         .dev_infos_get                = bnx2x_dev_infos_get,
406         .rx_queue_setup               = bnx2x_dev_rx_queue_setup,
407         .rx_queue_release             = bnx2x_dev_rx_queue_release,
408         .tx_queue_setup               = bnx2x_dev_tx_queue_setup,
409         .tx_queue_release             = bnx2x_dev_tx_queue_release,
410         .mac_addr_add                 = bnx2x_mac_addr_add,
411         .mac_addr_remove              = bnx2x_mac_addr_remove,
412 };
413
414
415 static int
416 bnx2x_common_dev_init(struct rte_eth_dev *eth_dev, int is_vf)
417 {
418         int ret = 0;
419         struct rte_pci_device *pci_dev;
420         struct bnx2x_softc *sc;
421
422         PMD_INIT_FUNC_TRACE();
423
424         eth_dev->dev_ops = is_vf ? &bnx2xvf_eth_dev_ops : &bnx2x_eth_dev_ops;
425         pci_dev = eth_dev->pci_dev;
426
427         rte_eth_copy_pci_info(eth_dev, pci_dev);
428
429         sc = eth_dev->data->dev_private;
430         sc->pcie_bus    = pci_dev->addr.bus;
431         sc->pcie_device = pci_dev->addr.devid;
432
433         if (is_vf)
434                 sc->flags = BNX2X_IS_VF_FLAG;
435
436         sc->devinfo.vendor_id    = pci_dev->id.vendor_id;
437         sc->devinfo.device_id    = pci_dev->id.device_id;
438         sc->devinfo.subvendor_id = pci_dev->id.subsystem_vendor_id;
439         sc->devinfo.subdevice_id = pci_dev->id.subsystem_device_id;
440
441         sc->pcie_func = pci_dev->addr.function;
442         sc->bar[BAR0].base_addr = (void *)pci_dev->mem_resource[0].addr;
443         if (is_vf)
444                 sc->bar[BAR1].base_addr = (void *)
445                         ((uintptr_t)pci_dev->mem_resource[0].addr + PXP_VF_ADDR_DB_START);
446         else
447                 sc->bar[BAR1].base_addr = pci_dev->mem_resource[2].addr;
448
449         assert(sc->bar[BAR0].base_addr);
450         assert(sc->bar[BAR1].base_addr);
451
452         bnx2x_load_firmware(sc);
453         assert(sc->firmware);
454
455         if (eth_dev->data->dev_conf.rx_adv_conf.rss_conf.rss_hf & ETH_RSS_NONFRAG_IPV4_UDP)
456                 sc->udp_rss = 1;
457
458         sc->rx_budget = BNX2X_RX_BUDGET;
459         sc->hc_rx_ticks = BNX2X_RX_TICKS;
460         sc->hc_tx_ticks = BNX2X_TX_TICKS;
461
462         sc->interrupt_mode = INTR_MODE_SINGLE_MSIX;
463         sc->rx_mode = BNX2X_RX_MODE_NORMAL;
464
465         sc->pci_dev = pci_dev;
466         ret = bnx2x_attach(sc);
467         if (ret) {
468                 PMD_DRV_LOG(ERR, "bnx2x_attach failed (%d)", ret);
469         }
470
471         eth_dev->data->mac_addrs = (struct ether_addr *)sc->link_params.mac_addr;
472
473         PMD_DRV_LOG(INFO, "pcie_bus=%d, pcie_device=%d",
474                         sc->pcie_bus, sc->pcie_device);
475         PMD_DRV_LOG(INFO, "bar0.addr=%p, bar1.addr=%p",
476                         sc->bar[BAR0].base_addr, sc->bar[BAR1].base_addr);
477         PMD_DRV_LOG(INFO, "port=%d, path=%d, vnic=%d, func=%d",
478                         PORT_ID(sc), PATH_ID(sc), VNIC_ID(sc), FUNC_ID(sc));
479         PMD_DRV_LOG(INFO, "portID=%d vendorID=0x%x deviceID=0x%x",
480                         eth_dev->data->port_id, pci_dev->id.vendor_id, pci_dev->id.device_id);
481
482         return ret;
483 }
484
485 static int
486 eth_bnx2x_dev_init(struct rte_eth_dev *eth_dev)
487 {
488         PMD_INIT_FUNC_TRACE();
489         return bnx2x_common_dev_init(eth_dev, 0);
490 }
491
492 static int
493 eth_bnx2xvf_dev_init(struct rte_eth_dev *eth_dev)
494 {
495         PMD_INIT_FUNC_TRACE();
496         return bnx2x_common_dev_init(eth_dev, 1);
497 }
498
499 static struct eth_driver rte_bnx2x_pmd = {
500         .pci_drv = {
501                 .name = "rte_bnx2x_pmd",
502                 .id_table = pci_id_bnx2x_map,
503                 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
504         },
505         .eth_dev_init = eth_bnx2x_dev_init,
506         .dev_private_size = sizeof(struct bnx2x_softc),
507 };
508
509 /*
510  * virtual function driver struct
511  */
512 static struct eth_driver rte_bnx2xvf_pmd = {
513         .pci_drv = {
514                 .name = "rte_bnx2xvf_pmd",
515                 .id_table = pci_id_bnx2xvf_map,
516                 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
517         },
518         .eth_dev_init = eth_bnx2xvf_dev_init,
519         .dev_private_size = sizeof(struct bnx2x_softc),
520 };
521
522 static int rte_bnx2x_pmd_init(const char *name __rte_unused, const char *params __rte_unused)
523 {
524         PMD_INIT_FUNC_TRACE();
525         rte_eth_driver_register(&rte_bnx2x_pmd);
526
527         return 0;
528 }
529
530 static int rte_bnx2xvf_pmd_init(const char *name __rte_unused, const char *params __rte_unused)
531 {
532         PMD_INIT_FUNC_TRACE();
533         rte_eth_driver_register(&rte_bnx2xvf_pmd);
534
535         return 0;
536 }
537
538 static struct rte_driver rte_bnx2x_driver = {
539         .type = PMD_PDEV,
540         .init = rte_bnx2x_pmd_init,
541 };
542
543 static struct rte_driver rte_bnx2xvf_driver = {
544         .type = PMD_PDEV,
545         .init = rte_bnx2xvf_pmd_init,
546 };
547
548 PMD_REGISTER_DRIVER(rte_bnx2x_driver);
549 PMD_REGISTER_DRIVER(rte_bnx2xvf_driver);