Imported Upstream version 17.05.2
[deb_dpdk.git] / drivers / net / sfc / sfc_port.c
1 /*-
2  *   BSD LICENSE
3  *
4  * Copyright (c) 2016-2017 Solarflare Communications Inc.
5  * All rights reserved.
6  *
7  * This software was jointly developed between OKTET Labs (under contract
8  * for Solarflare) and Solarflare Communications, Inc.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright notice,
14  *    this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright notice,
16  *    this list of conditions and the following disclaimer in the documentation
17  *    and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
29  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31
32 #include "efx.h"
33
34 #include "sfc.h"
35 #include "sfc_log.h"
36 #include "sfc_kvargs.h"
37
38 /** Default MAC statistics update period is 1 second */
39 #define SFC_MAC_STATS_UPDATE_PERIOD_MS_DEF      MS_PER_S
40
41 /** The number of microseconds to sleep on attempt to get statistics update */
42 #define SFC_MAC_STATS_UPDATE_RETRY_INTERVAL_US  10
43
44 /** The number of attempts to await arrival of freshly generated statistics */
45 #define SFC_MAC_STATS_UPDATE_NB_ATTEMPTS        50
46
47 /**
48  * Update MAC statistics in the buffer.
49  *
50  * @param       sa      Adapter
51  *
52  * @return Status code
53  * @retval      0       Success
54  * @retval      EAGAIN  Try again
55  * @retval      ENOMEM  Memory allocation failure
56  */
57 int
58 sfc_port_update_mac_stats(struct sfc_adapter *sa)
59 {
60         struct sfc_port *port = &sa->port;
61         efsys_mem_t *esmp = &port->mac_stats_dma_mem;
62         uint32_t *genp = NULL;
63         uint32_t gen_old;
64         unsigned int nb_attempts = 0;
65         int rc;
66
67         SFC_ASSERT(rte_spinlock_is_locked(&port->mac_stats_lock));
68
69         if (sa->state != SFC_ADAPTER_STARTED)
70                 return EINVAL;
71
72         /*
73          * If periodic statistics DMA'ing is off or if not supported,
74          * make a manual request and keep an eye on timer if need be
75          */
76         if (!port->mac_stats_periodic_dma_supported ||
77             (port->mac_stats_update_period_ms == 0)) {
78                 if (port->mac_stats_update_period_ms != 0) {
79                         uint64_t timestamp = sfc_get_system_msecs();
80
81                         if ((timestamp -
82                              port->mac_stats_last_request_timestamp) <
83                             port->mac_stats_update_period_ms)
84                                 return 0;
85
86                         port->mac_stats_last_request_timestamp = timestamp;
87                 }
88
89                 rc = efx_mac_stats_upload(sa->nic, esmp);
90                 if (rc != 0)
91                         return rc;
92
93                 genp = &port->mac_stats_update_generation;
94                 gen_old = *genp;
95         }
96
97         do {
98                 if (nb_attempts > 0)
99                         rte_delay_us(SFC_MAC_STATS_UPDATE_RETRY_INTERVAL_US);
100
101                 rc = efx_mac_stats_update(sa->nic, esmp,
102                                           port->mac_stats_buf, genp);
103                 if (rc != 0)
104                         return rc;
105
106         } while ((genp != NULL) && (*genp == gen_old) &&
107                  (++nb_attempts < SFC_MAC_STATS_UPDATE_NB_ATTEMPTS));
108
109         return 0;
110 }
111
112 int
113 sfc_port_reset_mac_stats(struct sfc_adapter *sa)
114 {
115         struct sfc_port *port = &sa->port;
116         int rc;
117
118         rte_spinlock_lock(&port->mac_stats_lock);
119         rc = efx_mac_stats_clear(sa->nic);
120         rte_spinlock_unlock(&port->mac_stats_lock);
121
122         return rc;
123 }
124
125 static int
126 sfc_port_init_dev_link(struct sfc_adapter *sa)
127 {
128         struct rte_eth_link *dev_link = &sa->eth_dev->data->dev_link;
129         int rc;
130         efx_link_mode_t link_mode;
131         struct rte_eth_link current_link;
132
133         rc = efx_port_poll(sa->nic, &link_mode);
134         if (rc != 0)
135                 return rc;
136
137         sfc_port_link_mode_to_info(link_mode, &current_link);
138
139         EFX_STATIC_ASSERT(sizeof(*dev_link) == sizeof(rte_atomic64_t));
140         rte_atomic64_set((rte_atomic64_t *)dev_link,
141                          *(uint64_t *)&current_link);
142
143         return 0;
144 }
145
146 int
147 sfc_port_start(struct sfc_adapter *sa)
148 {
149         struct sfc_port *port = &sa->port;
150         int rc;
151         uint32_t phy_adv_cap;
152         const uint32_t phy_pause_caps =
153                 ((1u << EFX_PHY_CAP_PAUSE) | (1u << EFX_PHY_CAP_ASYM));
154
155         sfc_log_init(sa, "entry");
156
157         sfc_log_init(sa, "init filters");
158         rc = efx_filter_init(sa->nic);
159         if (rc != 0)
160                 goto fail_filter_init;
161
162         sfc_log_init(sa, "init port");
163         rc = efx_port_init(sa->nic);
164         if (rc != 0)
165                 goto fail_port_init;
166
167         sfc_log_init(sa, "set flow control to %#x autoneg=%u",
168                      port->flow_ctrl, port->flow_ctrl_autoneg);
169         rc = efx_mac_fcntl_set(sa->nic, port->flow_ctrl,
170                                port->flow_ctrl_autoneg);
171         if (rc != 0)
172                 goto fail_mac_fcntl_set;
173
174         /* Preserve pause capabilities set by above efx_mac_fcntl_set()  */
175         efx_phy_adv_cap_get(sa->nic, EFX_PHY_CAP_CURRENT, &phy_adv_cap);
176         SFC_ASSERT((port->phy_adv_cap & phy_pause_caps) == 0);
177         phy_adv_cap = port->phy_adv_cap | (phy_adv_cap & phy_pause_caps);
178
179         sfc_log_init(sa, "set phy adv caps to %#x", phy_adv_cap);
180         rc = efx_phy_adv_cap_set(sa->nic, phy_adv_cap);
181         if (rc != 0)
182                 goto fail_phy_adv_cap_set;
183
184         sfc_log_init(sa, "set MAC PDU %u", (unsigned int)port->pdu);
185         rc = efx_mac_pdu_set(sa->nic, port->pdu);
186         if (rc != 0)
187                 goto fail_mac_pdu_set;
188
189         sfc_log_init(sa, "set MAC address");
190         rc = efx_mac_addr_set(sa->nic,
191                               sa->eth_dev->data->mac_addrs[0].addr_bytes);
192         if (rc != 0)
193                 goto fail_mac_addr_set;
194
195         sfc_log_init(sa, "set MAC filters");
196         port->promisc = (sa->eth_dev->data->promiscuous != 0) ?
197                         B_TRUE : B_FALSE;
198         port->allmulti = (sa->eth_dev->data->all_multicast != 0) ?
199                          B_TRUE : B_FALSE;
200         rc = sfc_set_rx_mode(sa);
201         if (rc != 0)
202                 goto fail_mac_filter_set;
203
204         sfc_log_init(sa, "set multicast address list");
205         rc = efx_mac_multicast_list_set(sa->nic, port->mcast_addrs,
206                                         port->nb_mcast_addrs);
207         if (rc != 0)
208                 goto fail_mcast_address_list_set;
209
210         if (port->mac_stats_reset_pending) {
211                 rc = sfc_port_reset_mac_stats(sa);
212                 if (rc != 0)
213                         sfc_err(sa, "statistics reset failed (requested "
214                                     "before the port was started)");
215
216                 port->mac_stats_reset_pending = B_FALSE;
217         }
218
219         efx_mac_stats_get_mask(sa->nic, port->mac_stats_mask,
220                                sizeof(port->mac_stats_mask));
221
222         port->mac_stats_update_generation = 0;
223
224         if (port->mac_stats_update_period_ms != 0) {
225                 /*
226                  * Update MAC stats using periodic DMA;
227                  * any positive update interval different from
228                  * 1000 ms can be set only on SFN8xxx provided
229                  * that FW version is 6.2.1.1033 or higher
230                  */
231                 sfc_log_init(sa, "request MAC stats DMA'ing");
232                 rc = efx_mac_stats_periodic(sa->nic, &port->mac_stats_dma_mem,
233                                             port->mac_stats_update_period_ms,
234                                             B_FALSE);
235                 if (rc == 0) {
236                         port->mac_stats_periodic_dma_supported = B_TRUE;
237                 } else if (rc == EOPNOTSUPP) {
238                         port->mac_stats_periodic_dma_supported = B_FALSE;
239                         port->mac_stats_last_request_timestamp = 0;
240                 } else {
241                         goto fail_mac_stats_periodic;
242                 }
243         }
244
245         if ((port->mac_stats_update_period_ms != 0) &&
246             port->mac_stats_periodic_dma_supported) {
247                 /*
248                  * Request an explicit MAC stats upload immediately to
249                  * preclude bogus figures readback if the user decides
250                  * to read stats before periodic DMA is really started
251                  */
252                 rc = efx_mac_stats_upload(sa->nic, &port->mac_stats_dma_mem);
253                 if (rc != 0)
254                         goto fail_mac_stats_upload;
255         }
256
257         sfc_log_init(sa, "disable MAC drain");
258         rc = efx_mac_drain(sa->nic, B_FALSE);
259         if (rc != 0)
260                 goto fail_mac_drain;
261
262         /* Synchronize link status knowledge */
263         rc = sfc_port_init_dev_link(sa);
264         if (rc != 0)
265                 goto fail_port_init_dev_link;
266
267         sfc_log_init(sa, "done");
268         return 0;
269
270 fail_port_init_dev_link:
271         (void)efx_mac_drain(sa->nic, B_TRUE);
272
273 fail_mac_drain:
274         (void)efx_mac_stats_periodic(sa->nic, &port->mac_stats_dma_mem,
275                                      0, B_FALSE);
276
277 fail_mac_stats_upload:
278 fail_mac_stats_periodic:
279 fail_mcast_address_list_set:
280 fail_mac_filter_set:
281 fail_mac_addr_set:
282 fail_mac_pdu_set:
283 fail_phy_adv_cap_set:
284 fail_mac_fcntl_set:
285         efx_port_fini(sa->nic);
286
287 fail_port_init:
288         efx_filter_fini(sa->nic);
289
290 fail_filter_init:
291         sfc_log_init(sa, "failed %d", rc);
292         return rc;
293 }
294
295 void
296 sfc_port_stop(struct sfc_adapter *sa)
297 {
298         sfc_log_init(sa, "entry");
299
300         efx_mac_drain(sa->nic, B_TRUE);
301
302         (void)efx_mac_stats_periodic(sa->nic, &sa->port.mac_stats_dma_mem,
303                                      0, B_FALSE);
304
305         efx_port_fini(sa->nic);
306         efx_filter_fini(sa->nic);
307
308         sfc_log_init(sa, "done");
309 }
310
311 int
312 sfc_port_configure(struct sfc_adapter *sa)
313 {
314         const struct rte_eth_dev_data *dev_data = sa->eth_dev->data;
315         struct sfc_port *port = &sa->port;
316
317         sfc_log_init(sa, "entry");
318
319         if (dev_data->dev_conf.rxmode.jumbo_frame)
320                 port->pdu = dev_data->dev_conf.rxmode.max_rx_pkt_len;
321         else
322                 port->pdu = EFX_MAC_PDU(dev_data->mtu);
323
324         return 0;
325 }
326
327 void
328 sfc_port_close(struct sfc_adapter *sa)
329 {
330         sfc_log_init(sa, "entry");
331 }
332
333 int
334 sfc_port_attach(struct sfc_adapter *sa)
335 {
336         struct sfc_port *port = &sa->port;
337         long kvarg_stats_update_period_ms;
338         int rc;
339
340         sfc_log_init(sa, "entry");
341
342         efx_phy_adv_cap_get(sa->nic, EFX_PHY_CAP_PERM, &port->phy_adv_cap_mask);
343
344         /* Enable flow control by default */
345         port->flow_ctrl = EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE;
346         port->flow_ctrl_autoneg = B_TRUE;
347
348         port->max_mcast_addrs = EFX_MAC_MULTICAST_LIST_MAX;
349         port->nb_mcast_addrs = 0;
350         port->mcast_addrs = rte_calloc_socket("mcast_addr_list_buf",
351                                               port->max_mcast_addrs,
352                                               EFX_MAC_ADDR_LEN, 0,
353                                               sa->socket_id);
354         if (port->mcast_addrs == NULL) {
355                 rc = ENOMEM;
356                 goto fail_mcast_addr_list_buf_alloc;
357         }
358
359         rte_spinlock_init(&port->mac_stats_lock);
360
361         rc = ENOMEM;
362         port->mac_stats_buf = rte_calloc_socket("mac_stats_buf", EFX_MAC_NSTATS,
363                                                 sizeof(uint64_t), 0,
364                                                 sa->socket_id);
365         if (port->mac_stats_buf == NULL)
366                 goto fail_mac_stats_buf_alloc;
367
368         rc = sfc_dma_alloc(sa, "mac_stats", 0, EFX_MAC_STATS_SIZE,
369                            sa->socket_id, &port->mac_stats_dma_mem);
370         if (rc != 0)
371                 goto fail_mac_stats_dma_alloc;
372
373         port->mac_stats_reset_pending = B_FALSE;
374
375         kvarg_stats_update_period_ms = SFC_MAC_STATS_UPDATE_PERIOD_MS_DEF;
376
377         rc = sfc_kvargs_process(sa, SFC_KVARG_STATS_UPDATE_PERIOD_MS,
378                                 sfc_kvarg_long_handler,
379                                 &kvarg_stats_update_period_ms);
380         if ((rc == 0) &&
381             ((kvarg_stats_update_period_ms < 0) ||
382              (kvarg_stats_update_period_ms > UINT16_MAX))) {
383                 sfc_err(sa, "wrong '" SFC_KVARG_STATS_UPDATE_PERIOD_MS "' "
384                             "was set (%ld);", kvarg_stats_update_period_ms);
385                 sfc_err(sa, "it must not be less than 0 "
386                             "or greater than %" PRIu16, UINT16_MAX);
387                 rc = EINVAL;
388                 goto fail_kvarg_stats_update_period_ms;
389         } else if (rc != 0) {
390                 goto fail_kvarg_stats_update_period_ms;
391         }
392
393         port->mac_stats_update_period_ms = kvarg_stats_update_period_ms;
394
395         sfc_log_init(sa, "done");
396         return 0;
397
398 fail_kvarg_stats_update_period_ms:
399 fail_mac_stats_dma_alloc:
400         rte_free(port->mac_stats_buf);
401 fail_mac_stats_buf_alloc:
402 fail_mcast_addr_list_buf_alloc:
403         sfc_log_init(sa, "failed %d", rc);
404         return rc;
405 }
406
407 void
408 sfc_port_detach(struct sfc_adapter *sa)
409 {
410         struct sfc_port *port = &sa->port;
411
412         sfc_log_init(sa, "entry");
413
414         sfc_dma_free(sa, &port->mac_stats_dma_mem);
415         rte_free(port->mac_stats_buf);
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 }