net/sfc: support link speed and duplex settings
[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_start(struct sfc_adapter *sa)
66 {
67         struct sfc_port *port = &sa->port;
68         int rc;
69
70         sfc_log_init(sa, "entry");
71
72         sfc_log_init(sa, "init filters");
73         rc = efx_filter_init(sa->nic);
74         if (rc != 0)
75                 goto fail_filter_init;
76
77         sfc_log_init(sa, "init port");
78         rc = efx_port_init(sa->nic);
79         if (rc != 0)
80                 goto fail_port_init;
81
82         sfc_log_init(sa, "set flow control to %#x autoneg=%u",
83                      port->flow_ctrl, port->flow_ctrl_autoneg);
84         rc = efx_mac_fcntl_set(sa->nic, port->flow_ctrl,
85                                port->flow_ctrl_autoneg);
86         if (rc != 0)
87                 goto fail_mac_fcntl_set;
88
89         sfc_log_init(sa, "set phy adv caps to %#x", port->phy_adv_cap);
90         rc = efx_phy_adv_cap_set(sa->nic, port->phy_adv_cap);
91         if (rc != 0)
92                 goto fail_phy_adv_cap_set;
93
94         sfc_log_init(sa, "set MAC PDU %u", (unsigned int)port->pdu);
95         rc = efx_mac_pdu_set(sa->nic, port->pdu);
96         if (rc != 0)
97                 goto fail_mac_pdu_set;
98
99         sfc_log_init(sa, "set MAC address");
100         rc = efx_mac_addr_set(sa->nic,
101                               sa->eth_dev->data->mac_addrs[0].addr_bytes);
102         if (rc != 0)
103                 goto fail_mac_addr_set;
104
105         sfc_log_init(sa, "set MAC filters");
106         rc = efx_mac_filter_set(sa->nic, B_TRUE, B_TRUE, B_TRUE, B_TRUE);
107         if (rc != 0)
108                 goto fail_mac_filter_set;
109
110         efx_mac_stats_get_mask(sa->nic, port->mac_stats_mask,
111                                sizeof(port->mac_stats_mask));
112
113         /* Update MAC stats using periodic DMA.
114          * Common code always uses 1000ms update period, so period_ms
115          * parameter only needs to be non-zero to start updates.
116          */
117         sfc_log_init(sa, "request MAC stats DMA'ing");
118         rc = efx_mac_stats_periodic(sa->nic, &port->mac_stats_dma_mem,
119                                     1000, B_FALSE);
120         if (rc != 0)
121                 goto fail_mac_stats_periodic;
122
123         sfc_log_init(sa, "disable MAC drain");
124         rc = efx_mac_drain(sa->nic, B_FALSE);
125         if (rc != 0)
126                 goto fail_mac_drain;
127
128         sfc_log_init(sa, "done");
129         return 0;
130
131 fail_mac_drain:
132         (void)efx_mac_stats_periodic(sa->nic, &port->mac_stats_dma_mem,
133                                      0, B_FALSE);
134
135 fail_mac_stats_periodic:
136 fail_mac_filter_set:
137 fail_mac_addr_set:
138 fail_mac_pdu_set:
139 fail_phy_adv_cap_set:
140 fail_mac_fcntl_set:
141         efx_port_fini(sa->nic);
142
143 fail_port_init:
144         efx_filter_fini(sa->nic);
145
146 fail_filter_init:
147         sfc_log_init(sa, "failed %d", rc);
148         return rc;
149 }
150
151 void
152 sfc_port_stop(struct sfc_adapter *sa)
153 {
154         sfc_log_init(sa, "entry");
155
156         efx_mac_drain(sa->nic, B_TRUE);
157
158         (void)efx_mac_stats_periodic(sa->nic, &sa->port.mac_stats_dma_mem,
159                                      0, B_FALSE);
160
161         efx_port_fini(sa->nic);
162         efx_filter_fini(sa->nic);
163
164         sfc_log_init(sa, "done");
165 }
166
167 int
168 sfc_port_init(struct sfc_adapter *sa)
169 {
170         const struct rte_eth_dev_data *dev_data = sa->eth_dev->data;
171         struct sfc_port *port = &sa->port;
172         int rc;
173
174         sfc_log_init(sa, "entry");
175
176         /* Enable flow control by default */
177         port->flow_ctrl = EFX_FCNTL_RESPOND | EFX_FCNTL_GENERATE;
178         port->flow_ctrl_autoneg = B_TRUE;
179
180         if (dev_data->dev_conf.rxmode.jumbo_frame)
181                 port->pdu = dev_data->dev_conf.rxmode.max_rx_pkt_len;
182         else
183                 port->pdu = EFX_MAC_PDU(dev_data->mtu);
184
185         rte_spinlock_init(&port->mac_stats_lock);
186
187         rc = ENOMEM;
188         port->mac_stats_buf = rte_calloc_socket("mac_stats_buf", EFX_MAC_NSTATS,
189                                                 sizeof(uint64_t), 0,
190                                                 sa->socket_id);
191         if (port->mac_stats_buf == NULL)
192                 goto fail_mac_stats_buf_alloc;
193
194         rc = sfc_dma_alloc(sa, "mac_stats", 0, EFX_MAC_STATS_SIZE,
195                            sa->socket_id, &port->mac_stats_dma_mem);
196         if (rc != 0)
197                 goto fail_mac_stats_dma_alloc;
198
199         sfc_log_init(sa, "done");
200         return 0;
201
202 fail_mac_stats_dma_alloc:
203         rte_free(port->mac_stats_buf);
204 fail_mac_stats_buf_alloc:
205         sfc_log_init(sa, "failed %d", rc);
206         return rc;
207 }
208
209 void
210 sfc_port_fini(struct sfc_adapter *sa)
211 {
212         struct sfc_port *port = &sa->port;
213
214         sfc_log_init(sa, "entry");
215
216         sfc_dma_free(sa, &port->mac_stats_dma_mem);
217         rte_free(port->mac_stats_buf);
218
219         sfc_log_init(sa, "done");
220 }
221
222 void
223 sfc_port_link_mode_to_info(efx_link_mode_t link_mode,
224                            struct rte_eth_link *link_info)
225 {
226         SFC_ASSERT(link_mode < EFX_LINK_NMODES);
227
228         memset(link_info, 0, sizeof(*link_info));
229         if ((link_mode == EFX_LINK_DOWN) || (link_mode == EFX_LINK_UNKNOWN))
230                 link_info->link_status = ETH_LINK_DOWN;
231         else
232                 link_info->link_status = ETH_LINK_UP;
233
234         switch (link_mode) {
235         case EFX_LINK_10HDX:
236                 link_info->link_speed  = ETH_SPEED_NUM_10M;
237                 link_info->link_duplex = ETH_LINK_HALF_DUPLEX;
238                 break;
239         case EFX_LINK_10FDX:
240                 link_info->link_speed  = ETH_SPEED_NUM_10M;
241                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
242                 break;
243         case EFX_LINK_100HDX:
244                 link_info->link_speed  = ETH_SPEED_NUM_100M;
245                 link_info->link_duplex = ETH_LINK_HALF_DUPLEX;
246                 break;
247         case EFX_LINK_100FDX:
248                 link_info->link_speed  = ETH_SPEED_NUM_100M;
249                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
250                 break;
251         case EFX_LINK_1000HDX:
252                 link_info->link_speed  = ETH_SPEED_NUM_1G;
253                 link_info->link_duplex = ETH_LINK_HALF_DUPLEX;
254                 break;
255         case EFX_LINK_1000FDX:
256                 link_info->link_speed  = ETH_SPEED_NUM_1G;
257                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
258                 break;
259         case EFX_LINK_10000FDX:
260                 link_info->link_speed  = ETH_SPEED_NUM_10G;
261                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
262                 break;
263         case EFX_LINK_40000FDX:
264                 link_info->link_speed  = ETH_SPEED_NUM_40G;
265                 link_info->link_duplex = ETH_LINK_FULL_DUPLEX;
266                 break;
267         default:
268                 SFC_ASSERT(B_FALSE);
269                 /* FALLTHROUGH */
270         case EFX_LINK_UNKNOWN:
271         case EFX_LINK_DOWN:
272                 link_info->link_speed  = ETH_SPEED_NUM_NONE;
273                 link_info->link_duplex = 0;
274                 break;
275         }
276
277         link_info->link_autoneg = ETH_LINK_AUTONEG;
278 }