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.
36 * Update MAC statistics in the buffer.
42 * @retval EAGAIN Try again
43 * @retval ENOMEM Memory allocation failure
46 sfc_port_update_mac_stats(struct sfc_adapter *sa)
48 struct sfc_port *port = &sa->port;
51 SFC_ASSERT(rte_spinlock_is_locked(&port->mac_stats_lock));
53 if (sa->state != SFC_ADAPTER_STARTED)
56 rc = efx_mac_stats_update(sa->nic, &port->mac_stats_dma_mem,
57 port->mac_stats_buf, NULL);
65 sfc_port_reset_mac_stats(struct sfc_adapter *sa)
67 struct sfc_port *port = &sa->port;
70 rte_spinlock_lock(&port->mac_stats_lock);
71 rc = efx_mac_stats_clear(sa->nic);
72 rte_spinlock_unlock(&port->mac_stats_lock);
78 sfc_port_init_dev_link(struct sfc_adapter *sa)
80 struct rte_eth_link *dev_link = &sa->eth_dev->data->dev_link;
82 efx_link_mode_t link_mode;
83 struct rte_eth_link current_link;
85 rc = efx_port_poll(sa->nic, &link_mode);
89 sfc_port_link_mode_to_info(link_mode, ¤t_link);
91 EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
92 rte_atomic64_set((rte_atomic64_t *)dev_link,
93 *(uint64_t *)¤t_link);
99 sfc_port_start(struct sfc_adapter *sa)
101 struct sfc_port *port = &sa->port;
103 uint32_t phy_adv_cap;
104 const uint32_t phy_pause_caps =
105 ((1u << EFX_PHY_CAP_PAUSE) | (1u << EFX_PHY_CAP_ASYM));
107 sfc_log_init(sa, "entry");
109 sfc_log_init(sa, "init filters");
110 rc = efx_filter_init(sa->nic);
112 goto fail_filter_init;
114 sfc_log_init(sa, "init port");
115 rc = efx_port_init(sa->nic);
119 sfc_log_init(sa, "set flow control to %#x autoneg=%u",
120 port->flow_ctrl, port->flow_ctrl_autoneg);
121 rc = efx_mac_fcntl_set(sa->nic, port->flow_ctrl,
122 port->flow_ctrl_autoneg);
124 goto fail_mac_fcntl_set;
126 /* Preserve pause capabilities set by above efx_mac_fcntl_set() */
127 efx_phy_adv_cap_get(sa->nic, EFX_PHY_CAP_CURRENT, &phy_adv_cap);
128 SFC_ASSERT((port->phy_adv_cap & phy_pause_caps) == 0);
129 phy_adv_cap = port->phy_adv_cap | (phy_adv_cap & phy_pause_caps);
131 sfc_log_init(sa, "set phy adv caps to %#x", phy_adv_cap);
132 rc = efx_phy_adv_cap_set(sa->nic, phy_adv_cap);
134 goto fail_phy_adv_cap_set;
136 sfc_log_init(sa, "set MAC PDU %u", (unsigned int)port->pdu);
137 rc = efx_mac_pdu_set(sa->nic, port->pdu);
139 goto fail_mac_pdu_set;
141 sfc_log_init(sa, "set MAC address");
142 rc = efx_mac_addr_set(sa->nic,
143 sa->eth_dev->data->mac_addrs[0].addr_bytes);
145 goto fail_mac_addr_set;
147 sfc_log_init(sa, "set MAC filters");
148 port->promisc = (sa->eth_dev->data->promiscuous != 0) ?
150 port->allmulti = (sa->eth_dev->data->all_multicast != 0) ?
152 rc = sfc_set_rx_mode(sa);
154 goto fail_mac_filter_set;
156 if (port->mac_stats_reset_pending) {
157 rc = sfc_port_reset_mac_stats(sa);
159 sfc_err(sa, "statistics reset failed (requested "
160 "before the port was started)");
162 port->mac_stats_reset_pending = B_FALSE;
165 efx_mac_stats_get_mask(sa->nic, port->mac_stats_mask,
166 sizeof(port->mac_stats_mask));
168 /* Update MAC stats using periodic DMA.
169 * Common code always uses 1000ms update period, so period_ms
170 * parameter only needs to be non-zero to start updates.
172 sfc_log_init(sa, "request MAC stats DMA'ing");
173 rc = efx_mac_stats_periodic(sa->nic, &port->mac_stats_dma_mem,
176 goto fail_mac_stats_periodic;
178 sfc_log_init(sa, "disable MAC drain");
179 rc = efx_mac_drain(sa->nic, B_FALSE);
183 /* Synchronize link status knowledge */
184 rc = sfc_port_init_dev_link(sa);
186 goto fail_port_init_dev_link;
188 sfc_log_init(sa, "done");
191 fail_port_init_dev_link:
192 (void)efx_mac_drain(sa->nic, B_TRUE);
195 (void)efx_mac_stats_periodic(sa->nic, &port->mac_stats_dma_mem,
198 fail_mac_stats_periodic:
202 fail_phy_adv_cap_set:
204 efx_port_fini(sa->nic);
207 efx_filter_fini(sa->nic);
210 sfc_log_init(sa, "failed %d", rc);
215 sfc_port_stop(struct sfc_adapter *sa)
217 sfc_log_init(sa, "entry");
219 efx_mac_drain(sa->nic, B_TRUE);
221 (void)efx_mac_stats_periodic(sa->nic, &sa->port.mac_stats_dma_mem,
224 efx_port_fini(sa->nic);
225 efx_filter_fini(sa->nic);
227 sfc_log_init(sa, "done");
231 sfc_port_init(struct sfc_adapter *sa)
233 const struct rte_eth_dev_data *dev_data = sa->eth_dev->data;
234 struct sfc_port *port = &sa->port;
237 sfc_log_init(sa, "entry");
239 /* Enable flow control by default */
240 port->flow_ctrl = EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE;
241 port->flow_ctrl_autoneg = B_TRUE;
243 if (dev_data->dev_conf.rxmode.jumbo_frame)
244 port->pdu = dev_data->dev_conf.rxmode.max_rx_pkt_len;
246 port->pdu = EFX_MAC_PDU(dev_data->mtu);
248 rte_spinlock_init(&port->mac_stats_lock);
251 port->mac_stats_buf = rte_calloc_socket("mac_stats_buf", EFX_MAC_NSTATS,
254 if (port->mac_stats_buf == NULL)
255 goto fail_mac_stats_buf_alloc;
257 rc = sfc_dma_alloc(sa, "mac_stats", 0, EFX_MAC_STATS_SIZE,
258 sa->socket_id, &port->mac_stats_dma_mem);
260 goto fail_mac_stats_dma_alloc;
262 port->mac_stats_reset_pending = B_FALSE;
264 sfc_log_init(sa, "done");
267 fail_mac_stats_dma_alloc:
268 rte_free(port->mac_stats_buf);
269 fail_mac_stats_buf_alloc:
270 sfc_log_init(sa, "failed %d", rc);
275 sfc_port_fini(struct sfc_adapter *sa)
277 struct sfc_port *port = &sa->port;
279 sfc_log_init(sa, "entry");
281 sfc_dma_free(sa, &port->mac_stats_dma_mem);
282 rte_free(port->mac_stats_buf);
284 sfc_log_init(sa, "done");
288 sfc_set_rx_mode(struct sfc_adapter *sa)
290 struct sfc_port *port = &sa->port;
293 rc = efx_mac_filter_set(sa->nic, port->promisc, B_TRUE,
294 port->promisc || port->allmulti, B_TRUE);
300 sfc_port_link_mode_to_info(efx_link_mode_t link_mode,
301 struct rte_eth_link *link_info)
303 SFC_ASSERT(link_mode < EFX_LINK_NMODES);
305 memset(link_info, 0, sizeof(*link_info));
306 if ((link_mode == EFX_LINK_DOWN) || (link_mode == EFX_LINK_UNKNOWN))
307 link_info->link_status = ETH_LINK_DOWN;
309 link_info->link_status = ETH_LINK_UP;
313 link_info->link_speed = ETH_SPEED_NUM_10M;
314 link_info->link_duplex = ETH_LINK_HALF_DUPLEX;
317 link_info->link_speed = ETH_SPEED_NUM_10M;
318 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
320 case EFX_LINK_100HDX:
321 link_info->link_speed = ETH_SPEED_NUM_100M;
322 link_info->link_duplex = ETH_LINK_HALF_DUPLEX;
324 case EFX_LINK_100FDX:
325 link_info->link_speed = ETH_SPEED_NUM_100M;
326 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
328 case EFX_LINK_1000HDX:
329 link_info->link_speed = ETH_SPEED_NUM_1G;
330 link_info->link_duplex = ETH_LINK_HALF_DUPLEX;
332 case EFX_LINK_1000FDX:
333 link_info->link_speed = ETH_SPEED_NUM_1G;
334 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
336 case EFX_LINK_10000FDX:
337 link_info->link_speed = ETH_SPEED_NUM_10G;
338 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
340 case EFX_LINK_40000FDX:
341 link_info->link_speed = ETH_SPEED_NUM_40G;
342 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
347 case EFX_LINK_UNKNOWN:
349 link_info->link_speed = ETH_SPEED_NUM_NONE;
350 link_info->link_duplex = 0;
354 link_info->link_autoneg = ETH_LINK_AUTONEG;