common/sfc_efx/base: move 14b prefix check out of caps get
[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         if ((rc = ef10_mcdi_get_pf_count(enp, &encp->enc_hw_pf_count)) != 0)
1035                 goto fail1;
1036
1037
1038         req.emr_cmd = MC_CMD_GET_CAPABILITIES;
1039         req.emr_in_buf = payload;
1040         req.emr_in_length = MC_CMD_GET_CAPABILITIES_IN_LEN;
1041         req.emr_out_buf = payload;
1042         req.emr_out_length = MC_CMD_GET_CAPABILITIES_V5_OUT_LEN;
1043
1044         efx_mcdi_execute_quiet(enp, &req);
1045
1046         if (req.emr_rc != 0) {
1047                 rc = req.emr_rc;
1048                 goto fail2;
1049         }
1050
1051         if (req.emr_out_length_used < MC_CMD_GET_CAPABILITIES_OUT_LEN) {
1052                 rc = EMSGSIZE;
1053                 goto fail3;
1054         }
1055
1056 #define CAP_FLAGS1(_req, _flag)                                         \
1057         (MCDI_OUT_DWORD((_req), GET_CAPABILITIES_OUT_FLAGS1) &          \
1058         (1u << (MC_CMD_GET_CAPABILITIES_V2_OUT_ ## _flag ## _LBN)))
1059
1060 #define CAP_FLAGS2(_req, _flag)                                         \
1061         (((_req).emr_out_length_used >= MC_CMD_GET_CAPABILITIES_V2_OUT_LEN) && \
1062             (MCDI_OUT_DWORD((_req), GET_CAPABILITIES_V2_OUT_FLAGS2) &   \
1063             (1u << (MC_CMD_GET_CAPABILITIES_V2_OUT_ ## _flag ## _LBN))))
1064
1065         /* Check if RXDP firmware inserts 14 byte prefix */
1066         if (CAP_FLAGS1(req, RX_PREFIX_LEN_14))
1067                 encp->enc_rx_prefix_size = 14;
1068         else
1069                 encp->enc_rx_prefix_size = 0;
1070
1071 #if EFSYS_OPT_RX_SCALE
1072         /* Check if the firmware supports additional RSS modes */
1073         if (CAP_FLAGS1(req, ADDITIONAL_RSS_MODES))
1074                 encp->enc_rx_scale_additional_modes_supported = B_TRUE;
1075         else
1076                 encp->enc_rx_scale_additional_modes_supported = B_FALSE;
1077 #endif /* EFSYS_OPT_RX_SCALE */
1078
1079         /* Check if the firmware supports TSO */
1080         if (CAP_FLAGS1(req, TX_TSO))
1081                 encp->enc_fw_assisted_tso_enabled = B_TRUE;
1082         else
1083                 encp->enc_fw_assisted_tso_enabled = B_FALSE;
1084
1085         /* Check if the firmware supports FATSOv2 */
1086         if (CAP_FLAGS2(req, TX_TSO_V2)) {
1087                 encp->enc_fw_assisted_tso_v2_enabled = B_TRUE;
1088                 encp->enc_fw_assisted_tso_v2_n_contexts = MCDI_OUT_WORD(req,
1089                     GET_CAPABILITIES_V2_OUT_TX_TSO_V2_N_CONTEXTS);
1090         } else {
1091                 encp->enc_fw_assisted_tso_v2_enabled = B_FALSE;
1092                 encp->enc_fw_assisted_tso_v2_n_contexts = 0;
1093         }
1094
1095         /* Check if the firmware supports FATSOv2 encap */
1096         if (CAP_FLAGS2(req, TX_TSO_V2_ENCAP))
1097                 encp->enc_fw_assisted_tso_v2_encap_enabled = B_TRUE;
1098         else
1099                 encp->enc_fw_assisted_tso_v2_encap_enabled = B_FALSE;
1100
1101         /* Check if the firmware has vadapter/vport/vswitch support */
1102         if (CAP_FLAGS1(req, EVB))
1103                 encp->enc_datapath_cap_evb = B_TRUE;
1104         else
1105                 encp->enc_datapath_cap_evb = B_FALSE;
1106
1107         /* Check if the firmware supports vport reconfiguration */
1108         if (CAP_FLAGS1(req, VPORT_RECONFIGURE))
1109                 encp->enc_vport_reconfigure_supported = B_TRUE;
1110         else
1111                 encp->enc_vport_reconfigure_supported = B_FALSE;
1112
1113         /* Check if the firmware supports VLAN insertion */
1114         if (CAP_FLAGS1(req, TX_VLAN_INSERTION))
1115                 encp->enc_hw_tx_insert_vlan_enabled = B_TRUE;
1116         else
1117                 encp->enc_hw_tx_insert_vlan_enabled = B_FALSE;
1118
1119         /* Check if the firmware supports RX event batching */
1120         if (CAP_FLAGS1(req, RX_BATCHING))
1121                 encp->enc_rx_batching_enabled = B_TRUE;
1122         else
1123                 encp->enc_rx_batching_enabled = B_FALSE;
1124
1125         /*
1126          * Even if batching isn't reported as supported, we may still get
1127          * batched events (see bug61153).
1128          */
1129         encp->enc_rx_batch_max = 16;
1130
1131         /* Check if the firmware supports disabling scatter on RXQs */
1132         if (CAP_FLAGS1(req, RX_DISABLE_SCATTER))
1133                 encp->enc_rx_disable_scatter_supported = B_TRUE;
1134         else
1135                 encp->enc_rx_disable_scatter_supported = B_FALSE;
1136
1137         /* Check if the firmware supports packed stream mode */
1138         if (CAP_FLAGS1(req, RX_PACKED_STREAM))
1139                 encp->enc_rx_packed_stream_supported = B_TRUE;
1140         else
1141                 encp->enc_rx_packed_stream_supported = B_FALSE;
1142
1143         /*
1144          * Check if the firmware supports configurable buffer sizes
1145          * for packed stream mode (otherwise buffer size is 1Mbyte)
1146          */
1147         if (CAP_FLAGS1(req, RX_PACKED_STREAM_VAR_BUFFERS))
1148                 encp->enc_rx_var_packed_stream_supported = B_TRUE;
1149         else
1150                 encp->enc_rx_var_packed_stream_supported = B_FALSE;
1151
1152         /* Check if the firmware supports equal stride super-buffer mode */
1153         if (CAP_FLAGS2(req, EQUAL_STRIDE_SUPER_BUFFER))
1154                 encp->enc_rx_es_super_buffer_supported = B_TRUE;
1155         else
1156                 encp->enc_rx_es_super_buffer_supported = B_FALSE;
1157
1158         /* Check if the firmware supports FW subvariant w/o Tx checksumming */
1159         if (CAP_FLAGS2(req, FW_SUBVARIANT_NO_TX_CSUM))
1160                 encp->enc_fw_subvariant_no_tx_csum_supported = B_TRUE;
1161         else
1162                 encp->enc_fw_subvariant_no_tx_csum_supported = B_FALSE;
1163
1164         /* Check if the firmware supports set mac with running filters */
1165         if (CAP_FLAGS1(req, VADAPTOR_PERMIT_SET_MAC_WHEN_FILTERS_INSTALLED))
1166                 encp->enc_allow_set_mac_with_installed_filters = B_TRUE;
1167         else
1168                 encp->enc_allow_set_mac_with_installed_filters = B_FALSE;
1169
1170         /*
1171          * Check if firmware supports the extended MC_CMD_SET_MAC, which allows
1172          * specifying which parameters to configure.
1173          */
1174         if (CAP_FLAGS1(req, SET_MAC_ENHANCED))
1175                 encp->enc_enhanced_set_mac_supported = B_TRUE;
1176         else
1177                 encp->enc_enhanced_set_mac_supported = B_FALSE;
1178
1179         /*
1180          * Check if firmware supports version 2 of MC_CMD_INIT_EVQ, which allows
1181          * us to let the firmware choose the settings to use on an EVQ.
1182          */
1183         if (CAP_FLAGS2(req, INIT_EVQ_V2))
1184                 encp->enc_init_evq_v2_supported = B_TRUE;
1185         else
1186                 encp->enc_init_evq_v2_supported = B_FALSE;
1187
1188         /*
1189          * Check if the NO_CONT_EV mode for RX events is supported.
1190          */
1191         if (CAP_FLAGS2(req, INIT_RXQ_NO_CONT_EV))
1192                 encp->enc_no_cont_ev_mode_supported = B_TRUE;
1193         else
1194                 encp->enc_no_cont_ev_mode_supported = B_FALSE;
1195
1196         /*
1197          * Check if buffer size may and must be specified on INIT_RXQ.
1198          * It may be always specified to efx_rx_qcreate(), but will be
1199          * just kept libefx internal if MCDI does not support it.
1200          */
1201         if (CAP_FLAGS2(req, INIT_RXQ_WITH_BUFFER_SIZE))
1202                 encp->enc_init_rxq_with_buffer_size = B_TRUE;
1203         else
1204                 encp->enc_init_rxq_with_buffer_size = B_FALSE;
1205
1206         /*
1207          * Check if firmware-verified NVRAM updates must be used.
1208          *
1209          * The firmware trusted installer requires all NVRAM updates to use
1210          * version 2 of MC_CMD_NVRAM_UPDATE_START (to enable verified update)
1211          * and version 2 of MC_CMD_NVRAM_UPDATE_FINISH (to verify the updated
1212          * partition and report the result).
1213          */
1214         if (CAP_FLAGS2(req, NVRAM_UPDATE_REPORT_VERIFY_RESULT))
1215                 encp->enc_nvram_update_verify_result_supported = B_TRUE;
1216         else
1217                 encp->enc_nvram_update_verify_result_supported = B_FALSE;
1218
1219         if (CAP_FLAGS2(req, NVRAM_UPDATE_POLL_VERIFY_RESULT))
1220                 encp->enc_nvram_update_poll_verify_result_supported = B_TRUE;
1221         else
1222                 encp->enc_nvram_update_poll_verify_result_supported = B_FALSE;
1223
1224         /*
1225          * Check if firmware update via the BUNDLE partition is supported
1226          */
1227         if (CAP_FLAGS2(req, BUNDLE_UPDATE))
1228                 encp->enc_nvram_bundle_update_supported = B_TRUE;
1229         else
1230                 encp->enc_nvram_bundle_update_supported = B_FALSE;
1231
1232         /*
1233          * Check if firmware provides packet memory and Rx datapath
1234          * counters.
1235          */
1236         if (CAP_FLAGS1(req, PM_AND_RXDP_COUNTERS))
1237                 encp->enc_pm_and_rxdp_counters = B_TRUE;
1238         else
1239                 encp->enc_pm_and_rxdp_counters = B_FALSE;
1240
1241         /*
1242          * Check if the 40G MAC hardware is capable of reporting
1243          * statistics for Tx size bins.
1244          */
1245         if (CAP_FLAGS2(req, MAC_STATS_40G_TX_SIZE_BINS))
1246                 encp->enc_mac_stats_40g_tx_size_bins = B_TRUE;
1247         else
1248                 encp->enc_mac_stats_40g_tx_size_bins = B_FALSE;
1249
1250         /*
1251          * Check if firmware supports VXLAN and NVGRE tunnels.
1252          * The capability indicates Geneve protocol support as well.
1253          */
1254         if (CAP_FLAGS1(req, VXLAN_NVGRE)) {
1255                 encp->enc_tunnel_encapsulations_supported =
1256                     (1u << EFX_TUNNEL_PROTOCOL_VXLAN) |
1257                     (1u << EFX_TUNNEL_PROTOCOL_GENEVE) |
1258                     (1u << EFX_TUNNEL_PROTOCOL_NVGRE);
1259
1260                 EFX_STATIC_ASSERT(EFX_TUNNEL_MAXNENTRIES ==
1261                     MC_CMD_SET_TUNNEL_ENCAP_UDP_PORTS_IN_ENTRIES_MAXNUM);
1262                 encp->enc_tunnel_config_udp_entries_max =
1263                     EFX_TUNNEL_MAXNENTRIES;
1264         } else {
1265                 encp->enc_tunnel_config_udp_entries_max = 0;
1266         }
1267
1268         /*
1269          * Check if firmware reports the VI window mode.
1270          * Medford2 has a variable VI window size (8K, 16K or 64K).
1271          * Medford and Huntington have a fixed 8K VI window size.
1272          */
1273         if (req.emr_out_length_used >= MC_CMD_GET_CAPABILITIES_V3_OUT_LEN) {
1274                 uint8_t mode =
1275                     MCDI_OUT_BYTE(req, GET_CAPABILITIES_V3_OUT_VI_WINDOW_MODE);
1276
1277                 switch (mode) {
1278                 case MC_CMD_GET_CAPABILITIES_V3_OUT_VI_WINDOW_MODE_8K:
1279                         encp->enc_vi_window_shift = EFX_VI_WINDOW_SHIFT_8K;
1280                         break;
1281                 case MC_CMD_GET_CAPABILITIES_V3_OUT_VI_WINDOW_MODE_16K:
1282                         encp->enc_vi_window_shift = EFX_VI_WINDOW_SHIFT_16K;
1283                         break;
1284                 case MC_CMD_GET_CAPABILITIES_V3_OUT_VI_WINDOW_MODE_64K:
1285                         encp->enc_vi_window_shift = EFX_VI_WINDOW_SHIFT_64K;
1286                         break;
1287                 default:
1288                         encp->enc_vi_window_shift = EFX_VI_WINDOW_SHIFT_INVALID;
1289                         break;
1290                 }
1291         } else if ((enp->en_family == EFX_FAMILY_HUNTINGTON) ||
1292                     (enp->en_family == EFX_FAMILY_MEDFORD)) {
1293                 /* Huntington and Medford have fixed 8K window size */
1294                 encp->enc_vi_window_shift = EFX_VI_WINDOW_SHIFT_8K;
1295         } else {
1296                 encp->enc_vi_window_shift = EFX_VI_WINDOW_SHIFT_INVALID;
1297         }
1298
1299         /* Check if firmware supports extended MAC stats. */
1300         if (req.emr_out_length_used >= MC_CMD_GET_CAPABILITIES_V4_OUT_LEN) {
1301                 /* Extended stats buffer supported */
1302                 encp->enc_mac_stats_nstats = MCDI_OUT_WORD(req,
1303                     GET_CAPABILITIES_V4_OUT_MAC_STATS_NUM_STATS);
1304         } else {
1305                 /* Use Siena-compatible legacy MAC stats */
1306                 encp->enc_mac_stats_nstats = MC_CMD_MAC_NSTATS;
1307         }
1308
1309         if (encp->enc_mac_stats_nstats >= MC_CMD_MAC_NSTATS_V2)
1310                 encp->enc_fec_counters = B_TRUE;
1311         else
1312                 encp->enc_fec_counters = B_FALSE;
1313
1314         /* Check if the firmware provides head-of-line blocking counters */
1315         if (CAP_FLAGS2(req, RXDP_HLB_IDLE))
1316                 encp->enc_hlb_counters = B_TRUE;
1317         else
1318                 encp->enc_hlb_counters = B_FALSE;
1319
1320 #if EFSYS_OPT_RX_SCALE
1321         if (CAP_FLAGS1(req, RX_RSS_LIMITED)) {
1322                 /* Only one exclusive RSS context is available per port. */
1323                 encp->enc_rx_scale_max_exclusive_contexts = 1;
1324
1325                 switch (enp->en_family) {
1326                 case EFX_FAMILY_MEDFORD2:
1327                         encp->enc_rx_scale_hash_alg_mask =
1328                             (1U << EFX_RX_HASHALG_TOEPLITZ);
1329                         break;
1330
1331                 case EFX_FAMILY_MEDFORD:
1332                 case EFX_FAMILY_HUNTINGTON:
1333                         /*
1334                          * Packed stream firmware variant maintains a
1335                          * non-standard algorithm for hash computation.
1336                          * It implies explicit XORing together
1337                          * source + destination IP addresses (or last
1338                          * four bytes in the case of IPv6) and using the
1339                          * resulting value as the input to a Toeplitz hash.
1340                          */
1341                         encp->enc_rx_scale_hash_alg_mask =
1342                             (1U << EFX_RX_HASHALG_PACKED_STREAM);
1343                         break;
1344
1345                 default:
1346                         rc = EINVAL;
1347                         goto fail4;
1348                 }
1349
1350                 /* Port numbers cannot contribute to the hash value */
1351                 encp->enc_rx_scale_l4_hash_supported = B_FALSE;
1352         } else {
1353                 /*
1354                  * Maximum number of exclusive RSS contexts.
1355                  * EF10 hardware supports 64 in total, but 6 are reserved
1356                  * for shared contexts. They are a global resource so
1357                  * not all may be available.
1358                  */
1359                 encp->enc_rx_scale_max_exclusive_contexts = 64 - 6;
1360
1361                 encp->enc_rx_scale_hash_alg_mask =
1362                     (1U << EFX_RX_HASHALG_TOEPLITZ);
1363
1364                 /*
1365                  * It is possible to use port numbers as
1366                  * the input data for hash computation.
1367                  */
1368                 encp->enc_rx_scale_l4_hash_supported = B_TRUE;
1369         }
1370 #endif /* EFSYS_OPT_RX_SCALE */
1371
1372         /* Check if the firmware supports "FLAG" and "MARK" filter actions */
1373         if (CAP_FLAGS2(req, FILTER_ACTION_FLAG))
1374                 encp->enc_filter_action_flag_supported = B_TRUE;
1375         else
1376                 encp->enc_filter_action_flag_supported = B_FALSE;
1377
1378         if (CAP_FLAGS2(req, FILTER_ACTION_MARK))
1379                 encp->enc_filter_action_mark_supported = B_TRUE;
1380         else
1381                 encp->enc_filter_action_mark_supported = B_FALSE;
1382
1383         /* Get maximum supported value for "MARK" filter action */
1384         if (req.emr_out_length_used >= MC_CMD_GET_CAPABILITIES_V5_OUT_LEN)
1385                 encp->enc_filter_action_mark_max = MCDI_OUT_DWORD(req,
1386                     GET_CAPABILITIES_V5_OUT_FILTER_ACTION_MARK_MAX);
1387         else
1388                 encp->enc_filter_action_mark_max = 0;
1389
1390 #undef CAP_FLAGS1
1391 #undef CAP_FLAGS2
1392
1393         return (0);
1394
1395 #if EFSYS_OPT_RX_SCALE
1396 fail4:
1397         EFSYS_PROBE(fail4);
1398 #endif /* EFSYS_OPT_RX_SCALE */
1399 fail3:
1400         EFSYS_PROBE(fail3);
1401 fail2:
1402         EFSYS_PROBE(fail2);
1403 fail1:
1404         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1405
1406         return (rc);
1407 }
1408
1409
1410 #define EF10_LEGACY_PF_PRIVILEGE_MASK                                   \
1411         (MC_CMD_PRIVILEGE_MASK_IN_GRP_ADMIN                     |       \
1412         MC_CMD_PRIVILEGE_MASK_IN_GRP_LINK                       |       \
1413         MC_CMD_PRIVILEGE_MASK_IN_GRP_ONLOAD                     |       \
1414         MC_CMD_PRIVILEGE_MASK_IN_GRP_PTP                        |       \
1415         MC_CMD_PRIVILEGE_MASK_IN_GRP_INSECURE_FILTERS           |       \
1416         MC_CMD_PRIVILEGE_MASK_IN_GRP_MAC_SPOOFING               |       \
1417         MC_CMD_PRIVILEGE_MASK_IN_GRP_UNICAST                    |       \
1418         MC_CMD_PRIVILEGE_MASK_IN_GRP_MULTICAST                  |       \
1419         MC_CMD_PRIVILEGE_MASK_IN_GRP_BROADCAST                  |       \
1420         MC_CMD_PRIVILEGE_MASK_IN_GRP_ALL_MULTICAST              |       \
1421         MC_CMD_PRIVILEGE_MASK_IN_GRP_PROMISCUOUS)
1422
1423 #define EF10_LEGACY_VF_PRIVILEGE_MASK   0
1424
1425
1426         __checkReturn           efx_rc_t
1427 ef10_get_privilege_mask(
1428         __in                    efx_nic_t *enp,
1429         __out                   uint32_t *maskp)
1430 {
1431         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1432         uint32_t mask;
1433         efx_rc_t rc;
1434
1435         if ((rc = efx_mcdi_privilege_mask(enp, encp->enc_pf, encp->enc_vf,
1436                                             &mask)) != 0) {
1437                 if (rc != ENOTSUP)
1438                         goto fail1;
1439
1440                 /* Fallback for old firmware without privilege mask support */
1441                 if (EFX_PCI_FUNCTION_IS_PF(encp)) {
1442                         /* Assume PF has admin privilege */
1443                         mask = EF10_LEGACY_PF_PRIVILEGE_MASK;
1444                 } else {
1445                         /* VF is always unprivileged by default */
1446                         mask = EF10_LEGACY_VF_PRIVILEGE_MASK;
1447                 }
1448         }
1449
1450         *maskp = mask;
1451
1452         return (0);
1453
1454 fail1:
1455         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1456
1457         return (rc);
1458 }
1459
1460
1461 #define EFX_EXT_PORT_MAX        4
1462 #define EFX_EXT_PORT_NA         0xFF
1463
1464 /*
1465  * Table of mapping schemes from port number to external number.
1466  *
1467  * Each port number ultimately corresponds to a connector: either as part of
1468  * a cable assembly attached to a module inserted in an SFP+/QSFP+ cage on
1469  * the board, or fixed to the board (e.g. 10GBASE-T magjack on SFN5121T
1470  * "Salina"). In general:
1471  *
1472  * Port number (0-based)
1473  *     |
1474  *   port mapping (n:1)
1475  *     |
1476  *     v
1477  * External port number (1-based)
1478  *     |
1479  *   fixed (1:1) or cable assembly (1:m)
1480  *     |
1481  *     v
1482  * Connector
1483  *
1484  * The external numbering refers to the cages or magjacks on the board,
1485  * as visibly annotated on the board or back panel. This table describes
1486  * how to determine which external cage/magjack corresponds to the port
1487  * numbers used by the driver.
1488  *
1489  * The count of consecutive port numbers that map to each external number,
1490  * is determined by the chip family and the current port mode.
1491  *
1492  * For the Huntington family, the current port mode cannot be discovered,
1493  * but a single mapping is used by all modes for a given chip variant,
1494  * so the mapping used is instead the last match in the table to the full
1495  * set of port modes to which the NIC can be configured. Therefore the
1496  * ordering of entries in the mapping table is significant.
1497  */
1498 static struct ef10_external_port_map_s {
1499         efx_family_t    family;
1500         uint32_t        modes_mask;
1501         uint8_t         base_port[EFX_EXT_PORT_MAX];
1502 }       __ef10_external_port_mappings[] = {
1503         /*
1504          * Modes used by Huntington family controllers where each port
1505          * number maps to a separate cage.
1506          * SFN7x22F (Torino):
1507          *      port 0 -> cage 1
1508          *      port 1 -> cage 2
1509          * SFN7xx4F (Pavia):
1510          *      port 0 -> cage 1
1511          *      port 1 -> cage 2
1512          *      port 2 -> cage 3
1513          *      port 3 -> cage 4
1514          */
1515         {
1516                 EFX_FAMILY_HUNTINGTON,
1517                 (1U << TLV_PORT_MODE_10G) |                     /* mode 0 */
1518                 (1U << TLV_PORT_MODE_10G_10G) |                 /* mode 2 */
1519                 (1U << TLV_PORT_MODE_10G_10G_10G_10G),          /* mode 4 */
1520                 { 0, 1, 2, 3 }
1521         },
1522         /*
1523          * Modes which for Huntington identify a chip variant where 2
1524          * adjacent port numbers map to each cage.
1525          * SFN7x42Q (Monza):
1526          *      port 0 -> cage 1
1527          *      port 1 -> cage 1
1528          *      port 2 -> cage 2
1529          *      port 3 -> cage 2
1530          */
1531         {
1532                 EFX_FAMILY_HUNTINGTON,
1533                 (1U << TLV_PORT_MODE_40G) |                     /* mode 1 */
1534                 (1U << TLV_PORT_MODE_40G_40G) |                 /* mode 3 */
1535                 (1U << TLV_PORT_MODE_40G_10G_10G) |             /* mode 6 */
1536                 (1U << TLV_PORT_MODE_10G_10G_40G),              /* mode 7 */
1537                 { 0, 2, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA }
1538         },
1539         /*
1540          * Modes that on Medford allocate each port number to a separate
1541          * cage.
1542          *      port 0 -> cage 1
1543          *      port 1 -> cage 2
1544          *      port 2 -> cage 3
1545          *      port 3 -> cage 4
1546          */
1547         {
1548                 EFX_FAMILY_MEDFORD,
1549                 (1U << TLV_PORT_MODE_1x1_NA) |                  /* mode 0 */
1550                 (1U << TLV_PORT_MODE_1x4_NA) |                  /* mode 1 */
1551                 (1U << TLV_PORT_MODE_1x1_1x1),                  /* mode 2 */
1552                 { 0, 1, 2, 3 }
1553         },
1554         /*
1555          * Modes that on Medford allocate 2 adjacent port numbers to each
1556          * cage.
1557          *      port 0 -> cage 1
1558          *      port 1 -> cage 1
1559          *      port 2 -> cage 2
1560          *      port 3 -> cage 2
1561          */
1562         {
1563                 EFX_FAMILY_MEDFORD,
1564                 (1U << TLV_PORT_MODE_1x4_1x4) |                 /* mode 3 */
1565                 (1U << TLV_PORT_MODE_2x1_2x1) |                 /* mode 5 */
1566                 (1U << TLV_PORT_MODE_1x4_2x1) |                 /* mode 6 */
1567                 (1U << TLV_PORT_MODE_2x1_1x4) |                 /* mode 7 */
1568                 /* Do not use 10G_10G_10G_10G_Q1_Q2 (see bug63270) */
1569                 (1U << TLV_PORT_MODE_10G_10G_10G_10G_Q1_Q2),    /* mode 9 */
1570                 { 0, 2, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA }
1571         },
1572         /*
1573          * Modes that on Medford allocate 4 adjacent port numbers to
1574          * cage 1.
1575          *      port 0 -> cage 1
1576          *      port 1 -> cage 1
1577          *      port 2 -> cage 1
1578          *      port 3 -> cage 1
1579          */
1580         {
1581                 EFX_FAMILY_MEDFORD,
1582                 /* Do not use 10G_10G_10G_10G_Q1 (see bug63270) */
1583                 (1U << TLV_PORT_MODE_4x1_NA),                   /* mode 4 */
1584                 { 0, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA }
1585         },
1586         /*
1587          * Modes that on Medford allocate 4 adjacent port numbers to
1588          * cage 2.
1589          *      port 0 -> cage 2
1590          *      port 1 -> cage 2
1591          *      port 2 -> cage 2
1592          *      port 3 -> cage 2
1593          */
1594         {
1595                 EFX_FAMILY_MEDFORD,
1596                 (1U << TLV_PORT_MODE_NA_4x1),                   /* mode 8 */
1597                 { EFX_EXT_PORT_NA, 0, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA }
1598         },
1599         /*
1600          * Modes that on Medford2 allocate each port number to a separate
1601          * cage.
1602          *      port 0 -> cage 1
1603          *      port 1 -> cage 2
1604          *      port 2 -> cage 3
1605          *      port 3 -> cage 4
1606          */
1607         {
1608                 EFX_FAMILY_MEDFORD2,
1609                 (1U << TLV_PORT_MODE_1x1_NA) |                  /* mode 0 */
1610                 (1U << TLV_PORT_MODE_1x4_NA) |                  /* mode 1 */
1611                 (1U << TLV_PORT_MODE_1x1_1x1) |                 /* mode 2 */
1612                 (1U << TLV_PORT_MODE_1x4_1x4) |                 /* mode 3 */
1613                 (1U << TLV_PORT_MODE_1x2_NA) |                  /* mode 10 */
1614                 (1U << TLV_PORT_MODE_1x2_1x2) |                 /* mode 12 */
1615                 (1U << TLV_PORT_MODE_1x4_1x2) |                 /* mode 15 */
1616                 (1U << TLV_PORT_MODE_1x2_1x4),                  /* mode 16 */
1617                 { 0, 1, 2, 3 }
1618         },
1619         /*
1620          * Modes that on Medford2 allocate 1 port to cage 1 and the rest
1621          * to cage 2.
1622          *      port 0 -> cage 1
1623          *      port 1 -> cage 2
1624          *      port 2 -> cage 2
1625          */
1626         {
1627                 EFX_FAMILY_MEDFORD2,
1628                 (1U << TLV_PORT_MODE_1x2_2x1) |                 /* mode 17 */
1629                 (1U << TLV_PORT_MODE_1x4_2x1),                  /* mode 6 */
1630                 { 0, 1, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA }
1631         },
1632         /*
1633          * Modes that on Medford2 allocate 2 adjacent port numbers to cage 1
1634          * and the rest to cage 2.
1635          *      port 0 -> cage 1
1636          *      port 1 -> cage 1
1637          *      port 2 -> cage 2
1638          *      port 3 -> cage 2
1639          */
1640         {
1641                 EFX_FAMILY_MEDFORD2,
1642                 (1U << TLV_PORT_MODE_2x1_2x1) |                 /* mode 4 */
1643                 (1U << TLV_PORT_MODE_2x1_1x4) |                 /* mode 7 */
1644                 (1U << TLV_PORT_MODE_2x2_NA) |                  /* mode 13 */
1645                 (1U << TLV_PORT_MODE_2x1_1x2),                  /* mode 18 */
1646                 { 0, 2, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA }
1647         },
1648         /*
1649          * Modes that on Medford2 allocate up to 4 adjacent port numbers
1650          * to cage 1.
1651          *      port 0 -> cage 1
1652          *      port 1 -> cage 1
1653          *      port 2 -> cage 1
1654          *      port 3 -> cage 1
1655          */
1656         {
1657                 EFX_FAMILY_MEDFORD2,
1658                 (1U << TLV_PORT_MODE_4x1_NA),                   /* mode 5 */
1659                 { 0, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA }
1660         },
1661         /*
1662          * Modes that on Medford2 allocate up to 4 adjacent port numbers
1663          * to cage 2.
1664          *      port 0 -> cage 2
1665          *      port 1 -> cage 2
1666          *      port 2 -> cage 2
1667          *      port 3 -> cage 2
1668          */
1669         {
1670                 EFX_FAMILY_MEDFORD2,
1671                 (1U << TLV_PORT_MODE_NA_4x1) |                  /* mode 8 */
1672                 (1U << TLV_PORT_MODE_NA_1x2) |                  /* mode 11 */
1673                 (1U << TLV_PORT_MODE_NA_2x2),                   /* mode 14 */
1674                 { EFX_EXT_PORT_NA, 0, EFX_EXT_PORT_NA, EFX_EXT_PORT_NA }
1675         },
1676 };
1677
1678 static  __checkReturn   efx_rc_t
1679 ef10_external_port_mapping(
1680         __in            efx_nic_t *enp,
1681         __in            uint32_t port,
1682         __out           uint8_t *external_portp)
1683 {
1684         efx_rc_t rc;
1685         int i;
1686         uint32_t port_modes;
1687         uint32_t matches;
1688         uint32_t current;
1689         struct ef10_external_port_map_s *mapp = NULL;
1690         int ext_index = port; /* Default 1-1 mapping */
1691
1692         if ((rc = efx_mcdi_get_port_modes(enp, &port_modes, &current,
1693                     NULL)) != 0) {
1694                 /*
1695                  * No current port mode information (i.e. Huntington)
1696                  * - infer mapping from available modes
1697                  */
1698                 if ((rc = efx_mcdi_get_port_modes(enp,
1699                             &port_modes, NULL, NULL)) != 0) {
1700                         /*
1701                          * No port mode information available
1702                          * - use default mapping
1703                          */
1704                         goto out;
1705                 }
1706         } else {
1707                 /* Only need to scan the current mode */
1708                 port_modes = 1 << current;
1709         }
1710
1711         /*
1712          * Infer the internal port -> external number mapping from
1713          * the possible port modes for this NIC.
1714          */
1715         for (i = 0; i < EFX_ARRAY_SIZE(__ef10_external_port_mappings); ++i) {
1716                 struct ef10_external_port_map_s *eepmp =
1717                     &__ef10_external_port_mappings[i];
1718                 if (eepmp->family != enp->en_family)
1719                         continue;
1720                 matches = (eepmp->modes_mask & port_modes);
1721                 if (matches != 0) {
1722                         /*
1723                          * Some modes match. For some Huntington boards
1724                          * there will be multiple matches. The mapping on the
1725                          * last match is used.
1726                          */
1727                         mapp = eepmp;
1728                         port_modes &= ~matches;
1729                 }
1730         }
1731
1732         if (port_modes != 0) {
1733                 /* Some advertised modes are not supported */
1734                 rc = ENOTSUP;
1735                 goto fail1;
1736         }
1737
1738 out:
1739         if (mapp != NULL) {
1740                 /*
1741                  * External ports are assigned a sequence of consecutive
1742                  * port numbers, so find the one with the closest base_port.
1743                  */
1744                 uint32_t delta = EFX_EXT_PORT_NA;
1745
1746                 for (i = 0; i < EFX_EXT_PORT_MAX; i++) {
1747                         uint32_t base = mapp->base_port[i];
1748                         if ((base != EFX_EXT_PORT_NA) && (base <= port)) {
1749                                 if ((port - base) < delta) {
1750                                         delta = (port - base);
1751                                         ext_index = i;
1752                                 }
1753                         }
1754                 }
1755         }
1756         *external_portp = (uint8_t)(ext_index + 1);
1757
1758         return (0);
1759
1760 fail1:
1761         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1762
1763         return (rc);
1764 }
1765
1766 static  __checkReturn   efx_rc_t
1767 ef10_set_workaround_bug26807(
1768         __in            efx_nic_t *enp)
1769 {
1770         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1771         uint32_t flags;
1772         efx_rc_t rc;
1773
1774         /*
1775          * If the bug26807 workaround is enabled, then firmware has enabled
1776          * support for chained multicast filters. Firmware will reset (FLR)
1777          * functions which have filters in the hardware filter table when the
1778          * workaround is enabled/disabled.
1779          *
1780          * We must recheck if the workaround is enabled after inserting the
1781          * first hardware filter, in case it has been changed since this check.
1782          */
1783         rc = efx_mcdi_set_workaround(enp, MC_CMD_WORKAROUND_BUG26807,
1784             B_TRUE, &flags);
1785         if (rc == 0) {
1786                 encp->enc_bug26807_workaround = B_TRUE;
1787                 if (flags & (1 << MC_CMD_WORKAROUND_EXT_OUT_FLR_DONE_LBN)) {
1788                         /*
1789                          * Other functions had installed filters before the
1790                          * workaround was enabled, and they have been reset
1791                          * by firmware.
1792                          */
1793                         EFSYS_PROBE(bug26807_workaround_flr_done);
1794                         /* FIXME: bump MC warm boot count ? */
1795                 }
1796         } else if (rc == EACCES) {
1797                 /*
1798                  * Unprivileged functions cannot enable the workaround in older
1799                  * firmware.
1800                  */
1801                 encp->enc_bug26807_workaround = B_FALSE;
1802         } else if ((rc == ENOTSUP) || (rc == ENOENT)) {
1803                 encp->enc_bug26807_workaround = B_FALSE;
1804         } else {
1805                 goto fail1;
1806         }
1807
1808         return (0);
1809
1810 fail1:
1811         EFSYS_PROBE1(fail1, efx_rc_t, rc);
1812
1813         return (rc);
1814 }
1815
1816 static  __checkReturn   efx_rc_t
1817 ef10_nic_board_cfg(
1818         __in            efx_nic_t *enp)
1819 {
1820         const efx_nic_ops_t *enop = enp->en_enop;
1821         efx_mcdi_iface_t *emip = &(enp->en_mcdi.em_emip);
1822         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
1823         ef10_link_state_t els;
1824         efx_port_t *epp = &(enp->en_port);
1825         uint32_t board_type = 0;
1826         uint32_t base, nvec;
1827         uint32_t port;
1828         uint32_t mask;
1829         uint32_t pf;
1830         uint32_t vf;
1831         uint8_t mac_addr[6] = { 0 };
1832         efx_rc_t rc;
1833
1834         /* Get the (zero-based) MCDI port number */
1835         if ((rc = efx_mcdi_get_port_assignment(enp, &port)) != 0)
1836                 goto fail1;
1837
1838         /* EFX MCDI interface uses one-based port numbers */
1839         emip->emi_port = port + 1;
1840
1841         encp->enc_assigned_port = port;
1842
1843         if ((rc = ef10_external_port_mapping(enp, port,
1844                     &encp->enc_external_port)) != 0)
1845                 goto fail2;
1846
1847         /*
1848          * Get PCIe function number from firmware (used for
1849          * per-function privilege and dynamic config info).
1850          *  - PCIe PF: pf = PF number, vf = 0xffff.
1851          *  - PCIe VF: pf = parent PF, vf = VF number.
1852          */
1853         if ((rc = efx_mcdi_get_function_info(enp, &pf, &vf)) != 0)
1854                 goto fail3;
1855
1856         encp->enc_pf = pf;
1857         encp->enc_vf = vf;
1858
1859         /* MAC address for this function */
1860         if (EFX_PCI_FUNCTION_IS_PF(encp)) {
1861                 rc = efx_mcdi_get_mac_address_pf(enp, mac_addr);
1862 #if EFSYS_OPT_ALLOW_UNCONFIGURED_NIC
1863                 /*
1864                  * Disable static config checking, ONLY for manufacturing test
1865                  * and setup at the factory, to allow the static config to be
1866                  * installed.
1867                  */
1868 #else /* EFSYS_OPT_ALLOW_UNCONFIGURED_NIC */
1869                 if ((rc == 0) && (mac_addr[0] & 0x02)) {
1870                         /*
1871                          * If the static config does not include a global MAC
1872                          * address pool then the board may return a locally
1873                          * administered MAC address (this should only happen on
1874                          * incorrectly programmed boards).
1875                          */
1876                         rc = EINVAL;
1877                 }
1878 #endif /* EFSYS_OPT_ALLOW_UNCONFIGURED_NIC */
1879         } else {
1880                 rc = efx_mcdi_get_mac_address_vf(enp, mac_addr);
1881         }
1882         if (rc != 0)
1883                 goto fail4;
1884
1885         EFX_MAC_ADDR_COPY(encp->enc_mac_addr, mac_addr);
1886
1887         /* Board configuration (legacy) */
1888         rc = efx_mcdi_get_board_cfg(enp, &board_type, NULL, NULL);
1889         if (rc != 0) {
1890                 /* Unprivileged functions may not be able to read board cfg */
1891                 if (rc == EACCES)
1892                         board_type = 0;
1893                 else
1894                         goto fail5;
1895         }
1896
1897         encp->enc_board_type = board_type;
1898         encp->enc_clk_mult = 1; /* not used for EF10 */
1899
1900         /* Fill out fields in enp->en_port and enp->en_nic_cfg from MCDI */
1901         if ((rc = efx_mcdi_get_phy_cfg(enp)) != 0)
1902                 goto fail6;
1903
1904         /*
1905          * Firmware with support for *_FEC capability bits does not
1906          * report that the corresponding *_FEC_REQUESTED bits are supported.
1907          * Add them here so that drivers understand that they are supported.
1908          */
1909         if (epp->ep_phy_cap_mask & (1u << EFX_PHY_CAP_BASER_FEC))
1910                 epp->ep_phy_cap_mask |=
1911                     (1u << EFX_PHY_CAP_BASER_FEC_REQUESTED);
1912         if (epp->ep_phy_cap_mask & (1u << EFX_PHY_CAP_RS_FEC))
1913                 epp->ep_phy_cap_mask |=
1914                     (1u << EFX_PHY_CAP_RS_FEC_REQUESTED);
1915         if (epp->ep_phy_cap_mask & (1u << EFX_PHY_CAP_25G_BASER_FEC))
1916                 epp->ep_phy_cap_mask |=
1917                     (1u << EFX_PHY_CAP_25G_BASER_FEC_REQUESTED);
1918
1919         /* Obtain the default PHY advertised capabilities */
1920         if ((rc = ef10_phy_get_link(enp, &els)) != 0)
1921                 goto fail7;
1922         epp->ep_default_adv_cap_mask = els.epls.epls_adv_cap_mask;
1923         epp->ep_adv_cap_mask = els.epls.epls_adv_cap_mask;
1924
1925         /* Check capabilities of running datapath firmware */
1926         if ((rc = ef10_get_datapath_caps(enp)) != 0)
1927                 goto fail8;
1928
1929         /*
1930          * Huntington RXDP firmware inserts a 0 or 14 byte prefix.
1931          * We only support the 14 byte prefix here.
1932          */
1933         if (encp->enc_rx_prefix_size != 14) {
1934                 rc = ENOTSUP;
1935                 goto fail9;
1936         }
1937
1938         /* Alignment for WPTR updates */
1939         encp->enc_rx_push_align = EF10_RX_WPTR_ALIGN;
1940
1941         encp->enc_tx_dma_desc_size_max = EFX_MASK32(ESF_DZ_RX_KER_BYTE_CNT);
1942         /* No boundary crossing limits */
1943         encp->enc_tx_dma_desc_boundary = 0;
1944
1945         /*
1946          * Maximum number of bytes into the frame the TCP header can start for
1947          * firmware assisted TSO to work.
1948          */
1949         encp->enc_tx_tso_tcp_header_offset_limit = EF10_TCP_HEADER_OFFSET_LIMIT;
1950
1951         /*
1952          * Set resource limits for MC_CMD_ALLOC_VIS. Note that we cannot use
1953          * MC_CMD_GET_RESOURCE_LIMITS here as that reports the available
1954          * resources (allocated to this PCIe function), which is zero until
1955          * after we have allocated VIs.
1956          */
1957         encp->enc_evq_limit = 1024;
1958         encp->enc_rxq_limit = EFX_RXQ_LIMIT_TARGET;
1959         encp->enc_txq_limit = EFX_TXQ_LIMIT_TARGET;
1960
1961         encp->enc_buftbl_limit = UINT32_MAX;
1962
1963         /* Get interrupt vector limits */
1964         if ((rc = efx_mcdi_get_vector_cfg(enp, &base, &nvec, NULL)) != 0) {
1965                 if (EFX_PCI_FUNCTION_IS_PF(encp))
1966                         goto fail10;
1967
1968                 /* Ignore error (cannot query vector limits from a VF). */
1969                 base = 0;
1970                 nvec = 1024;
1971         }
1972         encp->enc_intr_vec_base = base;
1973         encp->enc_intr_limit = nvec;
1974
1975         /*
1976          * Get the current privilege mask. Note that this may be modified
1977          * dynamically, so this value is informational only. DO NOT use
1978          * the privilege mask to check for sufficient privileges, as that
1979          * can result in time-of-check/time-of-use bugs.
1980          */
1981         if ((rc = ef10_get_privilege_mask(enp, &mask)) != 0)
1982                 goto fail11;
1983         encp->enc_privilege_mask = mask;
1984
1985         if ((rc = ef10_set_workaround_bug26807(enp)) != 0)
1986                 goto fail11;
1987
1988         /* Get remaining controller-specific board config */
1989         if ((rc = enop->eno_board_cfg(enp)) != 0)
1990                 if (rc != EACCES)
1991                         goto fail12;
1992
1993         return (0);
1994
1995 fail12:
1996         EFSYS_PROBE(fail12);
1997 fail11:
1998         EFSYS_PROBE(fail11);
1999 fail10:
2000         EFSYS_PROBE(fail10);
2001 fail9:
2002         EFSYS_PROBE(fail9);
2003 fail8:
2004         EFSYS_PROBE(fail8);
2005 fail7:
2006         EFSYS_PROBE(fail7);
2007 fail6:
2008         EFSYS_PROBE(fail6);
2009 fail5:
2010         EFSYS_PROBE(fail5);
2011 fail4:
2012         EFSYS_PROBE(fail4);
2013 fail3:
2014         EFSYS_PROBE(fail3);
2015 fail2:
2016         EFSYS_PROBE(fail2);
2017 fail1:
2018         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2019
2020         return (rc);
2021 }
2022
2023         __checkReturn   efx_rc_t
2024 ef10_nic_probe(
2025         __in            efx_nic_t *enp)
2026 {
2027         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
2028         efx_drv_cfg_t *edcp = &(enp->en_drv_cfg);
2029         efx_rc_t rc;
2030
2031         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
2032
2033         /* Read and clear any assertion state */
2034         if ((rc = efx_mcdi_read_assertion(enp)) != 0)
2035                 goto fail1;
2036
2037         /* Exit the assertion handler */
2038         if ((rc = efx_mcdi_exit_assertion_handler(enp)) != 0)
2039                 if (rc != EACCES)
2040                         goto fail2;
2041
2042         if ((rc = efx_mcdi_drv_attach(enp, B_TRUE)) != 0)
2043                 goto fail3;
2044
2045         if ((rc = ef10_nic_board_cfg(enp)) != 0)
2046                 goto fail4;
2047
2048         /*
2049          * Set default driver config limits (based on board config).
2050          *
2051          * FIXME: For now allocate a fixed number of VIs which is likely to be
2052          * sufficient and small enough to allow multiple functions on the same
2053          * port.
2054          */
2055         edcp->edc_min_vi_count = edcp->edc_max_vi_count =
2056             MIN(128, MAX(encp->enc_rxq_limit, encp->enc_txq_limit));
2057
2058         /* The client driver must configure and enable PIO buffer support */
2059         edcp->edc_max_piobuf_count = 0;
2060         edcp->edc_pio_alloc_size = 0;
2061
2062 #if EFSYS_OPT_MAC_STATS
2063         /* Wipe the MAC statistics */
2064         if ((rc = efx_mcdi_mac_stats_clear(enp)) != 0)
2065                 goto fail5;
2066 #endif
2067
2068 #if EFSYS_OPT_LOOPBACK
2069         if ((rc = efx_mcdi_get_loopback_modes(enp)) != 0)
2070                 goto fail6;
2071 #endif
2072
2073 #if EFSYS_OPT_MON_STATS
2074         if ((rc = mcdi_mon_cfg_build(enp)) != 0) {
2075                 /* Unprivileged functions do not have access to sensors */
2076                 if (rc != EACCES)
2077                         goto fail7;
2078         }
2079 #endif
2080
2081         encp->enc_features = enp->en_features;
2082
2083         return (0);
2084
2085 #if EFSYS_OPT_MON_STATS
2086 fail7:
2087         EFSYS_PROBE(fail7);
2088 #endif
2089 #if EFSYS_OPT_LOOPBACK
2090 fail6:
2091         EFSYS_PROBE(fail6);
2092 #endif
2093 #if EFSYS_OPT_MAC_STATS
2094 fail5:
2095         EFSYS_PROBE(fail5);
2096 #endif
2097 fail4:
2098         EFSYS_PROBE(fail4);
2099 fail3:
2100         EFSYS_PROBE(fail3);
2101 fail2:
2102         EFSYS_PROBE(fail2);
2103 fail1:
2104         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2105
2106         return (rc);
2107 }
2108
2109         __checkReturn   efx_rc_t
2110 ef10_nic_set_drv_limits(
2111         __inout         efx_nic_t *enp,
2112         __in            efx_drv_limits_t *edlp)
2113 {
2114         efx_nic_cfg_t *encp = &(enp->en_nic_cfg);
2115         efx_drv_cfg_t *edcp = &(enp->en_drv_cfg);
2116         uint32_t min_evq_count, max_evq_count;
2117         uint32_t min_rxq_count, max_rxq_count;
2118         uint32_t min_txq_count, max_txq_count;
2119         efx_rc_t rc;
2120
2121         if (edlp == NULL) {
2122                 rc = EINVAL;
2123                 goto fail1;
2124         }
2125
2126         /* Get minimum required and maximum usable VI limits */
2127         min_evq_count = MIN(edlp->edl_min_evq_count, encp->enc_evq_limit);
2128         min_rxq_count = MIN(edlp->edl_min_rxq_count, encp->enc_rxq_limit);
2129         min_txq_count = MIN(edlp->edl_min_txq_count, encp->enc_txq_limit);
2130
2131         edcp->edc_min_vi_count =
2132             MAX(min_evq_count, MAX(min_rxq_count, min_txq_count));
2133
2134         max_evq_count = MIN(edlp->edl_max_evq_count, encp->enc_evq_limit);
2135         max_rxq_count = MIN(edlp->edl_max_rxq_count, encp->enc_rxq_limit);
2136         max_txq_count = MIN(edlp->edl_max_txq_count, encp->enc_txq_limit);
2137
2138         edcp->edc_max_vi_count =
2139             MAX(max_evq_count, MAX(max_rxq_count, max_txq_count));
2140
2141         /*
2142          * Check limits for sub-allocated piobuf blocks.
2143          * PIO is optional, so don't fail if the limits are incorrect.
2144          */
2145         if ((encp->enc_piobuf_size == 0) ||
2146             (encp->enc_piobuf_limit == 0) ||
2147             (edlp->edl_min_pio_alloc_size == 0) ||
2148             (edlp->edl_min_pio_alloc_size > encp->enc_piobuf_size)) {
2149                 /* Disable PIO */
2150                 edcp->edc_max_piobuf_count = 0;
2151                 edcp->edc_pio_alloc_size = 0;
2152         } else {
2153                 uint32_t blk_size, blk_count, blks_per_piobuf;
2154
2155                 blk_size =
2156                     MAX(edlp->edl_min_pio_alloc_size,
2157                             encp->enc_piobuf_min_alloc_size);
2158
2159                 blks_per_piobuf = encp->enc_piobuf_size / blk_size;
2160                 EFSYS_ASSERT3U(blks_per_piobuf, <=, 32);
2161
2162                 blk_count = (encp->enc_piobuf_limit * blks_per_piobuf);
2163
2164                 /* A zero max pio alloc count means unlimited */
2165                 if ((edlp->edl_max_pio_alloc_count > 0) &&
2166                     (edlp->edl_max_pio_alloc_count < blk_count)) {
2167                         blk_count = edlp->edl_max_pio_alloc_count;
2168                 }
2169
2170                 edcp->edc_pio_alloc_size = blk_size;
2171                 edcp->edc_max_piobuf_count =
2172                     (blk_count + (blks_per_piobuf - 1)) / blks_per_piobuf;
2173         }
2174
2175         return (0);
2176
2177 fail1:
2178         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2179
2180         return (rc);
2181 }
2182
2183
2184         __checkReturn   efx_rc_t
2185 ef10_nic_reset(
2186         __in            efx_nic_t *enp)
2187 {
2188         efx_mcdi_req_t req;
2189         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_ENTITY_RESET_IN_LEN,
2190                 MC_CMD_ENTITY_RESET_OUT_LEN);
2191         efx_rc_t rc;
2192
2193         /* ef10_nic_reset() is called to recover from BADASSERT failures. */
2194         if ((rc = efx_mcdi_read_assertion(enp)) != 0)
2195                 goto fail1;
2196         if ((rc = efx_mcdi_exit_assertion_handler(enp)) != 0)
2197                 goto fail2;
2198
2199         req.emr_cmd = MC_CMD_ENTITY_RESET;
2200         req.emr_in_buf = payload;
2201         req.emr_in_length = MC_CMD_ENTITY_RESET_IN_LEN;
2202         req.emr_out_buf = payload;
2203         req.emr_out_length = MC_CMD_ENTITY_RESET_OUT_LEN;
2204
2205         MCDI_IN_POPULATE_DWORD_1(req, ENTITY_RESET_IN_FLAG,
2206             ENTITY_RESET_IN_FUNCTION_RESOURCE_RESET, 1);
2207
2208         efx_mcdi_execute(enp, &req);
2209
2210         if (req.emr_rc != 0) {
2211                 rc = req.emr_rc;
2212                 goto fail3;
2213         }
2214
2215         /* Clear RX/TX DMA queue errors */
2216         enp->en_reset_flags &= ~(EFX_RESET_RXQ_ERR | EFX_RESET_TXQ_ERR);
2217
2218         return (0);
2219
2220 fail3:
2221         EFSYS_PROBE(fail3);
2222 fail2:
2223         EFSYS_PROBE(fail2);
2224 fail1:
2225         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2226
2227         return (rc);
2228 }
2229
2230 static  __checkReturn   efx_rc_t
2231 ef10_upstream_port_vadaptor_alloc(
2232         __in            efx_nic_t *enp)
2233 {
2234         uint32_t retry;
2235         uint32_t delay_us;
2236         efx_rc_t rc;
2237
2238         /*
2239          * On a VF, this may fail with MC_CMD_ERR_NO_EVB_PORT (ENOENT) if the PF
2240          * driver has yet to bring up the EVB port. See bug 56147. In this case,
2241          * retry the request several times after waiting a while. The wait time
2242          * between retries starts small (10ms) and exponentially increases.
2243          * Total wait time is a little over two seconds. Retry logic in the
2244          * client driver may mean this whole loop is repeated if it continues to
2245          * fail.
2246          */
2247         retry = 0;
2248         delay_us = 10000;
2249         while ((rc = efx_mcdi_vadaptor_alloc(enp, EVB_PORT_ID_ASSIGNED)) != 0) {
2250                 if (EFX_PCI_FUNCTION_IS_PF(&enp->en_nic_cfg) ||
2251                     (rc != ENOENT)) {
2252                         /*
2253                          * Do not retry alloc for PF, or for other errors on
2254                          * a VF.
2255                          */
2256                         goto fail1;
2257                 }
2258
2259                 /* VF startup before PF is ready. Retry allocation. */
2260                 if (retry > 5) {
2261                         /* Too many attempts */
2262                         rc = EINVAL;
2263                         goto fail2;
2264                 }
2265                 EFSYS_PROBE1(mcdi_no_evb_port_retry, int, retry);
2266                 EFSYS_SLEEP(delay_us);
2267                 retry++;
2268                 if (delay_us < 500000)
2269                         delay_us <<= 2;
2270         }
2271
2272         return (0);
2273
2274 fail2:
2275         EFSYS_PROBE(fail2);
2276 fail1:
2277         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2278
2279         return (rc);
2280 }
2281
2282         __checkReturn   efx_rc_t
2283 ef10_nic_init(
2284         __in            efx_nic_t *enp)
2285 {
2286         efx_drv_cfg_t *edcp = &(enp->en_drv_cfg);
2287         uint32_t min_vi_count, max_vi_count;
2288         uint32_t vi_count, vi_base, vi_shift;
2289         uint32_t i;
2290         uint32_t vi_window_size;
2291         efx_rc_t rc;
2292         boolean_t alloc_vadaptor = B_TRUE;
2293
2294         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
2295
2296         /* Enable reporting of some events (e.g. link change) */
2297         if ((rc = efx_mcdi_log_ctrl(enp)) != 0)
2298                 goto fail1;
2299
2300         /* Allocate (optional) on-chip PIO buffers */
2301         ef10_nic_alloc_piobufs(enp, edcp->edc_max_piobuf_count);
2302
2303         /*
2304          * For best performance, PIO writes should use a write-combined
2305          * (WC) memory mapping. Using a separate WC mapping for the PIO
2306          * aperture of each VI would be a burden to drivers (and not
2307          * possible if the host page size is >4Kbyte).
2308          *
2309          * To avoid this we use a single uncached (UC) mapping for VI
2310          * register access, and a single WC mapping for extra VIs used
2311          * for PIO writes.
2312          *
2313          * Each piobuf must be linked to a VI in the WC mapping, and to
2314          * each VI that is using a sub-allocated block from the piobuf.
2315          */
2316         min_vi_count = edcp->edc_min_vi_count;
2317         max_vi_count =
2318             edcp->edc_max_vi_count + enp->en_arch.ef10.ena_piobuf_count;
2319
2320         /* Ensure that the previously attached driver's VIs are freed */
2321         if ((rc = efx_mcdi_free_vis(enp)) != 0)
2322                 goto fail2;
2323
2324         /*
2325          * Reserve VI resources (EVQ+RXQ+TXQ) for this PCIe function. If this
2326          * fails then retrying the request for fewer VI resources may succeed.
2327          */
2328         vi_count = 0;
2329         if ((rc = efx_mcdi_alloc_vis(enp, min_vi_count, max_vi_count,
2330                     &vi_base, &vi_count, &vi_shift)) != 0)
2331                 goto fail3;
2332
2333         EFSYS_PROBE2(vi_alloc, uint32_t, vi_base, uint32_t, vi_count);
2334
2335         if (vi_count < min_vi_count) {
2336                 rc = ENOMEM;
2337                 goto fail4;
2338         }
2339
2340         enp->en_arch.ef10.ena_vi_base = vi_base;
2341         enp->en_arch.ef10.ena_vi_count = vi_count;
2342         enp->en_arch.ef10.ena_vi_shift = vi_shift;
2343
2344         if (vi_count < min_vi_count + enp->en_arch.ef10.ena_piobuf_count) {
2345                 /* Not enough extra VIs to map piobufs */
2346                 ef10_nic_free_piobufs(enp);
2347         }
2348
2349         enp->en_arch.ef10.ena_pio_write_vi_base =
2350             vi_count - enp->en_arch.ef10.ena_piobuf_count;
2351
2352         EFSYS_ASSERT3U(enp->en_nic_cfg.enc_vi_window_shift, !=,
2353             EFX_VI_WINDOW_SHIFT_INVALID);
2354         EFSYS_ASSERT3U(enp->en_nic_cfg.enc_vi_window_shift, <=,
2355             EFX_VI_WINDOW_SHIFT_64K);
2356         vi_window_size = 1U << enp->en_nic_cfg.enc_vi_window_shift;
2357
2358         /* Save UC memory mapping details */
2359         enp->en_arch.ef10.ena_uc_mem_map_offset = 0;
2360         if (enp->en_arch.ef10.ena_piobuf_count > 0) {
2361                 enp->en_arch.ef10.ena_uc_mem_map_size =
2362                     (vi_window_size *
2363                     enp->en_arch.ef10.ena_pio_write_vi_base);
2364         } else {
2365                 enp->en_arch.ef10.ena_uc_mem_map_size =
2366                     (vi_window_size *
2367                     enp->en_arch.ef10.ena_vi_count);
2368         }
2369
2370         /* Save WC memory mapping details */
2371         enp->en_arch.ef10.ena_wc_mem_map_offset =
2372             enp->en_arch.ef10.ena_uc_mem_map_offset +
2373             enp->en_arch.ef10.ena_uc_mem_map_size;
2374
2375         enp->en_arch.ef10.ena_wc_mem_map_size =
2376             (vi_window_size *
2377             enp->en_arch.ef10.ena_piobuf_count);
2378
2379         /* Link piobufs to extra VIs in WC mapping */
2380         if (enp->en_arch.ef10.ena_piobuf_count > 0) {
2381                 for (i = 0; i < enp->en_arch.ef10.ena_piobuf_count; i++) {
2382                         rc = efx_mcdi_link_piobuf(enp,
2383                             enp->en_arch.ef10.ena_pio_write_vi_base + i,
2384                             enp->en_arch.ef10.ena_piobuf_handle[i]);
2385                         if (rc != 0)
2386                                 break;
2387                 }
2388         }
2389
2390         /*
2391          * For SR-IOV use case, vAdaptor is allocated for PF and associated VFs
2392          * during NIC initialization when vSwitch is created and vports are
2393          * allocated. Hence, skip vAdaptor allocation for EVB and update vport
2394          * id in NIC structure with the one allocated for PF.
2395          */
2396
2397         enp->en_vport_id = EVB_PORT_ID_ASSIGNED;
2398 #if EFSYS_OPT_EVB
2399         if ((enp->en_vswitchp != NULL) && (enp->en_vswitchp->ev_evcp != NULL)) {
2400                 /* For EVB use vport allocated on vswitch */
2401                 enp->en_vport_id = enp->en_vswitchp->ev_evcp->evc_vport_id;
2402                 alloc_vadaptor = B_FALSE;
2403         }
2404 #endif
2405         if (alloc_vadaptor != B_FALSE) {
2406                 /* Allocate a vAdaptor attached to our upstream vPort/pPort */
2407                 if ((rc = ef10_upstream_port_vadaptor_alloc(enp)) != 0)
2408                         goto fail5;
2409         }
2410         enp->en_nic_cfg.enc_mcdi_max_payload_length = MCDI_CTL_SDU_LEN_MAX_V2;
2411
2412         return (0);
2413
2414 fail5:
2415         EFSYS_PROBE(fail5);
2416 fail4:
2417         EFSYS_PROBE(fail4);
2418 fail3:
2419         EFSYS_PROBE(fail3);
2420 fail2:
2421         EFSYS_PROBE(fail2);
2422
2423         ef10_nic_free_piobufs(enp);
2424
2425 fail1:
2426         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2427
2428         return (rc);
2429 }
2430
2431         __checkReturn   efx_rc_t
2432 ef10_nic_get_vi_pool(
2433         __in            efx_nic_t *enp,
2434         __out           uint32_t *vi_countp)
2435 {
2436         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
2437
2438         /*
2439          * Report VIs that the client driver can use.
2440          * Do not include VIs used for PIO buffer writes.
2441          */
2442         *vi_countp = enp->en_arch.ef10.ena_pio_write_vi_base;
2443
2444         return (0);
2445 }
2446
2447         __checkReturn   efx_rc_t
2448 ef10_nic_get_bar_region(
2449         __in            efx_nic_t *enp,
2450         __in            efx_nic_region_t region,
2451         __out           uint32_t *offsetp,
2452         __out           size_t *sizep)
2453 {
2454         efx_rc_t rc;
2455
2456         EFSYS_ASSERT(EFX_FAMILY_IS_EF10(enp));
2457
2458         /*
2459          * TODO: Specify host memory mapping alignment and granularity
2460          * in efx_drv_limits_t so that they can be taken into account
2461          * when allocating extra VIs for PIO writes.
2462          */
2463         switch (region) {
2464         case EFX_REGION_VI:
2465                 /* UC mapped memory BAR region for VI registers */
2466                 *offsetp = enp->en_arch.ef10.ena_uc_mem_map_offset;
2467                 *sizep = enp->en_arch.ef10.ena_uc_mem_map_size;
2468                 break;
2469
2470         case EFX_REGION_PIO_WRITE_VI:
2471                 /* WC mapped memory BAR region for piobuf writes */
2472                 *offsetp = enp->en_arch.ef10.ena_wc_mem_map_offset;
2473                 *sizep = enp->en_arch.ef10.ena_wc_mem_map_size;
2474                 break;
2475
2476         default:
2477                 rc = EINVAL;
2478                 goto fail1;
2479         }
2480
2481         return (0);
2482
2483 fail1:
2484         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2485
2486         return (rc);
2487 }
2488
2489         __checkReturn   boolean_t
2490 ef10_nic_hw_unavailable(
2491         __in            efx_nic_t *enp)
2492 {
2493         efx_dword_t dword;
2494
2495         if (enp->en_reset_flags & EFX_RESET_HW_UNAVAIL)
2496                 return (B_TRUE);
2497
2498         EFX_BAR_READD(enp, ER_DZ_BIU_MC_SFT_STATUS_REG, &dword, B_FALSE);
2499         if (EFX_DWORD_FIELD(dword, EFX_DWORD_0) == 0xffffffff)
2500                 goto unavail;
2501
2502         return (B_FALSE);
2503
2504 unavail:
2505         ef10_nic_set_hw_unavailable(enp);
2506
2507         return (B_TRUE);
2508 }
2509
2510                         void
2511 ef10_nic_set_hw_unavailable(
2512         __in            efx_nic_t *enp)
2513 {
2514         EFSYS_PROBE(hw_unavail);
2515         enp->en_reset_flags |= EFX_RESET_HW_UNAVAIL;
2516 }
2517
2518
2519                         void
2520 ef10_nic_fini(
2521         __in            efx_nic_t *enp)
2522 {
2523         uint32_t i;
2524         efx_rc_t rc;
2525         boolean_t do_vadaptor_free = B_TRUE;
2526
2527 #if EFSYS_OPT_EVB
2528         if (enp->en_vswitchp != NULL) {
2529                 /*
2530                  * For SR-IOV the vAdaptor is freed with the vswitch,
2531                  * so do not free it here.
2532                  */
2533                 do_vadaptor_free = B_FALSE;
2534         }
2535 #endif
2536         if (do_vadaptor_free != B_FALSE) {
2537                 (void) efx_mcdi_vadaptor_free(enp, enp->en_vport_id);
2538                 enp->en_vport_id = EVB_PORT_ID_NULL;
2539         }
2540
2541         /* Unlink piobufs from extra VIs in WC mapping */
2542         if (enp->en_arch.ef10.ena_piobuf_count > 0) {
2543                 for (i = 0; i < enp->en_arch.ef10.ena_piobuf_count; i++) {
2544                         rc = efx_mcdi_unlink_piobuf(enp,
2545                             enp->en_arch.ef10.ena_pio_write_vi_base + i);
2546                         if (rc != 0)
2547                                 break;
2548                 }
2549         }
2550
2551         ef10_nic_free_piobufs(enp);
2552
2553         (void) efx_mcdi_free_vis(enp);
2554         enp->en_arch.ef10.ena_vi_count = 0;
2555 }
2556
2557                         void
2558 ef10_nic_unprobe(
2559         __in            efx_nic_t *enp)
2560 {
2561 #if EFSYS_OPT_MON_STATS
2562         mcdi_mon_cfg_free(enp);
2563 #endif /* EFSYS_OPT_MON_STATS */
2564         (void) efx_mcdi_drv_attach(enp, B_FALSE);
2565 }
2566
2567 #if EFSYS_OPT_DIAG
2568
2569         __checkReturn   efx_rc_t
2570 ef10_nic_register_test(
2571         __in            efx_nic_t *enp)
2572 {
2573         efx_rc_t rc;
2574
2575         /* FIXME */
2576         _NOTE(ARGUNUSED(enp))
2577         _NOTE(CONSTANTCONDITION)
2578         if (B_FALSE) {
2579                 rc = ENOTSUP;
2580                 goto fail1;
2581         }
2582         /* FIXME */
2583
2584         return (0);
2585
2586 fail1:
2587         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2588
2589         return (rc);
2590 }
2591
2592 #endif  /* EFSYS_OPT_DIAG */
2593
2594 #if EFSYS_OPT_FW_SUBVARIANT_AWARE
2595
2596         __checkReturn   efx_rc_t
2597 efx_mcdi_get_nic_global(
2598         __in            efx_nic_t *enp,
2599         __in            uint32_t key,
2600         __out           uint32_t *valuep)
2601 {
2602         efx_mcdi_req_t req;
2603         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_GET_NIC_GLOBAL_IN_LEN,
2604                 MC_CMD_GET_NIC_GLOBAL_OUT_LEN);
2605         efx_rc_t rc;
2606
2607         req.emr_cmd = MC_CMD_GET_NIC_GLOBAL;
2608         req.emr_in_buf = payload;
2609         req.emr_in_length = MC_CMD_GET_NIC_GLOBAL_IN_LEN;
2610         req.emr_out_buf = payload;
2611         req.emr_out_length = MC_CMD_GET_NIC_GLOBAL_OUT_LEN;
2612
2613         MCDI_IN_SET_DWORD(req, GET_NIC_GLOBAL_IN_KEY, key);
2614
2615         efx_mcdi_execute(enp, &req);
2616
2617         if (req.emr_rc != 0) {
2618                 rc = req.emr_rc;
2619                 goto fail1;
2620         }
2621
2622         if (req.emr_out_length_used != MC_CMD_GET_NIC_GLOBAL_OUT_LEN) {
2623                 rc = EMSGSIZE;
2624                 goto fail2;
2625         }
2626
2627         *valuep = MCDI_OUT_DWORD(req, GET_NIC_GLOBAL_OUT_VALUE);
2628
2629         return (0);
2630
2631 fail2:
2632         EFSYS_PROBE(fail2);
2633 fail1:
2634         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2635
2636         return (rc);
2637 }
2638
2639         __checkReturn   efx_rc_t
2640 efx_mcdi_set_nic_global(
2641         __in            efx_nic_t *enp,
2642         __in            uint32_t key,
2643         __in            uint32_t value)
2644 {
2645         efx_mcdi_req_t req;
2646         EFX_MCDI_DECLARE_BUF(payload, MC_CMD_SET_NIC_GLOBAL_IN_LEN, 0);
2647         efx_rc_t rc;
2648
2649         req.emr_cmd = MC_CMD_SET_NIC_GLOBAL;
2650         req.emr_in_buf = payload;
2651         req.emr_in_length = MC_CMD_SET_NIC_GLOBAL_IN_LEN;
2652         req.emr_out_buf = NULL;
2653         req.emr_out_length = 0;
2654
2655         MCDI_IN_SET_DWORD(req, SET_NIC_GLOBAL_IN_KEY, key);
2656         MCDI_IN_SET_DWORD(req, SET_NIC_GLOBAL_IN_VALUE, value);
2657
2658         efx_mcdi_execute(enp, &req);
2659
2660         if (req.emr_rc != 0) {
2661                 rc = req.emr_rc;
2662                 goto fail1;
2663         }
2664
2665         return (0);
2666
2667 fail1:
2668         EFSYS_PROBE1(fail1, efx_rc_t, rc);
2669
2670         return (rc);
2671 }
2672
2673 #endif  /* EFSYS_OPT_FW_SUBVARIANT_AWARE */
2674
2675 #endif  /* EFX_OPTS_EF10() */