net/sfc: support statistics reset
[dpdk.git] / drivers / net / sfc / sfc_port.c
1 /*-
2  * Copyright (c) 2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * This software was jointly developed between OKTET Labs (under contract
6  * for Solarflare) and Solarflare Communications, Inc.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
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.
16  *
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.
28  */
29
30 #include "efx.h"
31
32 #include "sfc.h"
33 #include "sfc_log.h"
34
35 /**
36  * Update MAC statistics in the buffer.
37  *
38  * @param       sa      Adapter
39  *
40  * @return Status code
41  * @retval      0       Success
42  * @retval      EAGAIN  Try again
43  * @retval      ENOMEM  Memory allocation failure
44  */
45 int
46 sfc_port_update_mac_stats(struct sfc_adapter *sa)
47 {
48         struct sfc_port *port = &sa->port;
49         int rc;
50
51         SFC_ASSERT(rte_spinlock_is_locked(&port->mac_stats_lock));
52
53         if (sa->state != SFC_ADAPTER_STARTED)
54                 return EINVAL;
55
56         rc = efx_mac_stats_update(sa->nic, &port->mac_stats_dma_mem,
57                                   port->mac_stats_buf, NULL);
58         if (rc != 0)
59                 return rc;
60
61         return 0;
62 }
63
64 int
65 sfc_port_reset_mac_stats(struct sfc_adapter *sa)
66 {
67         struct sfc_port *port = &sa->port;
68         int rc;
69
70         rte_spinlock_lock(&port->mac_stats_lock);
71         rc = efx_mac_stats_clear(sa->nic);
72         rte_spinlock_unlock(&port->mac_stats_lock);
73
74         return rc;
75 }
76
77 static int
78 sfc_port_init_dev_link(struct sfc_adapter *sa)
79 {
80         struct rte_eth_link *dev_link = &sa->eth_dev->data->dev_link;
81         int rc;
82         efx_link_mode_t link_mode;
83         struct rte_eth_link current_link;
84
85         rc = efx_port_poll(sa->nic, &link_mode);
86         if (rc != 0)
87                 return rc;
88
89         sfc_port_link_mode_to_info(link_mode, &current_link);
90
91         EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
92         rte_atomic64_set((rte_atomic64_t *)dev_link,
93                          *(uint64_t *)&current_link);
94
95         return 0;
96 }
97
98 int
99 sfc_port_start(struct sfc_adapter *sa)
100 {
101         struct sfc_port *port = &sa->port;
102         int rc;
103         uint32_t phy_adv_cap;
104         const uint32_t phy_pause_caps =
105                 ((1u << EFX_PHY_CAP_PAUSE) | (1u << EFX_PHY_CAP_ASYM));
106
107         sfc_log_init(sa, "entry");
108
109         sfc_log_init(sa, "init filters");
110         rc = efx_filter_init(sa->nic);
111         if (rc != 0)
112                 goto fail_filter_init;
113
114         sfc_log_init(sa, "init port");
115         rc = efx_port_init(sa->nic);
116         if (rc != 0)
117                 goto fail_port_init;
118
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);
123         if (rc != 0)
124                 goto fail_mac_fcntl_set;
125
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);
130
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);
133         if (rc != 0)
134                 goto fail_phy_adv_cap_set;
135
136         sfc_log_init(sa, "set MAC PDU %u", (unsigned int)port->pdu);
137         rc = efx_mac_pdu_set(sa->nic, port->pdu);
138         if (rc != 0)
139                 goto fail_mac_pdu_set;
140
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);
144         if (rc != 0)
145                 goto fail_mac_addr_set;
146
147         sfc_log_init(sa, "set MAC filters");
148         port->promisc = (sa->eth_dev->data->promiscuous != 0) ?
149                         B_TRUE : B_FALSE;
150         port->allmulti = (sa->eth_dev->data->all_multicast != 0) ?
151                          B_TRUE : B_FALSE;
152         rc = sfc_set_rx_mode(sa);
153         if (rc != 0)
154                 goto fail_mac_filter_set;
155
156         if (port->mac_stats_reset_pending) {
157                 rc = sfc_port_reset_mac_stats(sa);
158                 if (rc != 0)
159                         sfc_err(sa, "statistics reset failed (requested "
160                                     "before the port was started)");
161
162                 port->mac_stats_reset_pending = B_FALSE;
163         }
164
165         efx_mac_stats_get_mask(sa->nic, port->mac_stats_mask,
166                                sizeof(port->mac_stats_mask));
167
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.
171          */
172         sfc_log_init(sa, "request MAC stats DMA'ing");
173         rc = efx_mac_stats_periodic(sa->nic, &port->mac_stats_dma_mem,
174                                     1000, B_FALSE);
175         if (rc != 0)
176                 goto fail_mac_stats_periodic;
177
178         sfc_log_init(sa, "disable MAC drain");
179         rc = efx_mac_drain(sa->nic, B_FALSE);
180         if (rc != 0)
181                 goto fail_mac_drain;
182
183         /* Synchronize link status knowledge */
184         rc = sfc_port_init_dev_link(sa);
185         if (rc != 0)
186                 goto fail_port_init_dev_link;
187
188         sfc_log_init(sa, "done");
189         return 0;
190
191 fail_port_init_dev_link:
192         (void)efx_mac_drain(sa->nic, B_TRUE);
193
194 fail_mac_drain:
195         (void)efx_mac_stats_periodic(sa->nic, &port->mac_stats_dma_mem,
196                                      0, B_FALSE);
197
198 fail_mac_stats_periodic:
199 fail_mac_filter_set:
200 fail_mac_addr_set:
201 fail_mac_pdu_set:
202 fail_phy_adv_cap_set:
203 fail_mac_fcntl_set:
204         efx_port_fini(sa->nic);
205
206 fail_port_init:
207         efx_filter_fini(sa->nic);
208
209 fail_filter_init:
210         sfc_log_init(sa, "failed %d", rc);
211         return rc;
212 }
213
214 void
215 sfc_port_stop(struct sfc_adapter *sa)
216 {
217         sfc_log_init(sa, "entry");
218
219         efx_mac_drain(sa->nic, B_TRUE);
220
221         (void)efx_mac_stats_periodic(sa->nic, &sa->port.mac_stats_dma_mem,
222                                      0, B_FALSE);
223
224         efx_port_fini(sa->nic);
225         efx_filter_fini(sa->nic);
226
227         sfc_log_init(sa, "done");
228 }
229
230 int
231 sfc_port_init(struct sfc_adapter *sa)
232 {
233         const struct rte_eth_dev_data *dev_data = sa->eth_dev->data;
234         struct sfc_port *port = &sa->port;
235         int rc;
236
237         sfc_log_init(sa, "entry");
238
239         /* Enable flow control by default */
240         port->flow_ctrl = EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE;
241         port->flow_ctrl_autoneg = B_TRUE;
242
243         if (dev_data->dev_conf.rxmode.jumbo_frame)
244                 port->pdu = dev_data->dev_conf.rxmode.max_rx_pkt_len;
245         else
246                 port->pdu = EFX_MAC_PDU(dev_data->mtu);
247
248         rte_spinlock_init(&port->mac_stats_lock);
249
250         rc = ENOMEM;
251         port->mac_stats_buf = rte_calloc_socket("mac_stats_buf", EFX_MAC_NSTATS,
252                                                 sizeof(uint64_t), 0,
253                                                 sa->socket_id);
254         if (port->mac_stats_buf == NULL)
255                 goto fail_mac_stats_buf_alloc;
256
257         rc = sfc_dma_alloc(sa, "mac_stats", 0, EFX_MAC_STATS_SIZE,
258                            sa->socket_id, &port->mac_stats_dma_mem);
259         if (rc != 0)
260                 goto fail_mac_stats_dma_alloc;
261
262         port->mac_stats_reset_pending = B_FALSE;
263
264         sfc_log_init(sa, "done");
265         return 0;
266
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);
271         return rc;
272 }
273
274 void
275 sfc_port_fini(struct sfc_adapter *sa)
276 {
277         struct sfc_port *port = &sa->port;
278
279         sfc_log_init(sa, "entry");
280
281         sfc_dma_free(sa, &port->mac_stats_dma_mem);
282         rte_free(port->mac_stats_buf);
283
284         sfc_log_init(sa, "done");
285 }
286
287 int
288 sfc_set_rx_mode(struct sfc_adapter *sa)
289 {
290         struct sfc_port *port = &sa->port;
291         int rc;
292
293         rc = efx_mac_filter_set(sa->nic, port->promisc, B_TRUE,
294                                 port->promisc || port->allmulti, B_TRUE);
295
296         return rc;
297 }
298
299 void
300 sfc_port_link_mode_to_info(efx_link_mode_t link_mode,
301                            struct rte_eth_link *link_info)
302 {
303         SFC_ASSERT(link_mode < EFX_LINK_NMODES);
304
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;
308         else
309                 link_info->link_status = ETH_LINK_UP;
310
311         switch (link_mode) {
312         case EFX_LINK_10HDX:
313                 link_info->link_speed  = ETH_SPEED_NUM_10M;
314                 link_info->link_duplex = ETH_LINK_HALF_DUPLEX;
315                 break;
316         case EFX_LINK_10FDX:
317                 link_info->link_speed  = ETH_SPEED_NUM_10M;
318                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
319                 break;
320         case EFX_LINK_100HDX:
321                 link_info->link_speed  = ETH_SPEED_NUM_100M;
322                 link_info->link_duplex = ETH_LINK_HALF_DUPLEX;
323                 break;
324         case EFX_LINK_100FDX:
325                 link_info->link_speed  = ETH_SPEED_NUM_100M;
326                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
327                 break;
328         case EFX_LINK_1000HDX:
329                 link_info->link_speed  = ETH_SPEED_NUM_1G;
330                 link_info->link_duplex = ETH_LINK_HALF_DUPLEX;
331                 break;
332         case EFX_LINK_1000FDX:
333                 link_info->link_speed  = ETH_SPEED_NUM_1G;
334                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
335                 break;
336         case EFX_LINK_10000FDX:
337                 link_info->link_speed  = ETH_SPEED_NUM_10G;
338                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
339                 break;
340         case EFX_LINK_40000FDX:
341                 link_info->link_speed  = ETH_SPEED_NUM_40G;
342                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
343                 break;
344         default:
345                 SFC_ASSERT(B_FALSE);
346                 /* FALLTHROUGH */
347         case EFX_LINK_UNKNOWN:
348         case EFX_LINK_DOWN:
349                 link_info->link_speed  = ETH_SPEED_NUM_NONE;
350                 link_info->link_duplex = 0;
351                 break;
352         }
353
354         link_info->link_autoneg = ETH_LINK_AUTONEG;
355 }