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