1 /* SPDX-License-Identifier: BSD-3-Clause
3 * Copyright(c) 2019-2020 Xilinx, Inc.
4 * Copyright(c) 2016-2019 Solarflare Communications Inc.
6 * This software was jointly developed between OKTET Labs (under contract
7 * for Solarflare) and Solarflare Communications, Inc.
14 #include "sfc_kvargs.h"
16 /** Default MAC statistics update period is 1 second */
17 #define SFC_MAC_STATS_UPDATE_PERIOD_MS_DEF MS_PER_S
19 /** The number of microseconds to sleep on attempt to get statistics update */
20 #define SFC_MAC_STATS_UPDATE_RETRY_INTERVAL_US 10
22 /** The number of attempts to await arrival of freshly generated statistics */
23 #define SFC_MAC_STATS_UPDATE_NB_ATTEMPTS 50
26 * Update MAC statistics in the buffer.
32 * @retval EAGAIN Try again
33 * @retval ENOMEM Memory allocation failure
36 sfc_port_update_mac_stats(struct sfc_adapter *sa)
38 struct sfc_port *port = &sa->port;
39 efsys_mem_t *esmp = &port->mac_stats_dma_mem;
40 uint32_t *genp = NULL;
42 unsigned int nb_attempts = 0;
45 SFC_ASSERT(rte_spinlock_is_locked(&port->mac_stats_lock));
47 if (sa->state != SFC_ADAPTER_STARTED)
51 * If periodic statistics DMA'ing is off or if not supported,
52 * make a manual request and keep an eye on timer if need be
54 if (!port->mac_stats_periodic_dma_supported ||
55 (port->mac_stats_update_period_ms == 0)) {
56 if (port->mac_stats_update_period_ms != 0) {
57 uint64_t timestamp = sfc_get_system_msecs();
60 port->mac_stats_last_request_timestamp) <
61 port->mac_stats_update_period_ms)
64 port->mac_stats_last_request_timestamp = timestamp;
67 rc = efx_mac_stats_upload(sa->nic, esmp);
71 genp = &port->mac_stats_update_generation;
77 rte_delay_us(SFC_MAC_STATS_UPDATE_RETRY_INTERVAL_US);
79 rc = efx_mac_stats_update(sa->nic, esmp,
80 port->mac_stats_buf, genp);
84 } while ((genp != NULL) && (*genp == gen_old) &&
85 (++nb_attempts < SFC_MAC_STATS_UPDATE_NB_ATTEMPTS));
91 sfc_port_reset_sw_stats(struct sfc_adapter *sa)
93 struct sfc_port *port = &sa->port;
96 * Reset diff stats explicitly since check which does not allow
97 * the statistics to grow backward could deny it.
103 sfc_port_reset_mac_stats(struct sfc_adapter *sa)
105 struct sfc_port *port = &sa->port;
108 rte_spinlock_lock(&port->mac_stats_lock);
109 rc = efx_mac_stats_clear(sa->nic);
111 sfc_port_reset_sw_stats(sa);
112 rte_spinlock_unlock(&port->mac_stats_lock);
118 sfc_port_init_dev_link(struct sfc_adapter *sa)
120 struct rte_eth_link *dev_link = &sa->eth_dev->data->dev_link;
122 efx_link_mode_t link_mode;
123 struct rte_eth_link current_link;
125 rc = efx_port_poll(sa->nic, &link_mode);
129 sfc_port_link_mode_to_info(link_mode, ¤t_link);
131 EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
132 rte_atomic64_set((rte_atomic64_t *)dev_link,
133 *(uint64_t *)¤t_link);
138 #if EFSYS_OPT_LOOPBACK
140 static efx_link_mode_t
141 sfc_port_phy_caps_to_max_link_speed(uint32_t phy_caps)
143 if (phy_caps & (1u << EFX_PHY_CAP_100000FDX))
144 return EFX_LINK_100000FDX;
145 if (phy_caps & (1u << EFX_PHY_CAP_50000FDX))
146 return EFX_LINK_50000FDX;
147 if (phy_caps & (1u << EFX_PHY_CAP_40000FDX))
148 return EFX_LINK_40000FDX;
149 if (phy_caps & (1u << EFX_PHY_CAP_25000FDX))
150 return EFX_LINK_25000FDX;
151 if (phy_caps & (1u << EFX_PHY_CAP_10000FDX))
152 return EFX_LINK_10000FDX;
153 if (phy_caps & (1u << EFX_PHY_CAP_1000FDX))
154 return EFX_LINK_1000FDX;
155 return EFX_LINK_UNKNOWN;
161 sfc_port_start(struct sfc_adapter *sa)
163 struct sfc_port *port = &sa->port;
165 uint32_t phy_adv_cap;
166 const uint32_t phy_pause_caps =
167 ((1u << EFX_PHY_CAP_PAUSE) | (1u << EFX_PHY_CAP_ASYM));
170 sfc_log_init(sa, "entry");
172 sfc_log_init(sa, "init filters");
173 rc = efx_filter_init(sa->nic);
175 goto fail_filter_init;
177 sfc_log_init(sa, "init port");
178 rc = efx_port_init(sa->nic);
182 #if EFSYS_OPT_LOOPBACK
183 if (sa->eth_dev->data->dev_conf.lpbk_mode != 0) {
184 efx_link_mode_t link_mode;
187 sfc_port_phy_caps_to_max_link_speed(port->phy_adv_cap);
188 sfc_log_init(sa, "set loopback link_mode=%u type=%u", link_mode,
189 sa->eth_dev->data->dev_conf.lpbk_mode);
190 rc = efx_port_loopback_set(sa->nic, link_mode,
191 sa->eth_dev->data->dev_conf.lpbk_mode);
193 goto fail_loopback_set;
197 sfc_log_init(sa, "set flow control to %#x autoneg=%u",
198 port->flow_ctrl, port->flow_ctrl_autoneg);
199 rc = efx_mac_fcntl_set(sa->nic, port->flow_ctrl,
200 port->flow_ctrl_autoneg);
202 goto fail_mac_fcntl_set;
204 /* Preserve pause capabilities set by above efx_mac_fcntl_set() */
205 efx_phy_adv_cap_get(sa->nic, EFX_PHY_CAP_CURRENT, &phy_adv_cap);
206 SFC_ASSERT((port->phy_adv_cap & phy_pause_caps) == 0);
207 phy_adv_cap = port->phy_adv_cap | (phy_adv_cap & phy_pause_caps);
210 * No controls for FEC yet. Use default FEC mode.
211 * I.e. advertise everything supported (*_FEC=1), but do not request
212 * anything explicitly (*_FEC_REQUESTED=0).
214 phy_adv_cap |= port->phy_adv_cap_mask &
215 (1u << EFX_PHY_CAP_BASER_FEC |
216 1u << EFX_PHY_CAP_RS_FEC |
217 1u << EFX_PHY_CAP_25G_BASER_FEC);
219 sfc_log_init(sa, "set phy adv caps to %#x", phy_adv_cap);
220 rc = efx_phy_adv_cap_set(sa->nic, phy_adv_cap);
222 goto fail_phy_adv_cap_set;
224 sfc_log_init(sa, "set MAC PDU %u", (unsigned int)port->pdu);
225 rc = efx_mac_pdu_set(sa->nic, port->pdu);
227 goto fail_mac_pdu_set;
229 if (!sfc_sa2shared(sa)->isolated) {
230 struct rte_ether_addr *addr = &port->default_mac_addr;
232 sfc_log_init(sa, "set MAC address");
233 rc = efx_mac_addr_set(sa->nic, addr->addr_bytes);
235 goto fail_mac_addr_set;
237 sfc_log_init(sa, "set MAC filters");
238 port->promisc = (sa->eth_dev->data->promiscuous != 0) ?
240 port->allmulti = (sa->eth_dev->data->all_multicast != 0) ?
242 rc = sfc_set_rx_mode_unchecked(sa);
244 goto fail_mac_filter_set;
246 sfc_log_init(sa, "set multicast address list");
247 rc = efx_mac_multicast_list_set(sa->nic, port->mcast_addrs,
248 port->nb_mcast_addrs);
250 goto fail_mcast_address_list_set;
253 if (port->mac_stats_reset_pending) {
254 rc = sfc_port_reset_mac_stats(sa);
256 sfc_err(sa, "statistics reset failed (requested "
257 "before the port was started)");
259 port->mac_stats_reset_pending = B_FALSE;
262 efx_mac_stats_get_mask(sa->nic, port->mac_stats_mask,
263 sizeof(port->mac_stats_mask));
265 for (i = 0, port->mac_stats_nb_supported = 0; i < EFX_MAC_NSTATS; ++i)
266 if (EFX_MAC_STAT_SUPPORTED(port->mac_stats_mask, i))
267 port->mac_stats_nb_supported++;
269 port->mac_stats_update_generation = 0;
271 if (port->mac_stats_update_period_ms != 0) {
273 * Update MAC stats using periodic DMA;
274 * any positive update interval different from
275 * 1000 ms can be set only on SFN8xxx provided
276 * that FW version is 6.2.1.1033 or higher
278 sfc_log_init(sa, "request MAC stats DMA'ing");
279 rc = efx_mac_stats_periodic(sa->nic, &port->mac_stats_dma_mem,
280 port->mac_stats_update_period_ms,
283 port->mac_stats_periodic_dma_supported = B_TRUE;
284 } else if (rc == EOPNOTSUPP) {
285 port->mac_stats_periodic_dma_supported = B_FALSE;
286 port->mac_stats_last_request_timestamp = 0;
288 goto fail_mac_stats_periodic;
292 if ((port->mac_stats_update_period_ms != 0) &&
293 port->mac_stats_periodic_dma_supported) {
295 * Request an explicit MAC stats upload immediately to
296 * preclude bogus figures readback if the user decides
297 * to read stats before periodic DMA is really started
299 rc = efx_mac_stats_upload(sa->nic, &port->mac_stats_dma_mem);
301 goto fail_mac_stats_upload;
304 sfc_log_init(sa, "disable MAC drain");
305 rc = efx_mac_drain(sa->nic, B_FALSE);
309 /* Synchronize link status knowledge */
310 rc = sfc_port_init_dev_link(sa);
312 goto fail_port_init_dev_link;
314 sfc_log_init(sa, "done");
317 fail_port_init_dev_link:
318 (void)efx_mac_drain(sa->nic, B_TRUE);
321 fail_mac_stats_upload:
322 (void)efx_mac_stats_periodic(sa->nic, &port->mac_stats_dma_mem,
325 fail_mac_stats_periodic:
326 fail_mcast_address_list_set:
330 fail_phy_adv_cap_set:
332 #if EFSYS_OPT_LOOPBACK
335 efx_port_fini(sa->nic);
338 efx_filter_fini(sa->nic);
341 sfc_log_init(sa, "failed %d", rc);
346 sfc_port_stop(struct sfc_adapter *sa)
348 sfc_log_init(sa, "entry");
350 efx_mac_drain(sa->nic, B_TRUE);
352 (void)efx_mac_stats_periodic(sa->nic, &sa->port.mac_stats_dma_mem,
355 efx_port_fini(sa->nic);
356 efx_filter_fini(sa->nic);
358 sfc_log_init(sa, "done");
362 sfc_port_configure(struct sfc_adapter *sa)
364 const struct rte_eth_dev_data *dev_data = sa->eth_dev->data;
365 struct sfc_port *port = &sa->port;
366 const struct rte_eth_rxmode *rxmode = &dev_data->dev_conf.rxmode;
368 sfc_log_init(sa, "entry");
370 if (rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME)
371 port->pdu = rxmode->max_rx_pkt_len;
373 port->pdu = EFX_MAC_PDU(dev_data->mtu);
379 sfc_port_close(struct sfc_adapter *sa)
381 sfc_log_init(sa, "entry");
385 sfc_port_attach(struct sfc_adapter *sa)
387 struct sfc_port *port = &sa->port;
388 const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
389 const struct rte_ether_addr *from;
391 size_t mac_stats_size;
392 long kvarg_stats_update_period_ms;
395 sfc_log_init(sa, "entry");
397 efx_phy_adv_cap_get(sa->nic, EFX_PHY_CAP_PERM, &port->phy_adv_cap_mask);
399 /* Enable flow control by default */
400 port->flow_ctrl = EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE;
401 port->flow_ctrl_autoneg = B_TRUE;
403 RTE_BUILD_BUG_ON(sizeof(encp->enc_mac_addr) != sizeof(*from));
404 from = (const struct rte_ether_addr *)(encp->enc_mac_addr);
405 rte_ether_addr_copy(from, &port->default_mac_addr);
407 port->max_mcast_addrs = EFX_MAC_MULTICAST_LIST_MAX;
408 port->nb_mcast_addrs = 0;
409 port->mcast_addrs = rte_calloc_socket("mcast_addr_list_buf",
410 port->max_mcast_addrs,
413 if (port->mcast_addrs == NULL) {
415 goto fail_mcast_addr_list_buf_alloc;
418 rte_spinlock_init(&port->mac_stats_lock);
421 port->mac_stats_buf = rte_calloc_socket("mac_stats_buf", EFX_MAC_NSTATS,
424 if (port->mac_stats_buf == NULL)
425 goto fail_mac_stats_buf_alloc;
427 mac_nstats = efx_nic_cfg_get(sa->nic)->enc_mac_stats_nstats;
428 mac_stats_size = RTE_ALIGN(mac_nstats * sizeof(uint64_t), EFX_BUF_SIZE);
429 rc = sfc_dma_alloc(sa, "mac_stats", 0, mac_stats_size,
430 sa->socket_id, &port->mac_stats_dma_mem);
432 goto fail_mac_stats_dma_alloc;
434 port->mac_stats_reset_pending = B_FALSE;
436 kvarg_stats_update_period_ms = SFC_MAC_STATS_UPDATE_PERIOD_MS_DEF;
438 rc = sfc_kvargs_process(sa, SFC_KVARG_STATS_UPDATE_PERIOD_MS,
439 sfc_kvarg_long_handler,
440 &kvarg_stats_update_period_ms);
442 ((kvarg_stats_update_period_ms < 0) ||
443 (kvarg_stats_update_period_ms > UINT16_MAX))) {
444 sfc_err(sa, "wrong '" SFC_KVARG_STATS_UPDATE_PERIOD_MS "' "
445 "was set (%ld);", kvarg_stats_update_period_ms);
446 sfc_err(sa, "it must not be less than 0 "
447 "or greater than %" PRIu16, UINT16_MAX);
449 goto fail_kvarg_stats_update_period_ms;
450 } else if (rc != 0) {
451 goto fail_kvarg_stats_update_period_ms;
454 port->mac_stats_update_period_ms = kvarg_stats_update_period_ms;
456 sfc_log_init(sa, "done");
459 fail_kvarg_stats_update_period_ms:
460 sfc_dma_free(sa, &port->mac_stats_dma_mem);
462 fail_mac_stats_dma_alloc:
463 rte_free(port->mac_stats_buf);
465 fail_mac_stats_buf_alloc:
466 rte_free(port->mcast_addrs);
468 fail_mcast_addr_list_buf_alloc:
469 sfc_log_init(sa, "failed %d", rc);
474 sfc_port_detach(struct sfc_adapter *sa)
476 struct sfc_port *port = &sa->port;
478 sfc_log_init(sa, "entry");
480 sfc_dma_free(sa, &port->mac_stats_dma_mem);
481 rte_free(port->mac_stats_buf);
483 rte_free(port->mcast_addrs);
485 sfc_log_init(sa, "done");
489 sfc_get_requested_all_ucast(struct sfc_port *port)
491 return port->promisc;
495 sfc_get_requested_all_mcast(struct sfc_port *port)
497 return port->promisc || port->allmulti;
501 sfc_set_rx_mode_unchecked(struct sfc_adapter *sa)
503 struct sfc_port *port = &sa->port;
504 boolean_t requested_all_ucast = sfc_get_requested_all_ucast(port);
505 boolean_t requested_all_mcast = sfc_get_requested_all_mcast(port);
508 rc = efx_mac_filter_set(sa->nic, requested_all_ucast, B_TRUE,
509 requested_all_mcast, B_TRUE);
517 sfc_set_rx_mode(struct sfc_adapter *sa)
519 struct sfc_port *port = &sa->port;
520 boolean_t old_all_ucast;
521 boolean_t old_all_mcast;
522 boolean_t requested_all_ucast = sfc_get_requested_all_ucast(port);
523 boolean_t requested_all_mcast = sfc_get_requested_all_mcast(port);
524 boolean_t actual_all_ucast;
525 boolean_t actual_all_mcast;
528 efx_mac_filter_get_all_ucast_mcast(sa->nic, &old_all_ucast,
531 rc = sfc_set_rx_mode_unchecked(sa);
535 efx_mac_filter_get_all_ucast_mcast(sa->nic, &actual_all_ucast,
538 if (actual_all_ucast != requested_all_ucast ||
539 actual_all_mcast != requested_all_mcast) {
541 * MAC filter set succeeded but not all requested modes
542 * were applied. The rollback is necessary to bring back the
543 * consistent old state.
545 (void)efx_mac_filter_set(sa->nic, old_all_ucast, B_TRUE,
546 old_all_mcast, B_TRUE);
555 sfc_port_link_mode_to_info(efx_link_mode_t link_mode,
556 struct rte_eth_link *link_info)
558 SFC_ASSERT(link_mode < EFX_LINK_NMODES);
560 memset(link_info, 0, sizeof(*link_info));
561 if ((link_mode == EFX_LINK_DOWN) || (link_mode == EFX_LINK_UNKNOWN))
562 link_info->link_status = ETH_LINK_DOWN;
564 link_info->link_status = ETH_LINK_UP;
568 link_info->link_speed = ETH_SPEED_NUM_10M;
569 link_info->link_duplex = ETH_LINK_HALF_DUPLEX;
572 link_info->link_speed = ETH_SPEED_NUM_10M;
573 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
575 case EFX_LINK_100HDX:
576 link_info->link_speed = ETH_SPEED_NUM_100M;
577 link_info->link_duplex = ETH_LINK_HALF_DUPLEX;
579 case EFX_LINK_100FDX:
580 link_info->link_speed = ETH_SPEED_NUM_100M;
581 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
583 case EFX_LINK_1000HDX:
584 link_info->link_speed = ETH_SPEED_NUM_1G;
585 link_info->link_duplex = ETH_LINK_HALF_DUPLEX;
587 case EFX_LINK_1000FDX:
588 link_info->link_speed = ETH_SPEED_NUM_1G;
589 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
591 case EFX_LINK_10000FDX:
592 link_info->link_speed = ETH_SPEED_NUM_10G;
593 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
595 case EFX_LINK_25000FDX:
596 link_info->link_speed = ETH_SPEED_NUM_25G;
597 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
599 case EFX_LINK_40000FDX:
600 link_info->link_speed = ETH_SPEED_NUM_40G;
601 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
603 case EFX_LINK_50000FDX:
604 link_info->link_speed = ETH_SPEED_NUM_50G;
605 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
607 case EFX_LINK_100000FDX:
608 link_info->link_speed = ETH_SPEED_NUM_100G;
609 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
614 case EFX_LINK_UNKNOWN:
616 link_info->link_speed = ETH_SPEED_NUM_NONE;
617 link_info->link_duplex = 0;
621 link_info->link_autoneg = ETH_LINK_AUTONEG;