net/sfc/base: add EVB module vSwitch/vPort/vAdaptor ops
[dpdk.git] / drivers / net / sfc / base / ef10_nic.c
1 /* SPDX-License-Identifier: BSD-3-Clause
2  *
3  * Copyright (c) 2012-2018 Solarflare Communications Inc.
4  * All rights reserved.
5  */
6
7 #include "efx.h"
8 #include "efx_impl.h"
9 #if EFSYS_OPT_MON_MCDI
10 #include "mcdi_mon.h"
11 #endif
12
13 #if EFX_OPTS_EF10()
14
15 #include "ef10_tlv_layout.h"
16
17         __checkReturn   efx_rc_t
18 efx_mcdi_get_port_assignment(
19         __in            efx_nic_t *enp,
20         __out           uint32_t *portp)
21 {
22         efx_mcdi_req_t req;
23         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_GET_PORT_ASSIGNMENT_IN_LEN,
24                 MC_CMD_GET_PORT_ASSIGNMENT_OUT_LEN);
25         efx_rc_t rc;
26
27         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
28
29         req.emr_cmd = MC_CMD_GET_PORT_ASSIGNMENT;
30         req.emr_in_buf = payload;
31         req.emr_in_length = MC_CMD_GET_PORT_ASSIGNMENT_IN_LEN;
32         req.emr_out_buf = payload;
33         req.emr_out_length = MC_CMD_GET_PORT_ASSIGNMENT_OUT_LEN;
34
35         efx_mcdi_execute(enp, &req);
36
37         if (req.emr_rc != 0) {
38                 rc = req.emr_rc;
39                 goto fail1;
40         }
41
42         if (req.emr_out_length_used < MC_CMD_GET_PORT_ASSIGNMENT_OUT_LEN) {
43                 rc = EMSGSIZE;
44                 goto fail2;
45         }
46
47         *portp = MCDI_OUT_DWORD(req, GET_PORT_ASSIGNMENT_OUT_PORT);
48
49         return (0);
50
51 fail2:
52         EFSYS_PROBE(fail2);
53 fail1:
54         EFSYS_PROBE1(fail1, efx_rc_t, rc);
55
56         return (rc);
57 }
58
59         __checkReturn   efx_rc_t
60 efx_mcdi_get_port_modes(
61         __in            efx_nic_t *enp,
62         __out           uint32_t *modesp,
63         __out_opt       uint32_t *current_modep,
64         __out_opt       uint32_t *default_modep)
65 {
66         efx_mcdi_req_t req;
67         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_GET_PORT_MODES_IN_LEN,
68                 MC_CMD_GET_PORT_MODES_OUT_LEN);
69         efx_rc_t rc;
70
71         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
72
73         req.emr_cmd = MC_CMD_GET_PORT_MODES;
74         req.emr_in_buf = payload;
75         req.emr_in_length = MC_CMD_GET_PORT_MODES_IN_LEN;
76         req.emr_out_buf = payload;
77         req.emr_out_length = MC_CMD_GET_PORT_MODES_OUT_LEN;
78
79         efx_mcdi_execute(enp, &req);
80
81         if (req.emr_rc != 0) {
82                 rc = req.emr_rc;
83                 goto fail1;
84         }
85
86         /*
87          * Require only Modes and DefaultMode fields, unless the current mode
88          * was requested (CurrentMode field was added for Medford).
89          */
90         if (req.emr_out_length_used <
91             MC_CMD_GET_PORT_MODES_OUT_CURRENT_MODE_OFST) {
92                 rc = EMSGSIZE;
93                 goto fail2;
94         }
95         if ((current_modep != NULL) && (req.emr_out_length_used <
96             MC_CMD_GET_PORT_MODES_OUT_CURRENT_MODE_OFST + 4)) {
97                 rc = EMSGSIZE;
98                 goto fail3;
99         }
100
101         *modesp = MCDI_OUT_DWORD(req, GET_PORT_MODES_OUT_MODES);
102
103         if (current_modep != NULL) {
104                 *current_modep = MCDI_OUT_DWORD(req,
105                                             GET_PORT_MODES_OUT_CURRENT_MODE);
106         }
107
108         if (default_modep != NULL) {
109                 *default_modep = MCDI_OUT_DWORD(req,
110                                             GET_PORT_MODES_OUT_DEFAULT_MODE);
111         }
112
113         return (0);
114
115 fail3:
116         EFSYS_PROBE(fail3);
117 fail2:
118         EFSYS_PROBE(fail2);
119 fail1:
120         EFSYS_PROBE1(fail1, efx_rc_t, rc);
121
122         return (rc);
123 }
124
125         __checkReturn   efx_rc_t
126 ef10_nic_get_port_mode_bandwidth(
127         __in            efx_nic_t *enp,
128         __out           uint32_t *bandwidth_mbpsp)
129 {
130         uint32_t port_modes;
131         uint32_t current_mode;
132         efx_port_t *epp = &(enp->en_port);
133
134         uint32_t single_lane;
135         uint32_t dual_lane;
136         uint32_t quad_lane;
137         uint32_t bandwidth;
138         efx_rc_t rc;
139
140         if ((rc = efx_mcdi_get_port_modes(enp, &port_modes,
141                                     &current_mode, NULL)) != 0) {
142                 /* No port mode info available. */
143                 goto fail1;
144         }
145
146         if (epp->ep_phy_cap_mask & (1 << EFX_PHY_CAP_25000FDX))
147                 single_lane = 25000;
148         else
149                 single_lane = 10000;
150
151         if (epp->ep_phy_cap_mask & (1 << EFX_PHY_CAP_50000FDX))
152                 dual_lane = 50000;
153         else
154                 dual_lane = 20000;
155
156         if (epp->ep_phy_cap_mask & (1 << EFX_PHY_CAP_100000FDX))
157                 quad_lane = 100000;
158         else
159                 quad_lane = 40000;
160
161         switch (current_mode) {
162         case TLV_PORT_MODE_1x1_NA:                      /* mode 0 */
163                 bandwidth = single_lane;
164                 break;
165         case TLV_PORT_MODE_1x2_NA:                      /* mode 10 */
166         case TLV_PORT_MODE_NA_1x2:                      /* mode 11 */
167                 bandwidth = dual_lane;
168                 break;
169         case TLV_PORT_MODE_1x1_1x1:                     /* mode 2 */
170                 bandwidth = single_lane + single_lane;
171                 break;
172         case TLV_PORT_MODE_4x1_NA:                      /* mode 4 */
173         case TLV_PORT_MODE_NA_4x1:                      /* mode 8 */
174                 bandwidth = 4 * single_lane;
175                 break;
176         case TLV_PORT_MODE_2x1_2x1:                     /* mode 5 */
177                 bandwidth = (2 * single_lane) + (2 * single_lane);
178                 break;
179         case TLV_PORT_MODE_1x2_1x2:                     /* mode 12 */
180                 bandwidth = dual_lane + dual_lane;
181                 break;
182         case TLV_PORT_MODE_1x2_2x1:                     /* mode 17 */
183         case TLV_PORT_MODE_2x1_1x2:                     /* mode 18 */
184                 bandwidth = dual_lane + (2 * single_lane);
185                 break;
186         /* Legacy Medford-only mode. Do not use (see bug63270) */
187         case TLV_PORT_MODE_10G_10G_10G_10G_Q1_Q2:       /* mode 9 */
188                 bandwidth = 4 * single_lane;
189                 break;
190         case TLV_PORT_MODE_1x4_NA:                      /* mode 1 */
191         case TLV_PORT_MODE_NA_1x4:                      /* mode 22 */
192                 bandwidth = quad_lane;
193                 break;
194         case TLV_PORT_MODE_2x2_NA:                      /* mode 13 */
195         case TLV_PORT_MODE_NA_2x2:                      /* mode 14 */
196                 bandwidth = 2 * dual_lane;
197                 break;
198         case TLV_PORT_MODE_1x4_2x1:                     /* mode 6 */
199         case TLV_PORT_MODE_2x1_1x4:                     /* mode 7 */
200                 bandwidth = quad_lane + (2 * single_lane);
201                 break;
202         case TLV_PORT_MODE_1x4_1x2:                     /* mode 15 */
203         case TLV_PORT_MODE_1x2_1x4:                     /* mode 16 */
204                 bandwidth = quad_lane + dual_lane;
205                 break;
206         case TLV_PORT_MODE_1x4_1x4:                     /* mode 3 */
207                 bandwidth = quad_lane + quad_lane;
208                 break;
209         default:
210                 rc = EINVAL;
211                 goto fail2;
212         }
213
214         *bandwidth_mbpsp = bandwidth;
215
216         return (0);
217
218 fail2:
219         EFSYS_PROBE(fail2);
220 fail1:
221         EFSYS_PROBE1(fail1, efx_rc_t, rc);
222
223         return (rc);
224 }
225
226         __checkReturn           efx_rc_t
227 efx_mcdi_vadaptor_alloc(
228         __in                    efx_nic_t *enp,
229         __in                    uint32_t port_id)
230 {
231         efx_mcdi_req_t req;
232         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_VADAPTOR_ALLOC_IN_LEN,
233                 MC_CMD_VADAPTOR_ALLOC_OUT_LEN);
234         efx_rc_t rc;
235
236         EFSYS_ASSERT3U(enp->en_vport_id, ==, EVB_PORT_ID_NULL);
237
238         req.emr_cmd = MC_CMD_VADAPTOR_ALLOC;
239         req.emr_in_buf = payload;
240         req.emr_in_length = MC_CMD_VADAPTOR_ALLOC_IN_LEN;
241         req.emr_out_buf = payload;
242         req.emr_out_length = MC_CMD_VADAPTOR_ALLOC_OUT_LEN;
243
244         MCDI_IN_SET_DWORD(req, VADAPTOR_ALLOC_IN_UPSTREAM_PORT_ID, port_id);
245         MCDI_IN_POPULATE_DWORD_1(req, VADAPTOR_ALLOC_IN_FLAGS,
246             VADAPTOR_ALLOC_IN_FLAG_PERMIT_SET_MAC_WHEN_FILTERS_INSTALLED,
247             enp->en_nic_cfg.enc_allow_set_mac_with_installed_filters ? 1 : 0);
248
249         efx_mcdi_execute(enp, &req);
250
251         if (req.emr_rc != 0) {
252                 rc = req.emr_rc;
253                 goto fail1;
254         }
255
256         return (0);
257
258 fail1:
259         EFSYS_PROBE1(fail1, efx_rc_t, rc);
260
261         return (rc);
262 }
263
264         __checkReturn           efx_rc_t
265 efx_mcdi_vadaptor_free(
266         __in                    efx_nic_t *enp,
267         __in                    uint32_t port_id)
268 {
269         efx_mcdi_req_t req;
270         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_VADAPTOR_FREE_IN_LEN,
271                 MC_CMD_VADAPTOR_FREE_OUT_LEN);
272         efx_rc_t rc;
273
274         req.emr_cmd = MC_CMD_VADAPTOR_FREE;
275         req.emr_in_buf = payload;
276         req.emr_in_length = MC_CMD_VADAPTOR_FREE_IN_LEN;
277         req.emr_out_buf = payload;
278         req.emr_out_length = MC_CMD_VADAPTOR_FREE_OUT_LEN;
279
280         MCDI_IN_SET_DWORD(req, VADAPTOR_FREE_IN_UPSTREAM_PORT_ID, port_id);
281
282         efx_mcdi_execute(enp, &req);
283
284         if (req.emr_rc != 0) {
285                 rc = req.emr_rc;
286                 goto fail1;
287         }
288
289         return (0);
290
291 fail1:
292         EFSYS_PROBE1(fail1, efx_rc_t, rc);
293
294         return (rc);
295 }
296
297         __checkReturn   efx_rc_t
298 efx_mcdi_get_mac_address_pf(
299         __in                    efx_nic_t *enp,
300         __out_ecount_opt(6)     uint8_t mac_addrp[6])
301 {
302         efx_mcdi_req_t req;
303         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_GET_MAC_ADDRESSES_IN_LEN,
304                 MC_CMD_GET_MAC_ADDRESSES_OUT_LEN);
305         efx_rc_t rc;
306
307         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
308
309         req.emr_cmd = MC_CMD_GET_MAC_ADDRESSES;
310         req.emr_in_buf = payload;
311         req.emr_in_length = MC_CMD_GET_MAC_ADDRESSES_IN_LEN;
312         req.emr_out_buf = payload;
313         req.emr_out_length = MC_CMD_GET_MAC_ADDRESSES_OUT_LEN;
314
315         efx_mcdi_execute(enp, &req);
316
317         if (req.emr_rc != 0) {
318                 rc = req.emr_rc;
319                 goto fail1;
320         }
321
322         if (req.emr_out_length_used < MC_CMD_GET_MAC_ADDRESSES_OUT_LEN) {
323                 rc = EMSGSIZE;
324                 goto fail2;
325         }
326
327         if (MCDI_OUT_DWORD(req, GET_MAC_ADDRESSES_OUT_MAC_COUNT) < 1) {
328                 rc = ENOENT;
329                 goto fail3;
330         }
331
332         if (mac_addrp != NULL) {
333                 uint8_t *addrp;
334
335                 addrp = MCDI_OUT2(req, uint8_t,
336                     GET_MAC_ADDRESSES_OUT_MAC_ADDR_BASE);
337
338                 EFX_MAC_ADDR_COPY(mac_addrp, addrp);
339         }
340
341         return (0);
342
343 fail3:
344         EFSYS_PROBE(fail3);
345 fail2:
346         EFSYS_PROBE(fail2);
347 fail1:
348         EFSYS_PROBE1(fail1, efx_rc_t, rc);
349
350         return (rc);
351 }
352
353         __checkReturn   efx_rc_t
354 efx_mcdi_get_mac_address_vf(
355         __in                    efx_nic_t *enp,
356         __out_ecount_opt(6)     uint8_t mac_addrp[6])
357 {
358         efx_mcdi_req_t req;
359         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_VPORT_GET_MAC_ADDRESSES_IN_LEN,
360                 MC_CMD_VPORT_GET_MAC_ADDRESSES_OUT_LENMAX);
361         efx_rc_t rc;
362
363         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
364
365         req.emr_cmd = MC_CMD_VPORT_GET_MAC_ADDRESSES;
366         req.emr_in_buf = payload;
367         req.emr_in_length = MC_CMD_VPORT_GET_MAC_ADDRESSES_IN_LEN;
368         req.emr_out_buf = payload;
369         req.emr_out_length = MC_CMD_VPORT_GET_MAC_ADDRESSES_OUT_LENMAX;
370
371         MCDI_IN_SET_DWORD(req, VPORT_GET_MAC_ADDRESSES_IN_VPORT_ID,
372             EVB_PORT_ID_ASSIGNED);
373
374         efx_mcdi_execute(enp, &req);
375
376         if (req.emr_rc != 0) {
377                 rc = req.emr_rc;
378                 goto fail1;
379         }
380
381         if (req.emr_out_length_used <
382             MC_CMD_VPORT_GET_MAC_ADDRESSES_OUT_LENMIN) {
383                 rc = EMSGSIZE;
384                 goto fail2;
385         }
386
387         if (MCDI_OUT_DWORD(req,
388                 VPORT_GET_MAC_ADDRESSES_OUT_MACADDR_COUNT) < 1) {
389                 rc = ENOENT;
390                 goto fail3;
391         }
392
393         if (mac_addrp != NULL) {
394                 uint8_t *addrp;
395
396                 addrp = MCDI_OUT2(req, uint8_t,
397                     VPORT_GET_MAC_ADDRESSES_OUT_MACADDR);
398
399                 EFX_MAC_ADDR_COPY(mac_addrp, addrp);
400         }
401
402         return (0);
403
404 fail3:
405         EFSYS_PROBE(fail3);
406 fail2:
407         EFSYS_PROBE(fail2);
408 fail1:
409         EFSYS_PROBE1(fail1, efx_rc_t, rc);
410
411         return (rc);
412 }
413
414         __checkReturn   efx_rc_t
415 efx_mcdi_get_clock(
416         __in            efx_nic_t *enp,
417         __out           uint32_t *sys_freqp,
418         __out           uint32_t *dpcpu_freqp)
419 {
420         efx_mcdi_req_t req;
421         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_GET_CLOCK_IN_LEN,
422                 MC_CMD_GET_CLOCK_OUT_LEN);
423         efx_rc_t rc;
424
425         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
426
427         req.emr_cmd = MC_CMD_GET_CLOCK;
428         req.emr_in_buf = payload;
429         req.emr_in_length = MC_CMD_GET_CLOCK_IN_LEN;
430         req.emr_out_buf = payload;
431         req.emr_out_length = MC_CMD_GET_CLOCK_OUT_LEN;
432
433         efx_mcdi_execute(enp, &req);
434
435         if (req.emr_rc != 0) {
436                 rc = req.emr_rc;
437                 goto fail1;
438         }
439
440         if (req.emr_out_length_used < MC_CMD_GET_CLOCK_OUT_LEN) {
441                 rc = EMSGSIZE;
442                 goto fail2;
443         }
444
445         *sys_freqp = MCDI_OUT_DWORD(req, GET_CLOCK_OUT_SYS_FREQ);
446         if (*sys_freqp == 0) {
447                 rc = EINVAL;
448                 goto fail3;
449         }
450         *dpcpu_freqp = MCDI_OUT_DWORD(req, GET_CLOCK_OUT_DPCPU_FREQ);
451         if (*dpcpu_freqp == 0) {
452                 rc = EINVAL;
453                 goto fail4;
454         }
455
456         return (0);
457
458 fail4:
459         EFSYS_PROBE(fail4);
460 fail3:
461         EFSYS_PROBE(fail3);
462 fail2:
463         EFSYS_PROBE(fail2);
464 fail1:
465         EFSYS_PROBE1(fail1, efx_rc_t, rc);
466
467         return (rc);
468 }
469
470         __checkReturn   efx_rc_t
471 efx_mcdi_get_rxdp_config(
472         __in            efx_nic_t *enp,
473         __out           uint32_t *end_paddingp)
474 {
475         efx_mcdi_req_t req;
476         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_GET_RXDP_CONFIG_IN_LEN,
477                 MC_CMD_GET_RXDP_CONFIG_OUT_LEN);
478         uint32_t end_padding;
479         efx_rc_t rc;
480
481         req.emr_cmd = MC_CMD_GET_RXDP_CONFIG;
482         req.emr_in_buf = payload;
483         req.emr_in_length = MC_CMD_GET_RXDP_CONFIG_IN_LEN;
484         req.emr_out_buf = payload;
485         req.emr_out_length = MC_CMD_GET_RXDP_CONFIG_OUT_LEN;
486
487         efx_mcdi_execute(enp, &req);
488         if (req.emr_rc != 0) {
489                 rc = req.emr_rc;
490                 goto fail1;
491         }
492
493         if (MCDI_OUT_DWORD_FIELD(req, GET_RXDP_CONFIG_OUT_DATA,
494                                     GET_RXDP_CONFIG_OUT_PAD_HOST_DMA) == 0) {
495                 /* RX DMA end padding is disabled */
496                 end_padding = 0;
497         } else {
498                 switch (MCDI_OUT_DWORD_FIELD(req, GET_RXDP_CONFIG_OUT_DATA,
499                                             GET_RXDP_CONFIG_OUT_PAD_HOST_LEN)) {
500                 case MC_CMD_SET_RXDP_CONFIG_IN_PAD_HOST_64:
501                         end_padding = 64;
502                         break;
503                 case MC_CMD_SET_RXDP_CONFIG_IN_PAD_HOST_128:
504                         end_padding = 128;
505                         break;
506                 case MC_CMD_SET_RXDP_CONFIG_IN_PAD_HOST_256:
507                         end_padding = 256;
508                         break;
509                 default:
510                         rc = ENOTSUP;
511                         goto fail2;
512                 }
513         }
514
515         *end_paddingp = end_padding;
516
517         return (0);
518
519 fail2:
520         EFSYS_PROBE(fail2);
521 fail1:
522         EFSYS_PROBE1(fail1, efx_rc_t, rc);
523
524         return (rc);
525 }
526
527         __checkReturn   efx_rc_t
528 efx_mcdi_get_vector_cfg(
529         __in            efx_nic_t *enp,
530         __out_opt       uint32_t *vec_basep,
531         __out_opt       uint32_t *pf_nvecp,
532         __out_opt       uint32_t *vf_nvecp)
533 {
534         efx_mcdi_req_t req;
535         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_GET_VECTOR_CFG_IN_LEN,
536                 MC_CMD_GET_VECTOR_CFG_OUT_LEN);
537         efx_rc_t rc;
538
539         req.emr_cmd = MC_CMD_GET_VECTOR_CFG;
540         req.emr_in_buf = payload;
541         req.emr_in_length = MC_CMD_GET_VECTOR_CFG_IN_LEN;
542         req.emr_out_buf = payload;
543         req.emr_out_length = MC_CMD_GET_VECTOR_CFG_OUT_LEN;
544
545         efx_mcdi_execute(enp, &req);
546
547         if (req.emr_rc != 0) {
548                 rc = req.emr_rc;
549                 goto fail1;
550         }
551
552         if (req.emr_out_length_used < MC_CMD_GET_VECTOR_CFG_OUT_LEN) {
553                 rc = EMSGSIZE;
554                 goto fail2;
555         }
556
557         if (vec_basep != NULL)
558                 *vec_basep = MCDI_OUT_DWORD(req, GET_VECTOR_CFG_OUT_VEC_BASE);
559         if (pf_nvecp != NULL)
560                 *pf_nvecp = MCDI_OUT_DWORD(req, GET_VECTOR_CFG_OUT_VECS_PER_PF);
561         if (vf_nvecp != NULL)
562                 *vf_nvecp = MCDI_OUT_DWORD(req, GET_VECTOR_CFG_OUT_VECS_PER_VF);
563
564         return (0);
565
566 fail2:
567         EFSYS_PROBE(fail2);
568 fail1:
569         EFSYS_PROBE1(fail1, efx_rc_t, rc);
570
571         return (rc);
572 }
573
574 static  __checkReturn   efx_rc_t
575 efx_mcdi_alloc_vis(
576         __in            efx_nic_t *enp,
577         __in            uint32_t min_vi_count,
578         __in            uint32_t max_vi_count,
579         __out           uint32_t *vi_basep,
580         __out           uint32_t *vi_countp,
581         __out           uint32_t *vi_shiftp)
582 {
583         efx_mcdi_req_t req;
584         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_ALLOC_VIS_IN_LEN,
585                 MC_CMD_ALLOC_VIS_EXT_OUT_LEN);
586         efx_rc_t rc;
587
588         if (vi_countp == NULL) {
589                 rc = EINVAL;
590                 goto fail1;
591         }
592
593         req.emr_cmd = MC_CMD_ALLOC_VIS;
594         req.emr_in_buf = payload;
595         req.emr_in_length = MC_CMD_ALLOC_VIS_IN_LEN;
596         req.emr_out_buf = payload;
597         req.emr_out_length = MC_CMD_ALLOC_VIS_EXT_OUT_LEN;
598
599         MCDI_IN_SET_DWORD(req, ALLOC_VIS_IN_MIN_VI_COUNT, min_vi_count);
600         MCDI_IN_SET_DWORD(req, ALLOC_VIS_IN_MAX_VI_COUNT, max_vi_count);
601
602         efx_mcdi_execute(enp, &req);
603
604         if (req.emr_rc != 0) {
605                 rc = req.emr_rc;
606                 goto fail2;
607         }
608
609         if (req.emr_out_length_used < MC_CMD_ALLOC_VIS_OUT_LEN) {
610                 rc = EMSGSIZE;
611                 goto fail3;
612         }
613
614         *vi_basep = MCDI_OUT_DWORD(req, ALLOC_VIS_OUT_VI_BASE);
615         *vi_countp = MCDI_OUT_DWORD(req, ALLOC_VIS_OUT_VI_COUNT);
616
617         /* Report VI_SHIFT if available (always zero for Huntington) */
618         if (req.emr_out_length_used < MC_CMD_ALLOC_VIS_EXT_OUT_LEN)
619                 *vi_shiftp = 0;
620         else
621                 *vi_shiftp = MCDI_OUT_DWORD(req, ALLOC_VIS_EXT_OUT_VI_SHIFT);
622
623         return (0);
624
625 fail3:
626         EFSYS_PROBE(fail3);
627 fail2:
628         EFSYS_PROBE(fail2);
629 fail1:
630         EFSYS_PROBE1(fail1, efx_rc_t, rc);
631
632         return (rc);
633 }
634
635
636 static  __checkReturn   efx_rc_t
637 efx_mcdi_free_vis(
638         __in            efx_nic_t *enp)
639 {
640         efx_mcdi_req_t req;
641         efx_rc_t rc;
642
643         EFX_STATIC_ASSERT(MC_CMD_FREE_VIS_IN_LEN == 0);
644         EFX_STATIC_ASSERT(MC_CMD_FREE_VIS_OUT_LEN == 0);
645
646         req.emr_cmd = MC_CMD_FREE_VIS;
647         req.emr_in_buf = NULL;
648         req.emr_in_length = 0;
649         req.emr_out_buf = NULL;
650         req.emr_out_length = 0;
651
652         efx_mcdi_execute_quiet(enp, &req);
653
654         /* Ignore ELREADY (no allocated VIs, so nothing to free) */
655         if ((req.emr_rc != 0) && (req.emr_rc != EALREADY)) {
656                 rc = req.emr_rc;
657                 goto fail1;
658         }
659
660         return (0);
661
662 fail1:
663         EFSYS_PROBE1(fail1, efx_rc_t, rc);
664
665         return (rc);
666 }
667
668
669 static  __checkReturn   efx_rc_t
670 efx_mcdi_alloc_piobuf(
671         __in            efx_nic_t *enp,
672         __out           efx_piobuf_handle_t *handlep)
673 {
674         efx_mcdi_req_t req;
675         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_ALLOC_PIOBUF_IN_LEN,
676                 MC_CMD_ALLOC_PIOBUF_OUT_LEN);
677         efx_rc_t rc;
678
679         if (handlep == NULL) {
680                 rc = EINVAL;
681                 goto fail1;
682         }
683
684         req.emr_cmd = MC_CMD_ALLOC_PIOBUF;
685         req.emr_in_buf = payload;
686         req.emr_in_length = MC_CMD_ALLOC_PIOBUF_IN_LEN;
687         req.emr_out_buf = payload;
688         req.emr_out_length = MC_CMD_ALLOC_PIOBUF_OUT_LEN;
689
690         efx_mcdi_execute_quiet(enp, &req);
691
692         if (req.emr_rc != 0) {
693                 rc = req.emr_rc;
694                 goto fail2;
695         }
696
697         if (req.emr_out_length_used < MC_CMD_ALLOC_PIOBUF_OUT_LEN) {
698                 rc = EMSGSIZE;
699                 goto fail3;
700         }
701
702         *handlep = MCDI_OUT_DWORD(req, ALLOC_PIOBUF_OUT_PIOBUF_HANDLE);
703
704         return (0);
705
706 fail3:
707         EFSYS_PROBE(fail3);
708 fail2:
709         EFSYS_PROBE(fail2);
710 fail1:
711         EFSYS_PROBE1(fail1, efx_rc_t, rc);
712
713         return (rc);
714 }
715
716 static  __checkReturn   efx_rc_t
717 efx_mcdi_free_piobuf(
718         __in            efx_nic_t *enp,
719         __in            efx_piobuf_handle_t handle)
720 {
721         efx_mcdi_req_t req;
722         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_FREE_PIOBUF_IN_LEN,
723                 MC_CMD_FREE_PIOBUF_OUT_LEN);
724         efx_rc_t rc;
725
726         req.emr_cmd = MC_CMD_FREE_PIOBUF;
727         req.emr_in_buf = payload;
728         req.emr_in_length = MC_CMD_FREE_PIOBUF_IN_LEN;
729         req.emr_out_buf = payload;
730         req.emr_out_length = MC_CMD_FREE_PIOBUF_OUT_LEN;
731
732         MCDI_IN_SET_DWORD(req, FREE_PIOBUF_IN_PIOBUF_HANDLE, handle);
733
734         efx_mcdi_execute_quiet(enp, &req);
735
736         if (req.emr_rc != 0) {
737                 rc = req.emr_rc;
738                 goto fail1;
739         }
740
741         return (0);
742
743 fail1:
744         EFSYS_PROBE1(fail1, efx_rc_t, rc);
745
746         return (rc);
747 }
748
749 static  __checkReturn   efx_rc_t
750 efx_mcdi_link_piobuf(
751         __in            efx_nic_t *enp,
752         __in            uint32_t vi_index,
753         __in            efx_piobuf_handle_t handle)
754 {
755         efx_mcdi_req_t req;
756         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_LINK_PIOBUF_IN_LEN,
757                 MC_CMD_LINK_PIOBUF_OUT_LEN);
758         efx_rc_t rc;
759
760         req.emr_cmd = MC_CMD_LINK_PIOBUF;
761         req.emr_in_buf = payload;
762         req.emr_in_length = MC_CMD_LINK_PIOBUF_IN_LEN;
763         req.emr_out_buf = payload;
764         req.emr_out_length = MC_CMD_LINK_PIOBUF_OUT_LEN;
765
766         MCDI_IN_SET_DWORD(req, LINK_PIOBUF_IN_PIOBUF_HANDLE, handle);
767         MCDI_IN_SET_DWORD(req, LINK_PIOBUF_IN_TXQ_INSTANCE, vi_index);
768
769         efx_mcdi_execute(enp, &req);
770
771         if (req.emr_rc != 0) {
772                 rc = req.emr_rc;
773                 goto fail1;
774         }
775
776         return (0);
777
778 fail1:
779         EFSYS_PROBE1(fail1, efx_rc_t, rc);
780
781         return (rc);
782 }
783
784 static  __checkReturn   efx_rc_t
785 efx_mcdi_unlink_piobuf(
786         __in            efx_nic_t *enp,
787         __in            uint32_t vi_index)
788 {
789         efx_mcdi_req_t req;
790         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_UNLINK_PIOBUF_IN_LEN,
791                 MC_CMD_UNLINK_PIOBUF_OUT_LEN);
792         efx_rc_t rc;
793
794         req.emr_cmd = MC_CMD_UNLINK_PIOBUF;
795         req.emr_in_buf = payload;
796         req.emr_in_length = MC_CMD_UNLINK_PIOBUF_IN_LEN;
797         req.emr_out_buf = payload;
798         req.emr_out_length = MC_CMD_UNLINK_PIOBUF_OUT_LEN;
799
800         MCDI_IN_SET_DWORD(req, UNLINK_PIOBUF_IN_TXQ_INSTANCE, vi_index);
801
802         efx_mcdi_execute_quiet(enp, &req);
803
804         if (req.emr_rc != 0) {
805                 rc = req.emr_rc;
806                 goto fail1;
807         }
808
809         return (0);
810
811 fail1:
812         EFSYS_PROBE1(fail1, efx_rc_t, rc);
813
814         return (rc);
815 }
816
817 static                  void
818 ef10_nic_alloc_piobufs(
819         __in            efx_nic_t *enp,
820         __in            uint32_t max_piobuf_count)
821 {
822         efx_piobuf_handle_t *handlep;
823         unsigned int i;
824
825         EFSYS_ASSERT3U(max_piobuf_count, <=,
826             EFX_ARRAY_SIZE(enp->en_arch.ef10.ena_piobuf_handle));
827
828         enp->en_arch.ef10.ena_piobuf_count = 0;
829
830         for (i = 0; i < max_piobuf_count; i++) {
831                 handlep = &enp->en_arch.ef10.ena_piobuf_handle[i];
832
833                 if (efx_mcdi_alloc_piobuf(enp, handlep) != 0)
834                         goto fail1;
835
836                 enp->en_arch.ef10.ena_pio_alloc_map[i] = 0;
837                 enp->en_arch.ef10.ena_piobuf_count++;
838         }
839
840         return;
841
842 fail1:
843         for (i = 0; i < enp->en_arch.ef10.ena_piobuf_count; i++) {
844                 handlep = &enp->en_arch.ef10.ena_piobuf_handle[i];
845
846                 (void) efx_mcdi_free_piobuf(enp, *handlep);
847                 *handlep = EFX_PIOBUF_HANDLE_INVALID;
848         }
849         enp->en_arch.ef10.ena_piobuf_count = 0;
850 }
851
852
853 static                  void
854 ef10_nic_free_piobufs(
855         __in            efx_nic_t *enp)
856 {
857         efx_piobuf_handle_t *handlep;
858         unsigned int i;
859
860         for (i = 0; i < enp->en_arch.ef10.ena_piobuf_count; i++) {
861                 handlep = &enp->en_arch.ef10.ena_piobuf_handle[i];
862
863                 (void) efx_mcdi_free_piobuf(enp, *handlep);
864                 *handlep = EFX_PIOBUF_HANDLE_INVALID;
865         }
866         enp->en_arch.ef10.ena_piobuf_count = 0;
867 }
868
869 /* Sub-allocate a block from a piobuf */
870         __checkReturn   efx_rc_t
871 ef10_nic_pio_alloc(
872         __inout         efx_nic_t *enp,
873         __out           uint32_t *bufnump,
874         __out           efx_piobuf_handle_t *handlep,
875         __out           uint32_t *blknump,
876         __out           uint32_t *offsetp,
877         __out           size_t *sizep)
878 {
879         efx_nic_cfg_t *encp = &enp->en_nic_cfg;
880         efx_drv_cfg_t *edcp = &enp->en_drv_cfg;
881         uint32_t blk_per_buf;
882         uint32_t buf, blk;
883         efx_rc_t rc;
884
885         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
886         EFSYS_ASSERT(bufnump);
887         EFSYS_ASSERT(handlep);
888         EFSYS_ASSERT(blknump);
889         EFSYS_ASSERT(offsetp);
890         EFSYS_ASSERT(sizep);
891
892         if ((edcp->edc_pio_alloc_size == 0) ||
893             (enp->en_arch.ef10.ena_piobuf_count == 0)) {
894                 rc = ENOMEM;
895                 goto fail1;
896         }
897         blk_per_buf = encp->enc_piobuf_size / edcp->edc_pio_alloc_size;
898
899         for (buf = 0; buf < enp->en_arch.ef10.ena_piobuf_count; buf++) {
900                 uint32_t *map = &enp->en_arch.ef10.ena_pio_alloc_map[buf];
901
902                 if (~(*map) == 0)
903                         continue;
904
905                 EFSYS_ASSERT3U(blk_per_buf, <=, (8 * sizeof (*map)));
906                 for (blk = 0; blk < blk_per_buf; blk++) {
907                         if ((*map & (1u << blk)) == 0) {
908                                 *map |= (1u << blk);
909                                 goto done;
910                         }
911                 }
912         }
913         rc = ENOMEM;
914         goto fail2;
915
916 done:
917         *handlep = enp->en_arch.ef10.ena_piobuf_handle[buf];
918         *bufnump = buf;
919         *blknump = blk;
920         *sizep = edcp->edc_pio_alloc_size;
921         *offsetp = blk * (*sizep);
922
923         return (0);
924
925 fail2:
926         EFSYS_PROBE(fail2);
927 fail1:
928         EFSYS_PROBE1(fail1, efx_rc_t, rc);
929
930         return (rc);
931 }
932
933 /* Free a piobuf sub-allocated block */
934         __checkReturn   efx_rc_t
935 ef10_nic_pio_free(
936         __inout         efx_nic_t *enp,
937         __in            uint32_t bufnum,
938         __in            uint32_t blknum)
939 {
940         uint32_t *map;
941         efx_rc_t rc;
942
943         if ((bufnum >= enp->en_arch.ef10.ena_piobuf_count) ||
944             (blknum >= (8 * sizeof (*map)))) {
945                 rc = EINVAL;
946                 goto fail1;
947         }
948
949         map = &enp->en_arch.ef10.ena_pio_alloc_map[bufnum];
950         if ((*map & (1u << blknum)) == 0) {
951                 rc = ENOENT;
952                 goto fail2;
953         }
954         *map &= ~(1u << blknum);
955
956         return (0);
957
958 fail2:
959         EFSYS_PROBE(fail2);
960 fail1:
961         EFSYS_PROBE1(fail1, efx_rc_t, rc);
962
963         return (rc);
964 }
965
966         __checkReturn   efx_rc_t
967 ef10_nic_pio_link(
968         __inout         efx_nic_t *enp,
969         __in            uint32_t vi_index,
970         __in            efx_piobuf_handle_t handle)
971 {
972         return (efx_mcdi_link_piobuf(enp, vi_index, handle));
973 }
974
975         __checkReturn   efx_rc_t
976 ef10_nic_pio_unlink(
977         __inout         efx_nic_t *enp,
978         __in            uint32_t vi_index)
979 {
980         return (efx_mcdi_unlink_piobuf(enp, vi_index));
981 }
982
983 static  __checkReturn   efx_rc_t
984 ef10_mcdi_get_pf_count(
985         __in            efx_nic_t *enp,
986         __out           uint32_t *pf_countp)
987 {
988         efx_mcdi_req_t req;
989         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_GET_PF_COUNT_IN_LEN,
990                 MC_CMD_GET_PF_COUNT_OUT_LEN);
991         efx_rc_t rc;
992
993         req.emr_cmd = MC_CMD_GET_PF_COUNT;
994         req.emr_in_buf = payload;
995         req.emr_in_length = MC_CMD_GET_PF_COUNT_IN_LEN;
996         req.emr_out_buf = payload;
997         req.emr_out_length = MC_CMD_GET_PF_COUNT_OUT_LEN;
998
999         efx_mcdi_execute(enp, &req);
1000
1001         if (req.emr_rc != 0) {
1002                 rc = req.emr_rc;
1003                 goto fail1;
1004         }
1005
1006         if (req.emr_out_length_used < MC_CMD_GET_PF_COUNT_OUT_LEN) {
1007                 rc = EMSGSIZE;
1008                 goto fail2;
1009         }
1010
1011         *pf_countp = *MCDI_OUT(req, uint8_t,
1012                                 MC_CMD_GET_PF_COUNT_OUT_PF_COUNT_OFST);
1013
1014         EFSYS_ASSERT(*pf_countp != 0);
1015
1016         return (0);
1017
1018 fail2:
1019         EFSYS_PROBE(fail2);
1020 fail1:
1021         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1022
1023         return (rc);
1024 }
1025
1026 static  __checkReturn   efx_rc_t
1027 ef10_get_datapath_caps(
1028         __in            efx_nic_t *enp)
1029 {
1030         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1031         efx_mcdi_req_t req;
1032         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_GET_CAPABILITIES_IN_LEN,
1033                 MC_CMD_GET_CAPABILITIES_V5_OUT_LEN);
1034         efx_rc_t rc;
1035
1036         if ((rc = ef10_mcdi_get_pf_count(enp, &encp->enc_hw_pf_count)) != 0)
1037                 goto fail1;
1038
1039
1040         req.emr_cmd = MC_CMD_GET_CAPABILITIES;
1041         req.emr_in_buf = payload;
1042         req.emr_in_length = MC_CMD_GET_CAPABILITIES_IN_LEN;
1043         req.emr_out_buf = payload;
1044         req.emr_out_length = MC_CMD_GET_CAPABILITIES_V5_OUT_LEN;
1045
1046         efx_mcdi_execute_quiet(enp, &req);
1047
1048         if (req.emr_rc != 0) {
1049                 rc = req.emr_rc;
1050                 goto fail2;
1051         }
1052
1053         if (req.emr_out_length_used < MC_CMD_GET_CAPABILITIES_OUT_LEN) {
1054                 rc = EMSGSIZE;
1055                 goto fail3;
1056         }
1057
1058 #define CAP_FLAGS1(_req, _flag)                                         \
1059         (MCDI_OUT_DWORD((_req), GET_CAPABILITIES_OUT_FLAGS1) &          \
1060         (1u << (MC_CMD_GET_CAPABILITIES_V2_OUT_ ## _flag ## _LBN)))
1061
1062 #define CAP_FLAGS2(_req, _flag)                                         \
1063         (((_req).emr_out_length_used >= MC_CMD_GET_CAPABILITIES_V2_OUT_LEN) && \
1064             (MCDI_OUT_DWORD((_req), GET_CAPABILITIES_V2_OUT_FLAGS2) &   \
1065             (1u << (MC_CMD_GET_CAPABILITIES_V2_OUT_ ## _flag ## _LBN))))
1066
1067         /*
1068          * Huntington RXDP firmware inserts a 0 or 14 byte prefix.
1069          * We only support the 14 byte prefix here.
1070          */
1071         if (CAP_FLAGS1(req, RX_PREFIX_LEN_14) == 0) {
1072                 rc = ENOTSUP;
1073                 goto fail4;
1074         }
1075         encp->enc_rx_prefix_size = 14;
1076
1077 #if EFSYS_OPT_RX_SCALE
1078         /* Check if the firmware supports additional RSS modes */
1079         if (CAP_FLAGS1(req, ADDITIONAL_RSS_MODES))
1080                 encp->enc_rx_scale_additional_modes_supported = B_TRUE;
1081         else
1082                 encp->enc_rx_scale_additional_modes_supported = B_FALSE;
1083 #endif /* EFSYS_OPT_RX_SCALE */
1084
1085         /* Check if the firmware supports TSO */
1086         if (CAP_FLAGS1(req, TX_TSO))
1087                 encp->enc_fw_assisted_tso_enabled = B_TRUE;
1088         else
1089                 encp->enc_fw_assisted_tso_enabled = B_FALSE;
1090
1091         /* Check if the firmware supports FATSOv2 */
1092         if (CAP_FLAGS2(req, TX_TSO_V2)) {
1093                 encp->enc_fw_assisted_tso_v2_enabled = B_TRUE;
1094                 encp->enc_fw_assisted_tso_v2_n_contexts = MCDI_OUT_WORD(req,
1095                     GET_CAPABILITIES_V2_OUT_TX_TSO_V2_N_CONTEXTS);
1096         } else {
1097                 encp->enc_fw_assisted_tso_v2_enabled = B_FALSE;
1098                 encp->enc_fw_assisted_tso_v2_n_contexts = 0;
1099         }
1100
1101         /* Check if the firmware supports FATSOv2 encap */
1102         if (CAP_FLAGS2(req, TX_TSO_V2_ENCAP))
1103                 encp->enc_fw_assisted_tso_v2_encap_enabled = B_TRUE;
1104         else
1105                 encp->enc_fw_assisted_tso_v2_encap_enabled = B_FALSE;
1106
1107         /* Check if the firmware has vadapter/vport/vswitch support */
1108         if (CAP_FLAGS1(req, EVB))
1109                 encp->enc_datapath_cap_evb = B_TRUE;
1110         else
1111                 encp->enc_datapath_cap_evb = B_FALSE;
1112
1113         /* Check if the firmware supports VLAN insertion */
1114         if (CAP_FLAGS1(req, TX_VLAN_INSERTION))
1115                 encp->enc_hw_tx_insert_vlan_enabled = B_TRUE;
1116         else
1117                 encp->enc_hw_tx_insert_vlan_enabled = B_FALSE;
1118
1119         /* Check if the firmware supports RX event batching */
1120         if (CAP_FLAGS1(req, RX_BATCHING))
1121                 encp->enc_rx_batching_enabled = B_TRUE;
1122         else
1123                 encp->enc_rx_batching_enabled = B_FALSE;
1124
1125         /*
1126          * Even if batching isn't reported as supported, we may still get
1127          * batched events (see bug61153).
1128          */
1129         encp->enc_rx_batch_max = 16;
1130
1131         /* Check if the firmware supports disabling scatter on RXQs */
1132         if (CAP_FLAGS1(req, RX_DISABLE_SCATTER))
1133                 encp->enc_rx_disable_scatter_supported = B_TRUE;
1134         else
1135                 encp->enc_rx_disable_scatter_supported = B_FALSE;
1136
1137         /* Check if the firmware supports packed stream mode */
1138         if (CAP_FLAGS1(req, RX_PACKED_STREAM))
1139                 encp->enc_rx_packed_stream_supported = B_TRUE;
1140         else
1141                 encp->enc_rx_packed_stream_supported = B_FALSE;
1142
1143         /*
1144          * Check if the firmware supports configurable buffer sizes
1145          * for packed stream mode (otherwise buffer size is 1Mbyte)
1146          */
1147         if (CAP_FLAGS1(req, RX_PACKED_STREAM_VAR_BUFFERS))
1148                 encp->enc_rx_var_packed_stream_supported = B_TRUE;
1149         else
1150                 encp->enc_rx_var_packed_stream_supported = B_FALSE;
1151
1152         /* Check if the firmware supports equal stride super-buffer mode */
1153         if (CAP_FLAGS2(req, EQUAL_STRIDE_SUPER_BUFFER))
1154                 encp->enc_rx_es_super_buffer_supported = B_TRUE;
1155         else
1156                 encp->enc_rx_es_super_buffer_supported = B_FALSE;
1157
1158         /* Check if the firmware supports FW subvariant w/o Tx checksumming */
1159         if (CAP_FLAGS2(req, FW_SUBVARIANT_NO_TX_CSUM))
1160                 encp->enc_fw_subvariant_no_tx_csum_supported = B_TRUE;
1161         else
1162                 encp->enc_fw_subvariant_no_tx_csum_supported = B_FALSE;
1163
1164         /* Check if the firmware supports set mac with running filters */
1165         if (CAP_FLAGS1(req, VADAPTOR_PERMIT_SET_MAC_WHEN_FILTERS_INSTALLED))
1166                 encp->enc_allow_set_mac_with_installed_filters = B_TRUE;
1167         else
1168                 encp->enc_allow_set_mac_with_installed_filters = B_FALSE;
1169
1170         /*
1171          * Check if firmware supports the extended MC_CMD_SET_MAC, which allows
1172          * specifying which parameters to configure.
1173          */
1174         if (CAP_FLAGS1(req, SET_MAC_ENHANCED))
1175                 encp->enc_enhanced_set_mac_supported = B_TRUE;
1176         else
1177                 encp->enc_enhanced_set_mac_supported = B_FALSE;
1178
1179         /*
1180          * Check if firmware supports version 2 of MC_CMD_INIT_EVQ, which allows
1181          * us to let the firmware choose the settings to use on an EVQ.
1182          */
1183         if (CAP_FLAGS2(req, INIT_EVQ_V2))
1184                 encp->enc_init_evq_v2_supported = B_TRUE;
1185         else
1186                 encp->enc_init_evq_v2_supported = B_FALSE;
1187
1188         /*
1189          * Check if the NO_CONT_EV mode for RX events is supported.
1190          */
1191         if (CAP_FLAGS2(req, INIT_RXQ_NO_CONT_EV))
1192                 encp->enc_no_cont_ev_mode_supported = B_TRUE;
1193         else
1194                 encp->enc_no_cont_ev_mode_supported = B_FALSE;
1195
1196         /*
1197          * Check if buffer size may and must be specified on INIT_RXQ.
1198          * It may be always specified to efx_rx_qcreate(), but will be
1199          * just kept libefx internal if MCDI does not support it.
1200          */
1201         if (CAP_FLAGS2(req, INIT_RXQ_WITH_BUFFER_SIZE))
1202                 encp->enc_init_rxq_with_buffer_size = B_TRUE;
1203         else
1204                 encp->enc_init_rxq_with_buffer_size = B_FALSE;
1205
1206         /*
1207          * Check if firmware-verified NVRAM updates must be used.
1208          *
1209          * The firmware trusted installer requires all NVRAM updates to use
1210          * version 2 of MC_CMD_NVRAM_UPDATE_START (to enable verified update)
1211          * and version 2 of MC_CMD_NVRAM_UPDATE_FINISH (to verify the updated
1212          * partition and report the result).
1213          */
1214         if (CAP_FLAGS2(req, NVRAM_UPDATE_REPORT_VERIFY_RESULT))
1215                 encp->enc_nvram_update_verify_result_supported = B_TRUE;
1216         else
1217                 encp->enc_nvram_update_verify_result_supported = B_FALSE;
1218
1219         if (CAP_FLAGS2(req, NVRAM_UPDATE_POLL_VERIFY_RESULT))
1220                 encp->enc_nvram_update_poll_verify_result_supported = B_TRUE;
1221         else
1222                 encp->enc_nvram_update_poll_verify_result_supported = B_FALSE;
1223
1224         /*
1225          * Check if firmware update via the BUNDLE partition is supported
1226          */
1227         if (CAP_FLAGS2(req, BUNDLE_UPDATE))
1228                 encp->enc_nvram_bundle_update_supported = B_TRUE;
1229         else
1230                 encp->enc_nvram_bundle_update_supported = B_FALSE;
1231
1232         /*
1233          * Check if firmware provides packet memory and Rx datapath
1234          * counters.
1235          */
1236         if (CAP_FLAGS1(req, PM_AND_RXDP_COUNTERS))
1237                 encp->enc_pm_and_rxdp_counters = B_TRUE;
1238         else
1239                 encp->enc_pm_and_rxdp_counters = B_FALSE;
1240
1241         /*
1242          * Check if the 40G MAC hardware is capable of reporting
1243          * statistics for Tx size bins.
1244          */
1245         if (CAP_FLAGS2(req, MAC_STATS_40G_TX_SIZE_BINS))
1246                 encp->enc_mac_stats_40g_tx_size_bins = B_TRUE;
1247         else
1248                 encp->enc_mac_stats_40g_tx_size_bins = B_FALSE;
1249
1250         /*
1251          * Check if firmware supports VXLAN and NVGRE tunnels.
1252          * The capability indicates Geneve protocol support as well.
1253          */
1254         if (CAP_FLAGS1(req, VXLAN_NVGRE)) {
1255                 encp->enc_tunnel_encapsulations_supported =
1256                     (1u << EFX_TUNNEL_PROTOCOL_VXLAN) |
1257                     (1u << EFX_TUNNEL_PROTOCOL_GENEVE) |
1258                     (1u << EFX_TUNNEL_PROTOCOL_NVGRE);
1259
1260                 EFX_STATIC_ASSERT(EFX_TUNNEL_MAXNENTRIES ==
1261                     MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_ENTRIES_MAXNUM);
1262                 encp->enc_tunnel_config_udp_entries_max =
1263                     EFX_TUNNEL_MAXNENTRIES;
1264         } else {
1265                 encp->enc_tunnel_config_udp_entries_max = 0;
1266         }
1267
1268         /*
1269          * Check if firmware reports the VI window mode.
1270          * Medford2 has a variable VI window size (8K, 16K or 64K).
1271          * Medford and Huntington have a fixed 8K VI window size.
1272          */
1273         if (req.emr_out_length_used >= MC_CMD_GET_CAPABILITIES_V3_OUT_LEN) {
1274                 uint8_t mode =
1275                     MCDI_OUT_BYTE(req, GET_CAPABILITIES_V3_OUT_VI_WINDOW_MODE);
1276
1277                 switch (mode) {
1278                 case MC_CMD_GET_CAPABILITIES_V3_OUT_VI_WINDOW_MODE_8K:
1279                         encp->enc_vi_window_shift = EFX_VI_WINDOW_SHIFT_8K;
1280                         break;
1281                 case MC_CMD_GET_CAPABILITIES_V3_OUT_VI_WINDOW_MODE_16K:
1282                         encp->enc_vi_window_shift = EFX_VI_WINDOW_SHIFT_16K;
1283                         break;
1284                 case MC_CMD_GET_CAPABILITIES_V3_OUT_VI_WINDOW_MODE_64K:
1285                         encp->enc_vi_window_shift = EFX_VI_WINDOW_SHIFT_64K;
1286                         break;
1287                 default:
1288                         encp->enc_vi_window_shift = EFX_VI_WINDOW_SHIFT_INVALID;
1289                         break;
1290                 }
1291         } else if ((enp->en_family == EFX_FAMILY_HUNTINGTON) ||
1292                     (enp->en_family == EFX_FAMILY_MEDFORD)) {
1293                 /* Huntington and Medford have fixed 8K window size */
1294                 encp->enc_vi_window_shift = EFX_VI_WINDOW_SHIFT_8K;
1295         } else {
1296                 encp->enc_vi_window_shift = EFX_VI_WINDOW_SHIFT_INVALID;
1297         }
1298
1299         /* Check if firmware supports extended MAC stats. */
1300         if (req.emr_out_length_used >= MC_CMD_GET_CAPABILITIES_V4_OUT_LEN) {
1301                 /* Extended stats buffer supported */
1302                 encp->enc_mac_stats_nstats = MCDI_OUT_WORD(req,
1303                     GET_CAPABILITIES_V4_OUT_MAC_STATS_NUM_STATS);
1304         } else {
1305                 /* Use Siena-compatible legacy MAC stats */
1306                 encp->enc_mac_stats_nstats = MC_CMD_MAC_NSTATS;
1307         }
1308
1309         if (encp->enc_mac_stats_nstats >= MC_CMD_MAC_NSTATS_V2)
1310                 encp->enc_fec_counters = B_TRUE;
1311         else
1312                 encp->enc_fec_counters = B_FALSE;
1313
1314         /* Check if the firmware provides head-of-line blocking counters */
1315         if (CAP_FLAGS2(req, RXDP_HLB_IDLE))
1316                 encp->enc_hlb_counters = B_TRUE;
1317         else
1318                 encp->enc_hlb_counters = B_FALSE;
1319
1320 #if EFSYS_OPT_RX_SCALE
1321         if (CAP_FLAGS1(req, RX_RSS_LIMITED)) {
1322                 /* Only one exclusive RSS context is available per port. */
1323                 encp->enc_rx_scale_max_exclusive_contexts = 1;
1324
1325                 switch (enp->en_family) {
1326                 case EFX_FAMILY_MEDFORD2:
1327                         encp->enc_rx_scale_hash_alg_mask =
1328                             (1U << EFX_RX_HASHALG_TOEPLITZ);
1329                         break;
1330
1331                 case EFX_FAMILY_MEDFORD:
1332                 case EFX_FAMILY_HUNTINGTON:
1333                         /*
1334                          * Packed stream firmware variant maintains a
1335                          * non-standard algorithm for hash computation.
1336                          * It implies explicit XORing together
1337                          * source + destination IP addresses (or last
1338                          * four bytes in the case of IPv6) and using the
1339                          * resulting value as the input to a Toeplitz hash.
1340                          */
1341                         encp->enc_rx_scale_hash_alg_mask =
1342                             (1U << EFX_RX_HASHALG_PACKED_STREAM);
1343                         break;
1344
1345                 default:
1346                         rc = EINVAL;
1347                         goto fail5;
1348                 }
1349
1350                 /* Port numbers cannot contribute to the hash value */
1351                 encp->enc_rx_scale_l4_hash_supported = B_FALSE;
1352         } else {
1353                 /*
1354                  * Maximum number of exclusive RSS contexts.
1355                  * EF10 hardware supports 64 in total, but 6 are reserved
1356                  * for shared contexts. They are a global resource so
1357                  * not all may be available.
1358                  */
1359                 encp->enc_rx_scale_max_exclusive_contexts = 64 - 6;
1360
1361                 encp->enc_rx_scale_hash_alg_mask =
1362                     (1U << EFX_RX_HASHALG_TOEPLITZ);
1363
1364                 /*
1365                  * It is possible to use port numbers as
1366                  * the input data for hash computation.
1367                  */
1368                 encp->enc_rx_scale_l4_hash_supported = B_TRUE;
1369         }
1370 #endif /* EFSYS_OPT_RX_SCALE */
1371
1372         /* Check if the firmware supports "FLAG" and "MARK" filter actions */
1373         if (CAP_FLAGS2(req, FILTER_ACTION_FLAG))
1374                 encp->enc_filter_action_flag_supported = B_TRUE;
1375         else
1376                 encp->enc_filter_action_flag_supported = B_FALSE;
1377
1378         if (CAP_FLAGS2(req, FILTER_ACTION_MARK))
1379                 encp->enc_filter_action_mark_supported = B_TRUE;
1380         else
1381                 encp->enc_filter_action_mark_supported = B_FALSE;
1382
1383         /* Get maximum supported value for "MARK" filter action */
1384         if (req.emr_out_length_used >= MC_CMD_GET_CAPABILITIES_V5_OUT_LEN)
1385                 encp->enc_filter_action_mark_max = MCDI_OUT_DWORD(req,
1386                     GET_CAPABILITIES_V5_OUT_FILTER_ACTION_MARK_MAX);
1387         else
1388                 encp->enc_filter_action_mark_max = 0;
1389
1390 #undef CAP_FLAGS1
1391 #undef CAP_FLAGS2
1392
1393         return (0);
1394
1395 #if EFSYS_OPT_RX_SCALE
1396 fail5:
1397         EFSYS_PROBE(fail5);
1398 #endif /* EFSYS_OPT_RX_SCALE */
1399 fail4:
1400         EFSYS_PROBE(fail4);
1401 fail3:
1402         EFSYS_PROBE(fail3);
1403 fail2:
1404         EFSYS_PROBE(fail2);
1405 fail1:
1406         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1407
1408         return (rc);
1409 }
1410
1411
1412 #define EF10_LEGACY_PF_PRIVILEGE_MASK                                   \
1413         (MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN                     |       \
1414         MC_CMD_PRIVILEGE_MASK_IN_GRP_LINK                       |       \
1415         MC_CMD_PRIVILEGE_MASK_IN_GRP_ONLOAD                     |       \
1416         MC_CMD_PRIVILEGE_MASK_IN_GRP_PTP                        |       \
1417         MC_CMD_PRIVILEGE_MASK_IN_GRP_INSECURE_FILTERS           |       \
1418         MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING               |       \
1419         MC_CMD_PRIVILEGE_MASK_IN_GRP_UNICAST                    |       \
1420         MC_CMD_PRIVILEGE_MASK_IN_GRP_MULTICAST                  |       \
1421         MC_CMD_PRIVILEGE_MASK_IN_GRP_BROADCAST                  |       \
1422         MC_CMD_PRIVILEGE_MASK_IN_GRP_ALL_MULTICAST              |       \
1423         MC_CMD_PRIVILEGE_MASK_IN_GRP_PROMISCUOUS)
1424
1425 #define EF10_LEGACY_VF_PRIVILEGE_MASK   0
1426
1427
1428         __checkReturn           efx_rc_t
1429 ef10_get_privilege_mask(
1430         __in                    efx_nic_t *enp,
1431         __out                   uint32_t *maskp)
1432 {
1433         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1434         uint32_t mask;
1435         efx_rc_t rc;
1436
1437         if ((rc = efx_mcdi_privilege_mask(enp, encp->enc_pf, encp->enc_vf,
1438                                             &mask)) != 0) {
1439                 if (rc != ENOTSUP)
1440                         goto fail1;
1441
1442                 /* Fallback for old firmware without privilege mask support */
1443                 if (EFX_PCI_FUNCTION_IS_PF(encp)) {
1444                         /* Assume PF has admin privilege */
1445                         mask = EF10_LEGACY_PF_PRIVILEGE_MASK;
1446                 } else {
1447                         /* VF is always unprivileged by default */
1448                         mask = EF10_LEGACY_VF_PRIVILEGE_MASK;
1449                 }
1450         }
1451
1452         *maskp = mask;
1453
1454         return (0);
1455
1456 fail1:
1457         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1458
1459         return (rc);
1460 }
1461
1462
1463 #define EFX_EXT_PORT_MAX        4
1464 #define EFX_EXT_PORT_NA         0xFF
1465
1466 /*
1467  * Table of mapping schemes from port number to external number.
1468  *
1469  * Each port number ultimately corresponds to a connector: either as part of
1470  * a cable assembly attached to a module inserted in an SFP+/QSFP+ cage on
1471  * the board, or fixed to the board (e.g. 10GBASE-T magjack on SFN5121T
1472  * "Salina"). In general:
1473  *
1474  * Port number (0-based)
1475  *     |
1476  *   port mapping (n:1)
1477  *     |
1478  *     v
1479  * External port number (1-based)
1480  *     |
1481  *   fixed (1:1) or cable assembly (1:m)
1482  *     |
1483  *     v
1484  * Connector
1485  *
1486  * The external numbering refers to the cages or magjacks on the board,
1487  * as visibly annotated on the board or back panel. This table describes
1488  * how to determine which external cage/magjack corresponds to the port
1489  * numbers used by the driver.
1490  *
1491  * The count of consecutive port numbers that map to each external number,
1492  * is determined by the chip family and the current port mode.
1493  *
1494  * For the Huntington family, the current port mode cannot be discovered,
1495  * but a single mapping is used by all modes for a given chip variant,
1496  * so the mapping used is instead the last match in the table to the full
1497  * set of port modes to which the NIC can be configured. Therefore the
1498  * ordering of entries in the mapping table is significant.
1499  */
1500 static struct ef10_external_port_map_s {
1501         efx_family_t    family;
1502         uint32_t        modes_mask;
1503         uint8_t         base_port[EFX_EXT_PORT_MAX];
1504 }       __ef10_external_port_mappings[] = {
1505         /*
1506          * Modes used by Huntington family controllers where each port
1507          * number maps to a separate cage.
1508          * SFN7x22F (Torino):
1509          *      port 0 -> cage 1
1510          *      port 1 -> cage 2
1511          * SFN7xx4F (Pavia):
1512          *      port 0 -> cage 1
1513          *      port 1 -> cage 2
1514          *      port 2 -> cage 3
1515          *      port 3 -> cage 4
1516          */
1517         {
1518                 EFX_FAMILY_HUNTINGTON,
1519                 (1U << TLV_PORT_MODE_10G) |                     /* mode 0 */
1520                 (1U << TLV_PORT_MODE_10G_10G) |                 /* mode 2 */
1521                 (1U << TLV_PORT_MODE_10G_10G_10G_10G),          /* mode 4 */
1522                 { 0, 1, 2, 3 }
1523         },
1524         /*
1525          * Modes which for Huntington identify a chip variant where 2
1526          * adjacent port numbers map to each cage.
1527          * SFN7x42Q (Monza):
1528          *      port 0 -> cage 1
1529          *      port 1 -> cage 1
1530          *      port 2 -> cage 2
1531          *      port 3 -> cage 2
1532          */
1533         {
1534                 EFX_FAMILY_HUNTINGTON,
1535                 (1U << TLV_PORT_MODE_40G) |                     /* mode 1 */
1536                 (1U << TLV_PORT_MODE_40G_40G) |                 /* mode 3 */
1537                 (1U << TLV_PORT_MODE_40G_10G_10G) |             /* mode 6 */
1538                 (1U << TLV_PORT_MODE_10G_10G_40G),              /* mode 7 */
1539                 { 0, 2, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA }
1540         },
1541         /*
1542          * Modes that on Medford allocate each port number to a separate
1543          * cage.
1544          *      port 0 -> cage 1
1545          *      port 1 -> cage 2
1546          *      port 2 -> cage 3
1547          *      port 3 -> cage 4
1548          */
1549         {
1550                 EFX_FAMILY_MEDFORD,
1551                 (1U << TLV_PORT_MODE_1x1_NA) |                  /* mode 0 */
1552                 (1U << TLV_PORT_MODE_1x4_NA) |                  /* mode 1 */
1553                 (1U << TLV_PORT_MODE_1x1_1x1),                  /* mode 2 */
1554                 { 0, 1, 2, 3 }
1555         },
1556         /*
1557          * Modes that on Medford allocate 2 adjacent port numbers to each
1558          * cage.
1559          *      port 0 -> cage 1
1560          *      port 1 -> cage 1
1561          *      port 2 -> cage 2
1562          *      port 3 -> cage 2
1563          */
1564         {
1565                 EFX_FAMILY_MEDFORD,
1566                 (1U << TLV_PORT_MODE_1x4_1x4) |                 /* mode 3 */
1567                 (1U << TLV_PORT_MODE_2x1_2x1) |                 /* mode 5 */
1568                 (1U << TLV_PORT_MODE_1x4_2x1) |                 /* mode 6 */
1569                 (1U << TLV_PORT_MODE_2x1_1x4) |                 /* mode 7 */
1570                 /* Do not use 10G_10G_10G_10G_Q1_Q2 (see bug63270) */
1571                 (1U << TLV_PORT_MODE_10G_10G_10G_10G_Q1_Q2),    /* mode 9 */
1572                 { 0, 2, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA }
1573         },
1574         /*
1575          * Modes that on Medford allocate 4 adjacent port numbers to
1576          * cage 1.
1577          *      port 0 -> cage 1
1578          *      port 1 -> cage 1
1579          *      port 2 -> cage 1
1580          *      port 3 -> cage 1
1581          */
1582         {
1583                 EFX_FAMILY_MEDFORD,
1584                 /* Do not use 10G_10G_10G_10G_Q1 (see bug63270) */
1585                 (1U << TLV_PORT_MODE_4x1_NA),                   /* mode 4 */
1586                 { 0, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA }
1587         },
1588         /*
1589          * Modes that on Medford allocate 4 adjacent port numbers to
1590          * cage 2.
1591          *      port 0 -> cage 2
1592          *      port 1 -> cage 2
1593          *      port 2 -> cage 2
1594          *      port 3 -> cage 2
1595          */
1596         {
1597                 EFX_FAMILY_MEDFORD,
1598                 (1U << TLV_PORT_MODE_NA_4x1),                   /* mode 8 */
1599                 { EFX_EXT_PORT_NA, 0, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA }
1600         },
1601         /*
1602          * Modes that on Medford2 allocate each port number to a separate
1603          * cage.
1604          *      port 0 -> cage 1
1605          *      port 1 -> cage 2
1606          *      port 2 -> cage 3
1607          *      port 3 -> cage 4
1608          */
1609         {
1610                 EFX_FAMILY_MEDFORD2,
1611                 (1U << TLV_PORT_MODE_1x1_NA) |                  /* mode 0 */
1612                 (1U << TLV_PORT_MODE_1x4_NA) |                  /* mode 1 */
1613                 (1U << TLV_PORT_MODE_1x1_1x1) |                 /* mode 2 */
1614                 (1U << TLV_PORT_MODE_1x4_1x4) |                 /* mode 3 */
1615                 (1U << TLV_PORT_MODE_1x2_NA) |                  /* mode 10 */
1616                 (1U << TLV_PORT_MODE_1x2_1x2) |                 /* mode 12 */
1617                 (1U << TLV_PORT_MODE_1x4_1x2) |                 /* mode 15 */
1618                 (1U << TLV_PORT_MODE_1x2_1x4),                  /* mode 16 */
1619                 { 0, 1, 2, 3 }
1620         },
1621         /*
1622          * Modes that on Medford2 allocate 1 port to cage 1 and the rest
1623          * to cage 2.
1624          *      port 0 -> cage 1
1625          *      port 1 -> cage 2
1626          *      port 2 -> cage 2
1627          */
1628         {
1629                 EFX_FAMILY_MEDFORD2,
1630                 (1U << TLV_PORT_MODE_1x2_2x1) |                 /* mode 17 */
1631                 (1U << TLV_PORT_MODE_1x4_2x1),                  /* mode 6 */
1632                 { 0, 1, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA }
1633         },
1634         /*
1635          * Modes that on Medford2 allocate 2 adjacent port numbers to cage 1
1636          * and the rest to cage 2.
1637          *      port 0 -> cage 1
1638          *      port 1 -> cage 1
1639          *      port 2 -> cage 2
1640          *      port 3 -> cage 2
1641          */
1642         {
1643                 EFX_FAMILY_MEDFORD2,
1644                 (1U << TLV_PORT_MODE_2x1_2x1) |                 /* mode 4 */
1645                 (1U << TLV_PORT_MODE_2x1_1x4) |                 /* mode 7 */
1646                 (1U << TLV_PORT_MODE_2x2_NA) |                  /* mode 13 */
1647                 (1U << TLV_PORT_MODE_2x1_1x2),                  /* mode 18 */
1648                 { 0, 2, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA }
1649         },
1650         /*
1651          * Modes that on Medford2 allocate up to 4 adjacent port numbers
1652          * to cage 1.
1653          *      port 0 -> cage 1
1654          *      port 1 -> cage 1
1655          *      port 2 -> cage 1
1656          *      port 3 -> cage 1
1657          */
1658         {
1659                 EFX_FAMILY_MEDFORD2,
1660                 (1U << TLV_PORT_MODE_4x1_NA),                   /* mode 5 */
1661                 { 0, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA }
1662         },
1663         /*
1664          * Modes that on Medford2 allocate up to 4 adjacent port numbers
1665          * to cage 2.
1666          *      port 0 -> cage 2
1667          *      port 1 -> cage 2
1668          *      port 2 -> cage 2
1669          *      port 3 -> cage 2
1670          */
1671         {
1672                 EFX_FAMILY_MEDFORD2,
1673                 (1U << TLV_PORT_MODE_NA_4x1) |                  /* mode 8 */
1674                 (1U << TLV_PORT_MODE_NA_1x2) |                  /* mode 11 */
1675                 (1U << TLV_PORT_MODE_NA_2x2),                   /* mode 14 */
1676                 { EFX_EXT_PORT_NA, 0, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA }
1677         },
1678 };
1679
1680 static  __checkReturn   efx_rc_t
1681 ef10_external_port_mapping(
1682         __in            efx_nic_t *enp,
1683         __in            uint32_t port,
1684         __out           uint8_t *external_portp)
1685 {
1686         efx_rc_t rc;
1687         int i;
1688         uint32_t port_modes;
1689         uint32_t matches;
1690         uint32_t current;
1691         struct ef10_external_port_map_s *mapp = NULL;
1692         int ext_index = port; /* Default 1-1 mapping */
1693
1694         if ((rc = efx_mcdi_get_port_modes(enp, &port_modes, &current,
1695                     NULL)) != 0) {
1696                 /*
1697                  * No current port mode information (i.e. Huntington)
1698                  * - infer mapping from available modes
1699                  */
1700                 if ((rc = efx_mcdi_get_port_modes(enp,
1701                             &port_modes, NULL, NULL)) != 0) {
1702                         /*
1703                          * No port mode information available
1704                          * - use default mapping
1705                          */
1706                         goto out;
1707                 }
1708         } else {
1709                 /* Only need to scan the current mode */
1710                 port_modes = 1 << current;
1711         }
1712
1713         /*
1714          * Infer the internal port -> external number mapping from
1715          * the possible port modes for this NIC.
1716          */
1717         for (i = 0; i < EFX_ARRAY_SIZE(__ef10_external_port_mappings); ++i) {
1718                 struct ef10_external_port_map_s *eepmp =
1719                     &__ef10_external_port_mappings[i];
1720                 if (eepmp->family != enp->en_family)
1721                         continue;
1722                 matches = (eepmp->modes_mask & port_modes);
1723                 if (matches != 0) {
1724                         /*
1725                          * Some modes match. For some Huntington boards
1726                          * there will be multiple matches. The mapping on the
1727                          * last match is used.
1728                          */
1729                         mapp = eepmp;
1730                         port_modes &= ~matches;
1731                 }
1732         }
1733
1734         if (port_modes != 0) {
1735                 /* Some advertised modes are not supported */
1736                 rc = ENOTSUP;
1737                 goto fail1;
1738         }
1739
1740 out:
1741         if (mapp != NULL) {
1742                 /*
1743                  * External ports are assigned a sequence of consecutive
1744                  * port numbers, so find the one with the closest base_port.
1745                  */
1746                 uint32_t delta = EFX_EXT_PORT_NA;
1747
1748                 for (i = 0; i < EFX_EXT_PORT_MAX; i++) {
1749                         uint32_t base = mapp->base_port[i];
1750                         if ((base != EFX_EXT_PORT_NA) && (base <= port)) {
1751                                 if ((port - base) < delta) {
1752                                         delta = (port - base);
1753                                         ext_index = i;
1754                                 }
1755                         }
1756                 }
1757         }
1758         *external_portp = (uint8_t)(ext_index + 1);
1759
1760         return (0);
1761
1762 fail1:
1763         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1764
1765         return (rc);
1766 }
1767
1768 static  __checkReturn   efx_rc_t
1769 ef10_set_workaround_bug26807(
1770         __in            efx_nic_t *enp)
1771 {
1772         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1773         uint32_t flags;
1774         efx_rc_t rc;
1775
1776         /*
1777          * If the bug26807 workaround is enabled, then firmware has enabled
1778          * support for chained multicast filters. Firmware will reset (FLR)
1779          * functions which have filters in the hardware filter table when the
1780          * workaround is enabled/disabled.
1781          *
1782          * We must recheck if the workaround is enabled after inserting the
1783          * first hardware filter, in case it has been changed since this check.
1784          */
1785         rc = efx_mcdi_set_workaround(enp, MC_CMD_WORKAROUND_BUG26807,
1786             B_TRUE, &flags);
1787         if (rc == 0) {
1788                 encp->enc_bug26807_workaround = B_TRUE;
1789                 if (flags & (1 << MC_CMD_WORKAROUND_EXT_OUT_FLR_DONE_LBN)) {
1790                         /*
1791                          * Other functions had installed filters before the
1792                          * workaround was enabled, and they have been reset
1793                          * by firmware.
1794                          */
1795                         EFSYS_PROBE(bug26807_workaround_flr_done);
1796                         /* FIXME: bump MC warm boot count ? */
1797                 }
1798         } else if (rc == EACCES) {
1799                 /*
1800                  * Unprivileged functions cannot enable the workaround in older
1801                  * firmware.
1802                  */
1803                 encp->enc_bug26807_workaround = B_FALSE;
1804         } else if ((rc == ENOTSUP) || (rc == ENOENT)) {
1805                 encp->enc_bug26807_workaround = B_FALSE;
1806         } else {
1807                 goto fail1;
1808         }
1809
1810         return (0);
1811
1812 fail1:
1813         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1814
1815         return (rc);
1816 }
1817
1818 static  __checkReturn   efx_rc_t
1819 ef10_nic_board_cfg(
1820         __in            efx_nic_t *enp)
1821 {
1822         const efx_nic_ops_t *enop = enp->en_enop;
1823         efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
1824         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1825         ef10_link_state_t els;
1826         efx_port_t *epp = &(enp->en_port);
1827         uint32_t board_type = 0;
1828         uint32_t base, nvec;
1829         uint32_t port;
1830         uint32_t mask;
1831         uint32_t pf;
1832         uint32_t vf;
1833         uint8_t mac_addr[6] = { 0 };
1834         efx_rc_t rc;
1835
1836         /* Get the (zero-based) MCDI port number */
1837         if ((rc = efx_mcdi_get_port_assignment(enp, &port)) != 0)
1838                 goto fail1;
1839
1840         /* EFX MCDI interface uses one-based port numbers */
1841         emip->emi_port = port + 1;
1842
1843         encp->enc_assigned_port = port;
1844
1845         if ((rc = ef10_external_port_mapping(enp, port,
1846                     &encp->enc_external_port)) != 0)
1847                 goto fail2;
1848
1849         /*
1850          * Get PCIe function number from firmware (used for
1851          * per-function privilege and dynamic config info).
1852          *  - PCIe PF: pf = PF number, vf = 0xffff.
1853          *  - PCIe VF: pf = parent PF, vf = VF number.
1854          */
1855         if ((rc = efx_mcdi_get_function_info(enp, &pf, &vf)) != 0)
1856                 goto fail3;
1857
1858         encp->enc_pf = pf;
1859         encp->enc_vf = vf;
1860
1861         /* MAC address for this function */
1862         if (EFX_PCI_FUNCTION_IS_PF(encp)) {
1863                 rc = efx_mcdi_get_mac_address_pf(enp, mac_addr);
1864 #if EFSYS_OPT_ALLOW_UNCONFIGURED_NIC
1865                 /*
1866                  * Disable static config checking, ONLY for manufacturing test
1867                  * and setup at the factory, to allow the static config to be
1868                  * installed.
1869                  */
1870 #else /* EFSYS_OPT_ALLOW_UNCONFIGURED_NIC */
1871                 if ((rc == 0) && (mac_addr[0] & 0x02)) {
1872                         /*
1873                          * If the static config does not include a global MAC
1874                          * address pool then the board may return a locally
1875                          * administered MAC address (this should only happen on
1876                          * incorrectly programmed boards).
1877                          */
1878                         rc = EINVAL;
1879                 }
1880 #endif /* EFSYS_OPT_ALLOW_UNCONFIGURED_NIC */
1881         } else {
1882                 rc = efx_mcdi_get_mac_address_vf(enp, mac_addr);
1883         }
1884         if (rc != 0)
1885                 goto fail4;
1886
1887         EFX_MAC_ADDR_COPY(encp->enc_mac_addr, mac_addr);
1888
1889         /* Board configuration (legacy) */
1890         rc = efx_mcdi_get_board_cfg(enp, &board_type, NULL, NULL);
1891         if (rc != 0) {
1892                 /* Unprivileged functions may not be able to read board cfg */
1893                 if (rc == EACCES)
1894                         board_type = 0;
1895                 else
1896                         goto fail5;
1897         }
1898
1899         encp->enc_board_type = board_type;
1900         encp->enc_clk_mult = 1; /* not used for EF10 */
1901
1902         /* Fill out fields in enp->en_port and enp->en_nic_cfg from MCDI */
1903         if ((rc = efx_mcdi_get_phy_cfg(enp)) != 0)
1904                 goto fail6;
1905
1906         /*
1907          * Firmware with support for *_FEC capability bits does not
1908          * report that the corresponding *_FEC_REQUESTED bits are supported.
1909          * Add them here so that drivers understand that they are supported.
1910          */
1911         if (epp->ep_phy_cap_mask & (1u << EFX_PHY_CAP_BASER_FEC))
1912                 epp->ep_phy_cap_mask |=
1913                     (1u << EFX_PHY_CAP_BASER_FEC_REQUESTED);
1914         if (epp->ep_phy_cap_mask & (1u << EFX_PHY_CAP_RS_FEC))
1915                 epp->ep_phy_cap_mask |=
1916                     (1u << EFX_PHY_CAP_RS_FEC_REQUESTED);
1917         if (epp->ep_phy_cap_mask & (1u << EFX_PHY_CAP_25G_BASER_FEC))
1918                 epp->ep_phy_cap_mask |=
1919                     (1u << EFX_PHY_CAP_25G_BASER_FEC_REQUESTED);
1920
1921         /* Obtain the default PHY advertised capabilities */
1922         if ((rc = ef10_phy_get_link(enp, &els)) != 0)
1923                 goto fail7;
1924         epp->ep_default_adv_cap_mask = els.epls.epls_adv_cap_mask;
1925         epp->ep_adv_cap_mask = els.epls.epls_adv_cap_mask;
1926
1927         /* Check capabilities of running datapath firmware */
1928         if ((rc = ef10_get_datapath_caps(enp)) != 0)
1929                 goto fail8;
1930
1931         /* Alignment for WPTR updates */
1932         encp->enc_rx_push_align = EF10_RX_WPTR_ALIGN;
1933
1934         encp->enc_tx_dma_desc_size_max = EFX_MASK32(ESF_DZ_RX_KER_BYTE_CNT);
1935         /* No boundary crossing limits */
1936         encp->enc_tx_dma_desc_boundary = 0;
1937
1938         /*
1939          * Maximum number of bytes into the frame the TCP header can start for
1940          * firmware assisted TSO to work.
1941          */
1942         encp->enc_tx_tso_tcp_header_offset_limit = EF10_TCP_HEADER_OFFSET_LIMIT;
1943
1944         /*
1945          * Set resource limits for MC_CMD_ALLOC_VIS. Note that we cannot use
1946          * MC_CMD_GET_RESOURCE_LIMITS here as that reports the available
1947          * resources (allocated to this PCIe function), which is zero until
1948          * after we have allocated VIs.
1949          */
1950         encp->enc_evq_limit = 1024;
1951         encp->enc_rxq_limit = EFX_RXQ_LIMIT_TARGET;
1952         encp->enc_txq_limit = EFX_TXQ_LIMIT_TARGET;
1953
1954         encp->enc_buftbl_limit = UINT32_MAX;
1955
1956         /* Get interrupt vector limits */
1957         if ((rc = efx_mcdi_get_vector_cfg(enp, &base, &nvec, NULL)) != 0) {
1958                 if (EFX_PCI_FUNCTION_IS_PF(encp))
1959                         goto fail9;
1960
1961                 /* Ignore error (cannot query vector limits from a VF). */
1962                 base = 0;
1963                 nvec = 1024;
1964         }
1965         encp->enc_intr_vec_base = base;
1966         encp->enc_intr_limit = nvec;
1967
1968         /*
1969          * Get the current privilege mask. Note that this may be modified
1970          * dynamically, so this value is informational only. DO NOT use
1971          * the privilege mask to check for sufficient privileges, as that
1972          * can result in time-of-check/time-of-use bugs.
1973          */
1974         if ((rc = ef10_get_privilege_mask(enp, &mask)) != 0)
1975                 goto fail10;
1976         encp->enc_privilege_mask = mask;
1977
1978         if ((rc = ef10_set_workaround_bug26807(enp)) != 0)
1979                 goto fail11;
1980
1981         /* Get remaining controller-specific board config */
1982         if ((rc = enop->eno_board_cfg(enp)) != 0)
1983                 if (rc != EACCES)
1984                         goto fail12;
1985
1986         return (0);
1987
1988 fail12:
1989         EFSYS_PROBE(fail12);
1990 fail11:
1991         EFSYS_PROBE(fail11);
1992 fail10:
1993         EFSYS_PROBE(fail10);
1994 fail9:
1995         EFSYS_PROBE(fail9);
1996 fail8:
1997         EFSYS_PROBE(fail8);
1998 fail7:
1999         EFSYS_PROBE(fail7);
2000 fail6:
2001         EFSYS_PROBE(fail6);
2002 fail5:
2003         EFSYS_PROBE(fail5);
2004 fail4:
2005         EFSYS_PROBE(fail4);
2006 fail3:
2007         EFSYS_PROBE(fail3);
2008 fail2:
2009         EFSYS_PROBE(fail2);
2010 fail1:
2011         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2012
2013         return (rc);
2014 }
2015
2016         __checkReturn   efx_rc_t
2017 ef10_nic_probe(
2018         __in            efx_nic_t *enp)
2019 {
2020         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
2021         efx_drv_cfg_t *edcp = &(enp->en_drv_cfg);
2022         efx_rc_t rc;
2023
2024         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
2025
2026         /* Read and clear any assertion state */
2027         if ((rc = efx_mcdi_read_assertion(enp)) != 0)
2028                 goto fail1;
2029
2030         /* Exit the assertion handler */
2031         if ((rc = efx_mcdi_exit_assertion_handler(enp)) != 0)
2032                 if (rc != EACCES)
2033                         goto fail2;
2034
2035         if ((rc = efx_mcdi_drv_attach(enp, B_TRUE)) != 0)
2036                 goto fail3;
2037
2038         if ((rc = ef10_nic_board_cfg(enp)) != 0)
2039                 goto fail4;
2040
2041         /*
2042          * Set default driver config limits (based on board config).
2043          *
2044          * FIXME: For now allocate a fixed number of VIs which is likely to be
2045          * sufficient and small enough to allow multiple functions on the same
2046          * port.
2047          */
2048         edcp->edc_min_vi_count = edcp->edc_max_vi_count =
2049             MIN(128, MAX(encp->enc_rxq_limit, encp->enc_txq_limit));
2050
2051         /* The client driver must configure and enable PIO buffer support */
2052         edcp->edc_max_piobuf_count = 0;
2053         edcp->edc_pio_alloc_size = 0;
2054
2055 #if EFSYS_OPT_MAC_STATS
2056         /* Wipe the MAC statistics */
2057         if ((rc = efx_mcdi_mac_stats_clear(enp)) != 0)
2058                 goto fail5;
2059 #endif
2060
2061 #if EFSYS_OPT_LOOPBACK
2062         if ((rc = efx_mcdi_get_loopback_modes(enp)) != 0)
2063                 goto fail6;
2064 #endif
2065
2066 #if EFSYS_OPT_MON_STATS
2067         if ((rc = mcdi_mon_cfg_build(enp)) != 0) {
2068                 /* Unprivileged functions do not have access to sensors */
2069                 if (rc != EACCES)
2070                         goto fail7;
2071         }
2072 #endif
2073
2074         encp->enc_features = enp->en_features;
2075
2076         return (0);
2077
2078 #if EFSYS_OPT_MON_STATS
2079 fail7:
2080         EFSYS_PROBE(fail7);
2081 #endif
2082 #if EFSYS_OPT_LOOPBACK
2083 fail6:
2084         EFSYS_PROBE(fail6);
2085 #endif
2086 #if EFSYS_OPT_MAC_STATS
2087 fail5:
2088         EFSYS_PROBE(fail5);
2089 #endif
2090 fail4:
2091         EFSYS_PROBE(fail4);
2092 fail3:
2093         EFSYS_PROBE(fail3);
2094 fail2:
2095         EFSYS_PROBE(fail2);
2096 fail1:
2097         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2098
2099         return (rc);
2100 }
2101
2102         __checkReturn   efx_rc_t
2103 ef10_nic_set_drv_limits(
2104         __inout         efx_nic_t *enp,
2105         __in            efx_drv_limits_t *edlp)
2106 {
2107         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
2108         efx_drv_cfg_t *edcp = &(enp->en_drv_cfg);
2109         uint32_t min_evq_count, max_evq_count;
2110         uint32_t min_rxq_count, max_rxq_count;
2111         uint32_t min_txq_count, max_txq_count;
2112         efx_rc_t rc;
2113
2114         if (edlp == NULL) {
2115                 rc = EINVAL;
2116                 goto fail1;
2117         }
2118
2119         /* Get minimum required and maximum usable VI limits */
2120         min_evq_count = MIN(edlp->edl_min_evq_count, encp->enc_evq_limit);
2121         min_rxq_count = MIN(edlp->edl_min_rxq_count, encp->enc_rxq_limit);
2122         min_txq_count = MIN(edlp->edl_min_txq_count, encp->enc_txq_limit);
2123
2124         edcp->edc_min_vi_count =
2125             MAX(min_evq_count, MAX(min_rxq_count, min_txq_count));
2126
2127         max_evq_count = MIN(edlp->edl_max_evq_count, encp->enc_evq_limit);
2128         max_rxq_count = MIN(edlp->edl_max_rxq_count, encp->enc_rxq_limit);
2129         max_txq_count = MIN(edlp->edl_max_txq_count, encp->enc_txq_limit);
2130
2131         edcp->edc_max_vi_count =
2132             MAX(max_evq_count, MAX(max_rxq_count, max_txq_count));
2133
2134         /*
2135          * Check limits for sub-allocated piobuf blocks.
2136          * PIO is optional, so don't fail if the limits are incorrect.
2137          */
2138         if ((encp->enc_piobuf_size == 0) ||
2139             (encp->enc_piobuf_limit == 0) ||
2140             (edlp->edl_min_pio_alloc_size == 0) ||
2141             (edlp->edl_min_pio_alloc_size > encp->enc_piobuf_size)) {
2142                 /* Disable PIO */
2143                 edcp->edc_max_piobuf_count = 0;
2144                 edcp->edc_pio_alloc_size = 0;
2145         } else {
2146                 uint32_t blk_size, blk_count, blks_per_piobuf;
2147
2148                 blk_size =
2149                     MAX(edlp->edl_min_pio_alloc_size,
2150                             encp->enc_piobuf_min_alloc_size);
2151
2152                 blks_per_piobuf = encp->enc_piobuf_size / blk_size;
2153                 EFSYS_ASSERT3U(blks_per_piobuf, <=, 32);
2154
2155                 blk_count = (encp->enc_piobuf_limit * blks_per_piobuf);
2156
2157                 /* A zero max pio alloc count means unlimited */
2158                 if ((edlp->edl_max_pio_alloc_count > 0) &&
2159                     (edlp->edl_max_pio_alloc_count < blk_count)) {
2160                         blk_count = edlp->edl_max_pio_alloc_count;
2161                 }
2162
2163                 edcp->edc_pio_alloc_size = blk_size;
2164                 edcp->edc_max_piobuf_count =
2165                     (blk_count + (blks_per_piobuf - 1)) / blks_per_piobuf;
2166         }
2167
2168         return (0);
2169
2170 fail1:
2171         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2172
2173         return (rc);
2174 }
2175
2176
2177         __checkReturn   efx_rc_t
2178 ef10_nic_reset(
2179         __in            efx_nic_t *enp)
2180 {
2181         efx_mcdi_req_t req;
2182         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_ENTITY_RESET_IN_LEN,
2183                 MC_CMD_ENTITY_RESET_OUT_LEN);
2184         efx_rc_t rc;
2185
2186         /* ef10_nic_reset() is called to recover from BADASSERT failures. */
2187         if ((rc = efx_mcdi_read_assertion(enp)) != 0)
2188                 goto fail1;
2189         if ((rc = efx_mcdi_exit_assertion_handler(enp)) != 0)
2190                 goto fail2;
2191
2192         req.emr_cmd = MC_CMD_ENTITY_RESET;
2193         req.emr_in_buf = payload;
2194         req.emr_in_length = MC_CMD_ENTITY_RESET_IN_LEN;
2195         req.emr_out_buf = payload;
2196         req.emr_out_length = MC_CMD_ENTITY_RESET_OUT_LEN;
2197
2198         MCDI_IN_POPULATE_DWORD_1(req, ENTITY_RESET_IN_FLAG,
2199             ENTITY_RESET_IN_FUNCTION_RESOURCE_RESET, 1);
2200
2201         efx_mcdi_execute(enp, &req);
2202
2203         if (req.emr_rc != 0) {
2204                 rc = req.emr_rc;
2205                 goto fail3;
2206         }
2207
2208         /* Clear RX/TX DMA queue errors */
2209         enp->en_reset_flags &= ~(EFX_RESET_RXQ_ERR | EFX_RESET_TXQ_ERR);
2210
2211         return (0);
2212
2213 fail3:
2214         EFSYS_PROBE(fail3);
2215 fail2:
2216         EFSYS_PROBE(fail2);
2217 fail1:
2218         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2219
2220         return (rc);
2221 }
2222
2223         __checkReturn   efx_rc_t
2224 ef10_nic_init(
2225         __in            efx_nic_t *enp)
2226 {
2227         efx_drv_cfg_t *edcp = &(enp->en_drv_cfg);
2228         uint32_t min_vi_count, max_vi_count;
2229         uint32_t vi_count, vi_base, vi_shift;
2230         uint32_t i;
2231         uint32_t retry;
2232         uint32_t delay_us;
2233         uint32_t vi_window_size;
2234         efx_rc_t rc;
2235
2236         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
2237
2238         /* Enable reporting of some events (e.g. link change) */
2239         if ((rc = efx_mcdi_log_ctrl(enp)) != 0)
2240                 goto fail1;
2241
2242         /* Allocate (optional) on-chip PIO buffers */
2243         ef10_nic_alloc_piobufs(enp, edcp->edc_max_piobuf_count);
2244
2245         /*
2246          * For best performance, PIO writes should use a write-combined
2247          * (WC) memory mapping. Using a separate WC mapping for the PIO
2248          * aperture of each VI would be a burden to drivers (and not
2249          * possible if the host page size is >4Kbyte).
2250          *
2251          * To avoid this we use a single uncached (UC) mapping for VI
2252          * register access, and a single WC mapping for extra VIs used
2253          * for PIO writes.
2254          *
2255          * Each piobuf must be linked to a VI in the WC mapping, and to
2256          * each VI that is using a sub-allocated block from the piobuf.
2257          */
2258         min_vi_count = edcp->edc_min_vi_count;
2259         max_vi_count =
2260             edcp->edc_max_vi_count + enp->en_arch.ef10.ena_piobuf_count;
2261
2262         /* Ensure that the previously attached driver's VIs are freed */
2263         if ((rc = efx_mcdi_free_vis(enp)) != 0)
2264                 goto fail2;
2265
2266         /*
2267          * Reserve VI resources (EVQ+RXQ+TXQ) for this PCIe function. If this
2268          * fails then retrying the request for fewer VI resources may succeed.
2269          */
2270         vi_count = 0;
2271         if ((rc = efx_mcdi_alloc_vis(enp, min_vi_count, max_vi_count,
2272                     &vi_base, &vi_count, &vi_shift)) != 0)
2273                 goto fail3;
2274
2275         EFSYS_PROBE2(vi_alloc, uint32_t, vi_base, uint32_t, vi_count);
2276
2277         if (vi_count < min_vi_count) {
2278                 rc = ENOMEM;
2279                 goto fail4;
2280         }
2281
2282         enp->en_arch.ef10.ena_vi_base = vi_base;
2283         enp->en_arch.ef10.ena_vi_count = vi_count;
2284         enp->en_arch.ef10.ena_vi_shift = vi_shift;
2285
2286         if (vi_count < min_vi_count + enp->en_arch.ef10.ena_piobuf_count) {
2287                 /* Not enough extra VIs to map piobufs */
2288                 ef10_nic_free_piobufs(enp);
2289         }
2290
2291         enp->en_arch.ef10.ena_pio_write_vi_base =
2292             vi_count - enp->en_arch.ef10.ena_piobuf_count;
2293
2294         EFSYS_ASSERT3U(enp->en_nic_cfg.enc_vi_window_shift, !=,
2295             EFX_VI_WINDOW_SHIFT_INVALID);
2296         EFSYS_ASSERT3U(enp->en_nic_cfg.enc_vi_window_shift, <=,
2297             EFX_VI_WINDOW_SHIFT_64K);
2298         vi_window_size = 1U << enp->en_nic_cfg.enc_vi_window_shift;
2299
2300         /* Save UC memory mapping details */
2301         enp->en_arch.ef10.ena_uc_mem_map_offset = 0;
2302         if (enp->en_arch.ef10.ena_piobuf_count > 0) {
2303                 enp->en_arch.ef10.ena_uc_mem_map_size =
2304                     (vi_window_size *
2305                     enp->en_arch.ef10.ena_pio_write_vi_base);
2306         } else {
2307                 enp->en_arch.ef10.ena_uc_mem_map_size =
2308                     (vi_window_size *
2309                     enp->en_arch.ef10.ena_vi_count);
2310         }
2311
2312         /* Save WC memory mapping details */
2313         enp->en_arch.ef10.ena_wc_mem_map_offset =
2314             enp->en_arch.ef10.ena_uc_mem_map_offset +
2315             enp->en_arch.ef10.ena_uc_mem_map_size;
2316
2317         enp->en_arch.ef10.ena_wc_mem_map_size =
2318             (vi_window_size *
2319             enp->en_arch.ef10.ena_piobuf_count);
2320
2321         /* Link piobufs to extra VIs in WC mapping */
2322         if (enp->en_arch.ef10.ena_piobuf_count > 0) {
2323                 for (i = 0; i < enp->en_arch.ef10.ena_piobuf_count; i++) {
2324                         rc = efx_mcdi_link_piobuf(enp,
2325                             enp->en_arch.ef10.ena_pio_write_vi_base + i,
2326                             enp->en_arch.ef10.ena_piobuf_handle[i]);
2327                         if (rc != 0)
2328                                 break;
2329                 }
2330         }
2331
2332         /*
2333          * Allocate a vAdaptor attached to our upstream vPort/pPort.
2334          *
2335          * On a VF, this may fail with MC_CMD_ERR_NO_EVB_PORT (ENOENT) if the PF
2336          * driver has yet to bring up the EVB port. See bug 56147. In this case,
2337          * retry the request several times after waiting a while. The wait time
2338          * between retries starts small (10ms) and exponentially increases.
2339          * Total wait time is a little over two seconds. Retry logic in the
2340          * client driver may mean this whole loop is repeated if it continues to
2341          * fail.
2342          */
2343         retry = 0;
2344         delay_us = 10000;
2345         while ((rc = efx_mcdi_vadaptor_alloc(enp, EVB_PORT_ID_ASSIGNED)) != 0) {
2346                 if (EFX_PCI_FUNCTION_IS_PF(&enp->en_nic_cfg) ||
2347                     (rc != ENOENT)) {
2348                         /*
2349                          * Do not retry alloc for PF, or for other errors on
2350                          * a VF.
2351                          */
2352                         goto fail5;
2353                 }
2354
2355                 /* VF startup before PF is ready. Retry allocation. */
2356                 if (retry > 5) {
2357                         /* Too many attempts */
2358                         rc = EINVAL;
2359                         goto fail6;
2360                 }
2361                 EFSYS_PROBE1(mcdi_no_evb_port_retry, int, retry);
2362                 EFSYS_SLEEP(delay_us);
2363                 retry++;
2364                 if (delay_us < 500000)
2365                         delay_us <<= 2;
2366         }
2367
2368         enp->en_vport_id = EVB_PORT_ID_ASSIGNED;
2369         enp->en_nic_cfg.enc_mcdi_max_payload_length = MCDI_CTL_SDU_LEN_MAX_V2;
2370
2371         return (0);
2372
2373 fail6:
2374         EFSYS_PROBE(fail6);
2375 fail5:
2376         EFSYS_PROBE(fail5);
2377 fail4:
2378         EFSYS_PROBE(fail4);
2379 fail3:
2380         EFSYS_PROBE(fail3);
2381 fail2:
2382         EFSYS_PROBE(fail2);
2383
2384         ef10_nic_free_piobufs(enp);
2385
2386 fail1:
2387         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2388
2389         return (rc);
2390 }
2391
2392         __checkReturn   efx_rc_t
2393 ef10_nic_get_vi_pool(
2394         __in            efx_nic_t *enp,
2395         __out           uint32_t *vi_countp)
2396 {
2397         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
2398
2399         /*
2400          * Report VIs that the client driver can use.
2401          * Do not include VIs used for PIO buffer writes.
2402          */
2403         *vi_countp = enp->en_arch.ef10.ena_pio_write_vi_base;
2404
2405         return (0);
2406 }
2407
2408         __checkReturn   efx_rc_t
2409 ef10_nic_get_bar_region(
2410         __in            efx_nic_t *enp,
2411         __in            efx_nic_region_t region,
2412         __out           uint32_t *offsetp,
2413         __out           size_t *sizep)
2414 {
2415         efx_rc_t rc;
2416
2417         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
2418
2419         /*
2420          * TODO: Specify host memory mapping alignment and granularity
2421          * in efx_drv_limits_t so that they can be taken into account
2422          * when allocating extra VIs for PIO writes.
2423          */
2424         switch (region) {
2425         case EFX_REGION_VI:
2426                 /* UC mapped memory BAR region for VI registers */
2427                 *offsetp = enp->en_arch.ef10.ena_uc_mem_map_offset;
2428                 *sizep = enp->en_arch.ef10.ena_uc_mem_map_size;
2429                 break;
2430
2431         case EFX_REGION_PIO_WRITE_VI:
2432                 /* WC mapped memory BAR region for piobuf writes */
2433                 *offsetp = enp->en_arch.ef10.ena_wc_mem_map_offset;
2434                 *sizep = enp->en_arch.ef10.ena_wc_mem_map_size;
2435                 break;
2436
2437         default:
2438                 rc = EINVAL;
2439                 goto fail1;
2440         }
2441
2442         return (0);
2443
2444 fail1:
2445         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2446
2447         return (rc);
2448 }
2449
2450         __checkReturn   boolean_t
2451 ef10_nic_hw_unavailable(
2452         __in            efx_nic_t *enp)
2453 {
2454         efx_dword_t dword;
2455
2456         if (enp->en_reset_flags & EFX_RESET_HW_UNAVAIL)
2457                 return (B_TRUE);
2458
2459         EFX_BAR_READD(enp, ER_DZ_BIU_MC_SFT_STATUS_REG, &dword, B_FALSE);
2460         if (EFX_DWORD_FIELD(dword, EFX_DWORD_0) == 0xffffffff)
2461                 goto unavail;
2462
2463         return (B_FALSE);
2464
2465 unavail:
2466         ef10_nic_set_hw_unavailable(enp);
2467
2468         return (B_TRUE);
2469 }
2470
2471                         void
2472 ef10_nic_set_hw_unavailable(
2473         __in            efx_nic_t *enp)
2474 {
2475         EFSYS_PROBE(hw_unavail);
2476         enp->en_reset_flags |= EFX_RESET_HW_UNAVAIL;
2477 }
2478
2479
2480                         void
2481 ef10_nic_fini(
2482         __in            efx_nic_t *enp)
2483 {
2484         uint32_t i;
2485         efx_rc_t rc;
2486
2487         (void) efx_mcdi_vadaptor_free(enp, enp->en_vport_id);
2488         enp->en_vport_id = 0;
2489
2490         /* Unlink piobufs from extra VIs in WC mapping */
2491         if (enp->en_arch.ef10.ena_piobuf_count > 0) {
2492                 for (i = 0; i < enp->en_arch.ef10.ena_piobuf_count; i++) {
2493                         rc = efx_mcdi_unlink_piobuf(enp,
2494                             enp->en_arch.ef10.ena_pio_write_vi_base + i);
2495                         if (rc != 0)
2496                                 break;
2497                 }
2498         }
2499
2500         ef10_nic_free_piobufs(enp);
2501
2502         (void) efx_mcdi_free_vis(enp);
2503         enp->en_arch.ef10.ena_vi_count = 0;
2504 }
2505
2506                         void
2507 ef10_nic_unprobe(
2508         __in            efx_nic_t *enp)
2509 {
2510 #if EFSYS_OPT_MON_STATS
2511         mcdi_mon_cfg_free(enp);
2512 #endif /* EFSYS_OPT_MON_STATS */
2513         (void) efx_mcdi_drv_attach(enp, B_FALSE);
2514 }
2515
2516 #if EFSYS_OPT_DIAG
2517
2518         __checkReturn   efx_rc_t
2519 ef10_nic_register_test(
2520         __in            efx_nic_t *enp)
2521 {
2522         efx_rc_t rc;
2523
2524         /* FIXME */
2525         _NOTE(ARGUNUSED(enp))
2526         _NOTE(CONSTANTCONDITION)
2527         if (B_FALSE) {
2528                 rc = ENOTSUP;
2529                 goto fail1;
2530         }
2531         /* FIXME */
2532
2533         return (0);
2534
2535 fail1:
2536         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2537
2538         return (rc);
2539 }
2540
2541 #endif  /* EFSYS_OPT_DIAG */
2542
2543 #if EFSYS_OPT_FW_SUBVARIANT_AWARE
2544
2545         __checkReturn   efx_rc_t
2546 efx_mcdi_get_nic_global(
2547         __in            efx_nic_t *enp,
2548         __in            uint32_t key,
2549         __out           uint32_t *valuep)
2550 {
2551         efx_mcdi_req_t req;
2552         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_GET_NIC_GLOBAL_IN_LEN,
2553                 MC_CMD_GET_NIC_GLOBAL_OUT_LEN);
2554         efx_rc_t rc;
2555
2556         req.emr_cmd = MC_CMD_GET_NIC_GLOBAL;
2557         req.emr_in_buf = payload;
2558         req.emr_in_length = MC_CMD_GET_NIC_GLOBAL_IN_LEN;
2559         req.emr_out_buf = payload;
2560         req.emr_out_length = MC_CMD_GET_NIC_GLOBAL_OUT_LEN;
2561
2562         MCDI_IN_SET_DWORD(req, GET_NIC_GLOBAL_IN_KEY, key);
2563
2564         efx_mcdi_execute(enp, &req);
2565
2566         if (req.emr_rc != 0) {
2567                 rc = req.emr_rc;
2568                 goto fail1;
2569         }
2570
2571         if (req.emr_out_length_used != MC_CMD_GET_NIC_GLOBAL_OUT_LEN) {
2572                 rc = EMSGSIZE;
2573                 goto fail2;
2574         }
2575
2576         *valuep = MCDI_OUT_DWORD(req, GET_NIC_GLOBAL_OUT_VALUE);
2577
2578         return (0);
2579
2580 fail2:
2581         EFSYS_PROBE(fail2);
2582 fail1:
2583         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2584
2585         return (rc);
2586 }
2587
2588         __checkReturn   efx_rc_t
2589 efx_mcdi_set_nic_global(
2590         __in            efx_nic_t *enp,
2591         __in            uint32_t key,
2592         __in            uint32_t value)
2593 {
2594         efx_mcdi_req_t req;
2595         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_SET_NIC_GLOBAL_IN_LEN, 0);
2596         efx_rc_t rc;
2597
2598         req.emr_cmd = MC_CMD_SET_NIC_GLOBAL;
2599         req.emr_in_buf = payload;
2600         req.emr_in_length = MC_CMD_SET_NIC_GLOBAL_IN_LEN;
2601         req.emr_out_buf = NULL;
2602         req.emr_out_length = 0;
2603
2604         MCDI_IN_SET_DWORD(req, SET_NIC_GLOBAL_IN_KEY, key);
2605         MCDI_IN_SET_DWORD(req, SET_NIC_GLOBAL_IN_VALUE, value);
2606
2607         efx_mcdi_execute(enp, &req);
2608
2609         if (req.emr_rc != 0) {
2610                 rc = req.emr_rc;
2611                 goto fail1;
2612         }
2613
2614         return (0);
2615
2616 fail1:
2617         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2618
2619         return (rc);
2620 }
2621
2622 #endif  /* EFSYS_OPT_FW_SUBVARIANT_AWARE */
2623
2624 #endif  /* EFX_OPTS_EF10() */