net/sfc: add init on attach
[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 const struct eth_dev_ops sfc_eth_dev_ops = {
53         .dev_infos_get                  = sfc_dev_infos_get,
54 };
55
56 static int
57 sfc_eth_dev_init(struct rte_eth_dev *dev)
58 {
59         struct sfc_adapter *sa = dev->data->dev_private;
60         struct rte_pci_device *pci_dev = SFC_DEV_TO_PCI(dev);
61         int rc;
62         const efx_nic_cfg_t *encp;
63         const struct ether_addr *from;
64
65         /* Required for logging */
66         sa->eth_dev = dev;
67
68         /* Copy PCI device info to the dev->data */
69         rte_eth_copy_pci_info(dev, pci_dev);
70
71         rc = sfc_kvargs_parse(sa);
72         if (rc != 0)
73                 goto fail_kvargs_parse;
74
75         rc = sfc_kvargs_process(sa, SFC_KVARG_DEBUG_INIT,
76                                 sfc_kvarg_bool_handler, &sa->debug_init);
77         if (rc != 0)
78                 goto fail_kvarg_debug_init;
79
80         sfc_log_init(sa, "entry");
81
82         dev->data->mac_addrs = rte_zmalloc("sfc", ETHER_ADDR_LEN, 0);
83         if (dev->data->mac_addrs == NULL) {
84                 rc = ENOMEM;
85                 goto fail_mac_addrs;
86         }
87
88         sfc_adapter_lock_init(sa);
89         sfc_adapter_lock(sa);
90
91         sfc_log_init(sa, "attaching");
92         rc = sfc_attach(sa);
93         if (rc != 0)
94                 goto fail_attach;
95
96         encp = efx_nic_cfg_get(sa->nic);
97
98         /*
99          * The arguments are really reverse order in comparison to
100          * Linux kernel. Copy from NIC config to Ethernet device data.
101          */
102         from = (const struct ether_addr *)(encp->enc_mac_addr);
103         ether_addr_copy(from, &dev->data->mac_addrs[0]);
104
105         dev->dev_ops = &sfc_eth_dev_ops;
106
107         sfc_adapter_unlock(sa);
108
109         sfc_log_init(sa, "done");
110         return 0;
111
112 fail_attach:
113         sfc_adapter_unlock(sa);
114         sfc_adapter_lock_fini(sa);
115         rte_free(dev->data->mac_addrs);
116         dev->data->mac_addrs = NULL;
117
118 fail_mac_addrs:
119 fail_kvarg_debug_init:
120         sfc_kvargs_cleanup(sa);
121
122 fail_kvargs_parse:
123         sfc_log_init(sa, "failed %d", rc);
124         SFC_ASSERT(rc > 0);
125         return -rc;
126 }
127
128 static int
129 sfc_eth_dev_uninit(struct rte_eth_dev *dev)
130 {
131         struct sfc_adapter *sa = dev->data->dev_private;
132
133         sfc_log_init(sa, "entry");
134
135         sfc_adapter_lock(sa);
136
137         sfc_detach(sa);
138
139         rte_free(dev->data->mac_addrs);
140         dev->data->mac_addrs = NULL;
141
142         dev->dev_ops = NULL;
143
144         sfc_kvargs_cleanup(sa);
145
146         sfc_adapter_unlock(sa);
147         sfc_adapter_lock_fini(sa);
148
149         sfc_log_init(sa, "done");
150
151         /* Required for logging, so cleanup last */
152         sa->eth_dev = NULL;
153         return 0;
154 }
155
156 static const struct rte_pci_id pci_id_sfc_efx_map[] = {
157         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_FARMINGDALE) },
158         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_GREENPORT) },
159         { RTE_PCI_DEVICE(EFX_PCI_VENID_SFC, EFX_PCI_DEVID_MEDFORD) },
160         { .vendor_id = 0 /* sentinel */ }
161 };
162
163 static struct eth_driver sfc_efx_pmd = {
164         .pci_drv = {
165                 .id_table = pci_id_sfc_efx_map,
166                 .drv_flags =
167                         RTE_PCI_DRV_NEED_MAPPING,
168                 .probe = rte_eth_dev_pci_probe,
169                 .remove = rte_eth_dev_pci_remove,
170         },
171         .eth_dev_init = sfc_eth_dev_init,
172         .eth_dev_uninit = sfc_eth_dev_uninit,
173         .dev_private_size = sizeof(struct sfc_adapter),
174 };
175
176 RTE_PMD_REGISTER_PCI(net_sfc_efx, sfc_efx_pmd.pci_drv);
177 RTE_PMD_REGISTER_PCI_TABLE(net_sfc_efx, pci_id_sfc_efx_map);
178 RTE_PMD_REGISTER_PARAM_STRING(net_sfc_efx,
179         SFC_KVARG_DEBUG_INIT "=" SFC_KVARG_VALUES_BOOL);