net/enic: use dynamic log types
[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
303         sfc_log_init(sa, "entry");
304
305         if (dev_data->dev_conf.rxmode.jumbo_frame)
306                 port->pdu = dev_data->dev_conf.rxmode.max_rx_pkt_len;
307         else
308                 port->pdu = EFX_MAC_PDU(dev_data->mtu);
309
310         return 0;
311 }
312
313 void
314 sfc_port_close(struct sfc_adapter *sa)
315 {
316         sfc_log_init(sa, "entry");
317 }
318
319 int
320 sfc_port_attach(struct sfc_adapter *sa)
321 {
322         struct sfc_port *port = &sa->port;
323         const efx_nic_cfg_t *encp = efx_nic_cfg_get(sa->nic);
324         const struct ether_addr *from;
325         long kvarg_stats_update_period_ms;
326         int rc;
327
328         sfc_log_init(sa, "entry");
329
330         efx_phy_adv_cap_get(sa->nic, EFX_PHY_CAP_PERM, &port->phy_adv_cap_mask);
331
332         /* Enable flow control by default */
333         port->flow_ctrl = EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE;
334         port->flow_ctrl_autoneg = B_TRUE;
335
336         RTE_BUILD_BUG_ON(sizeof(encp->enc_mac_addr) != sizeof(*from));
337         from = (const struct ether_addr *)(encp->enc_mac_addr);
338         ether_addr_copy(from, &port->default_mac_addr);
339
340         port->max_mcast_addrs = EFX_MAC_MULTICAST_LIST_MAX;
341         port->nb_mcast_addrs = 0;
342         port->mcast_addrs = rte_calloc_socket("mcast_addr_list_buf",
343                                               port->max_mcast_addrs,
344                                               EFX_MAC_ADDR_LEN, 0,
345                                               sa->socket_id);
346         if (port->mcast_addrs == NULL) {
347                 rc = ENOMEM;
348                 goto fail_mcast_addr_list_buf_alloc;
349         }
350
351         rte_spinlock_init(&port->mac_stats_lock);
352
353         rc = ENOMEM;
354         port->mac_stats_buf = rte_calloc_socket("mac_stats_buf", EFX_MAC_NSTATS,
355                                                 sizeof(uint64_t), 0,
356                                                 sa->socket_id);
357         if (port->mac_stats_buf == NULL)
358                 goto fail_mac_stats_buf_alloc;
359
360         rc = sfc_dma_alloc(sa, "mac_stats", 0, EFX_MAC_STATS_SIZE,
361                            sa->socket_id, &port->mac_stats_dma_mem);
362         if (rc != 0)
363                 goto fail_mac_stats_dma_alloc;
364
365         port->mac_stats_reset_pending = B_FALSE;
366
367         kvarg_stats_update_period_ms = SFC_MAC_STATS_UPDATE_PERIOD_MS_DEF;
368
369         rc = sfc_kvargs_process(sa, SFC_KVARG_STATS_UPDATE_PERIOD_MS,
370                                 sfc_kvarg_long_handler,
371                                 &kvarg_stats_update_period_ms);
372         if ((rc == 0) &&
373             ((kvarg_stats_update_period_ms < 0) ||
374              (kvarg_stats_update_period_ms > UINT16_MAX))) {
375                 sfc_err(sa, "wrong '" SFC_KVARG_STATS_UPDATE_PERIOD_MS "' "
376                             "was set (%ld);", kvarg_stats_update_period_ms);
377                 sfc_err(sa, "it must not be less than 0 "
378                             "or greater than %" PRIu16, UINT16_MAX);
379                 rc = EINVAL;
380                 goto fail_kvarg_stats_update_period_ms;
381         } else if (rc != 0) {
382                 goto fail_kvarg_stats_update_period_ms;
383         }
384
385         port->mac_stats_update_period_ms = kvarg_stats_update_period_ms;
386
387         sfc_log_init(sa, "done");
388         return 0;
389
390 fail_kvarg_stats_update_period_ms:
391         sfc_dma_free(sa, &port->mac_stats_dma_mem);
392
393 fail_mac_stats_dma_alloc:
394         rte_free(port->mac_stats_buf);
395
396 fail_mac_stats_buf_alloc:
397         rte_free(port->mcast_addrs);
398
399 fail_mcast_addr_list_buf_alloc:
400         sfc_log_init(sa, "failed %d", rc);
401         return rc;
402 }
403
404 void
405 sfc_port_detach(struct sfc_adapter *sa)
406 {
407         struct sfc_port *port = &sa->port;
408
409         sfc_log_init(sa, "entry");
410
411         sfc_dma_free(sa, &port->mac_stats_dma_mem);
412         rte_free(port->mac_stats_buf);
413
414         rte_free(port->mcast_addrs);
415
416         sfc_log_init(sa, "done");
417 }
418
419 int
420 sfc_set_rx_mode(struct sfc_adapter *sa)
421 {
422         struct sfc_port *port = &sa->port;
423         int rc;
424
425         rc = efx_mac_filter_set(sa->nic, port->promisc, B_TRUE,
426                                 port->promisc || port->allmulti, B_TRUE);
427
428         return rc;
429 }
430
431 void
432 sfc_port_link_mode_to_info(efx_link_mode_t link_mode,
433                            struct rte_eth_link *link_info)
434 {
435         SFC_ASSERT(link_mode < EFX_LINK_NMODES);
436
437         memset(link_info, 0, sizeof(*link_info));
438         if ((link_mode == EFX_LINK_DOWN) || (link_mode == EFX_LINK_UNKNOWN))
439                 link_info->link_status = ETH_LINK_DOWN;
440         else
441                 link_info->link_status = ETH_LINK_UP;
442
443         switch (link_mode) {
444         case EFX_LINK_10HDX:
445                 link_info->link_speed  = ETH_SPEED_NUM_10M;
446                 link_info->link_duplex = ETH_LINK_HALF_DUPLEX;
447                 break;
448         case EFX_LINK_10FDX:
449                 link_info->link_speed  = ETH_SPEED_NUM_10M;
450                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
451                 break;
452         case EFX_LINK_100HDX:
453                 link_info->link_speed  = ETH_SPEED_NUM_100M;
454                 link_info->link_duplex = ETH_LINK_HALF_DUPLEX;
455                 break;
456         case EFX_LINK_100FDX:
457                 link_info->link_speed  = ETH_SPEED_NUM_100M;
458                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
459                 break;
460         case EFX_LINK_1000HDX:
461                 link_info->link_speed  = ETH_SPEED_NUM_1G;
462                 link_info->link_duplex = ETH_LINK_HALF_DUPLEX;
463                 break;
464         case EFX_LINK_1000FDX:
465                 link_info->link_speed  = ETH_SPEED_NUM_1G;
466                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
467                 break;
468         case EFX_LINK_10000FDX:
469                 link_info->link_speed  = ETH_SPEED_NUM_10G;
470                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
471                 break;
472         case EFX_LINK_40000FDX:
473                 link_info->link_speed  = ETH_SPEED_NUM_40G;
474                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
475                 break;
476         default:
477                 SFC_ASSERT(B_FALSE);
478                 /* FALLTHROUGH */
479         case EFX_LINK_UNKNOWN:
480         case EFX_LINK_DOWN:
481                 link_info->link_speed  = ETH_SPEED_NUM_NONE;
482                 link_info->link_duplex = 0;
483                 break;
484         }
485
486         link_info->link_autoneg = ETH_LINK_AUTONEG;
487 }