net/sfc/base: import SFN7xxx family support
[dpdk.git] / drivers / net / sfc / base / ef10_mac.c
1 /*
2  * Copyright (c) 2012-2016 Solarflare Communications Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice,
9  *    this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright notice,
11  *    this list of conditions and the following disclaimer in the documentation
12  *    and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
15  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
16  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
24  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  *
26  * The views and conclusions contained in the software and documentation are
27  * those of the authors and should not be interpreted as representing official
28  * policies, either expressed or implied, of the FreeBSD Project.
29  */
30
31 #include "efx.h"
32 #include "efx_impl.h"
33
34
35 #if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD
36
37         __checkReturn   efx_rc_t
38 ef10_mac_poll(
39         __in            efx_nic_t *enp,
40         __out           efx_link_mode_t *link_modep)
41 {
42         efx_port_t *epp = &(enp->en_port);
43         ef10_link_state_t els;
44         efx_rc_t rc;
45
46         if ((rc = ef10_phy_get_link(enp, &els)) != 0)
47                 goto fail1;
48
49         epp->ep_adv_cap_mask = els.els_adv_cap_mask;
50         epp->ep_fcntl = els.els_fcntl;
51
52         *link_modep = els.els_link_mode;
53
54         return (0);
55
56 fail1:
57         EFSYS_PROBE1(fail1, efx_rc_t, rc);
58
59         *link_modep = EFX_LINK_UNKNOWN;
60
61         return (rc);
62 }
63
64         __checkReturn   efx_rc_t
65 ef10_mac_up(
66         __in            efx_nic_t *enp,
67         __out           boolean_t *mac_upp)
68 {
69         ef10_link_state_t els;
70         efx_rc_t rc;
71
72         /*
73          * Because EF10 doesn't *require* polling, we can't rely on
74          * ef10_mac_poll() being executed to populate epp->ep_mac_up.
75          */
76         if ((rc = ef10_phy_get_link(enp, &els)) != 0)
77                 goto fail1;
78
79         *mac_upp = els.els_mac_up;
80
81         return (0);
82
83 fail1:
84         EFSYS_PROBE1(fail1, efx_rc_t, rc);
85
86         return (rc);
87 }
88
89 /*
90  * EF10 adapters use MC_CMD_VADAPTOR_SET_MAC to set the
91  * MAC address; the address field in MC_CMD_SET_MAC has no
92  * effect.
93  * MC_CMD_VADAPTOR_SET_MAC requires mac-spoofing privilege and
94  * the port to have no filters or queues active.
95  */
96 static  __checkReturn   efx_rc_t
97 efx_mcdi_vadapter_set_mac(
98         __in            efx_nic_t *enp)
99 {
100         efx_port_t *epp = &(enp->en_port);
101         efx_mcdi_req_t req;
102         uint8_t payload[MAX(MC_CMD_VADAPTOR_SET_MAC_IN_LEN,
103                             MC_CMD_VADAPTOR_SET_MAC_OUT_LEN)];
104         efx_rc_t rc;
105
106         (void) memset(payload, 0, sizeof (payload));
107         req.emr_cmd = MC_CMD_VADAPTOR_SET_MAC;
108         req.emr_in_buf = payload;
109         req.emr_in_length = MC_CMD_VADAPTOR_SET_MAC_IN_LEN;
110         req.emr_out_buf = payload;
111         req.emr_out_length = MC_CMD_VADAPTOR_SET_MAC_OUT_LEN;
112
113         MCDI_IN_SET_DWORD(req, VADAPTOR_SET_MAC_IN_UPSTREAM_PORT_ID,
114             enp->en_vport_id);
115         EFX_MAC_ADDR_COPY(MCDI_IN2(req, uint8_t, VADAPTOR_SET_MAC_IN_MACADDR),
116             epp->ep_mac_addr);
117
118         efx_mcdi_execute(enp, &req);
119
120         if (req.emr_rc != 0) {
121                 rc = req.emr_rc;
122                 goto fail1;
123         }
124
125         return (0);
126
127 fail1:
128         EFSYS_PROBE1(fail1, efx_rc_t, rc);
129
130         return (rc);
131 }
132
133         __checkReturn   efx_rc_t
134 ef10_mac_addr_set(
135         __in            efx_nic_t *enp)
136 {
137         efx_rc_t rc;
138
139         if ((rc = efx_mcdi_vadapter_set_mac(enp)) != 0) {
140                 if (rc != ENOTSUP)
141                         goto fail1;
142
143                 /*
144                  * Fallback for older Huntington firmware without Vadapter
145                  * support.
146                  */
147                 if ((rc = ef10_mac_reconfigure(enp)) != 0)
148                         goto fail2;
149         }
150
151         return (0);
152
153 fail2:
154         EFSYS_PROBE(fail2);
155
156 fail1:
157         EFSYS_PROBE1(fail1, efx_rc_t, rc);
158
159         return (rc);
160 }
161
162 static  __checkReturn   efx_rc_t
163 efx_mcdi_mtu_set(
164         __in            efx_nic_t *enp,
165         __in            uint32_t mtu)
166 {
167         efx_mcdi_req_t req;
168         uint8_t payload[MAX(MC_CMD_SET_MAC_EXT_IN_LEN,
169                             MC_CMD_SET_MAC_OUT_LEN)];
170         efx_rc_t rc;
171
172         (void) memset(payload, 0, sizeof (payload));
173         req.emr_cmd = MC_CMD_SET_MAC;
174         req.emr_in_buf = payload;
175         req.emr_in_length = MC_CMD_SET_MAC_EXT_IN_LEN;
176         req.emr_out_buf = payload;
177         req.emr_out_length = MC_CMD_SET_MAC_OUT_LEN;
178
179         /* Only configure the MTU in this call to MC_CMD_SET_MAC */
180         MCDI_IN_SET_DWORD(req, SET_MAC_EXT_IN_MTU, mtu);
181         MCDI_IN_POPULATE_DWORD_1(req, SET_MAC_EXT_IN_CONTROL,
182                             SET_MAC_EXT_IN_CFG_MTU, 1);
183
184         efx_mcdi_execute(enp, &req);
185
186         if (req.emr_rc != 0) {
187                 rc = req.emr_rc;
188                 goto fail1;
189         }
190
191         return (0);
192
193 fail1:
194         EFSYS_PROBE1(fail1, efx_rc_t, rc);
195
196         return (rc);
197 }
198
199 static  __checkReturn           efx_rc_t
200 efx_mcdi_mtu_get(
201         __in            efx_nic_t *enp,
202         __out           size_t *mtu)
203 {
204         efx_mcdi_req_t req;
205         uint8_t payload[MAX(MC_CMD_SET_MAC_EXT_IN_LEN,
206                             MC_CMD_SET_MAC_V2_OUT_LEN)];
207         efx_rc_t rc;
208
209         (void) memset(payload, 0, sizeof (payload));
210         req.emr_cmd = MC_CMD_SET_MAC;
211         req.emr_in_buf = payload;
212         req.emr_in_length = MC_CMD_SET_MAC_EXT_IN_LEN;
213         req.emr_out_buf = payload;
214         req.emr_out_length = MC_CMD_SET_MAC_V2_OUT_LEN;
215
216         /*
217          * With MC_CMD_SET_MAC_EXT_IN_CONTROL set to 0, this just queries the
218          * MTU.  This should always be supported on Medford, but it is not
219          * supported on older Huntington firmware.
220          */
221         MCDI_IN_SET_DWORD(req, SET_MAC_EXT_IN_CONTROL, 0);
222
223         efx_mcdi_execute(enp, &req);
224
225         if (req.emr_rc != 0) {
226                 rc = req.emr_rc;
227                 goto fail1;
228         }
229         if (req.emr_out_length_used < MC_CMD_SET_MAC_V2_OUT_MTU_OFST + 4) {
230                 rc = EMSGSIZE;
231                 goto fail2;
232         }
233
234         *mtu = MCDI_OUT_DWORD(req, SET_MAC_V2_OUT_MTU);
235
236         return (0);
237
238 fail2:
239         EFSYS_PROBE(fail2);
240 fail1:
241         EFSYS_PROBE1(fail1, efx_rc_t, rc);
242
243         return (rc);
244 }
245
246         __checkReturn   efx_rc_t
247 ef10_mac_pdu_set(
248         __in            efx_nic_t *enp)
249 {
250         efx_port_t *epp = &(enp->en_port);
251         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
252         efx_rc_t rc;
253
254         if (encp->enc_enhanced_set_mac_supported) {
255                 if ((rc = efx_mcdi_mtu_set(enp, epp->ep_mac_pdu)) != 0)
256                         goto fail1;
257         } else {
258                 /*
259                  * Fallback for older Huntington firmware, which always
260                  * configure all of the parameters to MC_CMD_SET_MAC. This isn't
261                  * suitable for setting the MTU on unpriviliged functions.
262                  */
263                 if ((rc = ef10_mac_reconfigure(enp)) != 0)
264                         goto fail2;
265         }
266
267         return (0);
268
269 fail2:
270         EFSYS_PROBE(fail2);
271 fail1:
272         EFSYS_PROBE1(fail1, efx_rc_t, rc);
273
274         return (rc);
275 }
276
277         __checkReturn           efx_rc_t
278 ef10_mac_pdu_get(
279         __in            efx_nic_t *enp,
280         __out           size_t *pdu)
281 {
282         efx_rc_t rc;
283
284         if ((rc = efx_mcdi_mtu_get(enp, pdu)) != 0)
285                 goto fail1;
286
287         return (0);
288
289 fail1:
290         EFSYS_PROBE1(fail1, efx_rc_t, rc);
291
292         return (rc);
293 }
294
295 __checkReturn   efx_rc_t
296 ef10_mac_reconfigure(
297         __in            efx_nic_t *enp)
298 {
299         efx_port_t *epp = &(enp->en_port);
300         efx_mcdi_req_t req;
301         uint8_t payload[MAX(MC_CMD_SET_MAC_IN_LEN,
302                             MC_CMD_SET_MAC_OUT_LEN)];
303         efx_rc_t rc;
304
305         (void) memset(payload, 0, sizeof (payload));
306         req.emr_cmd = MC_CMD_SET_MAC;
307         req.emr_in_buf = payload;
308         req.emr_in_length = MC_CMD_SET_MAC_IN_LEN;
309         req.emr_out_buf = payload;
310         req.emr_out_length = MC_CMD_SET_MAC_OUT_LEN;
311
312         MCDI_IN_SET_DWORD(req, SET_MAC_IN_MTU, epp->ep_mac_pdu);
313         MCDI_IN_SET_DWORD(req, SET_MAC_IN_DRAIN, epp->ep_mac_drain ? 1 : 0);
314         EFX_MAC_ADDR_COPY(MCDI_IN2(req, uint8_t, SET_MAC_IN_ADDR),
315                             epp->ep_mac_addr);
316
317         /*
318          * Note: The Huntington MAC does not support REJECT_BRDCST.
319          * The REJECT_UNCST flag will also prevent multicast traffic
320          * from reaching the filters. As Huntington filters drop any
321          * traffic that does not match a filter it is ok to leave the
322          * MAC running in promiscuous mode. See bug41141.
323          *
324          * FIXME: Does REJECT_UNCST behave the same way on Medford?
325          */
326         MCDI_IN_POPULATE_DWORD_2(req, SET_MAC_IN_REJECT,
327                                     SET_MAC_IN_REJECT_UNCST, 0,
328                                     SET_MAC_IN_REJECT_BRDCST, 0);
329
330         /*
331          * Flow control, whether it is auto-negotiated or not,
332          * is set via the PHY advertised capabilities.  When set to
333          * automatic the MAC will use the PHY settings to determine
334          * the flow control settings.
335          */
336         MCDI_IN_SET_DWORD(req, SET_MAC_IN_FCNTL, MC_CMD_FCNTL_AUTO);
337
338         /* Do not include the Ethernet frame checksum in RX packets */
339         MCDI_IN_POPULATE_DWORD_1(req, SET_MAC_IN_FLAGS,
340                                     SET_MAC_IN_FLAG_INCLUDE_FCS, 0);
341
342         efx_mcdi_execute_quiet(enp, &req);
343
344         if (req.emr_rc != 0) {
345                 /*
346                  * Unprivileged functions cannot control link state,
347                  * but still need to configure filters.
348                  */
349                 if (req.emr_rc != EACCES) {
350                         rc = req.emr_rc;
351                         goto fail1;
352                 }
353         }
354
355         /*
356          * Apply the filters for the MAC configuration.
357          * If the NIC isn't ready to accept filters this may
358          * return success without setting anything.
359          */
360         rc = efx_filter_reconfigure(enp, epp->ep_mac_addr,
361                                     epp->ep_all_unicst, epp->ep_mulcst,
362                                     epp->ep_all_mulcst, epp->ep_brdcst,
363                                     epp->ep_mulcst_addr_list,
364                                     epp->ep_mulcst_addr_count);
365
366         return (0);
367
368 fail1:
369         EFSYS_PROBE1(fail1, efx_rc_t, rc);
370
371         return (rc);
372 }
373
374         __checkReturn                   efx_rc_t
375 ef10_mac_multicast_list_set(
376         __in                            efx_nic_t *enp)
377 {
378         efx_port_t *epp = &(enp->en_port);
379         const efx_mac_ops_t *emop = epp->ep_emop;
380         efx_rc_t rc;
381
382         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
383                     enp->en_family == EFX_FAMILY_MEDFORD);
384
385         if ((rc = emop->emo_reconfigure(enp)) != 0)
386                 goto fail1;
387
388         return (0);
389
390 fail1:
391         EFSYS_PROBE1(fail1, efx_rc_t, rc);
392
393         return (rc);
394 }
395
396         __checkReturn   efx_rc_t
397 ef10_mac_filter_default_rxq_set(
398         __in            efx_nic_t *enp,
399         __in            efx_rxq_t *erp,
400         __in            boolean_t using_rss)
401 {
402         efx_port_t *epp = &(enp->en_port);
403         efx_rxq_t *old_rxq;
404         boolean_t old_using_rss;
405         efx_rc_t rc;
406
407         ef10_filter_get_default_rxq(enp, &old_rxq, &old_using_rss);
408
409         ef10_filter_default_rxq_set(enp, erp, using_rss);
410
411         rc = efx_filter_reconfigure(enp, epp->ep_mac_addr,
412                                     epp->ep_all_unicst, epp->ep_mulcst,
413                                     epp->ep_all_mulcst, epp->ep_brdcst,
414                                     epp->ep_mulcst_addr_list,
415                                     epp->ep_mulcst_addr_count);
416
417         if (rc != 0)
418                 goto fail1;
419
420         return (0);
421
422 fail1:
423         EFSYS_PROBE1(fail1, efx_rc_t, rc);
424
425         ef10_filter_default_rxq_set(enp, old_rxq, old_using_rss);
426
427         return (rc);
428 }
429
430                         void
431 ef10_mac_filter_default_rxq_clear(
432         __in            efx_nic_t *enp)
433 {
434         efx_port_t *epp = &(enp->en_port);
435
436         ef10_filter_default_rxq_clear(enp);
437
438         efx_filter_reconfigure(enp, epp->ep_mac_addr,
439                                     epp->ep_all_unicst, epp->ep_mulcst,
440                                     epp->ep_all_mulcst, epp->ep_brdcst,
441                                     epp->ep_mulcst_addr_list,
442                                     epp->ep_mulcst_addr_count);
443 }
444
445
446 #endif  /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD */