cxgbe: add pmd skeleton
[dpdk.git] / drivers / net / cxgbe / cxgbe_ethdev.c
1 /*-
2  *   BSD LICENSE
3  *
4  *   Copyright(c) 2014-2015 Chelsio Communications.
5  *   All rights reserved.
6  *
7  *   Redistribution and use in source and binary forms, with or without
8  *   modification, are permitted provided that the following conditions
9  *   are met:
10  *
11  *     * Redistributions of source code must retain the above copyright
12  *       notice, this list of conditions and the following disclaimer.
13  *     * Redistributions in binary form must reproduce the above copyright
14  *       notice, this list of conditions and the following disclaimer in
15  *       the documentation and/or other materials provided with the
16  *       distribution.
17  *     * Neither the name of Chelsio Communications nor the names of its
18  *       contributors may be used to endorse or promote products derived
19  *       from this software without specific prior written permission.
20  *
21  *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  *   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  *   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  *   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  *   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  *   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  *   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  *   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32  */
33
34 #include <sys/queue.h>
35 #include <stdio.h>
36 #include <errno.h>
37 #include <stdint.h>
38 #include <string.h>
39 #include <unistd.h>
40 #include <stdarg.h>
41 #include <inttypes.h>
42 #include <netinet/in.h>
43
44 #include <rte_byteorder.h>
45 #include <rte_common.h>
46 #include <rte_cycles.h>
47 #include <rte_interrupts.h>
48 #include <rte_log.h>
49 #include <rte_debug.h>
50 #include <rte_pci.h>
51 #include <rte_atomic.h>
52 #include <rte_branch_prediction.h>
53 #include <rte_memory.h>
54 #include <rte_memzone.h>
55 #include <rte_tailq.h>
56 #include <rte_eal.h>
57 #include <rte_alarm.h>
58 #include <rte_ether.h>
59 #include <rte_ethdev.h>
60 #include <rte_atomic.h>
61 #include <rte_malloc.h>
62 #include <rte_random.h>
63 #include <rte_dev.h>
64
65 #include "cxgbe.h"
66
67 /*
68  * Macros needed to support the PCI Device ID Table ...
69  */
70 #define CH_PCI_DEVICE_ID_TABLE_DEFINE_BEGIN \
71         static struct rte_pci_id cxgb4_pci_tbl[] = {
72 #define CH_PCI_DEVICE_ID_FUNCTION 0x4
73
74 #define PCI_VENDOR_ID_CHELSIO 0x1425
75
76 #define CH_PCI_ID_TABLE_ENTRY(devid) \
77                 { RTE_PCI_DEVICE(PCI_VENDOR_ID_CHELSIO, (devid)) }
78
79 #define CH_PCI_DEVICE_ID_TABLE_DEFINE_END \
80                 { .vendor_id = 0, } \
81         }
82
83 /*
84  *... and the PCI ID Table itself ...
85  */
86 #include "t4_pci_id_tbl.h"
87
88 static struct eth_dev_ops cxgbe_eth_dev_ops = {
89 };
90
91 /*
92  * Initialize driver
93  * It returns 0 on success.
94  */
95 static int eth_cxgbe_dev_init(struct rte_eth_dev *eth_dev)
96 {
97         struct rte_pci_device *pci_dev;
98         struct port_info *pi = (struct port_info *)(eth_dev->data->dev_private);
99         struct adapter *adapter = NULL;
100         char name[RTE_ETH_NAME_MAX_LEN];
101         int err = 0;
102
103         CXGBE_FUNC_TRACE();
104
105         eth_dev->dev_ops = &cxgbe_eth_dev_ops;
106
107         /* for secondary processes, we don't initialise any further as primary
108          * has already done this work.
109          */
110         if (rte_eal_process_type() != RTE_PROC_PRIMARY)
111                 return 0;
112
113         pci_dev = eth_dev->pci_dev;
114         snprintf(name, sizeof(name), "cxgbeadapter%d", eth_dev->data->port_id);
115         adapter = rte_zmalloc(name, sizeof(*adapter), 0);
116         if (!adapter)
117                 return -1;
118
119         adapter->use_unpacked_mode = 1;
120         adapter->regs = (void *)pci_dev->mem_resource[0].addr;
121         if (!adapter->regs) {
122                 dev_err(adapter, "%s: cannot map device registers\n", __func__);
123                 err = -ENOMEM;
124                 goto out_free_adapter;
125         }
126         adapter->pdev = pci_dev;
127         adapter->eth_dev = eth_dev;
128         pi->adapter = adapter;
129
130         err = cxgbe_probe(adapter);
131         if (err)
132                 dev_err(adapter, "%s: cxgbe probe failed with err %d\n",
133                         __func__, err);
134
135 out_free_adapter:
136         return err;
137 }
138
139 static struct eth_driver rte_cxgbe_pmd = {
140         {
141                 .name = "rte_cxgbe_pmd",
142                 .id_table = cxgb4_pci_tbl,
143                 .drv_flags = RTE_PCI_DRV_NEED_MAPPING | RTE_PCI_DRV_INTR_LSC,
144         },
145         .eth_dev_init = eth_cxgbe_dev_init,
146         .dev_private_size = sizeof(struct port_info),
147 };
148
149 /*
150  * Driver initialization routine.
151  * Invoked once at EAL init time.
152  * Register itself as the [Poll Mode] Driver of PCI CXGBE devices.
153  */
154 static int rte_cxgbe_pmd_init(const char *name __rte_unused,
155                               const char *params __rte_unused)
156 {
157         CXGBE_FUNC_TRACE();
158
159         rte_eth_driver_register(&rte_cxgbe_pmd);
160         return 0;
161 }
162
163 static struct rte_driver rte_cxgbe_driver = {
164         .name = "cxgbe_driver",
165         .type = PMD_PDEV,
166         .init = rte_cxgbe_pmd_init,
167 };
168
169 PMD_REGISTER_DRIVER(rte_cxgbe_driver);