net/ixgbe: fix CRC stripping capability
[dpdk.git] / drivers / net / sfc / sfc_port.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright (c) 2016-2018 Solarflare Communications Inc.
4  * All rights reserved.
5  *
6  * This software was jointly developed between OKTET Labs (under contract
7  * for Solarflare) and Solarflare Communications, Inc.
8  */
9
10 #include "efx.h"
11
12 #include "sfc.h"
13 #include "sfc_log.h"
14 #include "sfc_kvargs.h"
15
16 /** Default MAC statistics update period is 1 second */
17 #define SFC_MAC_STATS_UPDATE_PERIOD_MS_DEF      MS_PER_S
18
19 /** The number of microseconds to sleep on attempt to get statistics update */
20 #define SFC_MAC_STATS_UPDATE_RETRY_INTERVAL_US  10
21
22 /** The number of attempts to await arrival of freshly generated statistics */
23 #define SFC_MAC_STATS_UPDATE_NB_ATTEMPTS        50
24
25 /**
26  * Update MAC statistics in the buffer.
27  *
28  * @param       sa      Adapter
29  *
30  * @return Status code
31  * @retval      0       Success
32  * @retval      EAGAIN  Try again
33  * @retval      ENOMEM  Memory allocation failure
34  */
35 int
36 sfc_port_update_mac_stats(struct sfc_adapter *sa)
37 {
38         struct sfc_port *port = &sa->port;
39         efsys_mem_t *esmp = &port->mac_stats_dma_mem;
40         uint32_t *genp = NULL;
41         uint32_t gen_old;
42         unsigned int nb_attempts = 0;
43         int rc;
44
45         SFC_ASSERT(rte_spinlock_is_locked(&port->mac_stats_lock));
46
47         if (sa->state != SFC_ADAPTER_STARTED)
48                 return EINVAL;
49
50         /*
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
53          */
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();
58
59                         if ((timestamp -
60                              port->mac_stats_last_request_timestamp) <
61                             port->mac_stats_update_period_ms)
62                                 return 0;
63
64                         port->mac_stats_last_request_timestamp = timestamp;
65                 }
66
67                 rc = efx_mac_stats_upload(sa->nic, esmp);
68                 if (rc != 0)
69                         return rc;
70
71                 genp = &port->mac_stats_update_generation;
72                 gen_old = *genp;
73         }
74
75         do {
76                 if (nb_attempts > 0)
77                         rte_delay_us(SFC_MAC_STATS_UPDATE_RETRY_INTERVAL_US);
78
79                 rc = efx_mac_stats_update(sa->nic, esmp,
80                                           port->mac_stats_buf, genp);
81                 if (rc != 0)
82                         return rc;
83
84         } while ((genp != NULL) && (*genp == gen_old) &&
85                  (++nb_attempts < SFC_MAC_STATS_UPDATE_NB_ATTEMPTS));
86
87         return 0;
88 }
89
90 int
91 sfc_port_reset_mac_stats(struct sfc_adapter *sa)
92 {
93         struct sfc_port *port = &sa->port;
94         int rc;
95
96         rte_spinlock_lock(&port->mac_stats_lock);
97         rc = efx_mac_stats_clear(sa->nic);
98         rte_spinlock_unlock(&port->mac_stats_lock);
99
100         return rc;
101 }
102
103 static int
104 sfc_port_init_dev_link(struct sfc_adapter *sa)
105 {
106         struct rte_eth_link *dev_link = &sa->eth_dev->data->dev_link;
107         int rc;
108         efx_link_mode_t link_mode;
109         struct rte_eth_link current_link;
110
111         rc = efx_port_poll(sa->nic, &link_mode);
112         if (rc != 0)
113                 return rc;
114
115         sfc_port_link_mode_to_info(link_mode, &current_link);
116
117         EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
118         rte_atomic64_set((rte_atomic64_t *)dev_link,
119                          *(uint64_t *)&current_link);
120
121         return 0;
122 }
123
124 int
125 sfc_port_start(struct sfc_adapter *sa)
126 {
127         struct sfc_port *port = &sa->port;
128         int rc;
129         uint32_t phy_adv_cap;
130         const uint32_t phy_pause_caps =
131                 ((1u << EFX_PHY_CAP_PAUSE) | (1u << EFX_PHY_CAP_ASYM));
132         unsigned int i;
133
134         sfc_log_init(sa, "entry");
135
136         sfc_log_init(sa, "init filters");
137         rc = efx_filter_init(sa->nic);
138         if (rc != 0)
139                 goto fail_filter_init;
140
141         sfc_log_init(sa, "init port");
142         rc = efx_port_init(sa->nic);
143         if (rc != 0)
144                 goto fail_port_init;
145
146         sfc_log_init(sa, "set flow control to %#x autoneg=%u",
147                      port->flow_ctrl, port->flow_ctrl_autoneg);
148         rc = efx_mac_fcntl_set(sa->nic, port->flow_ctrl,
149                                port->flow_ctrl_autoneg);
150         if (rc != 0)
151                 goto fail_mac_fcntl_set;
152
153         /* Preserve pause capabilities set by above efx_mac_fcntl_set()  */
154         efx_phy_adv_cap_get(sa->nic, EFX_PHY_CAP_CURRENT, &phy_adv_cap);
155         SFC_ASSERT((port->phy_adv_cap & phy_pause_caps) == 0);
156         phy_adv_cap = port->phy_adv_cap | (phy_adv_cap & phy_pause_caps);
157
158         sfc_log_init(sa, "set phy adv caps to %#x", phy_adv_cap);
159         rc = efx_phy_adv_cap_set(sa->nic, phy_adv_cap);
160         if (rc != 0)
161                 goto fail_phy_adv_cap_set;
162
163         sfc_log_init(sa, "set MAC PDU %u", (unsigned int)port->pdu);
164         rc = efx_mac_pdu_set(sa->nic, port->pdu);
165         if (rc != 0)
166                 goto fail_mac_pdu_set;
167
168         if (!port->isolated) {
169                 struct ether_addr *addr = &port->default_mac_addr;
170
171                 sfc_log_init(sa, "set MAC address");
172                 rc = efx_mac_addr_set(sa->nic, addr->addr_bytes);
173                 if (rc != 0)
174                         goto fail_mac_addr_set;
175
176                 sfc_log_init(sa, "set MAC filters");
177                 port->promisc = (sa->eth_dev->data->promiscuous != 0) ?
178                                 B_TRUE : B_FALSE;
179                 port->allmulti = (sa->eth_dev->data->all_multicast != 0) ?
180                                  B_TRUE : B_FALSE;
181                 rc = sfc_set_rx_mode(sa);
182                 if (rc != 0)
183                         goto fail_mac_filter_set;
184
185                 sfc_log_init(sa, "set multicast address list");
186                 rc = efx_mac_multicast_list_set(sa->nic, port->mcast_addrs,
187                                                 port->nb_mcast_addrs);
188                 if (rc != 0)
189                         goto fail_mcast_address_list_set;
190         }
191
192         if (port->mac_stats_reset_pending) {
193                 rc = sfc_port_reset_mac_stats(sa);
194                 if (rc != 0)
195                         sfc_err(sa, "statistics reset failed (requested "
196                                     "before the port was started)");
197
198                 port->mac_stats_reset_pending = B_FALSE;
199         }
200
201         efx_mac_stats_get_mask(sa->nic, port->mac_stats_mask,
202                                sizeof(port->mac_stats_mask));
203
204         for (i = 0, port->mac_stats_nb_supported = 0; i < EFX_MAC_NSTATS; ++i)
205                 if (EFX_MAC_STAT_SUPPORTED(port->mac_stats_mask, i))
206                         port->mac_stats_nb_supported++;
207
208         port->mac_stats_update_generation = 0;
209
210         if (port->mac_stats_update_period_ms != 0) {
211                 /*
212                  * Update MAC stats using periodic DMA;
213                  * any positive update interval different from
214                  * 1000 ms can be set only on SFN8xxx provided
215                  * that FW version is 6.2.1.1033 or higher
216                  */
217                 sfc_log_init(sa, "request MAC stats DMA'ing");
218                 rc = efx_mac_stats_periodic(sa->nic, &port->mac_stats_dma_mem,
219                                             port->mac_stats_update_period_ms,
220                                             B_FALSE);
221                 if (rc == 0) {
222                         port->mac_stats_periodic_dma_supported = B_TRUE;
223                 } else if (rc == EOPNOTSUPP) {
224                         port->mac_stats_periodic_dma_supported = B_FALSE;
225                         port->mac_stats_last_request_timestamp = 0;
226                 } else {
227                         goto fail_mac_stats_periodic;
228                 }
229         }
230
231         if ((port->mac_stats_update_period_ms != 0) &&
232             port->mac_stats_periodic_dma_supported) {
233                 /*
234                  * Request an explicit MAC stats upload immediately to
235                  * preclude bogus figures readback if the user decides
236                  * to read stats before periodic DMA is really started
237                  */
238                 rc = efx_mac_stats_upload(sa->nic, &port->mac_stats_dma_mem);
239                 if (rc != 0)
240                         goto fail_mac_stats_upload;
241         }
242
243         sfc_log_init(sa, "disable MAC drain");
244         rc = efx_mac_drain(sa->nic, B_FALSE);
245         if (rc != 0)
246                 goto fail_mac_drain;
247
248         /* Synchronize link status knowledge */
249         rc = sfc_port_init_dev_link(sa);
250         if (rc != 0)
251                 goto fail_port_init_dev_link;
252
253         sfc_log_init(sa, "done");
254         return 0;
255
256 fail_port_init_dev_link:
257         (void)efx_mac_drain(sa->nic, B_TRUE);
258
259 fail_mac_drain:
260 fail_mac_stats_upload:
261         (void)efx_mac_stats_periodic(sa->nic, &port->mac_stats_dma_mem,
262                                      0, B_FALSE);
263
264 fail_mac_stats_periodic:
265 fail_mcast_address_list_set:
266 fail_mac_filter_set:
267 fail_mac_addr_set:
268 fail_mac_pdu_set:
269 fail_phy_adv_cap_set:
270 fail_mac_fcntl_set:
271         efx_port_fini(sa->nic);
272
273 fail_port_init:
274         efx_filter_fini(sa->nic);
275
276 fail_filter_init:
277         sfc_log_init(sa, "failed %d", rc);
278         return rc;
279 }
280
281 void
282 sfc_port_stop(struct sfc_adapter *sa)
283 {
284         sfc_log_init(sa, "entry");
285
286         efx_mac_drain(sa->nic, B_TRUE);
287
288         (void)efx_mac_stats_periodic(sa->nic, &sa->port.mac_stats_dma_mem,
289                                      0, B_FALSE);
290
291         efx_port_fini(sa->nic);
292         efx_filter_fini(sa->nic);
293
294         sfc_log_init(sa, "done");
295 }
296
297 int
298 sfc_port_configure(struct sfc_adapter *sa)
299 {
300         const struct rte_eth_dev_data *dev_data = sa->eth_dev->data;
301         struct sfc_port *port = &sa->port;
302         const struct rte_eth_rxmode *rxmode = &dev_data->dev_conf.rxmode;
303
304         sfc_log_init(sa, "entry");
305
306         if (rxmode->offloads & DEV_RX_OFFLOAD_JUMBO_FRAME)
307                 port->pdu = rxmode->max_rx_pkt_len;
308         else
309                 port->pdu = EFX_MAC_PDU(dev_data->mtu);
310
311         return 0;
312 }
313
314 void
315 sfc_port_close(struct sfc_adapter *sa)
316 {
317         sfc_log_init(sa, "entry");
318 }
319
320 int
321 sfc_port_attach(struct sfc_adapter *sa)
322 {
323         struct sfc_port *port = &sa->port;
324         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
325         const struct ether_addr *from;
326         long kvarg_stats_update_period_ms;
327         int rc;
328
329         sfc_log_init(sa, "entry");
330
331         efx_phy_adv_cap_get(sa->nic, EFX_PHY_CAP_PERM, &port->phy_adv_cap_mask);
332
333         /* Enable flow control by default */
334         port->flow_ctrl = EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE;
335         port->flow_ctrl_autoneg = B_TRUE;
336
337         RTE_BUILD_BUG_ON(sizeof(encp->enc_mac_addr) != sizeof(*from));
338         from = (const struct ether_addr *)(encp->enc_mac_addr);
339         ether_addr_copy(from, &port->default_mac_addr);
340
341         port->max_mcast_addrs = EFX_MAC_MULTICAST_LIST_MAX;
342         port->nb_mcast_addrs = 0;
343         port->mcast_addrs = rte_calloc_socket("mcast_addr_list_buf",
344                                               port->max_mcast_addrs,
345                                               EFX_MAC_ADDR_LEN, 0,
346                                               sa->socket_id);
347         if (port->mcast_addrs == NULL) {
348                 rc = ENOMEM;
349                 goto fail_mcast_addr_list_buf_alloc;
350         }
351
352         rte_spinlock_init(&port->mac_stats_lock);
353
354         rc = ENOMEM;
355         port->mac_stats_buf = rte_calloc_socket("mac_stats_buf", EFX_MAC_NSTATS,
356                                                 sizeof(uint64_t), 0,
357                                                 sa->socket_id);
358         if (port->mac_stats_buf == NULL)
359                 goto fail_mac_stats_buf_alloc;
360
361         rc = sfc_dma_alloc(sa, "mac_stats", 0, EFX_MAC_STATS_SIZE,
362                            sa->socket_id, &port->mac_stats_dma_mem);
363         if (rc != 0)
364                 goto fail_mac_stats_dma_alloc;
365
366         port->mac_stats_reset_pending = B_FALSE;
367
368         kvarg_stats_update_period_ms = SFC_MAC_STATS_UPDATE_PERIOD_MS_DEF;
369
370         rc = sfc_kvargs_process(sa, SFC_KVARG_STATS_UPDATE_PERIOD_MS,
371                                 sfc_kvarg_long_handler,
372                                 &kvarg_stats_update_period_ms);
373         if ((rc == 0) &&
374             ((kvarg_stats_update_period_ms < 0) ||
375              (kvarg_stats_update_period_ms > UINT16_MAX))) {
376                 sfc_err(sa, "wrong '" SFC_KVARG_STATS_UPDATE_PERIOD_MS "' "
377                             "was set (%ld);", kvarg_stats_update_period_ms);
378                 sfc_err(sa, "it must not be less than 0 "
379                             "or greater than %" PRIu16, UINT16_MAX);
380                 rc = EINVAL;
381                 goto fail_kvarg_stats_update_period_ms;
382         } else if (rc != 0) {
383                 goto fail_kvarg_stats_update_period_ms;
384         }
385
386         port->mac_stats_update_period_ms = kvarg_stats_update_period_ms;
387
388         sfc_log_init(sa, "done");
389         return 0;
390
391 fail_kvarg_stats_update_period_ms:
392         sfc_dma_free(sa, &port->mac_stats_dma_mem);
393
394 fail_mac_stats_dma_alloc:
395         rte_free(port->mac_stats_buf);
396
397 fail_mac_stats_buf_alloc:
398         rte_free(port->mcast_addrs);
399
400 fail_mcast_addr_list_buf_alloc:
401         sfc_log_init(sa, "failed %d", rc);
402         return rc;
403 }
404
405 void
406 sfc_port_detach(struct sfc_adapter *sa)
407 {
408         struct sfc_port *port = &sa->port;
409
410         sfc_log_init(sa, "entry");
411
412         sfc_dma_free(sa, &port->mac_stats_dma_mem);
413         rte_free(port->mac_stats_buf);
414
415         rte_free(port->mcast_addrs);
416
417         sfc_log_init(sa, "done");
418 }
419
420 int
421 sfc_set_rx_mode(struct sfc_adapter *sa)
422 {
423         struct sfc_port *port = &sa->port;
424         int rc;
425
426         rc = efx_mac_filter_set(sa->nic, port->promisc, B_TRUE,
427                                 port->promisc || port->allmulti, B_TRUE);
428
429         return rc;
430 }
431
432 void
433 sfc_port_link_mode_to_info(efx_link_mode_t link_mode,
434                            struct rte_eth_link *link_info)
435 {
436         SFC_ASSERT(link_mode < EFX_LINK_NMODES);
437
438         memset(link_info, 0, sizeof(*link_info));
439         if ((link_mode == EFX_LINK_DOWN) || (link_mode == EFX_LINK_UNKNOWN))
440                 link_info->link_status = ETH_LINK_DOWN;
441         else
442                 link_info->link_status = ETH_LINK_UP;
443
444         switch (link_mode) {
445         case EFX_LINK_10HDX:
446                 link_info->link_speed  = ETH_SPEED_NUM_10M;
447                 link_info->link_duplex = ETH_LINK_HALF_DUPLEX;
448                 break;
449         case EFX_LINK_10FDX:
450                 link_info->link_speed  = ETH_SPEED_NUM_10M;
451                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
452                 break;
453         case EFX_LINK_100HDX:
454                 link_info->link_speed  = ETH_SPEED_NUM_100M;
455                 link_info->link_duplex = ETH_LINK_HALF_DUPLEX;
456                 break;
457         case EFX_LINK_100FDX:
458                 link_info->link_speed  = ETH_SPEED_NUM_100M;
459                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
460                 break;
461         case EFX_LINK_1000HDX:
462                 link_info->link_speed  = ETH_SPEED_NUM_1G;
463                 link_info->link_duplex = ETH_LINK_HALF_DUPLEX;
464                 break;
465         case EFX_LINK_1000FDX:
466                 link_info->link_speed  = ETH_SPEED_NUM_1G;
467                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
468                 break;
469         case EFX_LINK_10000FDX:
470                 link_info->link_speed  = ETH_SPEED_NUM_10G;
471                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
472                 break;
473         case EFX_LINK_40000FDX:
474                 link_info->link_speed  = ETH_SPEED_NUM_40G;
475                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
476                 break;
477         default:
478                 SFC_ASSERT(B_FALSE);
479                 /* FALLTHROUGH */
480         case EFX_LINK_UNKNOWN:
481         case EFX_LINK_DOWN:
482                 link_info->link_speed  = ETH_SPEED_NUM_NONE;
483                 link_info->link_duplex = 0;
484                 break;
485         }
486
487         link_info->link_autoneg = ETH_LINK_AUTONEG;
488 }