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