net/sfc: add device start and stop operations
[dpdk.git] / drivers / net / sfc / sfc_ethdev.c
1 /*-
2  * Copyright (c) 2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * This software was jointly developed between OKTET Labs (under contract
6  * for Solarflare) and Solarflare Communications, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright notice,
14  *    this list of conditions and the following disclaimer in the documentation
15  *    and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
19  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
21  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
27  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29
30 #include <rte_dev.h>
31 #include <rte_ethdev.h>
32 #include <rte_pci.h>
33
34 #include "efx.h"
35
36 #include "sfc.h"
37 #include "sfc_debug.h"
38 #include "sfc_log.h"
39 #include "sfc_kvargs.h"
40
41
42 static void
43 sfc_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
44 {
45         struct sfc_adapter *sa = dev->data->dev_private;
46
47         sfc_log_init(sa, "entry");
48
49         dev_info->pci_dev = RTE_DEV_TO_PCI(dev->device);
50 }
51
52 static int
53 sfc_dev_configure(struct rte_eth_dev *dev)
54 {
55         struct rte_eth_dev_data *dev_data = dev->data;
56         struct sfc_adapter *sa = dev_data->dev_private;
57         int rc;
58
59         sfc_log_init(sa, "entry n_rxq=%u n_txq=%u",
60                      dev_data->nb_rx_queues, dev_data->nb_tx_queues);
61
62         sfc_adapter_lock(sa);
63         switch (sa->state) {
64         case SFC_ADAPTER_CONFIGURED:
65                 sfc_close(sa);
66                 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
67                 /* FALLTHROUGH */
68         case SFC_ADAPTER_INITIALIZED:
69                 rc = sfc_configure(sa);
70                 break;
71         default:
72                 sfc_err(sa, "unexpected adapter state %u to configure",
73                         sa->state);
74                 rc = EINVAL;
75                 break;
76         }
77         sfc_adapter_unlock(sa);
78
79         sfc_log_init(sa, "done %d", rc);
80         SFC_ASSERT(rc >= 0);
81         return -rc;
82 }
83
84 static int
85 sfc_dev_start(struct rte_eth_dev *dev)
86 {
87         struct sfc_adapter *sa = dev->data->dev_private;
88         int rc;
89
90         sfc_log_init(sa, "entry");
91
92         sfc_adapter_lock(sa);
93         rc = sfc_start(sa);
94         sfc_adapter_unlock(sa);
95
96         sfc_log_init(sa, "done %d", rc);
97         SFC_ASSERT(rc >= 0);
98         return -rc;
99 }
100
101 static void
102 sfc_dev_stop(struct rte_eth_dev *dev)
103 {
104         struct sfc_adapter *sa = dev->data->dev_private;
105
106         sfc_log_init(sa, "entry");
107
108         sfc_adapter_lock(sa);
109         sfc_stop(sa);
110         sfc_adapter_unlock(sa);
111
112         sfc_log_init(sa, "done");
113 }
114
115 static void
116 sfc_dev_close(struct rte_eth_dev *dev)
117 {
118         struct sfc_adapter *sa = dev->data->dev_private;
119
120         sfc_log_init(sa, "entry");
121
122         sfc_adapter_lock(sa);
123         switch (sa->state) {
124         case SFC_ADAPTER_STARTED:
125                 sfc_stop(sa);
126                 SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
127                 /* FALLTHROUGH */
128         case SFC_ADAPTER_CONFIGURED:
129                 sfc_close(sa);
130                 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
131                 /* FALLTHROUGH */
132         case SFC_ADAPTER_INITIALIZED:
133                 break;
134         default:
135                 sfc_err(sa, "unexpected adapter state %u on close", sa->state);
136                 break;
137         }
138         sfc_adapter_unlock(sa);
139
140         sfc_log_init(sa, "done");
141 }
142
143 static const struct eth_dev_ops sfc_eth_dev_ops = {
144         .dev_configure                  = sfc_dev_configure,
145         .dev_start                      = sfc_dev_start,
146         .dev_stop                       = sfc_dev_stop,
147         .dev_close                      = sfc_dev_close,
148         .dev_infos_get                  = sfc_dev_infos_get,
149 };
150
151 static int
152 sfc_eth_dev_init(struct rte_eth_dev *dev)
153 {
154         struct sfc_adapter *sa = dev->data->dev_private;
155         struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(dev);
156         int rc;
157         const efx_nic_cfg_t *encp;
158         const struct ether_addr *from;
159
160         /* Required for logging */
161         sa->eth_dev = dev;
162
163         /* Copy PCI device info to the dev->data */
164         rte_eth_copy_pci_info(dev, pci_dev);
165
166         rc = sfc_kvargs_parse(sa);
167         if (rc != 0)
168                 goto fail_kvargs_parse;
169
170         rc = sfc_kvargs_process(sa, SFC_KVARG_DEBUG_INIT,
171                                 sfc_kvarg_bool_handler, &sa->debug_init);
172         if (rc != 0)
173                 goto fail_kvarg_debug_init;
174
175         sfc_log_init(sa, "entry");
176
177         dev->data->mac_addrs = rte_zmalloc("sfc", ETHER_ADDR_LEN, 0);
178         if (dev->data->mac_addrs == NULL) {
179                 rc = ENOMEM;
180                 goto fail_mac_addrs;
181         }
182
183         sfc_adapter_lock_init(sa);
184         sfc_adapter_lock(sa);
185
186         sfc_log_init(sa, "attaching");
187         rc = sfc_attach(sa);
188         if (rc != 0)
189                 goto fail_attach;
190
191         encp = efx_nic_cfg_get(sa->nic);
192
193         /*
194          * The arguments are really reverse order in comparison to
195          * Linux kernel. Copy from NIC config to Ethernet device data.
196          */
197         from = (const struct ether_addr *)(encp->enc_mac_addr);
198         ether_addr_copy(from, &dev->data->mac_addrs[0]);
199
200         dev->dev_ops = &sfc_eth_dev_ops;
201
202         sfc_adapter_unlock(sa);
203
204         sfc_log_init(sa, "done");
205         return 0;
206
207 fail_attach:
208         sfc_adapter_unlock(sa);
209         sfc_adapter_lock_fini(sa);
210         rte_free(dev->data->mac_addrs);
211         dev->data->mac_addrs = NULL;
212
213 fail_mac_addrs:
214 fail_kvarg_debug_init:
215         sfc_kvargs_cleanup(sa);
216
217 fail_kvargs_parse:
218         sfc_log_init(sa, "failed %d", rc);
219         SFC_ASSERT(rc > 0);
220         return -rc;
221 }
222
223 static int
224 sfc_eth_dev_uninit(struct rte_eth_dev *dev)
225 {
226         struct sfc_adapter *sa = dev->data->dev_private;
227
228         sfc_log_init(sa, "entry");
229
230         sfc_adapter_lock(sa);
231
232         sfc_detach(sa);
233
234         rte_free(dev->data->mac_addrs);
235         dev->data->mac_addrs = NULL;
236
237         dev->dev_ops = NULL;
238
239         sfc_kvargs_cleanup(sa);
240
241         sfc_adapter_unlock(sa);
242         sfc_adapter_lock_fini(sa);
243
244         sfc_log_init(sa, "done");
245
246         /* Required for logging, so cleanup last */
247         sa->eth_dev = NULL;
248         return 0;
249 }
250
251 static const struct rte_pci_id pci_id_sfc_efx_map[] = {
252         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_FARMINGDALE) },
253         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_GREENPORT) },
254         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_MEDFORD) },
255         { .vendor_id = 0 /* sentinel */ }
256 };
257
258 static struct eth_driver sfc_efx_pmd = {
259         .pci_drv = {
260                 .id_table = pci_id_sfc_efx_map,
261                 .drv_flags =
262                         RTE_PCI_DRV_NEED_MAPPING,
263                 .probe = rte_eth_dev_pci_probe,
264                 .remove = rte_eth_dev_pci_remove,
265         },
266         .eth_dev_init = sfc_eth_dev_init,
267         .eth_dev_uninit = sfc_eth_dev_uninit,
268         .dev_private_size = sizeof(struct sfc_adapter),
269 };
270
271 RTE_PMD_REGISTER_PCI(net_sfc_efx, sfc_efx_pmd.pci_drv);
272 RTE_PMD_REGISTER_PCI_TABLE(net_sfc_efx, pci_id_sfc_efx_map);
273 RTE_PMD_REGISTER_PARAM_STRING(net_sfc_efx,
274         SFC_KVARG_DEBUG_INIT "=" SFC_KVARG_VALUES_BOOL);