net/sfc/base: add API to control UDP tunnel ports
[dpdk.git] / drivers / net / sfc / base / ef10_nic.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 #if EFSYS_OPT_MON_MCDI
34 #include "mcdi_mon.h"
35 #endif
36
37 #if EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD
38
39 #include "ef10_tlv_layout.h"
40
41         __checkReturn   efx_rc_t
42 efx_mcdi_get_port_assignment(
43         __in            efx_nic_t *enp,
44         __out           uint32_t *portp)
45 {
46         efx_mcdi_req_t req;
47         uint8_t payload[MAX(MC_CMD_GET_PORT_ASSIGNMENT_IN_LEN,
48                             MC_CMD_GET_PORT_ASSIGNMENT_OUT_LEN)];
49         efx_rc_t rc;
50
51         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
52                     enp->en_family == EFX_FAMILY_MEDFORD);
53
54         (void) memset(payload, 0, sizeof (payload));
55         req.emr_cmd = MC_CMD_GET_PORT_ASSIGNMENT;
56         req.emr_in_buf = payload;
57         req.emr_in_length = MC_CMD_GET_PORT_ASSIGNMENT_IN_LEN;
58         req.emr_out_buf = payload;
59         req.emr_out_length = MC_CMD_GET_PORT_ASSIGNMENT_OUT_LEN;
60
61         efx_mcdi_execute(enp, &req);
62
63         if (req.emr_rc != 0) {
64                 rc = req.emr_rc;
65                 goto fail1;
66         }
67
68         if (req.emr_out_length_used < MC_CMD_GET_PORT_ASSIGNMENT_OUT_LEN) {
69                 rc = EMSGSIZE;
70                 goto fail2;
71         }
72
73         *portp = MCDI_OUT_DWORD(req, GET_PORT_ASSIGNMENT_OUT_PORT);
74
75         return (0);
76
77 fail2:
78         EFSYS_PROBE(fail2);
79 fail1:
80         EFSYS_PROBE1(fail1, efx_rc_t, rc);
81
82         return (rc);
83 }
84
85         __checkReturn   efx_rc_t
86 efx_mcdi_get_port_modes(
87         __in            efx_nic_t *enp,
88         __out           uint32_t *modesp,
89         __out_opt       uint32_t *current_modep)
90 {
91         efx_mcdi_req_t req;
92         uint8_t payload[MAX(MC_CMD_GET_PORT_MODES_IN_LEN,
93                             MC_CMD_GET_PORT_MODES_OUT_LEN)];
94         efx_rc_t rc;
95
96         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
97                     enp->en_family == EFX_FAMILY_MEDFORD);
98
99         (void) memset(payload, 0, sizeof (payload));
100         req.emr_cmd = MC_CMD_GET_PORT_MODES;
101         req.emr_in_buf = payload;
102         req.emr_in_length = MC_CMD_GET_PORT_MODES_IN_LEN;
103         req.emr_out_buf = payload;
104         req.emr_out_length = MC_CMD_GET_PORT_MODES_OUT_LEN;
105
106         efx_mcdi_execute(enp, &req);
107
108         if (req.emr_rc != 0) {
109                 rc = req.emr_rc;
110                 goto fail1;
111         }
112
113         /*
114          * Require only Modes and DefaultMode fields, unless the current mode
115          * was requested (CurrentMode field was added for Medford).
116          */
117         if (req.emr_out_length_used <
118             MC_CMD_GET_PORT_MODES_OUT_CURRENT_MODE_OFST) {
119                 rc = EMSGSIZE;
120                 goto fail2;
121         }
122         if ((current_modep != NULL) && (req.emr_out_length_used <
123             MC_CMD_GET_PORT_MODES_OUT_CURRENT_MODE_OFST + 4)) {
124                 rc = EMSGSIZE;
125                 goto fail3;
126         }
127
128         *modesp = MCDI_OUT_DWORD(req, GET_PORT_MODES_OUT_MODES);
129
130         if (current_modep != NULL) {
131                 *current_modep = MCDI_OUT_DWORD(req,
132                                             GET_PORT_MODES_OUT_CURRENT_MODE);
133         }
134
135         return (0);
136
137 fail3:
138         EFSYS_PROBE(fail3);
139 fail2:
140         EFSYS_PROBE(fail2);
141 fail1:
142         EFSYS_PROBE1(fail1, efx_rc_t, rc);
143
144         return (rc);
145 }
146
147         __checkReturn   efx_rc_t
148 ef10_nic_get_port_mode_bandwidth(
149         __in            uint32_t port_mode,
150         __out           uint32_t *bandwidth_mbpsp)
151 {
152         uint32_t bandwidth;
153         efx_rc_t rc;
154
155         switch (port_mode) {
156         case TLV_PORT_MODE_10G:
157                 bandwidth = 10000;
158                 break;
159         case TLV_PORT_MODE_10G_10G:
160                 bandwidth = 10000 * 2;
161                 break;
162         case TLV_PORT_MODE_10G_10G_10G_10G:
163         case TLV_PORT_MODE_10G_10G_10G_10G_Q:
164         case TLV_PORT_MODE_10G_10G_10G_10G_Q1_Q2:
165         case TLV_PORT_MODE_10G_10G_10G_10G_Q2:
166                 bandwidth = 10000 * 4;
167                 break;
168         case TLV_PORT_MODE_40G:
169                 bandwidth = 40000;
170                 break;
171         case TLV_PORT_MODE_40G_40G:
172                 bandwidth = 40000 * 2;
173                 break;
174         case TLV_PORT_MODE_40G_10G_10G:
175         case TLV_PORT_MODE_10G_10G_40G:
176                 bandwidth = 40000 + (10000 * 2);
177                 break;
178         default:
179                 rc = EINVAL;
180                 goto fail1;
181         }
182
183         *bandwidth_mbpsp = bandwidth;
184
185         return (0);
186
187 fail1:
188         EFSYS_PROBE1(fail1, efx_rc_t, rc);
189
190         return (rc);
191 }
192
193 static  __checkReturn           efx_rc_t
194 efx_mcdi_vadaptor_alloc(
195         __in                    efx_nic_t *enp,
196         __in                    uint32_t port_id)
197 {
198         efx_mcdi_req_t req;
199         uint8_t payload[MAX(MC_CMD_VADAPTOR_ALLOC_IN_LEN,
200                             MC_CMD_VADAPTOR_ALLOC_OUT_LEN)];
201         efx_rc_t rc;
202
203         EFSYS_ASSERT3U(enp->en_vport_id, ==, EVB_PORT_ID_NULL);
204
205         (void) memset(payload, 0, sizeof (payload));
206         req.emr_cmd = MC_CMD_VADAPTOR_ALLOC;
207         req.emr_in_buf = payload;
208         req.emr_in_length = MC_CMD_VADAPTOR_ALLOC_IN_LEN;
209         req.emr_out_buf = payload;
210         req.emr_out_length = MC_CMD_VADAPTOR_ALLOC_OUT_LEN;
211
212         MCDI_IN_SET_DWORD(req, VADAPTOR_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
213         MCDI_IN_POPULATE_DWORD_1(req, VADAPTOR_ALLOC_IN_FLAGS,
214             VADAPTOR_ALLOC_IN_FLAG_PERMIT_SET_MAC_WHEN_FILTERS_INSTALLED,
215             enp->en_nic_cfg.enc_allow_set_mac_with_installed_filters ? 1 : 0);
216
217         efx_mcdi_execute(enp, &req);
218
219         if (req.emr_rc != 0) {
220                 rc = req.emr_rc;
221                 goto fail1;
222         }
223
224         return (0);
225
226 fail1:
227         EFSYS_PROBE1(fail1, efx_rc_t, rc);
228
229         return (rc);
230 }
231
232 static  __checkReturn           efx_rc_t
233 efx_mcdi_vadaptor_free(
234         __in                    efx_nic_t *enp,
235         __in                    uint32_t port_id)
236 {
237         efx_mcdi_req_t req;
238         uint8_t payload[MAX(MC_CMD_VADAPTOR_FREE_IN_LEN,
239                             MC_CMD_VADAPTOR_FREE_OUT_LEN)];
240         efx_rc_t rc;
241
242         (void) memset(payload, 0, sizeof (payload));
243         req.emr_cmd = MC_CMD_VADAPTOR_FREE;
244         req.emr_in_buf = payload;
245         req.emr_in_length = MC_CMD_VADAPTOR_FREE_IN_LEN;
246         req.emr_out_buf = payload;
247         req.emr_out_length = MC_CMD_VADAPTOR_FREE_OUT_LEN;
248
249         MCDI_IN_SET_DWORD(req, VADAPTOR_FREE_IN_UPSTREAM_PORT_ID, port_id);
250
251         efx_mcdi_execute(enp, &req);
252
253         if (req.emr_rc != 0) {
254                 rc = req.emr_rc;
255                 goto fail1;
256         }
257
258         return (0);
259
260 fail1:
261         EFSYS_PROBE1(fail1, efx_rc_t, rc);
262
263         return (rc);
264 }
265
266         __checkReturn   efx_rc_t
267 efx_mcdi_get_mac_address_pf(
268         __in                    efx_nic_t *enp,
269         __out_ecount_opt(6)     uint8_t mac_addrp[6])
270 {
271         efx_mcdi_req_t req;
272         uint8_t payload[MAX(MC_CMD_GET_MAC_ADDRESSES_IN_LEN,
273                             MC_CMD_GET_MAC_ADDRESSES_OUT_LEN)];
274         efx_rc_t rc;
275
276         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
277                     enp->en_family == EFX_FAMILY_MEDFORD);
278
279         (void) memset(payload, 0, sizeof (payload));
280         req.emr_cmd = MC_CMD_GET_MAC_ADDRESSES;
281         req.emr_in_buf = payload;
282         req.emr_in_length = MC_CMD_GET_MAC_ADDRESSES_IN_LEN;
283         req.emr_out_buf = payload;
284         req.emr_out_length = MC_CMD_GET_MAC_ADDRESSES_OUT_LEN;
285
286         efx_mcdi_execute(enp, &req);
287
288         if (req.emr_rc != 0) {
289                 rc = req.emr_rc;
290                 goto fail1;
291         }
292
293         if (req.emr_out_length_used < MC_CMD_GET_MAC_ADDRESSES_OUT_LEN) {
294                 rc = EMSGSIZE;
295                 goto fail2;
296         }
297
298         if (MCDI_OUT_DWORD(req, GET_MAC_ADDRESSES_OUT_MAC_COUNT) < 1) {
299                 rc = ENOENT;
300                 goto fail3;
301         }
302
303         if (mac_addrp != NULL) {
304                 uint8_t *addrp;
305
306                 addrp = MCDI_OUT2(req, uint8_t,
307                     GET_MAC_ADDRESSES_OUT_MAC_ADDR_BASE);
308
309                 EFX_MAC_ADDR_COPY(mac_addrp, addrp);
310         }
311
312         return (0);
313
314 fail3:
315         EFSYS_PROBE(fail3);
316 fail2:
317         EFSYS_PROBE(fail2);
318 fail1:
319         EFSYS_PROBE1(fail1, efx_rc_t, rc);
320
321         return (rc);
322 }
323
324         __checkReturn   efx_rc_t
325 efx_mcdi_get_mac_address_vf(
326         __in                    efx_nic_t *enp,
327         __out_ecount_opt(6)     uint8_t mac_addrp[6])
328 {
329         efx_mcdi_req_t req;
330         uint8_t payload[MAX(MC_CMD_VPORT_GET_MAC_ADDRESSES_IN_LEN,
331                             MC_CMD_VPORT_GET_MAC_ADDRESSES_OUT_LENMAX)];
332         efx_rc_t rc;
333
334         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
335                     enp->en_family == EFX_FAMILY_MEDFORD);
336
337         (void) memset(payload, 0, sizeof (payload));
338         req.emr_cmd = MC_CMD_VPORT_GET_MAC_ADDRESSES;
339         req.emr_in_buf = payload;
340         req.emr_in_length = MC_CMD_VPORT_GET_MAC_ADDRESSES_IN_LEN;
341         req.emr_out_buf = payload;
342         req.emr_out_length = MC_CMD_VPORT_GET_MAC_ADDRESSES_OUT_LENMAX;
343
344         MCDI_IN_SET_DWORD(req, VPORT_GET_MAC_ADDRESSES_IN_VPORT_ID,
345             EVB_PORT_ID_ASSIGNED);
346
347         efx_mcdi_execute(enp, &req);
348
349         if (req.emr_rc != 0) {
350                 rc = req.emr_rc;
351                 goto fail1;
352         }
353
354         if (req.emr_out_length_used <
355             MC_CMD_VPORT_GET_MAC_ADDRESSES_OUT_LENMIN) {
356                 rc = EMSGSIZE;
357                 goto fail2;
358         }
359
360         if (MCDI_OUT_DWORD(req,
361                 VPORT_GET_MAC_ADDRESSES_OUT_MACADDR_COUNT) < 1) {
362                 rc = ENOENT;
363                 goto fail3;
364         }
365
366         if (mac_addrp != NULL) {
367                 uint8_t *addrp;
368
369                 addrp = MCDI_OUT2(req, uint8_t,
370                     VPORT_GET_MAC_ADDRESSES_OUT_MACADDR);
371
372                 EFX_MAC_ADDR_COPY(mac_addrp, addrp);
373         }
374
375         return (0);
376
377 fail3:
378         EFSYS_PROBE(fail3);
379 fail2:
380         EFSYS_PROBE(fail2);
381 fail1:
382         EFSYS_PROBE1(fail1, efx_rc_t, rc);
383
384         return (rc);
385 }
386
387         __checkReturn   efx_rc_t
388 efx_mcdi_get_clock(
389         __in            efx_nic_t *enp,
390         __out           uint32_t *sys_freqp,
391         __out           uint32_t *dpcpu_freqp)
392 {
393         efx_mcdi_req_t req;
394         uint8_t payload[MAX(MC_CMD_GET_CLOCK_IN_LEN,
395                             MC_CMD_GET_CLOCK_OUT_LEN)];
396         efx_rc_t rc;
397
398         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
399                     enp->en_family == EFX_FAMILY_MEDFORD);
400
401         (void) memset(payload, 0, sizeof (payload));
402         req.emr_cmd = MC_CMD_GET_CLOCK;
403         req.emr_in_buf = payload;
404         req.emr_in_length = MC_CMD_GET_CLOCK_IN_LEN;
405         req.emr_out_buf = payload;
406         req.emr_out_length = MC_CMD_GET_CLOCK_OUT_LEN;
407
408         efx_mcdi_execute(enp, &req);
409
410         if (req.emr_rc != 0) {
411                 rc = req.emr_rc;
412                 goto fail1;
413         }
414
415         if (req.emr_out_length_used < MC_CMD_GET_CLOCK_OUT_LEN) {
416                 rc = EMSGSIZE;
417                 goto fail2;
418         }
419
420         *sys_freqp = MCDI_OUT_DWORD(req, GET_CLOCK_OUT_SYS_FREQ);
421         if (*sys_freqp == 0) {
422                 rc = EINVAL;
423                 goto fail3;
424         }
425         *dpcpu_freqp = MCDI_OUT_DWORD(req, GET_CLOCK_OUT_DPCPU_FREQ);
426         if (*dpcpu_freqp == 0) {
427                 rc = EINVAL;
428                 goto fail4;
429         }
430
431         return (0);
432
433 fail4:
434         EFSYS_PROBE(fail4);
435 fail3:
436         EFSYS_PROBE(fail3);
437 fail2:
438         EFSYS_PROBE(fail2);
439 fail1:
440         EFSYS_PROBE1(fail1, efx_rc_t, rc);
441
442         return (rc);
443 }
444
445         __checkReturn   efx_rc_t
446 efx_mcdi_get_vector_cfg(
447         __in            efx_nic_t *enp,
448         __out_opt       uint32_t *vec_basep,
449         __out_opt       uint32_t *pf_nvecp,
450         __out_opt       uint32_t *vf_nvecp)
451 {
452         efx_mcdi_req_t req;
453         uint8_t payload[MAX(MC_CMD_GET_VECTOR_CFG_IN_LEN,
454                             MC_CMD_GET_VECTOR_CFG_OUT_LEN)];
455         efx_rc_t rc;
456
457         (void) memset(payload, 0, sizeof (payload));
458         req.emr_cmd = MC_CMD_GET_VECTOR_CFG;
459         req.emr_in_buf = payload;
460         req.emr_in_length = MC_CMD_GET_VECTOR_CFG_IN_LEN;
461         req.emr_out_buf = payload;
462         req.emr_out_length = MC_CMD_GET_VECTOR_CFG_OUT_LEN;
463
464         efx_mcdi_execute(enp, &req);
465
466         if (req.emr_rc != 0) {
467                 rc = req.emr_rc;
468                 goto fail1;
469         }
470
471         if (req.emr_out_length_used < MC_CMD_GET_VECTOR_CFG_OUT_LEN) {
472                 rc = EMSGSIZE;
473                 goto fail2;
474         }
475
476         if (vec_basep != NULL)
477                 *vec_basep = MCDI_OUT_DWORD(req, GET_VECTOR_CFG_OUT_VEC_BASE);
478         if (pf_nvecp != NULL)
479                 *pf_nvecp = MCDI_OUT_DWORD(req, GET_VECTOR_CFG_OUT_VECS_PER_PF);
480         if (vf_nvecp != NULL)
481                 *vf_nvecp = MCDI_OUT_DWORD(req, GET_VECTOR_CFG_OUT_VECS_PER_VF);
482
483         return (0);
484
485 fail2:
486         EFSYS_PROBE(fail2);
487 fail1:
488         EFSYS_PROBE1(fail1, efx_rc_t, rc);
489
490         return (rc);
491 }
492
493 static  __checkReturn   efx_rc_t
494 efx_mcdi_alloc_vis(
495         __in            efx_nic_t *enp,
496         __in            uint32_t min_vi_count,
497         __in            uint32_t max_vi_count,
498         __out           uint32_t *vi_basep,
499         __out           uint32_t *vi_countp,
500         __out           uint32_t *vi_shiftp)
501 {
502         efx_mcdi_req_t req;
503         uint8_t payload[MAX(MC_CMD_ALLOC_VIS_IN_LEN,
504                             MC_CMD_ALLOC_VIS_EXT_OUT_LEN)];
505         efx_rc_t rc;
506
507         if (vi_countp == NULL) {
508                 rc = EINVAL;
509                 goto fail1;
510         }
511
512         (void) memset(payload, 0, sizeof (payload));
513         req.emr_cmd = MC_CMD_ALLOC_VIS;
514         req.emr_in_buf = payload;
515         req.emr_in_length = MC_CMD_ALLOC_VIS_IN_LEN;
516         req.emr_out_buf = payload;
517         req.emr_out_length = MC_CMD_ALLOC_VIS_EXT_OUT_LEN;
518
519         MCDI_IN_SET_DWORD(req, ALLOC_VIS_IN_MIN_VI_COUNT, min_vi_count);
520         MCDI_IN_SET_DWORD(req, ALLOC_VIS_IN_MAX_VI_COUNT, max_vi_count);
521
522         efx_mcdi_execute(enp, &req);
523
524         if (req.emr_rc != 0) {
525                 rc = req.emr_rc;
526                 goto fail2;
527         }
528
529         if (req.emr_out_length_used < MC_CMD_ALLOC_VIS_OUT_LEN) {
530                 rc = EMSGSIZE;
531                 goto fail3;
532         }
533
534         *vi_basep = MCDI_OUT_DWORD(req, ALLOC_VIS_OUT_VI_BASE);
535         *vi_countp = MCDI_OUT_DWORD(req, ALLOC_VIS_OUT_VI_COUNT);
536
537         /* Report VI_SHIFT if available (always zero for Huntington) */
538         if (req.emr_out_length_used < MC_CMD_ALLOC_VIS_EXT_OUT_LEN)
539                 *vi_shiftp = 0;
540         else
541                 *vi_shiftp = MCDI_OUT_DWORD(req, ALLOC_VIS_EXT_OUT_VI_SHIFT);
542
543         return (0);
544
545 fail3:
546         EFSYS_PROBE(fail3);
547 fail2:
548         EFSYS_PROBE(fail2);
549 fail1:
550         EFSYS_PROBE1(fail1, efx_rc_t, rc);
551
552         return (rc);
553 }
554
555
556 static  __checkReturn   efx_rc_t
557 efx_mcdi_free_vis(
558         __in            efx_nic_t *enp)
559 {
560         efx_mcdi_req_t req;
561         efx_rc_t rc;
562
563         EFX_STATIC_ASSERT(MC_CMD_FREE_VIS_IN_LEN == 0);
564         EFX_STATIC_ASSERT(MC_CMD_FREE_VIS_OUT_LEN == 0);
565
566         req.emr_cmd = MC_CMD_FREE_VIS;
567         req.emr_in_buf = NULL;
568         req.emr_in_length = 0;
569         req.emr_out_buf = NULL;
570         req.emr_out_length = 0;
571
572         efx_mcdi_execute_quiet(enp, &req);
573
574         /* Ignore ELREADY (no allocated VIs, so nothing to free) */
575         if ((req.emr_rc != 0) && (req.emr_rc != EALREADY)) {
576                 rc = req.emr_rc;
577                 goto fail1;
578         }
579
580         return (0);
581
582 fail1:
583         EFSYS_PROBE1(fail1, efx_rc_t, rc);
584
585         return (rc);
586 }
587
588
589 static  __checkReturn   efx_rc_t
590 efx_mcdi_alloc_piobuf(
591         __in            efx_nic_t *enp,
592         __out           efx_piobuf_handle_t *handlep)
593 {
594         efx_mcdi_req_t req;
595         uint8_t payload[MAX(MC_CMD_ALLOC_PIOBUF_IN_LEN,
596                             MC_CMD_ALLOC_PIOBUF_OUT_LEN)];
597         efx_rc_t rc;
598
599         if (handlep == NULL) {
600                 rc = EINVAL;
601                 goto fail1;
602         }
603
604         (void) memset(payload, 0, sizeof (payload));
605         req.emr_cmd = MC_CMD_ALLOC_PIOBUF;
606         req.emr_in_buf = payload;
607         req.emr_in_length = MC_CMD_ALLOC_PIOBUF_IN_LEN;
608         req.emr_out_buf = payload;
609         req.emr_out_length = MC_CMD_ALLOC_PIOBUF_OUT_LEN;
610
611         efx_mcdi_execute_quiet(enp, &req);
612
613         if (req.emr_rc != 0) {
614                 rc = req.emr_rc;
615                 goto fail2;
616         }
617
618         if (req.emr_out_length_used < MC_CMD_ALLOC_PIOBUF_OUT_LEN) {
619                 rc = EMSGSIZE;
620                 goto fail3;
621         }
622
623         *handlep = MCDI_OUT_DWORD(req, ALLOC_PIOBUF_OUT_PIOBUF_HANDLE);
624
625         return (0);
626
627 fail3:
628         EFSYS_PROBE(fail3);
629 fail2:
630         EFSYS_PROBE(fail2);
631 fail1:
632         EFSYS_PROBE1(fail1, efx_rc_t, rc);
633
634         return (rc);
635 }
636
637 static  __checkReturn   efx_rc_t
638 efx_mcdi_free_piobuf(
639         __in            efx_nic_t *enp,
640         __in            efx_piobuf_handle_t handle)
641 {
642         efx_mcdi_req_t req;
643         uint8_t payload[MAX(MC_CMD_FREE_PIOBUF_IN_LEN,
644                             MC_CMD_FREE_PIOBUF_OUT_LEN)];
645         efx_rc_t rc;
646
647         (void) memset(payload, 0, sizeof (payload));
648         req.emr_cmd = MC_CMD_FREE_PIOBUF;
649         req.emr_in_buf = payload;
650         req.emr_in_length = MC_CMD_FREE_PIOBUF_IN_LEN;
651         req.emr_out_buf = payload;
652         req.emr_out_length = MC_CMD_FREE_PIOBUF_OUT_LEN;
653
654         MCDI_IN_SET_DWORD(req, FREE_PIOBUF_IN_PIOBUF_HANDLE, handle);
655
656         efx_mcdi_execute_quiet(enp, &req);
657
658         if (req.emr_rc != 0) {
659                 rc = req.emr_rc;
660                 goto fail1;
661         }
662
663         return (0);
664
665 fail1:
666         EFSYS_PROBE1(fail1, efx_rc_t, rc);
667
668         return (rc);
669 }
670
671 static  __checkReturn   efx_rc_t
672 efx_mcdi_link_piobuf(
673         __in            efx_nic_t *enp,
674         __in            uint32_t vi_index,
675         __in            efx_piobuf_handle_t handle)
676 {
677         efx_mcdi_req_t req;
678         uint8_t payload[MAX(MC_CMD_LINK_PIOBUF_IN_LEN,
679                             MC_CMD_LINK_PIOBUF_OUT_LEN)];
680         efx_rc_t rc;
681
682         (void) memset(payload, 0, sizeof (payload));
683         req.emr_cmd = MC_CMD_LINK_PIOBUF;
684         req.emr_in_buf = payload;
685         req.emr_in_length = MC_CMD_LINK_PIOBUF_IN_LEN;
686         req.emr_out_buf = payload;
687         req.emr_out_length = MC_CMD_LINK_PIOBUF_OUT_LEN;
688
689         MCDI_IN_SET_DWORD(req, LINK_PIOBUF_IN_PIOBUF_HANDLE, handle);
690         MCDI_IN_SET_DWORD(req, LINK_PIOBUF_IN_TXQ_INSTANCE, vi_index);
691
692         efx_mcdi_execute(enp, &req);
693
694         if (req.emr_rc != 0) {
695                 rc = req.emr_rc;
696                 goto fail1;
697         }
698
699         return (0);
700
701 fail1:
702         EFSYS_PROBE1(fail1, efx_rc_t, rc);
703
704         return (rc);
705 }
706
707 static  __checkReturn   efx_rc_t
708 efx_mcdi_unlink_piobuf(
709         __in            efx_nic_t *enp,
710         __in            uint32_t vi_index)
711 {
712         efx_mcdi_req_t req;
713         uint8_t payload[MAX(MC_CMD_UNLINK_PIOBUF_IN_LEN,
714                             MC_CMD_UNLINK_PIOBUF_OUT_LEN)];
715         efx_rc_t rc;
716
717         (void) memset(payload, 0, sizeof (payload));
718         req.emr_cmd = MC_CMD_UNLINK_PIOBUF;
719         req.emr_in_buf = payload;
720         req.emr_in_length = MC_CMD_UNLINK_PIOBUF_IN_LEN;
721         req.emr_out_buf = payload;
722         req.emr_out_length = MC_CMD_UNLINK_PIOBUF_OUT_LEN;
723
724         MCDI_IN_SET_DWORD(req, UNLINK_PIOBUF_IN_TXQ_INSTANCE, vi_index);
725
726         efx_mcdi_execute_quiet(enp, &req);
727
728         if (req.emr_rc != 0) {
729                 rc = req.emr_rc;
730                 goto fail1;
731         }
732
733         return (0);
734
735 fail1:
736         EFSYS_PROBE1(fail1, efx_rc_t, rc);
737
738         return (rc);
739 }
740
741 static                  void
742 ef10_nic_alloc_piobufs(
743         __in            efx_nic_t *enp,
744         __in            uint32_t max_piobuf_count)
745 {
746         efx_piobuf_handle_t *handlep;
747         unsigned int i;
748
749         EFSYS_ASSERT3U(max_piobuf_count, <=,
750             EFX_ARRAY_SIZE(enp->en_arch.ef10.ena_piobuf_handle));
751
752         enp->en_arch.ef10.ena_piobuf_count = 0;
753
754         for (i = 0; i < max_piobuf_count; i++) {
755                 handlep = &enp->en_arch.ef10.ena_piobuf_handle[i];
756
757                 if (efx_mcdi_alloc_piobuf(enp, handlep) != 0)
758                         goto fail1;
759
760                 enp->en_arch.ef10.ena_pio_alloc_map[i] = 0;
761                 enp->en_arch.ef10.ena_piobuf_count++;
762         }
763
764         return;
765
766 fail1:
767         for (i = 0; i < enp->en_arch.ef10.ena_piobuf_count; i++) {
768                 handlep = &enp->en_arch.ef10.ena_piobuf_handle[i];
769
770                 efx_mcdi_free_piobuf(enp, *handlep);
771                 *handlep = EFX_PIOBUF_HANDLE_INVALID;
772         }
773         enp->en_arch.ef10.ena_piobuf_count = 0;
774 }
775
776
777 static                  void
778 ef10_nic_free_piobufs(
779         __in            efx_nic_t *enp)
780 {
781         efx_piobuf_handle_t *handlep;
782         unsigned int i;
783
784         for (i = 0; i < enp->en_arch.ef10.ena_piobuf_count; i++) {
785                 handlep = &enp->en_arch.ef10.ena_piobuf_handle[i];
786
787                 efx_mcdi_free_piobuf(enp, *handlep);
788                 *handlep = EFX_PIOBUF_HANDLE_INVALID;
789         }
790         enp->en_arch.ef10.ena_piobuf_count = 0;
791 }
792
793 /* Sub-allocate a block from a piobuf */
794         __checkReturn   efx_rc_t
795 ef10_nic_pio_alloc(
796         __inout         efx_nic_t *enp,
797         __out           uint32_t *bufnump,
798         __out           efx_piobuf_handle_t *handlep,
799         __out           uint32_t *blknump,
800         __out           uint32_t *offsetp,
801         __out           size_t *sizep)
802 {
803         efx_nic_cfg_t *encp = &enp->en_nic_cfg;
804         efx_drv_cfg_t *edcp = &enp->en_drv_cfg;
805         uint32_t blk_per_buf;
806         uint32_t buf, blk;
807         efx_rc_t rc;
808
809         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
810                     enp->en_family == EFX_FAMILY_MEDFORD);
811         EFSYS_ASSERT(bufnump);
812         EFSYS_ASSERT(handlep);
813         EFSYS_ASSERT(blknump);
814         EFSYS_ASSERT(offsetp);
815         EFSYS_ASSERT(sizep);
816
817         if ((edcp->edc_pio_alloc_size == 0) ||
818             (enp->en_arch.ef10.ena_piobuf_count == 0)) {
819                 rc = ENOMEM;
820                 goto fail1;
821         }
822         blk_per_buf = encp->enc_piobuf_size / edcp->edc_pio_alloc_size;
823
824         for (buf = 0; buf < enp->en_arch.ef10.ena_piobuf_count; buf++) {
825                 uint32_t *map = &enp->en_arch.ef10.ena_pio_alloc_map[buf];
826
827                 if (~(*map) == 0)
828                         continue;
829
830                 EFSYS_ASSERT3U(blk_per_buf, <=, (8 * sizeof (*map)));
831                 for (blk = 0; blk < blk_per_buf; blk++) {
832                         if ((*map & (1u << blk)) == 0) {
833                                 *map |= (1u << blk);
834                                 goto done;
835                         }
836                 }
837         }
838         rc = ENOMEM;
839         goto fail2;
840
841 done:
842         *handlep = enp->en_arch.ef10.ena_piobuf_handle[buf];
843         *bufnump = buf;
844         *blknump = blk;
845         *sizep = edcp->edc_pio_alloc_size;
846         *offsetp = blk * (*sizep);
847
848         return (0);
849
850 fail2:
851         EFSYS_PROBE(fail2);
852 fail1:
853         EFSYS_PROBE1(fail1, efx_rc_t, rc);
854
855         return (rc);
856 }
857
858 /* Free a piobuf sub-allocated block */
859         __checkReturn   efx_rc_t
860 ef10_nic_pio_free(
861         __inout         efx_nic_t *enp,
862         __in            uint32_t bufnum,
863         __in            uint32_t blknum)
864 {
865         uint32_t *map;
866         efx_rc_t rc;
867
868         if ((bufnum >= enp->en_arch.ef10.ena_piobuf_count) ||
869             (blknum >= (8 * sizeof (*map)))) {
870                 rc = EINVAL;
871                 goto fail1;
872         }
873
874         map = &enp->en_arch.ef10.ena_pio_alloc_map[bufnum];
875         if ((*map & (1u << blknum)) == 0) {
876                 rc = ENOENT;
877                 goto fail2;
878         }
879         *map &= ~(1u << blknum);
880
881         return (0);
882
883 fail2:
884         EFSYS_PROBE(fail2);
885 fail1:
886         EFSYS_PROBE1(fail1, efx_rc_t, rc);
887
888         return (rc);
889 }
890
891         __checkReturn   efx_rc_t
892 ef10_nic_pio_link(
893         __inout         efx_nic_t *enp,
894         __in            uint32_t vi_index,
895         __in            efx_piobuf_handle_t handle)
896 {
897         return (efx_mcdi_link_piobuf(enp, vi_index, handle));
898 }
899
900         __checkReturn   efx_rc_t
901 ef10_nic_pio_unlink(
902         __inout         efx_nic_t *enp,
903         __in            uint32_t vi_index)
904 {
905         return (efx_mcdi_unlink_piobuf(enp, vi_index));
906 }
907
908 static  __checkReturn   efx_rc_t
909 ef10_mcdi_get_pf_count(
910         __in            efx_nic_t *enp,
911         __out           uint32_t *pf_countp)
912 {
913         efx_mcdi_req_t req;
914         uint8_t payload[MAX(MC_CMD_GET_PF_COUNT_IN_LEN,
915                             MC_CMD_GET_PF_COUNT_OUT_LEN)];
916         efx_rc_t rc;
917
918         (void) memset(payload, 0, sizeof (payload));
919         req.emr_cmd = MC_CMD_GET_PF_COUNT;
920         req.emr_in_buf = payload;
921         req.emr_in_length = MC_CMD_GET_PF_COUNT_IN_LEN;
922         req.emr_out_buf = payload;
923         req.emr_out_length = MC_CMD_GET_PF_COUNT_OUT_LEN;
924
925         efx_mcdi_execute(enp, &req);
926
927         if (req.emr_rc != 0) {
928                 rc = req.emr_rc;
929                 goto fail1;
930         }
931
932         if (req.emr_out_length_used < MC_CMD_GET_PF_COUNT_OUT_LEN) {
933                 rc = EMSGSIZE;
934                 goto fail2;
935         }
936
937         *pf_countp = *MCDI_OUT(req, uint8_t,
938                                 MC_CMD_GET_PF_COUNT_OUT_PF_COUNT_OFST);
939
940         EFSYS_ASSERT(*pf_countp != 0);
941
942         return (0);
943
944 fail2:
945         EFSYS_PROBE(fail2);
946 fail1:
947         EFSYS_PROBE1(fail1, efx_rc_t, rc);
948
949         return (rc);
950 }
951
952         __checkReturn   efx_rc_t
953 ef10_get_datapath_caps(
954         __in            efx_nic_t *enp)
955 {
956         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
957         uint32_t flags;
958         uint32_t flags2;
959         uint32_t tso2nc;
960         efx_rc_t rc;
961
962         if ((rc = efx_mcdi_get_capabilities(enp, &flags, NULL, NULL,
963                                             &flags2, &tso2nc)) != 0)
964                 goto fail1;
965
966         if ((rc = ef10_mcdi_get_pf_count(enp, &encp->enc_hw_pf_count)) != 0)
967                 goto fail1;
968
969 #define CAP_FLAG(flags1, field)         \
970         ((flags1) & (1 << (MC_CMD_GET_CAPABILITIES_V2_OUT_ ## field ## _LBN)))
971
972 #define CAP_FLAG2(flags2, field)        \
973         ((flags2) & (1 << (MC_CMD_GET_CAPABILITIES_V2_OUT_ ## field ## _LBN)))
974
975         /*
976          * Huntington RXDP firmware inserts a 0 or 14 byte prefix.
977          * We only support the 14 byte prefix here.
978          */
979         if (CAP_FLAG(flags, RX_PREFIX_LEN_14) == 0) {
980                 rc = ENOTSUP;
981                 goto fail2;
982         }
983         encp->enc_rx_prefix_size = 14;
984
985         /* Check if the firmware supports TSO */
986         encp->enc_fw_assisted_tso_enabled =
987             CAP_FLAG(flags, TX_TSO) ? B_TRUE : B_FALSE;
988
989         /* Check if the firmware supports FATSOv2 */
990         encp->enc_fw_assisted_tso_v2_enabled =
991             CAP_FLAG2(flags2, TX_TSO_V2) ? B_TRUE : B_FALSE;
992
993         /* Get the number of TSO contexts (FATSOv2) */
994         encp->enc_fw_assisted_tso_v2_n_contexts =
995                 CAP_FLAG2(flags2, TX_TSO_V2) ? tso2nc : 0;
996
997         /* Check if the firmware has vadapter/vport/vswitch support */
998         encp->enc_datapath_cap_evb =
999             CAP_FLAG(flags, EVB) ? B_TRUE : B_FALSE;
1000
1001         /* Check if the firmware supports VLAN insertion */
1002         encp->enc_hw_tx_insert_vlan_enabled =
1003             CAP_FLAG(flags, TX_VLAN_INSERTION) ? B_TRUE : B_FALSE;
1004
1005         /* Check if the firmware supports RX event batching */
1006         encp->enc_rx_batching_enabled =
1007             CAP_FLAG(flags, RX_BATCHING) ? B_TRUE : B_FALSE;
1008
1009         /*
1010          * Even if batching isn't reported as supported, we may still get
1011          * batched events (see bug61153).
1012          */
1013         encp->enc_rx_batch_max = 16;
1014
1015         /* Check if the firmware supports disabling scatter on RXQs */
1016         encp->enc_rx_disable_scatter_supported =
1017             CAP_FLAG(flags, RX_DISABLE_SCATTER) ? B_TRUE : B_FALSE;
1018
1019         /* Check if the firmware supports packed stream mode */
1020         encp->enc_rx_packed_stream_supported =
1021             CAP_FLAG(flags, RX_PACKED_STREAM) ? B_TRUE : B_FALSE;
1022
1023         /*
1024          * Check if the firmware supports configurable buffer sizes
1025          * for packed stream mode (otherwise buffer size is 1Mbyte)
1026          */
1027         encp->enc_rx_var_packed_stream_supported =
1028             CAP_FLAG(flags, RX_PACKED_STREAM_VAR_BUFFERS) ? B_TRUE : B_FALSE;
1029
1030         /* Check if the firmware supports set mac with running filters */
1031         encp->enc_allow_set_mac_with_installed_filters =
1032             CAP_FLAG(flags, VADAPTOR_PERMIT_SET_MAC_WHEN_FILTERS_INSTALLED) ?
1033             B_TRUE : B_FALSE;
1034
1035         /*
1036          * Check if firmware supports the extended MC_CMD_SET_MAC, which allows
1037          * specifying which parameters to configure.
1038          */
1039         encp->enc_enhanced_set_mac_supported =
1040                 CAP_FLAG(flags, SET_MAC_ENHANCED) ? B_TRUE : B_FALSE;
1041
1042         /*
1043          * Check if firmware supports version 2 of MC_CMD_INIT_EVQ, which allows
1044          * us to let the firmware choose the settings to use on an EVQ.
1045          */
1046         encp->enc_init_evq_v2_supported =
1047                 CAP_FLAG2(flags2, INIT_EVQ_V2) ? B_TRUE : B_FALSE;
1048
1049         /*
1050          * Check if firmware-verified NVRAM updates must be used.
1051          *
1052          * The firmware trusted installer requires all NVRAM updates to use
1053          * version 2 of MC_CMD_NVRAM_UPDATE_START (to enable verified update)
1054          * and version 2 of MC_CMD_NVRAM_UPDATE_FINISH (to verify the updated
1055          * partition and report the result).
1056          */
1057         encp->enc_nvram_update_verify_result_supported =
1058             CAP_FLAG2(flags2, NVRAM_UPDATE_REPORT_VERIFY_RESULT) ?
1059             B_TRUE : B_FALSE;
1060
1061         /*
1062          * Check if firmware provides packet memory and Rx datapath
1063          * counters.
1064          */
1065         encp->enc_pm_and_rxdp_counters =
1066             CAP_FLAG(flags, PM_AND_RXDP_COUNTERS) ? B_TRUE : B_FALSE;
1067
1068         /*
1069          * Check if the 40G MAC hardware is capable of reporting
1070          * statistics for Tx size bins.
1071          */
1072         encp->enc_mac_stats_40g_tx_size_bins =
1073             CAP_FLAG2(flags2, MAC_STATS_40G_TX_SIZE_BINS) ? B_TRUE : B_FALSE;
1074
1075         /*
1076          * Check if firmware supports VXLAN and NVGRE tunnels.
1077          * The capability indicates Geneve protocol support as well.
1078          */
1079         if (CAP_FLAG(flags, VXLAN_NVGRE)) {
1080                 encp->enc_tunnel_encapsulations_supported =
1081                     (1u << EFX_TUNNEL_PROTOCOL_VXLAN) |
1082                     (1u << EFX_TUNNEL_PROTOCOL_GENEVE) |
1083                     (1u << EFX_TUNNEL_PROTOCOL_NVGRE);
1084
1085                 EFX_STATIC_ASSERT(EFX_TUNNEL_MAXNENTRIES ==
1086                     MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_ENTRIES_MAXNUM);
1087                 encp->enc_tunnel_config_udp_entries_max =
1088                     EFX_TUNNEL_MAXNENTRIES;
1089         } else {
1090                 encp->enc_tunnel_config_udp_entries_max = 0;
1091         }
1092
1093 #undef CAP_FLAG
1094 #undef CAP_FLAG2
1095
1096         return (0);
1097
1098 fail2:
1099         EFSYS_PROBE(fail2);
1100 fail1:
1101         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1102
1103         return (rc);
1104 }
1105
1106
1107 #define EF10_LEGACY_PF_PRIVILEGE_MASK                                   \
1108         (MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN                     |       \
1109         MC_CMD_PRIVILEGE_MASK_IN_GRP_LINK                       |       \
1110         MC_CMD_PRIVILEGE_MASK_IN_GRP_ONLOAD                     |       \
1111         MC_CMD_PRIVILEGE_MASK_IN_GRP_PTP                        |       \
1112         MC_CMD_PRIVILEGE_MASK_IN_GRP_INSECURE_FILTERS           |       \
1113         MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING               |       \
1114         MC_CMD_PRIVILEGE_MASK_IN_GRP_UNICAST                    |       \
1115         MC_CMD_PRIVILEGE_MASK_IN_GRP_MULTICAST                  |       \
1116         MC_CMD_PRIVILEGE_MASK_IN_GRP_BROADCAST                  |       \
1117         MC_CMD_PRIVILEGE_MASK_IN_GRP_ALL_MULTICAST              |       \
1118         MC_CMD_PRIVILEGE_MASK_IN_GRP_PROMISCUOUS)
1119
1120 #define EF10_LEGACY_VF_PRIVILEGE_MASK   0
1121
1122
1123         __checkReturn           efx_rc_t
1124 ef10_get_privilege_mask(
1125         __in                    efx_nic_t *enp,
1126         __out                   uint32_t *maskp)
1127 {
1128         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1129         uint32_t mask;
1130         efx_rc_t rc;
1131
1132         if ((rc = efx_mcdi_privilege_mask(enp, encp->enc_pf, encp->enc_vf,
1133                                             &mask)) != 0) {
1134                 if (rc != ENOTSUP)
1135                         goto fail1;
1136
1137                 /* Fallback for old firmware without privilege mask support */
1138                 if (EFX_PCI_FUNCTION_IS_PF(encp)) {
1139                         /* Assume PF has admin privilege */
1140                         mask = EF10_LEGACY_PF_PRIVILEGE_MASK;
1141                 } else {
1142                         /* VF is always unprivileged by default */
1143                         mask = EF10_LEGACY_VF_PRIVILEGE_MASK;
1144                 }
1145         }
1146
1147         *maskp = mask;
1148
1149         return (0);
1150
1151 fail1:
1152         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1153
1154         return (rc);
1155 }
1156
1157
1158 /*
1159  * Table of mapping schemes from port number to the number of the external
1160  * connector on the board. The external numbering does not distinguish
1161  * off-board separated outputs such as from multi-headed cables.
1162  *
1163  * The count of adjacent port numbers that map to each external port
1164  * and the offset in the numbering, is determined by the chip family and
1165  * current port mode.
1166  *
1167  * For the Huntington family, the current port mode cannot be discovered,
1168  * so the mapping used is instead the last match in the table to the full
1169  * set of port modes to which the NIC can be configured. Therefore the
1170  * ordering of entries in the mapping table is significant.
1171  */
1172 static struct {
1173         efx_family_t    family;
1174         uint32_t        modes_mask;
1175         int32_t         count;
1176         int32_t         offset;
1177 }       __ef10_external_port_mappings[] = {
1178         /* Supported modes with 1 output per external port */
1179         {
1180                 EFX_FAMILY_HUNTINGTON,
1181                 (1 << TLV_PORT_MODE_10G) |
1182                 (1 << TLV_PORT_MODE_10G_10G) |
1183                 (1 << TLV_PORT_MODE_10G_10G_10G_10G),
1184                 1,
1185                 1
1186         },
1187         {
1188                 EFX_FAMILY_MEDFORD,
1189                 (1 << TLV_PORT_MODE_10G) |
1190                 (1 << TLV_PORT_MODE_10G_10G),
1191                 1,
1192                 1
1193         },
1194         /* Supported modes with 2 outputs per external port */
1195         {
1196                 EFX_FAMILY_HUNTINGTON,
1197                 (1 << TLV_PORT_MODE_40G) |
1198                 (1 << TLV_PORT_MODE_40G_40G) |
1199                 (1 << TLV_PORT_MODE_40G_10G_10G) |
1200                 (1 << TLV_PORT_MODE_10G_10G_40G),
1201                 2,
1202                 1
1203         },
1204         {
1205                 EFX_FAMILY_MEDFORD,
1206                 (1 << TLV_PORT_MODE_40G) |
1207                 (1 << TLV_PORT_MODE_40G_40G) |
1208                 (1 << TLV_PORT_MODE_40G_10G_10G) |
1209                 (1 << TLV_PORT_MODE_10G_10G_40G) |
1210                 (1 << TLV_PORT_MODE_10G_10G_10G_10G_Q1_Q2),
1211                 2,
1212                 1
1213         },
1214         /* Supported modes with 4 outputs per external port */
1215         {
1216                 EFX_FAMILY_MEDFORD,
1217                 (1 << TLV_PORT_MODE_10G_10G_10G_10G_Q) |
1218                 (1 << TLV_PORT_MODE_10G_10G_10G_10G_Q1),
1219                 4,
1220                 1,
1221         },
1222         {
1223                 EFX_FAMILY_MEDFORD,
1224                 (1 << TLV_PORT_MODE_10G_10G_10G_10G_Q2),
1225                 4,
1226                 2
1227         },
1228 };
1229
1230         __checkReturn   efx_rc_t
1231 ef10_external_port_mapping(
1232         __in            efx_nic_t *enp,
1233         __in            uint32_t port,
1234         __out           uint8_t *external_portp)
1235 {
1236         efx_rc_t rc;
1237         int i;
1238         uint32_t port_modes;
1239         uint32_t matches;
1240         uint32_t current;
1241         int32_t count = 1; /* Default 1-1 mapping */
1242         int32_t offset = 1; /* Default starting external port number */
1243
1244         if ((rc = efx_mcdi_get_port_modes(enp, &port_modes, &current)) != 0) {
1245                 /*
1246                  * No current port mode information
1247                  * - infer mapping from available modes
1248                  */
1249                 if ((rc = efx_mcdi_get_port_modes(enp,
1250                             &port_modes, NULL)) != 0) {
1251                         /*
1252                          * No port mode information available
1253                          * - use default mapping
1254                          */
1255                         goto out;
1256                 }
1257         } else {
1258                 /* Only need to scan the current mode */
1259                 port_modes = 1 << current;
1260         }
1261
1262         /*
1263          * Infer the internal port -> external port mapping from
1264          * the possible port modes for this NIC.
1265          */
1266         for (i = 0; i < EFX_ARRAY_SIZE(__ef10_external_port_mappings); ++i) {
1267                 if (__ef10_external_port_mappings[i].family !=
1268                     enp->en_family)
1269                         continue;
1270                 matches = (__ef10_external_port_mappings[i].modes_mask &
1271                     port_modes);
1272                 if (matches != 0) {
1273                         count = __ef10_external_port_mappings[i].count;
1274                         offset = __ef10_external_port_mappings[i].offset;
1275                         port_modes &= ~matches;
1276                 }
1277         }
1278
1279         if (port_modes != 0) {
1280                 /* Some advertised modes are not supported */
1281                 rc = ENOTSUP;
1282                 goto fail1;
1283         }
1284
1285 out:
1286         /*
1287          * Scale as required by last matched mode and then convert to
1288          * correctly offset numbering
1289          */
1290         *external_portp = (uint8_t)((port / count) + offset);
1291         return (0);
1292
1293 fail1:
1294         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1295
1296         return (rc);
1297 }
1298
1299
1300         __checkReturn   efx_rc_t
1301 ef10_nic_probe(
1302         __in            efx_nic_t *enp)
1303 {
1304         const efx_nic_ops_t *enop = enp->en_enop;
1305         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1306         efx_drv_cfg_t *edcp = &(enp->en_drv_cfg);
1307         efx_rc_t rc;
1308
1309         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
1310                     enp->en_family == EFX_FAMILY_MEDFORD);
1311
1312         /* Read and clear any assertion state */
1313         if ((rc = efx_mcdi_read_assertion(enp)) != 0)
1314                 goto fail1;
1315
1316         /* Exit the assertion handler */
1317         if ((rc = efx_mcdi_exit_assertion_handler(enp)) != 0)
1318                 if (rc != EACCES)
1319                         goto fail2;
1320
1321         if ((rc = efx_mcdi_drv_attach(enp, B_TRUE)) != 0)
1322                 goto fail3;
1323
1324         if ((rc = enop->eno_board_cfg(enp)) != 0)
1325                 if (rc != EACCES)
1326                         goto fail4;
1327
1328         /*
1329          * Set default driver config limits (based on board config).
1330          *
1331          * FIXME: For now allocate a fixed number of VIs which is likely to be
1332          * sufficient and small enough to allow multiple functions on the same
1333          * port.
1334          */
1335         edcp->edc_min_vi_count = edcp->edc_max_vi_count =
1336             MIN(128, MAX(encp->enc_rxq_limit, encp->enc_txq_limit));
1337
1338         /* The client driver must configure and enable PIO buffer support */
1339         edcp->edc_max_piobuf_count = 0;
1340         edcp->edc_pio_alloc_size = 0;
1341
1342 #if EFSYS_OPT_MAC_STATS
1343         /* Wipe the MAC statistics */
1344         if ((rc = efx_mcdi_mac_stats_clear(enp)) != 0)
1345                 goto fail5;
1346 #endif
1347
1348 #if EFSYS_OPT_LOOPBACK
1349         if ((rc = efx_mcdi_get_loopback_modes(enp)) != 0)
1350                 goto fail6;
1351 #endif
1352
1353 #if EFSYS_OPT_MON_STATS
1354         if ((rc = mcdi_mon_cfg_build(enp)) != 0) {
1355                 /* Unprivileged functions do not have access to sensors */
1356                 if (rc != EACCES)
1357                         goto fail7;
1358         }
1359 #endif
1360
1361         encp->enc_features = enp->en_features;
1362
1363         return (0);
1364
1365 #if EFSYS_OPT_MON_STATS
1366 fail7:
1367         EFSYS_PROBE(fail7);
1368 #endif
1369 #if EFSYS_OPT_LOOPBACK
1370 fail6:
1371         EFSYS_PROBE(fail6);
1372 #endif
1373 #if EFSYS_OPT_MAC_STATS
1374 fail5:
1375         EFSYS_PROBE(fail5);
1376 #endif
1377 fail4:
1378         EFSYS_PROBE(fail4);
1379 fail3:
1380         EFSYS_PROBE(fail3);
1381 fail2:
1382         EFSYS_PROBE(fail2);
1383 fail1:
1384         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1385
1386         return (rc);
1387 }
1388
1389         __checkReturn   efx_rc_t
1390 ef10_nic_set_drv_limits(
1391         __inout         efx_nic_t *enp,
1392         __in            efx_drv_limits_t *edlp)
1393 {
1394         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1395         efx_drv_cfg_t *edcp = &(enp->en_drv_cfg);
1396         uint32_t min_evq_count, max_evq_count;
1397         uint32_t min_rxq_count, max_rxq_count;
1398         uint32_t min_txq_count, max_txq_count;
1399         efx_rc_t rc;
1400
1401         if (edlp == NULL) {
1402                 rc = EINVAL;
1403                 goto fail1;
1404         }
1405
1406         /* Get minimum required and maximum usable VI limits */
1407         min_evq_count = MIN(edlp->edl_min_evq_count, encp->enc_evq_limit);
1408         min_rxq_count = MIN(edlp->edl_min_rxq_count, encp->enc_rxq_limit);
1409         min_txq_count = MIN(edlp->edl_min_txq_count, encp->enc_txq_limit);
1410
1411         edcp->edc_min_vi_count =
1412             MAX(min_evq_count, MAX(min_rxq_count, min_txq_count));
1413
1414         max_evq_count = MIN(edlp->edl_max_evq_count, encp->enc_evq_limit);
1415         max_rxq_count = MIN(edlp->edl_max_rxq_count, encp->enc_rxq_limit);
1416         max_txq_count = MIN(edlp->edl_max_txq_count, encp->enc_txq_limit);
1417
1418         edcp->edc_max_vi_count =
1419             MAX(max_evq_count, MAX(max_rxq_count, max_txq_count));
1420
1421         /*
1422          * Check limits for sub-allocated piobuf blocks.
1423          * PIO is optional, so don't fail if the limits are incorrect.
1424          */
1425         if ((encp->enc_piobuf_size == 0) ||
1426             (encp->enc_piobuf_limit == 0) ||
1427             (edlp->edl_min_pio_alloc_size == 0) ||
1428             (edlp->edl_min_pio_alloc_size > encp->enc_piobuf_size)) {
1429                 /* Disable PIO */
1430                 edcp->edc_max_piobuf_count = 0;
1431                 edcp->edc_pio_alloc_size = 0;
1432         } else {
1433                 uint32_t blk_size, blk_count, blks_per_piobuf;
1434
1435                 blk_size =
1436                     MAX(edlp->edl_min_pio_alloc_size,
1437                             encp->enc_piobuf_min_alloc_size);
1438
1439                 blks_per_piobuf = encp->enc_piobuf_size / blk_size;
1440                 EFSYS_ASSERT3U(blks_per_piobuf, <=, 32);
1441
1442                 blk_count = (encp->enc_piobuf_limit * blks_per_piobuf);
1443
1444                 /* A zero max pio alloc count means unlimited */
1445                 if ((edlp->edl_max_pio_alloc_count > 0) &&
1446                     (edlp->edl_max_pio_alloc_count < blk_count)) {
1447                         blk_count = edlp->edl_max_pio_alloc_count;
1448                 }
1449
1450                 edcp->edc_pio_alloc_size = blk_size;
1451                 edcp->edc_max_piobuf_count =
1452                     (blk_count + (blks_per_piobuf - 1)) / blks_per_piobuf;
1453         }
1454
1455         return (0);
1456
1457 fail1:
1458         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1459
1460         return (rc);
1461 }
1462
1463
1464         __checkReturn   efx_rc_t
1465 ef10_nic_reset(
1466         __in            efx_nic_t *enp)
1467 {
1468         efx_mcdi_req_t req;
1469         uint8_t payload[MAX(MC_CMD_ENTITY_RESET_IN_LEN,
1470                             MC_CMD_ENTITY_RESET_OUT_LEN)];
1471         efx_rc_t rc;
1472
1473         /* ef10_nic_reset() is called to recover from BADASSERT failures. */
1474         if ((rc = efx_mcdi_read_assertion(enp)) != 0)
1475                 goto fail1;
1476         if ((rc = efx_mcdi_exit_assertion_handler(enp)) != 0)
1477                 goto fail2;
1478
1479         (void) memset(payload, 0, sizeof (payload));
1480         req.emr_cmd = MC_CMD_ENTITY_RESET;
1481         req.emr_in_buf = payload;
1482         req.emr_in_length = MC_CMD_ENTITY_RESET_IN_LEN;
1483         req.emr_out_buf = payload;
1484         req.emr_out_length = MC_CMD_ENTITY_RESET_OUT_LEN;
1485
1486         MCDI_IN_POPULATE_DWORD_1(req, ENTITY_RESET_IN_FLAG,
1487             ENTITY_RESET_IN_FUNCTION_RESOURCE_RESET, 1);
1488
1489         efx_mcdi_execute(enp, &req);
1490
1491         if (req.emr_rc != 0) {
1492                 rc = req.emr_rc;
1493                 goto fail3;
1494         }
1495
1496         /* Clear RX/TX DMA queue errors */
1497         enp->en_reset_flags &= ~(EFX_RESET_RXQ_ERR | EFX_RESET_TXQ_ERR);
1498
1499         return (0);
1500
1501 fail3:
1502         EFSYS_PROBE(fail3);
1503 fail2:
1504         EFSYS_PROBE(fail2);
1505 fail1:
1506         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1507
1508         return (rc);
1509 }
1510
1511         __checkReturn   efx_rc_t
1512 ef10_nic_init(
1513         __in            efx_nic_t *enp)
1514 {
1515         efx_drv_cfg_t *edcp = &(enp->en_drv_cfg);
1516         uint32_t min_vi_count, max_vi_count;
1517         uint32_t vi_count, vi_base, vi_shift;
1518         uint32_t i;
1519         uint32_t retry;
1520         uint32_t delay_us;
1521         efx_rc_t rc;
1522
1523         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
1524                     enp->en_family == EFX_FAMILY_MEDFORD);
1525
1526         /* Enable reporting of some events (e.g. link change) */
1527         if ((rc = efx_mcdi_log_ctrl(enp)) != 0)
1528                 goto fail1;
1529
1530         /* Allocate (optional) on-chip PIO buffers */
1531         ef10_nic_alloc_piobufs(enp, edcp->edc_max_piobuf_count);
1532
1533         /*
1534          * For best performance, PIO writes should use a write-combined
1535          * (WC) memory mapping. Using a separate WC mapping for the PIO
1536          * aperture of each VI would be a burden to drivers (and not
1537          * possible if the host page size is >4Kbyte).
1538          *
1539          * To avoid this we use a single uncached (UC) mapping for VI
1540          * register access, and a single WC mapping for extra VIs used
1541          * for PIO writes.
1542          *
1543          * Each piobuf must be linked to a VI in the WC mapping, and to
1544          * each VI that is using a sub-allocated block from the piobuf.
1545          */
1546         min_vi_count = edcp->edc_min_vi_count;
1547         max_vi_count =
1548             edcp->edc_max_vi_count + enp->en_arch.ef10.ena_piobuf_count;
1549
1550         /* Ensure that the previously attached driver's VIs are freed */
1551         if ((rc = efx_mcdi_free_vis(enp)) != 0)
1552                 goto fail2;
1553
1554         /*
1555          * Reserve VI resources (EVQ+RXQ+TXQ) for this PCIe function. If this
1556          * fails then retrying the request for fewer VI resources may succeed.
1557          */
1558         vi_count = 0;
1559         if ((rc = efx_mcdi_alloc_vis(enp, min_vi_count, max_vi_count,
1560                     &vi_base, &vi_count, &vi_shift)) != 0)
1561                 goto fail3;
1562
1563         EFSYS_PROBE2(vi_alloc, uint32_t, vi_base, uint32_t, vi_count);
1564
1565         if (vi_count < min_vi_count) {
1566                 rc = ENOMEM;
1567                 goto fail4;
1568         }
1569
1570         enp->en_arch.ef10.ena_vi_base = vi_base;
1571         enp->en_arch.ef10.ena_vi_count = vi_count;
1572         enp->en_arch.ef10.ena_vi_shift = vi_shift;
1573
1574         if (vi_count < min_vi_count + enp->en_arch.ef10.ena_piobuf_count) {
1575                 /* Not enough extra VIs to map piobufs */
1576                 ef10_nic_free_piobufs(enp);
1577         }
1578
1579         enp->en_arch.ef10.ena_pio_write_vi_base =
1580             vi_count - enp->en_arch.ef10.ena_piobuf_count;
1581
1582         /* Save UC memory mapping details */
1583         enp->en_arch.ef10.ena_uc_mem_map_offset = 0;
1584         if (enp->en_arch.ef10.ena_piobuf_count > 0) {
1585                 enp->en_arch.ef10.ena_uc_mem_map_size =
1586                     (ER_DZ_TX_PIOBUF_STEP *
1587                     enp->en_arch.ef10.ena_pio_write_vi_base);
1588         } else {
1589                 enp->en_arch.ef10.ena_uc_mem_map_size =
1590                     (ER_DZ_TX_PIOBUF_STEP *
1591                     enp->en_arch.ef10.ena_vi_count);
1592         }
1593
1594         /* Save WC memory mapping details */
1595         enp->en_arch.ef10.ena_wc_mem_map_offset =
1596             enp->en_arch.ef10.ena_uc_mem_map_offset +
1597             enp->en_arch.ef10.ena_uc_mem_map_size;
1598
1599         enp->en_arch.ef10.ena_wc_mem_map_size =
1600             (ER_DZ_TX_PIOBUF_STEP *
1601             enp->en_arch.ef10.ena_piobuf_count);
1602
1603         /* Link piobufs to extra VIs in WC mapping */
1604         if (enp->en_arch.ef10.ena_piobuf_count > 0) {
1605                 for (i = 0; i < enp->en_arch.ef10.ena_piobuf_count; i++) {
1606                         rc = efx_mcdi_link_piobuf(enp,
1607                             enp->en_arch.ef10.ena_pio_write_vi_base + i,
1608                             enp->en_arch.ef10.ena_piobuf_handle[i]);
1609                         if (rc != 0)
1610                                 break;
1611                 }
1612         }
1613
1614         /*
1615          * Allocate a vAdaptor attached to our upstream vPort/pPort.
1616          *
1617          * On a VF, this may fail with MC_CMD_ERR_NO_EVB_PORT (ENOENT) if the PF
1618          * driver has yet to bring up the EVB port. See bug 56147. In this case,
1619          * retry the request several times after waiting a while. The wait time
1620          * between retries starts small (10ms) and exponentially increases.
1621          * Total wait time is a little over two seconds. Retry logic in the
1622          * client driver may mean this whole loop is repeated if it continues to
1623          * fail.
1624          */
1625         retry = 0;
1626         delay_us = 10000;
1627         while ((rc = efx_mcdi_vadaptor_alloc(enp, EVB_PORT_ID_ASSIGNED)) != 0) {
1628                 if (EFX_PCI_FUNCTION_IS_PF(&enp->en_nic_cfg) ||
1629                     (rc != ENOENT)) {
1630                         /*
1631                          * Do not retry alloc for PF, or for other errors on
1632                          * a VF.
1633                          */
1634                         goto fail5;
1635                 }
1636
1637                 /* VF startup before PF is ready. Retry allocation. */
1638                 if (retry > 5) {
1639                         /* Too many attempts */
1640                         rc = EINVAL;
1641                         goto fail6;
1642                 }
1643                 EFSYS_PROBE1(mcdi_no_evb_port_retry, int, retry);
1644                 EFSYS_SLEEP(delay_us);
1645                 retry++;
1646                 if (delay_us < 500000)
1647                         delay_us <<= 2;
1648         }
1649
1650         enp->en_vport_id = EVB_PORT_ID_ASSIGNED;
1651         enp->en_nic_cfg.enc_mcdi_max_payload_length = MCDI_CTL_SDU_LEN_MAX_V2;
1652
1653         return (0);
1654
1655 fail6:
1656         EFSYS_PROBE(fail6);
1657 fail5:
1658         EFSYS_PROBE(fail5);
1659 fail4:
1660         EFSYS_PROBE(fail4);
1661 fail3:
1662         EFSYS_PROBE(fail3);
1663 fail2:
1664         EFSYS_PROBE(fail2);
1665
1666         ef10_nic_free_piobufs(enp);
1667
1668 fail1:
1669         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1670
1671         return (rc);
1672 }
1673
1674         __checkReturn   efx_rc_t
1675 ef10_nic_get_vi_pool(
1676         __in            efx_nic_t *enp,
1677         __out           uint32_t *vi_countp)
1678 {
1679         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
1680                     enp->en_family == EFX_FAMILY_MEDFORD);
1681
1682         /*
1683          * Report VIs that the client driver can use.
1684          * Do not include VIs used for PIO buffer writes.
1685          */
1686         *vi_countp = enp->en_arch.ef10.ena_pio_write_vi_base;
1687
1688         return (0);
1689 }
1690
1691         __checkReturn   efx_rc_t
1692 ef10_nic_get_bar_region(
1693         __in            efx_nic_t *enp,
1694         __in            efx_nic_region_t region,
1695         __out           uint32_t *offsetp,
1696         __out           size_t *sizep)
1697 {
1698         efx_rc_t rc;
1699
1700         EFSYS_ASSERT(enp->en_family == EFX_FAMILY_HUNTINGTON ||
1701                     enp->en_family == EFX_FAMILY_MEDFORD);
1702
1703         /*
1704          * TODO: Specify host memory mapping alignment and granularity
1705          * in efx_drv_limits_t so that they can be taken into account
1706          * when allocating extra VIs for PIO writes.
1707          */
1708         switch (region) {
1709         case EFX_REGION_VI:
1710                 /* UC mapped memory BAR region for VI registers */
1711                 *offsetp = enp->en_arch.ef10.ena_uc_mem_map_offset;
1712                 *sizep = enp->en_arch.ef10.ena_uc_mem_map_size;
1713                 break;
1714
1715         case EFX_REGION_PIO_WRITE_VI:
1716                 /* WC mapped memory BAR region for piobuf writes */
1717                 *offsetp = enp->en_arch.ef10.ena_wc_mem_map_offset;
1718                 *sizep = enp->en_arch.ef10.ena_wc_mem_map_size;
1719                 break;
1720
1721         default:
1722                 rc = EINVAL;
1723                 goto fail1;
1724         }
1725
1726         return (0);
1727
1728 fail1:
1729         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1730
1731         return (rc);
1732 }
1733
1734                         void
1735 ef10_nic_fini(
1736         __in            efx_nic_t *enp)
1737 {
1738         uint32_t i;
1739         efx_rc_t rc;
1740
1741         (void) efx_mcdi_vadaptor_free(enp, enp->en_vport_id);
1742         enp->en_vport_id = 0;
1743
1744         /* Unlink piobufs from extra VIs in WC mapping */
1745         if (enp->en_arch.ef10.ena_piobuf_count > 0) {
1746                 for (i = 0; i < enp->en_arch.ef10.ena_piobuf_count; i++) {
1747                         rc = efx_mcdi_unlink_piobuf(enp,
1748                             enp->en_arch.ef10.ena_pio_write_vi_base + i);
1749                         if (rc != 0)
1750                                 break;
1751                 }
1752         }
1753
1754         ef10_nic_free_piobufs(enp);
1755
1756         (void) efx_mcdi_free_vis(enp);
1757         enp->en_arch.ef10.ena_vi_count = 0;
1758 }
1759
1760                         void
1761 ef10_nic_unprobe(
1762         __in            efx_nic_t *enp)
1763 {
1764 #if EFSYS_OPT_MON_STATS
1765         mcdi_mon_cfg_free(enp);
1766 #endif /* EFSYS_OPT_MON_STATS */
1767         (void) efx_mcdi_drv_attach(enp, B_FALSE);
1768 }
1769
1770 #if EFSYS_OPT_DIAG
1771
1772         __checkReturn   efx_rc_t
1773 ef10_nic_register_test(
1774         __in            efx_nic_t *enp)
1775 {
1776         efx_rc_t rc;
1777
1778         /* FIXME */
1779         _NOTE(ARGUNUSED(enp))
1780         _NOTE(CONSTANTCONDITION)
1781         if (B_FALSE) {
1782                 rc = ENOTSUP;
1783                 goto fail1;
1784         }
1785         /* FIXME */
1786
1787         return (0);
1788
1789 fail1:
1790         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1791
1792         return (rc);
1793 }
1794
1795 #endif  /* EFSYS_OPT_DIAG */
1796
1797
1798 #endif  /* EFSYS_OPT_HUNTINGTON || EFSYS_OPT_MEDFORD */