2 * Copyright (c) 2016 Solarflare Communications Inc.
5 * This software was jointly developed between OKTET Labs (under contract
6 * for Solarflare) and Solarflare Communications, Inc.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
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.
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.
31 #include <rte_ethdev.h>
37 #include "sfc_debug.h"
39 #include "sfc_kvargs.h"
44 sfc_dev_infos_get(struct rte_eth_dev *dev, struct rte_eth_dev_info *dev_info)
46 struct sfc_adapter *sa = dev->data->dev_private;
48 sfc_log_init(sa, "entry");
50 dev_info->pci_dev = RTE_DEV_TO_PCI(dev->device);
51 dev_info->max_rx_pktlen = EFX_MAC_PDU_MAX;
55 sfc_dev_configure(struct rte_eth_dev *dev)
57 struct rte_eth_dev_data *dev_data = dev->data;
58 struct sfc_adapter *sa = dev_data->dev_private;
61 sfc_log_init(sa, "entry n_rxq=%u n_txq=%u",
62 dev_data->nb_rx_queues, dev_data->nb_tx_queues);
66 case SFC_ADAPTER_CONFIGURED:
68 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
70 case SFC_ADAPTER_INITIALIZED:
71 rc = sfc_configure(sa);
74 sfc_err(sa, "unexpected adapter state %u to configure",
79 sfc_adapter_unlock(sa);
81 sfc_log_init(sa, "done %d", rc);
87 sfc_dev_start(struct rte_eth_dev *dev)
89 struct sfc_adapter *sa = dev->data->dev_private;
92 sfc_log_init(sa, "entry");
96 sfc_adapter_unlock(sa);
98 sfc_log_init(sa, "done %d", rc);
104 sfc_dev_link_update(struct rte_eth_dev *dev, int wait_to_complete)
106 struct sfc_adapter *sa = dev->data->dev_private;
107 struct rte_eth_link *dev_link = &dev->data->dev_link;
108 struct rte_eth_link old_link;
109 struct rte_eth_link current_link;
111 sfc_log_init(sa, "entry");
113 if (sa->state != SFC_ADAPTER_STARTED)
117 EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
118 *(int64_t *)&old_link = rte_atomic64_read((rte_atomic64_t *)dev_link);
120 if (wait_to_complete) {
121 efx_link_mode_t link_mode;
123 efx_port_poll(sa->nic, &link_mode);
124 sfc_port_link_mode_to_info(link_mode, ¤t_link);
126 if (!rte_atomic64_cmpset((volatile uint64_t *)dev_link,
127 *(uint64_t *)&old_link,
128 *(uint64_t *)¤t_link))
131 sfc_ev_mgmt_qpoll(sa);
132 *(int64_t *)¤t_link =
133 rte_atomic64_read((rte_atomic64_t *)dev_link);
136 if (old_link.link_status != current_link.link_status)
137 sfc_info(sa, "Link status is %s",
138 current_link.link_status ? "UP" : "DOWN");
140 return old_link.link_status == current_link.link_status ? 0 : -1;
144 sfc_dev_stop(struct rte_eth_dev *dev)
146 struct sfc_adapter *sa = dev->data->dev_private;
148 sfc_log_init(sa, "entry");
150 sfc_adapter_lock(sa);
152 sfc_adapter_unlock(sa);
154 sfc_log_init(sa, "done");
158 sfc_dev_close(struct rte_eth_dev *dev)
160 struct sfc_adapter *sa = dev->data->dev_private;
162 sfc_log_init(sa, "entry");
164 sfc_adapter_lock(sa);
166 case SFC_ADAPTER_STARTED:
168 SFC_ASSERT(sa->state == SFC_ADAPTER_CONFIGURED);
170 case SFC_ADAPTER_CONFIGURED:
172 SFC_ASSERT(sa->state == SFC_ADAPTER_INITIALIZED);
174 case SFC_ADAPTER_INITIALIZED:
177 sfc_err(sa, "unexpected adapter state %u on close", sa->state);
180 sfc_adapter_unlock(sa);
182 sfc_log_init(sa, "done");
185 static const struct eth_dev_ops sfc_eth_dev_ops = {
186 .dev_configure = sfc_dev_configure,
187 .dev_start = sfc_dev_start,
188 .dev_stop = sfc_dev_stop,
189 .dev_close = sfc_dev_close,
190 .link_update = sfc_dev_link_update,
191 .dev_infos_get = sfc_dev_infos_get,
195 sfc_eth_dev_init(struct rte_eth_dev *dev)
197 struct sfc_adapter *sa = dev->data->dev_private;
198 struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(dev);
200 const efx_nic_cfg_t *encp;
201 const struct ether_addr *from;
203 /* Required for logging */
206 /* Copy PCI device info to the dev->data */
207 rte_eth_copy_pci_info(dev, pci_dev);
209 rc = sfc_kvargs_parse(sa);
211 goto fail_kvargs_parse;
213 rc = sfc_kvargs_process(sa, SFC_KVARG_DEBUG_INIT,
214 sfc_kvarg_bool_handler, &sa->debug_init);
216 goto fail_kvarg_debug_init;
218 sfc_log_init(sa, "entry");
220 dev->data->mac_addrs = rte_zmalloc("sfc", ETHER_ADDR_LEN, 0);
221 if (dev->data->mac_addrs == NULL) {
226 sfc_adapter_lock_init(sa);
227 sfc_adapter_lock(sa);
229 sfc_log_init(sa, "attaching");
234 encp = efx_nic_cfg_get(sa->nic);
237 * The arguments are really reverse order in comparison to
238 * Linux kernel. Copy from NIC config to Ethernet device data.
240 from = (const struct ether_addr *)(encp->enc_mac_addr);
241 ether_addr_copy(from, &dev->data->mac_addrs[0]);
243 dev->dev_ops = &sfc_eth_dev_ops;
245 sfc_adapter_unlock(sa);
247 sfc_log_init(sa, "done");
251 sfc_adapter_unlock(sa);
252 sfc_adapter_lock_fini(sa);
253 rte_free(dev->data->mac_addrs);
254 dev->data->mac_addrs = NULL;
257 fail_kvarg_debug_init:
258 sfc_kvargs_cleanup(sa);
261 sfc_log_init(sa, "failed %d", rc);
267 sfc_eth_dev_uninit(struct rte_eth_dev *dev)
269 struct sfc_adapter *sa = dev->data->dev_private;
271 sfc_log_init(sa, "entry");
273 sfc_adapter_lock(sa);
277 rte_free(dev->data->mac_addrs);
278 dev->data->mac_addrs = NULL;
282 sfc_kvargs_cleanup(sa);
284 sfc_adapter_unlock(sa);
285 sfc_adapter_lock_fini(sa);
287 sfc_log_init(sa, "done");
289 /* Required for logging, so cleanup last */
294 static const struct rte_pci_id pci_id_sfc_efx_map[] = {
295 { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_FARMINGDALE) },
296 { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_GREENPORT) },
297 { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_MEDFORD) },
298 { .vendor_id = 0 /* sentinel */ }
301 static struct eth_driver sfc_efx_pmd = {
303 .id_table = pci_id_sfc_efx_map,
305 RTE_PCI_DRV_NEED_MAPPING,
306 .probe = rte_eth_dev_pci_probe,
307 .remove = rte_eth_dev_pci_remove,
309 .eth_dev_init = sfc_eth_dev_init,
310 .eth_dev_uninit = sfc_eth_dev_uninit,
311 .dev_private_size = sizeof(struct sfc_adapter),
314 RTE_PMD_REGISTER_PCI(net_sfc_efx, sfc_efx_pmd.pci_drv);
315 RTE_PMD_REGISTER_PCI_TABLE(net_sfc_efx, pci_id_sfc_efx_map);
316 RTE_PMD_REGISTER_PARAM_STRING(net_sfc_efx,
317 SFC_KVARG_DEBUG_INIT "=" SFC_KVARG_VALUES_BOOL);