4 * Copyright(c) 2013-2015 Intel Corporation. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
17 * * Neither the name of Intel Corporation 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.
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.
34 #include <rte_ethdev.h>
35 #include <rte_malloc.h>
36 #include <rte_memzone.h>
37 #include <rte_string_fns.h>
39 #include <rte_spinlock.h>
42 #include "base/fm10k_api.h"
44 /* Default delay to acquire mailbox lock */
45 #define FM10K_MBXLOCK_DELAY_US 20
48 fm10k_mbx_initlock(struct fm10k_hw *hw)
50 rte_spinlock_init(FM10K_DEV_PRIVATE_TO_MBXLOCK(hw->back));
54 fm10k_mbx_lock(struct fm10k_hw *hw)
56 while (!rte_spinlock_trylock(FM10K_DEV_PRIVATE_TO_MBXLOCK(hw->back)))
57 rte_delay_us(FM10K_MBXLOCK_DELAY_US);
61 fm10k_mbx_unlock(struct fm10k_hw *hw)
63 rte_spinlock_unlock(FM10K_DEV_PRIVATE_TO_MBXLOCK(hw->back));
67 fm10k_dev_configure(struct rte_eth_dev *dev)
69 PMD_INIT_FUNC_TRACE();
71 if (dev->data->dev_conf.rxmode.hw_strip_crc == 0)
72 PMD_INIT_LOG(WARNING, "fm10k always strip CRC");
78 fm10k_stats_get(struct rte_eth_dev *dev, struct rte_eth_stats *stats)
80 uint64_t ipackets, opackets, ibytes, obytes;
82 FM10K_DEV_PRIVATE_TO_HW(dev->data->dev_private);
83 struct fm10k_hw_stats *hw_stats =
84 FM10K_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
87 PMD_INIT_FUNC_TRACE();
89 fm10k_update_hw_stats(hw, hw_stats);
91 ipackets = opackets = ibytes = obytes = 0;
92 for (i = 0; (i < RTE_ETHDEV_QUEUE_STAT_CNTRS) &&
93 (i < FM10K_MAX_QUEUES_PF); ++i) {
94 stats->q_ipackets[i] = hw_stats->q[i].rx_packets.count;
95 stats->q_opackets[i] = hw_stats->q[i].tx_packets.count;
96 stats->q_ibytes[i] = hw_stats->q[i].rx_bytes.count;
97 stats->q_obytes[i] = hw_stats->q[i].tx_bytes.count;
98 ipackets += stats->q_ipackets[i];
99 opackets += stats->q_opackets[i];
100 ibytes += stats->q_ibytes[i];
101 obytes += stats->q_obytes[i];
103 stats->ipackets = ipackets;
104 stats->opackets = opackets;
105 stats->ibytes = ibytes;
106 stats->obytes = obytes;
110 fm10k_stats_reset(struct rte_eth_dev *dev)
112 struct fm10k_hw *hw = FM10K_DEV_PRIVATE_TO_HW(dev->data->dev_private);
113 struct fm10k_hw_stats *hw_stats =
114 FM10K_DEV_PRIVATE_TO_STATS(dev->data->dev_private);
116 PMD_INIT_FUNC_TRACE();
118 memset(hw_stats, 0, sizeof(*hw_stats));
119 fm10k_rebind_hw_stats(hw, hw_stats);
122 /* Mailbox message handler in VF */
123 static const struct fm10k_msg_data fm10k_msgdata_vf[] = {
124 FM10K_TLV_MSG_TEST_HANDLER(fm10k_tlv_msg_test),
125 FM10K_VF_MSG_MAC_VLAN_HANDLER(fm10k_msg_mac_vlan_vf),
126 FM10K_VF_MSG_LPORT_STATE_HANDLER(fm10k_msg_lport_state_vf),
127 FM10K_TLV_MSG_ERROR_HANDLER(fm10k_tlv_msg_error),
130 /* Mailbox message handler in PF */
131 static const struct fm10k_msg_data fm10k_msgdata_pf[] = {
132 FM10K_PF_MSG_ERR_HANDLER(XCAST_MODES, fm10k_msg_err_pf),
133 FM10K_PF_MSG_ERR_HANDLER(UPDATE_MAC_FWD_RULE, fm10k_msg_err_pf),
134 FM10K_PF_MSG_LPORT_MAP_HANDLER(fm10k_msg_lport_map_pf),
135 FM10K_PF_MSG_ERR_HANDLER(LPORT_CREATE, fm10k_msg_err_pf),
136 FM10K_PF_MSG_ERR_HANDLER(LPORT_DELETE, fm10k_msg_err_pf),
137 FM10K_PF_MSG_UPDATE_PVID_HANDLER(fm10k_msg_update_pvid_pf),
138 FM10K_TLV_MSG_ERROR_HANDLER(fm10k_tlv_msg_error),
142 fm10k_setup_mbx_service(struct fm10k_hw *hw)
146 /* Initialize mailbox lock */
147 fm10k_mbx_initlock(hw);
149 /* Replace default message handler with new ones */
150 if (hw->mac.type == fm10k_mac_pf)
151 err = hw->mbx.ops.register_handlers(&hw->mbx, fm10k_msgdata_pf);
153 err = hw->mbx.ops.register_handlers(&hw->mbx, fm10k_msgdata_vf);
156 PMD_INIT_LOG(ERR, "Failed to register mailbox handler.err:%d",
160 /* Connect to SM for PF device or PF for VF device */
161 return hw->mbx.ops.connect(hw, &hw->mbx);
164 static struct eth_dev_ops fm10k_eth_dev_ops = {
165 .dev_configure = fm10k_dev_configure,
166 .stats_get = fm10k_stats_get,
167 .stats_reset = fm10k_stats_reset,
171 eth_fm10k_dev_init(__rte_unused struct eth_driver *eth_drv,
172 struct rte_eth_dev *dev)
174 struct fm10k_hw *hw = FM10K_DEV_PRIVATE_TO_HW(dev->data->dev_private);
177 PMD_INIT_FUNC_TRACE();
179 dev->dev_ops = &fm10k_eth_dev_ops;
181 /* only initialize in the primary process */
182 if (rte_eal_process_type() != RTE_PROC_PRIMARY)
185 /* Vendor and Device ID need to be set before init of shared code */
186 memset(hw, 0, sizeof(*hw));
187 hw->device_id = dev->pci_dev->id.device_id;
188 hw->vendor_id = dev->pci_dev->id.vendor_id;
189 hw->subsystem_device_id = dev->pci_dev->id.subsystem_device_id;
190 hw->subsystem_vendor_id = dev->pci_dev->id.subsystem_vendor_id;
192 hw->hw_addr = (void *)dev->pci_dev->mem_resource[0].addr;
193 if (hw->hw_addr == NULL) {
194 PMD_INIT_LOG(ERR, "Bad mem resource."
195 " Try to blacklist unused devices.");
199 /* Store fm10k_adapter pointer */
200 hw->back = dev->data->dev_private;
202 /* Initialize the shared code */
203 diag = fm10k_init_shared_code(hw);
204 if (diag != FM10K_SUCCESS) {
205 PMD_INIT_LOG(ERR, "Shared code init failed: %d", diag);
210 * Inialize bus info. Normally we would call fm10k_get_bus_info(), but
211 * there is no way to get link status without reading BAR4. Until this
212 * works, assume we have maximum bandwidth.
213 * @todo - fix bus info
215 hw->bus_caps.speed = fm10k_bus_speed_8000;
216 hw->bus_caps.width = fm10k_bus_width_pcie_x8;
217 hw->bus_caps.payload = fm10k_bus_payload_512;
218 hw->bus.speed = fm10k_bus_speed_8000;
219 hw->bus.width = fm10k_bus_width_pcie_x8;
220 hw->bus.payload = fm10k_bus_payload_256;
222 /* Initialize the hw */
223 diag = fm10k_init_hw(hw);
224 if (diag != FM10K_SUCCESS) {
225 PMD_INIT_LOG(ERR, "Hardware init failed: %d", diag);
229 /* Initialize MAC address(es) */
230 dev->data->mac_addrs = rte_zmalloc("fm10k", ETHER_ADDR_LEN, 0);
231 if (dev->data->mac_addrs == NULL) {
232 PMD_INIT_LOG(ERR, "Cannot allocate memory for MAC addresses");
236 diag = fm10k_read_mac_addr(hw);
237 if (diag != FM10K_SUCCESS) {
239 * TODO: remove special handling on VF. Need shared code to
242 if (hw->mac.type == fm10k_mac_pf) {
243 PMD_INIT_LOG(ERR, "Read MAC addr failed: %d", diag);
246 /* Generate a random addr */
247 eth_random_addr(hw->mac.addr);
248 memcpy(hw->mac.perm_addr, hw->mac.addr, ETH_ALEN);
252 ether_addr_copy((const struct ether_addr *)hw->mac.addr,
253 &dev->data->mac_addrs[0]);
255 /* Reset the hw statistics */
256 fm10k_stats_reset(dev);
259 diag = fm10k_reset_hw(hw);
260 if (diag != FM10K_SUCCESS) {
261 PMD_INIT_LOG(ERR, "Hardware reset failed: %d", diag);
265 /* Setup mailbox service */
266 diag = fm10k_setup_mbx_service(hw);
267 if (diag != FM10K_SUCCESS) {
268 PMD_INIT_LOG(ERR, "Failed to setup mailbox: %d", diag);
273 * Below function will trigger operations on mailbox, acquire lock to
274 * avoid race condition from interrupt handler. Operations on mailbox
275 * FIFO will trigger interrupt to PF/SM, in which interrupt handler
276 * will handle and generate an interrupt to our side. Then, FIFO in
277 * mailbox will be touched.
280 /* Enable port first */
281 hw->mac.ops.update_lport_state(hw, 0, 0, 1);
283 /* Update default vlan */
284 hw->mac.ops.update_vlan(hw, hw->mac.default_vid, 0, true);
287 * Add default mac/vlan filter. glort is assigned by SM for PF, while is
288 * unused for VF. PF will assign correct glort for VF.
290 hw->mac.ops.update_uc_addr(hw, hw->mac.dglort_map, hw->mac.addr,
291 hw->mac.default_vid, 1, 0);
293 /* Set unicast mode by default. App can change to other mode in other
296 hw->mac.ops.update_xcast_mode(hw, hw->mac.dglort_map,
297 FM10K_XCAST_MODE_MULTI);
299 fm10k_mbx_unlock(hw);
305 * The set of PCI devices this driver supports. This driver will enable both PF
306 * and SRIOV-VF devices.
308 static struct rte_pci_id pci_id_fm10k_map[] = {
309 #define RTE_PCI_DEV_ID_DECL_FM10K(vend, dev) { RTE_PCI_DEVICE(vend, dev) },
310 #include "rte_pci_dev_ids.h"
311 { .vendor_id = 0, /* sentinel */ },
314 static struct eth_driver rte_pmd_fm10k = {
316 .name = "rte_pmd_fm10k",
317 .id_table = pci_id_fm10k_map,
318 .drv_flags = RTE_PCI_DRV_NEED_MAPPING,
320 .eth_dev_init = eth_fm10k_dev_init,
321 .dev_private_size = sizeof(struct fm10k_adapter),
325 * Driver initialization routine.
326 * Invoked once at EAL init time.
327 * Register itself as the [Poll Mode] Driver of PCI FM10K devices.
330 rte_pmd_fm10k_init(__rte_unused const char *name,
331 __rte_unused const char *params)
333 PMD_INIT_FUNC_TRACE();
334 rte_eth_driver_register(&rte_pmd_fm10k);
338 static struct rte_driver rte_fm10k_driver = {
340 .init = rte_pmd_fm10k_init,
343 PMD_REGISTER_DRIVER(rte_fm10k_driver);