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